output/alsa: simplify alsa_output_try_format_both()

Merge three functions into one and call get_bitformat() only once.
This commit is contained in:
Max Kellermann 2012-03-22 00:23:07 +01:00
parent 11e83eb7e7
commit 2874d68bdb

View File

@ -260,11 +260,13 @@ byteswap_bitformat(snd_pcm_format_t fmt)
} }
/** /**
* Attempts to configure the specified sample format. * Attempts to configure the specified sample format, and tries the
* reversed host byte order if was not supported.
*/ */
static int static int
alsa_output_try_format(snd_pcm_t *pcm, snd_pcm_hw_params_t *hwparams, alsa_output_try_format(snd_pcm_t *pcm, snd_pcm_hw_params_t *hwparams,
struct audio_format *audio_format, struct audio_format *audio_format,
bool *reverse_endian_r,
enum sample_format sample_format) enum sample_format sample_format)
{ {
snd_pcm_format_t alsa_format = get_bitformat(sample_format); snd_pcm_format_t alsa_format = get_bitformat(sample_format);
@ -272,51 +274,22 @@ alsa_output_try_format(snd_pcm_t *pcm, snd_pcm_hw_params_t *hwparams,
return -EINVAL; return -EINVAL;
int err = snd_pcm_hw_params_set_format(pcm, hwparams, alsa_format); int err = snd_pcm_hw_params_set_format(pcm, hwparams, alsa_format);
if (err == 0) if (err == 0) {
*reverse_endian_r = false;
audio_format->format = sample_format; audio_format->format = sample_format;
return err;
} }
/** if (err != -EINVAL)
* Attempts to configure the specified sample format with reversed return err;
* host byte order.
*/ alsa_format = byteswap_bitformat(alsa_format);
static int
alsa_output_try_reverse(snd_pcm_t *pcm, snd_pcm_hw_params_t *hwparams,
struct audio_format *audio_format,
enum sample_format sample_format)
{
snd_pcm_format_t alsa_format =
byteswap_bitformat(get_bitformat(sample_format));
if (alsa_format == SND_PCM_FORMAT_UNKNOWN) if (alsa_format == SND_PCM_FORMAT_UNKNOWN)
return -EINVAL; return -EINVAL;
int err = snd_pcm_hw_params_set_format(pcm, hwparams, alsa_format); err = snd_pcm_hw_params_set_format(pcm, hwparams, alsa_format);
if (err == 0) if (err == 0) {
audio_format->format = sample_format;
return err;
}
/**
* Attempts to configure the specified sample format, and tries the
* reversed host byte order if was not supported.
*/
static int
alsa_output_try_format_both(snd_pcm_t *pcm, snd_pcm_hw_params_t *hwparams,
struct audio_format *audio_format,
bool *reverse_endian_r,
enum sample_format sample_format)
{
*reverse_endian_r = false;
int err = alsa_output_try_format(pcm, hwparams, audio_format,
sample_format);
if (err == -EINVAL) {
*reverse_endian_r = true; *reverse_endian_r = true;
err = alsa_output_try_reverse(pcm, hwparams, audio_format, audio_format->format = sample_format;
sample_format);
} }
return err; return err;
@ -332,7 +305,7 @@ alsa_output_setup_format(snd_pcm_t *pcm, snd_pcm_hw_params_t *hwparams,
{ {
/* try the input format first */ /* try the input format first */
int err = alsa_output_try_format_both(pcm, hwparams, audio_format, int err = alsa_output_try_format(pcm, hwparams, audio_format,
reverse_endian_r, reverse_endian_r,
audio_format->format); audio_format->format);
if (err != -EINVAL) if (err != -EINVAL)
@ -353,7 +326,7 @@ alsa_output_setup_format(snd_pcm_t *pcm, snd_pcm_hw_params_t *hwparams,
if (probe_formats[i] == audio_format->format) if (probe_formats[i] == audio_format->format)
continue; continue;
err = alsa_output_try_format_both(pcm, hwparams, audio_format, err = alsa_output_try_format(pcm, hwparams, audio_format,
reverse_endian_r, reverse_endian_r,
probe_formats[i]); probe_formats[i]);
if (err != -EINVAL) if (err != -EINVAL)