PipeWire 1.6.8

Loading...
Searching...
No Matches
raw-json.h
Go to the documentation of this file.
1/* Simple Plugin API */
2/* SPDX-FileCopyrightText: Copyright © 2024 Wim Taymans */
3/* SPDX-License-Identifier: MIT */
4
5#ifndef SPA_AUDIO_RAW_JSON_H
6#define SPA_AUDIO_RAW_JSON_H
7
8#include <spa/utils/dict.h>
9#include <spa/utils/json.h>
10#include <spa/param/audio/raw.h>
11#include <spa/param/audio/raw-types.h>
12#include <spa/param/audio/layout-types.h>
13
14#ifdef __cplusplus
15 extern "C" {
16#endif
17
22
23#ifndef SPA_API_AUDIO_RAW_JSON
24 #ifdef SPA_API_IMPL
25 #define SPA_API_AUDIO_RAW_JSON SPA_API_IMPL
26 #else
27 #define SPA_API_AUDIO_RAW_JSON static inline
28 #endif
29#endif
30
31 SPA_API_AUDIO_RAW_JSON int
32 spa_audio_parse_position_n(const char *str, size_t len,
33 uint32_t *position, uint32_t max_position, uint32_t *n_channels)
34{
35 struct spa_json iter;
36 char v[256];
37 uint32_t channels = 0;
38
39 if (spa_json_begin_array_relax(&iter, str, len) <= 0)
40 return 0;
41
42 while (spa_json_get_string(&iter, v, sizeof(v)) > 0) {
43 if (channels < max_position)
44 position[channels] = spa_type_audio_channel_from_short_name(v);
45 channels++;
46 }
47 *n_channels = channels;
48 return channels;
49}
50
51 SPA_API_AUDIO_RAW_JSON int
52 spa_audio_parse_position(const char *str, size_t len,
53 uint32_t *position, uint32_t *n_channels)
54{
55 return spa_audio_parse_position_n(str, len, position, SPA_AUDIO_MAX_CHANNELS, n_channels);
56}
57
58 SPA_API_AUDIO_RAW_JSON int
59 spa_audio_parse_layout(const char *str, uint32_t *position, uint32_t max_position,
60 uint32_t *n_channels)
61{
62 struct spa_audio_layout_info l;
63 uint32_t i;
64 if (spa_audio_layout_info_parse_name(&l, sizeof(l), str) <= 0)
65 return 0;
66 for (i = 0; i < l.n_channels && i < max_position; i++)
67 position[i] = l.position[i];
68 *n_channels = l.n_channels;
69 return l.n_channels;
70}
71
72 SPA_API_AUDIO_RAW_JSON int
73 spa_audio_info_raw_ext_update(struct spa_audio_info_raw *info, size_t size,
74 const char *key, const char *val, bool force)
75{
76 uint32_t v;
77 uint32_t max_position = SPA_AUDIO_INFO_RAW_MAX_POSITION(size);
78
79 if (!SPA_AUDIO_INFO_RAW_VALID_SIZE(size))
80 return -EINVAL;
81
82 if (spa_streq(key, SPA_KEY_AUDIO_FORMAT)) {
83 if (force || info->format == 0)
84 info->format = (enum spa_audio_format)spa_type_audio_format_from_short_name(val);
85 } else if (spa_streq(key, SPA_KEY_AUDIO_RATE)) {
86 if (spa_atou32(val, &v, 0) && (force || info->rate == 0))
87 info->rate = v;
88 } else if (spa_streq(key, SPA_KEY_AUDIO_CHANNELS)) {
89 if (spa_atou32(val, &v, 0) && (force || info->channels == 0)) {
90 if (v > max_position)
91 return -ECHRNG;
92 info->channels = v;
93 }
94 } else if (spa_streq(key, SPA_KEY_AUDIO_LAYOUT)) {
95 if (force || info->channels == 0) {
96 if (spa_audio_parse_layout(val, info->position, max_position, &v) > 0) {
97 if (v > max_position)
98 return -ECHRNG;
99 info->channels = v;
100 SPA_FLAG_CLEAR(info->flags, SPA_AUDIO_FLAG_UNPOSITIONED);
101 }
102 }
103 } else if (spa_streq(key, SPA_KEY_AUDIO_POSITION)) {
104 if (force || info->channels == 0) {
105 if (spa_audio_parse_position_n(val, strlen(val), info->position,
106 max_position, &v) > 0) {
107 if (v > max_position)
108 return -ECHRNG;
109 info->channels = v;
110 SPA_FLAG_CLEAR(info->flags, SPA_AUDIO_FLAG_UNPOSITIONED);
111 }
112 }
113 }
114 return 0;
115}
116
117 SPA_API_AUDIO_RAW_JSON int
118 spa_audio_info_raw_update(struct spa_audio_info_raw *info,
119 const char *key, const char *val, bool force)
120{
121 return spa_audio_info_raw_ext_update(info, sizeof(*info), key, val, force);
122}
123
124 SPA_API_AUDIO_RAW_JSON int
125 spa_audio_info_raw_ext_init_dict_keys_va(struct spa_audio_info_raw *info, size_t size,
126 const struct spa_dict *defaults,
127 const struct spa_dict *dict, va_list args)
128{
129 int res;
130
131 if (!SPA_AUDIO_INFO_RAW_VALID_SIZE(size))
132 return -EINVAL;
133
134 memset(info, 0, size);
135 SPA_FLAG_SET(info->flags, SPA_AUDIO_FLAG_UNPOSITIONED);
136 if (dict) {
137 const char *val, *key;
138 while ((key = va_arg(args, const char *))) {
139 if ((val = spa_dict_lookup(dict, key)) == NULL)
140 continue;
141 if ((res = spa_audio_info_raw_ext_update(info, size,
142 key, val, true)) < 0)
143 return res;
144 }
145 }
146 if (defaults) {
147 const struct spa_dict_item *it;
148 spa_dict_for_each(it, defaults)
149 if ((res = spa_audio_info_raw_ext_update(info, size,
150 it->key, it->value, false)) < 0)
151 return res;
152 }
153 return 0;
154}
155
156 SPA_API_AUDIO_RAW_JSON int SPA_SENTINEL
157 spa_audio_info_raw_ext_init_dict_keys(struct spa_audio_info_raw *info, size_t size,
158 const struct spa_dict *defaults,
159 const struct spa_dict *dict, ...)
160{
161 va_list args;
162 int res;
163 va_start(args, dict);
164 res = spa_audio_info_raw_ext_init_dict_keys_va(info, size, defaults, dict, args);
165 va_end(args);
166 return res;
167}
168
169 SPA_API_AUDIO_RAW_JSON int SPA_SENTINEL
170 spa_audio_info_raw_init_dict_keys(struct spa_audio_info_raw *info,
171 const struct spa_dict *defaults,
172 const struct spa_dict *dict, ...)
173{
174 va_list args;
175 int res;
176 va_start(args, dict);
177 res = spa_audio_info_raw_ext_init_dict_keys_va(info, sizeof(*info), defaults, dict, args);
178 va_end(args);
179 return res;
180}
181
185
186#ifdef __cplusplus
187} /* extern "C" */
188#endif
189
190#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