/
tvbrowsehelper.cpp
561 lines (480 loc) · 17.1 KB
/
tvbrowsehelper.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
// Qt
#include <QCoreApplication>
// MythTV
#include "mythcorecontext.h"
#include "tvbrowsehelper.h"
#include "playercontext.h"
#include "remoteencoder.h"
#include "recordinginfo.h"
#include "channelutil.h"
#include "mythlogging.h"
#include "cardutil.h"
#include "tv_play.h"
#define LOC QString("BrowseHelper: ")
static void format_time(int seconds, QString& tMin, QString& tHrsMin)
{
int minutes = seconds / 60;
int hours = minutes / 60;
int min = minutes % 60;
tMin = TV::tr("%n minute(s)", "", minutes);
tHrsMin = QString("%1:%2").arg(hours).arg(min, 2, 10, QChar('0'));
}
TVBrowseHelper::TVBrowseHelper(TV* Parent)
: MThread("TVBrowseHelper"),
m_parent(Parent)
{
}
TVBrowseHelper::~TVBrowseHelper()
{
BrowseStop();
BrowseWait();
}
void TVBrowseHelper::BrowseInit(uint BrowseMaxForward, bool BrowseAllTuners,
bool UseChannelGroups, const QString &DBChannelOrdering)
{
m_dbBrowseMaxForward = BrowseMaxForward;
m_dbBrowseAllTuners = BrowseAllTuners;
m_dbUseChannelGroups = UseChannelGroups;
m_dbAllChannels = ChannelUtil::GetChannels(0, true, "channum, callsign");
ChannelUtil::SortChannels(m_dbAllChannels, DBChannelOrdering, false);
for (const auto & chan : m_dbAllChannels)
{
m_dbChanidToChannum[chan.m_chanId] = chan.m_chanNum;
m_dbChanidToSourceid[chan.m_chanId] = chan.m_sourceId;
m_dbChannumToChanids.insert(chan.m_chanNum,chan.m_chanId);
}
m_dbAllVisibleChannels = ChannelUtil::GetChannels(0, true, "channum, callsign");
ChannelUtil::SortChannels(m_dbAllVisibleChannels, DBChannelOrdering, false);
start();
}
void TVBrowseHelper::BrowseStop()
{
QMutexLocker locker(&m_browseLock);
m_browseList.clear();
m_browseRun = false;
m_browseWait.wakeAll();
}
void TVBrowseHelper::BrowseWait()
{
MThread::wait();
}
/// \brief Begins channel browsing.
/// \note This may only be called from the UI thread.
bool TVBrowseHelper::BrowseStart(bool SkipBrowse)
{
if (!gCoreContext->IsUIThread())
return false;
QMutexLocker locker(&m_browseLock);
if (m_browseTimerId)
return true;
m_parent->ClearOSD();
m_parent->GetPlayerReadLock();
PlayerContext* context = m_parent->GetPlayerContext();
context->LockPlayingInfo(__FILE__, __LINE__);
if (context->m_playingInfo)
{
m_browseChanNum = context->m_playingInfo->GetChanNum();
m_browseChanId = context->m_playingInfo->GetChanID();
m_browseStartTime = context->m_playingInfo->GetScheduledStartTime(MythDate::ISODate);
context->UnlockPlayingInfo(__FILE__, __LINE__);
m_parent->ReturnPlayerLock();
if (!SkipBrowse)
{
BrowseInfo bi(BROWSE_SAME, m_browseChanNum, m_browseChanId, m_browseStartTime);
locker.unlock();
BrowseDispInfo(bi);
}
return true;
}
context->UnlockPlayingInfo(__FILE__, __LINE__);
m_parent->ReturnPlayerLock();
return false;
}
/** \brief Ends channel browsing.
* Changing the channel if change_channel is true.
* \note This may only be called from the UI thread.
* \param change_channel iff true we call ChangeChannel()
*/
void TVBrowseHelper::BrowseEnd(bool ChangeChannel)
{
if (!gCoreContext->IsUIThread())
return;
QMutexLocker locker(&m_browseLock);
if (m_browseTimerId)
{
m_parent->KillTimer(m_browseTimerId);
m_browseTimerId = 0;
}
m_browseList.clear();
m_browseWait.wakeAll();
OSD* osd = m_parent->GetOSDL();
if (osd)
osd->HideWindow("browse_info");
m_parent->ReturnOSDLock();
if (ChangeChannel)
m_parent->ChangeChannel(0, m_browseChanNum);
}
void TVBrowseHelper::BrowseDispInfo(const BrowseInfo& Browseinfo)
{
if (!gCoreContext->IsUIThread())
return;
if (!BrowseStart(true))
return;
m_parent->KillTimer(m_browseTimerId);
m_browseTimerId = m_parent->StartTimer(static_cast<int>(TV::kBrowseTimeout), __LINE__);
QMutexLocker locker(&m_browseLock);
if (BROWSE_SAME == Browseinfo.m_dir)
m_browseList.clear();
m_browseList.push_back(Browseinfo);
m_browseWait.wakeAll();
}
void TVBrowseHelper::BrowseDispInfo(BrowseDirection Direction)
{
BrowseInfo bi(Direction);
if (BROWSE_SAME != Direction)
BrowseDispInfo(bi);
}
void TVBrowseHelper::BrowseChannel(const QString& Channum)
{
if (!gCoreContext->IsUIThread())
return;
if (m_dbBrowseAllTuners)
{
BrowseInfo bi(Channum, 0);
BrowseDispInfo(bi);
return;
}
m_parent->GetPlayerReadLock();
PlayerContext* context = m_parent->GetPlayerContext();
if (!context->m_recorder || !context->m_lastCardid)
{
m_parent->ReturnPlayerLock();
return;
}
uint inputid = static_cast<uint>(context->m_lastCardid);
m_parent->ReturnPlayerLock();
uint sourceid = CardUtil::GetSourceID(inputid);
if (sourceid)
{
BrowseInfo bi(Channum, sourceid);
BrowseDispInfo(bi);
}
}
BrowseInfo TVBrowseHelper::GetBrowsedInfo() const
{
QMutexLocker locker(&m_browseLock);
BrowseInfo bi(BROWSE_SAME);
bi.m_chanNum = m_browseChanNum;
bi.m_chanId = m_browseChanId;
bi.m_startTime = m_browseStartTime;
return bi;
}
/**
* \note This may only be called from the UI thread.
*/
bool TVBrowseHelper::IsBrowsing() const
{
if (!gCoreContext->IsUIThread())
return true;
return m_browseTimerId != 0;
}
/** \brief Returns a chanid for the channum, or 0 if none is available.
*
* This will prefer a given sourceid first, and then a given card id,
* but if one or the other can not be satisfied but m_dbBrowseAllTuners
* is set then it will look to see if the chanid is available on any
* tuner.
*/
uint TVBrowseHelper::GetBrowseChanId(const QString& Channum, uint PrefCardid, uint PrefSourceid) const
{
if (PrefSourceid) {
for (const auto & chan : m_dbAllChannels)
if (chan.m_sourceId == PrefSourceid && chan.m_chanNum == Channum)
return chan.m_chanId;
}
if (PrefCardid) {
for (const auto & chan : m_dbAllChannels)
if (chan.GetInputIds().contains(PrefCardid) && chan.m_chanNum == Channum)
return chan.m_chanId;
}
if (m_dbBrowseAllTuners)
{
for (const auto & chan : m_dbAllChannels)
if (chan.m_chanNum == Channum)
return chan.m_chanId;
}
return 0;
}
/**
* \brief Fetches information on the desired program from the backend.
* \param direction BrowseDirection to get information on.
* \param infoMap InfoMap to fill in with returned data
*/
void TVBrowseHelper::GetNextProgram(BrowseDirection Direction, InfoMap& Infomap) const
{
m_parent->GetPlayerReadLock();
PlayerContext* context = m_parent->GetPlayerContext();
if (!context->m_recorder)
{
m_parent->ReturnPlayerLock();
return;
}
QString title;
QString subtitle;
QString desc;
QString category;
QString endtime;
QString callsign;
QString iconpath;
QDateTime begts;
QDateTime endts;
QString starttime = Infomap["dbstarttime"];
QString chanid = Infomap["chanid"];
QString channum = Infomap["channum"];
QString seriesid = Infomap["seriesid"];
QString programid = Infomap["programid"];
context->m_recorder->GetNextProgram(Direction, title, subtitle, desc, category,
starttime, endtime, callsign, iconpath,
channum, chanid, seriesid, programid);
m_parent->ReturnPlayerLock();
if (!starttime.isEmpty())
begts = MythDate::fromString(starttime);
else
begts = MythDate::fromString(Infomap["dbstarttime"]);
Infomap["starttime"] = MythDate::toString(begts, MythDate::kTime);
Infomap["startdate"] = MythDate::toString(begts, MythDate::kDateFull | MythDate::kSimplify);
Infomap["endtime"] = Infomap["enddate"] = "";
if (!endtime.isEmpty())
{
endts = MythDate::fromString(endtime);
Infomap["endtime"] = MythDate::toString(endts, MythDate::kTime);
Infomap["enddate"] = MythDate::toString(endts, MythDate::kDateFull | MythDate::kSimplify);
}
Infomap["lenmins"] = TV::tr("%n minute(s)", "", 0);
Infomap["lentime"] = "0:00";
if (begts.isValid() && endts.isValid())
{
QString lenM;
QString lenHM;
format_time(static_cast<int>(begts.secsTo(endts)), lenM, lenHM);
Infomap["lenmins"] = lenM;
Infomap["lentime"] = lenHM;
}
Infomap["dbstarttime"] = starttime;
Infomap["dbendtime"] = endtime;
Infomap["title"] = title;
Infomap["subtitle"] = subtitle;
Infomap["description"] = desc;
Infomap["category"] = category;
Infomap["callsign"] = callsign;
Infomap["channum"] = channum;
Infomap["chanid"] = chanid;
Infomap["iconpath"] = iconpath;
Infomap["seriesid"] = seriesid;
Infomap["programid"] = programid;
}
void TVBrowseHelper::GetNextProgramDB(BrowseDirection direction, InfoMap& Infomap) const
{
uint chanid = Infomap["chanid"].toUInt();
if (!chanid)
{
LOG(VB_GENERAL, LOG_ERR, LOC + "GetNextProgramDB() requires a chanid");
return;
}
int chandir = -1;
switch (direction)
{
case BROWSE_UP: chandir = CHANNEL_DIRECTION_UP; break;
case BROWSE_DOWN: chandir = CHANNEL_DIRECTION_DOWN; break;
case BROWSE_FAVORITE: chandir = CHANNEL_DIRECTION_FAVORITE; break;
case BROWSE_SAME:
case BROWSE_LEFT:
case BROWSE_RIGHT:
case BROWSE_INVALID: break;
}
if (chandir != -1)
{
chanid = ChannelUtil::GetNextChannel(m_dbAllVisibleChannels, chanid, 0 /*mplexid_restriction*/,
0 /* chanid restriction */,
static_cast<ChannelChangeDirection>(chandir),
true /*skip non visible*/, true /*skip same callsign*/);
}
Infomap["chanid"] = QString::number(chanid);
Infomap["channum"] = m_dbChanidToChannum[chanid];
QDateTime nowtime = MythDate::current();
QDateTime latesttime = nowtime.addSecs(6*60*60);
QDateTime browsetime = MythDate::fromString(Infomap["dbstarttime"]);
MSqlBindings bindings;
bindings[":CHANID"] = chanid;
bindings[":NOWTS"] = nowtime;
bindings[":LATESTTS"] = latesttime;
bindings[":BROWSETS"] = browsetime;
bindings[":BROWSETS2"] = browsetime;
QString querystr = " WHERE program.chanid = :CHANID ";
switch (direction)
{
case BROWSE_LEFT:
querystr += " AND program.endtime <= :BROWSETS AND program.endtime > :NOWTS ";
break;
case BROWSE_RIGHT:
querystr += " AND program.starttime > :BROWSETS AND program.starttime < :LATESTTS ";
break;
default:
querystr += " AND program.starttime <= :BROWSETS AND program.endtime > :BROWSETS2 ";
};
ProgramList progList;
ProgramList dummySched;
LoadFromProgram(progList, querystr, bindings, dummySched);
if (progList.empty())
{
Infomap["dbstarttime"] = "";
return;
}
const ProgramInfo* prog = (direction == BROWSE_LEFT) ?
progList[static_cast<uint>(progList.size() - 1)] : progList[0];
Infomap["dbstarttime"] = prog->GetScheduledStartTime(MythDate::ISODate);
}
void TVBrowseHelper::run()
{
RunProlog();
LOG(VB_PLAYBACK, LOG_INFO, LOC + "Helper thread starting");
QMutexLocker locker(&m_browseLock);
while (true)
{
while (m_browseList.empty() && m_browseRun)
m_browseWait.wait(&m_browseLock);
if (!m_browseRun)
break;
BrowseInfo bi = m_browseList.front();
m_browseList.pop_front();
std::vector<uint> chanids;
if (BROWSE_SAME == bi.m_dir)
{
if (!bi.m_chanId)
{
std::vector<uint> chanids_extra;
uint sourceid = m_dbChanidToSourceid[m_browseChanId];
QMultiMap<QString,uint>::iterator it;
it = m_dbChannumToChanids.lowerBound(bi.m_chanNum);
for ( ; (it != m_dbChannumToChanids.end()) &&
(it.key() == bi.m_chanNum); ++it)
{
if (m_dbChanidToSourceid[*it] == sourceid)
chanids.push_back(*it);
else
chanids_extra.push_back(*it);
}
chanids.insert(chanids.end(),
chanids_extra.begin(),
chanids_extra.end());
}
m_browseChanNum = bi.m_chanNum;
m_browseChanId = (chanids.empty()) ? bi.m_chanId : chanids[0];
m_browseStartTime = bi.m_startTime;
}
BrowseDirection direction = bi.m_dir;
QDateTime lasttime = MythDate::fromString(m_browseStartTime);
QDateTime curtime = MythDate::current();
if (lasttime < curtime)
m_browseStartTime = curtime.toString(Qt::ISODate);
QDateTime maxtime = curtime.addSecs(m_dbBrowseMaxForward);
if ((lasttime > maxtime) && (direction == BROWSE_RIGHT))
continue;
m_browseLock.unlock();
// if browsing channel groups is enabled or
// direction if BROWSE_FAVORITES
// Then pick the next channel in the channel group list to browse
// If channel group is ALL CHANNELS (-1), then bypass picking from
// the channel group list
if ((m_dbUseChannelGroups || (direction == BROWSE_FAVORITE)) &&
(direction != BROWSE_RIGHT) && (direction != BROWSE_LEFT) &&
(direction != BROWSE_SAME))
{
m_parent->m_channelGroupLock.lock();
if (m_parent->m_channelGroupId > -1)
{
ChannelChangeDirection dir = CHANNEL_DIRECTION_SAME;
if ((direction == BROWSE_UP) || (direction == BROWSE_FAVORITE))
dir = CHANNEL_DIRECTION_UP;
else if (direction == BROWSE_DOWN)
dir = CHANNEL_DIRECTION_DOWN;
uint chanid = ChannelUtil::GetNextChannel(m_parent->m_channelGroupChannelList, m_browseChanId, 0, 0, dir);
direction = BROWSE_SAME;
m_parent->m_channelGroupLock.unlock();
m_browseLock.lock();
m_browseChanId = chanid;
m_browseChanNum.clear();
m_browseLock.unlock();
}
else
{
m_parent->m_channelGroupLock.unlock();
}
}
if (direction == BROWSE_FAVORITE)
direction = BROWSE_UP;
InfoMap infoMap;
infoMap["dbstarttime"] = m_browseStartTime;
infoMap["channum"] = m_browseChanNum;
infoMap["chanid"] = QString::number(m_browseChanId);
m_parent->GetPlayerReadLock();
if (!m_dbBrowseAllTuners)
{
GetNextProgram(direction, infoMap);
}
else
{
if (!chanids.empty())
{
for (uint chanid : chanids)
{
if (TV::IsTunable(chanid))
{
infoMap["chanid"] = QString::number(chanid);
GetNextProgramDB(direction, infoMap);
break;
}
}
}
else
{
uint orig_chanid = infoMap["chanid"].toUInt();
GetNextProgramDB(direction, infoMap);
while (!TV::IsTunable(infoMap["chanid"].toUInt()) &&
(infoMap["chanid"].toUInt() != orig_chanid))
{
GetNextProgramDB(direction, infoMap);
}
}
}
m_parent->ReturnPlayerLock();
m_browseLock.lock();
m_browseChanNum = infoMap["channum"];
m_browseChanId = infoMap["chanid"].toUInt();
if (((direction == BROWSE_LEFT) || (direction == BROWSE_RIGHT)) &&
!infoMap["dbstarttime"].isEmpty())
{
m_browseStartTime = infoMap["dbstarttime"];
}
if (!m_browseList.empty())
{
// send partial info to UI for appearance of responsiveness
QCoreApplication::postEvent(m_parent, new UpdateBrowseInfoEvent(infoMap));
continue;
}
m_browseLock.unlock();
// pull in additional data from the DB...
if (m_parent->m_channelGroupId > -1 && m_dbUseChannelGroups)
infoMap["channelgroup"] = ChannelGroup::GetChannelGroupName(m_parent->m_channelGroupId);
else
infoMap["channelgroup"] = QObject::tr("All channels");
QDateTime startts = MythDate::fromString(m_browseStartTime);
RecordingInfo recinfo(m_browseChanId, startts, false);
recinfo.ToMap(infoMap);
infoMap["iconpath"] = ChannelUtil::GetIcon(recinfo.GetChanID());
m_browseLock.lock();
QCoreApplication::postEvent(m_parent, new UpdateBrowseInfoEvent(infoMap));
}
LOG(VB_PLAYBACK, LOG_INFO, LOC + "Helper thread exiting");
RunEpilog();
}