pipewire/pinos/server/channel.c

559 lines
18 KiB
C
Raw Normal View History

2015-06-30 18:06:36 +02:00
/* Pinos
2015-04-16 16:58:33 +02:00
* 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>
2015-04-16 16:58:33 +02:00
#include <sys/socket.h>
#include <gio/gunixfdlist.h>
#include "pinos/client/enumtypes.h"
2015-04-16 16:58:33 +02:00
#include "pinos/server/daemon.h"
#include "pinos/server/channel.h"
#include "pinos/dbus/org-pinos.h"
2015-04-16 16:58:33 +02:00
struct _PinosChannelPrivate
2015-04-16 16:58:33 +02:00
{
PinosDaemon *daemon;
PinosChannel1 *iface;
2015-04-16 16:58:33 +02:00
gchar *object_path;
gchar *client_path;
gchar *owner_path;
GBytes *possible_formats;
PinosProperties *properties;
GBytes *requested_format;
PinosChannelState state;
GBytes *format;
2015-04-16 16:58:33 +02:00
GSocket *socket;
};
#define PINOS_CHANNEL_GET_PRIVATE(obj) \
(G_TYPE_INSTANCE_GET_PRIVATE ((obj), PINOS_TYPE_CHANNEL, PinosChannelPrivate))
2015-04-16 16:58:33 +02:00
G_DEFINE_TYPE (PinosChannel, pinos_channel, G_TYPE_OBJECT);
2015-04-16 16:58:33 +02:00
enum
{
PROP_0,
PROP_DAEMON,
2015-04-16 16:58:33 +02:00
PROP_OBJECT_PATH,
PROP_CLIENT_PATH,
PROP_OWNER_PATH,
PROP_POSSIBLE_FORMATS,
PROP_PROPERTIES,
PROP_REQUESTED_FORMAT,
PROP_FORMAT,
2015-04-16 16:58:33 +02:00
PROP_SOCKET,
PROP_STATE,
2015-04-16 16:58:33 +02:00
};
2015-05-27 18:16:52 +02:00
enum
{
SIGNAL_REMOVE,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };
2015-04-16 16:58:33 +02:00
static void
pinos_channel_get_property (GObject *_object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
2015-04-16 16:58:33 +02:00
{
PinosChannel *channel = PINOS_CHANNEL (_object);
PinosChannelPrivate *priv = channel->priv;
2015-04-16 16:58:33 +02:00
switch (prop_id) {
case PROP_DAEMON:
g_value_set_object (value, priv->daemon);
2015-04-16 16:58:33 +02:00
break;
case PROP_OBJECT_PATH:
g_value_set_string (value, priv->object_path);
break;
case PROP_CLIENT_PATH:
g_value_set_string (value, priv->client_path);
break;
case PROP_OWNER_PATH:
g_value_set_string (value, priv->owner_path);
break;
case PROP_POSSIBLE_FORMATS:
g_value_set_boxed (value, priv->possible_formats);
break;
case PROP_PROPERTIES:
g_value_set_boxed (value, priv->properties);
break;
case PROP_REQUESTED_FORMAT:
g_value_set_boxed (value, priv->requested_format);
break;
case PROP_FORMAT:
g_value_set_boxed (value, priv->format);
2015-04-22 16:58:36 +02:00
break;
2015-04-16 16:58:33 +02:00
case PROP_SOCKET:
g_value_set_object (value, priv->socket);
break;
case PROP_STATE:
g_value_set_uint (value, priv->state);
break;
2015-04-16 16:58:33 +02:00
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (channel, prop_id, pspec);
2015-04-16 16:58:33 +02:00
break;
}
}
static void
pinos_channel_set_property (GObject *_object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
2015-04-16 16:58:33 +02:00
{
PinosChannel *channel = PINOS_CHANNEL (_object);
PinosChannelPrivate *priv = channel->priv;
2015-04-16 16:58:33 +02:00
switch (prop_id) {
case PROP_DAEMON:
priv->daemon = g_value_dup_object (value);
2015-04-16 16:58:33 +02:00
break;
case PROP_OBJECT_PATH:
priv->object_path = g_value_dup_string (value);
break;
case PROP_CLIENT_PATH:
priv->client_path = g_value_dup_string (value);
g_object_set (priv->iface, "client", priv->client_path, NULL);
break;
case PROP_OWNER_PATH:
priv->owner_path = g_value_dup_string (value);
g_object_set (priv->iface, "owner", priv->owner_path, NULL);
break;
case PROP_POSSIBLE_FORMATS:
if (priv->possible_formats)
g_bytes_unref (priv->possible_formats);
priv->possible_formats = g_value_dup_boxed (value);
g_object_set (priv->iface, "possible-formats",
g_bytes_get_data (priv->possible_formats, NULL), NULL);
break;
case PROP_PROPERTIES:
if (priv->properties)
pinos_properties_free (priv->properties);
priv->properties = g_value_dup_boxed (value);
g_object_set (priv->iface, "properties",
pinos_properties_to_variant (priv->properties), NULL);
break;
case PROP_FORMAT:
if (priv->format)
g_bytes_unref (priv->format);
priv->format = g_value_dup_boxed (value);
g_object_set (priv->iface, "format",
g_bytes_get_data (priv->format, NULL), NULL);
2015-04-22 16:58:36 +02:00
break;
2015-04-16 16:58:33 +02:00
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (channel, prop_id, pspec);
2015-04-16 16:58:33 +02:00
break;
}
}
2016-04-07 17:28:46 +02:00
static void
clear_formats (PinosChannel *channel)
2016-04-07 17:28:46 +02:00
{
PinosChannelPrivate *priv = channel->priv;
2016-04-07 17:28:46 +02:00
g_debug ("channel %p: clear format", channel);
2016-04-11 15:26:15 +02:00
2016-04-07 17:28:46 +02:00
g_clear_pointer (&priv->requested_format, g_bytes_unref);
g_clear_pointer (&priv->format, g_bytes_unref);
}
static void
stop_transfer (PinosChannel *channel)
2016-04-07 17:28:46 +02:00
{
PinosChannelPrivate *priv = channel->priv;
2016-04-07 17:28:46 +02:00
g_debug ("channel %p: stop transfer", channel);
2016-04-11 15:26:15 +02:00
2016-04-07 17:28:46 +02:00
if (priv->socket) {
g_clear_object (&priv->socket);
g_object_notify (G_OBJECT (channel), "socket");
2016-04-07 17:28:46 +02:00
}
clear_formats (channel);
priv->state = PINOS_CHANNEL_STATE_IDLE;
2016-04-07 17:28:46 +02:00
g_object_set (priv->iface,
"state", priv->state,
NULL);
}
2015-04-16 16:58:33 +02:00
static gboolean
handle_start (PinosChannel1 *interface,
GDBusMethodInvocation *invocation,
const gchar *arg_requested_format,
gpointer user_data)
2015-04-16 16:58:33 +02:00
{
PinosChannel *channel = user_data;
PinosChannelPrivate *priv = channel->priv;
2015-04-16 16:58:33 +02:00
GUnixFDList *fdlist;
gint fd[2];
2016-04-11 15:26:15 +02:00
const gchar *format;
2015-04-16 16:58:33 +02:00
priv->state = PINOS_CHANNEL_STATE_STARTING;
priv->requested_format = g_bytes_new (arg_requested_format,
strlen (arg_requested_format) + 1);
2015-04-16 16:58:33 +02:00
socketpair (AF_UNIX, SOCK_STREAM, 0, fd);
2016-04-07 17:28:46 +02:00
g_debug ("channel %p: handle start, fd[%d,%d]", channel, fd[0], fd[1]);
2016-04-11 15:26:15 +02:00
2016-04-07 17:28:46 +02:00
g_clear_object (&priv->socket);
2015-04-16 16:58:33 +02:00
priv->socket = g_socket_new_from_fd (fd[0], NULL);
2016-04-11 15:26:15 +02:00
g_object_set_data (G_OBJECT (priv->socket), "pinos-client-path", priv->client_path);
g_debug ("channel %p: notify socket %p, path %s", channel, priv->socket, priv->client_path);
g_object_notify (G_OBJECT (channel), "socket");
2015-04-16 16:58:33 +02:00
/* the notify of the socket above should configure the format */
if (priv->format == NULL)
goto no_format;
2016-04-11 15:26:15 +02:00
format = g_bytes_get_data (priv->format, NULL);
priv->state = PINOS_CHANNEL_STATE_STREAMING;
g_debug ("channel %p: we are now streaming in format \"%s\"", channel, format);
2015-04-16 16:58:33 +02:00
fdlist = g_unix_fd_list_new ();
g_unix_fd_list_append (fdlist, fd[1], NULL);
2015-04-16 16:58:33 +02:00
g_dbus_method_invocation_return_value_with_unix_fd_list (invocation,
g_variant_new ("(hs@a{sv})",
0,
2016-04-11 15:26:15 +02:00
format,
pinos_properties_to_variant (priv->properties)),
fdlist);
2016-04-07 17:28:46 +02:00
g_object_unref (fdlist);
close (fd[1]);
2015-04-16 16:58:33 +02:00
g_object_set (priv->iface,
2016-04-11 15:26:15 +02:00
"format", format,
"state", priv->state,
NULL);
2015-04-16 16:58:33 +02:00
return TRUE;
/* error */
no_format:
{
g_debug ("channel %p: no format configured", channel);
g_dbus_method_invocation_return_dbus_error (invocation,
2015-06-30 18:06:36 +02:00
"org.pinos.Error", "No format");
close (fd[0]);
close (fd[1]);
g_clear_pointer (&priv->requested_format, g_bytes_unref);
g_clear_object (&priv->socket);
return TRUE;
}
2015-04-16 16:58:33 +02:00
}
static gboolean
handle_stop (PinosChannel1 *interface,
GDBusMethodInvocation *invocation,
gpointer user_data)
{
PinosChannel *channel = user_data;
g_debug ("channel %p: handle stop", channel);
stop_transfer (channel);
g_dbus_method_invocation_return_value (invocation, NULL);
return TRUE;
}
static gboolean
handle_remove (PinosChannel1 *interface,
GDBusMethodInvocation *invocation,
gpointer user_data)
2015-04-16 16:58:33 +02:00
{
PinosChannel *channel = user_data;
2015-04-16 16:58:33 +02:00
g_debug ("channel %p: handle remove", channel);
stop_transfer (channel);
2015-04-16 16:58:33 +02:00
g_signal_emit (channel, signals[SIGNAL_REMOVE], 0, NULL);
2015-05-27 18:16:52 +02:00
g_dbus_method_invocation_return_value (invocation, NULL);
2015-04-16 16:58:33 +02:00
return TRUE;
}
static void
channel_register_object (PinosChannel *channel,
const gchar *prefix)
2015-04-16 16:58:33 +02:00
{
PinosChannelPrivate *priv = channel->priv;
PinosObjectSkeleton *skel;
2015-04-16 16:58:33 +02:00
gchar *name;
name = g_strdup_printf ("%s/channel", prefix);
skel = pinos_object_skeleton_new (name);
2015-04-16 16:58:33 +02:00
g_free (name);
pinos_object_skeleton_set_channel1 (skel, priv->iface);
2015-04-16 16:58:33 +02:00
g_free (priv->object_path);
priv->object_path = pinos_daemon_export_uniquely (priv->daemon, G_DBUS_OBJECT_SKELETON (skel));
g_debug ("channel %p: register object %s", channel, priv->object_path);
2015-04-16 16:58:33 +02:00
}
static void
channel_unregister_object (PinosChannel *channel)
2015-04-16 16:58:33 +02:00
{
PinosChannelPrivate *priv = channel->priv;
2015-04-16 16:58:33 +02:00
g_debug ("channel %p: unregister object", channel);
pinos_daemon_unexport (priv->daemon, priv->object_path);
2015-04-16 16:58:33 +02:00
}
static void
pinos_channel_dispose (GObject * object)
2015-04-16 16:58:33 +02:00
{
PinosChannel *channel = PINOS_CHANNEL (object);
PinosChannelPrivate *priv = channel->priv;
2015-04-16 16:58:33 +02:00
g_debug ("channel %p: dispose", channel);
clear_formats (channel);
2016-04-07 17:28:46 +02:00
g_clear_object (&priv->socket);
channel_unregister_object (channel);
2015-04-22 16:58:36 +02:00
G_OBJECT_CLASS (pinos_channel_parent_class)->dispose (object);
}
static void
pinos_channel_finalize (GObject * object)
{
PinosChannel *channel = PINOS_CHANNEL (object);
PinosChannelPrivate *priv = channel->priv;
g_debug ("channel %p: finalize", channel);
2016-04-07 17:28:46 +02:00
if (priv->possible_formats)
g_bytes_unref (priv->possible_formats);
if (priv->properties)
pinos_properties_free (priv->properties);
2015-05-27 18:16:52 +02:00
g_clear_object (&priv->daemon);
g_clear_object (&priv->iface);
g_free (priv->client_path);
2015-04-16 16:58:33 +02:00
g_free (priv->object_path);
g_free (priv->owner_path);
2015-04-16 16:58:33 +02:00
G_OBJECT_CLASS (pinos_channel_parent_class)->finalize (object);
2015-04-16 16:58:33 +02:00
}
2015-04-22 16:58:36 +02:00
2015-04-16 16:58:33 +02:00
static void
pinos_channel_constructed (GObject * object)
2015-04-16 16:58:33 +02:00
{
PinosChannel *channel = PINOS_CHANNEL (object);
PinosChannelPrivate *priv = channel->priv;
2015-04-16 16:58:33 +02:00
g_debug ("channel %p: constructed", channel);
channel_register_object (channel, priv->object_path);
2015-04-16 16:58:33 +02:00
G_OBJECT_CLASS (pinos_channel_parent_class)->constructed (object);
2015-04-16 16:58:33 +02:00
}
static void
pinos_channel_class_init (PinosChannelClass * klass)
2015-04-16 16:58:33 +02:00
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
g_type_class_add_private (klass, sizeof (PinosChannelPrivate));
2015-04-16 16:58:33 +02:00
gobject_class->constructed = pinos_channel_constructed;
gobject_class->dispose = pinos_channel_dispose;
gobject_class->finalize = pinos_channel_finalize;
gobject_class->set_property = pinos_channel_set_property;
gobject_class->get_property = pinos_channel_get_property;
2015-04-16 16:58:33 +02:00
g_object_class_install_property (gobject_class,
PROP_DAEMON,
g_param_spec_object ("daemon",
"Daemon",
"The Daemon",
PINOS_TYPE_DAEMON,
2015-04-16 16:58:33 +02:00
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_OBJECT_PATH,
g_param_spec_string ("object-path",
"Object Path",
"The object path",
NULL,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
2015-04-22 16:58:36 +02:00
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_CLIENT_PATH,
g_param_spec_string ("client-path",
"Client Path",
"The client object path",
NULL,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_OWNER_PATH,
g_param_spec_string ("owner-path",
"Owner Path",
"The owner object path",
2015-04-22 16:58:36 +02:00
NULL,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
2015-04-16 16:58:33 +02:00
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_POSSIBLE_FORMATS,
g_param_spec_boxed ("possible-formats",
"Possible Formats",
"The possbile formats of the stream",
G_TYPE_BYTES,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_PROPERTIES,
g_param_spec_boxed ("properties",
"Properties",
"Extra properties of the stream",
PINOS_TYPE_PROPERTIES,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_REQUESTED_FORMAT,
g_param_spec_boxed ("requested-format",
"Requested Format",
"The requested format of the stream",
G_TYPE_BYTES,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_FORMAT,
g_param_spec_boxed ("format",
"Format",
"The format of the stream",
G_TYPE_BYTES,
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS));
2015-04-16 16:58:33 +02:00
g_object_class_install_property (gobject_class,
PROP_SOCKET,
g_param_spec_object ("socket",
"Socket",
"The socket with data",
G_TYPE_SOCKET,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS));
2015-05-27 18:16:52 +02:00
signals[SIGNAL_REMOVE] = g_signal_new ("remove",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
0,
NULL,
NULL,
g_cclosure_marshal_generic,
G_TYPE_NONE,
0,
G_TYPE_NONE);
2015-04-16 16:58:33 +02:00
}
static void
pinos_channel_init (PinosChannel * channel)
2015-04-16 16:58:33 +02:00
{
PinosChannelPrivate *priv = channel->priv = PINOS_CHANNEL_GET_PRIVATE (channel);
priv->iface = pinos_channel1_skeleton_new ();
g_signal_connect (priv->iface, "handle-start", (GCallback) handle_start, channel);
g_signal_connect (priv->iface, "handle-stop", (GCallback) handle_stop, channel);
g_signal_connect (priv->iface, "handle-remove", (GCallback) handle_remove, channel);
priv->state = PINOS_CHANNEL_STATE_IDLE;
g_object_set (priv->iface, "state", priv->state, NULL);
2016-04-11 15:26:15 +02:00
g_debug ("channel %p: new", channel);
2015-04-16 16:58:33 +02:00
}
2016-04-12 08:55:20 +02:00
/**
* pinos_channel_remove:
* @channel: a #PinosChannel
2016-04-12 08:55:20 +02:00
*
* Remove @channel. This will stop the transfer on the channel and
* free the resources allocated by @channel.
2016-04-12 08:55:20 +02:00
*/
2015-06-04 16:34:47 +02:00
void
pinos_channel_remove (PinosChannel *channel)
2015-06-04 16:34:47 +02:00
{
g_debug ("channel %p: remove", channel);
stop_transfer (channel);
2015-06-04 16:34:47 +02:00
g_signal_emit (channel, signals[SIGNAL_REMOVE], 0, NULL);
2015-06-04 16:34:47 +02:00
}
2016-04-12 08:55:20 +02:00
/**
* pinos_channel_get_object_path:
* @channel: a #PinosChannel
2016-04-12 08:55:20 +02:00
*
* Get the object patch of @channel
2016-04-12 08:55:20 +02:00
*
* Returns: the object path of @source.
*/
2015-04-16 16:58:33 +02:00
const gchar *
pinos_channel_get_object_path (PinosChannel *channel)
2015-04-16 16:58:33 +02:00
{
PinosChannelPrivate *priv;
2015-04-16 16:58:33 +02:00
g_return_val_if_fail (PINOS_IS_CHANNEL (channel), NULL);
priv = channel->priv;
2015-04-16 16:58:33 +02:00
return priv->object_path;
}