mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
for bug #241, change buffer size when chunk size changed.
This commit is contained in:
parent
19b1d750aa
commit
aee00877df
6 changed files with 52 additions and 20 deletions
|
@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
// when we read from socket less than this value,
|
// when we read from socket less than this value,
|
||||||
// sleep a while to merge read.
|
// sleep a while to merge read.
|
||||||
// @see https://github.com/winlinvip/simple-rtmp-server/issues/241
|
// @see https://github.com/winlinvip/simple-rtmp-server/issues/241
|
||||||
#define SRS_MERGED_READ_SIZE (SOCKET_READ_SIZE / 10)
|
#define SRS_MERGED_READ_SIZE(buffer) (buffer / 10)
|
||||||
// the time to sleep to merge read, to read more bytes.
|
// the time to sleep to merge read, to read more bytes.
|
||||||
#define SRS_MERGED_READ_US (300 * 1000)
|
#define SRS_MERGED_READ_US (300 * 1000)
|
||||||
|
|
||||||
|
@ -334,7 +334,7 @@ void SrsPublishRecvThread::on_recv_error(int ret)
|
||||||
st_cond_signal(error);
|
st_cond_signal(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SrsPublishRecvThread::on_read(ssize_t nread)
|
void SrsPublishRecvThread::on_read(int nb_buffer, ssize_t nread)
|
||||||
{
|
{
|
||||||
if (nread < 0) {
|
if (nread < 0) {
|
||||||
return;
|
return;
|
||||||
|
@ -346,7 +346,7 @@ void SrsPublishRecvThread::on_read(ssize_t nread)
|
||||||
* that is, we merge some data to read together.
|
* that is, we merge some data to read together.
|
||||||
* @see https://github.com/winlinvip/simple-rtmp-server/issues/241
|
* @see https://github.com/winlinvip/simple-rtmp-server/issues/241
|
||||||
*/
|
*/
|
||||||
if (nread < SRS_MERGED_READ_SIZE) {
|
if (nread < SRS_MERGED_READ_SIZE(nb_buffer)) {
|
||||||
st_usleep(SRS_MERGED_READ_US);
|
st_usleep(SRS_MERGED_READ_US);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -173,7 +173,7 @@ public:
|
||||||
virtual void on_recv_error(int ret);
|
virtual void on_recv_error(int ret);
|
||||||
// interface IMergeReadHandler
|
// interface IMergeReadHandler
|
||||||
public:
|
public:
|
||||||
virtual void on_read(ssize_t nread);
|
virtual void on_read(int nb_buffer, ssize_t nread);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<!DOCTYPE QtCreatorProject>
|
<!DOCTYPE QtCreatorProject>
|
||||||
<!-- Written by QtCreator 3.1.1, 2014-12-01T23:49:21. -->
|
|
||||||
<qtcreator>
|
<qtcreator>
|
||||||
<data>
|
<data>
|
||||||
<variable>ProjectExplorer.Project.ActiveTarget</variable>
|
<variable>ProjectExplorer.Project.ActiveTarget</variable>
|
||||||
|
|
|
@ -26,6 +26,14 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
#include <srs_kernel_error.hpp>
|
#include <srs_kernel_error.hpp>
|
||||||
#include <srs_kernel_log.hpp>
|
#include <srs_kernel_log.hpp>
|
||||||
|
|
||||||
|
// 4KB=4096
|
||||||
|
// 8KB=8192
|
||||||
|
// 16KB=16384
|
||||||
|
// 32KB=32768
|
||||||
|
// 64KB=65536
|
||||||
|
// @see https://github.com/winlinvip/simple-rtmp-server/issues/241
|
||||||
|
#define SOCKET_READ_SIZE 4096
|
||||||
|
|
||||||
IMergeReadHandler::IMergeReadHandler()
|
IMergeReadHandler::IMergeReadHandler()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -39,7 +47,8 @@ SrsBuffer::SrsBuffer()
|
||||||
merged_read = false;
|
merged_read = false;
|
||||||
_handler = NULL;
|
_handler = NULL;
|
||||||
|
|
||||||
buffer = new char[SOCKET_READ_SIZE];
|
nb_buffer = SOCKET_READ_SIZE;
|
||||||
|
buffer = new char[nb_buffer];
|
||||||
}
|
}
|
||||||
|
|
||||||
SrsBuffer::~SrsBuffer()
|
SrsBuffer::~SrsBuffer()
|
||||||
|
@ -92,7 +101,7 @@ int SrsBuffer::grow(ISrsBufferReader* reader, int required_size)
|
||||||
|
|
||||||
while (length() < required_size) {
|
while (length() < required_size) {
|
||||||
ssize_t nread;
|
ssize_t nread;
|
||||||
if ((ret = reader->read(buffer, SOCKET_READ_SIZE, &nread)) != ERROR_SUCCESS) {
|
if ((ret = reader->read(buffer, nb_buffer, &nread)) != ERROR_SUCCESS) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,7 +112,7 @@ int SrsBuffer::grow(ISrsBufferReader* reader, int required_size)
|
||||||
* @see https://github.com/winlinvip/simple-rtmp-server/issues/241
|
* @see https://github.com/winlinvip/simple-rtmp-server/issues/241
|
||||||
*/
|
*/
|
||||||
if (merged_read && _handler) {
|
if (merged_read && _handler) {
|
||||||
_handler->on_read(nread);
|
_handler->on_read(nb_buffer, nread);
|
||||||
}
|
}
|
||||||
|
|
||||||
srs_assert((int)nread > 0);
|
srs_assert((int)nread > 0);
|
||||||
|
@ -119,4 +128,21 @@ void SrsBuffer::set_merge_read(bool v, IMergeReadHandler* handler)
|
||||||
_handler = handler;
|
_handler = handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SrsBuffer::on_chunk_size(int32_t chunk_size)
|
||||||
|
{
|
||||||
|
if (nb_buffer >= chunk_size) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
srs_freep(buffer);
|
||||||
|
|
||||||
|
nb_buffer = chunk_size;
|
||||||
|
buffer = new char[nb_buffer];
|
||||||
|
}
|
||||||
|
|
||||||
|
int SrsBuffer::buffer_size()
|
||||||
|
{
|
||||||
|
return nb_buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -34,14 +34,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
#include <srs_protocol_io.hpp>
|
#include <srs_protocol_io.hpp>
|
||||||
|
|
||||||
// 4KB=4096
|
|
||||||
// 8KB=8192
|
|
||||||
// 16KB=16384
|
|
||||||
// 32KB=32768
|
|
||||||
// 64KB=65536
|
|
||||||
// @see https://github.com/winlinvip/simple-rtmp-server/issues/241
|
|
||||||
#define SOCKET_READ_SIZE 4096
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* to improve read performance, merge some packets then read,
|
* to improve read performance, merge some packets then read,
|
||||||
* when it on and read small bytes, we sleep to wait more data.,
|
* when it on and read small bytes, we sleep to wait more data.,
|
||||||
|
@ -59,7 +51,7 @@ public:
|
||||||
* some small bytes.
|
* some small bytes.
|
||||||
* @remark, it only for server-side, client srs-librtmp just ignore.
|
* @remark, it only for server-side, client srs-librtmp just ignore.
|
||||||
*/
|
*/
|
||||||
virtual void on_read(ssize_t nread) = 0;
|
virtual void on_read(int nb_buffer, ssize_t nread) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -75,6 +67,7 @@ private:
|
||||||
// data and socket buffer
|
// data and socket buffer
|
||||||
std::vector<char> data;
|
std::vector<char> data;
|
||||||
char* buffer;
|
char* buffer;
|
||||||
|
int nb_buffer;
|
||||||
public:
|
public:
|
||||||
SrsBuffer();
|
SrsBuffer();
|
||||||
virtual ~SrsBuffer();
|
virtual ~SrsBuffer();
|
||||||
|
@ -121,6 +114,17 @@ public:
|
||||||
* @see https://github.com/winlinvip/simple-rtmp-server/issues/241
|
* @see https://github.com/winlinvip/simple-rtmp-server/issues/241
|
||||||
*/
|
*/
|
||||||
virtual void set_merge_read(bool v, IMergeReadHandler* handler);
|
virtual void set_merge_read(bool v, IMergeReadHandler* handler);
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* when chunk size changed, the buffer should change the buffer also.
|
||||||
|
* to keep the socket buffer size always greater than chunk size.
|
||||||
|
* @see https://github.com/winlinvip/simple-rtmp-server/issues/241
|
||||||
|
*/
|
||||||
|
virtual void on_chunk_size(int32_t chunk_size);
|
||||||
|
/**
|
||||||
|
* get the size of socket buffer to read.
|
||||||
|
*/
|
||||||
|
virtual int buffer_size();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -1688,10 +1688,13 @@ int SrsProtocol::on_recv_message(SrsMessage* msg)
|
||||||
pkt->chunk_size, SRS_CONSTS_RTMP_MIN_CHUNK_SIZE,
|
pkt->chunk_size, SRS_CONSTS_RTMP_MIN_CHUNK_SIZE,
|
||||||
SRS_CONSTS_RTMP_MAX_CHUNK_SIZE);
|
SRS_CONSTS_RTMP_MAX_CHUNK_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int sock_buffer = in_buffer->buffer_size();
|
||||||
in_chunk_size = pkt->chunk_size;
|
in_chunk_size = pkt->chunk_size;
|
||||||
|
in_buffer->on_chunk_size(pkt->chunk_size);
|
||||||
srs_trace("input chunk size to %d", pkt->chunk_size);
|
srs_trace("input chunk size to %d, sock buf %d=>%d",
|
||||||
|
pkt->chunk_size, sock_buffer, in_buffer->buffer_size());
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case RTMP_MSG_UserControlMessage: {
|
case RTMP_MSG_UserControlMessage: {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue