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 GST_DEBUG_CATEGORY_EXTERN (oss_debug);
71 #define GST_CAT_DEFAULT oss_debug
73 static const GstElementDetails gst_oss_src_details =
74 GST_ELEMENT_DETAILS ("Audio Source (OSS)",
76 "Capture from a sound card via OSS",
77 "Erik Walthinsen <omega@cse.ogi.edu>, " "Wim Taymans <wim@fluendo.com>");
79 #define DEFAULT_DEVICE "/dev/dsp"
80 #define DEFAULT_DEVICE_NAME ""
89 GST_BOILERPLATE_WITH_INTERFACE (GstOssSrc, gst_oss_src, GstAudioSrc,
90 GST_TYPE_AUDIO_SRC, GstMixer, GST_TYPE_MIXER, gst_oss_src_mixer);
92 GST_IMPLEMENT_OSS_MIXER_METHODS (GstOssSrc, gst_oss_src_mixer);
94 static void gst_oss_src_get_property (GObject * object, guint prop_id,
95 GValue * value, GParamSpec * pspec);
96 static void gst_oss_src_set_property (GObject * object, guint prop_id,
97 const GValue * value, GParamSpec * pspec);
99 static void gst_oss_src_dispose (GObject * object);
100 static void gst_oss_src_finalize (GstOssSrc * osssrc);
102 static GstCaps *gst_oss_src_getcaps (GstBaseSrc * bsrc);
104 static gboolean gst_oss_src_open (GstAudioSrc * asrc);
105 static gboolean gst_oss_src_close (GstAudioSrc * asrc);
106 static gboolean gst_oss_src_prepare (GstAudioSrc * asrc,
107 GstRingBufferSpec * spec);
108 static gboolean gst_oss_src_unprepare (GstAudioSrc * asrc);
109 static guint gst_oss_src_read (GstAudioSrc * asrc, gpointer data, guint length);
110 static guint gst_oss_src_delay (GstAudioSrc * asrc);
111 static void gst_oss_src_reset (GstAudioSrc * asrc);
115 static GstStaticPadTemplate osssrc_src_factory = GST_STATIC_PAD_TEMPLATE ("src",
118 GST_STATIC_CAPS ("audio/x-raw-int, "
119 "endianness = (int) { " G_STRINGIFY (G_BYTE_ORDER) " }, "
120 "signed = (boolean) { TRUE, FALSE }, "
123 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]; "
125 "signed = (boolean) { TRUE, FALSE }, "
128 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]")
133 gst_oss_src_dispose (GObject * object)
135 G_OBJECT_CLASS (parent_class)->dispose (object);
139 gst_oss_src_base_init (gpointer g_class)
141 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
143 gst_element_class_set_details (element_class, &gst_oss_src_details);
145 gst_element_class_add_pad_template (element_class,
146 gst_static_pad_template_get (&osssrc_src_factory));
149 gst_oss_src_class_init (GstOssSrcClass * klass)
151 GObjectClass *gobject_class;
152 GstElementClass *gstelement_class;
153 GstBaseSrcClass *gstbasesrc_class;
154 GstBaseAudioSrcClass *gstbaseaudiosrc_class;
155 GstAudioSrcClass *gstaudiosrc_class;
157 gobject_class = (GObjectClass *) klass;
158 gstelement_class = (GstElementClass *) klass;
159 gstbasesrc_class = (GstBaseSrcClass *) klass;
160 gstbaseaudiosrc_class = (GstBaseAudioSrcClass *) klass;
161 gstaudiosrc_class = (GstAudioSrcClass *) klass;
163 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_oss_src_dispose);
164 gobject_class->finalize =
165 (GObjectFinalizeFunc) GST_DEBUG_FUNCPTR (gst_oss_src_finalize);
166 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_oss_src_get_property);
167 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_oss_src_set_property);
169 gstbasesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_oss_src_getcaps);
171 gstaudiosrc_class->open = GST_DEBUG_FUNCPTR (gst_oss_src_open);
172 gstaudiosrc_class->prepare = GST_DEBUG_FUNCPTR (gst_oss_src_prepare);
173 gstaudiosrc_class->unprepare = GST_DEBUG_FUNCPTR (gst_oss_src_unprepare);
174 gstaudiosrc_class->close = GST_DEBUG_FUNCPTR (gst_oss_src_close);
175 gstaudiosrc_class->read = GST_DEBUG_FUNCPTR (gst_oss_src_read);
176 gstaudiosrc_class->delay = GST_DEBUG_FUNCPTR (gst_oss_src_delay);
177 gstaudiosrc_class->reset = GST_DEBUG_FUNCPTR (gst_oss_src_reset);
179 g_object_class_install_property (gobject_class, PROP_DEVICE,
180 g_param_spec_string ("device", "Device",
181 "OSS device (usually /dev/dspN)", DEFAULT_DEVICE, G_PARAM_READWRITE));
183 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
184 g_param_spec_string ("device-name", "Device name",
185 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
190 gst_oss_src_set_property (GObject * object, guint prop_id,
191 const GValue * value, GParamSpec * pspec)
195 src = GST_OSS_SRC (object);
200 g_free (src->device);
201 src->device = g_value_dup_string (value);
204 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
210 gst_oss_src_get_property (GObject * object, guint prop_id,
211 GValue * value, GParamSpec * pspec)
215 src = GST_OSS_SRC (object);
219 g_value_set_string (value, src->device);
221 case PROP_DEVICE_NAME:
222 g_value_set_string (value, src->device_name);
225 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
231 gst_oss_src_init (GstOssSrc * osssrc, GstOssSrcClass * g_class)
235 GST_DEBUG ("initializing osssrc");
237 device = g_getenv ("AUDIODEV");
239 device = DEFAULT_DEVICE;
242 osssrc->device = g_strdup (device);
243 osssrc->device_name = g_strdup (DEFAULT_DEVICE_NAME);
247 gst_oss_src_finalize (GstOssSrc * osssrc)
249 g_free (osssrc->device);
250 g_free (osssrc->device_name);
252 G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (osssrc));
256 gst_oss_src_getcaps (GstBaseSrc * bsrc)
261 osssrc = GST_OSS_SRC (bsrc);
263 if (osssrc->fd == -1) {
264 caps = gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SRC_PAD
267 caps = gst_oss_helper_probe_caps (osssrc->fd);
276 /* well... hacker's delight explains... */
282 x = x - ((x >> 1) & 0x55555555);
283 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
284 x = (x + (x >> 4)) & 0x0f0f0f0f;
287 return (x & 0x0000003f) - 1;
291 gst_oss_src_get_format (GstBufferFormat fmt)
297 result = AFMT_MU_LAW;
303 result = AFMT_IMA_ADPCM;
309 result = AFMT_S16_LE;
312 result = AFMT_S16_BE;
318 result = AFMT_U16_LE;
321 result = AFMT_U16_BE;
334 gst_oss_src_open (GstAudioSrc * asrc)
339 oss = GST_OSS_SRC (asrc);
344 oss->fd = open (oss->device, mode, 0);
349 oss->mixer = gst_ossmixer_new ("/dev/mixer", GST_OSS_MIXER_CAPTURE);
352 g_free (oss->device_name);
353 oss->device_name = g_strdup (oss->mixer->cardname);
360 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
361 ("Unable to open device %s for recording: %s",
362 oss->device, g_strerror (errno)), (NULL));
368 gst_oss_src_close (GstAudioSrc * asrc)
372 oss = GST_OSS_SRC (asrc);
377 gst_ossmixer_free (oss->mixer);
385 gst_oss_src_prepare (GstAudioSrc * asrc, GstRingBufferSpec * spec)
388 struct audio_buf_info info;
392 oss = GST_OSS_SRC (asrc);
394 mode = fcntl (oss->fd, F_GETFL);
396 if (fcntl (oss->fd, F_SETFL, mode) == -1)
399 fmt = gst_oss_src_get_format (spec->format);
403 tmp = ilog2 (spec->segsize);
404 tmp = ((spec->segtotal & 0x7fff) << 16) | tmp;
405 GST_DEBUG_OBJECT (oss, "set segsize: %d, segtotal: %d, value: %08x",
406 spec->segsize, spec->segtotal, tmp);
408 SET_PARAM (oss, SNDCTL_DSP_SETFRAGMENT, tmp, "SETFRAGMENT");
410 SET_PARAM (oss, SNDCTL_DSP_RESET, 0, "RESET");
412 SET_PARAM (oss, SNDCTL_DSP_SETFMT, fmt, "SETFMT");
413 if (spec->channels == 2)
414 SET_PARAM (oss, SNDCTL_DSP_STEREO, 1, "STEREO");
415 SET_PARAM (oss, SNDCTL_DSP_CHANNELS, spec->channels, "CHANNELS");
416 SET_PARAM (oss, SNDCTL_DSP_SPEED, spec->rate, "SPEED");
418 GET_PARAM (oss, SNDCTL_DSP_GETISPACE, &info, "GETISPACE");
420 spec->segsize = info.fragsize;
421 spec->segtotal = info.fragstotal;
423 if (spec->width != 16 && spec->width != 8)
426 spec->bytes_per_sample = (spec->width / 8) * spec->channels;
427 oss->bytes_per_sample = (spec->width / 8) * spec->channels;
428 memset (spec->silence_sample, 0, spec->bytes_per_sample);
430 GST_DEBUG_OBJECT (oss, "got segsize: %d, segtotal: %d, value: %08x",
431 spec->segsize, spec->segtotal, tmp);
437 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
438 ("Unable to set device %s in non blocking mode: %s",
439 oss->device, g_strerror (errno)), (NULL));
444 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
445 ("Unable to get format %d", spec->format), (NULL));
450 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
451 ("Unexpected width %d", spec->width), (NULL));
457 gst_oss_src_unprepare (GstAudioSrc * asrc)
459 /* could do a SNDCTL_DSP_RESET, but the OSS manual recommends a close/open */
461 if (!gst_oss_src_close (asrc))
464 if (!gst_oss_src_open (asrc))
471 GST_DEBUG_OBJECT (asrc, "Could not close the audio device");
476 GST_DEBUG_OBJECT (asrc, "Could not reopen the audio device");
482 gst_oss_src_read (GstAudioSrc * asrc, gpointer data, guint length)
484 return read (GST_OSS_SRC (asrc)->fd, data, length);
488 gst_oss_src_delay (GstAudioSrc * asrc)
494 oss = GST_OSS_SRC (asrc);
496 #ifdef SNDCTL_DSP_GETODELAY
497 ret = ioctl (oss->fd, SNDCTL_DSP_GETODELAY, &delay);
504 ret = ioctl (oss->fd, SNDCTL_DSP_GETOSPACE, &info);
506 delay = (ret < 0 ? 0 : (info.fragstotal * info.fragsize) - info.bytes);
508 return delay / oss->bytes_per_sample;
512 gst_oss_src_reset (GstAudioSrc * asrc)
518 oss = GST_OSS_SRC (asrc);
520 /* deadlocks on my machine... */
521 //ret = ioctl (oss->fd, SNDCTL_DSP_RESET, 0);