mediamtx/internal/hls/muxer_test.go

497 lines
12 KiB
Go
Raw Normal View History

2021-07-24 16:31:54 +00:00
package hls
import (
2022-08-27 14:00:08 +00:00
"io"
2021-08-14 14:12:35 +00:00
"regexp"
2021-07-24 16:31:54 +00:00
"testing"
"time"
"github.com/aler9/gortsplib/v2/pkg/format"
"github.com/aler9/gortsplib/v2/pkg/mpeg4audio"
2021-07-24 16:31:54 +00:00
"github.com/stretchr/testify/require"
)
var testTime = time.Date(2010, 0o1, 0o1, 0o1, 0o1, 0o1, 0, time.UTC)
2022-06-02 17:03:08 +00:00
// baseline profile without POC
var testSPS = []byte{
0x67, 0x42, 0xc0, 0x28, 0xd9, 0x00, 0x78, 0x02,
0x27, 0xe5, 0x84, 0x00, 0x00, 0x03, 0x00, 0x04,
0x00, 0x00, 0x03, 0x00, 0xf0, 0x3c, 0x60, 0xc9,
0x20,
}
2022-01-28 23:17:05 +00:00
func TestMuxerVideoAudio(t *testing.T) {
videoTrack := &format.H264{
PayloadTyp: 96,
2022-11-15 22:46:40 +00:00
SPS: testSPS,
PPS: []byte{0x08},
PacketizationMode: 1,
2022-06-23 11:54:48 +00:00
}
2021-07-24 16:31:54 +00:00
audioTrack := &format.MPEG4Audio{
PayloadTyp: 97,
2022-08-05 21:50:45 +00:00
Config: &mpeg4audio.Config{
2022-06-24 15:00:28 +00:00
Type: 2,
SampleRate: 44100,
ChannelCount: 2,
},
2022-06-23 11:54:48 +00:00
SizeLength: 13,
IndexLength: 3,
IndexDeltaLength: 3,
}
2021-07-24 16:31:54 +00:00
2022-08-27 14:00:08 +00:00
for _, ca := range []string{
"mpegts",
"fmp4",
} {
t.Run(ca, func(t *testing.T) {
var v MuxerVariant
if ca == "mpegts" {
v = MuxerVariantMPEGTS
} else {
v = MuxerVariantFMP4
}
m, err := NewMuxer(v, 3, 1*time.Second, 0, 50*1024*1024, videoTrack, audioTrack)
require.NoError(t, err)
defer m.Close()
// group without IDR
d := 1 * time.Second
err = m.WriteH264(testTime.Add(d-1*time.Second), d, [][]byte{
{0x06},
{0x07},
})
require.NoError(t, err)
// group with IDR
d = 2 * time.Second
err = m.WriteH264(testTime.Add(d-1*time.Second), d, [][]byte{
testSPS, // SPS
{8}, // PPS
{5}, // IDR
})
require.NoError(t, err)
d = 3 * time.Second
err = m.WriteAAC(testTime.Add(d-1*time.Second), d, []byte{
0x01, 0x02, 0x03, 0x04,
})
require.NoError(t, err)
d = 3500 * time.Millisecond
err = m.WriteAAC(testTime.Add(d-1*time.Second), d, []byte{
0x01, 0x02, 0x03, 0x04,
})
require.NoError(t, err)
// group without IDR
d = 4 * time.Second
err = m.WriteH264(testTime.Add(d-1*time.Second), d, [][]byte{
{1}, // non-IDR
})
require.NoError(t, err)
d = 4500 * time.Millisecond
err = m.WriteAAC(testTime.Add(d-1*time.Second), d, []byte{
0x01, 0x02, 0x03, 0x04,
})
require.NoError(t, err)
// group with IDR
d = 6 * time.Second
err = m.WriteH264(testTime.Add(d-1*time.Second), d, [][]byte{
{5}, // IDR
})
require.NoError(t, err)
// group with IDR
d = 7 * time.Second
err = m.WriteH264(testTime.Add(d-1*time.Second), d, [][]byte{
{5}, // IDR
})
require.NoError(t, err)
2022-09-17 19:19:45 +00:00
byts, err := io.ReadAll(m.File("index.m3u8", "", "", "").Body)
2022-08-27 14:00:08 +00:00
require.NoError(t, err)
if ca == "mpegts" {
require.Equal(t, "#EXTM3U\n"+
"#EXT-X-VERSION:3\n"+
"#EXT-X-INDEPENDENT-SEGMENTS\n"+
"\n"+
"#EXT-X-STREAM-INF:BANDWIDTH=200000,CODECS=\"avc1.42c028,mp4a.40.2\"\n"+
"stream.m3u8\n", string(byts))
} else {
require.Equal(t, "#EXTM3U\n"+
"#EXT-X-VERSION:9\n"+
"#EXT-X-INDEPENDENT-SEGMENTS\n"+
"\n"+
"#EXT-X-STREAM-INF:BANDWIDTH=200000,CODECS=\"avc1.42c028,mp4a.40.2\"\n"+
"stream.m3u8\n", string(byts))
}
2022-09-17 19:19:45 +00:00
byts, err = io.ReadAll(m.File("stream.m3u8", "", "", "").Body)
2022-08-27 14:00:08 +00:00
require.NoError(t, err)
var ma []string
if ca == "mpegts" {
re := regexp.MustCompile(`^#EXTM3U\n` +
`#EXT-X-VERSION:3\n` +
`#EXT-X-ALLOW-CACHE:NO\n` +
`#EXT-X-TARGETDURATION:4\n` +
`#EXT-X-MEDIA-SEQUENCE:0\n` +
`#EXT-X-PROGRAM-DATE-TIME:(.*?)\n` +
`#EXTINF:4,\n` +
`(seg0\.ts)\n` +
2022-08-27 14:00:08 +00:00
`#EXT-X-PROGRAM-DATE-TIME:(.*?)\n` +
`#EXTINF:1,\n` +
`(seg1\.ts)\n$`)
2022-08-27 14:00:08 +00:00
ma = re.FindStringSubmatch(string(byts))
} else {
re := regexp.MustCompile(`^#EXTM3U\n` +
`#EXT-X-VERSION:9\n` +
`#EXT-X-TARGETDURATION:4\n` +
`#EXT-X-MEDIA-SEQUENCE:0\n` +
`#EXT-X-MAP:URI="init.mp4"\n` +
`#EXT-X-PROGRAM-DATE-TIME:(.*?)\n` +
`#EXTINF:4.00000,\n` +
`(seg0\.mp4)\n` +
2022-08-27 14:00:08 +00:00
`#EXT-X-PROGRAM-DATE-TIME:(.*?)\n` +
`#EXTINF:1.00000,\n` +
`(seg1\.mp4)\n$`)
2022-08-27 14:00:08 +00:00
ma = re.FindStringSubmatch(string(byts))
}
require.NotEqual(t, 0, len(ma))
if ca == "mpegts" {
2022-09-17 20:21:16 +00:00
_, err := io.ReadAll(m.File(ma[2], "", "", "").Body)
2022-08-27 14:00:08 +00:00
require.NoError(t, err)
} else {
2022-09-17 20:21:16 +00:00
_, err := io.ReadAll(m.File("init.mp4", "", "", "").Body)
2022-08-27 14:00:08 +00:00
require.NoError(t, err)
2022-09-17 20:21:16 +00:00
_, err = io.ReadAll(m.File(ma[2], "", "", "").Body)
2022-08-27 14:00:08 +00:00
require.NoError(t, err)
}
})
}
2022-03-20 16:46:24 +00:00
}
func TestMuxerVideoOnly(t *testing.T) {
videoTrack := &format.H264{
PayloadTyp: 96,
2022-11-15 22:46:40 +00:00
SPS: testSPS,
PPS: []byte{0x08},
PacketizationMode: 1,
2022-06-23 11:54:48 +00:00
}
2022-03-20 16:46:24 +00:00
2022-08-27 14:00:08 +00:00
for _, ca := range []string{
"mpegts",
"fmp4",
} {
t.Run(ca, func(t *testing.T) {
var v MuxerVariant
if ca == "mpegts" {
v = MuxerVariantMPEGTS
} else {
v = MuxerVariantFMP4
}
m, err := NewMuxer(v, 3, 1*time.Second, 0, 50*1024*1024, videoTrack, nil)
require.NoError(t, err)
defer m.Close()
// group with IDR
d := 2 * time.Second
err = m.WriteH264(testTime.Add(d-2*time.Second), d, [][]byte{
testSPS, // SPS
{8}, // PPS
{5}, // IDR
})
require.NoError(t, err)
// group with IDR
d = 6 * time.Second
err = m.WriteH264(testTime.Add(d-2*time.Second), d, [][]byte{
{5}, // IDR
})
require.NoError(t, err)
// group with IDR
d = 7 * time.Second
err = m.WriteH264(testTime.Add(d-2*time.Second), d, [][]byte{
{5}, // IDR
})
require.NoError(t, err)
2022-09-17 19:19:45 +00:00
byts, err := io.ReadAll(m.File("index.m3u8", "", "", "").Body)
2022-08-27 14:00:08 +00:00
require.NoError(t, err)
if ca == "mpegts" {
require.Equal(t, "#EXTM3U\n"+
"#EXT-X-VERSION:3\n"+
"#EXT-X-INDEPENDENT-SEGMENTS\n"+
"\n"+
"#EXT-X-STREAM-INF:BANDWIDTH=200000,CODECS=\"avc1.42c028\"\n"+
"stream.m3u8\n", string(byts))
} else {
require.Equal(t, "#EXTM3U\n"+
"#EXT-X-VERSION:9\n"+
"#EXT-X-INDEPENDENT-SEGMENTS\n"+
"\n"+
"#EXT-X-STREAM-INF:BANDWIDTH=200000,CODECS=\"avc1.42c028\"\n"+
"stream.m3u8\n", string(byts))
}
2022-09-17 19:19:45 +00:00
byts, err = io.ReadAll(m.File("stream.m3u8", "", "", "").Body)
2022-08-27 14:00:08 +00:00
require.NoError(t, err)
var ma []string
if ca == "mpegts" {
re := regexp.MustCompile(`^#EXTM3U\n` +
`#EXT-X-VERSION:3\n` +
`#EXT-X-ALLOW-CACHE:NO\n` +
`#EXT-X-TARGETDURATION:4\n` +
`#EXT-X-MEDIA-SEQUENCE:0\n` +
`#EXT-X-PROGRAM-DATE-TIME:(.*?)\n` +
`#EXTINF:4,\n` +
`(seg0\.ts)\n` +
2022-08-27 14:00:08 +00:00
`#EXT-X-PROGRAM-DATE-TIME:(.*?)\n` +
`#EXTINF:1,\n` +
`(seg1\.ts)\n$`)
2022-08-27 14:00:08 +00:00
ma = re.FindStringSubmatch(string(byts))
} else {
re := regexp.MustCompile(`^#EXTM3U\n` +
`#EXT-X-VERSION:9\n` +
`#EXT-X-TARGETDURATION:4\n` +
`#EXT-X-MEDIA-SEQUENCE:0\n` +
`#EXT-X-MAP:URI="init.mp4"\n` +
`#EXT-X-PROGRAM-DATE-TIME:(.*?)\n` +
`#EXTINF:4.00000,\n` +
`(seg0\.mp4)\n` +
2022-08-27 14:00:08 +00:00
`#EXT-X-PROGRAM-DATE-TIME:(.*?)\n` +
`#EXTINF:1.00000,\n` +
`(seg1\.mp4)\n$`)
2022-08-27 14:00:08 +00:00
ma = re.FindStringSubmatch(string(byts))
}
require.NotEqual(t, 0, len(ma))
2022-09-17 20:21:16 +00:00
if ca == "mpegts" {
_, err := io.ReadAll(m.File(ma[2], "", "", "").Body)
2022-08-27 14:00:08 +00:00
require.NoError(t, err)
2022-09-17 20:21:16 +00:00
} else {
_, err := io.ReadAll(m.File("init.mp4", "", "", "").Body)
2022-08-27 14:00:08 +00:00
require.NoError(t, err)
2022-09-17 20:21:16 +00:00
_, err = io.ReadAll(m.File(ma[2], "", "", "").Body)
2022-08-27 14:00:08 +00:00
require.NoError(t, err)
}
})
}
2021-07-24 16:31:54 +00:00
}
2022-03-20 16:46:24 +00:00
func TestMuxerAudioOnly(t *testing.T) {
audioTrack := &format.MPEG4Audio{
PayloadTyp: 97,
2022-08-05 21:50:45 +00:00
Config: &mpeg4audio.Config{
2022-06-24 15:00:28 +00:00
Type: 2,
SampleRate: 44100,
ChannelCount: 2,
},
2022-06-23 11:54:48 +00:00
SizeLength: 13,
IndexLength: 3,
IndexDeltaLength: 3,
}
2022-01-28 23:17:05 +00:00
2022-08-27 14:00:08 +00:00
for _, ca := range []string{
"mpegts",
"fmp4",
} {
t.Run(ca, func(t *testing.T) {
var v MuxerVariant
if ca == "mpegts" {
v = MuxerVariantMPEGTS
} else {
v = MuxerVariantFMP4
}
m, err := NewMuxer(v, 3, 1*time.Second, 0, 50*1024*1024, nil, audioTrack)
require.NoError(t, err)
defer m.Close()
for i := 0; i < 100; i++ {
d := 1 * time.Second
err = m.WriteAAC(testTime.Add(d-1*time.Second), d, []byte{
0x01, 0x02, 0x03, 0x04,
})
require.NoError(t, err)
}
d := 2 * time.Second
err = m.WriteAAC(testTime.Add(d-1*time.Second), d, []byte{
0x01, 0x02, 0x03, 0x04,
})
require.NoError(t, err)
d = 3 * time.Second
err = m.WriteAAC(testTime.Add(d-1*time.Second), d, []byte{
0x01, 0x02, 0x03, 0x04,
})
require.NoError(t, err)
2022-09-17 19:19:45 +00:00
byts, err := io.ReadAll(m.File("index.m3u8", "", "", "").Body)
2022-08-27 14:00:08 +00:00
require.NoError(t, err)
if ca == "mpegts" {
require.Equal(t, "#EXTM3U\n"+
"#EXT-X-VERSION:3\n"+
"#EXT-X-INDEPENDENT-SEGMENTS\n"+
"\n"+
"#EXT-X-STREAM-INF:BANDWIDTH=200000,CODECS=\"mp4a.40.2\"\n"+
"stream.m3u8\n", string(byts))
} else {
require.Equal(t, "#EXTM3U\n"+
"#EXT-X-VERSION:9\n"+
"#EXT-X-INDEPENDENT-SEGMENTS\n"+
"\n"+
"#EXT-X-STREAM-INF:BANDWIDTH=200000,CODECS=\"mp4a.40.2\"\n"+
"stream.m3u8\n", string(byts))
}
2022-09-17 19:19:45 +00:00
byts, err = io.ReadAll(m.File("stream.m3u8", "", "", "").Body)
2022-08-27 14:00:08 +00:00
require.NoError(t, err)
var ma []string
if ca == "mpegts" {
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` +
`#EXT-X-PROGRAM-DATE-TIME:(.*?)\n` +
`#EXTINF:1,\n` +
`(seg0\.ts)\n$`)
2022-08-27 14:00:08 +00:00
ma = re.FindStringSubmatch(string(byts))
} else {
re := regexp.MustCompile(`^#EXTM3U\n` +
`#EXT-X-VERSION:9\n` +
`#EXT-X-TARGETDURATION:2\n` +
`#EXT-X-MEDIA-SEQUENCE:0\n` +
`#EXT-X-MAP:URI="init.mp4"\n` +
`#EXT-X-PROGRAM-DATE-TIME:(.*?)\n` +
`#EXTINF:2.32200,\n` +
`(seg0\.mp4)\n` +
2022-08-27 14:00:08 +00:00
`#EXT-X-PROGRAM-DATE-TIME:(.*?)\n` +
`#EXTINF:0.02322,\n` +
`(seg1\.mp4)\n$`)
2022-08-27 14:00:08 +00:00
ma = re.FindStringSubmatch(string(byts))
}
require.NotEqual(t, 0, len(ma))
2022-09-17 20:21:16 +00:00
if ca == "mpegts" {
_, err := io.ReadAll(m.File(ma[2], "", "", "").Body)
2022-08-27 14:00:08 +00:00
require.NoError(t, err)
2022-09-17 20:21:16 +00:00
} else {
_, err := io.ReadAll(m.File("init.mp4", "", "", "").Body)
2022-08-27 14:00:08 +00:00
require.NoError(t, err)
2022-09-17 20:21:16 +00:00
_, err = io.ReadAll(m.File(ma[2], "", "", "").Body)
2022-08-27 14:00:08 +00:00
require.NoError(t, err)
}
2022-01-28 23:17:05 +00:00
})
}
}
Implement Low-Latency HLS (#938) * add hlsVariant parameter * hls: split muxer into variants * hls: implement fmp4 segments * hls muxer: implement low latency mode * hls muxer: support audio with fmp4 mode * hls muxer: rewrite file router * hls muxer: implement preload hint * hls muxer: add various error codes * hls muxer: use explicit flags * hls muxer: fix error in aac pts * hls muxer: fix sudden freezes with video+audio * hls muxer: skip empty parts * hls muxer: fix video FPS * hls muxer: add parameter hlsPartDuration * hls muxer: refactor fmp4 muxer * hls muxer: fix CAN-SKIP-UNTIL * hls muxer: refactor code * hls muxer: show only parts of last 2 segments * hls muxer: implementa playlist delta updates * hls muxer: change playlist content type * hls muxer: improve video dts precision * hls muxer: fix video sample flags * hls muxer: improve iphone audio support * hls muxer: improve mp4 timestamp precision * hls muxer: add offset between pts and dts * hls muxer: close muxer in case of error * hls muxer: stop logging requests with the info level * hls muxer: rename entry into sample * hls muxer: compensate video dts error over time * hls muxer: change default segment count * hls muxer: add starting gap * hls muxer: set default part duration to 200ms * hls muxer: fix audio-only streams on ios * hls muxer: add playsinline attribute to video tag of default web page * hls muxer: keep mpegts as the default hls variant * hls muxer: implement encryption * hls muxer: rewrite dts estimation * hls muxer: improve DTS precision * hls muxer: use right SPS/PPS for each sample * hls muxer: adjust part duration dynamically * add comments * update readme * hls muxer: fix memory leak * hls muxer: decrease ram consumption
2022-05-31 17:17:26 +00:00
func TestMuxerCloseBeforeFirstSegmentReader(t *testing.T) {
videoTrack := &format.H264{
PayloadTyp: 96,
2022-11-15 22:46:40 +00:00
SPS: testSPS,
PPS: []byte{0x08},
PacketizationMode: 1,
2022-06-23 11:54:48 +00:00
}
Implement Low-Latency HLS (#938) * add hlsVariant parameter * hls: split muxer into variants * hls: implement fmp4 segments * hls muxer: implement low latency mode * hls muxer: support audio with fmp4 mode * hls muxer: rewrite file router * hls muxer: implement preload hint * hls muxer: add various error codes * hls muxer: use explicit flags * hls muxer: fix error in aac pts * hls muxer: fix sudden freezes with video+audio * hls muxer: skip empty parts * hls muxer: fix video FPS * hls muxer: add parameter hlsPartDuration * hls muxer: refactor fmp4 muxer * hls muxer: fix CAN-SKIP-UNTIL * hls muxer: refactor code * hls muxer: show only parts of last 2 segments * hls muxer: implementa playlist delta updates * hls muxer: change playlist content type * hls muxer: improve video dts precision * hls muxer: fix video sample flags * hls muxer: improve iphone audio support * hls muxer: improve mp4 timestamp precision * hls muxer: add offset between pts and dts * hls muxer: close muxer in case of error * hls muxer: stop logging requests with the info level * hls muxer: rename entry into sample * hls muxer: compensate video dts error over time * hls muxer: change default segment count * hls muxer: add starting gap * hls muxer: set default part duration to 200ms * hls muxer: fix audio-only streams on ios * hls muxer: add playsinline attribute to video tag of default web page * hls muxer: keep mpegts as the default hls variant * hls muxer: implement encryption * hls muxer: rewrite dts estimation * hls muxer: improve DTS precision * hls muxer: use right SPS/PPS for each sample * hls muxer: adjust part duration dynamically * add comments * update readme * hls muxer: fix memory leak * hls muxer: decrease ram consumption
2022-05-31 17:17:26 +00:00
m, err := NewMuxer(MuxerVariantMPEGTS, 3, 1*time.Second, 0, 50*1024*1024, videoTrack, nil)
require.NoError(t, err)
// group with IDR
err = m.WriteH264(testTime, 2*time.Second, [][]byte{
2022-06-02 17:03:08 +00:00
testSPS, // SPS
{8}, // PPS
{5}, // IDR
})
require.NoError(t, err)
m.Close()
Implement Low-Latency HLS (#938) * add hlsVariant parameter * hls: split muxer into variants * hls: implement fmp4 segments * hls muxer: implement low latency mode * hls muxer: support audio with fmp4 mode * hls muxer: rewrite file router * hls muxer: implement preload hint * hls muxer: add various error codes * hls muxer: use explicit flags * hls muxer: fix error in aac pts * hls muxer: fix sudden freezes with video+audio * hls muxer: skip empty parts * hls muxer: fix video FPS * hls muxer: add parameter hlsPartDuration * hls muxer: refactor fmp4 muxer * hls muxer: fix CAN-SKIP-UNTIL * hls muxer: refactor code * hls muxer: show only parts of last 2 segments * hls muxer: implementa playlist delta updates * hls muxer: change playlist content type * hls muxer: improve video dts precision * hls muxer: fix video sample flags * hls muxer: improve iphone audio support * hls muxer: improve mp4 timestamp precision * hls muxer: add offset between pts and dts * hls muxer: close muxer in case of error * hls muxer: stop logging requests with the info level * hls muxer: rename entry into sample * hls muxer: compensate video dts error over time * hls muxer: change default segment count * hls muxer: add starting gap * hls muxer: set default part duration to 200ms * hls muxer: fix audio-only streams on ios * hls muxer: add playsinline attribute to video tag of default web page * hls muxer: keep mpegts as the default hls variant * hls muxer: implement encryption * hls muxer: rewrite dts estimation * hls muxer: improve DTS precision * hls muxer: use right SPS/PPS for each sample * hls muxer: adjust part duration dynamically * add comments * update readme * hls muxer: fix memory leak * hls muxer: decrease ram consumption
2022-05-31 17:17:26 +00:00
b := m.File("stream.m3u8", "", "", "").Body
require.Equal(t, nil, b)
}
func TestMuxerMaxSegmentSize(t *testing.T) {
videoTrack := &format.H264{
PayloadTyp: 96,
2022-11-15 22:46:40 +00:00
SPS: testSPS,
PPS: []byte{0x08},
PacketizationMode: 1,
2022-06-23 11:54:48 +00:00
}
Implement Low-Latency HLS (#938) * add hlsVariant parameter * hls: split muxer into variants * hls: implement fmp4 segments * hls muxer: implement low latency mode * hls muxer: support audio with fmp4 mode * hls muxer: rewrite file router * hls muxer: implement preload hint * hls muxer: add various error codes * hls muxer: use explicit flags * hls muxer: fix error in aac pts * hls muxer: fix sudden freezes with video+audio * hls muxer: skip empty parts * hls muxer: fix video FPS * hls muxer: add parameter hlsPartDuration * hls muxer: refactor fmp4 muxer * hls muxer: fix CAN-SKIP-UNTIL * hls muxer: refactor code * hls muxer: show only parts of last 2 segments * hls muxer: implementa playlist delta updates * hls muxer: change playlist content type * hls muxer: improve video dts precision * hls muxer: fix video sample flags * hls muxer: improve iphone audio support * hls muxer: improve mp4 timestamp precision * hls muxer: add offset between pts and dts * hls muxer: close muxer in case of error * hls muxer: stop logging requests with the info level * hls muxer: rename entry into sample * hls muxer: compensate video dts error over time * hls muxer: change default segment count * hls muxer: add starting gap * hls muxer: set default part duration to 200ms * hls muxer: fix audio-only streams on ios * hls muxer: add playsinline attribute to video tag of default web page * hls muxer: keep mpegts as the default hls variant * hls muxer: implement encryption * hls muxer: rewrite dts estimation * hls muxer: improve DTS precision * hls muxer: use right SPS/PPS for each sample * hls muxer: adjust part duration dynamically * add comments * update readme * hls muxer: fix memory leak * hls muxer: decrease ram consumption
2022-05-31 17:17:26 +00:00
m, err := NewMuxer(MuxerVariantMPEGTS, 3, 1*time.Second, 0, 0, videoTrack, nil)
require.NoError(t, err)
defer m.Close()
err = m.WriteH264(testTime, 2*time.Second, [][]byte{
2022-06-02 17:03:08 +00:00
testSPS,
{5}, // IDR
})
require.EqualError(t, err, "reached maximum segment size")
}
func TestMuxerDoubleRead(t *testing.T) {
videoTrack := &format.H264{
PayloadTyp: 96,
2022-11-15 22:46:40 +00:00
SPS: testSPS,
PPS: []byte{0x08},
PacketizationMode: 1,
2022-06-23 11:54:48 +00:00
}
Implement Low-Latency HLS (#938) * add hlsVariant parameter * hls: split muxer into variants * hls: implement fmp4 segments * hls muxer: implement low latency mode * hls muxer: support audio with fmp4 mode * hls muxer: rewrite file router * hls muxer: implement preload hint * hls muxer: add various error codes * hls muxer: use explicit flags * hls muxer: fix error in aac pts * hls muxer: fix sudden freezes with video+audio * hls muxer: skip empty parts * hls muxer: fix video FPS * hls muxer: add parameter hlsPartDuration * hls muxer: refactor fmp4 muxer * hls muxer: fix CAN-SKIP-UNTIL * hls muxer: refactor code * hls muxer: show only parts of last 2 segments * hls muxer: implementa playlist delta updates * hls muxer: change playlist content type * hls muxer: improve video dts precision * hls muxer: fix video sample flags * hls muxer: improve iphone audio support * hls muxer: improve mp4 timestamp precision * hls muxer: add offset between pts and dts * hls muxer: close muxer in case of error * hls muxer: stop logging requests with the info level * hls muxer: rename entry into sample * hls muxer: compensate video dts error over time * hls muxer: change default segment count * hls muxer: add starting gap * hls muxer: set default part duration to 200ms * hls muxer: fix audio-only streams on ios * hls muxer: add playsinline attribute to video tag of default web page * hls muxer: keep mpegts as the default hls variant * hls muxer: implement encryption * hls muxer: rewrite dts estimation * hls muxer: improve DTS precision * hls muxer: use right SPS/PPS for each sample * hls muxer: adjust part duration dynamically * add comments * update readme * hls muxer: fix memory leak * hls muxer: decrease ram consumption
2022-05-31 17:17:26 +00:00
m, err := NewMuxer(MuxerVariantMPEGTS, 3, 1*time.Second, 0, 50*1024*1024, videoTrack, nil)
require.NoError(t, err)
defer m.Close()
err = m.WriteH264(testTime, 0, [][]byte{
2022-06-02 17:03:08 +00:00
testSPS,
{5}, // IDR
{1},
})
require.NoError(t, err)
err = m.WriteH264(testTime, 2*time.Second, [][]byte{
2022-06-02 17:03:08 +00:00
{5}, // IDR
{2},
})
require.NoError(t, err)
2022-09-17 19:19:45 +00:00
byts, err := io.ReadAll(m.File("stream.m3u8", "", "", "").Body)
Implement Low-Latency HLS (#938) * add hlsVariant parameter * hls: split muxer into variants * hls: implement fmp4 segments * hls muxer: implement low latency mode * hls muxer: support audio with fmp4 mode * hls muxer: rewrite file router * hls muxer: implement preload hint * hls muxer: add various error codes * hls muxer: use explicit flags * hls muxer: fix error in aac pts * hls muxer: fix sudden freezes with video+audio * hls muxer: skip empty parts * hls muxer: fix video FPS * hls muxer: add parameter hlsPartDuration * hls muxer: refactor fmp4 muxer * hls muxer: fix CAN-SKIP-UNTIL * hls muxer: refactor code * hls muxer: show only parts of last 2 segments * hls muxer: implementa playlist delta updates * hls muxer: change playlist content type * hls muxer: improve video dts precision * hls muxer: fix video sample flags * hls muxer: improve iphone audio support * hls muxer: improve mp4 timestamp precision * hls muxer: add offset between pts and dts * hls muxer: close muxer in case of error * hls muxer: stop logging requests with the info level * hls muxer: rename entry into sample * hls muxer: compensate video dts error over time * hls muxer: change default segment count * hls muxer: add starting gap * hls muxer: set default part duration to 200ms * hls muxer: fix audio-only streams on ios * hls muxer: add playsinline attribute to video tag of default web page * hls muxer: keep mpegts as the default hls variant * hls muxer: implement encryption * hls muxer: rewrite dts estimation * hls muxer: improve DTS precision * hls muxer: use right SPS/PPS for each sample * hls muxer: adjust part duration dynamically * add comments * update readme * hls muxer: fix memory leak * hls muxer: decrease ram consumption
2022-05-31 17:17:26 +00:00
require.NoError(t, err)
re := regexp.MustCompile(`^#EXTM3U\n` +
`#EXT-X-VERSION:3\n` +
`#EXT-X-ALLOW-CACHE:NO\n` +
`#EXT-X-TARGETDURATION:2\n` +
`#EXT-X-MEDIA-SEQUENCE:0\n` +
`#EXT-X-PROGRAM-DATE-TIME:(.*?)\n` +
`#EXTINF:2,\n` +
`(seg0\.ts)\n$`)
Implement Low-Latency HLS (#938) * add hlsVariant parameter * hls: split muxer into variants * hls: implement fmp4 segments * hls muxer: implement low latency mode * hls muxer: support audio with fmp4 mode * hls muxer: rewrite file router * hls muxer: implement preload hint * hls muxer: add various error codes * hls muxer: use explicit flags * hls muxer: fix error in aac pts * hls muxer: fix sudden freezes with video+audio * hls muxer: skip empty parts * hls muxer: fix video FPS * hls muxer: add parameter hlsPartDuration * hls muxer: refactor fmp4 muxer * hls muxer: fix CAN-SKIP-UNTIL * hls muxer: refactor code * hls muxer: show only parts of last 2 segments * hls muxer: implementa playlist delta updates * hls muxer: change playlist content type * hls muxer: improve video dts precision * hls muxer: fix video sample flags * hls muxer: improve iphone audio support * hls muxer: improve mp4 timestamp precision * hls muxer: add offset between pts and dts * hls muxer: close muxer in case of error * hls muxer: stop logging requests with the info level * hls muxer: rename entry into sample * hls muxer: compensate video dts error over time * hls muxer: change default segment count * hls muxer: add starting gap * hls muxer: set default part duration to 200ms * hls muxer: fix audio-only streams on ios * hls muxer: add playsinline attribute to video tag of default web page * hls muxer: keep mpegts as the default hls variant * hls muxer: implement encryption * hls muxer: rewrite dts estimation * hls muxer: improve DTS precision * hls muxer: use right SPS/PPS for each sample * hls muxer: adjust part duration dynamically * add comments * update readme * hls muxer: fix memory leak * hls muxer: decrease ram consumption
2022-05-31 17:17:26 +00:00
ma := re.FindStringSubmatch(string(byts))
require.NotEqual(t, 0, len(ma))
2022-09-17 19:19:45 +00:00
byts1, err := io.ReadAll(m.File(ma[2], "", "", "").Body)
require.NoError(t, err)
2022-09-17 19:19:45 +00:00
byts2, err := io.ReadAll(m.File(ma[2], "", "", "").Body)
require.NoError(t, err)
require.Equal(t, byts1, byts2)
}