Skip to content

Commit fda7441

Browse files
ethourisMikołaj Małecki
andauthored
Refax: moved buffer tools to a separate file (#2499)
Co-authored-by: Mikołaj Małecki <[email protected]>
1 parent 8e9958a commit fda7441

File tree

7 files changed

+354
-176
lines changed

7 files changed

+354
-176
lines changed

srtcore/buffer_rcv.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,48 @@
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 - 2009, 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+
146
#include <cmath>
247
#include <limits>
348
#include "buffer_rcv.h"

srtcore/buffer_rcv.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#ifndef INC_SRT_BUFFER_RCV_H
1212
#define INC_SRT_BUFFER_RCV_H
1313

14-
#include "buffer_snd.h" // AvgBufSize
14+
#include "buffer_tools.h" // AvgBufSize
1515
#include "common.h"
1616
#include "queue.h"
1717
#include "sync.h"

srtcore/buffer_snd.cpp

Lines changed: 7 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ modified by
5252

5353
#include "platform_sys.h"
5454

55-
#include <cstring>
5655
#include <cmath>
5756
#include "buffer_snd.h"
5857
#include "packet.h"
@@ -65,103 +64,6 @@ using namespace std;
6564
using namespace srt_logging;
6665
using namespace sync;
6766

68-
// You can change this value at build config by using "ENFORCE" options.
69-
#if !defined(SRT_MAVG_SAMPLING_RATE)
70-
#define SRT_MAVG_SAMPLING_RATE 40
71-
#endif
72-
73-
bool AvgBufSize::isTimeToUpdate(const time_point& now) const
74-
{
75-
const int usMAvgBasePeriod = 1000000; // 1s in microseconds
76-
const int us2ms = 1000;
77-
const int msMAvgPeriod = (usMAvgBasePeriod / SRT_MAVG_SAMPLING_RATE) / us2ms;
78-
const uint64_t elapsed_ms = count_milliseconds(now - m_tsLastSamplingTime); // ms since last sampling
79-
return (elapsed_ms >= msMAvgPeriod);
80-
}
81-
82-
void AvgBufSize::update(const steady_clock::time_point& now, int pkts, int bytes, int timespan_ms)
83-
{
84-
const uint64_t elapsed_ms = count_milliseconds(now - m_tsLastSamplingTime); // ms since last sampling
85-
m_tsLastSamplingTime = now;
86-
const uint64_t one_second_in_ms = 1000;
87-
if (elapsed_ms > one_second_in_ms)
88-
{
89-
// No sampling in last 1 sec, initialize average
90-
m_dCountMAvg = pkts;
91-
m_dBytesCountMAvg = bytes;
92-
m_dTimespanMAvg = timespan_ms;
93-
return;
94-
}
95-
96-
//
97-
// weight last average value between -1 sec and last sampling time (LST)
98-
// and new value between last sampling time and now
99-
// |elapsed_ms|
100-
// +----------------------------------+-------+
101-
// -1 LST 0(now)
102-
//
103-
m_dCountMAvg = avg_iir_w<1000, double>(m_dCountMAvg, pkts, elapsed_ms);
104-
m_dBytesCountMAvg = avg_iir_w<1000, double>(m_dBytesCountMAvg, bytes, elapsed_ms);
105-
m_dTimespanMAvg = avg_iir_w<1000, double>(m_dTimespanMAvg, timespan_ms, elapsed_ms);
106-
}
107-
108-
int round_val(double val)
109-
{
110-
return static_cast<int>(round(val));
111-
}
112-
113-
CRateEstimator::CRateEstimator()
114-
: m_iInRatePktsCount(0)
115-
, m_iInRateBytesCount(0)
116-
, m_InRatePeriod(INPUTRATE_FAST_START_US) // 0.5 sec (fast start)
117-
, m_iInRateBps(INPUTRATE_INITIAL_BYTESPS)
118-
{}
119-
120-
void CRateEstimator::setInputRateSmpPeriod(int period)
121-
{
122-
m_InRatePeriod = (uint64_t)period; //(usec) 0=no input rate calculation
123-
}
124-
125-
void CRateEstimator::updateInputRate(const time_point& time, int pkts, int bytes)
126-
{
127-
// no input rate calculation
128-
if (m_InRatePeriod == 0)
129-
return;
130-
131-
if (is_zero(m_tsInRateStartTime))
132-
{
133-
m_tsInRateStartTime = time;
134-
return;
135-
}
136-
else if (time < m_tsInRateStartTime)
137-
{
138-
// Old packets are being submitted for estimation, e.g. during the backup link activation.
139-
return;
140-
}
141-
142-
m_iInRatePktsCount += pkts;
143-
m_iInRateBytesCount += bytes;
144-
145-
// Trigger early update in fast start mode
146-
const bool early_update = (m_InRatePeriod < INPUTRATE_RUNNING_US) && (m_iInRatePktsCount > INPUTRATE_MAX_PACKETS);
147-
148-
const uint64_t period_us = count_microseconds(time - m_tsInRateStartTime);
149-
if (early_update || period_us > m_InRatePeriod)
150-
{
151-
// Required Byte/sec rate (payload + headers)
152-
m_iInRateBytesCount += (m_iInRatePktsCount * CPacket::SRT_DATA_HDR_SIZE);
153-
m_iInRateBps = (int)(((int64_t)m_iInRateBytesCount * 1000000) / period_us);
154-
HLOGC(bslog.Debug,
155-
log << "updateInputRate: pkts:" << m_iInRateBytesCount << " bytes:" << m_iInRatePktsCount
156-
<< " rate=" << (m_iInRateBps * 8) / 1000 << "kbps interval=" << period_us);
157-
m_iInRatePktsCount = 0;
158-
m_iInRateBytesCount = 0;
159-
m_tsInRateStartTime = time;
160-
161-
setInputRateSmpPeriod(INPUTRATE_RUNNING_US);
162-
}
163-
}
164-
16567
CSndBuffer::CSndBuffer(int size, int maxpld, int authtag)
16668
: m_BufLock()
16769
, m_pBlock(NULL)
@@ -654,6 +556,13 @@ int CSndBuffer::getCurrBufSize() const
654556
return m_iCount;
655557
}
656558

