| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include <stdio.h> |
| 17 |
#include <stdlib.h> |
| 18 |
#include <string.h> |
| 19 |
#include <sys/types.h> |
| 20 |
|
| 21 |
#include "jitterbuf.h" |
| 22 |
|
| 23 |
|
| 24 |
#define JB_LONGMAX 2147483647L |
| 25 |
#define JB_LONGMIN (-JB_LONGMAX - 1L) |
| 26 |
|
| 27 |
|
| 28 |
#if (defined(WIN32) || defined(_WIN32_WCE)) && defined(_MSC_VER) |
| 29 |
#define jb_warn if (warnf) warnf |
| 30 |
#define jb_err if (errf) errf |
| 31 |
#define jb_dbg if (dbgf) dbgf |
| 32 |
|
| 33 |
#ifdef DEEP_DEBUG |
| 34 |
#define jb_dbg2 if (dbgf) dbgf |
| 35 |
#else |
| 36 |
#define jb_dbg2 if (0) dbgf |
| 37 |
#endif |
| 38 |
|
| 39 |
#else |
| 40 |
|
| 41 |
#define jb_warn(...) (warnf ? warnf(__VA_ARGS__) : (void)0) |
| 42 |
#define jb_err(...) (errf ? errf(__VA_ARGS__) : (void)0) |
| 43 |
#define jb_dbg(...) (dbgf ? dbgf(__VA_ARGS__) : (void)0) |
| 44 |
|
| 45 |
#ifdef DEEP_DEBUG |
| 46 |
#define jb_dbg2(...) (dbgf ? dbgf(__VA_ARGS__) : (void)0) |
| 47 |
#else |
| 48 |
#define jb_dbg2(...) ((void)0) |
| 49 |
#endif |
| 50 |
|
| 51 |
#endif |
| 52 |
|
| 53 |
static jb_output_function_t warnf, errf, dbgf; |
| 54 |
|
| 55 |
void jb_setoutput(jb_output_function_t err, jb_output_function_t warn, jb_output_function_t dbg) |
| 56 |
{ |
| 57 |
errf = err; |
| 58 |
warnf = warn; |
| 59 |
dbgf = dbg; |
| 60 |
} |
| 61 |
|
| 62 |
static void increment_losspct(jitterbuf *jb) |
| 63 |
{ |
| 64 |
jb->info.losspct = (100000 + 499 * jb->info.losspct)/500; |
| 65 |
} |
| 66 |
|
| 67 |
static void decrement_losspct(jitterbuf *jb) |
| 68 |
{ |
| 69 |
jb->info.losspct = (499 * jb->info.losspct)/500; |
| 70 |
} |
| 71 |
|
| 72 |
void jb_reset(jitterbuf *jb) |
| 73 |
{ |
| 74 |
|
| 75 |
jb_conf s = jb->info.conf; |
| 76 |
memset(jb, 0, sizeof(*jb)); |
| 77 |
jb->info.conf = s; |
| 78 |
|
| 79 |
|
| 80 |
jb->info.current = jb->info.target = jb->info.conf.target_extra; |
| 81 |
jb->info.silence_begin_ts = -1; |
| 82 |
} |
| 83 |
|
| 84 |
jitterbuf * jb_new() |
| 85 |
{ |
| 86 |
jitterbuf *jb; |
| 87 |
|
| 88 |
if (!(jb = (jitterbuf *)malloc(sizeof(*jb)))) |
| 89 |
return NULL; |
| 90 |
|
| 91 |
jb->info.conf.target_extra = JB_TARGET_EXTRA; |
| 92 |
|
| 93 |
jb_reset(jb); |
| 94 |
|
| 95 |
jb_dbg2("jb_new() = %x\n", jb); |
| 96 |
return jb; |
| 97 |
} |
| 98 |
|
| 99 |
void jb_destroy(jitterbuf *jb) |
| 100 |
{ |
| 101 |
jb_frame *frame; |
| 102 |
jb_dbg2("jb_destroy(%x)\n", jb); |
| 103 |
|
| 104 |
|
| 105 |
frame = jb->free; |
| 106 |
while (frame != NULL) { |
| 107 |
jb_frame *next = frame->next; |
| 108 |
free(frame); |
| 109 |
frame = next; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
free(jb); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
#if 0 |
| 119 |
static int longcmp(const void *a, const void *b) |
| 120 |
{ |
| 121 |
return *(long *)a - *(long *)b; |
| 122 |
} |
| 123 |
#endif |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
static int history_put(jitterbuf *jb, long ts, long now, long ms) |
| 130 |
{ |
| 131 |
long delay = now - (ts - jb->info.resync_offset); |
| 132 |
long threshold = 2 * jb->info.jitter + jb->info.conf.resync_threshold; |
| 133 |
long kicked; |
| 134 |
|
| 135 |
|
| 136 |
if (ts <= 0) |
| 137 |
return 0; |
| 138 |
|
| 139 |
|
| 140 |
if (jb->info.conf.resync_threshold != -1) { |
| 141 |
if (labs(delay - jb->info.last_delay) > threshold) { |
| 142 |
jb->info.cnt_delay_discont++; |
| 143 |
if (jb->info.cnt_delay_discont > 3) { |
| 144 |
|
| 145 |
jb->info.cnt_delay_discont = 0; |
| 146 |
jb->hist_ptr = 0; |
| 147 |
jb->hist_maxbuf_valid = 0; |
| 148 |
|
| 149 |
jb_warn("Resyncing the jb. last_delay %ld, this delay %ld, threshold %ld, new offset %ld\n", jb->info.last_delay, delay, threshold, ts - now); |
| 150 |
jb->info.resync_offset = ts - now; |
| 151 |
jb->info.last_delay = delay = 0; |
| 152 |
} else { |
| 153 |
return -1; |
| 154 |
} |
| 155 |
} else { |
| 156 |
jb->info.last_delay = delay; |
| 157 |
jb->info.cnt_delay_discont = 0; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
kicked = jb->history[jb->hist_ptr % JB_HISTORY_SZ]; |
| 162 |
|
| 163 |
jb->history[(jb->hist_ptr++) % JB_HISTORY_SZ] = delay; |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
if (!jb->hist_maxbuf_valid) |
| 173 |
return 0; |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
if (jb->hist_ptr < JB_HISTORY_SZ) |
| 178 |
goto invalidate; |
| 179 |
|
| 180 |
|
| 181 |
if (delay < jb->hist_minbuf[JB_HISTORY_MAXBUF_SZ-1]) |
| 182 |
goto invalidate; |
| 183 |
|
| 184 |
|
| 185 |
if (delay > jb->hist_maxbuf[JB_HISTORY_MAXBUF_SZ-1]) |
| 186 |
goto invalidate; |
| 187 |
|
| 188 |
|
| 189 |
if (kicked <= jb->hist_minbuf[JB_HISTORY_MAXBUF_SZ-1]) |
| 190 |
goto invalidate; |
| 191 |
|
| 192 |
if (kicked >= jb->hist_maxbuf[JB_HISTORY_MAXBUF_SZ-1]) |
| 193 |
goto invalidate; |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
return 0; |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
invalidate: |
| 202 |
jb->hist_maxbuf_valid = 0; |
| 203 |
return 0; |
| 204 |
} |
| 205 |
|
| 206 |
static void history_calc_maxbuf(jitterbuf *jb) |
| 207 |
{ |
| 208 |
int i,j; |
| 209 |
|
| 210 |
if (jb->hist_ptr == 0) |
| 211 |
return; |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
for (i=0;i<JB_HISTORY_MAXBUF_SZ;i++) { |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
jb->hist_maxbuf[i] = JB_LONGMIN; |
| 221 |
jb->hist_minbuf[i] = JB_LONGMAX; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
i = (jb->hist_ptr > JB_HISTORY_SZ) ? (jb->hist_ptr - JB_HISTORY_SZ) : 0; |
| 229 |
|
| 230 |
for (;i<jb->hist_ptr;i++) { |
| 231 |
long toins = jb->history[i % JB_HISTORY_SZ]; |
| 232 |
|
| 233 |
|
| 234 |
if (toins > jb->hist_maxbuf[JB_HISTORY_MAXBUF_SZ-1]) { |
| 235 |
|
| 236 |
|
| 237 |
for (j=0;j<JB_HISTORY_MAXBUF_SZ;j++) { |
| 238 |
|
| 239 |
if (toins > jb->hist_maxbuf[j]) { |
| 240 |
|
| 241 |
memmove(jb->hist_maxbuf + j + 1, jb->hist_maxbuf + j, (JB_HISTORY_MAXBUF_SZ - (j + 1)) * sizeof(jb->hist_maxbuf[0])); |
| 242 |
|
| 243 |
jb->hist_maxbuf[j] = toins; |
| 244 |
|
| 245 |
break; |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
if (toins < jb->hist_minbuf[JB_HISTORY_MAXBUF_SZ-1]) { |
| 252 |
|
| 253 |
|
| 254 |
for (j=0;j<JB_HISTORY_MAXBUF_SZ;j++) { |
| 255 |
|
| 256 |
if (toins < jb->hist_minbuf[j]) { |
| 257 |
|
| 258 |
memmove(jb->hist_minbuf + j + 1, jb->hist_minbuf + j, (JB_HISTORY_MAXBUF_SZ - (j + 1)) * sizeof(jb->hist_minbuf[0])); |
| 259 |
|
| 260 |
jb->hist_minbuf[j] = toins; |
| 261 |
|
| 262 |
break; |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
if (0) { |
| 268 |
int k; |
| 269 |
fprintf(stderr, "toins = %ld\n", toins); |
| 270 |
fprintf(stderr, "maxbuf ="); |
| 271 |
for (k=0;k<JB_HISTORY_MAXBUF_SZ;k++) |
| 272 |
fprintf(stderr, "%ld ", jb->hist_maxbuf[k]); |
| 273 |
fprintf(stderr, "\nminbuf ="); |
| 274 |
for (k=0;k<JB_HISTORY_MAXBUF_SZ;k++) |
| 275 |
fprintf(stderr, "%ld ", jb->hist_minbuf[k]); |
| 276 |
fprintf(stderr, "\n"); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
jb->hist_maxbuf_valid = 1; |
| 281 |
} |
| 282 |
|
| 283 |
static void history_get(jitterbuf *jb) |
| 284 |
{ |
| 285 |
long max, min, jitter; |
| 286 |
int index; |
| 287 |
int count; |
| 288 |
|
| 289 |
if (!jb->hist_maxbuf_valid) |
| 290 |
history_calc_maxbuf(jb); |
| 291 |
|
| 292 |
|
| 293 |
count = (jb->hist_ptr < JB_HISTORY_SZ) ? jb->hist_ptr : JB_HISTORY_SZ; |
| 294 |
|
| 295 |
|
| 296 |
index = count * JB_HISTORY_DROPPCT / 100; |
| 297 |
|
| 298 |
|
| 299 |
if (index > (JB_HISTORY_MAXBUF_SZ - 1)) |
| 300 |
index = JB_HISTORY_MAXBUF_SZ - 1; |
| 301 |
|
| 302 |
|
| 303 |
if (index < 0) { |
| 304 |
jb->info.min = 0; |
| 305 |
jb->info.jitter = 0; |
| 306 |
return; |
| 307 |
} |
| 308 |
|
| 309 |
max = jb->hist_maxbuf[index]; |
| 310 |
min = jb->hist_minbuf[index]; |
| 311 |
|
| 312 |
jitter = max - min; |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
jb->info.min = min; |
| 322 |
jb->info.jitter = jitter; |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
static int queue_put(jitterbuf *jb, void *data, const enum jb_frame_type type, long ms, long ts) |
| 327 |
{ |
| 328 |
jb_frame *frame; |
| 329 |
jb_frame *p; |
| 330 |
int head = 0; |
| 331 |
long resync_ts = ts - jb->info.resync_offset; |
| 332 |
|
| 333 |
if ((frame = jb->free)) { |
| 334 |
jb->free = frame->next; |
| 335 |
} else if (!(frame = (jb_frame *)malloc(sizeof(*frame)))) { |
| 336 |
jb_err("cannot allocate frame\n"); |
| 337 |
return 0; |
| 338 |
} |
| 339 |
|
| 340 |
jb->info.frames_cur++; |
| 341 |
|
| 342 |
frame->data = data; |
| 343 |
frame->ts = resync_ts; |
| 344 |
frame->ms = ms; |
| 345 |
frame->type = type; |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
if (!jb->frames) { |
| 353 |
jb->frames = frame; |
| 354 |
frame->next = frame; |
| 355 |
frame->prev = frame; |
| 356 |
head = 1; |
| 357 |
} else if (resync_ts < jb->frames->ts) { |
| 358 |
frame->next = jb->frames; |
| 359 |
frame->prev = jb->frames->prev; |
| 360 |
|
| 361 |
frame->next->prev = frame; |
| 362 |
frame->prev->next = frame; |
| 363 |
|
| 364 |
|
| 365 |
jb->info.frames_ooo++; |
| 366 |
|
| 367 |
jb->frames = frame; |
| 368 |
head = 1; |
| 369 |
} else { |
| 370 |
p = jb->frames; |
| 371 |
|
| 372 |
|
| 373 |
if (resync_ts < p->prev->ts) jb->info.frames_ooo++; |
| 374 |
|
| 375 |
while (resync_ts < p->prev->ts && p->prev != jb->frames) |
| 376 |
p = p->prev; |
| 377 |
|
| 378 |
frame->next = p; |
| 379 |
frame->prev = p->prev; |
| 380 |
|
| 381 |
frame->next->prev = frame; |
| 382 |
frame->prev->next = frame; |
| 383 |
} |
| 384 |
return head; |
| 385 |
} |
| 386 |
|
| 387 |
static long queue_next(jitterbuf *jb) |
| 388 |
{ |
| 389 |
if (jb->frames) |
| 390 |
return jb->frames->ts; |
| 391 |
else |
| 392 |
return -1; |
| 393 |
} |
| 394 |
|
| 395 |
static long queue_last(jitterbuf *jb) |
| 396 |
{ |
| 397 |
if (jb->frames) |
| 398 |
return jb->frames->prev->ts; |
| 399 |
else |
| 400 |
return -1; |
| 401 |
} |
| 402 |
|
| 403 |
static jb_frame *_queue_get(jitterbuf *jb, long ts, int all) |
| 404 |
{ |
| 405 |
jb_frame *frame; |
| 406 |
frame = jb->frames; |
| 407 |
|
| 408 |
if (!frame) |
| 409 |
return NULL; |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
if (all || ts >= frame->ts) { |
| 414 |
|
| 415 |
frame->prev->next = frame->next; |
| 416 |
frame->next->prev = frame->prev; |
| 417 |
|
| 418 |
if (frame->next == frame) |
| 419 |
jb->frames = NULL; |
| 420 |
else |
| 421 |
jb->frames = frame->next; |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
frame->next = jb->free; |
| 426 |
jb->free = frame; |
| 427 |
|
| 428 |
jb->info.frames_cur--; |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
return frame; |
| 433 |
} |
| 434 |
|
| 435 |
return NULL; |
| 436 |
} |
| 437 |
|
| 438 |
static jb_frame *queue_get(jitterbuf *jb, long ts) |
| 439 |
{ |
| 440 |
return _queue_get(jb,ts,0); |
| 441 |
} |
| 442 |
|
| 443 |
static jb_frame *queue_getall(jitterbuf *jb) |
| 444 |
{ |
| 445 |
return _queue_get(jb,0,1); |
| 446 |
} |
| 447 |
|
| 448 |
#if 0 |
| 449 |
|
| 450 |
static void jb_dbginfo(jitterbuf *jb) |
| 451 |
{ |
| 452 |
if (dbgf == NULL) |
| 453 |
return; |
| 454 |
|
| 455 |
jb_dbg("\njb info: fin=%ld fout=%ld flate=%ld flost=%ld fdrop=%ld fcur=%ld\n", |
| 456 |
jb->info.frames_in, jb->info.frames_out, jb->info.frames_late, jb->info.frames_lost, jb->info.frames_dropped, jb->info.frames_cur); |
| 457 |
|
| 458 |
jb_dbg("jitter=%ld current=%ld target=%ld min=%ld sil=%d len=%d len/fcur=%ld\n", |
| 459 |
jb->info.jitter, jb->info.current, jb->info.target, jb->info.min, jb->info.silence_begin_ts, jb->info.current - jb->info.min, |
| 460 |
jb->info.frames_cur ? (jb->info.current - jb->info.min)/jb->info.frames_cur : -8); |
| 461 |
if (jb->info.frames_in > 0) |
| 462 |
jb_dbg("jb info: Loss PCT = %ld%%, Late PCT = %ld%%\n", |
| 463 |
jb->info.frames_lost * 100/(jb->info.frames_in + jb->info.frames_lost), |
| 464 |
jb->info.frames_late * 100/jb->info.frames_in); |
| 465 |
jb_dbg("jb info: queue %d -> %d. last_ts %d (queue len: %d) last_ms %d\n", |
| 466 |
queue_next(jb), |
| 467 |
queue_last(jb), |
| 468 |
jb->info.next_voice_ts, |
| 469 |
queue_last(jb) - queue_next(jb), |
| 470 |
jb->info.last_voice_ms); |
| 471 |
} |
| 472 |
#endif |
| 473 |
|
| 474 |
#ifdef DEEP_DEBUG |
| 475 |
static void jb_chkqueue(jitterbuf *jb) |
| 476 |
{ |
| 477 |
int i=0; |
| 478 |
jb_frame *p = jb->frames; |
| 479 |
|
| 480 |
if (!p) { |
| 481 |
return; |
| 482 |
} |
| 483 |
|
| 484 |
do { |
| 485 |
if (p->next == NULL) { |
| 486 |
jb_err("Queue is BROKEN at item [%d]", i); |
| 487 |
} |
| 488 |
i++; |
| 489 |
p=p->next; |
| 490 |
} while (p->next != jb->frames); |
| 491 |
} |
| 492 |
|
| 493 |
static void jb_dbgqueue(jitterbuf *jb) |
| 494 |
{ |
| 495 |
int i=0; |
| 496 |
jb_frame *p = jb->frames; |
| 497 |
|
| 498 |
jb_dbg("queue: "); |
| 499 |
|
| 500 |
if (!p) { |
| 501 |
jb_dbg("EMPTY\n"); |
| 502 |
return; |
| 503 |
} |
| 504 |
|
| 505 |
do { |
| 506 |
jb_dbg("[%d]=%ld ", i++, p->ts); |
| 507 |
p=p->next; |
| 508 |
} while (p->next != jb->frames); |
| 509 |
|
| 510 |
jb_dbg("\n"); |
| 511 |
} |
| 512 |
#endif |
| 513 |
|
| 514 |
enum jb_return_code jb_put(jitterbuf *jb, void *data, const enum jb_frame_type type, long ms, long ts, long now) |
| 515 |
{ |
| 516 |
jb_dbg2("jb_put(%x,%x,%ld,%ld,%ld)\n", jb, data, ms, ts, now); |
| 517 |
|
| 518 |
jb->info.frames_in++; |
| 519 |
|
| 520 |
if (type == JB_TYPE_VOICE) { |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
if (history_put(jb,ts,now,ms)) |
| 527 |
return JB_DROP; |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
if (queue_put(jb,data,type,ms,ts)) { |
| 532 |
return JB_SCHED; |
| 533 |
} |
| 534 |
|
| 535 |
return JB_OK; |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
static enum jb_return_code _jb_get(jitterbuf *jb, jb_frame *frameout, long now, long interpl) |
| 540 |
{ |
| 541 |
jb_frame *frame; |
| 542 |
long diff; |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
history_get(jb); |
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
jb->info.target = jb->info.jitter + jb->info.min + jb->info.conf.target_extra; |
| 551 |
|
| 552 |
|
| 553 |
if ((jb->info.conf.max_jitterbuf) && ((jb->info.target - jb->info.min) > jb->info.conf.max_jitterbuf)) { |
| 554 |
jb_dbg("clamping target from %d to %d\n", (jb->info.target - jb->info.min), jb->info.conf.max_jitterbuf); |
| 555 |
jb->info.target = jb->info.min + jb->info.conf.max_jitterbuf; |
| 556 |
} |
| 557 |
|
| 558 |
diff = jb->info.target - jb->info.current; |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
if (!jb->info.silence_begin_ts) { |
| 565 |
|
| 566 |
if ((diff > 0) && |
| 567 |
|
| 568 |
(((jb->info.last_adjustment + JB_ADJUST_DELAY) < now) || |
| 569 |
|
| 570 |
(diff > queue_last(jb) - queue_next(jb)) ) ) { |
| 571 |
|
| 572 |
jb->info.current += interpl; |
| 573 |
jb->info.next_voice_ts += interpl; |
| 574 |
jb->info.last_voice_ms = interpl; |
| 575 |
jb->info.last_adjustment = now; |
| 576 |
jb->info.cnt_contig_interp++; |
| 577 |
|
| 578 |
if (jb->info.conf.max_contig_interp && jb->info.cnt_contig_interp >= jb->info.conf.max_contig_interp) { |
| 579 |
jb->info.silence_begin_ts = jb->info.next_voice_ts - jb->info.current; |
| 580 |
} |
| 581 |
jb_dbg("G"); |
| 582 |
return JB_INTERP; |
| 583 |
} |
| 584 |
|
| 585 |
frame = queue_get(jb, jb->info.next_voice_ts - jb->info.current); |
| 586 |
|
| 587 |
|
| 588 |
if (frame && frame->type != JB_TYPE_VOICE) { |
| 589 |
|
| 590 |
if (frame->type == JB_TYPE_SILENCE) { |
| 591 |
jb->info.silence_begin_ts = frame->ts; |
| 592 |
jb->info.cnt_contig_interp = 0; |
| 593 |
} |
| 594 |
|
| 595 |
*frameout = *frame; |
| 596 |
jb->info.frames_out++; |
| 597 |
jb_dbg("o"); |
| 598 |
return JB_OK; |
| 599 |
} |
| 600 |
|
| 601 |
|
| 602 |
if (frame && frame->ts + jb->info.current < jb->info.next_voice_ts) { |
| 603 |
if (frame->ts + jb->info.current > jb->info.next_voice_ts - jb->info.last_voice_ms) { |
| 604 |
|
| 605 |
|
| 606 |
*frameout = *frame; |
| 607 |
|
| 608 |
jb->info.next_voice_ts = frame->ts + jb->info.current + frame->ms; |
| 609 |
jb->info.frames_out++; |
| 610 |
decrement_losspct(jb); |
| 611 |
jb->info.cnt_contig_interp = 0; |
| 612 |
jb_dbg("v"); |
| 613 |
return JB_OK; |
| 614 |
} else { |
| 615 |
|
| 616 |
*frameout = *frame; |
| 617 |
jb->info.frames_out++; |
| 618 |
decrement_losspct(jb); |
| 619 |
jb->info.frames_late++; |
| 620 |
jb->info.frames_lost--; |
| 621 |
jb_dbg("l"); |
| 622 |
|
| 623 |
|
| 624 |
return JB_DROP; |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
|
| 629 |
if (frame && frame->ms > 0) { |
| 630 |
jb->info.last_voice_ms = frame->ms; |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
if (diff < -jb->info.conf.target_extra && |
| 638 |
((!frame && jb->info.last_adjustment + 80 < now) || |
| 639 |
(jb->info.last_adjustment + 500 < now))) { |
| 640 |
|
| 641 |
jb->info.last_adjustment = now; |
| 642 |
jb->info.cnt_contig_interp = 0; |
| 643 |
|
| 644 |
if (frame) { |
| 645 |
*frameout = *frame; |
| 646 |
|
| 647 |
jb->info.current -= frame->ms; |
| 648 |
jb->info.frames_out++; |
| 649 |
decrement_losspct(jb); |
| 650 |
jb->info.frames_dropped++; |
| 651 |
jb_dbg("s"); |
| 652 |
return JB_DROP; |
| 653 |
} else { |
| 654 |
|
| 655 |
jb->info.current -= jb->info.last_voice_ms; |
| 656 |
jb->info.frames_lost++; |
| 657 |
increment_losspct(jb); |
| 658 |
jb_dbg("S"); |
| 659 |
return JB_NOFRAME; |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
|
| 664 |
if (!frame) { |
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
jb->info.frames_lost++; |
| 687 |
increment_losspct(jb); |
| 688 |
jb->info.next_voice_ts += interpl; |
| 689 |
jb->info.last_voice_ms = interpl; |
| 690 |
jb->info.cnt_contig_interp++; |
| 691 |
|
| 692 |
if (jb->info.conf.max_contig_interp && jb->info.cnt_contig_interp >= jb->info.conf.max_contig_interp) { |
| 693 |
jb->info.silence_begin_ts = jb->info.next_voice_ts - jb->info.current; |
| 694 |
} |
| 695 |
jb_dbg("L"); |
| 696 |
return JB_INTERP; |
| 697 |
} |
| 698 |
|
| 699 |
|
| 700 |
*frameout = *frame; |
| 701 |
jb->info.next_voice_ts += frame->ms; |
| 702 |
jb->info.frames_out++; |
| 703 |
jb->info.cnt_contig_interp = 0; |
| 704 |
decrement_losspct(jb); |
| 705 |
jb_dbg("v"); |
| 706 |
return JB_OK; |
| 707 |
} else { |
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
if (diff < -jb->info.conf.target_extra && |
| 717 |
jb->info.last_adjustment + 10 <= now) { |
| 718 |
jb->info.current -= interpl; |
| 719 |
jb->info.last_adjustment = now; |
| 720 |
} |
| 721 |
|
| 722 |
frame = queue_get(jb, now - jb->info.current); |
| 723 |
if (!frame) { |
| 724 |
return JB_NOFRAME; |
| 725 |
} else if (frame->type != JB_TYPE_VOICE) { |
| 726 |
|
| 727 |
*frameout = *frame; |
| 728 |
jb->info.frames_out++; |
| 729 |
return JB_OK; |
| 730 |
} |
| 731 |
if (frame->ts < jb->info.silence_begin_ts) { |
| 732 |
|
| 733 |
*frameout = *frame; |
| 734 |
jb->info.frames_out++; |
| 735 |
decrement_losspct(jb); |
| 736 |
jb->info.frames_late++; |
| 737 |
jb->info.frames_lost--; |
| 738 |
jb_dbg("l"); |
| 739 |
|
| 740 |
|
| 741 |
return JB_DROP; |
| 742 |
} else { |
| 743 |
|
| 744 |
|
| 745 |
jb->info.current = jb->info.target; |
| 746 |
jb->info.silence_begin_ts = 0; |
| 747 |
jb->info.next_voice_ts = frame->ts + jb->info.current + frame->ms; |
| 748 |
jb->info.last_voice_ms = frame->ms; |
| 749 |
jb->info.frames_out++; |
| 750 |
decrement_losspct(jb); |
| 751 |
*frameout = *frame; |
| 752 |
jb_dbg("V"); |
| 753 |
return JB_OK; |
| 754 |
} |
| 755 |
} |
| 756 |
} |
| 757 |
|
| 758 |
long jb_next(jitterbuf *jb) |
| 759 |
{ |
| 760 |
if (jb->info.silence_begin_ts) { |
| 761 |
if (jb->frames) { |
| 762 |
long next = queue_next(jb); |
| 763 |
history_get(jb); |
| 764 |
|
| 765 |
if (jb->info.target - jb->info.current < -jb->info.conf.target_extra) |
| 766 |
return jb->info.last_adjustment + 10; |
| 767 |
return next + jb->info.target; |
| 768 |
} |
| 769 |
else |
| 770 |
return JB_LONGMAX; |
| 771 |
} else { |
| 772 |
return jb->info.next_voice_ts; |
| 773 |
} |
| 774 |
} |
| 775 |
|
| 776 |
enum jb_return_code jb_get(jitterbuf *jb, jb_frame *frameout, long now, long interpl) |
| 777 |
{ |
| 778 |
enum jb_return_code ret = _jb_get(jb, frameout, now, interpl); |
| 779 |
#if 0 |
| 780 |
static int lastts=0; |
| 781 |
int thists = ((ret == JB_OK) || (ret == JB_DROP)) ? frameout->ts : 0; |
| 782 |
jb_warn("jb_get(%x,%x,%ld) = %d (%d)\n", jb, frameout, now, ret, thists); |
| 783 |
if (thists && thists < lastts) jb_warn("XXXX timestamp roll-back!!!\n"); |
| 784 |
lastts = thists; |
| 785 |
#endif |
| 786 |
return ret; |
| 787 |
} |
| 788 |
|
| 789 |
enum jb_return_code jb_getall(jitterbuf *jb, jb_frame *frameout) |
| 790 |
{ |
| 791 |
jb_frame *frame; |
| 792 |
frame = queue_getall(jb); |
| 793 |
|
| 794 |
if (!frame) { |
| 795 |
return JB_NOFRAME; |
| 796 |
} |
| 797 |
|
| 798 |
*frameout = *frame; |
| 799 |
return JB_OK; |
| 800 |
} |
| 801 |
|
| 802 |
|
| 803 |
enum jb_return_code jb_getinfo(jitterbuf *jb, jb_info *stats) |
| 804 |
{ |
| 805 |
history_get(jb); |
| 806 |
|
| 807 |
*stats = jb->info; |
| 808 |
|
| 809 |
return JB_OK; |
| 810 |
} |
| 811 |
|
| 812 |
enum jb_return_code jb_setconf(jitterbuf *jb, jb_conf *conf) |
| 813 |
{ |
| 814 |
|
| 815 |
|
| 816 |
jb->info.conf.max_jitterbuf = conf->max_jitterbuf; |
| 817 |
jb->info.conf.resync_threshold = conf->resync_threshold; |
| 818 |
jb->info.conf.max_contig_interp = conf->max_contig_interp; |
| 819 |
|
| 820 |
|
| 821 |
jb->info.conf.target_extra = ( conf->target_extra == -1 ) |
| 822 |
? JB_TARGET_EXTRA |
| 823 |
: conf->target_extra |
| 824 |
; |
| 825 |
|
| 826 |
|
| 827 |
jb->info.current = jb->info.conf.target_extra; |
| 828 |
jb->info.target = jb->info.conf.target_extra; |
| 829 |
|
| 830 |
return JB_OK; |
| 831 |
} |
| 832 |
|
| 833 |
|