2021-12-07 12:14:58 +01:00
|
|
|
// @flow
|
|
|
|
|
2022-06-14 06:34:35 +02:00
|
|
|
let gTitle = '';
|
|
|
|
let gChannelTitle = '';
|
|
|
|
|
2021-12-07 12:14:58 +01:00
|
|
|
/**
|
2022-06-14 06:34:35 +02:00
|
|
|
* Wrapper for @silvermine/videojs-chromecast to consolidate all things related
|
|
|
|
* to chromecast.
|
2021-12-07 12:14:58 +01:00
|
|
|
*/
|
|
|
|
export default class Chromecast {
|
|
|
|
/**
|
|
|
|
* Actions that need to happen after initializing 'videojs'
|
|
|
|
*/
|
|
|
|
static initialize(player: any) {
|
|
|
|
// --- Start plugin ---
|
|
|
|
player.chromecast();
|
2022-06-14 06:34:35 +02:00
|
|
|
|
2021-12-07 12:14:58 +01:00
|
|
|
// --- Init cast framework ---
|
|
|
|
const CHROMECAST_API_SCRIPT_ID = 'chromecastApi';
|
|
|
|
const existingChromecastScript = document.getElementById(CHROMECAST_API_SCRIPT_ID);
|
|
|
|
if (!existingChromecastScript) {
|
|
|
|
const script = document.createElement('script');
|
|
|
|
script.src = 'https://www.gstatic.com/cv/js/sender/v1/cast_sender.js?loadCastFramework=1';
|
|
|
|
script.id = CHROMECAST_API_SCRIPT_ID;
|
|
|
|
// $FlowFixMe
|
|
|
|
document.body.appendChild(script);
|
|
|
|
}
|
|
|
|
}
|
2022-06-14 06:34:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A React-to-vjs interface to pass the new content and channel titles to the
|
|
|
|
* chromecast plugin. Inline functions cannot be used in the `chromecast`
|
|
|
|
* property in `videoJsOptions` due to stale closure, since we no longer
|
|
|
|
* dispose the player when the src changes.
|
|
|
|
*
|
|
|
|
* We need this info from React because are unable to derive them from the
|
|
|
|
* `src` argument of `requestTitleFn | requestSubtitleFn`.
|
|
|
|
*
|
|
|
|
* @param title
|
|
|
|
* @param channelTitle
|
|
|
|
*/
|
|
|
|
static updateTitles(title: ?string, channelTitle: ?string) {
|
|
|
|
gTitle = title;
|
|
|
|
gChannelTitle = channelTitle;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the required 'chromecast' options to be appended to the videojs
|
|
|
|
* options object.
|
|
|
|
*/
|
|
|
|
static getOptions() {
|
|
|
|
return {
|
|
|
|
chromecast: {
|
|
|
|
requestTitleFn: (src: ?string) => gTitle || '',
|
|
|
|
requestSubtitleFn: (src: ?string) => gChannelTitle || '',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2021-12-07 12:14:58 +01:00
|
|
|
}
|