4 * Copyright (C) 2005-2012 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 "guilib/Key.h"
28 #include "threads/Condition.h"
38 typedef boost::shared_ptr<IAddon> AddonPtr;
41 namespace MEDIA_DETECT
46 #include "cores/IPlayer.h"
47 #include "cores/playercorefactory/PlayerCoreFactory.h"
48 #include "PlayListPlayer.h"
49 #if !defined(_WIN32) && defined(HAS_DVD_DRIVE)
50 #include "storage/DetectDVDType.h"
53 #include "win32/WIN32Util.h"
55 #include "utils/Stopwatch.h"
56 #include "network/Network.h"
57 #include "utils/CharsetConverter.h"
58 #ifdef HAS_PERFORMANCE_SAMPLE
59 #include "utils/PerformanceStats.h"
61 #include "windowing/XBMC_events.h"
62 #include "threads/Thread.h"
65 class CKaraokeLyricsManager;
66 class CInertialScrollingHandler;
73 class CHTTPImageHandler;
74 class CHTTPVfsHandler;
76 class CHTTPJsonRpcHandler;
78 #ifdef HAS_WEB_INTERFACE
79 class CHTTPWebinterfaceHandler;
80 class CHTTPWebinterfaceAddonsHandler;
85 class CVideoInfoScanner;
90 class CMusicInfoScanner;
93 class CBackgroundPlayer : public CThread
96 CBackgroundPlayer(const CFileItem &item, int iPlayList);
97 virtual ~CBackgroundPlayer();
98 virtual void Process();
104 class CApplication : public CXBApplicationEx, public IPlayerCallback, public IMsgTargetCallback
120 virtual ~CApplication(void);
121 virtual bool Initialize();
122 virtual void FrameMove(bool processEvents, bool processGUI = true);
123 virtual void Render();
124 virtual bool RenderNoPresent();
125 virtual void Preflight();
126 virtual bool Create();
127 virtual bool Cleanup();
131 bool DestroyWindow();
132 void StartServices();
135 bool StartServer(enum ESERVERS eServer, bool bStart, bool bWait = false);
137 bool StartWebServer();
138 void StopWebServer();
139 bool StartAirplayServer();
140 void StopAirplayServer(bool bWait);
141 bool StartJSONRPCServer();
142 void StopJSONRPCServer(bool bWait);
144 void StopUPnP(bool bWait);
145 void StartUPnPRenderer();
146 void StopUPnPRenderer();
147 void StartUPnPServer();
148 void StopUPnPServer();
149 void StartPVRManager();
150 void StopPVRManager();
151 bool StartEventServer();
152 bool StopEventServer(bool bWait, bool promptuser);
153 void RefreshEventServer();
154 void StartZeroconf();
156 void DimLCDOnPlayback(bool dim);
157 bool IsCurrentThread() const;
158 void Stop(int exitCode);
160 void UnloadSkin(bool forReload = false);
161 bool LoadUserWindows();
163 const CStdString& CurrentFile();
164 CFileItem& CurrentFileItem();
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 bool PlayFile(const CFileItem& item, bool bRestart = false);
180 void SaveFileState(bool bForeground = false);
181 void UpdateFileState();
183 void Restart(bool bSamePosition = true);
184 void DelayedPlayerRestart();
185 void CheckDelayedPlayerRestart();
186 bool IsPlaying() const;
187 bool IsPaused() const;
188 bool IsPlayingAudio() const;
189 bool IsPlayingVideo() const;
190 bool IsPlayingFullScreenVideo() const;
191 bool IsStartingPlayback() const { return m_bPlaybackStarting; }
193 bool OnKey(const CKey& key);
194 bool OnAppCommand(const CAction &action);
195 bool OnAction(const CAction &action);
196 void CheckShutdown();
197 void InhibitIdleShutdown(bool inhibit);
198 bool IsIdleShutdownInhibited() const;
199 // Checks whether the screensaver and / or DPMS should become active.
200 void CheckScreenSaverAndDPMS();
201 void CheckPlayingProgress();
202 void CheckAudioScrobblerStatus();
203 void ActivateScreenSaver(bool forceType = false);
205 virtual void Process();
207 void ResetScreenSaver();
208 int GetVolume() const;
209 void SetVolume(float iValue, bool isPercentage = true);
210 bool IsMuted() const;
211 void ToggleMute(void);
212 void ShowVolumeBar(const CAction *action = NULL);
213 int GetPlaySpeed() const;
214 int GetSubtitleDelay() const;
215 int GetAudioDelay() const;
216 void SetPlaySpeed(int iSpeed);
217 void ResetSystemIdleTimer();
218 void ResetScreenSaverTimer();
219 void StopScreenSaverTimer();
220 // Wakes up from the screensaver and / or DPMS. Returns true if woken up.
221 bool WakeUpScreenSaverAndDPMS(bool bPowerOffKeyPressed = false);
222 bool WakeUpScreenSaver(bool bPowerOffKeyPressed = false);
224 \brief Returns the total time in fractional seconds of the currently playing media
226 Beware that this method returns fractional seconds whereas IPlayer::GetTotalTime() returns milliseconds.
228 double GetTotalTime() const;
230 \brief Returns the current time in fractional seconds of the currently playing media
232 Beware that this method returns fractional seconds whereas IPlayer::GetTime() returns milliseconds.
234 double GetTime() const;
235 float GetPercentage() const;
237 // Get the percentage of data currently cached/buffered (aq/vq + FileCache) from the input stream if applicable.
238 float GetCachePercentage() const;
240 void SeekPercentage(float percent);
241 void SeekTime( double dTime = 0.0 );
243 void StopShutdownTimer();
244 void ResetShutdownTimers();
246 void StopVideoScan();
247 void StopMusicScan();
248 bool IsMusicScanning() const;
249 bool IsVideoScanning() const;
251 void StartVideoCleanup();
253 void StartVideoScan(const CStdString &path, bool scanAll = false);
254 void StartMusicScan(const CStdString &path, int flags = 0);
255 void StartMusicAlbumScan(const CStdString& strDirectory, bool refresh=false);
256 void StartMusicArtistScan(const CStdString& strDirectory, bool refresh=false);
258 void UpdateLibraries();
259 void CheckMusicPlaylist();
261 bool ExecuteXBMCAction(std::string action);
263 static bool OnEvent(XBMC_Event& newEvent);
265 #if defined(HAS_LINUX_NETWORK)
266 CNetworkLinux& getNetwork();
267 #elif defined(HAS_WIN32_NETWORK)
268 CNetworkWin32& getNetwork();
270 CNetwork& getNetwork();
272 #ifdef HAS_PERFORMANCE_SAMPLE
273 CPerformanceStats &GetPerformanceStats();
277 MEDIA_DETECT::CAutorun* m_Autorun;
280 #if !defined(_WIN32) && defined(HAS_DVD_DRIVE)
281 MEDIA_DETECT::CDetectDVDMedia m_DetectDVDType;
286 #ifdef HAS_WEB_SERVER
287 CWebServer& m_WebServer;
288 CHTTPImageHandler& m_httpImageHandler;
289 CHTTPVfsHandler& m_httpVfsHandler;
291 CHTTPJsonRpcHandler& m_httpJsonRpcHandler;
293 #ifdef HAS_WEB_INTERFACE
294 CHTTPWebinterfaceHandler& m_httpWebinterfaceHandler;
295 CHTTPWebinterfaceAddonsHandler& m_httpWebinterfaceAddonsHandler;
299 inline bool IsInScreenSaver() { return m_bScreenSave; };
300 int m_iScreenSaveLock; // spiff: are we checking for a lock? if so, ignore the screensaver state, if -1 we have failed to input locks
303 bool m_bPlaybackStarting;
305 CKaraokeLyricsManager* m_pKaraokeMgr;
307 PLAYERCOREID m_eForcedNextPlayer;
308 CStdString m_strPlayListFile;
310 int GlobalIdleTime();
312 bool WaitFrame(unsigned int timeout);
314 void EnablePlatformDirectories(bool enable=true)
316 m_bPlatformDirectories = enable;
319 bool PlatformDirectoriesEnabled()
321 return m_bPlatformDirectories;
324 void SetStandAlone(bool value);
328 return m_bStandalone;
331 void SetEnableLegacyRes(bool value)
333 m_bEnableLegacyRes = value;
336 bool IsEnableLegacyRes()
338 return m_bEnableLegacyRes;
341 void SetEnableTestMode(bool value)
346 bool IsEnableTestMode()
351 bool IsPresentFrame();
354 bool ToggleDPMS(bool manual);
356 float GetDimScreenSaverLevel() const;
358 /*! \brief Retrieve the applications seek handler.
359 \return a constant pointer to the seek handler.
362 const CSeekHandler *GetSeekHandler() const { return m_seekHandler; };
364 bool SwitchToFullScreen();
366 CSplash* GetSplash() { return m_splash; }
367 void SetRenderGUI(bool renderGUI);
369 bool LoadSkin(const CStdString& skinID);
370 void LoadSkin(const boost::shared_ptr<ADDON::CSkinInfo>& skin);
372 bool m_skinReloading; // if true we disallow LoadSkin until ReloadSkin is called
374 #if defined(TARGET_DARWIN_IOS)
375 friend class CWinEventsIOS;
379 ADDON::AddonPtr m_screenSaver;
383 CWinIdleTimer m_idleTimer;
385 CStopWatch m_idleTimer;
387 CStopWatch m_restartPlayerTimer;
388 CStopWatch m_frameTime;
389 CStopWatch m_navigationTimer;
390 CStopWatch m_slowTimer;
391 CStopWatch m_screenSaverTimer;
392 CStopWatch m_shutdownTimer;
394 bool m_bInhibitIdleShutdown;
400 CFileItemPtr m_itemCurrentFile;
401 CFileItemList* m_currentStack;
402 CFileItemPtr m_stackFileItemToUpdate;
404 CStdString m_prevMedia;
406 ThreadIdentifier m_threadID; // application thread ID. Used in applicationMessanger to know where we are firing a thread with delay from.
407 PLAYERCOREID m_eCurrentPlayer;
408 bool m_bInitializing;
409 bool m_bPlatformDirectories;
411 CBookmark& m_progressTrackingVideoResumeBookmark;
412 CFileItemPtr m_progressTrackingItem;
413 bool m_progressTrackingPlayCountUpdate;
416 int m_currentStackPosition;
417 int m_nextPlaylistItem;
419 bool m_bPresentFrame;
420 unsigned int m_lastFrameTime;
421 unsigned int m_lastRenderTime;
424 bool m_bEnableLegacyRes;
426 bool m_bSystemScreenSaverEnable;
429 CCriticalSection m_frameMutex;
430 XbmcThreads::ConditionVariable m_frameCond;
432 VIDEO::CVideoInfoScanner *m_videoInfoScanner;
433 MUSIC_INFO::CMusicInfoScanner *m_musicInfoScanner;
438 void SetHardwareVolume(float hardwareVolume);
441 void VolumeChanged() const;
443 bool PlayStack(const CFileItem& item, bool bRestart);
445 bool ProcessRemote(float frameTime);
446 bool ProcessGamepad(float frameTime);
447 bool ProcessEventServer(float frameTime);
448 bool ProcessPeripherals(float frameTime);
449 bool ProcessJoystickEvent(const std::string& joystickName, int button, bool isAxis, float fAmount, unsigned int holdTime = 0);
450 int GetActiveWindowID(void);
452 float NavigationIdleTime();
453 static bool AlwaysProcess(const CAction& action);
455 void SaveCurrentFileSettings();
457 bool InitDirectoriesLinux();
458 bool InitDirectoriesOSX();
459 bool InitDirectoriesWin32();
460 void CreateUserDirs();
462 CSeekHandler *m_seekHandler;
463 CInertialScrollingHandler *m_pInertialScrollingHandler;
464 #if defined(HAS_LINUX_NETWORK)
465 CNetworkLinux m_network;
466 #elif defined(HAS_WIN32_NETWORK)
467 CNetworkWin32 m_network;
471 #ifdef HAS_PERFORMANCE_SAMPLE
472 CPerformanceStats m_perfStats;
475 #ifdef HAS_EVENT_SERVER
476 std::map<std::string, std::map<int, float> > m_lastAxisMap;
481 extern CApplication g_application;