2020-05-10 19:32:40 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-10-13 22:29:27 +00:00
|
|
|
"io/ioutil"
|
2020-06-08 19:36:42 +00:00
|
|
|
"net"
|
2020-05-10 19:32:40 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2020-10-24 17:55:47 +00:00
|
|
|
"path/filepath"
|
2020-10-18 22:08:44 +00:00
|
|
|
"regexp"
|
2020-07-10 10:22:30 +00:00
|
|
|
"strconv"
|
2020-05-10 19:32:40 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-10-13 21:36:27 +00:00
|
|
|
"github.com/aler9/gortsplib"
|
2020-05-10 19:32:40 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-10-13 22:50:08 +00:00
|
|
|
|
2020-11-01 21:56:56 +00:00
|
|
|
"github.com/aler9/rtsp-simple-server/internal/conf"
|
2020-05-10 19:32:40 +00:00
|
|
|
)
|
|
|
|
|
2020-06-08 19:36:42 +00:00
|
|
|
var ownDockerIp = func() string {
|
|
|
|
out, err := exec.Command("docker", "network", "inspect", "bridge",
|
|
|
|
"-f", "{{range .IPAM.Config}}{{.Subnet}}{{end}}").Output()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ipnet, err := net.ParseCIDR(string(out[:len(out)-1]))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ifaces, err := net.Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, i := range ifaces {
|
|
|
|
addrs, err := i.Addrs()
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, addr := range addrs {
|
|
|
|
if v, ok := addr.(*net.IPNet); ok {
|
|
|
|
if ipnet.Contains(v.IP) {
|
|
|
|
return v.IP.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
panic("IP not found")
|
|
|
|
}()
|
|
|
|
|
2020-05-10 19:32:40 +00:00
|
|
|
type container struct {
|
2020-07-10 10:22:30 +00:00
|
|
|
name string
|
2020-05-10 19:32:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newContainer(image string, name string, args []string) (*container, error) {
|
|
|
|
c := &container{
|
2020-07-10 10:22:30 +00:00
|
|
|
name: name,
|
2020-05-10 19:32:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exec.Command("docker", "kill", "rtsp-simple-server-test-"+name).Run()
|
|
|
|
exec.Command("docker", "wait", "rtsp-simple-server-test-"+name).Run()
|
|
|
|
|
2020-06-08 19:36:42 +00:00
|
|
|
cmd := []string{"docker", "run",
|
2020-05-10 19:32:40 +00:00
|
|
|
"--name=rtsp-simple-server-test-" + name,
|
|
|
|
"rtsp-simple-server-test-" + image}
|
|
|
|
cmd = append(cmd, args...)
|
|
|
|
ecmd := exec.Command(cmd[0], cmd[1:]...)
|
2020-07-10 10:22:30 +00:00
|
|
|
ecmd.Stdout = nil
|
2020-05-10 19:32:40 +00:00
|
|
|
ecmd.Stderr = os.Stderr
|
|
|
|
|
|
|
|
err := ecmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *container) close() {
|
|
|
|
exec.Command("docker", "kill", "rtsp-simple-server-test-"+c.name).Run()
|
|
|
|
exec.Command("docker", "wait", "rtsp-simple-server-test-"+c.name).Run()
|
|
|
|
exec.Command("docker", "rm", "rtsp-simple-server-test-"+c.name).Run()
|
|
|
|
}
|
|
|
|
|
2020-07-10 10:22:30 +00:00
|
|
|
func (c *container) wait() int {
|
2020-05-10 19:32:40 +00:00
|
|
|
exec.Command("docker", "wait", "rtsp-simple-server-test-"+c.name).Run()
|
2020-07-10 10:22:30 +00:00
|
|
|
out, _ := exec.Command("docker", "inspect", "rtsp-simple-server-test-"+c.name,
|
2020-10-03 19:10:41 +00:00
|
|
|
"-f", "{{.State.ExitCode}}").Output()
|
2020-07-10 10:22:30 +00:00
|
|
|
code, _ := strconv.ParseInt(string(out[:len(out)-1]), 10, 64)
|
|
|
|
return int(code)
|
2020-05-10 19:32:40 +00:00
|
|
|
}
|
|
|
|
|
2020-10-03 19:10:41 +00:00
|
|
|
func (c *container) ip() string {
|
|
|
|
out, _ := exec.Command("docker", "inspect", "rtsp-simple-server-test-"+c.name,
|
|
|
|
"-f", "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}").Output()
|
|
|
|
return string(out[:len(out)-1])
|
|
|
|
}
|
|
|
|
|
2020-10-13 22:29:27 +00:00
|
|
|
func testProgram(conf string) (*program, error) {
|
|
|
|
if conf == "" {
|
2020-10-13 23:19:14 +00:00
|
|
|
return newProgram([]string{})
|
2020-10-13 22:29:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tmpf, err := ioutil.TempFile(os.TempDir(), "rtsp-")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer os.Remove(tmpf.Name())
|
|
|
|
|
|
|
|
tmpf.WriteString(conf)
|
|
|
|
tmpf.Close()
|
|
|
|
|
2020-10-13 23:19:14 +00:00
|
|
|
return newProgram([]string{tmpf.Name()})
|
2020-10-13 22:29:27 +00:00
|
|
|
}
|
|
|
|
|
2020-10-13 21:36:27 +00:00
|
|
|
func TestEnvironment(t *testing.T) {
|
|
|
|
// string
|
2020-10-30 23:11:51 +00:00
|
|
|
os.Setenv("RTSP_RUNONCONNECT", "test=cmd")
|
2020-10-13 21:36:27 +00:00
|
|
|
defer os.Unsetenv("RTSP_RUNONCONNECT")
|
|
|
|
|
|
|
|
// int
|
|
|
|
os.Setenv("RTSP_RTSPPORT", "8555")
|
|
|
|
defer os.Unsetenv("RTSP_RTSPPORT")
|
|
|
|
|
|
|
|
// bool
|
|
|
|
os.Setenv("RTSP_METRICS", "yes")
|
|
|
|
defer os.Unsetenv("RTSP_METRICS")
|
|
|
|
|
|
|
|
// duration
|
|
|
|
os.Setenv("RTSP_READTIMEOUT", "22s")
|
|
|
|
defer os.Unsetenv("RTSP_READTIMEOUT")
|
|
|
|
|
|
|
|
// slice
|
|
|
|
os.Setenv("RTSP_LOGDESTINATIONS", "stdout,file")
|
|
|
|
defer os.Unsetenv("RTSP_LOGDESTINATIONS")
|
|
|
|
|
|
|
|
// map key
|
|
|
|
os.Setenv("RTSP_PATHS_TEST2", "")
|
|
|
|
defer os.Unsetenv("RTSP_PATHS_TEST2")
|
|
|
|
|
2020-10-18 22:08:44 +00:00
|
|
|
// map values, "all" path
|
|
|
|
os.Setenv("RTSP_PATHS_ALL_READUSER", "testuser")
|
|
|
|
defer os.Unsetenv("RTSP_PATHS_ALL_READUSER")
|
|
|
|
os.Setenv("RTSP_PATHS_ALL_READPASS", "testpass")
|
|
|
|
defer os.Unsetenv("RTSP_PATHS_ALL_READPASS")
|
|
|
|
|
|
|
|
// map values, generic path
|
|
|
|
os.Setenv("RTSP_PATHS_CAM1_SOURCE", "rtsp://testing")
|
|
|
|
defer os.Unsetenv("RTSP_PATHS_CAM1_SOURCE")
|
|
|
|
os.Setenv("RTSP_PATHS_CAM1_SOURCEPROTOCOL", "tcp")
|
|
|
|
defer os.Unsetenv("RTSP_PATHS_CAM1_SOURCEPROTOCOL")
|
|
|
|
os.Setenv("RTSP_PATHS_CAM1_SOURCEONDEMAND", "yes")
|
|
|
|
defer os.Unsetenv("RTSP_PATHS_CAM1_SOURCEONDEMAND")
|
2020-10-13 21:36:27 +00:00
|
|
|
|
2020-10-13 22:29:27 +00:00
|
|
|
p, err := testProgram("")
|
2020-10-13 21:36:27 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer p.close()
|
|
|
|
|
2020-10-30 23:11:51 +00:00
|
|
|
require.Equal(t, "test=cmd", p.conf.RunOnConnect)
|
2020-10-13 21:36:27 +00:00
|
|
|
|
|
|
|
require.Equal(t, 8555, p.conf.RtspPort)
|
|
|
|
|
|
|
|
require.Equal(t, true, p.conf.Metrics)
|
|
|
|
|
|
|
|
require.Equal(t, 22*time.Second, p.conf.ReadTimeout)
|
|
|
|
|
|
|
|
require.Equal(t, []string{"stdout", "file"}, p.conf.LogDestinations)
|
|
|
|
|
|
|
|
pa, ok := p.conf.Paths["test2"]
|
|
|
|
require.Equal(t, true, ok)
|
2020-10-13 22:50:08 +00:00
|
|
|
require.Equal(t, &conf.PathConf{
|
2020-11-01 15:51:12 +00:00
|
|
|
Source: "record",
|
|
|
|
SourceOnDemandStartTimeout: 10 * time.Second,
|
|
|
|
SourceOnDemandCloseAfter: 10 * time.Second,
|
|
|
|
RunOnDemandStartTimeout: 10 * time.Second,
|
|
|
|
RunOnDemandCloseAfter: 10 * time.Second,
|
2020-10-13 21:36:27 +00:00
|
|
|
}, pa)
|
|
|
|
|
2020-10-18 22:08:44 +00:00
|
|
|
pa, ok = p.conf.Paths["~^.*$"]
|
|
|
|
require.Equal(t, true, ok)
|
|
|
|
require.Equal(t, &conf.PathConf{
|
2020-11-01 15:51:12 +00:00
|
|
|
Regexp: regexp.MustCompile("^.*$"),
|
|
|
|
Source: "record",
|
2020-11-21 12:34:27 +00:00
|
|
|
SourceProtocol: "automatic",
|
2020-11-01 15:51:12 +00:00
|
|
|
SourceOnDemandStartTimeout: 10 * time.Second,
|
|
|
|
SourceOnDemandCloseAfter: 10 * time.Second,
|
|
|
|
ReadUser: "testuser",
|
|
|
|
ReadPass: "testpass",
|
|
|
|
RunOnDemandStartTimeout: 10 * time.Second,
|
|
|
|
RunOnDemandCloseAfter: 10 * time.Second,
|
2020-10-18 22:08:44 +00:00
|
|
|
}, pa)
|
|
|
|
|
|
|
|
pa, ok = p.conf.Paths["cam1"]
|
2020-10-13 21:36:27 +00:00
|
|
|
require.Equal(t, true, ok)
|
2020-10-13 22:50:08 +00:00
|
|
|
require.Equal(t, &conf.PathConf{
|
2020-11-21 12:34:27 +00:00
|
|
|
Source: "rtsp://testing",
|
|
|
|
SourceProtocol: "tcp",
|
|
|
|
SourceProtocolParsed: func() *gortsplib.StreamProtocol {
|
|
|
|
v := gortsplib.StreamProtocolTCP
|
|
|
|
return &v
|
|
|
|
}(),
|
2020-11-01 15:51:12 +00:00
|
|
|
SourceOnDemand: true,
|
|
|
|
SourceOnDemandStartTimeout: 10 * time.Second,
|
|
|
|
SourceOnDemandCloseAfter: 10 * time.Second,
|
|
|
|
RunOnDemandStartTimeout: 10 * time.Second,
|
|
|
|
RunOnDemandCloseAfter: 10 * time.Second,
|
2020-10-13 21:36:27 +00:00
|
|
|
}, pa)
|
|
|
|
}
|
|
|
|
|
2020-10-18 22:22:16 +00:00
|
|
|
func TestEnvironmentNoFile(t *testing.T) {
|
|
|
|
os.Setenv("RTSP_PATHS_CAM1_SOURCE", "rtsp://testing")
|
|
|
|
defer os.Unsetenv("RTSP_PATHS_CAM1_SOURCE")
|
|
|
|
|
|
|
|
p, err := testProgram("{}")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer p.close()
|
|
|
|
|
|
|
|
pa, ok := p.conf.Paths["cam1"]
|
|
|
|
require.Equal(t, true, ok)
|
|
|
|
require.Equal(t, &conf.PathConf{
|
2020-11-01 15:51:12 +00:00
|
|
|
Source: "rtsp://testing",
|
2020-11-21 12:34:27 +00:00
|
|
|
SourceProtocol: "automatic",
|
2020-11-01 15:51:12 +00:00
|
|
|
SourceOnDemandStartTimeout: 10 * time.Second,
|
|
|
|
SourceOnDemandCloseAfter: 10 * time.Second,
|
|
|
|
RunOnDemandStartTimeout: 10 * time.Second,
|
|
|
|
RunOnDemandCloseAfter: 10 * time.Second,
|
2020-10-18 22:22:16 +00:00
|
|
|
}, pa)
|
|
|
|
}
|
|
|
|
|
2020-07-18 16:23:26 +00:00
|
|
|
func TestPublish(t *testing.T) {
|
|
|
|
for _, conf := range []struct {
|
|
|
|
publishSoft string
|
|
|
|
publishProto string
|
|
|
|
}{
|
|
|
|
{"ffmpeg", "udp"},
|
|
|
|
{"ffmpeg", "tcp"},
|
|
|
|
{"gstreamer", "udp"},
|
|
|
|
{"gstreamer", "tcp"},
|
2020-05-10 19:32:40 +00:00
|
|
|
} {
|
2020-07-18 16:23:26 +00:00
|
|
|
t.Run(conf.publishSoft+"_"+conf.publishProto, func(t *testing.T) {
|
2020-10-13 22:29:27 +00:00
|
|
|
p, err := testProgram("")
|
2020-05-10 19:32:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer p.close()
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
2020-07-18 16:23:26 +00:00
|
|
|
switch conf.publishSoft {
|
|
|
|
case "ffmpeg":
|
2020-10-27 23:29:53 +00:00
|
|
|
cnt1, err := newContainer("ffmpeg", "source", []string{
|
2020-07-18 16:23:26 +00:00
|
|
|
"-re",
|
|
|
|
"-stream_loop", "-1",
|
|
|
|
"-i", "/emptyvideo.ts",
|
|
|
|
"-c", "copy",
|
|
|
|
"-f", "rtsp",
|
|
|
|
"-rtsp_transport", conf.publishProto,
|
|
|
|
"rtsp://" + ownDockerIp + ":8554/teststream",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer cnt1.close()
|
|
|
|
|
|
|
|
default:
|
|
|
|
cnt1, err := newContainer("gstreamer", "source", []string{
|
2020-10-05 19:07:34 +00:00
|
|
|
"filesrc location=emptyvideo.ts ! tsdemux ! queue ! video/x-h264 ! h264parse config-interval=1 ! rtspclientsink " +
|
2020-07-28 13:20:34 +00:00
|
|
|
"location=rtsp://" + ownDockerIp + ":8554/teststream protocols=" + conf.publishProto + " latency=0",
|
2020-07-18 16:23:26 +00:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer cnt1.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
2020-10-27 23:29:53 +00:00
|
|
|
cnt2, err := newContainer("ffmpeg", "dest", []string{
|
2020-07-18 16:23:26 +00:00
|
|
|
"-rtsp_transport", "udp",
|
|
|
|
"-i", "rtsp://" + ownDockerIp + ":8554/teststream",
|
|
|
|
"-vframes", "1",
|
|
|
|
"-f", "image2",
|
|
|
|
"-y", "/dev/null",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer cnt2.close()
|
|
|
|
|
|
|
|
code := cnt2.wait()
|
|
|
|
require.Equal(t, 0, code)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRead(t *testing.T) {
|
|
|
|
for _, conf := range []struct {
|
|
|
|
readSoft string
|
|
|
|
readProto string
|
|
|
|
}{
|
|
|
|
{"ffmpeg", "udp"},
|
|
|
|
{"ffmpeg", "tcp"},
|
|
|
|
{"vlc", "udp"},
|
|
|
|
{"vlc", "tcp"},
|
|
|
|
} {
|
|
|
|
t.Run(conf.readSoft+"_"+conf.readProto, func(t *testing.T) {
|
2020-10-13 22:29:27 +00:00
|
|
|
p, err := testProgram("")
|
2020-07-18 16:23:26 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer p.close()
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
2020-10-27 23:29:53 +00:00
|
|
|
cnt1, err := newContainer("ffmpeg", "source", []string{
|
2020-05-10 19:32:40 +00:00
|
|
|
"-re",
|
|
|
|
"-stream_loop", "-1",
|
|
|
|
"-i", "/emptyvideo.ts",
|
|
|
|
"-c", "copy",
|
|
|
|
"-f", "rtsp",
|
2020-07-18 16:23:26 +00:00
|
|
|
"-rtsp_transport", "udp",
|
2020-06-08 19:36:42 +00:00
|
|
|
"rtsp://" + ownDockerIp + ":8554/teststream",
|
2020-05-10 19:32:40 +00:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer cnt1.close()
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
2020-07-18 16:23:26 +00:00
|
|
|
switch conf.readSoft {
|
|
|
|
case "ffmpeg":
|
2020-10-27 23:29:53 +00:00
|
|
|
cnt2, err := newContainer("ffmpeg", "dest", []string{
|
2020-07-18 16:23:26 +00:00
|
|
|
"-rtsp_transport", conf.readProto,
|
2020-07-10 10:22:30 +00:00
|
|
|
"-i", "rtsp://" + ownDockerIp + ":8554/teststream",
|
|
|
|
"-vframes", "1",
|
|
|
|
"-f", "image2",
|
|
|
|
"-y", "/dev/null",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer cnt2.close()
|
|
|
|
|
|
|
|
code := cnt2.wait()
|
|
|
|
require.Equal(t, 0, code)
|
|
|
|
|
2020-07-18 16:23:26 +00:00
|
|
|
default:
|
2020-07-10 10:22:30 +00:00
|
|
|
args := []string{}
|
2020-07-18 16:23:26 +00:00
|
|
|
if conf.readProto == "tcp" {
|
2020-07-10 10:22:30 +00:00
|
|
|
args = append(args, "--rtsp-tcp")
|
|
|
|
}
|
|
|
|
args = append(args, "rtsp://"+ownDockerIp+":8554/teststream")
|
2020-05-10 19:32:40 +00:00
|
|
|
|
2020-07-10 10:22:30 +00:00
|
|
|
cnt2, err := newContainer("vlc", "dest", args)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer cnt2.close()
|
2020-05-10 19:32:40 +00:00
|
|
|
|
2020-07-10 10:22:30 +00:00
|
|
|
code := cnt2.wait()
|
|
|
|
require.Equal(t, 0, code)
|
|
|
|
}
|
2020-05-10 19:32:40 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-05 11:19:55 +00:00
|
|
|
func TestTCPOnly(t *testing.T) {
|
2020-10-27 23:29:53 +00:00
|
|
|
p, err := testProgram("protocols: [tcp]\n")
|
2020-08-29 17:48:41 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer p.close()
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
2020-10-27 23:29:53 +00:00
|
|
|
cnt1, err := newContainer("ffmpeg", "source", []string{
|
2020-08-29 17:48:41 +00:00
|
|
|
"-re",
|
|
|
|
"-stream_loop", "-1",
|
|
|
|
"-i", "/emptyvideo.ts",
|
|
|
|
"-c", "copy",
|
|
|
|
"-f", "rtsp",
|
|
|
|
"-rtsp_transport", "tcp",
|
|
|
|
"rtsp://" + ownDockerIp + ":8554/teststream",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer cnt1.close()
|
|
|
|
|
2020-10-27 23:29:53 +00:00
|
|
|
cnt2, err := newContainer("ffmpeg", "dest", []string{
|
2020-08-29 17:48:41 +00:00
|
|
|
"-rtsp_transport", "tcp",
|
|
|
|
"-i", "rtsp://" + ownDockerIp + ":8554/teststream",
|
|
|
|
"-vframes", "1",
|
|
|
|
"-f", "image2",
|
|
|
|
"-y", "/dev/null",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer cnt2.close()
|
|
|
|
|
|
|
|
code := cnt2.wait()
|
|
|
|
require.Equal(t, 0, code)
|
|
|
|
}
|
|
|
|
|
2020-08-29 18:49:26 +00:00
|
|
|
func TestPathWithSlash(t *testing.T) {
|
2020-10-13 22:29:27 +00:00
|
|
|
p, err := testProgram("")
|
2020-08-29 18:49:26 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer p.close()
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
2020-10-27 23:29:53 +00:00
|
|
|
cnt1, err := newContainer("ffmpeg", "source", []string{
|
2020-08-29 18:49:26 +00:00
|
|
|
"-re",
|
|
|
|
"-stream_loop", "-1",
|
|
|
|
"-i", "/emptyvideo.ts",
|
|
|
|
"-c", "copy",
|
|
|
|
"-f", "rtsp",
|
|
|
|
"-rtsp_transport", "udp",
|
|
|
|
"rtsp://" + ownDockerIp + ":8554/test/stream",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer cnt1.close()
|
|
|
|
|
2020-10-27 23:29:53 +00:00
|
|
|
cnt2, err := newContainer("ffmpeg", "dest", []string{
|
2020-08-29 18:49:26 +00:00
|
|
|
"-rtsp_transport", "udp",
|
|
|
|
"-i", "rtsp://" + ownDockerIp + ":8554/test/stream",
|
|
|
|
"-vframes", "1",
|
|
|
|
"-f", "image2",
|
|
|
|
"-y", "/dev/null",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer cnt2.close()
|
|
|
|
|
|
|
|
code := cnt2.wait()
|
|
|
|
require.Equal(t, 0, code)
|
|
|
|
}
|
|
|
|
|
2020-09-05 18:53:56 +00:00
|
|
|
func TestPathWithQuery(t *testing.T) {
|
2020-10-13 22:29:27 +00:00
|
|
|
p, err := testProgram("")
|
2020-09-05 18:53:56 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer p.close()
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
2020-10-27 23:29:53 +00:00
|
|
|
cnt1, err := newContainer("ffmpeg", "source", []string{
|
2020-09-05 18:53:56 +00:00
|
|
|
"-re",
|
|
|
|
"-stream_loop", "-1",
|
|
|
|
"-i", "/emptyvideo.ts",
|
|
|
|
"-c", "copy",
|
|
|
|
"-f", "rtsp",
|
|
|
|
"-rtsp_transport", "udp",
|
|
|
|
"rtsp://" + ownDockerIp + ":8554/test?param1=val¶m2=val",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer cnt1.close()
|
|
|
|
|
2020-10-27 23:29:53 +00:00
|
|
|
cnt2, err := newContainer("ffmpeg", "dest", []string{
|
2020-09-05 18:53:56 +00:00
|
|
|
"-rtsp_transport", "udp",
|
|
|
|
"-i", "rtsp://" + ownDockerIp + ":8554/test?param3=otherval",
|
|
|
|
"-vframes", "1",
|
|
|
|
"-f", "image2",
|
|
|
|
"-y", "/dev/null",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer cnt2.close()
|
|
|
|
|
|
|
|
code := cnt2.wait()
|
|
|
|
require.Equal(t, 0, code)
|
|
|
|
}
|
|
|
|
|
2020-07-10 10:22:30 +00:00
|
|
|
func TestAuth(t *testing.T) {
|
|
|
|
t.Run("publish", func(t *testing.T) {
|
2020-10-27 23:29:53 +00:00
|
|
|
p, err := testProgram("paths:\n" +
|
2020-07-10 10:22:30 +00:00
|
|
|
" all:\n" +
|
|
|
|
" publishUser: testuser\n" +
|
2020-10-31 12:06:39 +00:00
|
|
|
" publishPass: test!$()*+.;<=>[]^_-{}\n" +
|
2020-10-27 23:29:53 +00:00
|
|
|
" publishIps: [172.17.0.0/16]\n")
|
2020-07-10 10:22:30 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer p.close()
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
cnt1, err := newContainer("ffmpeg", "source", []string{
|
|
|
|
"-re",
|
|
|
|
"-stream_loop", "-1",
|
|
|
|
"-i", "/emptyvideo.ts",
|
|
|
|
"-c", "copy",
|
|
|
|
"-f", "rtsp",
|
|
|
|
"-rtsp_transport", "udp",
|
2020-10-31 14:43:08 +00:00
|
|
|
"rtsp://testuser:test!$()*+.;<=>[]^_-{}@" + ownDockerIp + ":8554/test/stream",
|
2020-07-10 10:22:30 +00:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer cnt1.close()
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
cnt2, err := newContainer("ffmpeg", "dest", []string{
|
|
|
|
"-rtsp_transport", "udp",
|
2020-10-31 14:43:08 +00:00
|
|
|
"-i", "rtsp://" + ownDockerIp + ":8554/test/stream",
|
2020-07-10 10:22:30 +00:00
|
|
|
"-vframes", "1",
|
|
|
|
"-f", "image2",
|
|
|
|
"-y", "/dev/null",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer cnt2.close()
|
2020-05-10 19:32:40 +00:00
|
|
|
|
2020-07-10 10:22:30 +00:00
|
|
|
code := cnt2.wait()
|
|
|
|
require.Equal(t, 0, code)
|
2020-05-10 19:32:40 +00:00
|
|
|
})
|
|
|
|
|
2020-07-10 10:22:30 +00:00
|
|
|
for _, soft := range []string{
|
|
|
|
"ffmpeg",
|
|
|
|
"vlc",
|
|
|
|
} {
|
|
|
|
t.Run("read_"+soft, func(t *testing.T) {
|
2020-10-27 23:29:53 +00:00
|
|
|
p, err := testProgram("paths:\n" +
|
2020-07-10 10:22:30 +00:00
|
|
|
" all:\n" +
|
|
|
|
" readUser: testuser\n" +
|
2020-10-31 12:06:39 +00:00
|
|
|
" readPass: test!$()*+.;<=>[]^_-{}\n" +
|
2020-10-27 23:29:53 +00:00
|
|
|
" readIps: [172.17.0.0/16]\n")
|
2020-07-10 10:22:30 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer p.close()
|
2020-05-10 20:56:46 +00:00
|
|
|
|
2020-07-10 10:22:30 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
2020-05-10 20:56:46 +00:00
|
|
|
|
2020-07-10 10:22:30 +00:00
|
|
|
cnt1, err := newContainer("ffmpeg", "source", []string{
|
|
|
|
"-re",
|
|
|
|
"-stream_loop", "-1",
|
|
|
|
"-i", "/emptyvideo.ts",
|
|
|
|
"-c", "copy",
|
|
|
|
"-f", "rtsp",
|
|
|
|
"-rtsp_transport", "udp",
|
2020-10-31 14:43:08 +00:00
|
|
|
"rtsp://" + ownDockerIp + ":8554/test/stream",
|
2020-07-10 10:22:30 +00:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer cnt1.close()
|
2020-05-10 20:56:46 +00:00
|
|
|
|
2020-07-10 10:22:30 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
2020-05-10 20:56:46 +00:00
|
|
|
|
2020-07-10 10:22:30 +00:00
|
|
|
if soft == "ffmpeg" {
|
|
|
|
cnt2, err := newContainer("ffmpeg", "dest", []string{
|
|
|
|
"-rtsp_transport", "udp",
|
2020-10-31 14:43:08 +00:00
|
|
|
"-i", "rtsp://testuser:test!$()*+.;<=>[]^_-{}@" + ownDockerIp + ":8554/test/stream",
|
2020-07-10 10:22:30 +00:00
|
|
|
"-vframes", "1",
|
|
|
|
"-f", "image2",
|
|
|
|
"-y", "/dev/null",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer cnt2.close()
|
|
|
|
|
|
|
|
code := cnt2.wait()
|
|
|
|
require.Equal(t, 0, code)
|
|
|
|
|
|
|
|
} else {
|
2020-10-31 12:06:39 +00:00
|
|
|
cnt2, err := newContainer("vlc", "dest", []string{
|
2020-10-31 14:43:08 +00:00
|
|
|
"rtsp://testuser:test!$()*+.;<=>[]^_-{}@" + ownDockerIp + ":8554/test/stream",
|
2020-10-31 12:06:39 +00:00
|
|
|
})
|
2020-07-10 10:22:30 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer cnt2.close()
|
|
|
|
|
|
|
|
code := cnt2.wait()
|
|
|
|
require.Equal(t, 0, code)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-05-10 20:56:46 +00:00
|
|
|
}
|
2020-06-30 13:12:39 +00:00
|
|
|
|
2020-10-03 19:10:41 +00:00
|
|
|
func TestSourceRtsp(t *testing.T) {
|
2020-06-30 13:12:39 +00:00
|
|
|
for _, proto := range []string{
|
|
|
|
"udp",
|
|
|
|
"tcp",
|
|
|
|
} {
|
|
|
|
t.Run(proto, func(t *testing.T) {
|
2020-10-27 23:29:53 +00:00
|
|
|
p1, err := testProgram("paths:\n" +
|
2020-06-30 13:12:39 +00:00
|
|
|
" all:\n" +
|
|
|
|
" readUser: testuser\n" +
|
2020-10-27 23:29:53 +00:00
|
|
|
" readPass: testpass\n")
|
2020-06-30 13:12:39 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer p1.close()
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
cnt1, err := newContainer("ffmpeg", "source", []string{
|
|
|
|
"-re",
|
|
|
|
"-stream_loop", "-1",
|
|
|
|
"-i", "/emptyvideo.ts",
|
|
|
|
"-c", "copy",
|
|
|
|
"-f", "rtsp",
|
|
|
|
"-rtsp_transport", "udp",
|
|
|
|
"rtsp://" + ownDockerIp + ":8554/teststream",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer cnt1.close()
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
2020-10-27 23:29:53 +00:00
|
|
|
p2, err := testProgram("rtspPort: 8555\n" +
|
2020-06-30 13:12:39 +00:00
|
|
|
"rtpPort: 8100\n" +
|
|
|
|
"rtcpPort: 8101\n" +
|
|
|
|
"\n" +
|
|
|
|
"paths:\n" +
|
|
|
|
" proxied:\n" +
|
|
|
|
" source: rtsp://testuser:testpass@localhost:8554/teststream\n" +
|
2020-07-30 11:41:56 +00:00
|
|
|
" sourceProtocol: " + proto + "\n" +
|
2020-10-27 23:29:53 +00:00
|
|
|
" sourceOnDemand: yes\n")
|
2020-06-30 13:12:39 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer p2.close()
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
cnt2, err := newContainer("ffmpeg", "dest", []string{
|
|
|
|
"-rtsp_transport", "udp",
|
|
|
|
"-i", "rtsp://" + ownDockerIp + ":8555/proxied",
|
|
|
|
"-vframes", "1",
|
|
|
|
"-f", "image2",
|
|
|
|
"-y", "/dev/null",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer cnt2.close()
|
|
|
|
|
2020-07-10 10:22:30 +00:00
|
|
|
code := cnt2.wait()
|
|
|
|
require.Equal(t, 0, code)
|
2020-06-30 13:12:39 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-07-30 11:41:56 +00:00
|
|
|
|
2020-10-03 19:10:41 +00:00
|
|
|
func TestSourceRtmp(t *testing.T) {
|
|
|
|
cnt1, err := newContainer("nginx-rtmp", "rtmpserver", []string{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer cnt1.close()
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
cnt2, err := newContainer("ffmpeg", "source", []string{
|
|
|
|
"-re",
|
|
|
|
"-stream_loop", "-1",
|
|
|
|
"-i", "/emptyvideo.ts",
|
|
|
|
"-c", "copy",
|
|
|
|
"-f", "flv",
|
|
|
|
"rtmp://" + cnt1.ip() + "/stream/test",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer cnt2.close()
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
2020-10-27 23:29:53 +00:00
|
|
|
p, err := testProgram("paths:\n" +
|
2020-10-03 19:10:41 +00:00
|
|
|
" proxied:\n" +
|
|
|
|
" source: rtmp://" + cnt1.ip() + "/stream/test\n" +
|
2020-10-27 23:29:53 +00:00
|
|
|
" sourceOnDemand: yes\n")
|
2020-10-03 19:10:41 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer p.close()
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
cnt3, err := newContainer("ffmpeg", "dest", []string{
|
|
|
|
"-rtsp_transport", "udp",
|
|
|
|
"-i", "rtsp://" + ownDockerIp + ":8554/proxied",
|
|
|
|
"-vframes", "1",
|
|
|
|
"-f", "image2",
|
|
|
|
"-y", "/dev/null",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer cnt3.close()
|
|
|
|
|
|
|
|
code := cnt3.wait()
|
|
|
|
require.Equal(t, 0, code)
|
|
|
|
}
|
|
|
|
|
2020-10-27 23:29:53 +00:00
|
|
|
func TestRedirect(t *testing.T) {
|
|
|
|
p1, err := testProgram("paths:\n" +
|
|
|
|
" path1:\n" +
|
|
|
|
" source: redirect\n" +
|
|
|
|
" sourceRedirect: rtsp://" + ownDockerIp + ":8554/path2\n" +
|
|
|
|
" path2:\n")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer p1.close()
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
cnt1, err := newContainer("ffmpeg", "source", []string{
|
|
|
|
"-re",
|
|
|
|
"-stream_loop", "-1",
|
|
|
|
"-i", "/emptyvideo.ts",
|
|
|
|
"-c", "copy",
|
|
|
|
"-f", "rtsp",
|
|
|
|
"-rtsp_transport", "udp",
|
|
|
|
"rtsp://" + ownDockerIp + ":8554/path2",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer cnt1.close()
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
cnt2, err := newContainer("ffmpeg", "dest", []string{
|
|
|
|
"-rtsp_transport", "udp",
|
|
|
|
"-i", "rtsp://" + ownDockerIp + ":8554/path1",
|
|
|
|
"-vframes", "1",
|
|
|
|
"-f", "image2",
|
|
|
|
"-y", "/dev/null",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer cnt2.close()
|
|
|
|
|
|
|
|
code := cnt2.wait()
|
|
|
|
require.Equal(t, 0, code)
|
|
|
|
}
|
|
|
|
|
2020-11-01 16:48:00 +00:00
|
|
|
func TestFallback(t *testing.T) {
|
|
|
|
p1, err := testProgram("paths:\n" +
|
|
|
|
" path1:\n" +
|
|
|
|
" fallback: rtsp://" + ownDockerIp + ":8554/path2\n" +
|
|
|
|
" path2:\n")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer p1.close()
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
cnt1, err := newContainer("ffmpeg", "source", []string{
|
|
|
|
"-re",
|
|
|
|
"-stream_loop", "-1",
|
|
|
|
"-i", "/emptyvideo.ts",
|
|
|
|
"-c", "copy",
|
|
|
|
"-f", "rtsp",
|
|
|
|
"-rtsp_transport", "udp",
|
|
|
|
"rtsp://" + ownDockerIp + ":8554/path2",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer cnt1.close()
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
cnt2, err := newContainer("ffmpeg", "dest", []string{
|
|
|
|
"-rtsp_transport", "udp",
|
|
|
|
"-i", "rtsp://" + ownDockerIp + ":8554/path1",
|
|
|
|
"-vframes", "1",
|
|
|
|
"-f", "image2",
|
|
|
|
"-y", "/dev/null",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer cnt2.close()
|
|
|
|
|
|
|
|
code := cnt2.wait()
|
|
|
|
require.Equal(t, 0, code)
|
|
|
|
}
|
|
|
|
|
2020-07-30 11:41:56 +00:00
|
|
|
func TestRunOnDemand(t *testing.T) {
|
2020-10-27 23:29:53 +00:00
|
|
|
p1, err := testProgram("paths:\n" +
|
2020-09-19 15:13:45 +00:00
|
|
|
" all:\n" +
|
2020-11-01 16:33:06 +00:00
|
|
|
" runOnDemand: ffmpeg -hide_banner -loglevel error -re -i testimages/ffmpeg/emptyvideo.ts -c copy -f rtsp rtsp://localhost:$RTSP_PORT/$RTSP_PATH\n")
|
2020-07-30 11:41:56 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer p1.close()
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
cnt1, err := newContainer("ffmpeg", "dest", []string{
|
|
|
|
"-i", "rtsp://" + ownDockerIp + ":8554/ondemand",
|
|
|
|
"-vframes", "1",
|
|
|
|
"-f", "image2",
|
|
|
|
"-y", "/dev/null",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer cnt1.close()
|
|
|
|
|
|
|
|
code := cnt1.wait()
|
|
|
|
require.Equal(t, 0, code)
|
|
|
|
}
|
2020-10-24 17:55:47 +00:00
|
|
|
|
|
|
|
func TestHotReloading(t *testing.T) {
|
|
|
|
confPath := filepath.Join(os.TempDir(), "rtsp-conf")
|
|
|
|
|
|
|
|
err := ioutil.WriteFile(confPath, []byte("paths:\n"+
|
|
|
|
" test1:\n"+
|
2020-11-08 21:07:15 +00:00
|
|
|
" runOnDemand: ffmpeg -hide_banner -loglevel error -re -i testimages/ffmpeg/emptyvideo.ts -c copy -f rtsp rtsp://localhost:$RTSP_PORT/$RTSP_PATH\n"+
|
|
|
|
" test3:\n"+
|
|
|
|
" runOnInit: echo aaa\n"+
|
|
|
|
" test4:\n"+
|
|
|
|
" runOnInit: echo bbb\n"),
|
2020-10-24 17:55:47 +00:00
|
|
|
0644)
|
|
|
|
require.NoError(t, err)
|
2020-11-08 21:07:15 +00:00
|
|
|
defer os.Remove(confPath)
|
2020-10-24 17:55:47 +00:00
|
|
|
|
|
|
|
p, err := newProgram([]string{confPath})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer p.close()
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
func() {
|
|
|
|
cnt1, err := newContainer("ffmpeg", "dest", []string{
|
|
|
|
"-i", "rtsp://" + ownDockerIp + ":8554/test1",
|
|
|
|
"-vframes", "1",
|
|
|
|
"-f", "image2",
|
|
|
|
"-y", "/dev/null",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer cnt1.close()
|
|
|
|
|
|
|
|
code := cnt1.wait()
|
|
|
|
require.Equal(t, 0, code)
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(confPath, []byte("paths:\n"+
|
|
|
|
" test2:\n"+
|
2020-11-08 21:07:15 +00:00
|
|
|
" runOnDemand: ffmpeg -hide_banner -loglevel error -re -i testimages/ffmpeg/emptyvideo.ts -c copy -f rtsp rtsp://localhost:$RTSP_PORT/$RTSP_PATH\n"+
|
|
|
|
" test3:\n"+
|
|
|
|
" test4:\n"+
|
|
|
|
" runOnInit: echo bbb\n"),
|
2020-10-24 17:55:47 +00:00
|
|
|
0644)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
func() {
|
|
|
|
cnt1, err := newContainer("ffmpeg", "dest", []string{
|
|
|
|
"-i", "rtsp://" + ownDockerIp + ":8554/test1",
|
|
|
|
"-vframes", "1",
|
|
|
|
"-f", "image2",
|
|
|
|
"-y", "/dev/null",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer cnt1.close()
|
|
|
|
|
|
|
|
code := cnt1.wait()
|
|
|
|
require.Equal(t, 1, code)
|
|
|
|
}()
|
|
|
|
|
|
|
|
func() {
|
|
|
|
cnt1, err := newContainer("ffmpeg", "dest", []string{
|
|
|
|
"-i", "rtsp://" + ownDockerIp + ":8554/test2",
|
|
|
|
"-vframes", "1",
|
|
|
|
"-f", "image2",
|
|
|
|
"-y", "/dev/null",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer cnt1.close()
|
|
|
|
|
|
|
|
code := cnt1.wait()
|
|
|
|
require.Equal(t, 0, code)
|
|
|
|
}()
|
|
|
|
}
|