Skip to content

Commit

Permalink
Single function for newICECandidatesFromICE
Browse files Browse the repository at this point in the history
  • Loading branch information
joeturki committed Jan 8, 2025
1 parent 52f5979 commit 71f7c02
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 50 deletions.
54 changes: 14 additions & 40 deletions icecandidate.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ type ICECandidate struct {

// Conversion for package ice

func newICECandidatesFromICE(iceCandidates []ice.Candidate) ([]ICECandidate, error) {
func newICECandidatesFromICE(iceCandidates []ice.Candidate, sdpMid string, sdpMLineIndex uint16) ([]ICECandidate, error) {
candidates := []ICECandidate{}

for _, i := range iceCandidates {
c, err := newICECandidateFromICE(i)
c, err := newICECandidateFromICE(i, sdpMid, sdpMLineIndex)
if err != nil {
return nil, err
}
Expand All @@ -42,7 +42,7 @@ func newICECandidatesFromICE(iceCandidates []ice.Candidate) ([]ICECandidate, err
return candidates, nil
}

func newICECandidateFromICE(i ice.Candidate) (ICECandidate, error) {
func newICECandidateFromICE(i ice.Candidate, sdpMid string, sdpMLineIndex uint16) (ICECandidate, error) {
typ, err := convertTypeFromICE(i.Type())
if err != nil {
return ICECandidate{}, err
Expand All @@ -53,15 +53,17 @@ func newICECandidateFromICE(i ice.Candidate) (ICECandidate, error) {
}

c := ICECandidate{
statsID: i.ID(),
Foundation: i.Foundation(),
Priority: i.Priority(),
Address: i.Address(),
Protocol: protocol,
Port: uint16(i.Port()),
Component: i.Component(),
Typ: typ,
TCPType: i.TCPType().String(),
statsID: i.ID(),
Foundation: i.Foundation(),
Priority: i.Priority(),
Address: i.Address(),
Protocol: protocol,
Port: uint16(i.Port()),
Component: i.Component(),
Typ: typ,
TCPType: i.TCPType().String(),
SDPMid: sdpMid,
SDPMLineIndex: sdpMLineIndex,
}

if i.RelatedAddress() != nil {
Expand All @@ -72,34 +74,6 @@ func newICECandidateFromICE(i ice.Candidate) (ICECandidate, error) {
return c, nil
}

// newIdentifiedICECandidateFromICE creates a new ICECandidate with the specified media stream identification tag and media description index.
func newIdentifiedICECandidateFromICE(candidate ice.Candidate, sdpMid string, sdpMLineIndex uint16) (ICECandidate, error) {
c, err := newICECandidateFromICE(candidate)
if err != nil {
return ICECandidate{}, err
}

c.SDPMid = sdpMid
c.SDPMLineIndex = sdpMLineIndex

return c, nil
}

// newICECandidatesFromICE creates ICECandidates from ice.Candidates with the specified media stream identification tag and media description index.
func newIdentifiedICECandidatesFromICE(iceCandidates []ice.Candidate, sdpMid string, sdpMLineIndex uint16) ([]ICECandidate, error) {
candidates := []ICECandidate{}

for _, i := range iceCandidates {
c, err := newIdentifiedICECandidateFromICE(i, sdpMid, sdpMLineIndex)
if err != nil {
return nil, err
}
candidates = append(candidates, c)
}

return candidates, nil
}

func (c ICECandidate) toICE() (ice.Candidate, error) {
candidateID := c.statsID
switch c.Typ {
Expand Down
4 changes: 2 additions & 2 deletions icecandidate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func TestNewIdentifiedICECandidateFromICE(t *testing.T) {
ice, err := ice.NewCandidateHost(&config)
assert.NoError(t, err)

ct, err := newIdentifiedICECandidateFromICE(ice, "1", 2)
ct, err := newICECandidateFromICE(ice, "1", 2)
assert.NoError(t, err)

assert.Equal(t, "1", ct.SDPMid)
Expand All @@ -203,7 +203,7 @@ func TestNewIdentifiedICECandidatesFromICE(t *testing.T) {
sdpMid := "1"
sdpMLineIndex := uint16(2)

results, err := newIdentifiedICECandidatesFromICE(candidates, sdpMid, sdpMLineIndex)
results, err := newICECandidatesFromICE(candidates, sdpMid, sdpMLineIndex)

assert.NoError(t, err)

Expand Down
4 changes: 2 additions & 2 deletions icegatherer.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func (g *ICEGatherer) Gather() error {
sdpMLineIndex := uint16(g.sdpMLineIndex.Load())

if candidate != nil {
c, err := newIdentifiedICECandidateFromICE(candidate, sdpMid, sdpMLineIndex)
c, err := newICECandidateFromICE(candidate, sdpMid, sdpMLineIndex)
if err != nil {
g.log.Warnf("Failed to convert ice.Candidate: %s", err)
return
Expand Down Expand Up @@ -292,7 +292,7 @@ func (g *ICEGatherer) GetLocalCandidates() ([]ICECandidate, error) {

sdpMLineIndex := uint16(g.sdpMLineIndex.Load())

return newIdentifiedICECandidatesFromICE(iceCandidates, sdpMid, sdpMLineIndex)
return newICECandidatesFromICE(iceCandidates, sdpMid, sdpMLineIndex)
}

// OnLocalCandidate sets an event handler which fires when a new local ICE candidate is available
Expand Down
6 changes: 3 additions & 3 deletions icetransport.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ func (t *ICETransport) GetSelectedCandidatePair() (*ICECandidatePair, error) {
return nil, err
}

local, err := newICECandidateFromICE(icePair.Local)
local, err := newICECandidateFromICE(icePair.Local, "", 0)
if err != nil {
return nil, err
}

remote, err := newICECandidateFromICE(icePair.Remote)
remote, err := newICECandidateFromICE(icePair.Remote, "", 0)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -118,7 +118,7 @@ func (t *ICETransport) Start(gatherer *ICEGatherer, params ICEParameters, role *
return err
}
if err := agent.OnSelectedCandidatePairChange(func(local, remote ice.Candidate) {
candidates, err := newICECandidatesFromICE([]ice.Candidate{local, remote})
candidates, err := newICECandidatesFromICE([]ice.Candidate{local, remote}, "", 0)
if err != nil {
t.log.Warnf("%w: %s", errICECandiatesCoversionFailed, err)
return
Expand Down
2 changes: 1 addition & 1 deletion peerconnection.go
Original file line number Diff line number Diff line change
Expand Up @@ -1811,7 +1811,7 @@ func (pc *PeerConnection) AddICECandidate(candidate ICECandidateInit) error {
return err
}

c, err := newICECandidateFromICE(candidate)
c, err := newICECandidateFromICE(candidate, "", 0)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion peerconnection_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ func valueToICECandidate(val js.Value) *ICECandidate {
return nil
}

iceCandidate, err := newICECandidateFromICE(c)
iceCandidate, err := newICECandidateFromICE(c, "", 0)
if err != nil {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion sdp.go
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ func extractICEDetailsFromMedia(media *sdp.MediaDescription, log logging.Leveled
return "", "", nil, err
}

candidate, err := newICECandidateFromICE(c)
candidate, err := newICECandidateFromICE(c, "", 0)
if err != nil {
return "", "", nil, err
}
Expand Down

0 comments on commit 71f7c02

Please sign in to comment.