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:
Raja Subramanian
2024-06-14 11:10:57 +05:30
committed by GitHub
parent 6e4b0c20d1
commit ea60368100
2 changed files with 30 additions and 21 deletions
+20 -18
View File
@@ -132,6 +132,7 @@ type Buffer struct {
packetNotFoundCount atomic.Uint32 packetNotFoundCount atomic.Uint32
packetTooOldCount atomic.Uint32 packetTooOldCount atomic.Uint32
extPacketTooMuchCount atomic.Uint32 extPacketTooMuchCount atomic.Uint32
invalidPacketCount atomic.Uint32
primaryBufferForRTX *Buffer primaryBufferForRTX *Buffer
rtxPktBuf []byte 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 { if err = utils.ValidateRTPPacket(&rtpPacket, b.payloadType, b.mediaSSRC); err != nil {
b.logger.Warnw( invalidPacketCount := b.invalidPacketCount.Inc()
"validating RTP packet failed", err, if (invalidPacketCount-1)%100 == 0 {
"version", rtpPacket.Version, b.logger.Warnw(
"padding", rtpPacket.Padding, "validating RTP packet failed", err,
"marker", rtpPacket.Marker, "version", rtpPacket.Version,
"expectedPayloadType", b.payloadType, "padding", rtpPacket.Padding,
"payloadType", rtpPacket.PayloadType, "marker", rtpPacket.Marker,
"sequenceNumber", rtpPacket.SequenceNumber, "expectedPayloadType", b.payloadType,
"timestamp", rtpPacket.Timestamp, "payloadType", rtpPacket.PayloadType,
"expectedSSRC", b.mediaSSRC, "sequenceNumber", rtpPacket.SequenceNumber,
"ssrc", rtpPacket.SSRC, "timestamp", rtpPacket.Timestamp,
"numExtensions", len(rtpPacket.Extensions), "expectedSSRC", b.mediaSSRC,
"payloadSize", len(rtpPacket.Payload), "ssrc", rtpPacket.SSRC,
"rtpStats", b.rtpStats, "numExtensions", len(rtpPacket.Extensions),
"snRangeMap", b.snRangeMap, "payloadSize", len(rtpPacket.Payload),
) "rtpStats", b.rtpStats,
b.Unlock() "snRangeMap", b.snRangeMap,
return )
}
} }
now := time.Now() now := time.Now()
+10 -3
View File
@@ -16,6 +16,7 @@ package utils
import ( import (
"errors" "errors"
"fmt"
"strings" "strings"
"github.com/pion/interceptor" "github.com/pion/interceptor"
@@ -54,18 +55,24 @@ func GetHeaderExtensionID(extensions []interceptor.RTPHeaderExtension, extension
return 0 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 // 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 { func ValidateRTPPacket(pkt *rtp.Packet, expectedPayloadType uint8, expectedSSRC uint32) error {
if pkt.Version != 2 { 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 { 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 { 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 return nil