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