-
Notifications
You must be signed in to change notification settings - Fork 2
/
device.c
1808 lines (1651 loc) · 54.7 KB
/
device.c
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
/*
* device.c: The basic device interface
*
* See the main source file 'vdr.c' for copyright information and
* how to reach the author.
*
* $Id: device.c 4.2 2015/09/05 11:42:17 kls Exp $
*/
#include "device.h"
#include <errno.h>
#include <math.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include "audio.h"
#include "channels.h"
#include "i18n.h"
#include "player.h"
#include "receiver.h"
#include "status.h"
#include "transfer.h"
// --- cLiveSubtitle ---------------------------------------------------------
class cLiveSubtitle : public cReceiver {
protected:
virtual void Receive(const uchar *Data, int Length);
public:
cLiveSubtitle(int SPid);
virtual ~cLiveSubtitle();
};
cLiveSubtitle::cLiveSubtitle(int SPid)
{
AddPid(SPid);
}
cLiveSubtitle::~cLiveSubtitle()
{
cReceiver::Detach();
}
void cLiveSubtitle::Receive(const uchar *Data, int Length)
{
if (cDevice::PrimaryDevice())
cDevice::PrimaryDevice()->PlayTs(Data, Length);
}
// --- cDeviceHook -----------------------------------------------------------
cDeviceHook::cDeviceHook(void)
{
cDevice::deviceHooks.Add(this);
}
bool cDeviceHook::DeviceProvidesTransponder(const cDevice *Device, const cChannel *Channel) const
{
return true;
}
// --- cDevice ---------------------------------------------------------------
// The minimum number of unknown PS1 packets to consider this a "pre 1.3.19 private stream":
#define MIN_PRE_1_3_19_PRIVATESTREAM 10
int cDevice::numDevices = 0;
int cDevice::useDevice = 0;
int cDevice::nextCardIndex = 0;
int cDevice::currentChannel = 1;
cDevice *cDevice::device[MAXDEVICES] = { NULL };
cDevice *cDevice::primaryDevice = NULL;
cList<cDeviceHook> cDevice::deviceHooks;
cDevice::cDevice(void)
:patPmtParser(true)
{
cardIndex = nextCardIndex++;
dsyslog("new device number %d", CardIndex() + 1);
SetDescription("device %d receiver", CardIndex() + 1);
mute = false;
volume = Setup.CurrentVolume;
sectionHandler = NULL;
eitFilter = NULL;
patFilter = NULL;
sdtFilter = NULL;
nitFilter = NULL;
camSlot = NULL;
startScrambleDetection = 0;
occupiedTimeout = 0;
player = NULL;
isPlayingVideo = false;
keepTracks = false; // used in ClrAvailableTracks()!
ClrAvailableTracks();
currentAudioTrack = ttNone;
currentAudioTrackMissingCount = 0;
currentSubtitleTrack = ttNone;
liveSubtitle = NULL;
dvbSubtitleConverter = NULL;
autoSelectPreferredSubtitleLanguage = true;
for (int i = 0; i < MAXRECEIVERS; i++)
receiver[i] = NULL;
if (numDevices < MAXDEVICES)
device[numDevices++] = this;
else
esyslog("ERROR: too many devices!");
}
cDevice::~cDevice()
{
Detach(player);
DetachAllReceivers();
delete liveSubtitle;
delete dvbSubtitleConverter;
if (this == primaryDevice)
primaryDevice = NULL;
}
bool cDevice::WaitForAllDevicesReady(int Timeout)
{
for (time_t t0 = time(NULL); time(NULL) - t0 < Timeout; ) {
bool ready = true;
for (int i = 0; i < numDevices; i++) {
if (device[i] && !device[i]->Ready()) {
ready = false;
cCondWait::SleepMs(100);
}
}
if (ready)
return true;
}
return false;
}
void cDevice::SetUseDevice(int n)
{
if (n < MAXDEVICES)
useDevice |= (1 << n);
}
int cDevice::NextCardIndex(int n)
{
if (n > 0) {
nextCardIndex += n;
if (nextCardIndex >= MAXDEVICES)
esyslog("ERROR: nextCardIndex too big (%d)", nextCardIndex);
}
else if (n < 0)
esyslog("ERROR: invalid value in nextCardIndex(%d)", n);
return nextCardIndex;
}
int cDevice::DeviceNumber(void) const
{
for (int i = 0; i < numDevices; i++) {
if (device[i] == this)
return i;
}
return -1;
}
cString cDevice::DeviceType(void) const
{
return "";
}
cString cDevice::DeviceName(void) const
{
return "";
}
void cDevice::MakePrimaryDevice(bool On)
{
if (!On) {
DELETENULL(liveSubtitle);
DELETENULL(dvbSubtitleConverter);
}
}
bool cDevice::SetPrimaryDevice(int n)
{
n--;
if (0 <= n && n < numDevices && device[n]) {
isyslog("setting primary device to %d", n + 1);
if (primaryDevice)
primaryDevice->MakePrimaryDevice(false);
primaryDevice = device[n];
primaryDevice->MakePrimaryDevice(true);
primaryDevice->SetVideoFormat(Setup.VideoFormat);
primaryDevice->SetVolumeDevice(Setup.CurrentVolume);
return true;
}
esyslog("ERROR: invalid primary device number: %d", n + 1);
return false;
}
bool cDevice::HasDecoder(void) const
{
return false;
}
cSpuDecoder *cDevice::GetSpuDecoder(void)
{
return NULL;
}
cDevice *cDevice::ActualDevice(void)
{
cDevice *d = cTransferControl::ReceiverDevice();
if (!d)
d = PrimaryDevice();
return d;
}
cDevice *cDevice::GetDevice(int Index)
{
return (0 <= Index && Index < numDevices) ? device[Index] : NULL;
}
static int GetClippedNumProvidedSystems(int AvailableBits, cDevice *Device)
{
int MaxNumProvidedSystems = (1 << AvailableBits) - 1;
int NumProvidedSystems = Device->NumProvidedSystems();
if (NumProvidedSystems > MaxNumProvidedSystems) {
esyslog("ERROR: device %d supports %d modulation systems but cDevice::GetDevice() currently only supports %d delivery systems which should be fixed", Device->CardIndex() + 1, NumProvidedSystems, MaxNumProvidedSystems);
NumProvidedSystems = MaxNumProvidedSystems;
}
else if (NumProvidedSystems <= 0) {
esyslog("ERROR: device %d reported an invalid number (%d) of supported delivery systems - assuming 1", Device->CardIndex() + 1, NumProvidedSystems);
NumProvidedSystems = 1;
}
return NumProvidedSystems;
}
cDevice *cDevice::GetDevice(const cChannel *Channel, int Priority, bool LiveView, bool Query)
{
// Collect the current priorities of all CAM slots that can decrypt the channel:
int NumCamSlots = CamSlots.Count();
int SlotPriority[NumCamSlots];
int NumUsableSlots = 0;
bool InternalCamNeeded = false;
if (Channel->Ca() >= CA_ENCRYPTED_MIN) {
for (cCamSlot *CamSlot = CamSlots.First(); CamSlot; CamSlot = CamSlots.Next(CamSlot)) {
SlotPriority[CamSlot->Index()] = MAXPRIORITY + 1; // assumes it can't be used
if (CamSlot->ModuleStatus() == msReady) {
if (CamSlot->ProvidesCa(Channel->Caids())) {
if (!ChannelCamRelations.CamChecked(Channel->GetChannelID(), CamSlot->SlotNumber())) {
SlotPriority[CamSlot->Index()] = CamSlot->Priority();
NumUsableSlots++;
}
}
}
}
if (!NumUsableSlots)
InternalCamNeeded = true; // no CAM is able to decrypt this channel
}
bool NeedsDetachReceivers = false;
cDevice *d = NULL;
cCamSlot *s = NULL;
uint32_t Impact = 0xFFFFFFFF; // we're looking for a device with the least impact
for (int j = 0; j < NumCamSlots || !NumUsableSlots; j++) {
if (NumUsableSlots && SlotPriority[j] > MAXPRIORITY)
continue; // there is no CAM available in this slot
for (int i = 0; i < numDevices; i++) {
if (Channel->Ca() && Channel->Ca() <= CA_DVB_MAX && Channel->Ca() != device[i]->CardIndex() + 1)
continue; // a specific card was requested, but not this one
bool HasInternalCam = device[i]->HasInternalCam();
if (InternalCamNeeded && !HasInternalCam)
continue; // no CAM is able to decrypt this channel and the device uses vdr handled CAMs
if (NumUsableSlots && !HasInternalCam && !CamSlots.Get(j)->Assign(device[i], true))
continue; // CAM slot can't be used with this device
bool ndr;
if (device[i]->ProvidesChannel(Channel, Priority, &ndr)) { // this device is basically able to do the job
if (NumUsableSlots && !HasInternalCam && device[i]->CamSlot() && device[i]->CamSlot() != CamSlots.Get(j))
ndr = true; // using a different CAM slot requires detaching receivers
// Put together an integer number that reflects the "impact" using
// this device would have on the overall system. Each condition is represented
// by one bit in the number (or several bits, if the condition is actually
// a numeric value). The sequence in which the conditions are listed corresponds
// to their individual severity, where the one listed first will make the most
// difference, because it results in the most significant bit of the result.
uint32_t imp = 0;
imp <<= 1; imp |= LiveView ? !device[i]->IsPrimaryDevice() || ndr : 0; // prefer the primary device for live viewing if we don't need to detach existing receivers
imp <<= 1; imp |= !device[i]->Receiving() && (device[i] != cTransferControl::ReceiverDevice() || device[i]->IsPrimaryDevice()) || ndr; // use receiving devices if we don't need to detach existing receivers, but avoid primary device in local transfer mode
imp <<= 1; imp |= device[i]->Receiving(); // avoid devices that are receiving
imp <<= 4; imp |= GetClippedNumProvidedSystems(4, device[i]) - 1; // avoid cards which support multiple delivery systems
imp <<= 1; imp |= device[i] == cTransferControl::ReceiverDevice(); // avoid the Transfer Mode receiver device
imp <<= 8; imp |= device[i]->Priority() - IDLEPRIORITY; // use the device with the lowest priority (- IDLEPRIORITY to assure that values -100..99 can be used)
imp <<= 8; imp |= ((NumUsableSlots && !HasInternalCam) ? SlotPriority[j] : IDLEPRIORITY) - IDLEPRIORITY;// use the CAM slot with the lowest priority (- IDLEPRIORITY to assure that values -100..99 can be used)
imp <<= 1; imp |= ndr; // avoid devices if we need to detach existing receivers
imp <<= 1; imp |= (NumUsableSlots || InternalCamNeeded) ? 0 : device[i]->HasCi(); // avoid cards with Common Interface for FTA channels
imp <<= 1; imp |= device[i]->AvoidRecording(); // avoid SD full featured cards
imp <<= 1; imp |= (NumUsableSlots && !HasInternalCam) ? !ChannelCamRelations.CamDecrypt(Channel->GetChannelID(), j + 1) : 0; // prefer CAMs that are known to decrypt this channel
imp <<= 1; imp |= device[i]->IsPrimaryDevice(); // avoid the primary device
if (imp < Impact) {
// This device has less impact than any previous one, so we take it.
Impact = imp;
d = device[i];
NeedsDetachReceivers = ndr;
if (NumUsableSlots && !HasInternalCam)
s = CamSlots.Get(j);
}
}
}
if (!NumUsableSlots)
break; // no CAM necessary, so just one loop over the devices
}
if (d && !Query) {
if (NeedsDetachReceivers)
d->DetachAllReceivers();
if (s) {
if (s->Device() != d) {
if (s->Device())
s->Device()->DetachAllReceivers();
if (d->CamSlot())
d->CamSlot()->Assign(NULL);
s->Assign(d);
}
}
else if (d->CamSlot() && !d->CamSlot()->IsDecrypting())
d->CamSlot()->Assign(NULL);
}
return d;
}
cDevice *cDevice::GetDeviceForTransponder(const cChannel *Channel, int Priority)
{
cDevice *Device = NULL;
for (int i = 0; i < cDevice::NumDevices(); i++) {
if (cDevice *d = cDevice::GetDevice(i)) {
if (d->IsTunedToTransponder(Channel))
return d; // if any device is tuned to the transponder, we're done
if (d->ProvidesTransponder(Channel)) {
if (d->MaySwitchTransponder(Channel))
Device = d; // this device may switch to the transponder without disturbing any receiver or live view
else if (!d->Occupied() && d->MaySwitchTransponder(Channel)) { // MaySwitchTransponder() implicitly calls Occupied()
if (d->Priority() < Priority && (!Device || d->Priority() < Device->Priority()))
Device = d; // use this one only if no other with less impact can be found
}
}
}
}
return Device;
}
bool cDevice::HasCi(void)
{
return false;
}
void cDevice::SetCamSlot(cCamSlot *CamSlot)
{
LOCK_THREAD;
camSlot = CamSlot;
}
void cDevice::Shutdown(void)
{
deviceHooks.Clear();
for (int i = 0; i < numDevices; i++) {
delete device[i];
device[i] = NULL;
}
}
uchar *cDevice::GrabImage(int &Size, bool Jpeg, int Quality, int SizeX, int SizeY)
{
return NULL;
}
bool cDevice::GrabImageFile(const char *FileName, bool Jpeg, int Quality, int SizeX, int SizeY)
{
int result = 0;
int fd = open(FileName, O_WRONLY | O_CREAT | O_NOFOLLOW | O_TRUNC, DEFFILEMODE);
if (fd >= 0) {
int ImageSize;
uchar *Image = GrabImage(ImageSize, Jpeg, Quality, SizeX, SizeY);
if (Image) {
if (safe_write(fd, Image, ImageSize) == ImageSize)
isyslog("grabbed image to %s", FileName);
else {
LOG_ERROR_STR(FileName);
result |= 1;
}
free(Image);
}
else
result |= 1;
close(fd);
}
else {
LOG_ERROR_STR(FileName);
result |= 1;
}
return result == 0;
}
void cDevice::SetVideoDisplayFormat(eVideoDisplayFormat VideoDisplayFormat)
{
cSpuDecoder *spuDecoder = GetSpuDecoder();
if (spuDecoder) {
if (Setup.VideoFormat)
spuDecoder->setScaleMode(cSpuDecoder::eSpuNormal);
else {
switch (VideoDisplayFormat) {
case vdfPanAndScan:
spuDecoder->setScaleMode(cSpuDecoder::eSpuPanAndScan);
break;
case vdfLetterBox:
spuDecoder->setScaleMode(cSpuDecoder::eSpuLetterBox);
break;
case vdfCenterCutOut:
spuDecoder->setScaleMode(cSpuDecoder::eSpuNormal);
break;
default: esyslog("ERROR: invalid value for VideoDisplayFormat '%d'", VideoDisplayFormat);
}
}
}
}
void cDevice::SetVideoFormat(bool VideoFormat16_9)
{
}
void cDevice::GetVideoSize(int &Width, int &Height, double &VideoAspect)
{
Width = 0;
Height = 0;
VideoAspect = 1.0;
}
void cDevice::GetOsdSize(int &Width, int &Height, double &PixelAspect)
{
Width = 720;
Height = 480;
PixelAspect = 1.0;
}
//#define PRINTPIDS(s) { char b[500]; char *q = b; q += sprintf(q, "%d %s ", CardIndex(), s); for (int i = 0; i < MAXPIDHANDLES; i++) q += sprintf(q, " %s%4d %d", i == ptOther ? "* " : "", pidHandles[i].pid, pidHandles[i].used); dsyslog("%s", b); }
#define PRINTPIDS(s)
bool cDevice::HasPid(int Pid) const
{
for (int i = 0; i < MAXPIDHANDLES; i++) {
if (pidHandles[i].pid == Pid)
return true;
}
return false;
}
bool cDevice::AddPid(int Pid, ePidType PidType, int StreamType)
{
if (Pid || PidType == ptPcr) {
int n = -1;
int a = -1;
if (PidType != ptPcr) { // PPID always has to be explicit
for (int i = 0; i < MAXPIDHANDLES; i++) {
if (i != ptPcr) {
if (pidHandles[i].pid == Pid)
n = i;
else if (a < 0 && i >= ptOther && !pidHandles[i].used)
a = i;
}
}
}
if (n >= 0) {
// The Pid is already in use
if (++pidHandles[n].used == 2 && n <= ptTeletext) {
// It's a special PID that may have to be switched into "tap" mode
PRINTPIDS("A");
if (!SetPid(&pidHandles[n], n, true)) {
esyslog("ERROR: can't set PID %d on device %d", Pid, CardIndex() + 1);
if (PidType <= ptTeletext)
DetachAll(Pid);
DelPid(Pid, PidType);
return false;
}
if (camSlot)
camSlot->SetPid(Pid, true);
}
PRINTPIDS("a");
return true;
}
else if (PidType < ptOther) {
// The Pid is not yet in use and it is a special one
n = PidType;
}
else if (a >= 0) {
// The Pid is not yet in use and we have a free slot
n = a;
}
else {
esyslog("ERROR: no free slot for PID %d on device %d", Pid, CardIndex() + 1);
return false;
}
if (n >= 0) {
pidHandles[n].pid = Pid;
pidHandles[n].streamType = StreamType;
pidHandles[n].used = 1;
PRINTPIDS("C");
if (!SetPid(&pidHandles[n], n, true)) {
esyslog("ERROR: can't set PID %d on device %d", Pid, CardIndex() + 1);
if (PidType <= ptTeletext)
DetachAll(Pid);
DelPid(Pid, PidType);
return false;
}
if (camSlot)
camSlot->SetPid(Pid, true);
}
}
return true;
}
void cDevice::DelPid(int Pid, ePidType PidType)
{
if (Pid || PidType == ptPcr) {
int n = -1;
if (PidType == ptPcr)
n = PidType; // PPID always has to be explicit
else {
for (int i = 0; i < MAXPIDHANDLES; i++) {
if (pidHandles[i].pid == Pid) {
n = i;
break;
}
}
}
if (n >= 0 && pidHandles[n].used) {
PRINTPIDS("D");
if (--pidHandles[n].used < 2) {
SetPid(&pidHandles[n], n, false);
if (pidHandles[n].used == 0) {
pidHandles[n].handle = -1;
pidHandles[n].pid = 0;
if (camSlot)
camSlot->SetPid(Pid, false);
}
}
PRINTPIDS("E");
}
}
}
bool cDevice::SetPid(cPidHandle *Handle, int Type, bool On)
{
return false;
}
void cDevice::DelLivePids(void)
{
for (int i = ptAudio; i < ptOther; i++) {
if (pidHandles[i].pid)
DelPid(pidHandles[i].pid, ePidType(i));
}
}
void cDevice::StartSectionHandler(void)
{
if (!sectionHandler) {
sectionHandler = new cSectionHandler(this);
AttachFilter(eitFilter = new cEitFilter);
AttachFilter(patFilter = new cPatFilter);
AttachFilter(sdtFilter = new cSdtFilter(patFilter));
AttachFilter(nitFilter = new cNitFilter(sdtFilter));
}
}
void cDevice::StopSectionHandler(void)
{
if (sectionHandler) {
delete nitFilter;
delete sdtFilter;
delete patFilter;
delete eitFilter;
delete sectionHandler;
nitFilter = NULL;
sdtFilter = NULL;
patFilter = NULL;
eitFilter = NULL;
sectionHandler = NULL;
}
}
int cDevice::OpenFilter(u_short Pid, u_char Tid, u_char Mask)
{
return -1;
}
int cDevice::ReadFilter(int Handle, void *Buffer, size_t Length)
{
return safe_read(Handle, Buffer, Length);
}
void cDevice::CloseFilter(int Handle)
{
close(Handle);
}
void cDevice::AttachFilter(cFilter *Filter)
{
if (sectionHandler)
sectionHandler->Attach(Filter);
}
void cDevice::Detach(cFilter *Filter)
{
if (sectionHandler)
sectionHandler->Detach(Filter);
}
bool cDevice::ProvidesSource(int Source) const
{
return false;
}
bool cDevice::DeviceHooksProvidesTransponder(const cChannel *Channel) const
{
cDeviceHook *Hook = deviceHooks.First();
while (Hook) {
if (!Hook->DeviceProvidesTransponder(this, Channel))
return false;
Hook = deviceHooks.Next(Hook);
}
return true;
}
bool cDevice::ProvidesTransponder(const cChannel *Channel) const
{
return false;
}
bool cDevice::ProvidesTransponderExclusively(const cChannel *Channel) const
{
for (int i = 0; i < numDevices; i++) {
if (device[i] && device[i] != this && device[i]->ProvidesTransponder(Channel))
return false;
}
return true;
}
bool cDevice::ProvidesChannel(const cChannel *Channel, int Priority, bool *NeedsDetachReceivers) const
{
return false;
}
bool cDevice::ProvidesEIT(void) const
{
return false;
}
int cDevice::NumProvidedSystems(void) const
{
return 0;
}
const cPositioner *cDevice::Positioner(void) const
{
return NULL;
}
int cDevice::SignalStrength(void) const
{
return -1;
}
int cDevice::SignalQuality(void) const
{
return -1;
}
const cChannel *cDevice::GetCurrentlyTunedTransponder(void) const
{
return NULL;
}
bool cDevice::IsTunedToTransponder(const cChannel *Channel) const
{
return false;
}
bool cDevice::MaySwitchTransponder(const cChannel *Channel) const
{
return time(NULL) > occupiedTimeout && !Receiving() && !(pidHandles[ptAudio].pid || pidHandles[ptVideo].pid || pidHandles[ptDolby].pid);
}
bool cDevice::SwitchChannel(const cChannel *Channel, bool LiveView)
{
if (LiveView) {
isyslog("switching to channel %d (%s)", Channel->Number(), Channel->Name());
cControl::Shutdown(); // prevents old channel from being shown too long if GetDevice() takes longer
}
for (int i = 3; i--;) {
switch (SetChannel(Channel, LiveView)) {
case scrOk: return true;
case scrNotAvailable: Skins.Message(mtInfo, tr("Channel not available!"));
return false;
case scrNoTransfer: Skins.Message(mtError, tr("Can't start Transfer Mode!"));
return false;
case scrFailed: break; // loop will retry
default: esyslog("ERROR: invalid return value from SetChannel");
}
esyslog("retrying");
}
return false;
}
bool cDevice::SwitchChannel(int Direction)
{
bool result = false;
Direction = sgn(Direction);
if (Direction) {
cControl::Shutdown(); // prevents old channel from being shown too long if GetDevice() takes longer
int n = CurrentChannel() + Direction;
int first = n;
LOCK_CHANNELS_READ;
const cChannel *Channel;
while ((Channel = Channels->GetByNumber(n, Direction)) != NULL) {
// try only channels which are currently available
if (GetDevice(Channel, LIVEPRIORITY, true, true))
break;
n = Channel->Number() + Direction;
}
if (Channel) {
int d = n - first;
if (abs(d) == 1)
dsyslog("skipped channel %d", first);
else if (d)
dsyslog("skipped channels %d..%d", first, n - sgn(d));
if (PrimaryDevice()->SwitchChannel(Channel, true))
result = true;
}
else if (n != first)
Skins.Message(mtError, tr("Channel not available!"));
}
return result;
}
eSetChannelResult cDevice::SetChannel(const cChannel *Channel, bool LiveView)
{
cStatus::MsgChannelSwitch(this, 0, LiveView);
if (LiveView) {
StopReplay();
DELETENULL(liveSubtitle);
DELETENULL(dvbSubtitleConverter);
}
cDevice *Device = (LiveView && IsPrimaryDevice()) ? GetDevice(Channel, LIVEPRIORITY, true) : this;
bool NeedsTransferMode = LiveView && Device != PrimaryDevice();
// If the CAM slot wants the TS data, we need to switch to Transfer Mode:
if (!NeedsTransferMode && LiveView && IsPrimaryDevice() && CamSlot() && CamSlot()->WantsTsData())
NeedsTransferMode = true;
eSetChannelResult Result = scrOk;
// If this DVB card can't receive this channel, let's see if we can
// use the card that actually can receive it and transfer data from there:
if (NeedsTransferMode) {
if (Device && PrimaryDevice()->CanReplay()) {
if (Device->SetChannel(Channel, false) == scrOk) // calling SetChannel() directly, not SwitchChannel()!
cControl::Launch(new cTransferControl(Device, Channel));
else
Result = scrNoTransfer;
}
else
Result = scrNotAvailable;
}
else {
// Stop section handling:
if (sectionHandler) {
sectionHandler->SetStatus(false);
sectionHandler->SetChannel(NULL);
}
// Tell the camSlot about the channel switch and add all PIDs of this
// channel to it, for possible later decryption:
if (camSlot)
camSlot->AddChannel(Channel);
if (SetChannelDevice(Channel, LiveView)) {
// Start section handling:
if (sectionHandler) {
if (patFilter)
patFilter->Trigger(Channel->Sid());
sectionHandler->SetChannel(Channel);
sectionHandler->SetStatus(true);
}
// Start decrypting any PIDs that might have been set in SetChannelDevice():
if (camSlot)
camSlot->StartDecrypting();
}
else
Result = scrFailed;
}
if (Result == scrOk) {
if (LiveView && IsPrimaryDevice()) {
currentChannel = Channel->Number();
// Set the available audio tracks:
ClrAvailableTracks();
for (int i = 0; i < MAXAPIDS; i++)
SetAvailableTrack(ttAudio, i, Channel->Apid(i), Channel->Alang(i));
if (Setup.UseDolbyDigital) {
for (int i = 0; i < MAXDPIDS; i++)
SetAvailableTrack(ttDolby, i, Channel->Dpid(i), Channel->Dlang(i));
}
for (int i = 0; i < MAXSPIDS; i++)
SetAvailableTrack(ttSubtitle, i, Channel->Spid(i), Channel->Slang(i));
if (!NeedsTransferMode)
EnsureAudioTrack(true);
EnsureSubtitleTrack();
}
cStatus::MsgChannelSwitch(this, Channel->Number(), LiveView); // only report status if channel switch successful
}
return Result;
}
void cDevice::ForceTransferMode(void)
{
if (!cTransferControl::ReceiverDevice()) {
LOCK_CHANNELS_READ;
if (const cChannel *Channel = Channels->GetByNumber(CurrentChannel()))
SetChannelDevice(Channel, false); // this implicitly starts Transfer Mode
}
}
int cDevice::Occupied(void) const
{
int Seconds = occupiedTimeout - time(NULL);
return Seconds > 0 ? Seconds : 0;
}
void cDevice::SetOccupied(int Seconds)
{
if (Seconds >= 0)
occupiedTimeout = time(NULL) + min(Seconds, MAXOCCUPIEDTIMEOUT);
}
bool cDevice::SetChannelDevice(const cChannel *Channel, bool LiveView)
{
return false;
}
bool cDevice::HasLock(int TimeoutMs) const
{
return true;
}
bool cDevice::HasProgramme(void) const
{
return Replaying() || pidHandles[ptAudio].pid || pidHandles[ptVideo].pid;
}
int cDevice::GetAudioChannelDevice(void)
{
return 0;
}
void cDevice::SetAudioChannelDevice(int AudioChannel)
{
}
void cDevice::SetVolumeDevice(int Volume)
{
}
void cDevice::SetDigitalAudioDevice(bool On)
{
}
void cDevice::SetAudioTrackDevice(eTrackType Type)
{
}
void cDevice::SetSubtitleTrackDevice(eTrackType Type)
{
}
bool cDevice::ToggleMute(void)
{
int OldVolume = volume;
mute = !mute;
//XXX why is it necessary to use different sequences???
if (mute) {
SetVolume(0, true);
Audios.MuteAudio(mute); // Mute external audio after analog audio
}
else {
Audios.MuteAudio(mute); // Enable external audio before analog audio
SetVolume(OldVolume, true);
}
volume = OldVolume;
return mute;
}
int cDevice::GetAudioChannel(void)
{
int c = GetAudioChannelDevice();
return (0 <= c && c <= 2) ? c : 0;
}
void cDevice::SetAudioChannel(int AudioChannel)
{
if (0 <= AudioChannel && AudioChannel <= 2)
SetAudioChannelDevice(AudioChannel);
}
void cDevice::SetVolume(int Volume, bool Absolute)
{
int OldVolume = volume;
double VolumeDelta = double(MAXVOLUME) / Setup.VolumeSteps;
double VolumeLinearize = (Setup.VolumeLinearize >= 0) ? (Setup.VolumeLinearize / 10.0 + 1.0) : (1.0 / ((-Setup.VolumeLinearize / 10.0) + 1.0));
volume = constrain(int(floor((Absolute ? Volume : volume + Volume) / VolumeDelta + 0.5) * VolumeDelta), 0, MAXVOLUME);
SetVolumeDevice(MAXVOLUME - int(pow(1.0 - pow(double(volume) / MAXVOLUME, VolumeLinearize), 1.0 / VolumeLinearize) * MAXVOLUME));
Absolute |= mute;
cStatus::MsgSetVolume(Absolute ? volume : volume - OldVolume, Absolute);
if (volume > 0) {
mute = false;
Audios.MuteAudio(mute);
}
}
void cDevice::ClrAvailableTracks(bool DescriptionsOnly, bool IdsOnly)
{
if (keepTracks)
return;
if (DescriptionsOnly) {
for (int i = ttNone; i < ttMaxTrackTypes; i++)
*availableTracks[i].description = 0;
}
else {
if (IdsOnly) {
for (int i = ttNone; i < ttMaxTrackTypes; i++)
availableTracks[i].id = 0;
}
else
memset(availableTracks, 0, sizeof(availableTracks));
pre_1_3_19_PrivateStream = 0;
SetAudioChannel(0); // fall back to stereo
currentAudioTrackMissingCount = 0;
currentAudioTrack = ttNone;
currentSubtitleTrack = ttNone;
}
}
bool cDevice::SetAvailableTrack(eTrackType Type, int Index, uint16_t Id, const char *Language, const char *Description)
{
eTrackType t = eTrackType(Type + Index);
if (Type == ttAudio && IS_AUDIO_TRACK(t) ||
Type == ttDolby && IS_DOLBY_TRACK(t) ||
Type == ttSubtitle && IS_SUBTITLE_TRACK(t)) {
if (Language)
strn0cpy(availableTracks[t].language, Language, sizeof(availableTracks[t].language));
if (Description)
Utf8Strn0Cpy(availableTracks[t].description, Description, sizeof(availableTracks[t].description));
if (Id) {
availableTracks[t].id = Id; // setting 'id' last to avoid the need for extensive locking
if (Type == ttAudio || Type == ttDolby) {
int numAudioTracks = NumAudioTracks();
if (!availableTracks[currentAudioTrack].id && numAudioTracks && currentAudioTrackMissingCount++ > numAudioTracks * 10)
EnsureAudioTrack();
else if (t == currentAudioTrack)
currentAudioTrackMissingCount = 0;
}
else if (Type == ttSubtitle && autoSelectPreferredSubtitleLanguage)
EnsureSubtitleTrack();
}
return true;
}
else
esyslog("ERROR: SetAvailableTrack called with invalid Type/Index (%d/%d)", Type, Index);
return false;
}
const tTrackId *cDevice::GetTrack(eTrackType Type)
{
return (ttNone < Type && Type < ttMaxTrackTypes) ? &availableTracks[Type] : NULL;
}
int cDevice::NumTracks(eTrackType FirstTrack, eTrackType LastTrack) const
{
int n = 0;
for (int i = FirstTrack; i <= LastTrack; i++) {
if (availableTracks[i].id)
n++;
}
return n;
}