Files
gortsplib/pkg/rtplossdetector/lossdetector.go

37 lines
786 B
Go

// Package rtplossdetector implements an algorithm that detects lost packets.
package rtplossdetector
import (
"github.com/pion/rtp"
)
// LossDetector detects lost packets.
type LossDetector struct {
initialized bool
expectedSeqNum uint16
}
// New allocates a LossDetector.
func New() *LossDetector {
return &LossDetector{}
}
// Process processes a RTP packet.
// It returns the number of lost packets.
func (r *LossDetector) Process(pkt *rtp.Packet) int {
if !r.initialized {
r.initialized = true
r.expectedSeqNum = pkt.SequenceNumber + 1
return 0
}
if pkt.SequenceNumber != r.expectedSeqNum {
diff := pkt.SequenceNumber - r.expectedSeqNum
r.expectedSeqNum = pkt.SequenceNumber + 1
return int(diff)
}
r.expectedSeqNum = pkt.SequenceNumber + 1
return 0
}