mirror of
https://github.com/hensm/fx_cast.git
synced 2026-06-12 02:29:59 +00:00
Restructure background script (#70)
Splits some background script functionality into separate modules: - Receiver selector handling is moved to ./SelectorManager. - Status bridge handling is moved to ./StatusManager. - Menu creation and updates are handled in ./createMenus. - Shim creation is handled in ./createShim. TypedEventTarget allows EventTarget-derived classes to export typed events. Options type definition is moved to ./lib/options, module assumes more responsibility for update handling and provides a "changed" event. Private cast._requestSession method allows bypassing receiver selector.
This commit is contained in:
@@ -1,17 +1,22 @@
|
||||
"use strict";
|
||||
|
||||
import { Options } from "../defaultOptions";
|
||||
import cast, { init } from "../shim/export";
|
||||
import mediaCasting from "../lib/mediaCasting";
|
||||
import options from "../lib/options";
|
||||
import cast, { ensureInit } from "../shim/export";
|
||||
|
||||
import { Message, Receiver } from "../types";
|
||||
|
||||
|
||||
// Variables passed from background
|
||||
const { srcUrl
|
||||
const { selectedReceiver
|
||||
, srcUrl
|
||||
, targetElementId }
|
||||
: { srcUrl: string
|
||||
: { selectedReceiver: Receiver
|
||||
, srcUrl: string
|
||||
, targetElementId: number } = (window as any);
|
||||
|
||||
|
||||
let options: Options;
|
||||
let backgroundPort: browser.runtime.Port;
|
||||
|
||||
let session: cast.Session;
|
||||
let currentMedia: cast.media.Media;
|
||||
@@ -24,16 +29,13 @@ const isLocalFile = srcUrl.startsWith("file:");
|
||||
const mediaElement = browser.menus.getTargetElement(
|
||||
targetElementId) as HTMLMediaElement;
|
||||
|
||||
window.addEventListener("beforeunload", () => {
|
||||
browser.runtime.sendMessage({
|
||||
window.addEventListener("beforeunload", async () => {
|
||||
backgroundPort.postMessage({
|
||||
subject: "bridge:/mediaServer/stop"
|
||||
});
|
||||
|
||||
if (options.mediaStopOnUnload) {
|
||||
if (await options.get("mediaStopOnUnload")) {
|
||||
session.stop(null, null);
|
||||
/*currentMedia.stop(null
|
||||
, onMediaStopSuccess
|
||||
, onMediaStopError);*/
|
||||
}
|
||||
});
|
||||
|
||||
@@ -55,42 +57,63 @@ function getLocalAddress () {
|
||||
}
|
||||
|
||||
|
||||
async function onRequestSessionSuccess (newSession: cast.Session) {
|
||||
cast.logMessage("onRequestSessionSuccess");
|
||||
|
||||
session = newSession;
|
||||
|
||||
let mediaUrl = new URL(srcUrl);
|
||||
const port = options.localMediaServerPort;
|
||||
|
||||
if (isLocalFile) {
|
||||
await new Promise((resolve, reject) => {
|
||||
browser.runtime.sendMessage({
|
||||
subject: "bridge:/mediaServer/start"
|
||||
, data: {
|
||||
filePath: decodeURI(mediaUrl.pathname)
|
||||
, port
|
||||
}
|
||||
});
|
||||
|
||||
browser.runtime.onMessage.addListener(function onMessage (message) {
|
||||
if (message.subject === "mediaCast:/mediaServer/started") {
|
||||
browser.runtime.onMessage.removeListener(onMessage);
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
function startMediaServer (filePath: string, port: number) {
|
||||
return new Promise((resolve, reject) => {
|
||||
backgroundPort.postMessage({
|
||||
subject: "bridge:/mediaServer/start"
|
||||
, data: {
|
||||
filePath: decodeURI(filePath)
|
||||
, port
|
||||
}
|
||||
});
|
||||
|
||||
// Address of local HTTP server
|
||||
mediaUrl = new URL(`http://${await getLocalAddress()}:${port}/`);
|
||||
backgroundPort.onMessage.addListener(
|
||||
function onMessage (message: Message) {
|
||||
|
||||
switch (message.subject) {
|
||||
case "mediaCast:/mediaServer/started": {
|
||||
backgroundPort.onMessage.removeListener(onMessage);
|
||||
resolve();
|
||||
}
|
||||
case "mediaCast:/mediaServer/error": {
|
||||
backgroundPort.onMessage.removeListener(onMessage);
|
||||
reject();
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function loadMedia () {
|
||||
let mediaUrl = new URL(srcUrl);
|
||||
const mediaTitle = mediaUrl.pathname;
|
||||
|
||||
/**
|
||||
* If the media is a local file, start an HTTP media server
|
||||
* and change the media URL to point to it.
|
||||
*/
|
||||
if (isLocalFile) {
|
||||
const host = await getLocalAddress();
|
||||
const port = await options.get("localMediaServerPort");
|
||||
|
||||
try {
|
||||
// Wait until media server is listening
|
||||
await startMediaServer(mediaUrl.pathname, port);
|
||||
} catch (err) {
|
||||
console.error("Failed to start media server");
|
||||
return;
|
||||
}
|
||||
|
||||
mediaUrl = new URL(`http://${host}:${port}/`);
|
||||
}
|
||||
|
||||
|
||||
const mediaInfo = new cast.media.MediaInfo(mediaUrl.href, null);
|
||||
|
||||
// Media metadata (title/poster)
|
||||
mediaInfo.metadata = new cast.media.GenericMediaMetadata();
|
||||
mediaInfo.metadata.metadataType = cast.media.MetadataType.GENERIC;
|
||||
mediaInfo.metadata.title = mediaUrl.pathname;
|
||||
mediaInfo.metadata.title = mediaTitle;
|
||||
|
||||
if (mediaElement instanceof HTMLVideoElement && mediaElement.poster) {
|
||||
mediaInfo.metadata.images = [
|
||||
@@ -164,41 +187,13 @@ async function onRequestSessionSuccess (newSession: cast.Session) {
|
||||
, onLoadMediaError);
|
||||
}
|
||||
|
||||
function onRequestSessionError () {
|
||||
cast.logMessage("onRequestSessionError");
|
||||
}
|
||||
|
||||
|
||||
function sessionListener (newSession: cast.Session) {
|
||||
cast.logMessage("sessionListener");
|
||||
}
|
||||
|
||||
function receiverListener (availability: string) {
|
||||
cast.logMessage("receiverListener");
|
||||
|
||||
if (availability === cast.ReceiverAvailability.AVAILABLE) {
|
||||
cast.requestSession(
|
||||
onRequestSessionSuccess
|
||||
, onRequestSessionError);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function onInitializeSuccess () {
|
||||
cast.logMessage("onInitializeSuccess");
|
||||
}
|
||||
|
||||
function onInitializeError () {
|
||||
cast.logMessage("onInitializeError");
|
||||
}
|
||||
|
||||
|
||||
function onLoadMediaSuccess (media: cast.media.Media) {
|
||||
async function onLoadMediaSuccess (media: cast.media.Media) {
|
||||
cast.logMessage("onLoadMediaSuccess");
|
||||
|
||||
currentMedia = media;
|
||||
|
||||
if (options.mediaSyncElement) {
|
||||
if (await options.get("mediaSyncElement")) {
|
||||
mediaElement.addEventListener("play", () => {
|
||||
if (ignoreMediaEvents) {
|
||||
ignoreMediaEvents = false;
|
||||
@@ -312,73 +307,57 @@ function onLoadMediaSuccess (media: cast.media.Media) {
|
||||
}
|
||||
}
|
||||
|
||||
function onRequestSessionError () {
|
||||
cast.logMessage("onRequestSessionError");
|
||||
}
|
||||
function sessionListener (newSession: cast.Session) {
|
||||
cast.logMessage("sessionListener");
|
||||
}
|
||||
function onInitializeSuccess () {
|
||||
cast.logMessage("onInitializeSuccess");
|
||||
}
|
||||
function onInitializeError () {
|
||||
cast.logMessage("onInitializeError");
|
||||
}
|
||||
function onLoadMediaError () {
|
||||
cast.logMessage("onLoadMediaError");
|
||||
}
|
||||
|
||||
|
||||
/* play */
|
||||
function onMediaPlaySuccess () {
|
||||
cast.logMessage("onMediaPlaySuccess");
|
||||
}
|
||||
|
||||
function onMediaPlayError (err: cast.Error) {
|
||||
cast.logMessage("onMediaPlayError");
|
||||
}
|
||||
|
||||
|
||||
/* pause */
|
||||
function onMediaPauseSuccess () {
|
||||
cast.logMessage("onMediaPauseSuccess");
|
||||
}
|
||||
|
||||
function onMediaPauseError (err: cast.Error) {
|
||||
cast.logMessage("onMediaPauseError");
|
||||
}
|
||||
|
||||
|
||||
/* stop */
|
||||
function onMediaStopSuccess () {
|
||||
cast.logMessage("onMediaStopSuccess");
|
||||
}
|
||||
|
||||
function onMediaStopError (err: cast.Error) {
|
||||
cast.logMessage("onMediaStopError");
|
||||
}
|
||||
|
||||
|
||||
/* seek */
|
||||
function onMediaSeekSuccess () {
|
||||
cast.logMessage("onMediaSeekSuccess");
|
||||
}
|
||||
|
||||
function onMediaSeekError (err: cast.Error) {
|
||||
cast.logMessage("onMediaSeekError");
|
||||
}
|
||||
|
||||
|
||||
init().then(async bridgeInfo => {
|
||||
if (!bridgeInfo.isVersionCompatible) {
|
||||
console.error("__onGCastApiAvailable error");
|
||||
return;
|
||||
}
|
||||
ensureInit().then(async (port) => {
|
||||
backgroundPort = port;
|
||||
|
||||
options = (await browser.storage.sync.get("options")).options;
|
||||
|
||||
if (isLocalFile && !options.localMediaEnabled) {
|
||||
const isLocalMediaEnabled = await options.get("localMediaEnabled");
|
||||
if (isLocalFile && !isLocalMediaEnabled) {
|
||||
cast.logMessage("Local media casting not enabled");
|
||||
return;
|
||||
}
|
||||
|
||||
session = await mediaCasting.getMediaSession(selectedReceiver);
|
||||
|
||||
const sessionRequest = new cast.SessionRequest(
|
||||
cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID);
|
||||
|
||||
const apiConfig = new cast.ApiConfig(sessionRequest
|
||||
, sessionListener
|
||||
, receiverListener);
|
||||
|
||||
cast.initialize(apiConfig
|
||||
, onInitializeSuccess
|
||||
, onInitializeError);
|
||||
loadMedia();
|
||||
});
|
||||
|
||||
@@ -1,15 +1,19 @@
|
||||
"use strict";
|
||||
|
||||
import options from "../lib/options";
|
||||
import cast, { init } from "../shim/export";
|
||||
import cast, { ensureInit } from "../shim/export";
|
||||
|
||||
import { ReceiverSelectorMediaType }
|
||||
from "../receiver_selectors/ReceiverSelector";
|
||||
|
||||
import { Receiver } from "../types";
|
||||
|
||||
|
||||
// Variables passed from background
|
||||
const { selectedMedia }
|
||||
: { selectedMedia: ReceiverSelectorMediaType } = (window as any);
|
||||
const { selectedMedia
|
||||
, selectedReceiver }
|
||||
: { selectedMedia: ReceiverSelectorMediaType
|
||||
, selectedReceiver: Receiver } = (window as any);
|
||||
|
||||
|
||||
const FX_CAST_RECEIVER_APP_NAMESPACE = "urn:x-cast:fx_cast";
|
||||
@@ -42,6 +46,10 @@ if (typeof navigator.mediaDevices.getDisplayMedia === "undefined") {
|
||||
* receiver device.
|
||||
*/
|
||||
function sendAppMessage (subject: string, data: any) {
|
||||
if (!session) {
|
||||
return;
|
||||
}
|
||||
|
||||
session.sendMessage(FX_CAST_RECEIVER_APP_NAMESPACE, {
|
||||
subject
|
||||
, data
|
||||
@@ -54,9 +62,7 @@ window.addEventListener("beforeunload", () => {
|
||||
});
|
||||
|
||||
|
||||
async function onRequestSessionSuccess (
|
||||
newSession: cast.Session
|
||||
, newSelectedMedia: ReceiverSelectorMediaType) {
|
||||
async function onRequestSessionSuccess (newSession: cast.Session) {
|
||||
|
||||
cast.logMessage("onRequestSessionSuccess");
|
||||
|
||||
@@ -83,7 +89,7 @@ async function onRequestSessionSuccess (
|
||||
sendAppMessage("iceCandidate", ev.candidate);
|
||||
});
|
||||
|
||||
switch (newSelectedMedia) {
|
||||
switch (selectedMedia) {
|
||||
case ReceiverSelectorMediaType.Tab: {
|
||||
const canvas = document.createElement("canvas");
|
||||
const ctx = canvas.getContext("2d");
|
||||
@@ -161,8 +167,9 @@ function receiverListener (availability: string) {
|
||||
|
||||
if (availability === cast.ReceiverAvailability.AVAILABLE) {
|
||||
wasSessionRequested = true;
|
||||
cast.requestSession(
|
||||
onRequestSessionSuccess
|
||||
cast._requestSession(
|
||||
selectedReceiver
|
||||
, onRequestSessionSuccess
|
||||
, onRequestSessionError);
|
||||
}
|
||||
}
|
||||
@@ -182,13 +189,7 @@ function onInitializeError () {
|
||||
}
|
||||
|
||||
|
||||
init().then(async bridgeInfo => {
|
||||
if (!bridgeInfo.isVersionCompatible) {
|
||||
console.error("__onGCastApiAvailable error");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
ensureInit().then(async () => {
|
||||
const mirroringAppId = await options.get("mirroringAppId");
|
||||
const sessionRequest = new cast.SessionRequest(mirroringAppId);
|
||||
|
||||
@@ -196,9 +197,7 @@ init().then(async bridgeInfo => {
|
||||
sessionRequest
|
||||
, sessionListener
|
||||
, receiverListener
|
||||
, undefined, undefined
|
||||
, selectedMedia
|
||||
, availableMediaTypes);
|
||||
, undefined, undefined);
|
||||
|
||||
cast.initialize(apiConfig
|
||||
, onInitializeSuccess
|
||||
|
||||
Reference in New Issue
Block a user