PageRenderTime 30ms CodeModel.GetById 1ms app.highlight 23ms RepoModel.GetById 2ms app.codeStats 0ms

/xbmc/Application.h

http://github.com/xbmc/xbmc
C Header | 490 lines | 330 code | 89 blank | 71 comment | 2 complexity | 92ddb7b49a6c587b2011d6d62ae84d09 MD5 | raw file
  1/*
  2 *  Copyright (C) 2005-2018 Team Kodi
  3 *  This file is part of Kodi - https://kodi.tv
  4 *
  5 *  SPDX-License-Identifier: GPL-2.0-or-later
  6 *  See LICENSES/README.md for more information.
  7 */
  8
  9#pragma once
 10
 11#include "XBApplicationEx.h"
 12
 13#include "guilib/IMsgTargetCallback.h"
 14#include "windowing/Resolution.h"
 15#include "utils/GlobalsHandling.h"
 16#include "messaging/IMessageTarget.h"
 17#include "ServiceManager.h"
 18#include "ApplicationStackHelper.h"
 19
 20#include <atomic>
 21#include <deque>
 22#include <map>
 23#include <memory>
 24#include <string>
 25#include <vector>
 26
 27#include "cores/IPlayerCallback.h"
 28#include "settings/lib/ISettingsHandler.h"
 29#include "settings/lib/ISettingCallback.h"
 30#include "settings/ISubSettings.h"
 31#if !defined(TARGET_WINDOWS) && defined(HAS_DVD_DRIVE)
 32#include "storage/DetectDVDType.h"
 33#endif
 34#ifdef TARGET_WINDOWS
 35#include "powermanagement/WinIdleTimer.h"
 36#endif
 37#include "utils/Stopwatch.h"
 38#include "windowing/OSScreenSaver.h"
 39#include "windowing/XBMC_events.h"
 40#include "threads/SystemClock.h"
 41#include "threads/Thread.h"
 42
 43#include "ApplicationPlayer.h"
 44
 45class CAction;
 46class CFileItem;
 47class CFileItemList;
 48class CKey;
 49class CSeekHandler;
 50class CInertialScrollingHandler;
 51class CSplash;
 52class CBookmark;
 53class IActionListener;
 54class CGUIComponent;
 55class CAppInboundProtocol;
 56class CSettingsComponent;
 57
 58namespace ADDON
 59{
 60  class CSkinInfo;
 61  class IAddon;
 62  typedef std::shared_ptr<IAddon> AddonPtr;
 63}
 64
 65namespace ANNOUNCEMENT
 66{
 67  class CAnnouncementManager;
 68}
 69
 70namespace MEDIA_DETECT
 71{
 72  class CAutorun;
 73}
 74
 75namespace PLAYLIST
 76{
 77  class CPlayList;
 78}
 79
 80namespace ActiveAE
 81{
 82  class CActiveAE;
 83}
 84
 85namespace VIDEO
 86{
 87  class CVideoInfoScanner;
 88}
 89
 90namespace MUSIC_INFO
 91{
 92  class CMusicInfoScanner;
 93}
 94
 95#define VOLUME_MINIMUM 0.0f        // -60dB
 96#define VOLUME_MAXIMUM 1.0f        // 0dB
 97#define VOLUME_DYNAMIC_RANGE 90.0f // 60dB
 98
 99// replay gain settings struct for quick access by the player multiple
