2c6a9d547ad4593ff8284e0bb858485ccbb879d5
[speexdsp.git] / libspeex / jitter.c
1 /* Copyright (C) 2002 Jean-Marc Valin 
2    File: speex_jitter.h
3
4    Adaptive jitter buffer for Speex
5
6    Redistribution and use in source and binary forms, with or without
7    modification, are permitted provided that the following conditions
8    are met:
9    
10    - Redistributions of source code must retain the above copyright
11    notice, this list of conditions and the following disclaimer.
12    
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.
16    
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.
20    
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.
32
33 */
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38
39
40 #include "misc.h"
41 #include <speex/speex.h>
42 #include <speex/speex_bits.h>
43 #include <speex/speex_jitter.h>
44
45 #ifndef NULL
46 #define NULL 0
47 #endif
48
49 #define LATE_BINS 15
50 #define MAX_MARGIN 30                     /**< Number of bins in margin histogram */
51
52 #define SPEEX_JITTER_MAX_BUFFER_SIZE 200   /**< Maximum number of packets in jitter buffer */
53
54
55
56 #define GT32(a,b) (((spx_int32_t)((a)-(b)))>0)
57 #define GE32(a,b) (((spx_int32_t)((a)-(b)))>=0)
58 #define LT32(a,b) (((spx_int32_t)((a)-(b)))<0)
59 #define LE32(a,b) (((spx_int32_t)((a)-(b)))<=0)
60
61 /** Jitter buffer structure */
62 struct JitterBuffer_ {
63    spx_uint32_t pointer_timestamp;                                        /**< Timestamp of what we will *get* next */
64    spx_uint32_t current_timestamp;                                        /**< Timestamp of the local clock (what we will *play* next) */
65
66    char *buf[SPEEX_JITTER_MAX_BUFFER_SIZE];                               /**< Buffer of packets (NULL if slot is free) */
67    spx_uint32_t timestamp[SPEEX_JITTER_MAX_BUFFER_SIZE];                  /**< Timestamp of packet                 */
68    int span[SPEEX_JITTER_MAX_BUFFER_SIZE];                                /**< Timestamp of packet                 */
69    int len[SPEEX_JITTER_MAX_BUFFER_SIZE];                                 /**< Number of bytes in packet           */
70
71    int tick_size;                                                         /**< Output granularity                  */
72    int reset_state;                                                       /**< True if state was just reset        */
73    int buffer_margin;                                                     /**< How many frames we want to keep in the buffer (lower bound) */
74    int late_cutoff;                                                       /**< How late must a packet be for it not to be considered at all */
75    int interp_requested;                                                  /**< An interpolation is requested by speex_jitter_update_delay() */
76
77    int lost_count;                                                        /**< Number of consecutive lost packets  */
78    float shortterm_margin[MAX_MARGIN];                                    /**< Short term margin histogram         */
79    float longterm_margin[MAX_MARGIN];                                     /**< Long term margin histogram          */
80    float loss_rate;                                                       /**< Average loss rate                   */
81 };
82
83 /** Initialise jitter buffer */
84 JitterBuffer *jitter_buffer_init(int tick)
85 {
86    JitterBuffer *jitter = (JitterBuffer*)speex_alloc(sizeof(JitterBuffer));
87    if (jitter)
88    {
89       int i;
90       for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++)
91          jitter->buf[i]=NULL;
92       jitter->tick_size = tick;
93       jitter->buffer_margin = 1;
94       jitter->late_cutoff = 50;
95       jitter_buffer_reset(jitter);
96    }
97    return jitter;
98 }
99
100 /** Reset jitter buffer */
101 void jitter_buffer_reset(JitterBuffer *jitter)
102 {
103    int i;
104    for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++)
105    {
106       if (jitter->buf[i])
107       {
108          speex_free(jitter->buf[i]);
109          jitter->buf[i] = NULL;
110       }
111    }
112    /* Timestamp is actually undefined at this point */
113    jitter->pointer_timestamp = 0;
114    jitter->current_timestamp = 0;
115    jitter->reset_state = 1;
116    jitter->lost_count = 0;
117    jitter->loss_rate = 0;
118    for (i=0;i<MAX_MARGIN;i++)
119    {
120       jitter->shortterm_margin[i] = 0;
121       jitter->longterm_margin[i] = 0;
122    }
123    /*fprintf (stderr, "reset\n");*/
124 }
125
126 /** Destroy jitter buffer */
127 void jitter_buffer_destroy(JitterBuffer *jitter)
128 {
129    jitter_buffer_reset(jitter);
130    speex_free(jitter);
131 }
132
133 /** Put one packet into the jitter buffer */
134 void jitter_buffer_put(JitterBuffer *jitter, const JitterBufferPacket *packet)
135 {
136    int i,j;
137    spx_int32_t arrival_margin;
138    /*fprintf (stderr, "put packet %d %d\n", timestamp, span);*/
139    if (jitter->reset_state)
140    {
141       jitter->reset_state=0;
142       jitter->pointer_timestamp = packet->timestamp;
143       jitter->current_timestamp = packet->timestamp;
144       /*fprintf(stderr, "reset to %d\n", timestamp);*/
145    }
146    
147    /* Cleanup buffer (remove old packets that weren't played) */
148    for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++)
149    {
150       /* Make sure we don't discard a "just-late" packet in case we want to play it next (if we interpolate). */
151       if (jitter->buf[i] && LE32(jitter->timestamp[i] + jitter->span[i], jitter->pointer_timestamp))
152       {
153          /*fprintf (stderr, "cleaned (not played)\n");*/
154          speex_free(jitter->buf[i]);
155          jitter->buf[i] = NULL;
156       }
157    }
158
159    /*Find an empty slot in the buffer*/
160    for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++)
161    {
162       if (jitter->buf[i]==NULL)
163          break;
164    }
165
166    /*fprintf(stderr, "%d %d %f\n", timestamp, jitter->pointer_timestamp, jitter->drift_average);*/
167    /*No place left in the buffer*/
168    if (i==SPEEX_JITTER_MAX_BUFFER_SIZE)
169    {
170       int earliest=jitter->timestamp[0];
171       i=0;
172       for (j=1;j<SPEEX_JITTER_MAX_BUFFER_SIZE;j++)
173       {
174          if (!jitter->buf[i] || LT32(jitter->timestamp[j],earliest))
175          {
176             earliest = jitter->timestamp[j];
177             i=j;
178          }
179       }
180       speex_free(jitter->buf[i]);
181       jitter->buf[i]=NULL;
182       if (jitter->lost_count>20)
183       {
184          jitter_buffer_reset(jitter);
185       }
186       /*fprintf (stderr, "Buffer is full, discarding earliest frame %d (currently at %d)\n", timestamp, jitter->pointer_timestamp);*/      
187    }
188    
189    /* Copy packet in buffer */
190    jitter->buf[i]=(char*)speex_alloc(packet->len);
191    for (j=0;j<packet->len;j++)
192       jitter->buf[i][j]=packet->data[j];
193    jitter->timestamp[i]=packet->timestamp;
194    jitter->span[i]=packet->span;
195    jitter->len[i]=packet->len;
196    
197    /* Adjust the buffer size depending on network conditions.
198       The arrival margin is how much in advance (or late) the packet it */
199    arrival_margin = (((spx_int32_t)packet->timestamp) - ((spx_int32_t)jitter->current_timestamp))/jitter->tick_size - jitter->buffer_margin;
200    
201    if (arrival_margin >= -jitter->late_cutoff)
202    {
203       /* Here we compute the histogram based on the time of arrival of the packet.
204          This is based on a (first-order) recursive average. We keep both a short-term
205          histogram and a long-term histogram */
206       spx_int32_t int_margin;
207       /* First, apply the "damping" of the recursive average to all bins */
208       for (i=0;i<MAX_MARGIN;i++)
209       {
210          jitter->shortterm_margin[i] *= .98;
211          jitter->longterm_margin[i] *= .995;
212       }
213       /* What histogram bin the packet should be counted in */
214       int_margin = LATE_BINS + arrival_margin;
215       if (int_margin>MAX_MARGIN-1)
216          int_margin = MAX_MARGIN-1;
217       if (int_margin<0)
218          int_margin = 0;
219       /* Add the packet to the right bin */
220       jitter->shortterm_margin[int_margin] += .02;
221       jitter->longterm_margin[int_margin] += .005;
222    } else {
223       /* Packet has arrived *way* too late, we pretty much consider it lost and not take it into account in the histogram */
224       /*fprintf (stderr, "way too late = %d\n", arrival_margin);*/
225       if (jitter->lost_count>20)
226       {
227          jitter_buffer_reset(jitter);
228       }
229    }
230 #if 0 /* Enable to check how much is being buffered */
231    if (rand()%1000==0)
232    {
233       int count = 0;
234       for (j=0;j<SPEEX_JITTER_MAX_BUFFER_SIZE;j++)
235       {
236          if (jitter->buf[j])
237             count++;
238       }
239       fprintf (stderr, "buffer_size = %d\n", count);
240    }
241 #endif
242 }
243
244 /** Get one packet from the jitter buffer */
245 int jitter_buffer_get(JitterBuffer *jitter, JitterBufferPacket *packet, spx_int32_t *start_offset)
246 {
247    int i;
248    unsigned int j;
249    float late_ratio_short;
250    float late_ratio_long;
251    float ontime_ratio_short;
252    float ontime_ratio_long;
253    float early_ratio_short;
254    float early_ratio_long;
255    int chunk_size;
256    int incomplete = 0;
257    
258    if (jitter->interp_requested)
259    {
260       jitter->interp_requested = 0;
261       if (start_offset)
262          *start_offset = 0;
263       packet->timestamp = jitter->pointer_timestamp;
264       packet->span = jitter->tick_size;
265       jitter->pointer_timestamp += jitter->tick_size;
266       packet->len = 0;
267       return JITTER_BUFFER_MISSING;
268    }
269    if (LT32(jitter->current_timestamp+jitter->tick_size, jitter->pointer_timestamp))
270    {
271       jitter->current_timestamp = jitter->pointer_timestamp;
272       speex_warning("did you forget to call jitter_buffer_tick() by any chance?");
273    }
274    /*fprintf (stderr, "get packet %d %d\n", jitter->pointer_timestamp, jitter->current_timestamp);*/
275
276    /* FIXME: This should be only what remaining of the current tick */
277    chunk_size = jitter->tick_size;
278    
279    /* Compiling arrival statistics */
280    
281    late_ratio_short = 0;
282    late_ratio_long = 0;
283    /* Count the proportion of packets that are late */
284    for (i=0;i<LATE_BINS;i++)
285    {
286       late_ratio_short += jitter->shortterm_margin[i];
287       late_ratio_long += jitter->longterm_margin[i];
288    }
289    /* Count the proportion of packets that are just on time */
290    ontime_ratio_short = jitter->shortterm_margin[LATE_BINS];
291    ontime_ratio_long = jitter->longterm_margin[LATE_BINS];
292    early_ratio_short = early_ratio_long = 0;
293    /* Count the proportion of packets that are early */
294    for (i=LATE_BINS+1;i<MAX_MARGIN;i++)
295    {
296       early_ratio_short += jitter->shortterm_margin[i];
297       early_ratio_long += jitter->longterm_margin[i];
298    }
299    if (0&&jitter->pointer_timestamp%1000==0)
300    {
301       /*fprintf (stderr, "%f %f %f %f %f %f\n", early_ratio_short, early_ratio_long, ontime_ratio_short, ontime_ratio_long, late_ratio_short, late_ratio_long);*/
302       /*fprintf (stderr, "%f %f\n", early_ratio_short + ontime_ratio_short + late_ratio_short, early_ratio_long + ontime_ratio_long + late_ratio_long);*/
303    }
304    
305    
306    /* Searching for the packet that fits best */
307    
308    /* Search the buffer for a packet with the right timestamp and spanning the whole current chunk */
309    for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++)
310    {
311       if (jitter->buf[i] && jitter->timestamp[i]==jitter->pointer_timestamp && GE32(jitter->timestamp[i]+jitter->span[i],jitter->pointer_timestamp+chunk_size))
312          break;
313    }
314    
315    /* If no match, try for an "older" packet that still spans (fully) the current chunk */
316    if (i==SPEEX_JITTER_MAX_BUFFER_SIZE)
317    {
318       for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++)
319       {
320          if (jitter->buf[i] && LE32(jitter->timestamp[i], jitter->pointer_timestamp) && GE32(jitter->timestamp[i]+jitter->span[i],jitter->pointer_timestamp+chunk_size))
321             break;
322       }
323    }
324    
325    /* If still no match, try for an "older" packet that spans part of the current chunk */
326    if (i==SPEEX_JITTER_MAX_BUFFER_SIZE)
327    {
328       for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++)
329       {
330          if (jitter->buf[i] && LE32(jitter->timestamp[i], jitter->pointer_timestamp) && GT32(jitter->timestamp[i]+jitter->span[i],jitter->pointer_timestamp))
331             break;
332       }
333    }
334    
335    /* If still no match, try for earliest packet possible */
336    if (i==SPEEX_JITTER_MAX_BUFFER_SIZE)
337    {
338       int found = 0;
339       spx_uint32_t best_time=0;
340       int best_span=0;
341       int besti=0;
342       for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++)
343       {
344          /* check if packet starts within current chunk */
345          if (jitter->buf[i] && LT32(jitter->timestamp[i],jitter->pointer_timestamp+chunk_size) && GE32(jitter->timestamp[i],jitter->pointer_timestamp))
346          {
347             if (!found || LT32(jitter->timestamp[i],best_time) || (jitter->timestamp[i]==best_time && GT32(jitter->span[i],best_span)))
348             {
349                best_time = jitter->timestamp[i];
350                best_span = jitter->span[i];
351                besti = i;
352                found = 1;
353             }
354          }
355       }
356       if (found)
357       {
358          i=besti;
359          incomplete = 1;
360          /*fprintf (stderr, "incomplete: %d %d %d %d\n", jitter->timestamp[i], jitter->pointer_timestamp, chunk_size, jitter->span[i]);*/
361       }
362    }
363
364    /* If we find something */
365    if (i!=SPEEX_JITTER_MAX_BUFFER_SIZE)
366    {
367       /* We (obviously) haven't lost this packet */
368       jitter->lost_count = 0;
369       jitter->loss_rate = .999*jitter->loss_rate;
370       /* Check for potential overflow */
371       packet->len = jitter->len[i];
372       /* Copy packet */
373       for (j=0;j<packet->len;j++)
374          packet->data[j] = jitter->buf[i][j];
375       /* Remove packet */
376       speex_free(jitter->buf[i]);
377       jitter->buf[i] = NULL;
378       /* Set timestamp and span (if requested) */
379       if (start_offset)
380          *start_offset = (spx_int32_t)jitter->timestamp[i]-(spx_int32_t)jitter->pointer_timestamp;
381       packet->timestamp = jitter->timestamp[i];
382       packet->span = jitter->span[i];
383       /* Point at the end of the current packet */
384       jitter->pointer_timestamp = jitter->timestamp[i]+jitter->span[i];
385       if (incomplete)
386          return JITTER_BUFFER_INCOMPLETE;
387       else
388          return JITTER_BUFFER_OK;
389    }
390    
391    
392    /* If we haven't found anything worth returning */
393    /*fprintf (stderr, "not found\n");*/
394    jitter->lost_count++;
395    /*fprintf (stderr, "m");*/
396    /*fprintf (stderr, "lost_count = %d\n", jitter->lost_count);*/
397    jitter->loss_rate = .999*jitter->loss_rate + .001;
398    if (start_offset)
399       *start_offset = 0;
400    packet->timestamp = jitter->pointer_timestamp;
401    packet->span = jitter->tick_size;
402    jitter->pointer_timestamp += chunk_size;
403    packet->len = 0;
404    
405    /* Adjusting the buffering bssed on the amount of packets that are early/on time/late */   
406    if (late_ratio_short > .1 || late_ratio_long > .03)
407    {
408       /* If too many packets are arriving late */
409       jitter->shortterm_margin[MAX_MARGIN-1] += jitter->shortterm_margin[MAX_MARGIN-2];
410       jitter->longterm_margin[MAX_MARGIN-1] += jitter->longterm_margin[MAX_MARGIN-2];
411       for (i=MAX_MARGIN-3;i>=0;i--)
412       {
413          jitter->shortterm_margin[i+1] = jitter->shortterm_margin[i];
414          jitter->longterm_margin[i+1] = jitter->longterm_margin[i];         
415       }
416       jitter->shortterm_margin[0] = 0;
417       jitter->longterm_margin[0] = 0;            
418       jitter->pointer_timestamp -= jitter->tick_size;
419       jitter->current_timestamp -= jitter->tick_size;
420       /*fprintf (stderr, "i");*/
421       /*fprintf (stderr, "interpolate (getting some slack)\n");*/
422    }
423
424    return JITTER_BUFFER_MISSING;
425
426 }
427
428 /** Get pointer timestamp of jitter buffer */
429 int jitter_buffer_get_pointer_timestamp(JitterBuffer *jitter)
430 {
431    return jitter->pointer_timestamp;
432 }
433
434 void jitter_buffer_tick(JitterBuffer *jitter)
435 {
436    jitter->current_timestamp += jitter->tick_size;
437 }
438
439 /* Let the jitter buffer know it's the right time to adjust the buffering delay to the network conditions */
440 int jitter_buffer_update_delay(JitterBuffer *jitter, JitterBufferPacket *packet, spx_int32_t *start_offset)
441 {
442    int i;
443    float late_ratio_short;
444    float late_ratio_long;
445    float ontime_ratio_short;
446    float ontime_ratio_long;
447    float early_ratio_short;
448    float early_ratio_long;
449    
450    if (LT32(jitter->current_timestamp+jitter->tick_size, jitter->pointer_timestamp))
451    {
452       jitter->current_timestamp = jitter->pointer_timestamp;
453       speex_warning("did you forget to call jitter_buffer_tick() by any chance?");
454    }
455    /*fprintf (stderr, "get packet %d %d\n", jitter->pointer_timestamp, jitter->current_timestamp);*/
456
457    /* FIXME: This should be only what remaining of the current tick */
458    late_ratio_short = 0;
459    late_ratio_long = 0;
460    /* Count the proportion of packets that are late */
461    for (i=0;i<LATE_BINS;i++)
462    {
463       late_ratio_short += jitter->shortterm_margin[i];
464       late_ratio_long += jitter->longterm_margin[i];
465    }
466    /* Count the proportion of packets that are just on time */
467    ontime_ratio_short = jitter->shortterm_margin[LATE_BINS];
468    ontime_ratio_long = jitter->longterm_margin[LATE_BINS];
469    early_ratio_short = early_ratio_long = 0;
470    /* Count the proportion of packets that are early */
471    for (i=LATE_BINS+1;i<MAX_MARGIN;i++)
472    {
473       early_ratio_short += jitter->shortterm_margin[i];
474       early_ratio_long += jitter->longterm_margin[i];
475    }
476    
477    /* Adjusting the buffering bssed on the amount of packets that are early/on time/late */   
478    if (late_ratio_short > .1 || late_ratio_long > .03)
479    {
480       /* If too many packets are arriving late */
481       jitter->shortterm_margin[MAX_MARGIN-1] += jitter->shortterm_margin[MAX_MARGIN-2];
482       jitter->longterm_margin[MAX_MARGIN-1] += jitter->longterm_margin[MAX_MARGIN-2];
483       for (i=MAX_MARGIN-3;i>=0;i--)
484       {
485          jitter->shortterm_margin[i+1] = jitter->shortterm_margin[i];
486          jitter->longterm_margin[i+1] = jitter->longterm_margin[i];         
487       }
488       jitter->shortterm_margin[0] = 0;
489       jitter->longterm_margin[0] = 0;            
490       jitter->pointer_timestamp -= jitter->tick_size;
491       jitter->current_timestamp -= jitter->tick_size;
492       jitter->interp_requested = 1;
493       return JITTER_BUFFER_ADJUST_INTERPOLATE;
494    
495    } else if (late_ratio_short + ontime_ratio_short < .005 && late_ratio_long + ontime_ratio_long < .01 && early_ratio_short > .8)
496    {
497       /* Many frames arriving early */
498       jitter->shortterm_margin[0] += jitter->shortterm_margin[1];
499       jitter->longterm_margin[0] += jitter->longterm_margin[1];
500       for (i=1;i<MAX_MARGIN-1;i++)
501       {
502          jitter->shortterm_margin[i] = jitter->shortterm_margin[i+1];
503          jitter->longterm_margin[i] = jitter->longterm_margin[i+1];         
504       }
505       jitter->shortterm_margin[MAX_MARGIN-1] = 0;
506       jitter->longterm_margin[MAX_MARGIN-1] = 0;      
507       /*fprintf (stderr, "drop frame\n");*/
508       /*fprintf (stderr, "d");*/
509       jitter->pointer_timestamp += jitter->tick_size;
510       jitter->current_timestamp += jitter->tick_size;
511       return JITTER_BUFFER_ADJUST_DROP;
512    }
513    
514    return JITTER_BUFFER_ADJUST_OK;
515 }
516
517 /* Used like the ioctl function to control the jitter buffer parameters */
518 int jitter_buffer_ctl(JitterBuffer *jitter, int request, void *ptr)
519 {
520    int count, i;
521    switch(request)
522    {
523       case JITTER_BUFFER_SET_MARGIN:
524          jitter->buffer_margin = *(spx_int32_t*)ptr;
525          break;
526       case JITTER_BUFFER_GET_MARGIN:
527          *(spx_int32_t*)ptr = jitter->buffer_margin;
528          break;
529       case JITTER_BUFFER_GET_AVALIABLE_COUNT:
530          count = 0;
531          for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++)
532          {
533             if (jitter->buf[i] && LE32(jitter->pointer_timestamp, jitter->timestamp[i]))
534             {
535                count++;
536             }
537          }
538          *(spx_int32_t*)ptr = count;
539          break;
540       default:
541          speex_warning_int("Unknown jitter_buffer_ctl request: ", request);
542          return -1;
543    }
544    return 0;
545 }
546
547
548
549 void speex_jitter_init(SpeexJitter *jitter, void *decoder, int sampling_rate)
550 {
551    jitter->dec = decoder;
552    speex_decoder_ctl(decoder, SPEEX_GET_FRAME_SIZE, &jitter->frame_size);
553
554    jitter->packets = jitter_buffer_init(jitter->frame_size);
555
556    speex_bits_init(&jitter->current_packet);
557    jitter->valid_bits = 0;
558
559 }
560
561 void speex_jitter_destroy(SpeexJitter *jitter)
562 {
563    jitter_buffer_destroy(jitter->packets);
564    speex_bits_destroy(&jitter->current_packet);
565 }
566
567 void speex_jitter_put(SpeexJitter *jitter, char *packet, int len, int timestamp)
568 {
569    JitterBufferPacket p;
570    p.data = packet;
571    p.len = len;
572    p.timestamp = timestamp;
573    p.span = jitter->frame_size;
574    jitter_buffer_put(jitter->packets, &p);
575 }
576
577 void speex_jitter_get(SpeexJitter *jitter, short *out, int *current_timestamp)
578 {
579    int i;
580    int ret;
581    spx_int32_t activity;
582    char data[2048];
583    JitterBufferPacket packet;
584    packet.data = data;
585    
586    if (jitter->valid_bits)
587    {
588       /* Try decoding last received packet */
589       ret = speex_decode_int(jitter->dec, &jitter->current_packet, out);
590       if (ret == 0)
591       {
592          jitter_buffer_tick(jitter->packets);
593          return;
594       } else {
595          jitter->valid_bits = 0;
596       }
597    }
598
599    ret = jitter_buffer_get(jitter->packets, &packet, NULL);
600    
601    if (ret != JITTER_BUFFER_OK)
602    {
603       /* No packet found */
604
605       /*fprintf (stderr, "lost/late frame\n");*/
606       /*Packet is late or lost*/
607       speex_decode_int(jitter->dec, NULL, out);
608    } else {
609       speex_bits_read_from(&jitter->current_packet, packet.data, packet.len);
610       /* Decode packet */
611       ret = speex_decode_int(jitter->dec, &jitter->current_packet, out);
612       if (ret == 0)
613       {
614          jitter->valid_bits = 1;
615       } else {
616          /* Error while decoding */
617          for (i=0;i<jitter->frame_size;i++)
618             out[i]=0;
619       }
620    }
621    speex_decoder_ctl(jitter->dec, SPEEX_GET_ACTIVITY, &activity);
622    if (activity < 30)
623       jitter_buffer_update_delay(jitter->packets, &packet, NULL);
624    jitter_buffer_tick(jitter->packets);
625 }
626
627 int speex_jitter_get_pointer_timestamp(SpeexJitter *jitter)
628 {
629    return jitter_buffer_get_pointer_timestamp(jitter->packets);
630 }