Blame view

trunk/src/rtmp/srs_protocol_buffer.cpp 3.8 KB
winlin authored
1 2 3
/*
The MIT License (MIT)
4
Copyright (c) 2013-2014 winlin
winlin authored
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

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.
*/
24
#include <srs_protocol_buffer.hpp>
winlin authored
25
26
#include <srs_kernel_error.hpp>
27
#include <srs_kernel_log.hpp>
28
#include <srs_kernel_utility.hpp>
winlin authored
29
30 31 32 33 34 35 36
IMergeReadHandler::IMergeReadHandler()
{
}

IMergeReadHandler::~IMergeReadHandler()
{
}
winlin authored
37 38 39

SrsBuffer::SrsBuffer()
{
40 41 42
    merged_read = false;
    _handler = NULL;
    
43 44
    nb_buffer = SOCKET_READ_SIZE;
    buffer = new char[nb_buffer];
winlin authored
45 46 47 48
}

SrsBuffer::~SrsBuffer()
{
49
    srs_freep(buffer);
winlin authored
50 51
}
52
int SrsBuffer::length()
winlin authored
53
{
54 55 56
    int len = (int)data.size();
    srs_assert(len >= 0);
    return len;
57 58
}
winlin authored
59 60
char* SrsBuffer::bytes()
{
61
    return (length() == 0)? NULL : &data.at(0);
winlin authored
62 63
}
64
void SrsBuffer::erase(int size)
65
{
66 67 68
    if (size <= 0) {
        return;
    }
69
    
70
    if (size >= length()) {
71
        data.clear();
72 73 74
        return;
    }
    
75
    data.erase(data.begin(), data.begin() + size);
winlin authored
76 77
}
78
void SrsBuffer::append(const char* bytes, int size)
winlin authored
79
{
80
    srs_assert(size > 0);
81
82
    data.insert(data.end(), bytes, bytes + size);
winlin authored
83 84
}
85
int SrsBuffer::grow(ISrsBufferReader* reader, int required_size)
winlin authored
86
{
87 88 89 90 91 92 93 94
    int ret = ERROR_SUCCESS;

    if (required_size < 0) {
        ret = ERROR_SYSTEM_SIZE_NEGATIVE;
        srs_error("size is negative. size=%d, ret=%d", required_size, ret);
        return ret;
    }
95
    while (length() < required_size) {
96
        ssize_t nread;
97
        if ((ret = reader->read(buffer, nb_buffer, &nread)) != ERROR_SUCCESS) {
98
            return ret;
99 100
        }
        
101 102 103 104 105 106 107
        /**
        * 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/winlinvip/simple-rtmp-server/issues/241
        */
        if (merged_read && _handler) {
108
            _handler->on_read(nread);
109 110
        }
        
111 112 113 114 115
        srs_assert((int)nread > 0);
        append(buffer, (int)nread);
    }
    
    return ret;
winlin authored
116 117
}
118
void SrsBuffer::set_merge_read(bool v, int max_buffer, IMergeReadHandler* handler)
119 120 121
{
    merged_read = v;
    _handler = handler;
122
123 124 125 126 127
    // limit the max buffer.
    int buffer_size = srs_min(max_buffer, SOCKET_MAX_BUF);

    if (v && buffer_size != nb_buffer) {
        reset_buffer(buffer_size);
128
    }
129 130 131 132

    if (_handler) {
        _handler->on_buffer_change(nb_buffer);
    }
133 134
}
135 136 137 138 139 140
void SrsBuffer::on_chunk_size(int32_t chunk_size)
{
    if (nb_buffer >= chunk_size) {
        return;
    }
141 142 143 144 145 146
    // limit the max buffer.
    int buffer_size = srs_min(chunk_size, SOCKET_MAX_BUF);

    if (buffer_size != nb_buffer) {
        reset_buffer(buffer_size);
    }
147 148 149 150

    if (_handler) {
        _handler->on_buffer_change(nb_buffer);
    }
151 152 153 154 155 156 157
}

int SrsBuffer::buffer_size()
{
    return nb_buffer;
}
158 159 160 161 162 163 164
void SrsBuffer::reset_buffer(int size)
{
    srs_freep(buffer);

    nb_buffer = size;
    buffer = new char[nb_buffer];
}