Skip to content

Commit 1a1488d

Browse files
authored
Expose ICE candidates ToICE() method
Expose ToICE() method on ICE candidates to allow for easier conversion to ice package ICE candidates. Resolve #3069
1 parent 5ce8e05 commit 1a1488d

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

Diff for: icecandidate.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ func newICECandidateFromICE(candidate ice.Candidate, sdpMid string, sdpMLineInde
8080
return newCandidate, nil
8181
}
8282

83-
func (c ICECandidate) toICE() (cand ice.Candidate, err error) {
83+
// ToICE converts ICECandidate to ice.Candidate.
84+
func (c ICECandidate) ToICE() (cand ice.Candidate, err error) {
8485
candidateID := c.statsID
8586
switch c.Typ {
8687
case ICECandidateTypeHost:
@@ -216,7 +217,7 @@ func convertTypeFromICE(t ice.CandidateType) (ICECandidateType, error) {
216217
}
217218

218219
func (c ICECandidate) String() string {
219-
ic, err := c.toICE()
220+
ic, err := c.ToICE()
220221
if err != nil {
221222
return fmt.Sprintf("%#v failed to convert to ICE: %s", c, err)
222223
}
@@ -229,7 +230,7 @@ func (c ICECandidate) String() string {
229230
func (c ICECandidate) ToJSON() ICECandidateInit {
230231
candidateStr := ""
231232

232-
candidate, err := c.toICE()
233+
candidate, err := c.ToICE()
233234
if err == nil {
234235
candidateStr = candidate.Marshal()
235236
}

Diff for: icecandidate_test.go

+26-2
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func TestICECandidate_Convert(t *testing.T) {
130130

131131
// first copy the candidate ID so it matches the new one
132132
testCase.native.statsID = expectedICE.ID()
133-
actualICE, err := testCase.native.toICE()
133+
actualICE, err := testCase.native.ToICE()
134134
assert.NoError(t, err)
135135

136136
assert.Equal(t, expectedICE, actualICE, "testCase: %d ice not equal %v", i, actualICE)
@@ -239,6 +239,30 @@ func TestICECandidateZeroSDPid(t *testing.T) {
239239
assert.Equal(t, candidate.SDPMLineIndex, uint16(0))
240240
}
241241

242+
func TestICECandidateString(t *testing.T) {
243+
candidate := ICECandidate{
244+
Foundation: "foundation",
245+
Priority: 128,
246+
Address: "1.0.0.1",
247+
Protocol: ICEProtocolUDP,
248+
Port: 1234,
249+
Typ: ICECandidateTypeHost,
250+
Component: 1,
251+
}
252+
iceCandidateConfig := ice.CandidateHostConfig{
253+
Network: "udp",
254+
Address: "1.0.0.1",
255+
Port: 1234,
256+
Component: 1,
257+
Foundation: "foundation",
258+
Priority: 128,
259+
}
260+
iceCandidate, err := ice.NewCandidateHost(&iceCandidateConfig)
261+
assert.NoError(t, err)
262+
263+
assert.Equal(t, candidate.String(), iceCandidate.String())
264+
}
265+
242266
func TestICECandidateSDPMid_ToJSON(t *testing.T) {
243267
candidate := ICECandidate{}
244268

@@ -348,7 +372,7 @@ func TestICECandidateExtensions_ToJSON(t *testing.T) {
348372
assert.Equal(t, sdpMLineIndex, *candidateInit.SDPMLineIndex)
349373
assert.Equal(t, "candidate:"+cand.candidate, candidateInit.Candidate)
350374

351-
iceBack, err := iceCandidate.toICE()
375+
iceBack, err := iceCandidate.ToICE()
352376

353377
assert.NoError(t, err)
354378
assert.Equal(t, cand.extensions, iceBack.Extensions())

Diff for: icetransport.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ func (t *ICETransport) SetRemoteCandidates(remoteCandidates []ICECandidate) erro
298298
}
299299

300300
for _, c := range remoteCandidates {
301-
i, err := c.toICE()
301+
i, err := c.ToICE()
302302
if err != nil {
303303
return err
304304
}
@@ -326,7 +326,7 @@ func (t *ICETransport) AddRemoteCandidate(remoteCandidate *ICECandidate) error {
326326
}
327327

328328
if remoteCandidate != nil {
329-
if candidate, err = remoteCandidate.toICE(); err != nil {
329+
if candidate, err = remoteCandidate.ToICE(); err != nil {
330330
return err
331331
}
332332
}

Diff for: sdp.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ func addCandidatesToMediaDescriptions(
302302
}
303303

304304
for _, c := range candidates {
305-
candidate, err := c.toICE()
305+
candidate, err := c.ToICE()
306306
if err != nil {
307307
return err
308308
}

0 commit comments

Comments
 (0)