From 74621618ff06a85a72c4ab939685ae02cf5de0aa Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Tue, 23 Apr 2019 12:16:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96http/rtsp=E6=8E=A8=E6=B5=81?= =?UTF-8?q?=E5=99=A8/rtmp=E6=8E=A8=E6=B5=81=E5=99=A8=E5=86=85=E5=AD=98?= =?UTF-8?q?=E5=8D=A0=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Http/HttpClient.cpp | 8 ++++++++ src/Rtmp/RtmpPusher.cpp | 3 +++ src/Rtsp/RtspPusher.cpp | 2 ++ 3 files changed, 13 insertions(+) diff --git a/src/Http/HttpClient.cpp b/src/Http/HttpClient.cpp index 9cb316d7..71081a3f 100644 --- a/src/Http/HttpClient.cpp +++ b/src/Http/HttpClient.cpp @@ -115,6 +115,9 @@ void HttpClient::onConnect(const SockException &ex) { return; } + //先假设http客户端只会接收一点点数据(只接受http头,节省内存) + _sock->setReadBuffer(std::make_shared(1 * 1024)); + _totalBodySize = 0; _recvedBodySize = 0; HttpRequestSplitter::reset(); @@ -155,6 +158,9 @@ int64_t HttpClient::onRecvHeader(const char *data, uint64_t len) { } if(_parser["Transfer-Encoding"] == "chunked"){ + //我们认为这种情况下后面应该有大量的数据过来,加大接收缓存提高性能 + _sock->setReadBuffer(std::make_shared(256 * 1024)); + //如果Transfer-Encoding字段等于chunked,则认为后续的content是不限制长度的 _totalBodySize = -1; _chunkedSplitter = std::make_shared([this](const char *data,uint64_t len){ @@ -179,6 +185,8 @@ int64_t HttpClient::onRecvHeader(const char *data, uint64_t len) { //但是由于我们没必要等content接收完毕才回调onRecvContent(因为这样浪费内存并且要多次拷贝数据) //所以返回-1代表我们接下来分段接收content _recvedBodySize = 0; + //根据_totalBodySize设置接收缓存大小 + _sock->setReadBuffer(std::make_shared(MAX(_totalBodySize + 1,256 * 1024))); return -1; } diff --git a/src/Rtmp/RtmpPusher.cpp b/src/Rtmp/RtmpPusher.cpp index d64438f7..bbe5a27a 100644 --- a/src/Rtmp/RtmpPusher.cpp +++ b/src/Rtmp/RtmpPusher.cpp @@ -130,6 +130,9 @@ void RtmpPusher::onConnect(const SockException &err){ onPublishResult(err); return; } + //推流器不需要多大的接收缓存,节省内存占用 + _sock->setReadBuffer(std::make_shared(1 * 1024)); + weak_ptr weakSelf = dynamic_pointer_cast(shared_from_this()); startClientSession([weakSelf](){ auto strongSelf=weakSelf.lock(); diff --git a/src/Rtsp/RtspPusher.cpp b/src/Rtsp/RtspPusher.cpp index 17f2ae11..4b96de81 100644 --- a/src/Rtsp/RtspPusher.cpp +++ b/src/Rtsp/RtspPusher.cpp @@ -139,6 +139,8 @@ void RtspPusher::onConnect(const SockException &err) { onPublishResult(err); return; } + //推流器不需要多大的接收缓存,节省内存占用 + _sock->setReadBuffer(std::make_shared(1 * 1024)); sendAnnounce(); }