jellyfin-web/dashboard-ui/scripts/chromecast.js

887 lines
25 KiB
JavaScript
Raw Normal View History

2016-02-29 23:02:03 -07:00
define(['appSettings'], function (appSettings) {
2014-08-16 22:38:13 -07:00
// Based on https://github.com/googlecast/CastVideos-chrome/blob/master/CastVideos.js
2015-12-25 21:08:25 -07:00
var currentResolve;
var currentReject;
var PlayerName = 'Chromecast';
function sendConnectionResult(isOk) {
var resolve = currentResolve;
var reject = currentReject;
currentResolve = null;
currentReject = null;
if (isOk) {
if (resolve) {
resolve();
}
} else {
if (reject) {
reject();
} else {
MediaController.removeActivePlayer(PlayerName);
}
}
}
2014-08-16 22:38:13 -07:00
/**
* Constants of states for Chromecast device
**/
2014-07-07 19:27:17 -07:00
var DEVICE_STATE = {
2014-08-16 22:38:13 -07:00
'IDLE': 0,
'ACTIVE': 1,
'WARNING': 2,
2015-12-30 10:02:11 -07:00
'ERROR': 3
2014-08-16 22:38:13 -07:00
};
/**
* Constants of states for CastPlayer
**/
2014-07-07 19:27:17 -07:00
var PLAYER_STATE = {
2014-08-16 22:38:13 -07:00
'IDLE': 'IDLE',
'LOADING': 'LOADING',
'LOADED': 'LOADED',
'PLAYING': 'PLAYING',
'PAUSED': 'PAUSED',
'STOPPED': 'STOPPED',
'SEEKING': 'SEEKING',
2014-07-07 19:27:17 -07:00
'ERROR': 'ERROR'
2014-08-16 22:38:13 -07:00
};
2015-05-27 22:51:48 -07:00
var applicationID = "2D4B1DA3";
2015-05-24 11:33:28 -07:00
2015-12-30 10:02:11 -07:00
// This is the beta version used for testing new changes
2016-09-21 10:05:35 -07:00
2015-12-30 10:02:11 -07:00
//applicationID = '27C4EB5B';
var messageNamespace = 'urn:x-cast:com.connectsdk';
2014-08-16 22:38:13 -07:00
var CastPlayer = function () {
/* 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);
2014-07-07 19:27:17 -07:00
this.initializeCastPlayer();
2014-08-16 22:38:13 -07:00
};
/**
* Initialize Cast media player
* Initializes the API. Note that either successCallback and errorCallback will be
* invoked once the API has finished initialization. The sessionListener and
* receiverListener may be invoked at any time afterwards, and possibly more than once.
*/
2014-07-07 19:27:17 -07:00
CastPlayer.prototype.initializeCastPlayer = function () {
2014-08-16 22:38:13 -07:00
2016-01-08 21:28:09 -07:00
var chrome = window.chrome;
2014-07-07 19:27:17 -07:00
if (!chrome) {
return;
2014-08-16 22:38:13 -07:00
}
2014-07-07 19:27:17 -07:00
if (!chrome.cast || !chrome.cast.isAvailable) {
2014-08-16 22:38:13 -07:00
setTimeout(this.initializeCastPlayer.bind(this), 1000);
2014-07-07 19:27:17 -07:00
return;
2014-08-16 22:38:13 -07:00
}
// request session
var sessionRequest = new chrome.cast.SessionRequest(applicationID);
var apiConfig = new chrome.cast.ApiConfig(sessionRequest,
this.sessionListener.bind(this),
2016-03-04 21:16:37 -07:00
this.receiverListener.bind(this),
"origin_scoped");
2014-08-16 22:38:13 -07:00
2015-12-23 10:46:01 -07:00
console.log('chromecast.initialize');
2014-08-16 22:38:13 -07:00
2014-07-07 19:27:17 -07:00
chrome.cast.initialize(apiConfig, this.onInitSuccess.bind(this), this.errorHandler);
2014-08-16 22:38:13 -07:00
};
/**
* Callback function for init success
*/
2014-07-07 19:27:17 -07:00
CastPlayer.prototype.onInitSuccess = function () {
2014-08-16 22:38:13 -07:00
this.isInitialized = true;
2015-12-23 10:46:01 -07:00
console.log("chromecast init success");
2014-08-16 22:38:13 -07:00
};
/**
* Generic error callback function
*/
2014-07-07 19:27:17 -07:00
CastPlayer.prototype.onError = function () {
2015-12-23 10:46:01 -07:00
console.log("chromecast error");
2014-08-16 22:38:13 -07: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
* status gets synced up with current media of the session
*/
2014-07-07 19:27:17 -07:00
CastPlayer.prototype.sessionListener = function (e) {
2014-08-16 22:38:13 -07:00
this.session = e;
2014-07-07 19:27:17 -07:00
if (this.session) {
2014-08-16 22:38:13 -07:00
2015-12-23 10:46:01 -07:00
console.log('sessionListener ' + JSON.stringify(e));
2014-08-16 22:38:13 -07:00
2014-07-07 19:27:17 -07:00
if (this.session.media[0]) {
this.onMediaDiscovered('activeSession', this.session.media[0]);
2014-08-16 22:38:13 -07:00
}
2014-08-18 07:20:02 -07:00
this.onSessionConnected(e);
2014-07-07 19:27:17 -07:00
}
2014-08-16 22:38:13 -07:00
};
CastPlayer.prototype.messageListener = function (namespace, message) {
if (typeof (message) === 'string') {
message = JSON.parse(message);
}
2014-08-17 20:00:37 -07:00
2015-04-07 10:51:14 -07:00
if (message.type == 'playbackerror') {
var errorCode = message.data;
setTimeout(function () {
Dashboard.alert({
message: Globalize.translate('MessagePlaybackError' + errorCode),
title: Globalize.translate('HeaderPlaybackError')
});
}, 300);
}
else if (message.type == 'connectionerror') {
setTimeout(function () {
Dashboard.alert({
message: Globalize.translate('MessageChromecastConnectionError'),
title: Globalize.translate('HeaderError')
});
}, 300);
}
2016-03-06 23:53:38 -07:00
else if (message.type) {
2015-06-29 13:38:15 -07:00
Events.trigger(this, message.type, [message.data]);
2014-08-17 20:00:37 -07:00
}
2014-08-16 22:38:13 -07:00
};
/**
* @param {string} e Receiver availability
* This indicates availability of receivers but
* does not provide a list of device IDs
*/
2014-07-07 19:27:17 -07:00
CastPlayer.prototype.receiverListener = function (e) {
2014-08-16 22:38:13 -07:00
2014-07-07 19:27:17 -07:00
if (e === 'available') {
2015-12-23 10:46:01 -07:00
console.log("chromecast receiver found");
2014-07-07 19:27:17 -07:00
this.hasReceivers = true;
2014-08-16 22:38:13 -07:00
}
2014-07-07 19:27:17 -07:00
else {
2015-12-23 10:46:01 -07:00
console.log("chromecast receiver list empty");
2014-07-07 19:27:17 -07:00
this.hasReceivers = false;
}
2014-08-16 22:38:13 -07:00
};
/**
* session update listener
*/
2014-07-07 19:27:17 -07:00
CastPlayer.prototype.sessionUpdateListener = function (isAlive) {
2014-08-16 22:38:13 -07:00
2015-12-23 10:46:01 -07:00
console.log('sessionUpdateListener alive: ' + isAlive);
2014-08-16 22:38:13 -07:00
if (isAlive) {
}
else {
this.session = null;
this.deviceState = DEVICE_STATE.IDLE;
this.castPlayerState = PLAYER_STATE.IDLE;
2015-05-24 11:33:28 -07:00
2015-12-23 10:46:01 -07:00
console.log('sessionUpdateListener: setting currentMediaSession to null');
2014-08-16 22:38:13 -07:00
this.currentMediaSession = null;
2015-12-25 21:08:25 -07:00
sendConnectionResult(false);
2014-07-07 19:27:17 -07:00
}
2014-08-16 22:38:13 -07: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
* session request in opt_sessionRequest.
*/
2014-07-07 19:27:17 -07:00
CastPlayer.prototype.launchApp = function () {
2015-12-23 10:46:01 -07:00
console.log("chromecast launching app...");
2014-08-16 22:38:13 -07:00
chrome.cast.requestSession(this.onRequestSessionSuccess.bind(this), this.onLaunchError.bind(this));
};
/**
* Callback function for request session success
* @param {Object} e A chrome.cast.Session object
*/
2014-07-07 19:27:17 -07:00
CastPlayer.prototype.onRequestSessionSuccess = function (e) {
2014-08-18 07:20:02 -07:00
2015-12-23 10:46:01 -07:00
console.log("chromecast session success: " + e.sessionId);
2014-08-18 07:20:02 -07:00
this.onSessionConnected(e);
};
CastPlayer.prototype.onSessionConnected = function (session) {
this.session = session;
2014-08-16 22:38:13 -07:00
this.deviceState = DEVICE_STATE.ACTIVE;
2014-08-18 07:20:02 -07:00
this.session.addMessageListener(messageNamespace, this.messageListener.bind(this));
2014-08-18 19:49:16 -07:00
this.session.addMediaListener(this.sessionMediaListener.bind(this));
2014-07-07 19:27:17 -07:00
this.session.addUpdateListener(this.sessionUpdateListener.bind(this));
2014-08-18 07:20:02 -07:00
2015-06-29 13:38:15 -07:00
Events.trigger(this, 'connect');
2014-08-18 07:20:02 -07:00
2014-08-19 15:28:35 -07:00
this.sendMessage({
options: {},
command: 'Identify'
});
2014-08-16 22:38:13 -07:00
};
2014-08-18 19:49:16 -07:00
/**
* session update listener
*/
CastPlayer.prototype.sessionMediaListener = function (e) {
2015-12-23 10:46:01 -07:00
console.log('sessionMediaListener');
2014-08-18 19:49:16 -07:00
this.currentMediaSession = e;
this.currentMediaSession.addUpdateListener(this.mediaStatusUpdateHandler);
};
2014-08-16 22:38:13 -07:00
/**
* Callback function for launch error
*/
2014-07-07 19:27:17 -07:00
CastPlayer.prototype.onLaunchError = function () {
2015-12-23 10:46:01 -07:00
console.log("chromecast launch error");
2014-08-16 22:38:13 -07:00
this.deviceState = DEVICE_STATE.ERROR;
2015-12-25 21:08:25 -07:00
sendConnectionResult(false);
2014-08-16 22:38:13 -07:00
};
/**
* Stops the running receiver application associated with the session.
*/
2014-07-07 19:27:17 -07:00
CastPlayer.prototype.stopApp = function () {
2015-08-07 07:21:29 -07:00
if (this.session) {
this.session.stop(this.onStopAppSuccess.bind(this, 'Session stopped'),
this.errorHandler);
}
2014-08-16 22:38:13 -07:00
};
/**
* Callback function for stop app success
*/
2014-07-07 19:27:17 -07:00
CastPlayer.prototype.onStopAppSuccess = function (message) {
2015-12-23 10:46:01 -07:00
console.log(message);
2014-08-16 22:38:13 -07:00
this.deviceState = DEVICE_STATE.IDLE;
this.castPlayerState = PLAYER_STATE.IDLE;
2015-05-24 11:33:28 -07:00
2015-12-23 10:46:01 -07:00
console.log('onStopAppSuccess: setting currentMediaSession to null');
2014-08-16 22:38:13 -07:00
this.currentMediaSession = null;
};
/**
* Loads media into a running receiver application
* @param {Number} mediaIndex An index number to indicate current media content
*/
CastPlayer.prototype.loadMedia = function (options, command) {
2014-07-07 19:27:17 -07:00
if (!this.session) {
2015-12-23 10:46:01 -07:00
console.log("no session");
2014-07-07 19:27:17 -07:00
return;
2014-08-09 22:29:45 -07:00
}
2014-08-18 20:25:56 -07:00
// Convert the items to smaller stubs to send the minimal amount of information
options.items = options.items.map(function (i) {
return {
Id: i.Id,
Name: i.Name,
Type: i.Type,
MediaType: i.MediaType,
IsFolder: i.IsFolder
};
});
2014-08-18 19:49:16 -07:00
this.sendMessage({
2014-08-16 22:38:13 -07:00
options: options,
2014-08-18 19:49:16 -07:00
command: command
});
};
CastPlayer.prototype.sendMessage = function (message) {
2014-08-16 22:38:13 -07:00
2014-08-18 19:49:16 -07:00
var player = this;
2014-08-25 19:30:52 -07:00
var receiverName = null;
if (castPlayer.session && castPlayer.session.receiver && castPlayer.session.receiver.friendlyName) {
receiverName = castPlayer.session.receiver.friendlyName;
}
2016-03-16 13:38:01 -07:00
message = Object.assign(message, {
2014-08-16 22:38:13 -07:00
userId: Dashboard.getCurrentUserId(),
deviceId: ApiClient.deviceId(),
accessToken: ApiClient.accessToken(),
2014-08-21 19:24:38 -07:00
serverAddress: ApiClient.serverAddress(),
2016-01-04 19:07:17 -07:00
receiverName: receiverName
2014-08-18 19:49:16 -07:00
});
2014-08-16 22:38:13 -07:00
2016-02-29 23:02:03 -07:00
var bitrateSetting = appSettings.maxChromecastBitrate();
2016-01-04 19:07:17 -07:00
if (bitrateSetting) {
message.maxBitrate = bitrateSetting;
}
require(['chromecasthelpers'], function (chromecasthelpers) {
2014-08-18 09:25:59 -07:00
chromecasthelpers.getServerAddress(ApiClient).then(function (serverAddress) {
message.serverAddress = serverAddress;
2014-08-18 19:49:16 -07:00
player.sendMessageInternal(message);
});
2014-08-19 15:28:35 -07:00
});
2014-08-18 09:25:59 -07:00
};
2014-08-18 19:49:16 -07:00
CastPlayer.prototype.sendMessageInternal = function (message) {
2014-08-16 22:38:13 -07:00
message = JSON.stringify(message);
2015-12-23 10:46:01 -07:00
//console.log(message);
2014-08-16 22:38:13 -07:00
this.session.sendMessage(messageNamespace, message, this.onPlayCommandSuccess.bind(this), this.errorHandler);
2014-08-18 19:49:16 -07:00
};
2014-08-16 22:38:13 -07:00
2014-08-17 20:00:37 -07:00
CastPlayer.prototype.onPlayCommandSuccess = function () {
2015-12-23 10:46:01 -07:00
console.log('Message was sent to receiver ok.');
2014-08-16 22:38:13 -07:00
};
/**
* Callback function for loadMedia success
* @param {Object} mediaSession A new media object.
*/
2014-07-07 19:27:17 -07:00
CastPlayer.prototype.onMediaDiscovered = function (how, mediaSession) {
2014-08-16 22:38:13 -07:00
2015-12-23 10:46:01 -07:00
console.log("chromecast new media session ID:" + mediaSession.mediaSessionId + ' (' + how + ')');
2014-08-16 22:38:13 -07:00
this.currentMediaSession = mediaSession;
2014-07-07 19:27:17 -07:00
if (how == 'loadMedia') {
2014-08-16 22:38:13 -07:00
this.castPlayerState = PLAYER_STATE.PLAYING;
}
2014-07-07 19:27:17 -07:00
if (how == 'activeSession') {
this.castPlayerState = mediaSession.playerState;
2014-08-16 22:38:13 -07:00
}
this.currentMediaSession.addUpdateListener(this.mediaStatusUpdateHandler);
};
/**
* Callback function for media status update from receiver
* @param {!Boolean} e true/false
*/
2014-07-07 19:27:17 -07:00
CastPlayer.prototype.onMediaStatusUpdate = function (e) {
2014-08-17 11:12:17 -07:00
2014-07-07 19:27:17 -07:00
if (e == false) {
this.castPlayerState = PLAYER_STATE.IDLE;
2014-08-16 22:38:13 -07:00
}
2015-12-23 10:46:01 -07:00
console.log("chromecast updating media: " + e);
2014-08-16 22:38:13 -07:00
};
/**
* Set media volume in Cast mode
* @param {Boolean} mute A boolean
*/
2014-07-07 19:27:17 -07:00
CastPlayer.prototype.setReceiverVolume = function (mute, vol) {
2014-08-16 22:38:13 -07:00
2014-07-07 19:27:17 -07:00
if (!this.currentMediaSession) {
2015-12-23 10:46:01 -07:00
console.log('this.currentMediaSession is null');
2014-07-07 19:27:17 -07:00
return;
2014-08-16 22:38:13 -07:00
}
2014-07-07 19:27:17 -07:00
if (!mute) {
2014-08-17 20:00:37 -07:00
2014-08-21 08:55:35 -07:00
this.session.setReceiverVolumeLevel((vol || 1),
2014-08-16 22:38:13 -07:00
this.mediaCommandSuccessCallback.bind(this),
2014-07-07 19:27:17 -07:00
this.errorHandler);
2014-08-16 22:38:13 -07:00
}
2014-07-07 19:27:17 -07:00
else {
2014-08-16 22:38:13 -07:00
this.session.setReceiverMuted(true,
this.mediaCommandSuccessCallback.bind(this),
2014-07-07 19:27:17 -07:00
this.errorHandler);
}
2014-08-16 22:38:13 -07:00
};
/**
* Mute CC
*/
2014-07-07 19:27:17 -07:00
CastPlayer.prototype.mute = function () {
this.setReceiverVolume(true);
2014-08-16 22:38:13 -07:00
};
/**
* Callback function for media command success
*/
2014-07-07 19:27:17 -07:00
CastPlayer.prototype.mediaCommandSuccessCallback = function (info, e) {
2015-12-23 10:46:01 -07:00
console.log(info);
2014-08-16 22:38:13 -07:00
};
// Create Cast Player
2015-05-08 20:48:43 -07:00
var castPlayer;
2014-08-16 22:38:13 -07:00
function chromecastPlayer() {
var self = this;
// MediaController needs this
self.name = PlayerName;
self.getItemsForPlayback = function (query) {
var userId = Dashboard.getCurrentUserId();
2015-08-14 20:15:25 -07:00
if (query.Ids && query.Ids.split(',').length == 1) {
2016-03-04 21:16:37 -07:00
return ApiClient.getItem(userId, query.Ids.split(',')).then(function (item) {
return {
Items: [item],
TotalRecordCount: 1
};
2015-12-14 08:43:03 -07:00
});
2015-08-14 20:15:25 -07:00
}
else {
2014-08-16 22:38:13 -07:00
2015-08-14 20:15:25 -07:00
query.Limit = query.Limit || 100;
query.ExcludeLocationTypes = "Virtual";
return ApiClient.getItems(userId, query);
}
2014-07-07 19:27:17 -07:00
};
2014-08-09 22:29:45 -07:00
2015-12-23 10:46:01 -07:00
Events.on(castPlayer, "connect", function (e) {
2014-08-09 22:29:45 -07:00
2015-12-25 21:08:25 -07:00
if (currentResolve) {
sendConnectionResult(true);
} else {
MediaController.setActivePlayer(PlayerName, self.getCurrentTargetInfo());
}
2015-05-18 09:40:20 -07:00
2015-12-23 10:46:01 -07:00
console.log('cc: connect');
2014-08-18 07:20:02 -07:00
// Reset this so the next query doesn't make it appear like content is playing.
self.lastPlayerData = {};
});
2014-08-16 22:38:13 -07:00
2015-12-23 10:46:01 -07:00
Events.on(castPlayer, "playbackstart", function (e, data) {
2014-08-16 22:38:13 -07:00
2015-12-23 10:46:01 -07:00
console.log('cc: playbackstart');
2014-08-18 19:49:16 -07:00
castPlayer.initializeCastPlayer();
2014-08-17 20:00:37 -07:00
var state = self.getPlayerStateInternal(data);
2015-06-29 13:38:15 -07:00
Events.trigger(self, "playbackstart", [state]);
2014-08-17 20:00:37 -07:00
});
2014-08-16 22:38:13 -07:00
2015-12-23 10:46:01 -07:00
Events.on(castPlayer, "playbackstop", function (e, data) {
2014-08-16 22:38:13 -07:00
2015-12-23 10:46:01 -07:00
console.log('cc: playbackstop');
2014-08-17 20:00:37 -07:00
var state = self.getPlayerStateInternal(data);
2014-08-16 22:38:13 -07:00
2015-06-29 13:38:15 -07:00
Events.trigger(self, "playbackstop", [state]);
2014-08-18 07:20:02 -07:00
// Reset this so the next query doesn't make it appear like content is playing.
self.lastPlayerData = {};
2014-08-17 20:00:37 -07:00
});
2014-08-16 22:38:13 -07:00
2015-12-23 10:46:01 -07:00
Events.on(castPlayer, "playbackprogress", function (e, data) {
2014-08-16 22:38:13 -07:00
2015-12-23 10:46:01 -07:00
console.log('cc: positionchange');
2014-08-17 20:00:37 -07:00
var state = self.getPlayerStateInternal(data);
2014-08-16 22:38:13 -07:00
2015-06-29 13:38:15 -07:00
Events.trigger(self, "positionchange", [state]);
2014-08-17 20:00:37 -07:00
});
2014-08-09 22:29:45 -07:00
2016-03-06 23:53:38 -07:00
Events.on(castPlayer, "volumechange", function (e, data) {
console.log('cc: volumechange');
var state = self.getPlayerStateInternal(data);
Events.trigger(self, "volumechange", [state]);
});
Events.on(castPlayer, "playstatechange", function (e, data) {
console.log('cc: playstatechange');
var state = self.getPlayerStateInternal(data);
Events.trigger(self, "playstatechange", [state]);
});
2014-07-07 19:27:17 -07:00
self.play = function (options) {
2014-08-09 22:29:45 -07:00
2015-12-14 08:43:03 -07:00
Dashboard.getCurrentUser().then(function (user) {
2014-08-09 22:29:45 -07:00
if (options.items) {
2014-08-18 07:20:02 -07:00
self.playWithCommand(options, 'PlayNow');
2014-08-09 22:29:45 -07:00
} else {
self.getItemsForPlayback({
Ids: options.ids.join(',')
2015-12-14 08:43:03 -07:00
}).then(function (result) {
2014-08-09 22:29:45 -07:00
2014-08-18 07:20:02 -07:00
options.items = result.Items;
self.playWithCommand(options, 'PlayNow');
2014-08-09 22:29:45 -07:00
});
}
});
};
self.playWithCommand = function (options, command) {
2014-08-16 22:38:13 -07:00
if (!options.items) {
2015-12-14 08:43:03 -07:00
ApiClient.getItem(Dashboard.getCurrentUserId(), options.ids[0]).then(function (item) {
2014-08-16 22:38:13 -07:00
options.items = [item];
self.playWithCommand(options, command);
});
return;
}
castPlayer.loadMedia(options, command);
};
2014-07-07 19:27:17 -07:00
self.unpause = function () {
2015-05-23 13:44:15 -07:00
castPlayer.sendMessage({
options: {},
command: 'Unpause'
});
2014-08-16 22:38:13 -07:00
};
2014-07-07 19:27:17 -07:00
self.pause = function () {
2015-05-23 13:44:15 -07:00
castPlayer.sendMessage({
options: {},
command: 'Pause'
});
2014-08-16 22:38:13 -07:00
};
2014-07-07 19:27:17 -07:00
self.shuffle = function (id) {
2014-08-09 22:29:45 -07:00
var userId = Dashboard.getCurrentUserId();
2015-12-14 08:43:03 -07:00
ApiClient.getItem(userId, id).then(function (item) {
2014-08-09 22:29:45 -07:00
2014-08-18 07:20:02 -07:00
self.playWithCommand({
2014-08-09 22:29:45 -07:00
2014-08-18 07:20:02 -07:00
items: [item]
2014-08-09 22:29:45 -07:00
2014-08-18 07:20:02 -07:00
}, 'Shuffle');
2014-08-09 22:29:45 -07:00
2014-07-07 19:27:17 -07:00
});
2014-08-09 22:29:45 -07:00
};
2014-07-07 19:27:17 -07:00
self.instantMix = function (id) {
2014-08-09 22:29:45 -07:00
var userId = Dashboard.getCurrentUserId();
2015-12-14 08:43:03 -07:00
ApiClient.getItem(userId, id).then(function (item) {
2014-08-09 22:29:45 -07:00
2014-08-18 07:20:02 -07:00
self.playWithCommand({
2014-08-09 22:29:45 -07:00
2014-08-18 07:20:02 -07:00
items: [item]
2014-08-09 22:29:45 -07:00
2014-08-18 07:20:02 -07:00
}, 'InstantMix');
2014-08-09 22:29:45 -07:00
2014-07-07 19:27:17 -07:00
});
2014-08-09 22:29:45 -07:00
};
2014-08-16 22:38:13 -07:00
2014-07-07 19:27:17 -07:00
self.canQueueMediaType = function (mediaType) {
return mediaType == "Audio";
2014-08-16 22:38:13 -07:00
};
2014-07-07 19:27:17 -07:00
self.queue = function (options) {
2016-02-17 22:34:53 -07:00
self.playWithCommand(options, 'PlayLast');
2014-08-16 22:38:13 -07:00
};
2014-07-07 19:27:17 -07:00
self.queueNext = function (options) {
2014-08-09 22:29:45 -07:00
self.playWithCommand(options, 'PlayNext');
2014-08-16 22:38:13 -07:00
};
2014-07-07 19:27:17 -07:00
self.stop = function () {
2015-05-23 13:44:15 -07:00
castPlayer.sendMessage({
options: {},
command: 'Stop'
});
2014-08-16 22:38:13 -07:00
};
2014-07-07 19:27:17 -07:00
self.displayContent = function (options) {
2014-08-16 22:38:13 -07:00
2014-08-18 19:49:16 -07:00
castPlayer.sendMessage({
options: options,
command: 'DisplayContent'
});
2014-08-16 22:38:13 -07:00
};
2014-07-07 19:27:17 -07:00
self.mute = function () {
2015-05-23 13:44:15 -07:00
castPlayer.sendMessage({
options: {},
command: 'Mute'
});
2015-05-24 11:33:28 -07:00
//castPlayer.mute();
2014-08-16 22:38:13 -07:00
};
2014-07-07 19:27:17 -07:00
self.unMute = function () {
2014-08-21 08:55:35 -07:00
self.setVolume(getCurrentVolume() + 2);
2014-08-16 22:38:13 -07:00
};
2015-07-26 14:02:23 -07:00
self.setRepeatMode = function (mode) {
castPlayer.sendMessage({
options: {
RepeatMode: mode
},
command: 'SetRepeatMode'
});
};
2014-07-07 19:27:17 -07:00
self.toggleMute = function () {
2014-08-21 08:55:35 -07:00
var state = self.lastPlayerData || {};
state = state.PlayState || {};
if (state.IsMuted) {
self.unMute();
} else {
self.mute();
}
2014-08-16 22:38:13 -07:00
};
2014-07-07 19:27:17 -07:00
self.getTargets = function () {
2014-08-16 22:38:13 -07:00
2016-03-04 21:16:37 -07:00
var targets = [];
2014-08-16 22:38:13 -07:00
2016-03-04 21:16:37 -07:00
if (castPlayer.hasReceivers) {
targets.push(self.getCurrentTargetInfo());
}
2014-08-16 22:38:13 -07:00
2016-03-04 21:16:37 -07:00
return Promise.resolve(targets);
2014-08-16 22:38:13 -07:00
};
2014-07-07 19:27:17 -07:00
self.getCurrentTargetInfo = function () {
2014-08-16 22:38:13 -07:00
var appName = null;
2014-07-07 19:27:17 -07:00
if (castPlayer.session && castPlayer.session.receiver && castPlayer.session.receiver.friendlyName) {
appName = castPlayer.session.receiver.friendlyName;
2014-08-16 22:38:13 -07:00
}
2014-07-07 19:27:17 -07:00
return {
2014-08-16 22:38:13 -07:00
name: PlayerName,
id: PlayerName,
playerName: PlayerName,
playableMediaTypes: ["Audio", "Video"],
isLocalPlayer: false,
appName: PlayerName,
deviceName: appName,
supportedCommands: ["VolumeUp",
"VolumeDown",
"Mute",
"Unmute",
"ToggleMute",
2014-08-21 08:55:35 -07:00
"SetVolume",
"SetAudioStreamIndex",
"SetSubtitleStreamIndex",
2015-07-26 14:02:23 -07:00
"DisplayContent",
2015-08-30 10:26:30 -07:00
"SetRepeatMode",
"EndSession"]
2014-07-07 19:27:17 -07:00
};
2014-08-16 22:38:13 -07:00
};
2014-07-07 19:27:17 -07:00
self.seek = function (position) {
2015-05-23 13:44:15 -07:00
position = parseInt(position);
position = position / 10000000;
castPlayer.sendMessage({
options: {
position: position
},
command: 'Seek'
});
2014-08-16 22:38:13 -07:00
};
2014-08-21 08:55:35 -07:00
self.setAudioStreamIndex = function (index) {
castPlayer.sendMessage({
options: {
index: index
},
command: 'SetAudioStreamIndex'
});
};
self.setSubtitleStreamIndex = function (index) {
castPlayer.sendMessage({
options: {
index: index
},
command: 'SetSubtitleStreamIndex'
});
};
2014-07-07 19:27:17 -07:00
self.nextTrack = function () {
2014-08-18 20:25:56 -07:00
castPlayer.sendMessage({
options: {},
command: 'NextTrack'
});
2014-08-16 22:38:13 -07:00
};
2014-07-07 19:27:17 -07:00
self.previousTrack = function () {
2014-08-18 20:25:56 -07:00
castPlayer.sendMessage({
options: {},
command: 'PreviousTrack'
});
2014-08-16 22:38:13 -07:00
};
self.beginPlayerUpdates = function () {
// Setup polling here
};
self.endPlayerUpdates = function () {
// Stop polling here
};
2014-08-18 18:42:53 -07:00
function getCurrentVolume() {
var state = self.lastPlayerData || {};
state = state.PlayState || {};
return state.VolumeLevel == null ? 100 : state.VolumeLevel;
}
2014-07-07 19:27:17 -07:00
self.volumeDown = function () {
2014-08-18 18:42:53 -07:00
2015-06-08 22:56:46 -07:00
castPlayer.sendMessage({
options: {},
command: 'VolumeDown'
});
2014-08-16 22:38:13 -07:00
};
2015-08-30 10:26:30 -07:00
self.endSession = function () {
self.stop();
setTimeout(function () {
castPlayer.stopApp();
}, 1000);
2015-08-30 10:26:30 -07:00
};
2014-07-07 19:27:17 -07:00
self.volumeUp = function () {
2014-08-18 18:42:53 -07:00
2015-06-08 22:56:46 -07:00
castPlayer.sendMessage({
options: {},
command: 'VolumeUp'
});
2014-08-16 22:38:13 -07:00
};
2014-07-07 19:27:17 -07:00
self.setVolume = function (vol) {
2014-08-18 18:42:53 -07:00
vol = Math.min(vol, 100);
vol = Math.max(vol, 0);
2015-05-23 13:44:15 -07:00
//castPlayer.setReceiverVolume(false, (vol / 100));
castPlayer.sendMessage({
options: {
volume: vol
},
command: 'SetVolume'
});
2014-08-16 22:38:13 -07:00
};
2014-07-07 19:27:17 -07:00
self.getPlayerState = function () {
2014-08-16 22:38:13 -07:00
2016-03-04 21:16:37 -07:00
var result = self.getPlayerStateInternal();
return Promise.resolve(result);
2014-08-16 22:38:13 -07:00
};
2014-08-17 20:00:37 -07:00
self.lastPlayerData = {};
2014-08-16 22:38:13 -07:00
2014-08-17 20:00:37 -07:00
self.getPlayerStateInternal = function (data) {
2014-08-16 22:38:13 -07:00
2014-08-17 20:00:37 -07:00
data = data || self.lastPlayerData;
self.lastPlayerData = data;
2014-08-18 19:49:16 -07:00
2015-12-23 10:46:01 -07:00
console.log(JSON.stringify(data));
2014-08-17 20:00:37 -07:00
return data;
2014-07-07 19:27:17 -07:00
};
2015-05-18 09:40:20 -07:00
self.tryPair = function (target) {
2016-09-21 10:05:35 -07:00
if (castPlayer.deviceState != DEVICE_STATE.ACTIVE && castPlayer.isInitialized) {
2015-12-25 21:08:25 -07:00
2016-09-21 10:05:35 -07:00
return new Promise(function (resolve, reject) {
2015-12-25 21:08:25 -07:00
currentResolve = resolve;
currentReject = reject;
castPlayer.launchApp();
2016-09-21 10:05:35 -07:00
});
} else {
2015-12-25 21:08:25 -07:00
2016-09-21 10:05:35 -07:00
currentResolve = null;
currentReject = null;
2015-12-25 21:08:25 -07:00
2016-09-21 10:05:35 -07:00
return Promise.reject();
}
2015-05-18 09:40:20 -07:00
};
2014-08-16 22:38:13 -07:00
}
2015-05-08 20:48:43 -07:00
function initializeChromecast() {
castPlayer = new CastPlayer();
2014-08-16 22:38:13 -07:00
2016-01-08 21:28:09 -07:00
var registeredPlayer = new chromecastPlayer();
2016-09-04 22:39:14 -07:00
window.CCastPlayer = registeredPlayer;
2016-01-08 21:28:09 -07:00
MediaController.registerPlayer(registeredPlayer);
// To allow the native android app to override
document.dispatchEvent(new CustomEvent("chromecastloaded", {
detail: {
player: registeredPlayer
}
}));
2015-05-08 20:48:43 -07:00
}
2016-02-07 12:47:09 -07:00
var fileref = document.createElement('script');
fileref.setAttribute("type", "text/javascript");
fileref.onload = initializeChromecast;
fileref.setAttribute("src", "https://www.gstatic.com/cv/js/sender/v1/cast_sender.js");
document.querySelector('head').appendChild(fileref);
2014-08-16 22:38:13 -07:00
2016-01-08 21:28:09 -07:00
});