[xiph-commits] r3731 - libsydneyaudio/trunk/src

j at svn.annodex.net j at svn.annodex.net
Sat Oct 18 09:50:32 PDT 2008


Author: j
Date: 2008-10-18 09:50:32 -0700 (Sat, 18 Oct 2008)
New Revision: 3731

Modified:
   libsydneyaudio/trunk/src/sydney_audio_alsa.c
Log:
- update alsa backend, do not use custom ringbuffer, alsa provides that
- use frame bytes functions provided by alsa (snd_pcm_frames_to_bytes)
- do not request lowest possibly delay(0) but set it to 500ms (500000) in snd_pcm_set_params,
  this removes clicking and makes the alsa backend work with pulseaudio



Modified: libsydneyaudio/trunk/src/sydney_audio_alsa.c
===================================================================
--- libsydneyaudio/trunk/src/sydney_audio_alsa.c	2008-10-14 09:43:26 UTC (rev 3730)
+++ libsydneyaudio/trunk/src/sydney_audio_alsa.c	2008-10-18 16:50:32 UTC (rev 3731)
@@ -34,76 +34,28 @@
  * ***** END LICENSE BLOCK ***** *
  */
 #include <stdlib.h>
-#include <pthread.h>
 #include <alsa/asoundlib.h>
 #include "sydney_audio.h"
 
 /* ALSA implementation based heavily on sydney_audio_mac.c */
 
-/*
- * The audio interface is based on a "pull" I/O model, which means you
- * can't just provide a data buffer and tell the audio device to play; you must
- * register a callback and provide data as the device asks for it. To support
- * sydney audio's "write-to-play" style interface, we have to buffer up the
- * data as it arrives and feed it to the callback as required.
- *
- * This is handled by a simple linked list of buffers; data is always written
- * to the tail and read from the head. Each buffer tracks the start and end
- * positions of its contained data. Buffers are allocated when the tail buffer
- * fills, and freed when the head buffer empties. There is always at least one
- * buffer allocated.
- *
- *       s   e      s      e      s  e            + data read
- *    +++#####  ->  ########  ->  ####----        # data written
- *    ^                           ^               - empty
- *    bl_head                     bl_tail
- */
 
-typedef struct sa_buf sa_buf;
-struct sa_buf {
-  unsigned int      size;
-  unsigned int      start;
-  unsigned int      end;
-  sa_buf          * next;
-  unsigned char     data[0];
-};
-
 struct sa_stream {
   snd_pcm_t*        output_unit;
-  pthread_t         thread_id;
-  pthread_mutex_t   mutex;
   char              playing;
   int64_t           bytes_played;
+  int64_t           bytes_written;
 
   /* audio format info */
   unsigned int      rate;
   unsigned int      n_channels;
-  unsigned int      bytes_per_ch;
-
-  /* buffer list */
-  sa_buf          * bl_head;
-  sa_buf          * bl_tail;
-  int               n_bufs;
+  snd_pcm_uframes_t buffer_size;
+  snd_pcm_uframes_t period_size;
+  unsigned int      buffer_bytes;
+  unsigned int      period_bytes;
 };
 
