pipewire/spa/include/spa/pod/pod.h
Wim Taymans cc842cbdc8 Type changes
Only allow properties inside objects, this makes it easier to
iterate the object, which is needed for efficiently processing
control streams.
Add a choice type to mark variable properties.
SPA_TYPE_Enum -> SPA_TYPE_Id to avoid confusion with choice enum
Make it easier to allocate and initialize properties on the stack
Make more efficient methods to make objects.
2018-09-05 16:41:07 +02:00

253 lines
7 KiB
C

/* Simple Plugin API
* Copyright (C) 2017 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __SPA_POD_H__
#define __SPA_POD_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <stdarg.h>
#include <spa/utils/defs.h>
#include <spa/utils/type.h>
#ifndef SPA_POD_MAX_DEPTH
#define SPA_POD_MAX_DEPTH 16
#endif
#define SPA_POD_BODY_SIZE(pod) (((struct spa_pod*)(pod))->size)
#define SPA_POD_TYPE(pod) (((struct spa_pod*)(pod))->type)
#define SPA_POD_SIZE(pod) (sizeof(struct spa_pod) + SPA_POD_BODY_SIZE(pod))
#define SPA_POD_CONTENTS_SIZE(type,pod) (SPA_POD_SIZE(pod)-sizeof(type))
#define SPA_POD_CONTENTS(type,pod) SPA_MEMBER((pod),sizeof(type),void)
#define SPA_POD_CONTENTS_CONST(type,pod) SPA_MEMBER((pod),sizeof(type),const void)
#define SPA_POD_BODY(pod) SPA_MEMBER((pod),sizeof(struct spa_pod),void)
#define SPA_POD_BODY_CONST(pod) SPA_MEMBER((pod),sizeof(struct spa_pod),const void)
struct spa_pod {
uint32_t size;
uint32_t type; /* a basic id of enum spa_type */
};
#define SPA_POD_VALUE(type,pod) (((type*)pod)->value)
struct spa_pod_bool {
struct spa_pod pod;
int32_t value;
int32_t __padding;
};
struct spa_pod_id {
struct spa_pod pod;
uint32_t value;
int32_t __padding;
};
struct spa_pod_int {
struct spa_pod pod;
int32_t value;
int32_t __padding;
};
struct spa_pod_long {
struct spa_pod pod;
int64_t value;
};
struct spa_pod_float {
struct spa_pod pod;
float value;
int32_t __padding;
};
struct spa_pod_double {
struct spa_pod pod;
double value;
};
struct spa_pod_string {
struct spa_pod pod;
/* value here */
};
struct spa_pod_bytes {
struct spa_pod pod;
/* value here */
};
struct spa_pod_rectangle {
struct spa_pod pod;
struct spa_rectangle value;
};
struct spa_pod_fraction {
struct spa_pod pod;
struct spa_fraction value;
};
struct spa_pod_bitmap {
struct spa_pod pod;
/* array of uint8_t follows with the bitmap */
};
#define SPA_POD_ARRAY_CHILD(arr) (&((struct spa_pod_array*)(arr))->body.child)
#define SPA_POD_ARRAY_TYPE(arr) (SPA_POD_TYPE(SPA_POD_ARRAY_CHILD(arr)))
#define SPA_POD_ARRAY_SIZE(arr) (SPA_POD_BODY_SIZE(SPA_POD_ARRAY_CHILD(arr)))
#define SPA_POD_ARRAY_N_VALUES(arr) ((SPA_POD_BODY_SIZE(arr) - sizeof(struct spa_pod_array_body)) / SPA_POD_ARRAY_SIZE(arr))
struct spa_pod_array_body {
struct spa_pod child;
/* array with elements of child.size follows */
};
struct spa_pod_array {
struct spa_pod pod;
struct spa_pod_array_body body;
};
#define SPA_POD_CHOICE_CHILD(choice) (&((struct spa_pod_choice*)(choice))->body.child)
#define SPA_POD_CHOICE_TYPE(choice) (((struct spa_pod_choice*)(choice))->body.type)
#define SPA_POD_CHOICE_VALUE_TYPE(choice) (SPA_POD_TYPE(SPA_POD_CHOICE_CHILD(choice)))
#define SPA_POD_CHOICE_VALUE_SIZE(choice) (SPA_POD_BODY_SIZE(SPA_POD_CHOICE_CHILD(choice)))
#define SPA_POD_CHOICE_N_VALUES(choice) ((SPA_POD_BODY_SIZE(choice) - sizeof(struct spa_pod_choice_body)) / SPA_POD_CHOICE_VALUE_SIZE(choice))
#define SPA_POD_CHOICE_VALUES(choice) (SPA_POD_CONTENTS(struct spa_pod_choice, choice))
enum spa_choice_type {
SPA_CHOICE_None, /**< no choice, first value is current */
SPA_CHOICE_Range, /**< range: default, min, max */
SPA_CHOICE_Step, /**< range with step: default, min, max, step */
SPA_CHOICE_Enum, /**< list: default, alternative,... */
SPA_CHOICE_Flags, /**< flags: default, possible flags,... */
};
struct spa_pod_choice_body {
uint32_t type; /**< type of choice, one of enum spa_choice_type */
uint32_t flags; /**< extra flags */
struct spa_pod child;
/* array with elements of child.size follows */
};
struct spa_pod_choice {
struct spa_pod pod;
struct spa_pod_choice_body body;
};
struct spa_pod_struct {
struct spa_pod pod;
/* one or more spa_pod follow */
};
struct spa_pod_object_body {
uint32_t type; /**< one of enum spa_type */
uint32_t id; /**< id of the object, depends on the object type */
/* contents follow, series of spa_pod */
};
struct spa_pod_object {
struct spa_pod pod;
struct spa_pod_object_body body;
};
static inline bool spa_pod_is_object_type(const struct spa_pod *pod, uint32_t type)
{
return (pod && pod->type == SPA_TYPE_Object
&& ((struct spa_pod_object *) pod)->body.type == type);
}
static inline bool spa_pod_is_object_id(const struct spa_pod *pod, uint32_t id)
{
return (pod && pod->type == SPA_TYPE_Object
&& ((struct spa_pod_object *) pod)->body.id == id);
}
struct spa_pod_pointer_body {
uint32_t type; /**< pointer id, one of enum spa_type */
const void *value;
};
struct spa_pod_pointer {
struct spa_pod pod;
struct spa_pod_pointer_body body;
};
struct spa_pod_fd {
struct spa_pod pod;
int value;
};
static inline struct spa_pod *spa_pod_get_values(const struct spa_pod *pod, uint32_t *n_vals, uint32_t *choice)
{
if (pod->type == SPA_TYPE_Choice) {
*choice = SPA_POD_CHOICE_TYPE(pod);
*n_vals = *choice == SPA_CHOICE_None ? 1 : SPA_POD_CHOICE_N_VALUES(pod);
return (struct spa_pod*)SPA_POD_CHOICE_CHILD(pod);
} else {
*n_vals = 1;
*choice = SPA_CHOICE_None;
return (struct spa_pod*)pod;
}
}
#define SPA_POD_PROP_SIZE(prop) (sizeof(struct spa_pod_prop) + (prop)->value.size)
struct spa_pod_prop {
uint32_t key; /**< key of property, list of valid keys depends on the
* object type */
#define SPA_POD_PROP_FLAG_OPTIONAL (1 << 0) /**< property value is optional */
#define SPA_POD_PROP_FLAG_READONLY (1 << 1) /**< property is readonly */
#define SPA_POD_PROP_FLAG_DEPRECATED (1 << 2) /**< property is deprecated */
#define SPA_POD_PROP_FLAG_INFO (1 << 3) /**< property is informational and is not
* used when filtering */
#define SPA_POD_PROP_FLAG_CONTROLLABLE (1 << 4) /**< property can be controlled */
uint32_t flags;
struct spa_pod value;
/* value follows */
};
#define SPA_POD_CONTROL_SIZE(ev) (sizeof(struct spa_pod_control) + (ev)->value.size)
/* controls can be inside a sequence and mark timed values */
struct spa_pod_control {
uint32_t offset; /**< media offset */
uint32_t type; /**< type of control, enum spa_control_type */
struct spa_pod value; /**< control value, depends on type */
/* value contents follow */
};
struct spa_pod_sequence_body {
uint32_t unit;
uint32_t pad;
/* series of struct spa_pod_control follows */
};
/** a sequence of timed controls */
struct spa_pod_sequence {
struct spa_pod pod;
struct spa_pod_sequence_body body;
};
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* __SPA_POD_H__ */