Index: sys/sunaudio/gstsunaudiomixerctrl.c
===================================================================
RCS file: /cvs/gstreamer/gst-plugins-good/sys/sunaudio/gstsunaudiomixerctrl.c,v
retrieving revision 1.7
diff -u -r1.7 gstsunaudiomixerctrl.c
--- sys/sunaudio/gstsunaudiomixerctrl.c	15 Sep 2006 17:10:22 -0000	1.7
+++ sys/sunaudio/gstsunaudiomixerctrl.c	8 Dec 2006 23:32:46 -0000
@@ -35,6 +35,9 @@
 #include "gstsunaudiomixerctrl.h"
 #include "gstsunaudiomixertrack.h"
 
+GST_DEBUG_CATEGORY_EXTERN (sunaudio_debug);
+#define GST_CAT_DEFAULT sunaudio_debug
+
 #define SCALE_FACTOR 2.55       /* 255/100 */
 
 static gboolean
Index: sys/sunaudio/gstsunaudiosink.c
===================================================================
RCS file: /cvs/gstreamer/gst-plugins-good/sys/sunaudio/gstsunaudiosink.c,v
retrieving revision 1.8
diff -u -r1.8 gstsunaudiosink.c
--- sys/sunaudio/gstsunaudiosink.c	8 Dec 2006 15:12:01 -0000	1.8
+++ sys/sunaudio/gstsunaudiosink.c	8 Dec 2006 23:32:46 -0000
@@ -44,10 +44,14 @@
 #include <string.h>
 #include <stropts.h>
 #include <unistd.h>
+#include <time.h>
 #include <sys/mman.h>
 
 #include "gstsunaudiosink.h"
 
+GST_DEBUG_CATEGORY_EXTERN (sunaudio_debug);
+#define GST_CAT_DEFAULT sunaudio_debug
+
 /* elementfactory information */
 static const GstElementDetails plugin_details =
 GST_ELEMENT_DETAILS ("Sun Audio Sink",
@@ -175,12 +179,12 @@
   g_object_class_install_property (gobject_class, PROP_DEVICE,
       g_param_spec_string ("device", "Device", "Audio Device (/dev/audio)",
           DEFAULT_DEVICE, G_PARAM_READWRITE));
-
 }
 
 static void
 gst_sunaudiosink_init (GstSunAudioSink * sunaudiosink)
 {
+  GstBaseAudioSink *ba_sink = GST_BASE_AUDIO_SINK (sunaudiosink);
   const char *audiodev;
 
   GST_DEBUG_OBJECT (sunaudiosink, "initializing sunaudiosink");
@@ -340,16 +344,12 @@
   ainfo.play.encoding = AUDIO_ENCODING_LINEAR;
   ainfo.play.port = ports;
 
-  /*
-   * SunAudio doesn't really give access to buffer size, these values work.  Setting
-   * the buffer so large (512K) is a bit annoying because this causes the volume
-   * control in audio players to be slow in responding since the audio volume won't
-   * change until the buffer empties.  SunAudio doesn't seem to allow changing the
-   * audio output buffer size to anything smaller, though.  I notice setting the
-   * values smaller causes the audio to stutter, which is worse.
-   */
-  spec->segsize = 4096;
-  spec->segtotal = 128;
+  /* buffer_time for playback is not implemented in Solaris at the moment,
+     but at some point in the future, it might be */
+  ainfo.play.buffer_size = 
+      gst_util_uint64_scale (spec->rate * spec->bytes_per_sample,
+          spec->buffer_time, GST_SECOND / GST_USECOND);
+
   spec->silence_sample[0] = 0;
   spec->silence_sample[1] = 0;
   spec->silence_sample[2] = 0;
@@ -362,6 +362,28 @@
     return FALSE;
   }
 
+  /* Now read back the info to find out the actual buffer size and set 
+     segtotal */
+  AUDIO_INITINFO(&ainfo);
+
+  ret = ioctl (sunaudiosink->fd, AUDIO_GETINFO, &ainfo);
+  if (ret == -1) {
+    GST_ELEMENT_ERROR (sunaudiosink, RESOURCE, SETTINGS, (NULL), ("%s",
+            strerror (errno)));
+    return FALSE;
+  }
+  sunaudiosink->segtotal = spec->segtotal = 
+      ainfo.play.buffer_size / spec->segsize;
+  sunaudiosink->segtotal_samples = 
+      spec->segtotal * spec->segsize / spec->bytes_per_sample;
+
+  sunaudiosink->segs_written = (gint) ainfo.play.eof;
+  sunaudiosink->samples_written = ainfo.play.samples;
+  sunaudiosink->bytes_per_sample = spec->bytes_per_sample;
+
+  GST_DEBUG_OBJECT (sunaudiosink, "Got device buffer_size of %u", 
+      ainfo.play.buffer_size);
+
   return TRUE;
 }
 
