+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
saifmukhtar · 2026-11-24
main
| 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 | |
| 18 | /** |
| 19 | * \addtogroup spa_param |
| 20 | * \{ |
| 21 | */ |
| 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 | |
| 182 | /** |
| 183 | * \} |
| 184 | */ |
| 185 | |
| 186 | #ifdef __cplusplus |
| 187 | } /* extern "C" */ |
| 188 | #endif |
| 189 | |
| 190 | #endif /* SPA_AUDIO_RAW_JSON_H */ |