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->waitingTime[bufferInst->insertPosition] = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 310 | /* Update buffer parameters */ |
| 311 | bufferInst->numPacketsInBuffer++; |
| 312 | bufferInst->currentMemoryPos += (RTPpacket->payloadLen + 1) >> 1; |
| 313 | |
| 314 | #ifdef NETEQ_DELAY_LOGGING |
| 315 | /* special code for offline delay logging */ |
| 316 | if (*flushed) |
| 317 | { |
| 318 | temp_var = NETEQ_DELAY_LOGGING_SIGNAL_FLUSH; |
leozwang@webrtc.org | 354b0ed | 2012-06-01 17:46:21 +0000 | [diff] [blame] | 319 | if (fwrite(&temp_var, sizeof(int), 1, delay_fid2) != 1) { |
| 320 | return -1; |
| 321 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 322 | } |
| 323 | temp_var = NETEQ_DELAY_LOGGING_SIGNAL_RECIN; |
leozwang@webrtc.org | 354b0ed | 2012-06-01 17:46:21 +0000 | [diff] [blame] | 324 | if ((fwrite(&temp_var, sizeof(int), |
| 325 | 1, delay_fid2) != 1) || |
| 326 | (fwrite(&RTPpacket->timeStamp, sizeof(WebRtc_UWord32), |
| 327 | 1, delay_fid2) != 1) || |
| 328 | (fwrite(&RTPpacket->seqNumber, sizeof(WebRtc_UWord16), |
| 329 | 1, delay_fid2) != 1) || |
| 330 | (fwrite(&RTPpacket->payloadType, sizeof(int), |
| 331 | 1, delay_fid2) != 1) || |
| 332 | (fwrite(&RTPpacket->payloadLen, sizeof(WebRtc_Word16), |
| 333 | 1, delay_fid2) != 1)) { |
| 334 | return -1; |
| 335 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 336 | tot_received_packets++; |
| 337 | #endif /* NETEQ_DELAY_LOGGING */ |
| 338 | |
| 339 | return (0); |
| 340 | } |
| 341 | |
| 342 | |
| 343 | int WebRtcNetEQ_PacketBufferExtract(PacketBuf_t *bufferInst, RTPPacket_t *RTPpacket, |
henrik.lundin@webrtc.org | dbba1f9 | 2011-12-20 15:45:05 +0000 | [diff] [blame] | 344 | int bufferPosition, int *waitingTime) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 345 | { |
| 346 | |
| 347 | /* Sanity check */ |
| 348 | if (bufferInst->startPayloadMemory == NULL) |
| 349 | { |
| 350 | /* packet buffer has not been initialized */ |
| 351 | return (PBUFFER_NOT_INITIALIZED); |
| 352 | } |
| 353 | |
| 354 | if (bufferPosition < 0 || bufferPosition >= bufferInst->maxInsertPositions) |
| 355 | { |
| 356 | /* buffer position is outside valid range */ |
| 357 | return (NETEQ_OTHER_ERROR); |
| 358 | } |
| 359 | |
| 360 | /* Check that there is a valid payload in the specified position */ |
| 361 | if (bufferInst->payloadLengthBytes[bufferPosition] <= 0) |
| 362 | { |
| 363 | /* The position does not contain a valid payload */ |
| 364 | RTPpacket->payloadLen = 0; /* Set zero length */ |
| 365 | return (PBUFFER_NONEXISTING_PACKET); /* Return error */ |
| 366 | } |
| 367 | |
| 368 | /* Payload exists => extract payload data */ |
| 369 | |
| 370 | /* Copy the actual data payload to RTP packet struct */ |
| 371 | |
| 372 | WEBRTC_SPL_MEMCPY_W16((WebRtc_Word16*) RTPpacket->payload, |
| 373 | bufferInst->payloadLocation[bufferPosition], |
| 374 | (bufferInst->payloadLengthBytes[bufferPosition] + 1) >> 1); /*length in WebRtc_Word16*/ |
| 375 | |
| 376 | /* Copy payload parameters */ |
| 377 | RTPpacket->payloadLen = bufferInst->payloadLengthBytes[bufferPosition]; |
| 378 | RTPpacket->payloadType = bufferInst->payloadType[bufferPosition]; |
| 379 | RTPpacket->seqNumber = bufferInst->seqNumber[bufferPosition]; |
| 380 | RTPpacket->timeStamp = bufferInst->timeStamp[bufferPosition]; |
| 381 | RTPpacket->rcuPlCntr = bufferInst->rcuPlCntr[bufferPosition]; |
henrik.lundin@webrtc.org | dbba1f9 | 2011-12-20 15:45:05 +0000 | [diff] [blame] | 382 | *waitingTime = bufferInst->waitingTime[bufferPosition]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 383 | RTPpacket->starts_byte1 = 0; /* payload is 16-bit aligned */ |
| 384 | |
| 385 | /* Clear the position in the packet buffer */ |
| 386 | bufferInst->payloadType[bufferPosition] = -1; |
| 387 | bufferInst->payloadLengthBytes[bufferPosition] = 0; |
| 388 | bufferInst->seqNumber[bufferPosition] = 0; |
| 389 | bufferInst->timeStamp[bufferPosition] = 0; |
henrik.lundin@webrtc.org | dbba1f9 | 2011-12-20 15:45:05 +0000 | [diff] [blame] | 390 | bufferInst->waitingTime[bufferPosition] = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 391 | bufferInst->payloadLocation[bufferPosition] = bufferInst->startPayloadMemory; |
| 392 | |
| 393 | /* Reduce packet counter with one */ |
| 394 | bufferInst->numPacketsInBuffer--; |
| 395 | |
| 396 | return (0); |
| 397 | } |
| 398 | |
kma@webrtc.org | 89a1000 | 2012-01-30 15:37:33 +0000 | [diff] [blame] | 399 | int WebRtcNetEQ_PacketBufferFindLowestTimestamp(PacketBuf_t* buffer_inst, |
| 400 | uint32_t current_time_stamp, |
| 401 | uint32_t* time_stamp, |
| 402 | int* buffer_position, |
| 403 | int erase_old_packets, |
| 404 | int16_t* payload_type) { |
| 405 | int32_t time_stamp_diff = WEBRTC_SPL_WORD32_MAX; /* Smallest diff found. */ |
| 406 | int32_t new_diff; |
| 407 | int i; |
| 408 | int16_t rcu_payload_cntr; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 409 | |
kma@webrtc.org | 89a1000 | 2012-01-30 15:37:33 +0000 | [diff] [blame] | 410 | if (buffer_inst->startPayloadMemory == NULL) { |
| 411 | /* Packet buffer has not been initialized. */ |
| 412 | return PBUFFER_NOT_INITIALIZED; |
| 413 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 414 | |
kma@webrtc.org | 89a1000 | 2012-01-30 15:37:33 +0000 | [diff] [blame] | 415 | /* Initialize all return values. */ |
| 416 | *time_stamp = 0; |
| 417 | *payload_type = -1; /* Indicates that no packet was found. */ |
| 418 | *buffer_position = -1; /* Indicates that no packet was found. */ |
| 419 | rcu_payload_cntr = WEBRTC_SPL_WORD16_MAX; /* Indicates no packet found. */ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 420 | |
kma@webrtc.org | 89a1000 | 2012-01-30 15:37:33 +0000 | [diff] [blame] | 421 | /* Check if buffer is empty. */ |
| 422 | if (buffer_inst->numPacketsInBuffer <= 0) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 423 | return 0; |
kma@webrtc.org | 89a1000 | 2012-01-30 15:37:33 +0000 | [diff] [blame] | 424 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 425 | |
kma@webrtc.org | 89a1000 | 2012-01-30 15:37:33 +0000 | [diff] [blame] | 426 | /* Loop through all slots in buffer. */ |
| 427 | if (erase_old_packets) { /* If old payloads should be discarded. */ |
| 428 | for (i = 0; i < buffer_inst->maxInsertPositions; ++i) { |
| 429 | /* Calculate difference between this slot and current_time_stamp. */ |
| 430 | new_diff = (int32_t)(buffer_inst->timeStamp[i] - current_time_stamp); |
| 431 | |
| 432 | /* Check if payload should be discarded. */ |
| 433 | if ((new_diff < 0) /* Payload is too old */ |
| 434 | && (new_diff > -30000) /* Account for TS wrap-around. */ |
| 435 | && (buffer_inst->payloadLengthBytes[i] > 0)) { /* Payload exists. */ |
| 436 | /* Throw away old packet. */ |
| 437 | |
| 438 | /* Clear the position in the buffer. */ |
| 439 | buffer_inst->payloadType[i] = -1; |
| 440 | buffer_inst->payloadLengthBytes[i] = 0; |
| 441 | |
| 442 | /* Reduce packet counter by one. */ |
| 443 | buffer_inst->numPacketsInBuffer--; |
| 444 | /* Increase discard counter for in-call statistics. */ |
| 445 | buffer_inst->discardedPackets++; |
| 446 | } else if (((new_diff < time_stamp_diff) |
| 447 | || ((new_diff == time_stamp_diff) |
| 448 | && (buffer_inst->rcuPlCntr[i] < rcu_payload_cntr))) |
| 449 | && (buffer_inst->payloadLengthBytes[i] > 0)) { |
| 450 | /* New diff is smaller than previous diffs or we have a candidate with a |
| 451 | * time stamp as previous candidate but better RCU-counter; |
| 452 | * and the payload exists. |
| 453 | */ |
| 454 | /* Save this position as the best candidate. */ |
| 455 | *buffer_position = i; |
| 456 | time_stamp_diff = new_diff; |
| 457 | *payload_type = buffer_inst->payloadType[i]; |
| 458 | rcu_payload_cntr = buffer_inst->rcuPlCntr[i]; |
| 459 | } |
| 460 | } |
| 461 | } else { |
| 462 | for (i = 0; i < buffer_inst->maxInsertPositions; ++i) { |
| 463 | /* Calculate difference between this slot and current_time_stamp. */ |
| 464 | new_diff = (int32_t)(buffer_inst->timeStamp[i] - current_time_stamp); |
| 465 | |
| 466 | /* Check if this is the oldest packet. */ |
| 467 | if (((new_diff < time_stamp_diff) |
| 468 | || ((new_diff == time_stamp_diff) |
| 469 | && (buffer_inst->rcuPlCntr[i] < rcu_payload_cntr))) |
| 470 | && (buffer_inst->payloadLengthBytes[i] > 0)) { |
| 471 | /* New diff is smaller than previous diffs or we have a candidate with a |
| 472 | * time_stamp as previous candidate but better RCU-counter; |
| 473 | * and the payload exists. |
| 474 | */ |
| 475 | /* Save this position as the best candidate. */ |
| 476 | *buffer_position = i; |
| 477 | time_stamp_diff = new_diff; |
| 478 | *payload_type = buffer_inst->payloadType[i]; |
| 479 | rcu_payload_cntr = buffer_inst->rcuPlCntr[i]; |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | /* Check that we did find a real position. */ |
| 485 | if (*buffer_position >= 0) { |
| 486 | /* Get the time_stamp for the best position. */ |
| 487 | *time_stamp = buffer_inst->timeStamp[*buffer_position]; |
| 488 | } |
| 489 | |
| 490 | return 0; |
| 491 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 492 | |
tina.legrand@webrtc.org | 5ac387c | 2012-11-19 08:02:55 +0000 | [diff] [blame] | 493 | int WebRtcNetEQ_PacketBufferGetPacketSize(const PacketBuf_t* buffer_inst, |
| 494 | int buffer_pos, |
| 495 | const CodecDbInst_t* codec_database, |
| 496 | int codec_pos, int last_duration) { |
| 497 | if (codec_database->funcDurationEst[codec_pos] == NULL) { |
| 498 | return last_duration; |
| 499 | } |
| 500 | return (*codec_database->funcDurationEst[codec_pos])( |
| 501 | codec_database->codec_state[codec_pos], |
| 502 | (const uint8_t *)buffer_inst->payloadLocation[buffer_pos], |
| 503 | buffer_inst->payloadLengthBytes[buffer_pos]); |
| 504 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 505 | |
tina.legrand@webrtc.org | 5ac387c | 2012-11-19 08:02:55 +0000 | [diff] [blame] | 506 | WebRtc_Word32 WebRtcNetEQ_PacketBufferGetSize(const PacketBuf_t* buffer_inst, |
| 507 | const CodecDbInst_t* |
| 508 | codec_database) { |
| 509 | int i, count; |
| 510 | int last_duration; |
| 511 | int last_codec_pos; |
| 512 | int last_payload_type; |
| 513 | WebRtc_Word32 size_samples; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 514 | |
tina.legrand@webrtc.org | 5ac387c | 2012-11-19 08:02:55 +0000 | [diff] [blame] | 515 | count = 0; |
| 516 | last_duration = buffer_inst->packSizeSamples; |
| 517 | last_codec_pos = -1; |
| 518 | last_payload_type = -1; |
| 519 | size_samples = 0; |
| 520 | |
| 521 | /* Loop through all slots in the buffer */ |
| 522 | for (i = 0; i < buffer_inst->maxInsertPositions; i++) { |
| 523 | /* Only count the packets with non-zero size */ |
| 524 | if (buffer_inst->payloadLengthBytes[i] != 0) { |
| 525 | int payload_type; |
| 526 | int codec_pos; |
| 527 | /* Figure out the codec database entry for this payload_type. */ |
| 528 | payload_type = buffer_inst->payloadType[i]; |
| 529 | /* Remember the last one, to avoid the database search. */ |
| 530 | if(payload_type == last_payload_type) { |
| 531 | codec_pos = last_codec_pos; |
| 532 | } |
| 533 | else { |
| 534 | codec_pos = WebRtcNetEQ_DbGetCodec(codec_database, |
| 535 | payload_type); |
| 536 | if (codec_pos >= 0) { |
| 537 | codec_pos = codec_database->position[codec_pos]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 538 | } |
tina.legrand@webrtc.org | 5ac387c | 2012-11-19 08:02:55 +0000 | [diff] [blame] | 539 | } |
| 540 | last_codec_pos = codec_pos; |
| 541 | last_payload_type = payload_type; |
| 542 | if (codec_pos >= 0) { |
| 543 | /* |
| 544 | * Right now WebRtcNetEQ_PacketBufferGetPacketSize either always |
| 545 | * returns last_duration or always computes the real duration without |
| 546 | * looking at last_duration. If an implementation really wanted to use |
| 547 | * last_duration to compute a changing duration, we would have to |
| 548 | * iterate through the packets in chronological order by timestamp. |
| 549 | */ |
| 550 | last_duration = WebRtcNetEQ_PacketBufferGetPacketSize( |
| 551 | buffer_inst, i, codec_database, codec_pos, |
| 552 | last_duration); |
| 553 | } |
| 554 | /* Add in the size of this packet. */ |
| 555 | size_samples += last_duration; |
| 556 | count++; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 557 | } |
tina.legrand@webrtc.org | 5ac387c | 2012-11-19 08:02:55 +0000 | [diff] [blame] | 558 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 559 | |
tina.legrand@webrtc.org | 5ac387c | 2012-11-19 08:02:55 +0000 | [diff] [blame] | 560 | /* Sanity check; size cannot be negative */ |
| 561 | if (size_samples < 0) { |
| 562 | size_samples = 0; |
| 563 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 564 | |
tina.legrand@webrtc.org | 5ac387c | 2012-11-19 08:02:55 +0000 | [diff] [blame] | 565 | return size_samples; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 566 | } |
| 567 | |
henrik.lundin@webrtc.org | dbba1f9 | 2011-12-20 15:45:05 +0000 | [diff] [blame] | 568 | void WebRtcNetEQ_IncrementWaitingTimes(PacketBuf_t *buffer_inst) { |
| 569 | int i; |
| 570 | /* Loop through all slots in the buffer. */ |
| 571 | for (i = 0; i < buffer_inst->maxInsertPositions; ++i) { |
| 572 | /* Only increment waiting time for the packets with non-zero size. */ |
| 573 | if (buffer_inst->payloadLengthBytes[i] != 0) { |
| 574 | buffer_inst->waitingTime[i]++; |
| 575 | } |
| 576 | } |
| 577 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 578 | |
| 579 | int WebRtcNetEQ_GetDefaultCodecSettings(const enum WebRtcNetEQDecoder *codecID, |
| 580 | int noOfCodecs, int *maxBytes, int *maxSlots) |
| 581 | { |
| 582 | int i; |
| 583 | int ok = 0; |
| 584 | WebRtc_Word16 w16_tmp; |
| 585 | WebRtc_Word16 codecBytes; |
| 586 | WebRtc_Word16 codecBuffers; |
| 587 | |
| 588 | /* Initialize return variables to zero */ |
| 589 | *maxBytes = 0; |
| 590 | *maxSlots = 0; |
| 591 | |
| 592 | /* Loop through all codecs supplied to function */ |
| 593 | for (i = 0; i < noOfCodecs; i++) |
| 594 | { |
| 595 | /* Find current codec and set parameters accordingly */ |
| 596 | |
tina.legrand@webrtc.org | 4517585 | 2012-06-01 09:27:35 +0000 | [diff] [blame] | 597 | if ((codecID[i] == kDecoderPCMu) || (codecID[i] == kDecoderPCMu_2ch)) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 598 | { |
| 599 | codecBytes = 1680; /* Up to 210ms @ 64kbps */ |
| 600 | codecBuffers = 30; /* Down to 5ms frames */ |
| 601 | } |
tina.legrand@webrtc.org | 4517585 | 2012-06-01 09:27:35 +0000 | [diff] [blame] | 602 | else if ((codecID[i] == kDecoderPCMa) || |
| 603 | (codecID[i] == kDecoderPCMa_2ch)) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 604 | { |
| 605 | codecBytes = 1680; /* Up to 210ms @ 64kbps */ |
| 606 | codecBuffers = 30; /* Down to 5ms frames */ |
| 607 | } |
| 608 | else if (codecID[i] == kDecoderILBC) |
| 609 | { |
| 610 | codecBytes = 380; /* 200ms @ 15.2kbps (20ms frames) */ |
| 611 | codecBuffers = 10; |
| 612 | } |
| 613 | else if (codecID[i] == kDecoderISAC) |
| 614 | { |
| 615 | codecBytes = 960; /* 240ms @ 32kbps (60ms frames) */ |
| 616 | codecBuffers = 8; |
| 617 | } |
| 618 | else if (codecID[i] == kDecoderISACswb) |
| 619 | { |
| 620 | codecBytes = 1560; /* 240ms @ 52kbps (30ms frames) */ |
| 621 | codecBuffers = 8; |
| 622 | } |
tina.legrand@webrtc.org | c459058 | 2012-11-28 12:23:29 +0000 | [diff] [blame] | 623 | else if (codecID[i] == kDecoderOpus) |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 624 | { |
| 625 | codecBytes = 15300; /* 240ms @ 510kbps (60ms frames) */ |
| 626 | codecBuffers = 30; /* Replicating the value for PCMu/a */ |
| 627 | } |
tina.legrand@webrtc.org | 4517585 | 2012-06-01 09:27:35 +0000 | [diff] [blame] | 628 | else if ((codecID[i] == kDecoderPCM16B) || |
| 629 | (codecID[i] == kDecoderPCM16B_2ch)) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 630 | { |
| 631 | codecBytes = 3360; /* 210ms */ |
| 632 | codecBuffers = 15; |
| 633 | } |
tina.legrand@webrtc.org | 4517585 | 2012-06-01 09:27:35 +0000 | [diff] [blame] | 634 | else if ((codecID[i] == kDecoderPCM16Bwb) || |
| 635 | (codecID[i] == kDecoderPCM16Bwb_2ch)) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 636 | { |
| 637 | codecBytes = 6720; /* 210ms */ |
| 638 | codecBuffers = 15; |
| 639 | } |
tina.legrand@webrtc.org | 4517585 | 2012-06-01 09:27:35 +0000 | [diff] [blame] | 640 | else if ((codecID[i] == kDecoderPCM16Bswb32kHz) || |
| 641 | (codecID[i] == kDecoderPCM16Bswb32kHz_2ch)) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 642 | { |
| 643 | codecBytes = 13440; /* 210ms */ |
| 644 | codecBuffers = 15; |
| 645 | } |
| 646 | else if (codecID[i] == kDecoderPCM16Bswb48kHz) |
| 647 | { |
| 648 | codecBytes = 20160; /* 210ms */ |
| 649 | codecBuffers = 15; |
| 650 | } |
tina.legrand@webrtc.org | 4517585 | 2012-06-01 09:27:35 +0000 | [diff] [blame] | 651 | else if ((codecID[i] == kDecoderG722) || |
| 652 | (codecID[i] == kDecoderG722_2ch)) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 653 | { |
| 654 | codecBytes = 1680; /* 210ms @ 64kbps */ |
| 655 | codecBuffers = 15; |
| 656 | } |
| 657 | else if (codecID[i] == kDecoderRED) |
| 658 | { |
| 659 | codecBytes = 0; /* Should not be max... */ |
| 660 | codecBuffers = 0; |
| 661 | } |
| 662 | else if (codecID[i] == kDecoderAVT) |
| 663 | { |
| 664 | codecBytes = 0; /* Should not be max... */ |
| 665 | codecBuffers = 0; |
| 666 | } |
| 667 | else if (codecID[i] == kDecoderCNG) |
| 668 | { |
| 669 | codecBytes = 0; /* Should not be max... */ |
| 670 | codecBuffers = 0; |
| 671 | } |
| 672 | else if (codecID[i] == kDecoderG729) |
| 673 | { |
| 674 | codecBytes = 210; /* 210ms @ 8kbps */ |
| 675 | codecBuffers = 20; /* max 200ms supported for 10ms frames */ |
| 676 | } |
| 677 | else if (codecID[i] == kDecoderG729_1) |
| 678 | { |
| 679 | codecBytes = 840; /* 210ms @ 32kbps */ |
| 680 | codecBuffers = 10; /* max 200ms supported for 20ms frames */ |
| 681 | } |
| 682 | else if (codecID[i] == kDecoderG726_16) |
| 683 | { |
| 684 | codecBytes = 400; /* 200ms @ 16kbps */ |
| 685 | codecBuffers = 10; |
| 686 | } |
| 687 | else if (codecID[i] == kDecoderG726_24) |
| 688 | { |
| 689 | codecBytes = 600; /* 200ms @ 24kbps */ |
| 690 | codecBuffers = 10; |
| 691 | } |
| 692 | else if (codecID[i] == kDecoderG726_32) |
| 693 | { |
| 694 | codecBytes = 800; /* 200ms @ 32kbps */ |
| 695 | codecBuffers = 10; |
| 696 | } |
| 697 | else if (codecID[i] == kDecoderG726_40) |
| 698 | { |
| 699 | codecBytes = 1000; /* 200ms @ 40kbps */ |
| 700 | codecBuffers = 10; |
| 701 | } |
| 702 | else if (codecID[i] == kDecoderG722_1_16) |
| 703 | { |
| 704 | codecBytes = 420; /* 210ms @ 16kbps */ |
| 705 | codecBuffers = 10; |
| 706 | } |
| 707 | else if (codecID[i] == kDecoderG722_1_24) |
| 708 | { |
| 709 | codecBytes = 630; /* 210ms @ 24kbps */ |
| 710 | codecBuffers = 10; |
| 711 | } |
| 712 | else if (codecID[i] == kDecoderG722_1_32) |
| 713 | { |
| 714 | codecBytes = 840; /* 210ms @ 32kbps */ |
| 715 | codecBuffers = 10; |
| 716 | } |
| 717 | else if (codecID[i] == kDecoderG722_1C_24) |
| 718 | { |
| 719 | codecBytes = 630; /* 210ms @ 24kbps */ |
| 720 | codecBuffers = 10; |
| 721 | } |
| 722 | else if (codecID[i] == kDecoderG722_1C_32) |
| 723 | { |
| 724 | codecBytes = 840; /* 210ms @ 32kbps */ |
| 725 | codecBuffers = 10; |
| 726 | } |
| 727 | else if (codecID[i] == kDecoderG722_1C_48) |
| 728 | { |
| 729 | codecBytes = 1260; /* 210ms @ 48kbps */ |
| 730 | codecBuffers = 10; |
| 731 | } |
| 732 | else if (codecID[i] == kDecoderSPEEX_8) |
| 733 | { |
| 734 | codecBytes = 1250; /* 210ms @ 50kbps */ |
| 735 | codecBuffers = 10; |
| 736 | } |
| 737 | else if (codecID[i] == kDecoderSPEEX_16) |
| 738 | { |
| 739 | codecBytes = 1250; /* 210ms @ 50kbps */ |
| 740 | codecBuffers = 10; |
| 741 | } |
tina.legrand@webrtc.org | 4517585 | 2012-06-01 09:27:35 +0000 | [diff] [blame] | 742 | else if ((codecID[i] == kDecoderCELT_32) || |
| 743 | (codecID[i] == kDecoderCELT_32_2ch)) |
tina.legrand@webrtc.org | df69775 | 2012-02-08 10:22:21 +0000 | [diff] [blame] | 744 | { |
| 745 | codecBytes = 1250; /* 210ms @ 50kbps */ |
| 746 | codecBuffers = 10; |
| 747 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 748 | else if (codecID[i] == kDecoderGSMFR) |
| 749 | { |
| 750 | codecBytes = 340; /* 200ms */ |
| 751 | codecBuffers = 10; |
| 752 | } |
| 753 | else if (codecID[i] == kDecoderAMR) |
| 754 | { |
| 755 | codecBytes = 384; /* 240ms @ 12.2kbps+headers (60ms frames) */ |
| 756 | codecBuffers = 10; |
| 757 | } |
| 758 | else if (codecID[i] == kDecoderAMRWB) |
| 759 | { |
| 760 | codecBytes = 744; |
| 761 | codecBuffers = 10; |
| 762 | } |
| 763 | else if (codecID[i] == kDecoderArbitrary) |
| 764 | { |
| 765 | codecBytes = 6720; /* Assume worst case uncompressed WB 210ms */ |
| 766 | codecBuffers = 15; |
| 767 | } |
| 768 | else |
| 769 | { |
| 770 | /*Unknow codec */ |
| 771 | codecBytes = 0; |
| 772 | codecBuffers = 0; |
| 773 | ok = CODEC_DB_UNKNOWN_CODEC; |
| 774 | } |
| 775 | |
| 776 | /* Update max variables */ |
| 777 | *maxBytes = WEBRTC_SPL_MAX((*maxBytes), codecBytes); |
| 778 | *maxSlots = WEBRTC_SPL_MAX((*maxSlots), codecBuffers); |
| 779 | |
| 780 | } /* end of for loop */ |
| 781 | |
| 782 | /* |
| 783 | * Add size needed by the additional pointers for each slot inside struct, |
| 784 | * as indicated on each line below. |
| 785 | */ |
| 786 | w16_tmp = (sizeof(WebRtc_UWord32) /* timeStamp */ |
| 787 | + sizeof(WebRtc_Word16*) /* payloadLocation */ |
| 788 | + sizeof(WebRtc_UWord16) /* seqNumber */ |
henrik.lundin@webrtc.org | dbba1f9 | 2011-12-20 15:45:05 +0000 | [diff] [blame] | 789 | + sizeof(WebRtc_Word16) /* payloadType */ |
| 790 | + sizeof(WebRtc_Word16) /* payloadLengthBytes */ |
| 791 | + sizeof(WebRtc_Word16) /* rcuPlCntr */ |
| 792 | + sizeof(int)); /* waitingTime */ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 793 | /* Add the extra size per slot to the memory count */ |
| 794 | *maxBytes += w16_tmp * (*maxSlots); |
| 795 | |
| 796 | return ok; |
| 797 | } |