From 3c8e1f8ff70b755bc2d1da3719b9208e3add60f9 Mon Sep 17 00:00:00 2001 From: winlin Date: Mon, 28 Oct 2013 22:09:11 +0800 Subject: [PATCH] fix bug of extended-timestamp, support librtmp/ffmpeg publish --- trunk/src/core/srs_core_buffer.cpp | 7 ++++++- trunk/src/core/srs_core_error.hpp | 2 ++ trunk/src/core/srs_core_protocol.cpp | 23 +++++++++++++++++++++-- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/trunk/src/core/srs_core_buffer.cpp b/trunk/src/core/srs_core_buffer.cpp index ca1d283f9..f1dce357f 100755 --- a/trunk/src/core/srs_core_buffer.cpp +++ b/trunk/src/core/srs_core_buffer.cpp @@ -25,6 +25,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include #include +#include #define SOCKET_READ_SIZE 4096 @@ -62,7 +63,11 @@ int SrsBuffer::ensure_buffer_bytes(SrsSocket* skt, int required_size) { int ret = ERROR_SUCCESS; - srs_assert(required_size >= 0); + if (required_size < 0) { + ret = ERROR_SYSTEM_SIZE_NEGATIVE; + srs_error("size is negative. size=%d, ret=%d", required_size, ret); + return ret; + } while (size() < required_size) { char buffer[SOCKET_READ_SIZE]; diff --git a/trunk/src/core/srs_core_error.hpp b/trunk/src/core/srs_core_error.hpp index bac825066..427022592 100755 --- a/trunk/src/core/srs_core_error.hpp +++ b/trunk/src/core/srs_core_error.hpp @@ -64,11 +64,13 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #define ERROR_RTMP_CHUNK_SIZE 310 #define ERROR_RTMP_TRY_SIMPLE_HS 311 #define ERROR_RTMP_CH_SCHEMA 312 +#define ERROR_RTMP_PACKET_SIZE 313 #define ERROR_SYSTEM_STREAM_INIT 400 #define ERROR_SYSTEM_PACKET_INVALID 401 #define ERROR_SYSTEM_CLIENT_INVALID 402 #define ERROR_SYSTEM_ASSERT_FAILED 403 +#define ERROR_SYSTEM_SIZE_NEGATIVE 404 // see librtmp. // failed when open ssl create the dh diff --git a/trunk/src/core/srs_core_protocol.cpp b/trunk/src/core/srs_core_protocol.cpp index 05b2e690d..f42db4a24 100755 --- a/trunk/src/core/srs_core_protocol.cpp +++ b/trunk/src/core/srs_core_protocol.cpp @@ -827,6 +827,15 @@ int SrsProtocol::read_message_header(SrsChunkStream* chunk, char fmt, int bh_siz pp[0] = *p++; pp[3] = 0; + // if msg exists in cache, the size must not changed. + if (chunk->msg->size > 0 && chunk->msg->size != chunk->header.payload_length) { + ret = ERROR_RTMP_PACKET_SIZE; + srs_error("msg exists in chunk cache, " + "size=%d cannot change to %d, ret=%d", + chunk->msg->size, chunk->header.payload_length, ret); + return ret; + } + chunk->header.message_type = *p++; if (fmt == 0) { @@ -867,11 +876,21 @@ int SrsProtocol::read_message_header(SrsChunkStream* chunk, char fmt, int bh_siz return ret; } - char* pp = (char*)&chunk->header.timestamp; + // ffmpeg/librtmp may donot send this filed, need to detect the value. + // @see also: http://blog.csdn.net/win_lin/article/details/13363699 + int32_t timestamp = 0x00; + char* pp = (char*)×tamp; pp[3] = *p++; pp[2] = *p++; pp[1] = *p++; pp[0] = *p++; + + if (chunk->header.timestamp > RTMP_EXTENDED_TIMESTAMP && chunk->header.timestamp != timestamp) { + mh_size -= 4; + srs_verbose("ignore the 4bytes extended timestamp. mh_size=%d", mh_size); + } else { + chunk->header.timestamp = timestamp; + } srs_verbose("header read ext_time completed. time=%d", chunk->header.timestamp); } @@ -897,7 +916,7 @@ int SrsProtocol::read_message_payload(SrsChunkStream* chunk, int bh_size, int mh int ret = ERROR_SUCCESS; // empty message - if (chunk->header.payload_length == 0) { + if (chunk->header.payload_length <= 0) { // need erase the header in buffer. buffer->erase(bh_size + mh_size);