-
 /*
- * Use a default buffer size with enough room for one second of audio,
- * assuming stereo data at 44.1kHz with 32 bits per channel, and impose
- * a generous limit on the number of buffers.
- */
-#define BUF_SIZE    (2 * 44100 * 4)
-#define BUF_LIMIT   5
-
-#if BUF_LIMIT < 2
-#error BUF_LIMIT must be at least 2!
-#endif
-
-static void audio_callback(void* s);
-static sa_buf *new_buffer(void);
-
-
-/*
  * -----------------------------------------------------------------------------
  * Startup and shutdown functions
  * -----------------------------------------------------------------------------
@@ -141,25 +93,13 @@
   if ((s = malloc(sizeof(sa_stream_t))) == NULL) {
     return SA_ERROR_OOM;
   }
-  if ((s->bl_head = new_buffer()) == NULL) {
-    free(s);
-    return SA_ERROR_OOM;
-  }
-  if (pthread_mutex_init(&s->mutex, NULL) != 0) {
-    free(s->bl_head);
-    free(s);
-    return SA_ERROR_SYSTEM;
-  }
 
   s->output_unit  = NULL;
-  s->thread_id    = 0;
   s->playing      = 0;
   s->bytes_played = 0;
+  s->bytes_written = 0;
   s->rate         = rate;
   s->n_channels   = n_channels;
-  s->bytes_per_ch = 2;
-  s->bl_tail      = s->bl_head;
-  s->n_bufs       = 1;
 
   *_s = s;
   return SA_SUCCESS;
@@ -189,11 +129,19 @@
                          s->n_channels,
                          s->rate,
                          1,
-                         0) < 0) {
+                         500000) < 0) {
     snd_pcm_close(s->output_unit);
     s->output_unit = NULL;
     return SA_ERROR_NOT_SUPPORTED;
   }
+  
+  if (snd_pcm_get_params(s->output_unit, &s->buffer_size, &s->period_size) < 0) {
+    snd_pcm_close(s->output_unit);
+    s->output_unit = NULL;
+    return SA_ERROR_NOT_SUPPORTED;
+  }
+  s->period_bytes = (unsigned int)snd_pcm_frames_to_bytes(s->output_unit, s->period_size);
+  s->buffer_bytes = (unsigned int)snd_pcm_frames_to_bytes(s->output_unit, s->buffer_size);
 
   return SA_SUCCESS;
 }
@@ -206,15 +154,7 @@
   if (s == NULL) {
     return SA_SUCCESS;
   }
-
-  pthread_mutex_lock(&s->mutex);
-
   /*
-   * This causes the thread sending data to ALSA to stop
-   */
-  s->thread_id = 0;
-
-  /*
    * Shut down the audio output device.
    */
   if (s->output_unit != NULL) {
@@ -222,22 +162,6 @@
       result = SA_ERROR_SYSTEM;
     }
   }
-
-  pthread_mutex_unlock(&s->mutex);
-
-  /*
-   * Release resources.
-   */
-  if (pthread_mutex_destroy(&s->mutex) != 0) {
-    result = SA_ERROR_SYSTEM;
-  }
-  while (s->bl_head != NULL) {
-    sa_buf  * next = s->bl_head->next;
-    free(s->bl_head);
-    s->bl_head = next;
-  }
-  free(s);
-
   return result;
 }
 
