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

1703 lines
52 KiB
JavaScript
Raw Normal View History

2014-07-15 12:16:16 -07:00
(function (document, setTimeout, clearTimeout, screen, store, $, 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 testableVideoElement = document.createElement('video');
var currentProgressInterval;
2014-01-10 06:52:01 -07:00
var canClientSeek;
var currentPlaylistIndex = 0;
2013-05-23 13:09:01 -07:00
2014-06-23 21:18:02 -07:00
self.currentMediaElement = 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/, '');
self.getVideoQualityOptions = function (videoWidth) {
var bitrateSetting = AppSettings.maxStreamingBitrate();
var maxAllowedWidth = videoWidth || 4096;
var options = [];
// Some 1080- videos are reported as 1912?
if (maxAllowedWidth >= 1900) {
options.push({ name: '1080p - 30Mbps', maxWidth: 1920, bitrate: 30000000 });
options.push({ name: '1080p - 25Mbps', maxWidth: 1920, bitrate: 25000000 });
options.push({ name: '1080p - 20Mbps', maxWidth: 1920, bitrate: 20000000 });
options.push({ name: '1080p - 15Mbps', maxWidth: 1920, bitrate: 15000000 });
options.push({ name: '1080p - 10Mbps', maxWidth: 1920, bitrate: 10000000 });
options.push({ name: '1080p - 8Mbps', maxWidth: 1920, bitrate: 8000000 });
options.push({ name: '1080p - 6Mbps', maxWidth: 1920, bitrate: 6000000 });
options.push({ name: '1080p - 5Mbps', maxWidth: 1920, bitrate: 5000000 });
} else if (maxAllowedWidth >= 1260) {
options.push({ name: '720p - 10Mbps', maxWidth: 1280, bitrate: 10000000 });
options.push({ name: '720p - 8Mbps', maxWidth: 1280, bitrate: 8000000 });
options.push({ name: '720p - 6Mbps', maxWidth: 1280, bitrate: 6000000 });
options.push({ name: '720p - 5Mbps', maxWidth: 1280, bitrate: 5000000 });
} else if (maxAllowedWidth >= 460) {
options.push({ name: '480p - 4Mbps', maxWidth: 720, bitrate: 4000000 });
options.push({ name: '480p - 3Mbps', maxWidth: 720, bitrate: 3000000 });
options.push({ name: '480p - 2.5Mbps', maxWidth: 720, bitrate: 2500000 });
options.push({ name: '480p - 2Mbps', maxWidth: 720, bitrate: 2000000 });
options.push({ name: '480p - 1.5Mbps', maxWidth: 720, bitrate: 1500000 });
}
if (maxAllowedWidth >= 1260) {
options.push({ name: '720p - 4Mbps', maxWidth: 1280, bitrate: 4000000 });
options.push({ name: '720p - 3Mbps', maxWidth: 1280, bitrate: 3000000 });
options.push({ name: '720p - 2Mbps', maxWidth: 1280, bitrate: 2000000 });
// The extra 1 is because they're keyed off the bitrate value
2015-04-10 19:43:30 -07:00
options.push({ name: '720p - 1.5Mbps', maxWidth: 1280, bitrate: 1500001 });
options.push({ name: '720p - 1Mbps', maxWidth: 1280, bitrate: 1000001 });
}
options.push({ name: '480p - 1.0Mbps', maxWidth: 720, bitrate: 1000000 });
options.push({ name: '480p - 720kbps', maxWidth: 720, bitrate: 720000 });
options.push({ name: '480p - 420kbps', maxWidth: 720, bitrate: 420000 });
options.push({ name: '360p', maxWidth: 640, bitrate: 400000 });
options.push({ name: '240p', maxWidth: 426, bitrate: 320000 });
2015-03-29 20:04:55 -07:00
options.push({ name: '144p', maxWidth: 256, 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;
};
self.getDeviceProfile = function () {
var qualityOption = self.getVideoQualityOptions().filter(function (q) {
return q.selected;
})[0];
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'
//});
}
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-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({
Container: 'aac',
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-24 11:33:28 -07:00
if (canPlayAac && $.browser.safari) {
profile.TranscodingProfiles.push({
Container: 'aac',
Type: 'Audio',
AudioCodec: 'aac',
Context: 'Streaming',
Protocol: 'http'
});
} else {
profile.TranscodingProfiles.push({
Container: 'mp3',
Type: 'Audio',
AudioCodec: 'mp3',
Context: 'Streaming',
Protocol: 'http'
});
}
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-05-06 20:11:51 -07:00
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
]
});
profile.CodecProfiles.push({
Type: 'Video',
Codec: 'h264',
Conditions: [
{
Condition: 'NotEquals',
Property: 'IsAnamorphic',
2015-03-26 16:10:34 -07:00
Value: 'true',
IsRequired: false
},
{
Condition: 'EqualsAny',
Property: 'VideoProfile',
Value: 'high|main|baseline|constrained baseline'
},
{
Condition: 'LessThanEqual',
Property: 'VideoLevel',
Value: '41'
},
{
Condition: 'LessThanEqual',
Property: 'Width',
Value: qualityOption.maxWidth
}]
});
profile.CodecProfiles.push({
Type: 'Video',
Codec: 'vpx',
Conditions: [
{
Condition: 'NotEquals',
Property: 'IsAnamorphic',
2015-03-26 16:10:34 -07:00
Value: 'true',
IsRequired: false
},
{
Condition: 'LessThanEqual',
Property: 'Width',
Value: qualityOption.maxWidth
}]
});
// 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;
};
self.updateCanClientSeek = function (elem) {
2014-10-07 18:37:45 -07:00
2014-01-10 06:52:01 -07:00
var duration = elem.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
2014-10-22 21:26:01 -07:00
self.getCurrentSrc = function (mediaElement) {
return mediaElement.currentSrc;
};
self.getCurrentTicks = function (mediaElement) {
2014-06-13 07:24:14 -07:00
2014-06-23 21:18:02 -07:00
var playerTime = Math.floor(10000000 * (mediaElement || self.currentMediaElement).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;
self.lastProgressReport = 0;
2013-05-23 13:09:01 -07:00
currentProgressInterval = setInterval(function () {
2014-06-23 21:18:02 -07:00
if (self.currentMediaElement) {
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('.'));
};
self.canPlayHls = function () {
var media = testableVideoElement;
// safari
if (media.canPlayType('application/x-mpegURL').replace(/no/, '') ||
media.canPlayType('application/vnd.apple.mpegURL').replace(/no/, '')) {
return true;
}
return false;
};
self.changeStream = function (ticks, params) {
2013-05-23 18:47:07 -07:00
2014-06-23 21:18:02 -07:00
var element = self.currentMediaElement;
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
2013-05-25 18:53:34 -07:00
element.currentTime = ticks / (1000 * 10000);
return;
}
2013-05-23 18:47:07 -07:00
params = params || {};
2013-05-23 18:47:07 -07:00
var currentSrc = element.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-03-29 11:31:28 -07:00
changeStreamToUrl(element, 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-03-29 11:31:28 -07:00
changeStreamToUrl(element, playSessionId, currentSrc, ticks);
2015-03-26 12:18:21 -07:00
}
});
};
2013-05-23 18:47:07 -07:00
2015-03-29 11:31:28 -07:00
function changeStreamToUrl(element, playSessionId, url, newPositionTicks) {
2014-01-10 06:52:01 -07:00
clearProgressInterval();
2015-05-13 20:24:25 -07:00
$(element).off('ended.playbackstopped').off('ended.playnext').one("loadedmetadata.mediaplayerevent", function (e) {
// The IE video player won't autoplay without this
if ($.browser.msie && self.currentItem.MediaType == "Video") {
this.play();
}
}).one("play", function () {
2013-05-23 18:47:07 -07:00
self.updateCanClientSeek(this);
2013-05-23 18:47:07 -07:00
$(this).on('ended.playbackstopped', self.onPlaybackStopped).one('ended.playnext', 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;
element.src = url;
});
self.updateTextStreamUrls(newPositionTicks || 0);
} else {
self.startTimeTicksOffset = newPositionTicks;
element.src = url;
element.play();
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;
positionSlider.val(percent).slider('enable').slider('refresh');
2013-04-30 12:13:06 -07:00
}
2013-05-24 07:35:15 -07:00
} else {
if (positionSlider) {
positionSlider.slider('disable').slider('refresh');
}
}
if (currentTimeElement) {
currentTimeElement.html(timeText);
2013-05-24 07:35:15 -07:00
}
2013-05-23 13:09:01 -07:00
2014-06-23 21:18:02 -07:00
var state = self.getPlayerStateInternal(self.currentMediaElement, self.currentItem, self.currentMediaSource);
$(self).trigger('positionchange', [state]);
};
2013-04-30 10:21:21 -07:00
2014-06-13 07:24:14 -07:00
var supportsTextTracks;
2014-06-11 19:38:40 -07:00
self.supportsTextTracks = function () {
2014-06-13 07:24:14 -07:00
if (supportsTextTracks == null) {
supportsTextTracks = document.createElement('video').textTracks != null;
}
// For now, until ready
return supportsTextTracks;
2014-06-11 19:38:40 -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
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];
if (options.startPositionTicks || firstItem.MediaType !== 'Video' || !self.canAutoPlayVideo()) {
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) {
mediaUrl = mediaSource.Path;
2015-05-18 18:46:31 -07:00
playMethod = 'DirectPlay';
} 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 {
startTimeTicksOffset = startPosition || 0;
2015-04-01 19:47:59 -07:00
mediaUrl = ApiClient.getUrl(mediaSource.TranscodingUrl);
if (mediaSource.TranscodingSubProtocol == 'hls') {
mediaUrl += seekParam;
contentType = 'application/x-mpegURL';
} else {
contentType = 'video/' + mediaSource.TranscodingContainer;
}
}
}
} else {
contentType = 'audio/' + mediaSource.Container;
if (mediaSource.enableDirectPlay) {
mediaUrl = mediaSource.Path;
2015-05-18 18:46:31 -07:00
playMethod = 'DirectPlay';
} 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 {
contentType = 'audio/' + mediaSource.TranscodingContainer;
2015-04-01 19:47:59 -07:00
mediaUrl = ApiClient.getUrl(mediaSource.TranscodingUrl);
}
startTimeTicksOffset = startPosition || 0;
}
}
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()) {
2013-04-10 14:03:28 -07:00
self.stop();
}
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-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-04-28 06:56:57 -07:00
self.playVideo(item, self.currentMediaSource, startPosition);
2015-03-28 21:56:39 -07:00
} else if (item.MediaType === "Audio") {
self.currentMediaElement = playAudio(item, self.currentMediaSource, startPosition);
self.currentDurationTicks = self.currentMediaSource.RunTimeTicks;
}
if (callback) {
callback();
}
}
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
2014-08-10 15:13:17 -07:00
console.log('playing next track');
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
2014-06-23 21:18:02 -07:00
if (!self.currentMediaElement) {
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
2014-06-23 21:18:02 -07:00
if (!self.currentMediaElement) {
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
2014-06-23 21:18:02 -07:00
self.currentMediaElement.pause();
2013-05-23 13:09:01 -07:00
};
self.unpause = function () {
2014-06-23 21:18:02 -07:00
self.currentMediaElement.play();
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 () {
2014-10-22 21:26:01 -07:00
return self.currentMediaElement.volume * 100;
};
self.toggleMute = function () {
2014-06-23 21:18:02 -07:00
if (self.currentMediaElement) {
2014-04-30 20:24:55 -07:00
console.log('MediaPlayer toggling mute');
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 () {
2014-06-23 21:18:02 -07:00
if (self.currentMediaElement) {
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 () {
2014-06-23 21:18:02 -07:00
if (self.currentMediaElement) {
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) {
2014-06-23 21:18:02 -07:00
if (self.currentMediaElement) {
2014-04-30 20:24:55 -07:00
console.log('MediaPlayer setting volume to ' + val);
2014-06-23 21:18:02 -07:00
self.currentMediaElement.volume = val / 100;
2014-06-23 21:18:02 -07:00
self.onVolumeChanged(self.currentMediaElement);
self.saveVolume();
}
2013-04-30 10:21:21 -07:00
};
self.saveVolume = function (val) {
if (val) {
2014-07-15 12:16:16 -07:00
store.setItem("volume", val);
}
};
self.getSavedVolume = function () {
2014-07-15 12:16:16 -07:00
return store.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 });
});
});
};
2013-04-10 14:03:28 -07:00
self.stop = function () {
2013-03-31 18:52:07 -07:00
2014-06-23 21:18:02 -07:00
var elem = self.currentMediaElement;
2013-03-31 18:52:07 -07:00
2014-07-01 14:13:32 -07:00
if (elem) {
2013-02-20 18:33:05 -07:00
2014-07-01 14:13:32 -07:00
elem.pause();
2014-07-01 14:13:32 -07:00
$(elem).off("ended.playnext").one("ended", function () {
2013-06-07 10:23:23 -07:00
2014-07-01 14:13:32 -07:00
$(this).off();
2014-07-01 14:13:32 -07:00
if (this.tagName.toLowerCase() != 'audio') {
$(this).remove();
}
2014-07-17 15:21:35 -07:00
elem.src = null;
2014-07-01 14:13:32 -07:00
elem.src = "";
self.currentMediaElement = null;
self.currentItem = null;
self.currentMediaSource = null;
2014-07-21 18:29:06 -07:00
// When the browser regains focus it may start auto-playing the last video
if ($.browser.safari) {
elem.src = 'files/dummy.mp4';
elem.play();
}
2014-07-01 14:13:32 -07:00
}).trigger("ended");
} else {
2014-06-23 21:18:02 -07:00
self.currentMediaElement = 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 () {
2014-06-23 21:18:02 -07:00
return self.currentMediaElement != null;
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();
2014-06-23 21:18:02 -07:00
var result = self.getPlayerStateInternal(self.currentMediaElement, self.currentItem, self.currentMediaSource);
deferred.resolveWith(null, [result]);
return deferred.promise();
};
self.getPlayerStateInternal = function (playerElement, item, mediaSource) {
2014-04-22 10:25:54 -07:00
var state = {
PlayState: {}
};
if (playerElement) {
2014-04-22 10:25:54 -07:00
state.PlayState.VolumeLevel = playerElement.volume * 100;
state.PlayState.IsMuted = playerElement.volume == 0;
state.PlayState.IsPaused = playerElement.paused;
state.PlayState.PositionTicks = self.getCurrentTicks(playerElement);
var currentSrc = playerElement.currentSrc;
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
};
self.onPlaybackStart = function (playerElement, item, mediaSource) {
self.updateCanClientSeek(playerElement);
var state = self.getPlayerStateInternal(playerElement, item, mediaSource);
$(self).trigger('playbackstart', [state]);
2014-05-07 11:38:50 -07:00
self.startProgressInterval();
};
self.onVolumeChanged = function (playerElement) {
self.saveVolume(playerElement.volume);
2014-06-11 12:31:33 -07:00
var state = self.getPlayerStateInternal(playerElement, self.currentItem, self.currentMediaSource);
$(self).trigger('volumechange', [state]);
};
2014-08-14 06:24:30 -07:00
self.cleanup = function () {
2014-08-10 15:13:17 -07:00
};
self.onPlaybackStopped = function () {
2014-08-10 15:13:17 -07:00
console.log('playback stopped');
2014-06-28 12:35:30 -07:00
$('body').removeClass('bodyWithPopupOpen');
var playerElement = this;
2014-06-21 22:52:31 -07:00
$(playerElement).off('.mediaplayerevent').off('ended.playbackstopped');
2014-08-10 15:13:17 -07:00
self.cleanup(playerElement);
clearProgressInterval();
2014-06-11 12:31:33 -07:00
var item = self.currentItem;
var mediaSource = self.currentMediaSource;
if (item.MediaType == "Video") {
2015-03-20 10:29:18 -07:00
if (self.isFullScreen()) {
self.exitFullScreen();
}
self.resetEnhancements();
}
var state = self.getPlayerStateInternal(playerElement, item, mediaSource);
$(self).trigger('playbackstop', [state]);
};
self.onPlaystateChange = function (playerElement) {
2014-06-11 12:31:33 -07:00
var state = self.getPlayerStateInternal(playerElement, self.currentItem, self.currentMediaSource);
$(self).trigger('playstatechange', [state]);
};
$(window).on("beforeunload", function () {
// Try to report playback stopped before the browser closes
2014-06-23 21:18:02 -07:00
if (self.currentItem && self.currentMediaElement && currentProgressInterval) {
2014-06-23 21:18:02 -07:00
self.onPlaybackStopped.call(self.currentMediaElement);
}
});
2014-05-07 11:38:50 -07:00
function sendProgressUpdate() {
2015-03-29 11:35:18 -07:00
var element = self.currentMediaElement;
var state = self.getPlayerStateInternal(element, 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
}
self.canPlayWebm = function () {
return testableVideoElement.canPlayType('video/webm').replace(/no/, '');
2014-08-14 06:24:30 -07:00
};
2014-04-14 20:54:52 -07:00
self.canAutoPlayAudio = function () {
2015-05-02 09:34:27 -07:00
if (Dashboard.isRunningInCordova()) {
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;
};
function getAudioElement() {
var elem = $('.mediaPlayerAudio');
if (elem.length) {
return elem;
}
var html = '';
2014-04-14 20:54:52 -07:00
var requiresControls = !self.canAutoPlayAudio();
if (requiresControls) {
html += '<div class="mediaPlayerAudioContainer"><div class="mediaPlayerAudioContainerInner">';;
} else {
html += '<div class="mediaPlayerAudioContainer" style="display:none;"><div class="mediaPlayerAudioContainerInner">';;
}
2015-04-06 13:43:40 -07:00
html += '<audio class="mediaPlayerAudio" crossorigin="anonymous" controls>';
html += '</audio></div></div>';
$(document.body).append(html);
return $('.mediaPlayerAudio');
2014-04-02 14:55:19 -07:00
}
2015-05-22 12:16:14 -07:00
function onTimeUpdate() {
var currentTicks = self.getCurrentTicks(this);
self.setCurrentTime(currentTicks);
}
function playAudio(item, mediaSource, startPositionTicks) {
2015-03-07 22:37:48 -07:00
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();
return getAudioElement().each(function () {
2014-04-13 10:27:13 -07:00
this.src = audioUrl;
this.volume = initialVolume;
2015-05-08 20:48:43 -07:00
this.poster = self.getPosterUrl(item);
this.play();
2014-06-21 22:52:31 -07:00
}).on("volumechange.mediaplayerevent", function () {
2014-08-10 15:13:17 -07:00
console.log('audio element event: volumechange');
self.onVolumeChanged(this);
2014-06-21 22:52:31 -07:00
}).one("playing.mediaplayerevent", function () {
2014-08-10 15:13:17 -07:00
console.log('audio element event: playing');
$('.mediaPlayerAudioContainer').hide();
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
$(this).on("ended.playbackstopped", self.onPlaybackStopped).one('ended.playnext', self.playNextAfterEnded);
self.onPlaybackStart(this, item, mediaSource);
2014-06-21 22:52:31 -07:00
}).on("pause.mediaplayerevent", function () {
2014-08-10 15:13:17 -07:00
console.log('audio element event: pause');
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 () {
2014-08-10 15:13:17 -07:00
console.log('audio element event: playing');
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-05-22 12:16:14 -07:00
}).on("timeupdate.mediaplayerevent", onTimeUpdate)[0];
};
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-05-20 10:29:26 -07:00
})(document, setTimeout, clearTimeout, screen, window.appStorage, $, setInterval, window);