+ +
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 © 2023 Wim Taymans */ |
| 3 | /* SPDX-License-Identifier: MIT */ |
| 4 | |
| 5 | #ifndef SPA_PARAM_TAG_UTILS_H |
| 6 | #define SPA_PARAM_TAG_UTILS_H |
| 7 | |
| 8 | #include <float.h> |
| 9 | |
| 10 | #include <spa/utils/dict.h> |
| 11 | #include <spa/pod/builder.h> |
| 12 | #include <spa/pod/iter.h> |
| 13 | #include <spa/pod/parser.h> |
| 14 | #include <spa/pod/compare.h> |
| 15 | #include <spa/param/tag.h> |
| 16 | |
| 17 | #ifdef __cplusplus |
| 18 | extern "C" { |
| 19 | #endif |
| 20 | |
| 21 | /** |
| 22 | * \addtogroup spa_param |
| 23 | * \{ |
| 24 | */ |
| 25 | |
| 26 | #ifndef SPA_API_TAG_UTILS |
| 27 | #ifdef SPA_API_IMPL |
| 28 | #define SPA_API_TAG_UTILS SPA_API_IMPL |
| 29 | #else |
| 30 | #define SPA_API_TAG_UTILS static inline |
| 31 | #endif |
| 32 | #endif |
| 33 | |
| 34 | SPA_API_TAG_UTILS int |
| 35 | spa_tag_compare(const struct spa_pod *a, const struct spa_pod *b) |
| 36 | { |
| 37 | return spa_pod_memcmp(a, b); |
| 38 | } |
| 39 | |
| 40 | SPA_API_TAG_UTILS int |
| 41 | spa_tag_parse(const struct spa_pod *tag, struct spa_tag_info *info, void **state) |
| 42 | { |
| 43 | int res; |
| 44 | const struct spa_pod_object *obj = (const struct spa_pod_object*)tag; |
| 45 | const struct spa_pod_prop *first, *start, *cur; |
| 46 | |
| 47 | spa_zero(*info); |
| 48 | |
| 49 | if ((res = spa_pod_parse_object(tag, |
| 50 | SPA_TYPE_OBJECT_ParamTag, NULL, |
| 51 | SPA_PARAM_TAG_direction, SPA_POD_Id(&info->direction))) < 0) |
| 52 | return res; |
| 53 | |
| 54 | first = spa_pod_prop_first(&obj->body); |
| 55 | start = *state ? spa_pod_prop_next((struct spa_pod_prop*)*state) : first; |
| 56 | |
| 57 | res = 0; |
| 58 | for (cur = start; spa_pod_prop_is_inside(&obj->body, obj->pod.size, cur); |
| 59 | cur = spa_pod_prop_next(cur)) { |
| 60 | if (cur->key == SPA_PARAM_TAG_info) { |
| 61 | info->info = &cur->value; |
| 62 | *state = (void*)cur; |
| 63 | return 1; |
| 64 | } |
| 65 | } |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | SPA_API_TAG_UTILS int |
| 70 | spa_tag_info_parse(const struct spa_tag_info *info, struct spa_dict *dict, struct spa_dict_item *items) |
| 71 | { |
| 72 | struct spa_pod_parser prs; |
| 73 | uint32_t n, n_items; |
| 74 | const char *key, *value; |
| 75 | struct spa_pod_frame f[1]; |
| 76 | |
| 77 | spa_pod_parser_pod(&prs, info->info); |
| 78 | if (spa_pod_parser_push_struct(&prs, &f[0]) < 0 || |
| 79 | spa_pod_parser_get_int(&prs, (int32_t*)&n_items) < 0) |
| 80 | return -EINVAL; |
| 81 | |
| 82 | if (items == NULL) { |
| 83 | dict->n_items = n_items; |
| 84 | return 0; |
| 85 | } |
| 86 | n_items = SPA_MIN(dict->n_items, n_items); |
| 87 | |
| 88 | for (n = 0; n < n_items; n++) { |
| 89 | if (spa_pod_parser_get(&prs, |
| 90 | SPA_POD_String(&key), |
| 91 | SPA_POD_String(&value), |
| 92 | NULL) < 0) |
| 93 | break; |
| 94 | if (key == NULL || value == NULL) |
| 95 | return -EINVAL; |
| 96 | items[n].key = key; |
| 97 | items[n].value = value; |
| 98 | } |
| 99 | *dict = SPA_DICT_INIT(items, n); |
| 100 | spa_pod_parser_pop(&prs, &f[0]); |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | SPA_API_TAG_UTILS void |
| 105 | spa_tag_build_start(struct spa_pod_builder *builder, struct spa_pod_frame *f, |
| 106 | uint32_t id, enum spa_direction direction) |
| 107 | { |
| 108 | spa_pod_builder_push_object(builder, f, SPA_TYPE_OBJECT_ParamTag, id); |
| 109 | spa_pod_builder_add(builder, |
| 110 | SPA_PARAM_TAG_direction, SPA_POD_Id(direction), |
| 111 | 0); |
| 112 | } |
| 113 | |
| 114 | SPA_API_TAG_UTILS void |
| 115 | spa_tag_build_add_info(struct spa_pod_builder *builder, const struct spa_pod *info) |
| 116 | { |
| 117 | spa_pod_builder_add(builder, |
| 118 | SPA_PARAM_TAG_info, SPA_POD_Pod(info), |
| 119 | 0); |
| 120 | } |
| 121 | |
| 122 | SPA_API_TAG_UTILS void |
| 123 | spa_tag_build_add_dict(struct spa_pod_builder *builder, const struct spa_dict *dict) |
| 124 | { |
| 125 | uint32_t i, n_items; |
| 126 | struct spa_pod_frame f; |
| 127 | |
| 128 | n_items = dict ? dict->n_items : 0; |
| 129 | |
| 130 | spa_pod_builder_prop(builder, SPA_PARAM_TAG_info, SPA_POD_PROP_FLAG_HINT_DICT); |
| 131 | spa_pod_builder_push_struct(builder, &f); |
| 132 | spa_pod_builder_int(builder, n_items); |
| 133 | for (i = 0; i < n_items; i++) { |
| 134 | spa_pod_builder_string(builder, dict->items[i].key); |
| 135 | spa_pod_builder_string(builder, dict->items[i].value); |
| 136 | } |
| 137 | spa_pod_builder_pop(builder, &f); |
| 138 | } |
| 139 | |
| 140 | SPA_API_TAG_UTILS struct spa_pod * |
| 141 | spa_tag_build_end(struct spa_pod_builder *builder, struct spa_pod_frame *f) |
| 142 | { |
| 143 | return (struct spa_pod*)spa_pod_builder_pop(builder, f); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * \} |
| 148 | */ |
| 149 | |
| 150 | #ifdef __cplusplus |
| 151 | } /* extern "C" */ |
| 152 | #endif |
| 153 | |
| 154 | #endif /* SPA_PARAM_TAG_UTILS_H */ |