Add new pod parser and builder

Add a new pod builder and parser that is less verbose and
a little more powerful.
This commit is contained in:
Wim Taymans 2017-09-21 18:57:41 +02:00
parent 3e49aec61b
commit 24d80e5c00
54 changed files with 3616 additions and 2817 deletions

View file

@ -89,7 +89,7 @@ static void fill_item(struct impl *this, struct item *item, struct udev_device *
{
const char *str, *name;
struct spa_pod_builder b = { NULL, };
struct spa_pod_frame f[3];
struct type *t = &this->type;
if (item->udevice)
udev_device_unref(item->udevice);
@ -112,59 +112,43 @@ static void fill_item(struct impl *this, struct item *item, struct udev_device *
spa_pod_builder_init(&b, this->item_buffer, sizeof(this->item_buffer));
spa_pod_builder_push_object(&b, &f[0], 0, this->type.monitor.MonitorItem);
spa_pod_builder_add(&b,
"<", 0, t->monitor.MonitorItem,
":", t->monitor.id, "s", udev_device_get_syspath(item->udevice),
":", t->monitor.flags, "i", 0,
":", t->monitor.state, "i", SPA_MONITOR_ITEM_STATE_AVAILABLE,
":", t->monitor.name, "s", name,
":", t->monitor.klass, "s", "Video/Source",
":", t->monitor.factory, "p", t->handle_factory, &spa_v4l2_source_factory,
":", t->monitor.info, "[",
NULL);
spa_pod_builder_add(&b,
SPA_POD_PROP(&f[1], this->type.monitor.id, 0, SPA_POD_TYPE_STRING, 1,
udev_device_get_syspath(item->udevice)),
SPA_POD_PROP(&f[1], this->type.monitor.flags, 0, SPA_POD_TYPE_INT, 1,
0),
SPA_POD_PROP(&f[1], this->type.monitor.state, 0, SPA_POD_TYPE_INT, 1,
SPA_MONITOR_ITEM_STATE_AVAILABLE),
SPA_POD_PROP(&f[1], this->type.monitor.name, 0, SPA_POD_TYPE_STRING, 1,
name),
SPA_POD_PROP(&f[1], this->type.monitor.klass, 0, SPA_POD_TYPE_STRING, 1,
"Video/Source"),
SPA_POD_PROP(&f[1], this->type.monitor.factory, 0, SPA_POD_TYPE_POINTER, 1,
this->type.handle_factory, &spa_v4l2_source_factory),
0);
spa_pod_builder_add(&b,
SPA_POD_TYPE_PROP, &f[1], this->type.monitor.info, 0,
SPA_POD_TYPE_STRUCT, 1, &f[2], 0);
spa_pod_builder_add(&b,
SPA_POD_TYPE_STRING, "udev-probed", SPA_POD_TYPE_STRING, "1",
SPA_POD_TYPE_STRING, "device.api", SPA_POD_TYPE_STRING, "v4l2",
SPA_POD_TYPE_STRING, "device.path", SPA_POD_TYPE_STRING,
udev_device_get_devnode(item->udevice), 0);
"s", "udev-probed", "s", "1",
"s", "device.api", "s", "v4l2",
"s", "device.path", "s", udev_device_get_devnode(item->udevice),
NULL);
str = udev_device_get_property_value(item->udevice, "ID_PATH");
if (!(str && *str))
str = udev_device_get_syspath(item->udevice);
if (str && *str) {
spa_pod_builder_add(&b, SPA_POD_TYPE_STRING, "device.bus_path", SPA_POD_TYPE_STRING,
str, 0);
spa_pod_builder_add(&b, "s", "device.bus_path", "s", str, 0);
}
if ((str = udev_device_get_syspath(item->udevice)) && *str) {
spa_pod_builder_add(&b, SPA_POD_TYPE_STRING, "sysfs.path", SPA_POD_TYPE_STRING,
str, 0);
spa_pod_builder_add(&b, "s", "sysfs.path", "s", str, 0);
}
if ((str = udev_device_get_property_value(item->udevice, "ID_ID")) && *str) {
spa_pod_builder_add(&b, SPA_POD_TYPE_STRING, "udev.id", SPA_POD_TYPE_STRING,
str, 0);
spa_pod_builder_add(&b, "s", "udev.id", "s", str, 0);
}
if ((str = udev_device_get_property_value(item->udevice, "ID_BUS")) && *str) {
spa_pod_builder_add(&b, SPA_POD_TYPE_STRING, "device.bus", SPA_POD_TYPE_STRING,
str, 0);
spa_pod_builder_add(&b, "s", "device.bus", "s", str, 0);
}
if ((str = udev_device_get_property_value(item->udevice, "SUBSYSTEM")) && *str) {
spa_pod_builder_add(&b, SPA_POD_TYPE_STRING, "device.subsystem",
SPA_POD_TYPE_STRING, str, 0);
spa_pod_builder_add(&b, "s", "device.subsystem", "s", str, 0);
}
if ((str = udev_device_get_property_value(item->udevice, "ID_VENDOR_ID")) && *str) {
spa_pod_builder_add(&b, SPA_POD_TYPE_STRING, "device.vendor.id",
SPA_POD_TYPE_STRING, str, 0);
spa_pod_builder_add(&b, "s", "device.vendor.id", "s", str, 0);
}
str = udev_device_get_property_value(item->udevice, "ID_VENDOR_FROM_DATABASE");
if (!(str && *str)) {
@ -174,29 +158,20 @@ static void fill_item(struct impl *this, struct item *item, struct udev_device *
}
}
if (str && *str) {
spa_pod_builder_add(&b, SPA_POD_TYPE_STRING, "device.vendor.name",
SPA_POD_TYPE_STRING, str, 0);
spa_pod_builder_add(&b, "s", "device.vendor.name", "s", str, 0);
}
if ((str = udev_device_get_property_value(item->udevice, "ID_MODEL_ID")) && *str) {
spa_pod_builder_add(&b, SPA_POD_TYPE_STRING, "device.product.id",
SPA_POD_TYPE_STRING, str, 0);
spa_pod_builder_add(&b, "s", "device.product.id", "s", str, 0);
}
spa_pod_builder_add(&b, SPA_POD_TYPE_STRING, "device.product.name", SPA_POD_TYPE_STRING,
name, 0);
spa_pod_builder_add(&b, "s", "device.product.name", "s", name, 0);
if ((str = udev_device_get_property_value(item->udevice, "ID_SERIAL")) && *str) {
spa_pod_builder_add(&b, SPA_POD_TYPE_STRING, "device.serial", SPA_POD_TYPE_STRING,
str, 0);
spa_pod_builder_add(&b, "s", "device.serial", "s", str, 0);
}
if ((str = udev_device_get_property_value(item->udevice, "ID_V4L_CAPABILITIES")) && *str) {
spa_pod_builder_add(&b, SPA_POD_TYPE_STRING, "device.capabilities",
SPA_POD_TYPE_STRING, str, 0);
spa_pod_builder_add(&b, "s", "device.capabilities", "s", str, 0);
}
spa_pod_builder_add(&b, -SPA_POD_TYPE_STRUCT, &f[2], -SPA_POD_TYPE_PROP, &f[1], 0);
spa_pod_builder_pop(&b, &f[0]);
item->item = SPA_POD_BUILDER_DEREF(&b, f[0].ref, struct spa_monitor_item);
item->item = spa_pod_builder_add(&b, "]>", NULL);
}
static void impl_on_fd_events(struct spa_source *source)
@ -207,7 +182,6 @@ static void impl_on_fd_events(struct spa_source *source)
const char *action;
uint32_t type;
struct spa_pod_builder b = { NULL, };
struct spa_pod_frame f[1];
uint8_t buffer[4096];
dev = udev_monitor_receive_device(this->umonitor);
@ -228,9 +202,8 @@ static void impl_on_fd_events(struct spa_source *source)
return;
spa_pod_builder_init(&b, buffer, sizeof(buffer));
spa_pod_builder_object(&b, &f[0], 0, type, SPA_POD_TYPE_POD, this->uitem.item);
event = spa_pod_builder_object(&b, 0, type, "P", this->uitem.item);
event = SPA_POD_BUILDER_DEREF(&b, f[0].ref, struct spa_event);
this->callbacks->event(this->callbacks_data, event);
}

