Introduce the concept of a Node

Make an object for a processing node.
Implement a sink node. Make it possible to implement Sink and Source
interfaces to provide input/output from the node.
Improve pinosdepay to track fds and handle format changes.
This commit is contained in:
Wim Taymans 2016-05-05 13:31:18 +02:00
parent 7597e48e02
commit b885d40390
27 changed files with 3150 additions and 160 deletions

View file

@ -237,7 +237,7 @@ handle_start (PinosChannel1 *interface,
socketpair (AF_UNIX, SOCK_STREAM, 0, fd);
g_debug ("channel %p: handle start, fd[%d,%d]", channel, fd[0], fd[1]);
g_debug ("channel %p: handle start, fd[%d,%d], format %s", channel, fd[0], fd[1], arg_requested_format);
g_clear_object (&priv->socket);
priv->socket = g_socket_new_from_fd (fd[0], NULL);

View file

@ -148,6 +148,7 @@ handle_create_source_channel (PinosClient1 *interface,
{
PinosClient *client = user_data;
PinosClientPrivate *priv = client->priv;
PinosNode *node;
PinosSource *source;
PinosChannel *channel;
const gchar *object_path, *sender;
@ -162,11 +163,15 @@ handle_create_source_channel (PinosClient1 *interface,
formats = g_bytes_new (arg_accepted_formats, strlen (arg_accepted_formats) + 1);
props = pinos_properties_from_variant (arg_properties);
source = pinos_daemon_find_source (priv->daemon,
arg_source,
props,
formats,
&error);
node = pinos_daemon_find_node (priv->daemon,
arg_source,
props,
formats,
&error);
if (node == NULL)
goto no_node;
source = pinos_node_get_source (node);
if (source == NULL)
goto no_source;
@ -190,7 +195,7 @@ handle_create_source_channel (PinosClient1 *interface,
client);
object_path = pinos_channel_get_object_path (channel);
g_debug ("client %p: add channel %p, %s", client, channel, object_path);
g_debug ("client %p: add source channel %p, %s", client, channel, object_path);
g_dbus_method_invocation_return_value (invocation,
g_variant_new ("(o)", object_path));
@ -203,18 +208,124 @@ not_allowed:
"org.pinos.Error", "not client owner");
return TRUE;
}
no_source:
no_node:
{
g_debug ("client %p: could not find source %s, %s", client, arg_source, error->message);
g_debug ("client %p: could not find node %s, %s", client, arg_source, 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 channel %s", client, error->message);
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_dbus_method_invocation_return_gerror (invocation, error);
g_clear_error (&error);
return TRUE;
@ -298,7 +409,7 @@ no_source:
}
no_channel:
{
g_debug ("client %p: could not create channel %s", client, error->message);
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_clear_error (&error);
@ -340,6 +451,9 @@ client_register_object (PinosClient *client,
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,
client);
g_signal_connect (priv->client1, "handle-create-upload-channel",
(GCallback) handle_create_upload_channel,
client);

View file

@ -37,7 +37,7 @@ struct _PinosDaemonPrivate
GDBusConnection *connection;
GDBusObjectManagerServer *server_manager;
GList *sources;
GList *nodes;
GHashTable *senders;
@ -340,87 +340,87 @@ pinos_daemon_unexport (PinosDaemon *daemon,
}
/**
* pinos_daemon_add_source:
* pinos_daemon_add_node:
* @daemon: a #PinosDaemon
* @source: a #PinosSource
* @node: a #PinosNode
*
* Add @source to @daemon.
* Add @node to @daemon.
*/
void
pinos_daemon_add_source (PinosDaemon *daemon,
PinosSource *source)
pinos_daemon_add_node (PinosDaemon *daemon,
PinosNode *node)
{
PinosDaemonPrivate *priv;
g_return_if_fail (PINOS_IS_DAEMON (daemon));
g_return_if_fail (PINOS_IS_SOURCE (source));
g_return_if_fail (PINOS_IS_NODE (node));
priv = daemon->priv;
priv->sources = g_list_prepend (priv->sources, source);
priv->nodes = g_list_prepend (priv->nodes, node);
}
/**
* pinos_daemon_remove_source:
* pinos_daemon_remove_node:
* @daemon: a #PinosDaemon
* @source: a #PinosSource
* @node: a #PinosNode
*
* Remove @source from @daemon.
* Remove @node from @daemon.
*/
void
pinos_daemon_remove_source (PinosDaemon *daemon,
PinosSource *source)
pinos_daemon_remove_node (PinosDaemon *daemon,
PinosNode *node)
{
PinosDaemonPrivate *priv;
g_return_if_fail (PINOS_IS_DAEMON (daemon));
g_return_if_fail (PINOS_IS_SOURCE (source));
g_return_if_fail (PINOS_IS_NODE (node));
priv = daemon->priv;
priv->sources = g_list_remove (priv->sources, source);
priv->nodes = g_list_remove (priv->nodes, node);
}
/**
* pinos_daemon_find_source:
* pinos_daemon_find_node:
* @daemon: a #PinosDaemon
* @name: a source name
* @props: source properties
* @name: a node name
* @props: node properties
* @format_filter: a format filter
* @error: location for an error
*
* Find the best source in @daemon that matches the given parameters.
* Find the best node in @daemon that matches the given parameters.
*
* Returns: a #PinosSource or %NULL when no source could be found.
* Returns: a #PinosNode or %NULL when no node could be found.
*/
PinosSource *
pinos_daemon_find_source (PinosDaemon *daemon,
const gchar *name,
PinosProperties *props,
GBytes *format_filter,
GError **error)
PinosNode *
pinos_daemon_find_node (PinosDaemon *daemon,
const gchar *name,
PinosProperties *props,
GBytes *format_filter,
GError **error)
{
PinosDaemonPrivate *priv;
PinosSource *best = NULL;
PinosNode *best = NULL;
GList *walk;
g_return_val_if_fail (PINOS_IS_DAEMON (daemon), NULL);
priv = daemon->priv;
for (walk = priv->sources; walk; walk = g_list_next (walk)) {
PinosSource *s = walk->data;
for (walk = priv->nodes; walk; walk = g_list_next (walk)) {
PinosNode *n = walk->data;
if (name == NULL) {
best = s;
best = n;
break;
}
else if (g_str_has_suffix (pinos_source_get_object_path (s), name))
best = s;
else if (g_str_has_suffix (pinos_node_get_object_path (n), name))
best = n;
}
if (best == NULL) {
if (error)
*error = g_error_new (G_IO_ERROR,
G_IO_ERROR_NOT_FOUND,
"Source not found");
"Node not found");
}
return best;
}

View file

@ -37,7 +37,7 @@ typedef struct _PinosDaemon PinosDaemon;
typedef struct _PinosDaemonClass PinosDaemonClass;
typedef struct _PinosDaemonPrivate PinosDaemonPrivate;
#include <pinos/server/source.h>
#include <pinos/server/node.h>
#include <pinos/client/properties.h>
/**
@ -71,9 +71,9 @@ void pinos_daemon_stop (PinosDaemon *daemon);
gchar * pinos_daemon_export_uniquely (PinosDaemon *daemon, GDBusObjectSkeleton *skel);
void pinos_daemon_unexport (PinosDaemon *daemon, const gchar *name);
void pinos_daemon_add_source (PinosDaemon *daemon, PinosSource *source);
void pinos_daemon_remove_source (PinosDaemon *daemon, PinosSource *source);
PinosSource * pinos_daemon_find_source (PinosDaemon *daemon,
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,
const gchar *name,
PinosProperties *props,
GBytes *format_filter,

333
pinos/server/node.c Normal file
View file

@ -0,0 +1,333 @@
/* 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/node.h"
#include "pinos/server/source.h"
#include "pinos/server/daemon.h"
#include "pinos/dbus/org-pinos.h"
#define PINOS_NODE_GET_PRIVATE(node) \
(G_TYPE_INSTANCE_GET_PRIVATE ((node), PINOS_TYPE_NODE, PinosNodePrivate))
struct _PinosNodePrivate
{
PinosDaemon *daemon;
PinosObjectSkeleton *skeleton;
gchar *object_path;
PinosSource *source;
PinosSink *sink;
};
G_DEFINE_TYPE (PinosNode, pinos_node, G_TYPE_OBJECT);
enum
{
PROP_0,
PROP_DAEMON,
PROP_SKELETON,
PROP_OBJECT_PATH,
};
static void
pinos_node_get_property (GObject *_object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
PinosNode *node = PINOS_NODE (_object);
PinosNodePrivate *priv = node->priv;
switch (prop_id) {
case PROP_DAEMON:
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;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (node, prop_id, pspec);
break;
}
}
static void
pinos_node_set_property (GObject *_object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
PinosNode *node = PINOS_NODE (_object);
PinosNodePrivate *priv = node->priv;
switch (prop_id) {
case PROP_DAEMON:
priv->daemon = g_value_dup_object (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (node, prop_id, pspec);
break;
}
}
static void
node_register_object (PinosNode *node)
{
PinosNodePrivate *priv = node->priv;
PinosDaemon *daemon = priv->daemon;
priv->skeleton = pinos_object_skeleton_new (PINOS_DBUS_OBJECT_NODE);
g_free (priv->object_path);
priv->object_path = pinos_daemon_export_uniquely (daemon, G_DBUS_OBJECT_SKELETON (priv->skeleton));
pinos_daemon_add_node (daemon, node);
return;
}
static void
node_unregister_object (PinosNode *node)
{
PinosNodePrivate *priv = node->priv;
pinos_daemon_unexport (priv->daemon, priv->object_path);
pinos_daemon_remove_node (priv->daemon, node);
g_clear_object (&priv->skeleton);
}
static void
pinos_node_constructed (GObject * obj)
{
PinosNode *node = PINOS_NODE (obj);
node_register_object (node);
G_OBJECT_CLASS (pinos_node_parent_class)->constructed (obj);
}
static void
pinos_node_dispose (GObject * obj)
{
PinosNode *node = PINOS_NODE (obj);
node_unregister_object (node);
G_OBJECT_CLASS (pinos_node_parent_class)->dispose (obj);
}
static void
pinos_node_finalize (GObject * obj)
{
PinosNode *node = PINOS_NODE (obj);
PinosNodePrivate *priv = node->priv;
g_free (priv->object_path);
G_OBJECT_CLASS (pinos_node_parent_class)->finalize (obj);
}
static void
pinos_node_class_init (PinosNodeClass * klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
g_type_class_add_private (klass, sizeof (PinosNodePrivate));
gobject_class->constructed = pinos_node_constructed;
gobject_class->dispose = pinos_node_dispose;
gobject_class->finalize = pinos_node_finalize;
gobject_class->set_property = pinos_node_set_property;
gobject_class->get_property = pinos_node_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_OBJECT_PATH,
g_param_spec_string ("object-path",
"Object Path",
"The object path",
NULL,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS));
}
static void
pinos_node_init (PinosNode * node)
{
node->priv = PINOS_NODE_GET_PRIVATE (node);
}
/**
* pinos_node_get_daemon:
* @node: a #PinosNode
*
* Get the daemon of @node.
*
* Returns: the daemon of @node.
*/
PinosDaemon *
pinos_node_get_daemon (PinosNode *node)
{
PinosNodePrivate *priv;
g_return_val_if_fail (PINOS_IS_NODE (node), NULL);
priv = node->priv;
return priv->daemon;
}
/**
* pinos_node_get_object_path:
* @node: a #PinosNode
*
* Get the object path of @node.
*
* Returns: the object path of @node.
*/
const gchar *
pinos_node_get_object_path (PinosNode *node)
{
PinosNodePrivate *priv;
g_return_val_if_fail (PINOS_IS_NODE (node), NULL);
priv = node->priv;
return priv->object_path;
}
/**
* pinos_node_set_source:
* @node: a #PinosNode
* @source: a #PinosSource
*
* Set the #PinosSource of @node
*/
void
pinos_node_set_source (PinosNode *node, PinosSource *source, GObject *iface)
{
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));
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;
}
}
/**
* pinos_node_get_source:
* @node: a #PinosNode
*
* Get the #PinosSource of @node
*
* Returns: the #PinosSource of @node or %NULL
*/
PinosSource *
pinos_node_get_source (PinosNode *node)
{
PinosNodePrivate *priv;
g_return_val_if_fail (PINOS_IS_NODE (node), NULL);
priv = node->priv;
return priv->source;
}
/**
* 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)
{
PinosNodePrivate *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;
}
}
/**
* pinos_node_get_sink:
* @node: a #PinosNode
*
* Get the #PinosSink of @node
*
* Returns: the #PinosSink of @node or %NULL
*/
PinosSink *
pinos_node_get_sink (PinosNode *node)
{
PinosNodePrivate *priv;
g_return_val_if_fail (PINOS_IS_NODE (node), NULL);
priv = node->priv;
return priv->sink;
}
PinosNode *
pinos_node_new (PinosDaemon *daemon)
{
return g_object_new (PINOS_TYPE_NODE,
"daemon", daemon,
NULL);
}

85
pinos/server/node.h Normal file
View file

@ -0,0 +1,85 @@
/* 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_NODE_H__
#define __PINOS_NODE_H__
#include <glib-object.h>
G_BEGIN_DECLS
typedef struct _PinosNode PinosNode;
typedef struct _PinosNodeClass PinosNodeClass;
typedef struct _PinosNodePrivate PinosNodePrivate;
#include <pinos/client/introspect.h>
#include <pinos/server/daemon.h>
#include <pinos/server/source.h>
#include <pinos/server/sink.h>
#define PINOS_TYPE_NODE (pinos_node_get_type ())
#define PINOS_IS_NODE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PINOS_TYPE_NODE))
#define PINOS_IS_NODE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PINOS_TYPE_NODE))
#define PINOS_NODE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), PINOS_TYPE_NODE, PinosNodeClass))
#define PINOS_NODE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), PINOS_TYPE_NODE, PinosNode))
#define PINOS_NODE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PINOS_TYPE_NODE, PinosNodeClass))
#define PINOS_NODE_CAST(obj) ((PinosNode*)(obj))
#define PINOS_NODE_CLASS_CAST(klass) ((PinosNodeClass*)(klass))
/**
* PinosNode:
*
* Pinos node class.
*/
struct _PinosNode {
GObject object;
PinosNodePrivate *priv;
};
/**
* PinosNodeClass:
*
* Pinos node class.
*/
struct _PinosNodeClass {
GObjectClass parent_class;
};
/* normal GObject stuff */
GType pinos_node_get_type (void);
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_set_sink (PinosNode *node,
PinosSink *sink,
GObject *iface);
PinosSink * pinos_node_get_sink (PinosNode *node);
G_END_DECLS
#endif /* __PINOS_NODE_H__ */

646
pinos/server/sink.c Normal file
View file

@ -0,0 +1,646 @@
/* 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;
}

109
pinos/server/sink.h Normal file
View file

@ -0,0 +1,109 @@
/* 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

@ -23,7 +23,7 @@
#include "pinos/client/enumtypes.h"
#include "pinos/server/source.h"
#include "pinos/server/daemon.h"
#include "pinos/server/node.h"
#include "pinos/dbus/org-pinos.h"
@ -33,9 +33,8 @@
struct _PinosSourcePrivate
{
PinosDaemon *daemon;
PinosNode *node;
PinosSource1 *iface;
gchar *object_path;
gchar *name;
PinosProperties *properties;
@ -52,8 +51,7 @@ G_DEFINE_ABSTRACT_TYPE (PinosSource, pinos_source, G_TYPE_OBJECT);
enum
{
PROP_0,
PROP_DAEMON,
PROP_OBJECT_PATH,
PROP_NODE,
PROP_NAME,
PROP_STATE,
PROP_PROPERTIES
@ -69,12 +67,8 @@ pinos_source_get_property (GObject *_object,
PinosSourcePrivate *priv = source->priv;
switch (prop_id) {
case PROP_DAEMON:
g_value_set_object (value, priv->daemon);
break;
case PROP_OBJECT_PATH:
g_value_set_string (value, priv->object_path);
case PROP_NODE:
g_value_set_object (value, priv->node);
break;
case PROP_NAME:
@ -105,13 +99,8 @@ pinos_source_set_property (GObject *_object,
PinosSourcePrivate *priv = source->priv;
switch (prop_id) {
case PROP_DAEMON:
priv->daemon = g_value_dup_object (value);
break;
case PROP_OBJECT_PATH:
g_free (priv->object_path);
priv->object_path = g_value_dup_string (value);
case PROP_NODE:
priv->node = g_value_dup_object (value);
break;
case PROP_NAME:
@ -140,15 +129,11 @@ static void
source_register_object (PinosSource *source)
{
PinosSourcePrivate *priv = source->priv;
PinosDaemon *daemon = priv->daemon;
PinosObjectSkeleton *skel;
GBytes *formats;
GVariant *variant;
formats = pinos_source_get_formats (source, NULL, NULL);
skel = pinos_object_skeleton_new (PINOS_DBUS_OBJECT_SOURCE);
if (priv->properties)
variant = pinos_properties_to_variant (priv->properties);
else
@ -160,12 +145,9 @@ source_register_object (PinosSource *source)
"properties", variant,
"possible-formats", g_bytes_get_data (formats, NULL),
NULL);
pinos_object_skeleton_set_source1 (skel, priv->iface);
g_bytes_unref (formats);
g_free (priv->object_path);
priv->object_path = pinos_daemon_export_uniquely (daemon, G_DBUS_OBJECT_SKELETON (skel));
pinos_daemon_add_source (daemon, source);
pinos_node_set_source (priv->node, source, G_OBJECT (priv->iface));
return;
}
@ -175,8 +157,7 @@ source_unregister_object (PinosSource *source)
{
PinosSourcePrivate *priv = source->priv;
pinos_daemon_remove_source (priv->daemon, source);
pinos_daemon_unexport (priv->daemon, priv->object_path);
pinos_node_set_source (priv->node, NULL, NULL);
g_clear_object (&priv->iface);
}
@ -215,7 +196,6 @@ pinos_source_finalize (GObject * object)
PinosSource *source = PINOS_SOURCE (object);
PinosSourcePrivate *priv = source->priv;
g_free (priv->object_path);
g_free (priv->name);
if (priv->properties)
pinos_properties_free (priv->properties);
@ -256,10 +236,10 @@ default_create_channel (PinosSource *source,
if (possible_formats == NULL)
return NULL;
channel = g_object_new (PINOS_TYPE_CHANNEL, "daemon", priv->daemon,
channel = g_object_new (PINOS_TYPE_CHANNEL, "daemon", pinos_node_get_daemon (priv->node),
"object-path", prefix,
"client-path", client_path,
"owner-path", priv->object_path,
"owner-path", pinos_node_get_object_path (priv->node),
"possible-formats", possible_formats,
"properties", props,
NULL);
@ -319,21 +299,11 @@ pinos_source_class_init (PinosSourceClass * klass)
gobject_class->get_property = pinos_source_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_OBJECT_PATH,
g_param_spec_string ("object-path",
"Object Path",
"The object path",
NULL,
PROP_NODE,
g_param_spec_object ("node",
"Node",
"The Node",
PINOS_TYPE_NODE,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
@ -674,22 +644,3 @@ pinos_source_release_channel (PinosSource *source,
return res;
}
/**
* pinos_source_get_object_path:
* @source: a #PinosSource
*
* Get the object path of @source.
*
* Returns: the object path of @source.
*/
const gchar *
pinos_source_get_object_path (PinosSource *source)
{
PinosSourcePrivate *priv;
g_return_val_if_fail (PINOS_IS_SOURCE (source), NULL);
priv = source->priv;
return priv->object_path;
}

View file

@ -82,8 +82,6 @@ struct _PinosSourceClass {
/* normal GObject stuff */
GType pinos_source_get_type (void);
const gchar * pinos_source_get_object_path (PinosSource *source);
GBytes * pinos_source_get_formats (PinosSource *source,
GBytes *filter,
GError **error);