4 * Copyright (C) 2005-2013 Team XBMC
7 * This Program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This Program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with XBMC; see the file COPYING. If not, see
19 * <http://www.gnu.org/licenses/>.
23 #include "system.h" // for HAS_DVD_DRIVE et. al.
24 #include "XBApplicationEx.h"
26 #include "guilib/IMsgTargetCallback.h"
27 #include "threads/Condition.h"
28 #include "utils/GlobalsHandling.h"
40 typedef boost::shared_ptr<IAddon> AddonPtr;
43 namespace MEDIA_DETECT
47 class CPlayerController;
49 #include "cores/IPlayerCallback.h"
50 #include "cores/playercorefactory/PlayerCoreFactory.h"
51 #include "PlayListPlayer.h"
52 #include "settings/lib/ISettingsHandler.h"
53 #include "settings/lib/ISettingCallback.h"
54 #include "settings/lib/ISubSettings.h"
55 #if !defined(TARGET_WINDOWS) && defined(HAS_DVD_DRIVE)
56 #include "storage/DetectDVDType.h"
59 #include "win32/WIN32Util.h"
61 #include "utils/Stopwatch.h"
62 #ifdef HAS_PERFORMANCE_SAMPLE
63 #include "utils/PerformanceStats.h"
65 #include "windowing/XBMC_events.h"
66 #include "threads/Thread.h"
68 #include "ApplicationPlayer.h"
71 class CKaraokeLyricsManager;
72 class CInertialScrollingHandler;
80 class CVideoInfoScanner;
85 class CMusicInfoScanner;
88 #define VOLUME_MINIMUM 0.0f // -60dB
89 #define VOLUME_MAXIMUM 1.0f // 0dB
90 #define VOLUME_DYNAMIC_RANGE 90.0f // 60dB
91 #define VOLUME_CONTROL_STEPS 90 // 90 steps
93 // replay gain settings struct for quick access by the player multiple
94 // times per second (saves doing settings lookup)
95 struct ReplayGainSettings
103 class CBackgroundPlayer : public CThread
106 CBackgroundPlayer(const CFileItem &item, int iPlayList);
107 virtual ~CBackgroundPlayer();
108 virtual void Process();
114 class CApplication : public CXBApplicationEx, public IPlayerCallback, public IMsgTargetCallback,
115 public ISettingCallback, public ISettingsHandler, public ISubSettings
117 friend class CApplicationPlayer;
132 virtual ~CApplication(void);
133 virtual bool Initialize();
134 virtual void FrameMove(bool processEvents, bool processGUI = true);
135 virtual void Render();
136 virtual bool RenderNoPresent();
137 virtual void Preflight();
138 virtual bool Create();
139 virtual bool Cleanup();
143 bool DestroyWindow();
144 void StartServices();
147 bool StartServer(enum ESERVERS eServer, bool bStart, bool bWait = false);
149 void StartPVRManager(bool bOpenPVRWindow = false);
150 void StopPVRManager();
151 bool IsCurrentThread() const;
152 void Stop(int exitCode);
154 void UnloadSkin(bool forReload = false);
155 bool LoadUserWindows();
156 void ReloadSkin(bool confirm = false);
157 const CStdString& CurrentFile();
158 CFileItem& CurrentFileItem();
159 CFileItem& CurrentUnstackedItem();
160 virtual bool OnMessage(CGUIMessage& message);
161 PLAYERCOREID GetCurrentPlayer();
162 virtual void OnPlayBackEnded();
163 virtual void OnPlayBackStarted();
164 virtual void OnPlayBackPaused();
165 virtual void OnPlayBackResumed();
166 virtual void OnPlayBackStopped();
167 virtual void OnQueueNextItem();
168 virtual void OnPlayBackSeek(int iTime, int seekOffset);
169 virtual void OnPlayBackSeekChapter(int iChapter);
170 virtual void OnPlayBackSpeedChanged(int iSpeed);
171 bool PlayMedia(const CFileItem& item, int iPlaylist = PLAYLIST_MUSIC);
172 bool PlayMediaSync(const CFileItem& item, int iPlaylist = PLAYLIST_MUSIC);
173 bool ProcessAndStartPlaylist(const CStdString& strPlayList, PLAYLIST::CPlayList& playlist, int iPlaylist, int track=0);
174 PlayBackRet PlayFile(const CFileItem& item, bool bRestart = false);
175 void SaveFileState(bool bForeground = false);
176 void UpdateFileState();
178 void Restart(bool bSamePosition = true);
179 void DelayedPlayerRestart();
180 void CheckDelayedPlayerRestart();
181 bool IsPlayingFullScreenVideo() const;
182 bool IsStartingPlayback() const { return m_bPlaybackStarting; }
184 bool OnKey(const CKey& key);
185 bool OnAppCommand(const CAction &action);
186 bool OnAction(const CAction &action);
187 void CheckShutdown();
188 void InhibitIdleShutdown(bool inhibit);
189 bool IsIdleShutdownInhibited() const;
190 // Checks whether the screensaver and / or DPMS should become active.
191 void CheckScreenSaverAndDPMS();
192 void CheckPlayingProgress();
193 void ActivateScreenSaver(bool forceType = false);
194 void CloseNetworkShares();
196 virtual void Process();
198 void ResetScreenSaver();
199 float GetVolume(bool percentage = true) const;
200 void SetVolume(float iValue, bool isPercentage = true);
201 bool IsMuted() const;
202 bool IsMutedInternal() const { return m_muted; }
203 void ToggleMute(void);
204 void SetMute(bool mute);
205 void ShowVolumeBar(const CAction *action = NULL);
206 int GetSubtitleDelay() const;
207 int GetAudioDelay() const;
208 void ResetSystemIdleTimer();
209 void ResetScreenSaverTimer();
210 void StopScreenSaverTimer();
211 // Wakes up from the screensaver and / or DPMS. Returns true if woken up.
212 bool WakeUpScreenSaverAndDPMS(bool bPowerOffKeyPressed = false);
213 bool WakeUpScreenSaver(bool bPowerOffKeyPressed = false);
215 \brief Returns the total time in fractional seconds of the currently playing media
217 Beware that this method returns fractional seconds whereas IPlayer::GetTotalTime() returns milliseconds.
219 double GetTotalTime() const;
221 \brief Returns the current time in fractional seconds of the currently playing media
223 Beware that this method returns fractional seconds whereas IPlayer::GetTime() returns milliseconds.
225 double GetTime() const;
226 float GetPercentage() const;
228 // Get the percentage of data currently cached/buffered (aq/vq + FileCache) from the input stream if applicable.
229 float GetCachePercentage() const;
231 void SeekPercentage(float percent);
232 void SeekTime( double dTime = 0.0 );
234 void StopShutdownTimer();
235 void ResetShutdownTimers();
237 void StopVideoScan();
238 void StopMusicScan();
239 bool IsMusicScanning() const;
240 bool IsVideoScanning() const;
242 void StartVideoCleanup();
244 void StartVideoScan(const CStdString &path, bool scanAll = false);
245 void StartMusicScan(const CStdString &path, int flags = 0);
246 void StartMusicAlbumScan(const CStdString& strDirectory, bool refresh=false);
247 void StartMusicArtistScan(const CStdString& strDirectory, bool refresh=false);
249 void UpdateLibraries();
250 void CheckMusicPlaylist();
252 bool ExecuteXBMCAction(std::string action);
254 static bool OnEvent(XBMC_Event& newEvent);
256 CNetwork& getNetwork();
257 #ifdef HAS_PERFORMANCE_SAMPLE
258 CPerformanceStats &GetPerformanceStats();
262 MEDIA_DETECT::CAutorun* m_Autorun;
265 #if !defined(TARGET_WINDOWS) && defined(HAS_DVD_DRIVE)
266 MEDIA_DETECT::CDetectDVDMedia m_DetectDVDType;
269 CApplicationPlayer* m_pPlayer;
271 inline bool IsInScreenSaver() { return m_bScreenSave; };
272 int m_iScreenSaveLock; // spiff: are we checking for a lock? if so, ignore the screensaver state, if -1 we have failed to input locks
274 bool m_bPlaybackStarting;
283 PlayState m_ePlayState;
284 CCriticalSection m_playStateMutex;
286 CKaraokeLyricsManager* m_pKaraokeMgr;
288 PLAYERCOREID m_eForcedNextPlayer;
289 CStdString m_strPlayListFile;
291 int GlobalIdleTime();
293 void EnablePlatformDirectories(bool enable=true)
295 m_bPlatformDirectories = enable;
298 bool PlatformDirectoriesEnabled()
300 return m_bPlatformDirectories;
303 void SetStandAlone(bool value);
307 return m_bStandalone;
310 void SetEnableLegacyRes(bool value)
312 m_bEnableLegacyRes = value;
315 bool IsEnableLegacyRes()
317 return m_bEnableLegacyRes;
320 void SetEnableTestMode(bool value)
325 bool IsEnableTestMode()
331 bool ToggleDPMS(bool manual);
333 float GetDimScreenSaverLevel() const;
335 /*! \brief Retrieve the applications seek handler.
336 \return a constant pointer to the seek handler.
339 const CSeekHandler *GetSeekHandler() const { return m_seekHandler; };
341 bool SwitchToFullScreen();
343 CSplash* GetSplash() { return m_splash; }
344 void SetRenderGUI(bool renderGUI);
345 bool GetRenderGUI() const { return m_renderGUI; };
347 bool SetLanguage(const CStdString &strLanguage);
349 ReplayGainSettings& GetReplayGainSettings() { return m_replayGainSettings; }
351 void SetLoggingIn(bool loggingIn) { m_loggingIn = loggingIn; }
354 virtual bool OnSettingsSaving() const;
356 virtual bool Load(const TiXmlNode *settings);
357 virtual bool Save(TiXmlNode *settings) const;
359 virtual void OnSettingChanged(const CSetting *setting);
360 virtual void OnSettingAction(const CSetting *setting);
361 virtual bool OnSettingUpdate(CSetting* &setting, const char *oldSettingId, const TiXmlNode *oldSettingNode);
363 bool LoadSkin(const CStdString& skinID);
364 void LoadSkin(const boost::shared_ptr<ADDON::CSkinInfo>& skin);
366 bool m_skinReloading; // if true we disallow LoadSkin until ReloadSkin is called
367 bool m_skinReverting;
371 #if defined(TARGET_DARWIN_IOS)
372 friend class CWinEventsIOS;
374 #if defined(TARGET_ANDROID)
375 friend class CWinEventsAndroid;
379 ADDON::AddonPtr m_screenSaver;
382 #ifdef TARGET_WINDOWS
383 CWinIdleTimer m_idleTimer;
384 CWinIdleTimer m_screenSaverTimer;
386 CStopWatch m_idleTimer;
387 CStopWatch m_screenSaverTimer;
389 CStopWatch m_restartPlayerTimer;
390 CStopWatch m_frameTime;
391 CStopWatch m_navigationTimer;
392 CStopWatch m_slowTimer;
393 CStopWatch m_shutdownTimer;
395 bool m_bInhibitIdleShutdown;
401 CFileItemPtr m_itemCurrentFile;
402 CFileItemList* m_currentStack;
403 CFileItemPtr m_stackFileItemToUpdate;
405 CStdString m_prevMedia;
407 ThreadIdentifier m_threadID; // application thread ID. Used in applicationMessanger to know where we are firing a thread with delay from.
408 bool m_bInitializing;
409 bool m_bPlatformDirectories;
411 CBookmark& m_progressTrackingVideoResumeBookmark;
412 CFileItemPtr m_progressTrackingItem;
413 bool m_progressTrackingPlayCountUpdate;
415 int m_currentStackPosition;
416 int m_nextPlaylistItem;
418 bool m_bPresentFrame;
419 unsigned int m_lastFrameTime;
420 unsigned int m_lastRenderTime;
423 bool m_bEnableLegacyRes;
425 bool m_bSystemScreenSaverEnable;
427 VIDEO::CVideoInfoScanner *m_videoInfoScanner;
428 MUSIC_INFO::CMusicInfoScanner *m_musicInfoScanner;
436 void SetHardwareVolume(float hardwareVolume);
438 void VolumeChanged() const;
440 PlayBackRet PlayStack(const CFileItem& item, bool bRestart);
442 bool ProcessRemote(float frameTime);
443 bool ProcessGamepad(float frameTime);
444 bool ProcessEventServer(float frameTime);
445 bool ProcessPeripherals(float frameTime);
446 bool ProcessJoystickEvent(const std::string& joystickName, int button, short inputType, float fAmount, unsigned int holdTime = 0);
447 bool ExecuteInputAction(const CAction &action);
448 int GetActiveWindowID(void);
450 float NavigationIdleTime();
451 static bool AlwaysProcess(const CAction& action);
453 void SaveCurrentFileSettings();
455 bool InitDirectoriesLinux();
456 bool InitDirectoriesOSX();
457 bool InitDirectoriesWin32();
458 void CreateUserDirs();
460 CSeekHandler *m_seekHandler;
461 CPlayerController *m_playerController;
462 CInertialScrollingHandler *m_pInertialScrollingHandler;
464 #ifdef HAS_PERFORMANCE_SAMPLE
465 CPerformanceStats m_perfStats;
468 #ifdef HAS_EVENT_SERVER
469 std::map<std::string, std::map<int, float> > m_lastAxisMap;
472 ReplayGainSettings m_replayGainSettings;
475 XBMC_GLOBAL_REF(CApplication,g_application);
476 #define g_application XBMC_GLOBAL_USE(CApplication)