Rework how clients connect.

Add buffer flags. The idea is to make it possible to easily check when a
buffer contains control information that we need to parse to update the
port fields.
Make the client create remote nodes and ports and set up proxies for
them.
Make a port base class implementing most of the logic to pass buffers
locally and remotely.
Remove most code from stream.c, it's now in the port.
Make a portsink and portsrc that can write and read to/from any port. We
use these in the server to send and receive data.
Rework format negotiation. The final format is now sent in-line before
the data. The server will select a format on output ports.
This commit is contained in:
Wim Taymans 2016-05-17 09:38:30 +02:00
parent e85c3002f7
commit 4a5ed1e1f5
35 changed files with 3111 additions and 761 deletions

View file

@ -65,12 +65,16 @@ void
pinos_buffer_clear (PinosBuffer *buffer)
{
PinosStackBuffer *sb = PSB (buffer);
gint i;
g_return_if_fail (is_valid_buffer (buffer));
sb->magic = 0;
g_free (sb->free_data);
for (i = 0; i < sb->n_fds; i++)
close (sb->fds[i]);
g_free (sb->free_fds);
sb->n_fds = 0;
}
/**
@ -94,6 +98,27 @@ pinos_buffer_get_version (PinosBuffer *buffer)
return hdr->version;
}
/**
* pinos_buffer_get_flags
* @buffer: a #PinosBuffer
*
* Get the buffer flags
*
* Returns: the buffer flags.
*/
PinosBufferFlags
pinos_buffer_get_flags (PinosBuffer *buffer)
{
PinosStackBuffer *sb = PSB (buffer);
PinosStackHeader *hdr;
g_return_val_if_fail (is_valid_buffer (buffer), -1);
hdr = sb->data;
return hdr->flags;
}
/**
* pinos_buffer_get_fd:
* @buffer: a #PinosBuffer
@ -397,12 +422,30 @@ pinos_buffer_builder_init_full (PinosBufferBuilder *builder,
sh = sb->sh = sb->buf.data;
sh->version = version;
sh->flags = 0;
sh->length = 0;
sb->type = 0;
sb->offset = 0;
}
/**
* pinos_buffer_builder_set_flags:
* @builder: a #PinosBufferBuilder
* @flags: flags to set
*
* Set the flags on the buffer from @builder.
*/
void
pinos_buffer_builder_set_flags (PinosBufferBuilder *builder, PinosBufferFlags flags)
{
struct stack_builder *sb = PPSB (builder);
g_return_if_fail (is_valid_builder (builder));
sb->sh->flags = flags;
}
/**
* pinos_buffer_builder_clear:
* @builder: a #PinosBufferBuilder
@ -742,6 +785,7 @@ pinos_buffer_builder_add_format_change (PinosBufferBuilder *builder,
len);
*p++ = payload->id;
strcpy (p, payload->format);
sb->sh->flags |= PINOS_BUFFER_FLAG_CONTROL;
return TRUE;
}

View file

@ -31,6 +31,19 @@ typedef struct _PinosBufferBuilder PinosBufferBuilder;
#define PINOS_BUFFER_VERSION 0
/**
* PinosBufferFlags:
* @PINOS_BUFFER_FLAG_NONE: no flags
* @PINOS_BUFFER_FLAG_CONTROL: the buffer contains control info such
* as new format or properties.
*
* The possible buffer flags.
*/
typedef enum {
PINOS_BUFFER_FLAG_NONE = 0,
PINOS_BUFFER_FLAG_CONTROL = (1 << 0),
} PinosBufferFlags;
struct _PinosBuffer {
/*< private >*/
gsize x[16];
@ -45,6 +58,7 @@ void pinos_buffer_init_data (PinosBuffer *buffer,
void pinos_buffer_clear (PinosBuffer *buffer);
guint32 pinos_buffer_get_version (PinosBuffer *buffer);
PinosBufferFlags pinos_buffer_get_flags (PinosBuffer *buffer);
int pinos_buffer_get_fd (PinosBuffer *buffer,
gint index);
@ -116,6 +130,8 @@ void pinos_buffer_builder_init_full (PinosBufferBuilder *bui
#define pinos_buffer_builder_init_into(b,d,md,f,mf) pinos_buffer_builder_init_full(b, PINOS_BUFFER_VERSION,d,md,f,mf);
#define pinos_buffer_builder_init(b) pinos_buffer_builder_init_into(b, NULL, 0, NULL, 0);
void pinos_buffer_builder_set_flags (PinosBufferBuilder *builder,
PinosBufferFlags flags);
void pinos_buffer_builder_clear (PinosBufferBuilder *builder);
void pinos_buffer_builder_end (PinosBufferBuilder *builder,
PinosBuffer *buffer);

View file

@ -18,11 +18,14 @@
*/
#include <gio/gio.h>
#include <gio/gunixfdlist.h>
#include "pinos/client/pinos.h"
#include "pinos/client/subscribe.h"
#include "pinos/client/enumtypes.h"
#include "pinos/client/context.h"
#include "pinos/client/private.h"
#include "pinos/client/client-node.h"
#include "pinos/client/client-port.h"
@ -51,25 +54,163 @@ client_node_set_state (PinosNode *node,
return FALSE;
}
typedef struct {
PinosDirection direction;
gchar *name;
PinosProperties *properties;
GBytes *possible_formats;
GSocket *socket;
} CreatePortData;
static void
create_port_data_free (CreatePortData *data)
{
g_free (data->name);
if (data->properties)
pinos_properties_free (data->properties);
g_clear_object (&data->socket);
g_slice_free (CreatePortData, data);
}
static void
on_port_proxy (GObject *source_object,
GAsyncResult *res,
gpointer user_data)
{
GTask *task = user_data;
CreatePortData *data = g_task_get_task_data (task);
PinosClientNode *node = g_task_get_source_object (task);
PinosClientNodePrivate *priv = node->priv;
PinosContext *context = priv->context;
GError *error = NULL;
GDBusProxy *proxy;
PinosClientPort *port;
proxy = pinos_subscribe_get_proxy_finish (context->priv->subscribe,
res,
&error);
if (proxy == NULL)
goto port_failed;
port = pinos_client_port_new (node,
proxy,
data->socket);
g_task_return_pointer (task, port, (GDestroyNotify) g_object_unref);
g_object_unref (task);
return;
port_failed:
{
g_warning ("failed to get port proxy: %s", error->message);
g_task_return_error (task, error);
g_object_unref (task);
return;
}
}
static void
on_port_created (GObject *source_object,
GAsyncResult *res,
gpointer user_data)
{
GTask *task = user_data;
PinosClientNode *node = g_task_get_source_object (task);
CreatePortData *data = g_task_get_task_data (task);
PinosClientNodePrivate *priv = node->priv;
PinosContext *context = priv->context;
GVariant *ret;
GError *error = NULL;
const gchar *port_path;
GUnixFDList *fdlist;
gint fd, fd_idx;
g_assert (priv->proxy == G_DBUS_PROXY (source_object));
ret = g_dbus_proxy_call_with_unix_fd_list_finish (priv->proxy, &fdlist, res, &error);
if (ret == NULL)
goto create_failed;
g_variant_get (ret, "(&oh)", &port_path, &fd_idx);
fd = g_unix_fd_list_get (fdlist, fd_idx, &error);
g_object_unref (fdlist);
if (fd == -1)
goto create_failed;
data->socket = g_socket_new_from_fd (fd, &error);
if (data->socket == NULL)
goto create_failed;
pinos_subscribe_get_proxy (context->priv->subscribe,
PINOS_DBUS_SERVICE,
port_path,
"org.pinos.Port1",
NULL,
on_port_proxy,
task);
g_variant_unref (ret);
return;
/* ERRORS */
create_failed:
{
g_warning ("failed to create port: %s", error->message);
g_task_return_error (task, error);
g_object_unref (task);
return;
}
}
static gboolean
do_create_port (GTask *task)
{
PinosClientNode *node = g_task_get_source_object (task);
PinosClientNodePrivate *priv = node->priv;
CreatePortData *data = g_task_get_task_data (task);
g_dbus_proxy_call (priv->proxy,
"CreatePort",
g_variant_new ("(us@a{sv}s)",
data->direction,
data->name,
pinos_properties_to_variant (data->properties),
g_bytes_get_data (data->possible_formats, NULL)),
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL, /* GCancellable *cancellable */
on_port_created,
task);
return FALSE;
}
static void
client_node_create_port (PinosNode *node,
PinosDirection direction,
const gchar *name,
GBytes *possible_formats,
PinosProperties *props,
PinosProperties *properties,
GTask *task)
{
PinosPort *port;
PinosClientNodePrivate *priv = PINOS_CLIENT_NODE (node)->priv;
PinosContext *context = priv->context;
CreatePortData *data;
port = g_object_new (PINOS_TYPE_CLIENT_PORT,
"node", node,
"direction", direction,
"name", name,
"possible-formats", possible_formats,
"properties", props,
NULL);
data = g_slice_new (CreatePortData);
data->direction = direction;
data->name = g_strdup (name);
data->possible_formats = possible_formats ? g_bytes_ref (possible_formats) : NULL;
data->properties = properties ? pinos_properties_copy (properties) : NULL;
g_task_set_task_data (task, data, (GDestroyNotify) create_port_data_free);
g_main_context_invoke (context->priv->context,
(GSourceFunc) do_create_port,
task);
g_task_return_pointer (task, port, (GDestroyNotify) g_object_unref);
}
static void

View file

@ -32,7 +32,7 @@
struct _PinosClientPortPrivate
{
gint foo;
GDBusProxy *proxy;
};
G_DEFINE_TYPE (PinosClientPort, pinos_client_port, PINOS_TYPE_PORT);
@ -40,6 +40,7 @@ G_DEFINE_TYPE (PinosClientPort, pinos_client_port, PINOS_TYPE_PORT);
enum
{
PROP_0,
PROP_PROXY,
};
static void
@ -49,8 +50,13 @@ pinos_client_port_get_property (GObject *_object,
GParamSpec *pspec)
{
PinosClientPort *port = PINOS_CLIENT_PORT (_object);
PinosClientPortPrivate *priv = port->priv;
switch (prop_id) {
case PROP_PROXY:
g_value_set_object (value, priv->proxy);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (port, prop_id, pspec);
break;
@ -64,20 +70,126 @@ pinos_client_port_set_property (GObject *_object,
GParamSpec *pspec)
{
PinosClientPort *port = PINOS_CLIENT_PORT (_object);
PinosClientPortPrivate *priv = port->priv;
switch (prop_id) {
case PROP_PROXY:
priv->proxy = g_value_dup_object (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (port, prop_id, pspec);
break;
}
}
static void
proxy_g_properties_changed (GDBusProxy *_proxy,
GVariant *changed_properties,
GStrv invalidated_properties,
gpointer user_data)
{
PinosClientPort *port = user_data;
GVariantIter *iter;
gchar *key;
g_variant_get (changed_properties, "a{sv}", &iter);
while (g_variant_iter_next (iter, "{&sv}", &key, NULL)) {
GVariant *variant;
gsize size;
gpointer data;
GBytes *bytes;
g_debug ("changed %s", key);
variant = g_dbus_proxy_get_cached_property (_proxy, key);
if (variant == NULL)
continue;
if (strcmp (key, "Format") == 0) {
data = g_variant_dup_string (variant, &size);
bytes = g_bytes_new_take (data, size);
g_object_set (port, "format", bytes, NULL);
}
g_variant_unref (variant);
}
g_variant_iter_free (iter);
}
#if 0
static void
proxy_set_property_cb (GDBusProxy *proxy,
GAsyncResult *res,
gpointer user_data)
{
GVariant *ret;
GError *error = NULL;
ret = g_dbus_proxy_call_finish (proxy, res, &error);
if (ret == NULL) {
g_warning ("Error setting property: %s", error->message);
g_error_free (error);
} else
g_variant_unref (ret);
}
static void
on_property_notify (GObject *obj,
GParamSpec *pspec,
gpointer user_data)
{
PinosPort *port = PINOS_PORT (obj);
PinosClientPortPrivate *priv = PINOS_CLIENT_PORT (port)->priv;
const gchar *prop_name = NULL;
GVariant *variant;
g_debug ("update %s", pspec ? g_param_spec_get_name (pspec) : "NULL");
if (pspec == NULL || strcmp (g_param_spec_get_name (pspec), "properties") == 0) {
PinosProperties *props = pinos_port_get_properties (port);
prop_name = "Properties";
variant = props ? pinos_properties_to_variant (props) : NULL;
}
if (pspec == NULL || strcmp (g_param_spec_get_name (pspec), "possible-formats") == 0) {
GBytes *bytes = pinos_port_get_possible_formats (port);
prop_name = "PossibleFormats";
variant = bytes ? g_variant_new_string (g_bytes_get_data (bytes, NULL)) : NULL;
}
if (pspec == NULL || strcmp (g_param_spec_get_name (pspec), "format") == 0) {
GBytes *bytes = pinos_port_get_format (port);
prop_name = "Format";
variant = bytes ? g_variant_new_string (g_bytes_get_data (bytes, NULL)) : NULL;
}
if (prop_name) {
g_dbus_proxy_call (G_DBUS_PROXY (priv->proxy),
"org.freedesktop.DBus.Properties.Set",
g_variant_new ("(ssv)",
"org.pinos.Port1",
prop_name,
variant),
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
(GAsyncReadyCallback) proxy_set_property_cb,
port);
if (variant)
g_variant_unref (variant);
}
}
#endif
static void
pinos_client_port_constructed (GObject * object)
{
PinosClientPort *port = PINOS_CLIENT_PORT (object);
PinosClientPortPrivate *priv = port->priv;
g_debug ("client-port %p: constructed", port);
g_signal_connect (priv->proxy,
"g-properties-changed",
(GCallback) proxy_g_properties_changed,
port);
G_OBJECT_CLASS (pinos_client_port_parent_class)->constructed (object);
}
@ -92,12 +204,15 @@ pinos_client_port_dispose (GObject * object)
G_OBJECT_CLASS (pinos_client_port_parent_class)->dispose (object);
}
static void
pinos_client_port_finalize (GObject * object)
{
PinosClientPort *port = PINOS_CLIENT_PORT (object);
PinosClientPortPrivate *priv = port->priv;
g_debug ("client-port %p: finalize", port);
g_clear_object (&priv->proxy);
G_OBJECT_CLASS (pinos_client_port_parent_class)->finalize (object);
}
@ -114,6 +229,16 @@ pinos_client_port_class_init (PinosClientPortClass * klass)
gobject_class->finalize = pinos_client_port_finalize;
gobject_class->set_property = pinos_client_port_set_property;
gobject_class->get_property = pinos_client_port_get_property;
g_object_class_install_property (gobject_class,
PROP_PROXY,
g_param_spec_object ("proxy",
"Proxy",
"The Proxy",
G_TYPE_DBUS_PROXY,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
}
static void
@ -121,3 +246,72 @@ pinos_client_port_init (PinosClientPort * port)
{
port->priv = PINOS_CLIENT_PORT_GET_PRIVATE (port);
}
/**
* pinos_client_port_new:
* @node: a #PinosClientNode
* @id: an id
* @socket: a socket with the server port
*
* Create a new client port.
*
* Returns: a new client port
*/
PinosClientPort *
pinos_client_port_new (PinosClientNode *node,
gpointer id,
GSocket *socket)
{
PinosClientPort *port;
GDBusProxy *proxy = id;
GVariant *variant;
PinosDirection direction = PINOS_DIRECTION_INVALID;
const gchar *name = "unknown";
GBytes *possible_formats = NULL;
GBytes *format = NULL;
PinosProperties *properties = NULL;
variant = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (proxy), "Direction");
if (variant != NULL) {
direction = g_variant_get_uint32 (variant);
g_variant_unref (variant);
}
variant = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (proxy), "Name");
if (variant != NULL) {
name = g_variant_get_string (variant, NULL);
g_variant_unref (variant);
}
variant = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (proxy), "PossibleFormats");
if (variant != NULL) {
gsize size;
gpointer data;
data = g_variant_dup_string (variant, &size);
possible_formats = g_bytes_new_take (data, size);
g_variant_unref (variant);
}
variant = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (proxy), "Format");
if (variant != NULL) {
gsize size;
gpointer data;
data = g_variant_dup_string (variant, &size);
format = g_bytes_new_take (data, size);
g_variant_unref (variant);
}
variant = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (proxy), "Properties");
if (variant != NULL) {
properties = pinos_properties_from_variant (variant);
g_variant_unref (variant);
}
port = g_object_new (PINOS_TYPE_CLIENT_PORT,
"node", node,
"direction", direction,
"name", name,
"possible-formats", possible_formats,
"format", format,
"properties", properties,
"proxy", proxy,
"socket", socket,
NULL);
return port;
}

View file

@ -30,6 +30,7 @@ typedef struct _PinosClientPortPrivate PinosClientPortPrivate;
#include <pinos/client/introspect.h>
#include <pinos/client/port.h>
#include <pinos/client/client-node.h>
#define PINOS_TYPE_CLIENT_PORT (pinos_client_port_get_type ())
#define PINOS_IS_CLIENT_PORT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PINOS_TYPE_CLIENT_PORT))
@ -66,6 +67,10 @@ struct _PinosClientPortClass {
/* normal GObject stuff */
GType pinos_client_port_get_type (void);
PinosClientPort * pinos_client_port_new (PinosClientNode *node,
gpointer id,
GSocket *socket);
GBytes * pinos_client_port_get_formats (PinosClientPort *port,
GBytes *filter,
GError **error);

View file

@ -792,6 +792,7 @@ on_node_proxy (GObject *source_object,
NULL);
g_task_return_pointer (task, node, (GDestroyNotify) g_object_unref);
g_object_unref (task);
return;
@ -799,6 +800,7 @@ node_failed:
{
g_warning ("failed to get node proxy: %s", error->message);
g_task_return_error (task, error);
g_object_unref (task);
return;
}
}
@ -839,6 +841,7 @@ create_failed:
{
g_warning ("failed to create node: %s", error->message);
g_task_return_error (task, error);
g_object_unref (task);
return;
}
}

