1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00

fix repeated automatic attempts to cast to a device

This commit is contained in:
dkanada 2019-06-19 05:02:21 -07:00
parent b61a5b53e5
commit abd0b0130f
2 changed files with 37 additions and 92 deletions

View file

@ -52,12 +52,10 @@ define(['appSettings', 'userSettings', 'playbackManager', 'connectionManager', '
'ERROR': 'ERROR' 'ERROR': 'ERROR'
}; };
// production version registered with google
// replace this value if you want to test changes on another instance
var applicationID = "F007D354"; var applicationID = "F007D354";
// This is the beta version used for testing new changes
//applicationID = '27C4EB5B';
var messageNamespace = 'urn:x-cast:com.connectsdk'; var messageNamespace = 'urn:x-cast:com.connectsdk';
var CastPlayer = function () { var CastPlayer = function () {
@ -91,15 +89,12 @@ define(['appSettings', 'userSettings', 'playbackManager', 'connectionManager', '
* receiverListener may be invoked at any time afterwards, and possibly more than once. * receiverListener may be invoked at any time afterwards, and possibly more than once.
*/ */
CastPlayer.prototype.initializeCastPlayer = function () { CastPlayer.prototype.initializeCastPlayer = function () {
var chrome = window.chrome; var chrome = window.chrome;
if (!chrome) { if (!chrome) {
return; return;
} }
if (!chrome.cast || !chrome.cast.isAvailable) { if (!chrome.cast || !chrome.cast.isAvailable) {
setTimeout(this.initializeCastPlayer.bind(this), 1000); setTimeout(this.initializeCastPlayer.bind(this), 1000);
return; return;
} }
@ -108,13 +103,10 @@ define(['appSettings', 'userSettings', 'playbackManager', 'connectionManager', '
var sessionRequest = new chrome.cast.SessionRequest(applicationID); var sessionRequest = new chrome.cast.SessionRequest(applicationID);
var apiConfig = new chrome.cast.ApiConfig(sessionRequest, var apiConfig = new chrome.cast.ApiConfig(sessionRequest,
this.sessionListener.bind(this), this.sessionListener.bind(this),
this.receiverListener.bind(this), this.receiverListener.bind(this));
"origin_scoped");
console.log('chromecast.initialize'); console.log('chromecast.initialize');
chrome.cast.initialize(apiConfig, this.onInitSuccess.bind(this), this.errorHandler); chrome.cast.initialize(apiConfig, this.onInitSuccess.bind(this), this.errorHandler);
}; };
/** /**
@ -140,12 +132,8 @@ define(['appSettings', 'userSettings', 'playbackManager', 'connectionManager', '
* status gets synced up with current media of the session * status gets synced up with current media of the session
*/ */
CastPlayer.prototype.sessionListener = function (e) { CastPlayer.prototype.sessionListener = function (e) {
this.session = e; this.session = e;
if (this.session) { if (this.session) {
//console.log('sessionListener ' + JSON.stringify(e));
if (this.session.media[0]) { if (this.session.media[0]) {
this.onMediaDiscovered('activeSession', this.session.media[0]); this.onMediaDiscovered('activeSession', this.session.media[0]);
} }
@ -164,28 +152,20 @@ define(['appSettings', 'userSettings', 'playbackManager', 'connectionManager', '
} }
CastPlayer.prototype.messageListener = function (namespace, message) { CastPlayer.prototype.messageListener = function (namespace, message) {
if (typeof (message) === 'string') { if (typeof (message) === 'string') {
message = JSON.parse(message); message = JSON.parse(message);
} }
if (message.type === 'playbackerror') { if (message.type === 'playbackerror') {
var errorCode = message.data; var errorCode = message.data;
setTimeout(function () { setTimeout(function () {
alertText(globalize.translate('MessagePlaybackError' + errorCode), globalize.translate('HeaderPlaybackError')); alertText(globalize.translate('MessagePlaybackError' + errorCode), globalize.translate('HeaderPlaybackError'));
}, 300); }, 300);
} else if (message.type === 'connectionerror') {
}
else if (message.type === 'connectionerror') {
setTimeout(function () { setTimeout(function () {
alertText(globalize.translate('MessageChromecastConnectionError'), globalize.translate('HeaderError')); alertText(globalize.translate('MessageChromecastConnectionError'), globalize.translate('HeaderError'));
}, 300); }, 300);
} else if (message.type) {
}
else if (message.type) {
events.trigger(this, message.type, [message.data]); events.trigger(this, message.type, [message.data]);
} }
}; };
@ -196,13 +176,11 @@ define(['appSettings', 'userSettings', 'playbackManager', 'connectionManager', '
* does not provide a list of device IDs * does not provide a list of device IDs
*/ */
CastPlayer.prototype.receiverListener = function (e) { CastPlayer.prototype.receiverListener = function (e) {
if (e === 'available') { if (e === 'available') {
//console.log("chromecast receiver found"); console.log("chromecast receiver found");
this.hasReceivers = true; this.hasReceivers = true;
} } else {
else { console.log("chromecast receiver list empty");
//console.log("chromecast receiver list empty");
this.hasReceivers = false; this.hasReceivers = false;
} }
}; };
@ -211,19 +189,16 @@ define(['appSettings', 'userSettings', 'playbackManager', 'connectionManager', '
* session update listener * session update listener
*/ */
CastPlayer.prototype.sessionUpdateListener = function (isAlive) { CastPlayer.prototype.sessionUpdateListener = function (isAlive) {
//console.log('sessionUpdateListener alive: ' + isAlive);
if (isAlive) { if (isAlive) {
} console.log('sessionUpdateListener: not alive');
else { } else {
this.session = null; this.session = null;
this.deviceState = DEVICE_STATE.IDLE; this.deviceState = DEVICE_STATE.IDLE;
this.castPlayerState = PLAYER_STATE.IDLE; this.castPlayerState = PLAYER_STATE.IDLE;
document.removeEventListener("volumeupbutton", onVolumeUpKeyDown, false); document.removeEventListener("volumeupbutton", onVolumeUpKeyDown, false);
document.removeEventListener("volumedownbutton", onVolumeDownKeyDown, false); document.removeEventListener("volumedownbutton", onVolumeDownKeyDown, false);
//console.log('sessionUpdateListener: setting currentMediaSession to null'); console.log('sessionUpdateListener: setting currentMediaSession to null');
this.currentMediaSession = null; this.currentMediaSession = null;
sendConnectionResult(false); sendConnectionResult(false);
@ -236,7 +211,7 @@ define(['appSettings', 'userSettings', 'playbackManager', 'connectionManager', '
* session request in opt_sessionRequest. * session request in opt_sessionRequest.
*/ */
CastPlayer.prototype.launchApp = function () { CastPlayer.prototype.launchApp = function () {
//console.log("chromecast launching app..."); console.log("chromecast launching app...");
chrome.cast.requestSession(this.onRequestSessionSuccess.bind(this), this.onLaunchError.bind(this)); chrome.cast.requestSession(this.onRequestSessionSuccess.bind(this), this.onLaunchError.bind(this));
}; };
@ -245,15 +220,12 @@ define(['appSettings', 'userSettings', 'playbackManager', 'connectionManager', '
* @param {Object} e A chrome.cast.Session object * @param {Object} e A chrome.cast.Session object
*/ */
CastPlayer.prototype.onRequestSessionSuccess = function (e) { CastPlayer.prototype.onRequestSessionSuccess = function (e) {
console.log("chromecast session success: " + e.sessionId);
//console.log("chromecast session success: " + e.sessionId);
this.onSessionConnected(e); this.onSessionConnected(e);
}; };
CastPlayer.prototype.onSessionConnected = function (session) { CastPlayer.prototype.onSessionConnected = function (session) {
this.session = session; this.session = session;
this.deviceState = DEVICE_STATE.ACTIVE; this.deviceState = DEVICE_STATE.ACTIVE;
this.session.addMessageListener(messageNamespace, this.messageListener.bind(this)); this.session.addMessageListener(messageNamespace, this.messageListener.bind(this));
@ -264,7 +236,6 @@ define(['appSettings', 'userSettings', 'playbackManager', 'connectionManager', '
document.addEventListener("volumedownbutton", onVolumeDownKeyDown, false); document.addEventListener("volumedownbutton", onVolumeDownKeyDown, false);
events.trigger(this, 'connect'); events.trigger(this, 'connect');
this.sendMessage({ this.sendMessage({
options: {}, options: {},
command: 'Identify' command: 'Identify'
@ -283,8 +254,6 @@ define(['appSettings', 'userSettings', 'playbackManager', 'connectionManager', '
* session update listener * session update listener
*/ */
CastPlayer.prototype.sessionMediaListener = function (e) { CastPlayer.prototype.sessionMediaListener = function (e) {
//console.log('sessionMediaListener');
this.currentMediaSession = e; this.currentMediaSession = e;
this.currentMediaSession.addUpdateListener(this.mediaStatusUpdateHandler); this.currentMediaSession.addUpdateListener(this.mediaStatusUpdateHandler);
}; };
@ -293,9 +262,8 @@ define(['appSettings', 'userSettings', 'playbackManager', 'connectionManager', '
* Callback function for launch error * Callback function for launch error
*/ */
CastPlayer.prototype.onLaunchError = function () { CastPlayer.prototype.onLaunchError = function () {
//console.log("chromecast launch error"); console.log("chromecast launch error");
this.deviceState = DEVICE_STATE.ERROR; this.deviceState = DEVICE_STATE.ERROR;
sendConnectionResult(false); sendConnectionResult(false);
}; };
@ -303,25 +271,22 @@ define(['appSettings', 'userSettings', 'playbackManager', 'connectionManager', '
* Stops the running receiver application associated with the session. * Stops the running receiver application associated with the session.
*/ */
CastPlayer.prototype.stopApp = function () { CastPlayer.prototype.stopApp = function () {
if (this.session) { if (this.session) {
this.session.stop(this.onStopAppSuccess.bind(this, 'Session stopped'), this.session.stop(this.onStopAppSuccess.bind(this, 'Session stopped'), this.errorHandler);
this.errorHandler);
} }
}; };
/** /**
* Callback function for stop app success * Callback function for stop app success
*/ */
CastPlayer.prototype.onStopAppSuccess = function (message) { CastPlayer.prototype.onStopAppSuccess = function (message) {
//console.log(message); console.log(message);
this.deviceState = DEVICE_STATE.IDLE; this.deviceState = DEVICE_STATE.IDLE;
this.castPlayerState = PLAYER_STATE.IDLE; this.castPlayerState = PLAYER_STATE.IDLE;
document.removeEventListener("volumeupbutton", onVolumeUpKeyDown, false); document.removeEventListener("volumeupbutton", onVolumeUpKeyDown, false);
document.removeEventListener("volumedownbutton", onVolumeDownKeyDown, false); document.removeEventListener("volumedownbutton", onVolumeDownKeyDown, false);
//console.log('onStopAppSuccess: setting currentMediaSession to null');
this.currentMediaSession = null; this.currentMediaSession = null;
}; };
@ -330,15 +295,13 @@ define(['appSettings', 'userSettings', 'playbackManager', 'connectionManager', '
* @param {Number} mediaIndex An index number to indicate current media content * @param {Number} mediaIndex An index number to indicate current media content
*/ */
CastPlayer.prototype.loadMedia = function (options, command) { CastPlayer.prototype.loadMedia = function (options, command) {
if (!this.session) { if (!this.session) {
//console.log("no session"); console.log("no session");
return Promise.reject(); return Promise.reject();
} }
// Convert the items to smaller stubs to send the minimal amount of information // convert items to smaller stubs to send minimal amount of information
options.items = options.items.map(function (i) { options.items = options.items.map(function (i) {
return { return {
Id: i.Id, Id: i.Id,
ServerId: i.ServerId, ServerId: i.ServerId,
@ -397,22 +360,17 @@ define(['appSettings', 'userSettings', 'playbackManager', 'connectionManager', '
} }
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
require(['chromecastHelper'], function (chromecastHelper) { require(['chromecastHelper'], function (chromecastHelper) {
chromecastHelper.getServerAddress(apiClient).then(function (serverAddress) { chromecastHelper.getServerAddress(apiClient).then(function (serverAddress) {
message.serverAddress = serverAddress; message.serverAddress = serverAddress;
player.sendMessageInternal(message).then(resolve, reject); player.sendMessageInternal(message).then(resolve, reject);
}, reject); }, reject);
}); });
}); });
}; };
CastPlayer.prototype.sendMessageInternal = function (message) { CastPlayer.prototype.sendMessageInternal = function (message) {
message = JSON.stringify(message); message = JSON.stringify(message);
//console.log(message);
this.session.sendMessage(messageNamespace, message, this.onPlayCommandSuccess.bind(this), this.errorHandler); this.session.sendMessage(messageNamespace, message, this.onPlayCommandSuccess.bind(this), this.errorHandler);
return Promise.resolve(); return Promise.resolve();
@ -447,11 +405,10 @@ define(['appSettings', 'userSettings', 'playbackManager', 'connectionManager', '
* @param {!Boolean} e true/false * @param {!Boolean} e true/false
*/ */
CastPlayer.prototype.onMediaStatusUpdate = function (e) { CastPlayer.prototype.onMediaStatusUpdate = function (e) {
//console.log("chromecast updating media: " + e);
if (e === false) { if (e === false) {
this.castPlayerState = PLAYER_STATE.IDLE; this.castPlayerState = PLAYER_STATE.IDLE;
} }
//console.log("chromecast updating media: " + e);
}; };
/** /**
@ -459,19 +416,16 @@ define(['appSettings', 'userSettings', 'playbackManager', 'connectionManager', '
* @param {Boolean} mute A boolean * @param {Boolean} mute A boolean
*/ */
CastPlayer.prototype.setReceiverVolume = function (mute, vol) { CastPlayer.prototype.setReceiverVolume = function (mute, vol) {
if (!this.currentMediaSession) { if (!this.currentMediaSession) {
//console.log('this.currentMediaSession is null'); //console.log('this.currentMediaSession is null');
return; return;
} }
if (!mute) { if (!mute) {
this.session.setReceiverVolumeLevel((vol || 1), this.session.setReceiverVolumeLevel((vol || 1),
this.mediaCommandSuccessCallback.bind(this), this.mediaCommandSuccessCallback.bind(this),
this.errorHandler); this.errorHandler);
} } else {
else {
this.session.setReceiverMuted(true, this.session.setReceiverMuted(true,
this.mediaCommandSuccessCallback.bind(this), this.mediaCommandSuccessCallback.bind(this),
this.errorHandler); this.errorHandler);
@ -636,19 +590,15 @@ define(['appSettings', 'userSettings', 'playbackManager', 'connectionManager', '
} }
ChromecastPlayer.prototype.tryPair = function (target) { ChromecastPlayer.prototype.tryPair = function (target) {
var castPlayer = this._castPlayer; var castPlayer = this._castPlayer;
if (castPlayer.deviceState !== DEVICE_STATE.ACTIVE && castPlayer.isInitialized) { if (castPlayer.deviceState !== DEVICE_STATE.ACTIVE && castPlayer.isInitialized) {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
currentResolve = resolve; currentResolve = resolve;
currentReject = reject; currentReject = reject;
castPlayer.launchApp(); castPlayer.launchApp();
}); });
} else { } else {
currentResolve = null; currentResolve = null;
currentReject = null; currentReject = null;
@ -657,7 +607,6 @@ define(['appSettings', 'userSettings', 'playbackManager', 'connectionManager', '
}; };
ChromecastPlayer.prototype.getTargets = function () { ChromecastPlayer.prototype.getTargets = function () {
var targets = []; var targets = [];
if (this._castPlayer && this._castPlayer.hasReceivers) { if (this._castPlayer && this._castPlayer.hasReceivers) {

View file

@ -919,14 +919,10 @@ define(['events', 'datetime', 'appSettings', 'itemHelper', 'pluginManager', 'pla
events.trigger(self, 'pairing'); events.trigger(self, 'pairing');
promise.then(function () { promise.then(function () {
events.trigger(self, 'paired'); events.trigger(self, 'paired');
setCurrentPlayerInternal(player, targetInfo); setCurrentPlayerInternal(player, targetInfo);
}, function () { }, function () {
events.trigger(self, 'pairerror'); events.trigger(self, 'pairerror');
if (currentPairingId === targetInfo.id) { if (currentPairingId === targetInfo.id) {
currentPairingId = null; currentPairingId = null;
} }