2020-09-28 19:18:49 +00:00
|
|
|
package srt
|
2020-09-29 16:52:27 +00:00
|
|
|
|
|
|
|
import (
|
2020-09-30 14:53:15 +00:00
|
|
|
"bufio"
|
|
|
|
"os/exec"
|
2020-09-29 16:52:27 +00:00
|
|
|
"testing"
|
2020-09-30 14:53:15 +00:00
|
|
|
"time"
|
2020-09-29 16:52:27 +00:00
|
|
|
)
|
|
|
|
|
2020-09-30 14:53:15 +00:00
|
|
|
// TestSplitHostPort Try to split a host like 127.0.0.1:1234 in host, port (127.0.0.1, 1234à
|
2020-09-29 16:52:27 +00:00
|
|
|
func TestSplitHostPort(t *testing.T) {
|
|
|
|
host, port := splitHostPort("127.0.0.1:1234")
|
|
|
|
if host != "127.0.0.1" || port != 1234 {
|
|
|
|
t.Errorf("splitHostPort returned %v:%d != 127.0.0.1:1234", host, port)
|
|
|
|
}
|
|
|
|
|
|
|
|
host, port = splitHostPort(":1234")
|
|
|
|
if host != "0.0.0.0" || port != 1234 {
|
|
|
|
t.Errorf("splitHostPort returned %v:%d != 0.0.0.0:1234", host, port)
|
|
|
|
}
|
|
|
|
}
|
2020-09-30 14:53:15 +00:00
|
|
|
|
|
|
|
// TestServeSRT Serve a SRT server, stream content during 5 seconds and ensure that it is well received
|
|
|
|
func TestServeSRT(t *testing.T) {
|
2020-09-30 17:28:30 +00:00
|
|
|
which := exec.Command("which", "ffmpeg")
|
|
|
|
if err := which.Start(); err != nil {
|
|
|
|
t.Fatal("Error while checking if ffmpeg got installed:", err)
|
|
|
|
}
|
|
|
|
state, err := which.Process.Wait()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Error while checking if ffmpeg got installed:", err)
|
|
|
|
}
|
|
|
|
if state.ExitCode() != 0 {
|
|
|
|
// FFMPEG is not installed
|
|
|
|
t.Skip("WARNING: FFMPEG is not installed. Skipping stream test")
|
|
|
|
}
|
|
|
|
|
2020-10-04 18:16:29 +00:00
|
|
|
go Serve(&Options{ListenAddress: ":9711", MaxClients: 2}, nil, nil, nil)
|
2020-09-30 14:53:15 +00:00
|
|
|
|
2020-10-04 18:52:23 +00:00
|
|
|
ffmpeg := exec.Command("ffmpeg", "-hide_banner", "-loglevel", "error",
|
|
|
|
"-f", "lavfi", "-i", "testsrc=size=640x480:rate=10",
|
2020-10-02 20:39:38 +00:00
|
|
|
"-f", "flv", "srt://127.0.0.1:9711?streamid=demo:")
|
2020-09-30 14:53:15 +00:00
|
|
|
|
|
|
|
errOutput, err := ffmpeg.StderrPipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Error while querying ffmpeg output:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ffmpeg.Start(); err != nil {
|
|
|
|
t.Fatal("Error while starting ffmpeg:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
scanner := bufio.NewScanner(errOutput)
|
|
|
|
for scanner.Scan() {
|
2020-10-04 19:05:42 +00:00
|
|
|
t.Fatalf("ffmpeg virtual source returned %s", scanner.Text())
|
2020-09-30 14:53:15 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-10-04 18:52:23 +00:00
|
|
|
time.Sleep(5 * time.Second) // Delay is in nanoseconds, here 5s
|
2020-09-30 14:53:15 +00:00
|
|
|
|
2020-10-01 10:07:28 +00:00
|
|
|
// TODO Kill SRT server
|
2020-09-30 14:53:15 +00:00
|
|
|
}
|