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
21 * @short_description: output sound using OSS4
25 * This element lets you output sound using the Open Sound System (OSS)
29 * Note that you should almost always use generic audio conversion elements
30 * like audioconvert and audioresample in front of an audiosink to make sure
31 * your pipeline works under all circumstances (those conversion elements will
32 * act in passthrough-mode if no conversion is necessary).
34 * <title>Example pipelines</title>
37 * gst-launch -v audiotestsrc ! audioconvert ! volume volume=0.1 ! oss4sink
39 * will output a sine wave (continuous beep sound) to your sound card (with
40 * a very low volume as precaution).
44 * gst-launch -v filesrc location=music.ogg ! decodebin ! audioconvert ! audioresample ! oss4sink
46 * will play an Ogg/Vorbis audio file and output it using the Open Sound System
54 /* TODO: - add "volume" property for stream volume control and intercept tags
62 #include <sys/types.h>
64 #include <sys/ioctl.h>
70 #include <gst/gst-i18n-plugin.h>
72 #define NO_LEGACY_MIXER
73 #include "oss4-audio.h"
74 #include "oss4-sink.h"
75 #include "oss4-property-probe.h"
76 #include "oss4-soundcard.h"
78 GST_DEBUG_CATEGORY_EXTERN (oss4sink_debug);
79 #define GST_CAT_DEFAULT oss4sink_debug
81 static void gst_oss4_sink_init_interfaces (GType type);
82 static void gst_oss4_sink_dispose (GObject * object);
83 static void gst_oss4_sink_finalise (GObject * object);
85 static void gst_oss4_sink_get_property (GObject * object, guint prop_id,
86 GValue * value, GParamSpec * pspec);
87 static void gst_oss4_sink_set_property (GObject * object, guint prop_id,
88 const GValue * value, GParamSpec * pspec);
90 static GstCaps *gst_oss4_sink_getcaps (GstBaseSink * bsink);
91 static gboolean gst_oss4_sink_open (GstAudioSink * asink,
92 gboolean silent_errors);
93 static gboolean gst_oss4_sink_open_func (GstAudioSink * asink);
94 static gboolean gst_oss4_sink_close (GstAudioSink * asink);
95 static gboolean gst_oss4_sink_prepare (GstAudioSink * asink,
96 GstRingBufferSpec * spec);
97 static gboolean gst_oss4_sink_unprepare (GstAudioSink * asink);
98 static guint gst_oss4_sink_write (GstAudioSink * asink, gpointer data,
100 static guint gst_oss4_sink_delay (GstAudioSink * asink);
101 static void gst_oss4_sink_reset (GstAudioSink * asink);
103 #define DEFAULT_DEVICE NULL
104 #define DEFAULT_DEVICE_NAME NULL
113 GST_BOILERPLATE_FULL (GstOss4Sink, gst_oss4_sink, GstAudioSink,
114 GST_TYPE_AUDIO_SINK, gst_oss4_sink_init_interfaces);
117 gst_oss4_sink_dispose (GObject * object)
119 GstOss4Sink *osssink = GST_OSS4_SINK (object);
121 if (osssink->probed_caps) {
122 gst_caps_unref (osssink->probed_caps);
123 osssink->probed_caps = NULL;
126 G_OBJECT_CLASS (parent_class)->dispose (object);
130 gst_oss4_sink_base_init (gpointer g_class)
132 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
133 GstPadTemplate *templ;
135 gst_element_class_set_details_simple (element_class,
136 "OSS v4 Audio Sink", "Sink/Audio",
137 "Output to a sound card via OSS version 4",
138 "Tim-Philipp Müller <tim centricular net>");
140 templ = gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
141 gst_oss4_audio_get_template_caps ());
142 gst_element_class_add_pad_template (element_class, templ);
146 gst_oss4_sink_class_init (GstOss4SinkClass * klass)
148 GstAudioSinkClass *audiosink_class = (GstAudioSinkClass *) klass;
149 GstBaseSinkClass *basesink_class = (GstBaseSinkClass *) klass;
150 GObjectClass *gobject_class = (GObjectClass *) klass;
152 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_oss4_sink_dispose);
153 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_oss4_sink_finalise);
154 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_oss4_sink_get_property);
155 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_oss4_sink_set_property);
157 g_object_class_install_property (gobject_class, PROP_DEVICE,
158 g_param_spec_string ("device", "Device",
159 "OSS4 device (e.g. /dev/oss/hdaudio0/pcm0 or /dev/dspN) "
160 "(NULL = use first available playback device)",
161 DEFAULT_DEVICE, G_PARAM_READWRITE));
163 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
164 g_param_spec_string ("device-name", "Device name",
165 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
168 basesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_oss4_sink_getcaps);
170 audiosink_class->open = GST_DEBUG_FUNCPTR (gst_oss4_sink_open_func);
171 audiosink_class->close = GST_DEBUG_FUNCPTR (gst_oss4_sink_close);
172 audiosink_class->prepare = GST_DEBUG_FUNCPTR (gst_oss4_sink_prepare);
173 audiosink_class->unprepare = GST_DEBUG_FUNCPTR (gst_oss4_sink_unprepare);
174 audiosink_class->write = GST_DEBUG_FUNCPTR (gst_oss4_sink_write);
175 audiosink_class->delay = GST_DEBUG_FUNCPTR (gst_oss4_sink_delay);
176 audiosink_class->reset = GST_DEBUG_FUNCPTR (gst_oss4_sink_reset);
180 gst_oss4_sink_init (GstOss4Sink * osssink, GstOss4SinkClass * klass)
184 device = g_getenv ("AUDIODEV");
186 device = DEFAULT_DEVICE;
187 osssink->device = g_strdup (device);
189 osssink->bytes_per_sample = 0;
190 osssink->probed_caps = NULL;
191 osssink->device_name = NULL;
195 gst_oss4_sink_finalise (GObject * object)
197 GstOss4Sink *osssink = GST_OSS4_SINK (object);
199 g_free (osssink->device);
200 osssink->device = NULL;
202 g_list_free (osssink->property_probe_list);
203 osssink->property_probe_list = NULL;
205 G_OBJECT_CLASS (parent_class)->finalize (object);
209 gst_oss4_sink_set_property (GObject * object, guint prop_id,
210 const GValue * value, GParamSpec * pspec)
212 GstOss4Sink *oss = GST_OSS4_SINK (object);
216 GST_OBJECT_LOCK (oss);
218 g_free (oss->device);
219 oss->device = g_value_dup_string (value);
220 if (oss->probed_caps) {
221 gst_caps_unref (oss->probed_caps);
222 oss->probed_caps = NULL;
224 g_free (oss->device_name);
225 oss->device_name = NULL;
227 g_warning ("%s: can't change \"device\" property while audio sink "
228 "is open", GST_OBJECT_NAME (oss));
230 GST_OBJECT_UNLOCK (oss);
233 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
239 gst_oss4_sink_get_property (GObject * object, guint prop_id,
240 GValue * value, GParamSpec * pspec)
242 GstOss4Sink *oss = GST_OSS4_SINK (object);
246 GST_OBJECT_LOCK (oss);
247 g_value_set_string (value, oss->device);
248 GST_OBJECT_UNLOCK (oss);
250 case PROP_DEVICE_NAME:
251 GST_OBJECT_LOCK (oss);
252 if (oss->fd == -1 && oss->device != NULL) {
253 /* If device is set, try to retrieve the name even if we're not open */
254 if (gst_oss4_sink_open (GST_AUDIO_SINK (oss), TRUE)) {
255 g_value_set_string (value, oss->device_name);
256 gst_oss4_sink_close (GST_AUDIO_SINK (oss));
260 gst_oss4_property_probe_find_device_name_nofd (GST_OBJECT (oss),
262 g_value_set_string (value, name);
266 g_value_set_string (value, oss->device_name);
268 GST_OBJECT_UNLOCK (oss);
271 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
277 gst_oss4_sink_getcaps (GstBaseSink * bsink)
282 oss = GST_OSS4_SINK (bsink);
285 caps = gst_caps_copy (gst_oss4_audio_get_template_caps ());
286 } else if (oss->probed_caps) {
287 caps = gst_caps_copy (oss->probed_caps);
289 caps = gst_oss4_audio_probe_caps (GST_OBJECT (oss), oss->fd);
290 if (caps != NULL && !gst_caps_is_empty (caps)) {
291 oss->probed_caps = gst_caps_copy (caps);
298 /* note: we must not take the object lock here unless we fix up get_property */
300 gst_oss4_sink_open (GstAudioSink * asink, gboolean silent_errors)
306 oss = GST_OSS4_SINK (asink);
309 device = g_strdup (oss->device);
311 device = gst_oss4_audio_find_device (GST_OBJECT_CAST (oss));
313 /* desperate times, desperate measures */
315 device = g_strdup ("/dev/dsp0");
317 GST_INFO_OBJECT (oss, "Trying to open OSS4 device '%s'", device);
319 /* we open in non-blocking mode even if we don't really want to do writes
320 * non-blocking because we can't be sure that this is really a genuine
321 * OSS4 device with well-behaved drivers etc. We really don't want to
322 * hang forever under any circumstances. */
323 oss->fd = open (device, O_WRONLY | O_NONBLOCK, 0);
335 GST_INFO_OBJECT (oss, "Opened device '%s'", device);
337 /* Make sure it's OSS4. If it's old OSS, let osssink handle it */
338 if (!gst_oss4_audio_check_version (GST_OBJECT_CAST (oss), oss->fd))
341 /* now remove the non-blocking flag. */
342 mode = fcntl (oss->fd, F_GETFL);
344 if (fcntl (oss->fd, F_SETFL, mode) < 0) {
345 /* some drivers do no support unsetting the non-blocking flag, try to
346 * close/open the device then. This is racy but we error out properly. */
347 GST_WARNING_OBJECT (oss, "failed to unset O_NONBLOCK (buggy driver?), "
348 "will try to re-open device now");
349 gst_oss4_sink_close (asink);
350 if ((oss->fd = open (device, O_WRONLY, 0)) == -1)
354 oss->open_device = device;
356 /* not using ENGINEINFO here because it sometimes returns a different and
357 * less useful name than AUDIOINFO for the same device */
358 if (!gst_oss4_property_probe_find_device_name (GST_OBJECT (oss), oss->fd,
359 oss->open_device, &oss->device_name)) {
360 oss->device_name = NULL;
363 /* list output routings, for informational purposes only so far */
365 oss_mixer_enuminfo routings = { 0, };
368 if (ioctl (oss->fd, SNDCTL_DSP_GET_PLAYTGT_NAMES, &routings) != -1) {
369 GST_LOG_OBJECT (oss, "%u output routings (static list: %d)",
370 routings.nvalues, !!(routings.version == 0));
371 for (i = 0; i < routings.nvalues; ++i) {
372 GST_LOG_OBJECT (oss, " output routing %d: %s", i,
373 &routings.strings[routings.strindex[i]]);
383 if (!silent_errors) {
384 GST_ELEMENT_ERROR (oss, RESOURCE, BUSY,
385 (_("Could not open audio device for playback. "
386 "Device is being used by another application.")), (NULL));
393 if (!silent_errors) {
394 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_WRITE,
395 (_("Could not open audio device for playback."
396 "You don't have permission to open the device.")),
404 if (!silent_errors) {
405 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_WRITE,
406 (_("Could not open audio device for playback.")), GST_ERROR_SYSTEM);
413 if (!silent_errors) {
414 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_WRITE,
415 (_("Could not open audio device for playback."
416 "This version of the Open Sound System is not supported by this "
417 "element.")), ("Try the 'osssink' element instead"));
419 gst_oss4_sink_close (asink);
425 if (!silent_errors) {
426 GST_ELEMENT_ERROR (oss, RESOURCE, SETTINGS, (NULL),
427 ("Unable to set device %s into non-blocking mode: %s",
428 oss->device, g_strerror (errno)));
436 gst_oss4_sink_open_func (GstAudioSink * asink)
438 return gst_oss4_sink_open (asink, FALSE);
442 gst_oss4_sink_close (GstAudioSink * asink)
444 GstOss4Sink *oss = GST_OSS4_SINK (asink);
447 GST_DEBUG_OBJECT (oss, "closing device");
452 oss->bytes_per_sample = 0;
453 /* we keep the probed caps cached, at least until the device changes */
455 g_free (oss->open_device);
456 oss->open_device = NULL;
458 g_free (oss->device_name);
459 oss->device_name = NULL;
461 if (oss->probed_caps) {
462 gst_caps_unref (oss->probed_caps);
463 oss->probed_caps = NULL;
470 gst_oss4_sink_prepare (GstAudioSink * asink, GstRingBufferSpec * spec)
474 oss = GST_OSS4_SINK (asink);
476 if (!gst_oss4_audio_set_format (GST_OBJECT_CAST (oss), oss->fd, spec)) {
477 GST_WARNING_OBJECT (oss, "Couldn't set requested format %" GST_PTR_FORMAT,
482 oss->bytes_per_sample = spec->bytes_per_sample;
487 gst_oss4_sink_unprepare (GstAudioSink * asink)
489 /* could do a SNDCTL_DSP_HALT, but the OSS manual recommends a close/open,
490 * since HALT won't properly reset some devices, apparently */
492 if (!gst_oss4_sink_close (asink))
495 if (!gst_oss4_sink_open_func (asink))
503 GST_DEBUG_OBJECT (asink, "Couldn't close the audio device");
508 GST_DEBUG_OBJECT (asink, "Couldn't reopen the audio device");
514 gst_oss4_sink_write (GstAudioSink * asink, gpointer data, guint length)
519 oss = GST_OSS4_SINK_CAST (asink);
521 n = write (oss->fd, data, length);
522 GST_LOG_OBJECT (asink, "wrote %d/%d samples, %d bytes",
523 n / oss->bytes_per_sample, length / oss->bytes_per_sample, n);
525 if (G_UNLIKELY (n < 0)) {
529 /* This is the most likely cause, I think */
530 GST_ELEMENT_ERROR (asink, RESOURCE, WRITE,
531 (_("Playback is not supported by this audio device.")),
532 ("write: %s (device: %s) (maybe this is an input-only device?)",
533 g_strerror (errno), oss->open_device));
537 GST_ELEMENT_ERROR (asink, RESOURCE, WRITE,
538 (_("Audio playback error.")),
539 ("write: %s (device: %s)", g_strerror (errno), oss->open_device));
549 gst_oss4_sink_delay (GstAudioSink * asink)
554 oss = GST_OSS4_SINK_CAST (asink);
556 if (ioctl (oss->fd, SNDCTL_DSP_GETODELAY, &delay) < 0 || delay < 0) {
557 GST_LOG_OBJECT (oss, "GETODELAY failed");
561 return delay / oss->bytes_per_sample;
565 gst_oss4_sink_reset (GstAudioSink * asink)
567 /* There's nothing we can do here really: OSS can't handle access to the
568 * same device/fd from multiple threads and might deadlock or blow up in
569 * other ways if we try an ioctl SNDCTL_DSP_HALT or similar */
573 gst_oss4_sink_init_interfaces (GType type)
575 gst_oss4_add_property_probe_interface (type);