From 6e0b7c7e48150c9688b8e3cfcc6b341c02181fd3 Mon Sep 17 00:00:00 2001 From: ZHENK Date: Wed, 9 Dec 2020 19:00:01 +1300 Subject: [PATCH] Pass nil candidate into pion/ice Pass empty string candidate.Candidate into pion/ice to handle. Co-authored-by: JooYoung --- peerconnection.go | 17 ++++++++++++++++- peerconnection_test.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/peerconnection.go b/peerconnection.go index dae2ff7b89f..48a1409e959 100644 --- a/peerconnection.go +++ b/peerconnection.go @@ -1454,13 +1454,28 @@ func (pc *PeerConnection) RemoteDescription() *SessionDescription { } // AddICECandidate accepts an ICE candidate string and adds it -// to the existing set of candidates +// to the existing set of candidates. +// If ICE candidate string is empty, do not adds it to the existing +// set of candidates func (pc *PeerConnection) AddICECandidate(candidate ICECandidateInit) error { if pc.RemoteDescription() == nil { return &rtcerr.InvalidStateError{Err: ErrNoRemoteDescription} } candidateValue := strings.TrimPrefix(candidate.Candidate, "candidate:") + + if candidateValue == "" { + agent := pc.iceTransport.gatherer.getAgent() + if agent == nil { + return fmt.Errorf("%w: unable to add remote candidates", errICEAgentNotExist) + } + + err := agent.AddRemoteCandidate(nil) + if err != nil { + return err + } + } + c, err := ice.UnmarshalCandidate(candidateValue) if err != nil { return err diff --git a/peerconnection_test.go b/peerconnection_test.go index 0a8ac8afc1a..89f598d7ec1 100644 --- a/peerconnection_test.go +++ b/peerconnection_test.go @@ -276,6 +276,43 @@ func TestSetRemoteDescription(t *testing.T) { } } +func TestEmptyCandidate(t *testing.T) { + testCases := []struct { + ICECandidate ICECandidateInit + expectError bool + }{ + {ICECandidateInit{"", nil, nil, nil}, false}, + {ICECandidateInit{ + "211962667 1 udp 2122194687 10.0.3.1 40864 typ host generation 0", + nil, nil, nil, + }, false}, + {ICECandidateInit{ + "1234567", + nil, nil, nil, + }, true}, + } + + for i, testCase := range testCases { + peerConn, err := NewPeerConnection(Configuration{}) + if err != nil { + t.Errorf("Case %d: got error: %v", i, err) + } + + err = peerConn.SetRemoteDescription(SessionDescription{Type: SDPTypeOffer, SDP: minimalOffer}) + if err != nil { + t.Errorf("Case %d: got error: %v", i, err) + } + + if testCase.expectError { + assert.Error(t, peerConn.AddICECandidate(testCase.ICECandidate)) + } else { + assert.NoError(t, peerConn.AddICECandidate(testCase.ICECandidate)) + } + + assert.NoError(t, peerConn.Close()) + } +} + func TestCreateOfferAnswer(t *testing.T) { offerPeerConn, err := NewPeerConnection(Configuration{}) assert.NoError(t, err)