fixed-point: Got rid of the three last float bits in the
[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 /*
36 TODO:
37 - Add short-term estimate
38 - Defensive programming
39   + warn when last returned < last desired (begative buffering)
40   + warn if update_delay not called between get() and tick() or is called twice in a row
41 - Linked list structure for holding the packets instead of the current fixed-size array
42   + return memory to a pool
43   + allow pre-allocation of the pool
44   + optional max number of elements
45 - Statistics
46   + drift
47   + loss
48   + late
49   + jitter
50   + buffering delay
51 */
52 #ifdef HAVE_CONFIG_H
53 #include "config.h"
54 #endif
55
56
57 #include "arch.h"
58 #include <speex/speex.h>
59 #include <speex/speex_bits.h>
60 #include <speex/speex_jitter.h>
61 #include "os_support.h"
62
63 #ifndef NULL
64 #define NULL 0
65 #endif
66
67 #define SPEEX_JITTER_MAX_BUFFER_SIZE 200   /**< Maximum number of packets in jitter buffer */
68
69 #define TSUB(a,b) ((spx_int32_t)((a)-(b)))
70
71 #define GT32(a,b) (((spx_int32_t)((a)-(b)))>0)
72 #define GE32(a,b) (((spx_int32_t)((a)-(b)))>=0)
73 #define LT32(a,b) (((spx_int32_t)((a)-(b)))<0)
74 #define LE32(a,b) (((spx_int32_t)((a)-(b)))<=0)
75
76 #define ROUND_DOWN(x, step) ((x)<0 ? ((x)-(step)+1)/(step)*(step) : (x)/(step)*(step)) 
77
78 #define MAX_TIMINGS 20
79 #define MAX_BUFFERS 3
80 #define TOP_DELAY 20
81
82 /** Buffer that keeps the time of arrival of the latest packets */
83 struct TimingBuffer {
84    int filled;                         /**< Number of entries occupied in "timing" and "counts"*/
85    int curr_count;                     /**< Number of packet timings we got (including those we discarded) */
86    spx_int16_t timing[MAX_TIMINGS];    /**< Sorted list of all timings ("latest" packets first) */
87    spx_int16_t counts[MAX_TIMINGS];    /**< Order the packets were put in (will be used for short-term estimate) */
88 };
89
90 static void tb_init(struct TimingBuffer *tb)
91 {
92    tb->filled = 0;
93    tb->curr_count = 0;
94 }
95
96 /* Add the timing of a new packet to the TimingBuffer */
97 static void tb_add(struct TimingBuffer *tb, spx_int16_t timing)
98 {
99    int pos;
100    /* Discard packet that won't make it into the list because they're too early */
101    if (tb->filled >= MAX_TIMINGS && timing >= tb->timing[tb->filled-1])
102    {
103       tb->curr_count++;
104       return;
105    }
106    
107    /* Find where the timing info goes in the sorted list */
108    pos = 0;
109    /* FIXME: Do bisection instead of linear search */
110    while (pos<tb->filled && timing >= tb->timing[pos])
111    {
112       pos++;
113    }
114    
115    speex_assert(pos <= tb->filled && pos < MAX_TIMINGS);
116    
117    /* Shift everything so we can perform the insertion */
118    if (pos < tb->filled)
119    {
120       int move_size = tb->filled-pos;
121       if (tb->filled == MAX_TIMINGS)
122          move_size -= 1;
123       speex_move(&tb->timing[pos+1], &tb->timing[pos], move_size*sizeof(tb->timing[0]));
124       speex_move(&tb->counts[pos+1], &tb->counts[pos], move_size*sizeof(tb->counts[0]));
125    }
126    /* Insert */
127    tb->timing[pos] = timing;
128    tb->counts[pos] = tb->curr_count;
129    
130    tb->curr_count++;
131    if (tb->filled<MAX_TIMINGS)
132       tb->filled++;
133 }
134
135
136
137 /** Jitter buffer structure */
138 struct JitterBuffer_ {
139    spx_uint32_t pointer_timestamp;                             /**< Timestamp of what we will *get* next */
140    spx_uint32_t last_returned_timestamp;                       /**< Useful for getting the next packet with the same timestamp (for fragmented media) */
141    spx_uint32_t next_stop;                                     /**< Estimated time the next get() will be called */
142    
143    spx_int32_t buffered;                                       /**< Amount of data we think is still buffered by the application (timestamp units)*/
144    
145    JitterBufferPacket packets[SPEEX_JITTER_MAX_BUFFER_SIZE];   /**< Packets stored in the buffer */
146    spx_uint32_t arrival[SPEEX_JITTER_MAX_BUFFER_SIZE];         /**< Packet arrival time (0 means it was late, even though it's a valid timestamp) */
147    
148    void (*destroy) (void *);                                   /**< Callback for destroying a packet */
149
150    spx_int32_t delay_step;                                     /**< Size of the steps when adjusting buffering (timestamp units) */
151    spx_int32_t concealment_size;                               /**< Size of the packet loss concealment "units" */
152    int reset_state;                                            /**< True if state was just reset        */
153    int buffer_margin;                                          /**< How many frames we want to keep in the buffer (lower bound) */
154    int late_cutoff;                                            /**< How late must a packet be for it not to be considered at all */
155    int interp_requested;                                       /**< An interpolation is requested by speex_jitter_update_delay() */
156
157    struct TimingBuffer _tb[MAX_BUFFERS];                       /**< Don't use those directly */
158    struct TimingBuffer *timeBuffers[MAX_BUFFERS];              /**< Storing arrival time of latest frames so we can compute some stats */
159    int window_size;                                            /**< Total window over which the late frames are counted */
160    int subwindow_size;                                         /**< Sub-window size for faster computation  */
161    int max_late_rate;                                          /**< Absolute maximum amount of late packets tolerable (in percent) */
162    int latency_tradeoff;                                       /**< Latency equivalent of losing one percent of packets */
163    int auto_tradeoff;                                          /**< Latency equivalent of losing one percent of packets (automatic default) */
164    
165    int lost_count;                                             /**< Number of consecutive lost packets  */
166 };
167
168 /** Based on available data, this computes the optimal delay for the jitter buffer. 
169    The optimised function is in timestamp units and is:
170    cost = delay + late_factor*[number of frames that would be late if we used that delay]
171    @param tb Array of buffers
172    @param late_factor Equivalent cost of a late frame (in timestamp units) 
173  */
174 static spx_int16_t compute_opt_delay(JitterBuffer *jitter)
175 {
176    int i;
177    spx_int16_t opt=0;
178    spx_int32_t best_cost=0x7fffffff;
179    int late = 0;
180    int pos[MAX_BUFFERS];
181    int tot_count;
182    float late_factor;
183    int penalty_taken = 0;
184    int best = 0;
185    int worst = 0;
186    spx_int32_t deltaT;
187    struct TimingBuffer *tb;
188    
189    tb = jitter->_tb;
190    
191    /* Number of packet timings we have received (including those we didn't keep) */
192    tot_count = 0;
193    for (i=0;i<MAX_BUFFERS;i++)
194       tot_count += tb[i].curr_count;
195    if (tot_count==0)
196       return 0;
197    
198    /* Compute cost for one lost packet */
199    if (jitter->latency_tradeoff != 0)
200       late_factor = jitter->latency_tradeoff * 100.0f / tot_count;
201    else
202       late_factor = jitter->auto_tradeoff * jitter->window_size/tot_count;
203    
204    /*fprintf(stderr, "late_factor = %f\n", late_factor);*/
205    for (i=0;i<MAX_BUFFERS;i++)
206       pos[i] = 0;
207    
208    /* Pick the TOP_DELAY "latest" packets (doesn't need to actually be late 
209       for the current settings) */
210    for (i=0;i<TOP_DELAY;i++)
211    {
212       int j;
213       int next=-1;
214       int latest = 32767;
215       /* Pick latest amoung all sub-windows */
216       for (j=0;j<MAX_BUFFERS;j++)
217       {
218          if (pos[j] < tb[j].filled && tb[j].timing[pos[j]] < latest)
219          {
220             next = j;
221             latest = tb[j].timing[pos[j]];
222          }
223       }
224       if (next != -1)
225       {
226          spx_int32_t cost;
227          
228          if (i==0)
229             worst = latest;
230          best = latest;
231          latest = ROUND_DOWN(latest, jitter->delay_step);
232          pos[next]++;
233          
234          /* Actual cost function that tells us how bad using this delay would be */
235          cost = -latest + late_factor*late;
236          /*fprintf(stderr, "cost %d = %d + %f * %d\n", cost, -latest, late_factor, late);*/
237          if (cost < best_cost)
238          {
239             best_cost = cost;
240             opt = latest;
241          }
242       } else {
243          break;
244       }
245       
246       /* For the next timing we will consider, there will be one more late packet to count */
247       late++;
248       /* Two-frame penalty if we're going to increase the amount of late frames (hysteresis) */
249       if (latest >= 0 && !penalty_taken)
250       {
251          penalty_taken = 1;
252          late+=2;
253       }
254    }
255    
256    deltaT = best-worst;
257    /* This is a default "automatic latency tradeoff" when none is provided */
258    jitter->auto_tradeoff = 1 + deltaT/TOP_DELAY;
259    /*fprintf(stderr, "auto_tradeoff = %d (%d %d %d)\n", jitter->auto_tradeoff, best, worst, i);*/
260    
261    /* FIXME: Compute a short-term estimate too and combine with the long-term one */
262    
263    /* Prevents reducing the buffer size when we haven't really had much data */
264    if (tot_count < TOP_DELAY && opt > 0)
265       return 0;
266    return opt;
267 }
268
269
270 /** Initialise jitter buffer */
271 JitterBuffer *jitter_buffer_init(void)
272 {
273    JitterBuffer *jitter = (JitterBuffer*)speex_alloc(sizeof(JitterBuffer));
274    if (jitter)
275    {
276       int i;
277       spx_int32_t tmp;
278       for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++)
279          jitter->packets[i].data=NULL;
280       jitter->delay_step = 1;
281       jitter->concealment_size = 1;
282       /*FIXME: Should this be 0 or 1?*/
283       jitter->buffer_margin = 0;
284       jitter->late_cutoff = 50;
285       jitter->destroy = NULL;
286       jitter->latency_tradeoff = 0;
287       tmp = 4;
288       jitter_buffer_ctl(jitter, JITTER_BUFFER_SET_MAX_LATE_RATE, &tmp);
289       jitter_buffer_reset(jitter);
290    }
291    return jitter;
292 }
293
294 /** Reset jitter buffer */
295 void jitter_buffer_reset(JitterBuffer *jitter)
296 {
297    int i;
298    for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++)
299    {
300       if (jitter->packets[i].data)
301       {
302          if (jitter->destroy)
303             jitter->destroy(jitter->packets[i].data);
304          else
305             speex_free(jitter->packets[i].data);
306          jitter->packets[i].data = NULL;
307       }
308    }
309    /* Timestamp is actually undefined at this point */
310    jitter->pointer_timestamp = 0;
311    jitter->next_stop = 0;
312    jitter->reset_state = 1;
313    jitter->lost_count = 0;
314    jitter->buffered = 0;
315    jitter->auto_tradeoff = 32000;
316    
317    for (i=0;i<MAX_BUFFERS;i++)
318    {
319       tb_init(&jitter->_tb[i]);
320       jitter->timeBuffers[i] = &jitter->_tb[i];
321    }
322    /*fprintf (stderr, "reset\n");*/
323 }
324
325 /** Destroy jitter buffer */
326 void jitter_buffer_destroy(JitterBuffer *jitter)
327 {
328    jitter_buffer_reset(jitter);
329    speex_free(jitter);
330 }
331
332 /** Take the following timing into consideration for future calculations */
333 static void update_timings(JitterBuffer *jitter, spx_int32_t timing)
334 {
335    if (timing < -32767)
336       timing = -32767;
337    if (timing > 32767)
338       timing = 32767;
339    /* If the current sub-window is full, perform a rotation and discard oldest sub-widow */
340    if (jitter->timeBuffers[0]->curr_count >= jitter->subwindow_size)
341    {
342       int i;
343       /*fprintf(stderr, "Rotate buffer\n");*/
344       struct TimingBuffer *tmp = jitter->timeBuffers[MAX_BUFFERS-1];
345       for (i=MAX_BUFFERS-1;i>=1;i--)
346          jitter->timeBuffers[i] = jitter->timeBuffers[i-1];
347       jitter->timeBuffers[0] = tmp;
348       tb_init(jitter->timeBuffers[0]);
349    }
350    tb_add(jitter->timeBuffers[0], timing);
351 }
352
353 /** Compensate all timings when we do an adjustment of the buffering */
354 static void shift_timings(JitterBuffer *jitter, spx_int16_t amount)
355 {
356    int i, j;
357    for (i=0;i<MAX_BUFFERS;i++)
358    {
359       for (j=0;j<jitter->timeBuffers[i]->filled;j++)
360          jitter->timeBuffers[i]->timing[j] += amount;
361    }
362 }
363
364
365 /** Put one packet into the jitter buffer */
366 void jitter_buffer_put(JitterBuffer *jitter, const JitterBufferPacket *packet)
367 {
368    int i,j;
369    int late;
370    /*fprintf (stderr, "put packet %d %d\n", timestamp, span);*/
371    
372    /* Syncing on the first packet to arrive */
373    if (jitter->reset_state)
374    {
375       jitter->reset_state=0;
376       jitter->pointer_timestamp = packet->timestamp;
377       jitter->next_stop = packet->timestamp;
378       /*fprintf(stderr, "reset to %d\n", timestamp);*/
379    }
380    
381    /* Cleanup buffer (remove old packets that weren't played) */
382    for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++)
383    {
384       /* Make sure we don't discard a "just-late" packet in case we want to play it next (if we interpolate). */
385       if (jitter->packets[i].data && LE32(jitter->packets[i].timestamp + jitter->packets[i].span, jitter->pointer_timestamp))
386       {
387          /*fprintf (stderr, "cleaned (not played)\n");*/
388          if (jitter->destroy)
389             jitter->destroy(jitter->packets[i].data);
390          else
391             speex_free(jitter->packets[i].data);
392          jitter->packets[i].data = NULL;
393       }
394    }
395
396    /*fprintf(stderr, "arrival: %d %d %d\n", packet->timestamp, jitter->next_stop, jitter->pointer_timestamp);*/
397    /* Check if packet is late (could still be useful though) */
398    if (LT32(packet->timestamp, jitter->next_stop))
399    {
400       update_timings(jitter, ((spx_int32_t)packet->timestamp) - ((spx_int32_t)jitter->next_stop) - jitter->buffer_margin);
401       late = 1;
402    } else {
403       late = 0;
404    }
405    
406    /* Only insert the packet if it's not hopelessly late (i.e. totally useless) */
407    if (GE32(packet->timestamp+packet->span+jitter->delay_step, jitter->pointer_timestamp))
408    {
409
410       /*Find an empty slot in the buffer*/
411       for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++)
412       {
413          if (jitter->packets[i].data==NULL)
414             break;
415       }
416       
417       /*No place left in the buffer, need to make room for it by discarding the oldest packet */
418       if (i==SPEEX_JITTER_MAX_BUFFER_SIZE)
419       {
420          int earliest=jitter->packets[0].timestamp;
421          i=0;
422          for (j=1;j<SPEEX_JITTER_MAX_BUFFER_SIZE;j++)
423          {
424             if (!jitter->packets[i].data || LT32(jitter->packets[j].timestamp,earliest))
425             {
426                earliest = jitter->packets[j].timestamp;
427                i=j;
428             }
429          }
430          if (jitter->destroy)
431             jitter->destroy(jitter->packets[i].data);
432          else
433             speex_free(jitter->packets[i].data);
434          jitter->packets[i].data=NULL;
435          if (jitter->lost_count>20)
436          {
437             jitter_buffer_reset(jitter);
438          }
439          /*fprintf (stderr, "Buffer is full, discarding earliest frame %d (currently at %d)\n", timestamp, jitter->pointer_timestamp);*/      
440       }
441    
442       /* Copy packet in buffer */
443       if (jitter->destroy)
444       {
445          jitter->packets[i].data = packet->data;
446       } else {
447          jitter->packets[i].data=(char*)speex_alloc(packet->len);
448          for (j=0;j<packet->len;j++)
449             jitter->packets[i].data[j]=packet->data[j];
450       }
451       jitter->packets[i].timestamp=packet->timestamp;
452       jitter->packets[i].span=packet->span;
453       jitter->packets[i].len=packet->len;
454       jitter->packets[i].user_data=packet->user_data;
455       if (late)
456          jitter->arrival[i] = 0;
457       else
458          jitter->arrival[i] = jitter->next_stop;
459    }
460    
461    
462 }
463
464 /** Get one packet from the jitter buffer */
465 int jitter_buffer_get(JitterBuffer *jitter, JitterBufferPacket *packet, spx_int32_t desired_span, spx_int32_t *start_offset)
466 {
467    int i;
468    unsigned int j;
469    int incomplete = 0;
470    spx_int16_t opt;
471    
472    jitter->last_returned_timestamp = jitter->pointer_timestamp;
473          
474    if (jitter->interp_requested != 0)
475    {
476       if (start_offset)
477          *start_offset = 0;
478       packet->timestamp = jitter->pointer_timestamp;
479       packet->span = jitter->interp_requested;
480       
481       /* Increment the pointer because it got decremented in the delay update */
482       jitter->pointer_timestamp += jitter->interp_requested;
483       packet->len = 0;
484       /*fprintf (stderr, "Deferred interpolate\n");*/
485       
486       jitter->interp_requested = 0;
487       
488       jitter->buffered = packet->span - desired_span;
489
490       return JITTER_BUFFER_MISSING;
491    }
492    
493    /* Searching for the packet that fits best */
494    
495    /* Search the buffer for a packet with the right timestamp and spanning the whole current chunk */
496    for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++)
497    {
498       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))
499          break;
500    }
501    
502    /* If no match, try for an "older" packet that still spans (fully) the current chunk */
503    if (i==SPEEX_JITTER_MAX_BUFFER_SIZE)
504    {
505       for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++)
506       {
507          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))
508             break;
509       }
510    }
511    
512    /* If still no match, try for an "older" packet that spans part of the current chunk */
513    if (i==SPEEX_JITTER_MAX_BUFFER_SIZE)
514    {
515       for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++)
516       {
517          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))
518             break;
519       }
520    }
521    
522    /* If still no match, try for earliest packet possible */
523    if (i==SPEEX_JITTER_MAX_BUFFER_SIZE)
524    {
525       int found = 0;
526       spx_uint32_t best_time=0;
527       int best_span=0;
528       int besti=0;
529       for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++)
530       {
531          /* check if packet starts within current chunk */
532          if (jitter->packets[i].data && LT32(jitter->packets[i].timestamp,jitter->pointer_timestamp+desired_span) && GE32(jitter->packets[i].timestamp,jitter->pointer_timestamp))
533          {
534             if (!found || LT32(jitter->packets[i].timestamp,best_time) || (jitter->packets[i].timestamp==best_time && GT32(jitter->packets[i].span,best_span)))
535             {
536                best_time = jitter->packets[i].timestamp;
537                best_span = jitter->packets[i].span;
538                besti = i;
539                found = 1;
540             }
541          }
542       }
543       if (found)
544       {
545          i=besti;
546          incomplete = 1;
547          /*fprintf (stderr, "incomplete: %d %d %d %d\n", jitter->packets[i].timestamp, jitter->pointer_timestamp, chunk_size, jitter->packets[i].span);*/
548       }
549    }
550
551    /* If we find something */
552    if (i!=SPEEX_JITTER_MAX_BUFFER_SIZE)
553    {
554       
555       
556       /* We (obviously) haven't lost this packet */
557       jitter->lost_count = 0;
558       
559       /* In this case, 0 isn't as a valid timestamp */
560       if (jitter->arrival[i] != 0)
561       {
562          update_timings(jitter, ((spx_int32_t)jitter->packets[i].timestamp) - ((spx_int32_t)jitter->arrival[i]) - jitter->buffer_margin);
563       }
564       
565       
566       /* FIXME: Check for potential overflow */
567       packet->len = jitter->packets[i].len;
568       /* Copy packet */
569       if (jitter->destroy)
570       {
571          packet->data = jitter->packets[i].data;
572       } else {
573          for (j=0;j<packet->len;j++)
574             packet->data[j] = jitter->packets[i].data[j];
575          /* Remove packet */
576          speex_free(jitter->packets[i].data);
577       }
578       jitter->packets[i].data = NULL;
579       /* Set timestamp and span (if requested) */
580       if (start_offset)
581          *start_offset = (spx_int32_t)jitter->packets[i].timestamp-(spx_int32_t)jitter->pointer_timestamp;
582       
583       packet->timestamp = jitter->packets[i].timestamp;
584       jitter->last_returned_timestamp = packet->timestamp;
585       
586       packet->span = jitter->packets[i].span;
587       packet->user_data = jitter->packets[i].user_data;
588       /* Point to the end of the current packet */
589       jitter->pointer_timestamp = jitter->packets[i].timestamp+jitter->packets[i].span;
590
591       jitter->buffered = *start_offset + packet->span - desired_span;
592
593       if (incomplete)
594          return JITTER_BUFFER_INCOMPLETE;
595       else
596          return JITTER_BUFFER_OK;
597    }
598    
599    
600    /* If we haven't found anything worth returning */
601    
602    /*fprintf (stderr, "not found\n");*/
603    jitter->lost_count++;
604    /*fprintf (stderr, "m");*/
605    /*fprintf (stderr, "lost_count = %d\n", jitter->lost_count);*/
606    if (start_offset)
607       *start_offset = 0;
608    
609    opt = compute_opt_delay(jitter);
610    
611    /* Should we force an increase in the buffer or just do normal interpolation? */   
612    if (opt < 0)
613    {
614       /* Need to increase buffering */
615       
616       /* Shift histogram to compensate */
617       shift_timings(jitter, -opt);
618       
619       packet->timestamp = jitter->pointer_timestamp;
620       packet->span = -opt;
621       /* Don't move the pointer_timestamp forward */
622       packet->len = 0;
623       
624       /*jitter->pointer_timestamp -= jitter->delay_step;*/
625       /*fprintf (stderr, "Forced to interpolate\n");*/
626    } else {
627       /* Normal packet loss */
628       packet->timestamp = jitter->pointer_timestamp;
629       
630       desired_span = ROUND_DOWN(desired_span, jitter->concealment_size);
631       packet->span = desired_span;
632       jitter->pointer_timestamp += desired_span;
633       packet->len = 0;
634       /*fprintf (stderr, "Normal loss\n");*/
635    }
636
637    jitter->buffered = packet->span - desired_span;
638    return JITTER_BUFFER_MISSING;
639
640 }
641
642 int jitter_buffer_get_another(JitterBuffer *jitter, JitterBufferPacket *packet)
643 {
644    int i, j;
645    for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++)
646    {
647       if (jitter->packets[i].data && jitter->packets[i].timestamp==jitter->last_returned_timestamp)
648          break;
649    }
650    if (i!=SPEEX_JITTER_MAX_BUFFER_SIZE)
651    {
652       /* Copy packet */
653       packet->len = jitter->packets[i].len;
654       if (jitter->destroy)
655       {
656          packet->data = jitter->packets[i].data;
657       } else {
658          for (j=0;j<packet->len;j++)
659             packet->data[j] = jitter->packets[i].data[j];
660          /* Remove packet */
661          speex_free(jitter->packets[i].data);
662       }
663       jitter->packets[i].data = NULL;
664       packet->timestamp = jitter->packets[i].timestamp;
665       packet->span = jitter->packets[i].span;
666       packet->user_data = jitter->packets[i].user_data;
667       return JITTER_BUFFER_OK;
668    } else {
669       packet->data = NULL;
670       packet->len = 0;
671       packet->span = 0;
672       return JITTER_BUFFER_MISSING;
673    }
674 }
675
676 /** Get pointer timestamp of jitter buffer */
677 int jitter_buffer_get_pointer_timestamp(JitterBuffer *jitter)
678 {
679    return jitter->pointer_timestamp;
680 }
681
682 void jitter_buffer_tick(JitterBuffer *jitter)
683 {
684    if (jitter->buffered >= 0)
685    {
686       jitter->next_stop = jitter->pointer_timestamp - jitter->buffered;
687    } else {
688       jitter->next_stop = jitter->pointer_timestamp;
689       speex_warning_int("jitter buffer sees negative buffering, you code might be broken. Value is ", jitter->buffered);
690    }
691    jitter->buffered = 0;
692 }
693
694 void jitter_buffer_remaining_span(JitterBuffer *jitter, spx_uint32_t rem)
695 {
696    if (jitter->buffered < 0)
697       speex_warning_int("jitter buffer sees negative buffering, you code might be broken. Value is ", jitter->buffered);
698    jitter->next_stop = jitter->pointer_timestamp - rem;
699 }
700
701 /* Let the jitter buffer know it's the right time to adjust the buffering delay to the network conditions */
702 int jitter_buffer_update_delay(JitterBuffer *jitter, JitterBufferPacket *packet, spx_int32_t *start_offset)
703 {
704    spx_int16_t opt = compute_opt_delay(jitter);
705    /*fprintf(stderr, "opt adjustment is %d ", opt);*/
706    
707    if (opt < 0)
708    {
709       shift_timings(jitter, -opt);
710       
711       jitter->pointer_timestamp += opt;
712       jitter->interp_requested = -opt;
713       /*fprintf (stderr, "Decision to interpolate %d samples\n", -opt);*/
714    } else if (opt > 0)
715    {
716       shift_timings(jitter, -opt);
717       jitter->pointer_timestamp += opt;
718       /*fprintf (stderr, "Decision to drop %d samples\n", opt);*/
719    }
720    
721    return opt;
722 }
723
724 /* Used like the ioctl function to control the jitter buffer parameters */
725 int jitter_buffer_ctl(JitterBuffer *jitter, int request, void *ptr)
726 {
727    int count, i;
728    switch(request)
729    {
730       case JITTER_BUFFER_SET_MARGIN:
731          jitter->buffer_margin = *(spx_int32_t*)ptr;
732          break;
733       case JITTER_BUFFER_GET_MARGIN:
734          *(spx_int32_t*)ptr = jitter->buffer_margin;
735          break;
736       case JITTER_BUFFER_GET_AVALIABLE_COUNT:
737          count = 0;
738          for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++)
739          {
740             if (jitter->packets[i].data && LE32(jitter->pointer_timestamp, jitter->packets[i].timestamp))
741             {
742                count++;
743             }
744          }
745          *(spx_int32_t*)ptr = count;
746          break;
747       case JITTER_BUFFER_SET_DESTROY_CALLBACK:
748          jitter->destroy = (void (*) (void *))ptr;
749          break;
750       case JITTER_BUFFER_GET_DESTROY_CALLBACK:
751          *(void (**) (void *))ptr = jitter->destroy;
752          break;
753       case JITTER_BUFFER_SET_DELAY_STEP:
754          jitter->delay_step = *(spx_int32_t*)ptr;
755          break;
756       case JITTER_BUFFER_GET_DELAY_STEP:
757          *(spx_int32_t*)ptr = jitter->delay_step;
758          break;
759       case JITTER_BUFFER_SET_CONCEALMENT_SIZE:
760          jitter->concealment_size = *(spx_int32_t*)ptr;
761          break;
762       case JITTER_BUFFER_GET_CONCEALMENT_SIZE:
763          *(spx_int32_t*)ptr = jitter->concealment_size;
764          break;
765       case JITTER_BUFFER_SET_MAX_LATE_RATE:
766          jitter->max_late_rate = *(spx_int32_t*)ptr;
767          jitter->window_size = 100*TOP_DELAY/jitter->max_late_rate;
768          jitter->subwindow_size = jitter->window_size/MAX_BUFFERS;
769          break;
770       case JITTER_BUFFER_GET_MAX_LATE_RATE:
771          *(spx_int32_t*)ptr = jitter->max_late_rate;
772          break;
773       case JITTER_BUFFER_SET_LATE_COST:
774          jitter->latency_tradeoff = *(spx_int32_t*)ptr;
775          break;
776       case JITTER_BUFFER_GET_LATE_COST:
777          *(spx_int32_t*)ptr = jitter->latency_tradeoff;
778          break;
779       default:
780          speex_warning_int("Unknown jitter_buffer_ctl request: ", request);
781          return -1;
782    }
783    return 0;
784 }
785