electron-cast 0.3.0 → 0.4.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/CHANGELOG.md +66 -0
- package/README.md +122 -12
- package/dist/cjs/client/auth/build-time.d.ts +7 -0
- package/dist/cjs/client/auth/build-time.js +11 -0
- package/dist/cjs/client/auth/build-time.js.map +1 -0
- package/dist/cjs/client/auth/cast-auth.d.ts +44 -0
- package/dist/cjs/client/auth/cast-auth.js +145 -0
- package/dist/cjs/client/auth/cast-auth.js.map +1 -0
- package/dist/cjs/client/auth/cast-cert-validator.d.ts +34 -0
- package/dist/cjs/client/auth/cast-cert-validator.js +89 -0
- package/dist/cjs/client/auth/cast-cert-validator.js.map +1 -0
- package/dist/cjs/client/auth/crl.d.ts +45 -0
- package/dist/cjs/client/auth/crl.js +227 -0
- package/dist/cjs/client/auth/crl.js.map +1 -0
- package/dist/cjs/client/auth/der.d.ts +62 -0
- package/dist/cjs/client/auth/der.js +208 -0
- package/dist/cjs/client/auth/der.js.map +1 -0
- package/dist/cjs/client/auth/names.d.ts +52 -0
- package/dist/cjs/client/auth/names.js +447 -0
- package/dist/cjs/client/auth/names.js.map +1 -0
- package/dist/cjs/client/auth/path-validator.d.ts +29 -0
- package/dist/cjs/client/auth/path-validator.js +421 -0
- package/dist/cjs/client/auth/path-validator.js.map +1 -0
- package/dist/cjs/client/auth/trust-data.d.ts +24 -0
- package/dist/cjs/client/auth/trust-data.js +97 -0
- package/dist/cjs/client/auth/trust-data.js.map +1 -0
- package/dist/cjs/client/auth/x509.d.ts +109 -0
- package/dist/cjs/client/auth/x509.js +442 -0
- package/dist/cjs/client/auth/x509.js.map +1 -0
- package/dist/cjs/client/cast-client.d.ts +111 -0
- package/dist/cjs/client/cast-client.js +505 -0
- package/dist/cjs/client/cast-client.js.map +1 -0
- package/dist/cjs/client/errors.d.ts +28 -0
- package/dist/cjs/client/errors.js +50 -0
- package/dist/cjs/client/errors.js.map +1 -0
- package/dist/cjs/client/index.d.ts +6 -0
- package/dist/cjs/client/index.js +21 -0
- package/dist/cjs/client/index.js.map +1 -0
- package/dist/cjs/client/namespaces.d.ts +9 -0
- package/dist/cjs/client/namespaces.js +13 -0
- package/dist/cjs/client/namespaces.js.map +1 -0
- package/dist/cjs/client/types.d.ts +27 -0
- package/dist/cjs/client/types.js +3 -0
- package/dist/cjs/client/types.js.map +1 -0
- package/dist/cjs/discovery/cast-discovery.d.ts +103 -0
- package/dist/cjs/discovery/cast-discovery.js +323 -0
- package/dist/cjs/discovery/cast-discovery.js.map +1 -0
- package/dist/cjs/discovery/device-cache.d.ts +45 -0
- package/dist/cjs/discovery/device-cache.js +240 -0
- package/dist/cjs/discovery/device-cache.js.map +1 -0
- package/dist/cjs/discovery/dns-packet.d.ts +70 -0
- package/dist/cjs/discovery/dns-packet.js +251 -0
- package/dist/cjs/discovery/dns-packet.js.map +1 -0
- package/dist/cjs/discovery/index.d.ts +4 -0
- package/dist/cjs/discovery/index.js +21 -0
- package/dist/cjs/discovery/index.js.map +1 -0
- package/dist/cjs/discovery/udp-socket.d.ts +16 -0
- package/dist/cjs/discovery/udp-socket.js +163 -0
- package/dist/cjs/discovery/udp-socket.js.map +1 -0
- package/dist/cjs/electron/main.d.ts +86 -0
- package/dist/cjs/electron/main.js +408 -0
- package/dist/cjs/electron/main.js.map +1 -0
- package/dist/cjs/electron/manager-api.d.ts +132 -0
- package/dist/cjs/electron/manager-api.js +207 -0
- package/dist/cjs/electron/manager-api.js.map +1 -0
- package/dist/cjs/electron/preload.d.ts +39 -0
- package/dist/cjs/electron/preload.js +96 -0
- package/dist/cjs/electron/preload.js.map +1 -0
- package/dist/cjs/electron/presentation-envelope.d.ts +60 -0
- package/dist/cjs/electron/presentation-envelope.js +318 -0
- package/dist/cjs/electron/presentation-envelope.js.map +1 -0
- package/dist/cjs/electron/presentation-handler.d.ts +100 -0
- package/dist/cjs/electron/presentation-handler.js +499 -0
- package/dist/cjs/electron/presentation-handler.js.map +1 -0
- package/dist/cjs/electron/session-service.d.ts +174 -0
- package/dist/cjs/electron/session-service.js +891 -0
- package/dist/cjs/electron/session-service.js.map +1 -0
- package/dist/cjs/index.d.ts +8 -0
- package/dist/cjs/index.js +26 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/polyfill/bridge-types.d.ts +86 -0
- package/dist/cjs/polyfill/bridge-types.js +3 -0
- package/dist/cjs/polyfill/bridge-types.js.map +1 -0
- package/dist/cjs/polyfill/install.d.ts +12 -0
- package/dist/cjs/polyfill/install.js +1569 -0
- package/dist/cjs/polyfill/install.js.map +1 -0
- package/dist/cjs/polyfill/presentation-standin.d.ts +13 -0
- package/dist/cjs/polyfill/presentation-standin.js +279 -0
- package/dist/cjs/polyfill/presentation-standin.js.map +1 -0
- package/dist/cjs/protocol/cast-message.d.ts +20 -0
- package/dist/cjs/protocol/cast-message.js +171 -0
- package/dist/cjs/protocol/cast-message.js.map +1 -0
- package/dist/cjs/protocol/device-auth.d.ts +61 -0
- package/dist/cjs/protocol/device-auth.js +113 -0
- package/dist/cjs/protocol/device-auth.js.map +1 -0
- package/dist/cjs/protocol/errors.d.ts +4 -0
- package/dist/cjs/protocol/errors.js +12 -0
- package/dist/cjs/protocol/errors.js.map +1 -0
- package/dist/cjs/protocol/framing.d.ts +18 -0
- package/dist/cjs/protocol/framing.js +100 -0
- package/dist/cjs/protocol/framing.js.map +1 -0
- package/dist/cjs/protocol/index.d.ts +4 -0
- package/dist/cjs/protocol/index.js +21 -0
- package/dist/cjs/protocol/index.js.map +1 -0
- package/dist/cjs/protocol/proto-wire.d.ts +11 -0
- package/dist/cjs/protocol/proto-wire.js +64 -0
- package/dist/cjs/protocol/proto-wire.js.map +1 -0
- package/dist/client/auth/build-time.d.ts +7 -0
- package/dist/client/auth/build-time.js +8 -0
- package/dist/client/auth/build-time.js.map +1 -0
- package/dist/client/auth/cast-auth.d.ts +44 -0
- package/dist/client/auth/cast-auth.js +141 -0
- package/dist/client/auth/cast-auth.js.map +1 -0
- package/dist/client/auth/cast-cert-validator.d.ts +34 -0
- package/dist/client/auth/cast-cert-validator.js +86 -0
- package/dist/client/auth/cast-cert-validator.js.map +1 -0
- package/dist/client/auth/crl.d.ts +45 -0
- package/dist/client/auth/crl.js +221 -0
- package/dist/client/auth/crl.js.map +1 -0
- package/dist/client/auth/der.d.ts +62 -0
- package/dist/client/auth/der.js +196 -0
- package/dist/client/auth/der.js.map +1 -0
- package/dist/client/auth/names.d.ts +52 -0
- package/dist/client/auth/names.js +438 -0
- package/dist/client/auth/names.js.map +1 -0
- package/dist/client/auth/path-validator.d.ts +29 -0
- package/dist/client/auth/path-validator.js +417 -0
- package/dist/client/auth/path-validator.js.map +1 -0
- package/dist/client/auth/trust-data.d.ts +24 -0
- package/dist/client/auth/trust-data.js +94 -0
- package/dist/client/auth/trust-data.js.map +1 -0
- package/dist/client/auth/x509.d.ts +109 -0
- package/dist/client/auth/x509.js +430 -0
- package/dist/client/auth/x509.js.map +1 -0
- package/dist/client/cast-client.d.ts +20 -1
- package/dist/client/cast-client.js +74 -3
- package/dist/client/cast-client.js.map +1 -1
- package/dist/client/errors.d.ts +11 -0
- package/dist/client/errors.js +13 -0
- package/dist/client/errors.js.map +1 -1
- package/dist/client/index.d.ts +2 -0
- package/dist/discovery/cast-discovery.d.ts +21 -2
- package/dist/discovery/cast-discovery.js +94 -14
- package/dist/discovery/cast-discovery.js.map +1 -1
- package/dist/discovery/device-cache.d.ts +6 -1
- package/dist/discovery/device-cache.js +31 -7
- package/dist/discovery/device-cache.js.map +1 -1
- package/dist/discovery/udp-socket.d.ts +5 -1
- package/dist/discovery/udp-socket.js +56 -5
- package/dist/discovery/udp-socket.js.map +1 -1
- package/dist/electron/main.d.ts +26 -4
- package/dist/electron/main.js +193 -55
- package/dist/electron/main.js.map +1 -1
- package/dist/electron/manager-api.d.ts +132 -0
- package/dist/electron/manager-api.js +198 -0
- package/dist/electron/manager-api.js.map +1 -0
- package/dist/electron/preload.d.ts +5 -0
- package/dist/electron/preload.js +9 -1
- package/dist/electron/preload.js.map +1 -1
- package/dist/electron/presentation-envelope.js +7 -1
- package/dist/electron/presentation-envelope.js.map +1 -1
- package/dist/electron/presentation-handler.js +16 -9
- package/dist/electron/presentation-handler.js.map +1 -1
- package/dist/electron/session-service.d.ts +50 -1
- package/dist/electron/session-service.js +256 -33
- package/dist/electron/session-service.js.map +1 -1
- package/dist/polyfill/presentation-standin.js +3 -2
- package/dist/polyfill/presentation-standin.js.map +1 -1
- package/dist/protocol/device-auth.d.ts +61 -0
- package/dist/protocol/device-auth.js +106 -0
- package/dist/protocol/device-auth.js.map +1 -0
- package/dist/protocol/index.d.ts +1 -0
- package/dist/protocol/index.js +1 -0
- package/dist/protocol/index.js.map +1 -1
- package/dist/protocol/proto-wire.d.ts +11 -0
- package/dist/protocol/proto-wire.js +60 -0
- package/dist/protocol/proto-wire.js.map +1 -0
- package/package.json +47 -12
- package/src/client/auth/build-time.ts +7 -0
- package/src/client/auth/cast-auth.ts +184 -0
- package/src/client/auth/cast-cert-validator.ts +133 -0
- package/src/client/auth/crl.ts +228 -0
- package/src/client/auth/der.ts +193 -0
- package/src/client/auth/names.ts +411 -0
- package/src/client/auth/path-validator.ts +449 -0
- package/src/client/auth/trust-data.ts +95 -0
- package/src/client/auth/x509.ts +488 -0
- package/src/client/cast-client.ts +85 -3
- package/src/client/errors.ts +29 -0
- package/src/client/index.ts +2 -0
- package/src/discovery/cast-discovery.ts +104 -19
- package/src/discovery/device-cache.ts +38 -8
- package/src/discovery/udp-socket.ts +63 -4
- package/src/electron/main.ts +220 -60
- package/src/electron/manager-api.ts +312 -0
- package/src/electron/preload.ts +15 -1
- package/src/electron/presentation-envelope.ts +7 -1
- package/src/electron/presentation-handler.ts +16 -10
- package/src/electron/session-service.ts +265 -34
- package/src/polyfill/presentation-standin.ts +3 -2
- package/src/protocol/device-auth.ts +126 -0
- package/src/protocol/index.ts +1 -0
- package/src/protocol/proto-wire.ts +55 -0
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
// The main-process API of the cast manager, modelled on Chrome's Media Router (see docs/MAIN-API.md for the mapping):
|
|
2
|
+
// sinks (devices) and routes (sessions) are observed through observers, a route is terminated or detached, and
|
|
3
|
+
// volume goes through a route controller. It is kept apart from main.ts and session-service.ts, which only expose
|
|
4
|
+
// the narrow hooks used here.
|
|
5
|
+
import type { CastDevice } from "../discovery/index.ts";
|
|
6
|
+
import type { BridgeResult } from "../polyfill/bridge-types.ts";
|
|
7
|
+
import type { DeviceSource } from "./session-service.ts";
|
|
8
|
+
|
|
9
|
+
/** Chrome's `SinkIconType` values that apply to Cast devices. */
|
|
10
|
+
export type SinkIconType = "cast" | "cast_audio" | "cast_audio_group";
|
|
11
|
+
|
|
12
|
+
/** A device a route can be created on (Chrome: `media_router::MediaSink`). */
|
|
13
|
+
export interface MediaSink {
|
|
14
|
+
id: string;
|
|
15
|
+
name: string;
|
|
16
|
+
/** The device model. electron-cast's own: Chrome's MediaSink has none (the model is `CastSinkExtraData::model_name`). */
|
|
17
|
+
description: string;
|
|
18
|
+
iconType: SinkIconType;
|
|
19
|
+
/** The discovered device behind the sink. */
|
|
20
|
+
device: CastDevice;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** What a sinks observer wants sinks for (Chrome: the observer's `MediaSource`); omit it for every device. */
|
|
24
|
+
export interface MediaSinksQuery {
|
|
25
|
+
/** Only sinks with all of these capabilities (`video_out`, `audio_out`, ...), like `requestSession`'s. */
|
|
26
|
+
capabilities?: string[];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Chrome: `MediaSinksObserver`. */
|
|
30
|
+
export interface MediaSinksObserver {
|
|
31
|
+
/** The full current list, on registration and then each time it changes. */
|
|
32
|
+
onSinksUpdated(sinks: MediaSink[]): void;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** A live session on a sink (Chrome: `media_router::MediaRoute`; a session is what Chrome's Cast provider routes to). */
|
|
36
|
+
export interface MediaRoute {
|
|
37
|
+
/** The session id (Chrome: `media_route_id`). */
|
|
38
|
+
id: string;
|
|
39
|
+
/** `cast:<appId>`, the Cast source URN's scheme and app id (Chrome: `media_source`). */
|
|
40
|
+
mediaSource: string;
|
|
41
|
+
sinkId: string;
|
|
42
|
+
/** The app's status text, else its name (Chrome: `description`). */
|
|
43
|
+
description: string;
|
|
44
|
+
// Fields below are electron-cast's: Chrome's route knows its page through the extension or the Presentation API.
|
|
45
|
+
appId: string;
|
|
46
|
+
displayName: string;
|
|
47
|
+
/** Origin of the page that started the session. */
|
|
48
|
+
origin: string;
|
|
49
|
+
/** Receiver volume as last reported; `null` until the device reports one. */
|
|
50
|
+
volume: { level: number | null; muted: boolean | null };
|
|
51
|
+
/** `webContents.id` of every page currently attached. */
|
|
52
|
+
members: number[];
|
|
53
|
+
/** `webContents.id` of pages that navigated away and may rejoin; they get no events meanwhile. */
|
|
54
|
+
parkedSenders: number[];
|
|
55
|
+
/** True when no page is attached: the route lives on in the device only. */
|
|
56
|
+
parked: boolean;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Chrome: `MediaRoutesObserver`. */
|
|
60
|
+
export interface MediaRoutesObserver {
|
|
61
|
+
/** The full current list, on registration and then each time a route starts, ends or changes. */
|
|
62
|
+
onRoutesUpdated(routes: MediaRoute[]): void;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Chrome: `media_router::mojom::MediaController`, here only the volume half (`SetMute`, `SetVolume`). */
|
|
66
|
+
export interface MediaRouteController {
|
|
67
|
+
setMute(muted: boolean): Promise<void>;
|
|
68
|
+
/** 0 to 1. */
|
|
69
|
+
setVolume(level: number): Promise<void>;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** What a failed route call rejects with; `code` is a bridge code or `"route_not_found"`. */
|
|
73
|
+
export class MediaRouteError extends Error {
|
|
74
|
+
readonly code: string;
|
|
75
|
+
constructor(code: string, message?: string) {
|
|
76
|
+
super(message ?? code);
|
|
77
|
+
this.name = "MediaRouteError";
|
|
78
|
+
this.code = code;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The main-process API added to {@link CastManager}. Each method says which `MediaRouter` method it mirrors. */
|
|
83
|
+
export interface CastManagerApi {
|
|
84
|
+
/** `MediaRouter::RegisterMediaSinksObserver`. Returns the unregister function. */
|
|
85
|
+
registerMediaSinksObserver(observer: MediaSinksObserver, query?: MediaSinksQuery): () => void;
|
|
86
|
+
/** `MediaRouter::RegisterMediaRoutesObserver`. Returns the unregister function. */
|
|
87
|
+
registerMediaRoutesObserver(observer: MediaRoutesObserver): () => void;
|
|
88
|
+
/** The current routes (what `MediaRoutesObserver` last got), parked ones included. */
|
|
89
|
+
getRoutes(): MediaRoute[];
|
|
90
|
+
/** `MediaRouter::TerminateRoute`: stops the receiver app; pages get `sessionEnded` as if the device had. */
|
|
91
|
+
terminateRoute(routeId: string): Promise<void>;
|
|
92
|
+
/** electron-cast's own (Chrome's Cast provider leaves `DetachRoute` unimplemented): every page lets go of the route and its transport closes; the app keeps running. */
|
|
93
|
+
detachRoute(routeId: string): void;
|
|
94
|
+
/** `MediaRouter::GetMediaController` / `mojom::MediaController`: volume and mute go to the receiver, so pages see `sessionUpdated`. */
|
|
95
|
+
getRouteController(routeId: string): MediaRouteController;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Third argument of `selectDevice`. Like Chrome's dialog, which registers a sinks observer while it is open, the
|
|
100
|
+
* picker hears of sinks that appear (mDNS often takes seconds) until it answers. Old two-argument pickers ignore it.
|
|
101
|
+
*/
|
|
102
|
+
export interface DevicePickerContext {
|
|
103
|
+
/** Aborted when the request no longer waits for a choice (answered, cancelled, page gone, manager disposed). */
|
|
104
|
+
readonly signal: AbortSignal;
|
|
105
|
+
/**
|
|
106
|
+
* Like `CastManagerApi.registerMediaSinksObserver`, restricted to the devices that can serve this request. The
|
|
107
|
+
* observer is also dropped when `signal` aborts.
|
|
108
|
+
*/
|
|
109
|
+
registerMediaSinksObserver(observer: MediaSinksObserver): () => void;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** What the session service exposes to the API (a narrow slice of it). */
|
|
113
|
+
export interface SessionHost {
|
|
114
|
+
mediaRoutes(): MediaRoute[];
|
|
115
|
+
observeSessions(listener: () => void): () => void;
|
|
116
|
+
terminateSession(sessionId: string): Promise<BridgeResult>;
|
|
117
|
+
setSessionVolume(sessionId: string, volume: { level: number } | { muted: boolean }): Promise<BridgeResult>;
|
|
118
|
+
detachSession(sessionId: string): BridgeResult;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** A Map that reports new and removed keys, so the service needs no per-site notification. */
|
|
122
|
+
export class ObservedMap<K, V> extends Map<K, V> {
|
|
123
|
+
private readonly notify: (kind: "started" | "ended", value: V) => void;
|
|
124
|
+
constructor(notify: (kind: "started" | "ended", value: V) => void) {
|
|
125
|
+
super();
|
|
126
|
+
this.notify = notify;
|
|
127
|
+
}
|
|
128
|
+
override set(key: K, value: V): this {
|
|
129
|
+
const isNew = !this.has(key);
|
|
130
|
+
super.set(key, value);
|
|
131
|
+
if (isNew) this.notify("started", value);
|
|
132
|
+
return this;
|
|
133
|
+
}
|
|
134
|
+
override delete(key: K): boolean {
|
|
135
|
+
const value = this.get(key);
|
|
136
|
+
const had = super.delete(key);
|
|
137
|
+
if (had) this.notify("ended", value as V);
|
|
138
|
+
return had;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** The sink for a discovered device. */
|
|
143
|
+
export function toMediaSink(device: CastDevice): MediaSink {
|
|
144
|
+
// GetCastSinkIconType (media_sink_util.cc): video_out first, then a multizone group, else audio.
|
|
145
|
+
const iconType: SinkIconType = (device.capabilities & 1) !== 0 ? "cast" : (device.capabilities & 32) !== 0 ? "cast_audio_group" : "cast_audio";
|
|
146
|
+
return { id: device.id, name: device.name, description: device.model, iconType, device: { ...device } };
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Keeps observers of a changing list. Each gets the list on registration and after every change; a throwing observer
|
|
151
|
+
* is logged and does not stop the others.
|
|
152
|
+
*/
|
|
153
|
+
class ObserverSet<T> {
|
|
154
|
+
private readonly observers = new Set<(items: T[]) => void>();
|
|
155
|
+
private readonly list: () => T[];
|
|
156
|
+
constructor(list: () => T[]) {
|
|
157
|
+
this.list = list;
|
|
158
|
+
}
|
|
159
|
+
add(call: (items: T[]) => void): () => void {
|
|
160
|
+
this.observers.add(call);
|
|
161
|
+
this.deliver(call, this.list());
|
|
162
|
+
return () => void this.observers.delete(call);
|
|
163
|
+
}
|
|
164
|
+
notify(): void {
|
|
165
|
+
const items = this.list();
|
|
166
|
+
for (const o of [...this.observers]) this.deliver(o, items);
|
|
167
|
+
}
|
|
168
|
+
clear(): void {
|
|
169
|
+
this.observers.clear();
|
|
170
|
+
}
|
|
171
|
+
private deliver(call: (items: T[]) => void, items: T[]): void {
|
|
172
|
+
try {
|
|
173
|
+
call(items);
|
|
174
|
+
} catch (err) {
|
|
175
|
+
console.error("electron-cast: a media router observer failed", err);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Sinks observation over a device source: `registerMediaSinksObserver` plus the source subscription, which is made
|
|
182
|
+
* while at least one observer is registered. A change that leaves the (filtered) list as it was is not reported.
|
|
183
|
+
*/
|
|
184
|
+
export function createSinksObservers(source: DeviceSource): {
|
|
185
|
+
register(observer: MediaSinksObserver, query?: MediaSinksQuery, signal?: AbortSignal, eligible?: (d: CastDevice) => boolean): () => void;
|
|
186
|
+
dispose(): void;
|
|
187
|
+
} {
|
|
188
|
+
const events = ["deviceUp", "deviceDown", "deviceChanged"] as const;
|
|
189
|
+
const entries = new Set<() => void>();
|
|
190
|
+
const onChange = (): void => {
|
|
191
|
+
for (const e of [...entries]) e();
|
|
192
|
+
};
|
|
193
|
+
const attach = (): void => {
|
|
194
|
+
if (entries.size === 1) for (const e of events) source.on(e, onChange);
|
|
195
|
+
};
|
|
196
|
+
const detach = (): void => {
|
|
197
|
+
if (entries.size === 0) for (const e of events) source.off(e, onChange);
|
|
198
|
+
};
|
|
199
|
+
return {
|
|
200
|
+
register(observer, query, signal, eligible) {
|
|
201
|
+
const caps = query?.capabilities;
|
|
202
|
+
const matches = (d: CastDevice): boolean => (eligible?.(d) ?? true) && (caps === undefined || capsMatch(d, caps));
|
|
203
|
+
const current = (): MediaSink[] => source.devices().filter(matches).map(toMediaSink);
|
|
204
|
+
let last = "";
|
|
205
|
+
const check = (): void => {
|
|
206
|
+
const sinks = current();
|
|
207
|
+
const key = JSON.stringify(sinks);
|
|
208
|
+
if (key === last) return;
|
|
209
|
+
last = key;
|
|
210
|
+
try {
|
|
211
|
+
observer.onSinksUpdated(sinks);
|
|
212
|
+
} catch (err) {
|
|
213
|
+
console.error("electron-cast: a media sinks observer failed", err);
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
const unregister = (): void => {
|
|
217
|
+
entries.delete(check);
|
|
218
|
+
signal?.removeEventListener("abort", unregister);
|
|
219
|
+
detach();
|
|
220
|
+
};
|
|
221
|
+
if (signal?.aborted) return () => {};
|
|
222
|
+
entries.add(check);
|
|
223
|
+
attach();
|
|
224
|
+
signal?.addEventListener("abort", unregister, { once: true });
|
|
225
|
+
check();
|
|
226
|
+
return unregister;
|
|
227
|
+
},
|
|
228
|
+
dispose() {
|
|
229
|
+
entries.clear();
|
|
230
|
+
detach();
|
|
231
|
+
},
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const CAPABILITY_BITS: Record<string, number> = { video_out: 1, video_in: 2, audio_out: 4, audio_in: 8, multizone_group: 32 };
|
|
236
|
+
|
|
237
|
+
/** True when every named capability is set on the device (unknown names are ignored, as in the session service). */
|
|
238
|
+
function capsMatch(device: CastDevice, capabilities: string[]): boolean {
|
|
239
|
+
for (const cap of capabilities) {
|
|
240
|
+
const bit = CAPABILITY_BITS[cap];
|
|
241
|
+
if (bit !== undefined && (device.capabilities & bit) === 0) return false;
|
|
242
|
+
}
|
|
243
|
+
return true;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** A picker context for one pending request; `close()` aborts its signal and drops its observers. */
|
|
247
|
+
export function createPickerContext(
|
|
248
|
+
sinks: ReturnType<typeof createSinksObservers>,
|
|
249
|
+
eligible: (device: CastDevice) => boolean,
|
|
250
|
+
): { context: DevicePickerContext; close(): void } {
|
|
251
|
+
const ac = new AbortController();
|
|
252
|
+
const context: DevicePickerContext = {
|
|
253
|
+
signal: ac.signal,
|
|
254
|
+
registerMediaSinksObserver: (observer) => sinks.register(observer, undefined, ac.signal, eligible),
|
|
255
|
+
};
|
|
256
|
+
return { context, close: () => ac.abort() };
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function must(result: BridgeResult): void {
|
|
260
|
+
if (!result.ok) throw new MediaRouteError(result.code, result.description);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/** Wire the device source and the session host into the {@link CastManagerApi}. */
|
|
264
|
+
export function createManagerApi(
|
|
265
|
+
sinks: ReturnType<typeof createSinksObservers>,
|
|
266
|
+
host: SessionHost,
|
|
267
|
+
): CastManagerApi & { dispose(): void } {
|
|
268
|
+
const routeObservers = new ObserverSet<MediaRoute>(() => host.mediaRoutes());
|
|
269
|
+
const unobserve = host.observeSessions(() => routeObservers.notify());
|
|
270
|
+
|
|
271
|
+
const need = (routeId: string): void => {
|
|
272
|
+
if (!host.mediaRoutes().some((r) => r.id === routeId)) {
|
|
273
|
+
throw new MediaRouteError("route_not_found", `no route ${routeId}`);
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
return {
|
|
278
|
+
registerMediaSinksObserver: (observer, query) => sinks.register(observer, query),
|
|
279
|
+
registerMediaRoutesObserver: (observer) => routeObservers.add((routes) => observer.onRoutesUpdated(routes)),
|
|
280
|
+
getRoutes: () => host.mediaRoutes(),
|
|
281
|
+
async terminateRoute(routeId) {
|
|
282
|
+
need(routeId);
|
|
283
|
+
must(await host.terminateSession(routeId));
|
|
284
|
+
},
|
|
285
|
+
detachRoute(routeId) {
|
|
286
|
+
need(routeId);
|
|
287
|
+
must(host.detachSession(routeId));
|
|
288
|
+
},
|
|
289
|
+
getRouteController(routeId) {
|
|
290
|
+
need(routeId);
|
|
291
|
+
return {
|
|
292
|
+
async setMute(muted) {
|
|
293
|
+
if (typeof muted !== "boolean") throw new TypeError("muted must be a boolean");
|
|
294
|
+
need(routeId);
|
|
295
|
+
must(await host.setSessionVolume(routeId, { muted }));
|
|
296
|
+
},
|
|
297
|
+
async setVolume(level) {
|
|
298
|
+
if (typeof level !== "number" || !Number.isFinite(level) || level < 0 || level > 1) {
|
|
299
|
+
throw new RangeError("volume level must be a number from 0 to 1");
|
|
300
|
+
}
|
|
301
|
+
need(routeId);
|
|
302
|
+
must(await host.setSessionVolume(routeId, { level }));
|
|
303
|
+
},
|
|
304
|
+
};
|
|
305
|
+
},
|
|
306
|
+
dispose() {
|
|
307
|
+
unobserve();
|
|
308
|
+
routeObservers.clear();
|
|
309
|
+
sinks.dispose();
|
|
310
|
+
},
|
|
311
|
+
};
|
|
312
|
+
}
|
package/src/electron/preload.ts
CHANGED
|
@@ -27,6 +27,16 @@ export interface CastBridgeOptions {
|
|
|
27
27
|
*/
|
|
28
28
|
mode?: "chromecast" | "presentation";
|
|
29
29
|
presentationBridgeKey?: string; // default "__electronCastPresentationBridge"
|
|
30
|
+
/**
|
|
31
|
+
* Whether the frame has transient user activation right now. Default: `navigator.userActivation.isActive` read in
|
|
32
|
+
* the preload's own (isolated) world, false when the property is missing. Presentation mode only.
|
|
33
|
+
*/
|
|
34
|
+
isUserActive?: () => boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function frameIsUserActive(): boolean {
|
|
38
|
+
const nav = (globalThis as { navigator?: { userActivation?: { isActive?: unknown } } }).navigator;
|
|
39
|
+
return nav?.userActivation?.isActive === true;
|
|
30
40
|
}
|
|
31
41
|
|
|
32
42
|
function describe(err: unknown): string {
|
|
@@ -81,12 +91,16 @@ function installPresentationBridge(options: CastBridgeOptions, channel: string):
|
|
|
81
91
|
const ipc = options.ipcRenderer;
|
|
82
92
|
if (typeof ipc.send !== "function") throw new TypeError("presentation mode needs ipcRenderer.send");
|
|
83
93
|
const send = ipc.send.bind(ipc);
|
|
94
|
+
const isUserActive = options.isUserActive ?? frameIsUserActive;
|
|
84
95
|
const bridge = {
|
|
85
96
|
/** Forward one call to the main process; never rejects. */
|
|
86
97
|
call(method: string, params: unknown): Promise<unknown> {
|
|
87
98
|
let invoked: Promise<unknown>;
|
|
88
99
|
try {
|
|
89
|
-
|
|
100
|
+
// This runs in the preload's world, inside the page's call, so a page cannot supply its own value: Chrome
|
|
101
|
+
// checks RenderFrameHost::HasTransientUserActivation() in the browser, and this is the nearest Electron offers.
|
|
102
|
+
const sent = method === "start" ? { ...(typeof params === "object" && params !== null ? params : {}), hasGesture: isUserActive() } : params;
|
|
103
|
+
invoked = ipc.invoke(`${channel}:presentation`, method, sent);
|
|
90
104
|
} catch (err) {
|
|
91
105
|
invoked = Promise.reject(err);
|
|
92
106
|
}
|
|
@@ -274,7 +274,10 @@ export function parseEnvelope(raw: string): ParsedEnvelope | null {
|
|
|
274
274
|
return out;
|
|
275
275
|
}
|
|
276
276
|
|
|
277
|
-
/**
|
|
277
|
+
/**
|
|
278
|
+
* The v2_message media request types Chromium forwards to the receiver (IsMediaRequestMessageType in
|
|
279
|
+
* cast_message_util.cc); SET_VOLUME/STOP/LAUNCH are not among them. Any type missing here gets no reply at all.
|
|
280
|
+
*/
|
|
278
281
|
const MEDIA_REQUEST_TYPES = new Set([
|
|
279
282
|
"LOAD",
|
|
280
283
|
"QUEUE_LOAD",
|
|
@@ -289,6 +292,9 @@ const MEDIA_REQUEST_TYPES = new Set([
|
|
|
289
292
|
"QUEUE_UPDATE",
|
|
290
293
|
"QUEUE_REMOVE",
|
|
291
294
|
"QUEUE_REORDER",
|
|
295
|
+
"QUEUE_NEXT",
|
|
296
|
+
"QUEUE_PREV",
|
|
297
|
+
"PRECACHE",
|
|
292
298
|
]);
|
|
293
299
|
|
|
294
300
|
/** True when a v2_message type is one of the media requests forwarded to the receiver. */
|
|
@@ -240,7 +240,8 @@ export class CastPresentationHandler {
|
|
|
240
240
|
snapshot = sessions[0]!;
|
|
241
241
|
} else if (presentationId.startsWith(CAST_SESSION_PREFIX)) {
|
|
242
242
|
const sessionId = presentationId.slice(CAST_SESSION_PREFIX.length);
|
|
243
|
-
|
|
243
|
+
// Like Chrome, also finds a session this run did not start (one still playing from before an app restart).
|
|
244
|
+
const res = await this.service.handle(sender, "joinRunningSession", { sessionId, appId: parsed.appId });
|
|
244
245
|
if (!res.ok) throw new PresentationError("NotFoundError", res.description ?? "No matching route");
|
|
245
246
|
const s = res.value as SessionSnapshot;
|
|
246
247
|
if (s.appId !== parsed.appId) {
|
|
@@ -420,8 +421,14 @@ export class CastPresentationHandler {
|
|
|
420
421
|
});
|
|
421
422
|
if (res.ok) {
|
|
422
423
|
conn.deliver(envelope("app_message", null, conn.clientId, env.sequenceNumber));
|
|
423
|
-
} else {
|
|
424
|
-
|
|
424
|
+
} else if (res.code === "session_error") {
|
|
425
|
+
// Chromium's AppActivity::SendAppMessageToReceiver answers a missing session this way; a failed channel
|
|
426
|
+
// write gets neither an ack nor an error there.
|
|
427
|
+
conn.deliver(envelope("error", {
|
|
428
|
+
code: "session_error",
|
|
429
|
+
description: `Invalid session ID: ${m.sessionId}`,
|
|
430
|
+
details: null,
|
|
431
|
+
}, conn.clientId, env.sequenceNumber));
|
|
425
432
|
}
|
|
426
433
|
}
|
|
427
434
|
|
|
@@ -445,7 +452,8 @@ export class CastPresentationHandler {
|
|
|
445
452
|
}
|
|
446
453
|
}
|
|
447
454
|
if (failed !== null) {
|
|
448
|
-
|
|
455
|
+
// Chromium's SendResultResponse never reports the real cause.
|
|
456
|
+
conn.deliver(envelope("error", { code: "internal_error", description: "unknown error", details: null }, conn.clientId, seq));
|
|
449
457
|
} else {
|
|
450
458
|
conn.deliver(envelope("v2_message", null, conn.clientId, seq));
|
|
451
459
|
}
|
|
@@ -454,7 +462,7 @@ export class CastPresentationHandler {
|
|
|
454
462
|
if (type === "STOP") {
|
|
455
463
|
const res = await this.service.handle(sender, "stopSession", { sessionId: m.sessionId as string });
|
|
456
464
|
if (!res.ok) {
|
|
457
|
-
conn.deliver(envelope("error", { code:
|
|
465
|
+
conn.deliver(envelope("error", { code: "internal_error", description: "unknown error", details: null }, conn.clientId, seq));
|
|
458
466
|
return;
|
|
459
467
|
}
|
|
460
468
|
conn.deliver(envelope("v2_message", null, conn.clientId, seq));
|
|
@@ -478,13 +486,11 @@ export class CastPresentationHandler {
|
|
|
478
486
|
namespace: MEDIA_NAMESPACE,
|
|
479
487
|
message: JSON.stringify(body),
|
|
480
488
|
});
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
conn.deliver(envelope("error", { code: res.code, description: res.description ?? null, details: null }, conn.clientId, seq));
|
|
484
|
-
}
|
|
489
|
+
// A request that could not be sent gets no reply, as in Chromium (SendMediaRequestToReceiver returns no id).
|
|
490
|
+
if (!res.ok) conn.pendingMedia.delete(requestId);
|
|
485
491
|
return;
|
|
486
492
|
}
|
|
487
|
-
// Anything else
|
|
493
|
+
// Anything else is dropped without a reply, as Chromium's HandleV2ProtocolMessage does (it only logs).
|
|
488
494
|
}
|
|
489
495
|
|
|
490
496
|
private async handleLeave(conn: Conn, env: ParsedEnvelope): Promise<void> {
|