-
Notifications
You must be signed in to change notification settings - Fork 12
/
BonDriver_DVB.cpp
1645 lines (1555 loc) · 42.6 KB
/
BonDriver_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 "BonDriver_DVB.h"
namespace BonDriver_DVB {
static char g_strSpace[32];
static stChannel g_stChannels[2][MAX_CH];
static int g_AdapterNo;
static int g_Type; // 0 : ISDB_S / 1 : ISDB_T <- 判定はm_fefdにioctl()かけるまで保留
static int g_GetCnrMode; // 0 : FE_READ_SIGNAL_STRENGTH / 1 : FE_READ_SNR / 2 : DTV_STAT_CNR
static BOOL g_UseLNB;
static BOOL g_UseServiceID;
static DWORD g_Crc32Table[256];
static BOOL g_ModPMT;
static BOOL g_TsSync;
static DWORD g_dwDelFlag;
static int Convert(char *src, char *dst, size_t dstsize)
{
iconv_t d = ::iconv_open("UTF-16LE", "UTF-8");
if (d == (iconv_t)-1)
return -1;
size_t srclen = ::strlen(src) + 1;
size_t dstlen = dstsize - 2;
size_t ret = ::iconv(d, &src, &srclen, &dst, &dstlen);
*dst = *(dst + 1) = '\0';
::iconv_close(d);
if (ret == (size_t)-1)
return -2;
return 0;
}
static void InitCrc32Table()
{
DWORD i, j, crc;
for (i = 0; i < 256; i++)
{
crc = i << 24;
for (j = 0; j < 8; j++)
crc = (crc << 1) ^ ((crc & 0x80000000) ? 0x04c11db7 : 0);
g_Crc32Table[i] = crc;
}
}
static DWORD CalcCRC32(BYTE *p, DWORD len)
{
DWORD i, crc = 0xffffffff;
for (i = 0; i < len; i++)
crc = (crc << 8) ^ g_Crc32Table[(crc >> 24) ^ p[i]];
return crc;
}
static BOOL IsTagMatch(const char *line, const char *tag, char **value)
{
const int taglen = ::strlen(tag);
const char *p;
if (::strncmp(line, tag, taglen) != 0)
return FALSE;
p = line + taglen;
while (*p == ' ' || *p == '\t')
p++;
if (value == NULL && *p == '\0')
return TRUE;
if (*p++ != '=')
return FALSE;
while (*p == ' ' || *p == '\t')
p++;
*value = const_cast<char *>(p);
return TRUE;
}
static int Init()
{
FILE *fp;
char *p, buf[512];
Dl_info info;
if (::dladdr((void *)Init, &info) == 0)
return -1;
::strncpy(buf, info.dli_fname, sizeof(buf) - 8);
buf[sizeof(buf) - 8] = '\0';
::strcat(buf, ".conf");
fp = ::fopen(buf, "r");
if (fp == NULL)
return -2;
for (int i = 0; i < MAX_CH; i++)
{
g_stChannels[0][i].bUnused = TRUE;
g_stChannels[1][i].bUnused = TRUE;
}
int idx = 0;
BOOL baFlag = FALSE;
BOOL bgFlag = FALSE;
BOOL blFlag = FALSE;
BOOL bsFlag = FALSE;
BOOL bmFlag = FALSE;
BOOL btFlag = FALSE;
BOOL bdFlag = FALSE;
while (::fgets(buf, sizeof(buf), fp))
{
if (buf[0] == ';')
continue;
p = buf + ::strlen(buf) - 1;
while ((p >= buf) && (*p == '\r' || *p == '\n'))
*p-- = '\0';
if (p < buf)
continue;
if ((idx != 0) && IsTagMatch(buf, "#ISDB_S", NULL))
idx = 0;
else if ((idx != 1) && IsTagMatch(buf, "#ISDB_T", NULL))
idx = 1;
else if (!baFlag && IsTagMatch(buf, "#ADAPTER_NO", &p))
{
g_AdapterNo = ::atoi(p);
baFlag = TRUE;
}
else if (!bgFlag && IsTagMatch(buf, "#GETCNRMODE", &p))
{
g_GetCnrMode = ::atoi(p);
bgFlag = TRUE;
}
else if (!blFlag && IsTagMatch(buf, "#USELNB", &p))
{
g_UseLNB = ::atoi(p);
blFlag = TRUE;
}
else if (!bsFlag && IsTagMatch(buf, "#USESERVICEID", &p))
{
g_UseServiceID = ::atoi(p);
bsFlag = TRUE;
}
else if (!bmFlag && IsTagMatch(buf, "#MODPMT", &p))
{
g_ModPMT = ::atoi(p);
bmFlag = TRUE;
}
else if (!btFlag && IsTagMatch(buf, "#TSSYNC", &p))
{
g_TsSync = ::atoi(p);
btFlag = TRUE;
}
else if (!bdFlag && IsTagMatch(buf, "#DEL", &p))
{
char *pb = p;
const char *name[] = { "EIT", "H-EIT", "M-EIT", "L-EIT", "CAT", "NIT", "SDT", "TOT", "SDTT", "BIT", "CDT", "ECM", "EMM", "TYPED", NULL };
int n, cnt = 1;
while (*p != '\0')
{
if (*p == ',')
cnt++;
p++;
}
char **pp = new char *[cnt];
p = pb;
n = 0;
do
{
while (*p == '\t' || *p == ' ')
p++;
pp[n++] = p;
while (*p != '\t' && *p != ' ' && *p != ',' && *p != '\0')
p++;
if (*p != ',' && *p != '\0')
{
*p++ = '\0';
while (*p != ',' && *p != '\0')
p++;
}
*p++ = '\0';
} while (n < cnt);
for (int i = 0; i < cnt; i++)
{
for (int j = 0; name[j] != NULL; j++)
{
if (strcmp(pp[i], name[j]) == 0)
{
if (j == 0)
g_dwDelFlag |= 0x7; // EIT = H-EIT | M-EIT | L-EIT
else
g_dwDelFlag |= (1 << (j - 1));
break;
}
}
}
delete[] pp;
bdFlag = TRUE;
}
else
{
int n = 0;
char *cp[5];
BOOL bOk = FALSE;
p = cp[n++] = buf;
while (1)
{
p = ::strchr(p, '\t');
if (p)
{
*p++ = '\0';
cp[n++] = p;
if (g_UseServiceID)
{
if (n > 4)
{
bOk = TRUE;
break;
}
}
else
{
if (n > 3)
{
bOk = TRUE;
break;
}
}
}
else
break;
}
if (bOk)
{
DWORD dw = ::atoi(cp[1]);
if (dw < MAX_CH)
{
if (Convert(cp[0], g_stChannels[idx][dw].strChName, MAX_CN_LEN) < 0)
{
::fclose(fp);
return -3;
}
g_stChannels[idx][dw].freq.frequencyno = ::atoi(cp[2]);
g_stChannels[idx][dw].freq.tsid = ::strtoul(cp[3], NULL, 16);
if (g_UseServiceID)
g_stChannels[idx][dw].ServiceID = ::strtoul(cp[4], NULL, 10);
g_stChannels[idx][dw].bUnused = FALSE;
}
}
}
}
::fclose(fp);
if (g_UseServiceID)
InitCrc32Table();
return 0;
}
static float CalcCNR_S(uint16_t signal)
{
static const float fCnrTable[] = {
30.344f, // 0x0000
30.344f, // 0x1000
19.163f, // 0x2000
15.215f, // 0x3000
12.405f, // 0x4000
10.147f, // 0x5000
8.198f, // 0x6000
6.285f, // 0x7000
4.074f, // 0x8000
1.177f, // 0x9000
0.000f // 0xa000 <- 実際に0になるのは0x9550だが、CNRがこの辺の値での誤差は実用上問題無しと判断
};
unsigned char idx = (signal >> 12) & 0xff;
if (idx <= 0x1)
return 30.344f;
else if (idx >= 0xa)
return 0.0f;
// 線形補間
unsigned short off = signal & 0x0fff;
return fCnrTable[idx] - (((fCnrTable[idx] - fCnrTable[idx + 1]) / 4096) * off);
}
static float CalcCNR_T(uint16_t signal)
{
double P = ::log10(5505024 / (double)signal) * 10;
return (float)((0.000024 * P * P * P * P) - (0.0016 * P * P * P) + (0.0398 * P * P) + (0.5491 * P) + 3.0965);
}
static unsigned int GetFrequency_S(int ch)
{
unsigned int freq;
if (ch < 12)
freq = 1049480 + 38360 * ch;
else if (ch < 24)
freq = 1613000 + 40000 * (ch - 12);
else if (ch < 36)
freq = 1593000 + 40000 * (ch - 24);
else
freq = 1049480; // 変な値なので0chに
return freq;
}
static unsigned int GetFrequency_T(int ch)
{
unsigned int freq;
if (ch < 12)
freq = 93142857 + 6000000 * ch;
else if (ch < 17)
freq = 167142857 + 6000000 * (ch - 12);
else if (ch < 63)
freq = 195142857 + 6000000 * (ch - 17);
else if (ch < 113)
freq = 473142857 + 6000000 * (ch - 63);
else
freq = 93142857; // 変な値なので0chに
return freq;
}
cBonDriverDVB *cBonDriverDVB::m_spThis = NULL;
cCriticalSection cBonDriverDVB::m_sInstanceLock;
BOOL cBonDriverDVB::m_sbInit = TRUE;
extern "C" IBonDriver *CreateBonDriver()
{
LOCK(cBonDriverDVB::m_sInstanceLock);
if (cBonDriverDVB::m_sbInit)
{
if (Init() < 0)
return NULL;
cBonDriverDVB::m_sbInit = FALSE;
}
// 複数読み込み禁止
cBonDriverDVB *pDVB = NULL;
if (cBonDriverDVB::m_spThis == NULL)
pDVB = new cBonDriverDVB();
return pDVB;
}
cBonDriverDVB::cBonDriverDVB() : m_fifoTS(m_c, m_m), m_fifoRawTS(m_c, m_m), m_StopTsSplit(m_c, m_m)
{
m_spThis = this;
Convert((char *)TUNER_NAME, m_TunerName, sizeof(m_TunerName));
m_LastBuf = NULL;
m_bTuner = FALSE;
m_fCNR = 0;
m_dwSpace = m_dwChannel = 0x7fffffff; // INT_MAX
m_dwServiceID = 0xffffffff;
m_fefd = m_dmxfd = m_dvrfd = -1;
m_hTsRead = m_hTsSplit = 0;
m_bStopTsRead = m_bChannelChanged = m_bUpdateCNR = FALSE;
m_dwUnitSize = m_dwSyncBufPos = 0;
pthread_mutexattr_t attr;
::pthread_mutexattr_init(&attr);
::pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
::pthread_mutex_init(&m_m, &attr);
::pthread_cond_init(&m_c, NULL);
}
cBonDriverDVB::~cBonDriverDVB()
{
CloseTuner();
{
LOCK(m_writeLock);
TsFlush(g_UseServiceID);
delete m_LastBuf;
}
::pthread_cond_destroy(&m_c);
::pthread_mutex_destroy(&m_m);
m_spThis = NULL;
}
const BOOL cBonDriverDVB::OpenTuner(void)
{
if (m_bTuner)
return TRUE;
char buf[64];
::sprintf(buf, "/dev/dvb/adapter%d/frontend0", g_AdapterNo);
m_fefd = ::open(buf, O_RDWR | O_CLOEXEC);
if (m_fefd < 0)
{
::fprintf(stderr, "OpenTuner() frontend open() error: adapter%d\n", g_AdapterNo);
return FALSE;
}
dvb_frontend_info info;
if (::ioctl(m_fefd, FE_GET_INFO, &info) < 0)
{
::fprintf(stderr, "OpenTuner() ioctl(FE_GET_INFO) error: adapter%d\n", g_AdapterNo);
goto err1;
}
char *p;
if (info.type == FE_QPSK)
{
g_Type = 0;
p = (char *)"BS/CS110";
}
else if (info.type == FE_OFDM)
{
g_Type = 1;
p = (char *)"UHF";
}
else
{
::fprintf(stderr, "OpenTuner() adapter type unknown: adapter%d\n", g_AdapterNo);
goto err1;
}
if (Convert(p, g_strSpace, sizeof(g_strSpace)) < 0)
goto err1;
::sprintf(buf, "/dev/dvb/adapter%d/demux0", g_AdapterNo);
m_dmxfd = ::open(buf, O_RDONLY | O_CLOEXEC);
if (m_dmxfd < 0)
{
::fprintf(stderr, "OpenTuner() demux open() error: adapter%d\n", g_AdapterNo);
goto err1;
}
dmx_pes_filter_params filter;
// ::memset(&filter, 0, sizeof(filter));
filter.pid = 0x2000;
filter.input = DMX_IN_FRONTEND;
filter.output = DMX_OUT_TS_TAP;
filter.pes_type = DMX_PES_VIDEO;
filter.flags = DMX_IMMEDIATE_START;
if (::ioctl(m_dmxfd, DMX_SET_PES_FILTER, &filter) < 0)
{
::fprintf(stderr, "OpenTuner() ioctl(DMX_SET_PES_FILTER) error: adapter%d\n", g_AdapterNo);
goto err0;
}
::sprintf(buf, "/dev/dvb/adapter%d/dvr0", g_AdapterNo);
// 休止チャンネルなどでCloseTuner()がブロックしてしまうのでO_NONBLOCKが必要
m_dvrfd = ::open(buf, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
if (m_dvrfd < 0)
{
::fprintf(stderr, "OpenTuner() dvr open() error: adapter%d\n", g_AdapterNo);
goto err0;
}
if (g_UseLNB && (g_Type == 0))
{
dtv_property prop[1];
prop[0].cmd = DTV_VOLTAGE;
prop[0].u.data = SEC_VOLTAGE_18;
dtv_properties props;
// ::memset(&props, 0, sizeof(props));
props.props = prop;
props.num = 1;
if (::ioctl(m_fefd, FE_SET_PROPERTY, &props) < 0)
::fprintf(stderr, "LNB ON failed: adapter%d\n", g_AdapterNo);
}
m_bTuner = TRUE;
return TRUE;
err0:
::close(m_dmxfd);
m_dmxfd = -1;
err1:
::close(m_fefd);
m_fefd = -1;
return FALSE;
}
void cBonDriverDVB::CloseTuner(void)
{
if (m_bTuner)
{
if (m_hTsRead)
{
m_bStopTsRead = TRUE;
::pthread_join(m_hTsRead, NULL);
m_hTsRead = 0;
}
if (g_UseLNB && (g_Type == 0))
{
dtv_property prop[1];
prop[0].cmd = DTV_VOLTAGE;
prop[0].u.data = SEC_VOLTAGE_OFF;
dtv_properties props;
// ::memset(&props, 0, sizeof(props));
props.props = prop;
props.num = 1;
if (::ioctl(m_fefd, FE_SET_PROPERTY, &props) < 0)
::fprintf(stderr, "LNB OFF failed: adapter%d\n", g_AdapterNo);
}
::close(m_dvrfd);
::close(m_dmxfd);
::close(m_fefd);
m_fefd = m_dmxfd = m_dvrfd = -1;
m_bTuner = FALSE;
}
}
const BOOL cBonDriverDVB::SetChannel(const BYTE bCh)
{
return TRUE;
}
const float cBonDriverDVB::GetSignalLevel(void)
{
timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 10 * 1000 * 1000;
for (int i = 0; m_bUpdateCNR && (i < 50); i++)
::nanosleep(&ts, NULL);
return m_fCNR;
}
const DWORD cBonDriverDVB::WaitTsStream(const DWORD dwTimeOut)
{
if (!m_bTuner)
return WAIT_ABANDONED;
if (m_fifoTS.Size() != 0)
return WAIT_OBJECT_0;
else
return WAIT_TIMEOUT; // 手抜き
}
const DWORD cBonDriverDVB::GetReadyCount(void)
{
if (!m_bTuner)
return 0;
return (DWORD)m_fifoTS.Size();
}
const BOOL cBonDriverDVB::GetTsStream(BYTE *pDst, DWORD *pdwSize, DWORD *pdwRemain)
{
if (!m_bTuner)
return FALSE;
BYTE *pSrc;
if (GetTsStream(&pSrc, pdwSize, pdwRemain))
{
if (*pdwSize)
::memcpy(pDst, pSrc, *pdwSize);
return TRUE;
}
return FALSE;
}
const BOOL cBonDriverDVB::GetTsStream(BYTE **ppDst, DWORD *pdwSize, DWORD *pdwRemain)
{
if (!m_bTuner)
return FALSE;
BOOL b;
{
LOCK(m_writeLock);
if (m_fifoTS.Size() != 0)
{
delete m_LastBuf;
m_fifoTS.Pop(&m_LastBuf);
*ppDst = m_LastBuf->pbBuf;
*pdwSize = m_LastBuf->dwSize;
*pdwRemain = (DWORD)m_fifoTS.Size();
b = TRUE;
}
else
{
*pdwSize = 0;
*pdwRemain = 0;
b = FALSE;
}
}
return b;
}
void cBonDriverDVB::PurgeTsStream(void)
{
if (!m_bTuner)
return;
{
LOCK(m_writeLock);
TsFlush(g_UseServiceID);
}
}
void cBonDriverDVB::Release(void)
{
LOCK(m_sInstanceLock);
delete this;
}
LPCTSTR cBonDriverDVB::GetTunerName(void)
{
return (LPCTSTR)m_TunerName;
}
const BOOL cBonDriverDVB::IsTunerOpening(void)
{
return FALSE;
}
LPCTSTR cBonDriverDVB::EnumTuningSpace(const DWORD dwSpace)
{
if (!m_bTuner)
return NULL;
if (dwSpace != 0)
return NULL;
return (LPCTSTR)g_strSpace;
}
LPCTSTR cBonDriverDVB::EnumChannelName(const DWORD dwSpace, const DWORD dwChannel)
{
if (!m_bTuner)
return NULL;
if (dwSpace != 0)
return NULL;
if (dwChannel >= MAX_CH)
return NULL;
if (g_stChannels[g_Type][dwChannel].bUnused)
return NULL;
return (LPCTSTR)(g_stChannels[g_Type][dwChannel].strChName);
}
// 経緯的にはDTV_ISDBS_TS_IDがDTV_STREAM_IDに置き換わり、その際にDTV_ISDBS_TS_ID_LEGACYが
// 追加されたらしいので、多分これで大丈夫…
#ifndef DTV_STREAM_ID
#define DTV_STREAM_ID DTV_ISDBS_TS_ID
#endif
const BOOL cBonDriverDVB::SetChannel(const DWORD dwSpace, const DWORD dwChannel)
{
BOOL bFlag;
if (!m_bTuner)
goto err;
if (dwSpace != 0)
goto err;
if (dwChannel >= MAX_CH)
goto err;
if (g_stChannels[g_Type][dwChannel].bUnused)
goto err;
if (dwChannel == m_dwChannel)
return TRUE;
bFlag = TRUE;
if (g_UseServiceID)
{
if (m_dwChannel != 0x7fffffff)
{
if ((g_stChannels[g_Type][dwChannel].freq.frequencyno == g_stChannels[g_Type][m_dwChannel].freq.frequencyno) &&
(g_stChannels[g_Type][dwChannel].freq.tsid == g_stChannels[g_Type][m_dwChannel].freq.tsid))
{
bFlag = FALSE;
}
}
m_bChannelChanged = TRUE;
m_dwServiceID = g_stChannels[g_Type][dwChannel].ServiceID;
}
if (bFlag)
{
unsigned int f;
dtv_property prop[3];
dtv_properties props;
// ::memset(&props, 0, sizeof(props));
if (g_Type == 0)
{
f = GetFrequency_S(g_stChannels[g_Type][dwChannel].freq.frequencyno);
prop[0].cmd = DTV_FREQUENCY;
prop[0].u.data = f;
prop[1].cmd = DTV_STREAM_ID;
prop[1].u.data = g_stChannels[g_Type][dwChannel].freq.tsid;
prop[2].cmd = DTV_TUNE;
props.props = prop;
props.num = 3;
}
else
{
f = GetFrequency_T(g_stChannels[g_Type][dwChannel].freq.frequencyno);
prop[0].cmd = DTV_FREQUENCY;
prop[0].u.data = f;
prop[1].cmd = DTV_TUNE;
props.props = prop;
props.num = 2;
}
if (::ioctl(m_fefd, FE_SET_PROPERTY, &props) < 0)
{
::fprintf(stderr, "SetChannel() ioctl(FE_SET_PROPERTY) error: adapter%d\n", g_AdapterNo);
goto err;
}
fe_status_t status;
timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 250 * 1000 * 1000; // 250ms
BOOL bOk = FALSE;
for (int i = 0; i < 12; i++) // 250ms * 12で最大3秒待つ
{
if (::ioctl(m_fefd, FE_READ_STATUS, &status) < 0)
{
::fprintf(stderr, "SetChannel() ioctl(FE_READ_STATUS) error: adapter%d\n", g_AdapterNo);
goto err;
}
if (status & FE_HAS_LOCK)
{
bOk = TRUE;
break;
}
::nanosleep(&ts, NULL);
}
if (!bOk)
{
::fprintf(stderr, "SetChannel() timeout: adapter%d\n", g_AdapterNo);
goto err;
}
}
{
LOCK(m_writeLock);
TsFlush(g_UseServiceID);
m_bUpdateCNR = TRUE;
}
if (!m_hTsRead)
{
m_bStopTsRead = FALSE;
if (::pthread_create(&m_hTsRead, NULL, cBonDriverDVB::TsReader, this))
{
::perror("pthread_create1");
goto err;
}
}
m_dwSpace = dwSpace;
m_dwChannel = dwChannel;
return TRUE;
err:
m_bChannelChanged = FALSE;
m_fCNR = 0;
return FALSE;
}
const DWORD cBonDriverDVB::GetCurSpace(void)
{
return m_dwSpace;
}
const DWORD cBonDriverDVB::GetCurChannel(void)
{
return m_dwChannel;
}
void *cBonDriverDVB::TsReader(LPVOID pv)
{
cBonDriverDVB *pDVB = static_cast<cBonDriverDVB *>(pv);
DWORD now, before = 0;
DWORD &ret = pDVB->m_tRet;
ret = 300;
BYTE *pBuf, *pTsBuf;
timeval tv;
timespec ts;
int len, pos;
struct pollfd pfd;
pfd.fd = pDVB->m_dvrfd;
pfd.events = POLLIN;
if (g_UseServiceID)
{
if (::pthread_create(&(pDVB->m_hTsSplit), NULL, cBonDriverDVB::TsSplitter, pDVB))
{
::perror("pthread_create2");
ret = 301;
return &ret;
}
}
ts.tv_sec = 0;
ts.tv_nsec = WAIT_TIME * 1000 * 1000;
// TS読み込みループ
pTsBuf = new BYTE[TS_BUFSIZE];
pos = 0;
while (!pDVB->m_bStopTsRead)
{
pDVB->m_writeLock.Enter();
::gettimeofday(&tv, NULL);
now = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
if (((now - before) >= 1000) || pDVB->m_bUpdateCNR)
{
float f = 0;
if (g_GetCnrMode == 2)
{
#ifdef DTV_STAT_CNR
dtv_property prop[1];
// ::memset(prop, 0, sizeof(prop));
prop[0].cmd = DTV_STAT_CNR;
dtv_properties props;
// ::memset(&props, 0, sizeof(props));
props.props = prop;
props.num = 1;
if (::ioctl(pDVB->m_fefd, FE_GET_PROPERTY, &props) < 0)
::fprintf(stderr, "TsReader() ioctl(FE_GET_PROPERTY:DTV_STAT_CNR) error: adapter%d\n", g_AdapterNo);
else
f = (float)prop[0].u.st.stat[0].svalue / 1000;
#endif
}
else if (g_GetCnrMode == 1)
{
int16_t cnr;
if (::ioctl(pDVB->m_fefd, FE_READ_SNR, &cnr) < 0)
::fprintf(stderr, "TsReader() ioctl(FE_READ_SNR) error: adapter%d\n", g_AdapterNo);
else
f = (float)cnr / 1000;
}
else
{
int16_t signal;
if (::ioctl(pDVB->m_fefd, FE_READ_SIGNAL_STRENGTH, &signal) < 0)
::fprintf(stderr, "TsReader() ioctl(FE_READ_SIGNAL_STRENGTH) error: adapter%d\n", g_AdapterNo);
else
{
if (g_Type == 0)
f = CalcCNR_S(signal);
else
f = CalcCNR_T(signal);
}
}
pDVB->m_fCNR = f;
before = now;
if (pDVB->m_bUpdateCNR)
{
pos = 0;
pDVB->m_bUpdateCNR = FALSE;
}
}
if (pos == TS_BUFSIZE)
{
TS_DATA *pData = new TS_DATA();
pData->dwSize = TS_BUFSIZE;
pData->pbBuf = pTsBuf;
if (g_UseServiceID)
pDVB->m_fifoRawTS.Push(pData);
else
pDVB->m_fifoTS.Push(pData);
pTsBuf = new BYTE[TS_BUFSIZE];
pos = 0;
}
pDVB->m_writeLock.Leave();
// この不自然な位置で読み込みを行うのは、ロック/アンロックの回数を減らしつつ
// スレッド起動時に初回の読み込みを捨てないようにする為…
if((::poll(&pfd, 1, WAIT_TIME_POLL) > 0) && (pfd.revents & POLLIN))
{
pBuf = pTsBuf + pos;
if ((len = ::read(pDVB->m_dvrfd, pBuf, TS_BUFSIZE - pos)) > 0)
{
pos += len;
}
::nanosleep(&ts, NULL);
}
}
delete[] pTsBuf;
if (g_UseServiceID)
{
pDVB->m_StopTsSplit.Set();
::pthread_join(pDVB->m_hTsSplit, NULL);
pDVB->m_hTsSplit = 0;
pDVB->m_StopTsSplit.Reset();
}
return &ret;
}
#define MAX_PID 0x2000 // (8 * sizeof(int))で割り切れる
#define PID_SET(pid, map) ((map)->bits[(pid) / (8 * sizeof(int))] |= (1 << ((pid) % (8 * sizeof(int)))))
#define PID_CLR(pid, map) ((map)->bits[(pid) / (8 * sizeof(int))] &= ~(1 << ((pid) % (8 * sizeof(int)))))
#define PID_ISSET(pid, map) ((map)->bits[(pid) / (8 * sizeof(int))] & (1 << ((pid) % (8 * sizeof(int)))))
#define PID_MERGE(dst, src) {for(int i=0;i<(int)(MAX_PID / (8 * sizeof(int)));i++){(dst)->bits[i] |= (src)->bits[i];}}
#define PID_ZERO(map) (::memset((map), 0 , sizeof(*(map))))
struct pid_set {
int bits[MAX_PID / (8 * sizeof(int))];
};
#define FLAG_HEIT 0x0001
#define FLAG_MEIT 0x0002
#define FLAG_LEIT 0x0004
#define FLAG_CAT 0x0008
#define FLAG_NIT 0x0010
#define FLAG_SDT 0x0020
#define FLAG_TOT 0x0040
#define FLAG_SDTT 0x0080
#define FLAG_BIT 0x0100
#define FLAG_CDT 0x0200
#define FLAG_ECM 0x0400
#define FLAG_EMM 0x0800
#define FLAG_TYPED 0x1000
void *cBonDriverDVB::TsSplitter(LPVOID pv)
{
cBonDriverDVB *pDVB = static_cast<cBonDriverDVB *>(pv);
BYTE *pTsBuf, pPAT[TS_PKTSIZE];
BYTE pPMT[4104+TS_PKTSIZE]; // 4104 = 8(TSヘッダ + pointer_field + table_idからsection_length) + 4096(セクション長最大値)
BYTE pPMTPackets[TS_PKTSIZE*32];
int pos, iNumSplit;
unsigned char pat_ci, rpmt_ci, wpmt_ci, lpmt_version, lcat_version, ver;
unsigned short ltsid, pidPMT, pidEMM, pmt_tail;
BOOL bChangePMT, bSplitPMT, bPMTComplete;
pid_set pids, save_pids[2], *p_new_pids, *p_old_pids;
pTsBuf = new BYTE[TS_BUFSIZE];
pos = 0;
pat_ci = 0x10; // 0x1(payloadのみ) << 4 | 0x0(ci初期値)
lpmt_version = lcat_version = wpmt_ci = 0xff;
ltsid = pidPMT = pidEMM = 0xffff; // 現在のTSID及びPMT,EMMのPID
bChangePMT = bSplitPMT = bPMTComplete = FALSE;
PID_ZERO(&pids);
p_new_pids = &save_pids[0];
p_old_pids = &save_pids[1];
PID_ZERO(p_new_pids);
PID_ZERO(p_old_pids);
cEvent *h[2] = { &(pDVB->m_StopTsSplit), pDVB->m_fifoRawTS.GetEventHandle() };
while (1)
{
DWORD dwRet = cEvent::MultipleWait(2, h);
switch (dwRet)
{
case WAIT_OBJECT_0:
goto end;
case WAIT_OBJECT_0 + 1:
{
TS_DATA *pRawBuf = NULL;
pDVB->m_fifoRawTS.Pop(&pRawBuf);
if (pRawBuf == NULL) // イベントのトリガからPop()までの間に別スレッドにFlush()される可能性はゼロではない
break;
BYTE *pSrc, *pSrcHead;
DWORD dwLeft;
if (g_TsSync)
{
pDVB->TsSync(pRawBuf->pbBuf, pRawBuf->dwSize, &pSrcHead, &dwLeft);
pSrc = pSrcHead;
}
else
{
pSrc = pRawBuf->pbBuf;
dwLeft = pRawBuf->dwSize; // 必ずTS_PKTSIZEの倍数で来る
}
while (dwLeft > 0)
{
unsigned short pid = GetPID(&pSrc[1]);
if (pid == 0x0000) // PAT
{
// ビットエラー無しかつpayload先頭かつadaptation_field無し、PSIのpointer_fieldは0x00の前提
if (!(pSrc[1] & 0x80) && (pSrc[1] & 0x40) && !(pSrc[3] & 0x20) && (pSrc[4] == 0x00))
{
// section_length
// 9 = transport_stream_idからlast_section_numberまでの5バイト + CRC_32の4バイト
int len = (((int)(pSrc[6] & 0x0f) << 8) | pSrc[7]) - 9;
// 13 = TSパケットの頭から最初のprogram_numberまでのオフセット
int off = 13;
// PATは1TSパケットに収まってる前提
while ((len >= 4) && ((off + 4) < TS_PKTSIZE))
{
unsigned short sid = GetSID(&pSrc[off]);
if (pDVB->m_dwServiceID == sid)
{
pid = GetPID(&pSrc[off+2]);
break;
}
off += 4;
len -= 4;
}
if (pid != 0x0000) // 対象ServiceIDのPMTのPIDが取得できた
{
// transport_stream_id
unsigned short tsid = ((unsigned short)pSrc[8] << 8) | pSrc[9];
if (pidPMT != pid || ltsid != tsid) // PMTのPIDが更新された or チャンネルが変更された
{
// TSヘッダ
pPAT[0] = 0x47;
pPAT[1] = 0x60;
pPAT[2] = 0x00;
pPAT[3] = pat_ci;
// pointer_field
pPAT[4] = 0x00;
// PAT
pPAT[5] = 0x00; // table_id
pPAT[6] = 0xb0; // section_syntax_indicator(1) + '0'(1) + reserved(2) + section_length(4/12)
pPAT[7] = 0x11; // section_length(8/12)
pPAT[8] = tsid >> 8;
pPAT[9] = tsid & 0xff;
pPAT[10] = 0xc1; // reserved(2) + version_number(5) + current_next_indicator(1)
pPAT[11] = 0x00; // section_number
pPAT[12] = 0x00; // last_section_number
pPAT[13] = 0x00; // program_number(8/16)
pPAT[14] = 0x00; // program_number(8/16)
pPAT[15] = 0xe0; // reserved(3) + network_PID(5/13)
pPAT[16] = 0x10; // network_PID(8/13)
// 対象ServiceIDのテーブルコピー
pPAT[17] = pSrc[off];
pPAT[18] = pSrc[off+1];
pPAT[19] = pSrc[off+2];