From aa33422de680989c5b7c4a91684886fd42e382cc Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sat, 1 Nov 2008 14:04:14 +0100 Subject: [PATCH] alsa, jack: no const pointers for allocated strings Make the pointers "device" and "name" non-const, so we don't need the xfree() hack. The default value is expressed as NULL. --- src/output/alsa_plugin.c | 42 ++++++++++++++++++++++++---------------- src/output/jack_plugin.c | 26 ++++++++++++++++--------- 2 files changed, 42 insertions(+), 26 deletions(-) diff --git a/src/output/alsa_plugin.c b/src/output/alsa_plugin.c index 4802778dc..493eaaa96 100644 --- a/src/output/alsa_plugin.c +++ b/src/output/alsa_plugin.c @@ -20,6 +20,7 @@ #include "../utils.h" #include "../log.h" +#include #include #define ALSA_PCM_NEW_HW_PARAMS_API @@ -33,7 +34,7 @@ typedef snd_pcm_sframes_t alsa_writei_t(snd_pcm_t * pcm, const void *buffer, snd_pcm_uframes_t size); typedef struct _AlsaData { - const char *device; + char *device; /** the mode flags passed to snd_pcm_open */ int mode; @@ -46,11 +47,16 @@ typedef struct _AlsaData { int useMmap; } AlsaData; +static const char * +alsa_device(const AlsaData *ad) +{ + return ad->device != NULL ? ad->device : default_device; +} + static AlsaData *newAlsaData(void) { AlsaData *ret = xmalloc(sizeof(AlsaData)); - ret->device = default_device; ret->mode = 0; ret->pcmHandle = NULL; ret->writei = snd_pcm_writei; @@ -63,8 +69,7 @@ static AlsaData *newAlsaData(void) static void freeAlsaData(AlsaData * ad) { - if (ad->device && ad->device != default_device) - xfree(ad->device); + g_free(ad->device); free(ad); } @@ -75,6 +80,9 @@ alsa_configure(AlsaData *ad, ConfigParam *param) if ((bp = getBlockParam(param, "device"))) ad->device = xstrdup(bp->value); + else + ad->device = NULL; + ad->useMmap = getBoolBlockParam(param, "use_mmap", 1); if (ad->useMmap == CONF_BOOL_UNSET) ad->useMmap = 0; @@ -170,9 +178,9 @@ static bool alsa_openDevice(void *data, struct audio_format *audioFormat) if ((bitformat = get_bitformat(audioFormat)) == SND_PCM_FORMAT_UNKNOWN) ERROR("ALSA device \"%s\" doesn't support %u bit audio\n", - ad->device, audioFormat->bits); + alsa_device(ad), audioFormat->bits); - err = snd_pcm_open(&ad->pcmHandle, ad->device, + err = snd_pcm_open(&ad->pcmHandle, alsa_device(ad), SND_PCM_STREAM_PLAYBACK, ad->mode); if (err < 0) { ad->pcmHandle = NULL; @@ -194,7 +202,7 @@ configure_hw: SND_PCM_ACCESS_MMAP_INTERLEAVED); if (err < 0) { ERROR("Cannot set mmap'ed mode on ALSA device \"%s\": " - " %s\n", ad->device, snd_strerror(-err)); + " %s\n", alsa_device(ad), snd_strerror(-err)); ERROR("Falling back to direct write mode\n"); ad->useMmap = 0; } else @@ -217,14 +225,14 @@ configure_hw: SND_PCM_FORMAT_S16); if (err == 0) { DEBUG("ALSA device \"%s\": converting %u bit to 16 bit\n", - ad->device, audioFormat->bits); + alsa_device(ad), audioFormat->bits); audioFormat->bits = 16; } } if (err < 0) { ERROR("ALSA device \"%s\" does not support %u bit audio: " - "%s\n", ad->device, audioFormat->bits, snd_strerror(-err)); + "%s\n", alsa_device(ad), audioFormat->bits, snd_strerror(-err)); goto fail; } @@ -232,7 +240,7 @@ configure_hw: &channels); if (err < 0) { ERROR("ALSA device \"%s\" does not support %i channels: " - "%s\n", ad->device, (int)audioFormat->channels, + "%s\n", alsa_device(ad), (int)audioFormat->channels, snd_strerror(-err)); goto fail; } @@ -242,7 +250,7 @@ configure_hw: &sample_rate, NULL); if (err < 0 || sample_rate == 0) { ERROR("ALSA device \"%s\" does not support %u Hz audio\n", - ad->device, audioFormat->sample_rate); + alsa_device(ad), audioFormat->sample_rate); goto fail; } audioFormat->sample_rate = sample_rate; @@ -315,7 +323,7 @@ configure_hw: ad->sampleSize = audio_format_frame_size(audioFormat); DEBUG("ALSA device \"%s\" will be playing %i bit, %u channel audio at " - "%u Hz\n", ad->device, audioFormat->bits, + "%u Hz\n", alsa_device(ad), audioFormat->bits, channels, sample_rate); return true; @@ -323,9 +331,9 @@ configure_hw: error: if (cmd) { ERROR("Error opening ALSA device \"%s\" (%s): %s\n", - ad->device, cmd, snd_strerror(-err)); + alsa_device(ad), cmd, snd_strerror(-err)); } else { - ERROR("Error opening ALSA device \"%s\": %s\n", ad->device, + ERROR("Error opening ALSA device \"%s\": %s\n", alsa_device(ad), snd_strerror(-err)); } fail: @@ -338,9 +346,9 @@ fail: static int alsa_errorRecovery(AlsaData * ad, int err) { if (err == -EPIPE) { - DEBUG("Underrun on ALSA device \"%s\"\n", ad->device); + DEBUG("Underrun on ALSA device \"%s\"\n", alsa_device(ad)); } else if (err == -ESTRPIPE) { - DEBUG("ALSA device \"%s\" was suspended\n", ad->device); + DEBUG("ALSA device \"%s\" was suspended\n", alsa_device(ad)); } switch (snd_pcm_state(ad->pcmHandle)) { @@ -410,7 +418,7 @@ alsa_playAudio(void *data, const char *playChunk, size_t size) if (ret < 0) { if (alsa_errorRecovery(ad, ret) < 0) { ERROR("closing ALSA device \"%s\" due to write " - "error: %s\n", ad->device, + "error: %s\n", alsa_device(ad), snd_strerror(-errno)); return false; } diff --git a/src/output/jack_plugin.c b/src/output/jack_plugin.c index 1e6c81b75..d26658e07 100644 --- a/src/output/jack_plugin.c +++ b/src/output/jack_plugin.c @@ -21,6 +21,7 @@ #include +#include #include #include #include @@ -31,7 +32,7 @@ struct jack_data { struct audio_output *ao; /* configuration */ - const char *name; + char *name; const char *output_ports[2]; int ringbuffer_size; @@ -46,6 +47,12 @@ struct jack_data { int shutdown; }; +static const char * +mpd_jack_name(const struct jack_data *jd) +{ + return jd->name != NULL ? jd->name : "mpd"; +} + static struct jack_data * mpd_jack_new(void) { @@ -53,7 +60,6 @@ mpd_jack_new(void) ret = xcalloc(sizeof(*ret), 1); - ret->name = "mpd"; ret->ringbuffer_size = 32768; return ret; @@ -90,8 +96,7 @@ mpd_jack_free(struct jack_data *jd) mpd_jack_client_free(jd); - if (strcmp(jd->name, "mpd") != 0) - xfree(jd->name); + g_free(jd->name); for ( i = ARRAY_SIZE(jd->output_ports); --i >= 0; ) { if (!jd->output_ports[i]) @@ -237,7 +242,8 @@ mpd_jack_init(struct audio_output *ao, && (strcmp(bp->value, "mpd") != 0) ) { jd->name = xstrdup(bp->value); DEBUG("name=%s\n", jd->name); - } + } else + jd->name = NULL; return jd; } @@ -256,7 +262,7 @@ mpd_jack_connect(struct jack_data *jd, struct audio_format *audio_format) jd->audio_format = audio_format; - if ( (jd->client = jack_client_new(jd->name)) == NULL ) { + if ((jd->client = jack_client_new(mpd_jack_name(jd))) == NULL) { ERROR("jack server not running?\n"); return -1; } @@ -299,14 +305,16 @@ mpd_jack_connect(struct jack_data *jd, struct audio_format *audio_format) } if ( jd->output_ports[1] ) { + const char *name = mpd_jack_name(jd); + jd->ringbuffer[0] = jack_ringbuffer_create(jd->ringbuffer_size); jd->ringbuffer[1] = jack_ringbuffer_create(jd->ringbuffer_size); memset(jd->ringbuffer[0]->buf, 0, jd->ringbuffer[0]->size); memset(jd->ringbuffer[1]->buf, 0, jd->ringbuffer[1]->size); - port_name = xmalloc(sizeof(char)*(7+strlen(jd->name))); + port_name = xmalloc(sizeof(char) * (7 + strlen(name))); - sprintf(port_name, "%s:left", jd->name); + sprintf(port_name, "%s:left", name); if ( (jack_connect(jd->client, port_name, jd->output_ports[0])) != 0 ) { ERROR("%s is not a valid Jack Client / Port\n", @@ -314,7 +322,7 @@ mpd_jack_connect(struct jack_data *jd, struct audio_format *audio_format) free(port_name); return -1; } - sprintf(port_name, "%s:right", jd->name); + sprintf(port_name, "%s:right", name); if ( (jack_connect(jd->client, port_name, jd->output_ports[1])) != 0 ) { ERROR("%s is not a valid Jack Client / Port\n",