From 18a6e04854b90ac9886009004a572893aa550d4f Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Thu, 23 Dec 2021 18:05:28 +0100 Subject: [PATCH] hls: fix Android compatibility by adding missing field into ADTS packets (#749) --- internal/hls/muxer_test.go | 19 ++++++++++++++++++- internal/hls/muxer_ts_generator.go | 1 + 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/internal/hls/muxer_test.go b/internal/hls/muxer_test.go index f7a92f5a..eff1ff50 100644 --- a/internal/hls/muxer_test.go +++ b/internal/hls/muxer_test.go @@ -7,6 +7,7 @@ import ( "time" "github.com/aler9/gortsplib" + "github.com/aler9/gortsplib/pkg/aac" "github.com/stretchr/testify/require" ) @@ -89,13 +90,17 @@ func TestMuxer(t *testing.T) { byts, err = ioutil.ReadAll(m.Segment(ma[1])) require.NoError(t, err) + // PMT checkTSPacket(t, byts, 0, 1) byts = byts[188:] + + // PAT checkTSPacket(t, byts, 4096, 1) byts = byts[188:] + // PES (H264) checkTSPacket(t, byts, 256, 3) - byts = byts[4+145+15:] + byts = byts[164:] require.Equal(t, []byte{ 0, 0, 0, 1, 9, 240, // AUD @@ -105,6 +110,18 @@ func TestMuxer(t *testing.T) { }, byts[:24], ) + byts = byts[24:] + + // PES (AAC) + checkTSPacket(t, byts, 257, 3) + byts = byts[177:] + aus, err := aac.DecodeADTS(byts[:11]) + require.NoError(t, err) + require.Equal(t, 1, len(aus)) + require.Equal(t, 2, aus[0].Type) + require.Equal(t, 44100, aus[0].SampleRate) + require.Equal(t, 2, aus[0].ChannelCount) + require.Equal(t, []byte{0x01, 0x02, 0x03, 0x04}, aus[0].AU) } func TestMuxerCloseBeforeFirstSegment(t *testing.T) { diff --git a/internal/hls/muxer_ts_generator.go b/internal/hls/muxer_ts_generator.go index bbd6eae5..92b4ac51 100644 --- a/internal/hls/muxer_ts_generator.go +++ b/internal/hls/muxer_ts_generator.go @@ -148,6 +148,7 @@ func (m *muxerTSGenerator) writeAAC(pts time.Duration, aus [][]byte) error { for _, au := range aus { enc, err := aac.EncodeADTS([]*aac.ADTSPacket{ { + Type: m.aacConf.Type, SampleRate: m.aacConf.SampleRate, ChannelCount: m.aacConf.ChannelCount, AU: au,