output/alsa: always receive host byte order samples

Don't use audio_format.reverse_endian.
This commit is contained in:
Max Kellermann 2012-03-21 19:09:22 +01:00
parent 7ebf8e66c9
commit 3dba09f339
2 changed files with 62 additions and 3 deletions

View File

@ -1201,6 +1201,7 @@ test_run_output_LDADD = $(MPD_LIBS) \
$(ENCODER_LIBS) \ $(ENCODER_LIBS) \
libmixer_plugins.a \ libmixer_plugins.a \
$(FILTER_LIBS) \ $(FILTER_LIBS) \
libutil.a \
$(GLIB_LIBS) $(GLIB_LIBS)
test_run_output_SOURCES = test/run_output.c \ test_run_output_SOURCES = test/run_output.c \
test/stdbin.h \ test/stdbin.h \

View File

@ -21,6 +21,8 @@
#include "alsa_output_plugin.h" #include "alsa_output_plugin.h"
#include "output_api.h" #include "output_api.h"
#include "mixer_list.h" #include "mixer_list.h"
#include "pcm_buffer.h"
#include "pcm_byteswap.h"
#include <glib.h> #include <glib.h>
#include <alsa/asoundlib.h> #include <alsa/asoundlib.h>
@ -45,6 +47,13 @@ typedef snd_pcm_sframes_t alsa_writei_t(snd_pcm_t * pcm, const void *buffer,
struct alsa_data { struct alsa_data {
struct audio_output base; struct audio_output base;
/**
* The buffer used to reverse the byte order.
*
* @see #reverse_endian
*/
struct pcm_buffer reverse_buffer;
/** the configured name of the ALSA device; NULL for the /** the configured name of the ALSA device; NULL for the
default device */ default device */
char *device; char *device;
@ -52,6 +61,21 @@ struct alsa_data {
/** use memory mapped I/O? */ /** use memory mapped I/O? */
bool use_mmap; bool use_mmap;
/**
* Does ALSA expect samples in reverse byte order? (i.e. not
* host byte order)
*
* This attribute is only valid while the device is open.
*/
bool reverse_endian;
/**
* Which sample format is being sent to the play() method?
*
* This attribute is only valid while the device is open.
*/
enum sample_format sample_format;
/** libasound's buffer_time setting (in microseconds) */ /** libasound's buffer_time setting (in microseconds) */
unsigned int buffer_time; unsigned int buffer_time;
@ -167,6 +191,23 @@ alsa_finish(struct audio_output *ao)
snd_config_update_free_global(); snd_config_update_free_global();
} }
static bool
alsa_output_enable(struct audio_output *ao, G_GNUC_UNUSED GError **error_r)
{
struct alsa_data *ad = (struct alsa_data *)ao;
pcm_buffer_init(&ad->reverse_buffer);
return true;
}
static void
alsa_output_disable(struct audio_output *ao)
{
struct alsa_data *ad = (struct alsa_data *)ao;
pcm_buffer_deinit(&ad->reverse_buffer);
}
static bool static bool
alsa_test_default_device(void) alsa_test_default_device(void)
{ {
@ -288,13 +329,18 @@ alsa_output_try_reverse(snd_pcm_t *pcm, snd_pcm_hw_params_t *hwparams,
static int static int
alsa_output_try_format_both(snd_pcm_t *pcm, snd_pcm_hw_params_t *hwparams, alsa_output_try_format_both(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)
{ {
*reverse_endian_r = false;
int err = alsa_output_try_format(pcm, hwparams, audio_format, int err = alsa_output_try_format(pcm, hwparams, audio_format,
sample_format); sample_format);
if (err == -EINVAL) if (err == -EINVAL) {
*reverse_endian_r = true;
err = alsa_output_try_reverse(pcm, hwparams, audio_format, err = alsa_output_try_reverse(pcm, hwparams, audio_format,
sample_format); sample_format);
}
return err; return err;
} }
@ -304,11 +350,13 @@ alsa_output_try_format_both(snd_pcm_t *pcm, snd_pcm_hw_params_t *hwparams,
*/ */
static int static int
alsa_output_setup_format(snd_pcm_t *pcm, snd_pcm_hw_params_t *hwparams, alsa_output_setup_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)
{ {
/* 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_both(pcm, hwparams, audio_format,
reverse_endian_r,
audio_format->format); audio_format->format);
if (err != -EINVAL) if (err != -EINVAL)
return err; return err;
@ -329,6 +377,7 @@ alsa_output_setup_format(snd_pcm_t *pcm, snd_pcm_hw_params_t *hwparams,
continue; continue;
err = alsa_output_try_format_both(pcm, hwparams, audio_format, err = alsa_output_try_format_both(pcm, hwparams, audio_format,
reverse_endian_r,
probe_formats[i]); probe_formats[i]);
if (err != -EINVAL) if (err != -EINVAL)
return err; return err;
@ -387,7 +436,8 @@ configure_hw:
ad->writei = snd_pcm_writei; ad->writei = snd_pcm_writei;
} }
err = alsa_output_setup_format(ad->pcm, hwparams, audio_format); err = alsa_output_setup_format(ad->pcm, hwparams, audio_format,
&ad->reverse_endian);
if (err < 0) { if (err < 0) {
g_set_error(error, alsa_output_quark(), err, g_set_error(error, alsa_output_quark(), err,
"ALSA device \"%s\" does not support format %s: %s", "ALSA device \"%s\" does not support format %s: %s",
@ -397,6 +447,8 @@ configure_hw:
return false; return false;
} }
ad->sample_format = audio_format->format;
err = snd_pcm_hw_params_set_channels_near(ad->pcm, hwparams, err = snd_pcm_hw_params_set_channels_near(ad->pcm, hwparams,
&channels); &channels);
if (err < 0) { if (err < 0) {
@ -660,6 +712,10 @@ alsa_play(struct audio_output *ao, const void *chunk, size_t size,
{ {
struct alsa_data *ad = (struct alsa_data *)ao; struct alsa_data *ad = (struct alsa_data *)ao;
if (ad->reverse_endian)
chunk = pcm_byteswap(&ad->reverse_buffer, ad->sample_format,
chunk, size);
size /= ad->frame_size; size /= ad->frame_size;
while (true) { while (true) {
@ -684,6 +740,8 @@ const struct audio_output_plugin alsa_output_plugin = {
.test_default_device = alsa_test_default_device, .test_default_device = alsa_test_default_device,
.init = alsa_init, .init = alsa_init,
.finish = alsa_finish, .finish = alsa_finish,
.enable = alsa_output_enable,
.disable = alsa_output_disable,
.open = alsa_open, .open = alsa_open,
.play = alsa_play, .play = alsa_play,
.drain = alsa_drain, .drain = alsa_drain,