From bb7cd6af3a09c90875caa75c5867e13a6a9c95ea Mon Sep 17 00:00:00 2001 From: "Alex.CR" Date: Mon, 18 Oct 2021 15:03:47 +0800 Subject: [PATCH] SRT: Solve mpegts demux assert bug (#2670) v4.0.180 * solve mpegts demux bug * remove assert * add error log in mpegts demux * sovle compile problem Co-authored-by: shiwei --- trunk/doc/CHANGELOG.md | 1 + trunk/src/core/srs_core_version4.hpp | 2 +- trunk/src/srt/ts_demux.cpp | 51 +++++++++++++++------------- 3 files changed, 30 insertions(+), 24 deletions(-) diff --git a/trunk/doc/CHANGELOG.md b/trunk/doc/CHANGELOG.md index 3487bf051d..bd72a493af 100644 --- a/trunk/doc/CHANGELOG.md +++ b/trunk/doc/CHANGELOG.md @@ -8,6 +8,7 @@ The changelog for SRS. ## SRS 4.0 Changelog +* v4.0, 2021-10-18, Merge [#2670](https://github.com/ossrs/srs/pull/2670): SRT: Solve mpegts demux assert bug. v4.0.180 * v4.0, 2021-10-16, Merge [#2665](https://github.com/ossrs/srs/pull/2665): API: Fix the same 'client_id' error when asynchronous call. v4.0.179 * v4.0, 2021-10-13, Merge [#2671](https://github.com/ossrs/srs/pull/2671): SRT: Pes error when mpegts demux in srt. v4.0.178 * v4.0, 2021-10-12, Merge [#2550](https://github.com/ossrs/srs/pull/2550): API use publish params. v4.0.177 diff --git a/trunk/src/core/srs_core_version4.hpp b/trunk/src/core/srs_core_version4.hpp index e314bca758..61bdbb9272 100644 --- a/trunk/src/core/srs_core_version4.hpp +++ b/trunk/src/core/srs_core_version4.hpp @@ -9,6 +9,6 @@ #define VERSION_MAJOR 4 #define VERSION_MINOR 0 -#define VERSION_REVISION 179 +#define VERSION_REVISION 180 #endif diff --git a/trunk/src/srt/ts_demux.cpp b/trunk/src/srt/ts_demux.cpp index 6ea25a810c..92d055e442 100644 --- a/trunk/src/srt/ts_demux.cpp +++ b/trunk/src/srt/ts_demux.cpp @@ -5,7 +5,7 @@ // #include "ts_demux.hpp" -#include +#include "srt_log.hpp" #include ts_demux::ts_demux():_data_total(0) @@ -41,11 +41,6 @@ int ts_demux::decode_unit(unsigned char* data_p, std::string key_path, TS_DATA_C pos++; npos = pos; - //printf("ts header(0x%02x) payload_unit_start_indicator:%d, pid:%d, adaptation_field_control:%d, pos:%d\r\n", - // ts_header_info._sync_byte, - // ts_header_info._payload_unit_start_indicator, ts_header_info._PID, - // ts_header_info._adaptation_field_control, pos); - adaptation_field* field_p = &(ts_header_info._adaptation_field_info); // adaptation field // 0x01 No adaptation_field, payload only @@ -153,8 +148,16 @@ int ts_demux::decode_unit(unsigned char* data_p, std::string key_path, TS_DATA_C _pat._section_number = data_p[pos]; pos++; _pat._last_section_number = data_p[pos]; - assert(_pat._table_id == 0x00); - assert((188 - npos) > (_pat._section_length+3)); // PAT = section_length + 3 + + if (_pat._table_id != 0x00) { + srt_log_error("pat table id(0x%02x) error, it must be 0x00", _pat._table_id); + return -1; + } + // PAT = section_length + 3 + if((188 - npos) <= (_pat._section_length + 3)) { + srt_log_error("pat _section_length(%d) error, the left len:%d", _pat._section_length, (188 - npos)); + return -1; + } pos++; _pat._pid_vec.clear(); for (;pos+4 <= _pat._section_length-5-4+9 + npos;) { // 4:CRC, 5:follow section_length item rpos + 4(following unit length) section_length + 9(above field and unit_start_first_byte ) @@ -167,13 +170,11 @@ int ts_demux::decode_unit(unsigned char* data_p, std::string key_path, TS_DATA_C if (pid_info._program_number == 0) { // // network_PID 13 uimsbf pid_info._network_id = (data_p[pos]<<8|data_p[pos+1])&0x1FFF; - //printf("#### network id:%d.\r\n", pid_info._network_id); pos += 2; } else { // // program_map_PID 13 uimsbf pid_info._pid = (data_p[pos]<<8|data_p[pos+1])&0x1FFF; - //printf("#### pmt id:%d.\r\n", pid_info._pid); pos += 2; } _pat._pid_vec.push_back(pid_info); @@ -217,7 +218,12 @@ int ts_demux::decode_unit(unsigned char* data_p, std::string key_path, TS_DATA_C //reserved 4 bslbf _pmt._program_info_length = ((data_p[pos]<<8)|data_p[pos+1])&0x0FFF;//program_info_length 12 uimsbf pos += 2; - assert(_pmt._table_id==0x02); // 0x02, // TS_program_map_section + + //0x02, // TS_program_map_section + if (_pmt._table_id != 0x02) { + srt_log_error("pmt tableid(0x%02x) error, it must be 0x02", _pmt._table_id) + return -1; + } memcpy(_pmt._dscr, data_p+pos, _pmt._program_info_length); // for (i = 0; i < N; i++) { // descriptor() @@ -250,7 +256,6 @@ int ts_demux::decode_unit(unsigned char* data_p, std::string key_path, TS_DATA_C pos += descriptor_length; } // save program_number(stream num) elementary_PID(PES PID) stream_type(stream codec) - //printf("pmt pid:%d, streamtype:%d, pos:%d\r\n", pid_info._elementary_PID, pid_info._stream_type, pos); _pmt._stream_pid_vec.push_back(pid_info); _pmt._pid2steamtype.insert(std::make_pair((unsigned short)pid_info._elementary_PID, pid_info._stream_type)); } @@ -274,8 +279,8 @@ int ts_demux::decode_unit(unsigned char* data_p, std::string key_path, TS_DATA_C on_callback(callback, _last_pid, key_path, _last_dts, _last_pts); int ret = pes_parse(data_p+npos, npos, &ret_data_p, ret_size, dts, pts); - assert(ret <= 188); if (ret > 188) { + srt_log_error("pes length(%d) error", ret); return -1; } @@ -291,9 +296,6 @@ int ts_demux::decode_unit(unsigned char* data_p, std::string key_path, TS_DATA_C } } } - //if(!isFound){ - // printf("unknown PID = %X \n", ts_header_info._PID); - //} } } @@ -380,13 +382,15 @@ int ts_demux::pes_parse(unsigned char* p, size_t npos, pos += 3; int stream_id = p[pos]; //stream_id 8 uimsbf pos++; - //printf("pes parse %02x %02x.\r\n", p[pos], p[pos+1]); + int PES_packet_length = ((unsigned int)p[pos]<<8)|p[pos+1]; //PES_packet_length 16 uimsbf (void)PES_packet_length; pos += 2; - //printf("pes parse packet_start_code_prefix:%d, npos:%lu, PES_packet_length:%d, stream_id:%d.\r\n", - // packet_start_code_prefix, npos, PES_packet_length, stream_id); - assert(0x00000001 == packet_start_code_prefix); + + if (0x00000001 != packet_start_code_prefix) { + srt_log_error("pes packet start code prefix(%06x) error, it must be 0x00 00 01", packet_start_code_prefix); + return 255; + } if (stream_id != 188//program_stream_map 1011 1100 && stream_id != 190//padding_stream 1011 1110 && stream_id != 191//private_stream_2 1011 1111 @@ -397,6 +401,10 @@ int ts_demux::pes_parse(unsigned char* p, size_t npos, && stream_id != 248//ITU-T Rec. H.222.1 type E stream 1111 1000 ) { + if (0x80 != (p[pos] & 0xc0)) { + srt_log_error("the first 2 bits:0x%02x error, it must be 0x80.", (p[pos] & 0xc0)); + return 255; + } //skip 2bits//'10' 2 bslbf int PES_scrambling_control = (p[pos]&30)>>4; //PES_scrambling_control 2 bslbf (void)PES_scrambling_control; @@ -563,9 +571,6 @@ int ts_demux::pes_parse(unsigned char* p, size_t npos, // } *ret_pp = p+pos; ret_size = 188-(npos+pos); - //printf("pes parse body size:%lu, data:0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x, dts:%lu(%lu), pts:%lu(%lu)\r\n", - // ret_size, p[pos], p[pos+1], p[pos+2], p[pos+3], p[pos+4], p[pos+5], - // dts, dts/90, pts, pts/90); } else if ( stream_id == 188//program_stream_map 1011 1100 BC || stream_id == 191//private_stream_2 1011 1111 BF