1 /* GStreamer OSS4 audio source
2 * Copyright (C) 2007-2008 Tim-Philipp Müller <tim centricular net>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 * SECTION:element-oss4src
23 * This element lets you record sound using the Open Sound System (OSS)
27 * <title>Example pipelines</title>
29 * gst-launch-1.0 -v oss4src ! queue ! audioconvert ! vorbisenc ! oggmux ! filesink location=mymusic.ogg
30 * ]| will record sound from your sound card using OSS4 and encode it to an
31 * Ogg/Vorbis file (this will only work if your mixer settings are right
32 * and the right inputs areenabled etc.)
36 /* FIXME: make sure we're not doing ioctls from the app thread (e.g. via the
37 * mixer interface) while recording */
43 #include <sys/types.h>
45 #include <sys/ioctl.h>
51 #include <gst/gst-i18n-plugin.h>
53 #define NO_LEGACY_MIXER
54 #include "oss4-audio.h"
55 #include "oss4-source.h"
56 #include "oss4-property-probe.h"
57 #include "oss4-soundcard.h"
59 #define GST_OSS4_SOURCE_IS_OPEN(src) (GST_OSS4_SOURCE(src)->fd != -1)
61 GST_DEBUG_CATEGORY_EXTERN (oss4src_debug);
62 #define GST_CAT_DEFAULT oss4src_debug
64 #define DEFAULT_DEVICE NULL
65 #define DEFAULT_DEVICE_NAME NULL
74 #define gst_oss4_source_parent_class parent_class
75 G_DEFINE_TYPE (GstOss4Source, gst_oss4_source, GST_TYPE_AUDIO_SRC);
77 static void gst_oss4_source_get_property (GObject * object, guint prop_id,
78 GValue * value, GParamSpec * pspec);
79 static void gst_oss4_source_set_property (GObject * object, guint prop_id,
80 const GValue * value, GParamSpec * pspec);
82 static void gst_oss4_source_dispose (GObject * object);
83 static void gst_oss4_source_finalize (GstOss4Source * osssrc);
85 static GstCaps *gst_oss4_source_getcaps (GstBaseSrc * bsrc, GstCaps * filter);
87 static gboolean gst_oss4_source_open (GstAudioSrc * asrc,
88 gboolean silent_errors);
89 static gboolean gst_oss4_source_open_func (GstAudioSrc * asrc);
90 static gboolean gst_oss4_source_close (GstAudioSrc * asrc);
91 static gboolean gst_oss4_source_prepare (GstAudioSrc * asrc,
92 GstAudioRingBufferSpec * spec);
93 static gboolean gst_oss4_source_unprepare (GstAudioSrc * asrc);
94 static guint gst_oss4_source_read (GstAudioSrc * asrc, gpointer data,
95 guint length, GstClockTime * timestamp);
96 static guint gst_oss4_source_delay (GstAudioSrc * asrc);
97 static void gst_oss4_source_reset (GstAudioSrc * asrc);
100 gst_oss4_source_class_init (GstOss4SourceClass * klass)
102 GObjectClass *gobject_class;
103 GstElementClass *gstelement_class;
104 GstBaseSrcClass *gstbasesrc_class;
105 GstAudioSrcClass *gstaudiosrc_class;
106 GstPadTemplate *templ;
108 gobject_class = (GObjectClass *) klass;
109 gstelement_class = (GstElementClass *) klass;
110 gstbasesrc_class = (GstBaseSrcClass *) klass;
111 gstaudiosrc_class = (GstAudioSrcClass *) klass;
113 gobject_class->dispose = gst_oss4_source_dispose;
114 gobject_class->finalize = (GObjectFinalizeFunc) gst_oss4_source_finalize;
115 gobject_class->get_property = gst_oss4_source_get_property;
116 gobject_class->set_property = gst_oss4_source_set_property;
118 gstbasesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_oss4_source_getcaps);
120 gstaudiosrc_class->open = GST_DEBUG_FUNCPTR (gst_oss4_source_open_func);
121 gstaudiosrc_class->prepare = GST_DEBUG_FUNCPTR (gst_oss4_source_prepare);
122 gstaudiosrc_class->unprepare = GST_DEBUG_FUNCPTR (gst_oss4_source_unprepare);
123 gstaudiosrc_class->close = GST_DEBUG_FUNCPTR (gst_oss4_source_close);
124 gstaudiosrc_class->read = GST_DEBUG_FUNCPTR (gst_oss4_source_read);
125 gstaudiosrc_class->delay = GST_DEBUG_FUNCPTR (gst_oss4_source_delay);
126 gstaudiosrc_class->reset = GST_DEBUG_FUNCPTR (gst_oss4_source_reset);
128 g_object_class_install_property (gobject_class, PROP_DEVICE,
129 g_param_spec_string ("device", "Device",
130 "OSS4 device (e.g. /dev/oss/hdaudio0/pcm0 or /dev/dspN) "
131 "(NULL = use first available device)",
132 DEFAULT_DEVICE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
134 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
135 g_param_spec_string ("device-name", "Device name",
136 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
137 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
139 gst_element_class_set_static_metadata (gstelement_class,
140 "OSS v4 Audio Source", "Source/Audio",
141 "Capture from a sound card via OSS version 4",
142 "Tim-Philipp Müller <tim centricular net>");
144 templ = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
145 gst_oss4_audio_get_template_caps ());
146 gst_element_class_add_pad_template (gstelement_class, templ);
150 gst_oss4_source_init (GstOss4Source * osssrc)
154 device = g_getenv ("AUDIODEV");
156 device = DEFAULT_DEVICE;
159 osssrc->device = g_strdup (device);
160 osssrc->device_name = g_strdup (DEFAULT_DEVICE_NAME);
161 osssrc->device_name = NULL;
165 gst_oss4_source_finalize (GstOss4Source * oss)
167 g_free (oss->device);
170 G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (oss));
174 gst_oss4_source_dispose (GObject * object)
176 GstOss4Source *oss = GST_OSS4_SOURCE (object);
178 if (oss->probed_caps) {
179 gst_caps_unref (oss->probed_caps);
180 oss->probed_caps = NULL;
183 G_OBJECT_CLASS (parent_class)->dispose (object);
187 gst_oss4_source_set_property (GObject * object, guint prop_id,
188 const GValue * value, GParamSpec * pspec)
192 oss = GST_OSS4_SOURCE (object);
196 GST_OBJECT_LOCK (oss);
198 g_free (oss->device);
199 oss->device = g_value_dup_string (value);
200 g_free (oss->device_name);
201 oss->device_name = NULL;
203 g_warning ("%s: can't change \"device\" property while audio source "
204 "is open", GST_OBJECT_NAME (oss));
206 GST_OBJECT_UNLOCK (oss);
209 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
215 gst_oss4_source_get_property (GObject * object, guint prop_id,
216 GValue * value, GParamSpec * pspec)
220 oss = GST_OSS4_SOURCE (object);
224 GST_OBJECT_LOCK (oss);
225 g_value_set_string (value, oss->device);
226 GST_OBJECT_UNLOCK (oss);
228 case PROP_DEVICE_NAME:
229 GST_OBJECT_LOCK (oss);
230 /* If device is set, try to retrieve the name even if we're not open */
231 if (oss->fd == -1 && oss->device != NULL) {
232 if (gst_oss4_source_open (GST_AUDIO_SRC (oss), TRUE)) {
233 g_value_set_string (value, oss->device_name);
234 gst_oss4_source_close (GST_AUDIO_SRC (oss));
238 gst_oss4_property_probe_find_device_name_nofd (GST_OBJECT (oss),
240 g_value_set_string (value, name);
244 g_value_set_string (value, oss->device_name);
247 GST_OBJECT_UNLOCK (oss);
250 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
256 gst_oss4_source_getcaps (GstBaseSrc * bsrc, GstCaps * filter)
261 oss = GST_OSS4_SOURCE (bsrc);
264 caps = gst_oss4_audio_get_template_caps ();
265 } else if (oss->probed_caps) {
266 caps = gst_caps_copy (oss->probed_caps);
268 caps = gst_oss4_audio_probe_caps (GST_OBJECT (oss), oss->fd);
269 if (caps != NULL && !gst_caps_is_empty (caps)) {
270 oss->probed_caps = gst_caps_copy (caps);
274 if (filter && caps) {
275 GstCaps *intersection;
278 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
279 gst_caps_unref (caps);
286 /* note: we must not take the object lock here unless we fix up get_property */
288 gst_oss4_source_open (GstAudioSrc * asrc, gboolean silent_errors)
294 oss = GST_OSS4_SOURCE (asrc);
297 device = g_strdup (oss->device);
299 device = gst_oss4_audio_find_device (GST_OBJECT_CAST (oss));
301 /* desperate times, desperate measures */
303 device = g_strdup ("/dev/dsp0");
305 GST_INFO_OBJECT (oss, "Trying to open OSS4 device '%s'", device);
307 /* we open in non-blocking mode even if we don't really want to do writes
308 * non-blocking because we can't be sure that this is really a genuine
309 * OSS4 device with well-behaved drivers etc. We really don't want to
310 * hang forever under any circumstances. */
311 oss->fd = open (device, O_RDONLY | O_NONBLOCK, 0);
323 GST_INFO_OBJECT (oss, "Opened device");
325 /* Make sure it's OSS4. If it's old OSS, let osssink handle it */
326 if (!gst_oss4_audio_check_version (GST_OBJECT_CAST (oss), oss->fd))
329 /* now remove the non-blocking flag. */
330 mode = fcntl (oss->fd, F_GETFL);
332 if (fcntl (oss->fd, F_SETFL, mode) < 0) {
333 /* some drivers do no support unsetting the non-blocking flag, try to
334 * close/open the device then. This is racy but we error out properly. */
335 GST_WARNING_OBJECT (oss, "failed to unset O_NONBLOCK (buggy driver?), "
336 "will try to re-open device now");
337 gst_oss4_source_close (asrc);
338 if ((oss->fd = open (device, O_RDONLY, 0)) == -1)
342 oss->open_device = device;
344 /* not using ENGINEINFO here because it sometimes returns a different and
345 * less useful name than AUDIOINFO for the same device */
346 if (!gst_oss4_property_probe_find_device_name (GST_OBJECT (oss), oss->fd,
347 oss->open_device, &oss->device_name)) {
348 oss->device_name = NULL;
356 if (!silent_errors) {
357 GST_ELEMENT_ERROR (oss, RESOURCE, BUSY,
358 (_("Could not open audio device for playback. "
359 "Device is being used by another application.")), (NULL));
366 if (!silent_errors) {
367 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
368 (_("Could not open audio device for playback. "
369 "You don't have permission to open the device.")),
377 if (!silent_errors) {
378 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
379 (_("Could not open audio device for playback.")), GST_ERROR_SYSTEM);
386 gst_oss4_source_close (asrc);
387 if (!silent_errors) {
388 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
389 (_("Could not open audio device for playback. "
390 "This version of the Open Sound System is not supported by this "
391 "element.")), ("Try the 'osssink' element instead"));
398 if (!silent_errors) {
399 GST_ELEMENT_ERROR (oss, RESOURCE, SETTINGS, (NULL),
400 ("Unable to set device %s into non-blocking mode: %s",
401 oss->device, g_strerror (errno)));
409 gst_oss4_source_open_func (GstAudioSrc * asrc)
411 return gst_oss4_source_open (asrc, FALSE);
415 gst_oss4_source_close (GstAudioSrc * asrc)
419 oss = GST_OSS4_SOURCE (asrc);
422 GST_DEBUG_OBJECT (oss, "closing device");
427 oss->bytes_per_sample = 0;
429 gst_caps_replace (&oss->probed_caps, NULL);
431 g_free (oss->open_device);
432 oss->open_device = NULL;
434 g_free (oss->device_name);
435 oss->device_name = NULL;
441 gst_oss4_source_prepare (GstAudioSrc * asrc, GstAudioRingBufferSpec * spec)
445 oss = GST_OSS4_SOURCE (asrc);
447 if (!gst_oss4_audio_set_format (GST_OBJECT_CAST (oss), oss->fd, spec)) {
448 GST_WARNING_OBJECT (oss, "Couldn't set requested format %" GST_PTR_FORMAT,
453 oss->bytes_per_sample = GST_AUDIO_INFO_BPF (&spec->info);
459 gst_oss4_source_unprepare (GstAudioSrc * asrc)
461 /* could do a SNDCTL_DSP_HALT, but the OSS manual recommends a close/open,
462 * since HALT won't properly reset some devices, apparently */
464 if (!gst_oss4_source_close (asrc))
467 if (!gst_oss4_source_open_func (asrc))
475 GST_DEBUG_OBJECT (asrc, "Couldn't close the audio device");
480 GST_DEBUG_OBJECT (asrc, "Couldn't reopen the audio device");
486 gst_oss4_source_read (GstAudioSrc * asrc, gpointer data, guint length,
487 GstClockTime * timestamp)
492 oss = GST_OSS4_SOURCE_CAST (asrc);
494 n = read (oss->fd, data, length);
495 GST_LOG_OBJECT (asrc, "%u bytes, %u samples", n, n / oss->bytes_per_sample);
497 if (G_UNLIKELY (n < 0)) {
501 /* This is the most likely cause, I think */
502 GST_ELEMENT_ERROR (asrc, RESOURCE, READ,
503 (_("Recording is not supported by this audio device.")),
504 ("read: %s (device: %s) (maybe this is an output-only device?)",
505 g_strerror (errno), oss->open_device));
509 GST_ELEMENT_ERROR (asrc, RESOURCE, READ,
510 (_("Error recording from audio device.")),
511 ("read: %s (device: %s)", g_strerror (errno), oss->open_device));
521 gst_oss4_source_delay (GstAudioSrc * asrc)
523 audio_buf_info info = { 0, };
527 oss = GST_OSS4_SOURCE_CAST (asrc);
529 if (ioctl (oss->fd, SNDCTL_DSP_GETISPACE, &info) == -1) {
530 GST_LOG_OBJECT (oss, "GETISPACE failed: %s", g_strerror (errno));
534 delay = (info.fragstotal * info.fragsize) - info.bytes;
535 GST_LOG_OBJECT (oss, "fragstotal:%d, fragsize:%d, bytes:%d, delay:%d",
536 info.fragstotal, info.fragsize, info.bytes, delay);
541 gst_oss4_source_reset (GstAudioSrc * asrc)
543 /* There's nothing we can do here really: OSS can't handle access to the
544 * same device/fd from multiple threads and might deadlock or blow up in
545 * other ways if we try an ioctl SNDCTL_DSP_HALT or similar */