|
@@ -69,10 +69,21 @@ func TranscodeAndStream(w http.ResponseWriter, r *http.Request, inputFile string
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+ // Create a channel to signal when the client disconnects
|
|
|
+ done := make(chan struct{})
|
|
|
+
|
|
|
+ // Monitor client connection close
|
|
|
+ go func() {
|
|
|
+ <-r.Context().Done()
|
|
|
+ cmd.Process.Kill() // Kill the FFmpeg process when client disconnects
|
|
|
+ close(done)
|
|
|
+ }()
|
|
|
+
|
|
|
// Copy the command output to the HTTP response in a separate goroutine
|
|
|
go func() {
|
|
|
if _, err := io.Copy(w, stdout); err != nil {
|
|
|
- //End of video
|
|
|
+ // End of video or client disconnected
|
|
|
+ cmd.Process.Kill()
|
|
|
return
|
|
|
}
|
|
|
}()
|
|
@@ -85,9 +96,14 @@ func TranscodeAndStream(w http.ResponseWriter, r *http.Request, inputFile string
|
|
|
}
|
|
|
}()
|
|
|
|
|
|
- // Wait for the command to finish
|
|
|
- if err := cmd.Wait(); err != nil {
|
|
|
- http.Error(w, "FFmpeg process failed", http.StatusInternalServerError)
|
|
|
- log.Printf("FFmpeg process failed: %v", err)
|
|
|
- }
|
|
|
+ go func() {
|
|
|
+ if err := cmd.Wait(); err != nil {
|
|
|
+ log.Printf("FFmpeg process failed: %v", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ // Wait for the command to finish or client disconnect
|
|
|
+ <-done
|
|
|
+ log.Println("[Media Server] Transcode client disconnected")
|
|
|
}
|