Blame view

trunk/src/kernel/srs_kernel_stream.cpp 4.1 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.
*/
winlin authored
24
#include <srs_kernel_stream.hpp>
winlin authored
25
26
#include <srs_kernel_log.hpp>
27
#include <srs_kernel_error.hpp>
winlin authored
28 29 30

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

SrsStream::~SrsStream()
{
}

int SrsStream::initialize(char* _bytes, int _size)
{
44 45 46 47 48 49 50 51 52 53 54 55 56
    int ret = ERROR_SUCCESS;
    
    if (!_bytes) {
        ret = ERROR_SYSTEM_STREAM_INIT;
        srs_error("stream param bytes must not be NULL. ret=%d", ret);
        return ret;
    }
    
    if (_size <= 0) {
        ret = ERROR_SYSTEM_STREAM_INIT;
        srs_error("stream param size must be positive. ret=%d", ret);
        return ret;
    }
winlin authored
57
58 59
    size = _size;
    p = bytes = _bytes;
winlin authored
60
61
    return ret;
winlin authored
62 63 64 65
}

void SrsStream::reset()
{
66
    p = bytes;
winlin authored
67 68 69 70
}

bool SrsStream::empty()
{
71
    return !p || !bytes || (p >= bytes + size);
winlin authored
72 73 74 75
}

bool SrsStream::require(int required_size)
{
76
    return !empty() && (required_size <= bytes + size - p);
winlin authored
77 78 79 80
}

void SrsStream::skip(int size)
{
81
    p += size;
winlin authored
82 83 84 85
}

int SrsStream::pos()
{
86
    return p - bytes;
winlin authored
87 88
}
89 90
int SrsStream::left()
{
91
    return size - pos();
92 93 94 95
}

char* SrsStream::current()
{
96
    return p;
97 98
}
winlin authored
99 100
int8_t SrsStream::read_1bytes()
{
101 102 103
    srs_assert(require(1));
    
    return (int8_t)*p++;
winlin authored
104 105 106 107
}

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

int64_t SrsStream::read_8bytes()
{
147 148 149 150
    srs_assert(require(8));
    
    int64_t value;
    pp = (char*)&value;
winlin authored
151 152 153 154 155 156 157 158
    pp[7] = *p++;
    pp[6] = *p++;
    pp[5] = *p++;
    pp[4] = *p++;
    pp[3] = *p++;
    pp[2] = *p++;
    pp[1] = *p++;
    pp[0] = *p++;
159 160
    
    return value;
winlin authored
161 162 163 164
}

std::string SrsStream::read_string(int len)
{
165 166 167 168 169 170 171 172
    srs_assert(require(len));
    
    std::string value;
    value.append(p, len);
    
    p += len;
    
    return value;
winlin authored
173 174 175 176
}

void SrsStream::write_1bytes(int8_t value)
{
177 178 179
    srs_assert(require(1));
    
    *p++ = value;
winlin authored
180 181 182 183
}

void SrsStream::write_2bytes(int16_t value)
{
184 185 186 187 188
    srs_assert(require(2));
    
    pp = (char*)&value;
    *p++ = pp[1];
    *p++ = pp[0];
winlin authored
189 190 191 192
}

void SrsStream::write_4bytes(int32_t value)
{
193 194 195 196 197 198 199
    srs_assert(require(4));
    
    pp = (char*)&value;
    *p++ = pp[3];
    *p++ = pp[2];
    *p++ = pp[1];
    *p++ = pp[0];
winlin authored
200 201 202 203
}

void SrsStream::write_8bytes(int64_t value)
{
204 205 206 207 208 209 210 211 212 213 214
    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
215 216 217 218
}

void SrsStream::write_string(std::string value)
{
219 220 221 222
    srs_assert(require(value.length()));
    
    memcpy(p, value.data(), value.length());
    p += value.length();
winlin authored
223 224
}