lbry-desktop/ui/component/viewers/videoViewer/internal/chromecast.js
infinite-persistence 363fe82050
Fix chromecast title handling (#1685)
Fixed the title that did not update from stale closure because we no longer re-initialize the plugin.

We still continue to sever the connection when switching sources for now (although videojs is now single-instance) due to a problem that stops the next remote playback after 5-10 seconds. Unclear whether it is the plugin problem or due to our changes (although I don't see this issue in their repo).
2022-06-14 00:34:35 -04:00

59 lines
1.7 KiB
JavaScript

// @flow
let gTitle = '';
let gChannelTitle = '';
/**
* Wrapper for @silvermine/videojs-chromecast to consolidate all things related
* to chromecast.
*/
export default class Chromecast {
/**
* Actions that need to happen after initializing 'videojs'
*/
static initialize(player: any) {
// --- Start plugin ---
player.chromecast();
// --- 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);
}
}
/**
* 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 || '',
},
};
}
}