modules: fix format parsing

Use the same logic for parsing the format.
This commit is contained in:
Wim Taymans 2022-06-23 12:39:51 +02:00
parent da95043002
commit c8b8b24a9c
8 changed files with 210 additions and 178 deletions

View file

@ -107,6 +107,7 @@
* tunnel.mode = playback
* # Set the pipe name to tunnel to
* pipe.filename = "/tmp/fifo_output"
* #audio.format=<sample format>
* #audio.rate=<sample rate>
* #audio.channels=<number of channels>
* #audio.position=<channel map>
@ -125,6 +126,11 @@
#define DEFAULT_CAPTURE_FILENAME "/tmp/fifo_input"
#define DEFAULT_PLAYBACK_FILENAME "/tmp/fifo_output"
#define DEFAULT_FORMAT "S16"
#define DEFAULT_RATE 48000
#define DEFAULT_CHANNELS 2
#define DEFAULT_POSITION "[ FL FR ]"
PW_LOG_TOPIC_STATIC(mod_topic, "mod." NAME);
#define PW_LOG_TOPIC_DEFAULT mod_topic
@ -133,6 +139,7 @@ PW_LOG_TOPIC_STATIC(mod_topic, "mod." NAME);
"[ node.name=<name of the nodes> ] " \
"[ node.description=<description of the nodes> ] " \
"[ node.target=<remote node target name> ] " \
"[ audio.format=<sample format> ] " \
"[ audio.rate=<sample rate> ] " \
"[ audio.channels=<number of channels> ] " \
"[ audio.position=<channel map> ] " \
@ -511,31 +518,28 @@ static inline uint32_t format_from_name(const char *name, size_t len)
return SPA_AUDIO_FORMAT_UNKNOWN;
}
static void parse_audio_info(struct pw_properties *props, struct spa_audio_info_raw *info)
static void parse_audio_info(const struct pw_properties *props, struct spa_audio_info_raw *info)
{
const char *str;
*info = SPA_AUDIO_INFO_RAW_INIT(
.rate = 48000,
.channels = 2,
.format = SPA_AUDIO_FORMAT_S16);
if ((str = pw_properties_get(props, PW_KEY_AUDIO_FORMAT)) != NULL) {
uint32_t id;
id = format_from_name(str, strlen(str));
if (id != SPA_AUDIO_FORMAT_UNKNOWN)
info->format = id;
}
spa_zero(*info);
if ((str = pw_properties_get(props, PW_KEY_AUDIO_FORMAT)) == NULL)
str = DEFAULT_FORMAT;
info->format = format_from_name(str, strlen(str));
info->rate = pw_properties_get_uint32(props, PW_KEY_AUDIO_RATE, info->rate);
if (info->rate == 0)
info->rate = DEFAULT_RATE;
info->channels = pw_properties_get_uint32(props, PW_KEY_AUDIO_CHANNELS, info->channels);
info->channels = SPA_MIN(info->channels, SPA_AUDIO_MAX_CHANNELS);
if ((str = pw_properties_get(props, SPA_KEY_AUDIO_POSITION)) != NULL)
parse_position(info, str, strlen(str));
if (info->channels == 0)
parse_position(info, DEFAULT_POSITION, strlen(DEFAULT_POSITION));
}
static int calc_frame_size(struct spa_audio_info_raw *info)
static int calc_frame_size(const struct spa_audio_info_raw *info)
{
int res = info->channels;
switch (info->format) {
@ -662,13 +666,6 @@ int pipewire__module_init(struct pw_impl_module *module, const char *args)
parse_audio_info(impl->stream_props, &impl->info);
if (impl->info.rate != 0 &&
pw_properties_get(props, PW_KEY_NODE_RATE) == NULL)
pw_properties_setf(props, PW_KEY_NODE_RATE,
"1/%u", impl->info.rate),
copy_props(impl, props, PW_KEY_NODE_RATE);
impl->frame_size = calc_frame_size(&impl->info);
if (impl->frame_size == 0) {
pw_log_error("unsupported audio format:%d channels:%d",
@ -676,6 +673,13 @@ int pipewire__module_init(struct pw_impl_module *module, const char *args)
res = -EINVAL;
goto error;
}
if (impl->info.rate != 0 &&
pw_properties_get(props, PW_KEY_NODE_RATE) == NULL)
pw_properties_setf(props, PW_KEY_NODE_RATE,
"1/%u", impl->info.rate),
copy_props(impl, props, PW_KEY_NODE_RATE);
impl->leftover = calloc(1, impl->frame_size);
if (impl->leftover == NULL) {
res = -errno;