Casting architecture
Immich's web and mobile apps support casting photos and videos to a Chromecast device. Such Cast devices run a "receiver" app that fetches photos and videos directly from the Immich server. The user controls the Cast device through the sender app and navigates between photos and videos which is then reflected on the screen.
Components and data flow
- The user clicks the Cast button on the sender (i.e. mobile or web app)
- The sender discovers available Cast devices and presents a list to the user
- The user selects a Cast device
- The sender sends the Immich Custom Receiver application ID to the Cast device
- The Cast device connects to Google's server and queries for the application URL using the application ID
- The sender creates a temporary Immich session (15 minutes plus the video duration, when known) and adds its token as the
sessionKeyquery parameter to each media URL. - When the user selects a photo or video on the sender device, the preview URL of this media is sent to the Cast device together with the session key
- The receiver requests the media from the Immich server over HTTPS. Asset media responses with a
sessionKeyinclude headers that allow the receiver's cross-origin request.
Immich custom receiver
For performance reasons, Immich uses a Custom Receiver application since the other options did not give us enough control over the receiver application. Performance was an important consideration for this since the custom app allows us to preload next and previous assets, greatly speeding up the navigation.
For a photo, the sender sends SHOW_PHOTO with the current photo's URL, optional previous and next photo URLs, and an optional thumbnail fallback URL. The receiver fetches and displays the current photo, preloads its neighbors, and responds with PHOTO_READY or PHOTO_ERROR. CLEAR_PHOTO clears the display. For these photo messages, the receiver accepts URLs only when their origin matches its own and their path starts with /api/assets/.
Videos use the standard Cast media namespace. The sender loads a one-item queue with repeat enabled and marks the video with immichLoop in the media's custom data; the custom receiver uses that flag to loop its video element. Cast first sends a direct playback or original-media URL. If loading or playback fails, the sender retries once using the asset's HLS main playlist, resuming from the last known position. The original contentId is retained while contentUrl points at HLS, so sender controls continue tracking the same asset. Real-time transcoding must be enabled on the server and the asset must have streaming metadata; otherwise the HLS attempt fails normally.
The receiver uses CAF's Shaka HLS player with fragmented MP4 segments. Its manifest and segment request hooks add the Cast session token only to URLs inside the selected asset's streaming path. It releases HLS sessions on media changes, stop, errors, and receiver shutdown. It periodically sends a HEAD request for the last segment to keep the session alive while paused or looping buffered media, without downloading that segment again. Authentication still expires after its allotted duration; a prolonged pause or repeated looping beyond that window requires reloading the video.
Local development
The receiver application must be hosted on a publicly available location for all Immich instances to reach. Immich hosts a central application at (URL to be determined), hosted as static assets on a Cloudflare Worker. This application is registered on the (Google Cast SDK Developer Console)[https://cast.google.com/publish/] under the Immich-managed account.
When developing the custom receiver app, do the following steps:
- Create a Google Cast Developer Account in the Google Cast SDK Developer Console. Google charges a one-time, non-refundable $5 USD registration fee. Complete the account details and allow up to 48 hours for registration to finish.
- Set up your Cast device with the Google Home app and connect it to the same Wi-Fi network as your computer.
- In the Developer Console, choose Add new application and select Custom Receiver. Enter the receiver URL
https://your-immich-host/cast/receiver.htmland save. Record the application ID assigned by Google. - Register your Cast device in the same Developer Console: choose Add new device, enter its serial number and a description, then save. Wait at least 15 minutes for registration, then restart the device.
- Ensure the Cast device can reach the receiver page and media URLs over HTTPS.
- In the web app, enter the application ID under Account Settings > Features > Cast > Receiver application ID override. It is saved in that browser's local storage, not to the Immich server or your account, and applies only in that browser. To use the custom receiver on another browser, set the override there too.
- In the mobile app, enter it under Settings > Advanced > Receiver application ID override. This is stored locally on that device.
Testing
Unfortunately, automated testing is not possible with casting, so we have to do these test scenarios manually. A few acceptance tests are listed here and should be done from both web, Android, and iOS:
- Start casting and confirm the Cast picker opens and launches the custom Immich receiver and it loads the Immich splash screen.
- Cast a photo. Confirm it loads, then navigate to the previous and next photos using the sender. Navigate quickly across several photos and confirm the receiver ends on the selected photo. There should be no black frames between loading photos.
- With real-time transcoding enabled, cast a video the receiver cannot play directly. Confirm it retries as HLS, adapts quality, and supports seeking. Pause for more than five minutes and resume, then switch to a photo and confirm the HLS session is released. Repeat with transcoding disabled and confirm the failure does not cause repeated retries.
- Cast a video longer than 15 minutes and confirm authenticated segment requests continue for the full video.
- Cast a video. Confirm it plays, loops at the end, and responds to play, pause, seek, and stop actions from the sender.
- While playing a video, it should display the video thumbnail on the client device as a static image and not play the video locally.
- While playing a video from Android, changing the device volume should change the cast volume.
- Switch between photos and videos during an active session. Confirm the previous media stops and does not replace or interrupt the newly selected asset.
- Disconnect from the sender and from the Cast device, then reconnect. Confirm the receiver name and current casting state recover correctly.
- Make the Immich server inaccessible for the Cast device, but not the Immich client. When casting, it should not fail silently but show clear error message.
Another complication is the difference between different Cast device generations. Care must be taking when making changes since the device you are testing on might be different to the devices used by our users.