2014-02-22 22:52:30 -07:00
|
|
|
|
(function (window, chrome, console) {
|
|
|
|
|
|
|
|
|
|
// Based on https://github.com/googlecast/CastVideos-chrome/blob/master/CastVideos.js
|
2014-02-23 10:53:25 -07:00
|
|
|
|
|
2014-02-22 22:52:30 -07:00
|
|
|
|
/**
|
|
|
|
|
* Constants of states for Chromecast device
|
|
|
|
|
**/
|
|
|
|
|
var DEVICE_STATE = {
|
|
|
|
|
'IDLE': 0,
|
|
|
|
|
'ACTIVE': 1,
|
|
|
|
|
'WARNING': 2,
|
|
|
|
|
'ERROR': 3,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constants of states for CastPlayer
|
|
|
|
|
**/
|
|
|
|
|
var PLAYER_STATE = {
|
|
|
|
|
'IDLE': 'IDLE',
|
|
|
|
|
'LOADING': 'LOADING',
|
|
|
|
|
'LOADED': 'LOADED',
|
|
|
|
|
'PLAYING': 'PLAYING',
|
|
|
|
|
'PAUSED': 'PAUSED',
|
|
|
|
|
'STOPPED': 'STOPPED',
|
|
|
|
|
'SEEKING': 'SEEKING',
|
|
|
|
|
'ERROR': 'ERROR'
|
|
|
|
|
};
|
|
|
|
|
|
2014-04-06 10:53:23 -07:00
|
|
|
|
var PlayerName = 'Chromecast';
|
|
|
|
|
|
2014-02-22 22:52:30 -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 {Number} volume
|
|
|
|
|
this.currentVolume = 0.5;
|
2014-04-06 10:53:23 -07:00
|
|
|
|
|
2014-02-22 22:52:30 -07: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 20:27:13 -07:00
|
|
|
|
this.hasReceivers = false;
|
|
|
|
|
|
2014-02-22 22:52:30 -07:00
|
|
|
|
this.initializeCastPlayer();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
CastPlayer.prototype.initializeCastPlayer = function () {
|
|
|
|
|
|
2014-02-23 10:29:02 -07:00
|
|
|
|
if (!chrome) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-02-23 10:53:25 -07:00
|
|
|
|
|
2014-02-22 22:52:30 -07:00
|
|
|
|
if (!chrome.cast || !chrome.cast.isAvailable) {
|
2014-03-30 16:26:16 -07:00
|
|
|
|
|
2014-02-23 10:29:02 -07:00
|
|
|
|
setTimeout(this.initializeCastPlayer.bind(this), 1000);
|
2014-02-22 22:52:30 -07:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-06 10:53:23 -07:00
|
|
|
|
// v1 Id AE4DA10A
|
|
|
|
|
// v2 Id 472F0435
|
2014-04-07 09:26:48 -07:00
|
|
|
|
var applicationID = '472F0435';
|
2014-02-22 22:52:30 -07: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 10:53:23 -07:00
|
|
|
|
console.log('chromecast.initialize');
|
2014-02-23 10:53:25 -07:00
|
|
|
|
|
2014-02-22 22:52:30 -07:00
|
|
|
|
chrome.cast.initialize(apiConfig, this.onInitSuccess.bind(this), this.onError.bind(this));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Callback function for init success
|
|
|
|
|
*/
|
|
|
|
|
CastPlayer.prototype.onInitSuccess = function () {
|
2014-04-06 10:53:23 -07:00
|
|
|
|
this.isInitialized = true;
|
|
|
|
|
console.log("chromecast init success");
|
2014-02-22 22:52:30 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generic error callback function
|
|
|
|
|
*/
|
|
|
|
|
CastPlayer.prototype.onError = function () {
|
2014-03-29 12:23:47 -07:00
|
|
|
|
console.log("chromecast error");
|
2014-02-22 22:52:30 -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-02-23 10:53:25 -07:00
|
|
|
|
CastPlayer.prototype.sessionListener = function (e) {
|
2014-02-22 22:52:30 -07:00
|
|
|
|
this.session = e;
|
|
|
|
|
if (this.session) {
|
|
|
|
|
this.deviceState = DEVICE_STATE.ACTIVE;
|
2014-04-06 10:53:23 -07:00
|
|
|
|
MediaController.setActivePlayer(PlayerName);
|
2014-02-22 22:52:30 -07:00
|
|
|
|
if (this.session.media[0]) {
|
|
|
|
|
this.onMediaDiscovered('activeSession', this.session.media[0]);
|
|
|
|
|
}
|
2014-04-06 10:53:23 -07:00
|
|
|
|
|
2014-03-30 16:26:16 -07:00
|
|
|
|
this.session.addUpdateListener(this.sessionUpdateListener.bind(this));
|
2014-02-22 22:52:30 -07:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param {string} e Receiver availability
|
|
|
|
|
* This indicates availability of receivers but
|
|
|
|
|
* does not provide a list of device IDs
|
|
|
|
|
*/
|
|
|
|
|
CastPlayer.prototype.receiverListener = function (e) {
|
2014-03-30 16:26:16 -07:00
|
|
|
|
|
2014-02-22 22:52:30 -07:00
|
|
|
|
if (e === 'available') {
|
2014-03-29 12:23:47 -07:00
|
|
|
|
console.log("chromecast receiver found");
|
2014-02-23 20:27:13 -07:00
|
|
|
|
this.hasReceivers = true;
|
2014-02-22 22:52:30 -07:00
|
|
|
|
}
|
|
|
|
|
else {
|
2014-03-29 12:23:47 -07:00
|
|
|
|
console.log("chromecast receiver list empty");
|
2014-02-23 20:27:13 -07:00
|
|
|
|
this.hasReceivers = false;
|
2014-02-22 22:52:30 -07:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2014-03-30 16:26:16 -07:00
|
|
|
|
/**
|
|
|
|
|
* session update listener
|
|
|
|
|
*/
|
|
|
|
|
CastPlayer.prototype.sessionUpdateListener = function (isAlive) {
|
|
|
|
|
if (!isAlive) {
|
|
|
|
|
this.session = null;
|
|
|
|
|
this.deviceState = DEVICE_STATE.IDLE;
|
|
|
|
|
this.castPlayerState = PLAYER_STATE.IDLE;
|
|
|
|
|
this.currentMediaSession = null;
|
|
|
|
|
clearInterval(this.timer);
|
|
|
|
|
|
2014-04-06 10:53:23 -07:00
|
|
|
|
MediaController.removeActivePlayer(PlayerName);
|
2014-03-30 16:26:16 -07:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2014-02-22 22:52:30 -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.
|
|
|
|
|
*/
|
|
|
|
|
CastPlayer.prototype.launchApp = function () {
|
2014-04-06 10:53:23 -07:00
|
|
|
|
console.log("chromecast launching app...");
|
2014-02-22 22:52:30 -07:00
|
|
|
|
chrome.cast.requestSession(this.onRequestSessionSuccess.bind(this), this.onLaunchError.bind(this));
|
|
|
|
|
if (this.timer) {
|
|
|
|
|
clearInterval(this.timer);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Callback function for request session success
|
|
|
|
|
* @param {Object} e A chrome.cast.Session object
|
|
|
|
|
*/
|
|
|
|
|
CastPlayer.prototype.onRequestSessionSuccess = function (e) {
|
2014-04-06 10:53:23 -07:00
|
|
|
|
console.log("chromecast session success: " + e.sessionId);
|
2014-02-22 22:52:30 -07:00
|
|
|
|
this.session = e;
|
|
|
|
|
this.deviceState = DEVICE_STATE.ACTIVE;
|
2014-03-30 16:26:16 -07:00
|
|
|
|
this.session.addUpdateListener(this.sessionUpdateListener.bind(this));
|
2014-02-22 22:52:30 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Callback function for launch error
|
|
|
|
|
*/
|
|
|
|
|
CastPlayer.prototype.onLaunchError = function () {
|
2014-04-06 10:53:23 -07:00
|
|
|
|
console.log("chromecast launch error");
|
2014-02-22 22:52:30 -07:00
|
|
|
|
this.deviceState = DEVICE_STATE.ERROR;
|
2014-02-23 13:35:58 -07:00
|
|
|
|
Dashboard.alert({
|
|
|
|
|
|
|
|
|
|
title: "Error Launching Chromecast",
|
2014-02-23 20:27:13 -07:00
|
|
|
|
message: "There was an error launching chromecast. Please ensure your device is connected to your wifi network."
|
2014-02-23 13:35:58 -07:00
|
|
|
|
|
|
|
|
|
});
|
2014-04-05 10:01:04 -07:00
|
|
|
|
|
2014-04-06 10:53:23 -07:00
|
|
|
|
MediaController.removeActivePlayer(PlayerName);
|
2014-02-22 22:52:30 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Stops the running receiver application associated with the session.
|
|
|
|
|
*/
|
|
|
|
|
CastPlayer.prototype.stopApp = function () {
|
|
|
|
|
this.session.stop(this.onStopAppSuccess.bind(this, 'Session stopped'),
|
|
|
|
|
this.onError.bind(this));
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Callback function for stop app success
|
|
|
|
|
*/
|
|
|
|
|
CastPlayer.prototype.onStopAppSuccess = function (message) {
|
|
|
|
|
console.log(message);
|
|
|
|
|
this.deviceState = DEVICE_STATE.IDLE;
|
|
|
|
|
this.castPlayerState = PLAYER_STATE.IDLE;
|
|
|
|
|
this.currentMediaSession = null;
|
|
|
|
|
clearInterval(this.timer);
|
|
|
|
|
};
|
|
|
|
|
|
2014-04-06 11:00:37 -07:00
|
|
|
|
function getCodecLimits() {
|
|
|
|
|
return {
|
2014-04-06 10:53:23 -07:00
|
|
|
|
|
2014-04-06 11:00:37 -07:00
|
|
|
|
maxVideoAudioChannels: 6,
|
|
|
|
|
maxAudioChannels: 2,
|
|
|
|
|
maxVideoLevel: 41,
|
|
|
|
|
maxWidth: 1920,
|
|
|
|
|
maxHeight: 1080,
|
|
|
|
|
maxSampleRate: 44100
|
2014-04-06 10:53:23 -07:00
|
|
|
|
|
2014-04-06 11:00:37 -07:00
|
|
|
|
};
|
2014-04-06 10:53:23 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function canDirectStream(mediaType, mediaSource, maxBitrate) {
|
|
|
|
|
|
|
|
|
|
// If bitrate is unknown don't direct stream
|
|
|
|
|
if (!mediaSource.Bitrate || mediaSource.Bitrate > maxBitrate) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-06 11:00:37 -07:00
|
|
|
|
var codecLimits = getCodecLimits();
|
|
|
|
|
|
2014-04-06 10:53:23 -07:00
|
|
|
|
if (mediaType == "Audio") {
|
|
|
|
|
|
|
|
|
|
return ['mp3', 'aac'].indexOf(mediaSource.Container || '') != -1;
|
|
|
|
|
}
|
|
|
|
|
else if (mediaType == "Video") {
|
|
|
|
|
|
|
|
|
|
var videoStream = mediaSource.MediaStreams.filter(function (s) {
|
|
|
|
|
|
|
|
|
|
return s.Type == 'Video';
|
|
|
|
|
|
|
|
|
|
})[0];
|
|
|
|
|
|
|
|
|
|
if (!videoStream) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (['high', 'main', 'baseline'].indexOf((videoStream.Profile || '').toLowerCase()) == -1) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-06 11:00:37 -07:00
|
|
|
|
if (!videoStream.Level || videoStream.Level > codecLimits.maxVideoLevel) {
|
2014-04-06 10:53:23 -07:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-06 11:00:37 -07:00
|
|
|
|
if (!videoStream.Width || videoStream.Width > codecLimits.maxWidth) {
|
2014-04-06 10:53:23 -07:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-06 11:00:37 -07:00
|
|
|
|
if (!videoStream.Height || videoStream.Height > codecLimits.maxHeight) {
|
2014-04-06 10:53:23 -07:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ['mp4'].indexOf(mediaSource.Container || '') != -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new Error('Unrecognized MediaType');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function canPlayAudioStreamDirect(audioStream, isVideo) {
|
|
|
|
|
|
|
|
|
|
var audioCodec = (audioStream.Codec || '').toLowerCase().replace('-', '');
|
|
|
|
|
|
|
|
|
|
if (audioCodec.indexOf('aac') == -1 &&
|
|
|
|
|
audioCodec.indexOf('mp3') == -1 &&
|
|
|
|
|
audioCodec.indexOf('mpeg') == -1) {
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-06 11:00:37 -07:00
|
|
|
|
var codecLimits = getCodecLimits();
|
|
|
|
|
|
|
|
|
|
var maxChannels = isVideo ? codecLimits.maxVideoAudioChannels : codecLimits.maxAudioChannels;
|
2014-04-06 10:53:23 -07:00
|
|
|
|
|
|
|
|
|
if (!audioStream.Channels || audioStream.Channels > maxChannels) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-06 11:00:37 -07:00
|
|
|
|
if (!audioStream.SampleRate || audioStream.SampleRate > codecLimits.maxSampleRate) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-06 10:53:23 -07:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isSupportedCodec(mediaType, mediaSource) {
|
|
|
|
|
|
|
|
|
|
if (mediaType == "Audio") {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else if (mediaType == "Video") {
|
|
|
|
|
|
|
|
|
|
return mediaSource.MediaStreams.filter(function (m) {
|
|
|
|
|
|
|
|
|
|
return m.Type == "Video" && (m.Codec || '').toLowerCase() == 'h264';
|
|
|
|
|
|
|
|
|
|
}).length > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new Error('Unrecognized MediaType');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getStreamByIndex(streams, type, index) {
|
|
|
|
|
return streams.filter(function (s) {
|
|
|
|
|
|
|
|
|
|
return s.Type == type && s.Index == index;
|
|
|
|
|
|
|
|
|
|
})[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getDefaultAudioStream(mediaStreams, user) {
|
|
|
|
|
|
|
|
|
|
// Find all audio streams
|
|
|
|
|
var audioStreams = mediaStreams.filter(function (stream) {
|
|
|
|
|
return stream.Type == "Audio";
|
|
|
|
|
|
|
|
|
|
}).sort(function (a, b) {
|
|
|
|
|
|
|
|
|
|
var av = a.IsDefault ? 0 : 1;
|
|
|
|
|
var bv = b.IsDefault ? 0 : 1;
|
|
|
|
|
|
|
|
|
|
return av - bv;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (user.Configuration.AudioLanguagePreference) {
|
|
|
|
|
|
|
|
|
|
for (var i = 0, length = audioStreams.length; i < length; i++) {
|
|
|
|
|
var mediaStream = audioStreams[i];
|
|
|
|
|
|
|
|
|
|
if (mediaStream.Language == user.Configuration.AudioLanguagePreference) {
|
|
|
|
|
return mediaStream.Index;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Just use the first audio stream
|
|
|
|
|
return audioStreams[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getMediaSourceInfo(user, item, maxBitrate, mediaSourceId, audioStreamIndex, subtitleStreamIndex) {
|
|
|
|
|
|
|
|
|
|
var sources = item.MediaSources;
|
|
|
|
|
|
|
|
|
|
// If a specific stream was requested, filter the list
|
|
|
|
|
if (mediaSourceId) {
|
|
|
|
|
sources = sources.filter(function (m) {
|
|
|
|
|
|
|
|
|
|
return m.Id == mediaSourceId;
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find first one that can be direct streamed
|
|
|
|
|
var source = sources.filter(function (m) {
|
|
|
|
|
|
|
|
|
|
var audioStreams = m.MediaStreams.filter(function (s) {
|
|
|
|
|
return s.Type == 'Audio';
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var audioStream = mediaSourceId == m.Id && audioStreamIndex != null ? getStreamByIndex(audioStreams, 'Audio', audioStreamIndex) : getDefaultAudioStream(audioStreams, user);
|
|
|
|
|
|
|
|
|
|
if (!audioStream || !canPlayAudioStreamDirect(audioStream, item.MediaType == 'Video')) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var subtitleStream = mediaSourceId == m.Id && subtitleStreamIndex != null ? getStreamByIndex(m.MediaStreams, 'Subtitle', subtitleStreamIndex) : null;
|
|
|
|
|
|
|
|
|
|
if (subtitleStream) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return canDirectStream(item.MediaType, m, maxBitrate, audioStream);
|
|
|
|
|
|
|
|
|
|
})[0];
|
|
|
|
|
|
|
|
|
|
if (source) {
|
|
|
|
|
return {
|
|
|
|
|
mediaSource: source,
|
|
|
|
|
isStatic: true,
|
|
|
|
|
streamContainer: source.Container
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find first one with supported codec
|
|
|
|
|
source = sources.filter(function (m) {
|
|
|
|
|
|
|
|
|
|
return isSupportedCodec(item.MediaType, m);
|
|
|
|
|
|
|
|
|
|
})[0];
|
|
|
|
|
|
|
|
|
|
// Default to first one
|
|
|
|
|
return {
|
|
|
|
|
mediaSource: source || sources[0],
|
|
|
|
|
isStatic: false,
|
|
|
|
|
streamContainer: item.MediaType == 'Audio' ? 'mp3' : 'm3u8'
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-07 09:26:48 -07:00
|
|
|
|
function getCustomData(item, mediaSourceId, startTimeTicks, audioStreamIndex, subtitleStreamIndex) {
|
2014-04-06 10:53:23 -07:00
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
|
|
serverAddress: ApiClient.serverAddress(),
|
|
|
|
|
itemId: item.Id,
|
|
|
|
|
userId: Dashboard.getCurrentUserId(),
|
|
|
|
|
deviceName: ApiClient.deviceName(),
|
|
|
|
|
//deviceId: ApiClient.deviceId(),
|
2014-04-07 09:26:48 -07:00
|
|
|
|
startTimeTicks: startTimeTicks || 0,
|
|
|
|
|
audioStreamIndex: audioStreamIndex,
|
|
|
|
|
subtitleStreamIndex: subtitleStreamIndex
|
2014-04-06 10:53:23 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getMetadata(item) {
|
|
|
|
|
|
|
|
|
|
var metadata = {};
|
|
|
|
|
|
|
|
|
|
if (item.Type == 'Episode') {
|
|
|
|
|
metadata = new chrome.cast.media.TvShowMediaMetadata();
|
|
|
|
|
metadata.type = chrome.cast.media.MetadataType.TV_SHOW;
|
|
|
|
|
|
|
|
|
|
metadata.episodeTitle = item.Name;
|
|
|
|
|
|
|
|
|
|
if (item.PremiereDate) {
|
|
|
|
|
metadata.originalAirdate = parseISO8601Date(item.PremiereDate).toISOString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
metadata.seriesTitle = item.SeriesName;
|
|
|
|
|
|
|
|
|
|
if (item.IndexNumber != null) {
|
|
|
|
|
metadata.episode = metadata.episodeNumber = item.IndexNumber;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (item.ParentIndexNumber != null) {
|
|
|
|
|
metadata.season = metadata.seasonNumber = item.ParentIndexNumber;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (item.Type == 'Photo') {
|
|
|
|
|
metadata = new chrome.cast.media.PhotoMediaMetadata();
|
|
|
|
|
metadata.type = chrome.cast.media.MetadataType.PHOTO;
|
|
|
|
|
|
|
|
|
|
if (item.PremiereDate) {
|
|
|
|
|
metadata.creationDateTime = parseISO8601Date(item.PremiereDate).toISOString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (item.MediaType == 'Audio') {
|
|
|
|
|
metadata = new chrome.cast.media.MusicTrackMediaMetadata();
|
|
|
|
|
metadata.type = chrome.cast.media.MetadataType.MUSIC_TRACK;
|
|
|
|
|
|
|
|
|
|
if (item.ProductionYear) {
|
|
|
|
|
metadata.releaseYear = item.ProductionYear;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (item.PremiereDate) {
|
|
|
|
|
metadata.releaseDate = parseISO8601Date(item.PremiereDate).toISOString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
metadata.songName = item.Name;
|
|
|
|
|
metadata.artist = item.Artists & item.Artists.length ? item.Artists[0] : '';
|
|
|
|
|
metadata.albumArtist = item.AlbumArtist;
|
|
|
|
|
|
|
|
|
|
if (item.IndexNumber != null) {
|
|
|
|
|
metadata.trackNumber = item.IndexNumber;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (item.ParentIndexNumber != null) {
|
|
|
|
|
metadata.discNumber = item.ParentIndexNumber;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var composer = (item.People || []).filter(function (p) {
|
|
|
|
|
return p.PersonType == 'Type';
|
|
|
|
|
})[0];
|
|
|
|
|
|
|
|
|
|
if (composer) {
|
|
|
|
|
metadata.composer = composer.Name;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (item.MediaType == 'Movie') {
|
|
|
|
|
metadata = new chrome.cast.media.MovieMediaMetadata();
|
|
|
|
|
metadata.type = chrome.cast.media.MetadataType.MUSIC_TRACK;
|
|
|
|
|
|
|
|
|
|
if (item.ProductionYear) {
|
|
|
|
|
metadata.releaseYear = item.ProductionYear;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (item.PremiereDate) {
|
|
|
|
|
metadata.releaseDate = parseISO8601Date(item.PremiereDate).toISOString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
metadata = new chrome.cast.media.GenericMediaMetadata();
|
|
|
|
|
metadata.type = chrome.cast.media.MetadataType.GENERIC;
|
|
|
|
|
|
|
|
|
|
if (item.ProductionYear) {
|
|
|
|
|
metadata.releaseYear = item.ProductionYear;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (item.PremiereDate) {
|
|
|
|
|
metadata.releaseDate = parseISO8601Date(item.PremiereDate).toISOString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
metadata.title = item.Name;
|
|
|
|
|
|
|
|
|
|
if (item.Studios && item.Studios.length) {
|
|
|
|
|
metadata.Studio = item.Studios[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return metadata;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getStreamUrl(item, mediaSourceInfo, startTimeTicks, maxBitrate) {
|
|
|
|
|
|
|
|
|
|
var url;
|
|
|
|
|
|
2014-04-06 11:00:37 -07:00
|
|
|
|
var codecLimits = getCodecLimits();
|
|
|
|
|
|
2014-04-06 10:53:23 -07:00
|
|
|
|
if (item.MediaType == 'Audio') {
|
|
|
|
|
|
|
|
|
|
url = ApiClient.serverAddress() + '/mediabrowser/audio/' + item.Id + '/stream.' + mediaSourceInfo.streamContainer + '?';
|
|
|
|
|
url += '&static=' + mediaSourceInfo.isStatic.toString();
|
2014-04-06 11:00:37 -07:00
|
|
|
|
url += '&maxaudiochannels=' + codecLimits.maxAudioChannels;
|
2014-04-06 10:53:23 -07:00
|
|
|
|
|
|
|
|
|
if (startTimeTicks) {
|
|
|
|
|
url += '&startTimeTicks=' + startTimeTicks.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (maxBitrate) {
|
|
|
|
|
url += '&audiobitrate=' + Math.min(maxBitrate, 320000).toString();
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-06 11:00:37 -07:00
|
|
|
|
url += '&audiosamplerate=' + codecLimits.maxSampleRate;
|
2014-04-06 10:53:23 -07:00
|
|
|
|
url += '&mediasourceid=' + mediaSourceInfo.mediaSource.Id;
|
|
|
|
|
|
|
|
|
|
return url;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else if (item.MediaType == 'Video') {
|
|
|
|
|
|
|
|
|
|
url = ApiClient.serverAddress() + '/mediabrowser/videos/' + item.Id + '/stream.' + mediaSourceInfo.streamContainer + '?';
|
|
|
|
|
url += 'static=' + mediaSourceInfo.isStatic.toString();
|
2014-04-06 11:00:37 -07:00
|
|
|
|
url += '&maxaudiochannels=' + codecLimits.maxVideoAudioChannels;
|
2014-04-06 10:53:23 -07:00
|
|
|
|
|
|
|
|
|
if (startTimeTicks) {
|
|
|
|
|
url += '&startTimeTicks=' + startTimeTicks.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (maxBitrate) {
|
|
|
|
|
|
|
|
|
|
var audioRate = 768000;
|
|
|
|
|
url += '&audiobitrate=' + audioRate.toString();
|
|
|
|
|
url += '&videobitrate=' + (maxBitrate - audioRate).toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
url += '&profile=high';
|
2014-04-06 11:00:37 -07:00
|
|
|
|
url += '&level=' + codecLimits.maxVideoLevel;
|
2014-04-06 10:53:23 -07:00
|
|
|
|
|
2014-04-06 11:00:37 -07:00
|
|
|
|
url += '&maxwidth=' + codecLimits.maxWidth;
|
|
|
|
|
url += '&maxheight=' + codecLimits.maxHeight;
|
2014-04-06 10:53:23 -07:00
|
|
|
|
|
|
|
|
|
url += '&videoCodec=h264';
|
|
|
|
|
url += '&audioCodec=aac';
|
|
|
|
|
|
2014-04-06 11:00:37 -07:00
|
|
|
|
url += '&audiosamplerate=' + codecLimits.maxSampleRate;
|
2014-04-06 10:53:23 -07:00
|
|
|
|
url += '&mediasourceid=' + mediaSourceInfo.mediaSource.Id;
|
|
|
|
|
|
|
|
|
|
return url;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new Error('Unrecognized MediaType');
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-22 22:52:30 -07:00
|
|
|
|
/**
|
|
|
|
|
* Loads media into a running receiver application
|
|
|
|
|
* @param {Number} mediaIndex An index number to indicate current media content
|
|
|
|
|
*/
|
2014-04-06 10:53:23 -07:00
|
|
|
|
CastPlayer.prototype.loadMedia = function (user, item, startTimeTicks, mediaSourceId, audioStreamIndex, subtitleStreamIndex) {
|
|
|
|
|
|
2014-02-23 13:35:58 -07:00
|
|
|
|
if (!this.session) {
|
|
|
|
|
console.log("no session");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-02-22 22:52:30 -07:00
|
|
|
|
|
2014-04-06 10:53:23 -07:00
|
|
|
|
var maxBitrate = 12000000;
|
|
|
|
|
var mediaInfo = getMediaSourceInfo(user, item, maxBitrate, mediaSourceId, audioStreamIndex, subtitleStreamIndex);
|
|
|
|
|
|
|
|
|
|
var streamUrl = getStreamUrl(item, mediaInfo, startTimeTicks, maxBitrate);
|
2014-02-22 22:52:30 -07:00
|
|
|
|
|
2014-04-06 10:53:23 -07:00
|
|
|
|
var castMediaInfo = new chrome.cast.media.MediaInfo(streamUrl);
|
2014-02-22 22:52:30 -07:00
|
|
|
|
|
2014-04-07 09:26:48 -07:00
|
|
|
|
castMediaInfo.customData = getCustomData(item, mediaInfo.mediaSource.Id, startTimeTicks, audioStreamIndex, subtitleStreamIndex);
|
2014-04-06 10:53:23 -07:00
|
|
|
|
castMediaInfo.metadata = getMetadata(item);
|
2014-02-22 22:52:30 -07:00
|
|
|
|
|
2014-04-06 10:53:23 -07:00
|
|
|
|
if (mediaInfo.streamContainer == 'm3u8') {
|
|
|
|
|
castMediaInfo.contentType = 'application/x-mpegURL';
|
|
|
|
|
} else {
|
|
|
|
|
castMediaInfo.contentType = item.MediaType.toLowerCase() + '/' + mediaInfo.streamContainer.toLowerCase();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
castMediaInfo.streamType = mediaInfo.isStatic ? chrome.cast.media.StreamType.BUFFERED : chrome.cast.media.StreamType.LIVE;
|
2014-02-22 22:52:30 -07:00
|
|
|
|
|
2014-04-06 10:53:23 -07:00
|
|
|
|
var request = new chrome.cast.media.LoadRequest(castMediaInfo);
|
|
|
|
|
request.autoplay = true;
|
|
|
|
|
request.currentTime = 0;
|
|
|
|
|
|
|
|
|
|
this.castPlayerState = PLAYER_STATE.LOADING;
|
|
|
|
|
this.session.loadMedia(request,
|
2014-04-07 12:12:16 -07:00
|
|
|
|
this.onMediaDiscovered.bind(this, 'loadMedia'),
|
|
|
|
|
this.onLoadMediaError.bind(this)
|
|
|
|
|
);
|
2014-02-22 22:52:30 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Callback function for loadMedia success
|
|
|
|
|
* @param {Object} mediaSession A new media object.
|
|
|
|
|
*/
|
|
|
|
|
CastPlayer.prototype.onMediaDiscovered = function (how, mediaSession) {
|
2014-04-06 10:53:23 -07:00
|
|
|
|
|
|
|
|
|
console.log("chromecast new media session ID:" + mediaSession.mediaSessionId + ' (' + how + ')');
|
2014-02-23 13:35:58 -07:00
|
|
|
|
this.currentMediaSession = mediaSession;
|
|
|
|
|
if (how == 'loadMedia') {
|
2014-04-06 10:53:23 -07:00
|
|
|
|
this.castPlayerState = PLAYER_STATE.PLAYING;
|
2014-02-23 13:35:58 -07:00
|
|
|
|
}
|
2014-02-22 22:52:30 -07:00
|
|
|
|
|
2014-02-23 13:35:58 -07:00
|
|
|
|
if (how == 'activeSession') {
|
|
|
|
|
this.castPlayerState = this.session.media[0].playerState;
|
|
|
|
|
this.currentMediaTime = this.session.media[0].currentTime;
|
|
|
|
|
}
|
2014-02-22 22:52:30 -07:00
|
|
|
|
|
2014-02-23 13:35:58 -07:00
|
|
|
|
if (this.castPlayerState == PLAYER_STATE.PLAYING) {
|
|
|
|
|
// start progress timer
|
|
|
|
|
//this.startProgressTimer(this.incrementMediaTime);
|
|
|
|
|
}
|
2014-02-22 22:52:30 -07:00
|
|
|
|
|
2014-02-23 13:35:58 -07:00
|
|
|
|
this.currentMediaSession.addUpdateListener(this.onMediaStatusUpdate.bind(this));
|
2014-02-22 22:52:30 -07:00
|
|
|
|
|
|
|
|
|
//this.currentMediaDuration = this.currentMediaSession.media.duration;
|
|
|
|
|
//var duration = this.currentMediaDuration;
|
|
|
|
|
//var hr = parseInt(duration / 3600);
|
|
|
|
|
//duration -= hr * 3600;
|
|
|
|
|
//var min = parseInt(duration / 60);
|
|
|
|
|
//var sec = parseInt(duration % 60);
|
|
|
|
|
//if (hr > 0) {
|
|
|
|
|
// duration = hr + ":" + min + ":" + sec;
|
|
|
|
|
//}
|
|
|
|
|
//else {
|
|
|
|
|
// if (min > 0) {
|
|
|
|
|
// duration = min + ":" + sec;
|
|
|
|
|
// }
|
|
|
|
|
// else {
|
|
|
|
|
// duration = sec;
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
//document.getElementById("duration").innerHTML = duration;
|
|
|
|
|
};
|
|
|
|
|
|
2014-02-23 13:35:58 -07:00
|
|
|
|
/**
|
|
|
|
|
* Callback function when media load returns error
|
|
|
|
|
*/
|
|
|
|
|
CastPlayer.prototype.onLoadMediaError = function (e) {
|
2014-04-06 10:53:23 -07:00
|
|
|
|
console.log("chromecast media error");
|
2014-02-23 13:35:58 -07:00
|
|
|
|
this.castPlayerState = PLAYER_STATE.IDLE;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Callback function for media status update from receiver
|
|
|
|
|
* @param {!Boolean} e true/false
|
|
|
|
|
*/
|
|
|
|
|
CastPlayer.prototype.onMediaStatusUpdate = function (e) {
|
|
|
|
|
if (e == false) {
|
|
|
|
|
this.currentMediaTime = 0;
|
|
|
|
|
this.castPlayerState = PLAYER_STATE.IDLE;
|
|
|
|
|
}
|
2014-04-06 10:53:23 -07:00
|
|
|
|
console.log("chromecast updating media");
|
2014-04-05 10:01:04 -07:00
|
|
|
|
//this.updateProgressBar(e);
|
2014-02-23 13:35:58 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper function
|
|
|
|
|
* Increment media current position by 1 second
|
|
|
|
|
*/
|
|
|
|
|
CastPlayer.prototype.incrementMediaTime = function () {
|
2014-04-05 10:01:04 -07:00
|
|
|
|
if (this.castPlayerState == PLAYER_STATE.PLAYING) {
|
2014-02-23 13:35:58 -07:00
|
|
|
|
if (this.currentMediaTime < this.currentMediaDuration) {
|
|
|
|
|
this.currentMediaTime += 1;
|
|
|
|
|
this.updateProgressBarByTimer();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
this.currentMediaTime = 0;
|
|
|
|
|
clearInterval(this.timer);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Play media in Cast mode
|
|
|
|
|
*/
|
|
|
|
|
CastPlayer.prototype.playMedia = function () {
|
2014-04-06 10:53:23 -07:00
|
|
|
|
|
2014-02-23 13:35:58 -07:00
|
|
|
|
if (!this.currentMediaSession) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (this.castPlayerState) {
|
|
|
|
|
case PLAYER_STATE.LOADED:
|
|
|
|
|
case PLAYER_STATE.PAUSED:
|
|
|
|
|
this.currentMediaSession.play(null,
|
|
|
|
|
this.mediaCommandSuccessCallback.bind(this, "playing started for " + this.currentMediaSession.sessionId),
|
|
|
|
|
this.onError.bind(this));
|
|
|
|
|
this.currentMediaSession.addUpdateListener(this.onMediaStatusUpdate.bind(this));
|
|
|
|
|
this.castPlayerState = PLAYER_STATE.PLAYING;
|
|
|
|
|
// start progress timer
|
|
|
|
|
this.startProgressTimer(this.incrementMediaTime);
|
|
|
|
|
break;
|
|
|
|
|
case PLAYER_STATE.IDLE:
|
|
|
|
|
case PLAYER_STATE.LOADING:
|
|
|
|
|
case PLAYER_STATE.STOPPED:
|
|
|
|
|
this.loadMedia(this.currentMediaIndex);
|
|
|
|
|
this.currentMediaSession.addUpdateListener(this.onMediaStatusUpdate.bind(this));
|
|
|
|
|
this.castPlayerState = PLAYER_STATE.PLAYING;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Pause media playback in Cast mode
|
|
|
|
|
*/
|
|
|
|
|
CastPlayer.prototype.pauseMedia = function () {
|
2014-04-06 10:53:23 -07:00
|
|
|
|
|
2014-02-23 13:35:58 -07:00
|
|
|
|
if (!this.currentMediaSession) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.castPlayerState == PLAYER_STATE.PLAYING) {
|
|
|
|
|
this.castPlayerState = PLAYER_STATE.PAUSED;
|
|
|
|
|
this.currentMediaSession.pause(null,
|
|
|
|
|
this.mediaCommandSuccessCallback.bind(this, "paused " + this.currentMediaSession.sessionId),
|
|
|
|
|
this.onError.bind(this));
|
|
|
|
|
clearInterval(this.timer);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2014-04-05 10:01:04 -07:00
|
|
|
|
* Stop CC playback
|
2014-02-23 13:35:58 -07:00
|
|
|
|
*/
|
|
|
|
|
CastPlayer.prototype.stopMedia = function () {
|
2014-04-06 10:53:23 -07:00
|
|
|
|
|
2014-02-23 13:35:58 -07:00
|
|
|
|
if (!this.currentMediaSession) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.currentMediaSession.stop(null,
|
|
|
|
|
this.mediaCommandSuccessCallback.bind(this, "stopped " + this.currentMediaSession.sessionId),
|
|
|
|
|
this.onError.bind(this));
|
|
|
|
|
this.castPlayerState = PLAYER_STATE.STOPPED;
|
|
|
|
|
clearInterval(this.timer);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set media volume in Cast mode
|
|
|
|
|
* @param {Boolean} mute A boolean
|
|
|
|
|
*/
|
|
|
|
|
CastPlayer.prototype.setReceiverVolume = function (mute) {
|
2014-04-06 10:53:23 -07:00
|
|
|
|
|
2014-02-23 13:35:58 -07:00
|
|
|
|
if (!this.currentMediaSession) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!mute) {
|
2014-04-05 10:01:04 -07:00
|
|
|
|
this.session.setReceiverVolumeLevel(1,
|
2014-02-23 13:35:58 -07:00
|
|
|
|
this.mediaCommandSuccessCallback.bind(this),
|
|
|
|
|
this.onError.bind(this));
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
this.session.setReceiverMuted(true,
|
|
|
|
|
this.mediaCommandSuccessCallback.bind(this),
|
|
|
|
|
this.onError.bind(this));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2014-04-05 10:01:04 -07:00
|
|
|
|
* Toggle mute CC
|
2014-02-23 13:35:58 -07:00
|
|
|
|
*/
|
2014-04-05 10:01:04 -07:00
|
|
|
|
CastPlayer.prototype.toggleMute = function () {
|
2014-02-23 13:35:58 -07:00
|
|
|
|
if (this.audio == true) {
|
2014-04-05 10:01:04 -07:00
|
|
|
|
this.mute();
|
2014-02-23 13:35:58 -07:00
|
|
|
|
}
|
|
|
|
|
else {
|
2014-04-05 10:01:04 -07:00
|
|
|
|
this.unMute();
|
2014-02-23 13:35:58 -07:00
|
|
|
|
}
|
2014-04-05 10:01:04 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Mute CC
|
|
|
|
|
*/
|
|
|
|
|
CastPlayer.prototype.mute = function () {
|
|
|
|
|
this.audio = false;
|
|
|
|
|
this.setReceiverVolume(true);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Unmute CC
|
|
|
|
|
*/
|
|
|
|
|
CastPlayer.prototype.unMute = function () {
|
|
|
|
|
this.audio = true;
|
|
|
|
|
this.setReceiverVolume(false);
|
2014-02-23 13:35:58 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* media seek function in either Cast or local mode
|
|
|
|
|
* @param {Event} e An event object from seek
|
|
|
|
|
*/
|
|
|
|
|
CastPlayer.prototype.seekMedia = function (event) {
|
|
|
|
|
var pos = parseInt(event.offsetX);
|
|
|
|
|
var pi = document.getElementById("progress_indicator");
|
|
|
|
|
var p = document.getElementById("progress");
|
|
|
|
|
if (event.currentTarget.id == 'progress_indicator') {
|
|
|
|
|
var curr = parseInt(this.currentMediaTime + this.currentMediaDuration * pos / PROGRESS_BAR_WIDTH);
|
|
|
|
|
var pp = parseInt(pi.style.marginLeft) + pos;
|
|
|
|
|
var pw = parseInt(p.style.width) + pos;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
var curr = parseInt(pos * this.currentMediaDuration / PROGRESS_BAR_WIDTH);
|
|
|
|
|
var pp = pos - 21 - PROGRESS_BAR_WIDTH;
|
|
|
|
|
var pw = pos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.castPlayerState != PLAYER_STATE.PLAYING && this.castPlayerState != PLAYER_STATE.PAUSED) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.currentMediaTime = curr;
|
|
|
|
|
console.log('Seeking ' + this.currentMediaSession.sessionId + ':' +
|
|
|
|
|
this.currentMediaSession.mediaSessionId + ' to ' + pos + "%");
|
|
|
|
|
var request = new chrome.cast.media.SeekRequest();
|
|
|
|
|
request.currentTime = this.currentMediaTime;
|
|
|
|
|
this.currentMediaSession.seek(request,
|
|
|
|
|
this.onSeekSuccess.bind(this, 'media seek done'),
|
|
|
|
|
this.onError.bind(this));
|
|
|
|
|
this.castPlayerState = PLAYER_STATE.SEEKING;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Callback function for seek success
|
|
|
|
|
* @param {String} info A string that describe seek event
|
|
|
|
|
*/
|
|
|
|
|
CastPlayer.prototype.onSeekSuccess = function (info) {
|
|
|
|
|
console.log(info);
|
|
|
|
|
this.castPlayerState = PLAYER_STATE.PLAYING;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Callback function for media command success
|
|
|
|
|
*/
|
|
|
|
|
CastPlayer.prototype.mediaCommandSuccessCallback = function (info, e) {
|
|
|
|
|
console.log(info);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update progress bar when there is a media status update
|
|
|
|
|
* @param {Object} e An media status update object
|
|
|
|
|
*/
|
|
|
|
|
CastPlayer.prototype.updateProgressBar = function (e) {
|
|
|
|
|
var p = document.getElementById("progress");
|
|
|
|
|
var pi = document.getElementById("progress_indicator");
|
|
|
|
|
if (e.idleReason == 'FINISHED' && e.playerState == 'IDLE') {
|
|
|
|
|
p.style.width = '0px';
|
|
|
|
|
pi.style.marginLeft = -21 - PROGRESS_BAR_WIDTH + 'px';
|
|
|
|
|
clearInterval(this.timer);
|
|
|
|
|
this.castPlayerState = PLAYER_STATE.STOPPED;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
p.style.width = Math.ceil(PROGRESS_BAR_WIDTH * e.currentTime / this.currentMediaSession.media.duration + 1) + 'px';
|
|
|
|
|
this.progressFlag = false;
|
|
|
|
|
setTimeout(this.setProgressFlag.bind(this), 1000); // don't update progress in 1 second
|
|
|
|
|
var pp = Math.ceil(PROGRESS_BAR_WIDTH * e.currentTime / this.currentMediaSession.media.duration);
|
|
|
|
|
pi.style.marginLeft = -21 - PROGRESS_BAR_WIDTH + pp + 'px';
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set progressFlag with a timeout of 1 second to avoid UI update
|
|
|
|
|
* until a media status update from receiver
|
|
|
|
|
*/
|
|
|
|
|
CastPlayer.prototype.setProgressFlag = function () {
|
|
|
|
|
this.progressFlag = true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update progress bar based on timer
|
|
|
|
|
*/
|
|
|
|
|
CastPlayer.prototype.updateProgressBarByTimer = function () {
|
|
|
|
|
var p = document.getElementById("progress");
|
|
|
|
|
if (isNaN(parseInt(p.style.width))) {
|
|
|
|
|
p.style.width = 0;
|
|
|
|
|
}
|
|
|
|
|
if (this.currentMediaDuration > 0) {
|
|
|
|
|
var pp = Math.floor(PROGRESS_BAR_WIDTH * this.currentMediaTime / this.currentMediaDuration);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.progressFlag) {
|
|
|
|
|
// don't update progress if it's been updated on media status update event
|
|
|
|
|
p.style.width = pp + 'px';
|
|
|
|
|
var pi = document.getElementById("progress_indicator");
|
|
|
|
|
pi.style.marginLeft = -21 - PROGRESS_BAR_WIDTH + pp + 'px';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pp > PROGRESS_BAR_WIDTH) {
|
|
|
|
|
clearInterval(this.timer);
|
|
|
|
|
this.deviceState = DEVICE_STATE.IDLE;
|
|
|
|
|
this.castPlayerState = PLAYER_STATE.IDLE;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2014-04-05 10:01:04 -07:00
|
|
|
|
var castPlayer = new CastPlayer();
|
2014-02-23 13:35:58 -07:00
|
|
|
|
|
2014-04-05 10:01:04 -07:00
|
|
|
|
function chromecastPlayer() {
|
2014-02-23 13:35:58 -07:00
|
|
|
|
|
2014-04-05 10:01:04 -07:00
|
|
|
|
var self = this;
|
2014-02-23 13:35:58 -07:00
|
|
|
|
|
2014-04-06 10:53:23 -07:00
|
|
|
|
self.name = PlayerName;
|
2014-02-23 13:35:58 -07:00
|
|
|
|
|
2014-04-05 10:01:04 -07:00
|
|
|
|
self.play = function (options) {
|
2014-02-23 10:53:25 -07:00
|
|
|
|
|
2014-04-06 10:53:23 -07:00
|
|
|
|
if (options.items) {
|
|
|
|
|
|
|
|
|
|
Dashboard.getCurrentUser().done(function (user) {
|
|
|
|
|
|
|
|
|
|
castPlayer.loadMedia(user, options.items[0], options.startTimeTicks);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
var userId = Dashboard.getCurrentUserId();
|
|
|
|
|
|
|
|
|
|
var query = {};
|
|
|
|
|
query.Limit = query.Limit || 100;
|
|
|
|
|
query.Fields = "MediaSources,Chapters";
|
|
|
|
|
query.ExcludeLocationTypes = "Virtual";
|
|
|
|
|
query.Ids = options.ids.join(',');
|
|
|
|
|
|
|
|
|
|
ApiClient.getItems(userId, query).done(function (result) {
|
|
|
|
|
|
|
|
|
|
options.items = result.Items;
|
|
|
|
|
|
|
|
|
|
self.play(options);
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-05 10:01:04 -07:00
|
|
|
|
};
|
2014-03-30 16:26:16 -07:00
|
|
|
|
|
2014-04-05 10:01:04 -07:00
|
|
|
|
self.shuffle = function (id) {
|
2014-04-06 10:53:23 -07:00
|
|
|
|
self.play({ ids: [id] });
|
2014-04-05 10:01:04 -07:00
|
|
|
|
};
|
2014-02-23 10:29:02 -07:00
|
|
|
|
|
2014-04-05 10:01:04 -07:00
|
|
|
|
self.instantMix = function (id) {
|
2014-04-06 10:53:23 -07:00
|
|
|
|
self.play({ ids: [id] });
|
2014-04-05 10:01:04 -07:00
|
|
|
|
};
|
2014-02-22 22:52:30 -07:00
|
|
|
|
|
2014-04-05 10:01:04 -07:00
|
|
|
|
self.queue = function (options) {
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.queueNext = function (options) {
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.stop = function () {
|
2014-04-06 10:53:23 -07:00
|
|
|
|
castPlayer.stop();
|
2014-04-05 10:01:04 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.canQueueMediaType = function (mediaType) {
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
|
2014-04-06 10:53:23 -07:00
|
|
|
|
self.mute = function () {
|
|
|
|
|
castPlayer.mute();
|
2014-04-05 10:01:04 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.unMute = function () {
|
2014-04-06 10:53:23 -07:00
|
|
|
|
castPlayer.unMute();
|
2014-04-05 10:01:04 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.toggleMute = function () {
|
2014-04-06 10:53:23 -07:00
|
|
|
|
castPlayer.toggleMute();
|
2014-04-05 10:01:04 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.getTargets = function () {
|
|
|
|
|
|
|
|
|
|
var targets = [];
|
|
|
|
|
|
2014-04-06 10:53:23 -07:00
|
|
|
|
targets.push(self.getCurrentTargetInfo());
|
2014-04-05 10:01:04 -07:00
|
|
|
|
|
|
|
|
|
return targets;
|
|
|
|
|
};
|
|
|
|
|
|
2014-04-06 10:53:23 -07:00
|
|
|
|
self.getCurrentTargetInfo = function () {
|
2014-03-30 16:26:16 -07:00
|
|
|
|
|
2014-04-06 10:53:23 -07:00
|
|
|
|
var appName = null;
|
2014-02-22 22:52:30 -07:00
|
|
|
|
|
2014-04-06 10:53:23 -07:00
|
|
|
|
if (castPlayer.session && castPlayer.session.receiver && castPlayer.session.friendlyName) {
|
|
|
|
|
appName = castPlayer.session.friendlyName;
|
|
|
|
|
}
|
2014-02-23 10:53:25 -07:00
|
|
|
|
|
2014-04-06 10:53:23 -07:00
|
|
|
|
return {
|
|
|
|
|
name: PlayerName,
|
|
|
|
|
id: PlayerName,
|
|
|
|
|
playerName: self.name,
|
|
|
|
|
playableMediaTypes: ["Audio", "Video"],
|
|
|
|
|
isLocalPlayer: false,
|
|
|
|
|
appName: appName
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
}
|
2014-02-23 13:35:58 -07:00
|
|
|
|
|
2014-04-06 10:53:23 -07:00
|
|
|
|
MediaController.registerPlayer(new chromecastPlayer());
|
2014-02-22 22:52:30 -07:00
|
|
|
|
|
2014-04-05 10:01:04 -07:00
|
|
|
|
$(MediaController).on('playerchange', function () {
|
|
|
|
|
|
2014-04-06 10:53:23 -07:00
|
|
|
|
if (MediaController.getPlayerInfo().name == PlayerName) {
|
|
|
|
|
|
|
|
|
|
if (castPlayer.deviceState != DEVICE_STATE.ACTIVE && castPlayer.isInitialized) {
|
|
|
|
|
castPlayer.launchApp();
|
|
|
|
|
}
|
2014-04-05 10:01:04 -07:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2014-02-22 22:52:30 -07:00
|
|
|
|
})(window, window.chrome, console);
|