chiark / gitweb /
disorder-playrtp now prefers to drop silent packets (rather than
authorRichard Kettlewell <rjk@greenend.org.uk>
Sat, 18 Apr 2009 12:44:05 +0000 (13:44 +0100)
committerRichard Kettlewell <rjk@greenend.org.uk>
Sat, 18 Apr 2009 12:44:05 +0000 (13:44 +0100)
filling up buffers and having the OS eventually drop them from the
input socket's buffer).

clients/playrtp.c
clients/playrtp.h

index fe750b2f47d0692a7e8a5c9cb26e8e03dd727e20..bb780d8a8d809ea8650d2ef85899e015fe79eeac 100644 (file)
@@ -402,6 +402,14 @@ static void *listen_thread(void attribute((unused)) *arg) {
       fatal(0, "unsupported RTP payload type %d",
             header.mpt & 0x7F);
     }
+    /* See if packet is silent */
+    const uint16_t *s = p->samples_raw;
+    unsigned n = p->nsamples;
+    for(; n > 0; --n)
+      if(*s++)
+        break;
+    if(!n)
+      p->flags |= SILENT;
     if(logfp)
       fprintf(logfp, "sequence %u timestamp %"PRIx32" length %"PRIx32" end %"PRIx32"\n",
               seq, timestamp, p->nsamples, timestamp + p->nsamples);
@@ -505,6 +513,7 @@ static size_t playrtp_callback(void *buffer,
                                size_t max_samples,
                                void attribute((unused)) *userdata) {
   size_t samples;
+  int silent = 0;
 
   pthread_mutex_lock(&lock);
   /* Get the next packet, junking any that are now in the past */
@@ -535,6 +544,7 @@ static size_t playrtp_callback(void *buffer,
       *bufptr++ = (int16_t)ntohs(*ptr++);
       --i;
     }
+    silent = !!(p->flags & SILENT);
   } else {
     /* There is no suitable packet.  We introduce 0s up to the next packet, or
      * to fill the buffer if there's no next packet or that's too many.  The
@@ -545,6 +555,7 @@ static size_t playrtp_callback(void *buffer,
       samples = max_samples;
     //info("infill by %zu", samples);
     memset(buffer, 0, samples * uaudio_sample_size);
+    silent = 1;
   }
   /* Debug dump */
   if(dump_buffer) {
@@ -555,6 +566,12 @@ static size_t playrtp_callback(void *buffer,
   }
   /* Advance timestamp */
   next_timestamp += samples;
+  /* If we're getting behind then try to drop just silent packets */
+  if(nsamples > minbuffer && silent) {
+    info("dropping %zu samples (%"PRIu32" > %"PRIu32")",
+         samples, nsamples, minbuffer);
+    samples = 0;
+  }
   /* Junk obsolete packets */
   playrtp_next_packet();
   pthread_mutex_unlock(&lock);
index d5caa4c1c66cdeaae135f63fb46557f3c6af4b3f..ff8a76197052ffc4734866d6c8dc1fe9bf11688d 100644 (file)
@@ -58,11 +58,15 @@ struct packet {
    *
    * Valid values are:
    * - @ref IDLE - the idle bit was set in the RTP packet
+   * - @ref SILENT - packet is entirely silent
    */
   unsigned flags;
 /** @brief idle bit set in RTP packet*/
 #define IDLE 0x0001
 
+/** @brief RTP packet is entirely silent */
+#define SILENT 0x0002
+
   /** @brief Raw sample data
    *
    * Only the first @p nsamples samples are defined; the rest is uninitialized