buffer_tools.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. Copyright (c) 2001 - 2009, The Board of Trustees of the University of Illinois.
  12. All rights reserved.
  13. Redistribution and use in source and binary forms, with or without
  14. modification, are permitted provided that the following conditions are
  15. met:
  16. * Redistributions of source code must retain the above
  17. copyright notice, this list of conditions and the
  18. following disclaimer.
  19. * Redistributions in binary form must reproduce the
  20. above copyright notice, this list of conditions
  21. and the following disclaimer in the documentation
  22. and/or other materials provided with the distribution.
  23. * Neither the name of the University of Illinois
  24. nor the names of its contributors may be used to
  25. endorse or promote products derived from this
  26. software without specific prior written permission.
  27. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  28. IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  29. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  31. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  32. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  33. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  34. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  35. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  36. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  37. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *****************************************************************************/
  39. /*****************************************************************************
  40. written by
  41. Yunhong Gu, last updated 05/05/2009
  42. modified by
  43. Haivision Systems Inc.
  44. *****************************************************************************/
  45. #ifndef INC_SRT_BUFFER_TOOLS_H
  46. #define INC_SRT_BUFFER_TOOLS_H
  47. #include "common.h"
  48. namespace srt
  49. {
  50. /// The AvgBufSize class is used to calculate moving average of the buffer (RCV or SND)
  51. class AvgBufSize
  52. {
  53. typedef sync::steady_clock::time_point time_point;
  54. public:
  55. AvgBufSize()
  56. : m_dBytesCountMAvg(0.0)
  57. , m_dCountMAvg(0.0)
  58. , m_dTimespanMAvg(0.0)
  59. {
  60. }
  61. public:
  62. bool isTimeToUpdate(const time_point& now) const;
  63. void update(const time_point& now, int pkts, int bytes, int timespan_ms);
  64. public:
  65. inline double pkts() const { return m_dCountMAvg; }
  66. inline double timespan_ms() const { return m_dTimespanMAvg; }
  67. inline double bytes() const { return m_dBytesCountMAvg; }
  68. private:
  69. time_point m_tsLastSamplingTime;
  70. double m_dBytesCountMAvg;
  71. double m_dCountMAvg;
  72. double m_dTimespanMAvg;
  73. };
  74. /// The class to estimate source bitrate based on samples submitted to the buffer.
  75. /// Is currently only used by the CSndBuffer.
  76. class CRateEstimator
  77. {
  78. typedef sync::steady_clock::time_point time_point;
  79. typedef sync::steady_clock::duration duration;
  80. public:
  81. CRateEstimator();
  82. public:
  83. uint64_t getInRatePeriod() const { return m_InRatePeriod; }
  84. /// Retrieve input bitrate in bytes per second
  85. int getInputRate() const { return m_iInRateBps; }
  86. void setInputRateSmpPeriod(int period);
  87. /// Update input rate calculation.
  88. /// @param [in] time current time
  89. /// @param [in] pkts number of packets newly added to the buffer
  90. /// @param [in] bytes number of payload bytes in those newly added packets
  91. void updateInputRate(const time_point& time, int pkts = 0, int bytes = 0);
  92. void resetInputRateSmpPeriod(bool disable = false) { setInputRateSmpPeriod(disable ? 0 : INPUTRATE_FAST_START_US); }
  93. private: // Constants
  94. static const uint64_t INPUTRATE_FAST_START_US = 500000; // 500 ms
  95. static const uint64_t INPUTRATE_RUNNING_US = 1000000; // 1000 ms
  96. static const int64_t INPUTRATE_MAX_PACKETS = 2000; // ~ 21 Mbps of 1316 bytes payload
  97. static const int INPUTRATE_INITIAL_BYTESPS = BW_INFINITE;
  98. private:
  99. int m_iInRatePktsCount; // number of payload packets added since InRateStartTime.
  100. int m_iInRateBytesCount; // number of payload bytes added since InRateStartTime.
  101. time_point m_tsInRateStartTime;
  102. uint64_t m_InRatePeriod; // usec
  103. int m_iInRateBps; // Input Rate in Bytes/sec
  104. };
  105. class CSndRateEstimator
  106. {
  107. typedef sync::steady_clock::time_point time_point;
  108. public:
  109. CSndRateEstimator(const time_point& tsNow);
  110. /// Add sample.
  111. /// @param [in] time sample (sending) time.
  112. /// @param [in] pkts number of packets in the sample.
  113. /// @param [in] bytes number of payload bytes in the sample.
  114. void addSample(const time_point& time, int pkts = 0, size_t bytes = 0);
  115. /// Retrieve estimated bitrate in bytes per second
  116. int getRate() const { return m_iRateBps; }
  117. /// Retrieve estimated bitrate in bytes per second inluding the current sampling interval.
  118. int getCurrentRate() const;
  119. private:
  120. static const int NUM_PERIODS = 10;
  121. static const int SAMPLE_DURATION_MS = 100; // 100 ms
  122. struct Sample
  123. {
  124. int m_iPktsCount; // number of payload packets
  125. int m_iBytesCount; // number of payload bytes
  126. void reset()
  127. {
  128. m_iPktsCount = 0;
  129. m_iBytesCount = 0;
  130. }
  131. Sample()
  132. : m_iPktsCount(0)
  133. , m_iBytesCount(0)
  134. {
  135. }
  136. Sample(int iPkts, int iBytes)
  137. : m_iPktsCount(iPkts)
  138. , m_iBytesCount(iBytes)
  139. {
  140. }
  141. Sample operator+(const Sample& other)
  142. {
  143. return Sample(m_iPktsCount + other.m_iPktsCount, m_iBytesCount + other.m_iBytesCount);
  144. }
  145. Sample& operator+=(const Sample& other)
  146. {
  147. *this = *this + other;
  148. return *this;
  149. }
  150. bool empty() const { return m_iPktsCount == 0; }
  151. };
  152. int incSampleIdx(int val, int inc = 1) const;
  153. Sample m_Samples[NUM_PERIODS];
  154. time_point m_tsFirstSampleTime; //< Start time of the first sameple.
  155. int m_iFirstSampleIdx; //< Index of the first sample.
  156. int m_iCurSampleIdx; //< Index of the current sample being collected.
  157. int m_iRateBps; // Input Rate in Bytes/sec
  158. };
  159. } // namespace srt
  160. #endif