PipeWire  1.6.4
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 #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 
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 
35 spa_tag_compare(const struct spa_pod *a, const struct spa_pod *b)
36 {
37  return spa_pod_memcmp(a, b);
38 }
39 
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,
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 
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->items = items;
100  spa_pod_parser_pop(&prs, &f[0]);
101  return 0;
102 }
103 
105 spa_tag_build_start(struct spa_pod_builder *builder, struct spa_pod_frame *f,
106  uint32_t id, enum spa_direction direction)
107 {
109  spa_pod_builder_add(builder,
111  0);
112 }
113 
115 spa_tag_build_add_info(struct spa_pod_builder *builder, const struct spa_pod *info)
116 {
117  spa_pod_builder_add(builder,
119  0);
120 }
121 
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 
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 
150 #ifdef __cplusplus
151 } /* extern "C" */
152 #endif
153 
154 #endif /* SPA_PARAM_TAG_UTILS_H */
spa/pod/builder.h
spa/pod/compare.h
uint32_t int int res
Definition: core.h:433
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:148
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:112
SPA_API_TAG_UTILS void spa_tag_build_add_info(struct spa_pod_builder *builder, const struct spa_pod *info)
Definition: tag-utils.h:122
SPA_API_TAG_UTILS void spa_tag_build_add_dict(struct spa_pod_builder *builder, const struct spa_dict *dict)
Definition: tag-utils.h:130
#define SPA_API_TAG_UTILS
Definition: tag-utils.h:37
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:77
SPA_API_TAG_UTILS int spa_tag_parse(const struct spa_pod *tag, struct spa_tag_info *info, void **state)
Definition: tag-utils.h:48
SPA_API_TAG_UTILS int spa_tag_compare(const struct spa_pod *a, const struct spa_pod *b)
Definition: tag-utils.h:42
@ 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 String: value)* )
Definition: tag.h:28
SPA_API_POD_BUILDER int spa_pod_builder_int(struct spa_pod_builder *builder, int32_t val)
Definition: builder.h:296
#define SPA_POD_String(val)
Definition: vararg.h:102
SPA_API_POD_PARSER int spa_pod_parser_get_int(struct spa_pod_parser *parser, int32_t *value)
Definition: parser.h:236
SPA_API_POD_PARSER int spa_pod_parser_pop(struct spa_pod_parser *parser, struct spa_pod_frame *frame)
Definition: parser.h:204
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:351
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:56
SPA_API_POD_BUILDER int spa_pod_builder_push_struct(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition: builder.h:462
SPA_API_POD_BUILDER int spa_pod_builder_add(struct spa_pod_builder *builder,...)
Definition: builder.h:713
#define SPA_POD_Id(val)
Definition: vararg.h:53
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:475
SPA_API_POD_PARSER int spa_pod_parser_push_struct(struct spa_pod_parser *parser, struct spa_pod_frame *frame)
Definition: parser.h:400
SPA_API_POD_ITER struct spa_pod_prop * spa_pod_prop_first(const struct spa_pod_object_body *body)
Definition: iter.h:51
SPA_API_POD_PARSER int spa_pod_parser_get(struct spa_pod_parser *parser,...)
Definition: parser.h:849
SPA_API_POD_ITER struct spa_pod_prop * spa_pod_prop_next(const struct spa_pod_prop *iter)
Definition: iter.h:65
#define SPA_POD_PROP_FLAG_HINT_DICT
contains a dictionary struct as (Struct( Int : n_items, (String : key, String : value)*))
Definition: pod.h:235
#define spa_pod_parse_object(pod, type, id,...)
Definition: parser.h:935
SPA_API_POD_BUILDER void * spa_pod_builder_pop(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition: builder.h:213
#define SPA_POD_Pod(val)
Definition: vararg.h:136
SPA_API_POD_COMPARE int spa_pod_memcmp(const struct spa_pod *a, const struct spa_pod *b)
Definition: compare.h:90
SPA_API_POD_BUILDER int spa_pod_builder_prop(struct spa_pod_builder *builder, uint32_t key, uint32_t flags)
Definition: builder.h:490
@ SPA_TYPE_OBJECT_ParamTag
Definition: type.h:97
#define SPA_MIN(a, b)
Definition: defs.h:165
#define spa_zero(x)
Definition: defs.h:512
spa_direction
Definition: defs.h:106
spa/pod/iter.h
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: body.h:38
Definition: pod.h:202
struct spa_pod pod
Definition: pod.h:203
struct spa_pod_object_body body
Definition: pod.h:204
Definition: parser.h:44
Definition: pod.h:227
uint32_t key
key of property, list of valid keys depends on the object type
Definition: pod.h:228
struct spa_pod value
Definition: pod.h:249
Definition: pod.h:57
uint32_t size
Definition: pod.h:58
helper structure for managing tag objects
Definition: tag.h:36
enum spa_direction direction
Definition: tag.h:37
const struct spa_pod * info
Definition: tag.h:38
spa/param/tag.h
spa/utils/dict.h