reintroduce channels

Bring back the channel object. Making a node and port on the client side
was rather awkward because of the async nature of many methods. It feels
better to have a specific communication channel object to interface with
a server side port.
Use port activate/deactivate to start/stop streams
Remove links from the ports. We let other objects install a callback on
the port to receive and route buffers.
This commit is contained in:
Wim Taymans 2016-07-20 17:29:34 +02:00
parent eefe6aacb9
commit e167d30296
26 changed files with 2840 additions and 675 deletions

736
pinos/server/channel.c Normal file
View file

@ -0,0 +1,736 @@
/* Pinos
* Copyright (C) 2015 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <string.h>
#include <sys/socket.h>
#include <errno.h>
#include <gio/gunixfdlist.h>
#include "pinos/client/pinos.h"
#include "pinos/client/enumtypes.h"
#include "pinos/client/private.h"
#include "pinos/server/daemon.h"
#include "pinos/server/channel.h"
#include "pinos/server/utils.h"
#include "pinos/dbus/org-pinos.h"
#define MAX_BUFFER_SIZE 1024
#define MAX_FDS 16
struct _PinosChannelPrivate
{
PinosDaemon *daemon;
PinosChannel1 *iface;
gchar *object_path;
gchar *client_path;
PinosPort *port;
PinosDirection direction;
GBytes *possible_formats;
PinosProperties *properties;
PinosChannelState state;
GBytes *format;
gulong send_id;
int fd;
GSource *socket_source;
GSocket *sockets[2];
PinosBuffer recv_buffer;
guint8 recv_data[MAX_BUFFER_SIZE];
int recv_fds[MAX_FDS];
guint8 send_data[MAX_BUFFER_SIZE];
int send_fds[MAX_FDS];
GSocket *socket;
};
#define PINOS_CHANNEL_GET_PRIVATE(obj) \
(G_TYPE_INSTANCE_GET_PRIVATE ((obj), PINOS_TYPE_CHANNEL, PinosChannelPrivate))
G_DEFINE_TYPE (PinosChannel, pinos_channel, G_TYPE_OBJECT);
enum
{
PROP_0,
PROP_DAEMON,
PROP_PORT,
PROP_OBJECT_PATH,
PROP_CLIENT_PATH,
PROP_DIRECTION,
PROP_POSSIBLE_FORMATS,
PROP_PROPERTIES,
PROP_FORMAT,
PROP_SOCKET,
PROP_STATE,
};
enum
{
SIGNAL_REMOVE,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };
static void
pinos_channel_get_property (GObject *_object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
PinosChannel *channel = PINOS_CHANNEL (_object);
PinosChannelPrivate *priv = channel->priv;
switch (prop_id) {
case PROP_DAEMON:
g_value_set_object (value, priv->daemon);
break;
case PROP_PORT:
g_value_set_object (value, priv->port);
break;
case PROP_OBJECT_PATH:
g_value_set_string (value, priv->object_path);
break;
case PROP_CLIENT_PATH:
g_value_set_string (value, priv->client_path);
break;
case PROP_DIRECTION:
g_value_set_enum (value, priv->direction);
break;
case PROP_POSSIBLE_FORMATS:
g_value_set_boxed (value, priv->possible_formats);
break;
case PROP_PROPERTIES:
g_value_set_boxed (value, priv->properties);
break;
case PROP_FORMAT:
g_value_set_boxed (value, priv->format);
break;
case PROP_SOCKET:
g_value_set_object (value, priv->socket);
break;
case PROP_STATE:
g_value_set_uint (value, priv->state);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (channel, prop_id, pspec);
break;
}
}
static void
pinos_channel_set_property (GObject *_object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
PinosChannel *channel = PINOS_CHANNEL (_object);
PinosChannelPrivate *priv = channel->priv;
switch (prop_id) {
case PROP_DAEMON:
priv->daemon = g_value_dup_object (value);
break;
case PROP_PORT:
priv->port = g_value_dup_object (value);
break;
case PROP_OBJECT_PATH:
priv->object_path = g_value_dup_string (value);
break;
case PROP_CLIENT_PATH:
priv->client_path = g_value_dup_string (value);
g_object_set (priv->iface, "client", priv->client_path, NULL);
break;
case PROP_DIRECTION:
priv->direction = g_value_get_enum (value);
g_object_set (priv->iface, "direction", priv->direction, NULL);
break;
case PROP_POSSIBLE_FORMATS:
if (priv->possible_formats)
g_bytes_unref (priv->possible_formats);
priv->possible_formats = g_value_dup_boxed (value);
g_object_set (priv->iface, "possible-formats",
g_bytes_get_data (priv->possible_formats, NULL), NULL);
break;
case PROP_PROPERTIES:
if (priv->properties)
pinos_properties_free (priv->properties);
priv->properties = g_value_dup_boxed (value);
g_object_set (priv->iface, "properties", priv->properties ?
pinos_properties_to_variant (priv->properties) : NULL, NULL);
break;
case PROP_FORMAT:
if (priv->format)
g_bytes_unref (priv->format);
priv->format = g_value_dup_boxed (value);
g_object_set (priv->iface, "format", priv->format ?
g_bytes_get_data (priv->format, NULL) : NULL, NULL);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (channel, prop_id, pspec);
break;
}
}
static void
clear_formats (PinosChannel *channel)
{
PinosChannelPrivate *priv = channel->priv;
g_debug ("channel %p: clear format", channel);
g_clear_pointer (&priv->format, g_bytes_unref);
}
static void
stop_transfer (PinosChannel *channel)
{
PinosChannelPrivate *priv = channel->priv;
g_debug ("channel %p: stop transfer", channel);
pinos_port_deactivate (priv->port);
clear_formats (channel);
priv->state = PINOS_CHANNEL_STATE_STOPPED;
g_object_set (priv->iface,
"state", priv->state,
NULL);
}
static gboolean
handle_start (PinosChannel1 *interface,
GDBusMethodInvocation *invocation,
const gchar *arg_requested_format,
gpointer user_data)
{
PinosChannel *channel = user_data;
PinosChannelPrivate *priv = channel->priv;
GBytes *req_format, *format;
const gchar *format_str;
GError *error = NULL;
priv->state = PINOS_CHANNEL_STATE_STARTING;
req_format = g_bytes_new (arg_requested_format, strlen (arg_requested_format) + 1);
format = pinos_format_filter (priv->possible_formats, req_format, &error);
if (format == NULL)
goto no_format;
format_str = g_bytes_get_data (format, NULL);
g_debug ("channel %p: handle start, format %s", channel, format_str);
g_object_set (priv->port, "possible-formats", format, NULL);
pinos_port_activate (priv->port);
g_object_get (priv->port, "format", &format, NULL);
if (format == NULL)
goto no_format_activate;
format_str = g_bytes_get_data (format, NULL);
priv->state = PINOS_CHANNEL_STATE_STREAMING;
g_debug ("channel %p: we are now streaming in format \"%s\"", channel, format_str);
g_dbus_method_invocation_return_value (invocation,
g_variant_new ("(s@a{sv})",
format_str,
pinos_properties_to_variant (priv->properties)));
g_object_set (priv->iface,
"format", format_str,
"state", priv->state,
NULL);
return TRUE;
no_format:
{
g_debug ("no format found");
g_dbus_method_invocation_return_gerror (invocation, error);
g_clear_error (&error);
return TRUE;
}
no_format_activate:
{
g_debug ("no format found when activating");
g_dbus_method_invocation_return_dbus_error (invocation,
"org.pinos.Error", "can't negotiate formats");
return TRUE;
}
}
static gboolean
handle_stop (PinosChannel1 *interface,
GDBusMethodInvocation *invocation,
gpointer user_data)
{
PinosChannel *channel = user_data;
g_debug ("channel %p: handle stop", channel);
stop_transfer (channel);
g_dbus_method_invocation_return_value (invocation, NULL);
return TRUE;
}
static gboolean
handle_remove (PinosChannel1 *interface,
GDBusMethodInvocation *invocation,
gpointer user_data)
{
PinosChannel *channel = user_data;
g_debug ("channel %p: handle remove", channel);
stop_transfer (channel);
g_signal_emit (channel, signals[SIGNAL_REMOVE], 0, NULL);
g_dbus_method_invocation_return_value (invocation, NULL);
return TRUE;
}
static gboolean
on_send_buffer (PinosPort *port,
PinosBuffer *buffer,
GError **error,
gpointer user_data)
{
PinosChannel *channel = user_data;
PinosChannelPrivate *priv = channel->priv;
gboolean res;
if (priv->state == PINOS_CHANNEL_STATE_STREAMING)
res = pinos_io_write_buffer (priv->fd, buffer, error);
else
res = TRUE;
return res;
}
static gboolean
on_socket_condition (GSocket *socket,
GIOCondition condition,
gpointer user_data)
{
PinosChannel *channel = user_data;
PinosChannelPrivate *priv = channel->priv;
switch (condition) {
case G_IO_IN:
{
PinosBuffer *buffer = &priv->recv_buffer;
GError *error = NULL;
if (!pinos_io_read_buffer (priv->fd,
buffer,
priv->recv_data,
MAX_BUFFER_SIZE,
priv->recv_fds,
MAX_FDS,
&error)) {
g_warning ("channel %p: failed to read buffer: %s", channel, error->message);
g_clear_error (&error);
return TRUE;
}
if (!pinos_port_receive_buffer (priv->port, buffer, &error)) {
g_warning ("channel %p: port %p failed to receive buffer: %s", channel, priv->port, error->message);
g_clear_error (&error);
}
g_assert (pinos_buffer_unref (buffer) == FALSE);
break;
}
case G_IO_OUT:
g_warning ("can do IO OUT\n");
break;
default:
break;
}
return TRUE;
}
static void
handle_socket (PinosChannel *channel, GSocket *socket)
{
PinosChannelPrivate *priv = channel->priv;
GMainContext *context = g_main_context_get_thread_default();
g_debug ("channel %p: handle socket in context %p", channel, 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, channel, NULL);
g_source_attach (priv->socket_source, context);
}
static void
unhandle_socket (PinosChannel *channel)
{
PinosChannelPrivate *priv = channel->priv;
g_debug ("channel %p: unhandle socket", channel);
if (priv->socket_source) {
g_source_destroy (priv->socket_source);
g_clear_pointer (&priv->socket_source, g_source_unref);
priv->fd = -1;
}
}
/**
* pinos_channel_get_socket_pair:
* @channel: a #PinosChannel
* @error: a #GError
*
* Create or return a previously create socket pair for @channel. The
* Socket for the other end is returned.
*
* Returns: a #GSocket that can be used to send/receive buffers to channel.
*/
GSocket *
pinos_channel_get_socket_pair (PinosChannel *channel,
GError **error)
{
PinosChannelPrivate *priv;
g_return_val_if_fail (PINOS_IS_CHANNEL (channel), FALSE);
priv = channel->priv;
if (priv->sockets[1] == NULL) {
int fd[2];
if (socketpair (AF_UNIX, SOCK_STREAM, 0, fd) != 0)
goto no_sockets;
priv->sockets[0] = g_socket_new_from_fd (fd[0], error);
if (priv->sockets[0] == NULL)
goto create_failed;
priv->sockets[1] = g_socket_new_from_fd (fd[1], error);
if (priv->sockets[1] == NULL)
goto create_failed;
handle_socket (channel, priv->sockets[0]);
}
return g_object_ref (priv->sockets[1]);
/* ERRORS */
no_sockets:
{
g_set_error (error,
G_IO_ERROR,
g_io_error_from_errno (errno),
"could not create socketpair: %s", strerror (errno));
return NULL;
}
create_failed:
{
g_clear_object (&priv->sockets[0]);
g_clear_object (&priv->sockets[1]);
return NULL;
}
}
static void
channel_register_object (PinosChannel *channel)
{
PinosChannelPrivate *priv = channel->priv;
PinosObjectSkeleton *skel;
gchar *name;
priv->send_id = pinos_port_add_send_buffer_cb (priv->port, on_send_buffer, channel, NULL);
name = g_strdup_printf ("%s/channel", priv->client_path);
skel = pinos_object_skeleton_new (name);
g_free (name);
pinos_object_skeleton_set_channel1 (skel, priv->iface);
g_free (priv->object_path);
priv->object_path = pinos_daemon_export_uniquely (priv->daemon, G_DBUS_OBJECT_SKELETON (skel));
g_object_unref (skel);
g_debug ("channel %p: register object %s", channel, priv->object_path);
}
static void
channel_unregister_object (PinosChannel *channel)
{
PinosChannelPrivate *priv = channel->priv;
pinos_port_remove_send_buffer_cb (priv->port, priv->send_id);
g_debug ("channel %p: unregister object", channel);
pinos_daemon_unexport (priv->daemon, priv->object_path);
}
static void
pinos_channel_dispose (GObject * object)
{
PinosChannel *channel = PINOS_CHANNEL (object);
PinosChannelPrivate *priv = channel->priv;
g_debug ("channel %p: dispose", channel);
pinos_port_deactivate (priv->port);
clear_formats (channel);
unhandle_socket (channel);
g_clear_object (&priv->socket);
channel_unregister_object (channel);
G_OBJECT_CLASS (pinos_channel_parent_class)->dispose (object);
}
static void
pinos_channel_finalize (GObject * object)
{
PinosChannel *channel = PINOS_CHANNEL (object);
PinosChannelPrivate *priv = channel->priv;
g_debug ("channel %p: finalize", channel);
if (priv->possible_formats)
g_bytes_unref (priv->possible_formats);
if (priv->properties)
pinos_properties_free (priv->properties);
g_clear_object (&priv->port);
g_clear_object (&priv->daemon);
g_clear_object (&priv->iface);
g_free (priv->client_path);
g_free (priv->object_path);
G_OBJECT_CLASS (pinos_channel_parent_class)->finalize (object);
}
static void
pinos_channel_constructed (GObject * object)
{
PinosChannel *channel = PINOS_CHANNEL (object);
g_debug ("channel %p: constructed", channel);
channel_register_object (channel);
G_OBJECT_CLASS (pinos_channel_parent_class)->constructed (object);
}
static void
pinos_channel_class_init (PinosChannelClass * klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
g_type_class_add_private (klass, sizeof (PinosChannelPrivate));
gobject_class->constructed = pinos_channel_constructed;
gobject_class->dispose = pinos_channel_dispose;
gobject_class->finalize = pinos_channel_finalize;
gobject_class->set_property = pinos_channel_set_property;
gobject_class->get_property = pinos_channel_get_property;
g_object_class_install_property (gobject_class,
PROP_DAEMON,
g_param_spec_object ("daemon",
"Daemon",
"The Daemon",
PINOS_TYPE_DAEMON,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_PORT,
g_param_spec_object ("port",
"Port",
"The Port",
PINOS_TYPE_PORT,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_OBJECT_PATH,
g_param_spec_string ("object-path",
"Object Path",
"The object path",
NULL,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_CLIENT_PATH,
g_param_spec_string ("client-path",
"Client Path",
"The client object path",
NULL,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_DIRECTION,
g_param_spec_enum ("direction",
"Direction",
"The direction of the port",
PINOS_TYPE_DIRECTION,
PINOS_DIRECTION_INVALID,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_POSSIBLE_FORMATS,
g_param_spec_boxed ("possible-formats",
"Possible Formats",
"The possbile formats of the stream",
G_TYPE_BYTES,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_PROPERTIES,
g_param_spec_boxed ("properties",
"Properties",
"Extra properties of the stream",
PINOS_TYPE_PROPERTIES,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_FORMAT,
g_param_spec_boxed ("format",
"Format",
"The format of the stream",
G_TYPE_BYTES,
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_SOCKET,
g_param_spec_object ("socket",
"Socket",
"The socket with data",
G_TYPE_SOCKET,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS));
signals[SIGNAL_REMOVE] = g_signal_new ("remove",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
0,
NULL,
NULL,
g_cclosure_marshal_generic,
G_TYPE_NONE,
0,
G_TYPE_NONE);
}
static void
pinos_channel_init (PinosChannel * channel)
{
PinosChannelPrivate *priv = channel->priv = PINOS_CHANNEL_GET_PRIVATE (channel);
priv->iface = pinos_channel1_skeleton_new ();
g_signal_connect (priv->iface, "handle-start", (GCallback) handle_start, channel);
g_signal_connect (priv->iface, "handle-stop", (GCallback) handle_stop, channel);
g_signal_connect (priv->iface, "handle-remove", (GCallback) handle_remove, channel);
priv->state = PINOS_CHANNEL_STATE_STOPPED;
g_object_set (priv->iface, "state", priv->state, NULL);
priv->direction = PINOS_DIRECTION_INVALID;
g_debug ("channel %p: new", channel);
}
/**
* pinos_channel_remove:
* @channel: a #PinosChannel
*
* Remove @channel. This will stop the transfer on the channel and
* free the resources allocated by @channel.
*/
void
pinos_channel_remove (PinosChannel *channel)
{
g_debug ("channel %p: remove", channel);
stop_transfer (channel);
g_signal_emit (channel, signals[SIGNAL_REMOVE], 0, NULL);
}
const gchar *
pinos_channel_get_client_path (PinosChannel *channel)
{
PinosChannelPrivate *priv;
g_return_val_if_fail (PINOS_IS_CHANNEL (channel), NULL);
priv = channel->priv;
return priv->client_path;
}
/**
* pinos_channel_get_object_path:
* @channel: a #PinosChannel
*
* Get the object patch of @channel
*
* Returns: the object path of @source.
*/
const gchar *
pinos_channel_get_object_path (PinosChannel *channel)
{
PinosChannelPrivate *priv;
g_return_val_if_fail (PINOS_IS_CHANNEL (channel), NULL);
priv = channel->priv;
return priv->object_path;
}

73
pinos/server/channel.h Normal file
View file

@ -0,0 +1,73 @@
/* Pinos
* Copyright (C) 2015 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __PINOS_CHANNEL_H__
#define __PINOS_CHANNEL_H__
#include <glib-object.h>
G_BEGIN_DECLS
#define PINOS_TYPE_CHANNEL (pinos_channel_get_type ())
#define PINOS_IS_CHANNEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PINOS_TYPE_CHANNEL))
#define PINOS_IS_CHANNEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PINOS_TYPE_CHANNEL))
#define PINOS_CHANNEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), PINOS_TYPE_CHANNEL, PinosChannelClass))
#define PINOS_CHANNEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), PINOS_TYPE_CHANNEL, PinosChannel))
#define PINOS_CHANNEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PINOS_TYPE_CHANNEL, PinosChannelClass))
#define PINOS_CHANNEL_CAST(obj) ((PinosChannel*)(obj))
#define PINOS_CHANNEL_CLASS_CAST(klass) ((PinosChannelClass*)(klass))
typedef struct _PinosChannel PinosChannel;
typedef struct _PinosChannelClass PinosChannelClass;
typedef struct _PinosChannelPrivate PinosChannelPrivate;
/**
* PinosChannel:
*
* Pinos source channel object class.
*/
struct _PinosChannel {
GObject object;
PinosChannelPrivate *priv;
};
/**
* PinosChannelClass:
*
* Pinos source channel object class.
*/
struct _PinosChannelClass {
GObjectClass parent_class;
};
/* normal GObject stuff */
GType pinos_channel_get_type (void);
void pinos_channel_remove (PinosChannel *channel);
const gchar * pinos_channel_get_client_path (PinosChannel *channel);
const gchar * pinos_channel_get_object_path (PinosChannel *channel);
GSocket * pinos_channel_get_socket_pair (PinosChannel *channel,
GError **error);
G_END_DECLS
#endif /* __PINOS_CHANNEL_H__ */

447
pinos/server/client.c Normal file
View file

@ -0,0 +1,447 @@
/* Pinos
* Copyright (C) 2015 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <string.h>
#include "pinos/client/pinos.h"
#include "pinos/server/client.h"
#include "pinos/dbus/org-pinos.h"
struct _PinosClientPrivate
{
PinosDaemon *daemon;
PinosClient1 *iface;
guint id;
gchar *sender;
gchar *object_path;
PinosProperties *properties;
PinosFdManager *fdmanager;
GList *objects;
};
#define PINOS_CLIENT_GET_PRIVATE(obj) \
(G_TYPE_INSTANCE_GET_PRIVATE ((obj), PINOS_TYPE_CLIENT, PinosClientPrivate))
G_DEFINE_TYPE (PinosClient, pinos_client, G_TYPE_OBJECT);
enum
{
PROP_0,
PROP_DAEMON,
PROP_SENDER,
PROP_OBJECT_PATH,
PROP_PROPERTIES,
};
enum
{
SIGNAL_APPEARED,
SIGNAL_VANISHED,
SIGNAL_REMOVE,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };
static void
client_name_appeared_handler (GDBusConnection *connection,
const gchar *name,
const gchar *name_owner,
gpointer user_data)
{
PinosClient *client = user_data;
g_debug ("client %p: appeared %s %s", client, name, name_owner);
g_signal_emit (client, signals[SIGNAL_APPEARED], 0, NULL);
}
static void
client_name_vanished_handler (GDBusConnection *connection,
const gchar *name,
gpointer user_data)
{
PinosClient *client = user_data;
PinosClientPrivate *priv = client->priv;
g_debug ("client %p: vanished %s", client, name);
g_signal_emit (client, signals[SIGNAL_VANISHED], 0, NULL);
g_bus_unwatch_name (priv->id);
}
static void
client_watch_name (PinosClient *client)
{
PinosClientPrivate *priv = client->priv;
GDBusConnection *connection;
g_object_get (priv->daemon, "connection", &connection, NULL);
priv->id = g_bus_watch_name_on_connection (connection,
priv->sender,
G_BUS_NAME_WATCHER_FLAGS_NONE,
client_name_appeared_handler,
client_name_vanished_handler,
client,
(GDestroyNotify) g_object_unref);
}
static void
pinos_client_get_property (GObject *_object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
PinosClient *client = PINOS_CLIENT (_object);
PinosClientPrivate *priv = client->priv;
switch (prop_id) {
case PROP_DAEMON:
g_value_set_object (value, priv->daemon);
break;
case PROP_SENDER:
g_value_set_string (value, priv->sender);
break;
case PROP_OBJECT_PATH:
g_value_set_string (value, priv->object_path);
break;
case PROP_PROPERTIES:
g_value_set_boxed (value, priv->properties);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (client, prop_id, pspec);
break;
}
}
static void
pinos_client_set_property (GObject *_object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
PinosClient *client = PINOS_CLIENT (_object);
PinosClientPrivate *priv = client->priv;
switch (prop_id) {
case PROP_DAEMON:
priv->daemon = g_value_dup_object (value);
break;
case PROP_SENDER:
priv->sender = g_value_dup_string (value);
pinos_client1_set_sender (priv->iface, priv->sender);
break;
case PROP_PROPERTIES:
if (priv->properties)
pinos_properties_free (priv->properties);
priv->properties = g_value_dup_boxed (value);
pinos_client1_set_properties (priv->iface, priv->properties ?
pinos_properties_to_variant (priv->properties) : NULL);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (client, prop_id, pspec);
break;
}
}
static void
client_register_object (PinosClient *client)
{
PinosClientPrivate *priv = client->priv;
PinosDaemon *daemon = priv->daemon;
PinosObjectSkeleton *skel;
skel = pinos_object_skeleton_new (PINOS_DBUS_OBJECT_CLIENT);
pinos_object_skeleton_set_client1 (skel, priv->iface);
g_free (priv->object_path);
priv->object_path = pinos_daemon_export_uniquely (daemon, G_DBUS_OBJECT_SKELETON (skel));
g_object_unref (skel);
g_debug ("client %p: register %s", client, priv->object_path);
}
static void
client_unregister_object (PinosClient *client)
{
PinosClientPrivate *priv = client->priv;
PinosDaemon *daemon = priv->daemon;
g_debug ("client %p: unregister", client);
pinos_daemon_unexport (daemon, priv->object_path);
}
static void
pinos_client_dispose (GObject * object)
{
PinosClient *client = PINOS_CLIENT (object);
PinosClientPrivate *priv = client->priv;
GList *copy;
g_debug ("client %p: dispose", client);
pinos_fd_manager_remove_all (priv->fdmanager, priv->object_path);
copy = g_list_copy (priv->objects);
g_list_free_full (copy, g_object_unref);
client_unregister_object (client);
G_OBJECT_CLASS (pinos_client_parent_class)->dispose (object);
}
static void
pinos_client_finalize (GObject * object)
{
PinosClient *client = PINOS_CLIENT (object);
PinosClientPrivate *priv = client->priv;
g_debug ("client %p: finalize", client);
g_clear_object (&priv->daemon);
g_clear_object (&priv->iface);
g_free (priv->sender);
g_free (priv->object_path);
if (priv->properties)
pinos_properties_free (priv->properties);
g_clear_object (&priv->fdmanager);
G_OBJECT_CLASS (pinos_client_parent_class)->finalize (object);
}
static void
pinos_client_constructed (GObject * object)
{
PinosClient *client = PINOS_CLIENT (object);
g_debug ("client %p: constructed", client);
client_watch_name (client);
client_register_object (client);
G_OBJECT_CLASS (pinos_client_parent_class)->constructed (object);
}
static void
pinos_client_class_init (PinosClientClass * klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
g_type_class_add_private (klass, sizeof (PinosClientPrivate));
gobject_class->constructed = pinos_client_constructed;
gobject_class->dispose = pinos_client_dispose;
gobject_class->finalize = pinos_client_finalize;
gobject_class->set_property = pinos_client_set_property;
gobject_class->get_property = pinos_client_get_property;
g_object_class_install_property (gobject_class,
PROP_DAEMON,
g_param_spec_object ("daemon",
"Daemon",
"The daemon",
PINOS_TYPE_DAEMON,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_SENDER,
g_param_spec_string ("sender",
"Sender",
"The sender",
NULL,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_OBJECT_PATH,
g_param_spec_string ("object-path",
"Object Path",
"The object path",
NULL,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_PROPERTIES,
g_param_spec_boxed ("properties",
"Properties",
"Client properties",
PINOS_TYPE_PROPERTIES,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT |
G_PARAM_STATIC_STRINGS));
signals[SIGNAL_APPEARED] = g_signal_new ("appeared",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
0,
NULL,
NULL,
g_cclosure_marshal_generic,
G_TYPE_NONE,
0,
G_TYPE_NONE);
signals[SIGNAL_VANISHED] = g_signal_new ("vanished",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
0,
NULL,
NULL,
g_cclosure_marshal_generic,
G_TYPE_NONE,
0,
G_TYPE_NONE);
signals[SIGNAL_REMOVE] = g_signal_new ("remove",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
0,
NULL,
NULL,
g_cclosure_marshal_generic,
G_TYPE_NONE,
0,
G_TYPE_NONE);
}
static void
pinos_client_init (PinosClient * client)
{
PinosClientPrivate *priv = client->priv = PINOS_CLIENT_GET_PRIVATE (client);
priv->iface = pinos_client1_skeleton_new ();
g_debug ("client %p: new", client);
priv->fdmanager = pinos_fd_manager_get (PINOS_FD_MANAGER_DEFAULT);
}
/**
* pinos_client_new:
* @daemon: a #PinosDaemon
* @sender: the sender id
* @prefix: a prefix
* @properties: extra client properties
*
* Make a new #PinosClient object and register it to @daemon under the @prefix.
*
* Returns: a new #PinosClient
*/
PinosClient *
pinos_client_new (PinosDaemon *daemon,
const gchar *sender,
PinosProperties *properties)
{
g_return_val_if_fail (PINOS_IS_DAEMON (daemon), NULL);
return g_object_new (PINOS_TYPE_CLIENT, "daemon", daemon,
"sender", sender,
"properties", properties,
NULL);
}
/**
* pinos_client_remove:
* @client: a #PinosClient
*
* Trigger removal of @client
*/
void
pinos_client_remove (PinosClient *client)
{
g_return_if_fail (PINOS_IS_CLIENT (client));
g_debug ("client %p: remove", client);
g_signal_emit (client, signals[SIGNAL_REMOVE], 0, NULL);
}
void
pinos_client_add_object (PinosClient *client,
GObject *object)
{
PinosClientPrivate *priv;
g_return_if_fail (PINOS_IS_CLIENT (client));
g_return_if_fail (G_IS_OBJECT (object));
priv = client->priv;
priv->objects = g_list_prepend (priv->objects, object);
}
void
pinos_client_remove_object (PinosClient *client,
GObject *object)
{
PinosClientPrivate *priv;
g_return_if_fail (PINOS_IS_CLIENT (client));
g_return_if_fail (G_IS_OBJECT (object));
priv = client->priv;
priv->objects = g_list_remove (priv->objects, object);
g_object_unref (object);
}
/**
* pinos_client_get_sender:
* @client: a #PinosClient
*
* Get the sender of @client.
*
* Returns: the sender of @client
*/
const gchar *
pinos_client_get_sender (PinosClient *client)
{
PinosClientPrivate *priv;
g_return_val_if_fail (PINOS_IS_CLIENT (client), NULL);
priv = client->priv;
return priv->sender;
}
/**
* pinos_client_get_object_path:
* @client: a #PinosClient
*
* Get the object path of @client.
*
* Returns: the object path of @client
*/
const gchar *
pinos_client_get_object_path (PinosClient *client)
{
PinosClientPrivate *priv;
g_return_val_if_fail (PINOS_IS_CLIENT (client), NULL);
priv = client->priv;
return priv->object_path;
}

81
pinos/server/client.h Normal file
View file

@ -0,0 +1,81 @@
/* Pinos
* Copyright (C) 2015 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __PINOS_CLIENT_H__
#define __PINOS_CLIENT_H__
#include <glib-object.h>
#include <pinos/server/daemon.h>
G_BEGIN_DECLS
#define PINOS_TYPE_CLIENT (pinos_client_get_type ())
#define PINOS_IS_CLIENT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PINOS_TYPE_CLIENT))
#define PINOS_IS_CLIENT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PINOS_TYPE_CLIENT))
#define PINOS_CLIENT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), PINOS_TYPE_CLIENT, PinosClientClass))
#define PINOS_CLIENT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), PINOS_TYPE_CLIENT, PinosClient))
#define PINOS_CLIENT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PINOS_TYPE_CLIENT, PinosClientClass))
#define PINOS_CLIENT_CAST(obj) ((PinosClient*)(obj))
#define PINOS_CLIENT_CLASS_CAST(klass) ((PinosClientClass*)(klass))
typedef struct _PinosClient PinosClient;
typedef struct _PinosClientClass PinosClientClass;
typedef struct _PinosClientPrivate PinosClientPrivate;
/**
* PinosClient:
*
* Pinos client object class.
*/
struct _PinosClient {
GObject object;
PinosClientPrivate *priv;
};
/**
* PinosClientClass:
*
* Pinos client object class.
*/
struct _PinosClientClass {
GObjectClass parent_class;
};
/* normal GObject stuff */
GType pinos_client_get_type (void);
PinosClient * pinos_client_new (PinosDaemon *daemon,
const gchar *sender,
PinosProperties *properties);
void pinos_client_remove (PinosClient *client);
const gchar * pinos_client_get_sender (PinosClient *client);
const gchar * pinos_client_get_object_path (PinosClient *client);
void pinos_client_add_object (PinosClient *client,
GObject *object);
void pinos_client_remove_object (PinosClient *client,
GObject *object);
G_END_DECLS
#endif /* __PINOS_CLIENT_H__ */

View file

@ -20,6 +20,7 @@
#include <string.h>
#include <gio/gio.h>
#include <gio/gunixfdlist.h>
#include "config.h"
@ -27,6 +28,8 @@
#include "pinos/server/daemon.h"
#include "pinos/server/node.h"
#include "pinos/server/client.h"
#include "pinos/server/channel.h"
#include "pinos/dbus/org-pinos.h"
@ -42,7 +45,7 @@ struct _PinosDaemonPrivate
GList *nodes;
GHashTable *senders;
GHashTable *clients;
PinosProperties *properties;
@ -52,95 +55,174 @@ struct _PinosDaemonPrivate
enum
{
PROP_0,
PROP_CONNECTION,
PROP_PROPERTIES,
};
typedef struct {
guint id;
gchar *sender;
PinosDaemon *daemon;
GList *objects;
} SenderData;
static void
sender_name_appeared_handler (GDBusConnection *connection,
const gchar *name,
const gchar *name_owner,
gpointer user_data)
handle_client_appeared (PinosClient *client, gpointer user_data)
{
SenderData *data = user_data;
PinosDaemonPrivate *priv = data->daemon->priv;
PinosDaemon *daemon = user_data;
PinosDaemonPrivate *priv = daemon->priv;
g_debug ("daemon %p: appeared %s %s", data->daemon, name, name_owner);
g_debug ("daemon %p: appeared %p", daemon, client);
g_hash_table_insert (priv->senders, data->sender, data);
g_hash_table_insert (priv->clients, (gpointer) pinos_client_get_sender (client), client);
}
static void
sender_name_vanished_handler (GDBusConnection *connection,
const gchar *name,
gpointer user_data)
handle_client_vanished (PinosClient *client, gpointer user_data)
{
SenderData *data = user_data;
PinosDaemon *daemon = user_data;
PinosDaemonPrivate *priv = daemon->priv;
g_debug ("daemon %p: vanished %s", data->daemon, name);
g_bus_unwatch_name (data->id);
g_debug ("daemon %p: vanished %p", daemon, client);
g_hash_table_remove (priv->clients, (gpointer) pinos_client_get_sender (client));
}
static void
data_free (SenderData *data)
{
g_debug ("daemon %p: free sender data %p for %s", data->daemon, data, data->sender);
g_list_free_full (data->objects, g_object_unref);
g_hash_table_remove (data->daemon->priv->senders, data->sender);
g_free (data->sender);
g_free (data);
}
static SenderData *
sender_data_new (PinosDaemon *daemon,
const gchar *sender)
static PinosClient *
sender_get_client (PinosDaemon *daemon,
const gchar *sender)
{
PinosDaemonPrivate *priv = daemon->priv;
SenderData *data;
PinosClient *client;
data = g_new0 (SenderData, 1);
data->daemon = daemon;
data->sender = g_strdup (sender);
client = g_hash_table_lookup (priv->clients, sender);
if (client == NULL) {
client = pinos_client_new (daemon, sender, NULL);
g_debug ("daemon %p: new sender data %p for %s", daemon, data, sender);
g_debug ("daemon %p: new client %p for %s", daemon, client, sender);
g_signal_connect (client,
"appeared",
(GCallback) handle_client_appeared,
daemon);
g_signal_connect (client,
"vanished",
(GCallback) handle_client_vanished,
daemon);
}
return client;
}
data->id = g_bus_watch_name_on_connection (priv->connection,
sender,
G_BUS_NAME_WATCHER_FLAGS_NONE,
sender_name_appeared_handler,
sender_name_vanished_handler,
data,
(GDestroyNotify) data_free);
return data;
static void
handle_remove_channel (PinosChannel *channel,
gpointer user_data)
{
PinosClient *client = user_data;
g_debug ("client %p: remove channel %p", client, channel);
pinos_client_remove_object (client, G_OBJECT (channel));
}
static gboolean
handle_create_channel (PinosDaemon1 *interface,
GDBusMethodInvocation *invocation,
const gchar *arg_node,
PinosDirection direction,
const gchar *arg_possible_formats,
GVariant *arg_properties,
gpointer user_data)
{
PinosDaemon *daemon = user_data;
const gchar *sender, *object_path;
PinosProperties *props;
PinosClient *client;
PinosPort *port;
PinosChannel *channel;
GBytes *formats;
GError *error = NULL;
GUnixFDList *fdlist;
GSocket *socket;
gint fdidx;
sender = g_dbus_method_invocation_get_sender (invocation);
g_debug ("daemon %p: %s create channel", daemon, sender);
formats = g_bytes_new (arg_possible_formats, strlen (arg_possible_formats) + 1);
props = pinos_properties_from_variant (arg_properties);
port = pinos_daemon_find_port (daemon,
direction,
arg_node,
props,
formats,
&error);
if (port == NULL)
goto no_port;
g_debug ("daemon %p: matched port %p", daemon, port);
client = sender_get_client (daemon, sender);
channel = pinos_port_create_channel (port,
pinos_client_get_object_path (client),
formats,
props,
&error);
pinos_properties_free (props);
g_bytes_unref (formats);
if (channel == NULL)
goto no_channel;
pinos_client_add_object (client, G_OBJECT (channel));
g_signal_connect (channel,
"remove",
(GCallback) handle_remove_channel,
client);
socket = pinos_channel_get_socket_pair (channel, &error);
if (socket == NULL)
goto no_socket;
object_path = pinos_channel_get_object_path (channel);
g_debug ("daemon %p: add channel %p, %s", daemon, channel, object_path);
fdlist = g_unix_fd_list_new ();
fdidx = g_unix_fd_list_append (fdlist, g_socket_get_fd (socket), NULL);
g_dbus_method_invocation_return_value_with_unix_fd_list (invocation,
g_variant_new ("(oh)", object_path, fdidx), fdlist);
g_object_unref (fdlist);
return TRUE;
/* ERRORS */
no_port:
{
g_debug ("daemon %p: could not find port %s, %s", daemon, arg_node, error->message);
pinos_properties_free (props);
g_bytes_unref (formats);
goto exit_error;
}
no_channel:
{
g_debug ("daemon %p: could not create channel %s", daemon, error->message);
goto exit_error;
}
no_socket:
{
g_debug ("daemon %p: could not create channel %s", daemon, error->message);
goto exit_error;
}
exit_error:
{
g_dbus_method_invocation_return_gerror (invocation, error);
g_clear_error (&error);
return TRUE;
}
}
static void
handle_remove_node (PinosNode *node,
gpointer user_data)
{
PinosDaemon *daemon = user_data;
PinosDaemonPrivate *priv = daemon->priv;
const gchar *sender;
SenderData *data;
PinosClient *client = user_data;
sender = pinos_node_get_sender (node);
g_debug ("daemon %p: sender %s removed node %p", daemon, sender, node);
data = g_hash_table_lookup (priv->senders, sender);
if (data == NULL)
return;
g_debug ("daemon %p: node %p unref", daemon, node);
data->objects = g_list_remove (data->objects, node);
g_object_unref (node);
g_debug ("client %p: node %p remove", daemon, node);
pinos_client_remove_object (client, G_OBJECT (node));
}
static gboolean
@ -155,7 +237,7 @@ handle_create_node (PinosDaemon1 *interface,
PinosDaemonPrivate *priv = daemon->priv;
PinosNodeFactory *factory;
PinosNode *node;
SenderData *data;
PinosClient *client;
const gchar *sender, *object_path;
PinosProperties *props;
@ -184,16 +266,13 @@ handle_create_node (PinosDaemon1 *interface,
if (node == NULL)
goto no_node;
data = g_hash_table_lookup (priv->senders, sender);
if (data == NULL)
data = sender_data_new (daemon, sender);
data->objects = g_list_prepend (data->objects, node);
client = sender_get_client (daemon, sender);
pinos_client_add_object (client, G_OBJECT (node));
g_signal_connect (node,
"remove",
(GCallback) handle_remove_node,
daemon);
client);
object_path = pinos_node_get_object_path (node);
g_debug ("daemon %p: added node %p with path %s", daemon, node, object_path);
@ -212,6 +291,18 @@ no_node:
}
}
static gboolean
handle_link_nodes (PinosDaemon1 *interface,
GDBusMethodInvocation *invocation,
const gchar *arg_node,
const gchar *arg_port,
const gchar *arg_possible_formats,
GVariant *arg_properties,
gpointer user_data)
{
return TRUE;
}
static void
export_server_object (PinosDaemon *daemon,
GDBusObjectManagerServer *manager)
@ -520,6 +611,10 @@ pinos_daemon_get_property (GObject *_object,
g_value_set_boxed (value, priv->properties);
break;
case PROP_CONNECTION:
g_value_set_object (value, priv->connection);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (daemon, prop_id, pspec);
break;
@ -586,7 +681,7 @@ pinos_daemon_finalize (GObject * object)
g_debug ("daemon %p: finalize", object);
g_clear_object (&priv->server_manager);
g_clear_object (&priv->iface);
g_hash_table_unref (priv->senders);
g_hash_table_unref (priv->clients);
g_hash_table_unref (priv->node_factories);
G_OBJECT_CLASS (pinos_daemon_parent_class)->finalize (object);
@ -606,6 +701,15 @@ pinos_daemon_class_init (PinosDaemonClass * klass)
gobject_class->set_property = pinos_daemon_set_property;
gobject_class->get_property = pinos_daemon_get_property;
g_object_class_install_property (gobject_class,
PROP_CONNECTION,
g_param_spec_object ("connection",
"Connection",
"The DBus connection",
G_TYPE_DBUS_CONNECTION,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_PROPERTIES,
g_param_spec_boxed ("properties",
@ -626,9 +730,11 @@ pinos_daemon_init (PinosDaemon * daemon)
g_debug ("daemon %p: new", daemon);
priv->iface = pinos_daemon1_skeleton_new ();
g_signal_connect (priv->iface, "handle-create-node", (GCallback) handle_create_node, daemon);
g_signal_connect (priv->iface, "handle-create-channel", (GCallback) handle_create_channel, daemon);
g_signal_connect (priv->iface, "handle-link-nodes", (GCallback) handle_link_nodes, daemon);
priv->server_manager = g_dbus_object_manager_server_new (PINOS_DBUS_OBJECT_PREFIX);
priv->senders = g_hash_table_new (g_str_hash, g_str_equal);
priv->clients = g_hash_table_new (g_str_hash, g_str_equal);
priv->node_factories = g_hash_table_new_full (g_str_hash,
g_str_equal,
g_free,

View file

@ -18,9 +18,7 @@
*/
#include <string.h>
#include <sys/socket.h>
#include <gst/gst.h>
#include <gio/gio.h>
#include "pinos/client/pinos.h"
@ -28,6 +26,7 @@
#include "pinos/server/port.h"
#include "pinos/server/node.h"
#include "pinos/server/utils.h"
#define PINOS_PORT_GET_PRIVATE(obj) \
(G_TYPE_INSTANCE_GET_PRIVATE ((obj), PINOS_TYPE_PORT, PinosPortPrivate))
@ -38,30 +37,34 @@
#define PINOS_DEBUG_TRANSPORT(format,args...)
#endif
#define MAX_BUFFER_SIZE 1024
#define MAX_FDS 16
typedef struct {
gulong id;
PinosBufferCallback send_buffer_cb;
gpointer send_buffer_data;
GDestroyNotify send_buffer_notify;
} SendData;
struct _PinosPortPrivate
{
PinosDaemon *daemon;
gulong id;
PinosNode *node;
PinosDirection direction;
GBytes *possible_formats;
GBytes *format;
PinosProperties *properties;
guint8 send_data[MAX_BUFFER_SIZE];
int send_fds[MAX_FDS];
PinosBuffer *buffer;
GPtrArray *peers;
gchar **peer_paths;
guint max_peers;
PinosReceivedBufferCallback received_buffer_cb;
PinosBufferCallback received_buffer_cb;
gpointer received_buffer_data;
GDestroyNotify received_buffer_notify;
gint active_count;
GList *send_datas;
};
G_DEFINE_TYPE (PinosPort, pinos_port, G_TYPE_OBJECT);
@ -71,10 +74,7 @@ enum
PROP_0,
PROP_DAEMON,
PROP_NODE,
PROP_MAIN_CONTEXT,
PROP_DIRECTION,
PROP_MAX_PEERS,
PROP_PEERS,
PROP_POSSIBLE_FORMATS,
PROP_FORMAT,
PROP_PROPERTIES,
@ -84,8 +84,8 @@ enum
{
SIGNAL_FORMAT_REQUEST,
SIGNAL_REMOVE,
SIGNAL_LINKED,
SIGNAL_UNLINKED,
SIGNAL_ACTIVATE,
SIGNAL_DEACTIVATE,
LAST_SIGNAL
};
@ -94,7 +94,7 @@ static guint signals[LAST_SIGNAL] = { 0 };
void
pinos_port_set_received_buffer_cb (PinosPort *port,
PinosReceivedBufferCallback cb,
PinosBufferCallback cb,
gpointer user_data,
GDestroyNotify notify)
{
@ -103,7 +103,7 @@ pinos_port_set_received_buffer_cb (PinosPort *port,
g_return_if_fail (PINOS_IS_PORT (port));
priv = port->priv;
g_debug ("port %p: set callback", port);
g_debug ("port %p: set receive callback", port);
if (priv->received_buffer_notify)
priv->received_buffer_notify (priv->received_buffer_data);;
@ -112,6 +112,54 @@ pinos_port_set_received_buffer_cb (PinosPort *port,
priv->received_buffer_notify = notify;
}
gulong
pinos_port_add_send_buffer_cb (PinosPort *port,
PinosBufferCallback 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);
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;
priv->send_datas = g_list_prepend (priv->send_datas, data);
return data->id;
}
void
pinos_port_remove_send_buffer_cb (PinosPort *port,
gulong id)
{
PinosPortPrivate *priv;
GList *walk;
g_return_if_fail (PINOS_IS_PORT (port));
priv = port->priv;
g_debug ("port %p: remove send callback %lu", port, id);
for (walk = priv->send_datas; walk; walk = g_list_next (walk)) {
SendData *data = walk->data;
if (data->id == id) {
if (data->send_buffer_notify)
data->send_buffer_notify (data->send_buffer_data);;
g_slice_free (SendData, data);
priv->send_datas = g_list_delete_link (priv->send_datas, walk);
break;
}
}
}
static void
pinos_port_get_property (GObject *_object,
@ -131,14 +179,6 @@ pinos_port_get_property (GObject *_object,
g_value_set_object (value, priv->node);
break;
case PROP_MAX_PEERS:
g_value_set_uint (value, priv->max_peers);
break;
case PROP_PEERS:
g_value_set_boxed (value, priv->peer_paths);
break;
case PROP_DIRECTION:
g_value_set_enum (value, priv->direction);
break;
@ -183,16 +223,6 @@ pinos_port_set_property (GObject *_object,
priv->direction = g_value_get_enum (value);
break;
case PROP_MAX_PEERS:
priv->max_peers = g_value_get_uint (value);
break;
case PROP_PEERS:
if (priv->peer_paths)
g_strfreev (priv->peer_paths);
priv->peer_paths = g_value_dup_boxed (value);
break;
case PROP_POSSIBLE_FORMATS:
if (priv->possible_formats)
g_bytes_unref (priv->possible_formats);
@ -242,6 +272,7 @@ pinos_port_finalize (GObject * object)
{
PinosPort *port = PINOS_PORT (object);
PinosPortPrivate *priv = port->priv;
GList *walk;
g_debug ("port %p: finalize", port);
g_clear_pointer (&priv->possible_formats, g_bytes_unref);
@ -249,7 +280,14 @@ pinos_port_finalize (GObject * object)
g_clear_pointer (&priv->properties, pinos_properties_free);
if (priv->received_buffer_notify)
priv->received_buffer_notify (priv->received_buffer_data);
g_ptr_array_unref (priv->peers);
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);
g_slice_free (SendData, data);
}
g_list_free (priv->send_datas);
g_clear_object (&priv->daemon);
G_OBJECT_CLASS (pinos_port_parent_class)->finalize (object);
@ -298,23 +336,6 @@ pinos_port_class_init (PinosPortClass * klass)
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_MAX_PEERS,
g_param_spec_uint ("max-peers",
"Max Peers",
"The maximum number of peer ports",
1, G_MAXUINT, G_MAXUINT,
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_PEERS,
g_param_spec_boxed ("peers",
"Peers",
"The peer ports of the port",
G_TYPE_STRV,
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_POSSIBLE_FORMATS,
@ -333,7 +354,7 @@ pinos_port_class_init (PinosPortClass * klass)
"The format of the port",
G_TYPE_BYTES,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_CONSTRUCT |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
@ -368,27 +389,26 @@ pinos_port_class_init (PinosPortClass * klass)
G_TYPE_NONE,
0,
G_TYPE_NONE);
signals[SIGNAL_LINKED] = g_signal_new ("linked",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
0,
NULL,
NULL,
g_cclosure_marshal_generic,
G_TYPE_BOOLEAN,
1,
PINOS_TYPE_PORT);
signals[SIGNAL_UNLINKED] = g_signal_new ("unlinked",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
0,
NULL,
NULL,
g_cclosure_marshal_generic,
G_TYPE_NONE,
1,
PINOS_TYPE_PORT);
signals[SIGNAL_ACTIVATE] = g_signal_new ("activate",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
0,
NULL,
NULL,
g_cclosure_marshal_generic,
G_TYPE_NONE,
0,
G_TYPE_NONE);
signals[SIGNAL_DEACTIVATE] = g_signal_new ("deactivate",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
0,
NULL,
NULL,
g_cclosure_marshal_generic,
G_TYPE_NONE,
0,
G_TYPE_NONE);
}
static void
@ -398,7 +418,6 @@ pinos_port_init (PinosPort * port)
g_debug ("port %p: new", port);
priv->direction = PINOS_DIRECTION_INVALID;
priv->peers = g_ptr_array_new_full (64, NULL);
}
/**
@ -529,72 +548,14 @@ pinos_port_filter_formats (PinosPort *port,
GBytes *filter,
GError **error)
{
GstCaps *tmp, *caps, *cfilter;
gchar *str;
PinosPortPrivate *priv;
GBytes *res;
g_return_val_if_fail (PINOS_IS_PORT (port), NULL);
priv = port->priv;
if (filter) {
cfilter = gst_caps_from_string (g_bytes_get_data (filter, NULL));
if (cfilter == NULL)
goto invalid_filter;
} else {
cfilter = NULL;
}
g_signal_emit (port, signals[SIGNAL_FORMAT_REQUEST], 0, NULL);
if (priv->possible_formats)
caps = gst_caps_from_string (g_bytes_get_data (priv->possible_formats, NULL));
else
caps = gst_caps_new_any ();
if (caps && cfilter) {
tmp = gst_caps_intersect_full (caps, cfilter, GST_CAPS_INTERSECT_FIRST);
gst_caps_take (&caps, tmp);
}
g_clear_pointer (&cfilter, gst_caps_unref);
if (caps == NULL || gst_caps_is_empty (caps))
goto no_format;
str = gst_caps_to_string (caps);
gst_caps_unref (caps);
res = g_bytes_new_take (str, strlen (str) + 1);
if (priv->direction == PINOS_DIRECTION_OUTPUT) {
guint i;
for (i = 0; i < priv->peers->len; i++) {
PinosPort *peer = g_ptr_array_index (priv->peers, i);
res = pinos_port_filter_formats (peer, res, error);
}
}
return res;
invalid_filter:
{
g_set_error (error,
G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
"Invalid filter received");
return NULL;
}
no_format:
{
g_set_error (error,
G_IO_ERROR,
G_IO_ERROR_NOT_FOUND,
"No compatible format found");
if (cfilter)
gst_caps_unref (cfilter);
if (caps)
gst_caps_unref (caps);
return NULL;
}
return pinos_format_filter (priv->possible_formats, filter, error);
}
static void
@ -625,224 +586,123 @@ parse_control_buffer (PinosPort *port, PinosBuffer *buffer)
}
}
static gboolean
pinos_port_receive_buffer (PinosPort *port,
PinosBuffer *buffer,
GError **error)
/**
* pinos_port_create_channel:
* @port: a #PinosPort
* @client_path: the client path
* @format_filter: a #GBytes
* @props: #PinosProperties
* @prefix: a prefix
* @error: a #GError or %NULL
*
* Create a new #PinosChannel for @port.
*
* Returns: a new #PinosChannel or %NULL, in wich case @error will contain
* more information about the error.
*/
PinosChannel *
pinos_port_create_channel (PinosPort *port,
const gchar *client_path,
GBytes *format_filter,
PinosProperties *props,
GError **error)
{
PinosPortPrivate *priv = port->priv;
PinosPortPrivate *priv;
PinosChannel *channel;
GBytes *possible_formats;
if (priv->buffer)
goto buffer_queued;
g_return_val_if_fail (PINOS_IS_PORT (port), NULL);
priv = port->priv;
PINOS_DEBUG_TRANSPORT ("port %p: receive buffer %p", port, buffer);
if (pinos_buffer_get_flags (buffer) & PINOS_BUFFER_FLAG_CONTROL)
parse_control_buffer (port, buffer);
possible_formats = pinos_port_filter_formats (port, format_filter, error);
if (possible_formats == NULL)
return NULL;
priv->buffer = buffer;
if (priv->received_buffer_cb)
priv->received_buffer_cb (port, priv->received_buffer_data);
priv->buffer = NULL;
g_debug ("port %p: make channel with formats %s", port,
g_bytes_get_data (possible_formats, NULL));
return TRUE;
channel = g_object_new (PINOS_TYPE_CHANNEL, "daemon", priv->daemon,
"port", port,
"client-path", client_path,
"direction", priv->direction,
"possible-formats", possible_formats,
"properties", props,
NULL);
g_bytes_unref (possible_formats);
if (channel == NULL)
goto no_channel;
return channel;
/* ERRORS */
buffer_queued:
no_channel:
{
g_set_error (error,
G_IO_ERROR,
G_IO_ERROR_NOT_FOUND,
"buffer was already queued on port");
return FALSE;
if (error)
*error = g_error_new (G_IO_ERROR,
G_IO_ERROR_FAILED,
"Could not create channel");
return NULL;
}
}
static void
update_peer_paths (PinosPort *port)
{
PinosPortPrivate *priv = port->priv;
gchar **paths;
guint i;
gint path_index = 0;
paths = g_new0 (gchar *, priv->peers->len + 1);
for (i = 0; i < priv->peers->len; i++) {
PinosPort *peer;
gchar *path;
peer = g_ptr_array_index (priv->peers, i);
g_object_get (peer, "object-path", &path, NULL);
paths[path_index++] = path;
}
g_object_set (port, "peers", paths, NULL);
g_strfreev (paths);
}
/**
* pinos_port_link:
* @source: a source #PinosPort
* @destination: a destination #PinosPort
*
* Link two ports together.
*
* Returns: %TRUE if ports could be linked.
*/
gboolean
pinos_port_link (PinosPort *source, PinosPort *destination)
{
gboolean res = TRUE;
g_return_val_if_fail (PINOS_IS_PORT (source), FALSE);
g_return_val_if_fail (PINOS_IS_PORT (destination), FALSE);
g_return_val_if_fail (source->priv->direction != destination->priv->direction, FALSE);
if (source->priv->peers->len >= source->priv->max_peers)
return FALSE;
if (destination->priv->peers->len >= destination->priv->max_peers)
return FALSE;
if (source->priv->direction != PINOS_DIRECTION_OUTPUT) {
PinosPort *tmp;
tmp = source;
source = destination;
destination = tmp;
}
g_signal_emit (source, signals[SIGNAL_LINKED], 0, destination, &res);
if (!res)
return FALSE;
g_signal_emit (destination, signals[SIGNAL_LINKED], 0, source, &res);
if (!res)
return FALSE;
g_debug ("port %p: linked to %p", source, destination);
g_ptr_array_add (source->priv->peers, destination);
g_ptr_array_add (destination->priv->peers, source);
update_peer_paths (source);
update_peer_paths (destination);
if (source->priv->format) {
PinosBufferBuilder builder;
PinosBuffer pbuf;
PinosPacketFormatChange fc;
GError *error = NULL;
pinos_port_buffer_builder_init (destination, &builder);
fc.id = 0;
fc.format = g_bytes_get_data (source->priv->format, NULL);
pinos_buffer_builder_add_format_change (&builder, &fc);
pinos_buffer_builder_end (&builder, &pbuf);
if (!pinos_port_receive_buffer (destination, &pbuf, &error)) {
g_warning ("port %p: counld not receive format: %s", destination, error->message);
g_clear_error (&error);
}
pinos_buffer_unref (&pbuf);
}
return TRUE;
}
/**
* pinos_port_unlink:
* @source: a source #PinosPort
* @destination: a destination #PinosPort
*
* Link two ports together.
*
* Returns: %TRUE if ports could be linked.
*/
gboolean
pinos_port_unlink (PinosPort *source, PinosPort *destination)
{
g_return_val_if_fail (PINOS_IS_PORT (source), FALSE);
g_return_val_if_fail (PINOS_IS_PORT (destination), FALSE);
g_ptr_array_remove (source->priv->peers, destination);
g_ptr_array_remove (destination->priv->peers, source);
update_peer_paths (source);
update_peer_paths (destination);
g_debug ("port %p: unlinked from %p", source, destination);
g_signal_emit (source, signals[SIGNAL_UNLINKED], 0, destination);
g_signal_emit (destination, signals[SIGNAL_UNLINKED], 0, source);
return TRUE;
}
static void
pinos_port_unlink_all (PinosPort *port)
{
PinosPortPrivate *priv = port->priv;
guint i;
for (i = 0; i < priv->peers->len; i++) {
PinosPort *peer = g_ptr_array_index (priv->peers, i);
g_ptr_array_remove (peer->priv->peers, port);
g_ptr_array_index (priv->peers, i) = NULL;
g_signal_emit (port, signals[SIGNAL_UNLINKED], 0, peer);
g_signal_emit (peer, signals[SIGNAL_UNLINKED], 0, port);
}
g_ptr_array_set_size (priv->peers, 0);
}
/**
* pinos_port_get_links:
* @port: a #PinosPort
* @n_linkes: location to hold the result number of links
*
* Get the links and number of links on this port
*
* Returns: an array of @n_links elements of type #PinosPort.
*/
PinosPort *
pinos_port_get_links (PinosPort *port, guint *n_links)
{
PinosPortPrivate *priv;
g_return_val_if_fail (PINOS_IS_PORT (port), NULL);
priv = port->priv;
if (n_links)
*n_links = priv->peers->len;
return (PinosPort *) priv->peers->pdata;
}
/**
* pinos_port_peek_buffer:
* @port: a #PinosPort
*
* Peek the buffer on @port.
*
* Returns: a #PinosBuffer or %NULL when no buffer has arrived on the pad.
*/
PinosBuffer *
pinos_port_peek_buffer (PinosPort *port)
{
PinosPortPrivate *priv;
g_return_val_if_fail (PINOS_IS_PORT (port), NULL);
priv = port->priv;
return priv->buffer;
}
void
pinos_port_buffer_builder_init (PinosPort *port,
PinosBufferBuilder *builder)
pinos_port_activate (PinosPort *port)
{
PinosPortPrivate *priv;
g_return_if_fail (PINOS_IS_PORT (port));
priv = port->priv;
g_return_if_fail (priv->active_count >= 0);
pinos_buffer_builder_init_into (builder,
priv->send_data, MAX_BUFFER_SIZE,
priv->send_fds, MAX_FDS);
g_debug ("port %p: activate count now %d", port, priv->active_count);
if (priv->active_count++ == 0)
g_signal_emit (port, signals[SIGNAL_ACTIVATE], 0, NULL);
}
void
pinos_port_deactivate (PinosPort *port)
{
PinosPortPrivate *priv;
g_return_if_fail (PINOS_IS_PORT (port));
priv = port->priv;
g_return_if_fail (priv->active_count > 0);
g_debug ("port %p: deactivate count now %d", port, priv->active_count);
if (--priv->active_count == 0)
g_signal_emit (port, signals[SIGNAL_DEACTIVATE], 0, NULL);
}
/**
* pinos_port_receive_buffer:
* @port: a #PinosPort
* @buffer: a #PinosBuffer
* @error: a #GError or %NULL
*
* Receive @buffer on @port
*
* Returns: %TRUE on success. @error is set when %FALSE is returned.
*/
gboolean
pinos_port_receive_buffer (PinosPort *port,
PinosBuffer *buffer,
GError **error)
{
gboolean res = TRUE;
PinosPortPrivate *priv = port->priv;
PINOS_DEBUG_TRANSPORT ("port %p: receive buffer %p", port, buffer);
if (pinos_buffer_get_flags (buffer) & PINOS_BUFFER_FLAG_CONTROL)
parse_control_buffer (port, buffer);
if (priv->received_buffer_cb)
res = priv->received_buffer_cb (port, buffer, error, priv->received_buffer_data);
return res;
}
/**
@ -851,7 +711,7 @@ pinos_port_buffer_builder_init (PinosPort *port,
* @buffer: a #PinosBuffer
* @error: a #GError or %NULL
*
* Send @buffer to ports connected to @port
* Send @buffer out on @port.
*
* Returns: %TRUE on success. @error is set when %FALSE is returned.
*/
@ -860,29 +720,21 @@ pinos_port_send_buffer (PinosPort *port,
PinosBuffer *buffer,
GError **error)
{
PinosPortPrivate *priv;
PinosPort *peer;
gboolean res = TRUE;
guint i;
GError *err = NULL;
PinosPortPrivate *priv;
GList *walk;
g_return_val_if_fail (PINOS_IS_PORT (port), FALSE);
priv = port->priv;
PINOS_DEBUG_TRANSPORT ("port %p: send buffer %p", port, buffer);
if (pinos_buffer_get_flags (buffer) & PINOS_BUFFER_FLAG_CONTROL)
parse_control_buffer (port, buffer);
PINOS_DEBUG_TRANSPORT ("port %p: send buffer %p", port, buffer);
priv = port->priv;
for (i = 0; i < priv->peers->len; i++) {
peer = g_ptr_array_index (priv->peers, i);
res = pinos_port_receive_buffer (peer, buffer, &err);
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);
}
if (!res) {
if (error == NULL)
g_warning ("could not send buffer: %s", err->message);
g_propagate_error (error, err);
}
return res;
}

View file

@ -30,6 +30,7 @@ typedef struct _PinosPortPrivate PinosPortPrivate;
#include <pinos/client/introspect.h>
#include <pinos/client/buffer.h>
#include <pinos/server/channel.h>
#include <pinos/server/daemon.h>
#define PINOS_TYPE_PORT (pinos_port_get_type ())
@ -61,48 +62,50 @@ struct _PinosPortClass {
GObjectClass parent_class;
};
typedef void (*PinosReceivedBufferCallback) (PinosPort *port, gpointer user_data);
typedef gboolean (*PinosBufferCallback) (PinosPort *port, PinosBuffer *buffer, GError **error, gpointer user_data);
/* normal GObject stuff */
GType pinos_port_get_type (void);
void pinos_port_set_received_buffer_cb (PinosPort *port,
PinosReceivedBufferCallback cb,
PinosBufferCallback cb,
gpointer user_data,
GDestroyNotify notify);
gulong pinos_port_add_send_buffer_cb (PinosPort *port,
PinosBufferCallback cb,
gpointer user_data,
GDestroyNotify notify);
void pinos_port_remove_send_buffer_cb (PinosPort *port,
gulong id);
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_get_possible_formats (PinosPort *port);
GBytes * pinos_port_filter_formats (PinosPort *port,
GBytes *filter,
GError **error);
GBytes * pinos_port_get_format (PinosPort *port);
GSocket * pinos_port_get_socket_pair (PinosPort *port,
GError **error);
void pinos_port_activate (PinosPort *port);
void pinos_port_deactivate (PinosPort *port);
gboolean pinos_port_link (PinosPort *source,
PinosPort *destination);
gboolean pinos_port_unlink (PinosPort *source,
PinosPort *destination);
PinosPort * pinos_port_get_links (PinosPort *port,
guint *n_links);
PinosBuffer * pinos_port_peek_buffer (PinosPort *port);
void pinos_port_buffer_builder_init (PinosPort *port,
PinosBufferBuilder *builder);
PinosChannel * pinos_port_create_channel (PinosPort *port,
const gchar *client_path,
GBytes *format_filter,
PinosProperties *props,
GError **error);
gboolean pinos_port_send_buffer (PinosPort *port,
PinosBuffer *buffer,
GError **error);
gboolean pinos_port_receive_buffer (PinosPort *port,
PinosBuffer *buffer,
GError **error);
G_END_DECLS

84
pinos/server/utils.c Normal file
View file

@ -0,0 +1,84 @@
/* Pinos
* Copyright (C) 2015 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <string.h>
#include <gst/gst.h>
#include <gio/gio.h>
#include "pinos/server/utils.h"
GBytes *
pinos_format_filter (GBytes *format,
GBytes *filter,
GError **error)
{
GstCaps *tmp, *caps, *cfilter;
gchar *str;
GBytes *res;
if (filter) {
cfilter = gst_caps_from_string (g_bytes_get_data (filter, NULL));
if (cfilter == NULL)
goto invalid_filter;
} else {
cfilter = NULL;
}
if (format)
caps = gst_caps_from_string (g_bytes_get_data (format, NULL));
else
caps = gst_caps_new_any ();
if (caps && cfilter) {
tmp = gst_caps_intersect_full (caps, cfilter, GST_CAPS_INTERSECT_FIRST);
gst_caps_take (&caps, tmp);
}
g_clear_pointer (&cfilter, gst_caps_unref);
if (caps == NULL || gst_caps_is_empty (caps))
goto no_format;
str = gst_caps_to_string (caps);
gst_caps_unref (caps);
res = g_bytes_new_take (str, strlen (str) + 1);
return res;
invalid_filter:
{
g_set_error (error,
G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
"Invalid filter received");
return NULL;
}
no_format:
{
g_set_error (error,
G_IO_ERROR,
G_IO_ERROR_NOT_FOUND,
"No compatible format found");
if (cfilter)
gst_caps_unref (cfilter);
if (caps)
gst_caps_unref (caps);
return NULL;
}
}

31
pinos/server/utils.h Normal file
View file

@ -0,0 +1,31 @@
/* Pinos
* Copyright (C) 2015 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __PINOS_UTILS_H__
#define __PINOS_UTILS_H__
#include <glib-object.h>
G_BEGIN_DECLS
GBytes * pinos_format_filter (GBytes *format1, GBytes *format2, GError **error);
G_END_DECLS
#endif /* __PINOS_UTILS_H__ */