diff --git a/pkg/config/config.go b/pkg/config/config.go index 6a0f24af..2d45cd87 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -219,12 +219,12 @@ func NewConfig(confString string, c *cli.Context) (*Config, error) { Redis: RedisConfig{}, Room: RoomConfig{ AutoCreate: true, - // by default only enable opus and VP8 EnabledCodecs: []CodecSpec{ {Mime: webrtc.MimeTypeOpus}, {Mime: webrtc.MimeTypeVP8}, {Mime: webrtc.MimeTypeH264}, - // {Mime: webrtc.MimeTypeVP9}, + {Mime: webrtc.MimeTypeAV1}, + {Mime: webrtc.MimeTypeVP9}, }, EmptyTimeout: 5 * 60, }, diff --git a/pkg/rtc/mediaengine.go b/pkg/rtc/mediaengine.go index 8da740c4..19e1f6e5 100644 --- a/pkg/rtc/mediaengine.go +++ b/pkg/rtc/mediaengine.go @@ -43,6 +43,10 @@ func registerCodecs(me *webrtc.MediaEngine, codecs []*livekit.Codec, rtcpFeedbac RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeH264, ClockRate: 90000, SDPFmtpLine: "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=640032", RTCPFeedback: rtcpFeedback.Video}, PayloadType: 123, }, + { + RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeAV1, ClockRate: 90000, RTCPFeedback: rtcpFeedback.Video}, + PayloadType: 35, + }, } { if isCodecEnabled(codecs, codec.RTPCodecCapability) { if err := me.RegisterCodec(codec, webrtc.RTPCodecTypeVideo); err != nil { diff --git a/pkg/sfu/buffer/buffer.go b/pkg/sfu/buffer/buffer.go index 20f9aabf..9f95141a 100644 --- a/pkg/sfu/buffer/buffer.go +++ b/pkg/sfu/buffer/buffer.go @@ -452,6 +452,11 @@ func (b *Buffer) getExtPacket(rawPacket []byte, rtpPacket *rtp.Packet, arrivalTi ep.TemporalLayer = int32(vp8Packet.TID) case "video/h264": ep.KeyFrame = IsH264Keyframe(rtpPacket.Payload) + + case "video/vp9": + ep.KeyFrame = IsVp9Keyframe(rtpPacket.Payload) + case "video/av1": + ep.KeyFrame = IsAV1Keyframe(rtpPacket.Payload) } if ep.KeyFrame { b.logger.Debugw("key frame received") diff --git a/pkg/sfu/buffer/helpers.go b/pkg/sfu/buffer/helpers.go index 29435213..c9f7a530 100644 --- a/pkg/sfu/buffer/helpers.go +++ b/pkg/sfu/buffer/helpers.go @@ -6,6 +6,7 @@ import ( "time" "github.com/livekit/protocol/logger" + "github.com/pion/rtp/codecs" ) var ( @@ -277,6 +278,105 @@ func IsH264Keyframe(payload []byte) bool { return false } +// IsAV1Keyframe detects if vp9 payload is a keyframe +// taken from https://github.com/jech/galene/blob/master/codecs/codecs.go +// all credits belongs to Juliusz Chroboczek @jech and the awesome Galene SFU +func IsVp9Keyframe(payload []byte) bool { + var vp9 codecs.VP9Packet + _, err := vp9.Unmarshal(payload) + if err != nil || len(vp9.Payload) < 1 { + return false + } + if !vp9.B { + return false + } + + if (vp9.Payload[0] & 0xc0) != 0x80 { + return false + } + + profile := (vp9.Payload[0] >> 4) & 0x3 + if profile != 3 { + return (vp9.Payload[0] & 0xC) == 0 + } + return (vp9.Payload[0] & 0x6) == 0 +} + +// IsAV1Keyframe detects if av1 payload is a keyframe +// taken from https://github.com/jech/galene/blob/master/codecs/codecs.go +// all credits belongs to Juliusz Chroboczek @jech and the awesome Galene SFU +func IsAV1Keyframe(payload []byte) bool { + if len(payload) < 2 { + return false + } + // Z=0, N=1 + if (payload[0] & 0x88) != 0x08 { + return false + } + w := (payload[0] & 0x30) >> 4 + + getObu := func(data []byte, last bool) ([]byte, int, bool) { + if last { + return data, len(data), false + } + offset := 0 + length := 0 + for { + if len(data) <= offset { + return nil, offset, offset > 0 + } + l := data[offset] + length |= int(l&0x7f) << (offset * 7) + offset++ + if (l & 0x80) == 0 { + break + } + } + if len(data) < offset+length { + return data[offset:], len(data), true + } + return data[offset : offset+length], + offset + length, false + } + offset := 1 + i := 0 + for { + obu, length, truncated := + getObu(payload[offset:], int(w) == i+1) + if len(obu) < 1 { + return false + } + tpe := (obu[0] & 0x38) >> 3 + switch i { + case 0: + // OBU_SEQUENCE_HEADER + if tpe != 1 { + return false + } + default: + // OBU_FRAME_HEADER or OBU_FRAME + if tpe == 3 || tpe == 6 { + if len(obu) < 2 { + return false + } + // show_existing_frame == 0 + if (obu[1] & 0x80) != 0 { + return false + } + // frame_type == KEY_FRAME + return (obu[1] & 0x60) == 0 + } + } + if truncated || i >= int(w) { + // the first frame header is in a second + // packet, give up. + return false + } + offset += length + i++ + } +} + // ------------------------------------- var (