Loading...
Searching...
No Matches
raw-json.h
Go to the documentation of this file.
/* Simple Plugin API */
/* SPDX-FileCopyrightText: Copyright © 2024 Wim Taymans */
/* SPDX-License-Identifier: MIT */
#ifndef SPA_AUDIO_RAW_JSON_H
#define SPA_AUDIO_RAW_JSON_H
#include <spa/utils/dict.h>
#include <spa/utils/json.h>
#include <spa/param/audio/raw.h>
#include <spa/param/audio/raw-types.h>
#include <spa/param/audio/layout-types.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifndef SPA_API_AUDIO_RAW_JSON
#ifdef SPA_API_IMPL
#define SPA_API_AUDIO_RAW_JSON SPA_API_IMPL
#else
#define SPA_API_AUDIO_RAW_JSON static inline
#endif
#endif
SPA_API_AUDIO_RAW_JSON int
spa_audio_parse_position_n(const char *str, size_t len,
uint32_t *position, uint32_t max_position, uint32_t *n_channels)
{
struct spa_json iter;
char v[256];
uint32_t channels = 0;
if (spa_json_begin_array_relax(&iter, str, len) <= 0)
return 0;
while (spa_json_get_string(&iter, v, sizeof(v)) > 0) {
if (channels < max_position)
position[channels] = spa_type_audio_channel_from_short_name(v);
channels++;
}
*n_channels = channels;
return channels;
}
SPA_API_AUDIO_RAW_JSON int
spa_audio_parse_position(const char *str, size_t len,
uint32_t *position, uint32_t *n_channels)
{
return spa_audio_parse_position_n(str, len, position, SPA_AUDIO_MAX_CHANNELS, n_channels);
}
SPA_API_AUDIO_RAW_JSON int
spa_audio_parse_layout(const char *str, uint32_t *position, uint32_t max_position,
uint32_t *n_channels)
{
struct spa_audio_layout_info l;
uint32_t i;
if (spa_audio_layout_info_parse_name(&l, sizeof(l), str) <= 0)
return 0;
for (i = 0; i < l.n_channels && i < max_position; i++)
position[i] = l.position[i];
*n_channels = l.n_channels;
return l.n_channels;
}
SPA_API_AUDIO_RAW_JSON int
spa_audio_info_raw_ext_update(struct spa_audio_info_raw *info, size_t size,
const char *key, const char *val, bool force)
{
uint32_t v;
uint32_t max_position = SPA_AUDIO_INFO_RAW_MAX_POSITION(size);
if (!SPA_AUDIO_INFO_RAW_VALID_SIZE(size))
return -EINVAL;
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)) {
if (v > max_position)
return -ECHRNG;
info->channels = v;
}
} else if (spa_streq(key, SPA_KEY_AUDIO_LAYOUT)) {
if (force || info->channels == 0) {
if (spa_audio_parse_layout(val, info->position, max_position, &v) > 0) {
if (v > max_position)
return -ECHRNG;
info->channels = v;
SPA_FLAG_CLEAR(info->flags, SPA_AUDIO_FLAG_UNPOSITIONED);
}
}
} else if (spa_streq(key, SPA_KEY_AUDIO_POSITION)) {
if (force || info->channels == 0) {
if (spa_audio_parse_position_n(val, strlen(val), info->position,
max_position, &v) > 0) {
if (v > max_position)
return -ECHRNG;
info->channels = v;
SPA_FLAG_CLEAR(info->flags, SPA_AUDIO_FLAG_UNPOSITIONED);
}
}
}
return 0;
}
SPA_API_AUDIO_RAW_JSON int
spa_audio_info_raw_update(struct spa_audio_info_raw *info,
const char *key, const char *val, bool force)
{
return spa_audio_info_raw_ext_update(info, sizeof(*info), key, val, force);
}
SPA_API_AUDIO_RAW_JSON int
spa_audio_info_raw_ext_init_dict_keys_va(struct spa_audio_info_raw *info, size_t size,
const struct spa_dict *defaults,
const struct spa_dict *dict, va_list args)
{
int res;
if (!SPA_AUDIO_INFO_RAW_VALID_SIZE(size))
return -EINVAL;
memset(info, 0, size);
SPA_FLAG_SET(info->flags, SPA_AUDIO_FLAG_UNPOSITIONED);
if (dict) {
const char *val, *key;
while ((key = va_arg(args, const char *))) {
if ((val = spa_dict_lookup(dict, key)) == NULL)
continue;
if ((res = spa_audio_info_raw_ext_update(info, size,
key, val, true)) < 0)
return res;
}
}
if (defaults) {
const struct spa_dict_item *it;
spa_dict_for_each(it, defaults)
if ((res = spa_audio_info_raw_ext_update(info, size,
it->key, it->value, false)) < 0)
return res;
}
return 0;
}
SPA_API_AUDIO_RAW_JSON int SPA_SENTINEL
spa_audio_info_raw_ext_init_dict_keys(struct spa_audio_info_raw *info, size_t size,
const struct spa_dict *defaults,
const struct spa_dict *dict, ...)
{
va_list args;
int res;
va_start(args, dict);
res = spa_audio_info_raw_ext_init_dict_keys_va(info, size, defaults, dict, args);
va_end(args);
return res;
}
SPA_API_AUDIO_RAW_JSON 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, ...)
{
va_list args;
int res;
va_start(args, dict);
res = spa_audio_info_raw_ext_init_dict_keys_va(info, sizeof(*info), defaults, dict, args);
va_end(args);
return res;
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* SPA_AUDIO_RAW_JSON_H */
raw-types.h
spa/param/audio/raw-types.h
raw.h
spa/param/audio/raw.h
args
uint32_t int int const char va_list args
Definition core.h:434
va_end
va_end(args)
res
uint32_t int int res
Definition core.h:433
va_start
va_start(args, message)
spa_dict_lookup
SPA_API_DICT const char * spa_dict_lookup(const struct spa_dict *dict, const char *key)
Definition dict.h:114
spa_dict_for_each
#define spa_dict_for_each(item, dict)
Definition dict.h:71
spa_json_begin_array_relax
SPA_API_JSON_UTILS int spa_json_begin_array_relax(struct spa_json *iter, const char *data, size_t size)
Definition json.h:195
spa_json_get_string
SPA_API_JSON_UTILS int spa_json_get_string(struct spa_json *iter, char *res, int maxlen)
Definition json.h:84
spa_type_audio_format_from_short_name
SPA_API_AUDIO_RAW_TYPES uint32_t spa_type_audio_format_from_short_name(const char *name)
Definition raw-types.h:149
spa_audio_info_raw_update
SPA_API_AUDIO_RAW_JSON int spa_audio_info_raw_update(struct spa_audio_info_raw *info, const char *key, const char *val, bool force)
Definition raw-json.h:125
spa_audio_info_raw_ext_init_dict_keys_va
SPA_API_AUDIO_RAW_JSON int spa_audio_info_raw_ext_init_dict_keys_va(struct spa_audio_info_raw *info, size_t size, const struct spa_dict *defaults, const struct spa_dict *dict, va_list args)
Definition raw-json.h:132
SPA_KEY_AUDIO_POSITION
#define SPA_KEY_AUDIO_POSITION
channel positions as comma separated list of channels ex.
Definition raw.h:326
SPA_AUDIO_FLAG_UNPOSITIONED
#define SPA_AUDIO_FLAG_UNPOSITIONED
Definition raw.h:288
spa_audio_info_raw_init_dict_keys
SPA_API_AUDIO_RAW_JSON int spa_audio_info_raw_init_dict_keys(struct spa_audio_info_raw *info, const struct spa_dict *defaults, const struct spa_dict *dict,...)
Definition raw-json.h:177
SPA_AUDIO_INFO_RAW_MAX_POSITION
#define SPA_AUDIO_INFO_RAW_MAX_POSITION(size)
Definition raw.h:307
SPA_KEY_AUDIO_RATE
#define SPA_KEY_AUDIO_RATE
an audio sample rate as int
Definition raw.h:322
spa_type_audio_channel_from_short_name
SPA_API_AUDIO_RAW_TYPES uint32_t spa_type_audio_channel_from_short_name(const char *name)
Definition raw-types.h:281
spa_audio_parse_position_n
SPA_API_AUDIO_RAW_JSON int spa_audio_parse_position_n(const char *str, size_t len, uint32_t *position, uint32_t max_position, uint32_t *n_channels)
Definition raw-json.h:39
spa_audio_layout_info_parse_name
SPA_API_AUDIO_LAYOUT_TYPES int spa_audio_layout_info_parse_name(struct spa_audio_layout_info *layout, size_t size, const char *name)
Definition layout-types.h:89
SPA_KEY_AUDIO_CHANNELS
#define SPA_KEY_AUDIO_CHANNELS
an audio channel count as int
Definition raw.h:320
spa_audio_info_raw_ext_update
SPA_API_AUDIO_RAW_JSON int spa_audio_info_raw_ext_update(struct spa_audio_info_raw *info, size_t size, const char *key, const char *val, bool force)
Definition raw-json.h:80
spa_audio_parse_position
SPA_API_AUDIO_RAW_JSON int spa_audio_parse_position(const char *str, size_t len, uint32_t *position, uint32_t *n_channels)
Definition raw-json.h:59
SPA_API_AUDIO_RAW_JSON
#define SPA_API_AUDIO_RAW_JSON
Definition raw-json.h:34
SPA_KEY_AUDIO_FORMAT
#define SPA_KEY_AUDIO_FORMAT
an audio format as string, Ex.
Definition raw.h:314
spa_audio_format
spa_audio_format
Definition raw.h:33
SPA_KEY_AUDIO_LAYOUT
#define SPA_KEY_AUDIO_LAYOUT
channel positions as predefined layout
Definition raw.h:324
SPA_AUDIO_INFO_RAW_VALID_SIZE
#define SPA_AUDIO_INFO_RAW_VALID_SIZE(size)
Definition raw.h:310
SPA_AUDIO_MAX_CHANNELS
#define SPA_AUDIO_MAX_CHANNELS
Definition raw.h:30
spa_audio_parse_layout
SPA_API_AUDIO_RAW_JSON int spa_audio_parse_layout(const char *str, uint32_t *position, uint32_t max_position, uint32_t *n_channels)
Definition raw-json.h:66
spa_audio_info_raw_ext_init_dict_keys
SPA_API_AUDIO_RAW_JSON int spa_audio_info_raw_ext_init_dict_keys(struct spa_audio_info_raw *info, size_t size, const struct spa_dict *defaults, const struct spa_dict *dict,...)
Definition raw-json.h:164
spa_atou32
SPA_API_STRING bool spa_atou32(const char *str, uint32_t *val, int base)
Convert str to an uint32_t with the given base and store the result in val.
Definition string.h:139
spa_streq
SPA_API_STRING bool spa_streq(const char *s1, const char *s2)
Definition string.h:52
SPA_FLAG_SET
#define SPA_FLAG_SET(field, flag)
Definition defs.h:93
SPA_FLAG_CLEAR
#define SPA_FLAG_CLEAR(field, flag)
Definition defs.h:94
SPA_SENTINEL
#define SPA_SENTINEL
Definition defs.h:307
json.h
spa/utils/json.h
layout-types.h
spa/param/audio/layout-types.h
spa_audio_info_raw
Audio information description.
Definition raw.h:292
spa_audio_layout_info
Definition layout.h:26
spa_audio_layout_info::position
uint32_t position[SPA_AUDIO_MAX_CHANNELS]
Definition layout.h:28
spa_audio_layout_info::n_channels
uint32_t n_channels
Definition layout.h:27
spa_dict_item
Definition dict.h:41
spa_dict
Definition dict.h:51
spa_json
Definition json-core.h:49
dict.h
spa/utils/dict.h