streamer_teste.cpp
19 KB
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
/***************************************************************************
* Copyright (C) 2005 by Jeff Ferr *
* root@sat *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
// Exemplo adaptado do streamer feito por <Marcos Vasconcelos> (marcovas@lavid.ufpb.br)
#include "jdatagramsocket.h"
#include "jconnectionpipe.h"
#include "jprogramassociationsection.h"
#include "jprogrammapsection.h"
#include "jtransportstreampacket.h"
#include "jfile.h"
#include "jfileinputstream.h"
#include "jdate.h"
#include "jsocketlib.h"
#include <iostream>
#include <vector>
#include <iomanip>
#include <cstdio>
#include <string>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <fcntl.h>
#include <sys/time.h>
using namespace jsocket;
using namespace jio;
const double CLOCK_2_TIME_CONST = (double)1.0/0.09;
uint8_t nullPacket [188]= {
0x47, 0x1F, 0xff, 0x1F, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};
class PatInformation{
protected:
int programCount;
bool cat;
int *pmtPIDs;
int catPID;
void ParsePat(jmpeg::ProgramAssociationSection *patSection)
{
if (patSection->HasFailed() == true) {
std::cout << "PAT section failed" << std::endl;
return;
}
std::vector<jmpeg::ProgramAssociationSection::Program *> program_map;
int k = 0;
patSection->GetPrograms(program_map);
programCount = program_map.size();
pmtPIDs = new int[programCount];
for (std::vector<jmpeg::ProgramAssociationSection::Program *>::iterator i=program_map.begin(); i!=program_map.end(); i++) {
if ((*i)->GetProgramNumber() == 0) {
cat = true;
catPID = (*i)->GetProgramID();
} else {
pmtPIDs[k++] = (*i)->GetProgramID();
}
std::cout << "PMT:: program_number::[" << (*i)->GetProgramNumber() << "], pid::[" << std::hex << (*i)->GetProgramID() << "]" << std::endl;
}
}
public:
PatInformation ():
programCount (0),
cat (false),
pmtPIDs (0),
catPID (-1)
{
}
PatInformation(jmpeg::ProgramAssociationSection *patSection):
programCount(0),
cat(false),
pmtPIDs(0),
catPID(-1)
{
if (patSection->HasFailed() == true) {
return;
}
SetPatSection(patSection);
}
virtual ~PatInformation()
{
}
static jmpeg::ProgramAssociationSection * GetPatSection(uint8_t *packet)
{
if (jmpeg::TransportStreamPacket::GetProgramID(packet) == 0) {
if (jmpeg::TransportStreamPacket::GetPayloadUnitStartIndicator(packet) == 1) {
jmpeg::ProgramAssociationSection *psi = new jmpeg::ProgramAssociationSection();
uint8_t pointer,
payload[188];
uint32_t size;
jmpeg::TransportStreamPacket::GetPayload(packet, payload, &size);
pointer = jmpeg::TransportStreamPacket::GetPointerField(packet);
psi->Push((payload + pointer + 1), size - pointer - 1);
return psi;
}
}
std::cout << "PAT section failed" << std::endl;
return NULL;
}
void SetPatSection(jmpeg::ProgramAssociationSection *p)
{
ParsePat(p);
}
bool HasCAT()
{
return cat;
}
int GetProgramCount()
{
return programCount;
}
int GetCatPid()
{
return catPID;
}
int * GetPmtPIDs()
{
return pmtPIDs;
}
};
class PmtInformation{
protected:
jmpeg::ProgramMapSection *pmtSection;
int audioPID;
int videoPID;
void ParsePmt ()
{
std::vector<jmpeg::ProgramMapSection::Program *> program_map;
pmtSection->GetPrograms(program_map);
for (std::vector<jmpeg::ProgramMapSection::Program *>::iterator i=program_map.begin(); i!=program_map.end(); i++) {
std::cout << "Services:: pid::[" << (*i)->GetElementaryPID() << "], type::[" << (*i)->GetStreamType() << "]" << std::endl;
int t = (*i)->GetStreamType();
if (isAudioStream(t)) {
audioPID = (*i)->GetElementaryPID();
} else if (isVideoStream(t)) {
videoPID = (*i)->GetElementaryPID();
}
}
}
public:
PmtInformation ():
pmtSection(0),
audioPID(-1),
videoPID(-1)
{
}
PmtInformation(jmpeg::ProgramMapSection *p):
pmtSection(p),
audioPID(-1),
videoPID(-1)
{
SetData(pmtSection);
}
virtual ~PmtInformation()
{
}
static bool isAudioStream(int t)
{
switch (t) {
case 0x02:
case 0x03:
case 0x04:
case 0x0F:
case 0x11:
case 0x81:
return true;
default:
return false;
}
}
static bool isVideoStream(int t)
{
switch (t) {
case 0x00:
case 0x01:
case 0x02:
case 0x1B:
return true;
default:
return false;
}
}
static std::string getStreamDescriptor (int t)
{
switch (t) {
case 0x00:
return "ITU-T | ISO/IEC Reserved";
case 0x01:
return "ISO/IEC 11172 Video";
case 0x02:
return "ITU-T Rec. H.262 | ISO/IEC 13818-2 Video or ISO/IEC 11172-2 constrained parameter video stream";
case 0x03:
return "ISO/IEC 11172 Audio";
case 0x04:
return "ISO/IEC 13818-3 Audio";
case 0x05:
return "ITU-T Rec. H.222.0 | ISO/IEC 13818-1 private_sections";
case 0x06:
return "ITU-T Rec. H.222.0 | ISO/IEC 13818-1 PES packets containing private data";
case 0x07:
return "ISO/IEC 13522 MHEG";
case 0x08:
return "ITU-T Rec. H.222.0 | ISO/IEC 13818-1 Annex A DSM-CC";
case 0x09:
return "ITU-T Rec. H.222.1";
case 0x0A:
return "ISO/IEC 13818-6 type A";
case 0x0B:
return "ISO/IEC 13818-6 type B";
case 0x0C:
return "ISO/IEC 13818-6 type C";
case 0x0D:
return "ISO/IEC 13818-6 type D";
case 0x0E:
return "ITU-T Rec. H.222.0 | ISO/IEC 13818-1 auxiliary";
case 0x0F:
return "ISO/IEC 13818-7 Audio with ADTS transport syntax (AAC)";
case 0x10:
return "ISO/IEC 14496-2 Visual";
case 0x11:
return "ISO/IEC 14496-3 Audio with the LATM transport syntax as defined in ISO/IEC 14496-3 / AMD 1";
case 0x12:
return "ISO/IEC 14496-1 SL-packetized stream or FlexMux stream carried in PES packets";
case 0x13:
return "ISO/IEC 14496-1 SL-packetized stream or FlexMux stream carried in ISO/IEC14496_sections";
case 0x14:
return "ISO/IEC 13818-6 Synchronized Download Protocol";
case 0x15:
return "Matadata carried in PES packet";
case 0x16:
return "Metadata carried in metadata_sections";
case 0x17:
return "Metadata carried in data caroulsel as defined in ISO/IEC 13818-6";
case 0x18:
return "Metadata carried in data caroulsel as defined in ISO/IEC 13818-6";
case 0x19:
return "Metadata carried in download synchronized protocol as defined in ISO/IEC 13818-6";
case 0x1A:
return "IPMP Stream as defined in ISO/IEC 13818-11";
case 0x1B:
return "ITU-T Rec. H.264 | ISO/IEC 14496-10 Video";
case 0x7E:
return "Data pipe";
case 0x7F:
return "IPMP Stream";
case 0x81:
return "A52 Audio";
}
if (t >= 0x15 && t <= 0x7F)
return "ITU-T Rec. H.222.0 | ISO/IEC 13818-1 Reserved";
else if (t >= 0x80 && t <= 0xff)
return "User Private";
else
return "Unknown type";
}
static jmpeg::ProgramMapSection * GetPmtSection(uint8_t *packet)
{
if (jmpeg::TransportStreamPacket::GetPayloadUnitStartIndicator(packet) == 1) {
jmpeg::ProgramMapSection *psi = new jmpeg::ProgramMapSection();
uint8_t pointer,
payload[188];
uint32_t size;
jmpeg::TransportStreamPacket::GetPayload(packet, payload, &size);
pointer = jmpeg::TransportStreamPacket::GetPointerField(packet);
psi->Push((payload + pointer + 1), size - pointer - 1);
return psi;
}
std::cout << "PMT section failed" << std::endl;
return NULL;
}
int getAudioPID()
{
return audioPID;
}
int getVideoPID()
{
return videoPID;
}
void SetData(jmpeg::ProgramMapSection *s)
{
pmtSection = s;
ParsePmt();
}
};
class Streamer: public jthread::Thread{
protected:
std::string fileName,
remoteHost;
unsigned short port;
int videoPIDsLength,
*videoPIDs,
audioPIDsLength,
*audioPIDs,
pmtPIDsLength,
*pmtPIDs,
pidPcr;
bool ok,
paused,
loop,
stop,
running;
public:
Streamer(std::string file, std::string host, unsigned short p, bool l = false):
jthread::Thread(),
fileName(file),
remoteHost(host),
port(p),
videoPIDsLength(0),
videoPIDs(0),
audioPIDsLength(0),
audioPIDs(0),
pmtPIDsLength(0),
pmtPIDs(0),
paused(false),
loop(l)
{
ok = true;
stop = false;
running = false;
pidPcr = 0;
}
virtual ~Streamer()
{
if (audioPIDs != 0)
delete audioPIDs;
if (videoPIDs != 0)
delete videoPIDs;
}
void Pause()
{
paused = true;
}
void Unpause()
{
paused = false;
}
void Stop()
{
stop = true;
while(running)
usleep(100);
pidPcr = 0;
}
int getPIDsLength()
{
return 0;
}
int getPMTsLength()
{
return 0;
}
virtual void Run()
{
unsigned pids [8191],
videoPackets = 0;
for (int i = 0; i < 8191; i++) {
pids [i] = 0;
}
const int MAX = 188;
uint8_t *buffer = new uint8_t [MAX],
*cursor = buffer;
jmpeg::ProgramAssociationSection *pat = 0;
jmpeg::ProgramMapSection *pmt = 0;
PatInformation patInfo;
uint64_t pcr = 0,
ipcr = 0,
factor = 0;
int pid = -1,
*pmtPids = 0,
programCount = 0,
catPid = -1,
pmtLength = 0,
videoPID = -1,
audioPID = -1;
jsocket::Connection *socket = new jsocket::DatagramSocket(remoteHost, port);
jsocket::ConnectionPipe *pipe_socket = new jsocket::ConnectionPipe(socket, jsocket::JCP_SENDER, 7*188);
pipe_socket->Start();
PmtInformation *pmtInfo = new PmtInformation();
// FDFile *f = new FDFile(fileName.c_str());
jio::File file(fileName, jio::JFF_READ_ONLY);
unsigned payloadLength;
int i;
uint8_t * payload = new uint8_t[188];
std::cout << "Opening " << fileName << std::endl;
if (file.Exists() == false) {
std::cout << "Error opening " << fileName << std::endl;
return;
}
jio::FileInputStream input(&file);
uint64_t t0,
tf;
uint64_t atual = 0LL,
primeiro = 0LL,
pcrExt = 0LL,
tosleep = 0LL;
int r,
packetNo = 0LL;
running = true;
stop = false;
t0 = jcommon::Date::CurrentTimeMillis();
while (!stop) {
while (paused) {
primeiro = 0;
jthread::Thread::MSleep(100);
}
if ((r = input.Read((char *)cursor, MAX)) < 188) {
if (loop) {
primeiro = 0;
input.Reset();
} else {
break;
}
}
pid = jmpeg::TransportStreamPacket::GetProgramID(buffer);
pids [pid]++;
if ((pid >= 0x0000 && pid <= 0x0001) || (pid >= 0x0010 && pid <= 0x1ffe)) {
if (jmpeg::TransportStreamPacket::HasAdaptationField(buffer)) {
unsigned plength = jmpeg::TransportStreamPacket::GetAdaptationFieldLength(buffer);
// Tem adaptation field aqui. Tamanho = plength
if (plength > 0) {
jmpeg::AdaptationField *field = jmpeg::TransportStreamPacket::GetAdaptationField(buffer);
if (field->GetPCRFlag()) {
if (primeiro == 0) {
// Primeiro PCR a passar por aqui
if (pidPcr == 0) {
pidPcr = pid;
}
pcr = field->GetPCRBase();
pcrExt = field->GetPCRExtension();
pcr = (pcr * 300 + pcrExt) / 300;
primeiro = jcommon::Date::CurrentTimeMillis();
} else if (pid == pidPcr) {
if (field->GetLength() > 0) {
ipcr = field->GetPCRBase();
pcrExt = field->GetPCRExtension();
ipcr = (ipcr * 300LL + pcrExt) / 300LL;
if (ipcr >= pcr) {
atual = jcommon::Date::CurrentTimeMillis();
factor = (uint64_t)((double)(ipcr-pcr)*CLOCK_2_TIME_CONST);
if (atual - primeiro < factor) {
tosleep = factor - (atual - primeiro);
jthread::Thread::MSleep(tosleep);
} else {
//cout << "Perdi o tempo, nao vou dormir agora" << endl;
}
} else {
std::cout << "PCR restarted" << std::endl;
primeiro = 0;
}
}
}
} else {
// Não tem PCR aqui
}
if (field->GetOPCRFlag()) {
// Tem OPCR aqui
} else {
// Não tem OPCR aqui
}
}
}
}
if (pid == 8191) {
// NULL packet
} else if (pid == 0 && pmtPids == 0) {
std::cout << "Getting PAT information" << std::endl;
pat = PatInformation::GetPatSection(buffer);
if (pat == 0)
continue;
patInfo.SetPatSection(pat);
programCount = patInfo.GetProgramCount();
if (patInfo.HasCAT()) {
catPid = patInfo.GetCatPid();
std::cout << "CAT pid::[" << catPid << "]" << std::endl;
}
if (pmtPids == 0) {
pmtPids = patInfo.GetPmtPIDs();
pmtLength = patInfo.GetProgramCount();
}
} else if (pid == pmtInfo->getVideoPID()) {
jmpeg::TransportStreamPacket::GetPayload(buffer, payload, &payloadLength);
videoPackets++;
} else if (pmtPids != 0) {
for (i = 0; i < pmtLength; i++) {
if (pid == pmtPids [i] && pmt == 0) {
// TODO:: tem que mexer aqui
if (jmpeg::TransportStreamPacket::GetPayloadUnitStartIndicator(buffer) == 1) {
pmt = PmtInformation::GetPmtSection(buffer);
pmtInfo->SetData(pmt);
videoPID = pmtInfo->getVideoPID();
audioPID = pmtInfo->getAudioPID();
}
}
}
}
++packetNo;
if (pipe_socket->Send((const char *)buffer, 188) < 0) {
std::cout << "Error writing output" << std::endl;
break;
}
if ((packetNo % 6) == 0) {
//pipe->Write((char*)nullPacket, 188);
}
}
tf = jcommon::Date::CurrentTimeMillis();
uint64_t diff = (tf-t0)/1000LL,
hour = diff/3600LL,
min = (diff%3600LL)/60LL,
sec = (diff%60LL);
std::cout << std::dec << "Elapsed time: " << std::setfill('0') << std::setw(2) << hour << ':' << std::setw(2) << min << ':' << std::setw(2) << sec << std::endl;
running = false;
if(buffer)
delete buffer;
}
};
void frame_rate(std::string iFile, std::string address, int port, bool loop)
{
Streamer *streamer;
streamer = new Streamer(iFile, address, port, loop);
streamer->Run();
}
void fixed_rate(std::string filename, std::string host, int port, uint32_t taxa)
{
uint64_t tempoini,
tempocor,
tempoesp,
count = 0LL,
rate = (long long)(taxa/8);
int r;
if (rate <= 0LL) {
std::cout << "Incorrent rate" << std::endl;
exit(0);
}
tempoini = jcommon::Date::CurrentTimeMicros();
try {
DatagramSocket s(host, port, false, 0, 65535, 1500);
OutputStream *o = s.GetOutputStream();
FileInputStream file(filename);
char receive[65535];
std::cout << "Streamming " << filename << std::endl;
do {
r = file.Read(receive, 1500);
if (r <= 0) {
break;
}
count += (int64_t)r;
o->Write(receive, r);
tempoesp = (uint64_t)(tempoini+(uint64_t)((1000000LL*count)/rate));
tempocor = jcommon::Date::CurrentTimeMicros();
if (tempoesp > tempocor) {
jthread::Thread::USleep((uint64_t)(tempoesp-tempocor));
}
std::cout << (count*8000000LL)/(tempocor-tempoini) << " bits/sec\r" << std::flush;
} while (r != 0);
s.Close();
} catch (...) {
std::cout << "Error [unknown]" << std::endl;
}
}
int main(int argc, char **argv)
{
InitWindowsSocket();
if (argc < 2) {
goto error;
}
if (strncmp(argv[1], "fixedrate", 10) == 0) {
if (argc == 6) {
std::cout << "\nInitializing fixed rate streamer" << std::endl;
fixed_rate(argv[2], argv[3], atoi(argv[4]), atoi(argv[5]));
} else {
goto error;
}
} else if (strncmp(argv[1], "framerate", 10) == 0) {
if (argc == 5 || argc == 6) {
std::cout << "\nInitializing frame rate streamer" << std::endl;
bool b = false;
if (argc == 6) {
b = true;
}
frame_rate(argv[2], argv[3], atoi(argv[4]), b);
} else {
goto error;
}
} else {
goto error;
}
ReleaseWindowsSocket();
return EXIT_SUCCESS;
error:
std::cout << "use:: " << argv[0] << " fixedrate <file> <host> <port> <rate>" << std::endl;
std::cout << "use:: " << argv[0] << " framerate <tsfile> <host> <port> [loop]" << std::endl;
return EXIT_FAILURE;
}