add support for vp9 and av1 (#574)
This commit is contained in:
@@ -219,12 +219,12 @@ func NewConfig(confString string, c *cli.Context) (*Config, error) {
|
|||||||
Redis: RedisConfig{},
|
Redis: RedisConfig{},
|
||||||
Room: RoomConfig{
|
Room: RoomConfig{
|
||||||
AutoCreate: true,
|
AutoCreate: true,
|
||||||
// by default only enable opus and VP8
|
|
||||||
EnabledCodecs: []CodecSpec{
|
EnabledCodecs: []CodecSpec{
|
||||||
{Mime: webrtc.MimeTypeOpus},
|
{Mime: webrtc.MimeTypeOpus},
|
||||||
{Mime: webrtc.MimeTypeVP8},
|
{Mime: webrtc.MimeTypeVP8},
|
||||||
{Mime: webrtc.MimeTypeH264},
|
{Mime: webrtc.MimeTypeH264},
|
||||||
// {Mime: webrtc.MimeTypeVP9},
|
{Mime: webrtc.MimeTypeAV1},
|
||||||
|
{Mime: webrtc.MimeTypeVP9},
|
||||||
},
|
},
|
||||||
EmptyTimeout: 5 * 60,
|
EmptyTimeout: 5 * 60,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -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},
|
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,
|
PayloadType: 123,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeAV1, ClockRate: 90000, RTCPFeedback: rtcpFeedback.Video},
|
||||||
|
PayloadType: 35,
|
||||||
|
},
|
||||||
} {
|
} {
|
||||||
if isCodecEnabled(codecs, codec.RTPCodecCapability) {
|
if isCodecEnabled(codecs, codec.RTPCodecCapability) {
|
||||||
if err := me.RegisterCodec(codec, webrtc.RTPCodecTypeVideo); err != nil {
|
if err := me.RegisterCodec(codec, webrtc.RTPCodecTypeVideo); err != nil {
|
||||||
|
|||||||
@@ -452,6 +452,11 @@ func (b *Buffer) getExtPacket(rawPacket []byte, rtpPacket *rtp.Packet, arrivalTi
|
|||||||
ep.TemporalLayer = int32(vp8Packet.TID)
|
ep.TemporalLayer = int32(vp8Packet.TID)
|
||||||
case "video/h264":
|
case "video/h264":
|
||||||
ep.KeyFrame = IsH264Keyframe(rtpPacket.Payload)
|
ep.KeyFrame = IsH264Keyframe(rtpPacket.Payload)
|
||||||
|
|
||||||
|
case "video/vp9":
|
||||||
|
ep.KeyFrame = IsVp9Keyframe(rtpPacket.Payload)
|
||||||
|
case "video/av1":
|
||||||
|
ep.KeyFrame = IsAV1Keyframe(rtpPacket.Payload)
|
||||||
}
|
}
|
||||||
if ep.KeyFrame {
|
if ep.KeyFrame {
|
||||||
b.logger.Debugw("key frame received")
|
b.logger.Debugw("key frame received")
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/livekit/protocol/logger"
|
"github.com/livekit/protocol/logger"
|
||||||
|
"github.com/pion/rtp/codecs"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -277,6 +278,105 @@ func IsH264Keyframe(payload []byte) bool {
|
|||||||
return false
|
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 (
|
var (
|
||||||
|
|||||||
Reference in New Issue
Block a user