mirror of
https://gitlab.freedesktop.org/pipewire/pipewire.git
synced 2025-11-02 09:01:50 -05:00
spa: add spa_json_begin_array/object and relaxed versions
Add spa_json_begin_array/object to replace spa_json_init+spa_json_begin_array/object This function is better because it does not waste a useless spa_json structure as an iterator. The relaxed versions also error out when the container is mismatched because parsing a mismatched container is not going to give any results anyway.
This commit is contained in:
parent
feccb882b6
commit
cd81b5f39a
51 changed files with 401 additions and 452 deletions
|
|
@ -70,10 +70,9 @@ static int parse_mic_geometry(struct impl_data *impl, const char *mic_geometry,
|
|||
{
|
||||
int res;
|
||||
size_t i;
|
||||
struct spa_json it[2];
|
||||
struct spa_json it[1];
|
||||
|
||||
spa_json_init(&it[0], mic_geometry, strlen(mic_geometry));
|
||||
if (spa_json_enter_array(&it[0], &it[1]) <= 0) {
|
||||
if (spa_json_begin_array(&it[0], mic_geometry, strlen(mic_geometry)) <= 0) {
|
||||
spa_log_error(impl->log, "Error: webrtc.mic-geometry expects an array");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
|
@ -81,7 +80,7 @@ static int parse_mic_geometry(struct impl_data *impl, const char *mic_geometry,
|
|||
for (i = 0; i < geometry.size(); i++) {
|
||||
float f[3];
|
||||
|
||||
if ((res = parse_point(&it[1], f)) < 0) {
|
||||
if ((res = parse_point(&it[0], f)) < 0) {
|
||||
spa_log_error(impl->log, "Error: can't parse webrtc.mic-geometry points: %d", res);
|
||||
return res;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -452,17 +452,16 @@ static int add_pro_profile(pa_card *impl, uint32_t index)
|
|||
|
||||
static bool contains_string(const char *arr, const char *str)
|
||||
{
|
||||
struct spa_json it[2];
|
||||
struct spa_json it[1];
|
||||
char v[256];
|
||||
|
||||
if (arr == NULL || str == NULL)
|
||||
return false;
|
||||
|
||||
spa_json_init(&it[0], arr, strlen(arr));
|
||||
if (spa_json_enter_array(&it[0], &it[1]) <= 0)
|
||||
spa_json_init(&it[1], arr, strlen(arr));
|
||||
if (spa_json_begin_array_relax(&it[0], arr, strlen(arr)) <= 0)
|
||||
return false;
|
||||
|
||||
while (spa_json_get_string(&it[1], v, sizeof(v)) > 0) {
|
||||
while (spa_json_get_string(&it[0], v, sizeof(v)) > 0) {
|
||||
if (spa_streq(v, str))
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -968,16 +968,15 @@ int spa_alsa_init(struct state *state, const struct spa_dict *info)
|
|||
} else if (spa_streq(k, "clock.quantum-limit")) {
|
||||
spa_atou32(s, &state->quantum_limit, 0);
|
||||
} else if (spa_streq(k, SPA_KEY_API_ALSA_BIND_CTLS)) {
|
||||
struct spa_json it[2];
|
||||
struct spa_json it[1];
|
||||
char v[256];
|
||||
unsigned int i = 0;
|
||||
|
||||
/* Read a list of ALSA control names to bind as params */
|
||||
spa_json_init(&it[0], s, strlen(s));
|
||||
if (spa_json_enter_array(&it[0], &it[1]) <= 0)
|
||||
spa_json_init(&it[1], s, strlen(s));
|
||||
if (spa_json_begin_array_relax(&it[0], s, strlen(s)) <= 0)
|
||||
continue;
|
||||
|
||||
while (spa_json_get_string(&it[1], v, sizeof(v)) > 0 &&
|
||||
while (spa_json_get_string(&it[0], v, sizeof(v)) > 0 &&
|
||||
i < SPA_N_ELEMENTS(state->bound_ctls)) {
|
||||
snprintf(state->bound_ctls[i].name,
|
||||
sizeof(state->bound_ctls[i].name), "%s", v);
|
||||
|
|
|
|||
|
|
@ -325,15 +325,14 @@ static inline uint32_t spa_alsa_channel_from_name(const char *name)
|
|||
|
||||
static inline void spa_alsa_parse_position(struct channel_map *map, const char *val, size_t len)
|
||||
{
|
||||
struct spa_json it[2];
|
||||
struct spa_json it[1];
|
||||
char v[256];
|
||||
|
||||
spa_json_init(&it[0], val, len);
|
||||
if (spa_json_enter_array(&it[0], &it[1]) <= 0)
|
||||
spa_json_init(&it[1], val, len);
|
||||
if (spa_json_begin_array_relax(&it[0], val, len) <= 0)
|
||||
return;
|
||||
|
||||
map->channels = 0;
|
||||
while (spa_json_get_string(&it[1], v, sizeof(v)) > 0 &&
|
||||
while (spa_json_get_string(&it[0], v, sizeof(v)) > 0 &&
|
||||
map->channels < SPA_AUDIO_MAX_CHANNELS) {
|
||||
map->pos[map->channels++] = spa_alsa_channel_from_name(v);
|
||||
}
|
||||
|
|
@ -341,16 +340,15 @@ static inline void spa_alsa_parse_position(struct channel_map *map, const char *
|
|||
|
||||
static inline uint32_t spa_alsa_parse_rates(uint32_t *rates, uint32_t max, const char *val, size_t len)
|
||||
{
|
||||
struct spa_json it[2];
|
||||
struct spa_json it[1];
|
||||
char v[256];
|
||||
uint32_t count;
|
||||
|
||||
spa_json_init(&it[0], val, len);
|
||||
if (spa_json_enter_array(&it[0], &it[1]) <= 0)
|
||||
spa_json_init(&it[1], val, len);
|
||||
if (spa_json_begin_array_relax(&it[0], val, len) <= 0)
|
||||
return 0;
|
||||
|
||||
count = 0;
|
||||
while (spa_json_get_string(&it[1], v, sizeof(v)) > 0 && count < max)
|
||||
while (spa_json_get_string(&it[0], v, sizeof(v)) > 0 && count < max)
|
||||
rates[count++] = atoi(v);
|
||||
return count;
|
||||
}
|
||||
|
|
@ -367,15 +365,14 @@ static inline uint32_t spa_alsa_iec958_codec_from_name(const char *name)
|
|||
|
||||
static inline void spa_alsa_parse_iec958_codecs(uint64_t *codecs, const char *val, size_t len)
|
||||
{
|
||||
struct spa_json it[2];
|
||||
struct spa_json it[1];
|
||||
char v[256];
|
||||
|
||||
spa_json_init(&it[0], val, len);
|
||||
if (spa_json_enter_array(&it[0], &it[1]) <= 0)
|
||||
spa_json_init(&it[1], val, len);
|
||||
if (spa_json_begin_array_relax(&it[0], val, len) <= 0)
|
||||
return;
|
||||
|
||||
*codecs = 0;
|
||||
while (spa_json_get_string(&it[1], v, sizeof(v)) > 0)
|
||||
while (spa_json_get_string(&it[0], v, sizeof(v)) > 0)
|
||||
*codecs |= 1ULL << spa_alsa_iec958_codec_from_name(v);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1830,15 +1830,14 @@ static int do_auto_port_config(struct impl *this, const char *str)
|
|||
bool have_format = false, monitor = false, control = false;
|
||||
struct spa_audio_info format = { 0, };
|
||||
enum spa_param_port_config_mode mode = SPA_PARAM_PORT_CONFIG_MODE_none;
|
||||
struct spa_json it[2];
|
||||
struct spa_json it[1];
|
||||
char key[1024], val[256];
|
||||
|
||||
spa_json_init(&it[0], str, strlen(str));
|
||||
if (spa_json_enter_object(&it[0], &it[1]) <= 0)
|
||||
if (spa_json_begin_object(&it[0], str, strlen(str)) <= 0)
|
||||
return -EINVAL;
|
||||
|
||||
while (spa_json_get_string(&it[1], key, sizeof(key)) > 0) {
|
||||
if (spa_json_get_string(&it[1], val, sizeof(val)) <= 0)
|
||||
while (spa_json_get_string(&it[0], key, sizeof(key)) > 0) {
|
||||
if (spa_json_get_string(&it[0], val, sizeof(val)) <= 0)
|
||||
break;
|
||||
|
||||
if (spa_streq(key, "mode")) {
|
||||
|
|
|
|||
|
|
@ -3425,15 +3425,14 @@ static uint32_t channel_from_name(const char *name)
|
|||
|
||||
static inline uint32_t parse_position(uint32_t *pos, const char *val, size_t len)
|
||||
{
|
||||
struct spa_json it[2];
|
||||
struct spa_json it[1];
|
||||
char v[256];
|
||||
uint32_t i = 0;
|
||||
|
||||
spa_json_init(&it[0], val, len);
|
||||
if (spa_json_enter_array(&it[0], &it[1]) <= 0)
|
||||
spa_json_init(&it[1], val, len);
|
||||
if (spa_json_begin_array_relax(&it[0], val, len) <= 0)
|
||||
return 0;
|
||||
|
||||
while (spa_json_get_string(&it[1], v, sizeof(v)) > 0 &&
|
||||
while (spa_json_get_string(&it[0], v, sizeof(v)) > 0 &&
|
||||
i < SPA_AUDIO_MAX_CHANNELS) {
|
||||
pos[i++] = channel_from_name(v);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -286,15 +286,14 @@ static inline uint32_t spa_avb_channel_from_name(const char *name)
|
|||
|
||||
static inline void spa_avb_parse_position(struct channel_map *map, const char *val, size_t len)
|
||||
{
|
||||
struct spa_json it[2];
|
||||
struct spa_json it[1];
|
||||
char v[256];
|
||||
|
||||
spa_json_init(&it[0], val, len);
|
||||
if (spa_json_enter_array(&it[0], &it[1]) <= 0)
|
||||
spa_json_init(&it[1], val, len);
|
||||
if (spa_json_begin_array_relax(&it[0], val, len) <= 0)
|
||||
return;
|
||||
|
||||
map->channels = 0;
|
||||
while (spa_json_get_string(&it[1], v, sizeof(v)) > 0 &&
|
||||
while (spa_json_get_string(&it[0], v, sizeof(v)) > 0 &&
|
||||
map->channels < SPA_AUDIO_MAX_CHANNELS) {
|
||||
map->pos[map->channels++] = spa_avb_channel_from_name(v);
|
||||
}
|
||||
|
|
@ -302,16 +301,15 @@ static inline void spa_avb_parse_position(struct channel_map *map, const char *v
|
|||
|
||||
static inline uint32_t spa_avb_parse_rates(uint32_t *rates, uint32_t max, const char *val, size_t len)
|
||||
{
|
||||
struct spa_json it[2];
|
||||
struct spa_json it[1];
|
||||
char v[256];
|
||||
uint32_t count;
|
||||
|
||||
spa_json_init(&it[0], val, len);
|
||||
if (spa_json_enter_array(&it[0], &it[1]) <= 0)
|
||||
spa_json_init(&it[1], val, len);
|
||||
if (spa_json_begin_array_relax(&it[0], val, len) <= 0)
|
||||
return 0;
|
||||
|
||||
count = 0;
|
||||
while (spa_json_get_string(&it[1], v, sizeof(v)) > 0 && count < max)
|
||||
while (spa_json_get_string(&it[0], v, sizeof(v)) > 0 && count < max)
|
||||
rates[count++] = atoi(v);
|
||||
return count;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6075,13 +6075,11 @@ impl_get_size(const struct spa_handle_factory *factory,
|
|||
|
||||
int spa_bt_profiles_from_json_array(const char *str)
|
||||
{
|
||||
struct spa_json it, it_array;
|
||||
struct spa_json it_array;
|
||||
char role_name[256];
|
||||
enum spa_bt_profile profiles = SPA_BT_PROFILE_NULL;
|
||||
|
||||
spa_json_init(&it, str, strlen(str));
|
||||
|
||||
if (spa_json_enter_array(&it, &it_array) <= 0)
|
||||
if (spa_json_begin_array(&it_array, str, strlen(str)) <= 0)
|
||||
return -EINVAL;
|
||||
|
||||
while (spa_json_get_string(&it_array, role_name, sizeof(role_name)) > 0) {
|
||||
|
|
@ -6144,7 +6142,7 @@ static void parse_broadcast_source_config(struct spa_bt_monitor *monitor, const
|
|||
char bcode[BROADCAST_CODE_LEN + 3];
|
||||
int cursor;
|
||||
int big_id = 0;
|
||||
struct spa_json it[4], it_array[4];
|
||||
struct spa_json it[3], it_array[4];
|
||||
struct spa_list big_list = SPA_LIST_INIT(&big_list);
|
||||
struct spa_error_location loc;
|
||||
struct spa_bt_big *big;
|
||||
|
|
@ -6153,14 +6151,12 @@ static void parse_broadcast_source_config(struct spa_bt_monitor *monitor, const
|
|||
if (!(info && (str = spa_dict_lookup(info, "bluez5.bcast_source.config"))))
|
||||
return;
|
||||
|
||||
spa_json_init(&it[0], str, strlen(str));
|
||||
|
||||
/* Verify is an array of BIGS */
|
||||
if (spa_json_enter_array(&it[0], &it_array[0]) <= 0)
|
||||
if (spa_json_begin_array(&it_array[0], str, strlen(str)) <= 0)
|
||||
goto parse_failed;
|
||||
|
||||
/* Iterate on all BIG objects */
|
||||
while (spa_json_enter_object(&it_array[0], &it[1]) > 0) {
|
||||
while (spa_json_enter_object(&it_array[0], &it[0]) > 0) {
|
||||
struct spa_bt_big *big_entry = calloc(1, sizeof(struct spa_bt_big));
|
||||
|
||||
if (!big_entry)
|
||||
|
|
@ -6171,22 +6167,22 @@ static void parse_broadcast_source_config(struct spa_bt_monitor *monitor, const
|
|||
spa_list_append(&big_list, &big_entry->link);
|
||||
|
||||
/* Iterate on all BIG values */
|
||||
while (spa_json_get_string(&it[1], key, sizeof(key)) > 0) {
|
||||
while (spa_json_get_string(&it[0], key, sizeof(key)) > 0) {
|
||||
if (spa_streq(key, "broadcast_code")) {
|
||||
if (spa_json_get_string(&it[1], bcode, sizeof(bcode)) <= 0)
|
||||
if (spa_json_get_string(&it[0], bcode, sizeof(bcode)) <= 0)
|
||||
goto parse_failed;
|
||||
if (strlen(bcode) > BROADCAST_CODE_LEN)
|
||||
goto parse_failed;
|
||||
memcpy(big_entry->broadcast_code, bcode, strlen(bcode));
|
||||
spa_log_debug(monitor->log, "big_entry->broadcast_code %s", big_entry->broadcast_code);
|
||||
} else if (spa_streq(key, "encryption")) {
|
||||
if (spa_json_get_bool(&it[1], &big_entry->encryption) <= 0)
|
||||
if (spa_json_get_bool(&it[0], &big_entry->encryption) <= 0)
|
||||
goto parse_failed;
|
||||
spa_log_debug(monitor->log, "big_entry->encryption %d", big_entry->encryption);
|
||||
} else if (spa_streq(key, "bis")) {
|
||||
if (spa_json_enter_array(&it[1], &it_array[1]) <= 0)
|
||||
if (spa_json_enter_array(&it[0], &it_array[1]) <= 0)
|
||||
goto parse_failed;
|
||||
while (spa_json_enter_object(&it_array[1], &it[2]) > 0) {
|
||||
while (spa_json_enter_object(&it_array[1], &it[1]) > 0) {
|
||||
/* Iterate on all BIS values */
|
||||
struct spa_bt_bis *bis_entry = calloc(1, sizeof(struct spa_bt_bis));
|
||||
|
||||
|
|
@ -6196,19 +6192,19 @@ static void parse_broadcast_source_config(struct spa_bt_monitor *monitor, const
|
|||
spa_list_init(&bis_entry->metadata_list);
|
||||
spa_list_append(&big_entry->bis_list, &bis_entry->link);
|
||||
|
||||
while (spa_json_get_string(&it[2], bis_key, sizeof(bis_key)) > 0) {
|
||||
while (spa_json_get_string(&it[1], bis_key, sizeof(bis_key)) > 0) {
|
||||
if (spa_streq(bis_key, "qos_preset")) {
|
||||
if (spa_json_get_string(&it[2], bis_entry->qos_preset, sizeof(bis_entry->qos_preset)) <= 0)
|
||||
if (spa_json_get_string(&it[1], bis_entry->qos_preset, sizeof(bis_entry->qos_preset)) <= 0)
|
||||
goto parse_failed;
|
||||
spa_log_debug(monitor->log, "bis_entry->qos_preset %s", bis_entry->qos_preset);
|
||||
} else if (spa_streq(bis_key, "audio_channel_allocation")) {
|
||||
if (spa_json_get_int(&it[2], &bis_entry->channel_allocation) <= 0)
|
||||
if (spa_json_get_int(&it[1], &bis_entry->channel_allocation) <= 0)
|
||||
goto parse_failed;
|
||||
spa_log_debug(monitor->log, "bis_entry->channel_allocation %d", bis_entry->channel_allocation);
|
||||
} else if (spa_streq(bis_key, "metadata")) {
|
||||
if (spa_json_enter_array(&it[2], &it_array[2]) <= 0)
|
||||
if (spa_json_enter_array(&it[1], &it_array[2]) <= 0)
|
||||
goto parse_failed;
|
||||
while (spa_json_enter_object(&it_array[2], &it[3]) > 0) {
|
||||
while (spa_json_enter_object(&it_array[2], &it[2]) > 0) {
|
||||
struct spa_bt_metadata *metadata_entry = calloc(1, sizeof(struct spa_bt_metadata));
|
||||
|
||||
if (!metadata_entry)
|
||||
|
|
@ -6216,13 +6212,13 @@ static void parse_broadcast_source_config(struct spa_bt_monitor *monitor, const
|
|||
|
||||
spa_list_append(&bis_entry->metadata_list, &metadata_entry->link);
|
||||
|
||||
while (spa_json_get_string(&it[3], qos_key, sizeof(qos_key)) > 0) {
|
||||
while (spa_json_get_string(&it[2], qos_key, sizeof(qos_key)) > 0) {
|
||||
if (spa_streq(qos_key, "type")) {
|
||||
if (spa_json_get_int(&it[3], &metadata_entry->type) <= 0)
|
||||
if (spa_json_get_int(&it[2], &metadata_entry->type) <= 0)
|
||||
goto parse_failed;
|
||||
spa_log_debug(monitor->log, "metadata_entry->type %d", metadata_entry->type);
|
||||
} else if (spa_streq(qos_key, "value")) {
|
||||
if (spa_json_enter_array(&it[3], &it_array[3]) <= 0)
|
||||
if (spa_json_enter_array(&it[2], &it_array[3]) <= 0)
|
||||
goto parse_failed;
|
||||
for (cursor = 0; cursor < METADATA_MAX_LEN - 1; cursor++) {
|
||||
int temp_val = 0;
|
||||
|
|
@ -6254,7 +6250,7 @@ errno_failed:
|
|||
|
||||
parse_failed:
|
||||
str = spa_dict_lookup(info, "bluez5.bcast_source.config");
|
||||
if (spa_json_get_error(&it[0], str, &loc)) {
|
||||
if (spa_json_get_error(&it_array[0], str, &loc)) {
|
||||
spa_debug_log_error_location(monitor->log, SPA_LOG_LEVEL_WARN,
|
||||
&loc, "malformed bluez5.bcast_source.config: %s", loc.reason);
|
||||
} else {
|
||||
|
|
@ -6272,7 +6268,7 @@ static int parse_codec_array(struct spa_bt_monitor *this, const struct spa_dict
|
|||
const struct media_codec * const * const media_codecs = this->media_codecs;
|
||||
const char *str;
|
||||
struct spa_dict_item *codecs;
|
||||
struct spa_json it, it_array;
|
||||
struct spa_json it_array;
|
||||
char codec_name[256];
|
||||
size_t num_codecs;
|
||||
int i;
|
||||
|
|
@ -6290,9 +6286,7 @@ static int parse_codec_array(struct spa_bt_monitor *this, const struct spa_dict
|
|||
if (info == NULL || (str = spa_dict_lookup(info, "bluez5.codecs")) == NULL)
|
||||
goto fallback;
|
||||
|
||||
spa_json_init(&it, str, strlen(str));
|
||||
|
||||
if (spa_json_enter_array(&it, &it_array) <= 0) {
|
||||
if (spa_json_begin_array(&it_array, str, strlen(str)) <= 0) {
|
||||
spa_log_error(this->log, "property bluez5.codecs '%s' is not an array", str);
|
||||
goto fallback;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -881,15 +881,14 @@ static uint32_t channel_from_name(const char *name)
|
|||
|
||||
static inline void parse_position(struct impl *this, const char *val, size_t len)
|
||||
{
|
||||
struct spa_json it[2];
|
||||
struct spa_json it[1];
|
||||
char v[256];
|
||||
|
||||
spa_json_init(&it[0], val, len);
|
||||
if (spa_json_enter_array(&it[0], &it[1]) <= 0)
|
||||
spa_json_init(&it[1], val, len);
|
||||
if (spa_json_begin_array_relax(&it[0], val, len) <= 0)
|
||||
return;
|
||||
|
||||
this->props.channels = 0;
|
||||
while (spa_json_get_string(&it[1], v, sizeof(v)) > 0 &&
|
||||
while (spa_json_get_string(&it[0], v, sizeof(v)) > 0 &&
|
||||
this->props.channels < SPA_AUDIO_MAX_CHANNELS) {
|
||||
this->props.pos[this->props.channels++] = channel_from_name(v);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue