From 2d3d6ab604053c3b797fc14e750ff70985b08491 Mon Sep 17 00:00:00 2001 From: boks1971 Date: Mon, 16 Oct 2023 14:51:17 +0530 Subject: [PATCH] Fix wrap around on out of order packet Sequence number cycle calculation was over estimating the cycles if packets came out of order. A sequence like 65535, 0, 65534 would increment the cycle counter twice. --- pkg/report/receiver_interceptor_test.go | 4 ++++ pkg/report/receiver_stream.go | 8 ++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/pkg/report/receiver_interceptor_test.go b/pkg/report/receiver_interceptor_test.go index dc2d2049..b0fe534b 100644 --- a/pkg/report/receiver_interceptor_test.go +++ b/pkg/report/receiver_interceptor_test.go @@ -178,6 +178,10 @@ func TestReceiverInterceptor(t *testing.T) { SequenceNumber: 0x00, }}) + stream.ReceiveRTP(&rtp.Packet{Header: rtp.Header{ + SequenceNumber: 0xfffe, + }}) + pkts := <-stream.WrittenRTCP() assert.Equal(t, len(pkts), 1) rr, ok := pkts[0].(*rtcp.ReceiverReport) diff --git a/pkg/report/receiver_stream.go b/pkg/report/receiver_stream.go index 3f239c2e..ebe08473 100644 --- a/pkg/report/receiver_stream.go +++ b/pkg/report/receiver_stream.go @@ -64,10 +64,10 @@ func (stream *receiverStream) processRTP(now time.Time, pktHeader *rtp.Header) { } else { // following frames stream.setReceived(pktHeader.SequenceNumber) - diff := int32(pktHeader.SequenceNumber) - int32(stream.lastSeqnum) - if diff > 0 || diff < -0x0FFF { - // overflow - if diff < -0x0FFF { + diff := pktHeader.SequenceNumber - stream.lastSeqnum + if diff > 0 && diff < (1<<15) { + // wrap around + if pktHeader.SequenceNumber < stream.lastSeqnum { stream.seqnumCycles++ }