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

1736 lines
54 KiB
JavaScript
Raw Normal View History

2015-12-14 08:43:03 -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;
2015-09-23 09:42:35 -07:00
var currentPlaylistIndex = -1;
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;
2015-09-15 11:09:44 -07:00
self.streamInfo = {};
2014-07-01 14:13:32 -07:00
2014-03-29 09:58:49 -07:00
self.name = 'Html5 Player';
self.getTargets = function () {
2015-12-30 13:25:17 -07:00
return new Promise(function (resolve, reject) {
resolve(self.getTargetsInternal());
});
};
self.getTargetsInternal = function () {
2014-03-29 09:58:49 -07:00
var targets = [{
2015-05-06 20:11:51 -07:00
name: Globalize.translate('MyDevice'),
2015-12-14 08:43:03 -07:00
id: AppInfo.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
2016-01-28 14:27:41 -07:00
function updateDeviceProfileForAndroid(profile) {
2016-01-28 14:27:41 -07:00
// Just here as an easy escape out, if ever needed
2016-02-05 10:04:38 -07:00
var enableVlcVideo = window.VlcAudio;
2016-01-28 14:27:41 -07:00
var enableVlcAudio = true;
2016-01-28 14:27:41 -07:00
if (enableVlcVideo) {
2016-01-28 21:18:31 -07:00
profile.DirectPlayProfiles.push({
Container: "m4v,3gp,ts,mpegts,mov,xvid,vob,mkv,wmv,asf,ogm,ogv,m2v,avi,mpg,mpeg,mp4,webm",
Type: 'Video'
});
2016-01-28 21:18:31 -07:00
profile.CodecProfiles = profile.CodecProfiles.filter(function (i) {
return i.Type == 'Audio';
});
2015-10-17 18:18:29 -07:00
2016-01-28 14:27:41 -07:00
profile.SubtitleProfiles = [];
profile.SubtitleProfiles.push({
Format: 'srt',
Method: 'External'
});
profile.SubtitleProfiles.push({
Format: 'srt',
Method: 'Embed'
});
profile.SubtitleProfiles.push({
Format: 'subrip',
Method: 'Embed'
});
profile.SubtitleProfiles.push({
Format: 'ass',
Method: 'Embed'
});
profile.SubtitleProfiles.push({
Format: 'ssa',
Method: 'Embed'
});
profile.SubtitleProfiles.push({
Format: 'pgs',
Method: 'Embed'
});
profile.SubtitleProfiles.push({
Format: 'pgssub',
Method: 'Embed'
});
profile.SubtitleProfiles.push({
Format: 'dvdsub',
Method: 'Embed'
});
profile.SubtitleProfiles.push({
Format: 'vtt',
Method: 'Embed'
});
profile.SubtitleProfiles.push({
Format: 'sub',
Method: 'Embed'
});
profile.SubtitleProfiles.push({
Format: 'idx',
Method: 'Embed'
});
profile.SubtitleProfiles.push({
Format: 'smi',
Method: 'Embed'
});
2015-03-26 12:18:21 -07:00
2016-01-28 14:27:41 -07:00
// These don't play very well
profile.CodecProfiles.push({
Type: 'VideoAudio',
Codec: 'dca',
Conditions: [
{
Condition: 'LessThanEqual',
Property: 'AudioChannels',
Value: 6
2016-01-13 22:18:46 -07:00
}
2016-01-28 14:27:41 -07:00
]
});
2016-01-28 14:27:41 -07:00
profile.CodecProfiles.push({
Type: 'VideoAudio',
Codec: 'aac,mp3',
Conditions: [
{
Condition: 'LessThanEqual',
Property: 'AudioChannels',
Value: '6'
2015-12-30 10:02:11 -07:00
}
2016-01-28 14:27:41 -07:00
]
});
2016-01-28 14:27:41 -07:00
profile.CodecProfiles.push({
Type: 'Video',
Codec: 'h264',
Conditions: [
{
Condition: 'EqualsAny',
Property: 'VideoProfile',
Value: 'high|main|baseline|constrained baseline'
},
{
Condition: 'LessThanEqual',
Property: 'VideoLevel',
Value: '41'
}]
});
}
2016-01-28 14:27:41 -07:00
if (enableVlcAudio) {
2015-06-10 23:27:05 -07:00
2016-01-28 21:18:31 -07:00
profile.DirectPlayProfiles.push({
Container: "aac,mp3,mpa,wav,wma,mp2,ogg,oga,webma,ape,opus",
Type: 'Audio'
});
2015-05-22 12:16:14 -07:00
2016-01-28 21:18:31 -07:00
profile.CodecProfiles = profile.CodecProfiles.filter(function (i) {
return i.Type != 'Audio';
});
2016-01-28 14:27:41 -07:00
profile.CodecProfiles.push({
Type: 'Audio',
Conditions: [{
Condition: 'LessThanEqual',
Property: 'AudioChannels',
Value: '2'
}]
});
}
}
2015-12-14 08:43:03 -07:00
2016-01-28 14:27:41 -07:00
function updateDeviceProfileForIOS(profile) {
2015-05-24 11:33:28 -07:00
2016-01-28 21:18:31 -07:00
profile.DirectPlayProfiles.push({
Container: "aac,mp3,mpa,wav,wma,mp2,ogg,oga,webma,ape,opus,flac",
Type: 'Audio'
});
2016-01-28 14:27:41 -07:00
}
2015-12-23 10:46:01 -07:00
2016-01-28 14:27:41 -07:00
self.getDeviceProfile = function (maxHeight) {
2015-12-23 10:46:01 -07:00
2016-01-28 14:27:41 -07:00
return new Promise(function (resolve, reject) {
2015-09-05 09:58:27 -07:00
2016-02-01 13:41:56 -07:00
require(['browserdeviceprofile', 'qualityoptions'], function (profile, qualityoptions) {
var bitrateSetting = AppSettings.maxStreamingBitrate();
if (!maxHeight) {
maxHeight = qualityoptions.getVideoQualityOptions(bitrateSetting).filter(function (q) {
return q.selected;
})[0].maxHeight;
}
2016-01-28 14:27:41 -07:00
if (AppInfo.isNativeApp && browserInfo.android) {
updateDeviceProfileForAndroid(profile);
2015-12-30 10:02:11 -07:00
}
2016-01-28 14:27:41 -07:00
else if (AppInfo.isNativeApp && browserInfo.safari) {
updateDeviceProfileForIOS(profile);
2015-12-30 10:02:11 -07:00
}
2016-02-01 13:41:56 -07:00
profile.MaxStreamingBitrate = bitrateSetting;
2015-12-30 10:02:11 -07:00
resolve(profile);
});
2015-05-06 20:11:51 -07:00
});
};
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-09-19 19:06:56 -07:00
// Returns true if the player can seek using native client-side seeking functions
function canPlayerSeek() {
2014-10-07 18:37:45 -07:00
2015-09-19 19:06:56 -07:00
var mediaRenderer = self.currentMediaRenderer;
2015-09-09 10:49:44 -07:00
var currentSrc = self.getCurrentSrc(mediaRenderer);
2014-10-03 18:42:38 -07:00
2015-09-09 10:49:44 -07:00
if ((currentSrc || '').indexOf('.m3u8') != -1) {
2015-09-19 19:06:56 -07:00
return true;
2015-09-09 10:49:44 -07:00
} else {
var duration = mediaRenderer.duration();
2015-09-19 19:06:56 -07:00
return duration && !isNaN(duration) && duration != Number.POSITIVE_INFINITY && duration != Number.NEGATIVE_INFINITY;
2015-09-09 10:49:44 -07:00
}
2015-09-19 19:06:56 -07:00
}
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
2015-12-23 10:46:01 -07:00
console.log('playNextAfterEnded');
Events.off(this, 'ended', self.playNextAfterEnded);
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
2015-12-14 08:43:03 -07:00
if (browserInfo.safari) {
2015-07-08 17:20:01 -07:00
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-07-10 11:30:42 -07:00
if (AppInfo.isNativeApp) {
2015-07-06 19:25:23 -07:00
return true;
}
2015-06-07 13:06:18 -07:00
var media = document.createElement('video');
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-10 11:30:42 -07:00
// viblast can help us here
//return true;
2016-01-28 11:29:24 -07:00
return window.MediaSource != null && !browserInfo.firefox;
2015-06-01 22:46:06 -07:00
};
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
2015-09-19 19:06:56 -07:00
if (canPlayerSeek() && 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-12-30 10:02:11 -07:00
self.getDeviceProfile().then(function (deviceProfile) {
2015-12-30 13:25:17 -07:00
2015-12-30 10:02:11 -07:00
var audioStreamIndex = params.AudioStreamIndex == null ? (getParameterByName('AudioStreamIndex', currentSrc) || null) : params.AudioStreamIndex;
if (typeof (audioStreamIndex) == 'string') {
audioStreamIndex = parseInt(audioStreamIndex);
}
2014-04-11 20:48:57 -07:00
2015-12-30 10:02:11 -07:00
var subtitleStreamIndex = params.SubtitleStreamIndex == null ? (getParameterByName('SubtitleStreamIndex', currentSrc) || null) : params.SubtitleStreamIndex;
if (typeof (subtitleStreamIndex) == 'string') {
subtitleStreamIndex = parseInt(subtitleStreamIndex);
}
2014-04-02 14:55:19 -07:00
2015-12-30 10:02:11 -07:00
MediaController.getPlaybackInfo(self.currentItem.Id, deviceProfile, ticks, self.currentMediaSource, audioStreamIndex, subtitleStreamIndex, liveStreamId).then(function (result) {
2014-07-02 11:34:08 -07:00
2015-12-30 10:02:11 -07:00
if (validatePlaybackInfoResult(result)) {
2013-05-23 18:47:07 -07:00
2015-12-30 10:02:11 -07:00
self.currentMediaSource = result.MediaSources[0];
self.createStreamInfo(self.currentItem.MediaType, self.currentItem, self.currentMediaSource, ticks).then(function (streamInfo) {
2015-07-16 05:56:38 -07:00
2015-12-30 10:02:11 -07:00
if (!streamInfo.url) {
MediaController.showPlaybackInfoErrorMessage('NoCompatibleStream');
self.stop();
return;
}
2015-12-30 10:02:11 -07:00
self.currentSubtitleStreamIndex = subtitleStreamIndex;
2013-05-23 18:47:07 -07:00
2015-12-30 10:02:11 -07:00
changeStreamToUrl(mediaRenderer, playSessionId, streamInfo);
});
}
});
2015-03-26 12:18:21 -07:00
});
};
2013-05-23 18:47:07 -07:00
2015-12-14 08:43:03 -07:00
function changeStreamToUrl(mediaRenderer, playSessionId, streamInfo) {
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-12-23 10:46:01 -07:00
function onPlayingOnce() {
2013-05-23 18:47:07 -07:00
2016-02-01 12:10:44 -07:00
Events.off(mediaRenderer, "play", onPlayingOnce);
Events.on(mediaRenderer, 'ended', self.onPlaybackStopped);
2015-07-06 19:25:23 -07:00
2016-02-01 12:10:44 -07:00
Events.on(mediaRenderer, 'ended', self.playNextAfterEnded);
self.startProgressInterval();
sendProgressUpdate();
2015-12-23 10:46:01 -07:00
}
2014-04-11 20:48:57 -07:00
2015-12-23 10:46:01 -07:00
Events.on(mediaRenderer, "play", onPlayingOnce);
2014-06-14 19:58:00 -07:00
if (self.currentItem.MediaType == "Video") {
2015-12-14 08:43:03 -07:00
ApiClient.stopActiveEncodings(playSessionId).then(function () {
2015-09-15 11:09:44 -07:00
self.setSrcIntoRenderer(mediaRenderer, streamInfo, self.currentItem, self.currentMediaSource);
});
} else {
2015-09-15 11:09:44 -07:00
self.setSrcIntoRenderer(mediaRenderer, streamInfo, self.currentItem, self.currentMediaSource);
2013-05-23 18:47:07 -07:00
}
}
2013-04-30 12:13:06 -07:00
2015-09-15 11:09:44 -07:00
self.setSrcIntoRenderer = function (mediaRenderer, streamInfo, item, mediaSource) {
2015-07-30 18:52:11 -07:00
var subtitleStreams = mediaSource.MediaStreams.filter(function (s) {
return s.Type == 'Subtitle';
});
var textStreams = subtitleStreams.filter(function (s) {
return s.DeliveryMethod == 'External';
});
var tracks = [];
for (var i = 0, length = textStreams.length; i < length; i++) {
var textStream = textStreams[i];
var textStreamUrl = !textStream.IsExternalUrl ? ApiClient.getUrl(textStream.DeliveryUrl) : textStream.DeliveryUrl;
tracks.push({
url: textStreamUrl,
language: (textStream.Language || 'und'),
2016-02-23 11:48:26 -07:00
isDefault: textStream.Index == mediaSource.DefaultSubtitleStreamIndex,
index: textStream.Index
2015-07-30 18:52:11 -07:00
});
}
2015-12-14 08:43:03 -07:00
self.startTimeTicksOffset = streamInfo.startTimeTicksOffset || 0;
2015-09-15 11:09:44 -07:00
mediaRenderer.setCurrentSrc(streamInfo, item, mediaSource, tracks);
self.streamInfo = streamInfo;
2015-12-14 08:43:03 -07:00
//self.updateTextStreamUrls(streamInfo.startTimeTicksOffset || 0);
2015-07-30 18:52:11 -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);
2015-09-09 10:49:44 -07:00
var mediaRenderer = self.currentMediaRenderer;
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.value = percent;
2013-04-30 12:13:06 -07:00
}
2015-09-09 10:49:44 -07:00
}
2015-09-09 10:49:44 -07:00
if (positionSlider) {
2015-09-19 19:06:56 -07:00
positionSlider.disabled = !((self.currentDurationTicks || 0) > 0 || canPlayerSeek());
}
if (currentTimeElement) {
currentTimeElement.html(timeText);
2013-05-24 07:35:15 -07:00
}
2013-05-23 13:09:01 -07:00
2015-09-09 10:49:44 -07:00
var state = self.getPlayerStateInternal(mediaRenderer, 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
2016-02-13 21:47:35 -07:00
function translateItemsForPlayback(items, smart) {
2014-03-29 12:02:43 -07:00
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"
});
}
2016-02-13 21:47:35 -07:00
else if (smart && firstItem.Type == "Episode" && items.length == 1) {
2016-02-25 06:36:24 -07:00
promise = ApiClient.getCurrentUser().then(function(user) {
if (!user.Configuration.EnableEpisodeAutoQueue) {
return null;
}
2016-02-13 21:47:35 -07:00
2016-02-25 06:36:24 -07:00
return ApiClient.getEpisodes(firstItem.SeriesId, {
IsVirtualUnaired: false,
IsMissing: false,
UserId: ApiClient.getCurrentUserId(),
Fields: getItemFields
2016-02-13 21:47:35 -07:00
2016-02-25 06:36:24 -07:00
}).then(function (episodesResult) {
2016-02-13 21:47:35 -07:00
2016-02-25 06:36:24 -07:00
var foundItem = false;
episodesResult.Items = episodesResult.Items.filter(function (e) {
if (foundItem) {
return true;
}
if (e.Id == firstItem.Id) {
foundItem = true;
return true;
}
2016-02-13 21:47:35 -07:00
2016-02-25 06:36:24 -07:00
return false;
});
episodesResult.TotalRecordCount = episodesResult.Items.length;
return episodesResult;
2016-02-13 21:47:35 -07:00
});
});
}
2014-03-29 12:02:43 -07:00
if (promise) {
2016-02-25 06:36:24 -07:00
return promise.then(function (result) {
2014-03-29 12:02:43 -07:00
2016-02-25 06:36:24 -07:00
return result ? result.Items : items;
2014-03-29 12:02:43 -07:00
});
} else {
2016-02-25 06:36:24 -07:00
return Promise.resolve(items);
2015-12-14 08:43:03 -07:00
}
2014-03-29 12:02:43 -07:00
}
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();
2015-12-14 08:43:03 -07:00
Dashboard.getCurrentUser().then(function (user) {
2014-03-29 08:40:32 -07:00
if (options.items) {
2016-02-13 21:47:35 -07:00
translateItemsForPlayback(options.items, true).then(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(',')
2015-12-14 08:43:03 -07:00
}).then(function (result) {
2013-05-24 19:45:12 -07:00
2016-02-13 21:47:35 -07:00
translateItemsForPlayback(result.Items, true).then(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") {
2015-12-14 08:43:03 -07:00
Dashboard.showLoadingMsg();
2015-07-09 20:00:03 -07:00
}
2015-12-14 08:43:03 -07:00
if (options.startPositionTicks || firstItem.MediaType !== 'Video' || !AppSettings.enableCinemaMode()) {
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
}
2015-12-14 08:43:03 -07:00
ApiClient.getJSON(ApiClient.getUrl('Users/' + user.Id + '/Items/' + firstItem.Id + '/Intros')).then(function (intros) {
2014-08-01 19:34:45 -07:00
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
2015-09-20 09:16:06 -07:00
var promises = versions.map(function (v) {
return MediaController.supportsDirectPlay(v);
});
2014-03-20 21:52:46 -07:00
2015-12-30 13:25:17 -07:00
return Promise.all(promises).then(function (responses) {
2015-03-16 21:08:09 -07:00
2015-09-20 09:16:06 -07:00
for (var i = 0, length = versions.length; i < length; i++) {
2015-12-14 08:43:03 -07:00
versions[i].enableDirectPlay = responses[i] || false;
2015-09-20 09:16:06 -07:00
}
var optimalVersion = versions.filter(function (v) {
2015-03-07 22:37:48 -07:00
2015-09-20 09:16:06 -07:00
return v.enableDirectPlay;
2015-03-07 22:37:48 -07:00
2015-09-20 09:16:06 -07:00
})[0];
2015-03-07 22:37:48 -07:00
2015-09-20 09:16:06 -07:00
if (!optimalVersion) {
optimalVersion = versions.filter(function (v) {
return v.SupportsDirectStream;
})[0];
}
optimalVersion = optimalVersion || versions.filter(function (s) {
return s.SupportsTranscoding;
2014-03-20 21:52:46 -07:00
})[0];
2014-03-20 20:31:40 -07:00
2015-12-30 13:25:17 -07:00
return optimalVersion;
2015-09-20 09:16:06 -07:00
});
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) {
2016-02-14 13:34:54 -07:00
return new Promise(function (resolve, reject) {
2015-09-21 08:43:10 -07:00
2016-02-14 13:34:54 -07:00
var mediaUrl;
var contentType;
var startTimeTicksOffset = 0;
2016-02-14 13:34:54 -07:00
var startPositionInSeekParam = startPosition ? (startPosition / 10000000) : 0;
var seekParam = startPositionInSeekParam ? '#t=' + startPositionInSeekParam : '';
var playMethod = 'Transcode';
2016-02-14 13:34:54 -07:00
if (type == 'Video') {
2016-02-14 13:34:54 -07:00
contentType = 'video/' + mediaSource.Container;
2016-02-14 13:34:54 -07:00
if (mediaSource.enableDirectPlay) {
mediaUrl = mediaSource.Path;
2015-07-14 09:39:34 -07:00
2016-02-14 13:34:54 -07:00
playMethod = 'DirectPlay';
2015-05-31 11:22:51 -07:00
2016-02-14 13:34:54 -07:00
} else {
2016-02-14 13:34:54 -07:00
if (mediaSource.SupportsDirectStream) {
2016-02-14 13:34:54 -07:00
var directOptions = {
Static: true,
mediaSourceId: mediaSource.Id,
deviceId: ApiClient.deviceId(),
api_key: ApiClient.accessToken()
};
2016-02-14 13:34:54 -07:00
if (mediaSource.LiveStreamId) {
directOptions.LiveStreamId = mediaSource.LiveStreamId;
}
2016-02-14 13:34:54 -07:00
mediaUrl = ApiClient.getUrl('Videos/' + item.Id + '/stream.' + mediaSource.Container, directOptions);
mediaUrl += seekParam;
2016-02-14 13:34:54 -07:00
playMethod = 'DirectStream';
} else if (mediaSource.SupportsTranscoding) {
2016-02-14 13:34:54 -07:00
mediaUrl = ApiClient.getUrl(mediaSource.TranscodingUrl);
2016-02-14 13:34:54 -07:00
if (mediaSource.TranscodingSubProtocol == 'hls') {
2016-02-14 13:34:54 -07:00
mediaUrl += seekParam;
contentType = 'application/x-mpegURL';
2015-12-23 10:46:01 -07:00
2016-02-14 13:34:54 -07:00
} else {
2016-02-14 13:34:54 -07:00
// Reports of stuttering with h264 stream copy in IE
if (mediaUrl.indexOf('.mkv') == -1) {
mediaUrl += '&EnableAutoStreamCopy=false';
}
if (mediaUrl.toLowerCase().indexOf('copytimestamps=true') == -1) {
startTimeTicksOffset = startPosition || 0;
}
2015-12-14 08:43:03 -07:00
2016-02-14 13:34:54 -07:00
contentType = 'video/' + mediaSource.TranscodingContainer;
}
}
}
2016-02-14 13:34:54 -07:00
} else {
2016-02-14 13:34:54 -07:00
contentType = 'audio/' + mediaSource.Container;
2016-02-14 13:34:54 -07:00
if (mediaSource.enableDirectPlay) {
2016-02-14 13:34:54 -07:00
mediaUrl = mediaSource.Path;
2015-07-14 09:39:34 -07:00
2016-02-14 13:34:54 -07:00
playMethod = 'DirectPlay';
2015-05-31 11:22:51 -07:00
2016-02-14 13:34:54 -07:00
} else {
2016-02-14 13:34:54 -07:00
var isDirectStream = mediaSource.SupportsDirectStream;
2016-02-14 13:34:54 -07:00
if (isDirectStream) {
2016-02-14 13:34:54 -07:00
var outputContainer = (mediaSource.Container || '').toLowerCase();
2016-02-14 13:34:54 -07:00
var directOptions = {
Static: true,
mediaSourceId: mediaSource.Id,
deviceId: ApiClient.deviceId(),
api_key: ApiClient.accessToken()
};
2016-02-14 13:34:54 -07:00
if (mediaSource.LiveStreamId) {
directOptions.LiveStreamId = mediaSource.LiveStreamId;
}
2016-02-14 13:34:54 -07:00
mediaUrl = ApiClient.getUrl('Audio/' + item.Id + '/stream.' + outputContainer, directOptions);
mediaUrl += seekParam;
2015-05-18 18:46:31 -07:00
2016-02-14 13:34:54 -07:00
playMethod = 'DirectStream';
2016-02-14 13:34:54 -07:00
} else if (mediaSource.SupportsTranscoding) {
2016-02-14 13:34:54 -07:00
mediaUrl = ApiClient.getUrl(mediaSource.TranscodingUrl);
2016-02-14 13:34:54 -07:00
if (mediaSource.TranscodingSubProtocol == 'hls') {
2015-07-03 04:51:45 -07:00
2016-02-14 13:34:54 -07:00
mediaUrl += seekParam;
contentType = 'application/x-mpegURL';
} else {
2015-07-03 04:51:45 -07:00
2016-02-14 13:34:54 -07:00
startTimeTicksOffset = startPosition || 0;
contentType = 'audio/' + mediaSource.TranscodingContainer;
}
2015-07-03 04:51:45 -07:00
}
}
}
2016-02-14 13:34:54 -07:00
var resultInfo = {
url: mediaUrl,
mimeType: contentType,
startTimeTicksOffset: startTimeTicksOffset,
startPositionInSeekParam: startPositionInSeekParam,
playMethod: playMethod
};
2015-09-21 08:43:10 -07:00
2016-02-14 13:34:54 -07:00
if (playMethod == 'DirectPlay' && mediaSource.Protocol == 'File') {
2015-09-21 08:43:10 -07:00
2016-02-14 13:34:54 -07:00
require(['localassetmanager'], function () {
2015-09-21 08:43:10 -07:00
2016-02-14 13:34:54 -07:00
LocalAssetManager.translateFilePath(resultInfo.url).then(function (path) {
2015-09-21 08:43:10 -07:00
2016-02-14 13:34:54 -07:00
resultInfo.url = path;
console.log('LocalAssetManager.translateFilePath: path: ' + resultInfo.url + ' result: ' + path);
resolve(resultInfo);
});
2015-09-21 08:43:10 -07:00
});
2016-02-14 13:34:54 -07:00
}
else {
resolve(resultInfo);
}
});
};
2015-12-14 08:43:03 -07:00
self.lastBitrateDetections = {};
2015-09-05 14:15:36 -07:00
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-12-14 08:43:03 -07:00
Dashboard.hideLoadingMsg();
2015-05-19 12:15:40 -07:00
MediaController.showPlaybackInfoErrorMessage('PlaceHolder');
2015-03-25 21:44:24 -07:00
return;
}
2015-12-30 13:25:17 -07:00
var onBitrateDetected = function () {
self.getDeviceProfile().then(function (deviceProfile) {
2015-12-30 10:02:11 -07:00
playOnDeviceProfileCreated(deviceProfile, item, startPosition, callback);
});
};
2015-12-14 08:43:03 -07:00
var bitrateDetectionKey = ApiClient.serverAddress();
2015-09-05 14:15:36 -07:00
2015-12-14 08:43:03 -07:00
if (item.MediaType == 'Video' && AppSettings.enableAutomaticBitrateDetection() && (new Date().getTime() - (self.lastBitrateDetections[bitrateDetectionKey] || 0)) > 300000) {
2015-09-05 14:15:36 -07:00
2015-12-14 08:43:03 -07:00
Dashboard.showLoadingMsg();
2015-09-05 14:15:36 -07:00
2015-12-14 08:43:03 -07:00
ApiClient.detectBitrate().then(function (bitrate) {
2015-12-23 10:46:01 -07:00
console.log('Max bitrate auto detected to ' + bitrate);
2015-12-14 08:43:03 -07:00
self.lastBitrateDetections[bitrateDetectionKey] = new Date().getTime();
2015-09-05 14:15:36 -07:00
AppSettings.maxStreamingBitrate(bitrate);
2015-12-30 10:02:11 -07:00
onBitrateDetected();
2015-12-14 08:43:03 -07:00
2015-12-30 10:02:11 -07:00
}, onBitrateDetected);
2015-09-05 14:15:36 -07:00
} else {
2015-12-30 10:02:11 -07:00
onBitrateDetected();
2015-09-05 14:15:36 -07:00
}
};
2015-09-20 09:16:06 -07:00
self.tryStartPlayback = function (deviceProfile, item, startPosition, callback) {
2015-05-06 20:11:51 -07:00
if (item.MediaType === "Video") {
2015-12-14 08:43:03 -07:00
Dashboard.showLoadingMsg();
2015-05-06 20:11:51 -07:00
}
2015-04-14 08:26:02 -07:00
2015-12-14 08:43:03 -07:00
MediaController.getPlaybackInfo(item.Id, deviceProfile, startPosition).then(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-12-14 08:43:03 -07:00
getOptimalMediaSource(item.MediaType, playbackInfoResult.MediaSources).then(function (mediaSource) {
2015-09-20 09:16:06 -07:00
if (mediaSource) {
2014-11-18 19:45:12 -07:00
2015-09-20 09:16:06 -07:00
if (mediaSource.RequiresOpening) {
2015-02-05 22:39:07 -07:00
2015-12-14 08:43:03 -07:00
MediaController.getLiveStream(item.Id, playbackInfoResult.PlaySessionId, deviceProfile, startPosition, mediaSource, null, null).then(function (openLiveStreamResult) {
2014-11-18 19:45:12 -07:00
2015-12-14 08:43:03 -07:00
MediaController.supportsDirectPlay(openLiveStreamResult.MediaSource).then(function (result) {
2015-03-03 00:03:17 -07:00
2015-09-20 09:16:06 -07:00
openLiveStreamResult.MediaSource.enableDirectPlay = result;
callback(openLiveStreamResult.MediaSource);
});
2015-04-13 09:21:10 -07:00
2015-09-20 09:16:06 -07:00
});
2015-03-03 00:03:17 -07:00
2015-09-20 09:16:06 -07:00
} else {
callback(mediaSource);
}
2015-03-28 21:56:39 -07:00
} else {
2015-12-14 08:43:03 -07:00
Dashboard.hideLoadingMsg();
2015-09-20 09:16:06 -07:00
MediaController.showPlaybackInfoErrorMessage('NoCompatibleStream');
2015-03-07 22:37:48 -07:00
}
2015-09-20 09:16:06 -07:00
});
2015-03-03 00:03:17 -07:00
}
2015-03-07 22:37:48 -07:00
});
2015-09-20 09:16:06 -07:00
};
function playOnDeviceProfileCreated(deviceProfile, item, startPosition, callback) {
self.tryStartPlayback(deviceProfile, item, startPosition, function (mediaSource) {
playInternalPostMediaSourceSelection(item, mediaSource, startPosition, callback);
});
2015-09-05 14:15:36 -07:00
}
2015-03-28 21:56:39 -07:00
function playInternalPostMediaSourceSelection(item, mediaSource, startPosition, callback) {
2015-12-14 08:43:03 -07:00
Dashboard.hideLoadingMsg();
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-12-14 08:43:03 -07:00
requirejs(['videorenderer', 'scripts/mediaplayer-video'], 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-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) {
2015-12-14 08:43:03 -07:00
return new Promise(function (resolve, reject) {
2013-06-07 09:06:32 -07:00
2015-12-14 08:43:03 -07:00
ApiClient.getItem(userId, query.Ids.split(',')).then(function (item) {
resolve({
Items: [item],
TotalRecordCount: 1
});
});
});
2015-02-24 11:25:19 -07:00
}
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
2015-07-26 14:02:23 -07:00
var newIndex;
switch (self.getRepeatMode()) {
case 'RepeatOne':
newIndex = currentPlaylistIndex;
break;
case 'RepeatAll':
newIndex = currentPlaylistIndex + 1;
if (newIndex >= self.playlist.length) {
newIndex = 0;
}
break;
default:
newIndex = currentPlaylistIndex + 1;
break;
}
2013-06-07 09:06:32 -07:00
var newItem = self.playlist[newIndex];
2013-05-23 13:09:01 -07:00
if (newItem) {
2014-07-17 15:21:35 -07:00
2015-12-23 10:46:01 -07:00
console.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
2015-12-14 08:43:03 -07:00
Dashboard.getCurrentUser().then(function (user) {
2013-06-07 09:06:32 -07:00
2014-03-29 08:40:32 -07:00
if (options.items) {
2015-12-14 08:43:03 -07:00
translateItemsForPlayback(options.items).then(function (items) {
2014-03-29 12:02:43 -07:00
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
2015-12-14 08:43:03 -07:00
}).then(function (result) {
2013-06-07 09:06:32 -07:00
2015-12-14 08:43:03 -07:00
translateItemsForPlayback(result.Items).then(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
2015-12-14 08:43:03 -07:00
Dashboard.getCurrentUser().then(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(',')
2015-12-14 08:43:03 -07:00
}).then(function (result) {
2014-03-29 08:40:32 -07:00
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-12-23 10:46:01 -07:00
console.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-12-23 10:46:01 -07:00
console.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();
2015-12-14 08:43:03 -07:00
ApiClient.getItem(userId, id).then(function (item) {
2014-03-29 08:40:32 -07:00
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;
}
2015-12-14 08:43:03 -07:00
self.getItemsForPlayback(query).then(function (result) {
2014-03-29 08:40:32 -07:00
self.play({ items: result.Items });
});
});
};
self.instantMix = function (id) {
2015-10-07 14:42:29 -07:00
var itemLimit = 100;
2014-03-29 08:40:32 -07:00
2015-10-07 14:42:29 -07:00
ApiClient.getInstantMixFromItem(id, {
UserId: Dashboard.getCurrentUserId(),
Fields: getItemFields,
Limit: itemLimit
2014-03-29 08:40:32 -07:00
2015-12-14 08:43:03 -07:00
}).then(function (result) {
2014-03-29 08:40:32 -07:00
2015-10-07 14:42:29 -07:00
self.play({ items: result.Items });
2014-03-29 08:40:32 -07:00
});
};
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-29 11:45:42 -07:00
Events.off(mediaRenderer, 'ended', self.playNextAfterEnded);
2013-06-07 10:23:23 -07:00
2016-01-27 23:42:27 -07:00
var stopTranscoding = false;
if (!currentProgressInterval) {
stopTranscoding = true;
}
2015-12-23 10:46:01 -07:00
mediaRenderer.stop();
2015-12-23 10:46:01 -07:00
Events.trigger(mediaRenderer, "ended");
//self.onPlaybackStopped.call(mediaRenderer);
2015-06-25 14:50:56 -07:00
2015-12-23 10:46:01 -07:00
// TODO: Unbind video events
unBindAudioEvents(mediaRenderer);
2015-12-23 10:46:01 -07:00
mediaRenderer.cleanup(destroyRenderer);
2015-07-06 19:25:23 -07:00
2015-12-23 10:46:01 -07:00
self.currentMediaRenderer = null;
self.currentItem = null;
2016-01-27 23:42:27 -07:00
2015-12-23 10:46:01 -07:00
self.currentSubtitleStreamIndex = null;
self.streamInfo = {};
2014-07-01 14:13:32 -07:00
2016-01-27 23:42:27 -07:00
self.currentMediaSource = null;
if (stopTranscoding) {
ApiClient.stopActiveEncodings();
}
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;
2015-09-15 11:09:44 -07:00
self.currentSubtitleStreamIndex = null;
self.streamInfo = {};
2014-07-01 14:13:32 -07:00
}
2013-03-26 14:32:01 -07:00
2015-12-14 08:43:03 -07:00
if (self.resetEnhancements) {
self.resetEnhancements();
2013-12-29 19:41:22 -07:00
}
2013-04-10 14:03:28 -07:00
};
2013-03-26 14:32:01 -07:00
2015-12-23 10:46:01 -07:00
function unBindAudioEvents(mediaRenderer) {
Events.off(mediaRenderer, "volumechange", onVolumeChange);
Events.off(mediaRenderer, "pause", onPause);
Events.off(mediaRenderer, "playing", onPlaying);
Events.off(mediaRenderer, "timeupdate", onTimeUpdate);
}
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 () {
2015-12-30 13:25:17 -07:00
return new Promise(function (resolve, reject) {
2015-12-30 13:25:17 -07:00
var result = self.getPlayerStateInternal(self.currentMediaRenderer, self.currentItem, self.currentMediaSource);
resolve(result);
});
};
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);
2015-07-26 14:02:23 -07:00
state.PlayState.RepeatMode = self.getRepeatMode();
2014-04-22 10:25:54 -07:00
2015-09-15 11:09:44 -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
2015-09-15 11:09:44 -07:00
state.PlayState.PlayMethod = self.streamInfo.playMethod;
2015-03-29 11:38:18 -07:00
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;
2016-01-26 23:06:26 -07:00
state.PlayState.LiveStreamId = mediaSource.LiveStreamId;
2014-04-22 10:25:54 -07:00
state.NowPlayingItem = {
RunTimeTicks: mediaSource.RunTimeTicks
};
2015-09-19 19:06:56 -07:00
state.PlayState.CanSeek = (mediaSource.RunTimeTicks || 0) > 0 || canPlayerSeek();
}
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-09-25 19:31:13 -07:00
self.onBeforePlaybackStart = function (mediaRenderer, item, mediaSource) {
var state = self.getPlayerStateInternal(mediaRenderer, item, mediaSource);
Events.trigger(self, 'beforeplaybackstart', [state]);
};
2015-06-07 13:06:18 -07:00
self.onPlaybackStart = function (mediaRenderer, item, mediaSource) {
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-12-23 10:46:01 -07:00
console.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-12-23 10:46:01 -07:00
// TODO: Unbind other events
unBindAudioEvents(mediaRenderer);
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
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-12-23 10:46:01 -07:00
console.log('mediaplayer onPlaystateChange');
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]);
};
2016-01-27 23:42:27 -07:00
function onAppClose() {
// Try to report playback stopped before the browser closes
2016-01-27 23:42:27 -07:00
if (self.currentItem && self.currentMediaRenderer) {
if (currentProgressInterval) {
2016-01-27 23:42:27 -07:00
self.onPlaybackStopped.call(self.currentMediaRenderer);
} else {
ApiClient.stopActiveEncodings();
}
}
2016-01-27 23:42:27 -07:00
}
window.addEventListener("beforeunload", onAppClose);
if (browserInfo.safari) {
document.addEventListener("pause", onAppClose);
}
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);
2015-07-30 07:34:46 -07:00
console.log('repeat mode ' + info.RepeatMode);
2014-05-07 11:38:50 -07:00
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
}
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-12-14 08:43:03 -07:00
if (browserInfo.mobile) {
2014-04-14 20:54:52 -07:00
return false;
}
return true;
};
2015-07-26 14:02:23 -07:00
var repeatMode = 'RepeatNone';
self.getRepeatMode = function () {
return repeatMode;
};
self.setRepeatMode = function (mode) {
repeatMode = mode;
};
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-12-14 08:43:03 -07:00
self.createStreamInfo('Audio', item, mediaSource, startPositionTicks).then(function (streamInfo) {
2014-04-13 10:27:13 -07:00
2015-09-21 08:43:10 -07:00
self.startTimeTicksOffset = streamInfo.startTimeTicksOffset;
2015-09-21 08:43:10 -07:00
var initialVolume = self.getSavedVolume();
var mediaRenderer = new AudioRenderer({
poster: self.getPosterUrl(item)
});
2015-12-23 10:46:01 -07:00
function onPlayingOnce() {
2015-06-28 07:45:21 -07:00
2015-12-23 10:46:01 -07:00
Events.off(mediaRenderer, "playing", onPlayingOnce);
2015-12-23 10:46:01 -07:00
console.log('audio element event: playing');
2014-08-10 15:13:17 -07:00
2015-09-21 08:43:10 -07:00
// For some reason this is firing at the start, so don't bind until playback has begun
2015-12-23 10:46:01 -07:00
Events.on(mediaRenderer, 'ended', self.onPlaybackStopped);
Events.on(mediaRenderer, 'ended', self.playNextAfterEnded);
2015-12-23 10:46:01 -07:00
self.onPlaybackStart(mediaRenderer, item, mediaSource);
}
2014-11-13 23:27:10 -07:00
2015-12-23 10:46:01 -07:00
Events.on(mediaRenderer, "volumechange", onVolumeChange);
Events.on(mediaRenderer, "playing", onPlayingOnce);
Events.on(mediaRenderer, "pause", onPause);
Events.on(mediaRenderer, "playing", onPlaying);
Events.on(mediaRenderer, "timeupdate", onTimeUpdate);
2015-06-07 13:06:18 -07:00
2015-09-21 08:43:10 -07:00
self.currentMediaRenderer = mediaRenderer;
self.currentDurationTicks = self.currentMediaSource.RunTimeTicks;
2015-09-09 20:22:52 -07:00
2015-12-14 08:43:03 -07:00
mediaRenderer.init().then(function () {
2015-09-09 20:22:52 -07:00
2015-09-21 08:43:10 -07:00
// Set volume first to avoid an audible change
mediaRenderer.volume(initialVolume);
2015-09-09 20:22:52 -07:00
2015-09-25 19:31:13 -07:00
self.onBeforePlaybackStart(mediaRenderer, item, mediaSource);
2015-09-21 08:43:10 -07:00
mediaRenderer.setCurrentSrc(streamInfo, item, mediaSource);
self.streamInfo = streamInfo;
});
2015-09-09 20:22:52 -07:00
});
2015-06-07 20:16:42 -07:00
}
2015-12-23 10:46:01 -07:00
function onVolumeChange() {
console.log('audio element event: pause');
self.onPlaystateChange(this);
// In the event timeupdate isn't firing, at least we can update when this happens
self.setCurrentTime(self.getCurrentTicks());
}
function onPause() {
console.log('audio element event: pause');
self.onPlaystateChange(this);
// In the event timeupdate isn't firing, at least we can update when this happens
self.setCurrentTime(self.getCurrentTicks());
}
function onPlaying() {
console.log('audio element event: playing');
self.onPlaystateChange(this);
// In the event timeupdate isn't firing, at least we can update when this happens
self.setCurrentTime(self.getCurrentTicks());
}
2014-03-22 09:16:43 -07:00
var getItemFields = "MediaSources,Chapters";
2015-05-18 09:40:20 -07:00
self.tryPair = function (target) {
2015-12-30 13:25:17 -07:00
return new Promise(function (resolve, reject) {
resolve();
});
};
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-12-14 08:43:03 -07:00
window.MediaController.registerPlayer(window.MediaPlayer);
2015-12-30 13:25:17 -07:00
window.MediaController.setActivePlayer(window.MediaPlayer, window.MediaPlayer.getTargetsInternal()[0]);
2014-03-29 08:40:32 -07:00
2015-12-14 08:43:03 -07:00
})(document, setTimeout, clearTimeout, screen, setInterval, window);