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, "skipping non-int format");
127 if (!gst_structure_get_int (s, "width", &width) ||
128 !gst_structure_get_int (s, "depth", &depth))
130 if (width == 0 || (width % 8) != 0)
131 continue; /* Only full byte widths are valid */
132 for (w = 0; w < G_N_ELEMENTS (pcmformats); w++)
133 if (pcmformats[w].width == width && pcmformats[w].depth == depth)
135 if (w == G_N_ELEMENTS (pcmformats))
136 continue; /* Unknown format */
138 if (snd_pcm_format_mask_test (mask, pcmformats[w].sformat) &&
139 snd_pcm_format_mask_test (mask, pcmformats[w].uformat)) {
140 /* template contains { true, false } or just one, leave it as it is */
141 scopy = gst_structure_copy (s);
142 } else if (snd_pcm_format_mask_test (mask, pcmformats[w].sformat)) {
143 scopy = gst_structure_copy (s);
144 gst_structure_set (scopy, "signed", G_TYPE_BOOLEAN, TRUE, NULL);
145 } else if (snd_pcm_format_mask_test (mask, pcmformats[w].uformat)) {
146 scopy = gst_structure_copy (s);
147 gst_structure_set (scopy, "signed", G_TYPE_BOOLEAN, FALSE, NULL);
153 /* TODO: proper endianness detection, for now it's CPU endianness only */
154 gst_structure_set (scopy, "endianness", G_TYPE_INT, G_BYTE_ORDER, NULL);
156 gst_caps_append_structure (caps, scopy);
160 snd_pcm_format_mask_free (mask);
161 gst_caps_unref (in_caps);
165 /* we don't have channel mappings for more than this many channels */
166 #define GST_ALSA_MAX_CHANNELS 8
168 static GstStructure *
169 get_channel_free_structure (const GstStructure * in_structure)
171 GstStructure *s = gst_structure_copy (in_structure);
173 gst_structure_remove_field (s, "channels");
178 caps_add_channel_configuration (GstCaps * caps,
179 const GstStructure * in_structure, gint min_chans, gint max_chans)
181 GstAudioChannelPosition pos[8] = {
182 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
183 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
184 GST_AUDIO_CHANNEL_POSITION_REAR_LEFT,
185 GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT,
186 GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER,
187 GST_AUDIO_CHANNEL_POSITION_LFE,
188 GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT,
189 GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT
191 GstStructure *s = NULL;
194 if (min_chans == max_chans && max_chans <= 2) {
195 s = get_channel_free_structure (in_structure);
196 gst_structure_set (s, "channels", G_TYPE_INT, max_chans, NULL);
197 gst_caps_append_structure (caps, s);
201 g_assert (min_chans >= 1);
203 /* mono and stereo don't need channel configurations */
204 if (min_chans == 2) {
205 s = get_channel_free_structure (in_structure);
206 gst_structure_set (s, "channels", G_TYPE_INT, 2, NULL);
207 gst_caps_append_structure (caps, s);
208 } else if (min_chans == 1 && max_chans >= 2) {
209 s = get_channel_free_structure (in_structure);
210 gst_structure_set (s, "channels", GST_TYPE_INT_RANGE, 1, 2, NULL);
211 gst_caps_append_structure (caps, s);
214 /* don't know whether to use 2.1 or 3.0 here - but I suspect
215 * alsa might work around that/fix it somehow. Can we tell alsa
216 * what our channel layout is like? */
217 if (max_chans >= 3 && min_chans <= 3) {
218 GstAudioChannelPosition pos_21[3] = {
219 GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT,
220 GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT,
221 GST_AUDIO_CHANNEL_POSITION_LFE
224 s = get_channel_free_structure (in_structure);
225 gst_structure_set (s, "channels", G_TYPE_INT, 3, NULL);
226 gst_audio_set_channel_positions (s, pos_21);
227 gst_caps_append_structure (caps, s);
230 /* everything else (4, 6, 8 channels) needs a channel layout */
231 for (c = MAX (4, min_chans); c <= 8; c += 2) {
232 if (max_chans >= c) {
233 s = get_channel_free_structure (in_structure);
234 gst_structure_set (s, "channels", G_TYPE_INT, c, NULL);
235 gst_audio_set_channel_positions (s, pos);
236 gst_caps_append_structure (caps, s);
240 for (c = MAX (9, min_chans); c <= max_chans; ++c) {
241 GstAudioChannelPosition *ch_layout;
244 ch_layout = g_new (GstAudioChannelPosition, c);
245 for (i = 0; i < c; ++i) {
246 ch_layout[i] = GST_AUDIO_CHANNEL_POSITION_NONE;
248 s = get_channel_free_structure (in_structure);
249 gst_structure_set (s, "channels", G_TYPE_INT, c, NULL);
250 gst_audio_set_channel_positions (s, ch_layout);
251 gst_caps_append_structure (caps, s);
257 gst_alsa_detect_channels (GstObject * obj, snd_pcm_hw_params_t * hw_params,
262 gint min_chans, max_chans;
265 GST_LOG_OBJECT (obj, "probing channels ...");
267 if ((err = snd_pcm_hw_params_get_channels_min (hw_params, &min)) < 0)
270 if ((err = snd_pcm_hw_params_get_channels_max (hw_params, &max)) < 0)
273 /* note: the above functions may return (guint) -1 */
279 max_chans = GST_ALSA_MAX_CHANNELS;
280 } else if (max_chans < 0) {
281 max_chans = GST_ALSA_MAX_CHANNELS;
284 if (min_chans > max_chans) {
287 GST_WARNING_OBJECT (obj, "minimum channels > maximum channels (%d > %d), "
288 "please fix your soundcard drivers", min, max);
290 min_chans = max_chans;
294 /* pro cards seem to return large numbers for min_channels */
295 if (min_chans > GST_ALSA_MAX_CHANNELS) {
296 GST_DEBUG_OBJECT (obj, "min_chans = %u, looks like a pro card", min_chans);
297 if (max_chans < min_chans) {
298 max_chans = min_chans;
300 /* only support [max_chans; max_chans] for these cards for now
301 * to avoid inflating the source caps with loads of structures ... */
302 min_chans = max_chans;
305 min_chans = MAX (min_chans, 1);
306 max_chans = MIN (GST_ALSA_MAX_CHANNELS, max_chans);
309 GST_DEBUG_OBJECT (obj, "Min. channels = %d (%d)", min_chans, min);
310 GST_DEBUG_OBJECT (obj, "Max. channels = %d (%d)", max_chans, max);
312 caps = gst_caps_new_empty ();
314 for (i = 0; i < gst_caps_get_size (in_caps); ++i) {
317 gint c_min = min_chans;
318 gint c_max = max_chans;
320 s = gst_caps_get_structure (in_caps, i);
321 /* the template caps might limit the number of channels (like alsasrc),
322 * in which case we don't want to return a superset, so hack around this
323 * for the two common cases where the channels are either a fixed number
324 * or a min/max range). Example: alsasrc template has channels = [1,2] and
325 * the detection will claim to support 8 channels for device 'plughw:0' */
326 field_type = gst_structure_get_field_type (s, "channels");
327 if (field_type == G_TYPE_INT) {
328 gst_structure_get_int (s, "channels", &c_min);
329 gst_structure_get_int (s, "channels", &c_max);
330 } else if (field_type == GST_TYPE_INT_RANGE) {
333 val = gst_structure_get_value (s, "channels");
334 c_min = CLAMP (gst_value_get_int_range_min (val), min_chans, max_chans);
335 c_max = CLAMP (gst_value_get_int_range_max (val), min_chans, max_chans);
341 caps_add_channel_configuration (caps, s, c_min, c_max);
344 gst_caps_unref (in_caps);
351 GST_ERROR_OBJECT (obj, "failed to query minimum channel count: %s",
357 GST_ERROR_OBJECT (obj, "failed to query maximum channel count: %s",
364 gst_alsa_open_iec958_pcm (GstObject * obj)
366 char *iec958_pcm_name = NULL;
367 snd_pcm_t *pcm = NULL;
369 char devstr[256]; /* Storage for local 'default' device string */
372 * Try and open our default iec958 device. Fall back to searching on card x
373 * if this fails, which should only happen on older alsa setups
376 /* The string will be one of these:
377 * SPDIF_CON: Non-audio flag not set:
378 * spdif:{AES0 0x0 AES1 0x82 AES2 0x0 AES3 0x2}
379 * SPDIF_CON: Non-audio flag set:
380 * spdif:{AES0 0x2 AES1 0x82 AES2 0x0 AES3 0x2}
383 "iec958:{AES0 0x%02x AES1 0x%02x AES2 0x%02x AES3 0x%02x}",
384 IEC958_AES0_CON_EMPHASIS_NONE | IEC958_AES0_NONAUDIO,
385 IEC958_AES1_CON_ORIGINAL | IEC958_AES1_CON_PCM_CODER,
386 0, IEC958_AES3_CON_FS_48000);
388 GST_DEBUG_OBJECT (obj, "Generated device string \"%s\"", devstr);
389 iec958_pcm_name = devstr;
391 res = snd_pcm_open (&pcm, iec958_pcm_name, SND_PCM_STREAM_PLAYBACK, 0);
392 if (G_UNLIKELY (res < 0)) {
393 GST_DEBUG_OBJECT (obj, "failed opening IEC958 device: %s",
403 * gst_alsa_probe_supported_formats:
405 * Takes the template caps and returns the subset which is actually
406 * supported by this device.
411 gst_alsa_probe_supported_formats (GstObject * obj, snd_pcm_t * handle,
412 const GstCaps * template_caps)
414 snd_pcm_hw_params_t *hw_params;
415 snd_pcm_stream_t stream_type;
419 snd_pcm_hw_params_malloc (&hw_params);
420 if ((err = snd_pcm_hw_params_any (handle, hw_params)) < 0)
423 stream_type = snd_pcm_stream (handle);
425 caps = gst_caps_copy (template_caps);
427 if (!(caps = gst_alsa_detect_formats (obj, hw_params, caps)))
428 goto subroutine_error;
430 if (!(caps = gst_alsa_detect_rates (obj, hw_params, caps)))
431 goto subroutine_error;
433 if (!(caps = gst_alsa_detect_channels (obj, hw_params, caps)))
434 goto subroutine_error;
436 /* Try opening IEC958 device to see if we can support that format (playback
437 * only for now but we could add SPDIF capture later) */
438 if (stream_type == SND_PCM_STREAM_PLAYBACK) {
439 snd_pcm_t *pcm = gst_alsa_open_iec958_pcm (obj);
441 if (G_LIKELY (pcm)) {
442 gst_caps_append (caps, gst_caps_new_simple ("audio/x-iec958", NULL));
447 snd_pcm_hw_params_free (hw_params);
453 GST_ERROR_OBJECT (obj, "failed to query formats: %s", snd_strerror (err));
454 snd_pcm_hw_params_free (hw_params);
459 GST_ERROR_OBJECT (obj, "failed to query formats");
460 snd_pcm_hw_params_free (hw_params);
465 /* returns the card name when the device number is unknown or -1 */
467 gst_alsa_find_device_name_no_handle (GstObject * obj, const gchar * devcard,
468 gint device_num, snd_pcm_stream_t stream)
470 snd_ctl_card_info_t *info = NULL;
471 snd_ctl_t *ctl = NULL;
475 GST_LOG_OBJECT (obj, "[%s] device=%d", devcard, device_num);
477 if (snd_ctl_open (&ctl, devcard, 0) < 0)
480 snd_ctl_card_info_malloc (&info);
481 if (snd_ctl_card_info (ctl, info) < 0)
484 if (device_num != -1) {
485 while (snd_ctl_pcm_next_device (ctl, &dev) == 0 && dev >= 0) {
486 if (dev == device_num) {
487 snd_pcm_info_t *pcminfo;
489 snd_pcm_info_malloc (&pcminfo);
490 snd_pcm_info_set_device (pcminfo, dev);
491 snd_pcm_info_set_subdevice (pcminfo, 0);
492 snd_pcm_info_set_stream (pcminfo, stream);
493 if (snd_ctl_pcm_info (ctl, pcminfo) < 0) {
494 snd_pcm_info_free (pcminfo);
498 ret = (gchar *) snd_pcm_info_get_name (pcminfo);
500 ret = g_strdup (ret);
501 GST_LOG_OBJECT (obj, "name from pcminfo: %s", ret);
503 snd_pcm_info_free (pcminfo);
514 GST_LOG_OBJECT (obj, "trying card name");
515 card = snd_ctl_card_info_get_card (info);
516 snd_card_get_name (card, &name);
517 ret = g_strdup (name);
522 snd_ctl_card_info_free (info);
529 gst_alsa_find_card_name (GstObject * obj, const gchar * devcard,
530 snd_pcm_stream_t stream)
532 return gst_alsa_find_device_name_no_handle (obj, devcard, -1, stream);
536 gst_alsa_find_device_name (GstObject * obj, const gchar * device,
537 snd_pcm_t * handle, snd_pcm_stream_t stream)
541 if (device != NULL) {
545 GST_LOG_OBJECT (obj, "Trying to get device name from string '%s'", device);
547 /* only want name:card bit, but not devices and subdevices */
548 dev = g_strdup (device);
549 if ((comma = strchr (dev, ','))) {
551 devnum = atoi (comma + 1);
552 ret = gst_alsa_find_device_name_no_handle (obj, dev, devnum, stream);
557 if (ret == NULL && handle != NULL) {
558 snd_pcm_info_t *info;
560 GST_LOG_OBJECT (obj, "Trying to get device name from open handle");
561 snd_pcm_info_malloc (&info);
562 snd_pcm_info (handle, info);
563 ret = g_strdup (snd_pcm_info_get_name (info));
564 snd_pcm_info_free (info);
567 GST_LOG_OBJECT (obj, "Device name for device '%s': %s",
568 GST_STR_NULL (device), GST_STR_NULL (ret));