mirror of
https://github.com/hensm/fx_cast.git
synced 2026-06-10 17:49:58 +00:00
Pass Receiver objects instead of ReceiverDevice objects to cast API
This commit is contained in:
@@ -16,8 +16,10 @@ import deviceManager from "./deviceManager";
|
||||
import castManager from "./castManager";
|
||||
|
||||
import { BaseConfig, baseConfigStorage, getAppTag } from "../cast/googleApi";
|
||||
import type { SessionRequest } from "../cast/sdk/classes";
|
||||
import type { SenderMediaMessage, SenderMessage } from "../cast/sdk/types";
|
||||
import type { SessionRequest } from "../cast/sdk/classes";
|
||||
import { ReceiverAction } from "../cast/sdk/enums";
|
||||
import { createReceiver } from "../cast/utils";
|
||||
|
||||
const POPUP_URL = browser.runtime.getURL("ui/popup/index.html");
|
||||
|
||||
@@ -330,7 +332,10 @@ export default class ReceiverSelector extends TypedEventTarget<ReceiverSelectorE
|
||||
* If the current context is running the mirroring app, pretend
|
||||
* it doesn't exist because it shouldn't be launched like this.
|
||||
*/
|
||||
if (castInstance?.appId === (await options.get("mirroringAppId"))) {
|
||||
if (
|
||||
castInstance?.apiConfig?.sessionRequest.appId ===
|
||||
(await options.get("mirroringAppId"))
|
||||
) {
|
||||
castInstance = undefined;
|
||||
}
|
||||
|
||||
@@ -377,7 +382,7 @@ export default class ReceiverSelector extends TypedEventTarget<ReceiverSelectorE
|
||||
await deviceManager.init();
|
||||
|
||||
let isRequestAppAudioCompatible: Optional<boolean>;
|
||||
if (castInstance?.appId) {
|
||||
if (castInstance?.apiConfig?.sessionRequest.appId) {
|
||||
if (!baseConfig) {
|
||||
try {
|
||||
baseConfig = (await baseConfigStorage.get("baseConfig"))
|
||||
@@ -389,7 +394,7 @@ export default class ReceiverSelector extends TypedEventTarget<ReceiverSelectorE
|
||||
|
||||
isRequestAppAudioCompatible = getAppTag(
|
||||
baseConfig,
|
||||
castInstance.appId
|
||||
castInstance.apiConfig?.sessionRequest.appId
|
||||
)?.supports_audio_only;
|
||||
}
|
||||
|
||||
@@ -429,7 +434,7 @@ export default class ReceiverSelector extends TypedEventTarget<ReceiverSelectorE
|
||||
receiverDevices: deviceManager.getDevices(),
|
||||
defaultMediaType,
|
||||
availableMediaTypes,
|
||||
appId: castInstance?.appId,
|
||||
appId: castInstance?.apiConfig?.sessionRequest.appId,
|
||||
// Create page info
|
||||
pageInfo: pageUrl
|
||||
? {
|
||||
@@ -464,9 +469,15 @@ function createSelector() {
|
||||
);
|
||||
if (!castInstance) return;
|
||||
|
||||
const device = deviceManager.getDeviceById(ev.detail.deviceId);
|
||||
if (!device) return;
|
||||
|
||||
castInstance.contentPort.postMessage({
|
||||
subject: "cast:receiverStoppedAction",
|
||||
data: { deviceId: ev.detail.deviceId }
|
||||
subject: "cast:sendReceiverAction",
|
||||
data: {
|
||||
receiver: createReceiver(device),
|
||||
action: ReceiverAction.STOP
|
||||
}
|
||||
});
|
||||
};
|
||||
selector.addEventListener("stop", onStop);
|
||||
|
||||
@@ -8,6 +8,10 @@ import { stringify } from "../lib/utils";
|
||||
|
||||
import { ReceiverSelectorMediaType } from "../types";
|
||||
|
||||
import type { ApiConfig } from "../cast/sdk/classes";
|
||||
import { ReceiverAction } from "../cast/sdk/enums";
|
||||
import { createReceiver } from "../cast/utils";
|
||||
|
||||
import deviceManager from "./deviceManager";
|
||||
import ReceiverSelector, { ReceiverSelection } from "./ReceiverSelector";
|
||||
|
||||
@@ -18,7 +22,10 @@ export interface CastInstance {
|
||||
contentPort: AnyPort;
|
||||
contentTabId?: number;
|
||||
contentFrameId?: number;
|
||||
appId?: string;
|
||||
|
||||
/** ApiConfig provided on initialization. */
|
||||
apiConfig?: ApiConfig;
|
||||
/** Established session details. */
|
||||
session?: CastSession;
|
||||
}
|
||||
|
||||
@@ -39,23 +46,23 @@ export default new (class {
|
||||
}
|
||||
});
|
||||
|
||||
// Forward receiver eventes to cast instances
|
||||
deviceManager.addEventListener("deviceUp", ev => {
|
||||
// Pass receiver availability updates to cast API.
|
||||
const updateReceiverAvailability = () => {
|
||||
const isAvailable = deviceManager.getDevices().length > 0;
|
||||
|
||||
for (const instance of this.activeInstances) {
|
||||
instance.contentPort.postMessage({
|
||||
subject: "cast:receiverDeviceUp",
|
||||
data: { receiverDevice: ev.detail.deviceInfo }
|
||||
subject: "cast:updateReceiverAvailability",
|
||||
data: { isAvailable }
|
||||
});
|
||||
}
|
||||
});
|
||||
deviceManager.addEventListener("deviceDown", ev => {
|
||||
for (const instance of this.activeInstances) {
|
||||
instance.contentPort.postMessage({
|
||||
subject: "cast:receiverDeviceDown",
|
||||
data: { receiverDeviceId: ev.detail.deviceId }
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
deviceManager.addEventListener("deviceUp", updateReceiverAvailability);
|
||||
deviceManager.addEventListener(
|
||||
"deviceDown",
|
||||
updateReceiverAvailability
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -197,7 +204,7 @@ export default new (class {
|
||||
) {
|
||||
// Intercept messages to store relevant info
|
||||
switch (message.subject) {
|
||||
case "cast:sessionCreated": {
|
||||
case "main:castSessionCreated": {
|
||||
// Close after session is created
|
||||
const selector = ReceiverSelector.sharedInstance;
|
||||
if (
|
||||
@@ -211,12 +218,38 @@ export default new (class {
|
||||
selector.close();
|
||||
}
|
||||
|
||||
const { receiverId: deviceId } = message.data;
|
||||
|
||||
instance.session = {
|
||||
deviceId: message.data.receiverId,
|
||||
deviceId,
|
||||
sessionId: message.data.sessionId
|
||||
};
|
||||
|
||||
const device = deviceManager.getDeviceById(deviceId);
|
||||
if (!device) {
|
||||
logger.error(
|
||||
"[on main:castSessionCreated]: Could not find device with ID:",
|
||||
deviceId
|
||||
);
|
||||
break;
|
||||
}
|
||||
|
||||
instance.contentPort.postMessage({
|
||||
subject: "cast:sessionCreated",
|
||||
data: {
|
||||
...message.data,
|
||||
receiver: createReceiver(device)
|
||||
}
|
||||
});
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case "main:castSessionUpdated":
|
||||
instance.contentPort.postMessage({
|
||||
subject: "cast:sessionUpdated",
|
||||
data: message.data
|
||||
});
|
||||
}
|
||||
|
||||
instance.contentPort.postMessage(message);
|
||||
@@ -239,14 +272,14 @@ export default new (class {
|
||||
switch (message.subject) {
|
||||
// Cast API has been initialized
|
||||
case "main:initializeCast": {
|
||||
instance.appId = message.data.appId;
|
||||
instance.apiConfig = message.data.apiConfig;
|
||||
|
||||
for (const receiverDevice of deviceManager.getDevices()) {
|
||||
instance.contentPort.postMessage({
|
||||
subject: "cast:receiverDeviceUp",
|
||||
data: { receiverDevice }
|
||||
});
|
||||
}
|
||||
instance.contentPort.postMessage({
|
||||
subject: "cast:updateReceiverAvailability",
|
||||
data: {
|
||||
isAvailable: deviceManager.getDevices().length > 0
|
||||
}
|
||||
});
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -262,11 +295,13 @@ export default new (class {
|
||||
);
|
||||
}
|
||||
|
||||
const { sessionRequest } = message.data;
|
||||
|
||||
try {
|
||||
const selection = await ReceiverSelector.getSelection(
|
||||
instance.contentTabId,
|
||||
instance.contentFrameId,
|
||||
{ sessionRequest: message.data.sessionRequest }
|
||||
{ sessionRequest }
|
||||
);
|
||||
|
||||
// Handle cancellation
|
||||
@@ -297,9 +332,12 @@ export default new (class {
|
||||
break;
|
||||
}
|
||||
|
||||
instance.contentPort.postMessage({
|
||||
subject: "cast:selectReceiver/selected",
|
||||
data: selection
|
||||
instance.bridgePort.postMessage({
|
||||
subject: "bridge:createCastSession",
|
||||
data: {
|
||||
appId: sessionRequest.appId,
|
||||
receiverDevice: selection.receiverDevice
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
// TODO: Report errors properly
|
||||
@@ -336,9 +374,24 @@ export default new (class {
|
||||
);
|
||||
}
|
||||
|
||||
if (!instance.apiConfig?.sessionRequest.appId) {
|
||||
throw logger.error("Invalid session request");
|
||||
}
|
||||
|
||||
instance.contentPort.postMessage({
|
||||
subject: "cast:launchApp",
|
||||
data: { receiverDevice: opts.selection.receiverDevice }
|
||||
subject: "cast:sendReceiverAction",
|
||||
data: {
|
||||
receiver: createReceiver(opts.selection.receiverDevice),
|
||||
action: ReceiverAction.CAST
|
||||
}
|
||||
});
|
||||
|
||||
instance.bridgePort.postMessage({
|
||||
subject: "bridge:createCastSession",
|
||||
data: {
|
||||
appId: instance.apiConfig?.sessionRequest.appId,
|
||||
receiverDevice: opts.selection.receiverDevice
|
||||
}
|
||||
});
|
||||
|
||||
break;
|
||||
|
||||
@@ -30,8 +30,8 @@ interface EventMap {
|
||||
|
||||
export default new (class extends TypedEventTarget<EventMap> {
|
||||
/**
|
||||
* Map of receiver device IDs to devices. Updated as
|
||||
* receiverDevice messages are received from the bridge.
|
||||
* Map of receiver device IDs to devices. Updated as receiverDevice
|
||||
* messages are received from the bridge.
|
||||
*/
|
||||
private receiverDevices = new Map<string, ReceiverDevice>();
|
||||
|
||||
@@ -43,8 +43,8 @@ export default new (class extends TypedEventTarget<EventMap> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize (or re-initialize) a bridge connection to
|
||||
* start dispatching events.
|
||||
* Initializes (or re-initializes) a bridge connection to start
|
||||
* dispatching events.
|
||||
*/
|
||||
async refresh() {
|
||||
this.bridgePort?.disconnect();
|
||||
@@ -63,33 +63,16 @@ export default new (class extends TypedEventTarget<EventMap> {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of receiver devices
|
||||
*/
|
||||
/** Gets a list of receiver devices. */
|
||||
getDevices() {
|
||||
return Array.from(this.receiverDevices.values());
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops a receiver app running on a given device.
|
||||
*/
|
||||
stopReceiverApp(receiverDeviceId: string) {
|
||||
if (!this.bridgePort) {
|
||||
logger.error(
|
||||
"Failed to stop receiver device, no bridge connection"
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const receiverDevice = this.receiverDevices.get(receiverDeviceId);
|
||||
if (receiverDevice) {
|
||||
this.bridgePort.postMessage({
|
||||
subject: "bridge:stopCastSession",
|
||||
data: { receiverDevice }
|
||||
});
|
||||
}
|
||||
/** Gets a device by ID. */
|
||||
getDeviceById(deviceId: string) {
|
||||
return this.receiverDevices.get(deviceId);
|
||||
}
|
||||
|
||||
/** Sends an NS_RECEIVER message to a given device. */
|
||||
sendReceiverMessage(deviceId: string, message: SenderMessage) {
|
||||
if (!this.bridgePort) {
|
||||
logger.error(
|
||||
@@ -112,6 +95,7 @@ export default new (class extends TypedEventTarget<EventMap> {
|
||||
});
|
||||
}
|
||||
|
||||
/** Sends an NS_MEDIA message to a given device. */
|
||||
sendMediaMessage(deviceId: string, message: SenderMediaMessage) {
|
||||
if (!this.bridgePort) {
|
||||
logger.error("Failed to send media message (no bridge connection)");
|
||||
|
||||
Reference in New Issue
Block a user