2020-09-30 13:07:36 +00:00
|
|
|
package forwarding
|
2020-10-01 09:20:34 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"os/exec"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2020-10-04 18:16:29 +00:00
|
|
|
|
|
|
|
"gitlab.crans.org/nounous/ghostream/stream/srt"
|
2020-10-01 09:20:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestServeSRT Serve a SRT server, stream content during 5 seconds and ensure that it is well received
|
|
|
|
func TestForwardStream(t *testing.T) {
|
2020-10-04 18:16:29 +00:00
|
|
|
// Start virtual RTMP server with ffmpeg
|
2020-10-04 18:42:16 +00:00
|
|
|
forwardedFfmpeg := exec.Command("ffmpeg", "-hide_banner", "-loglevel", "error",
|
|
|
|
"-y", // allow overwrite /dev/null
|
2020-10-01 18:53:26 +00:00
|
|
|
"-listen", "1", "-i", "rtmp://127.0.0.1:1936/live/app", "-f", "null", "-c", "copy", "/dev/null")
|
2020-10-01 09:20:34 +00:00
|
|
|
forwardingErrOutput, err := forwardedFfmpeg.StderrPipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Error while querying ffmpeg forwardingOutput:", err)
|
|
|
|
}
|
|
|
|
if err := forwardedFfmpeg.Start(); err != nil {
|
|
|
|
t.Fatal("Error while starting forwarding stream ffmpeg instance:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
scanner := bufio.NewScanner(forwardingErrOutput)
|
|
|
|
for scanner.Scan() {
|
2020-10-04 19:05:42 +00:00
|
|
|
t.Fatalf("ffmpeg virtual RTMP server returned %s", scanner.Text())
|
2020-10-01 09:20:34 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
forwardingList := make(map[string][]string)
|
|
|
|
forwardingList["demo"] = []string{"rtmp://127.0.0.1:1936/live/app"}
|
2020-10-01 10:00:59 +00:00
|
|
|
|
2020-10-04 18:16:29 +00:00
|
|
|
forwardingChannel := make(chan srt.Packet)
|
2020-10-01 10:00:59 +00:00
|
|
|
|
2020-10-01 09:20:34 +00:00
|
|
|
// Register forwarding stream list
|
2020-10-04 18:16:29 +00:00
|
|
|
go Serve(forwardingChannel, forwardingList)
|
2020-10-01 09:20:34 +00:00
|
|
|
|
2020-10-04 18:16:29 +00:00
|
|
|
// Serve SRT Server without authentification backend
|
2020-10-09 20:24:01 +00:00
|
|
|
go srt.Serve(&srt.Options{Enabled: true, ListenAddress: ":9712", MaxClients: 2}, nil, forwardingChannel, nil)
|
2020-10-01 09:20:34 +00:00
|
|
|
|
2020-10-04 18:42:16 +00:00
|
|
|
ffmpeg := exec.Command("ffmpeg", "-hide_banner", "-loglevel", "error",
|
2020-10-01 18:53:26 +00:00
|
|
|
"-re", "-f", "lavfi", "-i", "testsrc=size=640x480:rate=10",
|
2020-10-02 20:39:38 +00:00
|
|
|
"-f", "flv", "srt://127.0.0.1:9712?streamid=demo:")
|
2020-10-01 09:20:34 +00:00
|
|
|
|
|
|
|
errOutput, err := ffmpeg.StderrPipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Error while querying ffmpeg forwardingOutput:", 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-10-01 09:20:34 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-10-04 18:52:23 +00:00
|
|
|
time.Sleep(5 * time.Second) // Delay is in nanoseconds, here 5s
|
2020-10-01 09:20:34 +00:00
|
|
|
|
2020-10-09 20:31:19 +00:00
|
|
|
err = ffmpeg.Process.Kill()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error while killing ffmpeg: already died?")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = forwardedFfmpeg.Process.Kill()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error while killing ffmpeg: already died?")
|
|
|
|
}
|
|
|
|
|
2020-10-01 10:07:28 +00:00
|
|
|
// TODO Kill SRT server
|
2020-10-01 09:20:34 +00:00
|
|
|
}
|