Skip to content

Cast: fix device connection lifecycle and implement joinApplication#3351

Open
FloodExLLC wants to merge 4 commits intomicrog:masterfrom
FloodExLLC:fix/cast-connect-and-route-controller
Open

Cast: fix device connection lifecycle and implement joinApplication#3351
FloodExLLC wants to merge 4 commits intomicrog:masterfrom
FloodExLLC:fix/cast-connect-and-route-controller

Conversation

@FloodExLLC
Copy link

Fixes the two root causes that prevent Cast from working in practice.

Problem

The ChromeCast object (from the chromecast-java-api-v2 library) requires
an explicit connect() call to open a TLS socket before any operation can
succeed. The existing code created the ChromeCast instance but never called
connect(), so launchApp(), sendRawRequest(), stopSession() etc. all
threw IOException immediately, making Cast non-functional for all apps.

A second issue was that joinApplication() always launched a new instance
instead of joining an existing session, breaking multi-sender scenarios
and session resume.

Changes

CastDeviceControllerImpl

  • Add ensureConnected() helper that calls chromecast.connect() if not
    already connected; invoke it at the start of every outgoing operation
  • Fix launchApplication() to handle a null app response gracefully
    rather than NPE-ing on app.sessionId
  • Implement joinApplication() correctly: query device status, join
    the already-running matching session (wasLaunched = false), fall back
    to launching only when the app is not present

CastMediaRouteController

  • Implement onSelect(): open TLS connection when user selects route
  • Implement onUnselect() / onUnselect(int): close connection on deselect
  • Implement onRelease(): close connection when controller is destroyed

Closes #580

The ChromeCast object was created but connect() was never called
before launchApp(), sendRawRequest() etc., causing all outgoing
operations to fail with IOException because no socket existed.

- Add ensureConnected() helper; call it at the top of every
  outgoing operation (launchApplication, joinApplication,
  sendMessage, stopApplication)
- Fix launchApplication() to guard against a null Application
  response instead of NPE-ing on app.sessionId
- Implement joinApplication() properly: query device status,
  join an already-running matching session (wasLaunched=false),
  and only fall back to launching when the app is absent
onSelect() and onUnselect() were stubs. The Cast device connection
lifecycle must mirror the route selection lifecycle so the socket
is open before the Cast session begins and closed when the user
switches away.

- onSelect(): open TCP/TLS connection to the Cast device
- onUnselect() / onUnselect(int): close the connection
- onRelease(): close the connection when the controller is destroyed
@Excellencedev
Copy link

Does not even build which makes it clear you didn't test this

@Tthecreator
Copy link

Tthecreator commented Mar 22, 2026

Hey! Thank you for this PR. Seems on the surface you have done some great work.
I have seen the claim you put out on my bounty for this on BountyHub. But I also see it is not compiling yet, so please implement it properly.
I am a bit loaded at the moment, but I would like to properly test that it works first. For that I will make a test build and then I would like to see it working on my TV with Youtube native, Youtube Revanced, Youtube Music, Crunchyroll, General screencast and either one of Netflix/Hulu/Prime.

FloodExLLC added 2 commits March 22, 2026 09:04
chromecast.connect() throws both IOException and GeneralSecurityException
(a checked exception). The ensureConnected() helper only declared
throws IOException, causing a compile error.

Wrap the connect() call to catch GeneralSecurityException and rethrow
as IOException so all callers remain unchanged.

Fixes: microg#3351
….onSelect()

Same fix as CastDeviceControllerImpl: chromecast.connect() throws both
IOException and GeneralSecurityException. Update the catch clause to
handle both using a multi-catch.
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.

What's the status on Google Cast implementation?

3 participants