PipeWire 1.6.8

Loading...
Searching...
No Matches
builder.h
Go to the documentation of this file.
1/* Simple Plugin API */
2/* SPDX-FileCopyrightText: Copyright © 2018 Wim Taymans */
3/* SPDX-License-Identifier: MIT */
4
5#ifndef SPA_POD_BUILDER_H
6#define SPA_POD_BUILDER_H
7
8#include <stdarg.h>
9
10#include <spa/utils/hook.h>
11#include <spa/pod/body.h>
12#include <spa/pod/vararg.h>
13
14#ifdef __cplusplus
15 extern "C" {
16#endif
17
21
26
27#ifndef SPA_API_POD_BUILDER
28 #ifdef SPA_API_IMPL
29 #define SPA_API_POD_BUILDER SPA_API_IMPL
30 #else
31 #define SPA_API_POD_BUILDER static inline
32 #endif
33#endif
34
35 struct spa_pod_builder_state {
36 uint32_t offset;
37#define SPA_POD_BUILDER_FLAG_BODY (1<<0)
38#define SPA_POD_BUILDER_FLAG_FIRST (1<<1)
39 uint32_t flags;
40 struct spa_pod_frame *frame;
41};
42
43 struct spa_pod_builder;
44
45 struct spa_pod_builder_callbacks {
46#define SPA_VERSION_POD_BUILDER_CALLBACKS 0
47 uint32_t version;
48
49 int (*overflow) (void *data, uint32_t size);
50};
51
52 struct spa_pod_builder {
53 void *data;
54 uint32_t size;
55 uint32_t _padding;
56 struct spa_pod_builder_state state;
57 struct spa_callbacks callbacks;
58};
59
60#define SPA_POD_BUILDER_INIT(buffer,size) ((struct spa_pod_builder){ (buffer), (size), 0, {0,0,NULL},{NULL,NULL}})
61
62 SPA_API_POD_BUILDER void
63 spa_pod_builder_get_state(struct spa_pod_builder *builder, struct spa_pod_builder_state *state)
64{
65 *state = builder->state;
66}
67
68 SPA_API_POD_BUILDER bool spa_pod_builder_corrupted(const struct spa_pod_builder *builder)
69{
70 return builder->state.offset > builder->size;
71}
72
73 SPA_API_POD_BUILDER void
74 spa_pod_builder_set_callbacks(struct spa_pod_builder *builder,
75 const struct spa_pod_builder_callbacks *callbacks, void *data)
76{
77 builder->callbacks = SPA_CALLBACKS_INIT(callbacks, data);
78}
79
80 SPA_API_POD_BUILDER void
81 spa_pod_builder_reset(struct spa_pod_builder *builder, struct spa_pod_builder_state *state)
82{
83 struct spa_pod_frame *f;
84 uint32_t size = builder->state.offset - state->offset;
85 builder->state = *state;
86 for (f = builder->state.frame; f ; f = f->parent)
87 f->pod.size -= SPA_MIN(size, f->pod.size);
88}
89
90 SPA_API_POD_BUILDER void spa_pod_builder_init(struct spa_pod_builder *builder, void *data, uint32_t size)
91{
92 *builder = SPA_POD_BUILDER_INIT(data, size);
93}
94
95 SPA_API_POD_BUILDER struct spa_pod *
96 spa_pod_builder_deref_fallback(struct spa_pod_builder *builder, uint32_t offset, struct spa_pod *fallback)
97{
98 uint32_t size = builder->size;
99 if (offset + UINT64_C(8) <= size) {
100 struct spa_pod *pod = SPA_PTROFF(builder->data, offset, struct spa_pod);
101 if (offset + (uint64_t)SPA_POD_SIZE(pod) <= size &&
102 SPA_POD_IS_VALID(pod))
103 return pod;
104 }
105 return fallback;
106}
107
108 SPA_API_POD_BUILDER struct spa_pod *
109 spa_pod_builder_deref(struct spa_pod_builder *builder, uint32_t offset)
110{
111 return (struct spa_pod*)spa_pod_builder_deref_fallback(builder, offset, NULL);
112}
113
114 SPA_API_POD_BUILDER struct spa_pod *
115 spa_pod_builder_frame(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
116{
117 if (frame->offset + (uint64_t)SPA_POD_SIZE(&frame->pod) <= builder->size)
118 return SPA_PTROFF(builder->data, frame->offset, struct spa_pod);
119 return NULL;
120}
121
122 SPA_API_POD_BUILDER void
123 spa_pod_builder_push(struct spa_pod_builder *builder,
124 struct spa_pod_frame *frame,
125 const struct spa_pod *pod,
126 uint32_t offset)
127{
128 frame->pod = *pod;
129 frame->offset = offset;
130 frame->parent = builder->state.frame;
131 frame->flags = builder->state.flags;
132 builder->state.frame = frame;
133
134 if (frame->pod.type == SPA_TYPE_Array || frame->pod.type == SPA_TYPE_Choice)
135 builder->state.flags = SPA_POD_BUILDER_FLAG_FIRST | SPA_POD_BUILDER_FLAG_BODY;
136}
137
138 SPA_API_POD_BUILDER int spa_pod_builder_raw(struct spa_pod_builder *builder, const void *data, uint32_t size)
139{
140 int res = 0;
141 struct spa_pod_frame *f;
142 uint32_t offset = builder->state.offset;
143 size_t data_offset = -1;
144 uint64_t total_size = offset + (uint64_t) size;
145
146 if (total_size > builder->size) {
147 if (total_size > UINT32_MAX)
148 return -ENOSPC;
149
150 /* data could be inside the data we will realloc */
151 if (spa_ptrinside(builder->data, builder->size, data, size, NULL))
152 data_offset = SPA_PTRDIFF(data, builder->data);
153
154 res = -ENOSPC;
155 if (offset <= builder->size)
156 spa_callbacks_call_res(&builder->callbacks,
157 struct spa_pod_builder_callbacks, res,
158 overflow, 0, total_size);
159 }
160 if (res == 0 && data) {
161 if (data_offset != (size_t) -1)
162 data = SPA_PTROFF(builder->data, data_offset, const void);
163
164 memcpy(SPA_PTROFF(builder->data, offset, void), data, size);
165 }
166
167 builder->state.offset = total_size;
168
169 for (f = builder->state.frame; f ; f = f->parent)
170 f->pod.size += size;
171
172 return res;
173}
174
175 SPA_API_POD_BUILDER void spa_pod_builder_remove(struct spa_pod_builder *builder, uint32_t size)
176{
177 struct spa_pod_frame *f;
178 builder->state.offset -= SPA_MIN(size, builder->state.offset);
179 for (f = builder->state.frame; f ; f = f->parent)
180 f->pod.size -= SPA_MIN(size, f->pod.size);
181}
182
183 SPA_API_POD_BUILDER int spa_pod_builder_pad(struct spa_pod_builder *builder, uint32_t size)
184{
185 uint64_t zeroes = 0;
186 if (builder->state.flags == SPA_POD_BUILDER_FLAG_BODY)
187 return 0;
188 size = SPA_ROUND_UP_N(size, SPA_POD_ALIGN) - size;
189 return size ? spa_pod_builder_raw(builder, &zeroes, size) : 0;
190}
191
192 SPA_API_POD_BUILDER int
193 spa_pod_builder_raw_padded(struct spa_pod_builder *builder, const void *data, uint32_t size)
194{
195 int r, res = spa_pod_builder_raw(builder, data, size);
196 if ((r = spa_pod_builder_pad(builder, size)) < 0)
197 res = r;
198 return res;
199}
200
201 SPA_API_POD_BUILDER void *spa_pod_builder_pop(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
202{
203 struct spa_pod *pod;
204
205 if (SPA_FLAG_IS_SET(builder->state.flags, SPA_POD_BUILDER_FLAG_FIRST)) {
206 const struct spa_pod p = { 0, SPA_TYPE_None };
207 spa_pod_builder_raw(builder, &p, sizeof(p));
208 }
209 if ((pod = (struct spa_pod*)spa_pod_builder_frame(builder, frame)) != NULL)
210 *pod = frame->pod;
211
212 builder->state.frame = frame->parent;
213 builder->state.flags = frame->flags;
214 spa_pod_builder_pad(builder, builder->state.offset);
215 return pod;
216}
217
218 SPA_API_POD_BUILDER int
219 spa_pod_builder_primitive_body(struct spa_pod_builder *builder, const struct spa_pod *p,
220 const void *body, uint32_t body_size, const char *suffix, uint32_t suffix_size)
221{
222 int res = 0, r;
223 uint32_t size = SPA_POD_SIZE(p) - body_size - suffix_size;
224 if (builder->state.flags != SPA_POD_BUILDER_FLAG_BODY) {
225 SPA_FLAG_CLEAR(builder->state.flags, SPA_POD_BUILDER_FLAG_FIRST);
226 res = spa_pod_builder_raw(builder, p, size);
227 }
228 if (body_size > 0 && (r = spa_pod_builder_raw(builder, body, body_size)) < 0)
229 res = r;
230 if (suffix_size > 0 && (r = spa_pod_builder_raw(builder, suffix, suffix_size)) < 0)
231 res = r;
232 if ((r = spa_pod_builder_pad(builder, builder->state.offset)) < 0)
233 res = r;
234 return res;
235}
236
237 SPA_API_POD_BUILDER int
238 spa_pod_builder_primitive(struct spa_pod_builder *builder, const struct spa_pod *p)
239{
240 return spa_pod_builder_primitive_body(builder, p,
241 SPA_POD_BODY_CONST(p), p->size, NULL, 0);
242}
243
244#define SPA_POD_INIT(size,type) ((struct spa_pod) { (size), (type) })
245
246#define SPA_POD_INIT_None() SPA_POD_INIT(0, SPA_TYPE_None)
247
248 SPA_API_POD_BUILDER int spa_pod_builder_none(struct spa_pod_builder *builder)
249{
250 const struct spa_pod p = SPA_POD_INIT_None();
251 return spa_pod_builder_primitive(builder, &p);
252}
253
254 SPA_API_POD_BUILDER int spa_pod_builder_child(struct spa_pod_builder *builder, uint32_t size, uint32_t type)
255{
256 const struct spa_pod p = SPA_POD_INIT(size,type);
257 SPA_FLAG_CLEAR(builder->state.flags, SPA_POD_BUILDER_FLAG_FIRST);
258 return spa_pod_builder_raw(builder, &p, sizeof(p));
259}
260
261#define SPA_POD_INIT_Bool(val) ((struct spa_pod_bool){ { sizeof(uint32_t), SPA_TYPE_Bool }, (val) ? 1 : 0, 0 })
262
263 SPA_API_POD_BUILDER int spa_pod_builder_bool(struct spa_pod_builder *builder, bool val)
264{
265 const struct spa_pod_bool p = SPA_POD_INIT_Bool(val);
266 return spa_pod_builder_primitive(builder, &p.pod);
267}
268
269#define SPA_POD_INIT_Id(val) ((struct spa_pod_id){ { sizeof(uint32_t), SPA_TYPE_Id }, (val), 0 })
270
271 SPA_API_POD_BUILDER int spa_pod_builder_id(struct spa_pod_builder *builder, uint32_t val)
272{
273 const struct spa_pod_id p = SPA_POD_INIT_Id(val);
274 return spa_pod_builder_primitive(builder, &p.pod);
275}
276
277#define SPA_POD_INIT_Int(val) ((struct spa_pod_int){ { sizeof(int32_t), SPA_TYPE_Int }, (val), 0 })
278
279 SPA_API_POD_BUILDER int spa_pod_builder_int(struct spa_pod_builder *builder, int32_t val)
280{
281 const struct spa_pod_int p = SPA_POD_INIT_Int(val);
282 return spa_pod_builder_primitive(builder, &p.pod);
283}
284
285#define SPA_POD_INIT_Long(val) ((struct spa_pod_long){ { sizeof(int64_t), SPA_TYPE_Long }, (val) })
286
287 SPA_API_POD_BUILDER int spa_pod_builder_long(struct spa_pod_builder *builder, int64_t val)
288{
289 const struct spa_pod_long p = SPA_POD_INIT_Long(val);
290 return spa_pod_builder_primitive(builder, &p.pod);
291}
292
293#define SPA_POD_INIT_Float(val) ((struct spa_pod_float){ { sizeof(float), SPA_TYPE_Float }, (val), 0 })
294
295 SPA_API_POD_BUILDER int spa_pod_builder_float(struct spa_pod_builder *builder, float val)
296{
297 const struct spa_pod_float p = SPA_POD_INIT_Float(val);
298 return spa_pod_builder_primitive(builder, &p.pod);
299}
300
301#define SPA_POD_INIT_Double(val) ((struct spa_pod_double){ { sizeof(double), SPA_TYPE_Double }, (val) })
302
303 SPA_API_POD_BUILDER int spa_pod_builder_double(struct spa_pod_builder *builder, double val)
304{
305 const struct spa_pod_double p = SPA_POD_INIT_Double(val);
306 return spa_pod_builder_primitive(builder, &p.pod);
307}
308
309#define SPA_POD_INIT_String(len) ((struct spa_pod_string){ { (len), SPA_TYPE_String } })
310
311 SPA_API_POD_BUILDER int
312 spa_pod_builder_write_string(struct spa_pod_builder *builder, const char *str, uint32_t len)
313{
314 int r, res;
315 res = spa_pod_builder_raw(builder, str, len);
316 if ((r = spa_pod_builder_raw(builder, "", 1)) < 0)
317 res = r;
318 if ((r = spa_pod_builder_pad(builder, builder->state.offset)) < 0)
319 res = r;
320 return res;
321}
322
323 SPA_API_POD_BUILDER int
324 spa_pod_builder_string_len(struct spa_pod_builder *builder, const char *str, uint32_t len)
325{
326 const struct spa_pod_string p = SPA_POD_INIT_String(len+1);
327 return spa_pod_builder_primitive_body(builder, &p.pod, str, len, "", 1);
328}
329
330 SPA_API_POD_BUILDER int spa_pod_builder_string(struct spa_pod_builder *builder, const char *str)
331{
332 uint32_t len = str ? strlen(str) : 0;
333 return spa_pod_builder_string_len(builder, str ? str : "", len);
334}
335
336#define SPA_POD_INIT_Bytes(len) ((struct spa_pod_bytes){ { (len), SPA_TYPE_Bytes } })
337
338 SPA_API_POD_BUILDER int
339 spa_pod_builder_bytes(struct spa_pod_builder *builder, const void *bytes, uint32_t len)
340{
341 const struct spa_pod_bytes p = SPA_POD_INIT_Bytes(len);
342 return spa_pod_builder_primitive_body(builder, &p.pod, bytes, len, NULL, 0);
343}
344 SPA_API_POD_BUILDER void *
345 spa_pod_builder_reserve_bytes(struct spa_pod_builder *builder, uint32_t len)
346{
347 uint32_t offset = builder->state.offset;
348 if (spa_pod_builder_bytes(builder, NULL, len) < 0)
349 return NULL;
350 return SPA_PTROFF(builder->data, offset + sizeof(struct spa_pod), void);
351}
352
353#define SPA_POD_INIT_Pointer(type,value) ((struct spa_pod_pointer){ { sizeof(struct spa_pod_pointer_body), SPA_TYPE_Pointer }, { (type), 0, (value) } })
354
355 SPA_API_POD_BUILDER int
356 spa_pod_builder_pointer(struct spa_pod_builder *builder, uint32_t type, const void *val)
357{
358 const struct spa_pod_pointer p = SPA_POD_INIT_Pointer(type, val);
359 return spa_pod_builder_primitive(builder, &p.pod);
360}
361
362#define SPA_POD_INIT_Fd(fd) ((struct spa_pod_fd){ { sizeof(int64_t), SPA_TYPE_Fd }, (fd) })
363
364 SPA_API_POD_BUILDER int spa_pod_builder_fd(struct spa_pod_builder *builder, int64_t fd)
365{
366 const struct spa_pod_fd p = SPA_POD_INIT_Fd(fd);
367 return spa_pod_builder_primitive(builder, &p.pod);
368}
369
370#define SPA_POD_INIT_Rectangle(val) ((struct spa_pod_rectangle){ { sizeof(struct spa_rectangle), SPA_TYPE_Rectangle }, (val) })
371
372 SPA_API_POD_BUILDER int
373 spa_pod_builder_rectangle(struct spa_pod_builder *builder, uint32_t width, uint32_t height)
374{
375 const struct spa_pod_rectangle p = SPA_POD_INIT_Rectangle(SPA_RECTANGLE(width, height));
376 return spa_pod_builder_primitive(builder, &p.pod);
377}
378
379#define SPA_POD_INIT_Fraction(val) ((struct spa_pod_fraction){ { sizeof(struct spa_fraction), SPA_TYPE_Fraction }, (val) })
380
381 SPA_API_POD_BUILDER int
382 spa_pod_builder_fraction(struct spa_pod_builder *builder, uint32_t num, uint32_t denom)
383{
384 const struct spa_pod_fraction p = SPA_POD_INIT_Fraction(SPA_FRACTION(num, denom));
385 return spa_pod_builder_primitive(builder, &p.pod);
386}
387
388 SPA_API_POD_BUILDER int
389 spa_pod_builder_push_array(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
390{
391 const struct spa_pod_array p =
392 { {sizeof(struct spa_pod_array_body) - sizeof(struct spa_pod), SPA_TYPE_Array},
393 {{0, 0}} };
394 uint32_t offset = builder->state.offset;
395 int res = spa_pod_builder_raw(builder, &p, sizeof(p) - sizeof(struct spa_pod));
396 spa_pod_builder_push(builder, frame, &p.pod, offset);
397 return res;
398}
399
400 SPA_API_POD_BUILDER int
401 spa_pod_builder_array(struct spa_pod_builder *builder,
402 uint32_t child_size, uint32_t child_type, uint32_t n_elems, const void *elems)
403{
404 const struct spa_pod_array p = {
405 {(uint32_t)(sizeof(struct spa_pod_array_body) + n_elems * child_size), SPA_TYPE_Array},
406 {{child_size, child_type}}
407 };
408 return spa_pod_builder_primitive_body(builder, &p.pod, elems, n_elems * child_size, NULL, 0);
409}
410
411#define SPA_POD_INIT_CHOICE_BODY(type, flags, child_size, child_type) \
412 ((struct spa_pod_choice_body) { (type), (flags), { (child_size), (child_type) }})
413
414#define SPA_POD_INIT_Choice(type, ctype, child_type, n_vals, ...) \
415 ((struct { struct spa_pod_choice choice; ctype vals[(n_vals)];}) \
416 { { { (n_vals) * sizeof(ctype) + sizeof(struct spa_pod_choice_body), SPA_TYPE_Choice }, \
417 { (type), 0, { sizeof(ctype), (child_type) } } }, { __VA_ARGS__ } })
418
419 SPA_API_POD_BUILDER int
420 spa_pod_builder_push_choice(struct spa_pod_builder *builder, struct spa_pod_frame *frame,
421 uint32_t type, uint32_t flags)
422{
423 const struct spa_pod_choice p =
424 { {sizeof(struct spa_pod_choice_body) - sizeof(struct spa_pod), SPA_TYPE_Choice},
425 { type, flags, {0, 0}} };
426 uint32_t offset = builder->state.offset;
427 int res = spa_pod_builder_raw(builder, &p, sizeof(p) - sizeof(struct spa_pod));
428 spa_pod_builder_push(builder, frame, &p.pod, offset);
429 return res;
430}
431
432#define SPA_POD_INIT_Struct(size) ((struct spa_pod_struct){ { (size), SPA_TYPE_Struct } })
433
434 SPA_API_POD_BUILDER int
435 spa_pod_builder_push_struct(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
436{
437 const struct spa_pod_struct p = SPA_POD_INIT_Struct(0);
438 uint32_t offset = builder->state.offset;
439 int res = spa_pod_builder_raw(builder, &p, sizeof(p));
440 spa_pod_builder_push(builder, frame, &p.pod, offset);
441 return res;
442}
443
444#define SPA_POD_INIT_Object(size,type,id,...) ((struct spa_pod_object){ { (size), SPA_TYPE_Object }, { (type), (id) }, ##__VA_ARGS__ })
445
446 SPA_API_POD_BUILDER int
447 spa_pod_builder_push_object(struct spa_pod_builder *builder, struct spa_pod_frame *frame,
448 uint32_t type, uint32_t id)
449{
450 const struct spa_pod_object p =
451 SPA_POD_INIT_Object(sizeof(struct spa_pod_object_body), type, id);
452 uint32_t offset = builder->state.offset;
453 int res = spa_pod_builder_raw(builder, &p, sizeof(p));
454 spa_pod_builder_push(builder, frame, &p.pod, offset);
455 return res;
456}
457
458#define SPA_POD_INIT_Prop(key,flags,size,type) \
459 ((struct spa_pod_prop){ (key), (flags), { (size), (type) } })
460
461 SPA_API_POD_BUILDER int
462 spa_pod_builder_prop(struct spa_pod_builder *builder, uint32_t key, uint32_t flags)
463{
464 const struct { uint32_t key; uint32_t flags; } p = { key, flags };
465 return spa_pod_builder_raw(builder, &p, sizeof(p));
466}
467
468#define SPA_POD_INIT_Sequence(size,unit) \
469 ((struct spa_pod_sequence){ { (size), SPA_TYPE_Sequence}, {(unit), 0 } })
470
471 SPA_API_POD_BUILDER int
472 spa_pod_builder_push_sequence(struct spa_pod_builder *builder, struct spa_pod_frame *frame, uint32_t unit)
473{
474 const struct spa_pod_sequence p =
475 SPA_POD_INIT_Sequence(sizeof(struct spa_pod_sequence_body), unit);
476 uint32_t offset = builder->state.offset;
477 int res = spa_pod_builder_raw(builder, &p, sizeof(p));
478 spa_pod_builder_push(builder, frame, &p.pod, offset);
479 return res;
480}
481
482 SPA_API_POD_BUILDER int
483 spa_pod_builder_control(struct spa_pod_builder *builder, uint32_t offset, uint32_t type)
484{
485 const struct { uint32_t offset; uint32_t type; } p = { offset, type };
486 return spa_pod_builder_raw(builder, &p, sizeof(p));
487}
488
489 SPA_API_POD_BUILDER uint32_t spa_choice_from_id_flags(char id, uint32_t *flags)
490{
491 switch (id) {
492 case 'r':
493 return SPA_CHOICE_Range;
494 case 's':
495 return SPA_CHOICE_Step;
496 case 'e':
497 return SPA_CHOICE_Enum;
498 case 'F':
499 *flags |= SPA_POD_PROP_FLAG_DROP;
500 SPA_FALLTHROUGH;
501 case 'f':
502 return SPA_CHOICE_Flags;
503 case 'n':
504 default:
505 return SPA_CHOICE_None;
506 }
507}
508 SPA_API_POD_BUILDER uint32_t spa_choice_from_id(char id)
509{
510 uint32_t flags = 0;
511 return spa_choice_from_id_flags(id, &flags);
512}
513
514#define SPA_POD_BUILDER_COLLECT(builder,type,args) \
515 do { \
516 switch (type) { \
517 case 'b': \
518 spa_pod_builder_bool(builder, !!va_arg(args, int)); \
519 break; \
520 case 'I': \
521 spa_pod_builder_id(builder, va_arg(args, uint32_t)); \
522 break; \
523 case 'i': \
524 spa_pod_builder_int(builder, va_arg(args, int)); \
525 break; \
526 case 'l': \
527 spa_pod_builder_long(builder, va_arg(args, int64_t)); \
528 break; \
529 case 'f': \
530 spa_pod_builder_float(builder, (float)va_arg(args, double)); \
531 break; \
532 case 'd': \
533 spa_pod_builder_double(builder, va_arg(args, double)); \
534 break; \
535 case 's': \
536 { \
537 char *strval = va_arg(args, char *); \
538 if (strval != NULL) { \
539 size_t len = strlen(strval); \
540 spa_pod_builder_string_len(builder, strval, len); \
541 } \
542 else \
543 spa_pod_builder_none(builder); \
544 break; \
545 } \
546 case 'S': \
547 { \
548 char *strval = va_arg(args, char *); \
549 size_t len = va_arg(args, int); \
550 spa_pod_builder_string_len(builder, strval, len); \
551 break; \
552 } \
553 case 'y': \
554 { \
555 void *ptr = va_arg(args, void *); \
556 int len = va_arg(args, int); \
557 spa_pod_builder_bytes(builder, ptr, len); \
558 break; \
559 } \
560 case 'R': \
561 { \
562 struct spa_rectangle *rectval = \
563 va_arg(args, struct spa_rectangle *); \
564 spa_pod_builder_rectangle(builder, \
565 rectval->width, rectval->height); \
566 break; \
567 } \
568 case 'F': \
569 { \
570 struct spa_fraction *fracval = \
571 va_arg(args, struct spa_fraction *); \
572 spa_pod_builder_fraction(builder, fracval->num, fracval->denom);\
573 break; \
574 } \
575 case 'a': \
576 { \
577 int child_size = va_arg(args, int); \
578 int child_type = va_arg(args, int); \
579 int n_elems = va_arg(args, int); \
580 void *elems = va_arg(args, void *); \
581 spa_pod_builder_array(builder, child_size, \
582 child_type, n_elems, elems); \
583 break; \
584 } \
585 case 'p': \
586 { \
587 int t = va_arg(args, uint32_t); \
588 spa_pod_builder_pointer(builder, t, va_arg(args, void *)); \
589 break; \
590 } \
591 case 'h': \
592 spa_pod_builder_fd(builder, va_arg(args, int)); \
593 break; \
594 case 'P': \
595 case 'O': \
596 case 'T': \
597 case 'V': \
598 { \
599 struct spa_pod *pod = va_arg(args, struct spa_pod *); \
600 if (pod == NULL) \
601 spa_pod_builder_none(builder); \
602 else \
603 spa_pod_builder_primitive(builder, pod); \
604 break; \
605 } \
606 case 'Q': \
607 case 'N': \
608 case 'U': \
609 case 'W': \
610 { \
611 struct spa_pod *pod = va_arg(args, struct spa_pod *); \
612 const void *body = va_arg(args, const void *); \
613 spa_pod_builder_primitive_body(builder, pod, \
614 body, pod->size, NULL, 0); \
615 break; \
616 } \
617 } \
618} while(false)
619
620 SPA_API_POD_BUILDER int
621 spa_pod_builder_addv(struct spa_pod_builder *builder, va_list args)
622{
623 int res = 0;
624 struct spa_pod_frame *frame = builder->state.frame;
625 uint32_t ftype = frame ? frame->pod.type : (uint32_t)SPA_TYPE_None;
626
627 do {
628 const char *format;
629 int n_values = 1;
630 struct spa_pod_frame f;
631 bool choice;
632 uint32_t key = 0, flags = 0, offset = 0, type = 0, ctype = 0;
633
634 switch (ftype) {
635 case SPA_TYPE_Object:
636 key = va_arg(args, uint32_t);
637 if (key == 0)
638 goto exit;
639 if (key == SPA_ID_INVALID) {
640 key = va_arg(args, uint32_t);
641 flags = va_arg(args, uint32_t);
642 }
643 break;
644 case SPA_TYPE_Sequence:
645 offset = va_arg(args, uint32_t);
646 type = va_arg(args, uint32_t);
647 if (type == 0)
648 goto exit;
649 break;
650 }
651
652
653 if ((format = va_arg(args, const char *)) == NULL)
654 break;
655
656 choice = *format == '?';
657 if (choice) {
658 ctype = spa_choice_from_id_flags(*++format, &flags);
659 if (*format != '\0')
660 format++;
661 }
662 switch (ftype) {
663 case SPA_TYPE_Object:
664 spa_pod_builder_prop(builder, key, flags);
665 break;
666 case SPA_TYPE_Sequence:
667 spa_pod_builder_control(builder, offset, type);
668 break;
669 }
670 if (choice) {
671 spa_pod_builder_push_choice(builder, &f, ctype, 0);
672 n_values = va_arg(args, int);
673 }
674 while (n_values-- > 0)
675 SPA_POD_BUILDER_COLLECT(builder, *format, args);
676
677 if (choice)
678 spa_pod_builder_pop(builder, &f);
679 } while (true);
680
681 exit:
682 return res;
683}
684
685 SPA_API_POD_BUILDER int spa_pod_builder_add(struct spa_pod_builder *builder, ...)
686{
687 int res;
688 va_list args;
689
690 va_start(args, builder);
691 res = spa_pod_builder_addv(builder, args);
692 va_end(args);
693
694 return res;
695}
696
697#define spa_pod_builder_add_object(b,type,id,...) \
698({ \
699 struct spa_pod_builder *_b = (b); \
700 struct spa_pod_frame _f; \
701 spa_pod_builder_push_object(_b, &_f, type, id); \
702 spa_pod_builder_add(_b, ##__VA_ARGS__, 0); \
703 spa_pod_builder_pop(_b, &_f); \
704})
705
706#define spa_pod_builder_add_struct(b,...) \
707({ \
708 struct spa_pod_builder *_b = (b); \
709 struct spa_pod_frame _f; \
710 spa_pod_builder_push_struct(_b, &_f); \
711 spa_pod_builder_add(_b, ##__VA_ARGS__, NULL); \
712 spa_pod_builder_pop(_b, &_f); \
713})
714
715#define spa_pod_builder_add_sequence(b,unit,...) \
716({ \
717 struct spa_pod_builder *_b = (b); \
718 struct spa_pod_frame _f; \
719 spa_pod_builder_push_sequence(_b, &_f, unit); \
720 spa_pod_builder_add(_b, ##__VA_ARGS__, 0, 0); \
721 spa_pod_builder_pop(_b, &_f); \
722})
723
724
725 SPA_API_POD_BUILDER struct spa_pod *
726 spa_pod_copy(const struct spa_pod *pod)
727{
728 size_t size;
729 struct spa_pod *c;
730
731 size = SPA_POD_SIZE(pod);
732 if ((c = (struct spa_pod *) malloc(size)) == NULL)
733 return NULL;
734 return (struct spa_pod *) memcpy(c, pod, size);
735}
736
740
741#ifdef __cplusplus
742} /* extern "C" */
743#endif
744
745#endif /* SPA_POD_BUILDER_H */
body.h
spa/pod/body.h
args
uint32_t int int const char va_list args
Definition core.h:434
va_end
va_end(args)
r
uint32_t int int const char int r
Definition core.h:447
res
uint32_t int int res
Definition core.h:433
va_start
va_start(args, message)
SPA_CALLBACKS_INIT
#define SPA_CALLBACKS_INIT(_funcs, _data)
Initialize the set of functions funcs as a spa_callbacks, together with _data.
Definition hook.h:144
spa_callbacks_call_res
#define spa_callbacks_call_res(callbacks, type, res, method, vers,...)
Invoke method named method in the callbacks.
Definition hook.h:217
SPA_POD_INIT_Double
#define SPA_POD_INIT_Double(val)
Definition builder.h:321
spa_pod_builder_pop
SPA_API_POD_BUILDER void * spa_pod_builder_pop(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition builder.h:213
spa_pod_builder_corrupted
SPA_API_POD_BUILDER bool spa_pod_builder_corrupted(const struct spa_pod_builder *builder)
Definition builder.h:80
spa_pod_builder_int
SPA_API_POD_BUILDER int spa_pod_builder_int(struct spa_pod_builder *builder, int32_t val)
Definition builder.h:296
spa_pod_builder_long
SPA_API_POD_BUILDER int spa_pod_builder_long(struct spa_pod_builder *builder, int64_t val)
Definition builder.h:305
SPA_POD_INIT_None
#define SPA_POD_INIT_None()
Definition builder.h:260
spa_pod_builder_push_choice
SPA_API_POD_BUILDER int spa_pod_builder_push_choice(struct spa_pod_builder *builder, struct spa_pod_frame *frame, uint32_t type, uint32_t flags)
Definition builder.h:446
spa_pod_builder_pad
SPA_API_POD_BUILDER int spa_pod_builder_pad(struct spa_pod_builder *builder, uint32_t size)
Definition builder.h:195
SPA_POD_INIT
#define SPA_POD_INIT(size, type)
Definition builder.h:257
SPA_POD_IS_VALID
#define SPA_POD_IS_VALID(pod)
Definition pod.h:48
spa_pod_builder_none
SPA_API_POD_BUILDER int spa_pod_builder_none(struct spa_pod_builder *builder)
Definition builder.h:262
spa_choice_from_id_flags
SPA_API_POD_BUILDER uint32_t spa_choice_from_id_flags(char id, uint32_t *flags)
Definition builder.h:517
spa_pod_builder_string
SPA_API_POD_BUILDER int spa_pod_builder_string(struct spa_pod_builder *builder, const char *str)
Definition builder.h:351
spa_pod_builder_id
SPA_API_POD_BUILDER int spa_pod_builder_id(struct spa_pod_builder *builder, uint32_t val)
Definition builder.h:287
SPA_POD_BODY_CONST
#define SPA_POD_BODY_CONST(pod)
Definition pod.h:46
spa_pod_builder_child
SPA_API_POD_BUILDER int spa_pod_builder_child(struct spa_pod_builder *builder, uint32_t size, uint32_t type)
Definition builder.h:268
SPA_POD_INIT_Object
#define SPA_POD_INIT_Object(size, type, id,...)
Definition builder.h:472
spa_pod_builder_double
SPA_API_POD_BUILDER int spa_pod_builder_double(struct spa_pod_builder *builder, double val)
Definition builder.h:323
spa_pod_builder_fraction
SPA_API_POD_BUILDER int spa_pod_builder_fraction(struct spa_pod_builder *builder, uint32_t num, uint32_t denom)
Definition builder.h:408
SPA_POD_BUILDER_FLAG_BODY
#define SPA_POD_BUILDER_FLAG_BODY
Definition builder.h:45
spa_pod_builder_fd
SPA_API_POD_BUILDER int spa_pod_builder_fd(struct spa_pod_builder *builder, int64_t fd)
Definition builder.h:388
spa_pod_builder_pointer
SPA_API_POD_BUILDER int spa_pod_builder_pointer(struct spa_pod_builder *builder, uint32_t type, const void *val)
Definition builder.h:379
SPA_POD_INIT_Sequence
#define SPA_POD_INIT_Sequence(size, unit)
Definition builder.h:496
SPA_POD_INIT_Rectangle
#define SPA_POD_INIT_Rectangle(val)
Definition builder.h:395
spa_pod_builder_get_state
SPA_API_POD_BUILDER void spa_pod_builder_get_state(struct spa_pod_builder *builder, struct spa_pod_builder_state *state)
Definition builder.h:75
SPA_POD_PROP_FLAG_DROP
#define SPA_POD_PROP_FLAG_DROP
drop property, when filtering, both sides need the property or it will be dropped.
Definition pod.h:246
SPA_POD_INIT_Bool
#define SPA_POD_INIT_Bool(val)
Definition builder.h:276
SPA_POD_INIT_String
#define SPA_POD_INIT_String(len)
Definition builder.h:330
spa_pod_builder_control
SPA_API_POD_BUILDER int spa_pod_builder_control(struct spa_pod_builder *builder, uint32_t offset, uint32_t type)
Definition builder.h:511
SPA_POD_INIT_Id
#define SPA_POD_INIT_Id(val)
Definition builder.h:285
spa_pod_builder_push_struct
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_pod_builder_add
SPA_API_POD_BUILDER int spa_pod_builder_add(struct spa_pod_builder *builder,...)
Definition builder.h:713
spa_pod_builder_deref
SPA_API_POD_BUILDER struct spa_pod * spa_pod_builder_deref(struct spa_pod_builder *builder, uint32_t offset)
Definition builder.h:121
SPA_POD_BUILDER_INIT
#define SPA_POD_BUILDER_INIT(buffer, size)
Definition builder.h:72
spa_pod_builder_bool
SPA_API_POD_BUILDER int spa_pod_builder_bool(struct spa_pod_builder *builder, bool val)
Definition builder.h:278
spa_pod_builder_rectangle
SPA_API_POD_BUILDER int spa_pod_builder_rectangle(struct spa_pod_builder *builder, uint32_t width, uint32_t height)
Definition builder.h:398
SPA_POD_INIT_Struct
#define SPA_POD_INIT_Struct(size)
Definition builder.h:459
spa_pod_builder_push_object
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_pod_builder_remove
SPA_API_POD_BUILDER void spa_pod_builder_remove(struct spa_pod_builder *builder, uint32_t size)
Definition builder.h:187
spa_pod_builder_addv
SPA_API_POD_BUILDER int spa_pod_builder_addv(struct spa_pod_builder *builder, va_list args)
Definition builder.h:649
spa_pod_builder_init
SPA_API_POD_BUILDER void spa_pod_builder_init(struct spa_pod_builder *builder, void *data, uint32_t size)
Definition builder.h:102
spa_pod_builder_reset
SPA_API_POD_BUILDER void spa_pod_builder_reset(struct spa_pod_builder *builder, struct spa_pod_builder_state *state)
Definition builder.h:93
spa_pod_builder_string_len
SPA_API_POD_BUILDER int spa_pod_builder_string_len(struct spa_pod_builder *builder, const char *str, uint32_t len)
Definition builder.h:345
spa_pod_copy
SPA_API_POD_BUILDER struct spa_pod * spa_pod_copy(const struct spa_pod *pod)
Copy a pod structure.
Definition builder.h:754
spa_pod_builder_bytes
SPA_API_POD_BUILDER int spa_pod_builder_bytes(struct spa_pod_builder *builder, const void *bytes, uint32_t len)
Definition builder.h:361
SPA_POD_BUILDER_COLLECT
#define SPA_POD_BUILDER_COLLECT(builder, type, args)
Definition builder.h:542
spa_pod_builder_push
SPA_API_POD_BUILDER void spa_pod_builder_push(struct spa_pod_builder *builder, struct spa_pod_frame *frame, const struct spa_pod *pod, uint32_t offset)
Definition builder.h:135
spa_choice_from_id
SPA_API_POD_BUILDER uint32_t spa_choice_from_id(char id)
Definition builder.h:536
spa_pod_builder_primitive_body
SPA_API_POD_BUILDER int spa_pod_builder_primitive_body(struct spa_pod_builder *builder, const struct spa_pod *p, const void *body, uint32_t body_size, const char *suffix, uint32_t suffix_size)
Definition builder.h:231
spa_pod_builder_write_string
SPA_API_POD_BUILDER int spa_pod_builder_write_string(struct spa_pod_builder *builder, const char *str, uint32_t len)
Definition builder.h:333
spa_pod_builder_float
SPA_API_POD_BUILDER int spa_pod_builder_float(struct spa_pod_builder *builder, float val)
Definition builder.h:314
spa_pod_builder_push_array
SPA_API_POD_BUILDER int spa_pod_builder_push_array(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition builder.h:415
SPA_POD_INIT_Int
#define SPA_POD_INIT_Int(val)
Definition builder.h:294
SPA_POD_INIT_Float
#define SPA_POD_INIT_Float(val)
Definition builder.h:312
spa_pod_builder_raw
SPA_API_POD_BUILDER int spa_pod_builder_raw(struct spa_pod_builder *builder, const void *data, uint32_t size)
Definition builder.h:150
SPA_POD_INIT_Bytes
#define SPA_POD_INIT_Bytes(len)
Definition builder.h:358
SPA_POD_INIT_Fd
#define SPA_POD_INIT_Fd(fd)
Definition builder.h:386
spa_pod_builder_push_sequence
SPA_API_POD_BUILDER int spa_pod_builder_push_sequence(struct spa_pod_builder *builder, struct spa_pod_frame *frame, uint32_t unit)
Definition builder.h:500
spa_pod_builder_frame
SPA_API_POD_BUILDER struct spa_pod * spa_pod_builder_frame(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition builder.h:127
spa_pod_builder_reserve_bytes
SPA_API_POD_BUILDER void * spa_pod_builder_reserve_bytes(struct spa_pod_builder *builder, uint32_t len)
Definition builder.h:367
spa_pod_builder_primitive
SPA_API_POD_BUILDER int spa_pod_builder_primitive(struct spa_pod_builder *builder, const struct spa_pod *p)
Definition builder.h:250
spa_pod_builder_raw_padded
SPA_API_POD_BUILDER int spa_pod_builder_raw_padded(struct spa_pod_builder *builder, const void *data, uint32_t size)
Definition builder.h:205
SPA_POD_INIT_Long
#define SPA_POD_INIT_Long(val)
Definition builder.h:303
SPA_POD_INIT_Pointer
#define SPA_POD_INIT_Pointer(type, value)
Definition builder.h:376
SPA_API_POD_BUILDER
#define SPA_API_POD_BUILDER
Definition builder.h:38
SPA_POD_INIT_Fraction
#define SPA_POD_INIT_Fraction(val)
Definition builder.h:405
SPA_POD_BUILDER_FLAG_FIRST
#define SPA_POD_BUILDER_FLAG_FIRST
Definition builder.h:47
SPA_POD_SIZE
#define SPA_POD_SIZE(pod)
Definition pod.h:35
SPA_POD_ALIGN
#define SPA_POD_ALIGN
Definition pod.h:26
spa_pod_builder_set_callbacks
SPA_API_POD_BUILDER void spa_pod_builder_set_callbacks(struct spa_pod_builder *builder, const struct spa_pod_builder_callbacks *callbacks, void *data)
Definition builder.h:86
spa_pod_builder_prop
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_pod_builder_array
SPA_API_POD_BUILDER int spa_pod_builder_array(struct spa_pod_builder *builder, uint32_t child_size, uint32_t child_type, uint32_t n_elems, const void *elems)
Definition builder.h:427
spa_pod_builder_deref_fallback
SPA_API_POD_BUILDER struct spa_pod * spa_pod_builder_deref_fallback(struct spa_pod_builder *builder, uint32_t offset, struct spa_pod *fallback)
Definition builder.h:108
SPA_CHOICE_Step
@ SPA_CHOICE_Step
range with step: default, min, max, step
Definition pod.h:163
SPA_CHOICE_None
@ SPA_CHOICE_None
no choice, first value is current
Definition pod.h:161
SPA_CHOICE_Flags
@ SPA_CHOICE_Flags
flags: first value is flags
Definition pod.h:165
SPA_CHOICE_Range
@ SPA_CHOICE_Range
range: default, min, max
Definition pod.h:162
SPA_CHOICE_Enum
@ SPA_CHOICE_Enum
list: default, alternative,...
Definition pod.h:164
SPA_TYPE_Object
@ SPA_TYPE_Object
Definition type.h:56
SPA_TYPE_None
@ SPA_TYPE_None
Definition type.h:42
SPA_TYPE_Sequence
@ SPA_TYPE_Sequence
Definition type.h:57
SPA_TYPE_Choice
@ SPA_TYPE_Choice
Definition type.h:60
SPA_TYPE_Array
@ SPA_TYPE_Array
Definition type.h:54
SPA_MIN
#define SPA_MIN(a, b)
Definition defs.h:165
SPA_ROUND_UP_N
#define SPA_ROUND_UP_N(num, align)
Definition defs.h:364
SPA_FRACTION
#define SPA_FRACTION(num, denom)
Definition defs.h:136
SPA_ID_INVALID
#define SPA_ID_INVALID
Definition defs.h:250
SPA_FLAG_IS_SET
#define SPA_FLAG_IS_SET(field, flag)
Definition defs.h:90
spa_ptrinside
SPA_API_UTILS_DEFS bool spa_ptrinside(const void *p1, size_t s1, const void *p2, size_t s2, size_t *remaining)
Definition defs.h:402
SPA_FALLTHROUGH
#define SPA_FALLTHROUGH
SPA_FALLTHROUGH is an annotation to suppress compiler warnings about switch cases that fall through w...
Definition defs.h:84
SPA_FLAG_CLEAR
#define SPA_FLAG_CLEAR(field, flag)
Definition defs.h:94
SPA_PTROFF
#define SPA_PTROFF(ptr_, offset_, type_)
Return the address (buffer + offset) as pointer of type.
Definition defs.h:222
SPA_PTRDIFF
#define SPA_PTRDIFF(p1, p2)
Definition defs.h:238
SPA_RECTANGLE
#define SPA_RECTANGLE(width, height)
Definition defs.h:115
hook.h
spa/utils/hook.h
spa_callbacks
Callbacks, contains the structure with functions and the data passed to the functions.
Definition hook.h:126
spa_pod_array_body
Definition pod.h:135
spa_pod_array
Definition pod.h:140
spa_pod_array::pod
struct spa_pod pod
Definition pod.h:141
spa_pod_bool
Definition pod.h:65
spa_pod_bool::pod
struct spa_pod pod
Definition pod.h:66
spa_pod_builder_callbacks
Definition builder.h:55
spa_pod_builder_callbacks::overflow
int(* overflow)(void *data, uint32_t size)
Definition builder.h:60
spa_pod_builder_callbacks::version
uint32_t version
Definition builder.h:58
spa_pod_builder_state
Definition builder.h:42
spa_pod_builder_state::flags
uint32_t flags
Definition builder.h:48
spa_pod_builder_state::offset
uint32_t offset
Definition builder.h:43
spa_pod_builder_state::frame
struct spa_pod_frame * frame
Definition builder.h:49
spa_pod_builder
Definition builder.h:63
spa_pod_builder::_padding
uint32_t _padding
Definition builder.h:66
spa_pod_builder::callbacks
struct spa_callbacks callbacks
Definition builder.h:68
spa_pod_builder::data
void * data
Definition builder.h:64
spa_pod_builder::state
struct spa_pod_builder_state state
Definition builder.h:67
spa_pod_builder::size
uint32_t size
Definition builder.h:65
spa_pod_bytes
Definition pod.h:104
spa_pod_bytes::pod
struct spa_pod pod
Definition pod.h:105
spa_pod_choice_body
Definition pod.h:168
spa_pod_choice_body::type
uint32_t type
type of choice, one of enum spa_choice_type
Definition pod.h:169
spa_pod_choice_body::flags
uint32_t flags
extra flags
Definition pod.h:170
spa_pod_choice
Definition pod.h:176
spa_pod_choice::pod
struct spa_pod pod
Definition pod.h:177
spa_pod_double
Definition pod.h:94
spa_pod_double::pod
struct spa_pod pod
Definition pod.h:95
spa_pod_fd
Definition pod.h:218
spa_pod_fd::pod
struct spa_pod pod
Definition pod.h:219
spa_pod_float
Definition pod.h:88
spa_pod_float::pod
struct spa_pod pod
Definition pod.h:89
spa_pod_fraction
Definition pod.h:114
spa_pod_fraction::pod
struct spa_pod pod
Definition pod.h:115
spa_pod_frame
Definition body.h:38
spa_pod_frame::pod
struct spa_pod pod
Definition body.h:39
spa_pod_frame::offset
uint32_t offset
Definition body.h:41
spa_pod_frame::parent
struct spa_pod_frame * parent
Definition body.h:40
spa_pod_frame::flags
uint32_t flags
Definition body.h:42
spa_pod_id
Definition pod.h:71
spa_pod_id::pod
struct spa_pod pod
Definition pod.h:72
spa_pod_int
Definition pod.h:77
spa_pod_int::pod
struct spa_pod pod
Definition pod.h:78
spa_pod_long
Definition pod.h:83
spa_pod_long::pod
struct spa_pod pod
Definition pod.h:84
spa_pod_object_body
Definition pod.h:196
spa_pod_object
Definition pod.h:202
spa_pod_object::pod
struct spa_pod pod
Definition pod.h:203
spa_pod_pointer
Definition pod.h:213
spa_pod_pointer::pod
struct spa_pod pod
Definition pod.h:214
spa_pod_rectangle
Definition pod.h:109
spa_pod_rectangle::pod
struct spa_pod pod
Definition pod.h:110
spa_pod_sequence_body
Definition pod.h:264
spa_pod_sequence
a sequence of timed controls
Definition pod.h:271
spa_pod_sequence::pod
struct spa_pod pod
Definition pod.h:272
spa_pod_string
Definition pod.h:99
spa_pod_string::pod
struct spa_pod pod
Definition pod.h:100
spa_pod_struct
Definition pod.h:186
spa_pod_struct::pod
struct spa_pod pod
Definition pod.h:187
spa_pod
Definition pod.h:57
spa_pod::type
uint32_t type
Definition pod.h:59
spa_pod::size
uint32_t size
Definition pod.h:58
vararg.h
spa/pod/vararg.h