Blame view

trunk/src/app/srs_app_kbps.cpp 6.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
The MIT License (MIT)

Copyright (c) 2013-2014 winlin

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.
*/

#include <srs_app_kbps.hpp>
26
#include <srs_kernel_utility.hpp>
27
#include <srs_app_st.hpp>
28
29 30
#define _SRS_BANDWIDTH_LIMIT_INTERVAL_MS 100
31 32 33 34 35 36
SrsKbpsSample::SrsKbpsSample()
{
    bytes = time = 0;
    kbps = 0;
}
37 38 39 40
SrsKbpsSlice::SrsKbpsSlice()
{
    io.in = NULL;
    io.out = NULL;
41
    last_bytes = io_bytes_base = starttime = bytes = delta_bytes = 0;
42 43 44 45 46
}

SrsKbpsSlice::~SrsKbpsSlice()
{
}
47
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
int64_t SrsKbpsSlice::get_total_bytes()
{
    return bytes + last_bytes - io_bytes_base;
}

void SrsKbpsSlice::sample()
{
    int64_t now = srs_get_system_time_ms();
    int64_t total_bytes = get_total_bytes();
    
    if (sample_30s.time <= 0) {
        sample_30s.kbps = 0;
        sample_30s.time = now;
        sample_30s.bytes = total_bytes;
    }
    if (sample_1m.time <= 0) {
        sample_1m.kbps = 0;
        sample_1m.time = now;
        sample_1m.bytes = total_bytes;
    }
    if (sample_5m.time <= 0) {
        sample_5m.kbps = 0;
        sample_5m.time = now;
        sample_5m.bytes = total_bytes;
    }
    if (sample_60m.time <= 0) {
        sample_60m.kbps = 0;
        sample_60m.time = now;
        sample_60m.bytes = total_bytes;
    }
    
    if (now - sample_30s.time > 30 * 1000) {
        sample_30s.kbps = (total_bytes - sample_30s.bytes) * 8 / (now - sample_30s.time);
        sample_30s.time = now;
        sample_30s.bytes = total_bytes;
    }
    if (now - sample_1m.time > 60 * 1000) {
        sample_1m.kbps = (total_bytes - sample_1m.bytes) * 8 / (now - sample_1m.time);
        sample_1m.time = now;
        sample_1m.bytes = total_bytes;
    }
    if (now - sample_5m.time > 300 * 1000) {
        sample_5m.kbps = (total_bytes - sample_5m.bytes) * 8 / (now - sample_5m.time);
        sample_5m.time = now;
        sample_5m.bytes = total_bytes;
    }
    if (now - sample_60m.time > 3600 * 1000) {
        sample_60m.kbps = (total_bytes - sample_60m.bytes) * 8 / (now - sample_60m.time);
        sample_60m.time = now;
        sample_60m.bytes = total_bytes;
    }
}
101 102 103 104 105 106 107 108
IKbpsDelta::IKbpsDelta()
{
}

IKbpsDelta::~IKbpsDelta()
{
}
109 110 111 112 113 114 115 116
SrsKbps::SrsKbps()
{
}

SrsKbps::~SrsKbps()
{
}
117
void SrsKbps::set_io(ISrsProtocolStatistic* in, ISrsProtocolStatistic* out)
118
{
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    // set input stream
    // now, set start time.
    if (is.starttime == 0) {
        is.starttime = srs_get_system_time_ms();
    }
    // save the old in bytes.
    if (is.io.in) {
        is.bytes += is.last_bytes - is.io_bytes_base;
    }
    // use new io.
    is.io.in = in;
    is.last_bytes = is.io_bytes_base = 0;
    if (in) {
        is.last_bytes = is.io_bytes_base = in->get_recv_bytes();
    }
134 135
    // resample
    is.sample();
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
    
    // set output stream
    // now, set start time.
    if (os.starttime == 0) {
        os.starttime = srs_get_system_time_ms();
    }
    // save the old in bytes.
    if (os.io.out) {
        os.bytes += os.last_bytes - os.io_bytes_base;
    }
    // use new io.
    os.io.out = out;
    os.last_bytes = os.io_bytes_base = 0;
    if (out) {
        os.last_bytes = os.io_bytes_base = out->get_send_bytes();
    }
152 153
    // resample
    os.sample();
154 155 156 157
}

