ICE candidate marking (#3128)

- Update filtered if dropping a pending candidate later.
- Ordinal for selected pair so it is easy to see which got selected
  later.
This commit is contained in:
Raja Subramanian
2024-10-22 20:23:55 +05:30
committed by GitHub
parent b30cc9066a
commit 487a3fc3fb
3 changed files with 38 additions and 28 deletions
+4 -4
View File
@@ -1816,8 +1816,8 @@ func connectionDetailsFields(infos []*types.ICEConnectionInfo) []interface{} {
candidates := make([]string, 0, len(info.Remote)+len(info.Local)) candidates := make([]string, 0, len(info.Remote)+len(info.Local))
for _, c := range info.Local { for _, c := range info.Local {
cStr := "[local]" cStr := "[local]"
if c.Selected { if c.SelectedOrder != 0 {
cStr += "[selected]" cStr += fmt.Sprintf("[selected:%d]", c.SelectedOrder)
} else if c.Filtered { } else if c.Filtered {
cStr += "[filtered]" cStr += "[filtered]"
} }
@@ -1829,8 +1829,8 @@ func connectionDetailsFields(infos []*types.ICEConnectionInfo) []interface{} {
} }
for _, c := range info.Remote { for _, c := range info.Remote {
cStr := "[remote]" cStr := "[remote]"
if c.Selected { if c.SelectedOrder != 0 {
cStr += "[selected]" cStr += fmt.Sprintf("[selected:%d]", c.SelectedOrder)
} else if c.Filtered { } else if c.Filtered {
cStr += "[filtered]" cStr += "[filtered]"
} }
+4 -2
View File
@@ -1413,7 +1413,7 @@ func (t *PCTransport) handleRemoteICECandidate(e event) error {
filtered = true filtered = true
} }
t.connectionDetails.AddRemoteCandidate(*c, filtered, true) t.connectionDetails.AddRemoteCandidate(*c, filtered, true, false)
if filtered { if filtered {
return nil return nil
} }
@@ -1425,6 +1425,7 @@ func (t *PCTransport) handleRemoteICECandidate(e event) error {
if t.params.DropRemoteICECandidates { if t.params.DropRemoteICECandidates {
t.params.Logger.Debugw("dropping remote ICE candidate", "candidate", c.Candidate) t.params.Logger.Debugw("dropping remote ICE candidate", "candidate", c.Candidate)
t.connectionDetails.AddRemoteCandidate(*c, true, true, true)
return nil return nil
} }
@@ -1494,7 +1495,7 @@ func (t *PCTransport) filterCandidates(sd webrtc.SessionDescription, preferTCP,
if isLocal { if isLocal {
t.connectionDetails.AddLocalICECandidate(c, excluded, false) t.connectionDetails.AddLocalICECandidate(c, excluded, false)
} else { } else {
t.connectionDetails.AddRemoteICECandidate(c, excluded, false) t.connectionDetails.AddRemoteICECandidate(c, excluded, false, false)
} }
} else { } else {
filteredAttrs = append(filteredAttrs, a) filteredAttrs = append(filteredAttrs, a)
@@ -1700,6 +1701,7 @@ func (t *PCTransport) setRemoteDescription(sd webrtc.SessionDescription) error {
for _, c := range t.pendingRemoteCandidates { for _, c := range t.pendingRemoteCandidates {
if t.params.DropRemoteICECandidates { if t.params.DropRemoteICECandidates {
t.connectionDetails.AddRemoteCandidate(*c, true, true, true)
continue continue
} }
if err := t.pc.AddICECandidate(*c); err != nil { if err := t.pc.AddICECandidate(*c); err != nil {
+18 -10
View File
@@ -42,7 +42,7 @@ type ICECandidateExtended struct {
// only one of local or remote is set. This is due to type foo in Pion // only one of local or remote is set. This is due to type foo in Pion
Local *webrtc.ICECandidate Local *webrtc.ICECandidate
Remote ice.Candidate Remote ice.Candidate
Selected bool SelectedOrder int
Filtered bool Filtered bool
Trickle bool Trickle bool
} }
@@ -65,6 +65,7 @@ func (i *ICEConnectionInfo) HasCandidates() bool {
type ICEConnectionDetails struct { type ICEConnectionDetails struct {
ICEConnectionInfo ICEConnectionInfo
lock sync.Mutex lock sync.Mutex
selectedCount int
logger logger.Logger logger logger.Logger
} }
@@ -92,7 +93,7 @@ func (d *ICEConnectionDetails) GetInfo() *ICEConnectionInfo {
info.Local = append(info.Local, &ICECandidateExtended{ info.Local = append(info.Local, &ICECandidateExtended{
Local: c.Local, Local: c.Local,
Filtered: c.Filtered, Filtered: c.Filtered,
Selected: c.Selected, SelectedOrder: c.SelectedOrder,
Trickle: c.Trickle, Trickle: c.Trickle,
}) })
} }
@@ -100,7 +101,7 @@ func (d *ICEConnectionDetails) GetInfo() *ICEConnectionInfo {
info.Remote = append(info.Remote, &ICECandidateExtended{ info.Remote = append(info.Remote, &ICECandidateExtended{
Remote: c.Remote, Remote: c.Remote,
Filtered: c.Filtered, Filtered: c.Filtered,
Selected: c.Selected, SelectedOrder: c.SelectedOrder,
Trickle: c.Trickle, Trickle: c.Trickle,
}) })
} }
@@ -133,16 +134,16 @@ func (d *ICEConnectionDetails) AddLocalICECandidate(c ice.Candidate, filtered, t
d.AddLocalCandidate(candidate, filtered, trickle) d.AddLocalCandidate(candidate, filtered, trickle)
} }
func (d *ICEConnectionDetails) AddRemoteCandidate(c webrtc.ICECandidateInit, filtered, trickle bool) { func (d *ICEConnectionDetails) AddRemoteCandidate(c webrtc.ICECandidateInit, filtered, trickle, canUpdate bool) {
candidate, err := unmarshalICECandidate(c) candidate, err := unmarshalICECandidate(c)
if err != nil { if err != nil {
d.logger.Errorw("could not unmarshal candidate", err, "candidate", c) d.logger.Errorw("could not unmarshal candidate", err, "candidate", c)
return return
} }
d.AddRemoteICECandidate(candidate, filtered, trickle) d.AddRemoteICECandidate(candidate, filtered, trickle, canUpdate)
} }
func (d *ICEConnectionDetails) AddRemoteICECandidate(candidate ice.Candidate, filtered, trickle bool) { func (d *ICEConnectionDetails) AddRemoteICECandidate(candidate ice.Candidate, filtered, trickle, canUpdate bool) {
if candidate == nil { if candidate == nil {
// end-of-candidates candidate // end-of-candidates candidate
return return
@@ -150,10 +151,14 @@ func (d *ICEConnectionDetails) AddRemoteICECandidate(candidate ice.Candidate, fi
d.lock.Lock() d.lock.Lock()
defer d.lock.Unlock() defer d.lock.Unlock()
compFn := func(e *ICECandidateExtended) bool { indexFn := func(e *ICECandidateExtended) bool {
return isICECandidateEqualTo(e.Remote, candidate) return isICECandidateEqualTo(e.Remote, candidate)
} }
if slices.ContainsFunc(d.Remote, compFn) { if idx := slices.IndexFunc(d.Remote, indexFn); idx != -1 {
if canUpdate {
d.Remote[idx].Filtered = filtered
d.Remote[idx].Trickle = trickle
}
return return
} }
d.Remote = append(d.Remote, &ICECandidateExtended{ d.Remote = append(d.Remote, &ICECandidateExtended{
@@ -174,6 +179,9 @@ func (d *ICEConnectionDetails) Clear() {
func (d *ICEConnectionDetails) SetSelectedPair(pair *webrtc.ICECandidatePair) { func (d *ICEConnectionDetails) SetSelectedPair(pair *webrtc.ICECandidatePair) {
d.lock.Lock() d.lock.Lock()
defer d.lock.Unlock() defer d.lock.Unlock()
d.selectedCount++
remoteIdx := slices.IndexFunc(d.Remote, func(e *ICECandidateExtended) bool { remoteIdx := slices.IndexFunc(d.Remote, func(e *ICECandidateExtended) bool {
return isICECandidateEqualToCandidate(e.Remote, pair.Remote) return isICECandidateEqualToCandidate(e.Remote, pair.Remote)
}) })
@@ -195,7 +203,7 @@ func (d *ICEConnectionDetails) SetSelectedPair(pair *webrtc.ICECandidatePair) {
remoteIdx = len(d.Remote) - 1 remoteIdx = len(d.Remote) - 1
} }
remote := d.Remote[remoteIdx] remote := d.Remote[remoteIdx]
remote.Selected = true remote.SelectedOrder = d.selectedCount
localIdx := slices.IndexFunc(d.Local, func(e *ICECandidateExtended) bool { localIdx := slices.IndexFunc(d.Local, func(e *ICECandidateExtended) bool {
return isCandidateEqualTo(e.Local, pair.Local) return isCandidateEqualTo(e.Local, pair.Local)
@@ -206,7 +214,7 @@ func (d *ICEConnectionDetails) SetSelectedPair(pair *webrtc.ICECandidatePair) {
return return
} }
local := d.Local[localIdx] local := d.Local[localIdx]
local.Selected = true local.SelectedOrder = d.selectedCount
d.Type = ICEConnectionTypeUDP d.Type = ICEConnectionTypeUDP
if pair.Remote.Protocol == webrtc.ICEProtocolTCP { if pair.Remote.Protocol == webrtc.ICEProtocolTCP {