winlin

for #250, parse pes packet ok, ts message ok.

@@ -1539,7 +1539,8 @@ int TSPayloadPES::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t @@ -1539,7 +1539,8 @@ int TSPayloadPES::demux(TSContext* ctx, TSPacket* pkt, u_int8_t* start, u_int8_t
1539 // for (i = 0; i < PES_packet_length; i++) { 1539 // for (i = 0; i < PES_packet_length; i++) {
1540 // PES_packet_data_byte 1540 // PES_packet_data_byte
1541 // } 1541 // }
1542 - } else if (stream_id != PES_padding_stream) { 1542 + // TODO: FIXME: implements it.
  1543 + } else if (stream_id == PES_padding_stream) {
1543 // for (i = 0; i < PES_packet_length; i++) { 1544 // for (i = 0; i < PES_packet_length; i++) {
1544 // padding_byte 1545 // padding_byte
1545 // } 1546 // }
@@ -73,7 +73,8 @@ int SrsMpegtsOverUdp::on_udp_packet(sockaddr_in* from, char* buf, int nb_buf) @@ -73,7 +73,8 @@ int SrsMpegtsOverUdp::on_udp_packet(sockaddr_in* from, char* buf, int nb_buf)
73 73
74 // process each ts packet 74 // process each ts packet
75 if ((ret = on_ts_packet(stream)) != ERROR_SUCCESS) { 75 if ((ret = on_ts_packet(stream)) != ERROR_SUCCESS) {
76 - break; 76 + srs_warn("mpegts: ignore parse ts packet failed. ret=%d", ret);
  77 + continue;
77 } 78 }
78 srs_info("mpegts: parse ts packet completed"); 79 srs_info("mpegts: parse ts packet completed");
79 } 80 }
@@ -86,7 +87,7 @@ int SrsMpegtsOverUdp::on_ts_packet(SrsStream* stream) @@ -86,7 +87,7 @@ int SrsMpegtsOverUdp::on_ts_packet(SrsStream* stream)
86 { 87 {
87 int ret = ERROR_SUCCESS; 88 int ret = ERROR_SUCCESS;
88 89
89 - if ((ret = context->decode(stream)) != ERROR_SUCCESS) { 90 + if ((ret = context->decode(stream, this)) != ERROR_SUCCESS) {
90 srs_error("mpegts: decode ts packet failed. ret=%d", ret); 91 srs_error("mpegts: decode ts packet failed. ret=%d", ret);
91 return ret; 92 return ret;
92 } 93 }
@@ -94,4 +95,11 @@ int SrsMpegtsOverUdp::on_ts_packet(SrsStream* stream) @@ -94,4 +95,11 @@ int SrsMpegtsOverUdp::on_ts_packet(SrsStream* stream)
94 return ret; 95 return ret;
95 } 96 }
96 97
  98 +int SrsMpegtsOverUdp::on_ts_message(SrsTsMessage* msg)
  99 +{
  100 + int ret = ERROR_SUCCESS;
  101 + // TODO: FIXME: implements it.
  102 + return ret;
  103 +}
  104 +
97 #endif 105 #endif
@@ -39,10 +39,12 @@ class SrsConfDirective; @@ -39,10 +39,12 @@ class SrsConfDirective;
39 39
40 #ifdef SRS_AUTO_STREAM_CASTER 40 #ifdef SRS_AUTO_STREAM_CASTER
41 41
  42 +#include <srs_kernel_ts.hpp>
  43 +
42 /** 44 /**
43 * the mpegts over udp stream caster. 45 * the mpegts over udp stream caster.
44 */ 46 */
45 -class SrsMpegtsOverUdp 47 +class SrsMpegtsOverUdp : public ISrsTsHandler
46 { 48 {
47 private: 49 private:
48 SrsStream* stream; 50 SrsStream* stream;
@@ -67,6 +69,9 @@ private: @@ -67,6 +69,9 @@ private:
67 * the stream contains the ts packet to parse. 69 * the stream contains the ts packet to parse.
68 */ 70 */
69 virtual int on_ts_packet(SrsStream* stream); 71 virtual int on_ts_packet(SrsStream* stream);
  72 +// interface ISrsTsHandler
  73 +public:
  74 + virtual int on_ts_message(SrsTsMessage* msg);
70 }; 75 };
71 76
72 #endif 77 #endif
@@ -226,6 +226,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -226,6 +226,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
226 #define ERROR_STREAM_CASTER_TS_PSI 4016 226 #define ERROR_STREAM_CASTER_TS_PSI 4016
227 #define ERROR_STREAM_CASTER_TS_PAT 4017 227 #define ERROR_STREAM_CASTER_TS_PAT 4017
228 #define ERROR_STREAM_CASTER_TS_PMT 4018 228 #define ERROR_STREAM_CASTER_TS_PMT 4018
  229 +#define ERROR_STREAM_CASTER_TS_PSE 4019