100// times per second (saves doing settings lookup)
101struct ReplayGainSettings
102{
103  int iPreAmp;
104  int iNoGainPreAmp;
105  int iType;
106  bool bAvoidClipping;
107};
108
109enum StartupAction
110{
111  STARTUP_ACTION_NONE = 0,
112  STARTUP_ACTION_PLAY_TV,
113  STARTUP_ACTION_PLAY_RADIO
114};
115
116class CApplication : public CXBApplicationEx, public IPlayerCallback, public IMsgTargetCallback,
117                     public ISettingCallback, public ISettingsHandler, public ISubSettings,
118                     public KODI::MESSAGING::IMessageTarget
119{
120friend class CAppInboundProtocol;
121
122public:
123
124  // If playback time of current item is greater than this value, ACTION_PREV_ITEM will seek to start
125  // of currently playing item, otherwise it will seek to start of the previous item in playlist
126  static const unsigned int ACTION_PREV_ITEM_THRESHOLD = 3; // seconds;
127
128  enum ESERVERS
129  {
130    ES_WEBSERVER = 1,
131    ES_AIRPLAYSERVER,
132    ES_JSONRPCSERVER,
133    ES_UPNPRENDERER,
134    ES_UPNPSERVER,
135    ES_EVENTSERVER,
136    ES_ZEROCONF
137  };
138
139  CApplication(void);
140  ~CApplication(void) override;
141  bool Initialize() override;
142  void FrameMove(bool processEvents, bool processGUI = true) override;
143  void Render() override;
144  virtual void Preflight();
145  bool Create(const CAppParamParser &params);
146  bool Cleanup() override;
147
148  bool IsInitialized() { return !m_bInitializing; }
149
150  bool CreateGUI();
151  bool InitWindow(RESOLUTION res = RES_INVALID);
152  void StartServices();
153  void StopServices();
154
155  bool StartServer(enum ESERVERS eServer, bool bStart, bool bWait = false);
156
157  bool IsCurrentThread() const;
158  void Stop(int exitCode);
159  void UnloadSkin(bool forReload = false);
160  bool LoadCustomWindows();
161  void ReloadSkin(bool confirm = false);
162  const std::string& CurrentFile();
163  CFileItem& CurrentFileItem();
164  std::shared_ptr<CFileItem> CurrentFileItemPtr();
165  CFileItem& CurrentUnstackedItem();
166  bool OnMessage(CGUIMessage& message) override;
167  CApplicationPlayer& GetAppPlayer();
168  std::string GetCurrentPlayer();
169  CApplicationStackHelper& GetAppStackHelper();
170  void OnPlayBackEnded() override;
171  void OnPlayBackStarted(const CFileItem &file) override;
172  void OnPlayerCloseFile(const CFileItem &file, const CBookmark &bookmark) override;
173  void OnPlayBackPaused() override;
174  void OnPlayBackResumed() override;
175  void OnPlayBackStopped() override;
176  void OnPlayBackError() override;
177  void OnQueueNextItem() override;
178  void OnPlayBackSeek(int64_t iTime, int64_t seekOffset) override;
179  void OnPlayBackSeekChapter(int iChapter) override;
180  void OnPlayBackSpeedChanged(int iSpeed) override;
181  void OnAVChange() override;
182  void OnAVStarted(const CFileItem &file) override;
183  void RequestVideoSettings(const CFileItem &fileItem) override;
184  void StoreVideoSettings(const CFileItem &fileItem, CVideoSettings vs) override;
185
186  int  GetMessageMask() override;
187  void OnApplicationMessage(KODI::MESSAGING::ThreadMessage* pMsg) override;
188
189  bool PlayMedia(CFileItem& item, const std::string &player, int iPlaylist);
190  bool ProcessAndStartPlaylist(const std::string& strPlayList, PLAYLIST::CPlayList& playlist, int iPlaylist, int track=0);
191  bool PlayFile(CFileItem item, const std::string& player, bool bRestart = false);
192  void StopPlaying();
193  void Restart(bool bSamePosition = true);
194  void DelayedPlayerRestart();
195  void CheckDelayedPlayerRestart();
196  bool IsPlayingFullScreenVideo() const;
197  bool IsFullScreen();
198  bool OnAction(const CAction &action);
199  void CheckShutdown();
200  void InhibitIdleShutdown(bool inhibit);
201  bool IsIdleShutdownInhibited() const;
202  void InhibitScreenSaver(bool inhibit);
203  bool IsScreenSaverInhibited() const;
204  // Checks whether the screensaver and / or DPMS should become active.
205  void CheckScreenSaverAndDPMS();
206  void ActivateScreenSaver(bool forceType = false);
207  void CloseNetworkShares();
208
209  void ShowAppMigrationMessage();
210  void Process() override;
211  void ProcessSlow();
212  void ResetScreenSaver();
213  float GetVolumePercent() const;
214  float GetVolumeRatio() const;
215  void SetVolume(float iValue, bool isPercentage = true);
216  bool IsMuted() const;
217  bool IsMutedInternal() const { return m_muted; }
218  void ToggleMute(void);
219  void SetMute(bool mute);
220  void ShowVolumeBar(const CAction *action = NULL);
221  int GetSubtitleDelay();
222  int GetAudioDelay();
223  void ResetSystemIdleTimer();
224  void ResetScreenSaverTimer();
225  void StopScreenSaverTimer();
226  // Wakes up from the screensaver and / or DPMS. Returns true if woken up.
227  bool WakeUpScreenSaverAndDPMS(bool bPowerOffKeyPressed = false);
228  bool WakeUpScreenSaver(bool bPowerOffKeyPressed = false);
229  /*!
230   \brief Returns the total time in fractional seconds of the currently playing media
231
232   Beware that this method returns fractional seconds whereas IPlayer::GetTotalTime() returns milliseconds.
233   */
234  double GetTotalTime() const;
235  /*!
236   \brief Returns the current time in fractional seconds of the currently playing media
237
238   Beware that this method returns fractional seconds whereas IPlayer::GetTime() returns milliseconds.
239   */
240  double GetTime() const;
241  float GetPercentage() const;
242
243  // Get the percentage of data currently cached/buffered (aq/vq + FileCache) from the input stream if applicable.
244  float GetCachePercentage() const;
245
246  void SeekPercentage(float percent);
247  void SeekTime( double dTime = 0.0 );
248
249  void StopShutdownTimer();
250  void ResetShutdownTimers();
251
252  void StopVideoScan();
253  void StopMusicScan();
254  bool IsMusicScanning() const;
255  bool IsVideoScanning() const;
256
257  /*!
258   \brief Starts a video library cleanup.
259   \param userInitiated Whether the action was initiated by the user (either via GUI or any other method) or not.  It is meant to hide or show dialogs.
260   \param content Content type to clean, blank for everything
261   */
262  void StartVideoCleanup(bool userInitiated = true, const std::string& content = "");
263
264  /*!
265   \brief Starts a video library update.
266   \param path The path to scan or "" (empty string) for a global scan.
267   \param userInitiated Whether the action was initiated by the user (either via GUI or any other method) or not.  It is meant to hide or show dialogs.
268   \param scanAll Whether to scan everything not already scanned (regardless of whether the user normally doesn't want a folder scanned).
269   */
270  void StartVideoScan(const std::string &path, bool userInitiated = true, bool scanAll = false);
271
272  /*!
273  \brief Starts a music library cleanup.
274  \param userInitiated Whether the action was initiated by the user (either via GUI or any other method) or not.  It is meant to hide or show dialogs.
275  */
276  void StartMusicCleanup(bool userInitiated = true);
277
278  /*!
279   \brief Starts a music library update.
280   \param path The path to scan or "" (empty string) for a global scan.
281   \param userInitiated Whether the action was initiated by the user (either via GUI or any other method) or not.  It is meant to hide or show dialogs.
282   \param flags Flags for controlling the scanning process.  See xbmc/music/infoscanner/MusicInfoScanner.h for possible values.
283   */
284  void StartMusicScan(const std::string &path, bool userInitiated = true, int flags = 0);
285  void StartMusicAlbumScan(const std::string& strDirectory, bool refresh = false);
286  void StartMusicArtistScan(const std::string& strDirectory, bool refresh = false);
287
288  void UpdateLibraries();
289
290  void UpdateCurrentPlayArt();
291
292  bool ExecuteXBMCAction(std::string action, const CGUIListItemPtr &item = NULL);
293
294#ifdef HAS_DVD_DRIVE
295  std::unique_ptr<MEDIA_DETECT::CAutorun> m_Autorun;
296#endif
297
298#if !defined(TARGET_WINDOWS) && defined(HAS_DVD_DRIVE)
299  MEDIA_DETECT::CDetectDVDMedia m_DetectDVDType;
300#endif
301
302  inline bool IsInScreenSaver() { return m_screensaverActive; };
303  inline std::string ScreensaverIdInUse() { return m_screensaverIdInUse; }
304
305  inline bool IsDPMSActive() { return m_dpmsIsActive; };
306  int m_iScreenSaveLock = 0; // spiff: are we checking for a lock? if so, ignore the screensaver state, if -1 we have failed to input locks
307
308  std::string m_strPlayListFile;
309
310  int GlobalIdleTime();
311
312  bool PlatformDirectoriesEnabled() { return m_bPlatformDirectories; }
313  bool IsStandAlone() { return m_bStandalone; }
314  bool IsEnableTestMode() { return m_bTestMode; }
315
316  bool IsAppFocused() const { return m_AppFocused; }
317
318  void Minimize();
319  bool ToggleDPMS(bool manual);
320
321  bool SwitchToFullScreen(bool force = false);
322
323  bool GetRenderGUI() const override { return m_renderGUI; };
324
325  bool SetLanguage(const std::string &strLanguage);
326  bool LoadLanguage(bool reload);
327
328  ReplayGainSettings& GetReplayGainSettings() { return m_replayGainSettings; }
329
330  void SetLoggingIn(bool switchingProfiles);
331
332  /*!
333   \brief Register an action listener.
334   \param listener The listener to register
335   */
336  void RegisterActionListener(IActionListener *listener);
337  /*!
338   \brief Unregister an action listener.
339   \param listener The listener to unregister
340   */
341  void UnregisterActionListener(IActionListener *listener);
342
343  std::unique_ptr<CServiceManager> m_ServiceManager;
344
345  /*!
346  \brief Locks calls from outside kodi (e.g. python) until framemove is processed.
347  */
348  void LockFrameMoveGuard();
349
350  /*!
351  \brief Unlocks calls from outside kodi (e.g. python).
352  */
353  void UnlockFrameMoveGuard();
354
355  void SetRenderGUI(bool renderGUI);
356
357protected:
358  bool OnSettingsSaving() const override;
359  bool Load(const TiXmlNode *settings) override;
360  bool Save(TiXmlNode *settings) const override;
361  void OnSettingChanged(std::shared_ptr<const CSetting> setting) override;
362  void OnSettingAction(std::shared_ptr<const CSetting> setting) override;
363  bool OnSettingUpdate(std::shared_ptr<CSetting> setting, const char *oldSettingId, const TiXmlNode *oldSettingNode) override;
364
365  bool LoadSkin(const std::string& skinID);
366
367  void CheckOSScreenSaverInhibitionSetting();
368  void PlaybackCleanup();
369
370  // inbound protocol
371  bool OnEvent(XBMC_Event& newEvent);
372
373  /*!
374   \brief Delegates the action to all registered action handlers.
375   \param action The action
376   \return true, if the action was taken by one of the action listener.
377   */
378  bool NotifyActionListeners(const CAction &action) const;
379
380  std::shared_ptr<ANNOUNCEMENT::CAnnouncementManager> m_pAnnouncementManager;
381  std::unique_ptr<CSettingsComponent> m_pSettingsComponent;
382  std::unique_ptr<CGUIComponent> m_pGUI;
383  std::unique_ptr<CWinSystemBase> m_pWinSystem;
384  std::unique_ptr<ActiveAE::CActiveAE> m_pActiveAE;
385  std::shared_ptr<CAppInboundProtocol> m_pAppPort;
386  std::deque<XBMC_Event> m_portEvents;
387  CCriticalSection m_portSection;
388
389  bool m_confirmSkinChange = true;
390  bool m_ignoreSkinSettingChanges = false;
391
392  bool m_saveSkinOnUnloading = true;
393  bool m_autoExecScriptExecuted = false;
394
395#if defined(TARGET_DARWIN_IOS)
396  friend class CWinEventsIOS;
397#endif
398#if defined(TARGET_DARWIN_TVOS)
399  friend class CWinEventsTVOS;
400#endif
401#if defined(TARGET_ANDROID)
402  friend class CWinEventsAndroid;
403#endif
404  // screensaver
405  bool m_screensaverActive = false;
406  std::string m_screensaverIdInUse;
407  ADDON::AddonPtr m_pythonScreenSaver; // @warning: Fallback for Python interface, for binaries not needed!
408  // OS screen saver inhibitor that is always active if user selected a Kodi screen saver
409  KODI::WINDOWING::COSScreenSaverInhibitor m_globalScreensaverInhibitor;
410  // Inhibitor that is active e.g. during video playback
411  KODI::WINDOWING::COSScreenSaverInhibitor m_screensaverInhibitor;
412
413  // timer information
414#ifdef TARGET_WINDOWS
415  CWinIdleTimer m_idleTimer;
416  CWinIdleTimer m_screenSaverTimer;
417#else
418  CStopWatch m_idleTimer;
419  CStopWatch m_screenSaverTimer;
420#endif
421  CStopWatch m_restartPlayerTimer;
422  CStopWatch m_frameTime;
423  CStopWatch m_navigationTimer;
424  CStopWatch m_slowTimer;
425  CStopWatch m_shutdownTimer;
426  XbmcThreads::EndTime m_guiRefreshTimer;
427
428  bool m_bInhibitIdleShutdown = false;
429  bool m_bInhibitScreenSaver = false;
430
431  bool m_dpmsIsActive = false;
432  bool m_dpmsIsManual = false;
433
434  CFileItemPtr m_itemCurrentFile;
435
436  std::string m_prevMedia;
437  std::thread::id m_threadID;       // application thread ID.  Used in applicationMessenger to know where we are firing a thread with delay from.
438  bool m_bInitializing = true;
439  bool m_bPlatformDirectories = true;
440
441  int m_nextPlaylistItem = -1;
442
443  unsigned int m_lastRenderTime = 0;
444  bool m_skipGuiRender = false;
445
446  bool m_bStandalone = false;
447  bool m_bTestMode = false;
448  bool m_bSystemScreenSaverEnable = false;
449
450  std::unique_ptr<MUSIC_INFO::CMusicInfoScanner> m_musicInfoScanner;
451
452  bool m_muted = false;
453  float m_volumeLevel = VOLUME_MAXIMUM;
454
455  void Mute();
456  void UnMute();
457
458  void SetHardwareVolume(float hardwareVolume);
459
460  void VolumeChanged();
461
462  bool PlayStack(CFileItem& item, bool bRestart);
463
464  float NavigationIdleTime();
465  void HandlePortEvents();
466
467  /*! \brief Helper method to determine how to handle TMSG_SHUTDOWN
468  */
469  void HandleShutdownMessage();
470
471  CInertialScrollingHandler *m_pInertialScrollingHandler;
472
473  ReplayGainSettings m_replayGainSettings;
474  std::vector<IActionListener *> m_actionListeners;
475  std::vector<std::string> m_incompatibleAddons;  /*!< Result of addon migration */
476
477private:
478  mutable CCriticalSection m_critSection; /*!< critical section for all changes to this class, except for changes to triggers */
479
480  CCriticalSection m_frameMoveGuard;              /*!< critical section for synchronizing GUI actions from inside and outside (python) */
481  std::atomic_uint m_WaitingExternalCalls;        /*!< counts threads wich are waiting to be processed in FrameMove */
482  unsigned int m_ProcessedExternalCalls = 0;          /*!< counts calls wich are processed during one "door open" cycle in FrameMove */
483  unsigned int m_ProcessedExternalDecay = 0;      /*!< counts to close door after a few frames of no python activity */
484  CApplicationPlayer m_appPlayer;
485  CEvent m_playerEvent;
486  CApplicationStackHelper m_stackHelper;
487};
488
489XBMC_GLOBAL_REF(CApplication,g_application);
490#define g_application XBMC_GLOBAL_USE(CApplication)