From 1a1488d290b7e66a0191ef32828cd507b8a1ba4a Mon Sep 17 00:00:00 2001 From: Joe Turki Date: Tue, 25 Mar 2025 17:24:25 +0200 Subject: [PATCH] Expose ICE candidates ToICE() method Expose ToICE() method on ICE candidates to allow for easier conversion to ice package ICE candidates. Resolve https://github.com/pion/webrtc/issues/3069 --- icecandidate.go | 7 ++++--- icecandidate_test.go | 28 ++++++++++++++++++++++++++-- icetransport.go | 4 ++-- sdp.go | 2 +- 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/icecandidate.go b/icecandidate.go index fb40f33d..db7af840 100644 --- a/icecandidate.go +++ b/icecandidate.go @@ -80,7 +80,8 @@ func newICECandidateFromICE(candidate ice.Candidate, sdpMid string, sdpMLineInde return newCandidate, nil } -func (c ICECandidate) toICE() (cand ice.Candidate, err error) { +// ToICE converts ICECandidate to ice.Candidate. +func (c ICECandidate) ToICE() (cand ice.Candidate, err error) { candidateID := c.statsID switch c.Typ { case ICECandidateTypeHost: @@ -216,7 +217,7 @@ func convertTypeFromICE(t ice.CandidateType) (ICECandidateType, error) { } func (c ICECandidate) String() string { - ic, err := c.toICE() + ic, err := c.ToICE() if err != nil { return fmt.Sprintf("%#v failed to convert to ICE: %s", c, err) } @@ -229,7 +230,7 @@ func (c ICECandidate) String() string { func (c ICECandidate) ToJSON() ICECandidateInit { candidateStr := "" - candidate, err := c.toICE() + candidate, err := c.ToICE() if err == nil { candidateStr = candidate.Marshal() } diff --git a/icecandidate_test.go b/icecandidate_test.go index 689c104c..c3ca6361 100644 --- a/icecandidate_test.go +++ b/icecandidate_test.go @@ -130,7 +130,7 @@ func TestICECandidate_Convert(t *testing.T) { // first copy the candidate ID so it matches the new one testCase.native.statsID = expectedICE.ID() - actualICE, err := testCase.native.toICE() + actualICE, err := testCase.native.ToICE() assert.NoError(t, err) assert.Equal(t, expectedICE, actualICE, "testCase: %d ice not equal %v", i, actualICE) @@ -239,6 +239,30 @@ func TestICECandidateZeroSDPid(t *testing.T) { assert.Equal(t, candidate.SDPMLineIndex, uint16(0)) } +func TestICECandidateString(t *testing.T) { + candidate := ICECandidate{ + Foundation: "foundation", + Priority: 128, + Address: "1.0.0.1", + Protocol: ICEProtocolUDP, + Port: 1234, + Typ: ICECandidateTypeHost, + Component: 1, + } + iceCandidateConfig := ice.CandidateHostConfig{ + Network: "udp", + Address: "1.0.0.1", + Port: 1234, + Component: 1, + Foundation: "foundation", + Priority: 128, + } + iceCandidate, err := ice.NewCandidateHost(&iceCandidateConfig) + assert.NoError(t, err) + + assert.Equal(t, candidate.String(), iceCandidate.String()) +} + func TestICECandidateSDPMid_ToJSON(t *testing.T) { candidate := ICECandidate{} @@ -348,7 +372,7 @@ func TestICECandidateExtensions_ToJSON(t *testing.T) { assert.Equal(t, sdpMLineIndex, *candidateInit.SDPMLineIndex) assert.Equal(t, "candidate:"+cand.candidate, candidateInit.Candidate) - iceBack, err := iceCandidate.toICE() + iceBack, err := iceCandidate.ToICE() assert.NoError(t, err) assert.Equal(t, cand.extensions, iceBack.Extensions()) diff --git a/icetransport.go b/icetransport.go index a0a46542..f8130e10 100644 --- a/icetransport.go +++ b/icetransport.go @@ -298,7 +298,7 @@ func (t *ICETransport) SetRemoteCandidates(remoteCandidates []ICECandidate) erro } for _, c := range remoteCandidates { - i, err := c.toICE() + i, err := c.ToICE() if err != nil { return err } @@ -326,7 +326,7 @@ func (t *ICETransport) AddRemoteCandidate(remoteCandidate *ICECandidate) error { } if remoteCandidate != nil { - if candidate, err = remoteCandidate.toICE(); err != nil { + if candidate, err = remoteCandidate.ToICE(); err != nil { return err } } diff --git a/sdp.go b/sdp.go index b41d3bfe..fa10b695 100644 --- a/sdp.go +++ b/sdp.go @@ -302,7 +302,7 @@ func addCandidatesToMediaDescriptions( } for _, c := range candidates { - candidate, err := c.toICE() + candidate, err := c.ToICE() if err != nil { return err }