Close published tracks on participant close (#2446)

This commit is contained in:
Raja Subramanian
2024-02-05 13:41:41 +05:30
committed by GitHub
parent 7c16ca6a0c
commit b7147efb87
5 changed files with 52 additions and 76 deletions
+11 -12
View File
@@ -56,17 +56,16 @@ type MediaTrackParams struct {
ParticipantID livekit.ParticipantID ParticipantID livekit.ParticipantID
ParticipantIdentity livekit.ParticipantIdentity ParticipantIdentity livekit.ParticipantIdentity
ParticipantVersion uint32 ParticipantVersion uint32
// channel to send RTCP packets to the source BufferFactory *buffer.Factory
RTCPChan chan []rtcp.Packet ReceiverConfig ReceiverConfig
BufferFactory *buffer.Factory SubscriberConfig DirectionConfig
ReceiverConfig ReceiverConfig PLIThrottleConfig config.PLIThrottleConfig
SubscriberConfig DirectionConfig AudioConfig config.AudioConfig
PLIThrottleConfig config.PLIThrottleConfig VideoConfig config.VideoConfig
AudioConfig config.AudioConfig Telemetry telemetry.TelemetryService
VideoConfig config.VideoConfig Logger logger.Logger
Telemetry telemetry.TelemetryService SimTracks map[uint32]SimulcastTrackInfo
Logger logger.Logger OnRTCP func([]rtcp.Packet)
SimTracks map[uint32]SimulcastTrackInfo
} }
func NewMediaTrack(params MediaTrackParams, ti *livekit.TrackInfo) *MediaTrack { func NewMediaTrack(params MediaTrackParams, ti *livekit.TrackInfo) *MediaTrack {
@@ -252,13 +251,13 @@ func (t *MediaTrack) AddReceiver(receiver *webrtc.RTPReceiver, track *webrtc.Tra
ti, ti,
LoggerWithCodecMime(t.params.Logger, mime), LoggerWithCodecMime(t.params.Logger, mime),
twcc, twcc,
t.params.OnRTCP,
t.params.VideoConfig.StreamTracker, t.params.VideoConfig.StreamTracker,
sfu.WithPliThrottleConfig(t.params.PLIThrottleConfig), sfu.WithPliThrottleConfig(t.params.PLIThrottleConfig),
sfu.WithAudioConfig(t.params.AudioConfig), sfu.WithAudioConfig(t.params.AudioConfig),
sfu.WithLoadBalanceThreshold(20), sfu.WithLoadBalanceThreshold(20),
sfu.WithStreamTrackers(), sfu.WithStreamTrackers(),
) )
newWR.SetRTCPCh(t.params.RTCPChan)
newWR.OnCloseHandler(func() { newWR.OnCloseHandler(func() {
t.MediaTrackReceiver.SetClosing() t.MediaTrackReceiver.SetClosing()
t.MediaTrackReceiver.ClearReceiver(mime, false) t.MediaTrackReceiver.ClearReceiver(mime, false)
+10 -32
View File
@@ -158,7 +158,7 @@ type ParticipantImpl struct {
disconnectTimer *time.Timer disconnectTimer *time.Timer
migrationTimer *time.Timer migrationTimer *time.Timer
rtcpCh chan []rtcp.Packet pubRTCPQueue *sutils.OpsQueue
// hold reference for MediaTrack // hold reference for MediaTrack
twcc *twcc.Responder twcc *twcc.Responder
@@ -240,7 +240,7 @@ func NewParticipant(params ParticipantParams) (*ParticipantImpl, error) {
} }
p := &ParticipantImpl{ p := &ParticipantImpl{
params: params, params: params,
rtcpCh: make(chan []rtcp.Packet, 100), pubRTCPQueue: sutils.NewOpsQueue("pub-rtcp", 64, false),
pendingTracks: make(map[string]*pendingTrackInfo), pendingTracks: make(map[string]*pendingTrackInfo),
pendingPublishingTracks: make(map[livekit.TrackID]*pendingTrackInfo), pendingPublishingTracks: make(map[livekit.TrackID]*pendingTrackInfo),
connectedAt: time.Now(), connectedAt: time.Now(),
@@ -1467,7 +1467,7 @@ func (p *ParticipantImpl) onPublisherInitialConnected() {
if p.supervisor != nil { if p.supervisor != nil {
p.supervisor.SetPublisherPeerConnectionConnected(true) p.supervisor.SetPublisherPeerConnectionConnected(true)
} }
go p.publisherRTCPWorker() p.pubRTCPQueue.Start()
} }
func (p *ParticipantImpl) onSubscriberInitialConnected() { func (p *ParticipantImpl) onSubscriberInitialConnected() {
@@ -2000,7 +2000,6 @@ func (p *ParticipantImpl) addMediaTrack(signalCid string, sdpCid string, ti *liv
ParticipantID: p.params.SID, ParticipantID: p.params.SID,
ParticipantIdentity: p.params.Identity, ParticipantIdentity: p.params.Identity,
ParticipantVersion: p.version.Load(), ParticipantVersion: p.version.Load(),
RTCPChan: p.rtcpCh,
BufferFactory: p.params.Config.BufferFactory, BufferFactory: p.params.Config.BufferFactory,
ReceiverConfig: p.params.Config.Receiver, ReceiverConfig: p.params.Config.Receiver,
AudioConfig: p.params.AudioConfig, AudioConfig: p.params.AudioConfig,
@@ -2010,6 +2009,7 @@ func (p *ParticipantImpl) addMediaTrack(signalCid string, sdpCid string, ti *liv
SubscriberConfig: p.params.Config.Subscriber, SubscriberConfig: p.params.Config.Subscriber,
PLIThrottleConfig: p.params.PLIThrottleConfig, PLIThrottleConfig: p.params.PLIThrottleConfig,
SimTracks: p.params.SimTracks, SimTracks: p.params.SimTracks,
OnRTCP: p.postRtcp,
}, ti) }, ti)
mt.OnSubscribedMaxQualityChange(p.onSubscribedMaxQualityChange) mt.OnSubscribedMaxQualityChange(p.onSubscribedMaxQualityChange)
@@ -2117,7 +2117,7 @@ func (p *ParticipantImpl) hasPendingMigratedTrack() bool {
} }
func (p *ParticipantImpl) onUpTrackManagerClose() { func (p *ParticipantImpl) onUpTrackManagerClose() {
p.postRtcp(nil) p.pubRTCPQueue.Stop()
} }
func (p *ParticipantImpl) getPendingTrack(clientId string, kind livekit.TrackType) (string, *livekit.TrackInfo, bool) { func (p *ParticipantImpl) getPendingTrack(clientId string, kind livekit.TrackType) (string, *livekit.TrackInfo, bool) {
@@ -2241,28 +2241,6 @@ func (p *ParticipantImpl) getPublishedTrackBySdpCid(clientId string) types.Media
return nil return nil
} }
func (p *ParticipantImpl) publisherRTCPWorker() {
defer func() {
if r := Recover(p.GetLogger()); r != nil {
os.Exit(1)
}
}()
// read from rtcpChan
for pkts := range p.rtcpCh {
if pkts == nil {
p.pubLogger.Debugw("exiting publisher RTCP worker")
return
}
if err := p.TransportManager.WritePublisherRTCP(pkts); err != nil {
if !IsEOF(err) {
p.pubLogger.Errorw("could not write RTCP to participant", err)
}
}
}
}
func (p *ParticipantImpl) DebugInfo() map[string]interface{} { func (p *ParticipantImpl) DebugInfo() map[string]interface{} {
info := map[string]interface{}{ info := map[string]interface{}{
"ID": p.params.SID, "ID": p.params.SID,
@@ -2291,11 +2269,11 @@ func (p *ParticipantImpl) DebugInfo() map[string]interface{} {
} }
func (p *ParticipantImpl) postRtcp(pkts []rtcp.Packet) { func (p *ParticipantImpl) postRtcp(pkts []rtcp.Packet) {
select { p.pubRTCPQueue.Enqueue(func() {
case p.rtcpCh <- pkts: if err := p.TransportManager.WritePublisherRTCP(pkts); err != nil && !IsEOF(err) {
default: p.pubLogger.Errorw("could not write RTCP to participant", err)
p.params.Logger.Warnw("rtcp channel full", nil) }
} })
} }
func (p *ParticipantImpl) setDowntracksConnected() { func (p *ParticipantImpl) setDowntracksConnected() {
+25 -22
View File
@@ -64,22 +64,37 @@ func NewUpTrackManager(params UpTrackManagerParams) *UpTrackManager {
func (u *UpTrackManager) Close(willBeResumed bool) { func (u *UpTrackManager) Close(willBeResumed bool) {
u.lock.Lock() u.lock.Lock()
u.closed = true if u.closed {
notify := len(u.publishedTracks) == 0 u.lock.Unlock()
u.lock.Unlock() return
// remove all subscribers
for _, t := range u.GetPublishedTracks() {
t.ClearAllReceivers(willBeResumed)
} }
if notify && u.onClose != nil { u.closed = true
u.onClose()
publishedTracks := u.publishedTracks
u.publishedTracks = make(map[livekit.TrackID]types.MediaTrack)
u.lock.Unlock()
for _, t := range publishedTracks {
t.Close(willBeResumed)
}
if onClose := u.getOnUpTrackManagerClose(); onClose != nil {
onClose()
} }
} }
func (u *UpTrackManager) OnUpTrackManagerClose(f func()) { func (u *UpTrackManager) OnUpTrackManagerClose(f func()) {
u.lock.Lock()
u.onClose = f u.onClose = f
u.lock.Unlock()
}
func (u *UpTrackManager) getOnUpTrackManagerClose() func() {
u.lock.RLock()
defer u.lock.RUnlock()
return u.onClose
} }
func (u *UpTrackManager) ToProto() []*livekit.TrackInfo { func (u *UpTrackManager) ToProto() []*livekit.TrackInfo {
@@ -247,22 +262,10 @@ func (u *UpTrackManager) AddPublishedTrack(track types.MediaTrack) {
u.params.Logger.Debugw("added published track", "trackID", track.ID(), "trackInfo", logger.Proto(track.ToProto())) u.params.Logger.Debugw("added published track", "trackID", track.ID(), "trackInfo", logger.Proto(track.ToProto()))
track.AddOnClose(func() { track.AddOnClose(func() {
notifyClose := false
// cleanup
u.lock.Lock() u.lock.Lock()
trackID := track.ID() delete(u.publishedTracks, track.ID())
delete(u.publishedTracks, trackID)
// not modifying subscription permissions, will get reset on next update from participant // not modifying subscription permissions, will get reset on next update from participant
if u.closed && len(u.publishedTracks) == 0 {
notifyClose = true
}
u.lock.Unlock() u.lock.Unlock()
if notifyClose && u.onClose != nil {
u.onClose()
}
}) })
} }
+1 -1
View File
@@ -264,7 +264,7 @@ func (b *Buffer) Bind(params webrtc.RTPParameters, codec webrtc.RTPCodecCapabili
b.bound = true b.bound = true
} }
// Write adds an RTP Packet, out of order, new packet may be arrived later // Write adds an RTP Packet, ordering is not guaranteed, newer packets may arrive later
func (b *Buffer) Write(pkt []byte) (n int, err error) { func (b *Buffer) Write(pkt []byte) (n int, err error) {
b.Lock() b.Lock()
defer b.Unlock() defer b.Unlock()
+5 -9
View File
@@ -108,7 +108,7 @@ type WebRTCReceiver struct {
useTrackers bool useTrackers bool
trackInfo atomic.Pointer[livekit.TrackInfo] trackInfo atomic.Pointer[livekit.TrackInfo]
rtcpCh chan []rtcp.Packet onRTCP func([]rtcp.Packet)
twcc *twcc.Responder twcc *twcc.Responder
@@ -198,6 +198,7 @@ func NewWebRTCReceiver(
trackInfo *livekit.TrackInfo, trackInfo *livekit.TrackInfo,
logger logger.Logger, logger logger.Logger,
twcc *twcc.Responder, twcc *twcc.Responder,
onRTCP func([]rtcp.Packet),
trackersConfig config.StreamTrackersConfig, trackersConfig config.StreamTrackersConfig,
opts ...ReceiverOpts, opts ...ReceiverOpts,
) *WebRTCReceiver { ) *WebRTCReceiver {
@@ -209,6 +210,7 @@ func NewWebRTCReceiver(
codec: track.Codec(), codec: track.Codec(),
kind: track.Kind(), kind: track.Kind(),
twcc: twcc, twcc: twcc,
onRTCP: onRTCP,
isSVC: IsSvcCodec(track.Codec().MimeType), isSVC: IsSvcCodec(track.Codec().MimeType),
isRED: IsRedCodec(track.Codec().MimeType), isRED: IsRedCodec(track.Codec().MimeType),
} }
@@ -514,10 +516,8 @@ func (w *WebRTCReceiver) sendRTCP(packets []rtcp.Packet) {
return return
} }
select { if w.onRTCP != nil {
case w.rtcpCh <- packets: w.onRTCP(packets)
default:
w.logger.Warnw("sendRTCP failed, rtcp channel full", nil)
} }
} }
@@ -531,10 +531,6 @@ func (w *WebRTCReceiver) SendPLI(layer int32, force bool) {
buff.SendPLI(force) buff.SendPLI(force)
} }
func (w *WebRTCReceiver) SetRTCPCh(ch chan []rtcp.Packet) {
w.rtcpCh = ch
}
func (w *WebRTCReceiver) getBuffer(layer int32) *buffer.Buffer { func (w *WebRTCReceiver) getBuffer(layer int32) *buffer.Buffer {
w.bufferMu.RLock() w.bufferMu.RLock()
defer w.bufferMu.RUnlock() defer w.bufferMu.RUnlock()