1 /* Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
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.
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.
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.
20 #include <gst/audio/multichannel.h>
23 gst_alsa_detect_rates (GstObject * obj, snd_pcm_hw_params_t * hw_params,
28 gint err, dir, min_rate, max_rate, i;
30 GST_LOG_OBJECT (obj, "probing sample rates ...");
32 if ((err = snd_pcm_hw_params_get_rate_min (hw_params, &min, &dir)) < 0)
35 if ((err = snd_pcm_hw_params_get_rate_max (hw_params, &max, &dir)) < 0)
42 min_rate = 4000; /* random 'sensible minimum' */
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);
49 GST_DEBUG_OBJECT (obj, "Min. rate = %u (%d)", min_rate, min);
50 GST_DEBUG_OBJECT (obj, "Max. rate = %u (%d)", max_rate, max);
52 caps = gst_caps_make_writable (in_caps);
54 for (i = 0; i < gst_caps_get_size (caps); ++i) {
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);
61 gst_structure_set (s, "rate", GST_TYPE_INT_RANGE,
62 min_rate, max_rate, NULL);
71 GST_ERROR_OBJECT (obj, "failed to query minimum sample rate: %s",
73 gst_caps_unref (in_caps);
78 GST_ERROR_OBJECT (obj, "failed to query maximum sample rate: %s",
80 gst_caps_unref (in_caps);
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}, {
99 24, 24, SND_PCM_FORMAT_S24_3BE, SND_PCM_FORMAT_U24_3BE}, {
101 32, 32, SND_PCM_FORMAT_S32, SND_PCM_FORMAT_U32}
105 gst_alsa_detect_formats (GstObject * obj, snd_pcm_hw_params_t * hw_params,
108 snd_pcm_format_mask_t *mask;
113 snd_pcm_format_mask_malloc (&mask);
114 snd_pcm_hw_params_get_format_mask (hw_params, mask);
116 caps = gst_caps_new_empty ();
118 for (i = 0; i < gst_caps_get_size (in_caps); ++i) {
120 gint w, width = 0, depth = 0;
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");
130 if (!gst_structure_get_int (s, "width", &width) ||
131 !gst_structure_get_int (s, "depth", &depth))
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)
138 if (w == G_N_ELEMENTS (pcmformats))
139 continue; /* Unknown format */
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);
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);
159 gst_caps_append_structure (caps, scopy);
163 snd_pcm_format_mask_free (mask);
164 gst_caps_unref (in_caps);
168 /* we don't have channel mappings for more than this many channels */
169 #define GST_ALSA_MAX_CHANNELS 8
171 static GstStructure *
172 get_channel_free_structure (const GstStructure * in_structure)
174 GstStructure *s = gst_structure_copy (in_structure);
176 gst_structure_remove_field (s, "channels");
181 caps_add_channel_configuration (GstCaps * caps,
182 const GstStructure * in_structure, gint min_chans, gint max_chans)
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
194 GstStructure *s = NULL;
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);
204 g_assert (min_chans >= 1);
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);
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
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);
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);
243 for (c = MAX (9, min_chans); c <= max_chans; ++c) {
244 GstAudioChannelPosition *ch_layout;
247 ch_layout = g_new (GstAudioChannelPosition, c);
248 for (i = 0; i < c; ++i) {
249 ch_layout[i] = GST_AUDIO_CHANNEL_POSITION_NONE;
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);
260 gst_alsa_detect_channels (GstObject * obj, snd_pcm_hw_params_t * hw_params,
265 gint min_chans, max_chans;
268 GST_LOG_OBJECT (obj, "probing channels ...");
270 if ((err = snd_pcm_hw_params_get_channels_min (hw_params, &min)) < 0)
273 if ((err = snd_pcm_hw_params_get_channels_max (hw_params, &max)) < 0)
276 /* note: the above functions may return (guint) -1 */
282 max_chans = GST_ALSA_MAX_CHANNELS;
283 } else if (max_chans < 0) {
284 max_chans = GST_ALSA_MAX_CHANNELS;
287 if (min_chans > max_chans) {
290 GST_WARNING_OBJECT (obj, "minimum channels > maximum channels (%d > %d), "
291 "please fix your soundcard drivers", min, max);
293 min_chans = max_chans;
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;
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;
308 min_chans = MAX (min_chans, 1);
309 max_chans = MIN (GST_ALSA_MAX_CHANNELS, max_chans);
312 GST_DEBUG_OBJECT (obj, "Min. channels = %d (%d)", min_chans, min);
313 GST_DEBUG_OBJECT (obj, "Max. channels = %d (%d)", max_chans, max);
315 caps = gst_caps_new_empty ();
317 for (i = 0; i < gst_caps_get_size (in_caps); ++i) {
320 gint c_min = min_chans;
321 gint c_max = max_chans;
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) {
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);
344 caps_add_channel_configuration (caps, s, c_min, c_max);
347 gst_caps_unref (in_caps);
354 GST_ERROR_OBJECT (obj, "failed to query minimum channel count: %s",
360 GST_ERROR_OBJECT (obj, "failed to query maximum channel count: %s",
367 gst_alsa_open_iec958_pcm (GstObject * obj)
369 char *iec958_pcm_name = NULL;
370 snd_pcm_t *pcm = NULL;
372 char devstr[256]; /* Storage for local 'default' device string */
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
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}
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);
391 GST_DEBUG_OBJECT (obj, "Generated device string \"%s\"", devstr);
392 iec958_pcm_name = devstr;
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",
406 * gst_alsa_probe_supported_formats:
408 * Takes the template caps and returns the subset which is actually
409 * supported by this device.
414 gst_alsa_probe_supported_formats (GstObject * obj, snd_pcm_t * handle,
415 const GstCaps * template_caps)
417 snd_pcm_hw_params_t *hw_params;
418 snd_pcm_stream_t stream_type;
422 snd_pcm_hw_params_malloc (&hw_params);
423 if ((err = snd_pcm_hw_params_any (handle, hw_params)) < 0)
426 stream_type = snd_pcm_stream (handle);
428 caps = gst_caps_copy (template_caps);
430 if (!(caps = gst_alsa_detect_formats (obj, hw_params, caps)))
431 goto subroutine_error;
433 if (!(caps = gst_alsa_detect_rates (obj, hw_params, caps)))
434 goto subroutine_error;
436 if (!(caps = gst_alsa_detect_channels (obj, hw_params, caps)))
437 goto subroutine_error;
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);
444 if (G_LIKELY (pcm)) {
445 gst_caps_append (caps, gst_caps_new_simple ("audio/x-iec958", NULL));
450 snd_pcm_hw_params_free (hw_params);
456 GST_ERROR_OBJECT (obj, "failed to query formats: %s", snd_strerror (err));
457 snd_pcm_hw_params_free (hw_params);
462 GST_ERROR_OBJECT (obj, "failed to query formats");
463 snd_pcm_hw_params_free (hw_params);
468 /* returns the card name when the device number is unknown or -1 */
470 gst_alsa_find_device_name_no_handle (GstObject * obj, const gchar * devcard,
471 gint device_num, snd_pcm_stream_t stream)
473 snd_ctl_card_info_t *info = NULL;
474 snd_ctl_t *ctl = NULL;
478 GST_LOG_OBJECT (obj, "[%s] device=%d", devcard, device_num);
480 if (snd_ctl_open (&ctl, devcard, 0) < 0)
483 snd_ctl_card_info_malloc (&info);
484 if (snd_ctl_card_info (ctl, info) < 0)
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;
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);
501 ret = (gchar *) snd_pcm_info_get_name (pcminfo);
503 ret = g_strdup (ret);
504 GST_LOG_OBJECT (obj, "name from pcminfo: %s", ret);
506 snd_pcm_info_free (pcminfo);
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);
525 snd_ctl_card_info_free (info);
532 gst_alsa_find_card_name (GstObject * obj, const gchar * devcard,
533 snd_pcm_stream_t stream)
535 return gst_alsa_find_device_name_no_handle (obj, devcard, -1, stream);
539 gst_alsa_find_device_name (GstObject * obj, const gchar * device,
540 snd_pcm_t * handle, snd_pcm_stream_t stream)
544 if (device != NULL) {
548 GST_LOG_OBJECT (obj, "Trying to get device name from string '%s'", device);
550 /* only want name:card bit, but not devices and subdevices */
551 dev = g_strdup (device);
552 if ((comma = strchr (dev, ','))) {
554 devnum = atoi (comma + 1);
555 ret = gst_alsa_find_device_name_no_handle (obj, dev, devnum, stream);
560 if (ret == NULL && handle != NULL) {
561 snd_pcm_info_t *info;
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);
570 GST_LOG_OBJECT (obj, "Device name for device '%s': %s",
571 GST_STR_NULL (device), GST_STR_NULL (ret));