From 0cb09ee9457cb2e492ed7698fdc752a3b762a9b3 Mon Sep 17 00:00:00 2001 From: David Zhao Date: Tue, 2 Feb 2021 23:55:56 -0800 Subject: [PATCH] handle cid changes between publish intent and onTrack --- cmd/cli/client/client.go | 2 ++ pkg/rtc/participant.go | 20 +++++++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/cmd/cli/client/client.go b/cmd/cli/client/client.go index 72f083c24..fb13d50a1 100644 --- a/cmd/cli/client/client.go +++ b/cmd/cli/client/client.go @@ -589,4 +589,6 @@ func (c *RTCClient) SendNacks(count int) { }) } c.lock.Unlock() + + c.PeerConn.WriteRTCP(packets) } diff --git a/pkg/rtc/participant.go b/pkg/rtc/participant.go index fdb4cc5e2..0dba3cb48 100644 --- a/pkg/rtc/participant.go +++ b/pkg/rtc/participant.go @@ -532,7 +532,7 @@ func (p *ParticipantImpl) updateState(state livekit.ParticipantInfo_State) { func (p *ParticipantImpl) onMediaTrack(track *webrtc.TrackRemote, rtpReceiver *webrtc.RTPReceiver) { logger.Debugw("mediaTrack added", "participant", p.Identity(), "remoteTrack", track.ID()) - ti := p.popPendingTrack(track.ID()) + ti := p.popPendingTrack(track.ID(), ToProtoTrackKind(track.Kind())) if ti == nil { return } @@ -552,7 +552,7 @@ func (p *ParticipantImpl) onDataChannel(dc *webrtc.DataChannel) { logger.Debugw("dataChannel added", "participant", p.Identity(), "label", dc.Label()) // data channels have numeric ids, so we use its label to identify - ti := p.popPendingTrack(dc.Label()) + ti := p.popPendingTrack(dc.Label(), livekit.TrackType_DATA) if ti == nil { return } @@ -563,10 +563,24 @@ func (p *ParticipantImpl) onDataChannel(dc *webrtc.DataChannel) { p.handleTrackPublished(dt) } -func (p *ParticipantImpl) popPendingTrack(clientId string) *livekit.TrackInfo { +func (p *ParticipantImpl) popPendingTrack(clientId string, kind livekit.TrackType) *livekit.TrackInfo { p.lock.Lock() defer p.lock.Unlock() ti := p.pendingTracks[clientId] + + // then find the first one that matches type. with MediaStreamTrack, it's possible for the client id to + // change after being added to PeerConnection + if ti == nil { + for cid, info := range p.pendingTracks { + if info.Type == kind { + ti = info + clientId = cid + break + } + } + } + + // if still not found, we are done if ti == nil { logger.Errorw("track info not published prior to track", "clientId", clientId) } else {