PipeWire: spa/param/audio/raw-json.h Source File

PipeWire 1.4.2 |

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