Update and add documentation for platform specific plugins (sys).
[platform/upstream/gst-plugins-good.git] / sys / sunaudio / gstsunaudiosink.c
1 /*
2  * GStreamer - SunAudio sink
3  * Copyright (C) 2004 David A. Schleef <ds@schleef.org>
4  * Copyright (C) 2005,2006 Sun Microsystems, Inc.,
5  *               Brian Cameron <brian.cameron@sun.com>
6  * Copyright (C) 2006 Jan Schmidt <thaytan@mad.scientist.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 /**
25  * SECTION:element-sunaudiosink
26  *
27  * sunaudiosink is an audio sink designed to work with the Sun Audio
28  * interface available in Solaris.
29  *
30  * <refsect2>
31  * <title>Example launch line</title>
32  * |[
33  * gst-launch audiotestsrc volume=0.5 ! sunaudiosink
34  * ]|
35  * </refsect2>
36  */
37
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41
42 #include <fcntl.h>
43 #include <string.h>
44 #include <stropts.h>
45 #include <unistd.h>
46 #include <sys/mman.h>
47
48 #include "gstsunaudiosink.h"
49
50 GST_DEBUG_CATEGORY_EXTERN (sunaudio_debug);
51 #define GST_CAT_DEFAULT sunaudio_debug
52
53 /* elementfactory information */
54 static const GstElementDetails plugin_details =
55 GST_ELEMENT_DETAILS ("Sun Audio Sink",
56     "Sink/Audio",
57     "Audio sink for Sun Audio devices",
58     "David A. Schleef <ds@schleef.org>, "
59     "Brian Cameron <brian.cameron@sun.com>");
60
61 static void gst_sunaudiosink_base_init (gpointer g_class);
62 static void gst_sunaudiosink_class_init (GstSunAudioSinkClass * klass);
63 static void gst_sunaudiosink_init (GstSunAudioSink * filter);
64 static void gst_sunaudiosink_dispose (GObject * object);
65 static void gst_sunaudiosink_finalize (GObject * object);
66
67 static void gst_sunaudiosink_set_property (GObject * object, guint prop_id,
68     const GValue * value, GParamSpec * pspec);
69 static void gst_sunaudiosink_get_property (GObject * object, guint prop_id,
70     GValue * value, GParamSpec * pspec);
71
72 static GstCaps *gst_sunaudiosink_getcaps (GstBaseSink * bsink);
73
74 static gboolean gst_sunaudiosink_open (GstAudioSink * asink);
75 static gboolean gst_sunaudiosink_close (GstAudioSink * asink);
76 static gboolean gst_sunaudiosink_prepare (GstAudioSink * asink,
77     GstRingBufferSpec * spec);
78 static gboolean gst_sunaudiosink_unprepare (GstAudioSink * asink);
79 static guint gst_sunaudiosink_write (GstAudioSink * asink, gpointer data,
80     guint length);
81 static guint gst_sunaudiosink_delay (GstAudioSink * asink);
82 static void gst_sunaudiosink_reset (GstAudioSink * asink);
83
84 #define DEFAULT_DEVICE  "/dev/audio"
85 enum
86 {
87   PROP_0,
88   PROP_DEVICE,
89 };
90
91 static GstStaticPadTemplate gst_sunaudiosink_factory =
92 GST_STATIC_PAD_TEMPLATE ("sink",
93     GST_PAD_SINK,
94     GST_PAD_ALWAYS,
95     GST_STATIC_CAPS ("audio/x-raw-int, "
96         "endianness = (int) BYTE_ORDER, "
97         "signed = (boolean) TRUE, " "width = (int) 16, " "depth = (int) 16, "
98         /* [5510,48000] seems to be a Solaris limit */
99         "rate = (int) [ 5510, 48000 ], " "channels = (int) [ 1, 2 ]")
100     );
101
102 static GstElementClass *parent_class = NULL;
103
104 GType
105 gst_sunaudiosink_get_type (void)
106 {
107   static GType plugin_type = 0;
108
109   if (!plugin_type) {
110     static const GTypeInfo plugin_info = {
111       sizeof (GstSunAudioSinkClass),
112       gst_sunaudiosink_base_init,
113       NULL,
114       (GClassInitFunc) gst_sunaudiosink_class_init,
115       NULL,
116       NULL,
117       sizeof (GstSunAudioSink),
118       0,
119       (GInstanceInitFunc) gst_sunaudiosink_init,
120     };
121
122     plugin_type = g_type_register_static (GST_TYPE_AUDIO_SINK,
123         "GstSunAudioSink", &plugin_info, 0);
124   }
125   return plugin_type;
126 }
127
128 static void
129 gst_sunaudiosink_dispose (GObject * object)
130 {
131   G_OBJECT_CLASS (parent_class)->dispose (object);
132 }
133
134 static void
135 gst_sunaudiosink_finalize (GObject * object)
136 {
137   GstSunAudioSink *sunaudiosink = GST_SUNAUDIO_SINK (object);
138
139   g_mutex_free (sunaudiosink->write_mutex);
140   g_cond_free (sunaudiosink->sleep_cond);
141
142   g_free (sunaudiosink->device);
143
144   if (sunaudiosink->fd != -1) {
145     close (sunaudiosink->fd);
146     sunaudiosink->fd = -1;
147   }
148
149   G_OBJECT_CLASS (parent_class)->finalize (object);
150 }
151
152 static void
153 gst_sunaudiosink_base_init (gpointer g_class)
154 {
155   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
156
157   gst_element_class_add_pad_template (element_class,
158       gst_static_pad_template_get (&gst_sunaudiosink_factory));
159   gst_element_class_set_details (element_class, &plugin_details);
160 }
161
162 static void
163 gst_sunaudiosink_class_init (GstSunAudioSinkClass * klass)
164 {
165   GObjectClass *gobject_class;
166   GstElementClass *gstelement_class;
167   GstBaseSinkClass *gstbasesink_class;
168   GstBaseAudioSinkClass *gstbaseaudiosink_class;
169   GstAudioSinkClass *gstaudiosink_class;
170
171   gobject_class = (GObjectClass *) klass;
172   gstelement_class = (GstElementClass *) klass;
173   gstbasesink_class = (GstBaseSinkClass *) klass;
174   gstbaseaudiosink_class = (GstBaseAudioSinkClass *) klass;
175   gstaudiosink_class = (GstAudioSinkClass *) klass;
176
177   parent_class = g_type_class_peek_parent (klass);
178
179   gobject_class->dispose = gst_sunaudiosink_dispose;
180   gobject_class->finalize = gst_sunaudiosink_finalize;
181
182   gobject_class->set_property =
183       GST_DEBUG_FUNCPTR (gst_sunaudiosink_set_property);
184   gobject_class->get_property =
185       GST_DEBUG_FUNCPTR (gst_sunaudiosink_get_property);
186
187   gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_sunaudiosink_getcaps);
188
189   gstaudiosink_class->open = GST_DEBUG_FUNCPTR (gst_sunaudiosink_open);
190   gstaudiosink_class->close = GST_DEBUG_FUNCPTR (gst_sunaudiosink_close);
191   gstaudiosink_class->prepare = GST_DEBUG_FUNCPTR (gst_sunaudiosink_prepare);
192   gstaudiosink_class->unprepare =
193       GST_DEBUG_FUNCPTR (gst_sunaudiosink_unprepare);
194   gstaudiosink_class->write = GST_DEBUG_FUNCPTR (gst_sunaudiosink_write);
195   gstaudiosink_class->delay = GST_DEBUG_FUNCPTR (gst_sunaudiosink_delay);
196   gstaudiosink_class->reset = GST_DEBUG_FUNCPTR (gst_sunaudiosink_reset);
197
198   g_object_class_install_property (gobject_class, PROP_DEVICE,
199       g_param_spec_string ("device", "Device", "Audio Device (/dev/audio)",
200           DEFAULT_DEVICE, G_PARAM_READWRITE));
201 }
202
203 static void
204 gst_sunaudiosink_init (GstSunAudioSink * sunaudiosink)
205 {
206   GstBaseAudioSink *ba_sink = GST_BASE_AUDIO_SINK (sunaudiosink);
207   const char *audiodev;
208
209   GST_DEBUG_OBJECT (sunaudiosink, "initializing sunaudiosink");
210
211   sunaudiosink->fd = -1;
212
213   audiodev = g_getenv ("AUDIODEV");
214   if (audiodev == NULL)
215     audiodev = DEFAULT_DEVICE;
216   sunaudiosink->device = g_strdup (audiodev);
217
218   /* mutex and gconf used to control the write method */
219   sunaudiosink->write_mutex = g_mutex_new ();
220   sunaudiosink->sleep_cond = g_cond_new ();
221 }
222
223 static void
224 gst_sunaudiosink_set_property (GObject * object, guint prop_id,
225     const GValue * value, GParamSpec * pspec)
226 {
227   GstSunAudioSink *sunaudiosink;
228
229   sunaudiosink = GST_SUNAUDIO_SINK (object);
230
231   switch (prop_id) {
232     case PROP_DEVICE:
233       GST_OBJECT_LOCK (sunaudiosink);
234       g_free (sunaudiosink->device);
235       sunaudiosink->device = g_strdup (g_value_get_string (value));
236       GST_OBJECT_UNLOCK (sunaudiosink);
237       break;
238     default:
239       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
240       break;
241   }
242 }
243
244 static void
245 gst_sunaudiosink_get_property (GObject * object, guint prop_id,
246     GValue * value, GParamSpec * pspec)
247 {
248   GstSunAudioSink *sunaudiosink;
249
250   sunaudiosink = GST_SUNAUDIO_SINK (object);
251
252   switch (prop_id) {
253     case PROP_DEVICE:
254       GST_OBJECT_LOCK (sunaudiosink);
255       g_value_set_string (value, sunaudiosink->device);
256       GST_OBJECT_UNLOCK (sunaudiosink);
257       break;
258     default:
259       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
260       break;
261   }
262 }
263
264 static GstCaps *
265 gst_sunaudiosink_getcaps (GstBaseSink * bsink)
266 {
267   GstPadTemplate *pad_template;
268   GstCaps *caps = NULL;
269   GstSunAudioSink *sunaudiosink = GST_SUNAUDIO_SINK (bsink);
270
271   GST_DEBUG_OBJECT (sunaudiosink, "getcaps called");
272
273   pad_template = gst_static_pad_template_get (&gst_sunaudiosink_factory);
274   caps = gst_caps_copy (gst_pad_template_get_caps (pad_template));
275
276   gst_object_unref (pad_template);
277
278   return caps;
279 }
280
281 static gboolean
282 gst_sunaudiosink_open (GstAudioSink * asink)
283 {
284   GstSunAudioSink *sunaudiosink = GST_SUNAUDIO_SINK (asink);
285   int fd, ret;
286
287   /* First try to open non-blocking */
288   GST_OBJECT_LOCK (sunaudiosink);
289   fd = open (sunaudiosink->device, O_WRONLY | O_NONBLOCK);
290
291   if (fd >= 0) {
292     close (fd);
293     fd = open (sunaudiosink->device, O_WRONLY);
294   }
295
296   if (fd == -1) {
297     GST_OBJECT_UNLOCK (sunaudiosink);
298     goto open_failed;
299   }
300
301   sunaudiosink->fd = fd;
302   GST_OBJECT_UNLOCK (sunaudiosink);
303
304   ret = ioctl (fd, AUDIO_GETDEV, &sunaudiosink->dev);
305   if (ret == -1)
306     goto ioctl_error;
307
308   GST_DEBUG_OBJECT (sunaudiosink, "name %s", sunaudiosink->dev.name);
309   GST_DEBUG_OBJECT (sunaudiosink, "version %s", sunaudiosink->dev.version);
310   GST_DEBUG_OBJECT (sunaudiosink, "config %s", sunaudiosink->dev.config);
311
312   ret = ioctl (fd, AUDIO_GETINFO, &sunaudiosink->info);
313   if (ret == -1)
314     goto ioctl_error;
315
316   GST_DEBUG_OBJECT (sunaudiosink, "monitor_gain %d",
317       sunaudiosink->info.monitor_gain);
318   GST_DEBUG_OBJECT (sunaudiosink, "output_muted %d",
319       sunaudiosink->info.output_muted);
320   GST_DEBUG_OBJECT (sunaudiosink, "hw_features %08x",
321       sunaudiosink->info.hw_features);
322   GST_DEBUG_OBJECT (sunaudiosink, "sw_features %08x",
323       sunaudiosink->info.sw_features);
324   GST_DEBUG_OBJECT (sunaudiosink, "sw_features_enabled %08x",
325       sunaudiosink->info.sw_features_enabled);
326
327   return TRUE;
328
329 open_failed:
330   GST_ELEMENT_ERROR (sunaudiosink, RESOURCE, OPEN_WRITE, (NULL),
331       ("can't open connection to Sun Audio device %s", sunaudiosink->device));
332   return FALSE;
333 ioctl_error:
334   close (sunaudiosink->fd);
335   GST_ELEMENT_ERROR (sunaudiosink, RESOURCE, SETTINGS, (NULL), ("%s",
336           strerror (errno)));
337   return FALSE;
338 }
339
340 static gboolean
341 gst_sunaudiosink_close (GstAudioSink * asink)
342 {
343   GstSunAudioSink *sunaudiosink = GST_SUNAUDIO_SINK (asink);
344
345   if (sunaudiosink->fd != -1) {
346     close (sunaudiosink->fd);
347     sunaudiosink->fd = -1;
348   }
349   return TRUE;
350 }
351
352 static gboolean
353 gst_sunaudiosink_prepare (GstAudioSink * asink, GstRingBufferSpec * spec)
354 {
355   GstSunAudioSink *sunaudiosink = GST_SUNAUDIO_SINK (asink);
356   audio_info_t ainfo;
357   int ret;
358   int ports;
359
360   ret = ioctl (sunaudiosink->fd, AUDIO_GETINFO, &ainfo);
361   if (ret == -1) {
362     GST_ELEMENT_ERROR (sunaudiosink, RESOURCE, SETTINGS, (NULL), ("%s",
363             strerror (errno)));
364     return FALSE;
365   }
366
367   if (spec->width != 16)
368     return FALSE;
369
370   ports = ainfo.play.port;
371
372   AUDIO_INITINFO (&ainfo);
373
374   ainfo.play.sample_rate = spec->rate;
375   ainfo.play.channels = spec->channels;
376   ainfo.play.precision = spec->width;
377   ainfo.play.encoding = AUDIO_ENCODING_LINEAR;
378   ainfo.play.port = ports;
379
380   /* buffer_time for playback is not implemented in Solaris at the moment,
381      but at some point in the future, it might be */
382   ainfo.play.buffer_size =
383       gst_util_uint64_scale (spec->rate * spec->bytes_per_sample,
384       spec->buffer_time, GST_SECOND / GST_USECOND);
385
386   spec->silence_sample[0] = 0;
387   spec->silence_sample[1] = 0;
388   spec->silence_sample[2] = 0;
389   spec->silence_sample[3] = 0;
390
391   ret = ioctl (sunaudiosink->fd, AUDIO_SETINFO, &ainfo);
392   if (ret == -1) {
393     GST_ELEMENT_ERROR (sunaudiosink, RESOURCE, SETTINGS, (NULL), ("%s",
394             strerror (errno)));
395     return FALSE;
396   }
397
398   /* Now read back the info to find out the actual buffer size and set 
399      segtotal */
400   AUDIO_INITINFO (&ainfo);
401
402   ret = ioctl (sunaudiosink->fd, AUDIO_GETINFO, &ainfo);
403   if (ret == -1) {
404     GST_ELEMENT_ERROR (sunaudiosink, RESOURCE, SETTINGS, (NULL), ("%s",
405             strerror (errno)));
406     return FALSE;
407   }
408 #if 0
409   /* We don't actually use the buffer_size from the sound device, because
410    * it seems it's just bogus sometimes */
411   sunaudiosink->segtotal = spec->segtotal =
412       ainfo.play.buffer_size / spec->segsize;
413 #else
414   sunaudiosink->segtotal = spec->segtotal;
415 #endif
416   sunaudiosink->segtotal_samples =
417       spec->segtotal * spec->segsize / spec->bytes_per_sample;
418
419   sunaudiosink->segs_written = (gint) ainfo.play.eof;
420   sunaudiosink->samples_written = ainfo.play.samples;
421   sunaudiosink->bytes_per_sample = spec->bytes_per_sample;
422
423   GST_DEBUG_OBJECT (sunaudiosink, "Got device buffer_size of %u",
424       ainfo.play.buffer_size);
425
426   return TRUE;
427 }
428
429 static gboolean
430 gst_sunaudiosink_unprepare (GstAudioSink * asink)
431 {
432   return TRUE;
433 }
434
435 #define LOOP_WHILE_EINTR(v,func) do { (v) = (func); } \
436                 while ((v) == -1 && errno == EINTR);
437
438 /* Called with the write_mutex held */
439 static void
440 gst_sunaudio_sink_do_delay (GstSunAudioSink * sink)
441 {
442   GstBaseAudioSink *ba_sink = GST_BASE_AUDIO_SINK (sink);
443   GstClockTime total_sleep;
444   GstClockTime max_sleep;
445   gint sleep_usecs;
446   GTimeVal sleep_end;
447   gint err;
448   audio_info_t ainfo;
449   guint diff;
450
451   /* This code below ensures that we don't race any further than buffer_time 
452    * ahead of the audio output, by sleeping if the next write call would cause
453    * us to advance too far in the ring-buffer */
454   LOOP_WHILE_EINTR (err, ioctl (sink->fd, AUDIO_GETINFO, &ainfo));
455   if (err < 0)
456     goto write_error;
457
458   /* Compute our offset from the output (copes with overflow) */
459   diff = (guint) (sink->segs_written) - ainfo.play.eof;
460   if (diff > sink->segtotal) {
461     /* This implies that reset did a flush just as the sound device aquired
462      * some buffers internally, and it causes us to be out of sync with the
463      * eof measure. This corrects it */
464     sink->segs_written = ainfo.play.eof;
465     diff = 0;
466   }
467
468   if (diff + 1 < sink->segtotal)
469     return;                     /* no need to sleep at all */
470
471   /* Never sleep longer than the initial number of undrained segments in the 
472      device plus one */
473   total_sleep = 0;
474   max_sleep = (diff + 1) * (ba_sink->latency_time * GST_USECOND);
475   /* sleep for a segment period between .eof polls */
476   sleep_usecs = ba_sink->latency_time;
477
478   /* Current time is our reference point */
479   g_get_current_time (&sleep_end);
480
481   /* If the next segment would take us too far along the ring buffer,
482    * sleep for a bit to free up a slot. If there were a way to find out
483    * when the eof field actually increments, we could use, but the only
484    * notification mechanism seems to be SIGPOLL, which we can't use from
485    * a support library */
486   while (diff + 1 >= sink->segtotal && total_sleep < max_sleep) {
487     GST_LOG_OBJECT (sink, "need to block to drain segment(s). "
488         "Sleeping for %d us", sleep_usecs);
489
490     g_time_val_add (&sleep_end, sleep_usecs);
491
492     if (g_cond_timed_wait (sink->sleep_cond, sink->write_mutex, &sleep_end)) {
493       GST_LOG_OBJECT (sink, "Waking up early due to reset");
494       return;                   /* Got told to wake up */
495     }
496     total_sleep += (sleep_usecs * GST_USECOND);
497
498     LOOP_WHILE_EINTR (err, ioctl (sink->fd, AUDIO_GETINFO, &ainfo));
499     if (err < 0)
500       goto write_error;
501
502     /* Compute our (new) offset from the output (copes with overflow) */
503     diff = (guint) g_atomic_int_get (&sink->segs_written) - ainfo.play.eof;
504   }
505
506   return;
507
508 write_error:
509   GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
510       ("Playback error on device '%s': %s", sink->device, strerror (errno)));
511   return;
512 poll_failed:
513   GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
514       ("Playback error on device '%s': %s", sink->device, strerror (errno)));
515   return;
516 }
517
518 static guint
519 gst_sunaudiosink_write (GstAudioSink * asink, gpointer data, guint length)
520 {
521   GstSunAudioSink *sink = GST_SUNAUDIO_SINK (asink);
522
523   gint bytes_written, err;
524
525   g_mutex_lock (sink->write_mutex);
526   if (sink->flushing) {
527     /* Exit immediately if reset tells us to */
528     g_mutex_unlock (sink->write_mutex);
529     return length;
530   }
531
532   LOOP_WHILE_EINTR (bytes_written, write (sink->fd, data, length));
533   if (bytes_written < 0) {
534     err = bytes_written;
535     goto write_error;
536   }
537
538   /* Increment our sample counter, for delay calcs */
539   g_atomic_int_add (&sink->samples_written, length / sink->bytes_per_sample);
540
541   /* Don't consider the segment written if we didn't output the whole lot yet */
542   if (bytes_written < length) {
543     g_mutex_unlock (sink->write_mutex);
544     return (guint) bytes_written;
545   }
546
547   /* Write a zero length output to trigger increment of the eof field */
548   LOOP_WHILE_EINTR (err, write (sink->fd, NULL, 0));
549   if (err < 0)
550     goto write_error;
551
552   /* Count this extra segment we've written */
553   sink->segs_written += 1;
554
555   /* Now delay so we don't overrun the ring buffer */
556   gst_sunaudio_sink_do_delay (sink);
557
558   g_mutex_unlock (sink->write_mutex);
559   return length;
560
561 write_error:
562   g_mutex_unlock (sink->write_mutex);
563
564   GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
565       ("Playback error on device '%s': %s", sink->device, strerror (errno)));
566   return length;                /* Say we wrote the segment to let the ringbuffer exit */
567 }
568
569 /*
570  * Provide the current number of unplayed samples that have been written
571  * to the device */
572 static guint
573 gst_sunaudiosink_delay (GstAudioSink * asink)
574 {
575   GstSunAudioSink *sink = GST_SUNAUDIO_SINK (asink);
576   audio_info_t ainfo;
577   gint ret;
578   guint offset;
579
580   ret = ioctl (sink->fd, AUDIO_GETINFO, &ainfo);
581   if (G_UNLIKELY (ret == -1))
582     return 0;
583
584   offset = (g_atomic_int_get (&sink->samples_written) - ainfo.play.samples);
585
586   /* If the offset is larger than the total ringbuffer size, then we asked
587      between the write call and when samples_written is updated */
588   if (G_UNLIKELY (offset > sink->segtotal_samples))
589     return 0;
590
591   return offset;
592 }
593
594 static void
595 gst_sunaudiosink_reset (GstAudioSink * asink)
596 {
597   /* Get current values */
598   GstSunAudioSink *sunaudiosink = GST_SUNAUDIO_SINK (asink);
599   audio_info_t ainfo;
600   int ret;
601
602   ret = ioctl (sunaudiosink->fd, AUDIO_GETINFO, &ainfo);
603   if (ret == -1) {
604     /*
605      * Should never happen, but if we couldn't getinfo, then no point
606      * trying to setinfo
607      */
608     GST_ELEMENT_ERROR (sunaudiosink, RESOURCE, SETTINGS, (NULL), ("%s",
609             strerror (errno)));
610     return;
611   }
612
613   /*
614    * Pause the audio - so audio stops playing immediately rather than
615    * waiting for the ringbuffer to empty.
616    */
617   ainfo.play.pause = !NULL;
618   ret = ioctl (sunaudiosink->fd, AUDIO_SETINFO, &ainfo);
619   if (ret == -1) {
620     GST_ELEMENT_ERROR (sunaudiosink, RESOURCE, SETTINGS, (NULL), ("%s",
621             strerror (errno)));
622   }
623
624   /* Flush the audio */
625   ret = ioctl (sunaudiosink->fd, I_FLUSH, FLUSHW);
626   if (ret == -1) {
627     GST_ELEMENT_ERROR (sunaudiosink, RESOURCE, SETTINGS, (NULL), ("%s",
628             strerror (errno)));
629   }
630
631   /* Now, we take the write_mutex and signal to ensure the write thread
632    * is not busy, and we signal the condition to wake up any sleeper,
633    * then we flush again in case the write wrote something after we flushed,
634    * and finally release the lock and unpause */
635   g_mutex_lock (sunaudiosink->write_mutex);
636   sunaudiosink->flushing = TRUE;
637
638   g_cond_signal (sunaudiosink->sleep_cond);
639
640   ret = ioctl (sunaudiosink->fd, I_FLUSH, FLUSHW);
641   if (ret == -1) {
642     GST_ELEMENT_ERROR (sunaudiosink, RESOURCE, SETTINGS, (NULL), ("%s",
643             strerror (errno)));
644   }
645
646   /* unpause the audio */
647   ainfo.play.pause = NULL;
648   ret = ioctl (sunaudiosink->fd, AUDIO_SETINFO, &ainfo);
649   if (ret == -1) {
650     GST_ELEMENT_ERROR (sunaudiosink, RESOURCE, SETTINGS, (NULL), ("%s",
651             strerror (errno)));
652   }
653
654   /* After flushing the audio device, we need to remeasure the sample count
655    * and segments written count so we're in sync with the device */
656
657   sunaudiosink->segs_written = ainfo.play.eof;
658   g_atomic_int_set (&sunaudiosink->samples_written, ainfo.play.samples);
659
660   sunaudiosink->flushing = FALSE;
661   g_mutex_unlock (sunaudiosink->write_mutex);
662 }