More work on implementing remote protocol

Rework things so that we negotiate buffer pools beforehand and only pass
buffer ids around We can then remove the refcount of buffers, events and
commands.
More work on buffer reuse
Use the node state changes to trigger the next step in the configuration
sequence.
Move most of the client-node to a plugin
Do buffer allocation in the port link.
This commit is contained in:
Wim Taymans 2016-08-02 16:34:44 +02:00
parent 05829f33e6
commit 3ace7e9648
36 changed files with 1780 additions and 1450 deletions

View file

@ -25,6 +25,8 @@
#include <sys/mman.h>
#include <unistd.h>
#include <fcntl.h>
#include <dlfcn.h>
#include <poll.h>
#include <gio/gunixfdlist.h>
@ -42,24 +44,17 @@
#include "spa/include/spa/control.h"
#define MAX_BUFFER_SIZE 1024
#define MAX_FDS 16
struct _PinosClientNodePrivate
{
int fd;
GSource *socket_source;
GSocket *sockets[2];
SpaControl recv_control;
SpaPollFd fds[16];
unsigned int n_fds;
SpaPollItem poll;
guint8 recv_data[MAX_BUFFER_SIZE];
int recv_fds[MAX_FDS];
guint8 send_data[MAX_BUFFER_SIZE];
int send_fds[MAX_FDS];
GHashTable *mem_ids;
gboolean running;
pthread_t thread;
};
#define PINOS_CLIENT_NODE_GET_PRIVATE(obj) \
@ -110,193 +105,6 @@ pinos_client_node_set_property (GObject *_object,
}
}
static gboolean
parse_control (PinosClientNode *cnode,
SpaControl *ctrl)
{
PinosNode *node = PINOS_NODE (cnode);
PinosClientNodePrivate *priv = cnode->priv;
SpaControlIter it;
spa_control_iter_init (&it, ctrl);
while (spa_control_iter_next (&it) == SPA_RESULT_OK) {
SpaControlCmd cmd = spa_control_iter_get_cmd (&it);
switch (cmd) {
case SPA_CONTROL_CMD_ADD_PORT:
case SPA_CONTROL_CMD_REMOVE_PORT:
case SPA_CONTROL_CMD_SET_FORMAT:
case SPA_CONTROL_CMD_SET_PROPERTY:
case SPA_CONTROL_CMD_END_CONFIGURE:
case SPA_CONTROL_CMD_PAUSE:
case SPA_CONTROL_CMD_START:
case SPA_CONTROL_CMD_STOP:
g_warning ("client-node %p: got unexpected control %d", node, cmd);
break;
case SPA_CONTROL_CMD_NODE_UPDATE:
case SPA_CONTROL_CMD_PORT_UPDATE:
case SPA_CONTROL_CMD_PORT_REMOVED:
g_warning ("client-node %p: command not implemented %d", node, cmd);
break;
case SPA_CONTROL_CMD_START_CONFIGURE:
{
SpaControlBuilder builder;
SpaControl control;
guint8 buffer[1024];
/* set port format */
/* send end-configure */
spa_control_builder_init_into (&builder, buffer, 1024, NULL, 0);
spa_control_builder_add_cmd (&builder, SPA_CONTROL_CMD_END_CONFIGURE, NULL);
spa_control_builder_end (&builder, &control);
if (spa_control_write (&control, priv->fd) < 0)
g_warning ("client-node %p: error writing control", node);
break;
}
case SPA_CONTROL_CMD_PORT_STATUS_CHANGE:
{
g_warning ("client-node %p: command not implemented %d", node, cmd);
break;
}
case SPA_CONTROL_CMD_START_ALLOC:
{
SpaControlBuilder builder;
SpaControl control;
guint8 buffer[1024];
GList *ports, *walk;
/* FIXME read port memory requirements */
/* FIXME add_mem */
/* send start */
spa_control_builder_init_into (&builder, buffer, 1024, NULL, 0);
spa_control_builder_add_cmd (&builder, SPA_CONTROL_CMD_START, NULL);
spa_control_builder_end (&builder, &control);
if (spa_control_write (&control, priv->fd) < 0)
g_warning ("client-node %p: error writing control", node);
ports = pinos_node_get_ports (node);
for (walk = ports; walk; walk = g_list_next (walk)) {
PinosPort *port = walk->data;
pinos_port_activate (port);
}
break;
}
case SPA_CONTROL_CMD_NEED_INPUT:
{
break;
}
case SPA_CONTROL_CMD_HAVE_OUTPUT:
{
break;
}
case SPA_CONTROL_CMD_ADD_MEM:
break;
case SPA_CONTROL_CMD_REMOVE_MEM:
break;
case SPA_CONTROL_CMD_ADD_BUFFER:
break;
case SPA_CONTROL_CMD_REMOVE_BUFFER:
break;
case SPA_CONTROL_CMD_PROCESS_BUFFER:
{
break;
}
case SPA_CONTROL_CMD_REUSE_BUFFER:
{
break;
}
default:
g_warning ("client-node %p: command unhandled %d", node, cmd);
break;
}
}
spa_control_iter_end (&it);
return TRUE;
}
static gboolean
on_socket_condition (GSocket *socket,
GIOCondition condition,
gpointer user_data)
{
PinosClientNode *node = user_data;
PinosClientNodePrivate *priv = node->priv;
switch (condition) {
case G_IO_IN:
{
SpaControl *control = &priv->recv_control;
if (spa_control_read (control,
priv->fd,
priv->recv_data,
MAX_BUFFER_SIZE,
priv->recv_fds,
MAX_FDS) < 0) {
g_warning ("client-node %p: failed to read buffer", node);
return TRUE;
}
parse_control (node, control);
#if 0
if (!pinos_port_receive_buffer (priv->port, buffer, &error)) {
g_warning ("client-node %p: port %p failed to receive buffer: %s", node, priv->port, error->message);
g_clear_error (&error);
}
#endif
spa_control_clear (control);
break;
}
case G_IO_OUT:
g_warning ("can do IO OUT\n");
break;
default:
break;
}
return TRUE;
}
static void
handle_socket (PinosClientNode *node, GSocket *socket)
{
PinosClientNodePrivate *priv = node->priv;
GMainContext *context = g_main_context_get_thread_default();
g_debug ("client-node %p: handle socket in context %p", node, context);
priv->fd = g_socket_get_fd (socket);
priv->socket_source = g_socket_create_source (socket, G_IO_IN, NULL);
g_source_set_callback (priv->socket_source, (GSourceFunc) on_socket_condition, node, NULL);
g_source_attach (priv->socket_source, context);
}
static void
unhandle_socket (PinosClientNode *node)
{
PinosClientNodePrivate *priv = node->priv;
g_debug ("client-node %p: unhandle socket", node);
if (priv->socket_source) {
g_source_destroy (priv->socket_source);
g_clear_pointer (&priv->socket_source, g_source_unref);
priv->fd = -1;
}
}
/**
* pinos_client_node_get_socket_pair:
* @node: a #PinosClientNode
@ -308,15 +116,19 @@ unhandle_socket (PinosClientNode *node)
* Returns: a #GSocket that can be used to send/receive buffers to node.
*/
GSocket *
pinos_client_node_get_socket_pair (PinosClientNode *node,
GError **error)
pinos_client_node_get_socket_pair (PinosClientNode *this,
GError **error)
{
PinosNode *node;
PinosClientNodePrivate *priv;
g_return_val_if_fail (PINOS_IS_CLIENT_NODE (node), FALSE);
priv = node->priv;
g_return_val_if_fail (PINOS_IS_CLIENT_NODE (this), FALSE);
node = PINOS_NODE (this);
priv = this->priv;
if (priv->sockets[1] == NULL) {
SpaProps *props;
SpaPropValue value;
int fd[2];
if (socketpair (AF_UNIX, SOCK_STREAM, 0, fd) != 0)
@ -330,7 +142,14 @@ pinos_client_node_get_socket_pair (PinosClientNode *node,
if (priv->sockets[1] == NULL)
goto create_failed;
handle_socket (node, priv->sockets[0]);
priv->fd = g_socket_get_fd (priv->sockets[0]);
spa_node_get_props (node->node, &props);
value.type = SPA_PROP_TYPE_INT;
value.value = &priv->fd;
value.size = sizeof (int);
spa_props_set_prop (props, spa_props_index_for_name (props, "socket"), &value);
spa_node_set_props (node->node, props);
}
return g_object_ref (priv->sockets[1]);
@ -351,156 +170,169 @@ create_failed:
}
}
static void
on_format_change (GObject *obj,
GParamSpec *pspec,
gpointer user_data)
{
PinosClientNode *node = user_data;
PinosClientNodePrivate *priv = node->priv;
GBytes *format;
SpaControl control;
SpaControlBuilder builder;
SpaControlCmdSetFormat sf;
guint8 buf[1024];
g_object_get (obj, "format", &format, NULL);
if (format == NULL)
return ;
g_debug ("port %p: format change %s", obj, (gchar*)g_bytes_get_data (format, NULL));
spa_control_builder_init_into (&builder, buf, 1024, NULL, 0);
sf.port = 0;
sf.format = NULL;
sf.str = g_bytes_get_data (format, NULL);
spa_control_builder_add_cmd (&builder, SPA_CONTROL_CMD_SET_FORMAT, &sf);
spa_control_builder_end (&builder, &control);
if (spa_control_write (&control, priv->fd))
g_warning ("client-node %p: error writing control", node);
}
static int
tmpfile_create (void *data, gsize size)
{
char filename[] = "/dev/shm/tmpfilepay.XXXXXX";
int fd;
fd = mkostemp (filename, O_CLOEXEC);
if (fd == -1) {
g_debug ("Failed to create temporary file: %s", strerror (errno));
return -1;
}
unlink (filename);
if (write (fd, data, size) != (gssize) size)
g_debug ("Failed to write data: %s", strerror (errno));
return fd;
}
typedef struct {
SpaBuffer buffer;
SpaData datas[16];
int idx[16];
SpaBuffer *orig;
} MyBuffer;
static gboolean
on_received_buffer (PinosPort *port, SpaBuffer *buffer, GError **error, gpointer user_data)
on_received_buffer (PinosPort *port, uint32_t buffer_id, GError **error, gpointer user_data)
{
PinosClientNode *node = user_data;
PinosClientNodePrivate *priv = node->priv;
SpaControl control;
SpaControlBuilder builder;
guint8 buf[1024];
int fds[16];
SpaControlCmdAddBuffer ab;
SpaControlCmdProcessBuffer pb;
SpaControlCmdRemoveBuffer rb;
bool tmpfile = false;
PinosNode *node = user_data;
PinosClientNode *this = PINOS_CLIENT_NODE (node);
PinosClientNodePrivate *priv = this->priv;
SpaResult res;
SpaInputInfo info[1];
if (pinos_port_get_direction (port) == PINOS_DIRECTION_OUTPUT) {
/* FIXME, does not happen */
spa_control_builder_init_into (&builder, buf, 1024, NULL, 0);
spa_control_builder_add_cmd (&builder, SPA_CONTROL_CMD_HAVE_OUTPUT, NULL);
spa_control_builder_end (&builder, &control);
info[0].port_id = port->id;
info[0].buffer_id = buffer_id;
info[0].flags = SPA_INPUT_FLAG_NONE;
info[0].offset = 0;
info[0].size = -1;
if (spa_control_write (&control, priv->fd)) {
g_warning ("client-node %p: error writing control", node);
return FALSE;
}
} else {
unsigned int i;
MyBuffer b;
spa_control_builder_init_into (&builder, buf, 1024, fds, 16);
b.buffer.refcount = 1;
b.buffer.notify = NULL;
b.buffer.id = buffer->id;
b.buffer.size = buffer->size;
b.buffer.n_metas = buffer->n_metas;
b.buffer.metas = buffer->metas;
b.buffer.n_datas = buffer->n_datas;
b.buffer.datas = b.datas;
for (i = 0; i < buffer->n_datas; i++) {
SpaData *d = &buffer->datas[i];
int fd;
SpaControlCmdAddMem am;
if (d->type == SPA_DATA_TYPE_FD) {
fd = *((int *)d->ptr);
} else {
fd = tmpfile_create (d->ptr, d->size + d->offset);
tmpfile = true;
}
am.port = 0;
am.id = i;
am.type = 0;
am.fd_index = spa_control_builder_add_fd (&builder, fd, tmpfile ? true : false);
am.offset = 0;
am.size = d->offset + d->size;
spa_control_builder_add_cmd (&builder, SPA_CONTROL_CMD_ADD_MEM, &am);
b.idx[i] = i;
b.datas[i].type = SPA_DATA_TYPE_MEMID;
b.datas[i].ptr_type = NULL;
b.datas[i].ptr = &b.idx[i];
b.datas[i].offset = d->offset;
b.datas[i].size = d->size;
b.datas[i].stride = d->stride;
}
ab.port = 0;
ab.buffer = &b.buffer;
spa_control_builder_add_cmd (&builder, SPA_CONTROL_CMD_ADD_BUFFER, &ab);
pb.port = 0;
pb.id = b.buffer.id;
spa_control_builder_add_cmd (&builder, SPA_CONTROL_CMD_PROCESS_BUFFER, &pb);
rb.port = 0;
rb.id = b.buffer.id;
spa_control_builder_add_cmd (&builder, SPA_CONTROL_CMD_REMOVE_BUFFER, &rb);
for (i = 0; i < buffer->n_datas; i++) {
SpaControlCmdRemoveMem rm;
rm.port = 0;
rm.id = i;
spa_control_builder_add_cmd (&builder, SPA_CONTROL_CMD_REMOVE_MEM, &rm);
}
spa_control_builder_end (&builder, &control);
if (spa_control_write (&control, priv->fd))
g_warning ("client-node %p: error writing control", node);
spa_control_clear (&control);
}
if ((res = spa_node_port_push_input (node->node, 1, info)) < 0)
g_warning ("client-node %p: error pushing buffer: %d, %d", node, res, info[0].status);
return TRUE;
}
static gboolean
on_received_event (PinosPort *port, SpaEvent *event, GError **error, gpointer user_data)
{
PinosNode *node = user_data;
PinosClientNode *this = PINOS_CLIENT_NODE (node);
PinosClientNodePrivate *priv = this->priv;
SpaResult res;
if ((res = spa_node_port_push_event (node->node, port->id, event)) < 0)
g_warning ("client-node %p: error pushing event: %d", node, res);
return TRUE;
}
static void *
loop (void *user_data)
{
PinosClientNode *this = user_data;
PinosClientNodePrivate *priv = this->priv;
int r;
g_debug ("client-node %p: enter thread", this);
while (priv->running) {
SpaPollNotifyData ndata;
r = poll ((struct pollfd *) priv->fds, priv->n_fds, -1);
if (r < 0) {
if (errno == EINTR)
continue;
break;
}
if (r == 0) {
g_debug ("client-node %p: select timeout", this);
break;
}
if (priv->poll.after_cb) {
ndata.fds = priv->poll.fds;
ndata.n_fds = priv->poll.n_fds;
ndata.user_data = priv->poll.user_data;
priv->poll.after_cb (&ndata);
}
}
g_debug ("client-node %p: leave thread", this);
return NULL;
}
static void
start_thread (PinosClientNode *this)
{
PinosClientNodePrivate *priv = this->priv;
int err;
if (!priv->running) {
priv->running = true;
if ((err = pthread_create (&priv->thread, NULL, loop, this)) != 0) {
g_debug ("client-node %p: can't create thread", strerror (err));
priv->running = false;
}
}
}
static void
stop_thread (PinosClientNode *this)
{
PinosClientNodePrivate *priv = this->priv;
if (priv->running) {
priv->running = false;
pthread_join (priv->thread, NULL);
}
}
static void
on_node_event (SpaNode *node, SpaEvent *event, void *user_data)
{
PinosClientNode *this = user_data;
PinosClientNodePrivate *priv = this->priv;
switch (event->type) {
case SPA_EVENT_TYPE_STATE_CHANGE:
{
SpaEventStateChange *sc = event->data;
switch (sc->state) {
case SPA_NODE_STATE_CONFIGURE:
{
GList *ports, *walk;
ports = pinos_node_get_ports (PINOS_NODE (this));
for (walk = ports; walk; walk = g_list_next (walk)) {
PinosPort *port = walk->data;
pinos_port_activate (port);
}
}
default:
break;
}
break;
}
case SPA_EVENT_TYPE_ADD_POLL:
{
SpaPollItem *poll = event->data;
priv->poll = *poll;
priv->fds[0] = poll->fds[0];
priv->n_fds = 1;
priv->poll.fds = priv->fds;
start_thread (this);
break;
}
case SPA_EVENT_TYPE_REMOVE_POLL:
{
stop_thread (this);
break;
}
case SPA_EVENT_TYPE_REUSE_BUFFER:
{
PinosPort *port;
GError *error = NULL;
port = pinos_node_find_port (PINOS_NODE (this), event->port_id);
pinos_port_send_event (port, event, &error);
break;
}
default:
g_debug ("client-node %p: got event %d", this, event->type);
break;
}
}
static void
setup_node (PinosClientNode *this)
{
PinosNode *node = PINOS_NODE (this);
SpaResult res;
if ((res = spa_node_set_event_callback (node->node, on_node_event, this)) < 0)
g_warning ("client-node %p: error setting callback", this);
}
static PinosPort *
add_port (PinosNode *node,
PinosDirection direction,
@ -509,12 +341,13 @@ add_port (PinosNode *node,
{
PinosPort *port;
if (spa_node_add_port (node->node, direction, id) < 0)
g_warning ("client-node %p: error adding port", node);
port = PINOS_NODE_CLASS (pinos_client_node_parent_class)->add_port (node, direction, id, error);
if (port) {
pinos_port_set_received_buffer_cb (port, on_received_buffer, node, NULL);
g_signal_connect (port, "notify::format", (GCallback) on_format_change, node);
pinos_port_set_received_cb (port, on_received_buffer, on_received_event, node, NULL);
}
return port;
}
@ -523,16 +356,18 @@ static gboolean
remove_port (PinosNode *node,
guint id)
{
if (spa_node_remove_port (node->node, id) < 0)
g_warning ("client-node %p: error removing port", node);
return PINOS_NODE_CLASS (pinos_client_node_parent_class)->remove_port (node, id);
}
static void
pinos_client_node_dispose (GObject * object)
{
PinosClientNode *node = PINOS_CLIENT_NODE (object);
PinosClientNode *this = PINOS_CLIENT_NODE (object);
g_debug ("client-node %p: dispose", node);
unhandle_socket (node);
g_debug ("client-node %p: dispose", this);
G_OBJECT_CLASS (pinos_client_node_parent_class)->dispose (object);
}
@ -540,11 +375,9 @@ pinos_client_node_dispose (GObject * object)
static void
pinos_client_node_finalize (GObject * object)
{
PinosClientNode *node = PINOS_CLIENT_NODE (object);
PinosClientNodePrivate *priv = node->priv;
PinosClientNode *this = PINOS_CLIENT_NODE (object);
g_debug ("client-node %p: finalize", node);
g_hash_table_unref (priv->mem_ids);
g_debug ("client-node %p: finalize", this);
G_OBJECT_CLASS (pinos_client_node_parent_class)->finalize (object);
}
@ -552,11 +385,13 @@ pinos_client_node_finalize (GObject * object)
static void
pinos_client_node_constructed (GObject * object)
{
PinosClientNode *node = PINOS_CLIENT_NODE (object);
PinosClientNode *this = PINOS_CLIENT_NODE (object);
g_debug ("client-node %p: constructed", node);
g_debug ("client-node %p: constructed", this);
G_OBJECT_CLASS (pinos_client_node_parent_class)->constructed (object);
setup_node (this);
}
static void
@ -584,3 +419,85 @@ pinos_client_node_init (PinosClientNode * node)
g_debug ("client-node %p: new", node);
}
static SpaResult
make_node (SpaNode **node, const char *lib, const char *name)
{
SpaHandle *handle;
SpaResult res;
void *hnd, *state = NULL;
SpaEnumHandleFactoryFunc enum_func;
if ((hnd = dlopen (lib, RTLD_NOW)) == NULL) {
g_error ("can't load %s: %s", lib, dlerror());
return SPA_RESULT_ERROR;
}
if ((enum_func = dlsym (hnd, "spa_enum_handle_factory")) == NULL) {
g_error ("can't find enum function");
return SPA_RESULT_ERROR;
}
while (true) {
const SpaHandleFactory *factory;
void *iface;
if ((res = enum_func (&factory, &state)) < 0) {
if (res != SPA_RESULT_ENUM_END)
g_error ("can't enumerate factories: %d", res);
break;
}
if (strcmp (factory->name, name))
continue;
handle = calloc (1, factory->size);
if ((res = factory->init (factory, handle)) < 0) {
g_error ("can't make factory instance: %d", res);
return res;
}
if ((res = handle->get_interface (handle, SPA_INTERFACE_ID_NODE, &iface)) < 0) {
g_error ("can't get interface %d", res);
return res;
}
*node = iface;
return SPA_RESULT_OK;
}
return SPA_RESULT_ERROR;
}
/**
* pinos_client_node_new:
* @daemon: a #PinosDaemon
* @sender: the path of the owner
* @name: a name
* @properties: extra properties
*
* Create a new #PinosNode.
*
* Returns: a new #PinosNode
*/
PinosNode *
pinos_client_node_new (PinosDaemon *daemon,
const gchar *sender,
const gchar *name,
PinosProperties *properties)
{
SpaNode *n;
SpaResult res;
g_return_val_if_fail (PINOS_IS_DAEMON (daemon), NULL);
if ((res = make_node (&n,
"spa/build/plugins/remote/libspa-remote.so",
"proxy")) < 0) {
g_error ("can't create proxy: %d", res);
return NULL;
}
return g_object_new (PINOS_TYPE_CLIENT_NODE,
"daemon", daemon,
"sender", sender,
"name", name,
"properties", properties,
"node", n,
NULL);
}

View file

@ -62,6 +62,11 @@ struct _PinosClientNodeClass {
/* normal GObject stuff */
GType pinos_client_node_get_type (void);
PinosNode * pinos_client_node_new (PinosDaemon *daemon,
const gchar *sender,
const gchar *name,
PinosProperties *properties);
GSocket * pinos_client_node_get_socket_pair (PinosClientNode *node,
GError **error);

View file

@ -148,7 +148,8 @@ handle_create_node (PinosDaemon1 *interface,
node = pinos_node_new (daemon,
sender,
arg_name,
props);
props,
NULL);
}
pinos_properties_free (props);
@ -204,12 +205,10 @@ handle_create_client_node (PinosDaemon1 *interface,
g_debug ("daemon %p: create client-node: %s", daemon, sender);
props = pinos_properties_from_variant (arg_properties);
node = g_object_new (PINOS_TYPE_CLIENT_NODE,
"daemon", daemon,
"sender", sender,
"name", arg_name,
"properties", props,
NULL);
node = pinos_client_node_new (daemon,
sender,
arg_name,
props);
client = sender_get_client (daemon, sender);

View file

@ -21,6 +21,8 @@
#include <gio/gio.h>
#include <spa/include/spa/video/format.h>
#include "pinos/client/pinos.h"
#include "pinos/client/enumtypes.h"
@ -40,11 +42,24 @@ struct _PinosLinkPrivate
gchar *object_path;
gulong input_id, output_id;
gboolean active;
gboolean negotiated;
gboolean allocated;
PinosPort *output;
PinosPort *input;
SpaNode *output_node;
uint32_t output_port;
SpaNode *input_node;
uint32_t input_port;
GBytes *possible_formats;
GBytes *format;
SpaBuffer *buffers[16];
unsigned int n_buffers;
};
G_DEFINE_TYPE (PinosLink, pinos_link, G_TYPE_OBJECT);
@ -69,21 +84,39 @@ enum
static guint signals[LAST_SIGNAL] = { 0 };
static gboolean
on_output_send (PinosPort *port, SpaBuffer *buffer, GError **error, gpointer user_data)
on_output_buffer (PinosPort *port, uint32_t buffer_id, GError **error, gpointer user_data)
{
PinosLink *link = user_data;
PinosLinkPrivate *priv = link->priv;
return pinos_port_receive_buffer (priv->input, buffer, error);
return pinos_port_receive_buffer (priv->input, buffer_id, error);
}
static gboolean
on_input_send (PinosPort *port, SpaBuffer *buffer, GError **error, gpointer user_data)
on_output_event (PinosPort *port, SpaEvent *event, GError **error, gpointer user_data)
{
PinosLink *link = user_data;
PinosLinkPrivate *priv = link->priv;
return pinos_port_receive_buffer (priv->output, buffer, error);
return pinos_port_receive_event (priv->input, event, error);
}
static gboolean
on_input_buffer (PinosPort *port, uint32_t buffer_id, GError **error, gpointer user_data)
{
PinosLink *link = user_data;
PinosLinkPrivate *priv = link->priv;
return pinos_port_receive_buffer (priv->output, buffer_id, error);
}
static gboolean
on_input_event (PinosPort *port, SpaEvent *event, GError **error, gpointer user_data)
{
PinosLink *link = user_data;
PinosLinkPrivate *priv = link->priv;
return pinos_port_receive_event (priv->output, event, error);
}
static void
@ -142,10 +175,14 @@ pinos_link_set_property (GObject *_object,
case PROP_OUTPUT:
priv->output = g_value_dup_object (value);
priv->output_node = priv->output->node->node;
priv->output_port = priv->output->id;
break;
case PROP_INPUT:
priv->input = g_value_dup_object (value);
priv->input_node = priv->input->node->node;
priv->input_port = priv->input->id;
break;
case PROP_OBJECT_PATH:
@ -199,11 +236,101 @@ link_unregister_object (PinosLink *link)
pinos_daemon_unexport (priv->daemon, priv->object_path);
}
static SpaResult
do_negotiate (PinosLink *this)
{
PinosLinkPrivate *priv = this->priv;
SpaResult res;
SpaFormat *format;
SpaProps *props;
uint32_t val;
SpaPropValue value;
void *state = NULL;
SpaFraction frac;
SpaRectangle rect;
g_debug ("link %p: doing set format", this);
if ((res = spa_node_port_enum_formats (priv->output_node, priv->output_port, &format, NULL, &state)) < 0) {
g_warning ("error enum formats: %d", res);
return res;
}
props = &format->props;
value.type = SPA_PROP_TYPE_UINT32;
value.size = sizeof (uint32_t);
value.value = &val;
val = SPA_VIDEO_FORMAT_YUY2;
if ((res = spa_props_set_prop (props, spa_props_index_for_id (props, SPA_PROP_ID_VIDEO_FORMAT), &value)) < 0)
return res;
value.type = SPA_PROP_TYPE_RECTANGLE;
value.size = sizeof (SpaRectangle);
value.value = &rect;
rect.width = 320;
rect.height = 240;
if ((res = spa_props_set_prop (props, spa_props_index_for_id (props, SPA_PROP_ID_VIDEO_SIZE), &value)) < 0)
return res;
value.type = SPA_PROP_TYPE_FRACTION;
value.size = sizeof (SpaFraction);
value.value = &frac;
frac.num = 25;
frac.denom = 1;
if ((res = spa_props_set_prop (props, spa_props_index_for_id (props, SPA_PROP_ID_VIDEO_FRAMERATE), &value)) < 0)
return res;
if ((res = spa_node_port_set_format (priv->output_node, priv->output_port, 0, format)) < 0) {
g_warning ("error set format output: %d", res);
return res;
}
if ((res = spa_node_port_set_format (priv->input_node, priv->input_port, 0, format)) < 0) {
g_warning ("error set format input: %d", res);
return res;
}
priv->negotiated = TRUE;
return SPA_RESULT_OK;
}
static SpaResult
do_allocation (PinosLink *this)
{
PinosLinkPrivate *priv = this->priv;
SpaResult res;
g_debug ("link %p: doing alloc buffers", this);
priv->n_buffers = 16;
if ((res = spa_node_port_alloc_buffers (priv->output_node, priv->output_port,
NULL, 0,
priv->buffers, &priv->n_buffers)) < 0) {
g_warning ("error alloc buffers: %d", res);
return res;
}
if ((res = spa_node_port_use_buffers (priv->input_node, priv->input_port,
priv->buffers, priv->n_buffers)) < 0) {
g_warning ("error alloc buffers: %d", res);
return res;
}
priv->allocated = TRUE;
return SPA_RESULT_OK;
}
static gboolean
on_activate (PinosPort *port, gpointer user_data)
{
PinosLink *link = user_data;
PinosLinkPrivate *priv = link->priv;
PinosLink *this = user_data;
PinosLinkPrivate *priv = this->priv;
SpaCommand cmd;
SpaResult res;
if (priv->active)
return TRUE;
@ -214,6 +341,19 @@ on_activate (PinosPort *port, gpointer user_data)
pinos_port_activate (priv->input);
priv->active = TRUE;
if (!priv->negotiated)
do_negotiate (this);
/* negotiate allocation */
if (!priv->allocated)
do_allocation (this);
cmd.type = SPA_COMMAND_START;
if ((res = spa_node_send_command (priv->input_node, &cmd)) < 0)
g_warning ("got error %d", res);
if ((res = spa_node_send_command (priv->output_node, &cmd)) < 0)
g_warning ("got error %d", res);
return TRUE;
}
@ -222,6 +362,8 @@ on_deactivate (PinosPort *port, gpointer user_data)
{
PinosLink *link = user_data;
PinosLinkPrivate *priv = link->priv;
SpaCommand cmd;
SpaResult res;
if (!priv->active)
return TRUE;
@ -232,46 +374,32 @@ on_deactivate (PinosPort *port, gpointer user_data)
pinos_port_deactivate (priv->input);
priv->active = FALSE;
cmd.type = SPA_COMMAND_STOP;
if ((res = spa_node_send_command (priv->input_node, &cmd)) < 0)
g_warning ("got error %d", res);
if ((res = spa_node_send_command (priv->output_node, &cmd)) < 0)
g_warning ("got error %d", res);
return TRUE;
}
static void
on_format_change (GObject *obj,
GParamSpec *pspec,
gpointer user_data)
{
PinosLink *link = user_data;
PinosLinkPrivate *priv = link->priv;
GBytes *formats;
g_object_get (priv->output, "format", &formats, NULL);
g_debug ("port %p: format change %s", priv->output, (gchar*)g_bytes_get_data (formats, NULL));
g_object_set (priv->input, "format", formats, NULL);
}
static void
pinos_link_constructed (GObject * object)
{
PinosLink *link = PINOS_LINK (object);
PinosLinkPrivate *priv = link->priv;
GBytes *formats;
priv->output_id = pinos_port_add_send_buffer_cb (priv->output,
on_output_send,
priv->output_id = pinos_port_add_send_cb (priv->output,
on_output_buffer,
on_output_event,
link,
NULL);
priv->input_id = pinos_port_add_send_buffer_cb (priv->input,
on_input_send,
priv->input_id = pinos_port_add_send_cb (priv->input,
on_input_buffer,
on_input_event,
link,
NULL);
g_object_get (priv->input, "possible-formats", &formats, NULL);
g_object_set (priv->output, "possible-formats", formats, NULL);
g_object_get (priv->output, "format", &formats, NULL);
g_object_set (priv->input, "format", formats, NULL);
g_signal_connect (priv->output, "notify::format", (GCallback) on_format_change, link);
g_signal_connect (priv->input, "activate", (GCallback) on_activate, link);
g_signal_connect (priv->input, "deactivate", (GCallback) on_deactivate, link);
g_signal_connect (priv->output, "activate", (GCallback) on_activate, link);
@ -291,8 +419,8 @@ pinos_link_dispose (GObject * object)
g_debug ("link %p: dispose", link);
pinos_port_remove_send_buffer_cb (priv->input, priv->input_id);
pinos_port_remove_send_buffer_cb (priv->output, priv->output_id);
pinos_port_remove_send_cb (priv->input, priv->input_id);
pinos_port_remove_send_cb (priv->output, priv->output_id);
if (priv->active) {
priv->active = FALSE;
pinos_port_deactivate (priv->input);
@ -415,7 +543,7 @@ pinos_link_new (PinosDaemon *daemon,
PinosLink *link;
PinosPort *tmp;
if (pinos_port_get_direction (output) != PINOS_DIRECTION_OUTPUT) {
if (output->direction != PINOS_DIRECTION_OUTPUT) {
tmp = output;
output = input;
input = tmp;

View file

@ -63,6 +63,7 @@ enum
PROP_NAME,
PROP_STATE,
PROP_PROPERTIES,
PROP_NODE,
};
enum
@ -239,6 +240,10 @@ pinos_node_get_property (GObject *_object,
g_value_set_boxed (value, priv->properties);
break;
case PROP_NODE:
g_value_set_pointer (value, node->node);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (node, prop_id, pspec);
break;
@ -273,6 +278,10 @@ pinos_node_set_property (GObject *_object,
priv->properties = g_value_dup_boxed (value);
break;
case PROP_NODE:
node->node = g_value_get_pointer (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (node, prop_id, pspec);
break;
@ -456,6 +465,15 @@ pinos_node_class_init (PinosNodeClass * klass)
G_PARAM_CONSTRUCT |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_NODE,
g_param_spec_pointer ("node",
"Node",
"The SPA node",
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
signals[SIGNAL_REMOVE] = g_signal_new ("remove",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
@ -509,7 +527,8 @@ PinosNode *
pinos_node_new (PinosDaemon *daemon,
const gchar *sender,
const gchar *name,
PinosProperties *properties)
PinosProperties *properties,
SpaNode *node)
{
g_return_val_if_fail (PINOS_IS_DAEMON (daemon), NULL);
@ -518,6 +537,7 @@ pinos_node_new (PinosDaemon *daemon,
"sender", sender,
"name", name,
"properties", properties,
"node", node,
NULL);
}
@ -653,7 +673,7 @@ pinos_node_remove (PinosNode *node)
static void
do_remove_port (PinosPort *port, PinosNode *node)
{
pinos_node_remove_port (node, pinos_port_get_id (port));
pinos_node_remove_port (node, port->id);
}
/**

View file

@ -28,6 +28,8 @@ typedef struct _PinosNode PinosNode;
typedef struct _PinosNodeClass PinosNodeClass;
typedef struct _PinosNodePrivate PinosNodePrivate;
#include <spa/include/spa/node.h>
#include <pinos/client/introspect.h>
#include <pinos/server/daemon.h>
#include <pinos/server/port.h>
@ -49,6 +51,8 @@ typedef struct _PinosNodePrivate PinosNodePrivate;
struct _PinosNode {
GObject object;
SpaNode *node;
PinosNodePrivate *priv;
};
@ -78,7 +82,8 @@ GType pinos_node_get_type (void);
PinosNode * pinos_node_new (PinosDaemon *daemon,
const gchar *sender,
const gchar *name,
PinosProperties *properties);
PinosProperties *properties,
SpaNode *node);
void pinos_node_remove (PinosNode *node);
const gchar * pinos_node_get_name (PinosNode *node);

View file

@ -40,25 +40,25 @@
typedef struct {
gulong id;
PinosBufferCallback send_buffer_cb;
gpointer send_buffer_data;
GDestroyNotify send_buffer_notify;
PinosEventCallback send_event_cb;
gpointer send_data;
GDestroyNotify send_notify;
} SendData;
struct _PinosPortPrivate
{
PinosDaemon *daemon;
guint id;
gulong data_id;
PinosNode *node;
PinosDirection direction;
GBytes *possible_formats;
GBytes *format;
PinosProperties *properties;
PinosBufferCallback received_buffer_cb;
gpointer received_buffer_data;
GDestroyNotify received_buffer_notify;
PinosEventCallback received_event_cb;
gpointer received_data;
GDestroyNotify received_notify;
gint active_count;
@ -92,10 +92,11 @@ static guint signals[LAST_SIGNAL] = { 0 };
void
pinos_port_set_received_buffer_cb (PinosPort *port,
PinosBufferCallback cb,
gpointer user_data,
GDestroyNotify notify)
pinos_port_set_received_cb (PinosPort *port,
PinosBufferCallback buffer_cb,
PinosEventCallback event_cb,
gpointer user_data,
GDestroyNotify notify)
{
PinosPortPrivate *priv;
@ -104,41 +105,45 @@ pinos_port_set_received_buffer_cb (PinosPort *port,
g_debug ("port %p: set receive callback", port);
if (priv->received_buffer_notify)
priv->received_buffer_notify (priv->received_buffer_data);;
priv->received_buffer_cb = cb;
priv->received_buffer_data = user_data;
priv->received_buffer_notify = notify;
if (priv->received_notify)
priv->received_notify (priv->received_data);;
priv->received_buffer_cb = buffer_cb;
priv->received_event_cb = event_cb;
priv->received_data = user_data;
priv->received_notify = notify;
}
gulong
pinos_port_add_send_buffer_cb (PinosPort *port,
PinosBufferCallback cb,
gpointer user_data,
GDestroyNotify notify)
pinos_port_add_send_cb (PinosPort *port,
PinosBufferCallback buffer_cb,
PinosEventCallback event_cb,
gpointer user_data,
GDestroyNotify notify)
{
PinosPortPrivate *priv;
SendData *data;
g_return_val_if_fail (PINOS_IS_PORT (port), -1);
g_return_val_if_fail (cb != NULL, -1);
g_return_val_if_fail (buffer_cb != NULL, -1);
g_return_val_if_fail (event_cb != NULL, -1);
priv = port->priv;
g_debug ("port %p: add send callback", port);
data = g_slice_new (SendData);
data->id = priv->id++;
data->send_buffer_cb = cb;
data->send_buffer_data = user_data;
data->send_buffer_notify = notify;
data->id = priv->data_id++;
data->send_buffer_cb = buffer_cb;
data->send_event_cb = event_cb;
data->send_data = user_data;
data->send_notify = notify;
priv->send_datas = g_list_prepend (priv->send_datas, data);
return data->id;
}
void
pinos_port_remove_send_buffer_cb (PinosPort *port,
gulong id)
pinos_port_remove_send_cb (PinosPort *port,
gulong id)
{
PinosPortPrivate *priv;
GList *walk;
@ -151,8 +156,8 @@ pinos_port_remove_send_buffer_cb (PinosPort *port,
SendData *data = walk->data;
if (data->id == id) {
if (data->send_buffer_notify)
data->send_buffer_notify (data->send_buffer_data);;
if (data->send_notify)
data->send_notify (data->send_data);;
g_slice_free (SendData, data);
priv->send_datas = g_list_delete_link (priv->send_datas, walk);
break;
@ -175,15 +180,15 @@ pinos_port_get_property (GObject *_object,
break;
case PROP_NODE:
g_value_set_object (value, priv->node);
g_value_set_object (value, port->node);
break;
case PROP_DIRECTION:
g_value_set_enum (value, priv->direction);
g_value_set_enum (value, port->direction);
break;
case PROP_ID:
g_value_set_uint (value, priv->id);
g_value_set_uint (value, port->id);
break;
case PROP_POSSIBLE_FORMATS:
@ -219,15 +224,15 @@ pinos_port_set_property (GObject *_object,
break;
case PROP_NODE:
priv->node = g_value_get_object (value);
port->node = g_value_get_object (value);
break;
case PROP_DIRECTION:
priv->direction = g_value_get_enum (value);
port->direction = g_value_get_enum (value);
break;
case PROP_ID:
priv->id = g_value_get_uint (value);
port->id = g_value_get_uint (value);
break;
case PROP_POSSIBLE_FORMATS:
@ -285,12 +290,12 @@ pinos_port_finalize (GObject * object)
g_clear_pointer (&priv->possible_formats, g_bytes_unref);
g_clear_pointer (&priv->format, g_bytes_unref);
g_clear_pointer (&priv->properties, pinos_properties_free);
if (priv->received_buffer_notify)
priv->received_buffer_notify (priv->received_buffer_data);
if (priv->received_notify)
priv->received_notify (priv->received_data);
for (walk = priv->send_datas; walk; walk = g_list_next (walk)) {
SendData *data = walk->data;
if (data->send_buffer_notify)
data->send_buffer_notify (data->send_buffer_data);
if (data->send_notify)
data->send_notify (data->send_data);
g_slice_free (SendData, data);
}
g_list_free (priv->send_datas);
@ -435,7 +440,7 @@ pinos_port_init (PinosPort * port)
PinosPortPrivate *priv = port->priv = PINOS_PORT_GET_PRIVATE (port);
g_debug ("port %p: new", port);
priv->direction = PINOS_DIRECTION_INVALID;
port->direction = PINOS_DIRECTION_INVALID;
}
/**
@ -453,63 +458,6 @@ pinos_port_remove (PinosPort *port)
g_signal_emit (port, signals[SIGNAL_REMOVE], 0, NULL);
}
/**
* pinos_port_get_node:
* @port: a #PinosPort
*
* Get the parent #PinosNode of @port
*
* Returns: the parent node or %NULL
*/
PinosNode *
pinos_port_get_node (PinosPort *port)
{
PinosPortPrivate *priv;
g_return_val_if_fail (PINOS_IS_PORT (port), NULL);
priv = port->priv;
return priv->node;
}
/**
* pinos_port_get_direction:
* @port: a #PinosPort
*
* Get the direction of @port
*
* Returns: the direction or %NULL
*/
PinosDirection
pinos_port_get_direction (PinosPort *port)
{
PinosPortPrivate *priv;
g_return_val_if_fail (PINOS_IS_PORT (port), PINOS_DIRECTION_INVALID);
priv = port->priv;
return priv->direction;
}
/**
* pinos_port_get_id:
* @port: a #PinosPort
*
* Get the id of @port
*
* Returns: the id or %NULL
*/
guint
pinos_port_get_id (PinosPort *port)
{
PinosPortPrivate *priv;
g_return_val_if_fail (PINOS_IS_PORT (port), -1);
priv = port->priv;
return priv->id;
}
/**
* pinos_port_get_possible_formats:
* @port: a #PinosPort
@ -625,11 +573,10 @@ pinos_port_deactivate (PinosPort *port)
g_signal_emit (port, signals[SIGNAL_DEACTIVATE], 0, NULL);
}
/**
* pinos_port_receive_buffer:
* @port: a #PinosPort
* @buffer: a #PinosBuffer
* @buffer_id: a buffer id
* @error: a #GError or %NULL
*
* Receive @buffer on @port
@ -638,16 +585,42 @@ pinos_port_deactivate (PinosPort *port)
*/
gboolean
pinos_port_receive_buffer (PinosPort *port,
SpaBuffer *buffer,
uint32_t buffer_id,
GError **error)
{
gboolean res = TRUE;
PinosPortPrivate *priv = port->priv;
PINOS_DEBUG_TRANSPORT ("port %p: receive buffer %p", port, buffer);
PINOS_DEBUG_TRANSPORT ("port %p: receive buffer %d", port, buffer_id);
if (priv->received_buffer_cb)
res = priv->received_buffer_cb (port, buffer, error, priv->received_buffer_data);
res = priv->received_buffer_cb (port, buffer_id, error, priv->received_data);
return res;
}
/**
* pinos_port_receive_event:
* @port: a #PinosPort
* @event: a #SpaEvent
* @error: a #GError or %NULL
*
* Receive @event on @port
*
* Returns: %TRUE on success. @error is set when %FALSE is returned.
*/
gboolean
pinos_port_receive_event (PinosPort *port,
SpaEvent *event,
GError **error)
{
gboolean res = TRUE;
PinosPortPrivate *priv = port->priv;
PINOS_DEBUG_TRANSPORT ("port %p: receive event %p", port, event);
if (priv->received_event_cb)
res = priv->received_event_cb (port, event, error, priv->received_data);
return res;
}
@ -655,7 +628,7 @@ pinos_port_receive_buffer (PinosPort *port,
/**
* pinos_port_send_buffer:
* @port: a #PinosPort
* @buffer: a #SpaBuffer
* @buffer_id: a buffer id
* @error: a #GError or %NULL
*
* Send @buffer out on @port.
@ -664,7 +637,7 @@ pinos_port_receive_buffer (PinosPort *port,
*/
gboolean
pinos_port_send_buffer (PinosPort *port,
SpaBuffer *buffer,
uint32_t buffer_id,
GError **error)
{
gboolean res = TRUE;
@ -673,13 +646,45 @@ pinos_port_send_buffer (PinosPort *port,
g_return_val_if_fail (PINOS_IS_PORT (port), FALSE);
PINOS_DEBUG_TRANSPORT ("port %p: send buffer %p", port, buffer);
PINOS_DEBUG_TRANSPORT ("port %p: send buffer %d", port, buffer_id);
priv = port->priv;
for (walk = priv->send_datas; walk; walk = g_list_next (walk)) {
SendData *data = walk->data;
data->send_buffer_cb (port, buffer, error, data->send_buffer_data);
data->send_buffer_cb (port, buffer_id, error, data->send_data);
}
return res;
}
/**
* pinos_port_send_event:
* @port: a #PinosPort
* @event: a #SpaEvent
* @error: a #GError or %NULL
*
* Send @event out on @port.
*
* Returns: %TRUE on success. @error is set when %FALSE is returned.
*/
gboolean
pinos_port_send_event (PinosPort *port,
SpaEvent *event,
GError **error)
{
gboolean res = TRUE;
PinosPortPrivate *priv;
GList *walk;
g_return_val_if_fail (PINOS_IS_PORT (port), FALSE);
PINOS_DEBUG_TRANSPORT ("port %p: send event %p", port, event);
priv = port->priv;
for (walk = priv->send_datas; walk; walk = g_list_next (walk)) {
SendData *data = walk->data;
data->send_event_cb (port, event, error, data->send_data);
}
return res;
}

View file

@ -51,6 +51,10 @@ typedef struct _PinosPortPrivate PinosPortPrivate;
struct _PinosPort {
GObject object;
PinosDirection direction;
uint32_t id;
PinosNode *node;
PinosPortPrivate *priv;
};
@ -67,28 +71,28 @@ struct _PinosPortClass {
GTask *task);
};
typedef gboolean (*PinosBufferCallback) (PinosPort *port, SpaBuffer *buffer, GError **error, gpointer user_data);
typedef gboolean (*PinosBufferCallback) (PinosPort *port, uint32_t buffer_id, GError **error, gpointer user_data);
typedef gboolean (*PinosEventCallback) (PinosPort *port, SpaEvent *event, GError **error, gpointer user_data);
/* normal GObject stuff */
GType pinos_port_get_type (void);
void pinos_port_set_received_buffer_cb (PinosPort *port,
PinosBufferCallback cb,
void pinos_port_set_received_cb (PinosPort *port,
PinosBufferCallback buffer_cb,
PinosEventCallback event_cb,
gpointer user_data,
GDestroyNotify notify);
gulong pinos_port_add_send_buffer_cb (PinosPort *port,
PinosBufferCallback cb,
gulong pinos_port_add_send_cb (PinosPort *port,
PinosBufferCallback buffer_cb,
PinosEventCallback event_cb,
gpointer user_data,
GDestroyNotify notify);
void pinos_port_remove_send_buffer_cb (PinosPort *port,
void pinos_port_remove_send_cb (PinosPort *port,
gulong id);
void pinos_port_remove (PinosPort *port);
PinosNode * pinos_port_get_node (PinosPort *port);
const gchar * pinos_port_get_name (PinosPort *port);
PinosDirection pinos_port_get_direction (PinosPort *port);
guint pinos_port_get_id (PinosPort *port);
PinosProperties * pinos_port_get_properties (PinosPort *port);
GBytes * pinos_port_get_possible_formats (PinosPort *port);
@ -97,14 +101,21 @@ GBytes * pinos_port_filter_formats (PinosPort *port,
GError **error);
GBytes * pinos_port_get_format (PinosPort *port);
void pinos_port_activate (PinosPort *port);
void pinos_port_deactivate (PinosPort *port);
gboolean pinos_port_send_buffer (PinosPort *port,
SpaBuffer *buffer,
uint32_t buffer_id,
GError **error);
gboolean pinos_port_send_event (PinosPort *port,
SpaEvent *event,
GError **error);
gboolean pinos_port_receive_buffer (PinosPort *port,
SpaBuffer *buffer,
uint32_t buffer_id,
GError **error);
gboolean pinos_port_receive_event (PinosPort *port,
SpaEvent *event,
GError **error);
G_END_DECLS