spa: make helper to init spa_audio_info_raw from dict

Make a function that can initialize raw audio info from a dict and fill
in the defaults. We can use this in many of the modules when the audio
format is parsed.
This commit is contained in:
Wim Taymans 2024-09-18 15:46:49 +02:00
parent d932e52d5b
commit e3a7035e8f
19 changed files with 204 additions and 255 deletions

View file

@ -14,7 +14,9 @@ extern "C" {
* \{
*/
#include <spa/utils/dict.h>
#include <spa/utils/json.h>
#include <spa/param/audio/raw.h>
#include <spa/param/audio/raw-types.h>
static inline int
@ -36,6 +38,51 @@ spa_audio_parse_position(const char *str, size_t len,
return channels;
}
static inline int
spa_audio_info_raw_update(struct spa_audio_info_raw *info, const char *key, const char *val, bool force)
{
uint32_t v;
if (spa_streq(key, SPA_KEY_AUDIO_FORMAT)) {
if (force || info->format == 0)
info->format = (enum spa_audio_format)spa_type_audio_format_from_short_name(val);
} else if (spa_streq(key, SPA_KEY_AUDIO_RATE)) {
if (spa_atou32(val, &v, 0) && (force || info->rate == 0))
info->rate = v;
} else if (spa_streq(key, SPA_KEY_AUDIO_CHANNELS)) {
if (spa_atou32(val, &v, 0) && (force || info->channels == 0))
info->channels = SPA_MIN(v, SPA_AUDIO_MAX_CHANNELS);
} else if (spa_streq(key, SPA_KEY_AUDIO_POSITION)) {
if (force || info->channels == 0)
spa_audio_parse_position(val, strlen(val), info->position, &info->channels);
}
return 0;
}
static inline int SPA_SENTINEL
spa_audio_info_raw_init_dict_keys(struct spa_audio_info_raw *info,
const struct spa_dict *defaults,
const struct spa_dict *dict, ...)
{
spa_zero(*info);
if (dict) {
const char *val, *key;
va_list args;
va_start(args, dict);
while ((key = va_arg(args, const char *))) {
if ((val = spa_dict_lookup(dict, key)) == NULL)
continue;
spa_audio_info_raw_update(info, key, val, true);
}
va_end(args);
}
if (defaults) {
const struct spa_dict_item *it;
spa_dict_for_each(it, defaults)
spa_audio_info_raw_update(info, it->key, it->value, false);
}
return 0;
}
/**
* \}
*/

View file

@ -28,7 +28,8 @@ struct spa_dict_item {
const char *value;
};
#define SPA_DICT_ITEM_INIT(key,value) ((struct spa_dict_item) { (key), (value) })
#define SPA_DICT_ITEM(key,value) ((struct spa_dict_item) { (key), (value) })
#define SPA_DICT_ITEM_INIT(key,value) SPA_DICT_ITEM(key,value)
struct spa_dict {
#define SPA_DICT_FLAG_SORTED (1<<0) /**< items are sorted */
@ -37,8 +38,12 @@ struct spa_dict {
const struct spa_dict_item *items;
};
#define SPA_DICT_INIT(items,n_items) ((struct spa_dict) { 0, (n_items), (items) })
#define SPA_DICT_INIT_ARRAY(items) ((struct spa_dict) { 0, SPA_N_ELEMENTS(items), (items) })
#define SPA_DICT(items,n_items) ((struct spa_dict) { 0, (n_items), (items) })
#define SPA_DICT_ARRAY(items) SPA_DICT((items),SPA_N_ELEMENTS(items))
#define SPA_DICT_ITEMS(...) SPA_DICT_ARRAY(((struct spa_dict_item[]) { __VA_ARGS__}))
#define SPA_DICT_INIT(items,n_items) SPA_DICT(items,n_items)
#define SPA_DICT_INIT_ARRAY(items) SPA_DICT_ARRAY(items)
#define spa_dict_for_each(item, dict) \
for ((item) = (dict)->items; \