From 6016b9b1d03edaa419637edb33382f4407238ab9 Mon Sep 17 00:00:00 2001 From: Josh Allmann Date: Fri, 6 Sep 2024 17:14:16 +0000 Subject: [PATCH] Check audio codec --- server/mediaserver.go | 7 +++-- server/mediaserver_test.go | 59 ++++++++++++++++++++++++-------------- 2 files changed, 41 insertions(+), 25 deletions(-) diff --git a/server/mediaserver.go b/server/mediaserver.go index 2617f3217d..975aef7fb2 100644 --- a/server/mediaserver.go +++ b/server/mediaserver.go @@ -989,7 +989,7 @@ func (s *LivepeerServer) HandlePush(w http.ResponseWriter, r *http.Request) { cxn.mu.Lock() if cxn.mediaFormat == (ffmpeg.MediaFormatInfo{}) { cxn.mediaFormat = mediaFormat - } else if !videoCompatible(cxn.mediaFormat, mediaFormat) { + } else if !mediaCompatible(cxn.mediaFormat, mediaFormat) { cxn.mediaFormat = mediaFormat segPar.ForceSessionReinit = true } @@ -1650,8 +1650,9 @@ func getRemoteAddr(r *http.Request) string { return strings.Split(addr, ":")[0] } -func videoCompatible(a, b ffmpeg.MediaFormatInfo) bool { - return a.Vcodec == b.Vcodec && +func mediaCompatible(a, b ffmpeg.MediaFormatInfo) bool { + return a.Acodec == b.Acodec && + a.Vcodec == b.Vcodec && a.PixFormat == b.PixFormat && a.Width == b.Width && a.Height == b.Height diff --git a/server/mediaserver_test.go b/server/mediaserver_test.go index 1d7964be6e..8a7db8659f 100644 --- a/server/mediaserver_test.go +++ b/server/mediaserver_test.go @@ -1494,16 +1494,17 @@ func TestJsonProfileToVideoProfiles(t *testing.T) { assert.Equal("unable to parse the H264 encoder profile: unknown VideoProfile profile name", err.Error()) } -func TestVideoCompatible(t *testing.T) { +func TestMediaCompatible(t *testing.T) { empty := ffmpeg.MediaFormatInfo{} normal := ffmpeg.MediaFormatInfo{ - Acodec: "aac", - Vcodec: "h264", - PixFormat: ffmpeg.PixelFormat{RawValue: ffmpeg.PixelFormatNV12}, - Format: "mpegts", - Width: 100, - Height: 200, - DurSecs: 5, + Acodec: "aac", + Vcodec: "h264", + PixFormat: ffmpeg.PixelFormat{RawValue: ffmpeg.PixelFormatNV12}, + Format: "mpegts", + Width: 100, + Height: 200, + AudioBitrate: 300, + DurSecs: 5, } tests := []struct { name string @@ -1519,19 +1520,21 @@ func TestVideoCompatible(t *testing.T) { match: true, a: normal, b: ffmpeg.MediaFormatInfo{ - Acodec: "opus", - Format: "mp4", - DurSecs: 10, - Vcodec: normal.Vcodec, - PixFormat: normal.PixFormat, - Width: normal.Width, - Height: normal.Height, + Format: "mp4", + DurSecs: 10, + AudioBitrate: 400, + Acodec: normal.Acodec, + Vcodec: normal.Vcodec, + PixFormat: normal.PixFormat, + Width: normal.Width, + Height: normal.Height, }, }, { name: "w", a: normal, b: ffmpeg.MediaFormatInfo{ Width: normal.Width + 1, + Acodec: normal.Acodec, Vcodec: normal.Vcodec, PixFormat: normal.PixFormat, Height: normal.Height, @@ -1540,36 +1543,48 @@ func TestVideoCompatible(t *testing.T) { name: "h", a: normal, b: ffmpeg.MediaFormatInfo{ - Width: normal.Width, + Height: normal.Height + 1, + Acodec: normal.Acodec, Vcodec: normal.Vcodec, PixFormat: normal.PixFormat, - Height: normal.Height + 1, + Width: normal.Width, }, }, { name: "pixfmt", a: normal, b: ffmpeg.MediaFormatInfo{ Width: normal.Width, + Acodec: normal.Acodec, Vcodec: normal.Vcodec, PixFormat: ffmpeg.PixelFormat{RawValue: ffmpeg.PixelFormatYUV420P}, Height: normal.Height, }, }, { - name: "codec", + name: "video codec", a: normal, b: ffmpeg.MediaFormatInfo{ - Width: normal.Width, + Vcodec: "flv", Acodec: normal.Acodec, Format: normal.Format, - DurSecs: normal.DurSecs, - Vcodec: "flv", PixFormat: normal.PixFormat, + Width: normal.Width, + Height: normal.Height, + }, + }, { + name: "audio codec", + a: normal, + b: ffmpeg.MediaFormatInfo{ + Acodec: "opus", + Vcodec: normal.Vcodec, + Format: normal.Format, + PixFormat: normal.PixFormat, + Width: normal.Width, Height: normal.Height, }, }} for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - assert.Equal(t, tt.match, videoCompatible(tt.a, tt.b)) + assert.Equal(t, tt.match, mediaCompatible(tt.a, tt.b)) }) } }