jitter buffer: histogram shifting function (not used yet)
[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 - Write generic functions for computing stats and shifting the histogram
38 - Take into account the delay step when computing the stats and when shifting
39 - Linked list structure for holding the packets instead of the current fixed-size array
40   + return memory to a pool
41   + allow pre-allocation of the pool
42   + optional max number of elements 
43 */
44 #ifdef HAVE_CONFIG_H
45 #include "config.h"
46 #endif
47
48
49 #include "misc.h"
50 #include <speex/speex.h>
51 #include <speex/speex_bits.h>
52 #include <speex/speex_jitter.h>
53 #include "os_support.h"
54
55 #ifndef NULL
56 #define NULL 0
57 #endif
58
59 #define LATE_BINS 15
60 #define MAX_MARGIN 30                     /**< Number of bins in margin histogram */
61
62 #define SPEEX_JITTER_MAX_BUFFER_SIZE 200   /**< Maximum number of packets in jitter buffer */
63
64 #define TSUB(a,b) ((spx_int32_t)((a)-(b)))
65
66 #define GT32(a,b) (((spx_int32_t)((a)-(b)))>0)
67 #define GE32(a,b) (((spx_int32_t)((a)-(b)))>=0)
68 #define LT32(a,b) (((spx_int32_t)((a)-(b)))<0)
69 #define LE32(a,b) (((spx_int32_t)((a)-(b)))<=0)
70
71 /** Jitter buffer structure */
72 struct JitterBuffer_ {
73    spx_uint32_t pointer_timestamp;                                        /**< Timestamp of what we will *get* next */
74    spx_uint32_t last_returned_timestamp;
75    spx_uint32_t next_stop;
76    
77    JitterBufferPacket packets[SPEEX_JITTER_MAX_BUFFER_SIZE];              /**< Packets stored in the buffer */
78    spx_uint32_t arrival[SPEEX_JITTER_MAX_BUFFER_SIZE];                    /**< Packet arrival time (0 means it was late, even though it's a valid timestamp) */
79    
80    void (*destroy) (void *);                                              /**< Callback for destroying a packet */
81
82    int resolution;                                                        /**< Time resolution for histogram (timestamp units) */
83    int delay_step;                                                        /**< Size of the steps when adjusting buffering (timestamp units) */
84    int reset_state;                                                       /**< True if state was just reset        */
85    int buffer_margin;                                                     /**< How many frames we want to keep in the buffer (lower bound) */
86    int late_cutoff;                                                       /**< How late must a packet be for it not to be considered at all */
87    int interp_requested;                                                  /**< An interpolation is requested by speex_jitter_update_delay() */
88
89    int lost_count;                                                        /**< Number of consecutive lost packets  */
90    float shortterm_margin[MAX_MARGIN];                                    /**< Short term margin histogram         */
91    float longterm_margin[MAX_MARGIN];                                     /**< Long term margin histogram          */
92    float loss_rate;                                                       /**< Average loss rate                   */
93 };
94
95 /** Initialise jitter buffer */
96 JitterBuffer *jitter_buffer_init(int resolution)
97 {
98    JitterBuffer *jitter = (JitterBuffer*)speex_alloc(sizeof(JitterBuffer));
99    if (jitter)
100    {
101       int i;
102       for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++)
103          jitter->packets[i].data=NULL;
104       jitter->resolution = resolution;
105       jitter->delay_step = resolution;
106       //FIXME: Should this be 0 or 1?
107       jitter->buffer_margin = 1;
108       jitter->late_cutoff = 50;
109       jitter->destroy = NULL;
110       jitter_buffer_reset(jitter);
111    }
112    return jitter;
113 }
114
115 /** Reset jitter buffer */
116 void jitter_buffer_reset(JitterBuffer *jitter)
117 {
118    int i;
119    for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++)
120    {
121       if (jitter->packets[i].data)
122       {
123          if (jitter->destroy)
124             jitter->destroy(jitter->packets[i].data);
125          else
126             speex_free(jitter->packets[i].data);
127          jitter->packets[i].data = NULL;
128       }
129    }
130    /* Timestamp is actually undefined at this point */
131    jitter->pointer_timestamp = 0;
132    jitter->next_stop = 0;
133    jitter->reset_state = 1;
134    jitter->lost_count = 0;
135    jitter->loss_rate = 0;
136    for (i=0;i<MAX_MARGIN;i++)
137    {
138       jitter->shortterm_margin[i] = 0;
139       jitter->longterm_margin[i] = 0;
140    }
141    /*fprintf (stderr, "reset\n");*/
142 }
143
144 /** Destroy jitter buffer */
145 void jitter_buffer_destroy(JitterBuffer *jitter)
146 {
147    jitter_buffer_reset(jitter);
148    speex_free(jitter);
149 }
150
151 static void update_histogram(JitterBuffer *jitter, spx_int32_t arrival_margin)
152 {
153    int i;
154    if (arrival_margin >= -jitter->late_cutoff)
155    {
156       /* Here we compute the histogram based on the time of arrival of the packet.
157       This is based on a (first-order) recursive average. We keep both a short-term
158       histogram and a long-term histogram */
159       spx_int32_t int_margin;
160       /* First, apply the "damping" of the recursive average to all bins */
161       for (i=0;i<MAX_MARGIN;i++)
162       {
163          jitter->shortterm_margin[i] *= .98;
164          jitter->longterm_margin[i] *= .995;
165       }
166       /* What histogram bin the packet should be counted in */
167       int_margin = LATE_BINS + arrival_margin;
168       if (int_margin>MAX_MARGIN-1)
169          int_margin = MAX_MARGIN-1;
170       if (int_margin<0)
171          int_margin = 0;
172       /* Add the packet to the right bin */
173       jitter->shortterm_margin[int_margin] += .02;
174       jitter->longterm_margin[int_margin] += .005;
175    } else {
176       /* Packet has arrived *way* too late, we pretty much consider it lost and not take it into account in the histogram */
177       /*fprintf (stderr, "way too late = %d\n", arrival_margin);*/
178       if (jitter->lost_count>20)
179       {
180          jitter_buffer_reset(jitter);
181       }
182    }
183 }
184
185 static void shift_histogram(JitterBuffer *jitter, int amount)
186 {
187    int i, c;
188    if (amount == 0)
189       return;
190    if (amount > 0)
191    {
192       /* FIXME: This is terribly inefficient */
193       for (c=0;c<amount;c++)
194       {
195          jitter->shortterm_margin[MAX_MARGIN-1] += jitter->shortterm_margin[MAX_MARGIN-2];
196          jitter->longterm_margin[MAX_MARGIN-1] += jitter->longterm_margin[MAX_MARGIN-2];
197          for (i=MAX_MARGIN-3;i>=0;i--)
198          {
199             jitter->shortterm_margin[i+1] = jitter->shortterm_margin[i];
200             jitter->longterm_margin[i+1] = jitter->longterm_margin[i];
201          }
202          jitter->shortterm_margin[0] = 0;
203          jitter->longterm_margin[0] = 0;
204       }
205    } else {
206       /* FIXME: This is terribly inefficient */
207       for (c=0;c<-amount;c++)
208       {
209          jitter->shortterm_margin[0] += jitter->shortterm_margin[1];
210          jitter->longterm_margin[0] += jitter->longterm_margin[1];
211          for (i=1;i<MAX_MARGIN-1;i++)
212          {
213             jitter->shortterm_margin[i] = jitter->shortterm_margin[i+1];
214             jitter->longterm_margin[i] = jitter->longterm_margin[i+1];
215          }
216          jitter->shortterm_margin[MAX_MARGIN-1] = 0;
217          jitter->longterm_margin[MAX_MARGIN-1] = 0;
218       }
219    }
220 }
221
222 /** Put one packet into the jitter buffer */
223 void jitter_buffer_put(JitterBuffer *jitter, const JitterBufferPacket *packet)
224 {
225    int i,j;
226    int late;
227    spx_int32_t arrival_margin;
228    spx_int32_t arrival_time;
229    /*fprintf (stderr, "put packet %d %d\n", timestamp, span);*/
230    
231    /* Syncing on the first packet to arrive */
232    if (jitter->reset_state)
233    {
234       jitter->reset_state=0;
235       jitter->pointer_timestamp = packet->timestamp;
236       jitter->next_stop = packet->timestamp;
237       /*fprintf(stderr, "reset to %d\n", timestamp);*/
238    }
239    
240    /* Cleanup buffer (remove old packets that weren't played) */
241    for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++)
242    {
243       /* Make sure we don't discard a "just-late" packet in case we want to play it next (if we interpolate). */
244       if (jitter->packets[i].data && LE32(jitter->packets[i].timestamp + jitter->packets[i].span, jitter->pointer_timestamp))
245       {
246          /*fprintf (stderr, "cleaned (not played)\n");*/
247          if (jitter->destroy)
248             jitter->destroy(jitter->packets[i].data);
249          else
250             speex_free(jitter->packets[i].data);
251          jitter->packets[i].data = NULL;
252       }
253    }
254
255    fprintf(stderr, "arrival: %d %d %d\n", packet->timestamp, jitter->next_stop, jitter->pointer_timestamp);
256    /* Check if packet is late (could still be useful though) */
257    if (LT32(packet->timestamp, jitter->next_stop))
258    {
259       fprintf(stderr, "late by %d\n", jitter->next_stop - packet->timestamp);
260       
261       /* The arrival margin is how much in advance (or in this case late) the packet it (in resolution units) */
262       arrival_margin = (((spx_int32_t)packet->timestamp) - ((spx_int32_t)jitter->next_stop))/jitter->resolution - jitter->buffer_margin;
263    
264       fprintf(stderr, "put arrival_margin = %d\n", arrival_margin);
265
266       update_histogram(jitter, arrival_margin);
267       late = 1;
268    } else {
269       late = 0;
270    }
271    
272    /* Only insert the packet if it's not hopelessly late (i.e. totally useless) */
273    if (GE32(packet->timestamp+packet->span+jitter->delay_step, jitter->pointer_timestamp))
274    {
275
276       /*Find an empty slot in the buffer*/
277       for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++)
278       {
279          if (jitter->packets[i].data==NULL)
280             break;
281       }
282       
283       /*No place left in the buffer, need to make room for it by discarding the oldest packet */
284       if (i==SPEEX_JITTER_MAX_BUFFER_SIZE)
285       {
286          int earliest=jitter->packets[0].timestamp;
287          i=0;
288          for (j=1;j<SPEEX_JITTER_MAX_BUFFER_SIZE;j++)
289          {
290             if (!jitter->packets[i].data || LT32(jitter->packets[j].timestamp,earliest))
291             {
292                earliest = jitter->packets[j].timestamp;
293                i=j;
294             }
295          }
296          if (jitter->destroy)
297             jitter->destroy(jitter->packets[i].data);
298          else
299             speex_free(jitter->packets[i].data);
300          jitter->packets[i].data=NULL;
301          if (jitter->lost_count>20)
302          {
303             jitter_buffer_reset(jitter);
304          }
305          /*fprintf (stderr, "Buffer is full, discarding earliest frame %d (currently at %d)\n", timestamp, jitter->pointer_timestamp);*/      
306       }
307    
308       /* Copy packet in buffer */
309       if (jitter->destroy)
310       {
311          jitter->packets[i].data = packet->data;
312       } else {
313          jitter->packets[i].data=(char*)speex_alloc(packet->len);
314          for (j=0;j<packet->len;j++)
315             jitter->packets[i].data[j]=packet->data[j];
316       }
317       jitter->packets[i].timestamp=packet->timestamp;
318       jitter->packets[i].span=packet->span;
319       jitter->packets[i].len=packet->len;
320       jitter->packets[i].user_data=packet->user_data;
321       if (late)
322          jitter->arrival[i] = 0;
323       else
324          jitter->arrival[i] = jitter->next_stop;
325    }
326    
327    
328 }
329
330 /** Get one packet from the jitter buffer */
331 int jitter_buffer_get(JitterBuffer *jitter, JitterBufferPacket *packet, spx_int32_t desired_span, spx_int32_t *start_offset)
332 {
333    int i;
334    unsigned int j;
335    float late_ratio_short;
336    float late_ratio_long;
337    float ontime_ratio_short;
338    float ontime_ratio_long;
339    float early_ratio_short;
340    float early_ratio_long;
341    int incomplete = 0;
342    
343    jitter->last_returned_timestamp = jitter->pointer_timestamp;
344          
345    if (jitter->interp_requested)
346    {
347       jitter->interp_requested = 0;
348       if (start_offset)
349          *start_offset = 0;
350       packet->timestamp = jitter->pointer_timestamp;
351       packet->span = jitter->delay_step;
352       
353       /* Increment the pointer because it got decremented in the delay update */
354       jitter->pointer_timestamp += jitter->delay_step;
355       packet->len = 0;
356       fprintf (stderr, "Deferred interpolate\n");
357       
358       return JITTER_BUFFER_MISSING;
359    }
360    
361    /* Compiling arrival statistics */
362    
363    late_ratio_short = 0;
364    late_ratio_long = 0;
365    /* Count the proportion of packets that are late */
366    for (i=0;i<LATE_BINS;i++)
367    {
368       late_ratio_short += jitter->shortterm_margin[i];
369       late_ratio_long += jitter->longterm_margin[i];
370    }
371    /* Count the proportion of packets that are just on time */
372    ontime_ratio_short = jitter->shortterm_margin[LATE_BINS];
373    ontime_ratio_long = jitter->longterm_margin[LATE_BINS];
374    early_ratio_short = early_ratio_long = 0;
375    /* Count the proportion of packets that are early */
376    for (i=LATE_BINS+1;i<MAX_MARGIN;i++)
377    {
378       early_ratio_short += jitter->shortterm_margin[i];
379       early_ratio_long += jitter->longterm_margin[i];
380    }
381    if (0&&jitter->pointer_timestamp%1000==0)
382    {
383       /*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);*/
384       /*fprintf (stderr, "%f %f\n", early_ratio_short + ontime_ratio_short + late_ratio_short, early_ratio_long + ontime_ratio_long + late_ratio_long);*/
385    }
386    
387    
388    /* Searching for the packet that fits best */
389    
390    /* Search the buffer for a packet with the right timestamp and spanning the whole current chunk */
391    for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++)
392    {
393       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))
394          break;
395    }
396    
397    /* If no match, try for an "older" packet that still spans (fully) the current chunk */
398    if (i==SPEEX_JITTER_MAX_BUFFER_SIZE)
399    {
400       for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++)
401       {
402          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))
403             break;
404       }
405    }
406    
407    /* If still no match, try for an "older" packet that spans part of the current chunk */
408    if (i==SPEEX_JITTER_MAX_BUFFER_SIZE)
409    {
410       for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++)
411       {
412          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))
413             break;
414       }
415    }
416    
417    /* If still no match, try for earliest packet possible */
418    if (i==SPEEX_JITTER_MAX_BUFFER_SIZE)
419    {
420       int found = 0;
421       spx_uint32_t best_time=0;
422       int best_span=0;
423       int besti=0;
424       for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++)
425       {
426          /* check if packet starts within current chunk */
427          if (jitter->packets[i].data && LT32(jitter->packets[i].timestamp,jitter->pointer_timestamp+desired_span) && GE32(jitter->packets[i].timestamp,jitter->pointer_timestamp))
428          {
429             if (!found || LT32(jitter->packets[i].timestamp,best_time) || (jitter->packets[i].timestamp==best_time && GT32(jitter->packets[i].span,best_span)))
430             {
431                best_time = jitter->packets[i].timestamp;
432                best_span = jitter->packets[i].span;
433                besti = i;
434                found = 1;
435             }
436          }
437       }
438       if (found)
439       {
440          i=besti;
441          incomplete = 1;
442          /*fprintf (stderr, "incomplete: %d %d %d %d\n", jitter->packets[i].timestamp, jitter->pointer_timestamp, chunk_size, jitter->packets[i].span);*/
443       }
444    }
445
446    /* If we find something */
447    if (i!=SPEEX_JITTER_MAX_BUFFER_SIZE)
448    {
449       
450       
451       /* We (obviously) haven't lost this packet */
452       jitter->lost_count = 0;
453       jitter->loss_rate = .999*jitter->loss_rate;
454       
455       /* In this case, 0 isn't as a valid timestamp */
456       if (jitter->arrival[i] != 0)
457       {
458          spx_int32_t arrival_margin;
459          fprintf(stderr, "early by %d\n", jitter->packets[i].timestamp - jitter->arrival[i]);
460          
461          /* The arrival margin is how much in advance (or in this case late) the packet it (in resolution units) */
462          arrival_margin = (((spx_int32_t)jitter->packets[i].timestamp) - ((spx_int32_t)jitter->arrival[i]))/jitter->resolution - jitter->buffer_margin;
463    
464          fprintf(stderr, "get arrival_margin = %d\n", arrival_margin);
465
466          update_histogram(jitter, arrival_margin);
467
468       }
469       
470       
471       /* FIXME: Check for potential overflow */
472       packet->len = jitter->packets[i].len;
473       /* Copy packet */
474       if (jitter->destroy)
475       {
476          packet->data = jitter->packets[i].data;
477       } else {
478          for (j=0;j<packet->len;j++)
479             packet->data[j] = jitter->packets[i].data[j];
480          /* Remove packet */
481          speex_free(jitter->packets[i].data);
482       }
483       jitter->packets[i].data = NULL;
484       /* Set timestamp and span (if requested) */
485       if (start_offset)
486          *start_offset = (spx_int32_t)jitter->packets[i].timestamp-(spx_int32_t)jitter->pointer_timestamp;
487       
488       packet->timestamp = jitter->packets[i].timestamp;
489       jitter->last_returned_timestamp = packet->timestamp;
490       
491       packet->span = jitter->packets[i].span;
492       packet->user_data = jitter->packets[i].user_data;
493       /* Point to the end of the current packet */
494       jitter->pointer_timestamp = jitter->packets[i].timestamp+jitter->packets[i].span;
495
496       if (incomplete)
497          return JITTER_BUFFER_INCOMPLETE;
498       else
499          return JITTER_BUFFER_OK;
500    }
501    
502    
503    /* If we haven't found anything worth returning */
504    
505    /*fprintf (stderr, "not found\n");*/
506    jitter->lost_count++;
507    /*fprintf (stderr, "m");*/
508    /*fprintf (stderr, "lost_count = %d\n", jitter->lost_count);*/
509    jitter->loss_rate = .999*jitter->loss_rate + .001;
510    if (start_offset)
511       *start_offset = 0;
512    
513    /* Should we force an increase in the buffer or just do normal interpolation? */   
514    if (late_ratio_short > .1 || late_ratio_long > .03)
515    {
516       /* Increase buffering */
517       
518       /* Shift histogram */
519       jitter->shortterm_margin[MAX_MARGIN-1] += jitter->shortterm_margin[MAX_MARGIN-2];
520       jitter->longterm_margin[MAX_MARGIN-1] += jitter->longterm_margin[MAX_MARGIN-2];
521       for (i=MAX_MARGIN-3;i>=0;i--)
522       {
523          jitter->shortterm_margin[i+1] = jitter->shortterm_margin[i];
524          jitter->longterm_margin[i+1] = jitter->longterm_margin[i];         
525       }
526       jitter->shortterm_margin[0] = 0;
527       jitter->longterm_margin[0] = 0;
528       
529       packet->timestamp = jitter->pointer_timestamp;
530       packet->span = jitter->delay_step;
531       /* Don't move the pointer_timestamp forward */
532       packet->len = 0;
533       
534       jitter->pointer_timestamp -= jitter->delay_step;
535       fprintf (stderr, "Forced to interpolate\n");
536    } else {
537       /* Normal packet loss */
538       packet->timestamp = jitter->pointer_timestamp;
539       packet->span = desired_span;
540       jitter->pointer_timestamp += desired_span;
541       packet->len = 0;
542       fprintf (stderr, "Normal loss\n");
543    }
544
545    return JITTER_BUFFER_MISSING;
546
547 }
548
549 int jitter_buffer_get_another(JitterBuffer *jitter, JitterBufferPacket *packet)
550 {
551    int i, j;
552    for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++)
553    {
554       if (jitter->packets[i].data && jitter->packets[i].timestamp==jitter->last_returned_timestamp)
555          break;
556    }
557    if (i!=SPEEX_JITTER_MAX_BUFFER_SIZE)
558    {
559       /* Copy packet */
560       packet->len = jitter->packets[i].len;
561       if (jitter->destroy)
562       {
563          packet->data = jitter->packets[i].data;
564       } else {
565          for (j=0;j<packet->len;j++)
566             packet->data[j] = jitter->packets[i].data[j];
567          /* Remove packet */
568          speex_free(jitter->packets[i].data);
569       }
570       jitter->packets[i].data = NULL;
571       packet->timestamp = jitter->packets[i].timestamp;
572       packet->span = jitter->packets[i].span;
573       packet->user_data = jitter->packets[i].user_data;
574       return JITTER_BUFFER_OK;
575    } else {
576       packet->data = NULL;
577       packet->len = 0;
578       packet->span = 0;
579       return JITTER_BUFFER_MISSING;
580    }
581 }
582
583 /** Get pointer timestamp of jitter buffer */
584 int jitter_buffer_get_pointer_timestamp(JitterBuffer *jitter)
585 {
586    return jitter->pointer_timestamp;
587 }
588
589 void jitter_buffer_tick(JitterBuffer *jitter)
590 {
591    jitter->next_stop = jitter->pointer_timestamp;
592 }
593
594 void jitter_buffer_remaining_span(JitterBuffer *jitter, spx_uint32_t rem)
595 {
596    jitter->next_stop = jitter->pointer_timestamp - rem;
597 }
598
599 /* Let the jitter buffer know it's the right time to adjust the buffering delay to the network conditions */
600 int jitter_buffer_update_delay(JitterBuffer *jitter, JitterBufferPacket *packet, spx_int32_t *start_offset)
601 {
602    int i;
603    float late_ratio_short;
604    float late_ratio_long;
605    float ontime_ratio_short;
606    float ontime_ratio_long;
607    float early_ratio_short;
608    float early_ratio_long;
609    
610    late_ratio_short = 0;
611    late_ratio_long = 0;
612    /* Count the proportion of packets that are late */
613    for (i=0;i<LATE_BINS;i++)
614    {
615       late_ratio_short += jitter->shortterm_margin[i];
616       late_ratio_long += jitter->longterm_margin[i];
617    }
618    /* Count the proportion of packets that are just on time */
619    ontime_ratio_short = jitter->shortterm_margin[LATE_BINS];
620    ontime_ratio_long = jitter->longterm_margin[LATE_BINS];
621    early_ratio_short = early_ratio_long = 0;
622    /* Count the proportion of packets that are early */
623    for (i=LATE_BINS+1;i<MAX_MARGIN;i++)
624    {
625       early_ratio_short += jitter->shortterm_margin[i];
626       early_ratio_long += jitter->longterm_margin[i];
627    }
628    
629    /* Adjusting the buffering bssed on the amount of packets that are early/on time/late */   
630    if (late_ratio_short > .1 || late_ratio_long > .03)
631    {
632       /* If too many packets are arriving late */
633       jitter->shortterm_margin[MAX_MARGIN-1] += jitter->shortterm_margin[MAX_MARGIN-2];
634       jitter->longterm_margin[MAX_MARGIN-1] += jitter->longterm_margin[MAX_MARGIN-2];
635       for (i=MAX_MARGIN-3;i>=0;i--)
636       {
637          jitter->shortterm_margin[i+1] = jitter->shortterm_margin[i];
638          jitter->longterm_margin[i+1] = jitter->longterm_margin[i];         
639       }
640       jitter->shortterm_margin[0] = 0;
641       jitter->longterm_margin[0] = 0;            
642       jitter->pointer_timestamp -= jitter->delay_step;
643       jitter->interp_requested = 1;
644       fprintf (stderr, "Decision to interpolate\n");
645       return JITTER_BUFFER_ADJUST_INTERPOLATE;
646    
647    } else if (late_ratio_short + ontime_ratio_short < .005 && late_ratio_long + ontime_ratio_long < .01 && early_ratio_short > .8)
648    {
649       /* Many frames arriving early */
650       jitter->shortterm_margin[0] += jitter->shortterm_margin[1];
651       jitter->longterm_margin[0] += jitter->longterm_margin[1];
652       for (i=1;i<MAX_MARGIN-1;i++)
653       {
654          jitter->shortterm_margin[i] = jitter->shortterm_margin[i+1];
655          jitter->longterm_margin[i] = jitter->longterm_margin[i+1];         
656       }
657       jitter->shortterm_margin[MAX_MARGIN-1] = 0;
658       jitter->longterm_margin[MAX_MARGIN-1] = 0;      
659       /*fprintf (stderr, "drop frame\n");*/
660       /*fprintf (stderr, "d");*/
661       jitter->pointer_timestamp += jitter->delay_step;
662       fprintf (stderr, "Decision to drop\n");
663       return JITTER_BUFFER_ADJUST_DROP;
664    }
665    
666    return JITTER_BUFFER_ADJUST_OK;
667 }
668
669 /* Used like the ioctl function to control the jitter buffer parameters */
670 int jitter_buffer_ctl(JitterBuffer *jitter, int request, void *ptr)
671 {
672    int count, i;
673    switch(request)
674    {
675       case JITTER_BUFFER_SET_MARGIN:
676          jitter->buffer_margin = *(spx_int32_t*)ptr;
677          break;
678       case JITTER_BUFFER_GET_MARGIN:
679          *(spx_int32_t*)ptr = jitter->buffer_margin;
680          break;
681       case JITTER_BUFFER_GET_AVALIABLE_COUNT:
682          count = 0;
683          for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++)
684          {
685             if (jitter->packets[i].data && LE32(jitter->pointer_timestamp, jitter->packets[i].timestamp))
686             {
687                count++;
688             }
689          }
690          *(spx_int32_t*)ptr = count;
691          break;
692       case JITTER_BUFFER_SET_DESTROY_CALLBACK:
693          jitter->destroy = (void (*) (void *))ptr;
694          break;
695       case JITTER_BUFFER_GET_DESTROY_CALLBACK:
696          *(void (**) (void *))ptr = jitter->destroy;
697          break;
698       case JITTER_BUFFER_SET_DELAY_STEP:
699          jitter->delay_step = *(spx_int32_t*)ptr;
700          break;
701       case JITTER_BUFFER_GET_DELAY_STEP:
702          *(spx_int32_t*)ptr = jitter->delay_step;
703          break;
704       default:
705          speex_warning_int("Unknown jitter_buffer_ctl request: ", request);
706          return -1;
707    }
708    return 0;
709 }
710