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>
63 #define NO_LEGACY_MIXER
64 #include "oss4-audio.h"
65 #include "oss4-sink.h"
66 #include "oss4-property-probe.h"
67 #include "oss4-soundcard.h"
69 GST_DEBUG_CATEGORY_EXTERN (oss4sink_debug);
70 #define GST_CAT_DEFAULT oss4sink_debug
72 static void gst_oss4_sink_init_interfaces (GType type);
73 static void gst_oss4_sink_dispose (GObject * object);
74 static void gst_oss4_sink_finalise (GObject * object);
76 static void gst_oss4_sink_get_property (GObject * object, guint prop_id,
77 GValue * value, GParamSpec * pspec);
78 static void gst_oss4_sink_set_property (GObject * object, guint prop_id,
79 const GValue * value, GParamSpec * pspec);
81 static GstCaps *gst_oss4_sink_getcaps (GstBaseSink * bsink);
82 static gboolean gst_oss4_sink_open (GstAudioSink * asink,
83 gboolean silent_errors);
84 static gboolean gst_oss4_sink_open_func (GstAudioSink * asink);
85 static gboolean gst_oss4_sink_close (GstAudioSink * asink);
86 static gboolean gst_oss4_sink_prepare (GstAudioSink * asink,
87 GstRingBufferSpec * spec);
88 static gboolean gst_oss4_sink_unprepare (GstAudioSink * asink);
89 static guint gst_oss4_sink_write (GstAudioSink * asink, gpointer data,
91 static guint gst_oss4_sink_delay (GstAudioSink * asink);
92 static void gst_oss4_sink_reset (GstAudioSink * asink);
94 #define DEFAULT_DEVICE NULL
95 #define DEFAULT_DEVICE_NAME NULL
104 GST_BOILERPLATE_FULL (GstOss4Sink, gst_oss4_sink, GstAudioSink,
105 GST_TYPE_AUDIO_SINK, gst_oss4_sink_init_interfaces);
108 gst_oss4_sink_dispose (GObject * object)
110 GstOss4Sink *osssink = GST_OSS4_SINK (object);
112 if (osssink->probed_caps) {
113 gst_caps_unref (osssink->probed_caps);
114 osssink->probed_caps = NULL;
117 G_OBJECT_CLASS (parent_class)->dispose (object);
121 gst_oss4_sink_base_init (gpointer g_class)
123 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
124 GstPadTemplate *templ;
126 gst_element_class_set_details_simple (element_class,
127 "OSS v4 Audio Sink", "Sink/Audio",
128 "Output to a sound card via OSS version 4",
129 "Tim-Philipp Müller <tim centricular net>");
131 templ = gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
132 gst_oss4_audio_get_template_caps ());
133 gst_element_class_add_pad_template (element_class, templ);
137 gst_oss4_sink_class_init (GstOss4SinkClass * klass)
139 GstAudioSinkClass *audiosink_class = (GstAudioSinkClass *) klass;
140 GstBaseSinkClass *basesink_class = (GstBaseSinkClass *) klass;
141 GObjectClass *gobject_class = (GObjectClass *) klass;
143 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_oss4_sink_dispose);
144 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_oss4_sink_finalise);
145 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_oss4_sink_get_property);
146 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_oss4_sink_set_property);
148 g_object_class_install_property (gobject_class, PROP_DEVICE,
149 g_param_spec_string ("device", "Device",
150 "OSS4 device (e.g. /dev/oss/hdaudio0/pcm0 or /dev/dspN) "
151 "(NULL = use first available playback device)",
152 DEFAULT_DEVICE, G_PARAM_READWRITE));
154 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
155 g_param_spec_string ("device-name", "Device name",
156 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
159 basesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_oss4_sink_getcaps);
161 audiosink_class->open = GST_DEBUG_FUNCPTR (gst_oss4_sink_open_func);
162 audiosink_class->close = GST_DEBUG_FUNCPTR (gst_oss4_sink_close);
163 audiosink_class->prepare = GST_DEBUG_FUNCPTR (gst_oss4_sink_prepare);
164 audiosink_class->unprepare = GST_DEBUG_FUNCPTR (gst_oss4_sink_unprepare);
165 audiosink_class->write = GST_DEBUG_FUNCPTR (gst_oss4_sink_write);
166 audiosink_class->delay = GST_DEBUG_FUNCPTR (gst_oss4_sink_delay);
167 audiosink_class->reset = GST_DEBUG_FUNCPTR (gst_oss4_sink_reset);
171 gst_oss4_sink_init (GstOss4Sink * osssink, GstOss4SinkClass * klass)
175 device = g_getenv ("AUDIODEV");
177 device = DEFAULT_DEVICE;
178 osssink->device = g_strdup (device);
180 osssink->bytes_per_sample = 0;
181 osssink->probed_caps = NULL;
182 osssink->device_name = NULL;
186 gst_oss4_sink_finalise (GObject * object)
188 GstOss4Sink *osssink = GST_OSS4_SINK (object);
190 g_free (osssink->device);
191 osssink->device = NULL;
193 g_list_free (osssink->property_probe_list);
194 osssink->property_probe_list = NULL;
196 G_OBJECT_CLASS (parent_class)->finalize (object);
200 gst_oss4_sink_set_property (GObject * object, guint prop_id,
201 const GValue * value, GParamSpec * pspec)
203 GstOss4Sink *oss = GST_OSS4_SINK (object);
207 GST_OBJECT_LOCK (oss);
209 g_free (oss->device);
210 oss->device = g_value_dup_string (value);
211 if (oss->probed_caps) {
212 gst_caps_unref (oss->probed_caps);
213 oss->probed_caps = NULL;
215 g_free (oss->device_name);
216 oss->device_name = NULL;
218 g_warning ("%s: can't change \"device\" property while audio sink "
219 "is open", GST_OBJECT_NAME (oss));
221 GST_OBJECT_UNLOCK (oss);
224 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
230 gst_oss4_sink_get_property (GObject * object, guint prop_id,
231 GValue * value, GParamSpec * pspec)
233 GstOss4Sink *oss = GST_OSS4_SINK (object);
237 GST_OBJECT_LOCK (oss);
238 g_value_set_string (value, oss->device);
239 GST_OBJECT_UNLOCK (oss);
241 case PROP_DEVICE_NAME:
242 GST_OBJECT_LOCK (oss);
243 if (oss->fd == -1 && oss->device != NULL) {
244 /* If device is set, try to retrieve the name even if we're not open */
245 if (gst_oss4_sink_open (GST_AUDIO_SINK (oss), TRUE)) {
246 g_value_set_string (value, oss->device_name);
247 gst_oss4_sink_close (GST_AUDIO_SINK (oss));
251 gst_oss4_property_probe_find_device_name_nofd (GST_OBJECT (oss),
253 g_value_set_string (value, name);
257 g_value_set_string (value, oss->device_name);
259 GST_OBJECT_UNLOCK (oss);
262 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
268 gst_oss4_sink_getcaps (GstBaseSink * bsink)
273 oss = GST_OSS4_SINK (bsink);
276 caps = gst_caps_copy (gst_oss4_audio_get_template_caps ());
277 } else if (oss->probed_caps) {
278 caps = gst_caps_copy (oss->probed_caps);
280 caps = gst_oss4_audio_probe_caps (GST_OBJECT (oss), oss->fd);
281 if (caps != NULL && !gst_caps_is_empty (caps)) {
282 oss->probed_caps = gst_caps_copy (caps);
289 /* note: we must not take the object lock here unless we fix up get_property */
291 gst_oss4_sink_open (GstAudioSink * asink, gboolean silent_errors)
297 oss = GST_OSS4_SINK (asink);
300 device = g_strdup (oss->device);
302 device = gst_oss4_audio_find_device (GST_OBJECT_CAST (oss));
304 /* desperate times, desperate measures */
306 device = g_strdup ("/dev/dsp0");
308 GST_INFO_OBJECT (oss, "Trying to open OSS4 device '%s'", device);
310 /* we open in non-blocking mode even if we don't really want to do writes
311 * non-blocking because we can't be sure that this is really a genuine
312 * OSS4 device with well-behaved drivers etc. We really don't want to
313 * hang forever under any circumstances. */
314 oss->fd = open (device, O_WRONLY | O_NONBLOCK, 0);
326 GST_INFO_OBJECT (oss, "Opened device '%s'", device);
328 /* Make sure it's OSS4. If it's old OSS, let osssink handle it */
329 if (!gst_oss4_audio_check_version (GST_OBJECT_CAST (oss), oss->fd))
332 /* now remove the non-blocking flag. */
333 mode = fcntl (oss->fd, F_GETFL);
335 if (fcntl (oss->fd, F_SETFL, mode) < 0) {
336 /* some drivers do no support unsetting the non-blocking flag, try to
337 * close/open the device then. This is racy but we error out properly. */
338 GST_WARNING_OBJECT (oss, "failed to unset O_NONBLOCK (buggy driver?), "
339 "will try to re-open device now");
340 gst_oss4_sink_close (asink);
341 if ((oss->fd = open (device, O_WRONLY, 0)) == -1)
345 oss->open_device = device;
347 /* not using ENGINEINFO here because it sometimes returns a different and
348 * less useful name than AUDIOINFO for the same device */
349 if (!gst_oss4_property_probe_find_device_name (GST_OBJECT (oss), oss->fd,
350 oss->open_device, &oss->device_name)) {
351 oss->device_name = NULL;
354 /* list output routings, for informational purposes only so far */
356 oss_mixer_enuminfo routings = { 0, };
359 if (ioctl (oss->fd, SNDCTL_DSP_GET_PLAYTGT_NAMES, &routings) != -1) {
360 GST_LOG_OBJECT (oss, "%u output routings (static list: %d)",
361 routings.nvalues, !!(routings.version == 0));
362 for (i = 0; i < routings.nvalues; ++i) {
363 GST_LOG_OBJECT (oss, " output routing %d: %s", i,
364 &routings.strings[routings.strindex[i]]);
374 if (!silent_errors) {
375 GST_ELEMENT_ERROR (oss, RESOURCE, BUSY,
376 (_("Could not open audio device for playback. "
377 "Device is being used by another application.")), (NULL));
384 if (!silent_errors) {
385 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_WRITE,
386 (_("Could not open audio device for playback. "
387 "You don't have permission to open the device.")),
395 if (!silent_errors) {
396 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_WRITE,
397 (_("Could not open audio device for playback.")), GST_ERROR_SYSTEM);
404 if (!silent_errors) {
405 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_WRITE,
406 (_("Could not open audio device for playback. "
407 "This version of the Open Sound System is not supported by this "
408 "element.")), ("Try the 'osssink' element instead"));
410 gst_oss4_sink_close (asink);
416 if (!silent_errors) {
417 GST_ELEMENT_ERROR (oss, RESOURCE, SETTINGS, (NULL),
418 ("Unable to set device %s into non-blocking mode: %s",
419 oss->device, g_strerror (errno)));
427 gst_oss4_sink_open_func (GstAudioSink * asink)
429 return gst_oss4_sink_open (asink, FALSE);
433 gst_oss4_sink_close (GstAudioSink * asink)
435 GstOss4Sink *oss = GST_OSS4_SINK (asink);
438 GST_DEBUG_OBJECT (oss, "closing device");
443 oss->bytes_per_sample = 0;
444 /* we keep the probed caps cached, at least until the device changes */
446 g_free (oss->open_device);
447 oss->open_device = NULL;
449 g_free (oss->device_name);
450 oss->device_name = NULL;
452 if (oss->probed_caps) {
453 gst_caps_unref (oss->probed_caps);
454 oss->probed_caps = NULL;
461 gst_oss4_sink_prepare (GstAudioSink * asink, GstRingBufferSpec * spec)
465 oss = GST_OSS4_SINK (asink);
467 if (!gst_oss4_audio_set_format (GST_OBJECT_CAST (oss), oss->fd, spec)) {
468 GST_WARNING_OBJECT (oss, "Couldn't set requested format %" GST_PTR_FORMAT,
473 oss->bytes_per_sample = spec->bytes_per_sample;
478 gst_oss4_sink_unprepare (GstAudioSink * asink)
480 /* could do a SNDCTL_DSP_HALT, but the OSS manual recommends a close/open,
481 * since HALT won't properly reset some devices, apparently */
483 if (!gst_oss4_sink_close (asink))
486 if (!gst_oss4_sink_open_func (asink))
494 GST_DEBUG_OBJECT (asink, "Couldn't close the audio device");
499 GST_DEBUG_OBJECT (asink, "Couldn't reopen the audio device");
505 gst_oss4_sink_write (GstAudioSink * asink, gpointer data, guint length)
510 oss = GST_OSS4_SINK_CAST (asink);
512 n = write (oss->fd, data, length);
513 GST_LOG_OBJECT (asink, "wrote %d/%d samples, %d bytes",
514 n / oss->bytes_per_sample, length / oss->bytes_per_sample, n);
516 if (G_UNLIKELY (n < 0)) {
520 /* This is the most likely cause, I think */
521 GST_ELEMENT_ERROR (asink, RESOURCE, WRITE,
522 (_("Playback is not supported by this audio device.")),
523 ("write: %s (device: %s) (maybe this is an input-only device?)",
524 g_strerror (errno), oss->open_device));
528 GST_ELEMENT_ERROR (asink, RESOURCE, WRITE,
529 (_("Audio playback error.")),
530 ("write: %s (device: %s)", g_strerror (errno), oss->open_device));
540 gst_oss4_sink_delay (GstAudioSink * asink)
545 oss = GST_OSS4_SINK_CAST (asink);
547 if (ioctl (oss->fd, SNDCTL_DSP_GETODELAY, &delay) < 0 || delay < 0) {
548 GST_LOG_OBJECT (oss, "GETODELAY failed");
552 return delay / oss->bytes_per_sample;
556 gst_oss4_sink_reset (GstAudioSink * asink)
558 /* There's nothing we can do here really: OSS can't handle access to the
559 * same device/fd from multiple threads and might deadlock or blow up in
560 * other ways if we try an ioctl SNDCTL_DSP_HALT or similar */
564 gst_oss4_sink_init_interfaces (GType type)
566 gst_oss4_add_property_probe_interface (type);