/
dvb.cpp
2355 lines (2088 loc) · 58.1 KB
/
dvb.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 <linux/dvb/frontend.h>
#include <linux/dvb/dmx.h>
#include <linux/dvb/version.h>
#include <lib/base/eerror.h>
#include <lib/base/estring.h>
#include <lib/base/wrappers.h>
#include <lib/dvb/cahandler.h>
#include <lib/dvb/idvb.h>
#include <lib/dvb/dvb.h>
#include <lib/dvb/sec.h>
#include <lib/dvb/fbc.h>
#include <lib/dvb/specs.h>
#include "filepush.h"
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#define MIN(a,b) (a < b ? a : b)
#define MAX(a,b) (a > b ? a : b)
DEFINE_REF(eDVBRegisteredFrontend);
DEFINE_REF(eDVBRegisteredDemux);
DEFINE_REF(eDVBAllocatedFrontend);
void eDVBRegisteredFrontend::closeFrontend()
{
if (!m_inuse && m_frontend->closeFrontend()) // frontend busy
disable->start(60000, true); // retry close in 60secs
}
eDVBAllocatedFrontend::eDVBAllocatedFrontend(eDVBRegisteredFrontend *fe, eFBCTunerManager *fbcmng)
: m_fe(fe), m_fbcmng(fbcmng)
{
m_fe->inc_use();
}
eDVBAllocatedFrontend::~eDVBAllocatedFrontend()
{
m_fe->dec_use();
if (m_fe->m_frontend->is_FBCTuner() && m_fbcmng)
m_fbcmng->Unlink(m_fe);
}
DEFINE_REF(eDVBAllocatedDemux);
eDVBAllocatedDemux::eDVBAllocatedDemux(eDVBRegisteredDemux *demux): m_demux(demux)
{
m_demux->m_inuse++;
}
eDVBAllocatedDemux::~eDVBAllocatedDemux()
{
--m_demux->m_inuse;
}
DEFINE_REF(eDVBResourceManager);
eDVBResourceManager *eDVBResourceManager::instance;
RESULT eDVBResourceManager::getInstance(ePtr<eDVBResourceManager> &ptr)
{
if (instance)
{
ptr = instance;
return 0;
}
return -1;
}
ePtr<eDVBResourceManager> NewResourceManagerPtr(void)
{
ePtr<eDVBResourceManager> ptr;
eDVBResourceManager::getInstance(ptr);
return ptr;
}
eDVBResourceManager::eDVBResourceManager()
:m_releaseCachedChannelTimer(eTimer::create(eApp))
{
avail = 1;
busy = 0;
m_sec = new eDVBSatelliteEquipmentControl(m_frontend, m_simulate_frontend);
if (!instance)
instance = this;
int num_adapter = 1;
while (eDVBAdapterLinux::exist(num_adapter))
{
if (eDVBAdapterLinux::isusb(num_adapter))
{
eDVBAdapterLinux *adapter = new eDVBUsbAdapter(num_adapter);
addAdapter(adapter);
}
num_adapter++;
}
if (eDVBAdapterLinux::exist(0))
{
eDVBAdapterLinux *adapter = new eDVBAdapterLinux(0);
adapter->scanDevices();
addAdapter(adapter, true);
}
eDebug("[eDVBResourceManager] found %zd adapter, %zd frontends(%zd sim) and %zd demux",
m_adapter.size(), m_frontend.size(), m_simulate_frontend.size(), m_demux.size());
m_fbcmng = new eFBCTunerManager(instance);
CONNECT(m_releaseCachedChannelTimer->timeout, eDVBResourceManager::releaseCachedChannel);
}
void eDVBResourceManager::feStateChanged()
{
int mask=0;
for (eSmartPtrList<eDVBRegisteredFrontend>::iterator i(m_frontend.begin()); i != m_frontend.end(); ++i)
if (i->m_inuse)
mask |= ( 1 << i->m_frontend->getSlotID() );
/* emit */ frontendUseMaskChanged(mask);
}
std::map<std::string, std::string> eDVBAdapterLinux::mappedFrontendName;
DEFINE_REF(eDVBAdapterLinux);
eDVBAdapterLinux::eDVBAdapterLinux(int nr): m_nr(nr)
{
}
void eDVBAdapterLinux::scanDevices()
{
// scan frontends
int num_fe = 0;
eDebug("[eDVBResourceManager] scanning for frontends..");
while (1)
{
/*
* Some frontend devices might have been just created, if
* they are virtual (vtuner) frontends.
* In that case, we cannot be sure the devicenodes are available yet.
* So it is safer to scan for sys entries, than for device nodes
*/
char filename[128];
snprintf(filename, sizeof(filename), "/sys/class/dvb/dvb%d.frontend%d", m_nr, num_fe);
if (::access(filename, X_OK) < 0) break;
snprintf(filename, sizeof(filename), "/dev/dvb/adapter%d/frontend%d", m_nr, num_fe);
eDVBFrontend *fe;
std::string name = filename;
std::map<std::string, std::string>::iterator it = mappedFrontendName.find(name);
if (it != mappedFrontendName.end()) name = it->second;
{
int ok = 0;
fe = new eDVBFrontend(name.c_str(), num_fe, ok, true);
if (ok)
m_simulate_frontend.push_back(ePtr<eDVBFrontend>(fe));
}
{
int ok = 0;
fe = new eDVBFrontend(name.c_str(), num_fe, ok, false, fe);
if (ok)
m_frontend.push_back(ePtr<eDVBFrontend>(fe));
}
++num_fe;
}
// scan demux
int num_demux = 0;
while (1)
{
char filename[128];
sprintf(filename, "/dev/dvb/adapter%d/demux%d", m_nr, num_demux);
if (::access(filename, R_OK) < 0) break;
ePtr<eDVBDemux> demux;
demux = new eDVBDemux(m_nr, num_demux);
m_demux.push_back(demux);
++num_demux;
}
}
int eDVBAdapterLinux::getNumDemux()
{
return m_demux.size();
}
RESULT eDVBAdapterLinux::getDemux(ePtr<eDVBDemux> &demux, int nr)
{
eDebug("[eDVBAdapterLinux] get demux %d", nr);
eSmartPtrList<eDVBDemux>::iterator i(m_demux.begin());
while (nr && (i != m_demux.end()))
{
--nr;
++i;
}
if (i != m_demux.end())
demux = *i;
else
return -1;
return 0;
}
int eDVBAdapterLinux::getNumFrontends()
{
return m_frontend.size();
}
RESULT eDVBAdapterLinux::getFrontend(ePtr<eDVBFrontend> &fe, int nr, bool simulate)
{
eSmartPtrList<eDVBFrontend>::iterator i(simulate ? m_simulate_frontend.begin() : m_frontend.begin());
while (nr && (i != m_frontend.end()))
{
--nr;
++i;
}
if (i != m_frontend.end())
fe = *i;
else
return -1;
return 0;
}
int eDVBAdapterLinux::exist(int nr)
{
char filename[128];
sprintf(filename, "/dev/dvb/adapter%d", nr);
return (::access(filename, X_OK) >= 0) ? 1 : 0;
}
bool eDVBAdapterLinux::isusb(int nr)
{
char devicename[256];
snprintf(devicename, sizeof(devicename), "/sys/class/dvb/dvb%d.frontend0/device/ep_00", nr);
if (::access(devicename, X_OK) >= 0)
{
return true;
}
snprintf(devicename, sizeof(devicename), "/sys/class/dvb/dvb%d.frontend0/device/subsystem", nr);
return readLink(devicename).find("/usb") != std::string::npos;
}
DEFINE_REF(eDVBUsbAdapter);
eDVBUsbAdapter::eDVBUsbAdapter(int nr)
: eDVBAdapterLinux(nr)
{
int file;
char type[8];
struct dvb_frontend_info fe_info;
int frontend = -1;
char filename[256];
char name[128] = {0};
int vtunerid = nr - 1;
pumpThread = 0;
int num_fe = 0;
while (1)
{
/*
* Some frontend devices might have been just created, if
* they are virtual (vtuner) frontends.
* In that case, we cannot be sure the devicenodes are available yet.
* So it is safer to scan for sys entries, than for device nodes
*/
snprintf(filename, sizeof(filename), "/sys/class/dvb/dvb0.frontend%d", num_fe);
if (::access(filename, X_OK) < 0) break;
num_fe++;
}
snprintf(filename, sizeof(filename), "/dev/dvb/adapter0/frontend%d", num_fe);
virtualFrontendName = filename;
demuxFd = vtunerFd = pipeFd[0] = pipeFd[1] = -1;
/* find the device name */
snprintf(filename, sizeof(filename), "/sys/class/dvb/dvb%d.frontend0/device/product", nr);
file = ::open(filename, O_RDONLY);
if (file < 0)
{
snprintf(filename, sizeof(filename), "/sys/class/dvb/dvb%d.frontend0/device/manufacturer", nr);
file = ::open(filename, O_RDONLY);
}
if (file >= 0)
{
int len = singleRead(file, name, sizeof(name) - 1);
if (len >= 0)
{
name[len] = 0;
}
::close(file);
file = -1;
}
snprintf(filename, sizeof(filename), "/dev/dvb/adapter%d/frontend0", nr);
frontend = open(filename, O_RDWR);
if (frontend < 0)
{
goto error;
}
if (::ioctl(frontend, FE_GET_INFO, &fe_info) < 0)
{
::close(frontend);
frontend = -1;
goto error;
}
struct dtv_properties props;
struct dtv_property prop[1];
prop[0].cmd = DTV_ENUM_DELSYS;
memset(prop[0].u.buffer.data, 0, sizeof(prop[0].u.buffer.data));
prop[0].u.buffer.len = 0;
props.num = 1;
props.props = prop;
if (ioctl(frontend, FE_GET_PROPERTY, &props) < 0)
eDebug("[eDVBUsbAdapter] FE_GET_PROPERTY DTV_ENUM_DELSYS failed %m");
::close(frontend);
frontend = -1;
usbFrontendName = filename;
if (!name[0])
{
/* fallback to the dvb_frontend_info name */
int len = MIN(sizeof(fe_info.name), sizeof(name) - 1);
strncpy(name, fe_info.name, len);
name[len] = 0;
}
if (name[0])
{
/* strip trailing LF / CR / whitespace */
int len = strlen(name);
char *tmp = name;
while (len && (strchr(" \n\r\t", tmp[len - 1]) != NULL))
{
tmp[--len] = 0;
}
}
if (!name[0])
{
/* we did not find a usable name, fallback to a default */
snprintf(name, sizeof(name), "usb frontend");
}
snprintf(filename, sizeof(filename), "/dev/dvb/adapter%d/demux0", nr);
demuxFd = open(filename, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
if (demuxFd < 0)
{
goto error;
}
while (vtunerFd < 0)
{
snprintf(filename, sizeof(filename), "/dev/misc/vtuner%d", vtunerid);
if (::access(filename, F_OK) < 0) break;
vtunerFd = open(filename, O_RDWR | O_CLOEXEC);
if (vtunerFd < 0)
{
vtunerid++;
}
}
if (vtunerFd < 0)
{
goto error;
}
eDebug("[eDVBUsbAdapter] linking adapter%d/frontend0 to vtuner%d", nr, vtunerid);
switch (fe_info.type)
{
case FE_QPSK:
strcpy(type,"DVB-S2");
break;
case FE_QAM:
strcpy(type,"DVB-C");
break;
case FE_OFDM:
if (fe_info.caps & FE_CAN_2G_MODULATION)
strcpy(type,"DVB-T2");
else
strcpy(type,"DVB-T");
break;
case FE_ATSC:
strcpy(type,"ATSC");
break;
default:
eDebug("[eDVBUsbAdapter] Frontend type 0x%x not supported", fe_info.type);
goto error;
}
#ifdef TUNER_FBC
#define VTUNER_GET_MESSAGE 11
#define VTUNER_SET_RESPONSE 12
#define VTUNER_SET_NAME 13
#define VTUNER_SET_TYPE 14
#define VTUNER_SET_HAS_OUTPUTS 15
#define VTUNER_SET_FE_INFO 16
#define VTUNER_SET_NUM_MODES 17
#define VTUNER_SET_MODES 18
#else
#define VTUNER_GET_MESSAGE 1
#define VTUNER_SET_RESPONSE 2
#define VTUNER_SET_NAME 3
#define VTUNER_SET_TYPE 4
#define VTUNER_SET_HAS_OUTPUTS 5
#define VTUNER_SET_FE_INFO 6
#define VTUNER_SET_NUM_MODES 7
#define VTUNER_SET_MODES 8
#endif
#define VTUNER_SET_DELSYS 32
#define VTUNER_SET_ADAPTER 33
ioctl(vtunerFd, VTUNER_SET_NAME, name);
ioctl(vtunerFd, VTUNER_SET_TYPE, type);
ioctl(vtunerFd, VTUNER_SET_FE_INFO, &fe_info);
if (prop[0].u.buffer.len > 0)
ioctl(vtunerFd, VTUNER_SET_DELSYS, prop[0].u.buffer.data);
ioctl(vtunerFd, VTUNER_SET_HAS_OUTPUTS, "no");
ioctl(vtunerFd, VTUNER_SET_ADAPTER, nr);
memset(pidList, 0xff, sizeof(pidList));
mappedFrontendName[virtualFrontendName] = usbFrontendName;
if (pipe(pipeFd) == -1)
{
eWarning("[eDVBUsbAdapter] failed to create pipe (%m)");
goto error;
}
running = true;
pthread_create(&pumpThread, NULL, threadproc, (void*)this);
return;
error:
if (vtunerFd >= 0)
{
close(vtunerFd);
vtunerFd = -1;
}
if (demuxFd >= 0)
{
close(demuxFd);
demuxFd = -1;
}
}
eDVBUsbAdapter::~eDVBUsbAdapter()
{
running = false;
if (pipeFd[1] >= 0)
{
::close(pipeFd[1]);
pipeFd[1] = -1;
}
if (pumpThread) pthread_join(pumpThread, NULL);
if (pipeFd[0] >= 0)
{
::close(pipeFd[0]);
pipeFd[0] = -1;
}
if (vtunerFd >= 0)
{
close(vtunerFd);
vtunerFd = -1;
}
if (demuxFd >= 0)
{
close(demuxFd);
demuxFd = -1;
}
}
void *eDVBUsbAdapter::threadproc(void *arg)
{
eDVBUsbAdapter *user = (eDVBUsbAdapter*)arg;
return user->vtunerPump();
}
static bool exist_in_pidlist(unsigned short int* pidlist, unsigned short int value)
{
for (int i=0; i<30; ++i)
if (pidlist[i] == value)
return true;
return false;
}
void *eDVBUsbAdapter::vtunerPump()
{
int pidcount = 0;
if (vtunerFd < 0 || demuxFd < 0 || pipeFd[0] < 0) return NULL;
#define MSG_PIDLIST 14
struct vtuner_message
{
int type;
unsigned short int pidlist[30];
unsigned char pad[64]; /* nobody knows the much data the driver will try to copy into our struct, add some padding to be sure */
};
#define DEMUX_BUFFER_SIZE (16 * 1024 * 188 ) /* 3 MB */
ioctl(demuxFd, DMX_SET_BUFFER_SIZE, DEMUX_BUFFER_SIZE);
while (running)
{
fd_set rset, xset;
int maxfd = vtunerFd;
if (demuxFd > maxfd) maxfd = demuxFd;
if (pipeFd[0] > maxfd) maxfd = pipeFd[0];
FD_ZERO(&rset);
FD_ZERO(&xset);
FD_SET(vtunerFd, &xset);
FD_SET(demuxFd, &rset);
FD_SET(pipeFd[0], &rset);
if (Select(maxfd + 1, &rset, NULL, &xset, NULL) > 0)
{
if (FD_ISSET(vtunerFd, &xset))
{
struct vtuner_message message;
memset(message.pidlist, 0xff, sizeof(message.pidlist));
::ioctl(vtunerFd, VTUNER_GET_MESSAGE, &message);
switch (message.type)
{
case MSG_PIDLIST:
/* remove old pids */
for (int i = 0; i < 30; i++)
{
if (pidList[i] == 0xffff)
continue;
if (exist_in_pidlist(message.pidlist, pidList[i]))
continue;
if (pidcount > 1)
{
::ioctl(demuxFd, DMX_REMOVE_PID, &pidList[i]);
pidcount--;
}
else if (pidcount == 1)
{
::ioctl(demuxFd, DMX_STOP);
pidcount = 0;
}
}
/* add new pids */
for (int i = 0; i < 30; i++)
{
if (message.pidlist[i] == 0xffff)
continue;
if (exist_in_pidlist(pidList, message.pidlist[i]))
continue;
if (pidcount)
{
::ioctl(demuxFd, DMX_ADD_PID, &message.pidlist[i]);
pidcount++;
}
else
{
struct dmx_pes_filter_params filter;
filter.input = DMX_IN_FRONTEND;
filter.flags = 0;
filter.pid = message.pidlist[i];
filter.output = DMX_OUT_TSDEMUX_TAP;
filter.pes_type = DMX_PES_OTHER;
if (ioctl(demuxFd, DMX_SET_PES_FILTER, &filter) >= 0
&& ioctl(demuxFd, DMX_START) >= 0)
{
pidcount = 1;
}
}
}
/* copy pids */
memcpy(pidList, message.pidlist, sizeof(message.pidlist));
break;
}
}
if (FD_ISSET(demuxFd, &rset))
{
ssize_t size = singleRead(demuxFd, buffer, sizeof(buffer));
if (size > 0 && writeAll(vtunerFd, buffer, size) <= 0)
{
break;
}
}
}
}
return NULL;
}
eDVBResourceManager::~eDVBResourceManager()
{
if (instance == this)
instance = 0;
}
void eDVBResourceManager::addAdapter(iDVBAdapter *adapter, bool front)
{
int num_fe = adapter->getNumFrontends();
int num_demux = adapter->getNumDemux();
if (front)
{
m_adapter.push_front(adapter);
}
else
{
m_adapter.push_back(adapter);
}
int i;
for (i=0; i<num_demux; ++i)
{
ePtr<eDVBDemux> demux;
if (!adapter->getDemux(demux, i))
m_demux.push_back(new eDVBRegisteredDemux(demux, adapter));
}
ePtr<eDVBRegisteredFrontend> prev_dvbt_frontend;
for (i=0; i<num_fe; ++i)
{
ePtr<eDVBFrontend> frontend;
if (!adapter->getFrontend(frontend, i))
{
eDVBRegisteredFrontend *new_fe = new eDVBRegisteredFrontend(frontend, adapter);
CONNECT(new_fe->stateChanged, eDVBResourceManager::feStateChanged);
m_frontend.push_back(new_fe);
frontend->setSEC(m_sec);
// we must link all dvb-t frontends ( for active antenna voltage )
if (frontend->supportsDeliverySystem(SYS_DVBT, false) || frontend->supportsDeliverySystem(SYS_DVBT2, false))
{
if (prev_dvbt_frontend)
{
prev_dvbt_frontend->m_frontend->setData(eDVBFrontend::LINKED_NEXT_PTR, (long)new_fe);
frontend->setData(eDVBFrontend::LINKED_PREV_PTR, (long)&(*prev_dvbt_frontend));
}
prev_dvbt_frontend = new_fe;
}
}
}
prev_dvbt_frontend = 0;
for (i=0; i<num_fe; ++i)
{
ePtr<eDVBFrontend> frontend;
if (!adapter->getFrontend(frontend, i, true))
{
eDVBRegisteredFrontend *new_fe = new eDVBRegisteredFrontend(frontend, adapter);
// CONNECT(new_fe->stateChanged, eDVBResourceManager::feStateChanged);
m_simulate_frontend.push_back(new_fe);
frontend->setSEC(m_sec);
// we must link all dvb-t frontends ( for active antenna voltage )
if (frontend->supportsDeliverySystem(SYS_DVBT, false) || frontend->supportsDeliverySystem(SYS_DVBT2, false))
{
if (prev_dvbt_frontend)
{
prev_dvbt_frontend->m_frontend->setData(eDVBFrontend::LINKED_NEXT_PTR, (long)new_fe);
frontend->setData(eDVBFrontend::LINKED_PREV_PTR, (long)&(*prev_dvbt_frontend));
}
prev_dvbt_frontend = new_fe;
}
}
}
}
PyObject *eDVBResourceManager::setFrontendSlotInformations(ePyObject list)
{
if (!PyList_Check(list))
{
PyErr_SetString(PyExc_StandardError, "eDVBResourceManager::setFrontendSlotInformations argument should be a python list");
return NULL;
}
unsigned int assigned=0;
for (eSmartPtrList<eDVBRegisteredFrontend>::iterator i(m_frontend.begin()); i != m_frontend.end(); ++i)
{
int pos=0;
while (pos < PyList_Size(list)) {
ePyObject obj = PyList_GET_ITEM(list, pos++);
ePyObject Id, Descr, Enabled, IsDVBS2, frontendId;
if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 5)
continue;
Id = PyTuple_GET_ITEM(obj, 0);
Descr = PyTuple_GET_ITEM(obj, 1);
Enabled = PyTuple_GET_ITEM(obj, 2);
IsDVBS2 = PyTuple_GET_ITEM(obj, 3);
frontendId = PyTuple_GET_ITEM(obj, 4);
if (!PyInt_Check(Id) || !PyString_Check(Descr) || !PyBool_Check(Enabled) || !PyBool_Check(IsDVBS2) || !PyInt_Check(frontendId))
continue;
if (!i->m_frontend->setSlotInfo(PyInt_AsLong(Id), PyString_AS_STRING(Descr), Enabled == Py_True, IsDVBS2 == Py_True, PyInt_AsLong(frontendId)))
continue;
++assigned;
break;
}
}
if (assigned != m_frontend.size()) {
eDebug("[eDVBResourceManager::setFrontendSlotInformations] .. assigned %zd socket informations, but %d registered frontends!",
m_frontend.size(), assigned);
}
for (eSmartPtrList<eDVBRegisteredFrontend>::iterator i(m_simulate_frontend.begin()); i != m_simulate_frontend.end(); ++i)
{
int pos=0;
while (pos < PyList_Size(list)) {
ePyObject obj = PyList_GET_ITEM(list, pos++);
ePyObject Id, Descr, Enabled, IsDVBS2, frontendId;
if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 5)
continue;
Id = PyTuple_GET_ITEM(obj, 0);
Descr = PyTuple_GET_ITEM(obj, 1);
Enabled = PyTuple_GET_ITEM(obj, 2);
IsDVBS2 = PyTuple_GET_ITEM(obj, 3);
frontendId = PyTuple_GET_ITEM(obj, 4);
if (!PyInt_Check(Id) || !PyString_Check(Descr) || !PyBool_Check(Enabled) || !PyBool_Check(IsDVBS2) || !PyInt_Check(frontendId))
continue;
if (!i->m_frontend->setSlotInfo(PyInt_AsLong(Id), PyString_AS_STRING(Descr), Enabled == Py_True, IsDVBS2 == Py_True, PyInt_AsLong(frontendId)))
continue;
break;
}
}
Py_RETURN_NONE;
}
bool eDVBResourceManager::frontendIsCompatible(int index, const char *type)
{
for (eSmartPtrList<eDVBRegisteredFrontend>::iterator i(m_frontend.begin()); i != m_frontend.end(); ++i)
{
if (i->m_frontend->getSlotID() == index)
{
if (!strcmp(type, "DVB-S2"))
{
return i->m_frontend->supportsDeliverySystem(SYS_DVBS2, false);
}
else if (!strcmp(type, "DVB-S"))
{
return i->m_frontend->supportsDeliverySystem(SYS_DVBS, false);
}
else if (!strcmp(type, "DVB-T2"))
{
return i->m_frontend->supportsDeliverySystem(SYS_DVBT2, false);
}
else if (!strcmp(type, "DVB-T"))
{
return i->m_frontend->supportsDeliverySystem(SYS_DVBT, false);
}
else if (!strcmp(type, "DVB-C"))
{
#if defined SYS_DVBC_ANNEX_A
return i->m_frontend->supportsDeliverySystem(SYS_DVBC_ANNEX_A, false) || i->m_frontend->supportsDeliverySystem(SYS_DVBC_ANNEX_C, false);
#else
return i->m_frontend->supportsDeliverySystem(SYS_DVBC_ANNEX_AC, false);
#endif
}
else if (!strcmp(type, "ATSC"))
{
return i->m_frontend->supportsDeliverySystem(SYS_ATSC, false) || i->m_frontend->supportsDeliverySystem(SYS_DVBC_ANNEX_B, false);
}
break;
}
}
return false;
}
bool eDVBResourceManager::frontendIsMultistream(int index)
{
for (eSmartPtrList<eDVBRegisteredFrontend>::iterator i(m_frontend.begin()); i != m_frontend.end(); ++i)
{
if (i->m_frontend->getSlotID() == index)
{
return i->m_frontend->is_multistream();
}
}
return false;
}
std::string eDVBResourceManager::getFrontendCapabilities(int index)
{
for (eSmartPtrList<eDVBRegisteredFrontend>::iterator i(m_frontend.begin()); i != m_frontend.end(); ++i)
{
if (i->m_frontend->getSlotID() == index)
{
return i->m_frontend->getCapabilities();
}
}
return "";
}
void eDVBResourceManager::setFrontendType(int index, const char *types)
{
for (eSmartPtrList<eDVBRegisteredFrontend>::iterator i(m_frontend.begin()); i != m_frontend.end(); ++i)
{
if (i->m_frontend->getSlotID() != index)
{
continue;
}
std::vector<fe_delivery_system_t> whitelist;
for (const auto& type : split(types, ","))
{
if (type == "DVB-S2" || type == "DVB-S")
{
whitelist.push_back(SYS_DVBS);
whitelist.push_back(SYS_DVBS2);
}
else if (type == "DVB-T2" || type == "DVB-T")
{
whitelist.push_back(SYS_DVBT);
whitelist.push_back(SYS_DVBT2);
}
else if (type == "DVB-C")
{
#if defined SYS_DVBC_ANNEX_A
whitelist.push_back(SYS_DVBC_ANNEX_A);
whitelist.push_back(SYS_DVBC_ANNEX_C);
#else
whitelist.push_back(SYS_DVBC_ANNEX_AC);
#endif
}
else if (type == "ATSC")
{
whitelist.push_back(SYS_ATSC);
whitelist.push_back(SYS_DVBC_ANNEX_B);
}
else if (type == "UNDEFINED")
{
whitelist.push_back(SYS_UNDEFINED);
}
}
i->m_frontend->setDeliverySystemWhitelist(whitelist);
break;
}
}
RESULT eDVBResourceManager::allocateFrontend(ePtr<eDVBAllocatedFrontend> &fe, ePtr<iDVBFrontendParameters> &feparm, bool simulate)
{
eSmartPtrList<eDVBRegisteredFrontend> &frontends = simulate ? m_simulate_frontend : m_frontend;
eDVBRegisteredFrontend *best, *fbc_fe, *best_fbc_fe;
int bestval, foundone, current_fbc_setid, c;
bool check_fbc_leaf_linkable;
fbc_fe = NULL;
best_fbc_fe = NULL;
best = NULL;
bestval = 0;
foundone = 0;
check_fbc_leaf_linkable = false;
current_fbc_setid = -1;
for (eSmartPtrList<eDVBRegisteredFrontend>::iterator i(frontends.begin()); i != frontends.end(); ++i)
{
c = 0;
fbc_fe = NULL;
if (i->m_frontend->is_FBCTuner() && m_fbcmng->CanLink(*i))
{
int fbc_setid = m_fbcmng->GetFBCSetID(i->m_frontend->getSlotID());
if (fbc_setid != current_fbc_setid)
{
current_fbc_setid = fbc_setid;
check_fbc_leaf_linkable = false;
}
if (!check_fbc_leaf_linkable)
{
c = m_fbcmng->IsCompatibleWith(feparm, *i, fbc_fe, simulate);
check_fbc_leaf_linkable = true;
//eDebug("[eDVBResourceManager::allocateFrontend] m_fbcmng->isCompatibleWith slotid : %p (%d), fbc_fe : %p (%d), score : %d", (eDVBRegisteredFrontend *)*i, i->m_frontend->ge);
}
}
else
c = i->m_frontend->isCompatibleWith(feparm);
if (c) /* if we have at least one frontend which is compatible with the source, flag this. */
{
// eDebug("[eDVBResourceManager] allocateFrontend, score=%d", c);
foundone = 1;
}
if (!i->m_inuse)
{
if (c > bestval)
{
bestval = c;
best = *i;
best_fbc_fe = fbc_fe;
}
}
}
if (best)
{
if (best_fbc_fe)
m_fbcmng->AddLink(best, best_fbc_fe, simulate);
fe = new eDVBAllocatedFrontend(best, m_fbcmng);
return 0;
}
fe = 0;
if (foundone)
return errAllSourcesBusy;
else
return errNoSourceFound;
}
RESULT eDVBResourceManager::allocateFrontendByIndex(ePtr<eDVBAllocatedFrontend> &fe, int slot_index)
{
int err = errNoSourceFound;
for (eSmartPtrList<eDVBRegisteredFrontend>::iterator i(m_frontend.begin()); i != m_frontend.end(); ++i)
if (!i->m_inuse && i->m_frontend->getSlotID() == slot_index)
{
// check if another slot linked to this is in use
long tmp;
i->m_frontend->getData(eDVBFrontend::SATPOS_DEPENDS_PTR, tmp);
if ( tmp != -1 )
{
eDVBRegisteredFrontend *satpos_depends_to_fe = (eDVBRegisteredFrontend *)tmp;
if (satpos_depends_to_fe->m_inuse)
{
eDebug("[eDVBResourceManager] another satpos depending frontend is in use.. so allocateFrontendByIndex not possible!");
err = errAllSourcesBusy;
goto alloc_fe_by_id_not_possible;
}
}
else // check linked tuners
{
i->m_frontend->getData(eDVBFrontend::LINKED_NEXT_PTR, tmp);
while ( tmp != -1 )
{
eDVBRegisteredFrontend *next = (eDVBRegisteredFrontend *) tmp;
if (next->m_inuse)
{
eDebug("[eDVBResourceManager] another linked frontend is in use.. so allocateFrontendByIndex not possible!");
err = errAllSourcesBusy;
goto alloc_fe_by_id_not_possible;
}
next->m_frontend->getData(eDVBFrontend::LINKED_NEXT_PTR, tmp);
}
i->m_frontend->getData(eDVBFrontend::LINKED_PREV_PTR, tmp);
while ( tmp != -1 )
{
eDVBRegisteredFrontend *prev = (eDVBRegisteredFrontend *) tmp;
if (prev->m_inuse)
{
eDebug("[eDVBResourceManager] another linked frontend is in use.. so allocateFrontendByIndex not possible!");
err = errAllSourcesBusy;
goto alloc_fe_by_id_not_possible;
}
prev->m_frontend->getData(eDVBFrontend::LINKED_PREV_PTR, tmp);
}
}
fe = new eDVBAllocatedFrontend(i, m_fbcmng);
return 0;
}
alloc_fe_by_id_not_possible:
fe = 0;
return err;
}
RESULT eDVBResourceManager::allocateDemux(eDVBRegisteredFrontend *fe, ePtr<eDVBAllocatedDemux> &demux, int &cap)
{
eDebug("[eDVBResourceManager] allocate demux cap=%02X", cap);
eSmartPtrList<eDVBRegisteredDemux>::iterator i(m_demux.begin());
if (i == m_demux.end())
return -1;
iDVBAdapter *adapter = fe ? fe->m_adapter : m_adapter.begin();
int fesource = fe ? fe->m_frontend->getDVBID() : -1;
ePtr<eDVBRegisteredDemux> unused;
uint8_t d, a;
/*
* For pvr playback, start with the last demux.
* On some hardware, there are less ca devices than demuxes, so try to leave
* the first demuxes for live tv, and start with the last for pvr playback
*/
bool use_decode_demux = (fe || (cap & iDVBChannel::capDecode));
if (!use_decode_demux)
{
i = m_demux.end();
--i;