Blame view

trunk/src/kernel/srs_kernel_stream.cpp 4.6 KB
winlin authored
1 2 3
/*
The MIT License (MIT)
4
Copyright (c) 2013-2015 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.
*/
winlin authored
24
#include <srs_kernel_stream.hpp>
winlin authored
25
26 27
using namespace std;
28
#include <srs_kernel_log.hpp>
29
#include <srs_kernel_error.hpp>
30
#include <srs_kernel_utility.hpp>
winlin authored
31 32 33

SrsStream::SrsStream()
{
34 35
    p = _bytes = NULL;
    _size = 0;
36 37 38
    
    // TODO: support both little and big endian.
    srs_assert(srs_is_little_endian());
winlin authored
39 40 41 42 43 44
}

SrsStream::~SrsStream()
{
}
45
int SrsStream::initialize(char* bytes, int size)
winlin authored
46
{
47 48
    int ret = ERROR_SUCCESS;
    
49 50
    if (!bytes) {
        ret = ERROR_KERNEL_STREAM_INIT;
51 52 53 54
        srs_error("stream param bytes must not be NULL. ret=%d", ret);
        return ret;
    }
    
55 56
    if (size <= 0) {
        ret = ERROR_KERNEL_STREAM_INIT;
57 58 59
        srs_error("stream param size must be positive. ret=%d", ret);
        return ret;
    }
winlin authored
60
61 62 63
    _size = size;
    p = _bytes = bytes;
    srs_info("init stream ok, size=%d", size);
winlin authored
64
65
    return ret;
winlin authored
66 67
}
68
char* SrsStream::data()
winlin authored
69
{
70
    return _bytes;
winlin authored
71 72
}
73
int SrsStream::size()
winlin authored
74
{
75
    return _size;
winlin authored
76 77
}
78
int SrsStream::pos()
winlin authored
79
{
80
    return p - _bytes;
winlin authored
81 82
}
83
bool SrsStream::empty()
winlin authored
84
{
85
    return !_bytes || (p >= _bytes + _size);
winlin authored
86 87
}
88
bool SrsStream::require(int required_size)
89
{
90 91 92
    srs_assert(required_size > 0);
    
    return required_size <= _size - (p - _bytes);
93 94
}
95
void SrsStream::skip(int size)
96
{
97 98 99
    srs_assert(p);
    
    p += size;
100 101
}
winlin authored
102 103
int8_t SrsStream::read_1bytes()
{
104 105 106
    srs_assert(require(1));
    
    return (int8_t)*p++;
winlin authored
107 108 109 110
}

int16_t SrsStream::read_2bytes()
{
111 112 113 114 115 116 117 118
    srs_assert(require(2));
    
    int16_t value;
    pp = (char*)&value;
    pp[1] = *p++;
    pp[0] = *p++;
    
    return value;
winlin authored
119 120
}
121 122
int32_t SrsStream::read_3bytes()
{
123 124 125 126 127 128 129 130 131
    srs_assert(require(3));
    
    int32_t value = 0x00;
    pp = (char*)&value;
    pp[2] = *p++;
    pp[1] = *p++;
    pp[0] = *p++;
    
    return value;
132 133
}
winlin authored
134 135
int32_t SrsStream::read_4bytes()
{
136 137 138 139 140 141 142 143 144 145
    srs_assert(require(4));
    
    int32_t value;
    pp = (char*)&value;
    pp[3] = *p++;
    pp[2] = *p++;
    pp[1] = *p++;
    pp[0] = *p++;
    
    return value;
winlin authored
146 147 148 149
}

int64_t SrsStream::read_8bytes()
{
150 151 152 153
    srs_assert(require(8));
    
    int64_t value;
    pp = (char*)&value;
winlin authored
154 155 156 157 158 159 160 161
    pp[7] = *p++;
    pp[6] = *p++;
    pp[5] = *p++;
    pp[4] = *p++;
    pp[3] = *p++;
    pp[2] = *p++;
    pp[1] = *p++;
    pp[0] = *p++;
162 163
    
    return value;
winlin authored
164 165
}
166
string SrsStream::read_string(int len)
winlin authored
167
{
168 169 170 171 172 173 174 175
    srs_assert(require(len));
    
    std::string value;
    value.append(p, len);
    
    p += len;
    
    return value;
winlin authored
176 177
}
178 179 180 181 182 183 184 185 186
void SrsStream::read_bytes(char* data, int size)
{
    srs_assert(require(size));
    
    memcpy(data, p, size);
    
    p += size;
}
winlin authored
187 188
void SrsStream::write_1bytes(int8_t value)
{
189 190 191
    srs_assert(require(1));
    
    *p++ = value;
winlin authored
192 193 194 195
}

void SrsStream::write_2bytes(int16_t value)
{
196 197 198 199 200
    srs_assert(require(2));
    
    pp = (char*)&value;
    *p++ = pp[1];
    *p++ = pp[0];
winlin authored
201 202 203 204
}

void SrsStream::write_4bytes(int32_t value)
{
205 206 207 208 209 210 211
    srs_assert(require(4));
    
    pp = (char*)&value;
    *p++ = pp[3];
    *p++ = pp[2];
    *p++ = pp[1];
    *p++ = pp[0];
winlin authored
212 213
}
winlin authored
214 215 216 217 218 219 220 221 222 223
void SrsStream::write_3bytes(int32_t value)
{
    srs_assert(require(3));
    
    pp = (char*)&value;
    *p++ = pp[2];
    *p++ = pp[1];
    *p++ = pp[0];
}
winlin authored
224 225
void SrsStream::write_8bytes(int64_t value)
{
226 227 228 229 230 231 232 233 234 235 236
    srs_assert(require(8));
    
    pp = (char*)&value;
    *p++ = pp[7];
    *p++ = pp[6];
    *p++ = pp[5];
    *p++ = pp[4];
    *p++ = pp[3];
    *p++ = pp[2];
    *p++ = pp[1];
    *p++ = pp[0];
winlin authored
237 238
}
239
void SrsStream::write_string(string value)
winlin authored
240
{
241 242 243 244
    srs_assert(require(value.length()));
    
    memcpy(p, value.data(), value.length());
    p += value.length();
winlin authored
245 246
}
247 248 249 250 251 252 253 254
void SrsStream::write_bytes(char* data, int size)
{
    srs_assert(require(size));
    
    memcpy(p, data, size);
    p += size;
}
255