229 230
230 /** 231 /**
231 * whether the error code is an system control error. 232 * whether the error code is an system control error.
@@ -402,6 +402,90 @@ SrsMpegtsFrame::SrsMpegtsFrame() @@ -402,6 +402,90 @@ SrsMpegtsFrame::SrsMpegtsFrame()
402 key = false; 402 key = false;
403 } 403 }
404 404
  405 +SrsTsChannel::SrsTsChannel()
  406 +{
  407 + pid = 0;
  408 + apply = SrsTsPidApplyReserved;
  409 + stream = SrsTsStreamReserved;
  410 + msg = NULL;
  411 +}
  412 +
  413 +SrsTsChannel::~SrsTsChannel()
  414 +{
  415 + srs_freep(msg);
  416 +}
  417 +
  418 +SrsTsMessage::SrsTsMessage()
  419 +{
  420 + payload = NULL;
  421 + clear();
  422 +}
  423 +
  424 +SrsTsMessage::~SrsTsMessage()
  425 +{
  426 + srs_freep(payload);
  427 +}
  428 +
  429 +int SrsTsMessage::dump(SrsStream* stream, int* pnb_bytes)
  430 +{
  431 + int ret = ERROR_SUCCESS;
  432 +
  433 + if (stream->empty()) {
  434 + return ret;
  435 + }
  436 +
  437 + // xB
  438 + int nb_bytes = stream->size() - stream->pos();
  439 + if (PES_packet_length > 0) {
  440 + nb_bytes = srs_min(nb_bytes, PES_packet_length - payload->length());
  441 + }
  442 +
  443 + if (nb_bytes > 0) {
  444 + if (!stream->require(nb_bytes)) {
  445 + ret = ERROR_STREAM_CASTER_TS_PSE;
  446 + srs_error("ts: dump PSE bytes failed, requires=%dB. ret=%d", nb_bytes, ret);
  447 + return ret;
  448 + }
  449 +
  450 + payload->append(stream->data() + stream->pos(), nb_bytes);
  451 + stream->skip(nb_bytes);
  452 + }
  453 +
  454 + *pnb_bytes = nb_bytes;
  455 +
  456 + return ret;
  457 +}
  458 +
  459 +bool SrsTsMessage::completed(int8_t payload_unit_start_indicator)
  460 +{
  461 + if (PES_packet_length == 0) {
  462 + return payload_unit_start_indicator;
  463 + }
  464 + return payload->length() >= PES_packet_length;
  465 +}
  466 +
  467 +bool SrsTsMessage::fresh()
  468 +{
  469 + return payload->length() == 0;
  470 +}
  471 +
  472 +void SrsTsMessage::clear()
  473 +{
  474 + dts = pts = 0;
  475 + continuity_counter = 0;
  476 + PES_packet_length = 0;
  477 + srs_freep(payload);
  478 + payload = new SrsSimpleBuffer();
  479 +}
  480 +
  481 +ISrsTsHandler::ISrsTsHandler()
  482 +{
  483 +}
  484 +
  485 +ISrsTsHandler::~ISrsTsHandler()
  486 +{
  487 +}
  488 +
405 SrsTsContext::SrsTsContext() 489 SrsTsContext::SrsTsContext()
406 { 490 {
407 } 491 }
@@ -416,7 +500,7 @@ SrsTsContext::~SrsTsContext() @@ -416,7 +500,7 @@ SrsTsContext::~SrsTsContext()
416 pids.clear(); 500 pids.clear();
417 } 501 }
418 502
419 -int SrsTsContext::decode(SrsStream* stream) 503 +int SrsTsContext::decode(SrsStream* stream, ISrsTsHandler* handler)
420 { 504 {
421 int ret = ERROR_SUCCESS; 505 int ret = ERROR_SUCCESS;
422 506
@@ -426,10 +510,21 @@ int SrsTsContext::decode(SrsStream* stream) @@ -426,10 +510,21 @@ int SrsTsContext::decode(SrsStream* stream)
426 SrsTsPacket* packet = new SrsTsPacket(this); 510 SrsTsPacket* packet = new SrsTsPacket(this);
427 SrsAutoFree(SrsTsPacket, packet); 511 SrsAutoFree(SrsTsPacket, packet);
428 512
429 - if ((ret = packet->decode(stream)) != ERROR_SUCCESS) { 513 + SrsTsMessage* msg = NULL;
  514 + if ((ret = packet->decode(stream, &msg)) != ERROR_SUCCESS) {
430 srs_error("mpegts: decode ts packet failed. ret=%d", ret); 515 srs_error("mpegts: decode ts packet failed. ret=%d", ret);
431 return ret; 516 return ret;
432 } 517 }
  518 +
  519 + if (!msg) {
  520 + continue;
  521 + }
  522 + SrsAutoFree(SrsTsMessage, msg);
  523 +
  524 + if ((ret = handler->on_ts_message(msg)) != ERROR_SUCCESS) {
  525 + srs_error("mpegts: handler ts message failed. ret=%d", ret);
  526 + return ret;
  527 + }
433 } 528 }
434 529
435 return ret; 530 return ret;
@@ -481,7 +576,7 @@ SrsTsPacket::~SrsTsPacket() @@ -481,7 +576,7 @@ SrsTsPacket::~SrsTsPacket()
481 srs_freep(payload); 576 srs_freep(payload);
482 } 577 }
483 578
484 -int SrsTsPacket::decode(SrsStream* stream) 579 +int SrsTsPacket::decode(SrsStream* stream, SrsTsMessage** ppmsg)
485 { 580 {
486 int ret = ERROR_SUCCESS; 581 int ret = ERROR_SUCCESS;
487 582
@@ -555,7 +650,7 @@ int SrsTsPacket::decode(SrsStream* stream) @@ -555,7 +650,7 @@ int SrsTsPacket::decode(SrsStream* stream)
555 } 650 }
556 } 651 }
557 652
558 - if (payload && (ret = payload->decode(stream)) != ERROR_SUCCESS) { 653 + if (payload && (ret = payload->decode(stream, ppmsg)) != ERROR_SUCCESS) {
559 srs_error("ts: demux payload failed. ret=%d", ret); 654 srs_error("ts: demux payload failed. ret=%d", ret);
560 return ret; 655 return ret;
561 } 656 }
@@ -821,7 +916,6 @@ SrsTsPayloadPES::SrsTsPayloadPES(SrsTsPacket* p) : SrsTsPayload(p) @@ -821,7 +916,6 @@ SrsTsPayloadPES::SrsTsPayloadPES(SrsTsPacket* p) : SrsTsPayload(p)
821 PES_extension_field = NULL; 916 PES_extension_field = NULL;
822 nb_stuffings = 0; 917 nb_stuffings = 0;
823 nb_bytes = 0; 918 nb_bytes = 0;
824 - bytes = NULL;  
825 nb_paddings = 0; 919 nb_paddings = 0;
826 } 920 }
827 921
@@ -830,12 +924,463 @@ SrsTsPayloadPES::~SrsTsPayloadPES() @@ -830,12 +924,463 @@ SrsTsPayloadPES::~SrsTsPayloadPES()
830 srs_freep(PES_private_data); 924 srs_freep(PES_private_data);
831 srs_freep(pack_field); 925 srs_freep(pack_field);
832 srs_freep(PES_extension_field); 926 srs_freep(PES_extension_field);
833 - srs_freep(bytes);  
834 } 927 }
835 928
836 -int SrsTsPayloadPES::decode(SrsStream* stream) 929 +int SrsTsPayloadPES::decode(SrsStream* stream, SrsTsMessage** ppmsg)
837 { 930 {
838 int ret = ERROR_SUCCESS; 931 int ret = ERROR_SUCCESS;
  932 +
  933 + // find the channel from chunk.
  934 + SrsTsChannel* channel = packet->context->get(packet->pid);
  935 + if (!channel) {
  936 + ret = ERROR_STREAM_CASTER_TS_PSE;
  937 + srs_error("ts: demux PES no channel for pid=%#x. ret=%d", packet->pid, ret);
  938 + return ret;
  939 + }
  940 +
  941 + // init msg.
  942 + SrsTsMessage* msg = channel->msg;
  943 + if (!msg) {
  944 + msg = new SrsTsMessage();
  945 + channel->msg = msg;
  946 + }
  947 +
  948 + // check when fresh, the payload_unit_start_indicator
  949 + // should be 1 for the fresh msg.
  950 + if (msg->fresh() && !packet->payload_unit_start_indicator) {
  951 + ret = ERROR_STREAM_CASTER_TS_PSE;
  952 + srs_error("ts: PES fresh packet length=%d, unit_start=%d. ret=%d",
  953 + msg->PES_packet_length, packet->payload_unit_start_indicator, ret);
  954 + return ret;
  955 + }
  956 +
  957 + // check when not fresh and PES_packet_length>0,
  958 + // the payload_unit_start_indicator should never be 1 when not completed.
  959 + if (!msg->fresh() && msg->PES_packet_length > 0
  960 + && packet->payload_unit_start_indicator
  961 + && !msg->completed(packet->payload_unit_start_indicator)
  962 + ) {
  963 + ret = ERROR_STREAM_CASTER_TS_PSE;
  964 + srs_error("ts: PES packet length=%d, payload=%d, unit_start=%d. ret=%d",
  965 + msg->PES_packet_length, msg->payload->length(),
  966 + packet->payload_unit_start_indicator, ret);
  967 +
  968 + // reparse current msg.
  969 + stream->skip(stream->pos() * -1);
  970 + msg->clear();
  971 + return ERROR_SUCCESS;
  972 + }
  973 +
  974 + // check the continuity counter
  975 + if (!msg->fresh()) {
  976 + // late-incoming or duplicated continuity, drop message.
  977 + // @remark check overflow, the counter plus 1 should greater when invalid.
  978 + if (msg->continuity_counter >= packet->continuity_counter
  979 + && ((msg->continuity_counter + 1) & 0x0f) > packet->continuity_counter
  980 + ) {
  981 + srs_warn("ts: drop PES %dB for duplicated continuity=%#x", msg->continuity_counter);
  982 + stream->skip(stream->size() - stream->pos());
  983 + return ret;
  984 + }
  985 +
  986 + // when got partially message, the continous count must be continuous, or drop it.
  987 + if (((msg->continuity_counter + 1) & 0x0f) != packet->continuity_counter) {
  988 + ret = ERROR_STREAM_CASTER_TS_PSE;
  989 + srs_error("ts: continuity must be continous, msg=%#x, packet=%#x. ret=%d",
  990 + msg->continuity_counter, packet->continuity_counter, ret);
  991 +
  992 + // reparse current msg.
  993 + stream->skip(stream->pos() * -1);
  994 + msg->clear();
  995 + return ERROR_SUCCESS;
  996 + }
  997 + }
  998 + msg->continuity_counter = packet->continuity_counter;
  999 +
  1000 + // for the PES_packet_length(0), reap when completed.
  1001 + if (!msg->fresh() && msg->completed(packet->payload_unit_start_indicator)) {
  1002 + // reap previous PES packet.
  1003 + *ppmsg = msg;
  1004 + channel->msg = NULL;
  1005 +
  1006 + // reparse current msg.
  1007 + stream->skip(stream->pos() * -1);
  1008 +
  1009 + return ret;
  1010 + }
  1011 +
  1012 + // contious packet, append bytes for unit start is 0
  1013 + if (!packet->payload_unit_start_indicator) {
  1014 + if ((ret = msg->dump(stream, &nb_bytes)) != ERROR_SUCCESS) {
  1015 + return ret;
  1016 + }
  1017 + }
  1018 +
  1019 + // when unit start, parse the fresh msg.
  1020 + if (packet->payload_unit_start_indicator) {
  1021 + // 6B fixed header.
  1022 + if (!stream->require(6)) {
  1023 + ret = ERROR_STREAM_CASTER_TS_PSE;
  1024 + srs_error("ts: demux PSE failed. ret=%d", ret);
  1025 + return ret;
  1026 + }
  1027 + // 3B
  1028 + packet_start_code_prefix = stream->read_3bytes();
  1029 + // 1B
  1030 + stream_id = stream->read_1bytes();
  1031 + // 2B
  1032 + PES_packet_length = stream->read_2bytes();
  1033 +
  1034 + // check the packet start prefix.
  1035 + packet_start_code_prefix &= 0xFFFFFF;
  1036 + if (packet_start_code_prefix != 0x01) {
  1037 + ret = ERROR_STREAM_CASTER_TS_PSE;
  1038 + srs_error("ts: demux PSE start code failed, expect=0x01, actual=%#x. ret=%d", packet_start_code_prefix, ret);
  1039 + return ret;
  1040 + }
  1041 + int pos_packet = stream->pos();
  1042 +
  1043 + // @remark SrsTsPESStreamIdAudio and SrsTsPESStreamIdVideo is not used here.
  1044 + SrsTsPESStreamId sid = (SrsTsPESStreamId)stream_id;
  1045 +
  1046 + if (sid != SrsTsPESStreamIdProgramStreamMap
  1047 + && sid != SrsTsPESStreamIdPaddingStream
  1048 + && sid != SrsTsPESStreamIdPrivateStream2
  1049 + && sid != SrsTsPESStreamIdEcmStream
  1050 + && sid != SrsTsPESStreamIdEmmStream
  1051 + && sid != SrsTsPESStreamIdProgramStreamDirectory
  1052 + && sid != SrsTsPESStreamIdDsmccStream
  1053 + && sid != SrsTsPESStreamIdH2221TypeE
  1054 + ) {
  1055 + // 3B flags.
  1056 + if (!stream->require(3)) {
  1057 + ret = ERROR_STREAM_CASTER_TS_PSE;
  1058 + srs_error("ts: demux PSE flags failed. ret=%d", ret);
  1059 + return ret;
  1060 + }
  1061 + // 1B
  1062 + original_or_copy = stream->read_1bytes();
  1063 + // 1B
  1064 + PES_extension_flag = stream->read_1bytes();
  1065 + // 1B
  1066 + PES_header_data_length = stream->read_1bytes();
  1067 + // position of header start.
  1068 + int pos_header = stream->pos();
  1069 +
  1070 + //int8_t const2bits = (original_or_copy >> 6) & 0x03;
  1071 + PES_scrambling_control = (original_or_copy >> 4) & 0x03;
  1072 + PES_priority = (original_or_copy >> 3) & 0x01;
  1073 + data_alignment_indicator = (original_or_copy >> 2) & 0x01;
  1074 + copyright = (original_or_copy >> 1) & 0x01;
  1075 + original_or_copy &= 0x01;
  1076 +
  1077 + PTS_DTS_flags = (PES_extension_flag >> 6) & 0x03;
  1078 + ESCR_flag = (PES_extension_flag >> 5) & 0x01;
  1079 + ES_rate_flag = (PES_extension_flag >> 4) & 0x01;
  1080 + DSM_trick_mode_flag = (PES_extension_flag >> 3) & 0x01;
  1081 + additional_copy_info_flag = (PES_extension_flag >> 2) & 0x01;
  1082 + PES_CRC_flag = (PES_extension_flag >> 1) & 0x01;
  1083 + PES_extension_flag &= 0x01;
  1084 +
  1085 + // check required together.
  1086 + int nb_required = 0;
  1087 + nb_required += (PTS_DTS_flags == 0x2)? 5:0;
  1088 + nb_required += (PTS_DTS_flags == 0x3)? 10:0;
  1089 + nb_required += ESCR_flag? 6:0;
  1090 + nb_required += ES_rate_flag? 3:0;
  1091 + nb_required += DSM_trick_mode_flag? 1:0;
  1092 + nb_required += additional_copy_info_flag? 1:0;
  1093 + nb_required += PES_CRC_flag? 2:0;
  1094 + nb_required += PES_extension_flag? 1:0;
  1095 + if (!stream->require(nb_required)) {
  1096 + ret = ERROR_STREAM_CASTER_TS_PSE;
  1097 + srs_error("ts: demux PSE payload failed. ret=%d", ret);
  1098 + return ret;
  1099 + }
  1100 +
  1101 + // 5B
  1102 + if (PTS_DTS_flags == 0x2) {
  1103 + if ((ret = decode_33bits_dts_pts(stream, &pts)) != ERROR_SUCCESS) {
  1104 + return ret;
  1105 + }
  1106 + dts = pts;
  1107 +
  1108 + // update the dts and pts of message.
  1109 + msg->dts = dts;
  1110 + msg->pts = pts;
  1111 + }
  1112 +
  1113 + // 10B
  1114 + if (PTS_DTS_flags == 0x3) {
  1115 + if ((ret = decode_33bits_dts_pts(stream, &pts)) != ERROR_SUCCESS) {
  1116 + return ret;
  1117 + }
  1118 + if ((ret = decode_33bits_dts_pts(stream, &dts)) != ERROR_SUCCESS) {
  1119 + return ret;
  1120 + }
  1121 +
  1122 + // check sync, the diff of dts and pts should never greater than 1s.
  1123 + if (dts - pts > 90000 || pts - dts > 90000) {
  1124 + srs_warn("ts: sync dts=%"PRId64", pts=%"PRId64, dts, pts);
  1125 + }
  1126 +
  1127 + // update the dts and pts of message.
  1128 + msg->dts = dts;
  1129 + msg->pts = pts;
  1130 + }
  1131 +
  1132 + // 6B
  1133 + if (ESCR_flag) {
  1134 + ESCR_extension = 0;
  1135 + ESCR_base = 0;
  1136 +
  1137 + stream->skip(6);
  1138 + srs_warn("ts: demux PES, ignore the escr.");
  1139 + }
  1140 +
  1141 + // 3B
  1142 + if (ES_rate_flag) {
  1143 + ES_rate = stream->read_3bytes();
  1144 +
  1145 + ES_rate = ES_rate >> 1;
  1146 + ES_rate &= 0x3FFFFF;
  1147 + }
  1148 +
  1149 + // 1B
  1150 + if (DSM_trick_mode_flag) {
  1151 + trick_mode_control = stream->read_1bytes();
  1152 +
  1153 + trick_mode_value = trick_mode_control & 0x1f;
  1154 + trick_mode_control = (trick_mode_control >> 5) & 0x03;
  1155 + }
  1156 +
  1157 + // 1B
  1158 + if (additional_copy_info_flag) {
  1159 + additional_copy_info = stream->read_1bytes();
  1160 +
  1161 + additional_copy_info &= 0x7f;
  1162 + }
  1163 +
  1164 + // 2B
  1165 + if (PES_CRC_flag) {
  1166 + previous_PES_packet_CRC = stream->read_2bytes();
  1167 + }
  1168 +
  1169 + // 1B
  1170 + if (PES_extension_flag) {
  1171 + PES_extension_flag_2 = stream->read_1bytes();
  1172 +
  1173 + PES_private_data_flag = (PES_extension_flag_2 >> 7) & 0x01;
  1174 + pack_header_field_flag = (PES_extension_flag_2 >> 6) & 0x01;
  1175 + program_packet_sequence_counter_flag = (PES_extension_flag_2 >> 5) & 0x01;
  1176 + P_STD_buffer_flag = (PES_extension_flag_2 >> 4) & 0x01;
  1177 + PES_extension_flag_2 &= PES_extension_flag_2 & 0x01;
  1178 +
  1179 + nb_required = 0;
  1180 + nb_required += PES_private_data_flag? 16:0;
  1181 + nb_required += pack_header_field_flag? 1:0; // 1+x bytes.
  1182 + nb_required += program_packet_sequence_counter_flag? 2:0;
  1183 + nb_required += P_STD_buffer_flag? 2:0;
  1184 + nb_required += PES_extension_flag_2? 1:0; // 1+x bytes.
  1185 + if (!stream->require(nb_required)) {
  1186 + ret = ERROR_STREAM_CASTER_TS_PSE;
  1187 + srs_error("ts: demux PSE ext payload failed. ret=%d", ret);
  1188 + return ret;
  1189 + }
  1190 +
  1191 + // 16B
  1192 + if (PES_private_data_flag) {
  1193 + srs_freep(PES_private_data);
  1194 + PES_private_data = new char[16];
  1195 + stream->read_bytes(PES_private_data, 16);
  1196 + }
  1197 +
  1198 + // (1+x)B
  1199 + if (pack_header_field_flag) {
  1200 + pack_field_length = stream->read_1bytes();
  1201 + if (pack_field_length > 0) {
  1202 + // the adjust required bytes.
  1203 + nb_required = nb_required - 16 - 1 + pack_field_length;
  1204 + if (!stream->require(nb_required)) {
  1205 + ret = ERROR_STREAM_CASTER_TS_PSE;
  1206 + srs_error("ts: demux PSE ext pack failed. ret=%d", ret);
  1207 + return ret;
  1208 + }
  1209 + srs_freep(pack_field);
  1210 + pack_field = new char[pack_field_length];
  1211 + stream->read_bytes(pack_field, pack_field_length);
  1212 + }
  1213 + }
  1214 +
  1215 + // 2B
  1216 + if (program_packet_sequence_counter_flag) {
  1217 + program_packet_sequence_counter = stream->read_1bytes();
  1218 + program_packet_sequence_counter &= 0x7f;
  1219 +
  1220 + original_stuff_length = stream->read_1bytes();
  1221 + MPEG1_MPEG2_identifier = (original_stuff_length >> 6) & 0x01;
  1222 + original_stuff_length &= 0x3f;
  1223 + }
  1224 +
  1225 + // 2B
  1226 + if (P_STD_buffer_flag) {
  1227 + P_STD_buffer_size = stream->read_2bytes();
  1228 +
  1229 + // '01'
  1230 + //int8_t const2bits = (P_STD_buffer_scale >>14) & 0x03;
  1231 +
  1232 + P_STD_buffer_scale = (P_STD_buffer_scale >>13) & 0x01;
  1233 + P_STD_buffer_size &= 0x1FFF;
  1234 + }
  1235 +
  1236 + // (1+x)B
  1237 + if (PES_extension_flag_2) {
  1238 + PES_extension_field_length = stream->read_1bytes();
  1239 + PES_extension_field_length &= 0x07;
  1240 +
  1241 + if (PES_extension_field_length > 0) {
  1242 + if (!stream->require(PES_extension_field_length)) {
  1243 + ret = ERROR_STREAM_CASTER_TS_PSE;
  1244 + srs_error("ts: demux PSE ext field failed. ret=%d", ret);
  1245 + return ret;
  1246 + }
  1247 + srs_freep(PES_extension_field);
  1248 + PES_extension_field = new char[PES_extension_field_length];
  1249 + stream->read_bytes(PES_extension_field, PES_extension_field_length);
  1250 + }
  1251 + }
  1252 + }
  1253 +
  1254 + // stuffing_byte
  1255 + nb_stuffings = PES_header_data_length - (stream->pos() - pos_header);
  1256 + if (nb_stuffings > 0) {
  1257 + if (!stream->require(nb_stuffings)) {
  1258 + ret = ERROR_STREAM_CASTER_TS_PSE;
  1259 + srs_error("ts: demux PSE stuffings failed. ret=%d", ret);
  1260 + return ret;
  1261 + }
  1262 + stream->skip(nb_stuffings);
  1263 + }
  1264 +
  1265 + // PES_packet_data_byte, page58.
  1266 + // the packet size contains the header size.
  1267 + // The number of PES_packet_data_bytes, N, is specified by the
  1268 + // PES_packet_length field. N shall be equal to the value
  1269 + // indicated in the PES_packet_length minus the number of bytes
  1270 + // between the last byte of the PES_packet_length field and the
  1271 + // first PES_packet_data_byte.
  1272 + /**
  1273 + * when actual packet length > 0xffff(65535),
  1274 + * which exceed the max u_int16_t packet length,
  1275 + * use 0 packet length, the next unit start indicates the end of packet.
  1276 + */
  1277 + if (PES_packet_length > 0) {
  1278 + int nb_packet = PES_packet_length - (stream->pos() - pos_packet);
  1279 + msg->PES_packet_length = srs_max(0, nb_packet);
  1280 + }
  1281 +
  1282 + // xB
  1283 + if ((ret = msg->dump(stream, &nb_bytes)) != ERROR_SUCCESS) {
  1284 + return ret;
  1285 + }
  1286 + } else if (sid == SrsTsPESStreamIdProgramStreamMap
  1287 + || sid == SrsTsPESStreamIdPrivateStream2
  1288 + || sid == SrsTsPESStreamIdEcmStream
  1289 + || sid == SrsTsPESStreamIdEmmStream
  1290 + || sid == SrsTsPESStreamIdProgramStreamDirectory
  1291 + || sid == SrsTsPESStreamIdDsmccStream
  1292 + || sid == SrsTsPESStreamIdH2221TypeE
  1293 + ) {
  1294 + // for (i = 0; i < PES_packet_length; i++) {
  1295 + // PES_packet_data_byte
  1296 + // }
  1297 +
  1298 + // xB
  1299 + if ((ret = msg->dump(stream, &nb_bytes)) != ERROR_SUCCESS) {
  1300 + return ret;
  1301 + }
  1302 + } else if (sid == SrsTsPESStreamIdPaddingStream) {
  1303 + // for (i = 0; i < PES_packet_length; i++) {
  1304 + // padding_byte
  1305 + // }
  1306 + nb_paddings = stream->size() - stream->pos();
  1307 + stream->skip(nb_paddings);
  1308 + srs_info("ts: drop %dB padding bytes", nb_paddings);
  1309 + } else {
  1310 + int nb_drop = stream->size() - stream->pos();
  1311 + stream->skip(nb_drop);
  1312 + srs_warn("ts: drop the pes packet %dB for stream_id=%#x", nb_drop, stream_id);
  1313 + }
  1314 + }
  1315 +
  1316 + // check msg, reap when completed.
  1317 + if (msg->completed(packet->payload_unit_start_indicator)) {
  1318 + *ppmsg = msg;
  1319 + channel->msg = NULL;
  1320 + srs_info("ts: reap msg for completed.");
  1321 + }
  1322 +
  1323 + return ret;
  1324 +}
  1325 +
  1326 +int SrsTsPayloadPES::decode_33bits_dts_pts(SrsStream* stream, int64_t* pv)
  1327 +{
  1328 + int ret = ERROR_SUCCESS;
  1329 +
  1330 + if (!stream->require(5)) {
  1331 + ret = ERROR_STREAM_CASTER_TS_PSE;
  1332 + srs_error("ts: demux PSE dts/pts failed. ret=%d", ret);
  1333 + return ret;
  1334 + }
  1335 +
  1336 + // decode the 33bits schema.
  1337 + // ===========1B
  1338 + // 4bits const maybe '0001', '0010' or '0011'.
  1339 + // 3bits DTS/PTS [32..30]
  1340 + // 1bit const '1'
  1341 + int64_t dts_pts_30_32 = stream->read_1bytes();
  1342 + if ((dts_pts_30_32 & 0x01) != 0x01) {
  1343 + ret = ERROR_STREAM_CASTER_TS_PSE;
  1344 + srs_error("ts: demux PSE dts/pts 30-32 failed. ret=%d", ret);
  1345 + return ret;
  1346 + }
  1347 + // @remark, we donot check the high 4bits, maybe '0001', '0010' or '0011'.
  1348 + // so we just ensure the high 4bits is not 0x00.
  1349 + if (((dts_pts_30_32 >> 4) & 0x0f) == 0x00) {
  1350 + ret = ERROR_STREAM_CASTER_TS_PSE;
  1351 + srs_error("ts: demux PSE dts/pts 30-32 failed. ret=%d", ret);
  1352 + return ret;
  1353 + }
  1354 + dts_pts_30_32 = (dts_pts_30_32 >> 1) & 0x07;
  1355 +
  1356 + // ===========2B
  1357 + // 15bits DTS/PTS [29..15]
  1358 + // 1bit const '1'
  1359 + int64_t dts_pts_15_29 = stream->read_2bytes();
  1360 + if ((dts_pts_15_29 & 0x01) != 0x01) {
  1361 + ret = ERROR_STREAM_CASTER_TS_PSE;
  1362 + srs_error("ts: demux PSE dts/pts 15-29 failed. ret=%d", ret);
  1363 + return ret;
  1364 + }
  1365 + dts_pts_15_29 = (dts_pts_15_29 >> 1) & 0x7fff;
  1366 +
  1367 + // ===========2B
  1368 + // 15bits DTS/PTS [14..0]
  1369 + // 1bit const '1'
  1370 + int64_t dts_pts_0_14 = stream->read_2bytes();
  1371 + if ((dts_pts_0_14 & 0x01) != 0x01) {
  1372 + ret = ERROR_STREAM_CASTER_TS_PSE;
  1373 + srs_error("ts: demux PSE dts/pts 0-14 failed. ret=%d", ret);
  1374 + return ret;
  1375 + }
  1376 + dts_pts_0_14 = (dts_pts_0_14 >> 1) & 0x7fff;
  1377 +
  1378 + int64_t v = 0x00;
  1379 + v |= (dts_pts_30_32 << 30) & 0x1c0000000LL;
  1380 + v |= (dts_pts_15_29 << 15) & 0x3fff8000LL;
  1381 + v |= dts_pts_0_14 & 0x7fff;
  1382 + *pv = v;
  1383 +
839 return ret; 1384 return ret;
840 } 1385 }
841 1386
@@ -849,7 +1394,7 @@ SrsTsPayloadPSI::~SrsTsPayloadPSI() @@ -849,7 +1394,7 @@ SrsTsPayloadPSI::~SrsTsPayloadPSI()
849 { 1394 {
850 } 1395 }
851 1396
852 -int SrsTsPayloadPSI::decode(SrsStream* stream) 1397 +int SrsTsPayloadPSI::decode(SrsStream* stream, SrsTsMessage** /*ppmsg*/)
853 { 1398 {
854 int ret = ERROR_SUCCESS; 1399 int ret = ERROR_SUCCESS;
855 1400
@@ -45,6 +45,7 @@ class SrsCodecSample; @@ -45,6 +45,7 @@ class SrsCodecSample;
45 class SrsSimpleBuffer; 45 class SrsSimpleBuffer;
46 class SrsTsAdaptationField; 46 class SrsTsAdaptationField;
47 class SrsTsPayload; 47 class SrsTsPayload;
  48 +class SrsTsMessage;
48 49
49 // Transport Stream packets are 188 bytes in length. 50 // Transport Stream packets are 188 bytes in length.
50 #define SRS_TS_PACKET_SIZE 188 51 #define SRS_TS_PACKET_SIZE 188
@@ -180,6 +181,65 @@ struct SrsTsChannel @@ -180,6 +181,65 @@ struct SrsTsChannel
180 int pid; 181 int pid;
181 SrsTsPidApply apply; 182 SrsTsPidApply apply;
182 SrsTsStream stream; 183 SrsTsStream stream;
  184 + SrsTsMessage* msg;
  185 +
  186 + SrsTsChannel();
  187 + virtual ~SrsTsChannel();
  188 +};
  189 +
  190 +/**
  191 +* the media audio/video message parsed from PES packet.
  192 +*/
  193 +class SrsTsMessage
  194 +{
  195 +public:
  196 + int64_t dts;
  197 + int64_t pts;
  198 + u_int16_t PES_packet_length;
  199 + u_int8_t continuity_counter;
  200 + SrsSimpleBuffer* payload;
  201 +public:
  202 + SrsTsMessage();
  203 + virtual ~SrsTsMessage();
  204 +public:
  205 + /**
  206 + * dumps all bytes in stream to ts message.
  207 + */
  208 + virtual int dump(SrsStream* stream, int* pnb_bytes);
  209 + /**
  210 + * whether ts message is completed to reap.
  211 + * @param payload_unit_start_indicator whether new ts message start.
  212 + * PES_packet_length is 0, the payload_unit_start_indicator=1 to reap ts message.
  213 + * PES_packet_length > 0, the payload.length() == PES_packet_length to reap ts message.
  214 + * @remark when PES_packet_length>0, the payload_unit_start_indicator should never be 1 when not completed.
  215 + * @remark when fresh, the payload_unit_start_indicator should be 1.
  216 + */
  217 + virtual bool completed(int8_t payload_unit_start_indicator);
  218 + /**
  219 + * whether the message is fresh.
  220 + */
  221 + virtual bool fresh();
  222 + /**
  223 + * clear current message.
  224 + */
  225 + virtual void clear();
  226 +};
  227 +
  228 +/**
  229 +* the ts message handler.
  230 +*/
  231 +class ISrsTsHandler
  232 +{
  233 +public:
  234 + ISrsTsHandler();
  235 + virtual ~ISrsTsHandler();
  236 +public:
  237 + /**
  238 + * when ts context got message, use handler to process it.
  239 + * @param msg the ts msg, user should never free it.
  240 + * @return an int error code.
  241 + */
  242 + virtual int on_ts_message(SrsTsMessage* msg) = 0;
183 }; 243 };
184 244
185 /** 245 /**
@@ -195,9 +255,10 @@ public: @@ -195,9 +255,10 @@ public:
195 public: 255 public:
196 /** 256 /**
197 * the stream contains only one ts packet. 257 * the stream contains only one ts packet.
  258 + * @param handler the ts message handler to process the msg.
198 * @remark we will consume all bytes in stream. 259 * @remark we will consume all bytes in stream.
199 */ 260 */
200 - virtual int decode(SrsStream* stream); 261 + virtual int decode(SrsStream* stream, ISrsTsHandler* handler);
201 public: 262 public:
202 /** 263 /**
203 * get the pid apply, the parsed pid. 264 * get the pid apply, the parsed pid.
@@ -237,7 +298,7 @@ public: @@ -237,7 +298,7 @@ public:
237 * Transport Stream packets that carry PES packets (refer to 2.4.3.6) or PSI data (refer to 2.4.4). 298 * Transport Stream packets that carry PES packets (refer to 2.4.3.6) or PSI data (refer to 2.4.4).
238 * 299 *
239 * When the payload of the Transport Stream packet contains PES packet data, the payload_unit_start_indicator has the 300 * When the payload of the Transport Stream packet contains PES packet data, the payload_unit_start_indicator has the
240 - * following significance: a '1' indicates that the payload of this Transport Stream packet will commence with the first byte 301 + * following significance: a '1' indicates that the payload of this Transport Stream packet will commence(start) with the first byte
241 * of a PES packet and a '0' indicates no PES packet shall start in this Transport Stream packet. If the 302 * of a PES packet and a '0' indicates no PES packet shall start in this Transport Stream packet. If the
242 * payload_unit_start_indicator is set to '1', then one and only one PES packet starts in this Transport Stream packet. This 303 * payload_unit_start_indicator is set to '1', then one and only one PES packet starts in this Transport Stream packet. This
243 * also applies to private streams of stream_type 6 (refer to Table 2-29). 304 * also applies to private streams of stream_type 6 (refer to Table 2-29).
@@ -293,7 +354,7 @@ public: @@ -293,7 +354,7 @@ public:
293 * 354 *
294 * In Transport Streams, duplicate packets may be sent as two, and only two, consecutive Transport Stream packets of the 355 * In Transport Streams, duplicate packets may be sent as two, and only two, consecutive Transport Stream packets of the
295 * same PID. The duplicate packets shall have the same continuity_counter value as the original packet and the 356 * same PID. The duplicate packets shall have the same continuity_counter value as the original packet and the
296 - * adaptation_field_control field shall be equal to '01' or '11'. In duplicate packets each byte of the original packet shall be 357 + * adaptation_field_control field shall be equal to '01'(payload only) or '11'(both). In duplicate packets each byte of the original packet shall be
297 * duplicated, with the exception that in the program clock reference fields, if present, a valid value shall be encoded. 358 * duplicated, with the exception that in the program clock reference fields, if present, a valid value shall be encoded.
298 * 359 *
299 * The continuity_counter in a particular Transport Stream packet is continuous when it differs by a positive value of one 360 * The continuity_counter in a particular Transport Stream packet is continuous when it differs by a positive value of one
@@ -312,7 +373,7 @@ public: @@ -312,7 +373,7 @@ public:
312 SrsTsPacket(SrsTsContext* c); 373 SrsTsPacket(SrsTsContext* c);
313 virtual ~SrsTsPacket(); 374 virtual ~SrsTsPacket();
314 public: 375 public:
315 - virtual int decode(SrsStream* stream); 376 + virtual int decode(SrsStream* stream, SrsTsMessage** ppmsg);
316 }; 377 };
317 378
318 /** 379 /**
@@ -658,7 +719,64 @@ public: @@ -658,7 +719,64 @@ public:
658 SrsTsPayload(SrsTsPacket* p); 719 SrsTsPayload(SrsTsPacket* p);
659 virtual ~SrsTsPayload(); 720 virtual ~SrsTsPayload();
660 public: 721 public:
661 - virtual int decode(SrsStream* stream) = 0; 722 + virtual int decode(SrsStream* stream, SrsTsMessage** ppmsg) = 0;
  723 +};
  724 +
  725 +/**
  726 +* the stream_id of PES payload of ts packet.
  727 +* Table 2-18 – Stream_id assignments, hls-mpeg-ts-iso13818-1.pdf, page 52.
  728 +*/
  729 +enum SrsTsPESStreamId
  730 +{
  731 + // program_stream_map
  732 + SrsTsPESStreamIdProgramStreamMap = 0xbc, // 0b10111100
  733 + // private_stream_1
  734 + SrsTsPESStreamIdPrivateStream1 = 0xbd, // 0b10111101
  735 + // padding_stream
  736 + SrsTsPESStreamIdPaddingStream = 0xbe, // 0b10111110
  737 + // private_stream_2
  738 + SrsTsPESStreamIdPrivateStream2 = 0xbf, // 0b10111111
  739 +
  740 + // 110x xxxx
  741 + // ISO/IEC 13818-3 or ISO/IEC 11172-3 or ISO/IEC 13818-7 or ISO/IEC
  742 + // 14496-3 audio stream number x xxxx
  743 + // (stream_id>>5)&0x07 == SrsTsPESStreamIdAudio
  744 + SrsTsPESStreamIdAudio = 0x06, // 0b110
  745 +
  746 + // 1110 xxxx
  747 + // ITU-T Rec. H.262 | ISO/IEC 13818-2 or ISO/IEC 11172-2 or ISO/IEC
  748 + // 14496-2 video stream number xxxx
  749 + // (stream_id>>4)&0x0f == SrsTsPESStreamIdVideo
  750 + SrsTsPESStreamIdVideo = 0x0e, // 0b1110
  751 +
  752 + // ECM_stream
  753 + SrsTsPESStreamIdEcmStream = 0xf0, // 0b11110000
  754 + // EMM_stream
  755 + SrsTsPESStreamIdEmmStream = 0xf1, // 0b11110001
  756 + // DSMCC_stream
  757 + SrsTsPESStreamIdDsmccStream = 0xf2, // 0b11110010
  758 + // 13522_stream
  759 + SrsTsPESStreamId13522Stream = 0xf3, // 0b11110011
  760 + // H_222_1_type_A
  761 + SrsTsPESStreamIdH2221TypeA = 0xf4, // 0b11110100
  762 + // H_222_1_type_B
  763 + SrsTsPESStreamIdH2221TypeB = 0xf5, // 0b11110101
  764 + // H_222_1_type_C
  765 + SrsTsPESStreamIdH2221TypeC = 0xf6, // 0b11110110
  766 + // H_222_1_type_D
  767 + SrsTsPESStreamIdH2221TypeD = 0xf7, // 0b11110111
  768 + // H_222_1_type_E
  769 + SrsTsPESStreamIdH2221TypeE = 0xf8, // 0b11111000
  770 + // ancillary_stream
  771 + SrsTsPESStreamIdAncillaryStream = 0xf9, // 0b11111001
  772 + // SL_packetized_stream
  773 + SrsTsPESStreamIdSlPacketizedStream = 0xfa, // 0b11111010
  774 + // FlexMux_stream
  775 + SrsTsPESStreamIdFlexMuxStream = 0xfb, // 0b11111011
  776 + // reserved data stream
  777 + // 1111 1100 … 1111 1110
  778 + // program_stream_directory
  779 + SrsTsPESStreamIdProgramStreamDirectory = 0xff, // 0b11111111
662 }; 780 };
663 781
664 /** 782 /**
@@ -774,7 +892,7 @@ public: @@ -774,7 +892,7 @@ public:
774 */ 892 */
775 u_int8_t PES_header_data_length; //8bits 893 u_int8_t PES_header_data_length; //8bits
776 894
777 - // 8B 895 + // 5B
778 /** 896 /**
779 * Presentation times shall be related to decoding times as follows: The PTS is a 33-bit 897 * Presentation times shall be related to decoding times as follows: The PTS is a 33-bit
780 * number coded in three separate fields. It indicates the time of presentation, tp n (k), in the system target decoder of a 898 * number coded in three separate fields. It indicates the time of presentation, tp n (k), in the system target decoder of a
@@ -782,15 +900,37 @@ public: @@ -782,15 +900,37 @@ public:
782 * frequency divided by 300 (yielding 90 kHz). The presentation time is derived from the PTS according to equation 2-11 900 * frequency divided by 300 (yielding 90 kHz). The presentation time is derived from the PTS according to equation 2-11
783 * below. Refer to 2.7.4 for constraints on the frequency of coding presentation timestamps. 901 * below. Refer to 2.7.4 for constraints on the frequency of coding presentation timestamps.
784 */ 902 */
  903 + // ===========1B
  904 + // 4bits const
  905 + // 3bits PTS [32..30]
  906 + // 1bit const '1'
  907 + // ===========2B
  908 + // 15bits PTS [29..15]
  909 + // 1bit const '1'
  910 + // ===========2B
  911 + // 15bits PTS [14..0]
  912 + // 1bit const '1'
