PipeWire: spa/pod/builder.h Source File

PipeWire 1.4.1 |

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