1 /* GStreamer OSS4 audio sink
2 * Copyright (C) 2007-2008 Tim-Philipp Müller <tim centricular net>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 * SECTION:element-oss4sink
22 * This element lets you output sound using the Open Sound System (OSS)
25 * Note that you should almost always use generic audio conversion elements
26 * like audioconvert and audioresample in front of an audiosink to make sure
27 * your pipeline works under all circumstances (those conversion elements will
28 * act in passthrough-mode if no conversion is necessary).
31 * <title>Example pipelines</title>
33 * gst-launch -v audiotestsrc ! audioconvert ! volume volume=0.1 ! oss4sink
34 * ]| will output a sine wave (continuous beep sound) to your sound card (with
35 * a very low volume as precaution).
37 * gst-launch -v filesrc location=music.ogg ! decodebin ! audioconvert ! audioresample ! oss4sink
38 * ]| will play an Ogg/Vorbis audio file and output it using the Open Sound System
45 /* TODO: - add "volume" property for stream volume control and intercept tags
53 #include <sys/types.h>
55 #include <sys/ioctl.h>
61 #include <gst/gst-i18n-plugin.h>
62 #include <gst/interfaces/streamvolume.h>
64 #define NO_LEGACY_MIXER
65 #include "oss4-audio.h"
66 #include "oss4-sink.h"
67 #include "oss4-property-probe.h"
68 #include "oss4-soundcard.h"
70 GST_DEBUG_CATEGORY_EXTERN (oss4sink_debug);
71 #define GST_CAT_DEFAULT oss4sink_debug
73 static void gst_oss4_sink_init_interfaces (GType type);
74 static void gst_oss4_sink_dispose (GObject * object);
75 static void gst_oss4_sink_finalise (GObject * object);
77 static void gst_oss4_sink_get_property (GObject * object, guint prop_id,
78 GValue * value, GParamSpec * pspec);
79 static void gst_oss4_sink_set_property (GObject * object, guint prop_id,
80 const GValue * value, GParamSpec * pspec);
82 static GstCaps *gst_oss4_sink_getcaps (GstBaseSink * bsink);
83 static gboolean gst_oss4_sink_open (GstAudioSink * asink,
84 gboolean silent_errors);
85 static gboolean gst_oss4_sink_open_func (GstAudioSink * asink);
86 static gboolean gst_oss4_sink_close (GstAudioSink * asink);
87 static gboolean gst_oss4_sink_prepare (GstAudioSink * asink,
88 GstRingBufferSpec * spec);
89 static gboolean gst_oss4_sink_unprepare (GstAudioSink * asink);
90 static guint gst_oss4_sink_write (GstAudioSink * asink, gpointer data,
92 static guint gst_oss4_sink_delay (GstAudioSink * asink);
93 static void gst_oss4_sink_reset (GstAudioSink * asink);
95 #define DEFAULT_DEVICE NULL
96 #define DEFAULT_DEVICE_NAME NULL
97 #define DEFAULT_MUTE FALSE
98 #define DEFAULT_VOLUME 1.0
99 #define MAX_VOLUME 10.0
111 GST_BOILERPLATE_FULL (GstOss4Sink, gst_oss4_sink, GstAudioSink,
112 GST_TYPE_AUDIO_SINK, gst_oss4_sink_init_interfaces);
115 gst_oss4_sink_dispose (GObject * object)
117 GstOss4Sink *osssink = GST_OSS4_SINK (object);
119 if (osssink->probed_caps) {
120 gst_caps_unref (osssink->probed_caps);
121 osssink->probed_caps = NULL;
124 G_OBJECT_CLASS (parent_class)->dispose (object);
128 gst_oss4_sink_base_init (gpointer g_class)
130 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
131 GstPadTemplate *templ;
133 gst_element_class_set_details_simple (element_class,
134 "OSS v4 Audio Sink", "Sink/Audio",
135 "Output to a sound card via OSS version 4",
136 "Tim-Philipp Müller <tim centricular net>");
138 templ = gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
139 gst_oss4_audio_get_template_caps ());
140 gst_element_class_add_pad_template (element_class, templ);
144 gst_oss4_sink_class_init (GstOss4SinkClass * klass)
146 GstAudioSinkClass *audiosink_class = (GstAudioSinkClass *) klass;
147 GstBaseSinkClass *basesink_class = (GstBaseSinkClass *) klass;
148 GObjectClass *gobject_class = (GObjectClass *) klass;
150 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_oss4_sink_dispose);
151 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_oss4_sink_finalise);
152 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_oss4_sink_get_property);
153 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_oss4_sink_set_property);
155 g_object_class_install_property (gobject_class, PROP_DEVICE,
156 g_param_spec_string ("device", "Device",
157 "OSS4 device (e.g. /dev/oss/hdaudio0/pcm0 or /dev/dspN) "
158 "(NULL = use first available playback device)",
159 DEFAULT_DEVICE, G_PARAM_READWRITE));
161 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
162 g_param_spec_string ("device-name", "Device name",
163 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
166 g_object_class_install_property (gobject_class,
168 g_param_spec_double ("volume", "Volume",
169 "Linear volume of this stream, 1.0=100%", 0.0, MAX_VOLUME,
170 DEFAULT_VOLUME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
172 g_object_class_install_property (gobject_class,
174 g_param_spec_boolean ("mute", "Mute",
175 "Mute state of this stream", DEFAULT_MUTE,
176 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
178 basesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_oss4_sink_getcaps);
180 audiosink_class->open = GST_DEBUG_FUNCPTR (gst_oss4_sink_open_func);
181 audiosink_class->close = GST_DEBUG_FUNCPTR (gst_oss4_sink_close);
182 audiosink_class->prepare = GST_DEBUG_FUNCPTR (gst_oss4_sink_prepare);
183 audiosink_class->unprepare = GST_DEBUG_FUNCPTR (gst_oss4_sink_unprepare);
184 audiosink_class->write = GST_DEBUG_FUNCPTR (gst_oss4_sink_write);
185 audiosink_class->delay = GST_DEBUG_FUNCPTR (gst_oss4_sink_delay);
186 audiosink_class->reset = GST_DEBUG_FUNCPTR (gst_oss4_sink_reset);
190 gst_oss4_sink_init (GstOss4Sink * osssink, GstOss4SinkClass * klass)
194 device = g_getenv ("AUDIODEV");
196 device = DEFAULT_DEVICE;
197 osssink->device = g_strdup (device);
199 osssink->bytes_per_sample = 0;
200 osssink->probed_caps = NULL;
201 osssink->device_name = NULL;
202 osssink->mute_volume = 100 | (100 << 8);
206 gst_oss4_sink_finalise (GObject * object)
208 GstOss4Sink *osssink = GST_OSS4_SINK (object);
210 g_free (osssink->device);
211 osssink->device = NULL;
213 g_list_free (osssink->property_probe_list);
214 osssink->property_probe_list = NULL;
216 G_OBJECT_CLASS (parent_class)->finalize (object);
220 gst_oss4_sink_set_volume (GstOss4Sink * oss, gdouble volume)
224 volume = volume * 100.0;
225 ivol = (int) volume | ((int) volume << 8);
226 GST_OBJECT_LOCK (oss);
227 if (ioctl (oss->fd, SNDCTL_DSP_SETPLAYVOL, &ivol) < 0) {
228 GST_LOG_OBJECT (oss, "SETPLAYVOL failed");
230 GST_OBJECT_UNLOCK (oss);
234 gst_oss4_sink_get_volume (GstOss4Sink * oss)
236 int ivol, lvol, rvol;
237 gdouble dvol = DEFAULT_VOLUME;
239 GST_OBJECT_LOCK (oss);
240 if (ioctl (oss->fd, SNDCTL_DSP_GETPLAYVOL, &ivol) < 0) {
241 GST_LOG_OBJECT (oss, "GETPLAYVOL failed");
243 /* Return the higher of the two volume channels, if different */
245 rvol = (ivol >> 8) & 0xff;
246 dvol = MAX (lvol, rvol) / 100.0;
248 GST_OBJECT_UNLOCK (oss);
254 gst_oss4_sink_set_mute (GstOss4Sink * oss, gboolean mute)
260 * OSSv4 does not have a per-channel mute, so simulate by setting
261 * the value to 0. Save the volume before doing a mute so we can
262 * reset the value when the user un-mutes.
266 GST_OBJECT_LOCK (oss);
267 if (ioctl (oss->fd, SNDCTL_DSP_GETPLAYVOL, &oss->mute_volume) < 0) {
268 GST_LOG_OBJECT (oss, "GETPLAYVOL failed");
270 if (ioctl (oss->fd, SNDCTL_DSP_SETPLAYVOL, &ivol) < 0) {
271 GST_LOG_OBJECT (oss, "SETPLAYVOL failed");
273 GST_OBJECT_UNLOCK (oss);
276 * If the saved volume is 0, then reset it to 100. Otherwise the mute
277 * can get stuck. This can happen, for example, due to rounding
278 * errors in converting from the float to an integer.
280 if (oss->mute_volume == 0) {
281 oss->mute_volume = 100 | (100 << 8);
283 GST_OBJECT_LOCK (oss);
284 if (ioctl (oss->fd, SNDCTL_DSP_SETPLAYVOL, &oss->mute_volume) < 0) {
285 GST_LOG_OBJECT (oss, "SETPLAYVOL failed");
287 GST_OBJECT_UNLOCK (oss);
292 gst_oss4_sink_get_mute (GstOss4Sink * oss)
294 int ivol, lvol, rvol;
296 GST_OBJECT_LOCK (oss);
297 if (ioctl (oss->fd, SNDCTL_DSP_GETPLAYVOL, &ivol) < 0) {
298 GST_LOG_OBJECT (oss, "GETPLAYVOL failed");
302 rvol = (ivol >> 8) & 0xff;
304 GST_OBJECT_UNLOCK (oss);
306 return (lvol == 0 && rvol == 0);
310 gst_oss4_sink_set_property (GObject * object, guint prop_id,
311 const GValue * value, GParamSpec * pspec)
313 GstOss4Sink *oss = GST_OSS4_SINK (object);
317 GST_OBJECT_LOCK (oss);
319 g_free (oss->device);
320 oss->device = g_value_dup_string (value);
321 if (oss->probed_caps) {
322 gst_caps_unref (oss->probed_caps);
323 oss->probed_caps = NULL;
325 g_free (oss->device_name);
326 oss->device_name = NULL;
328 g_warning ("%s: can't change \"device\" property while audio sink "
329 "is open", GST_OBJECT_NAME (oss));
331 GST_OBJECT_UNLOCK (oss);
334 gst_oss4_sink_set_volume (oss, g_value_get_double (value));
337 gst_oss4_sink_set_mute (oss, g_value_get_boolean (value));
340 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
346 gst_oss4_sink_get_property (GObject * object, guint prop_id,
347 GValue * value, GParamSpec * pspec)
349 GstOss4Sink *oss = GST_OSS4_SINK (object);
353 GST_OBJECT_LOCK (oss);
354 g_value_set_string (value, oss->device);
355 GST_OBJECT_UNLOCK (oss);
357 case PROP_DEVICE_NAME:
358 GST_OBJECT_LOCK (oss);
359 if (oss->fd == -1 && oss->device != NULL) {
360 /* If device is set, try to retrieve the name even if we're not open */
361 if (gst_oss4_sink_open (GST_AUDIO_SINK (oss), TRUE)) {
362 g_value_set_string (value, oss->device_name);
363 gst_oss4_sink_close (GST_AUDIO_SINK (oss));
367 gst_oss4_property_probe_find_device_name_nofd (GST_OBJECT (oss),
369 g_value_set_string (value, name);
373 g_value_set_string (value, oss->device_name);
375 GST_OBJECT_UNLOCK (oss);
378 g_value_set_double (value, gst_oss4_sink_get_volume (oss));
381 g_value_set_boolean (value, gst_oss4_sink_get_mute (oss));
384 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
390 gst_oss4_sink_getcaps (GstBaseSink * bsink)
395 oss = GST_OSS4_SINK (bsink);
398 caps = gst_caps_copy (gst_oss4_audio_get_template_caps ());
399 } else if (oss->probed_caps) {
400 caps = gst_caps_copy (oss->probed_caps);
402 caps = gst_oss4_audio_probe_caps (GST_OBJECT (oss), oss->fd);
403 if (caps != NULL && !gst_caps_is_empty (caps)) {
404 oss->probed_caps = gst_caps_copy (caps);
411 /* note: we must not take the object lock here unless we fix up get_property */
413 gst_oss4_sink_open (GstAudioSink * asink, gboolean silent_errors)
419 oss = GST_OSS4_SINK (asink);
422 device = g_strdup (oss->device);
424 device = gst_oss4_audio_find_device (GST_OBJECT_CAST (oss));
426 /* desperate times, desperate measures */
428 device = g_strdup ("/dev/dsp0");
430 GST_INFO_OBJECT (oss, "Trying to open OSS4 device '%s'", device);
432 /* we open in non-blocking mode even if we don't really want to do writes
433 * non-blocking because we can't be sure that this is really a genuine
434 * OSS4 device with well-behaved drivers etc. We really don't want to
435 * hang forever under any circumstances. */
436 oss->fd = open (device, O_WRONLY | O_NONBLOCK, 0);
448 GST_INFO_OBJECT (oss, "Opened device '%s'", device);
450 /* Make sure it's OSS4. If it's old OSS, let osssink handle it */
451 if (!gst_oss4_audio_check_version (GST_OBJECT_CAST (oss), oss->fd))
454 /* now remove the non-blocking flag. */
455 mode = fcntl (oss->fd, F_GETFL);
457 if (fcntl (oss->fd, F_SETFL, mode) < 0) {
458 /* some drivers do no support unsetting the non-blocking flag, try to
459 * close/open the device then. This is racy but we error out properly. */
460 GST_WARNING_OBJECT (oss, "failed to unset O_NONBLOCK (buggy driver?), "
461 "will try to re-open device now");
462 gst_oss4_sink_close (asink);
463 if ((oss->fd = open (device, O_WRONLY, 0)) == -1)
467 oss->open_device = device;
469 /* not using ENGINEINFO here because it sometimes returns a different and
470 * less useful name than AUDIOINFO for the same device */
471 if (!gst_oss4_property_probe_find_device_name (GST_OBJECT (oss), oss->fd,
472 oss->open_device, &oss->device_name)) {
473 oss->device_name = NULL;
476 /* list output routings, for informational purposes only so far */
478 oss_mixer_enuminfo routings = { 0, };
481 if (ioctl (oss->fd, SNDCTL_DSP_GET_PLAYTGT_NAMES, &routings) != -1) {
482 GST_LOG_OBJECT (oss, "%u output routings (static list: %d)",
483 routings.nvalues, !!(routings.version == 0));
484 for (i = 0; i < routings.nvalues; ++i) {
485 GST_LOG_OBJECT (oss, " output routing %d: %s", i,
486 &routings.strings[routings.strindex[i]]);
496 if (!silent_errors) {
497 GST_ELEMENT_ERROR (oss, RESOURCE, BUSY,
498 (_("Could not open audio device for playback. "
499 "Device is being used by another application.")), (NULL));
506 if (!silent_errors) {
507 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_WRITE,
508 (_("Could not open audio device for playback. "
509 "You don't have permission to open the device.")),
517 if (!silent_errors) {
518 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_WRITE,
519 (_("Could not open audio device for playback.")), GST_ERROR_SYSTEM);
526 if (!silent_errors) {
527 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_WRITE,
528 (_("Could not open audio device for playback. "
529 "This version of the Open Sound System is not supported by this "
530 "element.")), ("Try the 'osssink' element instead"));
532 gst_oss4_sink_close (asink);
538 if (!silent_errors) {
539 GST_ELEMENT_ERROR (oss, RESOURCE, SETTINGS, (NULL),
540 ("Unable to set device %s into non-blocking mode: %s",
541 oss->device, g_strerror (errno)));
549 gst_oss4_sink_open_func (GstAudioSink * asink)
551 return gst_oss4_sink_open (asink, FALSE);
555 gst_oss4_sink_close (GstAudioSink * asink)
557 GstOss4Sink *oss = GST_OSS4_SINK (asink);
560 GST_DEBUG_OBJECT (oss, "closing device");
565 oss->bytes_per_sample = 0;
566 /* we keep the probed caps cached, at least until the device changes */
568 g_free (oss->open_device);
569 oss->open_device = NULL;
571 g_free (oss->device_name);
572 oss->device_name = NULL;
574 if (oss->probed_caps) {
575 gst_caps_unref (oss->probed_caps);
576 oss->probed_caps = NULL;
583 gst_oss4_sink_prepare (GstAudioSink * asink, GstRingBufferSpec * spec)
587 oss = GST_OSS4_SINK (asink);
589 if (!gst_oss4_audio_set_format (GST_OBJECT_CAST (oss), oss->fd, spec)) {
590 GST_WARNING_OBJECT (oss, "Couldn't set requested format %" GST_PTR_FORMAT,
595 oss->bytes_per_sample = spec->bytes_per_sample;
600 gst_oss4_sink_unprepare (GstAudioSink * asink)
602 /* could do a SNDCTL_DSP_HALT, but the OSS manual recommends a close/open,
603 * since HALT won't properly reset some devices, apparently */
605 if (!gst_oss4_sink_close (asink))
608 if (!gst_oss4_sink_open_func (asink))
616 GST_DEBUG_OBJECT (asink, "Couldn't close the audio device");
621 GST_DEBUG_OBJECT (asink, "Couldn't reopen the audio device");
627 gst_oss4_sink_write (GstAudioSink * asink, gpointer data, guint length)
632 oss = GST_OSS4_SINK_CAST (asink);
634 n = write (oss->fd, data, length);
635 GST_LOG_OBJECT (asink, "wrote %d/%d samples, %d bytes",
636 n / oss->bytes_per_sample, length / oss->bytes_per_sample, n);
638 if (G_UNLIKELY (n < 0)) {
642 /* This is the most likely cause, I think */
643 GST_ELEMENT_ERROR (asink, RESOURCE, WRITE,
644 (_("Playback is not supported by this audio device.")),
645 ("write: %s (device: %s) (maybe this is an input-only device?)",
646 g_strerror (errno), oss->open_device));
650 GST_ELEMENT_ERROR (asink, RESOURCE, WRITE,
651 (_("Audio playback error.")),
652 ("write: %s (device: %s)", g_strerror (errno), oss->open_device));
662 gst_oss4_sink_delay (GstAudioSink * asink)
667 oss = GST_OSS4_SINK_CAST (asink);
669 GST_OBJECT_LOCK (oss);
670 if (ioctl (oss->fd, SNDCTL_DSP_GETODELAY, &delay) < 0 || delay < 0) {
671 GST_LOG_OBJECT (oss, "GETODELAY failed");
673 GST_OBJECT_UNLOCK (oss);
675 if (G_UNLIKELY (delay < 0)) /* error case */
678 return delay / oss->bytes_per_sample;
682 gst_oss4_sink_reset (GstAudioSink * asink)
684 /* There's nothing we can do here really: OSS can't handle access to the
685 * same device/fd from multiple threads and might deadlock or blow up in
686 * other ways if we try an ioctl SNDCTL_DSP_HALT or similar */
690 gst_oss4_sink_init_interfaces (GType type)
692 static const GInterfaceInfo svol_iface_info = {
696 g_type_add_interface_static (type, GST_TYPE_STREAM_VOLUME, &svol_iface_info);
698 gst_oss4_add_property_probe_interface (type);