Merge branch 'master' into 0.11
[platform/upstream/gst-plugins-good.git] / sys / oss / gstosssrc.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *               2000,2005 Wim Taymans <wim@fluendo.com>
4  *
5  * gstosssrc.c: 
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 /**
24  * SECTION:element-osssrc
25  *
26  * This element lets you record sound using the Open Sound System (OSS).
27  *
28  * <refsect2>
29  * <title>Example pipelines</title>
30  * |[
31  * gst-launch -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.)
35  * </refsect2>
36  */
37
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41
42 #include <sys/ioctl.h>
43 #include <fcntl.h>
44 #include <errno.h>
45 #include <unistd.h>
46 #include <string.h>
47
48 #ifdef HAVE_OSS_INCLUDE_IN_SYS
49 # include <sys/soundcard.h>
50 #else
51 # ifdef HAVE_OSS_INCLUDE_IN_ROOT
52 #  include <soundcard.h>
53 # else
54 #  ifdef HAVE_OSS_INCLUDE_IN_MACHINE
55 #   include <machine/soundcard.h>
56 #  else
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 */
61
62 #include "gstosssrc.h"
63 #include "common.h"
64
65 #include <gst/gst-i18n-plugin.h>
66
67 GST_DEBUG_CATEGORY_EXTERN (oss_debug);
68 #define GST_CAT_DEFAULT oss_debug
69
70 #define DEFAULT_DEVICE          "/dev/dsp"
71 #define DEFAULT_DEVICE_NAME     ""
72
73 enum
74 {
75   PROP_0,
76   PROP_DEVICE,
77   PROP_DEVICE_NAME,
78 };
79
80 GST_BOILERPLATE_WITH_INTERFACE (GstOssSrc, gst_oss_src, GstAudioSrc,
81     GST_TYPE_AUDIO_SRC, GstMixer, GST_TYPE_MIXER, gst_oss_src_mixer);
82
83 GST_IMPLEMENT_OSS_MIXER_METHODS (GstOssSrc, gst_oss_src_mixer);
84
85 static void gst_oss_src_get_property (GObject * object, guint prop_id,
86     GValue * value, GParamSpec * pspec);
87 static void gst_oss_src_set_property (GObject * object, guint prop_id,
88     const GValue * value, GParamSpec * pspec);
89
90 static void gst_oss_src_dispose (GObject * object);
91 static void gst_oss_src_finalize (GstOssSrc * osssrc);
92
93 static GstCaps *gst_oss_src_getcaps (GstBaseSrc * bsrc);
94
95 static gboolean gst_oss_src_open (GstAudioSrc * asrc);
96 static gboolean gst_oss_src_close (GstAudioSrc * asrc);
97 static gboolean gst_oss_src_prepare (GstAudioSrc * asrc,
98     GstRingBufferSpec * spec);
99 static gboolean gst_oss_src_unprepare (GstAudioSrc * asrc);
100 static guint gst_oss_src_read (GstAudioSrc * asrc, gpointer data, guint length);
101 static guint gst_oss_src_delay (GstAudioSrc * asrc);
102 static void gst_oss_src_reset (GstAudioSrc * asrc);
103
104 #define FORMATS "{" GST_AUDIO_NE(S16)","GST_AUDIO_NE(U16)", S8, U8 }"
105
106 static GstStaticPadTemplate osssrc_src_factory = GST_STATIC_PAD_TEMPLATE ("src",
107     GST_PAD_SRC,
108     GST_PAD_ALWAYS,
109     GST_STATIC_CAPS ("audio/x-raw, "
110         "format = (string) " FORMATS ", "
111         "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]")
112     );
113
114 static void
115 gst_oss_src_dispose (GObject * object)
116 {
117   G_OBJECT_CLASS (parent_class)->dispose (object);
118 }
119
120 static void
121 gst_oss_src_base_init (gpointer g_class)
122 {
123   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
124
125   gst_element_class_set_details_simple (element_class, "Audio Source (OSS)",
126       "Source/Audio",
127       "Capture from a sound card via OSS",
128       "Erik Walthinsen <omega@cse.ogi.edu>, " "Wim Taymans <wim@fluendo.com>");
129
130   gst_element_class_add_pad_template (element_class,
131       gst_static_pad_template_get (&osssrc_src_factory));
132 }
133
134 static void
135 gst_oss_src_class_init (GstOssSrcClass * klass)
136 {
137   GObjectClass *gobject_class;
138   GstBaseSrcClass *gstbasesrc_class;
139   GstAudioSrcClass *gstaudiosrc_class;
140
141   gobject_class = (GObjectClass *) klass;
142   gstbasesrc_class = (GstBaseSrcClass *) klass;
143   gstaudiosrc_class = (GstAudioSrcClass *) klass;
144
145   gobject_class->dispose = gst_oss_src_dispose;
146   gobject_class->finalize = (GObjectFinalizeFunc) gst_oss_src_finalize;
147   gobject_class->get_property = gst_oss_src_get_property;
148   gobject_class->set_property = gst_oss_src_set_property;
149
150   gstbasesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_oss_src_getcaps);
151
152   gstaudiosrc_class->open = GST_DEBUG_FUNCPTR (gst_oss_src_open);
153   gstaudiosrc_class->prepare = GST_DEBUG_FUNCPTR (gst_oss_src_prepare);
154   gstaudiosrc_class->unprepare = GST_DEBUG_FUNCPTR (gst_oss_src_unprepare);
155   gstaudiosrc_class->close = GST_DEBUG_FUNCPTR (gst_oss_src_close);
156   gstaudiosrc_class->read = GST_DEBUG_FUNCPTR (gst_oss_src_read);
157   gstaudiosrc_class->delay = GST_DEBUG_FUNCPTR (gst_oss_src_delay);
158   gstaudiosrc_class->reset = GST_DEBUG_FUNCPTR (gst_oss_src_reset);
159
160   g_object_class_install_property (gobject_class, PROP_DEVICE,
161       g_param_spec_string ("device", "Device",
162           "OSS device (usually /dev/dspN)", DEFAULT_DEVICE,
163           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
164
165   g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
166       g_param_spec_string ("device-name", "Device name",
167           "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
168           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
169 }
170
171 static void
172 gst_oss_src_set_property (GObject * object, guint prop_id,
173     const GValue * value, GParamSpec * pspec)
174 {
175   GstOssSrc *src;
176
177   src = GST_OSS_SRC (object);
178
179   switch (prop_id) {
180     case PROP_DEVICE:
181       if (src->device)
182         g_free (src->device);
183       src->device = g_value_dup_string (value);
184       break;
185     default:
186       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
187       break;
188   }
189 }
190
191 static void
192 gst_oss_src_get_property (GObject * object, guint prop_id,
193     GValue * value, GParamSpec * pspec)
194 {
195   GstOssSrc *src;
196
197   src = GST_OSS_SRC (object);
198
199   switch (prop_id) {
200     case PROP_DEVICE:
201       g_value_set_string (value, src->device);
202       break;
203     case PROP_DEVICE_NAME:
204       g_value_set_string (value, src->device_name);
205       break;
206     default:
207       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
208       break;
209   }
210 }
211
212 static void
213 gst_oss_src_init (GstOssSrc * osssrc, GstOssSrcClass * g_class)
214 {
215   const gchar *device;
216
217   GST_DEBUG ("initializing osssrc");
218
219   device = g_getenv ("AUDIODEV");
220   if (device == NULL)
221     device = DEFAULT_DEVICE;
222
223   osssrc->fd = -1;
224   osssrc->device = g_strdup (device);
225   osssrc->device_name = g_strdup (DEFAULT_DEVICE_NAME);
226   osssrc->probed_caps = NULL;
227 }
228
229 static void
230 gst_oss_src_finalize (GstOssSrc * osssrc)
231 {
232   g_free (osssrc->device);
233   g_free (osssrc->device_name);
234
235   G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (osssrc));
236 }
237
238 static GstCaps *
239 gst_oss_src_getcaps (GstBaseSrc * bsrc)
240 {
241   GstOssSrc *osssrc;
242   GstCaps *caps;
243
244   osssrc = GST_OSS_SRC (bsrc);
245
246   if (osssrc->fd == -1) {
247     GST_DEBUG_OBJECT (osssrc, "device not open, using template caps");
248     return NULL;                /* base class will get template caps for us */
249   }
250
251   if (osssrc->probed_caps) {
252     GST_LOG_OBJECT (osssrc, "Returning cached caps");
253     return gst_caps_ref (osssrc->probed_caps);
254   }
255
256   caps = gst_oss_helper_probe_caps (osssrc->fd);
257
258   if (caps) {
259     osssrc->probed_caps = gst_caps_ref (caps);
260   }
261
262   GST_INFO_OBJECT (osssrc, "returning caps %" GST_PTR_FORMAT, caps);
263
264   return caps;
265 }
266
267 static gint
268 ilog2 (gint x)
269 {
270   /* well... hacker's delight explains... */
271   x = x | (x >> 1);
272   x = x | (x >> 2);
273   x = x | (x >> 4);
274   x = x | (x >> 8);
275   x = x | (x >> 16);
276   x = x - ((x >> 1) & 0x55555555);
277   x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
278   x = (x + (x >> 4)) & 0x0f0f0f0f;
279   x = x + (x >> 8);
280   x = x + (x >> 16);
281   return (x & 0x0000003f) - 1;
282 }
283
284 static gint
285 gst_oss_src_get_format (GstBufferFormat fmt)
286 {
287   gint result;
288
289   switch (fmt) {
290     case GST_MU_LAW:
291       result = AFMT_MU_LAW;
292       break;
293     case GST_A_LAW:
294       result = AFMT_A_LAW;
295       break;
296     case GST_IMA_ADPCM:
297       result = AFMT_IMA_ADPCM;
298       break;
299     case GST_U8:
300       result = AFMT_U8;
301       break;
302     case GST_S16_LE:
303       result = AFMT_S16_LE;
304       break;
305     case GST_S16_BE:
306       result = AFMT_S16_BE;
307       break;
308     case GST_S8:
309       result = AFMT_S8;
310       break;
311     case GST_U16_LE:
312       result = AFMT_U16_LE;
313       break;
314     case GST_U16_BE:
315       result = AFMT_U16_BE;
316       break;
317     case GST_MPEG:
318       result = AFMT_MPEG;
319       break;
320     default:
321       result = 0;
322       break;
323   }
324   return result;
325 }
326
327 static gboolean
328 gst_oss_src_open (GstAudioSrc * asrc)
329 {
330   GstOssSrc *oss;
331   int mode;
332
333   oss = GST_OSS_SRC (asrc);
334
335   mode = O_RDONLY;
336   mode |= O_NONBLOCK;
337
338   oss->fd = open (oss->device, mode, 0);
339   if (oss->fd == -1) {
340     switch (errno) {
341       case EACCES:
342         goto no_permission;
343       default:
344         goto open_failed;
345     }
346   }
347
348   if (!oss->mixer) {
349     oss->mixer = gst_ossmixer_new ("/dev/mixer", GST_OSS_MIXER_CAPTURE);
350
351     if (oss->mixer) {
352       g_free (oss->device_name);
353       oss->device_name = g_strdup (oss->mixer->cardname);
354     }
355   }
356   return TRUE;
357
358 no_permission:
359   {
360     GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
361         (_("Could not open audio device for recording. "
362                 "You don't have permission to open the device.")),
363         GST_ERROR_SYSTEM);
364     return FALSE;
365   }
366 open_failed:
367   {
368     GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
369         (_("Could not open audio device for recording.")),
370         ("Unable to open device %s for recording: %s",
371             oss->device, g_strerror (errno)));
372     return FALSE;
373   }
374 }
375
376 static gboolean
377 gst_oss_src_close (GstAudioSrc * asrc)
378 {
379   GstOssSrc *oss;
380
381   oss = GST_OSS_SRC (asrc);
382
383   close (oss->fd);
384
385   if (oss->mixer) {
386     gst_ossmixer_free (oss->mixer);
387     oss->mixer = NULL;
388   }
389
390   gst_caps_replace (&oss->probed_caps, NULL);
391
392   return TRUE;
393 }
394
395 static gboolean
396 gst_oss_src_prepare (GstAudioSrc * asrc, GstRingBufferSpec * spec)
397 {
398   GstOssSrc *oss;
399   struct audio_buf_info info;
400   int mode;
401   int fmt, tmp;
402
403   oss = GST_OSS_SRC (asrc);
404
405   mode = fcntl (oss->fd, F_GETFL);
406   mode &= ~O_NONBLOCK;
407   if (fcntl (oss->fd, F_SETFL, mode) == -1)
408     goto non_block;
409
410   fmt = gst_oss_src_get_format (spec->format);
411   if (fmt == 0)
412     goto wrong_format;
413
414   tmp = ilog2 (spec->segsize);
415   tmp = ((spec->segtotal & 0x7fff) << 16) | tmp;
416   GST_DEBUG_OBJECT (oss, "set segsize: %d, segtotal: %d, value: %08x",
417       spec->segsize, spec->segtotal, tmp);
418
419   SET_PARAM (oss, SNDCTL_DSP_SETFRAGMENT, tmp, "SETFRAGMENT");
420
421   SET_PARAM (oss, SNDCTL_DSP_RESET, 0, "RESET");
422
423   SET_PARAM (oss, SNDCTL_DSP_SETFMT, fmt, "SETFMT");
424   if (spec->channels == 2)
425     SET_PARAM (oss, SNDCTL_DSP_STEREO, 1, "STEREO");
426   SET_PARAM (oss, SNDCTL_DSP_CHANNELS, spec->channels, "CHANNELS");
427   SET_PARAM (oss, SNDCTL_DSP_SPEED, spec->rate, "SPEED");
428
429   GET_PARAM (oss, SNDCTL_DSP_GETISPACE, &info, "GETISPACE");
430
431   spec->segsize = info.fragsize;
432   spec->segtotal = info.fragstotal;
433
434   if (spec->width != 16 && spec->width != 8)
435     goto dodgy_width;
436
437   spec->bytes_per_sample = (spec->width / 8) * spec->channels;
438   oss->bytes_per_sample = (spec->width / 8) * spec->channels;
439   memset (spec->silence_sample, 0, spec->bytes_per_sample);
440
441   GST_DEBUG_OBJECT (oss, "got segsize: %d, segtotal: %d, value: %08x",
442       spec->segsize, spec->segtotal, tmp);
443
444   return TRUE;
445
446 non_block:
447   {
448     GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
449         ("Unable to set device %s in non blocking mode: %s",
450             oss->device, g_strerror (errno)), (NULL));
451     return FALSE;
452   }
453 wrong_format:
454   {
455     GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
456         ("Unable to get format %d", spec->format), (NULL));
457     return FALSE;
458   }
459 dodgy_width:
460   {
461     GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
462         ("Unexpected width %d", spec->width), (NULL));
463     return FALSE;
464   }
465 }
466
467 static gboolean
468 gst_oss_src_unprepare (GstAudioSrc * asrc)
469 {
470   /* could do a SNDCTL_DSP_RESET, but the OSS manual recommends a close/open */
471
472   if (!gst_oss_src_close (asrc))
473     goto couldnt_close;
474
475   if (!gst_oss_src_open (asrc))
476     goto couldnt_reopen;
477
478   return TRUE;
479
480 couldnt_close:
481   {
482     GST_DEBUG_OBJECT (asrc, "Could not close the audio device");
483     return FALSE;
484   }
485 couldnt_reopen:
486   {
487     GST_DEBUG_OBJECT (asrc, "Could not reopen the audio device");
488     return FALSE;
489   }
490 }
491
492 static guint
493 gst_oss_src_read (GstAudioSrc * asrc, gpointer data, guint length)
494 {
495   return read (GST_OSS_SRC (asrc)->fd, data, length);
496 }
497
498 static guint
499 gst_oss_src_delay (GstAudioSrc * asrc)
500 {
501   GstOssSrc *oss;
502   gint delay = 0;
503   gint ret;
504
505   oss = GST_OSS_SRC (asrc);
506
507 #ifdef SNDCTL_DSP_GETODELAY
508   ret = ioctl (oss->fd, SNDCTL_DSP_GETODELAY, &delay);
509 #else
510   ret = -1;
511 #endif
512   if (ret < 0) {
513     audio_buf_info info;
514
515     ret = ioctl (oss->fd, SNDCTL_DSP_GETOSPACE, &info);
516
517     delay = (ret < 0 ? 0 : (info.fragstotal * info.fragsize) - info.bytes);
518   }
519   return delay / oss->bytes_per_sample;
520 }
521
522 static void
523 gst_oss_src_reset (GstAudioSrc * asrc)
524 {
525   /* There's nothing we can do here really: OSS can't handle access to the
526    * same device/fd from multiple threads and might deadlock or blow up in
527    * other ways if we try an ioctl SNDCTL_DSP_RESET or similar */
528 }