[xiph-commits] r14072 - trunk/speex/libspeex

jm at svn.xiph.org jm at svn.xiph.org
Tue Oct 30 23:55:30 PDT 2007


Author: jm
Date: 2007-10-30 23:55:29 -0700 (Tue, 30 Oct 2007)
New Revision: 14072

Modified:
   trunk/speex/libspeex/jitter.c
Log:
jitter buffer: implemented zero-copy (untested)


Modified: trunk/speex/libspeex/jitter.c
===================================================================
--- trunk/speex/libspeex/jitter.c	2007-10-31 06:32:54 UTC (rev 14071)
+++ trunk/speex/libspeex/jitter.c	2007-10-31 06:55:29 UTC (rev 14072)
@@ -34,8 +34,6 @@
 
 /* TODO:
  - Implement JITTER_BUFFER_SET_DESTROY_CALLBACK
- - Implement JITTER_BUFFER_SET_TIME_ADJUST
- - Get rid of tick()
  - Multiple get / get_multiple()
  - User data?
  - Use JitterBufferPacket internally
@@ -78,6 +76,8 @@
    int span[SPEEX_JITTER_MAX_BUFFER_SIZE];                                /**< Timestamp of packet                 */
    int len[SPEEX_JITTER_MAX_BUFFER_SIZE];                                 /**< Number of bytes in packet           */
    
+   void (*destroy) (void *);                                           /**< Callback for destroying a packet */
+
    spx_int32_t sub_clock;                                                 /** Time since last get() call (incremented by tick()) */
    int resolution;                                                        /**< Time resolution for histogram (timestamp units) */
    int delay_step;                                                        /**< Size of the steps when adjusting buffering (timestamp units) */
@@ -105,6 +105,7 @@
       jitter->delay_step = resolution;
       jitter->buffer_margin = 1;
       jitter->late_cutoff = 50;
+      jitter->destroy = NULL;
       jitter_buffer_reset(jitter);
    }
    return jitter;
@@ -118,7 +119,10 @@
    {
       if (jitter->buf[i])
       {
-         speex_free(jitter->buf[i]);
+         if (jitter->destroy)
+            jitter->destroy(jitter->buf[i]);
+         else
+            speex_free(jitter->buf[i]);
          jitter->buf[i] = NULL;
       }
    }
@@ -165,7 +169,10 @@
       if (jitter->buf[i] && LE32(jitter->timestamp[i] + jitter->span[i], jitter->pointer_timestamp))
       {
          /*fprintf (stderr, "cleaned (not played)\n");*/
-         speex_free(jitter->buf[i]);
+         if (jitter->destroy)
+            jitter->destroy(jitter->buf[i]);
+         else
+            speex_free(jitter->buf[i]);
          jitter->buf[i] = NULL;
       }
    }
@@ -191,7 +198,10 @@
             i=j;
          }
       }
-      speex_free(jitter->buf[i]);
+      if (jitter->destroy)
+         jitter->destroy(jitter->buf[i]);
+      else
+         speex_free(jitter->buf[i]);
       jitter->buf[i]=NULL;
       if (jitter->lost_count>20)
       {
@@ -201,9 +211,14 @@
    }
    
    /* Copy packet in buffer */
-   jitter->buf[i]=(char*)speex_alloc(packet->len);
-   for (j=0;j<packet->len;j++)
-      jitter->buf[i][j]=packet->data[j];
+   if (jitter->destroy)
+   {
+      jitter->buf[i] = packet->data;
+   } else {
+      jitter->buf[i]=(char*)speex_alloc(packet->len);
+      for (j=0;j<packet->len;j++)
+         jitter->buf[i][j]=packet->data[j];
+   }
    jitter->timestamp[i]=packet->timestamp;
    jitter->span[i]=packet->span;
    jitter->len[i]=packet->len;
@@ -387,10 +402,15 @@
       /* Check for potential overflow */
       packet->len = jitter->len[i];
       /* Copy packet */
-      for (j=0;j<packet->len;j++)
-         packet->data[j] = jitter->buf[i][j];
-      /* Remove packet */
-      speex_free(jitter->buf[i]);
+      if (jitter->destroy)
+      {
+         packet->data = jitter->buf[i];
+      } else {
+         for (j=0;j<packet->len;j++)
+            packet->data[j] = jitter->buf[i][j];
+         /* Remove packet */
+         speex_free(jitter->buf[i]);
+      }
       jitter->buf[i] = NULL;
       /* Set timestamp and span (if requested) */
       if (start_offset)
@@ -553,6 +573,12 @@
          }
          *(spx_int32_t*)ptr = count;
          break;
+      case JITTER_BUFFER_SET_DESTROY_CALLBACK:
+         jitter->destroy = (void (*) (void *))ptr;
+         break;
+      case JITTER_BUFFER_GET_DESTROY_CALLBACK:
+         *(void (**) (void *))ptr = jitter->destroy;
+         break;
       case JITTER_BUFFER_SET_DELAY_STEP:
          jitter->delay_step = *(spx_int32_t*)ptr;
          break;



More information about the commits mailing list