2024-06-05 20:10:03 +00:00
|
|
|
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2022-03-10 09:44:48 +00:00
|
|
|
package rtp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Sequencer generates sequential sequence numbers for building RTP packets
|
|
|
|
type Sequencer interface {
|
|
|
|
NextSequenceNumber() uint16
|
|
|
|
RollOverCount() uint64
|
|
|
|
}
|
|
|
|
|
2024-09-29 19:40:13 +00:00
|
|
|
// maxInitialRandomSequenceNumber is the maximum value used for the initial sequence
|
|
|
|
// number when using NewRandomSequencer().
|
|
|
|
// This uses only half the potential sequence number space to avoid issues decrypting
|
|
|
|
// SRTP when the sequence number starts near the rollover and there is packet loss.
|
|
|
|
// See https://webrtc-review.googlesource.com/c/src/+/358360
|
|
|
|
const maxInitialRandomSequenceNumber = 1<<15 - 1
|
|
|
|
|
2022-03-10 09:44:48 +00:00
|
|
|
// NewRandomSequencer returns a new sequencer starting from a random sequence
|
|
|
|
// number
|
|
|
|
func NewRandomSequencer() Sequencer {
|
|
|
|
return &sequencer{
|
2024-09-29 19:40:13 +00:00
|
|
|
sequenceNumber: uint16(globalMathRandomGenerator.Intn(maxInitialRandomSequenceNumber)),
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFixedSequencer returns a new sequencer starting from a specific
|
|
|
|
// sequence number
|
|
|
|
func NewFixedSequencer(s uint16) Sequencer {
|
|
|
|
return &sequencer{
|
|
|
|
sequenceNumber: s - 1, // -1 because the first sequence number prepends 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type sequencer struct {
|
|
|
|
sequenceNumber uint16
|
|
|
|
rollOverCount uint64
|
|
|
|
mutex sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// NextSequenceNumber increment and returns a new sequence number for
|
|
|
|
// building RTP packets
|
|
|
|
func (s *sequencer) NextSequenceNumber() uint16 {
|
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
|
|
|
|
|
|
|
s.sequenceNumber++
|
|
|
|
if s.sequenceNumber == 0 {
|
|
|
|
s.rollOverCount++
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.sequenceNumber
|
|
|
|
}
|
|
|
|
|
|
|
|
// RollOverCount returns the amount of times the 16bit sequence number
|
|
|
|
// has wrapped
|
|
|
|
func (s *sequencer) RollOverCount() uint64 {
|
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
|
|
|
|
|
|
|
return s.rollOverCount
|
|
|
|
}
|