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.
This commit is contained in:
Max Kellermann 2008-11-01 14:04:14 +01:00
parent 9fdac529b4
commit aa33422de6
2 changed files with 42 additions and 26 deletions

View File

@ -20,6 +20,7 @@
#include "../utils.h" #include "../utils.h"
#include "../log.h" #include "../log.h"
#include <glib.h>
#include <alsa/asoundlib.h> #include <alsa/asoundlib.h>
#define ALSA_PCM_NEW_HW_PARAMS_API #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); snd_pcm_uframes_t size);
typedef struct _AlsaData { typedef struct _AlsaData {
const char *device; char *device;
/** the mode flags passed to snd_pcm_open */ /** the mode flags passed to snd_pcm_open */
int mode; int mode;
@ -46,11 +47,16 @@ typedef struct _AlsaData {
int useMmap; int useMmap;
} AlsaData; } AlsaData;
static const char *
alsa_device(const AlsaData *ad)
{
return ad->device != NULL ? ad->device : default_device;
}
static AlsaData *newAlsaData(void) static AlsaData *newAlsaData(void)
{ {
AlsaData *ret = xmalloc(sizeof(AlsaData)); AlsaData *ret = xmalloc(sizeof(AlsaData));
ret->device = default_device;
ret->mode = 0; ret->mode = 0;
ret->pcmHandle = NULL; ret->pcmHandle = NULL;
ret->writei = snd_pcm_writei; ret->writei = snd_pcm_writei;
@ -63,8 +69,7 @@ static AlsaData *newAlsaData(void)
static void freeAlsaData(AlsaData * ad) static void freeAlsaData(AlsaData * ad)
{ {
if (ad->device && ad->device != default_device) g_free(ad->device);
xfree(ad->device);
free(ad); free(ad);
} }
@ -75,6 +80,9 @@ alsa_configure(AlsaData *ad, ConfigParam *param)
if ((bp = getBlockParam(param, "device"))) if ((bp = getBlockParam(param, "device")))
ad->device = xstrdup(bp->value); ad->device = xstrdup(bp->value);
else
ad->device = NULL;
ad->useMmap = getBoolBlockParam(param, "use_mmap", 1); ad->useMmap = getBoolBlockParam(param, "use_mmap", 1);
if (ad->useMmap == CONF_BOOL_UNSET) if (ad->useMmap == CONF_BOOL_UNSET)
ad->useMmap = 0; 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) if ((bitformat = get_bitformat(audioFormat)) == SND_PCM_FORMAT_UNKNOWN)
ERROR("ALSA device \"%s\" doesn't support %u bit audio\n", 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); SND_PCM_STREAM_PLAYBACK, ad->mode);
if (err < 0) { if (err < 0) {
ad->pcmHandle = NULL; ad->pcmHandle = NULL;
@ -194,7 +202,7 @@ configure_hw:
SND_PCM_ACCESS_MMAP_INTERLEAVED); SND_PCM_ACCESS_MMAP_INTERLEAVED);
if (err < 0) { if (err < 0) {
ERROR("Cannot set mmap'ed mode on ALSA device \"%s\": " 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"); ERROR("Falling back to direct write mode\n");
ad->useMmap = 0; ad->useMmap = 0;
} else } else
@ -217,14 +225,14 @@ configure_hw:
SND_PCM_FORMAT_S16); SND_PCM_FORMAT_S16);
if (err == 0) { if (err == 0) {
DEBUG("ALSA device \"%s\": converting %u bit to 16 bit\n", DEBUG("ALSA device \"%s\": converting %u bit to 16 bit\n",
ad->device, audioFormat->bits); alsa_device(ad), audioFormat->bits);
audioFormat->bits = 16; audioFormat->bits = 16;
} }
} }
if (err < 0) { if (err < 0) {
ERROR("ALSA device \"%s\" does not support %u bit audio: " 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; goto fail;
} }
@ -232,7 +240,7 @@ configure_hw:
&channels); &channels);
if (err < 0) { if (err < 0) {
ERROR("ALSA device \"%s\" does not support %i channels: " 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)); snd_strerror(-err));
goto fail; goto fail;
} }
@ -242,7 +250,7 @@ configure_hw:
&sample_rate, NULL); &sample_rate, NULL);
if (err < 0 || sample_rate == 0) { if (err < 0 || sample_rate == 0) {
ERROR("ALSA device \"%s\" does not support %u Hz audio\n", ERROR("ALSA device \"%s\" does not support %u Hz audio\n",
ad->device, audioFormat->sample_rate); alsa_device(ad), audioFormat->sample_rate);
goto fail; goto fail;
} }
audioFormat->sample_rate = sample_rate; audioFormat->sample_rate = sample_rate;
@ -315,7 +323,7 @@ configure_hw:
ad->sampleSize = audio_format_frame_size(audioFormat); ad->sampleSize = audio_format_frame_size(audioFormat);
DEBUG("ALSA device \"%s\" will be playing %i bit, %u channel audio at " 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); channels, sample_rate);
return true; return true;
@ -323,9 +331,9 @@ configure_hw:
error: error:
if (cmd) { if (cmd) {
ERROR("Error opening ALSA device \"%s\" (%s): %s\n", ERROR("Error opening ALSA device \"%s\" (%s): %s\n",
ad->device, cmd, snd_strerror(-err)); alsa_device(ad), cmd, snd_strerror(-err));
} else { } 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)); snd_strerror(-err));
} }
fail: fail:
@ -338,9 +346,9 @@ fail:
static int alsa_errorRecovery(AlsaData * ad, int err) static int alsa_errorRecovery(AlsaData * ad, int err)
{ {
if (err == -EPIPE) { 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) { } 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)) { switch (snd_pcm_state(ad->pcmHandle)) {
@ -410,7 +418,7 @@ alsa_playAudio(void *data, const char *playChunk, size_t size)
if (ret < 0) { if (ret < 0) {
if (alsa_errorRecovery(ad, ret) < 0) { if (alsa_errorRecovery(ad, ret) < 0) {
ERROR("closing ALSA device \"%s\" due to write " ERROR("closing ALSA device \"%s\" due to write "
"error: %s\n", ad->device, "error: %s\n", alsa_device(ad),
snd_strerror(-errno)); snd_strerror(-errno));
return false; return false;
} }

View File

@ -21,6 +21,7 @@
#include <assert.h> #include <assert.h>
#include <glib.h>
#include <jack/jack.h> #include <jack/jack.h>
#include <jack/types.h> #include <jack/types.h>
#include <jack/ringbuffer.h> #include <jack/ringbuffer.h>
@ -31,7 +32,7 @@ struct jack_data {
struct audio_output *ao; struct audio_output *ao;
/* configuration */ /* configuration */
const char *name; char *name;
const char *output_ports[2]; const char *output_ports[2];
int ringbuffer_size; int ringbuffer_size;
@ -46,6 +47,12 @@ struct jack_data {
int shutdown; int shutdown;
}; };
static const char *
mpd_jack_name(const struct jack_data *jd)
{
return jd->name != NULL ? jd->name : "mpd";
}
static struct jack_data * static struct jack_data *
mpd_jack_new(void) mpd_jack_new(void)
{ {
@ -53,7 +60,6 @@ mpd_jack_new(void)
ret = xcalloc(sizeof(*ret), 1); ret = xcalloc(sizeof(*ret), 1);
ret->name = "mpd";
ret->ringbuffer_size = 32768; ret->ringbuffer_size = 32768;
return ret; return ret;
@ -90,8 +96,7 @@ mpd_jack_free(struct jack_data *jd)
mpd_jack_client_free(jd); mpd_jack_client_free(jd);
if (strcmp(jd->name, "mpd") != 0) g_free(jd->name);
xfree(jd->name);
for ( i = ARRAY_SIZE(jd->output_ports); --i >= 0; ) { for ( i = ARRAY_SIZE(jd->output_ports); --i >= 0; ) {
if (!jd->output_ports[i]) if (!jd->output_ports[i])
@ -237,7 +242,8 @@ mpd_jack_init(struct audio_output *ao,
&& (strcmp(bp->value, "mpd") != 0) ) { && (strcmp(bp->value, "mpd") != 0) ) {
jd->name = xstrdup(bp->value); jd->name = xstrdup(bp->value);
DEBUG("name=%s\n", jd->name); DEBUG("name=%s\n", jd->name);
} } else
jd->name = NULL;
return jd; return jd;
} }
@ -256,7 +262,7 @@ mpd_jack_connect(struct jack_data *jd, struct audio_format *audio_format)
jd->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"); ERROR("jack server not running?\n");
return -1; return -1;
} }
@ -299,14 +305,16 @@ mpd_jack_connect(struct jack_data *jd, struct audio_format *audio_format)
} }
if ( jd->output_ports[1] ) { if ( jd->output_ports[1] ) {
const char *name = mpd_jack_name(jd);
jd->ringbuffer[0] = jack_ringbuffer_create(jd->ringbuffer_size); jd->ringbuffer[0] = jack_ringbuffer_create(jd->ringbuffer_size);
jd->ringbuffer[1] = 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[0]->buf, 0, jd->ringbuffer[0]->size);
memset(jd->ringbuffer[1]->buf, 0, jd->ringbuffer[1]->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, if ( (jack_connect(jd->client, port_name,
jd->output_ports[0])) != 0 ) { jd->output_ports[0])) != 0 ) {
ERROR("%s is not a valid Jack Client / Port\n", 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); free(port_name);
return -1; return -1;
} }
sprintf(port_name, "%s:right", jd->name); sprintf(port_name, "%s:right", name);
if ( (jack_connect(jd->client, port_name, if ( (jack_connect(jd->client, port_name,
jd->output_ports[1])) != 0 ) { jd->output_ports[1])) != 0 ) {
ERROR("%s is not a valid Jack Client / Port\n", ERROR("%s is not a valid Jack Client / Port\n",