json: add helper function to parse channel positions

Use the helper instead of duplicating the same code.

Also add some helpers to parse a json array of uint32_t

Move some functions to convert between type name and id.
This commit is contained in:
Wim Taymans 2024-09-18 09:54:34 +02:00
parent 911a601b95
commit e2991f6398
34 changed files with 256 additions and 791 deletions

View file

@ -33,6 +33,14 @@ static const struct spa_type_info spa_type_audio_iec958_codec[] = {
{ 0, 0, NULL, NULL },
};
static inline uint32_t spa_type_audio_iec958_codec_from_short_name(const char *name)
{
return spa_type_from_short_name(name, spa_type_audio_iec958_codec, SPA_AUDIO_IEC958_CODEC_UNKNOWN);
}
static inline const char * spa_type_audio_iec958_codec_to_short_name(uint32_t type)
{
return spa_type_to_short_name(type, spa_type_audio_iec958_codec, "UNKNOWN");
}
/**
* \}
*/

View file

@ -0,0 +1,47 @@
/* 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
#ifdef __cplusplus
extern "C" {
#endif
/**
* \addtogroup spa_param
* \{
*/
#include <spa/utils/json.h>
#include <spa/param/audio/raw-types.h>
static inline int
spa_audio_parse_position(const char *str, size_t len,
uint32_t *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 &&
channels < SPA_AUDIO_MAX_CHANNELS) {
position[channels++] = spa_type_audio_channel_from_short_name(v);
}
*n_channels = channels;
return channels;
}
/**
* \}
*/
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* SPA_AUDIO_RAW_JSON_H */

View file

@ -129,6 +129,15 @@ static const struct spa_type_info spa_type_audio_format[] = {
{ 0, 0, NULL, NULL },
};
static inline uint32_t spa_type_audio_format_from_short_name(const char *name)
{
return spa_type_from_short_name(name, spa_type_audio_format, SPA_AUDIO_FORMAT_UNKNOWN);
}
static inline const char * spa_type_audio_format_to_short_name(uint32_t type)
{
return spa_type_to_short_name(type, spa_type_audio_format, "UNKNOWN");
}
#define SPA_TYPE_INFO_AudioFlags SPA_TYPE_INFO_FLAGS_BASE "AudioFlags"
#define SPA_TYPE_INFO_AUDIO_FLAGS_BASE SPA_TYPE_INFO_AudioFlags ":"
@ -250,12 +259,11 @@ static const struct spa_type_info spa_type_audio_channel[] = {
static inline uint32_t spa_type_audio_channel_from_short_name(const char *name)
{
int i;
for (i = 0; spa_type_audio_channel[i].name; i++) {
if (spa_streq(name, spa_type_short_name(spa_type_audio_channel[i].name)))
return spa_type_audio_channel[i].type;
}
return SPA_AUDIO_CHANNEL_UNKNOWN;
return spa_type_from_short_name(name, spa_type_audio_channel, SPA_AUDIO_CHANNEL_UNKNOWN);
}
static inline const char * spa_type_audio_channel_to_short_name(uint32_t type)
{
return spa_type_to_short_name(type, spa_type_audio_channel, "UNK");
}

View file

@ -20,7 +20,8 @@ extern "C" {
#define SPA_TYPE_INFO_VIDEO_FORMAT_BASE SPA_TYPE_INFO_VideoFormat ":"
static const struct spa_type_info spa_type_video_format[] = {
{ SPA_VIDEO_FORMAT_ENCODED, SPA_TYPE_Int, SPA_TYPE_INFO_VIDEO_FORMAT_BASE "encoded", NULL },
{ SPA_VIDEO_FORMAT_UNKNOWN, SPA_TYPE_Int, SPA_TYPE_INFO_VIDEO_FORMAT_BASE "UNKNOWN", NULL },
{ SPA_VIDEO_FORMAT_ENCODED, SPA_TYPE_Int, SPA_TYPE_INFO_VIDEO_FORMAT_BASE "ENCODED", NULL },
{ SPA_VIDEO_FORMAT_I420, SPA_TYPE_Int, SPA_TYPE_INFO_VIDEO_FORMAT_BASE "I420", NULL },
{ SPA_VIDEO_FORMAT_YV12, SPA_TYPE_Int, SPA_TYPE_INFO_VIDEO_FORMAT_BASE "YV12", NULL },
{ SPA_VIDEO_FORMAT_YUY2, SPA_TYPE_Int, SPA_TYPE_INFO_VIDEO_FORMAT_BASE "YUY2", NULL },
@ -110,6 +111,15 @@ static const struct spa_type_info spa_type_video_format[] = {
{ 0, 0, NULL, NULL },
};
static inline uint32_t spa_type_video_format_from_short_name(const char *name)
{
return spa_type_from_short_name(name, spa_type_video_format, SPA_VIDEO_FORMAT_UNKNOWN);
}
static inline const char * spa_type_video_format_to_short_name(uint32_t type)
{
return spa_type_to_short_name(type, spa_type_video_format, "UNKNOWN");
}
#define SPA_TYPE_INFO_VideoFlags SPA_TYPE_INFO_FLAGS_BASE "VideoFlags"
#define SPA_TYPE_INFO_VIDEO_FLAGS_BASE SPA_TYPE_INFO_VideoFlags ":"

View file

@ -185,6 +185,24 @@ static inline int spa_json_begin_array(struct spa_json * iter, const char *data,
return spa_json_begin_container(iter, data, size, '[', false);
}
#define spa_json_make_str_array_unpack(maxlen,type,conv) \
{ \
struct spa_json iter; \
char v[maxlen]; \
uint32_t count = 0; \
if (spa_json_begin_array_relax(&iter, arr, arr_len) <= 0) \
return -EINVAL; \
while (spa_json_get_string(&iter, v, sizeof(v)) > 0 && count < max) \
values[count++] = conv(v); \
return count; \
}
static inline int spa_json_str_array_uint32(const char *arr, size_t arr_len,
uint32_t *values, size_t max)
{
spa_json_make_str_array_unpack(32,uint32_t, atoi);
}
/**
* \}
*/

View file

@ -10,6 +10,7 @@ extern "C" {
#endif
#include <spa/utils/defs.h>
#include <spa/utils/string.h>
/** \defgroup spa_types Types
* Data type information enumerations
@ -136,6 +137,34 @@ static inline const char *spa_type_short_name(const char *name)
return name;
}
static inline uint32_t spa_type_from_short_name(const char *name,
const struct spa_type_info *info, uint32_t unknown)
{
int i;
for (i = 0; info[i].name; i++) {
if (spa_streq(name, spa_type_short_name(info[i].name)))
return info[i].type;
}
return unknown;
}
static inline const char * spa_type_to_name(uint32_t type,
const struct spa_type_info *info, const char *unknown)
{
int i;
for (i = 0; info[i].name; i++) {
if (info[i].type == type)
return info[i].name;
}
return unknown;
}
static inline const char * spa_type_to_short_name(uint32_t type,
const struct spa_type_info *info, const char *unknown)
{
const char *n = spa_type_to_name(type, info, unknown);
return n ? spa_type_short_name(n) : NULL;
}
/**
* \}
*/