From 64a8dfb09a42b72169ba6b416b50466a23858ced Mon Sep 17 00:00:00 2001 From: winlin Date: Mon, 31 Mar 2014 10:04:44 +0800 Subject: [PATCH] refine rtmp send protocol, use the same cache for header --- trunk/src/core/srs_core.hpp | 2 +- trunk/src/rtmp/srs_protocol_rtmp_stack.cpp | 17 ++++++----------- trunk/src/rtmp/srs_protocol_rtmp_stack.hpp | 6 +++--- 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/trunk/src/core/srs_core.hpp b/trunk/src/core/srs_core.hpp index 3b3e96ca6..f6ce1ae74 100644 --- a/trunk/src/core/srs_core.hpp +++ b/trunk/src/core/srs_core.hpp @@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // current release version #define VERSION_MAJOR "0" #define VERSION_MINOR "9" -#define VERSION_REVISION "39" +#define VERSION_REVISION "40" #define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION // server info. #define RTMP_SIG_SRS_KEY "srs" diff --git a/trunk/src/rtmp/srs_protocol_rtmp_stack.cpp b/trunk/src/rtmp/srs_protocol_rtmp_stack.cpp index 318ca6a94..0d272f7fe 100644 --- a/trunk/src/rtmp/srs_protocol_rtmp_stack.cpp +++ b/trunk/src/rtmp/srs_protocol_rtmp_stack.cpp @@ -424,12 +424,10 @@ int SrsProtocol::send_message(ISrsMessage* msg) // always write the header event payload is empty. do { // generate the header. - char* pheader = NULL; - int header_size = 0; + char* pheader = out_header_cache; if (p == (char*)msg->payload) { // write new chunk stream header, fmt is 0 - pheader = out_header_fmt0; *pheader++ = 0x00 | (msg->get_perfer_cid() & 0x3F); // chunk message header, 11 bytes @@ -470,12 +468,8 @@ int SrsProtocol::send_message(ISrsMessage* msg) *pheader++ = pp[1]; *pheader++ = pp[0]; } - - header_size = pheader - out_header_fmt0; - pheader = out_header_fmt0; } else { // write no message header chunk stream, fmt is 3 - pheader = out_header_fmt3; *pheader++ = 0xC0 | (msg->get_perfer_cid() & 0x3F); // chunk extended timestamp header, 0 or 4 bytes, big-endian @@ -499,9 +493,6 @@ int SrsProtocol::send_message(ISrsMessage* msg) *pheader++ = pp[1]; *pheader++ = pp[0]; } - - header_size = pheader - out_header_fmt3; - pheader = out_header_fmt3; } // sendout header and payload by writev. @@ -509,9 +500,13 @@ int SrsProtocol::send_message(ISrsMessage* msg) int payload_size = msg->size - (p - (char*)msg->payload); payload_size = srs_min(payload_size, out_chunk_size); + // always has header + int header_size = pheader - out_header_cache; + srs_assert(header_size > 0); + // send by writev iovec iov[2]; - iov[0].iov_base = pheader; + iov[0].iov_base = out_header_cache; iov[0].iov_len = header_size; iov[1].iov_base = p; iov[1].iov_len = payload_size; diff --git a/trunk/src/rtmp/srs_protocol_rtmp_stack.hpp b/trunk/src/rtmp/srs_protocol_rtmp_stack.hpp index 0d778754f..ea339afd6 100644 --- a/trunk/src/rtmp/srs_protocol_rtmp_stack.hpp +++ b/trunk/src/rtmp/srs_protocol_rtmp_stack.hpp @@ -106,7 +106,8 @@ class ISrsMessage; * 4bytes timestamp header, * that is, 1+4=5bytes. */ -#define RTMP_MAX_FMT3_HEADER_SIZE 5 +// always use fmt0 as cache. +//#define RTMP_MAX_FMT3_HEADER_SIZE 5 /** * the protocol provides the rtmp-message-protocol services, @@ -141,8 +142,7 @@ private: AckWindowSize in_ack_size; // peer out private: - char out_header_fmt0[RTMP_MAX_FMT0_HEADER_SIZE]; - char out_header_fmt3[RTMP_MAX_FMT3_HEADER_SIZE]; + char out_header_cache[RTMP_MAX_FMT0_HEADER_SIZE]; int32_t out_chunk_size; public: /**