Skip to content

Commit

Permalink
API: Use ICECandidateInit in AddICECandidate
Browse files Browse the repository at this point in the history
Resolves #358
  • Loading branch information
backkem committed Feb 21, 2019
1 parent ddcef2d commit 0e619e2
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 7 deletions.
9 changes: 9 additions & 0 deletions icecandidateinit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package webrtc

// ICECandidateInit is used to serialize ice candidates
type ICECandidateInit struct {
Candidate string `json:"candidate"`
SDPMid *string `json:"sdpMid,omitempty"`
SDPMLineIndex *uint16 `json:"sdpMLineIndex,omitempty"`
UsernameFragment string `json:"usernameFragment"`
}
53 changes: 53 additions & 0 deletions icecandidateinit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package webrtc

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
)

func TestICECandidateInit_Serialization(t *testing.T) {
tt := []struct {
candidate ICECandidateInit
serialized string
}{
{ICECandidateInit{
Candidate: "candidate:abc123",
SDPMid: refString("0"),
SDPMLineIndex: refUint16(0),
UsernameFragment: "def",
}, `{"candidate":"candidate:abc123","sdpMid":"0","sdpMLineIndex":0,"usernameFragment":"def"}`},
{ICECandidateInit{
Candidate: "candidate:abc123",
UsernameFragment: "def",
}, `{"candidate":"candidate:abc123","usernameFragment":"def"}`},
}

for i, tc := range tt {
b, err := json.Marshal(tc.candidate)
if err != nil {
t.Errorf("Failed to marshal %d: %v", i, err)
}
actualSerialized := string(b)
if actualSerialized != tc.serialized {
t.Errorf("%d expected %s got %s", i, tc.serialized, actualSerialized)
}

var actual ICECandidateInit
err = json.Unmarshal(b, &actual)
if err != nil {
t.Errorf("Failed to unmarshal %d: %v", i, err)
}

assert.Equal(t, tc.candidate, actual, "should match")
}
}

func refString(s string) *string {
return &s
}

func refUint16(i uint16) *uint16 {
return &i
}
12 changes: 5 additions & 7 deletions peerconnection.go
Original file line number Diff line number Diff line change
Expand Up @@ -1030,26 +1030,24 @@ func (pc *PeerConnection) RemoteDescription() *SessionDescription {

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

s = strings.TrimPrefix(s, "candidate:")

attribute := sdp.NewAttribute("candidate", s)
candidateValue := strings.TrimPrefix(candidate.Candidate, "candidate:")
attribute := sdp.NewAttribute("candidate", candidateValue)
sdpCandidate, err := attribute.ToICECandidate()
if err != nil {
return err
}

candidate, err := newICECandidateFromSDP(sdpCandidate)
iceCandidate, err := newICECandidateFromSDP(sdpCandidate)
if err != nil {
return err
}

return pc.iceTransport.AddRemoteCandidate(candidate)
return pc.iceTransport.AddRemoteCandidate(iceCandidate)
}

// ICEConnectionState returns the ICE connection state of the
Expand Down

0 comments on commit 0e619e2

Please sign in to comment.