1
/* Copyright (C) 2002 Jean-Marc Valin
4
Adaptive jitter buffer for Speex
6
Redistribution and use in source and binary forms, with or without
7
modification, are permitted provided that the following conditions
10
- Redistributions of source code must retain the above copyright
11
notice, this list of conditions and the following disclaimer.
13
- Redistributions in binary form must reproduce the above copyright
14
notice, this list of conditions and the following disclaimer in the
15
documentation and/or other materials provided with the distribution.
17
- Neither the name of the Xiph.org Foundation nor the names of its
18
contributors may be used to endorse or promote products derived from
19
this software without specific prior written permission.
21
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
25
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40
#include "speex_bits.h"
41
#include "speex_jitter.h"
43
void speex_jitter_init(SpeexJitter *jitter, void *decoder, int sampling_rate)
46
for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++)
49
jitter->timestamp[i]=-1;
52
jitter->dec = decoder;
53
speex_decoder_ctl(decoder, SPEEX_GET_FRAME_SIZE, &jitter->frame_size);
54
jitter->frame_time = 1000*jitter->frame_size / sampling_rate;
56
speex_bits_init(&jitter->current_packet);
57
jitter->valid_bits = 0;
59
jitter->buffer_size = 10;
61
jitter->pointer_timestamp = -jitter->frame_time * jitter->buffer_size;
63
jitter->underflow_count = 0;
64
jitter->drop_frame = 0;
65
jitter->interp_frame = 0;
68
void speex_jitter_destroy(SpeexJitter *jitter)
73
void speex_jitter_put(SpeexJitter *jitter, char *packet, int len, int timestamp)
77
/* Cleanup buffer (remove old packets that weren't played) */
78
for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++)
80
if (jitter->timestamp[i]<jitter->pointer_timestamp)
84
/*Find an empty slot in the buffer*/
85
for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++)
87
if (jitter->len[i]==-1)
91
if (i==SPEEX_JITTER_MAX_BUFFER_SIZE)
93
/*No place left in the buffer*/
95
/*skip some frame(s) */
99
/* Copy packet in buffer */
100
if (len>SPEEX_JITTER_MAX_PACKET_SIZE)
101
len=SPEEX_JITTER_MAX_PACKET_SIZE;
103
jitter->buf[i][j]=packet[j];
104
jitter->timestamp[i]=timestamp;
107
/* Detect when it's time to drop frames (too much stuff in buffer) */
108
if (timestamp-jitter->pointer_timestamp > (jitter->buffer_size+3)*jitter->frame_time)
109
jitter->drop_frame = 1;
111
/*Detect when it's time to interpolate a frame (not wnough stuff in buffer) */
112
if (timestamp-jitter->pointer_timestamp < (jitter->buffer_size-3)*jitter->frame_time)
113
jitter->underflow_count++;
115
jitter->underflow_count=0;
116
if (jitter->underflow_count > 10)
118
jitter->underflow_count=0;
119
jitter->interp_frame = 1;
122
/* Adjust the buffer size depending on network conditions */
125
void speex_jitter_get(SpeexJitter *jitter, short *out)
130
/* Handle frame interpolation (receiving too fast) */
131
if (jitter->interp_frame)
133
speex_decode(jitter->dec, NULL, out);
134
jitter->interp_frame = 0;
138
/* Increment timestamp */
139
jitter->pointer_timestamp += jitter->frame_time;
141
/* Handle frame dropping (receiving too fast) */
142
if (jitter->drop_frame)
144
jitter->pointer_timestamp += jitter->frame_time;
145
jitter->drop_frame = 0;
148
/* Send zeros while we fill in the buffer */
149
if (jitter->pointer_timestamp<0)
151
for (i=0;i<jitter->frame_size;i++)
156
/* Search the buffer for a packet with the right timestamp */
157
for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++)
159
if (jitter->len[i]!=-1 && jitter->timestamp[i]==jitter->pointer_timestamp)
163
if (i==SPEEX_JITTER_MAX_BUFFER_SIZE)
165
/* No packet found */
166
if (jitter->valid_bits)
168
/* Try decoding last received packet */
169
ret = speex_decode(jitter->dec, &jitter->current_packet, out);
173
jitter->valid_bits = 0;
176
/*Packet is late or lost*/
177
speex_decode(jitter->dec, NULL, out);
179
/* Found the right packet */
180
speex_bits_read_from(&jitter->current_packet, jitter->buf[i], jitter->len[i]);
183
ret = speex_decode(jitter->dec, &jitter->current_packet, out);
186
jitter->valid_bits = 1;
188
/* Error while decoding */
189
for (i=0;i<jitter->frame_size;i++)