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
24 * This element lets you output sound using the Open Sound System (OSS)
28 * Note that you should almost always use generic audio conversion elements
29 * like audioconvert and audioresample in front of an audiosink to make sure
30 * your pipeline works under all circumstances (those conversion elements will
31 * act in passthrough-mode if no conversion is necessary).
33 * <title>Example pipelines</title>
36 * gst-launch -v audiotestsrc ! audioconvert ! volume volume=0.1 ! oss4sink
38 * will output a sine wave (continuous beep sound) to your sound card (with
39 * a very low volume as precaution).
43 * gst-launch -v filesrc location=music.ogg ! decodebin ! audioconvert ! audioresample ! oss4sink
45 * will play an Ogg/Vorbis audio file and output it using the Open Sound System
53 /* TODO: - add "volume" property for stream volume control and intercept tags
61 #include <sys/types.h>
63 #include <sys/ioctl.h>
69 #include <gst/gst-i18n-plugin.h>
71 #define NO_LEGACY_MIXER
72 #include "oss4-audio.h"
73 #include "oss4-sink.h"
74 #include "oss4-property-probe.h"
75 #include "oss4-soundcard.h"
77 GST_DEBUG_CATEGORY_EXTERN (oss4sink_debug);
78 #define GST_CAT_DEFAULT oss4sink_debug
80 static void gst_oss4_sink_init_interfaces (GType type);
81 static void gst_oss4_sink_dispose (GObject * object);
82 static void gst_oss4_sink_finalise (GObject * object);
84 static void gst_oss4_sink_get_property (GObject * object, guint prop_id,
85 GValue * value, GParamSpec * pspec);
86 static void gst_oss4_sink_set_property (GObject * object, guint prop_id,
87 const GValue * value, GParamSpec * pspec);
89 static GstCaps *gst_oss4_sink_getcaps (GstBaseSink * bsink);
90 static gboolean gst_oss4_sink_open (GstAudioSink * asink,
91 gboolean silent_errors);
92 static gboolean gst_oss4_sink_open_func (GstAudioSink * asink);
93 static gboolean gst_oss4_sink_close (GstAudioSink * asink);
94 static gboolean gst_oss4_sink_prepare (GstAudioSink * asink,
95 GstRingBufferSpec * spec);
96 static gboolean gst_oss4_sink_unprepare (GstAudioSink * asink);
97 static guint gst_oss4_sink_write (GstAudioSink * asink, gpointer data,
99 static guint gst_oss4_sink_delay (GstAudioSink * asink);
100 static void gst_oss4_sink_reset (GstAudioSink * asink);
102 #define DEFAULT_DEVICE NULL
103 #define DEFAULT_DEVICE_NAME NULL
112 GST_BOILERPLATE_FULL (GstOss4Sink, gst_oss4_sink, GstAudioSink,
113 GST_TYPE_AUDIO_SINK, gst_oss4_sink_init_interfaces);
116 gst_oss4_sink_dispose (GObject * object)
118 GstOss4Sink *osssink = GST_OSS4_SINK (object);
120 if (osssink->probed_caps) {
121 gst_caps_unref (osssink->probed_caps);
122 osssink->probed_caps = NULL;
125 G_OBJECT_CLASS (parent_class)->dispose (object);
129 gst_oss4_sink_base_init (gpointer g_class)
131 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
132 GstPadTemplate *templ;
134 gst_element_class_set_details_simple (element_class,
135 "OSS v4 Audio Sink", "Sink/Audio",
136 "Output to a sound card via OSS version 4",
137 "Tim-Philipp Müller <tim centricular net>");
139 templ = gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
140 gst_oss4_audio_get_template_caps ());
141 gst_element_class_add_pad_template (element_class, templ);
145 gst_oss4_sink_class_init (GstOss4SinkClass * klass)
147 GstAudioSinkClass *audiosink_class = (GstAudioSinkClass *) klass;
148 GstBaseSinkClass *basesink_class = (GstBaseSinkClass *) klass;
149 GObjectClass *gobject_class = (GObjectClass *) klass;
151 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_oss4_sink_dispose);
152 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_oss4_sink_finalise);
153 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_oss4_sink_get_property);
154 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_oss4_sink_set_property);
156 g_object_class_install_property (gobject_class, PROP_DEVICE,
157 g_param_spec_string ("device", "Device",
158 "OSS4 device (e.g. /dev/oss/hdaudio0/pcm0 or /dev/dspN) "
159 "(NULL = use first available playback device)",
160 DEFAULT_DEVICE, G_PARAM_READWRITE));
162 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
163 g_param_spec_string ("device-name", "Device name",
164 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
167 basesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_oss4_sink_getcaps);
169 audiosink_class->open = GST_DEBUG_FUNCPTR (gst_oss4_sink_open_func);
170 audiosink_class->close = GST_DEBUG_FUNCPTR (gst_oss4_sink_close);
171 audiosink_class->prepare = GST_DEBUG_FUNCPTR (gst_oss4_sink_prepare);
172 audiosink_class->unprepare = GST_DEBUG_FUNCPTR (gst_oss4_sink_unprepare);
173 audiosink_class->write = GST_DEBUG_FUNCPTR (gst_oss4_sink_write);
174 audiosink_class->delay = GST_DEBUG_FUNCPTR (gst_oss4_sink_delay);
175 audiosink_class->reset = GST_DEBUG_FUNCPTR (gst_oss4_sink_reset);
179 gst_oss4_sink_init (GstOss4Sink * osssink, GstOss4SinkClass * klass)
183 device = g_getenv ("AUDIODEV");
185 device = DEFAULT_DEVICE;
186 osssink->device = g_strdup (device);
188 osssink->bytes_per_sample = 0;
189 osssink->probed_caps = NULL;
190 osssink->device_name = NULL;
194 gst_oss4_sink_finalise (GObject * object)
196 GstOss4Sink *osssink = GST_OSS4_SINK (object);
198 g_free (osssink->device);
199 osssink->device = NULL;
201 g_list_free (osssink->property_probe_list);
202 osssink->property_probe_list = NULL;
204 G_OBJECT_CLASS (parent_class)->finalize (object);
208 gst_oss4_sink_set_property (GObject * object, guint prop_id,
209 const GValue * value, GParamSpec * pspec)
211 GstOss4Sink *oss = GST_OSS4_SINK (object);
215 GST_OBJECT_LOCK (oss);
217 g_free (oss->device);
218 oss->device = g_value_dup_string (value);
219 if (oss->probed_caps) {
220 gst_caps_unref (oss->probed_caps);
221 oss->probed_caps = NULL;
223 g_free (oss->device_name);
224 oss->device_name = NULL;
226 g_warning ("%s: can't change \"device\" property while audio sink "
227 "is open", GST_OBJECT_NAME (oss));
229 GST_OBJECT_UNLOCK (oss);
232 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
238 gst_oss4_sink_get_property (GObject * object, guint prop_id,
239 GValue * value, GParamSpec * pspec)
241 GstOss4Sink *oss = GST_OSS4_SINK (object);
245 GST_OBJECT_LOCK (oss);
246 g_value_set_string (value, oss->device);
247 GST_OBJECT_UNLOCK (oss);
249 case PROP_DEVICE_NAME:
250 GST_OBJECT_LOCK (oss);
251 if (oss->fd == -1 && oss->device != NULL) {
252 /* If device is set, try to retrieve the name even if we're not open */
253 if (gst_oss4_sink_open (GST_AUDIO_SINK (oss), TRUE)) {
254 g_value_set_string (value, oss->device_name);
255 gst_oss4_sink_close (GST_AUDIO_SINK (oss));
259 gst_oss4_property_probe_find_device_name_nofd (GST_OBJECT (oss),
261 g_value_set_string (value, name);
265 g_value_set_string (value, oss->device_name);
267 GST_OBJECT_UNLOCK (oss);
270 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
276 gst_oss4_sink_getcaps (GstBaseSink * bsink)
281 oss = GST_OSS4_SINK (bsink);
284 caps = gst_caps_copy (gst_oss4_audio_get_template_caps ());
285 } else if (oss->probed_caps) {
286 caps = gst_caps_copy (oss->probed_caps);
288 caps = gst_oss4_audio_probe_caps (GST_OBJECT (oss), oss->fd);
289 if (caps != NULL && !gst_caps_is_empty (caps)) {
290 oss->probed_caps = gst_caps_copy (caps);
297 /* note: we must not take the object lock here unless we fix up get_property */
299 gst_oss4_sink_open (GstAudioSink * asink, gboolean silent_errors)
305 oss = GST_OSS4_SINK (asink);
308 device = g_strdup (oss->device);
310 device = gst_oss4_audio_find_device (GST_OBJECT_CAST (oss));
312 /* desperate times, desperate measures */
314 device = g_strdup ("/dev/dsp0");
316 GST_INFO_OBJECT (oss, "Trying to open OSS4 device '%s'", device);
318 /* we open in non-blocking mode even if we don't really want to do writes
319 * non-blocking because we can't be sure that this is really a genuine
320 * OSS4 device with well-behaved drivers etc. We really don't want to
321 * hang forever under any circumstances. */
322 oss->fd = open (device, O_WRONLY | O_NONBLOCK, 0);
334 GST_INFO_OBJECT (oss, "Opened device '%s'", device);
336 /* Make sure it's OSS4. If it's old OSS, let osssink handle it */
337 if (!gst_oss4_audio_check_version (GST_OBJECT_CAST (oss), oss->fd))
340 /* now remove the non-blocking flag. */
341 mode = fcntl (oss->fd, F_GETFL);
343 if (fcntl (oss->fd, F_SETFL, mode) < 0) {
344 /* some drivers do no support unsetting the non-blocking flag, try to
345 * close/open the device then. This is racy but we error out properly. */
346 GST_WARNING_OBJECT (oss, "failed to unset O_NONBLOCK (buggy driver?), "
347 "will try to re-open device now");
348 gst_oss4_sink_close (asink);
349 if ((oss->fd = open (device, O_WRONLY, 0)) == -1)
353 oss->open_device = device;
355 /* not using ENGINEINFO here because it sometimes returns a different and
356 * less useful name than AUDIOINFO for the same device */
357 if (!gst_oss4_property_probe_find_device_name (GST_OBJECT (oss), oss->fd,
358 oss->open_device, &oss->device_name)) {
359 oss->device_name = NULL;
362 /* list output routings, for informational purposes only so far */
364 oss_mixer_enuminfo routings = { 0, };
367 if (ioctl (oss->fd, SNDCTL_DSP_GET_PLAYTGT_NAMES, &routings) != -1) {
368 GST_LOG_OBJECT (oss, "%u output routings (static list: %d)",
369 routings.nvalues, !!(routings.version == 0));
370 for (i = 0; i < routings.nvalues; ++i) {
371 GST_LOG_OBJECT (oss, " output routing %d: %s", i,
372 &routings.strings[routings.strindex[i]]);
382 if (!silent_errors) {
383 GST_ELEMENT_ERROR (oss, RESOURCE, BUSY,
384 (_("Could not open audio device for playback. "
385 "Device is being used by another application.")), (NULL));
392 if (!silent_errors) {
393 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_WRITE,
394 (_("Could not open audio device for playback."
395 "You don't have permission to open the device.")),
403 if (!silent_errors) {
404 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_WRITE,
405 (_("Could not open audio device for playback.")), GST_ERROR_SYSTEM);
412 if (!silent_errors) {
413 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_WRITE,
414 (_("Could not open audio device for playback."
415 "This version of the Open Sound System is not supported by this "
416 "element.")), ("Try the 'osssink' element instead"));
418 gst_oss4_sink_close (asink);
424 if (!silent_errors) {
425 GST_ELEMENT_ERROR (oss, RESOURCE, SETTINGS, (NULL),
426 ("Unable to set device %s into non-blocking mode: %s",
427 oss->device, g_strerror (errno)));
435 gst_oss4_sink_open_func (GstAudioSink * asink)
437 return gst_oss4_sink_open (asink, FALSE);
441 gst_oss4_sink_close (GstAudioSink * asink)
443 GstOss4Sink *oss = GST_OSS4_SINK (asink);
446 GST_DEBUG_OBJECT (oss, "closing device");
451 oss->bytes_per_sample = 0;
452 /* we keep the probed caps cached, at least until the device changes */
454 g_free (oss->open_device);
455 oss->open_device = NULL;
457 g_free (oss->device_name);
458 oss->device_name = NULL;
460 if (oss->probed_caps) {
461 gst_caps_unref (oss->probed_caps);
462 oss->probed_caps = NULL;
469 gst_oss4_sink_prepare (GstAudioSink * asink, GstRingBufferSpec * spec)
473 oss = GST_OSS4_SINK (asink);
475 if (!gst_oss4_audio_set_format (GST_OBJECT_CAST (oss), oss->fd, spec)) {
476 GST_WARNING_OBJECT (oss, "Couldn't set requested format %" GST_PTR_FORMAT,
481 oss->bytes_per_sample = spec->bytes_per_sample;
486 gst_oss4_sink_unprepare (GstAudioSink * asink)
488 /* could do a SNDCTL_DSP_HALT, but the OSS manual recommends a close/open,
489 * since HALT won't properly reset some devices, apparently */
491 if (!gst_oss4_sink_close (asink))
494 if (!gst_oss4_sink_open_func (asink))
502 GST_DEBUG_OBJECT (asink, "Couldn't close the audio device");
507 GST_DEBUG_OBJECT (asink, "Couldn't reopen the audio device");
513 gst_oss4_sink_write (GstAudioSink * asink, gpointer data, guint length)
518 oss = GST_OSS4_SINK_CAST (asink);
520 n = write (oss->fd, data, length);
521 GST_LOG_OBJECT (asink, "wrote %d/%d samples, %d bytes",
522 n / oss->bytes_per_sample, length / oss->bytes_per_sample, n);
524 if (G_UNLIKELY (n < 0)) {
528 /* This is the most likely cause, I think */
529 GST_ELEMENT_ERROR (asink, RESOURCE, WRITE,
530 (_("Playback is not supported by this audio device.")),
531 ("write: %s (device: %s) (maybe this is an input-only device?)",
532 g_strerror (errno), oss->open_device));
536 GST_ELEMENT_ERROR (asink, RESOURCE, WRITE,
537 (_("Audio playback error.")),
538 ("write: %s (device: %s)", g_strerror (errno), oss->open_device));
548 gst_oss4_sink_delay (GstAudioSink * asink)
553 oss = GST_OSS4_SINK_CAST (asink);
555 if (ioctl (oss->fd, SNDCTL_DSP_GETODELAY, &delay) < 0 || delay < 0) {
556 GST_LOG_OBJECT (oss, "GETODELAY failed");
560 return delay / oss->bytes_per_sample;
564 gst_oss4_sink_reset (GstAudioSink * asink)
566 /* There's nothing we can do here really: OSS can't handle access to the
567 * same device/fd from multiple threads and might deadlock or blow up in
568 * other ways if we try an ioctl SNDCTL_DSP_HALT or similar */
572 gst_oss4_sink_init_interfaces (GType type)
574 gst_oss4_add_property_probe_interface (type);