From 339d6ab16ea9f49273a3a9b043a5c2da8509c170 Mon Sep 17 00:00:00 2001 From: Alessandro Ros Date: Sun, 19 Feb 2023 00:28:50 +0100 Subject: [PATCH] hls source: set type of audio medias to 'audio' (#1489) it was wrongly set to "video". --- .../{externalauth.go => external_auth.go} | 0 internal/core/hls_source.go | 9 ++- internal/core/hls_source_test.go | 66 ++++++++++++++++++- .../{streamformat.go => stream_format.go} | 0 .../core/{streammedia.go => stream_media.go} | 0 5 files changed, 73 insertions(+), 2 deletions(-) rename internal/core/{externalauth.go => external_auth.go} (100%) rename internal/core/{streamformat.go => stream_format.go} (100%) rename internal/core/{streammedia.go => stream_media.go} (100%) diff --git a/internal/core/externalauth.go b/internal/core/external_auth.go similarity index 100% rename from internal/core/externalauth.go rename to internal/core/external_auth.go diff --git a/internal/core/hls_source.go b/internal/core/hls_source.go index 1e58eeb1..74dda62e 100644 --- a/internal/core/hls_source.go +++ b/internal/core/hls_source.go @@ -59,7 +59,6 @@ func (s *hlsSource) run(ctx context.Context, cnf *conf.PathConf, reloadConf chan for _, track := range tracks { medi := &media.Media{ - Type: media.TypeVideo, Formats: []format.Format{track}, } medias = append(medias, medi) @@ -67,6 +66,8 @@ func (s *hlsSource) run(ctx context.Context, cnf *conf.PathConf, reloadConf chan switch track.(type) { case *format.H264: + medi.Type = media.TypeVideo + c.OnData(track, func(pts time.Duration, dat interface{}) { err := stream.writeData(medi, ctrack, &formatprocessor.DataH264{ PTS: pts, @@ -79,6 +80,8 @@ func (s *hlsSource) run(ctx context.Context, cnf *conf.PathConf, reloadConf chan }) case *format.H265: + medi.Type = media.TypeVideo + c.OnData(track, func(pts time.Duration, dat interface{}) { err := stream.writeData(medi, ctrack, &formatprocessor.DataH265{ PTS: pts, @@ -91,6 +94,8 @@ func (s *hlsSource) run(ctx context.Context, cnf *conf.PathConf, reloadConf chan }) case *format.MPEG4Audio: + medi.Type = media.TypeAudio + c.OnData(track, func(pts time.Duration, dat interface{}) { err := stream.writeData(medi, ctrack, &formatprocessor.DataMPEG4Audio{ PTS: pts, @@ -103,6 +108,8 @@ func (s *hlsSource) run(ctx context.Context, cnf *conf.PathConf, reloadConf chan }) case *format.Opus: + medi.Type = media.TypeAudio + c.OnData(track, func(pts time.Duration, dat interface{}) { err := stream.writeData(medi, ctrack, &formatprocessor.DataOpus{ PTS: pts, diff --git a/internal/core/hls_source_test.go b/internal/core/hls_source_test.go index 4b43bc7d..1add48c8 100644 --- a/internal/core/hls_source_test.go +++ b/internal/core/hls_source_test.go @@ -11,6 +11,9 @@ import ( "github.com/aler9/gortsplib/v2" "github.com/aler9/gortsplib/v2/pkg/codecs/h264" + "github.com/aler9/gortsplib/v2/pkg/codecs/mpeg4audio" + "github.com/aler9/gortsplib/v2/pkg/format" + "github.com/aler9/gortsplib/v2/pkg/media" "github.com/aler9/gortsplib/v2/pkg/url" "github.com/asticode/go-astits" "github.com/gin-gonic/gin" @@ -69,6 +72,11 @@ func (ts *testHLSServer) onSegment(ctx *gin.Context) { StreamType: astits.StreamTypeH264Video, }) + mux.AddElementaryStream(astits.PMTElementaryStream{ + ElementaryPID: 257, + StreamType: astits.StreamTypeAACAudio, + }) + mux.SetPCRPID(256) mux.WriteTables() @@ -87,7 +95,33 @@ func (ts *testHLSServer) onSegment(ctx *gin.Context) { PTSDTSIndicator: astits.PTSDTSIndicatorOnlyPTS, PTS: &astits.ClockReference{Base: int64(1 * 90000)}, }, - StreamID: 224, // = video + StreamID: 224, + }, + Data: enc, + }, + }) + + pkts := mpeg4audio.ADTSPackets{ + { + Type: 2, + SampleRate: 44100, + ChannelCount: 2, + AU: []byte{0x01, 0x02, 0x03, 0x04}, + }, + } + + enc, _ = pkts.Marshal() + + mux.WriteData(&astits.MuxerData{ + PID: 257, + PES: &astits.PESData{ + Header: &astits.PESHeader{ + OptionalHeader: &astits.PESOptionalHeader{ + MarkerBits: 2, + PTSDTSIndicator: astits.PTSDTSIndicatorOnlyPTS, + PTS: &astits.ClockReference{Base: int64(1 * 90000)}, + }, + StreamID: 192, }, Data: enc, }, @@ -148,6 +182,36 @@ func TestHLSSource(t *testing.T) { medias, baseURL, _, err := c.Describe(u) require.NoError(t, err) + require.Equal(t, media.Medias{ + { + Type: media.TypeVideo, + Control: medias[0].Control, + Formats: []format.Format{ + &format.H264{ + PayloadTyp: 96, + PacketizationMode: 1, + }, + }, + }, + { + Type: media.TypeAudio, + Control: medias[1].Control, + Formats: []format.Format{ + &format.MPEG4Audio{ + PayloadTyp: 96, + Config: &mpeg4audio.Config{ + Type: 2, + SampleRate: 44100, + ChannelCount: 2, + }, + SizeLength: 13, + IndexLength: 3, + IndexDeltaLength: 3, + }, + }, + }, + }, medias) + err = c.SetupAll(medias, baseURL) require.NoError(t, err) diff --git a/internal/core/streamformat.go b/internal/core/stream_format.go similarity index 100% rename from internal/core/streamformat.go rename to internal/core/stream_format.go diff --git a/internal/core/streammedia.go b/internal/core/stream_media.go similarity index 100% rename from internal/core/streammedia.go rename to internal/core/stream_media.go