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