@@ -252,6 +176,7 @@
 int
 sa_stream_write(sa_stream_t *s, const void *data, size_t nbytes) {
   int result = SA_SUCCESS;
+  snd_pcm_sframes_t frames, nframes;
 
   if (s == NULL || s->output_unit == NULL) {
     return SA_ERROR_NO_INIT;
@@ -260,186 +185,35 @@
     return SA_SUCCESS;
   }
 
-  pthread_mutex_lock(&s->mutex);
+  nframes = snd_pcm_bytes_to_frames(s->output_unit, nbytes);
 
-  /*
-   * Append the new data to the end of our buffer list.
-   */
-  while (1) {
-    unsigned int avail = s->bl_tail->size - s->bl_tail->end;
-
-    if (nbytes <= avail) {
-
-      /*
-       * The new data will fit into the current tail buffer, so
-       * just copy it in and we're done.
-       */
-      memcpy(s->bl_tail->data + s->bl_tail->end, data, nbytes);
-      s->bl_tail->end += nbytes;
-      break;
-
-    } else {
-
-      /*
-       * Copy what we can into the tail and allocate a new buffer
-       * for the rest.
-       */
-      memcpy(s->bl_tail->data + s->bl_tail->end, data, avail);
-      s->bl_tail->end += avail;
-      data = ((unsigned char *)data) + avail;
-      nbytes -= avail;
-
-      /* 
-       * If we still have data left to copy but we've hit the limit of
-       * allowable buffer allocations, we need to spin for a bit to allow
-       * the audio callback function to slurp some more data up.
-       */
-      if (nbytes > 0 && s->n_bufs == BUF_LIMIT) {
-#ifdef TIMING_TRACE
-        printf("#");  /* too much audio data */
-#endif
-        if (!s->playing) {
-          /*
-           * We haven't even started playing yet! That means the
-           * BUF_SIZE/BUF_LIMIT values are too low... Not much we can
-           * do here; spinning won't help because the audio callback
-           * hasn't been enabled yet. Oh well, error time.
-           */
-          printf("Too much audio data received before audio device enabled!\n");
-          result = SA_ERROR_SYSTEM;
-          break;
-        }
-        while (s->n_bufs == BUF_LIMIT) {
-          struct timespec ts = {0, 1000000};
-          pthread_mutex_unlock(&s->mutex);
-          nanosleep(&ts, NULL);
-          pthread_mutex_lock(&s->mutex);
-        }
-      }
-
-      /* 
-       * Allocate a new tail buffer, and go 'round again to fill it up.
-       */
-      if ((s->bl_tail->next = new_buffer()) == NULL) {
-        result = SA_ERROR_OOM;
-        break;
-      }
-      s->n_bufs++;
-      s->bl_tail = s->bl_tail->next;
-    
-    } /* if (nbytes <= avail), else */
-
-  } /* while (1) */
-
-  pthread_mutex_unlock(&s->mutex);
-
-  /*
-   * Once we have our first block of audio data, enable the audio callback
-   * function. This doesn't need to be protected by the mutex, because
-   * s->playing is not used in the audio callback thread, and it's probably
-   * better not to be inside the lock when we enable the audio callback.
-   */
-  if (!s->playing) {
-    s->playing = 1;
-    if (pthread_create(&s->thread_id, NULL, (void *)audio_callback, s) != 0) {
-      result = SA_ERROR_SYSTEM;
-    }
-  }
-
-  return result;
-}
-
-
-static void audio_callback(void* data)
-{
-  sa_stream_t* s = (sa_stream_t*)data;
-  snd_pcm_uframes_t buffer_size;
-  snd_pcm_uframes_t period_size;
-  unsigned int bytes_per_frame = s->n_channels * s->bytes_per_ch;
-  char* buffer = 0;
-
-#ifdef TIMING_TRACE
-  printf(".");  /* audio read 'tick' */
-#endif
-
-  snd_pcm_get_params(s->output_unit, &buffer_size, &period_size);
- 
-  buffer = malloc(period_size * bytes_per_frame);
- 
-  while(1) {
-    char* dst = buffer;
-    unsigned int bytes_to_copy   = period_size * bytes_per_frame;
-    snd_pcm_sframes_t frames;
-
-    pthread_mutex_lock(&s->mutex);
-    if (!s->thread_id)
-      break;
-
-    /*
-     * Consume data from the start of the buffer list.
-     */
-    while (1) {
-      unsigned int avail = s->bl_head->end - s->bl_head->start;
-      assert(s->bl_head->start <= s->bl_head->end);
-
-      if (avail >= bytes_to_copy) {
-        /*
-         * We have all we need in the head buffer, so just grab it and go.
-         */
-        memcpy(dst, s->bl_head->data + s->bl_head->start, bytes_to_copy);
-        s->bl_head->start += bytes_to_copy;
-        s->bytes_played += bytes_to_copy;
-        break;
-    
-      } else {
-        sa_buf* next = 0;
-        /*
-         * Copy what we can from the head and move on to the next buffer.
-         */
-        memcpy(dst, s->bl_head->data + s->bl_head->start, avail);
-        s->bl_head->start += avail;
-        dst += avail;
-        bytes_to_copy -= avail;
-        s->bytes_played += avail;
-
-        /*
-         * We want to free the now-empty buffer, but not if it's also the
-         * current tail. If it is the tail, we don't have enough data to fill
-         * the destination buffer, so we'll just zero it out and give up.
-         */
-        next = s->bl_head->next;
-        if (next == NULL) {
-#ifdef TIMING_TRACE
-          printf("!");  /* not enough audio data */
-#endif
-          memset(dst, 0, bytes_to_copy);
-          break;
-        }
-        free(s->bl_head);
-        s->bl_head = next;
-        s->n_bufs--;
-    
-      } /* if (avail >= bytes_to_copy), else */
-      
-    } /* while (1) */
-    
-    pthread_mutex_unlock(&s->mutex);
-    
-    frames = snd_pcm_writei(s->output_unit, buffer, period_size);
+  while(nframes>0) {
+    snd_pcm_sframes_t len = s->period_size;
+    if(nframes < s->period_size)
+      len = nframes;
+    frames = snd_pcm_writei(s->output_unit, data, len);
     if (frames < 0) {
       frames = snd_pcm_recover(s->output_unit, frames, 1);
       if (frames < 0) {
         printf("snc_pcm_recover error: %s\n", snd_strerror(frames));
+        return SA_ERROR_SYSTEM;
       }
-      if(frames > 0 && frames < period_size)
-        printf("short write (expected %d, wrote %d)\n", (int)period_size, (int)frames);;
+      if(frames > 0 && frames < len)
+        printf("short write (expected %d, wrote %d)\n", (int)len, (int)frames);
     }
+    nframes -= s->period_size;
+    data = ((unsigned char *)data) + s->period_bytes;
   }
-  free(buffer);
-}
 
+  s->bytes_written += nbytes;
 
+  if (!s->playing) {
+    s->playing = 1;
+  }
 
+  return result;
+}
+
 /*
  * -----------------------------------------------------------------------------
  * General query and support functions
@@ -448,41 +222,51 @@
 
 int
 sa_stream_get_write_size(sa_stream_t *s, size_t *size) {
-  sa_buf  * b;
-  size_t    used = 0;
+  snd_pcm_sframes_t avail;
 
   if (s == NULL || s->output_unit == NULL) {
     return SA_ERROR_NO_INIT;
   }
 
-  pthread_mutex_lock(&s->mutex);
+  avail = snd_pcm_avail_update(s->output_unit);
 
-  /*
-   * The sum of the free space in the tail buffer plus the size of any new
-   * buffers represents the write space available before blocking.
-   */
-  unsigned int avail = s->bl_tail->size - s->bl_tail->end;
-  avail += (BUF_LIMIT - s->n_bufs) * BUF_SIZE;
-  *size = avail;
+  *size = snd_pcm_frames_to_bytes(s->output_unit, avail);
 
