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
27 * sunaudiosink is an audio sink designed to work with the Sun Audio
28 * interface available in Solaris.
31 * <title>Example launch line</title>
33 * gst-launch audiotestsrc volume=0.5 ! sunaudiosink
48 #include "gstsunaudiosink.h"
50 GST_DEBUG_CATEGORY_EXTERN (sunaudio_debug);
51 #define GST_CAT_DEFAULT sunaudio_debug
53 /* elementfactory information */
54 static const GstElementDetails plugin_details =
55 GST_ELEMENT_DETAILS ("Sun Audio Sink",
57 "Audio sink for Sun Audio devices",
58 "David A. Schleef <ds@schleef.org>, "
59 "Brian Cameron <brian.cameron@sun.com>");
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);
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);
72 static GstCaps *gst_sunaudiosink_getcaps (GstBaseSink * bsink);
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,
81 static guint gst_sunaudiosink_delay (GstAudioSink * asink);
82 static void gst_sunaudiosink_reset (GstAudioSink * asink);
84 #define DEFAULT_DEVICE "/dev/audio"
91 static GstStaticPadTemplate gst_sunaudiosink_factory =
92 GST_STATIC_PAD_TEMPLATE ("sink",
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 ]")
102 static GstElementClass *parent_class = NULL;
105 gst_sunaudiosink_get_type (void)
107 static GType plugin_type = 0;
110 static const GTypeInfo plugin_info = {
111 sizeof (GstSunAudioSinkClass),
112 gst_sunaudiosink_base_init,
114 (GClassInitFunc) gst_sunaudiosink_class_init,
117 sizeof (GstSunAudioSink),
119 (GInstanceInitFunc) gst_sunaudiosink_init,
122 plugin_type = g_type_register_static (GST_TYPE_AUDIO_SINK,
123 "GstSunAudioSink", &plugin_info, 0);
129 gst_sunaudiosink_dispose (GObject * object)
131 G_OBJECT_CLASS (parent_class)->dispose (object);
135 gst_sunaudiosink_finalize (GObject * object)
137 GstSunAudioSink *sunaudiosink = GST_SUNAUDIO_SINK (object);
139 g_mutex_free (sunaudiosink->write_mutex);
140 g_cond_free (sunaudiosink->sleep_cond);
142 g_free (sunaudiosink->device);
144 if (sunaudiosink->fd != -1) {
145 close (sunaudiosink->fd);
146 sunaudiosink->fd = -1;
149 G_OBJECT_CLASS (parent_class)->finalize (object);
153 gst_sunaudiosink_base_init (gpointer g_class)
155 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
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);
163 gst_sunaudiosink_class_init (GstSunAudioSinkClass * klass)
165 GObjectClass *gobject_class;
166 GstElementClass *gstelement_class;
167 GstBaseSinkClass *gstbasesink_class;
168 GstBaseAudioSinkClass *gstbaseaudiosink_class;
169 GstAudioSinkClass *gstaudiosink_class;
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;
177 parent_class = g_type_class_peek_parent (klass);
179 gobject_class->dispose = gst_sunaudiosink_dispose;
180 gobject_class->finalize = gst_sunaudiosink_finalize;
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);
187 gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_sunaudiosink_getcaps);
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);
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));
204 gst_sunaudiosink_init (GstSunAudioSink * sunaudiosink)
206 GstBaseAudioSink *ba_sink = GST_BASE_AUDIO_SINK (sunaudiosink);
207 const char *audiodev;
209 GST_DEBUG_OBJECT (sunaudiosink, "initializing sunaudiosink");
211 sunaudiosink->fd = -1;
213 audiodev = g_getenv ("AUDIODEV");
214 if (audiodev == NULL)
215 audiodev = DEFAULT_DEVICE;
216 sunaudiosink->device = g_strdup (audiodev);
218 /* mutex and gconf used to control the write method */
219 sunaudiosink->write_mutex = g_mutex_new ();
220 sunaudiosink->sleep_cond = g_cond_new ();
224 gst_sunaudiosink_set_property (GObject * object, guint prop_id,
225 const GValue * value, GParamSpec * pspec)
227 GstSunAudioSink *sunaudiosink;
229 sunaudiosink = GST_SUNAUDIO_SINK (object);
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);
239 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
245 gst_sunaudiosink_get_property (GObject * object, guint prop_id,
246 GValue * value, GParamSpec * pspec)
248 GstSunAudioSink *sunaudiosink;
250 sunaudiosink = GST_SUNAUDIO_SINK (object);
254 GST_OBJECT_LOCK (sunaudiosink);
255 g_value_set_string (value, sunaudiosink->device);
256 GST_OBJECT_UNLOCK (sunaudiosink);
259 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
265 gst_sunaudiosink_getcaps (GstBaseSink * bsink)
267 GstPadTemplate *pad_template;
268 GstCaps *caps = NULL;
269 GstSunAudioSink *sunaudiosink = GST_SUNAUDIO_SINK (bsink);
271 GST_DEBUG_OBJECT (sunaudiosink, "getcaps called");
273 pad_template = gst_static_pad_template_get (&gst_sunaudiosink_factory);
274 caps = gst_caps_copy (gst_pad_template_get_caps (pad_template));
276 gst_object_unref (pad_template);
282 gst_sunaudiosink_open (GstAudioSink * asink)
284 GstSunAudioSink *sunaudiosink = GST_SUNAUDIO_SINK (asink);
287 /* First try to open non-blocking */
288 GST_OBJECT_LOCK (sunaudiosink);
289 fd = open (sunaudiosink->device, O_WRONLY | O_NONBLOCK);
293 fd = open (sunaudiosink->device, O_WRONLY);
297 GST_OBJECT_UNLOCK (sunaudiosink);
301 sunaudiosink->fd = fd;
302 GST_OBJECT_UNLOCK (sunaudiosink);
304 ret = ioctl (fd, AUDIO_GETDEV, &sunaudiosink->dev);
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);
312 ret = ioctl (fd, AUDIO_GETINFO, &sunaudiosink->info);
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);
330 GST_ELEMENT_ERROR (sunaudiosink, RESOURCE, OPEN_WRITE, (NULL),
331 ("can't open connection to Sun Audio device %s", sunaudiosink->device));
334 close (sunaudiosink->fd);
335 GST_ELEMENT_ERROR (sunaudiosink, RESOURCE, SETTINGS, (NULL), ("%s",
341 gst_sunaudiosink_close (GstAudioSink * asink)
343 GstSunAudioSink *sunaudiosink = GST_SUNAUDIO_SINK (asink);
345 if (sunaudiosink->fd != -1) {
346 close (sunaudiosink->fd);
347 sunaudiosink->fd = -1;
353 gst_sunaudiosink_prepare (GstAudioSink * asink, GstRingBufferSpec * spec)
355 GstSunAudioSink *sunaudiosink = GST_SUNAUDIO_SINK (asink);
360 ret = ioctl (sunaudiosink->fd, AUDIO_GETINFO, &ainfo);
362 GST_ELEMENT_ERROR (sunaudiosink, RESOURCE, SETTINGS, (NULL), ("%s",
367 if (spec->width != 16)
370 ports = ainfo.play.port;
372 AUDIO_INITINFO (&ainfo);
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;
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);
386 spec->silence_sample[0] = 0;
387 spec->silence_sample[1] = 0;
388 spec->silence_sample[2] = 0;
389 spec->silence_sample[3] = 0;
391 ret = ioctl (sunaudiosink->fd, AUDIO_SETINFO, &ainfo);
393 GST_ELEMENT_ERROR (sunaudiosink, RESOURCE, SETTINGS, (NULL), ("%s",
398 /* Now read back the info to find out the actual buffer size and set
400 AUDIO_INITINFO (&ainfo);
402 ret = ioctl (sunaudiosink->fd, AUDIO_GETINFO, &ainfo);
404 GST_ELEMENT_ERROR (sunaudiosink, RESOURCE, SETTINGS, (NULL), ("%s",
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;
414 sunaudiosink->segtotal = spec->segtotal;
416 sunaudiosink->segtotal_samples =
417 spec->segtotal * spec->segsize / spec->bytes_per_sample;
419 sunaudiosink->segs_written = (gint) ainfo.play.eof;
420 sunaudiosink->samples_written = ainfo.play.samples;
421 sunaudiosink->bytes_per_sample = spec->bytes_per_sample;
423 GST_DEBUG_OBJECT (sunaudiosink, "Got device buffer_size of %u",
424 ainfo.play.buffer_size);
430 gst_sunaudiosink_unprepare (GstAudioSink * asink)
435 #define LOOP_WHILE_EINTR(v,func) do { (v) = (func); } \
436 while ((v) == -1 && errno == EINTR);
438 /* Called with the write_mutex held */
440 gst_sunaudio_sink_do_delay (GstSunAudioSink * sink)
442 GstBaseAudioSink *ba_sink = GST_BASE_AUDIO_SINK (sink);
443 GstClockTime total_sleep;
444 GstClockTime max_sleep;
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));
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;
468 if (diff + 1 < sink->segtotal)
469 return; /* no need to sleep at all */
471 /* Never sleep longer than the initial number of undrained segments in the
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;
478 /* Current time is our reference point */
479 g_get_current_time (&sleep_end);
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);
490 g_time_val_add (&sleep_end, sleep_usecs);
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 */
496 total_sleep += (sleep_usecs * GST_USECOND);
498 LOOP_WHILE_EINTR (err, ioctl (sink->fd, AUDIO_GETINFO, &ainfo));
502 /* Compute our (new) offset from the output (copes with overflow) */
503 diff = (guint) g_atomic_int_get (&sink->segs_written) - ainfo.play.eof;
509 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
510 ("Playback error on device '%s': %s", sink->device, strerror (errno)));
513 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (NULL),
514 ("Playback error on device '%s': %s", sink->device, strerror (errno)));
519 gst_sunaudiosink_write (GstAudioSink * asink, gpointer data, guint length)
521 GstSunAudioSink *sink = GST_SUNAUDIO_SINK (asink);
523 gint bytes_written, err;
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);
532 LOOP_WHILE_EINTR (bytes_written, write (sink->fd, data, length));
533 if (bytes_written < 0) {
538 /* Increment our sample counter, for delay calcs */
539 g_atomic_int_add (&sink->samples_written, length / sink->bytes_per_sample);
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;
547 /* Write a zero length output to trigger increment of the eof field */
548 LOOP_WHILE_EINTR (err, write (sink->fd, NULL, 0));
552 /* Count this extra segment we've written */
553 sink->segs_written += 1;
555 /* Now delay so we don't overrun the ring buffer */
556 gst_sunaudio_sink_do_delay (sink);
558 g_mutex_unlock (sink->write_mutex);
562 g_mutex_unlock (sink->write_mutex);
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 */
570 * Provide the current number of unplayed samples that have been written
573 gst_sunaudiosink_delay (GstAudioSink * asink)
575 GstSunAudioSink *sink = GST_SUNAUDIO_SINK (asink);
580 ret = ioctl (sink->fd, AUDIO_GETINFO, &ainfo);
581 if (G_UNLIKELY (ret == -1))
584 offset = (g_atomic_int_get (&sink->samples_written) - ainfo.play.samples);
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))
595 gst_sunaudiosink_reset (GstAudioSink * asink)
597 /* Get current values */
598 GstSunAudioSink *sunaudiosink = GST_SUNAUDIO_SINK (asink);
602 ret = ioctl (sunaudiosink->fd, AUDIO_GETINFO, &ainfo);
605 * Should never happen, but if we couldn't getinfo, then no point
608 GST_ELEMENT_ERROR (sunaudiosink, RESOURCE, SETTINGS, (NULL), ("%s",
614 * Pause the audio - so audio stops playing immediately rather than
615 * waiting for the ringbuffer to empty.
617 ainfo.play.pause = !NULL;
618 ret = ioctl (sunaudiosink->fd, AUDIO_SETINFO, &ainfo);
620 GST_ELEMENT_ERROR (sunaudiosink, RESOURCE, SETTINGS, (NULL), ("%s",
624 /* Flush the audio */
625 ret = ioctl (sunaudiosink->fd, I_FLUSH, FLUSHW);
627 GST_ELEMENT_ERROR (sunaudiosink, RESOURCE, SETTINGS, (NULL), ("%s",
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;
638 g_cond_signal (sunaudiosink->sleep_cond);
640 ret = ioctl (sunaudiosink->fd, I_FLUSH, FLUSHW);
642 GST_ELEMENT_ERROR (sunaudiosink, RESOURCE, SETTINGS, (NULL), ("%s",
646 /* unpause the audio */
647 ainfo.play.pause = NULL;
648 ret = ioctl (sunaudiosink->fd, AUDIO_SETINFO, &ainfo);
650 GST_ELEMENT_ERROR (sunaudiosink, RESOURCE, SETTINGS, (NULL), ("%s",
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 */
657 sunaudiosink->segs_written = ainfo.play.eof;
658 g_atomic_int_set (&sunaudiosink->samples_written, ainfo.play.samples);
660 sunaudiosink->flushing = FALSE;
661 g_mutex_unlock (sunaudiosink->write_mutex);