From dc7a3fef9f4c13b45880394251d2b76a06a81672 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Thu, 1 Oct 2020 11:20:34 +0200 Subject: [PATCH] Test stream forwarding, but disable it while the project structure is not clean, ie. to avoid that forwarding is a dependency to srt --- stream/forwarding/forwarding_test.go | 95 ++++++++++++++++++++++++++++ stream/srt/srt.go | 13 ++-- 2 files changed, 101 insertions(+), 7 deletions(-) diff --git a/stream/forwarding/forwarding_test.go b/stream/forwarding/forwarding_test.go index 3fee73b..8b111c4 100644 --- a/stream/forwarding/forwarding_test.go +++ b/stream/forwarding/forwarding_test.go @@ -1 +1,96 @@ package forwarding + +import ( + "bufio" + "gitlab.crans.org/nounous/ghostream/stream/srt" + "log" + "os/exec" + "testing" + "time" +) + +// TestServeSRT Serve a SRT server, stream content during 5 seconds and ensure that it is well received +func TestForwardStream(t *testing.T) { + 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") + } + + forwardedFfmpeg := exec.Command("ffmpeg", + "-f", "flv", "-listen", "1", "-i", "rtmp://127.0.0.1:1936/live/app", "-c", "copy", "/dev/null") + forwardingOutput, err := forwardedFfmpeg.StdoutPipe() + 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(forwardingOutput) + for scanner.Scan() { + log.Printf("[FFMPEG FORWARD TEST] %s", scanner.Text()) + } + }() + + go func() { + scanner := bufio.NewScanner(forwardingErrOutput) + for scanner.Scan() { + log.Printf("[FFMPEG FORWARD ERR TEST] %s", scanner.Text()) + } + }() + + forwardingList := make(map[string][]string) + forwardingList["demo"] = []string{"rtmp://127.0.0.1:1936/live/app"} + // Register forwarding stream list + New(forwardingList) + + // Serve HTTP Server + go srt.Serve(&srt.Options{ListenAddress: ":9711", MaxClients: 2}) + + ffmpeg := exec.Command("ffmpeg", + "-i", "http://ftp.crans.org/events/Blender%20OpenMovies/big_buck_bunny_480p_stereo.ogg", + "-f", "flv", "srt://127.0.0.1:9711") + + output, err := ffmpeg.StdoutPipe() + 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(output) + for scanner.Scan() { + log.Printf("[FFMPEG TEST] %s", scanner.Text()) + } + }() + + go func() { + scanner := bufio.NewScanner(errOutput) + for scanner.Scan() { + log.Printf("[FFMPEG ERR TEST] %s", scanner.Text()) + } + }() + + time.Sleep(5000000000) // Delay is in nanoseconds, here 5s + + if ffmpegInputStreams["demo"] == nil { + t.Errorf("Stream forwarding does not appear to be working") + } + + // TODO Check that the stream ran +} diff --git a/stream/srt/srt.go b/stream/srt/srt.go index a11f173..9bab761 100644 --- a/stream/srt/srt.go +++ b/stream/srt/srt.go @@ -6,7 +6,6 @@ import ( "strconv" "github.com/haivision/srtgo" - "gitlab.crans.org/nounous/ghostream/stream/forwarding" ) // Options holds web package configuration @@ -60,10 +59,10 @@ func Serve(cfg *Options) { // Setup stream forwarding // FIXME: demo should be replaced by stream name - if err := forwarding.RegisterStream("demo"); err != nil { + /* if err := forwarding.RegisterStream("demo"); err != nil { log.Println("Error occurred during forward stream init:", err) break - } + } */ // Read RTP packets forever and send them to the WebRTC Client for { @@ -79,18 +78,18 @@ func Serve(cfg *Options) { break } - log.Printf("Received %d bytes", n) + // log.Printf("Received %d bytes", n) // Send raw packet to other streams - forwarding.SendPacket("demo", buff[:n]) + // forwarding.SendPacket("demo", buff[:n]) // TODO: Send to WebRTC // See https://github.com/ebml-go/webm/blob/master/reader.go //err := videoTrack.WriteSample(media.Sample{Data: data, Samples: uint32(sampleCount)}) } - if err := forwarding.CloseConnection("demo"); err != nil { + /* if err := forwarding.CloseConnection("demo"); err != nil { log.Printf("Failed to close forward stream: %s", err) - } + } */ } }