From 6005ad9eb4f0b68a55230a87bf8a11e6f4c915ad Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Fri, 22 May 2020 22:36:47 +0200 Subject: [PATCH] Create a connection interface, use it in gotICE. --- client.go | 27 ++++++++++++++++----------- conn.go | 12 ++++++++++++ 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/client.go b/client.go index 9b5713e..c68455f 100644 --- a/client.go +++ b/client.go @@ -654,6 +654,18 @@ func getDownConn(c *client, id string) *downConnection { return conn } +func getConn(c *client, id string) connection { + up := getUpConn(c, id) + if up != nil { + return up + } + down := getDownConn(c, id) + if down != nil { + return down + } + return nil +} + func addDownConn(c *client, id string, remote *upConnection) (*downConnection, error) { pc, err := groups.api.NewPeerConnection(iceConfiguration()) if err != nil { @@ -1065,18 +1077,11 @@ func gotAnswer(c *client, id string, answer webrtc.SessionDescription) error { } func gotICE(c *client, candidate *webrtc.ICECandidateInit, id string) error { - var pc *webrtc.PeerConnection - down := getDownConn(c, id) - if down != nil { - pc = down.pc - } else { - up := getUpConn(c, id) - if up == nil { - return errors.New("unknown id in ICE") - } - pc = up.pc + conn := getConn(c, id) + if conn == nil { + return errors.New("unknown id in ICE") } - return pc.AddICECandidate(*candidate) + return conn.getPC().AddICECandidate(*candidate) } func (c *client) setRequested(requested map[string]uint32) error { diff --git a/conn.go b/conn.go index 2c7646f..2223da2 100644 --- a/conn.go +++ b/conn.go @@ -16,6 +16,10 @@ import ( "github.com/pion/webrtc/v2" ) +type connection interface { + getPC() *webrtc.PeerConnection +} + type upTrack struct { track *webrtc.Track label string @@ -95,6 +99,10 @@ type upConnection struct { iceCandidates []*webrtc.ICECandidateInit } +func (up *upConnection) getPC() *webrtc.PeerConnection { + return up.pc +} + func getUpMid(pc *webrtc.PeerConnection, track *webrtc.Track) string { for _, t := range pc.GetTransceivers() { if t.Receiver() != nil && t.Receiver().Track() == track { @@ -189,3 +197,7 @@ type downConnection struct { tracks []*downTrack iceCandidates []*webrtc.ICECandidateInit } + +func (down *downConnection) getPC() *webrtc.PeerConnection { + return down.pc +}