/
eitscanner.cpp
303 lines (253 loc) · 8.52 KB
/
eitscanner.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// -*- Mode: c++ -*-
// POSIX headers
#include <sys/time.h>
#include "compat.h"
#include <cstdlib>
#include "tv_rec.h"
#include "channelbase.h"
#include "iso639.h"
#include "eitscanner.h"
#include "eithelper.h"
#include "scheduledrecording.h"
#include "mythmiscutil.h"
#include "mythdb.h"
#include "mythlogging.h"
#include "mthread.h"
#define LOC QString("EITScanner: ")
#define LOC_ID QString("EITScanner (%1): ").arg(cardnum)
/** \class EITScanner
* \brief Acts as glue between ChannelBase, EITSource, and EITHelper.
*
* This is the class where the "EIT Crawl" is implemented.
*
*/
QMutex EITScanner::resched_lock;
QDateTime EITScanner::resched_next_time = QDateTime::currentDateTime();
const uint EITScanner::kMinRescheduleInterval = 150;
EITScanner::EITScanner(uint _cardnum)
: channel(NULL), eitSource(NULL),
eitHelper(new EITHelper()), eventThread(new MThread("EIT", this)),
exitThread(false),
rec(NULL), activeScan(false),
activeScanStopped(true), activeScanTrigTime(0),
cardnum(_cardnum)
{
QStringList langPref = iso639_get_language_list();
eitHelper->SetLanguagePreferences(langPref);
eventThread->start(QThread::IdlePriority);
}
void EITScanner::TeardownAll(void)
{
StopActiveScan();
if (!exitThread)
{
lock.lock();
exitThread = true;
exitThreadCond.wakeAll();
lock.unlock();
}
eventThread->wait();
delete eventThread;
eventThread = NULL;
if (eitHelper)
{
delete eitHelper;
eitHelper = NULL;
}
}
/**
* \brief This runs the event loop for EITScanner until 'exitThread' is true.
*/
void EITScanner::run(void)
{
static const uint sz[] = { 2000, 1800, 1600, 1400, 1200, };
static const float rt[] = { 0.0f, 0.2f, 0.4f, 0.6f, 0.8f, };
lock.lock();
MythTimer t;
uint eitCount = 0;
while (!exitThread)
{
lock.unlock();
uint list_size = eitHelper->GetListSize();
float rate = 1.0f;
for (uint i = 0; i < 5; i++)
{
if (list_size >= sz[i])
{
rate = rt[i];
break;
}
}
lock.lock();
if (eitSource)
eitSource->SetEITRate(rate);
lock.unlock();
if (list_size)
{
eitCount += eitHelper->ProcessEvents();
t.start();
}
// If there have been any new events and we haven't
// seen any in a while, tell scheduler to run.
if (eitCount && (t.elapsed() > 60 * 1000))
{
LOG(VB_EIT, LOG_INFO,
LOC_ID + QString("Added %1 EIT Events").arg(eitCount));
eitCount = 0;
RescheduleRecordings();
}
if (activeScan && (QDateTime::currentDateTime() > activeScanNextTrig))
{
// if there have been any new events, tell scheduler to run.
if (eitCount)
{
LOG(VB_EIT, LOG_INFO,
LOC_ID + QString("Added %1 EIT Events").arg(eitCount));
eitCount = 0;
RescheduleRecordings();
}
if (activeScanNextChan == activeScanChannels.end())
activeScanNextChan = activeScanChannels.begin();
if (!(*activeScanNextChan).isEmpty())
{
eitHelper->WriteEITCache();
rec->SetChannel(*activeScanNextChan, TVRec::kFlagEITScan);
LOG(VB_EIT, LOG_INFO,
LOC_ID + QString("Now looking for EIT data on "
"multiplex of channel %1")
.arg(*activeScanNextChan));
}
activeScanNextTrig = QDateTime::currentDateTime()
.addSecs(activeScanTrigTime);
++activeScanNextChan;
// 24 hours ago
eitHelper->PruneEITCache(activeScanNextTrig.toTime_t() - 86400);
}
lock.lock();
if ((activeScan || activeScanStopped) && !exitThread)
exitThreadCond.wait(&lock, 400); // sleep up to 400 ms.
if (!activeScan && !activeScanStopped)
{
activeScanStopped = true;
activeScanCond.wakeAll();
}
}
activeScanStopped = true;
activeScanCond.wakeAll();
lock.unlock();
}
/** \fn EITScanner::RescheduleRecordings(void)
* \brief Tells scheduler about programming changes.
*
* This implements some very basic rate limiting. If a call is made
* to this within kMinRescheduleInterval of the last call it is ignored.
*/
void EITScanner::RescheduleRecordings(void)
{
if (!resched_lock.tryLock())
return;
if (resched_next_time > QDateTime::currentDateTime())
{
LOG(VB_EIT, LOG_INFO, LOC + "Rate limiting reschedules..");
resched_lock.unlock();
return;
}
resched_next_time =
QDateTime::currentDateTime().addSecs(kMinRescheduleInterval);
resched_lock.unlock();
ScheduledRecording::RescheduleMatch(0, 0, 0, QDateTime(), "EITScanner");
}
/** \fn EITScanner::StartPassiveScan(ChannelBase*, EITSource*, bool)
* \brief Start inserting Event Information Tables from the multiplex
* we happen to be tuned to into the database.
*/
void EITScanner::StartPassiveScan(ChannelBase *_channel,
EITSource *_eitSource)
{
QMutexLocker locker(&lock);
uint sourceid = _channel->GetCurrentSourceID();
eitSource = _eitSource;
channel = _channel;
eitHelper->SetSourceID(sourceid);
eitSource->SetEITHelper(eitHelper);
eitSource->SetEITRate(1.0f);
LOG(VB_EIT, LOG_INFO, LOC_ID + "Started passive scan.");
}
/** \fn EITScanner::StopPassiveScan(void)
* \brief Stops inserting Event Information Tables into DB.
*/
void EITScanner::StopPassiveScan(void)
{
QMutexLocker locker(&lock);
if (eitSource)
{
eitSource->SetEITHelper(NULL);
eitSource = NULL;
}
channel = NULL;
eitHelper->WriteEITCache();
eitHelper->SetSourceID(0);
}
void EITScanner::StartActiveScan(TVRec *_rec, uint max_seconds_per_source)
{
rec = _rec;
if (!activeScanChannels.size())
{
// TODO get input name and use it in crawl.
MSqlQuery query(MSqlQuery::InitCon());
query.prepare(
"SELECT channum, MIN(chanid) "
"FROM channel, cardinput, capturecard, videosource "
"WHERE cardinput.sourceid = channel.sourceid AND "
" videosource.sourceid = channel.sourceid AND "
" capturecard.cardid = cardinput.cardid AND "
" channel.mplexid IS NOT NULL AND "
" useonairguide = 1 AND "
" useeit = 1 AND "
" channum != '' AND "
" cardinput.cardid = :CARDID "
"GROUP BY mplexid "
"ORDER BY cardinput.sourceid, mplexid, "
" atsc_major_chan, atsc_minor_chan ");
query.bindValue(":CARDID", rec->GetCaptureCardNum());
if (!query.exec() || !query.isActive())
{
MythDB::DBError("EITScanner::StartActiveScan", query);
return;
}
while (query.next())
activeScanChannels.push_back(query.value(0).toString());
activeScanNextChan = activeScanChannels.begin();
}
LOG(VB_EIT, LOG_INFO, LOC_ID +
QString("StartActiveScan called with %1 multiplexes")
.arg(activeScanChannels.size()));
// Start at a random channel. This is so that multiple cards with
// the same source don't all scan the same channels in the same
// order when the backend is first started up.
if (activeScanChannels.size())
{
uint randomStart = random() % activeScanChannels.size();
activeScanNextChan = activeScanChannels.begin()+randomStart;
activeScanNextTrig = QDateTime::currentDateTime();
activeScanTrigTime = max_seconds_per_source;
// Add a little randomness to trigger time so multiple
// cards will have a staggered channel changing time.
activeScanTrigTime += random() % 29;
activeScanStopped = false;
activeScan = true;
}
}
void EITScanner::StopActiveScan(void)
{
QMutexLocker locker(&lock);
activeScanStopped = false;
activeScan = false;
exitThreadCond.wakeAll();
locker.unlock();
StopPassiveScan();
locker.relock();
while (!activeScan && !activeScanStopped)
activeScanCond.wait(&lock, 100);
rec = NULL;
}