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

1285 lines
40 KiB
JavaScript
Raw Normal View History

2014-02-23 00:52:30 -05:00
(function (window, chrome, console) {
// Based on https://github.com/googlecast/CastVideos-chrome/blob/master/CastVideos.js
2014-02-23 12:53:25 -05:00
2014-02-23 00:52:30 -05:00
/**
* Constants of states for Chromecast device
**/
2014-07-07 22:27:17 -04:00
var DEVICE_STATE = {
2014-02-23 00:52:30 -05:00
'IDLE': 0,
'ACTIVE': 1,
'WARNING': 2,
2014-07-07 22:27:17 -04:00
'ERROR': 3,
2014-02-23 00:52:30 -05:00
};
/**
* Constants of states for CastPlayer
**/
2014-07-07 22:27:17 -04:00
var PLAYER_STATE = {
2014-02-23 00:52:30 -05:00
'IDLE': 'IDLE',
'LOADING': 'LOADING',
'LOADED': 'LOADED',
'PLAYING': 'PLAYING',
'PAUSED': 'PAUSED',
'STOPPED': 'STOPPED',
'SEEKING': 'SEEKING',
2014-07-07 22:27:17 -04:00
'ERROR': 'ERROR'
2014-02-23 00:52:30 -05:00
};
2014-04-06 13:53:23 -04:00
var PlayerName = 'Chromecast';
2014-07-07 22:27:17 -04:00
var cPlayer = {
deviceState: DEVICE_STATE.IDLE
};
2014-02-23 00:52:30 -05: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 {Number} volume
2014-04-15 20:49:49 -07:00
this.currentVolume = 1;
2014-04-06 13:53:23 -04:00
2014-02-23 00:52:30 -05:00
// @type {string} a chrome.cast.Session object
this.session = null;
// @type {PLAYER_STATE} A state for Cast media player
this.castPlayerState = PLAYER_STATE.IDLE;
// @type {Boolean} Fullscreen mode on/off
this.fullscreen = false;
/* Current media variables */
// @type {Boolean} Audio on and off
this.audio = true;
// @type {Number} A number for current media index
this.currentMediaIndex = 0;
// @type {Number} A number for current media time
this.currentMediaTime = 0;
// @type {Number} A number for current media duration
this.currentMediaDuration = -1;
// @type {Timer} A timer for tracking progress of media
this.timer = null;
// @type {Boolean} A boolean to stop timer update of progress when triggered by media status event
this.progressFlag = true;
// @type {Number} A number in milliseconds for minimal progress update
this.timerStep = 1000;
2014-02-23 22:27:13 -05:00
this.hasReceivers = false;
2014-04-09 16:58:09 -07:00
this.currentMediaOffset = 0;
// Progress bar element id
this.progressBar = "positionSlider";
// Timec display element id
this.duration = "currentTime";
// Playback display element id
this.playback = "playTime";
// bind once - commit 2ebffc2271da0bc5e8b13821586aee2a2e3c7753
this.errorHandler = this.onError.bind(this);
this.incrementMediaTimeHandler = this.incrementMediaTime.bind(this);
this.mediaStatusUpdateHandler = this.onMediaStatusUpdate.bind(this);
2014-07-07 22:27:17 -04:00
this.initializeCastPlayer();
2014-02-23 00:52:30 -05: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 22:27:17 -04:00
CastPlayer.prototype.initializeCastPlayer = function () {
2014-02-23 00:52:30 -05:00
2014-07-07 22:27:17 -04:00
if (!chrome) {
return;
2014-02-23 12:29:02 -05:00
}
2014-02-23 12:53:25 -05:00
2014-07-07 22:27:17 -04:00
if (!chrome.cast || !chrome.cast.isAvailable) {
2014-03-30 16:26:16 -07:00
2014-02-23 12:29:02 -05:00
setTimeout(this.initializeCastPlayer.bind(this), 1000);
2014-07-07 22:27:17 -04:00
return;
2014-02-23 00:52:30 -05:00
}
2014-04-06 13:53:23 -04:00
// v1 Id AE4DA10A
// v2 Id 472F0435
2014-07-07 22:27:17 -04:00
// default receiver chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID
var applicationID = chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID;
2014-02-23 00:52:30 -05:00
// request session
var sessionRequest = new chrome.cast.SessionRequest(applicationID);
var apiConfig = new chrome.cast.ApiConfig(sessionRequest,
this.sessionListener.bind(this),
this.receiverListener.bind(this));
2014-04-06 13:53:23 -04:00
console.log('chromecast.initialize');
2014-02-23 12:53:25 -05:00
2014-07-07 22:27:17 -04:00
chrome.cast.initialize(apiConfig, this.onInitSuccess.bind(this), this.errorHandler);
2014-02-23 00:52:30 -05:00
};
/**
* Callback function for init success
*/
2014-07-07 22:27:17 -04:00
CastPlayer.prototype.onInitSuccess = function () {
2014-04-06 13:53:23 -04:00
this.isInitialized = true;
2014-07-07 22:27:17 -04:00
console.log("chromecast init success");
2014-02-23 00:52:30 -05:00
};
/**
* Generic error callback function
*/
2014-07-07 22:27:17 -04:00
CastPlayer.prototype.onError = function () {
console.log("chromecast error");
2014-02-23 00:52:30 -05: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 22:27:17 -04:00
CastPlayer.prototype.sessionListener = function (e) {
2014-02-23 00:52:30 -05:00
this.session = e;
2014-07-07 22:27:17 -04:00
if (this.session) {
2014-02-23 00:52:30 -05:00
this.deviceState = DEVICE_STATE.ACTIVE;
2014-04-06 13:53:23 -04:00
MediaController.setActivePlayer(PlayerName);
2014-07-07 22:27:17 -04:00
if (this.session.media[0]) {
this.onMediaDiscovered('activeSession', this.session.media[0]);
2014-02-23 00:52:30 -05:00
}
2014-04-06 13:53:23 -04:00
2014-07-07 22:27:17 -04:00
this.session.addUpdateListener(this.sessionUpdateListener.bind(this));
}
2014-02-23 00:52:30 -05:00
};
/**
* @param {string} e Receiver availability
* This indicates availability of receivers but
* does not provide a list of device IDs
*/
2014-07-07 22:27:17 -04:00
CastPlayer.prototype.receiverListener = function (e) {
2014-03-30 16:26:16 -07:00
2014-07-07 22:27:17 -04:00
if (e === 'available') {
console.log("chromecast receiver found");
2014-07-07 22:27:17 -04:00
this.hasReceivers = true;
2014-02-23 00:52:30 -05:00
}
2014-07-07 22:27:17 -04:00
else {
console.log("chromecast receiver list empty");
2014-07-07 22:27:17 -04:00
this.hasReceivers = false;
}
2014-02-23 00:52:30 -05:00
};
2014-03-30 16:26:16 -07:00
/**
* session update listener
*/
2014-07-07 22:27:17 -04:00
CastPlayer.prototype.sessionUpdateListener = function (isAlive) {
if (!isAlive) {
2014-03-30 16:26:16 -07:00
this.session = null;
this.deviceState = DEVICE_STATE.IDLE;
this.castPlayerState = PLAYER_STATE.IDLE;
this.currentMediaSession = null;
clearInterval(this.timer);
2014-07-07 22:27:17 -04:00
MediaController.removeActivePlayer(PlayerName);
}
2014-03-30 16:26:16 -07:00
};
2014-02-23 00:52:30 -05: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 22:27:17 -04:00
CastPlayer.prototype.launchApp = function () {
2014-04-06 13:53:23 -04:00
console.log("chromecast launching app...");
2014-02-23 00:52:30 -05:00
chrome.cast.requestSession(this.onRequestSessionSuccess.bind(this), this.onLaunchError.bind(this));
2014-07-07 22:27:17 -04:00
if (this.timer) {
clearInterval(this.timer);
}
2014-02-23 00:52:30 -05:00
};
/**
* Callback function for request session success
* @param {Object} e A chrome.cast.Session object
*/
2014-07-07 22:27:17 -04:00
CastPlayer.prototype.onRequestSessionSuccess = function (e) {
2014-04-06 13:53:23 -04:00
console.log("chromecast session success: " + e.sessionId);
2014-02-23 00:52:30 -05:00
this.session = e;
this.deviceState = DEVICE_STATE.ACTIVE;
2014-07-07 22:27:17 -04:00
this.session.addUpdateListener(this.sessionUpdateListener.bind(this));
2014-02-23 00:52:30 -05:00
};
/**
* Callback function for launch error
*/
2014-07-07 22:27:17 -04:00
CastPlayer.prototype.onLaunchError = function () {
2014-04-06 13:53:23 -04:00
console.log("chromecast launch error");
2014-02-23 00:52:30 -05:00
this.deviceState = DEVICE_STATE.ERROR;
2014-07-07 22:27:17 -04:00
Dashboard.alert({
2014-02-23 15:35:58 -05:00
2014-06-04 15:39:16 -04:00
title: Globalize.translate("Error"),
2014-07-07 22:27:17 -04:00
message: Globalize.translate("ErrorLaunchingChromecast")
2014-02-23 15:35:58 -05:00
});
2014-07-07 22:27:17 -04:00
MediaController.removeActivePlayer(PlayerName);
2014-02-23 00:52:30 -05:00
};
/**
* Stops the running receiver application associated with the session.
*/
2014-07-07 22:27:17 -04:00
CastPlayer.prototype.stopApp = function () {
2014-02-23 00:52:30 -05:00
this.session.stop(this.onStopAppSuccess.bind(this, 'Session stopped'),
2014-07-07 22:27:17 -04:00
this.errorHandler);
2014-02-23 00:52:30 -05:00
};
/**
* Callback function for stop app success
*/
2014-07-07 22:27:17 -04:00
CastPlayer.prototype.onStopAppSuccess = function (message) {
2014-02-23 00:52:30 -05:00
console.log(message);
this.deviceState = DEVICE_STATE.IDLE;
this.castPlayerState = PLAYER_STATE.IDLE;
this.currentMediaSession = null;
2014-07-07 22:27:17 -04:00
clearInterval(this.timer);
2014-02-23 00:52:30 -05:00
};
2014-04-15 20:49:49 -07:00
/**
* Loads media into a running receiver application
* @param {Number} mediaIndex An index number to indicate current media content
*/
2014-07-07 22:27:17 -04:00
CastPlayer.prototype.loadMedia = function (userId, options, command) {
var cPlayer = this;
if (!this.session) {
2014-04-15 20:49:49 -07:00
console.log("no session");
2014-07-07 22:27:17 -04:00
return;
2014-04-15 20:49:49 -07:00
}
2014-07-07 22:27:17 -04:00
startTimeTicks = 0; // TODO: update this
this.currentMediaOffset = startTimeTicks || 0;
2014-04-15 20:49:49 -07:00
2014-07-07 22:27:17 -04:00
ApiClient.getItem(userId, options.ids[0]).done(function (item) {
var maxBitrate = 12000000;
var mediaInfo = getMediaSourceInfo(userId, item, maxBitrate, item.MediaSources[0].Id, 0, 0);
2014-04-15 20:49:49 -07:00
2014-07-07 22:27:17 -04:00
var streamUrl = getStreamUrl(item, mediaInfo, startTimeTicks, maxBitrate);
2014-04-15 20:49:49 -07:00
2014-07-07 22:27:17 -04:00
var castMediaInfo = new chrome.cast.media.MediaInfo(streamUrl);
2014-04-15 20:49:49 -07:00
2014-07-07 22:27:17 -04:00
castMediaInfo.customData = getCustomData(item, mediaInfo.mediaSource.Id, startTimeTicks);
castMediaInfo.metadata = getMetadata(item);
if (mediaInfo.streamContainer == 'm3u8') {
castMediaInfo.contentType = 'application/x-mpegURL';
} else {
castMediaInfo.contentType = item.MediaType.toLowerCase() + '/' + mediaInfo.streamContainer.toLowerCase();
}
2014-04-15 20:49:49 -07:00
2014-07-07 22:27:17 -04:00
castMediaInfo.streamType = mediaInfo.isStatic ? chrome.cast.media.StreamType.BUFFERED : chrome.cast.media.StreamType.LIVE;
2014-04-15 20:49:49 -07:00
2014-07-07 22:27:17 -04:00
var request = new chrome.cast.media.LoadRequest(castMediaInfo);
request.autoplay = true;
request.currentTime = startTimeTicks ? startTimeTicks / 10000000 : 0;
2014-04-15 20:49:49 -07:00
2014-07-07 22:27:17 -04:00
cPlayer.castPlayerState = PLAYER_STATE.LOADING;
cPlayer.session.loadMedia(request,
cPlayer.onMediaDiscovered.bind(cPlayer, 'loadMedia'),
cPlayer.onLoadMediaError.bind(cPlayer));
});
2014-04-15 20:49:49 -07:00
};
/**
* Callback function for loadMedia success
* @param {Object} mediaSession A new media object.
*/
2014-07-07 22:27:17 -04:00
CastPlayer.prototype.onMediaDiscovered = function (how, mediaSession) {
2014-04-15 20:49:49 -07:00
console.log("chromecast new media session ID:" + mediaSession.mediaSessionId + ' (' + how + ')');
this.currentMediaSession = mediaSession;
this.currentMediaTime = mediaSession.currentTime;
2014-04-15 20:49:49 -07:00
2014-07-07 22:27:17 -04:00
if (how == 'loadMedia') {
2014-04-15 20:49:49 -07:00
this.castPlayerState = PLAYER_STATE.PLAYING;
clearInterval(this.timer);
2014-07-07 22:27:17 -04:00
this.startProgressTimer();
2014-04-15 20:49:49 -07:00
}
2014-07-07 22:27:17 -04:00
if (how == 'activeSession') {
this.castPlayerState = mediaSession.playerState;
2014-04-15 20:49:49 -07:00
}
if (this.castPlayerState == PLAYER_STATE.PLAYING) {
// start progress timer
2014-07-07 22:27:17 -04:00
this.startProgressTimer();
2014-04-15 20:49:49 -07:00
}
this.currentMediaSession.addUpdateListener(this.mediaStatusUpdateHandler);
2014-07-07 22:27:17 -04:00
this.currentMediaDuration = mediaSession.media.duration * 10000000;
2014-04-15 20:49:49 -07:00
};
/**
* Callback function when media load returns error
*/
2014-07-07 22:27:17 -04:00
CastPlayer.prototype.onLoadMediaError = function (e) {
2014-04-15 20:49:49 -07:00
console.log("chromecast media error");
2014-07-07 22:27:17 -04:00
this.castPlayerState = PLAYER_STATE.IDLE;
2014-04-15 20:49:49 -07:00
};
/**
* Callback function for media status update from receiver
* @param {!Boolean} e true/false
*/
2014-07-07 22:27:17 -04:00
CastPlayer.prototype.onMediaStatusUpdate = function (e) {
if (e == false) {
2014-04-15 20:49:49 -07:00
this.currentMediaTime = 0;
2014-07-07 22:27:17 -04:00
this.castPlayerState = PLAYER_STATE.IDLE;
2014-04-15 20:49:49 -07:00
}
console.log("chromecast updating media");
2014-07-07 22:27:17 -04:00
this.updateProgressBarByTimer();
2014-04-15 20:49:49 -07:00
};
/**
* Helper function
* Increment media current position by 1 second
*/
2014-07-07 22:27:17 -04:00
CastPlayer.prototype.incrementMediaTime = function () {
if (this.castPlayerState == PLAYER_STATE.PLAYING) {
if (this.currentMediaTime < this.currentMediaDuration) {
2014-04-15 20:49:49 -07:00
this.currentMediaTime += 1;
2014-07-07 22:27:17 -04:00
this.updateProgressBarByTimer();
2014-04-15 20:49:49 -07:00
}
2014-07-07 22:27:17 -04:00
else {
2014-04-15 20:49:49 -07:00
this.currentMediaTime = 0;
2014-07-07 22:27:17 -04:00
clearInterval(this.timer);
}
}
2014-04-15 20:49:49 -07:00
};
/**
* Play media in Cast mode
*/
2014-07-07 22:27:17 -04:00
CastPlayer.prototype.playMedia = function () {
2014-04-15 20:49:49 -07:00
2014-07-07 22:27:17 -04:00
if (!this.currentMediaSession) {
return;
2014-04-15 20:49:49 -07:00
}
2014-07-07 22:27:17 -04:00
switch (this.castPlayerState) {
2014-04-15 20:49:49 -07:00
case PLAYER_STATE.LOADED:
case PLAYER_STATE.PAUSED:
this.currentMediaSession.play(null,
this.mediaCommandSuccessCallback.bind(this, "playing started for " + this.currentMediaSession.sessionId),
this.errorHandler);
this.currentMediaSession.addUpdateListener(this.mediaStatusUpdateHandler);
2014-04-15 20:49:49 -07:00
this.castPlayerState = PLAYER_STATE.PLAYING;
// start progress timer
clearInterval(this.timer);
this.startProgressTimer();
2014-04-15 20:49:49 -07:00
break;
case PLAYER_STATE.IDLE:
case PLAYER_STATE.LOADING:
case PLAYER_STATE.STOPPED:
this.loadMedia();
this.currentMediaSession.addUpdateListener(this.mediaStatusUpdateHandler);
2014-04-15 20:49:49 -07:00
this.castPlayerState = PLAYER_STATE.PLAYING;
break;
default:
2014-07-07 22:27:17 -04:00
break;
}
2014-04-15 20:49:49 -07:00
};
/**
* Pause media playback in Cast mode
*/
2014-07-07 22:27:17 -04:00
CastPlayer.prototype.pauseMedia = function () {
2014-04-15 20:49:49 -07:00
2014-07-07 22:27:17 -04:00
if (!this.currentMediaSession) {
return;
2014-04-15 20:49:49 -07:00
}
2014-07-07 22:27:17 -04:00
if (this.castPlayerState == PLAYER_STATE.PLAYING) {
2014-04-15 20:49:49 -07:00
this.castPlayerState = PLAYER_STATE.PAUSED;
this.currentMediaSession.pause(null,
this.mediaCommandSuccessCallback.bind(this, "paused " + this.currentMediaSession.sessionId),
this.errorHandler);
2014-07-07 22:27:17 -04:00
clearInterval(this.timer);
}
2014-04-15 20:49:49 -07:00
};
/**
* Stop CC playback
*/
2014-07-07 22:27:17 -04:00
CastPlayer.prototype.stopMedia = function () {
2014-04-15 20:49:49 -07:00
2014-07-07 22:27:17 -04:00
if (!this.currentMediaSession) {
return;
2014-04-15 20:49:49 -07:00
}
this.currentMediaSession.stop(null,
this.mediaCommandSuccessCallback.bind(this, "stopped " + this.currentMediaSession.sessionId),
this.errorHandler);
2014-04-15 20:49:49 -07:00
this.castPlayerState = PLAYER_STATE.STOPPED;
2014-07-07 22:27:17 -04:00
clearInterval(this.timer);
2014-04-15 20:49:49 -07:00
};
/**
* Set media volume in Cast mode
* @param {Boolean} mute A boolean
*/
2014-07-07 22:27:17 -04:00
CastPlayer.prototype.setReceiverVolume = function (mute, vol) {
2014-04-15 20:49:49 -07:00
2014-07-07 22:27:17 -04:00
if (!this.currentMediaSession) {
return;
2014-04-15 20:49:49 -07:00
}
2014-07-07 22:27:17 -04:00
if (!mute) {
2014-04-15 20:49:49 -07:00
this.currentVolume = vol || 1;
this.session.setReceiverVolumeLevel(this.currentVolume,
this.mediaCommandSuccessCallback.bind(this),
2014-07-07 22:27:17 -04:00
this.errorHandler);
2014-04-15 20:49:49 -07:00
}
2014-07-07 22:27:17 -04:00
else {
2014-04-15 20:49:49 -07:00
this.session.setReceiverMuted(true,
this.mediaCommandSuccessCallback.bind(this),
2014-07-07 22:27:17 -04:00
this.errorHandler);
}
2014-04-15 20:49:49 -07:00
};
/**
* Toggle mute CC
*/
2014-07-07 22:27:17 -04:00
CastPlayer.prototype.toggleMute = function () {
if (this.audio == true) {
this.mute();
2014-04-15 20:49:49 -07:00
}
2014-07-07 22:27:17 -04:00
else {
this.unMute();
}
2014-04-15 20:49:49 -07:00
};
/**
* Mute CC
*/
2014-07-07 22:27:17 -04:00
CastPlayer.prototype.mute = function () {
2014-04-15 20:49:49 -07:00
this.audio = false;
2014-07-07 22:27:17 -04:00
this.setReceiverVolume(true);
2014-04-15 20:49:49 -07:00
};
/**
* Unmute CC
*/
2014-07-07 22:27:17 -04:00
CastPlayer.prototype.unMute = function () {
2014-04-15 20:49:49 -07:00
this.audio = true;
2014-07-07 22:27:17 -04:00
this.setReceiverVolume(false);
2014-04-15 20:49:49 -07:00
};
/**
* media seek function in either Cast or local mode
* @param {Event} e An event object from seek
*/
2014-07-07 22:27:17 -04:00
CastPlayer.prototype.seekMedia = function (event) {
2014-04-15 20:49:49 -07:00
var pos = parseInt(event);
var curr = pos / 10000000;
2014-04-15 20:49:49 -07:00
2014-07-07 22:27:17 -04:00
if (this.castPlayerState != PLAYER_STATE.PLAYING && this.castPlayerState != PLAYER_STATE.PAUSED) {
return;
2014-04-15 20:49:49 -07:00
}
this.currentMediaTime = curr;
console.log('Seeking ' + this.currentMediaSession.sessionId + ':' +
this.currentMediaSession.mediaSessionId + ' to ' + curr);
2014-04-15 20:49:49 -07:00
var request = new chrome.cast.media.SeekRequest();
request.currentTime = this.currentMediaTime;
this.currentMediaSession.seek(request,
this.onSeekSuccess.bind(this, 'media seek done'),
this.errorHandler);
2014-07-07 22:27:17 -04:00
this.castPlayerState = PLAYER_STATE.SEEKING;
2014-04-15 20:49:49 -07:00
};
/**
* Callback function for seek success
* @param {String} info A string that describe seek event
*/
2014-07-07 22:27:17 -04:00
CastPlayer.prototype.onSeekSuccess = function (info) {
2014-04-15 20:49:49 -07:00
console.log(info);
2014-07-07 22:27:17 -04:00
this.castPlayerState = PLAYER_STATE.PLAYING;
2014-04-15 20:49:49 -07:00
};
/**
* Callback function for media command success
*/
2014-07-07 22:27:17 -04:00
CastPlayer.prototype.mediaCommandSuccessCallback = function (info, e) {
console.log(info);
2014-04-15 20:49:49 -07:00
};
/**
* Update progress bar when there is a media status update
* @param {Object} e An media status update object
*/
2014-07-07 22:27:17 -04:00
CastPlayer.prototype.updateProgressBar = function (e) {
if (e.idleReason == 'FINISHED' && e.playerState == 'IDLE') {
2014-04-15 20:49:49 -07:00
clearInterval(this.timer);
this.castPlayerState = PLAYER_STATE.STOPPED;
2014-07-07 22:27:17 -04:00
if (e.idleReason == 'FINISHED') {
$(this).trigger("/playback/complete", e);
}
2014-04-15 20:49:49 -07:00
}
2014-07-07 22:27:17 -04:00
else {
2014-04-15 20:49:49 -07:00
var p = Number(e.currentTime / this.currentMediaSession.media.duration + 1).toFixed(3);
this.progressFlag = false;
setTimeout(this.setProgressFlag.bind(this), 1000); // don't update progress in 1 second
2014-07-07 22:27:17 -04:00
}
2014-04-15 20:49:49 -07:00
};
/**
* Set progressFlag with a timeout of 1 second to avoid UI update
* until a media status update from receiver
*/
2014-07-07 22:27:17 -04:00
CastPlayer.prototype.setProgressFlag = function () {
this.progressFlag = true;
2014-04-15 20:49:49 -07:00
};
/**
* Update progress bar based on timer
*/
2014-07-07 22:27:17 -04:00
CastPlayer.prototype.updateProgressBarByTimer = function () {
if (!this.currentMediaTime) {
this.currentMediaDuration = this.session.media[0].currentTime;
}
2014-07-07 22:27:17 -04:00
if (!this.currentMediaDuration) {
this.currentMediaDuration = this.session.media[0].media.customData.runTimeTicks;
}
2014-04-15 20:49:49 -07:00
var pp = 0;
2014-07-07 22:27:17 -04:00
if (this.currentMediaDuration > 0) {
pp = Number(this.currentMediaTime / this.currentMediaDuration).toFixed(3);
2014-04-15 20:49:49 -07:00
}
if (this.progressFlag) {
// don't update progress if it's been updated on media status update event
$(this).trigger("/playback/update",
2014-07-07 22:27:17 -04:00
[{
2014-04-15 20:49:49 -07:00
positionTicks: this.currentMediaTime * 10000000,
2014-07-07 22:27:17 -04:00
runtimeTicks: this.currentMediaDuration
}]);
2014-04-15 20:49:49 -07:00
}
2014-07-07 22:27:17 -04:00
if (pp > 100 || this.castPlayerState == PLAYER_STATE.IDLE) {
2014-04-15 20:49:49 -07:00
clearInterval(this.timer);
this.deviceState = DEVICE_STATE.IDLE;
this.castPlayerState = PLAYER_STATE.IDLE;
2014-07-07 22:27:17 -04:00
$(this).trigger("/playback/complete", true);
}
2014-04-15 20:49:49 -07:00
};
/**
* @param {function} A callback function for the fucntion to start timer
*/
2014-07-07 22:27:17 -04:00
CastPlayer.prototype.startProgressTimer = function () {
if (this.timer) {
2014-04-15 20:49:49 -07:00
clearInterval(this.timer);
2014-07-07 22:27:17 -04:00
this.timer = null;
2014-04-15 20:49:49 -07:00
}
// start progress timer
2014-07-07 22:27:17 -04:00
this.timer = setInterval(this.incrementMediaTimeHandler, this.timerStep);
2014-04-15 20:49:49 -07:00
};
2014-07-07 22:27:17 -04:00
// Create Cast Player
var castPlayer = new CastPlayer();
2014-04-15 20:49:49 -07:00
2014-07-07 22:27:17 -04:00
function getCodecLimits() {
2014-04-15 20:49:49 -07:00
2014-07-07 22:27:17 -04:00
return {
2014-04-06 13:53:23 -04:00
2014-04-06 14:00:37 -04:00
maxVideoAudioChannels: 6,
maxAudioChannels: 2,
maxVideoLevel: 41,
maxWidth: 1920,
maxHeight: 1080,
2014-07-07 22:27:17 -04:00
maxSampleRate: 44100
2014-04-06 13:53:23 -04:00
2014-07-07 22:27:17 -04:00
};
2014-04-06 13:53:23 -04:00
}
function canDirectStream(mediaType, mediaSource, maxBitrate) {
// If bitrate is unknown don't direct stream
2014-07-07 22:27:17 -04:00
if (!mediaSource.Bitrate || mediaSource.Bitrate > maxBitrate) {
return false;
2014-04-06 13:53:23 -04:00
}
2014-04-06 14:00:37 -04:00
var codecLimits = getCodecLimits();
2014-07-07 22:27:17 -04:00
if (mediaType == "Audio") {
2014-04-06 13:53:23 -04:00
2014-07-07 22:27:17 -04:00
return ['mp3', 'aac'].indexOf(mediaSource.Container || '') != -1;
2014-04-06 13:53:23 -04:00
}
2014-07-07 22:27:17 -04:00
else if (mediaType == "Video") {
2014-04-06 13:53:23 -04:00
2014-07-07 22:27:17 -04:00
var videoStream = mediaSource.MediaStreams.filter(function (s) {
2014-04-06 13:53:23 -04:00
2014-07-07 22:27:17 -04:00
return s.Type == 'Video';
2014-04-06 13:53:23 -04:00
})[0];
2014-07-07 22:27:17 -04:00
if (!videoStream) {
return false;
2014-04-06 13:53:23 -04:00
}
2014-07-07 22:27:17 -04:00
if (['high', 'main', 'baseline'].indexOf((videoStream.Profile || '').toLowerCase()) == -1) {
return false;
2014-04-06 13:53:23 -04:00
}
2014-07-07 22:27:17 -04:00
if (!videoStream.Level || videoStream.Level > codecLimits.maxVideoLevel) {
return false;
2014-04-06 13:53:23 -04:00
}
2014-07-07 22:27:17 -04:00
if (!videoStream.Width || videoStream.Width > codecLimits.maxWidth) {
return false;
2014-04-06 13:53:23 -04:00
}
2014-07-07 22:27:17 -04:00
if (!videoStream.Height || videoStream.Height > codecLimits.maxHeight) {
return false;
2014-04-06 13:53:23 -04:00
}
2014-07-07 22:27:17 -04:00
return ['mp4'].indexOf(mediaSource.Container || '') != -1;
2014-04-06 13:53:23 -04:00
}
2014-07-07 22:27:17 -04:00
throw new Error('Unrecognized MediaType');
2014-04-06 13:53:23 -04:00
}
2014-07-07 22:27:17 -04:00
function canPlayAudioStreamDirect(audioStream, isVideo) {
2014-04-06 13:53:23 -04:00
var audioCodec = (audioStream.Codec || '').toLowerCase().replace('-', '');
if (audioCodec.indexOf('aac') == -1 &&
audioCodec.indexOf('mp3') == -1 &&
2014-07-07 22:27:17 -04:00
audioCodec.indexOf('mpeg') == -1) {
2014-04-06 13:53:23 -04:00
2014-07-07 22:27:17 -04:00
return false;
2014-04-06 13:53:23 -04:00
}
2014-04-06 14:00:37 -04:00
var codecLimits = getCodecLimits();
var maxChannels = isVideo ? codecLimits.maxVideoAudioChannels : codecLimits.maxAudioChannels;
2014-04-06 13:53:23 -04:00
2014-07-07 22:27:17 -04:00
if (!audioStream.Channels || audioStream.Channels > maxChannels) {
return false;
2014-04-06 13:53:23 -04:00
}
2014-07-07 22:27:17 -04:00
if (!audioStream.SampleRate || audioStream.SampleRate > codecLimits.maxSampleRate) {
return false;
2014-04-06 14:00:37 -04:00
}
2014-07-07 22:27:17 -04:00
return true;
2014-04-06 13:53:23 -04:00
}
2014-07-07 22:27:17 -04:00
function isSupportedCodec(mediaType, mediaSource) {
2014-04-06 13:53:23 -04:00
2014-07-07 22:27:17 -04:00
if (mediaType == "Audio") {
return false;
2014-04-06 13:53:23 -04:00
}
2014-07-07 22:27:17 -04:00
else if (mediaType == "Video") {
2014-04-06 13:53:23 -04:00
2014-07-07 22:27:17 -04:00
return mediaSource.MediaStreams.filter(function (m) {
2014-04-06 13:53:23 -04:00
2014-07-07 22:27:17 -04:00
return m.Type == "Video" && (m.Codec || '').toLowerCase() == 'h264';
2014-04-06 13:53:23 -04:00
2014-07-07 22:27:17 -04:00
}).length > 0;
2014-04-06 13:53:23 -04:00
}
2014-07-07 22:27:17 -04:00
throw new Error('Unrecognized MediaType');
2014-04-06 13:53:23 -04:00
}
2014-07-07 22:27:17 -04:00
function getStreamByIndex(streams, type, index) {
return streams.filter(function (s) {
2014-04-06 13:53:23 -04:00
2014-07-07 22:27:17 -04:00
return s.Type == type && s.Index == index;
2014-04-06 13:53:23 -04:00
2014-07-07 22:27:17 -04:00
})[0];
2014-04-06 13:53:23 -04:00
}
function getDefaultAudioStream(mediaStreams, user) {
// Find all audio streams
2014-07-07 22:27:17 -04:00
var audioStreams = mediaStreams.filter(function (stream) {
return stream.Type == "Audio";
2014-04-06 13:53:23 -04:00
2014-07-07 22:27:17 -04:00
}).sort(function (a, b) {
2014-04-06 13:53:23 -04:00
var av = a.IsDefault ? 0 : 1;
var bv = b.IsDefault ? 0 : 1;
2014-07-07 22:27:17 -04:00
return av - bv;
2014-04-06 13:53:23 -04:00
});
2014-07-07 22:27:17 -04:00
if (user.Configuration.AudioLanguagePreference) {
2014-04-06 13:53:23 -04:00
2014-07-07 22:27:17 -04:00
for (var i = 0, length = audioStreams.length; i < length; i++) {
2014-04-06 13:53:23 -04:00
var mediaStream = audioStreams[i];
2014-07-07 22:27:17 -04:00
if (mediaStream.Language == user.Configuration.AudioLanguagePreference) {
return mediaStream.Index;
}
2014-04-06 13:53:23 -04:00
2014-07-07 22:27:17 -04:00
}
2014-04-06 13:53:23 -04:00
}
// Just use the first audio stream
2014-07-07 22:27:17 -04:00
return audioStreams[0];
2014-04-06 13:53:23 -04:00
}
2014-07-07 22:27:17 -04:00
function getMediaSourceInfo(user, item, maxBitrate, mediaSourceId, audioStreamIndex, subtitleStreamIndex) {
2014-04-06 13:53:23 -04:00
2014-04-15 20:49:49 -07:00
var sources = item.MediaSources || [];
2014-04-06 13:53:23 -04:00
// If a specific stream was requested, filter the list
2014-07-07 22:27:17 -04:00
if (mediaSourceId) {
sources = sources.filter(function (m) {
2014-04-06 13:53:23 -04:00
2014-07-07 22:27:17 -04:00
return m.Id == mediaSourceId;
2014-04-06 13:53:23 -04:00
2014-07-07 22:27:17 -04:00
});
2014-04-06 13:53:23 -04:00
}
// Find first one that can be direct streamed
2014-07-07 22:27:17 -04:00
var source = sources.filter(function (m) {
2014-04-06 13:53:23 -04:00
2014-07-07 22:27:17 -04:00
var audioStreams = m.MediaStreams.filter(function (s) {
return s.Type == 'Audio';
2014-04-06 13:53:23 -04:00
});
var audioStream = mediaSourceId == m.Id && audioStreamIndex != null ? getStreamByIndex(audioStreams, 'Audio', audioStreamIndex) : getDefaultAudioStream(audioStreams, user);
2014-07-07 22:27:17 -04:00
if (!audioStream || !canPlayAudioStreamDirect(audioStream, item.MediaType == 'Video')) {
return false;
2014-04-06 13:53:23 -04:00
}
var subtitleStream = mediaSourceId == m.Id && subtitleStreamIndex != null ? getStreamByIndex(m.MediaStreams, 'Subtitle', subtitleStreamIndex) : null;
2014-07-07 22:27:17 -04:00
if (subtitleStream) {
return false;
2014-04-06 13:53:23 -04:00
}
2014-07-07 22:27:17 -04:00
return canDirectStream(item.MediaType, m, maxBitrate, audioStream);
2014-04-06 13:53:23 -04:00
})[0];
2014-07-07 22:27:17 -04:00
if (source) {
return {
2014-04-06 13:53:23 -04:00
mediaSource: source,
isStatic: true,
2014-07-07 22:27:17 -04:00
streamContainer: source.Container
};
2014-04-06 13:53:23 -04:00
}
// Find first one with supported codec
2014-07-07 22:27:17 -04:00
source = sources.filter(function (m) {
2014-04-06 13:53:23 -04:00
2014-07-07 22:27:17 -04:00
return isSupportedCodec(item.MediaType, m);
2014-04-06 13:53:23 -04:00
})[0];
// Default to first one
2014-07-07 22:27:17 -04:00
return {
2014-04-06 13:53:23 -04:00
mediaSource: source || sources[0],
isStatic: false,
2014-07-07 22:27:17 -04:00
streamContainer: item.MediaType == 'Audio' ? 'mp3' : 'm3u8'
};
2014-04-06 13:53:23 -04:00
}
2014-07-07 22:27:17 -04:00
function getCustomData(item, mediaSourceId, startTimeTicks) {
2014-04-06 13:53:23 -04:00
2014-07-07 22:27:17 -04:00
return {
2014-04-06 13:53:23 -04:00
serverAddress: ApiClient.serverAddress(),
itemId: item.Id,
userId: Dashboard.getCurrentUserId(),
deviceName: ApiClient.deviceName(),
//deviceId: ApiClient.deviceId(),
startTimeTicks: startTimeTicks || 0,
2014-07-07 22:27:17 -04:00
runTimeTicks: item.RunTimeTicks
};
2014-04-06 13:53:23 -04:00
}
2014-07-07 22:27:17 -04:00
function getMetadata(item) {
2014-04-06 13:53:23 -04:00
var metadata = {};
2014-07-07 22:27:17 -04:00
if (item.Type == 'Episode') {
2014-04-06 13:53:23 -04:00
metadata = new chrome.cast.media.TvShowMediaMetadata();
metadata.type = chrome.cast.media.MetadataType.TV_SHOW;
metadata.episodeTitle = item.Name;
2014-07-07 22:27:17 -04:00
if (item.PremiereDate) {
metadata.originalAirdate = parseISO8601Date(item.PremiereDate).toISOString();
2014-04-06 13:53:23 -04:00
}
metadata.seriesTitle = item.SeriesName;
2014-07-07 22:27:17 -04:00
if (item.IndexNumber != null) {
metadata.episode = metadata.episodeNumber = item.IndexNumber;
2014-04-06 13:53:23 -04:00
}
2014-07-07 22:27:17 -04:00
if (item.ParentIndexNumber != null) {
metadata.season = metadata.seasonNumber = item.ParentIndexNumber;
}
2014-04-06 13:53:23 -04:00
}
2014-07-07 22:27:17 -04:00
else if (item.Type == 'Photo') {
2014-04-06 13:53:23 -04:00
metadata = new chrome.cast.media.PhotoMediaMetadata();
metadata.type = chrome.cast.media.MetadataType.PHOTO;
2014-07-07 22:27:17 -04:00
if (item.PremiereDate) {
metadata.creationDateTime = parseISO8601Date(item.PremiereDate).toISOString();
}
2014-04-06 13:53:23 -04:00
}
2014-07-07 22:27:17 -04:00
else if (item.MediaType == 'Audio') {
2014-04-06 13:53:23 -04:00
metadata = new chrome.cast.media.MusicTrackMediaMetadata();
metadata.type = chrome.cast.media.MetadataType.MUSIC_TRACK;
2014-07-07 22:27:17 -04:00
if (item.ProductionYear) {
metadata.releaseYear = item.ProductionYear;
2014-04-06 13:53:23 -04:00
}
2014-07-07 22:27:17 -04:00
if (item.PremiereDate) {
metadata.releaseDate = parseISO8601Date(item.PremiereDate).toISOString();
2014-04-06 13:53:23 -04:00
}
metadata.songName = item.Name;
2014-04-15 20:49:49 -07:00
metadata.artist = item.Artists & item.Artists.length ? item.Artists[0] : '';
metadata.albumArtist = item.AlbumArtist;
2014-07-07 22:27:17 -04:00
if (item.IndexNumber != null) {
metadata.trackNumber = item.IndexNumber;
2014-02-23 15:35:58 -05:00
}
2014-04-15 20:49:49 -07:00
2014-07-07 22:27:17 -04:00
if (item.ParentIndexNumber != null) {
metadata.discNumber = item.ParentIndexNumber;
2014-02-23 15:35:58 -05:00
}
2014-07-07 22:27:17 -04:00
var composer = (item.People || []).filter(function (p) {
return p.PersonType == 'Type';
2014-04-15 20:49:49 -07:00
})[0];
2014-04-06 13:53:23 -04:00
2014-07-07 22:27:17 -04:00
if (composer) {
metadata.composer = composer.Name;
}
2014-02-23 15:35:58 -05:00
}
2014-07-07 22:27:17 -04:00
else if (item.MediaType == 'Movie') {
2014-04-15 20:49:49 -07:00
metadata = new chrome.cast.media.MovieMediaMetadata();
metadata.type = chrome.cast.media.MetadataType.MOVIE;
2014-02-23 15:35:58 -05:00
2014-07-07 22:27:17 -04:00
if (item.ProductionYear) {
metadata.releaseYear = item.ProductionYear;
2014-04-15 20:49:49 -07:00
}
2014-04-06 13:53:23 -04:00
2014-07-07 22:27:17 -04:00
if (item.PremiereDate) {
metadata.releaseDate = parseISO8601Date(item.PremiereDate).toISOString();
}
2014-02-23 15:35:58 -05:00
}
2014-07-07 22:27:17 -04:00
else {
2014-04-15 20:49:49 -07:00
metadata = new chrome.cast.media.GenericMediaMetadata();
metadata.type = chrome.cast.media.MetadataType.GENERIC;
2014-07-07 22:27:17 -04:00
if (item.ProductionYear) {
metadata.releaseYear = item.ProductionYear;
2014-04-15 20:49:49 -07:00
}
2014-07-07 22:27:17 -04:00
if (item.PremiereDate) {
metadata.releaseDate = parseISO8601Date(item.PremiereDate).toISOString();
}
2014-02-23 15:35:58 -05:00
}
2014-04-15 20:49:49 -07:00
metadata.title = item.Name;
2014-04-06 13:53:23 -04:00
2014-07-07 22:27:17 -04:00
if (item.Studios && item.Studios.length) {
metadata.Studio = item.Studios[0];
2014-02-23 15:35:58 -05:00
}
2014-07-07 22:27:17 -04:00
return metadata;
2014-04-15 20:49:49 -07:00
}
2014-02-23 15:35:58 -05:00
2014-07-07 22:27:17 -04:00
function getStreamUrl(item, mediaSourceInfo, startTimeTicks, maxBitrate) {
2014-04-06 13:53:23 -04:00
2014-04-15 20:49:49 -07:00
var url;
2014-02-23 15:35:58 -05:00
2014-04-15 20:49:49 -07:00
var codecLimits = getCodecLimits();
2014-02-23 15:35:58 -05:00
2014-07-07 22:27:17 -04:00
if (item.MediaType == 'Audio') {
2014-04-15 20:49:49 -07:00
url = ApiClient.serverAddress() + '/mediabrowser/audio/' + item.Id + '/stream.' + mediaSourceInfo.streamContainer + '?';
2014-07-07 22:27:17 -04:00
2014-04-15 20:49:49 -07:00
url += '&static=' + mediaSourceInfo.isStatic.toString();
url += '&maxaudiochannels=' + codecLimits.maxAudioChannels;
2014-07-07 22:27:17 -04:00
if (startTimeTicks) {
url += '&startTimeTicks=' + startTimeTicks.toString();
2014-04-15 20:49:49 -07:00
}
2014-02-23 15:35:58 -05:00
2014-07-07 22:27:17 -04:00
if (maxBitrate) {
url += '&audiobitrate=' + Math.min(maxBitrate, 320000).toString();
2014-04-15 20:49:49 -07:00
}
2014-02-23 15:35:58 -05:00
2014-04-15 20:49:49 -07:00
url += '&audiosamplerate=' + codecLimits.maxSampleRate;
url += '&mediasourceid=' + mediaSourceInfo.mediaSource.Id;
2014-07-07 22:27:17 -04:00
url += '&deviceId=' + ApiClient.deviceId();
return url;
2014-02-23 15:35:58 -05:00
}
2014-07-07 22:27:17 -04:00
else if (item.MediaType == 'Video') {
2014-02-23 15:35:58 -05:00
2014-04-15 20:49:49 -07:00
url = ApiClient.serverAddress() + '/mediabrowser/videos/' + item.Id + '/stream.' + mediaSourceInfo.streamContainer + '?';
url += 'static=' + mediaSourceInfo.isStatic.toString();
url += '&maxaudiochannels=' + codecLimits.maxVideoAudioChannels;
2014-02-23 15:35:58 -05:00
2014-07-07 22:27:17 -04:00
if (startTimeTicks) {
url += '&startTimeTicks=' + startTimeTicks.toString();
2014-04-15 20:49:49 -07:00
}
2014-02-23 15:35:58 -05:00
2014-07-07 22:27:17 -04:00
if (maxBitrate) {
2014-02-23 15:35:58 -05:00
2014-04-15 20:49:49 -07:00
var audioRate = 768000;
url += '&audiobitrate=' + audioRate.toString();
2014-07-07 22:27:17 -04:00
url += '&videobitrate=' + (maxBitrate - audioRate).toString();
2014-04-15 20:49:49 -07:00
}
2014-02-23 15:35:58 -05:00
2014-04-15 20:49:49 -07:00
url += '&profile=high';
url += '&level=' + codecLimits.maxVideoLevel;
2014-02-23 15:35:58 -05:00
2014-04-15 20:49:49 -07:00
url += '&maxwidth=' + codecLimits.maxWidth;
url += '&maxheight=' + codecLimits.maxHeight;
2014-02-23 15:35:58 -05:00
2014-04-15 20:49:49 -07:00
url += '&videoCodec=h264';
url += '&audioCodec=aac,mp3';
2014-04-09 16:58:09 -07:00
2014-04-15 20:49:49 -07:00
url += '&audiosamplerate=' + codecLimits.maxSampleRate;
url += '&mediasourceid=' + mediaSourceInfo.mediaSource.Id;
2014-07-07 22:27:17 -04:00
url += '&deviceId=' + ApiClient.deviceId();
return url;
2014-04-15 20:49:49 -07:00
}
2014-07-07 22:27:17 -04:00
throw new Error('Unrecognized MediaType');
2014-04-15 20:49:49 -07:00
}
2014-02-23 15:35:58 -05:00
2014-07-07 22:27:17 -04:00
function chromecastPlayer() {
2014-02-23 15:35:58 -05:00
var self = this;
2014-02-23 15:35:58 -05:00
2014-04-15 20:49:49 -07:00
var getItemFields = "MediaSources,Chapters";
2014-04-06 13:53:23 -04:00
self.name = PlayerName;
2014-02-23 15:35:58 -05:00
2014-04-09 16:58:09 -07:00
self.isPaused = false;
2014-04-15 21:52:35 -07:00
self.isMuted = false;
2014-04-15 20:49:49 -07:00
self.positionTicks = 0;
self.runtimeTicks = 0;
2014-07-07 22:27:17 -04:00
$(castPlayer).on("/playback/complete", function (e) {
var state = self.getPlayerStateInternal();
2014-07-07 22:27:17 -04:00
$(self).trigger("playbackstop", [state]);
2014-04-15 20:49:49 -07:00
});
2014-07-07 22:27:17 -04:00
$(castPlayer).on("/playback/update", function (e, data) {
2014-04-18 00:56:38 -07:00
2014-04-15 20:49:49 -07:00
self.positionTicks = data.positionTicks;
self.runtimeTicks = data.runtimeTicks;
2014-04-06 13:53:23 -04:00
2014-04-15 20:49:49 -07:00
var state = self.getPlayerStateInternal();
2014-04-09 16:58:09 -07:00
2014-07-07 22:27:17 -04:00
$(self).trigger("positionchange", [state]);
2014-04-15 20:49:49 -07:00
});
2014-04-09 16:58:09 -07:00
2014-07-07 22:27:17 -04:00
self.play = function (options) {
castPlayer.loadMedia(Dashboard.getCurrentUserId(), options, 'PlayNow');
};
2014-03-30 16:26:16 -07:00
2014-07-07 22:27:17 -04:00
self.unpause = function () {
2014-04-09 16:58:09 -07:00
self.isPaused = !self.isPaused;
2014-07-07 22:27:17 -04:00
castPlayer.playMedia();
2014-04-09 16:58:09 -07:00
};
2014-07-07 22:27:17 -04:00
self.pause = function () {
2014-04-09 16:58:09 -07:00
self.isPaused = true;
2014-07-07 22:27:17 -04:00
castPlayer.pauseMedia();
2014-04-09 16:58:09 -07:00
};
2014-07-07 22:27:17 -04:00
self.shuffle = function (id) {
var userId = Dashboard.getCurrentUserId();
2014-07-07 22:27:17 -04:00
ApiClient.getItem(userId, id).done(function (item) {
var query = {
UserId: userId,
Fields: getItemFields,
Limit: 50,
Filters: "IsNotFolder",
Recursive: true,
2014-07-07 22:27:17 -04:00
SortBy: "Random"
};
2014-07-07 22:27:17 -04:00
if (item.IsFolder) {
query.ParentId = id;
}
2014-07-07 22:27:17 -04:00
else if (item.Type == "MusicArtist") {
query.MediaTypes = "Audio";
2014-07-07 22:27:17 -04:00
query.Artists = item.Name;
}
2014-07-07 22:27:17 -04:00
else if (item.Type == "MusicGenre") {
query.MediaTypes = "Audio";
2014-07-07 22:27:17 -04:00
query.Genres = item.Name;
} else {
return;
}
2014-07-07 22:27:17 -04:00
self.getItemsForPlayback(query).done(function (result) {
self.play({ items: result.Items });
});
});
};
2014-02-23 12:29:02 -05:00
2014-07-07 22:27:17 -04:00
self.instantMix = function (id) {
var userId = Dashboard.getCurrentUserId();
2014-07-07 22:27:17 -04:00
ApiClient.getItem(userId, id).done(function (item) {
var promise;
var mixLimit = 3;
2014-07-07 22:27:17 -04:00
if (item.Type == "MusicArtist") {
promise = ApiClient.getInstantMixFromArtist(name, {
UserId: userId,
Fields: getItemFields,
2014-07-07 22:27:17 -04:00
Limit: mixLimit
});
}
2014-07-07 22:27:17 -04:00
else if (item.Type == "MusicGenre") {
promise = ApiClient.getInstantMixFromMusicGenre(name, {
UserId: userId,
Fields: getItemFields,
2014-07-07 22:27:17 -04:00
Limit: mixLimit
});
}
2014-07-07 22:27:17 -04:00
else if (item.Type == "MusicAlbum") {
promise = ApiClient.getInstantMixFromAlbum(id, {
UserId: userId,
Fields: getItemFields,
2014-07-07 22:27:17 -04:00
Limit: mixLimit
});
}
2014-07-07 22:27:17 -04:00
else if (item.Type == "Audio") {
promise = ApiClient.getInstantMixFromSong(id, {
UserId: userId,
Fields: getItemFields,
2014-07-07 22:27:17 -04:00
Limit: mixLimit
});
}
2014-07-07 22:27:17 -04:00
else {
return;
}
2014-07-07 22:27:17 -04:00
promise.done(function (result) {
self.play({ items: result.Items });
});
});
};
2014-02-23 00:52:30 -05:00
2014-07-07 22:27:17 -04:00
self.canQueueMediaType = function (mediaType) {
return mediaType == "Audio";
};
2014-07-07 22:27:17 -04:00
self.queue = function (options) {
castPlayer.loadMedia(Dashboard.getCurrentUserId(), options, 'PlayLast');
};
2014-07-07 22:27:17 -04:00
self.queueNext = function (options) {
castPlayer.loadMedia(Dashboard.getCurrentUserId(), options, 'PlayNext');
};
2014-07-07 22:27:17 -04:00
self.stop = function () {
castPlayer.stopMedia();
};
2014-07-07 22:27:17 -04:00
self.displayContent = function (options) {
2014-04-13 13:27:13 -04:00
};
2014-07-07 22:27:17 -04:00
self.mute = function () {
2014-04-15 20:49:49 -07:00
self.isMuted = true;
2014-07-07 22:27:17 -04:00
castPlayer.mute();
};
2014-07-07 22:27:17 -04:00
self.unMute = function () {
2014-04-15 20:49:49 -07:00
self.isMuted = false;
2014-07-07 22:27:17 -04:00
castPlayer.unMute();
};
2014-07-07 22:27:17 -04:00
self.toggleMute = function () {
castPlayer.toggleMute();
};
2014-07-07 22:27:17 -04:00
self.getTargets = function () {
var targets = [];
2014-04-13 13:27:13 -04:00
2014-07-07 22:27:17 -04:00
if (castPlayer.hasReceivers) {
targets.push(self.getCurrentTargetInfo());
2014-04-13 13:27:13 -04:00
}
2014-07-07 22:27:17 -04:00
return targets;
};
2014-07-07 22:27:17 -04:00
self.getCurrentTargetInfo = function () {
2014-04-06 13:53:23 -04:00
var appName = null;
2014-07-07 22:27:17 -04:00
if (castPlayer.session && castPlayer.session.receiver && castPlayer.session.receiver.friendlyName) {
appName = castPlayer.session.receiver.friendlyName;
2014-04-06 13:53:23 -04:00
}
2014-02-23 12:53:25 -05:00
2014-07-07 22:27:17 -04:00
return {
2014-04-06 13:53:23 -04:00
name: PlayerName,
id: PlayerName,
playerName: self.name, // TODO: PlayerName == self.name, so do we need to use either/or?
2014-04-06 13:53:23 -04:00
playableMediaTypes: ["Audio", "Video"],
isLocalPlayer: false,
2014-04-13 13:27:13 -04:00
appName: appName,
2014-04-15 20:49:49 -07:00
supportedCommands: ["VolumeUp",
"VolumeDown",
"Mute",
"Unmute",
"ToggleMute",
"SetVolume",
2014-07-07 22:27:17 -04:00
"DisplayContent"]
};
2014-04-06 13:53:23 -04:00
};
2014-04-09 16:58:09 -07:00
2014-07-07 22:27:17 -04:00
self.seek = function (position) {
castPlayer.seekMedia(position);
};
2014-07-07 22:27:17 -04:00
self.nextTrack = function () {
};
2014-07-07 22:27:17 -04:00
self.previousTrack = function () {
};
self.beginPlayerUpdates = function () {
// Setup polling here
};
self.endPlayerUpdates = function () {
// Stop polling here
};
2014-07-07 22:27:17 -04:00
self.volumeDown = function () {
2014-04-15 20:49:49 -07:00
var vol = castPlayer.volumeLevel - 0.02;
2014-07-07 22:27:17 -04:00
castPlayer.setReceiverVolume(false, vol / 100);
};
2014-07-07 22:27:17 -04:00
self.volumeUp = function () {
2014-04-15 20:49:49 -07:00
var vol = castPlayer.volumeLevel + 0.02;
2014-07-07 22:27:17 -04:00
castPlayer.setReceiverVolume(false, vol / 100);
2014-04-15 20:49:49 -07:00
};
2014-07-07 22:27:17 -04:00
self.setVolume = function (vol) {
castPlayer.setReceiverVolume(false, vol / 100);
2014-04-09 16:58:09 -07:00
};
2014-07-07 22:27:17 -04:00
self.getPlayerState = function () {
var deferred = $.Deferred();
var result = self.getPlayerStateInternal();
deferred.resolveWith(null, [result]);
2014-07-07 22:27:17 -04:00
return deferred.promise();
};
2014-07-07 22:27:17 -04:00
self.getPlayerStateInternal = function () {
2014-07-07 22:27:17 -04:00
var state = {
PlayState: {
CanSeek: self.runtimeTicks && self.positionTicks > 0,
2014-04-22 13:25:54 -04:00
PositionTicks: self.positionTicks,
VolumeLevel: castPlayer.currentVolume * 100,
IsPaused: self.isPaused,
IsMuted: self.isMuted
2014-04-22 13:25:54 -04:00
// TODO: Implement
// AudioStreamIndex: null,
// SubtitleStreamIndex: null,
// PlayMethod: 'DirectStream' or 'Transcode'
2014-07-07 22:27:17 -04:00
}
2014-04-15 20:49:49 -07:00
};
2014-04-22 13:25:54 -04:00
// TODO: Implement
var isPlaying = false;
2014-04-22 13:25:54 -04:00
if (isPlaying) {
//state.PlayState.MediaSourceId = 'xxx';
2014-07-07 22:27:17 -04:00
state.NowPlayingItem = {
2014-04-22 13:31:06 -04:00
RunTimeTicks: self.runtimeTicks,
2014-07-07 22:27:17 -04:00
Name: 'Chromecast'
2014-04-22 13:25:54 -04:00
};
var nowPlayingItem = state.NowPlayingItem;
2014-04-22 13:25:54 -04:00
// TODO: Fill in these properties using chromecast mediainfo and/or custom data
//nowPlayingItem.Id = item.Id;
//nowPlayingItem.MediaType = item.MediaType;
//nowPlayingItem.Type = item.Type;
//nowPlayingItem.Name = item.Name;
//nowPlayingItem.IndexNumber = item.IndexNumber;
//nowPlayingItem.IndexNumberEnd = item.IndexNumberEnd;
//nowPlayingItem.ParentIndexNumber = item.ParentIndexNumber;
//nowPlayingItem.ProductionYear = item.ProductionYear;
//nowPlayingItem.PremiereDate = item.PremiereDate;
//nowPlayingItem.SeriesName = item.SeriesName;
//nowPlayingItem.Album = item.Album;
//nowPlayingItem.Artists = item.Artists;
}
2014-07-07 22:27:17 -04:00
return state;
};
2014-04-06 13:53:23 -04:00
}
2014-02-23 15:35:58 -05:00
2014-06-22 01:52:31 -04:00
//MediaController.registerPlayer(new chromecastPlayer());
2014-02-23 00:52:30 -05:00
2014-07-07 22:27:17 -04:00
//$(MediaController).on('playerchange', function () {
2014-04-06 13:53:23 -04:00
2014-07-07 22:27:17 -04:00
// if (MediaController.getPlayerInfo().name == PlayerName) {
// if (castPlayer.deviceState != DEVICE_STATE.ACTIVE && castPlayer.isInitialized) {
// castPlayer.launchApp();
// }
// }
//});
2014-02-23 00:52:30 -05:00
})(window, window.chrome, console);