From 8437370c1e71debe6c3b54913a6caa80711a17a2 Mon Sep 17 00:00:00 2001 From: winlin Date: Wed, 8 Jun 2022 20:38:07 +0800 Subject: [PATCH] SRT: Refine packet error handler. --- trunk/src/app/srs_app_srt_conn.cpp | 19 ++++++++++++++----- trunk/src/app/srs_app_srt_source.cpp | 3 ++- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/trunk/src/app/srs_app_srt_conn.cpp b/trunk/src/app/srs_app_srt_conn.cpp index 8c024978f..144410ad9 100644 --- a/trunk/src/app/srs_app_srt_conn.cpp +++ b/trunk/src/app/srs_app_srt_conn.cpp @@ -312,6 +312,7 @@ srs_error_t SrsMpegtsSrtConn::playing() return err; } +// TODO: FIXME: It's not atomic and has risk between multiple source checking. srs_error_t SrsMpegtsSrtConn::acquire_publish() { srs_error_t err = srs_success; @@ -370,8 +371,6 @@ srs_error_t SrsMpegtsSrtConn::do_publishing() } pprint->elapse(); - - // reportable if (pprint->can_print()) { SrsSrtStat s; if ((err = s.fetch(srt_fd_, true)) != srs_success) { @@ -485,9 +484,19 @@ srs_error_t SrsMpegtsSrtConn::on_srt_packet(char* buf, int nb_buf) { srs_error_t err = srs_success; - // Check srt payload, mpegts must be N times of SRS_TS_PACKET_SIZE, and the first byte must be 0x47 - if ((nb_buf <= 0) || (nb_buf % SRS_TS_PACKET_SIZE != 0) || (buf[0] != 0x47)) { - return srs_error_new(ERROR_SRT_CONN, "invalid ts packet"); + // Ignore if invalid length. + if (nb_buf <= 0) { + return err; + } + + // Check srt payload, mpegts must be N times of SRS_TS_PACKET_SIZE + if ((nb_buf % SRS_TS_PACKET_SIZE) != 0) { + return srs_error_new(ERROR_SRT_CONN, "invalid ts packet len=%d", nb_buf); + } + + // Check srt payload, the first byte must be 0x47 + if (buf[0] != 0x47) { + return srs_error_new(ERROR_SRT_CONN, "invalid ts packet first=%#x", (uint8_t)buf[0]); } SrsSrtPacket* packet = new SrsSrtPacket(); diff --git a/trunk/src/app/srs_app_srt_source.cpp b/trunk/src/app/srs_app_srt_source.cpp index 78b1d7054..4d4f68a31 100644 --- a/trunk/src/app/srs_app_srt_source.cpp +++ b/trunk/src/app/srs_app_srt_source.cpp @@ -287,7 +287,8 @@ srs_error_t SrsRtmpFromSrtBridge::on_packet(SrsSrtPacket *pkt) SrsBuffer* stream = new SrsBuffer(p, SRS_TS_PACKET_SIZE); SrsAutoFree(SrsBuffer, stream); - // process each ts packet + // Process each ts packet. Note that the jitter of UDP may cause video glitch when packet loss or wrong seq. We + // don't handle it because SRT will, see tlpkdrop at https://github.com/ossrs/srs/wiki/v5_EN_SRTParams if ((err = ts_ctx_->decode(stream, this)) != srs_success) { srs_warn("parse ts packet err=%s", srs_error_desc(err).c_str()); srs_error_reset(err);