From 487a3fc3fb405625e0641469513f1ec5be31b8d9 Mon Sep 17 00:00:00 2001 From: Raja Subramanian Date: Tue, 22 Oct 2024 20:23:55 +0530 Subject: [PATCH] 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. --- pkg/rtc/room.go | 8 +++---- pkg/rtc/transport.go | 6 +++-- pkg/rtc/types/ice.go | 52 +++++++++++++++++++++++++------------------- 3 files changed, 38 insertions(+), 28 deletions(-) diff --git a/pkg/rtc/room.go b/pkg/rtc/room.go index c3d643f7..0b19fa25 100644 --- a/pkg/rtc/room.go +++ b/pkg/rtc/room.go @@ -1816,8 +1816,8 @@ func connectionDetailsFields(infos []*types.ICEConnectionInfo) []interface{} { candidates := make([]string, 0, len(info.Remote)+len(info.Local)) for _, c := range info.Local { cStr := "[local]" - if c.Selected { - cStr += "[selected]" + if c.SelectedOrder != 0 { + cStr += fmt.Sprintf("[selected:%d]", c.SelectedOrder) } else if c.Filtered { cStr += "[filtered]" } @@ -1829,8 +1829,8 @@ func connectionDetailsFields(infos []*types.ICEConnectionInfo) []interface{} { } for _, c := range info.Remote { cStr := "[remote]" - if c.Selected { - cStr += "[selected]" + if c.SelectedOrder != 0 { + cStr += fmt.Sprintf("[selected:%d]", c.SelectedOrder) } else if c.Filtered { cStr += "[filtered]" } diff --git a/pkg/rtc/transport.go b/pkg/rtc/transport.go index 798fd8cb..a6db38c5 100644 --- a/pkg/rtc/transport.go +++ b/pkg/rtc/transport.go @@ -1413,7 +1413,7 @@ func (t *PCTransport) handleRemoteICECandidate(e event) error { filtered = true } - t.connectionDetails.AddRemoteCandidate(*c, filtered, true) + t.connectionDetails.AddRemoteCandidate(*c, filtered, true, false) if filtered { return nil } @@ -1425,6 +1425,7 @@ func (t *PCTransport) handleRemoteICECandidate(e event) error { if t.params.DropRemoteICECandidates { t.params.Logger.Debugw("dropping remote ICE candidate", "candidate", c.Candidate) + t.connectionDetails.AddRemoteCandidate(*c, true, true, true) return nil } @@ -1494,7 +1495,7 @@ func (t *PCTransport) filterCandidates(sd webrtc.SessionDescription, preferTCP, if isLocal { t.connectionDetails.AddLocalICECandidate(c, excluded, false) } else { - t.connectionDetails.AddRemoteICECandidate(c, excluded, false) + t.connectionDetails.AddRemoteICECandidate(c, excluded, false, false) } } else { filteredAttrs = append(filteredAttrs, a) @@ -1700,6 +1701,7 @@ func (t *PCTransport) setRemoteDescription(sd webrtc.SessionDescription) error { for _, c := range t.pendingRemoteCandidates { if t.params.DropRemoteICECandidates { + t.connectionDetails.AddRemoteCandidate(*c, true, true, true) continue } if err := t.pc.AddICECandidate(*c); err != nil { diff --git a/pkg/rtc/types/ice.go b/pkg/rtc/types/ice.go index 3b02b4f4..96c9365d 100644 --- a/pkg/rtc/types/ice.go +++ b/pkg/rtc/types/ice.go @@ -40,11 +40,11 @@ const ( type ICECandidateExtended struct { // only one of local or remote is set. This is due to type foo in Pion - Local *webrtc.ICECandidate - Remote ice.Candidate - Selected bool - Filtered bool - Trickle bool + Local *webrtc.ICECandidate + Remote ice.Candidate + SelectedOrder int + Filtered bool + Trickle bool } // -------------------------------------------- @@ -64,8 +64,9 @@ func (i *ICEConnectionInfo) HasCandidates() bool { type ICEConnectionDetails struct { ICEConnectionInfo - lock sync.Mutex - logger logger.Logger + lock sync.Mutex + selectedCount int + logger logger.Logger } func NewICEConnectionDetails(transport livekit.SignalTarget, l logger.Logger) *ICEConnectionDetails { @@ -90,18 +91,18 @@ func (d *ICEConnectionDetails) GetInfo() *ICEConnectionInfo { } for _, c := range d.Local { info.Local = append(info.Local, &ICECandidateExtended{ - Local: c.Local, - Filtered: c.Filtered, - Selected: c.Selected, - Trickle: c.Trickle, + Local: c.Local, + Filtered: c.Filtered, + SelectedOrder: c.SelectedOrder, + Trickle: c.Trickle, }) } for _, c := range d.Remote { info.Remote = append(info.Remote, &ICECandidateExtended{ - Remote: c.Remote, - Filtered: c.Filtered, - Selected: c.Selected, - Trickle: c.Trickle, + Remote: c.Remote, + Filtered: c.Filtered, + SelectedOrder: c.SelectedOrder, + Trickle: c.Trickle, }) } return info @@ -133,16 +134,16 @@ func (d *ICEConnectionDetails) AddLocalICECandidate(c ice.Candidate, filtered, t 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) if err != nil { d.logger.Errorw("could not unmarshal candidate", err, "candidate", c) 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 { // end-of-candidates candidate return @@ -150,10 +151,14 @@ func (d *ICEConnectionDetails) AddRemoteICECandidate(candidate ice.Candidate, fi d.lock.Lock() defer d.lock.Unlock() - compFn := func(e *ICECandidateExtended) bool { + indexFn := func(e *ICECandidateExtended) bool { 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 } d.Remote = append(d.Remote, &ICECandidateExtended{ @@ -174,6 +179,9 @@ func (d *ICEConnectionDetails) Clear() { func (d *ICEConnectionDetails) SetSelectedPair(pair *webrtc.ICECandidatePair) { d.lock.Lock() defer d.lock.Unlock() + + d.selectedCount++ + remoteIdx := slices.IndexFunc(d.Remote, func(e *ICECandidateExtended) bool { return isICECandidateEqualToCandidate(e.Remote, pair.Remote) }) @@ -195,7 +203,7 @@ func (d *ICEConnectionDetails) SetSelectedPair(pair *webrtc.ICECandidatePair) { remoteIdx = len(d.Remote) - 1 } remote := d.Remote[remoteIdx] - remote.Selected = true + remote.SelectedOrder = d.selectedCount localIdx := slices.IndexFunc(d.Local, func(e *ICECandidateExtended) bool { return isCandidateEqualTo(e.Local, pair.Local) @@ -206,7 +214,7 @@ func (d *ICEConnectionDetails) SetSelectedPair(pair *webrtc.ICECandidatePair) { return } local := d.Local[localIdx] - local.Selected = true + local.SelectedOrder = d.selectedCount d.Type = ICEConnectionTypeUDP if pair.Remote.Protocol == webrtc.ICEProtocolTCP {