@@ -371,21 +393,143 @@
   return TRUE;
 }
 
+#define LOOP_WHILE_EINTR(v,func) do { (v) = (func); } \
+		while ((v) == -1 && errno == EINTR);
+
+static void
+gst_sunaudio_sink_do_delay (GstSunAudioSink *sink, gint reset_cookie)
+{
+  GstBaseAudioSink *ba_sink = GST_BASE_AUDIO_SINK (sink);
+  GstClockTime total_sleep;
+  GstClockTime max_sleep;
+  GstClockTime sleep_nsecs = ba_sink->latency_time * GST_USECOND / 2;
+  struct timespec sleep_time;
+  struct timespec slept_time;
+  gint err;
+  audio_info_t ainfo;
+  guint diff;
+
+  /* This code below ensures that we don't race any further than buffer_time 
+   * ahead of the audio output, by sleeping if the next write call would cause
+   * us to advance too far in the ring-buffer */
+
+  LOOP_WHILE_EINTR (err, ioctl (sink->fd, AUDIO_GETINFO, &ainfo));
+  if (err < 0)
+    goto write_error;
+
+  /* Compute our offset from the output (copes with overflow) */
+  diff = (guint) g_atomic_int_get (&sink->segs_written) - ainfo.play.eof;
+
+  /* Never sleep longer than the initial number of undrained segments in the 
+     device plus one */
+  total_sleep = 0;
+  max_sleep = (diff + 1) * (ba_sink->latency_time * GST_USECOND);
+  /* sleep for half a segment period between .eof polls */
+  sleep_time.tv_sec = sleep_nsecs / GST_SECOND;
+  sleep_time.tv_nsec = sleep_nsecs % GST_SECOND;
+
+  /* If the next segment would take us too far along the ring buffer,
+   * sleep for a bit to free up a slot. If there were a way to find out
+   * when the eof field actually increments, we could use, but the only
+   * notification mechanism seems to be SIGPOLL, which we can't use from
+   * a support library */
+  while (diff+1 >= sink->segtotal && total_sleep < max_sleep &&
+      reset_cookie == g_atomic_int_get (&sink->reset_cookie)) {
+
+    GST_LOG_OBJECT (sink, "need to block to drain segment(s). Sleeping for %" 
+        G_GUINT64_FORMAT, sleep_nsecs);
+
+    if (nanosleep (&sleep_time, &slept_time) == 0)
+      total_sleep += sleep_nsecs;
+    else
+      total_sleep += slept_time.tv_sec * GST_SECOND + slept_time.tv_nsec;
+
+    LOOP_WHILE_EINTR (err, ioctl (sink->fd, AUDIO_GETINFO, &ainfo));
+    if (err < 0)
+      goto write_error;
+
+    /* Compute our (new) offset from the output (copes with overflow) */
+    diff = (guint) g_atomic_int_get (&sink->segs_written) - ainfo.play.eof;
+  } 
+
+  return;
+
+write_error:
+  GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
+      ("Playback error on device '%s': %s", sink->device,
+         strerror (errno)));
+}
+
 static guint
 gst_sunaudiosink_write (GstAudioSink * asink, gpointer data, guint length)
 {
-  return write (GST_SUNAUDIO_SINK (asink)->fd, data, length);
+  GstSunAudioSink *sink = GST_SUNAUDIO_SINK (asink);
+
+  gint bytes_written, err;
+  gint reset_cookie;
+
+  reset_cookie = g_atomic_int_get (&sink->reset_cookie);
+
+  LOOP_WHILE_EINTR (bytes_written, write (sink->fd, data, length));
+  if (bytes_written < 0) {
+    err = bytes_written;
+    goto write_error;
+  } 
+  if (reset_cookie != g_atomic_int_get (&sink->reset_cookie)) {
+    /* Reset was called while we were writing */
+    return (guint) bytes_written;
+  }
+
+  /* Increment our sample counter, for delay calcs */
+  g_atomic_int_add (&sink->samples_written, length / sink->bytes_per_sample);
+
+  /* Don't consider the segment written if we didn't output the whole lot yet */
+  if (bytes_written < length)
+    return (guint) bytes_written;
+
+  /* Write a zero length output to trigger increment of the eof field */
+  LOOP_WHILE_EINTR (err, write (sink->fd, NULL, 0));
+  if (err < 0)
+    goto write_error;
+
+  /* Count this extra segment we've written */
+  g_atomic_int_inc (&sink->segs_written);
+
+  /* Now delay so we don't overrun the ring buffer */
+  gst_sunaudio_sink_do_delay (sink, reset_cookie);
+
+  return length;
+
+write_error:
+  GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
+      ("Playback error on device '%s': %s", sink->device,
+         strerror (errno)));
+  return length; /* Say we wrote the segment to let the ringbuffer exit */
 }
 
 /*
- * Should provide the current delay between writing a sample to the
- * audio device and that sample being actually played.  Returning 0 for
- * now, but this isn't good for synchronization
- */
+ * Provide the current number of unplayed samples that have been written
+ * to the device */
 static guint
 gst_sunaudiosink_delay (GstAudioSink * asink)
 {
-  return 0;
+  GstSunAudioSink *sink = GST_SUNAUDIO_SINK (asink);
+  audio_info_t ainfo;
+  gint ret;
+  guint offset;
+
+  ret = ioctl (sink->fd, AUDIO_GETINFO, &ainfo);
+  if (G_UNLIKELY (ret == -1))
+    return 0;
+  
+  offset = (g_atomic_int_get (&sink->samples_written) -  ainfo.play.samples);
+
+  /* If the offset is larger than the total ringbuffer size, then we asked
+     between the write call and when samples_written is updated */
+  if (G_UNLIKELY (offset > sink->segtotal_samples))
+    return 0;
+
+  return offset;
 }
 
 static void
