Skip to content

Fix Cast playback instability by implementing efficient streaming server#864

Draft
google-labs-jules[bot] wants to merge 4 commits intocodex/fix-cast-function-connection-issuefrom
fix-cast-streaming-2302808141203345247
Draft

Fix Cast playback instability by implementing efficient streaming server#864
google-labs-jules[bot] wants to merge 4 commits intocodex/fix-cast-function-connection-issuefrom
fix-cast-streaming-2302808141203345247

Conversation

@google-labs-jules
Copy link
Contributor

Refactored MediaFileHttpServerService to address casting failures. The previous implementation loaded entire audio files into memory, causing high latency and potential OOM crashes, which led to Cast devices timing out or rejecting the stream. The new implementation uses respondOutputStream to stream data efficiently with a small buffer, and correctly handles HTTP Range requests required by Cast protocols.


PR created automatically by Jules for task 2302808141203345247 started by @theovilardo

- Replaced `readBytes()` and `readNBytes()` with `call.respondOutputStream` and a buffered copy loop to prevent OOM errors and reduce latency.
- Implemented strict `Range` request handling with manual `Content-Length` and `Content-Range` headers to ensure compatibility with Cast devices.
- Ensures the server keeps the connection open and streams data efficiently, addressing the "Invalid Request" and connection drop issues.
@google-labs-jules
Copy link
Contributor Author

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

- Removed the "AirReceiver" detection logic that forced a "minimal queue" mode (legacy load). This heuristic was causing failures.
- Enabled standard `QueueLoad` and `MediaLoadRequestData` for all devices, ensuring full metadata and album art support.
- Updated `resolveCastMimeType` to map `audio/mpeg` (and default MP3s) to `audio/mp3` to address strict or legacy MIME type validation in some receivers.
- This change, combined with the streaming server refactor, should resolve the "Invalid Request" errors and connection drops.
- Fixed `MediaLoadRequestData.setCurrentTime` call to use milliseconds (Long) instead of seconds (Double), preventing potential API mismatches.
- Simplified `MediaQueueItem` and `MediaInfo` construction by temporarily disabling `CustomData` and `Images`. This reduces the likelihood of the receiver rejecting the payload due to parsing errors or fetch failures.
- Reverted MIME type logic to prefer standard `audio/mpeg` for MP3s while maintaining the extension handling.
- These changes address the persistent "Invalid Request" error on `queueLoad` and `load` calls.
- Switched to an "Ultra Minimal" load strategy:
  - Disabled `queueLoad` entirely; now only attempts `load(MediaLoadRequestData)` with a single item.
  - Stripped ALL optional fields from `MediaInfo`: removed `Metadata`, `Images`, `CustomData`, and `StreamDuration`.
  - Forced `currentTime` to `0L` to prevent any seeking logic from triggering rejection.
  - Retained `audio/mpeg` MIME type and `STREAM_TYPE_BUFFERED`.
- This drastic simplification aims to identify and bypass whatever validation rule is causing the persistent "Invalid Request" error on strict receivers like AirReceiver.
- Once basic playback is confirmed working, features like metadata and queueing can be progressively restored.
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.

0 participants

Comments