From 64517c635888abc07f6cabcae6ee0fde1b5125cc Mon Sep 17 00:00:00 2001 From: Daniel Abramov Date: Mon, 6 Mar 2023 13:52:25 +0100 Subject: [PATCH] rewriter: add vp8 keyframe detection function Relates to https://github.com/matrix-org/waterfall/issues/100 --- pkg/conference/subscription/rewriter/vp8.go | 29 +++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 pkg/conference/subscription/rewriter/vp8.go diff --git a/pkg/conference/subscription/rewriter/vp8.go b/pkg/conference/subscription/rewriter/vp8.go new file mode 100644 index 0000000..59fe427 --- /dev/null +++ b/pkg/conference/subscription/rewriter/vp8.go @@ -0,0 +1,29 @@ +package rewriter + +import ( + "github.com/pion/rtp" + "github.com/pion/rtp/codecs" +) + +// Determines if a given packet contains a VP8 keyframe. +func IsVP8Keyframe(packet rtp.Packet) bool { + // First try to parse the packet as a VP8 packet. + vp8Packet := codecs.VP8Packet{} + + payload, err := vp8Packet.Unmarshal(packet.Payload) + if err != nil { + return false + } + + // At this point we know that we're dealing with a VP8 packet + // and we have a so-called "VP8 Payload Descriptor" that Pion + // parses into the `vp8Packet` structure. This is not to be + // confused with the "VP8 Payload Header", one bit of which + // we're parsing here. The P bit is set to 0 for the key frames. + Pbit := (payload[0] & 0x01) + + // We also must check that the S bit from the VP8 Payload Descriptor + // is set to 1 as S denotes a start of a new VP8 partition. Typically + // key frames have it set to 1. + return vp8Packet.S == 1 && Pbit == 0 +}