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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, 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-1.0 -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 */
63 #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 #define gst_oss_src_parent_class parent_class
81 G_DEFINE_TYPE (GstOssSrc, gst_oss_src, GST_TYPE_AUDIO_SRC);
83 static void gst_oss_src_get_property (GObject * object, guint prop_id,
84 GValue * value, GParamSpec * pspec);
85 static void gst_oss_src_set_property (GObject * object, guint prop_id,
86 const GValue * value, GParamSpec * pspec);
88 static void gst_oss_src_dispose (GObject * object);
89 static void gst_oss_src_finalize (GstOssSrc * osssrc);
91 static GstCaps *gst_oss_src_getcaps (GstBaseSrc * bsrc, GstCaps * filter);
93 static gboolean gst_oss_src_open (GstAudioSrc * asrc);
94 static gboolean gst_oss_src_close (GstAudioSrc * asrc);
95 static gboolean gst_oss_src_prepare (GstAudioSrc * asrc,
96 GstAudioRingBufferSpec * spec);
97 static gboolean gst_oss_src_unprepare (GstAudioSrc * asrc);
98 static guint gst_oss_src_read (GstAudioSrc * asrc, gpointer data, guint length,
99 GstClockTime * timestamp);
100 static guint gst_oss_src_delay (GstAudioSrc * asrc);
101 static void gst_oss_src_reset (GstAudioSrc * asrc);
103 #define FORMATS "{" GST_AUDIO_NE(S16)","GST_AUDIO_NE(U16)", S8, U8 }"
105 static GstStaticPadTemplate osssrc_src_factory = GST_STATIC_PAD_TEMPLATE ("src",
108 GST_STATIC_CAPS ("audio/x-raw, "
109 "format = (string) " FORMATS ", "
110 "layout = (string) interleaved, "
111 "rate = (int) [ 1, MAX ], "
112 "channels = (int) 1; "
114 "format = (string) " FORMATS ", "
115 "layout = (string) interleaved, "
116 "rate = (int) [ 1, MAX ], "
117 "channels = (int) 2, " "channel-mask = (bitmask) 0x3")
121 gst_oss_src_dispose (GObject * object)
123 G_OBJECT_CLASS (parent_class)->dispose (object);
127 gst_oss_src_class_init (GstOssSrcClass * klass)
129 GObjectClass *gobject_class;
130 GstElementClass *gstelement_class;
131 GstBaseSrcClass *gstbasesrc_class;
132 GstAudioSrcClass *gstaudiosrc_class;
134 gobject_class = (GObjectClass *) klass;
135 gstelement_class = (GstElementClass *) klass;
136 gstbasesrc_class = (GstBaseSrcClass *) klass;
137 gstaudiosrc_class = (GstAudioSrcClass *) klass;
139 gobject_class->dispose = gst_oss_src_dispose;
140 gobject_class->finalize = (GObjectFinalizeFunc) gst_oss_src_finalize;
141 gobject_class->get_property = gst_oss_src_get_property;
142 gobject_class->set_property = gst_oss_src_set_property;
144 gstbasesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_oss_src_getcaps);
146 gstaudiosrc_class->open = GST_DEBUG_FUNCPTR (gst_oss_src_open);
147 gstaudiosrc_class->prepare = GST_DEBUG_FUNCPTR (gst_oss_src_prepare);
148 gstaudiosrc_class->unprepare = GST_DEBUG_FUNCPTR (gst_oss_src_unprepare);
149 gstaudiosrc_class->close = GST_DEBUG_FUNCPTR (gst_oss_src_close);
150 gstaudiosrc_class->read = GST_DEBUG_FUNCPTR (gst_oss_src_read);
151 gstaudiosrc_class->delay = GST_DEBUG_FUNCPTR (gst_oss_src_delay);
152 gstaudiosrc_class->reset = GST_DEBUG_FUNCPTR (gst_oss_src_reset);
154 g_object_class_install_property (gobject_class, PROP_DEVICE,
155 g_param_spec_string ("device", "Device",
156 "OSS device (usually /dev/dspN)", DEFAULT_DEVICE,
157 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
159 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
160 g_param_spec_string ("device-name", "Device name",
161 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
162 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
165 gst_element_class_set_static_metadata (gstelement_class, "Audio Source (OSS)",
167 "Capture from a sound card via OSS",
168 "Erik Walthinsen <omega@cse.ogi.edu>, " "Wim Taymans <wim@fluendo.com>");
170 gst_element_class_add_static_pad_template (gstelement_class,
171 &osssrc_src_factory);
175 gst_oss_src_set_property (GObject * object, guint prop_id,
176 const GValue * value, GParamSpec * pspec)
180 src = GST_OSS_SRC (object);
184 g_free (src->device);
185 src->device = g_value_dup_string (value);
188 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
194 gst_oss_src_get_property (GObject * object, guint prop_id,
195 GValue * value, GParamSpec * pspec)
199 src = GST_OSS_SRC (object);
203 g_value_set_string (value, src->device);
205 case PROP_DEVICE_NAME:
206 g_value_set_string (value, src->device_name);
209 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
215 gst_oss_src_init (GstOssSrc * osssrc)
219 GST_DEBUG ("initializing osssrc");
221 device = g_getenv ("AUDIODEV");
223 device = DEFAULT_DEVICE;
226 osssrc->device = g_strdup (device);
227 osssrc->device_name = g_strdup (DEFAULT_DEVICE_NAME);
228 osssrc->probed_caps = NULL;
232 gst_oss_src_finalize (GstOssSrc * osssrc)
234 g_free (osssrc->device);
235 g_free (osssrc->device_name);
237 G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (osssrc));
241 gst_oss_src_getcaps (GstBaseSrc * bsrc, GstCaps * filter)
246 osssrc = GST_OSS_SRC (bsrc);
248 if (osssrc->fd == -1) {
249 GST_DEBUG_OBJECT (osssrc, "device not open, using template caps");
250 return NULL; /* base class will get template caps for us */
253 if (osssrc->probed_caps) {
254 GST_LOG_OBJECT (osssrc, "Returning cached caps");
255 return gst_caps_ref (osssrc->probed_caps);
258 caps = gst_oss_helper_probe_caps (osssrc->fd);
261 osssrc->probed_caps = gst_caps_ref (caps);
264 GST_INFO_OBJECT (osssrc, "returning caps %" GST_PTR_FORMAT, caps);
266 if (filter && caps) {
267 GstCaps *intersection;
270 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
271 gst_caps_unref (caps);
281 /* well... hacker's delight explains... */
287 x = x - ((x >> 1) & 0x55555555);
288 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
289 x = (x + (x >> 4)) & 0x0f0f0f0f;
292 return (x & 0x0000003f) - 1;
296 gst_oss_src_get_format (GstAudioRingBufferFormatType fmt, GstAudioFormat rfmt)
301 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_MU_LAW:
302 result = AFMT_MU_LAW;
304 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_A_LAW:
307 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_IMA_ADPCM:
308 result = AFMT_IMA_ADPCM;
310 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_MPEG:
313 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_RAW:
316 case GST_AUDIO_FORMAT_U8:
319 case GST_AUDIO_FORMAT_S16LE:
320 result = AFMT_S16_LE;
322 case GST_AUDIO_FORMAT_S16BE:
323 result = AFMT_S16_BE;
325 case GST_AUDIO_FORMAT_S8:
328 case GST_AUDIO_FORMAT_U16LE:
329 result = AFMT_U16_LE;
331 case GST_AUDIO_FORMAT_U16BE:
332 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);
368 g_free (oss->device_name);
369 oss->device_name = gst_oss_helper_get_card_name ("/dev/mixer");
375 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
376 (_("Could not open audio device for recording. "
377 "You don't have permission to open the device.")),
383 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
384 (_("Could not open audio device for recording.")),
385 ("Unable to open device %s for recording: %s",
386 oss->device, g_strerror (errno)));
392 gst_oss_src_close (GstAudioSrc * asrc)
396 oss = GST_OSS_SRC (asrc);
400 gst_caps_replace (&oss->probed_caps, NULL);
406 gst_oss_src_prepare (GstAudioSrc * asrc, GstAudioRingBufferSpec * spec)
409 struct audio_buf_info info;
412 guint width, rate, channels;
414 oss = GST_OSS_SRC (asrc);
416 mode = fcntl (oss->fd, F_GETFL);
418 if (fcntl (oss->fd, F_SETFL, mode) == -1)
421 fmt = gst_oss_src_get_format (spec->type,
422 GST_AUDIO_INFO_FORMAT (&spec->info));
426 width = GST_AUDIO_INFO_WIDTH (&spec->info);
427 rate = GST_AUDIO_INFO_RATE (&spec->info);
428 channels = GST_AUDIO_INFO_CHANNELS (&spec->info);
430 if (width != 16 && width != 8)
433 tmp = ilog2 (spec->segsize);
434 tmp = ((spec->segtotal & 0x7fff) << 16) | tmp;
435 GST_DEBUG_OBJECT (oss, "set segsize: %d, segtotal: %d, value: %08x",
436 spec->segsize, spec->segtotal, tmp);
438 SET_PARAM (oss, SNDCTL_DSP_SETFRAGMENT, tmp, "SETFRAGMENT");
440 SET_PARAM (oss, SNDCTL_DSP_RESET, 0, "RESET");
442 SET_PARAM (oss, SNDCTL_DSP_SETFMT, fmt, "SETFMT");
444 SET_PARAM (oss, SNDCTL_DSP_STEREO, 1, "STEREO");
445 SET_PARAM (oss, SNDCTL_DSP_CHANNELS, channels, "CHANNELS");
446 SET_PARAM (oss, SNDCTL_DSP_SPEED, rate, "SPEED");
448 GET_PARAM (oss, SNDCTL_DSP_GETISPACE, &info, "GETISPACE");
450 spec->segsize = info.fragsize;
451 spec->segtotal = info.fragstotal;
453 oss->bytes_per_sample = GST_AUDIO_INFO_BPF (&spec->info);
455 GST_DEBUG_OBJECT (oss, "got segsize: %d, segtotal: %d, value: %08x",
456 spec->segsize, spec->segtotal, tmp);
462 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
463 ("Unable to set device %s in non blocking mode: %s",
464 oss->device, g_strerror (errno)), (NULL));
469 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
470 ("Unable to get format (%d, %d)", spec->type,
471 GST_AUDIO_INFO_FORMAT (&spec->info)), (NULL));
476 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
477 ("Unexpected width %d", width), (NULL));
483 gst_oss_src_unprepare (GstAudioSrc * asrc)
485 /* could do a SNDCTL_DSP_RESET, but the OSS manual recommends a close/open */
487 if (!gst_oss_src_close (asrc))
490 if (!gst_oss_src_open (asrc))
497 GST_DEBUG_OBJECT (asrc, "Could not close the audio device");
502 GST_DEBUG_OBJECT (asrc, "Could not reopen the audio device");
508 gst_oss_src_read (GstAudioSrc * asrc, gpointer data, guint length,
509 GstClockTime * timestamp)
511 return read (GST_OSS_SRC (asrc)->fd, data, length);
515 gst_oss_src_delay (GstAudioSrc * asrc)
521 oss = GST_OSS_SRC (asrc);
523 #ifdef SNDCTL_DSP_GETODELAY
524 ret = ioctl (oss->fd, SNDCTL_DSP_GETODELAY, &delay);
531 ret = ioctl (oss->fd, SNDCTL_DSP_GETOSPACE, &info);
533 delay = (ret < 0 ? 0 : (info.fragstotal * info.fragsize) - info.bytes);
535 return delay / oss->bytes_per_sample;
539 gst_oss_src_reset (GstAudioSrc * asrc)
541 /* There's nothing we can do here really: OSS can't handle access to the
542 * same device/fd from multiple threads and might deadlock or blow up in
543 * other ways if we try an ioctl SNDCTL_DSP_RESET or similar */