[video] fix selection after changing video or extra art
[xbmc.git] / xbmc / pvr / PVRManager.h
blob92970351112d6347619c8a2260a4fc992800b688
1 /*
2 * Copyright (C) 2012-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #pragma once
11 #include "addons/kodi-dev-kit/include/kodi/c-api/addon-instance/pvr/pvr_general.h"
12 #include "interfaces/IAnnouncer.h"
13 #include "pvr/PVRComponentRegistration.h"
14 #include "pvr/guilib/PVRGUIActionListener.h"
15 #include "pvr/settings/PVRSettings.h"
16 #include "threads/CriticalSection.h"
17 #include "threads/Event.h"
18 #include "threads/Thread.h"
19 #include "utils/EventStream.h"
21 #include <memory>
22 #include <string>
23 #include <vector>
25 class CFileItem;
26 class CStopWatch;
28 namespace PVR
30 class CPVRChannel;
31 class CPVRChannelGroup;
32 class CPVRChannelGroupsContainer;
33 class CPVRProviders;
34 class CPVRClient;
35 class CPVRClients;
36 class CPVRDatabase;
37 class CPVRGUIInfo;
38 class CPVRGUIProgressHandler;
39 class CPVRManagerJobQueue;
40 class CPVRPlaybackState;
41 class CPVRRecording;
42 class CPVRRecordings;
43 class CPVRTimers;
44 class CPVREpgContainer;
45 class CPVREpgInfoTag;
47 enum class PVREvent
49 // PVR Manager states
50 ManagerError = 0,
51 ManagerStopped,
52 ManagerStarting,
53 ManagerStopping,
54 ManagerInterrupted,
55 ManagerStarted,
57 // Channel events
58 ChannelPlaybackStopped,
60 // Channel group events
61 ChannelGroup,
62 ChannelGroupInvalidated,
63 ChannelGroupsInvalidated,
65 // Recording events
66 RecordingsInvalidated,
68 // Timer events
69 AnnounceReminder,
70 Timers,
71 TimersInvalidated,
73 // Client events
74 ClientsPrioritiesInvalidated,
75 ClientsInvalidated,
77 // EPG events
78 Epg,
79 EpgActiveItem,
80 EpgContainer,
81 EpgItemUpdate,
82 EpgUpdatePending,
83 EpgDeleted,
85 // Saved searches events
86 SavedSearchesInvalidated,
88 // Item events
89 CurrentItem,
91 // System events
92 SystemSleep,
93 SystemWake,
96 class CPVRManager : private CThread, public ANNOUNCEMENT::IAnnouncer
98 public:
99 /*!
100 * @brief Create a new CPVRManager instance, which handles all PVR related operations in XBMC.
102 CPVRManager();
105 * @brief Stop the PVRManager and destroy all objects it created.
107 ~CPVRManager() override;
109 void Announce(ANNOUNCEMENT::AnnouncementFlag flag,
110 const std::string& sender,
111 const std::string& message,
112 const CVariant& data) override;
115 * @brief Get a PVR component.
116 * @return The component.
118 template<class T>
119 T& Get()
121 return *m_components->GetComponent<T>();
125 * @brief Get the providers container.
126 * @return The providers container.
128 std::shared_ptr<CPVRProviders> Providers() const;
131 * @brief Get the channel groups container.
132 * @return The groups container.
134 std::shared_ptr<CPVRChannelGroupsContainer> ChannelGroups() const;
137 * @brief Get the recordings container.
138 * @return The recordings container.
140 std::shared_ptr<CPVRRecordings> Recordings() const;
143 * @brief Get the timers container.
144 * @return The timers container.
146 std::shared_ptr<CPVRTimers> Timers() const;
149 * @brief Get the timers container.
150 * @return The timers container.
152 std::shared_ptr<CPVRClients> Clients() const;
155 * @brief Get the instance of a client that matches the given item.
156 * @param item The item containing a PVR recording, a PVR channel, a PVR timer or a PVR EPG event.
157 * @return the requested client on success, nullptr otherwise.
159 std::shared_ptr<CPVRClient> GetClient(const CFileItem& item) const;
162 * @brief Get the instance of a client that matches the given id.
163 * @param iClientId The id of a PVR client.
164 * @return the requested client on success, nullptr otherwise.
166 std::shared_ptr<CPVRClient> GetClient(int iClientId) const;
169 * @brief Get access to the pvr playback state.
170 * @return The playback state.
172 std::shared_ptr<CPVRPlaybackState> PlaybackState() const;
175 * @brief Get access to the epg container.
176 * @return The epg container.
178 CPVREpgContainer& EpgContainer();
181 * @brief Init PVRManager.
183 void Init();
186 * @brief Start the PVRManager, which loads all PVR data and starts some threads to update the PVR data.
188 void Start();
191 * @brief Stop PVRManager.
193 void Stop(bool bRestart = false);
196 * @brief Stop PVRManager, unload data.
198 void Unload();
201 * @brief Deinit PVRManager, unload data, unload addons.
203 void Deinit();
206 * @brief Propagate event on system sleep
208 void OnSleep();
211 * @brief Propagate event on system wake
213 void OnWake();
216 * @brief Get the TV database.
217 * @return The TV database.
219 std::shared_ptr<CPVRDatabase> GetTVDatabase() const;
222 * @brief Check whether the PVRManager has fully started.
223 * @return True if started, false otherwise.
225 bool IsStarted() const { return GetState() == ManagerState::STATE_STARTED; }
228 * @brief Inform PVR manager that playback of an item just started.
229 * @param item The item that started to play.
231 void OnPlaybackStarted(const CFileItem& item);
234 * @brief Inform PVR manager that playback of an item was stopped due to user interaction.
235 * @param item The item that stopped to play.
237 void OnPlaybackStopped(const CFileItem& item);
240 * @brief Inform PVR manager that playback of an item has stopped without user interaction.
241 * @param item The item that ended to play.
243 void OnPlaybackEnded(const CFileItem& item);
246 * @brief Let the background thread update the recordings list.
247 * @param clientId The id of the PVR client to update.
249 void TriggerRecordingsUpdate(int clientId);
250 void TriggerRecordingsUpdate();
253 * @brief Let the background thread update the size for any in progress recordings.
255 void TriggerRecordingsSizeInProgressUpdate();
258 * @brief Let the background thread update the timer list.
259 * @param clientId The id of the PVR client to update.
261 void TriggerTimersUpdate(int clientId);
262 void TriggerTimersUpdate();
265 * @brief Let the background thread update the channel list.
266 * @param clientId The id of the PVR client to update.
268 void TriggerChannelsUpdate(int clientId);
269 void TriggerChannelsUpdate();
272 * @brief Let the background thread update the provider list.
273 * @param clientId The id of the PVR client to update.
275 void TriggerProvidersUpdate(int clientId);
276 void TriggerProvidersUpdate();
279 * @brief Let the background thread update the channel groups list.
280 * @param clientId The id of the PVR client to update.
282 void TriggerChannelGroupsUpdate(int clientId);
283 void TriggerChannelGroupsUpdate();
286 * @brief Let the background thread search for all missing channel icons.
288 void TriggerSearchMissingChannelIcons();
291 * @brief Let the background thread erase stale texture db entries and image files.
293 void TriggerCleanupCachedImages();
296 * @brief Let the background thread search for missing channel icons for channels contained in the given group.
297 * @param group The channel group.
299 void TriggerSearchMissingChannelIcons(const std::shared_ptr<CPVRChannelGroup>& group);
302 * @brief Check whether names are still correct after the language settings changed.
304 void LocalizationChanged();
307 * @brief Check if parental lock is overridden at the given moment.
308 * @param channel The channel to check.
309 * @return True if parental lock is overridden, false otherwise.
311 bool IsParentalLocked(const std::shared_ptr<const CPVRChannel>& channel) const;
314 * @brief Check if parental lock is overridden at the given moment.
315 * @param epgTag The epg tag to check.
316 * @return True if parental lock is overridden, false otherwise.
318 bool IsParentalLocked(const std::shared_ptr<const CPVREpgInfoTag>& epgTag) const;
321 * @brief Restart the parental timer.
323 void RestartParentalTimer();
326 * @brief Signal a connection change of a client
328 void ConnectionStateChange(CPVRClient* client,
329 const std::string& connectString,
330 PVR_CONNECTION_STATE state,
331 const std::string& message);
334 * @brief Query the events available for CEventStream
336 CEventStream<PVREvent>& Events() { return m_events; }
339 * @brief Publish an event
340 * @param state the event
342 void PublishEvent(PVREvent state);
344 protected:
346 * @brief PVR update and control thread.
348 void Process() override;
350 private:
352 * @brief Executes "pvrpowermanagement.setwakeupcmd"
354 bool SetWakeupCommand();
356 enum class ManagerState
358 STATE_ERROR = 0,
359 STATE_STOPPED,
360 STATE_STARTING,
361 STATE_STOPPING,
362 STATE_INTERRUPTED,
363 STATE_STARTED
367 * @return True while the PVRManager is initialising.
369 bool IsInitialising() const { return GetState() == ManagerState::STATE_STARTING; }
372 * @brief Check whether the PVRManager has been stopped.
373 * @return True if stopped, false otherwise.
375 bool IsStopped() const { return GetState() == ManagerState::STATE_STOPPED; }
378 * @brief Get the current state of the PVR manager.
379 * @return the state.
381 ManagerState GetState() const;
384 * @brief Set the current state of the PVR manager.
385 * @param state the new state.
387 void SetState(ManagerState state);
390 * @brief Wait until at least one client is up. Update all data from database and the given PVR clients.
391 * @param stateToCheck Required state of the PVR manager while this method gets called.
393 void UpdateComponents(ManagerState stateToCheck);
396 * @brief Update all data from database and the given PVR clients.
397 * @param stateToCheck Required state of the PVR manager while this method gets called.
398 * @param progressHandler The progress handler to use for showing the different stages.
399 * @return True if at least one client is known and successfully loaded, false otherwise.
401 bool UpdateComponents(ManagerState stateToCheck,
402 const std::unique_ptr<CPVRGUIProgressHandler>& progressHandler);
405 * @brief Unload all PVR data (recordings, timers, channelgroups).
407 void UnloadComponents();
410 * @brief Check whether the given client id belongs to a known client.
411 * @return True if the client is known, false otherwise.
413 bool IsKnownClient(int clientID) const;
416 * @brief Reset all properties.
418 void ResetProperties();
421 * @brief Destroy PVRManager's objects.
423 void Clear();
426 * @brief Continue playback on the last played channel.
428 void TriggerPlayChannelOnStartup();
430 bool IsCurrentlyParentalLocked(const std::shared_ptr<const CPVRChannel>& channel,
431 bool bGenerallyLocked) const;
433 CEventSource<PVREvent> m_events;
435 /** @name containers */
436 //@{
437 std::shared_ptr<CPVRProviders> m_providers; /*!< pointer to the providers container */
438 std::shared_ptr<CPVRChannelGroupsContainer>
439 m_channelGroups; /*!< pointer to the channel groups container */
440 std::shared_ptr<CPVRRecordings> m_recordings; /*!< pointer to the recordings container */
441 std::shared_ptr<CPVRTimers> m_timers; /*!< pointer to the timers container */
442 std::shared_ptr<CPVRClients> m_addons; /*!< pointer to the pvr addon container */
443 std::unique_ptr<CPVRGUIInfo> m_guiInfo; /*!< pointer to the guiinfo data */
444 std::shared_ptr<CPVRComponentRegistration> m_components; /*!< pointer to the PVR components */
445 std::unique_ptr<CPVREpgContainer> m_epgContainer; /*!< the epg container */
446 //@}
448 std::vector<std::shared_ptr<CPVRClient>> m_knownClients; /*!< vector with all known clients */
449 std::unique_ptr<CPVRManagerJobQueue> m_pendingUpdates; /*!< vector of pending pvr updates */
450 std::shared_ptr<CPVRDatabase> m_database; /*!< the database for all PVR related data */
451 mutable CCriticalSection
452 m_critSection; /*!< critical section for all changes to this class, except for changes to triggers */
453 bool m_bFirstStart = true; /*!< true when the PVR manager was started first, false otherwise */
455 mutable CCriticalSection m_managerStateMutex;
456 ManagerState m_managerState = ManagerState::STATE_STOPPED;
457 std::unique_ptr<CStopWatch> m_parentalTimer;
459 CCriticalSection
460 m_startStopMutex; // mutex for protecting pvr manager's start/restart/stop sequence */
462 const std::shared_ptr<CPVRPlaybackState> m_playbackState;
463 CPVRGUIActionListener m_actionListener;
464 CPVRSettings m_settings;
466 } // namespace PVR