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 constexpr std::array<const uint,5> kSz { 2000, 1800, 1600, 1400, 1200, };
69  static constexpr std::array<const float,5> kRt { 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 >= kSz[i])
85  {
86  rate = kRt[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")
143  }
144  }
145 
147  .addSecs(m_activeScanTrigTime);
148  if (!m_activeScanChannels.empty())
149  {
153  }
154 
155  // 24 hours ago
156  EITHelper::PruneEITCache(m_activeScanNextTrig.toSecsSinceEpoch() - 86400);
157  }
158 
159  m_lock.lock();
161  m_exitThreadCond.wait(&m_lock, 400); // sleep up to 400 ms.
162 
164  {
165  m_activeScanStopped = true;
166  m_activeScanCond.wakeAll();
167  }
168  }
169 
170  if (eitCount) /* some events have been handled since the last schedule request */
171  {
173  }
174 
175  m_activeScanStopped = true;
176  m_activeScanCond.wakeAll();
177  m_lock.unlock();
178 }
179 
187 {
189 }
190 
196  EITSource *eitSource)
197 {
198  QMutexLocker locker(&m_lock);
199 
200  m_eitSource = eitSource;
201  m_channel = channel;
202 
204  m_eitSource->SetEITRate(1.0F);
207 
208  LOG(VB_EIT, LOG_INFO, LOC_ID + "Started passive scan.");
209 }
210 
215 {
216  QMutexLocker locker(&m_lock);
217 
218  if (m_eitSource)
219  {
220  m_eitSource->SetEITHelper(nullptr);
221  m_eitSource = nullptr;
222  }
223  m_channel = nullptr;
224 
228  LOG(VB_EIT, LOG_INFO, LOC_ID + "Stopped passive scan.");
229 }
230 
231 void EITScanner::StartActiveScan(TVRec *_rec, uint max_seconds_per_source)
232 {
233  m_rec = _rec;
234 
235  if (m_activeScanChannels.isEmpty())
236  {
237  // TODO get input name and use it in crawl.
239  query.prepare(
240  "SELECT channum, MIN(chanid) "
241  "FROM channel, capturecard, videosource "
242  "WHERE deleted IS NULL AND "
243  " capturecard.sourceid = channel.sourceid AND "
244  " videosource.sourceid = channel.sourceid AND "
245  " channel.mplexid IS NOT NULL AND "
246  " visible > 0 AND "
247  " useonairguide = 1 AND "
248  " useeit = 1 AND "
249  " channum != '' AND "
250  " capturecard.cardid = :CARDID "
251  "GROUP BY mplexid "
252  "ORDER BY capturecard.sourceid, mplexid, "
253  " atsc_major_chan, atsc_minor_chan ");
254  query.bindValue(":CARDID", m_rec->GetInputId());
255 
256  if (!query.exec() || !query.isActive())
257  {
258  MythDB::DBError("EITScanner::StartActiveScan", query);
259  return;
260  }
261 
262  while (query.next())
263  m_activeScanChannels.push_back(query.value(0).toString());
264 
266  }
267 
268  LOG(VB_EIT, LOG_INFO, LOC_ID +
269  QString("StartActiveScan called with %1 multiplexes")
270  .arg(m_activeScanChannels.size()));
271 
272  // Start at a random channel. This is so that multiple cards with
273  // the same source don't all scan the same channels in the same
274  // order when the backend is first started up.
275  if (!m_activeScanChannels.empty())
276  {
277  // The start channel is random. From now on, start on the
278  // next channel. This makes sure the immediately following
279  // channels get scanned in a timely manner if we keep erroring
280  // out on the previous channel.
285 
287  m_activeScanTrigTime = max_seconds_per_source;
288  // Add a little randomness to trigger time so multiple
289  // cards will have a staggered channel changing time.
290  m_activeScanTrigTime += random() % 29;
291  m_activeScanStopped = false;
292  m_activeScan = true;
293  }
294 }
295 
297 {
298  QMutexLocker locker(&m_lock);
299 
300  m_activeScanStopped = false;
301  m_activeScan = false;
302  m_exitThreadCond.wakeAll();
303 
304  locker.unlock();
305  StopPassiveScan();
306  locker.relock();
307 
308  LOG(VB_EIT, LOG_INFO, LOC_ID + "Stopped active scan.");
309 
310  while (!m_activeScan && !m_activeScanStopped)
311  m_activeScanCond.wait(&m_lock, 100);
312 
313  m_rec = nullptr;
314 }
MSqlQuery::isActive
bool isActive(void) const
Definition: mythdbcon.h:204
MSqlQuery::next
bool next(void)
Wrap QSqlQuery::next() so we can display the query results.
Definition: mythdbcon.cpp:783
MSqlQuery
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:126
EITHelper::WriteEITCache
static void WriteEITCache(void)
Definition: eithelper.cpp:760
channel
QDomElement channel
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:498
EITHelper::SetChannelID
void SetChannelID(uint _channelid)
Definition: eithelper.cpp:135
EITScanner::RescheduleRecordings
void RescheduleRecordings(void)
Tells scheduler about programming changes.
Definition: eitscanner.cpp:186
MThread::start
void start(QThread::Priority p=QThread::InheritPriority)
Tell MThread to start running the thread in the near future.
Definition: mthread.cpp:292
mythdb.h
EITScanner::m_exitThread
volatile bool m_exitThread
Definition: eitscanner.h:57
EITScanner::StopPassiveScan
void StopPassiveScan(void)
Stops inserting Event Information Tables into DB.
Definition: eitscanner.cpp:214
MythTimer
A QElapsedTimer based timer to replace use of QTime as a timer.
Definition: mythtimer.h:14
eitscanner.h
EITScanner::m_activeScanNextTrig
QDateTime m_activeScanNextTrig
Definition: eitscanner.h:64
EITScanner::m_activeScanCond
QWaitCondition m_activeScanCond
Definition: eitscanner.h:63
EITScanner::m_eitSource
EITSource * m_eitSource
Definition: eitscanner.h:53
EITScanner::StartActiveScan
void StartActiveScan(TVRec *_rec, uint max_seconds_per_source)
Definition: eitscanner.cpp:231
TVRec::GetSourceID
uint GetSourceID(void) const
Returns current source id.
Definition: tv_rec.cpp:3049
EITScanner::EITScanner
EITScanner(uint cardnum)
Definition: eitscanner.cpp:32
EITScanner::StartPassiveScan
void StartPassiveScan(ChannelBase *channel, EITSource *eitSource)
Start inserting Event Information Tables from the multiplex we happen to be tuned to into the databas...
Definition: eitscanner.cpp:195
MSqlQuery::value
QVariant value(int i) const
Definition: mythdbcon.h:198
arg
arg(title).arg(filename).arg(doDelete))
MSqlQuery::exec
bool exec(void)
Wrap QSqlQuery::exec() so we can display SQL.
Definition: mythdbcon.cpp:603
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
channelbase.h
EITScanner::m_rec
TVRec * m_rec
Definition: eitscanner.h:60
MythDate::current
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:10
EITScanner::m_eitHelper
EITHelper * m_eitHelper
Definition: eitscanner.h:55
EITSource::SetEITRate
virtual void SetEITRate(float rate)=0
EITScanner::m_activeScanNextChanIndex
uint m_activeScanNextChanIndex
Definition: eitscanner.h:68
EITScanner::StopActiveScan
void StopActiveScan(void)
Definition: eitscanner.cpp:296
EITHelper::RescheduleRecordings
void RescheduleRecordings(void)
Tells scheduler about programming changes.
Definition: eithelper.cpp:1398
TVRec::GetInputId
uint GetInputId(void) const
Returns the inputid.
Definition: tv_rec.h:234
mythdate.h
ChannelUtil::GetChanID
static int GetChanID(int db_mplexid, int service_transport_id, int major_channel, int minor_channel, int program_number)
Definition: channelutil.cpp:1310
mythlogging.h
ChannelBase
Abstract class providing a generic interface to tuning hardware.
Definition: channelbase.h:32
hardwareprofile.i18n.t
t
Definition: i18n.py:36
MSqlQuery::InitCon
static MSqlQueryInfo InitCon(ConnectionReuse _reuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:535
compat.h
eithelper.h
MythDB::DBError
static void DBError(const QString &where, const MSqlQuery &query)
Definition: mythdb.cpp:179
EITScanner::m_eventThread
MThread * m_eventThread
Definition: eitscanner.h:56
EITSource::SetEITHelper
virtual void SetEITHelper(EITHelper *)=0
scheduledrecording.h
EITScanner::m_exitThreadCond
QWaitCondition m_exitThreadCond
Definition: eitscanner.h:58
EITScanner::m_lock
QMutex m_lock
Definition: eitscanner.h:51
uint
unsigned int uint
Definition: compat.h:140
EITHelper
Definition: eithelper.h:93
TVRec::QueueEITChannelChange
bool QueueEITChannelChange(const QString &name)
Queues up a channel change for the EITScanner.
Definition: tv_rec.cpp:3139
EITScanner::m_activeScan
volatile bool m_activeScan
Definition: eitscanner.h:61
channelutil.h
EITHelper::SetLanguagePreferences
void SetLanguagePreferences(const QStringList &langPref)
Definition: eithelper.cpp:112
EITScanner::m_activeScanNextChan
QStringList::iterator m_activeScanNextChan
Definition: eitscanner.h:67
EITScanner::m_channel
ChannelBase * m_channel
Definition: eitscanner.h:52
EITHelper::ProcessEvents
uint ProcessEvents(void)
Inserts events in EIT list.
Definition: eithelper.cpp:63
LOC_ID
#define LOC_ID
Definition: eitscanner.cpp:23
random
static long int random(void)
Definition: compat.h:149
MSqlQuery::bindValue
void bindValue(const QString &placeholder, const QVariant &val)
Add a single binding.
Definition: mythdbcon.cpp:864
EITScanner::m_activeScanTrigTime
uint m_activeScanTrigTime
Definition: eitscanner.h:65
ChannelUtil::GetSourceIDForChannel
static uint GetSourceIDForChannel(uint chanid)
Definition: channelutil.cpp:806
iso639_get_language_list
QStringList iso639_get_language_list(void)
Returns list of three character ISO-639 language descriptors, starting with the most preferred.
Definition: iso639.cpp:35
EITScanner::run
void run(void) override
This runs the event loop for EITScanner until 'exitThread' is true.
Definition: eitscanner.cpp:66
TVRec
This is the coordinating class of the Recorder Subsystem.
Definition: tv_rec.h:143
MThread
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:49
mthread.h
tv_rec.h
mythtimer.h
iso639.h
ISO 639-1 and ISO 639-2 support functions.
EITHelper::GetListSize
uint GetListSize(void) const
Definition: eithelper.cpp:52
EITHelper::PruneEITCache
static void PruneEITCache(uint timestamp)
Definition: eithelper.cpp:755
MThread::wait
bool wait(unsigned long time=ULONG_MAX)
Wait for the MThread to exit, with a maximum timeout.
Definition: mthread.cpp:309
EITScanner::m_activeScanChannels
QStringList m_activeScanChannels
Definition: eitscanner.h:66
EITScanner::m_activeScanStopped
volatile bool m_activeScanStopped
Definition: eitscanner.h:62
EITHelper::SetSourceID
void SetSourceID(uint _sourceid)
Definition: eithelper.cpp:129
query
MSqlQuery query(MSqlQuery::InitCon())
EITScanner::TeardownAll
void TeardownAll(void)
Definition: eitscanner.cpp:42
EITSource
Definition: eitscanner.h:20
MSqlQuery::prepare
bool prepare(const QString &query)
QSqlQuery::prepare() is not thread safe in Qt <= 3.3.2.
Definition: mythdbcon.cpp:808
ChannelBase::GetChanID
virtual int GetChanID(void) const
Definition: channelbase.cpp:493