Validate RTP packets. (#2778)

* Validate RTP packets.

Check version, payload type (if available) and SSRC (if available)
and drop bad packets. And let repair mechanisms take effect for those
packets.

* address data race reported by test

* fix an unlock and test packets
This commit is contained in:
Raja Subramanian
2024-06-10 15:43:59 +05:30
committed by GitHub
parent a31f59b689
commit 129ba62d61
5 changed files with 81 additions and 15 deletions
+19
View File
@@ -15,9 +15,11 @@
package utils
import (
"errors"
"strings"
"github.com/pion/interceptor"
"github.com/pion/rtp"
"github.com/pion/webrtc/v3"
)
@@ -51,3 +53,20 @@ func GetHeaderExtensionID(extensions []interceptor.RTPHeaderExtension, extension
}
return 0
}
// 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")
}
if expectedPayloadType != 0 && pkt.PayloadType != expectedPayloadType {
return errors.New("invalid RTP payload type")
}
if expectedSSRC != 0 && pkt.SSRC != expectedSSRC {
return errors.New("invalid RTP SSRC")
}
return nil
}