-
Notifications
You must be signed in to change notification settings - Fork 341
/
mhi.cpp
1838 lines (1607 loc) · 55.6 KB
/
mhi.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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "mhi.h"
#include <unistd.h>
#include <QRegion>
#include <QBitArray>
#include <QVector>
#include <QUrl>
#include "interactivescreen.h"
#include "mythpainter.h"
#include "mythimage.h"
#include "mythuiimage.h"
#include "osd.h"
#include "mythdirs.h"
#include "myth_imgconvert.h"
#include "mythlogging.h"
static bool ft_loaded = false;
static FT_Library ft_library;
#define FONT_WIDTHRES 54
#define FONT_HEIGHTRES 72 // 1 pixel per point
#define FONT_TO_USE "FreeSans.ttf" // Tiresias Screenfont.ttf is mandated
#define SCALED_X(arg1) (int)(((float)arg1 * m_xScale) + 0.5f)
#define SCALED_Y(arg1) (int)(((float)arg1 * m_yScale) + 0.5f)
// LifecycleExtension tuneinfo:
const unsigned kTuneQuietly = 1U<<0; // b0 tune quietly
const unsigned kTuneKeepApp = 1U<<1; // b1 keep app running
const unsigned kTuneCarId = 1U<<2; // b2 carousel id in bits 8..16
const unsigned kTuneCarReset = 1U<<3; // b3 get carousel id from gateway info
const unsigned kTuneBcastDisa = 1U<<4; // b4 broadcaster_interrupt disable
// b5..7 reserverd
// b8..15 carousel id
// b16..31 reserved
const unsigned kTuneKeepChnl = 1U<<16; // Keep current channel
/** \class MHIImageData
* \brief Data for items in the interactive television display stack.
*/
class MHIImageData
{
public:
QImage m_image;
int m_x;
int m_y;
bool m_bUnder;
};
// Special value for the NetworkBootInfo version. Real values are a byte.
#define NBI_VERSION_UNSET 257
MHIContext::MHIContext(InteractiveTV *parent)
: m_parent(parent), m_dsmcc(NULL),
m_keyProfile(0),
m_engine(NULL), m_stop(false),
m_updated(false),
m_displayWidth(StdDisplayWidth), m_displayHeight(StdDisplayHeight),
m_face_loaded(false), m_engineThread(NULL), m_currentChannel(-1),
m_currentStream(-1), m_isLive(false), m_currentSource(-1),
m_audioTag(-1), m_videoTag(-1),
m_lastNbiVersion(NBI_VERSION_UNSET),
m_videoRect(0, 0, StdDisplayWidth, StdDisplayHeight),
m_displayRect(0, 0, StdDisplayWidth, StdDisplayHeight)
{
m_xScale = (float)m_displayWidth / (float)MHIContext::StdDisplayWidth;
m_yScale = (float)m_displayHeight / (float)MHIContext::StdDisplayHeight;
if (!ft_loaded)
{
FT_Error error = FT_Init_FreeType(&ft_library);
if (!error)
ft_loaded = true;
}
if (ft_loaded)
{
// TODO: We need bold and italic versions.
if (LoadFont(FONT_TO_USE))
m_face_loaded = true;
}
}
// Load the font. Copied, generally, from OSD::LoadFont.
bool MHIContext::LoadFont(QString name)
{
QString fullnameA = GetConfDir() + "/" + name;
QByteArray fnameA = fullnameA.toAscii();
FT_Error errorA = FT_New_Face(ft_library, fnameA.constData(), 0, &m_face);
if (!errorA)
return true;
QString fullnameB = GetFontsDir() + name;
QByteArray fnameB = fullnameB.toAscii();
FT_Error errorB = FT_New_Face(ft_library, fnameB.constData(), 0, &m_face);
if (!errorB)
return true;
QString fullnameC = GetShareDir() + "themes/" + name;
QByteArray fnameC = fullnameC.toAscii();
FT_Error errorC = FT_New_Face(ft_library, fnameC.constData(), 0, &m_face);
if (!errorC)
return true;
QString fullnameD = name;
QByteArray fnameD = fullnameD.toAscii();
FT_Error errorD = FT_New_Face(ft_library, fnameD.constData(), 0, &m_face);
if (!errorD)
return true;
LOG(VB_GENERAL, LOG_ERR, QString("[mhi] Unable to find font: %1").arg(name));
return false;
}
MHIContext::~MHIContext()
{
StopEngine();
delete(m_engine);
delete(m_dsmcc);
if (m_face_loaded) FT_Done_Face(m_face);
ClearDisplay();
ClearQueue();
}
void MHIContext::ClearDisplay(void)
{
list<MHIImageData*>::iterator it = m_display.begin();
for (; it != m_display.end(); ++it)
delete *it;
m_display.clear();
m_videoDisplayRect = QRect();
}
void MHIContext::ClearQueue(void)
{
MythDeque<DSMCCPacket*>::iterator it = m_dsmccQueue.begin();
for (; it != m_dsmccQueue.end(); ++it)
delete *it;
m_dsmccQueue.clear();
}
// Ask the engine to stop and block until it has.
void MHIContext::StopEngine(void)
{
if (NULL == m_engineThread)
return;
m_runLock.lock();
m_stop = true;
m_engine_wait.wakeAll();
m_runLock.unlock();
m_engineThread->wait();
delete m_engineThread;
m_engineThread = NULL;
}
// Start or restart the MHEG engine.
void MHIContext::Restart(int chanid, int sourceid, bool isLive)
{
int tuneinfo = m_tuneinfo.isEmpty() ? 0 : m_tuneinfo.takeFirst();
LOG(VB_MHEG, LOG_INFO,
QString("[mhi] Restart ch=%1 source=%2 live=%3 tuneinfo=0x%4")
.arg(chanid).arg(sourceid).arg(isLive).arg(tuneinfo,0,16));
m_currentSource = sourceid;
m_currentStream = chanid ? chanid : -1;
if (!(tuneinfo & kTuneKeepChnl))
m_currentChannel = m_currentStream;
if (tuneinfo & kTuneKeepApp)
{
// We have tuned to the channel in order to find the streams.
// Leave the MHEG engine running but restart the DSMCC carousel.
// This is a bit of a mess but it's the only way to be able to
// select streams from a different channel.
if (!m_dsmcc)
m_dsmcc = new Dsmcc();
{
QMutexLocker locker(&m_dsmccLock);
if (tuneinfo & kTuneCarReset)
m_dsmcc->Reset();
ClearQueue();
}
if (tuneinfo & (kTuneCarReset|kTuneCarId))
m_engine->EngineEvent(10); // NonDestructiveTuneOK
}
else
{
StopEngine();
m_audioTag = -1;
m_videoTag = -1;
if (!m_dsmcc)
m_dsmcc = new Dsmcc();
{
QMutexLocker locker(&m_dsmccLock);
m_dsmcc->Reset();
ClearQueue();
}
{
QMutexLocker locker(&m_keyLock);
m_keyQueue.clear();
}
if (!m_engine)
m_engine = MHCreateEngine(this);
m_engine->SetBooting();
ClearDisplay();
m_updated = true;
m_stop = false;
m_isLive = isLive;
// Don't set the NBI version here. Restart is called
// after the PMT is processed.
m_engineThread = new MThread("MHEG", this);
m_engineThread->start();
}
}
void MHIContext::run(void)
{
QMutexLocker locker(&m_runLock);
QTime t; t.start();
while (!m_stop)
{
locker.unlock();
int toWait;
// Dequeue and process any key presses.
int key = 0;
do
{
(void)NetworkBootRequested();
ProcessDSMCCQueue();
{
QMutexLocker locker(&m_keyLock);
key = m_keyQueue.dequeue();
}
if (key != 0)
m_engine->GenerateUserAction(key);
// Run the engine and find out how long to pause.
toWait = m_engine->RunAll();
if (toWait < 0)
return;
} while (key != 0);
toWait = (toWait > 1000 || toWait <= 0) ? 1000 : toWait;
locker.relock();
if (!m_stop && (toWait > 0))
m_engine_wait.wait(locker.mutex(), toWait);
}
}
// Dequeue and process any DSMCC packets.
void MHIContext::ProcessDSMCCQueue(void)
{
DSMCCPacket *packet = NULL;
do
{
{
QMutexLocker locker(&m_dsmccLock);
packet = m_dsmccQueue.dequeue();
}
if (packet)
{
m_dsmcc->ProcessSection(
packet->m_data, packet->m_length,
packet->m_componentTag, packet->m_carouselId,
packet->m_dataBroadcastId);
delete packet;
}
} while (packet);
}
void MHIContext::QueueDSMCCPacket(
unsigned char *data, int length, int componentTag,
unsigned carouselId, int dataBroadcastId)
{
unsigned char *dataCopy =
(unsigned char*) malloc(length * sizeof(unsigned char));
if (dataCopy == NULL)
return;
memcpy(dataCopy, data, length*sizeof(unsigned char));
{
QMutexLocker locker(&m_dsmccLock);
m_dsmccQueue.enqueue(new DSMCCPacket(dataCopy, length,
componentTag, carouselId,
dataBroadcastId));
}
QMutexLocker locker(&m_runLock);
m_engine_wait.wakeAll();
}
// A NetworkBootInfo sub-descriptor is present in the PMT.
void MHIContext::SetNetBootInfo(const unsigned char *data, uint length)
{
if (length < 2) // A valid descriptor should always have at least 2 bytes.
return;
LOG(VB_MHEG, LOG_INFO, QString("[mhi] SetNetBootInfo version %1 mode %2 len %3")
.arg(data[0]).arg(data[1]).arg(length));
QMutexLocker locker(&m_dsmccLock);
// Save the data from the descriptor.
m_nbiData.resize(0);
m_nbiData.reserve(length);
m_nbiData.insert(m_nbiData.begin(), data, data+length);
// If there is no Network Boot Info or we're setting it
// for the first time just update the "last version".
if (m_lastNbiVersion == NBI_VERSION_UNSET)
m_lastNbiVersion = data[0];
else
{
locker.unlock();
QMutexLocker locker2(&m_runLock);
m_engine_wait.wakeAll();
}
}
void MHIContext::NetworkBootRequested(void)
{
QMutexLocker locker(&m_dsmccLock);
if (m_nbiData.size() >= 2 && m_nbiData[0] != m_lastNbiVersion)
{
m_lastNbiVersion = m_nbiData[0]; // Update the saved version
switch (m_nbiData[1])
{
case 1:
m_dsmcc->Reset();
m_engine->SetBooting();
ClearDisplay();
m_updated = true;
break;
case 2:
m_engine->EngineEvent(9); // NetworkBootInfo EngineEvent
break;
default:
LOG(VB_MHEG, LOG_INFO, QString("[mhi] Unknown NetworkBoot type %1")
.arg(m_nbiData[1]));
break;
}
}
}
// Called by the engine to check for the presence of an object in the carousel.
bool MHIContext::CheckCarouselObject(QString objectPath)
{
if (objectPath.startsWith("http:") || objectPath.startsWith("https:"))
{
// TODO verify access to server in carousel file auth.servers
// TODO use TLS cert from carousel auth.tls.<x>
return m_ic.CheckFile(objectPath);
}
QStringList path = objectPath.split(QChar('/'), QString::SkipEmptyParts);
QByteArray result; // Unused
int res = m_dsmcc->GetDSMCCObject(path, result);
return res == 0; // It's available now.
}
// Called by the engine to request data from the carousel.
bool MHIContext::GetCarouselData(QString objectPath, QByteArray &result)
{
bool const isIC = objectPath.startsWith("http:") || objectPath.startsWith("https:");
// Get the path components. The string will normally begin with "//"
// since this is an absolute path but that will be removed by split.
QStringList path = objectPath.split(QChar('/'), QString::SkipEmptyParts);
// Since the DSMCC carousel and the MHEG engine are currently on the
// same thread this is safe. Otherwise we need to make a deep copy of
// the result.
QMutexLocker locker(&m_runLock);
bool bReported = false;
QTime t; t.start();
while (!m_stop)
{
locker.unlock();
if (isIC)
{
// TODO verify access to server in carousel file auth.servers
// TODO use TLS cert from carousel file auth.tls.<x>
switch (m_ic.GetFile(objectPath, result))
{
case MHInteractionChannel::kSuccess:
if (bReported)
LOG(VB_MHEG, LOG_INFO, QString("[mhi] Received %1").arg(objectPath));
return true;
case MHInteractionChannel::kError:
if (bReported)
LOG(VB_MHEG, LOG_INFO, QString("[mhi] Not found %1").arg(objectPath));
return false;
case MHInteractionChannel::kPending:
break;
}
}
else
{
int res = m_dsmcc->GetDSMCCObject(path, result);
if (res == 0)
{
if (bReported)
LOG(VB_MHEG, LOG_INFO, QString("[mhi] Received %1").arg(objectPath));
return true; // Found it
}
// NB don't exit if -1 (not present) is returned as the object may
// arrive later. Exiting can cause the inital app to not be found
}
if (t.elapsed() > 60000) // TODO get this from carousel info
return false; // Not there.
// Otherwise we block.
if (!bReported)
{
bReported = true;
LOG(VB_MHEG, LOG_INFO, QString("[mhi] Waiting for %1").arg(objectPath));
}
// Process DSMCC packets then block for a while or until we receive
// some more packets. We should eventually find out if this item is
// present.
ProcessDSMCCQueue();
locker.relock();
m_engine_wait.wait(locker.mutex(), 300);
}
return false; // Stop has been set. Say the object isn't present.
}
// Mapping from key name & UserInput register to UserInput EventData
class MHKeyLookup
{
typedef QPair< QString, int /*UserInput register*/ > key_t;
public:
MHKeyLookup();
int Find(const QString &name, int reg) const
{ return m_map.value(key_t(name,reg), 0); }
private:
void key(const QString &name, int code, int r1,
int r2=0, int r3=0, int r4=0, int r5=0, int r6=0, int r7=0, int r8=0, int r9=0);
QHash<key_t,int /*EventData*/ > m_map;
};
void MHKeyLookup::key(const QString &name, int code, int r1,
int r2, int r3, int r4, int r5, int r6, int r7, int r8, int r9)
{
m_map.insert(key_t(name,r1), code);
if (r2 > 0)
m_map.insert(key_t(name,r2), code);
if (r3 > 0)
m_map.insert(key_t(name,r3), code);
if (r4 > 0)
m_map.insert(key_t(name,r4), code);
if (r5 > 0)
m_map.insert(key_t(name,r5), code);
if (r6 > 0)
m_map.insert(key_t(name,r6), code);
if (r7 > 0)
m_map.insert(key_t(name,r7), code);
if (r8 > 0)
m_map.insert(key_t(name,r8), code);
if (r9 > 0)
m_map.insert(key_t(name,r9), code);
}
MHKeyLookup::MHKeyLookup()
{
// This supports the UK and NZ key profile registers.
// The UK uses 3, 4 and 5 and NZ 13, 14 and 15. These are
// similar but the NZ profile also provides an EPG key.
// ETSI ES 202 184 V2.2.1 (2011-03) adds group 6 for ICE.
// The BBC use group 7 for ICE
key(ACTION_UP, 1, 4,5,6,7,14,15);
key(ACTION_DOWN, 2, 4,5,6,7,14,15);
key(ACTION_LEFT, 3, 4,5,6,7,14,15);
key(ACTION_RIGHT, 4, 4,5,6,7,14,15);
key(ACTION_0, 5, 4,6,7,14);
key(ACTION_1, 6, 4,6,7,14);
key(ACTION_2, 7, 4,6,7,14);
key(ACTION_3, 8, 4,6,7,14);
key(ACTION_4, 9, 4,6,7,14);
key(ACTION_5, 10, 4,6,7,14);
key(ACTION_6, 11, 4,6,7,14);
key(ACTION_7, 12, 4,6,7,14);
key(ACTION_8, 13, 4,6,7,14);
key(ACTION_9, 14, 4,6,7,14);
key(ACTION_SELECT, 15, 4,5,6,7,14,15);
key(ACTION_TEXTEXIT, 16, 3,4,5,6,7,13,14,15); // 16= Cancel
// 17= help
// 18..99 reserved by DAVIC
key(ACTION_MENURED, 100, 3,4,5,6,7,13,14,15);
key(ACTION_MENUGREEN, 101, 3,4,5,6,7,13,14,15);
key(ACTION_MENUYELLOW, 102, 3,4,5,6,7,13,14,15);
key(ACTION_MENUBLUE, 103, 3,4,5,6,7,13,14,15);
key(ACTION_MENUTEXT, 104, 3,4,5,6,7);
key(ACTION_MENUTEXT, 105, 13,14,15); // NB from original Myth code
// 105..119 reserved for future spec
key(ACTION_STOP, 120, 6,7);
key(ACTION_PLAY, 121, 6,7);
key(ACTION_PAUSE, 122, 6,7);
key(ACTION_JUMPFFWD, 123, 6,7); // 123= Skip Forward
key(ACTION_JUMPRWND, 124, 6,7); // 124= Skip Back
#if 0 // These conflict with left & right
key(ACTION_SEEKFFWD, 125, 6,7); // 125= Fast Forward
key(ACTION_SEEKRWND, 126, 6,7); // 126= Rewind
#endif
key(ACTION_PLAYBACK, 127, 6,7);
// 128..256 reserved for future spec
// 257..299 vendor specific
key(ACTION_MENUEPG, 300, 13,14,15);
// 301.. Vendor specific
}
// Called from tv_play when a key is pressed.
// If it is one in the current profile we queue it for the engine
// and return true otherwise we return false.
bool MHIContext::OfferKey(QString key)
{
static const MHKeyLookup s_keymap;
int action = s_keymap.Find(key, m_keyProfile);
if (action == 0)
return false;
LOG(VB_GENERAL, LOG_INFO, QString("[mhi] Adding MHEG key %1:%2:%3")
.arg(key).arg(action).arg(m_keyQueue.size()) );
{ QMutexLocker locker(&m_keyLock);
m_keyQueue.enqueue(action);}
QMutexLocker locker2(&m_runLock);
m_engine_wait.wakeAll();
return true;
}
void MHIContext::Reinit(const QRect &display)
{
m_displayWidth = display.width();
m_displayHeight = display.height();
m_xScale = (float)m_displayWidth / (float)MHIContext::StdDisplayWidth;
m_yScale = (float)m_displayHeight / (float)MHIContext::StdDisplayHeight;
m_videoRect = QRect(QPoint(0,0), display.size());
m_displayRect = display;
}
void MHIContext::SetInputRegister(int num)
{
LOG(VB_MHEG, LOG_INFO, QString("[mhi] SetInputRegister %1").arg(num));
QMutexLocker locker(&m_keyLock);
m_keyQueue.clear();
m_keyProfile = num;
}
int MHIContext::GetICStatus()
{
// 0= Active, 1= Inactive, 2= Disabled
return m_ic.status();
}
// Called by the video player to redraw the image.
void MHIContext::UpdateOSD(InteractiveScreen *osdWindow,
MythPainter *osdPainter)
{
if (!osdWindow || !osdPainter)
return;
QMutexLocker locker(&m_display_lock);
// In MHEG the video is just another item in the display stack
// but when we create the OSD we overlay everything over the video.
// We need to cut out anything belowthe video on the display stack
// to leave the video area clear.
list<MHIImageData*>::iterator it = m_display.begin();
for (; it != m_display.end(); ++it)
{
MHIImageData *data = *it;
if (!data->m_bUnder)
continue;
QRect imageRect(data->m_x, data->m_y,
data->m_image.width(), data->m_image.height());
if (!m_videoDisplayRect.intersects(imageRect))
continue;
// Replace this item with a set of cut-outs.
it = m_display.erase(it);
QVector<QRect> rects =
(QRegion(imageRect) - QRegion(m_videoDisplayRect)).rects();
for (uint j = 0; j < (uint)rects.size(); j++)
{
QRect &rect = rects[j];
QImage image =
data->m_image.copy(rect.x()-data->m_x, rect.y()-data->m_y,
rect.width(), rect.height());
MHIImageData *newData = new MHIImageData;
newData->m_image = image;
newData->m_x = rect.x();
newData->m_y = rect.y();
newData->m_bUnder = true;
it = m_display.insert(it, newData);
++it;
}
--it;
delete data;
}
m_updated = false;
osdWindow->DeleteAllChildren();
// Copy all the display items into the display.
it = m_display.begin();
for (int count = 0; it != m_display.end(); ++it, count++)
{
MHIImageData *data = *it;
MythImage* image = osdPainter->GetFormatImage();
if (!image)
continue;
image->Assign(data->m_image);
MythUIImage *uiimage = new MythUIImage(osdWindow, QString("itv%1")
.arg(count));
if (uiimage)
{
uiimage->SetImage(image);
uiimage->SetArea(MythRect(data->m_x, data->m_y,
data->m_image.width(), data->m_image.height()));
}
image->DecrRef();
}
osdWindow->OptimiseDisplayedArea();
// N.B. bypasses OSD class hence no expiry set
osdWindow->SetVisible(true);
}
void MHIContext::GetInitialStreams(int &audioTag, int &videoTag)
{
audioTag = m_audioTag;
videoTag = m_videoTag;
}
// An area of the screen/image needs to be redrawn.
// Called from the MHEG engine.
// We always redraw the whole scene.
void MHIContext::RequireRedraw(const QRegion &)
{
m_display_lock.lock();
ClearDisplay();
m_display_lock.unlock();
// Always redraw the whole screen
m_engine->DrawDisplay(QRegion(0, 0, StdDisplayWidth, StdDisplayHeight));
m_updated = true;
}
void MHIContext::AddToDisplay(const QImage &image, int x, int y, bool bUnder /*=false*/)
{
MHIImageData *data = new MHIImageData;
int dispx = x + m_displayRect.left();
int dispy = y + m_displayRect.top();
data->m_image = image;
data->m_x = dispx;
data->m_y = dispy;
data->m_bUnder = bUnder;
QMutexLocker locker(&m_display_lock);
if (!bUnder)
m_display.push_back(data);
else
{
// Replace any existing items under the video with this
list<MHIImageData*>::iterator it = m_display.begin();
while (it != m_display.end())
{
MHIImageData *old = *it;
if (!old->m_bUnder)
++it;
else
{
it = m_display.erase(it);
delete old;
}
}
m_display.push_front(data);
}
}
// The videoRect gives the size and position to which the video must be scaled.
// The displayRect gives the rectangle reserved for the video.
// e.g. part of the video may be clipped within the displayRect.
void MHIContext::DrawVideo(const QRect &videoRect, const QRect &dispRect)
{
// tell the video player to resize the video stream
if (m_parent->GetNVP())
{
QRect vidRect(SCALED_X(videoRect.x()),
SCALED_Y(videoRect.y()),
SCALED_X(videoRect.width()),
SCALED_Y(videoRect.height()));
if (m_videoRect != vidRect)
{
m_parent->GetNVP()->SetVideoResize(vidRect.translated(m_displayRect.topLeft()));
m_videoRect = vidRect;
}
}
m_videoDisplayRect = QRect(SCALED_X(dispRect.x()),
SCALED_Y(dispRect.y()),
SCALED_X(dispRect.width()),
SCALED_Y(dispRect.height()));
// Mark all existing items in the display stack as under the video
QMutexLocker locker(&m_display_lock);
list<MHIImageData*>::iterator it = m_display.begin();
for (; it != m_display.end(); ++it)
{
(*it)->m_bUnder = true;
}
}
// Tuning. Get the index corresponding to a given channel.
// The format of the service is dvb://netID.[transPortID].serviceID
// where the IDs are in hex.
// or rec://svc/lcn/N where N is the "logical channel number"
// i.e. the Freeview channel.
// Returns -1 if it cannot find it.
int MHIContext::GetChannelIndex(const QString &str)
{
int nResult = -1;
do if (str.startsWith("dvb://"))
{
QStringList list = str.mid(6).split('.');
MSqlQuery query(MSqlQuery::InitCon());
if (list.size() != 3)
break; // Malformed.
// The various fields are expressed in hexadecimal.
// Convert them to decimal for the DB.
bool ok;
int netID = list[0].toInt(&ok, 16);
if (!ok)
break;
int serviceID = list[2].toInt(&ok, 16);
if (!ok)
break;
// We only return channels that match the current capture source.
if (list[1].isEmpty()) // TransportID is not specified
{
query.prepare(
"SELECT chanid "
"FROM channel, dtv_multiplex "
"WHERE networkid = :NETID AND"
" channel.mplexid = dtv_multiplex.mplexid AND "
" serviceid = :SERVICEID AND "
" channel.sourceid = :SOURCEID");
}
else
{
int transportID = list[1].toInt(&ok, 16);
if (!ok)
break;
query.prepare(
"SELECT chanid "
"FROM channel, dtv_multiplex "
"WHERE networkid = :NETID AND"
" channel.mplexid = dtv_multiplex.mplexid AND "
" serviceid = :SERVICEID AND "
" transportid = :TRANSID AND "
" channel.sourceid = :SOURCEID");
query.bindValue(":TRANSID", transportID);
}
query.bindValue(":NETID", netID);
query.bindValue(":SERVICEID", serviceID);
query.bindValue(":SOURCEID", m_currentSource);
if (query.exec() && query.isActive() && query.next())
nResult = query.value(0).toInt();
}
else if (str.startsWith("rec://svc/lcn/"))
{
// I haven't seen this yet so this is untested.
bool ok;
int channelNo = str.mid(14).toInt(&ok); // Decimal integer
if (!ok)
break;
MSqlQuery query(MSqlQuery::InitCon());
query.prepare("SELECT chanid "
"FROM channel "
"WHERE channum = :CHAN AND "
" channel.sourceid = :SOURCEID");
query.bindValue(":CHAN", channelNo);
query.bindValue(":SOURCEID", m_currentSource);
if (query.exec() && query.isActive() && query.next())
nResult = query.value(0).toInt();
}
else if (str == "rec://svc/cur")
nResult = m_currentStream > 0 ? m_currentStream : m_currentChannel;
else if (str == "rec://svc/def")
nResult = m_currentChannel;
else if (str.startsWith("rec://"))
;
while(0);
LOG(VB_MHEG, LOG_INFO, QString("[mhi] GetChannelIndex %1 => %2")
.arg(str).arg(nResult));
return nResult;
}
// Get netId etc from the channel index. This is the inverse of GetChannelIndex.
bool MHIContext::GetServiceInfo(int channelId, int &netId, int &origNetId,
int &transportId, int &serviceId)
{
MSqlQuery query(MSqlQuery::InitCon());
query.prepare("SELECT networkid, transportid, serviceid "
"FROM channel, dtv_multiplex "
"WHERE chanid = :CHANID AND "
" channel.mplexid = dtv_multiplex.mplexid");
query.bindValue(":CHANID", channelId);
if (query.exec() && query.isActive() && query.next())
{
netId = query.value(0).toInt();
origNetId = netId; // We don't have this in the database.
transportId = query.value(1).toInt();
serviceId = query.value(2).toInt();
LOG(VB_MHEG, LOG_INFO, QString("[mhi] GetServiceInfo %1 => NID=%2 TID=%3 SID=%4")
.arg(channelId).arg(netId).arg(transportId).arg(serviceId));
return true;
}
LOG(VB_MHEG, LOG_WARNING, QString("[mhi] GetServiceInfo %1 failed").arg(channelId));
return false;
}
bool MHIContext::TuneTo(int channel, int tuneinfo)
{
if (!m_isLive)
{
LOG(VB_MHEG, LOG_WARNING, QString("[mhi] Can't TuneTo %1 0x%2 while not live")
.arg(channel).arg(tuneinfo,0,16));
return false; // Can't tune if this is a recording.
}
LOG(VB_GENERAL, LOG_INFO, QString("[mhi] TuneTo %1 0x%2")
.arg(channel).arg(tuneinfo,0,16));
m_tuneinfo.append(tuneinfo);
// Post an event requesting a channel change.
MythEvent me(QString("NETWORK_CONTROL CHANID %1").arg(channel));
gCoreContext->dispatch(me);
// Reset the NBI version here to prevent a reboot.
QMutexLocker locker(&m_dsmccLock);
m_lastNbiVersion = NBI_VERSION_UNSET;
m_nbiData.resize(0);
return true;
}
// Begin playing the specified stream
bool MHIContext::BeginStream(const QString &stream, MHStream *notify)
{
LOG(VB_MHEG, LOG_INFO, QString("[mhi] BeginStream %1 0x%2")
.arg(stream).arg((quintptr)notify,0,16));
m_audioTag = -1;
m_videoTag = -1;
m_notify = notify;
if (stream.startsWith("http://") || stream.startsWith("https://"))
{
m_currentStream = -1;
// The url is sometimes only http:// during stream startup
if (QUrl(stream).authority().isEmpty())
return false;
return m_parent->GetNVP()->SetStream(stream);
}
int chan = GetChannelIndex(stream);
if (chan < 0)
return false;
if (VERBOSE_LEVEL_CHECK(VB_MHEG, LOG_ANY))
{
int netId, origNetId, transportId, serviceId;
GetServiceInfo(chan, netId, origNetId, transportId, serviceId);
}
if (chan != m_currentStream)
{
// We have to tune to the channel where the stream is to be found.
// Because the audio and video are both components of an MHEG stream
// they will both be on the same channel.
m_currentStream = chan;
return TuneTo(chan, kTuneKeepChnl|kTuneQuietly|kTuneKeepApp);
}
return true;
}
void MHIContext::EndStream()
{
LOG(VB_MHEG, LOG_INFO, QString("[mhi] EndStream 0x%1")
.arg((quintptr)m_notify,0,16) );
m_notify = 0;
(void)m_parent->GetNVP()->SetStream(QString());
}
// Callback from MythPlayer when a stream starts or stops
bool MHIContext::StreamStarted(bool bStarted)
{
if (!m_engine || !m_notify)
return false;
LOG(VB_MHEG, LOG_INFO, QString("[mhi] Stream 0x%1 %2")
.arg((quintptr)m_notify,0,16).arg(bStarted ? "started" : "stopped"));
m_engine->StreamStarted(m_notify, bStarted);
if (!bStarted)
m_notify = 0;
return m_currentStream == -1; // Return true if it's an http stream
}
// Begin playing audio
bool MHIContext::BeginAudio(int tag)
{
LOG(VB_MHEG, LOG_INFO, QString("[mhi] BeginAudio %1").arg(tag));
if (tag < 0)
return true; // Leave it at the default.
m_audioTag = tag;
if (m_parent->GetNVP())
return m_parent->GetNVP()->SetAudioByComponentTag(tag);
return false;
}
// Stop playing audio
void MHIContext::StopAudio()
{
// Do nothing at the moment.
}
// Begin displaying video from the specified stream
bool MHIContext::BeginVideo(int tag)
{
LOG(VB_MHEG, LOG_INFO, QString("[mhi] BeginVideo %1").arg(tag));
if (tag < 0)
return true; // Leave it at the default.
m_videoTag = tag;
if (m_parent->GetNVP())
return m_parent->GetNVP()->SetVideoByComponentTag(tag);
return false;
}
// Stop displaying video
void MHIContext::StopVideo()
{
// Do nothing at the moment.
}
// Get current stream position, -1 if unknown
long MHIContext::GetStreamPos()
{
return m_parent->GetNVP() ? m_parent->GetNVP()->GetStreamPos() : -1;
}
// Get current stream size, -1 if unknown
long MHIContext::GetStreamMaxPos()
{
return m_parent->GetNVP() ? m_parent->GetNVP()->GetStreamMaxPos() : -1;
}
// Set current stream position
long MHIContext::SetStreamPos(long pos)
{
return m_parent->GetNVP() ? m_parent->GetNVP()->SetStreamPos(pos) : -1;
}