winlin

add complex handshake utest

@@ -67,7 +67,7 @@ cd simple-rtmp-server/trunk @@ -67,7 +67,7 @@ cd simple-rtmp-server/trunk
67 [Usage: How to deploy low lantency application?](https://github.com/winlinvip/simple-rtmp-server/wiki/SampleRealtime)<br/> 67 [Usage: How to deploy low lantency application?](https://github.com/winlinvip/simple-rtmp-server/wiki/SampleRealtime)<br/>
68 [Usage: How to deploy srs on ARM?](https://github.com/winlinvip/simple-rtmp-server/wiki/SampleARM)<br/> 68 [Usage: How to deploy srs on ARM?](https://github.com/winlinvip/simple-rtmp-server/wiki/SampleARM)<br/>
69 [Usage: How to show the demo of SRS?](https://github.com/winlinvip/simple-rtmp-server/wiki/SampleDemo)<br/> 69 [Usage: How to show the demo of SRS?](https://github.com/winlinvip/simple-rtmp-server/wiki/SampleDemo)<br/>
70 -[Usage: Who is using SRS?](https://github.com/winlinvip/simple-rtmp-server/wiki/Sample)<br/> 70 +[Usage: Solution using SRS?](https://github.com/winlinvip/simple-rtmp-server/wiki/Sample)<br/>
71 71
72 ### System Requirements 72 ### System Requirements
73 Supported operating systems and hardware: 73 Supported operating systems and hardware:
@@ -80,14 +80,14 @@ Annex A CRC Decoder Model @@ -80,14 +80,14 @@ Annex A CRC Decoder Model
80 80
81 enum TSPidTable 81 enum TSPidTable
82 { 82 {
83 - // Program Association Table(see Table 2-25).  
84 - TSPidTablePAT = 0x00,  
85 - // Conditional Access Table (see Table 2-27).  
86 - TSPidTableCAT = 0x01,  
87 - // Transport Stream Description Table  
88 - TSPidTableTSDT = 0x02,  
89 - // null packets (see Table 2-3)  
90 - TSPidTableNULL = 0x01FFF, 83 + // Program Association Table(see Table 2-25).
  84 + TSPidTablePAT = 0x00,
  85 + // Conditional Access Table (see Table 2-27).
  86 + TSPidTableCAT = 0x01,
  87 + // Transport Stream Description Table
  88 + TSPidTableTSDT = 0x02,
  89 + // null packets (see Table 2-3)
  90 + TSPidTableNULL = 0x01FFF,
91 }; 91 };
92 92
93 /*adaptation_field_control*/ 93 /*adaptation_field_control*/
@@ -96,34 +96,34 @@ enum TSPidTable @@ -96,34 +96,34 @@ enum TSPidTable
96 */ 96 */
97 enum TSAdaptionType 97 enum TSAdaptionType
98 { 98 {
99 - // Reserved for future use by ISO/IEC  
100 - TSAdaptionTypeReserved = 0x00,  
101 - // No adaptation_field, payload only  
102 - TSAdaptionTypePayloadOnly = 0x01,  
103 - // Adaptation_field only, no payload  
104 - TSAdaptionTypeAdaptionOnly = 0x02,  
105 - // Adaptation_field followed by payload  
106 - TSAdaptionTypeBoth = 0x03, 99 + // Reserved for future use by ISO/IEC
  100 + TSAdaptionTypeReserved = 0x00,
  101 + // No adaptation_field, payload only
  102 + TSAdaptionTypePayloadOnly = 0x01,
  103 + // Adaptation_field only, no payload
  104 + TSAdaptionTypeAdaptionOnly = 0x02,
  105 + // Adaptation_field followed by payload
  106 + TSAdaptionTypeBoth = 0x03,
107 }; 107 };
108 #endif 108 #endif
109 109
110 // Table 2-29 – Stream type assignments. page 66. 110 // Table 2-29 – Stream type assignments. page 66.
111 enum TSStreamType 111 enum TSStreamType
112 { 112 {
113 - // ITU-T | ISO/IEC Reserved  
114 - TSStreamTypeReserved = 0x00,  
115 - /*defined by ffmpeg*/  
116 - TSStreamTypeVideoMpeg1 = 0x01,  
117 - TSStreamTypeVideoMpeg2 = 0x02,  
118 - TSStreamTypeAudioMpeg1 = 0x03,  
119 - TSStreamTypeAudioMpeg2 = 0x04,  
120 - TSStreamTypePrivateSection = 0x05,  
121 - TSStreamTypePrivateData = 0x06,  
122 - TSStreamTypeAudioAAC = 0x0f,  
123 - TSStreamTypeVideoMpeg4 = 0x10,  
124 - TSStreamTypeVideoH264 = 0x1b,  
125 - TSStreamTypeAudioAC3 = 0x81,  
126 - TSStreamTypeAudioDTS = 0x8a, 113 + // ITU-T | ISO/IEC Reserved
  114 + TSStreamTypeReserved = 0x00,
  115 + /*defined by ffmpeg*/
  116 + TSStreamTypeVideoMpeg1 = 0x01,
  117 + TSStreamTypeVideoMpeg2 = 0x02,
  118 + TSStreamTypeAudioMpeg1 = 0x03,
  119 + TSStreamTypeAudioMpeg2 = 0x04,
  120 + TSStreamTypePrivateSection = 0x05,
  121 + TSStreamTypePrivateData = 0x06,
  122 + TSStreamTypeAudioAAC = 0x0f,
  123 + TSStreamTypeVideoMpeg4 = 0x10,
  124 + TSStreamTypeVideoH264 = 0x1b,
  125 + TSStreamTypeAudioAC3 = 0x81,
  126 + TSStreamTypeAudioDTS = 0x8a,
127 }; 127 };
128 128
129 /** 129 /**
@@ -155,7 +155,7 @@ class TSMessage; @@ -155,7 +155,7 @@ class TSMessage;
155 class TSPacket 155 class TSPacket
156 { 156 {
157 public: 157 public:
158 - TSHeader* header; 158 + TSHeader* header;
159 TSAdaptionField* adaption_field; 159 TSAdaptionField* adaption_field;
160 TSPayload* payload; 160 TSPayload* payload;
161 161
@@ -272,7 +272,7 @@ public: @@ -272,7 +272,7 @@ public:
272 int size; 272 int size;
273 int pointer_field_size; 273 int pointer_field_size;
274 274
275 - TSPidType type; 275 + TSPidType type;
276 276
277 /** 277 /**
278 * 2.4.4.2 Semantics definition of fields in pointer syntax 278 * 2.4.4.2 Semantics definition of fields in pointer syntax
@@ -417,38 +417,38 @@ public: @@ -417,38 +417,38 @@ public:
417 */ 417 */
418 enum TSPESStreamId 418 enum TSPESStreamId
419 { 419 {
420 - PES_program_stream_map = 0xbc, // 0b10111100  
421 - PES_private_stream_1 = 0xbd, // 0b10111101  
422 - PES_padding_stream = 0xbe, // 0b10111110  
423 - PES_private_stream_2 = 0xbf, // 0b10111111  
424 -  
425 - // 110x xxxx  
426 - // ISO/IEC 13818-3 or ISO/IEC 11172-3 or ISO/IEC 13818-7 or ISO/IEC  
427 - // 14496-3 audio stream number x xxxx  
428 - // (stream_id>>5)&0x07 == PES_audio_prefix  
429 - PES_audio_prefix = 0x06, // 0b110  
430 -  
431 - // 1110 xxxx  
432 - // ITU-T Rec. H.262 | ISO/IEC 13818-2 or ISO/IEC 11172-2 or ISO/IEC  
433 - // 14496-2 video stream number xxxx  
434 - // (stream_id>>4)&0x0f == PES_audio_prefix  
435 - PES_video_prefix = 0x0e, // 0b1110  
436 -  
437 - PES_ECM_stream = 0xf0, // 0b11110000  
438 - PES_EMM_stream = 0xf1, // 0b11110001  
439 - PES_DSMCC_stream = 0xf2, // 0b11110010  
440 - PES_13522_stream = 0xf3, // 0b11110011  
441 - PES_H_222_1_type_A = 0xf4, // 0b11110100  
442 - PES_H_222_1_type_B = 0xf5, // 0b11110101  
443 - PES_H_222_1_type_C = 0xf6, // 0b11110110  
444 - PES_H_222_1_type_D = 0xf7, // 0b11110111  
445 - PES_H_222_1_type_E = 0xf8, // 0b11111000  
446 - PES_ancillary_stream = 0xf9, // 0b11111001  
447 - PES_SL_packetized_stream = 0xfa, // 0b11111010  
448 - PES_FlexMux_stream = 0xfb, // 0b11111011  
449 - // reserved data stream  
450 - // 1111 1100 … 1111 1110  
451 - PES_program_stream_directory= 0xff, // 0b11111111 420 + PES_program_stream_map = 0xbc, // 0b10111100
  421 + PES_private_stream_1 = 0xbd, // 0b10111101
  422 + PES_padding_stream = 0xbe, // 0b10111110
  423 + PES_private_stream_2 = 0xbf, // 0b10111111
  424 +
  425 + // 110x xxxx
  426 + // ISO/IEC 13818-3 or ISO/IEC 11172-3 or ISO/IEC 13818-7 or ISO/IEC
  427 + // 14496-3 audio stream number x xxxx
  428 + // (stream_id>>5)&0x07 == PES_audio_prefix
  429 + PES_audio_prefix = 0x06, // 0b110
  430 +
  431 + // 1110 xxxx
  432 + // ITU-T Rec. H.262 | ISO/IEC 13818-2 or ISO/IEC 11172-2 or ISO/IEC
  433 + // 14496-2 video stream number xxxx
  434 + // (stream_id>>4)&0x0f == PES_audio_prefix
  435 + PES_video_prefix = 0x0e, // 0b1110
  436 +
  437 + PES_ECM_stream = 0xf0, // 0b11110000
  438 + PES_EMM_stream = 0xf1, // 0b11110001
  439 + PES_DSMCC_stream = 0xf2, // 0b11110010
  440 + PES_13522_stream = 0xf3, // 0b11110011
  441 + PES_H_222_1_type_A = 0xf4, // 0b11110100
  442 + PES_H_222_1_type_B = 0xf5, // 0b11110101
  443 + PES_H_222_1_type_C = 0xf6, // 0b11110110
  444 + PES_H_222_1_type_D = 0xf7, // 0b11110111
  445 + PES_H_222_1_type_E = 0xf8, // 0b11111000
  446 + PES_ancillary_stream = 0xf9, // 0b11111001
  447 + PES_SL_packetized_stream = 0xfa, // 0b11111010
  448 + PES_FlexMux_stream = 0xfb, // 0b11111011
  449 + // reserved data stream
  450 + // 1111 1100 … 1111 1110
  451 + PES_program_stream_directory= 0xff, // 0b11111111
452 }; 452 };
453 453
454 454
@@ -465,64 +465,64 @@ public: @@ -465,64 +465,64 @@ public:
465 // 2B 465 // 2B
466 u_int16_t PES_packet_length; //16bits 466 u_int16_t PES_packet_length; //16bits
467 467
468 - // 1B  
469 - // 2bits const '10'  
470 - int8_t PES_scrambling_control; //2bits  
471 - int8_t PES_priority; //1bit  
472 - int8_t data_alignment_indicator; //1bit  
473 - int8_t copyright; //1bit  
474 - int8_t original_or_copy; //1bit  
475 -  
476 - // 1B  
477 - int8_t PTS_DTS_flags; //2bits  
478 - int8_t ESCR_flag; //1bit  
479 - int8_t ES_rate_flag; //1bit  
480 - int8_t DSM_trick_mode_flag; //1bit  
481 - int8_t additional_copy_info_flag; //1bit  
482 - int8_t PES_CRC_flag; //1bit  
483 - int8_t PES_extension_flag; //1bit  
484 -  
485 - // 1B  
486 - u_int8_t PES_header_data_length; //8bits  
487 -  
488 - int64_t pts; // 33bits  
489 - int64_t dts; // 33bits  
490 -  
491 - int16_t ESCR_extension; //9bits  
492 - int64_t ESCR_base; //33bits  
493 - int32_t ES_rate; //22bits  
494 -  
495 - int8_t trick_mode_control; //3bits  
496 - int8_t trick_mode_value; //5bits  
497 -  
498 - int8_t additional_copy_info; //7bits  
499 - int16_t previous_PES_packet_CRC; //16bits  
500 -  
501 - int8_t PES_private_data_flag; //1bit  
502 - int8_t pack_header_field_flag; //1bit  
503 - int8_t program_packet_sequence_counter_flag; //1bit  
504 - int8_t P_STD_buffer_flag; //1bit  
505 - // reserved 3bits  
506 - int8_t PES_extension_flag_2; //1bit  
507 -  
508 - // 16B  
509 - char* PES_private_data; //128bits  
510 -  
511 - int8_t pack_field_length; //8bits  
512 - char* pack_field; //[pack_field_length] bytes  
513 -  
514 - int8_t program_packet_sequence_counter; //7bits  
515 - int8_t MPEG1_MPEG2_identifier; //1bit  
516 - int8_t original_stuff_length; //6bits  
517 -  
518 - int8_t P_STD_buffer_scale; //1bit  
519 - int16_t P_STD_buffer_size; //13bits  
520 -  
521 - int8_t PES_extension_field_length; //7bits  
522 - char* PES_extension_field; //[PES_extension_field_length] bytes  
523 -  
524 - int stuffing_size;  
525 - char* stuffing_byte; 468 + // 1B
  469 + // 2bits const '10'
  470 + int8_t PES_scrambling_control; //2bits
  471 + int8_t PES_priority; //1bit
  472 + int8_t data_alignment_indicator; //1bit
  473 + int8_t copyright; //1bit
  474 + int8_t original_or_copy; //1bit
  475 +
  476 + // 1B
  477 + int8_t PTS_DTS_flags; //2bits
  478 + int8_t ESCR_flag; //1bit
  479 + int8_t ES_rate_flag; //1bit
  480 + int8_t DSM_trick_mode_flag; //1bit
  481 + int8_t additional_copy_info_flag; //1bit
  482 + int8_t PES_CRC_flag; //1bit
  483 + int8_t PES_extension_flag; //1bit
  484 +
  485 + // 1B
  486 + u_int8_t PES_header_data_length; //8bits
  487 +
  488 + int64_t pts; // 33bits
  489 + int64_t dts; // 33bits
  490 +
  491 + int16_t ESCR_extension; //9bits
  492 + int64_t ESCR_base; //33bits
  493 + int32_t ES_rate; //22bits
  494 +
  495 + int8_t trick_mode_control; //3bits
  496 + int8_t trick_mode_value; //5bits
  497 +
  498 + int8_t additional_copy_info; //7bits
  499 + int16_t previous_PES_packet_CRC; //16bits
  500 +
  501 + int8_t PES_private_data_flag; //1bit
  502 + int8_t pack_header_field_flag; //1bit
  503 + int8_t program_packet_sequence_counter_flag; //1bit
  504 + int8_t P_STD_buffer_flag; //1bit
  505 + // reserved 3bits
  506 + int8_t PES_extension_flag_2; //1bit
  507 +
  508 + // 16B
  509 + char* PES_private_data; //128bits
  510 +
  511 + int8_t pack_field_length; //8bits
  512 + char* pack_field; //[pack_field_length] bytes
  513 +
  514 + int8_t program_packet_sequence_counter; //7bits
  515 + int8_t MPEG1_MPEG2_identifier; //1bit
  516 + int8_t original_stuff_length; //6bits
  517 +
  518 + int8_t P_STD_buffer_scale; //1bit
  519 + int16_t P_STD_buffer_size; //13bits
  520 +
  521 + int8_t PES_extension_field_length; //7bits
  522 + char* PES_extension_field; //[PES_extension_field_length] bytes
  523 +
  524 + int stuffing_size;
  525 + char* stuffing_byte;
526 526
527 TSPayloadPES(); 527 TSPayloadPES();
528 virtual ~TSPayloadPES(); 528 virtual ~TSPayloadPES();
@@ -547,13 +547,13 @@ public: @@ -547,13 +547,13 @@ public:
547 */ 547 */
548 struct TSPid 548 struct TSPid
549 { 549 {
550 - TSPidType type;  
551 - // page 66.  
552 - TSStreamType stream_type;  
553 - // page 36  
554 - TSPidTable pid;  
555 - // page 36  
556 - u_int8_t continuity_counter; 550 + TSPidType type;
  551 + // page 66.
  552 + TSStreamType stream_type;
  553 + // page 36
  554 + TSPidTable pid;
  555 + // page 36
  556 + u_int8_t continuity_counter;
557 }; 557 };
558 558
559 /** 559 /**
@@ -562,29 +562,29 @@ struct TSPid @@ -562,29 +562,29 @@ struct TSPid
562 class TSMessage 562 class TSMessage
563 { 563 {
564 public: 564 public:
565 - // 2.4.3.2 Transport Stream packet layer. page 36  
566 - // the pid of PES packet.  
567 - TSPidTable pid;  
568 -  
569 - // the type of pid.  
570 - TSPidType type;  
571 - // the type of stream, codec type.  
572 - TSStreamType stream_type;  
573 - // page 36  
574 - u_int8_t continuity_counter;  
575 -  
576 - // 2.4.3.7 Semantic definition of fields in PES packet. page 49  
577 - // PES packet header size plus data size. 565 + // 2.4.3.2 Transport Stream packet layer. page 36
  566 + // the pid of PES packet.
  567 + TSPidTable pid;
  568 +
  569 + // the type of pid.
  570 + TSPidType type;
  571 + // the type of stream, codec type.
  572 + TSStreamType stream_type;
  573 + // page 36
  574 + u_int8_t continuity_counter;
  575 +
  576 + // 2.4.3.7 Semantic definition of fields in PES packet. page 49
  577 + // PES packet header size plus data size.
578 u_int16_t PES_packet_length; //16bits 578 u_int16_t PES_packet_length; //16bits
579 579
580 - // the stream id.  
581 - u_int8_t stream_id;  
582 -  
583 - // 2.4.3.7 Semantic definition of fields in PES packet. page 49.  
584 - int32_t packet_start_code_prefix;  
585 -  
586 - int64_t pts; // 33bits  
587 - int64_t dts; // 33bits 580 + // the stream id.
  581 + u_int8_t stream_id;
  582 +
  583 + // 2.4.3.7 Semantic definition of fields in PES packet. page 49.
  584 + int32_t packet_start_code_prefix;
  585 +
  586 + int64_t pts; // 33bits
  587 + int64_t dts; // 33bits
588 int64_t pcr; 588 int64_t pcr;
589 589
590 // header size. 590 // header size.
@@ -594,20 +594,20 @@ public: @@ -594,20 +594,20 @@ public:
594 int parsed_packet_size; 594 int parsed_packet_size;
595 595
596 // total packet size. 596 // total packet size.
597 - int packet_data_size;  
598 - char* packet_data;  
599 -  
600 - // for avc.  
601 - u_int8_t nal_ref_idc;  
602 - u_int8_t nal_unit_type;  
603 -  
604 - TSMessage();  
605 - virtual ~TSMessage();  
606 -  
607 - void append(u_int8_t*& p, int size);  
608 - void detach(TSContext* ctx, TSMessage*& pmsg);  
609 -  
610 - bool is_video(); 597 + int packet_data_size;
  598 + char* packet_data;
  599 +
  600 + // for avc.
  601 + u_int8_t nal_ref_idc;
  602 + u_int8_t nal_unit_type;
  603 +
  604 + TSMessage();
  605 + virtual ~TSMessage();
  606 +
  607 + void append(u_int8_t*& p, int size);
  608 + void detach(TSContext* ctx, TSMessage*& pmsg);
  609 +
  610 + bool is_video();
611 }; 611 };
612 612
613 // ts context 613 // ts context
@@ -618,9 +618,9 @@ public: @@ -618,9 +618,9 @@ public:
618 * consumed pids. 618 * consumed pids.
619 */ 619 */
620 int pid_size; 620 int pid_size;
621 - TSPid* pids;  
622 - int64_t ts_packet_count;  
623 - std::map<TSPidTable, TSMessage*> msgs; 621 + TSPid* pids;
  622 + int64_t ts_packet_count;
  623 + std::map<TSPidTable, TSMessage*> msgs;
624 624
625 TSContext(); 625 TSContext();
626 virtual ~TSContext(); 626 virtual ~TSContext();
@@ -675,17 +675,17 @@ TSPid* TSContext::get(TSPidTable pid) @@ -675,17 +675,17 @@ TSPid* TSContext::get(TSPidTable pid)
675 675
676 void TSContext::push(TSPidTable pid, TSStreamType stream_type, TSPidType type, u_int8_t continuity_counter) 676 void TSContext::push(TSPidTable pid, TSStreamType stream_type, TSPidType type, u_int8_t continuity_counter)
677 { 677 {
678 - TSPid* p = get(pid);  
679 - 678 + TSPid* p = get(pid);
  679 +
680 if (!p) { 680 if (!p) {
681 p = new TSPid[pid_size + 1]; 681 p = new TSPid[pid_size + 1];
682 - memcpy(p, pids, sizeof(TSPid) * pid_size);  
683 -  
684 - p[pid_size] = (TSPid){type, stream_type, pid, continuity_counter};  
685 - pid_size++;  
686 -  
687 - srs_freepa(pids);  
688 - pids = p; 682 + memcpy(p, pids, sizeof(TSPid) * pid_size);
  683 +
  684 + p[pid_size] = (TSPid){type, stream_type, pid, continuity_counter};
  685 + pid_size++;
  686 +
  687 + srs_freepa(pids);
  688 + pids = p;
689 } 689 }
690 690
691 p->continuity_counter = continuity_counter; 691 p->continuity_counter = continuity_counter;
@@ -693,73 +693,73 @@ void TSContext::push(TSPidTable pid, TSStreamType stream_type, TSPidType type, u @@ -693,73 +693,73 @@ void TSContext::push(TSPidTable pid, TSStreamType stream_type, TSPidType type, u
693 693
694 TSMessage* TSContext::get_msg(TSPidTable pid) 694 TSMessage* TSContext::get_msg(TSPidTable pid)
695 { 695 {
696 - if (msgs[pid] == NULL) {  
697 - TSMessage* msg = new TSMessage();  
698 - msg->pid = pid;  
699 - msgs[pid] = msg;  
700 - }  
701 -  
702 - return msgs[pid]; 696 + if (msgs[pid] == NULL) {
  697 + TSMessage* msg = new TSMessage();
  698 + msg->pid = pid;
  699 + msgs[pid] = msg;
  700 + }
  701 +
  702 + return msgs[pid];
703 } 703 }
704 704
705 void TSContext::detach(TSMessage* msg) 705 void TSContext::detach(TSMessage* msg)
706 { 706 {
707 - msgs[msg->pid] = NULL; 707 + msgs[msg->pid] = NULL;
708 } 708 }
709 709
710 TSMessage::TSMessage() 710 TSMessage::TSMessage()
711 { 711 {
712 - pid = TSPidTablePAT;  
713 - type = TSPidTypeReserved;  
714 - stream_type = TSStreamTypeReserved;  
715 - stream_id = 0;  
716 - packet_start_code_prefix = 0;  
717 - pts = dts = pcr = 0;  
718 - PES_packet_length = 0;  
719 - packet_header_size = 0;  
720 - parsed_packet_size = 0;  
721 - packet_data_size = 0;  
722 - packet_data = NULL;  
723 -  
724 - nal_ref_idc = 0;  
725 - nal_unit_type = 0; 712 + pid = TSPidTablePAT;
  713 + type = TSPidTypeReserved;
  714 + stream_type = TSStreamTypeReserved;
  715 + stream_id = 0;
  716 + packet_start_code_prefix = 0;
  717 + pts = dts = pcr = 0;
  718 + PES_packet_length = 0;
  719 + packet_header_size = 0;
  720 + parsed_packet_size = 0;
  721 + packet_data_size = 0;
  722 + packet_data = NULL;
  723 +
  724 + nal_ref_idc = 0;
  725 + nal_unit_type = 0;
726 } 726 }
727 727
728 TSMessage::~TSMessage() 728 TSMessage::~TSMessage()
729 { 729 {
730 - srs_freepa(packet_data); 730 + srs_freepa(packet_data);
731 } 731 }
732 732
733 void TSMessage::append(u_int8_t*& p, int size) 733 void TSMessage::append(u_int8_t*& p, int size)
734 { 734 {
735 - if (size <= 0) {  
736 - return;  
737 - }  
738 -  
739 - // for PES_packet_length is 0, the size is varient.  
740 - if (packet_data_size - parsed_packet_size < size) {  
741 - int realloc_size = size - (packet_data_size - parsed_packet_size);  
742 - packet_data = (char*)realloc(packet_data, packet_data_size + realloc_size);  
743 -  
744 - packet_data_size += realloc_size;  
745 - }  
746 -  
747 - memcpy(packet_data + parsed_packet_size, p, size);  
748 - p += size;  
749 - parsed_packet_size += size; 735 + if (size <= 0) {
  736 + return;
  737 + }
  738 +
  739 + // for PES_packet_length is 0, the size is varient.
  740 + if (packet_data_size - parsed_packet_size < size) {
  741 + int realloc_size = size - (packet_data_size - parsed_packet_size);
  742 + packet_data = (char*)realloc(packet_data, packet_data_size + realloc_size);
  743 +
  744 + packet_data_size += realloc_size;
  745 + }
  746 +
  747 + memcpy(packet_data + parsed_packet_size, p, size);
  748 + p += size;
  749 + parsed_packet_size += size;
750 } 750 }
751 751
752 void TSMessage::detach(TSContext* ctx, TSMessage*& pmsg) 752 void TSMessage::detach(TSContext* ctx, TSMessage*& pmsg)
753 { 753 {
754 - if (parsed_packet_size >= packet_data_size) {  
755 - ctx->detach(this);  
756 - pmsg = this;  
757 - } 754 + if (parsed_packet_size >= packet_data_size) {
  755 + ctx->detach(this);
  756 + pmsg = this;
  757 + }
758 } 758 }
759 759
760 bool TSMessage::is_video() 760 bool TSMessage::is_video()
761 { 761 {
762 - return type == TSPidTypeVideo; 762 + return type == TSPidTypeVideo;
763 } 763 }
764 764
765 TSAdaptionField::TSAdaptionField() 765 TSAdaptionField::TSAdaptionField()
@@ -1064,12 +1064,12 @@ TSPMTESInfo::TSPMTESInfo() @@ -1064,12 +1064,12 @@ TSPMTESInfo::TSPMTESInfo()
1064 stream_type = 0; 1064 stream_type = 0;
1065 elementary_PID = 0; 1065 elementary_PID = 0;
1066 ES_info_length = 0; 1066 ES_info_length = 0;
1067 - ES_info = NULL; 1067 + ES_info = NULL;
1068 } 1068 }
1069 1069
1070 TSPMTESInfo::~TSPMTESInfo() 1070 TSPMTESInfo::~TSPMTESInfo()
1071 { 1071 {
1072 - srs_freepa(ES_info); 1072 + srs_freepa(ES_info);
1073 } 1073 }
1074 1074
1075 TSPayloadPMT::TSPayloadPMT() 1075 TSPayloadPMT::TSPayloadPMT()
@@ -1091,18 +1091,18 @@ TSPayloadPMT::TSPayloadPMT() @@ -1091,18 +1091,18 @@ TSPayloadPMT::TSPayloadPMT()
1091 1091
1092 TSPayloadPMT::~TSPayloadPMT() 1092 TSPayloadPMT::~TSPayloadPMT()
1093 { 1093 {
1094 - srs_freepa(program_info_desc);  
1095 -  
1096 - for (std::vector<TSPMTESInfo*>::iterator it = ES_info.begin(); it != ES_info.end(); ++it) {  
1097 - TSPMTESInfo* info = *it;  
1098 - srs_freep(info);  
1099 - }  
1100 - ES_info.clear(); 1094 + srs_freepa(program_info_desc);
  1095 +
  1096 + for (std::vector<TSPMTESInfo*>::iterator it = ES_info.begin(); it != ES_info.end(); ++it) {
  1097 + TSPMTESInfo* info = *it;
  1098 + srs_freep(info);
  1099 + }
  1100 + ES_info.clear();
1101 } 1101 }
1102 1102
1103 TSPMTESInfo* TSPayloadPMT::at(int index) 1103 TSPMTESInfo* TSPayloadPMT::at(int index)
1104 { 1104 {
1105 - return ES_info.at(index); 1105 + return ES_info.at(index);
1106 } 1106 }
1107 1107
1108 int TSPayloadPMT::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p, TSMessage*& pmsg) 1108 int TSPayloadPMT::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p, TSMessage*& pmsg)
@@ -1144,55 +1144,55 @@ int TSPayloadPMT::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t @@ -1144,55 +1144,55 @@ int TSPayloadPMT::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t
1144 program_info_length &= 0xFFF; 1144 program_info_length &= 0xFFF;
1145 1145
1146 if (program_info_length > 0) { 1146 if (program_info_length > 0) {
1147 - program_info_desc = new char[program_info_length];  
1148 - memcpy(program_info_desc, p, program_info_length);  
1149 - p += program_info_length; 1147 + program_info_desc = new char[program_info_length];
  1148 + memcpy(program_info_desc, p, program_info_length);
  1149 + p += program_info_length;
1150 } 1150 }
1151 1151
1152 // [section_length] - 4(CRC) - 9B - [program_info_length] 1152 // [section_length] - 4(CRC) - 9B - [program_info_length]
1153 int ES_bytes = section_length - 4 - 9 - program_info_length; 1153 int ES_bytes = section_length - 4 - 9 - program_info_length;
1154 while (ES_bytes > 0) { 1154 while (ES_bytes > 0) {
1155 - TSPMTESInfo* info = new TSPMTESInfo();  
1156 -  
1157 - info->stream_type = *p++;  
1158 - ES_bytes--;  
1159 -  
1160 - pp = (char*)&info->elementary_PID;  
1161 - pp[1] = *p++;  
1162 - pp[0] = *p++;  
1163 - ES_bytes -= 2;  
1164 -  
1165 - info->elementary_PID &= 0x1FFF;  
1166 -  
1167 - pp = (char*)&info->ES_info_length;  
1168 - pp[1] = *p++;  
1169 - pp[0] = *p++;  
1170 - ES_bytes -= 2;  
1171 -  
1172 - info->ES_info_length &= 0x0FFF;  
1173 -  
1174 - if (info->ES_info_length > 0) {  
1175 - info->ES_info = new char[info->ES_info_length];  
1176 - memcpy(info->ES_info, p, info->ES_info_length);  
1177 -  
1178 - p += info->ES_info_length;  
1179 - ES_bytes -= info->ES_info_length;  
1180 - }  
1181 -  
1182 - ES_info.push_back(info);  
1183 -  
1184 - if (info->stream_type == TSStreamTypeVideoH264) {  
1185 - // TODO: support more video type.  
1186 - ctx->push((TSPidTable)info->elementary_PID, (TSStreamType)info->stream_type, TSPidTypeVideo, pkt->header->continuity_counter);  
1187 - trace("ts+pmt add pid: %d, type: H264 video", info->elementary_PID);  
1188 - } else if (info->stream_type == TSStreamTypeAudioAAC) {  
1189 - // TODO: support more audio type.  
1190 - // see aac: 6.2 Audio Data Transport Stream, ADTS  
1191 - ctx->push((TSPidTable)info->elementary_PID, (TSStreamType)info->stream_type, TSPidTypeAudio, pkt->header->continuity_counter);  
1192 - trace("ts+pmt add pid: %d, type: AAC audio", info->elementary_PID);  
1193 - } else {  
1194 - trace("ts+pmt ignore the stream type: %d", info->stream_type);  
1195 - } 1155 + TSPMTESInfo* info = new TSPMTESInfo();
  1156 +
  1157 + info->stream_type = *p++;
  1158 + ES_bytes--;
  1159 +
  1160 + pp = (char*)&info->elementary_PID;
  1161 + pp[1] = *p++;
  1162 + pp[0] = *p++;
  1163 + ES_bytes -= 2;
  1164 +
  1165 + info->elementary_PID &= 0x1FFF;
  1166 +
  1167 + pp = (char*)&info->ES_info_length;
  1168 + pp[1] = *p++;
  1169 + pp[0] = *p++;
  1170 + ES_bytes -= 2;
  1171 +
  1172 + info->ES_info_length &= 0x0FFF;
  1173 +
  1174 + if (info->ES_info_length > 0) {
  1175 + info->ES_info = new char[info->ES_info_length];
  1176 + memcpy(info->ES_info, p, info->ES_info_length);
  1177 +
  1178 + p += info->ES_info_length;
  1179 + ES_bytes -= info->ES_info_length;
  1180 + }
  1181 +
  1182 + ES_info.push_back(info);
  1183 +
  1184 + if (info->stream_type == TSStreamTypeVideoH264) {
  1185 + // TODO: support more video type.
  1186 + ctx->push((TSPidTable)info->elementary_PID, (TSStreamType)info->stream_type, TSPidTypeVideo, pkt->header->continuity_counter);
  1187 + trace("ts+pmt add pid: %d, type: H264 video", info->elementary_PID);
  1188 + } else if (info->stream_type == TSStreamTypeAudioAAC) {
  1189 + // TODO: support more audio type.
  1190 + // see aac: 6.2 Audio Data Transport Stream, ADTS
  1191 + ctx->push((TSPidTable)info->elementary_PID, (TSStreamType)info->stream_type, TSPidTypeAudio, pkt->header->continuity_counter);
  1192 + trace("ts+pmt add pid: %d, type: AAC audio", info->elementary_PID);
  1193 + } else {
  1194 + trace("ts+pmt ignore the stream type: %d", info->stream_type);
  1195 + }
1196 } 1196 }
1197 1197
1198 pp = (char*)&CRC_32; 1198 pp = (char*)&CRC_32;
@@ -1252,44 +1252,44 @@ TSPayloadPES::TSPayloadPES() @@ -1252,44 +1252,44 @@ TSPayloadPES::TSPayloadPES()
1252 1252
1253 TSPayloadPES::~TSPayloadPES() 1253 TSPayloadPES::~TSPayloadPES()
1254 { 1254 {
1255 - srs_freepa(PES_private_data);  
1256 - srs_freepa(pack_field);  
1257 - srs_freepa(PES_extension_field);  
1258 - srs_freepa(stuffing_byte); 1255 + srs_freepa(PES_private_data);
  1256 + srs_freepa(pack_field);
  1257 + srs_freepa(PES_extension_field);
  1258 + srs_freepa(stuffing_byte);
1259 } 1259 }
1260 1260
1261 int64_t TSPayloadPES::decode_33bits_int(u_int8_t*& p, int64_t& temp) 1261 int64_t TSPayloadPES::decode_33bits_int(u_int8_t*& p, int64_t& temp)
1262 { 1262 {
1263 - char* pp = (char*)&temp;  
1264 - pp[4] = *p++;  
1265 - pp[3] = *p++;  
1266 - pp[2] = *p++;  
1267 - pp[1] = *p++;  
1268 - pp[0] = *p++;  
1269 -  
1270 - return decode_33bits_int(temp); 1263 + char* pp = (char*)&temp;
  1264 + pp[4] = *p++;
  1265 + pp[3] = *p++;
  1266 + pp[2] = *p++;
  1267 + pp[1] = *p++;
  1268 + pp[0] = *p++;
  1269 +
  1270 + return decode_33bits_int(temp);
1271 } 1271 }
1272 1272
1273 int64_t TSPayloadPES::decode_33bits_int(int64_t& temp) 1273 int64_t TSPayloadPES::decode_33bits_int(int64_t& temp)
1274 { 1274 {
1275 int64_t ret = 0; 1275 int64_t ret = 0;
1276 -  
1277 - // marker_bit 1bit  
1278 - temp = temp >> 1;  
1279 - // PTS [14..0] 15bits  
1280 - ret |= temp & 0x7fff;  
1281 - // marker_bit 1bit  
1282 - temp = temp >> 1;  
1283 - // PTS [29..15] 15bits, 15zero, 29-15+1one  
1284 - ret |= temp & 0x3fff8000LL;  
1285 - // marker_bit 1bit  
1286 - temp = temp >> 1;  
1287 - // PTS [32..30] 3bits  
1288 - ret |= temp & 0x1c0000000LL;  
1289 -  
1290 - temp = temp >> 33;  
1291 -  
1292 - return ret; 1276 +
  1277 + // marker_bit 1bit
  1278 + temp = temp >> 1;
  1279 + // PTS [14..0] 15bits
  1280 + ret |= temp & 0x7fff;
  1281 + // marker_bit 1bit
  1282 + temp = temp >> 1;
  1283 + // PTS [29..15] 15bits, 15zero, 29-15+1one
  1284 + ret |= temp & 0x3fff8000LL;
  1285 + // marker_bit 1bit
  1286 + temp = temp >> 1;
  1287 + // PTS [32..30] 3bits
  1288 + ret |= temp & 0x1c0000000LL;
  1289 +
  1290 + temp = temp >> 33;
  1291 +
  1292 + return ret;
1293 } 1293 }
1294 1294
1295 int TSPayloadPES::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p, TSMessage*& pmsg) 1295 int TSPayloadPES::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* last, u_int8_t*& p, TSMessage*& pmsg)
@@ -1302,249 +1302,249 @@ int TSPayloadPES::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t @@ -1302,249 +1302,249 @@ int TSPayloadPES::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t
1302 pp[0] = *p++; 1302 pp[0] = *p++;
1303 1303
1304 packet_start_code_prefix &= 0xFFFFFF; 1304 packet_start_code_prefix &= 0xFFFFFF;
1305 - if (packet_start_code_prefix != 0x01) {  
1306 - trace("ts+pes decode unit start packet error, msg is empty.");  
1307 - return -1;  
1308 - }  
1309 -  
1310 - stream_id = *p++;  
1311 -  
1312 - pp = (char*)&PES_packet_length;  
1313 - pp[1] = *p++;  
1314 - pp[0] = *p++;  
1315 - u_int8_t* pos_packet = p;  
1316 -  
1317 - if (stream_id != PES_program_stream_map  
1318 - && stream_id != PES_padding_stream  
1319 - && stream_id != PES_private_stream_2  
1320 - && stream_id != PES_ECM_stream  
1321 - && stream_id != PES_EMM_stream  
1322 - && stream_id != PES_program_stream_directory  
1323 - && stream_id != PES_DSMCC_stream  
1324 - && stream_id != PES_H_222_1_type_E  
1325 - ) {  
1326 - original_or_copy = *p++;  
1327 -  
1328 - //int8_t const2bits = (original_or_copy >> 6) & 0x03;  
1329 - PES_scrambling_control = (original_or_copy >> 4) & 0x03;  
1330 - PES_priority = (original_or_copy >> 3) & 0x01;  
1331 - data_alignment_indicator = (original_or_copy >> 2) & 0x01;  
1332 - copyright = (original_or_copy >> 1) & 0x01;  
1333 - original_or_copy &= 0x01;  
1334 -  
1335 - PES_extension_flag = *p++;  
1336 -  
1337 - PTS_DTS_flags = (PES_extension_flag >> 6) & 0x03;  
1338 - ESCR_flag = (PES_extension_flag >> 5) & 0x01;  
1339 - ES_rate_flag = (PES_extension_flag >> 4) & 0x01;  
1340 - DSM_trick_mode_flag = (PES_extension_flag >> 3) & 0x01;  
1341 - additional_copy_info_flag = (PES_extension_flag >> 2) & 0x01;  
1342 - PES_CRC_flag = (PES_extension_flag >> 1) & 0x01;  
1343 - PES_extension_flag &= 0x01;  
1344 -  
1345 - PES_header_data_length = *p++;  
1346 - u_int8_t* pos_header = p;  
1347 -  
1348 - int64_t temp = 0;  
1349 - if (PTS_DTS_flags == 0x2) {  
1350 - pts = decode_33bits_int(p, temp);  
1351 - // '0010' 4bits  
1352 - //int8_t const4bits = temp & 0x0F;  
1353 - }  
1354 -  
1355 - if (PTS_DTS_flags == 0x3) {  
1356 - pts = decode_33bits_int(p, temp);  
1357 - // '0011' 4bits  
1358 - //int8_t const4bits = temp & 0x0F;  
1359 -  
1360 - dts = decode_33bits_int(p, temp);  
1361 - // '0001' 4bits  
1362 - //int8_t const4bits = temp & 0x0F;  
1363 - }  
1364 -  
1365 - if (ESCR_flag) {  
1366 - pp = (char*)&temp;  
1367 - pp[5] = *p++;  
1368 - pp[4] = *p++;  
1369 - pp[3] = *p++;  
1370 - pp[2] = *p++;  
1371 - pp[1] = *p++;  
1372 - pp[0] = *p++;  
1373 -  
1374 - // marker_bit 1bit  
1375 - temp = temp >> 1;  
1376 - // ESCR_extension 9bits  
1377 - ESCR_extension = temp & 0x1f;  
1378 - temp = temp >> 9;  
1379 -  
1380 - ESCR_base = decode_33bits_int(temp);  
1381 -  
1382 - // reserved 2bits  
1383 - //int8_t reserved2bits = temp & 0x03;  
1384 - }  
1385 -  
1386 - if (ES_rate_flag) {  
1387 - pp = (char*)&ES_rate;  
1388 - pp[2] = *p++;  
1389 - pp[1] = *p++;  
1390 - pp[0] = *p++;  
1391 -  
1392 - ES_rate = ES_rate >> 1;  
1393 - ES_rate &= 0x3FFFFF;  
1394 - }  
1395 -  
1396 - if (DSM_trick_mode_flag) {  
1397 - trick_mode_control = *p++;  
1398 -  
1399 - trick_mode_value = trick_mode_control & 0x1f;  
1400 - trick_mode_control = (trick_mode_control >> 5) & 0x03;  
1401 - }  
1402 -  
1403 - if (additional_copy_info_flag) {  
1404 - additional_copy_info = *p++;  
1405 - additional_copy_info &= 0x7f;  
1406 - }  
1407 -  
1408 - if (PES_CRC_flag) {  
1409 - pp = (char*)&previous_PES_packet_CRC;  
1410 - pp[1] = *p++;  
1411 - pp[0] = *p++;  
1412 - }  
1413 -  
1414 - if (PES_extension_flag) {  
1415 - PES_extension_flag_2 = *p++;  
1416 -  
1417 - PES_private_data_flag = (PES_extension_flag_2 >> 7) & 0x01;  
1418 - pack_header_field_flag = (PES_extension_flag_2 >> 6) & 0x01;  
1419 - program_packet_sequence_counter_flag = (PES_extension_flag_2 >> 5) & 0x01;  
1420 - P_STD_buffer_flag = (PES_extension_flag_2 >> 4) & 0x01;  
1421 - PES_extension_flag_2 &= PES_extension_flag_2 & 0x01;  
1422 -  
1423 - if (PES_private_data_flag) {  
1424 - PES_private_data = new char[16];  
1425 - memcpy(PES_private_data, p, 16);  
1426 - p += 16;  
1427 - }  
1428 -  
1429 - if (pack_header_field_flag) {  
1430 - pack_field_length = *p++;  
1431 - if (pack_field_length > 0) {  
1432 - pack_field = new char[pack_field_length];  
1433 - memcpy(pack_field, p, pack_field_length);  
1434 - p += pack_field_length;  
1435 - }  
1436 - }  
1437 -  
1438 - if (program_packet_sequence_counter_flag) {  
1439 - program_packet_sequence_counter = *p++;  
1440 - program_packet_sequence_counter &= 0x7f;  
1441 -  
1442 - original_stuff_length = *p++;  
1443 - MPEG1_MPEG2_identifier = (original_stuff_length >> 6) & 0x01;  
1444 - original_stuff_length &= 0x3f;  
1445 - }  
1446 -  
1447 - if (P_STD_buffer_flag) {  
1448 - pp = (char*)&P_STD_buffer_size;  
1449 - pp[1] = *p++;  
1450 - pp[0] = *p++;  
1451 -  
1452 - // '01'  
1453 - //int8_t const2bits = (P_STD_buffer_scale >>14) & 0x03;  
1454 -  
1455 - P_STD_buffer_scale = (P_STD_buffer_scale >>13) & 0x01;  
1456 - P_STD_buffer_size &= 0x1FFF;  
1457 - }  
1458 -  
1459 - if (PES_extension_flag_2) {  
1460 - PES_extension_field_length = *p++;  
1461 - PES_extension_field_length &= 0x07;  
1462 -  
1463 - if (PES_extension_field_length > 0) {  
1464 - PES_extension_field = new char[PES_extension_field_length];  
1465 - memcpy(PES_extension_field, p, PES_extension_field_length);  
1466 - p += PES_extension_field_length;  
1467 - }  
1468 - }  
1469 - }  
1470 -  
1471 - // stuffing_byte  
1472 - int stuffing_size = PES_header_data_length - (p - pos_header);  
1473 - if (stuffing_size > 0) {  
1474 - stuffing_byte = new char[stuffing_size];  
1475 - memcpy(stuffing_byte, p, stuffing_size);  
1476 - p += stuffing_size;  
1477 - }  
1478 -  
1479 - // get the pid.  
1480 - TSPid* pid = ctx->get(pkt->header->pid);  
1481 - if (!pid) {  
1482 - trace("ts+pes pid: %d type is invalid.", pkt->header->pid);  
1483 - }  
1484 -  
1485 - // get the message to build from the chunks(PES packets).  
1486 - TSMessage* msg = ctx->get_msg(pid->pid);  
1487 -  
1488 - msg->type = pid->type;  
1489 - msg->stream_type = pid->stream_type;  
1490 - msg->continuity_counter = pid->continuity_counter;  
1491 - msg->stream_id = stream_id;  
1492 - msg->packet_start_code_prefix = packet_start_code_prefix;  
1493 - msg->dts = dts;  
1494 - msg->pts = pts;  
1495 -  
1496 - // PES_packet_data_byte, page58.  
1497 - // the packet size contains the header size.  
1498 - // The number of PES_packet_data_bytes, N, is specified by the  
1499 - // PES_packet_length field. N shall be equal to the value  
1500 - // indicated in the PES_packet_length minus the number of bytes  
1501 - // between the last byte of the PES_packet_length field and the  
1502 - // first PES_packet_data_byte.  
1503 - msg->PES_packet_length = PES_packet_length;  
1504 - msg->packet_header_size = p - pos_packet;  
1505 - msg->packet_data_size = PES_packet_length - msg->packet_header_size;  
1506 -  
1507 - /**  
1508 - * when actual packet length > 0xffff(65535),  
1509 - * which exceed the max u_int16_t packet length,  
1510 - * use 0 packet length, the next unit start indicates the end of packet.  
1511 - */  
1512 - if (PES_packet_length == 0) {  
1513 - msg->packet_data_size = last - p - msg->packet_header_size;  
1514 - }  
1515 -  
1516 - if (msg->packet_data_size > 0) {  
1517 - msg->packet_data = new char[msg->packet_data_size];  
1518 - }  
1519 -  
1520 - // PES_packet_data_byte  
1521 - int size = srs_min(msg->packet_data_size, last - p);  
1522 - if (size > 0) {  
1523 - msg->append(p, size);  
1524 - }  
1525 -  
1526 - if (PES_packet_length > 0) {  
1527 - msg->detach(ctx, pmsg);  
1528 - }  
1529 -  
1530 - trace("ts+pes stream_id: %d size: %d pts: %"PRId64" dts: %"PRId64" total: %d header: %d packet_size: %d parsed_size: %d",  
1531 - stream_id, PES_packet_length, pts, dts, msg->PES_packet_length, msg->packet_header_size, msg->packet_data_size, msg->parsed_packet_size);  
1532 - } else if (stream_id == PES_program_stream_map  
1533 - || stream_id == PES_private_stream_2  
1534 - || stream_id == PES_ECM_stream  
1535 - || stream_id == PES_EMM_stream  
1536 - || stream_id == PES_program_stream_directory  
1537 - || stream_id == PES_DSMCC_stream  
1538 - || stream_id == PES_H_222_1_type_E  
1539 - ) {  
1540 - // for (i = 0; i < PES_packet_length; i++) {  
1541 - // PES_packet_data_byte  
1542 - // }  
1543 - } else if (stream_id != PES_padding_stream) {  
1544 - // for (i = 0; i < PES_packet_length; i++) {  
1545 - // padding_byte  
1546 - // }  
1547 - } 1305 + if (packet_start_code_prefix != 0x01) {
  1306 + trace("ts+pes decode unit start packet error, msg is empty.");
  1307 + return -1;
  1308 + }
  1309 +
  1310 + stream_id = *p++;
  1311 +
  1312 + pp = (char*)&PES_packet_length;
  1313 + pp[1] = *p++;
  1314 + pp[0] = *p++;
  1315 + u_int8_t* pos_packet = p;
  1316 +
  1317 + if (stream_id != PES_program_stream_map
  1318 + && stream_id != PES_padding_stream
  1319 + && stream_id != PES_private_stream_2
  1320 + && stream_id != PES_ECM_stream
  1321 + && stream_id != PES_EMM_stream
  1322 + && stream_id != PES_program_stream_directory
  1323 + && stream_id != PES_DSMCC_stream
  1324 + && stream_id != PES_H_222_1_type_E
  1325 + ) {
  1326 + original_or_copy = *p++;
  1327 +
  1328 + //int8_t const2bits = (original_or_copy >> 6) & 0x03;
  1329 + PES_scrambling_control = (original_or_copy >> 4) & 0x03;
  1330 + PES_priority = (original_or_copy >> 3) & 0x01;
  1331 + data_alignment_indicator = (original_or_copy >> 2) & 0x01;
  1332 + copyright = (original_or_copy >> 1) & 0x01;
  1333 + original_or_copy &= 0x01;
  1334 +
  1335 + PES_extension_flag = *p++;
  1336 +
  1337 + PTS_DTS_flags = (PES_extension_flag >> 6) & 0x03;
  1338 + ESCR_flag = (PES_extension_flag >> 5) & 0x01;
  1339 + ES_rate_flag = (PES_extension_flag >> 4) & 0x01;
  1340 + DSM_trick_mode_flag = (PES_extension_flag >> 3) & 0x01;
  1341 + additional_copy_info_flag = (PES_extension_flag >> 2) & 0x01;
  1342 + PES_CRC_flag = (PES_extension_flag >> 1) & 0x01;
  1343 + PES_extension_flag &= 0x01;
  1344 +
  1345 + PES_header_data_length = *p++;
  1346 + u_int8_t* pos_header = p;
  1347 +
  1348 + int64_t temp = 0;
  1349 + if (PTS_DTS_flags == 0x2) {
  1350 + pts = decode_33bits_int(p, temp);
  1351 + // '0010' 4bits
  1352 + //int8_t const4bits = temp & 0x0F;
  1353 + }
  1354 +
  1355 + if (PTS_DTS_flags == 0x3) {
  1356 + pts = decode_33bits_int(p, temp);
  1357 + // '0011' 4bits
  1358 + //int8_t const4bits = temp & 0x0F;
  1359 +
  1360 + dts = decode_33bits_int(p, temp);
  1361 + // '0001' 4bits
  1362 + //int8_t const4bits = temp & 0x0F;
  1363 + }
  1364 +
  1365 + if (ESCR_flag) {
  1366 + pp = (char*)&temp;
  1367 + pp[5] = *p++;
  1368 + pp[4] = *p++;
  1369 + pp[3] = *p++;
  1370 + pp[2] = *p++;
  1371 + pp[1] = *p++;
  1372 + pp[0] = *p++;
  1373 +
  1374 + // marker_bit 1bit
  1375 + temp = temp >> 1;
  1376 + // ESCR_extension 9bits
  1377 + ESCR_extension = temp & 0x1f;
  1378 + temp = temp >> 9;
  1379 +
  1380 + ESCR_base = decode_33bits_int(temp);
  1381 +
  1382 + // reserved 2bits
  1383 + //int8_t reserved2bits = temp & 0x03;
  1384 + }
  1385 +
  1386 + if (ES_rate_flag) {
  1387 + pp = (char*)&ES_rate;
  1388 + pp[2] = *p++;
  1389 + pp[1] = *p++;
  1390 + pp[0] = *p++;
  1391 +
  1392 + ES_rate = ES_rate >> 1;
  1393 + ES_rate &= 0x3FFFFF;
  1394 + }
  1395 +
  1396 + if (DSM_trick_mode_flag) {
  1397 + trick_mode_control = *p++;
  1398 +
  1399 + trick_mode_value = trick_mode_control & 0x1f;
  1400 + trick_mode_control = (trick_mode_control >> 5) & 0x03;
  1401 + }
  1402 +
  1403 + if (additional_copy_info_flag) {
  1404 + additional_copy_info = *p++;
  1405 + additional_copy_info &= 0x7f;
  1406 + }
  1407 +
  1408 + if (PES_CRC_flag) {
  1409 + pp = (char*)&previous_PES_packet_CRC;
  1410 + pp[1] = *p++;
  1411 + pp[0] = *p++;
  1412 + }
  1413 +
  1414 + if (PES_extension_flag) {
  1415 + PES_extension_flag_2 = *p++;
  1416 +
  1417 + PES_private_data_flag = (PES_extension_flag_2 >> 7) & 0x01;
  1418 + pack_header_field_flag = (PES_extension_flag_2 >> 6) & 0x01;
  1419 + program_packet_sequence_counter_flag = (PES_extension_flag_2 >> 5) & 0x01;
  1420 + P_STD_buffer_flag = (PES_extension_flag_2 >> 4) & 0x01;
  1421 + PES_extension_flag_2 &= PES_extension_flag_2 & 0x01;
  1422 +
  1423 + if (PES_private_data_flag) {
  1424 + PES_private_data = new char[16];
  1425 + memcpy(PES_private_data, p, 16);
  1426 + p += 16;
  1427 + }
  1428 +
  1429 + if (pack_header_field_flag) {
  1430 + pack_field_length = *p++;
  1431 + if (pack_field_length > 0) {
  1432 + pack_field = new char[pack_field_length];
  1433 + memcpy(pack_field, p, pack_field_length);
  1434 + p += pack_field_length;
  1435 + }
  1436 + }
  1437 +
  1438 + if (program_packet_sequence_counter_flag) {
  1439 + program_packet_sequence_counter = *p++;
  1440 + program_packet_sequence_counter &= 0x7f;
  1441 +
  1442 + original_stuff_length = *p++;
  1443 + MPEG1_MPEG2_identifier = (original_stuff_length >> 6) & 0x01;
  1444 + original_stuff_length &= 0x3f;
  1445 + }
  1446 +
  1447 + if (P_STD_buffer_flag) {
  1448 + pp = (char*)&P_STD_buffer_size;
  1449 + pp[1] = *p++;
  1450 + pp[0] = *p++;
  1451 +
  1452 + // '01'
  1453 + //int8_t const2bits = (P_STD_buffer_scale >>14) & 0x03;
  1454 +
  1455 + P_STD_buffer_scale = (P_STD_buffer_scale >>13) & 0x01;
  1456 + P_STD_buffer_size &= 0x1FFF;
  1457 + }
  1458 +
  1459 + if (PES_extension_flag_2) {
  1460 + PES_extension_field_length = *p++;
  1461 + PES_extension_field_length &= 0x07;
  1462 +
  1463 + if (PES_extension_field_length > 0) {
  1464 + PES_extension_field = new char[PES_extension_field_length];
  1465 + memcpy(PES_extension_field, p, PES_extension_field_length);
  1466 + p += PES_extension_field_length;
  1467 + }
  1468 + }
  1469 + }
  1470 +
  1471 + // stuffing_byte
  1472 + int stuffing_size = PES_header_data_length - (p - pos_header);
  1473 + if (stuffing_size > 0) {
  1474 + stuffing_byte = new char[stuffing_size];
  1475 + memcpy(stuffing_byte, p, stuffing_size);
  1476 + p += stuffing_size;
  1477 + }
  1478 +
  1479 + // get the pid.
  1480 + TSPid* pid = ctx->get(pkt->header->pid);
  1481 + if (!pid) {
  1482 + trace("ts+pes pid: %d type is invalid.", pkt->header->pid);
  1483 + }
  1484 +
  1485 + // get the message to build from the chunks(PES packets).
  1486 + TSMessage* msg = ctx->get_msg(pid->pid);
  1487 +
  1488 + msg->type = pid->type;
  1489 + msg->stream_type = pid->stream_type;
  1490 + msg->continuity_counter = pid->continuity_counter;
  1491 + msg->stream_id = stream_id;
  1492 + msg->packet_start_code_prefix = packet_start_code_prefix;
  1493 + msg->dts = dts;
  1494 + msg->pts = pts;
  1495 +
  1496 + // PES_packet_data_byte, page58.
  1497 + // the packet size contains the header size.
  1498 + // The number of PES_packet_data_bytes, N, is specified by the
  1499 + // PES_packet_length field. N shall be equal to the value
  1500 + // indicated in the PES_packet_length minus the number of bytes
  1501 + // between the last byte of the PES_packet_length field and the
  1502 + // first PES_packet_data_byte.
  1503 + msg->PES_packet_length = PES_packet_length;
  1504 + msg->packet_header_size = p - pos_packet;
  1505 + msg->packet_data_size = PES_packet_length - msg->packet_header_size;
  1506 +
  1507 + /**
  1508 + * when actual packet length > 0xffff(65535),
  1509 + * which exceed the max u_int16_t packet length,
  1510 + * use 0 packet length, the next unit start indicates the end of packet.
  1511 + */
  1512 + if (PES_packet_length == 0) {
  1513 + msg->packet_data_size = last - p - msg->packet_header_size;
  1514 + }
  1515 +
  1516 + if (msg->packet_data_size > 0) {
  1517 + msg->packet_data = new char[msg->packet_data_size];
  1518 + }
  1519 +
  1520 + // PES_packet_data_byte
  1521 + int size = srs_min(msg->packet_data_size, last - p);
  1522 + if (size > 0) {
  1523 + msg->append(p, size);
  1524 + }
  1525 +
  1526 + if (PES_packet_length > 0) {
  1527 + msg->detach(ctx, pmsg);
  1528 + }
  1529 +
  1530 + trace("ts+pes stream_id: %d size: %d pts: %"PRId64" dts: %"PRId64" total: %d header: %d packet_size: %d parsed_size: %d",
  1531 + stream_id, PES_packet_length, pts, dts, msg->PES_packet_length, msg->packet_header_size, msg->packet_data_size, msg->parsed_packet_size);
  1532 + } else if (stream_id == PES_program_stream_map
  1533 + || stream_id == PES_private_stream_2
  1534 + || stream_id == PES_ECM_stream
  1535 + || stream_id == PES_EMM_stream
  1536 + || stream_id == PES_program_stream_directory
  1537 + || stream_id == PES_DSMCC_stream
  1538 + || stream_id == PES_H_222_1_type_E
  1539 + ) {
  1540 + // for (i = 0; i < PES_packet_length; i++) {
  1541 + // PES_packet_data_byte
  1542 + // }
  1543 + } else if (stream_id != PES_padding_stream) {
  1544 + // for (i = 0; i < PES_packet_length; i++) {
  1545 + // padding_byte
  1546 + // }
  1547 + }
1548 1548
1549 return ret; 1549 return ret;
1550 } 1550 }
@@ -1603,16 +1603,16 @@ int TSPayload::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* l @@ -1603,16 +1603,16 @@ int TSPayload::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* l
1603 return pmt->demux(ctx, pkt, start, last, p, pmsg); 1603 return pmt->demux(ctx, pkt, start, last, p, pmsg);
1604 } 1604 }
1605 if (pid && (pid->type == TSPidTypeVideo || pid->type == TSPidTypeAudio)) { 1605 if (pid && (pid->type == TSPidTypeVideo || pid->type == TSPidTypeAudio)) {
1606 - TSMessage* msg = ctx->get_msg(pkt->header->pid); 1606 + TSMessage* msg = ctx->get_msg(pkt->header->pid);
1607 1607
1608 - if (pkt->adaption_field->pcr > 0) {  
1609 - msg->pcr = pkt->adaption_field->pcr;  
1610 - }  
1611 - 1608 + if (pkt->adaption_field->pcr > 0) {
  1609 + msg->pcr = pkt->adaption_field->pcr;
  1610 + }
  1611 +
1612 // flush previous PES_packet_length(0) packets. 1612 // flush previous PES_packet_length(0) packets.
1613 if (msg->packet_start_code_prefix == 0x01 1613 if (msg->packet_start_code_prefix == 0x01
1614 && pkt->header->payload_unit_start_indicator == 1 1614 && pkt->header->payload_unit_start_indicator == 1
1615 - && msg->PES_packet_length == 0 1615 + && msg->PES_packet_length == 0
1616 ) { 1616 ) {
1617 msg->detach(ctx, pmsg); 1617 msg->detach(ctx, pmsg);
1618 // reparse current message 1618 // reparse current message
@@ -1620,21 +1620,21 @@ int TSPayload::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* l @@ -1620,21 +1620,21 @@ int TSPayload::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* l
1620 return ret; 1620 return ret;
1621 } 1621 }
1622 1622
1623 - // parse continous packet.  
1624 - if (!pkt->header->payload_unit_start_indicator) {  
1625 - if (msg->packet_start_code_prefix != 0x01) {  
1626 - trace("ts+pes decode continous packet error, msg is empty.");  
1627 - return -1;  
1628 - }  
1629 - msg->append(p, last - p);  
1630 -  
1631 - // for PES_packet_length is 0, donot attach it.  
1632 - if (msg->PES_packet_length > 0) {  
1633 - msg->detach(ctx, pmsg);  
1634 - }  
1635 - return ret;  
1636 - }  
1637 - 1623 + // parse continous packet.
  1624 + if (!pkt->header->payload_unit_start_indicator) {
  1625 + if (msg->packet_start_code_prefix != 0x01) {
  1626 + trace("ts+pes decode continous packet error, msg is empty.");
  1627 + return -1;
  1628 + }
  1629 + msg->append(p, last - p);
  1630 +
  1631 + // for PES_packet_length is 0, donot attach it.
  1632 + if (msg->PES_packet_length > 0) {
  1633 + msg->detach(ctx, pmsg);
  1634 + }
  1635 + return ret;
  1636 + }
  1637 +
1638 type = pid->type; 1638 type = pid->type;
1639 pes = new TSPayloadPES(); 1639 pes = new TSPayloadPES();
1640 return pes->demux(ctx, pkt, start, last, p, pmsg); 1640 return pes->demux(ctx, pkt, start, last, p, pmsg);
@@ -1684,7 +1684,7 @@ int TSPacket::demux(TSContext* ctx, u_int8_t* start, u_int8_t* last, u_int8_t*& @@ -1684,7 +1684,7 @@ int TSPacket::demux(TSContext* ctx, u_int8_t* start, u_int8_t* last, u_int8_t*&
1684 payload->size = TS_PACKET_SIZE - header->get_size() - adaption_field->get_size(); 1684 payload->size = TS_PACKET_SIZE - header->get_size() - adaption_field->get_size();
1685 1685
1686 if (header->adaption_field_control == TSAdaptionTypePayloadOnly || header->adaption_field_control == TSAdaptionTypeBoth) { 1686 if (header->adaption_field_control == TSAdaptionTypePayloadOnly || header->adaption_field_control == TSAdaptionTypeBoth) {
1687 - // parse new packet. 1687 + // parse new packet.
1688 if ((ret = payload->demux(ctx, this, start, last, p, pmsg)) != 0) { 1688 if ((ret = payload->demux(ctx, this, start, last, p, pmsg)) != 0) {
1689 trace("ts+header payload decode error. ret=%d", ret); 1689 trace("ts+header payload decode error. ret=%d", ret);
1690 return ret; 1690 return ret;
@@ -1771,84 +1771,84 @@ int TSHeader::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* la @@ -1771,84 +1771,84 @@ int TSHeader::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t* la
1771 class TSH264Codec 1771 class TSH264Codec
1772 { 1772 {
1773 public: 1773 public:
1774 - u_int8_t* raw_data;  
1775 - int size;  
1776 -  
1777 - TSH264Codec()  
1778 - {  
1779 - size = 0;  
1780 - raw_data = NULL;  
1781 - }  
1782 -  
1783 - u_int8_t at(int index)  
1784 - {  
1785 - if (index >= size) {  
1786 - return 0;  
1787 - }  
1788 - return raw_data[index];  
1789 - }  
1790 -  
1791 - int parse(TSMessage* msg, char* last, char*& p)  
1792 - {  
1793 - int ret = 0;  
1794 -  
1795 - srs_assert(p);  
1796 -  
1797 - while (next_start_code_prefix(p) != 0x000001) {  
1798 - char ch = *p++;  
1799 - if (ch != 0x00) {  
1800 - trace("ts+h264 parse msg failed, "  
1801 - "expect 0x00 before start-code. actual is: %#x", (u_int8_t)ch);  
1802 - return -1;  
1803 - }  
1804 - }  
1805 -  
1806 - if (p >= last) {  
1807 - trace("ts+h264 parse msg finished, no start-code.");  
1808 - return ret;  
1809 - }  
1810 -  
1811 - // start_code_prefix_one_3bytes /* equal to 0x000001 */  
1812 - p += 3;  
1813 -  
1814 - if (p < last) {  
1815 - raw_data = (u_int8_t*)p;  
1816 - }  
1817 - while (p < last - 3) {  
1818 - if (match_start_code_prefix(p)) {  
1819 - break;  
1820 - }  
1821 - p++;  
1822 - }  
1823 -  
1824 - if (raw_data) {  
1825 - size = (u_int8_t*)p - raw_data;  
1826 - if (p == last - 3) {  
1827 - size = (u_int8_t*)last - raw_data;  
1828 - p = last;  
1829 - }  
1830 - }  
1831 -  
1832 - trace("ts+h264 parse msg finished");  
1833 - return ret;  
1834 - }  
1835 -  
1836 - bool match_start_code_prefix(char*p)  
1837 - {  
1838 - return p[0] == 0x00 && p[1] == 0x00 && (p[2] == 0x00 || p[2] == 0x01);  
1839 - }  
1840 -  
1841 - int32_t next_start_code_prefix(char* p)  
1842 - {  
1843 - int32_t value = 0;  
1844 - char* pp = (char*)&value;  
1845 -  
1846 - pp[2] = p[0];  
1847 - pp[1] = p[1];  
1848 - pp[0] = p[2];  
1849 -  
1850 - return value;  
1851 - } 1774 + u_int8_t* raw_data;
  1775 + int size;
  1776 +
  1777 + TSH264Codec()
  1778 + {
  1779 + size = 0;
  1780 + raw_data = NULL;
  1781 + }
  1782 +
  1783 + u_int8_t at(int index)
  1784 + {
  1785 + if (index >= size) {
  1786 + return 0;
  1787 + }
  1788 + return raw_data[index];
  1789 + }
  1790 +
  1791 + int parse(TSMessage* msg, char* last, char*& p)
  1792 + {
  1793 + int ret = 0;
  1794 +
  1795 + srs_assert(p);
  1796 +
  1797 + while (next_start_code_prefix(p) != 0x000001) {
  1798 + char ch = *p++;
  1799 + if (ch != 0x00) {
  1800 + trace("ts+h264 parse msg failed, "
  1801 + "expect 0x00 before start-code. actual is: %#x", (u_int8_t)ch);
  1802 + return -1;
  1803 + }
  1804 + }
  1805 +
  1806 + if (p >= last) {
  1807 + trace("ts+h264 parse msg finished, no start-code.");
  1808 + return ret;
  1809 + }
  1810 +
  1811 + // start_code_prefix_one_3bytes /* equal to 0x000001 */
  1812 + p += 3;
  1813 +
  1814 + if (p < last) {
  1815 + raw_data = (u_int8_t*)p;
  1816 + }
  1817 + while (p < last - 3) {
  1818 + if (match_start_code_prefix(p)) {
  1819 + break;
  1820 + }
  1821 + p++;
  1822 + }
  1823 +
  1824 + if (raw_data) {
  1825 + size = (u_int8_t*)p - raw_data;
  1826 + if (p == last - 3) {
  1827 + size = (u_int8_t*)last - raw_data;
  1828 + p = last;
  1829 + }
  1830 + }
  1831 +
  1832 + trace("ts+h264 parse msg finished");
  1833 + return ret;
  1834 + }
  1835 +
  1836 + bool match_start_code_prefix(char*p)
  1837 + {
  1838 + return p[0] == 0x00 && p[1] == 0x00 && (p[2] == 0x00 || p[2] == 0x01);
  1839 + }
  1840 +
  1841 + int32_t next_start_code_prefix(char* p)
  1842 + {
  1843 + int32_t value = 0;
  1844 + char* pp = (char*)&value;
  1845 +
  1846 + pp[2] = p[0];
  1847 + pp[1] = p[1];
  1848 + pp[0] = p[2];
  1849 +
  1850 + return value;
  1851 + }
1852 }; 1852 };
1853 1853
1854 /** 1854 /**
@@ -1857,22 +1857,22 @@ public: @@ -1857,22 +1857,22 @@ public:
1857 */ 1857 */
1858 enum TSAacSampleFrequency 1858 enum TSAacSampleFrequency
1859 { 1859 {
1860 - TSAacSampleFrequency96000 = 0x00,  
1861 - TSAacSampleFrequency88200 = 0x01,  
1862 - TSAacSampleFrequency64000 = 0x02,  
1863 - TSAacSampleFrequency48000 = 0x03,  
1864 - TSAacSampleFrequency44100 = 0x04,  
1865 - TSAacSampleFrequency32000 = 0x05,  
1866 - TSAacSampleFrequency24000 = 0x06,  
1867 - TSAacSampleFrequency22050 = 0x07,  
1868 - TSAacSampleFrequency16000 = 0x08,  
1869 - TSAacSampleFrequency12000 = 0x09,  
1870 - TSAacSampleFrequency11025 = 0x0a,  
1871 - TSAacSampleFrequency8000 = 0x0b,  
1872 - TSAacSampleFrequencyReserved0 = 0x0c,  
1873 - TSAacSampleFrequencyReserved1 = 0x0d,  
1874 - TSAacSampleFrequencyReserved2 = 0x0e,  
1875 - TSAacSampleFrequencyReserved3 = 0x0f, 1860 + TSAacSampleFrequency96000 = 0x00,
  1861 + TSAacSampleFrequency88200 = 0x01,
  1862 + TSAacSampleFrequency64000 = 0x02,
  1863 + TSAacSampleFrequency48000 = 0x03,
  1864 + TSAacSampleFrequency44100 = 0x04,
  1865 + TSAacSampleFrequency32000 = 0x05,
  1866 + TSAacSampleFrequency24000 = 0x06,
  1867 + TSAacSampleFrequency22050 = 0x07,
  1868 + TSAacSampleFrequency16000 = 0x08,
  1869 + TSAacSampleFrequency12000 = 0x09,
  1870 + TSAacSampleFrequency11025 = 0x0a,
  1871 + TSAacSampleFrequency8000 = 0x0b,
  1872 + TSAacSampleFrequencyReserved0 = 0x0c,
  1873 + TSAacSampleFrequencyReserved1 = 0x0d,
  1874 + TSAacSampleFrequencyReserved2 = 0x0e,
  1875 + TSAacSampleFrequencyReserved3 = 0x0f,
1876 }; 1876 };
1877 1877
1878 /** 1878 /**
@@ -1881,313 +1881,313 @@ enum TSAacSampleFrequency @@ -1881,313 +1881,313 @@ enum TSAacSampleFrequency
1881 class TSAacAdts 1881 class TSAacAdts
1882 { 1882 {
1883 public: 1883 public:
1884 - // adts_fixed_header  
1885 - // 2B, 16bits  
1886 - int16_t syncword; //12bits  
1887 - int8_t ID; //1bit  
1888 - int8_t layer; //2bits  
1889 - int8_t protection_absent; //1bit  
1890 - // 12bits  
1891 - int8_t profile; //2bit  
1892 - TSAacSampleFrequency sampling_frequency_index; //4bits  
1893 - int8_t private_bit; //1bit  
1894 - int8_t channel_configuration; //3bits  
1895 - int8_t original_or_copy; //1bit  
1896 - int8_t home; //1bit  
1897 -  
1898 - // adts_variable_header  
1899 - // 28bits  
1900 - int8_t copyright_identification_bit; //1bit  
1901 - int8_t copyright_identification_start; //1bit  
1902 - int16_t frame_length; //13bits  
1903 - int16_t adts_buffer_fullness; //11bits  
1904 - int8_t number_of_raw_data_blocks_in_frame; //2bits  
1905 -  
1906 - u_int8_t* raw_data;  
1907 - int size;  
1908 -  
1909 - TSAacAdts()  
1910 - {  
1911 - syncword = 0;  
1912 - ID = 0;  
1913 - layer = 0;  
1914 - protection_absent = 0;  
1915 - profile = 0;  
1916 - sampling_frequency_index = TSAacSampleFrequencyReserved0;  
1917 - private_bit = 0;  
1918 - channel_configuration = 0;  
1919 - original_or_copy = 0;  
1920 - home = 0;  
1921 - copyright_identification_bit = 0;  
1922 - copyright_identification_start = 0;  
1923 - frame_length = 0;  
1924 - adts_buffer_fullness = 0;  
1925 - number_of_raw_data_blocks_in_frame = 0;  
1926 -  
1927 - size = 0;  
1928 - raw_data = NULL;  
1929 - }  
1930 -  
1931 - u_int8_t at(int index)  
1932 - {  
1933 - if (index >= size) {  
1934 - return 0;  
1935 - }  
1936 - return raw_data[index];  
1937 - }  
1938 -  
1939 - int parse(TSMessage* msg, char*& p)  
1940 - {  
1941 - int ret = 0;  
1942 -  
1943 - srs_assert(p);  
1944 -  
1945 - char* start = p;  
1946 -  
1947 - // adts_fixed_header  
1948 - char* pp = (char*)&syncword;  
1949 - pp[1] = *p++;  
1950 - pp[0] = *p++;  
1951 -  
1952 - protection_absent = syncword & 0x01;  
1953 - layer = (syncword >> 1) & 0x03;  
1954 - ID = (syncword >> 3) & 0x01;  
1955 - syncword = (syncword >> 4) & 0x0FFF;  
1956 - if (syncword != 0xfff) {  
1957 - trace("ts+aac invalid sync word. expect 0xfff, actual %#x", syncword);  
1958 - return -1;  
1959 - }  
1960 -  
1961 - // adts_variable_header  
1962 - int64_t temp = 0;  
1963 - pp = (char*)&temp;  
1964 - pp[4] = *p++;  
1965 - pp[3] = *p++;  
1966 - pp[2] = *p++;  
1967 - pp[1] = *p++;  
1968 - pp[0] = *p++;  
1969 -  
1970 - number_of_raw_data_blocks_in_frame = temp & 0x03;  
1971 - temp = temp >> 2;  
1972 -  
1973 - adts_buffer_fullness = temp & 0x7FF;  
1974 - temp = temp >> 11;  
1975 -  
1976 - frame_length = temp & 0x1FFF;  
1977 - temp = temp >> 13;  
1978 -  
1979 - copyright_identification_start = temp & 0x01;  
1980 - temp = temp >> 1;  
1981 -  
1982 - copyright_identification_bit = temp & 0x01;  
1983 - temp = temp >> 1;  
1984 -  
1985 - // adts_fixed_header  
1986 - home = temp & 0x01;  
1987 - temp = temp >> 1;  
1988 -  
1989 - original_or_copy = temp & 0x01;  
1990 - temp = temp >> 1;  
1991 -  
1992 - channel_configuration = temp & 0x07;  
1993 - temp = temp >> 3;  
1994 -  
1995 - private_bit = temp & 0x01;  
1996 - temp = temp >> 1;  
1997 -  
1998 - sampling_frequency_index = (TSAacSampleFrequency)(temp & 0x0F);  
1999 - temp = temp >> 4;  
2000 -  
2001 - profile = temp & 0x03;  
2002 - temp = temp >> 2;  
2003 -  
2004 - if (!number_of_raw_data_blocks_in_frame) {  
2005 - // adts_error_check  
2006 - if (!protection_absent) {  
2007 - // crc_check  
2008 - trace("ts+aac TODO: crc_check.");  
2009 - }  
2010 - // raw_data_block  
2011 - raw_data = (u_int8_t*)p;  
2012 - size = frame_length - (p - start);  
2013 - p += size;  
2014 - } else {  
2015 - trace("ts+aac TODO: parse multiple blocks.");  
2016 - }  
2017 -  
2018 - return ret;  
2019 - } 1884 + // adts_fixed_header
  1885 + // 2B, 16bits
  1886 + int16_t syncword; //12bits
  1887 + int8_t ID; //1bit
  1888 + int8_t layer; //2bits
  1889 + int8_t protection_absent; //1bit
  1890 + // 12bits
  1891 + int8_t profile; //2bit
  1892 + TSAacSampleFrequency sampling_frequency_index; //4bits
  1893 + int8_t private_bit; //1bit
  1894 + int8_t channel_configuration; //3bits
  1895 + int8_t original_or_copy; //1bit
  1896 + int8_t home; //1bit
  1897 +
  1898 + // adts_variable_header
  1899 + // 28bits
  1900 + int8_t copyright_identification_bit; //1bit
  1901 + int8_t copyright_identification_start; //1bit
  1902 + int16_t frame_length; //13bits
  1903 + int16_t adts_buffer_fullness; //11bits
  1904 + int8_t number_of_raw_data_blocks_in_frame; //2bits
  1905 +
  1906 + u_int8_t* raw_data;
  1907 + int size;
  1908 +
  1909 + TSAacAdts()
  1910 + {
  1911 + syncword = 0;
  1912 + ID = 0;
  1913 + layer = 0;
  1914 + protection_absent = 0;
  1915 + profile = 0;
  1916 + sampling_frequency_index = TSAacSampleFrequencyReserved0;
  1917 + private_bit = 0;
  1918 + channel_configuration = 0;
  1919 + original_or_copy = 0;
  1920 + home = 0;
  1921 + copyright_identification_bit = 0;
  1922 + copyright_identification_start = 0;
  1923 + frame_length = 0;
  1924 + adts_buffer_fullness = 0;
  1925 + number_of_raw_data_blocks_in_frame = 0;
  1926 +
  1927 + size = 0;
  1928 + raw_data = NULL;
  1929 + }
  1930 +
  1931 + u_int8_t at(int index)
  1932 + {
  1933 + if (index >= size) {
  1934 + return 0;
  1935 + }
  1936 + return raw_data[index];
  1937 + }
  1938 +
  1939 + int parse(TSMessage* msg, char*& p)
  1940 + {
  1941 + int ret = 0;
  1942 +
  1943 + srs_assert(p);
  1944 +
  1945 + char* start = p;
  1946 +
  1947 + // adts_fixed_header
  1948 + char* pp = (char*)&syncword;
  1949 + pp[1] = *p++;
  1950 + pp[0] = *p++;
  1951 +
  1952 + protection_absent = syncword & 0x01;
  1953 + layer = (syncword >> 1) & 0x03;
  1954 + ID = (syncword >> 3) & 0x01;
  1955 + syncword = (syncword >> 4) & 0x0FFF;
  1956 + if (syncword != 0xfff) {
  1957 + trace("ts+aac invalid sync word. expect 0xfff, actual %#x", syncword);
  1958 + return -1;
  1959 + }
  1960 +
  1961 + // adts_variable_header
  1962 + int64_t temp = 0;
  1963 + pp = (char*)&temp;
  1964 + pp[4] = *p++;
  1965 + pp[3] = *p++;
  1966 + pp[2] = *p++;
  1967 + pp[1] = *p++;
  1968 + pp[0] = *p++;
  1969 +
  1970 + number_of_raw_data_blocks_in_frame = temp & 0x03;
  1971 + temp = temp >> 2;
  1972 +
  1973 + adts_buffer_fullness = temp & 0x7FF;
  1974 + temp = temp >> 11;
  1975 +
  1976 + frame_length = temp & 0x1FFF;
  1977 + temp = temp >> 13;
  1978 +
  1979 + copyright_identification_start = temp & 0x01;
  1980 + temp = temp >> 1;
  1981 +
  1982 + copyright_identification_bit = temp & 0x01;
  1983 + temp = temp >> 1;
  1984 +
  1985 + // adts_fixed_header
  1986 + home = temp & 0x01;
  1987 + temp = temp >> 1;
  1988 +
  1989 + original_or_copy = temp & 0x01;
  1990 + temp = temp >> 1;
  1991 +
  1992 + channel_configuration = temp & 0x07;
  1993 + temp = temp >> 3;
  1994 +
  1995 + private_bit = temp & 0x01;
  1996 + temp = temp >> 1;
  1997 +
  1998 + sampling_frequency_index = (TSAacSampleFrequency)(temp & 0x0F);
  1999 + temp = temp >> 4;
  2000 +
  2001 + profile = temp & 0x03;
  2002 + temp = temp >> 2;
  2003 +
  2004 + if (!number_of_raw_data_blocks_in_frame) {
  2005 + // adts_error_check
  2006 + if (!protection_absent) {
  2007 + // crc_check
  2008 + trace("ts+aac TODO: crc_check.");
  2009 + }
  2010 + // raw_data_block
  2011 + raw_data = (u_int8_t*)p;
  2012 + size = frame_length - (p - start);
  2013 + p += size;
  2014 + } else {
  2015 + trace("ts+aac TODO: parse multiple blocks.");
  2016 + }
  2017 +
  2018 + return ret;
  2019 + }
2020 }; 2020 };
2021 2021
2022 class AacMuxer 2022 class AacMuxer
2023 { 2023 {
2024 public: 2024 public:
2025 - int fd;  
2026 - const char* file;  
2027 -  
2028 - AacMuxer()  
2029 - {  
2030 - file = NULL;  
2031 - fd = 0;  
2032 - }  
2033 -  
2034 - virtual ~AacMuxer()  
2035 - {  
2036 - if (fd > 0) {  
2037 - close(fd);  
2038 - }  
2039 - }  
2040 -  
2041 - int open(const char* _file)  
2042 - {  
2043 - file = _file;  
2044 - if ((fd = ::open(file, O_CREAT|O_WRONLY|O_TRUNC,  
2045 - S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH)) < 0  
2046 - ) {  
2047 - return -1;  
2048 - }  
2049 -  
2050 - return 0;  
2051 - }  
2052 -  
2053 - int write_audio(char* data, int size)  
2054 - {  
2055 - if (size > 0 && write(fd, data, size) != size) {  
2056 - return -1;  
2057 - }  
2058 -  
2059 - return 0;  
2060 - }  
2061 -  
2062 - int write_video(char* data, int size)  
2063 - {  
2064 - return 0;  
2065 - } 2025 + int fd;
  2026 + const char* file;
  2027 +
  2028 + AacMuxer()
  2029 + {
  2030 + file = NULL;
  2031 + fd = 0;
  2032 + }
  2033 +
  2034 + virtual ~AacMuxer()
  2035 + {
  2036 + if (fd > 0) {
  2037 + close(fd);
  2038 + }
  2039 + }
  2040 +
  2041 + int open(const char* _file)
  2042 + {
  2043 + file = _file;
  2044 + if ((fd = ::open(file, O_CREAT|O_WRONLY|O_TRUNC,
  2045 + S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH)) < 0
  2046 + ) {
  2047 + return -1;
  2048 + }
  2049 +
  2050 + return 0;
  2051 + }
  2052 +
  2053 + int write_audio(char* data, int size)
  2054 + {
  2055 + if (size > 0 && write(fd, data, size) != size) {
  2056 + return -1;
  2057 + }
  2058 +
  2059 + return 0;
  2060 + }
  2061 +
  2062 + int write_video(char* data, int size)
  2063 + {
  2064 + return 0;
  2065 + }
2066 }; 2066 };
2067 2067
2068 int consume(TSMessage* msg, AacMuxer* aac_muxer) 2068 int consume(TSMessage* msg, AacMuxer* aac_muxer)
2069 { 2069 {
2070 - int ret = 0;  
2071 -  
2072 - char* p = msg->packet_data;  
2073 - if (!p) {  
2074 - trace("ts+aac+h264 ignore empty message.");  
2075 - return ret;  
2076 - }  
2077 -  
2078 - char* last = msg->packet_data + msg->packet_data_size; 2070 + int ret = 0;
  2071 +
  2072 + char* p = msg->packet_data;
  2073 + if (!p) {
  2074 + trace("ts+aac+h264 ignore empty message.");
  2075 + return ret;
  2076 + }
  2077 +
  2078 + char* last = msg->packet_data + msg->packet_data_size;
2079 2079
2080 if (!msg->is_video()) { 2080 if (!msg->is_video()) {
2081 - // write AAC raw audio.  
2082 - if (aac_muxer && (ret = aac_muxer->write_audio((char*)msg->packet_data, msg->packet_data_size)) != 0) {  
2083 - return ret;  
2084 - }  
2085 -  
2086 - // parse AAC audio.  
2087 - int64_t dts = -1;  
2088 - while (p < last) {  
2089 - TSAacAdts aac;  
2090 - if ((ret = aac.parse(msg, p)) != 0) {  
2091 - return ret;  
2092 - }  
2093 - trace("ts+aac audio raw data parsed, size: %d, 0x%02x 0x%02x 0x%02x 0x%02x",  
2094 - aac.size, aac.at(0), aac.at(1), aac.at(2), aac.at(3));  
2095 -  
2096 - if (dts == -1) {  
2097 - dts = (msg->dts == 0)? msg->pts : msg->dts;  
2098 - } else {  
2099 - // see ffmpeg: avpriv_aac_parse_header  
2100 - // rdb = get_bits(gbc, 2); /* number_of_raw_data_blocks_in_frame */  
2101 - // hdr->samples = (rdb + 1) * 1024;  
2102 - int samples = (aac.number_of_raw_data_blocks_in_frame + 1) * 1024;  
2103 - static int sample_rates[] = {  
2104 - 96000, 88200, 64000, 48000, 44100, 32000,  
2105 - 24000, 22050, 16000, 12000, 11025, 8000,  
2106 - 1, 1, 1, 1  
2107 - };  
2108 - int sample_rate = sample_rates[aac.sampling_frequency_index];  
2109 -  
2110 - dts += samples * 90000 / sample_rate;  
2111 - }  
2112 -  
2113 - trace("ts+aac+h264+data %s pts:%"PRId64" dts:%"PRId64" size: %d",  
2114 - (msg->type == TSPidTypeVideo)? "video":"audio", dts, dts, aac.frame_length);  
2115 -  
2116 - // TODO: process audio.  
2117 - }  
2118 - } else {  
2119 - trace("ts+aac+h264+data %s pts:%"PRId64" dts:%"PRId64" size: %d",  
2120 - (msg->type == TSPidTypeVideo)? "video":"audio", msg->pts,  
2121 - (msg->dts == 0)? msg->pts : msg->dts, msg->packet_data_size);  
2122 - 2081 + // write AAC raw audio.
  2082 + if (aac_muxer && (ret = aac_muxer->write_audio((char*)msg->packet_data, msg->packet_data_size)) != 0) {
  2083 + return ret;
  2084 + }
  2085 +
  2086 + // parse AAC audio.
  2087 + int64_t dts = -1;
  2088 + while (p < last) {
  2089 + TSAacAdts aac;
  2090 + if ((ret = aac.parse(msg, p)) != 0) {
  2091 + return ret;
  2092 + }
  2093 + trace("ts+aac audio raw data parsed, size: %d, 0x%02x 0x%02x 0x%02x 0x%02x",
  2094 + aac.size, aac.at(0), aac.at(1), aac.at(2), aac.at(3));
  2095 +
  2096 + if (dts == -1) {
  2097 + dts = (msg->dts == 0)? msg->pts : msg->dts;
  2098 + } else {
  2099 + // see ffmpeg: avpriv_aac_parse_header
  2100 + // rdb = get_bits(gbc, 2); /* number_of_raw_data_blocks_in_frame */
  2101 + // hdr->samples = (rdb + 1) * 1024;
  2102 + int samples = (aac.number_of_raw_data_blocks_in_frame + 1) * 1024;
  2103 + static int sample_rates[] = {
  2104 + 96000, 88200, 64000, 48000, 44100, 32000,
  2105 + 24000, 22050, 16000, 12000, 11025, 8000,
  2106 + 1, 1, 1, 1
  2107 + };
  2108 + int sample_rate = sample_rates[aac.sampling_frequency_index];
  2109 +
  2110 + dts += samples * 90000 / sample_rate;
  2111 + }
  2112 +
  2113 + trace("ts+aac+h264+data %s pts:%"PRId64" dts:%"PRId64" size: %d",
  2114 + (msg->type == TSPidTypeVideo)? "video":"audio", dts, dts, aac.frame_length);
  2115 +
  2116 + // TODO: process audio.
  2117 + }
  2118 + } else {
  2119 + trace("ts+aac+h264+data %s pts:%"PRId64" dts:%"PRId64" size: %d",
  2120 + (msg->type == TSPidTypeVideo)? "video":"audio", msg->pts,
  2121 + (msg->dts == 0)? msg->pts : msg->dts, msg->packet_data_size);
  2122 +
2123 // parse H264 video. 2123 // parse H264 video.
2124 bool first = true; 2124 bool first = true;
2125 - while (p < last) {  
2126 - TSH264Codec h264;  
2127 - if ((ret = h264.parse(msg, last, p)) != 0) {  
2128 - return ret;  
2129 - }  
2130 - trace("ts+h264 video raw data parsed, size: %d, 0x%02x 0x%02x 0x%02x 0x%02x",  
2131 - h264.size, h264.at(0), h264.at(1), h264.at(2), h264.at(3));  
2132 -  
2133 - // first?  
2134 - if (!first) {  
2135 - continue;  
2136 - }  
2137 - first = false;  
2138 -  
2139 - // TODO: process video.  
2140 -  
2141 - // directly check the sequence header for test_22m.flv  
2142 - if (h264.at(0) == 0x67 && h264.at(1) == 0x00 && h264.at(2) == 0x1f && h264.at(3) == 0xac) {  
2143 - trace("ts+h264 directly find the sequence header for test_22m.flv");  
2144 - }  
2145 - // 7.3.1 NAL unit syntax, hls-mpeg-ts-iso13818-1.pdf, page 44  
2146 - char* pp = (char*)h264.raw_data;  
2147 - int8_t nal_unit_type = *pp++;  
2148 - int8_t nal_ref_idc = (nal_unit_type >> 5) & 0x03;  
2149 - nal_unit_type &= 0x1f;  
2150 -  
2151 - msg->nal_ref_idc = nal_ref_idc;  
2152 - msg->nal_unit_type = nal_unit_type;  
2153 -  
2154 - if (nal_ref_idc != 0) {  
2155 - trace("ts+h264 got an SPS or PPS.");  
2156 - }  
2157 - if (nal_unit_type == 7) {  
2158 - trace("ts+h264 got an SPS.");  
2159 - } else if (nal_unit_type == 5) {  
2160 - trace("ts+h264 got an Coded slice of an IDR picture.");  
2161 - } else if (nal_unit_type == 8) {  
2162 - trace("ts+h264 got an PPS.");  
2163 - } else if (nal_unit_type == 9) {  
2164 - trace("ts+h264 got an Picture delimiter.");  
2165 - int8_t pic_type = *pp++;  
2166 - pic_type = (pic_type >> 6) & 0x07;  
2167 - if (pic_type == 0) {  
2168 - trace("ts+h264 got an I picture.");  
2169 - } else if (pic_type == 1) {  
2170 - trace("ts+h264 got an I,P picture.");  
2171 - } else if (pic_type == 2) {  
2172 - trace("ts+h264 got an I,P,B picture.");  
2173 - } else if (pic_type == 3) {  
2174 - trace("ts+h264 got an SI picture.");  
2175 - } else if (pic_type == 4) {  
2176 - trace("ts+h264 got an SI,SP picture.");  
2177 - } else if (pic_type == 5) {  
2178 - trace("ts+h264 got an I,SI picture.");  
2179 - } else if (pic_type == 6) {  
2180 - trace("ts+h264 got an I,SI,P,SP picture.");  
2181 - } else if (pic_type == 7) {  
2182 - trace("ts+h264 got an I,SI,P,SP,B picture.");  
2183 - }  
2184 - } else {  
2185 - trace("ts+h264 got an unknown unit type: %d.", nal_unit_type);  
2186 - }  
2187 - } 2125 + while (p < last) {
  2126 + TSH264Codec h264;
  2127 + if ((ret = h264.parse(msg, last, p)) != 0) {
  2128 + return ret;
  2129 + }
  2130 + trace("ts+h264 video raw data parsed, size: %d, 0x%02x 0x%02x 0x%02x 0x%02x",
  2131 + h264.size, h264.at(0), h264.at(1), h264.at(2), h264.at(3));
  2132 +
  2133 + // first?
  2134 + if (!first) {
  2135 + continue;
  2136 + }
  2137 + first = false;
  2138 +
  2139 + // TODO: process video.
  2140 +
  2141 + // directly check the sequence header for test_22m.flv
  2142 + if (h264.at(0) == 0x67 && h264.at(1) == 0x00 && h264.at(2) == 0x1f && h264.at(3) == 0xac) {
  2143 + trace("ts+h264 directly find the sequence header for test_22m.flv");
  2144 + }
  2145 + // 7.3.1 NAL unit syntax, hls-mpeg-ts-iso13818-1.pdf, page 44
  2146 + char* pp = (char*)h264.raw_data;
  2147 + int8_t nal_unit_type = *pp++;
  2148 + int8_t nal_ref_idc = (nal_unit_type >> 5) & 0x03;
  2149 + nal_unit_type &= 0x1f;
  2150 +
  2151 + msg->nal_ref_idc = nal_ref_idc;
  2152 + msg->nal_unit_type = nal_unit_type;
  2153 +
  2154 + if (nal_ref_idc != 0) {
  2155 + trace("ts+h264 got an SPS or PPS.");
  2156 + }
  2157 + if (nal_unit_type == 7) {
  2158 + trace("ts+h264 got an SPS.");
  2159 + } else if (nal_unit_type == 5) {
  2160 + trace("ts+h264 got an Coded slice of an IDR picture.");
  2161 + } else if (nal_unit_type == 8) {
  2162 + trace("ts+h264 got an PPS.");
  2163 + } else if (nal_unit_type == 9) {
  2164 + trace("ts+h264 got an Picture delimiter.");
  2165 + int8_t pic_type = *pp++;
  2166 + pic_type = (pic_type >> 6) & 0x07;
  2167 + if (pic_type == 0) {
  2168 + trace("ts+h264 got an I picture.");
  2169 + } else if (pic_type == 1) {
  2170 + trace("ts+h264 got an I,P picture.");
  2171 + } else if (pic_type == 2) {
  2172 + trace("ts+h264 got an I,P,B picture.");
  2173 + } else if (pic_type == 3) {
  2174 + trace("ts+h264 got an SI picture.");
  2175 + } else if (pic_type == 4) {
  2176 + trace("ts+h264 got an SI,SP picture.");
  2177 + } else if (pic_type == 5) {
  2178 + trace("ts+h264 got an I,SI picture.");
  2179 + } else if (pic_type == 6) {
  2180 + trace("ts+h264 got an I,SI,P,SP picture.");
  2181 + } else if (pic_type == 7) {
  2182 + trace("ts+h264 got an I,SI,P,SP,B picture.");
  2183 + }
  2184 + } else {
  2185 + trace("ts+h264 got an unknown unit type: %d.", nal_unit_type);
  2186 + }
  2187 + }
2188 } 2188 }
2189 2189
2190 - return ret; 2190 + return ret;
2191 } 2191 }
2192 2192
2193 int main(int argc, char** argv) 2193 int main(int argc, char** argv)
@@ -2234,36 +2234,36 @@ int main(int argc, char** argv) @@ -2234,36 +2234,36 @@ int main(int argc, char** argv)
2234 2234
2235 // maybe need to parse multiple times for the PES_packet_length(0) packets. 2235 // maybe need to parse multiple times for the PES_packet_length(0) packets.
2236 while (p == start) { 2236 while (p == start) {
2237 - TSPacket pkt;  
2238 - TSMessage* msg = NULL;  
2239 - if ((ret = pkt.demux(&ctx, start, last, p, msg)) != 0) {  
2240 - trace("demuxer+read decode ts packet error. ret=%d", ret);  
2241 - return ret;  
2242 - }  
2243 -  
2244 - offset += nread;  
2245 - if (!msg) {  
2246 - continue;  
2247 - }  
2248 -  
2249 - if ((ret = consume(msg, &aac_muxer)) != 0) {  
2250 - trace("demuxer+consume parse and consume message failed. ret=%d", ret);  
2251 - return -1;  
2252 - }  
2253 -  
2254 - int64_t pts = msg->pts;  
2255 - int64_t dts = (msg->dts == 0)? msg->pts : msg->dts;  
2256 - int64_t pcr = msg->pcr;  
2257 - static int64_t last_pcr_dts = 0;  
2258 - trace("demuxer+report id=%d, type=%s, size=%d, dts=%d, pts=%d, cts=%d, pcr=%d, dts-pcr=%d, ref=%d, unit=%d, dts(diff-pcr)=%d",  
2259 - ctx.ts_packet_count, (msg->type == TSPidTypeVideo)? "video":"audio",  
2260 - msg->parsed_packet_size, dts, pts, pts - dts, pcr, pcr? dts - pcr : 0,  
2261 - msg->nal_ref_idc, msg->nal_unit_type, pcr? dts - last_pcr_dts: 0);  
2262 - if (pcr > 0) {  
2263 - last_pcr_dts = dts;  
2264 - }  
2265 -  
2266 - srs_freep(msg); 2237 + TSPacket pkt;
  2238 + TSMessage* msg = NULL;
  2239 + if ((ret = pkt.demux(&ctx, start, last, p, msg)) != 0) {
  2240 + trace("demuxer+read decode ts packet error. ret=%d", ret);
  2241 + return ret;
  2242 + }
  2243 +
  2244 + offset += nread;
  2245 + if (!msg) {
  2246 + continue;
  2247 + }
  2248 +
  2249 + if ((ret = consume(msg, &aac_muxer)) != 0) {
  2250 + trace("demuxer+consume parse and consume message failed. ret=%d", ret);
  2251 + return -1;
  2252 + }
  2253 +
  2254 + int64_t pts = msg->pts;
  2255 + int64_t dts = (msg->dts == 0)? msg->pts : msg->dts;
  2256 + int64_t pcr = msg->pcr;
  2257 + static int64_t last_pcr_dts = 0;
  2258 + trace("demuxer+report id=%d, type=%s, size=%d, dts=%d, pts=%d, cts=%d, pcr=%d, dts-pcr=%d, ref=%d, unit=%d, dts(diff-pcr)=%d",
  2259 + ctx.ts_packet_count, (msg->type == TSPidTypeVideo)? "video":"audio",
  2260 + msg->parsed_packet_size, dts, pts, pts - dts, pcr, pcr? dts - pcr : 0,
  2261 + msg->nal_ref_idc, msg->nal_unit_type, pcr? dts - last_pcr_dts: 0);
  2262 + if (pcr > 0) {
  2263 + last_pcr_dts = dts;
  2264 + }
  2265 +
  2266 + srs_freep(msg);
2267 } 2267 }
2268 } 2268 }
2269 2269
@@ -31,46 +31,46 @@ gcc srs_play.c ../../objs/lib/srs_librtmp.a -g -O0 -lstdc++ -o srs_play @@ -31,46 +31,46 @@ gcc srs_play.c ../../objs/lib/srs_librtmp.a -g -O0 -lstdc++ -o srs_play
31 31
32 int main(int argc, char** argv) 32 int main(int argc, char** argv)
33 { 33 {
34 - srs_rtmp_t rtmp;  
35 -  
36 - // packet data  
37 - int type, size;  
38 - u_int32_t timestamp = 0;  
39 - char* data;  
40 - 34 + srs_rtmp_t rtmp;
  35 +
  36 + // packet data
  37 + int type, size;
  38 + u_int32_t timestamp = 0;
  39 + char* data;
  40 +
41 printf("suck rtmp stream like rtmpdump\n"); 41 printf("suck rtmp stream like rtmpdump\n");
42 printf("srs(simple-rtmp-server) client librtmp library.\n"); 42 printf("srs(simple-rtmp-server) client librtmp library.\n");
43 printf("version: %d.%d.%d\n", srs_version_major(), srs_version_minor(), srs_version_revision()); 43 printf("version: %d.%d.%d\n", srs_version_major(), srs_version_minor(), srs_version_revision());
44 44
45 rtmp = srs_rtmp_create("rtmp://127.0.0.1:1935/live/livestream"); 45 rtmp = srs_rtmp_create("rtmp://127.0.0.1:1935/live/livestream");
46 46
47 - if (srs_simple_handshake(rtmp) != 0) {  
48 - printf("simple handshake failed.\n");  
49 - goto rtmp_destroy;  
50 - }  
51 - printf("simple handshake success\n"); 47 + if (srs_simple_handshake(rtmp) != 0) {
  48 + printf("simple handshake failed.\n");
  49 + goto rtmp_destroy;
  50 + }
  51 + printf("simple handshake success\n");
  52 +
  53 + if (srs_connect_app(rtmp) != 0) {
  54 + printf("connect vhost/app failed.\n");
  55 + goto rtmp_destroy;
  56 + }
  57 + printf("connect vhost/app success\n");
  58 +
  59 + if (srs_play_stream(rtmp) != 0) {
  60 + printf("play stream failed.\n");
  61 + goto rtmp_destroy;
  62 + }
  63 + printf("play stream success\n");
52 64
53 - if (srs_connect_app(rtmp) != 0) {  
54 - printf("connect vhost/app failed.\n");  
55 - goto rtmp_destroy;  
56 - }  
57 - printf("connect vhost/app success\n"); 65 + for (;;) {
  66 + if (srs_read_packet(rtmp, &type, &timestamp, &data, &size) != 0) {
  67 + goto rtmp_destroy;
  68 + }
  69 + printf("got packet: type=%s, time=%d, size=%d\n", srs_type2string(type), timestamp, size);
  70 +
  71 + free(data);
  72 + }
58 73
59 - if (srs_play_stream(rtmp) != 0) {  
60 - printf("play stream failed.\n");  
61 - goto rtmp_destroy;  
62 - }  
63 - printf("play stream success\n");  
64 -  
65 - for (;;) {  
66 - if (srs_read_packet(rtmp, &type, &timestamp, &data, &size) != 0) {  
67 - goto rtmp_destroy;  
68 - }  
69 - printf("got packet: type=%s, time=%d, size=%d\n", srs_type2string(type), timestamp, size);  
70 -  
71 - free(data);  
72 - }  
73 -  
74 rtmp_destroy: 74 rtmp_destroy:
75 srs_rtmp_destroy(rtmp); 75 srs_rtmp_destroy(rtmp);
76 76
@@ -32,51 +32,52 @@ gcc srs_publish.c ../../objs/lib/srs_librtmp.a -g -O0 -lstdc++ -o srs_publish @@ -32,51 +32,52 @@ gcc srs_publish.c ../../objs/lib/srs_librtmp.a -g -O0 -lstdc++ -o srs_publish
32 32
33 int main(int argc, char** argv) 33 int main(int argc, char** argv)
34 { 34 {
35 - srs_rtmp_t rtmp;  
36 -  
37 - // packet data  
38 - int type, size;  
39 - u_int32_t timestamp = 0;  
40 - char* data;  
41 - 35 + srs_rtmp_t rtmp;
  36 +
  37 + // packet data
  38 + int type, size;
  39 + u_int32_t timestamp = 0;
  40 + char* data;
  41 +
42 printf("publish rtmp stream to server like FMLE/FFMPEG/Encoder\n"); 42 printf("publish rtmp stream to server like FMLE/FFMPEG/Encoder\n");
43 printf("srs(simple-rtmp-server) client librtmp library.\n"); 43 printf("srs(simple-rtmp-server) client librtmp library.\n");
44 printf("version: %d.%d.%d\n", srs_version_major(), srs_version_minor(), srs_version_revision()); 44 printf("version: %d.%d.%d\n", srs_version_major(), srs_version_minor(), srs_version_revision());
45 45
46 rtmp = srs_rtmp_create("rtmp://127.0.0.1:1935/live/livestream"); 46 rtmp = srs_rtmp_create("rtmp://127.0.0.1:1935/live/livestream");
47 47
48 - if (srs_simple_handshake(rtmp) != 0) {  
49 - printf("simple handshake failed.\n");  
50 - goto rtmp_destroy;  
51 - }  
52 - printf("simple handshake success\n"); 48 + //if (srs_simple_handshake(rtmp) != 0) {
  49 + if (srs_complex_handshake(rtmp) != 0) {
  50 + printf("simple handshake failed.\n");
  51 + goto rtmp_destroy;
  52 + }
  53 + printf("simple handshake success\n");
  54 +
  55 + if (srs_connect_app(rtmp) != 0) {
  56 + printf("connect vhost/app failed.\n");
  57 + goto rtmp_destroy;
  58 + }
  59 + printf("connect vhost/app success\n");
  60 +
  61 + if (srs_publish_stream(rtmp) != 0) {
  62 + printf("publish stream failed.\n");
  63 + goto rtmp_destroy;
  64 + }
  65 + printf("publish stream success\n");
53 66
54 - if (srs_connect_app(rtmp) != 0) {  
55 - printf("connect vhost/app failed.\n");  
56 - goto rtmp_destroy;  
57 - }  
58 - printf("connect vhost/app success\n"); 67 + for (;;) {
  68 + type = SRS_RTMP_TYPE_VIDEO;
  69 + timestamp += 40;
  70 + size = 4096;
  71 + data = (char*)malloc(4096);
  72 +
  73 + if (srs_write_packet(rtmp, type, timestamp, data, size) != 0) {
  74 + goto rtmp_destroy;
  75 + }
  76 + printf("sent packet: type=%s, time=%d, size=%d\n", srs_type2string(type), timestamp, size);
  77 +
  78 + usleep(40 * 1000);
  79 + }
59 80
60 - if (srs_publish_stream(rtmp) != 0) {  
61 - printf("publish stream failed.\n");  
62 - goto rtmp_destroy;  
63 - }  
64 - printf("publish stream success\n");  
65 -  
66 - for (;;) {  
67 - type = SRS_RTMP_TYPE_VIDEO;  
68 - timestamp += 40;  
69 - size = 4096;  
70 - data = (char*)malloc(4096);  
71 -  
72 - if (srs_write_packet(rtmp, type, timestamp, data, size) != 0) {  
73 - goto rtmp_destroy;  
74 - }  
75 - printf("sent packet: type=%s, time=%d, size=%d\n", srs_type2string(type), timestamp, size);  
76 -  
77 - usleep(40 * 1000);  
78 - }  
79 -  
80 rtmp_destroy: 81 rtmp_destroy:
81 srs_rtmp_destroy(rtmp); 82 srs_rtmp_destroy(rtmp);
82 83
@@ -34,43 +34,7 @@ class ISrsProtocolReaderWriter; @@ -34,43 +34,7 @@ class ISrsProtocolReaderWriter;
34 class SrsComplexHandshake; 34 class SrsComplexHandshake;
35 class SrsHandshakeBytes; 35 class SrsHandshakeBytes;
36 36
37 -/**  
38 -* try complex handshake, if failed, fallback to simple handshake.  
39 -*/  
40 -class SrsSimpleHandshake  
41 -{  
42 -public:  
43 - SrsSimpleHandshake();  
44 - virtual ~SrsSimpleHandshake();  
45 -public:  
46 - /**  
47 - * simple handshake.  
48 - */  
49 - virtual int handshake_with_client(SrsHandshakeBytes* hs_bytes, ISrsProtocolReaderWriter* io);  
50 - virtual int handshake_with_server(SrsHandshakeBytes* hs_bytes, ISrsProtocolReaderWriter* io);  
51 -};  
52 -  
53 -/**  
54 -* rtmp complex handshake,  
55 -* @see also crtmp(crtmpserver) or librtmp,  
56 -* @see also: http://blog.csdn.net/win_lin/article/details/13006803  
57 -*/  
58 -class SrsComplexHandshake  
59 -{  
60 -public:  
61 - SrsComplexHandshake();  
62 - virtual ~SrsComplexHandshake();  
63 -public:  
64 - /**  
65 - * complex hanshake.  
66 - * @return user must:  
67 - * continue connect app if success,  
68 - * try simple handshake if error is ERROR_RTMP_TRY_SIMPLE_HS,  
69 - * otherwise, disconnect  
70 - */  
71 - virtual int handshake_with_client(SrsHandshakeBytes* hs_bytes, ISrsProtocolReaderWriter* io);  
72 - virtual int handshake_with_server(SrsHandshakeBytes* hs_bytes, ISrsProtocolReaderWriter* io);  
73 -}; 37 +#ifdef SRS_SSL
74 38
75 namespace srs 39 namespace srs
76 { 40 {
@@ -303,4 +267,44 @@ namespace srs @@ -303,4 +267,44 @@ namespace srs
303 int openssl_HMACsha256(const void* data, int data_size, const void* key, int key_size, void* digest); 267 int openssl_HMACsha256(const void* data, int data_size, const void* key, int key_size, void* digest);
304 } 268 }
305 269
  270 +#endif
  271 +
  272 +/**
  273 +* try complex handshake, if failed, fallback to simple handshake.
  274 +*/
  275 +class SrsSimpleHandshake
  276 +{
  277 +public:
  278 + SrsSimpleHandshake();
  279 + virtual ~SrsSimpleHandshake();
  280 +public:
  281 + /**
  282 + * simple handshake.
  283 + */
  284 + virtual int handshake_with_client(SrsHandshakeBytes* hs_bytes, ISrsProtocolReaderWriter* io);
  285 + virtual int handshake_with_server(SrsHandshakeBytes* hs_bytes, ISrsProtocolReaderWriter* io);
  286 +};
  287 +
  288 +/**
  289 +* rtmp complex handshake,
  290 +* @see also crtmp(crtmpserver) or librtmp,
  291 +* @see also: http://blog.csdn.net/win_lin/article/details/13006803
  292 +*/
  293 +class SrsComplexHandshake
  294 +{
  295 +public:
  296 + SrsComplexHandshake();
  297 + virtual ~SrsComplexHandshake();
  298 +public:
  299 + /**
  300 + * complex hanshake.
  301 + * @return user must:
  302 + * continue connect app if success,
  303 + * try simple handshake if error is ERROR_RTMP_TRY_SIMPLE_HS,
  304 + * otherwise, disconnect
  305 + */
  306 + virtual int handshake_with_client(SrsHandshakeBytes* hs_bytes, ISrsProtocolReaderWriter* io);
  307 + virtual int handshake_with_server(SrsHandshakeBytes* hs_bytes, ISrsProtocolReaderWriter* io);
  308 +};
  309 +
306 #endif 310 #endif
@@ -229,7 +229,7 @@ int SrsHandshakeBytes::read_s0s1s2(ISrsProtocolReaderWriter* io) @@ -229,7 +229,7 @@ int SrsHandshakeBytes::read_s0s1s2(ISrsProtocolReaderWriter* io)
229 229
230 ssize_t nsize; 230 ssize_t nsize;
231 231
232 - c0c1 = new char[3073]; 232 + s0s1s2 = new char[3073];
233 if ((ret = io->read_fully(s0s1s2, 3073, &nsize)) != ERROR_SUCCESS) { 233 if ((ret = io->read_fully(s0s1s2, 3073, &nsize)) != ERROR_SUCCESS) {
234 srs_warn("read s0s1s2 failed. ret=%d", ret); 234 srs_warn("read s0s1s2 failed. ret=%d", ret);
235 return ret; 235 return ret;