| 1 |
/* |
| 2 |
* SRT - Secure, Reliable, Transport |
| 3 |
* Copyright (c) 2018 Haivision Systems Inc. |
| 4 |
* |
| 5 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 6 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 |
* |
| 9 |
*/ |
| 10 |
|
| 11 |
/***************************************************************************** |
| 12 |
Copyright (c) 2001 - 2011, The Board of Trustees of the University of Illinois. |
| 13 |
All rights reserved. |
| 14 |
|
| 15 |
Redistribution and use in source and binary forms, with or without |
| 16 |
modification, are permitted provided that the following conditions are |
| 17 |
met: |
| 18 |
|
| 19 |
* Redistributions of source code must retain the above |
| 20 |
copyright notice, this list of conditions and the |
| 21 |
following disclaimer. |
| 22 |
|
| 23 |
* Redistributions in binary form must reproduce the |
| 24 |
above copyright notice, this list of conditions |
| 25 |
and the following disclaimer in the documentation |
| 26 |
and/or other materials provided with the distribution. |
| 27 |
|
| 28 |
* Neither the name of the University of Illinois |
| 29 |
nor the names of its contributors may be used to |
| 30 |
endorse or promote products derived from this |
| 31 |
software without specific prior written permission. |
| 32 |
|
| 33 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 34 |
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 35 |
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 36 |
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 37 |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 38 |
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 39 |
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 40 |
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 41 |
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 42 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 43 |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 44 |
*****************************************************************************/ |
| 45 |
|
| 46 |
/***************************************************************************** |
| 47 |
written by |
| 48 |
Yunhong Gu, last updated 03/12/2011 |
| 49 |
modified by |
| 50 |
Haivision Systems Inc. |
| 51 |
*****************************************************************************/ |
| 52 |
|
| 53 |
#include "platform_sys.h" |
| 54 |
#include "buffer_tools.h" |
| 55 |
#include "packet.h" |
| 56 |
#include "logger_defs.h" |
| 57 |
#include "utilities.h" |
| 58 |
|
| 59 |
namespace srt { |
| 60 |
|
| 61 |
using namespace std; |
| 62 |
using namespace srt_logging; |
| 63 |
using namespace sync; |
| 64 |
|
| 65 |
// You can change this value at build config by using "ENFORCE" options. |
| 66 |
#if !defined(SRT_MAVG_SAMPLING_RATE) |
| 67 |
#define SRT_MAVG_SAMPLING_RATE 40 |
| 68 |
#endif |
| 69 |
|
| 70 |
bool AvgBufSize::isTimeToUpdate(const time_point& now) const |
| 71 |
{ |
| 72 |
const int usMAvgBasePeriod = 1000000; // 1s in microseconds |
| 73 |
const int us2ms = 1000; |
| 74 |
const int msMAvgPeriod = (usMAvgBasePeriod / SRT_MAVG_SAMPLING_RATE) / us2ms; |
| 75 |
const uint64_t elapsed_ms = count_milliseconds(now - m_tsLastSamplingTime); // ms since last sampling |
| 76 |
return (elapsed_ms >= msMAvgPeriod); |
| 77 |
} |
| 78 |
|
| 79 |
void AvgBufSize::update(const steady_clock::time_point& now, int pkts, int bytes, int timespan_ms) |
| 80 |
{ |
| 81 |
const uint64_t elapsed_ms = count_milliseconds(now - m_tsLastSamplingTime); // ms since last sampling |
| 82 |
m_tsLastSamplingTime = now; |
| 83 |
const uint64_t one_second_in_ms = 1000; |
| 84 |
if (elapsed_ms > one_second_in_ms) |
| 85 |
{ |
| 86 |
// No sampling in last 1 sec, initialize average |
| 87 |
m_dCountMAvg = pkts; |
| 88 |
m_dBytesCountMAvg = bytes; |
| 89 |
m_dTimespanMAvg = timespan_ms; |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
// |
| 94 |
// weight last average value between -1 sec and last sampling time (LST) |
| 95 |
// and new value between last sampling time and now |
| 96 |
// |elapsed_ms| |
| 97 |
// +----------------------------------+-------+ |
| 98 |
// -1 LST 0(now) |
| 99 |
// |
| 100 |
m_dCountMAvg = avg_iir_w<1000, double>(m_dCountMAvg, pkts, elapsed_ms); |
| 101 |
m_dBytesCountMAvg = avg_iir_w<1000, double>(m_dBytesCountMAvg, bytes, elapsed_ms); |
| 102 |
m_dTimespanMAvg = avg_iir_w<1000, double>(m_dTimespanMAvg, timespan_ms, elapsed_ms); |
| 103 |
} |
| 104 |
|
| 105 |
CRateEstimator::CRateEstimator(int /*family*/) |
| 106 |
: m_iInRatePktsCount(0) |
| 107 |
, m_iInRateBytesCount(0) |
| 108 |
, m_InRatePeriod(INPUTRATE_FAST_START_US) // 0.5 sec (fast start) |
| 109 |
, m_iInRateBps(INPUTRATE_INITIAL_BYTESPS) |
| 110 |
, m_iFullHeaderSize(CPacket::UDP_HDR_SIZE + CPacket::HDR_SIZE) |
| 111 |
{} |
| 112 |
|
| 113 |
void CRateEstimator::setInputRateSmpPeriod(int period) |
| 114 |
{ |
| 115 |
m_InRatePeriod = (uint64_t)period; //(usec) 0=no input rate calculation |
| 116 |
} |
| 117 |
|
| 118 |
void CRateEstimator::updateInputRate(const time_point& time, int pkts, int bytes) |
| 119 |
{ |
| 120 |
// no input rate calculation |
| 121 |
if (m_InRatePeriod == 0) |
| 122 |
return; |
| 123 |
|
| 124 |
if (is_zero(m_tsInRateStartTime)) |
| 125 |
{ |
| 126 |
m_tsInRateStartTime = time; |
| 127 |
return; |
| 128 |
} |
| 129 |
else if (time < m_tsInRateStartTime) |
| 130 |
{ |
| 131 |
// Old packets are being submitted for estimation, e.g. during the backup link activation. |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
m_iInRatePktsCount += pkts; |
| 136 |
m_iInRateBytesCount += bytes; |
| 137 |
|
| 138 |
// Trigger early update in fast start mode |
| 139 |
const bool early_update = (m_InRatePeriod < INPUTRATE_RUNNING_US) && (m_iInRatePktsCount > INPUTRATE_MAX_PACKETS); |
| 140 |
|
| 141 |
const uint64_t period_us = count_microseconds(time - m_tsInRateStartTime); |
| 142 |
if (!early_update && period_us <= m_InRatePeriod) |
| 143 |
return; |
| 144 |
|
| 145 |
// Required Byte/sec rate (payload + headers) |
| 146 |
m_iInRateBytesCount += (m_iInRatePktsCount * m_iFullHeaderSize); |
| 147 |
m_iInRateBps = (int)(((int64_t)m_iInRateBytesCount * 1000000) / period_us); |
| 148 |
HLOGC(bslog.Debug, |
| 149 |
log << "updateInputRate: pkts:" << m_iInRateBytesCount << " bytes:" << m_iInRatePktsCount |
| 150 |
<< " rate=" << (m_iInRateBps * 8) / 1000 << "kbps interval=" << period_us); |
| 151 |
m_iInRatePktsCount = 0; |
| 152 |
m_iInRateBytesCount = 0; |
| 153 |
m_tsInRateStartTime = time; |
| 154 |
|
| 155 |
setInputRateSmpPeriod(INPUTRATE_RUNNING_US); |
| 156 |
} |
| 157 |
|
| 158 |
CSndRateEstimator::CSndRateEstimator(const time_point& tsNow) |
| 159 |
: m_tsFirstSampleTime(tsNow) |
| 160 |
, m_iFirstSampleIdx(0) |
| 161 |
, m_iCurSampleIdx(0) |
| 162 |
, m_iRateBps(0) |
| 163 |
{ |
| 164 |
|
| 165 |
} |
| 166 |
|
| 167 |
void CSndRateEstimator::addSample(const time_point& ts, int pkts, size_t bytes) |
| 168 |
{ |
| 169 |
const int iSampleDeltaIdx = (int) count_milliseconds(ts - m_tsFirstSampleTime) / SAMPLE_DURATION_MS; |
| 170 |
const int delta = NUM_PERIODS - iSampleDeltaIdx; |
| 171 |
|
| 172 |
// TODO: -delta <= NUM_PERIODS, then just reset the state on the estimator. |
| 173 |
|
| 174 |
if (iSampleDeltaIdx >= 2 * NUM_PERIODS) |
| 175 |
{ |
| 176 |
// Just reset the estimator and start like if new. |
| 177 |
for (int i = 0; i < NUM_PERIODS; ++i) |
| 178 |
{ |
| 179 |
const int idx = incSampleIdx(m_iFirstSampleIdx, i); |
| 180 |
m_Samples[idx].reset(); |
| 181 |
|
| 182 |
if (idx == m_iCurSampleIdx) |
| 183 |
break; |
| 184 |
} |
| 185 |
|
| 186 |
m_iFirstSampleIdx = 0; |
| 187 |
m_iCurSampleIdx = 0; |
| 188 |
m_iRateBps = 0; |
| 189 |
m_tsFirstSampleTime += milliseconds_from(iSampleDeltaIdx * SAMPLE_DURATION_MS); |
| 190 |
} |
| 191 |
else if (iSampleDeltaIdx > NUM_PERIODS) |
| 192 |
{ |
| 193 |
// In run-time a constant flow of samples is expected. Once all periods are filled (after 1 second of sampling), |
| 194 |
// the iSampleDeltaIdx should be either (NUM_PERIODS - 1), |
| 195 |
// or NUM_PERIODS. In the later case it means the start of a new sampling period. |
| 196 |
int d = delta; |
| 197 |
while (d < 0) |
| 198 |
{ |
| 199 |
m_Samples[m_iFirstSampleIdx].reset(); |
| 200 |
m_iFirstSampleIdx = incSampleIdx(m_iFirstSampleIdx); |
| 201 |
m_tsFirstSampleTime += milliseconds_from(SAMPLE_DURATION_MS); |
| 202 |
m_iCurSampleIdx = incSampleIdx(m_iCurSampleIdx); |
| 203 |
++d; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
// Check if the new sample period has started. |
| 208 |
const int iNewDeltaIdx = (int) count_milliseconds(ts - m_tsFirstSampleTime) / SAMPLE_DURATION_MS; |
| 209 |
if (incSampleIdx(m_iFirstSampleIdx, iNewDeltaIdx) != m_iCurSampleIdx) |
| 210 |
{ |
| 211 |
// Now there should be some periods (at most last NUM_PERIODS) ready to be summed, |
| 212 |
// rate estimation updated, after which all the new entry should be added. |
| 213 |
Sample sum; |
| 214 |
int iNumPeriods = 0; |
| 215 |
bool bMetNonEmpty = false; |
| 216 |
for (int i = 0; i < NUM_PERIODS; ++i) |
| 217 |
{ |
| 218 |
const int idx = incSampleIdx(m_iFirstSampleIdx, i); |
| 219 |
const Sample& s = m_Samples[idx]; |
| 220 |
sum += s; |
| 221 |
if (bMetNonEmpty || !s.empty()) |
| 222 |
{ |
| 223 |
++iNumPeriods; |
| 224 |
bMetNonEmpty = true; |
| 225 |
} |
| 226 |
|
| 227 |
if (idx == m_iCurSampleIdx) |
| 228 |
break; |
| 229 |
} |
| 230 |
|
| 231 |
if (iNumPeriods == 0) |
| 232 |
{ |
| 233 |
m_iRateBps = 0; |
| 234 |
} |
| 235 |
else |
| 236 |
{ |
| 237 |
m_iRateBps = (sum.m_iBytesCount + CPacket::HDR_SIZE * sum.m_iPktsCount) * 1000 / (iNumPeriods * SAMPLE_DURATION_MS); |
| 238 |
} |
| 239 |
|
| 240 |
HLOGC(bslog.Note, |
| 241 |
log << "CSndRateEstimator: new rate estimation :" << (m_iRateBps * 8) / 1000 << " kbps. Based on " |
| 242 |
<< iNumPeriods << " periods, " << sum.m_iPktsCount << " packets, " << sum.m_iBytesCount << " bytes."); |
| 243 |
|
| 244 |
// Shift one sampling period to start collecting the new one. |
| 245 |
m_iCurSampleIdx = incSampleIdx(m_iCurSampleIdx); |
| 246 |
m_Samples[m_iCurSampleIdx].reset(); |
| 247 |
|
| 248 |
// If all NUM_SAMPLES are recorded, the first position has to be shifted as well. |
| 249 |
if (delta <= 0) |
| 250 |
{ |
| 251 |
m_iFirstSampleIdx = incSampleIdx(m_iFirstSampleIdx); |
| 252 |
m_tsFirstSampleTime += milliseconds_from(SAMPLE_DURATION_MS); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
m_Samples[m_iCurSampleIdx].m_iBytesCount += (int) bytes; |
| 257 |
m_Samples[m_iCurSampleIdx].m_iPktsCount += pkts; |
| 258 |
} |
| 259 |
|
| 260 |
int CSndRateEstimator::getCurrentRate() const |
| 261 |
{ |
| 262 |
SRT_ASSERT(m_iCurSampleIdx >= 0 && m_iCurSampleIdx < NUM_PERIODS); |
| 263 |
const Sample& s = m_Samples[m_iCurSampleIdx]; |
| 264 |
return (int) avg_iir<16, unsigned long long>(m_iRateBps, (CPacket::HDR_SIZE * s.m_iPktsCount + s.m_iBytesCount) * 1000 / SAMPLE_DURATION_MS); |
| 265 |
} |
| 266 |
|
| 267 |
int CSndRateEstimator::incSampleIdx(int val, int inc) const |
| 268 |
{ |
| 269 |
SRT_ASSERT(inc >= 0 && inc <= NUM_PERIODS); |
| 270 |
val += inc; |
| 271 |
while (val >= NUM_PERIODS) |
| 272 |
val -= NUM_PERIODS; |
| 273 |
return val; |
| 274 |
} |
| 275 |
|
| 276 |
} |
| 277 |
|