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 // C++
8 #include <cstdlib>
9 
10 // MythTV
11 #include "scheduledrecording.h"
12 #include "channelbase.h"
13 #include "channelutil.h"
14 #include "mythlogging.h"
15 #include "eitscanner.h"
16 #include "eithelper.h"
17 #include "mythtimer.h"
18 #include "mythdate.h"
19 #include "mthread.h"
20 #include "iso639.h"
21 #include "mythdb.h"
22 #include "tv_rec.h"
23 
24 #define LOC QString("EITScanner: ")
25 #define LOC_ID QString("EITScanner[%1]: ").arg(m_cardnum)
26 
34  : m_eitHelper(new EITHelper(cardnum)),
35  m_eventThread(new MThread("EIT", this)),
36  m_cardnum(cardnum)
37 {
38  QStringList langPref = iso639_get_language_list();
40 
41  LOG(VB_EIT, LOG_INFO, LOC_ID + "Start EIT scanner thread");
42  m_eventThread->start(QThread::IdlePriority);
43 }
44 
46 {
48  if (!m_exitThread)
49  {
50  m_lock.lock();
51  m_exitThread = true;
52  m_exitThreadCond.wakeAll();
53  m_lock.unlock();
54  }
56  delete m_eventThread;
57  m_eventThread = nullptr;
58 
59  if (m_eitHelper)
60  {
61  delete m_eitHelper;
62  m_eitHelper = nullptr;
63  }
64 }
65 
69 void EITScanner::run(void)
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  m_lock.lock();
82  if (m_eitSource)
83  m_eitSource->SetEITRate(1.0F);
84  m_lock.unlock();
85 
86  if (list_size)
87  {
88  eitCount += m_eitHelper->ProcessEvents();
89  t.start();
90  }
91 
92  // Tell the scheduler to run if we are in passive scan
93  // and there have been updated events since the last scheduler run
94  // but not in the last 60 seconds
95  if (!m_activeScan && eitCount && (t.elapsed() > 60s))
96  {
97  LOG(VB_EIT, LOG_INFO,
98  LOC_ID + QString("Added %1 EIT events in passive scan").arg(eitCount));
99  eitCount = 0;
101  }
102 
103  // Is it time to move to the next transport in active scan?
105  {
106  // If there have been any new events, tell scheduler to run.
107  if (eitCount)
108  {
109  LOG(VB_EIT, LOG_INFO,
110  LOC_ID + QString("Added %1 EIT events in active scan").arg(eitCount));
111  eitCount = 0;
113  }
114 
116  {
119  }
120 
121  if (!(*m_activeScanNextChan).isEmpty())
122  {
125  {
128  LOG(VB_EIT, LOG_INFO, LOC_ID +
129  QString("Now looking for EIT data on multiplex of channel %1 of source %2")
130  .arg(*m_activeScanNextChan).arg(m_rec->GetSourceID()));
131  }
132  }
133 
135  .addSecs(m_activeScanTrigTime.count());
136  if (!m_activeScanChannels.empty())
137  {
141  }
142 
143  // 24 hours ago
144  EITHelper::PruneEITCache(m_activeScanNextTrig.toSecsSinceEpoch() - 86400);
145  }
146 
147  m_lock.lock();
149  m_exitThreadCond.wait(&m_lock, 400); // sleep up to 400 ms.
150 
152  {
153  m_activeScanStopped = true;
154  m_activeScanCond.wakeAll();
155  }
156  }
157 
158  if (eitCount) /* some events have been handled since the last schedule request */
159  {
161  }
162 
163  m_activeScanStopped = true;
164  m_activeScanCond.wakeAll();
165  m_lock.unlock();
166 }
167 
172 {
174 }
175 
181  EITSource *eitSource)
182 {
183  QMutexLocker locker(&m_lock);
184 
185  m_eitSource = eitSource;
186  m_channel = channel;
187 
189  m_eitSource->SetEITRate(1.0F);
190  int chanid = m_channel->GetChanID();
191  m_eitHelper->SetChannelID(chanid);
193 
194  LOG(VB_EIT, LOG_INFO, LOC_ID + "Started passive scan");
195 }
196 
201 {
202  QMutexLocker locker(&m_lock);
203 
204  if (m_eitSource)
205  {
206  m_eitSource->SetEITHelper(nullptr);
207  m_eitSource = nullptr;
208  }
209  m_channel = nullptr;
210 
214  LOG(VB_EIT, LOG_INFO, LOC_ID + "Stopped passive scan");
215 }
216 
217 void EITScanner::StartActiveScan(TVRec *_rec, std::chrono::seconds max_seconds_per_source)
218 {
219  m_rec = _rec;
220 
221  if (m_activeScanChannels.isEmpty())
222  {
223  // TODO get input name and use it in crawl.
225  query.prepare(
226  "SELECT channum, MIN(chanid) "
227  "FROM channel, capturecard, videosource "
228  "WHERE deleted IS NULL AND "
229  " capturecard.sourceid = channel.sourceid AND "
230  " videosource.sourceid = channel.sourceid AND "
231  " channel.mplexid IS NOT NULL AND "
232  " visible > 0 AND "
233  " useonairguide = 1 AND "
234  " useeit = 1 AND "
235  " channum != '' AND "
236  " capturecard.cardid = :CARDID "
237  "GROUP BY mplexid "
238  "ORDER BY capturecard.sourceid, mplexid, "
239  " atsc_major_chan, atsc_minor_chan ");
240  query.bindValue(":CARDID", m_rec->GetInputId());
241 
242  if (!query.exec() || !query.isActive())
243  {
244  MythDB::DBError("EITScanner::StartActiveScan", query);
245  return;
246  }
247 
248  while (query.next())
249  m_activeScanChannels.push_back(query.value(0).toString());
250 
252  }
253 
254  LOG(VB_EIT, LOG_INFO, LOC_ID +
255  QString("StartActiveScan called with %1 multiplexes")
256  .arg(m_activeScanChannels.size()));
257 
258  // Start at a random channel. This is so that multiple cards with
259  // the same source don't all scan the same channels in the same
260  // order when the backend is first started up.
261  if (!m_activeScanChannels.empty())
262  {
263  // The start channel is random. From now on, start on the
264  // next channel. This makes sure the immediately following
265  // channels get scanned in a timely manner if we keep erroring
266  // out on the previous channel.
271 
273  m_activeScanTrigTime = max_seconds_per_source;
274  // Add a little randomness to trigger time so multiple
275  // cards will have a staggered channel changing time.
276  m_activeScanTrigTime += std::chrono::seconds(MythRandom() % 29);
277  m_activeScanStopped = false;
278  m_activeScan = true;
279  }
280 }
281 
283 {
284  QMutexLocker locker(&m_lock);
285 
286  m_activeScanStopped = false;
287  m_activeScan = false;
288  m_exitThreadCond.wakeAll();
289 
290  locker.unlock();
291  StopPassiveScan();
292  locker.relock();
293 
294  LOG(VB_EIT, LOG_INFO, LOC_ID + "Stopped active scan");
295 
296  while (!m_activeScan && !m_activeScanStopped)
297  m_activeScanCond.wait(&m_lock, 100);
298 
299  m_rec = nullptr;
300 }
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:125
EITHelper::WriteEITCache
static void WriteEITCache(void)
Definition: eithelper.cpp:765
channel
QDomElement channel
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:501
EITScanner::RescheduleRecordings
void RescheduleRecordings(void)
Tells scheduler about programming changes.
Definition: eitscanner.cpp:171
MThread::start
void start(QThread::Priority p=QThread::InheritPriority)
Tell MThread to start running the thread in the near future.
Definition: mthread.cpp:286
mythdb.h
EITScanner::m_exitThread
volatile bool m_exitThread
Definition: eitscanner.h:60
EITScanner::StopPassiveScan
void StopPassiveScan(void)
Stops inserting Event Information Tables into DB.
Definition: eitscanner.cpp:200
MythTimer
A QElapsedTimer based timer to replace use of QTime as a timer.
Definition: mythtimer.h:13
EITHelper::SetSourceID
void SetSourceID(uint sourceid)
Definition: eithelper.cpp:142
MThread::wait
bool wait(std::chrono::milliseconds time=std::chrono::milliseconds::max())
Wait for the MThread to exit, with a maximum timeout.
Definition: mthread.cpp:303
eitscanner.h
EITScanner::m_activeScanNextTrig
QDateTime m_activeScanNextTrig
Definition: eitscanner.h:67
EITScanner::m_activeScanCond
QWaitCondition m_activeScanCond
Definition: eitscanner.h:66
EITScanner::m_eitSource
EITSource * m_eitSource
Definition: eitscanner.h:56
TVRec::GetSourceID
uint GetSourceID(void) const
Returns current source id.
Definition: tv_rec.cpp:3048
EITScanner::EITScanner
EITScanner(uint cardnum)
Definition: eitscanner.cpp:33
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:180
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:63
MythDate::current
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:10
EITScanner::StartActiveScan
void StartActiveScan(TVRec *_rec, std::chrono::seconds max_seconds_per_source)
Definition: eitscanner.cpp:217
EITScanner::m_eitHelper
EITHelper * m_eitHelper
Definition: eitscanner.h:58
EITSource::SetEITRate
virtual void SetEITRate(float rate)=0
EITScanner::m_activeScanNextChanIndex
uint m_activeScanNextChanIndex
Definition: eitscanner.h:71
EITScanner::StopActiveScan
void StopActiveScan(void)
Definition: eitscanner.cpp:282
EITHelper::RescheduleRecordings
void RescheduleRecordings(void)
Tells scheduler about programming changes.
Definition: eithelper.cpp:1414
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:31
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:178
EITScanner::m_eventThread
MThread * m_eventThread
Definition: eitscanner.h:59
EITHelper::SetChannelID
void SetChannelID(uint channelid)
Definition: eithelper.cpp:148
EITSource::SetEITHelper
virtual void SetEITHelper(EITHelper *)=0
scheduledrecording.h
EITScanner::m_exitThreadCond
QWaitCondition m_exitThreadCond
Definition: eitscanner.h:61
EITScanner::m_lock
QMutex m_lock
Definition: eitscanner.h:54
uint
unsigned int uint
Definition: compat.h:140
EITHelper
Definition: eithelper.h:93
EITScanner::m_activeScanTrigTime
std::chrono::seconds m_activeScanTrigTime
Definition: eitscanner.h:68
TVRec::QueueEITChannelChange
bool QueueEITChannelChange(const QString &name)
Queues up a channel change for the EITScanner.
Definition: tv_rec.cpp:3138
EITScanner::m_activeScan
volatile bool m_activeScan
Definition: eitscanner.h:64
channelutil.h
EITHelper::SetLanguagePreferences
void SetLanguagePreferences(const QStringList &langPref)
Definition: eithelper.cpp:125
EITScanner::m_activeScanNextChan
QStringList::iterator m_activeScanNextChan
Definition: eitscanner.h:70
MythRandom
MBASE_PUBLIC uint32_t MythRandom()
Definition: mythmiscutil.h:24
EITScanner::m_channel
ChannelBase * m_channel
Definition: eitscanner.h:55
EITHelper::ProcessEvents
uint ProcessEvents(void)
Get events from queue and insert into DB after processing.
Definition: eithelper.cpp:76
LOC_ID
#define LOC_ID
Definition: eitscanner.cpp:25
MSqlQuery::bindValue
void bindValue(const QString &placeholder, const QVariant &val)
Add a single binding.
Definition: mythdbcon.cpp:864
ChannelUtil::GetSourceIDForChannel
static uint GetSourceIDForChannel(uint chanid)
Definition: channelutil.cpp:805
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:33
EITScanner::run
void run(void) override
This runs the event loop for EITScanner until 'exitThread' is true.
Definition: eitscanner.cpp:69
TVRec
This is the coordinating class of the Recorder Subsystem.
Definition: tv_rec.h:142
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:55
EITHelper::PruneEITCache
static void PruneEITCache(uint timestamp)
Definition: eithelper.cpp:760
EITScanner::m_activeScanChannels
QStringList m_activeScanChannels
Definition: eitscanner.h:69
EITScanner::m_activeScanStopped
volatile bool m_activeScanStopped
Definition: eitscanner.h:65
query
MSqlQuery query(MSqlQuery::InitCon())
EITScanner::TeardownAll
void TeardownAll(void)
Definition: eitscanner.cpp:45
EITSource
Definition: eitscanner.h:22
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:487