interface: add an interface struct

The interface struct has the type,version and methods of the
interface.
Make spa interfaces extend from spa_interface and make a
separate structure for the methods.
Pass a generic void* as the first argument of methods, like
we don in PipeWire.
Bundle the methods + implementation in a versioned inteface
and use that to invoke methods. This way we can do version
checks on the methods.
Make resource and proxy interfaces that we can can call. We
can then make the core interfaces independent on proxy/resource and
hide them in the lower layers.
Add add_listener method to methods of core interfaces, just
like SPA.
This commit is contained in:
Wim Taymans 2019-05-20 16:11:23 +02:00
parent eb6481efb3
commit ff946e3d4b
85 changed files with 3051 additions and 3000 deletions

View file

@ -115,19 +115,18 @@ static int emit_info(struct impl *this, bool full)
return 0;
}
static int impl_add_listener(struct spa_device *device,
static int impl_add_listener(void *object,
struct spa_hook *listener,
const struct spa_device_events *events,
void *data)
{
struct impl *this;
struct impl *this = object;
struct spa_hook_list save;
int res = 0;
spa_return_val_if_fail(device != NULL, -EINVAL);
spa_return_val_if_fail(this != NULL, -EINVAL);
spa_return_val_if_fail(events != NULL, -EINVAL);
this = SPA_CONTAINER_OF(device, struct impl, device);
spa_hook_list_isolate(&this->hooks, &save, listener, events, data);
if (events->info || events->object_info)
@ -138,25 +137,25 @@ static int impl_add_listener(struct spa_device *device,
return res;
}
static int impl_enum_params(struct spa_device *device, int seq,
static int impl_enum_params(void *object, int seq,
uint32_t id, uint32_t start, uint32_t num,
const struct spa_pod *filter)
{
return -ENOTSUP;
}
static int impl_set_param(struct spa_device *device,
static int impl_set_param(void *object,
uint32_t id, uint32_t flags,
const struct spa_pod *param)
{
return -ENOTSUP;
}
static const struct spa_device impl_device = {
SPA_VERSION_DEVICE,
impl_add_listener,
impl_enum_params,
impl_set_param,
static const struct spa_device_methods impl_device = {
SPA_VERSION_DEVICE_METHODS,
.add_listener = impl_add_listener,
.enum_params = impl_enum_params,
.set_param = impl_set_param,
};
static int impl_get_interface(struct spa_handle *handle, uint32_t type, void **interface)
@ -218,7 +217,10 @@ impl_init(const struct spa_handle_factory *factory,
spa_hook_list_init(&this->hooks);
this->device = impl_device;
this->device.iface = SPA_INTERFACE_INIT(
SPA_TYPE_INTERFACE_Device,
SPA_VERSION_DEVICE,
&impl_device, this);
this->dev.log = this->log;
this->dev.fd = -1;

View file

@ -225,6 +225,7 @@ static int start_monitor(struct impl *this)
this->source.fd = udev_monitor_get_fd(this->umonitor);;
this->source.mask = SPA_IO_IN | SPA_IO_ERR;
spa_log_debug(this->log, "monitor %p", this->umonitor);
spa_loop_add_source(this->main_loop, &this->source);
return 0;
@ -273,16 +274,14 @@ static int enum_devices(struct impl *this)
static int
impl_monitor_set_callbacks(struct spa_monitor *monitor,
impl_monitor_set_callbacks(void *object,
const struct spa_monitor_callbacks *callbacks,
void *data)
{
int res;
struct impl *this;
struct impl *this = object;
spa_return_val_if_fail(monitor != NULL, -EINVAL);
this = SPA_CONTAINER_OF(monitor, struct impl, monitor);
spa_return_val_if_fail(this != NULL, -EINVAL);
this->callbacks = SPA_CALLBACKS_INIT(callbacks, data);
@ -302,9 +301,9 @@ impl_monitor_set_callbacks(struct spa_monitor *monitor,
return 0;
}
static const struct spa_monitor impl_monitor = {
SPA_VERSION_MONITOR,
impl_monitor_set_callbacks,
static const struct spa_monitor_methods impl_monitor = {
SPA_VERSION_MONITOR_METHODS,
.set_callbacks = impl_monitor_set_callbacks,
};
static int impl_get_interface(struct spa_handle *handle, uint32_t type, void **interface)
@ -327,7 +326,7 @@ static int impl_get_interface(struct spa_handle *handle, uint32_t type, void **i
static int impl_clear(struct spa_handle *handle)
{
struct impl *this = (struct impl *) handle;
impl_monitor_set_callbacks(&this->monitor, NULL, NULL);
impl_monitor_set_callbacks(this, NULL, NULL);
return 0;
}
@ -367,7 +366,10 @@ impl_init(const struct spa_handle_factory *factory,
return -EINVAL;
}
this->monitor = impl_monitor;
this->monitor.iface = SPA_INTERFACE_INIT(
SPA_TYPE_INTERFACE_Monitor,
SPA_VERSION_MONITOR,
&impl_monitor, this);
return 0;
}
@ -394,7 +396,7 @@ impl_enum_interface_info(const struct spa_handle_factory *factory,
}
const struct spa_handle_factory spa_v4l2_monitor_factory = {
SPA_VERSION_MONITOR,
SPA_VERSION_HANDLE_FACTORY,
NAME,
NULL,
impl_get_size,

View file

@ -149,22 +149,20 @@ struct impl {
#include "v4l2-utils.c"
static int impl_node_enum_params(struct spa_node *node, int seq,
static int impl_node_enum_params(void *object, int seq,
uint32_t id, uint32_t start, uint32_t num,
const struct spa_pod *filter)
{
struct impl *this;
struct impl *this = object;
struct spa_pod *param;
struct spa_pod_builder b = { 0 };
uint8_t buffer[1024];
struct spa_result_node_params result;
uint32_t count = 0;
spa_return_val_if_fail(node != NULL, -EINVAL);
spa_return_val_if_fail(this != NULL, -EINVAL);
spa_return_val_if_fail(num != 0, -EINVAL);
this = SPA_CONTAINER_OF(node, struct impl, node);
result.id = id;
result.next = start;
next:
@ -236,15 +234,13 @@ static int impl_node_enum_params(struct spa_node *node, int seq,
return 0;
}
static int impl_node_set_param(struct spa_node *node,
static int impl_node_set_param(void *object,
uint32_t id, uint32_t flags,
const struct spa_pod *param)
{
struct impl *this;
struct impl *this = object;
spa_return_val_if_fail(node != NULL, -EINVAL);
this = SPA_CONTAINER_OF(node, struct impl, node);
spa_return_val_if_fail(this != NULL, -EINVAL);
switch (id) {
case SPA_PARAM_Props:
@ -266,13 +262,11 @@ static int impl_node_set_param(struct spa_node *node,
return 0;
}
static int impl_node_set_io(struct spa_node *node, uint32_t id, void *data, size_t size)
static int impl_node_set_io(void *object, uint32_t id, void *data, size_t size)
{
struct impl *this;
struct impl *this = object;
spa_return_val_if_fail(node != NULL, -EINVAL);
this = SPA_CONTAINER_OF(node, struct impl, node);
spa_return_val_if_fail(this != NULL, -EINVAL);
switch (id) {
case SPA_IO_Clock:
@ -287,16 +281,14 @@ static int impl_node_set_io(struct spa_node *node, uint32_t id, void *data, size
return 0;
}
static int impl_node_send_command(struct spa_node *node, const struct spa_command *command)
static int impl_node_send_command(void *object, const struct spa_command *command)
{
struct impl *this;
struct impl *this = object;
int res;
spa_return_val_if_fail(node != NULL, -EINVAL);
spa_return_val_if_fail(this != NULL, -EINVAL);
spa_return_val_if_fail(command != NULL, -EINVAL);
this = SPA_CONTAINER_OF(node, struct impl, node);
switch (SPA_NODE_COMMAND_ID(command)) {
case SPA_NODE_COMMAND_Start:
{
@ -352,17 +344,16 @@ static void emit_port_info(struct impl *this, struct port *port, bool full)
}
static int
impl_node_add_listener(struct spa_node *node,
impl_node_add_listener(void *object,
struct spa_hook *listener,
const struct spa_node_events *events,
void *data)
{
struct impl *this;
struct impl *this = object;
struct spa_hook_list save;
spa_return_val_if_fail(node != NULL, -EINVAL);
spa_return_val_if_fail(this != NULL, -EINVAL);
this = SPA_CONTAINER_OF(node, struct impl, node);
spa_hook_list_isolate(&this->hooks, &save, listener, events, data);
emit_node_info(this, true);
@ -373,43 +364,41 @@ impl_node_add_listener(struct spa_node *node,
return 0;
}
static int impl_node_set_callbacks(struct spa_node *node,
static int impl_node_set_callbacks(void *object,
const struct spa_node_callbacks *callbacks,
void *data)
{
struct impl *this;
struct impl *this = object;
spa_return_val_if_fail(node != NULL, -EINVAL);
this = SPA_CONTAINER_OF(node, struct impl, node);
spa_return_val_if_fail(this != NULL, -EINVAL);
this->callbacks = SPA_CALLBACKS_INIT(callbacks, data);
return 0;
}
static int impl_node_add_port(struct spa_node *node,
static int impl_node_add_port(void *object,
enum spa_direction direction,
uint32_t port_id, const struct spa_dict *props)
{
return -ENOTSUP;
}
static int impl_node_remove_port(struct spa_node *node,
static int impl_node_remove_port(void *object,
enum spa_direction direction,
uint32_t port_id)
{
return -ENOTSUP;
}
static int port_get_format(struct spa_node *node,
static int port_get_format(void *object,
enum spa_direction direction, uint32_t port_id,
uint32_t index,
const struct spa_pod *filter,
struct spa_pod **param,
struct spa_pod_builder *builder)
{
struct impl *this = SPA_CONTAINER_OF(node, struct impl, node);
struct impl *this = object;
struct port *port = GET_PORT(this, direction, port_id);
struct spa_pod_frame f;
@ -454,14 +443,14 @@ static int port_get_format(struct spa_node *node,
return 1;
}
static int impl_node_port_enum_params(struct spa_node *node, int seq,
static int impl_node_port_enum_params(void *object, int seq,
enum spa_direction direction,
uint32_t port_id,
uint32_t id, uint32_t start, uint32_t num,
const struct spa_pod *filter)
{
struct impl *this;
struct impl *this = object;
struct port *port;
struct spa_pod *param;
struct spa_pod_builder b = { 0 };
@ -470,11 +459,8 @@ static int impl_node_port_enum_params(struct spa_node *node, int seq,
uint32_t count = 0;
int res;
spa_return_val_if_fail(node != NULL, -EINVAL);
spa_return_val_if_fail(this != NULL, -EINVAL);
spa_return_val_if_fail(num != 0, -EINVAL);
this = SPA_CONTAINER_OF(node, struct impl, node);
spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), -EINVAL);
port = GET_PORT(this, direction, port_id);
@ -494,7 +480,7 @@ static int impl_node_port_enum_params(struct spa_node *node, int seq,
return spa_v4l2_enum_format(this, seq, start, num, filter);
case SPA_PARAM_Format:
if((res = port_get_format(node, direction, port_id,
if((res = port_get_format(this, direction, port_id,
result.index, filter, &param, &b)) <= 0)
return res;
break;
@ -564,12 +550,12 @@ static int impl_node_port_enum_params(struct spa_node *node, int seq,
return 0;
}
static int port_set_format(struct spa_node *node,
static int port_set_format(void *object,
enum spa_direction direction, uint32_t port_id,
uint32_t flags,
const struct spa_pod *format)
{
struct impl *this = SPA_CONTAINER_OF(node, struct impl, node);
struct impl *this = object;
struct spa_video_info info;
struct port *port = GET_PORT(this, direction, port_id);
int res;
@ -658,36 +644,33 @@ static int port_set_format(struct spa_node *node,
return 0;
}
static int impl_node_port_set_param(struct spa_node *node,
static int impl_node_port_set_param(void *object,
enum spa_direction direction, uint32_t port_id,
uint32_t id, uint32_t flags,
const struct spa_pod *param)
{
spa_return_val_if_fail(node != NULL, -EINVAL);
spa_return_val_if_fail(object != NULL, -EINVAL);
spa_return_val_if_fail(CHECK_PORT(node, direction, port_id), -EINVAL);
spa_return_val_if_fail(CHECK_PORT(object, direction, port_id), -EINVAL);
if (id == SPA_PARAM_Format) {
return port_set_format(node, direction, port_id, flags, param);
return port_set_format(object, direction, port_id, flags, param);
}
else
return -ENOENT;
}
static int impl_node_port_use_buffers(struct spa_node *node,
static int impl_node_port_use_buffers(void *object,
enum spa_direction direction,
uint32_t port_id,
struct spa_buffer **buffers,
uint32_t n_buffers)
{
struct impl *this;
struct impl *this = object;
struct port *port;
int res;
spa_return_val_if_fail(node != NULL, -EINVAL);
this = SPA_CONTAINER_OF(node, struct impl, node);
spa_return_val_if_fail(this != NULL, -EINVAL);
spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), -EINVAL);
port = GET_PORT(this, direction, port_id);
@ -708,7 +691,7 @@ static int impl_node_port_use_buffers(struct spa_node *node,
}
static int
impl_node_port_alloc_buffers(struct spa_node *node,
impl_node_port_alloc_buffers(void *object,
enum spa_direction direction,
uint32_t port_id,
struct spa_pod **params,
@ -716,15 +699,12 @@ impl_node_port_alloc_buffers(struct spa_node *node,
struct spa_buffer **buffers,
uint32_t *n_buffers)
{
struct impl *this;
struct impl *this = object;
struct port *port;
int res;
spa_return_val_if_fail(node != NULL, -EINVAL);
spa_return_val_if_fail(this != NULL, -EINVAL);
spa_return_val_if_fail(buffers != NULL, -EINVAL);
this = SPA_CONTAINER_OF(node, struct impl, node);
spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), -EINVAL);
port = GET_PORT(this, direction, port_id);
@ -737,19 +717,16 @@ impl_node_port_alloc_buffers(struct spa_node *node,
return res;
}
static int impl_node_port_set_io(struct spa_node *node,
static int impl_node_port_set_io(void *object,
enum spa_direction direction,
uint32_t port_id,
uint32_t id,
void *data, size_t size)
{
struct impl *this;
struct impl *this = object;
struct port *port;
spa_return_val_if_fail(node != NULL, -EINVAL);
this = SPA_CONTAINER_OF(node, struct impl, node);
spa_return_val_if_fail(this != NULL, -EINVAL);
spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), -EINVAL);
port = GET_PORT(this, direction, port_id);
@ -767,18 +744,17 @@ static int impl_node_port_set_io(struct spa_node *node,
return 0;
}
static int impl_node_port_reuse_buffer(struct spa_node *node,
static int impl_node_port_reuse_buffer(void *object,
uint32_t port_id,
uint32_t buffer_id)
{
struct impl *this;
struct impl *this = object;
struct port *port;
int res;
spa_return_val_if_fail(node != NULL, -EINVAL);
spa_return_val_if_fail(this != NULL, -EINVAL);
spa_return_val_if_fail(port_id == 0, -EINVAL);
this = SPA_CONTAINER_OF(node, struct impl, node);
port = GET_OUT_PORT(this, port_id);
spa_return_val_if_fail(buffer_id < port->n_buffers, -EINVAL);
@ -854,17 +830,15 @@ static int process_control(struct impl *this, struct spa_pod_sequence *control)
return 0;
}
static int impl_node_process(struct spa_node *node)
static int impl_node_process(void *object)
{
struct impl *this;
struct impl *this = object;
int res;
struct spa_io_buffers *io;
struct port *port;
struct buffer *b;
spa_return_val_if_fail(node != NULL, -EINVAL);
this = SPA_CONTAINER_OF(node, struct impl, node);
spa_return_val_if_fail(this != NULL, -EINVAL);
port = GET_OUT_PORT(this, 0);
io = port->io;
@ -873,7 +847,7 @@ static int impl_node_process(struct spa_node *node)
if (port->control)
process_control(this, &port->control->sequence);
spa_log_trace(this->log, NAME " %p; status %d", node, io->status);
spa_log_trace(this->log, NAME " %p; status %d", this, io->status);
if (io->status == SPA_STATUS_HAVE_BUFFER)
return SPA_STATUS_HAVE_BUFFER;
@ -891,7 +865,7 @@ static int impl_node_process(struct spa_node *node)
b = spa_list_first(&port->queue, struct buffer, link);
spa_list_remove(&b->link);
spa_log_trace(this->log, NAME " %p: dequeue buffer %d", node, b->id);
spa_log_trace(this->log, NAME " %p: dequeue buffer %d", this, b->id);
io->buffer_id = b->id;
io->status = SPA_STATUS_HAVE_BUFFER;
@ -899,8 +873,8 @@ static int impl_node_process(struct spa_node *node)
return SPA_STATUS_HAVE_BUFFER;
}
static const struct spa_node impl_node = {
SPA_VERSION_NODE,
static const struct spa_node_methods impl_node = {
SPA_VERSION_NODE_METHODS,
.add_listener = impl_node_add_listener,
.set_callbacks = impl_node_set_callbacks,
.enum_params = impl_node_enum_params,
@ -985,7 +959,10 @@ impl_init(const struct spa_handle_factory *factory,
return -EINVAL;
}
this->node = impl_node;
this->node.iface = SPA_INTERFACE_INIT(
SPA_TYPE_INTERFACE_Node,
SPA_VERSION_NODE,
&impl_node, this);
spa_hook_list_init(&this->hooks);
this->info_all = SPA_NODE_CHANGE_MASK_FLAGS |