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

1820 lines
56 KiB
JavaScript
Raw Normal View History

2015-06-25 14:50:56 -07:00
(function (document, setTimeout, clearTimeout, screen, $, setInterval, window) {
2013-02-20 18:33:05 -07:00
2013-04-10 14:03:28 -07:00
function mediaPlayer() {
2013-05-23 18:47:07 -07:00
2013-04-10 14:03:28 -07:00
var self = this;
2013-04-08 21:26:02 -07:00
2013-04-10 14:03:28 -07:00
var currentProgressInterval;
2014-01-10 06:52:01 -07:00
var canClientSeek;
var currentPlaylistIndex = 0;
2013-05-23 13:09:01 -07:00
2015-06-07 13:06:18 -07:00
self.currentMediaRenderer = null;
2014-06-11 12:31:33 -07:00
self.currentItem = null;
self.currentMediaSource = null;
2014-03-31 14:43:49 -07:00
self.currentDurationTicks = null;
2014-03-29 08:40:32 -07:00
self.startTimeTicksOffset = null;
2013-06-07 09:06:32 -07:00
self.playlist = [];
2014-03-29 08:40:32 -07:00
self.isLocalPlayer = true;
2014-04-06 10:53:23 -07:00
self.isDefaultPlayer = true;
2014-07-01 14:13:32 -07:00
2014-03-29 09:58:49 -07:00
self.name = 'Html5 Player';
self.getTargets = function () {
var targets = [{
2015-05-06 20:11:51 -07:00
name: Globalize.translate('MyDevice'),
2014-10-25 11:32:58 -07:00
id: ConnectionManager.deviceId(),
2014-03-29 11:20:42 -07:00
playerName: self.name,
playableMediaTypes: ['Audio', 'Video'],
2014-04-13 10:27:13 -07:00
isLocalPlayer: true,
supportedCommands: Dashboard.getSupportedRemoteCommands()
2014-03-29 09:58:49 -07:00
}];
return targets;
};
2014-03-29 08:40:32 -07:00
2015-05-24 11:33:28 -07:00
var canPlayAac = document.createElement('audio').canPlayType('audio/aac').replace(/no/, '');
2015-06-13 08:16:08 -07:00
self.getVideoQualityOptions = function (videoWidth, videoHeight) {
var bitrateSetting = AppSettings.maxStreamingBitrate();
2015-06-13 08:16:08 -07:00
var maxAllowedWidth = videoWidth || 4096;
2015-06-13 07:46:59 -07:00
var maxAllowedHeight = videoHeight || 2304;
var options = [];
// Some 1080- videos are reported as 1912?
2015-06-13 08:16:08 -07:00
if (maxAllowedWidth >= 1900) {
2015-06-13 07:46:59 -07:00
options.push({ name: '1080p - 30Mbps', maxHeight: 1080, bitrate: 30000000 });
options.push({ name: '1080p - 25Mbps', maxHeight: 1080, bitrate: 25000000 });
options.push({ name: '1080p - 20Mbps', maxHeight: 1080, bitrate: 20000000 });
options.push({ name: '1080p - 15Mbps', maxHeight: 1080, bitrate: 15000000 });
options.push({ name: '1080p - 10Mbps', maxHeight: 1080, bitrate: 10000000 });
options.push({ name: '1080p - 8Mbps', maxHeight: 1080, bitrate: 8000000 });
options.push({ name: '1080p - 6Mbps', maxHeight: 1080, bitrate: 6000000 });
options.push({ name: '1080p - 5Mbps', maxHeight: 1080, bitrate: 5000000 });
2015-06-13 08:16:08 -07:00
} else if (maxAllowedWidth >= 1260) {
2015-06-13 07:46:59 -07:00
options.push({ name: '720p - 10Mbps', maxHeight: 720, bitrate: 10000000 });
options.push({ name: '720p - 8Mbps', maxHeight: 720, bitrate: 8000000 });
options.push({ name: '720p - 6Mbps', maxHeight: 720, bitrate: 6000000 });
options.push({ name: '720p - 5Mbps', maxHeight: 720, bitrate: 5000000 });
2015-06-13 08:16:08 -07:00
} else if (maxAllowedWidth >= 700) {
2015-06-13 07:46:59 -07:00
options.push({ name: '480p - 4Mbps', maxHeight: 480, bitrate: 4000000 });
options.push({ name: '480p - 3Mbps', maxHeight: 480, bitrate: 3000000 });
options.push({ name: '480p - 2.5Mbps', maxHeight: 480, bitrate: 2500000 });
options.push({ name: '480p - 2Mbps', maxHeight: 480, bitrate: 2000000 });
options.push({ name: '480p - 1.5Mbps', maxHeight: 480, bitrate: 1500000 });
}
2015-06-13 08:16:08 -07:00
if (maxAllowedWidth >= 1260) {
2015-06-13 07:46:59 -07:00
options.push({ name: '720p - 4Mbps', maxHeight: 720, bitrate: 4000000 });
options.push({ name: '720p - 3Mbps', maxHeight: 720, bitrate: 3000000 });
options.push({ name: '720p - 2Mbps', maxHeight: 720, bitrate: 2000000 });
// The extra 1 is because they're keyed off the bitrate value
2015-06-13 07:46:59 -07:00
options.push({ name: '720p - 1.5Mbps', maxHeight: 720, bitrate: 1500001 });
options.push({ name: '720p - 1Mbps', maxHeight: 720, bitrate: 1000001 });
}
2015-06-13 07:46:59 -07:00
options.push({ name: '480p - 1.0Mbps', maxHeight: 480, bitrate: 1000000 });
options.push({ name: '480p - 720kbps', maxHeight: 480, bitrate: 720000 });
options.push({ name: '480p - 420kbps', maxHeight: 480, bitrate: 420000 });
options.push({ name: '360p', maxHeight: 360, bitrate: 400000 });
options.push({ name: '240p', maxHeight: 240, bitrate: 320000 });
options.push({ name: '144p', maxHeight: 144, bitrate: 192000 });
var i, length, option;
var selectedIndex = -1;
for (i = 0, length = options.length; i < length; i++) {
option = options[i];
if (selectedIndex == -1 && option.bitrate <= bitrateSetting) {
selectedIndex = i;
}
}
if (selectedIndex == -1) {
selectedIndex = options.length - 1;
}
options[selectedIndex].selected = true;
return options;
};
2015-06-13 07:46:59 -07:00
self.getDeviceProfile = function (maxHeight) {
2015-06-13 07:46:59 -07:00
if (!maxHeight) {
maxHeight = self.getVideoQualityOptions().filter(function (q) {
2015-05-26 08:31:50 -07:00
return q.selected;
2015-06-13 07:46:59 -07:00
})[0].maxHeight;
2015-05-26 08:31:50 -07:00
}
var bitrateSetting = AppSettings.maxStreamingBitrate();
2015-03-26 12:18:21 -07:00
var canPlayWebm = self.canPlayWebm();
var profile = {};
profile.MaxStreamingBitrate = bitrateSetting;
profile.MaxStaticBitrate = 40000000;
2015-03-26 16:10:34 -07:00
profile.MusicStreamingTranscodingBitrate = Math.min(bitrateSetting, 192000);
profile.DirectPlayProfiles = [];
profile.DirectPlayProfiles.push({
2015-05-02 09:34:27 -07:00
Container: 'mp4,m4v',
Type: 'Video',
VideoCodec: 'h264',
AudioCodec: 'aac,mp3'
});
if ($.browser.chrome) {
profile.DirectPlayProfiles.push({
2015-05-02 09:34:27 -07:00
Container: 'mkv',
Type: 'Video',
VideoCodec: 'h264',
AudioCodec: 'aac,mp3'
});
2015-05-06 20:11:51 -07:00
// TODO: Test this
//profile.DirectPlayProfiles.push({
// Container: 'mov',
// Type: 'Video',
// VideoCodec: 'h264',
// AudioCodec: 'aac,mp3'
//});
}
2015-07-06 19:25:23 -07:00
var directPlayVideoContainers = AppInfo.directPlayVideoContainers;
if (directPlayVideoContainers && directPlayVideoContainers.length) {
profile.DirectPlayProfiles.push({
Container: directPlayVideoContainers.join(','),
Type: 'Video'
});
}
profile.DirectPlayProfiles.push({
Container: 'mp3',
Type: 'Audio'
});
2015-05-24 11:33:28 -07:00
if (canPlayAac) {
profile.DirectPlayProfiles.push({
Container: 'aac',
Type: 'Audio'
});
}
2015-06-10 23:27:05 -07:00
var directPlayAudioContainers = AppInfo.directPlayAudioContainers;
if (directPlayAudioContainers && directPlayAudioContainers.length) {
profile.DirectPlayProfiles.push({
Container: directPlayAudioContainers.join(','),
Type: 'Audio'
});
}
2015-03-26 12:18:21 -07:00
if (canPlayWebm) {
profile.DirectPlayProfiles.push({
Container: 'webm',
Type: 'Video'
});
profile.DirectPlayProfiles.push({
Container: 'webm,webma',
Type: 'Audio'
});
}
profile.TranscodingProfiles = [];
2015-05-22 12:16:14 -07:00
if (self.canPlayHls()) {
profile.TranscodingProfiles.push({
Container: 'ts',
Type: 'Video',
AudioCodec: 'aac',
VideoCodec: 'h264',
Context: 'Streaming',
Protocol: 'hls'
});
2015-05-24 11:33:28 -07:00
2015-05-25 10:32:22 -07:00
if (canPlayAac && $.browser.safari) {
2015-05-24 11:33:28 -07:00
profile.TranscodingProfiles.push({
2015-06-07 18:23:56 -07:00
Container: 'ts',
2015-05-24 11:33:28 -07:00
Type: 'Audio',
AudioCodec: 'aac',
Context: 'Streaming',
Protocol: 'hls'
});
}
}
2015-03-26 12:18:21 -07:00
if (canPlayWebm) {
profile.TranscodingProfiles.push({
Container: 'webm',
Type: 'Video',
AudioCodec: 'vorbis',
VideoCodec: 'vpx',
Context: 'Streaming',
Protocol: 'http'
});
}
profile.TranscodingProfiles.push({
Container: 'mp4',
Type: 'Video',
AudioCodec: 'aac',
VideoCodec: 'h264',
Context: 'Streaming',
Protocol: 'http'
});
2015-05-31 14:07:44 -07:00
profile.TranscodingProfiles.push({
Container: 'mp4',
Type: 'Video',
AudioCodec: 'aac',
VideoCodec: 'h264',
Context: 'Static',
Protocol: 'http'
});
2015-05-24 11:33:28 -07:00
if (canPlayAac && $.browser.safari) {
profile.TranscodingProfiles.push({
Container: 'aac',
Type: 'Audio',
AudioCodec: 'aac',
Context: 'Streaming',
Protocol: 'http'
});
2015-05-31 14:07:44 -07:00
profile.TranscodingProfiles.push({
Container: 'aac',
Type: 'Audio',
AudioCodec: 'aac',
Context: 'Static',
Protocol: 'http'
});
2015-05-24 11:33:28 -07:00
} else {
profile.TranscodingProfiles.push({
Container: 'mp3',
Type: 'Audio',
AudioCodec: 'mp3',
Context: 'Streaming',
Protocol: 'http'
});
2015-05-31 14:07:44 -07:00
profile.TranscodingProfiles.push({
Container: 'mp3',
Type: 'Audio',
AudioCodec: 'mp3',
Context: 'Static',
Protocol: 'http'
});
2015-05-24 11:33:28 -07:00
}
profile.ContainerProfiles = [];
var audioConditions = [];
2015-04-24 20:30:44 -07:00
2015-04-25 20:25:07 -07:00
var maxAudioChannels = $.browser.msie || $.browser.safari ?
'2' :
'6';
2015-05-19 12:15:40 -07:00
audioConditions.push({
2015-04-25 20:25:07 -07:00
Condition: 'LessThanEqual',
Property: 'AudioChannels',
Value: maxAudioChannels
2015-05-19 12:15:40 -07:00
});
profile.CodecProfiles = [];
profile.CodecProfiles.push({
Type: 'Audio',
Conditions: audioConditions
});
2015-05-19 12:15:40 -07:00
profile.CodecProfiles.push({
Type: 'VideoAudio',
Codec: 'mp3',
Conditions: [{
Condition: 'LessThanEqual',
Property: 'AudioChannels',
Value: maxAudioChannels
}]
});
2015-04-24 20:30:44 -07:00
2015-07-06 19:25:23 -07:00
var isVlc = AppInfo.isNativeApp && $.browser.android;
if (!isVlc) {
profile.CodecProfiles.push({
Type: 'VideoAudio',
Codec: 'aac',
Container: 'mkv,mov',
Conditions: [
{
Condition: 'NotEquals',
Property: 'AudioProfile',
Value: 'HE-AAC'
},
{
Condition: 'NotEquals',
Property: 'AudioProfile',
Value: 'LC'
}
]
});
}
2015-04-24 20:30:44 -07:00
profile.CodecProfiles.push({
Type: 'VideoAudio',
2015-04-24 20:30:44 -07:00
Codec: 'aac',
2015-05-06 20:11:51 -07:00
Conditions: [
2015-05-19 12:15:40 -07:00
{
Condition: 'LessThanEqual',
Property: 'AudioChannels',
Value: maxAudioChannels
}
2015-05-06 20:11:51 -07:00
]
});
2015-07-06 19:25:23 -07:00
if (isVlc) {
profile.CodecProfiles.push({
Type: 'Video',
Codec: 'h264',
Conditions: [
{
Condition: 'EqualsAny',
Property: 'VideoProfile',
Value: 'high|main|baseline|constrained baseline'
},
{
Condition: 'LessThanEqual',
Property: 'VideoLevel',
Value: '41'
}]
});
} else {
profile.CodecProfiles.push({
Type: 'Video',
Codec: 'h264',
Conditions: [
{
Condition: 'NotEquals',
Property: 'IsAnamorphic',
Value: 'true',
IsRequired: false
},
{
Condition: 'EqualsAny',
Property: 'VideoProfile',
Value: 'high|main|baseline|constrained baseline'
},
{
Condition: 'LessThanEqual',
Property: 'VideoLevel',
Value: '41'
},
{
Condition: 'LessThanEqual',
Property: 'Height',
Value: maxHeight
}]
});
}
2015-07-06 19:25:23 -07:00
if (!isVlc) {
profile.CodecProfiles.push({
Type: 'Video',
Codec: 'vpx',
Conditions: [
{
Condition: 'NotEquals',
Property: 'IsAnamorphic',
Value: 'true',
IsRequired: false
},
{
Condition: 'LessThanEqual',
Property: 'Height',
Value: maxHeight
}]
});
}
// Subtitle profiles
// External vtt or burn in
profile.SubtitleProfiles = [];
if (self.supportsTextTracks()) {
profile.SubtitleProfiles.push({
Format: 'vtt',
Method: 'External'
});
}
2015-05-02 09:34:27 -07:00
profile.ResponseProfiles = [];
profile.ResponseProfiles.push({
Type: 'Video',
Container: 'm4v',
MimeType: 'video/mp4'
});
2015-05-06 20:11:51 -07:00
//profile.ResponseProfiles.push({
// Type: 'Video',
// Container: 'mkv',
// MimeType: 'video/webm'
//});
profile.ResponseProfiles.push({
Type: 'Video',
Container: 'mov',
MimeType: 'video/webm'
});
return profile;
};
2015-06-13 07:46:59 -07:00
var supportsTextTracks;
self.supportsTextTracks = function () {
if (supportsTextTracks == null) {
supportsTextTracks = document.createElement('video').textTracks != null;
}
// For now, until ready
return supportsTextTracks;
};
2015-06-07 13:06:18 -07:00
self.updateCanClientSeek = function (mediaRenderer) {
2014-10-07 18:37:45 -07:00
2015-06-07 13:06:18 -07:00
var duration = mediaRenderer.duration();
2014-10-03 18:42:38 -07:00
2014-01-10 06:52:01 -07:00
canClientSeek = duration && !isNaN(duration) && duration != Number.POSITIVE_INFINITY && duration != Number.NEGATIVE_INFINITY;
};
2013-05-23 13:09:01 -07:00
2015-06-07 13:06:18 -07:00
self.getCurrentSrc = function (mediaRenderer) {
return mediaRenderer.currentSrc();
2014-10-22 21:26:01 -07:00
};
2015-06-07 13:06:18 -07:00
self.getCurrentTicks = function (mediaRenderer) {
2014-06-13 07:24:14 -07:00
2015-06-25 14:50:56 -07:00
var playerTime = Math.floor(10000 * (mediaRenderer || self.currentMediaRenderer).currentTime());
2014-06-13 07:24:14 -07:00
2014-07-01 14:13:32 -07:00
playerTime += self.startTimeTicksOffset;
2014-06-13 07:24:14 -07:00
return playerTime;
};
2013-05-25 21:52:14 -07:00
self.playNextAfterEnded = function () {
2013-06-07 10:23:23 -07:00
self.nextTrack();
};
2013-05-23 13:09:01 -07:00
2014-05-07 11:38:50 -07:00
self.startProgressInterval = function () {
2013-05-23 13:09:01 -07:00
clearProgressInterval();
2014-11-13 23:27:10 -07:00
var intervalTime = ApiClient.isWebSocketOpen() ? 1200 : 5000;
2015-07-08 17:20:01 -07:00
// Ease up with safari because it doesn't perform as well
if ($.browser.safari) {
intervalTime = Math.max(intervalTime, 5000);
}
2014-11-13 23:27:10 -07:00
self.lastProgressReport = 0;
2013-05-23 13:09:01 -07:00
currentProgressInterval = setInterval(function () {
2015-06-07 13:06:18 -07:00
if (self.currentMediaRenderer) {
2014-11-13 23:27:10 -07:00
if ((new Date().getTime() - self.lastProgressReport) > intervalTime) {
self.lastProgressReport = new Date().getTime();
sendProgressUpdate();
}
2013-05-23 15:39:51 -07:00
}
2013-04-08 21:26:02 -07:00
2014-11-13 23:27:10 -07:00
}, 250);
};
2014-01-10 06:52:01 -07:00
2014-10-07 18:37:45 -07:00
self.getCurrentMediaExtension = function (currentSrc) {
2014-10-03 18:42:38 -07:00
currentSrc = currentSrc.split('?')[0];
return currentSrc.substring(currentSrc.lastIndexOf('.'));
};
2015-06-01 22:46:06 -07:00
self.canPlayNativeHls = function () {
2015-06-08 14:32:20 -07:00
// Don't use HLS on android 4.x, regardless of what the browser reports
2015-06-10 23:27:05 -07:00
//if ($.browser.android) {
// var agent = navigator.userAgent.toLowerCase();
// for (var i = 0; i <= 4; i++) {
// if (agent.indexOf('android 4.' + i) != -1) {
// return false;
// }
// }
//}
2015-06-07 18:23:56 -07:00
2015-07-06 19:25:23 -07:00
if ($.browser.android && AppInfo.isNativeApp) {
return true;
}
2015-06-07 13:06:18 -07:00
var media = document.createElement('video');
// safari
if (media.canPlayType('application/x-mpegURL').replace(/no/, '') ||
media.canPlayType('application/vnd.apple.mpegURL').replace(/no/, '')) {
return true;
}
return false;
};
2015-06-01 22:46:06 -07:00
self.canPlayHls = function () {
if (self.canPlayNativeHls()) {
return true;
}
2015-07-03 04:51:45 -07:00
if ($.browser.chrome) {
2015-07-06 19:25:23 -07:00
2015-07-03 04:51:45 -07:00
// viblast can help us here
//return true;
2015-07-09 20:00:03 -07:00
return window.MediaSource != null;
2015-07-03 04:51:45 -07:00
}
if ($.browser.msie) {
// viblast can help us here
//return true;
2015-07-09 20:00:03 -07:00
return window.MediaSource != null;
2015-07-03 04:51:45 -07:00
}
2015-06-01 22:46:06 -07:00
return false;
};
self.changeStream = function (ticks, params) {
2013-05-23 18:47:07 -07:00
2015-06-07 13:06:18 -07:00
var mediaRenderer = self.currentMediaRenderer;
2013-05-23 18:47:07 -07:00
2014-01-10 06:52:01 -07:00
if (canClientSeek && params == null) {
2013-05-23 18:47:07 -07:00
2015-06-25 14:50:56 -07:00
mediaRenderer.currentTime(ticks / 10000);
return;
}
2013-05-23 18:47:07 -07:00
params = params || {};
2013-05-23 18:47:07 -07:00
2015-06-07 13:06:18 -07:00
var currentSrc = mediaRenderer.currentSrc();
2013-05-23 18:47:07 -07:00
2015-03-29 11:31:28 -07:00
var playSessionId = getParameterByName('PlaySessionId', currentSrc);
2015-03-31 09:24:16 -07:00
var liveStreamId = getParameterByName('LiveStreamId', currentSrc);
2014-01-29 14:54:40 -07:00
2015-03-26 12:18:21 -07:00
if (params.AudioStreamIndex == null && params.SubtitleStreamIndex == null && params.Bitrate == null) {
2014-03-27 12:30:21 -07:00
2015-03-26 12:18:21 -07:00
currentSrc = replaceQueryString(currentSrc, 'starttimeticks', ticks || 0);
2015-06-07 13:06:18 -07:00
changeStreamToUrl(mediaRenderer, playSessionId, currentSrc, ticks);
2015-03-26 12:18:21 -07:00
return;
}
2014-04-11 20:48:57 -07:00
2015-03-26 12:18:21 -07:00
var deviceProfile = self.getDeviceProfile();
2014-04-11 20:48:57 -07:00
2015-03-26 13:31:57 -07:00
var audioStreamIndex = params.AudioStreamIndex == null ? (getParameterByName('AudioStreamIndex', currentSrc) || null) : params.AudioStreamIndex;
2015-03-26 12:18:21 -07:00
if (typeof (audioStreamIndex) == 'string') {
audioStreamIndex = parseInt(audioStreamIndex);
}
2014-04-11 20:48:57 -07:00
2015-03-26 13:31:57 -07:00
var subtitleStreamIndex = params.SubtitleStreamIndex == null ? (getParameterByName('SubtitleStreamIndex', currentSrc) || null) : params.SubtitleStreamIndex;
2015-03-26 12:18:21 -07:00
if (typeof (subtitleStreamIndex) == 'string') {
subtitleStreamIndex = parseInt(subtitleStreamIndex);
}
2014-04-02 14:55:19 -07:00
2015-05-25 10:32:22 -07:00
MediaController.getPlaybackInfo(self.currentItem.Id, deviceProfile, ticks, self.currentMediaSource, audioStreamIndex, subtitleStreamIndex, liveStreamId).done(function (result) {
2014-07-02 11:34:08 -07:00
2015-03-26 12:18:21 -07:00
if (validatePlaybackInfoResult(result)) {
2013-05-23 18:47:07 -07:00
2015-03-26 12:18:21 -07:00
self.currentMediaSource = result.MediaSources[0];
self.currentSubtitleStreamIndex = subtitleStreamIndex;
2013-05-23 18:47:07 -07:00
2015-04-01 19:47:59 -07:00
currentSrc = ApiClient.getUrl(self.currentMediaSource.TranscodingUrl);
2015-06-07 13:06:18 -07:00
changeStreamToUrl(mediaRenderer, playSessionId, currentSrc, ticks);
2015-03-26 12:18:21 -07:00
}
});
};
2013-05-23 18:47:07 -07:00
2015-06-07 13:06:18 -07:00
function changeStreamToUrl(mediaRenderer, playSessionId, url, newPositionTicks) {
2014-01-10 06:52:01 -07:00
clearProgressInterval();
2015-06-29 11:45:42 -07:00
Events.off(mediaRenderer, 'ended', self.onPlaybackStopped);
Events.off(mediaRenderer, 'ended', self.playNextAfterEnded);
2015-07-06 19:25:23 -07:00
2015-06-28 07:45:21 -07:00
$(mediaRenderer).one("play", function () {
2013-05-23 18:47:07 -07:00
self.updateCanClientSeek(this);
2013-05-23 18:47:07 -07:00
2015-06-29 11:45:42 -07:00
Events.on(this, 'ended', self.onPlaybackStopped);
2015-07-06 19:25:23 -07:00
2015-06-29 11:45:42 -07:00
$(this).one('ended', self.playNextAfterEnded);
self.startProgressInterval();
sendProgressUpdate();
2014-04-11 20:48:57 -07:00
});
2014-06-14 19:58:00 -07:00
if (self.currentItem.MediaType == "Video") {
2015-03-29 11:31:28 -07:00
ApiClient.stopActiveEncodings(playSessionId).done(function () {
self.startTimeTicksOffset = newPositionTicks;
2015-06-23 15:13:06 -07:00
mediaRenderer.setCurrentSrc(url, self.currentItem, self.currentMediaSource);
});
self.updateTextStreamUrls(newPositionTicks || 0);
} else {
self.startTimeTicksOffset = newPositionTicks;
2015-06-23 15:13:06 -07:00
mediaRenderer.setCurrentSrc(url, self.currentItem, self.currentMediaSource);
2013-05-23 18:47:07 -07:00
}
}
2013-04-30 12:13:06 -07:00
self.setCurrentTime = function (ticks, positionSlider, currentTimeElement) {
2013-05-23 13:09:01 -07:00
// Convert to ticks
ticks = Math.floor(ticks);
2013-06-07 10:29:33 -07:00
var timeText = Dashboard.getDisplayTime(ticks);
2013-05-23 13:09:01 -07:00
2014-03-31 14:43:49 -07:00
if (self.currentDurationTicks) {
2013-05-23 13:09:01 -07:00
2014-03-31 14:43:49 -07:00
timeText += " / " + Dashboard.getDisplayTime(self.currentDurationTicks);
2013-05-23 13:09:01 -07:00
if (positionSlider) {
2014-03-31 14:43:49 -07:00
var percent = ticks / self.currentDurationTicks;
2013-05-23 13:09:01 -07:00
percent *= 100;
2015-06-27 12:53:36 -07:00
positionSlider.disabled = false;
positionSlider.value = percent;
2013-04-30 12:13:06 -07:00
}
2013-05-24 07:35:15 -07:00
} else {
if (positionSlider) {
2015-06-27 12:53:36 -07:00
positionSlider.disabled = true;
}
}
if (currentTimeElement) {
currentTimeElement.html(timeText);
2013-05-24 07:35:15 -07:00
}
2013-05-23 13:09:01 -07:00
2015-06-07 13:06:18 -07:00
var state = self.getPlayerStateInternal(self.currentMediaRenderer, self.currentItem, self.currentMediaSource);
2015-06-28 07:45:21 -07:00
Events.trigger(self, 'positionchange', [state]);
};
2013-04-30 10:21:21 -07:00
2014-03-29 08:40:32 -07:00
self.canQueueMediaType = function (mediaType) {
2013-04-08 21:16:32 -07:00
2014-06-11 12:31:33 -07:00
return self.currentItem && self.currentItem.MediaType == mediaType;
2014-03-29 08:40:32 -07:00
};
2013-05-24 19:45:12 -07:00
2014-03-29 12:02:43 -07:00
function translateItemsForPlayback(items) {
var deferred = $.Deferred();
var firstItem = items[0];
var promise;
if (firstItem.Type == "Playlist") {
2014-03-29 12:02:43 -07:00
promise = self.getItemsForPlayback({
ParentId: firstItem.Id,
});
}
else if (firstItem.Type == "MusicArtist") {
2014-04-02 14:55:19 -07:00
2014-03-29 12:02:43 -07:00
promise = self.getItemsForPlayback({
2015-03-13 10:25:28 -07:00
ArtistIds: firstItem.Id,
2014-03-29 12:02:43 -07:00
Filters: "IsNotFolder",
Recursive: true,
SortBy: "SortName",
MediaTypes: "Audio"
});
}
else if (firstItem.Type == "MusicGenre") {
2014-04-02 14:55:19 -07:00
2014-03-29 12:02:43 -07:00
promise = self.getItemsForPlayback({
Genres: firstItem.Name,
Filters: "IsNotFolder",
Recursive: true,
SortBy: "SortName",
MediaTypes: "Audio"
});
}
else if (firstItem.IsFolder) {
promise = self.getItemsForPlayback({
ParentId: firstItem.Id,
Filters: "IsNotFolder",
Recursive: true,
SortBy: "SortName",
MediaTypes: "Audio,Video"
});
}
2014-03-29 12:02:43 -07:00
if (promise) {
promise.done(function (result) {
deferred.resolveWith(null, [result.Items]);
});
} else {
deferred.resolveWith(null, [items]);
}
return deferred.promise();
}
2014-03-29 08:40:32 -07:00
self.play = function (options) {
2013-05-24 19:45:12 -07:00
2015-07-09 20:00:03 -07:00
Dashboard.showLoadingMsg();
2014-03-29 08:40:32 -07:00
Dashboard.getCurrentUser().done(function (user) {
2014-03-29 08:40:32 -07:00
if (options.items) {
2014-03-29 12:02:43 -07:00
translateItemsForPlayback(options.items).done(function (items) {
2014-08-01 19:34:45 -07:00
self.playWithIntros(items, options, user);
2014-03-29 12:02:43 -07:00
});
2013-06-07 09:06:32 -07:00
2014-03-29 08:40:32 -07:00
} else {
2013-06-07 09:06:32 -07:00
2014-03-29 08:40:32 -07:00
self.getItemsForPlayback({
2013-06-07 09:06:32 -07:00
2014-03-29 08:40:32 -07:00
Ids: options.ids.join(',')
2014-03-29 08:40:32 -07:00
}).done(function (result) {
2013-05-24 19:45:12 -07:00
2014-03-29 12:02:43 -07:00
translateItemsForPlayback(result.Items).done(function (items) {
2014-08-01 19:34:45 -07:00
self.playWithIntros(items, options, user);
2014-03-29 12:02:43 -07:00
});
2013-06-07 09:06:32 -07:00
2014-03-29 08:40:32 -07:00
});
}
});
};
2014-08-01 19:34:45 -07:00
self.playWithIntros = function (items, options, user) {
var firstItem = items[0];
2015-07-09 20:00:03 -07:00
if (firstItem.MediaType === "Video") {
Dashboard.showModalLoadingMsg();
}
2015-07-03 04:51:45 -07:00
if (options.startPositionTicks || firstItem.MediaType !== 'Video') {
2014-08-01 19:34:45 -07:00
2015-03-03 00:03:17 -07:00
self.playInternal(firstItem, options.startPositionTicks, function () {
self.setPlaylistState(0, items);
2015-03-03 00:03:17 -07:00
});
2014-08-21 08:55:35 -07:00
return;
2014-08-01 19:34:45 -07:00
}
ApiClient.getJSON(ApiClient.getUrl('Users/' + user.Id + '/Items/' + firstItem.Id + '/Intros')).done(function (intros) {
items = intros.Items.concat(items);
2015-03-03 00:03:17 -07:00
self.playInternal(items[0], options.startPositionTicks, function () {
self.setPlaylistState(0, items);
2015-03-03 00:03:17 -07:00
});
2014-08-01 19:34:45 -07:00
});
};
2014-03-22 09:16:43 -07:00
function getOptimalMediaSource(mediaType, versions) {
2014-03-20 21:52:46 -07:00
var optimalVersion = versions.filter(function (v) {
2014-03-20 21:52:46 -07:00
2015-05-25 10:32:22 -07:00
v.enableDirectPlay = MediaController.supportsDirectPlay(v);
2014-03-20 21:52:46 -07:00
return v.enableDirectPlay;
2015-03-16 21:08:09 -07:00
})[0];
2015-03-07 22:37:48 -07:00
if (!optimalVersion) {
2015-03-07 22:37:48 -07:00
optimalVersion = versions.filter(function (v) {
return v.SupportsDirectStream;
2015-03-07 22:37:48 -07:00
2014-03-20 21:52:46 -07:00
})[0];
}
2014-03-20 20:31:40 -07:00
2015-03-03 00:03:17 -07:00
return optimalVersion || versions.filter(function (s) {
return s.SupportsTranscoding;
})[0];
2014-03-20 20:31:40 -07:00
}
2013-06-07 09:06:32 -07:00
2015-04-07 21:01:52 -07:00
self.createStreamInfo = function (type, item, mediaSource, startPosition) {
var mediaUrl;
var contentType;
var startTimeTicksOffset = 0;
var startPositionInSeekParam = startPosition ? (startPosition / 10000000) : 0;
var seekParam = startPositionInSeekParam ? '#t=' + startPositionInSeekParam : '';
2015-05-18 18:46:31 -07:00
var playMethod = 'Transcode';
2015-04-07 21:01:52 -07:00
if (type == 'Video') {
contentType = 'video/' + mediaSource.Container;
if (mediaSource.enableDirectPlay) {
2015-06-10 06:37:07 -07:00
mediaUrl = FileSystemBridge.translateFilePath(mediaSource.Path);
2015-05-18 18:46:31 -07:00
playMethod = 'DirectPlay';
2015-05-31 11:22:51 -07:00
} else {
if (mediaSource.SupportsDirectStream) {
mediaUrl = ApiClient.getUrl('Videos/' + item.Id + '/stream.' + mediaSource.Container, {
Static: true,
mediaSourceId: mediaSource.Id,
api_key: ApiClient.accessToken()
});
mediaUrl += seekParam;
2015-05-18 18:46:31 -07:00
playMethod = 'DirectStream';
} else {
2015-04-01 19:47:59 -07:00
mediaUrl = ApiClient.getUrl(mediaSource.TranscodingUrl);
if (mediaSource.TranscodingSubProtocol == 'hls') {
mediaUrl += seekParam;
contentType = 'application/x-mpegURL';
} else {
2015-07-03 04:51:45 -07:00
startTimeTicksOffset = startPosition || 0;
contentType = 'video/' + mediaSource.TranscodingContainer;
}
}
}
} else {
contentType = 'audio/' + mediaSource.Container;
if (mediaSource.enableDirectPlay) {
2015-06-10 06:37:07 -07:00
mediaUrl = FileSystemBridge.translateFilePath(mediaSource.Path);
2015-05-18 18:46:31 -07:00
playMethod = 'DirectPlay';
2015-05-31 11:22:51 -07:00
} else {
var isDirectStream = mediaSource.SupportsDirectStream;
if (isDirectStream) {
var outputContainer = (mediaSource.Container || '').toLowerCase();
mediaUrl = ApiClient.getUrl('Audio/' + item.Id + '/stream.' + outputContainer, {
mediaSourceId: mediaSource.Id,
deviceId: ApiClient.deviceId(),
api_key: ApiClient.accessToken()
});
mediaUrl += "&static=true" + seekParam;
2015-05-18 18:46:31 -07:00
playMethod = 'DirectStream';
} else {
2015-04-01 19:47:59 -07:00
mediaUrl = ApiClient.getUrl(mediaSource.TranscodingUrl);
2015-07-03 04:51:45 -07:00
if (mediaSource.TranscodingSubProtocol == 'hls') {
mediaUrl += seekParam;
contentType = 'application/x-mpegURL';
} else {
startTimeTicksOffset = startPosition || 0;
contentType = 'audio/' + mediaSource.TranscodingContainer;
}
}
}
}
return {
url: mediaUrl,
2015-05-25 10:32:22 -07:00
mimeType: contentType,
startTimeTicksOffset: startTimeTicksOffset,
2015-05-18 18:46:31 -07:00
startPositionInSeekParam: startPositionInSeekParam,
playMethod: playMethod
};
};
2015-03-03 00:03:17 -07:00
self.playInternal = function (item, startPosition, callback) {
2013-07-11 13:25:12 -07:00
if (item == null) {
throw new Error("item cannot be null");
}
2013-06-07 10:23:23 -07:00
if (self.isPlaying()) {
2015-06-25 14:50:56 -07:00
self.stop(false);
2013-04-10 14:03:28 -07:00
}
2013-04-08 21:16:32 -07:00
2015-03-07 22:37:48 -07:00
if (item.MediaType !== 'Audio' && item.MediaType !== 'Video') {
throw new Error("Unrecognized media type");
}
2015-03-25 21:44:24 -07:00
if (item.IsPlaceHolder) {
2015-07-09 20:00:03 -07:00
Dashboard.hideModalLoadingMsg();
2015-05-19 12:15:40 -07:00
MediaController.showPlaybackInfoErrorMessage('PlaceHolder');
2015-03-25 21:44:24 -07:00
return;
}
var deviceProfile = self.getDeviceProfile();
2015-05-06 20:11:51 -07:00
if (item.MediaType === "Video") {
Dashboard.showModalLoadingMsg();
}
2015-04-14 08:26:02 -07:00
2015-05-25 10:32:22 -07:00
MediaController.getPlaybackInfo(item.Id, deviceProfile, startPosition).done(function (playbackInfoResult) {
2015-03-07 22:37:48 -07:00
2015-04-13 09:21:10 -07:00
if (validatePlaybackInfoResult(playbackInfoResult)) {
2013-04-08 21:16:32 -07:00
2015-04-13 09:21:10 -07:00
var mediaSource = getOptimalMediaSource(item.MediaType, playbackInfoResult.MediaSources);
2014-11-18 19:45:12 -07:00
2015-03-07 22:37:48 -07:00
if (mediaSource) {
2015-02-05 22:39:07 -07:00
2015-03-29 09:45:16 -07:00
if (mediaSource.RequiresOpening) {
2014-11-18 19:45:12 -07:00
2015-05-25 10:32:22 -07:00
MediaController.getLiveStream(item.Id, playbackInfoResult.PlaySessionId, deviceProfile, startPosition, mediaSource, null, null).done(function (openLiveStreamResult) {
2015-03-03 00:03:17 -07:00
2015-05-25 10:32:22 -07:00
openLiveStreamResult.MediaSource.enableDirectPlay = MediaController.supportsDirectPlay(openLiveStreamResult.MediaSource);
2015-04-13 09:21:10 -07:00
2015-03-29 09:45:16 -07:00
playInternalPostMediaSourceSelection(item, openLiveStreamResult.MediaSource, startPosition, callback);
2015-03-28 21:56:39 -07:00
});
2015-03-03 00:03:17 -07:00
2015-03-28 21:56:39 -07:00
} else {
playInternalPostMediaSourceSelection(item, mediaSource, startPosition, callback);
2015-03-07 22:37:48 -07:00
}
} else {
2015-05-06 20:11:51 -07:00
Dashboard.hideModalLoadingMsg();
2015-05-19 12:15:40 -07:00
MediaController.showPlaybackInfoErrorMessage('NoCompatibleStream');
2015-03-03 00:03:17 -07:00
}
}
2014-03-20 20:31:40 -07:00
2015-03-07 22:37:48 -07:00
});
};
2015-03-28 21:56:39 -07:00
function playInternalPostMediaSourceSelection(item, mediaSource, startPosition, callback) {
2015-05-06 20:11:51 -07:00
Dashboard.hideModalLoadingMsg();
2015-04-14 08:26:02 -07:00
2015-03-28 21:56:39 -07:00
self.currentMediaSource = mediaSource;
self.currentItem = item;
if (item.MediaType === "Video") {
2015-07-05 11:34:52 -07:00
requirejs(['videorenderer'], function () {
2015-07-08 09:10:34 -07:00
self.playVideo(item, self.currentMediaSource, startPosition, callback);
2015-07-05 11:34:52 -07:00
});
2015-03-28 21:56:39 -07:00
} else if (item.MediaType === "Audio") {
2015-07-08 09:10:34 -07:00
playAudio(item, self.currentMediaSource, startPosition, callback);
2015-03-28 21:56:39 -07:00
}
}
2015-03-03 00:03:17 -07:00
function validatePlaybackInfoResult(result) {
2015-03-06 20:53:31 -07:00
if (result.ErrorCode) {
2015-05-19 12:15:40 -07:00
MediaController.showPlaybackInfoErrorMessage(result.ErrorCode);
2015-03-06 20:53:31 -07:00
return false;
}
2015-03-03 00:03:17 -07:00
return true;
}
2015-05-08 20:48:43 -07:00
self.getPosterUrl = function (item) {
2014-04-13 10:27:13 -07:00
2015-05-21 13:53:14 -07:00
// Safari often shows the poster under the video, which doesn't look good
if ($.browser.safari) {
return null;
}
2015-05-08 20:48:43 -07:00
var screenWidth = Math.max(screen.height, screen.width);
2014-04-13 10:27:13 -07:00
2015-05-08 20:48:43 -07:00
if (item.BackdropImageTags && item.BackdropImageTags.length) {
2014-04-13 10:27:13 -07:00
2015-05-08 20:48:43 -07:00
return ApiClient.getScaledImageUrl(item.Id, {
type: "Backdrop",
index: 0,
maxWidth: screenWidth,
tag: item.BackdropImageTags[0]
});
2014-04-13 10:27:13 -07:00
}
2015-05-08 20:48:43 -07:00
else if (item.ParentBackdropItemId && item.ParentBackdropImageTags && item.ParentBackdropImageTags.length) {
return ApiClient.getScaledImageUrl(item.ParentBackdropItemId, {
type: 'Backdrop',
index: 0,
maxWidth: screenWidth,
tag: item.ParentBackdropImageTags[0]
});
2014-04-13 10:27:13 -07:00
}
2015-05-08 20:48:43 -07:00
return null;
2014-04-13 10:27:13 -07:00
};
self.displayContent = function (options) {
2014-04-14 20:54:52 -07:00
2014-04-13 10:27:13 -07:00
// Handle it the same as a remote control command
2014-08-25 19:30:52 -07:00
Dashboard.onBrowseCommand(options);
2013-04-10 14:03:28 -07:00
};
2013-06-07 09:06:32 -07:00
self.getItemsForPlayback = function (query) {
var userId = Dashboard.getCurrentUserId();
2015-02-24 11:25:19 -07:00
if (query.Ids && query.Ids.split(',').length == 1) {
var deferred = DeferredBuilder.Deferred();
ApiClient.getItem(userId, query.Ids.split(',')).done(function (item) {
deferred.resolveWith(null, [
{
Items: [item],
TotalRecordCount: 1
}]);
});
2013-06-07 09:06:32 -07:00
2015-02-24 11:25:19 -07:00
return deferred.promise();
}
else {
query.Limit = query.Limit || 100;
query.Fields = getItemFields;
query.ExcludeLocationTypes = "Virtual";
return ApiClient.getItems(userId, query);
}
2013-06-07 09:06:32 -07:00
};
self.removeFromPlaylist = function (index) {
2013-06-07 10:23:23 -07:00
2013-06-07 09:06:32 -07:00
self.playlist.remove(index);
2013-06-07 10:23:23 -07:00
2013-05-23 13:09:01 -07:00
};
2013-06-07 09:06:32 -07:00
// Gets or sets the current playlist index
2013-06-07 10:23:23 -07:00
self.currentPlaylistIndex = function (i) {
2013-06-07 09:06:32 -07:00
if (i == null) {
return currentPlaylistIndex;
}
2013-05-23 13:09:01 -07:00
2013-06-07 09:06:32 -07:00
var newItem = self.playlist[i];
2013-05-23 13:09:01 -07:00
2015-03-03 00:03:17 -07:00
self.playInternal(newItem, 0, function () {
self.setPlaylistState(i);
2013-06-07 09:06:32 -07:00
});
2013-04-30 10:21:21 -07:00
};
// Set currentPlaylistIndex and playlist. Using a method allows for overloading in derived player implementations
self.setPlaylistState = function (i, items) {
if (!isNaN(i)) {
currentPlaylistIndex = i;
}
if (items) {
self.playlist = items;
}
if (self.updatePlaylistUi) {
self.updatePlaylistUi();
}
};
self.nextTrack = function () {
2013-05-23 13:09:01 -07:00
2013-06-07 09:06:32 -07:00
var newIndex = currentPlaylistIndex + 1;
var newItem = self.playlist[newIndex];
2013-05-23 13:09:01 -07:00
if (newItem) {
2014-07-17 15:21:35 -07:00
2015-06-26 20:27:38 -07:00
Logger.log('playing next track');
2014-08-10 15:13:17 -07:00
2015-03-03 00:03:17 -07:00
self.playInternal(newItem, 0, function () {
self.setPlaylistState(newIndex);
});
}
2013-05-23 13:09:01 -07:00
};
2013-08-09 08:55:22 -07:00
self.previousTrack = function () {
var newIndex = currentPlaylistIndex - 1;
if (newIndex >= 0) {
var newItem = self.playlist[newIndex];
if (newItem) {
2015-03-03 00:03:17 -07:00
self.playInternal(newItem, 0, function () {
self.setPlaylistState(newIndex);
});
}
}
};
2013-11-30 11:32:39 -07:00
self.queueItemsNext = function (items) {
var insertIndex = 1;
for (var i = 0, length = items.length; i < length; i++) {
self.playlist.splice(insertIndex, 0, items[i]);
insertIndex++;
}
};
2013-11-29 23:07:45 -07:00
self.queueItems = function (items) {
2013-06-07 09:06:32 -07:00
2013-11-29 23:07:45 -07:00
for (var i = 0, length = items.length; i < length; i++) {
2013-06-07 09:06:32 -07:00
2013-11-29 23:07:45 -07:00
self.playlist.push(items[i]);
}
2013-04-30 10:21:21 -07:00
};
2014-03-29 08:40:32 -07:00
self.queue = function (options) {
2013-06-07 09:06:32 -07:00
2015-06-07 13:06:18 -07:00
if (!self.playlist.length) {
2014-03-29 08:40:32 -07:00
self.play(options);
2014-01-18 10:58:05 -07:00
return;
}
2014-01-29 14:54:40 -07:00
2014-03-29 08:40:32 -07:00
Dashboard.getCurrentUser().done(function (user) {
2013-06-07 09:06:32 -07:00
2014-03-29 08:40:32 -07:00
if (options.items) {
2014-03-29 12:02:43 -07:00
translateItemsForPlayback(options.items).done(function (items) {
self.queueItems(items);
});
2014-03-29 08:40:32 -07:00
} else {
2013-06-07 09:06:32 -07:00
self.getItemsForPlayback({
2014-03-29 08:40:32 -07:00
Ids: options.ids.join(',')
2013-06-07 09:06:32 -07:00
}).done(function (result) {
2014-03-29 12:02:43 -07:00
translateItemsForPlayback(result.Items).done(function (items) {
2013-06-07 09:06:32 -07:00
2014-03-29 12:02:43 -07:00
self.queueItems(items);
});
2013-06-07 09:06:32 -07:00
2014-03-29 08:40:32 -07:00
});
2013-06-07 09:06:32 -07:00
}
2013-05-23 13:09:01 -07:00
});
};
2014-03-29 08:40:32 -07:00
self.queueNext = function (options) {
2013-08-08 07:53:25 -07:00
2015-06-07 13:06:18 -07:00
if (!self.playlist.length) {
2014-03-29 08:40:32 -07:00
self.play(options);
return;
}
2013-08-08 07:53:25 -07:00
2014-03-29 08:40:32 -07:00
Dashboard.getCurrentUser().done(function (user) {
2013-08-08 07:53:25 -07:00
2014-03-29 08:40:32 -07:00
if (options.items) {
2013-08-08 07:53:25 -07:00
2014-03-29 08:40:32 -07:00
self.queueItemsNext(options.items);
2013-08-08 07:53:25 -07:00
2014-03-29 08:40:32 -07:00
} else {
self.getItemsForPlayback({
2013-08-08 07:53:25 -07:00
2014-03-29 08:40:32 -07:00
Ids: options.ids.join(',')
}).done(function (result) {
options.items = result.Items;
self.queueItemsNext(options.items);
});
}
});
2013-08-08 07:53:25 -07:00
};
2013-05-23 13:09:01 -07:00
self.pause = function () {
2014-04-13 11:25:22 -07:00
2015-06-07 13:06:18 -07:00
self.currentMediaRenderer.pause();
2013-05-23 13:09:01 -07:00
};
self.unpause = function () {
2015-06-07 13:06:18 -07:00
self.currentMediaRenderer.unpause();
2013-05-23 13:09:01 -07:00
};
self.seek = function (position) {
2013-07-25 12:43:40 -07:00
self.changeStream(position);
};
2013-08-09 08:55:22 -07:00
2013-05-23 13:09:01 -07:00
self.mute = function () {
self.setVolume(0);
2013-05-23 13:09:01 -07:00
};
self.unMute = function () {
self.setVolume(self.getSavedVolume() * 100);
};
2014-11-13 23:27:10 -07:00
self.volume = function () {
2015-06-07 13:06:18 -07:00
return self.currentMediaRenderer.volume() * 100;
2014-10-22 21:26:01 -07:00
};
self.toggleMute = function () {
2015-06-07 13:06:18 -07:00
if (self.currentMediaRenderer) {
2015-06-26 20:27:38 -07:00
Logger.log('MediaPlayer toggling mute');
2014-04-30 20:24:55 -07:00
2014-10-22 21:26:01 -07:00
if (self.volume()) {
self.mute();
} else {
self.unMute();
}
}
};
2013-08-27 22:44:43 -07:00
self.volumeDown = function () {
2015-06-07 13:06:18 -07:00
if (self.currentMediaRenderer) {
2014-10-22 21:26:01 -07:00
self.setVolume(Math.max(self.volume() - 2, 0));
}
};
2013-08-27 22:44:43 -07:00
self.volumeUp = function () {
2015-06-07 13:06:18 -07:00
if (self.currentMediaRenderer) {
2014-10-22 21:26:01 -07:00
self.setVolume(Math.min(self.volume() + 2, 100));
}
};
// Sets volume using a 0-100 scale
self.setVolume = function (val) {
2015-06-07 13:06:18 -07:00
if (self.currentMediaRenderer) {
2015-06-26 20:27:38 -07:00
Logger.log('MediaPlayer setting volume to ' + val);
2015-06-07 13:06:18 -07:00
self.currentMediaRenderer.volume(val / 100);
2015-06-07 13:06:18 -07:00
self.onVolumeChanged(self.currentMediaRenderer);
2015-06-07 13:06:18 -07:00
//self.saveVolume();
}
2013-04-30 10:21:21 -07:00
};
self.saveVolume = function (val) {
if (val) {
2015-06-25 14:50:56 -07:00
appStorage.setItem("volume", val);
}
};
self.getSavedVolume = function () {
2015-06-25 14:50:56 -07:00
return appStorage.getItem("volume") || 0.5;
};
2014-03-29 08:40:32 -07:00
self.shuffle = function (id) {
var userId = Dashboard.getCurrentUserId();
ApiClient.getItem(userId, id).done(function (item) {
var query = {
UserId: userId,
Fields: getItemFields,
2014-11-02 20:38:43 -07:00
Limit: 100,
2014-03-29 08:40:32 -07:00
Filters: "IsNotFolder",
Recursive: true,
SortBy: "Random"
};
if (item.Type == "MusicArtist") {
2014-03-29 08:40:32 -07:00
query.MediaTypes = "Audio";
2015-03-13 10:25:28 -07:00
query.ArtistIds = item.Id;
2014-03-29 08:40:32 -07:00
}
else if (item.Type == "MusicGenre") {
query.MediaTypes = "Audio";
query.Genres = item.Name;
}
else if (item.IsFolder) {
query.ParentId = id;
}
else {
2014-03-29 08:40:32 -07:00
return;
}
self.getItemsForPlayback(query).done(function (result) {
self.play({ items: result.Items });
});
});
};
self.instantMix = function (id) {
var userId = Dashboard.getCurrentUserId();
ApiClient.getItem(userId, id).done(function (item) {
var promise;
2014-11-02 20:38:43 -07:00
var itemLimit = 100;
2014-03-29 08:40:32 -07:00
if (item.Type == "MusicArtist") {
2014-08-18 18:42:53 -07:00
promise = ApiClient.getInstantMixFromArtist({
2014-03-29 08:40:32 -07:00
UserId: Dashboard.getCurrentUserId(),
Fields: getItemFields,
2014-11-02 20:38:43 -07:00
Limit: itemLimit,
2014-08-18 18:42:53 -07:00
Id: id
2014-03-29 08:40:32 -07:00
});
}
else if (item.Type == "MusicGenre") {
2014-08-18 18:42:53 -07:00
promise = ApiClient.getInstantMixFromMusicGenre({
2014-03-29 08:40:32 -07:00
UserId: Dashboard.getCurrentUserId(),
Fields: getItemFields,
2014-11-02 20:38:43 -07:00
Limit: itemLimit,
2014-08-18 18:42:53 -07:00
Id: id
2014-03-29 08:40:32 -07:00
});
}
else if (item.Type == "MusicAlbum") {
promise = ApiClient.getInstantMixFromAlbum(id, {
2014-12-12 20:56:30 -07:00
UserId: Dashboard.getCurrentUserId(),
Fields: getItemFields,
Limit: itemLimit
});
}
else if (item.Type == "Playlist") {
promise = ApiClient.getInstantMixFromPlaylist(id, {
2014-03-29 08:40:32 -07:00
UserId: Dashboard.getCurrentUserId(),
Fields: getItemFields,
2014-11-02 20:38:43 -07:00
Limit: itemLimit
2014-03-29 08:40:32 -07:00
});
}
else if (item.Type == "Audio") {
promise = ApiClient.getInstantMixFromSong(id, {
UserId: Dashboard.getCurrentUserId(),
Fields: getItemFields,
2014-11-02 20:38:43 -07:00
Limit: itemLimit
2014-03-29 08:40:32 -07:00
});
}
else {
return;
}
promise.done(function (result) {
self.play({ items: result.Items });
});
});
};
2015-06-25 14:50:56 -07:00
self.stop = function (destroyRenderer) {
2013-03-31 18:52:07 -07:00
2015-06-07 13:06:18 -07:00
var mediaRenderer = self.currentMediaRenderer;
2013-03-31 18:52:07 -07:00
2015-06-07 13:06:18 -07:00
if (mediaRenderer) {
2013-02-20 18:33:05 -07:00
2015-06-10 20:01:41 -07:00
mediaRenderer.stop();
2015-06-29 11:45:42 -07:00
Events.off(mediaRenderer, 'ended', self.playNextAfterEnded);
2013-06-07 10:23:23 -07:00
2015-07-06 19:25:23 -07:00
$(mediaRenderer).one("ended", function () {
2015-06-28 07:45:21 -07:00
2015-06-29 11:45:42 -07:00
$(this).off('.mediaplayerevent');
2015-06-25 14:50:56 -07:00
this.cleanup(destroyRenderer);
2015-06-07 13:06:18 -07:00
self.currentMediaRenderer = null;
2014-07-01 14:13:32 -07:00
self.currentItem = null;
self.currentMediaSource = null;
2015-06-28 07:45:21 -07:00
});
2015-07-06 19:25:23 -07:00
2015-06-28 07:45:21 -07:00
Events.trigger(mediaRenderer, "ended");
2014-07-01 14:13:32 -07:00
} else {
2015-06-07 13:06:18 -07:00
self.currentMediaRenderer = null;
2014-06-11 12:31:33 -07:00
self.currentItem = null;
self.currentMediaSource = null;
2014-07-01 14:13:32 -07:00
}
2013-03-26 14:32:01 -07:00
2015-03-26 13:48:24 -07:00
if (self.isFullScreen()) {
self.exitFullScreen();
2013-12-29 19:41:22 -07:00
}
2015-03-26 13:48:24 -07:00
self.resetEnhancements();
2013-04-10 14:03:28 -07:00
};
2013-03-26 14:32:01 -07:00
2013-04-10 14:03:28 -07:00
self.isPlaying = function () {
2015-06-07 13:06:18 -07:00
return self.playlist.length > 0;
2013-04-10 14:03:28 -07:00
};
2013-05-25 17:53:51 -07:00
2014-04-13 10:27:13 -07:00
self.getPlayerState = function () {
var deferred = $.Deferred();
2015-06-07 13:06:18 -07:00
var result = self.getPlayerStateInternal(self.currentMediaRenderer, self.currentItem, self.currentMediaSource);
deferred.resolveWith(null, [result]);
return deferred.promise();
};
2015-06-07 13:06:18 -07:00
self.getPlayerStateInternal = function (mediaRenderer, item, mediaSource) {
2014-04-22 10:25:54 -07:00
var state = {
PlayState: {}
};
2015-06-07 13:06:18 -07:00
if (mediaRenderer) {
2015-06-07 13:06:18 -07:00
state.PlayState.VolumeLevel = mediaRenderer.volume() * 100;
state.PlayState.IsMuted = mediaRenderer.volume() == 0;
state.PlayState.IsPaused = mediaRenderer.paused();
state.PlayState.PositionTicks = self.getCurrentTicks(mediaRenderer);
2014-04-22 10:25:54 -07:00
2015-06-07 13:06:18 -07:00
var currentSrc = mediaRenderer.currentSrc();
2014-04-22 10:25:54 -07:00
if (currentSrc) {
var audioStreamIndex = getParameterByName('AudioStreamIndex', currentSrc);
if (audioStreamIndex) {
state.PlayState.AudioStreamIndex = parseInt(audioStreamIndex);
}
2014-06-11 12:31:33 -07:00
state.PlayState.SubtitleStreamIndex = self.currentSubtitleStreamIndex;
2014-04-22 10:25:54 -07:00
state.PlayState.PlayMethod = getParameterByName('static', currentSrc) == 'true' ?
'DirectStream' :
'Transcode';
2015-03-29 11:38:18 -07:00
state.PlayState.LiveStreamId = getParameterByName('LiveStreamId', currentSrc);
2015-04-01 14:55:50 -07:00
state.PlayState.PlaySessionId = getParameterByName('PlaySessionId', currentSrc);
2014-04-22 10:25:54 -07:00
}
}
if (mediaSource) {
2014-04-22 10:25:54 -07:00
state.PlayState.MediaSourceId = mediaSource.Id;
state.NowPlayingItem = {
RunTimeTicks: mediaSource.RunTimeTicks
};
2014-04-22 10:25:54 -07:00
state.PlayState.CanSeek = mediaSource.RunTimeTicks && mediaSource.RunTimeTicks > 0;
}
if (item) {
2015-05-25 10:32:22 -07:00
state.NowPlayingItem = self.getNowPlayingItemForReporting(item, mediaSource);
}
2014-04-27 18:57:29 -07:00
2015-05-25 10:32:22 -07:00
return state;
};
2014-04-22 10:25:54 -07:00
2015-05-25 10:32:22 -07:00
self.getNowPlayingItemForReporting = function (item, mediaSource) {
2015-05-25 10:32:22 -07:00
var nowPlayingItem = {};
2015-05-25 10:32:22 -07:00
nowPlayingItem.RunTimeTicks = mediaSource.RunTimeTicks;
2014-06-28 12:35:30 -07:00
2015-05-25 10:32:22 -07:00
nowPlayingItem.Id = item.Id;
nowPlayingItem.MediaType = item.MediaType;
nowPlayingItem.Type = item.Type;
nowPlayingItem.Name = item.Name;
2015-05-25 10:32:22 -07:00
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-04-13 10:27:13 -07:00
2015-05-25 10:32:22 -07:00
var imageTags = item.ImageTags || {};
2014-04-13 10:27:13 -07:00
2015-05-25 10:32:22 -07:00
if (item.SeriesPrimaryImageTag) {
2015-05-25 10:32:22 -07:00
nowPlayingItem.PrimaryImageItemId = item.SeriesId;
nowPlayingItem.PrimaryImageTag = item.SeriesPrimaryImageTag;
}
else if (imageTags.Primary) {
2015-05-25 10:32:22 -07:00
nowPlayingItem.PrimaryImageItemId = item.Id;
nowPlayingItem.PrimaryImageTag = imageTags.Primary;
}
else if (item.AlbumPrimaryImageTag) {
2015-05-25 10:32:22 -07:00
nowPlayingItem.PrimaryImageItemId = item.AlbumId;
nowPlayingItem.PrimaryImageTag = item.AlbumPrimaryImageTag;
}
else if (item.SeriesPrimaryImageTag) {
2015-05-25 10:32:22 -07:00
nowPlayingItem.PrimaryImageItemId = item.SeriesId;
nowPlayingItem.PrimaryImageTag = item.SeriesPrimaryImageTag;
}
2014-06-28 12:35:30 -07:00
2015-05-25 10:32:22 -07:00
if (item.BackdropImageTags && item.BackdropImageTags.length) {
2014-06-28 12:35:30 -07:00
2015-05-25 10:32:22 -07:00
nowPlayingItem.BackdropItemId = item.Id;
nowPlayingItem.BackdropImageTag = item.BackdropImageTags[0];
}
else if (item.ParentBackdropImageTags && item.ParentBackdropImageTags.length) {
nowPlayingItem.BackdropItemId = item.ParentBackdropItemId;
nowPlayingItem.BackdropImageTag = item.ParentBackdropImageTags[0];
}
2014-06-28 12:35:30 -07:00
2015-05-25 10:32:22 -07:00
if (imageTags.Thumb) {
nowPlayingItem.ThumbItemId = item.Id;
nowPlayingItem.ThumbImageTag = imageTags.Thumb;
}
2015-05-25 10:32:22 -07:00
if (imageTags.Logo) {
nowPlayingItem.LogoItemId = item.Id;
nowPlayingItem.LogoImageTag = imageTags.Logo;
}
else if (item.ParentLogoImageTag) {
nowPlayingItem.LogoItemId = item.ParentLogoItemId;
nowPlayingItem.LogoImageTag = item.ParentLogoImageTag;
}
return nowPlayingItem;
};
self.beginPlayerUpdates = function () {
// Nothing to setup here
};
self.endPlayerUpdates = function () {
// Nothing to setup here
};
2015-06-07 13:06:18 -07:00
self.onPlaybackStart = function (mediaRenderer, item, mediaSource) {
2015-06-07 13:06:18 -07:00
self.updateCanClientSeek(mediaRenderer);
2015-06-07 13:06:18 -07:00
var state = self.getPlayerStateInternal(mediaRenderer, item, mediaSource);
2015-06-28 07:45:21 -07:00
Events.trigger(self, 'playbackstart', [state]);
2014-05-07 11:38:50 -07:00
self.startProgressInterval();
};
2015-06-07 13:06:18 -07:00
self.onVolumeChanged = function (mediaRenderer) {
2015-06-07 13:06:18 -07:00
self.saveVolume(mediaRenderer.volume());
2015-06-07 13:06:18 -07:00
var state = self.getPlayerStateInternal(mediaRenderer, self.currentItem, self.currentMediaSource);
2015-06-28 07:45:21 -07:00
Events.trigger(self, 'volumechange', [state]);
};
2014-08-14 06:24:30 -07:00
self.cleanup = function () {
2014-08-10 15:13:17 -07:00
};
self.onPlaybackStopped = function () {
2015-06-26 20:27:38 -07:00
Logger.log('playback stopped');
2014-08-10 15:13:17 -07:00
2015-06-28 07:45:21 -07:00
document.body.classList.remove('bodyWithPopupOpen');
2014-06-28 12:35:30 -07:00
2015-06-07 13:06:18 -07:00
var mediaRenderer = this;
2015-06-28 07:45:21 -07:00
Events.off(mediaRenderer, '.mediaplayerevent');
2015-07-06 19:25:23 -07:00
2015-06-29 11:45:42 -07:00
Events.off(mediaRenderer, 'ended', self.onPlaybackStopped);
2015-07-03 04:51:45 -07:00
var item = self.currentItem;
var mediaSource = self.currentMediaSource;
var state = self.getPlayerStateInternal(mediaRenderer, item, mediaSource);
2015-06-07 13:06:18 -07:00
self.cleanup(mediaRenderer);
2014-08-10 15:13:17 -07:00
clearProgressInterval();
if (item.MediaType == "Video") {
2015-03-20 10:29:18 -07:00
if (self.isFullScreen()) {
self.exitFullScreen();
}
self.resetEnhancements();
}
2015-06-28 07:45:21 -07:00
Events.trigger(self, 'playbackstop', [state]);
};
2015-06-07 13:06:18 -07:00
self.onPlaystateChange = function (mediaRenderer) {
2015-06-07 13:06:18 -07:00
var state = self.getPlayerStateInternal(mediaRenderer, self.currentItem, self.currentMediaSource);
2015-06-28 07:45:21 -07:00
Events.trigger(self, 'playstatechange', [state]);
};
2015-06-28 07:45:21 -07:00
Events.on(window, "beforeunload", function () {
// Try to report playback stopped before the browser closes
2015-06-07 13:06:18 -07:00
if (self.currentItem && self.currentMediaRenderer && currentProgressInterval) {
2015-06-07 13:06:18 -07:00
self.onPlaybackStopped.call(self.currentMediaRenderer);
}
});
2014-05-07 11:38:50 -07:00
function sendProgressUpdate() {
2015-07-09 20:00:03 -07:00
var mediaRenderer = self.currentMediaRenderer;
if (mediaRenderer.enableProgressReporting === false) {
return;
}
var state = self.getPlayerStateInternal(mediaRenderer, self.currentItem, self.currentMediaSource);
2014-06-11 19:38:40 -07:00
2014-05-07 11:38:50 -07:00
var info = {
QueueableMediaTypes: state.NowPlayingItem.MediaType,
ItemId: state.NowPlayingItem.Id,
NowPlayingItem: state.NowPlayingItem
};
2014-04-30 08:07:02 -07:00
2014-05-07 11:38:50 -07:00
info = $.extend(info, state.PlayState);
ApiClient.reportPlaybackProgress(info);
2014-04-02 14:55:19 -07:00
}
function clearProgressInterval() {
if (currentProgressInterval) {
clearTimeout(currentProgressInterval);
currentProgressInterval = null;
}
2014-04-30 08:07:02 -07:00
}
2015-06-07 13:06:18 -07:00
self._canPlayWebm = null;
self.canPlayWebm = function () {
2015-06-07 18:23:56 -07:00
if (self._canPlayWebm == null) {
2015-07-06 19:25:23 -07:00
self._canPlayWebm = ($.browser.android && AppInfo.isNativeApp) || document.createElement('video').canPlayType('video/webm').replace(/no/, '');
2015-06-07 18:23:56 -07:00
}
2015-06-07 13:06:18 -07:00
return self._canPlayWebm;
2014-08-14 06:24:30 -07:00
};
2014-04-14 20:54:52 -07:00
self.canAutoPlayAudio = function () {
2015-05-28 16:37:43 -07:00
if (AppInfo.isNativeApp) {
2015-05-02 09:34:27 -07:00
return true;
}
2015-05-09 21:29:04 -07:00
if ($.browser.mobile) {
2014-04-14 20:54:52 -07:00
return false;
}
return true;
};
2015-05-22 12:16:14 -07:00
function onTimeUpdate() {
var currentTicks = self.getCurrentTicks(this);
self.setCurrentTime(currentTicks);
}
2015-07-08 09:10:34 -07:00
function playAudio(item, mediaSource, startPositionTicks, callback) {
2015-03-07 22:37:48 -07:00
2015-06-10 06:37:07 -07:00
requirejs(['audiorenderer'], function () {
2015-06-07 20:16:42 -07:00
playAudioInternal(item, mediaSource, startPositionTicks);
2015-07-08 09:10:34 -07:00
if (callback) {
callback();
}
2015-06-07 20:16:42 -07:00
});
}
function playAudioInternal(item, mediaSource, startPositionTicks) {
2015-04-07 21:01:52 -07:00
var streamInfo = self.createStreamInfo('Audio', item, mediaSource, startPositionTicks);
var audioUrl = streamInfo.url;
self.startTimeTicksOffset = streamInfo.startTimeTicksOffset;
2014-04-13 10:27:13 -07:00
var initialVolume = self.getSavedVolume();
2015-07-08 17:20:01 -07:00
var mediaRenderer = new AudioRenderer({
poster: self.getPosterUrl(item)
});
2015-06-10 20:01:41 -07:00
// Set volume first to avoid an audible change
2015-06-07 13:06:18 -07:00
mediaRenderer.volume(initialVolume);
2015-06-23 15:13:06 -07:00
mediaRenderer.setCurrentSrc(audioUrl, item, mediaSource);
2015-07-06 19:25:23 -07:00
Events.on(mediaRenderer, "volumechange.mediaplayerevent", function () {
2015-06-26 20:27:38 -07:00
Logger.log('audio element event: volumechange');
2014-08-10 15:13:17 -07:00
self.onVolumeChanged(this);
2015-06-28 07:45:21 -07:00
});
$(mediaRenderer).one("playing.mediaplayerevent", function () {
2015-06-26 20:27:38 -07:00
Logger.log('audio element event: playing');
2014-08-10 15:13:17 -07:00
// For some reason this is firing at the start, so don't bind until playback has begun
2015-06-29 11:45:42 -07:00
Events.on(this, 'ended', self.onPlaybackStopped);
$(this).one('ended', self.playNextAfterEnded);
2014-08-10 15:13:17 -07:00
self.onPlaybackStart(this, item, mediaSource);
2014-06-21 22:52:31 -07:00
}).on("pause.mediaplayerevent", function () {
2015-06-26 20:27:38 -07:00
Logger.log('audio element event: pause');
2014-08-10 15:13:17 -07:00
self.onPlaystateChange(this);
2014-11-13 23:27:10 -07:00
// In the event timeupdate isn't firing, at least we can update when this happens
self.setCurrentTime(self.getCurrentTicks());
2014-06-21 22:52:31 -07:00
}).on("playing.mediaplayerevent", function () {
2015-06-26 20:27:38 -07:00
Logger.log('audio element event: playing');
2014-08-10 15:13:17 -07:00
self.onPlaystateChange(this);
2014-11-13 23:27:10 -07:00
// In the event timeupdate isn't firing, at least we can update when this happens
self.setCurrentTime(self.getCurrentTicks());
2015-06-07 13:06:18 -07:00
}).on("timeupdate.mediaplayerevent", onTimeUpdate);
2015-06-07 20:16:42 -07:00
self.currentMediaRenderer = mediaRenderer;
self.currentDurationTicks = self.currentMediaSource.RunTimeTicks;
}
2014-03-22 09:16:43 -07:00
var getItemFields = "MediaSources,Chapters";
2015-05-18 09:40:20 -07:00
self.tryPair = function (target) {
var deferred = $.Deferred();
deferred.resolve();
return deferred.promise();
};
2013-04-10 14:03:28 -07:00
}
2013-03-26 14:32:01 -07:00
2013-04-10 14:03:28 -07:00
window.MediaPlayer = new mediaPlayer();
2013-04-08 21:26:02 -07:00
2015-05-21 13:53:14 -07:00
Dashboard.ready(function () {
2015-05-19 12:15:40 -07:00
window.MediaController.registerPlayer(window.MediaPlayer);
window.MediaController.setActivePlayer(window.MediaPlayer, window.MediaPlayer.getTargets()[0]);
});
2014-03-29 08:40:32 -07:00
2015-06-25 14:50:56 -07:00
})(document, setTimeout, clearTimeout, screen, $, setInterval, window);