Skip to content

Fix slow MP4 playback via proxy (switch to net/http streaming + range handling)#710

Merged
5rahim merged 8 commits into5rahim:nextfrom
Pal-droid:fix/mp4-proxy-handling
Apr 21, 2026
Merged

Fix slow MP4 playback via proxy (switch to net/http streaming + range handling)#710
5rahim merged 8 commits into5rahim:nextfrom
Pal-droid:fix/mp4-proxy-handling

Conversation

@Pal-droid
Copy link
Copy Markdown
Contributor

Summary

This PR fixes very slow playback for certain proxied MP4 streams by changing the proxy’s upstream HTTP behavior and response semantics to better match what browsers/video players expect.

Changes

  • Use net/http client + custom transport for upstream requests:

    • ForceAttemptHTTP2: false
    • DisableCompression: true
    • Accept-Encoding: identity
    • long timeout for streaming
  • Strip RFC7230 hop-by-hop headers when proxying upstream headers.

  • For non-HLS responses:

    • send minimal MP4-friendly headers (Content-Type, Accept-Ranges, Content-Range, Content-Length when safe)
    • return 206 Partial Content when the client sends Range
    • stream response body with periodic flush and context cancellation handling.
  • Preserve upstream status code for HLS playlist responses.

Copy link
Copy Markdown
Owner

@5rahim 5rahim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the PR, but i think this is trying to fix an issue buy doing too much at once, it should be scoped to the issue at hand first (which is the non-HLS path).
i'd suggest testing things step by step, e.g. as i see it, the likely culprit can be: the timeout length from the req client, compression or hopbyhop header or all three combined. if that doesn't do it you can then conditionally keep the content-length header like you're doing. if that still doesn't do it you could first, try to overwrite the content-type, then replace c.Stream and so on.

that way in the end, we know what the root cause is for future reference.

@5rahim 5rahim marked this pull request as draft April 20, 2026 18:39
@Pal-droid
Copy link
Copy Markdown
Contributor Author

I tested the changes step-by-step (timeout, identity/no compression, hop-by-hop headers, conditional Content-Length, and replacing c.Stream). The only change that consistently fixed it was switching the non-HLS upstream fetch from req/v3 to net/http (with HTTP/2 off + identity/no compression).

@Pal-droid Pal-droid requested a review from 5rahim April 20, 2026 18:40
@5rahim
Copy link
Copy Markdown
Owner

5rahim commented Apr 20, 2026

i investigated further since you said that req/v3 was the bottleneck and indeed simply adding DisableAutoReadResponse() fixed it because it was buffering the vid until timeout.

@5rahim
Copy link
Copy Markdown
Owner

5rahim commented Apr 20, 2026

i can still merge it as a contribution but you'll have to revert everything and add DisableAutoReadResponse(). after req.C()., the other changes aren't necessary

@5rahim 5rahim marked this pull request as ready for review April 20, 2026 19:48
@5rahim 5rahim marked this pull request as draft April 20, 2026 19:48
Comment thread internal/handlers/proxy.go
Comment thread internal/handlers/proxy.go Outdated
Comment thread internal/handlers/proxy.go Outdated
Removed unused videoProxyHTTPClient and related code for upstream requests. Updated header handling to use videoProxyClient2.
Removed comment about handling marshalling errors in toProxyURL function.
Removed unused 'time' import from proxy.go
@5rahim 5rahim marked this pull request as ready for review April 21, 2026 08:45
@Pal-droid
Copy link
Copy Markdown
Contributor Author

Pal-droid commented Apr 21, 2026

made those changes and noticed playback was fine but seeking was kinda slow

@Pal-droid Pal-droid requested a review from 5rahim April 21, 2026 09:08
@5rahim 5rahim changed the base branch from main to next April 21, 2026 10:23
@5rahim 5rahim merged commit f26d51b into 5rahim:next Apr 21, 2026
1 check passed
@Pal-droid Pal-droid deleted the fix/mp4-proxy-handling branch April 21, 2026 10:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants