From 7b9253046164d61788df6372382b307885b5a2e5 Mon Sep 17 00:00:00 2001 From: Raja Subramanian Date: Wed, 1 Apr 2026 11:40:01 +0530 Subject: [PATCH] Drop time inverted packets in RED -> Opus conversion. (#4418) A bunch of edges to note here RED packet does not have sequence number for redundant blocks. It only has timestamp offset compared to the primary payload. The receivers are supposed to use just timestamp to sequence the payload and decode. But, when converting from RED -> Opus, the packets extracted from RED packet should be assigned a sequence number before they can be forwarded. The simple rule is, if packet N contains X redundant payloads, they are assigned sequence number of N - X to N - 1. However there are cases like the following sequence (with 1 packet redundancy) - Seq num 10, timestamp 2000, forwarded - Seq num 11 is lost - Seq num 12 has a redundant payload. Seq num 12 has timestamp of 4000. Ideally would expect the redundant payload to have a timestamp offset of 1000, so the redundant payload can be mapped to sequence number 11 and timestamp 3000 (4000 - 1000). But, in the problematic case, it has an offset of 3000 resulting in sequence number 11 and timestamp of 1000 causing an inversion with packet at sequence number 10. Unclear if this a publisher issue, i. e. packing RED wrong or if this is some expected behaviour with DTX. i. e. the DTX packets are not included in redundant payload. For example, the sequence - Seq num 10 -> DTX - Seq num 11 -> DTX -> lost - Seq num 12 -> Regular packet and include sequence num 9 as that is the last regular packet. Anyhow, detect this condition and drop the time inverted packet. Note however this handles only inversion against the highest sent packet sequence number and timestamp. So, some old packet inverted with some other old packet getting forwarded will get through. That has been the case always though and detecting that would be expensive and complicated. At least for egress, will also look at adding a check for inversion so that it can catch it before sending it down the gstreamer pipeline. As the egress uses a jitter buffer with ordered sequence number emits, it will be simpler to detect timestamp going back when sequence number is moving forward (of course the mute/dtx challenege is there). --- pkg/sfu/redprimaryreceiver.go | 48 ++++++++++------------------------- 1 file changed, 13 insertions(+), 35 deletions(-) diff --git a/pkg/sfu/redprimaryreceiver.go b/pkg/sfu/redprimaryreceiver.go index a36312d0..4963a14b 100644 --- a/pkg/sfu/redprimaryreceiver.go +++ b/pkg/sfu/redprimaryreceiver.go @@ -43,11 +43,10 @@ type RedPrimaryReceiver struct { closed atomic.Bool redPT uint8 - firstPktReceived bool - lastSeq uint16 - lastExtSeq uint64 - lastTS uint32 - lastExtTS uint64 + firstPktReceived bool + lastSeq uint16 + highestForwardedExtSequenceNumber uint64 + highestForwardedExtTimestamp uint64 // bitset for upstream packet receive history [lastSeq-8, lastSeq-1], bit 1 represents packet received pktHistory byte @@ -93,32 +92,12 @@ func (r *RedPrimaryReceiver) ForwardRTP(pkt *buffer.ExtPacket, spatialLayer int3 pPkt.ExtTimestamp -= uint64(pkts[len(pkts)-1].Timestamp - pkts[i].Timestamp) } pPkt.Packet = sendPkt - if pPkt.ExtSequenceNumber > pkt.ExtSequenceNumber && pPkt.ExtTimestamp < pkt.ExtTimestamp { - r.logger.Warnw( - "timestamp inversion, dropping", nil, - "numPackets", len(pkts), - "primaryIncomingSN", pkt.Packet.Header.SequenceNumber, - "primaryIncomingTS", pkt.Packet.Header.Timestamp, - "primaryExtractedSN", pkts[len(pkts)-1].SequenceNumber, - "primaryExtractedTS", pkts[len(pkts)-1].Timestamp, - "primaryESN", pkt.ExtSequenceNumber, - "primaryETS", pkt.ExtTimestamp, - "packetIndex", i, - "packetExtractedSN", pkts[i].SequenceNumber, - "packetESN", pPkt.ExtSequenceNumber, - "packetExtractedTS", pkts[i].Timestamp, - "packetETS", pPkt.ExtTimestamp, - "pktHistory", r.pktHistory, - "redHeader", pkt.Packet.Payload[:10], - "payloadSize", len(pkt.Packet.Payload), - ) - continue // drop the packet which causes the inversion - } - if r.lastTS != 0 { - if pPkt.ExtSequenceNumber > r.lastExtSeq && pPkt.ExtTimestamp < r.lastExtTS { + if r.highestForwardedExtTimestamp != 0 { + if (pPkt.ExtSequenceNumber > r.highestForwardedExtSequenceNumber && pPkt.ExtTimestamp < r.highestForwardedExtTimestamp) || + (pPkt.ExtSequenceNumber < r.highestForwardedExtSequenceNumber && pPkt.ExtTimestamp > r.highestForwardedExtTimestamp) { r.logger.Warnw( - "timestamp inversion", nil, + "sequence number OR timestamp inversion, dropping", nil, "numPackets", len(pkts), "primaryIncomingSN", pkt.Packet.Header.SequenceNumber, "primaryIncomingTS", pkt.Packet.Header.Timestamp, @@ -135,15 +114,14 @@ func (r *RedPrimaryReceiver) ForwardRTP(pkt *buffer.ExtPacket, spatialLayer int3 "redHeader", pkt.Packet.Payload[:10], "payloadSize", len(pkt.Packet.Payload), "lastSeq", r.lastSeq, - "lastExtSeq", r.lastExtSeq, - "lastTS", r.lastTS, - "lastExtTS", r.lastExtTS, + "highestFowardedExtSequenceNumber", r.highestForwardedExtSequenceNumber, + "highestFowardedExtTimestamp", r.highestForwardedExtTimestamp, ) + continue // drop the packet which causes the inversion } } - r.lastTS = pPkt.Packet.Header.Timestamp - r.lastExtTS = pPkt.ExtTimestamp - r.lastExtSeq = pPkt.ExtSequenceNumber + r.highestForwardedExtSequenceNumber = max(r.highestForwardedExtSequenceNumber, pPkt.ExtSequenceNumber) + r.highestForwardedExtTimestamp = max(r.highestForwardedExtTimestamp, pPkt.ExtTimestamp) // not modify the ExtPacket.RawPacket here for performance since it is not used by the DownTrack, // otherwise it should be set to the correct value (marshal the primary rtp packet)