mirror of
https://github.com/livekit/livekit.git
synced 2026-08-02 00:29:27 +00:00
Do not error out on invalid packet. (#2789)
Remove the return when encountering invalid packet. Also, log more sparesely. Proper error returns from util so that we can selectively drop packets based on error type, for example SSRC mismatches are okay type of thing.
This commit is contained in:
+20
-18
@@ -132,6 +132,7 @@ type Buffer struct {
|
||||
packetNotFoundCount atomic.Uint32
|
||||
packetTooOldCount atomic.Uint32
|
||||
extPacketTooMuchCount atomic.Uint32
|
||||
invalidPacketCount atomic.Uint32
|
||||
|
||||
primaryBufferForRTX *Buffer
|
||||
rtxPktBuf []byte
|
||||
@@ -317,24 +318,25 @@ func (b *Buffer) Write(pkt []byte) (n int, err error) {
|
||||
}
|
||||
|
||||
if err = utils.ValidateRTPPacket(&rtpPacket, b.payloadType, b.mediaSSRC); err != nil {
|
||||
b.logger.Warnw(
|
||||
"validating RTP packet failed", err,
|
||||
"version", rtpPacket.Version,
|
||||
"padding", rtpPacket.Padding,
|
||||
"marker", rtpPacket.Marker,
|
||||
"expectedPayloadType", b.payloadType,
|
||||
"payloadType", rtpPacket.PayloadType,
|
||||
"sequenceNumber", rtpPacket.SequenceNumber,
|
||||
"timestamp", rtpPacket.Timestamp,
|
||||
"expectedSSRC", b.mediaSSRC,
|
||||
"ssrc", rtpPacket.SSRC,
|
||||
"numExtensions", len(rtpPacket.Extensions),
|
||||
"payloadSize", len(rtpPacket.Payload),
|
||||
"rtpStats", b.rtpStats,
|
||||
"snRangeMap", b.snRangeMap,
|
||||
)
|
||||
b.Unlock()
|
||||
return
|
||||
invalidPacketCount := b.invalidPacketCount.Inc()
|
||||
if (invalidPacketCount-1)%100 == 0 {
|
||||
b.logger.Warnw(
|
||||
"validating RTP packet failed", err,
|
||||
"version", rtpPacket.Version,
|
||||
"padding", rtpPacket.Padding,
|
||||
"marker", rtpPacket.Marker,
|
||||
"expectedPayloadType", b.payloadType,
|
||||
"payloadType", rtpPacket.PayloadType,
|
||||
"sequenceNumber", rtpPacket.SequenceNumber,
|
||||
"timestamp", rtpPacket.Timestamp,
|
||||
"expectedSSRC", b.mediaSSRC,
|
||||
"ssrc", rtpPacket.SSRC,
|
||||
"numExtensions", len(rtpPacket.Extensions),
|
||||
"payloadSize", len(rtpPacket.Payload),
|
||||
"rtpStats", b.rtpStats,
|
||||
"snRangeMap", b.snRangeMap,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
|
||||
@@ -16,6 +16,7 @@ package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/pion/interceptor"
|
||||
@@ -54,18 +55,24 @@ func GetHeaderExtensionID(extensions []interceptor.RTPHeaderExtension, extension
|
||||
return 0
|
||||
}
|
||||
|
||||
var (
|
||||
ErrInvalidRTPVersion = errors.New("invalid RTP version")
|
||||
ErrRTPPayloadTypeMismatch = errors.New("RTP payload type mismatch")
|
||||
ErrRTPSSRCMismatch = errors.New("RTP SSRC mismatch")
|
||||
)
|
||||
|
||||
// ValidateRTPPacket checks for a valid RTP packet and returns an error if fields are incorrect
|
||||
func ValidateRTPPacket(pkt *rtp.Packet, expectedPayloadType uint8, expectedSSRC uint32) error {
|
||||
if pkt.Version != 2 {
|
||||
return errors.New("invalid RTP version")
|
||||
return fmt.Errorf("%w, expected: 2, actual: %d", ErrInvalidRTPVersion, pkt.Version)
|
||||
}
|
||||
|
||||
if expectedPayloadType != 0 && pkt.PayloadType != expectedPayloadType {
|
||||
return errors.New("invalid RTP payload type")
|
||||
return fmt.Errorf("%w, expected: %d, actual: %d", ErrRTPPayloadTypeMismatch, expectedPayloadType, pkt.PayloadType)
|
||||
}
|
||||
|
||||
if expectedSSRC != 0 && pkt.SSRC != expectedSSRC {
|
||||
return errors.New("invalid RTP SSRC")
|
||||
return fmt.Errorf("%w, expected: %d, actual: %d", ErrRTPSSRCMismatch, expectedSSRC, pkt.SSRC)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user