MythTV  master
eitscanner.cpp
Go to the documentation of this file.
1 // -*- Mode: c++ -*-
2 
3 // POSIX headers
4 #include <sys/time.h>
5 #include "compat.h"
6 
7 #include <cstdlib>
8 
9 #include "scheduledrecording.h"
10 #include "channelbase.h"
11 #include "channelutil.h"
12 #include "mythlogging.h"
13 #include "eitscanner.h"
14 #include "eithelper.h"
15 #include "mythtimer.h"
16 #include "mythdate.h"
17 #include "mthread.h"
18 #include "iso639.h"
19 #include "mythdb.h"
20 #include "tv_rec.h"
21 
22 #define LOC QString("EITScanner: ")
23 #define LOC_ID QString("EITScanner[%1]: ").arg(m_cardnum)
24 
33  : m_eitHelper(new EITHelper()), m_eventThread(new MThread("EIT", this)),
34  m_cardnum(cardnum)
35 {
36  QStringList langPref = iso639_get_language_list();
38 
39  m_eventThread->start(QThread::IdlePriority);
40 }
41 
43 {
45  if (!m_exitThread)
46  {
47  m_lock.lock();
48  m_exitThread = true;
49  m_exitThreadCond.wakeAll();
50  m_lock.unlock();
51  }
53  delete m_eventThread;
54  m_eventThread = nullptr;
55 
56  if (m_eitHelper)
57  {
58  delete m_eitHelper;
59  m_eitHelper = nullptr;
60  }
61 }
62 
66 void EITScanner::run(void)
67 {
68  static const uint sz[] = { 2000, 1800, 1600, 1400, 1200, };
69  static const float rt[] = { 0.0F, 0.2F, 0.4F, 0.6F, 0.8F, };
70 
71  m_lock.lock();
72 
73  MythTimer t;
74  uint eitCount = 0;
75 
76  while (!m_exitThread)
77  {
78  m_lock.unlock();
79  uint list_size = m_eitHelper->GetListSize();
80 
81  float rate = 1.0F;
82  for (uint i = 0; i < 5; i++)
83  {
84  if (list_size >= sz[i])
85  {
86  rate = rt[i];
87  break;
88  }
89  }
90 
91  m_lock.lock();
92  if (m_eitSource)
93  m_eitSource->SetEITRate(rate);
94  m_lock.unlock();
95 
96  if (list_size)
97  {
98  eitCount += m_eitHelper->ProcessEvents();
99  t.start();
100  }
101 
102  // Tell the scheduler to run if
103  // we are in passive scan
104  // and there have been updated events since the last scheduler run
105  // but not in the last 60 seconds
106  if (!m_activeScan && eitCount && (t.elapsed() > 60 * 1000))
107  {
108  LOG(VB_EIT, LOG_INFO,
109  LOC_ID + QString("Added %1 EIT Events").arg(eitCount));
110  eitCount = 0;
112  }
113 
114  // Is it time to move to the next transport in active scan?
116  {
117  // if there have been any new events, tell scheduler to run.
118  if (eitCount)
119  {
120  LOG(VB_EIT, LOG_INFO,
121  LOC_ID + QString("Added %1 EIT Events").arg(eitCount));
122  eitCount = 0;
124  }
125 
127  {
130  }
131 
132  if (!(*m_activeScanNextChan).isEmpty())
133  {
136  {
139  LOG(VB_EIT, LOG_INFO,
140  LOC_ID + QString("Now looking for EIT data on "
141  "multiplex of channel %1")
142  .arg(*m_activeScanNextChan));
143  }
144  }
145 
147  .addSecs(m_activeScanTrigTime);
148  if (!m_activeScanChannels.empty())
149  {
153  }
154 
155  // 24 hours ago
156 #if QT_VERSION < QT_VERSION_CHECK(5,8,0)
157  m_eitHelper->PruneEITCache(m_activeScanNextTrig.toTime_t() - 86400);
158 #else
159  m_eitHelper->PruneEITCache(m_activeScanNextTrig.toSecsSinceEpoch() - 86400);
160 #endif
161  }
162 
163  m_lock.lock();
165  m_exitThreadCond.wait(&m_lock, 400); // sleep up to 400 ms.
166 
168  {
169  m_activeScanStopped = true;
170  m_activeScanCond.wakeAll();
171  }
172  }
173 
174  if (eitCount) /* some events have been handled since the last schedule request */
175  {
177  }
178 
179  m_activeScanStopped = true;
180  m_activeScanCond.wakeAll();
181  m_lock.unlock();
182 }
183 
191 {
193 }
194 
200  EITSource *eitSource)
201 {
202  QMutexLocker locker(&m_lock);
203 
204  m_eitSource = eitSource;
205  m_channel = channel;
206 
208  m_eitSource->SetEITRate(1.0F);
211 
212  LOG(VB_EIT, LOG_INFO, LOC_ID + "Started passive scan.");
213 }
214 
219 {
220  QMutexLocker locker(&m_lock);
221 
222  if (m_eitSource)
223  {
224  m_eitSource->SetEITHelper(nullptr);
225  m_eitSource = nullptr;
226  }
227  m_channel = nullptr;
228 
232 }
233 
234 void EITScanner::StartActiveScan(TVRec *_rec, uint max_seconds_per_source)
235 {
236  rec = _rec;
237 
238  if (m_activeScanChannels.isEmpty())
239  {
240  // TODO get input name and use it in crawl.
241  MSqlQuery query(MSqlQuery::InitCon());
242  query.prepare(
243  "SELECT channum, MIN(chanid) "
244  "FROM channel, capturecard, videosource "
245  "WHERE deleted IS NULL AND "
246  " capturecard.sourceid = channel.sourceid AND "
247  " videosource.sourceid = channel.sourceid AND "
248  " channel.mplexid IS NOT NULL AND "
249  " visible = 1 AND "
250  " useonairguide = 1 AND "
251  " useeit = 1 AND "
252  " channum != '' AND "
253  " capturecard.cardid = :CARDID "
254  "GROUP BY mplexid "
255  "ORDER BY capturecard.sourceid, mplexid, "
256  " atsc_major_chan, atsc_minor_chan ");
257  query.bindValue(":CARDID", rec->GetInputId());
258 
259  if (!query.exec() || !query.isActive())
260  {
261  MythDB::DBError("EITScanner::StartActiveScan", query);
262  return;
263  }
264 
265  while (query.next())
266  m_activeScanChannels.push_back(query.value(0).toString());
267 
269  }
270 
271  LOG(VB_EIT, LOG_INFO, LOC_ID +
272  QString("StartActiveScan called with %1 multiplexes")
273  .arg(m_activeScanChannels.size()));
274 
275  // Start at a random channel. This is so that multiple cards with
276  // the same source don't all scan the same channels in the same
277  // order when the backend is first started up.
278  if (!m_activeScanChannels.empty())
279  {
280  // The start channel is random. From now on, start on the
281  // next channel. This makes sure the immediately following
282  // channels get scanned in a timely manner if we keep erroring
283  // out on the previous channel.
288 
290  m_activeScanTrigTime = max_seconds_per_source;
291  // Add a little randomness to trigger time so multiple
292  // cards will have a staggered channel changing time.
293  m_activeScanTrigTime += random() % 29;
294  m_activeScanStopped = false;
295  m_activeScan = true;
296  }
297 }
298 
300 {
301  QMutexLocker locker(&m_lock);
302 
303  m_activeScanStopped = false;
304  m_activeScan = false;
305  m_exitThreadCond.wakeAll();
306 
307  locker.unlock();
308  StopPassiveScan();
309  locker.relock();
310 
311  while (!m_activeScan && !m_activeScanStopped)
312  m_activeScanCond.wait(&m_lock, 100);
313 
314  rec = nullptr;
315 }
bool next(void)
Wrap QSqlQuery::next() so we can display the query results.
Definition: mythdbcon.cpp:782
void start(QThread::Priority=QThread::InheritPriority)
Tell MThread to start running the thread in the near future.
Definition: mthread.cpp:294
ChannelBase * m_channel
Definition: eitscanner.h:52
virtual int GetChanID(void) const
ISO 639-1 and ISO 639-2 support functions.
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:46
void bindValue(const QString &placeholder, const QVariant &val)
Add a single binding.
Definition: mythdbcon.cpp:863
A QElapsedTimer based timer to replace use of QTime as a timer.
Definition: mythtimer.h:13
uint GetInputId(void)
Returns the inputid.
Definition: tv_rec.h:241
uint m_activeScanNextChanIndex
Definition: eitscanner.h:68
void run(void) override
This runs the event loop for EITScanner until 'exitThread' is true.
Definition: eitscanner.cpp:66
virtual void SetEITHelper(EITHelper *)=0
QStringList m_activeScanChannels
Definition: eitscanner.h:66
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:125
bool wait(unsigned long time=ULONG_MAX)
Wait for the MThread to exit, with a maximum timeout.
Definition: mthread.cpp:311
MThread * m_eventThread
Definition: eitscanner.h:56
void TeardownAll(void)
Definition: eitscanner.cpp:42
QMutex m_lock
Definition: eitscanner.h:51
uint GetSourceID(void) const
Returns current source id.
Definition: tv_rec.cpp:3037
unsigned int uint
Definition: compat.h:140
#define LOC_ID
Definition: eitscanner.cpp:23
QWaitCondition m_exitThreadCond
Definition: eitscanner.h:58
void SetLanguagePreferences(const QStringList &langPref)
Definition: eithelper.cpp:115
QStringList iso639_get_language_list(void)
Returns list of three character ISO-639 language descriptors, starting with the most preferred.
Definition: iso639.cpp:35
uint GetListSize(void) const
Definition: eithelper.cpp:55
void StopPassiveScan(void)
Stops inserting Event Information Tables into DB.
Definition: eitscanner.cpp:218
QVariant value(int i) const
Definition: mythdbcon.h:198
void RescheduleRecordings(void)
Tells scheduler about programming changes.
Definition: eitscanner.cpp:190
void WriteEITCache(void)
Definition: eithelper.cpp:759
QDateTime m_activeScanNextTrig
Definition: eitscanner.h:64
QStringList::iterator m_activeScanNextChan
Definition: eitscanner.h:67
bool QueueEITChannelChange(const QString &name)
Queues up a channel change for the EITScanner.
Definition: tv_rec.cpp:3127
static int GetChanID(int db_mplexid, int service_transport_id, int major_channel, int minor_channel, int program_number)
This is the coordinating class of the Recorder Subsystem.
Definition: tv_rec.h:150
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:10
unsigned char t
Definition: ParseText.cpp:329
bool isActive(void) const
Definition: mythdbcon.h:204
volatile bool m_activeScanStopped
Definition: eitscanner.h:62
static MSqlQueryInfo InitCon(ConnectionReuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:535
void SetSourceID(uint _sourceid)
Definition: eithelper.cpp:132
void StartPassiveScan(ChannelBase *, EITSource *)
Start inserting Event Information Tables from the multiplex we happen to be tuned to into the databas...
Definition: eitscanner.cpp:199
EITHelper * m_eitHelper
Definition: eitscanner.h:55
bool prepare(const QString &query)
QSqlQuery::prepare() is not thread safe in Qt <= 3.3.2.
Definition: mythdbcon.cpp:807
virtual void SetEITRate(float rate)=0
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
static uint GetSourceIDForChannel(uint chanid)
Abstract class providing a generic interface to tuning hardware.
Definition: channelbase.h:31
void PruneEITCache(uint timestamp)
Definition: eithelper.cpp:754
TVRec * rec
Definition: eitscanner.h:60
volatile bool m_exitThread
Definition: eitscanner.h:57
void StartActiveScan(TVRec *, uint max_seconds_per_source)
Definition: eitscanner.cpp:234
QWaitCondition m_activeScanCond
Definition: eitscanner.h:63
void SetChannelID(uint _channelid)
Definition: eithelper.cpp:138
bool exec(void)
Wrap QSqlQuery::exec() so we can display SQL.
Definition: mythdbcon.cpp:603
static long int random(void)
Definition: compat.h:149
uint ProcessEvents(void)
Inserts events in EIT list.
Definition: eithelper.cpp:66
static void DBError(const QString &where, const MSqlQuery &query)
Definition: mythdb.cpp:179
volatile bool m_activeScan
Definition: eitscanner.h:61
EITSource * m_eitSource
Definition: eitscanner.h:53
EITScanner(uint cardnum)
Definition: eitscanner.cpp:32
void RescheduleRecordings(void)
Tells scheduler about programming changes.
Definition: eithelper.cpp:1405
void StopActiveScan(void)
Definition: eitscanner.cpp:299
uint m_activeScanTrigTime
Definition: eitscanner.h:65