@@ -10,6 +10,7 @@ import (
10
10
"log/slog"
11
11
"net/http"
12
12
"net/url"
13
+ "os/exec"
13
14
"strings"
14
15
"time"
15
16
@@ -86,6 +87,7 @@ func startAIMediaServer(ls *LivepeerServer) error {
86
87
ls .HTTPMux .Handle ("/live/video-to-video/{stream}/start" , ls .StartLiveVideo ())
87
88
ls .HTTPMux .Handle ("/live/video-to-video/{prefix}/{stream}/start" , ls .StartLiveVideo ())
88
89
ls .HTTPMux .Handle ("/live/video-to-video/{stream}/update" , ls .UpdateLiveVideo ())
90
+ ls .HTTPMux .Handle ("/live/video-to-video/{stream}/smoketest" , ls .SmokeTestLiveVideo ())
89
91
90
92
// Stream status
91
93
ls .HTTPMux .Handle ("/live/video-to-video/{streamId}/status" , ls .GetLiveVideoToVideoStatus ())
@@ -652,3 +654,58 @@ func (ls *LivepeerServer) cleanupLive(stream string) {
652
654
pub .StopControl ()
653
655
}
654
656
}
657
+
658
+ // Default to using an FFMPEG test card
659
+ var ffmpegParams = []string {
660
+ "-re" ,
661
+ "-f" , "lavfi" ,
662
+ "-i" , "testsrc=size=1920x1080:rate=30,format=yuv420p" ,
663
+ "-f" , "lavfi" ,
664
+ "-i" , "sine" ,
665
+ "-c:v" , "libx264" ,
666
+ "-b:v" , "1000k" ,
667
+ "-x264-params" , "keyint=60" ,
668
+ "-c:a" , "aac" ,
669
+ "-f" , "flv" ,
670
+ }
671
+
672
+ func (ls * LivepeerServer ) SmokeTestLiveVideo () http.Handler {
673
+ return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
674
+ //ctx := r.Context()
675
+ if r .Method != http .MethodPut {
676
+ http .Error (w , "Method not allowed" , http .StatusMethodNotAllowed )
677
+ return
678
+ }
679
+ // Get stream from path param
680
+ stream := r .PathValue ("stream" )
681
+ if stream == "" {
682
+ http .Error (w , "Missing stream name" , http .StatusBadRequest )
683
+ return
684
+ }
685
+
686
+ rtmpURL := "rtmp://ai.livepeer.com:1935"
687
+ ingestURL := fmt .Sprintf ("%s/%s" , rtmpURL , stream )
688
+ params := append (ffmpegParams , ingestURL )
689
+
690
+ ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Minute )
691
+ cmd := exec .CommandContext (ctx , "ffmpeg" , params ... )
692
+ var outputBuf bytes.Buffer
693
+ var stdErr bytes.Buffer
694
+ cmd .Stdout = & outputBuf
695
+ cmd .Stderr = & stdErr
696
+
697
+ if err := cmd .Start (); err != nil {
698
+ cancel ()
699
+ clog .Errorf (ctx , "failed to start ffmpeg. Error: %s\n Command: ffmpeg %s" , err , strings .Join (params , " " ))
700
+ http .Error (w , "Failed to start stream" , http .StatusInternalServerError )
701
+ }
702
+
703
+ go func () {
704
+ defer cancel ()
705
+ if state , err := cmd .Process .Wait (); err != nil || state .ExitCode () != 0 {
706
+ clog .Errorf (ctx , "failed to run ffmpeg. Exit Code: %d, Error: %s\n Command: ffmpeg %s\n " , state .ExitCode (), err , strings .Join (ffmpegParams , " " ))
707
+ clog .Errorf (ctx , "ffmpeg output:\n %s\n %s\n " , outputBuf .String (), stdErr .String ())
708
+ }
709
+ }()
710
+ })
711
+ }
0 commit comments