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
25 * @short_description: record sound from your sound card using OSS
29 * This element lets you record sound using the Open Sound System (OSS).
31 * <title>Example pipelines</title>
34 * gst-launch -v osssrc ! audioconvert ! vorbisenc ! oggmux ! filesink location=mymusic.ogg
36 * will record sound from your sound card using OSS and encode it to an
37 * Ogg/Vorbis file (this will only work if your mixer settings are right
38 * and the right inputs enabled etc.)
47 #include <sys/ioctl.h>
53 #ifdef HAVE_OSS_INCLUDE_IN_SYS
54 # include <sys/soundcard.h>
56 # ifdef HAVE_OSS_INCLUDE_IN_ROOT
57 # include <soundcard.h>
59 # ifdef HAVE_OSS_INCLUDE_IN_MACHINE
60 # include <machine/soundcard.h>
62 # error "What to include?"
63 # endif /* HAVE_OSS_INCLUDE_IN_MACHINE */
64 # endif /* HAVE_OSS_INCLUDE_IN_ROOT */
65 #endif /* HAVE_OSS_INCLUDE_IN_SYS */
67 #include "gstosssrc.h"
70 #include <gst/gst-i18n-plugin.h>
72 GST_DEBUG_CATEGORY_EXTERN (oss_debug);
73 #define GST_CAT_DEFAULT oss_debug
75 static const GstElementDetails gst_oss_src_details =
76 GST_ELEMENT_DETAILS ("Audio Source (OSS)",
78 "Capture from a sound card via OSS",
79 "Erik Walthinsen <omega@cse.ogi.edu>, " "Wim Taymans <wim@fluendo.com>");
81 #define DEFAULT_DEVICE "/dev/dsp"
82 #define DEFAULT_DEVICE_NAME ""
91 GST_BOILERPLATE_WITH_INTERFACE (GstOssSrc, gst_oss_src, GstAudioSrc,
92 GST_TYPE_AUDIO_SRC, GstMixer, GST_TYPE_MIXER, gst_oss_src_mixer);
94 GST_IMPLEMENT_OSS_MIXER_METHODS (GstOssSrc, gst_oss_src_mixer);
96 static void gst_oss_src_get_property (GObject * object, guint prop_id,
97 GValue * value, GParamSpec * pspec);
98 static void gst_oss_src_set_property (GObject * object, guint prop_id,
99 const GValue * value, GParamSpec * pspec);
101 static void gst_oss_src_dispose (GObject * object);
102 static void gst_oss_src_finalize (GstOssSrc * osssrc);
104 static GstCaps *gst_oss_src_getcaps (GstBaseSrc * bsrc);
106 static gboolean gst_oss_src_open (GstAudioSrc * asrc);
107 static gboolean gst_oss_src_close (GstAudioSrc * asrc);
108 static gboolean gst_oss_src_prepare (GstAudioSrc * asrc,
109 GstRingBufferSpec * spec);
110 static gboolean gst_oss_src_unprepare (GstAudioSrc * asrc);
111 static guint gst_oss_src_read (GstAudioSrc * asrc, gpointer data, guint length);
112 static guint gst_oss_src_delay (GstAudioSrc * asrc);
113 static void gst_oss_src_reset (GstAudioSrc * asrc);
117 static GstStaticPadTemplate osssrc_src_factory = GST_STATIC_PAD_TEMPLATE ("src",
120 GST_STATIC_CAPS ("audio/x-raw-int, "
121 "endianness = (int) { " G_STRINGIFY (G_BYTE_ORDER) " }, "
122 "signed = (boolean) { TRUE, FALSE }, "
125 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]; "
127 "signed = (boolean) { TRUE, FALSE }, "
130 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]")
135 gst_oss_src_dispose (GObject * object)
137 G_OBJECT_CLASS (parent_class)->dispose (object);
141 gst_oss_src_base_init (gpointer g_class)
143 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
145 gst_element_class_set_details (element_class, &gst_oss_src_details);
147 gst_element_class_add_pad_template (element_class,
148 gst_static_pad_template_get (&osssrc_src_factory));
151 gst_oss_src_class_init (GstOssSrcClass * klass)
153 GObjectClass *gobject_class;
154 GstElementClass *gstelement_class;
155 GstBaseSrcClass *gstbasesrc_class;
156 GstBaseAudioSrcClass *gstbaseaudiosrc_class;
157 GstAudioSrcClass *gstaudiosrc_class;
159 gobject_class = (GObjectClass *) klass;
160 gstelement_class = (GstElementClass *) klass;
161 gstbasesrc_class = (GstBaseSrcClass *) klass;
162 gstbaseaudiosrc_class = (GstBaseAudioSrcClass *) klass;
163 gstaudiosrc_class = (GstAudioSrcClass *) klass;
165 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_oss_src_dispose);
166 gobject_class->finalize =
167 (GObjectFinalizeFunc) GST_DEBUG_FUNCPTR (gst_oss_src_finalize);
168 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_oss_src_get_property);
169 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_oss_src_set_property);
171 gstbasesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_oss_src_getcaps);
173 gstaudiosrc_class->open = GST_DEBUG_FUNCPTR (gst_oss_src_open);
174 gstaudiosrc_class->prepare = GST_DEBUG_FUNCPTR (gst_oss_src_prepare);
175 gstaudiosrc_class->unprepare = GST_DEBUG_FUNCPTR (gst_oss_src_unprepare);
176 gstaudiosrc_class->close = GST_DEBUG_FUNCPTR (gst_oss_src_close);
177 gstaudiosrc_class->read = GST_DEBUG_FUNCPTR (gst_oss_src_read);
178 gstaudiosrc_class->delay = GST_DEBUG_FUNCPTR (gst_oss_src_delay);
179 gstaudiosrc_class->reset = GST_DEBUG_FUNCPTR (gst_oss_src_reset);
181 g_object_class_install_property (gobject_class, PROP_DEVICE,
182 g_param_spec_string ("device", "Device",
183 "OSS device (usually /dev/dspN)", DEFAULT_DEVICE, G_PARAM_READWRITE));
185 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
186 g_param_spec_string ("device-name", "Device name",
187 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
192 gst_oss_src_set_property (GObject * object, guint prop_id,
193 const GValue * value, GParamSpec * pspec)
197 src = GST_OSS_SRC (object);
202 g_free (src->device);
203 src->device = g_value_dup_string (value);
206 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
212 gst_oss_src_get_property (GObject * object, guint prop_id,
213 GValue * value, GParamSpec * pspec)
217 src = GST_OSS_SRC (object);
221 g_value_set_string (value, src->device);
223 case PROP_DEVICE_NAME:
224 g_value_set_string (value, src->device_name);
227 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
233 gst_oss_src_init (GstOssSrc * osssrc, GstOssSrcClass * g_class)
237 GST_DEBUG ("initializing osssrc");
239 device = g_getenv ("AUDIODEV");
241 device = DEFAULT_DEVICE;
244 osssrc->device = g_strdup (device);
245 osssrc->device_name = g_strdup (DEFAULT_DEVICE_NAME);
246 osssrc->probed_caps = NULL;
250 gst_oss_src_finalize (GstOssSrc * osssrc)
252 g_free (osssrc->device);
253 g_free (osssrc->device_name);
255 G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (osssrc));
259 gst_oss_src_getcaps (GstBaseSrc * bsrc)
264 osssrc = GST_OSS_SRC (bsrc);
266 if (osssrc->fd == -1) {
267 GST_DEBUG_OBJECT (osssrc, "device not open, using template caps");
268 return NULL; /* base class will get template caps for us */
271 if (osssrc->probed_caps) {
272 GST_LOG_OBJECT (osssrc, "Returning cached caps");
273 return gst_caps_ref (osssrc->probed_caps);
276 caps = gst_oss_helper_probe_caps (osssrc->fd);
279 osssrc->probed_caps = gst_caps_ref (caps);
282 GST_INFO_OBJECT (osssrc, "returning caps %" GST_PTR_FORMAT, caps);
290 /* well... hacker's delight explains... */
296 x = x - ((x >> 1) & 0x55555555);
297 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
298 x = (x + (x >> 4)) & 0x0f0f0f0f;
301 return (x & 0x0000003f) - 1;
305 gst_oss_src_get_format (GstBufferFormat fmt)
311 result = AFMT_MU_LAW;
317 result = AFMT_IMA_ADPCM;
323 result = AFMT_S16_LE;
326 result = AFMT_S16_BE;
332 result = AFMT_U16_LE;
335 result = AFMT_U16_BE;
348 gst_oss_src_open (GstAudioSrc * asrc)
353 oss = GST_OSS_SRC (asrc);
358 oss->fd = open (oss->device, mode, 0);
369 oss->mixer = gst_ossmixer_new ("/dev/mixer", GST_OSS_MIXER_CAPTURE);
372 g_free (oss->device_name);
373 oss->device_name = g_strdup (oss->mixer->cardname);
380 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
381 (_("Could not open audio device for recording. "
382 "You don't have permission to open the device.")),
388 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
389 (_("Could not open audio device for recording.")),
390 ("Unable to open device %s for recording: %s",
391 oss->device, g_strerror (errno)));
397 gst_oss_src_close (GstAudioSrc * asrc)
401 oss = GST_OSS_SRC (asrc);
406 gst_ossmixer_free (oss->mixer);
410 gst_caps_replace (&oss->probed_caps, NULL);
416 gst_oss_src_prepare (GstAudioSrc * asrc, GstRingBufferSpec * spec)
419 struct audio_buf_info info;
423 oss = GST_OSS_SRC (asrc);
425 mode = fcntl (oss->fd, F_GETFL);
427 if (fcntl (oss->fd, F_SETFL, mode) == -1)
430 fmt = gst_oss_src_get_format (spec->format);
434 tmp = ilog2 (spec->segsize);
435 tmp = ((spec->segtotal & 0x7fff) << 16) | tmp;
436 GST_DEBUG_OBJECT (oss, "set segsize: %d, segtotal: %d, value: %08x",
437 spec->segsize, spec->segtotal, tmp);
439 SET_PARAM (oss, SNDCTL_DSP_SETFRAGMENT, tmp, "SETFRAGMENT");
441 SET_PARAM (oss, SNDCTL_DSP_RESET, 0, "RESET");
443 SET_PARAM (oss, SNDCTL_DSP_SETFMT, fmt, "SETFMT");
444 if (spec->channels == 2)
445 SET_PARAM (oss, SNDCTL_DSP_STEREO, 1, "STEREO");
446 SET_PARAM (oss, SNDCTL_DSP_CHANNELS, spec->channels, "CHANNELS");
447 SET_PARAM (oss, SNDCTL_DSP_SPEED, spec->rate, "SPEED");
449 GET_PARAM (oss, SNDCTL_DSP_GETISPACE, &info, "GETISPACE");
451 spec->segsize = info.fragsize;
452 spec->segtotal = info.fragstotal;
454 if (spec->width != 16 && spec->width != 8)
457 spec->bytes_per_sample = (spec->width / 8) * spec->channels;
458 oss->bytes_per_sample = (spec->width / 8) * spec->channels;
459 memset (spec->silence_sample, 0, spec->bytes_per_sample);
461 GST_DEBUG_OBJECT (oss, "got segsize: %d, segtotal: %d, value: %08x",
462 spec->segsize, spec->segtotal, tmp);
468 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
469 ("Unable to set device %s in non blocking mode: %s",
470 oss->device, g_strerror (errno)), (NULL));
475 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
476 ("Unable to get format %d", spec->format), (NULL));
481 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
482 ("Unexpected width %d", spec->width), (NULL));
488 gst_oss_src_unprepare (GstAudioSrc * asrc)
490 /* could do a SNDCTL_DSP_RESET, but the OSS manual recommends a close/open */
492 if (!gst_oss_src_close (asrc))
495 if (!gst_oss_src_open (asrc))
502 GST_DEBUG_OBJECT (asrc, "Could not close the audio device");
507 GST_DEBUG_OBJECT (asrc, "Could not reopen the audio device");
513 gst_oss_src_read (GstAudioSrc * asrc, gpointer data, guint length)
515 return read (GST_OSS_SRC (asrc)->fd, data, length);
519 gst_oss_src_delay (GstAudioSrc * asrc)
525 oss = GST_OSS_SRC (asrc);
527 #ifdef SNDCTL_DSP_GETODELAY
528 ret = ioctl (oss->fd, SNDCTL_DSP_GETODELAY, &delay);
535 ret = ioctl (oss->fd, SNDCTL_DSP_GETOSPACE, &info);
537 delay = (ret < 0 ? 0 : (info.fragstotal * info.fragsize) - info.bytes);
539 return delay / oss->bytes_per_sample;
543 gst_oss_src_reset (GstAudioSrc * asrc)
545 /* There's nothing we can do here really: OSS can't handle access to the
546 * same device/fd from multiple threads and might deadlock or blow up in
547 * other ways if we try an ioctl SNDCTL_DSP_RESET or similar */