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

364 lines
14 KiB
JavaScript
Raw Normal View History

2013-04-10 14:03:28 -07:00
(function (document, clearTimeout, screen, localStorage, _V_, $, setInterval) {
2013-02-20 18:33:05 -07:00
2013-04-10 14:03:28 -07:00
function mediaPlayer() {
var self = this;
2013-04-08 21:26:02 -07:00
2013-04-10 14:03:28 -07:00
var testableAudioElement = document.createElement('audio');
var testableVideoElement = document.createElement('video');
var currentMediaElement;
var currentProgressInterval;
2013-04-08 21:26:02 -07:00
2013-04-10 14:03:28 -07:00
function playAudio(items, params) {
var item = items[0];
2013-04-08 21:26:02 -07:00
2013-04-10 14:03:28 -07:00
var baseParams = {
audioChannels: 2,
audioBitrate: 128000
};
2013-04-08 21:26:02 -07:00
2013-04-10 14:03:28 -07:00
$.extend(baseParams, params);
2013-04-08 21:26:02 -07:00
2013-04-10 14:03:28 -07:00
var mp3Url = ApiClient.getUrl('Audio/' + item.Id + '/stream.mp3', $.extend({}, baseParams, {
audioCodec: 'mp3'
}));
2013-04-08 21:26:02 -07:00
2013-04-10 14:03:28 -07:00
var aacUrl = ApiClient.getUrl('Audio/' + item.Id + '/stream.aac', $.extend({}, baseParams, {
audioCodec: 'aac'
}));
2013-04-08 21:26:02 -07:00
2013-04-10 14:03:28 -07:00
var webmUrl = ApiClient.getUrl('Audio/' + item.Id + '/stream.webm', $.extend({}, baseParams, {
audioCodec: 'Vorbis'
}));
2013-04-08 21:26:02 -07:00
2013-04-10 14:03:28 -07:00
/* ffmpeg always says the ogg stream is corrupt after conversion
var oggUrl = ApiClient.getUrl('Audio/' + item.Id + '/stream.oga', $.extend({}, baseParams, {
audioCodec: 'Vorbis'
}));
*/
2013-04-08 21:26:02 -07:00
2013-04-10 14:03:28 -07:00
var html = '';
html += '<audio class="itemAudio" preload="none" controls autoplay>';
html += '<source type="audio/mpeg" src="' + mp3Url + '" />';
html += '<source type="audio/aac" src="' + aacUrl + '" />';
html += '<source type="audio/webm" src="' + webmUrl + '" />';
//html += '<source type="audio/ogg" src="' + oggUrl + '" />';
html += '</audio';
2013-04-08 21:26:02 -07:00
2013-04-10 14:03:28 -07:00
var nowPlayingBar = $('#nowPlayingBar').show();
//show stop button
$('#stopButton', nowPlayingBar).show();
2013-04-08 21:26:02 -07:00
2013-04-10 14:03:28 -07:00
$('#mediaElement', nowPlayingBar).html(html);
2013-04-08 21:26:02 -07:00
2013-04-10 14:03:28 -07:00
return $('audio', nowPlayingBar)[0];
}
2013-04-08 21:26:02 -07:00
2013-04-10 14:03:28 -07:00
function playVideo(items, startPosition) {
//stop/kill videoJS
if (currentMediaElement) self.stop();
var item = items[0];
// Account for screen rotation. Use the larger dimension as the width.
var screenWidth = Math.max(screen.height, screen.width);
var screenHeight = Math.min(screen.height, screen.width);
var volume = localStorage.getItem("volume") || 0.5;
var user = Dashboard.getCurrentUser();
var defaults = { languageIndex: null, subtitleIndex: null };
var userConfig = user.Configuration || {};
if (item.MediaStreams && item.MediaStreams.length) {
$.each(item.MediaStreams, function (i, stream) {
//get default subtitle stream
if (stream.Type == "Subtitle") {
if (userConfig.UseForcedSubtitlesOnly == true && userConfig.SubtitleLanguagePreference && !defaults.subtitleIndex) {
if (stream.Language == userConfig.SubtitleLanguagePreference && stream.IsForced == true) {
defaults.subtitleIndex = i;
}
} else if (userConfig.SubtitleLanguagePreference && !defaults.subtitleIndex) {
if (stream.Language == userConfig.SubtitleLanguagePreference) {
defaults.subtitleIndex = i;
}
} else if (userConfig.UseForcedSubtitlesOnly == true && !defaults.subtitleIndex) {
if (stream.IsForced == true) {
defaults.subtitleIndex = i;
}
2013-04-08 21:26:02 -07:00
}
2013-04-10 14:03:28 -07:00
} else if (stream.Type == "Audio") {
//get default language stream
if (userConfig.AudioLanguagePreference && !defaults.languageIndex) {
if (stream.Language == userConfig.AudioLanguagePreference) {
defaults.languageIndex = i;
}
2013-04-08 21:26:02 -07:00
}
}
2013-04-10 14:03:28 -07:00
});
}
2013-02-20 18:33:05 -07:00
2013-04-10 14:03:28 -07:00
var baseParams = {
audioChannels: 2,
audioBitrate: 128000,
videoBitrate: 1500000,
maxWidth: screenWidth,
maxHeight: screenHeight,
StartTimeTicks: 0,
SubtitleStreamIndex: null,
AudioStreamIndex: null
};
if (typeof (startPosition) != "undefined") {
baseParams['StartTimeTicks'] = startPosition;
}
2013-04-08 21:26:02 -07:00
2013-04-10 14:03:28 -07:00
var html = '<video id="videoWindow" class="itemVideo video-js vjs-default-skin"></video>';
2013-02-20 18:33:05 -07:00
2013-04-10 14:03:28 -07:00
var nowPlayingBar = $('#nowPlayingBar');
//hide stop button
$('#stopButton', nowPlayingBar).hide();
2013-02-20 18:33:05 -07:00
2013-04-10 14:03:28 -07:00
$('#mediaElement', nowPlayingBar).addClass("video").html(html).show();
2013-02-20 18:33:05 -07:00
2013-04-10 14:03:28 -07:00
_V_("videoWindow", { 'controls': true, 'autoplay': true, 'preload': 'auto' }, function () {
2013-02-20 18:33:05 -07:00
2013-04-10 14:03:28 -07:00
var mp4VideoUrl = ApiClient.getUrl('Videos/' + item.Id + '/stream.mp4', $.extend({}, baseParams, {
videoCodec: 'h264',
audioCodec: 'aac'
}));
2013-04-10 14:03:28 -07:00
var tsVideoUrl = ApiClient.getUrl('Videos/' + item.Id + '/stream.ts', $.extend({}, baseParams, {
videoCodec: 'h264',
audioCodec: 'aac'
}));
2013-02-20 18:33:05 -07:00
2013-04-10 14:03:28 -07:00
var webmVideoUrl = ApiClient.getUrl('Videos/' + item.Id + '/stream.webm', $.extend({}, baseParams, {
videoCodec: 'vpx',
audioCodec: 'Vorbis'
}));
2013-02-20 18:33:05 -07:00
2013-04-10 14:03:28 -07:00
var hlsVideoUrl = ApiClient.getUrl('Videos/' + item.Id + '/stream.m3u8', $.extend({}, baseParams, {
videoCodec: 'h264',
audioCodec: 'aac'
}));
2013-02-20 18:33:05 -07:00
2013-04-10 14:03:28 -07:00
var ogvVideoUrl = ApiClient.getUrl('Videos/' + item.Id + '/stream.ogv', $.extend({}, baseParams, {
videoCodec: 'theora',
audioCodec: 'Vorbis'
}));
2013-02-20 18:33:05 -07:00
2013-04-10 14:03:28 -07:00
(this).src([
{ type: "application/x-mpegURL", src: hlsVideoUrl },
{ type: "video/webm", src: webmVideoUrl },
{ type: "video/mp4", src: mp4VideoUrl },
{ type: "video/mp2t; codecs='h264, aac'", src: tsVideoUrl },
{ type: "video/ogg", src: ogvVideoUrl }]
).volume(volume);
2013-02-20 18:33:05 -07:00
2013-04-10 14:03:28 -07:00
videoJSextension.setup_video($('#videoWindow'), item, defaults);
2013-02-20 18:33:05 -07:00
2013-04-10 14:03:28 -07:00
(this).addEvent("loadstart", function () {
$(".vjs-remaining-time-display").hide();
});
2013-02-27 10:53:42 -07:00
2013-04-10 14:03:28 -07:00
(this).addEvent("durationchange", function () {
if ((this).duration() != "Infinity")
$(".vjs-remaining-time-display").show();
});
2013-02-20 18:33:05 -07:00
2013-04-10 14:03:28 -07:00
(this).addEvent("volumechange", function () {
localStorage.setItem("volume", (this).volume());
});
2013-02-20 18:33:05 -07:00
2013-04-10 14:03:28 -07:00
(this).addEvent("play", updateProgress);
ApiClient.reportPlaybackStart(Dashboard.getCurrentUserId(), item.Id);
2013-04-08 21:26:02 -07:00
});
2013-02-20 18:33:05 -07:00
2013-04-10 14:03:28 -07:00
return $('video', nowPlayingBar)[0];
}
2013-02-27 10:53:42 -07:00
2013-04-10 14:03:28 -07:00
function updateProgress() {
currentProgressInterval = setInterval(function () {
var player = _V_("videoWindow");
2013-02-20 18:33:05 -07:00
2013-04-10 14:03:28 -07:00
var startTimeTicks = player.tag.src.match(new RegExp("StartTimeTicks=[0-9]+", "g"));
var startTime = startTimeTicks[0].replace("StartTimeTicks=", "");
2013-02-20 18:33:05 -07:00
2013-04-10 14:03:28 -07:00
var itemString = player.tag.src.match(new RegExp("Videos/[0-9a-z\-]+", "g"));
var itemId = itemString[0].replace("Videos/", "");
2013-02-20 18:33:05 -07:00
2013-04-10 14:03:28 -07:00
var positionTicks = parseInt(startTime) + Math.floor(10000000 * player.currentTime());
2013-04-10 14:03:28 -07:00
ApiClient.reportPlaybackProgress(Dashboard.getCurrentUserId(), itemId, positionTicks);
}, 30000);
}
2013-04-10 14:03:28 -07:00
self.canPlay = function (item) {
var media;
2013-03-31 18:52:07 -07:00
2013-04-10 14:03:28 -07:00
if (item.MediaType === "Video") {
media = testableVideoElement;
if (media.canPlayType) {
2013-04-10 14:03:28 -07:00
return media.canPlayType('video/mp4').replace(/no/, '') || media.canPlayType('video/mp2t').replace(/no/, '') || media.canPlayType('video/webm').replace(/no/, '') || media.canPlayType('application/x-mpegURL').replace(/no/, '') || media.canPlayType('video/ogv').replace(/no/, '');
}
2013-04-08 21:16:32 -07:00
2013-04-10 14:03:28 -07:00
return false;
2013-04-08 21:26:02 -07:00
}
2013-04-08 21:16:32 -07:00
2013-04-10 14:03:28 -07:00
if (item.MediaType === "Audio") {
media = testableAudioElement;
if (media.canPlayType) {
2013-04-08 21:16:32 -07:00
2013-04-10 14:03:28 -07:00
return media.canPlayType('audio/mpeg').replace(/no/, '') || media.canPlayType('audio/webm').replace(/no/, '') || media.canPlayType('audio/aac').replace(/no/, '') || media.canPlayType('audio/ogg').replace(/no/, '');
}
2013-04-08 21:16:32 -07:00
2013-04-10 14:03:28 -07:00
return false;
2013-04-08 21:16:32 -07:00
}
2013-04-08 21:26:02 -07:00
return false;
2013-04-10 14:03:28 -07:00
};
2013-04-08 21:16:32 -07:00
2013-04-10 14:03:28 -07:00
self.play = function (items, startPosition) {
2013-04-08 21:16:32 -07:00
2013-04-10 14:03:28 -07:00
if (self.isPlaying()) {
self.stop();
}
2013-04-08 21:16:32 -07:00
2013-04-10 14:03:28 -07:00
var item = items[0];
2013-04-08 21:16:32 -07:00
2013-04-10 14:03:28 -07:00
var mediaElement;
2013-04-08 21:16:32 -07:00
2013-04-10 14:03:28 -07:00
if (item.MediaType === "Video") {
2013-04-08 21:16:32 -07:00
2013-04-10 14:03:28 -07:00
mediaElement = playVideo(items, startPosition);
} else if (item.MediaType === "Audio") {
2013-04-08 21:16:32 -07:00
2013-04-10 14:03:28 -07:00
mediaElement = playAudio(items);
}
2013-04-08 21:16:32 -07:00
2013-04-10 14:03:28 -07:00
if (!mediaElement) {
return;
}
2013-04-08 21:16:32 -07:00
2013-04-10 14:03:28 -07:00
currentMediaElement = mediaElement;
2013-04-08 21:16:32 -07:00
2013-04-10 14:03:28 -07:00
var nowPlayingBar = $('#nowPlayingBar').show();
2013-04-08 21:16:32 -07:00
2013-04-10 14:03:28 -07:00
if (items.length > 1) {
$('#previousTrackButton', nowPlayingBar)[0].disabled = false;
$('#nextTrackButton', nowPlayingBar)[0].disabled = false;
} else {
$('#previousTrackButton', nowPlayingBar)[0].disabled = true;
$('#nextTrackButton', nowPlayingBar)[0].disabled = true;
}
2013-04-08 21:16:32 -07:00
2013-04-10 14:03:28 -07:00
//display image and title
var imageTags = item.ImageTags || {};
var html = '';
var url = "";
if (item.BackdropImageTags && item.BackdropImageTags.length) {
url = ApiClient.getImageUrl(item.Id, {
type: "Backdrop",
height: 36,
tag: item.BackdropImageTags[0]
});
} else if (imageTags.Thumb) {
url = ApiClient.getImageUrl(item.Id, {
type: "Thumb",
height: 36,
tag: item.ImageTags.Thumb
});
} else if (imageTags.Primary) {
url = ApiClient.getImageUrl(item.Id, {
type: "Primary",
height: 36,
tag: item.ImageTags.Primary
});
} else {
url = "css/images/items/detail/video.png";
}
2013-04-08 21:16:32 -07:00
2013-04-10 14:03:28 -07:00
var name = item.Name;
var seriesName = '';
2013-04-08 21:16:32 -07:00
2013-04-10 14:03:28 -07:00
if (item.IndexNumber != null) {
name = item.IndexNumber + " - " + name;
}
if (item.ParentIndexNumber != null) {
name = item.ParentIndexNumber + "." + name;
}
if (item.SeriesName || item.Album || item.ProductionYear) {
seriesName = item.SeriesName || item.Album || item.ProductionYear;
}
2013-04-08 21:16:32 -07:00
2013-04-10 14:03:28 -07:00
html += "<div><img class='nowPlayingBarImage ' alt='' title='' src='" + url + "' style='height:36px;display:inline-block;' /></div>";
if (item.Type == "Movie")
html += '<div>' + name + '<br/>' + seriesName + '</div>';
else
html += '<div>' + seriesName + '<br/>' + name + '</div>';
2013-04-10 14:03:28 -07:00
$('#mediaInfo', nowPlayingBar).html(html);
};
2013-04-10 14:03:28 -07:00
self.stop = function () {
2013-03-31 18:52:07 -07:00
2013-04-10 14:03:28 -07:00
var elem = currentMediaElement;
2013-03-31 18:52:07 -07:00
2013-04-10 14:03:28 -07:00
//check if it's a video using VideoJS
if ($(elem).hasClass("vjs-tech")) {
var player = _V_("videoWindow");
2013-02-20 18:33:05 -07:00
2013-04-10 14:03:28 -07:00
var startTimeTicks = player.tag.src.match(new RegExp("StartTimeTicks=[0-9]+", "g"));
var startTime = startTimeTicks[0].replace("StartTimeTicks=", "");
2013-03-25 17:56:31 -07:00
2013-04-10 14:03:28 -07:00
var itemString = player.tag.src.match(new RegExp("Videos/[0-9a-z\-]+", "g"));
var itemId = itemString[0].replace("Videos/", "");
2013-03-26 14:32:01 -07:00
2013-04-10 14:03:28 -07:00
var positionTicks = parseInt(startTime) + Math.floor(10000000 * player.currentTime());
2013-04-05 19:44:57 -07:00
2013-04-10 14:03:28 -07:00
ApiClient.reportPlaybackStopped(Dashboard.getCurrentUserId(), itemId, positionTicks);
2013-04-05 19:44:57 -07:00
2013-04-10 14:03:28 -07:00
if (currentProgressInterval) {
clearTimeout(currentProgressInterval);
}
2013-03-26 14:32:01 -07:00
2013-04-10 14:03:28 -07:00
if (player.techName == "html5") {
player.tag.src = "";
player.tech.removeTriggers();
player.load();
}
2013-02-20 18:33:05 -07:00
2013-04-10 14:03:28 -07:00
//remove custom buttons
delete _V_.ControlBar.prototype.options.components.ResolutionSelectorButton;
delete _V_.ControlBar.prototype.options.components.SubtitleSelectorButton;
delete _V_.ControlBar.prototype.options.components.LanguageSelectorButton;
delete _V_.ControlBar.prototype.options.components.ChapterSelectorButton;
//player.tech.destroy();
player.destroy();
} else {
elem.pause();
elem.src = "";
2013-04-08 21:26:02 -07:00
}
2013-02-20 18:33:05 -07:00
2013-04-10 14:03:28 -07:00
$(elem).remove();
2013-02-27 10:53:42 -07:00
2013-04-10 14:03:28 -07:00
$('#nowPlayingBar').hide();
2013-03-26 14:32:01 -07:00
2013-04-10 14:03:28 -07:00
currentMediaElement = null;
};
2013-03-26 14:32:01 -07:00
2013-04-10 14:03:28 -07:00
self.isPlaying = function () {
return currentMediaElement;
};
}
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
2013-04-08 22:06:13 -07:00
})(document, clearTimeout, screen, localStorage, _V_, $, setInterval);