Skip to content

Instantly share code, notes, and snippets.

@jarettmillard
Created May 4, 2017 19:52
Show Gist options
  • Save jarettmillard/398698c191a20e0a1ebc44b593f5729f to your computer and use it in GitHub Desktop.
Save jarettmillard/398698c191a20e0a1ebc44b593f5729f to your computer and use it in GitHub Desktop.
TypeScript Definitions for Google Cast receiver
declare module cast {
export class receiver {
/**
* Version of the cast SDK.
*/
static VERSION: string;
/**
* The Cast receiver logger object.
* Sets the log verbosity level.
*/
setLevelValue(loggerLevel: cast.receiver.LoggerLevel): void;
/**
*
* The media namespace.
*/
MEDIA_NAMESPACE: string;
}
}
declare module cast.receiver {
/**
* Handles cast messages for a specific sender and namespace
* * (it is a point to point communications channel).
* * It should be used when the application wants to have a virtual connection
* * with a single sender for a specific protocol (namespace),
* * similar to a virtual websocket.
* * Applications should never create CastChannels, they should only be obtained
* * from the corresponding CastMessageBus calling the getCastChannel method.
* * Extends goog.events.EventTarget. Implements EventTarget.
*/
export class CastChannel {
/**
* Event handler for {code cast.receiver.CastChannel} close event.
*/
onClose(event: CastChannel.Event): void;
/**
* Event handler for cast.receiver.CastMessageBus message event.
*/
onMessage(event: CastChannel.Event): void;
getNamespace(): string;
getSenderId(): string;
/**
* Sends a message to the sender associated with this CastChannel.
*/
send(message: string): void;
}
module CastChannel {
/**
* System events dispatched by cast.receiver.CastChannel.
*/
export class Event {
message: any;
constructor(type: string, message: any);
}
export enum EventType {
CLOSE,
MESSAGE
}
}
/**
* Handles cast messages for a specific namespace.
* * Applications should never create a CastMessageBus,
* * they should only be obtained from the cast.receiver.CastReceiverManager instance.
* * Extends goog.events.EventTarget. Implements EventTarget.
*/
export class CastMessageBus {
/**
* Deserializes a serialized message.
*/
deserializeMessage(message: string): any;
/**
* Serializes a deserialized message.
*/
serializeMessage(message: any): string;
/**
* Event handler for cast.receiver.CastMessageBus message event.
*/
onMessage(event: CastMessageBus.Event): void;
/**
* The namespace of the messages processed by this CastMessageBus
*/
getNamespace(): string;
/**
* The type of messages processed by this CastMessageBus.
*/
getMessageType(): MessageType;
/**
* Sends a message to a specific sender.
*/
send(senderId: string, message: any): void;
/**
* Sends a message to all the senders connected.
*/
broadcast(message: any): void;
/**
* Provides a {cast.receiver.CastChannel} for a specific senderId.
*/
getCastChannel(senderId: string): CastChannel;
}
module CastMessageBus {
/**
* Event which contains the event raised when a new message is received
* * on the message bus for a specific namespace. Extends goog.events.Event.
*/
export class Event {
/**
* Application message.
*/
data: any;
/**
* The sender Id.
*/
senderId: string;
constructor(type: CastMessageBus.EventType, senderId: string, data: any);
}
/**
* Events dispatched by cast.receiver.CastMessageBus.
*/
export enum EventType {
MESSAGE
}
/**
* Fired when there is a message.
* Message types used by cast.receiver.CastMessageBus.
*/
export enum MessageType {
STRING,
JSON,
CUSTOM
}
}
/**
* Initializes the system manager so we can communicate with the platform.
* * This class is used to send/receive system messages/events.
* * It must only be instantiated just once (singleton).
* * Extends goog.events.EventTarget. Implements EventTarget.
*/
export class CastReceiverManager {
/**
* Initializes the system manager so we can communicate with the platform.
* * This class is used to send/receive system messages/events.
* * It must only be instantiated just once (singleton).
*/
constructor();
/**
* If provided, it processes the 'ready' event.
*/
onReady(event: CastReceiverManager.Event): void;
/**
* If provided, it processes the 'senderconnected' event.
*/
onSenderConnected(event: CastReceiverManager.Event): void;
/**
* If provided, it processes the 'senderdisconnected' event.
*/
onSenderDisconnected(event: CastReceiverManager.Event): void;
/**
* If provided, it processes the 'systemvolumechanged' event.
*/
onSystemVolumeChanged(event: CastReceiverManager.Event): void;
/**
* If provided, it processes the 'visibilitychanged' event.
*/
onVisibilityChanged(event: CastReceiverManager.Event): void;
/**
* Initializes the system manager.
*/
start(config?: Config): void;
/**
* Terminates the application.
*/
stop(): void;
/**
* When the application calls start, the system will send the ready event to indicate
* * that the application information is ready and the application can send messages as
* * soon as there is one sender connected.
*/
isSystemReady(): boolean;
/**
* Provides a list of the senders currently connected to the application.
*/
getSenders(): Array<string>;
/**
* Provides a copy of the sender object by senderId.
*/
getSender(senderId: string): cast.receiver.system.Sender;
/**
* Provides application information once the system is ready, otherwise it will be null.
*/
getApplicationData(): cast.receiver.system.ApplicationData;
/**
* Sets the application state.
*/
setApplicationState(statusText: string): void;
/**
* Provides a channel for a specific namespace (for any sender).
*/
getCastMessageBus(namespace: string, messageType?: cast.receiver.CastMessageBus.MessageType): cast.receiver.CastMessageBus;
static getInstance(): CastReceiverManager;
}
module CastReceiverManager {
/**
* Application configuration parameters.
*/
export class Config {
/**
* Maximum time in seconds before closing an idle sender connection.
* * Setting this value enables a heartbeat message to keep the connection alive.
* * Used to detect unresponsive senders faster than typical TCP timeouts
* * The minimum value is 5 seconds, there is no upper bound enforced but practically
* * it's minutes before platform TCP timeouts come into play. Default value is 10 seconds.
*/
maxInactivity: integer;
/**
* Text that represents the application status. It should meet internationalization
* * rules as may be displayed by the sender application.
*/
statusText: string;
constructor();
}
/**
* Event dispatched by cast.receiver.CastReceiverManager which contains system information. Extends goog.events.Event.
*/
export class Event {
/**
* Data associated with this event.
*/
data: any;
/**
* Event dispatched by cast.receiver.CastReceiverManager which contains system information.
*/
constructor(type: CastReceiverManager.EventType, data: any);
}
/**
* System events dispatched by cast.receiver.CastReceiverManager.
*/
export enum EventType {
READY,
SHUTDOWN,
SENDER_CONNECTED,
SENDER_DISCONNECTED,
ERROR,
SYSTEM_VOLUME_CHANGED,
VISIBILITY_CHANGED
}
}
/**
* This class is used to send/receive media messages/events.
*/
export class MediaManager {
/**
* Creates a media manager instance. This class is used to send/receive media messages/events.
* * @param mediaElement HTMLMediaElement or cast.receiver.media.Player
* * The DOM media element (video/audio) or a player that implements
* * the cast.receiver.media.Player interface. Note that the MediaManager will
* * add listeners for 'loadedmetadata', 'error', and 'ended' HTMLMediaElement events.
* * Must not be null.
* * @param @optional supportedCommands cast.receiver.media.Command
* * Media commands supported by the application.
* * LOAD, PLAY, STOP, GET_STATUS must always be supported. Optional.
*/
constructor(mediaElement: any, command: any);
/**
* The application developer can override this method to customize the media status
* * that will be send to the senders (this method will be called before the media status is sent).
* * By providing this method application developers can, for example, add custom data to the media status.
* * The current media status will be provided as a parameter and the method should return
* * the application-modified current status.
* * The default behavior is to return the incoming media status.
* * If the method returns null the media status message will not be sent
* * (developers should be aware that if the media status is a response to a sender status request
* * the sender will expect a response so this method should only return null it if the developer
* * is also overriding onGetStatus).
* Called when the media ends. The default behavior is to call ResetMediaElement with idle reason FINISHED.
*/
onEnded(): void;
/**
* Called when there is an error not triggered by a LOAD request.
* * The default behavior is to call ResetMediaElement.
*/
onError(error: any): void;
/**
* Processes the get status event.
*/
onGetStatus(event: any): void;
/**
* If provided, it processes the load event. The default behavior is to set the src and autoplay properties
* * of the media element and call its load method.
* * If provided, the currentTime property will be modified when the 'loadedmetadata' event is fired
* * (in onMetadataLoaded) as it can only be set when the media element duration property has been set.
*/
onLoad(event: any): void;
/**
* Called when load has had an error, it can be overridden to handle application specific logic.
* * The default behavior is to call resetMediaElement with idle reason ERROR and sendLoadError
* * with error type LOAD_FAILED.
*/
onLoadMetadataError(loadInfo: any): void;
/**
* Called when load has completed, it can be overridden to handle application specific action.
* * The default behavior is to set the currentTime property of the media element
* * (if it was provided in the LOAD request), then call sendLoadComplete.
*/
onMetadataLoaded(loadInfo: any): void;
/**
* Processes the pause event. The default behavior is to call the media element's pause method
* * and broadcast the status providing the incoming requestId.
*/
onPause(event: any): void;
/**
* Processes the play event. The default behavior is to call the media element's play method
* * and broadcast the status providing the incoming requestId.
*/
onPlay(event: any): void;
/**
* Processes the seek event. The default behavior is to call the media element's play
* * or pause methods (only if required based on the current state and the resume state value of the request)
* * and broadcast the status providing the incoming requestId.
*/
onSeek(event: any): void;
/**
* Processes the set volume event. The default behavior is to set volume and muted on the media element
* * as required and broadcast the status providing the incoming requestId.
*/
onSetVolume(event: any): void;
/**
* Processes the stop event. The default behavior is to call resetMediaElement,
* * with idle reason CANCELLED, and broadcast the status providing the incoming requestId.
*/
onStop(event: any): void;
/**
* Provides information about the media currently loaded.
* * @returns cast.receiver.media.MediaInformation The media information.
* * @see cast.receiver.media.MediaInformation
* Sets information about the media currently loaded. This information will be sent to the senders
* * when they request media status.
* * @param mediaInformation cast.receiver.media.MediaInformation
* * The new media information. Use resetMediaElement to reset its value. Must not be null.
* * @param @optional broadcast Bool
* * Whether the senders should be notified about the change
* * (if not provided, the senders will be notified). Optional.
* * @param @optional broadcastStatusCustomData Dynamic
* * If the senders should be notified this parameter allows to set the application-specific
* * custom data in the status message. Optional.
* * @throws Error If broadcastStatusCustomData is provided but broadcast is false.
* * @see cast.receiver.media.MediaInformation
*/
setMediaInformation(mediaInformation: any, broadcast?: boolean, broadcastStatusCustomData?: any): void;
/**
* Sends a media status message to all senders (broadcast).
* * Applications can use it when they have a custom state change.
* * It will call cast.receiver.MediaManager.prototype.customizedStatusCallback so applications can customize
* * the status message.
* * @param includeMedia Bool Whether to include media information.
* * @param @optional requestId Int The ID of the request that triggered the status change. May be null.
* * Optional.
* * @param @optional customData Dynamic The status message application-specific custom data. Optional.
*/
broadcastStatus(includeMedia: boolean, requestId?: integer, customData?: any): void;
/**
* Sets the IDLE reason. This allows applications that want to force the IDLE state to indicate the reason
* * that made the player going to IDLE state (a custom error, for example).
* * The idle reason will be sent in the next status message.
* * NOTE: Most applications do not need to set this value, it is only needed if they want to make the player
* * go to IDLE in special circumstances and the default idleReason does not reflect their intended behavior.
* * @param idleReason cast.receiver.media.IdleReason
* * The reason to be in the IDLE state.
*/
setIdleReason(idleReason: any): void;
/**
* Sends an error to a specific sender.
* * @param senderId String
* * The sender ID.
* * @param requestId Int
* * The ID of the incoming request that caused this error.
* * @param type cast.receiver.media.ErrorType
* * The error type.
* * @param @optional reason cast.receiver.media.ErrorReason
* * The error reason. May be null. Optional.
* * @param @optional customData Dynamic
* * The error message application-specific custom data. Optional.
*/
sendError(senderId: string, requestId: integer, errorType: any, errorReason: any, customData?: any): void;
/**
* Sends a media status message to a specific sender.
* * @param senderId String
* * The sender ID.
* * @param requestId Int
* * The ID of the incoming request that caused this error.
* * @param includeMedia Bool
* * Whether to include media information.
* * @param @optional customData Dynamic
* * The status message application-specific custom data. Optional.
*/
sendStatus(senderId: string, requestId: integer, includeMedia: boolean, customData?: any): void;
/**
* Associates a new media element or Player to the media manager.
* * @param mediaElement HTMLMediaElement or cast.receiver.media.Player
* * The DOM media element (video/audio) or a player that implements the
* * cast.receiver.media.Player interface.
*/
setMediaElement(mediaElement: any): void;
/**
* When the application overrides onLoad, it should use this method to trigger an error response to the sender.
* * This is typically due to application-specific verification issues.
* * @param @optional errorType cast.receiver.media.ErrorType
* * The error type, by default is assumed to be
* * cast.receiver.media.ErrorType.LOAD_FAILED, but the application can send an
* * INVALID_REQUEST for example if there is customData that does not match some
* * criteria.
* * @param @optional customData Dynamic
* * The error message application-specific custom data. Optional.
* * @see cast.receiver.media.ErrorType
*/
sendLoadError(errorType: any, customData?: any): void;
/**
* Sends the new status after a LOAD message has been completed succesfully.
* * Note: Applications do not normally need to call this API.
* * When the application overrides onLoad, it may need to manually declare that the LOAD request was sucessful.
* * The default implementaion will send the new status to the sender when the video/audio element raises
* * the 'loadedmetadata' event. The default behavior may not be acceptable in a couple scenarios:
* * 1) When the application does not want to declare LOAD succesful until for example 'canPlay' is raised
* * (instead of 'loadedmetadata').
* * 2) When the application is not actually loading the media element
* * (for example if LOAD is used to load an image).
* * @param @optional customData Dynamic
* * The status message application-specific custom data.
*/
sendLoadComplete(customData?: any): void;
/**
* Resets Media Element to IDLE state. After this call the mediaElement properties will change, paused will be true, currentTime will be zero and the src attribute will be empty. This only needs to be manually called if the developer wants to override the default behavior of onError, onStop or onEnded, for example.
* * @param @optional idleReason cast.receiver.media.IdleReason
* * The reason to be IDLE.
* * @param @optional broadcast Bool
* * Whether the senders should be notified about the change
* * (if not provided, the senders will be notified).
* * @param @optional requestId Int
* * If the status change is due to a sender request (for example STOP),
* * this is the ID of the sender request that will be added to the status message
* * so the sender can identify it. May be null.
* * @param @optional broadcastStatusCustomData Dynamic
* * If the senders should be notified, this parameter allows to set the
* * application-specific custom data in the status message.
* * @throws Error If broadcastStatusCustomData is provided but broadcast is false.
*/
resetMediaElement(idleReason: any, broadcast?: boolean, requestId?: integer, broadcastStatusCustomData?: any): void;
}
module MediaManager {
/**
* Event dispatched by cast.receiver.MediaManager which contains system information.
*/
export class Event {
/**
* Request data associated with this event.
* The ID of the sender that triggered the event.
*/
senderId: string;
constructor(type: string, data: RequestData, senderId: string);
}
/**
* Load Request Information. Extends cast.receiver.MediaManager.RequestData.
*/
export class LoadInfo {
/**
* Request data associated with this load request.
* The ID of the sender that triggered the event.
*/
senderId: string;
constructor(message: LoadRequestData, senderId: string);
}
/**
* Media event LOAD request data. Extends cast.receiver.MediaManager.RequestData.
*/
export class LoadRequestData {
/**
* If the autoplay parameter is specified, the media player will begin playing the content when it is loaded.
*/
autoplay: boolean;
/**
* Seconds since beginning of content.
*/
currentTime: number;
/**
* If the autoplay parameter is specified, the media player will begin playing the content when it is loaded.
*/
constructor();
}
/**
* Media event request data.
*/
export class RequestData {
/**
* Application-specific data for this request.
*/
customData: any;
/**
* Id of the media session that the request applies to.
*/
mediaSessionId: number;
/**
* Id of the request, used to correlate request/response.
*/
requestId: integer;
constructor();
}
/**
* Media event SEEK request data. Extends cast.receiver.MediaManager.RequestData
*/
export class SeekRequestData {
/**
* Seconds since beginning of content
*/
currentTime: number;
/**
* The playback state after a SEEK request
*/
constructor();
}
/**
* Media event SET_VOLUME request data. Extends cast.receiver.MediaManager.RequestData.
*/
export class VolumeRequestData {
/**
* The media stream volume.
*/
constructor();
}
export enum EventType {
LOAD,
STOP,
PAUSE,
PLAY,
SEEK,
SET_VOLUME,
GET_STATUS
}
}
export enum LoggerLevel {
DEBUG,
ERROR,
INFO,
NONE,
VERBOSE
}
}
declare module cast.receiver.media {
/**
* Represents the media information.
*/
export class MediaInformation {
/**
* Typically the url of the media.
*/
contentId: string;
/**
* The stream type.
*/
contentType: string;
/**
* Application-specific media information.
*/
customData: any;
/**
* The media duration.
*/
duration: number;
/**
* The media metadata.
*/
metadata: any;
/**
* The stream type.
*/
constructor();
}
/**
* Represents the status of a media session.
*/
export class MediaStatus {
/**
* The current playback position.
*/
currentTime: number;
/**
* Application-specific media status.
*/
customData: any;
/**
* If the state is IDLE, the reason the player went to IDLE state.
* The media information.
* Unique id for the session.
*/
mediaSessionId: number;
/**
* The playback rate.
*/
playbackRate: number;
/**
* The playback state.
* The commands supported by this player.
*/
supportedMediaCommands: number;
/**
* The current stream volume.
*/
constructor();
}
/**
* Represents the volume of a media session stream.
*/
export class Volume {
/**
* Value from 0 to 1 that represents the current stream volume level.
*/
level: number;
/**
* Whether the stream is muted.
*/
muted: boolean;
constructor();
}
export enum Command {
DUPLICATE_REQUEST_ID,
INVALID_COMMAND,
INVALID_MEDIA_SESSION_ID,
INVALID_PARAMS
}
export enum ErrorReason {
DUPLICATE_REQUEST_ID,
INVALID_COMMAND,
INVALID_MEDIA_SESSION_ID,
INVALID_PARAMS
}
/**
* Represents media error message types.
*/
export enum ErrorType {
INVALID_PLAYER_STATE,
INVALID_REQUEST,
LOAD_CANCELLED,
LOAD_FAILED
}
/**
* The reason for the player to be in IDLE state.
*/
export enum IdleReason {
CANCELLED,
ERROR,
FINISHED,
INTERRUPTED
}
export enum PlayerState {
BUFFERING,
IDLE,
PAUSED,
PLAYING
}
export enum SeekResumeState {
PLAYBACK_PAUSE,
PLAYBACK_START
}
/**
* Represents the stream types.
*/
export enum StreamType {
BUFFERED,
LIVE,
NONE
}
}
declare module cast.receiver.system {
/**
* Represents the data of the launched application.
*/
export class ApplicationData {
/**
* The application Id.
*/
id: string;
/**
* The id of the sender that launched the application.
*/
launchingSenderId: string;
/**
* The application name.
*/
name: string;
/**
* The namespaces used by the application.
*/
namespaces: Array<string>;
/**
* The session Id.
*/
sessionId: integer;
constructor();
}
/**
* Represents the data of a connected sender device.
*/
export class Sender {
/**
* The sender Id.
*/
id: string;
/**
* The userAgent of the sender.
*/
userAgent: string;
constructor();
}
}
@jeffcunat
Copy link

Hi @jarettmillard, thanks for your work.
I have updated it with more type definitions : https://gist.github.com/jeffcunat/484bc331294070fe4547bff058fd5269 as it seems we can not modify other gists in github.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment