Remove dynamic types

Do not use dynamic types anymore. The reason is that it's difficult:

- to maintain a shared type database over a network.
- the extra overhead when translating between processes and for
  maintaining the translation tables.
- race conditions in translating in RT-threads, this is a problem
  because we want to make event streams.

We now have simple enums with types and extension points for all
types. This is also nicer to use in general.
We don't need the mapper anymore or pass strings around as types.
There is a parallel type info system to get more info about ids and
enums and their hierarchy. It can also be used for debugging.
This commit is contained in:
Wim Taymans 2018-08-23 17:47:57 +02:00
parent e6977fa178
commit fca3e1d85d
162 changed files with 5200 additions and 7461 deletions

View file

@ -22,7 +22,6 @@
#include <stddef.h>
#include <spa/support/log.h>
#include <spa/support/type-map.h>
#include <spa/utils/list.h>
#include <spa/node/node.h>
#include <spa/node/io.h>
@ -72,55 +71,10 @@ struct port {
struct spa_list empty;
};
struct type {
uint32_t node;
uint32_t format;
uint32_t props;
uint32_t prop_volume;
uint32_t prop_mute;
struct spa_type_io io;
struct spa_type_param param;
struct spa_type_meta meta;
struct spa_type_data data;
struct spa_type_media_type media_type;
struct spa_type_media_subtype media_subtype;
struct spa_type_format_audio format_audio;
struct spa_type_audio_format audio_format;
struct spa_type_event_node event_node;
struct spa_type_command_node command_node;
struct spa_type_param_buffers param_buffers;
struct spa_type_param_meta param_meta;
struct spa_type_param_io param_io;
};
static inline void init_type(struct type *type, struct spa_type_map *map)
{
type->node = spa_type_map_get_id(map, SPA_TYPE__Node);
type->format = spa_type_map_get_id(map, SPA_TYPE__Format);
type->props = spa_type_map_get_id(map, SPA_TYPE__Props);
type->prop_volume = spa_type_map_get_id(map, SPA_TYPE_PROPS__volume);
type->prop_mute = spa_type_map_get_id(map, SPA_TYPE_PROPS__mute);
spa_type_io_map(map, &type->io);
spa_type_param_map(map, &type->param);
spa_type_meta_map(map, &type->meta);
spa_type_data_map(map, &type->data);
spa_type_media_type_map(map, &type->media_type);
spa_type_media_subtype_map(map, &type->media_subtype);
spa_type_format_audio_map(map, &type->format_audio);
spa_type_audio_format_map(map, &type->audio_format);
spa_type_event_node_map(map, &type->event_node);
spa_type_command_node_map(map, &type->command_node);
spa_type_param_buffers_map(map, &type->param_buffers);
spa_type_param_meta_map(map, &type->param_meta);
spa_type_param_io_map(map, &type->param_io);
}
struct impl {
struct spa_handle handle;
struct spa_node node;
struct type type;
struct spa_type_map *map;
struct spa_log *log;
struct props props;
@ -151,7 +105,6 @@ static int impl_node_enum_params(struct spa_node *node,
struct spa_pod_builder *builder)
{
struct impl *this;
struct type *t;
struct spa_pod_builder b = { 0 };
uint8_t buffer[1024];
struct spa_pod *param;
@ -162,57 +115,60 @@ static int impl_node_enum_params(struct spa_node *node,
spa_return_val_if_fail(builder != NULL, -EINVAL);
this = SPA_CONTAINER_OF(node, struct impl, node);
t = &this->type;
p = &this->props;
next:
spa_pod_builder_init(&b, buffer, sizeof(buffer));
if (id == t->param.idList) {
uint32_t list[] = { t->param.idPropInfo,
t->param.idProps };
switch (id) {
case SPA_ID_PARAM_List:
{
uint32_t list[] = { SPA_ID_PARAM_PropInfo,
SPA_ID_PARAM_Props };
if (*index < SPA_N_ELEMENTS(list))
param = spa_pod_builder_object(&b, id, t->param.List,
":", t->param.listId, "I", list[*index]);
param = spa_pod_builder_object(&b, id, SPA_ID_OBJECT_ParamList,
":", SPA_PARAM_LIST_id, "I", list[*index]);
else
return 0;
break;
}
else if (id == t->param.idPropInfo) {
case SPA_ID_PARAM_PropInfo:
switch (*index) {
case 0:
param = spa_pod_builder_object(&b,
id, t->param.PropInfo,
":", t->param.propId, "I", t->prop_volume,
":", t->param.propName, "s", "The volume",
":", t->param.propType, "dr", p->volume,
id, SPA_ID_OBJECT_PropInfo,
":", SPA_PROP_INFO_id, "I", SPA_PROP_volume,
":", SPA_PROP_INFO_name, "s", "The volume",
":", SPA_PROP_INFO_type, "dr", p->volume,
SPA_POD_PROP_MIN_MAX(0.0, 10.0));
break;
case 1:
param = spa_pod_builder_object(&b,
id, t->param.PropInfo,
":", t->param.propId, "I", t->prop_mute,
":", t->param.propName, "s", "Mute",
":", t->param.propType, "b", p->mute);
id, SPA_ID_OBJECT_PropInfo,
":", SPA_PROP_INFO_id, "I", SPA_PROP_mute,
":", SPA_PROP_INFO_name, "s", "Mute",
":", SPA_PROP_INFO_type, "b", p->mute);
break;
default:
return 0;
}
}
else if (id == t->param.idProps) {
break;
case SPA_ID_PARAM_Props:
switch (*index) {
case 0:
param = spa_pod_builder_object(&b,
id, t->props,
":", t->prop_volume, "d", p->volume,
":", t->prop_mute, "b", p->mute);
id, SPA_ID_OBJECT_Props,
":", SPA_PROP_volume, "d", p->volume,
":", SPA_PROP_mute, "b", p->mute);
break;
default:
return 0;
}
}
else
break;
default:
return -ENOENT;
}
(*index)++;
@ -226,14 +182,14 @@ static int impl_node_set_param(struct spa_node *node, uint32_t id, uint32_t flag
const struct spa_pod *param)
{
struct impl *this;
struct type *t;
spa_return_val_if_fail(node != NULL, -EINVAL);
this = SPA_CONTAINER_OF(node, struct impl, node);
t = &this->type;
if (id == t->param.idProps) {
switch (id) {
case SPA_ID_PARAM_Props:
{
struct props *p = &this->props;
if (param == NULL) {
@ -241,11 +197,13 @@ static int impl_node_set_param(struct spa_node *node, uint32_t id, uint32_t flag
return 0;
}
spa_pod_object_parse(param,
":", t->prop_volume, "?d", &p->volume,
":", t->prop_mute, "?b", &p->mute, NULL);
":", SPA_PROP_volume, "?d", &p->volume,
":", SPA_PROP_mute, "?b", &p->mute, NULL);
break;
}
else
default:
return -ENOENT;
}
return 0;
}
@ -259,13 +217,16 @@ static int impl_node_send_command(struct spa_node *node, const struct spa_comman
this = SPA_CONTAINER_OF(node, struct impl, node);
if (SPA_COMMAND_TYPE(command) == this->type.command_node.Start) {
switch (SPA_COMMAND_TYPE(command)) {
case SPA_ID_COMMAND_NODE_Start:
this->started = true;
} else if (SPA_COMMAND_TYPE(command) == this->type.command_node.Pause) {
break;
case SPA_ID_COMMAND_NODE_Pause:
this->started = false;
} else
break;
default:
return -ENOTSUP;
}
return 0;
}
@ -365,21 +326,18 @@ static int port_enum_formats(struct spa_node *node,
struct spa_pod **param,
struct spa_pod_builder *builder)
{
struct impl *this = SPA_CONTAINER_OF(node, struct impl, node);
struct type *t = &this->type;
switch (*index) {
case 0:
*param = spa_pod_builder_object(builder,
t->param.idEnumFormat, t->format,
"I", t->media_type.audio,
"I", t->media_subtype.raw,
":", t->format_audio.format, "Ieu", t->audio_format.S16,
SPA_POD_PROP_ENUM(2, t->audio_format.S16,
t->audio_format.S32),
":", t->format_audio.rate, "iru", 44100,
SPA_ID_PARAM_EnumFormat, SPA_ID_OBJECT_Format,
"I", SPA_MEDIA_TYPE_audio,
"I", SPA_MEDIA_SUBTYPE_raw,
":", SPA_FORMAT_AUDIO_format, "Ieu", SPA_AUDIO_FORMAT_S16,
SPA_POD_PROP_ENUM(2, SPA_AUDIO_FORMAT_S16,
SPA_AUDIO_FORMAT_S32),
":", SPA_FORMAT_AUDIO_rate, "iru", 44100,
SPA_POD_PROP_MIN_MAX(1, INT32_MAX),
":", t->format_audio.channels,"iru", 2,
":", SPA_FORMAT_AUDIO_channels,"iru", 2,
SPA_POD_PROP_MIN_MAX(1, INT32_MAX));
break;
default:
@ -397,7 +355,6 @@ static int port_get_format(struct spa_node *node,
{
struct impl *this = SPA_CONTAINER_OF(node, struct impl, node);
struct port *port;
struct type *t = &this->type;
port = GET_PORT(this, direction, port_id);
@ -407,12 +364,12 @@ static int port_get_format(struct spa_node *node,
return 0;
*param = spa_pod_builder_object(builder,
t->param.idFormat, t->format,
"I", t->media_type.audio,
"I", t->media_subtype.raw,
":", t->format_audio.format, "I", this->current_format.info.raw.format,
":", t->format_audio.rate, "i", this->current_format.info.raw.rate,
":", t->format_audio.channels, "i", this->current_format.info.raw.channels);
SPA_ID_PARAM_Format, SPA_ID_OBJECT_Format,
"I", SPA_MEDIA_TYPE_audio,
"I", SPA_MEDIA_SUBTYPE_raw,
":", SPA_FORMAT_AUDIO_format, "I", this->current_format.info.raw.format,
":", SPA_FORMAT_AUDIO_rate, "i", this->current_format.info.raw.rate,
":", SPA_FORMAT_AUDIO_channels, "i", this->current_format.info.raw.channels);
return 1;
}
@ -426,7 +383,6 @@ impl_node_port_enum_params(struct spa_node *node,
struct spa_pod_builder *builder)
{
struct impl *this;
struct type *t;
struct port *port;
struct spa_pod_builder b = { 0 };
uint8_t buffer[1024];
@ -438,7 +394,6 @@ impl_node_port_enum_params(struct spa_node *node,
spa_return_val_if_fail(builder != NULL, -EINVAL);
this = SPA_CONTAINER_OF(node, struct impl, node);
t = &this->type;
spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), -EINVAL);
@ -447,81 +402,79 @@ impl_node_port_enum_params(struct spa_node *node,
next:
spa_pod_builder_init(&b, buffer, sizeof(buffer));
if (id == t->param.idList) {
uint32_t list[] = { t->param.idEnumFormat,
t->param.idFormat,
t->param.idBuffers,
t->param.idMeta,
t->param_io.idBuffers,
t->param_io.idControl };
switch (id) {
case SPA_ID_PARAM_List:
{
uint32_t list[] = { SPA_ID_PARAM_EnumFormat,
SPA_ID_PARAM_Format,
SPA_ID_PARAM_Buffers,
SPA_ID_PARAM_Meta,
SPA_ID_PARAM_IO };
if (*index < SPA_N_ELEMENTS(list))
param = spa_pod_builder_object(&b, id, t->param.List,
":", t->param.listId, "I", list[*index]);
param = spa_pod_builder_object(&b, id, SPA_ID_OBJECT_ParamList,
":", SPA_PARAM_LIST_id, "I", list[*index]);
else
return 0;
break;
}
else if (id == t->param.idEnumFormat) {
case SPA_ID_PARAM_EnumFormat:
if ((res = port_enum_formats(node, direction, port_id, index, filter, &param, &b)) <= 0)
return res;
}
else if (id == t->param.idFormat) {
break;
case SPA_ID_PARAM_Format:
if ((res = port_get_format(node, direction, port_id, index, filter, &param, &b)) <= 0)
return res;
}
else if (id == t->param.idBuffers) {
break;
case SPA_ID_PARAM_Buffers:
if (!port->have_format)
return -EIO;
if (*index > 0)
return 0;
param = spa_pod_builder_object(&b,
id, t->param_buffers.Buffers,
":", t->param_buffers.size, "iru", 1024 * this->bpf,
SPA_POD_PROP_MIN_MAX(16 * this->bpf, INT32_MAX / this->bpf),
":", t->param_buffers.stride, "i", 0,
":", t->param_buffers.buffers, "iru", 2,
id, SPA_ID_OBJECT_ParamBuffers,
":", SPA_PARAM_BUFFERS_buffers, "iru", 2,
SPA_POD_PROP_MIN_MAX(1, MAX_BUFFERS),
":", t->param_buffers.align, "i", 16);
}
else if (id == t->param.idMeta) {
":", SPA_PARAM_BUFFERS_blocks, "i", 1,
":", SPA_PARAM_BUFFERS_size, "iru", 1024 * this->bpf,
SPA_POD_PROP_MIN_MAX(16 * this->bpf, INT32_MAX / this->bpf),
":", SPA_PARAM_BUFFERS_stride, "i", 0,
":", SPA_PARAM_BUFFERS_align, "i", 16);
break;
case SPA_ID_PARAM_Meta:
switch (*index) {
case 0:
param = spa_pod_builder_object(&b,
id, t->param_meta.Meta,
":", t->param_meta.type, "I", t->meta.Header,
":", t->param_meta.size, "i", sizeof(struct spa_meta_header));
id, SPA_ID_OBJECT_ParamMeta,
":", SPA_PARAM_META_type, "I", SPA_META_Header,
":", SPA_PARAM_META_size, "i", sizeof(struct spa_meta_header));
break;
default:
return 0;
}
}
else if (id == t->param_io.idBuffers) {
break;
case SPA_ID_PARAM_IO:
switch (*index) {
case 0:
param = spa_pod_builder_object(&b,
id, t->param_io.Buffers,
":", t->param_io.id, "I", t->io.Buffers,
":", t->param_io.size, "i", sizeof(struct spa_io_buffers));
id, SPA_ID_OBJECT_ParamIO,
":", SPA_PARAM_IO_id, "I", SPA_ID_IO_Buffers,
":", SPA_PARAM_IO_size, "i", sizeof(struct spa_io_buffers));
break;
case 1:
param = spa_pod_builder_object(&b,
id, SPA_ID_OBJECT_ParamIO,
":", SPA_PARAM_IO_id, "I", SPA_ID_IO_ControlRange,
":", SPA_PARAM_IO_size, "i", sizeof(struct spa_io_control_range));
break;
default:
return 0;
}
}
else if (id == t->param_io.idControl) {
switch (*index) {
case 0:
param = spa_pod_builder_object(&b,
id, t->param_io.Control,
":", t->param_io.id, "I", t->io.ControlRange,
":", t->param_io.size, "i", sizeof(struct spa_io_control_range));
break;
default:
return 0;
}
}
else
break;
default:
return -ENOENT;
}
(*index)++;
@ -561,11 +514,11 @@ static int port_set_format(struct spa_node *node,
"I", &info.media_type,
"I", &info.media_subtype);
if (info.media_type != this->type.media_type.audio ||
info.media_subtype != this->type.media_subtype.raw)
if (info.media_type != SPA_MEDIA_TYPE_audio ||
info.media_subtype != SPA_MEDIA_SUBTYPE_raw)
return -EINVAL;
if (spa_format_audio_raw_parse(format, &info.info.raw, &this->type.format_audio) < 0)
if (spa_format_audio_raw_parse(format, &info.info.raw) < 0)
return -EINVAL;
this->bpf = 2 * info.info.raw.channels;
@ -582,17 +535,11 @@ impl_node_port_set_param(struct spa_node *node,
uint32_t id, uint32_t flags,
const struct spa_pod *param)
{
struct impl *this;
struct type *t;
spa_return_val_if_fail(node != NULL, -EINVAL);
this = SPA_CONTAINER_OF(node, struct impl, node);
t = &this->type;
spa_return_val_if_fail(CHECK_PORT(node, direction, port_id), -EINVAL);
spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), -EINVAL);
if (id == t->param.idFormat) {
if (id == SPA_ID_PARAM_Format) {
return port_set_format(node, direction, port_id, flags, param);
}
else
@ -609,7 +556,6 @@ impl_node_port_use_buffers(struct spa_node *node,
struct impl *this;
struct port *port;
uint32_t i;
struct type *t;
spa_return_val_if_fail(node != NULL, -EINVAL);
@ -622,8 +568,6 @@ impl_node_port_use_buffers(struct spa_node *node,
if (!port->have_format)
return -EIO;
t = &this->type;
clear_buffers(this, port);
for (i = 0; i < n_buffers; i++) {
@ -633,11 +577,11 @@ impl_node_port_use_buffers(struct spa_node *node,
b = &port->buffers[i];
b->outbuf = buffers[i];
b->outstanding = direction == SPA_DIRECTION_INPUT;
b->h = spa_buffer_find_meta_data(buffers[i], t->meta.Header, sizeof(*b->h));
b->h = spa_buffer_find_meta_data(buffers[i], SPA_META_Header, sizeof(*b->h));
if ((d[0].type == t->data.MemPtr ||
d[0].type == t->data.MemFd ||
d[0].type == t->data.DmaBuf) && d[0].data != NULL) {
if ((d[0].type == SPA_DATA_MemPtr ||
d[0].type == SPA_DATA_MemFd ||
d[0].type == SPA_DATA_DmaBuf) && d[0].data != NULL) {
b->ptr = d[0].data;
b->size = d[0].maxsize;
} else {
@ -674,20 +618,18 @@ impl_node_port_set_io(struct spa_node *node,
{
struct impl *this;
struct port *port;
struct type *t;
spa_return_val_if_fail(node != NULL, -EINVAL);
this = SPA_CONTAINER_OF(node, struct impl, node);
t = &this->type;
spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), -EINVAL);
port = GET_PORT(this, direction, port_id);
if (id == t->io.Buffers)
if (id == SPA_ID_IO_Buffers)
port->io = data;
else if (id == t->io.ControlRange)
else if (id == SPA_ID_IO_ControlRange)
port->range = data;
else
return -ENOENT;
@ -888,7 +830,7 @@ static int impl_get_interface(struct spa_handle *handle, uint32_t interface_id,
this = (struct impl *) handle;
if (interface_id == this->type.node)
if (interface_id == SPA_ID_INTERFACE_Node)
*interface = &this->node;
else
return -ENOENT;
@ -927,16 +869,9 @@ impl_init(const struct spa_handle_factory *factory,
this = (struct impl *) handle;
for (i = 0; i < n_support; i++) {
if (strcmp(support[i].type, SPA_TYPE__TypeMap) == 0)
this->map = support[i].data;
else if (strcmp(support[i].type, SPA_TYPE__Log) == 0)
if (support[i].type == SPA_ID_INTERFACE_Log)
this->log = support[i].data;
}
if (this->map == NULL) {
spa_log_error(this->log, "a type-map is needed");
return -EINVAL;
}
init_type(&this->type, this->map);
this->node = impl_node;
reset_props(&this->props);
@ -953,7 +888,7 @@ impl_init(const struct spa_handle_factory *factory,
}
static const struct spa_interface_info impl_interfaces[] = {
{SPA_TYPE__Node,},
{SPA_ID_INTERFACE_Node,},
};
static int