Skip to content

Commit 0e619e2

Browse files
committed
API: Use ICECandidateInit in AddICECandidate
Resolves #358
1 parent ddcef2d commit 0e619e2

File tree

3 files changed

+67
-7
lines changed

3 files changed

+67
-7
lines changed

icecandidateinit.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package webrtc
2+
3+
// ICECandidateInit is used to serialize ice candidates
4+
type ICECandidateInit struct {
5+
Candidate string `json:"candidate"`
6+
SDPMid *string `json:"sdpMid,omitempty"`
7+
SDPMLineIndex *uint16 `json:"sdpMLineIndex,omitempty"`
8+
UsernameFragment string `json:"usernameFragment"`
9+
}

icecandidateinit_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package webrtc
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestICECandidateInit_Serialization(t *testing.T) {
11+
tt := []struct {
12+
candidate ICECandidateInit
13+
serialized string
14+
}{
15+
{ICECandidateInit{
16+
Candidate: "candidate:abc123",
17+
SDPMid: refString("0"),
18+
SDPMLineIndex: refUint16(0),
19+
UsernameFragment: "def",
20+
}, `{"candidate":"candidate:abc123","sdpMid":"0","sdpMLineIndex":0,"usernameFragment":"def"}`},
21+
{ICECandidateInit{
22+
Candidate: "candidate:abc123",
23+
UsernameFragment: "def",
24+
}, `{"candidate":"candidate:abc123","usernameFragment":"def"}`},
25+
}
26+
27+
for i, tc := range tt {
28+
b, err := json.Marshal(tc.candidate)
29+
if err != nil {
30+
t.Errorf("Failed to marshal %d: %v", i, err)
31+
}
32+
actualSerialized := string(b)
33+
if actualSerialized != tc.serialized {
34+
t.Errorf("%d expected %s got %s", i, tc.serialized, actualSerialized)
35+
}
36+
37+
var actual ICECandidateInit
38+
err = json.Unmarshal(b, &actual)
39+
if err != nil {
40+
t.Errorf("Failed to unmarshal %d: %v", i, err)
41+
}
42+
43+
assert.Equal(t, tc.candidate, actual, "should match")
44+
}
45+
}
46+
47+
func refString(s string) *string {
48+
return &s
49+
}
50+
51+
func refUint16(i uint16) *uint16 {
52+
return &i
53+
}

peerconnection.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -1030,26 +1030,24 @@ func (pc *PeerConnection) RemoteDescription() *SessionDescription {
10301030

10311031
// AddICECandidate accepts an ICE candidate string and adds it
10321032
// to the existing set of candidates
1033-
func (pc *PeerConnection) AddICECandidate(s string) error {
1034-
// TODO: AddICECandidate should take ICECandidateInit
1033+
func (pc *PeerConnection) AddICECandidate(candidate ICECandidateInit) error {
10351034
if pc.RemoteDescription() == nil {
10361035
return &rtcerr.InvalidStateError{Err: ErrNoRemoteDescription}
10371036
}
10381037

1039-
s = strings.TrimPrefix(s, "candidate:")
1040-
1041-
attribute := sdp.NewAttribute("candidate", s)
1038+
candidateValue := strings.TrimPrefix(candidate.Candidate, "candidate:")
1039+
attribute := sdp.NewAttribute("candidate", candidateValue)
10421040
sdpCandidate, err := attribute.ToICECandidate()
10431041
if err != nil {
10441042
return err
10451043
}
10461044

1047-
candidate, err := newICECandidateFromSDP(sdpCandidate)
1045+
iceCandidate, err := newICECandidateFromSDP(sdpCandidate)
10481046
if err != nil {
10491047
return err
10501048
}
10511049

1052-
return pc.iceTransport.AddRemoteCandidate(candidate)
1050+
return pc.iceTransport.AddRemoteCandidate(iceCandidate)
10531051
}
10541052

10551053
// ICEConnectionState returns the ICE connection state of the

0 commit comments

Comments
 (0)