@@ -407,6 +551,10 @@
     return;
   }
 
+  /* After flushing the audio device, this will ensure the write thread exits 
+   * quickly */
+  g_atomic_int_inc (&sunaudiosink->reset_cookie);
+
   /*
    * Pause the audio - so audio stops playing immediately rather than
    * waiting for the ringbuffer to empty.
@@ -432,4 +580,9 @@
     GST_ELEMENT_ERROR (sunaudiosink, RESOURCE, SETTINGS, (NULL), ("%s",
             strerror (errno)));
   }
+
+  /* After flushing the audio device, we need to remeasure the sample count
+   * and segments written count */
+  g_atomic_int_set (&sunaudiosink->segs_written, (gint) ainfo.play.eof);
+  g_atomic_int_set (&sunaudiosink->samples_written, (gint) ainfo.play.samples);
 }
Index: sys/sunaudio/gstsunaudiosink.h
===================================================================
RCS file: /cvs/gstreamer/gst-plugins-good/sys/sunaudio/gstsunaudiosink.h,v
retrieving revision 1.3
diff -u -r1.3 gstsunaudiosink.h
--- sys/sunaudio/gstsunaudiosink.h	1 Jun 2006 21:07:26 -0000	1.3
+++ sys/sunaudio/gstsunaudiosink.h	8 Dec 2006 23:32:46 -0000
@@ -47,7 +47,19 @@
   audio_device_t dev;
   audio_info_t info;
 
-  gint   bytes_per_sample;
+  /* Number of segments the ringbuffer is configured for */
+  guint segtotal;
+  guint segtotal_samples;
+
+  /* Number of segments written to the device */
+  gint segs_written;
+  /* Number of samples written to the device */
+  gint samples_written;
+  guint bytes_per_sample;
+
+  /* Incremented each time the reset function is called to wake up the write
+     method early */
+  gint reset_cookie;
 };
 
 struct _GstSunAudioSinkClass {
Index: sys/sunaudio/gstsunaudiosrc.c
===================================================================
RCS file: /cvs/gstreamer/gst-plugins-good/sys/sunaudio/gstsunaudiosrc.c,v
retrieving revision 1.4
diff -u -r1.4 gstsunaudiosrc.c
--- sys/sunaudio/gstsunaudiosrc.c	8 Dec 2006 15:12:01 -0000	1.4
+++ sys/sunaudio/gstsunaudiosrc.c	8 Dec 2006 23:32:46 -0000
@@ -50,6 +50,9 @@
 
 #include "gstsunaudiosrc.h"
 
+GST_DEBUG_CATEGORY_EXTERN (sunaudio_debug);
+#define GST_CAT_DEFAULT sunaudio_debug
+
 static GstElementDetails plugin_details =
 GST_ELEMENT_DETAILS ("Sun Audio Source",
     "Source/Audio",
