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 "layout = (string) interleaved, "
122 "rate = (int) [ 1, MAX ], "
123 "channels = (int) 1; "
125 "format = (string) " FORMATS ", "
126 "layout = (string) interleaved, "
127 "rate = (int) [ 1, MAX ], "
128 "channels = (int) 2, " "channel-mask = (bitmask) 0x3")
131 static GstElementClass *parent_class = NULL;
133 /* static guint gst_oss_sink_signals[LAST_SIGNAL] = { 0 }; */
136 gst_oss_sink_get_type (void)
138 static GType osssink_type = 0;
141 static const GTypeInfo osssink_info = {
142 sizeof (GstOssSinkClass),
143 gst_oss_sink_base_init,
145 (GClassInitFunc) gst_oss_sink_class_init,
150 (GInstanceInitFunc) gst_oss_sink_init,
154 g_type_register_static (GST_TYPE_AUDIO_SINK, "GstOssSink",
162 gst_oss_sink_dispose (GObject * object)
164 GstOssSink *osssink = GST_OSSSINK (object);
166 if (osssink->probed_caps) {
167 gst_caps_unref (osssink->probed_caps);
168 osssink->probed_caps = NULL;
171 G_OBJECT_CLASS (parent_class)->dispose (object);
175 gst_oss_sink_base_init (gpointer g_class)
177 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
179 gst_element_class_set_details_simple (element_class, "Audio Sink (OSS)",
181 "Output to a sound card via OSS",
182 "Erik Walthinsen <omega@cse.ogi.edu>, "
183 "Wim Taymans <wim.taymans@chello.be>");
185 gst_element_class_add_pad_template (element_class,
186 gst_static_pad_template_get (&osssink_sink_factory));
190 gst_oss_sink_class_init (GstOssSinkClass * klass)
192 GObjectClass *gobject_class;
193 GstBaseSinkClass *gstbasesink_class;
194 GstAudioSinkClass *gstaudiosink_class;
196 gobject_class = (GObjectClass *) klass;
197 gstbasesink_class = (GstBaseSinkClass *) klass;
198 gstaudiosink_class = (GstAudioSinkClass *) klass;
200 parent_class = g_type_class_peek_parent (klass);
202 gobject_class->dispose = gst_oss_sink_dispose;
203 gobject_class->finalize = gst_oss_sink_finalise;
204 gobject_class->get_property = gst_oss_sink_get_property;
205 gobject_class->set_property = gst_oss_sink_set_property;
207 g_object_class_install_property (gobject_class, PROP_DEVICE,
208 g_param_spec_string ("device", "Device",
209 "OSS device (usually /dev/dspN)", DEFAULT_DEVICE,
210 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
212 gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_oss_sink_getcaps);
214 gstaudiosink_class->open = GST_DEBUG_FUNCPTR (gst_oss_sink_open);
215 gstaudiosink_class->close = GST_DEBUG_FUNCPTR (gst_oss_sink_close);
216 gstaudiosink_class->prepare = GST_DEBUG_FUNCPTR (gst_oss_sink_prepare);
217 gstaudiosink_class->unprepare = GST_DEBUG_FUNCPTR (gst_oss_sink_unprepare);
218 gstaudiosink_class->write = GST_DEBUG_FUNCPTR (gst_oss_sink_write);
219 gstaudiosink_class->delay = GST_DEBUG_FUNCPTR (gst_oss_sink_delay);
220 gstaudiosink_class->reset = GST_DEBUG_FUNCPTR (gst_oss_sink_reset);
224 gst_oss_sink_init (GstOssSink * osssink)
228 GST_DEBUG_OBJECT (osssink, "initializing osssink");
230 device = g_getenv ("AUDIODEV");
232 device = DEFAULT_DEVICE;
233 osssink->device = g_strdup (device);
238 gst_oss_sink_finalise (GObject * object)
240 GstOssSink *osssink = GST_OSSSINK (object);
242 g_free (osssink->device);
244 G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (object));
248 gst_oss_sink_set_property (GObject * object, guint prop_id,
249 const GValue * value, GParamSpec * pspec)
253 sink = GST_OSSSINK (object);
257 g_free (sink->device);
258 sink->device = g_value_dup_string (value);
259 if (sink->probed_caps) {
260 gst_caps_unref (sink->probed_caps);
261 sink->probed_caps = NULL;
265 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
271 gst_oss_sink_get_property (GObject * object, guint prop_id,
272 GValue * value, GParamSpec * pspec)
276 sink = GST_OSSSINK (object);
280 g_value_set_string (value, sink->device);
283 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
289 gst_oss_sink_getcaps (GstBaseSink * bsink)
294 osssink = GST_OSSSINK (bsink);
296 if (osssink->fd == -1) {
297 caps = gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SINK_PAD
299 } else if (osssink->probed_caps) {
300 caps = gst_caps_copy (osssink->probed_caps);
302 caps = gst_oss_helper_probe_caps (osssink->fd);
303 if (caps && !gst_caps_is_empty (caps)) {
304 osssink->probed_caps = gst_caps_copy (caps);
314 /* well... hacker's delight explains... */
320 x = x - ((x >> 1) & 0x55555555);
321 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
322 x = (x + (x >> 4)) & 0x0f0f0f0f;
325 return (x & 0x0000003f) - 1;
329 gst_oss_sink_get_format (GstBufferFormat fmt)
335 result = AFMT_MU_LAW;
341 result = AFMT_IMA_ADPCM;
347 result = AFMT_S16_LE;
350 result = AFMT_S16_BE;
356 result = AFMT_U16_LE;
359 result = AFMT_U16_BE;
372 gst_oss_sink_open (GstAudioSink * asink)
377 oss = GST_OSSSINK (asink);
382 oss->fd = open (oss->device, mode, 0);
399 GST_ELEMENT_ERROR (oss, RESOURCE, BUSY,
400 (_("Could not open audio device for playback. "
401 "Device is being used by another application.")), (NULL));
406 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_WRITE,
407 (_("Could not open audio device for playback. "
408 "You don't have permission to open the device.")),
414 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_WRITE,
415 (_("Could not open audio device for playback.")), GST_ERROR_SYSTEM);
421 gst_oss_sink_close (GstAudioSink * asink)
423 close (GST_OSSSINK (asink)->fd);
424 GST_OSSSINK (asink)->fd = -1;
429 gst_oss_sink_prepare (GstAudioSink * asink, GstRingBufferSpec * spec)
432 struct audio_buf_info info;
436 oss = GST_OSSSINK (asink);
438 /* we opened non-blocking so that we can detect if the device is available
439 * without hanging forever. We now want to remove the non-blocking flag. */
440 mode = fcntl (oss->fd, F_GETFL);
442 if (fcntl (oss->fd, F_SETFL, mode) == -1) {
443 /* some drivers do no support unsetting the non-blocking flag, try to
444 * close/open the device then. This is racy but we error out properly. */
445 gst_oss_sink_close (asink);
446 if ((oss->fd = open (oss->device, O_WRONLY, 0)) == -1)
450 tmp = gst_oss_sink_get_format (spec->format);
454 if (spec->width != 16 && spec->width != 8)
457 SET_PARAM (oss, SNDCTL_DSP_SETFMT, tmp, "SETFMT");
458 if (spec->channels == 2)
459 SET_PARAM (oss, SNDCTL_DSP_STEREO, 1, "STEREO");
460 SET_PARAM (oss, SNDCTL_DSP_CHANNELS, spec->channels, "CHANNELS");
461 SET_PARAM (oss, SNDCTL_DSP_SPEED, spec->rate, "SPEED");
463 tmp = ilog2 (spec->segsize);
464 tmp = ((spec->segtotal & 0x7fff) << 16) | tmp;
465 GST_DEBUG_OBJECT (oss, "set segsize: %d, segtotal: %d, value: %08x",
466 spec->segsize, spec->segtotal, tmp);
468 SET_PARAM (oss, SNDCTL_DSP_SETFRAGMENT, tmp, "SETFRAGMENT");
469 GET_PARAM (oss, SNDCTL_DSP_GETOSPACE, &info, "GETOSPACE");
471 spec->segsize = info.fragsize;
472 spec->segtotal = info.fragstotal;
474 spec->bytes_per_sample = (spec->width / 8) * spec->channels;
475 oss->bytes_per_sample = (spec->width / 8) * spec->channels;
477 GST_DEBUG_OBJECT (oss, "got segsize: %d, segtotal: %d, value: %08x",
478 spec->segsize, spec->segtotal, tmp);
485 GST_ELEMENT_ERROR (oss, RESOURCE, SETTINGS, (NULL),
486 ("Unable to set device %s in non blocking mode: %s",
487 oss->device, g_strerror (errno)));
492 GST_ELEMENT_ERROR (oss, RESOURCE, SETTINGS, (NULL),
493 ("Unable to get format %d", spec->format));
498 GST_ELEMENT_ERROR (oss, RESOURCE, SETTINGS, (NULL),
499 ("unexpected width %d", spec->width));
505 gst_oss_sink_unprepare (GstAudioSink * asink)
507 /* could do a SNDCTL_DSP_RESET, but the OSS manual recommends a close/open */
509 if (!gst_oss_sink_close (asink))
512 if (!gst_oss_sink_open (asink))
520 GST_DEBUG_OBJECT (asink, "Could not close the audio device");
525 GST_DEBUG_OBJECT (asink, "Could not reopen the audio device");
531 gst_oss_sink_write (GstAudioSink * asink, gpointer data, guint length)
533 return write (GST_OSSSINK (asink)->fd, data, length);
537 gst_oss_sink_delay (GstAudioSink * asink)
543 oss = GST_OSSSINK (asink);
545 #ifdef SNDCTL_DSP_GETODELAY
546 ret = ioctl (oss->fd, SNDCTL_DSP_GETODELAY, &delay);
553 ret = ioctl (oss->fd, SNDCTL_DSP_GETOSPACE, &info);
555 delay = (ret < 0 ? 0 : (info.fragstotal * info.fragsize) - info.bytes);
557 return delay / oss->bytes_per_sample;
561 gst_oss_sink_reset (GstAudioSink * asink)
563 /* There's nothing we can do here really: OSS can't handle access to the
564 * same device/fd from multiple threads and might deadlock or blow up in
565 * other ways if we try an ioctl SNDCTL_DSP_RESET or similar */