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