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

3 lines
63 KiB
JavaScript
Raw Normal View History

2017-06-21 22:51:35 -07:00
define(["events","datetime","appSettings","itemHelper","pluginManager","playQueueManager","userSettings","globalize","connectionManager","loading","serverNotifications","apphost","fullscreenManager","layoutManager"],function(events,datetime,appSettings,itemHelper,pluginManager,PlayQueueManager,userSettings,globalize,connectionManager,loading,serverNotifications,apphost,fullscreenManager,layoutManager){"use strict";function enableLocalPlaylistManagement(player){return!player.getPlaylist&&!!player.isLocalPlayer}function bindToFullscreenChange(player){events.on(fullscreenManager,"fullscreenchange",function(){events.trigger(player,"fullscreenchange")})}function triggerPlayerChange(playbackManagerInstance,newPlayer,newTarget,previousPlayer,previousTargetInfo){(newPlayer||previousPlayer)&&(newTarget&&previousTargetInfo&&newTarget.id===previousTargetInfo.id||events.trigger(playbackManagerInstance,"playerchange",[newPlayer,newTarget,previousPlayer]))}function reportPlayback(state,serverId,method,progressEventName){if(serverId){var info=Object.assign({},state.PlayState);info.ItemId=state.NowPlayingItem.Id,progressEventName&&(info.EventName=progressEventName);var apiClient=connectionManager.getApiClient(serverId);apiClient[method](info)}}function normalizeName(t){return t.toLowerCase().replace(" ","")}function getItemsForPlayback(serverId,query){var apiClient=connectionManager.getApiClient(serverId);if(query.Ids&&1===query.Ids.split(",").length){var itemId=query.Ids.split(",");return apiClient.getItem(apiClient.getCurrentUserId(),itemId).then(function(item){return{Items:[item],TotalRecordCount:1}})}return query.Limit=query.Limit||200,query.Fields="MediaSources,Chapters",query.ExcludeLocationTypes="Virtual",query.EnableTotalRecordCount=!1,apiClient.getItems(apiClient.getCurrentUserId(),query)}function createStreamInfoFromUrlItem(item){return{url:item.Url||item.Path,playMethod:"DirectPlay",item:item,textTracks:[],mediaType:item.MediaType}}function backdropImageUrl(apiClient,item,options){return options=options||{},options.type=options.type||"Backdrop",options.maxWidth||options.width||options.maxHeight||options.height||(options.quality=100),item.BackdropImageTags&&item.BackdropImageTags.length?(options.tag=item.BackdropImageTags[0],apiClient.getScaledImageUrl(item.Id,options)):item.ParentBackdropImageTags&&item.ParentBackdropImageTags.length?(options.tag=item.ParentBackdropImageTags[0],apiClient.getScaledImageUrl(item.ParentBackdropItemId,options)):null}function getMimeType(type,container){if(container=(container||"").toLowerCase(),"audio"===type){if("opus"===container)return"audio/ogg";if("webma"===container)return"audio/webm";if("m4a"===container)return"audio/mp4"}else if("video"===type){if("mkv"===container)return"video/x-matroska";if("m4v"===container)return"video/mp4";if("mov"===container)return"video/quicktime";if("mpg"===container)return"video/mpeg";if("flv"===container)return"video/x-flv"}return type+"/"+container}function getParam(name,url){name=name.replace(/[\[]/,"\\[").replace(/[\]]/,"\\]");var regexS="[\\?&]"+name+"=([^&#]*)",regex=new RegExp(regexS,"i"),results=regex.exec(url);return null==results?"":decodeURIComponent(results[1].replace(/\+/g," "))}function isAutomaticPlayer(player){return!!player.isLocalPlayer}function getAutomaticPlayers(instance){var player=instance._currentPlayer;return player&&!isAutomaticPlayer(player)?[player]:instance.getPlayers().filter(isAutomaticPlayer)}function isServerItem(item){return!!item.Id}function enableIntros(item){return"Video"===item.MediaType&&("TvChannel"!==item.Type&&("InProgress"!==item.Status&&isServerItem(item)))}function getIntros(firstItem,apiClient,options){return!options.startPositionTicks&&options.fullscreen!==!1&&enableIntros(firstItem)&&userSettings.enableCinemaMode()?apiClient.getIntros(firstItem.Id):Promise.resolve({Items:[]})}function getAudioStreamUrl(item,transcodingProfile,directPlayContainers,maxBitrate,apiClient,startPosition){var url="Audio/"+item.Id+"/universal";return startingPlaySession++,apiClient.getUrl(url,{UserId:apiClient.getCurrentUserId(),DeviceId:
locks[prop]=resourceLock,resourceLock.acquire()},function(){})})}function releaseResourceLocks(player){if(!player)throw new Error("player cannot be null");if(player.isLocalPlayer&&!player.hasResourceLocks){var playerData=getPlayerData(player),locks=playerData.resourceLocks||{};locks.wakeLock&&locks.wakeLock.release(),locks.networkLock&&locks.networkLock.release(),locks.screenLock&&locks.screenLock.release()}}function enablePlaybackRetryWithTranscoding(streamInfo,errorType){return!!streamInfo&&(!("mediadecodeerror"!==errorType&&"medianotsupported"!==errorType||"Transcode"===streamInfo.playMethod||!streamInfo.mediaSource.SupportsTranscoding)||!("network"!==errorType||"DirectPlay"!==streamInfo.playMethod||!streamInfo.mediaSource.IsRemote||!streamInfo.mediaSource.SupportsTranscoding))}function onPlaybackError(e,error){var player=this;error=error||{};var errorType=error.type;console.log("playbackmanager playback error type: "+(errorType||""));var streamInfo=getPlayerData(player).streamInfo;if(enablePlaybackRetryWithTranscoding(streamInfo,errorType)){var startTime=getCurrentTicks(player)||streamInfo.playerStartPositionTicks;return void changeStream(player,startTime,{EnableDirectPlay:!1,EnableDirectStream:!1,AllowVideoStreamCopy:!1,AllowAudioStreamCopy:!1},!0)}onPlaybackStopped.call(player,e)}function onPlaybackStopped(e){var player=this;getPlayerData(player).isChangingStream||self.getPlayerState(player).then(function(state){var streamInfo=getPlayerData(player).streamInfo,nextItem=self._playNextAfterEnded?self._playQueueManager.getNextItemInfo():null,nextMediaType=nextItem?nextItem.item.MediaType:null,playbackStopInfo={player:player,state:state,nextItem:nextItem?nextItem.item:null,nextMediaType:nextMediaType};state.NextMediaType=nextMediaType,isServerItem(streamInfo.item)&&(player.supportsProgress===!1&&state.PlayState&&!state.PlayState.PositionTicks&&(state.PlayState.PositionTicks=streamInfo.item.RunTimeTicks),streamInfo.ended=!0,reportPlayback(state,streamInfo.item.ServerId,"reportPlaybackStopped")),state.NextItem=playbackStopInfo.nextItem,nextItem||self._playQueueManager.reset(),events.trigger(player,"playbackstop",[state]),events.trigger(self,"playbackstop",[playbackStopInfo]);var newPlayer=nextItem?getPlayer(nextItem.item,currentPlayOptions):null;newPlayer!==player&&(destroyPlayer(player),removeCurrentPlayer(player)),nextItem&&self.nextTrack()})}function onPlaybackChanging(activePlayer,newPlayer,newItem){return self.getPlayerState(activePlayer).then(function(state){var promise,serverId=self.currentItem(activePlayer).ServerId;return unbindStopped(activePlayer),promise=activePlayer===newPlayer?activePlayer.stop(!1):activePlayer.stop(!0),promise.then(function(){bindStopped(activePlayer),enableLocalPlaylistManagement(activePlayer)&&reportPlayback(state,serverId,"reportPlaybackStopped"),events.trigger(self,"playbackstop",[{player:activePlayer,state:state,nextItem:newItem,nextMediaType:newItem.MediaType}])})})}function bindStopped(player){enableLocalPlaylistManagement(player)&&(events.off(player,"stopped",onPlaybackStopped),events.on(player,"stopped",onPlaybackStopped))}function onPlaybackTimeUpdate(e){var player=this;sendProgressUpdate(player,"timeupdate")}function onPlaybackPause(e){var player=this;sendProgressUpdate(player,"pause")}function onPlaybackUnpause(e){var player=this;sendProgressUpdate(player,"unpause")}function onPlaybackVolumeChange(e){var player=this;sendProgressUpdate(player,"volumechange")}function onRepeatModeChange(e){var player=this;sendProgressUpdate(player,"repeatmodechange")}function unbindStopped(player){events.off(player,"stopped",onPlaybackStopped)}function initLegacyVolumeMethods(player){player.getVolume=function(){return player.volume()},player.setVolume=function(val){return player.volume(val)}}function initMediaPlayer(player){players.push(player),players.sort(function(a,b){return(a.priority||0)-(b.priority||0)}),player.isLocalPlayer!==!1&&(player.isLocalPlayer=!0),player.currentState={},player.getVolume&&player.setVolume||initLegacyVolumeMethods(player),enableLocalPlaylistManagement(player)?(events
break;case"SetBrightness":this.setBrightness(cmd.Arguments.Brightness,player);break;case"SetAudioStreamIndex":this.setAudioStreamIndex(parseInt(cmd.Arguments.Index),player);break;case"SetSubtitleStreamIndex":this.setSubtitleStreamIndex(parseInt(cmd.Arguments.Index),player);break;case"SetMaxStreamingBitrate":break;case"ToggleFullscreen":this.toggleFullscreen(player);break;default:player.sendCommand&&player.sendCommand(cmd)}},new PlaybackManager});