niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
tina.legrand@webrtc.org | df69775 | 2012-02-08 10:22:21 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * Implementation of the actual packet buffer data structure. |
| 13 | */ |
| 14 | |
| 15 | #include "packet_buffer.h" |
| 16 | |
| 17 | #include <string.h> /* to define NULL */ |
| 18 | |
| 19 | #include "signal_processing_library.h" |
| 20 | |
| 21 | #include "neteq_error_codes.h" |
| 22 | |
| 23 | #ifdef NETEQ_DELAY_LOGGING |
| 24 | /* special code for offline delay logging */ |
| 25 | #include "delay_logging.h" |
| 26 | #include <stdio.h> |
| 27 | |
| 28 | extern FILE *delay_fid2; /* file pointer to delay log file */ |
| 29 | extern WebRtc_UWord32 tot_received_packets; |
| 30 | #endif /* NETEQ_DELAY_LOGGING */ |
| 31 | |
| 32 | |
| 33 | int WebRtcNetEQ_PacketBufferInit(PacketBuf_t *bufferInst, int maxNoOfPackets, |
| 34 | WebRtc_Word16 *pw16_memory, int memorySize) |
| 35 | { |
| 36 | int i; |
| 37 | int pos = 0; |
| 38 | |
| 39 | /* Sanity check */ |
| 40 | if ((memorySize < PBUFFER_MIN_MEMORY_SIZE) || (pw16_memory == NULL) |
| 41 | || (maxNoOfPackets < 2) || (maxNoOfPackets > 600)) |
| 42 | { |
| 43 | /* Invalid parameters */ |
| 44 | return (PBUFFER_INIT_ERROR); |
| 45 | } |
| 46 | |
| 47 | /* Clear the buffer instance */ |
| 48 | WebRtcSpl_MemSetW16((WebRtc_Word16*) bufferInst, 0, |
| 49 | sizeof(PacketBuf_t) / sizeof(WebRtc_Word16)); |
| 50 | |
| 51 | /* Clear the buffer memory */ |
| 52 | WebRtcSpl_MemSetW16((WebRtc_Word16*) pw16_memory, 0, memorySize); |
| 53 | |
| 54 | /* Set maximum number of packets */ |
| 55 | bufferInst->maxInsertPositions = maxNoOfPackets; |
| 56 | |
| 57 | /* Initialize array pointers */ |
| 58 | /* After each pointer has been set, the index pos is advanced to point immediately |
| 59 | * after the the recently allocated vector. Note that one step for the pos index |
| 60 | * corresponds to a WebRtc_Word16. |
| 61 | */ |
| 62 | |
| 63 | bufferInst->timeStamp = (WebRtc_UWord32*) &pw16_memory[pos]; |
| 64 | pos += maxNoOfPackets << 1; /* advance maxNoOfPackets * WebRtc_UWord32 */ |
| 65 | |
| 66 | bufferInst->payloadLocation = (WebRtc_Word16**) &pw16_memory[pos]; |
| 67 | pos += maxNoOfPackets * (sizeof(WebRtc_Word16*) / sizeof(WebRtc_Word16)); /* advance */ |
| 68 | |
| 69 | bufferInst->seqNumber = (WebRtc_UWord16*) &pw16_memory[pos]; |
| 70 | pos += maxNoOfPackets; /* advance maxNoOfPackets * WebRtc_UWord16 */ |
| 71 | |
| 72 | bufferInst->payloadType = &pw16_memory[pos]; |
| 73 | pos += maxNoOfPackets; /* advance maxNoOfPackets * WebRtc_Word16 */ |
| 74 | |
| 75 | bufferInst->payloadLengthBytes = &pw16_memory[pos]; |
| 76 | pos += maxNoOfPackets; /* advance maxNoOfPackets * WebRtc_Word16 */ |
| 77 | |
| 78 | bufferInst->rcuPlCntr = &pw16_memory[pos]; |
| 79 | pos += maxNoOfPackets; /* advance maxNoOfPackets * WebRtc_Word16 */ |
| 80 | |
henrik.lundin@webrtc.org | dbba1f9 | 2011-12-20 15:45:05 +0000 | [diff] [blame] | 81 | bufferInst->waitingTime = (int*) (&pw16_memory[pos]); |
| 82 | /* Advance maxNoOfPackets * sizeof(waitingTime element). */ |
| 83 | pos += maxNoOfPackets * |
| 84 | sizeof(*bufferInst->waitingTime) / sizeof(*pw16_memory); |
| 85 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 86 | /* The payload memory starts after the slot arrays */ |
| 87 | bufferInst->startPayloadMemory = &pw16_memory[pos]; |
| 88 | bufferInst->currentMemoryPos = bufferInst->startPayloadMemory; |
| 89 | bufferInst->memorySizeW16 = (memorySize - pos); /* Remaining memory */ |
| 90 | |
| 91 | /* Initialize each payload slot as empty with infinite delay */ |
| 92 | for (i = 0; i < bufferInst->maxInsertPositions; i++) |
| 93 | { |
| 94 | bufferInst->payloadType[i] = -1; |
| 95 | } |
| 96 | |
| 97 | /* Reset buffer parameters */ |
| 98 | bufferInst->numPacketsInBuffer = 0; |
| 99 | bufferInst->packSizeSamples = 0; |
| 100 | bufferInst->insertPosition = 0; |
| 101 | |
| 102 | /* Reset buffer statistics */ |
| 103 | bufferInst->discardedPackets = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 104 | |
| 105 | return (0); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | int WebRtcNetEQ_PacketBufferFlush(PacketBuf_t *bufferInst) |
| 110 | { |
| 111 | int i; |
| 112 | |
| 113 | /* Sanity check */ |
| 114 | if (bufferInst->startPayloadMemory == NULL) |
| 115 | { |
| 116 | /* Packet buffer has not been initialized */ |
| 117 | /* Don't do the flushing operation, since we do not |
| 118 | know the state of the struct variables */ |
| 119 | return (0); |
| 120 | } |
| 121 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 122 | /* Set all payload lengths to zero */ |
| 123 | WebRtcSpl_MemSetW16(bufferInst->payloadLengthBytes, 0, bufferInst->maxInsertPositions); |
| 124 | |
| 125 | /* Reset buffer variables */ |
| 126 | bufferInst->numPacketsInBuffer = 0; |
| 127 | bufferInst->currentMemoryPos = bufferInst->startPayloadMemory; |
| 128 | bufferInst->insertPosition = 0; |
| 129 | |
| 130 | /* Clear all slots, starting with the last one */ |
| 131 | for (i = (bufferInst->maxInsertPositions - 1); i >= 0; i--) |
| 132 | { |
| 133 | bufferInst->payloadType[i] = -1; |
| 134 | bufferInst->timeStamp[i] = 0; |
| 135 | bufferInst->seqNumber[i] = 0; |
| 136 | } |
| 137 | |
| 138 | return (0); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | int WebRtcNetEQ_PacketBufferInsert(PacketBuf_t *bufferInst, const RTPPacket_t *RTPpacket, |
| 143 | WebRtc_Word16 *flushed) |
| 144 | { |
| 145 | int nextPos; |
| 146 | int i; |
| 147 | |
| 148 | #ifdef NETEQ_DELAY_LOGGING |
| 149 | /* special code for offline delay logging */ |
| 150 | int temp_var; |
| 151 | #endif /* NETEQ_DELAY_LOGGING */ |
| 152 | |
| 153 | /* Initialize to "no flush" */ |
| 154 | *flushed = 0; |
| 155 | |
| 156 | /* Sanity check */ |
| 157 | if (bufferInst->startPayloadMemory == NULL) |
| 158 | { |
| 159 | /* packet buffer has not been initialized */ |
| 160 | return (-1); |
| 161 | } |
| 162 | |
| 163 | /* Sanity check for payload length |
| 164 | (payloadLen in bytes and memory size in WebRtc_Word16) */ |
| 165 | if ((RTPpacket->payloadLen > (bufferInst->memorySizeW16 << 1)) || (RTPpacket->payloadLen |
| 166 | <= 0)) |
| 167 | { |
| 168 | /* faulty or too long payload length */ |
| 169 | return (-1); |
| 170 | } |
| 171 | |
| 172 | /* Find a position in the buffer for this packet */ |
| 173 | if (bufferInst->numPacketsInBuffer != 0) |
| 174 | { |
| 175 | /* Get the next slot */ |
| 176 | bufferInst->insertPosition++; |
| 177 | if (bufferInst->insertPosition >= bufferInst->maxInsertPositions) |
| 178 | { |
| 179 | /* "Wrap around" and start from the beginning */ |
| 180 | bufferInst->insertPosition = 0; |
| 181 | } |
| 182 | |
| 183 | /* Check if there is enough space for the new packet */ |
| 184 | if (bufferInst->currentMemoryPos + ((RTPpacket->payloadLen + 1) >> 1) |
| 185 | >= &bufferInst->startPayloadMemory[bufferInst->memorySizeW16]) |
| 186 | { |
| 187 | WebRtc_Word16 *tempMemAddress; |
| 188 | |
| 189 | /* |
| 190 | * Payload does not fit at the end of the memory, put it in the beginning |
| 191 | * instead |
| 192 | */ |
| 193 | bufferInst->currentMemoryPos = bufferInst->startPayloadMemory; |
| 194 | |
| 195 | /* |
| 196 | * Now, we must search for the next non-empty payload, |
| 197 | * finding the one with the lowest start address for the payload |
| 198 | */ |
| 199 | tempMemAddress = &bufferInst->startPayloadMemory[bufferInst->memorySizeW16]; |
| 200 | nextPos = -1; |
| 201 | |
| 202 | /* Loop through all slots again */ |
| 203 | for (i = 0; i < bufferInst->maxInsertPositions; i++) |
| 204 | { |
| 205 | /* Look for the non-empty slot with the lowest |
| 206 | payload location address */ |
| 207 | if (bufferInst->payloadLengthBytes[i] != 0 && bufferInst->payloadLocation[i] |
| 208 | < tempMemAddress) |
| 209 | { |
| 210 | tempMemAddress = bufferInst->payloadLocation[i]; |
| 211 | nextPos = i; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | /* Check that we did find a previous payload */ |
| 216 | if (nextPos == -1) |
| 217 | { |
| 218 | /* The buffer is corrupt => flush and return error */ |
| 219 | WebRtcNetEQ_PacketBufferFlush(bufferInst); |
| 220 | *flushed = 1; |
| 221 | return (-1); |
| 222 | } |
| 223 | } |
| 224 | else |
| 225 | { |
| 226 | /* Payload fits at the end of memory. */ |
| 227 | |
| 228 | /* Find the next non-empty slot. */ |
| 229 | nextPos = bufferInst->insertPosition + 1; |
| 230 | |
| 231 | /* Increase nextPos until a non-empty slot is found or end of array is encountered*/ |
| 232 | while ((bufferInst->payloadLengthBytes[nextPos] == 0) && (nextPos |
| 233 | < bufferInst->maxInsertPositions)) |
| 234 | { |
| 235 | nextPos++; |
| 236 | } |
| 237 | |
| 238 | if (nextPos == bufferInst->maxInsertPositions) |
| 239 | { |
| 240 | /* |
| 241 | * Reached the end of the array, so there must be a packet in the first |
| 242 | * position instead |
| 243 | */ |
| 244 | nextPos = 0; |
| 245 | |
| 246 | /* Increase nextPos until a non-empty slot is found */ |
| 247 | while (bufferInst->payloadLengthBytes[nextPos] == 0) |
| 248 | { |
| 249 | nextPos++; |
| 250 | } |
| 251 | } |
| 252 | } /* end if-else */ |
| 253 | |
| 254 | /* |
| 255 | * Check if the new payload will extend into a payload later in memory. |
| 256 | * If so, the buffer is full. |
| 257 | */ |
| 258 | if ((bufferInst->currentMemoryPos <= bufferInst->payloadLocation[nextPos]) |
| 259 | && ((&bufferInst->currentMemoryPos[(RTPpacket->payloadLen + 1) >> 1]) |
| 260 | > bufferInst->payloadLocation[nextPos])) |
| 261 | { |
| 262 | /* Buffer is full, so the buffer must be flushed */ |
| 263 | WebRtcNetEQ_PacketBufferFlush(bufferInst); |
| 264 | *flushed = 1; |
| 265 | } |
| 266 | |
| 267 | if (bufferInst->payloadLengthBytes[bufferInst->insertPosition] != 0) |
| 268 | { |
| 269 | /* All positions are already taken and entire buffer should be flushed */ |
| 270 | WebRtcNetEQ_PacketBufferFlush(bufferInst); |
| 271 | *flushed = 1; |
| 272 | } |
| 273 | |
| 274 | } |
| 275 | else |
| 276 | { |
| 277 | /* Buffer is empty, just insert the packet at the beginning */ |
| 278 | bufferInst->currentMemoryPos = bufferInst->startPayloadMemory; |
| 279 | bufferInst->insertPosition = 0; |
| 280 | } |
| 281 | |
| 282 | /* Insert packet in the found position */ |
| 283 | if (RTPpacket->starts_byte1 == 0) |
| 284 | { |
| 285 | /* Payload is 16-bit aligned => just copy it */ |
| 286 | |
| 287 | WEBRTC_SPL_MEMCPY_W16(bufferInst->currentMemoryPos, |
| 288 | RTPpacket->payload, (RTPpacket->payloadLen + 1) >> 1); |
| 289 | } |
| 290 | else |
| 291 | { |
| 292 | /* Payload is not 16-bit aligned => align it during copy operation */ |
| 293 | for (i = 0; i < RTPpacket->payloadLen; i++) |
| 294 | { |
| 295 | /* copy the (i+1)-th byte to the i-th byte */ |
| 296 | |
| 297 | WEBRTC_SPL_SET_BYTE(bufferInst->currentMemoryPos, |
| 298 | (WEBRTC_SPL_GET_BYTE(RTPpacket->payload, (i + 1))), i); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | /* Copy the packet information */ |
| 303 | bufferInst->payloadLocation[bufferInst->insertPosition] = bufferInst->currentMemoryPos; |
| 304 | bufferInst->payloadLengthBytes[bufferInst->insertPosition] = RTPpacket->payloadLen; |
| 305 | bufferInst->payloadType[bufferInst->insertPosition] = RTPpacket->payloadType; |
| 306 | bufferInst->seqNumber[bufferInst->insertPosition] = RTPpacket->seqNumber; |
| 307 | bufferInst->timeStamp[bufferInst->insertPosition] = RTPpacket->timeStamp; |
| 308 | bufferInst->rcuPlCntr[bufferInst->insertPosition] = RTPpacket->rcuPlCntr; |
henrik.lundin@webrtc.org | dbba1f9 | 2011-12-20 15:45:05 +0000 | [diff] [blame] | 309 | bufferInst->rcuPlCntr[bufferInst->insertPosition] = 0; |
| 310 | bufferInst->waitingTime[bufferInst->insertPosition] = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 311 | /* Update buffer parameters */ |
| 312 | bufferInst->numPacketsInBuffer++; |
| 313 | bufferInst->currentMemoryPos += (RTPpacket->payloadLen + 1) >> 1; |
| 314 | |
| 315 | #ifdef NETEQ_DELAY_LOGGING |
| 316 | /* special code for offline delay logging */ |
| 317 | if (*flushed) |
| 318 | { |
| 319 | temp_var = NETEQ_DELAY_LOGGING_SIGNAL_FLUSH; |
leozwang@webrtc.org | 354b0ed | 2012-06-01 17:46:21 +0000 | [diff] [blame] | 320 | if (fwrite(&temp_var, sizeof(int), 1, delay_fid2) != 1) { |
| 321 | return -1; |
| 322 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 323 | } |
| 324 | temp_var = NETEQ_DELAY_LOGGING_SIGNAL_RECIN; |
leozwang@webrtc.org | 354b0ed | 2012-06-01 17:46:21 +0000 | [diff] [blame] | 325 | if ((fwrite(&temp_var, sizeof(int), |
| 326 | 1, delay_fid2) != 1) || |
| 327 | (fwrite(&RTPpacket->timeStamp, sizeof(WebRtc_UWord32), |
| 328 | 1, delay_fid2) != 1) || |
| 329 | (fwrite(&RTPpacket->seqNumber, sizeof(WebRtc_UWord16), |
| 330 | 1, delay_fid2) != 1) || |
| 331 | (fwrite(&RTPpacket->payloadType, sizeof(int), |
| 332 | 1, delay_fid2) != 1) || |
| 333 | (fwrite(&RTPpacket->payloadLen, sizeof(WebRtc_Word16), |
| 334 | 1, delay_fid2) != 1)) { |
| 335 | return -1; |
| 336 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 337 | tot_received_packets++; |
| 338 | #endif /* NETEQ_DELAY_LOGGING */ |
| 339 | |
| 340 | return (0); |
| 341 | } |
| 342 | |
| 343 | |
| 344 | int WebRtcNetEQ_PacketBufferExtract(PacketBuf_t *bufferInst, RTPPacket_t *RTPpacket, |
henrik.lundin@webrtc.org | dbba1f9 | 2011-12-20 15:45:05 +0000 | [diff] [blame] | 345 | int bufferPosition, int *waitingTime) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 346 | { |
| 347 | |
| 348 | /* Sanity check */ |
| 349 | if (bufferInst->startPayloadMemory == NULL) |
| 350 | { |
| 351 | /* packet buffer has not been initialized */ |
| 352 | return (PBUFFER_NOT_INITIALIZED); |
| 353 | } |
| 354 | |
| 355 | if (bufferPosition < 0 || bufferPosition >= bufferInst->maxInsertPositions) |
| 356 | { |
| 357 | /* buffer position is outside valid range */ |
| 358 | return (NETEQ_OTHER_ERROR); |
| 359 | } |
| 360 | |
| 361 | /* Check that there is a valid payload in the specified position */ |
| 362 | if (bufferInst->payloadLengthBytes[bufferPosition] <= 0) |
| 363 | { |
| 364 | /* The position does not contain a valid payload */ |
| 365 | RTPpacket->payloadLen = 0; /* Set zero length */ |
| 366 | return (PBUFFER_NONEXISTING_PACKET); /* Return error */ |
| 367 | } |
| 368 | |
| 369 | /* Payload exists => extract payload data */ |
| 370 | |
| 371 | /* Copy the actual data payload to RTP packet struct */ |
| 372 | |
| 373 | WEBRTC_SPL_MEMCPY_W16((WebRtc_Word16*) RTPpacket->payload, |
| 374 | bufferInst->payloadLocation[bufferPosition], |
| 375 | (bufferInst->payloadLengthBytes[bufferPosition] + 1) >> 1); /*length in WebRtc_Word16*/ |
| 376 | |
| 377 | /* Copy payload parameters */ |
| 378 | RTPpacket->payloadLen = bufferInst->payloadLengthBytes[bufferPosition]; |
| 379 | RTPpacket->payloadType = bufferInst->payloadType[bufferPosition]; |
| 380 | RTPpacket->seqNumber = bufferInst->seqNumber[bufferPosition]; |
| 381 | RTPpacket->timeStamp = bufferInst->timeStamp[bufferPosition]; |
| 382 | RTPpacket->rcuPlCntr = bufferInst->rcuPlCntr[bufferPosition]; |
henrik.lundin@webrtc.org | dbba1f9 | 2011-12-20 15:45:05 +0000 | [diff] [blame] | 383 | *waitingTime = bufferInst->waitingTime[bufferPosition]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 384 | RTPpacket->starts_byte1 = 0; /* payload is 16-bit aligned */ |
| 385 | |
| 386 | /* Clear the position in the packet buffer */ |
| 387 | bufferInst->payloadType[bufferPosition] = -1; |
| 388 | bufferInst->payloadLengthBytes[bufferPosition] = 0; |
| 389 | bufferInst->seqNumber[bufferPosition] = 0; |
| 390 | bufferInst->timeStamp[bufferPosition] = 0; |
henrik.lundin@webrtc.org | dbba1f9 | 2011-12-20 15:45:05 +0000 | [diff] [blame] | 391 | bufferInst->waitingTime[bufferPosition] = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 392 | bufferInst->payloadLocation[bufferPosition] = bufferInst->startPayloadMemory; |
| 393 | |
| 394 | /* Reduce packet counter with one */ |
| 395 | bufferInst->numPacketsInBuffer--; |
| 396 | |
| 397 | return (0); |
| 398 | } |
| 399 | |
kma@webrtc.org | 89a1000 | 2012-01-30 15:37:33 +0000 | [diff] [blame] | 400 | int WebRtcNetEQ_PacketBufferFindLowestTimestamp(PacketBuf_t* buffer_inst, |
| 401 | uint32_t current_time_stamp, |
| 402 | uint32_t* time_stamp, |
| 403 | int* buffer_position, |
| 404 | int erase_old_packets, |
| 405 | int16_t* payload_type) { |
| 406 | int32_t time_stamp_diff = WEBRTC_SPL_WORD32_MAX; /* Smallest diff found. */ |
| 407 | int32_t new_diff; |
| 408 | int i; |
| 409 | int16_t rcu_payload_cntr; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 410 | |
kma@webrtc.org | 89a1000 | 2012-01-30 15:37:33 +0000 | [diff] [blame] | 411 | if (buffer_inst->startPayloadMemory == NULL) { |
| 412 | /* Packet buffer has not been initialized. */ |
| 413 | return PBUFFER_NOT_INITIALIZED; |
| 414 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 415 | |
kma@webrtc.org | 89a1000 | 2012-01-30 15:37:33 +0000 | [diff] [blame] | 416 | /* Initialize all return values. */ |
| 417 | *time_stamp = 0; |
| 418 | *payload_type = -1; /* Indicates that no packet was found. */ |
| 419 | *buffer_position = -1; /* Indicates that no packet was found. */ |
| 420 | rcu_payload_cntr = WEBRTC_SPL_WORD16_MAX; /* Indicates no packet found. */ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 421 | |
kma@webrtc.org | 89a1000 | 2012-01-30 15:37:33 +0000 | [diff] [blame] | 422 | /* Check if buffer is empty. */ |
| 423 | if (buffer_inst->numPacketsInBuffer <= 0) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 424 | return 0; |
kma@webrtc.org | 89a1000 | 2012-01-30 15:37:33 +0000 | [diff] [blame] | 425 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 426 | |
kma@webrtc.org | 89a1000 | 2012-01-30 15:37:33 +0000 | [diff] [blame] | 427 | /* Loop through all slots in buffer. */ |
| 428 | if (erase_old_packets) { /* If old payloads should be discarded. */ |
| 429 | for (i = 0; i < buffer_inst->maxInsertPositions; ++i) { |
| 430 | /* Calculate difference between this slot and current_time_stamp. */ |
| 431 | new_diff = (int32_t)(buffer_inst->timeStamp[i] - current_time_stamp); |
| 432 | |
| 433 | /* Check if payload should be discarded. */ |
| 434 | if ((new_diff < 0) /* Payload is too old */ |
| 435 | && (new_diff > -30000) /* Account for TS wrap-around. */ |
| 436 | && (buffer_inst->payloadLengthBytes[i] > 0)) { /* Payload exists. */ |
| 437 | /* Throw away old packet. */ |
| 438 | |
| 439 | /* Clear the position in the buffer. */ |
| 440 | buffer_inst->payloadType[i] = -1; |
| 441 | buffer_inst->payloadLengthBytes[i] = 0; |
| 442 | |
| 443 | /* Reduce packet counter by one. */ |
| 444 | buffer_inst->numPacketsInBuffer--; |
| 445 | /* Increase discard counter for in-call statistics. */ |
| 446 | buffer_inst->discardedPackets++; |
| 447 | } else if (((new_diff < time_stamp_diff) |
| 448 | || ((new_diff == time_stamp_diff) |
| 449 | && (buffer_inst->rcuPlCntr[i] < rcu_payload_cntr))) |
| 450 | && (buffer_inst->payloadLengthBytes[i] > 0)) { |
| 451 | /* New diff is smaller than previous diffs or we have a candidate with a |
| 452 | * time stamp as previous candidate but better RCU-counter; |
| 453 | * and the payload exists. |
| 454 | */ |
| 455 | /* Save this position as the best candidate. */ |
| 456 | *buffer_position = i; |
| 457 | time_stamp_diff = new_diff; |
| 458 | *payload_type = buffer_inst->payloadType[i]; |
| 459 | rcu_payload_cntr = buffer_inst->rcuPlCntr[i]; |
| 460 | } |
| 461 | } |
| 462 | } else { |
| 463 | for (i = 0; i < buffer_inst->maxInsertPositions; ++i) { |
| 464 | /* Calculate difference between this slot and current_time_stamp. */ |
| 465 | new_diff = (int32_t)(buffer_inst->timeStamp[i] - current_time_stamp); |
| 466 | |
| 467 | /* Check if this is the oldest packet. */ |
| 468 | if (((new_diff < time_stamp_diff) |
| 469 | || ((new_diff == time_stamp_diff) |
| 470 | && (buffer_inst->rcuPlCntr[i] < rcu_payload_cntr))) |
| 471 | && (buffer_inst->payloadLengthBytes[i] > 0)) { |
| 472 | /* New diff is smaller than previous diffs or we have a candidate with a |
| 473 | * time_stamp as previous candidate but better RCU-counter; |
| 474 | * and the payload exists. |
| 475 | */ |
| 476 | /* Save this position as the best candidate. */ |
| 477 | *buffer_position = i; |
| 478 | time_stamp_diff = new_diff; |
| 479 | *payload_type = buffer_inst->payloadType[i]; |
| 480 | rcu_payload_cntr = buffer_inst->rcuPlCntr[i]; |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | /* Check that we did find a real position. */ |
| 486 | if (*buffer_position >= 0) { |
| 487 | /* Get the time_stamp for the best position. */ |
| 488 | *time_stamp = buffer_inst->timeStamp[*buffer_position]; |
| 489 | } |
| 490 | |
| 491 | return 0; |
| 492 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 493 | |
| 494 | WebRtc_Word32 WebRtcNetEQ_PacketBufferGetSize(const PacketBuf_t *bufferInst) |
| 495 | { |
| 496 | int i, count; |
| 497 | WebRtc_Word32 sizeSamples; |
| 498 | |
| 499 | count = 0; |
| 500 | |
| 501 | /* Loop through all slots in the buffer */ |
| 502 | for (i = 0; i < bufferInst->maxInsertPositions; i++) |
| 503 | { |
| 504 | /* Only count the packets with non-zero size */ |
| 505 | if (bufferInst->payloadLengthBytes[i] != 0) |
| 506 | { |
| 507 | count++; |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | /* |
| 512 | * Calculate buffer size as number of packets times packet size |
| 513 | * (packet size is that of the latest decoded packet) |
| 514 | */ |
| 515 | sizeSamples = WEBRTC_SPL_MUL_16_16(bufferInst->packSizeSamples, count); |
| 516 | |
| 517 | /* Sanity check; size cannot be negative */ |
| 518 | if (sizeSamples < 0) |
| 519 | { |
| 520 | sizeSamples = 0; |
| 521 | } |
| 522 | |
| 523 | return sizeSamples; |
| 524 | } |
| 525 | |
henrik.lundin@webrtc.org | dbba1f9 | 2011-12-20 15:45:05 +0000 | [diff] [blame] | 526 | void WebRtcNetEQ_IncrementWaitingTimes(PacketBuf_t *buffer_inst) { |
| 527 | int i; |
| 528 | /* Loop through all slots in the buffer. */ |
| 529 | for (i = 0; i < buffer_inst->maxInsertPositions; ++i) { |
| 530 | /* Only increment waiting time for the packets with non-zero size. */ |
| 531 | if (buffer_inst->payloadLengthBytes[i] != 0) { |
| 532 | buffer_inst->waitingTime[i]++; |
| 533 | } |
| 534 | } |
| 535 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 536 | |
| 537 | int WebRtcNetEQ_GetDefaultCodecSettings(const enum WebRtcNetEQDecoder *codecID, |
| 538 | int noOfCodecs, int *maxBytes, int *maxSlots) |
| 539 | { |
| 540 | int i; |
| 541 | int ok = 0; |
| 542 | WebRtc_Word16 w16_tmp; |
| 543 | WebRtc_Word16 codecBytes; |
| 544 | WebRtc_Word16 codecBuffers; |
| 545 | |
| 546 | /* Initialize return variables to zero */ |
| 547 | *maxBytes = 0; |
| 548 | *maxSlots = 0; |
| 549 | |
| 550 | /* Loop through all codecs supplied to function */ |
| 551 | for (i = 0; i < noOfCodecs; i++) |
| 552 | { |
| 553 | /* Find current codec and set parameters accordingly */ |
| 554 | |
tina.legrand@webrtc.org | 4517585 | 2012-06-01 09:27:35 +0000 | [diff] [blame] | 555 | if ((codecID[i] == kDecoderPCMu) || (codecID[i] == kDecoderPCMu_2ch)) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 556 | { |
| 557 | codecBytes = 1680; /* Up to 210ms @ 64kbps */ |
| 558 | codecBuffers = 30; /* Down to 5ms frames */ |
| 559 | } |
tina.legrand@webrtc.org | 4517585 | 2012-06-01 09:27:35 +0000 | [diff] [blame] | 560 | else if ((codecID[i] == kDecoderPCMa) || |
| 561 | (codecID[i] == kDecoderPCMa_2ch)) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 562 | { |
| 563 | codecBytes = 1680; /* Up to 210ms @ 64kbps */ |
| 564 | codecBuffers = 30; /* Down to 5ms frames */ |
| 565 | } |
| 566 | else if (codecID[i] == kDecoderILBC) |
| 567 | { |
| 568 | codecBytes = 380; /* 200ms @ 15.2kbps (20ms frames) */ |
| 569 | codecBuffers = 10; |
| 570 | } |
| 571 | else if (codecID[i] == kDecoderISAC) |
| 572 | { |
| 573 | codecBytes = 960; /* 240ms @ 32kbps (60ms frames) */ |
| 574 | codecBuffers = 8; |
| 575 | } |
| 576 | else if (codecID[i] == kDecoderISACswb) |
| 577 | { |
| 578 | codecBytes = 1560; /* 240ms @ 52kbps (30ms frames) */ |
| 579 | codecBuffers = 8; |
| 580 | } |
tina.legrand@webrtc.org | 0ad3c1a | 2012-11-07 08:07:29 +0000 | [diff] [blame^] | 581 | else if ((codecID[i] == kDecoderOpus) || |
| 582 | (codecID[i] == kDecoderOpus_2ch)) |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 583 | { |
| 584 | codecBytes = 15300; /* 240ms @ 510kbps (60ms frames) */ |
| 585 | codecBuffers = 30; /* Replicating the value for PCMu/a */ |
| 586 | } |
tina.legrand@webrtc.org | 4517585 | 2012-06-01 09:27:35 +0000 | [diff] [blame] | 587 | else if ((codecID[i] == kDecoderPCM16B) || |
| 588 | (codecID[i] == kDecoderPCM16B_2ch)) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 589 | { |
| 590 | codecBytes = 3360; /* 210ms */ |
| 591 | codecBuffers = 15; |
| 592 | } |
tina.legrand@webrtc.org | 4517585 | 2012-06-01 09:27:35 +0000 | [diff] [blame] | 593 | else if ((codecID[i] == kDecoderPCM16Bwb) || |
| 594 | (codecID[i] == kDecoderPCM16Bwb_2ch)) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 595 | { |
| 596 | codecBytes = 6720; /* 210ms */ |
| 597 | codecBuffers = 15; |
| 598 | } |
tina.legrand@webrtc.org | 4517585 | 2012-06-01 09:27:35 +0000 | [diff] [blame] | 599 | else if ((codecID[i] == kDecoderPCM16Bswb32kHz) || |
| 600 | (codecID[i] == kDecoderPCM16Bswb32kHz_2ch)) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 601 | { |
| 602 | codecBytes = 13440; /* 210ms */ |
| 603 | codecBuffers = 15; |
| 604 | } |
| 605 | else if (codecID[i] == kDecoderPCM16Bswb48kHz) |
| 606 | { |
| 607 | codecBytes = 20160; /* 210ms */ |
| 608 | codecBuffers = 15; |
| 609 | } |
tina.legrand@webrtc.org | 4517585 | 2012-06-01 09:27:35 +0000 | [diff] [blame] | 610 | else if ((codecID[i] == kDecoderG722) || |
| 611 | (codecID[i] == kDecoderG722_2ch)) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 612 | { |
| 613 | codecBytes = 1680; /* 210ms @ 64kbps */ |
| 614 | codecBuffers = 15; |
| 615 | } |
| 616 | else if (codecID[i] == kDecoderRED) |
| 617 | { |
| 618 | codecBytes = 0; /* Should not be max... */ |
| 619 | codecBuffers = 0; |
| 620 | } |
| 621 | else if (codecID[i] == kDecoderAVT) |
| 622 | { |
| 623 | codecBytes = 0; /* Should not be max... */ |
| 624 | codecBuffers = 0; |
| 625 | } |
| 626 | else if (codecID[i] == kDecoderCNG) |
| 627 | { |
| 628 | codecBytes = 0; /* Should not be max... */ |
| 629 | codecBuffers = 0; |
| 630 | } |
| 631 | else if (codecID[i] == kDecoderG729) |
| 632 | { |
| 633 | codecBytes = 210; /* 210ms @ 8kbps */ |
| 634 | codecBuffers = 20; /* max 200ms supported for 10ms frames */ |
| 635 | } |
| 636 | else if (codecID[i] == kDecoderG729_1) |
| 637 | { |
| 638 | codecBytes = 840; /* 210ms @ 32kbps */ |
| 639 | codecBuffers = 10; /* max 200ms supported for 20ms frames */ |
| 640 | } |
| 641 | else if (codecID[i] == kDecoderG726_16) |
| 642 | { |
| 643 | codecBytes = 400; /* 200ms @ 16kbps */ |
| 644 | codecBuffers = 10; |
| 645 | } |
| 646 | else if (codecID[i] == kDecoderG726_24) |
| 647 | { |
| 648 | codecBytes = 600; /* 200ms @ 24kbps */ |
| 649 | codecBuffers = 10; |
| 650 | } |
| 651 | else if (codecID[i] == kDecoderG726_32) |
| 652 | { |
| 653 | codecBytes = 800; /* 200ms @ 32kbps */ |
| 654 | codecBuffers = 10; |
| 655 | } |
| 656 | else if (codecID[i] == kDecoderG726_40) |
| 657 | { |
| 658 | codecBytes = 1000; /* 200ms @ 40kbps */ |
| 659 | codecBuffers = 10; |
| 660 | } |
| 661 | else if (codecID[i] == kDecoderG722_1_16) |
| 662 | { |
| 663 | codecBytes = 420; /* 210ms @ 16kbps */ |
| 664 | codecBuffers = 10; |
| 665 | } |
| 666 | else if (codecID[i] == kDecoderG722_1_24) |
| 667 | { |
| 668 | codecBytes = 630; /* 210ms @ 24kbps */ |
| 669 | codecBuffers = 10; |
| 670 | } |
| 671 | else if (codecID[i] == kDecoderG722_1_32) |
| 672 | { |
| 673 | codecBytes = 840; /* 210ms @ 32kbps */ |
| 674 | codecBuffers = 10; |
| 675 | } |
| 676 | else if (codecID[i] == kDecoderG722_1C_24) |
| 677 | { |
| 678 | codecBytes = 630; /* 210ms @ 24kbps */ |
| 679 | codecBuffers = 10; |
| 680 | } |
| 681 | else if (codecID[i] == kDecoderG722_1C_32) |
| 682 | { |
| 683 | codecBytes = 840; /* 210ms @ 32kbps */ |
| 684 | codecBuffers = 10; |
| 685 | } |
| 686 | else if (codecID[i] == kDecoderG722_1C_48) |
| 687 | { |
| 688 | codecBytes = 1260; /* 210ms @ 48kbps */ |
| 689 | codecBuffers = 10; |
| 690 | } |
| 691 | else if (codecID[i] == kDecoderSPEEX_8) |
| 692 | { |
| 693 | codecBytes = 1250; /* 210ms @ 50kbps */ |
| 694 | codecBuffers = 10; |
| 695 | } |
| 696 | else if (codecID[i] == kDecoderSPEEX_16) |
| 697 | { |
| 698 | codecBytes = 1250; /* 210ms @ 50kbps */ |
| 699 | codecBuffers = 10; |
| 700 | } |
tina.legrand@webrtc.org | 4517585 | 2012-06-01 09:27:35 +0000 | [diff] [blame] | 701 | else if ((codecID[i] == kDecoderCELT_32) || |
| 702 | (codecID[i] == kDecoderCELT_32_2ch)) |
tina.legrand@webrtc.org | df69775 | 2012-02-08 10:22:21 +0000 | [diff] [blame] | 703 | { |
| 704 | codecBytes = 1250; /* 210ms @ 50kbps */ |
| 705 | codecBuffers = 10; |
| 706 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 707 | else if (codecID[i] == kDecoderGSMFR) |
| 708 | { |
| 709 | codecBytes = 340; /* 200ms */ |
| 710 | codecBuffers = 10; |
| 711 | } |
| 712 | else if (codecID[i] == kDecoderAMR) |
| 713 | { |
| 714 | codecBytes = 384; /* 240ms @ 12.2kbps+headers (60ms frames) */ |
| 715 | codecBuffers = 10; |
| 716 | } |
| 717 | else if (codecID[i] == kDecoderAMRWB) |
| 718 | { |
| 719 | codecBytes = 744; |
| 720 | codecBuffers = 10; |
| 721 | } |
| 722 | else if (codecID[i] == kDecoderArbitrary) |
| 723 | { |
| 724 | codecBytes = 6720; /* Assume worst case uncompressed WB 210ms */ |
| 725 | codecBuffers = 15; |
| 726 | } |
| 727 | else |
| 728 | { |
| 729 | /*Unknow codec */ |
| 730 | codecBytes = 0; |
| 731 | codecBuffers = 0; |
| 732 | ok = CODEC_DB_UNKNOWN_CODEC; |
| 733 | } |
| 734 | |
| 735 | /* Update max variables */ |
| 736 | *maxBytes = WEBRTC_SPL_MAX((*maxBytes), codecBytes); |
| 737 | *maxSlots = WEBRTC_SPL_MAX((*maxSlots), codecBuffers); |
| 738 | |
| 739 | } /* end of for loop */ |
| 740 | |
| 741 | /* |
| 742 | * Add size needed by the additional pointers for each slot inside struct, |
| 743 | * as indicated on each line below. |
| 744 | */ |
| 745 | w16_tmp = (sizeof(WebRtc_UWord32) /* timeStamp */ |
| 746 | + sizeof(WebRtc_Word16*) /* payloadLocation */ |
| 747 | + sizeof(WebRtc_UWord16) /* seqNumber */ |
henrik.lundin@webrtc.org | dbba1f9 | 2011-12-20 15:45:05 +0000 | [diff] [blame] | 748 | + sizeof(WebRtc_Word16) /* payloadType */ |
| 749 | + sizeof(WebRtc_Word16) /* payloadLengthBytes */ |
| 750 | + sizeof(WebRtc_Word16) /* rcuPlCntr */ |
| 751 | + sizeof(int)); /* waitingTime */ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 752 | /* Add the extra size per slot to the memory count */ |
| 753 | *maxBytes += w16_tmp * (*maxSlots); |
| 754 | |
| 755 | return ok; |
| 756 | } |