int SrsKbps::get_send_kbps()
{
158 159 160 161
    int64_t duration = srs_get_system_time_ms() - is.starttime;
    if (duration <= 0) {
        return 0;
    }
162
    int64_t bytes = get_send_bytes();
163
    return bytes * 8 / duration;
164 165 166 167
}

int SrsKbps::get_recv_kbps()
{
168 169 170 171
    int64_t duration = srs_get_system_time_ms() - os.starttime;
    if (duration <= 0) {
        return 0;
    }
172
    int64_t bytes = get_recv_bytes();
173 174 175
    return bytes * 8 / duration;
}
176
int SrsKbps::get_send_kbps_30s()
177 178 179 180
{
    return os.sample_30s.kbps;
}
181
int SrsKbps::get_recv_kbps_30s()
182 183 184 185
{
    return is.sample_30s.kbps;
}
186
int SrsKbps::get_send_kbps_5m()
187 188 189 190
{
    return os.sample_5m.kbps;
}
191
int SrsKbps::get_recv_kbps_5m()
192 193 194 195
{
    return is.sample_5m.kbps;
}
196 197
int64_t SrsKbps::get_send_bytes()
{
198
    return os.get_total_bytes();
199 200 201 202
}

int64_t SrsKbps::get_recv_bytes()
{
203 204 205
    return is.get_total_bytes();
}
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
int64_t SrsKbps::get_send_bytes_delta()
{
    int64_t delta = os.get_total_bytes() - os.delta_bytes;
    os.delta_bytes = os.get_total_bytes();
    return delta;
}

int64_t SrsKbps::get_recv_bytes_delta()
{
    int64_t delta = is.get_total_bytes() - is.delta_bytes;
    is.delta_bytes = is.get_total_bytes();
    return delta;
}

void SrsKbps::add_delta(IKbpsDelta* delta)
{
    srs_assert(delta);
    
    // update the total bytes
    is.last_bytes += delta->get_recv_bytes_delta();
    os.last_bytes += delta->get_send_bytes_delta();
    
    // we donot sample, please use sample() to do resample.
}
231 232
void SrsKbps::sample()
{
233
    // update the total bytes
234 235 236 237
    if (os.io.out) {
        os.last_bytes = os.io.out->get_send_bytes();
    }
    
238 239 240
    if (is.io.in) {
        is.last_bytes = is.io.in->get_recv_bytes();
    }
241 242 243
    
    // resample
    is.sample();
244
    os.sample();
245 246
}
247 248 249 250 251 252 253 254 255 256
SrsKbpsLimit::SrsKbpsLimit(SrsKbps* kbps, int limit_kbps)
{
    _kbps = kbps;
    _limit_kbps = limit_kbps;
}

SrsKbpsLimit::~SrsKbpsLimit()
{
}
257 258 259 260 261
int SrsKbpsLimit::limit_kbps()
{
    return _limit_kbps;
}
262 263
void SrsKbpsLimit::recv_limit()
{
264 265
    _kbps->sample();
    
266 267
    while (_kbps->get_recv_kbps() > _limit_kbps) {
        _kbps->sample();
268
        
269 270 271 272 273 274
        st_usleep(_SRS_BANDWIDTH_LIMIT_INTERVAL_MS * 1000);
    }
}

void SrsKbpsLimit::send_limit()
{
275 276
    _kbps->sample();
    
277 278
    while (_kbps->get_send_kbps() > _limit_kbps) {
        _kbps->sample();
279
        
280 281 282 283
        st_usleep(_SRS_BANDWIDTH_LIMIT_INTERVAL_MS * 1000);
    }
}
284