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

481 lines
14 KiB
JavaScript
Raw Normal View History

2014-09-15 20:33:30 -07:00
(function (window, store) {
function getExternalPlayers() {
return JSON.parse(store.getItem('externalplayers') || '[]');
}
function getUrl(player, item) {
return 'vlc://http://www.google.com';
}
2014-09-16 20:04:10 -07:00
function getCodecLimits(maxBitrate) {
var maxWidth;
if (maxBitrate <= 1000000) {
maxWidth = 720;
}
else if (maxBitrate <= 5000000) {
maxWidth = 1280;
} else {
maxWidth = 1280;
}
2014-09-15 20:33:30 -07:00
return {
maxVideoAudioChannels: 6,
maxAudioChannels: 2,
maxVideoLevel: 50,
2014-09-16 20:04:10 -07:00
maxWidth: maxWidth,
2014-09-15 20:33:30 -07:00
maxSampleRate: 48000
};
}
function canDirectStream(mediaType, mediaSource, maxBitrate) {
// If bitrate is unknown don't direct stream
if (!mediaSource.Bitrate || mediaSource.Bitrate > maxBitrate) {
return false;
}
2014-09-16 20:04:10 -07:00
var codecLimits = getCodecLimits(maxBitrate);
2014-09-15 20:33:30 -07:00
if (mediaType == "Audio") {
return true;
}
else if (mediaType == "Video") {
var videoStream = mediaSource.MediaStreams.filter(function (s) {
return s.Type == 'Video';
})[0];
if (!videoStream) {
return false;
}
2014-09-16 20:04:10 -07:00
if (videoStream.Width && videoStream.Width > codecLimits.maxWidth) {
return false;
}
2014-09-16 18:38:50 -07:00
if (mediaSource.VideoType != 'VideoFile') {
return false;
}
2014-09-15 20:33:30 -07:00
return mediaSource.Protocol == 'File';
}
throw new Error('Unrecognized MediaType');
}
2014-09-16 20:04:10 -07:00
function canPlayAudioStreamDirect(audioStream, isVideo, maxBitrate) {
2014-09-15 20:33:30 -07:00
var audioCodec = (audioStream.Codec || '').toLowerCase().replace('-', '');
if (audioCodec.indexOf('aac') == -1 &&
audioCodec.indexOf('mp3') == -1 &&
audioCodec.indexOf('mpeg') == -1) {
return false;
}
2014-09-16 20:04:10 -07:00
var codecLimits = getCodecLimits(maxBitrate);
2014-09-15 20:33:30 -07:00
var maxChannels = isVideo ? codecLimits.maxVideoAudioChannels : codecLimits.maxAudioChannels;
if (!audioStream.Channels || audioStream.Channels > maxChannels) {
return false;
}
if (!audioStream.SampleRate || audioStream.SampleRate > codecLimits.maxSampleRate) {
return false;
}
var bitrate = audioStream.BitRate;
if (!bitrate) {
return false;
}
if (isVideo) {
if (audioCodec.indexOf('aac') != -1 && bitrate > 768000) {
return false;
}
if (audioCodec.indexOf('mp3') != -1 || audioCodec.indexOf('mpeg') != -1) {
if (bitrate > 320000) {
return false;
}
}
} else {
if (bitrate > 320000) {
return false;
}
}
return true;
}
function isSupportedCodec(mediaType, mediaSource) {
if (mediaType == "Audio") {
return false;
}
else if (mediaType == "Video") {
return mediaSource.MediaStreams.filter(function (m) {
return m.Type == "Video" && (m.Codec || '').toLowerCase() == 'h264';
}).length > 0;
}
throw new Error('Unrecognized MediaType');
}
function getStreamByIndex(streams, type, index) {
return streams.filter(function (s) {
return s.Type == type && s.Index == index;
})[0];
}
function getMediaSourceInfo(item, maxBitrate, mediaSourceId, audioStreamIndex, subtitleStreamIndex) {
var sources = item.MediaSources.filter(function (m) {
m.audioStream = mediaSourceId == m.Id && audioStreamIndex != null ?
getStreamByIndex(m.MediaStreams, 'Audio', audioStreamIndex) :
getStreamByIndex(m.MediaStreams, 'Audio', m.DefaultAudioStreamIndex);
if (item.MediaType == "Audio" && !m.audioStream) {
m.audioStream = m.MediaStreams.filter(function (s) {
return s.Type == 'Audio';
})[0];
}
m.subtitleStream = mediaSourceId == m.Id && subtitleStreamIndex != null ?
getStreamByIndex(m.MediaStreams, 'Subtitle', subtitleStreamIndex) :
getStreamByIndex(m.MediaStreams, 'Subtitle', m.DefaultSubtitleStreamIndex);
return !mediaSourceId || m.Id == mediaSourceId;
});
// Find first one that can be direct streamed
var source = sources.filter(function (m) {
var audioStream = m.audioStream;
2014-09-16 20:04:10 -07:00
if (!audioStream || !canPlayAudioStreamDirect(audioStream, item.MediaType == 'Video', maxBitrate)) {
2014-09-15 20:33:30 -07:00
return false;
}
2014-09-16 18:38:50 -07:00
if (m.subtitleStream && m.subtitleStream.IsExternal) {
2014-09-15 20:33:30 -07:00
return false;
}
return canDirectStream(item.MediaType, m, maxBitrate, audioStream);
})[0];
if (source) {
return {
mediaSource: source,
isStatic: true,
streamContainer: source.Container
};
}
// Find first one with supported codec
source = sources.filter(function (m) {
return isSupportedCodec(item.MediaType, m);
})[0];
source = source || sources[0];
var container = item.MediaType == 'Audio' ? 'mp3' : 'm3u8';
// Default to first one
return {
mediaSource: source,
isStatic: false,
streamContainer: container
};
}
function getStreamInfo(serverAddress, deviceId, item, startPositionTicks, maxBitrate, mediaSourceId, audioStreamIndex, subtitleStreamIndex) {
var mediaSourceInfo = getMediaSourceInfo(item, maxBitrate, mediaSourceId, audioStreamIndex, subtitleStreamIndex);
var url = getStreamUrl(serverAddress, deviceId, item.MediaType, item.Id, mediaSourceInfo, startPositionTicks, maxBitrate);
2014-09-16 18:38:50 -07:00
if (mediaSourceInfo.subtitleStream && mediaSourceInfo.subtitleStream.IsExternal) {
2014-09-15 20:33:30 -07:00
url += "&SubtitleStreamIndex=" + mediaSourceInfo.Index;
}
mediaSourceInfo.url = url;
return mediaSourceInfo;
}
function getStreamUrl(serverAddress, deviceId, mediaType, itemId, mediaSourceInfo, startPositionTicks, maxBitrate) {
var url;
2014-09-16 20:04:10 -07:00
var codecLimits = getCodecLimits(maxBitrate);
2014-09-15 20:33:30 -07:00
if (mediaType == 'Audio') {
2015-01-17 12:30:23 -07:00
url = serverAddress + '/audio/' + itemId + '/stream.' + mediaSourceInfo.streamContainer;
2014-09-15 20:33:30 -07:00
url += '?mediasourceid=' + mediaSourceInfo.mediaSource.Id;
if (mediaSourceInfo.isStatic) {
url += '&static=true';
} else {
url += '&maxaudiochannels=' + codecLimits.maxAudioChannels;
if (startPositionTicks) {
url += '&startTimeTicks=' + startPositionTicks.toString();
}
if (maxBitrate) {
url += '&audiobitrate=' + Math.min(maxBitrate, 320000).toString();
}
url += '&deviceId=' + deviceId;
}
return url;
}
else if (mediaType == 'Video') {
if (mediaSourceInfo.isStatic) {
2015-01-17 12:30:23 -07:00
url = serverAddress + '/videos/' + itemId + '/stream.' + mediaSourceInfo.streamContainer + '?static=true';
2014-09-15 20:33:30 -07:00
}
else {
2015-01-17 12:30:23 -07:00
url = serverAddress + '/videos/' + itemId + '/stream.' + mediaSourceInfo.streamContainer + '?static=false';
2014-09-15 20:33:30 -07:00
}
url += '&maxaudiochannels=' + codecLimits.maxVideoAudioChannels;
if (maxBitrate) {
var audioRate = 320000;
url += '&audiobitrate=' + audioRate.toString();
url += '&videobitrate=' + (maxBitrate - audioRate).toString();
}
url += '&profile=high';
url += '&level=41';
url += '&maxwidth=' + codecLimits.maxWidth;
url += '&videoCodec=h264';
url += '&audioCodec=aac';
url += '&mediasourceid=' + mediaSourceInfo.mediaSource.Id;
url += '&deviceId=' + deviceId;
return url;
}
throw new Error('Unrecognized MediaType');
}
function getVideoUrl(item) {
2014-09-16 18:38:50 -07:00
var maxBitrate = AppSettings.maxStreamingBitrate();
2014-09-15 20:33:30 -07:00
var info = getStreamInfo(ApiClient.serverAddress(), ApiClient.deviceId(), item, null, maxBitrate);
return info.url;
}
function getPlayerUrl(item, player) {
return player.scheme.replace('{0}', getVideoUrl(item));
}
function showPostPlayMenu(item, userId) {
$('.externalPlayerPostPlayFlyout').popup("close").remove();
var html = '<div data-role="popup" class="externalPlayerPostPlayFlyout" data-history="false" data-theme="a" data-dismissible="false">';
html += '<ul data-role="listview" style="min-width: 220px;">';
html += '<li data-role="list-divider" style="padding: 1em;text-align:center;">' + Globalize.translate('HeaderExternalPlayerPlayback') + '</li>';
html += '</ul>';
html += '<div style="padding:1em;">';
2014-09-16 18:38:50 -07:00
var autoMarkWatched = item.RunTimeTicks;
if (item.RunTimeTicks && item.RunTimeTicks >= 3000000000) {
autoMarkWatched = false;
html += '<fieldset data-role="controlgroup">';
html += '<legend>' + Globalize.translate('LabelMarkAs') + '</legend>';
html += '<label for="radioMarkUnwatched">' + Globalize.translate('OptionUnwatched') + '</label>';
html += '<input type="radio" id="radioMarkUnwatched" name="radioGroupMarkPlaystate" class="radioPlaystate" />';
html += '<label for="radioMarkWatched">' + Globalize.translate('OptionWatched') + '</label>';
html += '<input type="radio" id="radioMarkWatched" checked="checked" name="radioGroupMarkPlaystate" class="radioPlaystate" />';
html += '<label for="radioMarkInProgress">' + Globalize.translate('OptionInProgress') + '</label>';
html += '<input type="radio" id="radioMarkInProgress" name="radioGroupMarkPlaystate" class="radioPlaystate" />';
html += '</fieldset>';
html += '<br/>';
html += '<p style="margin-top: 0;">' + Globalize.translate('LabelResumePoint') + '</p>';
html += '<div class="sliderContainer" style="display:block;margin-top:4px;">';
html += '<input class="playstateSlider" type="range" step=".001" min="0" max="100" value="0" style="display:none;" data-theme="a" data-highlight="true" />';
2014-09-15 20:33:30 -07:00
html += '</div>';
2014-09-16 18:38:50 -07:00
html += '<div class="sliderValue" style="text-align:center;margin:2px 0 4px;">0:00:00</div>';
2014-09-15 20:33:30 -07:00
html += '<br/>';
}
html += '<button type="button" class="btnDone" data-theme="b" data-icon="check">' + Globalize.translate('ButtonImDone') + '</button>';
html += '</div>';
html += '</div>';
$(document.body).append(html);
var elem = $('.externalPlayerPostPlayFlyout').popup({}).trigger('create').popup("open").on("popupafterclose", function () {
$(this).off("popupafterclose").remove();
});
2014-09-16 18:38:50 -07:00
$('.radioPlaystate', elem).on('change', function () {
if ($('#radioMarkInProgress', elem).checked()) {
$('.playstateSlider', elem).slider('enable');
} else {
$('.playstateSlider', elem).slider('disable');
}
}).trigger('change');
2014-09-15 20:33:30 -07:00
$('.btnDone', elem).on('click', function () {
$('.externalPlayerPostPlayFlyout').popup("close").remove();
ApiClient.stopActiveEncodings();
2014-09-16 18:38:50 -07:00
if ($('#radioMarkInProgress', elem).checked()) {
var pct = $(".playstateSlider", elem).val();
var ticks = item.RunTimeTicks * (Number(pct) * .01);
ApiClient.markPlayed(userId, item.Id, new Date());
}
else if (autoMarkWatched || $('#radioMarkWatched', elem).checked()) {
2014-09-15 20:33:30 -07:00
ApiClient.markPlayed(userId, item.Id, new Date());
2014-09-16 18:38:50 -07:00
}
else if ($('#radioMarkUnwatched', elem).checked()) {
2014-09-15 20:33:30 -07:00
2014-09-16 18:38:50 -07:00
ApiClient.markUnplayed(userId, item.Id);
2014-09-15 20:33:30 -07:00
}
});
2014-09-16 18:38:50 -07:00
$(".playstateSlider", elem).on("change", function (e) {
var pct = $(this).val();
var time = item.RunTimeTicks * (Number(pct) * .01);
var tooltext = Dashboard.getDisplayTime(time);
$('.sliderValue', elem).html(tooltext);
console.log("slidin", pct, self.currentDurationTicks, time);
});
2014-09-15 20:33:30 -07:00
}
function closePlayMenu() {
$('.externalPlayerFlyout').popup("close").remove();
}
function showMenuForItem(item, userId) {
closePlayMenu();
2014-10-25 11:32:58 -07:00
var html = '<div data-role="popup" class="externalPlayerFlyout" data-theme="a">';
2014-09-15 20:33:30 -07:00
html += '<ul data-role="listview" style="min-width: 200px;">';
2014-09-16 18:38:50 -07:00
html += '<li data-role="list-divider" style="padding: 1em;text-align:center;">' + Globalize.translate('HeaderSelectExternalPlayer') + '</li>';
2014-09-15 20:33:30 -07:00
html += '</ul>';
html += '<div style="padding:1em;">';
html += getExternalPlayers().map(function (p) {
return '<a href="' + getPlayerUrl(item, p) + '" data-role="button" data-icon="play" class="btnExternalPlayer">' + p.name + '</a>';
}).join('');
html += '</div>';
html += '</div>';
$(document.body).append(html);
var elem = $('.externalPlayerFlyout').popup({}).trigger('create').popup("open").on("popupafterclose", function () {
$(this).off("popupafterclose").remove();
});
$('.btnExternalPlayer', elem).on('click', function () {
closePlayMenu();
setTimeout(function () {
showPostPlayMenu(item, userId);
}, 500);
});
}
function showPlayMenu(itemId) {
var userId = Dashboard.getCurrentUserId();
ApiClient.getItem(userId, itemId).done(function (item) {
setTimeout(function () {
showMenuForItem(item, userId);
}, 500);
});
}
window.ExternalPlayer = {
getUrl: getUrl,
getExternalPlayers: getExternalPlayers,
showMenu: showPlayMenu
};
})(window, window.store);