Use atomic to store codec. (#3505)

* Use atomic to store codec.

It can change on up stream codec change, but not seeing any racy
behaviour with atomic access.

Reverting the previous change to mute with this change.

* no mime arg
This commit is contained in:
Raja Subramanian
2025-03-09 11:51:48 +05:30
committed by GitHub
parent 7f6afe05ad
commit 6c04909f88
2 changed files with 22 additions and 30 deletions
+2 -2
View File
@@ -183,8 +183,6 @@ func (t *MediaTrackSubscriptions) AddSubscriber(sub types.LocalParticipant, wr *
}() }()
} }
} }
subTrack.SetPublisherMuted(t.params.MediaTrack.IsMuted())
}) })
downTrack.OnBinding(func(err error) { downTrack.OnBinding(func(err error) {
if err != nil { if err != nil {
@@ -205,6 +203,8 @@ func (t *MediaTrackSubscriptions) AddSubscriber(sub types.LocalParticipant, wr *
} }
go subTrack.Bound(nil) go subTrack.Bound(nil)
subTrack.SetPublisherMuted(t.params.MediaTrack.IsMuted())
}) })
downTrack.OnStatsUpdate(func(_ *sfu.DownTrack, stat *livekit.AnalyticsStat) { downTrack.OnStatsUpdate(func(_ *sfu.DownTrack, stat *livekit.AnalyticsStat) {
+20 -28
View File
@@ -267,7 +267,7 @@ type DownTrack struct {
forwarder *Forwarder forwarder *Forwarder
upstreamCodecs []webrtc.RTPCodecParameters upstreamCodecs []webrtc.RTPCodecParameters
codec webrtc.RTPCodecCapability codec atomic.Value // webrtc.RTPCodecCapability
clockRate uint32 clockRate uint32
negotiatedCodecParameters []webrtc.RTPCodecParameters negotiatedCodecParameters []webrtc.RTPCodecParameters
@@ -362,7 +362,6 @@ func NewDownTrack(params DowntrackParams) (*DownTrack, error) {
id: params.Receiver.TrackID(), id: params.Receiver.TrackID(),
upstreamCodecs: codecs, upstreamCodecs: codecs,
kind: kind, kind: kind,
codec: codecs[0].RTPCodecCapability,
clockRate: codecs[0].ClockRate, clockRate: codecs[0].ClockRate,
pacer: params.Pacer, pacer: params.Pacer,
maxLayerNotifierCh: make(chan string, 1), maxLayerNotifierCh: make(chan string, 1),
@@ -370,6 +369,8 @@ func NewDownTrack(params DowntrackParams) (*DownTrack, error) {
createdAt: time.Now().UnixNano(), createdAt: time.Now().UnixNano(),
receiver: params.Receiver, receiver: params.Receiver,
} }
codec := codecs[0].RTPCodecCapability
d.codec.Store(codec)
d.bindState.Store(bindStateUnbound) d.bindState.Store(bindStateUnbound)
d.params.Logger = params.Logger.WithValues( d.params.Logger = params.Logger.WithValues(
"subscriberID", d.SubscriberID(), "subscriberID", d.SubscriberID(),
@@ -382,7 +383,7 @@ func NewDownTrack(params DowntrackParams) (*DownTrack, error) {
mdCacheSize, mdCacheSizeRTX = 8192, 1024 mdCacheSize, mdCacheSizeRTX = 8192, 1024
} }
d.rtpStats = rtpstats.NewRTPStatsSender(rtpstats.RTPStatsParams{ d.rtpStats = rtpstats.NewRTPStatsSender(rtpstats.RTPStatsParams{
ClockRate: d.codec.ClockRate, ClockRate: codec.ClockRate,
Logger: d.params.Logger.WithValues( Logger: d.params.Logger.WithValues(
"stream", "primary", "stream", "primary",
), ),
@@ -390,7 +391,7 @@ func NewDownTrack(params DowntrackParams) (*DownTrack, error) {
d.deltaStatsSenderSnapshotId = d.rtpStats.NewSenderSnapshotId() d.deltaStatsSenderSnapshotId = d.rtpStats.NewSenderSnapshotId()
d.rtpStatsRTX = rtpstats.NewRTPStatsSender(rtpstats.RTPStatsParams{ d.rtpStatsRTX = rtpstats.NewRTPStatsSender(rtpstats.RTPStatsParams{
ClockRate: d.codec.ClockRate, ClockRate: codec.ClockRate,
IsRTX: true, IsRTX: true,
Logger: d.params.Logger.WithValues( Logger: d.params.Logger.WithValues(
"stream", "rtx", "stream", "rtx",
@@ -577,16 +578,15 @@ func (d *DownTrack) Bind(t webrtc.TrackLocalContext) (webrtc.RTPCodecParameters,
d.sequencer = newSequencer(d.params.MaxTrack, d.kind == webrtc.RTPCodecTypeVideo, d.params.Logger) d.sequencer = newSequencer(d.params.MaxTrack, d.kind == webrtc.RTPCodecTypeVideo, d.params.Logger)
d.codec = codec.RTPCodecCapability d.codec.Store(codec.RTPCodecCapability)
if d.onBinding != nil { if d.onBinding != nil {
d.onBinding(nil) d.onBinding(nil)
} }
d.setBindStateLocked(bindStateBound) d.setBindStateLocked(bindStateBound)
mimeType := d.mimeTypeLocked()
d.bindLock.Unlock() d.bindLock.Unlock()
d.forwarder.DetermineCodec(codec.RTPCodecCapability, d.Receiver().HeaderExtensions()) d.forwarder.DetermineCodec(codec.RTPCodecCapability, d.Receiver().HeaderExtensions())
d.connectionStats.Start(mimeType, isFECEnabled) d.connectionStats.Start(d.Mime(), isFECEnabled)
d.params.Logger.Debugw("downtrack bound") d.params.Logger.Debugw("downtrack bound")
} }
@@ -640,7 +640,8 @@ func (d *DownTrack) handleReceiverReady() {
func (d *DownTrack) handleUpstreamCodecChange(mimeType string) { func (d *DownTrack) handleUpstreamCodecChange(mimeType string) {
d.bindLock.Lock() d.bindLock.Lock()
if mime.IsMimeTypeStringEqual(d.codec.MimeType, mimeType) { existingMimeType := d.codec.Load().(webrtc.RTPCodecCapability).MimeType
if mime.IsMimeTypeStringEqual(existingMimeType, mimeType) {
d.bindLock.Unlock() d.bindLock.Unlock()
return return
} }
@@ -681,9 +682,8 @@ func (d *DownTrack) handleUpstreamCodecChange(mimeType string) {
d.payloadType.Store(uint32(codec.PayloadType)) d.payloadType.Store(uint32(codec.PayloadType))
d.payloadTypeRTX.Store(uint32(utils.FindRTXPayloadType(codec.PayloadType, d.negotiatedCodecParameters))) d.payloadTypeRTX.Store(uint32(utils.FindRTXPayloadType(codec.PayloadType, d.negotiatedCodecParameters)))
d.codec = codec.RTPCodecCapability d.codec.Store(codec.RTPCodecCapability)
newMimeType := d.mimeTypeLocked() isFECEnabled := strings.Contains(strings.ToLower(codec.SDPFmtpLine), "fec")
isFECEnabled := strings.Contains(strings.ToLower(d.codec.SDPFmtpLine), "fec")
d.bindLock.Unlock() d.bindLock.Unlock()
d.params.Logger.Infow( d.params.Logger.Infow(
@@ -695,7 +695,7 @@ func (d *DownTrack) handleUpstreamCodecChange(mimeType string) {
d.forwarder.Restart() d.forwarder.Restart()
d.forwarder.DetermineCodec(codec.RTPCodecCapability, d.Receiver().HeaderExtensions()) d.forwarder.DetermineCodec(codec.RTPCodecCapability, d.Receiver().HeaderExtensions())
d.connectionStats.UpdateCodec(newMimeType, isFECEnabled) d.connectionStats.UpdateCodec(d.Mime(), isFECEnabled)
} }
// Unbind implements the teardown logic when the track is no longer needed. This happens // Unbind implements the teardown logic when the track is no longer needed. This happens
@@ -745,19 +745,11 @@ func (d *DownTrack) ID() string { return string(d.id) }
// Codec returns current track codec capability // Codec returns current track codec capability
func (d *DownTrack) Codec() webrtc.RTPCodecCapability { func (d *DownTrack) Codec() webrtc.RTPCodecCapability {
d.bindLock.Lock() return d.codec.Load().(webrtc.RTPCodecCapability)
defer d.bindLock.Unlock()
return d.codec
} }
func (d *DownTrack) Mime() mime.MimeType { func (d *DownTrack) Mime() mime.MimeType {
d.bindLock.Lock() return mime.NormalizeMimeType(d.codec.Load().(webrtc.RTPCodecCapability).MimeType)
defer d.bindLock.Unlock()
return d.mimeTypeLocked()
}
func (d *DownTrack) mimeTypeLocked() mime.MimeType {
return mime.NormalizeMimeType(d.codec.MimeType)
} }
// StreamID is the group this track belongs too. This must be unique // StreamID is the group this track belongs too. This must be unique
@@ -1266,7 +1258,7 @@ func (d *DownTrack) handleMute(muted bool, changed bool) {
// mute too. // mute too.
d.blankFramesGeneration.Inc() d.blankFramesGeneration.Inc()
if d.kind == webrtc.RTPCodecTypeAudio && muted { if d.kind == webrtc.RTPCodecTypeAudio && muted {
d.writeBlankFrameRTP(RTPBlankFramesMuteSeconds, d.blankFramesGeneration.Load(), d.Mime()) d.writeBlankFrameRTP(RTPBlankFramesMuteSeconds, d.blankFramesGeneration.Load())
} }
} }
@@ -1301,7 +1293,7 @@ func (d *DownTrack) CloseWithFlush(flush bool) {
// Otherwise, with transceiver re-use last frame from previous stream is held in the // Otherwise, with transceiver re-use last frame from previous stream is held in the
// display buffer and there could be a brief moment where the previous stream is displayed. // display buffer and there could be a brief moment where the previous stream is displayed.
if flush { if flush {
doneFlushing := d.writeBlankFrameRTP(RTPBlankFramesCloseSeconds, d.blankFramesGeneration.Inc(), d.mimeTypeLocked()) doneFlushing := d.writeBlankFrameRTP(RTPBlankFramesCloseSeconds, d.blankFramesGeneration.Inc())
// wait a limited time to flush // wait a limited time to flush
timer := time.NewTimer(flushTimeout) timer := time.NewTimer(flushTimeout)
@@ -1330,7 +1322,6 @@ func (d *DownTrack) CloseWithFlush(flush bool) {
d.rtcpReaderRTX.Close() d.rtcpReaderRTX.Close()
d.rtcpReaderRTX.OnPacket(nil) d.rtcpReaderRTX.OnPacket(nil)
} }
mime := d.codec.MimeType
d.bindLock.Unlock() d.bindLock.Unlock()
d.connectionStats.Close() d.connectionStats.Close()
@@ -1339,7 +1330,7 @@ func (d *DownTrack) CloseWithFlush(flush bool) {
d.rtpStatsRTX.Stop() d.rtpStatsRTX.Stop()
d.params.Logger.Debugw("rtp stats", d.params.Logger.Debugw("rtp stats",
"direction", "downstream", "direction", "downstream",
"mime", mime, "mime", d.Mime().String(),
"ssrc", d.ssrc, "ssrc", d.ssrc,
"stats", d.rtpStats, "stats", d.rtpStats,
"statsRTX", d.rtpStatsRTX, "statsRTX", d.rtpStatsRTX,
@@ -1688,7 +1679,7 @@ func (d *DownTrack) CreateSenderReport() *rtcp.SenderReport {
// not sending RTCP Sender Report for RTX // not sending RTCP Sender Report for RTX
} }
func (d *DownTrack) writeBlankFrameRTP(duration float32, generation uint32, mimeType mime.MimeType) chan struct{} { func (d *DownTrack) writeBlankFrameRTP(duration float32, generation uint32) chan struct{} {
done := make(chan struct{}) done := make(chan struct{})
go func() { go func() {
// don't send if not writable OR nothing has been sent // don't send if not writable OR nothing has been sent
@@ -1697,6 +1688,7 @@ func (d *DownTrack) writeBlankFrameRTP(duration float32, generation uint32, mime
return return
} }
mimeType := d.Mime()
var getBlankFrame func(bool) ([]byte, error) var getBlankFrame func(bool) ([]byte, error)
switch mimeType { switch mimeType {
case mime.MimeTypeOpus: case mime.MimeTypeOpus:
@@ -2354,7 +2346,7 @@ func (d *DownTrack) DebugInfo() map[string]interface{} {
"TrackID": d.id, "TrackID": d.id,
"StreamID": d.params.StreamID, "StreamID": d.params.StreamID,
"SSRC": d.ssrc, "SSRC": d.ssrc,
"MimeType": d.codec.MimeType, "MimeType": d.Mime().String(),
"BindState": d.bindState.Load().(bindState), "BindState": d.bindState.Load().(bindState),
"Muted": d.forwarder.IsMuted(), "Muted": d.forwarder.IsMuted(),
"PubMuted": d.forwarder.IsPubMuted(), "PubMuted": d.forwarder.IsPubMuted(),