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 /* elementfactory information */
77 static const GstElementDetails gst_oss_sink_details =
78 GST_ELEMENT_DETAILS ("Audio Sink (OSS)",
80 "Output to a sound card via OSS",
81 "Erik Walthinsen <omega@cse.ogi.edu>, "
82 "Wim Taymans <wim.taymans@chello.be>");
84 static void gst_oss_sink_base_init (gpointer g_class);
85 static void gst_oss_sink_class_init (GstOssSinkClass * klass);
86 static void gst_oss_sink_init (GstOssSink * osssink);
88 static void gst_oss_sink_dispose (GObject * object);
89 static void gst_oss_sink_finalise (GObject * object);
91 static void gst_oss_sink_get_property (GObject * object, guint prop_id,
92 GValue * value, GParamSpec * pspec);
93 static void gst_oss_sink_set_property (GObject * object, guint prop_id,
94 const GValue * value, GParamSpec * pspec);
96 static GstCaps *gst_oss_sink_getcaps (GstBaseSink * bsink);
98 static gboolean gst_oss_sink_open (GstAudioSink * asink);
99 static gboolean gst_oss_sink_close (GstAudioSink * asink);
100 static gboolean gst_oss_sink_prepare (GstAudioSink * asink,
101 GstRingBufferSpec * spec);
102 static gboolean gst_oss_sink_unprepare (GstAudioSink * asink);
103 static guint gst_oss_sink_write (GstAudioSink * asink, gpointer data,
105 static guint gst_oss_sink_delay (GstAudioSink * asink);
106 static void gst_oss_sink_reset (GstAudioSink * asink);
108 /* OssSink signals and args */
114 #define DEFAULT_DEVICE "/dev/dsp"
121 static GstStaticPadTemplate osssink_sink_factory =
122 GST_STATIC_PAD_TEMPLATE ("sink",
125 GST_STATIC_CAPS ("audio/x-raw-int, "
126 "endianness = (int) { " G_STRINGIFY (G_BYTE_ORDER) " }, "
127 "signed = (boolean) { TRUE, FALSE }, "
130 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]; "
132 "signed = (boolean) { TRUE, FALSE }, "
135 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]")
138 static GstElementClass *parent_class = NULL;
140 /* static guint gst_oss_sink_signals[LAST_SIGNAL] = { 0 }; */
143 gst_oss_sink_get_type (void)
145 static GType osssink_type = 0;
148 static const GTypeInfo osssink_info = {
149 sizeof (GstOssSinkClass),
150 gst_oss_sink_base_init,
152 (GClassInitFunc) gst_oss_sink_class_init,
157 (GInstanceInitFunc) gst_oss_sink_init,
161 g_type_register_static (GST_TYPE_AUDIO_SINK, "GstOssSink",
169 gst_oss_sink_dispose (GObject * object)
171 GstOssSink *osssink = GST_OSSSINK (object);
173 if (osssink->probed_caps) {
174 gst_caps_unref (osssink->probed_caps);
175 osssink->probed_caps = NULL;
178 G_OBJECT_CLASS (parent_class)->dispose (object);
182 gst_oss_sink_base_init (gpointer g_class)
184 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
186 gst_element_class_set_details (element_class, &gst_oss_sink_details);
188 gst_element_class_add_pad_template (element_class,
189 gst_static_pad_template_get (&osssink_sink_factory));
193 gst_oss_sink_class_init (GstOssSinkClass * klass)
195 GObjectClass *gobject_class;
196 GstBaseSinkClass *gstbasesink_class;
197 GstAudioSinkClass *gstaudiosink_class;
199 gobject_class = (GObjectClass *) klass;
200 gstbasesink_class = (GstBaseSinkClass *) klass;
201 gstaudiosink_class = (GstAudioSinkClass *) klass;
203 parent_class = g_type_class_peek_parent (klass);
205 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_oss_sink_dispose);
206 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_oss_sink_finalise);
207 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_oss_sink_get_property);
208 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_oss_sink_set_property);
210 g_object_class_install_property (gobject_class, PROP_DEVICE,
211 g_param_spec_string ("device", "Device",
212 "OSS device (usually /dev/dspN)", DEFAULT_DEVICE, G_PARAM_READWRITE));
214 gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_oss_sink_getcaps);
216 gstaudiosink_class->open = GST_DEBUG_FUNCPTR (gst_oss_sink_open);
217 gstaudiosink_class->close = GST_DEBUG_FUNCPTR (gst_oss_sink_close);
218 gstaudiosink_class->prepare = GST_DEBUG_FUNCPTR (gst_oss_sink_prepare);
219 gstaudiosink_class->unprepare = GST_DEBUG_FUNCPTR (gst_oss_sink_unprepare);
220 gstaudiosink_class->write = GST_DEBUG_FUNCPTR (gst_oss_sink_write);
221 gstaudiosink_class->delay = GST_DEBUG_FUNCPTR (gst_oss_sink_delay);
222 gstaudiosink_class->reset = GST_DEBUG_FUNCPTR (gst_oss_sink_reset);
226 gst_oss_sink_init (GstOssSink * osssink)
230 GST_DEBUG_OBJECT (osssink, "initializing osssink");
232 device = g_getenv ("AUDIODEV");
234 device = DEFAULT_DEVICE;
235 osssink->device = g_strdup (device);
240 gst_oss_sink_finalise (GObject * object)
242 GstOssSink *osssink = GST_OSSSINK (object);
244 g_free (osssink->device);
246 G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (object));
250 gst_oss_sink_set_property (GObject * object, guint prop_id,
251 const GValue * value, GParamSpec * pspec)
255 sink = GST_OSSSINK (object);
259 g_free (sink->device);
260 sink->device = g_value_dup_string (value);
261 if (sink->probed_caps) {
262 gst_caps_unref (sink->probed_caps);
263 sink->probed_caps = NULL;
267 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
273 gst_oss_sink_get_property (GObject * object, guint prop_id,
274 GValue * value, GParamSpec * pspec)
278 sink = GST_OSSSINK (object);
282 g_value_set_string (value, sink->device);
285 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
291 gst_oss_sink_getcaps (GstBaseSink * bsink)
296 osssink = GST_OSSSINK (bsink);
298 if (osssink->fd == -1) {
299 caps = gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SINK_PAD
301 } else if (osssink->probed_caps) {
302 caps = gst_caps_copy (osssink->probed_caps);
304 caps = gst_oss_helper_probe_caps (osssink->fd);
305 if (caps && !gst_caps_is_empty (caps)) {
306 osssink->probed_caps = gst_caps_copy (caps);
316 /* well... hacker's delight explains... */
322 x = x - ((x >> 1) & 0x55555555);
323 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
324 x = (x + (x >> 4)) & 0x0f0f0f0f;
327 return (x & 0x0000003f) - 1;
331 gst_oss_sink_get_format (GstBufferFormat fmt)
337 result = AFMT_MU_LAW;
343 result = AFMT_IMA_ADPCM;
349 result = AFMT_S16_LE;
352 result = AFMT_S16_BE;
358 result = AFMT_U16_LE;
361 result = AFMT_U16_BE;
374 gst_oss_sink_open (GstAudioSink * asink)
379 oss = GST_OSSSINK (asink);
384 oss->fd = open (oss->device, mode, 0);
401 GST_ELEMENT_ERROR (oss, RESOURCE, BUSY,
402 (_("Could not open audio device for playback. "
403 "Device is being used by another application.")), (NULL));
408 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_WRITE,
409 (_("Could not open audio device for playback. "
410 "You don't have permission to open the device.")),
416 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_WRITE,
417 (_("Could not open audio device for playback.")), GST_ERROR_SYSTEM);
423 gst_oss_sink_close (GstAudioSink * asink)
425 close (GST_OSSSINK (asink)->fd);
426 GST_OSSSINK (asink)->fd = -1;
431 gst_oss_sink_prepare (GstAudioSink * asink, GstRingBufferSpec * spec)
434 struct audio_buf_info info;
438 oss = GST_OSSSINK (asink);
440 /* we opened non-blocking so that we can detect if the device is available
441 * without hanging forever. We now want to remove the non-blocking flag. */
442 mode = fcntl (oss->fd, F_GETFL);
444 if (fcntl (oss->fd, F_SETFL, mode) == -1) {
445 /* some drivers do no support unsetting the non-blocking flag, try to
446 * close/open the device then. This is racy but we error out properly. */
447 gst_oss_sink_close (asink);
448 if ((oss->fd = open (oss->device, O_WRONLY, 0)) == -1)
452 tmp = gst_oss_sink_get_format (spec->format);
456 if (spec->width != 16 && spec->width != 8)
459 SET_PARAM (oss, SNDCTL_DSP_SETFMT, tmp, "SETFMT");
460 if (spec->channels == 2)
461 SET_PARAM (oss, SNDCTL_DSP_STEREO, 1, "STEREO");
462 SET_PARAM (oss, SNDCTL_DSP_CHANNELS, spec->channels, "CHANNELS");
463 SET_PARAM (oss, SNDCTL_DSP_SPEED, spec->rate, "SPEED");
465 tmp = ilog2 (spec->segsize);
466 tmp = ((spec->segtotal & 0x7fff) << 16) | tmp;
467 GST_DEBUG_OBJECT (oss, "set segsize: %d, segtotal: %d, value: %08x",
468 spec->segsize, spec->segtotal, tmp);
470 SET_PARAM (oss, SNDCTL_DSP_SETFRAGMENT, tmp, "SETFRAGMENT");
471 GET_PARAM (oss, SNDCTL_DSP_GETOSPACE, &info, "GETOSPACE");
473 spec->segsize = info.fragsize;
474 spec->segtotal = info.fragstotal;
476 spec->bytes_per_sample = (spec->width / 8) * spec->channels;
477 oss->bytes_per_sample = (spec->width / 8) * spec->channels;
479 GST_DEBUG_OBJECT (oss, "got segsize: %d, segtotal: %d, value: %08x",
480 spec->segsize, spec->segtotal, tmp);
487 GST_ELEMENT_ERROR (oss, RESOURCE, SETTINGS, (NULL),
488 ("Unable to set device %s in non blocking mode: %s",
489 oss->device, g_strerror (errno)));
494 GST_ELEMENT_ERROR (oss, RESOURCE, SETTINGS, (NULL),
495 ("Unable to get format %d", spec->format));
500 GST_ELEMENT_ERROR (oss, RESOURCE, SETTINGS, (NULL),
501 ("unexpected width %d", spec->width));
507 gst_oss_sink_unprepare (GstAudioSink * asink)
509 /* could do a SNDCTL_DSP_RESET, but the OSS manual recommends a close/open */
511 if (!gst_oss_sink_close (asink))
514 if (!gst_oss_sink_open (asink))
522 GST_DEBUG_OBJECT (asink, "Could not close the audio device");
527 GST_DEBUG_OBJECT (asink, "Could not reopen the audio device");
533 gst_oss_sink_write (GstAudioSink * asink, gpointer data, guint length)
535 return write (GST_OSSSINK (asink)->fd, data, length);
539 gst_oss_sink_delay (GstAudioSink * asink)
545 oss = GST_OSSSINK (asink);
547 #ifdef SNDCTL_DSP_GETODELAY
548 ret = ioctl (oss->fd, SNDCTL_DSP_GETODELAY, &delay);
555 ret = ioctl (oss->fd, SNDCTL_DSP_GETOSPACE, &info);
557 delay = (ret < 0 ? 0 : (info.fragstotal * info.fragsize) - info.bytes);
559 return delay / oss->bytes_per_sample;
563 gst_oss_sink_reset (GstAudioSink * asink)
565 /* There's nothing we can do here really: OSS can't handle access to the
566 * same device/fd from multiple threads and might deadlock or blow up in
567 * other ways if we try an ioctl SNDCTL_DSP_RESET or similar */