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
This commit is contained in:
Joe Turki
2025-03-25 17:24:25 +02:00
committed by GitHub
parent 5ce8e05d22
commit 1a1488d290
4 changed files with 33 additions and 8 deletions

View File

@@ -80,7 +80,8 @@ func newICECandidateFromICE(candidate ice.Candidate, sdpMid string, sdpMLineInde
return newCandidate, nil 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 candidateID := c.statsID
switch c.Typ { switch c.Typ {
case ICECandidateTypeHost: case ICECandidateTypeHost:
@@ -216,7 +217,7 @@ func convertTypeFromICE(t ice.CandidateType) (ICECandidateType, error) {
} }
func (c ICECandidate) String() string { func (c ICECandidate) String() string {
ic, err := c.toICE() ic, err := c.ToICE()
if err != nil { if err != nil {
return fmt.Sprintf("%#v failed to convert to ICE: %s", c, err) 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 { func (c ICECandidate) ToJSON() ICECandidateInit {
candidateStr := "" candidateStr := ""
candidate, err := c.toICE() candidate, err := c.ToICE()
if err == nil { if err == nil {
candidateStr = candidate.Marshal() candidateStr = candidate.Marshal()
} }

View File

@@ -130,7 +130,7 @@ func TestICECandidate_Convert(t *testing.T) {
// first copy the candidate ID so it matches the new one // first copy the candidate ID so it matches the new one
testCase.native.statsID = expectedICE.ID() testCase.native.statsID = expectedICE.ID()
actualICE, err := testCase.native.toICE() actualICE, err := testCase.native.ToICE()
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, expectedICE, actualICE, "testCase: %d ice not equal %v", i, actualICE) 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)) 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) { func TestICECandidateSDPMid_ToJSON(t *testing.T) {
candidate := ICECandidate{} candidate := ICECandidate{}
@@ -348,7 +372,7 @@ func TestICECandidateExtensions_ToJSON(t *testing.T) {
assert.Equal(t, sdpMLineIndex, *candidateInit.SDPMLineIndex) assert.Equal(t, sdpMLineIndex, *candidateInit.SDPMLineIndex)
assert.Equal(t, "candidate:"+cand.candidate, candidateInit.Candidate) assert.Equal(t, "candidate:"+cand.candidate, candidateInit.Candidate)
iceBack, err := iceCandidate.toICE() iceBack, err := iceCandidate.ToICE()
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, cand.extensions, iceBack.Extensions()) assert.Equal(t, cand.extensions, iceBack.Extensions())

View File

@@ -298,7 +298,7 @@ func (t *ICETransport) SetRemoteCandidates(remoteCandidates []ICECandidate) erro
} }
for _, c := range remoteCandidates { for _, c := range remoteCandidates {
i, err := c.toICE() i, err := c.ToICE()
if err != nil { if err != nil {
return err return err
} }
@@ -326,7 +326,7 @@ func (t *ICETransport) AddRemoteCandidate(remoteCandidate *ICECandidate) error {
} }
if remoteCandidate != nil { if remoteCandidate != nil {
if candidate, err = remoteCandidate.toICE(); err != nil { if candidate, err = remoteCandidate.ToICE(); err != nil {
return err return err
} }
} }

2
sdp.go
View File

@@ -302,7 +302,7 @@ func addCandidatesToMediaDescriptions(
} }
for _, c := range candidates { for _, c := range candidates {
candidate, err := c.toICE() candidate, err := c.ToICE()
if err != nil { if err != nil {
return err return err
} }