fix(stream): no anunciar un total falso mientras el remux crece (loop de re-seek)
serveGrowing anunciaba en Content-Range total = EstimatedSize() = el tamaño del MKV fuente mientras ffmpeg aún corría. Pero el fMP4 resultante no mide eso (el audio re-encodea a AAC y la fragmentación cambian el byte count), así que el <video> nativo mapeaba su timeline sobre una longitud falsa, pedía offsets que no cuadraban, re-seekeaba y reabría la conexión cientos de veces por segundo (el loop de reproducción remux). Mientras crece (!Final) la longitud real es DESCONOCIDA: ahora se sirve Content-Range "bytes start-end/*" (RFC 7233 §4.2) sin Content-Length, y el cliente lee secuencial en vez de re-seekear. Cuando ffmpeg termina, el tamaño real se conoce y se anuncia como antes. El 416 y el Content-Length del HEAD solo cuando el total es real (final).
This commit is contained in:
parent
9ab0763f8a
commit
5f2d1cdc70
2 changed files with 66 additions and 20 deletions
|
|
@ -1467,25 +1467,38 @@ func (ss *StreamServer) serveGrowing(w http.ResponseWriter, r *http.Request, src
|
|||
w.Header().Set("Content-Type", "video/mp4")
|
||||
w.Header().Set("Content-Disposition", fmt.Sprintf("inline; filename=%q", src.FileName()))
|
||||
|
||||
// Total to advertise: exact when ffmpeg has exited, else the estimate.
|
||||
total := src.EstimatedSize()
|
||||
if src.Final() {
|
||||
total = src.Size()
|
||||
// The instance length is KNOWN only once ffmpeg has exited. While the remux
|
||||
// is still growing, the final size is genuinely unknown — the source MKV
|
||||
// size is NOT it (the audio re-encode to AAC + fMP4 fragmentation change the
|
||||
// byte count). Advertising that wrong total made the native <video> map its
|
||||
// timeline onto a bogus length, request byte offsets that didn't line up,
|
||||
// re-seek, and reopen the connection hundreds of times a second (the remux
|
||||
// playback loop). Per RFC 7233 §4.2 we now send "/*" (unknown total) while
|
||||
// growing, so the player streams sequentially instead of re-seeking against
|
||||
// a fake size. `end` uses the estimate only as an upper-bound hint.
|
||||
final := src.Final()
|
||||
total := src.Size()
|
||||
if !final {
|
||||
total = src.EstimatedSize()
|
||||
}
|
||||
if total <= 0 {
|
||||
total = src.Size()
|
||||
}
|
||||
|
||||
start, explicitEnd := parseByteRange(r.Header.Get("Range"))
|
||||
if total > 0 && start >= total {
|
||||
// Range beyond what we expect to produce — let the browser recover.
|
||||
// A 416 is only sound against a KNOWN total. While growing we can't say a
|
||||
// start is unsatisfiable (more bytes are still coming), so only guard when
|
||||
// final.
|
||||
if final && total > 0 && start >= total {
|
||||
// Range beyond the real end — let the browser recover.
|
||||
w.Header().Set("Content-Range", fmt.Sprintf("bytes */%d", total))
|
||||
http.Error(w, "range not satisfiable", http.StatusRequestedRangeNotSatisfiable)
|
||||
return
|
||||
}
|
||||
|
||||
if r.Method == http.MethodHead {
|
||||
if total > 0 {
|
||||
// Only promise a length we actually know (final). While growing, omit it.
|
||||
if final && total > 0 {
|
||||
w.Header().Set("Content-Length", strconv.FormatInt(total, 10))
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
|
@ -1496,13 +1509,23 @@ func (ss *StreamServer) serveGrowing(w http.ResponseWriter, r *http.Request, src
|
|||
if explicitEnd >= 0 && explicitEnd < end {
|
||||
end = explicitEnd
|
||||
}
|
||||
if total > 0 {
|
||||
w.Header().Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", start, end, total))
|
||||
if end < start {
|
||||
end = start
|
||||
}
|
||||
// Exact Content-Length only when the source is final (true size known) so
|
||||
// we never promise bytes a still-running remux might not produce.
|
||||
if src.Final() && explicitEnd < 0 {
|
||||
w.Header().Set("Content-Length", strconv.FormatInt(src.Size()-start, 10))
|
||||
if final {
|
||||
if total > 0 {
|
||||
w.Header().Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", start, end, total))
|
||||
}
|
||||
// Exact Content-Length only when final (true size known) so we never
|
||||
// promise bytes a still-running remux might not produce.
|
||||
if explicitEnd < 0 {
|
||||
w.Header().Set("Content-Length", strconv.FormatInt(src.Size()-start, 10))
|
||||
}
|
||||
} else {
|
||||
// Growing: honest "unknown total" so the player doesn't re-seek against
|
||||
// a wrong size. No Content-Length (chunked) — bytes flow as ffmpeg makes
|
||||
// them and the read loop below blocks at the live edge.
|
||||
w.Header().Set("Content-Range", fmt.Sprintf("bytes %d-%d/*", start, end))
|
||||
}
|
||||
w.WriteHeader(http.StatusPartialContent)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue