work on stream negotiation and start

Add more buffer types to add and remove memory shared memory between the
server and client. We would like to send buffers only once and then
simply reference them by index.
Do format negotiation and stream start with a START message.
This commit is contained in:
Wim Taymans 2016-07-21 18:38:24 +02:00
parent 31041a4e16
commit af3de36416
23 changed files with 1246 additions and 349 deletions

View file

@ -154,9 +154,9 @@ pinos_channel_get_property (GObject *_object,
static void
pinos_channel_set_property (GObject *_object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
PinosChannel *channel = PINOS_CHANNEL (_object);
PinosChannelPrivate *priv = channel->priv;
@ -234,89 +234,6 @@ stop_transfer (PinosChannel *channel)
pinos_port_deactivate (priv->port);
clear_formats (channel);
priv->state = PINOS_CHANNEL_STATE_STOPPED;
g_object_set (priv->iface,
"state", priv->state,
NULL);
}
static gboolean
handle_start (PinosChannel1 *interface,
GDBusMethodInvocation *invocation,
const gchar *arg_requested_format,
gpointer user_data)
{
PinosChannel *channel = user_data;
PinosChannelPrivate *priv = channel->priv;
GBytes *req_format, *format;
const gchar *format_str;
GError *error = NULL;
priv->state = PINOS_CHANNEL_STATE_STARTING;
req_format = g_bytes_new (arg_requested_format, strlen (arg_requested_format) + 1);
format = pinos_format_filter (priv->possible_formats, req_format, &error);
if (format == NULL)
goto no_format;
format_str = g_bytes_get_data (format, NULL);
g_debug ("channel %p: handle start, format %s", channel, format_str);
g_object_set (priv->port, "possible-formats", format, NULL);
pinos_port_activate (priv->port);
g_object_get (priv->port, "format", &format, NULL);
if (format == NULL)
goto no_format_activate;
format_str = g_bytes_get_data (format, NULL);
priv->state = PINOS_CHANNEL_STATE_STREAMING;
g_debug ("channel %p: we are now streaming in format \"%s\"", channel, format_str);
g_dbus_method_invocation_return_value (invocation,
g_variant_new ("(s@a{sv})",
format_str,
pinos_properties_to_variant (priv->properties)));
g_object_set (priv->iface,
"format", format_str,
"state", priv->state,
NULL);
return TRUE;
no_format:
{
g_debug ("no format found");
g_dbus_method_invocation_return_gerror (invocation, error);
g_clear_error (&error);
return TRUE;
}
no_format_activate:
{
g_debug ("no format found when activating");
g_dbus_method_invocation_return_dbus_error (invocation,
"org.pinos.Error", "can't negotiate formats");
return TRUE;
}
}
static gboolean
handle_stop (PinosChannel1 *interface,
GDBusMethodInvocation *invocation,
gpointer user_data)
{
PinosChannel *channel = user_data;
g_debug ("channel %p: handle stop", channel);
stop_transfer (channel);
g_dbus_method_invocation_return_value (invocation, NULL);
return TRUE;
}
static gboolean
@ -354,6 +271,93 @@ on_send_buffer (PinosPort *port,
return res;
}
static gboolean
parse_buffer (PinosChannel *channel,
PinosBuffer *pbuf)
{
PinosBufferIter it;
PinosChannelPrivate *priv = channel->priv;
pinos_buffer_iter_init (&it, pbuf);
while (pinos_buffer_iter_next (&it)) {
PinosPacketType type = pinos_buffer_iter_get_type (&it);
switch (type) {
case PINOS_PACKET_TYPE_FORMAT_CHANGE:
{
PinosPacketFormatChange p;
GBytes *format, *req_format;
GError *error = NULL;
const gchar *format_str;
if (!pinos_buffer_iter_parse_format_change (&it, &p))
break;
req_format = g_bytes_new_static (p.format, strlen (p.format) + 1);
format = pinos_format_filter (priv->possible_formats, req_format, &error);
g_bytes_unref (req_format);
if (format == NULL)
break;
format_str = g_bytes_get_data (format, NULL);
g_debug ("channel %p: format change %s", channel, format_str);
g_object_set (priv->port, "possible-formats", format, NULL);
g_object_set (priv->iface, "format", format_str, NULL);
break;
}
case PINOS_PACKET_TYPE_START:
{
GBytes *format;
PinosBufferBuilder builder;
PinosPacketFormatChange fc;
PinosBuffer obuf;
guint8 buffer[1024];
GError *error = NULL;
pinos_port_activate (priv->port);
g_object_get (priv->port, "format", &format, NULL);
if (format == NULL)
break;
fc.id = 0;
fc.format = g_bytes_get_data (format, NULL);
g_debug ("channel %p: we are now streaming in format \"%s\"", channel, fc.format);
priv->state = PINOS_CHANNEL_STATE_STREAMING;
pinos_buffer_builder_init_into (&builder, buffer, 1024, NULL, 0);
pinos_buffer_builder_add_format_change (&builder, &fc);
pinos_buffer_builder_add_empty (&builder, PINOS_PACKET_TYPE_STREAMING);
pinos_buffer_builder_end (&builder, &obuf);
g_object_set (priv->iface, "state", priv->state, NULL);
if (!pinos_io_write_buffer (priv->fd, &obuf, &error)) {
g_warning ("channel %p: error writing buffer: %s", channel, error->message);
g_clear_error (&error);
}
break;
}
case PINOS_PACKET_TYPE_STOP:
{
break;
}
case PINOS_PACKET_TYPE_REUSE_MEM:
{
break;
}
default:
g_warning ("unhandled packet %d", type);
break;
}
}
pinos_buffer_iter_end (&it);
return TRUE;
}
static gboolean
on_socket_condition (GSocket *socket,
GIOCondition condition,
@ -380,6 +384,8 @@ on_socket_condition (GSocket *socket,
return TRUE;
}
parse_buffer (channel, buffer);
if (!pinos_port_receive_buffer (priv->port, buffer, &error)) {
g_warning ("channel %p: port %p failed to receive buffer: %s", channel, priv->port, error->message);
g_clear_error (&error);
@ -485,7 +491,10 @@ channel_register_object (PinosChannel *channel)
PinosObjectSkeleton *skel;
gchar *name;
priv->send_id = pinos_port_add_send_buffer_cb (priv->port, on_send_buffer, channel, NULL);
priv->send_id = pinos_port_add_send_buffer_cb (priv->port,
on_send_buffer,
channel,
NULL);
name = g_strdup_printf ("%s/channel", priv->client_path);
skel = pinos_object_skeleton_new (name);
@ -557,6 +566,7 @@ pinos_channel_constructed (GObject * object)
G_OBJECT_CLASS (pinos_channel_parent_class)->constructed (object);
}
static void
pinos_channel_class_init (PinosChannelClass * klass)
{
@ -677,8 +687,6 @@ pinos_channel_init (PinosChannel * channel)
PinosChannelPrivate *priv = channel->priv = PINOS_CHANNEL_GET_PRIVATE (channel);
priv->iface = pinos_channel1_skeleton_new ();
g_signal_connect (priv->iface, "handle-start", (GCallback) handle_start, channel);
g_signal_connect (priv->iface, "handle-stop", (GCallback) handle_stop, channel);
g_signal_connect (priv->iface, "handle-remove", (GCallback) handle_remove, channel);
priv->state = PINOS_CHANNEL_STATE_STOPPED;

401
pinos/server/link.c Normal file
View file

@ -0,0 +1,401 @@
/* 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 <gio/gio.h>
#include "pinos/client/pinos.h"
#include "pinos/client/enumtypes.h"
#include "pinos/server/link.h"
#include "pinos/server/port.h"
#include "pinos/dbus/org-pinos.h"
#define PINOS_LINK_GET_PRIVATE(obj) \
(G_TYPE_INSTANCE_GET_PRIVATE ((obj), PINOS_TYPE_LINK, PinosLinkPrivate))
struct _PinosLinkPrivate
{
PinosDaemon *daemon;
PinosLink1 *iface;
gchar *object_path;
PinosPort *src;
PinosPort *dest;
GBytes *possible_formats;
GBytes *format;
PinosProperties *properties;
};
G_DEFINE_TYPE (PinosLink, pinos_link, G_TYPE_OBJECT);
enum
{
PROP_0,
PROP_DAEMON,
PROP_OBJECT_PATH,
PROP_POSSIBLE_FORMATS,
PROP_FORMAT,
PROP_PROPERTIES,
};
enum
{
SIGNAL_REMOVE,
SIGNAL_ACTIVATE,
SIGNAL_DEACTIVATE,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };
static void
pinos_link_get_property (GObject *_object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
PinosLink *link = PINOS_LINK (_object);
PinosLinkPrivate *priv = link->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);
break;
case PROP_POSSIBLE_FORMATS:
g_value_set_boxed (value, priv->possible_formats);
break;
case PROP_FORMAT:
g_value_set_boxed (value, priv->format);
break;
case PROP_PROPERTIES:
g_value_set_boxed (value, priv->properties);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (link, prop_id, pspec);
break;
}
}
static void
pinos_link_set_property (GObject *_object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
PinosLink *link = PINOS_LINK (_object);
PinosLinkPrivate *priv = link->priv;
switch (prop_id) {
case PROP_DAEMON:
priv->daemon = g_value_dup_object (value);
break;
case PROP_OBJECT_PATH:
priv->object_path = g_value_dup_string (value);
break;
case PROP_POSSIBLE_FORMATS:
if (priv->possible_formats)
g_bytes_unref (priv->possible_formats);
priv->possible_formats = g_value_dup_boxed (value);
break;
case PROP_FORMAT:
if (priv->format)
g_bytes_unref (priv->format);
priv->format = g_value_dup_boxed (value);
break;
case PROP_PROPERTIES:
if (priv->properties)
pinos_properties_free (priv->properties);
priv->properties = g_value_dup_boxed (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (link, prop_id, pspec);
break;
}
}
static void
link_register_object (PinosLink *link)
{
PinosLinkPrivate *priv = link->priv;
PinosObjectSkeleton *skel;
gchar *name;
name = g_strdup_printf (PINOS_DBUS_OBJECT_LINK);
skel = pinos_object_skeleton_new (name);
g_free (name);
pinos_object_skeleton_set_link1 (skel, priv->iface);
g_free (priv->object_path);
priv->object_path = pinos_daemon_export_uniquely (priv->daemon, G_DBUS_OBJECT_SKELETON (skel));
g_object_unref (skel);
g_debug ("link %p: register object %s", link, priv->object_path);
}
static void
link_unregister_object (PinosLink *link)
{
PinosLinkPrivate *priv = link->priv;
g_debug ("link %p: unregister object", link);
pinos_daemon_unexport (priv->daemon, priv->object_path);
}
static void
pinos_link_constructed (GObject * object)
{
PinosLink *link = PINOS_LINK (object);
g_debug ("link %p: constructed", link);
link_register_object (link);
G_OBJECT_CLASS (pinos_link_parent_class)->constructed (object);
}
static void
pinos_link_dispose (GObject * object)
{
PinosLink *link = PINOS_LINK (object);
g_debug ("link %p: dispose", link);
link_unregister_object (link);
G_OBJECT_CLASS (pinos_link_parent_class)->dispose (object);
}
static void
pinos_link_finalize (GObject * object)
{
PinosLink *link = PINOS_LINK (object);
PinosLinkPrivate *priv = link->priv;
g_debug ("link %p: finalize", link);
g_clear_pointer (&priv->possible_formats, g_bytes_unref);
g_clear_pointer (&priv->format, g_bytes_unref);
g_clear_pointer (&priv->properties, pinos_properties_free);
g_clear_object (&priv->daemon);
g_clear_object (&priv->iface);
g_free (priv->object_path);
G_OBJECT_CLASS (pinos_link_parent_class)->finalize (object);
}
static void
pinos_link_class_init (PinosLinkClass * klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
//PinosLinkClass *link_class = PINOS_LINK_CLASS (klass);
g_type_class_add_private (klass, sizeof (PinosLinkPrivate));
gobject_class->constructed = pinos_link_constructed;
gobject_class->dispose = pinos_link_dispose;
gobject_class->finalize = pinos_link_finalize;
gobject_class->set_property = pinos_link_set_property;
gobject_class->get_property = pinos_link_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_POSSIBLE_FORMATS,
g_param_spec_boxed ("possible-formats",
"Possible Formats",
"The possbile formats of the link",
G_TYPE_BYTES,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_FORMAT,
g_param_spec_boxed ("format",
"Format",
"The format of the link",
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 link",
PINOS_TYPE_PROPERTIES,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
signals[SIGNAL_REMOVE] = g_signal_new ("remove",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
0,
NULL,
NULL,
g_cclosure_marshal_generic,
G_TYPE_NONE,
0,
G_TYPE_NONE);
signals[SIGNAL_ACTIVATE] = g_signal_new ("activate",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
0,
NULL,
NULL,
g_cclosure_marshal_generic,
G_TYPE_NONE,
0,
G_TYPE_NONE);
signals[SIGNAL_DEACTIVATE] = g_signal_new ("deactivate",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
0,
NULL,
NULL,
g_cclosure_marshal_generic,
G_TYPE_NONE,
0,
G_TYPE_NONE);
}
static void
pinos_link_init (PinosLink * link)
{
PinosLinkPrivate *priv = link->priv = PINOS_LINK_GET_PRIVATE (link);
priv->iface = pinos_link1_skeleton_new ();
g_debug ("link %p: new", link);
}
/**
* pinos_link_remove:
* @link: a #PinosLink
*
* Trigger removal of @link
*/
void
pinos_link_remove (PinosLink *link)
{
g_return_if_fail (PINOS_IS_LINK (link));
g_debug ("link %p: remove", link);
g_signal_emit (link, signals[SIGNAL_REMOVE], 0, NULL);
}
/**
* pinos_link_get_object_path:
* @link: a #PinosLink
*
* Get the object patch of @link
*
* Returns: the object path of @source.
*/
const gchar *
pinos_link_get_object_path (PinosLink *link)
{
PinosLinkPrivate *priv;
g_return_val_if_fail (PINOS_IS_LINK (link), NULL);
priv = link->priv;
return priv->object_path;
}
/**
* pinos_link_get_possible_formats:
* @link: a #PinosLink
*
* Get the possible formats of @link
*
* Returns: the possible formats or %NULL
*/
GBytes *
pinos_link_get_possible_formats (PinosLink *link)
{
PinosLinkPrivate *priv;
g_return_val_if_fail (PINOS_IS_LINK (link), NULL);
priv = link->priv;
return priv->possible_formats;
}
/**
* pinos_link_get_format:
* @link: a #PinosLink
*
* Get the format of @link
*
* Returns: the format or %NULL
*/
GBytes *
pinos_link_get_format (PinosLink *link)
{
PinosLinkPrivate *priv;
g_return_val_if_fail (PINOS_IS_LINK (link), NULL);
priv = link->priv;
return priv->format;
}
/**
* pinos_link_get_properties:
* @link: a #PinosLink
*
* Get the properties of @link
*
* Returns: the properties or %NULL
*/
PinosProperties *
pinos_link_get_properties (PinosLink *link)
{
PinosLinkPrivate *priv;
g_return_val_if_fail (PINOS_IS_LINK (link), NULL);
priv = link->priv;
return priv->properties;
}

75
pinos/server/link.h Normal file
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_LINK_H__
#define __PINOS_LINK_H__
#include <glib-object.h>
G_BEGIN_DECLS
typedef struct _PinosLink PinosLink;
typedef struct _PinosLinkClass PinosLinkClass;
typedef struct _PinosLinkPrivate PinosLinkPrivate;
#include <pinos/server/daemon.h>
#define PINOS_TYPE_LINK (pinos_link_get_type ())
#define PINOS_IS_LINK(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PINOS_TYPE_LINK))
#define PINOS_IS_LINK_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PINOS_TYPE_LINK))
#define PINOS_LINK_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), PINOS_TYPE_LINK, PinosLinkClass))
#define PINOS_LINK(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), PINOS_TYPE_LINK, PinosLink))
#define PINOS_LINK_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PINOS_TYPE_LINK, PinosLinkClass))
#define PINOS_LINK_CAST(obj) ((PinosLink*)(obj))
#define PINOS_LINK_CLASS_CAST(klass)((PinosLinkClass*)(klass))
/**
* PinosLink:
*
* Pinos link object class.
*/
struct _PinosLink {
GObject object;
PinosLinkPrivate *priv;
};
/**
* PinosLinkClass:
*
* Pinos link object class.
*/
struct _PinosLinkClass {
GObjectClass parent_class;
};
/* normal GObject stuff */
GType pinos_link_get_type (void);
void pinos_link_remove (PinosLink *link);
GBytes * pinos_link_get_possible_formats (PinosLink *link);
GBytes * pinos_link_get_format (PinosLink *link);
PinosProperties * pinos_link_get_properties (PinosLink *link);
const gchar * pinos_link_get_object_path (PinosLink *link);
G_END_DECLS
#endif /* __PINOS_LINK_H__ */

View file

@ -619,7 +619,7 @@ pinos_port_create_channel (PinosPort *port,
return NULL;
g_debug ("port %p: make channel with formats %s", port,
g_bytes_get_data (possible_formats, NULL));
(gchar *) g_bytes_get_data (possible_formats, NULL));
channel = g_object_new (PINOS_TYPE_CHANNEL, "daemon", priv->daemon,
"port", port,