window.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 - 2011, 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 01/22/2011
  42. modified by
  43. Haivision Systems Inc.
  44. *****************************************************************************/
  45. #ifndef INC_SRT_WINDOW_H
  46. #define INC_SRT_WINDOW_H
  47. #ifndef _WIN32
  48. #include <sys/time.h>
  49. #include <time.h>
  50. #endif
  51. #include "packet.h"
  52. #include "udt.h"
  53. namespace srt
  54. {
  55. namespace ACKWindowTools
  56. {
  57. struct Seq
  58. {
  59. int32_t iACKSeqNo; // Seq. No. of the ACK packet
  60. int32_t iACK; // Data packet Seq. No. carried by the ACK packet
  61. sync::steady_clock::time_point tsTimeStamp; // The timestamp when the ACK was sent
  62. };
  63. void store(Seq* r_aSeq, const size_t size, int& r_iHead, int& r_iTail, int32_t seq, int32_t ack);
  64. int acknowledge(Seq* r_aSeq, const size_t size, int& r_iHead, int& r_iTail, int32_t seq, int32_t& r_ack, const sync::steady_clock::time_point& currtime);
  65. }
  66. template <size_t SIZE>
  67. class CACKWindow
  68. {
  69. public:
  70. CACKWindow() :
  71. m_aSeq(),
  72. m_iHead(0),
  73. m_iTail(0)
  74. {
  75. m_aSeq[0].iACKSeqNo = SRT_SEQNO_NONE;
  76. }
  77. ~CACKWindow() {}
  78. /// Write an ACK record into the window.
  79. /// @param [in] seq Seq. No. of the ACK packet
  80. /// @param [in] ack Data packet Seq. No. carried by the ACK packet
  81. void store(int32_t seq, int32_t ack)
  82. {
  83. return ACKWindowTools::store(m_aSeq, SIZE, m_iHead, m_iTail, seq, ack);
  84. }
  85. /// Search the ACKACK "seq" in the window, find out the data packet "ack"
  86. /// and calculate RTT estimate based on the ACK/ACKACK pair
  87. /// @param [in] seq Seq. No. of the ACK packet carried within ACKACK
  88. /// @param [out] ack Acknowledged data packet Seq. No. from the ACK packet that matches the ACKACK
  89. /// @param [in] currtime The timestamp of ACKACK packet reception by the receiver
  90. /// @return RTT
  91. int acknowledge(int32_t seq, int32_t& r_ack, const sync::steady_clock::time_point& currtime)
  92. {
  93. return ACKWindowTools::acknowledge(m_aSeq, SIZE, m_iHead, m_iTail, seq, r_ack, currtime);
  94. }
  95. private:
  96. typedef ACKWindowTools::Seq Seq;
  97. Seq m_aSeq[SIZE];
  98. int m_iHead; // Pointer to the latest ACK record
  99. int m_iTail; // Pointer to the oldest ACK record
  100. private:
  101. CACKWindow(const CACKWindow&);
  102. CACKWindow& operator=(const CACKWindow&);
  103. };
  104. ////////////////////////////////////////////////////////////////////////////////
  105. class CPktTimeWindowTools
  106. {
  107. public:
  108. static int getPktRcvSpeed_in(const int* window, int* replica, const int* bytes, size_t asize, int& bytesps);
  109. static int getBandwidth_in(const int* window, int* replica, size_t psize);
  110. static void initializeWindowArrays(int* r_pktWindow, int* r_probeWindow, int* r_bytesWindow, size_t asize, size_t psize);
  111. };
  112. template <size_t ASIZE = 16, size_t PSIZE = 16>
  113. class CPktTimeWindow: CPktTimeWindowTools
  114. {
  115. public:
  116. CPktTimeWindow():
  117. m_aPktWindow(),
  118. m_aBytesWindow(),
  119. m_iPktWindowPtr(0),
  120. m_aProbeWindow(),
  121. m_iProbeWindowPtr(0),
  122. m_iLastSentTime(0),
  123. m_iMinPktSndInt(1000000),
  124. m_tsLastArrTime(sync::steady_clock::now()),
  125. m_tsCurrArrTime(),
  126. m_tsProbeTime(),
  127. m_Probe1Sequence(SRT_SEQNO_NONE)
  128. {
  129. // Exception: up to CUDT ctor
  130. sync::setupMutex(m_lockPktWindow, "PktWindow");
  131. sync::setupMutex(m_lockProbeWindow, "ProbeWindow");
  132. CPktTimeWindowTools::initializeWindowArrays(m_aPktWindow, m_aProbeWindow, m_aBytesWindow, ASIZE, PSIZE);
  133. }
  134. ~CPktTimeWindow()
  135. {
  136. }
  137. public:
  138. /// read the minimum packet sending interval.
  139. /// @return minimum packet sending interval (microseconds).
  140. int getMinPktSndInt() const { return m_iMinPktSndInt; }
  141. /// Calculate the packets arrival speed.
  142. /// @return Packet arrival speed (packets per second).
  143. int getPktRcvSpeed(int& w_bytesps) const
  144. {
  145. // Lock access to the packet Window
  146. sync::ScopedLock cg(m_lockPktWindow);
  147. int pktReplica[ASIZE]; // packet information window (inter-packet time)
  148. return getPktRcvSpeed_in(m_aPktWindow, pktReplica, m_aBytesWindow, ASIZE, (w_bytesps));
  149. }
  150. int getPktRcvSpeed() const
  151. {
  152. int bytesps;
  153. return getPktRcvSpeed((bytesps));
  154. }
  155. /// Estimate the bandwidth.
  156. /// @return Estimated bandwidth (packets per second).
  157. int getBandwidth() const
  158. {
  159. // Lock access to the packet Window
  160. sync::ScopedLock cg(m_lockProbeWindow);
  161. int probeReplica[PSIZE];
  162. return getBandwidth_in(m_aProbeWindow, probeReplica, PSIZE);
  163. }
  164. /// Record time information of a packet sending.
  165. /// @param currtime timestamp of the packet sending.
  166. void onPktSent(int currtime)
  167. {
  168. int interval = currtime - m_iLastSentTime;
  169. if ((interval < m_iMinPktSndInt) && (interval > 0))
  170. m_iMinPktSndInt = interval;
  171. m_iLastSentTime = currtime;
  172. }
  173. /// Record time information of an arrived packet.
  174. void onPktArrival(int pktsz = 0)
  175. {
  176. sync::ScopedLock cg(m_lockPktWindow);
  177. m_tsCurrArrTime = sync::steady_clock::now();
  178. // record the packet interval between the current and the last one
  179. m_aPktWindow[m_iPktWindowPtr] = (int) sync::count_microseconds(m_tsCurrArrTime - m_tsLastArrTime);
  180. m_aBytesWindow[m_iPktWindowPtr] = pktsz;
  181. // the window is logically circular
  182. ++ m_iPktWindowPtr;
  183. if (m_iPktWindowPtr == ASIZE)
  184. m_iPktWindowPtr = 0;
  185. // remember last packet arrival time
  186. m_tsLastArrTime = m_tsCurrArrTime;
  187. }
  188. /// Shortcut to test a packet for possible probe 1 or 2
  189. void probeArrival(const CPacket& pkt, bool unordered)
  190. {
  191. const int inorder16 = pkt.m_iSeqNo & PUMASK_SEQNO_PROBE;
  192. // for probe1, we want 16th packet
  193. if (inorder16 == 0)
  194. {
  195. probe1Arrival(pkt, unordered);
  196. }
  197. if (unordered)
  198. return;
  199. // for probe2, we want 17th packet
  200. if (inorder16 == 1)
  201. {
  202. probe2Arrival(pkt);
  203. }
  204. }
  205. /// Record the arrival time of the first probing packet.
  206. void probe1Arrival(const CPacket& pkt, bool unordered)
  207. {
  208. if (unordered && pkt.m_iSeqNo == m_Probe1Sequence)
  209. {
  210. // Reset the starting probe into "undefined", when
  211. // a packet has come as retransmitted before the
  212. // measurement at arrival of 17th could be taken.
  213. m_Probe1Sequence = SRT_SEQNO_NONE;
  214. return;
  215. }
  216. m_tsProbeTime = sync::steady_clock::now();
  217. m_Probe1Sequence = pkt.m_iSeqNo; // Record the sequence where 16th packet probe was taken
  218. }
  219. /// Record the arrival time of the second probing packet and the interval between packet pairs.
  220. void probe2Arrival(const CPacket& pkt)
  221. {
  222. // Reject probes that don't refer to the very next packet
  223. // towards the one that was lately notified by probe1Arrival.
  224. // Otherwise the result can be stupid.
  225. // Simply, in case when this wasn't called exactly for the
  226. // expected packet pair, behave as if the 17th packet was lost.
  227. // no start point yet (or was reset) OR not very next packet
  228. if (m_Probe1Sequence == SRT_SEQNO_NONE || CSeqNo::incseq(m_Probe1Sequence) != pkt.m_iSeqNo)
  229. return;
  230. // Grab the current time before trying to acquire
  231. // a mutex. This might add extra delay and therefore
  232. // screw up the measurement.
  233. const sync::steady_clock::time_point now = sync::steady_clock::now();
  234. // Lock access to the packet Window
  235. sync::ScopedLock cg(m_lockProbeWindow);
  236. m_tsCurrArrTime = now;
  237. // Reset the starting probe to prevent checking if the
  238. // measurement was already taken.
  239. m_Probe1Sequence = SRT_SEQNO_NONE;
  240. // record the probing packets interval
  241. // Adjust the time for what a complete packet would have take
  242. const int64_t timediff = sync::count_microseconds(m_tsCurrArrTime - m_tsProbeTime);
  243. const int64_t timediff_times_pl_size = timediff * CPacket::SRT_MAX_PAYLOAD_SIZE;
  244. // Let's take it simpler than it is coded here:
  245. // (stating that a packet has never zero size)
  246. //
  247. // probe_case = (now - previous_packet_time) * SRT_MAX_PAYLOAD_SIZE / pktsz;
  248. //
  249. // Meaning: if the packet is fully packed, probe_case = timediff.
  250. // Otherwise the timediff will be "converted" to a time that a fully packed packet "would take",
  251. // provided the arrival time is proportional to the payload size and skipping
  252. // the ETH+IP+UDP+SRT header part elliminates the constant packet delivery time influence.
  253. //
  254. const size_t pktsz = pkt.getLength();
  255. m_aProbeWindow[m_iProbeWindowPtr] = pktsz ? int(timediff_times_pl_size / pktsz) : int(timediff);
  256. // OLD CODE BEFORE BSTATS:
  257. // record the probing packets interval
  258. // m_aProbeWindow[m_iProbeWindowPtr] = int(m_tsCurrArrTime - m_tsProbeTime);
  259. // the window is logically circular
  260. ++ m_iProbeWindowPtr;
  261. if (m_iProbeWindowPtr == PSIZE)
  262. m_iProbeWindowPtr = 0;
  263. }
  264. private:
  265. int m_aPktWindow[ASIZE]; // Packet information window (inter-packet time)
  266. int m_aBytesWindow[ASIZE];
  267. int m_iPktWindowPtr; // Position pointer of the packet info. window
  268. mutable sync::Mutex m_lockPktWindow; // Used to synchronize access to the packet window
  269. int m_aProbeWindow[PSIZE]; // Record inter-packet time for probing packet pairs
  270. int m_iProbeWindowPtr; // Position pointer to the probing window
  271. mutable sync::Mutex m_lockProbeWindow; // Used to synchronize access to the probe window
  272. int m_iLastSentTime; // Last packet sending time
  273. int m_iMinPktSndInt; // Minimum packet sending interval
  274. sync::steady_clock::time_point m_tsLastArrTime; // Last packet arrival time
  275. sync::steady_clock::time_point m_tsCurrArrTime; // Current packet arrival time
  276. sync::steady_clock::time_point m_tsProbeTime; // Arrival time of the first probing packet
  277. int32_t m_Probe1Sequence; // Sequence number for which the arrival time was notified
  278. private:
  279. CPktTimeWindow(const CPktTimeWindow&);
  280. CPktTimeWindow &operator=(const CPktTimeWindow&);
  281. };
  282. } // namespace srt
  283. #endif