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
49 #include <sys/types.h>
51 #include <sys/ioctl.h>
57 #include <gst/gst-i18n-plugin.h>
58 #include <gst/interfaces/streamvolume.h>
60 #define NO_LEGACY_MIXER
61 #include "oss4-audio.h"
62 #include "oss4-sink.h"
63 #include "oss4-property-probe.h"
64 #include "oss4-soundcard.h"
66 GST_DEBUG_CATEGORY_EXTERN (oss4sink_debug);
67 #define GST_CAT_DEFAULT oss4sink_debug
69 static void gst_oss4_sink_init_interfaces (GType type);
70 static void gst_oss4_sink_dispose (GObject * object);
71 static void gst_oss4_sink_finalize (GObject * object);
73 static void gst_oss4_sink_get_property (GObject * object, guint prop_id,
74 GValue * value, GParamSpec * pspec);
75 static void gst_oss4_sink_set_property (GObject * object, guint prop_id,
76 const GValue * value, GParamSpec * pspec);
78 static GstCaps *gst_oss4_sink_getcaps (GstBaseSink * bsink);
79 static gboolean gst_oss4_sink_open (GstAudioSink * asink,
80 gboolean silent_errors);
81 static gboolean gst_oss4_sink_open_func (GstAudioSink * asink);
82 static gboolean gst_oss4_sink_close (GstAudioSink * asink);
83 static gboolean gst_oss4_sink_prepare (GstAudioSink * asink,
84 GstRingBufferSpec * spec);
85 static gboolean gst_oss4_sink_unprepare (GstAudioSink * asink);
86 static guint gst_oss4_sink_write (GstAudioSink * asink, gpointer data,
88 static guint gst_oss4_sink_delay (GstAudioSink * asink);
89 static void gst_oss4_sink_reset (GstAudioSink * asink);
91 #define DEFAULT_DEVICE NULL
92 #define DEFAULT_DEVICE_NAME NULL
93 #define DEFAULT_MUTE FALSE
94 #define DEFAULT_VOLUME 1.0
95 #define MAX_VOLUME 10.0
107 GST_BOILERPLATE_FULL (GstOss4Sink, gst_oss4_sink, GstAudioSink,
108 GST_TYPE_AUDIO_SINK, gst_oss4_sink_init_interfaces);
111 gst_oss4_sink_dispose (GObject * object)
113 GstOss4Sink *osssink = GST_OSS4_SINK (object);
115 if (osssink->probed_caps) {
116 gst_caps_unref (osssink->probed_caps);
117 osssink->probed_caps = NULL;
120 G_OBJECT_CLASS (parent_class)->dispose (object);
124 gst_oss4_sink_base_init (gpointer g_class)
126 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
127 GstPadTemplate *templ;
129 gst_element_class_set_details_simple (element_class,
130 "OSS v4 Audio Sink", "Sink/Audio",
131 "Output to a sound card via OSS version 4",
132 "Tim-Philipp Müller <tim centricular net>");
134 templ = gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
135 gst_oss4_audio_get_template_caps ());
136 gst_element_class_add_pad_template (element_class, templ);
140 gst_oss4_sink_class_init (GstOss4SinkClass * klass)
142 GstAudioSinkClass *audiosink_class = (GstAudioSinkClass *) klass;
143 GstBaseSinkClass *basesink_class = (GstBaseSinkClass *) klass;
144 GObjectClass *gobject_class = (GObjectClass *) klass;
146 gobject_class->dispose = gst_oss4_sink_dispose;
147 gobject_class->finalize = gst_oss4_sink_finalize;
148 gobject_class->get_property = gst_oss4_sink_get_property;
149 gobject_class->set_property = gst_oss4_sink_set_property;
151 g_object_class_install_property (gobject_class, PROP_DEVICE,
152 g_param_spec_string ("device", "Device",
153 "OSS4 device (e.g. /dev/oss/hdaudio0/pcm0 or /dev/dspN) "
154 "(NULL = use first available playback device)",
155 DEFAULT_DEVICE, G_PARAM_READWRITE));
157 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
158 g_param_spec_string ("device-name", "Device name",
159 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
162 g_object_class_install_property (gobject_class,
164 g_param_spec_double ("volume", "Volume",
165 "Linear volume of this stream, 1.0=100%", 0.0, MAX_VOLUME,
166 DEFAULT_VOLUME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
168 g_object_class_install_property (gobject_class,
170 g_param_spec_boolean ("mute", "Mute",
171 "Mute state of this stream", DEFAULT_MUTE,
172 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
174 basesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_oss4_sink_getcaps);
176 audiosink_class->open = GST_DEBUG_FUNCPTR (gst_oss4_sink_open_func);
177 audiosink_class->close = GST_DEBUG_FUNCPTR (gst_oss4_sink_close);
178 audiosink_class->prepare = GST_DEBUG_FUNCPTR (gst_oss4_sink_prepare);
179 audiosink_class->unprepare = GST_DEBUG_FUNCPTR (gst_oss4_sink_unprepare);
180 audiosink_class->write = GST_DEBUG_FUNCPTR (gst_oss4_sink_write);
181 audiosink_class->delay = GST_DEBUG_FUNCPTR (gst_oss4_sink_delay);
182 audiosink_class->reset = GST_DEBUG_FUNCPTR (gst_oss4_sink_reset);
186 gst_oss4_sink_init (GstOss4Sink * osssink, GstOss4SinkClass * klass)
190 device = g_getenv ("AUDIODEV");
192 device = DEFAULT_DEVICE;
193 osssink->device = g_strdup (device);
195 osssink->bytes_per_sample = 0;
196 osssink->probed_caps = NULL;
197 osssink->device_name = NULL;
198 osssink->mute_volume = 100 | (100 << 8);
202 gst_oss4_sink_finalize (GObject * object)
204 GstOss4Sink *osssink = GST_OSS4_SINK (object);
206 g_free (osssink->device);
207 osssink->device = NULL;
209 g_list_free (osssink->property_probe_list);
210 osssink->property_probe_list = NULL;
212 G_OBJECT_CLASS (parent_class)->finalize (object);
216 gst_oss4_sink_set_volume (GstOss4Sink * oss, gdouble volume)
220 volume = volume * 100.0;
221 ivol = (int) volume | ((int) volume << 8);
222 GST_OBJECT_LOCK (oss);
223 if (ioctl (oss->fd, SNDCTL_DSP_SETPLAYVOL, &ivol) < 0) {
224 GST_LOG_OBJECT (oss, "SETPLAYVOL failed");
226 GST_OBJECT_UNLOCK (oss);
230 gst_oss4_sink_get_volume (GstOss4Sink * oss)
232 int ivol, lvol, rvol;
233 gdouble dvol = DEFAULT_VOLUME;
235 GST_OBJECT_LOCK (oss);
236 if (ioctl (oss->fd, SNDCTL_DSP_GETPLAYVOL, &ivol) < 0) {
237 GST_LOG_OBJECT (oss, "GETPLAYVOL failed");
239 /* Return the higher of the two volume channels, if different */
241 rvol = (ivol >> 8) & 0xff;
242 dvol = MAX (lvol, rvol) / 100.0;
244 GST_OBJECT_UNLOCK (oss);
250 gst_oss4_sink_set_mute (GstOss4Sink * oss, gboolean mute)
256 * OSSv4 does not have a per-channel mute, so simulate by setting
257 * the value to 0. Save the volume before doing a mute so we can
258 * reset the value when the user un-mutes.
262 GST_OBJECT_LOCK (oss);
263 if (ioctl (oss->fd, SNDCTL_DSP_GETPLAYVOL, &oss->mute_volume) < 0) {
264 GST_LOG_OBJECT (oss, "GETPLAYVOL failed");
266 if (ioctl (oss->fd, SNDCTL_DSP_SETPLAYVOL, &ivol) < 0) {
267 GST_LOG_OBJECT (oss, "SETPLAYVOL failed");
269 GST_OBJECT_UNLOCK (oss);
272 * If the saved volume is 0, then reset it to 100. Otherwise the mute
273 * can get stuck. This can happen, for example, due to rounding
274 * errors in converting from the float to an integer.
276 if (oss->mute_volume == 0) {
277 oss->mute_volume = 100 | (100 << 8);
279 GST_OBJECT_LOCK (oss);
280 if (ioctl (oss->fd, SNDCTL_DSP_SETPLAYVOL, &oss->mute_volume) < 0) {
281 GST_LOG_OBJECT (oss, "SETPLAYVOL failed");
283 GST_OBJECT_UNLOCK (oss);
288 gst_oss4_sink_get_mute (GstOss4Sink * oss)
290 int ivol, lvol, rvol;
292 GST_OBJECT_LOCK (oss);
293 if (ioctl (oss->fd, SNDCTL_DSP_GETPLAYVOL, &ivol) < 0) {
294 GST_LOG_OBJECT (oss, "GETPLAYVOL failed");
298 rvol = (ivol >> 8) & 0xff;
300 GST_OBJECT_UNLOCK (oss);
302 return (lvol == 0 && rvol == 0);
306 gst_oss4_sink_set_property (GObject * object, guint prop_id,
307 const GValue * value, GParamSpec * pspec)
309 GstOss4Sink *oss = GST_OSS4_SINK (object);
313 GST_OBJECT_LOCK (oss);
315 g_free (oss->device);
316 oss->device = g_value_dup_string (value);
317 if (oss->probed_caps) {
318 gst_caps_unref (oss->probed_caps);
319 oss->probed_caps = NULL;
321 g_free (oss->device_name);
322 oss->device_name = NULL;
324 g_warning ("%s: can't change \"device\" property while audio sink "
325 "is open", GST_OBJECT_NAME (oss));
327 GST_OBJECT_UNLOCK (oss);
330 gst_oss4_sink_set_volume (oss, g_value_get_double (value));
333 gst_oss4_sink_set_mute (oss, g_value_get_boolean (value));
336 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
342 gst_oss4_sink_get_property (GObject * object, guint prop_id,
343 GValue * value, GParamSpec * pspec)
345 GstOss4Sink *oss = GST_OSS4_SINK (object);
349 GST_OBJECT_LOCK (oss);
350 g_value_set_string (value, oss->device);
351 GST_OBJECT_UNLOCK (oss);
353 case PROP_DEVICE_NAME:
354 GST_OBJECT_LOCK (oss);
355 if (oss->fd == -1 && oss->device != NULL) {
356 /* If device is set, try to retrieve the name even if we're not open */
357 if (gst_oss4_sink_open (GST_AUDIO_SINK (oss), TRUE)) {
358 g_value_set_string (value, oss->device_name);
359 gst_oss4_sink_close (GST_AUDIO_SINK (oss));
363 gst_oss4_property_probe_find_device_name_nofd (GST_OBJECT (oss),
365 g_value_set_string (value, name);
369 g_value_set_string (value, oss->device_name);
371 GST_OBJECT_UNLOCK (oss);
374 g_value_set_double (value, gst_oss4_sink_get_volume (oss));
377 g_value_set_boolean (value, gst_oss4_sink_get_mute (oss));
380 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
386 gst_oss4_sink_getcaps (GstBaseSink * bsink)
391 oss = GST_OSS4_SINK (bsink);
394 caps = gst_caps_copy (gst_oss4_audio_get_template_caps ());
395 } else if (oss->probed_caps) {
396 caps = gst_caps_copy (oss->probed_caps);
398 caps = gst_oss4_audio_probe_caps (GST_OBJECT (oss), oss->fd);
399 if (caps != NULL && !gst_caps_is_empty (caps)) {
400 oss->probed_caps = gst_caps_copy (caps);
407 /* note: we must not take the object lock here unless we fix up get_property */
409 gst_oss4_sink_open (GstAudioSink * asink, gboolean silent_errors)
415 oss = GST_OSS4_SINK (asink);
418 device = g_strdup (oss->device);
420 device = gst_oss4_audio_find_device (GST_OBJECT_CAST (oss));
422 /* desperate times, desperate measures */
424 device = g_strdup ("/dev/dsp0");
426 GST_INFO_OBJECT (oss, "Trying to open OSS4 device '%s'", device);
428 /* we open in non-blocking mode even if we don't really want to do writes
429 * non-blocking because we can't be sure that this is really a genuine
430 * OSS4 device with well-behaved drivers etc. We really don't want to
431 * hang forever under any circumstances. */
432 oss->fd = open (device, O_WRONLY | O_NONBLOCK, 0);
444 GST_INFO_OBJECT (oss, "Opened device '%s'", device);
446 /* Make sure it's OSS4. If it's old OSS, let osssink handle it */
447 if (!gst_oss4_audio_check_version (GST_OBJECT_CAST (oss), oss->fd))
450 /* now remove the non-blocking flag. */
451 mode = fcntl (oss->fd, F_GETFL);
453 if (fcntl (oss->fd, F_SETFL, mode) < 0) {
454 /* some drivers do no support unsetting the non-blocking flag, try to
455 * close/open the device then. This is racy but we error out properly. */
456 GST_WARNING_OBJECT (oss, "failed to unset O_NONBLOCK (buggy driver?), "
457 "will try to re-open device now");
458 gst_oss4_sink_close (asink);
459 if ((oss->fd = open (device, O_WRONLY, 0)) == -1)
463 oss->open_device = device;
465 /* not using ENGINEINFO here because it sometimes returns a different and
466 * less useful name than AUDIOINFO for the same device */
467 if (!gst_oss4_property_probe_find_device_name (GST_OBJECT (oss), oss->fd,
468 oss->open_device, &oss->device_name)) {
469 oss->device_name = NULL;
472 /* list output routings, for informational purposes only so far */
474 oss_mixer_enuminfo routings = { 0, };
477 if (ioctl (oss->fd, SNDCTL_DSP_GET_PLAYTGT_NAMES, &routings) != -1) {
478 GST_LOG_OBJECT (oss, "%u output routings (static list: %d)",
479 routings.nvalues, !!(routings.version == 0));
480 for (i = 0; i < routings.nvalues; ++i) {
481 GST_LOG_OBJECT (oss, " output routing %d: %s", i,
482 &routings.strings[routings.strindex[i]]);
492 if (!silent_errors) {
493 GST_ELEMENT_ERROR (oss, RESOURCE, BUSY,
494 (_("Could not open audio device for playback. "
495 "Device is being used by another application.")), (NULL));
502 if (!silent_errors) {
503 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_WRITE,
504 (_("Could not open audio device for playback. "
505 "You don't have permission to open the device.")),
513 if (!silent_errors) {
514 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_WRITE,
515 (_("Could not open audio device for playback.")), GST_ERROR_SYSTEM);
522 if (!silent_errors) {
523 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_WRITE,
524 (_("Could not open audio device for playback. "
525 "This version of the Open Sound System is not supported by this "
526 "element.")), ("Try the 'osssink' element instead"));
528 gst_oss4_sink_close (asink);
534 if (!silent_errors) {
535 GST_ELEMENT_ERROR (oss, RESOURCE, SETTINGS, (NULL),
536 ("Unable to set device %s into non-blocking mode: %s",
537 oss->device, g_strerror (errno)));
545 gst_oss4_sink_open_func (GstAudioSink * asink)
547 return gst_oss4_sink_open (asink, FALSE);
551 gst_oss4_sink_close (GstAudioSink * asink)
553 GstOss4Sink *oss = GST_OSS4_SINK (asink);
556 GST_DEBUG_OBJECT (oss, "closing device");
561 oss->bytes_per_sample = 0;
562 /* we keep the probed caps cached, at least until the device changes */
564 g_free (oss->open_device);
565 oss->open_device = NULL;
567 g_free (oss->device_name);
568 oss->device_name = NULL;
570 if (oss->probed_caps) {
571 gst_caps_unref (oss->probed_caps);
572 oss->probed_caps = NULL;
579 gst_oss4_sink_prepare (GstAudioSink * asink, GstRingBufferSpec * spec)
583 oss = GST_OSS4_SINK (asink);
585 if (!gst_oss4_audio_set_format (GST_OBJECT_CAST (oss), oss->fd, spec)) {
586 GST_WARNING_OBJECT (oss, "Couldn't set requested format %" GST_PTR_FORMAT,
591 oss->bytes_per_sample = spec->bytes_per_sample;
596 gst_oss4_sink_unprepare (GstAudioSink * asink)
598 /* could do a SNDCTL_DSP_HALT, but the OSS manual recommends a close/open,
599 * since HALT won't properly reset some devices, apparently */
601 if (!gst_oss4_sink_close (asink))
604 if (!gst_oss4_sink_open_func (asink))
612 GST_DEBUG_OBJECT (asink, "Couldn't close the audio device");
617 GST_DEBUG_OBJECT (asink, "Couldn't reopen the audio device");
623 gst_oss4_sink_write (GstAudioSink * asink, gpointer data, guint length)
628 oss = GST_OSS4_SINK_CAST (asink);
630 n = write (oss->fd, data, length);
631 GST_LOG_OBJECT (asink, "wrote %d/%d samples, %d bytes",
632 n / oss->bytes_per_sample, length / oss->bytes_per_sample, n);
634 if (G_UNLIKELY (n < 0)) {
638 /* This is the most likely cause, I think */
639 GST_ELEMENT_ERROR (asink, RESOURCE, WRITE,
640 (_("Playback is not supported by this audio device.")),
641 ("write: %s (device: %s) (maybe this is an input-only device?)",
642 g_strerror (errno), oss->open_device));
646 GST_ELEMENT_ERROR (asink, RESOURCE, WRITE,
647 (_("Audio playback error.")),
648 ("write: %s (device: %s)", g_strerror (errno), oss->open_device));
658 gst_oss4_sink_delay (GstAudioSink * asink)
663 oss = GST_OSS4_SINK_CAST (asink);
665 GST_OBJECT_LOCK (oss);
666 if (ioctl (oss->fd, SNDCTL_DSP_GETODELAY, &delay) < 0 || delay < 0) {
667 GST_LOG_OBJECT (oss, "GETODELAY failed");
669 GST_OBJECT_UNLOCK (oss);
671 if (G_UNLIKELY (delay < 0)) /* error case */
674 return delay / oss->bytes_per_sample;
678 gst_oss4_sink_reset (GstAudioSink * asink)
680 /* There's nothing we can do here really: OSS can't handle access to the
681 * same device/fd from multiple threads and might deadlock or blow up in
682 * other ways if we try an ioctl SNDCTL_DSP_HALT or similar */
686 gst_oss4_sink_init_interfaces (GType type)
688 static const GInterfaceInfo svol_iface_info = {
692 g_type_add_interface_static (type, GST_TYPE_STREAM_VOLUME, &svol_iface_info);
694 gst_oss4_add_property_probe_interface (type);