2023-01-05 11:54:00 +00:00
|
|
|
package formatprocessor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
2023-05-18 16:00:53 +00:00
|
|
|
"time"
|
2023-01-05 11:54:00 +00:00
|
|
|
|
2023-04-01 16:39:12 +00:00
|
|
|
"github.com/bluenviron/gortsplib/v3/pkg/formats"
|
|
|
|
"github.com/bluenviron/mediacommon/pkg/codecs/h265"
|
2023-01-05 11:54:00 +00:00
|
|
|
"github.com/pion/rtp"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestH265DynamicParams(t *testing.T) {
|
2023-04-01 16:39:12 +00:00
|
|
|
forma := &formats.H265{
|
2023-01-05 11:54:00 +00:00
|
|
|
PayloadTyp: 96,
|
|
|
|
}
|
|
|
|
|
2023-05-04 18:16:41 +00:00
|
|
|
p, err := New(1472, forma, false, nil)
|
2023-01-05 11:54:00 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-06-01 22:38:27 +00:00
|
|
|
enc, err := forma.CreateEncoder2()
|
|
|
|
require.NoError(t, err)
|
2023-01-05 11:54:00 +00:00
|
|
|
|
2023-01-05 12:10:28 +00:00
|
|
|
pkts, err := enc.Encode([][]byte{{byte(h265.NALUType_CRA_NUT) << 1, 0}}, 0)
|
|
|
|
require.NoError(t, err)
|
2023-06-01 22:38:27 +00:00
|
|
|
|
2023-03-10 11:44:59 +00:00
|
|
|
data := &UnitH265{RTPPackets: []*rtp.Packet{pkts[0]}}
|
2023-01-05 12:10:28 +00:00
|
|
|
p.Process(data, true)
|
|
|
|
|
|
|
|
require.Equal(t, [][]byte{
|
|
|
|
{byte(h265.NALUType_CRA_NUT) << 1, 0},
|
|
|
|
}, data.AU)
|
|
|
|
|
|
|
|
pkts, err = enc.Encode([][]byte{{byte(h265.NALUType_VPS_NUT) << 1, 1, 2, 3}}, 0)
|
2023-01-05 11:54:00 +00:00
|
|
|
require.NoError(t, err)
|
2023-03-10 11:44:59 +00:00
|
|
|
p.Process(&UnitH265{RTPPackets: []*rtp.Packet{pkts[0]}}, false)
|
2023-01-05 11:54:00 +00:00
|
|
|
|
|
|
|
pkts, err = enc.Encode([][]byte{{byte(h265.NALUType_SPS_NUT) << 1, 4, 5, 6}}, 0)
|
|
|
|
require.NoError(t, err)
|
2023-03-10 11:44:59 +00:00
|
|
|
p.Process(&UnitH265{RTPPackets: []*rtp.Packet{pkts[0]}}, false)
|
2023-01-05 11:54:00 +00:00
|
|
|
|
|
|
|
pkts, err = enc.Encode([][]byte{{byte(h265.NALUType_PPS_NUT) << 1, 7, 8, 9}}, 0)
|
|
|
|
require.NoError(t, err)
|
2023-03-10 11:44:59 +00:00
|
|
|
p.Process(&UnitH265{RTPPackets: []*rtp.Packet{pkts[0]}}, false)
|
2023-01-05 11:54:00 +00:00
|
|
|
|
|
|
|
require.Equal(t, []byte{byte(h265.NALUType_VPS_NUT) << 1, 1, 2, 3}, forma.VPS)
|
|
|
|
require.Equal(t, []byte{byte(h265.NALUType_SPS_NUT) << 1, 4, 5, 6}, forma.SPS)
|
|
|
|
require.Equal(t, []byte{byte(h265.NALUType_PPS_NUT) << 1, 7, 8, 9}, forma.PPS)
|
2023-01-05 12:10:28 +00:00
|
|
|
|
|
|
|
pkts, err = enc.Encode([][]byte{{byte(h265.NALUType_CRA_NUT) << 1, 0}}, 0)
|
|
|
|
require.NoError(t, err)
|
2023-03-10 11:44:59 +00:00
|
|
|
data = &UnitH265{RTPPackets: []*rtp.Packet{pkts[0]}}
|
2023-01-05 12:10:28 +00:00
|
|
|
p.Process(data, true)
|
|
|
|
|
|
|
|
require.Equal(t, [][]byte{
|
|
|
|
{byte(h265.NALUType_VPS_NUT) << 1, 1, 2, 3},
|
|
|
|
{byte(h265.NALUType_SPS_NUT) << 1, 4, 5, 6},
|
|
|
|
{byte(h265.NALUType_PPS_NUT) << 1, 7, 8, 9},
|
|
|
|
{byte(h265.NALUType_CRA_NUT) << 1, 0},
|
|
|
|
}, data.AU)
|
2023-01-05 11:54:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestH265OversizedPackets(t *testing.T) {
|
2023-04-01 16:39:12 +00:00
|
|
|
forma := &formats.H265{
|
2023-01-05 11:54:00 +00:00
|
|
|
PayloadTyp: 96,
|
|
|
|
VPS: []byte{byte(h265.NALUType_VPS_NUT) << 1, 10, 11, 12},
|
|
|
|
SPS: []byte{byte(h265.NALUType_SPS_NUT) << 1, 13, 14, 15},
|
|
|
|
PPS: []byte{byte(h265.NALUType_PPS_NUT) << 1, 16, 17, 18},
|
|
|
|
}
|
|
|
|
|
2023-05-04 18:16:41 +00:00
|
|
|
p, err := New(1472, forma, false, nil)
|
2023-01-05 11:54:00 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var out []*rtp.Packet
|
|
|
|
|
|
|
|
for _, pkt := range []*rtp.Packet{
|
|
|
|
{
|
|
|
|
Header: rtp.Header{
|
|
|
|
Version: 2,
|
|
|
|
Marker: true,
|
|
|
|
PayloadType: 96,
|
|
|
|
SequenceNumber: 123,
|
|
|
|
Timestamp: 45343,
|
|
|
|
SSRC: 563423,
|
|
|
|
Padding: true,
|
|
|
|
},
|
|
|
|
Payload: []byte{0x01, 0x02, 0x03, 0x04},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Header: rtp.Header{
|
|
|
|
Version: 2,
|
|
|
|
Marker: true,
|
|
|
|
PayloadType: 96,
|
|
|
|
SequenceNumber: 124,
|
|
|
|
Timestamp: 45343,
|
|
|
|
SSRC: 563423,
|
|
|
|
Padding: true,
|
|
|
|
},
|
|
|
|
Payload: bytes.Repeat([]byte{0x01, 0x02, 0x03, 0x04}, 2000/4),
|
|
|
|
},
|
|
|
|
} {
|
2023-03-10 11:44:59 +00:00
|
|
|
data := &UnitH265{RTPPackets: []*rtp.Packet{pkt}}
|
2023-01-05 11:54:00 +00:00
|
|
|
p.Process(data, false)
|
|
|
|
out = append(out, data.RTPPackets...)
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, []*rtp.Packet{
|
|
|
|
{
|
|
|
|
Header: rtp.Header{
|
|
|
|
Version: 2,
|
|
|
|
Marker: true,
|
|
|
|
PayloadType: 96,
|
|
|
|
SequenceNumber: 123,
|
|
|
|
Timestamp: 45343,
|
|
|
|
SSRC: 563423,
|
|
|
|
},
|
|
|
|
Payload: []byte{0x01, 0x02, 0x03, 0x04},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Header: rtp.Header{
|
|
|
|
Version: 2,
|
|
|
|
Marker: false,
|
|
|
|
PayloadType: 96,
|
|
|
|
SequenceNumber: 124,
|
|
|
|
Timestamp: 45343,
|
|
|
|
SSRC: 563423,
|
|
|
|
},
|
|
|
|
Payload: append(
|
|
|
|
append([]byte{0x63, 0x02, 0x80, 0x03, 0x04}, bytes.Repeat([]byte{0x01, 0x02, 0x03, 0x04}, 363)...),
|
|
|
|
[]byte{0x01, 0x02, 0x03}...,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Header: rtp.Header{
|
|
|
|
Version: 2,
|
|
|
|
Marker: true,
|
|
|
|
PayloadType: 96,
|
|
|
|
SequenceNumber: 125,
|
|
|
|
Timestamp: 45343,
|
|
|
|
SSRC: 563423,
|
|
|
|
},
|
|
|
|
Payload: append(
|
|
|
|
[]byte{0x63, 0x02, 0x40, 0x04},
|
|
|
|
bytes.Repeat([]byte{0x01, 0x02, 0x03, 0x04}, 135)...,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
}, out)
|
|
|
|
}
|
2023-02-23 13:46:26 +00:00
|
|
|
|
|
|
|
func TestH265EmptyPacket(t *testing.T) {
|
2023-04-01 16:39:12 +00:00
|
|
|
forma := &formats.H265{
|
2023-02-23 13:46:26 +00:00
|
|
|
PayloadTyp: 96,
|
|
|
|
}
|
|
|
|
|
2023-05-04 18:16:41 +00:00
|
|
|
p, err := New(1472, forma, true, nil)
|
2023-02-23 13:46:26 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-03-10 11:44:59 +00:00
|
|
|
unit := &UnitH265{
|
2023-02-23 13:46:26 +00:00
|
|
|
AU: [][]byte{
|
|
|
|
{byte(h265.NALUType_VPS_NUT) << 1, 10, 11, 12}, // VPS
|
|
|
|
{byte(h265.NALUType_SPS_NUT) << 1, 13, 14, 15}, // SPS
|
|
|
|
{byte(h265.NALUType_PPS_NUT) << 1, 16, 17, 18}, // PPS
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
p.Process(unit, false)
|
|
|
|
|
|
|
|
// if all NALUs have been removed, no RTP packets must be generated.
|
|
|
|
require.Equal(t, []*rtp.Packet(nil), unit.RTPPackets)
|
|
|
|
}
|
2023-05-18 16:00:53 +00:00
|
|
|
|
|
|
|
func TestH265KeyFrameWarning(t *testing.T) {
|
|
|
|
forma := &formats.H265{
|
|
|
|
PayloadTyp: 96,
|
|
|
|
}
|
|
|
|
|
|
|
|
w := &testLogWriter{recv: make(chan string, 1)}
|
|
|
|
p, err := New(1472, forma, true, w)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ntp := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
|
|
|
|
err = p.Process(&UnitH265{
|
|
|
|
AU: [][]byte{
|
|
|
|
{0x01},
|
|
|
|
},
|
|
|
|
NTP: ntp,
|
|
|
|
}, false)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ntp = ntp.Add(30 * time.Second)
|
|
|
|
err = p.Process(&UnitH265{
|
|
|
|
AU: [][]byte{
|
|
|
|
{0x01},
|
|
|
|
},
|
|
|
|
NTP: ntp,
|
|
|
|
}, false)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
logl := <-w.recv
|
|
|
|
require.Equal(t, "no H265 key frames received in 10s, stream can't be decoded", logl)
|
|
|
|
}
|