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
24 * This element lets you record sound using the Open Sound System (OSS)
27 * ## Example pipelines
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);
76 GST_ELEMENT_REGISTER_DEFINE (oss4src, "oss4src", GST_RANK_SECONDARY + 1,
77 GST_TYPE_OSS4_SOURCE);
79 static void gst_oss4_source_get_property (GObject * object, guint prop_id,
80 GValue * value, GParamSpec * pspec);
81 static void gst_oss4_source_set_property (GObject * object, guint prop_id,
82 const GValue * value, GParamSpec * pspec);
84 static void gst_oss4_source_dispose (GObject * object);
85 static void gst_oss4_source_finalize (GstOss4Source * osssrc);
87 static GstCaps *gst_oss4_source_getcaps (GstBaseSrc * bsrc, GstCaps * filter);
89 static gboolean gst_oss4_source_open (GstAudioSrc * asrc,
90 gboolean silent_errors);
91 static gboolean gst_oss4_source_open_func (GstAudioSrc * asrc);
92 static gboolean gst_oss4_source_close (GstAudioSrc * asrc);
93 static gboolean gst_oss4_source_prepare (GstAudioSrc * asrc,
94 GstAudioRingBufferSpec * spec);
95 static gboolean gst_oss4_source_unprepare (GstAudioSrc * asrc);
96 static guint gst_oss4_source_read (GstAudioSrc * asrc, gpointer data,
97 guint length, GstClockTime * timestamp);
98 static guint gst_oss4_source_delay (GstAudioSrc * asrc);
99 static void gst_oss4_source_reset (GstAudioSrc * asrc);
102 gst_oss4_source_class_init (GstOss4SourceClass * klass)
104 GObjectClass *gobject_class;
105 GstElementClass *gstelement_class;
106 GstBaseSrcClass *gstbasesrc_class;
107 GstAudioSrcClass *gstaudiosrc_class;
108 GstPadTemplate *templ;
110 gobject_class = (GObjectClass *) klass;
111 gstelement_class = (GstElementClass *) klass;
112 gstbasesrc_class = (GstBaseSrcClass *) klass;
113 gstaudiosrc_class = (GstAudioSrcClass *) klass;
115 gobject_class->dispose = gst_oss4_source_dispose;
116 gobject_class->finalize = (GObjectFinalizeFunc) gst_oss4_source_finalize;
117 gobject_class->get_property = gst_oss4_source_get_property;
118 gobject_class->set_property = gst_oss4_source_set_property;
120 gstbasesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_oss4_source_getcaps);
122 gstaudiosrc_class->open = GST_DEBUG_FUNCPTR (gst_oss4_source_open_func);
123 gstaudiosrc_class->prepare = GST_DEBUG_FUNCPTR (gst_oss4_source_prepare);
124 gstaudiosrc_class->unprepare = GST_DEBUG_FUNCPTR (gst_oss4_source_unprepare);
125 gstaudiosrc_class->close = GST_DEBUG_FUNCPTR (gst_oss4_source_close);
126 gstaudiosrc_class->read = GST_DEBUG_FUNCPTR (gst_oss4_source_read);
127 gstaudiosrc_class->delay = GST_DEBUG_FUNCPTR (gst_oss4_source_delay);
128 gstaudiosrc_class->reset = GST_DEBUG_FUNCPTR (gst_oss4_source_reset);
130 g_object_class_install_property (gobject_class, PROP_DEVICE,
131 g_param_spec_string ("device", "Device",
132 "OSS4 device (e.g. /dev/oss/hdaudio0/pcm0 or /dev/dspN) "
133 "(NULL = use first available device)",
134 DEFAULT_DEVICE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
136 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
137 g_param_spec_string ("device-name", "Device name",
138 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
139 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
141 gst_element_class_set_static_metadata (gstelement_class,
142 "OSS v4 Audio Source", "Source/Audio",
143 "Capture from a sound card via OSS version 4",
144 "Tim-Philipp Müller <tim centricular net>");
146 templ = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
147 gst_oss4_audio_get_template_caps ());
148 gst_element_class_add_pad_template (gstelement_class, templ);
152 gst_oss4_source_init (GstOss4Source * osssrc)
156 device = g_getenv ("AUDIODEV");
158 device = DEFAULT_DEVICE;
161 osssrc->device = g_strdup (device);
162 osssrc->device_name = g_strdup (DEFAULT_DEVICE_NAME);
163 osssrc->device_name = NULL;
167 gst_oss4_source_finalize (GstOss4Source * oss)
169 g_free (oss->device);
172 G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (oss));
176 gst_oss4_source_dispose (GObject * object)
178 GstOss4Source *oss = GST_OSS4_SOURCE (object);
180 if (oss->probed_caps) {
181 gst_caps_unref (oss->probed_caps);
182 oss->probed_caps = NULL;
185 G_OBJECT_CLASS (parent_class)->dispose (object);
189 gst_oss4_source_set_property (GObject * object, guint prop_id,
190 const GValue * value, GParamSpec * pspec)
194 oss = GST_OSS4_SOURCE (object);
198 GST_OBJECT_LOCK (oss);
200 g_free (oss->device);
201 oss->device = g_value_dup_string (value);
202 g_free (oss->device_name);
203 oss->device_name = NULL;
205 g_warning ("%s: can't change \"device\" property while audio source "
206 "is open", GST_OBJECT_NAME (oss));
208 GST_OBJECT_UNLOCK (oss);
211 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
217 gst_oss4_source_get_property (GObject * object, guint prop_id,
218 GValue * value, GParamSpec * pspec)
222 oss = GST_OSS4_SOURCE (object);
226 GST_OBJECT_LOCK (oss);
227 g_value_set_string (value, oss->device);
228 GST_OBJECT_UNLOCK (oss);
230 case PROP_DEVICE_NAME:
231 GST_OBJECT_LOCK (oss);
232 /* If device is set, try to retrieve the name even if we're not open */
233 if (oss->fd == -1 && oss->device != NULL) {
234 if (gst_oss4_source_open (GST_AUDIO_SRC (oss), TRUE)) {
235 g_value_set_string (value, oss->device_name);
236 gst_oss4_source_close (GST_AUDIO_SRC (oss));
240 gst_oss4_property_probe_find_device_name_nofd (GST_OBJECT (oss),
242 g_value_set_string (value, name);
246 g_value_set_string (value, oss->device_name);
249 GST_OBJECT_UNLOCK (oss);
252 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
258 gst_oss4_source_getcaps (GstBaseSrc * bsrc, GstCaps * filter)
263 oss = GST_OSS4_SOURCE (bsrc);
266 caps = gst_oss4_audio_get_template_caps ();
267 } else if (oss->probed_caps) {
268 caps = gst_caps_copy (oss->probed_caps);
270 caps = gst_oss4_audio_probe_caps (GST_OBJECT (oss), oss->fd);
271 if (caps != NULL && !gst_caps_is_empty (caps)) {
272 oss->probed_caps = gst_caps_copy (caps);
276 if (filter && caps) {
277 GstCaps *intersection;
280 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
281 gst_caps_unref (caps);
288 /* note: we must not take the object lock here unless we fix up get_property */
290 gst_oss4_source_open (GstAudioSrc * asrc, gboolean silent_errors)
296 oss = GST_OSS4_SOURCE (asrc);
299 device = g_strdup (oss->device);
301 device = gst_oss4_audio_find_device (GST_OBJECT_CAST (oss));
303 /* desperate times, desperate measures */
305 device = g_strdup ("/dev/dsp0");
307 GST_INFO_OBJECT (oss, "Trying to open OSS4 device '%s'", device);
309 /* we open in non-blocking mode even if we don't really want to do writes
310 * non-blocking because we can't be sure that this is really a genuine
311 * OSS4 device with well-behaved drivers etc. We really don't want to
312 * hang forever under any circumstances. */
313 oss->fd = open (device, O_RDONLY | O_NONBLOCK, 0);
325 GST_INFO_OBJECT (oss, "Opened device");
327 /* Make sure it's OSS4. If it's old OSS, let osssink handle it */
328 if (!gst_oss4_audio_check_version (GST_OBJECT_CAST (oss), oss->fd))
331 /* now remove the non-blocking flag. */
332 mode = fcntl (oss->fd, F_GETFL);
334 if (fcntl (oss->fd, F_SETFL, mode) < 0) {
335 /* some drivers do no support unsetting the non-blocking flag, try to
336 * close/open the device then. This is racy but we error out properly. */
337 GST_WARNING_OBJECT (oss, "failed to unset O_NONBLOCK (buggy driver?), "
338 "will try to re-open device now");
339 gst_oss4_source_close (asrc);
340 if ((oss->fd = open (device, O_RDONLY, 0)) == -1)
344 oss->open_device = device;
346 /* not using ENGINEINFO here because it sometimes returns a different and
347 * less useful name than AUDIOINFO for the same device */
348 if (!gst_oss4_property_probe_find_device_name (GST_OBJECT (oss), oss->fd,
349 oss->open_device, &oss->device_name)) {
350 oss->device_name = NULL;
358 if (!silent_errors) {
359 GST_ELEMENT_ERROR (oss, RESOURCE, BUSY,
360 (_("Could not open audio device for playback. "
361 "Device is being used by another application.")), (NULL));
368 if (!silent_errors) {
369 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
370 (_("Could not open audio device for playback. "
371 "You don't have permission to open the device.")),
379 if (!silent_errors) {
380 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
381 (_("Could not open audio device for playback.")), GST_ERROR_SYSTEM);
388 gst_oss4_source_close (asrc);
389 if (!silent_errors) {
390 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
391 (_("Could not open audio device for playback. "
392 "This version of the Open Sound System is not supported by this "
393 "element.")), ("Try the 'osssink' element instead"));
400 if (!silent_errors) {
401 GST_ELEMENT_ERROR (oss, RESOURCE, SETTINGS, (NULL),
402 ("Unable to set device %s into non-blocking mode: %s",
403 oss->device, g_strerror (errno)));
411 gst_oss4_source_open_func (GstAudioSrc * asrc)
413 return gst_oss4_source_open (asrc, FALSE);
417 gst_oss4_source_close (GstAudioSrc * asrc)
421 oss = GST_OSS4_SOURCE (asrc);
424 GST_DEBUG_OBJECT (oss, "closing device");
429 oss->bytes_per_sample = 0;
431 gst_caps_replace (&oss->probed_caps, NULL);
433 g_free (oss->open_device);
434 oss->open_device = NULL;
436 g_free (oss->device_name);
437 oss->device_name = NULL;
443 gst_oss4_source_prepare (GstAudioSrc * asrc, GstAudioRingBufferSpec * spec)
447 oss = GST_OSS4_SOURCE (asrc);
449 if (!gst_oss4_audio_set_format (GST_OBJECT_CAST (oss), oss->fd, spec)) {
450 GST_WARNING_OBJECT (oss, "Couldn't set requested format %" GST_PTR_FORMAT,
455 oss->bytes_per_sample = GST_AUDIO_INFO_BPF (&spec->info);
461 gst_oss4_source_unprepare (GstAudioSrc * asrc)
463 /* could do a SNDCTL_DSP_HALT, but the OSS manual recommends a close/open,
464 * since HALT won't properly reset some devices, apparently */
466 if (!gst_oss4_source_close (asrc))
469 if (!gst_oss4_source_open_func (asrc))
477 GST_DEBUG_OBJECT (asrc, "Couldn't close the audio device");
482 GST_DEBUG_OBJECT (asrc, "Couldn't reopen the audio device");
488 gst_oss4_source_read (GstAudioSrc * asrc, gpointer data, guint length,
489 GstClockTime * timestamp)
494 oss = GST_OSS4_SOURCE_CAST (asrc);
496 n = read (oss->fd, data, length);
497 GST_LOG_OBJECT (asrc, "%u bytes, %u samples", n, n / oss->bytes_per_sample);
499 if (G_UNLIKELY (n < 0)) {
503 /* This is the most likely cause, I think */
504 GST_ELEMENT_ERROR (asrc, RESOURCE, READ,
505 (_("Recording is not supported by this audio device.")),
506 ("read: %s (device: %s) (maybe this is an output-only device?)",
507 g_strerror (errno), oss->open_device));
511 GST_ELEMENT_ERROR (asrc, RESOURCE, READ,
512 (_("Error recording from audio device.")),
513 ("read: %s (device: %s)", g_strerror (errno), oss->open_device));
523 gst_oss4_source_delay (GstAudioSrc * asrc)
525 audio_buf_info info = { 0, };
529 oss = GST_OSS4_SOURCE_CAST (asrc);
531 if (ioctl (oss->fd, SNDCTL_DSP_GETISPACE, &info) == -1) {
532 GST_LOG_OBJECT (oss, "GETISPACE failed: %s", g_strerror (errno));
536 delay = (info.fragstotal * info.fragsize) - info.bytes;
537 GST_LOG_OBJECT (oss, "fragstotal:%d, fragsize:%d, bytes:%d, delay:%d",
538 info.fragstotal, info.fragsize, info.bytes, delay);
543 gst_oss4_source_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_HALT or similar */