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"
29 #include "utils/StdString.h"
41 typedef boost::shared_ptr<IAddon> AddonPtr;
44 namespace MEDIA_DETECT
48 class CPlayerController;
50 #include "cores/IPlayerCallback.h"
51 #include "cores/playercorefactory/PlayerCoreFactory.h"
52 #include "PlayListPlayer.h"
53 #include "settings/lib/ISettingsHandler.h"
54 #include "settings/lib/ISettingCallback.h"
55 #include "settings/lib/ISubSettings.h"
56 #if !defined(TARGET_WINDOWS) && defined(HAS_DVD_DRIVE)
57 #include "storage/DetectDVDType.h"
60 #include "win32/WIN32Util.h"
62 #include "utils/Stopwatch.h"
63 #ifdef HAS_PERFORMANCE_SAMPLE
64 #include "utils/PerformanceStats.h"
66 #include "windowing/XBMC_events.h"
67 #include "threads/Thread.h"
69 #include "ApplicationPlayer.h"
72 class CKaraokeLyricsManager;
73 class CInertialScrollingHandler;
81 class CVideoInfoScanner;
86 class CMusicInfoScanner;
89 #define VOLUME_MINIMUM 0.0f // -60dB
90 #define VOLUME_MAXIMUM 1.0f // 0dB
91 #define VOLUME_DYNAMIC_RANGE 90.0f // 60dB
92 #define VOLUME_CONTROL_STEPS 90 // 90 steps
94 // replay gain settings struct for quick access by the player multiple
95 // times per second (saves doing settings lookup)
96 struct ReplayGainSettings
104 class CBackgroundPlayer : public CThread
107 CBackgroundPlayer(const CFileItem &item, int iPlayList);
108 virtual ~CBackgroundPlayer();
109 virtual void Process();
115 class CApplication : public CXBApplicationEx, public IPlayerCallback, public IMsgTargetCallback,
116 public ISettingCallback, public ISettingsHandler, public ISubSettings
118 friend class CApplicationPlayer;
133 virtual ~CApplication(void);
134 virtual bool Initialize();
135 virtual void FrameMove(bool processEvents, bool processGUI = true);
136 virtual void Render();
137 virtual bool RenderNoPresent();
138 virtual void Preflight();
139 virtual bool Create();
140 virtual bool Cleanup();
144 bool DestroyWindow();
145 void StartServices();
148 bool StartServer(enum ESERVERS eServer, bool bStart, bool bWait = false);
151 * @brief Starts the PVR manager and decide if the manager should handle the startup window activation.
152 * @return true, if the startup window activation is handled by the pvr manager, otherwise false
154 bool StartPVRManager();
155 void StopPVRManager();
156 bool IsCurrentThread() const;
157 void Stop(int exitCode);
159 void UnloadSkin(bool forReload = false);
160 bool LoadUserWindows();
161 void ReloadSkin(bool confirm = false);
162 const std::string& CurrentFile();
163 CFileItem& CurrentFileItem();
164 CFileItem& CurrentUnstackedItem();
165 virtual bool OnMessage(CGUIMessage& message);
166 PLAYERCOREID GetCurrentPlayer();
167 virtual void OnPlayBackEnded();
168 virtual void OnPlayBackStarted();
169 virtual void OnPlayBackPaused();
170 virtual void OnPlayBackResumed();
171 virtual void OnPlayBackStopped();
172 virtual void OnQueueNextItem();
173 virtual void OnPlayBackSeek(int iTime, int seekOffset);
174 virtual void OnPlayBackSeekChapter(int iChapter);
175 virtual void OnPlayBackSpeedChanged(int iSpeed);
176 bool PlayMedia(const CFileItem& item, int iPlaylist = PLAYLIST_MUSIC);
177 bool PlayMediaSync(const CFileItem& item, int iPlaylist = PLAYLIST_MUSIC);
178 bool ProcessAndStartPlaylist(const CStdString& strPlayList, PLAYLIST::CPlayList& playlist, int iPlaylist, int track=0);
179 PlayBackRet PlayFile(const CFileItem& item, bool bRestart = false);
180 void SaveFileState(bool bForeground = false);
181 void UpdateFileState();
182 void LoadVideoSettings(const std::string &path);
184 void Restart(bool bSamePosition = true);
185 void DelayedPlayerRestart();
186 void CheckDelayedPlayerRestart();
187 bool IsPlayingFullScreenVideo() const;
188 bool IsStartingPlayback() const { return m_bPlaybackStarting; }
190 bool OnKey(const CKey& key);
191 bool OnAppCommand(const CAction &action);
192 bool OnAction(const CAction &action);
193 void CheckShutdown();
194 void InhibitIdleShutdown(bool inhibit);
195 bool IsIdleShutdownInhibited() const;
196 // Checks whether the screensaver and / or DPMS should become active.
197 void CheckScreenSaverAndDPMS();
198 void CheckPlayingProgress();
199 void ActivateScreenSaver(bool forceType = false);
200 void CloseNetworkShares();
202 virtual void Process();
204 void ResetScreenSaver();
205 float GetVolume(bool percentage = true) const;
206 void SetVolume(float iValue, bool isPercentage = true);
207 bool IsMuted() const;
208 bool IsMutedInternal() const { return m_muted; }
209 void ToggleMute(void);
210 void SetMute(bool mute);
211 void ShowVolumeBar(const CAction *action = NULL);
212 int GetSubtitleDelay() const;
213 int GetAudioDelay() const;
214 void ResetSystemIdleTimer();
215 void ResetScreenSaverTimer();
216 void StopScreenSaverTimer();
217 // Wakes up from the screensaver and / or DPMS. Returns true if woken up.
218 bool WakeUpScreenSaverAndDPMS(bool bPowerOffKeyPressed = false);
219 bool WakeUpScreenSaver(bool bPowerOffKeyPressed = false);
221 \brief Returns the total time in fractional seconds of the currently playing media
223 Beware that this method returns fractional seconds whereas IPlayer::GetTotalTime() returns milliseconds.
225 double GetTotalTime() const;
227 \brief Returns the current time in fractional seconds of the currently playing media
229 Beware that this method returns fractional seconds whereas IPlayer::GetTime() returns milliseconds.
231 double GetTime() const;
232 float GetPercentage() const;
234 // Get the percentage of data currently cached/buffered (aq/vq + FileCache) from the input stream if applicable.
235 float GetCachePercentage() const;
237 void SeekPercentage(float percent);
238 void SeekTime( double dTime = 0.0 );
240 void StopShutdownTimer();
241 void ResetShutdownTimers();
243 void StopVideoScan();
244 void StopMusicScan();
245 bool IsMusicScanning() const;
246 bool IsVideoScanning() const;
248 void StartVideoCleanup();
250 void StartVideoScan(const CStdString &path, bool scanAll = false);
251 void StartMusicScan(const CStdString &path, int flags = 0);
252 void StartMusicAlbumScan(const CStdString& strDirectory, bool refresh=false);
253 void StartMusicArtistScan(const CStdString& strDirectory, bool refresh=false);
255 void UpdateLibraries();
256 void CheckMusicPlaylist();
258 bool ExecuteXBMCAction(std::string action);
260 static bool OnEvent(XBMC_Event& newEvent);
262 CNetwork& getNetwork();
263 #ifdef HAS_PERFORMANCE_SAMPLE
264 CPerformanceStats &GetPerformanceStats();
268 MEDIA_DETECT::CAutorun* m_Autorun;
271 #if !defined(TARGET_WINDOWS) && defined(HAS_DVD_DRIVE)
272 MEDIA_DETECT::CDetectDVDMedia m_DetectDVDType;
275 CApplicationPlayer* m_pPlayer;
277 inline bool IsInScreenSaver() { return m_bScreenSave; };
278 inline bool IsDPMSActive() { return m_dpmsIsActive; };
279 int m_iScreenSaveLock; // spiff: are we checking for a lock? if so, ignore the screensaver state, if -1 we have failed to input locks
281 bool m_bPlaybackStarting;
290 PlayState m_ePlayState;
291 CCriticalSection m_playStateMutex;
293 CKaraokeLyricsManager* m_pKaraokeMgr;
295 PLAYERCOREID m_eForcedNextPlayer;
296 CStdString m_strPlayListFile;
298 int GlobalIdleTime();
300 void EnablePlatformDirectories(bool enable=true)
302 m_bPlatformDirectories = enable;
305 bool PlatformDirectoriesEnabled()
307 return m_bPlatformDirectories;
310 void SetStandAlone(bool value);
314 return m_bStandalone;
317 void SetEnableLegacyRes(bool value)
319 m_bEnableLegacyRes = value;
322 bool IsEnableLegacyRes()
324 return m_bEnableLegacyRes;
327 void SetEnableTestMode(bool value)
332 bool IsEnableTestMode()
338 bool ToggleDPMS(bool manual);
340 float GetDimScreenSaverLevel() const;
342 /*! \brief Retrieve the applications seek handler.
343 \return a constant pointer to the seek handler.
346 const CSeekHandler *GetSeekHandler() const { return m_seekHandler; };
348 bool SwitchToFullScreen();
350 CSplash* GetSplash() { return m_splash; }
351 void SetRenderGUI(bool renderGUI);
352 bool GetRenderGUI() const { return m_renderGUI; };
354 bool SetLanguage(const CStdString &strLanguage);
356 ReplayGainSettings& GetReplayGainSettings() { return m_replayGainSettings; }
358 void SetLoggingIn(bool loggingIn) { m_loggingIn = loggingIn; }
361 virtual bool OnSettingsSaving() const;
363 virtual bool Load(const TiXmlNode *settings);
364 virtual bool Save(TiXmlNode *settings) const;
366 virtual void OnSettingChanged(const CSetting *setting);
367 virtual void OnSettingAction(const CSetting *setting);
368 virtual bool OnSettingUpdate(CSetting* &setting, const char *oldSettingId, const TiXmlNode *oldSettingNode);
370 bool LoadSkin(const CStdString& skinID);
371 bool LoadSkin(const boost::shared_ptr<ADDON::CSkinInfo>& skin);
373 bool m_skinReverting;
377 #if defined(TARGET_DARWIN_IOS)
378 friend class CWinEventsIOS;
380 #if defined(TARGET_ANDROID)
381 friend class CWinEventsAndroid;
385 ADDON::AddonPtr m_screenSaver;
388 #ifdef TARGET_WINDOWS
389 CWinIdleTimer m_idleTimer;
390 CWinIdleTimer m_screenSaverTimer;
392 CStopWatch m_idleTimer;
393 CStopWatch m_screenSaverTimer;
395 CStopWatch m_restartPlayerTimer;
396 CStopWatch m_frameTime;
397 CStopWatch m_navigationTimer;
398 CStopWatch m_slowTimer;
399 CStopWatch m_shutdownTimer;
401 bool m_bInhibitIdleShutdown;
407 CFileItemPtr m_itemCurrentFile;
408 CFileItemList* m_currentStack;
409 CFileItemPtr m_stackFileItemToUpdate;
411 CStdString m_prevMedia;
413 ThreadIdentifier m_threadID; // application thread ID. Used in applicationMessanger to know where we are firing a thread with delay from.
414 bool m_bInitializing;
415 bool m_bPlatformDirectories;
417 CBookmark& m_progressTrackingVideoResumeBookmark;
418 CFileItemPtr m_progressTrackingItem;
419 bool m_progressTrackingPlayCountUpdate;
421 int m_currentStackPosition;
422 int m_nextPlaylistItem;
424 bool m_bPresentFrame;
425 unsigned int m_lastFrameTime;
426 unsigned int m_lastRenderTime;
429 bool m_bEnableLegacyRes;
431 bool m_bSystemScreenSaverEnable;
433 VIDEO::CVideoInfoScanner *m_videoInfoScanner;
434 MUSIC_INFO::CMusicInfoScanner *m_musicInfoScanner;
442 void SetHardwareVolume(float hardwareVolume);
444 void VolumeChanged() const;
446 PlayBackRet PlayStack(const CFileItem& item, bool bRestart);
448 bool ProcessRemote(float frameTime);
449 bool ProcessGamepad(float frameTime);
450 bool ProcessEventServer(float frameTime);
451 bool ProcessPeripherals(float frameTime);
452 bool ProcessJoystickEvent(const std::string& joystickName, int button, short inputType, float fAmount, unsigned int holdTime = 0);
453 bool ExecuteInputAction(const CAction &action);
454 int GetActiveWindowID(void);
456 float NavigationIdleTime();
457 static bool AlwaysProcess(const CAction& action);
459 bool InitDirectoriesLinux();
460 bool InitDirectoriesOSX();
461 bool InitDirectoriesWin32();
462 void CreateUserDirs();
464 CSeekHandler *m_seekHandler;
465 CPlayerController *m_playerController;
466 CInertialScrollingHandler *m_pInertialScrollingHandler;
468 #ifdef HAS_PERFORMANCE_SAMPLE
469 CPerformanceStats m_perfStats;
472 #ifdef HAS_EVENT_SERVER
473 std::map<std::string, std::map<int, float> > m_lastAxisMap;
476 ReplayGainSettings m_replayGainSettings;
479 XBMC_GLOBAL_REF(CApplication,g_application);
480 #define g_application XBMC_GLOBAL_USE(CApplication)