559+
namespace {
560+
int round_val(double val)
561+
{
562+
return static_cast<int>(round(val));
563+
}
564+
}
565+
657566
int CSndBuffer::getAvgBufSize(int& w_bytes, int& w_tsp)
658567
{
659568
ScopedLock bufferguard(m_BufLock); /* Consistency of pkts vs. bytes vs. spantime */

srtcore/buffer_snd.h

Lines changed: 5 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,12 @@ modified by
5050
Haivision Systems Inc.
5151
*****************************************************************************/
5252

53-
#ifndef INC_SRT_BUFFER_H
54-
#define INC_SRT_BUFFER_H
53+
#ifndef INC_SRT_BUFFER_SND_H
54+
#define INC_SRT_BUFFER_SND_H
5555

56-
#include "udt.h"
57-
#include "list.h"
58-
#include "queue.h"
59-
#include "tsbpd_time.h"
60-
#include "utilities.h"
56+
#include "srt.h"
57+
#include "packet.h"
58+
#include "buffer_tools.h"
6159

6260
// The notation used for "circular numbers" in comments:
6361
// The "cicrular numbers" are numbers that when increased up to the
@@ -73,76 +71,6 @@ modified by
7371

7472
namespace srt {
7573

76-
/// The AvgBufSize class is used to calculate moving average of the buffer (RCV or SND)
77-
class AvgBufSize
78-
{
79-
typedef sync::steady_clock::time_point time_point;
80-
81-
public:
82-
AvgBufSize()
83-
: m_dBytesCountMAvg(0.0)
84-
, m_dCountMAvg(0.0)
85-
, m_dTimespanMAvg(0.0)
86-
{
87-
}
88-
89-
public:
90-
bool isTimeToUpdate(const time_point& now) const;
91-
void update(const time_point& now, int pkts, int bytes, int timespan_ms);
92-
93-
public:
94-
inline double pkts() const { return m_dCountMAvg; }
95-
inline double timespan_ms() const { return m_dTimespanMAvg; }
96-
inline double bytes() const { return m_dBytesCountMAvg; }
97-
98-
private:
99-
time_point m_tsLastSamplingTime;
100-
double m_dBytesCountMAvg;
101-
double m_dCountMAvg;
102-
double m_dTimespanMAvg;
103-
};
104-
105-
/// The class to estimate source bitrate based on samples submitted to the buffer.
106-
/// Is currently only used by the CSndBuffer.
107-
class CRateEstimator
108-
{
109-
typedef sync::steady_clock::time_point time_point;
110-
typedef sync::steady_clock::duration duration;
111-
public:
112-
CRateEstimator();
113-
114-
public:
115-
uint64_t getInRatePeriod() const { return m_InRatePeriod; }
116-
117-
/// Retrieve input bitrate in bytes per second
118-
int getInputRate() const { return m_iInRateBps; }
119-
120-
void setInputRateSmpPeriod(int period);
121-
122-
/// Update input rate calculation.
123-
/// @param [in] time current time in microseconds
124-
/// @param [in] pkts number of packets newly added to the buffer
125-
/// @param [in] bytes number of payload bytes in those newly added packets
126-
///
127-
/// @return Current size of the data in the sending list.
128-
void updateInputRate(const time_point& time, int pkts = 0, int bytes = 0);
129-
130-
void resetInputRateSmpPeriod(bool disable = false) { setInputRateSmpPeriod(disable ? 0 : INPUTRATE_FAST_START_US); }
131-
132-
private: // Constants
133-
static const uint64_t INPUTRATE_FAST_START_US = 500000; // 500 ms
134-
static const uint64_t INPUTRATE_RUNNING_US = 1000000; // 1000 ms
135-
static const int64_t INPUTRATE_MAX_PACKETS = 2000; // ~ 21 Mbps of 1316 bytes payload
136-
static const int INPUTRATE_INITIAL_BYTESPS = BW_INFINITE;
137-
138-
private:
139-
int m_iInRatePktsCount; // number of payload bytes added since InRateStartTime
140-
int m_iInRateBytesCount; // number of payload bytes added since InRateStartTime
141-
time_point m_tsInRateStartTime;
142-
uint64_t m_InRatePeriod; // usec
143-
int m_iInRateBps; // Input Rate in Bytes/sec
144-
};
145-
14674
class CSndBuffer
14775
{
14876
typedef sync::steady_clock::time_point time_point;

0 commit comments

Comments
 (0)