mediamtx/internal/hls/muxer_test.go

199 lines
4.6 KiB
Go
Raw Normal View History

2021-07-24 16:31:54 +00:00
package hls
import (
"io/ioutil"
2021-08-14 14:12:35 +00:00
"regexp"
2021-07-24 16:31:54 +00:00
"testing"
"time"
"github.com/aler9/gortsplib"
"github.com/aler9/gortsplib/pkg/aac"
2021-07-24 16:31:54 +00:00
"github.com/stretchr/testify/require"
)
2021-08-14 14:12:35 +00:00
func checkTSPacket(t *testing.T, byts []byte, pid int, afc int) {
require.Equal(t, byte(0x47), byts[0]) // sync bit
require.Equal(t, uint16(pid), (uint16(byts[1])<<8|uint16(byts[2]))&0x1fff) // PID
require.Equal(t, uint8(afc), (byts[3]>>4)&0x03) // adaptation field control
}
2022-01-28 23:17:05 +00:00
func TestMuxerVideoAudio(t *testing.T) {
2022-01-30 16:36:42 +00:00
videoTrack, err := gortsplib.NewTrackH264(96, []byte{0x07, 0x01, 0x02, 0x03}, []byte{0x08}, nil)
2021-07-24 16:31:54 +00:00
require.NoError(t, err)
2022-01-30 16:36:42 +00:00
audioTrack, err := gortsplib.NewTrackAAC(97, 2, 44100, 2, nil)
2021-07-24 16:31:54 +00:00
require.NoError(t, err)
2021-08-14 14:12:35 +00:00
m, err := NewMuxer(3, 1*time.Second, videoTrack, audioTrack)
2021-07-24 16:31:54 +00:00
require.NoError(t, err)
defer m.Close()
// group without IDR
err = m.WriteH264(1*time.Second, [][]byte{
{0x06},
{0x07},
})
require.NoError(t, err)
// group with IDR
err = m.WriteH264(2*time.Second, [][]byte{
2021-08-14 14:12:35 +00:00
{5}, // IDR
{9}, // AUD
{8}, // PPS
{7}, // SPS
2021-07-24 16:31:54 +00:00
})
require.NoError(t, err)
err = m.WriteAAC(3*time.Second, [][]byte{
{0x01, 0x02, 0x03, 0x04},
{0x05, 0x06, 0x07, 0x08},
})
require.NoError(t, err)
// group without IDR
err = m.WriteH264(4*time.Second, [][]byte{
2021-08-14 14:12:35 +00:00
{6},
{7},
})
require.NoError(t, err)
time.Sleep(2 * time.Second)
// group with IDR
err = m.WriteH264(6*time.Second, [][]byte{
{5}, // IDR
2021-07-24 16:31:54 +00:00
})
require.NoError(t, err)
2021-08-16 16:07:10 +00:00
byts, err := ioutil.ReadAll(m.PrimaryPlaylist())
require.NoError(t, err)
require.Equal(t, "#EXTM3U\n"+
"#EXT-X-STREAM-INF:BANDWIDTH=200000,CODECS=\"avc1.010203,mp4a.40.2\"\n"+
"stream.m3u8\n", string(byts))
byts, err = ioutil.ReadAll(m.StreamPlaylist())
2021-07-24 16:31:54 +00:00
require.NoError(t, err)
2021-08-14 14:12:35 +00:00
re := regexp.MustCompile(`^#EXTM3U\n` +
`#EXT-X-VERSION:3\n` +
`#EXT-X-ALLOW-CACHE:NO\n` +
`#EXT-X-TARGETDURATION:4\n` +
2021-08-14 14:12:35 +00:00
`#EXT-X-MEDIA-SEQUENCE:0\n` +
`#EXTINF:4,\n` +
2021-08-14 14:12:35 +00:00
`([0-9]+\.ts)\n$`)
ma := re.FindStringSubmatch(string(byts))
require.NotEqual(t, 0, len(ma))
2021-08-14 14:12:35 +00:00
byts, err = ioutil.ReadAll(m.Segment(ma[1]))
2021-08-14 14:12:35 +00:00
require.NoError(t, err)
// PMT
2021-08-14 14:12:35 +00:00
checkTSPacket(t, byts, 0, 1)
byts = byts[188:]
// PAT
2021-08-14 14:12:35 +00:00
checkTSPacket(t, byts, 4096, 1)
byts = byts[188:]
// PES (H264)
2021-08-14 14:12:35 +00:00
checkTSPacket(t, byts, 256, 3)
byts = byts[164:]
2021-08-14 14:12:35 +00:00
require.Equal(t,
[]byte{
0, 0, 0, 1, 9, 240, // AUD
0, 0, 0, 1, 7, 1, 2, 3, // SPS
0, 0, 0, 1, 8, // PPS
0, 0, 0, 1, 5, // IDR
2021-08-14 14:12:35 +00:00
},
byts[:24],
2021-08-14 14:12:35 +00:00
)
byts = byts[24:]
// PES (AAC)
checkTSPacket(t, byts, 257, 3)
2021-12-23 18:13:06 +00:00
byts = byts[166:]
aus, err := aac.DecodeADTS(byts[:22])
require.NoError(t, err)
2021-12-23 18:13:06 +00:00
require.Equal(t, 2, 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)
2021-12-23 18:13:06 +00:00
require.Equal(t, 2, aus[1].Type)
require.Equal(t, 44100, aus[1].SampleRate)
require.Equal(t, 2, aus[1].ChannelCount)
require.Equal(t, []byte{0x05, 0x06, 0x07, 0x08}, aus[1].AU)
2021-07-24 16:31:54 +00:00
}
2022-01-28 23:17:05 +00:00
func TestMuxerAudio(t *testing.T) {
2022-01-30 16:36:42 +00:00
audioTrack, err := gortsplib.NewTrackAAC(97, 2, 44100, 2, nil)
2022-01-28 23:17:05 +00:00
require.NoError(t, err)
m, err := NewMuxer(3, 1*time.Second, nil, audioTrack)
require.NoError(t, err)
defer m.Close()
for i := 0; i < 100; i++ {
err = m.WriteAAC(1*time.Second, [][]byte{
{0x01, 0x02, 0x03, 0x04},
})
require.NoError(t, err)
}
err = m.WriteAAC(2*time.Second, [][]byte{
{0x01, 0x02, 0x03, 0x04},
{0x05, 0x06, 0x07, 0x08},
})
require.NoError(t, err)
err = m.WriteAAC(3*time.Second, [][]byte{
{0x01, 0x02, 0x03, 0x04},
{0x05, 0x06, 0x07, 0x08},
})
require.NoError(t, err)
byts, err := ioutil.ReadAll(m.PrimaryPlaylist())
require.NoError(t, err)
require.Equal(t, "#EXTM3U\n"+
"#EXT-X-STREAM-INF:BANDWIDTH=200000,CODECS=\"mp4a.40.2\"\n"+
"stream.m3u8\n", string(byts))
byts, err = ioutil.ReadAll(m.StreamPlaylist())
require.NoError(t, err)
re := regexp.MustCompile(`^#EXTM3U\n` +
`#EXT-X-VERSION:3\n` +
`#EXT-X-ALLOW-CACHE:NO\n` +
`#EXT-X-TARGETDURATION:1\n` +
`#EXT-X-MEDIA-SEQUENCE:0\n` +
`#EXTINF:1,\n` +
`([0-9]+\.ts)\n$`)
ma := re.FindStringSubmatch(string(byts))
require.NotEqual(t, 0, len(ma))
}
func TestMuxerCloseBeforeFirstSegment(t *testing.T) {
2022-01-30 16:36:42 +00:00
videoTrack, err := gortsplib.NewTrackH264(96, []byte{0x07, 0x01, 0x02, 0x03}, []byte{0x08}, nil)
require.NoError(t, err)
2022-01-30 16:36:42 +00:00
m, err := NewMuxer(3, 1*time.Second, videoTrack, nil)
require.NoError(t, err)
// group with IDR
err = m.WriteH264(2*time.Second, [][]byte{
{5}, // IDR
{9}, // AUD
{8}, // PPS
{7}, // SPS
})
require.NoError(t, err)
m.Close()
byts, err := ioutil.ReadAll(m.StreamPlaylist())
require.NoError(t, err)
require.Equal(t, []byte{}, byts)
}