define(['appSettings', 'datetime', 'mediaInfo', 'scrollStyles', 'paper-icon-button-light'], function (appSettings, datetime, mediaInfo) { function createVideoPlayer(self) { var initialVolume; var idleState = true; var muteButton = null; var unmuteButton = null; var volumeSlider = null; var positionSlider; var currentTimeElement; self.currentSubtitleStreamIndex = null; self.getCurrentSubtitleStream = function () { return self.getSubtitleStream(self.currentSubtitleStreamIndex); }; self.getSubtitleStream = function (index) { return self.currentMediaSource.MediaStreams.filter(function (s) { return s.Type == 'Subtitle' && s.Index == index; })[0]; }; self.toggleFullscreen = function () { if (self.isFullScreen()) { self.exitFullScreen(); } else { requestFullScreen(document.body); } }; function setClass(elems, method, className) { for (var i = 0, length = elems.length; i < length; i++) { elems[i].classList[method](className); } } self.resetEnhancements = function () { if (!initComplete) { return; } if (self.isFullScreen()) { self.exitFullScreen(); } var videoPlayerElement = document.querySelector('#videoPlayer'); fadeOut(videoPlayerElement); videoPlayerElement.classList.remove('fullscreenVideo'); videoPlayerElement.classList.remove('idlePlayer'); setClass(document.querySelectorAll('.hiddenOnIdle'), 'remove', 'inactive'); var video = videoPlayerElement.querySelector('video'); if (video) { video.parentNode.removeChild(video); } document.querySelector('.mediaButton.infoButton').classList.remove('active'); document.querySelector('.videoControls .nowPlayingInfo').classList.add('hide'); document.querySelector('.videoControls').classList.add('hiddenOnIdle'); }; self.exitFullScreen = function () { if (document.exitFullscreen) { document.exitFullscreen(); } else if (document.mozCancelFullScreen) { document.mozCancelFullScreen(); } else if (document.webkitExitFullscreen) { document.webkitExitFullscreen(); } else if (document.msExitFullscreen) { document.msExitFullscreen(); } document.querySelector('#videoPlayer').classList.remove('fullscreenVideo'); }; self.isFullScreen = function () { return document.fullscreen || document.mozFullScreen || document.webkitIsFullScreen || document.msFullscreenElement ? true : false; }; self.showSubtitleMenu = function () { var streams = self.currentMediaSource.MediaStreams.filter(function (currentStream) { return currentStream.Type == "Subtitle"; }); var currentIndex = self.currentSubtitleStreamIndex; if (currentIndex == null) { currentIndex = -1; } streams.unshift({ Index: -1, Language: Globalize.translate('ButtonOff') }); var menuItems = streams.map(function (stream) { var attributes = []; attributes.push(stream.Language || Globalize.translate('LabelUnknownLanguage')); if (stream.Codec) { attributes.push(stream.Codec); } var name = attributes.join(' - '); if (stream.IsDefault) { name += ' (D)'; } if (stream.IsForced) { name += ' (F)'; } if (stream.External) { name += ' (EXT)'; } var opt = { name: stream.DisplayTitle || name, id: stream.Index }; if (stream.Index == currentIndex) { opt.selected = true; } return opt; }); require(['actionsheet'], function (actionsheet) { actionsheet.show({ items: menuItems, // history.back() will cause the video player to stop enableHistory: false, positionTo: document.querySelector('.videoSubtitleButton'), callback: function (id) { var index = parseInt(id); if (index != currentIndex) { self.onSubtitleOptionSelected(index); } } }); }); }; self.showQualityFlyout = function () { require(['qualityoptions', 'actionsheet'], function (qualityoptions, actionsheet) { var currentSrc = self.getCurrentSrc(self.currentMediaRenderer).toLowerCase(); var isStatic = currentSrc.indexOf('static=true') != -1; var videoStream = self.currentMediaSource.MediaStreams.filter(function (stream) { return stream.Type == "Video"; })[0]; var videoWidth = videoStream ? videoStream.Width : null; var options = qualityoptions.getVideoQualityOptions(appSettings.maxStreamingBitrate(), videoWidth); if (isStatic) { options[0].name = "Direct"; } var menuItems = options.map(function (o) { var opt = { name: o.name, id: o.bitrate }; if (o.selected) { opt.selected = true; } return opt; }); var selectedId = options.filter(function (o) { return o.selected; }); selectedId = selectedId.length ? selectedId[0].bitrate : null; actionsheet.show({ items: menuItems, // history.back() will cause the video player to stop enableHistory: false, positionTo: document.querySelector('.videoQualityButton'), callback: function (id) { var bitrate = parseInt(id); if (bitrate != selectedId) { self.onQualityOptionSelected(bitrate); } } }); }); }; self.showAudioTracksFlyout = function () { var options = self.currentMediaSource.MediaStreams.filter(function (currentStream) { return currentStream.Type == "Audio"; }); var currentIndex = getParameterByName('AudioStreamIndex', self.getCurrentSrc(self.currentMediaRenderer)); var menuItems = options.map(function (stream) { var attributes = []; attributes.push(stream.Language || Globalize.translate('LabelUnknownLanguage')); if (stream.Codec) { attributes.push(stream.Codec); } if (stream.Profile) { attributes.push(stream.Profile); } if (stream.BitRate) { attributes.push((Math.floor(stream.BitRate / 1000)) + ' kbps'); } if (stream.Channels) { attributes.push(stream.Channels + ' ch'); } var name = attributes.join(' - '); if (stream.IsDefault) { name += ' (D)'; } var opt = { name: stream.DisplayTitle || name, id: stream.Index }; if (stream.Index == currentIndex) { opt.selected = true; } return opt; }); require(['actionsheet'], function (actionsheet) { actionsheet.show({ items: menuItems, // history.back() will cause the video player to stop enableHistory: false, positionTo: document.querySelector('.videoAudioButton'), callback: function (id) { var index = parseInt(id); if (index != currentIndex) { self.onAudioOptionSelected(index); } } }); }); }; self.setAudioStreamIndex = function (index) { self.changeStream(self.getCurrentTicks(), { AudioStreamIndex: index }); }; self.setSubtitleStreamIndex = function (index) { if (!self.currentMediaRenderer.supportsTextTracks()) { self.changeStream(self.getCurrentTicks(), { SubtitleStreamIndex: index }); self.currentSubtitleStreamIndex = index; return; } var currentStream = self.getCurrentSubtitleStream(); var newStream = self.getSubtitleStream(index); if (!currentStream && !newStream) return; var selectedTrackElementIndex = -1; if (currentStream && !newStream) { if (currentStream.DeliveryMethod != 'External') { // Need to change the transcoded stream to remove subs self.changeStream(self.getCurrentTicks(), { SubtitleStreamIndex: -1 }); } } else if (!currentStream && newStream) { if (newStream.DeliveryMethod == 'External') { selectedTrackElementIndex = index; } else { // Need to change the transcoded stream to add subs self.changeStream(self.getCurrentTicks(), { SubtitleStreamIndex: index }); } } else if (currentStream && newStream) { if (newStream.DeliveryMethod == 'External') { selectedTrackElementIndex = index; if (currentStream.DeliveryMethod != 'External') { self.changeStream(self.getCurrentTicks(), { SubtitleStreamIndex: -1 }); } } else { // Need to change the transcoded stream to add subs self.changeStream(self.getCurrentTicks(), { SubtitleStreamIndex: index }); } } self.setCurrentTrackElement(selectedTrackElementIndex); self.currentSubtitleStreamIndex = index; }; self.setCurrentTrackElement = function (index) { self.currentMediaRenderer.setCurrentTrackElement(index); }; self.updateTextStreamUrls = function (startPositionTicks) { self.currentMediaRenderer.updateTextStreamUrls(startPositionTicks); }; self.updateNowPlayingInfo = function (item) { if (!item) { throw new Error('item cannot be null'); } var mediaControls = document.querySelector("#videoPlayer"); var state = self.getPlayerStateInternal(self.currentMediaRenderer, item.CurrentProgram || item, self.currentMediaSource); var url = ""; var imageWidth = 400; var imageHeight = 300; if (state.NowPlayingItem.PrimaryImageTag) { url = ApiClient.getScaledImageUrl(state.NowPlayingItem.PrimaryImageItemId, { type: "Primary", width: imageWidth, tag: state.NowPlayingItem.PrimaryImageTag }); } else if (state.NowPlayingItem.PrimaryImageTag) { url = ApiClient.getScaledImageUrl(state.NowPlayingItem.PrimaryImageItemId, { type: "Primary", width: imageWidth, tag: state.NowPlayingItem.PrimaryImageTag }); } else if (state.NowPlayingItem.BackdropImageTag) { url = ApiClient.getScaledImageUrl(state.NowPlayingItem.BackdropItemId, { type: "Backdrop", height: imageHeight, tag: state.NowPlayingItem.BackdropImageTag, index: 0 }); } else if (state.NowPlayingItem.ThumbImageTag) { url = ApiClient.getScaledImageUrl(state.NowPlayingItem.ThumbImageItemId, { type: "Thumb", height: imageHeight, tag: state.NowPlayingItem.ThumbImageTag }); } if (url) { mediaControls.querySelector('.nowPlayingImage').innerHTML = ''; } else { mediaControls.querySelector('.nowPlayingImage').innerHTML = ''; } if (state.NowPlayingItem.LogoItemId) { url = ApiClient.getScaledImageUrl(state.NowPlayingItem.LogoItemId, { type: "Logo", height: 42, tag: state.NowPlayingItem.LogoImageTag }); mediaControls.querySelector('.videoTopControlsLogo').innerHTML = ''; } else { mediaControls.querySelector('.videoTopControlsLogo').innerHTML = ''; } var elem = mediaControls.querySelector('.nowPlayingTabs'); elem.innerHTML = getNowPlayingTabsHtml(item.CurrentProgram || item); ImageLoader.lazyChildren(elem); function onTabButtonClick() { if (!this.classList.contains('selectedNowPlayingTabButton')) { var selectedNowPlayingTabButton = document.querySelector('.selectedNowPlayingTabButton'); if (selectedNowPlayingTabButton) { selectedNowPlayingTabButton.classList.remove('selectedNowPlayingTabButton'); } this.classList.add('selectedNowPlayingTabButton'); setClass(document.querySelectorAll('.nowPlayingTab'), 'add', 'hide'); document.querySelector('.' + this.getAttribute('data-tab')).classList.remove('hide'); } } var nowPlayingTabButtons = elem.querySelectorAll('.nowPlayingTabButton'); for (var i = 0, length = nowPlayingTabButtons.length; i < length; i++) { nowPlayingTabButtons[i].addEventListener('click', onTabButtonClick); } elem.addEventListener('click', function (e) { var chapterCard = parentWithClass(e.target, 'chapterCard'); if (chapterCard) { self.seek(parseInt(chapterCard.getAttribute('data-position'))); } }); }; function parentWithClass(elem, className) { while (!elem.classList || !elem.classList.contains(className)) { elem = elem.parentNode; if (!elem) { return null; } } return elem; } function getNowPlayingTabsHtml(item) { var html = ''; html += '
'; html += ' '; if (item.Chapters && item.Chapters.length) { html += ' '; } if (item.People && item.People.length) { html += ' '; } return html; } function getSeekableDuration() { if (self.currentMediaSource && self.currentMediaSource.RunTimeTicks) { return self.currentMediaSource.RunTimeTicks; } if (self.currentMediaRenderer) { return self.getCurrentTicks(self.currentMediaRenderer); } return null; } function onPositionSliderChange() { var newPercent = parseInt(this.value); var newPositionTicks = (newPercent / 100) * getSeekableDuration(); self.changeStream(Math.floor(newPositionTicks)); } self.onAudioOptionSelected = function (index) { self.setAudioStreamIndex(index); }; self.onSubtitleOptionSelected = function (index) { self.setSubtitleStreamIndex(index); }; self.onQualityOptionSelected = function (bitrate) { appSettings.maxStreamingBitrate(bitrate); appSettings.enableAutomaticBitrateDetection(false); self.changeStream(self.getCurrentTicks(), { Bitrate: bitrate }); }; self.toggleInfo = function () { var button = document.querySelector('.mediaButton.infoButton'); var nowPlayingInfo = document.querySelector('.videoControls .nowPlayingInfo'); if (button.classList.contains('active')) { button.classList.remove('active'); document.querySelector('.videoControls').classList.add('hiddenOnIdle'); fadeOutDown(nowPlayingInfo); } else { button.classList.add('active'); document.querySelector('.videoControls').classList.remove('hiddenOnIdle'); nowPlayingInfo.classList.remove('hide'); fadeInUp(nowPlayingInfo); } }; self.toggleGuide = function () { var button = document.querySelector('.mediaButton.guideButton'); var nowPlayingInfo = document.querySelector('.videoControls .guide'); if (button.classList.contains('active')) { button.classList.remove('active'); document.querySelector('.videoControls').classList.add('hiddenOnIdle'); fadeOutDown(nowPlayingInfo); } else { button.classList.add('active'); document.querySelector('.videoControls').classList.remove('hiddenOnIdle'); nowPlayingInfo.classList.remove('hide'); fadeInUp(nowPlayingInfo); if (!self.guideInstance) { require(['tvguide'], function (tvguide) { self.guideInstance = new tvguide({ element: nowPlayingInfo, enablePaging: false }); }); } } }; function fadeInUp(elem) { var keyframes = [ { transform: 'translate3d(0, 100%, 0)', offset: 0 }, { transform: 'translate3d(0, 0, 0)', offset: 1 }]; var timing = { duration: 300, iterations: 1 }; if (elem.animate) { elem.animate(keyframes, timing); } } function fadeOutDown(elem) { var keyframes = [{ transform: 'translate3d(0, 0, 0)', offset: 0 }, { transform: 'translate3d(0, 100%, 0)', offset: 1 }]; var timing = { duration: 300, iterations: 1 }; var onFinish = function () { elem.classList.add('hide'); }; if (elem.animate) { elem.animate(keyframes, timing).onfinish = onFinish; } else { onFinish(); } } function ensureVideoPlayerElements() { var html = ''; html += ''; errorMsg += Globalize.translate('MessageEnsureOpenTuner'); errorMsg += '
'; } Dashboard.alert({ title: Globalize.translate('HeaderVideoError'), message: errorMsg }); var mediaRenderer = self.currentMediaRenderer; if (mediaRenderer) { self.onPlaybackStopped.call(mediaRenderer); } self.nextTrack(); } function onClick() { if (!browserInfo.mobile) { if (this.paused()) { self.unpause(); } else { self.pause(); } } } function onDoubleClick() { if (!browserInfo.mobile) { self.toggleFullscreen(); } } self.updatePlaylistUi = function () { if (!initComplete) { return; } var index = self.currentPlaylistIndex(null); var length = self.playlist.length; var requiresNativeControls = false; if (self.currentMediaRenderer && self.currentMediaRenderer.enableCustomVideoControls) { requiresNativeControls = self.currentMediaRenderer.enableCustomVideoControls(); } if (length < 2) { document.querySelector('.videoTrackControl').classList.add('hide'); return; } var controls = requiresNativeControls ? '.videoAdvancedControls' : '.videoControls'; controls = document.querySelector(controls); var previousTrackButton = controls.getElementsByClassName('previousTrackButton')[0]; var nextTrackButton = controls.getElementsByClassName('nextTrackButton')[0]; if (index === 0) { previousTrackButton.setAttribute('disabled', 'disabled'); } else { previousTrackButton.removeAttribute('disabled'); } if ((index + 1) >= length) { nextTrackButton.setAttribute('disabled', 'disabled'); } else { nextTrackButton.removeAttribute('disabled'); } previousTrackButton.classList.remove('hide'); nextTrackButton.classList.remove('hide'); }; } createVideoPlayer(MediaPlayer); });