1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-15 04:42:04 +00:00
srs/trunk/src/protocol/srs_protocol_stream.cpp

218 lines
6 KiB
C++
Raw Normal View History

2017-03-25 09:21:39 +00:00
/**
* The MIT License (MIT)
*
2017-03-25 13:29:29 +00:00
* Copyright (c) 2013-2017 OSSRS(winlin)
2017-03-25 09:21:39 +00:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
2013-11-23 03:36:07 +00:00
2015-09-22 01:01:47 +00:00
#include <srs_protocol_stream.hpp>
2013-11-23 03:36:07 +00:00
2015-03-18 08:34:27 +00:00
#include <stdlib.h>
#include <srs_kernel_error.hpp>
#include <srs_kernel_log.hpp>
#include <srs_kernel_utility.hpp>
#include <srs_core_performance.hpp>
2013-11-23 03:36:07 +00:00
// the default recv buffer size, 128KB.
#define SRS_DEFAULT_RECV_BUFFER_SIZE 131072
// limit user-space buffer to 256KB, for 3Mbps stream delivery.
// 800*2000/8=200000B(about 195KB).
// @remark it's ok for higher stream, the buffer is ok for one chunk is 256KB.
#define SRS_MAX_SOCKET_BUFFER 262144
// the max header size,
// @see SrsProtocol::read_message_header().
#define SRS_RTMP_MAX_MESSAGE_HEADER 11
#ifdef SRS_PERF_MERGED_READ
IMergeReadHandler::IMergeReadHandler()
{
}
IMergeReadHandler::~IMergeReadHandler()
{
}
#endif
2013-11-23 03:36:07 +00:00
2015-09-22 01:01:47 +00:00
SrsFastStream::SrsFastStream()
2013-11-23 03:36:07 +00:00
{
#ifdef SRS_PERF_MERGED_READ
merged_read = false;
_handler = NULL;
#endif
nb_buffer = SRS_DEFAULT_RECV_BUFFER_SIZE;
2015-03-18 08:34:27 +00:00
buffer = (char*)malloc(nb_buffer);
p = end = buffer;
2013-11-23 03:36:07 +00:00
}
2015-09-22 01:01:47 +00:00
SrsFastStream::~SrsFastStream()
2015-03-18 08:34:27 +00:00
{
free(buffer);
buffer = NULL;
}
2015-09-22 01:01:47 +00:00
int SrsFastStream::size()
2015-03-06 03:36:26 +00:00
{
2015-06-07 07:13:41 +00:00
return (int)(end - p);
2015-03-06 03:36:26 +00:00
}
2015-09-22 01:01:47 +00:00
char* SrsFastStream::bytes()
2015-03-06 03:36:26 +00:00
{
return p;
}
2015-09-22 01:01:47 +00:00
void SrsFastStream::set_buffer(int buffer_size)
{
// never exceed the max size.
2015-03-18 08:34:27 +00:00
if (buffer_size > SRS_MAX_SOCKET_BUFFER) {
2017-03-25 09:21:39 +00:00
srs_warn("limit the user-space buffer from %d to %d",
buffer_size, SRS_MAX_SOCKET_BUFFER);
}
// the user-space buffer size limit to a max value.
int nb_resize_buf = srs_min(buffer_size, SRS_MAX_SOCKET_BUFFER);
2017-03-25 09:21:39 +00:00
// only realloc when buffer changed bigger
2015-03-18 08:34:27 +00:00
if (nb_resize_buf <= nb_buffer) {
return;
}
// realloc for buffer change bigger.
int start = (int)(p - buffer);
int nb_bytes = (int)(end - p);
2015-03-18 08:34:27 +00:00
buffer = (char*)realloc(buffer, nb_resize_buf);
nb_buffer = nb_resize_buf;
p = buffer + start;
2015-03-18 08:34:27 +00:00
end = p + nb_bytes;
2013-11-23 03:36:07 +00:00
}
2015-09-22 01:01:47 +00:00
char SrsFastStream::read_1byte()
2013-11-23 03:36:07 +00:00
{
srs_assert(end - p >= 1);
return *p++;
2013-11-23 03:36:07 +00:00
}
2015-09-22 01:01:47 +00:00
char* SrsFastStream::read_slice(int size)
{
srs_assert(size >= 0);
srs_assert(end - p >= size);
srs_assert(p + size >= buffer);
2014-06-07 13:09:46 +00:00
char* ptr = p;
p += size;
2017-03-25 09:21:39 +00:00
return ptr;
2013-11-23 03:36:07 +00:00
}
2015-09-22 01:01:47 +00:00
void SrsFastStream::skip(int size)
2013-11-23 03:36:07 +00:00
{
srs_assert(end - p >= size);
srs_assert(p + size >= buffer);
p += size;
2013-11-23 03:36:07 +00:00
}
2017-01-30 11:59:59 +00:00
int SrsFastStream::grow(ISrsReader* reader, int required_size)
2013-11-23 03:36:07 +00:00
{
2014-03-18 03:32:58 +00:00
int ret = ERROR_SUCCESS;
2017-03-25 09:21:39 +00:00
2015-03-18 08:34:27 +00:00
// already got required size of bytes.
if (end - p >= required_size) {
2014-03-18 03:32:58 +00:00
return ret;
}
2017-03-25 09:21:39 +00:00
// must be positive.
srs_assert(required_size > 0);
2017-03-25 09:21:39 +00:00
// the free space of buffer,
2015-03-18 08:34:27 +00:00
// buffer = consumed_bytes + exists_bytes + free_space.
int nb_free_space = (int)(buffer + nb_buffer - end);
2016-09-12 10:21:14 +00:00
// the bytes already in buffer
int nb_exists_bytes = (int)(end - p);
srs_assert(nb_exists_bytes >= 0);
2015-03-18 08:34:27 +00:00
// resize the space when no left space.
2016-09-12 10:21:14 +00:00
if (nb_free_space < required_size - nb_exists_bytes) {
2015-03-18 08:34:27 +00:00
srs_verbose("move fast buffer %d bytes", nb_exists_bytes);
2017-03-25 09:21:39 +00:00
// reset or move to get more space.
2015-03-18 08:34:27 +00:00
if (!nb_exists_bytes) {
// reset when buffer is empty.
p = end = buffer;
srs_verbose("all consumed, reset fast buffer");
} else if (nb_exists_bytes < nb_buffer && p > buffer) {
2015-03-18 08:34:27 +00:00
// move the left bytes to start of buffer.
// @remark Only move memory when space is enough, or failed at next check.
// @see https://github.com/ossrs/srs/issues/848
2015-03-18 08:34:27 +00:00
buffer = (char*)memmove(buffer, p, nb_exists_bytes);
p = buffer;
2015-03-18 08:34:27 +00:00
end = p + nb_exists_bytes;
}
// check whether enough free space in buffer.
nb_free_space = (int)(buffer + nb_buffer - end);
2016-09-12 10:21:14 +00:00
if (nb_free_space < required_size - nb_exists_bytes) {
ret = ERROR_READER_BUFFER_OVERFLOW;
2017-03-25 09:21:39 +00:00
srs_error("buffer overflow, required=%d, max=%d, left=%d, ret=%d",
required_size, nb_buffer, nb_free_space, ret);
return ret;
}
}
2017-03-25 09:21:39 +00:00
// buffer is ok, read required size of bytes.
while (end - p < required_size) {
2014-03-18 03:32:58 +00:00
ssize_t nread;
2015-03-18 08:34:27 +00:00
if ((ret = reader->read(end, nb_free_space, &nread)) != ERROR_SUCCESS) {
return ret;
2014-03-18 03:32:58 +00:00
}
#ifdef SRS_PERF_MERGED_READ
/**
2017-03-25 09:21:39 +00:00
* to improve read performance, merge some packets then read,
* when it on and read small bytes, we sleep to wait more data.,
* that is, we merge some data to read together.
* @see https://github.com/ossrs/srs/issues/241
*/
if (merged_read && _handler) {
_handler->on_read(nread);
}
#endif
// we just move the ptr to next.
2014-03-18 03:32:58 +00:00
srs_assert((int)nread > 0);
end += nread;
2015-03-18 08:34:27 +00:00
nb_free_space -= nread;
2014-03-18 03:32:58 +00:00
}
return ret;
2013-11-23 03:36:07 +00:00
}
#ifdef SRS_PERF_MERGED_READ
2015-09-22 01:01:47 +00:00
void SrsFastStream::set_merge_read(bool v, IMergeReadHandler* handler)
{
merged_read = v;
_handler = handler;
}
#endif