Ticket #505: ticket505.patch
File ticket505.patch, 8.4 KB (added by nanang, 16 years ago) |
---|
-
pjmedia/src/pjmedia/jbuf.c
29 29 30 30 #define THIS_FILE "jbuf.c" 31 31 32 #define SAFE_SHRINKING_DIFF 2 33 #define USE_PREFETCH_BUFFERING 0 32 34 33 35 typedef struct jb_framelist_t 34 36 { … … 54 56 55 57 int jb_level; // delay between source & destination 56 58 // (calculated according of the number of get/put operations) 57 int jb_last_level; // last delay 58 int jb_last_jitter; // last jitter calculated 59 int jb_max_hist_jitter; // max jitter during the last jitter calculations 59 int jb_max_hist_level; // max level during the last level calculations 60 60 int jb_stable_hist; // num of times the delay has been lower then the prefetch num 61 unsigned jb_op_count; // of of operations.62 61 int jb_last_op; // last operation executed on the framelist->flist_buffer (put/get) 63 62 int jb_last_seq_no; // seq no. of the last frame inserted to the framelist->flist_buffer 64 63 int jb_prefetch; // no. of frame to insert before removing some … … 83 82 /* Enabling this would log the jitter buffer state about once per 84 83 * second. 85 84 */ 86 #if 085 #if 1 87 86 # define TRACE__(args) PJ_LOG(4,args) 88 87 #else 89 88 # define TRACE__(args) … … 304 303 jb->jb_frame_ptime = ptime; 305 304 jb->jb_last_seq_no = -1; 306 305 jb->jb_level = 0; 307 jb->jb_last_level = 0;308 jb->jb_last_jitter = 0;309 306 jb->jb_last_op = JB_OP_INIT; 310 307 jb->jb_prefetch = PJ_MIN(PJMEDIA_JB_DEFAULT_INIT_DELAY,max_count*4/5); 311 308 jb->jb_prefetch_cnt = 0; … … 313 310 jb->jb_max_prefetch = max_count*4/5; 314 311 jb->jb_stable_hist = 0; 315 312 jb->jb_status = JB_STATUS_INITIALIZING; 316 jb->jb_max_hist_ jitter= 0;313 jb->jb_max_hist_level = 0; 317 314 jb->jb_max_count = max_count; 318 315 319 316 *p_jb = jb; … … 366 363 { 367 364 jb->jb_last_seq_no = -1; 368 365 jb->jb_level = 0; 369 jb->jb_last_level = 0;370 jb->jb_last_jitter = 0;371 366 jb->jb_last_op = JB_OP_INIT; 372 367 jb->jb_prefetch_cnt = 0; 373 368 jb->jb_stable_hist = 0; 374 369 jb->jb_status = JB_STATUS_INITIALIZING; 375 jb->jb_max_hist_ jitter= 0;370 jb->jb_max_hist_level = 0; 376 371 377 372 jb_framelist_remove_head(&jb->jb_framelist, 378 373 jb_framelist_size(&jb->jb_framelist)); … … 388 383 389 384 static void jbuf_calculate_jitter(pjmedia_jbuf *jb) 390 385 { 391 unsigned stable_history_limit;386 int diff; 392 387 393 stable_history_limit = 1000 / jb->jb_frame_ptime; 388 /* Only apply burst-level calculation on PUT operation since if VAD is 389 * active the burst-level may not be accurate. 390 */ 391 if (jb->jb_last_op == JB_OP_PUT) { 394 392 395 jb->jb_last_jitter = PJ_ABS(jb->jb_level-jb->jb_last_level); 396 jb->jb_last_level = jb->jb_level; 397 jb->jb_max_hist_jitter = PJ_MAX(jb->jb_max_hist_jitter,jb->jb_last_jitter); 398 jb->jb_op_count++; 393 jb->jb_max_hist_level = PJ_MAX(jb->jb_max_hist_level,jb->jb_level); 399 394 400 if (jb->jb_last_jitter < jb->jb_prefetch) { 401 jb->jb_stable_hist += jb->jb_last_jitter; 402 if (jb->jb_stable_hist > (int)stable_history_limit) { 403 int seq_diff = (jb->jb_prefetch - jb->jb_max_hist_jitter)/3; 404 if (seq_diff<1) seq_diff = 1; 395 /* Level is decreasing */ 396 if (jb->jb_level < jb->jb_prefetch) { 405 397 406 jb->jb_prefetch -= seq_diff; 407 if (jb->jb_prefetch < jb->jb_min_prefetch) 408 jb->jb_prefetch = jb->jb_min_prefetch; 398 static const int stable_history_limit = 100; 399 400 jb->jb_stable_hist++; 401 402 /* Only update the prefetch if 'stable' condition is reached 403 * (not just short time impulse) 404 */ 405 if (jb->jb_stable_hist > (int)stable_history_limit) { 406 diff = (jb->jb_prefetch - jb->jb_max_hist_level) / 3; 409 407 410 jb->jb_stable_hist = 0;411 jb->jb_max_hist_jitter = 0;408 if (diff < 1) 409 diff = 1; 412 410 413 TRACE__((THIS_FILE,"jb updated(1), prefetch=%d, size=%d", 414 jb->jb_prefetch, jb_framelist_size(&jb->jb_framelist))); 411 jb->jb_prefetch -= diff; 412 if (jb->jb_prefetch < jb->jb_min_prefetch) 413 jb->jb_prefetch = jb->jb_min_prefetch; 415 414 416 /* These code is used to shorten the delay in the jitter buffer. 415 TRACE__((jb->name.ptr,"jb updated(1), prefetch=%d, size=%d", 416 jb->jb_prefetch, jb_framelist_size(&jb->jb_framelist))); 417 417 418 if (jb->jb_op_count >= stable_history_limit*2 && 419 (int)jb_framelist_size(&jb->jb_framelist) > jb->jb_prefetch+2) 420 { 421 jb_framelist_remove_head(&jb->jb_framelist,1); 422 423 PJ_LOG(5,(jb->name.ptr, 424 "jbuf optimizing, prefetch: %d, size=%d", 425 jb->jb_prefetch, 426 jb_framelist_size(&jb->jb_framelist))); 427 jb->jb_op_count = 0; 418 jb->jb_stable_hist = 0; 419 jb->jb_max_hist_level = 0; 428 420 } 429 */430 431 421 } 432 } else {433 jb->jb_prefetch = PJ_MIN(jb->jb_last_jitter,434 (int)(jb->jb_max_count*4/5));435 if (jb->jb_prefetch > jb->jb_max_prefetch)436 jb->jb_prefetch = jb->jb_max_prefetch;437 jb->jb_stable_hist = 0;438 jb->jb_max_hist_jitter = 0;439 422 440 TRACE__((THIS_FILE,"jb updated(2), prefetch=%d, size=%d",441 jb->jb_prefetch, jb_framelist_size(&jb->jb_framelist)));423 /* Level is increasing */ 424 else if (jb->jb_level > jb->jb_prefetch) { 442 425 443 /* These code is used to shorten the delay in the jitter buffer 444 when the current size is larger than the prefetch. But it does 445 not really work when the stream supports multiple frames, since 446 the size may grow only temporarily. 426 /* Instaneous set prefetch */ 427 jb->jb_prefetch = PJ_MIN(jb->jb_max_hist_level, 428 (int)(jb->jb_max_count*4/5)); 429 if (jb->jb_prefetch > jb->jb_max_prefetch) 430 jb->jb_prefetch = jb->jb_max_prefetch; 431 jb->jb_stable_hist = 0; 432 jb->jb_max_hist_level = 0; 447 433 448 if (jb->jb_op_count >= stable_history_limit * 2) { 449 if ((int)jb_framelist_size(&jb->jb_framelist) > jb->jb_prefetch+2) 450 { 451 jb_framelist_remove_head(&jb->jb_framelist,1); 434 TRACE__((jb->name.ptr,"jb updated(2), prefetch=%d, size=%d", 435 jb->jb_prefetch, jb_framelist_size(&jb->jb_framelist))); 436 } 452 437 453 PJ_LOG(5,(jb->name.ptr, 454 "jbuf optimizing prefetch: %d, size=%d", 455 jb->jb_prefetch, 456 jb_framelist_size(&jb->jb_framelist))); 457 } 458 459 jb->jb_op_count = 0; 438 /* Level is unchanged */ 439 else { 440 jb->jb_stable_hist = 0; 460 441 } 461 */462 442 } 443 444 /* These code is used for shortening the delay in the jitter buffer. */ 445 diff = jb_framelist_size(&jb->jb_framelist) - jb->jb_prefetch; 446 if (diff > SAFE_SHRINKING_DIFF) { 447 448 /* Drop some frames so jitter buffer size should be == jb_prefetch */ 449 jb_framelist_remove_head(&jb->jb_framelist, diff); 450 451 TRACE__((jb->name.ptr, 452 "JB shrinking %d frames, size=%d", diff, 453 jb_framelist_size(&jb->jb_framelist))); 454 } 455 456 jb->jb_level = 0; 463 457 } 464 458 465 static voidjbuf_update(pjmedia_jbuf *jb, int oper)459 PJ_INLINE(void) jbuf_update(pjmedia_jbuf *jb, int oper) 466 460 { 467 461 if(jb->jb_last_op != oper) { 468 462 jbuf_calculate_jitter(jb); … … 489 483 if(jb->jb_status == JB_STATUS_INITIALIZING) { 490 484 jb->jb_status = JB_STATUS_PROCESSING; 491 485 jb->jb_level = 0; 492 jb->jb_last_level = 0;493 jb->jb_last_jitter = 0;494 486 } else { 495 487 jbuf_update(jb, JB_OP_PUT); 496 488 } … … 537 529 { 538 530 pjmedia_jb_frame_type ftype; 539 531 540 jb->jb_level --;532 jb->jb_level++; 541 533 542 534 jbuf_update(jb, JB_OP_GET); 543 535 536 #if USE_PREFETCH_BUFFERING 537 544 538 if (jb_framelist_size(&jb->jb_framelist) == 0) { 545 539 jb->jb_prefetch_cnt = 0; 546 540 } … … 560 554 return; 561 555 } 562 556 557 #endif 558 563 559 /* Retrieve a frame from frame list */ 564 560 if (jb_framelist_get(&jb->jb_framelist,frame,size,&ftype) == PJ_FALSE) { 565 561 /* Can't return frame because jitter buffer is empty! */ -
pjmedia/src/pjmedia/stream.c
1458 1458 if (info->jb_min_pre >= 0) 1459 1459 jb_min_pre = info->jb_min_pre; 1460 1460 else 1461 jb_min_pre = 60 / stream->codec_param.info.frm_ptime; 1461 //jb_min_pre = 60 / stream->codec_param.info.frm_ptime; 1462 jb_min_pre = 1; 1462 1463 1463 1464 if (info->jb_max_pre > 0) 1464 1465 jb_max_pre = info->jb_max_pre; 1465 1466 else 1466 jb_max_pre = 240 / stream->codec_param.info.frm_ptime; 1467 //jb_max_pre = 240 / stream->codec_param.info.frm_ptime; 1468 jb_max_pre = jb_max * 4 / 5; 1467 1469 1468 1470 if (info->jb_init >= 0) 1469 1471 jb_init = info->jb_init; 1470 1472 else 1471 jb_init = (jb_min_pre + jb_max_pre) / 2; 1473 //jb_init = (jb_min_pre + jb_max_pre) / 2; 1474 jb_init = jb_min_pre; 1472 1475 1473 1476 1474 1477 /* Create jitter buffer */