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);
137 gst_object_unref (templ);
141 gst_oss4_sink_class_init (GstOss4SinkClass * klass)
143 GstAudioSinkClass *audiosink_class = (GstAudioSinkClass *) klass;
144 GstBaseSinkClass *basesink_class = (GstBaseSinkClass *) klass;
145 GObjectClass *gobject_class = (GObjectClass *) klass;
147 gobject_class->dispose = gst_oss4_sink_dispose;
148 gobject_class->finalize = gst_oss4_sink_finalize;
149 gobject_class->get_property = gst_oss4_sink_get_property;
150 gobject_class->set_property = gst_oss4_sink_set_property;
152 g_object_class_install_property (gobject_class, PROP_DEVICE,
153 g_param_spec_string ("device", "Device",
154 "OSS4 device (e.g. /dev/oss/hdaudio0/pcm0 or /dev/dspN) "
155 "(NULL = use first available playback device)",
156 DEFAULT_DEVICE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
158 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
159 g_param_spec_string ("device-name", "Device name",
160 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
161 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
163 g_object_class_install_property (gobject_class,
165 g_param_spec_double ("volume", "Volume",
166 "Linear volume of this stream, 1.0=100%", 0.0, MAX_VOLUME,
167 DEFAULT_VOLUME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
169 g_object_class_install_property (gobject_class,
171 g_param_spec_boolean ("mute", "Mute",
172 "Mute state of this stream", DEFAULT_MUTE,
173 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
175 basesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_oss4_sink_getcaps);
177 audiosink_class->open = GST_DEBUG_FUNCPTR (gst_oss4_sink_open_func);
178 audiosink_class->close = GST_DEBUG_FUNCPTR (gst_oss4_sink_close);
179 audiosink_class->prepare = GST_DEBUG_FUNCPTR (gst_oss4_sink_prepare);
180 audiosink_class->unprepare = GST_DEBUG_FUNCPTR (gst_oss4_sink_unprepare);
181 audiosink_class->write = GST_DEBUG_FUNCPTR (gst_oss4_sink_write);
182 audiosink_class->delay = GST_DEBUG_FUNCPTR (gst_oss4_sink_delay);
183 audiosink_class->reset = GST_DEBUG_FUNCPTR (gst_oss4_sink_reset);
187 gst_oss4_sink_init (GstOss4Sink * osssink, GstOss4SinkClass * klass)
191 device = g_getenv ("AUDIODEV");
193 device = DEFAULT_DEVICE;
194 osssink->device = g_strdup (device);
196 osssink->bytes_per_sample = 0;
197 osssink->probed_caps = NULL;
198 osssink->device_name = NULL;
199 osssink->mute_volume = 100 | (100 << 8);
203 gst_oss4_sink_finalize (GObject * object)
205 GstOss4Sink *osssink = GST_OSS4_SINK (object);
207 g_free (osssink->device);
208 osssink->device = NULL;
210 g_list_free (osssink->property_probe_list);
211 osssink->property_probe_list = NULL;
213 G_OBJECT_CLASS (parent_class)->finalize (object);
217 gst_oss4_sink_set_volume (GstOss4Sink * oss, gdouble volume)
221 volume = volume * 100.0;
222 ivol = (int) volume | ((int) volume << 8);
223 GST_OBJECT_LOCK (oss);
224 if (ioctl (oss->fd, SNDCTL_DSP_SETPLAYVOL, &ivol) < 0) {
225 GST_LOG_OBJECT (oss, "SETPLAYVOL failed");
227 GST_OBJECT_UNLOCK (oss);
231 gst_oss4_sink_get_volume (GstOss4Sink * oss)
233 int ivol, lvol, rvol;
234 gdouble dvol = DEFAULT_VOLUME;
236 GST_OBJECT_LOCK (oss);
237 if (ioctl (oss->fd, SNDCTL_DSP_GETPLAYVOL, &ivol) < 0) {
238 GST_LOG_OBJECT (oss, "GETPLAYVOL failed");
240 /* Return the higher of the two volume channels, if different */
242 rvol = (ivol >> 8) & 0xff;
243 dvol = MAX (lvol, rvol) / 100.0;
245 GST_OBJECT_UNLOCK (oss);
251 gst_oss4_sink_set_mute (GstOss4Sink * oss, gboolean mute)
257 * OSSv4 does not have a per-channel mute, so simulate by setting
258 * the value to 0. Save the volume before doing a mute so we can
259 * reset the value when the user un-mutes.
263 GST_OBJECT_LOCK (oss);
264 if (ioctl (oss->fd, SNDCTL_DSP_GETPLAYVOL, &oss->mute_volume) < 0) {
265 GST_LOG_OBJECT (oss, "GETPLAYVOL failed");
267 if (ioctl (oss->fd, SNDCTL_DSP_SETPLAYVOL, &ivol) < 0) {
268 GST_LOG_OBJECT (oss, "SETPLAYVOL failed");
270 GST_OBJECT_UNLOCK (oss);
273 * If the saved volume is 0, then reset it to 100. Otherwise the mute
274 * can get stuck. This can happen, for example, due to rounding
275 * errors in converting from the float to an integer.
277 if (oss->mute_volume == 0) {
278 oss->mute_volume = 100 | (100 << 8);
280 GST_OBJECT_LOCK (oss);
281 if (ioctl (oss->fd, SNDCTL_DSP_SETPLAYVOL, &oss->mute_volume) < 0) {
282 GST_LOG_OBJECT (oss, "SETPLAYVOL failed");
284 GST_OBJECT_UNLOCK (oss);
289 gst_oss4_sink_get_mute (GstOss4Sink * oss)
291 int ivol, lvol, rvol;
293 GST_OBJECT_LOCK (oss);
294 if (ioctl (oss->fd, SNDCTL_DSP_GETPLAYVOL, &ivol) < 0) {
295 GST_LOG_OBJECT (oss, "GETPLAYVOL failed");
299 rvol = (ivol >> 8) & 0xff;
301 GST_OBJECT_UNLOCK (oss);
303 return (lvol == 0 && rvol == 0);
307 gst_oss4_sink_set_property (GObject * object, guint prop_id,
308 const GValue * value, GParamSpec * pspec)
310 GstOss4Sink *oss = GST_OSS4_SINK (object);
314 GST_OBJECT_LOCK (oss);
316 g_free (oss->device);
317 oss->device = g_value_dup_string (value);
318 if (oss->probed_caps) {
319 gst_caps_unref (oss->probed_caps);
320 oss->probed_caps = NULL;
322 g_free (oss->device_name);
323 oss->device_name = NULL;
325 g_warning ("%s: can't change \"device\" property while audio sink "
326 "is open", GST_OBJECT_NAME (oss));
328 GST_OBJECT_UNLOCK (oss);
331 gst_oss4_sink_set_volume (oss, g_value_get_double (value));
334 gst_oss4_sink_set_mute (oss, g_value_get_boolean (value));
337 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
343 gst_oss4_sink_get_property (GObject * object, guint prop_id,
344 GValue * value, GParamSpec * pspec)
346 GstOss4Sink *oss = GST_OSS4_SINK (object);
350 GST_OBJECT_LOCK (oss);
351 g_value_set_string (value, oss->device);
352 GST_OBJECT_UNLOCK (oss);
354 case PROP_DEVICE_NAME:
355 GST_OBJECT_LOCK (oss);
356 if (oss->fd == -1 && oss->device != NULL) {
357 /* If device is set, try to retrieve the name even if we're not open */
358 if (gst_oss4_sink_open (GST_AUDIO_SINK (oss), TRUE)) {
359 g_value_set_string (value, oss->device_name);
360 gst_oss4_sink_close (GST_AUDIO_SINK (oss));
364 gst_oss4_property_probe_find_device_name_nofd (GST_OBJECT (oss),
366 g_value_set_string (value, name);
370 g_value_set_string (value, oss->device_name);
372 GST_OBJECT_UNLOCK (oss);
375 g_value_set_double (value, gst_oss4_sink_get_volume (oss));
378 g_value_set_boolean (value, gst_oss4_sink_get_mute (oss));
381 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
387 gst_oss4_sink_getcaps (GstBaseSink * bsink)
392 oss = GST_OSS4_SINK (bsink);
395 caps = gst_oss4_audio_get_template_caps ();
396 } else if (oss->probed_caps) {
397 caps = gst_caps_copy (oss->probed_caps);
399 caps = gst_oss4_audio_probe_caps (GST_OBJECT (oss), oss->fd);
400 if (caps != NULL && !gst_caps_is_empty (caps)) {
401 oss->probed_caps = gst_caps_copy (caps);
408 /* note: we must not take the object lock here unless we fix up get_property */
410 gst_oss4_sink_open (GstAudioSink * asink, gboolean silent_errors)
416 oss = GST_OSS4_SINK (asink);
419 device = g_strdup (oss->device);
421 device = gst_oss4_audio_find_device (GST_OBJECT_CAST (oss));
423 /* desperate times, desperate measures */
425 device = g_strdup ("/dev/dsp0");
427 GST_INFO_OBJECT (oss, "Trying to open OSS4 device '%s'", device);
429 /* we open in non-blocking mode even if we don't really want to do writes
430 * non-blocking because we can't be sure that this is really a genuine
431 * OSS4 device with well-behaved drivers etc. We really don't want to
432 * hang forever under any circumstances. */
433 oss->fd = open (device, O_WRONLY | O_NONBLOCK, 0);
445 GST_INFO_OBJECT (oss, "Opened device '%s'", device);
447 /* Make sure it's OSS4. If it's old OSS, let osssink handle it */
448 if (!gst_oss4_audio_check_version (GST_OBJECT_CAST (oss), oss->fd))
451 /* now remove the non-blocking flag. */
452 mode = fcntl (oss->fd, F_GETFL);
454 if (fcntl (oss->fd, F_SETFL, mode) < 0) {
455 /* some drivers do no support unsetting the non-blocking flag, try to
456 * close/open the device then. This is racy but we error out properly. */
457 GST_WARNING_OBJECT (oss, "failed to unset O_NONBLOCK (buggy driver?), "
458 "will try to re-open device now");
459 gst_oss4_sink_close (asink);
460 if ((oss->fd = open (device, O_WRONLY, 0)) == -1)
464 oss->open_device = device;
466 /* not using ENGINEINFO here because it sometimes returns a different and
467 * less useful name than AUDIOINFO for the same device */
468 if (!gst_oss4_property_probe_find_device_name (GST_OBJECT (oss), oss->fd,
469 oss->open_device, &oss->device_name)) {
470 oss->device_name = NULL;
473 /* list output routings, for informational purposes only so far */
475 oss_mixer_enuminfo routings = { 0, };
478 if (ioctl (oss->fd, SNDCTL_DSP_GET_PLAYTGT_NAMES, &routings) != -1) {
479 GST_LOG_OBJECT (oss, "%u output routings (static list: %d)",
480 routings.nvalues, !!(routings.version == 0));
481 for (i = 0; i < routings.nvalues; ++i) {
482 GST_LOG_OBJECT (oss, " output routing %d: %s", i,
483 &routings.strings[routings.strindex[i]]);
493 if (!silent_errors) {
494 GST_ELEMENT_ERROR (oss, RESOURCE, BUSY,
495 (_("Could not open audio device for playback. "
496 "Device is being used by another application.")), (NULL));
503 if (!silent_errors) {
504 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_WRITE,
505 (_("Could not open audio device for playback. "
506 "You don't have permission to open the device.")),
514 if (!silent_errors) {
515 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_WRITE,
516 (_("Could not open audio device for playback.")), GST_ERROR_SYSTEM);
523 if (!silent_errors) {
524 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_WRITE,
525 (_("Could not open audio device for playback. "
526 "This version of the Open Sound System is not supported by this "
527 "element.")), ("Try the 'osssink' element instead"));
529 gst_oss4_sink_close (asink);
535 if (!silent_errors) {
536 GST_ELEMENT_ERROR (oss, RESOURCE, SETTINGS, (NULL),
537 ("Unable to set device %s into non-blocking mode: %s",
538 oss->device, g_strerror (errno)));
546 gst_oss4_sink_open_func (GstAudioSink * asink)
548 return gst_oss4_sink_open (asink, FALSE);
552 gst_oss4_sink_close (GstAudioSink * asink)
554 GstOss4Sink *oss = GST_OSS4_SINK (asink);
557 GST_DEBUG_OBJECT (oss, "closing device");
562 oss->bytes_per_sample = 0;
563 /* we keep the probed caps cached, at least until the device changes */
565 g_free (oss->open_device);
566 oss->open_device = NULL;
568 g_free (oss->device_name);
569 oss->device_name = NULL;
571 if (oss->probed_caps) {
572 gst_caps_unref (oss->probed_caps);
573 oss->probed_caps = NULL;
580 gst_oss4_sink_prepare (GstAudioSink * asink, GstRingBufferSpec * spec)
584 oss = GST_OSS4_SINK (asink);
586 if (!gst_oss4_audio_set_format (GST_OBJECT_CAST (oss), oss->fd, spec)) {
587 GST_WARNING_OBJECT (oss, "Couldn't set requested format %" GST_PTR_FORMAT,
592 oss->bytes_per_sample = spec->bytes_per_sample;
597 gst_oss4_sink_unprepare (GstAudioSink * asink)
599 /* could do a SNDCTL_DSP_HALT, but the OSS manual recommends a close/open,
600 * since HALT won't properly reset some devices, apparently */
602 if (!gst_oss4_sink_close (asink))
605 if (!gst_oss4_sink_open_func (asink))
613 GST_DEBUG_OBJECT (asink, "Couldn't close the audio device");
618 GST_DEBUG_OBJECT (asink, "Couldn't reopen the audio device");
624 gst_oss4_sink_write (GstAudioSink * asink, gpointer data, guint length)
629 oss = GST_OSS4_SINK_CAST (asink);
631 n = write (oss->fd, data, length);
632 GST_LOG_OBJECT (asink, "wrote %d/%d samples, %d bytes",
633 n / oss->bytes_per_sample, length / oss->bytes_per_sample, n);
635 if (G_UNLIKELY (n < 0)) {
639 /* This is the most likely cause, I think */
640 GST_ELEMENT_ERROR (asink, RESOURCE, WRITE,
641 (_("Playback is not supported by this audio device.")),
642 ("write: %s (device: %s) (maybe this is an input-only device?)",
643 g_strerror (errno), oss->open_device));
647 GST_ELEMENT_ERROR (asink, RESOURCE, WRITE,
648 (_("Audio playback error.")),
649 ("write: %s (device: %s)", g_strerror (errno), oss->open_device));
659 gst_oss4_sink_delay (GstAudioSink * asink)
664 oss = GST_OSS4_SINK_CAST (asink);
666 GST_OBJECT_LOCK (oss);
667 if (ioctl (oss->fd, SNDCTL_DSP_GETODELAY, &delay) < 0 || delay < 0) {
668 GST_LOG_OBJECT (oss, "GETODELAY failed");
670 GST_OBJECT_UNLOCK (oss);
672 if (G_UNLIKELY (delay < 0)) /* error case */
675 return delay / oss->bytes_per_sample;
679 gst_oss4_sink_reset (GstAudioSink * asink)
681 /* There's nothing we can do here really: OSS can't handle access to the
682 * same device/fd from multiple threads and might deadlock or blow up in
683 * other ways if we try an ioctl SNDCTL_DSP_HALT or similar */
687 gst_oss4_sink_init_interfaces (GType type)
689 static const GInterfaceInfo svol_iface_info = {
693 g_type_add_interface_static (type, GST_TYPE_STREAM_VOLUME, &svol_iface_info);
695 gst_oss4_add_property_probe_interface (type);