diff --git a/cmd/mp4/mp4.go b/cmd/mp4/mp4.go index 825cb647..e6df4910 100644 --- a/cmd/mp4/mp4.go +++ b/cmd/mp4/mp4.go @@ -9,7 +9,6 @@ import ( "github.com/AlexxIT/go2rtc/cmd/api" "github.com/AlexxIT/go2rtc/cmd/app" "github.com/AlexxIT/go2rtc/cmd/streams" - "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/mp4" "github.com/AlexxIT/go2rtc/pkg/tcp" "github.com/rs/zerolog" @@ -106,12 +105,15 @@ func handlerMP4(w http.ResponseWriter, r *http.Request) { cons := &mp4.Consumer{ RemoteAddr: tcp.RemoteAddr(r), UserAgent: r.UserAgent(), - Medias: core.ParseQuery(r.URL.Query()), + Medias: mp4.ParseQuery(r.URL.Query()), } cons.Listen(func(msg any) { + if exit == nil { + return + } if data, ok := msg.([]byte); ok { - if _, err := w.Write(data); err != nil && exit != nil { + if _, err := w.Write(data); err != nil { select { case exit <- err: default: @@ -151,7 +153,10 @@ func handlerMP4(w http.ResponseWriter, r *http.Request) { if i, _ := strconv.Atoi(s); i > 0 { duration = time.AfterFunc(time.Second*time.Duration(i), func() { if exit != nil { - exit <- nil + select { + case exit <- nil: + default: + } exit = nil } }) @@ -159,6 +164,7 @@ func handlerMP4(w http.ResponseWriter, r *http.Request) { } err = <-exit + exit = nil log.Trace().Err(err).Caller().Send() diff --git a/pkg/mp4/helpers.go b/pkg/mp4/helpers.go index 909b59cb..c22f1220 100644 --- a/pkg/mp4/helpers.go +++ b/pkg/mp4/helpers.go @@ -4,9 +4,45 @@ import "github.com/AlexxIT/go2rtc/pkg/core" // ParseQuery - like usual parse, but with mp4 param handler func ParseQuery(query map[string][]string) []*core.Media { - if query["mp4"] != nil { - cons := Consumer{} - return cons.GetMedias() + if v := query["mp4"]; len(v) != 0 { + medias := []*core.Media{ + { + Kind: core.KindVideo, + Direction: core.DirectionSendonly, + Codecs: []*core.Codec{ + {Name: core.CodecH264}, + {Name: core.CodecH265}, + }, + }, + { + Kind: core.KindAudio, + Direction: core.DirectionSendonly, + Codecs: []*core.Codec{ + {Name: core.CodecAAC}, + }, + }, + } + + if v[0] == "" { + return medias // legacy + } + + medias[1].Codecs = append(medias[1].Codecs, + &core.Codec{Name: core.CodecPCMA}, + &core.Codec{Name: core.CodecPCMU}, + &core.Codec{Name: core.CodecPCM}, + ) + + if v[0] == "flac" { + return medias // modern browsers + } + + medias[1].Codecs = append(medias[1].Codecs, + &core.Codec{Name: core.CodecOpus}, + &core.Codec{Name: core.CodecMP3}, + ) + + return medias // Chrome, FFmpeg, VLC } return core.ParseQuery(query) diff --git a/www/links.html b/www/links.html index 94f5e4df..fab598a6 100644 --- a/www/links.html +++ b/www/links.html @@ -68,11 +68,13 @@