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-10-17 10:26:24 +00:00
|
|
|
|
|
|
|
"gitlab.crans.org/nounous/ghostream/stream"
|
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) {
|
2020-10-09 21:37:08 +00:00
|
|
|
// Split 127.0.0.1:1234
|
|
|
|
host, port, err := splitHostPort("127.0.0.1:1234")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to split host and port, %s", err)
|
|
|
|
}
|
2020-09-29 16:52:27 +00:00
|
|
|
if host != "127.0.0.1" || port != 1234 {
|
|
|
|
t.Errorf("splitHostPort returned %v:%d != 127.0.0.1:1234", host, port)
|
|
|
|
}
|
|
|
|
|
2020-10-09 21:37:08 +00:00
|
|
|
// Split :1234
|
|
|
|
host, port, err = splitHostPort(":1234")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to split host and port, %s", err)
|
|
|
|
}
|
2020-09-29 16:52:27 +00:00
|
|
|
if host != "0.0.0.0" || port != 1234 {
|
|
|
|
t.Errorf("splitHostPort returned %v:%d != 0.0.0.0:1234", host, port)
|
|
|
|
}
|
2020-10-09 21:37:08 +00:00
|
|
|
|
|
|
|
// Split demo, should fail
|
2020-10-14 19:33:51 +00:00
|
|
|
_, _, err = splitHostPort("demo")
|
2020-10-09 21:37:08 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("splitHostPort managed to split unsplitable hostport")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split demo:port, should fail
|
2020-10-14 19:33:51 +00:00
|
|
|
_, _, err = splitHostPort("demo:port")
|
2020-10-09 21:37:08 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("splitHostPort managed to split unsplitable hostport")
|
|
|
|
}
|
2020-09-29 16:52:27 +00:00
|
|
|
}
|
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-17 10:26:24 +00:00
|
|
|
// Init streams messaging and SRT server
|
2020-10-17 11:43:16 +00:00
|
|
|
streams := make(map[string]*stream.Stream)
|
2020-10-17 10:26:24 +00:00
|
|
|
go Serve(streams, nil, &Options{Enabled: true, ListenAddress: ":9711", MaxClients: 2})
|
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
|
|
|
}
|