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);
101 static GstCaps *gst_oss_src_getcaps (GstBaseSrc * bsrc);
103 static gboolean gst_oss_src_open (GstAudioSrc * asrc);
104 static gboolean gst_oss_src_close (GstAudioSrc * asrc);
105 static gboolean gst_oss_src_prepare (GstAudioSrc * asrc,
106 GstRingBufferSpec * spec);
107 static gboolean gst_oss_src_unprepare (GstAudioSrc * asrc);
108 static guint gst_oss_src_read (GstAudioSrc * asrc, gpointer data, guint length);
109 static guint gst_oss_src_delay (GstAudioSrc * asrc);
110 static void gst_oss_src_reset (GstAudioSrc * asrc);
114 static GstStaticPadTemplate osssrc_src_factory = GST_STATIC_PAD_TEMPLATE ("src",
117 GST_STATIC_CAPS ("audio/x-raw-int, "
118 "endianness = (int) { " G_STRINGIFY (G_BYTE_ORDER) " }, "
119 "signed = (boolean) { TRUE, FALSE }, "
122 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]; "
124 "signed = (boolean) { TRUE, FALSE }, "
127 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]")
132 gst_oss_src_dispose (GObject * object)
134 G_OBJECT_CLASS (parent_class)->dispose (object);
138 gst_oss_src_base_init (gpointer g_class)
140 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
142 gst_element_class_set_details (element_class, &gst_oss_src_details);
144 gst_element_class_add_pad_template (element_class,
145 gst_static_pad_template_get (&osssrc_src_factory));
148 gst_oss_src_class_init (GstOssSrcClass * klass)
150 GObjectClass *gobject_class;
151 GstElementClass *gstelement_class;
152 GstBaseSrcClass *gstbasesrc_class;
153 GstBaseAudioSrcClass *gstbaseaudiosrc_class;
154 GstAudioSrcClass *gstaudiosrc_class;
156 gobject_class = (GObjectClass *) klass;
157 gstelement_class = (GstElementClass *) klass;
158 gstbasesrc_class = (GstBaseSrcClass *) klass;
159 gstbaseaudiosrc_class = (GstBaseAudioSrcClass *) klass;
160 gstaudiosrc_class = (GstAudioSrcClass *) klass;
162 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_oss_src_dispose);
163 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_oss_src_get_property);
164 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_oss_src_set_property);
166 gstbasesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_oss_src_getcaps);
168 gstaudiosrc_class->open = GST_DEBUG_FUNCPTR (gst_oss_src_open);
169 gstaudiosrc_class->prepare = GST_DEBUG_FUNCPTR (gst_oss_src_prepare);
170 gstaudiosrc_class->unprepare = GST_DEBUG_FUNCPTR (gst_oss_src_unprepare);
171 gstaudiosrc_class->close = GST_DEBUG_FUNCPTR (gst_oss_src_close);
172 gstaudiosrc_class->read = GST_DEBUG_FUNCPTR (gst_oss_src_read);
173 gstaudiosrc_class->delay = GST_DEBUG_FUNCPTR (gst_oss_src_delay);
174 gstaudiosrc_class->reset = GST_DEBUG_FUNCPTR (gst_oss_src_reset);
176 g_object_class_install_property (gobject_class, PROP_DEVICE,
177 g_param_spec_string ("device", "Device",
178 "OSS device (usually /dev/dspN)", DEFAULT_DEVICE, G_PARAM_READWRITE));
180 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
181 g_param_spec_string ("device-name", "Device name",
182 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
187 gst_oss_src_set_property (GObject * object, guint prop_id,
188 const GValue * value, GParamSpec * pspec)
192 src = GST_OSS_SRC (object);
197 g_free (src->device);
198 src->device = g_value_dup_string (value);
201 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
207 gst_oss_src_get_property (GObject * object, guint prop_id,
208 GValue * value, GParamSpec * pspec)
212 src = GST_OSS_SRC (object);
216 g_value_set_string (value, src->device);
218 case PROP_DEVICE_NAME:
219 g_value_set_string (value, src->device_name);
222 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
228 gst_oss_src_init (GstOssSrc * osssrc, GstOssSrcClass * g_class)
230 GST_DEBUG ("initializing osssrc");
233 osssrc->device = g_strdup (DEFAULT_DEVICE);
234 osssrc->device_name = g_strdup (DEFAULT_DEVICE_NAME);
238 gst_oss_src_getcaps (GstBaseSrc * bsrc)
243 osssrc = GST_OSS_SRC (bsrc);
245 if (osssrc->fd == -1) {
246 caps = gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_SRC_PAD
249 caps = gst_oss_helper_probe_caps (osssrc->fd);
258 /* well... hacker's delight explains... */
264 x = x - ((x >> 1) & 0x55555555);
265 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
266 x = (x + (x >> 4)) & 0x0f0f0f0f;
269 return (x & 0x0000003f) - 1;
273 gst_oss_src_get_format (GstBufferFormat fmt)
279 result = AFMT_MU_LAW;
285 result = AFMT_IMA_ADPCM;
291 result = AFMT_S16_LE;
294 result = AFMT_S16_BE;
300 result = AFMT_U16_LE;
303 result = AFMT_U16_BE;
316 gst_oss_src_open (GstAudioSrc * asrc)
321 oss = GST_OSS_SRC (asrc);
326 oss->fd = open (oss->device, mode, 0);
331 oss->mixer = gst_ossmixer_new ("/dev/mixer", GST_OSS_MIXER_CAPTURE);
334 g_free (oss->device_name);
335 oss->device_name = g_strdup (oss->mixer->cardname);
342 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
343 ("Unable to open device %s for recording: %s",
344 oss->device, g_strerror (errno)), (NULL));
350 gst_oss_src_close (GstAudioSrc * asrc)
354 oss = GST_OSS_SRC (asrc);
359 gst_ossmixer_free (oss->mixer);
367 gst_oss_src_prepare (GstAudioSrc * asrc, GstRingBufferSpec * spec)
370 struct audio_buf_info info;
374 oss = GST_OSS_SRC (asrc);
376 mode = fcntl (oss->fd, F_GETFL);
378 if (fcntl (oss->fd, F_SETFL, mode) == -1)
381 fmt = gst_oss_src_get_format (spec->format);
385 tmp = ilog2 (spec->segsize);
386 tmp = ((spec->segtotal & 0x7fff) << 16) | tmp;
387 GST_DEBUG_OBJECT (oss, "set segsize: %d, segtotal: %d, value: %08x",
388 spec->segsize, spec->segtotal, tmp);
390 SET_PARAM (oss, SNDCTL_DSP_SETFRAGMENT, tmp, "SETFRAGMENT");
392 SET_PARAM (oss, SNDCTL_DSP_RESET, 0, "RESET");
394 SET_PARAM (oss, SNDCTL_DSP_SETFMT, fmt, "SETFMT");
395 if (spec->channels == 2)
396 SET_PARAM (oss, SNDCTL_DSP_STEREO, 1, "STEREO");
397 SET_PARAM (oss, SNDCTL_DSP_CHANNELS, spec->channels, "CHANNELS");
398 SET_PARAM (oss, SNDCTL_DSP_SPEED, spec->rate, "SPEED");
400 GET_PARAM (oss, SNDCTL_DSP_GETISPACE, &info, "GETISPACE");
402 spec->segsize = info.fragsize;
403 spec->segtotal = info.fragstotal;
405 if (spec->width != 16 && spec->width != 8)
408 spec->bytes_per_sample = (spec->width / 8) * spec->channels;
409 oss->bytes_per_sample = (spec->width / 8) * spec->channels;
410 memset (spec->silence_sample, 0, spec->bytes_per_sample);
412 GST_DEBUG_OBJECT (oss, "got segsize: %d, segtotal: %d, value: %08x",
413 spec->segsize, spec->segtotal, tmp);
419 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
420 ("Unable to set device %s in non blocking mode: %s",
421 oss->device, g_strerror (errno)), (NULL));
426 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
427 ("Unable to get format %d", spec->format), (NULL));
432 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
433 ("Unexpected width %d", spec->width), (NULL));
439 gst_oss_src_unprepare (GstAudioSrc * asrc)
441 /* could do a SNDCTL_DSP_RESET, but the OSS manual recommends a close/open */
443 if (!gst_oss_src_close (asrc))
446 if (!gst_oss_src_open (asrc))
453 GST_DEBUG_OBJECT (asrc, "Could not close the audio device");
458 GST_DEBUG_OBJECT (asrc, "Could not reopen the audio device");
464 gst_oss_src_read (GstAudioSrc * asrc, gpointer data, guint length)
466 return read (GST_OSS_SRC (asrc)->fd, data, length);
470 gst_oss_src_delay (GstAudioSrc * asrc)
476 oss = GST_OSS_SRC (asrc);
478 #ifdef SNDCTL_DSP_GETODELAY
479 ret = ioctl (oss->fd, SNDCTL_DSP_GETODELAY, &delay);
486 ret = ioctl (oss->fd, SNDCTL_DSP_GETOSPACE, &info);
488 delay = (ret < 0 ? 0 : (info.fragstotal * info.fragsize) - info.bytes);
490 return delay / oss->bytes_per_sample;
494 gst_oss_src_reset (GstAudioSrc * asrc)
500 oss = GST_OSS_SRC (asrc);
502 /* deadlocks on my machine... */
503 //ret = ioctl (oss->fd, SNDCTL_DSP_RESET, 0);