electron-cast 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +85 -2
- package/dist/client/cast-client.d.ts +92 -0
- package/dist/client/cast-client.js +427 -0
- package/dist/client/cast-client.js.map +1 -0
- package/dist/client/errors.d.ts +17 -0
- package/dist/client/errors.js +30 -0
- package/dist/client/errors.js.map +1 -0
- package/dist/client/index.d.ts +4 -0
- package/dist/client/index.js +5 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/namespaces.d.ts +9 -0
- package/dist/client/namespaces.js +10 -0
- package/dist/client/namespaces.js.map +1 -0
- package/dist/client/types.d.ts +27 -0
- package/dist/client/types.js +2 -0
- package/dist/client/types.js.map +1 -0
- package/dist/discovery/cast-discovery.d.ts +84 -0
- package/dist/discovery/cast-discovery.js +234 -0
- package/dist/discovery/cast-discovery.js.map +1 -0
- package/dist/discovery/device-cache.d.ts +40 -0
- package/dist/discovery/device-cache.js +212 -0
- package/dist/discovery/device-cache.js.map +1 -0
- package/dist/discovery/dns-packet.d.ts +70 -0
- package/dist/discovery/dns-packet.js +244 -0
- package/dist/discovery/dns-packet.js.map +1 -0
- package/dist/discovery/index.d.ts +4 -0
- package/dist/discovery/index.js +5 -0
- package/dist/discovery/index.js.map +1 -0
- package/dist/discovery/udp-socket.d.ts +12 -0
- package/dist/discovery/udp-socket.js +105 -0
- package/dist/discovery/udp-socket.js.map +1 -0
- package/dist/electron/main.d.ts +49 -0
- package/dist/electron/main.js +79 -0
- package/dist/electron/main.js.map +1 -0
- package/dist/electron/preload.d.ts +22 -0
- package/dist/electron/preload.js +36 -0
- package/dist/electron/preload.js.map +1 -0
- package/dist/electron/session-service.d.ts +125 -0
- package/dist/electron/session-service.js +661 -0
- package/dist/electron/session-service.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/polyfill/bridge-types.d.ts +82 -0
- package/dist/polyfill/bridge-types.js +2 -0
- package/dist/polyfill/bridge-types.js.map +1 -0
- package/dist/polyfill/install.d.ts +12 -0
- package/dist/polyfill/install.js +546 -0
- package/dist/polyfill/install.js.map +1 -0
- package/dist/protocol/cast-message.d.ts +20 -0
- package/dist/protocol/cast-message.js +166 -0
- package/dist/protocol/cast-message.js.map +1 -0
- package/dist/protocol/errors.d.ts +4 -0
- package/dist/protocol/errors.js +8 -0
- package/dist/protocol/errors.js.map +1 -0
- package/dist/protocol/framing.d.ts +18 -0
- package/dist/protocol/framing.js +95 -0
- package/dist/protocol/framing.js.map +1 -0
- package/dist/protocol/index.d.ts +3 -0
- package/dist/protocol/index.js +4 -0
- package/dist/protocol/index.js.map +1 -0
- package/package.json +59 -4
- package/src/client/cast-client.ts +483 -0
- package/src/client/errors.ts +32 -0
- package/src/client/index.ts +4 -0
- package/src/client/namespaces.ts +10 -0
- package/src/client/types.ts +27 -0
- package/src/discovery/cast-discovery.ts +265 -0
- package/src/discovery/device-cache.ts +231 -0
- package/src/discovery/dns-packet.ts +262 -0
- package/src/discovery/index.ts +4 -0
- package/src/discovery/udp-socket.ts +121 -0
- package/src/electron/main.ts +110 -0
- package/src/electron/preload.ts +57 -0
- package/src/electron/session-service.ts +715 -0
- package/src/index.ts +8 -0
- package/src/polyfill/bridge-types.ts +26 -0
- package/src/polyfill/install.ts +576 -0
- package/src/protocol/cast-message.ts +176 -0
- package/src/protocol/errors.ts +7 -0
- package/src/protocol/framing.ts +97 -0
- package/src/protocol/index.ts +3 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from "./protocol/index.ts";
|
|
2
|
+
export * from "./client/index.ts";
|
|
3
|
+
export * from "./discovery/index.ts";
|
|
4
|
+
export * from "./electron/session-service.ts";
|
|
5
|
+
export * from "./electron/main.ts";
|
|
6
|
+
export * from "./electron/preload.ts";
|
|
7
|
+
export { installChromeCast } from "./polyfill/install.ts";
|
|
8
|
+
export type * from "./polyfill/bridge-types.ts";
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export type BridgeResult<T = unknown> = { ok: true; value: T } | { ok: false; code: string; description?: string };
|
|
2
|
+
export type BridgeMethod = "initialize" | "requestSession" | "joinSession" | "sendMessage" | "stopSession"
|
|
3
|
+
| "leaveSession" | "setReceiverVolume" | "setReceiverMuted";
|
|
4
|
+
export interface CastBridge {
|
|
5
|
+
call(method: BridgeMethod, params: unknown): Promise<BridgeResult>; // never rejects; errors come as ok:false
|
|
6
|
+
onEvent(listener: (event: BridgeEvent) => void): void; // one listener; install calls it once
|
|
7
|
+
}
|
|
8
|
+
export interface VolumeSnapshot { level: number | null; muted: boolean | null; controlType?: string; stepInterval?: number }
|
|
9
|
+
export interface ImageSnapshot { url: string; height?: number | null; width?: number | null }
|
|
10
|
+
export interface ReceiverSnapshot {
|
|
11
|
+
label: string; friendlyName: string; capabilities: string[]; volume: VolumeSnapshot;
|
|
12
|
+
isActiveInput?: boolean | null; displayStatus?: { statusText: string; appImages: ImageSnapshot[] } | null;
|
|
13
|
+
}
|
|
14
|
+
export interface SessionSnapshot {
|
|
15
|
+
sessionId: string; appId: string; displayName: string; appImages: ImageSnapshot[]; statusText: string | null;
|
|
16
|
+
namespaces: { name: string }[]; transportId: string; receiver: ReceiverSnapshot;
|
|
17
|
+
senderApps?: { platform: string; url?: string | null; packageId?: string | null }[];
|
|
18
|
+
}
|
|
19
|
+
export type BridgeEvent =
|
|
20
|
+
| { type: "receiverAvailability"; available: boolean }
|
|
21
|
+
| { type: "sessionUpdated"; session: SessionSnapshot }
|
|
22
|
+
| { type: "sessionEnded"; sessionId: string; status: "stopped" | "disconnected" }
|
|
23
|
+
| { type: "message"; sessionId: string; namespace: string; message: string };
|
|
24
|
+
export interface InitializeParams { appId: string; capabilities: string[]; autoJoinPolicy: string }
|
|
25
|
+
export interface InitializeResult { available: boolean; sessions: SessionSnapshot[] }
|
|
26
|
+
export interface RequestSessionParams { appId: string; capabilities: string[]; timeoutMs: number }
|
|
@@ -0,0 +1,576 @@
|
|
|
1
|
+
import type { BridgeEvent, CastBridge } from "./bridge-types.ts";
|
|
2
|
+
|
|
3
|
+
export type {
|
|
4
|
+
BridgeEvent,
|
|
5
|
+
BridgeMethod,
|
|
6
|
+
BridgeResult,
|
|
7
|
+
CastBridge,
|
|
8
|
+
ImageSnapshot,
|
|
9
|
+
InitializeParams,
|
|
10
|
+
InitializeResult,
|
|
11
|
+
ReceiverSnapshot,
|
|
12
|
+
RequestSessionParams,
|
|
13
|
+
SessionSnapshot,
|
|
14
|
+
VolumeSnapshot,
|
|
15
|
+
} from "./bridge-types.ts";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Installs the legacy Google Cast sender API (`chrome.cast`, version [1, 2]) on the page's global object,
|
|
19
|
+
* forwarding every operation to the electron-cast bridge that the preload script published at
|
|
20
|
+
* `globalThis[bridgeKey]`. Returns `true` when the polyfill was installed, `false` when the bridge is
|
|
21
|
+
* missing or `chrome.cast` already exists (in which case nothing is changed).
|
|
22
|
+
*
|
|
23
|
+
* Electron runs this function in the page's main world through `contextBridge.executeInMainWorld`, which
|
|
24
|
+
* serializes it to source; the body must therefore stay self-contained: no imports, no module-level
|
|
25
|
+
* helpers or state. Type annotations are stripped and leave no runtime references behind.
|
|
26
|
+
*/
|
|
27
|
+
export function installChromeCast(bridgeKey: string): boolean {
|
|
28
|
+
const g: any = globalThis;
|
|
29
|
+
const rawBridge: any = g[bridgeKey];
|
|
30
|
+
if (!rawBridge || typeof rawBridge.call !== "function") return false;
|
|
31
|
+
const bridge: CastBridge = rawBridge;
|
|
32
|
+
if (g.chrome && g.chrome.cast) return false;
|
|
33
|
+
g.chrome ??= {};
|
|
34
|
+
|
|
35
|
+
const VERSION = [1, 2];
|
|
36
|
+
const AutoJoinPolicy = { TAB_AND_ORIGIN_SCOPED: "tab_and_origin_scoped", ORIGIN_SCOPED: "origin_scoped", PAGE_SCOPED: "page_scoped" };
|
|
37
|
+
const Capability = { VIDEO_OUT: "video_out", AUDIO_OUT: "audio_out", VIDEO_IN: "video_in", AUDIO_IN: "audio_in", MULTIZONE_GROUP: "multizone_group" };
|
|
38
|
+
const DefaultActionPolicy = { CREATE_SESSION: "create_session", CAST_THIS_TAB: "cast_this_tab" };
|
|
39
|
+
const DialAppState = { RUNNING: "running", STOPPED: "stopped", ERROR: "error" };
|
|
40
|
+
const ErrorCode = {
|
|
41
|
+
CANCEL: "cancel", TIMEOUT: "timeout", API_NOT_INITIALIZED: "api_not_initialized", INVALID_PARAMETER: "invalid_parameter",
|
|
42
|
+
EXTENSION_NOT_COMPATIBLE: "extension_not_compatible", EXTENSION_MISSING: "extension_missing",
|
|
43
|
+
RECEIVER_UNAVAILABLE: "receiver_unavailable", SESSION_ERROR: "session_error", CHANNEL_ERROR: "channel_error",
|
|
44
|
+
LOAD_MEDIA_FAILED: "load_media_failed",
|
|
45
|
+
};
|
|
46
|
+
const ReceiverAction = { CAST: "cast", STOP: "stop" };
|
|
47
|
+
const ReceiverAvailability = { AVAILABLE: "available", UNAVAILABLE: "unavailable" };
|
|
48
|
+
const ReceiverType = { CAST: "cast", DIAL: "dial", HANGOUT: "hangout", CUSTOM: "custom" };
|
|
49
|
+
const SenderPlatform = { CHROME: "chrome", IOS: "ios", ANDROID: "android" };
|
|
50
|
+
const SessionStatus = { CONNECTED: "connected", DISCONNECTED: "disconnected", STOPPED: "stopped" };
|
|
51
|
+
const VolumeControlType = { ATTENUATION: "attenuation", FIXED: "fixed", MASTER: "master" };
|
|
52
|
+
const media = { DEFAULT_MEDIA_RECEIVER_APP_ID: "CC1AD845" };
|
|
53
|
+
|
|
54
|
+
class Timeout {
|
|
55
|
+
requestSession = 60000;
|
|
56
|
+
leaveSession = 3000;
|
|
57
|
+
stopSession = 3000;
|
|
58
|
+
setReceiverVolume = 3000;
|
|
59
|
+
sendCustomMessage = 3000;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
class ApiConfig {
|
|
63
|
+
sessionRequest: any;
|
|
64
|
+
sessionListener: any;
|
|
65
|
+
receiverListener: any;
|
|
66
|
+
autoJoinPolicy: any;
|
|
67
|
+
defaultActionPolicy: any;
|
|
68
|
+
additionalSessionRequests: any[];
|
|
69
|
+
customDialLaunchCallback: any;
|
|
70
|
+
invisibleSender: boolean;
|
|
71
|
+
constructor(sessionRequest: any, sessionListener: any, receiverListener: any, autoJoinPolicy: any = "tab_and_origin_scoped", defaultActionPolicy: any = "create_session") {
|
|
72
|
+
this.sessionRequest = sessionRequest;
|
|
73
|
+
this.sessionListener = sessionListener;
|
|
74
|
+
this.receiverListener = receiverListener;
|
|
75
|
+
this.autoJoinPolicy = autoJoinPolicy;
|
|
76
|
+
this.defaultActionPolicy = defaultActionPolicy;
|
|
77
|
+
this.additionalSessionRequests = [];
|
|
78
|
+
this.customDialLaunchCallback = null;
|
|
79
|
+
this.invisibleSender = false;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
class SessionRequest {
|
|
84
|
+
appId: any;
|
|
85
|
+
capabilities: any;
|
|
86
|
+
requestSessionTimeout: any;
|
|
87
|
+
language: any;
|
|
88
|
+
dialRequest: any;
|
|
89
|
+
androidReceiverCompatible: any;
|
|
90
|
+
credentialsData: any;
|
|
91
|
+
constructor(appId: any, capabilities: any = ["video_out", "audio_out"], timeout: any = 60000, androidReceiverCompatible: any = false, credentialsData: any = null) {
|
|
92
|
+
this.appId = appId;
|
|
93
|
+
this.capabilities = capabilities;
|
|
94
|
+
this.requestSessionTimeout = timeout;
|
|
95
|
+
this.language = null;
|
|
96
|
+
this.dialRequest = null;
|
|
97
|
+
this.androidReceiverCompatible = androidReceiverCompatible;
|
|
98
|
+
this.credentialsData = credentialsData;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
class CastError {
|
|
103
|
+
code: any;
|
|
104
|
+
description: any;
|
|
105
|
+
details: any;
|
|
106
|
+
constructor(code: any, description: any = null, details: any = null) {
|
|
107
|
+
this.code = code;
|
|
108
|
+
this.description = description;
|
|
109
|
+
this.details = details;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
class Image {
|
|
114
|
+
url: any;
|
|
115
|
+
height: any;
|
|
116
|
+
width: any;
|
|
117
|
+
constructor(url: any) {
|
|
118
|
+
this.url = url;
|
|
119
|
+
this.height = null;
|
|
120
|
+
this.width = null;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
class Receiver {
|
|
125
|
+
label: any;
|
|
126
|
+
friendlyName: any;
|
|
127
|
+
capabilities: any;
|
|
128
|
+
volume: any;
|
|
129
|
+
receiverType: any;
|
|
130
|
+
isActiveInput: any;
|
|
131
|
+
displayStatus: any;
|
|
132
|
+
constructor(label: any, friendlyName: any, capabilities: any = [], volume: any = null) {
|
|
133
|
+
this.label = label;
|
|
134
|
+
this.friendlyName = friendlyName;
|
|
135
|
+
this.capabilities = capabilities;
|
|
136
|
+
this.volume = volume;
|
|
137
|
+
this.receiverType = "cast";
|
|
138
|
+
this.isActiveInput = null;
|
|
139
|
+
this.displayStatus = null;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
class ReceiverDisplayStatus {
|
|
144
|
+
statusText: any;
|
|
145
|
+
appImages: any;
|
|
146
|
+
constructor(statusText: any, appImages: any) {
|
|
147
|
+
this.statusText = statusText;
|
|
148
|
+
this.appImages = appImages;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
class SenderApplication {
|
|
153
|
+
platform: any;
|
|
154
|
+
url: any;
|
|
155
|
+
packageId: any;
|
|
156
|
+
constructor(platform: any) {
|
|
157
|
+
this.platform = platform;
|
|
158
|
+
this.url = null;
|
|
159
|
+
this.packageId = null;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
class Volume {
|
|
164
|
+
level: any;
|
|
165
|
+
muted: any;
|
|
166
|
+
constructor(level: any = null, muted: any = null) {
|
|
167
|
+
this.level = level;
|
|
168
|
+
this.muted = muted;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
class DialRequest {
|
|
173
|
+
launchUrl: any;
|
|
174
|
+
extraParameters: any;
|
|
175
|
+
constructor(launchUrl: any, extraParameters: any = {}) {
|
|
176
|
+
this.launchUrl = launchUrl;
|
|
177
|
+
this.extraParameters = extraParameters;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
class CredentialsData {
|
|
182
|
+
credentials: any;
|
|
183
|
+
constructor(credentials: any) {
|
|
184
|
+
this.credentials = credentials;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function safe(fn: any, ...args: any[]): void {
|
|
189
|
+
if (typeof fn !== "function") return;
|
|
190
|
+
try {
|
|
191
|
+
fn(...args);
|
|
192
|
+
} catch (err) {
|
|
193
|
+
console.error(err);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function fail(error: any, code: string, description?: string): void {
|
|
198
|
+
setTimeout(() => safe(error, new CastError(code, description ?? null)), 0);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function toImage(img: any): any {
|
|
202
|
+
const out = new Image(img.url);
|
|
203
|
+
if (img.height != null) out.height = img.height;
|
|
204
|
+
if (img.width != null) out.width = img.width;
|
|
205
|
+
return out;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function toVolume(vol: any): any {
|
|
209
|
+
const out: any = new Volume(vol.level ?? null, vol.muted ?? null);
|
|
210
|
+
if (vol.controlType != null) out.controlType = vol.controlType;
|
|
211
|
+
if (vol.stepInterval != null) out.stepInterval = vol.stepInterval;
|
|
212
|
+
return out;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function toReceiver(rcv: any): any {
|
|
216
|
+
const out = new Receiver(rcv.label, rcv.friendlyName, Array.isArray(rcv.capabilities) ? [...rcv.capabilities] : [], rcv.volume ? toVolume(rcv.volume) : null);
|
|
217
|
+
if (rcv.isActiveInput != null) out.isActiveInput = rcv.isActiveInput;
|
|
218
|
+
if (rcv.displayStatus) {
|
|
219
|
+
out.displayStatus = new ReceiverDisplayStatus(rcv.displayStatus.statusText, (rcv.displayStatus.appImages ?? []).map(toImage));
|
|
220
|
+
}
|
|
221
|
+
return out;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function applySnapshot(sess: any, snap: any): void {
|
|
225
|
+
sess.appId = snap.appId;
|
|
226
|
+
sess.displayName = snap.displayName;
|
|
227
|
+
sess.appImages = (snap.appImages ?? []).map(toImage);
|
|
228
|
+
sess.statusText = snap.statusText ?? null;
|
|
229
|
+
sess.namespaces = (snap.namespaces ?? []).map((n: any) => ({ name: n.name }));
|
|
230
|
+
sess.transportId = snap.transportId;
|
|
231
|
+
sess.receiver = toReceiver(snap.receiver);
|
|
232
|
+
if (Array.isArray(snap.senderApps)) {
|
|
233
|
+
sess.senderApps = snap.senderApps.map((a: any) => {
|
|
234
|
+
const sa = new SenderApplication(a.platform);
|
|
235
|
+
if (a.url != null) sa.url = a.url;
|
|
236
|
+
if (a.packageId != null) sa.packageId = a.packageId;
|
|
237
|
+
return sa;
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
class Session {
|
|
243
|
+
sessionId: any;
|
|
244
|
+
appId: any;
|
|
245
|
+
displayName: any;
|
|
246
|
+
appImages: any;
|
|
247
|
+
receiver: any;
|
|
248
|
+
senderApps: any[];
|
|
249
|
+
namespaces: any[];
|
|
250
|
+
media: any[];
|
|
251
|
+
status: any;
|
|
252
|
+
statusText: any;
|
|
253
|
+
transportId: any;
|
|
254
|
+
updateListeners: any[];
|
|
255
|
+
messageListeners: Map<string, any[]>;
|
|
256
|
+
constructor(sessionId: any, appId: any, displayName: any, appImages: any, receiver: any) {
|
|
257
|
+
this.sessionId = sessionId;
|
|
258
|
+
this.appId = appId;
|
|
259
|
+
this.displayName = displayName;
|
|
260
|
+
this.appImages = appImages;
|
|
261
|
+
this.receiver = receiver;
|
|
262
|
+
this.senderApps = [];
|
|
263
|
+
this.namespaces = [];
|
|
264
|
+
this.media = [];
|
|
265
|
+
this.status = "connected";
|
|
266
|
+
this.statusText = null;
|
|
267
|
+
this.transportId = sessionId;
|
|
268
|
+
this.updateListeners = [];
|
|
269
|
+
this.messageListeners = new Map<string, any[]>();
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
sendMessage(namespace: any, message: any, success?: any, error?: any): void {
|
|
273
|
+
const payload = typeof message === "string" ? message : JSON.stringify(message);
|
|
274
|
+
bridge.call("sendMessage", { sessionId: this.sessionId, namespace, message: payload }).then((res: any) => {
|
|
275
|
+
if (res.ok) {
|
|
276
|
+
safe(success);
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
fail(error, res.code, res.description);
|
|
280
|
+
}).catch((e: unknown) => fail(error, "session_error", String(e)));
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
addMessageListener(namespace: any, fn: any): void {
|
|
284
|
+
if (typeof fn !== "function") return;
|
|
285
|
+
let list = this.messageListeners.get(namespace);
|
|
286
|
+
if (!list) {
|
|
287
|
+
list = [];
|
|
288
|
+
this.messageListeners.set(namespace, list);
|
|
289
|
+
}
|
|
290
|
+
if (!list.includes(fn)) list.push(fn);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
removeMessageListener(namespace: any, fn: any): void {
|
|
294
|
+
const list = this.messageListeners.get(namespace);
|
|
295
|
+
if (!list) return;
|
|
296
|
+
const i = list.indexOf(fn);
|
|
297
|
+
if (i >= 0) list.splice(i, 1);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
addUpdateListener(fn: any): void {
|
|
301
|
+
if (typeof fn === "function" && !this.updateListeners.includes(fn)) this.updateListeners.push(fn);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
removeUpdateListener(fn: any): void {
|
|
305
|
+
const i = this.updateListeners.indexOf(fn);
|
|
306
|
+
if (i >= 0) this.updateListeners.splice(i, 1);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
addMediaListener(_fn: any): void {}
|
|
310
|
+
|
|
311
|
+
removeMediaListener(_fn: any): void {}
|
|
312
|
+
|
|
313
|
+
stop(success: any, error: any): void {
|
|
314
|
+
bridge.call("stopSession", { sessionId: this.sessionId }).then((res: any) => {
|
|
315
|
+
if (!res.ok) {
|
|
316
|
+
fail(error, res.code, res.description);
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
this.status = "stopped";
|
|
320
|
+
for (const fn of [...this.updateListeners]) safe(fn, false);
|
|
321
|
+
safe(success);
|
|
322
|
+
}).catch((e: unknown) => fail(error, "session_error", String(e)));
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
leave(success: any, error: any): void {
|
|
326
|
+
bridge.call("leaveSession", { sessionId: this.sessionId }).then((res: any) => {
|
|
327
|
+
if (!res.ok) {
|
|
328
|
+
fail(error, res.code, res.description);
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
this.status = "disconnected";
|
|
332
|
+
for (const fn of [...this.updateListeners]) safe(fn, false);
|
|
333
|
+
safe(success);
|
|
334
|
+
}).catch((e: unknown) => fail(error, "session_error", String(e)));
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
setReceiverVolumeLevel(level: any, success: any, error: any): void {
|
|
338
|
+
if (typeof level !== "number" || Number.isNaN(level) || level < 0 || level > 1) {
|
|
339
|
+
fail(error, "invalid_parameter");
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
bridge.call("setReceiverVolume", { sessionId: this.sessionId, level }).then((res: any) => {
|
|
343
|
+
if (res.ok) {
|
|
344
|
+
safe(success);
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
fail(error, res.code, res.description);
|
|
348
|
+
}).catch((e: unknown) => fail(error, "session_error", String(e)));
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
setReceiverMuted(muted: any, success: any, error: any): void {
|
|
352
|
+
bridge.call("setReceiverMuted", { sessionId: this.sessionId, muted }).then((res: any) => {
|
|
353
|
+
if (res.ok) {
|
|
354
|
+
safe(success);
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
fail(error, res.code, res.description);
|
|
358
|
+
}).catch((e: unknown) => fail(error, "session_error", String(e)));
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
loadMedia(_req: any, _success: any, error: any): void {
|
|
362
|
+
fail(error, "session_error", "Media is not supported yet");
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
queueLoad(_req: any, _success: any, error: any): void {
|
|
366
|
+
fail(error, "session_error", "Media is not supported yet");
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
const sessions = new Map<string, any>();
|
|
371
|
+
|
|
372
|
+
function upsertSession(snap: any): any {
|
|
373
|
+
let sess = sessions.get(snap.sessionId);
|
|
374
|
+
if (!sess) {
|
|
375
|
+
sess = new Session(snap.sessionId, snap.appId, snap.displayName, [], toReceiver(snap.receiver));
|
|
376
|
+
sessions.set(snap.sessionId, sess);
|
|
377
|
+
}
|
|
378
|
+
applySnapshot(sess, snap);
|
|
379
|
+
return sess;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
let initialized = false;
|
|
383
|
+
let apiConfig: any = null;
|
|
384
|
+
let pendingRequest = false;
|
|
385
|
+
let lastAvailability: string | null = null;
|
|
386
|
+
const receiverActionListeners: any[] = [];
|
|
387
|
+
|
|
388
|
+
function notifyUpdate(sess: any, alive: boolean): void {
|
|
389
|
+
for (const fn of [...sess.updateListeners]) safe(fn, alive);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
bridge.onEvent((event: BridgeEvent) => {
|
|
393
|
+
switch (event.type) {
|
|
394
|
+
case "receiverAvailability": {
|
|
395
|
+
const value = event.available ? "available" : "unavailable";
|
|
396
|
+
if (value === lastAvailability) return;
|
|
397
|
+
lastAvailability = value;
|
|
398
|
+
if (apiConfig) safe(apiConfig.receiverListener, value);
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
case "sessionUpdated": {
|
|
402
|
+
const sess = sessions.get(event.session.sessionId);
|
|
403
|
+
if (!sess) return;
|
|
404
|
+
applySnapshot(sess, event.session);
|
|
405
|
+
notifyUpdate(sess, true);
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
case "sessionEnded": {
|
|
409
|
+
const sess = sessions.get(event.sessionId);
|
|
410
|
+
if (!sess) return;
|
|
411
|
+
sess.status = event.status;
|
|
412
|
+
notifyUpdate(sess, false);
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
case "message": {
|
|
416
|
+
const sess = sessions.get(event.sessionId);
|
|
417
|
+
if (!sess) return;
|
|
418
|
+
const fns = sess.messageListeners.get(event.namespace) ?? [];
|
|
419
|
+
for (const fn of [...fns]) safe(fn, event.namespace, event.message);
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
function initialize(apiCfg: any, success: any, error: any): void {
|
|
426
|
+
const req = apiCfg && apiCfg.sessionRequest;
|
|
427
|
+
const valid = !!req && typeof req === "object" && typeof req.appId === "string"
|
|
428
|
+
&& typeof apiCfg.sessionListener === "function" && typeof apiCfg.receiverListener === "function";
|
|
429
|
+
if (!valid) {
|
|
430
|
+
fail(error, "invalid_parameter");
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
bridge.call("initialize", {
|
|
434
|
+
appId: req.appId,
|
|
435
|
+
capabilities: Array.isArray(req.capabilities) ? req.capabilities : [],
|
|
436
|
+
autoJoinPolicy: apiCfg.autoJoinPolicy ?? "tab_and_origin_scoped",
|
|
437
|
+
}).then((res: any) => {
|
|
438
|
+
if (!res.ok) {
|
|
439
|
+
fail(error, res.code, res.description);
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
const value = res.value ?? {};
|
|
443
|
+
initialized = true;
|
|
444
|
+
apiConfig = apiCfg;
|
|
445
|
+
const availability = value.available ? "available" : "unavailable";
|
|
446
|
+
lastAvailability = availability;
|
|
447
|
+
safe(success);
|
|
448
|
+
safe(apiCfg.receiverListener, availability);
|
|
449
|
+
for (const snap of value.sessions ?? []) safe(apiCfg.sessionListener, upsertSession(snap));
|
|
450
|
+
}).catch((e: unknown) => fail(error, "session_error", String(e)));
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
function requestSession(success: any, error: any, optSessionRequest?: any): void {
|
|
454
|
+
if (!initialized) {
|
|
455
|
+
fail(error, "api_not_initialized");
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
if (pendingRequest) {
|
|
459
|
+
fail(error, "invalid_parameter");
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
const req = optSessionRequest ?? apiConfig.sessionRequest;
|
|
463
|
+
pendingRequest = true;
|
|
464
|
+
bridge.call("requestSession", {
|
|
465
|
+
appId: req.appId,
|
|
466
|
+
capabilities: Array.isArray(req.capabilities) ? req.capabilities : [],
|
|
467
|
+
timeoutMs: req.requestSessionTimeout ?? 60000,
|
|
468
|
+
}).then((res: any) => {
|
|
469
|
+
pendingRequest = false;
|
|
470
|
+
if (!res.ok) {
|
|
471
|
+
fail(error, res.code, res.description);
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
const sess = upsertSession(res.value);
|
|
475
|
+
for (const fn of [...receiverActionListeners]) safe(fn, sess.receiver, ReceiverAction.CAST);
|
|
476
|
+
safe(success, sess);
|
|
477
|
+
}).catch((e: unknown) => {
|
|
478
|
+
pendingRequest = false;
|
|
479
|
+
fail(error, "session_error", String(e));
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
function requestSessionById(sessionId: any): void {
|
|
484
|
+
bridge.call("joinSession", { sessionId }).then((res: any) => {
|
|
485
|
+
if (!res.ok) return;
|
|
486
|
+
const sess = upsertSession(res.value);
|
|
487
|
+
if (apiConfig) safe(apiConfig.sessionListener, sess);
|
|
488
|
+
}).catch((e: unknown) => console.error(e));
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
function addReceiverActionListener(fn: any): void {
|
|
492
|
+
if (typeof fn === "function" && !receiverActionListeners.includes(fn)) receiverActionListeners.push(fn);
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
function removeReceiverActionListener(fn: any): void {
|
|
496
|
+
const i = receiverActionListeners.indexOf(fn);
|
|
497
|
+
if (i >= 0) receiverActionListeners.splice(i, 1);
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
function setCustomReceivers(_receivers: any, success: any, _error: any): void {
|
|
501
|
+
setTimeout(() => safe(success), 0);
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
function setReceiverDisplayStatus(_receiver: any, success: any, _error: any): void {
|
|
505
|
+
setTimeout(() => safe(success), 0);
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
function logMessage(_msg: any): void {}
|
|
509
|
+
|
|
510
|
+
function precache(_data: any): void {}
|
|
511
|
+
|
|
512
|
+
function unescape(s: any): any {
|
|
513
|
+
try {
|
|
514
|
+
return decodeURIComponent(s);
|
|
515
|
+
} catch {
|
|
516
|
+
return s;
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
g.chrome.cast = {
|
|
521
|
+
VERSION,
|
|
522
|
+
isAvailable: true,
|
|
523
|
+
AutoJoinPolicy,
|
|
524
|
+
Capability,
|
|
525
|
+
DefaultActionPolicy,
|
|
526
|
+
DialAppState,
|
|
527
|
+
ErrorCode,
|
|
528
|
+
ReceiverAction,
|
|
529
|
+
ReceiverAvailability,
|
|
530
|
+
ReceiverType,
|
|
531
|
+
SenderPlatform,
|
|
532
|
+
SessionStatus,
|
|
533
|
+
VolumeControlType,
|
|
534
|
+
timeout: new Timeout(),
|
|
535
|
+
media,
|
|
536
|
+
ApiConfig,
|
|
537
|
+
SessionRequest,
|
|
538
|
+
Error: CastError,
|
|
539
|
+
Image,
|
|
540
|
+
Receiver,
|
|
541
|
+
ReceiverDisplayStatus,
|
|
542
|
+
SenderApplication,
|
|
543
|
+
Volume,
|
|
544
|
+
Timeout,
|
|
545
|
+
DialRequest,
|
|
546
|
+
CredentialsData,
|
|
547
|
+
Session,
|
|
548
|
+
initialize,
|
|
549
|
+
requestSession,
|
|
550
|
+
requestSessionById,
|
|
551
|
+
addReceiverActionListener,
|
|
552
|
+
removeReceiverActionListener,
|
|
553
|
+
setCustomReceivers,
|
|
554
|
+
setReceiverDisplayStatus,
|
|
555
|
+
logMessage,
|
|
556
|
+
precache,
|
|
557
|
+
unescape,
|
|
558
|
+
};
|
|
559
|
+
|
|
560
|
+
let onGCastApiAvailable: any = g.__onGCastApiAvailable;
|
|
561
|
+
const notifyGCastApiAvailable = (): void => {
|
|
562
|
+
setTimeout(() => safe(onGCastApiAvailable, true), 0);
|
|
563
|
+
};
|
|
564
|
+
if (typeof onGCastApiAvailable === "function") notifyGCastApiAvailable();
|
|
565
|
+
Object.defineProperty(g, "__onGCastApiAvailable", {
|
|
566
|
+
configurable: true,
|
|
567
|
+
enumerable: true,
|
|
568
|
+
get: () => onGCastApiAvailable,
|
|
569
|
+
set: (v: any) => {
|
|
570
|
+
onGCastApiAvailable = v;
|
|
571
|
+
if (typeof v === "function") notifyGCastApiAvailable();
|
|
572
|
+
},
|
|
573
|
+
});
|
|
574
|
+
|
|
575
|
+
return true;
|
|
576
|
+
}
|