tizen 2.3.1 release
[framework/multimedia/gst-plugins-base0.10.git] / ext / alsa / gstalsa.c
1 /* Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
2  *
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Library General Public
5  * License as published by the Free Software Foundation; either
6  * version 2 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Library General Public License for more details.
12  *
13  * You should have received a copy of the GNU Library General Public
14  * License along with this library; if not, write to the Free
15  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16  */
17
18 #include "gstalsa.h"
19
20 #include <gst/audio/multichannel.h>
21
22 static GstCaps *
23 gst_alsa_detect_rates (GstObject * obj, snd_pcm_hw_params_t * hw_params,
24     GstCaps * in_caps)
25 {
26   GstCaps *caps;
27   guint min, max;
28   gint err, dir, min_rate, max_rate, i;
29
30   GST_LOG_OBJECT (obj, "probing sample rates ...");
31
32   if ((err = snd_pcm_hw_params_get_rate_min (hw_params, &min, &dir)) < 0)
33     goto min_rate_err;
34
35   if ((err = snd_pcm_hw_params_get_rate_max (hw_params, &max, &dir)) < 0)
36     goto max_rate_err;
37
38   min_rate = min;
39   max_rate = max;
40
41   if (min_rate < 4000)
42     min_rate = 4000;            /* random 'sensible minimum' */
43
44   if (max_rate <= 0)
45     max_rate = G_MAXINT;        /* or maybe just use 192400 or so? */
46   else if (max_rate > 0 && max_rate < 4000)
47     max_rate = MAX (4000, min_rate);
48
49   GST_DEBUG_OBJECT (obj, "Min. rate = %u (%d)", min_rate, min);
50   GST_DEBUG_OBJECT (obj, "Max. rate = %u (%d)", max_rate, max);
51
52   caps = gst_caps_make_writable (in_caps);
53
54   for (i = 0; i < gst_caps_get_size (caps); ++i) {
55     GstStructure *s;
56
57     s = gst_caps_get_structure (caps, i);
58     if (min_rate == max_rate) {
59       gst_structure_set (s, "rate", G_TYPE_INT, min_rate, NULL);
60     } else {
61       gst_structure_set (s, "rate", GST_TYPE_INT_RANGE,
62           min_rate, max_rate, NULL);
63     }
64   }
65
66   return caps;
67
68   /* ERRORS */
69 min_rate_err:
70   {
71     GST_ERROR_OBJECT (obj, "failed to query minimum sample rate: %s",
72         snd_strerror (err));
73     gst_caps_unref (in_caps);
74     return NULL;
75   }
76 max_rate_err:
77   {
78     GST_ERROR_OBJECT (obj, "failed to query maximum sample rate: %s",
79         snd_strerror (err));
80     gst_caps_unref (in_caps);
81     return NULL;
82   }
83 }
84
85 static const struct
86 {
87   const int width;
88   const int depth;
89   const int sformat;
90   const int uformat;
91 } pcmformats[] = {
92   {
93   8, 8, SND_PCM_FORMAT_S8, SND_PCM_FORMAT_U8}, {
94   16, 16, SND_PCM_FORMAT_S16, SND_PCM_FORMAT_U16}, {
95   32, 24, SND_PCM_FORMAT_S24, SND_PCM_FORMAT_U24}, {
96 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)   /* no endian-unspecific enum available */
97   24, 24, SND_PCM_FORMAT_S24_3LE, SND_PCM_FORMAT_U24_3LE}, {
98 #else
99   24, 24, SND_PCM_FORMAT_S24_3BE, SND_PCM_FORMAT_U24_3BE}, {
100 #endif
101   32, 32, SND_PCM_FORMAT_S32, SND_PCM_FORMAT_U32}
102 };
103
104 static GstCaps *
105 gst_alsa_detect_formats (GstObject * obj, snd_pcm_hw_params_t * hw_params,
106     GstCaps * in_caps)
107 {
108   snd_pcm_format_mask_t *mask;
109   GstStructure *s;
110   GstCaps *caps;
111   gint i;
112
113   snd_pcm_format_mask_malloc (&mask);
114   snd_pcm_hw_params_get_format_mask (hw_params, mask);
115
116   caps = gst_caps_new_empty ();
117
118   for (i = 0; i < gst_caps_get_size (in_caps); ++i) {
119     GstStructure *scopy;
120     gint w, width = 0, depth = 0;
121
122     s = gst_caps_get_structure (in_caps, i);
123     if (!gst_structure_has_name (s, "audio/x-raw-int")) {
124       GST_WARNING_OBJECT (obj, "checking for LPCM format");
125       if(!gst_structure_has_name (s, "audio/x-lpcm")) {
126         GST_WARNING_OBJECT (obj, "skipping non-int format");
127         continue;
128       }
129     }
130     if (!gst_structure_get_int (s, "width", &width) ||
131         !gst_structure_get_int (s, "depth", &depth))
132       continue;
133     if (width == 0 || (width % 8) != 0)
134       continue;                 /* Only full byte widths are valid */
135     for (w = 0; w < G_N_ELEMENTS (pcmformats); w++)
136       if (pcmformats[w].width == width && pcmformats[w].depth == depth)
137         break;
138     if (w == G_N_ELEMENTS (pcmformats))
139       continue;                 /* Unknown format */
140
141     if (snd_pcm_format_mask_test (mask, pcmformats[w].sformat) &&
142         snd_pcm_format_mask_test (mask, pcmformats[w].uformat)) {
143       /* template contains { true, false } or just one, leave it as it is */
144       scopy = gst_structure_copy (s);
145     } else if (snd_pcm_format_mask_test (mask, pcmformats[w].sformat)) {
146       scopy = gst_structure_copy (s);
147       gst_structure_set (scopy, "signed", G_TYPE_BOOLEAN, TRUE, NULL);
148     } else if (snd_pcm_format_mask_test (mask, pcmformats[w].uformat)) {
149       scopy = gst_structure_copy (s);
150       gst_structure_set (scopy, "signed", G_TYPE_BOOLEAN, FALSE, NULL);
151     } else {
152       scopy = NULL;
153     }
154     if (scopy) {
155       if (width > 8) {
156         /* TODO: proper endianness detection, for now it's CPU endianness only */
157         gst_structure_set (scopy, "endianness", G_TYPE_INT, G_BYTE_ORDER, NULL);
158       }
159       gst_caps_append_structure (caps, scopy);
160     }
161   }
162
163   snd_pcm_format_mask_free (mask);
164   gst_caps_unref (in_caps);
165   return caps;
166 }
167
168 /* we don't have channel mappings for more than this many channels */
169 #define GST_ALSA_MAX_CHANNELS 8
170
171 static GstStructure *
172 get_channel_free_structure (const GstStructure * in_structure)
173 {
174   GstStructure *s = gst_structure_copy (in_structure);
175
176   gst_structure_remove_field (s, "channels");
177   return s;
178 }
179
180 static void
181 caps_add_channel_configuration (GstCaps * caps,
182     const GstStructure * in_structure, gint min_chans, gint max_chans)
183 {
184   GstAudioChannelPosition pos[8] = {
185     GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
186     GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
187     GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
188     GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
189     GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
190     GST_AUDIO_CHANNEL_POSITION_LFE,
191     GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT,
192     GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT
193   };
194   GstStructure *s = NULL;
195   gint c;
196
197   if (min_chans == max_chans && max_chans <= 2) {
198     s = get_channel_free_structure (in_structure);
199     gst_structure_set (s, "channels", G_TYPE_INT, max_chans, NULL);
200     gst_caps_append_structure (caps, s);
201     return;
202   }
203
204   g_assert (min_chans >= 1);
205
206   /* mono and stereo don't need channel configurations */
207   if (min_chans == 2) {
208     s = get_channel_free_structure (in_structure);
209     gst_structure_set (s, "channels", G_TYPE_INT, 2, NULL);
210     gst_caps_append_structure (caps, s);
211   } else if (min_chans == 1 && max_chans >= 2) {
212     s = get_channel_free_structure (in_structure);
213     gst_structure_set (s, "channels", GST_TYPE_INT_RANGE, 1, 2, NULL);
214     gst_caps_append_structure (caps, s);
215   }
216
217   /* don't know whether to use 2.1 or 3.0 here - but I suspect
218    * alsa might work around that/fix it somehow. Can we tell alsa
219    * what our channel layout is like? */
220   if (max_chans >= 3 && min_chans <= 3) {
221     GstAudioChannelPosition pos_21[3] = {
222       GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
223       GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
224       GST_AUDIO_CHANNEL_POSITION_LFE
225     };
226
227     s = get_channel_free_structure (in_structure);
228     gst_structure_set (s, "channels", G_TYPE_INT, 3, NULL);
229     gst_audio_set_channel_positions (s, pos_21);
230     gst_caps_append_structure (caps, s);
231   }
232
233   /* everything else (4, 6, 8 channels) needs a channel layout */
234   for (c = MAX (4, min_chans); c <= 8; c += 2) {
235     if (max_chans >= c) {
236       s = get_channel_free_structure (in_structure);
237       gst_structure_set (s, "channels", G_TYPE_INT, c, NULL);
238       gst_audio_set_channel_positions (s, pos);
239       gst_caps_append_structure (caps, s);
240     }
241   }
242
243   for (c = MAX (9, min_chans); c <= max_chans; ++c) {
244     GstAudioChannelPosition *ch_layout;
245     guint i;
246
247     ch_layout = g_new (GstAudioChannelPosition, c);
248     for (i = 0; i < c; ++i) {
249       ch_layout[i] = GST_AUDIO_CHANNEL_POSITION_NONE;
250     }
251     s = get_channel_free_structure (in_structure);
252     gst_structure_set (s, "channels", G_TYPE_INT, c, NULL);
253     gst_audio_set_channel_positions (s, ch_layout);
254     gst_caps_append_structure (caps, s);
255     g_free (ch_layout);
256   }
257 }
258
259 static GstCaps *
260 gst_alsa_detect_channels (GstObject * obj, snd_pcm_hw_params_t * hw_params,
261     GstCaps * in_caps)
262 {
263   GstCaps *caps;
264   guint min, max;
265   gint min_chans, max_chans;
266   gint err, i;
267
268   GST_LOG_OBJECT (obj, "probing channels ...");
269
270   if ((err = snd_pcm_hw_params_get_channels_min (hw_params, &min)) < 0)
271     goto min_chan_error;
272
273   if ((err = snd_pcm_hw_params_get_channels_max (hw_params, &max)) < 0)
274     goto max_chan_error;
275
276   /* note: the above functions may return (guint) -1 */
277   min_chans = min;
278   max_chans = max;
279
280   if (min_chans < 0) {
281     min_chans = 1;
282     max_chans = GST_ALSA_MAX_CHANNELS;
283   } else if (max_chans < 0) {
284     max_chans = GST_ALSA_MAX_CHANNELS;
285   }
286
287   if (min_chans > max_chans) {
288     gint temp;
289
290     GST_WARNING_OBJECT (obj, "minimum channels > maximum channels (%d > %d), "
291         "please fix your soundcard drivers", min, max);
292     temp = min_chans;
293     min_chans = max_chans;
294     max_chans = temp;
295   }
296
297   /* pro cards seem to return large numbers for min_channels */
298   if (min_chans > GST_ALSA_MAX_CHANNELS) {
299     GST_DEBUG_OBJECT (obj, "min_chans = %u, looks like a pro card", min_chans);
300     if (max_chans < min_chans) {
301       max_chans = min_chans;
302     } else {
303       /* only support [max_chans; max_chans] for these cards for now
304        * to avoid inflating the source caps with loads of structures ... */
305       min_chans = max_chans;
306     }
307   } else {
308     min_chans = MAX (min_chans, 1);
309     max_chans = MIN (GST_ALSA_MAX_CHANNELS, max_chans);
310   }
311
312   GST_DEBUG_OBJECT (obj, "Min. channels = %d (%d)", min_chans, min);
313   GST_DEBUG_OBJECT (obj, "Max. channels = %d (%d)", max_chans, max);
314
315   caps = gst_caps_new_empty ();
316
317   for (i = 0; i < gst_caps_get_size (in_caps); ++i) {
318     GstStructure *s;
319     GType field_type;
320     gint c_min = min_chans;
321     gint c_max = max_chans;
322
323     s = gst_caps_get_structure (in_caps, i);
324     /* the template caps might limit the number of channels (like alsasrc),
325      * in which case we don't want to return a superset, so hack around this
326      * for the two common cases where the channels are either a fixed number
327      * or a min/max range). Example: alsasrc template has channels = [1,2] and 
328      * the detection will claim to support 8 channels for device 'plughw:0' */
329     field_type = gst_structure_get_field_type (s, "channels");
330     if (field_type == G_TYPE_INT) {
331       gst_structure_get_int (s, "channels", &c_min);
332       gst_structure_get_int (s, "channels", &c_max);
333     } else if (field_type == GST_TYPE_INT_RANGE) {
334       const GValue *val;
335
336       val = gst_structure_get_value (s, "channels");
337       c_min = CLAMP (gst_value_get_int_range_min (val), min_chans, max_chans);
338       c_max = CLAMP (gst_value_get_int_range_max (val), min_chans, max_chans);
339     } else {
340       c_min = min_chans;
341       c_max = max_chans;
342     }
343
344     caps_add_channel_configuration (caps, s, c_min, c_max);
345   }
346
347   gst_caps_unref (in_caps);
348
349   return caps;
350
351   /* ERRORS */
352 min_chan_error:
353   {
354     GST_ERROR_OBJECT (obj, "failed to query minimum channel count: %s",
355         snd_strerror (err));
356     return NULL;
357   }
358 max_chan_error:
359   {
360     GST_ERROR_OBJECT (obj, "failed to query maximum channel count: %s",
361         snd_strerror (err));
362     return NULL;
363   }
364 }
365
366 snd_pcm_t *
367 gst_alsa_open_iec958_pcm (GstObject * obj)
368 {
369   char *iec958_pcm_name = NULL;
370   snd_pcm_t *pcm = NULL;
371   int res;
372   char devstr[256];             /* Storage for local 'default' device string */
373
374   /*
375    * Try and open our default iec958 device. Fall back to searching on card x
376    * if this fails, which should only happen on older alsa setups
377    */
378
379   /* The string will be one of these:
380    * SPDIF_CON: Non-audio flag not set:
381    *    spdif:{AES0 0x0 AES1 0x82 AES2 0x0 AES3 0x2}
382    * SPDIF_CON: Non-audio flag set:
383    *    spdif:{AES0 0x2 AES1 0x82 AES2 0x0 AES3 0x2}
384    */
385   sprintf (devstr,
386       "iec958:{AES0 0x%02x AES1 0x%02x AES2 0x%02x AES3 0x%02x}",
387       IEC958_AES0_CON_EMPHASIS_NONE | IEC958_AES0_NONAUDIO,
388       IEC958_AES1_CON_ORIGINAL | IEC958_AES1_CON_PCM_CODER,
389       0, IEC958_AES3_CON_FS_48000);
390
391   GST_DEBUG_OBJECT (obj, "Generated device string \"%s\"", devstr);
392   iec958_pcm_name = devstr;
393
394   res = snd_pcm_open (&pcm, iec958_pcm_name, SND_PCM_STREAM_PLAYBACK, 0);
395   if (G_UNLIKELY (res < 0)) {
396     GST_DEBUG_OBJECT (obj, "failed opening IEC958 device: %s",
397         snd_strerror (res));
398     pcm = NULL;
399   }
400
401   return pcm;
402 }
403
404
405 /*
406  * gst_alsa_probe_supported_formats:
407  *
408  * Takes the template caps and returns the subset which is actually
409  * supported by this device.
410  *
411  */
412
413 GstCaps *
414 gst_alsa_probe_supported_formats (GstObject * obj, snd_pcm_t * handle,
415     const GstCaps * template_caps)
416 {
417   snd_pcm_hw_params_t *hw_params;
418   snd_pcm_stream_t stream_type;
419   GstCaps *caps;
420   gint err;
421
422   snd_pcm_hw_params_malloc (&hw_params);
423   if ((err = snd_pcm_hw_params_any (handle, hw_params)) < 0)
424     goto error;
425
426   stream_type = snd_pcm_stream (handle);
427
428   caps = gst_caps_copy (template_caps);
429
430   if (!(caps = gst_alsa_detect_formats (obj, hw_params, caps)))
431     goto subroutine_error;
432
433   if (!(caps = gst_alsa_detect_rates (obj, hw_params, caps)))
434     goto subroutine_error;
435
436   if (!(caps = gst_alsa_detect_channels (obj, hw_params, caps)))
437     goto subroutine_error;
438
439   /* Try opening IEC958 device to see if we can support that format (playback
440    * only for now but we could add SPDIF capture later) */
441   if (stream_type == SND_PCM_STREAM_PLAYBACK) {
442     snd_pcm_t *pcm = gst_alsa_open_iec958_pcm (obj);
443
444     if (G_LIKELY (pcm)) {
445       gst_caps_append (caps, gst_caps_new_simple ("audio/x-iec958", NULL));
446       snd_pcm_close (pcm);
447     }
448   }
449
450   snd_pcm_hw_params_free (hw_params);
451   return caps;
452
453   /* ERRORS */
454 error:
455   {
456     GST_ERROR_OBJECT (obj, "failed to query formats: %s", snd_strerror (err));
457     snd_pcm_hw_params_free (hw_params);
458     return NULL;
459   }
460 subroutine_error:
461   {
462     GST_ERROR_OBJECT (obj, "failed to query formats");
463     snd_pcm_hw_params_free (hw_params);
464     return NULL;
465   }
466 }
467
468 /* returns the card name when the device number is unknown or -1 */
469 static gchar *
470 gst_alsa_find_device_name_no_handle (GstObject * obj, const gchar * devcard,
471     gint device_num, snd_pcm_stream_t stream)
472 {
473   snd_ctl_card_info_t *info = NULL;
474   snd_ctl_t *ctl = NULL;
475   gchar *ret = NULL;
476   gint dev = -1;
477
478   GST_LOG_OBJECT (obj, "[%s] device=%d", devcard, device_num);
479
480   if (snd_ctl_open (&ctl, devcard, 0) < 0)
481     return NULL;
482
483   snd_ctl_card_info_malloc (&info);
484   if (snd_ctl_card_info (ctl, info) < 0)
485     goto done;
486
487   if (device_num != -1) {
488     while (snd_ctl_pcm_next_device (ctl, &dev) == 0 && dev >= 0) {
489       if (dev == device_num) {
490         snd_pcm_info_t *pcminfo;
491
492         snd_pcm_info_malloc (&pcminfo);
493         snd_pcm_info_set_device (pcminfo, dev);
494         snd_pcm_info_set_subdevice (pcminfo, 0);
495         snd_pcm_info_set_stream (pcminfo, stream);
496         if (snd_ctl_pcm_info (ctl, pcminfo) < 0) {
497           snd_pcm_info_free (pcminfo);
498           break;
499         }
500
501         ret = (gchar *) snd_pcm_info_get_name (pcminfo);
502         if (ret) {
503           ret = g_strdup (ret);
504           GST_LOG_OBJECT (obj, "name from pcminfo: %s", ret);
505         }
506         snd_pcm_info_free (pcminfo);
507         if (ret)
508           break;
509       }
510     }
511   }
512
513   if (ret == NULL) {
514     char *name = NULL;
515     gint card;
516
517     GST_LOG_OBJECT (obj, "trying card name");
518     card = snd_ctl_card_info_get_card (info);
519     snd_card_get_name (card, &name);
520     ret = g_strdup (name);
521     free (name);
522   }
523
524 done:
525   snd_ctl_card_info_free (info);
526   snd_ctl_close (ctl);
527
528   return ret;
529 }
530
531 gchar *
532 gst_alsa_find_card_name (GstObject * obj, const gchar * devcard,
533     snd_pcm_stream_t stream)
534 {
535   return gst_alsa_find_device_name_no_handle (obj, devcard, -1, stream);
536 }
537
538 gchar *
539 gst_alsa_find_device_name (GstObject * obj, const gchar * device,
540     snd_pcm_t * handle, snd_pcm_stream_t stream)
541 {
542   gchar *ret = NULL;
543
544   if (device != NULL) {
545     gchar *dev, *comma;
546     gint devnum;
547
548     GST_LOG_OBJECT (obj, "Trying to get device name from string '%s'", device);
549
550     /* only want name:card bit, but not devices and subdevices */
551     dev = g_strdup (device);
552     if ((comma = strchr (dev, ','))) {
553       *comma = '\0';
554       devnum = atoi (comma + 1);
555       ret = gst_alsa_find_device_name_no_handle (obj, dev, devnum, stream);
556     }
557     g_free (dev);
558   }
559
560   if (ret == NULL && handle != NULL) {
561     snd_pcm_info_t *info;
562
563     GST_LOG_OBJECT (obj, "Trying to get device name from open handle");
564     snd_pcm_info_malloc (&info);
565     snd_pcm_info (handle, info);
566     ret = g_strdup (snd_pcm_info_get_name (info));
567     snd_pcm_info_free (info);
568   }
569
570   GST_LOG_OBJECT (obj, "Device name for device '%s': %s",
571       GST_STR_NULL (device), GST_STR_NULL (ret));
572
573   return ret;
574 }