1 /* GStreamer OSS4 audio source
2  * Copyright (C) 2007-2008 Tim-Philipp Müller <tim centricular net>
3  *
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.
8  *
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.
13  *
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.
18  */
19 
20 /**
21  * SECTION:element-oss4src
22  *
23  * This element lets you record sound using the Open Sound System (OSS)
24  * version 4.
25  *
26  * <refsect2>
27  * <title>Example pipelines</title>
28  * |[
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.)
33  * </refsect2>
34  */
35 
36 /* FIXME: make sure we're not doing ioctls from the app thread (e.g. via the
37  * mixer interface) while recording */
38 
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
42 
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <sys/ioctl.h>
46 #include <fcntl.h>
47 #include <errno.h>
48 #include <unistd.h>
49 #include <string.h>
50 
51 #include <gst/gst-i18n-plugin.h>
52 
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"
58 
59 #define GST_OSS4_SOURCE_IS_OPEN(src)  (GST_OSS4_SOURCE(src)->fd != -1)
60 
61 GST_DEBUG_CATEGORY_EXTERN (oss4src_debug);
62 #define GST_CAT_DEFAULT oss4src_debug
63 
64 #define DEFAULT_DEVICE       NULL
65 #define DEFAULT_DEVICE_NAME  NULL
66 
67 enum
68 {
69   PROP_0,
70   PROP_DEVICE,
71   PROP_DEVICE_NAME
72 };
73 
74 #define gst_oss4_source_parent_class parent_class
75 G_DEFINE_TYPE (GstOss4Source, gst_oss4_source, GST_TYPE_AUDIO_SRC);
76 
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);
81 
82 static void gst_oss4_source_dispose (GObject * object);
83 static void gst_oss4_source_finalize (GstOss4Source * osssrc);
84 
85 static GstCaps *gst_oss4_source_getcaps (GstBaseSrc * bsrc, GstCaps * filter);
86 
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);
98 
99 static void
gst_oss4_source_class_init(GstOss4SourceClass * klass)100 gst_oss4_source_class_init (GstOss4SourceClass * klass)
101 {
102   GObjectClass *gobject_class;
103   GstElementClass *gstelement_class;
104   GstBaseSrcClass *gstbasesrc_class;
105   GstAudioSrcClass *gstaudiosrc_class;
106   GstPadTemplate *templ;
107 
108   gobject_class = (GObjectClass *) klass;
109   gstelement_class = (GstElementClass *) klass;
110   gstbasesrc_class = (GstBaseSrcClass *) klass;
111   gstaudiosrc_class = (GstAudioSrcClass *) klass;
112 
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;
117 
118   gstbasesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_oss4_source_getcaps);
119 
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);
127 
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));
133 
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));
138 
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>");
143 
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);
147 }
148 
149 static void
gst_oss4_source_init(GstOss4Source * osssrc)150 gst_oss4_source_init (GstOss4Source * osssrc)
151 {
152   const gchar *device;
153 
154   device = g_getenv ("AUDIODEV");
155   if (device == NULL)
156     device = DEFAULT_DEVICE;
157 
158   osssrc->fd = -1;
159   osssrc->device = g_strdup (device);
160   osssrc->device_name = g_strdup (DEFAULT_DEVICE_NAME);
161   osssrc->device_name = NULL;
162 }
163 
164 static void
gst_oss4_source_finalize(GstOss4Source * oss)165 gst_oss4_source_finalize (GstOss4Source * oss)
166 {
167   g_free (oss->device);
168   oss->device = NULL;
169 
170   G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (oss));
171 }
172 
173 static void
gst_oss4_source_dispose(GObject * object)174 gst_oss4_source_dispose (GObject * object)
175 {
176   GstOss4Source *oss = GST_OSS4_SOURCE (object);
177 
178   if (oss->probed_caps) {
179     gst_caps_unref (oss->probed_caps);
180     oss->probed_caps = NULL;
181   }
182 
183   G_OBJECT_CLASS (parent_class)->dispose (object);
184 }
185 
186 static void
gst_oss4_source_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)187 gst_oss4_source_set_property (GObject * object, guint prop_id,
188     const GValue * value, GParamSpec * pspec)
189 {
190   GstOss4Source *oss;
191 
192   oss = GST_OSS4_SOURCE (object);
193 
194   switch (prop_id) {
195     case PROP_DEVICE:
196       GST_OBJECT_LOCK (oss);
197       if (oss->fd == -1) {
198         g_free (oss->device);
199         oss->device = g_value_dup_string (value);
200         g_free (oss->device_name);
201         oss->device_name = NULL;
202       } else {
203         g_warning ("%s: can't change \"device\" property while audio source "
204             "is open", GST_OBJECT_NAME (oss));
205       }
206       GST_OBJECT_UNLOCK (oss);
207       break;
208     default:
209       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
210       break;
211   }
212 }
213 
214 static void
gst_oss4_source_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)215 gst_oss4_source_get_property (GObject * object, guint prop_id,
216     GValue * value, GParamSpec * pspec)
217 {
218   GstOss4Source *oss;
219 
220   oss = GST_OSS4_SOURCE (object);
221 
222   switch (prop_id) {
223     case PROP_DEVICE:
224       GST_OBJECT_LOCK (oss);
225       g_value_set_string (value, oss->device);
226       GST_OBJECT_UNLOCK (oss);
227       break;
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));
235         } else {
236           gchar *name = NULL;
237 
238           gst_oss4_property_probe_find_device_name_nofd (GST_OBJECT (oss),
239               oss->device, &name);
240           g_value_set_string (value, name);
241           g_free (name);
242         }
243       } else {
244         g_value_set_string (value, oss->device_name);
245       }
246 
247       GST_OBJECT_UNLOCK (oss);
248       break;
249     default:
250       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
251       break;
252   }
253 }
254 
255 static GstCaps *
gst_oss4_source_getcaps(GstBaseSrc * bsrc,GstCaps * filter)256 gst_oss4_source_getcaps (GstBaseSrc * bsrc, GstCaps * filter)
257 {
258   GstOss4Source *oss;
259   GstCaps *caps;
260 
261   oss = GST_OSS4_SOURCE (bsrc);
262 
263   if (oss->fd == -1) {
264     caps = gst_oss4_audio_get_template_caps ();
265   } else if (oss->probed_caps) {
266     caps = gst_caps_copy (oss->probed_caps);
267   } else {
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);
271     }
272   }
273 
274   if (filter && caps) {
275     GstCaps *intersection;
276 
277     intersection =
278         gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
279     gst_caps_unref (caps);
280     return intersection;
281   } else {
282     return caps;
283   }
284 }
285 
286 /* note: we must not take the object lock here unless we fix up get_property */
287 static gboolean
gst_oss4_source_open(GstAudioSrc * asrc,gboolean silent_errors)288 gst_oss4_source_open (GstAudioSrc * asrc, gboolean silent_errors)
289 {
290   GstOss4Source *oss;
291   gchar *device;
292   int mode;
293 
294   oss = GST_OSS4_SOURCE (asrc);
295 
296   if (oss->device)
297     device = g_strdup (oss->device);
298   else
299     device = gst_oss4_audio_find_device (GST_OBJECT_CAST (oss));
300 
301   /* desperate times, desperate measures */
302   if (device == NULL)
303     device = g_strdup ("/dev/dsp0");
304 
305   GST_INFO_OBJECT (oss, "Trying to open OSS4 device '%s'", device);
306 
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);
312   if (oss->fd == -1) {
313     switch (errno) {
314       case EBUSY:
315         goto busy;
316       case EACCES:
317         goto no_permission;
318       default:
319         goto open_failed;
320     }
321   }
322 
323   GST_INFO_OBJECT (oss, "Opened device");
324 
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))
327     goto legacy_oss;
328 
329   /* now remove the non-blocking flag. */
330   mode = fcntl (oss->fd, F_GETFL);
331   mode &= ~O_NONBLOCK;
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)
339       goto non_block;
340   }
341 
342   oss->open_device = device;
343 
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;
349   }
350 
351   return TRUE;
352 
353   /* ERRORS */
354 busy:
355   {
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));
360     }
361     g_free (device);
362     return FALSE;
363   }
364 no_permission:
365   {
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.")),
370           GST_ERROR_SYSTEM);
371     }
372     g_free (device);
373     return FALSE;
374   }
375 open_failed:
376   {
377     if (!silent_errors) {
378       GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
379           (_("Could not open audio device for playback.")), GST_ERROR_SYSTEM);
380     }
381     g_free (device);
382     return FALSE;
383   }
384 legacy_oss:
385   {
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"));
392     }
393     g_free (device);
394     return FALSE;
395   }
396 non_block:
397   {
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)));
402     }
403     g_free (device);
404     return FALSE;
405   }
406 }
407 
408 static gboolean
gst_oss4_source_open_func(GstAudioSrc * asrc)409 gst_oss4_source_open_func (GstAudioSrc * asrc)
410 {
411   return gst_oss4_source_open (asrc, FALSE);
412 }
413 
414 static gboolean
gst_oss4_source_close(GstAudioSrc * asrc)415 gst_oss4_source_close (GstAudioSrc * asrc)
416 {
417   GstOss4Source *oss;
418 
419   oss = GST_OSS4_SOURCE (asrc);
420 
421   if (oss->fd != -1) {
422     GST_DEBUG_OBJECT (oss, "closing device");
423     close (oss->fd);
424     oss->fd = -1;
425   }
426 
427   oss->bytes_per_sample = 0;
428 
429   gst_caps_replace (&oss->probed_caps, NULL);
430 
431   g_free (oss->open_device);
432   oss->open_device = NULL;
433 
434   g_free (oss->device_name);
435   oss->device_name = NULL;
436 
437   return TRUE;
438 }
439 
440 static gboolean
gst_oss4_source_prepare(GstAudioSrc * asrc,GstAudioRingBufferSpec * spec)441 gst_oss4_source_prepare (GstAudioSrc * asrc, GstAudioRingBufferSpec * spec)
442 {
443   GstOss4Source *oss;
444 
445   oss = GST_OSS4_SOURCE (asrc);
446 
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,
449         spec->caps);
450     return FALSE;
451   }
452 
453   oss->bytes_per_sample = GST_AUDIO_INFO_BPF (&spec->info);
454 
455   return TRUE;
456 }
457 
458 static gboolean
gst_oss4_source_unprepare(GstAudioSrc * asrc)459 gst_oss4_source_unprepare (GstAudioSrc * asrc)
460 {
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 */
463 
464   if (!gst_oss4_source_close (asrc))
465     goto couldnt_close;
466 
467   if (!gst_oss4_source_open_func (asrc))
468     goto couldnt_reopen;
469 
470   return TRUE;
471 
472   /* ERRORS */
473 couldnt_close:
474   {
475     GST_DEBUG_OBJECT (asrc, "Couldn't close the audio device");
476     return FALSE;
477   }
478 couldnt_reopen:
479   {
480     GST_DEBUG_OBJECT (asrc, "Couldn't reopen the audio device");
481     return FALSE;
482   }
483 }
484 
485 static guint
gst_oss4_source_read(GstAudioSrc * asrc,gpointer data,guint length,GstClockTime * timestamp)486 gst_oss4_source_read (GstAudioSrc * asrc, gpointer data, guint length,
487     GstClockTime * timestamp)
488 {
489   GstOss4Source *oss;
490   int n;
491 
492   oss = GST_OSS4_SOURCE_CAST (asrc);
493 
494   n = read (oss->fd, data, length);
495   GST_LOG_OBJECT (asrc, "%u bytes, %u samples", n, n / oss->bytes_per_sample);
496 
497   if (G_UNLIKELY (n < 0)) {
498     switch (errno) {
499       case ENOTSUP:
500       case EACCES:{
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));
506         break;
507       }
508       default:{
509         GST_ELEMENT_ERROR (asrc, RESOURCE, READ,
510             (_("Error recording from audio device.")),
511             ("read: %s (device: %s)", g_strerror (errno), oss->open_device));
512         break;
513       }
514     }
515   }
516 
517   return (guint) n;
518 }
519 
520 static guint
gst_oss4_source_delay(GstAudioSrc * asrc)521 gst_oss4_source_delay (GstAudioSrc * asrc)
522 {
523   audio_buf_info info = { 0, };
524   GstOss4Source *oss;
525   guint delay;
526 
527   oss = GST_OSS4_SOURCE_CAST (asrc);
528 
529   if (ioctl (oss->fd, SNDCTL_DSP_GETISPACE, &info) == -1) {
530     GST_LOG_OBJECT (oss, "GETISPACE failed: %s", g_strerror (errno));
531     return 0;
532   }
533 
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);
537   return delay;
538 }
539 
540 static void
gst_oss4_source_reset(GstAudioSrc * asrc)541 gst_oss4_source_reset (GstAudioSrc * asrc)
542 {
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 */
546 }
547