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  if (chanid > 0)
192  {
193  m_eitHelper->SetChannelID(chanid);
195  LOG(VB_EIT, LOG_INFO, LOC_ID +
196  QString("Started processing EIT events in %1 scan for channel %2 chanid %3")
197  .arg(m_activeScan ? "active" : "passive",
199  QString::number(chanid)));
200  }
201  else
202  {
203  LOG(VB_EIT, LOG_INFO, LOC_ID +
204  QString("Failed to start processing EIT events, invalid chanid:%1")
205  .arg(chanid));
206  }
207 }
208 
213 {
214  QMutexLocker locker(&m_lock);
215 
216  if (m_eitSource)
217  {
218  m_eitSource->SetEITHelper(nullptr);
219  m_eitSource = nullptr;
220  }
221  m_channel = nullptr;
222 
226  LOG(VB_EIT, LOG_INFO, LOC_ID +
227  QString("Stopped processing EIT events in %1 scan")
228  .arg(m_activeScanStopped ? "passive" : "active"));
229 }
230 
231 void EITScanner::StartActiveScan(TVRec *rec, std::chrono::seconds 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.
238  MSqlQuery query(MSqlQuery::InitCon());
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 += std::chrono::seconds(MythRandom() % 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:208
MSqlQuery::next
bool next(void)
Wrap QSqlQuery::next() so we can display the query results.
Definition: mythdbcon.cpp:812
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
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:59
EITScanner::StopPassiveScan
void StopPassiveScan(void)
Stops inserting Event Information Tables into DB.
Definition: eitscanner.cpp:212
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:139
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:66
EITScanner::m_activeScanCond
QWaitCondition m_activeScanCond
Definition: eitscanner.h:65
EITScanner::m_eitSource
EITSource * m_eitSource
Definition: eitscanner.h:55
TVRec::GetSourceID
uint GetSourceID(void) const
Returns current source id.
Definition: tv_rec.cpp:3051
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:197
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
ChannelBase::GetChannelName
virtual QString GetChannelName(void) const
Definition: channelbase.h:64
EITScanner::m_rec
TVRec * m_rec
Definition: eitscanner.h:62
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:57
EITSource::SetEITRate
virtual void SetEITRate(float rate)=0
EITScanner::m_activeScanNextChanIndex
uint m_activeScanNextChanIndex
Definition: eitscanner.h:70
EITScanner::StopActiveScan
void StopActiveScan(void)
Definition: eitscanner.cpp:296
EITHelper::RescheduleRecordings
void RescheduleRecordings(void)
Tells scheduler about programming changes.
Definition: eithelper.cpp:1407
TVRec::GetInputId
uint GetInputId(void) const
Returns the inputid.
Definition: tv_rec.h:237
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:1308
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:200
EITScanner::m_eventThread
MThread * m_eventThread
Definition: eitscanner.h:58
EITHelper::SetChannelID
void SetChannelID(uint channelid)
Definition: eithelper.cpp:145
EITSource::SetEITHelper
virtual void SetEITHelper(EITHelper *)=0
scheduledrecording.h
EITScanner::m_exitThreadCond
QWaitCondition m_exitThreadCond
Definition: eitscanner.h:60
EITScanner::m_lock
QMutex m_lock
Definition: eitscanner.h:53
uint
unsigned int uint
Definition: compat.h:140
EITHelper
Definition: eithelper.h:93
EITScanner::m_activeScanTrigTime
std::chrono::seconds m_activeScanTrigTime
Definition: eitscanner.h:67
TVRec::QueueEITChannelChange
bool QueueEITChannelChange(const QString &name)
Queues up a channel change for the EITScanner.
Definition: tv_rec.cpp:3141
EITScanner::m_activeScan
volatile bool m_activeScan
Definition: eitscanner.h:63
channelutil.h
EITHelper::SetLanguagePreferences
void SetLanguagePreferences(const QStringList &langPref)
Definition: eithelper.cpp:122
EITScanner::m_activeScanNextChan
QStringList::iterator m_activeScanNextChan
Definition: eitscanner.h:69
MythRandom
MBASE_PUBLIC uint32_t MythRandom()
Definition: mythmiscutil.h:21
EITScanner::m_channel
ChannelBase * m_channel
Definition: eitscanner.h:54
EITHelper::ProcessEvents
uint ProcessEvents(void)
Get events from queue and insert into DB after processing.
Definition: eithelper.cpp:73
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:893
ChannelUtil::GetSourceIDForChannel
static uint GetSourceIDForChannel(uint chanid)
Definition: channelutil.cpp:804
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:145
MThread
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:48
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:757
EITScanner::m_activeScanChannels
QStringList m_activeScanChannels
Definition: eitscanner.h:68
EITScanner::m_activeScanStopped
volatile bool m_activeScanStopped
Definition: eitscanner.h:64
EITScanner::StartActiveScan
void StartActiveScan(TVRec *rec, std::chrono::seconds max_seconds_per_source)
Definition: eitscanner.cpp:231
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:837
ChannelBase::GetChanID
virtual int GetChanID(void) const
Definition: channelbase.cpp:487