-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcmmbFile.cpp
2071 lines (1663 loc) · 53.2 KB
/
cmmbFile.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
// CMMB_File.cpp: implementation of the CMMB_File class.
//
//////////////////////////////////////////////////////////////////////
#include "cmmbFile.h"
#define TIME_SEND 20000 //20
#define MUXER_IP "234.1.2.3"
#define MUXER_PORT 9001
const byte commonKey[17] = {"abcdefghijklmnop"};
const byte commonIv[17] = {"1234567890123456"};
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//====================================================
static PRS_PARAM_t rsHandle = NULL;
//static U8* fec_block = NULL;
//static U8 fec_buf[32768];
//static S32 fec_length = 0;
//static thread_params_t* gpThreadParams = NULL;
//static SOCKET gsockUdp;
//static SOCKADDR_IN gaddrOut;
static UINT m_netRate=0;
static Udp m_sUdp;
extern thread_params_t gkThreadParams;
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
BYTE FillBuffer[188];
extern char bind_ip[16];
/////////////////////////////////////////////////////////////////////
static unsigned short crctab[1<<B] = {// as calculated by initcrctab()
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
};
//===========================================================
static WORD Get_16bitCRC(WORD icrc, BYTE* icp, UINT icnt)
{
unsigned short crc = icrc;
unsigned char *cp = icp;
unsigned int cnt = icnt;
while(cnt--)
crc = (crc<<B) ^ crctab[(crc>>(W-B)) ^ *cp++];
return (crc);
}
// Create XDB file
void CMMB_DTMB_MUX::CreateXDB(BYTE* pSrc, BYTE* pDst, WORD streamNo, UINT frameSeq, BYTE dataLen)
{
if((!pSrc)||(!pDst))return;
WORD crc = 0;
pDst[0]=0xff;
pDst[1]=dataLen+6; //fileNum + frameSeqNo + dataLen 179
pDst[2]=0x00;
pDst[3]=0x00;
pDst[4]=0x10;
pDst[5]=(streamNo>>8)&0xff;
pDst[6]=(streamNo)&0xff;
pDst[7]=(frameSeq>>24)&0xff;
pDst[8]=(frameSeq>>16)&0xff;
pDst[9]=(frameSeq>>8)&0xff;
pDst[10]=(frameSeq)&0xff;
memcpy(pDst+11,pSrc, dataLen);
crc = Get_16bitCRC(0, pDst+4, dataLen+7);
pDst[2] = (crc>>8)&0xff;
pDst[3] = (crc)&0xff;
}
void CMMB_DTMB_MUX::SetEncryptFlag(int flag)
{
encryptFlag = flag;
}
CMMB_DTMB_MUX::CMMB_DTMB_MUX()
{
m_srcFileName = "";
dstFileName = "";
tsFileName = "";
simpleFileName= "";
xpeFileName = "";
msgFileName = "";
encryptFlag = 1;
m_sendType = CMMB_MODE;
//================================
string cmd=CMD_FOLDER;
cmd += BS_SRCFILE;
system(cmd.c_str());
cmd = CMD_FOLDER;
cmd += CS_DSTFILE;
system(cmd.c_str());
//================================
}
// Create TS frame
void CMMB_DTMB_MUX::CreateTS(BYTE* pSrc, BYTE* pDst, BYTE count)
{
WORD pid=0;
//m_editPid.GetWindowTextA(pidStr);
pid=(WORD)atoi("8000");
pDst[0]=0x47;
pDst[1]=0x40 | ((pid>>8)&0x1F);
pDst[2]=pid & 0xff;
pDst[3]=0x10 | (count%16);
memcpy(pDst+4, pSrc, XDB_FRAM_LEN);
}
void CMMB_DTMB_MUX::CreateRS(BYTE* pSrc, BYTE* pDst, UINT count)
{
if(!rsHandle || !pDst || !pSrc)return;
memset(pDst, 0, RS_MSG_LEN);
memcpy(pDst, pSrc, count);
encode_rs_char(rsHandle, pDst, pDst + RS_K);
}
void CMMB_DTMB_MUX::CreateXPE(BYTE* pSrc, BYTE* xpe_buf, UINT srcLen, thread_params_t* pThreadParams, SERVICE_PARAM_t* pServiceParam, U8 packet_ID)
{
BYTE* payload_buf = NULL;
U32 payload_length = 0;
U32 xpe_length = 0;
U32 crc_32 = 0;
U32 xpe_single_packet_addin = 0;
payload_buf = pSrc;
payload_length = srcLen;
xpe_single_packet_addin = 5 + 4; //xpe header + CRC
xpe_length = payload_length + xpe_single_packet_addin;
xpe_buf[0] = 0x00;
xpe_buf[0] |= 0x80; //开始标志置1
xpe_buf[0] |= 0x40; //结束标志置1
//净荷类型
if (pServiceParam->FEC_style != FEC_STYLE_NULL)
{
//xpe_buf[0] |= 0x10; //进行过前向纠错的业务数据
}
xpe_buf[0] |= (payload_length & 0xf00) >> 8; //长度位12bits
xpe_buf[1] = (payload_length & 0x0ff);
xpe_buf[2] = 0x00;
//if (pServiceParam->bCRC32Indic)
{
xpe_buf[2] |= 0x80; //CRC指示1bits
}
xpe_buf[2] |= (packet_ID & 0x7f); //数据包标识:7bits
xpe_buf[3] = 0x00;
xpe_buf[3] |= ((0x01 & 0x01) << 7); //业务模式指示:1bit
xpe_buf[3] |= 0x7f; //reserved : 7bits
xpe_buf[4] = CalculateCheckSum(xpe_buf, 4); //校验和:8bits
memcpy(xpe_buf + 5, payload_buf, payload_length);
//FEC纠错能力测试
// if (gkThreadParams.add_error_count > 0)
// {
// memset(xpe_buf + 5, 0x00, gkThreadParams.add_error_count);
// }
if (pServiceParam->bCRC32Indic)
{
crc_32 = Encode_CRC_32(payload_buf, payload_length);
xpe_buf[xpe_length - 4] = (crc_32 & 0xff000000) >> 24;
xpe_buf[xpe_length - 3] = (crc_32 & 0x00ff0000) >> 16;
xpe_buf[xpe_length - 2] = (crc_32 & 0x0000ff00) >> 8;
xpe_buf[xpe_length - 1] = (crc_32 & 0x000000ff) >> 0;
}
}
void CMMB_DTMB_MUX::CreateUdpMsg(BYTE *pSrc, BYTE *pDst, UINT srclen, SERVICE_PARAM_t* pServiceParam)
{
if(!pSrc || !pDst)return;
memset(pDst, 0, MSG_PKG_LEN);
pDst[0] = 0x09; //版本号
pDst[1] = 0xf1; //保留位
pDst[2] = 0xa0; //xpe 包
pDst[3] = (pServiceParam->udp_count & 0xff00) >> 8;//UDP包序号高两位,递增1
pDst[4] = (pServiceParam->udp_count & 0x00ff) ;//UDP包序号低两位
pDst[5] = (pServiceParam->service_ID & 0xff00) >> 8;//存放service_id的高两位
pDst[6] = (U8)(pServiceParam->service_ID & 0x00ff); //存放service_id的低两位
pDst[7] = ((0x0f)<<4);
pDst[7] |=(pServiceParam->msg_count & 0xf00) >> 8; //数据序号高位
pDst[8] = (pServiceParam->msg_count & 0x0ff); //数据序号低位
memcpy(pDst+9, pSrc, srclen);
}
void CMMB_DTMB_MUX::DataRate(int rate)
{
int speed=0;
UINT addBytes=0;
speed = rate;
printf("speed is %d Mbit/s\n", speed);
if(5 == speed)
{
addBytes = 15*1024;
}
else if(6 == speed)
{
addBytes = 18*1024;
}
else if(7 == speed)
{
addBytes = 40*1024;
}
else if(8 == speed)
{
addBytes = 50*1024;
}
else if(9 == speed)
{
addBytes = 60*1024;
}
else if(10 == speed)
{
addBytes = 70*1024;
}
else if(11 == speed)
{
addBytes = 100*1024;
}
else if(12 == speed)
{
addBytes = 105*1024;
}
else if(13 == speed)
{
addBytes = 110*1024;
}
else if(14 == speed)
{
addBytes = 130*1024;
}
else if(15 == speed)
{
addBytes = 150*1024;
}
else if(16 == speed)
{
addBytes = 220*1024;
}
else if(17 == speed)
{
addBytes = 230*1024;
}
else if(18 == speed)
{
addBytes = 285*1024;
}
else if(19 == speed)
{
addBytes = 340*1024;
}
else if(20 == speed)
{
addBytes = 350*1024;
}
else
{
addBytes=0;
}
///("speed is %d", speed);
m_netRate = 1000000/((speed*1024*1024/8+addBytes)/1316);
}
void CMMB_DTMB_MUX::NetInit(thread_params_t* pthrParam)
{
thread_params_t* pThreadParams = pthrParam;
if((pThreadParams->SERVICE_PARAMS[0].alloc_bps > 0) &&
(pThreadParams->SERVICE_PARAMS[0].alloc_bps <= 10)
)
{
DataRate(pThreadParams->SERVICE_PARAMS[0].alloc_bps);
}
else
{
DataRate(1);
}
//gsockUdp=socket(AF_INET,SOCK_DGRAM,0);
//setsockopt(gsockClient, SOL_SOCKET, )
//int nBufLength = 1024*1024*32;
//setsockopt(gsockUdp, SOL_SOCKET, SO_SNDBUF, (char*)&nBufLength, sizeof(nBufLength));
//unsigned long param=1;
//if(SOCKET_ERROR==ioctlsocket(gsockUdp,FIONBIO,¶m))
//{
//printf("Can't set block!\n");
//return;
//}
//gaddrOut.sin_family=AF_INET;
//gaddrOut.sin_addr.s_un.s_addr=inet_addr(pThreadParams->pszMuxIP);//复用器ip—mux_ip从输出配置页面输入
//gaddrOut.sin_port = htons(pThreadParams->mux_port);
//=================================================
BOOL boolFlag = m_sUdp.Open(pThreadParams->pszControlIP, 5000);
if(!boolFlag)
{
printf("Can't bind the ip\n");
return;
}
boolFlag = m_sUdp.Connect(pThreadParams->pszMuxIP, pThreadParams->mux_port, pThreadParams->pszControlIP);
if(!boolFlag)
{
printf("Can't connect destination ip\n");
}
}
/*
UINT WriteNetData(PBYTE pBuffer, int writeSize)
{
UINT length = sendto(gsockUdp,(const char*)pBuffer,writeSize,0,(SOCKADDR*)&gaddrOut,sizeof(SOCKADDR_IN));
return length;
}
*/
WORD CMMB_DTMB_MUX::GetFileStreamNo(void)
{
WORD streamNo = 0;
srand(time(NULL));
streamNo = rand()%65536;
return streamNo;
}
// File control frame
void CMMB_DTMB_MUX::CreateControlFrame(BYTE* pDst, BYTE counter, WORD fileStreamNo, UINT sumFrame, UINT fileLen, const char* fileName)
{
if(!fileName)return;
//CString pidStr, groupIdStr, userIdStr;
WORD pid=0;
WORD groupId=0;
WORD userId=0;
WORD crc=0;
BYTE index=0;
BYTE len=0;
len=(BYTE)strlen(fileName);
//m_editPid.GetWindowTextA(pidStr);
//m_editGroupId.GetWindowTextA(groupIdStr);
//m_editUserId.GetWindowTextA(userIdStr);
pid=(WORD)atoi(DVBPID);
groupId=(WORD)atoi(DVBGROUPID);
userId=(WORD)atoi(DVBUSERID);
//Ts header
pDst[index++]=0x47;
pDst[index++]=0x40 | ((pid>>8)&0x1F);
pDst[index++]=pid & 0xff;
pDst[index++]=0x10 | (counter%16);
//xdb header
pDst[index++]=0xff; //sync
pDst[index++]=0x00; //len index=5
pDst[index++]=0x00; //crc high index=6
pDst[index++]=0x00; //crc low index=7
pDst[index++]=0x10; //type
pDst[index++]=(fileStreamNo>>8)&0xff;
pDst[index++]=(fileStreamNo)&0xff; //file stream no
pDst[index++]=0;
pDst[index++]=0;
pDst[index++]=0;
pDst[index++]=0; //fixed to 0
//group ID
pDst[index++]=(groupId>>8)&0xff;
pDst[index++]=(groupId)&0xff; //group ID
//user ID
pDst[index++]=(userId>>8)&0xff;
pDst[index++]=(userId)&0xff; //user ID
//sum frame number
pDst[index++]=(sumFrame>>24)&0xff;
pDst[index++]=(sumFrame>>16)&0xff;
pDst[index++]=(sumFrame>>8)&0xff;
pDst[index++]=(sumFrame)&0xff;
//file length
pDst[index++]=(fileLen>>24)&0xff;
pDst[index++]=(fileLen>>16)&0xff;
pDst[index++]=(fileLen>>8)&0xff;
pDst[index++]=(fileLen)&0xff;
//fileName length
pDst[index++]=len; //filename length
//TRACE("fileName is: %s, lenght is: %d", fileName, len);
//file type
pDst[index++]=0x56; //fixed
pDst[index++]=0x41; //fixed
//file name
memcpy(pDst+index,fileName, len);
index += len;
//md5 value
memcpy(pDst+index, m_checkSum, 16);
index += 16;
//crc check
crc = Get_16bitCRC(0, pDst+8, index-8);
pDst[6] = (crc>>8)&0xff;
pDst[7] = (crc)&0xff;
//frame len;
pDst[5] = index-9;
}
/*
void SleepExt(unsigned long us)
{
LARGE_INTEGER PT_litmp;
LONGLONG PT_QPart1,PT_QPart2;
double PT_dfMinus,PT_dfFreq,PT_dfTim;
QueryPerformanceFrequency(&PT_litmp);
PT_dfFreq=(double)PT_litmp.QuadPart; //获得系统的主频
QueryPerformanceCounter(&PT_litmp);
PT_QPart1=PT_litmp.QuadPart; //获得起始的值
do{
QueryPerformanceCounter(&PT_litmp);
PT_QPart2=PT_litmp.QuadPart; //获得结束计数
PT_dfMinus=(double)(PT_QPart2-PT_QPart1);
PT_dfTim=PT_dfMinus/PT_dfFreq; //计算出程序运行的时间,单位s
}while(PT_dfTim*1000000<us);
}
//*/
unsigned char CMMB_DTMB_MUX::GetContinueCounter(BYTE* pSrc, unsigned int len, BYTE off, unsigned int flen)
{
UINT ii=0;
INT jj=0;
BYTE *ptr=NULL;
BYTE Counter[10]={0};
memset(Counter, 255, 10);
for(ii=0; ii<len;)
{
if(CMMB_MODE == m_sendType)
{
if(0==ii)
ptr = pSrc + ii + 3 + off;
else
ptr = pSrc + ii + 3 + 5;
}
else if(DTMB_MODE == m_sendType)
{
ptr = pSrc + ii + 3;
}
//==========================
Counter[jj] = ptr[0]&0x0f;
//==========================
if(CMMB_MODE == m_sendType)
{
if(0==ii)
ii += flen + 9;
else
ii += flen;
}
else if(DTMB_MODE == m_sendType)
{
ii=ii + TS_FRAM_LEN;
}
//TRACE("Continue counter value: %d\n", Counter[jj]);
jj++;
}
for(jj=9;jj>0;jj--)
{
if(Counter[jj] < 255)break;
}
return Counter[jj];
}
///*
UINT CMMB_DTMB_MUX::SendFile(const char *fname)
{
//======================================================
fstream xsendFile;
const char* fileName;
UINT retLen=0;
UINT restBytes=0;
UINT proBytes=0;
UINT stepBytes=0;
//UINT packCou=0;
//UINT sendBytes=0;
BOOL hasDataFlag=FALSE;
UINT fileLen=0;
UINT sendCou=0;
UINT wrtNum=0;
UINT sendPacketCou=0;
//=====================================================
UINT frameLen=0;
UINT sendUnitLen=0;
BYTE continueCou=0;
BYTE tsOff=0;
//CString strTmp;
BOOL headerFlag = TRUE;
fileName = fname;
//fileName += extname;
printf("%s()->send file name is %s\n", __func__, fileName);
xsendFile.open(fileName, ios::in | ios::binary);
if(!xsendFile.is_open())
{
printf("%s()->Can't open the send file : %s.\n", __func__, fileName);
return 0;
}
//=====================================================
BYTE *fileBuf=NULL, *tmpBuf=NULL, *proBuf=NULL, *fillBuf=NULL;
if(CMMB_MODE == m_sendType)
{
sendUnitLen=MSG_SEND_UNIT;
frameLen=MSG_PKG_LEN; //ts: TS_FRAME_LEN msg: MSG_PKG_LEN xpe:XPE_PKG_LEN
}
else if(DTMB_MODE == m_sendType)
{
sendUnitLen=TS_SEND_UNIT;
frameLen=TS_SEND_UNIT; //ts: TS_FRAME_LEN msg: MSG_PKG_LEN xpe:XPE_PKG_LEN
}
//=====================================================
printf("%s()->send_type: %d, sendUnitLen: %d, frameLen: %d\n", __func__, m_sendType, sendUnitLen, frameLen);
//=====================================================
fileBuf = new BYTE[TS_SEND_PACKAGES*sendUnitLen];
tmpBuf = new BYTE[sendUnitLen];
proBuf = new BYTE[TS_SEND_BUF*sendUnitLen];
fillBuf = new BYTE[frameLen];
/*
BYTE fileBuf[TS_SEND_PACKAGES*MSG_SEND_UNIT];
BYTE tmpBuf[MSG_SEND_UNIT];
BYTE proBuf[TS_SEND_BUF*MSG_SEND_UNIT];
BYTE fillBuf[MSG_PKG_LEN];
//*/
//==========================================
printf("fileBuf=%p, tmpBuf=%p, proBuf=%p, fillBuf=%p\n", fileBuf, tmpBuf, proBuf, fillBuf);
//==========================================
xsendFile.seekg(0, ios::end);
fileLen = xsendFile.tellg();
xsendFile.seekg(0, ios::beg);
//printf("[%s] length is: %d\n", fileName, fileLen);
//fileLen = sendFile.GetLength();
//UINT coux=0;
//=================
//UINT frame=0;
//======================================================
//do
//{
do
{
memset(fileBuf, 0, TS_SEND_PACKAGES*sendUnitLen); //TS_SEND_PACKAGES*TS_SEND_UNIT
//retLen = sendFile.Read(fileBuf, TS_SEND_PACKAGES*sendUnitLen);
xsendFile.read((char*)fileBuf, TS_SEND_PACKAGES*sendUnitLen);
retLen = xsendFile.gcount();
//printf("Get %d bytes form file [%s]\n", retLen, fileName);
//printf("File End check: %d\n", sendFile.eof());
if(!retLen)break;
//===============================================
if(headerFlag)
{
headerFlag = FALSE;
memset(fillBuf, 0, frameLen); //MSG_PKG_LEN
memcpy(fillBuf, fileBuf, frameLen); //MSG_PKG_LEN
}
//===============================================
stepBytes=0;
memset(proBuf, 0, TS_SEND_BUF*sendUnitLen); //TS_SEND_UNIT
if(hasDataFlag)
{
memcpy(proBuf, tmpBuf, restBytes);
memcpy(proBuf + restBytes, fileBuf, retLen);
proBytes = restBytes + retLen;
}
else
{
memcpy(proBuf, fileBuf, retLen);
proBytes = retLen;
}
hasDataFlag=FALSE;
restBytes=0;
do
{
if(proBytes >= sendUnitLen) //TS_SEND_UNIT
{
//process the per package
//===============================================================
//===============================================================
//send to do
wrtNum = m_sUdp.Write(proBuf + stepBytes, sendUnitLen); // TS_SEND_UNIT
//wrtNum = WriteNetData(proBuf + stepBytes, sendUnitLen);
//frame = GetFrameNo(proBuf + stepBytes, TS_SEND_UNIT);
//strTmp.Format("frame: %d\n", frame);
//TRACE(strTmp);
if(CMMB_MODE == m_sendType)
{
usleep(TIME_SEND);
}
else if(DTMB_MODE == m_sendType)
{
usleep(m_netRate);
}
//===============================================================
//===============================================================
proBytes -= sendUnitLen; //TS_SEND_UNIT
stepBytes += sendUnitLen; //TS_SEND_UNIT
if(!proBytes)stepBytes=0;
//======================
sendPacketCou++;
if(sendPacketCou%100 == 0)
{
printf("Sending packet %d\n", sendPacketCou);
}
//======================
}
else
{
if(proBytes > 0)
{
memset(tmpBuf, 0x00, sendUnitLen); //TS_SEND_UNIT
memcpy(tmpBuf, proBuf + stepBytes, proBytes);
hasDataFlag=TRUE;
restBytes = proBytes;
}
break;
}
}while(proBytes>0);
}while(retLen>0);
//========================================================
if(restBytes > 0)
{
//printf("%s()->restBytes=%d\n", __func__, restBytes);
//process the last package
//send the last package
memset(tmpBuf, 0xff, sendUnitLen); //TS_SEND_UNIT
memcpy(tmpBuf, proBuf+stepBytes, restBytes);
//===================================================================
if(MSG_PKG_LEN == frameLen)
tsOff=14;
else if(XPE_PKG_LEN == frameLen)
tsOff=5;
else
tsOff=0;
//printf("The last package is %d Bytes\n", restBytes);
//====================================================================
///*
if(restBytes < sendUnitLen) //TS_SEND_UNIT
{
continueCou = GetContinueCounter(tmpBuf, restBytes, tsOff, XPE_PKG_LEN);
while(restBytes < sendUnitLen) //TS_SEND_UNIT
{
continueCou++;
if(continueCou > 15)continueCou=0;
fillBuf[3+tsOff] = (fillBuf[3+tsOff]&0xf0)| continueCou;
if(CMMB_MODE == m_sendType)
{
memcpy(tmpBuf + restBytes, fillBuf+9, XPE_PKG_LEN); //TS_FRAME_LEN
restBytes += XPE_PKG_LEN; //TS_FRAM_LEN
}
else if(DTMB_MODE == m_sendType)
{
memcpy(tmpBuf + restBytes, fillBuf, TS_FRAME_LEN); //TS_FRAME_LEN
restBytes += TS_FRAM_LEN;
}
}
}
//*/
//====================================================================
//send to do
m_sUdp.Write(tmpBuf, sendUnitLen); //TS_SEND_UNIT
//WriteNetData(tmpBuf, sendUnitLen);
sendCou += sendUnitLen; //TS_SEND_UNIT
//====================================================================
}
restBytes = 0;
//}while(!m_bStop);
sendPacketCou++;
printf("File [%s] has been send completed! times: %d\n", fname, sendPacketCou);
//sendFile.seekg(0, ios::beg);
//}while(0);
//========================================================
//release the buf
xsendFile.close();
delete []fileBuf;
delete []fillBuf;
delete []tmpBuf;
delete []proBuf;
fileBuf=fillBuf=tmpBuf=proBuf=NULL;
return 0;
}
#if 0
UINT SendCmmbFile(LPCSTR fname)
{
//======================================================
CFile sendFile;
CString fileName;
UINT retLen=0;
UINT restBytes=0;
UINT proBytes=0;
UINT stepBytes=0;
UINT packCou=0;
UINT sendBytes=0;
BOOL hasDataFlag=FALSE;
UINT fileLen=0;
UINT sendCou=0;
UINT wrtNum=0;
//=====================================================
fileName = fname;
//fileName += extname;
printf("file name is %s\n", fileName);
sendFile.Open(fileName,0,0);
//=====================================================
BYTE *fileBuf=NULL, *tmpBuf=NULL, *proBuf=NULL, *fillBuf=NULL;
fileBuf = new BYTE[TS_SEND_PACKAGES*TS_SEND_UNIT];
tmpBuf = new BYTE[TS_SEND_UNIT];
proBuf = new BYTE[TS_SEND_BUF*TS_SEND_UNIT];
fileLen = sendFile.GetLength();
UINT coux=0;
//=================
UINT frame=0;
UINT frameLen=0;
UINT sendUnitLen=0;
BYTE continueCou=0;
BYTE tsOff=0;
CString strTmp;
BOOL headerFlag = TRUE;
frameLen=MSG_PKG_LEN; //ts: TS_FRAME_LEN msg: MSG_PKG_LEN xpe:XPE_PKG_LEN
sendUnitLen=TS_SEND_UNIT;
fillBuf = new BYTE[frameLen];
//======================================================
//do
//{
do
{
memset(fileBuf, 0, TS_SEND_PACKAGES*sendUnitLen); //TS_SEND_PACKAGES*TS_SEND_UNIT
retLen = sendFile.Read(fileBuf, TS_SEND_PACKAGES*sendUnitLen);
if(!retLen)break;
//===============================================
if(headerFlag)
{
headerFlag = FALSE;
memset(fillBuf, 0, frameLen); //MSG_PKG_LEN
memcpy(fillBuf, fileBuf, frameLen); //MSG_PKG_LEN
}
//===============================================
stepBytes=0;
memset(proBuf, 0, TS_SEND_BUF*sendUnitLen); //TS_SEND_UNIT
if(hasDataFlag)
{
memcpy(proBuf, tmpBuf, restBytes);
memcpy(proBuf + restBytes, fileBuf, retLen);
proBytes = restBytes + retLen;
}
else
{
memcpy(proBuf, fileBuf, retLen);
proBytes = retLen;
}
hasDataFlag=FALSE;
restBytes=0;
do
{
if(proBytes >= sendUnitLen) //TS_SEND_UNIT
{
//process the per package
//===============================================================
//===============================================================
//send to do
wrtNum = m_sUdp.Write(proBuf + stepBytes, sendUnitLen); // TS_SEND_UNIT
//wrtNum = WriteNetData(proBuf + stepBytes, sendUnitLen);
//frame = GetFrameNo(proBuf + stepBytes, TS_SEND_UNIT);
//strTmp.Format("frame: %d\n", frame);
//TRACE(strTmp);
//SleepExt(m_netRate);
Sleep(40);
//===============================================================
//===============================================================
proBytes -= sendUnitLen; //TS_SEND_UNIT
stepBytes += sendUnitLen; //TS_SEND_UNIT
if(!proBytes)stepBytes=0;
}
else
{
if(proBytes > 0)
{
memset(tmpBuf, 0x00, sendUnitLen); //TS_SEND_UNIT
memcpy(tmpBuf, proBuf + stepBytes, proBytes);
hasDataFlag=TRUE;
restBytes = proBytes;
}
break;
}
}while(proBytes>0);
}while(retLen>0);
//========================================================
if(restBytes > 0)
{
//process the last package
//send the last package
memset(tmpBuf, 0xff, sendUnitLen); //TS_SEND_UNIT
memcpy(tmpBuf, proBuf+stepBytes, restBytes);
//===================================================================
if(MSG_PKG_LEN == frameLen)
tsOff=14;
else if(XPE_PKG_LEN == frameLen)
tsOff=5;
else
tsOff=0;
//====================================================================
if(restBytes < sendUnitLen) //TS_SEND_UNIT
{
continueCou = GetContinueCounter(tmpBuf, restBytes, tsOff, frameLen);
while(restBytes < sendUnitLen) //TS_SEND_UNIT
{