xbmc
EpgContainer.h
1 /*
2  * Copyright (C) 2012-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 "addons/kodi-dev-kit/include/kodi/c-api/addon-instance/pvr/pvr_epg.h"
12 #include "pvr/settings/PVRSettings.h"
13 #include "threads/CriticalSection.h"
14 #include "threads/Event.h"
15 #include "threads/Thread.h"
16 #include "utils/EventStream.h"
17 
18 #include <atomic>
19 #include <list>
20 #include <map>
21 #include <memory>
22 #include <string>
23 #include <utility>
24 #include <vector>
25 
26 class CDateTime;
27 
28 namespace PVR
29 {
30  class CEpgUpdateRequest;
31  class CEpgTagStateChange;
32  class CPVREpg;
33  class CPVREpgChannelData;
34  class CPVREpgDatabase;
35  class CPVREpgInfoTag;
36  class CPVREpgSearchFilter;
37 
38  enum class PVREvent;
39 
40  struct PVREpgSearchData;
41 
42  class CPVREpgContainer : private CThread
43  {
44  friend class CPVREpgDatabase;
45 
46  public:
47  CPVREpgContainer() = delete;
48 
52  explicit CPVREpgContainer(CEventSource<PVREvent>& eventSource);
53 
57  ~CPVREpgContainer() override;
58 
63  std::shared_ptr<CPVREpgDatabase> GetEpgDatabase() const;
64 
68  void Start();
69 
73  void Stop();
74 
79  bool Load();
80 
84  void Unload();
85 
90  bool IsStarted() const;
91 
97  bool QueueDeleteEpgs(const std::vector<std::shared_ptr<CPVREpg>>& epgs);
98 
103  void Notify(const PVREvent& event);
104 
112  std::shared_ptr<CPVREpg> CreateChannelEpg(int iEpgId, const std::string& strScraperName, const std::shared_ptr<CPVREpgChannelData>& channelData);
113 
118  std::pair<CDateTime, CDateTime> GetFirstAndLastEPGDate() const;
119 
124  std::vector<std::shared_ptr<CPVREpg>> GetAllEpgs() const;
125 
131  std::shared_ptr<CPVREpg> GetById(int iEpgId) const;
132 
139  std::shared_ptr<CPVREpg> GetByChannelUid(int iClientId, int iChannelUid) const;
140 
147  std::shared_ptr<CPVREpgInfoTag> GetTagById(const std::shared_ptr<CPVREpg>& epg, unsigned int iBroadcastId) const;
148 
154  std::shared_ptr<CPVREpgInfoTag> GetTagByDatabaseId(int iDatabaseId) const;
155 
161  std::vector<std::shared_ptr<CPVREpgInfoTag>> GetTags(const PVREpgSearchData& searchData) const;
162 
167  void SetHasPendingUpdates(bool bHasPendingUpdates = true);
168 
174  void UpdateRequest(int iClientID, int iUniqueChannelID);
175 
181  void UpdateFromClient(const std::shared_ptr<CPVREpgInfoTag>& tag, EPG_EVENT_STATE eNewState);
182 
187  int GetPastDaysToDisplay() const;
188 
193  int GetFutureDaysToDisplay() const;
194 
198  void OnPlaybackStarted();
199 
203  void OnPlaybackStopped();
204 
208  void OnSystemSleep();
209 
213  void OnSystemWake();
214 
219  int CleanupCachedImages();
220 
226  std::vector<std::shared_ptr<CPVREpgSearchFilter>> GetSavedSearches(bool bRadio);
227 
234  std::shared_ptr<CPVREpgSearchFilter> GetSavedSearchById(bool bRadio, int iId);
235 
242 
248  bool UpdateSavedSearchLastExecuted(const CPVREpgSearchFilter& epgSearch);
249 
255  bool DeleteSavedSearch(const CPVREpgSearchFilter& search);
256 
257  private:
262  bool CheckPlayingEvents();
263 
268  int NextEpgId();
269 
273  void WaitForUpdateFinish();
274 
281  bool PersistAll(unsigned int iMaxTimeslice) const;
282 
287  bool RemoveOldEntries();
288 
294  bool UpdateEPG(bool bOnlyPending = false);
295 
300  bool InterruptUpdate() const;
301 
305  void Process() override;
306 
310  void LoadFromDatabase();
311 
316  void InsertFromDB(const std::shared_ptr<CPVREpg>& newEpg);
317 
324  bool QueueDeleteEpg(const std::shared_ptr<CPVREpg>& epg,
325  const std::shared_ptr<CPVREpgDatabase>& database);
326 
327  std::shared_ptr<CPVREpgDatabase> m_database;
329  bool m_bIsUpdating = false;
330  std::atomic<bool> m_bIsInitialising = {
331  true};
332  bool m_bStarted = false;
333  bool m_bLoaded = false;
334  bool m_bPreventUpdates = false;
335  bool m_bPlaying = false;
336  int m_pendingUpdates = 0;
337  time_t m_iLastEpgCleanup = 0;
338  time_t m_iNextEpgUpdate = 0;
339  time_t m_iNextEpgActiveTagCheck = 0;
340  int m_iNextEpgId = 0;
342  std::map<int, std::shared_ptr<CPVREpg>> m_epgIdToEpgMap;
343  std::map<std::pair<int, int>, std::shared_ptr<CPVREpg>> m_channelUidToEpgMap;
345  mutable CCriticalSection m_critSection;
346  CEvent m_updateEvent;
348  std::list<CEpgUpdateRequest> m_updateRequests;
349  CCriticalSection m_updateRequestsLock;
351  std::list<CEpgTagStateChange> m_epgTagChanges;
352  CCriticalSection m_epgTagChangesLock;
354  bool m_bUpdateNotificationPending = false;
355  CPVRSettings m_settings;
356  CEventSource<PVREvent>& m_events;
357 
358  std::atomic<bool> m_bSuspended = {false};
359  };
360 }
This is an Event class built from a ConditionVariable.
Definition: Event.h:35
bool UpdateSavedSearchLastExecuted(const CPVREpgSearchFilter &epgSearch)
Update time last executed for the given search.
Definition: EpgContainer.cpp:996
int GetFutureDaysToDisplay() const
Get the number of future days to show in the guide and to import from backends.
Definition: EpgContainer.cpp:908
bool IsStarted() const
Check whether the EpgContainer has fully started.
Definition: EpgContainer.cpp:116
Definition: Thread.h:44
Definition: EpgSearchFilter.h:23
Definition: EpgContainer.h:42
Definition: EpgDatabase.h:31
Definition: ContextMenuManager.h:24
void Unload()
unload all EPG data.
Definition: EpgContainer.cpp:158
void Notify(const PVREvent &event)
CEventStream callback for PVR events.
Definition: EpgContainer.cpp:197
std::vector< std::shared_ptr< CPVREpgInfoTag > > GetTags(const PVREpgSearchData &searchData) const
Get all EPG tags matching the given search criteria.
Definition: EpgContainer.cpp:525
std::shared_ptr< CPVREpgSearchFilter > GetSavedSearchById(bool bRadio, int iId)
Get the saved search matching the given id.
Definition: EpgContainer.cpp:967
bool PersistSavedSearch(CPVREpgSearchFilter &search)
Persist a saved search in the database.
Definition: EpgContainer.cpp:979
void Start()
Start the EPG update thread.
Definition: EpgContainer.cpp:128
std::shared_ptr< CPVREpgDatabase > GetEpgDatabase() const
Get a pointer to the database instance.
Definition: EpgContainer.cpp:106
std::shared_ptr< CPVREpg > CreateChannelEpg(int iEpgId, const std::string &strScraperName, const std::shared_ptr< CPVREpgChannelData > &channelData)
Create the EPg for a given channel.
Definition: EpgContainer.cpp:560
void OnPlaybackStopped()
Inform the epg container that playback of an item was stopped due to user interaction.
Definition: EpgContainer.cpp:919
void SetHasPendingUpdates(bool bHasPendingUpdates=true)
Notify EPG container that there are pending manual EPG updates.
Definition: EpgContainer.cpp:882
void UpdateFromClient(const std::shared_ptr< CPVREpgInfoTag > &tag, EPG_EVENT_STATE eNewState)
A client announced an updated epg tag for a channel.
Definition: EpgContainer.cpp:897
bool QueueDeleteEpgs(const std::vector< std::shared_ptr< CPVREpg >> &epgs)
Queue the deletion of the given EPG tables from this container.
Definition: EpgContainer.cpp:618
void Stop()
Stop the EPG update thread.
Definition: EpgContainer.cpp:143
DateTime class, which uses FileTime as it&#39;s base.
Definition: XBDateTime.h:63
std::shared_ptr< CPVREpgInfoTag > GetTagByDatabaseId(int iDatabaseId) const
Get the EPG event with the given database id.
Definition: EpgContainer.cpp:504
void OnSystemSleep()
Inform the epg container that the system is going to sleep.
Definition: EpgContainer.cpp:925
void UpdateRequest(int iClientID, int iUniqueChannelID)
A client triggered an epg update request for a channel.
Definition: EpgContainer.cpp:891
~CPVREpgContainer() override
Destroy this instance.
Definition: EpgContainer.cpp:100
void OnSystemWake()
Inform the epg container that the system gets awake from sleep.
Definition: EpgContainer.cpp:930
Definition: EpgSearchData.h:20
std::vector< std::shared_ptr< CPVREpgSearchFilter > > GetSavedSearches(bool bRadio)
Get all saved searches from the database.
Definition: EpgContainer.cpp:955
bool DeleteSavedSearch(const CPVREpgSearchFilter &search)
Delete a saved search from the database.
Definition: EpgContainer.cpp:1008
std::pair< CDateTime, CDateTime > GetFirstAndLastEPGDate() const
Get the start and end time across all EPGs.
Definition: EpgContainer.cpp:811
void OnPlaybackStarted()
Inform the epg container that playback of an item just started.
Definition: EpgContainer.cpp:913
std::shared_ptr< CPVREpg > GetById(int iEpgId) const
Get an EPG given its ID.
Definition: EpgContainer.cpp:459
std::shared_ptr< CPVREpg > GetByChannelUid(int iClientId, int iChannelUid) const
Get an EPG given its client id and channel uid.
Definition: EpgContainer.cpp:474
bool Load()
(re)load EPG data.
Definition: EpgContainer.cpp:153
EPG_EVENT_STATE
Definition: pvr_epg.h:601
std::shared_ptr< CPVREpgInfoTag > GetTagById(const std::shared_ptr< CPVREpg > &epg, unsigned int iBroadcastId) const
Get the EPG event with the given event id.
Definition: EpgContainer.cpp:489
int CleanupCachedImages()
Erase stale texture db entries and image files.
Definition: EpgContainer.cpp:935
Definition: PVRSettings.h:27
std::vector< std::shared_ptr< CPVREpg > > GetAllEpgs() const
Get all EPGs.
Definition: EpgContainer.cpp:448
int GetPastDaysToDisplay() const
Get the number of past days to show in the guide and to import from backends.
Definition: EpgContainer.cpp:903