[xiph-commits] r14205 - in trunk/speex: include/speex libspeex

jm at svn.xiph.org jm at svn.xiph.org
Tue Nov 20 16:23:37 PST 2007


Author: jm
Date: 2007-11-20 16:23:36 -0800 (Tue, 20 Nov 2007)
New Revision: 14205

Modified:
   trunk/speex/include/speex/speex_jitter.h
   trunk/speex/libspeex/jitter.c
Log:
jitter buffer: put back the argument to the init function. Also, fixed an
incorrect behaviour the margin was non-zero and several packets arrived in
advance.


Modified: trunk/speex/include/speex/speex_jitter.h
===================================================================
--- trunk/speex/include/speex/speex_jitter.h	2007-11-20 20:01:18 UTC (rev 14204)
+++ trunk/speex/include/speex/speex_jitter.h	2007-11-21 00:23:36 UTC (rev 14205)
@@ -117,10 +117,12 @@
 
 /** Initialises jitter buffer 
  * 
- * @param tick Number of samples per "tick", i.e. the time period of the elements that will be retrieved
+ * @param step_size Starting value for the size of concleanment packets and delay 
+       adjustment steps. Can be changed at any time using JITTER_BUFFER_SET_DELAY_STEP
+       and JITTER_BUFFER_GET_CONCEALMENT_SIZE.
  * @return Newly created jitter buffer state
  */
-JitterBuffer *jitter_buffer_init(void);
+JitterBuffer *jitter_buffer_init(int step_size);
 
 /** Restores jitter buffer to its original state 
  * 

Modified: trunk/speex/libspeex/jitter.c
===================================================================
--- trunk/speex/libspeex/jitter.c	2007-11-20 20:01:18 UTC (rev 14204)
+++ trunk/speex/libspeex/jitter.c	2007-11-21 00:23:36 UTC (rev 14205)
@@ -268,7 +268,7 @@
 
 
 /** Initialise jitter buffer */
-JitterBuffer *jitter_buffer_init(void)
+JitterBuffer *jitter_buffer_init(int step_size)
 {
    JitterBuffer *jitter = (JitterBuffer*)speex_alloc(sizeof(JitterBuffer));
    if (jitter)
@@ -277,8 +277,8 @@
       spx_int32_t tmp;
       for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++)
          jitter->packets[i].data=NULL;
-      jitter->delay_step = 1;
-      jitter->concealment_size = 1;
+      jitter->delay_step = step_size;
+      jitter->concealment_size = step_size;
       /*FIXME: Should this be 0 or 1?*/
       jitter->buffer_margin = 0;
       jitter->late_cutoff = 50;
@@ -369,33 +369,27 @@
    int late;
    /*fprintf (stderr, "put packet %d %d\n", timestamp, span);*/
    
-   /* Syncing on the first packet to arrive */
-   if (jitter->reset_state)
-   {
-      jitter->reset_state=0;
-      jitter->pointer_timestamp = packet->timestamp;
-      jitter->next_stop = packet->timestamp;
-      /*fprintf(stderr, "reset to %d\n", timestamp);*/
-   }
-   
    /* Cleanup buffer (remove old packets that weren't played) */
-   for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++)
+   if (!jitter->reset_state)
    {
-      /* Make sure we don't discard a "just-late" packet in case we want to play it next (if we interpolate). */
-      if (jitter->packets[i].data && LE32(jitter->packets[i].timestamp + jitter->packets[i].span, jitter->pointer_timestamp))
+      for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++)
       {
-         /*fprintf (stderr, "cleaned (not played)\n");*/
-         if (jitter->destroy)
-            jitter->destroy(jitter->packets[i].data);
-         else
-            speex_free(jitter->packets[i].data);
-         jitter->packets[i].data = NULL;
+         /* Make sure we don't discard a "just-late" packet in case we want to play it next (if we interpolate). */
+         if (jitter->packets[i].data && LE32(jitter->packets[i].timestamp + jitter->packets[i].span, jitter->pointer_timestamp))
+         {
+            /*fprintf (stderr, "cleaned (not played)\n");*/
+            if (jitter->destroy)
+               jitter->destroy(jitter->packets[i].data);
+            else
+               speex_free(jitter->packets[i].data);
+            jitter->packets[i].data = NULL;
+         }
       }
    }
-
+   
    /*fprintf(stderr, "arrival: %d %d %d\n", packet->timestamp, jitter->next_stop, jitter->pointer_timestamp);*/
    /* Check if packet is late (could still be useful though) */
-   if (LT32(packet->timestamp, jitter->next_stop))
+   if (!jitter->reset_state && LT32(packet->timestamp, jitter->next_stop))
    {
       update_timings(jitter, ((spx_int32_t)packet->timestamp) - ((spx_int32_t)jitter->next_stop) - jitter->buffer_margin);
       late = 1;
@@ -404,7 +398,7 @@
    }
    
    /* Only insert the packet if it's not hopelessly late (i.e. totally useless) */
-   if (GE32(packet->timestamp+packet->span+jitter->delay_step, jitter->pointer_timestamp))
+   if (jitter->reset_state || GE32(packet->timestamp+packet->span+jitter->delay_step, jitter->pointer_timestamp))
    {
 
       /*Find an empty slot in the buffer*/
@@ -452,7 +446,7 @@
       jitter->packets[i].span=packet->span;
       jitter->packets[i].len=packet->len;
       jitter->packets[i].user_data=packet->user_data;
-      if (late)
+      if (jitter->reset_state || late)
          jitter->arrival[i] = 0;
       else
          jitter->arrival[i] = jitter->next_stop;
@@ -469,6 +463,35 @@
    int incomplete = 0;
    spx_int16_t opt;
    
+   /* Syncing on the first call */
+   if (jitter->reset_state)
+   {
+      int found = 0;
+      /* Find the oldest packet */
+      spx_uint32_t oldest;
+      for (i=0;i<SPEEX_JITTER_MAX_BUFFER_SIZE;i++)
+      {
+         if (jitter->packets[i].data && (!found || LT32(jitter->packets[i].timestamp,oldest)))
+         {
+            oldest = jitter->packets[i].timestamp;
+            found = 1;
+         }
+      }
+      if (found)
+      {
+         jitter->reset_state=0;         
+         jitter->pointer_timestamp = oldest;
+         jitter->next_stop = oldest;
+      } else {
+         if (start_offset)
+            *start_offset = 0;
+         packet->timestamp = 0;
+         packet->span = jitter->interp_requested;
+         return JITTER_BUFFER_MISSING;
+      }
+   }
+   
+
    jitter->last_returned_timestamp = jitter->pointer_timestamp;
          
    if (jitter->interp_requested != 0)



More information about the commits mailing list