PipeWire  1.2.5
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
9 extern "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 static inline int
25 spa_tag_compare(const struct spa_pod *a, const struct spa_pod *b)
26 {
27  return ((a == b) || (a && b && SPA_POD_SIZE(a) == SPA_POD_SIZE(b) &&
28  memcmp(a, b, SPA_POD_SIZE(b)) == 0)) ? 0 : 1;
29 }
30 
31 static inline int
32 spa_tag_parse(const struct spa_pod *tag, struct spa_tag_info *info, void **state)
33 {
34  int res;
35  const struct spa_pod_object *obj = (const struct spa_pod_object*)tag;
36  const struct spa_pod_prop *first, *start, *cur;
37 
38  spa_zero(*info);
39 
40  if ((res = spa_pod_parse_object(tag,
43  return res;
44 
45  first = spa_pod_prop_first(&obj->body);
46  start = *state ? spa_pod_prop_next((struct spa_pod_prop*)*state) : first;
47 
48  res = 0;
49  for (cur = start; spa_pod_prop_is_inside(&obj->body, obj->pod.size, cur);
50  cur = spa_pod_prop_next(cur)) {
51  if (cur->key == SPA_PARAM_TAG_info) {
52  info->info = &cur->value;
53  *state = (void*)cur;
54  return 1;
55  }
56  }
57  return 0;
58 }
59 
60 static inline int
61 spa_tag_info_parse(const struct spa_tag_info *info, struct spa_dict *dict, struct spa_dict_item *items)
62 {
63  struct spa_pod_parser prs;
64  uint32_t n, n_items;
65  const char *key, *value;
66  struct spa_pod_frame f[1];
67 
68  spa_pod_parser_pod(&prs, info->info);
69  if (spa_pod_parser_push_struct(&prs, &f[0]) < 0 ||
70  spa_pod_parser_get_int(&prs, (int32_t*)&n_items) < 0)
71  return -EINVAL;
72 
73  if (items == NULL) {
74  dict->n_items = n_items;
75  return 0;
76  }
77  n_items = SPA_MIN(dict->n_items, n_items);
78 
79  for (n = 0; n < n_items; n++) {
80  if (spa_pod_parser_get(&prs,
81  SPA_POD_String(&key),
82  SPA_POD_String(&value),
83  NULL) < 0)
84  break;
85  items[n].key = key;
86  items[n].value = value;
87  }
88  dict->items = items;
89  spa_pod_parser_pop(&prs, &f[0]);
90  return 0;
91 }
92 
93 static inline void
94 spa_tag_build_start(struct spa_pod_builder *builder, struct spa_pod_frame *f,
95  uint32_t id, enum spa_direction direction)
96 {
98  spa_pod_builder_add(builder,
100  0);
101 }
102 
103 static inline void
104 spa_tag_build_add_info(struct spa_pod_builder *builder, const struct spa_pod *info)
105 {
106  spa_pod_builder_add(builder,
108  0);
109 }
110 
111 static inline void
112 spa_tag_build_add_dict(struct spa_pod_builder *builder, const struct spa_dict *dict)
113 {
114  uint32_t i, n_items;
115  struct spa_pod_frame f;
116 
117  n_items = dict ? dict->n_items : 0;
118 
120  spa_pod_builder_push_struct(builder, &f);
121  spa_pod_builder_int(builder, n_items);
122  for (i = 0; i < n_items; i++) {
123  spa_pod_builder_string(builder, dict->items[i].key);
124  spa_pod_builder_string(builder, dict->items[i].value);
125  }
126  spa_pod_builder_pop(builder, &f);
127 }
128 
129 static inline struct spa_pod *
130 spa_tag_build_end(struct spa_pod_builder *builder, struct spa_pod_frame *f)
131 {
132  return (struct spa_pod*)spa_pod_builder_pop(builder, f);
133 }
134 
139 #ifdef __cplusplus
140 } /* extern "C" */
141 #endif
142 
143 #endif /* SPA_PARAM_TAG_UTILS_H */
spa_pod_object
Definition: pod.h:183
SPA_TYPE_OBJECT_ParamTag
@ SPA_TYPE_OBJECT_ParamTag
Definition: type.h:86
spa_tag_parse
static int spa_tag_parse(const struct spa_pod *tag, struct spa_tag_info *info, void **state)
Definition: tag-utils.h:37
spa_pod_parser_get
static int spa_pod_parser_get(struct spa_pod_parser *parser,...)
Definition: parser.h:505
spa_pod_parser_push_struct
static int spa_pod_parser_push_struct(struct spa_pod_parser *parser, struct spa_pod_frame *frame)
Definition: parser.h:247
SPA_POD_Pod
#define SPA_POD_Pod(val)
Definition: vararg.h:128
spa_dict_item::key
const char * key
Definition: dict.h:32
spa_pod_builder_add
static int spa_pod_builder_add(struct spa_pod_builder *builder,...)
Definition: builder.h:647
SPA_PARAM_TAG_info
@ SPA_PARAM_TAG_info
Struct( Int: n_items (String: key String: value)* )
Definition: tag.h:28
spa_pod_prop
Definition: pod.h:208
spa_pod_prop_first
static struct spa_pod_prop * spa_pod_prop_first(const struct spa_pod_object_body *body)
Definition: iter.h:47
spa_pod_builder_string
static int spa_pod_builder_string(struct spa_pod_builder *builder, const char *str)
Definition: builder.h:305
spa_pod_prop_is_inside
static 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:52
spa_pod_builder_int
static int spa_pod_builder_int(struct spa_pod_builder *builder, int32_t val)
Definition: builder.h:247
spa_dict_item
Definition: dict.h:31
spa_pod
Definition: pod.h:43
spa_pod_parser_get_int
static int spa_pod_parser_get_int(struct spa_pod_parser *parser, int32_t *value)
Definition: parser.h:148
spa_pod_builder_pop
static void * spa_pod_builder_pop(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition: builder.h:168
spa_tag_info::direction
enum spa_direction direction
Definition: tag.h:37
spa_tag_build_add_info
static void spa_tag_build_add_info(struct spa_pod_builder *builder, const struct spa_pod *info)
Definition: tag-utils.h:109
spa_pod_object::body
struct spa_pod_object_body body
Definition: pod.h:185
spa_tag_info::info
const struct spa_pod * info
Definition: tag.h:38
SPA_POD_PROP_FLAG_HINT_DICT
#define SPA_POD_PROP_FLAG_HINT_DICT
contains a dictionary struct as (Struct( Int : n_items, (String : key, String : value)*))
Definition: pod.h:216
spa_dict::items
const struct spa_dict_item * items
Definition: dict.h:44
spa_tag_build_add_dict
static void spa_tag_build_add_dict(struct spa_pod_builder *builder, const struct spa_dict *dict)
Definition: tag-utils.h:117
spa_pod_prop::key
uint32_t key
key of property, list of valid keys depends on the object type
Definition: pod.h:209
spa_pod::size
uint32_t size
Definition: pod.h:44
spa_dict
Definition: dict.h:39
spa_pod_builder_prop
static int spa_pod_builder_prop(struct spa_pod_builder *builder, uint32_t key, uint32_t flags)
Definition: builder.h:450
spa_pod_parser
Definition: parser.h:34
spa_zero
#define spa_zero(x)
Definition: defs.h:483
spa_pod_frame
Definition: iter.h:27
spa_pod_parse_object
#define spa_pod_parse_object(pod, type, id,...)
Definition: parser.h:576
spa_dict_item::value
const char * value
Definition: dict.h:33
spa_pod_parser_pod
static void spa_pod_parser_pod(struct spa_pod_parser *parser, const struct spa_pod *pod)
Definition: parser.h:50
spa_pod_object::pod
struct spa_pod pod
Definition: pod.h:184
SPA_PARAM_TAG_direction
@ SPA_PARAM_TAG_direction
direction, input/output (Id enum spa_direction)
Definition: tag.h:27
spa_tag_compare
static int spa_tag_compare(const struct spa_pod *a, const struct spa_pod *b)
Definition: tag-utils.h:30
parser.h
spa/pod/parser.h
spa_pod_prop_next
static struct spa_pod_prop * spa_pod_prop_next(const struct spa_pod_prop *iter)
Definition: iter.h:61
spa_pod_builder_push_object
static 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:435
SPA_POD_String
#define SPA_POD_String(val)
Definition: vararg.h:94
tag.h
spa/param/tag.h
spa_pod_builder_push_struct
static int spa_pod_builder_push_struct(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition: builder.h:422
SPA_POD_SIZE
#define SPA_POD_SIZE(pod)
Definition: pod.h:30
SPA_MIN
#define SPA_MIN(a, b)
Definition: defs.h:165
spa_direction
spa_direction
Definition: defs.h:106
spa_dict::n_items
uint32_t n_items
Definition: dict.h:43
spa_pod_prop::value
struct spa_pod value
Definition: pod.h:226
SPA_POD_Id
#define SPA_POD_Id(val)
Definition: vararg.h:49
spa_tag_info_parse
static int spa_tag_info_parse(const struct spa_tag_info *info, struct spa_dict *dict, struct spa_dict_item *items)
Definition: tag-utils.h:66
spa_pod_builder
Definition: builder.h:53
spa_tag_info
helper structure for managing tag objects
Definition: tag.h:36
spa_tag_build_start
static 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:99
builder.h
spa/pod/builder.h
spa_tag_build_end
static struct spa_pod * spa_tag_build_end(struct spa_pod_builder *builder, struct spa_pod_frame *f)
Definition: tag-utils.h:135
spa_pod_parser_pop
static int spa_pod_parser_pop(struct spa_pod_parser *parser, struct spa_pod_frame *frame)
Definition: parser.h:122
dict.h
spa/utils/dict.h