View file

@ -166,42 +166,25 @@ struct impl {
#define CHECK_PORT(this,direction,port_id) ((direction) == SPA_DIRECTION_OUTPUT && (port_id) == 0)
#define PROP(f,key,type,...) \
SPA_POD_PROP (f,key,0,type,1,__VA_ARGS__)
#define PROP_R(f,key,type,...) \
SPA_POD_PROP (f,key,SPA_POD_PROP_FLAG_READONLY,type,1,__VA_ARGS__)
#define PROP_MM(f,key,type,...) \
SPA_POD_PROP (f,key,SPA_POD_PROP_RANGE_MIN_MAX,type,3,__VA_ARGS__)
#define PROP_U_MM(f,key,type,...) \
SPA_POD_PROP (f,key,SPA_POD_PROP_FLAG_UNSET | \
SPA_POD_PROP_RANGE_MIN_MAX,type,3,__VA_ARGS__)
#define PROP_EN(f,key,type,n,...) \
SPA_POD_PROP (f,key,SPA_POD_PROP_RANGE_ENUM,type,n,__VA_ARGS__)
#define PROP_U_EN(f,key,type,n,...) \
SPA_POD_PROP (f,key,SPA_POD_PROP_FLAG_UNSET | \
SPA_POD_PROP_RANGE_ENUM,type,n,__VA_ARGS__)
#include "v4l2-utils.c"
static int impl_node_get_props(struct spa_node *node, struct spa_props **props)
{
struct impl *this;
struct spa_pod_builder b = { NULL, };
struct spa_pod_frame f[2];
struct type *t;
spa_return_val_if_fail(node != NULL, SPA_RESULT_INVALID_ARGUMENTS);
spa_return_val_if_fail(props != NULL, SPA_RESULT_INVALID_ARGUMENTS);
this = SPA_CONTAINER_OF(node, struct impl, node);
t = &this->type;
spa_pod_builder_init(&b, this->props_buffer, sizeof(this->props_buffer));
spa_pod_builder_props(&b, &f[0], this->type.props,
PROP(&f[1], this->type.prop_device, -SPA_POD_TYPE_STRING,
this->props.device, sizeof(this->props.device)),
PROP_R(&f[1], this->type.prop_device_name, -SPA_POD_TYPE_STRING,
this->props.device_name, sizeof(this->props.device_name)),
PROP_R(&f[1], this->type.prop_device_fd, SPA_POD_TYPE_INT,
this->props.device_fd));
*props = SPA_POD_BUILDER_DEREF(&b, f[0].ref, struct spa_props);
*props = spa_pod_builder_props(&b, t->props,
":", t->prop_device, "S", this->props.device, sizeof(this->props.device),
":", t->prop_device_name, "S-r", this->props.device_name, sizeof(this->props.device_name),
":", t->prop_device_fd, "i-r", this->props.device_fd);
return SPA_RESULT_OK;
}
@ -209,18 +192,20 @@ static int impl_node_get_props(struct spa_node *node, struct spa_props **props)
static int impl_node_set_props(struct spa_node *node, const struct spa_props *props)
{
struct impl *this;
struct type *t;
spa_return_val_if_fail(node != NULL, SPA_RESULT_INVALID_ARGUMENTS);
this = SPA_CONTAINER_OF(node, struct impl, node);
t = &this->type;
if (props == NULL) {
reset_props(&this->props);
return SPA_RESULT_OK;
} else {
spa_props_query(props, this->type.prop_device, -SPA_POD_TYPE_STRING,
this->props.device, sizeof(this->props.device), 0);
}
spa_props_parse(props,
":", t->prop_device, "?S", this->props.device, sizeof(this->props.device), NULL);
return SPA_RESULT_OK;
}
@ -456,10 +441,12 @@ static int impl_node_port_set_format(struct spa_node *node,
struct impl *this;
struct port *state;
struct spa_video_info info;
struct type *t;
spa_return_val_if_fail(node != NULL, SPA_RESULT_INVALID_ARGUMENTS);
this = SPA_CONTAINER_OF(node, struct impl, node);
t = &this->type;
spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), SPA_RESULT_INVALID_PORT);
@ -475,13 +462,13 @@ static int impl_node_port_set_format(struct spa_node *node,
info.media_type = SPA_FORMAT_MEDIA_TYPE(format);
info.media_subtype = SPA_FORMAT_MEDIA_SUBTYPE(format);
if (info.media_type != this->type.media_type.video) {
if (info.media_type != t->media_type.video) {
spa_log_error(this->log, "media type must be video");
return SPA_RESULT_INVALID_MEDIA_TYPE;
}
if (info.media_subtype == this->type.media_subtype.raw) {
if (!spa_format_video_raw_parse(format, &info.info.raw, &this->type.format_video)) {
if (info.media_subtype == t->media_subtype.raw) {
if (spa_format_video_raw_parse(format, &info.info.raw, &t->format_video) < 0) {
spa_log_error(this->log, "can't parse video raw");
return SPA_RESULT_INVALID_MEDIA_TYPE;
}
@ -492,8 +479,8 @@ static int impl_node_port_set_format(struct spa_node *node,
info.info.raw.size.width == state->current_format.info.raw.size.width &&
info.info.raw.size.height == state->current_format.info.raw.size.height)
return SPA_RESULT_OK;
} else if (info.media_subtype == this->type.media_subtype_video.mjpg) {
if (!spa_format_video_mjpg_parse(format, &info.info.mjpg, &this->type.format_video))
} else if (info.media_subtype == t->media_subtype_video.mjpg) {
if (spa_format_video_mjpg_parse(format, &info.info.mjpg, &t->format_video) < 0)
return SPA_RESULT_INVALID_MEDIA_TYPE;
if (state->have_format && info.media_type == state->current_format.media_type &&
@ -501,8 +488,8 @@ static int impl_node_port_set_format(struct spa_node *node,
info.info.mjpg.size.width == state->current_format.info.mjpg.size.width &&
info.info.mjpg.size.height == state->current_format.info.mjpg.size.height)
return SPA_RESULT_OK;
} else if (info.media_subtype == this->type.media_subtype_video.h264) {
if (!spa_format_video_h264_parse(format, &info.info.h264, &this->type.format_video))
} else if (info.media_subtype == t->media_subtype_video.h264) {
if (spa_format_video_h264_parse(format, &info.info.h264, &t->format_video) < 0)
return SPA_RESULT_INVALID_MEDIA_TYPE;
if (state->have_format && info.media_type == state->current_format.media_type &&
@ -537,12 +524,14 @@ static int impl_node_port_get_format(struct spa_node *node,
struct impl *this;
struct port *state;
struct spa_pod_builder b = { NULL, };
struct spa_pod_frame f[2];
struct spa_pod_frame f[1];
struct type *t;
spa_return_val_if_fail(node != NULL, SPA_RESULT_INVALID_ARGUMENTS);
spa_return_val_if_fail(format != NULL, SPA_RESULT_INVALID_ARGUMENTS);
this = SPA_CONTAINER_OF(node, struct impl, node);
t = &this->type;
spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), SPA_RESULT_INVALID_PORT);
@ -554,36 +543,28 @@ static int impl_node_port_get_format(struct spa_node *node,
b.data = state->format_buffer;
b.size = sizeof(state->format_buffer);
spa_pod_builder_push_format(&b, &f[0], this->type.format,
spa_pod_builder_push_format(&b, &f[0], t->format,
state->current_format.media_type,
state->current_format.media_subtype);
if (state->current_format.media_subtype == this->type.media_subtype.raw) {
if (state->current_format.media_subtype == t->media_subtype.raw) {
spa_pod_builder_add(&b,
PROP(&f[1], this->type.format_video.format, SPA_POD_TYPE_ID,
state->current_format.info.raw.format),
PROP(&f[1], this->type.format_video.size, -SPA_POD_TYPE_RECTANGLE,
&state->current_format.info.raw.size),
PROP(&f[1], this->type.format_video.framerate, -SPA_POD_TYPE_FRACTION,
&state->current_format.info.raw.framerate), 0);
} else if (state->current_format.media_subtype == this->type.media_subtype_video.mjpg ||
state->current_format.media_subtype == this->type.media_subtype_video.jpeg) {
":", t->format_video.format, "I", state->current_format.info.raw.format,
":", t->format_video.size, "R", &state->current_format.info.raw.size,
":", t->format_video.framerate, "F", &state->current_format.info.raw.framerate, 0);
} else if (state->current_format.media_subtype == t->media_subtype_video.mjpg ||
state->current_format.media_subtype == t->media_subtype_video.jpeg) {
spa_pod_builder_add(&b,
PROP(&f[1], this->type.format_video.size, -SPA_POD_TYPE_RECTANGLE,
&state->current_format.info.mjpg.size),
PROP(&f[1], this->type.format_video.framerate, -SPA_POD_TYPE_FRACTION,
&state->current_format.info.mjpg.framerate), 0);
} else if (state->current_format.media_subtype == this->type.media_subtype_video.h264) {
":", t->format_video.size, "R", &state->current_format.info.mjpg.size,
":", t->format_video.framerate, "F", &state->current_format.info.mjpg.framerate, 0);
} else if (state->current_format.media_subtype == t->media_subtype_video.h264) {
spa_pod_builder_add(&b,
PROP(&f[1], this->type.format_video.size, -SPA_POD_TYPE_RECTANGLE,
&state->current_format.info.h264.size),
PROP(&f[1], this->type.format_video.framerate, -SPA_POD_TYPE_FRACTION,
&state->current_format.info.h264.framerate), 0);
":", t->format_video.size, "R", &state->current_format.info.h264.size,
":", t->format_video.framerate, "F", &state->current_format.info.h264.framerate, 0);
} else
return SPA_RESULT_NO_FORMAT;
spa_pod_builder_pop(&b, &f[0]);
*format = SPA_POD_BUILDER_DEREF(&b, f[0].ref, struct spa_format);
return SPA_RESULT_OK;
@ -618,12 +599,13 @@ static int impl_node_port_enum_params(struct spa_node *node,
struct impl *this;
struct port *state;
struct spa_pod_builder b = { NULL, };
struct spa_pod_frame f[2];
struct type *t;
spa_return_val_if_fail(node != NULL, SPA_RESULT_INVALID_ARGUMENTS);
spa_return_val_if_fail(param != NULL, SPA_RESULT_INVALID_ARGUMENTS);
this = SPA_CONTAINER_OF(node, struct impl, node);
t = &this->type;
spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), SPA_RESULT_INVALID_PORT);
@ -633,30 +615,23 @@ static int impl_node_port_enum_params(struct spa_node *node,
switch (index) {
case 0:
spa_pod_builder_object(&b, &f[0], 0, this->type.param_alloc_buffers.Buffers,
PROP(&f[1], this->type.param_alloc_buffers.size, SPA_POD_TYPE_INT,
state->fmt.fmt.pix.sizeimage),
PROP(&f[1], this->type.param_alloc_buffers.stride, SPA_POD_TYPE_INT,
state->fmt.fmt.pix.bytesperline),
PROP_U_MM(&f[1], this->type.param_alloc_buffers.buffers, SPA_POD_TYPE_INT,
MAX_BUFFERS, 2, MAX_BUFFERS),
PROP(&f[1], this->type.param_alloc_buffers.align, SPA_POD_TYPE_INT, 16));
*param = spa_pod_builder_param(&b, t->param_alloc_buffers.Buffers,
":", t->param_alloc_buffers.size, "i", state->fmt.fmt.pix.sizeimage,
":", t->param_alloc_buffers.stride, "i", state->fmt.fmt.pix.bytesperline,
":", t->param_alloc_buffers.buffers, "iru", MAX_BUFFERS,
2, 2, MAX_BUFFERS,
":", t->param_alloc_buffers.align, "i", 16);
break;
case 1:
spa_pod_builder_object(&b, &f[0], 0, this->type.param_alloc_meta_enable.MetaEnable,
PROP(&f[1], this->type.param_alloc_meta_enable.type, SPA_POD_TYPE_ID,
this->type.meta.Header),
PROP(&f[1], this->type.param_alloc_meta_enable.size, SPA_POD_TYPE_INT,
sizeof(struct spa_meta_header)));
*param = spa_pod_builder_param(&b, t->param_alloc_meta_enable.MetaEnable,
":", t->param_alloc_meta_enable.type, "I", t->meta.Header,
":", t->param_alloc_meta_enable.size, "i", sizeof(struct spa_meta_header));
break;
default:
return SPA_RESULT_NOT_IMPLEMENTED;
}
*param = SPA_POD_BUILDER_DEREF(&b, f[0].ref, struct spa_param);
return SPA_RESULT_OK;
}

View file

@ -680,12 +680,11 @@ spa_v4l2_enum_format(struct impl *this,
if (media_subtype == this->type.media_subtype.raw) {
spa_pod_builder_add(&b,
PROP(&f[1], this->type.format_video.format, SPA_POD_TYPE_ID,
video_format), 0);
":", this->type.format_video.format, "I", video_format, 0);
}
spa_pod_builder_add(&b,
PROP(&f[1], this->type.format_video.size, SPA_POD_TYPE_RECTANGLE,
state->frmsize.discrete.width, state->frmsize.discrete.height), 0);
":", this->type.format_video.size, "R", &SPA_RECTANGLE(state->frmsize.discrete.width,
state->frmsize.discrete.height), 0);
spa_pod_builder_push_prop(&b, &f[1], this->type.format_video.framerate,
SPA_POD_PROP_RANGE_NONE | SPA_POD_PROP_FLAG_UNSET);