Files
gortsplib/track_test.go
2022-01-30 16:50:47 +01:00

311 lines
7.8 KiB
Go

package gortsplib
import (
"testing"
psdp "github.com/pion/sdp/v3"
"github.com/stretchr/testify/require"
"github.com/aler9/gortsplib/pkg/base"
)
func TestTrackNewFromMediaDescription(t *testing.T) {
for _, ca := range []struct {
name string
md *psdp.MediaDescription
track Track
}{
{
"pcma",
&psdp.MediaDescription{
MediaName: psdp.MediaName{
Media: "audio",
Protos: []string{"RTP", "AVP"},
Formats: []string{"8"},
},
},
&TrackGeneric{
clockRate: 8000,
media: "audio",
formats: []string{"8"},
},
},
{
"aac",
&psdp.MediaDescription{
MediaName: psdp.MediaName{
Media: "audio",
Protos: []string{"RTP", "AVP"},
Formats: []string{"96"},
},
Attributes: []psdp.Attribute{
{
Key: "rtpmap",
Value: "96 mpeg4-generic/48000/2",
},
{
Key: "fmtp",
Value: "96 profile-level-id=1; mode=AAC-hbr; sizelength=13; indexlength=3; indexdeltalength=3; config=1190",
},
},
},
&TrackAAC{
payloadType: 96,
typ: 2,
sampleRate: 48000,
channelCount: 2,
mpegConf: []byte{0x11, 0x90},
},
},
{
"aac uppercase",
&psdp.MediaDescription{
MediaName: psdp.MediaName{
Media: "audio",
Protos: []string{"RTP", "AVP"},
Formats: []string{"96"},
},
Attributes: []psdp.Attribute{
{
Key: "rtpmap",
Value: "96 MPEG4-GENERIC/48000/2",
},
{
Key: "fmtp",
Value: "96 profile-level-id=1; mode=AAC-hbr; sizelength=13; indexlength=3; indexdeltalength=3; config=1190",
},
},
},
&TrackAAC{
payloadType: 96,
typ: 2,
sampleRate: 48000,
channelCount: 2,
mpegConf: []byte{0x11, 0x90},
},
},
{
"opus",
&psdp.MediaDescription{
MediaName: psdp.MediaName{
Media: "audio",
Protos: []string{"RTP", "AVP"},
Formats: []string{"96"},
},
Attributes: []psdp.Attribute{
{
Key: "rtpmap",
Value: "96 opus/48000/2",
},
{
Key: "fmtp",
Value: "96 sprop-stereo=1",
},
},
},
&TrackOpus{
payloadType: 96,
sampleRate: 48000,
channelCount: 2,
},
},
{
"h264",
&psdp.MediaDescription{
MediaName: psdp.MediaName{
Media: "video",
Protos: []string{"RTP", "AVP"},
Formats: []string{"96"},
},
Attributes: []psdp.Attribute{
{
Key: "rtpmap",
Value: "96 H264/90000",
},
{
Key: "fmtp",
Value: "96 packetization-mode=1; " +
"sprop-parameter-sets=Z2QADKw7ULBLQgAAAwACAAADAD0I,aO48gA==; profile-level-id=64000C",
},
},
},
&TrackH264{
payloadType: 96,
sps: []byte{
0x67, 0x64, 0x00, 0x0c, 0xac, 0x3b, 0x50, 0xb0,
0x4b, 0x42, 0x00, 0x00, 0x03, 0x00, 0x02, 0x00,
0x00, 0x03, 0x00, 0x3d, 0x08,
},
pps: []byte{
0x68, 0xee, 0x3c, 0x80,
},
},
},
{
"h264 with a space at the end of rtpmap",
&psdp.MediaDescription{
MediaName: psdp.MediaName{
Media: "video",
Protos: []string{"RTP", "AVP"},
Formats: []string{"96"},
},
Attributes: []psdp.Attribute{
{
Key: "rtpmap",
Value: "96 H264/90000 ",
},
},
},
&TrackH264{
payloadType: 96,
},
},
{
"h265",
&psdp.MediaDescription{
MediaName: psdp.MediaName{
Media: "video",
Protos: []string{"RTP", "AVP"},
Formats: []string{"96"},
},
Attributes: []psdp.Attribute{
{
Key: "rtpmap",
Value: "96 H265/90000",
},
{
Key: "fmtp",
Value: "96 96 sprop-vps=QAEMAf//AWAAAAMAkAAAAwAAAwB4mZgJ; " +
"sprop-sps=QgEBAWAAAAMAkAAAAwAAAwB4oAPAgBDllmZpJMrgEAAAAwAQAAADAeCA; sprop-pps=RAHBcrRiQA==",
},
},
},
&TrackGeneric{
clockRate: 90000,
media: "video",
formats: []string{"96"},
rtpmap: "96 H265/90000",
fmtp: "96 96 sprop-vps=QAEMAf//AWAAAAMAkAAAAwAAAwB4mZgJ; " +
"sprop-sps=QgEBAWAAAAMAkAAAAwAAAwB4oAPAgBDllmZpJMrgEAAAAwAQAAADAeCA; sprop-pps=RAHBcrRiQA==",
},
},
} {
t.Run(ca.name, func(t *testing.T) {
track, err := newTrackFromMediaDescription(ca.md)
require.NoError(t, err)
require.Equal(t, ca.track, track)
})
}
}
func TestTrackURL(t *testing.T) {
for _, ca := range []struct {
name string
sdp []byte
baseURL *base.URL
ur *base.URL
}{
{
"missing control",
[]byte("v=0\r\n" +
"m=video 0 RTP/AVP 96\r\n" +
"a=rtpmap:96 H264/90000\r\n"),
mustParseURL("rtsp://myuser:mypass@192.168.1.99:554/path/"),
mustParseURL("rtsp://myuser:mypass@192.168.1.99:554/path/"),
},
{
"absolute control",
[]byte("v=0\r\n" +
"m=video 0 RTP/AVP 96\r\n" +
"a=rtpmap:96 H264/90000\r\n" +
"a=control:rtsp://localhost/path/trackID=7"),
mustParseURL("rtsp://myuser:mypass@192.168.1.99:554/path/"),
mustParseURL("rtsp://myuser:mypass@192.168.1.99:554/path/trackID=7"),
},
{
"relative control",
[]byte("v=0\r\n" +
"m=video 0 RTP/AVP 96\r\n" +
"a=rtpmap:96 H264/90000\r\n" +
"a=control:trackID=5"),
mustParseURL("rtsp://myuser:mypass@192.168.1.99:554/path/"),
mustParseURL("rtsp://myuser:mypass@192.168.1.99:554/path/trackID=5"),
},
{
"relative control, subpath",
[]byte("v=0\r\n" +
"m=video 0 RTP/AVP 96\r\n" +
"a=rtpmap:96 H264/90000\r\n" +
"a=control:trackID=5"),
mustParseURL("rtsp://myuser:mypass@192.168.1.99:554/sub/path/"),
mustParseURL("rtsp://myuser:mypass@192.168.1.99:554/sub/path/trackID=5"),
},
{
"relative control, url without slash",
[]byte("v=0\r\n" +
"m=video 0 RTP/AVP 96\r\n" +
"a=rtpmap:96 H264/90000\r\n" +
"a=control:trackID=5"),
mustParseURL("rtsp://myuser:mypass@192.168.1.99:554/sub/path"),
mustParseURL("rtsp://myuser:mypass@192.168.1.99:554/sub/path/trackID=5"),
},
{
"relative control, url with query",
[]byte("v=0\r\n" +
"m=video 0 RTP/AVP 96\r\n" +
"a=rtpmap:96 H264/90000\r\n" +
"a=control:trackID=5"),
mustParseURL("rtsp://myuser:mypass@192.168.1.99:554/" +
"test?user=tmp&password=BagRep1&channel=1&stream=0.sdp"),
mustParseURL("rtsp://myuser:mypass@192.168.1.99:554/" +
"test?user=tmp&password=BagRep1&channel=1&stream=0.sdp/trackID=5"),
},
{
"relative control, url with special chars and query",
[]byte("v=0\r\n" +
"m=video 0 RTP/AVP 96\r\n" +
"a=rtpmap:96 H264/90000\r\n" +
"a=control:trackID=5"),
mustParseURL("rtsp://myuser:mypass@192.168.1.99:554/" +
"te!st?user=tmp&password=BagRep1!&channel=1&stream=0.sdp"),
mustParseURL("rtsp://myuser:mypass@192.168.1.99:554/" +
"te!st?user=tmp&password=BagRep1!&channel=1&stream=0.sdp/trackID=5"),
},
{
"relative control, url with query without question mark",
[]byte("v=0\r\n" +
"m=video 0 RTP/AVP 96\r\n" +
"a=rtpmap:96 H264/90000\r\n" +
"a=control:trackID=5"),
mustParseURL("rtsp://myuser:mypass@192.168.1.99:554/user=tmp&password=BagRep1!&channel=1&stream=0.sdp"),
mustParseURL("rtsp://myuser:mypass@192.168.1.99:554/user=tmp&password=BagRep1!&channel=1&stream=0.sdp/trackID=5"),
},
{
"relative control, control is query",
[]byte("v=0\r\n" +
"m=video 0 RTP/AVP 96\r\n" +
"a=rtpmap:96 H264/90000\r\n" +
"a=control:?ctype=video"),
mustParseURL("rtsp://192.168.1.99:554/test"),
mustParseURL("rtsp://192.168.1.99:554/test?ctype=video"),
},
{
"relative control, control is query and no path",
[]byte("v=0\r\n" +
"m=video 0 RTP/AVP 96\r\n" +
"a=rtpmap:96 H264/90000\r\n" +
"a=control:?ctype=video"),
mustParseURL("rtsp://192.168.1.99:554/"),
mustParseURL("rtsp://192.168.1.99:554/?ctype=video"),
},
} {
t.Run(ca.name, func(t *testing.T) {
tracks, err := ReadTracks(ca.sdp)
require.NoError(t, err)
ur, err := tracks[0].url(ca.baseURL)
require.NoError(t, err)
require.Equal(t, ca.ur, ur)
})
}
}