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
25 * @short_description: output sound using OSS
29 * This element lets you output sound using the Open Sound System (OSS).
32 * Note that you should almost always use generic audio conversion elements
33 * like audioconvert and audioresample in front of an audiosink to make sure
34 * your pipeline works under all circumstances (those conversion elements will
35 * act in passthrough-mode if no conversion is necessary).
37 * <title>Example pipelines</title>
40 * gst-launch -v audiotestsrc ! audioconvert ! volume volume=0.1 ! osssink
42 * will output a sine wave (continuous beep sound) to your sound card (with
43 * a very low volume as precaution).
47 * gst-launch -v filesrc location=music.ogg ! decodebin ! audioconvert ! audioresample ! osssink
49 * will play an Ogg/Vorbis audio file and output it using the Open Sound System.
57 #include <sys/ioctl.h>
63 #ifdef HAVE_OSS_INCLUDE_IN_SYS
64 # include <sys/soundcard.h>
66 # ifdef HAVE_OSS_INCLUDE_IN_ROOT
67 # include <soundcard.h>
69 # ifdef HAVE_OSS_INCLUDE_IN_MACHINE
70 # include <machine/soundcard.h>
72 # error "What to include?"
73 # endif /* HAVE_OSS_INCLUDE_IN_MACHINE */
74 # endif /* HAVE_OSS_INCLUDE_IN_ROOT */
75 #endif /* HAVE_OSS_INCLUDE_IN_SYS */
78 #include "gstosssink.h"
80 #include <gst/gst-i18n-plugin.h>
82 GST_DEBUG_CATEGORY_EXTERN (oss_debug);
83 #define GST_CAT_DEFAULT oss_debug
85 /* elementfactory information */
86 static const GstElementDetails gst_oss_sink_details =
87 GST_ELEMENT_DETAILS ("Audio Sink (OSS)",
89 "Output to a sound card via OSS",
90 "Erik Walthinsen <omega@cse.ogi.edu>, "
91 "Wim Taymans <wim.taymans@chello.be>");
93 static void gst_oss_sink_base_init (gpointer g_class);
94 static void gst_oss_sink_class_init (GstOssSinkClass * klass);
95 static void gst_oss_sink_init (GstOssSink * osssink);
97 static void gst_oss_sink_dispose (GObject * object);
98 static void gst_oss_sink_finalise (GObject * object);
100 static void gst_oss_sink_get_property (GObject * object, guint prop_id,
101 GValue * value, GParamSpec * pspec);
102 static void gst_oss_sink_set_property (GObject * object, guint prop_id,
103 const GValue * value, GParamSpec * pspec);
105 static GstCaps *gst_oss_sink_getcaps (GstBaseSink * bsink);
107 static gboolean gst_oss_sink_open (GstAudioSink * asink);
108 static gboolean gst_oss_sink_close (GstAudioSink * asink);
109 static gboolean gst_oss_sink_prepare (GstAudioSink * asink,
110 GstRingBufferSpec * spec);
111 static gboolean gst_oss_sink_unprepare (GstAudioSink * asink);
112 static guint gst_oss_sink_write (GstAudioSink * asink, gpointer data,
114 static guint gst_oss_sink_delay (GstAudioSink * asink);
115 static void gst_oss_sink_reset (GstAudioSink * asink);
117 /* OssSink signals and args */
123 #define DEFAULT_DEVICE "/dev/dsp"
130 static GstStaticPadTemplate osssink_sink_factory =
131 GST_STATIC_PAD_TEMPLATE ("sink",
134 GST_STATIC_CAPS ("audio/x-raw-int, "
135 "endianness = (int) { " G_STRINGIFY (G_BYTE_ORDER) " }, "
136 "signed = (boolean) { TRUE, FALSE }, "
139 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]; "
141 "signed = (boolean) { TRUE, FALSE }, "
144 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]")
147 static GstElementClass *parent_class = NULL;
149 /* static guint gst_oss_sink_signals[LAST_SIGNAL] = { 0 }; */
152 gst_oss_sink_get_type (void)
154 static GType osssink_type = 0;
157 static const GTypeInfo osssink_info = {
158 sizeof (GstOssSinkClass),
159 gst_oss_sink_base_init,
161 (GClassInitFunc) gst_oss_sink_class_init,
166 (GInstanceInitFunc) gst_oss_sink_init,
170 g_type_register_static (GST_TYPE_AUDIO_SINK, "GstOssSink",
178 gst_oss_sink_dispose (GObject * object)
180 GstOssSink *osssink = GST_OSSSINK (object);
182 if (osssink->probed_caps) {
183 gst_caps_unref (osssink->probed_caps);
184 osssink->probed_caps = NULL;
187 G_OBJECT_CLASS (parent_class)->dispose (object);
191 gst_oss_sink_base_init (gpointer g_class)
193 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
195 gst_element_class_set_details (element_class, &gst_oss_sink_details);
197 gst_element_class_add_pad_template (element_class,
198 gst_static_pad_template_get (&osssink_sink_factory));
201 gst_oss_sink_class_init (GstOssSinkClass * klass)
203 GObjectClass *gobject_class;
204 GstElementClass *gstelement_class;
205 GstBaseSinkClass *gstbasesink_class;
206 GstBaseAudioSinkClass *gstbaseaudiosink_class;
207 GstAudioSinkClass *gstaudiosink_class;
209 gobject_class = (GObjectClass *) klass;
210 gstelement_class = (GstElementClass *) klass;
211 gstbasesink_class = (GstBaseSinkClass *) klass;
212 gstbaseaudiosink_class = (GstBaseAudioSinkClass *) klass;
213 gstaudiosink_class = (GstAudioSinkClass *) klass;
215 parent_class = g_type_class_peek_parent (klass);
217 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_oss_sink_dispose);
218 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_oss_sink_finalise);
219 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_oss_sink_get_property);
220 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_oss_sink_set_property);
222 g_object_class_install_property (gobject_class, PROP_DEVICE,
223 g_param_spec_string ("device", "Device",
224 "OSS device (usually /dev/dspN)", DEFAULT_DEVICE, G_PARAM_READWRITE));
226 gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_oss_sink_getcaps);
228 gstaudiosink_class->open = GST_DEBUG_FUNCPTR (gst_oss_sink_open);
229 gstaudiosink_class->close = GST_DEBUG_FUNCPTR (gst_oss_sink_close);
230 gstaudiosink_class->prepare = GST_DEBUG_FUNCPTR (gst_oss_sink_prepare);
231 gstaudiosink_class->unprepare = GST_DEBUG_FUNCPTR (gst_oss_sink_unprepare);
232 gstaudiosink_class->write = GST_DEBUG_FUNCPTR (gst_oss_sink_write);
233 gstaudiosink_class->delay = GST_DEBUG_FUNCPTR (gst_oss_sink_delay);
234 gstaudiosink_class->reset = GST_DEBUG_FUNCPTR (gst_oss_sink_reset);
238 gst_oss_sink_init (GstOssSink * osssink)
242 GST_DEBUG_OBJECT (osssink, "initializing osssink");
244 device = g_getenv ("AUDIODEV");
246 device = DEFAULT_DEVICE;
247 osssink->device = g_strdup (device);
252 gst_oss_sink_finalise (GObject * object)
254 GstOssSink *osssink = GST_OSSSINK (object);
256 g_free (osssink->device);
258 G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (object));
262 gst_oss_sink_set_property (GObject * object, guint prop_id,
263 const GValue * value, GParamSpec * pspec)
267 sink = GST_OSSSINK (object);
271 g_free (sink->device);
272 sink->device = g_value_dup_string (value);
273 if (sink->probed_caps) {
274 gst_caps_unref (sink->probed_caps);
275 sink->probed_caps = NULL;
279 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
285 gst_oss_sink_get_property (GObject * object, guint prop_id,
286 GValue * value, GParamSpec * pspec)
290 sink = GST_OSSSINK (object);
294 g_value_set_string (value, sink->device);
297 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
303 gst_oss_sink_getcaps (GstBaseSink * bsink)
308 osssink = GST_OSSSINK (bsink);
310 if (osssink->fd == -1) {
311 caps = gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SINK_PAD
313 } else if (osssink->probed_caps) {
314 caps = gst_caps_copy (osssink->probed_caps);
316 caps = gst_oss_helper_probe_caps (osssink->fd);
317 if (caps && !gst_caps_is_empty (caps)) {
318 osssink->probed_caps = gst_caps_copy (caps);
328 /* well... hacker's delight explains... */
334 x = x - ((x >> 1) & 0x55555555);
335 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
336 x = (x + (x >> 4)) & 0x0f0f0f0f;
339 return (x & 0x0000003f) - 1;
343 gst_oss_sink_get_format (GstBufferFormat fmt)
349 result = AFMT_MU_LAW;
355 result = AFMT_IMA_ADPCM;
361 result = AFMT_S16_LE;
364 result = AFMT_S16_BE;
370 result = AFMT_U16_LE;
373 result = AFMT_U16_BE;
386 gst_oss_sink_open (GstAudioSink * asink)
391 oss = GST_OSSSINK (asink);
396 oss->fd = open (oss->device, mode, 0);
413 GST_ELEMENT_ERROR (oss, RESOURCE, BUSY,
414 (_("Could not open audio device for playback. "
415 "Device is being used by another application.")), (NULL));
420 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_WRITE,
421 (_("Could not open audio device for playback. "
422 "You don't have permission to open the device.")),
428 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_WRITE,
429 (_("Could not open audio device for playback.")), GST_ERROR_SYSTEM);
435 gst_oss_sink_close (GstAudioSink * asink)
437 close (GST_OSSSINK (asink)->fd);
438 GST_OSSSINK (asink)->fd = -1;
443 gst_oss_sink_prepare (GstAudioSink * asink, GstRingBufferSpec * spec)
446 struct audio_buf_info info;
450 oss = GST_OSSSINK (asink);
452 /* we opened non-blocking so that we can detect if the device is available
453 * without hanging forever. We now want to remove the non-blocking flag. */
454 mode = fcntl (oss->fd, F_GETFL);
456 if (fcntl (oss->fd, F_SETFL, mode) == -1) {
457 /* some drivers do no support unsetting the non-blocking flag, try to
458 * close/open the device then. This is racy but we error out properly. */
459 gst_oss_sink_close (asink);
460 if ((oss->fd = open (oss->device, O_WRONLY, 0)) == -1)
464 tmp = gst_oss_sink_get_format (spec->format);
468 if (spec->width != 16 && spec->width != 8)
471 SET_PARAM (oss, SNDCTL_DSP_SETFMT, tmp, "SETFMT");
472 if (spec->channels == 2)
473 SET_PARAM (oss, SNDCTL_DSP_STEREO, 1, "STEREO");
474 SET_PARAM (oss, SNDCTL_DSP_CHANNELS, spec->channels, "CHANNELS");
475 SET_PARAM (oss, SNDCTL_DSP_SPEED, spec->rate, "SPEED");
477 tmp = ilog2 (spec->segsize);
478 tmp = ((spec->segtotal & 0x7fff) << 16) | tmp;
479 GST_DEBUG_OBJECT (oss, "set segsize: %d, segtotal: %d, value: %08x",
480 spec->segsize, spec->segtotal, tmp);
482 SET_PARAM (oss, SNDCTL_DSP_SETFRAGMENT, tmp, "SETFRAGMENT");
483 GET_PARAM (oss, SNDCTL_DSP_GETOSPACE, &info, "GETOSPACE");
485 spec->segsize = info.fragsize;
486 spec->segtotal = info.fragstotal;
488 spec->bytes_per_sample = (spec->width / 8) * spec->channels;
489 oss->bytes_per_sample = (spec->width / 8) * spec->channels;
491 GST_DEBUG_OBJECT (oss, "got segsize: %d, segtotal: %d, value: %08x",
492 spec->segsize, spec->segtotal, tmp);
499 GST_ELEMENT_ERROR (oss, RESOURCE, SETTINGS, (NULL),
500 ("Unable to set device %s in non blocking mode: %s",
501 oss->device, g_strerror (errno)));
506 GST_ELEMENT_ERROR (oss, RESOURCE, SETTINGS, (NULL),
507 ("Unable to get format %d", spec->format));
512 GST_ELEMENT_ERROR (oss, RESOURCE, SETTINGS, (NULL),
513 ("unexpected width %d", spec->width));
519 gst_oss_sink_unprepare (GstAudioSink * asink)
521 /* could do a SNDCTL_DSP_RESET, but the OSS manual recommends a close/open */
523 if (!gst_oss_sink_close (asink))
526 if (!gst_oss_sink_open (asink))
534 GST_DEBUG_OBJECT (asink, "Could not close the audio device");
539 GST_DEBUG_OBJECT (asink, "Could not reopen the audio device");
545 gst_oss_sink_write (GstAudioSink * asink, gpointer data, guint length)
547 return write (GST_OSSSINK (asink)->fd, data, length);
551 gst_oss_sink_delay (GstAudioSink * asink)
557 oss = GST_OSSSINK (asink);
559 #ifdef SNDCTL_DSP_GETODELAY
560 ret = ioctl (oss->fd, SNDCTL_DSP_GETODELAY, &delay);
567 ret = ioctl (oss->fd, SNDCTL_DSP_GETOSPACE, &info);
569 delay = (ret < 0 ? 0 : (info.fragstotal * info.fragsize) - info.bytes);
571 return delay / oss->bytes_per_sample;
575 gst_oss_sink_reset (GstAudioSink * asink)
577 /* There's nothing we can do here really: OSS can't handle access to the
578 * same device/fd from multiple threads and might deadlock or blow up in
579 * other ways if we try an ioctl SNDCTL_DSP_RESET or similar */