Lines Matching refs:jitter

4    Adaptive jitter buffer for Speex
49 + jitter
67 #define SPEEX_JITTER_MAX_BUFFER_SIZE 200 /**< Maximum number of packets in jitter buffer */
169 /** Based on available data, this computes the optimal delay for the jitter buffer.
175 static spx_int16_t compute_opt_delay(JitterBuffer *jitter)
190 tb = jitter->_tb;
200 if (jitter->latency_tradeoff != 0)
201 late_factor = jitter->latency_tradeoff * 100.0f / tot_count;
203 late_factor = jitter->auto_tradeoff * jitter->window_size/tot_count;
232 latest = ROUND_DOWN(latest, jitter->delay_step);
259 jitter->auto_tradeoff = 1 + deltaT/TOP_DELAY;
260 /*fprintf(stderr, "auto_tradeoff = %d (%d %d %d)\n", jitter->auto_tradeoff, best, worst, i);*/
271 /** Initialise jitter buffer */
274 JitterBuffer *jitter = (JitterBuffer*)speex_alloc(sizeof(JitterBuffer));
275 if (jitter)
280 jitter->packets[i].data=NULL;
281 jitter->delay_step = step_size;
282 jitter->concealment_size = step_size;
284 jitter->buffer_margin = 0;
285 jitter->late_cutoff = 50;
286 jitter->destroy = NULL;
287 jitter->latency_tradeoff = 0;
288 jitter->auto_adjust = 1;
290 jitter_buffer_ctl(jitter, JITTER_BUFFER_SET_MAX_LATE_RATE, &tmp);
291 jitter_buffer_reset(jitter);
293 return jitter;
296 /** Reset jitter buffer */
297 EXPORT void jitter_buffer_reset(JitterBuffer *jitter)
302 if (jitter->packets[i].data)
304 if (jitter->destroy)
305 jitter->destroy(jitter->packets[i].data);
307 speex_free(jitter->packets[i].data);
308 jitter->packets[i].data = NULL;
312 jitter->pointer_timestamp = 0;
313 jitter->next_stop = 0;
314 jitter->reset_state = 1;
315 jitter->lost_count = 0;
316 jitter->buffered = 0;
317 jitter->auto_tradeoff = 32000;
321 tb_init(&jitter->_tb[i]);
322 jitter->timeBuffers[i] = &jitter->_tb[i];
327 /** Destroy jitter buffer */
328 EXPORT void jitter_buffer_destroy(JitterBuffer *jitter)
330 jitter_buffer_reset(jitter);
331 speex_free(jitter);
335 static void update_timings(JitterBuffer *jitter, spx_int32_t timing)
342 if (jitter->timeBuffers[0]->curr_count >= jitter->subwindow_size)
346 struct TimingBuffer *tmp = jitter->timeBuffers[MAX_BUFFERS-1];
348 jitter->timeBuffers[i] = jitter->timeBuffers[i-1];
349 jitter->timeBuffers[0] = tmp;
350 tb_init(jitter->timeBuffers[0]);
352 tb_add(jitter->timeBuffers[0], timing);
356 static void shift_timings(JitterBuffer *jitter, spx_int16_t amount)
361 for (j=0;j<jitter->timeBuffers[i]->filled;j++)
362 jitter->timeBuffers[i]->timing[j] += amount;
367 /** Put one packet into the jitter buffer */
368 EXPORT void jitter_buffer_put(JitterBuffer *jitter, const JitterBufferPacket *packet)
375 if (!jitter->reset_state)
380 if (jitter->packets[i].data && LE32(jitter->packets[i].timestamp + jitter->packets[i].span, jitter->pointer_timestamp))
383 if (jitter->destroy)
384 jitter->destroy(jitter->packets[i].data);
386 speex_free(jitter->packets[i].data);
387 jitter->packets[i].data = NULL;
392 /*fprintf(stderr, "arrival: %d %d %d\n", packet->timestamp, jitter->next_stop, jitter->pointer_timestamp);*/
394 if (!jitter->reset_state && LT32(packet->timestamp, jitter->next_stop))
396 update_timings(jitter, ((spx_int32_t)packet->timestamp) - ((spx_int32_t)jitter->next_stop) - jitter->buffer_margin);
404 if (jitter->lost_count>20)
406 jitter_buffer_reset(jitter);
410 if (jitter->reset_state || GE32(packet->timestamp+packet->span+jitter->delay_step, jitter->pointer_timestamp))
416 if (jitter->packets[i].data==NULL)
423 int earliest=jitter->packets[0].timestamp;
427 if (!jitter->packets[i].data || LT32(jitter->packets[j].timestamp,earliest))
429 earliest = jitter->packets[j].timestamp;
433 if (jitter->destroy)
434 jitter->destroy(jitter->packets[i].data);
436 speex_free(jitter->packets[i].data);
437 jitter->packets[i].data=NULL;
438 /*fprintf (stderr, "Buffer is full, discarding earliest frame %d (currently at %d)\n", timestamp, jitter->pointer_timestamp);*/
442 if (jitter->destroy)
444 jitter->packets[i].data = packet->data;
446 jitter->packets[i].data=(char*)speex_alloc(packet->len);
448 jitter->packets[i].data[j]=packet->data[j];
450 jitter->packets[i].timestamp=packet->timestamp;
451 jitter->packets[i].span=packet->span;
452 jitter->packets[i].len=packet->len;
453 jitter->packets[i].sequence=packet->sequence;
454 jitter->packets[i].user_data=packet->user_data;
455 if (jitter->reset_state || late)
456 jitter->arrival[i] = 0;
458 jitter->arrival[i] = jitter->next_stop;
464 /** Get one packet from the jitter buffer */
465 EXPORT int jitter_buffer_get(JitterBuffer *jitter, JitterBufferPacket *packet, spx_int32_t desired_span, spx_int32_t *start_offset)
476 if (jitter->reset_state)
483 if (jitter->packets[i].data && (!found || LT32(jitter->packets[i].timestamp,oldest)))
485 oldest = jitter->packets[i].timestamp;
491 jitter->reset_state=0;
492 jitter->pointer_timestamp = oldest;
493 jitter->next_stop = oldest;
496 packet->span = jitter->interp_requested;
502 jitter->last_returned_timestamp = jitter->pointer_timestamp;
504 if (jitter->interp_requested != 0)
506 packet->timestamp = jitter->pointer_timestamp;
507 packet->span = jitter->interp_requested;
510 jitter->pointer_timestamp += jitter->interp_requested;
514 jitter->interp_requested = 0;
516 jitter->buffered = packet->span - desired_span;
526 if (jitter->packets[i].data && jitter->packets[i].timestamp==jitter->pointer_timestamp && GE32(jitter->packets[i].timestamp+jitter->packets[i].span,jitter->pointer_timestamp+desired_span))
535 if (jitter->packets[i].data && LE32(jitter->packets[i].timestamp, jitter->pointer_timestamp) && GE32(jitter->packets[i].timestamp+jitter->packets[i].span,jitter->pointer_timestamp+desired_span))
545 if (jitter->packets[i].data && LE32(jitter->packets[i].timestamp, jitter->pointer_timestamp) && GT32(jitter->packets[i].timestamp+jitter->packets[i].span,jitter->pointer_timestamp))
560 if (jitter->packets[i].data && LT32(jitter->packets[i].timestamp,jitter->pointer_timestamp+desired_span) && GE32(jitter->packets[i].timestamp,jitter->pointer_timestamp))
562 if (!found || LT32(jitter->packets[i].timestamp,best_time) || (jitter->packets[i].timestamp==best_time && GT32(jitter->packets[i].span,best_span)))
564 best_time = jitter->packets[i].timestamp;
565 best_span = jitter->packets[i].span;
575 /*fprintf (stderr, "incomplete: %d %d %d %d\n", jitter->packets[i].timestamp, jitter->pointer_timestamp, chunk_size, jitter->packets[i].span);*/
585 jitter->lost_count = 0;
588 if (jitter->arrival[i] != 0)
590 update_timings(jitter, ((spx_int32_t)jitter->packets[i].timestamp) - ((spx_int32_t)jitter->arrival[i]) - jitter->buffer_margin);
595 if (jitter->destroy)
597 packet->data = jitter->packets[i].data;
598 packet->len = jitter->packets[i].len;
600 if (jitter->packets[i].len > packet->len)
602 speex_warning_int("jitter_buffer_get(): packet too large to fit. Size is", jitter->packets[i].len);
604 packet->len = jitter->packets[i].len;
607 packet->data[j] = jitter->packets[i].data[j];
609 speex_free(jitter->packets[i].data);
611 jitter->packets[i].data = NULL;
613 offset = (spx_int32_t)jitter->packets[i].timestamp-(spx_int32_t)jitter->pointer_timestamp;
619 packet->timestamp = jitter->packets[i].timestamp;
620 jitter->last_returned_timestamp = packet->timestamp;
622 packet->span = jitter->packets[i].span;
623 packet->sequence = jitter->packets[i].sequence;
624 packet->user_data = jitter->packets[i].user_data;
626 jitter->pointer_timestamp = jitter->packets[i].timestamp+jitter->packets[i].span;
628 jitter->buffered = packet->span - desired_span;
631 jitter->buffered += *start_offset;
640 jitter->lost_count++;
642 /*fprintf (stderr, "lost_count = %d\n", jitter->lost_count);*/
644 opt = compute_opt_delay(jitter);
652 shift_timings(jitter, -opt);
654 packet->timestamp = jitter->pointer_timestamp;
659 jitter->buffered = packet->span - desired_span;
661 /*jitter->pointer_timestamp -= jitter->delay_step;*/
665 packet->timestamp = jitter->pointer_timestamp;
667 desired_span = ROUND_DOWN(desired_span, jitter->concealment_size);
669 jitter->pointer_timestamp += desired_span;
672 jitter->buffered = packet->span - desired_span;
680 EXPORT int jitter_buffer_get_another(JitterBuffer *jitter, JitterBufferPacket *packet)
685 if (jitter->packets[i].data && jitter->packets[i].timestamp==jitter->last_returned_timestamp)
691 packet->len = jitter->packets[i].len;
692 if (jitter->destroy)
694 packet->data = jitter->packets[i].data;
697 packet->data[j] = jitter->packets[i].data[j];
699 speex_free(jitter->packets[i].data);
701 jitter->packets[i].data = NULL;
702 packet->timestamp = jitter->packets[i].timestamp;
703 packet->span = jitter->packets[i].span;
704 packet->sequence = jitter->packets[i].sequence;
705 packet->user_data = jitter->packets[i].user_data;
715 /* Let the jitter buffer know it's the right time to adjust the buffering delay to the network conditions */
716 static int _jitter_buffer_update_delay(JitterBuffer *jitter, JitterBufferPacket *packet, spx_int32_t *start_offset)
718 spx_int16_t opt = compute_opt_delay(jitter);
723 shift_timings(jitter, -opt);
725 jitter->pointer_timestamp += opt;
726 jitter->interp_requested = -opt;
730 shift_timings(jitter, -opt);
731 jitter->pointer_timestamp += opt;
738 /* Let the jitter buffer know it's the right time to adjust the buffering delay to the network conditions */
739 EXPORT int jitter_buffer_update_delay(JitterBuffer *jitter, JitterBufferPacket *packet, spx_int32_t *start_offset)
743 jitter->auto_adjust = 0;
745 return _jitter_buffer_update_delay(jitter, packet, start_offset);
748 /** Get pointer timestamp of jitter buffer */
749 EXPORT int jitter_buffer_get_pointer_timestamp(JitterBuffer *jitter)
751 return jitter->pointer_timestamp;
754 EXPORT void jitter_buffer_tick(JitterBuffer *jitter)
757 if (jitter->auto_adjust)
758 _jitter_buffer_update_delay(jitter, NULL, NULL);
760 if (jitter->buffered >= 0)
762 jitter->next_stop = jitter->pointer_timestamp - jitter->buffered;
764 jitter->next_stop = jitter->pointer_timestamp;
765 speex_warning_int("jitter buffer sees negative buffering, your code might be broken. Value is ", jitter->buffered);
767 jitter->buffered = 0;
770 EXPORT void jitter_buffer_remaining_span(JitterBuffer *jitter, spx_uint32_t rem)
773 if (jitter->auto_adjust)
774 _jitter_buffer_update_delay(jitter, NULL, NULL);
776 if (jitter->buffered < 0)
777 speex_warning_int("jitter buffer sees negative buffering, your code might be broken. Value is ", jitter->buffered);
778 jitter->next_stop = jitter->pointer_timestamp - rem;
782 /* Used like the ioctl function to control the jitter buffer parameters */
783 EXPORT int jitter_buffer_ctl(JitterBuffer *jitter, int request, void *ptr)
789 jitter->buffer_margin = *(spx_int32_t*)ptr;
792 *(spx_int32_t*)ptr = jitter->buffer_margin;
798 if (jitter->packets[i].data && LE32(jitter->pointer_timestamp, jitter->packets[i].timestamp))
806 jitter->destroy = (void (*) (void *))ptr;
809 *(void (**) (void *))ptr = jitter->destroy;
812 jitter->delay_step = *(spx_int32_t*)ptr;
815 *(spx_int32_t*)ptr = jitter->delay_step;
818 jitter->concealment_size = *(spx_int32_t*)ptr;
821 *(spx_int32_t*)ptr = jitter->concealment_size;
824 jitter->max_late_rate = *(spx_int32_t*)ptr;
825 jitter->window_size = 100*TOP_DELAY/jitter->max_late_rate;
826 jitter->subwindow_size = jitter->window_size/MAX_BUFFERS;
829 *(spx_int32_t*)ptr = jitter->max_late_rate;
832 jitter->latency_tradeoff = *(spx_int32_t*)ptr;
835 *(spx_int32_t*)ptr = jitter->latency_tradeoff;