PipeWire 1.4.1
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
tag-utils.h
Go to the documentation of this file.
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#ifdef __cplusplus
9extern "C" {
10#endif
11
17#include <float.h>
18
19#include <spa/utils/dict.h>
20#include <spa/pod/builder.h>
21#include <spa/pod/parser.h>
22#include <spa/param/tag.h>
23
24#ifndef SPA_API_TAG_UTILS
25 #ifdef SPA_API_IMPL
26 #define SPA_API_TAG_UTILS SPA_API_IMPL
27 #else
28 #define SPA_API_TAG_UTILS static inline
29 #endif
30#endif
31
33spa_tag_compare(const struct spa_pod *a, const struct spa_pod *b)
34{
35 return ((a == b) || (a && b && SPA_POD_SIZE(a) == SPA_POD_SIZE(b) &&
36 memcmp(a, b, SPA_POD_SIZE(b)) == 0)) ? 0 : 1;
37}
38
40spa_tag_parse(const struct spa_pod *tag, struct spa_tag_info *info, void **state)
41{
42 int res;
43 const struct spa_pod_object *obj = (const struct spa_pod_object*)tag;
44 const struct spa_pod_prop *first, *start, *cur;
45
46 spa_zero(*info);
48 if ((res = spa_pod_parse_object(tag,
50 SPA_PARAM_TAG_direction, SPA_POD_Id(&info->direction))) < 0)
51 return res;
52
53 first = spa_pod_prop_first(&obj->body);
54 start = *state ? spa_pod_prop_next((struct spa_pod_prop*)*state) : first;
55
56 res = 0;
57 for (cur = start; spa_pod_prop_is_inside(&obj->body, obj->pod.size, cur);
58 cur = spa_pod_prop_next(cur)) {
59 if (cur->key == SPA_PARAM_TAG_info) {
60 info->info = &cur->value;
61 *state = (void*)cur;
62 return 1;
63 }
64 }
65 return 0;
66}
67
69spa_tag_info_parse(const struct spa_tag_info *info, struct spa_dict *dict, struct spa_dict_item *items)
70{
71 struct spa_pod_parser prs;
72 uint32_t n, n_items;
73 const char *key, *value;
74 struct spa_pod_frame f[1];
75
76 spa_pod_parser_pod(&prs, info->info);
77 if (spa_pod_parser_push_struct(&prs, &f[0]) < 0 ||
78 spa_pod_parser_get_int(&prs, (int32_t*)&n_items) < 0)
79 return -EINVAL;
80
81 if (items == NULL) {
82 dict->n_items = n_items;
83 return 0;
84 }
85 n_items = SPA_MIN(dict->n_items, n_items);
86
87 for (n = 0; n < n_items; n++) {
88 if (spa_pod_parser_get(&prs,
89 SPA_POD_String(&key),
90 SPA_POD_String(&value),
91 NULL) < 0)
92 break;
93 items[n].key = key;
94 items[n].value = value;
95 }
96 dict->items = items;
97 spa_pod_parser_pop(&prs, &f[0]);
98 return 0;
99}
100
102spa_tag_build_start(struct spa_pod_builder *builder, struct spa_pod_frame *f,
103 uint32_t id, enum spa_direction direction)
104{
106 spa_pod_builder_add(builder,
108 0);
110
112spa_tag_build_add_info(struct spa_pod_builder *builder, const struct spa_pod *info)
113{
114 spa_pod_builder_add(builder,
116 0);
117}
118
120spa_tag_build_add_dict(struct spa_pod_builder *builder, const struct spa_dict *dict)
121{
122 uint32_t i, n_items;
123 struct spa_pod_frame f;
124
125 n_items = dict ? dict->n_items : 0;
126
128 spa_pod_builder_push_struct(builder, &f);
129 spa_pod_builder_int(builder, n_items);
130 for (i = 0; i < n_items; i++) {
131 spa_pod_builder_string(builder, dict->items[i].key);
132 spa_pod_builder_string(builder, dict->items[i].value);
133 }
134 spa_pod_builder_pop(builder, &f);
135}
136
138spa_tag_build_end(struct spa_pod_builder *builder, struct spa_pod_frame *f)
139{
140 return (struct spa_pod*)spa_pod_builder_pop(builder, f);
141}
142
147#ifdef __cplusplus
148} /* extern "C" */
149#endif
150
151#endif /* SPA_PARAM_TAG_UTILS_H */
spa/pod/builder.h
uint32_t int int res
Definition core.h:433
SPA_API_TAG_UTILS void spa_tag_build_start(struct spa_pod_builder *builder, struct spa_pod_frame *f, uint32_t id, enum spa_direction direction)
Definition tag-utils.h:109
SPA_API_TAG_UTILS void spa_tag_build_add_info(struct spa_pod_builder *builder, const struct spa_pod *info)
Definition tag-utils.h:119
SPA_API_TAG_UTILS void spa_tag_build_add_dict(struct spa_pod_builder *builder, const struct spa_dict *dict)
Definition tag-utils.h:127
SPA_API_TAG_UTILS struct spa_pod * spa_tag_build_end(struct spa_pod_builder *builder, struct spa_pod_frame *f)
Definition tag-utils.h:145
#define SPA_API_TAG_UTILS
Definition tag-utils.h:35
SPA_API_TAG_UTILS int spa_tag_info_parse(const struct spa_tag_info *info, struct spa_dict *dict, struct spa_dict_item *items)
Definition tag-utils.h:76
SPA_API_TAG_UTILS int spa_tag_parse(const struct spa_pod *tag, struct spa_tag_info *info, void **state)
Definition tag-utils.h:47
SPA_API_TAG_UTILS int spa_tag_compare(const struct spa_pod *a, const struct spa_pod *b)
Definition tag-utils.h:40
@ SPA_PARAM_TAG_direction
direction, input/output (Id enum spa_direction)
Definition tag.h:27
@ SPA_PARAM_TAG_info
Struct( Int: n_items (String: key ...
Definition tag.h:28
SPA_API_POD_BUILDER void * spa_pod_builder_pop(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition builder.h:187
SPA_API_POD_BUILDER int spa_pod_builder_int(struct spa_pod_builder *builder, int32_t val)
Definition builder.h:266
SPA_API_POD_ITER struct spa_pod_prop * spa_pod_prop_first(const struct spa_pod_object_body *body)
Definition iter.h:57
#define SPA_POD_String(val)
Definition vararg.h:94
SPA_API_POD_PARSER int spa_pod_parser_get_int(struct spa_pod_parser *parser, int32_t *value)
Definition parser.h:158
SPA_API_POD_PARSER int spa_pod_parser_pop(struct spa_pod_parser *parser, struct spa_pod_frame *frame)
Definition parser.h:132
SPA_API_POD_PARSER void spa_pod_parser_pod(struct spa_pod_parser *parser, const struct spa_pod *pod)
Definition parser.h:60
SPA_API_POD_BUILDER int spa_pod_builder_string(struct spa_pod_builder *builder, const char *str)
Definition builder.h:324
SPA_API_POD_ITER bool spa_pod_prop_is_inside(const struct spa_pod_object_body *body, uint32_t size, const struct spa_pod_prop *iter)
Definition iter.h:62
SPA_API_POD_BUILDER int spa_pod_builder_push_struct(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition builder.h:441
SPA_API_POD_BUILDER int spa_pod_builder_add(struct spa_pod_builder *builder,...)
Definition builder.h:666
#define SPA_POD_Id(val)
Definition vararg.h:49
SPA_API_POD_BUILDER int spa_pod_builder_push_object(struct spa_pod_builder *builder, struct spa_pod_frame *frame, uint32_t type, uint32_t id)
Definition builder.h:454
SPA_API_POD_PARSER int spa_pod_parser_push_struct(struct spa_pod_parser *parser, struct spa_pod_frame *frame)
Definition parser.h:257
SPA_API_POD_ITER struct spa_pod_prop * spa_pod_prop_next(const struct spa_pod_prop *iter)
Definition iter.h:71
SPA_API_POD_PARSER int spa_pod_parser_get(struct spa_pod_parser *parser,...)
Definition parser.h:515
#define SPA_POD_PROP_FLAG_HINT_DICT
contains a dictionary struct as (Struct( Int : n_items,...
Definition pod.h:216
#define spa_pod_parse_object(pod, type, id,...)
Definition parser.h:586
#define SPA_POD_Pod(val)
Definition vararg.h:128
#define SPA_POD_SIZE(pod)
Definition pod.h:30
SPA_API_POD_BUILDER int spa_pod_builder_prop(struct spa_pod_builder *builder, uint32_t key, uint32_t flags)
Definition builder.h:469
@ SPA_TYPE_OBJECT_ParamTag
Definition type.h:97
#define SPA_MIN(a, b)
Definition defs.h:165
#define spa_zero(x)
Definition defs.h:508
spa_direction
Definition defs.h:106
spa/pod/parser.h
Definition dict.h:41
const char * key
Definition dict.h:42
const char * value
Definition dict.h:43
Definition dict.h:51
const struct spa_dict_item * items
Definition dict.h:56
uint32_t n_items
Definition dict.h:55
Definition builder.h:63
Definition iter.h:37
Definition pod.h:183
struct spa_pod pod
Definition pod.h:184
struct spa_pod_object_body body
Definition pod.h:185
Definition parser.h:44
Definition pod.h:208
uint32_t key
key of property, list of valid keys depends on the object type
Definition pod.h:209
struct spa_pod value
Definition pod.h:226
Definition pod.h:43
uint32_t size
Definition pod.h:44
helper structure for managing tag objects
Definition tag.h:36
spa/param/tag.h
spa/utils/dict.h