Commit Graph
1311 Commits
Author SHA1 Message Date
Raja SubramanianandClaude Fable 5.2 733991f781 rtpstats: keep per-packet state together and use a plain mutex (#5237)
Update runs for every packet. With many streams, most of its cost is
memory access across three structs and about 10 cache lines.

- use sync.Mutex, all readers are short getters
- embed the base structs, the WrapAround trackers, the history bitmap
  and the RTP converter by value
- group the fields that every packet updates, move gapHistogram after them
- compute expectedTSJump only when it is used

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-18 16:50:21 +05:30
Raja SubramanianandClaude Fable 5.2 de817c7d50 sfu: return the stream generation from HandleIncomingPacket (#5235)
A caller that writes a packet and forwards it in the same call gets the
generation it needs for a later ReadRTP of that packet.

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-18 00:24:03 +05:30
Raja SubramanianandClaude Fable 5.2 5ee9dd8da1 sfu: tighten RTP stats restart detection and RTCP report handling (#5220)
* sfu: tighten RTP stats restart detection and RTCP report handling

- A stream restart is a forward-moving run of rejected packets that
  lasts 1 s with no accepted packet, not 5 rejected packets.
- Receiver reports with a wrong cycle count are aligned to the highest
  sent sequence number and no longer dropped as runaway.
- No sender report is sent while publisher sender reports advance at
  less than half or more than twice the clock rate.
- Fold repeated drop and logging blocks into helpers and remove
  unreachable branches.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* sfu: use only the low 16 bits of the receiver report sequence number

Cycle correction could move a report before the start, underflow the
saved highest and block every later report. Map the report to the
latest matching sequence number at or before the highest sent, and
drop the misalignment bookkeeping.

A publisher sender report time stamp that does not advance is now
treated as an off clock rate, not as unmeasured.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* sfu: bound the receiver report distance and key the unchanged check on the accepted report

A report ahead of the highest sent mapped into the previous cycle once
the stream was longer than one cycle. A report must now be within half
a cycle behind the highest sent.

A repeated stale report matched the last received report, not the last
accepted one, and replaced newer loss and jitter values.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* Bump mediatransportutil for the OWD estimator fix

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* sfu: log the source stream generation in packetMeta

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* rtpstats: address review comments from Jules

Addresses only the comments from Jules (jtestard) on #5220.

Restart detection
- a reordered or duplicate packet of a run does not reset the run
- padding moves a run forward in sequence number and does not count
- only a packet with payload ends a run, a skipped sequence number does not
- comments on false positives and on the minimum run duration

Publisher clock rate
- measure only across publisher reports at least 1 s apart
- an older publisher report starts a new measurement
- comment on why Seed does not copy the measurement
- drop the reportTime log field, it repeats the report time

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* rtpstats: test publisher clock rate across sources with different timestamp bases

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* rtpstats: address review comments from Anunay

Addresses the comments from Anunay (anunaym14) on #5220.

- skip a sender report only when the publisher rate is low, a pair of
  publisher reports across an unmute reads high and the report is correct
- comment on why a declared restart run is final

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* rtpstats: comment on why a high publisher clock rate is not skipped

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-17 21:52:32 +05:30
Perfloop Agent 1d0bae0f76 sfu: bind retransmissions to the source cache generation (#5213)
* sfu: reproduce retransmission across receiver restart

Signed-off-by: Perfloop Agent <agent@perfloop.ai>

* sfu: use an atomic counter for cache generations

Signed-off-by: Perfloop Agent <agent@perfloop.ai>

* sfu: give test packets a stream generation

Signed-off-by: Perfloop Agent <agent@perfloop.ai>

---------

Signed-off-by: Perfloop Agent <agent@perfloop.ai>
2026-11-17 15:43:00 +05:30
anupamgit d5508f68ff sfu: roll back RTP state when pausing on downgrade (#5217)
* sfu: roll back RTP state when pausing on downgrade

Dropped higher-layer packets advanced the outgoing timestamp, so a lower layer that stayed slightly behind was rejected on every keyframe.

* sfu: test both sides of the downgrade switch threshold

Set the trails 10ms under and over the threshold, and pad a packet dropped after a sequence gap.
2026-11-17 10:47:35 +05:30
Dipesh Babu c641421db1 sfu: prevent 32-bit overflow in data-channel bitrate calculations (#5210) 2026-11-15 18:59:33 +05:30
armaan4658 9ceb006331 sfu: don't count opus useinbandfec=0 as FEC in the down track (#5182)
* sfu: don't count opus useinbandfec=0 as FEC in the down track

DownTrack decides whether opus FEC is on by looking for "fec" anywhere
in the fmtp line, both on Bind and when the upstream codec changes.
"useinbandfec=0" contains "fec", so a publisher that turns FEC off is
scored as if FEC were on. The connection quality scorer then lowers the
packet loss weight for opus from 8.0 to 5.33, and a subscriber with 8%
audio loss is reported as GOOD instead of POOR. The receiver side
already checks for useinbandfec=1, so the publisher's and subscribers'
scores disagree about the same stream.

Add utils.IsOpusInbandFECEnabled, which reads the useinbandfec
parameter by key and treats only a value of 1 as on, and use it for
all three FEC checks in the down track. The RED path already looked for
useinbandfec=1 and only gains tolerance of spaces around the parameter.

* sfu: read FEC from the upstream codec when the down track's codec changes

handleUpstreamCodecChange took the FEC flag from the codec the subscriber
negotiated, while Bind takes it from the upstream codec. FEC describes
what the publisher encodes, so after RED falls back to opus, a publisher
with useinbandfec=1 and a subscriber with useinbandfec=0 was scored as
having no FEC, and the reverse as having FEC.

Decide FEC in one place from the upstream codecs, using the upstream opus
codec for RED, and use it in both Bind and the codec change path. Log the
flag on codec change as Bind already does.

* sfu: use the same useinbandfec=1 check as the RED path for FEC

Drop utils.IsOpusInbandFECEnabled and check FEC with
strings.Contains(strings.ToLower(fmtp), "useinbandfec=1"), as the RED
path already did.

* sfu: move the FEC check to utils and match useinbandfec with a regex

Move the check to utils.IsFECEnabled, as asked in review, and match
useinbandfec=1 as one fmtp parameter with a regex, ignoring case and
spaces around the parameter and the "=". A publisher that sends
"minptime=10; useinbandfec = 1" was scored as having no FEC. Move the
tests to utils/helpers_test.go.
2026-11-12 15:27:03 +05:30
Mansur AkhmadiandMansur Akhmadi 08770220cd sfu: don't add a closed down track in SetReceiver (#5179)
Co-authored-by: Mansur Akhmadi <10739460+mansur-akhmadi@users.noreply.github.com>
2026-11-12 12:39:51 +05:30
Konstantinos Papazacharopoulos 7352c1d670 fix(sfu): don't reset a stream tracker that is unpaused while not paused (#5191)
Since #4453, StreamTrackerManager.UpdateTrackInfo calls
SetPaused(trackInfo.Muted) on every track info update, not only when
the mute state changes. StreamTracker.SetPaused(false) reset the tracker
even when it was not paused, so any track info update of an unmuted
video track (video size, codec info, simulcast layer SSRCs,
UpdateLocalVideoTrack) marked every live layer stopped, removed it from
the available layers, and zeroed its bitrate until the next packet and
bitrate report.

SetPaused now returns early when the paused state does not change, as
StreamTrackerDependencyDescriptor.SetPaused already does. A new tracker
starts unpaused in its reset state, so the SetPaused call in AddTracker
behaves the same.
2026-11-12 01:01:45 +05:30
Raja SubramanianandClaude Opus 5.5 031e7bd61d sfu: restart the dependency descriptor parser after a stream gap (#5192)
* sfu: restart the dependency descriptor parser of a publisher stream after a gap

libwebrtc numbers dependency descriptor frames with one counter across all
simulcast layers. A layer that dynacast paused for a long time resumes with a
frame number more than half the 16 bit range away from its last one. The
publisher buffer's parser never restarted, so it unwrapped the resumed frames
against the stale number and later lost a full wrap. Every packet of the layer
then fell below the structure thresholds and was dropped until the numbers
caught up, about 16 minutes for three 22 fps layers.

Enable the parser restart after a 30 s gap that relayed tracks already use, and
start the key frame seeder on a restart, as the restart drops the structure the
following packets need.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* sfu: request a key frame only when a restarted stream has no structure

The parser now reports the restart on the packet that caused it, so the
buffer does not read the parser's state. A stream that resumes with a key
frame carries its structure and needs no key frame request.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 00:39:21 +05:30
Raphael Fakhri 94edd5da75 sfu: find the RTX payload type when its fmtp line has more than apt (#5135)
FindRTXPayloadType compared the whole fmtp line with "apt=<pt>", so an
RTX codec negotiated as "apt=96;rtx-time=3000" (RFC 4588) was never
found. The down track then stored RTX payload type 0, sent
retransmissions on the primary SSRC and probed with padding even though
RTX was negotiated.

Compare only the apt parameter of the fmtp line.
2026-11-06 15:54:32 +05:30
cnderrauber e269e3e355 Update webrtc fork to dtls1.3 (#5142)
* Update webrtc fork to dtls1.3

* update dep

* Remove debug line

* Fix test
2026-11-05 18:20:18 +08:00
cnderrauber 4cbf2d3ebd Shrink NALU size when stripping h264/h265 aggregation packet trailer (#5125)
Fix #5082
2026-11-03 16:17:33 +08:00
Raja Subramanian 13b23fa722 Use Start on ReceiverBase to ensure struct is fully set up before (#5121)
callbacks.
2026-11-02 20:08:34 +05:30
armaan4658andClaude Opus 5.5 b7af480c2d rtc: reject AddTrack with a repeated video layer quality (#5115)
ValidVideoLayers checks that each layer's quality is in range but lets a
quality repeat. GetQualityForDimension uses the index into the sorted
layer sizes as the quality, so a list such as LOW, LOW, MEDIUM, HIGH maps
the largest size to index 3, which is OFF, and a subscriber asking for
that size receives no video.

Reject an AddTrack whose layer list repeats a quality. Each list (the
request's and each simulcast codec's) is checked on its own, so the same
qualities on the request and a codec are still accepted. With only three
qualities this also bounds a list to three layers.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 14:03:42 +05:30
Raja SubramanianandClaude Opus 5.5 8eb162f104 rtc: do not advertise a subscribed track the subscriber cannot decode (#5117)
* rtc: do not advertise a subscribed track the subscriber cannot decode

In single peer connection mode a subscriber offers a recvonly section with
its own codecs. If the published track uses a codec the subscriber did not
offer, DownTrack.Bind finds no match but returns nil, because returning an
error aborts the whole transport. pion then keeps the sender, so the answer
still carries the track msid and ssrc. The subscriber sees a short-lived
TrackSubscribed before the bind failure removes the track again.

Park such a sender before building the answer: drop its track so the
section answers inactive, then report the codec failure out of band so the
subscription manager unsubscribes without reentering negotiation.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* rtc: report codec mismatch via Handler, not the downtrack

Route the parked sender's failure through a new Handler method into the
participant, instead of reaching from transport into the downtrack. The
participant unsubscribes and reports the codec error. Keeps transport
free of the sfu package.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* rtc: use CodecsFromMediaDescription for the offered-codec check

Reuse the protocol sdp helper instead of hand-parsing rtpmap lines.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* rtc: reword codec-suppression comments

Describe it as a subscription, drop the stale prototype label.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* rtc: check every bindable codec before suppressing a sender

The check before the answer looked only at the primary codec. A subscriber
that offers a later upstream codec, for example Opus for a RED-first track,
lost a track that Bind can bind.

DownTrack.BindableCodecs returns the codecs Bind can pick, and both Bind and
the check use it. It also fixes the encrypted RED/Opus rule in Bind: the old
condition was never true, so an encrypted RED track could bind as Opus.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* rtc: keep published encrypted codecs bindable, record codec rejections

The encrypted RED/Opus rule cut every RED/Opus pair to the primary codec.
That also dropped a second codec the publisher sends on its own stream.
WrappedReceiver now decides which codecs bind: for an encrypted source,
only its published codecs, never the RED or Opus alternative it adds.

A sender parked before the answer now ends its subscription through the
same path as a bind error, so it records OnTrackSubscribeFailed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-02 12:32:28 +05:30
Raja SubramanianandClaude Opus 5.5 9a26932591 Harden media and room handling against bad inputs and races (#5114)
* sfu: ignore a nil publisher sender report in the forwarder

A stream restart replaces the receiver stats between the sender report
callback and its read, so the report can be nil. SetRefSenderReport now
keeps the previous report instead of reading the nil one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* dynacast: skip a codec regression when the target codec has no quality

HandleCodecRegression looked up the target codec after it released the
lock. If the manager closed in between, or the target mime type was
unknown, the lookup returned nil and RegressTo called Restart or Replace on
it. The target is now looked up with the source, and a missing one stops
the regression.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* service: create the internal record for a stored room that has none

RedisStore.LoadRoom returns a nil RoomInternal without an error when the
room exists but its internal field does not, for example after a delete
races a store. CreateRoom then wrote egress, agent, playout delay or sync
settings into the nil record. It now starts from an empty record, which
the store call writes back.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* sfu: drop a dependency descriptor whose chains do not match the structure

The parser takes a new structure from any packet, but DDForwarder keeps
the structure of the last packet it forwarded. When the publisher attached
a structure with fewer chains to a packet this downtrack dropped, the next
descriptor had fewer chain diffs than the forwarder's structure, and the
writer indexed past them. The forwarder now drops such a packet, and the
writer returns an error instead of indexing past the chain diffs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* deps: bump mediatransportutil for the empty VP9 payload fix

A VP9 packet without a dependency descriptor and with a 1-byte payload such
as 0x08 parses to an empty VP9 payload, and IsVP9KeyFrame read its first
byte. mediatransportutil#106 returns false for an empty payload. This pins
the transport-v4 commit that carries it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* sfu: keep the template test comment with its test

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* dynacast: create the target quality only for a valid regression

A target quality starts a timer that notifies the publisher. Look it up
only after the source codec is found, so a rejected regression creates
nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* service: restore the internal defaults for a room that lost them

A stored room without an internal record now gets the configured playout
delay and sync streams defaults. Its own settings, such as the empty
timeout and codecs, stay as stored.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 19:29:11 +05:30
Raja SubramanianandClaude Opus 4.8 996b5bfb9a rtc: reject AddTrack with an out-of-range video layer mode (#5106)
A client sets SimulcastCodec.VideoLayerMode on AddTrack. Any value other
than MODE_UNUSED is used as-is, so a forged out-of-range value flows into
the forwarder. Reject it in ValidVideoLayers, alongside the quality check.

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-30 20:37:35 +05:30
Raja Subramanian ebc71a6d11 rtc: reject AddTrack with an out-of-range video layer quality (#5102) 2026-10-30 02:16:04 +05:30
GrantandClaude Opus 5.5 d5f5bcb60c sfu: resync dependency descriptor frame numbers on unmute (#5094)
On mute the forwarder resyncs sequence numbers, timestamps and VP8 picture
IDs, but DDForwarder kept its frame number offset. libwebrtc counts DD
frames across all simulcast layers, so after a long mute the frame number
sent on unmute jumped by more than half the 16 bit range. The subscriber
took every frame as older than the last decoded one and dropped it, a
black tile until the numbers wrapped past it again.

Resync DDForwarder with the forwarder, and move the numbers to just after
the last one sent when they jump too far ahead.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-29 10:52:36 +05:30
Raja SubramanianandClaude Opus 5.5 4a2615f127 sfu: throttle PLIs with an atomic before taking the rtpstats lock (#5093)
* Throttle PLIs with an atomic before taking the rtpstats lock

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Reset the PLI throttle when the stream restarts

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-29 00:34:28 +05:30
Raja SubramanianandClaude Opus 5.5 e0e34aba8f Take the read lock in GetOrCreateBuffer when the buffer exists (#5092)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-29 00:29:03 +05:30
Raja SubramanianandClaude Opus 5.5 2f7337b759 rtc: ignore server-owned video layer fields in AddTrack (#5087)
The server sets the spatial layer, rid, ssrc and repair ssrc of each
video layer from SDP and RTP. Clear these fields on AddTrack request
layers, including each simulcast codec's layers.

Clamp the layer in ReceiverBase.SetMaxExpectedSpatialLayer to
[InvalidLayerSpatial, DefaultMaxLayerSpatial].

Keep a client set video layer mode, but warn when it differs from the
server determined mode.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 16:23:52 +05:30
Raja SubramanianandClaude Opus 5.5 d2cf39bce6 sfu: replace forward scheduling delay with processing per write (#5072)
* sfu: replace forward scheduling delay with processing per subscriber

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* sfu: describe processing per subscriber as per write

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* telemetry: name forward processing metrics per write

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Pin protocol with NodeStats forward_processing_per_write

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 01:52:17 +05:30
Raja SubramanianandClaude Opus 5.5 125ea44a61 sfu: report forward scheduling delay next to forwarding latency (#5065)
* sfu: report forward scheduling delay next to forwarding latency

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* sfu: keep full forwarding transit when packing scheduling delay

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* sfu: saturate reported forwarding p90s instead of wrapping

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 15:24:31 +05:30
Raja Subramanian 6294ab37c7 prometheus: count keyframes received and forwarded (#5060) 2026-10-25 13:08:05 +05:30
Raja SubramanianandClaude Opus 5.5 7c575a5c31 sfu: cap retransmissions per down track (#5054)
* sfu: cap retransmissions per down track (rtc.rtx config, off by default)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* sfu: rtx budget review fixes (sequencer undo, sent bytes, newest first, rtx_budget)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* sfu: rtx budget second review (order unchanged when off, keep NACK window, metric in down track)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* sfu: rtx budget sorts NACKs newest first by extended sequence number

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 11:05:54 +05:30
Raja Subramanian a8729ee65b sfu: read lock for BufferBase.GetPacket (#5055) 2026-10-24 23:35:23 +05:30
6342d8fd1b Enable upstream FlexFEC03 (#4845)
* Add upstream FlexFEC recovery support

* Guard short TWCC extensions in FEC packets

* Release completed FlexFEC states

* Validate FlexFEC payload type range

* Invoke FEC callbacks after unlocking

* Reduce FlexFEC protection state allocations

* Avoid allocations for complete FlexFEC packets

* Optimize ordered FlexFEC packet insertion

* Reuse RTP packet storage for FlexFEC recovery

* Limit pending FlexFEC repair packets

* Lower FlexFEC repair window to two seconds

* Harden upstream FlexFEC handling

* Test FlexFEC with E2EE media

* Fix FlexFEC negotiation with libwebrtc publishers

* Enhance payload length validation in decoder

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* remove the ability to configure payload type for FlexFEC

* moved some helpers into protocol

* Handle big gaps in media sequence numbers

Reset buffers when a big gap in media sequence numbers is detected.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* bump protocol & psrpc deps

* Handle FEC packet recovery failure

Increment FEC packets discarded counter and remove packet.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* do not NACK on fec recovered packet (#4896)

* do not NACK on fec recovered packet

* fix test race in CI

* Fix FEC write return after merging RTX feedback changes

* PR fixes for FlexFEC implementation (#4898)

* do not NACK on fec recovered packet

* refactor common code into unlockAndInvokeFECRecovery

* add FEC stats to RTPStats

* add log for when fecdecoder unavailable after creation

* rename markAsFECRepair to setFECRepairBufferLimit for clarity

* refactor packet generation for tests into util

* fix test race in CI

* move the unlocks back inline for readability

* update RTPstats

* Fix FEC write return after merging RTX feedback changes

* Use shared RTP packet helper in merged FEC NACK test

* Remove redundant return from FEC write handler

* packet post process (#5020)

* packet post process

* remove redundant writeRTX

* send out RTCP (NACK and RR) from packetPostProcess only

* handle FEC decoder on receiver restart (#5032)

* handle FEC decoder on receiver restart

* move onStreamRestartLocked into BufferBaseParams

---------

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Raja Subramanian <raja.gobi@tutanota.com>
2026-10-22 13:58:04 -07:00
Raja SubramanianandClaude Opus 4.8 a8afbd69bd Fix data race on global logger in sfu downtrack integration test (#5036)
TestDownTrackResumesAfterBandwidthPause called logger.InitFromConfig
(SetLogger) mid-run while the hwstats CPU monitor goroutine, started by
prometheus.Init in forwardstats_test, read the same non-atomic logger
package globals. go test -race flagged the write-vs-read and failed CI.

Set the global logger once in TestMain, before any test starts that
goroutine, and drop the per-test InitFromConfig.

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-21 12:14:51 +05:30
Raja SubramanianandClaude Fable 5.1 cde3991916 sfu: reset dummy start offset on forwarder restart (#5019)
* sfu: reset dummy start offset on forwarder restart

Restart() kept dummyStartTSOffset. The next source switch recomputed it
from a refTS that already included the old offset, so every switch after
that was off by the old value and the subscriber's timestamp jumped.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* sfu: log extTimestamp in sequencer packet meta, add Chrome case to AV1 config test

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* sfu: do not apply dummy start offset without a reference layer

With skipReferenceTS, refTS is the last outgoing timestamp, which is
already on the downtrack's timeline. Computing and adding an offset there
re-added the gap between the last packet and a restart on every later
layer switch.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-16 19:04:20 +05:30
Miguel Delgado 103d7bec84 sfu: apply dummy start offset modulo 2^32 on source switch (#5018)
processSourceSwitch added dummyStartTSOffset to extRefTS in 64 bits. When
the offset is negative and the reference layer's 32-bit RTP timestamp has
wrapped but is still below |offset|, the addition borrows from the upper
half and extRefTS ends up 2^32 below the right value. The wrap correction
that follows only compares the lower 32 bits, so it cannot undo it.

The switch point then looks ~47,722 s (2^32 / 90 kHz) behind and is
rejected with errSwitchPointTooFarBehind on every keyframe, until the
reference layer's timestamp climbs past |offset|, which can take hours.

Apply the offset modulo 2^32 before restoring the cycles from extLastTS.
The lower 32 bits, and so the timestamp sent to subscribers, are unchanged.
2026-10-16 18:24:18 +05:30
Anunay Maheshwari 0b58164597 fix(sfu): stop allocating per packet for abs-capture-time and retransmits (#5003)
* sfu: stop allocating per packet for abs-capture-time and retransmits

* sfu: drop unused abs-capture-time marshal errors

* sfu: size held extensions locally, guard nil holds, keep sequencer clones
2026-10-15 05:20:04 +05:30
David Colburn 708b8ea6ba sfu: use mediatransportutil/pkg/red in RedPrimaryReceiver (#5014)
RED depacketizing now lives in mediatransportutil so the SFU and
replay export share one implementation. Drop the local copies; the
encoder round-trip tests now exercise the shared package.
2026-10-14 16:57:01 -04:00
Raja SubramanianandClaude Opus 5.5 18df6ad4b6 sfu: probe with padding when recent packets are gone (#5009) (#5013)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 23:11:33 +05:30
Raja SubramanianandClaude Opus 5.5 e357fdffb7 rtpstats: take mono nanoseconds in GetExpectedRTPTimestamp (#5008)
Callers passed mono.Now() as a time.Time, which made it easy to pass
time.Now() by mistake and mix clocks. Take mono nanoseconds instead.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 17:44:12 +05:30
Raja SubramanianandClaude Opus 5.5 43291596c4 rtpstats: return sender report time in wall clock domain (#5006)
LastSenderReportTime() returned time.Unix(0, srNewest.At). But At is a
mono timestamp, so the result is not a real wall time. Connection quality
compares it to time.Now() to decide if RTCP is stale. If the wall clock
drifts from mono, for example after an NTP step, streams can look stale
when they are not, or the reverse.

- Convert At to wall time: now minus the mono time since the report.
- Return zero time when At is 0, so the scorer skips the stale check
  (before, it got the 1970 epoch and saw the stream as stale).

Follow-up to #5005, which fixed the same mix-up in DLSR.

Tests: rtpstats and connectionquality pass. No new test, because a
test process cannot make the mono and wall clocks drift.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 17:00:59 +05:30
Jibon L. Costa d8aaa45dd1 fix: compute RTCP receiver-report DLSR from monotonic clock (#5005) 2026-10-14 15:48:36 +05:30
Raja SubramanianandClaude Opus 5.5 d0fd37eaa0 Send NACKs when processing RTX packets. (#5000)
* Send NACKs when processing RTX packets.

Applying the fix proposed in https://github.com/livekit/livekit/issues/4999

* Add test for NACKs sent while processing RTX packets.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 11:07:35 +05:30
Raja Subramanian 5dac0aab20 sfu: send padding-only packets with no payload (#4993) 2026-10-12 20:22:15 +05:30
Raja SubramanianandClaude Opus 5.5 93296ad4f6 sfu: filter VP9 and AV1 simulcast temporal layers with the dependency descriptor (#4991)
* sfu: filter VP9 and AV1 simulcast temporal layers with the dependency descriptor

Every simulcast codec gets an explicit temporal layer selector. With the
dependency descriptor, VP8, VP9 and AV1 filter temporal layers by frame and
forward the descriptor. Without it, a pass-through selector forwards every
temporal layer and reports the target as reached, so a pending target cannot
block allocation. VP8DD becomes the codec-neutral DDTemporal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* sfu: VP8-only padding header in DDTemporal, structure gate on presence

DDTemporal returns the one byte VP8 payload descriptor for padding only for
VP8. The descriptor forwarder takes a structure from any packet that carries
one, as the parser accepts it only on the first packet of a frame.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 16:53:09 +05:30
Raja SubramanianandClaude Opus 5.5 d621849e3a sfu: keep VP8 descriptor frame numbers across migration (#4989)
* sfu: keep VP8 descriptor frame numbers across migration

The forwarder state carries the last dependency descriptor frame number
sent, so a migrated subscriber's first key frame continues the numbers
instead of being dropped as older than its last structure. Bumps protocol
for RTPForwarderState.dependency_descriptor_forwarder.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* sfu: pass on a pending descriptor seed when migrating again

A forwarder seeded before its codec was known returns the pending seed in
its state, so a second migration keeps the frame numbers continuous.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 12:52:19 +05:30
Raja SubramanianandClaude Opus 5.5 099e03fdf8 sfu: forward the dependency descriptor for VP8 simulcast (#4987)
* sfu: forward the dependency descriptor for VP8 simulcast

libwebrtc sends VP8 with a one byte payload descriptor when it sends the
dependency descriptor, so a VP8 subscriber gets neither picture IDs nor the
descriptor and finds frame references by sequence number alone. Under loss
with temporal filtering it stalls until a key frame.

DDForwarder rewrites the publisher's descriptor for each forwarded packet:
frame numbers continue across simulcast layer switches and stream restarts,
the structure goes out with the current layer's key frame, and the active
decode targets follow the forwarded temporal layer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* sfu: keep VP8 descriptor frame numbers from going back

Rebase frame numbers only when a new stream would go back past the last
number sent: on a layer switch, a stream restart (also when it starts with a
delta frame) and a new forwarder on the same downtrack, which now carries
over the last number sent.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* sfu: set VP8 descriptor decode targets from the packet's layer

The active decode targets use the temporal layer the packet is forwarded at,
not the selector's next layer on the last packet before a down switch. They
also go out on key frames, combined with the publisher's own mask.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* sfu: carry the current active decode targets on every DD packet

The parser puts the stream's current mask on every packet, so a mask change
on a dropped packet still reaches the VP8 descriptor forwarder, which no
longer tracks the publisher's mask itself.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* sfu: take the first DD active decode targets mask at any sequence number

A publisher whose first packet has RTP sequence number 0 never had its key
frame's mask applied, so every packet reported no active decode targets.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 12:08:49 +05:30
Raja SubramanianandClaude Opus 5.5 b1c801b979 sfu: pad the slot of a filtered packet that follows a sequence number gap (#4983)
A filtered packet that follows a sequence number gap now keeps its slot
and gets padding in it. Rolling it back moved the last outgoing number
below the missing packets' slots, so their retransmissions were rejected
and the subscriber stalled on the hole until a key frame.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 23:03:42 +05:30
Raja SubramanianandClaude Opus 5.5 9ddcc50767 sfu: parse VP8 and VP9 payload descriptors without an allocation (#4981)
* sfu: parse VP8 and VP9 payload descriptors without an allocation

Replace ExtPacket.Payload with typed VP8 and VP9 pointer fields that point
at backing structs inside the ExtPacket, so parsing does not allocate and a
wrong codec type is a compile error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* sfu: copy the VP8 descriptor in the test packet helper

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 20:43:17 +05:30
Raja SubramanianandClaude Opus 5.5 c2de273198 DownTrack: arm key frame requester timer only while a layer lock is pending (#4980)
The key frame requester re-armed its timer on every loop, so every video down
track woke up every 200 ms to 1 s for its whole life, even when the layer was
locked and there was nothing to do.

Re-arm the timer only while the requested layer is not locked, or while the
lock can change without an event: an acquisition grace is armed, or layers are
selected with the dependency descriptor, which can lose the lock on the packet
path. Post the event on resync, mute, max published layer change and packet
path layer switch, since those can change the lock without an allocation.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 11:25:37 +05:30
Raja SubramanianandClaude Opus 5.5 a67eb7b560 sfu: send padding in the slot of a dropped out-of-order packet (#4979)
On a gap the RTP munger gives the missing sequence numbers outgoing slots.
If a late packet then arrives and is dropped, nothing is ever sent in its
slot, and a subscriber that finds frame references by sequence number
stalls until a key frame. The forwarder now flags such drops, and the
downtrack sends a one byte padding only packet in the slot.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 11:23:29 +05:30
Raja SubramanianandClaude Opus 5.5 152328c76a sfu: filter VP8 temporal layers by frame when the dependency descriptor is sent (#4976)
* sfu: filter VP8 temporal layers by frame when the dependency descriptor is sent

With the dependency descriptor, libwebrtc sends a one byte VP8 payload
descriptor without a picture ID, so the VP8 munger's picture ID based
temporal drop logic stops filtering after the first gap. A new VP8DD codec
munger decides once per frame from the descriptor's frame number and
temporal ID and applies that decision to every packet of the frame.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* sfu: use the VP8 munger when picture IDs come with the dependency descriptor

VP8DD needs the one byte VP8 payload descriptor that libwebrtc sends with
the dependency descriptor. If a publisher keeps picture IDs next to the
descriptor, they need rewriting, so the forwarder switches to the VP8
munger and keeps switching temporal layers with the descriptor.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 11:16:34 +05:30
Raja SubramanianandClaude Opus 5.5 b90b0ca1df sfu: switch VP8 temporal layers up with the dependency descriptor (#4974)
libwebrtc strips the VP8 payload descriptor when it sends the dependency
descriptor, so the layer sync bit that #4947 waits for is never set and a
subscriber that lowers its frame rate stays at the base layer until a key
frame. A new temporal layer selector reads only the dependency descriptor:
it switches up at a switch point whose referenced frames were all
forwarded. The forwarder uses it for VP8 when the extension is negotiated
and falls back to the Y-bit selector when the descriptor is not sent.

Fixes #4973.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 00:47:42 +05:30
Raja SubramanianandClaude Fable 5.1 bc849eb47d sfu/buffer: parse the dependency descriptor without allocating (#4953)
* sfu/buffer: parse the dependency descriptor without allocating

Every VP9 and AV1 SVC packet allocated six objects in the dependency
descriptor parse: the descriptor, a clone of the frame template with
its three slices, a resolution copy, and a frame entity that took the
address of a parameter.

The pooled ExtDependencyDescriptor now owns the storage for the
descriptor, the template and the resolution, with fixed arrays for the
template's slices. The reader copies the matching template into that
storage.

Delta frame parse: 6 allocs, 176 B, 174 ns -> 0 allocs, 0 B, 122 ns.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* sfu/dd: never copy a parsed frame into a structure template

A caller can hand Unmarshal a descriptor whose FrameDependencies points
at one of the structure's templates. Copying the packet's custom fields
into it would change the template for every later packet, so the reader
allocates a fresh template in that case.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-06 19:46:20 +05:30