# Chromecast

Casting sends the current episode to Google Cast's Default Media Receiver
(`CC1AD845`) and queues the following episodes. The protocol, queue, status
parsing and discovery codec are shared in `services/media/cast`.

## Behavior

- The player's Cast button appears when the platform reports Cast support.
  Native platforms show a device dialog that runs discovery only while open;
  the web uses Chrome's own picker.
- Starting a cast pauses local playback, loads the current stream at the local
  position and appends up to two following episodes as their streams resolve.
  Leaving the player cancels unfinished extraction but not the cast.
- The cast panel replaces the centre controls. It shows the device and current
  item, with play/pause, seek, previous/next, a queue menu and Stop casting.
  In the minimized player it shows the compact form without the seek bar.
- Stopping the cast stops the receiver. If it was playing this episode, local
  playback seeks to the receiver's position.
- Subtitles: `.vtt` tracks are sent as text tracks. The selected track is
  enabled on the first item. The receiver's text style uses the stored player
  subtitle colours, bold/italic and font size (relative to 45).
- A stream is castable only when it is HTTP(S), needs no request headers and is
  not a device-local address. Otherwise another castable stream is chosen, or a
  notice explains that the episode cannot be cast.
- Failures are not retried. They surface as a notice or in the panel. Three
  missed heartbeats end the session.

## Platforms

| Platform | Implementation |
|---|---|
| Android | Shared CASTV2 client over TLS port 8009. mDNS discovery holds a Wi-Fi multicast lock (`CHANGE_WIFI_MULTICAST_STATE`). The Google Cast SDK is deliberately not used, which avoids a Play Services dependency and keeps one code path. |
| Desktop | Same CASTV2 client and mDNS discovery as Android. |
| iOS | Same CASTV2 session. Discovery uses `nw_browser` with Bonjour (`_googlecast._tcp`) and the connection uses Network.framework TLS. Requires the Local Network permission: `NSLocalNetworkUsageDescription` and `NSBonjourServices` in `Info.plist`. |
| Web | Chrome Cast Web Sender SDK, loaded lazily. Available only where the SDK reports Cast; the button is hidden elsewhere. |

The receiver's self-signed certificate is accepted only on the Cast
connection. Chromecast device authentication is not implemented.

## Limits

- The Default Media Receiver fetches media itself, so it cannot use streams
  that need request headers or cookies. The local header proxy listens only on
  loopback.
- Non-VTT subtitles are not sent. Native discovery uses the default network
  interface.
- Progress is saved from local playback when the player closes, not
  continuously from the receiver.

## Verification

2026-10-18: `CastProtocolTest` (6), `CastMdnsTest` (3) and `CastSessionTest`
(6) passed on desktop JVM and Kotlin/Wasm. They cover protobuf framing
round-trips, MEDIA_STATUS and queue mapping, VTT-only tracks and style colours,
mDNS name compression and truncation rejection, the handshake against a fake
transport, PONG replies and timeout/failure surfacing. Android, iOS simulator,
desktop and Wasm compiled. Casting to a real device was not exercised.
