Skip to content

Commit 0e60c16

Browse files
committed
Add smoke test endpoint
1 parent 56ab75e commit 0e60c16

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

server/ai_mediaserver.go

+57
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"log/slog"
1111
"net/http"
1212
"net/url"
13+
"os/exec"
1314
"strings"
1415
"time"
1516

@@ -86,6 +87,7 @@ func startAIMediaServer(ls *LivepeerServer) error {
8687
ls.HTTPMux.Handle("/live/video-to-video/{stream}/start", ls.StartLiveVideo())
8788
ls.HTTPMux.Handle("/live/video-to-video/{prefix}/{stream}/start", ls.StartLiveVideo())
8889
ls.HTTPMux.Handle("/live/video-to-video/{stream}/update", ls.UpdateLiveVideo())
90+
ls.HTTPMux.Handle("/live/video-to-video/{stream}/smoketest", ls.SmokeTestLiveVideo())
8991

9092
// Stream status
9193
ls.HTTPMux.Handle("/live/video-to-video/{streamId}/status", ls.GetLiveVideoToVideoStatus())
@@ -652,3 +654,58 @@ func (ls *LivepeerServer) cleanupLive(stream string) {
652654
pub.StopControl()
653655
}
654656
}
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\nCommand: 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\nCommand: 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

Comments
 (0)