-  pthread_mutex_unlock(&s->mutex);
   return SA_SUCCESS;
 }
 
-
 int
 sa_stream_get_position(sa_stream_t *s, sa_position_t position, int64_t *pos) {
-
+  int err;
+  snd_pcm_sframes_t delay;
+  
   if (s == NULL || s->output_unit == NULL) {
     return SA_ERROR_NO_INIT;
   }
+
   if (position != SA_POSITION_WRITE_SOFTWARE) {
     return SA_ERROR_NOT_SUPPORTED;
   }
 
-  pthread_mutex_lock(&s->mutex);
+  if (snd_pcm_state(s->output_unit) != SND_PCM_STATE_RUNNING) {
+    delay = 0;
+  }
+  else if (snd_pcm_delay (s->output_unit, &delay) != 0) {
+    printf("snd_pcm_delay failed\n");
+    delay = 0;
+  }
+  if (delay < 0) {
+    delay = 0;
+  }
+  if (delay > s->buffer_size) {
+    printf("delay just wrong %d vs %d\n", delay, s->buffer_size);
+    delay = s->buffer_size;
+  }
+
+  s->bytes_played = s->bytes_written - snd_pcm_frames_to_bytes(s->output_unit, delay);
+
   *pos = s->bytes_played;
-  pthread_mutex_unlock(&s->mutex);
+
   return SA_SUCCESS;
 }
 
@@ -493,12 +277,10 @@
   if (s == NULL || s->output_unit == NULL) {
     return SA_ERROR_NO_INIT;
   }
-
-  pthread_mutex_lock(&s->mutex);
 #if 0 /* TODO */
   AudioOutputUnitStop(s->output_unit);
 #endif
-  pthread_mutex_unlock(&s->mutex);
+
   return SA_SUCCESS;
 }
 
@@ -510,57 +292,23 @@
     return SA_ERROR_NO_INIT;
   }
 
-  pthread_mutex_lock(&s->mutex);
-
   /*
    * The audio device resets its mSampleTime counter after pausing,
    * so we need to clear our tracking value to keep that in sync.
    */
-  s->bytes_played = 0;
-#if 0 /* TODO */
-  AudioOutputUnitStart(s->output_unit);
-#endif
-  pthread_mutex_unlock(&s->mutex);
+  s->bytes_played = s->bytes_written = 0;
+
   return SA_SUCCESS;
 }
 
 
-static sa_buf *
-new_buffer(void) {
-  sa_buf  * b = malloc(sizeof(sa_buf) + BUF_SIZE);
-  if (b != NULL) {
-    b->size  = BUF_SIZE;
-    b->start = 0;
-    b->end   = 0;
-    b->next  = NULL;
-  }
-  return b;
-}
-
-
 int
 sa_stream_drain(sa_stream_t *s)
 {
   if (s == NULL || s->output_unit == NULL) {
     return SA_ERROR_NO_INIT;
   }
-
-  while (1) {
-    pthread_mutex_lock(&s->mutex);
-    sa_buf  * b;
-    size_t    used = 0;
-    for (b = s->bl_head; b != NULL; b = b->next) {
-      used += b->end - b->start;
-    }
-    pthread_mutex_unlock(&s->mutex);
-
-    if (used == 0) {
-      break;
-    }
-
-    struct timespec ts = {0, 1000000};
-    nanosleep(&ts, NULL);
-  }
+  snd_pcm_drain(s->output_unit);
   return SA_SUCCESS;
 }
 



More information about the commits mailing list