Introduce the concept of a Port

A port is an input or output on a Node.
Channels are created from the ports and inherit the direction of the
port.
do automatic port selection based on the direction and caps and
node/port name.
Simplify stream_connect by passing the direction.
Fix pinossink to connect in setcaps so that we know the format and can
select a good sink to connect to.
This commit is contained in:
Wim Taymans 2016-05-06 13:01:52 +02:00
parent b885d40390
commit ba4ef9b5d9
35 changed files with 1939 additions and 2120 deletions

View file

@ -36,7 +36,8 @@ struct _PinosChannelPrivate
gchar *object_path;
gchar *client_path;
gchar *owner_path;
gchar *port_path;
PinosDirection direction;
GBytes *possible_formats;
PinosProperties *properties;
@ -58,7 +59,8 @@ enum
PROP_DAEMON,
PROP_OBJECT_PATH,
PROP_CLIENT_PATH,
PROP_OWNER_PATH,
PROP_PORT_PATH,
PROP_DIRECTION,
PROP_POSSIBLE_FORMATS,
PROP_PROPERTIES,
PROP_REQUESTED_FORMAT,
@ -97,8 +99,12 @@ pinos_channel_get_property (GObject *_object,
g_value_set_string (value, priv->client_path);
break;
case PROP_OWNER_PATH:
g_value_set_string (value, priv->owner_path);
case PROP_PORT_PATH:
g_value_set_string (value, priv->port_path);
break;
case PROP_DIRECTION:
g_value_set_enum (value, priv->direction);
break;
case PROP_POSSIBLE_FORMATS:
@ -154,9 +160,14 @@ pinos_channel_set_property (GObject *_object,
g_object_set (priv->iface, "client", priv->client_path, NULL);
break;
case PROP_OWNER_PATH:
priv->owner_path = g_value_dup_string (value);
g_object_set (priv->iface, "owner", priv->owner_path, NULL);
case PROP_PORT_PATH:
priv->port_path = g_value_dup_string (value);
g_object_set (priv->iface, "port", priv->port_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:
@ -212,7 +223,7 @@ stop_transfer (PinosChannel *channel)
g_object_notify (G_OBJECT (channel), "socket");
}
clear_formats (channel);
priv->state = PINOS_CHANNEL_STATE_IDLE;
priv->state = PINOS_CHANNEL_STATE_STOPPED;
g_object_set (priv->iface,
"state", priv->state,
NULL);
@ -321,14 +332,13 @@ handle_remove (PinosChannel1 *interface,
}
static void
channel_register_object (PinosChannel *channel,
const gchar *prefix)
channel_register_object (PinosChannel *channel)
{
PinosChannelPrivate *priv = channel->priv;
PinosObjectSkeleton *skel;
gchar *name;
name = g_strdup_printf ("%s/channel", prefix);
name = g_strdup_printf ("%s/channel", priv->client_path);
skel = pinos_object_skeleton_new (name);
g_free (name);
@ -377,7 +387,7 @@ pinos_channel_finalize (GObject * object)
g_clear_object (&priv->iface);
g_free (priv->client_path);
g_free (priv->object_path);
g_free (priv->owner_path);
g_free (priv->port_path);
G_OBJECT_CLASS (pinos_channel_parent_class)->finalize (object);
}
@ -386,10 +396,9 @@ static void
pinos_channel_constructed (GObject * object)
{
PinosChannel *channel = PINOS_CHANNEL (object);
PinosChannelPrivate *priv = channel->priv;
g_debug ("channel %p: constructed", channel);
channel_register_object (channel, priv->object_path);
channel_register_object (channel);
G_OBJECT_CLASS (pinos_channel_parent_class)->constructed (object);
}
@ -438,15 +447,26 @@ pinos_channel_class_init (PinosChannelClass * klass)
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_OWNER_PATH,
g_param_spec_string ("owner-path",
"Owner Path",
"The owner object path",
PROP_PORT_PATH,
g_param_spec_string ("port-path",
"Port Path",
"The port object path",
NULL,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_CONSTRUCT |
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",
@ -516,9 +536,11 @@ pinos_channel_init (PinosChannel * 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_IDLE;
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);
}

View file

@ -1,77 +0,0 @@
/* 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_SOURCE_H__
#define __PINOS_CLIENT_SOURCE_H__
#include <glib-object.h>
G_BEGIN_DECLS
typedef struct _PinosClientSource PinosClientSource;
typedef struct _PinosClientSourceClass PinosClientSourceClass;
typedef struct _PinosClientSourcePrivate PinosClientSourcePrivate;
#include <pinos/server/source.h>
#define PINOS_TYPE_CLIENT_SOURCE (pinos_client_source_get_type ())
#define PINOS_IS_CLIENT_SOURCE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PINOS_TYPE_SOURCE))
#define PINOS_IS_CLIENT_SOURCE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PINOS_TYPE_SOURCE))
#define PINOS_CLIENT_SOURCE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), PINOS_TYPE_SOURCE, PinosClientSourceClass))
#define PINOS_CLIENT_SOURCE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), PINOS_TYPE_SOURCE, PinosClientSource))
#define PINOS_CLIENT_SOURCE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PINOS_TYPE_SOURCE, PinosClientSourceClass))
#define PINOS_CLIENT_SOURCE_CAST(obj) ((PinosClientSource*)(obj))
#define PINOS_CLIENT_SOURCE_CLASS_CAST(klass) ((PinosClientSourceClass*)(klass))
/**
* PinosClientSource:
*
* Pinos client source object class.
*/
struct _PinosClientSource {
PinosSource object;
PinosClientSourcePrivate *priv;
};
/**
* PinosClientSourceClass:
*
* Pinos client source object class.
*/
struct _PinosClientSourceClass {
PinosSourceClass parent_class;
};
/* normal GObject stuff */
GType pinos_client_source_get_type (void);
PinosSource * pinos_client_source_new (PinosDaemon *daemon,
GBytes *possible_formats);
PinosChannel * pinos_client_source_get_channel (PinosClientSource *source,
const gchar *client_path,
GBytes *format_filter,
PinosProperties *props,
const gchar *prefix,
GError **error);
G_END_DECLS
#endif /* __PINOS_CLIENT_SOURCE_H__ */

View file

@ -21,7 +21,7 @@
#include "pinos/client/pinos.h"
#include "pinos/server/client.h"
#include "pinos/server/client-source.h"
#include "pinos/server/upload-node.h"
#include "pinos/dbus/org-pinos.h"
@ -139,17 +139,17 @@ handle_remove_channel (PinosChannel *channel,
}
static gboolean
handle_create_source_channel (PinosClient1 *interface,
GDBusMethodInvocation *invocation,
const gchar *arg_source,
const gchar *arg_accepted_formats,
GVariant *arg_properties,
gpointer user_data)
handle_create_channel (PinosClient1 *interface,
GDBusMethodInvocation *invocation,
PinosDirection direction,
const gchar *arg_port,
const gchar *arg_possible_formats,
GVariant *arg_properties,
gpointer user_data)
{
PinosClient *client = user_data;
PinosClientPrivate *priv = client->priv;
PinosNode *node;
PinosSource *source;
PinosPort *port;
PinosChannel *channel;
const gchar *object_path, *sender;
GBytes *formats;
@ -160,27 +160,25 @@ handle_create_source_channel (PinosClient1 *interface,
if (g_strcmp0 (pinos_client_get_sender (client), sender) != 0)
goto not_allowed;
formats = g_bytes_new (arg_accepted_formats, strlen (arg_accepted_formats) + 1);
formats = g_bytes_new (arg_possible_formats, strlen (arg_possible_formats) + 1);
props = pinos_properties_from_variant (arg_properties);
node = pinos_daemon_find_node (priv->daemon,
arg_source,
port = pinos_daemon_find_port (priv->daemon,
direction,
arg_port,
props,
formats,
&error);
if (node == NULL)
goto no_node;
if (port == NULL)
goto no_port;
source = pinos_node_get_source (node);
if (source == NULL)
goto no_source;
g_debug ("client %p: matched port %s", client, pinos_port_get_object_path (port));
channel = pinos_source_create_channel (source,
priv->object_path,
formats,
props,
priv->object_path,
&error);
channel = pinos_port_create_channel (port,
priv->object_path,
formats,
props,
&error);
pinos_properties_free (props);
g_bytes_unref (formats);
@ -208,124 +206,18 @@ not_allowed:
"org.pinos.Error", "not client owner");
return TRUE;
}
no_node:
no_port:
{
g_debug ("client %p: could not find node %s, %s", client, arg_source, error->message);
g_debug ("client %p: could not find port %s, %s", client, arg_port, error->message);
g_dbus_method_invocation_return_gerror (invocation, error);
pinos_properties_free (props);
g_bytes_unref (formats);
g_clear_error (&error);
return TRUE;
}
no_source:
{
g_debug ("client %p: node %s is not a source", client, arg_source);
g_dbus_method_invocation_return_dbus_error (invocation,
"org.pinos.Error", "not node is not a source");
pinos_properties_free (props);
g_bytes_unref (formats);
return TRUE;
}
no_channel:
{
g_debug ("client %p: could not create source channel %s", client, error->message);
g_dbus_method_invocation_return_gerror (invocation, error);
g_clear_error (&error);
return TRUE;
}
}
static gboolean
handle_create_sink_channel (PinosClient1 *interface,
GDBusMethodInvocation *invocation,
const gchar *arg_sink,
const gchar *arg_accepted_formats,
GVariant *arg_properties,
gpointer user_data)
{
PinosClient *client = user_data;
PinosClientPrivate *priv = client->priv;
PinosNode *node;
PinosSink *sink;
PinosChannel *channel;
const gchar *object_path, *sender;
GBytes *formats;
PinosProperties *props;
GError *error = NULL;
sender = g_dbus_method_invocation_get_sender (invocation);
if (g_strcmp0 (pinos_client_get_sender (client), sender) != 0)
goto not_allowed;
formats = g_bytes_new (arg_accepted_formats, strlen (arg_accepted_formats) + 1);
props = pinos_properties_from_variant (arg_properties);
node = pinos_daemon_find_node (priv->daemon,
arg_sink,
props,
formats,
&error);
if (node == NULL)
goto no_node;
sink = pinos_node_get_sink (node);
if (sink == NULL)
goto no_sink;
channel = pinos_sink_create_channel (sink,
priv->object_path,
formats,
props,
priv->object_path,
&error);
pinos_properties_free (props);
g_bytes_unref (formats);
if (channel == NULL)
goto no_channel;
priv->channels = g_list_prepend (priv->channels, channel);
g_signal_connect (channel,
"remove",
(GCallback) handle_remove_channel,
client);
object_path = pinos_channel_get_object_path (channel);
g_debug ("client %p: add sink channel %p, %s", client, channel, object_path);
g_dbus_method_invocation_return_value (invocation,
g_variant_new ("(o)", object_path));
return TRUE;
/* ERRORS */
not_allowed:
{
g_dbus_method_invocation_return_dbus_error (invocation,
"org.pinos.Error", "not client owner");
return TRUE;
}
no_node:
{
g_debug ("client %p: could not find node %s, %s", client, arg_sink, error->message);
g_dbus_method_invocation_return_gerror (invocation, error);
pinos_properties_free (props);
g_bytes_unref (formats);
g_clear_error (&error);
return TRUE;
}
no_sink:
{
g_debug ("client %p: node %s is not a sink", client, arg_sink);
g_dbus_method_invocation_return_dbus_error (invocation,
"org.pinos.Error", "node is not a sink");
pinos_properties_free (props);
g_bytes_unref (formats);
return TRUE;
}
no_channel:
{
g_debug ("client %p: could not create sink channel %s", client, error->message);
g_debug ("client %p: could not create channel %s", client, error->message);
g_dbus_method_invocation_return_gerror (invocation, error);
g_clear_error (&error);
return TRUE;
@ -341,7 +233,7 @@ handle_create_upload_channel (PinosClient1 *interface,
{
PinosClient *client = user_data;
PinosClientPrivate *priv = client->priv;
PinosSource *source;
PinosNode *node;
PinosChannel *channel;
const gchar *channel_path, *sender;
GBytes *formats;
@ -354,19 +246,18 @@ handle_create_upload_channel (PinosClient1 *interface,
formats = g_bytes_new (arg_possible_formats, strlen (arg_possible_formats) + 1);
source = pinos_client_source_new (priv->daemon, formats);
if (source == NULL)
goto no_source;
node = pinos_upload_node_new (priv->daemon, formats);
if (node == NULL)
goto no_node;
sender = g_dbus_method_invocation_get_sender (invocation);
props = pinos_properties_from_variant (arg_properties);
channel = pinos_client_source_get_channel (PINOS_CLIENT_SOURCE (source),
priv->object_path,
formats,
props,
priv->object_path,
&error);
channel = pinos_upload_node_get_channel (PINOS_UPLOAD_NODE (node),
priv->object_path,
formats,
props,
&error);
pinos_properties_free (props);
if (channel == NULL)
@ -374,7 +265,7 @@ handle_create_upload_channel (PinosClient1 *interface,
g_object_set_data_full (G_OBJECT (channel),
"channel-owner",
source,
node,
g_object_unref);
channel_path = pinos_channel_get_object_path (channel);
@ -399,11 +290,11 @@ not_allowed:
"org.pinos.Error", "not client owner");
return TRUE;
}
no_source:
no_node:
{
g_debug ("client %p: could not create source", client);
g_debug ("client %p: could not create upload node", client);
g_dbus_method_invocation_return_dbus_error (invocation,
"org.pinos.Error", "Can't create source");
"org.pinos.Error", "Can't create upload node");
g_bytes_unref (formats);
return TRUE;
}
@ -411,7 +302,7 @@ no_channel:
{
g_debug ("client %p: could not create upload channel %s", client, error->message);
g_dbus_method_invocation_return_gerror (invocation, error);
g_object_unref (source);
g_object_unref (node);
g_clear_error (&error);
g_bytes_unref (formats);
return TRUE;
@ -448,11 +339,8 @@ client_register_object (PinosClient *client,
priv->client1 = pinos_client1_skeleton_new ();
pinos_client1_set_name (priv->client1, priv->sender);
pinos_client1_set_properties (priv->client1, pinos_properties_to_variant (priv->properties));
g_signal_connect (priv->client1, "handle-create-source-channel",
(GCallback) handle_create_source_channel,
client);
g_signal_connect (priv->client1, "handle-create-sink-channel",
(GCallback) handle_create_sink_channel,
g_signal_connect (priv->client1, "handle-create-channel",
(GCallback) handle_create_channel,
client);
g_signal_connect (priv->client1, "handle-create-upload-channel",
(GCallback) handle_create_upload_channel,

View file

@ -17,6 +17,8 @@
* Boston, MA 02110-1301, USA.
*/
#include <string.h>
#include <gio/gio.h>
#include "config.h"
@ -380,47 +382,83 @@ pinos_daemon_remove_node (PinosDaemon *daemon,
}
/**
* pinos_daemon_find_node:
* pinos_daemon_find_port:
* @daemon: a #PinosDaemon
* @name: a node name
* @props: node properties
* @name: a port name
* @props: port properties
* @format_filter: a format filter
* @error: location for an error
*
* Find the best node in @daemon that matches the given parameters.
* Find the best port in @daemon that matches the given parameters.
*
* Returns: a #PinosNode or %NULL when no node could be found.
* Returns: a #PinosPort or %NULL when no port could be found.
*/
PinosNode *
pinos_daemon_find_node (PinosDaemon *daemon,
PinosPort *
pinos_daemon_find_port (PinosDaemon *daemon,
PinosDirection direction,
const gchar *name,
PinosProperties *props,
GBytes *format_filter,
GError **error)
{
PinosDaemonPrivate *priv;
PinosNode *best = NULL;
GList *walk;
PinosPort *best = NULL;
GList *node, *port;
gboolean have_name;
g_return_val_if_fail (PINOS_IS_DAEMON (daemon), NULL);
priv = daemon->priv;
for (walk = priv->nodes; walk; walk = g_list_next (walk)) {
PinosNode *n = walk->data;
have_name = name ? strlen (name) > 0 : FALSE;
if (name == NULL) {
best = n;
break;
for (node = priv->nodes; node; node = g_list_next (node)) {
PinosNode *n = node->data;
gboolean node_found = FALSE;
/* we found the node */
if (have_name && g_str_has_suffix (pinos_node_get_object_path (n), name)) {
g_debug ("name \"%s\" matches node %s", name, pinos_node_get_object_path (n));
node_found = TRUE;
}
else if (g_str_has_suffix (pinos_node_get_object_path (n), name))
best = n;
}
for (port = pinos_node_get_ports (n); port; port = g_list_next (port)) {
PinosPort *p = port->data;
PinosDirection dir;
GBytes *format;
g_object_get (p, "direction", &dir, NULL);
if (dir != direction)
continue;
if (have_name && !node_found) {
if (!g_str_has_suffix (pinos_port_get_object_path (p), name))
continue;
g_debug ("name \"%s\" matches port %s", name, pinos_port_get_object_path (p));
best = p;
node_found = TRUE;
break;
}
format = pinos_port_get_formats (p, format_filter, NULL);
if (format != NULL) {
g_debug ("port %s with format %s matches filter %s",
pinos_port_get_object_path (p),
(gchar*)g_bytes_get_data (format, NULL),
format_filter ? (gchar*)g_bytes_get_data (format_filter, NULL) : "ANY");
g_bytes_unref (format);
best = p;
node_found = TRUE;
break;
}
}
if (node_found)
break;
}
if (best == NULL) {
if (error)
*error = g_error_new (G_IO_ERROR,
G_IO_ERROR_NOT_FOUND,
"Node not found");
"No matching Port found");
}
return best;
}

View file

@ -38,6 +38,7 @@ typedef struct _PinosDaemonClass PinosDaemonClass;
typedef struct _PinosDaemonPrivate PinosDaemonPrivate;
#include <pinos/server/node.h>
#include <pinos/server/port.h>
#include <pinos/client/properties.h>
/**
@ -73,7 +74,9 @@ void pinos_daemon_unexport (PinosDaemon *daemon, const gch
void pinos_daemon_add_node (PinosDaemon *daemon, PinosNode *node);
void pinos_daemon_remove_node (PinosDaemon *daemon, PinosNode *node);
PinosNode * pinos_daemon_find_node (PinosDaemon *daemon,
PinosPort * pinos_daemon_find_port (PinosDaemon *daemon,
PinosDirection direction,
const gchar *name,
PinosProperties *props,
GBytes *format_filter,

View file

@ -23,7 +23,6 @@
#include "pinos/client/enumtypes.h"
#include "pinos/server/node.h"
#include "pinos/server/source.h"
#include "pinos/server/daemon.h"
#include "pinos/dbus/org-pinos.h"
@ -35,10 +34,18 @@
struct _PinosNodePrivate
{
PinosDaemon *daemon;
PinosObjectSkeleton *skeleton;
PinosNode1 *iface;
gchar *object_path;
PinosSource *source;
PinosSink *sink;
gchar *name;
PinosNodeState state;
GError *error;
guint idle_timeout;
PinosProperties *properties;
GList *ports;
};
G_DEFINE_TYPE (PinosNode, pinos_node, G_TYPE_OBJECT);
@ -47,8 +54,10 @@ enum
{
PROP_0,
PROP_DAEMON,
PROP_SKELETON,
PROP_OBJECT_PATH,
PROP_NAME,
PROP_STATE,
PROP_PROPERTIES,
};
static void
@ -65,14 +74,22 @@ pinos_node_get_property (GObject *_object,
g_value_set_object (value, priv->daemon);
break;
case PROP_SKELETON:
g_value_set_object (value, priv->skeleton);
break;
case PROP_OBJECT_PATH:
g_value_set_string (value, priv->object_path);
break;
case PROP_NAME:
g_value_set_string (value, priv->name);
break;
case PROP_STATE:
g_value_set_enum (value, priv->state);
break;
case PROP_PROPERTIES:
g_value_set_boxed (value, priv->properties);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (node, prop_id, pspec);
break;
@ -93,6 +110,19 @@ pinos_node_set_property (GObject *_object,
priv->daemon = g_value_dup_object (value);
break;
case PROP_NAME:
priv->name = g_value_dup_string (value);
break;
case PROP_PROPERTIES:
if (priv->properties)
pinos_properties_free (priv->properties);
priv->properties = g_value_dup_boxed (value);
if (priv->iface)
pinos_node1_set_properties (priv->iface,
priv->properties ? pinos_properties_to_variant (priv->properties) : NULL);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (node, prop_id, pspec);
break;
@ -104,11 +134,20 @@ node_register_object (PinosNode *node)
{
PinosNodePrivate *priv = node->priv;
PinosDaemon *daemon = priv->daemon;
PinosObjectSkeleton *skel;
priv->skeleton = pinos_object_skeleton_new (PINOS_DBUS_OBJECT_NODE);
skel = pinos_object_skeleton_new (PINOS_DBUS_OBJECT_NODE);
priv->iface = pinos_node1_skeleton_new ();
pinos_node1_set_name (priv->iface, priv->name);
if (priv->properties)
pinos_node1_set_properties (priv->iface, pinos_properties_to_variant (priv->properties));
pinos_node1_set_state (priv->iface, priv->state);
pinos_object_skeleton_set_node1 (skel, priv->iface);
g_free (priv->object_path);
priv->object_path = pinos_daemon_export_uniquely (daemon, G_DBUS_OBJECT_SKELETON (priv->skeleton));
priv->object_path = pinos_daemon_export_uniquely (daemon, G_DBUS_OBJECT_SKELETON (skel));
g_object_unref (skel);
pinos_daemon_add_node (daemon, node);
@ -122,7 +161,7 @@ node_unregister_object (PinosNode *node)
pinos_daemon_unexport (priv->daemon, priv->object_path);
pinos_daemon_remove_node (priv->daemon, node);
g_clear_object (&priv->skeleton);
g_clear_object (&priv->iface);
}
static void
@ -187,6 +226,37 @@ pinos_node_class_init (PinosNodeClass * klass)
NULL,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_NAME,
g_param_spec_string ("name",
"Name",
"The node name",
NULL,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_STATE,
g_param_spec_enum ("state",
"State",
"The state of the node",
PINOS_TYPE_NODE_STATE,
PINOS_NODE_STATE_SUSPENDED,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_PROPERTIES,
g_param_spec_boxed ("properties",
"Properties",
"The properties of the node",
PINOS_TYPE_PROPERTIES,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT |
G_PARAM_STATIC_STRINGS));
}
static void
@ -234,96 +304,210 @@ pinos_node_get_object_path (PinosNode *node)
}
/**
* pinos_node_set_source:
* pinos_node_add_port:
* @node: a #PinosNode
* @source: a #PinosSource
* @port: a #PinosPort
*
* Set the #PinosSource of @node
* Add the #PinosPort to @node
*/
void
pinos_node_set_source (PinosNode *node, PinosSource *source, GObject *iface)
pinos_node_add_port (PinosNode *node, PinosPort *port)
{
PinosNodePrivate *priv;
g_return_if_fail (PINOS_IS_NODE (node));
g_return_if_fail (source == NULL || PINOS_IS_SOURCE (source));
g_return_if_fail (iface == NULL || PINOS_IS_SOURCE1 (iface));
g_return_if_fail (PINOS_IS_PORT (port));
priv = node->priv;
if (source) {
pinos_object_skeleton_set_source1 (priv->skeleton, PINOS_SOURCE1 (iface));
priv->source = source;
} else {
pinos_object_skeleton_set_source1 (priv->skeleton, NULL);
priv->source = NULL;
}
priv->ports = g_list_append (priv->ports, port);
}
/**
* pinos_node_get_source:
* pinos_node_remove_port:
* @node: a #PinosNode
* @port: a #PinosPort
*
* Remove the #PinosPort from @node
*/
void
pinos_node_remove_port (PinosNode *node, PinosPort *port)
{
PinosNodePrivate *priv;
g_return_if_fail (PINOS_IS_NODE (node));
g_return_if_fail (PINOS_IS_PORT (port));
priv = node->priv;
priv->ports = g_list_remove (priv->ports, port);
}
/**
* pinos_node_get_ports:
* @node: a #PinosNode
*
* Get the #PinosSource of @node
* Get the list of ports in @node.
*
* Returns: the #PinosSource of @node or %NULL
* Returns: a #GList of nodes owned by @node.
*/
PinosSource *
pinos_node_get_source (PinosNode *node)
GList *
pinos_node_get_ports (PinosNode *node)
{
PinosNodePrivate *priv;
g_return_val_if_fail (PINOS_IS_NODE (node), NULL);
priv = node->priv;
return priv->source;
return priv->ports;
}
/**
* pinos_node_set_sink:
* @node: a #PinosNode
* @sink: a #PinosSink
*
* Set the #PinosSink of @node
*/
void
pinos_node_set_sink (PinosNode *node, PinosSink *sink, GObject *iface)
static void
remove_idle_timeout (PinosNode *node)
{
PinosNodePrivate *priv;
PinosNodePrivate *priv = node->priv;
g_return_if_fail (PINOS_IS_NODE (node));
g_return_if_fail (sink == NULL || PINOS_IS_SINK (sink));
g_return_if_fail (iface == NULL || PINOS_IS_SINK1 (iface));
priv = node->priv;
if (sink) {
pinos_object_skeleton_set_sink1 (priv->skeleton, PINOS_SINK1 (iface));
priv->sink = sink;
} else {
pinos_object_skeleton_set_sink1 (priv->skeleton, NULL);
priv->sink = NULL;
if (priv->idle_timeout) {
g_source_remove (priv->idle_timeout);
priv->idle_timeout = 0;
}
}
/**
* pinos_node_get_sink:
* pinos_node_set_state:
* @node: a #PinosNode
* @state: a #PinosNodeState
*
* Get the #PinosSink of @node
* Set the state of @node to @state.
*
* Returns: the #PinosSink of @node or %NULL
* Returns: %TRUE on success.
*/
PinosSink *
pinos_node_get_sink (PinosNode *node)
gboolean
pinos_node_set_state (PinosNode *node,
PinosNodeState state)
{
PinosNodeClass *klass;
gboolean res;
g_return_val_if_fail (PINOS_IS_NODE (node), FALSE);
klass = PINOS_NODE_GET_CLASS (node);
remove_idle_timeout (node);
if (klass->set_state)
res = klass->set_state (node, state);
else
res = FALSE;
return res;
}
/**
* pinos_node_update_state:
* @node: a #PinosNode
* @state: a #PinosNodeState
*
* Update the state of a node. This method is used from
* inside @node itself.
*/
void
pinos_node_update_state (PinosNode *node,
PinosNodeState state)
{
PinosNodePrivate *priv;
g_return_val_if_fail (PINOS_IS_NODE (node), NULL);
g_return_if_fail (PINOS_IS_NODE (node));
priv = node->priv;
return priv->sink;
if (priv->state != state) {
priv->state = state;
pinos_node1_set_state (priv->iface, state);
g_object_notify (G_OBJECT (node), "state");
}
}
/**
* pinos_node_report_error:
* @node: a #PinosNode
* @error: a #GError
*
* Report an error from within @node.
*/
void
pinos_node_report_error (PinosNode *node,
GError *error)
{
PinosNodePrivate *priv;
g_return_if_fail (PINOS_IS_NODE (node));
priv = node->priv;
g_clear_error (&priv->error);
remove_idle_timeout (node);
priv->error = error;
priv->state = PINOS_NODE_STATE_ERROR;
g_debug ("got error state %s", error->message);
pinos_node1_set_state (priv->iface, priv->state);
g_object_notify (G_OBJECT (node), "state");
}
static gboolean
idle_timeout (PinosNode *node)
{
PinosNodePrivate *priv = node->priv;
priv->idle_timeout = 0;
pinos_node_set_state (node, PINOS_NODE_STATE_SUSPENDED);
return G_SOURCE_REMOVE;
}
/**
* pinos_node_report_idle:
* @node: a #PinosNode
*
* Mark @node as being idle. This will start a timeout that will
* set the node to SUSPENDED.
*/
void
pinos_node_report_idle (PinosNode *node)
{
PinosNodePrivate *priv;
g_return_if_fail (PINOS_IS_NODE (node));
priv = node->priv;
pinos_node_set_state (node, PINOS_NODE_STATE_IDLE);
priv->idle_timeout = g_timeout_add_seconds (3,
(GSourceFunc) idle_timeout,
node);
}
/**
* pinos_node_report_busy:
* @node: a #PinosNode
*
* Mark @node as being busy. This will set the state of the node
* to the RUNNING state.
*/
void
pinos_node_report_busy (PinosNode *node)
{
g_return_if_fail (PINOS_IS_NODE (node));
pinos_node_set_state (node, PINOS_NODE_STATE_RUNNING);
}
/**
* pinos_node_new:
* @daemon: a #PinosDaemon
*
* Make a new node
*
* Returns: a new #PinosNode
*/
PinosNode *
pinos_node_new (PinosDaemon *daemon)
{

View file

@ -30,8 +30,8 @@ typedef struct _PinosNodePrivate PinosNodePrivate;
#include <pinos/client/introspect.h>
#include <pinos/server/daemon.h>
#include <pinos/server/source.h>
#include <pinos/server/sink.h>
#include <pinos/server/node.h>
#include <pinos/server/port.h>
#define PINOS_TYPE_NODE (pinos_node_get_type ())
#define PINOS_IS_NODE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PINOS_TYPE_NODE))
@ -55,11 +55,15 @@ struct _PinosNode {
/**
* PinosNodeClass:
* @set_state: called to change the current state of the node
*
* Pinos node class.
*/
struct _PinosNodeClass {
GObjectClass parent_class;
gboolean (*set_state) (PinosNode *node, PinosNodeState state);
};
/* normal GObject stuff */
@ -70,15 +74,18 @@ PinosNode * pinos_node_new (PinosDaemon *daemon);
PinosDaemon * pinos_node_get_daemon (PinosNode *node);
const gchar * pinos_node_get_object_path (PinosNode *node);
void pinos_node_set_source (PinosNode *node,
PinosSource *source,
GObject *iface);
PinosSource * pinos_node_get_source (PinosNode *node);
void pinos_node_add_port (PinosNode *node,
PinosPort *port);
void pinos_node_remove_port (PinosNode *node,
PinosPort *port);
GList * pinos_node_get_ports (PinosNode *node);
void pinos_node_set_sink (PinosNode *node,
PinosSink *sink,
GObject *iface);
PinosSink * pinos_node_get_sink (PinosNode *node);
gboolean pinos_node_set_state (PinosNode *node, PinosNodeState state);
void pinos_node_update_state (PinosNode *node, PinosNodeState state);
void pinos_node_report_error (PinosNode *node, GError *error);
void pinos_node_report_idle (PinosNode *node);
void pinos_node_report_busy (PinosNode *node);
G_END_DECLS

666
pinos/server/port.c Normal file
View file

@ -0,0 +1,666 @@
/* 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/client/pinos.h"
#include "pinos/client/enumtypes.h"
#include "pinos/server/port.h"
#include "pinos/server/node.h"
#include "pinos/dbus/org-pinos.h"
#define PINOS_PORT_GET_PRIVATE(obj) \
(G_TYPE_INSTANCE_GET_PRIVATE ((obj), PINOS_TYPE_PORT, PinosPortPrivate))
struct _PinosPortPrivate
{
PinosNode *node;
PinosPort1 *iface;
gchar *object_path;
gchar *name;
PinosDirection direction;
GBytes *possible_formats;
PinosProperties *properties;
GList *channels;
};
G_DEFINE_TYPE (PinosPort, pinos_port, G_TYPE_OBJECT);
enum
{
PROP_0,
PROP_NODE,
PROP_OBJECT_PATH,
PROP_NAME,
PROP_DIRECTION,
PROP_POSSIBLE_FORMATS,
PROP_PROPERTIES
};
enum
{
SIGNAL_FORMAT_REQUEST,
SIGNAL_CHANNEL_ADDED,
SIGNAL_CHANNEL_REMOVED,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };
static void
pinos_port_get_property (GObject *_object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
PinosPort *port = PINOS_PORT (_object);
PinosPortPrivate *priv = port->priv;
switch (prop_id) {
case PROP_NODE:
g_value_set_object (value, priv->node);
break;
case PROP_NAME:
g_value_set_string (value, priv->name);
break;
case PROP_OBJECT_PATH:
g_value_set_string (value, priv->object_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;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (port, prop_id, pspec);
break;
}
}
static void
set_possible_formats (PinosPort *port, GBytes *formats)
{
PinosPortPrivate *priv;
GList *walk;
g_return_if_fail (PINOS_IS_PORT (port));
priv = port->priv;
if (priv->possible_formats)
g_bytes_unref (priv->possible_formats);
priv->possible_formats = formats ? g_bytes_ref (formats) : NULL;
if (priv->iface)
g_object_set (priv->iface, "possible-formats",
g_bytes_get_data (formats, NULL),
NULL);
for (walk = priv->channels; walk; walk = g_list_next (walk))
g_object_set (walk->data, "possible-formats", formats, NULL);
}
static void
pinos_port_set_property (GObject *_object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
PinosPort *port = PINOS_PORT (_object);
PinosPortPrivate *priv = port->priv;
switch (prop_id) {
case PROP_NODE:
priv->node = g_value_dup_object (value);
break;
case PROP_NAME:
priv->name = g_value_dup_string (value);
break;
case PROP_DIRECTION:
priv->direction = g_value_get_enum (value);
break;
case PROP_POSSIBLE_FORMATS:
set_possible_formats (port, g_value_get_boxed (value));
break;
case PROP_PROPERTIES:
if (priv->properties)
pinos_properties_free (priv->properties);
priv->properties = g_value_dup_boxed (value);
if (priv->iface)
g_object_set (priv->iface,
"properties", priv->properties ?
pinos_properties_to_variant (priv->properties) : NULL,
NULL);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (port, prop_id, pspec);
break;
}
}
static void
port_register_object (PinosPort *port)
{
PinosPortPrivate *priv = port->priv;
GBytes *formats;
GVariant *variant;
PinosObjectSkeleton *skel;
gchar *name;
name = g_strdup_printf ("%s/port", pinos_node_get_object_path (priv->node));
skel = pinos_object_skeleton_new (name);
g_free (name);
formats = pinos_port_get_formats (port, NULL, NULL);
if (priv->properties)
variant = pinos_properties_to_variant (priv->properties);
else
variant = NULL;
priv->iface = pinos_port1_skeleton_new ();
g_object_set (priv->iface, "name", priv->name,
"node", pinos_node_get_object_path (priv->node),
"direction", priv->direction,
"properties", variant,
"possible-formats", g_bytes_get_data (formats, NULL),
NULL);
g_bytes_unref (formats);
pinos_object_skeleton_set_port1 (skel, priv->iface);
g_free (priv->object_path);
priv->object_path = pinos_daemon_export_uniquely (pinos_node_get_daemon (priv->node),
G_DBUS_OBJECT_SKELETON (skel));
g_object_unref (skel);
pinos_node_add_port (priv->node, port);
return;
}
static void
port_unregister_object (PinosPort *port)
{
PinosPortPrivate *priv = port->priv;
pinos_node_remove_port (priv->node, port);
g_clear_object (&priv->iface);
}
static void
pinos_port_constructed (GObject * object)
{
PinosPort *port = PINOS_PORT (object);
port_register_object (port);
G_OBJECT_CLASS (pinos_port_parent_class)->constructed (object);
}
static void
do_remove_channel (PinosChannel *channel,
gpointer user_data)
{
pinos_channel_remove (channel);
}
static void
pinos_port_dispose (GObject * object)
{
PinosPort *port = PINOS_PORT (object);
PinosPortPrivate *priv = port->priv;
g_list_foreach (priv->channels, (GFunc) do_remove_channel, port);
port_unregister_object (port);
G_OBJECT_CLASS (pinos_port_parent_class)->dispose (object);
}
static void
pinos_port_finalize (GObject * object)
{
PinosPort *port = PINOS_PORT (object);
PinosPortPrivate *priv = port->priv;
g_free (priv->name);
if (priv->possible_formats)
g_bytes_unref (priv->possible_formats);
if (priv->properties)
pinos_properties_free (priv->properties);
G_OBJECT_CLASS (pinos_port_parent_class)->finalize (object);
}
static void
handle_remove_channel (PinosChannel *channel,
gpointer user_data)
{
PinosPort *port = user_data;
pinos_port_release_channel (port, channel);
}
static PinosChannel *
default_create_channel (PinosPort *port,
const gchar *client_path,
GBytes *format_filter,
PinosProperties *props,
GError **error)
{
PinosPortPrivate *priv = port->priv;
PinosChannel *channel;
GBytes *possible_formats;
possible_formats = pinos_port_get_formats (port, format_filter, error);
if (possible_formats == NULL)
return NULL;
channel = g_object_new (PINOS_TYPE_CHANNEL, "daemon", pinos_node_get_daemon (priv->node),
"client-path", client_path,
"direction", priv->direction,
"port-path", pinos_port_get_object_path (port),
"possible-formats", possible_formats,
"properties", props,
NULL);
g_bytes_unref (possible_formats);
if (channel == NULL)
goto no_channel;
g_signal_connect (channel,
"remove",
(GCallback) handle_remove_channel,
port);
priv->channels = g_list_prepend (priv->channels, channel);
return g_object_ref (channel);
/* ERRORS */
no_channel:
{
if (error)
*error = g_error_new (G_IO_ERROR,
G_IO_ERROR_FAILED,
"Could not create channel");
return NULL;
}
}
static gboolean
default_release_channel (PinosPort *port,
PinosChannel *channel)
{
PinosPortPrivate *priv = port->priv;
GList *find;
find = g_list_find (priv->channels, channel);
if (find == NULL)
return FALSE;
priv->channels = g_list_delete_link (priv->channels, find);
g_object_unref (channel);
return TRUE;
}
static void
pinos_port_class_init (PinosPortClass * klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
g_type_class_add_private (klass, sizeof (PinosPortPrivate));
gobject_class->constructed = pinos_port_constructed;
gobject_class->dispose = pinos_port_dispose;
gobject_class->finalize = pinos_port_finalize;
gobject_class->set_property = pinos_port_set_property;
gobject_class->get_property = pinos_port_get_property;
g_object_class_install_property (gobject_class,
PROP_NODE,
g_param_spec_object ("node",
"Node",
"The Node",
PINOS_TYPE_NODE,
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_NAME,
g_param_spec_string ("name",
"Name",
"The port name",
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 port",
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",
"The properties of the port",
PINOS_TYPE_PROPERTIES,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT |
G_PARAM_STATIC_STRINGS));
signals[SIGNAL_FORMAT_REQUEST] = g_signal_new ("format-request",
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_CHANNEL_ADDED] = g_signal_new ("channel-added",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
0,
NULL,
NULL,
g_cclosure_marshal_generic,
G_TYPE_NONE,
1,
PINOS_TYPE_CHANNEL);
signals[SIGNAL_CHANNEL_REMOVED] = g_signal_new ("channel-removed",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
0,
NULL,
NULL,
g_cclosure_marshal_generic,
G_TYPE_NONE,
1,
PINOS_TYPE_CHANNEL);
klass->create_channel = default_create_channel;
klass->release_channel = default_release_channel;
}
static void
pinos_port_init (PinosPort * port)
{
PinosPortPrivate *priv = port->priv = PINOS_PORT_GET_PRIVATE (port);
priv->direction = PINOS_DIRECTION_INVALID;
}
/**
* pinos_port_new:
*
*
* Returns: a new #PinosPort
*/
PinosPort *
pinos_port_new (PinosNode *node,
PinosDirection direction,
const gchar *name,
GBytes *possible_formats,
PinosProperties *props)
{
g_return_val_if_fail (PINOS_IS_NODE (node), NULL);
g_return_val_if_fail (name != NULL, NULL);
return g_object_new (PINOS_TYPE_PORT,
"node", node,
"direction", direction,
"name", name,
"possible-formats", possible_formats,
"properties", props,
NULL);
}
const gchar *
pinos_port_get_object_path (PinosPort *port)
{
PinosPortPrivate *priv;
g_return_val_if_fail (PINOS_IS_PORT (port), NULL);
priv = port->priv;
return priv->object_path;
}
/**
* pinos_port_get_formats:
* @port: a #PinosPort
* @filter: a #GBytes
* @error: a #GError or %NULL
*
* Get all the currently supported formats for @port and filter the
* results with @filter.
*
* Returns: the list of supported format. If %NULL is returned, @error will
* be set.
*/
GBytes *
pinos_port_get_formats (PinosPort *port,
GBytes *filter,
GError **error)
{
GstCaps *tmp, *caps, *cfilter;
gchar *str;
PinosPortPrivate *priv;
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);
g_clear_pointer (&cfilter, gst_caps_unref);
gst_caps_take (&caps, tmp);
}
if (caps == NULL || gst_caps_is_empty (caps))
goto no_format;
str = gst_caps_to_string (caps);
gst_caps_unref (caps);
return g_bytes_new_take (str, strlen (str) + 1);
invalid_filter:
{
if (error)
*error = g_error_new (G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
"Invalid filter received");
return NULL;
}
no_format:
{
if (error)
*error = g_error_new (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;
}
}
/**
* pinos_port_get_channels:
* @port: a #PinosPort
*
* Get all the channels in @port.
*
* Returns: a #GList of #PinosChannel objects.
*/
GList *
pinos_port_get_channels (PinosPort *port)
{
PinosPortPrivate *priv;
g_return_val_if_fail (PINOS_IS_PORT (port), NULL);
priv = port->priv;
return priv->channels;
}
/**
* 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)
{
PinosPortClass *klass;
PinosChannel *channel;
g_return_val_if_fail (PINOS_IS_PORT (port), NULL);
klass = PINOS_PORT_GET_CLASS (port);
if (klass->create_channel) {
channel = klass->create_channel (port, client_path, format_filter, props, error);
if (channel)
g_signal_emit (port, signals[SIGNAL_CHANNEL_ADDED], 0, channel);
} else {
if (error) {
*error = g_error_new (G_IO_ERROR,
G_IO_ERROR_NOT_SUPPORTED,
"CreateChannel not implemented");
}
channel = NULL;
}
return channel;
}
/**
* pinos_port_release_channel:
* @port: a #PinosPort
* @channel: a #PinosChannel
*
* Release the @channel in @port.
*
* Returns: %TRUE on success.
*/
gboolean
pinos_port_release_channel (PinosPort *port,
PinosChannel *channel)
{
PinosPortClass *klass;
gboolean res;
g_return_val_if_fail (PINOS_IS_PORT (port), FALSE);
g_return_val_if_fail (PINOS_IS_CHANNEL (channel), FALSE);
klass = PINOS_PORT_GET_CLASS (port);
if (klass->release_channel) {
g_signal_emit (port, signals[SIGNAL_CHANNEL_REMOVED], 0, channel);
res = klass->release_channel (port, channel);
}
else
res = FALSE;
return res;
}

101
pinos/server/port.h Normal file
View file

@ -0,0 +1,101 @@
/* 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_PORT_H__
#define __PINOS_PORT_H__
#include <glib-object.h>
G_BEGIN_DECLS
typedef struct _PinosPort PinosPort;
typedef struct _PinosPortClass PinosPortClass;
typedef struct _PinosPortPrivate PinosPortPrivate;
#include <pinos/client/introspect.h>
#include <pinos/server/node.h>
#include <pinos/server/channel.h>
#define PINOS_TYPE_PORT (pinos_port_get_type ())
#define PINOS_IS_PORT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PINOS_TYPE_PORT))
#define PINOS_IS_PORT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PINOS_TYPE_PORT))
#define PINOS_PORT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), PINOS_TYPE_PORT, PinosPortClass))
#define PINOS_PORT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), PINOS_TYPE_PORT, PinosPort))
#define PINOS_PORT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PINOS_TYPE_PORT, PinosPortClass))
#define PINOS_PORT_CAST(obj) ((PinosPort*)(obj))
#define PINOS_PORT_CLASS_CAST(klass) ((PinosPortClass*)(klass))
/**
* PinosPort:
*
* Pinos port object class.
*/
struct _PinosPort {
GObject object;
PinosPortPrivate *priv;
};
/**
* PinosPortClass:
* @get_formats: called to get a list of supported formats from the port
* @create_channel: called to create a new channel object
* @release_channel: called to release a channel object
*
* Pinos port object class.
*/
struct _PinosPortClass {
GObjectClass parent_class;
PinosChannel * (*create_channel) (PinosPort *port,
const gchar *client_path,
GBytes *format_filter,
PinosProperties *props,
GError **error);
gboolean (*release_channel) (PinosPort *port,
PinosChannel *channel);
};
/* normal GObject stuff */
GType pinos_port_get_type (void);
PinosPort * pinos_port_new (PinosNode *node,
PinosDirection direction,
const gchar *name,
GBytes *possible_formats,
PinosProperties *props);
const gchar * pinos_port_get_object_path (PinosPort *port);
GBytes * pinos_port_get_formats (PinosPort *port,
GBytes *filter,
GError **error);
PinosChannel * pinos_port_create_channel (PinosPort *port,
const gchar *client_path,
GBytes *format_filter,
PinosProperties *props,
GError **error);
gboolean pinos_port_release_channel (PinosPort *port,
PinosChannel *channel);
GList * pinos_port_get_channels (PinosPort *port);
G_END_DECLS
#endif /* __PINOS_PORT_H__ */

View file

@ -1,646 +0,0 @@
/* 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 <gio/gio.h>
#include "pinos/client/pinos.h"
#include "pinos/client/enumtypes.h"
#include "pinos/server/sink.h"
#include "pinos/server/node.h"
#include "pinos/dbus/org-pinos.h"
#define PINOS_SINK_GET_PRIVATE(obj) \
(G_TYPE_INSTANCE_GET_PRIVATE ((obj), PINOS_TYPE_SINK, PinosSinkPrivate))
struct _PinosSinkPrivate
{
PinosNode *node;
PinosSink1 *iface;
gchar *name;
PinosProperties *properties;
PinosSinkState state;
GError *error;
guint idle_timeout;
GList *channels;
};
G_DEFINE_ABSTRACT_TYPE (PinosSink, pinos_sink, G_TYPE_OBJECT);
enum
{
PROP_0,
PROP_NODE,
PROP_NAME,
PROP_STATE,
PROP_PROPERTIES
};
static void
pinos_sink_get_property (GObject *_object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
PinosSink *sink = PINOS_SINK (_object);
PinosSinkPrivate *priv = sink->priv;
switch (prop_id) {
case PROP_NODE:
g_value_set_object (value, priv->node);
break;
case PROP_NAME:
g_value_set_string (value, priv->name);
break;
case PROP_STATE:
g_value_set_enum (value, priv->state);
break;
case PROP_PROPERTIES:
g_value_set_boxed (value, priv->properties);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (sink, prop_id, pspec);
break;
}
}
static void
pinos_sink_set_property (GObject *_object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
PinosSink *sink = PINOS_SINK (_object);
PinosSinkPrivate *priv = sink->priv;
switch (prop_id) {
case PROP_NODE:
priv->node = g_value_dup_object (value);
break;
case PROP_NAME:
g_free (priv->name);
priv->name = g_value_dup_string (value);
break;
case PROP_PROPERTIES:
if (priv->properties)
pinos_properties_free (priv->properties);
priv->properties = g_value_dup_boxed (value);
if (priv->iface)
g_object_set (priv->iface,
"properties", priv->properties ?
pinos_properties_to_variant (priv->properties) : NULL,
NULL);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (sink, prop_id, pspec);
break;
}
}
static void
sink_register_object (PinosSink *sink)
{
PinosSinkPrivate *priv = sink->priv;
GBytes *formats;
GVariant *variant;
formats = pinos_sink_get_formats (sink, NULL, NULL);
if (priv->properties)
variant = pinos_properties_to_variant (priv->properties);
else
variant = NULL;
priv->iface = pinos_sink1_skeleton_new ();
g_object_set (priv->iface, "name", priv->name,
"state", priv->state,
"properties", variant,
"possible-formats", g_bytes_get_data (formats, NULL),
NULL);
g_bytes_unref (formats);
pinos_node_set_sink (priv->node, sink, G_OBJECT (priv->iface));
return;
}
static void
sink_unregister_object (PinosSink *sink)
{
PinosSinkPrivate *priv = sink->priv;
pinos_node_set_sink (priv->node, NULL, NULL);
g_clear_object (&priv->iface);
}
static void
pinos_sink_constructed (GObject * object)
{
PinosSink *sink = PINOS_SINK (object);
sink_register_object (sink);
G_OBJECT_CLASS (pinos_sink_parent_class)->constructed (object);
}
static void
do_remove_channel (PinosChannel *channel,
gpointer user_data)
{
pinos_channel_remove (channel);
}
static void
pinos_sink_dispose (GObject * object)
{
PinosSink *sink = PINOS_SINK (object);
PinosSinkPrivate *priv = sink->priv;
g_list_foreach (priv->channels, (GFunc) do_remove_channel, sink);
sink_unregister_object (sink);
G_OBJECT_CLASS (pinos_sink_parent_class)->dispose (object);
}
static void
pinos_sink_finalize (GObject * object)
{
PinosSink *sink = PINOS_SINK (object);
PinosSinkPrivate *priv = sink->priv;
g_free (priv->name);
if (priv->properties)
pinos_properties_free (priv->properties);
G_OBJECT_CLASS (pinos_sink_parent_class)->finalize (object);
}
static gboolean
default_set_state (PinosSink *sink,
PinosSinkState state)
{
pinos_sink_update_state (sink, state);
return TRUE;
}
static void
handle_remove_channel (PinosChannel *channel,
gpointer user_data)
{
PinosSink *sink = user_data;
pinos_sink_release_channel (sink, channel);
}
static PinosChannel *
default_create_channel (PinosSink *sink,
const gchar *client_path,
GBytes *format_filter,
PinosProperties *props,
const gchar *prefix,
GError **error)
{
PinosSinkPrivate *priv = sink->priv;
PinosChannel *channel;
GBytes *possible_formats;
possible_formats = pinos_sink_get_formats (sink, format_filter, error);
if (possible_formats == NULL)
return NULL;
channel = g_object_new (PINOS_TYPE_CHANNEL, "daemon", pinos_node_get_daemon (priv->node),
"object-path", prefix,
"client-path", client_path,
"owner-path", pinos_node_get_object_path (priv->node),
"possible-formats", possible_formats,
"properties", props,
NULL);
g_bytes_unref (possible_formats);
if (channel == NULL)
goto no_channel;
g_signal_connect (channel,
"remove",
(GCallback) handle_remove_channel,
sink);
priv->channels = g_list_prepend (priv->channels, channel);
return g_object_ref (channel);
/* ERRORS */
no_channel:
{
if (error)
*error = g_error_new (G_IO_ERROR,
G_IO_ERROR_FAILED,
"Could not create channel");
return NULL;
}
}
static gboolean
default_release_channel (PinosSink *sink,
PinosChannel *channel)
{
PinosSinkPrivate *priv = sink->priv;
GList *find;
find = g_list_find (priv->channels, channel);
if (find == NULL)
return FALSE;
priv->channels = g_list_delete_link (priv->channels, find);
g_object_unref (channel);
return TRUE;
}
static void
pinos_sink_class_init (PinosSinkClass * klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
g_type_class_add_private (klass, sizeof (PinosSinkPrivate));
gobject_class->constructed = pinos_sink_constructed;
gobject_class->dispose = pinos_sink_dispose;
gobject_class->finalize = pinos_sink_finalize;
gobject_class->set_property = pinos_sink_set_property;
gobject_class->get_property = pinos_sink_get_property;
g_object_class_install_property (gobject_class,
PROP_NODE,
g_param_spec_object ("node",
"Node",
"The Node",
PINOS_TYPE_NODE,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_NAME,
g_param_spec_string ("name",
"Name",
"The sink name",
NULL,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_STATE,
g_param_spec_enum ("state",
"State",
"The state of the sink",
PINOS_TYPE_SINK_STATE,
PINOS_SINK_STATE_SUSPENDED,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_PROPERTIES,
g_param_spec_boxed ("properties",
"Properties",
"The properties of the sink",
PINOS_TYPE_PROPERTIES,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT |
G_PARAM_STATIC_STRINGS));
klass->set_state = default_set_state;
klass->create_channel = default_create_channel;
klass->release_channel = default_release_channel;
}
static void
pinos_sink_init (PinosSink * sink)
{
PinosSinkPrivate *priv = sink->priv = PINOS_SINK_GET_PRIVATE (sink);
priv->state = PINOS_SINK_STATE_SUSPENDED;
}
/**
* pinos_sink_get_formats:
* @sink: a #PinosSink
* @filter: a #GBytes
* @error: a #GError or %NULL
*
* Get all the currently supported formats for @sink and filter the
* results with @filter.
*
* Returns: the list of supported format. If %NULL is returned, @error will
* be set.
*/
GBytes *
pinos_sink_get_formats (PinosSink *sink,
GBytes *filter,
GError **error)
{
PinosSinkClass *klass;
GBytes *res;
g_return_val_if_fail (PINOS_IS_SINK (sink), NULL);
klass = PINOS_SINK_GET_CLASS (sink);
if (klass->get_formats)
res = klass->get_formats (sink, filter, error);
else {
res = NULL;
if (error)
*error = g_error_new (G_IO_ERROR,
G_IO_ERROR_NOT_SUPPORTED,
"Format query is not supported");
}
return res;
}
static void
remove_idle_timeout (PinosSink *sink)
{
PinosSinkPrivate *priv = sink->priv;
if (priv->idle_timeout) {
g_source_remove (priv->idle_timeout);
priv->idle_timeout = 0;
}
}
/**
* pinos_sink_set_state:
* @sink: a #PinosSink
* @state: a #PinosSinkState
*
* Set the state of @sink to @state.
*
* Returns: %TRUE on success.
*/
gboolean
pinos_sink_set_state (PinosSink *sink,
PinosSinkState state)
{
PinosSinkClass *klass;
gboolean res;
g_return_val_if_fail (PINOS_IS_SINK (sink), FALSE);
klass = PINOS_SINK_GET_CLASS (sink);
remove_idle_timeout (sink);
if (klass->set_state)
res = klass->set_state (sink, state);
else
res = FALSE;
return res;
}
/**
* pinos_sink_update_state:
* @sink: a #PinosSink
* @state: a #PinosSinkState
*
* Update the state of a sink. This method is used from
* inside @sink itself.
*/
void
pinos_sink_update_state (PinosSink *sink,
PinosSinkState state)
{
PinosSinkPrivate *priv;
g_return_if_fail (PINOS_IS_SINK (sink));
priv = sink->priv;
if (priv->state != state) {
priv->state = state;
pinos_sink1_set_state (priv->iface, state);
g_object_notify (G_OBJECT (sink), "state");
}
}
/**
* pinos_sink_report_error:
* @sink: a #PinosSink
* @error: a #GError
*
* Report an error from within @sink.
*/
void
pinos_sink_report_error (PinosSink *sink,
GError *error)
{
PinosSinkPrivate *priv;
g_return_if_fail (PINOS_IS_SINK (sink));
priv = sink->priv;
g_clear_error (&priv->error);
remove_idle_timeout (sink);
priv->error = error;
priv->state = PINOS_SINK_STATE_ERROR;
g_debug ("got error state %s", error->message);
pinos_sink1_set_state (priv->iface, priv->state);
g_object_notify (G_OBJECT (sink), "state");
}
static gboolean
idle_timeout (PinosSink *sink)
{
PinosSinkPrivate *priv = sink->priv;
priv->idle_timeout = 0;
pinos_sink_set_state (sink, PINOS_SINK_STATE_SUSPENDED);
return G_SOURCE_REMOVE;
}
/**
* pinos_sink_report_idle:
* @sink: a #PinosSink
*
* Mark @sink as being idle. This will start a timeout that will
* set the sink to SUSPENDED.
*/
void
pinos_sink_report_idle (PinosSink *sink)
{
PinosSinkPrivate *priv;
g_return_if_fail (PINOS_IS_SINK (sink));
priv = sink->priv;
pinos_sink_set_state (sink, PINOS_SINK_STATE_IDLE);
priv->idle_timeout = g_timeout_add_seconds (3,
(GSourceFunc) idle_timeout,
sink);
}
/**
* pinos_sink_report_busy:
* @sink: a #PinosSink
*
* Mark @sink as being busy. This will set the state of the sink
* to the RUNNING state.
*/
void
pinos_sink_report_busy (PinosSink *sink)
{
g_return_if_fail (PINOS_IS_SINK (sink));
pinos_sink_set_state (sink, PINOS_SINK_STATE_RUNNING);
}
/**
* pinos_sink_update_possible_formats:
* @sink: a #PinosSink
* @formats: a #GBytes
*
* Update the possible formats in @sink to @formats. This function also
* updates the possible formats of the channels.
*/
void
pinos_sink_update_possible_formats (PinosSink *sink, GBytes *formats)
{
PinosSinkPrivate *priv;
GList *walk;
g_return_if_fail (PINOS_IS_SINK (sink));
priv = sink->priv;
if (priv->iface)
g_object_set (priv->iface, "possible-formats",
g_bytes_get_data (formats, NULL),
NULL);
for (walk = priv->channels; walk; walk = g_list_next (walk))
g_object_set (walk->data, "possible-formats", formats, NULL);
}
/**
* pinos_sink_update_format:
* @sink: a #PinosSink
* @format: a #GBytes
*
* Update the current format in @sink to @format. This function also
* updates the current format of the channels.
*/
void
pinos_sink_update_format (PinosSink *sink, GBytes *format)
{
PinosSinkPrivate *priv;
GList *walk;
g_return_if_fail (PINOS_IS_SINK (sink));
priv = sink->priv;
for (walk = priv->channels; walk; walk = g_list_next (walk))
g_object_set (walk->data, "format", format, NULL);
}
/**
* pinos_sink_create_channel:
* @sink: a #PinosSink
* @client_path: the client path
* @format_filter: a #GBytes
* @props: #PinosProperties
* @prefix: a prefix
* @error: a #GError or %NULL
*
* Create a new #PinosChannel for @sink.
*
* Returns: a new #PinosChannel or %NULL, in wich case @error will contain
* more information about the error.
*/
PinosChannel *
pinos_sink_create_channel (PinosSink *sink,
const gchar *client_path,
GBytes *format_filter,
PinosProperties *props,
const gchar *prefix,
GError **error)
{
PinosSinkClass *klass;
PinosChannel *res;
g_return_val_if_fail (PINOS_IS_SINK (sink), NULL);
klass = PINOS_SINK_GET_CLASS (sink);
if (klass->create_channel) {
res = klass->create_channel (sink, client_path, format_filter, props, prefix, error);
} else {
if (error) {
*error = g_error_new (G_IO_ERROR,
G_IO_ERROR_NOT_SUPPORTED,
"CreateChannel not implemented");
}
res = NULL;
}
return res;
}
/**
* pinos_sink_release_channel:
* @sink: a #PinosSink
* @channel: a #PinosChannel
*
* Release the @channel in @sink.
*
* Returns: %TRUE on success.
*/
gboolean
pinos_sink_release_channel (PinosSink *sink,
PinosChannel *channel)
{
PinosSinkClass *klass;
gboolean res;
g_return_val_if_fail (PINOS_IS_SINK (sink), FALSE);
g_return_val_if_fail (PINOS_IS_CHANNEL (channel), FALSE);
klass = PINOS_SINK_GET_CLASS (sink);
if (klass->release_channel)
res = klass->release_channel (sink, channel);
else
res = FALSE;
return res;
}

View file

@ -1,109 +0,0 @@
/* 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_SINK_H__
#define __PINOS_SINK_H__
#include <glib-object.h>
G_BEGIN_DECLS
typedef struct _PinosSink PinosSink;
typedef struct _PinosSinkClass PinosSinkClass;
typedef struct _PinosSinkPrivate PinosSinkPrivate;
#include <pinos/client/introspect.h>
#include <pinos/server/channel.h>
#define PINOS_TYPE_SINK (pinos_sink_get_type ())
#define PINOS_IS_SINK(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PINOS_TYPE_SINK))
#define PINOS_IS_SINK_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PINOS_TYPE_SINK))
#define PINOS_SINK_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), PINOS_TYPE_SINK, PinosSinkClass))
#define PINOS_SINK(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), PINOS_TYPE_SINK, PinosSink))
#define PINOS_SINK_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PINOS_TYPE_SINK, PinosSinkClass))
#define PINOS_SINK_CAST(obj) ((PinosSink*)(obj))
#define PINOS_SINK_CLASS_CAST(klass) ((PinosSinkClass*)(klass))
/**
* PinosSink:
*
* Pinos sink object class.
*/
struct _PinosSink {
GObject object;
PinosSinkPrivate *priv;
};
/**
* PinosSinkClass:
* @get_formats: called to get a list of supported formats from the sink
* @set_state: called to change the current state of the sink
* @create_channel: called to create a new channel object
* @release_channel: called to release a channel object
*
* Pinos sink object class.
*/
struct _PinosSinkClass {
GObjectClass parent_class;
GBytes * (*get_formats) (PinosSink *sink,
GBytes *filter,
GError **error);
gboolean (*set_state) (PinosSink *sink, PinosSinkState);
PinosChannel * (*create_channel) (PinosSink *sink,
const gchar *client_path,
GBytes *format_filter,
PinosProperties *props,
const gchar *prefix,
GError **error);
gboolean (*release_channel) (PinosSink *sink,
PinosChannel *channel);
};
/* normal GObject stuff */
GType pinos_sink_get_type (void);
GBytes * pinos_sink_get_formats (PinosSink *sink,
GBytes *filter,
GError **error);
gboolean pinos_sink_set_state (PinosSink *sink, PinosSinkState state);
void pinos_sink_update_state (PinosSink *sink, PinosSinkState state);
void pinos_sink_report_error (PinosSink *sink, GError *error);
void pinos_sink_report_idle (PinosSink *sink);
void pinos_sink_report_busy (PinosSink *sink);
void pinos_sink_update_possible_formats (PinosSink *sink, GBytes *formats);
void pinos_sink_update_format (PinosSink *sink, GBytes *format);
PinosChannel * pinos_sink_create_channel (PinosSink *sink,
const gchar *client_path,
GBytes *format_filter,
PinosProperties *props,
const gchar *prefix,
GError **error);
gboolean pinos_sink_release_channel (PinosSink *sink,
PinosChannel *channel);
G_END_DECLS
#endif /* __PINOS_SINK_H__ */

View file

@ -22,12 +22,12 @@
#include <gio/gio.h>
#include <pinos/server/daemon.h>
#include <pinos/server/client-source.h>
#include <pinos/server/upload-node.h>
#define PINOS_CLIENT_SOURCE_GET_PRIVATE(obj) \
(G_TYPE_INSTANCE_GET_PRIVATE ((obj), PINOS_TYPE_CLIENT_SOURCE, PinosClientSourcePrivate))
#define PINOS_UPLOAD_NODE_GET_PRIVATE(obj) \
(G_TYPE_INSTANCE_GET_PRIVATE ((obj), PINOS_TYPE_UPLOAD_NODE, PinosUploadNodePrivate))
struct _PinosClientSourcePrivate
struct _PinosUploadNodePrivate
{
GstElement *pipeline;
GstElement *src;
@ -37,10 +37,12 @@ struct _PinosClientSourcePrivate
GstCaps *format;
GBytes *possible_formats;
PinosPort *input, *output;
PinosChannel *channel;
};
G_DEFINE_TYPE (PinosClientSource, pinos_client_source, PINOS_TYPE_SOURCE);
G_DEFINE_TYPE (PinosUploadNode, pinos_upload_node, PINOS_TYPE_NODE);
enum
{
@ -49,13 +51,13 @@ enum
};
static void
client_source_get_property (GObject *_object,
upload_node_get_property (GObject *_object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
PinosClientSource *source = PINOS_CLIENT_SOURCE (_object);
PinosClientSourcePrivate *priv = source->priv;
PinosUploadNode *node = PINOS_UPLOAD_NODE (_object);
PinosUploadNodePrivate *priv = node->priv;
switch (prop_id) {
case PROP_POSSIBLE_FORMATS:
@ -63,43 +65,47 @@ client_source_get_property (GObject *_object,
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (source, prop_id, pspec);
G_OBJECT_WARN_INVALID_PROPERTY_ID (node, prop_id, pspec);
break;
}
}
static void
client_source_set_property (GObject *_object,
upload_node_set_property (GObject *_object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
PinosClientSource *source = PINOS_CLIENT_SOURCE (_object);
PinosClientSourcePrivate *priv = source->priv;
PinosUploadNode *node = PINOS_UPLOAD_NODE (_object);
PinosUploadNodePrivate *priv = node->priv;
switch (prop_id) {
case PROP_POSSIBLE_FORMATS:
if (priv->possible_formats)
g_bytes_unref (priv->possible_formats);
priv->possible_formats = g_value_dup_boxed (value);
pinos_source_update_possible_formats (PINOS_SOURCE (source),
priv->possible_formats);
g_object_set (priv->output, "possible-formats", priv->possible_formats, NULL);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (source, prop_id, pspec);
G_OBJECT_WARN_INVALID_PROPERTY_ID (node, prop_id, pspec);
break;
}
}
static void
update_channel_format (PinosChannel *channel, GBytes *format)
{
g_object_set (channel, "format", format, NULL);
}
static gboolean
bus_handler (GstBus *bus,
GstMessage *message,
gpointer user_data)
{
PinosSource *source = user_data;
PinosClientSourcePrivate *priv = PINOS_CLIENT_SOURCE (source)->priv;
PinosNode *node = user_data;
PinosUploadNodePrivate *priv = PINOS_UPLOAD_NODE (node)->priv;
switch (GST_MESSAGE_TYPE (message)) {
case GST_MESSAGE_ERROR:
@ -111,7 +117,7 @@ bus_handler (GstBus *bus,
g_warning ("got error %s (%s)\n", error->message, debug);
g_free (debug);
pinos_source_report_error (source, error);
pinos_node_report_error (node, error);
gst_element_set_state (priv->pipeline, GST_STATE_NULL);
break;
}
@ -128,9 +134,8 @@ bus_handler (GstBus *bus,
caps_str = gst_caps_to_string (caps);
format = g_bytes_new_take (caps_str, strlen (caps_str) + 1);
g_object_set (priv->channel, "possible-formats", format, "format", format, NULL);
pinos_source_update_possible_formats (source, format);
pinos_source_update_format (source, format);
g_list_foreach (pinos_port_get_channels (priv->output), (GFunc) update_channel_format, format);
g_list_foreach (pinos_port_get_channels (priv->input), (GFunc) update_channel_format, format);
g_bytes_unref (format);
}
break;
@ -142,9 +147,9 @@ bus_handler (GstBus *bus,
}
static void
setup_pipeline (PinosClientSource *source)
setup_pipeline (PinosUploadNode *node)
{
PinosClientSourcePrivate *priv = source->priv;
PinosUploadNodePrivate *priv = node->priv;
GstBus *bus;
priv->pipeline = gst_parse_launch ("socketsrc "
@ -159,116 +164,56 @@ setup_pipeline (PinosClientSource *source)
priv->src = gst_bin_get_by_name (GST_BIN (priv->pipeline), "src");
bus = gst_pipeline_get_bus (GST_PIPELINE (priv->pipeline));
priv->id = gst_bus_add_watch (bus, bus_handler, source);
priv->id = gst_bus_add_watch (bus, bus_handler, node);
gst_object_unref (bus);
g_debug ("client-source %p: setup pipeline", source);
}
static GstCaps *
collect_caps (PinosSource *source,
GstCaps *filter)
{
PinosClientSourcePrivate *priv = PINOS_CLIENT_SOURCE (source)->priv;
if (priv->format)
return gst_caps_ref (priv->format);
else
return gst_caps_new_any ();
g_debug ("upload-node %p: setup pipeline", node);
}
static gboolean
client_set_state (PinosSource *source,
PinosSourceState state)
node_set_state (PinosNode *node,
PinosNodeState state)
{
PinosClientSourcePrivate *priv = PINOS_CLIENT_SOURCE (source)->priv;
PinosUploadNodePrivate *priv = PINOS_UPLOAD_NODE (node)->priv;
switch (state) {
case PINOS_SOURCE_STATE_SUSPENDED:
case PINOS_NODE_STATE_SUSPENDED:
gst_element_set_state (priv->pipeline, GST_STATE_NULL);
break;
case PINOS_SOURCE_STATE_INITIALIZING:
case PINOS_NODE_STATE_INITIALIZING:
gst_element_set_state (priv->pipeline, GST_STATE_READY);
break;
case PINOS_SOURCE_STATE_IDLE:
case PINOS_NODE_STATE_IDLE:
gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
break;
case PINOS_SOURCE_STATE_RUNNING:
case PINOS_NODE_STATE_RUNNING:
gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
break;
case PINOS_SOURCE_STATE_ERROR:
case PINOS_NODE_STATE_ERROR:
break;
}
pinos_source_update_state (source, state);
pinos_node_update_state (node, state);
return TRUE;
}
static GBytes *
client_get_formats (PinosSource *source,
GBytes *filter,
GError **error)
{
GstCaps *caps, *cfilter;
gchar *str;
if (filter) {
cfilter = gst_caps_from_string (g_bytes_get_data (filter, NULL));
if (cfilter == NULL)
goto invalid_filter;
} else {
cfilter = NULL;
}
caps = collect_caps (source, cfilter);
if (caps == NULL)
goto no_format;
str = gst_caps_to_string (caps);
gst_caps_unref (caps);
if (cfilter)
gst_caps_unref (cfilter);
return g_bytes_new_take (str, strlen (str) + 1);
invalid_filter:
{
if (error)
*error = g_error_new (G_IO_ERROR,
G_IO_ERROR_INVALID_ARGUMENT,
"Invalid filter received");
return NULL;
}
no_format:
{
if (error)
*error = g_error_new (G_IO_ERROR,
G_IO_ERROR_NOT_FOUND,
"No compatible format found");
if (cfilter)
gst_caps_unref (cfilter);
return NULL;
}
}
static void
on_socket_notify (GObject *gobject,
GParamSpec *pspec,
gpointer user_data)
{
PinosClientSource *source = user_data;
PinosClientSourcePrivate *priv = source->priv;
PinosUploadNode *node = user_data;
PinosUploadNodePrivate *priv = node->priv;
GSocket *socket;
guint num_handles;
g_object_get (gobject, "socket", &socket, NULL);
g_debug ("client-source %p: output socket notify %p", source, socket);
g_debug ("upload-node %p: output socket notify %p", node, socket);
if (socket == NULL) {
GSocket *prev_socket = g_object_steal_data (gobject, "last-socket");
@ -292,66 +237,39 @@ on_socket_notify (GObject *gobject,
}
}
static PinosChannel *
client_create_channel (PinosSource *source,
const gchar *client_path,
GBytes *format_filter,
PinosProperties *props,
const gchar *prefix,
GError **error)
static void
on_channel_added (PinosPort *port, PinosChannel *channel, PinosNode *node)
{
PinosClientSourcePrivate *priv = PINOS_CLIENT_SOURCE (source)->priv;
PinosChannel *channel;
g_signal_connect (channel, "notify::socket", (GCallback) on_socket_notify, node);
/* propose format of input */
g_object_get (priv->channel, "format", &format_filter, NULL);
channel = PINOS_SOURCE_CLASS (pinos_client_source_parent_class)
->create_channel (source,
client_path,
format_filter,
props,
prefix,
error);
g_bytes_unref (format_filter);
if (channel == NULL)
return NULL;
g_debug ("client-source %p: create channel %p", source, channel);
g_signal_connect (channel, "notify::socket", (GCallback) on_socket_notify, source);
return channel;
}
static gboolean
client_release_channel (PinosSource *source,
PinosChannel *channel)
{
g_debug ("client-source %p: release channel %p", source, channel);
return PINOS_SOURCE_CLASS (pinos_client_source_parent_class)->release_channel (source, channel);
g_debug ("upload-node %p: create channel %p", node, channel);
}
static void
client_source_dispose (GObject * object)
on_channel_removed (PinosPort *port, PinosChannel *channel, PinosNode *node)
{
PinosClientSourcePrivate *priv = PINOS_CLIENT_SOURCE (object)->priv;
g_debug ("upload-node %p: release channel %p", node, channel);
}
g_debug ("client-source %p: dispose", object);
static void
upload_node_dispose (GObject * object)
{
PinosUploadNodePrivate *priv = PINOS_UPLOAD_NODE (object)->priv;
g_debug ("upload-node %p: dispose", object);
g_source_remove (priv->id);
gst_element_set_state (priv->pipeline, GST_STATE_NULL);
G_OBJECT_CLASS (pinos_client_source_parent_class)->dispose (object);
G_OBJECT_CLASS (pinos_upload_node_parent_class)->dispose (object);
}
static void
client_source_finalize (GObject * object)
upload_node_finalize (GObject * object)
{
PinosClientSourcePrivate *priv = PINOS_CLIENT_SOURCE (object)->priv;
PinosUploadNodePrivate *priv = PINOS_UPLOAD_NODE (object)->priv;
g_debug ("client-source %p: finalize", object);
g_debug ("upload-node %p: finalize", object);
g_clear_object (&priv->channel);
g_clear_object (&priv->sink);
@ -362,7 +280,7 @@ client_source_finalize (GObject * object)
g_bytes_unref (priv->possible_formats);
gst_caps_replace (&priv->format, NULL);
G_OBJECT_CLASS (pinos_client_source_parent_class)->finalize (object);
G_OBJECT_CLASS (pinos_upload_node_parent_class)->finalize (object);
}
@ -371,14 +289,14 @@ on_input_socket_notify (GObject *gobject,
GParamSpec *pspec,
gpointer user_data)
{
PinosClientSource *source = user_data;
PinosClientSourcePrivate *priv = source->priv;
PinosUploadNode *node = user_data;
PinosUploadNodePrivate *priv = node->priv;
GSocket *socket;
GBytes *requested_format;
GstCaps *caps;
g_object_get (gobject, "socket", &socket, NULL);
g_debug ("client-source %p: input socket notify %p", source, socket);
g_debug ("upload-node %p: input socket notify %p", node, socket);
if (socket) {
/* requested format is final format */
@ -397,11 +315,11 @@ on_input_socket_notify (GObject *gobject,
g_object_set (priv->src, "socket", socket, NULL);
if (socket) {
g_debug ("client-source %p: set pipeline to PLAYING", source);
g_debug ("upload-node %p: set pipeline to PLAYING", node);
gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
g_object_unref (socket);
} else {
g_debug ("client-source %p: set pipeline to READY", source);
g_debug ("upload-node %p: set pipeline to READY", node);
gst_element_set_state (priv->pipeline, GST_STATE_READY);
}
}
@ -410,16 +328,16 @@ static void
handle_remove_channel (PinosChannel *channel,
gpointer user_data)
{
PinosClientSource *source = user_data;
PinosClientSourcePrivate *priv = source->priv;
PinosUploadNode *node = user_data;
PinosUploadNodePrivate *priv = node->priv;
g_debug ("client-source %p: remove channel %p", source, priv->channel);
g_debug ("upload-node %p: remove channel %p", node, priv->channel);
g_clear_pointer (&priv->channel, g_object_unref);
}
/**
* pinos_client_source_get_channel:
* @source: a #PinosClientSource
* pinos_upload_node_get_channel:
* @node: a #PinosUploadNode
* @client_path: the client path
* @format_filter: a #GBytes
* @props: extra properties
@ -432,57 +350,54 @@ handle_remove_channel (PinosChannel *channel,
* Returns: a new #PinosChannel.
*/
PinosChannel *
pinos_client_source_get_channel (PinosClientSource *source,
const gchar *client_path,
GBytes *format_filter,
PinosProperties *props,
const gchar *prefix,
GError **error)
pinos_upload_node_get_channel (PinosUploadNode *node,
const gchar *client_path,
GBytes *format_filter,
PinosProperties *props,
GError **error)
{
PinosClientSourcePrivate *priv;
PinosUploadNodePrivate *priv;
g_return_val_if_fail (PINOS_IS_CLIENT_SOURCE (source), NULL);
priv = source->priv;
g_return_val_if_fail (PINOS_IS_UPLOAD_NODE (node), NULL);
priv = node->priv;
if (priv->channel == NULL) {
GstCaps *caps = gst_caps_from_string (g_bytes_get_data (format_filter, NULL));
gst_caps_take (&priv->format, caps);
priv->channel = PINOS_SOURCE_CLASS (pinos_client_source_parent_class)
->create_channel (PINOS_SOURCE (source),
client_path,
format_filter,
props,
prefix,
error);
priv->channel = pinos_port_create_channel (priv->input,
client_path,
format_filter,
props,
error);
if (priv->channel == NULL)
return NULL;
g_signal_connect (priv->channel,
"remove",
(GCallback) handle_remove_channel,
source);
node);
g_debug ("client-source %p: get source input %p", source, priv->channel);
g_signal_connect (priv->channel, "notify::socket", (GCallback) on_input_socket_notify, source);
g_debug ("upload-node %p: get input %p", node, priv->channel);
g_signal_connect (priv->channel, "notify::socket", (GCallback) on_input_socket_notify, node);
}
return g_object_ref (priv->channel);
}
static void
pinos_client_source_class_init (PinosClientSourceClass * klass)
pinos_upload_node_class_init (PinosUploadNodeClass * klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
PinosSourceClass *source_class = PINOS_SOURCE_CLASS (klass);
PinosNodeClass *node_class = PINOS_NODE_CLASS (klass);
g_type_class_add_private (klass, sizeof (PinosClientSourcePrivate));
g_type_class_add_private (klass, sizeof (PinosUploadNodePrivate));
gobject_class->dispose = client_source_dispose;
gobject_class->finalize = client_source_finalize;
gobject_class->dispose = upload_node_dispose;
gobject_class->finalize = upload_node_finalize;
gobject_class->get_property = client_source_get_property;
gobject_class->set_property = client_source_set_property;
gobject_class->get_property = upload_node_get_property;
gobject_class->set_property = upload_node_set_property;
g_object_class_install_property (gobject_class,
PROP_POSSIBLE_FORMATS,
@ -494,37 +409,47 @@ pinos_client_source_class_init (PinosClientSourceClass * klass)
G_PARAM_CONSTRUCT |
G_PARAM_STATIC_STRINGS));
source_class->get_formats = client_get_formats;
source_class->set_state = client_set_state;
source_class->create_channel = client_create_channel;
source_class->release_channel = client_release_channel;
node_class->set_state = node_set_state;
}
static void
pinos_client_source_init (PinosClientSource * source)
pinos_upload_node_init (PinosUploadNode * node)
{
source->priv = PINOS_CLIENT_SOURCE_GET_PRIVATE (source);
PinosUploadNodePrivate *priv = node->priv = PINOS_UPLOAD_NODE_GET_PRIVATE (node);
g_debug ("client-source %p: new", source);
setup_pipeline (source);
g_debug ("upload-node %p: new", node);
priv->input = pinos_port_new (PINOS_NODE (node),
PINOS_DIRECTION_INPUT,
"input",
priv->possible_formats,
NULL);
priv->output = pinos_port_new (PINOS_NODE (node),
PINOS_DIRECTION_OUTPUT,
"output",
priv->possible_formats,
NULL);
g_signal_connect (priv->output, "channel-added", (GCallback) on_channel_added, node);
g_signal_connect (priv->output, "channel-removed", (GCallback) on_channel_removed, node);
setup_pipeline (node);
}
/**
* pinos_client_source_new:
* pinos_upload_node_new:
* @daemon: the parent #PinosDaemon
* @possible_formats: a #GBytes
*
* Make a new #PinosSource that can be used to receive data from a client.
* Make a new #PinosNode that can be used to receive data from a client.
*
* Returns: a new #PinosSource.
* Returns: a new #PinosNode.
*/
PinosSource *
pinos_client_source_new (PinosDaemon *daemon,
GBytes *possible_formats)
PinosNode *
pinos_upload_node_new (PinosDaemon *daemon,
GBytes *possible_formats)
{
return g_object_new (PINOS_TYPE_CLIENT_SOURCE,
return g_object_new (PINOS_TYPE_UPLOAD_NODE,
"daemon", daemon,
"name", "client-source",
"name", "upload-node",
"possible-formats", possible_formats,
NULL);
}

View file

@ -0,0 +1,75 @@
/* 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_UPLOAD_NODE_H__
#define __PINOS_UPLOAD_NODE_H__
#include <glib-object.h>
G_BEGIN_DECLS
typedef struct _PinosUploadNode PinosUploadNode;
typedef struct _PinosUploadNodeClass PinosUploadNodeClass;
typedef struct _PinosUploadNodePrivate PinosUploadNodePrivate;
#include <pinos/server/node.h>
#define PINOS_TYPE_UPLOAD_NODE (pinos_upload_node_get_type ())
#define PINOS_IS_UPLOAD_NODE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PINOS_TYPE_UPLOAD_NODE))
#define PINOS_IS_UPLOAD_NODE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PINOS_TYPE_UPLOAD_NODE))
#define PINOS_UPLOAD_NODE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), PINOS_TYPE_UPLOAD_NODE, PinosUploadNodeClass))
#define PINOS_UPLOAD_NODE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), PINOS_TYPE_UPLOAD_NODE, PinosUploadNode))
#define PINOS_UPLOAD_NODE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PINOS_TYPE_UPLOAD_NODE, PinosUploadNodeClass))
#define PINOS_UPLOAD_NODE_CAST(obj) ((PinosUploadNode*)(obj))
#define PINOS_UPLOAD_NODE_CLASS_CAST(klass) ((PinosUploadNodeClass*)(klass))
/**
* PinosUploadNode:
*
* Pinos client source object class.
*/
struct _PinosUploadNode {
PinosNode object;
PinosUploadNodePrivate *priv;
};
/**
* PinosUploadNodeClass:
*
* Pinos client source object class.
*/
struct _PinosUploadNodeClass {
PinosNodeClass parent_class;
};
/* normal GObject stuff */
GType pinos_upload_node_get_type (void);
PinosNode * pinos_upload_node_new (PinosDaemon *daemon,
GBytes *possible_formats);
PinosChannel * pinos_upload_node_get_channel (PinosUploadNode *source,
const gchar *client_path,
GBytes *format_filter,
PinosProperties *props,
GError **error);
G_END_DECLS
#endif /* __PINOS_UPLOAD_NODE_H__ */