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