buffer_rcv.cpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  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. #include <cmath>
  40. #include <limits>
  41. #include "buffer_rcv.h"
  42. #include "logging.h"
  43. using namespace std;
  44. using namespace srt::sync;
  45. using namespace srt_logging;
  46. namespace srt_logging
  47. {
  48. extern Logger brlog;
  49. }
  50. #define rbuflog brlog
  51. namespace srt {
  52. namespace {
  53. struct ScopedLog
  54. {
  55. ScopedLog() {}
  56. ~ScopedLog()
  57. {
  58. LOGC(rbuflog.Warn, log << ss.str());
  59. }
  60. stringstream ss;
  61. };
  62. #define IF_RCVBUF_DEBUG(instr) (void)0
  63. // Check if iFirstNonreadPos is in range [iStartPos, (iStartPos + iMaxPosOff) % iSize].
  64. // The right edge is included because we expect iFirstNonreadPos to be
  65. // right after the last valid packet position if all packets are available.
  66. bool isInRange(int iStartPos, int iMaxPosOff, size_t iSize, int iFirstNonreadPos)
  67. {
  68. if (iFirstNonreadPos == iStartPos)
  69. return true;
  70. const int iLastPos = (iStartPos + iMaxPosOff) % iSize;
  71. const bool isOverrun = iLastPos < iStartPos;
  72. if (isOverrun)
  73. return iFirstNonreadPos > iStartPos || iFirstNonreadPos <= iLastPos;
  74. return iFirstNonreadPos > iStartPos && iFirstNonreadPos <= iLastPos;
  75. }
  76. }
  77. /*
  78. * RcvBufferNew (circular buffer):
  79. *
  80. * |<------------------- m_iSize ----------------------------->|
  81. * | |<----------- m_iMaxPosOff ------------>| |
  82. * | | | |
  83. * +---+---+---+---+---+---+---+---+---+---+---+---+---+ +---+
  84. * | 0 | 0 | 1 | 1 | 1 | 0 | 1 | 1 | 1 | 1 | 0 | 1 | 0 |...| 0 | m_pUnit[]
  85. * +---+---+---+---+---+---+---+---+---+---+---+---+---+ +---+
  86. * | |
  87. * | |__last pkt received
  88. * |___ m_iStartPos: first message to read
  89. *
  90. * m_pUnit[i]->m_iFlag: 0:free, 1:good, 2:passack, 3:dropped
  91. *
  92. * thread safety:
  93. * m_iStartPos: CUDT::m_RecvLock
  94. * m_iLastAckPos: CUDT::m_AckLock
  95. * m_iMaxPosOff: none? (modified on add and ack
  96. */
  97. CRcvBuffer::CRcvBuffer(int initSeqNo, size_t size, CUnitQueue* unitqueue, bool bMessageAPI)
  98. : m_entries(size)
  99. , m_szSize(size) // TODO: maybe just use m_entries.size()
  100. , m_pUnitQueue(unitqueue)
  101. , m_iStartSeqNo(initSeqNo)
  102. , m_iStartPos(0)
  103. , m_iFirstNonreadPos(0)
  104. , m_iMaxPosOff(0)
  105. , m_iNotch(0)
  106. , m_numOutOfOrderPackets(0)
  107. , m_iFirstReadableOutOfOrder(-1)
  108. , m_bPeerRexmitFlag(true)
  109. , m_bMessageAPI(bMessageAPI)
  110. , m_iBytesCount(0)
  111. , m_iPktsCount(0)
  112. , m_uAvgPayloadSz(SRT_LIVE_DEF_PLSIZE)
  113. {
  114. SRT_ASSERT(size < size_t(std::numeric_limits<int>::max())); // All position pointers are integers
  115. }
  116. CRcvBuffer::~CRcvBuffer()
  117. {
  118. // Can be optimized by only iterating m_iMaxPosOff from m_iStartPos.
  119. for (FixedArray<Entry>::iterator it = m_entries.begin(); it != m_entries.end(); ++it)
  120. {
  121. if (!it->pUnit)
  122. continue;
  123. m_pUnitQueue->makeUnitFree(it->pUnit);
  124. it->pUnit = NULL;
  125. }
  126. }
  127. int CRcvBuffer::insert(CUnit* unit)
  128. {
  129. SRT_ASSERT(unit != NULL);
  130. const int32_t seqno = unit->m_Packet.getSeqNo();
  131. const int offset = CSeqNo::seqoff(m_iStartSeqNo, seqno);
  132. IF_RCVBUF_DEBUG(ScopedLog scoped_log);
  133. IF_RCVBUF_DEBUG(scoped_log.ss << "CRcvBuffer::insert: seqno " << seqno);
  134. IF_RCVBUF_DEBUG(scoped_log.ss << " msgno " << unit->m_Packet.getMsgSeq(m_bPeerRexmitFlag));
  135. IF_RCVBUF_DEBUG(scoped_log.ss << " m_iStartSeqNo " << m_iStartSeqNo << " offset " << offset);
  136. if (offset < 0)
  137. {
  138. IF_RCVBUF_DEBUG(scoped_log.ss << " returns -2");
  139. return -2;
  140. }
  141. if (offset >= (int)capacity())
  142. {
  143. IF_RCVBUF_DEBUG(scoped_log.ss << " returns -3");
  144. return -3;
  145. }
  146. // TODO: Don't do assert here. Process this situation somehow.
  147. // If >= 2, then probably there is a long gap, and buffer needs to be reset.
  148. SRT_ASSERT((m_iStartPos + offset) / m_szSize < 2);
  149. const int pos = (m_iStartPos + offset) % m_szSize;
  150. if (offset >= m_iMaxPosOff)
  151. m_iMaxPosOff = offset + 1;
  152. // Packet already exists
  153. SRT_ASSERT(pos >= 0 && pos < int(m_szSize));
  154. if (m_entries[pos].status != EntryState_Empty)
  155. {
  156. IF_RCVBUF_DEBUG(scoped_log.ss << " returns -1");
  157. return -1;
  158. }
  159. SRT_ASSERT(m_entries[pos].pUnit == NULL);
  160. m_pUnitQueue->makeUnitTaken(unit);
  161. m_entries[pos].pUnit = unit;
  162. m_entries[pos].status = EntryState_Avail;
  163. countBytes(1, (int)unit->m_Packet.getLength());
  164. // If packet "in order" flag is zero, it can be read out of order.
  165. // With TSBPD enabled packets are always assumed in order (the flag is ignored).
  166. if (!m_tsbpd.isEnabled() && m_bMessageAPI && !unit->m_Packet.getMsgOrderFlag())
  167. {
  168. ++m_numOutOfOrderPackets;
  169. onInsertNotInOrderPacket(pos);
  170. }
  171. updateNonreadPos();
  172. IF_RCVBUF_DEBUG(scoped_log.ss << " returns 0 (OK)");
  173. return 0;
  174. }
  175. int CRcvBuffer::dropUpTo(int32_t seqno)
  176. {
  177. IF_RCVBUF_DEBUG(ScopedLog scoped_log);
  178. IF_RCVBUF_DEBUG(scoped_log.ss << "CRcvBuffer::dropUpTo: seqno " << seqno << " m_iStartSeqNo " << m_iStartSeqNo);
  179. int len = CSeqNo::seqoff(m_iStartSeqNo, seqno);
  180. if (len <= 0)
  181. {
  182. IF_RCVBUF_DEBUG(scoped_log.ss << ". Nothing to drop.");
  183. return 0;
  184. }
  185. m_iMaxPosOff -= len;
  186. if (m_iMaxPosOff < 0)
  187. m_iMaxPosOff = 0;
  188. const int iDropCnt = len;
  189. while (len > 0)
  190. {
  191. dropUnitInPos(m_iStartPos);
  192. m_entries[m_iStartPos].status = EntryState_Empty;
  193. SRT_ASSERT(m_entries[m_iStartPos].pUnit == NULL && m_entries[m_iStartPos].status == EntryState_Empty);
  194. m_iStartPos = incPos(m_iStartPos);
  195. --len;
  196. }
  197. // Update positions
  198. m_iStartSeqNo = seqno;
  199. // Move forward if there are "read/drop" entries.
  200. releaseNextFillerEntries();
  201. // If the nonread position is now behind the starting position, set it to the starting position and update.
  202. // Preceding packets were likely missing, and the non read position can probably be moved further now.
  203. if (CSeqNo::seqcmp(m_iFirstNonreadPos, m_iStartPos) < 0)
  204. {
  205. m_iFirstNonreadPos = m_iStartPos;
  206. updateNonreadPos();
  207. }
  208. if (!m_tsbpd.isEnabled() && m_bMessageAPI)
  209. updateFirstReadableOutOfOrder();
  210. return iDropCnt;
  211. }
  212. int CRcvBuffer::dropAll()
  213. {
  214. if (empty())
  215. return 0;
  216. const int end_seqno = CSeqNo::incseq(m_iStartSeqNo, m_iMaxPosOff);
  217. return dropUpTo(end_seqno);
  218. }
  219. int CRcvBuffer::dropMessage(int32_t seqnolo, int32_t seqnohi, int32_t msgno, DropActionIfExists actionOnExisting)
  220. {
  221. IF_RCVBUF_DEBUG(ScopedLog scoped_log);
  222. IF_RCVBUF_DEBUG(scoped_log.ss << "CRcvBuffer::dropMessage(): %(" << seqnolo << " - " << seqnohi << ")"
  223. << " #" << msgno << " actionOnExisting=" << actionOnExisting << " m_iStartSeqNo=%"
  224. << m_iStartSeqNo);
  225. // Drop by packet seqno range to also wipe those packets that do not exist in the buffer.
  226. const int offset_a = CSeqNo::seqoff(m_iStartSeqNo, seqnolo);
  227. const int offset_b = CSeqNo::seqoff(m_iStartSeqNo, seqnohi);
  228. if (offset_b < 0)
  229. {
  230. LOGC(rbuflog.Debug, log << "CRcvBuffer.dropMessage(): nothing to drop. Requested [" << seqnolo << "; "
  231. << seqnohi << "]. Buffer start " << m_iStartSeqNo << ".");
  232. return 0;
  233. }
  234. const bool bKeepExisting = (actionOnExisting == KEEP_EXISTING);
  235. int minDroppedOffset = -1;
  236. int iDropCnt = 0;
  237. const int start_off = max(0, offset_a);
  238. const int start_pos = incPos(m_iStartPos, start_off);
  239. const int end_off = min((int) m_szSize - 1, offset_b + 1);
  240. const int end_pos = incPos(m_iStartPos, end_off);
  241. bool bDropByMsgNo = msgno > SRT_MSGNO_CONTROL; // Excluding both SRT_MSGNO_NONE (-1) and SRT_MSGNO_CONTROL (0).
  242. for (int i = start_pos; i != end_pos; i = incPos(i))
  243. {
  244. // Check if the unit was already dropped earlier.
  245. if (m_entries[i].status == EntryState_Drop)
  246. continue;
  247. if (m_entries[i].pUnit)
  248. {
  249. const PacketBoundary bnd = packetAt(i).getMsgBoundary();
  250. // Don't drop messages, if all its packets are already in the buffer.
  251. // TODO: Don't drop a several-packet message if all packets are in the buffer.
  252. if (bKeepExisting && bnd == PB_SOLO)
  253. {
  254. bDropByMsgNo = false; // Solo packet, don't search for the rest of the message.
  255. LOGC(rbuflog.Debug,
  256. log << "CRcvBuffer::dropMessage(): Skipped dropping an existing SOLO packet %"
  257. << packetAt(i).getSeqNo() << ".");
  258. continue;
  259. }
  260. const int32_t msgseq = packetAt(i).getMsgSeq(m_bPeerRexmitFlag);
  261. if (msgno > SRT_MSGNO_CONTROL && msgseq != msgno)
  262. {
  263. LOGC(rbuflog.Warn, log << "CRcvBuffer.dropMessage(): Packet seqno %" << packetAt(i).getSeqNo() << " has msgno " << msgseq << " differs from requested " << msgno);
  264. }
  265. if (bDropByMsgNo && bnd == PB_FIRST)
  266. {
  267. // First packet of the message is about to be dropped. That was the only reason to search for msgno.
  268. bDropByMsgNo = false;
  269. }
  270. }
  271. dropUnitInPos(i);
  272. ++iDropCnt;
  273. m_entries[i].status = EntryState_Drop;
  274. if (minDroppedOffset == -1)
  275. minDroppedOffset = offPos(m_iStartPos, i);
  276. }
  277. if (bDropByMsgNo)
  278. {
  279. // If msgno is specified, potentially not the whole message was dropped using seqno range.
  280. // The sender might have removed the first packets of the message, and thus @a seqnolo may point to a packet in the middle.
  281. // The sender should have the last packet of the message it is requesting to be dropped.
  282. // Therefore we don't search forward, but need to check earlier packets in the RCV buffer.
  283. // Try to drop by the message number in case the message starts earlier than @a seqnolo.
  284. const int stop_pos = decPos(m_iStartPos);
  285. for (int i = start_pos; i != stop_pos; i = decPos(i))
  286. {
  287. // Can't drop if message number is not known.
  288. if (!m_entries[i].pUnit) // also dropped earlier.
  289. continue;
  290. const PacketBoundary bnd = packetAt(i).getMsgBoundary();
  291. const int32_t msgseq = packetAt(i).getMsgSeq(m_bPeerRexmitFlag);
  292. if (msgseq != msgno)
  293. break;
  294. if (bKeepExisting && bnd == PB_SOLO)
  295. {
  296. LOGC(rbuflog.Debug,
  297. log << "CRcvBuffer::dropMessage(): Skipped dropping an existing SOLO message packet %"
  298. << packetAt(i).getSeqNo() << ".");
  299. break;
  300. }
  301. ++iDropCnt;
  302. dropUnitInPos(i);
  303. m_entries[i].status = EntryState_Drop;
  304. // As the search goes backward, i is always earlier than minDroppedOffset.
  305. minDroppedOffset = offPos(m_iStartPos, i);
  306. // Break the loop if the start of the message has been found. No need to search further.
  307. if (bnd == PB_FIRST)
  308. break;
  309. }
  310. IF_RCVBUF_DEBUG(scoped_log.ss << " iDropCnt " << iDropCnt);
  311. }
  312. // Check if units before m_iFirstNonreadPos are dropped.
  313. const bool needUpdateNonreadPos = (minDroppedOffset != -1 && minDroppedOffset <= getRcvDataSize());
  314. releaseNextFillerEntries();
  315. if (needUpdateNonreadPos)
  316. {
  317. m_iFirstNonreadPos = m_iStartPos;
  318. updateNonreadPos();
  319. }
  320. if (!m_tsbpd.isEnabled() && m_bMessageAPI)
  321. {
  322. if (!checkFirstReadableOutOfOrder())
  323. m_iFirstReadableOutOfOrder = -1;
  324. updateFirstReadableOutOfOrder();
  325. }
  326. return iDropCnt;
  327. }
  328. int CRcvBuffer::readMessage(char* data, size_t len, SRT_MSGCTRL* msgctrl)
  329. {
  330. const bool canReadInOrder = hasReadableInorderPkts();
  331. if (!canReadInOrder && m_iFirstReadableOutOfOrder < 0)
  332. {
  333. LOGC(rbuflog.Warn, log << "CRcvBuffer.readMessage(): nothing to read. Ignored isRcvDataReady() result?");
  334. return 0;
  335. }
  336. const int readPos = canReadInOrder ? m_iStartPos : m_iFirstReadableOutOfOrder;
  337. IF_RCVBUF_DEBUG(ScopedLog scoped_log);
  338. IF_RCVBUF_DEBUG(scoped_log.ss << "CRcvBuffer::readMessage. m_iStartSeqNo " << m_iStartSeqNo << " m_iStartPos " << m_iStartPos << " readPos " << readPos);
  339. size_t remain = len;
  340. char* dst = data;
  341. int pkts_read = 0;
  342. int bytes_extracted = 0; // The total number of bytes extracted from the buffer.
  343. const bool updateStartPos = (readPos == m_iStartPos); // Indicates if the m_iStartPos can be changed
  344. for (int i = readPos;; i = incPos(i))
  345. {
  346. SRT_ASSERT(m_entries[i].pUnit);
  347. if (!m_entries[i].pUnit)
  348. {
  349. LOGC(rbuflog.Error, log << "CRcvBuffer::readMessage(): null packet encountered.");
  350. break;
  351. }
  352. const CPacket& packet = packetAt(i);
  353. const size_t pktsize = packet.getLength();
  354. const int32_t pktseqno = packet.getSeqNo();
  355. // unitsize can be zero
  356. const size_t unitsize = std::min(remain, pktsize);
  357. memcpy(dst, packet.m_pcData, unitsize);
  358. remain -= unitsize;
  359. dst += unitsize;
  360. ++pkts_read;
  361. bytes_extracted += (int) pktsize;
  362. if (m_tsbpd.isEnabled())
  363. updateTsbPdTimeBase(packet.getMsgTimeStamp());
  364. if (m_numOutOfOrderPackets && !packet.getMsgOrderFlag())
  365. --m_numOutOfOrderPackets;
  366. const bool pbLast = packet.getMsgBoundary() & PB_LAST;
  367. if (msgctrl && (packet.getMsgBoundary() & PB_FIRST))
  368. {
  369. msgctrl->msgno = packet.getMsgSeq(m_bPeerRexmitFlag);
  370. }
  371. if (msgctrl && pbLast)
  372. {
  373. msgctrl->srctime = count_microseconds(getPktTsbPdTime(packet.getMsgTimeStamp()).time_since_epoch());
  374. }
  375. if (msgctrl)
  376. msgctrl->pktseq = pktseqno;
  377. releaseUnitInPos(i);
  378. if (updateStartPos)
  379. {
  380. m_iStartPos = incPos(i);
  381. --m_iMaxPosOff;
  382. SRT_ASSERT(m_iMaxPosOff >= 0);
  383. m_iStartSeqNo = CSeqNo::incseq(pktseqno);
  384. }
  385. else
  386. {
  387. // If out of order, only mark it read.
  388. m_entries[i].status = EntryState_Read;
  389. }
  390. if (pbLast)
  391. {
  392. if (readPos == m_iFirstReadableOutOfOrder)
  393. m_iFirstReadableOutOfOrder = -1;
  394. break;
  395. }
  396. }
  397. countBytes(-pkts_read, -bytes_extracted);
  398. releaseNextFillerEntries();
  399. if (!isInRange(m_iStartPos, m_iMaxPosOff, m_szSize, m_iFirstNonreadPos))
  400. {
  401. m_iFirstNonreadPos = m_iStartPos;
  402. //updateNonreadPos();
  403. }
  404. if (!m_tsbpd.isEnabled())
  405. // We need updateFirstReadableOutOfOrder() here even if we are reading inorder,
  406. // incase readable inorder packets are all read out.
  407. updateFirstReadableOutOfOrder();
  408. const int bytes_read = int(dst - data);
  409. if (bytes_read < bytes_extracted)
  410. {
  411. LOGC(rbuflog.Error, log << "readMessage: small dst buffer, copied only " << bytes_read << "/" << bytes_extracted << " bytes.");
  412. }
  413. IF_RCVBUF_DEBUG(scoped_log.ss << " pldi64 " << *reinterpret_cast<uint64_t*>(data));
  414. return bytes_read;
  415. }
  416. namespace {
  417. /// @brief Writes bytes to file stream.
  418. /// @param data pointer to data to write.
  419. /// @param len the number of bytes to write
  420. /// @param dst_offset ignored
  421. /// @param arg a void pointer to the fstream to write to.
  422. /// @return true on success, false on failure
  423. bool writeBytesToFile(char* data, int len, int dst_offset SRT_ATR_UNUSED, void* arg)
  424. {
  425. fstream* pofs = reinterpret_cast<fstream*>(arg);
  426. pofs->write(data, len);
  427. return !pofs->fail();
  428. }
  429. /// @brief Copies bytes to the destination buffer.
  430. /// @param data pointer to data to copy.
  431. /// @param len the number of bytes to copy
  432. /// @param dst_offset offset in destination buffer
  433. /// @param arg A pointer to the destination buffer
  434. /// @return true on success, false on failure
  435. bool copyBytesToBuf(char* data, int len, int dst_offset, void* arg)
  436. {
  437. char* dst = reinterpret_cast<char*>(arg) + dst_offset;
  438. memcpy(dst, data, len);
  439. return true;
  440. }
  441. }
  442. int CRcvBuffer::readBufferTo(int len, copy_to_dst_f funcCopyToDst, void* arg)
  443. {
  444. int p = m_iStartPos;
  445. const int end_pos = m_iFirstNonreadPos;
  446. const bool bTsbPdEnabled = m_tsbpd.isEnabled();
  447. const steady_clock::time_point now = (bTsbPdEnabled ? steady_clock::now() : steady_clock::time_point());
  448. int rs = len;
  449. while ((p != end_pos) && (rs > 0))
  450. {
  451. if (!m_entries[p].pUnit)
  452. {
  453. p = incPos(p);
  454. LOGC(rbuflog.Error, log << "readBufferTo: IPE: NULL unit found in file transmission");
  455. return -1;
  456. }
  457. const srt::CPacket& pkt = packetAt(p);
  458. if (bTsbPdEnabled)
  459. {
  460. const steady_clock::time_point tsPlay = getPktTsbPdTime(pkt.getMsgTimeStamp());
  461. HLOGC(rbuflog.Debug,
  462. log << "readBuffer: check if time to play:"
  463. << " NOW=" << FormatTime(now)
  464. << " PKT TS=" << FormatTime(tsPlay));
  465. if ((tsPlay > now))
  466. break; /* too early for this unit, return whatever was copied */
  467. }
  468. const int pktlen = (int)pkt.getLength();
  469. const int remain_pktlen = pktlen - m_iNotch;
  470. const int unitsize = std::min(remain_pktlen, rs);
  471. if (!funcCopyToDst(pkt.m_pcData + m_iNotch, unitsize, len - rs, arg))
  472. break;
  473. if (rs >= remain_pktlen)
  474. {
  475. releaseUnitInPos(p);
  476. p = incPos(p);
  477. m_iNotch = 0;
  478. m_iStartPos = p;
  479. --m_iMaxPosOff;
  480. SRT_ASSERT(m_iMaxPosOff >= 0);
  481. m_iStartSeqNo = CSeqNo::incseq(m_iStartSeqNo);
  482. }
  483. else
  484. m_iNotch += rs;
  485. rs -= unitsize;
  486. }
  487. const int iBytesRead = len - rs;
  488. /* we removed acked bytes form receive buffer */
  489. countBytes(-1, -iBytesRead);
  490. // Update positions
  491. // Set nonread position to the starting position before updating,
  492. // because start position was increased, and preceding packets are invalid.
  493. if (!isInRange(m_iStartPos, m_iMaxPosOff, m_szSize, m_iFirstNonreadPos))
  494. {
  495. m_iFirstNonreadPos = m_iStartPos;
  496. }
  497. if (iBytesRead == 0)
  498. {
  499. LOGC(rbuflog.Error, log << "readBufferTo: 0 bytes read. m_iStartPos=" << m_iStartPos << ", m_iFirstNonreadPos=" << m_iFirstNonreadPos);
  500. }
  501. return iBytesRead;
  502. }
  503. int CRcvBuffer::readBuffer(char* dst, int len)
  504. {
  505. return readBufferTo(len, copyBytesToBuf, reinterpret_cast<void*>(dst));
  506. }
  507. int CRcvBuffer::readBufferToFile(fstream& ofs, int len)
  508. {
  509. return readBufferTo(len, writeBytesToFile, reinterpret_cast<void*>(&ofs));
  510. }
  511. bool CRcvBuffer::hasAvailablePackets() const
  512. {
  513. return hasReadableInorderPkts() || (m_numOutOfOrderPackets > 0 && m_iFirstReadableOutOfOrder != -1);
  514. }
  515. int CRcvBuffer::getRcvDataSize() const
  516. {
  517. if (m_iFirstNonreadPos >= m_iStartPos)
  518. return m_iFirstNonreadPos - m_iStartPos;
  519. return int(m_szSize + m_iFirstNonreadPos - m_iStartPos);
  520. }
  521. int CRcvBuffer::getTimespan_ms() const
  522. {
  523. if (!m_tsbpd.isEnabled())
  524. return 0;
  525. if (m_iMaxPosOff == 0)
  526. return 0;
  527. int lastpos = incPos(m_iStartPos, m_iMaxPosOff - 1);
  528. // Normally the last position should always be non empty
  529. // if TSBPD is enabled (reading out of order is not allowed).
  530. // However if decryption of the last packet fails, it may be dropped
  531. // from the buffer (AES-GCM), and the position will be empty.
  532. SRT_ASSERT(m_entries[lastpos].pUnit != NULL || m_entries[lastpos].status == EntryState_Drop);
  533. while (m_entries[lastpos].pUnit == NULL && lastpos != m_iStartPos)
  534. {
  535. lastpos = decPos(lastpos);
  536. }
  537. if (m_entries[lastpos].pUnit == NULL)
  538. return 0;
  539. int startpos = m_iStartPos;
  540. while (m_entries[startpos].pUnit == NULL && startpos != lastpos)
  541. {
  542. startpos = incPos(startpos);
  543. }
  544. if (m_entries[startpos].pUnit == NULL)
  545. return 0;
  546. const steady_clock::time_point startstamp =
  547. getPktTsbPdTime(packetAt(startpos).getMsgTimeStamp());
  548. const steady_clock::time_point endstamp = getPktTsbPdTime(packetAt(lastpos).getMsgTimeStamp());
  549. if (endstamp < startstamp)
  550. return 0;
  551. // One millisecond is added as a duration of a packet in the buffer.
  552. // If there is only one packet in the buffer, one millisecond is returned.
  553. return static_cast<int>(count_milliseconds(endstamp - startstamp) + 1);
  554. }
  555. int CRcvBuffer::getRcvDataSize(int& bytes, int& timespan) const
  556. {
  557. ScopedLock lck(m_BytesCountLock);
  558. bytes = m_iBytesCount;
  559. timespan = getTimespan_ms();
  560. return m_iPktsCount;
  561. }
  562. CRcvBuffer::PacketInfo CRcvBuffer::getFirstValidPacketInfo() const
  563. {
  564. const int end_pos = incPos(m_iStartPos, m_iMaxPosOff);
  565. for (int i = m_iStartPos; i != end_pos; i = incPos(i))
  566. {
  567. // TODO: Maybe check status?
  568. if (!m_entries[i].pUnit)
  569. continue;
  570. const CPacket& packet = packetAt(i);
  571. const PacketInfo info = { packet.getSeqNo(), i != m_iStartPos, getPktTsbPdTime(packet.getMsgTimeStamp()) };
  572. return info;
  573. }
  574. const PacketInfo info = { -1, false, time_point() };
  575. return info;
  576. }
  577. std::pair<int, int> CRcvBuffer::getAvailablePacketsRange() const
  578. {
  579. const int seqno_last = CSeqNo::incseq(m_iStartSeqNo, (int) countReadable());
  580. return std::pair<int, int>(m_iStartSeqNo, seqno_last);
  581. }
  582. size_t CRcvBuffer::countReadable() const
  583. {
  584. if (m_iFirstNonreadPos >= m_iStartPos)
  585. return m_iFirstNonreadPos - m_iStartPos;
  586. return m_szSize + m_iFirstNonreadPos - m_iStartPos;
  587. }
  588. bool CRcvBuffer::isRcvDataReady(time_point time_now) const
  589. {
  590. const bool haveInorderPackets = hasReadableInorderPkts();
  591. if (!m_tsbpd.isEnabled())
  592. {
  593. if (haveInorderPackets)
  594. return true;
  595. SRT_ASSERT((!m_bMessageAPI && m_numOutOfOrderPackets == 0) || m_bMessageAPI);
  596. return (m_numOutOfOrderPackets > 0 && m_iFirstReadableOutOfOrder != -1);
  597. }
  598. if (!haveInorderPackets)
  599. return false;
  600. const PacketInfo info = getFirstValidPacketInfo();
  601. return info.tsbpd_time <= time_now;
  602. }
  603. CRcvBuffer::PacketInfo CRcvBuffer::getFirstReadablePacketInfo(time_point time_now) const
  604. {
  605. const PacketInfo unreadableInfo = {SRT_SEQNO_NONE, false, time_point()};
  606. const bool hasInorderPackets = hasReadableInorderPkts();
  607. if (!m_tsbpd.isEnabled())
  608. {
  609. if (hasInorderPackets)
  610. {
  611. const CPacket& packet = packetAt(m_iStartPos);
  612. const PacketInfo info = {packet.getSeqNo(), false, time_point()};
  613. return info;
  614. }
  615. SRT_ASSERT((!m_bMessageAPI && m_numOutOfOrderPackets == 0) || m_bMessageAPI);
  616. if (m_iFirstReadableOutOfOrder >= 0)
  617. {
  618. SRT_ASSERT(m_numOutOfOrderPackets > 0);
  619. const CPacket& packet = packetAt(m_iFirstReadableOutOfOrder);
  620. const PacketInfo info = {packet.getSeqNo(), true, time_point()};
  621. return info;
  622. }
  623. return unreadableInfo;
  624. }
  625. if (!hasInorderPackets)
  626. return unreadableInfo;
  627. const PacketInfo info = getFirstValidPacketInfo();
  628. if (info.tsbpd_time <= time_now)
  629. return info;
  630. else
  631. return unreadableInfo;
  632. }
  633. void CRcvBuffer::countBytes(int pkts, int bytes)
  634. {
  635. ScopedLock lock(m_BytesCountLock);
  636. m_iBytesCount += bytes; // added or removed bytes from rcv buffer
  637. m_iPktsCount += pkts;
  638. if (bytes > 0) // Assuming one pkt when adding bytes
  639. m_uAvgPayloadSz = avg_iir<100>(m_uAvgPayloadSz, (unsigned) bytes);
  640. }
  641. void CRcvBuffer::releaseUnitInPos(int pos)
  642. {
  643. CUnit* tmp = m_entries[pos].pUnit;
  644. m_entries[pos] = Entry(); // pUnit = NULL; status = Empty
  645. if (tmp != NULL)
  646. m_pUnitQueue->makeUnitFree(tmp);
  647. }
  648. bool CRcvBuffer::dropUnitInPos(int pos)
  649. {
  650. if (!m_entries[pos].pUnit)
  651. return false;
  652. if (m_tsbpd.isEnabled())
  653. {
  654. updateTsbPdTimeBase(packetAt(pos).getMsgTimeStamp());
  655. }
  656. else if (m_bMessageAPI && !packetAt(pos).getMsgOrderFlag())
  657. {
  658. --m_numOutOfOrderPackets;
  659. if (pos == m_iFirstReadableOutOfOrder)
  660. m_iFirstReadableOutOfOrder = -1;
  661. }
  662. releaseUnitInPos(pos);
  663. return true;
  664. }
  665. void CRcvBuffer::releaseNextFillerEntries()
  666. {
  667. int pos = m_iStartPos;
  668. while (m_entries[pos].status == EntryState_Read || m_entries[pos].status == EntryState_Drop)
  669. {
  670. m_iStartSeqNo = CSeqNo::incseq(m_iStartSeqNo);
  671. releaseUnitInPos(pos);
  672. pos = incPos(pos);
  673. m_iStartPos = pos;
  674. --m_iMaxPosOff;
  675. if (m_iMaxPosOff < 0)
  676. m_iMaxPosOff = 0;
  677. }
  678. }
  679. // TODO: Is this function complete? There are some comments left inside.
  680. void CRcvBuffer::updateNonreadPos()
  681. {
  682. if (m_iMaxPosOff == 0)
  683. return;
  684. const int end_pos = incPos(m_iStartPos, m_iMaxPosOff); // The empty position right after the last valid entry.
  685. int pos = m_iFirstNonreadPos;
  686. while (m_entries[pos].pUnit && m_entries[pos].status == EntryState_Avail)
  687. {
  688. if (m_bMessageAPI && (packetAt(pos).getMsgBoundary() & PB_FIRST) == 0)
  689. break;
  690. for (int i = pos; i != end_pos; i = incPos(i))
  691. {
  692. if (!m_entries[i].pUnit || m_entries[pos].status != EntryState_Avail)
  693. {
  694. break;
  695. }
  696. // Check PB_LAST only in message mode.
  697. if (!m_bMessageAPI || packetAt(i).getMsgBoundary() & PB_LAST)
  698. {
  699. m_iFirstNonreadPos = incPos(i);
  700. break;
  701. }
  702. }
  703. if (pos == m_iFirstNonreadPos || !m_entries[m_iFirstNonreadPos].pUnit)
  704. break;
  705. pos = m_iFirstNonreadPos;
  706. }
  707. }
  708. int CRcvBuffer::findLastMessagePkt()
  709. {
  710. for (int i = m_iStartPos; i != m_iFirstNonreadPos; i = incPos(i))
  711. {
  712. SRT_ASSERT(m_entries[i].pUnit);
  713. if (packetAt(i).getMsgBoundary() & PB_LAST)
  714. {
  715. return i;
  716. }
  717. }
  718. return -1;
  719. }
  720. void CRcvBuffer::onInsertNotInOrderPacket(int insertPos)
  721. {
  722. if (m_numOutOfOrderPackets == 0)
  723. return;
  724. // If the following condition is true, there is already a packet,
  725. // that can be read out of order. We don't need to search for
  726. // another one. The search should be done when that packet is read out from the buffer.
  727. //
  728. // There might happen that the packet being added precedes the previously found one.
  729. // However, it is allowed to re bead out of order, so no need to update the position.
  730. if (m_iFirstReadableOutOfOrder >= 0)
  731. return;
  732. // Just a sanity check. This function is called when a new packet is added.
  733. // So the should be unacknowledged packets.
  734. SRT_ASSERT(m_iMaxPosOff > 0);
  735. SRT_ASSERT(m_entries[insertPos].pUnit);
  736. const CPacket& pkt = packetAt(insertPos);
  737. const PacketBoundary boundary = pkt.getMsgBoundary();
  738. //if ((boundary & PB_FIRST) && (boundary & PB_LAST))
  739. //{
  740. // // This packet can be read out of order
  741. // m_iFirstReadableOutOfOrder = insertPos;
  742. // return;
  743. //}
  744. const int msgNo = pkt.getMsgSeq(m_bPeerRexmitFlag);
  745. // First check last packet, because it is expected to be received last.
  746. const bool hasLast = (boundary & PB_LAST) || (-1 < scanNotInOrderMessageRight(insertPos, msgNo));
  747. if (!hasLast)
  748. return;
  749. const int firstPktPos = (boundary & PB_FIRST)
  750. ? insertPos
  751. : scanNotInOrderMessageLeft(insertPos, msgNo);
  752. if (firstPktPos < 0)
  753. return;
  754. m_iFirstReadableOutOfOrder = firstPktPos;
  755. return;
  756. }
  757. bool CRcvBuffer::checkFirstReadableOutOfOrder()
  758. {
  759. if (m_numOutOfOrderPackets <= 0 || m_iFirstReadableOutOfOrder < 0 || m_iMaxPosOff == 0)
  760. return false;
  761. const int endPos = incPos(m_iStartPos, m_iMaxPosOff);
  762. int msgno = -1;
  763. for (int pos = m_iFirstReadableOutOfOrder; pos != endPos; pos = incPos(pos))
  764. {
  765. if (!m_entries[pos].pUnit)
  766. return false;
  767. const CPacket& pkt = packetAt(pos);
  768. if (pkt.getMsgOrderFlag())
  769. return false;
  770. if (msgno == -1)
  771. msgno = pkt.getMsgSeq(m_bPeerRexmitFlag);
  772. else if (msgno != pkt.getMsgSeq(m_bPeerRexmitFlag))
  773. return false;
  774. if (pkt.getMsgBoundary() & PB_LAST)
  775. return true;
  776. }
  777. return false;
  778. }
  779. void CRcvBuffer::updateFirstReadableOutOfOrder()
  780. {
  781. if (hasReadableInorderPkts() || m_numOutOfOrderPackets <= 0 || m_iFirstReadableOutOfOrder >= 0)
  782. return;
  783. if (m_iMaxPosOff == 0)
  784. return;
  785. // TODO: unused variable outOfOrderPktsRemain?
  786. int outOfOrderPktsRemain = (int) m_numOutOfOrderPackets;
  787. // Search further packets to the right.
  788. // First check if there are packets to the right.
  789. const int lastPos = (m_iStartPos + m_iMaxPosOff - 1) % m_szSize;
  790. int posFirst = -1;
  791. int posLast = -1;
  792. int msgNo = -1;
  793. for (int pos = m_iStartPos; outOfOrderPktsRemain; pos = incPos(pos))
  794. {
  795. if (!m_entries[pos].pUnit)
  796. {
  797. posFirst = posLast = msgNo = -1;
  798. continue;
  799. }
  800. const CPacket& pkt = packetAt(pos);
  801. if (pkt.getMsgOrderFlag()) // Skip in order packet
  802. {
  803. posFirst = posLast = msgNo = -1;
  804. continue;
  805. }
  806. --outOfOrderPktsRemain;
  807. const PacketBoundary boundary = pkt.getMsgBoundary();
  808. if (boundary & PB_FIRST)
  809. {
  810. posFirst = pos;
  811. msgNo = pkt.getMsgSeq(m_bPeerRexmitFlag);
  812. }
  813. if (pkt.getMsgSeq(m_bPeerRexmitFlag) != msgNo)
  814. {
  815. posFirst = posLast = msgNo = -1;
  816. continue;
  817. }
  818. if (boundary & PB_LAST)
  819. {
  820. m_iFirstReadableOutOfOrder = posFirst;
  821. return;
  822. }
  823. if (pos == lastPos)
  824. break;
  825. }
  826. return;
  827. }
  828. int CRcvBuffer::scanNotInOrderMessageRight(const int startPos, int msgNo) const
  829. {
  830. // Search further packets to the right.
  831. // First check if there are packets to the right.
  832. const int lastPos = (m_iStartPos + m_iMaxPosOff - 1) % m_szSize;
  833. if (startPos == lastPos)
  834. return -1;
  835. int pos = startPos;
  836. do
  837. {
  838. pos = incPos(pos);
  839. if (!m_entries[pos].pUnit)
  840. break;
  841. const CPacket& pkt = packetAt(pos);
  842. if (pkt.getMsgSeq(m_bPeerRexmitFlag) != msgNo)
  843. {
  844. LOGC(rbuflog.Error, log << "Missing PB_LAST packet for msgNo " << msgNo);
  845. return -1;
  846. }
  847. const PacketBoundary boundary = pkt.getMsgBoundary();
  848. if (boundary & PB_LAST)
  849. return pos;
  850. } while (pos != lastPos);
  851. return -1;
  852. }
  853. int CRcvBuffer::scanNotInOrderMessageLeft(const int startPos, int msgNo) const
  854. {
  855. // Search preceding packets to the left.
  856. // First check if there are packets to the left.
  857. if (startPos == m_iStartPos)
  858. return -1;
  859. int pos = startPos;
  860. do
  861. {
  862. pos = decPos(pos);
  863. if (!m_entries[pos].pUnit)
  864. return -1;
  865. const CPacket& pkt = packetAt(pos);
  866. if (pkt.getMsgSeq(m_bPeerRexmitFlag) != msgNo)
  867. {
  868. LOGC(rbuflog.Error, log << "Missing PB_FIRST packet for msgNo " << msgNo);
  869. return -1;
  870. }
  871. const PacketBoundary boundary = pkt.getMsgBoundary();
  872. if (boundary & PB_FIRST)
  873. return pos;
  874. } while (pos != m_iStartPos);
  875. return -1;
  876. }
  877. bool CRcvBuffer::addRcvTsbPdDriftSample(uint32_t usTimestamp, const time_point& tsPktArrival, int usRTTSample)
  878. {
  879. return m_tsbpd.addDriftSample(usTimestamp, tsPktArrival, usRTTSample);
  880. }
  881. void CRcvBuffer::setTsbPdMode(const steady_clock::time_point& timebase, bool wrap, duration delay)
  882. {
  883. m_tsbpd.setTsbPdMode(timebase, wrap, delay);
  884. }
  885. void CRcvBuffer::applyGroupTime(const steady_clock::time_point& timebase,
  886. bool wrp,
  887. uint32_t delay,
  888. const steady_clock::duration& udrift)
  889. {
  890. m_tsbpd.applyGroupTime(timebase, wrp, delay, udrift);
  891. }
  892. void CRcvBuffer::applyGroupDrift(const steady_clock::time_point& timebase,
  893. bool wrp,
  894. const steady_clock::duration& udrift)
  895. {
  896. m_tsbpd.applyGroupDrift(timebase, wrp, udrift);
  897. }
  898. CRcvBuffer::time_point CRcvBuffer::getTsbPdTimeBase(uint32_t usPktTimestamp) const
  899. {
  900. return m_tsbpd.getTsbPdTimeBase(usPktTimestamp);
  901. }
  902. void CRcvBuffer::updateTsbPdTimeBase(uint32_t usPktTimestamp)
  903. {
  904. m_tsbpd.updateTsbPdTimeBase(usPktTimestamp);
  905. }
  906. string CRcvBuffer::strFullnessState(int iFirstUnackSeqNo, const time_point& tsNow) const
  907. {
  908. stringstream ss;
  909. ss << "iFirstUnackSeqNo=" << iFirstUnackSeqNo << " m_iStartSeqNo=" << m_iStartSeqNo
  910. << " m_iStartPos=" << m_iStartPos << " m_iMaxPosOff=" << m_iMaxPosOff << ". ";
  911. ss << "Space avail " << getAvailSize(iFirstUnackSeqNo) << "/" << m_szSize << " pkts. ";
  912. if (m_tsbpd.isEnabled() && m_iMaxPosOff > 0)
  913. {
  914. const PacketInfo nextValidPkt = getFirstValidPacketInfo();
  915. ss << "(TSBPD ready in ";
  916. if (!is_zero(nextValidPkt.tsbpd_time))
  917. {
  918. ss << count_milliseconds(nextValidPkt.tsbpd_time - tsNow) << "ms";
  919. const int iLastPos = incPos(m_iStartPos, m_iMaxPosOff - 1);
  920. if (m_entries[iLastPos].pUnit)
  921. {
  922. ss << ", timespan ";
  923. const uint32_t usPktTimestamp = packetAt(iLastPos).getMsgTimeStamp();
  924. ss << count_milliseconds(m_tsbpd.getPktTsbPdTime(usPktTimestamp) - nextValidPkt.tsbpd_time);
  925. ss << " ms";
  926. }
  927. }
  928. else
  929. {
  930. ss << "n/a";
  931. }
  932. ss << "). ";
  933. }
  934. ss << SRT_SYNC_CLOCK_STR " drift " << getDrift() / 1000 << " ms.";
  935. return ss.str();
  936. }
  937. CRcvBuffer::time_point CRcvBuffer::getPktTsbPdTime(uint32_t usPktTimestamp) const
  938. {
  939. return m_tsbpd.getPktTsbPdTime(usPktTimestamp);
  940. }
  941. /* Return moving average of acked data pkts, bytes, and timespan (ms) of the receive buffer */
  942. int CRcvBuffer::getRcvAvgDataSize(int& bytes, int& timespan)
  943. {
  944. // Average number of packets and timespan could be small,
  945. // so rounding is beneficial, while for the number of
  946. // bytes in the buffer is a higher value, so rounding can be omitted,
  947. // but probably better to round all three values.
  948. timespan = static_cast<int>(round((m_mavg.timespan_ms())));
  949. bytes = static_cast<int>(round((m_mavg.bytes())));
  950. return static_cast<int>(round(m_mavg.pkts()));
  951. }
  952. /* Update moving average of acked data pkts, bytes, and timespan (ms) of the receive buffer */
  953. void CRcvBuffer::updRcvAvgDataSize(const steady_clock::time_point& now)
  954. {
  955. if (!m_mavg.isTimeToUpdate(now))
  956. return;
  957. int bytes = 0;
  958. int timespan_ms = 0;
  959. const int pkts = getRcvDataSize(bytes, timespan_ms);
  960. m_mavg.update(now, pkts, bytes, timespan_ms);
  961. }
  962. } // namespace srt