audio: replaced parseAudioConfig() with audio_format_parse()
Added audio_format_parse() in a separate library, with a modern interface: return a GError instead of logging errors. This allows the caller to deal with the error.
This commit is contained in:
parent
5484aaee5f
commit
5090cf6484
@ -35,6 +35,7 @@ mpd_headers = \
|
||||
ack.h \
|
||||
audio.h \
|
||||
audio_format.h \
|
||||
audio_parser.h \
|
||||
audioOutput.h \
|
||||
output_internal.h \
|
||||
output_api.h \
|
||||
@ -136,6 +137,7 @@ mpd_SOURCES = \
|
||||
$(mpd_headers) \
|
||||
notify.c \
|
||||
audio.c \
|
||||
audio_parser.c \
|
||||
audioOutput.c \
|
||||
output_api.c \
|
||||
output_list.c \
|
||||
|
65
src/audio.c
65
src/audio.c
@ -18,6 +18,7 @@
|
||||
|
||||
#include "audio.h"
|
||||
#include "audio_format.h"
|
||||
#include "audio_parser.h"
|
||||
#include "output_api.h"
|
||||
#include "output_control.h"
|
||||
#include "output_internal.h"
|
||||
@ -44,67 +45,17 @@ void getOutputAudioFormat(const struct audio_format *inAudioFormat,
|
||||
void initAudioConfig(void)
|
||||
{
|
||||
const struct config_param *param = config_get_param(CONF_AUDIO_OUTPUT_FORMAT);
|
||||
GError *error = NULL;
|
||||
bool ret;
|
||||
|
||||
if (NULL == param || NULL == param->value)
|
||||
return;
|
||||
|
||||
if (0 != parseAudioConfig(&configured_audio_format, param->value)) {
|
||||
g_error("error parsing \"%s\" at line %i\n",
|
||||
CONF_AUDIO_OUTPUT_FORMAT, param->line);
|
||||
}
|
||||
}
|
||||
|
||||
int parseAudioConfig(struct audio_format *audioFormat, char *conf)
|
||||
{
|
||||
char *test;
|
||||
|
||||
memset(audioFormat, 0, sizeof(*audioFormat));
|
||||
|
||||
audioFormat->sample_rate = strtol(conf, &test, 10);
|
||||
|
||||
if (*test != ':') {
|
||||
g_warning("error parsing audio output format: %s\n", conf);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (audioFormat->sample_rate <= 0) {
|
||||
g_warning("sample rate %u is not >= 0\n",
|
||||
audioFormat->sample_rate);
|
||||
return -1;
|
||||
}
|
||||
|
||||
audioFormat->bits = (uint8_t)strtoul(test + 1, &test, 10);
|
||||
|
||||
if (*test != ':') {
|
||||
g_warning("error parsing audio output format: %s\n", conf);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (audioFormat->bits != 16 && audioFormat->bits != 24 &&
|
||||
audioFormat->bits != 8) {
|
||||
g_warning("bits %u can not be used for audio output\n",
|
||||
audioFormat->bits);
|
||||
return -1;
|
||||
}
|
||||
|
||||
audioFormat->channels = (uint8_t)strtoul(test + 1, &test, 10);
|
||||
|
||||
if (*test != '\0') {
|
||||
g_warning("error parsing audio output format: %s\n", conf);
|
||||
return -1;
|
||||
}
|
||||
|
||||
switch (audioFormat->channels) {
|
||||
case 1:
|
||||
case 2:
|
||||
break;
|
||||
default:
|
||||
g_warning("channels %u can not be used for audio output\n",
|
||||
audioFormat->channels);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
ret = audio_format_parse(&configured_audio_format, param->value,
|
||||
&error);
|
||||
if (!ret)
|
||||
g_error("error parsing \"%s\" at line %i: %s",
|
||||
CONF_AUDIO_OUTPUT_FORMAT, param->line, error->message);
|
||||
}
|
||||
|
||||
void finishAudioConfig(void)
|
||||
|
@ -26,8 +26,6 @@ struct audio_format;
|
||||
void getOutputAudioFormat(const struct audio_format *inFormat,
|
||||
struct audio_format *outFormat);
|
||||
|
||||
int parseAudioConfig(struct audio_format *audioFormat, char *conf);
|
||||
|
||||
/* make sure initPlayerData is called before this function!! */
|
||||
void initAudioConfig(void);
|
||||
|
||||
|
98
src/audio_parser.c
Normal file
98
src/audio_parser.c
Normal file
@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Copyright (C) 2003-2009 The Music Player Daemon Project
|
||||
* http://www.musicpd.org
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
/*
|
||||
* Parser functions for audio related objects.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "audio_parser.h"
|
||||
#include "audio_format.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
/**
|
||||
* The GLib quark used for errors reported by this library.
|
||||
*/
|
||||
static inline GQuark
|
||||
audio_parser_quark(void)
|
||||
{
|
||||
return g_quark_from_static_string("audio_parser");
|
||||
}
|
||||
|
||||
bool
|
||||
audio_format_parse(struct audio_format *dest, const char *src, GError **error)
|
||||
{
|
||||
char *endptr;
|
||||
unsigned long value;
|
||||
|
||||
audio_format_clear(dest);
|
||||
|
||||
/* parse sample rate */
|
||||
|
||||
value = strtoul(src, &endptr, 10);
|
||||
if (endptr == src) {
|
||||
g_set_error(error, audio_parser_quark(), 0,
|
||||
"Sample rate missing");
|
||||
return false;
|
||||
} else if (*endptr != ':') {
|
||||
g_set_error(error, audio_parser_quark(), 0,
|
||||
"Sample format missing");
|
||||
return false;
|
||||
} else if (value <= 0 || value > G_MAXINT32) {
|
||||
g_set_error(error, audio_parser_quark(), 0,
|
||||
"Invalid sample rate: %lu", value);
|
||||
return false;
|
||||
}
|
||||
|
||||
dest->sample_rate = value;
|
||||
|
||||
/* parse sample format */
|
||||
|
||||
src = endptr + 1;
|
||||
value = strtoul(src, &endptr, 10);
|
||||
if (endptr == src) {
|
||||
g_set_error(error, audio_parser_quark(), 0,
|
||||
"Sample format missing");
|
||||
return false;
|
||||
} else if (*endptr != ':') {
|
||||
g_set_error(error, audio_parser_quark(), 0,
|
||||
"Channel count missing");
|
||||
return false;
|
||||
} else if (value != 16 && value != 24 && value != 8) {
|
||||
g_set_error(error, audio_parser_quark(), 0,
|
||||
"Invalid sample format: %lu", value);
|
||||
return false;
|
||||
}
|
||||
|
||||
dest->bits = value;
|
||||
|
||||
/* parse channel count */
|
||||
|
||||
src = endptr + 1;
|
||||
value = strtoul(src, &endptr, 10);
|
||||
if (*endptr != 0 || (value != 1 && value != 2)) {
|
||||
g_set_error(error, audio_parser_quark(), 0,
|
||||
"Invalid channel count: %s", src);
|
||||
return false;
|
||||
}
|
||||
|
||||
dest->channels = value;
|
||||
|
||||
return true;
|
||||
}
|
36
src/audio_parser.h
Normal file
36
src/audio_parser.h
Normal file
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright (C) 2003-2009 The Music Player Daemon Project
|
||||
* http://www.musicpd.org
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
/*
|
||||
* Parser functions for audio related objects.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef AUDIO_PARSER_H
|
||||
#define AUDIO_PARSER_H
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
struct audio_format;
|
||||
|
||||
bool
|
||||
audio_format_parse(struct audio_format *dest, const char *src, GError **error);
|
||||
|
||||
#endif
|
@ -20,7 +20,7 @@
|
||||
#include "output_api.h"
|
||||
#include "output_internal.h"
|
||||
#include "output_list.h"
|
||||
#include "audio.h"
|
||||
#include "audio_parser.h"
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
@ -94,9 +94,14 @@ audio_output_init(struct audio_output *ao, const struct config_param *param)
|
||||
pcm_convert_init(&ao->convert_state);
|
||||
|
||||
if (format) {
|
||||
if (0 != parseAudioConfig(&ao->config_audio_format, format)) {
|
||||
g_error("error parsing format at line %i\n", bp->line);
|
||||
}
|
||||
GError *error = NULL;
|
||||
bool ret;
|
||||
|
||||
ret = audio_format_parse(&ao->config_audio_format, format,
|
||||
&error);
|
||||
if (!ret)
|
||||
g_error("error parsing format at line %i: %s",
|
||||
bp->line, error->message);
|
||||
} else
|
||||
audio_format_clear(&ao->config_audio_format);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user