mirror of
https://github.com/bluenviron/mediamtx
synced 2024-12-16 19:54:59 +00:00
361 lines
10 KiB
Go
361 lines
10 KiB
Go
package core
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/aler9/gortsplib"
|
|
"github.com/aler9/gortsplib/pkg/base"
|
|
psdp "github.com/pion/sdp/v3"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
var serverCert = []byte(`-----BEGIN CERTIFICATE-----
|
|
MIIDazCCAlOgAwIBAgIUXw1hEC3LFpTsllv7D3ARJyEq7sIwDQYJKoZIhvcNAQEL
|
|
BQAwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM
|
|
GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0yMDEyMTMxNzQ0NThaFw0zMDEy
|
|
MTExNzQ0NThaMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEw
|
|
HwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwggEiMA0GCSqGSIb3DQEB
|
|
AQUAA4IBDwAwggEKAoIBAQDG8DyyS51810GsGwgWr5rjJK7OE1kTTLSNEEKax8Bj
|
|
zOyiaz8rA2JGl2VUEpi2UjDr9Cm7nd+YIEVs91IIBOb7LGqObBh1kGF3u5aZxLkv
|
|
NJE+HrLVvUhaDobK2NU+Wibqc/EI3DfUkt1rSINvv9flwTFu1qHeuLWhoySzDKEp
|
|
OzYxpFhwjVSokZIjT4Red3OtFz7gl2E6OAWe2qoh5CwLYVdMWtKR0Xuw3BkDPk9I
|
|
qkQKx3fqv97LPEzhyZYjDT5WvGrgZ1WDAN3booxXF3oA1H3GHQc4m/vcLatOtb8e
|
|
nI59gMQLEbnp08cl873bAuNuM95EZieXTHNbwUnq5iybAgMBAAGjUzBRMB0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Ic1g+Dd2/BxbT/RHr05GK
|
|
6vwrfjsoRyMWteC1SsNs/CurjfQ/jqCfHNP5XPvxgd5Ec8sRJIiV7V5RTuWJsPu1
|
|
+3K6cnKEyg+0ekYmLertRFIY6SwWmY1fyKgTvxudMcsBY7dC4xs=
|
|
-----END RSA PRIVATE KEY-----
|
|
`)
|
|
|
|
type container struct {
|
|
name string
|
|
}
|
|
|
|
func newContainer(image string, name string, args []string) (*container, error) {
|
|
c := &container{
|
|
name: name,
|
|
}
|
|
|
|
exec.Command("docker", "kill", "rtsp-simple-server-test-"+name).Run()
|
|
exec.Command("docker", "wait", "rtsp-simple-server-test-"+name).Run()
|
|
|
|
// --network=host is needed to test multicast
|
|
cmd := []string{
|
|
"docker", "run",
|
|
"--network=host",
|
|
"--name=rtsp-simple-server-test-" + name,
|
|
"rtsp-simple-server-test-" + image,
|
|
}
|
|
cmd = append(cmd, args...)
|
|
ecmd := exec.Command(cmd[0], cmd[1:]...)
|
|
ecmd.Stdout = nil
|
|
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()
|
|
}
|
|
|
|
func (c *container) wait() int {
|
|
exec.Command("docker", "wait", "rtsp-simple-server-test-"+c.name).Run()
|
|
out, _ := exec.Command("docker", "inspect", "rtsp-simple-server-test-"+c.name,
|
|
"-f", "{{.State.ExitCode}}").Output()
|
|
code, _ := strconv.ParseInt(string(out[:len(out)-1]), 10, 64)
|
|
return int(code)
|
|
}
|
|
|
|
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])
|
|
}
|
|
|
|
func writeTempFile(byts []byte) (string, error) {
|
|
tmpf, err := ioutil.TempFile(os.TempDir(), "rtsp-")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer tmpf.Close()
|
|
|
|
_, err = tmpf.Write(byts)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return tmpf.Name(), nil
|
|
}
|
|
|
|
func newInstance(conf string) (*Core, bool) {
|
|
if conf == "" {
|
|
return New([]string{})
|
|
}
|
|
|
|
tmpf, err := writeTempFile([]byte(conf))
|
|
if err != nil {
|
|
return nil, false
|
|
}
|
|
defer os.Remove(tmpf)
|
|
|
|
return New([]string{tmpf})
|
|
}
|
|
|
|
func TestCorePathAutoDeletion(t *testing.T) {
|
|
for _, ca := range []string{"describe", "setup"} {
|
|
t.Run(ca, func(t *testing.T) {
|
|
p, ok := newInstance("paths:\n" +
|
|
" all:\n")
|
|
require.Equal(t, true, ok)
|
|
defer p.close()
|
|
|
|
func() {
|
|
c := gortsplib.Client{}
|
|
|
|
err := c.Start("rtsp", "localhost:8554")
|
|
require.NoError(t, err)
|
|
defer c.Close()
|
|
|
|
if ca == "describe" {
|
|
ur, err := base.ParseURL("rtsp://localhost:8554/mypath")
|
|
require.NoError(t, err)
|
|
|
|
_, _, _, err = c.Describe(ur)
|
|
require.EqualError(t, err, "bad status code: 404 (Not Found)")
|
|
} else {
|
|
baseURL, err := base.ParseURL("rtsp://localhost:8554/mypath/")
|
|
require.NoError(t, err)
|
|
|
|
track, err := gortsplib.NewTrackH264(96,
|
|
&gortsplib.TrackConfigH264{SPS: []byte{0x01, 0x02, 0x03, 0x04}, PPS: []byte{0x01, 0x02, 0x03, 0x04}})
|
|
require.NoError(t, err)
|
|
|
|
track.Media.Attributes = append(track.Media.Attributes, psdp.Attribute{
|
|
Key: "control",
|
|
Value: "trackID=0",
|
|
})
|
|
|
|
_, err = c.Setup(true, baseURL, track, 0, 0)
|
|
require.EqualError(t, err, "bad status code: 404 (Not Found)")
|
|
}
|
|
}()
|
|
|
|
res := p.pathManager.onAPIPathsList(pathAPIPathsListReq{})
|
|
require.NoError(t, res.Err)
|
|
|
|
require.Equal(t, 0, len(res.Data.Items))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCorePathRunOnDemand(t *testing.T) {
|
|
doneFile := filepath.Join(os.TempDir(), "ondemand_done")
|
|
|
|
srcFile := filepath.Join(os.TempDir(), "ondemand.go")
|
|
err := ioutil.WriteFile(srcFile, []byte(`
|
|
package main
|
|
|
|
import (
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"io/ioutil"
|
|
"github.com/aler9/gortsplib"
|
|
)
|
|
|
|
func main() {
|
|
track, err := gortsplib.NewTrackH264(96,
|
|
&gortsplib.TrackConfigH264{SPS: []byte{0x01, 0x02, 0x03, 0x04}, PPS: []byte{0x01, 0x02, 0x03, 0x04}})
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
source := gortsplib.Client{}
|
|
|
|
err = source.StartPublishing(
|
|
"rtsp://localhost:" + os.Getenv("RTSP_PORT") + "/" + os.Getenv("RTSP_PATH"),
|
|
gortsplib.Tracks{track})
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer source.Close()
|
|
|
|
c := make(chan os.Signal, 1)
|
|
signal.Notify(c, syscall.SIGINT)
|
|
<-c
|
|
|
|
err = ioutil.WriteFile("`+doneFile+`", []byte(""), 0644)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
`), 0o644)
|
|
require.NoError(t, err)
|
|
|
|
execFile := filepath.Join(os.TempDir(), "ondemand_cmd")
|
|
cmd := exec.Command("go", "build", "-o", execFile, srcFile)
|
|
cmd.Stdout = os.Stdout
|
|
cmd.Stderr = os.Stderr
|
|
err = cmd.Run()
|
|
require.NoError(t, err)
|
|
defer os.Remove(execFile)
|
|
|
|
os.Remove(srcFile)
|
|
|
|
for _, ca := range []string{"describe", "setup", "describe and setup"} {
|
|
t.Run(ca, func(t *testing.T) {
|
|
defer os.Remove(doneFile)
|
|
|
|
p1, ok := newInstance(fmt.Sprintf("rtmpDisable: yes\n"+
|
|
"hlsDisable: yes\n"+
|
|
"paths:\n"+
|
|
" all:\n"+
|
|
" runOnDemand: %s\n"+
|
|
" runOnDemandCloseAfter: 1s\n", execFile))
|
|
require.Equal(t, true, ok)
|
|
defer p1.close()
|
|
|
|
func() {
|
|
c := gortsplib.Client{}
|
|
|
|
err := c.Start("rtsp", "localhost:8554")
|
|
require.NoError(t, err)
|
|
defer c.Close()
|
|
|
|
if ca == "describe" || ca == "describe and setup" {
|
|
ur, err := base.ParseURL("rtsp://localhost:8554/ondemand")
|
|
require.NoError(t, err)
|
|
|
|
_, _, _, err = c.Describe(ur)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
if ca == "setup" || ca == "describe and setup" {
|
|
baseURL, err := base.ParseURL("rtsp://localhost:8554/ondemand/")
|
|
require.NoError(t, err)
|
|
|
|
track, err := gortsplib.NewTrackH264(96,
|
|
&gortsplib.TrackConfigH264{SPS: []byte{0x01, 0x02, 0x03, 0x04}, PPS: []byte{0x01, 0x02, 0x03, 0x04}})
|
|
require.NoError(t, err)
|
|
|
|
track.Media.Attributes = append(track.Media.Attributes, psdp.Attribute{
|
|
Key: "control",
|
|
Value: "trackID=0",
|
|
})
|
|
|
|
_, err = c.Setup(true, baseURL, track, 0, 0)
|
|
require.NoError(t, err)
|
|
}
|
|
}()
|
|
|
|
for {
|
|
_, err := os.Stat(doneFile)
|
|
if err == nil {
|
|
break
|
|
}
|
|
time.Sleep(100 * time.Millisecond)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCoreHotReloading(t *testing.T) {
|
|
confPath := filepath.Join(os.TempDir(), "rtsp-conf")
|
|
|
|
err := ioutil.WriteFile(confPath, []byte("paths:\n"+
|
|
" test1:\n"+
|
|
" publishUser: myuser\n"+
|
|
" publishPass: mypass\n"),
|
|
0o644)
|
|
require.NoError(t, err)
|
|
defer os.Remove(confPath)
|
|
|
|
p, ok := New([]string{confPath})
|
|
require.Equal(t, true, ok)
|
|
defer p.close()
|
|
|
|
func() {
|
|
track, err := gortsplib.NewTrackH264(96,
|
|
&gortsplib.TrackConfigH264{SPS: []byte{0x01, 0x02, 0x03, 0x04}, PPS: []byte{0x01, 0x02, 0x03, 0x04}})
|
|
require.NoError(t, err)
|
|
|
|
c := gortsplib.Client{}
|
|
|
|
err = c.StartPublishing(
|
|
"rtsp://localhost:8554/test1",
|
|
gortsplib.Tracks{track})
|
|
require.EqualError(t, err, "bad status code: 401 (Unauthorized)")
|
|
}()
|
|
|
|
err = ioutil.WriteFile(confPath, []byte("paths:\n"+
|
|
" test1:\n"),
|
|
0o644)
|
|
require.NoError(t, err)
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
func() {
|
|
track, err := gortsplib.NewTrackH264(96,
|
|
&gortsplib.TrackConfigH264{SPS: []byte{0x01, 0x02, 0x03, 0x04}, PPS: []byte{0x01, 0x02, 0x03, 0x04}})
|
|
require.NoError(t, err)
|
|
|
|
conn := gortsplib.Client{}
|
|
|
|
err = conn.StartPublishing(
|
|
"rtsp://localhost:8554/test1",
|
|
gortsplib.Tracks{track})
|
|
require.NoError(t, err)
|
|
defer conn.Close()
|
|
}()
|
|
}
|