PipeWire  1.4.1
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
9 extern "C" {
10 #endif
11 
17 #include <spa/utils/dict.h>
18 #include <spa/utils/json.h>
19 #include <spa/param/audio/raw.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 
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 
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)
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))
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)
66  }
67  }
68  return 0;
69 }
70 
73  const struct spa_dict *defaults,
74  const struct spa_dict *dict, ...)
75 {
76  spa_zero(*info);
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 
101 #ifdef __cplusplus
102 } /* extern "C" */
103 #endif
104 
105 #endif /* SPA_AUDIO_RAW_JSON_H */
spa/param/audio/raw-types.h
spa/param/audio/raw.h
uint32_t int int const char va_list args
Definition: core.h:434
va_end(args)
va_start(args, message)
SPA_API_DICT const char * spa_dict_lookup(const struct spa_dict *dict, const char *key)
Definition: dict.h:114
#define spa_dict_for_each(item, dict)
Definition: dict.h:71
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_API_JSON_UTILS int spa_json_get_string(struct spa_json *iter, char *res, int maxlen)
Definition: json.h:83
SPA_API_AUDIO_RAW_TYPES uint32_t spa_type_audio_format_from_short_name(const char *name)
Definition: raw-types.h:149
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
#define SPA_KEY_AUDIO_POSITION
channel positions as comma separated list of channels ex.
Definition: raw.h:304
#define SPA_AUDIO_FLAG_UNPOSITIONED
Definition: raw.h:280
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
#define SPA_KEY_AUDIO_RATE
an audio sample rate as int
Definition: raw.h:302
SPA_API_AUDIO_RAW_TYPES uint32_t spa_type_audio_channel_from_short_name(const char *name)
Definition: raw-types.h:281
#define SPA_KEY_AUDIO_CHANNELS
an audio channel count as int
Definition: raw.h:300
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
#define SPA_API_AUDIO_RAW_JSON
Definition: raw-json.h:33
#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
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_API_STRING bool spa_streq(const char *s1, const char *s2)
Definition: string.h:51
#define SPA_FLAG_SET(field, flag)
Definition: defs.h:93
#define SPA_MIN(a, b)
Definition: defs.h:165
#define spa_zero(x)
Definition: defs.h:508
#define SPA_FLAG_CLEAR(field, flag)
Definition: defs.h:94
#define SPA_SENTINEL
Definition: defs.h:305
spa/utils/json.h
Audio information description.
Definition: raw.h:282
enum spa_audio_format format
Definition: raw.h:283
uint32_t channels
Definition: raw.h:286
uint32_t flags
Definition: raw.h:284
uint32_t position[SPA_AUDIO_MAX_CHANNELS]
Definition: raw.h:287
uint32_t rate
Definition: raw.h:285
Definition: dict.h:41
Definition: dict.h:51
Definition: json-core.h:48
spa/utils/dict.h