fix(stream): fix black screen on remote/Tailscale streaming

Three root-cause fixes for VLC showing a black screen when opening a
stream from a different network or via Tailscale:

1. PrioritizeTail: when VLC opens an MKV/MP4 stream it immediately seeks
   to the end of the file to read the container index (seekhead/moov
   atom). For active torrents those end-pieces aren't downloaded yet, so
   the reader blocks indefinitely. PrioritizeTail() opens a background
   reader positioned at the last 5 MB, keeping those pieces at high
   priority until ctx is cancelled or they finish downloading.

2. /health endpoint: GET /health returns a lightweight JSON response
   {"status":"ok","streaming":bool,...} so connectivity can be tested
   with a simple curl from any device before involving VLC.

3. Per-request logging: every incoming /stream request now logs the
   client IP and Range header, making it trivial to confirm whether
   remote/Tailscale clients are reaching the server at all.
This commit is contained in:
Deivid Soto 2026-04-09 16:15:41 +02:00
parent 7eaf357680
commit f1b4f2e327
5 changed files with 185 additions and 0 deletions

View file

@ -380,3 +380,31 @@ func (r *responseRecorder) ReadFrom(src io.Reader) (int64, error) {
n, err := io.Copy(r.body, src)
return n, err
}
// TestPrioritizeTail_SmallFile verifica que PrioritizeTail no lanza goroutine
// cuando el archivo es demasiado pequeño (≤ 2×tailBytes).
func TestPrioritizeTail_SmallFile(t *testing.T) {
s := &StreamEngine{
totalBytes: 5 * 1024 * 1024, // 5 MB — menor que 2×5 MB
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// No debe entrar en pánico ni bloquear con file == nil
s.PrioritizeTail(ctx, 5*1024*1024)
// Si llega aquí sin pánico, el test pasa
}
// TestPrioritizeTail_NilFile verifica que PrioritizeTail es seguro cuando
// file es nil (engine no inicializado).
func TestPrioritizeTail_NilFile(t *testing.T) {
s := &StreamEngine{
totalBytes: 100 * 1024 * 1024,
file: nil,
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s.PrioritizeTail(ctx, 5*1024*1024)
// No debe entrar en pánico
}