2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000,2005 Wim Taymans <wim@fluendo.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * SECTION:element-osssink
26 * 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).
34 * <title>Example pipelines</title>
36 * gst-launch -v audiotestsrc ! audioconvert ! volume volume=0.1 ! osssink
37 * ]| will output a sine wave (continuous beep sound) to your sound card (with
38 * a very low volume as precaution).
40 * gst-launch -v filesrc location=music.ogg ! decodebin ! audioconvert ! audioresample ! osssink
41 * ]| will play an Ogg/Vorbis audio file and output it using the Open Sound System.
48 #include <sys/ioctl.h>
54 #ifdef HAVE_OSS_INCLUDE_IN_SYS
55 # include <sys/soundcard.h>
57 # ifdef HAVE_OSS_INCLUDE_IN_ROOT
58 # include <soundcard.h>
60 # ifdef HAVE_OSS_INCLUDE_IN_MACHINE
61 # include <machine/soundcard.h>
63 # error "What to include?"
64 # endif /* HAVE_OSS_INCLUDE_IN_MACHINE */
65 # endif /* HAVE_OSS_INCLUDE_IN_ROOT */
66 #endif /* HAVE_OSS_INCLUDE_IN_SYS */
69 #include "gstosssink.h"
71 #include <gst/gst-i18n-plugin.h>
73 GST_DEBUG_CATEGORY_EXTERN (oss_debug);
74 #define GST_CAT_DEFAULT oss_debug
76 static void gst_oss_sink_base_init (gpointer g_class);
77 static void gst_oss_sink_class_init (GstOssSinkClass * klass);
78 static void gst_oss_sink_init (GstOssSink * osssink);
80 static void gst_oss_sink_dispose (GObject * object);
81 static void gst_oss_sink_finalise (GObject * object);
83 static void gst_oss_sink_get_property (GObject * object, guint prop_id,
84 GValue * value, GParamSpec * pspec);
85 static void gst_oss_sink_set_property (GObject * object, guint prop_id,
86 const GValue * value, GParamSpec * pspec);
88 static GstCaps *gst_oss_sink_getcaps (GstBaseSink * bsink);
90 static gboolean gst_oss_sink_open (GstAudioSink * asink);
91 static gboolean gst_oss_sink_close (GstAudioSink * asink);
92 static gboolean gst_oss_sink_prepare (GstAudioSink * asink,
93 GstRingBufferSpec * spec);
94 static gboolean gst_oss_sink_unprepare (GstAudioSink * asink);
95 static guint gst_oss_sink_write (GstAudioSink * asink, gpointer data,
97 static guint gst_oss_sink_delay (GstAudioSink * asink);
98 static void gst_oss_sink_reset (GstAudioSink * asink);
100 /* OssSink signals and args */
106 #define DEFAULT_DEVICE "/dev/dsp"
113 #define FORMATS "{" GST_AUDIO_NE(S16)","GST_AUDIO_NE(U16)", S8, U8 }"
115 static GstStaticPadTemplate osssink_sink_factory =
116 GST_STATIC_PAD_TEMPLATE ("sink",
119 GST_STATIC_CAPS ("audio/x-raw, "
120 "format = (string) " FORMATS ", "
121 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]")
124 static GstElementClass *parent_class = NULL;
126 /* static guint gst_oss_sink_signals[LAST_SIGNAL] = { 0 }; */
129 gst_oss_sink_get_type (void)
131 static GType osssink_type = 0;
134 static const GTypeInfo osssink_info = {
135 sizeof (GstOssSinkClass),
136 gst_oss_sink_base_init,
138 (GClassInitFunc) gst_oss_sink_class_init,
143 (GInstanceInitFunc) gst_oss_sink_init,
147 g_type_register_static (GST_TYPE_AUDIO_SINK, "GstOssSink",
155 gst_oss_sink_dispose (GObject * object)
157 GstOssSink *osssink = GST_OSSSINK (object);
159 if (osssink->probed_caps) {
160 gst_caps_unref (osssink->probed_caps);
161 osssink->probed_caps = NULL;
164 G_OBJECT_CLASS (parent_class)->dispose (object);
168 gst_oss_sink_base_init (gpointer g_class)
170 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
172 gst_element_class_set_details_simple (element_class, "Audio Sink (OSS)",
174 "Output to a sound card via OSS",
175 "Erik Walthinsen <omega@cse.ogi.edu>, "
176 "Wim Taymans <wim.taymans@chello.be>");
178 gst_element_class_add_pad_template (element_class,
179 gst_static_pad_template_get (&osssink_sink_factory));
183 gst_oss_sink_class_init (GstOssSinkClass * klass)
185 GObjectClass *gobject_class;
186 GstBaseSinkClass *gstbasesink_class;
187 GstAudioSinkClass *gstaudiosink_class;
189 gobject_class = (GObjectClass *) klass;
190 gstbasesink_class = (GstBaseSinkClass *) klass;
191 gstaudiosink_class = (GstAudioSinkClass *) klass;
193 parent_class = g_type_class_peek_parent (klass);
195 gobject_class->dispose = gst_oss_sink_dispose;
196 gobject_class->finalize = gst_oss_sink_finalise;
197 gobject_class->get_property = gst_oss_sink_get_property;
198 gobject_class->set_property = gst_oss_sink_set_property;
200 g_object_class_install_property (gobject_class, PROP_DEVICE,
201 g_param_spec_string ("device", "Device",
202 "OSS device (usually /dev/dspN)", DEFAULT_DEVICE,
203 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
205 gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_oss_sink_getcaps);
207 gstaudiosink_class->open = GST_DEBUG_FUNCPTR (gst_oss_sink_open);
208 gstaudiosink_class->close = GST_DEBUG_FUNCPTR (gst_oss_sink_close);
209 gstaudiosink_class->prepare = GST_DEBUG_FUNCPTR (gst_oss_sink_prepare);
210 gstaudiosink_class->unprepare = GST_DEBUG_FUNCPTR (gst_oss_sink_unprepare);
211 gstaudiosink_class->write = GST_DEBUG_FUNCPTR (gst_oss_sink_write);
212 gstaudiosink_class->delay = GST_DEBUG_FUNCPTR (gst_oss_sink_delay);
213 gstaudiosink_class->reset = GST_DEBUG_FUNCPTR (gst_oss_sink_reset);
217 gst_oss_sink_init (GstOssSink * osssink)
221 GST_DEBUG_OBJECT (osssink, "initializing osssink");
223 device = g_getenv ("AUDIODEV");
225 device = DEFAULT_DEVICE;
226 osssink->device = g_strdup (device);
231 gst_oss_sink_finalise (GObject * object)
233 GstOssSink *osssink = GST_OSSSINK (object);
235 g_free (osssink->device);
237 G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (object));
241 gst_oss_sink_set_property (GObject * object, guint prop_id,
242 const GValue * value, GParamSpec * pspec)
246 sink = GST_OSSSINK (object);
250 g_free (sink->device);
251 sink->device = g_value_dup_string (value);
252 if (sink->probed_caps) {
253 gst_caps_unref (sink->probed_caps);
254 sink->probed_caps = NULL;
258 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
264 gst_oss_sink_get_property (GObject * object, guint prop_id,
265 GValue * value, GParamSpec * pspec)
269 sink = GST_OSSSINK (object);
273 g_value_set_string (value, sink->device);
276 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
282 gst_oss_sink_getcaps (GstBaseSink * bsink)
287 osssink = GST_OSSSINK (bsink);
289 if (osssink->fd == -1) {
290 caps = gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SINK_PAD
292 } else if (osssink->probed_caps) {
293 caps = gst_caps_copy (osssink->probed_caps);
295 caps = gst_oss_helper_probe_caps (osssink->fd);
296 if (caps && !gst_caps_is_empty (caps)) {
297 osssink->probed_caps = gst_caps_copy (caps);
307 /* well... hacker's delight explains... */
313 x = x - ((x >> 1) & 0x55555555);
314 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
315 x = (x + (x >> 4)) & 0x0f0f0f0f;
318 return (x & 0x0000003f) - 1;
322 gst_oss_sink_get_format (GstBufferFormat fmt)
328 result = AFMT_MU_LAW;
334 result = AFMT_IMA_ADPCM;
340 result = AFMT_S16_LE;
343 result = AFMT_S16_BE;
349 result = AFMT_U16_LE;
352 result = AFMT_U16_BE;
365 gst_oss_sink_open (GstAudioSink * asink)
370 oss = GST_OSSSINK (asink);
375 oss->fd = open (oss->device, mode, 0);
392 GST_ELEMENT_ERROR (oss, RESOURCE, BUSY,
393 (_("Could not open audio device for playback. "
394 "Device is being used by another application.")), (NULL));
399 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_WRITE,
400 (_("Could not open audio device for playback. "
401 "You don't have permission to open the device.")),
407 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_WRITE,
408 (_("Could not open audio device for playback.")), GST_ERROR_SYSTEM);
414 gst_oss_sink_close (GstAudioSink * asink)
416 close (GST_OSSSINK (asink)->fd);
417 GST_OSSSINK (asink)->fd = -1;
422 gst_oss_sink_prepare (GstAudioSink * asink, GstRingBufferSpec * spec)
425 struct audio_buf_info info;
429 oss = GST_OSSSINK (asink);
431 /* we opened non-blocking so that we can detect if the device is available
432 * without hanging forever. We now want to remove the non-blocking flag. */
433 mode = fcntl (oss->fd, F_GETFL);
435 if (fcntl (oss->fd, F_SETFL, mode) == -1) {
436 /* some drivers do no support unsetting the non-blocking flag, try to
437 * close/open the device then. This is racy but we error out properly. */
438 gst_oss_sink_close (asink);
439 if ((oss->fd = open (oss->device, O_WRONLY, 0)) == -1)
443 tmp = gst_oss_sink_get_format (spec->format);
447 if (spec->width != 16 && spec->width != 8)
450 SET_PARAM (oss, SNDCTL_DSP_SETFMT, tmp, "SETFMT");
451 if (spec->channels == 2)
452 SET_PARAM (oss, SNDCTL_DSP_STEREO, 1, "STEREO");
453 SET_PARAM (oss, SNDCTL_DSP_CHANNELS, spec->channels, "CHANNELS");
454 SET_PARAM (oss, SNDCTL_DSP_SPEED, spec->rate, "SPEED");
456 tmp = ilog2 (spec->segsize);
457 tmp = ((spec->segtotal & 0x7fff) << 16) | tmp;
458 GST_DEBUG_OBJECT (oss, "set segsize: %d, segtotal: %d, value: %08x",
459 spec->segsize, spec->segtotal, tmp);
461 SET_PARAM (oss, SNDCTL_DSP_SETFRAGMENT, tmp, "SETFRAGMENT");
462 GET_PARAM (oss, SNDCTL_DSP_GETOSPACE, &info, "GETOSPACE");
464 spec->segsize = info.fragsize;
465 spec->segtotal = info.fragstotal;
467 spec->bytes_per_sample = (spec->width / 8) * spec->channels;
468 oss->bytes_per_sample = (spec->width / 8) * spec->channels;
470 GST_DEBUG_OBJECT (oss, "got segsize: %d, segtotal: %d, value: %08x",
471 spec->segsize, spec->segtotal, tmp);
478 GST_ELEMENT_ERROR (oss, RESOURCE, SETTINGS, (NULL),
479 ("Unable to set device %s in non blocking mode: %s",
480 oss->device, g_strerror (errno)));
485 GST_ELEMENT_ERROR (oss, RESOURCE, SETTINGS, (NULL),
486 ("Unable to get format %d", spec->format));
491 GST_ELEMENT_ERROR (oss, RESOURCE, SETTINGS, (NULL),
492 ("unexpected width %d", spec->width));
498 gst_oss_sink_unprepare (GstAudioSink * asink)
500 /* could do a SNDCTL_DSP_RESET, but the OSS manual recommends a close/open */
502 if (!gst_oss_sink_close (asink))
505 if (!gst_oss_sink_open (asink))
513 GST_DEBUG_OBJECT (asink, "Could not close the audio device");
518 GST_DEBUG_OBJECT (asink, "Could not reopen the audio device");
524 gst_oss_sink_write (GstAudioSink * asink, gpointer data, guint length)
526 return write (GST_OSSSINK (asink)->fd, data, length);
530 gst_oss_sink_delay (GstAudioSink * asink)
536 oss = GST_OSSSINK (asink);
538 #ifdef SNDCTL_DSP_GETODELAY
539 ret = ioctl (oss->fd, SNDCTL_DSP_GETODELAY, &delay);
546 ret = ioctl (oss->fd, SNDCTL_DSP_GETOSPACE, &info);
548 delay = (ret < 0 ? 0 : (info.fragstotal * info.fragsize) - info.bytes);
550 return delay / oss->bytes_per_sample;
554 gst_oss_sink_reset (GstAudioSink * asink)
556 /* There's nothing we can do here really: OSS can't handle access to the
557 * same device/fd from multiple threads and might deadlock or blow up in
558 * other ways if we try an ioctl SNDCTL_DSP_RESET or similar */