jellyfin-web/dashboard-ui/bower_components/emby-webcomponents/playback/playbackmanager.js

2 lines
51 KiB
JavaScript
Raw Normal View History

2017-02-01 13:55:56 -07:00
define(["events","datetime","appSettings","pluginManager","userSettings","globalize","connectionManager","loading","serverNotifications","apphost","fullscreenManager","layoutManager"],function(events,datetime,appSettings,pluginManager,userSettings,globalize,connectionManager,loading,serverNotifications,apphost,fullscreenManager,layoutManager){"use strict";function enableLocalPlaylistManagement(player){return!!player.isLocalPlayer}function bindToFullscreenChange(player){events.on(fullscreenManager,"fullscreenchange",function(){events.trigger(player,"fullscreenchange")})}function PlaybackManager(){function triggerPlayerChange(newPlayer,newTarget,previousPlayer,previousTargetInfo){(newPlayer||previousPlayer)&&(newTarget&&previousTargetInfo&&newTarget.id===previousTargetInfo.id||events.trigger(self,"playerchange",[newPlayer,newTarget,previousPlayer]))}function displayPlayerInLocalGroup(player){return player.isLocalPlayer}function getSupportedCommands(player){if(player.isLocalPlayer){var list=["GoHome","GoToSettings","VolumeUp","VolumeDown","Mute","Unmute","ToggleMute","SetVolume","SetAudioStreamIndex","SetSubtitleStreamIndex","SetMaxStreamingBitrate","DisplayContent","GoToSearch","DisplayMessage","SetRepeatMode"];return apphost.supports("fullscreenchange")&&!layoutManager.tv&&list.push("ToggleFullscreen"),player.supports&&player.supports("pictureinpicture")&&list.push("PictureInPicture"),list}throw new Error("player must define supported commands")}function createTarget(player){return{name:player.name,id:player.id,playerName:player.name,playableMediaTypes:["Audio","Video","Game"].map(player.canPlayMediaType),isLocalPlayer:player.isLocalPlayer,supportedCommands:getSupportedCommands(player)}}function getPlayerTargets(player){return player.getTargets?player.getTargets():Promise.resolve([createTarget(player)])}function getCurrentSubtitleStream(player){var index=getPlayerData(player).subtitleStreamIndex;return null==index||index===-1?null:getSubtitleStream(player,index)}function getSubtitleStream(player,index){return self.currentMediaSource(player).MediaStreams.filter(function(s){return"Subtitle"===s.Type&&s.Index===index})[0]}function setCurrentPlayerInternal(player,targetInfo){var previousPlayer=currentPlayer,previousTargetInfo=currentTargetInfo;if(player&&!targetInfo&&player.isLocalPlayer&&(targetInfo=createTarget(player)),player&&!targetInfo)throw new Error("targetInfo cannot be null");currentPairingId=null,currentPlayer=player,currentTargetInfo=targetInfo,targetInfo&&console.log("Active player: "+JSON.stringify(targetInfo)),player&&player.isLocalPlayer&&(lastLocalPlayer=player),previousPlayer&&self.endPlayerUpdates(previousPlayer),player&&self.beginPlayerUpdates(player),triggerPlayerChange(player,targetInfo,previousPlayer,previousTargetInfo)}function getAutomaticPlayers(){var player=currentPlayer;return player&&!enableLocalPlaylistManagement(player)?[player]:self.getPlayers().filter(enableLocalPlaylistManagement)}function canPlayerSeek(player){var currentSrc=(player.currentSrc()||"").toLowerCase();return currentSrc.indexOf(".m3u8")!==-1||player.duration()}function changeStream(player,ticks,params){if(canPlayerSeek(player)&&null==params)return void player.currentTime(parseInt(ticks/1e4));params=params||{};var liveStreamId=getPlayerData(player).streamInfo.liveStreamId,playSessionId=getPlayerData(player).streamInfo.playSessionId,playerData=getPlayerData(player),currentItem=playerData.streamInfo.item;player.getDeviceProfile(currentItem).then(function(deviceProfile){var audioStreamIndex=null==params.AudioStreamIndex?getPlayerData(player).audioStreamIndex:params.AudioStreamIndex,subtitleStreamIndex=null==params.SubtitleStreamIndex?getPlayerData(player).subtitleStreamIndex:params.SubtitleStreamIndex,currentMediaSource=playerData.streamInfo.mediaSource,apiClient=connectionManager.getApiClient(currentItem.ServerId);ticks&&(ticks=parseInt(ticks));var maxBitrate=params.MaxStreamingBitrate||self.getMaxStreamingBitrate(player);getPlaybackInfo(apiClient,currentItem.Id,deviceProfile,maxBitrate,ticks,currentMediaSource,audioStreamIndex,sub
name:globalize.translate("ButtonYes"),id:"yes"}),menuItems.push({name:globalize.translate("ButtonNo"),id:"no"}),dialog({buttons:menuItems,text:globalize.translate("ConfirmEndPlayerSession")}).then(function(id){switch(id){case"yes":self.getCurrentPlayer().endSession(),self.setDefaultPlayerActive();break;case"no":self.setDefaultPlayerActive()}})}):self.setDefaultPlayerActive())},self.getTargets=function(){var promises=players.filter(function(p){return!displayPlayerInLocalGroup(p)}).map(getPlayerTargets);return Promise.all(promises).then(function(responses){var targets=[];targets.push({name:globalize.translate("sharedcomponents#HeaderMyDevice"),id:"localplayer",playerName:"localplayer",playableMediaTypes:["Audio","Video","Game"],isLocalPlayer:!0,supportedCommands:getSupportedCommands({isLocalPlayer:!0})});for(var i=0;i<responses.length;i++)for(var subTargets=responses[i],j=0;j<subTargets.length;j++)targets.push(subTargets[j]);return targets=targets.sort(function(a,b){var aVal=a.isLocalPlayer?0:1,bVal=b.isLocalPlayer?0:1;return aVal=aVal.toString()+a.name,bVal=bVal.toString()+b.name,aVal.localeCompare(bVal)})})},self.displayContent=function(options,player){player=player||currentPlayer,player&&player.displayContent&&player.displayContent(options)},self.sendCommand=function(cmd,player){switch(console.log("MediaController received command: "+cmd.Name),cmd.Name){case"SetRepeatMode":self.setRepeatMode(cmd.Arguments.RepeatMode,player);break;case"VolumeUp":self.volumeUp(player);break;case"VolumeDown":self.volumeDown(player);break;case"Mute":self.setMute(!0,player);break;case"Unmute":self.setMute(!1,player);break;case"ToggleMute":self.toggleMute(player);break;case"SetVolume":self.setVolume(cmd.Arguments.Volume,player);break;case"SetAudioStreamIndex":self.setAudioStreamIndex(parseInt(cmd.Arguments.Index),player);break;case"SetSubtitleStreamIndex":self.setSubtitleStreamIndex(parseInt(cmd.Arguments.Index),player);break;case"SetMaxStreamingBitrate":break;case"ToggleFullscreen":self.toggleFullscreen(player);break;default:player.sendCommand&&player.sendCommand(cmd)}},self.audioTracks=function(player){var mediaSource=self.currentMediaSource(player),mediaStreams=(mediaSource||{}).MediaStreams||[];return mediaStreams.filter(function(s){return"Audio"===s.Type})},self.subtitleTracks=function(player){var mediaSource=self.currentMediaSource(player),mediaStreams=(mediaSource||{}).MediaStreams||[];return mediaStreams.filter(function(s){return"Subtitle"===s.Type})},self.getPlaylist=function(player){return player=player||currentPlayer,player&&!enableLocalPlaylistManagement(player)?player.getPlaylist():Promise.resolve(playlist.slice(0))},self.getCurrentPlayer=function(){return currentPlayer},self.isPlaying=function(player){return player=player||currentPlayer,player&&!enableLocalPlaylistManagement(player)?player.isPlaying():null!=player&&null!=player.currentSrc()},self.isPlayingLocally=function(mediaTypes,player){if(player=player||currentPlayer,!player||!player.isLocalPlayer)return!1;var playerData=getPlayerData(player)||{};return mediaTypes.indexOf((playerData.streamInfo||{}).mediaType||"")!==-1},self.isPlayingVideo=function(player){if(player=player||currentPlayer,player&&!enableLocalPlaylistManagement(player))return player.isPlayingVideo();if(self.isPlaying()){var playerData=getPlayerData(player);return"Video"===playerData.streamInfo.mediaType}return!1},self.isPlayingAudio=function(player){if(player=player||currentPlayer,player&&!enableLocalPlaylistManagement(player))return player.isPlayingAudio();if(self.isPlaying()){var playerData=getPlayerData(player);return"Audio"===playerData.streamInfo.mediaType}return!1},self.getPlayers=function(){return players},self.canPlay=function(item){var itemType=item.Type,locationType=item.LocationType;return"MusicGenre"===itemType||"Season"===itemType||"Series"===itemType||"BoxSet"===itemType||"MusicAlbum"===itemType||"MusicArtist"===itemType||"Playlist"===itemType||("Virtual"!==locationType||"Program"===itemType)&&(("Program"!==itemType||!((new Date).getTime()>datetime.parseISO8601Date(item.EndDate).getTime()||(new