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

650 lines
19 KiB
JavaScript
Raw Normal View History

2015-06-07 20:16:42 -07:00
(function () {
2015-06-13 07:46:59 -07:00
var supportsTextTracks;
2015-11-20 13:46:00 -07:00
var hlsPlayer;
2015-07-13 14:26:11 -07:00
var requiresSettingStartTimeOnStart;
2015-12-23 10:46:01 -07:00
var subtitleTrackIndexToSetOnPlaying;
2015-06-13 07:46:59 -07:00
2015-07-03 09:49:49 -07:00
function htmlMediaRenderer(options) {
2015-06-07 20:16:42 -07:00
var mediaElement;
var self = this;
2015-09-08 07:35:52 -07:00
function hideStatusBar() {
if (options.type == 'video' && window.StatusBar) {
2015-09-23 19:31:40 -07:00
//StatusBar.backgroundColorByName("black");
2015-09-21 08:43:10 -07:00
//StatusBar.overlaysWebView(true);
2015-09-08 07:35:52 -07:00
StatusBar.hide();
}
}
function showStatusBar() {
if (options.type == 'video' && window.StatusBar) {
StatusBar.show();
2015-09-21 08:43:10 -07:00
//StatusBar.overlaysWebView(false);
2015-09-08 07:35:52 -07:00
}
}
2015-06-07 20:16:42 -07:00
function onEnded() {
2015-09-08 07:35:52 -07:00
showStatusBar();
2015-12-23 10:46:01 -07:00
Events.trigger(self, 'ended');
2015-06-07 20:16:42 -07:00
}
function onTimeUpdate() {
2015-07-13 14:26:11 -07:00
2015-11-20 13:46:00 -07:00
//if (isViblastStarted) {
2015-07-13 14:26:11 -07:00
2015-11-20 13:46:00 -07:00
// // This is a workaround for viblast not stopping playback at the end
// var time = this.currentTime;
// var duration = this.duration;
2015-07-13 14:26:11 -07:00
2015-11-20 13:46:00 -07:00
// if (duration) {
// if (time >= (duration - 1)) {
2015-07-15 04:26:47 -07:00
2015-11-20 13:46:00 -07:00
// //onEnded();
// return;
// }
// }
//}
2015-07-13 14:26:11 -07:00
2015-12-23 10:46:01 -07:00
Events.trigger(self, 'timeupdate');
2015-06-07 20:16:42 -07:00
}
function onVolumeChange() {
2015-12-23 10:46:01 -07:00
Events.trigger(self, 'volumechange');
2015-06-07 20:16:42 -07:00
}
2015-12-23 10:46:01 -07:00
function onOneAudioPlaying(e) {
var elem = e.target;
elem.removeEventListener('playing', onOneAudioPlaying);
2015-06-07 20:16:42 -07:00
$('.mediaPlayerAudioContainer').hide();
}
function onPlaying() {
2015-12-23 10:46:01 -07:00
Events.trigger(self, 'playing');
2015-06-07 20:16:42 -07:00
}
function onPlay() {
2015-12-23 10:46:01 -07:00
Events.trigger(self, 'play');
2015-06-07 20:16:42 -07:00
}
function onPause() {
2015-12-23 10:46:01 -07:00
Events.trigger(self, 'pause');
2015-06-07 20:16:42 -07:00
}
function onClick() {
2015-12-23 10:46:01 -07:00
Events.trigger(self, 'click');
2015-06-07 20:16:42 -07:00
}
function onDblClick() {
2015-12-23 10:46:01 -07:00
Events.trigger(self, 'dblclick');
2015-06-07 20:16:42 -07:00
}
2015-12-23 10:46:01 -07:00
function onError(e) {
2015-06-07 20:16:42 -07:00
2015-12-23 10:46:01 -07:00
var elem = e.target;
var errorCode = elem.error ? elem.error.code : '';
console.log('Media element error code: ' + errorCode);
2015-06-07 20:16:42 -07:00
2015-09-08 07:35:52 -07:00
showStatusBar();
2015-12-23 10:46:01 -07:00
Events.trigger(self, 'error');
2015-06-07 20:16:42 -07:00
}
2015-12-23 10:46:01 -07:00
function onLoadedMetadata(e) {
var elem = e.target;
elem.removeEventListener('loadedmetadata', onLoadedMetadata);
2015-06-07 20:16:42 -07:00
2015-11-20 13:46:00 -07:00
if (!hlsPlayer) {
2015-12-23 10:46:01 -07:00
elem.play();
2015-06-07 20:16:42 -07:00
}
}
2015-11-20 13:46:00 -07:00
function requireHlsPlayer(callback) {
2015-12-15 22:30:14 -07:00
require(['hlsjs'], function (hls) {
2015-11-20 13:46:00 -07:00
window.Hls = hls;
2015-07-09 20:00:03 -07:00
callback();
});
}
2015-07-13 14:26:11 -07:00
function getStartTime(url) {
2015-06-07 20:16:42 -07:00
2015-07-13 14:26:11 -07:00
var src = url;
2015-06-07 20:16:42 -07:00
2015-07-03 04:51:45 -07:00
var parts = src.split('#');
2015-06-07 20:16:42 -07:00
if (parts.length > 1) {
parts = parts[parts.length - 1].split('=');
if (parts.length == 2) {
2015-07-13 14:26:11 -07:00
return parseFloat(parts[1]);
}
}
2015-06-07 20:16:42 -07:00
2015-07-13 14:26:11 -07:00
return 0;
}
2015-12-23 10:46:01 -07:00
function onOneVideoPlaying(e) {
2015-07-13 14:26:11 -07:00
2015-09-08 07:35:52 -07:00
hideStatusBar();
2015-12-23 10:46:01 -07:00
var element = e.target;
element.removeEventListener('playing', onOneVideoPlaying);
self.setCurrentTrackElement(subtitleTrackIndexToSetOnPlaying);
2015-07-13 14:26:11 -07:00
var requiresNativeControls = !self.enableCustomVideoControls();
if (requiresNativeControls) {
2015-12-23 10:46:01 -07:00
$(element).attr('controls', 'controls');
2015-07-13 14:26:11 -07:00
}
if (requiresSettingStartTimeOnStart) {
var src = (self.currentSrc() || '').toLowerCase();
var startPositionInSeekParam = getStartTime(src);
// Appending #t=xxx to the query string doesn't seem to work with HLS
if (startPositionInSeekParam && src.indexOf('.m3u8') != -1) {
2015-11-20 13:46:00 -07:00
2015-12-14 08:43:03 -07:00
var delay = browserInfo.safari ? 2500 : 0;
2015-11-20 13:46:00 -07:00
if (delay) {
setTimeout(function () {
element.currentTime = startPositionInSeekParam;
}, delay);
} else {
2015-07-13 14:26:11 -07:00
element.currentTime = startPositionInSeekParam;
2015-11-20 13:46:00 -07:00
}
2015-06-07 20:16:42 -07:00
}
}
}
function createAudioElement() {
var elem = $('.mediaPlayerAudio');
if (!elem.length) {
var html = '';
var requiresControls = !MediaPlayer.canAutoPlayAudio();
if (requiresControls) {
2015-12-14 08:43:03 -07:00
html += '<div class="mediaPlayerAudioContainer" style="position: fixed;top: 40%;text-align: center;left: 0;right: 0;"><div class="mediaPlayerAudioContainerInner">';;
2015-06-07 20:16:42 -07:00
} else {
2015-12-14 08:43:03 -07:00
html += '<div class="mediaPlayerAudioContainer" style="display:none;padding: 1em;background: #222;"><div class="mediaPlayerAudioContainerInner">';;
2015-06-07 20:16:42 -07:00
}
html += '<audio class="mediaPlayerAudio" crossorigin="anonymous" controls>';
html += '</audio></div></div>';
$(document.body).append(html);
elem = $('.mediaPlayerAudio');
}
2015-12-23 10:46:01 -07:00
elem = elem[0];
elem.addEventListener('playing', onOneAudioPlaying);
elem.addEventListener('timeupdate', onTimeUpdate);
elem.addEventListener('ended', onEnded);
elem.addEventListener('volumechange', onVolumeChange);
elem.addEventListener('error', onError);
elem.addEventListener('pause', onPause);
elem.addEventListener('play', onPlay);
elem.addEventListener('playing', onPlaying);
return elem;
2015-06-07 20:16:42 -07:00
}
2015-11-20 13:46:00 -07:00
function enableHlsPlayer(src) {
2015-07-05 11:34:52 -07:00
if (src) {
if (src.indexOf('.m3u8') == -1) {
return false;
}
}
2015-07-03 04:51:45 -07:00
return MediaPlayer.canPlayHls() && !MediaPlayer.canPlayNativeHls();
}
2015-06-07 20:16:42 -07:00
function createVideoElement() {
2015-07-03 04:51:45 -07:00
var html = '';
var requiresNativeControls = !self.enableCustomVideoControls();
2015-09-09 20:22:52 -07:00
// Safari often displays the poster under the video and it doesn't look good
2015-12-14 08:43:03 -07:00
var poster = !browserInfo.safari && options.poster ? (' poster="' + options.poster + '"') : '';
2015-07-03 09:49:49 -07:00
2015-07-03 04:51:45 -07:00
// Can't autoplay in these browsers so we need to use the full controls
2015-12-14 08:43:03 -07:00
if (requiresNativeControls && AppInfo.isNativeApp && browserInfo.android) {
2015-07-03 09:49:49 -07:00
html += '<video class="itemVideo" id="itemVideo" preload="metadata" autoplay="autoplay" crossorigin="anonymous"' + poster + ' webkit-playsinline>';
2015-07-03 04:51:45 -07:00
}
else if (requiresNativeControls) {
2015-07-03 09:49:49 -07:00
html += '<video class="itemVideo" id="itemVideo" preload="metadata" autoplay="autoplay" crossorigin="anonymous"' + poster + ' controls="controls" webkit-playsinline>';
2015-07-03 04:51:45 -07:00
}
else {
// Chrome 35 won't play with preload none
2015-07-03 09:49:49 -07:00
html += '<video class="itemVideo" id="itemVideo" preload="metadata" autoplay="autoplay" crossorigin="anonymous"' + poster + ' webkit-playsinline>';
2015-07-03 04:51:45 -07:00
}
html += '</video>';
2015-12-14 08:43:03 -07:00
var elem = $('#videoElement', '#videoPlayer').prepend(html);
2015-06-07 20:16:42 -07:00
2015-12-23 10:46:01 -07:00
var itemVideo = $('.itemVideo', elem)[0];
itemVideo.addEventListener('loadedmetadata', onLoadedMetadata);
itemVideo.addEventListener('playing', onOneVideoPlaying);
itemVideo.addEventListener('timeupdate', onTimeUpdate);
itemVideo.addEventListener('ended', onEnded);
itemVideo.addEventListener('volumechange', onVolumeChange);
itemVideo.addEventListener('voluplaymechange', onPlay);
itemVideo.addEventListener('pause', onPause);
itemVideo.addEventListener('playing', onPlaying);
itemVideo.addEventListener('click', onClick);
itemVideo.addEventListener('dblclick', onDblClick);
itemVideo.addEventListener('error', onError);
return itemVideo;
2015-06-07 20:16:42 -07:00
}
2015-07-09 20:00:03 -07:00
// Save this for when playback stops, because querying the time at that point might return 0
var _currentTime;
2015-06-07 20:16:42 -07:00
self.currentTime = function (val) {
if (mediaElement) {
if (val != null) {
2015-06-25 14:50:56 -07:00
mediaElement.currentTime = val / 1000;
2015-06-07 20:16:42 -07:00
return;
}
2015-07-09 20:00:03 -07:00
if (_currentTime) {
return _currentTime * 1000;
}
2015-06-25 14:50:56 -07:00
return (mediaElement.currentTime || 0) * 1000;
2015-06-07 20:16:42 -07:00
}
};
self.duration = function (val) {
if (mediaElement) {
return mediaElement.duration;
}
return null;
};
2015-06-10 20:01:41 -07:00
self.stop = function () {
if (mediaElement) {
mediaElement.pause();
2015-07-03 04:51:45 -07:00
2015-11-20 13:46:00 -07:00
if (hlsPlayer) {
2015-07-09 20:00:03 -07:00
_currentTime = mediaElement.currentTime;
2015-07-03 09:49:49 -07:00
2015-10-26 09:21:00 -07:00
// Sometimes this fails
try {
2015-11-20 13:46:00 -07:00
hlsPlayer.destroy();
2015-10-26 09:21:00 -07:00
}
catch (err) {
2015-12-23 10:46:01 -07:00
console.log(err);
2015-10-26 09:21:00 -07:00
}
2015-11-20 13:46:00 -07:00
hlsPlayer = null;
2015-07-03 04:51:45 -07:00
}
2015-06-10 20:01:41 -07:00
}
};
2015-06-07 20:16:42 -07:00
self.pause = function () {
if (mediaElement) {
mediaElement.pause();
}
};
self.unpause = function () {
if (mediaElement) {
mediaElement.play();
}
};
self.volume = function (val) {
if (mediaElement) {
if (val != null) {
mediaElement.volume = val;
return;
}
return mediaElement.volume;
}
};
2015-07-03 04:51:45 -07:00
var currentSrc;
2015-09-15 11:09:44 -07:00
self.setCurrentSrc = function (streamInfo, item, mediaSource, tracks) {
2015-06-07 20:16:42 -07:00
var elem = mediaElement;
if (!elem) {
2015-07-03 04:51:45 -07:00
currentSrc = null;
2015-06-07 20:16:42 -07:00
return;
}
2015-09-15 11:09:44 -07:00
if (!streamInfo) {
2015-07-03 04:51:45 -07:00
currentSrc = null;
2015-06-07 20:16:42 -07:00
elem.src = null;
elem.src = "";
// When the browser regains focus it may start auto-playing the last video
2015-12-14 08:43:03 -07:00
if (browserInfo.safari) {
2015-06-07 20:16:42 -07:00
elem.src = 'files/dummy.mp4';
elem.play();
}
return;
}
2015-09-15 11:09:44 -07:00
var val = streamInfo.url;
2015-09-21 08:43:10 -07:00
2015-12-14 08:43:03 -07:00
if (AppInfo.isNativeApp && browserInfo.safari) {
2015-09-21 08:43:10 -07:00
val = val.replace('file://', '');
}
2015-07-13 14:26:11 -07:00
requiresSettingStartTimeOnStart = false;
var startTime = getStartTime(val);
2015-08-24 13:37:34 -07:00
var playNow = false;
2015-07-13 14:26:11 -07:00
2015-06-07 20:16:42 -07:00
if (elem.tagName.toLowerCase() == 'audio') {
2015-07-03 04:51:45 -07:00
elem.src = val;
2015-08-24 13:37:34 -07:00
playNow = true;
2015-07-03 04:51:45 -07:00
2015-06-07 20:16:42 -07:00
}
else {
2015-11-20 13:46:00 -07:00
if (hlsPlayer) {
hlsPlayer.destroy();
hlsPlayer = null;
2015-07-16 05:56:38 -07:00
}
2015-07-13 14:26:11 -07:00
if (startTime) {
2015-07-16 05:56:38 -07:00
2015-11-20 13:46:00 -07:00
//try {
// elem.currentTime = startTime;
//} catch (err) {
// // IE will throw an invalid state exception when trying to set currentTime before starting playback
//}
//requiresSettingStartTimeOnStart = elem.currentTime == 0;
requiresSettingStartTimeOnStart = true;
2015-07-13 14:26:11 -07:00
}
2015-08-15 14:58:52 -07:00
tracks = tracks || [];
2015-12-23 10:46:01 -07:00
var currentTrackIndex = -1;
for (var i = 0, length = tracks.length; i < length; i++) {
if (tracks[i].isDefault) {
currentTrackIndex = i;
break;
}
}
subtitleTrackIndexToSetOnPlaying = currentTrackIndex;
2015-11-20 13:46:00 -07:00
if (enableHlsPlayer(val)) {
2015-07-03 04:51:45 -07:00
2015-08-15 14:58:52 -07:00
setTracks(elem, tracks);
2015-07-06 00:06:09 -07:00
2015-11-20 13:46:00 -07:00
var hls = new Hls();
hls.loadSource(val);
2015-12-14 08:43:03 -07:00
hls.attachMedia(elem);
2015-11-20 13:46:00 -07:00
hls.on(Hls.Events.MANIFEST_PARSED, function () {
elem.play();
2015-07-03 04:51:45 -07:00
});
2015-11-20 13:46:00 -07:00
hlsPlayer = hls;
2015-07-03 09:49:49 -07:00
2015-07-03 04:51:45 -07:00
} else {
2015-07-13 14:26:11 -07:00
2015-07-03 04:51:45 -07:00
elem.src = val;
2015-08-24 13:37:34 -07:00
elem.autoplay = true;
2015-07-06 00:06:09 -07:00
2015-08-15 14:58:52 -07:00
setTracks(elem, tracks);
2015-07-06 00:06:09 -07:00
2015-12-23 10:46:01 -07:00
elem.addEventListener("loadedmetadata", onLoadedMetadata);
2015-08-24 13:37:34 -07:00
playNow = true;
2015-07-03 04:51:45 -07:00
}
2015-08-15 14:58:52 -07:00
self.setCurrentTrackElement(currentTrackIndex);
2015-07-03 04:51:45 -07:00
}
2015-07-03 08:59:12 -07:00
2015-07-03 04:51:45 -07:00
currentSrc = val;
2015-08-24 13:37:34 -07:00
if (playNow) {
elem.play();
}
2015-07-03 04:51:45 -07:00
};
2015-07-06 00:06:09 -07:00
function setTracks(elem, tracks) {
2015-07-03 04:51:45 -07:00
var html = tracks.map(function (t) {
var defaultAttribute = t.isDefault ? ' default' : '';
return '<track kind="subtitles" src="' + t.url + '" srclang="' + t.language + '"' + defaultAttribute + '></track>';
}).join('');
if (html) {
2015-07-06 00:06:09 -07:00
elem.innerHTML = html;
2015-06-07 20:16:42 -07:00
}
2015-07-06 00:06:09 -07:00
}
2015-06-07 20:16:42 -07:00
self.currentSrc = function () {
if (mediaElement) {
2015-07-03 04:51:45 -07:00
// We have to use this cached value because viblast will muck with the url
return currentSrc;
//return mediaElement.currentSrc;
2015-06-07 20:16:42 -07:00
}
};
self.paused = function () {
if (mediaElement) {
return mediaElement.paused;
}
return false;
};
2015-06-25 14:50:56 -07:00
self.cleanup = function (destroyRenderer) {
2015-06-07 20:16:42 -07:00
self.setCurrentSrc(null);
2015-07-09 20:00:03 -07:00
_currentTime = null;
2015-06-07 20:16:42 -07:00
var elem = mediaElement;
if (elem) {
2015-06-29 11:45:42 -07:00
if (elem.tagName == 'AUDIO') {
2015-12-23 10:46:01 -07:00
elem.removeEventListener('timeupdate', onTimeUpdate);
elem.removeEventListener('ended', onEnded);
elem.removeEventListener('volumechange', onVolumeChange);
elem.removeEventListener('playing', onOneAudioPlaying);
elem.removeEventListener('play', onPlay);
elem.removeEventListener('pause', onPause);
elem.removeEventListener('playing', onPlaying);
elem.removeEventListener('error', onError);
2015-06-29 11:45:42 -07:00
} else {
2015-12-23 10:46:01 -07:00
elem.removeEventListener('loadedmetadata', onLoadedMetadata);
elem.removeEventListener('playing', onOneVideoPlaying);
elem.removeEventListener('timeupdate', onTimeUpdate);
elem.removeEventListener('ended', onEnded);
elem.removeEventListener('volumechange', onVolumeChange);
elem.removeEventListener('play', onPlay);
elem.removeEventListener('pause', onPause);
elem.removeEventListener('playing', onPlaying);
elem.removeEventListener('click', onClick);
elem.removeEventListener('dblclick', onDblClick);
elem.removeEventListener('error', onError);
2015-06-29 11:45:42 -07:00
}
2015-06-07 20:16:42 -07:00
if (elem.tagName.toLowerCase() != 'audio') {
$(elem).remove();
}
}
2015-09-08 10:08:17 -07:00
showStatusBar();
2015-06-07 20:16:42 -07:00
};
2015-06-13 07:46:59 -07:00
self.supportsTextTracks = function () {
if (supportsTextTracks == null) {
supportsTextTracks = document.createElement('video').textTracks != null;
}
// For now, until ready
return supportsTextTracks;
};
self.setCurrentTrackElement = function (trackIndex) {
2015-12-23 10:46:01 -07:00
console.log('Setting new text track index to: ' + trackIndex);
2015-06-13 07:46:59 -07:00
var allTracks = mediaElement.textTracks; // get list of tracks
var modes = ['disabled', 'showing', 'hidden'];
for (var i = 0; i < allTracks.length; i++) {
var mode;
if (trackIndex == i) {
mode = 1; // show this track
} else {
mode = 0; // hide all other tracks
}
2015-12-23 10:46:01 -07:00
console.log('Setting track ' + i + ' mode to: ' + mode);
2015-06-13 07:46:59 -07:00
// Safari uses integers for the mode property
// http://www.jwplayer.com/html5/scripting/
var useNumericMode = false;
if (!isNaN(allTracks[i].mode)) {
useNumericMode = true;
}
if (useNumericMode) {
allTracks[i].mode = mode;
} else {
allTracks[i].mode = modes[mode];
}
}
};
self.updateTextStreamUrls = function (startPositionTicks) {
if (!self.supportsTextTracks()) {
return;
}
var allTracks = mediaElement.textTracks; // get list of tracks
for (var i = 0; i < allTracks.length; i++) {
var track = allTracks[i];
// This throws an error in IE, but is fine in chrome
// In IE it's not necessary anyway because changing the src seems to be enough
try {
while (track.cues.length) {
track.removeCue(track.cues[0]);
}
} catch (e) {
2015-12-23 10:46:01 -07:00
console.log('Error removing cue from textTrack');
2015-06-13 07:46:59 -07:00
}
}
$('track', mediaElement).each(function () {
2015-07-03 04:51:45 -07:00
this.src = replaceQueryString(this.src, 'startPositionTicks', startPositionTicks);
});
};
self.enableCustomVideoControls = function () {
2015-06-13 07:46:59 -07:00
2015-12-14 08:43:03 -07:00
if (AppInfo.isNativeApp && browserInfo.safari) {
2015-09-27 14:02:39 -07:00
if (navigator.userAgent.toLowerCase().indexOf('iphone') != -1) {
return true;
}
// Need to disable it in order to support picture in picture
return false;
2015-09-08 07:35:52 -07:00
}
2016-01-12 12:06:44 -07:00
return self.canAutoPlayVideo();
2015-07-03 04:51:45 -07:00
};
2015-06-13 07:46:59 -07:00
2015-07-03 04:51:45 -07:00
self.canAutoPlayVideo = function () {
2015-06-13 07:46:59 -07:00
2015-07-03 04:51:45 -07:00
if (AppInfo.isNativeApp) {
return true;
}
2015-12-14 08:43:03 -07:00
if (browserInfo.mobile) {
2015-07-03 04:51:45 -07:00
return false;
}
return true;
};
self.init = function () {
2015-12-14 08:43:03 -07:00
return new Promise(function (resolve, reject) {
2015-07-03 04:51:45 -07:00
2015-12-14 08:43:03 -07:00
if (options.type == 'video' && enableHlsPlayer()) {
2015-07-03 04:51:45 -07:00
2015-12-14 08:43:03 -07:00
requireHlsPlayer(resolve);
2015-07-03 04:51:45 -07:00
2015-12-14 08:43:03 -07:00
} else {
resolve();
}
});
2015-06-13 07:46:59 -07:00
};
2015-07-03 09:49:49 -07:00
if (options.type == 'audio') {
2015-06-07 20:16:42 -07:00
mediaElement = createAudioElement();
}
else {
mediaElement = createVideoElement();
}
}
2015-06-10 06:37:07 -07:00
if (!window.AudioRenderer) {
2015-07-03 09:49:49 -07:00
window.AudioRenderer = function (options) {
options = options || {};
options.type = 'audio';
return new htmlMediaRenderer(options);
};
2015-06-10 06:37:07 -07:00
}
if (!window.VideoRenderer) {
2015-07-03 09:49:49 -07:00
window.VideoRenderer = function (options) {
options = options || {};
options.type = 'video';
return new htmlMediaRenderer(options);
};
2015-06-10 06:37:07 -07:00
}
2015-06-07 20:16:42 -07:00
})();