mirror of
https://github.com/livekit/livekit.git
synced 2026-12-04 05:38:09 +00:00
* Do not recover FlexFEC from RTX repaired packets A packet repaired from RTX takes its header from the RTX packet: libwebrtc adds a MID to retransmissions until the RTX SSRC is acknowledged, and sets abs-send-time and transport-cc at send time. The stored packet therefore differs from the media the publisher protected, so using it as a protected packet recovered the wrong length, failing with "invalid recovered packet size", or silently corrupted the recovered packet's header extensions when the length happened to match. Like libwebrtc's FlexfecReceiver, which skips packets recovered by RTX, mark RTX repaired sequence numbers and treat them as missing for FEC recovery. They are still forwarded and update NACK state as before. With a libwebrtc publisher at 10% upstream loss, every recovery attempt in a block containing a retransmitted packet failed; with retransmissions excluded recoveries succeed. * Move RTX repair tracking into BufferBase Keep the RTX repaired history, its lookup and the publisher sequence number extension in BufferBase, so every buffer built on it, such as the relay, can exclude RTX repaired packets from FlexFEC recovery the same way. The history is now cleared with the rest of the sequence number state when the stream restarts. Buffer only marks repaired packets from writeRTX. Packets recovered by FEC also enter with isRTX set, but they are exact reconstructions and stay usable, so marking is explicit rather than tied to that flag. * Keep RTX-repaired history safe with large packet caches The RTX repair history had 512 fixed slots. With a larger video packet cache, e.g. packet_buffer_size_video: 1500, two cached repairs could share a slot, the earlier mark was lost, and FlexFEC could recover from the altered retransmission. - Size the history from the video packet cache, at least 512 slots, and allocate it on the first repair. - When a later repair reuses a slot, the earlier packet counts as repaired: losing a recovery is better than corrupting one. - Do not mark a retransmission of a packet that is already cached. The bucket rejects it as a duplicate and keeps the exact packet, which stays usable for recovery. - Mark a repair before ingesting it and do not feed RTX repaired packets to the FEC decoder, as BufferBase now feeds ingested media to the decoder. * Generate downstream FlexFEC with per-track protection presets Generate FlexFEC-03 repairs for subscribed video tracks. Subscribers select none (default), low, medium or high through UpdateTrackSettings.fec; repairs require rtc.flexfec.downstream_enabled and negotiated flexfec-03 support. Generate repairs at each frame marker, splitting large frames into blocks of at most 48 packets, with upstream Q8 repair-count rounding and at least one repair per block. Reserve measured repair overhead during layer allocation and charge repair traffic to pacing, bandwidth estimation and metrics. Carry repair sequence state through the DownTrack GetState/SeedState handoff, apply subscriber settings from immutable snapshots, and preserve upstream FEC on shared sendrecv codec lists when downstream generation is disabled. This squashes the three commits of dc/feature/fec_down (38a67502,d269ac59,7b02897c) and rebases them onto master. Conflict resolution: - pacer.Packet keeps master's held-extension scratch next to the FEC field, and the DownTrack sets FEC on the pacer packet master now creates before adding extensions - DownTrackParams and subscriber config keep master's RTXBudget - configureSenderParams uses master's enableFlexFEC name - tests follow master: pion/transport v5 packetio, mid-less receivers skipped when restricting codecs, and settings compared by value since they are stored as merged snapshots Requires the UpdateTrackSettings.fec field from livekit/protocol#1794, which is not in the pinned protocol version yet. * Add end-to-end tests for downstream FlexFEC Publish a VP8 track through the SFU to a FlexFEC-capable subscriber, simulate downstream loss before the subscriber's receive buffer, and recover the dropped packets from the repairs actually sent, using the same FlexFEC-03 decoder as upstream recovery. RTX is dropped meanwhile so retransmissions cannot fill the gaps. The test checks that no repairs are sent before a preset is selected, that every preset recovers isolated losses bit-exactly, that presets change without renegotiation, and that a subscriber which declines flexfec-03 receives media but no repairs. Frames alternate between one and five packets, so repair counts match the Q8 rounding for both block sizes. The test client gains a subscriber RTP filter to simulate loss and accessors for the subscriber descriptions. The tests use their own ports so they can run next to a local server on the default ports. * Support downstream FEC protection modes Adopt the FECProtection message from livekit/protocol#1987: subscribers select a mode (disabled, adaptive, fixed, minimum) and a level (low, medium, high; unspecified is low) per track. As before, an update that omits fec keeps the previous setting. Unknown modes disable FEC and unknown levels are treated as unspecified. The modes follow libwebrtc's RtpParameters::fec_protection (webrtc-sdk/webrtc#329): - adaptive ports VCMNackFecMethod: the kFecRateTable lookup with a 10 second max-window loss filter, key frame boost, ConvertFECRate, NACK only delta frames below 20ms RTT, and BitRateTooLowForFec. Inputs are the subscriber's receiver reports, the sent bit and frame rates, and the forwarded layer's dimensions. - fixed uses the level for delta and key frames. - minimum uses the larger of the adaptive rate and the level. - the adaptive state is updated in every mode, so switching to adaptive or minimum starts from the current loss history, and a protection change keeps the group that is accumulating and protects it at the new rate. Key frames get their own protection factor, so pacer packets carry a key frame flag. Repairs are still generated per frame; multi-frame blocks (max_fec_frames) are not supported. The end-to-end tests report the simulated loss in receiver reports, since the test client sends none, and check that adaptive protection sends no repairs without loss and protects with loss, and that minimum protection matches its level without loss and rises with loss. Requires livekit/protocol#1987, which is not in the pinned protocol version yet. * Point protocol at livekit/protocol#1987 * Report loss before repair in receiver reports to publishers Receiver reports counted packets restored by RTX or recovered by FlexFEC as received, so as long as repairs kept up the reported fraction lost stayed at zero, even with 20% upstream loss. libwebrtc receivers exclude recovered packets from their receive statistics, and a publisher's loss-based protection depends on that: with no reported loss, FlexFEC backs off to almost no repairs. Packets that fill a loss through a repair are now counted separately, and receiver reports include them in the fraction and cumulative loss. Reordered packets that arrive on their own still cancel the loss. The fraction is also capped at 255, so a full loss no longer wraps to zero. With 20% simulated upstream loss from an Android publisher, the reported fraction lost goes from 0 to about 0.15, the FEC packets received over 30 seconds go from 64 to 204, and the packets recovered by FEC from 11 to 61. * Protect zeroed abs-send-time and TWCC in downstream FlexFEC The pacer encoded FEC after it had filled in abs-send-time and the transport-wide sequence number. libwebrtc generates FEC before its sender fills them in, and its FlexFEC receiver zeroes both in media packets before recovery. Packets recovered from SFU repairs therefore got wrong bytes in those extensions, and in the payload when the header lengths within a group differed, for example when only the first packet carried abs-capture-time. Zero both extensions after the media packet is written and before it is fed to the encoder. * Aggregate receiver report loss for adaptive FEC protection Adaptive and minimum protection fed the fraction lost of every reception report into the max-window loss filter. libwebrtc receivers can send a report with each NACK, so a report often covers only a few packets and its fraction lost is very noisy. With 10% random loss, single reports read up to 70%, and the filter kept the maximum. Derive loss from the cumulative counters instead and produce a sample only once it covers at least 20 expected packets, like libwebrtc's SendSideBandwidthEstimation::UpdatePacketsLost. The end-to-end tests now report cumulative loss and the extended highest sequence number in their receiver reports. * Report loss before repair only to FlexFEC publishers Counting repaired packets as lost in receiver reports applied to every publisher, so any publisher whose loss RTX repaired started to see that loss and its bandwidth estimate could back off sooner. Only FlexFEC protection needs loss before repair to choose its rate. Count repairs only for streams that negotiated flexfec-03, which requires FlexFEC to be enabled on the server. Other publishers keep receiving loss after repair, as before. * Keep the newest RTX repair mark of a history slot A late retransmission of an older packet that has aged out of the cache overwrote the slot of a newer repair sharing it. The newer repair, which carries the RTX packet's header, could then be used for FlexFEC recovery and corrupt the recovered packet. Keep the highest extended sequence number written to a slot until the stream restarts. isRTXRepairedLocked already treats an older packet in a slot with a newer mark as repaired. * Measure FEC overhead against all media The allocator scales the whole media bitrate by the measured repair overhead, but the overhead was measured against protected media only. With adaptive protection below 20 ms RTT only key frames are protected, so the ratio of key frame repairs to key frame media was applied to every frame, and the reservation could make the allocator lower the layer or pause the track although the bandwidth was sufficient. Count all non-padding media while any protection is active. * Keep the loss sample that closes a filter window The max-window loss filter, ported from libwebrtc, dropped the report that closes a one-second window when the window already had a maximum, so a loss spike arriving on the boundary never reached adaptive protection. Store the larger of the window's maximum and that sample. * Skip the encoder allocation check under the race detector CI runs the tests with -race and swaps sync.Mutex for go-deadlock, whose Lock allocates, so Encode allocated about once per call there. Check allocations only without the race detector, like the other allocation tests. * Bump protocol to v1.55.0 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * sfu: reserve measured FEC overhead only on the forwarded layer The measured repair overhead comes from the spatial layer being forwarded. On a low layer with one or two packets per frame it can be 60-100%, and applying it to every layer reserved the high layer at nearly twice its cost, so the allocator could stay on the low layer. Attribute measurements to the forwarded spatial layer, restarting them when forwarding starts, resumes or switches layers, and reserve the configured rate for the other layers. Also reallocate only when a reserved overhead moves by 5 points or more, or turns on or off, so that per-second measurements and adaptive protection do not re-run allocation on every receiver report. * buffer: gate upstream FlexFEC on an explicit flag A publisher PC registers flexfec-03 when only downstream FEC is enabled, because it can also carry subscriptions. Its buffers then got a FEC payload type, which reported loss before repair to the publisher and let the decoder start from the observed repair SSRC, although upstream FEC was disabled. Buffers now ignore the negotiated FlexFEC payload type unless the buffer factory enables upstream FlexFEC. * pacer: protect interceptor-written transport-cc as zeros in FEC With UseSendSideBWEInterceptor, Pion's TWCC header extension interceptor writes transport-cc during WriteRTP while the DownTrack leaves TransportWideExtID at zero. The extension was then protected with its non-zero value, which the receiver zeroes before recovery, so recovered packets got wrong bytes there. Pass the negotiated transport-cc ID to the pacer for FEC and zero it before encoding as well. * sfu: skip the FEC encoder while protection is disabled The encoder was attached whenever flexfec-03 was negotiated, including in the default disabled mode, so every forwarded packet took the encoder lock and updated the adaptive state without generating repairs. Pass the encoder to the pacer only while protection is enabled. Receiver reports still keep the loss history; adaptive protection measures media rates again once enabled. * sfu: account sent FEC repairs in the down track RTP stats Repairs were counted only in the aggregate Prometheus counter, so the down track's stats under-counted what the subscriber receives. Record them in the sender's FEC packet and byte counters, separate from media, and export the FEC counters in RTPStats for both directions. * rtc: merge subscriber FEC settings in one place The subscription merged and normalized settings, and the subscribed track merged and normalized them again, while the DownTrack mapped unknown modes and levels a third time. Merge only in the subscription, store the merged settings in the subscribed track as is, and leave unknown values to the DownTrack. * prometheus: resolve fixed-label FEC counters once RecordFECDownstream runs on the pacer path for every repair batch and looked up both counter vectors by label each time. * rtpstats: seed a sender without holding both locks Seed took the receiving sender's lock and then the source's, so two senders seeding from each other at the same time could deadlock. Copy the source's state under its own lock first, then apply the copy under the receiving sender's lock. * telemetry: count FlexFEC repairs in traffic totals Retransmissions and padding count towards node and participant traffic, but FEC repairs, which are also on the wire, did not. Add repairs sent and received to the node packet and byte totals, and to the packets and bytes of TrafficStats. Analytics streams are unchanged: AnalyticsStream has no FEC field. * sfu: read the FEC transport-cc ID atomically setRTPHeaderExtensions runs again when the stream allocator listener is set, which can happen while media is already forwarded, and WriteRTP read the ID without synchronization. Store it atomically once the extensions are processed. * telemetry: count only FEC repairs sent in node traffic Media recovered from FEC repairs is counted as received media, so adding the repairs received to node ingress counted recovered packets twice. Node traffic feeds room admission against limit.bytes_per_sec, so ingress must not be inflated. Keep repairs sent in node egress, where nothing is synthesized, and leave ingress as before. TrafficStats is derived from RTPStats without a direction, so it cannot tell repairs sent from repairs received; leave it without FEC. * rtc: add WebRTCConfig.SetFlexFEC for per-session FlexFEC settings * rtc: apply a session's upstream FlexFEC to its buffers SetFlexFEC changed what a session negotiates with its publisher, but the publisher's buffers still followed the node config: the room's buffer factory read it once. A session with upstream on and a node with it off negotiated flexfec-03, so the publisher sent repairs that its buffers ignored; the reverse turned on upstream recovery and loss reported before repair for a session that asked for neither. The session's buffer factory now follows the session's upstream setting, whichever of SetFlexFEC and SetBufferFactory comes first. Without a SetFlexFEC call it follows the node config, as before. The setting applies to buffers the factory creates afterwards, so SetFlexFEC is meant to be called while the session is set up, before any track is published. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Jules Testard <jules.testard@livekit.io> Co-authored-by: David Chen <chenosaurus@gmail.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1987 lines
54 KiB
Go
1987 lines
54 KiB
Go
// Copyright 2023 LiveKit, Inc.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package buffer
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"slices"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/gammazero/deque"
|
|
"github.com/pion/rtcp"
|
|
"github.com/pion/rtp"
|
|
"github.com/pion/rtp/codecs"
|
|
"github.com/pion/sdp/v3"
|
|
"github.com/pion/webrtc/v4"
|
|
"go.uber.org/atomic"
|
|
|
|
"github.com/livekit/livekit-server/pkg/sfu/audio"
|
|
"github.com/livekit/livekit-server/pkg/sfu/flexfec"
|
|
act "github.com/livekit/livekit-server/pkg/sfu/rtpextension/abscapturetime"
|
|
dd "github.com/livekit/livekit-server/pkg/sfu/rtpextension/dependencydescriptor"
|
|
"github.com/livekit/livekit-server/pkg/sfu/rtpstats"
|
|
"github.com/livekit/livekit-server/pkg/sfu/utils"
|
|
"github.com/livekit/livekit-server/pkg/telemetry/prometheus"
|
|
"github.com/livekit/mediatransportutil/pkg/bucket"
|
|
"github.com/livekit/mediatransportutil/pkg/codec"
|
|
"github.com/livekit/mediatransportutil/pkg/nack"
|
|
"github.com/livekit/protocol/codecs/mime"
|
|
"github.com/livekit/protocol/livekit"
|
|
"github.com/livekit/protocol/logger"
|
|
"github.com/livekit/protocol/utils/mono"
|
|
)
|
|
|
|
var (
|
|
streamGenerationCounter atomic.Uint64
|
|
errFECMediaPacketNotFound = errors.New("fec media packet not found")
|
|
|
|
ExtPacketFactory = &sync.Pool{
|
|
New: func() any {
|
|
return &ExtPacket{}
|
|
},
|
|
}
|
|
|
|
// unmarshal scratch, a reused packet keeps its Extensions capacity
|
|
rtpPacketFactory = &sync.Pool{
|
|
New: func() any {
|
|
return &rtp.Packet{}
|
|
},
|
|
}
|
|
)
|
|
|
|
func ReleaseExtPacket(extPkt *ExtPacket) {
|
|
if extPkt == nil {
|
|
return
|
|
}
|
|
|
|
ReleaseExtDependencyDescriptor(extPkt.DependencyDescriptor)
|
|
|
|
*extPkt = ExtPacket{}
|
|
ExtPacketFactory.Put(extPkt)
|
|
}
|
|
|
|
// --------------------------------------
|
|
|
|
type ExtPacket struct {
|
|
VideoLayer
|
|
Arrival int64
|
|
ExtSequenceNumber uint64
|
|
StreamGeneration uint64 // packet cache identity for GetPacketForStream
|
|
ExtTimestamp uint64
|
|
Packet *rtp.Packet
|
|
VP8 *codec.VP8 // nil when not parsed
|
|
VP9 *codecs.VP9Packet // nil when not parsed
|
|
IsKeyFrame bool
|
|
RawPacket []byte
|
|
DependencyDescriptor *ExtDependencyDescriptor
|
|
AbsCaptureTimeExt *act.AbsCaptureTime
|
|
IsOutOfOrder bool
|
|
IsBuffered bool
|
|
|
|
// backing storage for Packet when the buffer creates the ext packet,
|
|
// extension and CSRC slices point into the write input until ReadExtended re-points them into RawPacket
|
|
pkt rtp.Packet
|
|
extensions [8]rtp.Extension
|
|
|
|
// backing storage for VP8, VP9 and abs-capture-time, so parsing does not allocate
|
|
vp8 codec.VP8
|
|
vp9 codecs.VP9Packet
|
|
absCaptureTime act.AbsCaptureTime
|
|
}
|
|
|
|
type BufferProvider interface {
|
|
SetLogger(lgr logger.Logger)
|
|
SetAudioLevelConfig(audioLevelConfig audio.AudioLevelConfig)
|
|
SetStreamRestartDetection(enable bool)
|
|
SetPLIThrottle(duration int64)
|
|
SetRTT(rtt uint32)
|
|
SetPaused(paused bool)
|
|
|
|
SendPLI(force bool)
|
|
|
|
ReadExtended(buf []byte) (*ExtPacket, error)
|
|
GetPacket(buf []byte, esn uint64) (int, error)
|
|
GetPacketForStream(buf []byte, esn uint64, streamGeneration uint64) (int, error)
|
|
|
|
GetAudioLevel() (float64, bool)
|
|
GetTemporalLayerFpsForSpatial(layer int32) []float32
|
|
GetStats() *livekit.RTPStats
|
|
GetDeltaStats() *StreamStatsWithLayers
|
|
GetDeltaStatsLite() *rtpstats.RTPDeltaInfoLite
|
|
GetLastSenderReportTime() time.Time
|
|
GetNACKPairs() []rtcp.NackPair
|
|
|
|
SetSenderReportData(srData *livekit.RTCPSenderReportState)
|
|
GetSenderReportData() *livekit.RTCPSenderReportState
|
|
|
|
OnRtcpSenderReport(fn func())
|
|
OnFpsChanged(fn func())
|
|
OnVideoSizeChanged(fn func([]codec.VideoSize))
|
|
OnCodecChange(fn func(webrtc.RTPCodecParameters))
|
|
OnStreamRestart(fn func(string))
|
|
|
|
StartKeyFrameSeeder()
|
|
StopKeyFrameSeeder()
|
|
|
|
HandleIncomingPacket(
|
|
rawPkt []byte,
|
|
rtpPacket *rtp.Packet,
|
|
arrivalTime int64,
|
|
isBuffered bool,
|
|
isRTX bool,
|
|
skippedSeqs []uint16,
|
|
oobSequenceNumber uint16,
|
|
) (extSequenceNumber uint64, streamGeneration uint64, err error)
|
|
|
|
MarkForRestartStream(reason string)
|
|
RestartStream(reason string)
|
|
|
|
CloseWithReason(reason string) (*livekit.RTPStats, error)
|
|
}
|
|
|
|
const (
|
|
bucketCapCheckInterval = 1e9
|
|
)
|
|
|
|
type BufferBaseParams struct {
|
|
SSRC uint32
|
|
MaxVideoPkts int
|
|
MaxAudioPkts int
|
|
LoggerComponents []string
|
|
SendPLI func()
|
|
IsReportingEnabled bool
|
|
IsOOBSequenceNumber bool
|
|
IsDDRestartEnabled bool
|
|
// IsFlexFECEnabled permits upstream FlexFEC. A repair stream can be
|
|
// negotiated on a transport only because its subscriptions use FEC; it is
|
|
// then ignored and loss is reported as before.
|
|
IsFlexFECEnabled bool
|
|
}
|
|
|
|
type BufferBase struct {
|
|
sync.RWMutex
|
|
|
|
params BufferBaseParams
|
|
|
|
readCond *sync.Cond
|
|
|
|
streamGeneration uint64
|
|
bucket *bucket.Bucket[uint64, uint16]
|
|
lastBucketCapCheckAt int64
|
|
|
|
nacker nack.NackQueueInterface
|
|
rtpStatsLite *rtpstats.RTPStatsReceiverLite
|
|
liteStatsSnapshotId uint32
|
|
|
|
extPackets deque.Deque[*ExtPacket]
|
|
|
|
codecType webrtc.RTPCodecType
|
|
closeOnce sync.Once
|
|
clockRate uint32
|
|
mime mime.MimeType
|
|
|
|
rtpParameters webrtc.RTPParameters
|
|
payloadType uint8
|
|
rtxPayloadType uint8
|
|
fecPayloadType uint8
|
|
fecSSRC uint32
|
|
fecDecoder *flexfec.Decoder
|
|
fecPktBuf []byte
|
|
|
|
snRangeMap *utils.RangeMap[uint64, uint64]
|
|
|
|
// extended sequence numbers + 1 of packets repaired from RTX, indexed by
|
|
// sequence number modulo the history size; zero marks an empty slot.
|
|
// Allocated on the first repair, see rtxRepairedHistorySize.
|
|
rtxRepairedSNs []uint64
|
|
|
|
audioLevelConfig audio.AudioLevelConfig
|
|
audioLevel *audio.AudioLevel
|
|
audioLevelExtID uint8
|
|
|
|
enableStreamRestartDetection bool
|
|
|
|
pliThrottle int64
|
|
lastPliAt atomic.Int64
|
|
|
|
rtpStats *rtpstats.RTPStatsReceiver
|
|
ppsSnapshotId uint32
|
|
rrSnapshotId uint32
|
|
deltaStatsSnapshotId uint32
|
|
|
|
// callbacks
|
|
onRtcpSenderReport func()
|
|
onFpsChanged func()
|
|
onVideoSizeChanged func([]codec.VideoSize)
|
|
onCodecChange func(webrtc.RTPCodecParameters)
|
|
onStreamRestart func(string)
|
|
|
|
// video size tracking for multiple spatial layers
|
|
currentVideoSize [DefaultMaxLayerSpatial + 1]codec.VideoSize
|
|
|
|
logger logger.Logger
|
|
|
|
// dependency descriptor
|
|
ddExtID uint8
|
|
ddParser *DependencyDescriptorParser
|
|
|
|
isPaused bool
|
|
frameRateCalculator [DefaultMaxLayerSpatial + 1]FrameRateCalculator
|
|
frameRateCalculated bool
|
|
|
|
packetNotFoundCount atomic.Uint32
|
|
packetTooOldCount atomic.Uint32
|
|
extPacketTooMuchCount atomic.Uint32
|
|
codecPayloadTypeNotFoundCount atomic.Uint32
|
|
|
|
absCaptureTimeExtID uint8
|
|
|
|
keyFrameSeederGeneration atomic.Int32
|
|
|
|
isRestartPending bool
|
|
|
|
isClosed atomic.Bool
|
|
}
|
|
|
|
func NewBufferBase(params BufferBaseParams) *BufferBase {
|
|
l := logger.GetLogger() // will be reset with correct context via SetLogger
|
|
for _, component := range params.LoggerComponents {
|
|
l = l.WithComponent(component)
|
|
}
|
|
l = l.WithValues("ssrc", params.SSRC)
|
|
|
|
b := &BufferBase{
|
|
params: params,
|
|
streamGeneration: streamGenerationCounter.Inc(),
|
|
lastBucketCapCheckAt: mono.UnixNano(),
|
|
snRangeMap: utils.NewRangeMap[uint64, uint64](100),
|
|
pliThrottle: int64(500 * time.Millisecond),
|
|
logger: l,
|
|
}
|
|
b.readCond = sync.NewCond(&b.RWMutex)
|
|
b.extPackets.SetBaseCap(128)
|
|
return b
|
|
}
|
|
|
|
func (b *BufferBase) SSRC() uint32 {
|
|
return b.params.SSRC
|
|
}
|
|
|
|
func (b *BufferBase) MaxVideoPkts() int {
|
|
return b.params.MaxVideoPkts
|
|
}
|
|
|
|
func (b *BufferBase) MaxAudioPkts() int {
|
|
return b.params.MaxAudioPkts
|
|
}
|
|
|
|
func (b *BufferBase) SetLogger(lgr logger.Logger) {
|
|
b.Lock()
|
|
defer b.Unlock()
|
|
|
|
for _, component := range b.params.LoggerComponents {
|
|
lgr = lgr.WithComponent(component)
|
|
}
|
|
lgr = lgr.WithValues("ssrc", b.params.SSRC)
|
|
b.logger = lgr
|
|
|
|
if b.rtpStats != nil {
|
|
b.rtpStats.SetLogger(b.logger)
|
|
}
|
|
|
|
if b.rtpStatsLite != nil {
|
|
b.rtpStatsLite.SetLogger(b.logger)
|
|
}
|
|
}
|
|
|
|
func (b *BufferBase) Bind(rtpParameters webrtc.RTPParameters, codec webrtc.RTPCodecCapability, bitrate int) error {
|
|
b.logger.Debugw("binding track")
|
|
|
|
b.Lock()
|
|
defer b.Unlock()
|
|
|
|
return b.BindLocked(rtpParameters, codec, bitrate)
|
|
}
|
|
|
|
func (b *BufferBase) BindLocked(rtpParameters webrtc.RTPParameters, codec webrtc.RTPCodecCapability, bitrate int) error {
|
|
b.logger.Debugw("binding track")
|
|
if codec.ClockRate == 0 {
|
|
b.logger.Warnw("invalid codec", nil, "rtpParameters", rtpParameters, "codec", codec, "bitrate", bitrate)
|
|
return errInvalidCodec
|
|
}
|
|
|
|
b.setupRTPStats(codec.ClockRate)
|
|
|
|
b.clockRate = codec.ClockRate
|
|
b.mime = mime.NormalizeMimeType(codec.MimeType)
|
|
b.rtpParameters = rtpParameters
|
|
for _, codecParameter := range rtpParameters.Codecs {
|
|
if mime.IsMimeTypeStringEqual(codecParameter.MimeType, codec.MimeType) {
|
|
b.payloadType = uint8(codecParameter.PayloadType)
|
|
break
|
|
}
|
|
}
|
|
|
|
if b.payloadType == 0 && !mime.IsMimeTypeStringEqual(codec.MimeType, webrtc.MimeTypePCMU) {
|
|
b.logger.Warnw(
|
|
"could not find payload type for codec", nil,
|
|
"codec", codec.MimeType,
|
|
"rtpParameters", rtpParameters,
|
|
)
|
|
b.payloadType = uint8(rtpParameters.Codecs[0].PayloadType)
|
|
}
|
|
|
|
// find RTX payload type
|
|
for _, codec := range rtpParameters.Codecs {
|
|
if mime.IsMimeTypeStringRTX(codec.MimeType) && strings.Contains(codec.SDPFmtpLine, fmt.Sprintf("apt=%d", b.payloadType)) {
|
|
b.rtxPayloadType = uint8(codec.PayloadType)
|
|
break
|
|
}
|
|
}
|
|
|
|
// find FlexFEC payload type, the repair stream is codec agnostic (no apt)
|
|
for _, codec := range rtpParameters.Codecs {
|
|
if b.params.IsFlexFECEnabled && strings.EqualFold(codec.MimeType, webrtc.MimeTypeFlexFEC03) {
|
|
b.fecPayloadType = uint8(codec.PayloadType)
|
|
break
|
|
}
|
|
}
|
|
|
|
for _, ext := range rtpParameters.HeaderExtensions {
|
|
switch ext.URI {
|
|
case dd.ExtensionURI:
|
|
if b.ddExtID != 0 {
|
|
b.logger.Warnw(
|
|
"multiple dependency descriptor extensions found", nil,
|
|
"id", ext.ID,
|
|
"previous", b.ddExtID,
|
|
)
|
|
continue
|
|
}
|
|
b.ddExtID = uint8(ext.ID)
|
|
b.createDDParserAndFrameRateCalculator()
|
|
|
|
case sdp.AudioLevelURI:
|
|
b.audioLevelExtID = uint8(ext.ID)
|
|
b.audioLevel = audio.NewAudioLevel(audio.AudioLevelParams{
|
|
ClockRate: b.clockRate,
|
|
})
|
|
b.audioLevel.SetConfig(b.audioLevelConfig)
|
|
|
|
case act.AbsCaptureTimeURI:
|
|
b.absCaptureTimeExtID = uint8(ext.ID)
|
|
}
|
|
}
|
|
|
|
switch {
|
|
case mime.IsMimeTypeAudio(b.mime):
|
|
b.codecType = webrtc.RTPCodecTypeAudio
|
|
b.bucket = bucket.NewBucket[uint64, uint16](
|
|
InitPacketBufferSizeAudio,
|
|
bucket.RTPMaxPktSize,
|
|
bucket.RTPSeqNumOffset,
|
|
)
|
|
|
|
case mime.IsMimeTypeVideo(b.mime):
|
|
b.codecType = webrtc.RTPCodecTypeVideo
|
|
b.bucket = bucket.NewBucket[uint64, uint16](
|
|
InitPacketBufferSizeVideo,
|
|
bucket.RTPMaxPktSize,
|
|
bucket.RTPSeqNumOffset,
|
|
)
|
|
|
|
if b.frameRateCalculator[0] == nil {
|
|
b.createFrameRateCalculator()
|
|
}
|
|
|
|
if bitrate > 0 {
|
|
pps := bitrate / 8 / 1200
|
|
for pps > b.bucket.Capacity() {
|
|
if b.bucket.Grow() >= b.params.MaxVideoPkts {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
default:
|
|
b.codecType = webrtc.RTPCodecType(0)
|
|
}
|
|
|
|
for _, fb := range codec.RTCPFeedback {
|
|
switch fb.Type {
|
|
case webrtc.TypeRTCPFBGoogREMB:
|
|
b.logger.Debugw("Setting feedback", "type", webrtc.TypeRTCPFBGoogREMB)
|
|
b.logger.Debugw("REMB not supported, RTCP feedback will not be generated")
|
|
|
|
case webrtc.TypeRTCPFBNACK:
|
|
// pion uses a single mediaengine to manage negotiated codecs of peerconnection, that means we can't have different
|
|
// codec settings at track level for same codec type, so enable nack for all audio receivers but don't create nack queue
|
|
// for red codec.
|
|
if b.mime == mime.MimeTypeRED {
|
|
break
|
|
}
|
|
|
|
b.logger.Debugw("Setting feedback", "type", webrtc.TypeRTCPFBNACK)
|
|
if b.nacker == nil {
|
|
b.nacker = nack.NewNACKQueue(nack.NackQueueParamsDefault)
|
|
}
|
|
}
|
|
}
|
|
|
|
if b.nacker == nil && b.params.IsOOBSequenceNumber {
|
|
b.nacker = nack.NewNACKQueue(nack.NackQueueParamsDefault)
|
|
}
|
|
|
|
b.StartKeyFrameSeeder()
|
|
b.maybeCreateFECDecoderLocked()
|
|
|
|
return nil
|
|
}
|
|
|
|
func (b *BufferBase) CloseWithReason(reason string) (stats *livekit.RTPStats, err error) {
|
|
b.closeOnce.Do(func() {
|
|
b.isClosed.Store(true)
|
|
|
|
b.StopKeyFrameSeeder()
|
|
|
|
b.Lock()
|
|
stats, _ = b.stopRTPStats(reason)
|
|
b.readCond.Broadcast()
|
|
b.Unlock()
|
|
|
|
go b.flushExtPackets()
|
|
})
|
|
return
|
|
}
|
|
|
|
func (b *BufferBase) IsClosed() bool {
|
|
return b.isClosed.Load()
|
|
}
|
|
|
|
func (b *BufferBase) SetPaused(paused bool) {
|
|
b.Lock()
|
|
defer b.Unlock()
|
|
|
|
b.isPaused = paused
|
|
}
|
|
|
|
func (b *BufferBase) SetAudioLevelConfig(audioLevelConfig audio.AudioLevelConfig) {
|
|
b.Lock()
|
|
defer b.Unlock()
|
|
|
|
b.audioLevelConfig = audioLevelConfig
|
|
if b.audioLevel != nil {
|
|
b.audioLevel.SetConfig(b.audioLevelConfig)
|
|
}
|
|
}
|
|
|
|
func (b *BufferBase) SetStreamRestartDetection(enable bool) {
|
|
b.Lock()
|
|
defer b.Unlock()
|
|
|
|
b.enableStreamRestartDetection = enable
|
|
}
|
|
|
|
func (b *BufferBase) setupRTPStats(clockRate uint32) {
|
|
b.rtpStats = rtpstats.NewRTPStatsReceiver(rtpstats.RTPStatsParams{})
|
|
// a new stream has no PLI history, so the PLI throttle starts fresh with it
|
|
b.lastPliAt.Store(0)
|
|
b.rtpStats.SetLogger(b.logger)
|
|
b.rtpStats.SetClockRate(clockRate)
|
|
|
|
b.ppsSnapshotId = b.rtpStats.NewSnapshotId()
|
|
if b.params.IsReportingEnabled {
|
|
b.rrSnapshotId = b.rtpStats.NewSnapshotId()
|
|
b.deltaStatsSnapshotId = b.rtpStats.NewSnapshotId()
|
|
}
|
|
|
|
b.setupRTPStatsLite(clockRate)
|
|
}
|
|
|
|
func (b *BufferBase) setupRTPStatsLite(clockRate uint32) {
|
|
if b.params.IsOOBSequenceNumber {
|
|
b.rtpStatsLite = rtpstats.NewRTPStatsReceiverLite(rtpstats.RTPStatsParams{})
|
|
b.rtpStatsLite.SetLogger(b.logger)
|
|
b.rtpStatsLite.SetClockRate(clockRate)
|
|
|
|
b.liteStatsSnapshotId = b.rtpStatsLite.NewSnapshotLiteId()
|
|
}
|
|
}
|
|
|
|
func (b *BufferBase) stopRTPStats(reason string) (stats *livekit.RTPStats, statsLite *livekit.RTPStats) {
|
|
if b.rtpStats != nil {
|
|
b.rtpStats.Stop()
|
|
stats = b.rtpStats.ToProto()
|
|
|
|
b.logger.Debugw(
|
|
"rtp stats",
|
|
"direction", "upstream",
|
|
"stats", b.rtpStats,
|
|
"reason", reason,
|
|
)
|
|
}
|
|
|
|
statsLite = b.stopRTPStatsLite(reason)
|
|
return
|
|
}
|
|
|
|
func (b *BufferBase) stopRTPStatsLite(reason string) (statsLite *livekit.RTPStats) {
|
|
if b.rtpStatsLite != nil {
|
|
b.rtpStatsLite.Stop()
|
|
statsLite = b.rtpStatsLite.ToProto()
|
|
|
|
b.logger.Debugw(
|
|
"rtp stats lite",
|
|
"direction", "upstream",
|
|
"statsLite", b.rtpStatsLite,
|
|
"reason", reason,
|
|
)
|
|
}
|
|
return
|
|
}
|
|
|
|
func (b *BufferBase) RestartOOBSequenceNumber(reason string) {
|
|
b.Lock()
|
|
defer b.Unlock()
|
|
|
|
b.stopRTPStatsLite(reason)
|
|
b.setupRTPStatsLite(b.clockRate)
|
|
|
|
if b.nacker != nil {
|
|
b.nacker = nack.NewNACKQueue(nack.NackQueueParamsDefault)
|
|
}
|
|
}
|
|
|
|
func (b *BufferBase) MarkForRestartStream(reason string) {
|
|
b.logger.Debugw("marking for stream restart", "reason", reason)
|
|
|
|
b.Lock()
|
|
defer b.Unlock()
|
|
|
|
b.isRestartPending = true
|
|
b.readCond.Broadcast()
|
|
}
|
|
|
|
func (b *BufferBase) RestartStream(reason string) {
|
|
b.logger.Debugw("stream restart", "reason", reason)
|
|
|
|
b.Lock()
|
|
defer b.Unlock()
|
|
|
|
b.restartStreamLocked(reason, false)
|
|
b.readCond.Broadcast()
|
|
}
|
|
|
|
func (b *BufferBase) restartStreamLocked(reason string, isDetected bool) {
|
|
b.logger.Infow("stream restart", "reason", reason)
|
|
|
|
// stop
|
|
b.StopKeyFrameSeeder()
|
|
b.stopRTPStats("stream-restart")
|
|
b.flushExtPacketsLocked()
|
|
|
|
// restart
|
|
b.snRangeMap = utils.NewRangeMap[uint64, uint64](100)
|
|
clear(b.rtxRepairedSNs)
|
|
b.setupRTPStats(b.clockRate)
|
|
|
|
b.streamGeneration = streamGenerationCounter.Inc()
|
|
b.bucket.ResyncOnNextPacket()
|
|
b.lastBucketCapCheckAt = mono.UnixNano()
|
|
|
|
if b.nacker != nil {
|
|
b.nacker = nack.NewNACKQueue(nack.NackQueueParamsDefault)
|
|
}
|
|
|
|
if b.audioLevel != nil {
|
|
b.audioLevel = audio.NewAudioLevel(audio.AudioLevelParams{
|
|
ClockRate: b.clockRate,
|
|
})
|
|
b.audioLevel.SetConfig(b.audioLevelConfig)
|
|
}
|
|
|
|
if b.ddExtID != 0 {
|
|
b.createDDParserAndFrameRateCalculator()
|
|
}
|
|
|
|
b.frameRateCalculated = false
|
|
if b.frameRateCalculator[0] == nil {
|
|
b.createFrameRateCalculator()
|
|
}
|
|
|
|
b.StartKeyFrameSeeder()
|
|
|
|
b.recreateFECDecoderLocked()
|
|
|
|
if isDetected {
|
|
b.isRestartPending = true
|
|
|
|
if f := b.onStreamRestart; f != nil {
|
|
go f(reason)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (b *BufferBase) createDDParserAndFrameRateCalculator() {
|
|
if mime.IsMimeTypeSVCCapable(b.mime) || b.mime == mime.MimeTypeVP8 {
|
|
frc := NewFrameRateCalculatorDD(b.clockRate, b.logger)
|
|
for i := range b.frameRateCalculator {
|
|
b.frameRateCalculator[i] = frc.GetFrameRateCalculatorForSpatial(int32(i))
|
|
}
|
|
b.ddParser = NewDependencyDescriptorParser(
|
|
b.ddExtID,
|
|
b.logger,
|
|
func(spatial, temporal int32) {
|
|
frc.SetMaxLayer(spatial, temporal)
|
|
},
|
|
b.params.IsDDRestartEnabled,
|
|
)
|
|
}
|
|
}
|
|
|
|
func (b *BufferBase) createFrameRateCalculator() {
|
|
switch b.mime {
|
|
case mime.MimeTypeVP8:
|
|
b.frameRateCalculator[0] = NewFrameRateCalculatorVP8(b.clockRate, b.logger)
|
|
|
|
case mime.MimeTypeVP9:
|
|
frc := NewFrameRateCalculatorVP9(b.clockRate, b.logger)
|
|
for i := range b.frameRateCalculator {
|
|
b.frameRateCalculator[i] = frc.GetFrameRateCalculatorForSpatial(int32(i))
|
|
}
|
|
|
|
case mime.MimeTypeH265:
|
|
b.frameRateCalculator[0] = NewFrameRateCalculatorH26x(b.clockRate, b.logger)
|
|
}
|
|
}
|
|
|
|
func (b *BufferBase) ReadExtended(buf []byte) (*ExtPacket, error) {
|
|
b.Lock()
|
|
for {
|
|
if b.isClosed.Load() {
|
|
b.Unlock()
|
|
return nil, io.EOF
|
|
}
|
|
|
|
if b.isRestartPending {
|
|
b.isRestartPending = false
|
|
b.Unlock()
|
|
return nil, nil
|
|
}
|
|
|
|
if b.extPackets.Len() > 0 {
|
|
ep := b.extPackets.PopFront()
|
|
patched := b.patchExtPacket(ep, buf)
|
|
if patched == nil {
|
|
ReleaseExtPacket(ep)
|
|
continue
|
|
}
|
|
|
|
b.Unlock()
|
|
return patched, nil
|
|
}
|
|
|
|
b.readCond.Wait()
|
|
}
|
|
}
|
|
|
|
func (b *BufferBase) SetPLIThrottle(duration int64) {
|
|
b.Lock()
|
|
defer b.Unlock()
|
|
|
|
b.pliThrottle = duration
|
|
}
|
|
|
|
func (b *BufferBase) SendPLI(force bool) {
|
|
b.RLock()
|
|
if b.codecType != webrtc.RTPCodecTypeVideo || b.rtpStats == nil {
|
|
b.RUnlock()
|
|
return
|
|
}
|
|
|
|
// throttle before the rtpstats lock, every subscriber of a track can ask for a key frame at once,
|
|
// under the buffer read lock so a stream restart cannot interleave with claiming the window
|
|
now := mono.UnixNano()
|
|
if force {
|
|
b.lastPliAt.Store(now)
|
|
} else {
|
|
lastPliAt := b.lastPliAt.Load()
|
|
if now-lastPliAt < b.pliThrottle || !b.lastPliAt.CompareAndSwap(lastPliAt, now) {
|
|
b.RUnlock()
|
|
return
|
|
}
|
|
}
|
|
|
|
rtpStats := b.rtpStats
|
|
pliThrottle := b.pliThrottle
|
|
b.RUnlock()
|
|
|
|
if !rtpStats.CheckAndUpdatePli(pliThrottle, force) {
|
|
return
|
|
}
|
|
|
|
if b.params.SendPLI != nil {
|
|
b.params.SendPLI()
|
|
}
|
|
}
|
|
|
|
func (b *BufferBase) SetRTT(rtt uint32) {
|
|
b.Lock()
|
|
defer b.Unlock()
|
|
|
|
if rtt == 0 {
|
|
return
|
|
}
|
|
|
|
if b.nacker != nil {
|
|
b.nacker.SetRTT(rtt)
|
|
}
|
|
|
|
if b.rtpStats != nil {
|
|
b.rtpStats.UpdateRtt(rtt)
|
|
}
|
|
}
|
|
|
|
func (b *BufferBase) WaitRead() {
|
|
b.readCond.Wait()
|
|
}
|
|
|
|
func (b *BufferBase) NotifyRead() {
|
|
b.readCond.Broadcast()
|
|
}
|
|
|
|
// HandleIncomingPacket returns the extended sequence number and the packet cache identity to use with GetPacketForStream.
|
|
func (b *BufferBase) HandleIncomingPacket(
|
|
rawPkt []byte,
|
|
rtpPacket *rtp.Packet,
|
|
arrivalTime int64,
|
|
isBuffered bool,
|
|
isRTX bool,
|
|
skippedSeqs []uint16,
|
|
oobSequenceNumber uint16,
|
|
) (uint64, uint64, error) {
|
|
b.Lock()
|
|
defer b.Unlock()
|
|
|
|
if b.isClosed.Load() {
|
|
return 0, 0, io.EOF
|
|
}
|
|
|
|
extSequenceNumber, err := b.HandleIncomingPacketLocked(
|
|
rawPkt,
|
|
rtpPacket,
|
|
arrivalTime,
|
|
isBuffered,
|
|
isRTX,
|
|
skippedSeqs,
|
|
oobSequenceNumber,
|
|
)
|
|
// a restart in the call above happens before the packet is cached, so this is the generation of the packet
|
|
return extSequenceNumber, b.streamGeneration, err
|
|
}
|
|
|
|
func (b *BufferBase) HandleIncomingPacketLocked(
|
|
rawPkt []byte,
|
|
rtpPacket *rtp.Packet,
|
|
arrivalTime int64,
|
|
isBuffered bool,
|
|
isRTX bool,
|
|
skippedSeqs []uint16,
|
|
oobSequenceNumber uint16,
|
|
) (uint64, error) {
|
|
if rtpPacket == nil {
|
|
rtpPacket = rtpPacketFactory.Get().(*rtp.Packet)
|
|
defer rtpPacketFactory.Put(rtpPacket)
|
|
if err := rtpPacket.Unmarshal(rawPkt); err != nil {
|
|
b.logger.Errorw("could not unmarshal RTP packet", err)
|
|
return 0, err
|
|
}
|
|
}
|
|
|
|
sequenceNumber := rtpPacket.SequenceNumber
|
|
extSequenceNumber, err := b.handleIncomingPacketLocked(
|
|
rawPkt, rtpPacket, arrivalTime, isBuffered, isRTX, skippedSeqs, oobSequenceNumber,
|
|
)
|
|
if b.fecDecoder != nil && !b.isRTXRepairedSequenceNumberLocked(sequenceNumber) {
|
|
// Media can complete a retained FEC window. Recover before callers
|
|
// generate feedback, using the publisher's sequence number because
|
|
// ingestion adjusts it when padding has been removed. A packet repaired
|
|
// from RTX is not the media FlexFEC protected, see markRTXRepairedLocked.
|
|
mediaPacket := *rtpPacket
|
|
mediaPacket.SequenceNumber = sequenceNumber
|
|
b.feedFECLocked(&mediaPacket, arrivalTime)
|
|
}
|
|
return extSequenceNumber, err
|
|
}
|
|
|
|
// handleIncomingPacketLocked ingests media without re-entering the decoder.
|
|
// Recovered packets use this path because DecodeFEC already chains recovery.
|
|
func (b *BufferBase) handleIncomingPacketLocked(
|
|
rawPkt []byte,
|
|
rtpPacket *rtp.Packet,
|
|
arrivalTime int64,
|
|
isBuffered bool,
|
|
isRTX bool,
|
|
skippedSeqs []uint16,
|
|
oobSequenceNumber uint16,
|
|
) (uint64, error) {
|
|
b.processAudioSsrcLevelHeaderExtension(rtpPacket, arrivalTime)
|
|
|
|
if len(skippedSeqs) > 0 {
|
|
// Use the current highest timestamp to prevent the case of old sequence number and newer timestamp.
|
|
// It is possible that the skipped packet is older. An example sequence
|
|
// - Packet 10, skipped 6, 7, 9 -> Packet 8 is unknown at this point
|
|
// - Packet 11, skipped 8 -> this would cause sequence number to be older, but using timestamp from Packet 11 will make time stamp diff +ve
|
|
ts := b.rtpStats.HighestTimestamp()
|
|
for _, sn := range skippedSeqs {
|
|
flowState := b.rtpStats.Update(
|
|
arrivalTime,
|
|
sn,
|
|
ts,
|
|
false, // no marker
|
|
0, // no header for skipped packet, so 0 size
|
|
0, // no payload
|
|
0, // no padding
|
|
)
|
|
if flowState.UnhandledReason == rtpstats.RTPFlowUnhandledReasonNone && !flowState.IsOutOfOrder {
|
|
if err := b.snRangeMap.ExcludeRange(flowState.ExtSequenceNumber, flowState.ExtSequenceNumber+1); err != nil {
|
|
b.logger.Errorw(
|
|
"could not exclude range", err,
|
|
"sequenceNumber", sn,
|
|
"extSequenceNumber", flowState.ExtSequenceNumber,
|
|
"rtpStats", b.rtpStats,
|
|
"rtpStatsLite", b.rtpStatsLite,
|
|
"snRangeMap", b.snRangeMap,
|
|
"skipped", skippedSeqs,
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// do not start on an RTX packet
|
|
if isRTX && !b.rtpStats.IsActive() {
|
|
return 0, errors.New("cannot start on rtx packet")
|
|
}
|
|
|
|
flowState := b.rtpStats.Update(
|
|
arrivalTime,
|
|
rtpPacket.Header.SequenceNumber,
|
|
rtpPacket.Header.Timestamp,
|
|
rtpPacket.Header.Marker,
|
|
rtpPacket.Header.MarshalSize(),
|
|
len(rtpPacket.Payload),
|
|
int(rtpPacket.Header.PaddingSize),
|
|
)
|
|
switch flowState.UnhandledReason {
|
|
case rtpstats.RTPFlowUnhandledReasonNone:
|
|
case rtpstats.RTPFlowUnhandledReasonRestart:
|
|
if !b.enableStreamRestartDetection {
|
|
return 0, fmt.Errorf("unhandled reason: %s", flowState.UnhandledReason.String())
|
|
}
|
|
|
|
b.restartStreamLocked("discontinuity", true)
|
|
|
|
flowState = b.rtpStats.Update(
|
|
arrivalTime,
|
|
rtpPacket.Header.SequenceNumber,
|
|
rtpPacket.Header.Timestamp,
|
|
rtpPacket.Header.Marker,
|
|
rtpPacket.Header.MarshalSize(),
|
|
len(rtpPacket.Payload),
|
|
int(rtpPacket.Header.PaddingSize),
|
|
)
|
|
default:
|
|
return 0, fmt.Errorf("unhandled reason: %s", flowState.UnhandledReason.String())
|
|
}
|
|
|
|
if isRTX && flowState.IsLossFilled && b.fecPayloadType != 0 {
|
|
// RTX retransmissions and FEC recoveries both arrive as repairs. Report
|
|
// them as lost only to publishers that negotiated FlexFEC, whose
|
|
// protection follows reported loss; other publishers keep seeing loss
|
|
// after repair, as before.
|
|
b.rtpStats.UpdateRepaired()
|
|
}
|
|
|
|
if b.params.IsOOBSequenceNumber {
|
|
b.updateOOBNACKState(oobSequenceNumber, arrivalTime, len(rawPkt))
|
|
} else {
|
|
b.updateNACKState(rtpPacket.SequenceNumber, flowState)
|
|
}
|
|
|
|
if len(rtpPacket.Payload) == 0 && (!flowState.IsOutOfOrder || flowState.IsDuplicate) {
|
|
// drop padding only in-order or duplicate packet
|
|
if !flowState.IsOutOfOrder {
|
|
// in-order packet - increment sequence number offset for subsequent packets
|
|
// Example:
|
|
// 40 - regular packet - pass through as sequence number 40
|
|
// 41 - missing packet - don't know what it is, could be padding or not
|
|
// 42 - padding only packet - in-order - drop - increment sequence number offset to 1 -
|
|
// range[0, 42] = 0 offset
|
|
// 41 - arrives out of order - get offset 0 from cache - passed through as sequence number 41
|
|
// 43 - regular packet - offset = 1 (running offset) - passes through as sequence number 42
|
|
// 44 - padding only - in order - drop - increment sequence number offset to 2
|
|
// range[0, 42] = 0 offset, range[43, 44] = 1 offset
|
|
// 43 - regular packet - out of order + duplicate - offset = 1 from cache -
|
|
// adjusted sequence number is 42, will be dropped by RTX buffer AddPacket method as duplicate
|
|
// 45 - regular packet - offset = 2 (running offset) - passed through with adjusted sequence number as 43
|
|
// 44 - padding only - out-of-order + duplicate - dropped as duplicate
|
|
//
|
|
if err := b.snRangeMap.ExcludeRange(flowState.ExtSequenceNumber, flowState.ExtSequenceNumber+1); err != nil {
|
|
b.logger.Errorw(
|
|
"could not exclude range", err,
|
|
"sn", rtpPacket.SequenceNumber,
|
|
"esn", flowState.ExtSequenceNumber,
|
|
"rtpStats", b.rtpStats,
|
|
"snRangeMap", b.snRangeMap,
|
|
)
|
|
}
|
|
}
|
|
return 0, errors.New("padding only packet")
|
|
}
|
|
|
|
if !flowState.IsOutOfOrder && rtpPacket.PayloadType != b.payloadType && b.codecType == webrtc.RTPCodecTypeVideo {
|
|
b.logger.Infow("possible codec change", "oldPT", b.payloadType, "receivedPT", rtpPacket.PayloadType)
|
|
b.handleCodecChange(rtpPacket.PayloadType)
|
|
}
|
|
|
|
// add to RTX buffer using sequence number after accounting for dropped padding only packets
|
|
snAdjustment, err := b.snRangeMap.GetValue(flowState.ExtSequenceNumber)
|
|
if err != nil {
|
|
b.logger.Errorw(
|
|
"could not get sequence number adjustment", err,
|
|
"sequenceNumber", rtpPacket.SequenceNumber,
|
|
"extSequenceNumber", flowState.ExtSequenceNumber,
|
|
"timestamp", rtpPacket.Timestamp,
|
|
"extTimestamp", flowState.ExtTimestamp,
|
|
"payloadSize", len(rtpPacket.Payload),
|
|
"paddingSize", rtpPacket.Header.PaddingSize,
|
|
"rtpStats", b.rtpStats,
|
|
"rtpStatsLite", b.rtpStatsLite,
|
|
"snRangeMap", b.snRangeMap,
|
|
)
|
|
return 0, err
|
|
}
|
|
|
|
flowState.ExtSequenceNumber -= snAdjustment
|
|
rtpPacket.Header.SequenceNumber = uint16(flowState.ExtSequenceNumber)
|
|
if _, err = b.bucket.AddPacketWithSequenceNumber(rawPkt, flowState.ExtSequenceNumber); err != nil {
|
|
if !flowState.IsDuplicate {
|
|
if errors.Is(err, bucket.ErrPacketTooOld) {
|
|
packetTooOldCount := b.packetTooOldCount.Inc()
|
|
if (packetTooOldCount-1)%100 == 0 {
|
|
// log a copy so flowState stays on the stack
|
|
fs := flowState
|
|
b.logger.Warnw(
|
|
"could not add packet to bucket", err,
|
|
"count", packetTooOldCount,
|
|
"flowState", &fs,
|
|
"snAdjustment", snAdjustment,
|
|
"incomingSequenceNumber", flowState.ExtSequenceNumber+snAdjustment,
|
|
"rtpStats", b.rtpStats,
|
|
"rtpStatsLite", b.rtpStatsLite,
|
|
"snRangeMap", b.snRangeMap,
|
|
"skipped", skippedSeqs,
|
|
)
|
|
}
|
|
} else if err != bucket.ErrRTXPacket {
|
|
fs := flowState
|
|
b.logger.Warnw(
|
|
"could not add packet to bucket", err,
|
|
"flowState", &fs,
|
|
"snAdjustment", snAdjustment,
|
|
"incomingSequenceNumber", flowState.ExtSequenceNumber+snAdjustment,
|
|
"rtpStats", b.rtpStats,
|
|
"rtpStatsLite", b.rtpStatsLite,
|
|
"snRangeMap", b.snRangeMap,
|
|
"skipped", skippedSeqs,
|
|
)
|
|
}
|
|
}
|
|
return 0, err
|
|
}
|
|
|
|
ep := b.getExtPacket(rtpPacket, arrivalTime, isBuffered, flowState)
|
|
if ep == nil {
|
|
return 0, errors.New("could not get ext packet")
|
|
}
|
|
b.extPackets.PushBack(ep)
|
|
b.readCond.Broadcast()
|
|
|
|
if b.extPackets.Len() > b.bucket.Capacity() {
|
|
if (b.extPacketTooMuchCount.Inc()-1)%100 == 0 {
|
|
b.logger.Warnw("too much ext packets", nil, "count", b.extPackets.Len())
|
|
}
|
|
}
|
|
|
|
b.maybeGrowBucket(arrivalTime)
|
|
|
|
return ep.ExtSequenceNumber, nil
|
|
}
|
|
|
|
// SetFlexFECEnabled permits upstream FlexFEC, see BufferBaseParams. It must be
|
|
// called before Bind.
|
|
func (b *BufferBase) SetFlexFECEnabled(enabled bool) {
|
|
b.Lock()
|
|
b.params.IsFlexFECEnabled = enabled
|
|
b.Unlock()
|
|
}
|
|
|
|
// SetFECSSRC identifies the repair stream protecting this buffer. It can be
|
|
// called before or after Bind; decoding starts once FlexFEC is negotiated.
|
|
func (b *BufferBase) SetFECSSRC(ssrc uint32) {
|
|
b.Lock()
|
|
b.fecSSRC = ssrc
|
|
b.maybeCreateFECDecoderLocked()
|
|
b.Unlock()
|
|
}
|
|
|
|
// maybeCreateFECDecoderLocked creates the FEC decoder as soon as the repair
|
|
// stream SSRC is known and the buffer is bound with a negotiated flexfec
|
|
// payload type. Protected media is read from the primary RTP packet bucket.
|
|
func (b *BufferBase) maybeCreateFECDecoderLocked() {
|
|
if b.fecDecoder != nil || b.fecSSRC == 0 || b.bucket == nil || b.fecPayloadType == 0 {
|
|
return
|
|
}
|
|
|
|
b.fecDecoder = flexfec.NewDecoder(b.fecSSRC, b.SSRC(), b.getFECMediaPacketLocked, b.logger)
|
|
b.logger.Debugw("flexfec decoder created", "fecSSRC", b.fecSSRC, "mediaSSRC", b.SSRC())
|
|
}
|
|
|
|
// recreateFECDecoderLocked drops FlexFEC state from the previous sequence
|
|
// space. Stream restart resyncs the packet bucket and sequence map, so repair
|
|
// packets retained by the old decoder would recover against the wrong media
|
|
// or be treated as duplicates of the new repair stream.
|
|
func (b *BufferBase) recreateFECDecoderLocked() {
|
|
if b.fecDecoder == nil {
|
|
return
|
|
}
|
|
|
|
b.fecDecoder = nil
|
|
b.maybeCreateFECDecoderLocked()
|
|
}
|
|
|
|
func (b *BufferBase) getFECMediaPacketLocked(sequenceNumber uint16, dst []byte) (int, error) {
|
|
if b.bucket == nil || b.rtpStats == nil {
|
|
return 0, errFECMediaPacketNotFound
|
|
}
|
|
|
|
// FlexFEC masks use the publisher's sequence-number space. BufferBase
|
|
// removes padding-only packets from the downstream space, so resolve the
|
|
// original extended sequence number and apply the same adjustment used
|
|
// when the packet was inserted into the bucket.
|
|
extendedSN, bucketSN, ok := b.bucketSequenceNumberLocked(sequenceNumber)
|
|
if !ok || b.isRTXRepairedLocked(extendedSN) {
|
|
return 0, errFECMediaPacketNotFound
|
|
}
|
|
|
|
return b.bucket.GetPacket(dst, bucketSN)
|
|
}
|
|
|
|
// bucketSequenceNumberLocked maps a sequence number of the publisher's space
|
|
// to its extended form and to the sequence number the packet has in the
|
|
// bucket, which leaves out removed padding-only packets.
|
|
func (b *BufferBase) bucketSequenceNumberLocked(sequenceNumber uint16) (extendedSN uint64, bucketSN uint64, ok bool) {
|
|
extendedSN, ok = b.publisherExtSequenceNumberLocked(sequenceNumber)
|
|
if !ok || b.snRangeMap == nil {
|
|
return 0, 0, false
|
|
}
|
|
|
|
sequenceNumberAdjustment, err := b.snRangeMap.GetValue(extendedSN)
|
|
if err != nil || sequenceNumberAdjustment > extendedSN {
|
|
return 0, 0, false
|
|
}
|
|
return extendedSN, extendedSN - sequenceNumberAdjustment, true
|
|
}
|
|
|
|
// isPacketCachedLocked reports whether the bucket holds the packet with this
|
|
// sequence number of the publisher's space.
|
|
func (b *BufferBase) isPacketCachedLocked(sequenceNumber uint16) bool {
|
|
if b.bucket == nil {
|
|
return false
|
|
}
|
|
_, bucketSN, ok := b.bucketSequenceNumberLocked(sequenceNumber)
|
|
if !ok {
|
|
return false
|
|
}
|
|
// an empty buffer only checks that the packet is there, without copying it
|
|
_, err := b.bucket.GetPacket(nil, bucketSN)
|
|
return errors.Is(err, bucket.ErrBufferTooSmall)
|
|
}
|
|
|
|
// FECDecoderStats returns cumulative FlexFEC decode counters of the buffer.
|
|
func (b *BufferBase) FECDecoderStats() flexfec.DecoderStats {
|
|
b.RLock()
|
|
defer b.RUnlock()
|
|
|
|
if b.fecDecoder == nil {
|
|
return flexfec.DecoderStats{}
|
|
}
|
|
return b.fecDecoder.Stats()
|
|
}
|
|
|
|
// HandleFECPacket decodes a packet from the coupled FlexFEC repair stream
|
|
// and injects recovered media into the regular packet pipeline. It reports
|
|
// whether the packet was passed to the decoder.
|
|
func (b *BufferBase) HandleFECPacket(fecPkt *rtp.Packet, arrivalTime int64) bool {
|
|
b.Lock()
|
|
defer b.Unlock()
|
|
|
|
return b.HandleFECPacketLocked(fecPkt, arrivalTime)
|
|
}
|
|
|
|
// HandleFECPacketLocked requires the buffer lock to be held. Recovery updates
|
|
// NACK state before callers generate feedback.
|
|
func (b *BufferBase) HandleFECPacketLocked(fecPkt *rtp.Packet, arrivalTime int64) bool {
|
|
if b.isClosed.Load() || b.bucket == nil {
|
|
return false
|
|
}
|
|
|
|
if b.fecPayloadType == 0 || fecPkt.PayloadType != b.fecPayloadType {
|
|
b.logger.Debugw("unexpected fec payload type", "expected", b.fecPayloadType, "actual", fecPkt.PayloadType)
|
|
return false
|
|
}
|
|
|
|
if b.fecDecoder == nil {
|
|
// normally created when the pair is declared, fall back to the
|
|
// observed repair stream SSRC
|
|
b.fecSSRC = fecPkt.SSRC
|
|
b.maybeCreateFECDecoderLocked()
|
|
if b.fecDecoder == nil {
|
|
b.logger.Infow("flexfec decoder unavailable after creation attempt", "fecSSRC", b.fecSSRC, "mediaSSRC", b.SSRC(), "payloadType", b.fecPayloadType)
|
|
return false
|
|
}
|
|
}
|
|
|
|
b.feedFECLocked(fecPkt, arrivalTime)
|
|
return true
|
|
}
|
|
|
|
// feedFECLocked runs a media or FEC packet through the FEC decoder and
|
|
// injects recovered packets into the packet pipeline. Must be called with the
|
|
// buffer lock held and a non-nil decoder.
|
|
func (b *BufferBase) feedFECLocked(pkt *rtp.Packet, arrivalTime int64) {
|
|
statsBefore := b.fecDecoder.Stats()
|
|
recovered := b.fecDecoder.DecodeFEC(pkt)
|
|
statsAfter := b.fecDecoder.Stats()
|
|
|
|
fecPackets := statsAfter.FECPacketsReceived - statsBefore.FECPacketsReceived
|
|
fecBytes := statsAfter.FECBytesReceived - statsBefore.FECBytesReceived
|
|
fecPacketsDiscarded := statsAfter.FECPacketsDiscarded - statsBefore.FECPacketsDiscarded
|
|
fecPacketsRecovered := statsAfter.PacketsRecovered - statsBefore.PacketsRecovered
|
|
if b.rtpStats != nil {
|
|
b.rtpStats.UpdateFEC(
|
|
fecPackets,
|
|
fecBytes,
|
|
fecPacketsDiscarded,
|
|
fecPacketsRecovered,
|
|
)
|
|
}
|
|
|
|
prometheus.RecordFECUpstream(int(fecPackets), int(fecPacketsRecovered), int(fecPacketsDiscarded), fecBytes)
|
|
|
|
if len(recovered) > 0 && b.fecPktBuf == nil {
|
|
b.fecPktBuf = make([]byte, bucket.RTPMaxPktSize)
|
|
}
|
|
for _, rp := range recovered {
|
|
n, err := rp.MarshalTo(b.fecPktBuf)
|
|
if err != nil {
|
|
b.logger.Warnw("could not marshal fec recovered packet", err, "ssrc", b.SSRC(), "sn", rp.SequenceNumber)
|
|
continue
|
|
}
|
|
|
|
// recovered packets flow through the regular pipeline: they are
|
|
// forwarded downstream and stop NACKs for the lost sequence numbers.
|
|
// They do not re-enter the decoder because chained recovery already
|
|
// completed within DecodeFEC. RTCP is generated once by the caller
|
|
// after all recovered packets have updated NACK state.
|
|
b.handleIncomingPacketLocked(b.fecPktBuf[:n], rp, arrivalTime, false, true, nil, 0)
|
|
}
|
|
}
|
|
|
|
func (b *BufferBase) updateNACKState(sequenceNumber uint16, flowState rtpstats.RTPFlowState) {
|
|
if b.nacker == nil {
|
|
return
|
|
}
|
|
|
|
b.nacker.Remove(sequenceNumber)
|
|
|
|
for lost := flowState.LossStartInclusive; lost != flowState.LossEndExclusive; lost++ {
|
|
b.nacker.Push(uint16(lost))
|
|
}
|
|
}
|
|
|
|
func (b *BufferBase) updateOOBNACKState(sequenceNumber uint16, arrivalTime int64, size int) {
|
|
if b.nacker == nil || !b.params.IsOOBSequenceNumber {
|
|
return
|
|
}
|
|
|
|
fsLite := b.rtpStatsLite.Update(arrivalTime, size, sequenceNumber)
|
|
if fsLite.IsNotHandled {
|
|
return
|
|
}
|
|
|
|
b.nacker.Remove(sequenceNumber)
|
|
|
|
for lost := fsLite.LossStartInclusive; lost != fsLite.LossEndExclusive; lost++ {
|
|
b.nacker.Push(uint16(lost))
|
|
}
|
|
}
|
|
|
|
func (b *BufferBase) processAudioSsrcLevelHeaderExtension(p *rtp.Packet, arrivalTime int64) {
|
|
if b.audioLevelExtID == 0 {
|
|
return
|
|
}
|
|
|
|
if e := p.GetExtension(b.audioLevelExtID); e != nil {
|
|
ext := rtp.AudioLevelExtension{}
|
|
if err := ext.Unmarshal(e); err == nil {
|
|
b.audioLevel.ObserveWithRTPTimestamp(ext.Level, p.Timestamp, arrivalTime)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (b *BufferBase) handleCodecChange(newPT uint8) {
|
|
var (
|
|
codecFound, rtxFound bool
|
|
rtxPt uint8
|
|
newCodec webrtc.RTPCodecParameters
|
|
)
|
|
for _, codec := range b.rtpParameters.Codecs {
|
|
if !codecFound && uint8(codec.PayloadType) == newPT {
|
|
newCodec = codec
|
|
codecFound = true
|
|
}
|
|
|
|
if mime.IsMimeTypeStringRTX(codec.MimeType) && strings.Contains(codec.SDPFmtpLine, fmt.Sprintf("apt=%d", newPT)) {
|
|
rtxFound = true
|
|
rtxPt = uint8(codec.PayloadType)
|
|
}
|
|
|
|
if codecFound && rtxFound {
|
|
break
|
|
}
|
|
}
|
|
if !codecFound {
|
|
codecNotFoundCount := b.codecPayloadTypeNotFoundCount.Inc()
|
|
if (codecNotFoundCount-1)%100 == 0 {
|
|
b.logger.Errorw(
|
|
"could not find codec for new payload type", nil,
|
|
"pt", newPT,
|
|
"rtpParameters", b.rtpParameters,
|
|
"count", codecNotFoundCount,
|
|
)
|
|
}
|
|
return
|
|
}
|
|
|
|
b.logger.Infow(
|
|
"codec changed",
|
|
"oldPayload", b.payloadType, "newPayload", newPT,
|
|
"oldRtxPayload", b.rtxPayloadType, "newRtxPayload", rtxPt,
|
|
"oldMime", b.mime, "newMime", newCodec.MimeType,
|
|
)
|
|
b.payloadType = newPT
|
|
b.rtxPayloadType = rtxPt
|
|
b.mime = mime.NormalizeMimeType(newCodec.MimeType)
|
|
|
|
if f := b.onCodecChange; f != nil {
|
|
go f(newCodec)
|
|
}
|
|
}
|
|
|
|
func (b *BufferBase) getExtPacket(
|
|
rtpPacket *rtp.Packet,
|
|
arrivalTime int64,
|
|
isBuffered bool,
|
|
flowState rtpstats.RTPFlowState,
|
|
) *ExtPacket {
|
|
ep := ExtPacketFactory.Get().(*ExtPacket)
|
|
*ep = ExtPacket{
|
|
Arrival: arrivalTime,
|
|
ExtSequenceNumber: flowState.ExtSequenceNumber,
|
|
StreamGeneration: b.streamGeneration,
|
|
ExtTimestamp: flowState.ExtTimestamp,
|
|
pkt: *rtpPacket,
|
|
VideoLayer: VideoLayer{
|
|
Spatial: InvalidLayerSpatial,
|
|
Temporal: InvalidLayerTemporal,
|
|
},
|
|
IsOutOfOrder: flowState.IsOutOfOrder,
|
|
IsBuffered: isBuffered,
|
|
}
|
|
// rtpPacket may be reused scratch, copy what it backs
|
|
ep.pkt.Extensions = append(ep.extensions[:0], rtpPacket.Extensions...)
|
|
ep.pkt.CSRC = slices.Clone(rtpPacket.CSRC)
|
|
ep.Packet = &ep.pkt
|
|
|
|
if len(ep.Packet.Payload) == 0 {
|
|
// padding only packet, nothing else to do
|
|
return ep
|
|
}
|
|
|
|
if err := b.processVideoPacket(ep); err != nil {
|
|
ReleaseExtPacket(ep)
|
|
return nil
|
|
}
|
|
|
|
if b.absCaptureTimeExtID != 0 {
|
|
if extData := rtpPacket.GetExtension(b.absCaptureTimeExtID); len(extData) != 0 {
|
|
if err := ep.absCaptureTime.Unmarshal(extData); err == nil {
|
|
ep.AbsCaptureTimeExt = &ep.absCaptureTime
|
|
}
|
|
}
|
|
}
|
|
|
|
return ep
|
|
}
|
|
|
|
func (b *BufferBase) processVideoPacket(ep *ExtPacket) error {
|
|
if b.codecType != webrtc.RTPCodecTypeVideo {
|
|
return nil
|
|
}
|
|
|
|
ep.Temporal = 0
|
|
var videoSize []codec.VideoSize
|
|
if b.ddParser != nil {
|
|
ddVal, videoLayer, err := b.ddParser.Parse(ep.Packet)
|
|
if err != nil {
|
|
if errors.Is(err, ErrDDExtentionNotFound) {
|
|
if b.mime == mime.MimeTypeVP8 || b.mime == mime.MimeTypeVP9 {
|
|
b.logger.Infow("dd extension not found, disable dd parser")
|
|
b.ddParser = nil
|
|
b.createFrameRateCalculator()
|
|
}
|
|
} else {
|
|
return err
|
|
}
|
|
} else if ddVal != nil {
|
|
if ddVal.Restarted && ddVal.Descriptor.AttachedStructure == nil {
|
|
// the restart dropped the structure, so later packets cannot be parsed until a key frame
|
|
b.StartKeyFrameSeeder()
|
|
}
|
|
ep.DependencyDescriptor = ddVal
|
|
ep.VideoLayer = videoLayer
|
|
videoSize = ExtractDependencyDescriptorVideoSize(ddVal.Descriptor)
|
|
// DD-TODO : notify active decode target change if changed.
|
|
}
|
|
}
|
|
|
|
switch b.mime {
|
|
case mime.MimeTypeVP8:
|
|
vp8Packet := &ep.vp8
|
|
if err := vp8Packet.Unmarshal(ep.Packet.Payload); err != nil {
|
|
b.logger.Warnw("could not unmarshal VP8 packet", err)
|
|
return err
|
|
}
|
|
ep.IsKeyFrame = vp8Packet.IsKeyFrame
|
|
if ep.DependencyDescriptor == nil {
|
|
ep.Temporal = int32(vp8Packet.TID)
|
|
|
|
if ep.IsKeyFrame {
|
|
if sz := codec.ExtractVP8VideoSize(vp8Packet, ep.Packet.Payload); sz.Width > 0 && sz.Height > 0 {
|
|
videoSize = append(videoSize, sz)
|
|
}
|
|
}
|
|
} else {
|
|
// vp8 with DependencyDescriptor enabled, use the TID from the descriptor
|
|
vp8Packet.TID = uint8(ep.Temporal)
|
|
}
|
|
ep.VP8 = vp8Packet
|
|
ep.Spatial = InvalidLayerSpatial // vp8 don't have spatial scalability, reset to invalid
|
|
|
|
case mime.MimeTypeVP9:
|
|
if ep.DependencyDescriptor == nil {
|
|
vp9Packet := &ep.vp9
|
|
_, err := vp9Packet.Unmarshal(ep.Packet.Payload)
|
|
if err != nil {
|
|
b.logger.Warnw("could not unmarshal VP9 packet", err)
|
|
return err
|
|
}
|
|
ep.VideoLayer = VideoLayer{
|
|
Spatial: int32(vp9Packet.SID),
|
|
Temporal: int32(vp9Packet.TID),
|
|
}
|
|
ep.VP9 = vp9Packet
|
|
ep.IsKeyFrame = codec.IsVP9KeyFrame(vp9Packet, ep.Packet.Payload)
|
|
|
|
if ep.IsKeyFrame {
|
|
for i := 0; i < len(vp9Packet.Width); i++ {
|
|
videoSize = append(videoSize, codec.VideoSize{
|
|
Width: uint32(vp9Packet.Width[i]),
|
|
Height: uint32(vp9Packet.Height[i]),
|
|
})
|
|
}
|
|
}
|
|
} else {
|
|
ep.IsKeyFrame = codec.IsVP9KeyFrame(nil, ep.Packet.Payload)
|
|
}
|
|
|
|
case mime.MimeTypeH264:
|
|
ep.IsKeyFrame = codec.IsH264KeyFrame(ep.Packet.Payload)
|
|
ep.Spatial = InvalidLayerSpatial // h.264 don't have spatial scalability, reset to invalid
|
|
|
|
// Check H264 key frame video size
|
|
if ep.IsKeyFrame {
|
|
if sz := codec.ExtractH264VideoSize(ep.Packet.Payload); sz.Width > 0 && sz.Height > 0 {
|
|
videoSize = append(videoSize, sz)
|
|
}
|
|
}
|
|
|
|
case mime.MimeTypeAV1:
|
|
ep.IsKeyFrame = codec.IsAV1KeyFrame(ep.Packet.Payload)
|
|
|
|
case mime.MimeTypeH265:
|
|
ep.IsKeyFrame = codec.IsH265KeyFrame(ep.Packet.Payload)
|
|
if ep.DependencyDescriptor == nil {
|
|
if len(ep.Packet.Payload) < 2 {
|
|
b.logger.Warnw("invalid H265 packet", nil, "payloadLen", len(ep.Packet.Payload))
|
|
return errors.New("invalid H265 packet")
|
|
}
|
|
ep.VideoLayer = VideoLayer{
|
|
Temporal: int32(ep.Packet.Payload[1]&0x07) - 1,
|
|
}
|
|
ep.Spatial = InvalidLayerSpatial
|
|
|
|
if ep.IsKeyFrame {
|
|
if sz := codec.ExtractH265VideoSize(ep.Packet.Payload); sz.Width > 0 && sz.Height > 0 {
|
|
videoSize = append(videoSize, sz)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if ep.IsKeyFrame {
|
|
if b.rtpStats != nil {
|
|
b.rtpStats.UpdateKeyFrame(1)
|
|
}
|
|
prometheus.IncrementKeyFrames(prometheus.Incoming)
|
|
}
|
|
|
|
if len(videoSize) > 0 {
|
|
b.checkVideoSizeChange(videoSize)
|
|
}
|
|
|
|
b.doFpsCalc(ep)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (b *BufferBase) patchExtPacket(ep *ExtPacket, buf []byte) *ExtPacket {
|
|
n, err := b.getPacketLocked(buf, ep.ExtSequenceNumber)
|
|
if err != nil {
|
|
packetNotFoundCount := b.packetNotFoundCount.Inc()
|
|
if (packetNotFoundCount-1)%20 == 0 {
|
|
b.logger.Warnw(
|
|
"could not get packet from bucket", err,
|
|
"sn", ep.Packet.SequenceNumber,
|
|
"headSN", b.bucket.HeadSequenceNumber(),
|
|
"count", packetNotFoundCount,
|
|
"rtpStats", b.rtpStats,
|
|
"rtpStatsLite", b.rtpStatsLite,
|
|
"snRangeMap", b.snRangeMap,
|
|
)
|
|
}
|
|
return nil
|
|
}
|
|
ep.RawPacket = buf[:n]
|
|
|
|
// parse the header again from the new buffer so extension and CSRC slices point into it,
|
|
// then point the payload there too, ep owns its rtp.Packet
|
|
payloadStart, err := ep.Packet.Header.Unmarshal(buf[:n])
|
|
if err != nil {
|
|
b.logger.Warnw("could not unmarshal header", err, "sn", ep.Packet.SequenceNumber)
|
|
return nil
|
|
}
|
|
payloadEnd := payloadStart + len(ep.Packet.Payload)
|
|
if payloadEnd > n {
|
|
b.logger.Warnw("unexpected marshal size", nil, "max", n, "need", payloadEnd)
|
|
return nil
|
|
}
|
|
ep.Packet.Payload = buf[payloadStart:payloadEnd]
|
|
|
|
return ep
|
|
}
|
|
|
|
func (b *BufferBase) flushExtPackets() {
|
|
b.Lock()
|
|
defer b.Unlock()
|
|
b.flushExtPacketsLocked()
|
|
}
|
|
|
|
func (b *BufferBase) flushExtPacketsLocked() {
|
|
for b.extPackets.Len() > 0 {
|
|
ep := b.extPackets.PopFront()
|
|
ReleaseExtPacket(ep)
|
|
}
|
|
b.extPackets.Clear()
|
|
}
|
|
|
|
func (b *BufferBase) maybeGrowBucket(now int64) {
|
|
if now-b.lastBucketCapCheckAt < bucketCapCheckInterval {
|
|
return
|
|
}
|
|
|
|
b.lastBucketCapCheckAt = now
|
|
|
|
// check and allocate in a go routine, away from the forwarding path
|
|
go func() {
|
|
b.Lock()
|
|
defer b.Unlock()
|
|
|
|
cap := b.bucket.Capacity()
|
|
maxPkts := b.params.MaxVideoPkts
|
|
if b.codecType == webrtc.RTPCodecTypeAudio {
|
|
maxPkts = b.params.MaxAudioPkts
|
|
}
|
|
if cap >= maxPkts {
|
|
return
|
|
}
|
|
|
|
oldCap := cap
|
|
if deltaInfo := b.rtpStats.DeltaInfo(b.ppsSnapshotId); deltaInfo != nil {
|
|
duration := deltaInfo.EndTime.Sub(deltaInfo.StartTime)
|
|
if duration < 500*time.Millisecond {
|
|
return
|
|
}
|
|
|
|
pps := int(time.Duration(deltaInfo.Packets) * time.Second / duration)
|
|
for pps > cap && cap < maxPkts {
|
|
cap = b.bucket.Grow()
|
|
}
|
|
if cap > oldCap {
|
|
b.logger.Infow(
|
|
"grow bucket",
|
|
"from", oldCap,
|
|
"to", cap,
|
|
"pps", pps,
|
|
"deltaInfo", deltaInfo,
|
|
"rtpStats", b.rtpStats,
|
|
)
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
|
|
func (b *BufferBase) doFpsCalc(ep *ExtPacket) {
|
|
if b.isPaused || b.frameRateCalculated || len(ep.Packet.Payload) == 0 {
|
|
return
|
|
}
|
|
|
|
spatial := ep.Spatial
|
|
if spatial < 0 || int(spatial) >= len(b.frameRateCalculator) {
|
|
spatial = 0
|
|
}
|
|
if fr := b.frameRateCalculator[spatial]; fr != nil {
|
|
if fr.RecvPacket(ep) {
|
|
complete := true
|
|
for _, fr2 := range b.frameRateCalculator {
|
|
if fr2 != nil && !fr2.Completed() {
|
|
complete = false
|
|
break
|
|
}
|
|
}
|
|
if complete {
|
|
b.frameRateCalculated = true
|
|
if f := b.onFpsChanged; f != nil {
|
|
go f()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (b *BufferBase) SetSenderReportData(srData *livekit.RTCPSenderReportState) {
|
|
b.RLock()
|
|
didSet := false
|
|
if b.rtpStats != nil {
|
|
didSet = b.rtpStats.SetRtcpSenderReportData(srData)
|
|
}
|
|
b.RUnlock()
|
|
|
|
if didSet {
|
|
if cb := b.getOnRtcpSenderReport(); cb != nil {
|
|
cb()
|
|
}
|
|
}
|
|
}
|
|
|
|
func (b *BufferBase) GetSenderReportData() *livekit.RTCPSenderReportState {
|
|
b.RLock()
|
|
defer b.RUnlock()
|
|
|
|
if b.rtpStats != nil {
|
|
return b.rtpStats.GetRtcpSenderReportData()
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (b *BufferBase) GetPacket(buff []byte, esn uint64) (int, error) {
|
|
// read only, so retransmissions for many subscribers do not serialize
|
|
b.RLock()
|
|
defer b.RUnlock()
|
|
|
|
return b.getPacketLocked(buff, esn)
|
|
}
|
|
|
|
// GetPacketForStream rejects metadata from an earlier cache generation. The
|
|
// generation check and packet copy share the lock used to reset and refill the
|
|
// bucket, so a restart cannot substitute new media for an in-flight repair.
|
|
func (b *BufferBase) GetPacketForStream(buff []byte, esn uint64, generation uint64) (int, error) {
|
|
b.RLock()
|
|
defer b.RUnlock()
|
|
|
|
if b.isClosed.Load() {
|
|
return 0, io.EOF
|
|
}
|
|
if generation == 0 || generation != b.streamGeneration {
|
|
return 0, bucket.ErrPacketMismatch
|
|
}
|
|
return b.bucket.GetPacket(buff, esn)
|
|
}
|
|
|
|
func (b *BufferBase) getPacketLocked(buff []byte, esn uint64) (int, error) {
|
|
if b.isClosed.Load() {
|
|
return 0, io.EOF
|
|
}
|
|
return b.bucket.GetPacket(buff, esn)
|
|
}
|
|
|
|
func (b *BufferBase) GetStats() *livekit.RTPStats {
|
|
b.RLock()
|
|
defer b.RUnlock()
|
|
|
|
if b.rtpStats == nil {
|
|
return nil
|
|
}
|
|
|
|
return b.rtpStats.ToProto()
|
|
}
|
|
|
|
func (b *BufferBase) GetDeltaStats() *StreamStatsWithLayers {
|
|
b.RLock()
|
|
defer b.RUnlock()
|
|
|
|
if b.rtpStats == nil {
|
|
return nil
|
|
}
|
|
|
|
deltaStats := b.rtpStats.DeltaInfo(b.deltaStatsSnapshotId)
|
|
if deltaStats == nil {
|
|
return nil
|
|
}
|
|
|
|
return &StreamStatsWithLayers{
|
|
RTPStats: deltaStats,
|
|
Layers: map[int32]*rtpstats.RTPDeltaInfo{
|
|
0: deltaStats,
|
|
},
|
|
}
|
|
}
|
|
|
|
func (b *BufferBase) GetDeltaStatsLite() *rtpstats.RTPDeltaInfoLite {
|
|
b.RLock()
|
|
defer b.RUnlock()
|
|
|
|
if b.rtpStatsLite == nil {
|
|
return nil
|
|
}
|
|
|
|
return b.rtpStatsLite.DeltaInfoLite(b.liteStatsSnapshotId)
|
|
}
|
|
|
|
func (b *BufferBase) GetLastSenderReportTime() time.Time {
|
|
b.RLock()
|
|
defer b.RUnlock()
|
|
|
|
if b.rtpStats == nil {
|
|
return time.Time{}
|
|
}
|
|
|
|
return b.rtpStats.LastSenderReportTime()
|
|
}
|
|
|
|
func (b *BufferBase) GetAudioLevel() (float64, bool) {
|
|
b.RLock()
|
|
defer b.RUnlock()
|
|
|
|
if b.audioLevel == nil {
|
|
return 0, false
|
|
}
|
|
|
|
return b.audioLevel.GetLevel(mono.UnixNano())
|
|
}
|
|
|
|
func (b *BufferBase) OnRtcpSenderReport(fn func()) {
|
|
b.Lock()
|
|
b.onRtcpSenderReport = fn
|
|
b.Unlock()
|
|
}
|
|
|
|
func (b *BufferBase) getOnRtcpSenderReport() func() {
|
|
b.RLock()
|
|
defer b.RUnlock()
|
|
|
|
return b.onRtcpSenderReport
|
|
}
|
|
|
|
func (b *BufferBase) OnFpsChanged(f func()) {
|
|
b.Lock()
|
|
b.onFpsChanged = f
|
|
b.Unlock()
|
|
}
|
|
|
|
func (b *BufferBase) OnVideoSizeChanged(fn func([]codec.VideoSize)) {
|
|
b.Lock()
|
|
b.onVideoSizeChanged = fn
|
|
b.Unlock()
|
|
}
|
|
|
|
func (b *BufferBase) OnCodecChange(fn func(webrtc.RTPCodecParameters)) {
|
|
b.Lock()
|
|
b.onCodecChange = fn
|
|
b.Unlock()
|
|
}
|
|
|
|
func (b *BufferBase) OnStreamRestart(fn func(string)) {
|
|
b.Lock()
|
|
b.onStreamRestart = fn
|
|
b.Unlock()
|
|
}
|
|
|
|
// checkVideoSizeChange checks if video size has changed for a specific spatial layer and fires callback
|
|
func (b *BufferBase) checkVideoSizeChange(videoSizes []codec.VideoSize) {
|
|
if len(videoSizes) > len(b.currentVideoSize) {
|
|
b.logger.Warnw(
|
|
"video size index out of range", nil,
|
|
"newSize", videoSizes,
|
|
"currentVideoSize", b.currentVideoSize,
|
|
)
|
|
return
|
|
}
|
|
|
|
if len(videoSizes) < len(b.currentVideoSize) {
|
|
videoSizes = append(videoSizes, make([]codec.VideoSize, len(b.currentVideoSize)-len(videoSizes))...)
|
|
}
|
|
|
|
changed := false
|
|
for i, sz := range videoSizes {
|
|
if b.currentVideoSize[i].Width != sz.Width || b.currentVideoSize[i].Height != sz.Height {
|
|
changed = true
|
|
break
|
|
}
|
|
}
|
|
|
|
if changed {
|
|
b.logger.Debugw("video size changed", "from", b.currentVideoSize, "to", videoSizes)
|
|
copy(b.currentVideoSize[:], videoSizes[:])
|
|
if b.onVideoSizeChanged != nil {
|
|
go b.onVideoSizeChanged(videoSizes)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (b *BufferBase) GetTemporalLayerFpsForSpatial(layer int32) []float32 {
|
|
b.RLock()
|
|
defer b.RUnlock()
|
|
|
|
if int(layer) >= len(b.frameRateCalculator) {
|
|
return nil
|
|
}
|
|
|
|
if fc := b.frameRateCalculator[layer]; fc != nil {
|
|
return fc.GetFrameRate()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (b *BufferBase) StartKeyFrameSeeder() {
|
|
if b.codecType == webrtc.RTPCodecTypeVideo {
|
|
go b.seedKeyFrame(b.keyFrameSeederGeneration.Inc())
|
|
}
|
|
}
|
|
|
|
func (b *BufferBase) StopKeyFrameSeeder() {
|
|
b.keyFrameSeederGeneration.Inc()
|
|
}
|
|
|
|
func (b *BufferBase) seedKeyFrame(keyFrameSeederGeneration int32) {
|
|
// a key frame is needed especially when using Dependency Descriptor
|
|
// to get the DD structure which is used in parsing subsequent packets,
|
|
// till then packets are dropped which results in stream tracker not
|
|
// getting any data which means it does not declare layer start.
|
|
//
|
|
// send gratuitous PLIs for some time or until a key frame is seen to
|
|
// get the engine rolling
|
|
timer := time.NewTimer(30 * time.Second)
|
|
defer timer.Stop()
|
|
|
|
ticker := time.NewTicker(time.Second)
|
|
defer ticker.Stop()
|
|
|
|
initialCount := uint32(0)
|
|
b.RLock()
|
|
rtpStats := b.rtpStats
|
|
lgr := b.logger
|
|
b.RUnlock()
|
|
lgr.Debugw("starting key frame seeder", "generation", keyFrameSeederGeneration)
|
|
if rtpStats == nil {
|
|
lgr.Debugw("cannot do key frame seeding without stats", "generation", keyFrameSeederGeneration)
|
|
return
|
|
}
|
|
initialCount, _ = rtpStats.KeyFrame()
|
|
|
|
for {
|
|
if b.isClosed.Load() || b.keyFrameSeederGeneration.Load() != keyFrameSeederGeneration {
|
|
lgr.Debugw(
|
|
"stopping key frame seeder: stopped",
|
|
"generation", keyFrameSeederGeneration,
|
|
"currentGeneration", b.keyFrameSeederGeneration.Load(),
|
|
)
|
|
return
|
|
}
|
|
|
|
select {
|
|
case <-timer.C:
|
|
lgr.Debugw("stopping key frame seeder: timeout", "generation", keyFrameSeederGeneration)
|
|
return
|
|
|
|
case <-ticker.C:
|
|
cnt, last := rtpStats.KeyFrame()
|
|
if cnt > initialCount {
|
|
lgr.Debugw(
|
|
"stopping key frame seeder: received key frame",
|
|
"generation", keyFrameSeederGeneration,
|
|
"keyFrameCountInitial", initialCount,
|
|
"keyFrameCount", cnt,
|
|
"lastKeyFrame", last,
|
|
)
|
|
return
|
|
}
|
|
|
|
b.SendPLI(false)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (b *BufferBase) GetNACKPairs() []rtcp.NackPair {
|
|
b.RLock()
|
|
defer b.RUnlock()
|
|
|
|
return b.GetNACKPairsLocked()
|
|
}
|
|
|
|
func (b *BufferBase) GetNACKPairsLocked() []rtcp.NackPair {
|
|
if b.nacker == nil {
|
|
return nil
|
|
}
|
|
|
|
pairs, numSeqNumsNacked := b.nacker.Pairs()
|
|
if !b.params.IsOOBSequenceNumber {
|
|
if b.rtpStats != nil {
|
|
b.rtpStats.UpdateNack(uint32(numSeqNumsNacked))
|
|
}
|
|
} else {
|
|
if b.rtpStatsLite != nil {
|
|
b.rtpStatsLite.UpdateNack(uint32(numSeqNumsNacked))
|
|
}
|
|
}
|
|
|
|
return pairs
|
|
}
|
|
|
|
func (b *BufferBase) GetRtcpReceptionReportLocked(proxyLoss uint8) *rtcp.ReceptionReport {
|
|
if b.rtpStats == nil {
|
|
return nil
|
|
}
|
|
|
|
return b.rtpStats.GetRtcpReceptionReport(b.params.SSRC, proxyLoss, b.rrSnapshotId)
|
|
}
|
|
|
|
// ---------------------------------------------------------------
|
|
|
|
// minRTXRepairedHistory is the smallest RTX repair history, see
|
|
// rtxRepairedHistorySize.
|
|
const minRTXRepairedHistory = 512
|
|
|
|
// rtxRepairedHistorySize covers at least the video packet cache, so that two
|
|
// repairs sharing a slot are rarely both cached. When they are, the earlier
|
|
// repair counts as unusable, see isRTXRepairedLocked.
|
|
func (b *BufferBase) rtxRepairedHistorySize() int {
|
|
return max(minRTXRepairedHistory, b.params.MaxVideoPkts)
|
|
}
|
|
|
|
// markRTXRepairedLocked records that a packet was repaired from RTX. Its
|
|
// header comes from the RTX packet: libwebrtc adds a MID to retransmissions
|
|
// until the RTX SSRC is acknowledged and sets abs-send-time and transport-cc
|
|
// at send time, so it differs from the media FlexFEC protected. Like
|
|
// libwebrtc, such packets are not used to recover others, see
|
|
// isRTXRepairedLocked. Packets recovered by FEC are exact and are not marked.
|
|
//
|
|
// A retransmission of a packet that is already cached is not marked: the
|
|
// bucket rejects it as a duplicate and keeps the exact packet.
|
|
func (b *BufferBase) markRTXRepairedLocked(sequenceNumber uint16) {
|
|
extSN, ok := b.publisherExtSequenceNumberLocked(sequenceNumber)
|
|
if !ok || b.isPacketCachedLocked(sequenceNumber) {
|
|
return
|
|
}
|
|
if b.rtxRepairedSNs == nil {
|
|
b.rtxRepairedSNs = make([]uint64, b.rtxRepairedHistorySize())
|
|
}
|
|
// keep the newest mark of a slot: a late retry of an older packet must not
|
|
// clear a newer repair, which isRTXRepairedLocked still has to exclude
|
|
slot := extSN % uint64(len(b.rtxRepairedSNs))
|
|
b.rtxRepairedSNs[slot] = max(b.rtxRepairedSNs[slot], extSN+1)
|
|
}
|
|
|
|
// isRTXRepairedLocked reports whether the packet with the given extended
|
|
// sequence number of the publisher's space was repaired from RTX. A slot that
|
|
// a later repair reused no longer tells whether this packet was repaired as
|
|
// well, so it counts as repaired: a packet that cannot be used only costs a
|
|
// recovery, a repaired packet used to recover would corrupt it.
|
|
func (b *BufferBase) isRTXRepairedLocked(extendedSN uint64) bool {
|
|
if len(b.rtxRepairedSNs) == 0 {
|
|
return false
|
|
}
|
|
marked := b.rtxRepairedSNs[extendedSN%uint64(len(b.rtxRepairedSNs))]
|
|
return marked != 0 && marked >= extendedSN+1
|
|
}
|
|
|
|
// isRTXRepairedSequenceNumberLocked is isRTXRepairedLocked for a sequence
|
|
// number of the publisher's space.
|
|
func (b *BufferBase) isRTXRepairedSequenceNumberLocked(sequenceNumber uint16) bool {
|
|
extSN, ok := b.publisherExtSequenceNumberLocked(sequenceNumber)
|
|
return ok && b.isRTXRepairedLocked(extSN)
|
|
}
|
|
|
|
// publisherExtSequenceNumberLocked extends a sequence number of the
|
|
// publisher's space relative to the highest one received.
|
|
func (b *BufferBase) publisherExtSequenceNumberLocked(sequenceNumber uint16) (uint64, bool) {
|
|
if b.rtpStats == nil {
|
|
return 0, false
|
|
}
|
|
highestSequenceNumber := b.rtpStats.ExtendedHighestSequenceNumber()
|
|
extendedSequenceNumber := int64(highestSequenceNumber) + int64(int16(sequenceNumber-uint16(highestSequenceNumber)))
|
|
if extendedSequenceNumber < 0 {
|
|
return 0, false
|
|
}
|
|
return uint64(extendedSequenceNumber), true
|
|
}
|