PipeWire 1.4.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#ifdef __cplusplus
9 extern "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
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 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 SPA_API_POD_BUILDER uint32_t
330 spa_pod_builder_bytes_start(struct spa_pod_builder *builder)
331{
332 uint32_t offset = builder->state.offset;
333 const struct spa_pod_bytes p = SPA_POD_INIT_Bytes(0);
334 spa_pod_builder_raw(builder, &p, sizeof(p));
335 return offset;
336}
337 SPA_API_POD_BUILDER int
338 spa_pod_builder_bytes_append(struct spa_pod_builder *builder, uint32_t offset,
339 const void *data, uint32_t size)
340{
341 int res = spa_pod_builder_raw(builder, data, size);
342 struct spa_pod *pod = spa_pod_builder_deref(builder, offset);
343 if (pod)
344 pod->size += size;
345 return res;
346}
347
348 SPA_API_POD_BUILDER int
349 spa_pod_builder_bytes_end(struct spa_pod_builder *builder, uint32_t offset SPA_UNUSED)
350{
351 return spa_pod_builder_pad(builder, builder->state.offset);
352}
353
354#define SPA_POD_INIT_Pointer(type,value) ((struct spa_pod_pointer){ { sizeof(struct spa_pod_pointer_body), SPA_TYPE_Pointer }, { (type), 0, (value) } })
355
356 SPA_API_POD_BUILDER int
357 spa_pod_builder_pointer(struct spa_pod_builder *builder, uint32_t type, const void *val)
358{
359 const struct spa_pod_pointer p = SPA_POD_INIT_Pointer(type, val);
360 return spa_pod_builder_primitive(builder, &p.pod);
361}
362
363#define SPA_POD_INIT_Fd(fd) ((struct spa_pod_fd){ { sizeof(int64_t), SPA_TYPE_Fd }, (fd) })
364
365 SPA_API_POD_BUILDER int spa_pod_builder_fd(struct spa_pod_builder *builder, int64_t fd)
366{
367 const struct spa_pod_fd p = SPA_POD_INIT_Fd(fd);
368 return spa_pod_builder_primitive(builder, &p.pod);
369}
370
371#define SPA_POD_INIT_Rectangle(val) ((struct spa_pod_rectangle){ { sizeof(struct spa_rectangle), SPA_TYPE_Rectangle }, (val) })
372
373 SPA_API_POD_BUILDER int
374 spa_pod_builder_rectangle(struct spa_pod_builder *builder, uint32_t width, uint32_t height)
375{
376 const struct spa_pod_rectangle p = SPA_POD_INIT_Rectangle(SPA_RECTANGLE(width, height));
377 return spa_pod_builder_primitive(builder, &p.pod);
378}
379
380#define SPA_POD_INIT_Fraction(val) ((struct spa_pod_fraction){ { sizeof(struct spa_fraction), SPA_TYPE_Fraction }, (val) })
381
382 SPA_API_POD_BUILDER int
383 spa_pod_builder_fraction(struct spa_pod_builder *builder, uint32_t num, uint32_t denom)
384{
385 const struct spa_pod_fraction p = SPA_POD_INIT_Fraction(SPA_FRACTION(num, denom));
386 return spa_pod_builder_primitive(builder, &p.pod);
387}
388
389 SPA_API_POD_BUILDER int
390 spa_pod_builder_push_array(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
391{
392 const struct spa_pod_array p =
393 { {sizeof(struct spa_pod_array_body) - sizeof(struct spa_pod), SPA_TYPE_Array},
394 {{0, 0}} };
395 uint32_t offset = builder->state.offset;
396 int res = spa_pod_builder_raw(builder, &p, sizeof(p) - sizeof(struct spa_pod));
397 spa_pod_builder_push(builder, frame, &p.pod, offset);
398 return res;
399}
400
401 SPA_API_POD_BUILDER int
402 spa_pod_builder_array(struct spa_pod_builder *builder,
403 uint32_t child_size, uint32_t child_type, uint32_t n_elems, const void *elems)
404{
405 const struct spa_pod_array p = {
406 {(uint32_t)(sizeof(struct spa_pod_array_body) + n_elems * child_size), SPA_TYPE_Array},
407 {{child_size, child_type}}
408 };
409 int r, res = spa_pod_builder_raw(builder, &p, sizeof(p));
410 if ((r = spa_pod_builder_raw_padded(builder, elems, child_size * n_elems)) < 0)
411 res = r;
412 return res;
413}
414
415#define SPA_POD_INIT_CHOICE_BODY(type, flags, child_size, child_type) \
416 ((struct spa_pod_choice_body) { (type), (flags), { (child_size), (child_type) }})
417
418#define SPA_POD_INIT_Choice(type, ctype, child_type, n_vals, ...) \
419 ((struct { struct spa_pod_choice choice; ctype vals[(n_vals)];}) \
420 { { { (n_vals) * sizeof(ctype) + sizeof(struct spa_pod_choice_body), SPA_TYPE_Choice }, \
421 { (type), 0, { sizeof(ctype), (child_type) } } }, { __VA_ARGS__ } })
422
423 SPA_API_POD_BUILDER int
424 spa_pod_builder_push_choice(struct spa_pod_builder *builder, struct spa_pod_frame *frame,
425 uint32_t type, uint32_t flags)
426{
427 const struct spa_pod_choice p =
428 { {sizeof(struct spa_pod_choice_body) - sizeof(struct spa_pod), SPA_TYPE_Choice},
429 { type, flags, {0, 0}} };
430 uint32_t offset = builder->state.offset;
431 int res = spa_pod_builder_raw(builder, &p, sizeof(p) - sizeof(struct spa_pod));
432 spa_pod_builder_push(builder, frame, &p.pod, offset);
433 return res;
434}
435
436#define SPA_POD_INIT_Struct(size) ((struct spa_pod_struct){ { (size), SPA_TYPE_Struct } })
437
438 SPA_API_POD_BUILDER int
439 spa_pod_builder_push_struct(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
440{
441 const struct spa_pod_struct p = SPA_POD_INIT_Struct(0);
442 uint32_t offset = builder->state.offset;
443 int res = spa_pod_builder_raw(builder, &p, sizeof(p));
444 spa_pod_builder_push(builder, frame, &p.pod, offset);
445 return res;
446}
447
448#define SPA_POD_INIT_Object(size,type,id,...) ((struct spa_pod_object){ { (size), SPA_TYPE_Object }, { (type), (id) }, ##__VA_ARGS__ })
449
450 SPA_API_POD_BUILDER int
451 spa_pod_builder_push_object(struct spa_pod_builder *builder, struct spa_pod_frame *frame,
452 uint32_t type, uint32_t id)
453{
454 const struct spa_pod_object p =
455 SPA_POD_INIT_Object(sizeof(struct spa_pod_object_body), type, id);
456 uint32_t offset = builder->state.offset;
457 int res = spa_pod_builder_raw(builder, &p, sizeof(p));
458 spa_pod_builder_push(builder, frame, &p.pod, offset);
459 return res;
460}
461
462#define SPA_POD_INIT_Prop(key,flags,size,type) \
463 ((struct spa_pod_prop){ (key), (flags), { (size), (type) } })
464
465 SPA_API_POD_BUILDER int
466 spa_pod_builder_prop(struct spa_pod_builder *builder, uint32_t key, uint32_t flags)
467{
468 const struct { uint32_t key; uint32_t flags; } p = { key, flags };
469 return spa_pod_builder_raw(builder, &p, sizeof(p));
470}
471
472#define SPA_POD_INIT_Sequence(size,unit) \
473 ((struct spa_pod_sequence){ { (size), SPA_TYPE_Sequence}, {(unit), 0 } })
474
475 SPA_API_POD_BUILDER int
476 spa_pod_builder_push_sequence(struct spa_pod_builder *builder, struct spa_pod_frame *frame, uint32_t unit)
477{
478 const struct spa_pod_sequence p =
479 SPA_POD_INIT_Sequence(sizeof(struct spa_pod_sequence_body), unit);
480 uint32_t offset = builder->state.offset;
481 int res = spa_pod_builder_raw(builder, &p, sizeof(p));
482 spa_pod_builder_push(builder, frame, &p.pod, offset);
483 return res;
484}
485
486 SPA_API_POD_BUILDER int
487 spa_pod_builder_control(struct spa_pod_builder *builder, uint32_t offset, uint32_t type)
488{
489 const struct { uint32_t offset; uint32_t type; } p = { offset, type };
490 return spa_pod_builder_raw(builder, &p, sizeof(p));
491}
492
493 SPA_API_POD_BUILDER uint32_t spa_choice_from_id(char id)
494{
495 switch (id) {
496 case 'r':
497 return SPA_CHOICE_Range;
498 case 's':
499 return SPA_CHOICE_Step;
500 case 'e':
501 return SPA_CHOICE_Enum;
502 case 'f':
503 return SPA_CHOICE_Flags;
504 case 'n':
505 default:
506 return SPA_CHOICE_None;
507 }
508}
509
510#define SPA_POD_BUILDER_COLLECT(builder,type,args) \
511 do { \
512 switch (type) { \
513 case 'b': \
514 spa_pod_builder_bool(builder, !!va_arg(args, int)); \
515 break; \
516 case 'I': \
517 spa_pod_builder_id(builder, va_arg(args, uint32_t)); \
518 break; \
519 case 'i': \
520 spa_pod_builder_int(builder, va_arg(args, int)); \
521 break; \
522 case 'l': \
523 spa_pod_builder_long(builder, va_arg(args, int64_t)); \
524 break; \
525 case 'f': \
526 spa_pod_builder_float(builder, (float)va_arg(args, double)); \
527 break; \
528 case 'd': \
529 spa_pod_builder_double(builder, va_arg(args, double)); \
530 break; \
531 case 's': \
532 { \
533 char *strval = va_arg(args, char *); \
534 if (strval != NULL) { \
535 size_t len = strlen(strval); \
536 spa_pod_builder_string_len(builder, strval, len); \
537 } \
538 else \
539 spa_pod_builder_none(builder); \
540 break; \
541 } \
542 case 'S': \
543 { \
544 char *strval = va_arg(args, char *); \
545 size_t len = va_arg(args, int); \
546 spa_pod_builder_string_len(builder, strval, len); \
547 break; \
548 } \
549 case 'y': \
550 { \
551 void *ptr = va_arg(args, void *); \
552 int len = va_arg(args, int); \
553 spa_pod_builder_bytes(builder, ptr, len); \
554 break; \
555 } \
556 case 'R': \
557 { \
558 struct spa_rectangle *rectval = \
559 va_arg(args, struct spa_rectangle *); \
560 spa_pod_builder_rectangle(builder, \
561 rectval->width, rectval->height); \
562 break; \
563 } \
564 case 'F': \
565 { \
566 struct spa_fraction *fracval = \
567 va_arg(args, struct spa_fraction *); \
568 spa_pod_builder_fraction(builder, fracval->num, fracval->denom);\
569 break; \
570 } \
571 case 'a': \
572 { \
573 int child_size = va_arg(args, int); \
574 int child_type = va_arg(args, int); \
575 int n_elems = va_arg(args, int); \
576 void *elems = va_arg(args, void *); \
577 spa_pod_builder_array(builder, child_size, \
578 child_type, n_elems, elems); \
579 break; \
580 } \
581 case 'p': \
582 { \
583 int t = va_arg(args, uint32_t); \
584 spa_pod_builder_pointer(builder, t, va_arg(args, void *)); \
585 break; \
586 } \
587 case 'h': \
588 spa_pod_builder_fd(builder, va_arg(args, int)); \
589 break; \
590 case 'P': \
591 case 'O': \
592 case 'T': \
593 case 'V': \
594 { \
595 struct spa_pod *pod = va_arg(args, struct spa_pod *); \
596 if (pod == NULL) \
597 spa_pod_builder_none(builder); \
598 else \
599 spa_pod_builder_primitive(builder, pod); \
600 break; \
601 } \
602 } \
603} while(false)
604
605 SPA_API_POD_BUILDER int
606 spa_pod_builder_addv(struct spa_pod_builder *builder, va_list args)
607{
608 int res = 0;
609 struct spa_pod_frame *frame = builder->state.frame;
610 uint32_t ftype = frame ? frame->pod.type : (uint32_t)SPA_TYPE_None;
611
612 do {
613 const char *format;
614 int n_values = 1;
615 struct spa_pod_frame f;
616 bool choice;
617
618 switch (ftype) {
619 case SPA_TYPE_Object:
620 {
621 uint32_t key = va_arg(args, uint32_t);
622 if (key == 0)
623 goto exit;
624 spa_pod_builder_prop(builder, key, 0);
625 break;
626 }
627 case SPA_TYPE_Sequence:
628 {
629 uint32_t offset = va_arg(args, uint32_t);
630 uint32_t type = va_arg(args, uint32_t);
631 if (type == 0)
632 goto exit;
633 spa_pod_builder_control(builder, offset, type);
634 SPA_FALLTHROUGH
635 }
636 default:
637 break;
638 }
639 if ((format = va_arg(args, const char *)) == NULL)
640 break;
641
642 choice = *format == '?';
643 if (choice) {
644 uint32_t type = spa_choice_from_id(*++format);
645 if (*format != '\0')
646 format++;
647
648 spa_pod_builder_push_choice(builder, &f, type, 0);
649
650 n_values = va_arg(args, int);
651 }
652 while (n_values-- > 0)
653 SPA_POD_BUILDER_COLLECT(builder, *format, args);
654
655 if (choice)
656 spa_pod_builder_pop(builder, &f);
657 } while (true);
658
659 exit:
660 return res;
661}
662
663 SPA_API_POD_BUILDER int spa_pod_builder_add(struct spa_pod_builder *builder, ...)
664{
665 int res;
666 va_list args;
667
668 va_start(args, builder);
669 res = spa_pod_builder_addv(builder, args);
670 va_end(args);
671
672 return res;
673}
674
675#define spa_pod_builder_add_object(b,type,id,...) \
676({ \
677 struct spa_pod_builder *_b = (b); \
678 struct spa_pod_frame _f; \
679 spa_pod_builder_push_object(_b, &_f, type, id); \
680 spa_pod_builder_add(_b, ##__VA_ARGS__, 0); \
681 spa_pod_builder_pop(_b, &_f); \
682})
683
684#define spa_pod_builder_add_struct(b,...) \
685({ \
686 struct spa_pod_builder *_b = (b); \
687 struct spa_pod_frame _f; \
688 spa_pod_builder_push_struct(_b, &_f); \
689 spa_pod_builder_add(_b, ##__VA_ARGS__, NULL); \
690 spa_pod_builder_pop(_b, &_f); \
691})
692
693#define spa_pod_builder_add_sequence(b,unit,...) \
694({ \
695 struct spa_pod_builder *_b = (b); \
696 struct spa_pod_frame _f; \
697 spa_pod_builder_push_sequence(_b, &_f, unit); \
698 spa_pod_builder_add(_b, ##__VA_ARGS__, 0, 0); \
699 spa_pod_builder_pop(_b, &_f); \
700})
701
702
703 SPA_API_POD_BUILDER struct spa_pod *
704 spa_pod_copy(const struct spa_pod *pod)
705{
706 size_t size;
707 struct spa_pod *c;
708
709 size = SPA_POD_SIZE(pod);
710 if ((c = (struct spa_pod *) malloc(size)) == NULL)
711 return NULL;
712 return (struct spa_pod *) memcpy(c, pod, size);
713}
714
718
719#ifdef __cplusplus
720} /* extern "C" */
721#endif
722
723#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:450
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:476
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:409
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:389
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:380
SPA_POD_INIT_Sequence
#define SPA_POD_INIT_Sequence(size, unit)
Definition builder.h:500
SPA_POD_INIT_Rectangle
#define SPA_POD_INIT_Rectangle(val)
Definition builder.h:396
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_builder_bytes_append
SPA_API_POD_BUILDER int spa_pod_builder_bytes_append(struct spa_pod_builder *builder, uint32_t offset, const void *data, uint32_t size)
Definition builder.h:360
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_bytes_end
SPA_API_POD_BUILDER int spa_pod_builder_bytes_end(struct spa_pod_builder *builder, uint32_t offset 1)
Definition builder.h:371
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:515
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:466
spa_pod_builder_add
SPA_API_POD_BUILDER int spa_pod_builder_add(struct spa_pod_builder *builder,...)
Definition builder.h:691
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:399
SPA_POD_INIT_Struct
#define SPA_POD_INIT_Struct(size)
Definition builder.h:463
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:479
spa_pod_builder_addv
SPA_API_POD_BUILDER int spa_pod_builder_addv(struct spa_pod_builder *builder, va_list args)
Definition builder.h:634
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:732
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:538
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:521
spa_pod_builder_bytes_start
SPA_API_POD_BUILDER uint32_t spa_pod_builder_bytes_start(struct spa_pod_builder *builder)
Definition builder.h:352
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:416
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:387
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:504
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:377
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:406
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:494
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:428
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_UNUSED
#define SPA_UNUSED
Definition defs.h:307
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