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";functionenableLocalPlaylistManagement(player){return!!player.isLocalPlayer}functionbindToFullscreenChange(player){events.on(fullscreenManager,"fullscreenchange",function(){events.trigger(player,"fullscreenchange")})}functionPlaybackManager(){functiontriggerPlayerChange(newPlayer,newTarget,previousPlayer,previousTargetInfo){(newPlayer||previousPlayer)&&(newTarget&&previousTargetInfo&&newTarget.id===previousTargetInfo.id||events.trigger(self,"playerchange",[newPlayer,newTarget,previousPlayer]))}functiondisplayPlayerInLocalGroup(player){returnplayer.isLocalPlayer}functiongetSupportedCommands(player){if(player.isLocalPlayer){varlist=["GoHome","GoToSettings","VolumeUp","VolumeDown","Mute","Unmute","ToggleMute","SetVolume","SetAudioStreamIndex","SetSubtitleStreamIndex","SetMaxStreamingBitrate","DisplayContent","GoToSearch","DisplayMessage","SetRepeatMode"];returnapphost.supports("fullscreenchange")&&!layoutManager.tv&&list.push("ToggleFullscreen"),player.supports&&(player.supports("PictureInPicture")&&list.push("PictureInPicture"),player.supports("SetBrightness")&&list.push("SetBrightness")),list}thrownewError("player must define supported commands")}functioncreateTarget(player){return{name:player.name,id:player.id,playerName:player.name,playableMediaTypes:["Audio","Video","Game"].map(player.canPlayMediaType),isLocalPlayer:player.isLocalPlayer,supportedCommands:getSupportedCommands(player)}}functiongetPlayerTargets(player){returnplayer.getTargets?player.getTargets():Promise.resolve([createTarget(player)])}functiongetCurrentSubtitleStream(player){varindex=getPlayerData(player).subtitleStreamIndex;returnnull==index||index===-1?null:getSubtitleStream(player,index)}functiongetSubtitleStream(player,index){returnself.currentMediaSource(player).MediaStreams.filter(function(s){return"Subtitle"===s.Type&&s.Index===index})[0]}functionsetCurrentPlayerInternal(player,targetInfo){varpreviousPlayer=currentPlayer,previousTargetInfo=currentTargetInfo;if(player&&!targetInfo&&player.isLocalPlayer&&(targetInfo=createTarget(player)),player&&!targetInfo)thrownewError("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)}functiongetAutomaticPlayers(){varplayer=currentPlayer;returnplayer&&!enableLocalPlaylistManagement(player)?[player]:self.getPlayers().filter(enableLocalPlaylistManagement)}functioncanPlayerSeek(player){varcurrentSrc=(getPlayerData(player).streamInfo.url||"").toLowerCase();returncurrentSrc.indexOf(".m3u8")!==-1||"Transcode"!==getPlayerData(player).streamInfo.playMethod&&player.duration()}functionchangeStream(player,ticks,params){if(canPlayerSeek(player)&&null==params)returnvoidplayer.currentTime(parseInt(ticks/1e4));params=params||{};varliveStreamId=getPlayerData(player).streamInfo.liveStreamId,playSessionId=getPlayerData(player).streamInfo.playSessionId,playerData=getPlayerData(player),currentItem=playerData.streamInfo.item;player.getDeviceProfile(currentItem).then(function(deviceProfile){varaudioStreamIndex=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));varmaxBitrate=params.MaxStreamingBitrate||self.get
})}},self.trySetActiveDeviceName=function(name){functionnormalizeName(t){returnt.toLowerCase().replace(" ","")}name=normalizeName(name),self.getTargets().then(function(result){vartarget=result.filter(function(p){returnnormalizeName(p.name)===name})[0];target&&self.trySetActivePlayer(target.playerName,target)})},self.setDefaultPlayerActive=function(){self.setActivePlayer("localplayer")},self.removeActivePlayer=function(name){varplayerInfo=self.getPlayerInfo();playerInfo&&playerInfo.name===name&&self.setDefaultPlayerActive()},self.removeActiveTarget=function(id){varplayerInfo=self.getPlayerInfo();playerInfo&&playerInfo.id===id&&self.setDefaultPlayerActive()},self.disconnectFromPlayer=function(){varplayerInfo=self.getPlayerInfo();playerInfo&&(playerInfo.supportedCommands.indexOf("EndSession")!==-1?require(["dialog"],function(dialog){varmenuItems=[];menuItems.push({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(){varpromises=players.filter(function(p){return!displayPlayerInLocalGroup(p)}).map(getPlayerTargets);returnPromise.all(promises).then(function(responses){vartargets=[];targets.push({name:globalize.translate("sharedcomponents#HeaderMyDevice"),id:"localplayer",playerName:"localplayer",playableMediaTypes:["Audio","Video","Game"],isLocalPlayer:!0,supportedCommands:getSupportedCommands({isLocalPlayer:!0})});for(vari=0;i<responses.length;i++)for(varsubTargets=responses[i],j=0;j<subTargets.length;j++)targets.push(subTargets[j]);returntargets=targets.sort(function(a,b){varaVal=a.isLocalPlayer?0:1,bVal=b.isLocalPlayer?0:1;returnaVal=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"SetBrightness":self.setBrightness(cmd.Arguments.Brightness,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){varmediaSource=self.currentMediaSource(player),mediaStreams=(mediaSource||{}).MediaStreams||[];returnmediaStreams.filter(function(s){return"Audio"===s.Type})},self.subtitleTracks=function(player){varmediaSource=self.currentMediaSource(player),mediaStreams=(mediaSource||{}).MediaStreams||[];returnmediaStreams.filter(function(s){return"Subtitle"===s.Type})},self.getPlaylist=function(player){returnplayer=player||currentPlayer,player&&!enableLocalPlaylistManagement(player)?player.getPlaylist():Promise.resolve(playlist.slice(0))},self.getCurrentPlayer=function(){returncurrentPlayer},self.isPlaying=function(player){returnplayer=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;varplayerData=getPlayerData(player)||{};returnmediaTypes.indexOf((playerData.streamInfo||{}).mediaType||"")!==-1},self.isPlay