PipeWire 1.3.0
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#ifdef __cplusplus
9extern "C" {
10#endif
11
17#include <spa/utils/dict.h>
18#include <spa/utils/json.h>
19#include <spa/param/audio/raw.h>
21
22static inline int
23spa_audio_parse_position(const char *str, size_t len,
24 uint32_t *position, uint32_t *n_channels)
25{
26 struct spa_json iter;
27 char v[256];
28 uint32_t channels = 0;
29
30 if (spa_json_begin_array_relax(&iter, str, len) <= 0)
31 return 0;
32
33 while (spa_json_get_string(&iter, v, sizeof(v)) > 0 &&
34 channels < SPA_AUDIO_MAX_CHANNELS) {
35 position[channels++] = spa_type_audio_channel_from_short_name(v);
36 }
37 *n_channels = channels;
38 return channels;
39}
40
41static inline int
42spa_audio_info_raw_update(struct spa_audio_info_raw *info, const char *key, const char *val, bool force)
43{
44 uint32_t v;
46 if (force || info->format == 0)
48 } else if (spa_streq(key, SPA_KEY_AUDIO_RATE)) {
49 if (spa_atou32(val, &v, 0) && (force || info->rate == 0))
50 info->rate = v;
51 } else if (spa_streq(key, SPA_KEY_AUDIO_CHANNELS)) {
52 if (spa_atou32(val, &v, 0) && (force || info->channels == 0))
53 info->channels = SPA_MIN(v, SPA_AUDIO_MAX_CHANNELS);
54 } else if (spa_streq(key, SPA_KEY_AUDIO_POSITION)) {
55 if (force || info->channels == 0)
56 spa_audio_parse_position(val, strlen(val), info->position, &info->channels);
57 }
58 return 0;
59}
60
61static inline int SPA_SENTINEL
63 const struct spa_dict *defaults,
64 const struct spa_dict *dict, ...)
65{
66 spa_zero(*info);
67 if (dict) {
68 const char *val, *key;
69 va_list args;
70 va_start(args, dict);
71 while ((key = va_arg(args, const char *))) {
72 if ((val = spa_dict_lookup(dict, key)) == NULL)
73 continue;
74 spa_audio_info_raw_update(info, key, val, true);
75 }
76 va_end(args);
77 }
78 if (defaults) {
79 const struct spa_dict_item *it;
80 spa_dict_for_each(it, defaults)
81 spa_audio_info_raw_update(info, it->key, it->value, false);
82 }
83 return 0;
84}
85
90#ifdef __cplusplus
91} /* extern "C" */
92#endif
93
94#endif /* SPA_AUDIO_RAW_JSON_H */
spa/param/audio/raw-types.h
spa/param/audio/raw.h
static const char * spa_dict_lookup(const struct spa_dict *dict, const char *key)
Definition dict.h:104
#define spa_dict_for_each(item, dict)
Definition dict.h:61
static int spa_json_get_string(struct spa_json *iter, char *res, int maxlen)
Definition json.h:73
static int spa_json_begin_array_relax(struct spa_json *iter, const char *data, size_t size)
Definition json.h:184
static int spa_audio_parse_position(const char *str, size_t len, uint32_t *position, uint32_t *n_channels)
Definition raw-json.h:28
static uint32_t spa_type_audio_format_from_short_name(const char *name)
Definition raw-types.h:139
static 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:67
#define SPA_KEY_AUDIO_POSITION
channel positions as comma separated list of channels ex.
Definition raw.h:304
static uint32_t spa_type_audio_channel_from_short_name(const char *name)
Definition raw-types.h:271
#define SPA_KEY_AUDIO_RATE
an audio sample rate as int
Definition raw.h:302
#define SPA_KEY_AUDIO_CHANNELS
an audio channel count as int
Definition raw.h:300
#define SPA_KEY_AUDIO_FORMAT
an audio format as string, Ex.
Definition raw.h:294
spa_audio_format
Definition raw.h:29
#define SPA_AUDIO_MAX_CHANNELS
Definition raw.h:27
static int spa_audio_info_raw_update(struct spa_audio_info_raw *info, const char *key, const char *val, bool force)
Definition raw-json.h:47
static 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:128
static bool spa_streq(const char *s1, const char *s2)
Definition string.h:41
#define SPA_MIN(a, b)
Definition defs.h:165
#define spa_zero(x)
Definition defs.h:483
#define SPA_SENTINEL
Definition defs.h:305
spa/utils/json.h
Audio information description.
Definition raw.h:282
Definition dict.h:31
Definition dict.h:41
Definition json-core.h:38
spa/utils/dict.h