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