785 int64_t pts; // 33bits 913 int64_t pts; // 33bits
  914 +
  915 + // 5B
786 /** 916 /**
787 * The DTS is a 33-bit number coded in three separate fields. It indicates the decoding time, 917 * The DTS is a 33-bit number coded in three separate fields. It indicates the decoding time,
788 * td n (j), in the system target decoder of an access unit j of elementary stream n. The value of DTS is specified in units of 918 * td n (j), in the system target decoder of an access unit j of elementary stream n. The value of DTS is specified in units of
789 * the period of the system clock frequency divided by 300 (yielding 90 kHz). 919 * the period of the system clock frequency divided by 300 (yielding 90 kHz).
790 */ 920 */
  921 + // ===========1B
  922 + // 4bits const
  923 + // 3bits DTS [32..30]
  924 + // 1bit const '1'
  925 + // ===========2B
  926 + // 15bits DTS [29..15]
  927 + // 1bit const '1'
  928 + // ===========2B
  929 + // 15bits DTS [14..0]
  930 + // 1bit const '1'
791 int64_t dts; // 33bits 931 int64_t dts; // 33bits
792 932
793 - // 8B 933 + // 6B
794 /** 934 /**
795 * The elementary stream clock reference is a 42-bit field coded in two parts. The first 935 * The elementary stream clock reference is a 42-bit field coded in two parts. The first
796 * part, ESCR_base, is a 33-bit field whose value is given by ESCR_base(i), as given in equation 2-14. The second part, 936 * part, ESCR_base, is a 33-bit field whose value is given by ESCR_base(i), as given in equation 2-14. The second part,
@@ -798,8 +938,19 @@ public: @@ -798,8 +938,19 @@ public:
798 * intended time of arrival of the byte containing the last bit of the ESCR_base at the input of the PES-STD for PES streams 938 * intended time of arrival of the byte containing the last bit of the ESCR_base at the input of the PES-STD for PES streams
799 * (refer to 2.5.2.4). 939 * (refer to 2.5.2.4).
800 */ 940 */
801 - int16_t ESCR_extension; //9bits 941 + // 2bits reserved
  942 + // 3bits ESCR_base[32..30]
  943 + // 1bit const '1'
  944 + // 15bits ESCR_base[29..15]
  945 + // 1bit const '1'
  946 + // 15bits ESCR_base[14..0]
  947 + // 1bit const '1'
  948 + // 9bits ESCR_extension
  949 + // 1bit const '1'
