Commit 885d6b01 authored by jm's avatar jm

jitter buffer: Cleaned up the remaining of the Speex-specific buffer and fixed

a bug in the timestamp adjustment when interpolating.


git-svn-id: http://svn.xiph.org/trunk/speex@14091 0101bb08-14d6-0310-b084-bc0e0c8e3800
parent 8b7a8534
......@@ -41,8 +41,7 @@
* @{
*/
#include "speex.h"
#include "speex_bits.h"
#include "speex/speex_types.h"
#ifdef __cplusplus
extern "C" {
......@@ -178,45 +177,8 @@ int jitter_buffer_update_delay(JitterBuffer *jitter, JitterBufferPacket *packet,
/* @} */
/** @defgroup SpeexJitter SpeexJitter: Adaptive jitter buffer specifically for Speex
* This is the jitter buffer that reorders UDP/RTP packets and adjusts the buffer size
* to maintain good quality and low latency. This is a simplified version that works only
* with Speex, but is much easier to use.
* @{
*/
/** Speex jitter-buffer state. Never use it directly! */
typedef struct SpeexJitter {
SpeexBits current_packet; /**< Current Speex packet */
int valid_bits; /**< True if Speex bits are valid */
JitterBuffer *packets; /**< Generic jitter buffer state */
void *dec; /**< Pointer to Speex decoder */
spx_int32_t frame_size; /**< Frame size of Speex decoder */
} SpeexJitter;
/** Initialise jitter buffer
*
* @param jitter State of the Speex jitter buffer
* @param decoder Speex decoder to call
* @param sampling_rate Sampling rate used by the decoder
*/
void speex_jitter_init(SpeexJitter *jitter, void *decoder, int sampling_rate);
/** Destroy jitter buffer */
void speex_jitter_destroy(SpeexJitter *jitter);
/** Put one packet into the jitter buffer */
void speex_jitter_put(SpeexJitter *jitter, char *packet, int len, int timestamp);
/** Get one packet from the jitter buffer */
void speex_jitter_get(SpeexJitter *jitter, spx_int16_t *out, int *start_offset);
/** Get pointer timestamp of jitter buffer */
int speex_jitter_get_pointer_timestamp(SpeexJitter *jitter);
#ifdef __cplusplus
}
#endif
/* @} */
#endif
......@@ -87,6 +87,7 @@ struct JitterBuffer_ {
int resolution; /**< Time resolution for histogram (timestamp units) */
int delay_step; /**< Size of the steps when adjusting buffering (timestamp units) */
int res_delay_step; /**< Size of the steps when adjusting buffering (resolution units) */
int reset_state; /**< True if state was just reset */
int buffer_margin; /**< How many frames we want to keep in the buffer (lower bound) */
int late_cutoff; /**< How late must a packet be for it not to be considered at all */
......@@ -116,6 +117,7 @@ JitterBuffer *jitter_buffer_init(int resolution)
jitter->packets[i].data=NULL;
jitter->resolution = resolution;
jitter->delay_step = resolution;
jitter->res_delay_step = 1;
//FIXME: Should this be 0 or 1?
jitter->buffer_margin = 1;
jitter->late_cutoff = 50;
......@@ -243,13 +245,20 @@ static void compute_statistics(JitterBuffer *jitter)
jitter->late_ratio_short += jitter->shortterm_margin[i];
jitter->late_ratio_long += jitter->longterm_margin[i];
}
/* Count the proportion of packets that are just on time */
jitter->ontime_ratio_short = jitter->shortterm_margin[LATE_BINS];
jitter->ontime_ratio_long = jitter->longterm_margin[LATE_BINS];
jitter->ontime_ratio_short = 0;
jitter->ontime_ratio_long = 0;
for (;i<LATE_BINS+jitter->res_delay_step;i++)
{
jitter->ontime_ratio_short = jitter->shortterm_margin[i];
jitter->ontime_ratio_long = jitter->longterm_margin[i];
}
jitter->early_ratio_short = 0;
jitter->early_ratio_long = 0;
/* Count the proportion of packets that are early */
for (i=LATE_BINS+1;i<MAX_MARGIN;i++)
for (;i<MAX_MARGIN;i++)
{
jitter->early_ratio_short += jitter->shortterm_margin[i];
jitter->early_ratio_long += jitter->longterm_margin[i];
......@@ -522,14 +531,14 @@ int jitter_buffer_get(JitterBuffer *jitter, JitterBufferPacket *packet, spx_int3
/* Increase buffering */
/* Shift histogram to compensate */
shift_histogram(jitter, 1);
shift_histogram(jitter, jitter->res_delay_step);
packet->timestamp = jitter->pointer_timestamp;
packet->span = jitter->delay_step;
/* Don't move the pointer_timestamp forward */
packet->len = 0;
jitter->pointer_timestamp -= jitter->delay_step;
/*jitter->pointer_timestamp -= jitter->delay_step;*/
fprintf (stderr, "Forced to interpolate\n");
} else {
/* Normal packet loss */
......@@ -605,7 +614,7 @@ int jitter_buffer_update_delay(JitterBuffer *jitter, JitterBufferPacket *packet,
if (jitter->late_ratio_short > .1 || jitter->late_ratio_long > .03)
{
/* If too many packets are arriving late */
shift_histogram(jitter, 1);
shift_histogram(jitter, jitter->res_delay_step);
jitter->pointer_timestamp -= jitter->delay_step;
jitter->interp_requested = 1;
......@@ -615,7 +624,7 @@ int jitter_buffer_update_delay(JitterBuffer *jitter, JitterBufferPacket *packet,
} else if (jitter->late_ratio_short + jitter->ontime_ratio_short < .005 && jitter->late_ratio_long + jitter->ontime_ratio_long < .01 && jitter->early_ratio_short > .8)
{
/* Many frames arriving early */
shift_histogram(jitter, -1);
shift_histogram(jitter, -jitter->res_delay_step);
jitter->pointer_timestamp += jitter->delay_step;
fprintf (stderr, "Decision to drop\n");
......@@ -656,6 +665,7 @@ int jitter_buffer_ctl(JitterBuffer *jitter, int request, void *ptr)
break;
case JITTER_BUFFER_SET_DELAY_STEP:
jitter->delay_step = *(spx_int32_t*)ptr;
jitter->res_delay_step = jitter->delay_step/jitter->resolution;
break;
case JITTER_BUFFER_GET_DELAY_STEP:
*(spx_int32_t*)ptr = jitter->delay_step;
......
#include <speex/speex_jitter.h>
#include "speex_jitter_buffer.h"
#ifndef NULL
#define NULL 0
......
......@@ -51,6 +51,7 @@
#include <speex/speex_jitter.h>
#include <speex/speex_preprocess.h>
#include <speex/speex_echo.h>
#include "speex_jitter_buffer.h"
#include <sched.h>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment