MythTV  master
eithelper.h
Go to the documentation of this file.
1 // -*- Mode: c++ -*-
2 
3 #ifndef EIT_HELPER_H
4 #define EIT_HELPER_H
5 
6 // C+ headers
7 #include <cstdint>
8 #include <ctime>
9 #include <utility>
10 
11 // Qt includes
12 #include <QDateTime>
13 #include <QMap>
14 #include <QMutex>
15 #include <QObject>
16 #include <QString>
17 
18 // MythTV includes
19 #include "mythchrono.h"
20 #include "mythdeque.h"
21 #include "mpegtables.h" // for GPS_LEAP_SECONDS
22 
23 class MSqlQuery;
24 
25 // An entry from the EIT table containing event details.
26 class ATSCEvent
27 {
28  public:
29  ATSCEvent(uint a, uint b, uint c, QString d,
30  const unsigned char *e, uint f)
31  : m_startTime(a), m_length(b), m_etm(c), m_descLength(f),
32  m_title(std::move(d)), m_desc(e),
33  m_scanTime(SystemClock::now()) {}
34 
35  bool IsStale() const {
36  // The minimum recommended repetition time for EIT events according to
37  // http://atsc.org/wp-content/uploads/2015/03/Program-and-system-information-protocol-implementation-guidelines-for-broadcaster.pdf
38  // is one minute. Consider any EIT event seen > 2 minutes in the past as stale.
39  return m_scanTime + 2min < SystemClock::now();
40  }
41 
42  uint32_t m_startTime;
43  uint32_t m_length;
44  uint32_t m_etm;
45  uint32_t m_descLength;
46  QString m_title;
47  const unsigned char *m_desc {nullptr};
48 
49  private:
50  // The time the event was created.
52 };
53 
54 // An entry from the ETT table containing description text for an event.
55 class ATSCEtt
56 {
57  public:
58  explicit ATSCEtt(QString text) :
59  m_ett_text(std::move(text)), m_scanTime(SystemClock::now()) {}
60 
61  bool IsStale() const {
62  // The minimum recommended repetition time for ETT events according to
63  // http://atsc.org/wp-content/uploads/2015/03/Program-and-system-information-protocol-implementation-guidelines-for-broadcaster.pdf
64  // is one minute. Consider any ETT event seen > 2 minutes in the past as stale.
65  return m_scanTime + 2min < SystemClock::now();
66  }
67 
68  QString m_ett_text;
69 
70  private:
71  // The time the ETT was created.
73 };
74 
75 using EventIDToATSCEvent = QMap<uint,ATSCEvent> ;
76 using EventIDToETT = QMap<uint,ATSCEtt>;
77 using ATSCSRCToEvents = QMap<uint,EventIDToATSCEvent>;
78 using ServiceToChanID = QMap<uint64_t,uint>;
79 
80 using FixupKey = uint64_t;
81 using FixupValue = uint64_t;
82 using FixupMap = QMap<FixupKey, FixupValue>;
83 
84 class DBEventEIT;
85 class EITFixUp;
86 class EITCache;
87 
89 class ExtendedTextTable;
92 
93 class EITHelper
94 {
95  public:
96  explicit EITHelper(uint cardnum);
97  EITHelper &operator=(const EITHelper &) = delete;
98  EITHelper(const EITHelper& rhs);
99  virtual ~EITHelper(void);
100 
101  uint GetListSize(void) const;
102  uint ProcessEvents(void);
103  bool EventQueueFull(void) const;
104 
105  uint GetGPSOffset(void) const { return (uint) (0 - m_gpsOffset); }
106 
107  void SetChannelID(uint channelid);
108  void SetGPSOffset(uint gps_offset) { m_gpsOffset = 0 - gps_offset; }
109  void SetFixup(uint atsc_major, uint atsc_minor, FixupValue eitfixup);
110  void SetLanguagePreferences(const QStringList &langPref);
111  void SetSourceID(uint sourceid);
112  void RescheduleRecordings(void);
113 
114 #ifdef USING_BACKEND
115  void AddEIT(uint atsc_major, uint atsc_minor,
116  const EventInformationTable *eit);
117  void AddETT(uint atsc_major, uint atsc_minor,
118  const ExtendedTextTable *ett);
119  void AddEIT(const DVBEventInformationTable *eit);
120  void AddEIT(const PremiereContentInformationTable *cit);
121 #else // if !USING_BACKEND
122  void AddEIT(uint /*atsc_major*/, uint /*atsc_minor*/, const EventInformationTable */*eit*/) {}
123  void AddETT(uint /*atsc_major*/, uint /*atsc_minor*/, const ExtendedTextTable */*ett*/) {}
124  void AddEIT(const DVBEventInformationTable */*eit*/) {}
125  void AddEIT(const PremiereContentInformationTable */*cit*/) {}
126 #endif // !USING_BACKEND
127 
128  // EIT cache handling
129  static void PruneEITCache(uint timestamp);
130  static void WriteEITCache(void);
131 
132  private:
133  uint GetChanID(uint atsc_major, uint atsc_minor); // Only ATSC
134  uint GetChanID(uint serviceid, uint networkid, uint tsid); // Only DVB
135  uint GetChanID(uint program_number); // Any DTV
136 
137  void CompleteEvent(uint atsc_major, uint atsc_minor, // Only ATSC
138  const ATSCEvent &event,
139  const QString &ett);
140 
141  mutable QMutex m_eitListLock;
143 
145 
147 
148  // Carry some values to optimize channel lookup and reschedules
149  uint m_cardnum {0}; // Card ID
150  uint m_sourceid {0}; // Video source ID
151  uint m_channelid {0}; // Channel ID
152  QDateTime m_maxStarttime; // Latest starttime of changed events
153  bool m_seenEITother {false}; // If false we only reschedule the active mplex
154 
157 
159 
160  QMap<uint,uint> m_languagePreferences;
161 
162  static const uint kChunkSize; // Maximum number of DB inserts per ProcessEvents call
163  static const uint kMaxSize; // Maximum number of events waiting to be processed
164 };
165 
166 #endif // EIT_HELPER_H
FixupKey
uint64_t FixupKey
Definition: eithelper.h:80
EITHelper::operator=
EITHelper & operator=(const EITHelper &)=delete
EITHelper::m_dbEvents
MythDeque< DBEventEIT * > m_dbEvents
Definition: eithelper.h:158
MSqlQuery
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:124
EITHelper::WriteEITCache
static void WriteEITCache(void)
Definition: eithelper.cpp:762
EITFixUp
EIT Fix Up Functions.
Definition: eitfixup.h:11
EITHelper::kMaxSize
static const uint kMaxSize
Definition: eithelper.h:163
ATSCEvent::ATSCEvent
ATSCEvent(uint a, uint b, uint c, QString d, const unsigned char *e, uint f)
Definition: eithelper.h:29
EITHelper::SetFixup
void SetFixup(uint atsc_major, uint atsc_minor, FixupValue eitfixup)
Definition: eithelper.cpp:115
DVBEventInformationTable
Definition: dvbtables.h:295
EITHelper::m_incompleteEvents
ATSCSRCToEvents m_incompleteEvents
Definition: eithelper.h:156
PremiereContentInformationTable
Definition: premieretables.h:10
ATSCEtt
Definition: eithelper.h:55
EITHelper::AddETT
void AddETT(uint atsc_major, uint atsc_minor, const ExtendedTextTable *ett)
Definition: eithelper.cpp:190
EITHelper::SetGPSOffset
void SetGPSOffset(uint gps_offset)
Definition: eithelper.h:108
EITHelper::SetSourceID
void SetSourceID(uint sourceid)
Definition: eithelper.cpp:139
EITHelper::m_sourceid
uint m_sourceid
Definition: eithelper.h:150
SystemTime
std::chrono::time_point< SystemClock > SystemTime
Definition: mythchrono.h:67
EITHelper::m_eitListLock
QMutex m_eitListLock
Definition: eithelper.h:141
FixupValue
uint64_t FixupValue
Definition: eithelper.h:81
ATSCEvent::m_scanTime
SystemTime m_scanTime
Definition: eithelper.h:51
ATSCEvent::m_length
uint32_t m_length
Definition: eithelper.h:43
ATSCEtt::m_ett_text
QString m_ett_text
Definition: eithelper.h:68
EventIDToATSCEvent
QMap< uint, ATSCEvent > EventIDToATSCEvent
Definition: eithelper.h:75
ServiceToChanID
QMap< uint64_t, uint > ServiceToChanID
Definition: eithelper.h:78
EITHelper::m_channelid
uint m_channelid
Definition: eithelper.h:151
ATSCEvent::m_descLength
uint32_t m_descLength
Definition: eithelper.h:45
mythdeque.h
EITHelper::m_fixup
FixupMap m_fixup
Definition: eithelper.h:155
EITHelper::AddEIT
void AddEIT(uint atsc_major, uint atsc_minor, const EventInformationTable *eit)
Definition: eithelper.cpp:151
EITHelper::RescheduleRecordings
void RescheduleRecordings(void)
Tells scheduler about programming changes.
Definition: eithelper.cpp:1407
EITHelper::m_cardnum
uint m_cardnum
Definition: eithelper.h:149
EITHelper::m_languagePreferences
QMap< uint, uint > m_languagePreferences
Definition: eithelper.h:160
ATSCEvent::IsStale
bool IsStale() const
Definition: eithelper.h:35
EITHelper::SetChannelID
void SetChannelID(uint channelid)
Definition: eithelper.cpp:145
EITHelper::m_gpsOffset
int m_gpsOffset
Definition: eithelper.h:146
mpegtables.h
EITHelper::~EITHelper
virtual ~EITHelper(void)
Definition: eithelper.cpp:45
ATSCEtt::ATSCEtt
ATSCEtt(QString text)
Definition: eithelper.h:58
EventInformationTable
EventInformationTables contain program titles, start times, and channel information.
Definition: atsctables.h:520
ATSCEvent::m_title
QString m_title
Definition: eithelper.h:46
EITHelper::CompleteEvent
void CompleteEvent(uint atsc_major, uint atsc_minor, const ATSCEvent &event, const QString &ett)
Definition: eithelper.cpp:771
uint
unsigned int uint
Definition: compat.h:140
EITHelper
Definition: eithelper.h:93
ATSCEtt::IsStale
bool IsStale() const
Definition: eithelper.h:61
ATSCEvent::m_desc
const unsigned char * m_desc
Definition: eithelper.h:47
SystemClock
std::chrono::system_clock SystemClock
Definition: mythchrono.h:66
EITHelper::GetChanID
uint GetChanID(uint atsc_major, uint atsc_minor)
Definition: eithelper.cpp:809
EventIDToETT
QMap< uint, ATSCEtt > EventIDToETT
Definition: eithelper.h:76
ATSCEvent::m_etm
uint32_t m_etm
Definition: eithelper.h:44
EITHelper::GetGPSOffset
uint GetGPSOffset(void) const
Definition: eithelper.h:105
EITHelper::SetLanguagePreferences
void SetLanguagePreferences(const QStringList &langPref)
Definition: eithelper.cpp:122
EITHelper::m_seenEITother
bool m_seenEITother
Definition: eithelper.h:153
DBEventEIT
Definition: programdata.h:177
EITHelper::ProcessEvents
uint ProcessEvents(void)
Get events from queue and insert into DB after processing.
Definition: eithelper.cpp:73
EITHelper::EITHelper
EITHelper(uint cardnum)
Definition: eithelper.cpp:39
MythDeque< DBEventEIT * >
std
Definition: mythchrono.h:23
EITHelper::m_srvToChanid
ServiceToChanID m_srvToChanid
Definition: eithelper.h:142
EITHelper::kChunkSize
static const uint kChunkSize
Definition: eithelper.h:162
mythchrono.h
EITHelper::GetListSize
uint GetListSize(void) const
Definition: eithelper.cpp:52
EITHelper::PruneEITCache
static void PruneEITCache(uint timestamp)
Definition: eithelper.cpp:757
ExtendedTextTable
ExtendedTextTable contain additional text not contained in EventInformationTables.
Definition: atsctables.h:620
ATSCEvent::m_startTime
uint32_t m_startTime
Definition: eithelper.h:42
d
static const iso6937table * d
Definition: iso6937tables.cpp:1025
ATSCEvent
Definition: eithelper.h:26
FixupMap
QMap< FixupKey, FixupValue > FixupMap
Definition: eithelper.h:82
EITHelper::s_eitCache
static EITCache * s_eitCache
Definition: eithelper.h:144
ATSCEtt::m_scanTime
SystemTime m_scanTime
Definition: eithelper.h:72
ATSCSRCToEvents
QMap< uint, EventIDToATSCEvent > ATSCSRCToEvents
Definition: eithelper.h:77
EITHelper::m_maxStarttime
QDateTime m_maxStarttime
Definition: eithelper.h:152
EITCache
Definition: eitcache.h:22
GPS_LEAP_SECONDS
#define GPS_LEAP_SECONDS
Leap seconds as of June 30th, 2012.
Definition: mpegtables.h:34
EITHelper::EventQueueFull
bool EventQueueFull(void) const
Definition: eithelper.cpp:58