-
Notifications
You must be signed in to change notification settings - Fork 315
/
scan.cpp
1910 lines (1745 loc) · 57.3 KB
/
scan.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 <fcntl.h>
#include <lib/dvb/idvb.h>
#include <dvbsi++/descriptor_tag.h>
#include <dvbsi++/service_descriptor.h>
#include <dvbsi++/satellite_delivery_system_descriptor.h>
#include <dvbsi++/s2_satellite_delivery_system_descriptor.h>
#include <dvbsi++/terrestrial_delivery_system_descriptor.h>
#include <dvbsi++/t2_delivery_system_descriptor.h>
#include <dvbsi++/logical_channel_descriptor.h>
#include <dvbsi++/cable_delivery_system_descriptor.h>
#include <dvbsi++/ca_identifier_descriptor.h>
#include <dvbsi++/registration_descriptor.h>
#include <dvbsi++/extension_descriptor.h>
#include <dvbsi++/frequency_list_descriptor.h>
#include <dvbsi++/default_authority_descriptor.h>
#include <dvbsi++/private_data_specifier_descriptor.h>
#include <lib/base/nconfig.h> // access to python config
#include <lib/dvb/specs.h>
#include <lib/dvb/esection.h>
#include <lib/dvb/scan.h>
#include <lib/dvb/frontend.h>
#include <lib/dvb/db.h>
#include <lib/dvb/frontendparms.h>
#include <lib/base/eenv.h>
#include <lib/base/eerror.h>
#include <lib/base/estring.h>
#include <lib/dvb/dvb.h>
#include <lib/dvb/db.h>
#include <lib/python/python.h>
#include <errno.h>
#include "absdiff.h"
#define SCAN_eDebug(x...) do { if (m_scan_debug) eDebug(x); } while(0)
#define SCAN_eDebugNoNewLineStart(x...) do { if (m_scan_debug) eDebugNoNewLineStart(x); } while(0)
#define SCAN_eDebugNoNewLine(x...) do { if (m_scan_debug) eDebugNoNewLine(x); } while(0)
DEFINE_REF(eDVBScan);
eDVBScan::eDVBScan(iDVBChannel *channel, bool usePAT, bool debug)
:m_channel(channel)
,m_channel_state(iDVBChannel::state_idle)
,m_ready(0)
,m_ready_all(usePAT ? (readySDT|readyPAT) : readySDT)
,m_pmt_running(false)
,m_abort_current_pmt(false)
,m_flags(0)
,m_usePAT(usePAT)
,m_scan_debug(debug)
{
if (m_channel->getDemux(m_demux))
SCAN_eDebug("[eDVBScan] failed to allocate demux!");
m_channel->connectStateChange(sigc::mem_fun(*this, &eDVBScan::stateChange), m_stateChanged_connection);
m_updateLCN = false;
}
eDVBScan::~eDVBScan()
{
}
int eDVBScan::isValidONIDTSID(int orbital_position, eOriginalNetworkID onid, eTransportStreamID tsid)
{
if(onid.get() == 0 || (onid.get() == 1 && tsid < 2) || onid.get() >= 0xFF00)
{
return 0;
}
return 1;
}
eDVBNamespace eDVBScan::buildNamespace(eOriginalNetworkID onid, eTransportStreamID tsid, unsigned long hash)
{
int orb_pos = (hash >> 16) & 0xFFFF;
if (orb_pos == 0xFFFF) // cable
{
if (eConfigManager::getConfigBoolValue("config.usage.subnetwork_cable", true))
hash &= ~0xFFFF;
}
else if (orb_pos == 0xEEEE) // terrestrial
{
if (eConfigManager::getConfigBoolValue("config.usage.subnetwork_terrestrial", true))
hash &= ~0xFFFF;
}
else if (eConfigManager::getConfigBoolValue("config.usage.subnetwork", true)
&& isValidONIDTSID(orb_pos, onid, tsid)) // on valid ONIDs, ignore frequency ("sub network") part
hash &= ~0xFFFF;
return eDVBNamespace(hash);
}
void eDVBScan::stateChange(iDVBChannel *ch)
{
int state;
if (ch->getState(state))
return;
if (m_channel_state == state)
return;
if (state == iDVBChannel::state_ok)
{
if (m_ch_current && m_channel)
{
int type;
m_ch_current->getSystem(type);
if (type == iDVBFrontend::feTerrestrial)
{
eDVBFrontendParametersTerrestrial parm;
m_ch_current->getDVBT(parm);
if (parm.system == eDVBFrontendParametersTerrestrial::System_DVB_T_T2)
{
/* we have a lock, this is a valid DVB-T transponder, set our system parameter */
parm.system = eDVBFrontendParametersTerrestrial::System_DVB_T;
m_ch_current->setDVBT(parm);
}
}
}
if (!m_ch_blindscan.empty())
{
/* update current blindscan iteration channel with scanned parameters */
if (m_ch_current && m_channel)
{
ePtr<iDVBFrontend> fe;
m_channel->getFrontend(fe);
if (fe)
{
ePtr<iDVBTransponderData> tp;
fe->getTransponderData(tp, false);
if (tp)
{
ePtr<eDVBFrontendParameters> feparm = new eDVBFrontendParameters;
int type;
m_ch_current->getSystem(type);
switch (type)
{
case iDVBFrontend::feSatellite:
{
eDVBFrontendParametersSatellite parm;
m_ch_current->getDVBS(parm);
parm.system = tp->getSystem();
parm.frequency = tp->getFrequency();
parm.symbol_rate = tp->getSymbolRate();
parm.modulation = tp->getModulation();
feparm->setDVBS(parm);
break;
}
case iDVBFrontend::feCable:
{
eDVBFrontendParametersCable parm;
m_ch_current->getDVBC(parm);
parm.system = tp->getSystem();
parm.frequency = tp->getFrequency();
parm.symbol_rate = tp->getSymbolRate();
parm.modulation = tp->getModulation();
feparm->setDVBC(parm);
break;
}
case iDVBFrontend::feTerrestrial:
{
eDVBFrontendParametersTerrestrial parm;
m_ch_current->getDVBT(parm);
parm.system = tp->getSystem();
parm.frequency = tp->getFrequency();
parm.bandwidth = tp->getBandwidth();
parm.modulation = tp->getConstellation();
feparm->setDVBT(parm);
break;
}
case iDVBFrontend::feATSC:
{
eDVBFrontendParametersATSC parm;
m_ch_current->getATSC(parm);
parm.system = tp->getSystem();
parm.frequency = tp->getFrequency();
feparm->setATSC(parm);
break;
}
}
m_ch_current = m_ch_blindscan_result = feparm;
}
}
}
}
startFilter();
m_channel_state = state;
} else if (state == iDVBChannel::state_failed)
{
if (m_ch_current && m_channel)
{
int type;
m_ch_current->getSystem(type);
m_ch_unavailable.push_back(m_ch_current);
if (type == iDVBFrontend::feTerrestrial)
{
eDVBFrontendParametersTerrestrial parm;
m_ch_current->getDVBT(parm);
if (parm.system == eDVBFrontendParametersTerrestrial::System_DVB_T_T2)
{
/* we have to scan T2 as well as T */
ePtr<iDVBFrontend> fe;
eDVBFrontendParameters eparm;
parm.system = eDVBFrontendParametersTerrestrial::System_DVB_T2;
eparm.setDVBT(parm);
m_channel->getFrontend(fe);
if (fe)
{
ePtr<iDVBFrontendParameters> feparm = new eDVBFrontendParameters(eparm);
/* but only if the frontend supports T2 */
if (fe->isCompatibleWith(feparm))
{
addChannelToScan(feparm);
}
}
}
}
}
if (!m_ch_blindscan.empty())
{
/* tune failure, this means the blindscan channel iteration run has completed */
SCAN_eDebug("[eDVBScan] blindscan channel completed");
m_ch_blindscan.pop_front();
}
nextChannel();
}
/* unavailable will timeout, anyway. */
}
RESULT eDVBScan::nextChannel()
{
ePtr<iDVBFrontend> fe;
m_SDT = 0; m_PAT = 0; m_BAT = 0; m_NIT = 0, m_PMT = 0;
m_ready = 0;
m_pat_tsid = eTransportStreamID();
/* check what we need */
m_ready_all = readySDT;
if (m_flags & scanNetworkSearch)
m_ready_all |= readyNIT;
if (m_flags & scanSearchBAT)
m_ready_all |= readyBAT;
if (m_usePAT)
m_ready_all |= readyPAT;
if (!m_ch_blindscan.empty())
{
/* keep iterating with the same 'channel' till we get a tune failure */
SCAN_eDebug("[eDVBScan] blindscan channel iteration");
m_ch_current = m_ch_blindscan.front();
}
else
{
m_ch_blindscan_result = NULL;
if (m_ch_toScan.empty())
{
SCAN_eDebug("[eDVBScan] no channels left: %zd scanned, %zd unavailable, %zd database.",
m_ch_scanned.size(), m_ch_unavailable.size(), m_new_channels.size());
for (std::map<eServiceReferenceDVB, ePtr<eDVBService> >::const_iterator
service(m_new_services.begin()); service != m_new_services.end(); ++service)
{
eDVBChannelID chid(service->first.getDVBNamespace(), service->first.getTransportStreamID(), service->first.getOriginalNetworkID());
std::map<eDVBChannelID, uint32_t>::iterator it = m_aus_da_flags.find(chid);
if (it != m_aus_da_flags.end())
{
SCAN_eDebug("[eDVBScan] use NIT da.au %08x:%04x:%04x 0x%08x %s", chid.dvbnamespace.get(), chid.original_network_id.get(), chid.transport_stream_id.get(), it->second, service->second->m_service_name.c_str());
service->second->m_aus_da_flag = it->second;
}
if (service->second->m_default_authority.empty())
{
for (int i = 0; i < 2; i++)
{
std::map<eDVBChannelID, std::string>::iterator it = m_default_authorities.find(chid);
if (it != m_default_authorities.end())
{
SCAN_eDebug("[eDVBScan] use NIT da %08x:%04x:%04x <%s> %s", chid.dvbnamespace.get(), chid.original_network_id.get(), chid.transport_stream_id.get(), it->second.c_str(), service->second->m_service_name.c_str());
service->second->m_default_authority = it->second;
break;
}
chid.transport_stream_id = 0;
}
}
}
m_event(evtFinish);
return -ENOENT;
}
m_ch_current = m_ch_toScan.front();
m_ch_toScan.pop_front();
}
if (m_channel->getFrontend(fe))
{
m_event(evtFail);
return -ENOTSUP;
}
m_chid_current = eDVBChannelID();
m_channel_state = iDVBChannel::state_idle;
if (fe->tune(*m_ch_current, !m_ch_blindscan.empty()))
return nextChannel();
m_event(evtUpdate);
return 0;
}
RESULT eDVBScan::startFilter()
{
bool startSDT=true;
int system;
ASSERT(m_demux);
/* only start required filters filter */
if (m_ready_all & readyPAT)
startSDT = m_ready & readyPAT;
// m_ch_current is not set, when eDVBScan is just used for a SDT update
if (!m_ch_current)
{
unsigned int channelFlags;
m_channel->getCurrentFrontendParameters(m_ch_current);
m_ch_current->getFlags(channelFlags);
if (channelFlags & iDVBFrontendParameters::flagOnlyFree)
m_flags |= scanOnlyFree;
}
m_VCT = 0;
m_ch_current->getSystem(system);
if (system == iDVBFrontend::feATSC)
{
m_VCT = new eTable<VirtualChannelTableSection>;
if (m_VCT->start(m_demux, eDVBVCTSpec()))
return -1;
CONNECT(m_VCT->tableReady, eDVBScan::VCTready);
startSDT = false;
}
m_SDT = 0;
if (startSDT && (m_ready_all & readySDT))
{
m_SDT = new eTable<ServiceDescriptionSection>;
int tsid=-1;
if (m_ready & readyPAT && m_ready & validPAT)
{
std::vector<ProgramAssociationSection*>::const_iterator i =
m_PAT->getSections().begin();
ASSERT(i != m_PAT->getSections().end());
tsid = (*i)->getTableIdExtension(); // in PAT this is the transport stream id
m_pat_tsid = eTransportStreamID(tsid);
for (; i != m_PAT->getSections().end(); ++i)
{
const ProgramAssociationSection &pat = **i;
ProgramAssociationConstIterator program = pat.getPrograms()->begin();
for (; program != pat.getPrograms()->end(); ++program)
m_pmts_to_read.insert(std::pair<unsigned short, service>((*program)->getProgramNumber(), service((*program)->getProgramMapPid())));
}
m_PMT = new eTable<ProgramMapSection>;
CONNECT(m_PMT->tableReady, eDVBScan::PMTready);
PMTready(-2);
// KabelBW HACK ... on 618Mhz and 626Mhz the transport stream id in PAT and SDT is different
{
int type;
m_ch_current->getSystem(type);
if (type == iDVBFrontend::feCable)
{
eDVBFrontendParametersCable parm;
m_ch_current->getDVBC(parm);
if ((tsid == 0x00d7 && absdiff(parm.frequency, 618000) < 2000) ||
(tsid == 0x00d8 && absdiff(parm.frequency, 626000) < 2000))
tsid = -1;
}
}
}
if (tsid == -1)
{
if (m_SDT->start(m_demux, eDVBSDTSpec()))
return -1;
}
else if (m_SDT->start(m_demux, eDVBSDTSpec(tsid, true)))
return -1;
CONNECT(m_SDT->tableReady, eDVBScan::SDTready);
}
if (!(m_ready & readyPAT))
{
m_PAT = 0;
if (m_ready_all & readyPAT)
{
m_PAT = new eTable<ProgramAssociationSection>;
if (m_PAT->start(m_demux, eDVBPATSpec(4000)))
return -1;
CONNECT(m_PAT->tableReady, eDVBScan::PATready);
}
m_NIT = 0;
if (m_ready_all & readyNIT)
{
m_NIT = new eTable<NetworkInformationSection>;
if (m_NIT->start(m_demux, eDVBNITSpec(m_networkid)))
return -1;
CONNECT(m_NIT->tableReady, eDVBScan::NITready);
}
m_BAT = 0;
if (m_ready_all & readyBAT)
{
m_BAT = new eTable<BouquetAssociationSection>;
if (m_BAT->start(m_demux, eDVBBATSpec()))
return -1;
CONNECT(m_BAT->tableReady, eDVBScan::BATready);
}
}
return 0;
}
void eDVBScan::SDTready(int err)
{
SCAN_eDebug("[eDVBScan] got sdt %d", err);
m_ready |= readySDT;
if (!err)
m_ready |= validSDT;
channelDone();
}
void eDVBScan::NITready(int err)
{
SCAN_eDebug("[eDVBScan] got nit, err %d", err);
m_ready |= readyNIT;
if (!err)
m_ready |= validNIT;
channelDone();
}
void eDVBScan::BATready(int err)
{
SCAN_eDebug("[eDVBScan] got bat, err %d", err);
m_ready |= readyBAT;
if (!err)
m_ready |= validBAT;
channelDone();
}
void eDVBScan::PATready(int err)
{
SCAN_eDebug("[eDVBScan] got pat, err %d", err);
m_ready |= readyPAT;
if (!err)
m_ready |= validPAT;
startFilter(); // for starting the SDT filter
}
void eDVBScan::VCTready(int err)
{
SCAN_eDebug("[eDVBScan] got vct %d", err);
m_ready |= readySDT;
if (!err)
m_ready |= validVCT;
channelDone();
}
void eDVBScan::PMTready(int err)
{
SCAN_eDebug("[eDVBScan] got pmt %d", err);
if (!err)
{
bool scrambled = false;
bool have_audio = false;
bool have_video = false;
unsigned short pcrpid = 0xFFFF;
std::vector<ProgramMapSection*>::const_iterator i;
for (i = m_PMT->getSections().begin(); i != m_PMT->getSections().end(); ++i)
{
const ProgramMapSection &pmt = **i;
if (pcrpid == 0xFFFF)
pcrpid = pmt.getPcrPid();
else
SCAN_eDebug("[eDVBScan] already have a pcrpid %04x %04x", pcrpid, pmt.getPcrPid());
ElementaryStreamInfoConstIterator es;
for (es = pmt.getEsInfo()->begin(); es != pmt.getEsInfo()->end(); ++es)
{
int isaudio = 0, isvideo = 0, is_scrambled = 0, forced_audio = 0, forced_video = 0;
switch ((*es)->getType())
{
case 0x1b: // AVC Video Stream (MPEG4 H264)
case 0x24: // H265 HEVC
case 0x10: // MPEG 4 Part 2
case 0x01: // MPEG 1 video
case 0x02: // MPEG 2 video
isvideo = 1;
forced_video = 1;
[[fallthrough]];
case 0x03: // MPEG 1 audio
case 0x04: // MPEG 2 audio
case 0x0f: // MPEG 2 AAC
case 0x11: // MPEG 4 AAC
if (!isvideo)
{
forced_audio = 1;
isaudio = 1;
}
[[fallthrough]];
case 0x06: // PES Private
case 0x81: // user private
case 0xEA: // TS_PSI_ST_SMPTE_VC1
for (DescriptorConstIterator desc = (*es)->getDescriptors()->begin();
desc != (*es)->getDescriptors()->end(); ++desc)
{
uint8_t tag = (*desc)->getTag();
/* PES private can contain AC-3, DTS or lots of other stuff.
check descriptors to get the exakt type. */
if (!forced_video && !forced_audio)
{
switch (tag)
{
case 0x1C: // TS_PSI_DT_MPEG4_Audio
case 0x2B: // TS_PSI_DT_MPEG2_AAC
case AAC_DESCRIPTOR:
case AC3_DESCRIPTOR:
case DTS_DESCRIPTOR:
case AUDIO_STREAM_DESCRIPTOR:
isaudio = 1;
break;
case 0x28: // TS_PSI_DT_AVC
case 0x1B: // TS_PSI_DT_MPEG4_Video
case VIDEO_STREAM_DESCRIPTOR:
isvideo = 1;
break;
case REGISTRATION_DESCRIPTOR: /* some services don't have a separate AC3 descriptor */
{
RegistrationDescriptor *d = (RegistrationDescriptor*)(*desc);
switch (d->getFormatIdentifier())
{
case 0x44545331 ... 0x44545333: // DTS1/DTS2/DTS3
case 0x41432d33: // == 'AC-3'
case 0x42535344: // == 'BSSD' (LPCM)
isaudio = 1;
break;
case 0x56432d31: // == 'VC-1'
isvideo = 1;
break;
default:
break;
}
}
[[fallthrough]];
default:
break;
}
}
if (tag == CA_DESCRIPTOR)
is_scrambled = 1;
}
[[fallthrough]];
default:
break;
}
if (isvideo)
have_video = true;
else if (isaudio)
have_audio = true;
else
continue;
if (is_scrambled)
scrambled = true;
}
for (DescriptorConstIterator desc = pmt.getDescriptors()->begin();
desc != pmt.getDescriptors()->end(); ++desc)
{
if ((*desc)->getTag() == CA_DESCRIPTOR)
scrambled = true;
}
}
m_pmt_in_progress->second.scrambled = scrambled;
if ( have_video )
m_pmt_in_progress->second.serviceType = 1;
else if ( have_audio )
m_pmt_in_progress->second.serviceType = 2;
else
m_pmt_in_progress->second.serviceType = 100;
}
if (err == -1) // timeout or removed by sdt
m_pmts_to_read.erase(m_pmt_in_progress++);
else if (m_pmt_running)
++m_pmt_in_progress;
else
{
m_pmt_in_progress = m_pmts_to_read.begin();
m_pmt_running = true;
}
if (m_pmt_in_progress != m_pmts_to_read.end())
m_PMT->start(m_demux, eDVBPMTSpec(m_pmt_in_progress->second.pmtPid, m_pmt_in_progress->first, 4000));
else
{
m_PMT = 0;
m_pmt_running = false;
channelDone();
}
}
void eDVBScan::addKnownGoodChannel(const eDVBChannelID &chid, iDVBFrontendParameters *feparm)
{
/* add it to the list of known channels. */
if (chid)
m_new_channels.insert(std::pair<eDVBChannelID,ePtr<iDVBFrontendParameters> >(chid, feparm));
}
void eDVBScan::addChannelToScan(iDVBFrontendParameters *feparm)
{
/* check if we don't already have that channel ... */
int type;
feparm->getSystem(type);
int offset = 2000;
switch(type)
{
case iDVBFrontend::feSatellite:
{
eDVBFrontendParametersSatellite parm;
feparm->getDVBS(parm);
SCAN_eDebug("[eDVBScan] try to add %d %d %d %d %d %d",
parm.orbital_position, parm.frequency, parm.symbol_rate, parm.polarisation, parm.fec, parm.modulation);
break;
}
case iDVBFrontend::feCable:
{
eDVBFrontendParametersCable parm;
feparm->getDVBC(parm);
SCAN_eDebug("[eDVBScan] try to add cable %d %d %d %d",
parm.frequency, parm.symbol_rate, parm.modulation, parm.fec_inner);
break;
}
case iDVBFrontend::feTerrestrial:
{
eDVBFrontendParametersTerrestrial parm;
feparm->getDVBT(parm);
SCAN_eDebug("[eDVBScan] try to add terres %d %d %d %d %d %d %d %d",
parm.frequency, parm.modulation, parm.transmission_mode, parm.hierarchy,
parm.guard_interval, parm.code_rate_LP, parm.code_rate_HP, parm.bandwidth);
offset = 120; // Closer than Australian offset frequency
break;
}
case iDVBFrontend::feATSC:
{
eDVBFrontendParametersATSC parm;
feparm->getATSC(parm);
SCAN_eDebug("[eDVBScan] try to add atsc %d %d %d %d",
parm.frequency, parm.modulation, parm.inversion, parm.system);
break;
}
}
int found_count=0;
/* ... in the list of channels to scan */
for (std::list<ePtr<iDVBFrontendParameters> >::iterator i(m_ch_toScan.begin()); i != m_ch_toScan.end();)
{
if (sameChannel(*i, feparm, false, offset))
{
if (!found_count)
{
*i = feparm; // update
SCAN_eDebug("[eDVBScan] update");
}
else
{
SCAN_eDebug("[eDVBScan] remove dupe");
m_ch_toScan.erase(i++);
continue;
}
++found_count;
}
++i;
}
if (found_count > 0)
{
SCAN_eDebug("[eDVBScan] already in todo list");
return;
}
/* ... in the list of successfully scanned channels */
for (std::list<ePtr<iDVBFrontendParameters> >::const_iterator i(m_ch_scanned.begin()); i != m_ch_scanned.end(); ++i)
if (sameChannel(*i, feparm, false, offset))
{
SCAN_eDebug("[eDVBScan] successfully scanned");
return;
}
/* ... in the list of unavailable channels */
for (std::list<ePtr<iDVBFrontendParameters> >::const_iterator i(m_ch_unavailable.begin()); i != m_ch_unavailable.end(); ++i)
if (sameChannel(*i, feparm, true, offset))
{
SCAN_eDebug("[eDVBScan] scanned but not available");
return;
}
/* ... on the current channel */
if (sameChannel(m_ch_current, feparm, false, offset))
{
SCAN_eDebug("[eDVBScan] is current");
return;
}
SCAN_eDebug("[eDVBScan] really add");
/* otherwise, add it to the todo list. */
m_ch_toScan.push_front(feparm); // better.. then the rotor not turning wild from east to west :)
}
int eDVBScan::sameChannel(iDVBFrontendParameters *ch1, iDVBFrontendParameters *ch2, bool exact, int offset) const
{
int diff;
if (ch1->calculateDifference(ch2, diff, exact))
return 0;
if (diff < offset)
return 1;
return 0;
}
void eDVBScan::channelDone()
{
SCAN_eDebug("[eDVBScan] channelDone with m_ready=0x%02x", m_ready);
if ((m_ready & validSDT) && (!(m_flags & scanOnlyFree) || !m_pmt_running))
{
unsigned long hash = 0;
m_ch_current->getHash(hash);
eDVBNamespace dvbnamespace = buildNamespace(
(**m_SDT->getSections().begin()).getOriginalNetworkId(),
(**m_SDT->getSections().begin()).getTransportStreamId(),
hash);
SCAN_eDebug("[eDVBScan] SDT: ");
std::vector<ServiceDescriptionSection*>::const_iterator i;
for (i = m_SDT->getSections().begin(); i != m_SDT->getSections().end(); ++i)
processSDT(dvbnamespace, **i);
m_ready &= ~validSDT;
}
if (m_ready & validVCT)
{
unsigned long hash = 0;
m_ch_current->getHash(hash);
int onid = 0; /* TODO: ATSC ONID? */
eDVBNamespace dvbnamespace = buildNamespace(
eOriginalNetworkID(onid),
(**m_VCT->getSections().begin()).getTransportStreamId(),
hash);
SCAN_eDebug("[eDVBScan] VCT: ");
std::vector<VirtualChannelTableSection*>::const_iterator i;
for (i = m_VCT->getSections().begin(); i != m_VCT->getSections().end(); ++i)
processVCT(dvbnamespace, **i, onid);
m_ready &= ~validVCT;
}
if (m_ready & validNIT)
{
SCAN_eDebug("[eDVBScan] dumping NIT");
int system;
std::list<ePtr<iDVBFrontendParameters> > m_ch_toScan_backup;
m_ch_current->getSystem(system);
if (m_flags & clearToScanOnFirstNIT)
{
m_ch_toScan_backup = m_ch_toScan;
m_ch_toScan.clear();
}
std::vector<NetworkInformationSection*>::const_iterator i;
for (i = m_NIT->getSections().begin(); i != m_NIT->getSections().end(); ++i)
{
if (m_networkid && m_networkid != (*i)->getTableIdExtension()) // in NIT this is the network id
{
SCAN_eDebug("[eDVBScan] ignoring NetworkId %d!", (*i)->getTableIdExtension());
continue;
}
else
{
SCAN_eDebug("[eDVBScan] m_networkid %d getTableIdExtension %d", m_networkid, (*i)->getTableIdExtension());
}
uint32_t aus_da_flag = 0;
std::string nit_default_authority;
const DescriptorList& descriptors = *(*i)->getDescriptors();
for (DescriptorConstIterator desc = descriptors.begin();
desc != descriptors.end(); ++desc)
{
switch ((*desc)->getTag())
{
case PRIVATE_DATA_SPECIFIER_DESCRIPTOR:
{
PrivateDataSpecifierDescriptor &d = (PrivateDataSpecifierDescriptor&)**desc;
eDebug("[eDVBScan] NIT private %08x", d.getPrivateDataSpecifier());
uint32_t priv = d.getPrivateDataSpecifier();
// ZZ if (priv >= 0x3200 && priv <= 0x320f)
if (priv)
{
aus_da_flag = priv;
}
break;
}
case DEFAULT_AUTHORITY_DESCRIPTOR:
{
DefaultAuthorityDescriptor &d = (DefaultAuthorityDescriptor&)**desc;
const DefaultAuthorityByteVector *da = d.getAuthorityBytes();
nit_default_authority = std::string((char*)da->data(), da->size());
std::transform(nit_default_authority.begin(), nit_default_authority.end(), nit_default_authority.begin(), ::tolower);
SCAN_eDebug("[eDVBScan] NIT da <%s>", nit_default_authority.c_str());
break;
}
}
}
const TransportStreamInfoList &tsinfovec = *(*i)->getTsInfo();
for (TransportStreamInfoConstIterator tsinfo(tsinfovec.begin());
tsinfo != tsinfovec.end(); ++tsinfo)
{
SCAN_eDebug("[eDVBScan] TSID: %04x ONID: %04x", (*tsinfo)->getTransportStreamId(),
(*tsinfo)->getOriginalNetworkId());
eOriginalNetworkID onid = (*tsinfo)->getOriginalNetworkId();
eTransportStreamID tsid = (*tsinfo)->getTransportStreamId();
eDVBNamespace ns(0);
bool T2 = false;
eDVBFrontendParametersTerrestrial t2transponder;
for (DescriptorConstIterator desc = (*tsinfo)->getDescriptors()->begin();
desc != (*tsinfo)->getDescriptors()->end(); ++desc)
{
switch ((*desc)->getTag())
{
case CABLE_DELIVERY_SYSTEM_DESCRIPTOR:
{
if (system != iDVBFrontend::feCable)
break; // when current locked transponder is no cable transponder ignore this descriptor
CableDeliverySystemDescriptor &d = (CableDeliverySystemDescriptor&)**desc;
ePtr<eDVBFrontendParameters> feparm = new eDVBFrontendParameters;
eDVBFrontendParametersCable cable;
cable.set(d);
feparm->setDVBC(cable);
unsigned long hash=0;
feparm->getHash(hash);
ns = buildNamespace(onid, tsid, hash);
addChannelToScan(feparm);
break;
}
case TERRESTRIAL_DELIVERY_SYSTEM_DESCRIPTOR:
{
if (system != iDVBFrontend::feTerrestrial)
break; // when current locked transponder is no terrestrial transponder ignore this descriptor
TerrestrialDeliverySystemDescriptor &d = (TerrestrialDeliverySystemDescriptor&)**desc;
ePtr<eDVBFrontendParameters> feparm = new eDVBFrontendParameters;
eDVBFrontendParametersTerrestrial terr;
terr.set(d);
feparm->setDVBT(terr);
unsigned long hash=0;
feparm->getHash(hash);
ns = buildNamespace(onid, tsid, hash);
SCAN_eDebug("[eDVBScan] terrestrial delivery system descriptor found %d", d.getCentreFrequency() * 10);
addChannelToScan(feparm);
break;
}
case FREQUENCY_LIST_DESCRIPTOR:
{
if (system != iDVBFrontend::feTerrestrial)
break; // when current locked transponder is no terrestrial transponder ignore this descriptor
FrequencyListDescriptor &d = (FrequencyListDescriptor&)**desc;
if (d.getCodingType() != 0x03)
break;
if (T2)
{
for (CentreFrequencyConstIterator it = d.getCentreFrequencies()->begin();
it != d.getCentreFrequencies()->end(); ++it)
{
SCAN_eDebug("[eDVBScan] T2 frequency list descriptor found %d", (*it) * 10);
t2transponder.frequency = (*it) * 10;
ePtr<eDVBFrontendParameters> feparm = new eDVBFrontendParameters;
feparm->setDVBT(t2transponder);
addChannelToScan(feparm);
}
}
else
{
for (CentreFrequencyConstIterator it = d.getCentreFrequencies()->begin();
it != d.getCentreFrequencies()->end(); ++it)
{
SCAN_eDebug("[eDVBScan] T1 frequency list descriptor found %d", (*it) * 10);
eDVBFrontendParametersTerrestrial terr;
m_ch_current->getDVBT(terr);
terr.frequency = (*it) * 10;
// Alternate frequencies don't have to use the same coding params - prefer auto
terr.code_rate_HP = terr.FEC_Auto;
terr.code_rate_LP = terr.FEC_Auto;
terr.modulation = terr.Modulation_Auto;
terr.transmission_mode = terr.TransmissionMode_Auto;
terr.guard_interval = terr.GuardInterval_Auto;
terr.hierarchy = terr.Hierarchy_Auto;
terr.inversion = terr.Inversion_Unknown;
ePtr<eDVBFrontendParameters> feparm = new eDVBFrontendParameters();
feparm->setDVBT(terr);
addChannelToScan(feparm);
}
}
break;
}
case LOGICAL_CHANNEL_DESCRIPTOR:
{
// we handle it later
break;
}
case S2_SATELLITE_DELIVERY_SYSTEM_DESCRIPTOR:
{
eDebug("[eDVBScan] S2_SATELLITE_DELIVERY_SYSTEM_DESCRIPTOR found");
//if (system != iDVBFrontend::feSatellite)
// break; // when current locked transponder is no satellite transponder ignore this descriptor
//S2SatelliteDeliverySystemDescriptor &d = (S2SatelliteDeliverySystemDescriptor&)**desc;
[[fallthrough]];
}
case SATELLITE_DELIVERY_SYSTEM_DESCRIPTOR:
{
if (system != iDVBFrontend::feSatellite)
break; // when current locked transponder is no satellite transponder ignore this descriptor
SatelliteDeliverySystemDescriptor &d = (SatelliteDeliverySystemDescriptor&)**desc;
if (d.getFrequency() < 10000)
break;
ePtr<eDVBFrontendParameters> feparm = new eDVBFrontendParameters;
eDVBFrontendParametersSatellite sat;
sat.set(d);
eDVBFrontendParametersSatellite p;
m_ch_current->getDVBS(p);
if ( absdiff(p.orbital_position, sat.orbital_position) < 5 )
sat.orbital_position = p.orbital_position;
if ( absdiff(absdiff(3600, p.orbital_position), sat.orbital_position) < 5 )
{
SCAN_eDebug("[eDVBScan] found transponder with incorrect west/east flag ... correct this");
sat.orbital_position = p.orbital_position;
}
feparm->setDVBS(sat);
if ( p.orbital_position != sat.orbital_position)
SCAN_eDebug("[eDVBScan] dropping this transponder, it's on another satellite.");
else
{
addChannelToScan(feparm);
}
break;
}
case EXTENSION_DESCRIPTOR:
{
if (system != iDVBFrontend::feTerrestrial)
break; // when current locked transponder is no terrestrial transponder ignore this descriptor
ExtensionDescriptor &d = (ExtensionDescriptor&)**desc;
switch (d.getExtensionTag())
{
case T2_DELIVERY_SYSTEM_DESCRIPTOR:
T2 = true;
T2DeliverySystemDescriptor &d = (T2DeliverySystemDescriptor&)**desc;
t2transponder.set(d);
for (T2CellConstIterator cell = d.getCells()->begin();
cell != d.getCells()->end(); ++cell)
{
for (T2FrequencyConstIterator freq = (*cell)->getCentreFrequencies()->begin();
freq != (*cell)->getCentreFrequencies()->end(); ++freq)
{
t2transponder.frequency = (*freq) * 10;
ePtr<eDVBFrontendParameters> feparm = new eDVBFrontendParameters;
feparm->setDVBT(t2transponder);
addChannelToScan(feparm);
}
}
}
break;
}
case PRIVATE_DATA_SPECIFIER_DESCRIPTOR:
case DEFAULT_AUTHORITY_DESCRIPTOR:
{
// later, when we have the namespace
break;