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 "libmythbase/compat.h"
6 
7 // C++
8 #include <cstdlib>
9 
10 // MythTV
11 #include "libmythbase/iso639.h"
12 #include "libmythbase/mthread.h"
13 #include "libmythbase/mythdate.h"
14 #include "libmythbase/mythdb.h"
16 #include "libmythbase/mythrandom.h"
17 #include "libmythbase/mythtimer.h"
18 
19 #include "cardutil.h"
20 #include "channelutil.h"
21 #include "eithelper.h"
22 #include "eitscanner.h"
23 #include "recorders/channelbase.h"
24 #include "scheduledrecording.h"
25 #include "sourceutil.h"
26 #include "tv_rec.h"
27 
28 #define LOC QString("EITScanner[%1]: ").arg(m_cardnum)
29 
36  : m_eitHelper(new EITHelper(cardnum)),
37  m_eventThread(new MThread("EIT", this)),
38  m_cardnum(cardnum)
39 {
40  QStringList langPref = iso639_get_language_list();
42 
43  LOG(VB_EIT, LOG_INFO, LOC + "Start EIT scanner thread");
44  m_eventThread->start(QThread::IdlePriority);
45 }
46 
51 {
53  if (!m_exitThread)
54  {
55  m_lock.lock();
56  m_exitThread = true;
57  m_exitThreadCond.wakeAll();
58  m_lock.unlock();
59  }
61  delete m_eventThread;
62  m_eventThread = nullptr;
63 
64  if (m_eitHelper)
65  {
66  delete m_eitHelper;
67  m_eitHelper = nullptr;
68  }
69 }
70 
74 void EITScanner::run(void)
75 {
76  m_lock.lock();
77 
78  MythTimer t;
79  uint eitCount = 0;
80 
81  while (!m_exitThread)
82  {
83  m_lock.unlock();
84 
85  uint list_size = m_eitHelper->GetListSize();
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 + 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 + QString("Added %1 EIT events in active scan").arg(eitCount));
111  eitCount = 0;
113  }
114 
116  {
118  }
119 
120  if (!(*m_activeScanNextChan).isEmpty())
121  {
124  {
125  uint sourceid = m_rec->GetSourceID();
126  uint chanid = ChannelUtil::GetChanID(sourceid, *m_activeScanNextChan);
127  m_eitHelper->SetChannelID(chanid);
128 
129  uint mplexid = ChannelUtil::GetMplexID(chanid);
130  QString sourcename = SourceUtil::GetSourceName(sourceid);
131  LOG(VB_EIT, LOG_INFO, LOC +
132  QString("Next EIT active scan source %1 '%2' multiplex %3 chanid %4 channel %5")
133  .arg(sourceid).arg(sourcename).arg(mplexid).arg(chanid).arg(*m_activeScanNextChan));
134  }
135  }
136 
139 
140  // Remove all EIT cache entries that are more than 24 hours old
141  EITHelper::PruneEITCache(m_activeScanNextTrig.toSecsSinceEpoch() - 86400);
142  }
143 
144  m_lock.lock();
146  m_exitThreadCond.wait(&m_lock, 400); // sleep up to 400 ms.
147 
149  {
150  m_activeScanStopped = true;
151  m_activeScanCond.wakeAll();
152  }
153  }
154 
155  if (eitCount) /* some events have been handled since the last schedule request */
156  {
158  }
159 
160  m_activeScanStopped = true;
161  m_activeScanCond.wakeAll();
162  m_lock.unlock();
163 }
164 
169 {
171 }
172 
178  EITSource *eitSource)
179 {
180  QMutexLocker locker(&m_lock);
181 
182  m_eitSource = eitSource;
183  m_channel = channel;
184 
186  m_eitSource->SetEITRate(1.0F);
187  int chanid = m_channel->GetChanID();
188  QString channum = m_channel->GetChannelName();
189  if (chanid > 0)
190  {
191  uint sourceid = m_channel->GetSourceID();
192  uint mplexid = ChannelUtil::GetMplexID(chanid);
193  QString sourcename = SourceUtil::GetSourceName(sourceid);
194  m_eitHelper->SetChannelID(chanid);
195  m_eitHelper->SetSourceID(sourceid);
196  LOG(VB_EIT, LOG_INFO, LOC +
197  QString("Start EIT %1 scan source %2 '%3' multiplex %4 chanid %5 channel %6")
198  .arg(m_activeScan ? "active" : "passive").arg(sourceid).arg(sourcename)
199  .arg(mplexid).arg(chanid).arg(channum));
200  }
201  else
202  {
203  LOG(VB_EIT, LOG_INFO, LOC +
204  QString("Failed to start EIT scan, invalid chanid %1 channum %2")
205  .arg(chanid).arg(channum));
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 +
227  QString("Stop EIT %1 scan")
228  .arg(m_activeScanStopped ? "passive" : "active"));
229 }
230 
234 void EITScanner::StartActiveScan(TVRec *rec, std::chrono::seconds max_seconds_per_source)
235 {
236  m_rec = rec;
237 
238  if (m_activeScanChannels.isEmpty())
239  {
240  // Create a channel list with one channel from each multiplex
241  // from the video source connected to this input.
242  // This list is then used in the EIT crawl so that all
243  // available multiplexes are visited in turn.
244  MSqlQuery query(MSqlQuery::InitCon());
245  query.prepare(
246  "SELECT channum, MIN(chanid) "
247  "FROM channel, capturecard, videosource "
248  "WHERE capturecard.sourceid = channel.sourceid AND "
249  " videosource.sourceid = channel.sourceid AND "
250  " channel.deleted IS NULL AND "
251  " channel.mplexid IS NOT NULL AND "
252  " channel.visible > 0 AND "
253  " channel.useonairguide = 1 AND "
254  " channel.channum != '' AND "
255  " videosource.useeit = 1 AND "
256  " capturecard.cardid = :CARDID "
257  "GROUP BY mplexid "
258  "ORDER BY capturecard.sourceid, mplexid, "
259  " atsc_major_chan, atsc_minor_chan ");
260  query.bindValue(":CARDID", m_rec->GetInputId());
261 
262  if (!query.exec() || !query.isActive())
263  {
264  MythDB::DBError("EITScanner::StartActiveScan", query);
265  return;
266  }
267 
268  while (query.next())
269  m_activeScanChannels.push_back(query.value(0).toString());
270 
272  }
273 
274  {
275  uint sourceid = m_rec->GetSourceID();
276  QString sourcename = SourceUtil::GetSourceName(sourceid);
277  LOG(VB_EIT, LOG_INFO, LOC +
278  QString("StartActiveScan for source %1 '%2' with %3 multiplexes")
279  .arg(sourceid).arg(sourcename).arg(m_activeScanChannels.size()));
280  }
281 
282  // Start at a random channel. This is so that multiple cards with
283  // the same source don't all scan the same channels in the same
284  // order when the backend is first started up.
285  if (!m_activeScanChannels.empty())
286  {
287  // The start channel is random. From now on, start on the
288  // next channel. This makes sure the immediately following
289  // channels get scanned in a timely manner if we keep erroring
290  // out on the previous channel.
291  auto nextChanIndex = MythRandom() % m_activeScanChannels.size();
292  m_activeScanNextChan = m_activeScanChannels.begin() + nextChanIndex;
293 
294  // Start scan now
296 
297  // Add a little randomness to trigger time so multiple
298  // cards will have a staggered channel changing time.
299  m_activeScanTrigTime = max_seconds_per_source;
300  m_activeScanTrigTime += std::chrono::seconds(MythRandom(0, 28));
301 
302  m_activeScanStopped = false;
303  m_activeScan = true;
304  }
305 }
306 
311 {
312  QMutexLocker locker(&m_lock);
313 
314  m_activeScanStopped = false;
315  m_activeScan = false;
316  m_exitThreadCond.wakeAll();
317 
318  locker.unlock();
320  locker.relock();
321 
322  while (!m_activeScan && !m_activeScanStopped)
323  m_activeScanCond.wait(&m_lock, 100);
324 
325  m_rec = nullptr;
326 }
MSqlQuery::isActive
bool isActive(void) const
Definition: mythdbcon.h:216
MSqlQuery::next
bool next(void)
Wrap QSqlQuery::next() so we can display the query results.
Definition: mythdbcon.cpp:811
MSqlQuery
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:128
EITHelper::WriteEITCache
static void WriteEITCache(void)
Definition: eithelper.cpp:763
EITScanner::RescheduleRecordings
void RescheduleRecordings(void)
Tells scheduler about programming changes.
Definition: eitscanner.cpp:168
MThread::start
void start(QThread::Priority p=QThread::InheritPriority)
Tell MThread to start running the thread in the near future.
Definition: mthread.cpp:283
EITScanner::StopEITEventProcessing
void StopEITEventProcessing(void)
Stops inserting Event Information Tables into DB.
Definition: eitscanner.cpp:212
mythdb.h
SourceUtil::GetSourceName
static QString GetSourceName(uint sourceid)
Definition: sourceutil.cpp:47
mythrandom.h
EITScanner::m_exitThread
volatile bool m_exitThread
Definition: eitscanner.h:53
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:140
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:300
eitscanner.h
EITScanner::StartEITEventProcessing
void StartEITEventProcessing(ChannelBase *channel, EITSource *eitSource)
Start inserting Event Information Tables from the multiplex we happen to be tuned to into the databas...
Definition: eitscanner.cpp:177
EITScanner::m_activeScanNextTrig
QDateTime m_activeScanNextTrig
Definition: eitscanner.h:60
EITScanner::m_activeScanCond
QWaitCondition m_activeScanCond
Definition: eitscanner.h:59
EITScanner::m_eitSource
EITSource * m_eitSource
Definition: eitscanner.h:49
TVRec::GetSourceID
uint GetSourceID(void) const
Returns current source id.
Definition: tv_rec.cpp:2997
EITScanner::EITScanner
EITScanner(uint cardnum)
Definition: eitscanner.cpp:35
MSqlQuery::value
QVariant value(int i) const
Definition: mythdbcon.h:205
MSqlQuery::exec
bool exec(void)
Wrap QSqlQuery::exec() so we can display SQL.
Definition: mythdbcon.cpp:617
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
channelbase.h
ChannelUtil::GetMplexID
static uint GetMplexID(uint sourceid, const QString &channum)
Definition: channelutil.cpp:460
ChannelBase::GetChannelName
virtual QString GetChannelName(void) const
Definition: channelbase.h:64
EITScanner::m_rec
TVRec * m_rec
Definition: eitscanner.h:56
MythDate::current
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:14
EITScanner::m_eitHelper
EITHelper * m_eitHelper
Definition: eitscanner.h:51
EITSource::SetEITRate
virtual void SetEITRate(float rate)=0
EITScanner::StopActiveScan
void StopActiveScan(void)
Stop active EIT scan.
Definition: eitscanner.cpp:310
EITHelper::RescheduleRecordings
void RescheduleRecordings(void)
Tells scheduler about programming changes.
Definition: eithelper.cpp:1411
TVRec::GetInputId
uint GetInputId(void) const
Returns the inputid.
Definition: tv_rec.h:236
mythdate.h
sourceutil.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:549
compat.h
eithelper.h
MythDB::DBError
static void DBError(const QString &where, const MSqlQuery &query)
Definition: mythdb.cpp:226
EITScanner::m_eventThread
MThread * m_eventThread
Definition: eitscanner.h:52
EITHelper::SetChannelID
void SetChannelID(uint channelid)
Definition: eithelper.cpp:146
EITSource::SetEITHelper
virtual void SetEITHelper(EITHelper *)=0
scheduledrecording.h
EITScanner::m_exitThreadCond
QWaitCondition m_exitThreadCond
Definition: eitscanner.h:54
EITScanner::m_lock
QMutex m_lock
Definition: eitscanner.h:47
uint
unsigned int uint
Definition: compat.h:81
EITHelper
Definition: eithelper.h:93
EITScanner::m_activeScanTrigTime
std::chrono::seconds m_activeScanTrigTime
Definition: eitscanner.h:61
TVRec::QueueEITChannelChange
bool QueueEITChannelChange(const QString &name)
Queues up a channel change for the EITScanner.
Definition: tv_rec.cpp:3087
EITScanner::m_activeScan
volatile bool m_activeScan
Definition: eitscanner.h:57
LOC
#define LOC
Definition: eitscanner.cpp:28
channelutil.h
EITHelper::SetLanguagePreferences
void SetLanguagePreferences(const QStringList &langPref)
Definition: eithelper.cpp:123
EITScanner::m_activeScanNextChan
QStringList::iterator m_activeScanNextChan
Definition: eitscanner.h:63
EITScanner::m_channel
ChannelBase * m_channel
Definition: eitscanner.h:48
EITHelper::ProcessEvents
uint ProcessEvents(void)
Get events from queue and insert into DB after processing.
Definition: eithelper.cpp:74
cardutil.h
MSqlQuery::bindValue
void bindValue(const QString &placeholder, const QVariant &val)
Add a single binding.
Definition: mythdbcon.cpp:887
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 'm_exitThread' is true.
Definition: eitscanner.cpp:74
TVRec
This is the coordinating class of the Recorder Subsystem.
Definition: tv_rec.h:144
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:53
EITHelper::PruneEITCache
static void PruneEITCache(uint timestamp)
Definition: eithelper.cpp:758
ChannelBase::GetSourceID
virtual uint GetSourceID(void) const
Definition: channelbase.h:71
EITScanner::m_activeScanChannels
QStringList m_activeScanChannels
Definition: eitscanner.h:62
EITScanner::m_activeScanStopped
volatile bool m_activeScanStopped
Definition: eitscanner.h:58
MythRandomStd::MythRandom
uint32_t MythRandom()
generate 32 random bits
Definition: mythrandom.h:20
EITScanner::StartActiveScan
void StartActiveScan(TVRec *rec, std::chrono::seconds max_seconds_per_source)
Start active EIT scan.
Definition: eitscanner.cpp:234
EITScanner::TeardownAll
void TeardownAll(void)
Stop active scan, delete thread and delete eithelper.
Definition: eitscanner.cpp:50
EITSource
Definition: eitscanner.h:19
MSqlQuery::prepare
bool prepare(const QString &query)
QSqlQuery::prepare() is not thread safe in Qt <= 3.3.2.
Definition: mythdbcon.cpp:836
ChannelBase::GetChanID
virtual int GetChanID(void) const
Definition: channelbase.cpp:494