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-osssrc
26 * This element lets you record sound using the Open Sound System (OSS).
29 * <title>Example pipelines</title>
31 * gst-launch -v osssrc ! audioconvert ! vorbisenc ! oggmux ! filesink location=mymusic.ogg
32 * ]| will record sound from your sound card using OSS and encode it to an
33 * Ogg/Vorbis file (this will only work if your mixer settings are right
34 * and the right inputs enabled etc.)
42 #include <sys/ioctl.h>
48 #ifdef HAVE_OSS_INCLUDE_IN_SYS
49 # include <sys/soundcard.h>
51 # ifdef HAVE_OSS_INCLUDE_IN_ROOT
52 # include <soundcard.h>
54 # ifdef HAVE_OSS_INCLUDE_IN_MACHINE
55 # include <machine/soundcard.h>
57 # error "What to include?"
58 # endif /* HAVE_OSS_INCLUDE_IN_MACHINE */
59 # endif /* HAVE_OSS_INCLUDE_IN_ROOT */
60 #endif /* HAVE_OSS_INCLUDE_IN_SYS */
62 #include "gstosssrc.h"
65 #include <gst/gst-i18n-plugin.h>
67 GST_DEBUG_CATEGORY_EXTERN (oss_debug);
68 #define GST_CAT_DEFAULT oss_debug
70 #define DEFAULT_DEVICE "/dev/dsp"
71 #define DEFAULT_DEVICE_NAME ""
80 GST_BOILERPLATE_WITH_INTERFACE (GstOssSrc, gst_oss_src, GstAudioSrc,
81 GST_TYPE_AUDIO_SRC, GstMixer, GST_TYPE_MIXER, gst_oss_src_mixer);
83 GST_IMPLEMENT_OSS_MIXER_METHODS (GstOssSrc, gst_oss_src_mixer);
85 static void gst_oss_src_get_property (GObject * object, guint prop_id,
86 GValue * value, GParamSpec * pspec);
87 static void gst_oss_src_set_property (GObject * object, guint prop_id,
88 const GValue * value, GParamSpec * pspec);
90 static void gst_oss_src_dispose (GObject * object);
91 static void gst_oss_src_finalize (GstOssSrc * osssrc);
93 static GstCaps *gst_oss_src_getcaps (GstBaseSrc * bsrc);
95 static gboolean gst_oss_src_open (GstAudioSrc * asrc);
96 static gboolean gst_oss_src_close (GstAudioSrc * asrc);
97 static gboolean gst_oss_src_prepare (GstAudioSrc * asrc,
98 GstRingBufferSpec * spec);
99 static gboolean gst_oss_src_unprepare (GstAudioSrc * asrc);
100 static guint gst_oss_src_read (GstAudioSrc * asrc, gpointer data, guint length);
101 static guint gst_oss_src_delay (GstAudioSrc * asrc);
102 static void gst_oss_src_reset (GstAudioSrc * asrc);
106 static GstStaticPadTemplate osssrc_src_factory = GST_STATIC_PAD_TEMPLATE ("src",
109 GST_STATIC_CAPS ("audio/x-raw-int, "
110 "endianness = (int) { " G_STRINGIFY (G_BYTE_ORDER) " }, "
111 "signed = (boolean) { TRUE, FALSE }, "
114 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]; "
116 "signed = (boolean) { TRUE, FALSE }, "
119 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]")
124 gst_oss_src_dispose (GObject * object)
126 G_OBJECT_CLASS (parent_class)->dispose (object);
130 gst_oss_src_base_init (gpointer g_class)
132 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
134 gst_element_class_set_details_simple (element_class, "Audio Source (OSS)",
136 "Capture from a sound card via OSS",
137 "Erik Walthinsen <omega@cse.ogi.edu>, " "Wim Taymans <wim@fluendo.com>");
139 gst_element_class_add_static_pad_template (element_class,
140 &osssrc_src_factory);
144 gst_oss_src_class_init (GstOssSrcClass * klass)
146 GObjectClass *gobject_class;
147 GstBaseSrcClass *gstbasesrc_class;
148 GstAudioSrcClass *gstaudiosrc_class;
150 gobject_class = (GObjectClass *) klass;
151 gstbasesrc_class = (GstBaseSrcClass *) klass;
152 gstaudiosrc_class = (GstAudioSrcClass *) klass;
154 gobject_class->dispose = gst_oss_src_dispose;
155 gobject_class->finalize = (GObjectFinalizeFunc) gst_oss_src_finalize;
156 gobject_class->get_property = gst_oss_src_get_property;
157 gobject_class->set_property = gst_oss_src_set_property;
159 gstbasesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_oss_src_getcaps);
161 gstaudiosrc_class->open = GST_DEBUG_FUNCPTR (gst_oss_src_open);
162 gstaudiosrc_class->prepare = GST_DEBUG_FUNCPTR (gst_oss_src_prepare);
163 gstaudiosrc_class->unprepare = GST_DEBUG_FUNCPTR (gst_oss_src_unprepare);
164 gstaudiosrc_class->close = GST_DEBUG_FUNCPTR (gst_oss_src_close);
165 gstaudiosrc_class->read = GST_DEBUG_FUNCPTR (gst_oss_src_read);
166 gstaudiosrc_class->delay = GST_DEBUG_FUNCPTR (gst_oss_src_delay);
167 gstaudiosrc_class->reset = GST_DEBUG_FUNCPTR (gst_oss_src_reset);
169 g_object_class_install_property (gobject_class, PROP_DEVICE,
170 g_param_spec_string ("device", "Device",
171 "OSS device (usually /dev/dspN)", DEFAULT_DEVICE,
172 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
174 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
175 g_param_spec_string ("device-name", "Device name",
176 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
177 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
181 gst_oss_src_set_property (GObject * object, guint prop_id,
182 const GValue * value, GParamSpec * pspec)
186 src = GST_OSS_SRC (object);
191 g_free (src->device);
192 src->device = g_value_dup_string (value);
195 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
201 gst_oss_src_get_property (GObject * object, guint prop_id,
202 GValue * value, GParamSpec * pspec)
206 src = GST_OSS_SRC (object);
210 g_value_set_string (value, src->device);
212 case PROP_DEVICE_NAME:
213 g_value_set_string (value, src->device_name);
216 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
222 gst_oss_src_init (GstOssSrc * osssrc, GstOssSrcClass * g_class)
226 GST_DEBUG ("initializing osssrc");
228 device = g_getenv ("AUDIODEV");
230 device = DEFAULT_DEVICE;
233 osssrc->device = g_strdup (device);
234 osssrc->device_name = g_strdup (DEFAULT_DEVICE_NAME);
235 osssrc->probed_caps = NULL;
239 gst_oss_src_finalize (GstOssSrc * osssrc)
241 g_free (osssrc->device);
242 g_free (osssrc->device_name);
244 G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (osssrc));
248 gst_oss_src_getcaps (GstBaseSrc * bsrc)
253 osssrc = GST_OSS_SRC (bsrc);
255 if (osssrc->fd == -1) {
256 GST_DEBUG_OBJECT (osssrc, "device not open, using template caps");
257 return NULL; /* base class will get template caps for us */
260 if (osssrc->probed_caps) {
261 GST_LOG_OBJECT (osssrc, "Returning cached caps");
262 return gst_caps_ref (osssrc->probed_caps);
265 caps = gst_oss_helper_probe_caps (osssrc->fd);
268 osssrc->probed_caps = gst_caps_ref (caps);
271 GST_INFO_OBJECT (osssrc, "returning caps %" GST_PTR_FORMAT, caps);
279 /* well... hacker's delight explains... */
285 x = x - ((x >> 1) & 0x55555555);
286 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
287 x = (x + (x >> 4)) & 0x0f0f0f0f;
290 return (x & 0x0000003f) - 1;
294 gst_oss_src_get_format (GstBufferFormat fmt)
300 result = AFMT_MU_LAW;
306 result = AFMT_IMA_ADPCM;
312 result = AFMT_S16_LE;
315 result = AFMT_S16_BE;
321 result = AFMT_U16_LE;
324 result = AFMT_U16_BE;
337 gst_oss_src_open (GstAudioSrc * asrc)
342 oss = GST_OSS_SRC (asrc);
347 oss->fd = open (oss->device, mode, 0);
358 oss->mixer = gst_ossmixer_new ("/dev/mixer", GST_OSS_MIXER_CAPTURE);
361 g_free (oss->device_name);
362 oss->device_name = g_strdup (oss->mixer->cardname);
369 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
370 (_("Could not open audio device for recording. "
371 "You don't have permission to open the device.")),
377 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
378 (_("Could not open audio device for recording.")),
379 ("Unable to open device %s for recording: %s",
380 oss->device, g_strerror (errno)));
386 gst_oss_src_close (GstAudioSrc * asrc)
390 oss = GST_OSS_SRC (asrc);
395 gst_ossmixer_free (oss->mixer);
399 gst_caps_replace (&oss->probed_caps, NULL);
405 gst_oss_src_prepare (GstAudioSrc * asrc, GstRingBufferSpec * spec)
408 struct audio_buf_info info;
412 oss = GST_OSS_SRC (asrc);
414 mode = fcntl (oss->fd, F_GETFL);
416 if (fcntl (oss->fd, F_SETFL, mode) == -1)
419 fmt = gst_oss_src_get_format (spec->format);
423 tmp = ilog2 (spec->segsize);
424 tmp = ((spec->segtotal & 0x7fff) << 16) | tmp;
425 GST_DEBUG_OBJECT (oss, "set segsize: %d, segtotal: %d, value: %08x",
426 spec->segsize, spec->segtotal, tmp);
428 SET_PARAM (oss, SNDCTL_DSP_SETFRAGMENT, tmp, "SETFRAGMENT");
430 SET_PARAM (oss, SNDCTL_DSP_RESET, 0, "RESET");
432 SET_PARAM (oss, SNDCTL_DSP_SETFMT, fmt, "SETFMT");
433 if (spec->channels == 2)
434 SET_PARAM (oss, SNDCTL_DSP_STEREO, 1, "STEREO");
435 SET_PARAM (oss, SNDCTL_DSP_CHANNELS, spec->channels, "CHANNELS");
436 SET_PARAM (oss, SNDCTL_DSP_SPEED, spec->rate, "SPEED");
438 GET_PARAM (oss, SNDCTL_DSP_GETISPACE, &info, "GETISPACE");
440 spec->segsize = info.fragsize;
441 spec->segtotal = info.fragstotal;
443 if (spec->width != 16 && spec->width != 8)
446 spec->bytes_per_sample = (spec->width / 8) * spec->channels;
447 oss->bytes_per_sample = (spec->width / 8) * spec->channels;
448 memset (spec->silence_sample, 0, spec->bytes_per_sample);
450 GST_DEBUG_OBJECT (oss, "got segsize: %d, segtotal: %d, value: %08x",
451 spec->segsize, spec->segtotal, tmp);
457 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
458 ("Unable to set device %s in non blocking mode: %s",
459 oss->device, g_strerror (errno)), (NULL));
464 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
465 ("Unable to get format %d", spec->format), (NULL));
470 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
471 ("Unexpected width %d", spec->width), (NULL));
477 gst_oss_src_unprepare (GstAudioSrc * asrc)
479 /* could do a SNDCTL_DSP_RESET, but the OSS manual recommends a close/open */
481 if (!gst_oss_src_close (asrc))
484 if (!gst_oss_src_open (asrc))
491 GST_DEBUG_OBJECT (asrc, "Could not close the audio device");
496 GST_DEBUG_OBJECT (asrc, "Could not reopen the audio device");
502 gst_oss_src_read (GstAudioSrc * asrc, gpointer data, guint length)
504 return read (GST_OSS_SRC (asrc)->fd, data, length);
508 gst_oss_src_delay (GstAudioSrc * asrc)
514 oss = GST_OSS_SRC (asrc);
516 #ifdef SNDCTL_DSP_GETODELAY
517 ret = ioctl (oss->fd, SNDCTL_DSP_GETODELAY, &delay);
524 ret = ioctl (oss->fd, SNDCTL_DSP_GETOSPACE, &info);
526 delay = (ret < 0 ? 0 : (info.fragstotal * info.fragsize) - info.bytes);
528 return delay / oss->bytes_per_sample;
532 gst_oss_src_reset (GstAudioSrc * asrc)
534 /* There's nothing we can do here really: OSS can't handle access to the
535 * same device/fd from multiple threads and might deadlock or blow up in
536 * other ways if we try an ioctl SNDCTL_DSP_RESET or similar */