2020-08-16 20:24:45 +02:00
|
|
|
import appSettings from '../../scripts/settings/appSettings';
|
2020-08-14 08:46:34 +02:00
|
|
|
import * as userSettings from '../../scripts/settings/userSettings';
|
2020-08-16 20:24:45 +02:00
|
|
|
import { playbackManager } from '../../components/playback/playbackmanager';
|
|
|
|
import globalize from '../../scripts/globalize';
|
2020-09-08 02:05:02 -04:00
|
|
|
import { ConnectionManager, Events } from 'jellyfin-apiclient';
|
2020-08-16 20:24:45 +02:00
|
|
|
import castSenderApiLoader from '../../components/castSenderApi';
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
// Based on https://github.com/googlecast/CastVideos-chrome/blob/master/CastVideos.js
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
let currentResolve;
|
|
|
|
let currentReject;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
const PlayerName = 'Google Cast';
|
2018-10-23 01:05:09 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
function sendConnectionResult(isOk) {
|
|
|
|
const resolve = currentResolve;
|
|
|
|
const reject = currentReject;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
currentResolve = null;
|
|
|
|
currentReject = null;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
if (isOk) {
|
|
|
|
if (resolve) {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (reject) {
|
|
|
|
reject();
|
2019-01-10 15:39:37 +03:00
|
|
|
} else {
|
2020-08-07 08:29:07 +01:00
|
|
|
playbackManager.removeActivePlayer(PlayerName);
|
2019-01-10 15:39:37 +03:00
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constants of states for Chromecast device
|
|
|
|
**/
|
|
|
|
const DEVICE_STATE = {
|
|
|
|
'IDLE': 0,
|
|
|
|
'ACTIVE': 1,
|
|
|
|
'WARNING': 2,
|
|
|
|
'ERROR': 3
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constants of states for CastPlayer
|
|
|
|
**/
|
|
|
|
const PLAYER_STATE = {
|
|
|
|
'IDLE': 'IDLE',
|
|
|
|
'LOADING': 'LOADING',
|
|
|
|
'LOADED': 'LOADED',
|
|
|
|
'PLAYING': 'PLAYING',
|
|
|
|
'PAUSED': 'PAUSED',
|
|
|
|
'STOPPED': 'STOPPED',
|
|
|
|
'SEEKING': 'SEEKING',
|
|
|
|
'ERROR': 'ERROR'
|
|
|
|
};
|
|
|
|
|
|
|
|
// production version registered with google
|
|
|
|
// replace this value if you want to test changes on another instance
|
|
|
|
const applicationStable = 'F007D354';
|
|
|
|
const applicationUnstable = '6F511C87';
|
|
|
|
|
|
|
|
const messageNamespace = 'urn:x-cast:com.connectsdk';
|
|
|
|
|
|
|
|
class CastPlayer {
|
|
|
|
constructor() {
|
2019-01-10 15:39:37 +03:00
|
|
|
/* device variables */
|
|
|
|
// @type {DEVICE_STATE} A state for device
|
|
|
|
this.deviceState = DEVICE_STATE.IDLE;
|
|
|
|
|
|
|
|
/* Cast player variables */
|
|
|
|
// @type {Object} a chrome.cast.media.Media object
|
|
|
|
this.currentMediaSession = null;
|
|
|
|
|
|
|
|
// @type {string} a chrome.cast.Session object
|
|
|
|
this.session = null;
|
|
|
|
// @type {PLAYER_STATE} A state for Cast media player
|
|
|
|
this.castPlayerState = PLAYER_STATE.IDLE;
|
|
|
|
|
|
|
|
this.hasReceivers = false;
|
|
|
|
|
|
|
|
// bind once - commit 2ebffc2271da0bc5e8b13821586aee2a2e3c7753
|
|
|
|
this.errorHandler = this.onError.bind(this);
|
|
|
|
this.mediaStatusUpdateHandler = this.onMediaStatusUpdate.bind(this);
|
|
|
|
|
|
|
|
this.initializeCastPlayer();
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
/**
|
2019-06-19 05:02:21 -07:00
|
|
|
* Initialize Cast media player
|
2019-01-10 15:39:37 +03:00
|
|
|
* Initializes the API. Note that either successCallback and errorCallback will be
|
2019-06-19 05:02:21 -07:00
|
|
|
* invoked once the API has finished initialization. The sessionListener and
|
|
|
|
* receiverListener may be invoked at any time afterwards, and possibly more than once.
|
2019-01-10 15:39:37 +03:00
|
|
|
*/
|
2020-08-07 08:29:07 +01:00
|
|
|
initializeCastPlayer() {
|
|
|
|
const chrome = window.chrome;
|
2019-01-10 15:39:37 +03:00
|
|
|
if (!chrome) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!chrome.cast || !chrome.cast.isAvailable) {
|
|
|
|
setTimeout(this.initializeCastPlayer.bind(this), 1000);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
let applicationID = applicationStable;
|
2020-07-31 22:16:20 +09:00
|
|
|
if (userSettings.chromecastVersion() === 'unstable') {
|
|
|
|
applicationID = applicationUnstable;
|
2020-06-01 19:38:59 +03:00
|
|
|
}
|
|
|
|
|
2019-01-10 15:39:37 +03:00
|
|
|
// request session
|
2020-08-07 08:29:07 +01:00
|
|
|
const sessionRequest = new chrome.cast.SessionRequest(applicationID);
|
|
|
|
const apiConfig = new chrome.cast.ApiConfig(sessionRequest,
|
2019-01-10 15:39:37 +03:00
|
|
|
this.sessionListener.bind(this),
|
2019-06-19 05:02:21 -07:00
|
|
|
this.receiverListener.bind(this));
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-02-16 03:44:43 +01:00
|
|
|
console.debug('chromecast.initialize');
|
2019-01-10 15:39:37 +03:00
|
|
|
chrome.cast.initialize(apiConfig, this.onInitSuccess.bind(this), this.errorHandler);
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
/**
|
2019-06-19 05:02:21 -07:00
|
|
|
* Callback function for init success
|
2019-01-10 15:39:37 +03:00
|
|
|
*/
|
2020-08-07 08:29:07 +01:00
|
|
|
onInitSuccess() {
|
2019-01-10 15:39:37 +03:00
|
|
|
this.isInitialized = true;
|
2020-05-04 12:44:12 +02:00
|
|
|
console.debug('chromecast init success');
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
/**
|
2019-06-19 05:02:21 -07:00
|
|
|
* Generic error callback function
|
2019-01-10 15:39:37 +03:00
|
|
|
*/
|
2020-08-07 08:29:07 +01:00
|
|
|
onError() {
|
2020-05-04 12:44:12 +02:00
|
|
|
console.debug('chromecast error');
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Object} e A new session
|
|
|
|
* This handles auto-join when a page is reloaded
|
|
|
|
* When active session is detected, playback will automatically
|
|
|
|
* join existing session and occur in Cast mode and media
|
2019-06-19 05:02:21 -07:00
|
|
|
* status gets synced up with current media of the session
|
2019-01-10 15:39:37 +03:00
|
|
|
*/
|
2020-08-07 08:29:07 +01:00
|
|
|
sessionListener(e) {
|
2019-01-10 15:39:37 +03:00
|
|
|
this.session = e;
|
|
|
|
if (this.session) {
|
|
|
|
if (this.session.media[0]) {
|
|
|
|
this.onMediaDiscovered('activeSession', this.session.media[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.onSessionConnected(e);
|
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
messageListener(namespace, message) {
|
2019-01-10 15:39:37 +03:00
|
|
|
if (typeof (message) === 'string') {
|
|
|
|
message = JSON.parse(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message.type === 'playbackerror') {
|
2020-08-07 08:29:07 +01:00
|
|
|
const errorCode = message.data;
|
2019-01-10 15:39:37 +03:00
|
|
|
setTimeout(function () {
|
|
|
|
alertText(globalize.translate('MessagePlaybackError' + errorCode), globalize.translate('HeaderPlaybackError'));
|
|
|
|
}, 300);
|
2019-06-19 05:02:21 -07:00
|
|
|
} else if (message.type === 'connectionerror') {
|
2019-01-10 15:39:37 +03:00
|
|
|
setTimeout(function () {
|
|
|
|
alertText(globalize.translate('MessageChromecastConnectionError'), globalize.translate('HeaderError'));
|
|
|
|
}, 300);
|
2019-06-19 05:02:21 -07:00
|
|
|
} else if (message.type) {
|
2020-09-08 02:05:02 -04:00
|
|
|
Events.trigger(this, message.type, [message.data]);
|
2019-01-10 15:39:37 +03:00
|
|
|
}
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} e Receiver availability
|
|
|
|
* This indicates availability of receivers but
|
|
|
|
* does not provide a list of device IDs
|
|
|
|
*/
|
2020-08-07 08:29:07 +01:00
|
|
|
receiverListener(e) {
|
2019-01-10 15:39:37 +03:00
|
|
|
if (e === 'available') {
|
2020-05-04 12:44:12 +02:00
|
|
|
console.debug('chromecast receiver found');
|
2019-01-10 15:39:37 +03:00
|
|
|
this.hasReceivers = true;
|
2019-06-19 05:02:21 -07:00
|
|
|
} else {
|
2020-05-04 12:44:12 +02:00
|
|
|
console.debug('chromecast receiver list empty');
|
2019-01-10 15:39:37 +03:00
|
|
|
this.hasReceivers = false;
|
|
|
|
}
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* session update listener
|
|
|
|
*/
|
2020-08-07 08:29:07 +01:00
|
|
|
sessionUpdateListener(isAlive) {
|
2019-01-10 15:39:37 +03:00
|
|
|
if (isAlive) {
|
2020-02-16 03:44:43 +01:00
|
|
|
console.debug('sessionUpdateListener: already alive');
|
2019-06-19 05:02:21 -07:00
|
|
|
} else {
|
2019-01-10 15:39:37 +03:00
|
|
|
this.session = null;
|
|
|
|
this.deviceState = DEVICE_STATE.IDLE;
|
|
|
|
this.castPlayerState = PLAYER_STATE.IDLE;
|
2020-05-04 12:44:12 +02:00
|
|
|
document.removeEventListener('volumeupbutton', onVolumeUpKeyDown, false);
|
|
|
|
document.removeEventListener('volumedownbutton', onVolumeDownKeyDown, false);
|
2019-06-19 05:02:21 -07:00
|
|
|
|
2020-02-16 03:44:43 +01:00
|
|
|
console.debug('sessionUpdateListener: setting currentMediaSession to null');
|
2019-01-10 15:39:37 +03:00
|
|
|
this.currentMediaSession = null;
|
|
|
|
|
|
|
|
sendConnectionResult(false);
|
|
|
|
}
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Requests that a receiver application session be created or joined. By default, the SessionRequest
|
|
|
|
* passed to the API at initialization time is used; this may be overridden by passing a different
|
2019-06-19 05:02:21 -07:00
|
|
|
* session request in opt_sessionRequest.
|
2019-01-10 15:39:37 +03:00
|
|
|
*/
|
2020-08-07 08:29:07 +01:00
|
|
|
launchApp() {
|
2020-05-04 12:44:12 +02:00
|
|
|
console.debug('chromecast launching app...');
|
2019-01-10 15:39:37 +03:00
|
|
|
chrome.cast.requestSession(this.onRequestSessionSuccess.bind(this), this.onLaunchError.bind(this));
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
/**
|
2019-06-19 05:02:21 -07:00
|
|
|
* Callback function for request session success
|
2019-01-10 15:39:37 +03:00
|
|
|
* @param {Object} e A chrome.cast.Session object
|
|
|
|
*/
|
2020-08-07 08:29:07 +01:00
|
|
|
onRequestSessionSuccess(e) {
|
2020-05-04 12:44:12 +02:00
|
|
|
console.debug('chromecast session success: ' + e.sessionId);
|
2019-01-10 15:39:37 +03:00
|
|
|
this.onSessionConnected(e);
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
onSessionConnected(session) {
|
2019-01-10 15:39:37 +03:00
|
|
|
this.session = session;
|
|
|
|
this.deviceState = DEVICE_STATE.ACTIVE;
|
|
|
|
|
|
|
|
this.session.addMessageListener(messageNamespace, this.messageListener.bind(this));
|
|
|
|
this.session.addMediaListener(this.sessionMediaListener.bind(this));
|
|
|
|
this.session.addUpdateListener(this.sessionUpdateListener.bind(this));
|
|
|
|
|
2020-05-04 12:44:12 +02:00
|
|
|
document.addEventListener('volumeupbutton', onVolumeUpKeyDown, false);
|
|
|
|
document.addEventListener('volumedownbutton', onVolumeDownKeyDown, false);
|
2019-04-02 09:43:49 +02:00
|
|
|
|
2020-09-08 02:05:02 -04:00
|
|
|
Events.trigger(this, 'connect');
|
2019-01-10 15:39:37 +03:00
|
|
|
this.sendMessage({
|
|
|
|
options: {},
|
|
|
|
command: 'Identify'
|
|
|
|
});
|
2019-04-02 09:43:49 +02:00
|
|
|
}
|
|
|
|
|
2019-01-10 15:39:37 +03:00
|
|
|
/**
|
|
|
|
* session update listener
|
|
|
|
*/
|
2020-08-07 08:29:07 +01:00
|
|
|
sessionMediaListener(e) {
|
2019-01-10 15:39:37 +03:00
|
|
|
this.currentMediaSession = e;
|
|
|
|
this.currentMediaSession.addUpdateListener(this.mediaStatusUpdateHandler);
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback function for launch error
|
|
|
|
*/
|
2020-08-07 08:29:07 +01:00
|
|
|
onLaunchError() {
|
2020-05-04 12:44:12 +02:00
|
|
|
console.debug('chromecast launch error');
|
2019-01-10 15:39:37 +03:00
|
|
|
this.deviceState = DEVICE_STATE.ERROR;
|
|
|
|
sendConnectionResult(false);
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops the running receiver application associated with the session.
|
|
|
|
*/
|
2020-08-07 08:29:07 +01:00
|
|
|
stopApp() {
|
2019-01-10 15:39:37 +03:00
|
|
|
if (this.session) {
|
2019-06-19 05:02:21 -07:00
|
|
|
this.session.stop(this.onStopAppSuccess.bind(this, 'Session stopped'), this.errorHandler);
|
2019-01-10 15:39:37 +03:00
|
|
|
}
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
/**
|
2019-06-19 05:02:21 -07:00
|
|
|
* Callback function for stop app success
|
2019-01-10 15:39:37 +03:00
|
|
|
*/
|
2020-08-07 08:29:07 +01:00
|
|
|
onStopAppSuccess(message) {
|
2020-02-16 03:44:43 +01:00
|
|
|
console.debug(message);
|
2019-06-19 05:02:21 -07:00
|
|
|
|
2019-01-10 15:39:37 +03:00
|
|
|
this.deviceState = DEVICE_STATE.IDLE;
|
|
|
|
this.castPlayerState = PLAYER_STATE.IDLE;
|
2020-05-04 12:44:12 +02:00
|
|
|
document.removeEventListener('volumeupbutton', onVolumeUpKeyDown, false);
|
|
|
|
document.removeEventListener('volumedownbutton', onVolumeDownKeyDown, false);
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
this.currentMediaSession = null;
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads media into a running receiver application
|
|
|
|
* @param {Number} mediaIndex An index number to indicate current media content
|
|
|
|
*/
|
2020-08-07 08:29:07 +01:00
|
|
|
loadMedia(options, command) {
|
2019-01-10 15:39:37 +03:00
|
|
|
if (!this.session) {
|
2020-05-04 12:44:12 +02:00
|
|
|
console.debug('no session');
|
2019-01-10 15:39:37 +03:00
|
|
|
return Promise.reject();
|
|
|
|
}
|
|
|
|
|
2019-06-19 05:02:21 -07:00
|
|
|
// convert items to smaller stubs to send minimal amount of information
|
2019-01-10 15:39:37 +03:00
|
|
|
options.items = options.items.map(function (i) {
|
|
|
|
return {
|
|
|
|
Id: i.Id,
|
|
|
|
ServerId: i.ServerId,
|
|
|
|
Name: i.Name,
|
|
|
|
Type: i.Type,
|
|
|
|
MediaType: i.MediaType,
|
|
|
|
IsFolder: i.IsFolder
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
return this.sendMessage({
|
|
|
|
options: options,
|
|
|
|
command: command
|
|
|
|
});
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
sendMessage(message) {
|
|
|
|
const player = this;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
let receiverName = null;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
const session = player.session;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
if (session && session.receiver && session.receiver.friendlyName) {
|
|
|
|
receiverName = session.receiver.friendlyName;
|
|
|
|
}
|
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
let apiClient;
|
2019-01-10 15:39:37 +03:00
|
|
|
if (message.options && message.options.ServerId) {
|
2020-08-16 20:24:45 +02:00
|
|
|
apiClient = ConnectionManager.getApiClient(message.options.ServerId);
|
2019-01-10 15:39:37 +03:00
|
|
|
} else if (message.options && message.options.items && message.options.items.length) {
|
2020-08-16 20:24:45 +02:00
|
|
|
apiClient = ConnectionManager.getApiClient(message.options.items[0].ServerId);
|
2019-01-10 15:39:37 +03:00
|
|
|
} else {
|
2020-09-08 13:35:17 -04:00
|
|
|
apiClient = window.ConnectionManager.currentApiClient();
|
2019-01-10 15:39:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
message = Object.assign(message, {
|
|
|
|
userId: apiClient.getCurrentUserId(),
|
|
|
|
deviceId: apiClient.deviceId(),
|
|
|
|
accessToken: apiClient.accessToken(),
|
|
|
|
serverAddress: apiClient.serverAddress(),
|
|
|
|
serverId: apiClient.serverId(),
|
|
|
|
serverVersion: apiClient.serverVersion(),
|
|
|
|
receiverName: receiverName
|
|
|
|
});
|
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
const bitrateSetting = appSettings.maxChromecastBitrate();
|
2019-01-10 15:39:37 +03:00
|
|
|
if (bitrateSetting) {
|
|
|
|
message.maxBitrate = bitrateSetting;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message.options && message.options.items) {
|
|
|
|
message.subtitleAppearance = userSettings.getSubtitleAppearanceSettings();
|
|
|
|
message.subtitleBurnIn = appSettings.get('subtitleburnin') || '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise(function (resolve, reject) {
|
2020-08-07 08:29:07 +01:00
|
|
|
import('./chromecastHelper').then(({ default: chromecastHelper }) => {
|
2019-01-10 15:39:37 +03:00
|
|
|
chromecastHelper.getServerAddress(apiClient).then(function (serverAddress) {
|
|
|
|
message.serverAddress = serverAddress;
|
|
|
|
player.sendMessageInternal(message).then(resolve, reject);
|
|
|
|
}, reject);
|
|
|
|
});
|
|
|
|
});
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
sendMessageInternal(message) {
|
2019-01-10 15:39:37 +03:00
|
|
|
message = JSON.stringify(message);
|
|
|
|
|
|
|
|
this.session.sendMessage(messageNamespace, message, this.onPlayCommandSuccess.bind(this), this.errorHandler);
|
|
|
|
return Promise.resolve();
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
onPlayCommandSuccess() {
|
2020-02-23 17:23:20 +09:00
|
|
|
console.debug('Message was sent to receiver ok.');
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback function for loadMedia success
|
|
|
|
* @param {Object} mediaSession A new media object.
|
|
|
|
*/
|
2020-08-07 08:29:07 +01:00
|
|
|
onMediaDiscovered(how, mediaSession) {
|
2020-05-04 12:44:12 +02:00
|
|
|
console.debug('chromecast new media session ID:' + mediaSession.mediaSessionId + ' (' + how + ')');
|
2019-01-10 15:39:37 +03:00
|
|
|
this.currentMediaSession = mediaSession;
|
|
|
|
|
|
|
|
if (how === 'loadMedia') {
|
|
|
|
this.castPlayerState = PLAYER_STATE.PLAYING;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (how === 'activeSession') {
|
|
|
|
this.castPlayerState = mediaSession.playerState;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.currentMediaSession.addUpdateListener(this.mediaStatusUpdateHandler);
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback function for media status update from receiver
|
|
|
|
* @param {!Boolean} e true/false
|
|
|
|
*/
|
2020-08-07 08:29:07 +01:00
|
|
|
onMediaStatusUpdate(e) {
|
2020-05-04 12:44:12 +02:00
|
|
|
console.debug('chromecast updating media: ' + e);
|
2019-01-10 15:39:37 +03:00
|
|
|
if (e === false) {
|
|
|
|
this.castPlayerState = PLAYER_STATE.IDLE;
|
|
|
|
}
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set media volume in Cast mode
|
2019-06-19 05:02:21 -07:00
|
|
|
* @param {Boolean} mute A boolean
|
2019-01-10 15:39:37 +03:00
|
|
|
*/
|
2020-08-07 08:29:07 +01:00
|
|
|
setReceiverVolume(mute, vol) {
|
2019-01-10 15:39:37 +03:00
|
|
|
if (!this.currentMediaSession) {
|
2020-02-23 17:23:20 +09:00
|
|
|
console.debug('this.currentMediaSession is null');
|
2019-01-10 15:39:37 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mute) {
|
|
|
|
this.session.setReceiverVolumeLevel((vol || 1),
|
|
|
|
this.mediaCommandSuccessCallback.bind(this),
|
|
|
|
this.errorHandler);
|
2019-06-19 05:02:21 -07:00
|
|
|
} else {
|
2019-01-10 15:39:37 +03:00
|
|
|
this.session.setReceiverMuted(true,
|
|
|
|
this.mediaCommandSuccessCallback.bind(this),
|
|
|
|
this.errorHandler);
|
|
|
|
}
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Mute CC
|
|
|
|
*/
|
2020-08-07 08:29:07 +01:00
|
|
|
mute() {
|
2019-01-10 15:39:37 +03:00
|
|
|
this.setReceiverVolume(true);
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
/**
|
2019-06-19 05:02:21 -07:00
|
|
|
* Callback function for media command success
|
2019-01-10 15:39:37 +03:00
|
|
|
*/
|
2020-08-07 08:29:07 +01:00
|
|
|
mediaCommandSuccessCallback(info, e) {
|
2020-02-23 17:23:20 +09:00
|
|
|
console.debug(info);
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
function alertText(text, title) {
|
2020-08-16 20:24:45 +02:00
|
|
|
import('../../components/alert').then(({default: alert}) => {
|
2020-08-07 08:29:07 +01:00
|
|
|
alert({
|
|
|
|
text: text,
|
|
|
|
title: title
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
function onVolumeUpKeyDown() {
|
|
|
|
playbackManager.volumeUp();
|
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
function onVolumeDownKeyDown() {
|
|
|
|
playbackManager.volumeDown();
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
function normalizeImages(state) {
|
|
|
|
if (state && state.NowPlayingItem) {
|
|
|
|
const item = state.NowPlayingItem;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
if (!item.ImageTags || !item.ImageTags.Primary) {
|
|
|
|
if (item.PrimaryImageTag) {
|
|
|
|
item.ImageTags = item.ImageTags || {};
|
|
|
|
item.ImageTags.Primary = item.PrimaryImageTag;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (item.BackdropImageTag && item.BackdropItemId === item.Id) {
|
|
|
|
item.BackdropImageTags = [item.BackdropImageTag];
|
|
|
|
}
|
|
|
|
if (item.BackdropImageTag && item.BackdropItemId !== item.Id) {
|
|
|
|
item.ParentBackdropImageTags = [item.BackdropImageTag];
|
|
|
|
item.ParentBackdropItemId = item.BackdropItemId;
|
2019-01-10 15:39:37 +03:00
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
function getItemsForPlayback(apiClient, query) {
|
|
|
|
const userId = apiClient.getCurrentUserId();
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
if (query.Ids && query.Ids.split(',').length === 1) {
|
|
|
|
return apiClient.getItem(userId, query.Ids.split(',')).then(function (item) {
|
|
|
|
return {
|
|
|
|
Items: [item],
|
|
|
|
TotalRecordCount: 1
|
|
|
|
};
|
2019-01-10 15:39:37 +03:00
|
|
|
});
|
2020-08-07 08:29:07 +01:00
|
|
|
} else {
|
|
|
|
query.Limit = query.Limit || 100;
|
|
|
|
query.ExcludeLocationTypes = 'Virtual';
|
|
|
|
query.EnableTotalRecordCount = false;
|
|
|
|
|
|
|
|
return apiClient.getItems(userId, query);
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
function bindEventForRelay(instance, eventName) {
|
2020-09-08 02:05:02 -04:00
|
|
|
Events.on(instance._castPlayer, eventName, function (e, data) {
|
2020-08-07 08:29:07 +01:00
|
|
|
console.debug('cc: ' + eventName);
|
|
|
|
const state = instance.getPlayerStateInternal(data);
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-09-08 02:05:02 -04:00
|
|
|
Events.trigger(instance, eventName, [state]);
|
2020-08-07 08:29:07 +01:00
|
|
|
});
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
function initializeChromecast() {
|
|
|
|
const instance = this;
|
|
|
|
instance._castPlayer = new CastPlayer();
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
// To allow the native android app to override
|
|
|
|
document.dispatchEvent(new CustomEvent('chromecastloaded', {
|
|
|
|
detail: {
|
|
|
|
player: instance
|
|
|
|
}
|
|
|
|
}));
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-09-08 02:05:02 -04:00
|
|
|
Events.on(instance._castPlayer, 'connect', function (e) {
|
2020-08-07 08:29:07 +01:00
|
|
|
if (currentResolve) {
|
|
|
|
sendConnectionResult(true);
|
|
|
|
} else {
|
|
|
|
playbackManager.setActivePlayer(PlayerName, instance.getCurrentTargetInfo());
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
console.debug('cc: connect');
|
|
|
|
// Reset this so that statechange will fire
|
|
|
|
instance.lastPlayerData = null;
|
|
|
|
});
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-09-08 02:05:02 -04:00
|
|
|
Events.on(instance._castPlayer, 'playbackstart', function (e, data) {
|
2020-08-07 08:29:07 +01:00
|
|
|
console.debug('cc: playbackstart');
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
instance._castPlayer.initializeCastPlayer();
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
const state = instance.getPlayerStateInternal(data);
|
2020-09-08 02:05:02 -04:00
|
|
|
Events.trigger(instance, 'playbackstart', [state]);
|
2020-08-07 08:29:07 +01:00
|
|
|
});
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-09-08 02:05:02 -04:00
|
|
|
Events.on(instance._castPlayer, 'playbackstop', function (e, data) {
|
2020-08-07 08:29:07 +01:00
|
|
|
console.debug('cc: playbackstop');
|
|
|
|
let state = instance.getPlayerStateInternal(data);
|
2019-04-01 14:34:50 +02:00
|
|
|
|
2020-09-08 02:05:02 -04:00
|
|
|
Events.trigger(instance, 'playbackstop', [state]);
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
state = instance.lastPlayerData.PlayState || {};
|
|
|
|
const volume = state.VolumeLevel || 0.5;
|
|
|
|
const mute = state.IsMuted || false;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
// Reset this so the next query doesn't make it appear like content is playing.
|
|
|
|
instance.lastPlayerData = {};
|
|
|
|
instance.lastPlayerData.PlayState = {};
|
|
|
|
instance.lastPlayerData.PlayState.VolumeLevel = volume;
|
|
|
|
instance.lastPlayerData.PlayState.IsMuted = mute;
|
|
|
|
});
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-09-08 02:05:02 -04:00
|
|
|
Events.on(instance._castPlayer, 'playbackprogress', function (e, data) {
|
2020-08-07 08:29:07 +01:00
|
|
|
console.debug('cc: positionchange');
|
|
|
|
const state = instance.getPlayerStateInternal(data);
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-09-08 02:05:02 -04:00
|
|
|
Events.trigger(instance, 'timeupdate', [state]);
|
2020-08-07 08:29:07 +01:00
|
|
|
});
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
bindEventForRelay(instance, 'timeupdate');
|
|
|
|
bindEventForRelay(instance, 'pause');
|
|
|
|
bindEventForRelay(instance, 'unpause');
|
|
|
|
bindEventForRelay(instance, 'volumechange');
|
|
|
|
bindEventForRelay(instance, 'repeatmodechange');
|
|
|
|
bindEventForRelay(instance, 'shufflequeuemodechange');
|
2018-10-23 01:05:09 +03:00
|
|
|
|
2020-09-08 02:05:02 -04:00
|
|
|
Events.on(instance._castPlayer, 'playstatechange', function (e, data) {
|
2020-08-07 08:29:07 +01:00
|
|
|
console.debug('cc: playstatechange');
|
|
|
|
const state = instance.getPlayerStateInternal(data);
|
|
|
|
|
2020-09-08 02:05:02 -04:00
|
|
|
Events.trigger(instance, 'pause', [state]);
|
2020-08-07 08:29:07 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class ChromecastPlayer {
|
|
|
|
constructor() {
|
2019-01-10 15:39:37 +03:00
|
|
|
// playbackManager needs this
|
|
|
|
this.name = PlayerName;
|
|
|
|
this.type = 'mediaplayer';
|
|
|
|
this.id = 'chromecast';
|
|
|
|
this.isLocalPlayer = false;
|
|
|
|
this.lastPlayerData = {};
|
|
|
|
|
2020-08-09 12:41:16 +01:00
|
|
|
new castSenderApiLoader().load().then(initializeChromecast.bind(this));
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
tryPair(target) {
|
|
|
|
const castPlayer = this._castPlayer;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
if (castPlayer.deviceState !== DEVICE_STATE.ACTIVE && castPlayer.isInitialized) {
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
currentResolve = resolve;
|
|
|
|
currentReject = reject;
|
|
|
|
castPlayer.launchApp();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
currentResolve = null;
|
|
|
|
currentReject = null;
|
|
|
|
|
|
|
|
return Promise.reject();
|
|
|
|
}
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
getTargets() {
|
|
|
|
const targets = [];
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
if (this._castPlayer && this._castPlayer.hasReceivers) {
|
|
|
|
targets.push(this.getCurrentTargetInfo());
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.resolve(targets);
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
// This is a privately used method
|
2020-08-07 08:29:07 +01:00
|
|
|
getCurrentTargetInfo() {
|
|
|
|
let appName = null;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
const castPlayer = this._castPlayer;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
if (castPlayer.session && castPlayer.session.receiver && castPlayer.session.receiver.friendlyName) {
|
|
|
|
appName = castPlayer.session.receiver.friendlyName;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2018-10-23 01:05:09 +03:00
|
|
|
name: PlayerName,
|
|
|
|
id: PlayerName,
|
|
|
|
playerName: PlayerName,
|
2020-05-04 12:44:12 +02:00
|
|
|
playableMediaTypes: ['Audio', 'Video'],
|
2019-01-10 15:39:37 +03:00
|
|
|
isLocalPlayer: false,
|
2018-10-23 01:05:09 +03:00
|
|
|
appName: PlayerName,
|
|
|
|
deviceName: appName,
|
2019-01-10 15:39:37 +03:00
|
|
|
supportedCommands: [
|
2020-05-04 12:44:12 +02:00
|
|
|
'VolumeUp',
|
|
|
|
'VolumeDown',
|
|
|
|
'Mute',
|
|
|
|
'Unmute',
|
|
|
|
'ToggleMute',
|
|
|
|
'SetVolume',
|
|
|
|
'SetAudioStreamIndex',
|
|
|
|
'SetSubtitleStreamIndex',
|
|
|
|
'DisplayContent',
|
|
|
|
'SetRepeatMode',
|
2020-06-23 19:09:22 +02:00
|
|
|
'SetShuffleQueue',
|
2020-05-04 12:44:12 +02:00
|
|
|
'EndSession',
|
|
|
|
'PlayMediaSource',
|
|
|
|
'PlayTrailers'
|
2019-01-10 15:39:37 +03:00
|
|
|
]
|
|
|
|
};
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
getPlayerStateInternal(data) {
|
|
|
|
let triggerStateChange = false;
|
2019-01-10 15:39:37 +03:00
|
|
|
if (data && !this.lastPlayerData) {
|
|
|
|
triggerStateChange = true;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
data = data || this.lastPlayerData;
|
|
|
|
this.lastPlayerData = data;
|
|
|
|
|
|
|
|
normalizeImages(data);
|
|
|
|
|
2020-02-23 17:23:20 +09:00
|
|
|
console.debug(JSON.stringify(data));
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
if (triggerStateChange) {
|
2020-09-08 02:05:02 -04:00
|
|
|
Events.trigger(this, 'statechange', [data]);
|
2019-01-10 15:39:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
playWithCommand(options, command) {
|
2018-10-23 01:05:09 +03:00
|
|
|
if (!options.items) {
|
2020-08-16 20:24:45 +02:00
|
|
|
const apiClient = ConnectionManager.getApiClient(options.serverId);
|
2020-08-07 08:29:07 +01:00
|
|
|
const instance = this;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
return apiClient.getItem(apiClient.getCurrentUserId(), options.ids[0]).then(function (item) {
|
|
|
|
options.items = [item];
|
|
|
|
return instance.playWithCommand(options, command);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-23 17:03:08 -05:00
|
|
|
if (options.items.length > 1 && options && options.ids) {
|
|
|
|
// Use the original request id array for sorting the result in the proper order
|
|
|
|
options.items.sort(function (a, b) {
|
|
|
|
return options.ids.indexOf(a.Id) - options.ids.indexOf(b.Id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-01-10 15:39:37 +03:00
|
|
|
return this._castPlayer.loadMedia(options, command);
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
seek(position) {
|
2019-01-10 15:39:37 +03:00
|
|
|
position = parseInt(position);
|
|
|
|
|
|
|
|
position = position / 10000000;
|
|
|
|
|
|
|
|
this._castPlayer.sendMessage({
|
2018-10-23 01:05:09 +03:00
|
|
|
options: {
|
|
|
|
position: position
|
|
|
|
},
|
2019-01-10 15:39:37 +03:00
|
|
|
command: 'Seek'
|
|
|
|
});
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
setAudioStreamIndex(index) {
|
2018-10-23 01:05:09 +03:00
|
|
|
this._castPlayer.sendMessage({
|
|
|
|
options: {
|
|
|
|
index: index
|
|
|
|
},
|
2019-01-10 15:39:37 +03:00
|
|
|
command: 'SetAudioStreamIndex'
|
|
|
|
});
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
setSubtitleStreamIndex(index) {
|
2018-10-23 01:05:09 +03:00
|
|
|
this._castPlayer.sendMessage({
|
|
|
|
options: {
|
|
|
|
index: index
|
|
|
|
},
|
2019-01-10 15:39:37 +03:00
|
|
|
command: 'SetSubtitleStreamIndex'
|
|
|
|
});
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
setMaxStreamingBitrate(options) {
|
2018-10-23 01:05:09 +03:00
|
|
|
this._castPlayer.sendMessage({
|
|
|
|
options: options,
|
2019-01-10 15:39:37 +03:00
|
|
|
command: 'SetMaxStreamingBitrate'
|
|
|
|
});
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
isFullscreen() {
|
|
|
|
let state = this.lastPlayerData || {};
|
2019-01-10 15:39:37 +03:00
|
|
|
state = state.PlayState || {};
|
|
|
|
return state.IsFullscreen;
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
nextTrack() {
|
2018-10-23 01:05:09 +03:00
|
|
|
this._castPlayer.sendMessage({
|
|
|
|
options: {},
|
2019-01-10 15:39:37 +03:00
|
|
|
command: 'NextTrack'
|
|
|
|
});
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
previousTrack() {
|
2018-10-23 01:05:09 +03:00
|
|
|
this._castPlayer.sendMessage({
|
|
|
|
options: {},
|
2019-01-10 15:39:37 +03:00
|
|
|
command: 'PreviousTrack'
|
|
|
|
});
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
volumeDown() {
|
|
|
|
let vol = this._castPlayer.session.receiver.volume.level;
|
2019-11-23 00:29:38 +09:00
|
|
|
if (vol == null) {
|
2019-03-31 12:05:54 +02:00
|
|
|
vol = 0.5;
|
2019-04-01 15:34:01 +02:00
|
|
|
}
|
2019-04-02 09:43:49 +02:00
|
|
|
vol -= 0.05;
|
2019-03-31 12:05:54 +02:00
|
|
|
vol = Math.max(vol, 0);
|
|
|
|
|
|
|
|
this._castPlayer.session.setReceiverVolumeLevel(vol);
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
endSession() {
|
|
|
|
const instance = this;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
this.stop().then(function () {
|
|
|
|
setTimeout(function () {
|
|
|
|
instance._castPlayer.stopApp();
|
|
|
|
}, 1000);
|
|
|
|
});
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
volumeUp() {
|
|
|
|
let vol = this._castPlayer.session.receiver.volume.level;
|
2019-11-23 00:29:38 +09:00
|
|
|
if (vol == null) {
|
2019-03-31 12:05:54 +02:00
|
|
|
vol = 0.5;
|
2019-04-01 15:34:01 +02:00
|
|
|
}
|
2019-04-02 09:43:49 +02:00
|
|
|
vol += 0.05;
|
2019-03-31 12:05:54 +02:00
|
|
|
vol = Math.min(vol, 1);
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2019-03-31 12:05:54 +02:00
|
|
|
this._castPlayer.session.setReceiverVolumeLevel(vol);
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
setVolume(vol) {
|
2019-01-10 15:39:37 +03:00
|
|
|
vol = Math.min(vol, 100);
|
|
|
|
vol = Math.max(vol, 0);
|
2019-03-31 12:05:54 +02:00
|
|
|
vol = vol / 100;
|
2019-06-19 05:02:21 -07:00
|
|
|
|
2019-03-31 12:05:54 +02:00
|
|
|
this._castPlayer.session.setReceiverVolumeLevel(vol);
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
unpause() {
|
2018-10-23 01:05:09 +03:00
|
|
|
this._castPlayer.sendMessage({
|
|
|
|
options: {},
|
2019-01-10 15:39:37 +03:00
|
|
|
command: 'Unpause'
|
|
|
|
});
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
playPause() {
|
2018-10-23 01:05:09 +03:00
|
|
|
this._castPlayer.sendMessage({
|
|
|
|
options: {},
|
2019-01-10 15:39:37 +03:00
|
|
|
command: 'PlayPause'
|
|
|
|
});
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
pause() {
|
2018-10-23 01:05:09 +03:00
|
|
|
this._castPlayer.sendMessage({
|
|
|
|
options: {},
|
2019-01-10 15:39:37 +03:00
|
|
|
command: 'Pause'
|
|
|
|
});
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
stop() {
|
2018-10-23 01:05:09 +03:00
|
|
|
return this._castPlayer.sendMessage({
|
|
|
|
options: {},
|
2019-01-10 15:39:37 +03:00
|
|
|
command: 'Stop'
|
|
|
|
});
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
displayContent(options) {
|
2018-10-23 01:05:09 +03:00
|
|
|
this._castPlayer.sendMessage({
|
|
|
|
options: options,
|
2019-01-10 15:39:37 +03:00
|
|
|
command: 'DisplayContent'
|
|
|
|
});
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
setMute(isMuted) {
|
|
|
|
const castPlayer = this._castPlayer;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
if (isMuted) {
|
|
|
|
castPlayer.sendMessage({
|
|
|
|
options: {},
|
|
|
|
command: 'Mute'
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
castPlayer.sendMessage({
|
|
|
|
options: {},
|
|
|
|
command: 'Unmute'
|
|
|
|
});
|
|
|
|
}
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
getRepeatMode() {
|
|
|
|
let state = this.lastPlayerData || {};
|
2019-01-10 15:39:37 +03:00
|
|
|
state = state.PlayState || {};
|
|
|
|
return state.RepeatMode;
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
getQueueShuffleMode() {
|
|
|
|
let state = this.lastPlayerData || {};
|
2020-06-22 11:10:26 +02:00
|
|
|
state = state.PlayState || {};
|
|
|
|
return state.ShuffleMode;
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2020-06-22 11:10:26 +02:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
playTrailers(item) {
|
2018-10-23 01:05:09 +03:00
|
|
|
this._castPlayer.sendMessage({
|
|
|
|
options: {
|
|
|
|
ItemId: item.Id,
|
|
|
|
ServerId: item.ServerId
|
|
|
|
},
|
2019-01-10 15:39:37 +03:00
|
|
|
command: 'PlayTrailers'
|
|
|
|
});
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
setRepeatMode(mode) {
|
2018-10-23 01:05:09 +03:00
|
|
|
this._castPlayer.sendMessage({
|
|
|
|
options: {
|
|
|
|
RepeatMode: mode
|
|
|
|
},
|
2019-01-10 15:39:37 +03:00
|
|
|
command: 'SetRepeatMode'
|
|
|
|
});
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
setQueueShuffleMode(value) {
|
2020-06-22 11:10:26 +02:00
|
|
|
this._castPlayer.sendMessage({
|
|
|
|
options: {
|
|
|
|
ShuffleMode: value
|
|
|
|
},
|
2020-06-23 19:09:22 +02:00
|
|
|
command: 'SetShuffleQueue'
|
2020-06-22 11:10:26 +02:00
|
|
|
});
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2020-06-22 11:10:26 +02:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
toggleMute() {
|
2018-10-23 01:05:09 +03:00
|
|
|
this._castPlayer.sendMessage({
|
|
|
|
options: {},
|
2019-01-10 15:39:37 +03:00
|
|
|
command: 'ToggleMute'
|
|
|
|
});
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
audioTracks() {
|
|
|
|
let state = this.lastPlayerData || {};
|
2019-01-10 15:39:37 +03:00
|
|
|
state = state.NowPlayingItem || {};
|
2020-08-07 08:29:07 +01:00
|
|
|
const streams = state.MediaStreams || [];
|
2019-01-10 15:39:37 +03:00
|
|
|
return streams.filter(function (s) {
|
|
|
|
return s.Type === 'Audio';
|
|
|
|
});
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
getAudioStreamIndex() {
|
|
|
|
let state = this.lastPlayerData || {};
|
2019-01-10 15:39:37 +03:00
|
|
|
state = state.PlayState || {};
|
|
|
|
return state.AudioStreamIndex;
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
subtitleTracks() {
|
|
|
|
let state = this.lastPlayerData || {};
|
2019-01-10 15:39:37 +03:00
|
|
|
state = state.NowPlayingItem || {};
|
2020-08-07 08:29:07 +01:00
|
|
|
const streams = state.MediaStreams || [];
|
2019-01-10 15:39:37 +03:00
|
|
|
return streams.filter(function (s) {
|
|
|
|
return s.Type === 'Subtitle';
|
|
|
|
});
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
getSubtitleStreamIndex() {
|
|
|
|
let state = this.lastPlayerData || {};
|
2019-01-10 15:39:37 +03:00
|
|
|
state = state.PlayState || {};
|
|
|
|
return state.SubtitleStreamIndex;
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
getMaxStreamingBitrate() {
|
|
|
|
let state = this.lastPlayerData || {};
|
2019-01-10 15:39:37 +03:00
|
|
|
state = state.PlayState || {};
|
|
|
|
return state.MaxStreamingBitrate;
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
getVolume() {
|
|
|
|
let state = this.lastPlayerData || {};
|
2019-01-10 15:39:37 +03:00
|
|
|
state = state.PlayState || {};
|
|
|
|
|
|
|
|
return state.VolumeLevel == null ? 100 : state.VolumeLevel;
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-25 22:21:31 -04:00
|
|
|
isPlaying(mediaType) {
|
2020-08-07 08:29:07 +01:00
|
|
|
const state = this.lastPlayerData || {};
|
2020-08-25 22:21:31 -04:00
|
|
|
return state.NowPlayingItem != null && (state.NowPlayingItem.MediaType === mediaType || !mediaType);
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
isPlayingVideo() {
|
|
|
|
let state = this.lastPlayerData || {};
|
2019-01-10 15:39:37 +03:00
|
|
|
state = state.NowPlayingItem || {};
|
|
|
|
return state.MediaType === 'Video';
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
isPlayingAudio() {
|
|
|
|
let state = this.lastPlayerData || {};
|
2019-01-10 15:39:37 +03:00
|
|
|
state = state.NowPlayingItem || {};
|
|
|
|
return state.MediaType === 'Audio';
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
currentTime(val) {
|
2019-01-10 15:39:37 +03:00
|
|
|
if (val != null) {
|
2020-08-15 11:30:36 -04:00
|
|
|
return this.seek(val * 10000);
|
2019-01-10 15:39:37 +03:00
|
|
|
}
|
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
let state = this.lastPlayerData || {};
|
2019-01-10 15:39:37 +03:00
|
|
|
state = state.PlayState || {};
|
2020-08-15 11:30:36 -04:00
|
|
|
return state.PositionTicks / 10000;
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
duration() {
|
|
|
|
let state = this.lastPlayerData || {};
|
2019-01-10 15:39:37 +03:00
|
|
|
state = state.NowPlayingItem || {};
|
|
|
|
return state.RunTimeTicks;
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
getBufferedRanges() {
|
|
|
|
let state = this.lastPlayerData || {};
|
2019-01-10 15:39:37 +03:00
|
|
|
state = state.PlayState || {};
|
|
|
|
return state.BufferedRanges || [];
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
paused() {
|
|
|
|
let state = this.lastPlayerData || {};
|
2019-01-10 15:39:37 +03:00
|
|
|
state = state.PlayState || {};
|
|
|
|
|
|
|
|
return state.IsPaused;
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
isMuted() {
|
|
|
|
let state = this.lastPlayerData || {};
|
2019-01-10 15:39:37 +03:00
|
|
|
state = state.PlayState || {};
|
|
|
|
|
|
|
|
return state.IsMuted;
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
shuffle(item) {
|
2020-08-16 20:24:45 +02:00
|
|
|
const apiClient = ConnectionManager.getApiClient(item.ServerId);
|
2020-08-07 08:29:07 +01:00
|
|
|
const userId = apiClient.getCurrentUserId();
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
const instance = this;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
apiClient.getItem(userId, item.Id).then(function (item) {
|
2018-10-23 01:05:09 +03:00
|
|
|
instance.playWithCommand({
|
|
|
|
items: [item]
|
2019-01-10 15:39:37 +03:00
|
|
|
}, 'Shuffle');
|
|
|
|
});
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
instantMix(item) {
|
2020-08-16 20:24:45 +02:00
|
|
|
const apiClient = ConnectionManager.getApiClient(item.ServerId);
|
2020-08-07 08:29:07 +01:00
|
|
|
const userId = apiClient.getCurrentUserId();
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
const instance = this;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
apiClient.getItem(userId, item.Id).then(function (item) {
|
2018-10-23 01:05:09 +03:00
|
|
|
instance.playWithCommand({
|
|
|
|
items: [item]
|
2019-01-10 15:39:37 +03:00
|
|
|
}, 'InstantMix');
|
|
|
|
});
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
canPlayMediaType(mediaType) {
|
2019-01-10 15:39:37 +03:00
|
|
|
mediaType = (mediaType || '').toLowerCase();
|
|
|
|
return mediaType === 'audio' || mediaType === 'video';
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
canQueueMediaType(mediaType) {
|
2019-01-10 15:39:37 +03:00
|
|
|
return this.canPlayMediaType(mediaType);
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
queue(options) {
|
2019-01-10 15:39:37 +03:00
|
|
|
this.playWithCommand(options, 'PlayLast');
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
queueNext(options) {
|
2019-01-10 15:39:37 +03:00
|
|
|
this.playWithCommand(options, 'PlayNext');
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
play(options) {
|
2019-01-10 15:39:37 +03:00
|
|
|
if (options.items) {
|
|
|
|
return this.playWithCommand(options, 'PlayNow');
|
|
|
|
} else {
|
|
|
|
if (!options.serverId) {
|
|
|
|
throw new Error('serverId required!');
|
|
|
|
}
|
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
const instance = this;
|
2020-08-16 20:24:45 +02:00
|
|
|
const apiClient = ConnectionManager.getApiClient(options.serverId);
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
return getItemsForPlayback(apiClient, {
|
|
|
|
Ids: options.ids.join(',')
|
|
|
|
}).then(function (result) {
|
|
|
|
options.items = result.Items;
|
|
|
|
return instance.playWithCommand(options, 'PlayNow');
|
|
|
|
});
|
|
|
|
}
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
toggleFullscreen() {
|
2019-01-10 15:39:37 +03:00
|
|
|
// not supported
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
beginPlayerUpdates() {
|
2019-01-10 15:39:37 +03:00
|
|
|
// Setup polling here
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
endPlayerUpdates() {
|
2019-01-10 15:39:37 +03:00
|
|
|
// Stop polling here
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
getPlaylist() {
|
2019-01-10 15:39:37 +03:00
|
|
|
return Promise.resolve([]);
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
getCurrentPlaylistItemId() {
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
setCurrentPlaylistItem(playlistItemId) {
|
2019-01-10 15:39:37 +03:00
|
|
|
return Promise.resolve();
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
removeFromPlaylist(playlistItemIds) {
|
2019-01-10 15:39:37 +03:00
|
|
|
return Promise.resolve();
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
getPlayerState() {
|
2019-01-10 15:39:37 +03:00
|
|
|
return this.getPlayerStateInternal() || {};
|
2020-08-07 08:29:07 +01:00
|
|
|
}
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-07 08:29:07 +01:00
|
|
|
export default ChromecastPlayer;
|