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>
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.
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.
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.
25 * SECTION:element-sunaudiosink
29 * sunaudiosink is an audio sink designed to work with the Sun Audio
30 * interface available in Solaris.
32 * <title>Example launch line</title>
35 * gst-launch -v sinesrc ! sunaudiosink
51 #include "gstsunaudiosink.h"
53 GST_DEBUG_CATEGORY_EXTERN (sunaudio_debug);
54 #define GST_CAT_DEFAULT sunaudio_debug
56 /* elementfactory information */
57 static const GstElementDetails plugin_details =
58 GST_ELEMENT_DETAILS ("Sun Audio Sink",
60 "Audio sink for Sun Audio devices",
61 "David A. Schleef <ds@schleef.org>, "
62 "Brian Cameron <brian.cameron@sun.com>");
64 static void gst_sunaudiosink_base_init (gpointer g_class);
65 static void gst_sunaudiosink_class_init (GstSunAudioSinkClass * klass);
66 static void gst_sunaudiosink_init (GstSunAudioSink * filter);
67 static void gst_sunaudiosink_dispose (GObject * object);
68 static void gst_sunaudiosink_finalize (GObject * object);
70 static void gst_sunaudiosink_set_property (GObject * object, guint prop_id,
71 const GValue * value, GParamSpec * pspec);
72 static void gst_sunaudiosink_get_property (GObject * object, guint prop_id,
73 GValue * value, GParamSpec * pspec);
75 static GstCaps *gst_sunaudiosink_getcaps (GstBaseSink * bsink);
77 static gboolean gst_sunaudiosink_open (GstAudioSink * asink);
78 static gboolean gst_sunaudiosink_close (GstAudioSink * asink);
79 static gboolean gst_sunaudiosink_prepare (GstAudioSink * asink,
80 GstRingBufferSpec * spec);
81 static gboolean gst_sunaudiosink_unprepare (GstAudioSink * asink);
82 static guint gst_sunaudiosink_write (GstAudioSink * asink, gpointer data,
84 static guint gst_sunaudiosink_delay (GstAudioSink * asink);
85 static void gst_sunaudiosink_reset (GstAudioSink * asink);
87 #define DEFAULT_DEVICE "/dev/audio"
94 static GstStaticPadTemplate gst_sunaudiosink_factory =
95 GST_STATIC_PAD_TEMPLATE ("sink",
98 GST_STATIC_CAPS ("audio/x-raw-int, "
99 "endianness = (int) BYTE_ORDER, "
100 "signed = (boolean) TRUE, " "width = (int) 16, " "depth = (int) 16, "
101 /* [5510,48000] seems to be a Solaris limit */
102 "rate = (int) [ 5510, 48000 ], " "channels = (int) [ 1, 2 ]")
105 static GstElementClass *parent_class = NULL;
108 gst_sunaudiosink_get_type (void)
110 static GType plugin_type = 0;
113 static const GTypeInfo plugin_info = {
114 sizeof (GstSunAudioSinkClass),
115 gst_sunaudiosink_base_init,
117 (GClassInitFunc) gst_sunaudiosink_class_init,
120 sizeof (GstSunAudioSink),
122 (GInstanceInitFunc) gst_sunaudiosink_init,
125 plugin_type = g_type_register_static (GST_TYPE_AUDIO_SINK,
126 "GstSunAudioSink", &plugin_info, 0);
132 gst_sunaudiosink_dispose (GObject * object)
134 G_OBJECT_CLASS (parent_class)->dispose (object);
138 gst_sunaudiosink_finalize (GObject * object)
140 GstSunAudioSink *sunaudiosink = GST_SUNAUDIO_SINK (object);
142 g_mutex_free (sunaudiosink->write_mutex);
143 g_cond_free (sunaudiosink->sleep_cond);
145 g_free (sunaudiosink->device);
147 if (sunaudiosink->fd != -1) {
148 close (sunaudiosink->fd);
149 sunaudiosink->fd = -1;
152 G_OBJECT_CLASS (parent_class)->finalize (object);
156 gst_sunaudiosink_base_init (gpointer g_class)
158 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
160 gst_element_class_add_pad_template (element_class,
161 gst_static_pad_template_get (&gst_sunaudiosink_factory));
162 gst_element_class_set_details (element_class, &plugin_details);
166 gst_sunaudiosink_class_init (GstSunAudioSinkClass * klass)
168 GObjectClass *gobject_class;
169 GstElementClass *gstelement_class;
170 GstBaseSinkClass *gstbasesink_class;
171 GstBaseAudioSinkClass *gstbaseaudiosink_class;
172 GstAudioSinkClass *gstaudiosink_class;
174 gobject_class = (GObjectClass *) klass;
175 gstelement_class = (GstElementClass *) klass;
176 gstbasesink_class = (GstBaseSinkClass *) klass;
177 gstbaseaudiosink_class = (GstBaseAudioSinkClass *) klass;
178 gstaudiosink_class = (GstAudioSinkClass *) klass;
180 parent_class = g_type_class_peek_parent (klass);
182 gobject_class->dispose = gst_sunaudiosink_dispose;
183 gobject_class->finalize = gst_sunaudiosink_finalize;
185 gobject_class->set_property =
186 GST_DEBUG_FUNCPTR (gst_sunaudiosink_set_property);
187 gobject_class->get_property =
188 GST_DEBUG_FUNCPTR (gst_sunaudiosink_get_property);
190 gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_sunaudiosink_getcaps);
192 gstaudiosink_class->open = GST_DEBUG_FUNCPTR (gst_sunaudiosink_open);
193 gstaudiosink_class->close = GST_DEBUG_FUNCPTR (gst_sunaudiosink_close);
194 gstaudiosink_class->prepare = GST_DEBUG_FUNCPTR (gst_sunaudiosink_prepare);
195 gstaudiosink_class->unprepare =
196 GST_DEBUG_FUNCPTR (gst_sunaudiosink_unprepare);
197 gstaudiosink_class->write = GST_DEBUG_FUNCPTR (gst_sunaudiosink_write);
198 gstaudiosink_class->delay = GST_DEBUG_FUNCPTR (gst_sunaudiosink_delay);
199 gstaudiosink_class->reset = GST_DEBUG_FUNCPTR (gst_sunaudiosink_reset);
201 g_object_class_install_property (gobject_class, PROP_DEVICE,
202 g_param_spec_string ("device", "Device", "Audio Device (/dev/audio)",
203 DEFAULT_DEVICE, G_PARAM_READWRITE));
207 gst_sunaudiosink_init (GstSunAudioSink * sunaudiosink)
209 GstBaseAudioSink *ba_sink = GST_BASE_AUDIO_SINK (sunaudiosink);
210 const char *audiodev;
212 GST_DEBUG_OBJECT (sunaudiosink, "initializing sunaudiosink");
214 sunaudiosink->fd = -1;
216 audiodev = g_getenv ("AUDIODEV");
217 if (audiodev == NULL)
218 audiodev = DEFAULT_DEVICE;
219 sunaudiosink->device = g_strdup (audiodev);
221 /* mutex and gconf used to control the write method */
222 sunaudiosink->write_mutex = g_mutex_new ();
223 sunaudiosink->sleep_cond = g_cond_new ();
227 gst_sunaudiosink_set_property (GObject * object, guint prop_id,
228 const GValue * value, GParamSpec * pspec)
230 GstSunAudioSink *sunaudiosink;
232 sunaudiosink = GST_SUNAUDIO_SINK (object);
236 GST_OBJECT_LOCK (sunaudiosink);
237 g_free (sunaudiosink->device);
238 sunaudiosink->device = g_strdup (g_value_get_string (value));
239 GST_OBJECT_UNLOCK (sunaudiosink);
242 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
248 gst_sunaudiosink_get_property (GObject * object, guint prop_id,
249 GValue * value, GParamSpec * pspec)
251 GstSunAudioSink *sunaudiosink;
253 sunaudiosink = GST_SUNAUDIO_SINK (object);
257 GST_OBJECT_LOCK (sunaudiosink);
258 g_value_set_string (value, sunaudiosink->device);
259 GST_OBJECT_UNLOCK (sunaudiosink);
262 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
268 gst_sunaudiosink_getcaps (GstBaseSink * bsink)
270 GstPadTemplate *pad_template;
271 GstCaps *caps = NULL;
272 GstSunAudioSink *sunaudiosink = GST_SUNAUDIO_SINK (bsink);
274 GST_DEBUG_OBJECT (sunaudiosink, "getcaps called");
276 pad_template = gst_static_pad_template_get (&gst_sunaudiosink_factory);
277 caps = gst_caps_copy (gst_pad_template_get_caps (pad_template));
279 gst_object_unref (pad_template);
285 gst_sunaudiosink_open (GstAudioSink * asink)
287 GstSunAudioSink *sunaudiosink = GST_SUNAUDIO_SINK (asink);
290 /* First try to open non-blocking */
291 GST_OBJECT_LOCK (sunaudiosink);
292 fd = open (sunaudiosink->device, O_WRONLY | O_NONBLOCK);
296 fd = open (sunaudiosink->device, O_WRONLY);
300 GST_OBJECT_UNLOCK (sunaudiosink);
304 sunaudiosink->fd = fd;
305 GST_OBJECT_UNLOCK (sunaudiosink);
307 ret = ioctl (fd, AUDIO_GETDEV, &sunaudiosink->dev);
311 GST_DEBUG_OBJECT (sunaudiosink, "name %s", sunaudiosink->dev.name);
312 GST_DEBUG_OBJECT (sunaudiosink, "version %s", sunaudiosink->dev.version);
313 GST_DEBUG_OBJECT (sunaudiosink, "config %s", sunaudiosink->dev.config);
315 ret = ioctl (fd, AUDIO_GETINFO, &sunaudiosink->info);
319 GST_DEBUG_OBJECT (sunaudiosink, "monitor_gain %d",
320 sunaudiosink->info.monitor_gain);
321 GST_DEBUG_OBJECT (sunaudiosink, "output_muted %d",
322 sunaudiosink->info.output_muted);
323 GST_DEBUG_OBJECT (sunaudiosink, "hw_features %08x",
324 sunaudiosink->info.hw_features);
325 GST_DEBUG_OBJECT (sunaudiosink, "sw_features %08x",
326 sunaudiosink->info.sw_features);
327 GST_DEBUG_OBJECT (sunaudiosink, "sw_features_enabled %08x",
328 sunaudiosink->info.sw_features_enabled);
333 GST_ELEMENT_ERROR (sunaudiosink, RESOURCE, OPEN_WRITE, (NULL),
334 ("can't open connection to Sun Audio device %s", sunaudiosink->device));
337 close (sunaudiosink->fd);
338 GST_ELEMENT_ERROR (sunaudiosink, RESOURCE, SETTINGS, (NULL), ("%s",
344 gst_sunaudiosink_close (GstAudioSink * asink)
346 GstSunAudioSink *sunaudiosink = GST_SUNAUDIO_SINK (asink);
348 if (sunaudiosink->fd != -1) {
349 close (sunaudiosink->fd);
350 sunaudiosink->fd = -1;
356 gst_sunaudiosink_prepare (GstAudioSink * asink, GstRingBufferSpec * spec)
358 GstSunAudioSink *sunaudiosink = GST_SUNAUDIO_SINK (asink);
363 ret = ioctl (sunaudiosink->fd, AUDIO_GETINFO, &ainfo);
365 GST_ELEMENT_ERROR (sunaudiosink, RESOURCE, SETTINGS, (NULL), ("%s",
370 if (spec->width != 16)
373 ports = ainfo.play.port;
375 AUDIO_INITINFO (&ainfo);
377 ainfo.play.sample_rate = spec->rate;
378 ainfo.play.channels = spec->channels;
379 ainfo.play.precision = spec->width;
380 ainfo.play.encoding = AUDIO_ENCODING_LINEAR;
381 ainfo.play.port = ports;
383 /* buffer_time for playback is not implemented in Solaris at the moment,
384 but at some point in the future, it might be */
385 ainfo.play.buffer_size =
386 gst_util_uint64_scale (spec->rate * spec->bytes_per_sample,
387 spec->buffer_time, GST_SECOND / GST_USECOND);
389 spec->silence_sample[0] = 0;
390 spec->silence_sample[1] = 0;
391 spec->silence_sample[2] = 0;
392 spec->silence_sample[3] = 0;
394 ret = ioctl (sunaudiosink->fd, AUDIO_SETINFO, &ainfo);
396 GST_ELEMENT_ERROR (sunaudiosink, RESOURCE, SETTINGS, (NULL), ("%s",
401 /* Now read back the info to find out the actual buffer size and set
403 AUDIO_INITINFO (&ainfo);
405 ret = ioctl (sunaudiosink->fd, AUDIO_GETINFO, &ainfo);
407 GST_ELEMENT_ERROR (sunaudiosink, RESOURCE, SETTINGS, (NULL), ("%s",
412 /* We don't actually use the buffer_size from the sound device, because
413 * it seems it's just bogus sometimes */
414 sunaudiosink->segtotal = spec->segtotal =
415 ainfo.play.buffer_size / spec->segsize;
417 sunaudiosink->segtotal = spec->segtotal;
419 sunaudiosink->segtotal_samples =
420 spec->segtotal * spec->segsize / spec->bytes_per_sample;
422 sunaudiosink->segs_written = (gint) ainfo.play.eof;
423 sunaudiosink->samples_written = ainfo.play.samples;
424 sunaudiosink->bytes_per_sample = spec->bytes_per_sample;
426 GST_DEBUG_OBJECT (sunaudiosink, "Got device buffer_size of %u",
427 ainfo.play.buffer_size);
433 gst_sunaudiosink_unprepare (GstAudioSink * asink)
438 #define LOOP_WHILE_EINTR(v,func) do { (v) = (func); } \
439 while ((v) == -1 && errno == EINTR);
441 /* Called with the write_mutex held */
443 gst_sunaudio_sink_do_delay (GstSunAudioSink * sink)
445 GstBaseAudioSink *ba_sink = GST_BASE_AUDIO_SINK (sink);
446 GstClockTime total_sleep;
447 GstClockTime max_sleep;
454 /* This code below ensures that we don't race any further than buffer_time
455 * ahead of the audio output, by sleeping if the next write call would cause
456 * us to advance too far in the ring-buffer */
457 LOOP_WHILE_EINTR (err, ioctl (sink->fd, AUDIO_GETINFO, &ainfo));
461 /* Compute our offset from the output (copes with overflow) */
462 diff = (guint) (sink->segs_written) - ainfo.play.eof;
463 if (diff > sink->segtotal) {
464 /* This implies that reset did a flush just as the sound device aquired
465 * some buffers internally, and it causes us to be out of sync with the
466 * eof measure. This corrects it */
467 sink->segs_written = ainfo.play.eof;
471 if (diff + 1 < sink->segtotal)
472 return; /* no need to sleep at all */
474 /* Never sleep longer than the initial number of undrained segments in the
477 max_sleep = (diff + 1) * (ba_sink->latency_time * GST_USECOND);
478 /* sleep for a segment period between .eof polls */
479 sleep_usecs = ba_sink->latency_time;
481 /* Current time is our reference point */
482 g_get_current_time (&sleep_end);
484 /* If the next segment would take us too far along the ring buffer,
485 * sleep for a bit to free up a slot. If there were a way to find out
486 * when the eof field actually increments, we could use, but the only
487 * notification mechanism seems to be SIGPOLL, which we can't use from
488 * a support library */
489 while (diff + 1 >= sink->segtotal && total_sleep < max_sleep) {
490 GST_LOG_OBJECT (sink, "need to block to drain segment(s). "
491 "Sleeping for %d us", sleep_usecs);
493 g_time_val_add (&sleep_end, sleep_usecs);
495 if (g_cond_timed_wait (sink->sleep_cond, sink->write_mutex, &sleep_end)) {
496 GST_LOG_OBJECT (sink, "Waking up early due to reset");
497 return; /* Got told to wake up */
499 total_sleep += (sleep_usecs * GST_USECOND);
501 LOOP_WHILE_EINTR (err, ioctl (sink->fd, AUDIO_GETINFO, &ainfo));
505 /* Compute our (new) offset from the output (copes with overflow) */
506 diff = (guint) g_atomic_int_get (&sink->segs_written) - ainfo.play.eof;
512 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
513 ("Playback error on device '%s': %s", sink->device, strerror (errno)));
516 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
517 ("Playback error on device '%s': %s", sink->device, strerror (errno)));
522 gst_sunaudiosink_write (GstAudioSink * asink, gpointer data, guint length)
524 GstSunAudioSink *sink = GST_SUNAUDIO_SINK (asink);
526 gint bytes_written, err;
528 g_mutex_lock (sink->write_mutex);
529 if (sink->flushing) {
530 /* Exit immediately if reset tells us to */
531 g_mutex_unlock (sink->write_mutex);
535 LOOP_WHILE_EINTR (bytes_written, write (sink->fd, data, length));
536 if (bytes_written < 0) {
541 /* Increment our sample counter, for delay calcs */
542 g_atomic_int_add (&sink->samples_written, length / sink->bytes_per_sample);
544 /* Don't consider the segment written if we didn't output the whole lot yet */
545 if (bytes_written < length) {
546 g_mutex_unlock (sink->write_mutex);
547 return (guint) bytes_written;
550 /* Write a zero length output to trigger increment of the eof field */
551 LOOP_WHILE_EINTR (err, write (sink->fd, NULL, 0));
555 /* Count this extra segment we've written */
556 sink->segs_written += 1;
558 /* Now delay so we don't overrun the ring buffer */
559 gst_sunaudio_sink_do_delay (sink);
561 g_mutex_unlock (sink->write_mutex);
565 g_mutex_unlock (sink->write_mutex);
567 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
568 ("Playback error on device '%s': %s", sink->device, strerror (errno)));
569 return length; /* Say we wrote the segment to let the ringbuffer exit */
573 * Provide the current number of unplayed samples that have been written
576 gst_sunaudiosink_delay (GstAudioSink * asink)
578 GstSunAudioSink *sink = GST_SUNAUDIO_SINK (asink);
583 ret = ioctl (sink->fd, AUDIO_GETINFO, &ainfo);
584 if (G_UNLIKELY (ret == -1))
587 offset = (g_atomic_int_get (&sink->samples_written) - ainfo.play.samples);
589 /* If the offset is larger than the total ringbuffer size, then we asked
590 between the write call and when samples_written is updated */
591 if (G_UNLIKELY (offset > sink->segtotal_samples))
598 gst_sunaudiosink_reset (GstAudioSink * asink)
600 /* Get current values */
601 GstSunAudioSink *sunaudiosink = GST_SUNAUDIO_SINK (asink);
605 ret = ioctl (sunaudiosink->fd, AUDIO_GETINFO, &ainfo);
608 * Should never happen, but if we couldn't getinfo, then no point
611 GST_ELEMENT_ERROR (sunaudiosink, RESOURCE, SETTINGS, (NULL), ("%s",
617 * Pause the audio - so audio stops playing immediately rather than
618 * waiting for the ringbuffer to empty.
620 ainfo.play.pause = !NULL;
621 ret = ioctl (sunaudiosink->fd, AUDIO_SETINFO, &ainfo);
623 GST_ELEMENT_ERROR (sunaudiosink, RESOURCE, SETTINGS, (NULL), ("%s",
627 /* Flush the audio */
628 ret = ioctl (sunaudiosink->fd, I_FLUSH, FLUSHW);
630 GST_ELEMENT_ERROR (sunaudiosink, RESOURCE, SETTINGS, (NULL), ("%s",
634 /* Now, we take the write_mutex and signal to ensure the write thread
635 * is not busy, and we signal the condition to wake up any sleeper,
636 * then we flush again in case the write wrote something after we flushed,
637 * and finally release the lock and unpause */
638 g_mutex_lock (sunaudiosink->write_mutex);
639 sunaudiosink->flushing = TRUE;
641 g_cond_signal (sunaudiosink->sleep_cond);
643 ret = ioctl (sunaudiosink->fd, I_FLUSH, FLUSHW);
645 GST_ELEMENT_ERROR (sunaudiosink, RESOURCE, SETTINGS, (NULL), ("%s",
649 /* unpause the audio */
650 ainfo.play.pause = NULL;
651 ret = ioctl (sunaudiosink->fd, AUDIO_SETINFO, &ainfo);
653 GST_ELEMENT_ERROR (sunaudiosink, RESOURCE, SETTINGS, (NULL), ("%s",
657 /* After flushing the audio device, we need to remeasure the sample count
658 * and segments written count so we're in sync with the device */
660 sunaudiosink->segs_written = ainfo.play.eof;
661 g_atomic_int_set (&sunaudiosink->samples_written, ainfo.play.samples);
663 sunaudiosink->flushing = FALSE;
664 g_mutex_unlock (sunaudiosink->write_mutex);