802 int64_t ESCR_base; //33bits 950 int64_t ESCR_base; //33bits
  951 + int16_t ESCR_extension; //9bits
  952 +
  953 + // 3B
803 /** 954 /**
804 * The ES_rate field is a 22-bit unsigned integer specifying the rate at which the 955 * The ES_rate field is a 22-bit unsigned integer specifying the rate at which the
805 * system target decoder receives bytes of the PES packet in the case of a PES stream. The ES_rate is valid in the PES 956 * system target decoder receives bytes of the PES packet in the case of a PES stream. The ES_rate is valid in the PES
@@ -808,6 +959,9 @@ public: @@ -808,6 +959,9 @@ public:
808 * ES_rate is used to define the time of arrival of bytes at the input of a P-STD for PES streams defined in 2.5.2.4. The 959 * ES_rate is used to define the time of arrival of bytes at the input of a P-STD for PES streams defined in 2.5.2.4. The
809 * value encoded in the ES_rate field may vary from PES_packet to PES_packet. 960 * value encoded in the ES_rate field may vary from PES_packet to PES_packet.
810 */ 961 */
  962 + // 1bit const '1'
  963 + // 22bits ES_rate
  964 + // 1bit const '1'
811 int32_t ES_rate; //22bits 965 int32_t ES_rate; //22bits
812 966
813 // 1B 967 // 1B
@@ -877,7 +1031,7 @@ public: @@ -877,7 +1031,7 @@ public:
877 /** 1031 /**
878 * This is an 8-bit field which indicates the length, in bytes, of the pack_header_field(). 1032 * This is an 8-bit field which indicates the length, in bytes, of the pack_header_field().
879 */ 1033 */
880 - int8_t pack_field_length; //8bits 1034 + u_int8_t pack_field_length; //8bits
881 char* pack_field; //[pack_field_length] bytes 1035 char* pack_field; //[pack_field_length] bytes
882 1036
883 // 2B 1037 // 2B
@@ -928,7 +1082,7 @@ public: @@ -928,7 +1082,7 @@ public:
928 * This is a 7-bit field which specifies the length, in bytes, of the data following this field in 1082 * This is a 7-bit field which specifies the length, in bytes, of the data following this field in
929 * the PES extension field up to and including any reserved bytes. 1083 * the PES extension field up to and including any reserved bytes.
930 */ 1084 */
931 - int8_t PES_extension_field_length; //7bits 1085 + u_int8_t PES_extension_field_length; //7bits
932 char* PES_extension_field; //[PES_extension_field_length] bytes 1086 char* PES_extension_field; //[PES_extension_field_length] bytes
933 1087
934 // NB 1088 // NB
@@ -953,7 +1107,6 @@ public: @@ -953,7 +1107,6 @@ public:
953 * PES_packet_data_byte field are user definable and will not be specified by ITU-T | ISO/IEC in the future. 1107 * PES_packet_data_byte field are user definable and will not be specified by ITU-T | ISO/IEC in the future.
954 */ 1108 */
955 int nb_bytes; 1109 int nb_bytes;
956 - char* bytes;  
957 1110
958 // NB 1111 // NB
959 /** 1112 /**
@@ -964,7 +1117,9 @@ public: @@ -964,7 +1117,9 @@ public:
964 SrsTsPayloadPES(SrsTsPacket* p); 1117 SrsTsPayloadPES(SrsTsPacket* p);
965 virtual ~SrsTsPayloadPES(); 1118 virtual ~SrsTsPayloadPES();
966 public: 1119 public:
967 - virtual int decode(SrsStream* stream); 1120 + virtual int decode(SrsStream* stream, SrsTsMessage** ppmsg);
  1121 +private:
  1122 + virtual int decode_33bits_dts_pts(SrsStream* stream, int64_t* pv);
968 }; 1123 };
969 1124
970 /** 1125 /**
@@ -1020,7 +1175,7 @@ public: @@ -1020,7 +1175,7 @@ public:
1020 SrsTsPayloadPSI(SrsTsPacket* p); 1175 SrsTsPayloadPSI(SrsTsPacket* p);
1021 virtual ~SrsTsPayloadPSI(); 1176 virtual ~SrsTsPayloadPSI();
1022 public: 1177 public:
1023 - virtual int decode(SrsStream* stream); 1178 + virtual int decode(SrsStream* stream, SrsTsMessage** ppmsg);
1024 protected: 1179 protected:
1025 virtual int psi_decode(SrsStream* stream) = 0; 1180 virtual int psi_decode(SrsStream* stream) = 0;
1026 }; 1181 };