View file

@ -433,6 +433,7 @@ port_fill_info (PinosPortInfo *info, GDBusProxy *proxy)
SET_STRING ("Name", name, 0);
SET_PROPERTIES ("Properties", properties, 1);
SET_BYTES ("PossibleFormats", possible_formats, 2);
SET_BYTES ("Format", format, 3);
if (changed)
g_hash_table_remove_all (changed);
@ -445,6 +446,8 @@ port_clear_info (PinosPortInfo *info)
pinos_properties_free (info->properties);
if (info->possible_formats)
g_bytes_unref (info->possible_formats);
if (info->format)
g_bytes_unref (info->format);
}
/**

View file

@ -267,7 +267,8 @@ void pinos_context_get_node_info_by_id (PinosContext *context,
* @change_mask: bitfield of changed fields since last call
* @name: name the port, suitable for display
* @properties: the properties of the port
* @possible formats: the possible formats this port can consume
* @possible_formats: the possible formats this port can consume
* @format: the current format on this port
*
* The port information. Extra information can be added in later
* versions.
@ -281,6 +282,7 @@ typedef struct {
const char *name;
PinosProperties *properties;
GBytes *possible_formats;
GBytes *format;
} PinosPortInfo;
/**

View file

@ -109,6 +109,7 @@ pinos_main_loop_constructed (GObject * object)
PinosMainLoopPrivate *priv = loop->priv;
priv->mainloop = g_main_loop_new (priv->maincontext, FALSE);
g_debug ("mainloop %p: contructed %p %p", loop, priv->maincontext, priv->mainloop);
G_OBJECT_CLASS (pinos_main_loop_parent_class)->constructed (object);
}
@ -267,7 +268,9 @@ handle_mainloop (PinosMainLoop *loop)
g_main_context_set_poll_func (priv->maincontext, do_poll);
g_main_context_push_thread_default (priv->maincontext);
g_debug ("mainloop %p: run mainloop %p context %p", loop, priv->mainloop, priv->maincontext);
g_main_loop_run (priv->mainloop);
g_debug ("mainloop %p: done", loop);
g_main_context_pop_thread_default (priv->maincontext);
g_main_context_set_poll_func (priv->maincontext, priv->poll_func);

View file

@ -130,6 +130,7 @@ pinos_node_dispose (GObject * obj)
PinosNodePrivate *priv = node->priv;
g_debug ("node %p: dispose", node);
pinos_node_set_state (node, PINOS_NODE_STATE_SUSPENDED);
g_list_free_full (priv->ports, (GDestroyNotify) g_object_unref);
priv->ports = NULL;
@ -215,6 +216,63 @@ pinos_node_init (PinosNode * node)
priv->state = PINOS_NODE_STATE_SUSPENDED;
}
/**
* pinos_node_get_name:
* @node: a #PinosNode
*
* Get the name of @node
*
* Returns: the name of @node
*/
const gchar *
pinos_node_get_name (PinosNode *node)
{
PinosNodePrivate *priv;
g_return_val_if_fail (PINOS_IS_NODE (node), NULL);
priv = node->priv;
return priv->name;
}
/**
* pinos_node_get_state:
* @node: a #PinosNode
*
* Get the state of @node
*
* Returns: the state of @node
*/
PinosNodeState
pinos_node_get_state (PinosNode *node)
{
PinosNodePrivate *priv;
g_return_val_if_fail (PINOS_IS_NODE (node), PINOS_NODE_STATE_ERROR);
priv = node->priv;
return priv->state;
}
/**
* pinos_node_get_properties:
* @node: a #PinosNode
*
* Get the properties of @node
*
* Returns: the properties of @node
*/
PinosProperties *
pinos_node_get_properties (PinosNode *node)
{
PinosNodePrivate *priv;
g_return_val_if_fail (PINOS_IS_NODE (node), NULL);
priv = node->priv;
return priv->properties;
}
/**
* pinos_node_remove:
* @node: a #PinosNode
@ -227,7 +285,7 @@ pinos_node_remove (PinosNode *node)
{
g_return_if_fail (PINOS_IS_NODE (node));
g_debug ("node %p: remove", node);
g_debug ("node %p: remove %d", node, G_OBJECT (node)->ref_count);
g_signal_emit (node, signals[SIGNAL_REMOVE], 0, NULL);
}
@ -269,7 +327,9 @@ pinos_node_create_port (PinosNode *node,
if (!klass->create_port)
return;
g_debug ("node %p %d: create port", node, G_OBJECT (node)->ref_count);
task = g_task_new (node, cancellable, callback, user_data);
g_debug ("node %p %d: create port", node, G_OBJECT (node)->ref_count);
klass->create_port (node, direction, name, possible_formats, props, task);
}
@ -289,6 +349,7 @@ pinos_node_create_port_finish (PinosNode *node,
priv->ports = g_list_append (priv->ports, port);
g_signal_connect (port, "remove", (GCallback) handle_remove_port, node);
}
g_debug ("node %p %d: created port %p", node, G_OBJECT (node)->ref_count, port);
return port;
}

View file

@ -79,6 +79,10 @@ struct _PinosNodeClass {
/* normal GObject stuff */
GType pinos_node_get_type (void);
const gchar * pinos_node_get_name (PinosNode *node);
PinosNodeState pinos_node_get_state (PinosNode *node);
PinosProperties * pinos_node_get_properties (PinosNode *node);
void pinos_node_remove (PinosNode *node);
void pinos_node_create_port (PinosNode *node,

View file

@ -104,3 +104,13 @@ pinos_fill_stream_properties (PinosProperties *properties)
{
g_return_if_fail (properties != NULL);
}
PinosDirection
pinos_direction_reverse (PinosDirection direction)
{
if (direction == PINOS_DIRECTION_INPUT)
return PINOS_DIRECTION_OUTPUT;
else if (direction == PINOS_DIRECTION_OUTPUT)
return PINOS_DIRECTION_INPUT;
return direction;
}

View file

@ -43,4 +43,6 @@ gchar *pinos_client_name (void);
void pinos_fill_context_properties (PinosProperties *properties);
void pinos_fill_stream_properties (PinosProperties *properties);
PinosDirection pinos_direction_reverse (PinosDirection direction);
#endif /* __PINOS_H__ */

File diff suppressed because it is too large Load diff

View file

@ -29,6 +29,7 @@ typedef struct _PinosPortClass PinosPortClass;
typedef struct _PinosPortPrivate PinosPortPrivate;
#include <pinos/client/introspect.h>
#include <pinos/client/buffer.h>
#define PINOS_TYPE_PORT (pinos_port_get_type ())
#define PINOS_IS_PORT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PINOS_TYPE_PORT))
@ -59,15 +60,52 @@ struct _PinosPortClass {
GObjectClass parent_class;
};
typedef void (*PinosReceivedBufferCallback) (PinosPort *port, gpointer user_data);
/* normal GObject stuff */
GType pinos_port_get_type (void);
void pinos_port_remove (PinosPort *port);
void pinos_port_set_received_buffer_cb (PinosPort *port,
PinosReceivedBufferCallback cb,
gpointer user_data,
GDestroyNotify notify);
PinosNode * pinos_port_get_node (PinosPort *port);
GBytes * pinos_port_get_formats (PinosPort *port,
GBytes *filter,
GError **error);
void pinos_port_remove (PinosPort *port);
PinosNode * pinos_port_get_node (PinosPort *port);
GSocket * pinos_port_get_socket (PinosPort *port);
const gchar * pinos_port_get_name (PinosPort *port);
PinosDirection pinos_port_get_direction (PinosPort *port);
GBytes * pinos_port_get_possible_formats (PinosPort *port);
GBytes * pinos_port_get_format (PinosPort *port);
PinosProperties * pinos_port_get_properties (PinosPort *port);
GBytes * pinos_port_filter_formats (PinosPort *port,
GBytes *filter,
GError **error);
gboolean pinos_port_update_format (PinosPort *port,
GBytes *format,
GError **error);
GSocket * pinos_port_get_socket_pair (PinosPort *port,
GError **error);
gboolean pinos_port_link (PinosPort *source,
PinosPort *destination);
gboolean pinos_port_unlink (PinosPort *source,
PinosPort *destination);
gint pinos_port_get_n_links (PinosPort *port);
gboolean pinos_port_receive_buffer (PinosPort *port,
PinosBuffer *buffer,
GError **error);
PinosBuffer * pinos_port_peek_buffer (PinosPort *port);
PinosBuffer * pinos_port_get_buffer (PinosPort *port);
gboolean pinos_port_send_buffer (PinosPort *port,
PinosBuffer *buffer,
GError **error);
G_END_DECLS

View file

@ -59,6 +59,7 @@ GDBusProxy * pinos_subscribe_get_proxy_finish (PinosSubscribe *subsc
typedef struct {
guint32 version;
guint32 flags;
guint32 length;
} PinosStackHeader;

View file

@ -54,10 +54,6 @@ struct _PinosStreamPrivate
gboolean disconnecting;
PinosStreamMode mode;
GSocket *socket;
GSource *socket_source;
PinosStackBuffer buffer;
};
#define PINOS_STREAM_GET_PRIVATE(obj) \
@ -74,7 +70,6 @@ enum
PROP_STATE,
PROP_POSSIBLE_FORMATS,
PROP_FORMAT,
PROP_SOCKET,
};
enum
@ -119,10 +114,6 @@ pinos_stream_get_property (GObject *_object,
g_value_set_boxed (value, priv->format);
break;
case PROP_SOCKET:
g_value_set_object (value, priv->socket);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (stream, prop_id, pspec);
break;
@ -153,6 +144,13 @@ pinos_stream_set_property (GObject *_object,
priv->properties = g_value_dup_boxed (value);
break;
case PROP_FORMAT:
if (priv->format)
g_bytes_unref (priv->format);
priv->format = g_value_dup_boxed (value);
g_object_set (priv->port, "format", priv->format, NULL);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (stream, prop_id, pspec);
break;
@ -210,6 +208,9 @@ subscription_cb (PinosSubscribe *subscribe,
}
break;
case PINOS_SUBSCRIPTION_FLAG_PORT:
break;
default:
break;
}
@ -237,9 +238,7 @@ pinos_stream_finalize (GObject * object)
g_debug ("free stream %p", stream);
g_clear_object (&priv->socket);
g_clear_object (&priv->node);
g_clear_object (&priv->port);
if (priv->possible_formats)
g_bytes_unref (priv->possible_formats);
@ -258,9 +257,6 @@ pinos_stream_finalize (GObject * object)
g_clear_object (&priv->context);
g_free (priv->name);
g_free (priv->buffer.free_data);
g_free (priv->buffer.free_fds);
G_OBJECT_CLASS (pinos_stream_parent_class)->finalize (object);
}
@ -358,25 +354,8 @@ pinos_stream_class_init (PinosStreamClass * klass)
"Format",
"The format of the stream",
G_TYPE_BYTES,
G_PARAM_READABLE |
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS));
/**
* PinosStream:socket
*
* The socket of the stream. When doing pinos_stream_start() with
* #PINOS_STREAM_MODE_SOCKET, the socket will contain a data stream with
* meta data and anciliary data containing fds with the data.
*/
g_object_class_install_property (gobject_class,
PROP_SOCKET,
g_param_spec_object ("socket",
"Socket",
"The stream socket",
G_TYPE_SOCKET,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS));
/**
* PinosStream:new-buffer
*
@ -494,6 +473,37 @@ pinos_stream_get_error (PinosStream *stream)
return stream->priv->error;
}
static void
on_received_buffer (PinosPort *port,
gpointer user_data)
{
PinosStream *stream = user_data;
g_debug ("buffer received");
g_signal_emit (stream, signals[SIGNAL_NEW_BUFFER], 0, NULL);
}
static void
on_port_notify (GObject *object,
GParamSpec *pspec,
gpointer user_data)
{
PinosPort *port = PINOS_PORT (object);
PinosStream *stream = user_data;
PinosStreamPrivate *priv = stream->priv;
if (pspec == NULL || strcmp (g_param_spec_get_name (pspec), "format")) {
g_clear_pointer (&priv->format, g_bytes_unref);
g_object_get (port, "format", &priv->format, NULL);
g_object_notify (G_OBJECT (stream), "format");
}
if (pspec == NULL || strcmp (g_param_spec_get_name (pspec), "possible-formats")) {
g_clear_pointer (&priv->possible_formats, g_bytes_unref);
g_object_get (port, "possible-formats", &priv->possible_formats, NULL);
g_object_notify (G_OBJECT (stream), "possible-formats");
}
}
static void
on_port_created (GObject *source_object,
GAsyncResult *res,
@ -511,6 +521,11 @@ on_port_created (GObject *source_object,
if (priv->port == NULL)
goto create_failed;
on_port_notify (G_OBJECT (priv->port), NULL, stream);
g_signal_connect (priv->port, "notify", (GCallback) on_port_notify, stream);
pinos_port_set_received_buffer_cb (priv->port, on_received_buffer, stream, NULL);
stream_set_state (stream, PINOS_STREAM_STATE_READY, NULL);
g_object_unref (stream);
@ -808,186 +823,19 @@ pinos_stream_disconnect (PinosStream *stream)
return TRUE;
}
static gboolean
on_socket_condition (GSocket *socket,
GIOCondition condition,
gpointer user_data)
{
PinosStream *stream = user_data;
PinosStreamPrivate *priv = stream->priv;
switch (condition) {
case G_IO_IN:
{
gssize len;
GInputVector ivec;
PinosStackHeader *hdr;
GSocketControlMessage **messages = NULL;
gint num_messages = 0;
gint flags = 0;
gsize need;
GError *error = NULL;
gint i;
need = sizeof (PinosStackHeader);
if (priv->buffer.max_size < need) {
priv->buffer.max_size = need;
priv->buffer.data = priv->buffer.free_data = g_realloc (priv->buffer.free_data, need);
}
hdr = priv->buffer.data;
/* read header first */
ivec.buffer = hdr;
ivec.size = sizeof (PinosStackHeader);
len = g_socket_receive_message (socket,
NULL,
&ivec,
1,
&messages,
&num_messages,
&flags,
NULL,
&error);
g_assert (len == sizeof (PinosStackHeader));
/* now we know the total length */
need += hdr->length;
if (priv->buffer.max_size < need) {
priv->buffer.max_size = need;
hdr = priv->buffer.data = priv->buffer.free_data = g_realloc (priv->buffer.free_data, need);
}
priv->buffer.size = need;
if (hdr->length > 0) {
/* read data */
len = g_socket_receive (socket,
(gchar *)priv->buffer.data + sizeof (PinosStackHeader),
hdr->length,
NULL,
&error);
g_assert (len == hdr->length);
}
if (priv->buffer.max_fds < num_messages) {
priv->buffer.max_fds = num_messages;
priv->buffer.fds = priv->buffer.free_fds = g_realloc (priv->buffer.free_fds,
num_messages * sizeof (int));
}
/* handle control messages */
for (i = 0; i < num_messages; i++) {
GSocketControlMessage *msg = messages[i];
gint *fds, n_fds, j;
if (g_socket_control_message_get_msg_type (msg) != SCM_RIGHTS)
continue;
fds = g_unix_fd_message_steal_fds (G_UNIX_FD_MESSAGE (msg), &n_fds);
for (j = 0; j < n_fds; j++)
priv->buffer.fds[i] = fds[i];
g_free (fds);
g_object_unref (msg);
}
g_free (messages);
priv->buffer.magic = PSB_MAGIC;
g_signal_emit (stream, signals[SIGNAL_NEW_BUFFER], 0, NULL);
priv->buffer.magic = 0;
priv->buffer.size = 0;
priv->buffer.n_fds = 0;
break;
}
case G_IO_OUT:
g_warning ("can do IO\n");
break;
default:
break;
}
return TRUE;
}
static void
handle_socket (PinosStream *stream, gint fd)
{
PinosStreamPrivate *priv = stream->priv;
GError *error = NULL;
priv->socket = g_socket_new_from_fd (fd, &error);
if (priv->socket == NULL)
goto socket_failed;
switch (priv->mode) {
case PINOS_STREAM_MODE_SOCKET:
g_object_notify (G_OBJECT (stream), "socket");
break;
case PINOS_STREAM_MODE_BUFFER:
{
priv->socket_source = g_socket_create_source (priv->socket, G_IO_IN, NULL);
g_source_set_callback (priv->socket_source, (GSourceFunc) on_socket_condition, stream, NULL);
g_source_attach (priv->socket_source, priv->context->priv->context);
break;
}
default:
break;
}
return;
/* ERRORS */
socket_failed:
{
g_warning ("failed to create socket: %s", error->message);
stream_set_state (stream, PINOS_STREAM_STATE_ERROR, error);
return;
}
}
static void
unhandle_socket (PinosStream *stream)
{
PinosStreamPrivate *priv = stream->priv;
switch (priv->mode) {
case PINOS_STREAM_MODE_SOCKET:
g_clear_object (&priv->socket);
g_object_notify (G_OBJECT (stream), "socket");
break;
case PINOS_STREAM_MODE_BUFFER:
if (priv->socket_source) {
g_source_destroy (priv->socket_source);
g_clear_pointer (&priv->socket_source, g_source_unref);
}
break;
default:
break;
}
}
/**
* pinos_stream_peek_buffer:
* pinos_stream_get_buffer:
* @stream: a #PinosStream
* @buffer: a #PinosBuffer
*
* Peek the next buffer from @stream. This function should be called from
* Get the next buffer from @stream. This function should be called from
* the new-buffer signal callback.
*
* Returns: %TRUE when @buffer contains valid information
*/
gboolean
pinos_stream_peek_buffer (PinosStream *stream,
PinosBuffer **buffer)
pinos_stream_get_buffer (PinosStream *stream,
PinosBuffer **buffer)
{
PinosStreamPrivate *priv;
@ -995,10 +843,9 @@ pinos_stream_peek_buffer (PinosStream *stream,
g_return_val_if_fail (buffer != NULL, FALSE);
priv = stream->priv;
g_return_val_if_fail (priv->state == PINOS_STREAM_STATE_STREAMING, FALSE);
g_return_val_if_fail (is_valid_buffer (&priv->buffer), FALSE);
//g_return_val_if_fail (priv->state == PINOS_STREAM_STATE_STREAMING, FALSE);
*buffer = (PinosBuffer *) &priv->buffer;
*buffer = pinos_port_get_buffer (priv->port);
return TRUE;
}
@ -1023,13 +870,7 @@ pinos_stream_send_buffer (PinosStream *stream,
PinosBuffer *buffer)
{
PinosStreamPrivate *priv;
gssize len;
PinosStackBuffer *sb = (PinosStackBuffer *) buffer;
GOutputVector ovec[1];
GSocketControlMessage *msg = NULL;
gint flags = 0;
GError *error = NULL;
gint i, n_msg;
g_return_val_if_fail (PINOS_IS_STREAM (stream), FALSE);
g_return_val_if_fail (buffer != NULL, FALSE);
@ -1037,47 +878,17 @@ pinos_stream_send_buffer (PinosStream *stream,
priv = stream->priv;
g_return_val_if_fail (priv->state == PINOS_STREAM_STATE_STREAMING, FALSE);
ovec[0].buffer = sb->data;
ovec[0].size = sb->size;
if (sb->n_fds) {
n_msg = 1;
msg = g_unix_fd_message_new ();
for (i = 0; i < sb->n_fds; i++)
if (!g_unix_fd_message_append_fd (G_UNIX_FD_MESSAGE (msg), sb->fds[i], &error))
goto append_failed;
}
else {
n_msg = 0;
}
len = g_socket_send_message (priv->socket,
NULL,
ovec,
1,
&msg,
n_msg,
flags,
NULL,
&error);
if (len == -1)
if (!pinos_port_send_buffer (priv->port, buffer, &error))
goto send_error;
g_assert (len == (gssize) sb->size);
return TRUE;
append_failed:
{
g_warning ("failed to append fd: %s", error->message);
g_object_unref (msg);
stream_set_state (stream, PINOS_STREAM_STATE_ERROR, error);
return FALSE;
}
/* ERRORS */
send_error:
{
g_warning ("failed to send_message: %s", error->message);
g_warning ("failed to send message: %s", error->message);
stream_set_state (stream, PINOS_STREAM_STATE_ERROR, error);
g_clear_error (&error);
return FALSE;
}
}

View file

@ -106,7 +106,7 @@ gboolean pinos_stream_start (PinosStream *stream,
PinosStreamMode mode);
gboolean pinos_stream_stop (PinosStream *stream);
gboolean pinos_stream_peek_buffer (PinosStream *stream,
gboolean pinos_stream_get_buffer (PinosStream *stream,
PinosBuffer **buffer);
gboolean pinos_stream_send_buffer (PinosStream *stream,
PinosBuffer *buffer);

View file

@ -786,10 +786,12 @@ pinos_subscribe_get_proxy (PinosSubscribe *subscribe,
if (data->pending) {
data->tasks = g_list_prepend (data->tasks, task);
} else if (data->proxy) {
g_task_return_pointer (task, g_object_ref (data->proxy), g_object_unref);
} else {
g_task_return_error (task, NULL);
} else {
if (data->proxy)
g_task_return_pointer (task, g_object_ref (data->proxy), g_object_unref);
else
g_task_return_error (task, NULL);
g_object_unref (task);
}
break;
}