mediamtx/internal/core/rtsp_server_test.go

917 lines
22 KiB
Go
Raw Normal View History

package core
2021-03-10 21:01:10 +00:00
import (
"bufio"
"bytes"
"fmt"
"net"
"os"
"path/filepath"
"testing"
"time"
"github.com/aler9/gortsplib"
"github.com/aler9/gortsplib/pkg/base"
"github.com/aler9/gortsplib/pkg/headers"
"github.com/stretchr/testify/require"
)
2021-05-29 17:12:26 +00:00
func mustParseURL(s string) *base.URL {
u, err := base.ParseURL(s)
if err != nil {
panic(err)
}
return u
}
func TestRTSPServerPublishRead(t *testing.T) {
2021-03-10 21:01:10 +00:00
for _, ca := range []struct {
publisherSoft string
publisherProto string
readerSoft string
readerProto string
}{
{"ffmpeg", "udp", "ffmpeg", "udp"},
{"ffmpeg", "udp", "ffmpeg", "multicast"},
{"ffmpeg", "udp", "ffmpeg", "tcp"},
{"ffmpeg", "udp", "gstreamer", "udp"},
{"ffmpeg", "udp", "gstreamer", "multicast"},
{"ffmpeg", "udp", "gstreamer", "tcp"},
{"ffmpeg", "udp", "vlc", "udp"},
{"ffmpeg", "udp", "vlc", "tcp"},
{"ffmpeg", "tcp", "ffmpeg", "udp"},
{"gstreamer", "udp", "ffmpeg", "udp"},
{"gstreamer", "tcp", "ffmpeg", "udp"},
{"ffmpeg", "tls", "ffmpeg", "tls"},
{"ffmpeg", "tls", "gstreamer", "tls"},
{"gstreamer", "tls", "ffmpeg", "tls"},
2021-03-10 21:01:10 +00:00
} {
t.Run(ca.publisherSoft+"_"+ca.publisherProto+"_"+
2021-03-10 21:01:10 +00:00
ca.readerSoft+"_"+ca.readerProto, func(t *testing.T) {
var proto string
var port string
if ca.publisherProto != "tls" {
2021-03-10 21:01:10 +00:00
proto = "rtsp"
port = "8554"
p, ok := newInstance("rtmpDisable: yes\n" +
2021-04-11 17:05:08 +00:00
"hlsDisable: yes\n" +
2021-03-10 21:01:10 +00:00
"readTimeout: 20s\n")
require.Equal(t, true, ok)
defer p.close()
} else {
proto = "rtsps"
port = "8555"
serverCertFpath, err := writeTempFile(serverCert)
require.NoError(t, err)
defer os.Remove(serverCertFpath)
serverKeyFpath, err := writeTempFile(serverKey)
require.NoError(t, err)
defer os.Remove(serverKeyFpath)
p, ok := newInstance("rtmpDisable: yes\n" +
2021-04-11 17:05:08 +00:00
"hlsDisable: yes\n" +
2021-03-10 21:01:10 +00:00
"readTimeout: 20s\n" +
"protocols: [tcp]\n" +
"encryption: yes\n" +
"serverCert: " + serverCertFpath + "\n" +
"serverKey: " + serverKeyFpath + "\n")
require.Equal(t, true, ok)
defer p.close()
}
switch ca.publisherSoft {
case "ffmpeg":
ps := func() string {
switch ca.publisherProto {
case "udp", "tcp":
return ca.publisherProto
default: // tls
return "tcp"
}
}()
2021-03-10 21:01:10 +00:00
cnt1, err := newContainer("ffmpeg", "source", []string{
"-re",
"-stream_loop", "-1",
2021-03-22 18:51:25 +00:00
"-i", "emptyvideo.mkv",
2021-03-10 21:01:10 +00:00
"-c", "copy",
"-f", "rtsp",
"-rtsp_transport",
ps,
proto + "://localhost:" + port + "/teststream",
2021-03-10 21:01:10 +00:00
})
require.NoError(t, err)
defer cnt1.close()
time.Sleep(1 * time.Second)
case "gstreamer":
ps := func() string {
switch ca.publisherProto {
case "udp", "tcp":
return ca.publisherProto
default: // tls
return "tcp"
}
}()
2021-03-10 21:01:10 +00:00
cnt1, err := newContainer("gstreamer", "source", []string{
2021-03-22 18:51:25 +00:00
"filesrc location=emptyvideo.mkv ! matroskademux ! video/x-h264 ! rtspclientsink " +
"location=" + proto + "://localhost:" + port + "/teststream " +
"protocols=" + ps + " tls-validation-flags=0 latency=0 timeout=0 rtx-time=0",
2021-03-10 21:01:10 +00:00
})
require.NoError(t, err)
defer cnt1.close()
time.Sleep(1 * time.Second)
}
time.Sleep(1 * time.Second)
switch ca.readerSoft {
case "ffmpeg":
ps := func() string {
switch ca.readerProto {
case "udp", "tcp":
return ca.publisherProto
case "multicast":
return "udp_multicast"
default: // tls
return "tcp"
}
}()
2021-03-10 21:01:10 +00:00
cnt2, err := newContainer("ffmpeg", "dest", []string{
"-rtsp_transport", ps,
"-i", proto + "://localhost:" + port + "/teststream",
2021-03-10 21:01:10 +00:00
"-vframes", "1",
"-f", "image2",
"-y", "/dev/null",
})
require.NoError(t, err)
defer cnt2.close()
require.Equal(t, 0, cnt2.wait())
case "gstreamer":
ps := func() string {
switch ca.readerProto {
case "udp", "tcp":
return ca.publisherProto
case "multicast":
return "udp-mcast"
default: // tls
return "tcp"
}
}()
2021-03-10 21:01:10 +00:00
cnt2, err := newContainer("gstreamer", "read", []string{
"rtspsrc location=" + proto + "://127.0.0.1:" + port + "/teststream protocols=" + ps + " tls-validation-flags=0 latency=0 " +
2021-03-10 21:01:10 +00:00
"! application/x-rtp,media=video ! decodebin ! exitafterframe ! fakesink",
})
require.NoError(t, err)
defer cnt2.close()
require.Equal(t, 0, cnt2.wait())
case "vlc":
args := []string{}
if ca.readerProto == "tcp" {
args = append(args, "--rtsp-tcp")
}
args = append(args, proto+"://localhost:"+port+"/teststream")
2021-03-10 21:01:10 +00:00
cnt2, err := newContainer("vlc", "dest", args)
require.NoError(t, err)
defer cnt2.close()
require.Equal(t, 0, cnt2.wait())
}
})
}
}
func TestRTSPServerAuth(t *testing.T) {
2021-03-10 21:01:10 +00:00
t.Run("publish", func(t *testing.T) {
p, ok := newInstance("rtmpDisable: yes\n" +
2021-04-11 17:05:08 +00:00
"hlsDisable: yes\n" +
2021-03-10 21:01:10 +00:00
"paths:\n" +
" all:\n" +
" publishUser: testuser\n" +
" publishPass: test!$()*+.;<=>[]^_-{}\n" +
" publishIPs: [127.0.0.0/16]\n")
2021-03-10 21:01:10 +00:00
require.Equal(t, true, ok)
defer p.close()
cnt1, err := newContainer("ffmpeg", "source", []string{
"-re",
"-stream_loop", "-1",
2021-03-22 18:51:25 +00:00
"-i", "emptyvideo.mkv",
2021-03-10 21:01:10 +00:00
"-c", "copy",
"-f", "rtsp",
"-rtsp_transport", "udp",
"rtsp://testuser:test!$()*+.;<=>[]^_-{}@127.0.0.1:8554/test/stream",
2021-03-10 21:01:10 +00:00
})
require.NoError(t, err)
defer cnt1.close()
time.Sleep(1 * time.Second)
cnt2, err := newContainer("ffmpeg", "dest", []string{
"-rtsp_transport", "udp",
"-i", "rtsp://localhost:8554/test/stream",
2021-03-10 21:01:10 +00:00
"-vframes", "1",
"-f", "image2",
"-y", "/dev/null",
})
require.NoError(t, err)
defer cnt2.close()
require.Equal(t, 0, cnt2.wait())
})
for _, soft := range []string{
"ffmpeg",
"vlc",
} {
t.Run("read_"+soft, func(t *testing.T) {
p, ok := newInstance("rtmpDisable: yes\n" +
2021-04-11 17:05:08 +00:00
"hlsDisable: yes\n" +
2021-03-10 21:01:10 +00:00
"paths:\n" +
" all:\n" +
" readUser: testuser\n" +
" readPass: test!$()*+.;<=>[]^_-{}\n" +
" readIPs: [127.0.0.0/16]\n")
2021-03-10 21:01:10 +00:00
require.Equal(t, true, ok)
defer p.close()
cnt1, err := newContainer("ffmpeg", "source", []string{
"-re",
"-stream_loop", "-1",
2021-03-22 18:51:25 +00:00
"-i", "emptyvideo.mkv",
2021-03-10 21:01:10 +00:00
"-c", "copy",
"-f", "rtsp",
"-rtsp_transport", "udp",
"rtsp://localhost:8554/test/stream",
2021-03-10 21:01:10 +00:00
})
require.NoError(t, err)
defer cnt1.close()
time.Sleep(1 * time.Second)
if soft == "ffmpeg" {
cnt2, err := newContainer("ffmpeg", "dest", []string{
"-rtsp_transport", "udp",
"-i", "rtsp://testuser:test!$()*+.;<=>[]^_-{}@127.0.0.1:8554/test/stream",
2021-03-10 21:01:10 +00:00
"-vframes", "1",
"-f", "image2",
"-y", "/dev/null",
})
require.NoError(t, err)
defer cnt2.close()
require.Equal(t, 0, cnt2.wait())
} else {
cnt2, err := newContainer("vlc", "dest", []string{
"rtsp://testuser:test!$()*+.;<=>[]^_-{}@localhost:8554/test/stream",
2021-03-10 21:01:10 +00:00
})
require.NoError(t, err)
defer cnt2.close()
require.Equal(t, 0, cnt2.wait())
}
})
}
t.Run("hashed", func(t *testing.T) {
p, ok := newInstance("rtmpDisable: yes\n" +
2021-04-11 17:05:08 +00:00
"hlsDisable: yes\n" +
2021-03-10 21:01:10 +00:00
"paths:\n" +
" all:\n" +
" readUser: sha256:rl3rgi4NcZkpAEcacZnQ2VuOfJ0FxAqCRaKB/SwdZoQ=\n" +
" readPass: sha256:E9JJ8stBJ7QM+nV4ZoUCeHk/gU3tPFh/5YieiJp6n2w=\n")
require.Equal(t, true, ok)
defer p.close()
cnt1, err := newContainer("ffmpeg", "source", []string{
"-re",
"-stream_loop", "-1",
2021-03-22 18:51:25 +00:00
"-i", "emptyvideo.mkv",
2021-03-10 21:01:10 +00:00
"-c", "copy",
"-f", "rtsp",
"-rtsp_transport", "udp",
"rtsp://localhost:8554/test/stream",
2021-03-10 21:01:10 +00:00
})
require.NoError(t, err)
defer cnt1.close()
cnt2, err := newContainer("ffmpeg", "dest", []string{
"-rtsp_transport", "udp",
"-i", "rtsp://testuser:testpass@localhost:8554/test/stream",
2021-03-10 21:01:10 +00:00
"-vframes", "1",
"-f", "image2",
"-y", "/dev/null",
})
require.NoError(t, err)
defer cnt2.close()
require.Equal(t, 0, cnt2.wait())
})
}
func TestRTSPServerAuthFail(t *testing.T) {
2021-03-10 21:01:10 +00:00
for _, ca := range []struct {
name string
user string
pass string
}{
{
2021-05-08 15:22:40 +00:00
"wronguser",
2021-03-10 21:01:10 +00:00
"test1user",
"testpass",
},
{
2021-05-08 15:22:40 +00:00
"wrongpass",
2021-03-10 21:01:10 +00:00
"testuser",
"test1pass",
},
{
2021-05-08 15:22:40 +00:00
"wrongboth",
2021-03-10 21:01:10 +00:00
"test1user",
"test1pass",
},
} {
2021-05-08 15:22:40 +00:00
t.Run("publish_"+ca.name, func(t *testing.T) {
p, ok := newInstance("rtmpDisable: yes\n" +
2021-04-11 17:05:08 +00:00
"hlsDisable: yes\n" +
"paths:\n" +
" all:\n" +
" publishUser: testuser\n" +
" publishPass: testpass\n")
2021-03-10 21:01:10 +00:00
require.Equal(t, true, ok)
defer p.close()
2021-08-25 17:51:59 +00:00
track, err := gortsplib.NewTrackH264(96, &gortsplib.TrackConfigH264{SPS: []byte{0x01, 0x02, 0x03, 0x04}, PPS: []byte{0x01, 0x02, 0x03, 0x04}})
2021-03-10 21:01:10 +00:00
require.NoError(t, err)
2021-05-08 15:22:40 +00:00
_, err = gortsplib.DialPublish(
"rtsp://"+ca.user+":"+ca.pass+"@localhost:8554/test/stream",
2021-05-08 15:22:40 +00:00
gortsplib.Tracks{track},
)
require.Equal(t, "invalid status code: 401 (Unauthorized)", err.Error())
2021-03-10 21:01:10 +00:00
})
}
for _, ca := range []struct {
name string
user string
pass string
}{
{
2021-05-08 15:22:40 +00:00
"wronguser",
2021-03-10 21:01:10 +00:00
"test1user",
"testpass",
},
{
2021-05-08 15:22:40 +00:00
"wrongpass",
2021-03-10 21:01:10 +00:00
"testuser",
"test1pass",
},
{
2021-05-08 15:22:40 +00:00
"wrongboth",
2021-03-10 21:01:10 +00:00
"test1user",
"test1pass",
},
} {
2021-05-08 15:22:40 +00:00
t.Run("read_"+ca.name, func(t *testing.T) {
p, ok := newInstance("rtmpDisable: yes\n" +
2021-04-11 17:05:08 +00:00
"hlsDisable: yes\n" +
"paths:\n" +
" all:\n" +
" readUser: testuser\n" +
" readPass: testpass\n")
2021-03-10 21:01:10 +00:00
require.Equal(t, true, ok)
defer p.close()
2021-05-08 15:22:40 +00:00
_, err := gortsplib.DialRead(
"rtsp://" + ca.user + ":" + ca.pass + "@localhost:8554/test/stream",
2021-05-08 15:22:40 +00:00
)
require.Equal(t, "invalid status code: 401 (Unauthorized)", err.Error())
2021-03-10 21:01:10 +00:00
})
}
2021-05-08 15:22:40 +00:00
t.Run("ip", func(t *testing.T) {
p, ok := newInstance("rtmpDisable: yes\n" +
2021-05-08 15:22:40 +00:00
"hlsDisable: yes\n" +
"paths:\n" +
" all:\n" +
" publishIPs: [128.0.0.1/32]\n")
2021-05-08 15:22:40 +00:00
require.Equal(t, true, ok)
defer p.close()
2021-08-25 17:51:59 +00:00
track, err := gortsplib.NewTrackH264(96, &gortsplib.TrackConfigH264{SPS: []byte{0x01, 0x02, 0x03, 0x04}, PPS: []byte{0x01, 0x02, 0x03, 0x04}})
2021-05-08 15:22:40 +00:00
require.NoError(t, err)
_, err = gortsplib.DialPublish(
"rtsp://localhost:8554/test/stream",
2021-05-08 15:22:40 +00:00
gortsplib.Tracks{track},
)
require.Equal(t, "invalid status code: 401 (Unauthorized)", err.Error())
2021-03-10 21:01:10 +00:00
})
}
func TestRTSPServerAutomaticProtocol(t *testing.T) {
2021-03-10 21:01:10 +00:00
for _, source := range []string{
"ffmpeg",
} {
t.Run(source, func(t *testing.T) {
p, ok := newInstance("rtmpDisable: yes\n" +
2021-04-11 17:05:08 +00:00
"hlsDisable: yes\n" +
2021-03-10 21:01:10 +00:00
"protocols: [tcp]\n")
require.Equal(t, true, ok)
defer p.close()
2021-03-20 13:14:41 +00:00
if source == "ffmpeg" {
2021-03-10 21:01:10 +00:00
cnt1, err := newContainer("ffmpeg", "source", []string{
"-re",
"-stream_loop", "-1",
2021-03-22 18:51:25 +00:00
"-i", "emptyvideo.mkv",
2021-03-10 21:01:10 +00:00
"-c", "copy",
"-f", "rtsp",
"rtsp://localhost:8554/teststream",
2021-03-10 21:01:10 +00:00
})
require.NoError(t, err)
defer cnt1.close()
}
cnt2, err := newContainer("ffmpeg", "dest", []string{
"-i", "rtsp://localhost:8554/teststream",
2021-03-10 21:01:10 +00:00
"-vframes", "1",
"-f", "image2",
"-y", "/dev/null",
})
require.NoError(t, err)
defer cnt2.close()
require.Equal(t, 0, cnt2.wait())
})
}
}
func TestRTSPServerPublisherOverride(t *testing.T) {
2021-04-03 16:02:56 +00:00
for _, ca := range []string{
"enabled",
"disabled",
} {
t.Run(ca, func(t *testing.T) {
conf := "rtmpDisable: yes\n" +
"protocols: [tcp]\n"
if ca == "disabled" {
conf += "paths:\n" +
" all:\n" +
" disablePublisherOverride: yes\n"
}
p, ok := newInstance(conf)
2021-04-03 16:02:56 +00:00
require.Equal(t, true, ok)
defer p.close()
2021-08-25 17:51:59 +00:00
track, err := gortsplib.NewTrackH264(96, &gortsplib.TrackConfigH264{SPS: []byte{0x01, 0x02, 0x03, 0x04}, PPS: []byte{0x01, 0x02, 0x03, 0x04}})
2021-04-03 16:02:56 +00:00
require.NoError(t, err)
s1, err := gortsplib.DialPublish("rtsp://localhost:8554/teststream",
2021-04-03 16:02:56 +00:00
gortsplib.Tracks{track})
require.NoError(t, err)
defer s1.Close()
2021-03-10 21:01:10 +00:00
s2, err := gortsplib.DialPublish("rtsp://localhost:8554/teststream",
2021-04-03 16:02:56 +00:00
gortsplib.Tracks{track})
if ca == "enabled" {
require.NoError(t, err)
defer s2.Close()
} else {
require.Error(t, err)
}
d1, err := gortsplib.DialRead("rtsp://localhost:8554/teststream")
2021-04-03 16:02:56 +00:00
require.NoError(t, err)
defer d1.Close()
2021-05-10 21:23:56 +00:00
readDone := make(chan struct{})
frameRecv := make(chan struct{})
go func() {
defer close(readDone)
d1.ReadFrames(func(trackID int, streamType base.StreamType, payload []byte) {
if streamType == gortsplib.StreamTypeRTP {
if ca == "enabled" {
require.Equal(t, []byte{0x05, 0x06, 0x07, 0x08}, payload)
} else {
require.Equal(t, []byte{0x01, 0x02, 0x03, 0x04}, payload)
}
close(frameRecv)
2021-05-10 21:23:56 +00:00
}
})
}()
err = s1.WriteFrame(0, gortsplib.StreamTypeRTP,
2021-04-03 16:02:56 +00:00
[]byte{0x01, 0x02, 0x03, 0x04})
if ca == "enabled" {
require.Error(t, err)
} else {
require.NoError(t, err)
}
2021-03-10 21:01:10 +00:00
2021-04-03 16:02:56 +00:00
if ca == "enabled" {
err = s2.WriteFrame(0, gortsplib.StreamTypeRTP,
2021-04-03 16:02:56 +00:00
[]byte{0x05, 0x06, 0x07, 0x08})
require.NoError(t, err)
}
2021-05-10 21:23:56 +00:00
<-frameRecv
d1.Close()
<-readDone
})
2021-04-03 16:02:56 +00:00
}
2021-03-10 21:01:10 +00:00
}
func TestRTSPServerNonCompliantFrameSize(t *testing.T) {
2021-03-10 21:01:10 +00:00
t.Run("publish", func(t *testing.T) {
p, ok := newInstance("rtmpDisable: yes\n" +
2021-04-11 17:05:08 +00:00
"hlsDisable: yes\n" +
2021-03-10 21:01:10 +00:00
"readBufferSize: 4500\n")
require.Equal(t, true, ok)
defer p.close()
2021-08-25 17:51:59 +00:00
track, err := gortsplib.NewTrackH264(96, &gortsplib.TrackConfigH264{SPS: []byte{0x01, 0x02, 0x03, 0x04}, PPS: []byte{0x01, 0x02, 0x03, 0x04}})
2021-03-10 21:01:10 +00:00
require.NoError(t, err)
2021-05-07 21:07:31 +00:00
client := &gortsplib.Client{
Protocol: func() *gortsplib.ClientProtocol {
v := gortsplib.ClientProtocolTCP
2021-03-10 21:01:10 +00:00
return &v
}(),
2021-04-03 16:02:56 +00:00
ReadBufferSize: 4500,
2021-03-10 21:01:10 +00:00
}
source, err := client.DialPublish("rtsp://localhost:8554/teststream",
2021-03-10 21:01:10 +00:00
gortsplib.Tracks{track})
require.NoError(t, err)
defer source.Close()
dest, err := client.DialRead("rtsp://localhost:8554/teststream")
2021-03-10 21:01:10 +00:00
require.NoError(t, err)
2021-04-03 16:02:56 +00:00
defer dest.Close()
2021-03-10 21:01:10 +00:00
2021-04-03 16:02:56 +00:00
input := bytes.Repeat([]byte{0x01, 0x02, 0x03, 0x04, 0x05}, 4096/5)
2021-03-10 21:01:10 +00:00
2021-05-10 21:23:56 +00:00
readDone := make(chan struct{})
frameRecv := make(chan struct{})
go func() {
defer close(readDone)
dest.ReadFrames(func(trackID int, streamType gortsplib.StreamType, payload []byte) {
require.Equal(t, gortsplib.StreamTypeRTP, streamType)
require.Equal(t, input, payload)
close(frameRecv)
})
}()
2021-04-03 16:02:56 +00:00
err = source.WriteFrame(0, gortsplib.StreamTypeRTP, input)
2021-03-10 21:01:10 +00:00
require.NoError(t, err)
2021-04-03 16:02:56 +00:00
2021-05-10 21:23:56 +00:00
<-frameRecv
dest.Close()
<-readDone
2021-03-10 21:01:10 +00:00
})
t.Run("proxy", func(t *testing.T) {
p1, ok := newInstance("rtmpDisable: yes\n" +
2021-04-11 17:05:08 +00:00
"hlsDisable: yes\n" +
2021-03-10 21:01:10 +00:00
"protocols: [tcp]\n" +
"readBufferSize: 4500\n")
require.Equal(t, true, ok)
defer p1.close()
2021-08-25 17:51:59 +00:00
track, err := gortsplib.NewTrackH264(96, &gortsplib.TrackConfigH264{SPS: []byte{0x01, 0x02, 0x03, 0x04}, PPS: []byte{0x01, 0x02, 0x03, 0x04}})
2021-03-10 21:01:10 +00:00
require.NoError(t, err)
2021-05-07 21:07:31 +00:00
client := &gortsplib.Client{
Protocol: func() *gortsplib.ClientProtocol {
v := gortsplib.ClientProtocolTCP
2021-03-10 21:01:10 +00:00
return &v
}(),
ReadBufferSize: 4500,
}
source, err := client.DialPublish("rtsp://localhost:8554/teststream",
2021-03-10 21:01:10 +00:00
gortsplib.Tracks{track})
require.NoError(t, err)
defer source.Close()
p2, ok := newInstance("rtmpDisable: yes\n" +
2021-04-11 17:05:08 +00:00
"hlsDisable: yes\n" +
2021-03-10 21:01:10 +00:00
"protocols: [tcp]\n" +
"readBufferSize: 4500\n" +
"rtspAddress: :8555\n" +
2021-03-10 21:01:10 +00:00
"paths:\n" +
" teststream:\n" +
" source: rtsp://localhost:8554/teststream\n" +
2021-04-03 16:02:56 +00:00
" sourceProtocol: tcp\n")
2021-03-10 21:01:10 +00:00
require.Equal(t, true, ok)
defer p2.close()
time.Sleep(100 * time.Millisecond)
dest, err := client.DialRead("rtsp://localhost:8555/teststream")
2021-03-10 21:01:10 +00:00
require.NoError(t, err)
defer dest.Close()
2021-04-03 16:02:56 +00:00
input := bytes.Repeat([]byte{0x01, 0x02, 0x03, 0x04, 0x05}, 4096/5)
2021-05-10 21:23:56 +00:00
readDone := make(chan struct{})
frameRecv := make(chan struct{})
go func() {
defer close(readDone)
dest.ReadFrames(func(trackID int, streamType gortsplib.StreamType, payload []byte) {
require.Equal(t, gortsplib.StreamTypeRTP, streamType)
require.Equal(t, input, payload)
close(frameRecv)
})
}()
2021-03-10 21:01:10 +00:00
err = source.WriteFrame(0, gortsplib.StreamTypeRTP, input)
2021-03-10 21:01:10 +00:00
require.NoError(t, err)
2021-05-10 21:23:56 +00:00
<-frameRecv
dest.Close()
<-readDone
2021-03-10 21:01:10 +00:00
})
}
func TestRTSPServerRedirect(t *testing.T) {
p1, ok := newInstance("rtmpDisable: yes\n" +
2021-04-11 17:05:08 +00:00
"hlsDisable: yes\n" +
2021-03-10 21:01:10 +00:00
"paths:\n" +
" path1:\n" +
" source: redirect\n" +
" sourceRedirect: rtsp://localhost:8554/path2\n" +
2021-03-10 21:01:10 +00:00
" path2:\n")
require.Equal(t, true, ok)
defer p1.close()
cnt1, err := newContainer("ffmpeg", "source", []string{
"-re",
"-stream_loop", "-1",
2021-03-22 18:51:25 +00:00
"-i", "emptyvideo.mkv",
2021-03-10 21:01:10 +00:00
"-c", "copy",
"-f", "rtsp",
"-rtsp_transport", "udp",
"rtsp://localhost:8554/path2",
2021-03-10 21:01:10 +00:00
})
require.NoError(t, err)
defer cnt1.close()
time.Sleep(1 * time.Second)
cnt2, err := newContainer("ffmpeg", "dest", []string{
"-rtsp_transport", "udp",
"-i", "rtsp://localhost:8554/path1",
2021-03-10 21:01:10 +00:00
"-vframes", "1",
"-f", "image2",
"-y", "/dev/null",
})
require.NoError(t, err)
defer cnt2.close()
require.Equal(t, 0, cnt2.wait())
}
func TestRTSPServerFallback(t *testing.T) {
2021-03-10 21:01:10 +00:00
for _, ca := range []string{
"absolute",
"relative",
} {
t.Run(ca, func(t *testing.T) {
val := func() string {
if ca == "absolute" {
return "rtsp://localhost:8554/path2"
2021-03-10 21:01:10 +00:00
}
return "/path2"
}()
p1, ok := newInstance("rtmpDisable: yes\n" +
2021-04-11 17:05:08 +00:00
"hlsDisable: yes\n" +
2021-03-10 21:01:10 +00:00
"paths:\n" +
" path1:\n" +
" fallback: " + val + "\n" +
" path2:\n")
require.Equal(t, true, ok)
defer p1.close()
cnt1, err := newContainer("ffmpeg", "source", []string{
"-re",
"-stream_loop", "-1",
2021-03-22 18:51:25 +00:00
"-i", "emptyvideo.mkv",
2021-03-10 21:01:10 +00:00
"-c", "copy",
"-f", "rtsp",
"-rtsp_transport", "udp",
"rtsp://localhost:8554/path2",
2021-03-10 21:01:10 +00:00
})
require.NoError(t, err)
defer cnt1.close()
time.Sleep(1 * time.Second)
cnt2, err := newContainer("ffmpeg", "dest", []string{
"-rtsp_transport", "udp",
"-i", "rtsp://localhost:8554/path1",
2021-03-10 21:01:10 +00:00
"-vframes", "1",
"-f", "image2",
"-y", "/dev/null",
})
require.NoError(t, err)
defer cnt2.close()
require.Equal(t, 0, cnt2.wait())
})
}
}
func TestRTSPServerRunOnDemand(t *testing.T) {
2021-03-10 21:01:10 +00:00
doneFile := filepath.Join(os.TempDir(), "ondemand_done")
onDemandFile, err := writeTempFile([]byte(fmt.Sprintf(`#!/bin/sh
trap 'touch %s; [ -z "$(jobs -p)" ] || kill $(jobs -p)' QUIT
ffmpeg -hide_banner -loglevel error -re -i ../../testimages/ffmpeg/emptyvideo.mkv -c copy -f rtsp rtsp://localhost:$RTSP_PORT/$RTSP_PATH &
2021-03-10 21:01:10 +00:00
wait
`, doneFile)))
require.NoError(t, err)
defer os.Remove(onDemandFile)
2021-05-23 16:43:49 +00:00
err = os.Chmod(onDemandFile, 0o755)
2021-03-10 21:01:10 +00:00
require.NoError(t, err)
t.Run("describe", func(t *testing.T) {
defer os.Remove(doneFile)
p1, ok := newInstance(fmt.Sprintf("rtmpDisable: yes\n"+
2021-04-11 17:05:08 +00:00
"hlsDisable: yes\n"+
2021-03-10 21:01:10 +00:00
"paths:\n"+
" all:\n"+
" runOnDemand: %s\n"+
" runOnDemandCloseAfter: 2s\n", onDemandFile))
require.Equal(t, true, ok)
defer p1.close()
func() {
conn, err := net.Dial("tcp", "127.0.0.1:8554")
require.NoError(t, err)
defer conn.Close()
bconn := bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn))
err = base.Request{
Method: base.Describe,
2021-05-29 17:12:26 +00:00
URL: mustParseURL("rtsp://localhost:8554/ondemand"),
2021-03-10 21:01:10 +00:00
Header: base.Header{
"CSeq": base.HeaderValue{"1"},
},
}.Write(bconn.Writer)
require.NoError(t, err)
var res base.Response
err = res.Read(bconn.Reader)
require.NoError(t, err)
require.Equal(t, base.StatusOK, res.StatusCode)
}()
for {
_, err := os.Stat(doneFile)
if err == nil {
break
}
time.Sleep(500 * time.Millisecond)
}
})
t.Run("describe and setup", func(t *testing.T) {
defer os.Remove(doneFile)
p1, ok := newInstance(fmt.Sprintf("rtmpDisable: yes\n"+
2021-04-11 17:05:08 +00:00
"hlsDisable: yes\n"+
2021-03-10 21:01:10 +00:00
"paths:\n"+
" all:\n"+
" runOnDemand: %s\n"+
" runOnDemandCloseAfter: 2s\n", onDemandFile))
require.Equal(t, true, ok)
defer p1.close()
func() {
conn, err := net.Dial("tcp", "127.0.0.1:8554")
require.NoError(t, err)
defer conn.Close()
bconn := bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn))
err = base.Request{
Method: base.Describe,
2021-05-29 17:12:26 +00:00
URL: mustParseURL("rtsp://localhost:8554/ondemand"),
2021-03-10 21:01:10 +00:00
Header: base.Header{
"CSeq": base.HeaderValue{"1"},
},
}.Write(bconn.Writer)
require.NoError(t, err)
var res base.Response
err = res.Read(bconn.Reader)
require.NoError(t, err)
require.Equal(t, base.StatusOK, res.StatusCode)
err = base.Request{
Method: base.Setup,
2021-05-29 17:12:26 +00:00
URL: mustParseURL("rtsp://localhost:8554/ondemand/trackID=0"),
2021-03-10 21:01:10 +00:00
Header: base.Header{
"CSeq": base.HeaderValue{"2"},
"Transport": headers.Transport{
Protocol: base.StreamProtocolTCP,
2021-03-10 21:01:10 +00:00
Delivery: func() *base.StreamDelivery {
v := base.StreamDeliveryUnicast
return &v
}(),
Mode: func() *headers.TransportMode {
v := headers.TransportModePlay
return &v
}(),
2021-03-21 10:22:49 +00:00
InterleavedIDs: &[2]int{0, 1},
2021-03-10 21:01:10 +00:00
}.Write(),
},
}.Write(bconn.Writer)
require.NoError(t, err)
err = res.Read(bconn.Reader)
require.NoError(t, err)
require.Equal(t, base.StatusOK, res.StatusCode)
}()
for {
_, err := os.Stat(doneFile)
if err == nil {
break
}
time.Sleep(500 * time.Millisecond)
}
})
t.Run("setup", func(t *testing.T) {
defer os.Remove(doneFile)
p1, ok := newInstance(fmt.Sprintf("rtmpDisable: yes\n"+
2021-04-11 17:05:08 +00:00
"hlsDisable: yes\n"+
2021-03-10 21:01:10 +00:00
"paths:\n"+
" all:\n"+
" runOnDemand: %s\n"+
" runOnDemandCloseAfter: 2s\n", onDemandFile))
require.Equal(t, true, ok)
defer p1.close()
func() {
conn, err := net.Dial("tcp", "127.0.0.1:8554")
require.NoError(t, err)
defer conn.Close()
bconn := bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn))
err = base.Request{
Method: base.Setup,
2021-05-29 17:12:26 +00:00
URL: mustParseURL("rtsp://localhost:8554/ondemand/trackID=0"),
2021-03-10 21:01:10 +00:00
Header: base.Header{
"CSeq": base.HeaderValue{"1"},
"Transport": headers.Transport{
Protocol: base.StreamProtocolTCP,
2021-03-10 21:01:10 +00:00
Delivery: func() *base.StreamDelivery {
v := base.StreamDeliveryUnicast
return &v
}(),
Mode: func() *headers.TransportMode {
v := headers.TransportModePlay
return &v
}(),
2021-03-21 10:22:49 +00:00
InterleavedIDs: &[2]int{0, 1},
2021-03-10 21:01:10 +00:00
}.Write(),
},
}.Write(bconn.Writer)
require.NoError(t, err)
var res base.Response
err = res.Read(bconn.Reader)
require.NoError(t, err)
require.Equal(t, base.StatusOK, res.StatusCode)
}()
for {
_, err := os.Stat(doneFile)
if err == nil {
break
}
time.Sleep(500 * time.Millisecond)
}
})
}