Chromecast service wraps the Google Cast Web Sender SDK and manages the full lifecycle of a Chromecast receiver session. It follows the same lifecycle shape as the Core service: an outer service class manages state, while an inner transport handles the raw Cast API calls.
The Chromecast receiver app ID is
1634F54B. This value is defined in CONSTANTS.js as CHROMECAST_RECEIVER_APP_ID and used when configuring the Cast context options.Constructor
Chromecast constructor takes no arguments.
Properties
All properties are read-only (defined withObject.defineProperties).
true once the Cast SDK is available and ChromecastTransport has emitted 'init'. Resets to false when stop() is called or the SDK is unavailable.Set to
new Error('Google Cast API not available') if ChromecastTransport fails to initialize. null otherwise, or after stop() resets it.true while the ChromecastTransport is being created and waiting for the Cast SDK availability callback.The active
ChromecastTransport instance, or null when not running.Methods
start()
Creates a ChromecastTransport and waits for the Cast SDK to become available. Does nothing if already active, starting, or errored.
stop()
Tears down the transport and resets all state. Safe to call at any time.
on(name, listener) / off(name, listener)
Subscribe to or unsubscribe from service-level events.
Event name. The
Chromecast service itself emits only 'stateChanged'.Callback function.
Events
Emitted whenever
active, error, or starting changes.ChromecastTransport
ChromecastTransport is created internally by Chromecast.start(). It registers Cast SDK event listeners and provides methods to interact with the current session.
Initialization
On construction,ChromecastTransport:
- Waits for
window.__onGCastApiAvailable— the callback injected by the Cast SDK loader script. - Registers listeners on
CastContextforCAST_STATE_CHANGEDandSESSION_STATE_CHANGED. - Emits
'init'on success, or'init-error'if the Cast API is not available.
The Cast SDK must be loaded via the standard
<script> tag before Chromecast.start() is called. ChromecastTransport will wait indefinitely for window.__onGCastApiAvailable to fire if the SDK has not yet loaded.Message chunking
Messages sent to the receiver are serialized withJSON.stringify and split into chunks of 20,000 characters each. Each chunk is sent as a separate Cast message on the namespace urn:x-cast:com.stremio. The receiver reassembles chunks by matching the shared id (generated with hat()) and ordering by index/length.
Methods
Returns the current cast state from
CastContext.getInstance().Returns the current session state from
CastContext.getInstance().Returns the device object of the current cast session, or
null if no session is active.Configures the Cast context. Call this with the receiver app ID before requesting a session.
Options object passed to
CastContext.getInstance().setOptions().Opens the Cast device picker and starts a session.
Ends the current session.
If
true, stops the receiver app. If false, disconnects the sender only.Serializes and sends a message to the receiver over
urn:x-cast:com.stremio. The message is automatically chunked if it exceeds 20,000 characters.Rejects with Error('Session not started') if no session is currently active.Any JSON-serializable object.
Subscribe to transport events. In addition to
'init' and 'init-error', the transport forwards all Cast SDK events:| Event name | Source |
|---|---|
cast.framework.CastContextEventType.CAST_STATE_CHANGED | Cast context |
cast.framework.CastContextEventType.SESSION_STATE_CHANGED | Cast context |
cast.framework.CastSession.APPLICATION_STATUS_CHANGED | Active session |
cast.framework.CastSession.APPLICATION_METADATA_CHANGED | Active session |
cast.framework.CastSession.ACTIVE_INPUT_STATE_CHANGED | Active session |
cast.framework.CastSession.VOLUME_CHANGED | Active session |
cast.framework.CastSession.MEDIA_SESSION | Active session |
'message' | Incoming receiver message (reassembled from chunks) |
'message-error' | Failed to parse an incoming message |
Removes all listeners. Called internally by
Chromecast.stop().