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