Skip to content

Commit

Permalink
Add handling if mp4 client drops connection
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed May 29, 2024
1 parent a9e7a73 commit 2ab1d9d
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions internal/mp4/mp4.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mp4

import (
"context"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -127,20 +128,20 @@ func handlerMP4(w http.ResponseWriter, r *http.Request) {
header.Set("Content-Disposition", `attachment; filename="`+filename+`"`)
}

var duration *time.Timer
if s := query.Get("duration"); s != "" {
if i, _ := strconv.Atoi(s); i > 0 {
duration = time.AfterFunc(time.Second*time.Duration(i), func() {
_ = cons.Stop()
})
}
ctx := r.Context() // handle when the client drops the connection

if i := core.Atoi(query.Get("duration")); i > 0 {
timeout := time.Second * time.Duration(i)
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, timeout)
defer cancel()
}

_, _ = cons.WriteTo(w)
go func() {
<-ctx.Done()
_ = cons.Stop()
stream.RemoveConsumer(cons)
}()

stream.RemoveConsumer(cons)

if duration != nil {
duration.Stop()
}
_, _ = cons.WriteTo(w)
}

0 comments on commit 2ab1d9d

Please sign in to comment.