From 65e7efa775abb24316097e08e14ec5ed03a9be92 Mon Sep 17 00:00:00 2001 From: Alexey Khit Date: Wed, 23 Nov 2022 21:45:10 +0300 Subject: [PATCH] Support codecs negotiation for MSE --- cmd/mp4/mse.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ pkg/mp4/consumer.go | 6 ++++++ pkg/mp4/muxer.go | 5 +++-- 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/cmd/mp4/mse.go b/cmd/mp4/mse.go index fb5fe5a4..3590fcea 100644 --- a/cmd/mp4/mse.go +++ b/cmd/mp4/mse.go @@ -5,6 +5,7 @@ import ( "github.com/AlexxIT/go2rtc/cmd/streams" "github.com/AlexxIT/go2rtc/pkg/mp4" "github.com/AlexxIT/go2rtc/pkg/streamer" + "strings" ) const MsgTypeMSE = "mse" // fMP4 @@ -22,6 +23,10 @@ func handlerWS(ctx *api.Context, msg *streamer.Message) { cons.UserAgent = ctx.Request.UserAgent() cons.RemoteAddr = ctx.Request.RemoteAddr + if codecs, ok := msg.Value.(string); ok { + cons.Medias = parseMedias(codecs) + } + cons.Listen(func(msg interface{}) { if data, ok := msg.([]byte); ok { for len(data) > packetSize { @@ -55,3 +60,42 @@ func handlerWS(ctx *api.Context, msg *streamer.Message) { cons.Start() } + +func parseMedias(codecs string) (medias []*streamer.Media) { + var videos []*streamer.Codec + var audios []*streamer.Codec + + for _, name := range strings.Split(codecs, ",") { + switch name { + case "avc1.640029": + codec := &streamer.Codec{Name: streamer.CodecH264} + videos = append(videos, codec) + case "hvc1.1.6.L153.B0": + codec := &streamer.Codec{Name: streamer.CodecH265} + videos = append(videos, codec) + case "mp4a.40.2": + codec := &streamer.Codec{Name: streamer.CodecAAC} + audios = append(audios, codec) + } + } + + if videos != nil { + media := &streamer.Media{ + Kind: streamer.KindVideo, + Direction: streamer.DirectionRecvonly, + Codecs: videos, + } + medias = append(medias, media) + } + + if audios != nil { + media := &streamer.Media{ + Kind: streamer.KindAudio, + Direction: streamer.DirectionRecvonly, + Codecs: audios, + } + medias = append(medias, media) + } + + return +} diff --git a/pkg/mp4/consumer.go b/pkg/mp4/consumer.go index d5882a45..f6a26657 100644 --- a/pkg/mp4/consumer.go +++ b/pkg/mp4/consumer.go @@ -12,6 +12,7 @@ import ( type Consumer struct { streamer.Element + Medias []*streamer.Media UserAgent string RemoteAddr string @@ -23,6 +24,11 @@ type Consumer struct { } func (c *Consumer) GetMedias() []*streamer.Media { + if c.Medias != nil { + return c.Medias + } + + // default medias return []*streamer.Media{ { Kind: streamer.KindVideo, diff --git a/pkg/mp4/muxer.go b/pkg/mp4/muxer.go index c167c478..ed99bb74 100644 --- a/pkg/mp4/muxer.go +++ b/pkg/mp4/muxer.go @@ -35,8 +35,9 @@ func (m *Muxer) MimeType(codecs []*streamer.Codec) string { case streamer.CodecH264: s += "avc1." + h264.GetProfileLevelID(codec.FmtpLine) case streamer.CodecH265: - // +Safari +Chrome +Edge -iOS15 -Android13 - s += "hvc1.1.6.L93.B0" // hev1.1.6.L93.B0 + // H.265 profile=main level=5.1 + // hvc1 - supported in Safari, hev1 - doesn't, both supported in Chrome + s += "hvc1.1.6.L153.B0" case streamer.CodecAAC: s += "mp4a.40.2" }