mirror of
https://gitlab.freedesktop.org/pipewire/pipewire
synced 2024-10-14 20:02:38 +00:00
reintroduce channels
Bring back the channel object. Making a node and port on the client side was rather awkward because of the async nature of many methods. It feels better to have a specific communication channel object to interface with a server side port. Use port activate/deactivate to start/stop streams Remove links from the ports. We let other objects install a callback on the port to receive and route buffers.
This commit is contained in:
parent
eefe6aacb9
commit
e167d30296
|
@ -35,6 +35,8 @@ Node1: a processing node, this can be a source, sink or transform
|
|||
/org/pinos/node*
|
||||
Port1: a port on a Node1, ports can be input or output ports
|
||||
/org/pinos/node*/port*
|
||||
Channel1: a communication channel between a client and port
|
||||
/org/pinos/client/channel*
|
||||
Link1: a link between 2 ports
|
||||
/org/pinos/link*
|
||||
|
||||
|
|
|
@ -180,6 +180,7 @@ lib_LTLIBRARIES = \
|
|||
# Public interface
|
||||
libpinos_@PINOS_MAJORMINOR@_la_SOURCES = \
|
||||
client/buffer.h client/buffer.c \
|
||||
client/io.c \
|
||||
client/context.h client/context.c \
|
||||
client/enumtypes.h client/enumtypes.c \
|
||||
client/introspect.h client/introspect.c \
|
||||
|
@ -204,10 +205,13 @@ lib_LTLIBRARIES += libpinoscore-@PINOS_MAJORMINOR@.la
|
|||
|
||||
# Pure core stuff
|
||||
libpinoscore_@PINOS_MAJORMINOR@_la_SOURCES = \
|
||||
server/channel.c server/channel.h \
|
||||
server/client.c server/client.h \
|
||||
server/daemon.c server/daemon.h \
|
||||
server/node.c server/node.h \
|
||||
server/port.c server/port.h \
|
||||
server/node-factory.c server/node-factory.h \
|
||||
server/utils.c server/utils.h \
|
||||
modules/gst/gst-manager.c modules/gst/gst-manager.h \
|
||||
modules/gst/gst-source.c modules/gst/gst-source.h \
|
||||
modules/gst/gst-sink.c modules/gst/gst-sink.h \
|
||||
|
|
|
@ -459,8 +459,8 @@ pinos_buffer_builder_init_full (PinosBufferBuilder *builder,
|
|||
sb->buf.max_size = sizeof (PinosStackHeader) + 128;
|
||||
sb->buf.data = g_malloc (sb->buf.max_size);
|
||||
sb->buf.free_data = sb->buf.data;
|
||||
g_warning ("builder %p: realloc buffer memory %"G_GSIZE_FORMAT" -> %"G_GSIZE_FORMAT,
|
||||
builder, max_data, sb->buf.max_size);
|
||||
// g_warning ("builder %p: realloc buffer memory %"G_GSIZE_FORMAT" -> %"G_GSIZE_FORMAT,
|
||||
// builder, max_data, sb->buf.max_size);
|
||||
} else {
|
||||
sb->buf.max_size = max_data;
|
||||
sb->buf.data = data;
|
||||
|
@ -581,8 +581,8 @@ pinos_buffer_builder_add_fd (PinosBufferBuilder *builder,
|
|||
|
||||
if (sb->buf.n_fds >= sb->buf.max_fds) {
|
||||
gint new_size = sb->buf.max_fds + 8;
|
||||
g_warning ("builder %p: realloc buffer fds %d -> %d",
|
||||
builder, sb->buf.max_fds, new_size);
|
||||
// g_warning ("builder %p: realloc buffer fds %d -> %d",
|
||||
// builder, sb->buf.max_fds, new_size);
|
||||
sb->buf.max_fds = new_size;
|
||||
sb->buf.free_fds = g_realloc (sb->buf.free_fds, new_size * sizeof (int));
|
||||
sb->buf.fds = sb->buf.free_fds;
|
||||
|
@ -599,8 +599,8 @@ builder_ensure_size (struct stack_builder *sb, gsize size)
|
|||
{
|
||||
if (sb->buf.size + size > sb->buf.max_size) {
|
||||
gsize new_size = sb->buf.size + MAX (size, 1024);
|
||||
g_warning ("builder %p: realloc buffer memory %"G_GSIZE_FORMAT" -> %"G_GSIZE_FORMAT,
|
||||
sb, sb->buf.max_size, new_size);
|
||||
// g_warning ("builder %p: realloc buffer memory %"G_GSIZE_FORMAT" -> %"G_GSIZE_FORMAT,
|
||||
// sb, sb->buf.max_size, new_size);
|
||||
sb->buf.max_size = new_size;
|
||||
sb->buf.free_data = g_realloc (sb->buf.free_data, new_size);
|
||||
sb->sh = sb->buf.data = sb->buf.free_data;
|
||||
|
|
|
@ -414,6 +414,13 @@ subscription_cb (PinosSubscribe *subscribe,
|
|||
else if (event == PINOS_SUBSCRIPTION_EVENT_REMOVE)
|
||||
priv->ports = g_list_remove (priv->ports, object);
|
||||
break;
|
||||
|
||||
case PINOS_SUBSCRIPTION_FLAG_CHANNEL:
|
||||
if (event == PINOS_SUBSCRIPTION_EVENT_NEW)
|
||||
priv->channels = g_list_prepend (priv->channels, object);
|
||||
else if (event == PINOS_SUBSCRIPTION_EVENT_REMOVE)
|
||||
priv->channels = g_list_remove (priv->channels, object);
|
||||
break;
|
||||
}
|
||||
|
||||
if (flags & priv->subscription_mask)
|
||||
|
|
|
@ -462,6 +462,143 @@ pinos_port_state_as_string (PinosPortState state)
|
|||
return val == NULL ? "invalid-state" : val->value_nick;
|
||||
}
|
||||
|
||||
/**
|
||||
* pinos_channel_state_as_string:
|
||||
* @state: a #PinosChannelState
|
||||
*
|
||||
* Return the string representation of @state.
|
||||
*
|
||||
* Returns: the string representation of @state.
|
||||
*/
|
||||
const gchar *
|
||||
pinos_channel_state_as_string (PinosChannelState state)
|
||||
{
|
||||
GEnumValue *val;
|
||||
|
||||
val = g_enum_get_value (G_ENUM_CLASS (g_type_class_ref (PINOS_TYPE_CHANNEL_STATE)),
|
||||
state);
|
||||
|
||||
return val == NULL ? "invalid-state" : val->value_nick;
|
||||
}
|
||||
|
||||
static void
|
||||
channel_fill_info (PinosChannelInfo *info, GDBusProxy *proxy)
|
||||
{
|
||||
GHashTable *changed = g_object_get_data (G_OBJECT (proxy), "pinos-changed-properties");
|
||||
|
||||
info->id = proxy;
|
||||
info->channel_path = g_dbus_proxy_get_object_path (proxy);
|
||||
SET_UINT32 ("Direction", direction, 2, PINOS_DIRECTION_INVALID);
|
||||
SET_STRING ("Client", client_path, 0);
|
||||
|
||||
info->change_mask = 0;
|
||||
SET_STRING ("Port", port_path, 0);
|
||||
SET_PROPERTIES ("Properties", properties, 1);
|
||||
SET_UINT32 ("State", state, 2, PINOS_CHANNEL_STATE_ERROR);
|
||||
SET_BYTES ("PossibleFormats", possible_formats, 3);
|
||||
SET_BYTES ("Format", format, 4);
|
||||
|
||||
if (changed)
|
||||
g_hash_table_remove_all (changed);
|
||||
}
|
||||
|
||||
static void
|
||||
channel_clear_info (PinosChannelInfo *info)
|
||||
{
|
||||
if (info->possible_formats)
|
||||
g_bytes_unref (info->possible_formats);
|
||||
if (info->format)
|
||||
g_bytes_unref (info->format);
|
||||
if (info->properties)
|
||||
pinos_properties_free (info->properties);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* pinos_context_list_channel_info:
|
||||
* @context: a connected #PinosContext
|
||||
* @flags: extra #PinosChannelInfoFlags
|
||||
* @cb: a #PinosChannelInfoCallback
|
||||
* @cancelable: a #GCancellable
|
||||
* @callback: a #GAsyncReadyCallback to call when the operation is finished
|
||||
* @user_data: user data passed to @cb
|
||||
*
|
||||
* Call @cb for each channel.
|
||||
*/
|
||||
void
|
||||
pinos_context_list_channel_info (PinosContext *context,
|
||||
PinosChannelInfoFlags flags,
|
||||
PinosChannelInfoCallback cb,
|
||||
GCancellable *cancellable,
|
||||
GAsyncReadyCallback callback,
|
||||
gpointer user_data)
|
||||
{
|
||||
PinosContextPrivate *priv;
|
||||
GList *walk;
|
||||
GTask *task;
|
||||
|
||||
g_return_if_fail (PINOS_IS_CONTEXT (context));
|
||||
g_return_if_fail (cb != NULL);
|
||||
|
||||
task = g_task_new (context, cancellable, callback, user_data);
|
||||
|
||||
priv = context->priv;
|
||||
|
||||
for (walk = priv->channels; walk; walk = g_list_next (walk)) {
|
||||
GDBusProxy *proxy = walk->data;
|
||||
PinosChannelInfo info;
|
||||
|
||||
channel_fill_info (&info, proxy);
|
||||
cb (context, &info, user_data);
|
||||
channel_clear_info (&info);
|
||||
}
|
||||
|
||||
g_task_return_boolean (task, TRUE);
|
||||
g_object_unref (task);
|
||||
}
|
||||
|
||||
/**
|
||||
* pinos_context_get_channel_info_by_id:
|
||||
* @context: a connected #PinosContext
|
||||
* @id: a channel id
|
||||
* @flags: extra #PinosChannelInfoFlags
|
||||
* @cb: a #PinosChannelInfoCallback
|
||||
* @cancelable: a #GCancellable
|
||||
* @callback: a #GAsyncReadyCallback to call when the operation is finished
|
||||
* @user_data: user data passed to @cb
|
||||
*
|
||||
* Call @cb for the channel with @id.
|
||||
*/
|
||||
void
|
||||
pinos_context_get_channel_info_by_id (PinosContext *context,
|
||||
gpointer id,
|
||||
PinosChannelInfoFlags flags,
|
||||
PinosChannelInfoCallback cb,
|
||||
GCancellable *cancellable,
|
||||
GAsyncReadyCallback callback,
|
||||
gpointer user_data)
|
||||
{
|
||||
PinosChannelInfo info;
|
||||
GDBusProxy *proxy;
|
||||
GTask *task;
|
||||
|
||||
g_return_if_fail (PINOS_IS_CONTEXT (context));
|
||||
g_return_if_fail (id != NULL);
|
||||
g_return_if_fail (cb != NULL);
|
||||
|
||||
task = g_task_new (context, cancellable, callback, user_data);
|
||||
|
||||
proxy = G_DBUS_PROXY (id);
|
||||
|
||||
channel_fill_info (&info, proxy);
|
||||
cb (context, &info, user_data);
|
||||
channel_clear_info (&info);
|
||||
|
||||
g_task_return_boolean (task, TRUE);
|
||||
g_object_unref (task);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
connection_fill_info (PinosConnectionInfo *info, GDBusProxy *proxy)
|
||||
{
|
||||
|
|
|
@ -270,6 +270,97 @@ void pinos_context_get_port_info_by_id (PinosContext *context,
|
|||
GCancellable *cancellable,
|
||||
GAsyncReadyCallback callback,
|
||||
gpointer user_data);
|
||||
|
||||
|
||||
/**
|
||||
* PinosChannelState:
|
||||
* @PINOS_CHANNEL_STATE_ERROR: the channel is in error
|
||||
* @PINOS_CHANNEL_STATE_STOPPED: the channel is stopped
|
||||
* @PINOS_CHANNEL_STATE_STARTING: the channel is starting
|
||||
* @PINOS_CHANNEL_STATE_STREAMING: the channel is streaming
|
||||
*
|
||||
* The different channel states
|
||||
*/
|
||||
typedef enum {
|
||||
PINOS_CHANNEL_STATE_ERROR = -1,
|
||||
PINOS_CHANNEL_STATE_STOPPED = 0,
|
||||
PINOS_CHANNEL_STATE_STARTING = 1,
|
||||
PINOS_CHANNEL_STATE_STREAMING = 2,
|
||||
} PinosChannelState;
|
||||
|
||||
const gchar * pinos_channel_state_as_string (PinosChannelState state);
|
||||
|
||||
/**
|
||||
* PinosChannelInfo:
|
||||
* @id: generic id of the channel_
|
||||
* @channel_path: the unique path of the channel
|
||||
* @direction: the channel direction
|
||||
* @client_path: the owner client
|
||||
* @change_mask: bitfield of changed fields since last call
|
||||
* @port_path: the owner port
|
||||
* @properties: the properties of the channel
|
||||
* @state: the state
|
||||
* @possible_formats: the possible formats
|
||||
* @format: when streaming, the current format
|
||||
*
|
||||
* The channel information. Extra information can be added in later
|
||||
* versions.
|
||||
*/
|
||||
typedef struct {
|
||||
gpointer id;
|
||||
const char *channel_path;
|
||||
PinosDirection direction;
|
||||
const char *client_path;
|
||||
guint64 change_mask;
|
||||
const char *port_path;
|
||||
PinosProperties *properties;
|
||||
PinosChannelState state;
|
||||
GBytes *possible_formats;
|
||||
GBytes *format;
|
||||
} PinosChannelInfo;
|
||||
|
||||
/**
|
||||
* PinosChannelInfoFlags:
|
||||
* @PINOS_CHANNEL_INFO_FLAGS_NONE: no flags
|
||||
* @PINOS_CHANNEL_INFO_FLAGS_NO_INPUT: don't list input channels
|
||||
* @PINOS_CHANNEL_INFO_FLAGS_NO_OUTPUT: don't list output channels
|
||||
*
|
||||
* Extra flags to pass to pinos_context_list_channel_info() and
|
||||
* pinos_context_get_channel_info_by_id().
|
||||
*/
|
||||
typedef enum {
|
||||
PINOS_CHANNEL_INFO_FLAGS_NONE = 0,
|
||||
PINOS_CHANNEL_INFO_FLAGS_NO_INPUT = (1 << 0),
|
||||
PINOS_CHANNEL_INFO_FLAGS_NO_OUTPUT = (1 << 1),
|
||||
} PinosChannelInfoFlags;
|
||||
|
||||
|
||||
/**
|
||||
* PinosChannelInfoCallback:
|
||||
* @c: a #PinosContext
|
||||
* @info: a #PinosChannelInfo
|
||||
* @user_data: user data
|
||||
*
|
||||
* Callback with information about the Pinos channel in @info.
|
||||
*/
|
||||
typedef void (*PinosChannelInfoCallback) (PinosContext *c,
|
||||
const PinosChannelInfo *info,
|
||||
gpointer user_data);
|
||||
|
||||
void pinos_context_list_channel_info (PinosContext *context,
|
||||
PinosChannelInfoFlags flags,
|
||||
PinosChannelInfoCallback cb,
|
||||
GCancellable *cancellable,
|
||||
GAsyncReadyCallback callback,
|
||||
gpointer user_data);
|
||||
void pinos_context_get_channel_info_by_id (PinosContext *context,
|
||||
gpointer id,
|
||||
PinosChannelInfoFlags flags,
|
||||
PinosChannelInfoCallback cb,
|
||||
GCancellable *cancellable,
|
||||
GAsyncReadyCallback callback,
|
||||
gpointer user_data);
|
||||
|
||||
/**
|
||||
* PinosConnectionInfo:
|
||||
* @id: generic id of the connection
|
||||
|
|
179
pinos/client/io.c
Normal file
179
pinos/client/io.c
Normal file
|
@ -0,0 +1,179 @@
|
|||
/* 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 <sys/socket.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <gio/gio.h>
|
||||
|
||||
#include "pinos/client/pinos.h"
|
||||
#include "pinos/client/private.h"
|
||||
|
||||
gboolean
|
||||
pinos_io_read_buffer (int fd,
|
||||
PinosBuffer *buffer,
|
||||
void *data,
|
||||
size_t max_data,
|
||||
int *fds,
|
||||
size_t max_fds,
|
||||
GError **error)
|
||||
{
|
||||
gssize len;
|
||||
PinosStackHeader *hdr;
|
||||
PinosStackBuffer *sb = (PinosStackBuffer *) buffer;
|
||||
gsize need;
|
||||
struct cmsghdr *cmsg;
|
||||
struct msghdr msg = {0};
|
||||
struct iovec iov[1];
|
||||
char cmsgbuf[CMSG_SPACE (max_fds * sizeof (int))];
|
||||
|
||||
g_assert (sb->refcount == 0);
|
||||
|
||||
sb->data = data;
|
||||
sb->max_size = max_data;
|
||||
sb->size = 0;
|
||||
sb->free_data = NULL;
|
||||
sb->fds = fds;
|
||||
sb->max_fds = max_fds;
|
||||
sb->n_fds = 0;
|
||||
sb->free_fds = NULL;
|
||||
|
||||
hdr = sb->data;
|
||||
|
||||
/* read header and control messages first */
|
||||
iov[0].iov_base = hdr;
|
||||
iov[0].iov_len = sizeof (PinosStackHeader);;
|
||||
msg.msg_iov = iov;
|
||||
msg.msg_iovlen = 1;
|
||||
msg.msg_control = cmsgbuf;
|
||||
msg.msg_controllen = sizeof (cmsgbuf);
|
||||
msg.msg_flags = MSG_CMSG_CLOEXEC;
|
||||
|
||||
while (TRUE) {
|
||||
len = recvmsg (fd, &msg, msg.msg_flags);
|
||||
if (len < 0) {
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
else
|
||||
goto recv_error;
|
||||
}
|
||||
break;
|
||||
}
|
||||
g_assert (len == sizeof (PinosStackHeader));
|
||||
|
||||
/* now we know the total length */
|
||||
need = sizeof (PinosStackHeader) + hdr->length;
|
||||
|
||||
if (sb->max_size < need) {
|
||||
g_warning ("io: realloc receive memory %" G_GSIZE_FORMAT" -> %" G_GSIZE_FORMAT, sb->max_size, need);
|
||||
sb->max_size = need;
|
||||
hdr = sb->data = sb->free_data = g_realloc (sb->free_data, need);
|
||||
}
|
||||
sb->size = need;
|
||||
|
||||
if (hdr->length > 0) {
|
||||
/* read data */
|
||||
while (TRUE) {
|
||||
len = recv (fd, (gchar *)sb->data + sizeof (PinosStackHeader), hdr->length, 0);
|
||||
if (len < 0) {
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
else
|
||||
goto recv_error;
|
||||
}
|
||||
break;
|
||||
}
|
||||
g_assert (len == hdr->length);
|
||||
}
|
||||
|
||||
/* handle control messages */
|
||||
for (cmsg = CMSG_FIRSTHDR (&msg); cmsg != NULL; cmsg = CMSG_NXTHDR (&msg, cmsg)) {
|
||||
if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS)
|
||||
continue;
|
||||
|
||||
sb->n_fds = (cmsg->cmsg_len - ((char *)CMSG_DATA (cmsg) - (char *)cmsg)) / sizeof (int);
|
||||
memcpy (sb->fds, CMSG_DATA (cmsg), sb->n_fds * sizeof (int));
|
||||
}
|
||||
sb->refcount = 1;
|
||||
sb->magic = PSB_MAGIC;
|
||||
|
||||
return TRUE;
|
||||
|
||||
/* ERRORS */
|
||||
recv_error:
|
||||
{
|
||||
g_set_error (error,
|
||||
G_IO_ERROR,
|
||||
g_io_error_from_errno (errno),
|
||||
"could not recvmsg: %s", strerror (errno));
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
gboolean
|
||||
pinos_io_write_buffer (int fd,
|
||||
PinosBuffer *buffer,
|
||||
GError **error)
|
||||
{
|
||||
PinosStackBuffer *sb = (PinosStackBuffer *) buffer;
|
||||
gssize len;
|
||||
struct msghdr msg = {0};
|
||||
struct iovec iov[1];
|
||||
struct cmsghdr *cmsg;
|
||||
char cmsgbuf[CMSG_SPACE (sb->n_fds * sizeof (int))];
|
||||
gint fds_len = sb->n_fds * sizeof (int);
|
||||
|
||||
iov[0].iov_base = sb->data;
|
||||
iov[0].iov_len = sb->size;
|
||||
msg.msg_iov = iov;
|
||||
msg.msg_iovlen = 1;
|
||||
msg.msg_control = cmsgbuf;
|
||||
msg.msg_controllen = CMSG_SPACE (fds_len);
|
||||
cmsg = CMSG_FIRSTHDR(&msg);
|
||||
cmsg->cmsg_level = SOL_SOCKET;
|
||||
cmsg->cmsg_type = SCM_RIGHTS;
|
||||
cmsg->cmsg_len = CMSG_LEN (fds_len);
|
||||
memcpy(CMSG_DATA(cmsg), sb->fds, fds_len);
|
||||
msg.msg_controllen = cmsg->cmsg_len;
|
||||
|
||||
while (TRUE) {
|
||||
len = sendmsg (fd, &msg, 0);
|
||||
if (len < 0) {
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
else
|
||||
goto send_error;
|
||||
}
|
||||
break;
|
||||
}
|
||||
g_assert (len == (gssize) sb->size);
|
||||
|
||||
return TRUE;
|
||||
|
||||
/* ERRORS */
|
||||
send_error:
|
||||
{
|
||||
g_set_error (error,
|
||||
G_IO_ERROR,
|
||||
g_io_error_from_errno (errno),
|
||||
"could not sendmsg: %s", strerror (errno));
|
||||
return FALSE;
|
||||
}
|
||||
}
|
|
@ -41,6 +41,7 @@ struct _PinosContextPrivate
|
|||
GList *nodes;
|
||||
GList *ports;
|
||||
GList *connections;
|
||||
GList *channels;
|
||||
};
|
||||
|
||||
void pinos_subscribe_get_proxy (PinosSubscribe *subscribe,
|
||||
|
@ -79,3 +80,14 @@ typedef struct {
|
|||
#define PSB_MAGIC ((gsize) 5493683301u)
|
||||
#define is_valid_buffer(b) (b != NULL && \
|
||||
PSB(b)->magic == PSB_MAGIC)
|
||||
|
||||
gboolean pinos_io_read_buffer (int fd,
|
||||
PinosBuffer *sb,
|
||||
void *data,
|
||||
size_t max_data,
|
||||
int *fds,
|
||||
size_t max_fds,
|
||||
GError **error);
|
||||
gboolean pinos_io_write_buffer (int fd,
|
||||
PinosBuffer *buffer,
|
||||
GError **error);
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
#include <sys/socket.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <gio/gio.h>
|
||||
#include <gio/gunixfdlist.h>
|
||||
#include <gio/gunixfdmessage.h>
|
||||
|
||||
|
@ -31,6 +32,9 @@
|
|||
|
||||
#include "pinos/client/private.h"
|
||||
|
||||
#define MAX_BUFFER_SIZE 1024
|
||||
#define MAX_FDS 16
|
||||
|
||||
struct _PinosStreamPrivate
|
||||
{
|
||||
PinosContext *context;
|
||||
|
@ -49,11 +53,18 @@ struct _PinosStreamPrivate
|
|||
|
||||
GBytes *format;
|
||||
|
||||
PinosNode *node;
|
||||
PinosPort *port;
|
||||
GDBusProxy *channel;
|
||||
gboolean disconnecting;
|
||||
|
||||
PinosStreamMode mode;
|
||||
GSocket *socket;
|
||||
GSource *socket_source;
|
||||
int fd;
|
||||
|
||||
PinosBuffer *buffer;
|
||||
PinosBuffer recv_buffer;
|
||||
guint8 recv_data[MAX_BUFFER_SIZE];
|
||||
int recv_fds[MAX_FDS];
|
||||
};
|
||||
|
||||
#define PINOS_STREAM_GET_PRIVATE(obj) \
|
||||
|
@ -184,100 +195,6 @@ stream_set_state (PinosStream *stream,
|
|||
}
|
||||
}
|
||||
|
||||
static GDBusProxy *
|
||||
get_proxy (PinosStream *stream, GList *list, const gchar *path)
|
||||
{
|
||||
GList *walk;
|
||||
|
||||
for (walk = list; walk; walk = g_list_next (walk)) {
|
||||
GDBusProxy *proxy = walk->data;
|
||||
|
||||
if (!g_strcmp0 (g_dbus_proxy_get_object_path (proxy), path)) {
|
||||
return proxy;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static GDBusProxy *
|
||||
get_port_proxy (PinosStream *stream, const gchar *path)
|
||||
{
|
||||
return get_proxy (stream, stream->priv->context->priv->ports, path);
|
||||
}
|
||||
|
||||
static GDBusProxy *
|
||||
get_node_proxy (PinosStream *stream, const gchar *path)
|
||||
{
|
||||
return get_proxy (stream, stream->priv->context->priv->nodes, path);
|
||||
}
|
||||
|
||||
static GDBusProxy *
|
||||
get_peer_port_proxy (PinosStream *stream)
|
||||
{
|
||||
PinosStreamPrivate *priv = stream->priv;
|
||||
GDBusProxy *port_proxy;
|
||||
GDBusProxy *peer_proxy = NULL;
|
||||
|
||||
if (priv->port == NULL)
|
||||
return NULL;
|
||||
|
||||
g_object_get (priv->port, "proxy", &port_proxy, NULL);
|
||||
if (port_proxy) {
|
||||
GVariant *v;
|
||||
|
||||
v = g_dbus_proxy_get_cached_property (port_proxy, "Peers");
|
||||
if (v) {
|
||||
GVariantIter *iter;
|
||||
gchar *peer_path;
|
||||
|
||||
g_variant_get (v, "ao", &iter);
|
||||
if (g_variant_iter_next (iter, "&o", &peer_path, NULL)) {
|
||||
peer_proxy = get_port_proxy (stream, peer_path);
|
||||
}
|
||||
g_variant_iter_free (iter);
|
||||
}
|
||||
}
|
||||
|
||||
return peer_proxy;
|
||||
}
|
||||
|
||||
static GDBusProxy *
|
||||
get_peer_node_proxy (PinosStream *stream)
|
||||
{
|
||||
GDBusProxy *peer_port;
|
||||
GVariant *v;
|
||||
GDBusProxy *node = NULL;
|
||||
|
||||
peer_port = get_peer_port_proxy (stream);
|
||||
if (peer_port) {
|
||||
v = g_dbus_proxy_get_cached_property (peer_port, "Node");
|
||||
if (v) {
|
||||
const gchar *node_path = g_variant_get_string (v, NULL);
|
||||
node = get_node_proxy (stream, node_path);
|
||||
g_variant_unref (v);
|
||||
}
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
static void
|
||||
merge_peer_properties (PinosStream *stream)
|
||||
{
|
||||
PinosStreamPrivate *priv = stream->priv;
|
||||
GDBusProxy *peer_node;
|
||||
|
||||
peer_node = get_peer_node_proxy (stream);
|
||||
if (peer_node) {
|
||||
GVariant *v = g_dbus_proxy_get_cached_property (peer_node, "Properties");
|
||||
if (v) {
|
||||
PinosProperties *props = pinos_properties_from_variant (v);
|
||||
priv->properties = pinos_properties_merge (priv->properties, props);
|
||||
pinos_properties_free (props);
|
||||
g_variant_unref (v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
subscription_cb (PinosSubscribe *subscribe,
|
||||
PinosSubscriptionEvent event,
|
||||
|
@ -289,28 +206,14 @@ subscription_cb (PinosSubscribe *subscribe,
|
|||
PinosStreamPrivate *priv = stream->priv;
|
||||
|
||||
switch (flags) {
|
||||
case PINOS_SUBSCRIPTION_FLAG_NODE:
|
||||
case PINOS_SUBSCRIPTION_FLAG_CHANNEL:
|
||||
if (event == PINOS_SUBSCRIPTION_EVENT_REMOVE) {
|
||||
if (object == priv->node && !priv->disconnecting) {
|
||||
if (object == priv->channel && !priv->disconnecting) {
|
||||
stream_set_state (stream,
|
||||
PINOS_STREAM_STATE_ERROR,
|
||||
g_error_new_literal (G_IO_ERROR,
|
||||
G_IO_ERROR_CLOSED,
|
||||
"Node disappeared"));
|
||||
}
|
||||
} else if (event == PINOS_SUBSCRIPTION_EVENT_NEW ||
|
||||
event == PINOS_SUBSCRIPTION_EVENT_CHANGE) {
|
||||
if (object == get_peer_node_proxy (stream)) {
|
||||
merge_peer_properties (stream);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case PINOS_SUBSCRIPTION_FLAG_PORT:
|
||||
if (event == PINOS_SUBSCRIPTION_EVENT_NEW ||
|
||||
event == PINOS_SUBSCRIPTION_EVENT_CHANGE) {
|
||||
if (object == get_peer_port_proxy (stream)) {
|
||||
merge_peer_properties (stream);
|
||||
"Channel disappeared"));
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
@ -342,7 +245,7 @@ pinos_stream_finalize (GObject * object)
|
|||
|
||||
g_debug ("free stream %p", stream);
|
||||
|
||||
g_clear_object (&priv->node);
|
||||
g_clear_object (&priv->channel);
|
||||
|
||||
if (priv->possible_formats)
|
||||
g_bytes_unref (priv->possible_formats);
|
||||
|
@ -577,12 +480,259 @@ pinos_stream_get_error (PinosStream *stream)
|
|||
return stream->priv->error;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
on_channel_proxy (GObject *source_object,
|
||||
GAsyncResult *res,
|
||||
gpointer user_data)
|
||||
{
|
||||
PinosStream *stream = user_data;
|
||||
PinosStreamPrivate *priv = stream->priv;
|
||||
PinosContext *context = priv->context;
|
||||
GVariant *v;
|
||||
gchar *str;
|
||||
GError *error = NULL;
|
||||
|
||||
priv->channel = pinos_subscribe_get_proxy_finish (context->priv->subscribe,
|
||||
res,
|
||||
&error);
|
||||
if (priv->channel == NULL)
|
||||
goto channel_failed;
|
||||
|
||||
/* get the port we are connected to */
|
||||
v = g_dbus_proxy_get_cached_property (priv->channel, "Port");
|
||||
if (v) {
|
||||
gsize len;
|
||||
str = g_variant_dup_string (v, &len);
|
||||
g_variant_unref (v);
|
||||
|
||||
g_free (priv->path);
|
||||
priv->path = str;
|
||||
}
|
||||
|
||||
v = g_dbus_proxy_get_cached_property (priv->channel, "PossibleFormats");
|
||||
if (v) {
|
||||
gsize len;
|
||||
str = g_variant_dup_string (v, &len);
|
||||
g_variant_unref (v);
|
||||
|
||||
if (priv->possible_formats)
|
||||
g_bytes_unref (priv->possible_formats);
|
||||
priv->possible_formats = g_bytes_new_take (str, len + 1);
|
||||
|
||||
g_object_notify (G_OBJECT (stream), "possible-formats");
|
||||
}
|
||||
v = g_dbus_proxy_get_cached_property (priv->channel, "Properties");
|
||||
if (v) {
|
||||
if (priv->properties)
|
||||
pinos_properties_free (priv->properties);
|
||||
priv->properties = pinos_properties_from_variant (v);
|
||||
g_variant_unref (v);
|
||||
|
||||
g_object_notify (G_OBJECT (stream), "properties");
|
||||
}
|
||||
|
||||
stream_set_state (stream, PINOS_STREAM_STATE_READY, NULL);
|
||||
g_object_unref (stream);
|
||||
|
||||
return;
|
||||
|
||||
channel_failed:
|
||||
{
|
||||
g_warning ("failed to get channel proxy: %s", error->message);
|
||||
stream_set_state (stream, PINOS_STREAM_STATE_ERROR, error);
|
||||
g_object_unref (stream);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static gboolean
|
||||
on_socket_condition (GSocket *socket,
|
||||
GIOCondition condition,
|
||||
gpointer user_data)
|
||||
{
|
||||
PinosStream *stream = user_data;
|
||||
PinosStreamPrivate *priv = stream->priv;
|
||||
|
||||
switch (condition) {
|
||||
case G_IO_IN:
|
||||
{
|
||||
PinosBuffer *buffer = &priv->recv_buffer;
|
||||
GError *error = NULL;
|
||||
|
||||
if (!pinos_io_read_buffer (priv->fd,
|
||||
buffer,
|
||||
priv->recv_data,
|
||||
MAX_BUFFER_SIZE,
|
||||
priv->recv_fds,
|
||||
MAX_FDS,
|
||||
&error)) {
|
||||
g_warning ("stream %p: failed to read buffer: %s", stream, error->message);
|
||||
g_clear_error (&error);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
priv->buffer = buffer;
|
||||
g_signal_emit (stream, signals[SIGNAL_NEW_BUFFER], 0, NULL);
|
||||
priv->buffer = NULL;
|
||||
|
||||
g_assert (pinos_buffer_unref (buffer) == FALSE);
|
||||
break;
|
||||
}
|
||||
|
||||
case G_IO_OUT:
|
||||
g_warning ("can do IO\n");
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static void
|
||||
handle_socket (PinosStream *stream, gint fd)
|
||||
{
|
||||
PinosStreamPrivate *priv = stream->priv;
|
||||
GError *error = NULL;
|
||||
|
||||
priv->socket = g_socket_new_from_fd (fd, &error);
|
||||
if (priv->socket == NULL)
|
||||
goto socket_failed;
|
||||
|
||||
switch (priv->mode) {
|
||||
case PINOS_STREAM_MODE_SOCKET:
|
||||
g_object_notify (G_OBJECT (stream), "socket");
|
||||
break;
|
||||
|
||||
case PINOS_STREAM_MODE_BUFFER:
|
||||
{
|
||||
priv->fd = g_socket_get_fd (priv->socket);
|
||||
priv->socket_source = g_socket_create_source (priv->socket, G_IO_IN, NULL);
|
||||
g_source_set_callback (priv->socket_source, (GSourceFunc) on_socket_condition, stream, NULL);
|
||||
g_source_attach (priv->socket_source, priv->context->priv->context);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return;
|
||||
|
||||
/* ERRORS */
|
||||
socket_failed:
|
||||
{
|
||||
g_warning ("failed to create socket: %s", error->message);
|
||||
stream_set_state (stream, PINOS_STREAM_STATE_ERROR, error);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
unhandle_socket (PinosStream *stream)
|
||||
{
|
||||
PinosStreamPrivate *priv = stream->priv;
|
||||
|
||||
switch (priv->mode) {
|
||||
case PINOS_STREAM_MODE_SOCKET:
|
||||
g_clear_object (&priv->socket);
|
||||
g_object_notify (G_OBJECT (stream), "socket");
|
||||
break;
|
||||
|
||||
case PINOS_STREAM_MODE_BUFFER:
|
||||
if (priv->socket_source) {
|
||||
g_source_destroy (priv->socket_source);
|
||||
g_clear_pointer (&priv->socket_source, g_source_unref);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
on_channel_created (GObject *source_object,
|
||||
GAsyncResult *res,
|
||||
gpointer user_data)
|
||||
{
|
||||
PinosStream *stream = user_data;
|
||||
PinosStreamPrivate *priv = stream->priv;
|
||||
PinosContext *context = priv->context;
|
||||
GVariant *ret;
|
||||
GError *error = NULL;
|
||||
const gchar *channel_path;
|
||||
GUnixFDList *fd_list;
|
||||
gint fd_idx, fd;
|
||||
|
||||
g_assert (context->priv->daemon == G_DBUS_PROXY (source_object));
|
||||
|
||||
ret = g_dbus_proxy_call_with_unix_fd_list_finish (context->priv->daemon,
|
||||
&fd_list,
|
||||
res, &error);
|
||||
if (ret == NULL)
|
||||
goto create_failed;
|
||||
|
||||
g_variant_get (ret, "(&oh)", &channel_path, &fd_idx);
|
||||
g_variant_unref (ret);
|
||||
|
||||
if ((fd = g_unix_fd_list_get (fd_list, fd_idx, &error)) < 0)
|
||||
goto fd_failed;
|
||||
|
||||
priv->fd = fd;
|
||||
|
||||
g_object_unref (fd_list);
|
||||
|
||||
pinos_subscribe_get_proxy (context->priv->subscribe,
|
||||
PINOS_DBUS_SERVICE,
|
||||
channel_path,
|
||||
"org.pinos.Channel1",
|
||||
NULL,
|
||||
on_channel_proxy,
|
||||
stream);
|
||||
|
||||
return;
|
||||
|
||||
/* ERRORS */
|
||||
create_failed:
|
||||
{
|
||||
g_warning ("failed to connect: %s", error->message);
|
||||
goto exit_error;
|
||||
}
|
||||
fd_failed:
|
||||
{
|
||||
g_warning ("failed to get FD: %s", error->message);
|
||||
g_object_unref (fd_list);
|
||||
goto exit_error;
|
||||
}
|
||||
exit_error:
|
||||
{
|
||||
stream_set_state (stream, PINOS_STREAM_STATE_ERROR, error);
|
||||
g_object_unref (stream);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static gboolean
|
||||
do_connect (PinosStream *stream)
|
||||
{
|
||||
PinosStreamPrivate *priv = stream->priv;
|
||||
PinosContext *context = priv->context;
|
||||
|
||||
g_dbus_proxy_call (context->priv->daemon,
|
||||
"CreateChannel",
|
||||
g_variant_new ("(sus@a{sv})",
|
||||
(priv->path ? priv->path : ""),
|
||||
priv->direction,
|
||||
g_bytes_get_data (priv->possible_formats, NULL),
|
||||
pinos_properties_to_variant (priv->properties)),
|
||||
G_DBUS_CALL_FLAGS_NONE,
|
||||
-1,
|
||||
NULL, /* GCancellable *cancellable */
|
||||
on_channel_created,
|
||||
stream);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
@ -633,11 +783,76 @@ pinos_stream_connect (PinosStream *stream,
|
|||
return TRUE;
|
||||
}
|
||||
|
||||
static void
|
||||
on_stream_started (GObject *source_object,
|
||||
GAsyncResult *res,
|
||||
gpointer user_data)
|
||||
{
|
||||
PinosStream *stream = user_data;
|
||||
PinosStreamPrivate *priv = stream->priv;
|
||||
gchar *format;
|
||||
GError *error = NULL;
|
||||
GVariant *result, *properties;
|
||||
|
||||
result = g_dbus_proxy_call_finish (priv->channel,
|
||||
res,
|
||||
&error);
|
||||
if (result == NULL)
|
||||
goto start_failed;
|
||||
|
||||
g_variant_get (result,
|
||||
"(s@a{sv})",
|
||||
&format,
|
||||
&properties);
|
||||
|
||||
g_variant_unref (result);
|
||||
|
||||
if (priv->format)
|
||||
g_bytes_unref (priv->format);
|
||||
priv->format = g_bytes_new_take (format, strlen (format) + 1);
|
||||
g_object_notify (G_OBJECT (stream), "format");
|
||||
|
||||
if (priv->properties)
|
||||
pinos_properties_free (priv->properties);
|
||||
priv->properties = pinos_properties_from_variant (properties);
|
||||
g_variant_unref (properties);
|
||||
g_object_notify (G_OBJECT (stream), "properties");
|
||||
|
||||
stream_set_state (stream, PINOS_STREAM_STATE_STREAMING, NULL);
|
||||
g_object_unref (stream);
|
||||
|
||||
return;
|
||||
|
||||
/* ERRORS */
|
||||
start_failed:
|
||||
{
|
||||
g_warning ("failed to start: %s", error->message);
|
||||
goto exit_error;
|
||||
}
|
||||
exit_error:
|
||||
{
|
||||
stream_set_state (stream, PINOS_STREAM_STATE_ERROR, error);
|
||||
g_object_unref (stream);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static gboolean
|
||||
do_start (PinosStream *stream)
|
||||
{
|
||||
stream_set_state (stream, PINOS_STREAM_STATE_STREAMING, NULL);
|
||||
g_object_unref (stream);
|
||||
PinosStreamPrivate *priv = stream->priv;
|
||||
|
||||
handle_socket (stream, priv->fd);
|
||||
|
||||
g_dbus_proxy_call (priv->channel,
|
||||
"Start",
|
||||
g_variant_new ("(s)",
|
||||
priv->format ? g_bytes_get_data (priv->format, NULL) : "ANY"),
|
||||
G_DBUS_CALL_FLAGS_NONE,
|
||||
-1,
|
||||
NULL, /* GCancellable *cancellable */
|
||||
on_stream_started,
|
||||
stream);
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
@ -683,11 +898,54 @@ pinos_stream_start (PinosStream *stream,
|
|||
return TRUE;
|
||||
}
|
||||
|
||||
static void
|
||||
on_stream_stopped (GObject *source_object,
|
||||
GAsyncResult *res,
|
||||
gpointer user_data)
|
||||
{
|
||||
PinosStream *stream = user_data;
|
||||
PinosStreamPrivate *priv = stream->priv;
|
||||
GVariant *ret;
|
||||
GError *error = NULL;
|
||||
|
||||
ret = g_dbus_proxy_call_finish (priv->channel, res, &error);
|
||||
if (ret == NULL)
|
||||
goto call_failed;
|
||||
|
||||
g_variant_unref (ret);
|
||||
|
||||
unhandle_socket (stream);
|
||||
g_clear_pointer (&priv->format, g_bytes_unref);
|
||||
g_object_notify (G_OBJECT (stream), "format");
|
||||
|
||||
stream_set_state (stream, PINOS_STREAM_STATE_READY, NULL);
|
||||
g_object_unref (stream);
|
||||
|
||||
return;
|
||||
|
||||
/* ERRORS */
|
||||
call_failed:
|
||||
{
|
||||
g_warning ("failed to stop: %s", error->message);
|
||||
stream_set_state (stream, PINOS_STREAM_STATE_ERROR, error);
|
||||
g_object_unref (stream);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static gboolean
|
||||
do_stop (PinosStream *stream)
|
||||
{
|
||||
stream_set_state (stream, PINOS_STREAM_STATE_READY, NULL);
|
||||
g_object_unref (stream);
|
||||
PinosStreamPrivate *priv = stream->priv;
|
||||
|
||||
g_dbus_proxy_call (priv->channel,
|
||||
"Stop",
|
||||
g_variant_new ("()"),
|
||||
G_DBUS_CALL_FLAGS_NONE,
|
||||
-1,
|
||||
NULL, /* GCancellable *cancellable */
|
||||
on_stream_stopped,
|
||||
stream);
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
@ -717,11 +975,56 @@ pinos_stream_stop (PinosStream *stream)
|
|||
return TRUE;
|
||||
}
|
||||
|
||||
static void
|
||||
on_channel_removed (GObject *source_object,
|
||||
GAsyncResult *res,
|
||||
gpointer user_data)
|
||||
{
|
||||
PinosStream *stream = user_data;
|
||||
PinosStreamPrivate *priv = stream->priv;
|
||||
GVariant *ret;
|
||||
GError *error = NULL;
|
||||
|
||||
g_assert (priv->channel == G_DBUS_PROXY (source_object));
|
||||
|
||||
priv->disconnecting = FALSE;
|
||||
g_clear_object (&priv->channel);
|
||||
|
||||
ret = g_dbus_proxy_call_finish (G_DBUS_PROXY (source_object), res, &error);
|
||||
if (ret == NULL)
|
||||
goto proxy_failed;
|
||||
|
||||
g_variant_unref (ret);
|
||||
|
||||
stream_set_state (stream, PINOS_STREAM_STATE_UNCONNECTED, NULL);
|
||||
g_object_unref (stream);
|
||||
return;
|
||||
|
||||
/* ERRORS */
|
||||
proxy_failed:
|
||||
{
|
||||
g_warning ("failed to disconnect: %s", error->message);
|
||||
stream_set_state (stream, PINOS_STREAM_STATE_ERROR, error);
|
||||
g_object_unref (stream);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static gboolean
|
||||
do_disconnect (PinosStream *stream)
|
||||
{
|
||||
PinosStreamPrivate *priv = stream->priv;
|
||||
|
||||
g_dbus_proxy_call (priv->channel,
|
||||
"Remove",
|
||||
g_variant_new ("()"),
|
||||
G_DBUS_CALL_FLAGS_NONE,
|
||||
-1,
|
||||
NULL, /* GCancellable *cancellable */
|
||||
on_channel_removed,
|
||||
stream);
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
@ -742,7 +1045,7 @@ pinos_stream_disconnect (PinosStream *stream)
|
|||
g_return_val_if_fail (PINOS_IS_STREAM (stream), FALSE);
|
||||
priv = stream->priv;
|
||||
g_return_val_if_fail (priv->state >= PINOS_STREAM_STATE_READY, FALSE);
|
||||
g_return_val_if_fail (priv->node != NULL, FALSE);
|
||||
g_return_val_if_fail (priv->channel != NULL, FALSE);
|
||||
context = priv->context;
|
||||
g_return_val_if_fail (pinos_context_get_state (context) >= PINOS_CONTEXT_STATE_CONNECTED, FALSE);
|
||||
g_return_val_if_fail (!priv->disconnecting, FALSE);
|
||||
|
@ -770,12 +1073,10 @@ pinos_stream_peek_buffer (PinosStream *stream)
|
|||
{
|
||||
PinosStreamPrivate *priv;
|
||||
|
||||
g_return_val_if_fail (PINOS_IS_STREAM (stream), FALSE);
|
||||
|
||||
g_return_val_if_fail (PINOS_IS_STREAM (stream), NULL);
|
||||
priv = stream->priv;
|
||||
//g_return_val_if_fail (priv->state == PINOS_STREAM_STATE_STREAMING, FALSE);
|
||||
|
||||
return NULL;
|
||||
return priv->buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -790,11 +1091,9 @@ pinos_stream_peek_buffer (PinosStream *stream)
|
|||
void
|
||||
pinos_stream_buffer_builder_init (PinosStream *stream, PinosBufferBuilder *builder)
|
||||
{
|
||||
PinosStreamPrivate *priv;
|
||||
|
||||
g_return_if_fail (PINOS_IS_STREAM (stream));
|
||||
priv = stream->priv;
|
||||
|
||||
pinos_buffer_builder_init (builder);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -825,5 +1124,10 @@ pinos_stream_send_buffer (PinosStream *stream,
|
|||
priv = stream->priv;
|
||||
g_return_val_if_fail (priv->state == PINOS_STREAM_STATE_STREAMING, FALSE);
|
||||
|
||||
if (!pinos_io_write_buffer (priv->fd, buffer, &error)) {
|
||||
g_warning ("stream %p: failed to read buffer: %s", stream, error->message);
|
||||
g_clear_error (&error);
|
||||
return FALSE;
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
|
|
@ -59,6 +59,7 @@ typedef enum {
|
|||
typedef enum {
|
||||
PINOS_STREAM_MODE_SOCKET = 0,
|
||||
PINOS_STREAM_MODE_BUFFER = 1,
|
||||
PINOS_STREAM_MODE_RINGBUFFER = 2,
|
||||
} PinosStreamMode;
|
||||
|
||||
/**
|
||||
|
|
|
@ -108,6 +108,9 @@ notify_event (PinosSubscribe *subscribe,
|
|||
else if (g_strcmp0 (interface_name, "org.pinos.Port1") == 0) {
|
||||
flags = PINOS_SUBSCRIPTION_FLAG_PORT;
|
||||
}
|
||||
else if (g_strcmp0 (interface_name, "org.pinos.Channel1") == 0) {
|
||||
flags = PINOS_SUBSCRIPTION_FLAG_CHANNEL;
|
||||
}
|
||||
g_signal_emit (subscribe, signals[SIGNAL_SUBSCRIPTION_EVENT], 0,
|
||||
event, flags, data->proxy);
|
||||
}
|
||||
|
|
|
@ -47,10 +47,11 @@ typedef enum {
|
|||
typedef enum {
|
||||
PINOS_SUBSCRIPTION_FLAG_DAEMON = (1 << 0),
|
||||
PINOS_SUBSCRIPTION_FLAG_NODE = (1 << 1),
|
||||
PINOS_SUBSCRIPTION_FLAG_PORT = (1 << 2)
|
||||
PINOS_SUBSCRIPTION_FLAG_PORT = (1 << 2),
|
||||
PINOS_SUBSCRIPTION_FLAG_CHANNEL = (1 << 3)
|
||||
} PinosSubscriptionFlags;
|
||||
|
||||
#define PINOS_SUBSCRIPTION_FLAGS_ALL 0x07
|
||||
#define PINOS_SUBSCRIPTION_FLAGS_ALL 0x0f
|
||||
|
||||
typedef enum {
|
||||
PINOS_SUBSCRIPTION_EVENT_NEW = 0,
|
||||
|
|
|
@ -69,6 +69,12 @@
|
|||
|
||||
</interface>
|
||||
|
||||
<interface name='org.pinos.Client1'>
|
||||
<!-- Sender: Sender of the client -->
|
||||
<property name='Sender' type='s' access='read' />
|
||||
<!-- Name: Properties of the client -->
|
||||
<property name='Properties' type='a{sv}' access='read' />
|
||||
</interface>
|
||||
|
||||
<!--
|
||||
org.pinos.Channel:
|
||||
|
|
|
@ -74,18 +74,15 @@ gst_pinos_port_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
|
|||
return TRUE;
|
||||
}
|
||||
|
||||
static void
|
||||
on_received_buffer (PinosPort *port, gpointer user_data)
|
||||
static gboolean
|
||||
on_received_buffer (PinosPort *port, PinosBuffer *pbuf, GError **error, gpointer user_data)
|
||||
{
|
||||
GstPinosPortSink *this = user_data;
|
||||
GstEvent *ev;
|
||||
PinosBuffer *pbuf;
|
||||
PinosBufferIter it;
|
||||
PinosBufferBuilder b;
|
||||
gboolean have_out = FALSE;
|
||||
|
||||
pbuf = pinos_port_peek_buffer (port);
|
||||
|
||||
if (this->pinos_input) {
|
||||
pinos_buffer_builder_init (&b);
|
||||
}
|
||||
|
@ -139,6 +136,7 @@ on_received_buffer (PinosPort *port, gpointer user_data)
|
|||
pinos_buffer_builder_clear (&b);
|
||||
}
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static void
|
||||
|
@ -230,7 +228,7 @@ gst_pinos_port_sink_setcaps (GstBaseSink * bsink, GstCaps * caps)
|
|||
PinosBuffer pbuf;
|
||||
PinosPacketFormatChange fc;
|
||||
|
||||
pinos_port_buffer_builder_init (this->port, &builder);
|
||||
pinos_buffer_builder_init (&builder);
|
||||
fc.id = 0;
|
||||
fc.format = cstr = gst_caps_to_string (caps);
|
||||
pinos_buffer_builder_add_format_change (&builder, &fc);
|
||||
|
@ -308,7 +306,7 @@ gst_pinos_port_sink_render_other (GstPinosPortSink * this, GstBuffer * buffer)
|
|||
hdr.pts = GST_BUFFER_PTS (buffer) + GST_ELEMENT_CAST (this)->base_time;
|
||||
hdr.dts_offset = 0;
|
||||
|
||||
pinos_port_buffer_builder_init (this->port, &builder);
|
||||
pinos_buffer_builder_init (&builder);
|
||||
pinos_buffer_builder_add_header (&builder, &hdr);
|
||||
|
||||
fdmem = gst_pinos_port_sink_get_fd_memory (this, buffer, &tmpfile);
|
||||
|
|
|
@ -99,7 +99,7 @@ fdpayload_data_destroy (gpointer user_data)
|
|||
|
||||
GST_DEBUG_OBJECT (this, "destroy %d", r.id);
|
||||
|
||||
pinos_port_buffer_builder_init (this->port, &b);
|
||||
pinos_buffer_builder_init (&b);
|
||||
pinos_buffer_builder_add_release_fd_payload (&b, &r);
|
||||
pinos_buffer_builder_end (&b, &pbuf);
|
||||
|
||||
|
@ -110,17 +110,17 @@ fdpayload_data_destroy (gpointer user_data)
|
|||
g_slice_free (FDPayloadData, data);
|
||||
}
|
||||
|
||||
static void
|
||||
on_received_buffer (PinosPort *port,
|
||||
gpointer user_data)
|
||||
static gboolean
|
||||
on_received_buffer (PinosPort *port,
|
||||
PinosBuffer *pbuf,
|
||||
GError **error,
|
||||
gpointer user_data)
|
||||
{
|
||||
GstPinosPortSrc *this = user_data;
|
||||
PinosBuffer *pbuf;
|
||||
PinosBufferIter it;
|
||||
GstBuffer *buf = NULL;
|
||||
|
||||
GST_LOG_OBJECT (this, "got new buffer");
|
||||
pbuf = pinos_port_peek_buffer (port);
|
||||
|
||||
pinos_buffer_iter_init (&it, pbuf);
|
||||
while (pinos_buffer_iter_next (&it)) {
|
||||
|
@ -199,7 +199,7 @@ on_received_buffer (PinosPort *port,
|
|||
g_cond_signal (&this->cond);
|
||||
}
|
||||
|
||||
return;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static void
|
||||
|
@ -462,7 +462,7 @@ gst_pinos_port_src_event (GstBaseSrc * src, GstEvent * event)
|
|||
refresh.request_type = all_headers ? 1 : 0;
|
||||
refresh.pts = running_time;
|
||||
|
||||
pinos_port_buffer_builder_init (this->port, &b);
|
||||
pinos_buffer_builder_init (&b);
|
||||
pinos_buffer_builder_add_refresh_request (&b, &refresh);
|
||||
pinos_buffer_builder_end (&b, &pbuf);
|
||||
|
||||
|
|
|
@ -275,34 +275,38 @@ static GstCaps *
|
|||
gst_pinos_src_src_fixate (GstBaseSrc * bsrc, GstCaps * caps)
|
||||
{
|
||||
GstStructure *structure;
|
||||
const gchar *name;
|
||||
|
||||
caps = gst_caps_make_writable (caps);
|
||||
|
||||
structure = gst_caps_get_structure (caps, 0);
|
||||
name = gst_structure_get_name (structure);
|
||||
|
||||
if (gst_structure_has_name (structure, "video/x-raw")) {
|
||||
if (g_str_has_prefix (name, "video/") || g_str_has_prefix (name, "image/")) {
|
||||
gst_structure_fixate_field_nearest_int (structure, "width", 320);
|
||||
gst_structure_fixate_field_nearest_int (structure, "height", 240);
|
||||
gst_structure_fixate_field_nearest_fraction (structure, "framerate", 30, 1);
|
||||
|
||||
if (gst_structure_has_field (structure, "pixel-aspect-ratio"))
|
||||
gst_structure_fixate_field_nearest_fraction (structure,
|
||||
"pixel-aspect-ratio", 1, 1);
|
||||
else
|
||||
gst_structure_set (structure, "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
|
||||
NULL);
|
||||
|
||||
if (gst_structure_has_field (structure, "colorimetry"))
|
||||
gst_structure_fixate_field_string (structure, "colorimetry", "bt601");
|
||||
if (gst_structure_has_field (structure, "chroma-site"))
|
||||
gst_structure_fixate_field_string (structure, "chroma-site", "mpeg2");
|
||||
if (strcmp (name, "video/x-raw") == 0) {
|
||||
if (gst_structure_has_field (structure, "pixel-aspect-ratio"))
|
||||
gst_structure_fixate_field_nearest_fraction (structure,
|
||||
"pixel-aspect-ratio", 1, 1);
|
||||
else
|
||||
gst_structure_set (structure, "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
|
||||
NULL);
|
||||
if (gst_structure_has_field (structure, "colorimetry"))
|
||||
gst_structure_fixate_field_string (structure, "colorimetry", "bt601");
|
||||
if (gst_structure_has_field (structure, "chroma-site"))
|
||||
gst_structure_fixate_field_string (structure, "chroma-site", "mpeg2");
|
||||
|
||||
if (gst_structure_has_field (structure, "interlace-mode"))
|
||||
gst_structure_fixate_field_string (structure, "interlace-mode",
|
||||
"progressive");
|
||||
else
|
||||
gst_structure_set (structure, "interlace-mode", G_TYPE_STRING,
|
||||
"progressive", NULL);
|
||||
if (gst_structure_has_field (structure, "interlace-mode"))
|
||||
gst_structure_fixate_field_string (structure, "interlace-mode",
|
||||
"progressive");
|
||||
else
|
||||
gst_structure_set (structure, "interlace-mode", G_TYPE_STRING,
|
||||
"progressive", NULL);
|
||||
}
|
||||
} else if (gst_structure_has_name (structure, "audio/x-raw")) {
|
||||
gst_structure_fixate_field_string (structure, "format", "S16LE");
|
||||
gst_structure_fixate_field_nearest_int (structure, "channels", 2);
|
||||
|
@ -562,9 +566,16 @@ gst_pinos_src_stream_start (GstPinosSrc *pinossrc, GstCaps * caps)
|
|||
}
|
||||
|
||||
g_object_get (pinossrc->stream, "properties", &props, NULL);
|
||||
g_object_get (pinossrc->stream, "format", &format, NULL);
|
||||
|
||||
pinos_main_loop_unlock (pinossrc->loop);
|
||||
|
||||
if (format) {
|
||||
caps = gst_caps_from_string (g_bytes_get_data (format, NULL));
|
||||
gst_base_src_set_caps (GST_BASE_SRC (pinossrc), caps);
|
||||
g_bytes_unref (format);
|
||||
}
|
||||
|
||||
parse_stream_properties (pinossrc, props);
|
||||
pinos_properties_free (props);
|
||||
|
||||
|
@ -600,6 +611,8 @@ gst_pinos_src_negotiate (GstBaseSrc * basesrc)
|
|||
GstCaps *caps = NULL;
|
||||
GstCaps *peercaps = NULL;
|
||||
gboolean result = FALSE;
|
||||
GBytes *possible;
|
||||
gchar *str;
|
||||
|
||||
/* first see what is possible on our source pad */
|
||||
thiscaps = gst_pad_query_caps (GST_BASE_SRC_PAD (basesrc), NULL);
|
||||
|
@ -622,62 +635,57 @@ gst_pinos_src_negotiate (GstBaseSrc * basesrc)
|
|||
/* no peer, work with our own caps then */
|
||||
caps = thiscaps;
|
||||
}
|
||||
if (caps && !gst_caps_is_empty (caps)) {
|
||||
GBytes *accepted;
|
||||
gchar *str;
|
||||
if (caps == NULL || gst_caps_is_empty (caps))
|
||||
goto no_common_caps;
|
||||
|
||||
GST_DEBUG_OBJECT (basesrc, "have caps: %" GST_PTR_FORMAT, caps);
|
||||
GST_DEBUG_OBJECT (basesrc, "have common caps: %" GST_PTR_FORMAT, caps);
|
||||
|
||||
/* open a connection with these caps */
|
||||
str = gst_caps_to_string (caps);
|
||||
accepted = g_bytes_new_take (str, strlen (str) + 1);
|
||||
|
||||
/* first disconnect */
|
||||
pinos_main_loop_lock (pinossrc->loop);
|
||||
if (pinos_stream_get_state (pinossrc->stream) != PINOS_STREAM_STATE_UNCONNECTED) {
|
||||
GST_DEBUG_OBJECT (basesrc, "disconnect capture");
|
||||
pinos_stream_disconnect (pinossrc->stream);
|
||||
while (TRUE) {
|
||||
PinosStreamState state = pinos_stream_get_state (pinossrc->stream);
|
||||
|
||||
if (state == PINOS_STREAM_STATE_UNCONNECTED)
|
||||
break;
|
||||
|
||||
if (state == PINOS_STREAM_STATE_ERROR) {
|
||||
g_bytes_unref (accepted);
|
||||
goto connect_error;
|
||||
}
|
||||
|
||||
pinos_main_loop_wait (pinossrc->loop);
|
||||
}
|
||||
}
|
||||
|
||||
GST_DEBUG_OBJECT (basesrc, "connect capture with path %s", pinossrc->path);
|
||||
pinos_stream_connect (pinossrc->stream,
|
||||
PINOS_DIRECTION_INPUT,
|
||||
pinossrc->path,
|
||||
PINOS_STREAM_FLAG_AUTOCONNECT,
|
||||
accepted);
|
||||
/* open a connection with these caps */
|
||||
str = gst_caps_to_string (caps);
|
||||
possible = g_bytes_new_take (str, strlen (str) + 1);
|
||||
|
||||
/* first disconnect */
|
||||
pinos_main_loop_lock (pinossrc->loop);
|
||||
if (pinos_stream_get_state (pinossrc->stream) != PINOS_STREAM_STATE_UNCONNECTED) {
|
||||
GST_DEBUG_OBJECT (basesrc, "disconnect capture");
|
||||
pinos_stream_disconnect (pinossrc->stream);
|
||||
while (TRUE) {
|
||||
PinosStreamState state = pinos_stream_get_state (pinossrc->stream);
|
||||
|
||||
if (state == PINOS_STREAM_STATE_READY)
|
||||
if (state == PINOS_STREAM_STATE_UNCONNECTED)
|
||||
break;
|
||||
|
||||
if (state == PINOS_STREAM_STATE_ERROR)
|
||||
if (state == PINOS_STREAM_STATE_ERROR) {
|
||||
g_bytes_unref (possible);
|
||||
goto connect_error;
|
||||
}
|
||||
|
||||
pinos_main_loop_wait (pinossrc->loop);
|
||||
}
|
||||
pinos_main_loop_unlock (pinossrc->loop);
|
||||
|
||||
result = gst_pinos_src_stream_start (pinossrc, NULL);
|
||||
} else {
|
||||
if (caps)
|
||||
gst_caps_unref (caps);
|
||||
GST_DEBUG_OBJECT (basesrc, "no common caps");
|
||||
}
|
||||
|
||||
GST_DEBUG_OBJECT (basesrc, "connect capture with path %s", pinossrc->path);
|
||||
pinos_stream_connect (pinossrc->stream,
|
||||
PINOS_DIRECTION_INPUT,
|
||||
pinossrc->path,
|
||||
PINOS_STREAM_FLAG_AUTOCONNECT,
|
||||
possible);
|
||||
|
||||
while (TRUE) {
|
||||
PinosStreamState state = pinos_stream_get_state (pinossrc->stream);
|
||||
|
||||
if (state == PINOS_STREAM_STATE_READY)
|
||||
break;
|
||||
|
||||
if (state == PINOS_STREAM_STATE_ERROR)
|
||||
goto connect_error;
|
||||
|
||||
pinos_main_loop_wait (pinossrc->loop);
|
||||
}
|
||||
pinos_main_loop_unlock (pinossrc->loop);
|
||||
|
||||
result = gst_pinos_src_stream_start (pinossrc, NULL);
|
||||
|
||||
pinossrc->negotiated = result;
|
||||
|
||||
return result;
|
||||
|
@ -696,7 +704,16 @@ no_caps:
|
|||
("This element did not produce valid caps"));
|
||||
if (thiscaps)
|
||||
gst_caps_unref (thiscaps);
|
||||
return TRUE;
|
||||
return FALSE;
|
||||
}
|
||||
no_common_caps:
|
||||
{
|
||||
GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
|
||||
("No supported formats found"),
|
||||
("This element does not have formats in common with the peer"));
|
||||
if (caps)
|
||||
gst_caps_unref (caps);
|
||||
return FALSE;
|
||||
}
|
||||
connect_error:
|
||||
{
|
||||
|
|
|
@ -362,17 +362,11 @@ set_property (GObject *object,
|
|||
}
|
||||
|
||||
static gboolean
|
||||
on_linked (PinosPort *port, PinosPort *peer, gpointer user_data)
|
||||
on_activate (PinosPort *port, gpointer user_data)
|
||||
{
|
||||
SourcePortData *data = user_data;
|
||||
PinosGstSource *source = data->source;
|
||||
PinosGstSourcePrivate *priv = source->priv;
|
||||
guint n_links;
|
||||
|
||||
pinos_port_get_links (port, &n_links);
|
||||
g_debug ("port %p: linked, now %d", port, n_links);
|
||||
if (n_links > 0)
|
||||
return TRUE;
|
||||
|
||||
if (priv->splitter) {
|
||||
data->peerpad = gst_element_get_request_pad (priv->splitter, "src_%u");
|
||||
|
@ -386,36 +380,32 @@ on_linked (PinosPort *port, PinosPort *peer, gpointer user_data)
|
|||
|
||||
pinos_node_report_busy (PINOS_NODE (source));
|
||||
|
||||
gst_element_set_state (data->sink, GST_STATE_PLAYING);
|
||||
if (data->convert) {
|
||||
gst_element_set_state (data->convert, GST_STATE_PLAYING);
|
||||
}
|
||||
gst_element_set_state (data->sink, GST_STATE_PLAYING);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static void
|
||||
on_unlinked (PinosPort *port, PinosPort *peer, gpointer user_data)
|
||||
on_deactivate (PinosPort *port, gpointer user_data)
|
||||
{
|
||||
SourcePortData *data = user_data;
|
||||
PinosGstSource *source = data->source;
|
||||
PinosGstSourcePrivate *priv = source->priv;
|
||||
guint n_links;
|
||||
|
||||
pinos_port_get_links (port, &n_links);
|
||||
g_debug ("port %p: unlinked, now %d", port, n_links);
|
||||
if (n_links > 0)
|
||||
return;
|
||||
|
||||
if (data->convert) {
|
||||
gst_element_set_state (data->convert, GST_STATE_NULL);
|
||||
}
|
||||
gst_element_set_state (data->sink, GST_STATE_NULL);
|
||||
|
||||
gst_pad_unlink (data->peerpad, data->sinkpad);
|
||||
if (priv->splitter)
|
||||
gst_element_release_request_pad (priv->splitter, data->peerpad);
|
||||
|
||||
gst_element_set_state (data->sink, GST_STATE_NULL);
|
||||
if (data->convert) {
|
||||
gst_element_set_state (data->convert, GST_STATE_NULL);
|
||||
}
|
||||
g_clear_object (&data->peerpad);
|
||||
|
||||
pinos_node_report_idle (PINOS_NODE (source));
|
||||
}
|
||||
|
||||
static void
|
||||
|
@ -588,8 +578,8 @@ add_port (PinosNode *node,
|
|||
->add_port (node, direction, error);
|
||||
|
||||
g_debug ("connecting signals");
|
||||
g_signal_connect (data->port, "linked", (GCallback) on_linked, data);
|
||||
g_signal_connect (data->port, "unlinked", (GCallback) on_unlinked, data);
|
||||
g_signal_connect (data->port, "activate", (GCallback) on_activate, data);
|
||||
g_signal_connect (data->port, "deactivate", (GCallback) on_deactivate, data);
|
||||
|
||||
data->sink = gst_element_factory_make ("pinosportsink", NULL);
|
||||
g_object_set (data->sink, "sync", TRUE,
|
||||
|
|
736
pinos/server/channel.c
Normal file
736
pinos/server/channel.c
Normal file
|
@ -0,0 +1,736 @@
|
|||
/* Pinos
|
||||
* Copyright (C) 2015 Wim Taymans <wim.taymans@gmail.com>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the
|
||||
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
||||
* Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <gio/gunixfdlist.h>
|
||||
|
||||
#include "pinos/client/pinos.h"
|
||||
#include "pinos/client/enumtypes.h"
|
||||
#include "pinos/client/private.h"
|
||||
|
||||
#include "pinos/server/daemon.h"
|
||||
#include "pinos/server/channel.h"
|
||||
#include "pinos/server/utils.h"
|
||||
|
||||
#include "pinos/dbus/org-pinos.h"
|
||||
|
||||
|
||||
#define MAX_BUFFER_SIZE 1024
|
||||
#define MAX_FDS 16
|
||||
|
||||
struct _PinosChannelPrivate
|
||||
{
|
||||
PinosDaemon *daemon;
|
||||
PinosChannel1 *iface;
|
||||
|
||||
gchar *object_path;
|
||||
gchar *client_path;
|
||||
PinosPort *port;
|
||||
PinosDirection direction;
|
||||
|
||||
GBytes *possible_formats;
|
||||
PinosProperties *properties;
|
||||
PinosChannelState state;
|
||||
GBytes *format;
|
||||
|
||||
gulong send_id;
|
||||
int fd;
|
||||
GSource *socket_source;
|
||||
GSocket *sockets[2];
|
||||
|
||||
PinosBuffer recv_buffer;
|
||||
|
||||
guint8 recv_data[MAX_BUFFER_SIZE];
|
||||
int recv_fds[MAX_FDS];
|
||||
|
||||
guint8 send_data[MAX_BUFFER_SIZE];
|
||||
int send_fds[MAX_FDS];
|
||||
|
||||
GSocket *socket;
|
||||
};
|
||||
|
||||
#define PINOS_CHANNEL_GET_PRIVATE(obj) \
|
||||
(G_TYPE_INSTANCE_GET_PRIVATE ((obj), PINOS_TYPE_CHANNEL, PinosChannelPrivate))
|
||||
|
||||
G_DEFINE_TYPE (PinosChannel, pinos_channel, G_TYPE_OBJECT);
|
||||
|
||||
enum
|
||||
{
|
||||
PROP_0,
|
||||
PROP_DAEMON,
|
||||
PROP_PORT,
|
||||
PROP_OBJECT_PATH,
|
||||
PROP_CLIENT_PATH,
|
||||
PROP_DIRECTION,
|
||||
PROP_POSSIBLE_FORMATS,
|
||||
PROP_PROPERTIES,
|
||||
PROP_FORMAT,
|
||||
PROP_SOCKET,
|
||||
PROP_STATE,
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
SIGNAL_REMOVE,
|
||||
LAST_SIGNAL
|
||||
};
|
||||
|
||||
static guint signals[LAST_SIGNAL] = { 0 };
|
||||
|
||||
static void
|
||||
pinos_channel_get_property (GObject *_object,
|
||||
guint prop_id,
|
||||
GValue *value,
|
||||
GParamSpec *pspec)
|
||||
{
|
||||
PinosChannel *channel = PINOS_CHANNEL (_object);
|
||||
PinosChannelPrivate *priv = channel->priv;
|
||||
|
||||
switch (prop_id) {
|
||||
case PROP_DAEMON:
|
||||
g_value_set_object (value, priv->daemon);
|
||||
break;
|
||||
|
||||
case PROP_PORT:
|
||||
g_value_set_object (value, priv->port);
|
||||
break;
|
||||
|
||||
case PROP_OBJECT_PATH:
|
||||
g_value_set_string (value, priv->object_path);
|
||||
break;
|
||||
|
||||
case PROP_CLIENT_PATH:
|
||||
g_value_set_string (value, priv->client_path);
|
||||
break;
|
||||
|
||||
case PROP_DIRECTION:
|
||||
g_value_set_enum (value, priv->direction);
|
||||
break;
|
||||
|
||||
case PROP_POSSIBLE_FORMATS:
|
||||
g_value_set_boxed (value, priv->possible_formats);
|
||||
break;
|
||||
|
||||
case PROP_PROPERTIES:
|
||||
g_value_set_boxed (value, priv->properties);
|
||||
break;
|
||||
|
||||
case PROP_FORMAT:
|
||||
g_value_set_boxed (value, priv->format);
|
||||
break;
|
||||
|
||||
case PROP_SOCKET:
|
||||
g_value_set_object (value, priv->socket);
|
||||
break;
|
||||
|
||||
case PROP_STATE:
|
||||
g_value_set_uint (value, priv->state);
|
||||
break;
|
||||
|
||||
default:
|
||||
G_OBJECT_WARN_INVALID_PROPERTY_ID (channel, prop_id, pspec);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
pinos_channel_set_property (GObject *_object,
|
||||
guint prop_id,
|
||||
const GValue *value,
|
||||
GParamSpec *pspec)
|
||||
{
|
||||
PinosChannel *channel = PINOS_CHANNEL (_object);
|
||||
PinosChannelPrivate *priv = channel->priv;
|
||||
|
||||
switch (prop_id) {
|
||||
case PROP_DAEMON:
|
||||
priv->daemon = g_value_dup_object (value);
|
||||
break;
|
||||
|
||||
case PROP_PORT:
|
||||
priv->port = g_value_dup_object (value);
|
||||
break;
|
||||
|
||||
case PROP_OBJECT_PATH:
|
||||
priv->object_path = g_value_dup_string (value);
|
||||
break;
|
||||
|
||||
case PROP_CLIENT_PATH:
|
||||
priv->client_path = g_value_dup_string (value);
|
||||
g_object_set (priv->iface, "client", priv->client_path, NULL);
|
||||
break;
|
||||
|
||||
case PROP_DIRECTION:
|
||||
priv->direction = g_value_get_enum (value);
|
||||
g_object_set (priv->iface, "direction", priv->direction, NULL);
|
||||
break;
|
||||
|
||||
case PROP_POSSIBLE_FORMATS:
|
||||
if (priv->possible_formats)
|
||||
g_bytes_unref (priv->possible_formats);
|
||||
priv->possible_formats = g_value_dup_boxed (value);
|
||||
g_object_set (priv->iface, "possible-formats",
|
||||
g_bytes_get_data (priv->possible_formats, NULL), NULL);
|
||||
break;
|
||||
|
||||
case PROP_PROPERTIES:
|
||||
if (priv->properties)
|
||||
pinos_properties_free (priv->properties);
|
||||
priv->properties = g_value_dup_boxed (value);
|
||||
g_object_set (priv->iface, "properties", priv->properties ?
|
||||
pinos_properties_to_variant (priv->properties) : NULL, NULL);
|
||||
break;
|
||||
|
||||
case PROP_FORMAT:
|
||||
if (priv->format)
|
||||
g_bytes_unref (priv->format);
|
||||
priv->format = g_value_dup_boxed (value);
|
||||
g_object_set (priv->iface, "format", priv->format ?
|
||||
g_bytes_get_data (priv->format, NULL) : NULL, NULL);
|
||||
break;
|
||||
|
||||
default:
|
||||
G_OBJECT_WARN_INVALID_PROPERTY_ID (channel, prop_id, pspec);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
clear_formats (PinosChannel *channel)
|
||||
{
|
||||
PinosChannelPrivate *priv = channel->priv;
|
||||
|
||||
g_debug ("channel %p: clear format", channel);
|
||||
|
||||
g_clear_pointer (&priv->format, g_bytes_unref);
|
||||
}
|
||||
|
||||
static void
|
||||
stop_transfer (PinosChannel *channel)
|
||||
{
|
||||
PinosChannelPrivate *priv = channel->priv;
|
||||
|
||||
g_debug ("channel %p: stop transfer", channel);
|
||||
|
||||
pinos_port_deactivate (priv->port);
|
||||
clear_formats (channel);
|
||||
priv->state = PINOS_CHANNEL_STATE_STOPPED;
|
||||
g_object_set (priv->iface,
|
||||
"state", priv->state,
|
||||
NULL);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static gboolean
|
||||
handle_start (PinosChannel1 *interface,
|
||||
GDBusMethodInvocation *invocation,
|
||||
const gchar *arg_requested_format,
|
||||
gpointer user_data)
|
||||
{
|
||||
PinosChannel *channel = user_data;
|
||||
PinosChannelPrivate *priv = channel->priv;
|
||||
GBytes *req_format, *format;
|
||||
const gchar *format_str;
|
||||
GError *error = NULL;
|
||||
|
||||
priv->state = PINOS_CHANNEL_STATE_STARTING;
|
||||
|
||||
req_format = g_bytes_new (arg_requested_format, strlen (arg_requested_format) + 1);
|
||||
|
||||
format = pinos_format_filter (priv->possible_formats, req_format, &error);
|
||||
if (format == NULL)
|
||||
goto no_format;
|
||||
|
||||
format_str = g_bytes_get_data (format, NULL);
|
||||
|
||||
g_debug ("channel %p: handle start, format %s", channel, format_str);
|
||||
|
||||
g_object_set (priv->port, "possible-formats", format, NULL);
|
||||
pinos_port_activate (priv->port);
|
||||
g_object_get (priv->port, "format", &format, NULL);
|
||||
if (format == NULL)
|
||||
goto no_format_activate;
|
||||
|
||||
format_str = g_bytes_get_data (format, NULL);
|
||||
|
||||
priv->state = PINOS_CHANNEL_STATE_STREAMING;
|
||||
g_debug ("channel %p: we are now streaming in format \"%s\"", channel, format_str);
|
||||
|
||||
g_dbus_method_invocation_return_value (invocation,
|
||||
g_variant_new ("(s@a{sv})",
|
||||
format_str,
|
||||
pinos_properties_to_variant (priv->properties)));
|
||||
g_object_set (priv->iface,
|
||||
"format", format_str,
|
||||
"state", priv->state,
|
||||
NULL);
|
||||
|
||||
return TRUE;
|
||||
|
||||
no_format:
|
||||
{
|
||||
g_debug ("no format found");
|
||||
g_dbus_method_invocation_return_gerror (invocation, error);
|
||||
g_clear_error (&error);
|
||||
return TRUE;
|
||||
}
|
||||
no_format_activate:
|
||||
{
|
||||
g_debug ("no format found when activating");
|
||||
g_dbus_method_invocation_return_dbus_error (invocation,
|
||||
"org.pinos.Error", "can't negotiate formats");
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
static gboolean
|
||||
handle_stop (PinosChannel1 *interface,
|
||||
GDBusMethodInvocation *invocation,
|
||||
gpointer user_data)
|
||||
{
|
||||
PinosChannel *channel = user_data;
|
||||
|
||||
g_debug ("channel %p: handle stop", channel);
|
||||
|
||||
stop_transfer (channel);
|
||||
|
||||
g_dbus_method_invocation_return_value (invocation, NULL);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
handle_remove (PinosChannel1 *interface,
|
||||
GDBusMethodInvocation *invocation,
|
||||
gpointer user_data)
|
||||
{
|
||||
PinosChannel *channel = user_data;
|
||||
|
||||
g_debug ("channel %p: handle remove", channel);
|
||||
stop_transfer (channel);
|
||||
|
||||
g_signal_emit (channel, signals[SIGNAL_REMOVE], 0, NULL);
|
||||
|
||||
g_dbus_method_invocation_return_value (invocation, NULL);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
on_send_buffer (PinosPort *port,
|
||||
PinosBuffer *buffer,
|
||||
GError **error,
|
||||
gpointer user_data)
|
||||
{
|
||||
PinosChannel *channel = user_data;
|
||||
PinosChannelPrivate *priv = channel->priv;
|
||||
gboolean res;
|
||||
|
||||
if (priv->state == PINOS_CHANNEL_STATE_STREAMING)
|
||||
res = pinos_io_write_buffer (priv->fd, buffer, error);
|
||||
else
|
||||
res = TRUE;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
on_socket_condition (GSocket *socket,
|
||||
GIOCondition condition,
|
||||
gpointer user_data)
|
||||
{
|
||||
PinosChannel *channel = user_data;
|
||||
PinosChannelPrivate *priv = channel->priv;
|
||||
|
||||
switch (condition) {
|
||||
case G_IO_IN:
|
||||
{
|
||||
PinosBuffer *buffer = &priv->recv_buffer;
|
||||
GError *error = NULL;
|
||||
|
||||
if (!pinos_io_read_buffer (priv->fd,
|
||||
buffer,
|
||||
priv->recv_data,
|
||||
MAX_BUFFER_SIZE,
|
||||
priv->recv_fds,
|
||||
MAX_FDS,
|
||||
&error)) {
|
||||
g_warning ("channel %p: failed to read buffer: %s", channel, error->message);
|
||||
g_clear_error (&error);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
if (!pinos_port_receive_buffer (priv->port, buffer, &error)) {
|
||||
g_warning ("channel %p: port %p failed to receive buffer: %s", channel, priv->port, error->message);
|
||||
g_clear_error (&error);
|
||||
}
|
||||
g_assert (pinos_buffer_unref (buffer) == FALSE);
|
||||
break;
|
||||
}
|
||||
|
||||
case G_IO_OUT:
|
||||
g_warning ("can do IO OUT\n");
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static void
|
||||
handle_socket (PinosChannel *channel, GSocket *socket)
|
||||
{
|
||||
PinosChannelPrivate *priv = channel->priv;
|
||||
GMainContext *context = g_main_context_get_thread_default();
|
||||
|
||||
g_debug ("channel %p: handle socket in context %p", channel, context);
|
||||
priv->fd = g_socket_get_fd (socket);
|
||||
priv->socket_source = g_socket_create_source (socket, G_IO_IN, NULL);
|
||||
g_source_set_callback (priv->socket_source, (GSourceFunc) on_socket_condition, channel, NULL);
|
||||
g_source_attach (priv->socket_source, context);
|
||||
}
|
||||
|
||||
static void
|
||||
unhandle_socket (PinosChannel *channel)
|
||||
{
|
||||
PinosChannelPrivate *priv = channel->priv;
|
||||
|
||||
g_debug ("channel %p: unhandle socket", channel);
|
||||
if (priv->socket_source) {
|
||||
g_source_destroy (priv->socket_source);
|
||||
g_clear_pointer (&priv->socket_source, g_source_unref);
|
||||
priv->fd = -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* pinos_channel_get_socket_pair:
|
||||
* @channel: a #PinosChannel
|
||||
* @error: a #GError
|
||||
*
|
||||
* Create or return a previously create socket pair for @channel. The
|
||||
* Socket for the other end is returned.
|
||||
*
|
||||
* Returns: a #GSocket that can be used to send/receive buffers to channel.
|
||||
*/
|
||||
GSocket *
|
||||
pinos_channel_get_socket_pair (PinosChannel *channel,
|
||||
GError **error)
|
||||
{
|
||||
PinosChannelPrivate *priv;
|
||||
|
||||
g_return_val_if_fail (PINOS_IS_CHANNEL (channel), FALSE);
|
||||
priv = channel->priv;
|
||||
|
||||
if (priv->sockets[1] == NULL) {
|
||||
int fd[2];
|
||||
|
||||
if (socketpair (AF_UNIX, SOCK_STREAM, 0, fd) != 0)
|
||||
goto no_sockets;
|
||||
|
||||
priv->sockets[0] = g_socket_new_from_fd (fd[0], error);
|
||||
if (priv->sockets[0] == NULL)
|
||||
goto create_failed;
|
||||
|
||||
priv->sockets[1] = g_socket_new_from_fd (fd[1], error);
|
||||
if (priv->sockets[1] == NULL)
|
||||
goto create_failed;
|
||||
|
||||
handle_socket (channel, priv->sockets[0]);
|
||||
}
|
||||
return g_object_ref (priv->sockets[1]);
|
||||
|
||||
/* ERRORS */
|
||||
no_sockets:
|
||||
{
|
||||
g_set_error (error,
|
||||
G_IO_ERROR,
|
||||
g_io_error_from_errno (errno),
|
||||
"could not create socketpair: %s", strerror (errno));
|
||||
return NULL;
|
||||
}
|
||||
create_failed:
|
||||
{
|
||||
g_clear_object (&priv->sockets[0]);
|
||||
g_clear_object (&priv->sockets[1]);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
channel_register_object (PinosChannel *channel)
|
||||
{
|
||||
PinosChannelPrivate *priv = channel->priv;
|
||||
PinosObjectSkeleton *skel;
|
||||
gchar *name;
|
||||
|
||||
priv->send_id = pinos_port_add_send_buffer_cb (priv->port, on_send_buffer, channel, NULL);
|
||||
|
||||
name = g_strdup_printf ("%s/channel", priv->client_path);
|
||||
skel = pinos_object_skeleton_new (name);
|
||||
g_free (name);
|
||||
|
||||
pinos_object_skeleton_set_channel1 (skel, priv->iface);
|
||||
|
||||
g_free (priv->object_path);
|
||||
priv->object_path = pinos_daemon_export_uniquely (priv->daemon, G_DBUS_OBJECT_SKELETON (skel));
|
||||
g_object_unref (skel);
|
||||
|
||||
g_debug ("channel %p: register object %s", channel, priv->object_path);
|
||||
}
|
||||
|
||||
static void
|
||||
channel_unregister_object (PinosChannel *channel)
|
||||
{
|
||||
PinosChannelPrivate *priv = channel->priv;
|
||||
|
||||
pinos_port_remove_send_buffer_cb (priv->port, priv->send_id);
|
||||
|
||||
g_debug ("channel %p: unregister object", channel);
|
||||
pinos_daemon_unexport (priv->daemon, priv->object_path);
|
||||
}
|
||||
|
||||
static void
|
||||
pinos_channel_dispose (GObject * object)
|
||||
{
|
||||
PinosChannel *channel = PINOS_CHANNEL (object);
|
||||
PinosChannelPrivate *priv = channel->priv;
|
||||
|
||||
g_debug ("channel %p: dispose", channel);
|
||||
pinos_port_deactivate (priv->port);
|
||||
clear_formats (channel);
|
||||
unhandle_socket (channel);
|
||||
g_clear_object (&priv->socket);
|
||||
channel_unregister_object (channel);
|
||||
|
||||
G_OBJECT_CLASS (pinos_channel_parent_class)->dispose (object);
|
||||
}
|
||||
|
||||
static void
|
||||
pinos_channel_finalize (GObject * object)
|
||||
{
|
||||
PinosChannel *channel = PINOS_CHANNEL (object);
|
||||
PinosChannelPrivate *priv = channel->priv;
|
||||
|
||||
g_debug ("channel %p: finalize", channel);
|
||||
if (priv->possible_formats)
|
||||
g_bytes_unref (priv->possible_formats);
|
||||
if (priv->properties)
|
||||
pinos_properties_free (priv->properties);
|
||||
g_clear_object (&priv->port);
|
||||
g_clear_object (&priv->daemon);
|
||||
g_clear_object (&priv->iface);
|
||||
g_free (priv->client_path);
|
||||
g_free (priv->object_path);
|
||||
|
||||
G_OBJECT_CLASS (pinos_channel_parent_class)->finalize (object);
|
||||
}
|
||||
|
||||
static void
|
||||
pinos_channel_constructed (GObject * object)
|
||||
{
|
||||
PinosChannel *channel = PINOS_CHANNEL (object);
|
||||
|
||||
g_debug ("channel %p: constructed", channel);
|
||||
channel_register_object (channel);
|
||||
|
||||
G_OBJECT_CLASS (pinos_channel_parent_class)->constructed (object);
|
||||
}
|
||||
static void
|
||||
pinos_channel_class_init (PinosChannelClass * klass)
|
||||
{
|
||||
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
||||
|
||||
g_type_class_add_private (klass, sizeof (PinosChannelPrivate));
|
||||
|
||||
gobject_class->constructed = pinos_channel_constructed;
|
||||
gobject_class->dispose = pinos_channel_dispose;
|
||||
gobject_class->finalize = pinos_channel_finalize;
|
||||
gobject_class->set_property = pinos_channel_set_property;
|
||||
gobject_class->get_property = pinos_channel_get_property;
|
||||
|
||||
g_object_class_install_property (gobject_class,
|
||||
PROP_DAEMON,
|
||||
g_param_spec_object ("daemon",
|
||||
"Daemon",
|
||||
"The Daemon",
|
||||
PINOS_TYPE_DAEMON,
|
||||
G_PARAM_READWRITE |
|
||||
G_PARAM_CONSTRUCT_ONLY |
|
||||
G_PARAM_STATIC_STRINGS));
|
||||
|
||||
g_object_class_install_property (gobject_class,
|
||||
PROP_PORT,
|
||||
g_param_spec_object ("port",
|
||||
"Port",
|
||||
"The Port",
|
||||
PINOS_TYPE_PORT,
|
||||
G_PARAM_READWRITE |
|
||||
G_PARAM_CONSTRUCT_ONLY |
|
||||
G_PARAM_STATIC_STRINGS));
|
||||
|
||||
g_object_class_install_property (gobject_class,
|
||||
PROP_OBJECT_PATH,
|
||||
g_param_spec_string ("object-path",
|
||||
"Object Path",
|
||||
"The object path",
|
||||
NULL,
|
||||
G_PARAM_READWRITE |
|
||||
G_PARAM_CONSTRUCT_ONLY |
|
||||
G_PARAM_STATIC_STRINGS));
|
||||
|
||||
g_object_class_install_property (gobject_class,
|
||||
PROP_CLIENT_PATH,
|
||||
g_param_spec_string ("client-path",
|
||||
"Client Path",
|
||||
"The client object path",
|
||||
NULL,
|
||||
G_PARAM_READWRITE |
|
||||
G_PARAM_CONSTRUCT_ONLY |
|
||||
G_PARAM_STATIC_STRINGS));
|
||||
|
||||
g_object_class_install_property (gobject_class,
|
||||
PROP_DIRECTION,
|
||||
g_param_spec_enum ("direction",
|
||||
"Direction",
|
||||
"The direction of the port",
|
||||
PINOS_TYPE_DIRECTION,
|
||||
PINOS_DIRECTION_INVALID,
|
||||
G_PARAM_READWRITE |
|
||||
G_PARAM_CONSTRUCT_ONLY |
|
||||
G_PARAM_STATIC_STRINGS));
|
||||
|
||||
g_object_class_install_property (gobject_class,
|
||||
PROP_POSSIBLE_FORMATS,
|
||||
g_param_spec_boxed ("possible-formats",
|
||||
"Possible Formats",
|
||||
"The possbile formats of the stream",
|
||||
G_TYPE_BYTES,
|
||||
G_PARAM_READWRITE |
|
||||
G_PARAM_CONSTRUCT |
|
||||
G_PARAM_STATIC_STRINGS));
|
||||
|
||||
g_object_class_install_property (gobject_class,
|
||||
PROP_PROPERTIES,
|
||||
g_param_spec_boxed ("properties",
|
||||
"Properties",
|
||||
"Extra properties of the stream",
|
||||
PINOS_TYPE_PROPERTIES,
|
||||
G_PARAM_READWRITE |
|
||||
G_PARAM_CONSTRUCT |
|
||||
G_PARAM_STATIC_STRINGS));
|
||||
|
||||
g_object_class_install_property (gobject_class,
|
||||
PROP_FORMAT,
|
||||
g_param_spec_boxed ("format",
|
||||
"Format",
|
||||
"The format of the stream",
|
||||
G_TYPE_BYTES,
|
||||
G_PARAM_READWRITE |
|
||||
G_PARAM_STATIC_STRINGS));
|
||||
|
||||
g_object_class_install_property (gobject_class,
|
||||
PROP_SOCKET,
|
||||
g_param_spec_object ("socket",
|
||||
"Socket",
|
||||
"The socket with data",
|
||||
G_TYPE_SOCKET,
|
||||
G_PARAM_READABLE |
|
||||
G_PARAM_STATIC_STRINGS));
|
||||
|
||||
signals[SIGNAL_REMOVE] = g_signal_new ("remove",
|
||||
G_TYPE_FROM_CLASS (klass),
|
||||
G_SIGNAL_RUN_LAST,
|
||||
0,
|
||||
NULL,
|
||||
NULL,
|
||||
g_cclosure_marshal_generic,
|
||||
G_TYPE_NONE,
|
||||
0,
|
||||
G_TYPE_NONE);
|
||||
}
|
||||
|
||||
static void
|
||||
pinos_channel_init (PinosChannel * channel)
|
||||
{
|
||||
PinosChannelPrivate *priv = channel->priv = PINOS_CHANNEL_GET_PRIVATE (channel);
|
||||
|
||||
priv->iface = pinos_channel1_skeleton_new ();
|
||||
g_signal_connect (priv->iface, "handle-start", (GCallback) handle_start, channel);
|
||||
g_signal_connect (priv->iface, "handle-stop", (GCallback) handle_stop, channel);
|
||||
g_signal_connect (priv->iface, "handle-remove", (GCallback) handle_remove, channel);
|
||||
|
||||
priv->state = PINOS_CHANNEL_STATE_STOPPED;
|
||||
g_object_set (priv->iface, "state", priv->state, NULL);
|
||||
|
||||
priv->direction = PINOS_DIRECTION_INVALID;
|
||||
|
||||
g_debug ("channel %p: new", channel);
|
||||
}
|
||||
|
||||
/**
|
||||
* pinos_channel_remove:
|
||||
* @channel: a #PinosChannel
|
||||
*
|
||||
* Remove @channel. This will stop the transfer on the channel and
|
||||
* free the resources allocated by @channel.
|
||||
*/
|
||||
void
|
||||
pinos_channel_remove (PinosChannel *channel)
|
||||
{
|
||||
g_debug ("channel %p: remove", channel);
|
||||
stop_transfer (channel);
|
||||
|
||||
g_signal_emit (channel, signals[SIGNAL_REMOVE], 0, NULL);
|
||||
}
|
||||
|
||||
const gchar *
|
||||
pinos_channel_get_client_path (PinosChannel *channel)
|
||||
{
|
||||
PinosChannelPrivate *priv;
|
||||
|
||||
g_return_val_if_fail (PINOS_IS_CHANNEL (channel), NULL);
|
||||
priv = channel->priv;
|
||||
|
||||
return priv->client_path;
|
||||
}
|
||||
|
||||
/**
|
||||
* pinos_channel_get_object_path:
|
||||
* @channel: a #PinosChannel
|
||||
*
|
||||
* Get the object patch of @channel
|
||||
*
|
||||
* Returns: the object path of @source.
|
||||
*/
|
||||
const gchar *
|
||||
pinos_channel_get_object_path (PinosChannel *channel)
|
||||
{
|
||||
PinosChannelPrivate *priv;
|
||||
|
||||
g_return_val_if_fail (PINOS_IS_CHANNEL (channel), NULL);
|
||||
priv = channel->priv;
|
||||
|
||||
return priv->object_path;
|
||||
}
|
73
pinos/server/channel.h
Normal file
73
pinos/server/channel.h
Normal file
|
@ -0,0 +1,73 @@
|
|||
/* Pinos
|
||||
* Copyright (C) 2015 Wim Taymans <wim.taymans@gmail.com>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the
|
||||
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
||||
* Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#ifndef __PINOS_CHANNEL_H__
|
||||
#define __PINOS_CHANNEL_H__
|
||||
|
||||
#include <glib-object.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
#define PINOS_TYPE_CHANNEL (pinos_channel_get_type ())
|
||||
#define PINOS_IS_CHANNEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PINOS_TYPE_CHANNEL))
|
||||
#define PINOS_IS_CHANNEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PINOS_TYPE_CHANNEL))
|
||||
#define PINOS_CHANNEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), PINOS_TYPE_CHANNEL, PinosChannelClass))
|
||||
#define PINOS_CHANNEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), PINOS_TYPE_CHANNEL, PinosChannel))
|
||||
#define PINOS_CHANNEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PINOS_TYPE_CHANNEL, PinosChannelClass))
|
||||
#define PINOS_CHANNEL_CAST(obj) ((PinosChannel*)(obj))
|
||||
#define PINOS_CHANNEL_CLASS_CAST(klass) ((PinosChannelClass*)(klass))
|
||||
|
||||
typedef struct _PinosChannel PinosChannel;
|
||||
typedef struct _PinosChannelClass PinosChannelClass;
|
||||
typedef struct _PinosChannelPrivate PinosChannelPrivate;
|
||||
|
||||
/**
|
||||
* PinosChannel:
|
||||
*
|
||||
* Pinos source channel object class.
|
||||
*/
|
||||
struct _PinosChannel {
|
||||
GObject object;
|
||||
|
||||
PinosChannelPrivate *priv;
|
||||
};
|
||||
|
||||
/**
|
||||
* PinosChannelClass:
|
||||
*
|
||||
* Pinos source channel object class.
|
||||
*/
|
||||
struct _PinosChannelClass {
|
||||
GObjectClass parent_class;
|
||||
};
|
||||
|
||||
/* normal GObject stuff */
|
||||
GType pinos_channel_get_type (void);
|
||||
|
||||
void pinos_channel_remove (PinosChannel *channel);
|
||||
|
||||
const gchar * pinos_channel_get_client_path (PinosChannel *channel);
|
||||
const gchar * pinos_channel_get_object_path (PinosChannel *channel);
|
||||
|
||||
GSocket * pinos_channel_get_socket_pair (PinosChannel *channel,
|
||||
GError **error);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /* __PINOS_CHANNEL_H__ */
|
447
pinos/server/client.c
Normal file
447
pinos/server/client.c
Normal file
|
@ -0,0 +1,447 @@
|
|||
/* Pinos
|
||||
* Copyright (C) 2015 Wim Taymans <wim.taymans@gmail.com>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the
|
||||
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
||||
* Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include "pinos/client/pinos.h"
|
||||
|
||||
#include "pinos/server/client.h"
|
||||
|
||||
#include "pinos/dbus/org-pinos.h"
|
||||
|
||||
struct _PinosClientPrivate
|
||||
{
|
||||
PinosDaemon *daemon;
|
||||
PinosClient1 *iface;
|
||||
|
||||
guint id;
|
||||
gchar *sender;
|
||||
gchar *object_path;
|
||||
PinosProperties *properties;
|
||||
|
||||
PinosFdManager *fdmanager;
|
||||
GList *objects;
|
||||
};
|
||||
|
||||
#define PINOS_CLIENT_GET_PRIVATE(obj) \
|
||||
(G_TYPE_INSTANCE_GET_PRIVATE ((obj), PINOS_TYPE_CLIENT, PinosClientPrivate))
|
||||
|
||||
G_DEFINE_TYPE (PinosClient, pinos_client, G_TYPE_OBJECT);
|
||||
|
||||
enum
|
||||
{
|
||||
PROP_0,
|
||||
PROP_DAEMON,
|
||||
PROP_SENDER,
|
||||
PROP_OBJECT_PATH,
|
||||
PROP_PROPERTIES,
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
SIGNAL_APPEARED,
|
||||
SIGNAL_VANISHED,
|
||||
SIGNAL_REMOVE,
|
||||
LAST_SIGNAL
|
||||
};
|
||||
|
||||
static guint signals[LAST_SIGNAL] = { 0 };
|
||||
|
||||
|
||||
static void
|
||||
client_name_appeared_handler (GDBusConnection *connection,
|
||||
const gchar *name,
|
||||
const gchar *name_owner,
|
||||
gpointer user_data)
|
||||
{
|
||||
PinosClient *client = user_data;
|
||||
|
||||
g_debug ("client %p: appeared %s %s", client, name, name_owner);
|
||||
|
||||
g_signal_emit (client, signals[SIGNAL_APPEARED], 0, NULL);
|
||||
}
|
||||
|
||||
static void
|
||||
client_name_vanished_handler (GDBusConnection *connection,
|
||||
const gchar *name,
|
||||
gpointer user_data)
|
||||
{
|
||||
PinosClient *client = user_data;
|
||||
PinosClientPrivate *priv = client->priv;
|
||||
|
||||
g_debug ("client %p: vanished %s", client, name);
|
||||
|
||||
g_signal_emit (client, signals[SIGNAL_VANISHED], 0, NULL);
|
||||
g_bus_unwatch_name (priv->id);
|
||||
}
|
||||
|
||||
static void
|
||||
client_watch_name (PinosClient *client)
|
||||
{
|
||||
PinosClientPrivate *priv = client->priv;
|
||||
GDBusConnection *connection;
|
||||
|
||||
g_object_get (priv->daemon, "connection", &connection, NULL);
|
||||
|
||||
priv->id = g_bus_watch_name_on_connection (connection,
|
||||
priv->sender,
|
||||
G_BUS_NAME_WATCHER_FLAGS_NONE,
|
||||
client_name_appeared_handler,
|
||||
client_name_vanished_handler,
|
||||
client,
|
||||
(GDestroyNotify) g_object_unref);
|
||||
}
|
||||
|
||||
static void
|
||||
pinos_client_get_property (GObject *_object,
|
||||
guint prop_id,
|
||||
GValue *value,
|
||||
GParamSpec *pspec)
|
||||
{
|
||||
PinosClient *client = PINOS_CLIENT (_object);
|
||||
PinosClientPrivate *priv = client->priv;
|
||||
|
||||
switch (prop_id) {
|
||||
case PROP_DAEMON:
|
||||
g_value_set_object (value, priv->daemon);
|
||||
break;
|
||||
|
||||
case PROP_SENDER:
|
||||
g_value_set_string (value, priv->sender);
|
||||
break;
|
||||
|
||||
case PROP_OBJECT_PATH:
|
||||
g_value_set_string (value, priv->object_path);
|
||||
break;
|
||||
|
||||
case PROP_PROPERTIES:
|
||||
g_value_set_boxed (value, priv->properties);
|
||||
break;
|
||||
|
||||
default:
|
||||
G_OBJECT_WARN_INVALID_PROPERTY_ID (client, prop_id, pspec);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
pinos_client_set_property (GObject *_object,
|
||||
guint prop_id,
|
||||
const GValue *value,
|
||||
GParamSpec *pspec)
|
||||
{
|
||||
PinosClient *client = PINOS_CLIENT (_object);
|
||||
PinosClientPrivate *priv = client->priv;
|
||||
|
||||
switch (prop_id) {
|
||||
case PROP_DAEMON:
|
||||
priv->daemon = g_value_dup_object (value);
|
||||
break;
|
||||
|
||||
case PROP_SENDER:
|
||||
priv->sender = g_value_dup_string (value);
|
||||
pinos_client1_set_sender (priv->iface, priv->sender);
|
||||
break;
|
||||
|
||||
case PROP_PROPERTIES:
|
||||
if (priv->properties)
|
||||
pinos_properties_free (priv->properties);
|
||||
priv->properties = g_value_dup_boxed (value);
|
||||
pinos_client1_set_properties (priv->iface, priv->properties ?
|
||||
pinos_properties_to_variant (priv->properties) : NULL);
|
||||
break;
|
||||
|
||||
default:
|
||||
G_OBJECT_WARN_INVALID_PROPERTY_ID (client, prop_id, pspec);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
client_register_object (PinosClient *client)
|
||||
{
|
||||
PinosClientPrivate *priv = client->priv;
|
||||
PinosDaemon *daemon = priv->daemon;
|
||||
PinosObjectSkeleton *skel;
|
||||
|
||||
skel = pinos_object_skeleton_new (PINOS_DBUS_OBJECT_CLIENT);
|
||||
|
||||
pinos_object_skeleton_set_client1 (skel, priv->iface);
|
||||
|
||||
g_free (priv->object_path);
|
||||
priv->object_path = pinos_daemon_export_uniquely (daemon, G_DBUS_OBJECT_SKELETON (skel));
|
||||
g_object_unref (skel);
|
||||
|
||||
g_debug ("client %p: register %s", client, priv->object_path);
|
||||
}
|
||||
|
||||
static void
|
||||
client_unregister_object (PinosClient *client)
|
||||
{
|
||||
PinosClientPrivate *priv = client->priv;
|
||||
PinosDaemon *daemon = priv->daemon;
|
||||
|
||||
g_debug ("client %p: unregister", client);
|
||||
pinos_daemon_unexport (daemon, priv->object_path);
|
||||
}
|
||||
|
||||
static void
|
||||
pinos_client_dispose (GObject * object)
|
||||
{
|
||||
PinosClient *client = PINOS_CLIENT (object);
|
||||
PinosClientPrivate *priv = client->priv;
|
||||
GList *copy;
|
||||
|
||||
g_debug ("client %p: dispose", client);
|
||||
pinos_fd_manager_remove_all (priv->fdmanager, priv->object_path);
|
||||
|
||||
copy = g_list_copy (priv->objects);
|
||||
g_list_free_full (copy, g_object_unref);
|
||||
|
||||
client_unregister_object (client);
|
||||
|
||||
G_OBJECT_CLASS (pinos_client_parent_class)->dispose (object);
|
||||
}
|
||||
|
||||
static void
|
||||
pinos_client_finalize (GObject * object)
|
||||
{
|
||||
PinosClient *client = PINOS_CLIENT (object);
|
||||
PinosClientPrivate *priv = client->priv;
|
||||
|
||||
g_debug ("client %p: finalize", client);
|
||||
g_clear_object (&priv->daemon);
|
||||
g_clear_object (&priv->iface);
|
||||
g_free (priv->sender);
|
||||
g_free (priv->object_path);
|
||||
if (priv->properties)
|
||||
pinos_properties_free (priv->properties);
|
||||
g_clear_object (&priv->fdmanager);
|
||||
|
||||
G_OBJECT_CLASS (pinos_client_parent_class)->finalize (object);
|
||||
}
|
||||
|
||||
static void
|
||||
pinos_client_constructed (GObject * object)
|
||||
{
|
||||
PinosClient *client = PINOS_CLIENT (object);
|
||||
|
||||
g_debug ("client %p: constructed", client);
|
||||
client_watch_name (client);
|
||||
client_register_object (client);
|
||||
|
||||
G_OBJECT_CLASS (pinos_client_parent_class)->constructed (object);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
pinos_client_class_init (PinosClientClass * klass)
|
||||
{
|
||||
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
||||
|
||||
g_type_class_add_private (klass, sizeof (PinosClientPrivate));
|
||||
|
||||
gobject_class->constructed = pinos_client_constructed;
|
||||
gobject_class->dispose = pinos_client_dispose;
|
||||
gobject_class->finalize = pinos_client_finalize;
|
||||
gobject_class->set_property = pinos_client_set_property;
|
||||
gobject_class->get_property = pinos_client_get_property;
|
||||
|
||||
g_object_class_install_property (gobject_class,
|
||||
PROP_DAEMON,
|
||||
g_param_spec_object ("daemon",
|
||||
"Daemon",
|
||||
"The daemon",
|
||||
PINOS_TYPE_DAEMON,
|
||||
G_PARAM_READWRITE |
|
||||
G_PARAM_CONSTRUCT_ONLY |
|
||||
G_PARAM_STATIC_STRINGS));
|
||||
|
||||
g_object_class_install_property (gobject_class,
|
||||
PROP_SENDER,
|
||||
g_param_spec_string ("sender",
|
||||
"Sender",
|
||||
"The sender",
|
||||
NULL,
|
||||
G_PARAM_READWRITE |
|
||||
G_PARAM_CONSTRUCT_ONLY |
|
||||
G_PARAM_STATIC_STRINGS));
|
||||
|
||||
g_object_class_install_property (gobject_class,
|
||||
PROP_OBJECT_PATH,
|
||||
g_param_spec_string ("object-path",
|
||||
"Object Path",
|
||||
"The object path",
|
||||
NULL,
|
||||
G_PARAM_READABLE |
|
||||
G_PARAM_STATIC_STRINGS));
|
||||
g_object_class_install_property (gobject_class,
|
||||
PROP_PROPERTIES,
|
||||
g_param_spec_boxed ("properties",
|
||||
"Properties",
|
||||
"Client properties",
|
||||
PINOS_TYPE_PROPERTIES,
|
||||
G_PARAM_READWRITE |
|
||||
G_PARAM_CONSTRUCT |
|
||||
G_PARAM_STATIC_STRINGS));
|
||||
|
||||
signals[SIGNAL_APPEARED] = g_signal_new ("appeared",
|
||||
G_TYPE_FROM_CLASS (klass),
|
||||
G_SIGNAL_RUN_LAST,
|
||||
0,
|
||||
NULL,
|
||||
NULL,
|
||||
g_cclosure_marshal_generic,
|
||||
G_TYPE_NONE,
|
||||
0,
|
||||
G_TYPE_NONE);
|
||||
signals[SIGNAL_VANISHED] = g_signal_new ("vanished",
|
||||
G_TYPE_FROM_CLASS (klass),
|
||||
G_SIGNAL_RUN_LAST,
|
||||
0,
|
||||
NULL,
|
||||
NULL,
|
||||
g_cclosure_marshal_generic,
|
||||
G_TYPE_NONE,
|
||||
0,
|
||||
G_TYPE_NONE);
|
||||
signals[SIGNAL_REMOVE] = g_signal_new ("remove",
|
||||
G_TYPE_FROM_CLASS (klass),
|
||||
G_SIGNAL_RUN_LAST,
|
||||
0,
|
||||
NULL,
|
||||
NULL,
|
||||
g_cclosure_marshal_generic,
|
||||
G_TYPE_NONE,
|
||||
0,
|
||||
G_TYPE_NONE);
|
||||
}
|
||||
|
||||
static void
|
||||
pinos_client_init (PinosClient * client)
|
||||
{
|
||||
PinosClientPrivate *priv = client->priv = PINOS_CLIENT_GET_PRIVATE (client);
|
||||
|
||||
priv->iface = pinos_client1_skeleton_new ();
|
||||
g_debug ("client %p: new", client);
|
||||
priv->fdmanager = pinos_fd_manager_get (PINOS_FD_MANAGER_DEFAULT);
|
||||
}
|
||||
|
||||
/**
|
||||
* pinos_client_new:
|
||||
* @daemon: a #PinosDaemon
|
||||
* @sender: the sender id
|
||||
* @prefix: a prefix
|
||||
* @properties: extra client properties
|
||||
*
|
||||
* Make a new #PinosClient object and register it to @daemon under the @prefix.
|
||||
*
|
||||
* Returns: a new #PinosClient
|
||||
*/
|
||||
PinosClient *
|
||||
pinos_client_new (PinosDaemon *daemon,
|
||||
const gchar *sender,
|
||||
PinosProperties *properties)
|
||||
{
|
||||
g_return_val_if_fail (PINOS_IS_DAEMON (daemon), NULL);
|
||||
|
||||
return g_object_new (PINOS_TYPE_CLIENT, "daemon", daemon,
|
||||
"sender", sender,
|
||||
"properties", properties,
|
||||
NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
* pinos_client_remove:
|
||||
* @client: a #PinosClient
|
||||
*
|
||||
* Trigger removal of @client
|
||||
*/
|
||||
void
|
||||
pinos_client_remove (PinosClient *client)
|
||||
{
|
||||
g_return_if_fail (PINOS_IS_CLIENT (client));
|
||||
|
||||
g_debug ("client %p: remove", client);
|
||||
g_signal_emit (client, signals[SIGNAL_REMOVE], 0, NULL);
|
||||
}
|
||||
|
||||
void
|
||||
pinos_client_add_object (PinosClient *client,
|
||||
GObject *object)
|
||||
{
|
||||
PinosClientPrivate *priv;
|
||||
|
||||
g_return_if_fail (PINOS_IS_CLIENT (client));
|
||||
g_return_if_fail (G_IS_OBJECT (object));
|
||||
priv = client->priv;
|
||||
|
||||
priv->objects = g_list_prepend (priv->objects, object);
|
||||
}
|
||||
|
||||
void
|
||||
pinos_client_remove_object (PinosClient *client,
|
||||
GObject *object)
|
||||
{
|
||||
PinosClientPrivate *priv;
|
||||
|
||||
g_return_if_fail (PINOS_IS_CLIENT (client));
|
||||
g_return_if_fail (G_IS_OBJECT (object));
|
||||
priv = client->priv;
|
||||
|
||||
priv->objects = g_list_remove (priv->objects, object);
|
||||
g_object_unref (object);
|
||||
}
|
||||
|
||||
/**
|
||||
* pinos_client_get_sender:
|
||||
* @client: a #PinosClient
|
||||
*
|
||||
* Get the sender of @client.
|
||||
*
|
||||
* Returns: the sender of @client
|
||||
*/
|
||||
const gchar *
|
||||
pinos_client_get_sender (PinosClient *client)
|
||||
{
|
||||
PinosClientPrivate *priv;
|
||||
|
||||
g_return_val_if_fail (PINOS_IS_CLIENT (client), NULL);
|
||||
priv = client->priv;
|
||||
|
||||
return priv->sender;
|
||||
}
|
||||
|
||||
/**
|
||||
* pinos_client_get_object_path:
|
||||
* @client: a #PinosClient
|
||||
*
|
||||
* Get the object path of @client.
|
||||
*
|
||||
* Returns: the object path of @client
|
||||
*/
|
||||
const gchar *
|
||||
pinos_client_get_object_path (PinosClient *client)
|
||||
{
|
||||
PinosClientPrivate *priv;
|
||||
|
||||
g_return_val_if_fail (PINOS_IS_CLIENT (client), NULL);
|
||||
priv = client->priv;
|
||||
|
||||
return priv->object_path;
|
||||
}
|
81
pinos/server/client.h
Normal file
81
pinos/server/client.h
Normal file
|
@ -0,0 +1,81 @@
|
|||
/* Pinos
|
||||
* Copyright (C) 2015 Wim Taymans <wim.taymans@gmail.com>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the
|
||||
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
||||
* Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#ifndef __PINOS_CLIENT_H__
|
||||
#define __PINOS_CLIENT_H__
|
||||
|
||||
#include <glib-object.h>
|
||||
|
||||
#include <pinos/server/daemon.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
#define PINOS_TYPE_CLIENT (pinos_client_get_type ())
|
||||
#define PINOS_IS_CLIENT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PINOS_TYPE_CLIENT))
|
||||
#define PINOS_IS_CLIENT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PINOS_TYPE_CLIENT))
|
||||
#define PINOS_CLIENT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), PINOS_TYPE_CLIENT, PinosClientClass))
|
||||
#define PINOS_CLIENT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), PINOS_TYPE_CLIENT, PinosClient))
|
||||
#define PINOS_CLIENT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PINOS_TYPE_CLIENT, PinosClientClass))
|
||||
#define PINOS_CLIENT_CAST(obj) ((PinosClient*)(obj))
|
||||
#define PINOS_CLIENT_CLASS_CAST(klass) ((PinosClientClass*)(klass))
|
||||
|
||||
typedef struct _PinosClient PinosClient;
|
||||
typedef struct _PinosClientClass PinosClientClass;
|
||||
typedef struct _PinosClientPrivate PinosClientPrivate;
|
||||
|
||||
/**
|
||||
* PinosClient:
|
||||
*
|
||||
* Pinos client object class.
|
||||
*/
|
||||
struct _PinosClient {
|
||||
GObject object;
|
||||
|
||||
PinosClientPrivate *priv;
|
||||
};
|
||||
|
||||
/**
|
||||
* PinosClientClass:
|
||||
*
|
||||
* Pinos client object class.
|
||||
*/
|
||||
struct _PinosClientClass {
|
||||
GObjectClass parent_class;
|
||||
};
|
||||
|
||||
/* normal GObject stuff */
|
||||
GType pinos_client_get_type (void);
|
||||
|
||||
PinosClient * pinos_client_new (PinosDaemon *daemon,
|
||||
const gchar *sender,
|
||||
PinosProperties *properties);
|
||||
|
||||
void pinos_client_remove (PinosClient *client);
|
||||
|
||||
const gchar * pinos_client_get_sender (PinosClient *client);
|
||||
const gchar * pinos_client_get_object_path (PinosClient *client);
|
||||
|
||||
void pinos_client_add_object (PinosClient *client,
|
||||
GObject *object);
|
||||
void pinos_client_remove_object (PinosClient *client,
|
||||
GObject *object);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /* __PINOS_CLIENT_H__ */
|
|
@ -20,6 +20,7 @@
|
|||
#include <string.h>
|
||||
|
||||
#include <gio/gio.h>
|
||||
#include <gio/gunixfdlist.h>
|
||||
|
||||
#include "config.h"
|
||||
|
||||
|
@ -27,6 +28,8 @@
|
|||
|
||||
#include "pinos/server/daemon.h"
|
||||
#include "pinos/server/node.h"
|
||||
#include "pinos/server/client.h"
|
||||
#include "pinos/server/channel.h"
|
||||
|
||||
#include "pinos/dbus/org-pinos.h"
|
||||
|
||||
|
@ -42,7 +45,7 @@ struct _PinosDaemonPrivate
|
|||
|
||||
GList *nodes;
|
||||
|
||||
GHashTable *senders;
|
||||
GHashTable *clients;
|
||||
|
||||
PinosProperties *properties;
|
||||
|
||||
|
@ -52,95 +55,174 @@ struct _PinosDaemonPrivate
|
|||
enum
|
||||
{
|
||||
PROP_0,
|
||||
PROP_CONNECTION,
|
||||
PROP_PROPERTIES,
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
guint id;
|
||||
gchar *sender;
|
||||
PinosDaemon *daemon;
|
||||
GList *objects;
|
||||
} SenderData;
|
||||
|
||||
static void
|
||||
sender_name_appeared_handler (GDBusConnection *connection,
|
||||
const gchar *name,
|
||||
const gchar *name_owner,
|
||||
gpointer user_data)
|
||||
handle_client_appeared (PinosClient *client, gpointer user_data)
|
||||
{
|
||||
SenderData *data = user_data;
|
||||
PinosDaemonPrivate *priv = data->daemon->priv;
|
||||
PinosDaemon *daemon = user_data;
|
||||
PinosDaemonPrivate *priv = daemon->priv;
|
||||
|
||||
g_debug ("daemon %p: appeared %s %s", data->daemon, name, name_owner);
|
||||
g_debug ("daemon %p: appeared %p", daemon, client);
|
||||
|
||||
g_hash_table_insert (priv->senders, data->sender, data);
|
||||
g_hash_table_insert (priv->clients, (gpointer) pinos_client_get_sender (client), client);
|
||||
}
|
||||
|
||||
static void
|
||||
sender_name_vanished_handler (GDBusConnection *connection,
|
||||
const gchar *name,
|
||||
gpointer user_data)
|
||||
handle_client_vanished (PinosClient *client, gpointer user_data)
|
||||
{
|
||||
SenderData *data = user_data;
|
||||
PinosDaemon *daemon = user_data;
|
||||
PinosDaemonPrivate *priv = daemon->priv;
|
||||
|
||||
g_debug ("daemon %p: vanished %s", data->daemon, name);
|
||||
|
||||
g_bus_unwatch_name (data->id);
|
||||
g_debug ("daemon %p: vanished %p", daemon, client);
|
||||
g_hash_table_remove (priv->clients, (gpointer) pinos_client_get_sender (client));
|
||||
}
|
||||
|
||||
static void
|
||||
data_free (SenderData *data)
|
||||
{
|
||||
g_debug ("daemon %p: free sender data %p for %s", data->daemon, data, data->sender);
|
||||
g_list_free_full (data->objects, g_object_unref);
|
||||
g_hash_table_remove (data->daemon->priv->senders, data->sender);
|
||||
g_free (data->sender);
|
||||
g_free (data);
|
||||
}
|
||||
|
||||
static SenderData *
|
||||
sender_data_new (PinosDaemon *daemon,
|
||||
const gchar *sender)
|
||||
static PinosClient *
|
||||
sender_get_client (PinosDaemon *daemon,
|
||||
const gchar *sender)
|
||||
{
|
||||
PinosDaemonPrivate *priv = daemon->priv;
|
||||
SenderData *data;
|
||||
PinosClient *client;
|
||||
|
||||
data = g_new0 (SenderData, 1);
|
||||
data->daemon = daemon;
|
||||
data->sender = g_strdup (sender);
|
||||
client = g_hash_table_lookup (priv->clients, sender);
|
||||
if (client == NULL) {
|
||||
client = pinos_client_new (daemon, sender, NULL);
|
||||
|
||||
g_debug ("daemon %p: new sender data %p for %s", daemon, data, sender);
|
||||
g_debug ("daemon %p: new client %p for %s", daemon, client, sender);
|
||||
g_signal_connect (client,
|
||||
"appeared",
|
||||
(GCallback) handle_client_appeared,
|
||||
daemon);
|
||||
g_signal_connect (client,
|
||||
"vanished",
|
||||
(GCallback) handle_client_vanished,
|
||||
daemon);
|
||||
}
|
||||
return client;
|
||||
}
|
||||
|
||||
data->id = g_bus_watch_name_on_connection (priv->connection,
|
||||
sender,
|
||||
G_BUS_NAME_WATCHER_FLAGS_NONE,
|
||||
sender_name_appeared_handler,
|
||||
sender_name_vanished_handler,
|
||||
data,
|
||||
(GDestroyNotify) data_free);
|
||||
return data;
|
||||
static void
|
||||
handle_remove_channel (PinosChannel *channel,
|
||||
gpointer user_data)
|
||||
{
|
||||
PinosClient *client = user_data;
|
||||
|
||||
g_debug ("client %p: remove channel %p", client, channel);
|
||||
pinos_client_remove_object (client, G_OBJECT (channel));
|
||||
}
|
||||
|
||||
static gboolean
|
||||
handle_create_channel (PinosDaemon1 *interface,
|
||||
GDBusMethodInvocation *invocation,
|
||||
const gchar *arg_node,
|
||||
PinosDirection direction,
|
||||
const gchar *arg_possible_formats,
|
||||
GVariant *arg_properties,
|
||||
gpointer user_data)
|
||||
{
|
||||
PinosDaemon *daemon = user_data;
|
||||
const gchar *sender, *object_path;
|
||||
PinosProperties *props;
|
||||
PinosClient *client;
|
||||
PinosPort *port;
|
||||
PinosChannel *channel;
|
||||
GBytes *formats;
|
||||
GError *error = NULL;
|
||||
GUnixFDList *fdlist;
|
||||
GSocket *socket;
|
||||
gint fdidx;
|
||||
|
||||
sender = g_dbus_method_invocation_get_sender (invocation);
|
||||
|
||||
g_debug ("daemon %p: %s create channel", daemon, sender);
|
||||
|
||||
formats = g_bytes_new (arg_possible_formats, strlen (arg_possible_formats) + 1);
|
||||
props = pinos_properties_from_variant (arg_properties);
|
||||
|
||||
port = pinos_daemon_find_port (daemon,
|
||||
direction,
|
||||
arg_node,
|
||||
props,
|
||||
formats,
|
||||
&error);
|
||||
if (port == NULL)
|
||||
goto no_port;
|
||||
|
||||
g_debug ("daemon %p: matched port %p", daemon, port);
|
||||
|
||||
client = sender_get_client (daemon, sender);
|
||||
|
||||
channel = pinos_port_create_channel (port,
|
||||
pinos_client_get_object_path (client),
|
||||
formats,
|
||||
props,
|
||||
&error);
|
||||
pinos_properties_free (props);
|
||||
g_bytes_unref (formats);
|
||||
|
||||
if (channel == NULL)
|
||||
goto no_channel;
|
||||
|
||||
pinos_client_add_object (client, G_OBJECT (channel));
|
||||
|
||||
g_signal_connect (channel,
|
||||
"remove",
|
||||
(GCallback) handle_remove_channel,
|
||||
client);
|
||||
|
||||
socket = pinos_channel_get_socket_pair (channel, &error);
|
||||
if (socket == NULL)
|
||||
goto no_socket;
|
||||
|
||||
object_path = pinos_channel_get_object_path (channel);
|
||||
g_debug ("daemon %p: add channel %p, %s", daemon, channel, object_path);
|
||||
|
||||
fdlist = g_unix_fd_list_new ();
|
||||
fdidx = g_unix_fd_list_append (fdlist, g_socket_get_fd (socket), NULL);
|
||||
|
||||
g_dbus_method_invocation_return_value_with_unix_fd_list (invocation,
|
||||
g_variant_new ("(oh)", object_path, fdidx), fdlist);
|
||||
g_object_unref (fdlist);
|
||||
|
||||
return TRUE;
|
||||
|
||||
/* ERRORS */
|
||||
no_port:
|
||||
{
|
||||
g_debug ("daemon %p: could not find port %s, %s", daemon, arg_node, error->message);
|
||||
pinos_properties_free (props);
|
||||
g_bytes_unref (formats);
|
||||
goto exit_error;
|
||||
}
|
||||
no_channel:
|
||||
{
|
||||
g_debug ("daemon %p: could not create channel %s", daemon, error->message);
|
||||
goto exit_error;
|
||||
}
|
||||
no_socket:
|
||||
{
|
||||
g_debug ("daemon %p: could not create channel %s", daemon, error->message);
|
||||
goto exit_error;
|
||||
}
|
||||
exit_error:
|
||||
{
|
||||
g_dbus_method_invocation_return_gerror (invocation, error);
|
||||
g_clear_error (&error);
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
handle_remove_node (PinosNode *node,
|
||||
gpointer user_data)
|
||||
{
|
||||
PinosDaemon *daemon = user_data;
|
||||
PinosDaemonPrivate *priv = daemon->priv;
|
||||
const gchar *sender;
|
||||
SenderData *data;
|
||||
PinosClient *client = user_data;
|
||||
|
||||
sender = pinos_node_get_sender (node);
|
||||
|
||||
g_debug ("daemon %p: sender %s removed node %p", daemon, sender, node);
|
||||
|
||||
data = g_hash_table_lookup (priv->senders, sender);
|
||||
if (data == NULL)
|
||||
return;
|
||||
|
||||
g_debug ("daemon %p: node %p unref", daemon, node);
|
||||
data->objects = g_list_remove (data->objects, node);
|
||||
g_object_unref (node);
|
||||
g_debug ("client %p: node %p remove", daemon, node);
|
||||
pinos_client_remove_object (client, G_OBJECT (node));
|
||||
}
|
||||
|
||||
static gboolean
|
||||
|
@ -155,7 +237,7 @@ handle_create_node (PinosDaemon1 *interface,
|
|||
PinosDaemonPrivate *priv = daemon->priv;
|
||||
PinosNodeFactory *factory;
|
||||
PinosNode *node;
|
||||
SenderData *data;
|
||||
PinosClient *client;
|
||||
const gchar *sender, *object_path;
|
||||
PinosProperties *props;
|
||||
|
||||
|
@ -184,16 +266,13 @@ handle_create_node (PinosDaemon1 *interface,
|
|||
if (node == NULL)
|
||||
goto no_node;
|
||||
|
||||
data = g_hash_table_lookup (priv->senders, sender);
|
||||
if (data == NULL)
|
||||
data = sender_data_new (daemon, sender);
|
||||
|
||||
data->objects = g_list_prepend (data->objects, node);
|
||||
client = sender_get_client (daemon, sender);
|
||||
pinos_client_add_object (client, G_OBJECT (node));
|
||||
|
||||
g_signal_connect (node,
|
||||
"remove",
|
||||
(GCallback) handle_remove_node,
|
||||
daemon);
|
||||
client);
|
||||
|
||||
object_path = pinos_node_get_object_path (node);
|
||||
g_debug ("daemon %p: added node %p with path %s", daemon, node, object_path);
|
||||
|
@ -212,6 +291,18 @@ no_node:
|
|||
}
|
||||
}
|
||||
|
||||
static gboolean
|
||||
handle_link_nodes (PinosDaemon1 *interface,
|
||||
GDBusMethodInvocation *invocation,
|
||||
const gchar *arg_node,
|
||||
const gchar *arg_port,
|
||||
const gchar *arg_possible_formats,
|
||||
GVariant *arg_properties,
|
||||
gpointer user_data)
|
||||
{
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static void
|
||||
export_server_object (PinosDaemon *daemon,
|
||||
GDBusObjectManagerServer *manager)
|
||||
|
@ -520,6 +611,10 @@ pinos_daemon_get_property (GObject *_object,
|
|||
g_value_set_boxed (value, priv->properties);
|
||||
break;
|
||||
|
||||
case PROP_CONNECTION:
|
||||
g_value_set_object (value, priv->connection);
|
||||
break;
|
||||
|
||||
default:
|
||||
G_OBJECT_WARN_INVALID_PROPERTY_ID (daemon, prop_id, pspec);
|
||||
break;
|
||||
|
@ -586,7 +681,7 @@ pinos_daemon_finalize (GObject * object)
|
|||
g_debug ("daemon %p: finalize", object);
|
||||
g_clear_object (&priv->server_manager);
|
||||
g_clear_object (&priv->iface);
|
||||
g_hash_table_unref (priv->senders);
|
||||
g_hash_table_unref (priv->clients);
|
||||
g_hash_table_unref (priv->node_factories);
|
||||
|
||||
G_OBJECT_CLASS (pinos_daemon_parent_class)->finalize (object);
|
||||
|
@ -606,6 +701,15 @@ pinos_daemon_class_init (PinosDaemonClass * klass)
|
|||
gobject_class->set_property = pinos_daemon_set_property;
|
||||
gobject_class->get_property = pinos_daemon_get_property;
|
||||
|
||||
g_object_class_install_property (gobject_class,
|
||||
PROP_CONNECTION,
|
||||
g_param_spec_object ("connection",
|
||||
"Connection",
|
||||
"The DBus connection",
|
||||
G_TYPE_DBUS_CONNECTION,
|
||||
G_PARAM_READABLE |
|
||||
G_PARAM_STATIC_STRINGS));
|
||||
|
||||
g_object_class_install_property (gobject_class,
|
||||
PROP_PROPERTIES,
|
||||
g_param_spec_boxed ("properties",
|
||||
|
@ -626,9 +730,11 @@ pinos_daemon_init (PinosDaemon * daemon)
|
|||
g_debug ("daemon %p: new", daemon);
|
||||
priv->iface = pinos_daemon1_skeleton_new ();
|
||||
g_signal_connect (priv->iface, "handle-create-node", (GCallback) handle_create_node, daemon);
|
||||
g_signal_connect (priv->iface, "handle-create-channel", (GCallback) handle_create_channel, daemon);
|
||||
g_signal_connect (priv->iface, "handle-link-nodes", (GCallback) handle_link_nodes, daemon);
|
||||
|
||||
priv->server_manager = g_dbus_object_manager_server_new (PINOS_DBUS_OBJECT_PREFIX);
|
||||
priv->senders = g_hash_table_new (g_str_hash, g_str_equal);
|
||||
priv->clients = g_hash_table_new (g_str_hash, g_str_equal);
|
||||
priv->node_factories = g_hash_table_new_full (g_str_hash,
|
||||
g_str_equal,
|
||||
g_free,
|
||||
|
|
|
@ -18,9 +18,7 @@
|
|||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
#include <gst/gst.h>
|
||||
#include <gio/gio.h>
|
||||
|
||||
#include "pinos/client/pinos.h"
|
||||
|
@ -28,6 +26,7 @@
|
|||
|
||||
#include "pinos/server/port.h"
|
||||
#include "pinos/server/node.h"
|
||||
#include "pinos/server/utils.h"
|
||||
|
||||
#define PINOS_PORT_GET_PRIVATE(obj) \
|
||||
(G_TYPE_INSTANCE_GET_PRIVATE ((obj), PINOS_TYPE_PORT, PinosPortPrivate))
|
||||
|
@ -38,30 +37,34 @@
|
|||
#define PINOS_DEBUG_TRANSPORT(format,args...)
|
||||
#endif
|
||||
|
||||
#define MAX_BUFFER_SIZE 1024
|
||||
#define MAX_FDS 16
|
||||
typedef struct {
|
||||
gulong id;
|
||||
PinosBufferCallback send_buffer_cb;
|
||||
gpointer send_buffer_data;
|
||||
GDestroyNotify send_buffer_notify;
|
||||
} SendData;
|
||||
|
||||
struct _PinosPortPrivate
|
||||
{
|
||||
PinosDaemon *daemon;
|
||||
|
||||
gulong id;
|
||||
|
||||
PinosNode *node;
|
||||
PinosDirection direction;
|
||||
GBytes *possible_formats;
|
||||
GBytes *format;
|
||||
PinosProperties *properties;
|
||||
|
||||
guint8 send_data[MAX_BUFFER_SIZE];
|
||||
int send_fds[MAX_FDS];
|
||||
|
||||
PinosBuffer *buffer;
|
||||
GPtrArray *peers;
|
||||
gchar **peer_paths;
|
||||
guint max_peers;
|
||||
|
||||
PinosReceivedBufferCallback received_buffer_cb;
|
||||
PinosBufferCallback received_buffer_cb;
|
||||
gpointer received_buffer_data;
|
||||
GDestroyNotify received_buffer_notify;
|
||||
|
||||
gint active_count;
|
||||
|
||||
GList *send_datas;
|
||||
};
|
||||
|
||||
G_DEFINE_TYPE (PinosPort, pinos_port, G_TYPE_OBJECT);
|
||||
|
@ -71,10 +74,7 @@ enum
|
|||
PROP_0,
|
||||
PROP_DAEMON,
|
||||
PROP_NODE,
|
||||
PROP_MAIN_CONTEXT,
|
||||
PROP_DIRECTION,
|
||||
PROP_MAX_PEERS,
|
||||
PROP_PEERS,
|
||||
PROP_POSSIBLE_FORMATS,
|
||||
PROP_FORMAT,
|
||||
PROP_PROPERTIES,
|
||||
|
@ -84,8 +84,8 @@ enum
|
|||
{
|
||||
SIGNAL_FORMAT_REQUEST,
|
||||
SIGNAL_REMOVE,
|
||||
SIGNAL_LINKED,
|
||||
SIGNAL_UNLINKED,
|
||||
SIGNAL_ACTIVATE,
|
||||
SIGNAL_DEACTIVATE,
|
||||
LAST_SIGNAL
|
||||
};
|
||||
|
||||
|
@ -94,7 +94,7 @@ static guint signals[LAST_SIGNAL] = { 0 };
|
|||
|
||||
void
|
||||
pinos_port_set_received_buffer_cb (PinosPort *port,
|
||||
PinosReceivedBufferCallback cb,
|
||||
PinosBufferCallback cb,
|
||||
gpointer user_data,
|
||||
GDestroyNotify notify)
|
||||
{
|
||||
|
@ -103,7 +103,7 @@ pinos_port_set_received_buffer_cb (PinosPort *port,
|
|||
g_return_if_fail (PINOS_IS_PORT (port));
|
||||
priv = port->priv;
|
||||
|
||||
g_debug ("port %p: set callback", port);
|
||||
g_debug ("port %p: set receive callback", port);
|
||||
|
||||
if (priv->received_buffer_notify)
|
||||
priv->received_buffer_notify (priv->received_buffer_data);;
|
||||
|
@ -112,6 +112,54 @@ pinos_port_set_received_buffer_cb (PinosPort *port,
|
|||
priv->received_buffer_notify = notify;
|
||||
}
|
||||
|
||||
gulong
|
||||
pinos_port_add_send_buffer_cb (PinosPort *port,
|
||||
PinosBufferCallback cb,
|
||||
gpointer user_data,
|
||||
GDestroyNotify notify)
|
||||
{
|
||||
PinosPortPrivate *priv;
|
||||
SendData *data;
|
||||
|
||||
g_return_val_if_fail (PINOS_IS_PORT (port), -1);
|
||||
g_return_val_if_fail (cb != NULL, -1);
|
||||
priv = port->priv;
|
||||
|
||||
g_debug ("port %p: add send callback", port);
|
||||
|
||||
data = g_slice_new (SendData);
|
||||
data->id = priv->id++;
|
||||
data->send_buffer_cb = cb;
|
||||
data->send_buffer_data = user_data;
|
||||
data->send_buffer_notify = notify;
|
||||
priv->send_datas = g_list_prepend (priv->send_datas, data);
|
||||
|
||||
return data->id;
|
||||
}
|
||||
|
||||
void
|
||||
pinos_port_remove_send_buffer_cb (PinosPort *port,
|
||||
gulong id)
|
||||
{
|
||||
PinosPortPrivate *priv;
|
||||
GList *walk;
|
||||
|
||||
g_return_if_fail (PINOS_IS_PORT (port));
|
||||
priv = port->priv;
|
||||
|
||||
g_debug ("port %p: remove send callback %lu", port, id);
|
||||
for (walk = priv->send_datas; walk; walk = g_list_next (walk)) {
|
||||
SendData *data = walk->data;
|
||||
|
||||
if (data->id == id) {
|
||||
if (data->send_buffer_notify)
|
||||
data->send_buffer_notify (data->send_buffer_data);;
|
||||
g_slice_free (SendData, data);
|
||||
priv->send_datas = g_list_delete_link (priv->send_datas, walk);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
pinos_port_get_property (GObject *_object,
|
||||
|
@ -131,14 +179,6 @@ pinos_port_get_property (GObject *_object,
|
|||
g_value_set_object (value, priv->node);
|
||||
break;
|
||||
|
||||
case PROP_MAX_PEERS:
|
||||
g_value_set_uint (value, priv->max_peers);
|
||||
break;
|
||||
|
||||
case PROP_PEERS:
|
||||
g_value_set_boxed (value, priv->peer_paths);
|
||||
break;
|
||||
|
||||
case PROP_DIRECTION:
|
||||
g_value_set_enum (value, priv->direction);
|
||||
break;
|
||||
|
@ -183,16 +223,6 @@ pinos_port_set_property (GObject *_object,
|
|||
priv->direction = g_value_get_enum (value);
|
||||
break;
|
||||
|
||||
case PROP_MAX_PEERS:
|
||||
priv->max_peers = g_value_get_uint (value);
|
||||
break;
|
||||
|
||||
case PROP_PEERS:
|
||||
if (priv->peer_paths)
|
||||
g_strfreev (priv->peer_paths);
|
||||
priv->peer_paths = g_value_dup_boxed (value);
|
||||
break;
|
||||
|
||||
case PROP_POSSIBLE_FORMATS:
|
||||
if (priv->possible_formats)
|
||||
g_bytes_unref (priv->possible_formats);
|
||||
|
@ -242,6 +272,7 @@ pinos_port_finalize (GObject * object)
|
|||
{
|
||||
PinosPort *port = PINOS_PORT (object);
|
||||
PinosPortPrivate *priv = port->priv;
|
||||
GList *walk;
|
||||
|
||||
g_debug ("port %p: finalize", port);
|
||||
g_clear_pointer (&priv->possible_formats, g_bytes_unref);
|
||||
|
@ -249,7 +280,14 @@ pinos_port_finalize (GObject * object)
|
|||
g_clear_pointer (&priv->properties, pinos_properties_free);
|
||||
if (priv->received_buffer_notify)
|
||||
priv->received_buffer_notify (priv->received_buffer_data);
|
||||
g_ptr_array_unref (priv->peers);
|
||||
for (walk = priv->send_datas; walk; walk = g_list_next (walk)) {
|
||||
SendData *data = walk->data;
|
||||
if (data->send_buffer_notify)
|
||||
data->send_buffer_notify (data->send_buffer_data);
|
||||
g_slice_free (SendData, data);
|
||||
}
|
||||
g_list_free (priv->send_datas);
|
||||
|
||||
g_clear_object (&priv->daemon);
|
||||
|
||||
G_OBJECT_CLASS (pinos_port_parent_class)->finalize (object);
|
||||
|
@ -298,23 +336,6 @@ pinos_port_class_init (PinosPortClass * klass)
|
|||
G_PARAM_READWRITE |
|
||||
G_PARAM_CONSTRUCT_ONLY |
|
||||
G_PARAM_STATIC_STRINGS));
|
||||
g_object_class_install_property (gobject_class,
|
||||
PROP_MAX_PEERS,
|
||||
g_param_spec_uint ("max-peers",
|
||||
"Max Peers",
|
||||
"The maximum number of peer ports",
|
||||
1, G_MAXUINT, G_MAXUINT,
|
||||
G_PARAM_READWRITE |
|
||||
G_PARAM_STATIC_STRINGS));
|
||||
|
||||
g_object_class_install_property (gobject_class,
|
||||
PROP_PEERS,
|
||||
g_param_spec_boxed ("peers",
|
||||
"Peers",
|
||||
"The peer ports of the port",
|
||||
G_TYPE_STRV,
|
||||
G_PARAM_READWRITE |
|
||||
G_PARAM_STATIC_STRINGS));
|
||||
|
||||
g_object_class_install_property (gobject_class,
|
||||
PROP_POSSIBLE_FORMATS,
|
||||
|
@ -333,7 +354,7 @@ pinos_port_class_init (PinosPortClass * klass)
|
|||
"The format of the port",
|
||||
G_TYPE_BYTES,
|
||||
G_PARAM_READWRITE |
|
||||
G_PARAM_CONSTRUCT_ONLY |
|
||||
G_PARAM_CONSTRUCT |
|
||||
G_PARAM_STATIC_STRINGS));
|
||||
|
||||
g_object_class_install_property (gobject_class,
|
||||
|
@ -368,27 +389,26 @@ pinos_port_class_init (PinosPortClass * klass)
|
|||
G_TYPE_NONE,
|
||||
0,
|
||||
G_TYPE_NONE);
|
||||
signals[SIGNAL_LINKED] = g_signal_new ("linked",
|
||||
G_TYPE_FROM_CLASS (klass),
|
||||
G_SIGNAL_RUN_LAST,
|
||||
0,
|
||||
NULL,
|
||||
NULL,
|
||||
g_cclosure_marshal_generic,
|
||||
G_TYPE_BOOLEAN,
|
||||
1,
|
||||
PINOS_TYPE_PORT);
|
||||
signals[SIGNAL_UNLINKED] = g_signal_new ("unlinked",
|
||||
G_TYPE_FROM_CLASS (klass),
|
||||
G_SIGNAL_RUN_LAST,
|
||||
0,
|
||||
NULL,
|
||||
NULL,
|
||||
g_cclosure_marshal_generic,
|
||||
G_TYPE_NONE,
|
||||
1,
|
||||
PINOS_TYPE_PORT);
|
||||
|
||||
signals[SIGNAL_ACTIVATE] = g_signal_new ("activate",
|
||||
G_TYPE_FROM_CLASS (klass),
|
||||
G_SIGNAL_RUN_LAST,
|
||||
0,
|
||||
NULL,
|
||||
NULL,
|
||||
g_cclosure_marshal_generic,
|
||||
G_TYPE_NONE,
|
||||
0,
|
||||
G_TYPE_NONE);
|
||||
signals[SIGNAL_DEACTIVATE] = g_signal_new ("deactivate",
|
||||
G_TYPE_FROM_CLASS (klass),
|
||||
G_SIGNAL_RUN_LAST,
|
||||
0,
|
||||
NULL,
|
||||
NULL,
|
||||
g_cclosure_marshal_generic,
|
||||
G_TYPE_NONE,
|
||||
0,
|
||||
G_TYPE_NONE);
|
||||
}
|
||||
|
||||
static void
|
||||
|
@ -398,7 +418,6 @@ pinos_port_init (PinosPort * port)
|
|||
|
||||
g_debug ("port %p: new", port);
|
||||
priv->direction = PINOS_DIRECTION_INVALID;
|
||||
priv->peers = g_ptr_array_new_full (64, NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -529,72 +548,14 @@ pinos_port_filter_formats (PinosPort *port,
|
|||
GBytes *filter,
|
||||
GError **error)
|
||||
{
|
||||
GstCaps *tmp, *caps, *cfilter;
|
||||
gchar *str;
|
||||
PinosPortPrivate *priv;
|
||||
GBytes *res;
|
||||
|
||||
g_return_val_if_fail (PINOS_IS_PORT (port), NULL);
|
||||
priv = port->priv;
|
||||
|
||||
if (filter) {
|
||||
cfilter = gst_caps_from_string (g_bytes_get_data (filter, NULL));
|
||||
if (cfilter == NULL)
|
||||
goto invalid_filter;
|
||||
} else {
|
||||
cfilter = NULL;
|
||||
}
|
||||
|
||||
g_signal_emit (port, signals[SIGNAL_FORMAT_REQUEST], 0, NULL);
|
||||
|
||||
if (priv->possible_formats)
|
||||
caps = gst_caps_from_string (g_bytes_get_data (priv->possible_formats, NULL));
|
||||
else
|
||||
caps = gst_caps_new_any ();
|
||||
|
||||
if (caps && cfilter) {
|
||||
tmp = gst_caps_intersect_full (caps, cfilter, GST_CAPS_INTERSECT_FIRST);
|
||||
gst_caps_take (&caps, tmp);
|
||||
}
|
||||
g_clear_pointer (&cfilter, gst_caps_unref);
|
||||
|
||||
if (caps == NULL || gst_caps_is_empty (caps))
|
||||
goto no_format;
|
||||
|
||||
str = gst_caps_to_string (caps);
|
||||
gst_caps_unref (caps);
|
||||
res = g_bytes_new_take (str, strlen (str) + 1);
|
||||
|
||||
if (priv->direction == PINOS_DIRECTION_OUTPUT) {
|
||||
guint i;
|
||||
for (i = 0; i < priv->peers->len; i++) {
|
||||
PinosPort *peer = g_ptr_array_index (priv->peers, i);
|
||||
res = pinos_port_filter_formats (peer, res, error);
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
|
||||
invalid_filter:
|
||||
{
|
||||
g_set_error (error,
|
||||
G_IO_ERROR,
|
||||
G_IO_ERROR_INVALID_ARGUMENT,
|
||||
"Invalid filter received");
|
||||
return NULL;
|
||||
}
|
||||
no_format:
|
||||
{
|
||||
g_set_error (error,
|
||||
G_IO_ERROR,
|
||||
G_IO_ERROR_NOT_FOUND,
|
||||
"No compatible format found");
|
||||
if (cfilter)
|
||||
gst_caps_unref (cfilter);
|
||||
if (caps)
|
||||
gst_caps_unref (caps);
|
||||
return NULL;
|
||||
}
|
||||
return pinos_format_filter (priv->possible_formats, filter, error);
|
||||
}
|
||||
|
||||
static void
|
||||
|
@ -625,224 +586,123 @@ parse_control_buffer (PinosPort *port, PinosBuffer *buffer)
|
|||
}
|
||||
}
|
||||
|
||||
static gboolean
|
||||
pinos_port_receive_buffer (PinosPort *port,
|
||||
PinosBuffer *buffer,
|
||||
GError **error)
|
||||
/**
|
||||
* pinos_port_create_channel:
|
||||
* @port: a #PinosPort
|
||||
* @client_path: the client path
|
||||
* @format_filter: a #GBytes
|
||||
* @props: #PinosProperties
|
||||
* @prefix: a prefix
|
||||
* @error: a #GError or %NULL
|
||||
*
|
||||
* Create a new #PinosChannel for @port.
|
||||
*
|
||||
* Returns: a new #PinosChannel or %NULL, in wich case @error will contain
|
||||
* more information about the error.
|
||||
*/
|
||||
PinosChannel *
|
||||
pinos_port_create_channel (PinosPort *port,
|
||||
const gchar *client_path,
|
||||
GBytes *format_filter,
|
||||
PinosProperties *props,
|
||||
GError **error)
|
||||
{
|
||||
PinosPortPrivate *priv = port->priv;
|
||||
PinosPortPrivate *priv;
|
||||
PinosChannel *channel;
|
||||
GBytes *possible_formats;
|
||||
|
||||
if (priv->buffer)
|
||||
goto buffer_queued;
|
||||
g_return_val_if_fail (PINOS_IS_PORT (port), NULL);
|
||||
priv = port->priv;
|
||||
|
||||
PINOS_DEBUG_TRANSPORT ("port %p: receive buffer %p", port, buffer);
|
||||
if (pinos_buffer_get_flags (buffer) & PINOS_BUFFER_FLAG_CONTROL)
|
||||
parse_control_buffer (port, buffer);
|
||||
possible_formats = pinos_port_filter_formats (port, format_filter, error);
|
||||
if (possible_formats == NULL)
|
||||
return NULL;
|
||||
|
||||
priv->buffer = buffer;
|
||||
if (priv->received_buffer_cb)
|
||||
priv->received_buffer_cb (port, priv->received_buffer_data);
|
||||
priv->buffer = NULL;
|
||||
g_debug ("port %p: make channel with formats %s", port,
|
||||
g_bytes_get_data (possible_formats, NULL));
|
||||
|
||||
return TRUE;
|
||||
channel = g_object_new (PINOS_TYPE_CHANNEL, "daemon", priv->daemon,
|
||||
"port", port,
|
||||
"client-path", client_path,
|
||||
"direction", priv->direction,
|
||||
"possible-formats", possible_formats,
|
||||
"properties", props,
|
||||
NULL);
|
||||
g_bytes_unref (possible_formats);
|
||||
|
||||
if (channel == NULL)
|
||||
goto no_channel;
|
||||
|
||||
return channel;
|
||||
|
||||
/* ERRORS */
|
||||
buffer_queued:
|
||||
no_channel:
|
||||
{
|
||||
g_set_error (error,
|
||||
G_IO_ERROR,
|
||||
G_IO_ERROR_NOT_FOUND,
|
||||
"buffer was already queued on port");
|
||||
return FALSE;
|
||||
if (error)
|
||||
*error = g_error_new (G_IO_ERROR,
|
||||
G_IO_ERROR_FAILED,
|
||||
"Could not create channel");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
update_peer_paths (PinosPort *port)
|
||||
{
|
||||
PinosPortPrivate *priv = port->priv;
|
||||
gchar **paths;
|
||||
guint i;
|
||||
gint path_index = 0;
|
||||
|
||||
paths = g_new0 (gchar *, priv->peers->len + 1);
|
||||
for (i = 0; i < priv->peers->len; i++) {
|
||||
PinosPort *peer;
|
||||
gchar *path;
|
||||
|
||||
peer = g_ptr_array_index (priv->peers, i);
|
||||
g_object_get (peer, "object-path", &path, NULL);
|
||||
paths[path_index++] = path;
|
||||
}
|
||||
|
||||
g_object_set (port, "peers", paths, NULL);
|
||||
g_strfreev (paths);
|
||||
}
|
||||
/**
|
||||
* pinos_port_link:
|
||||
* @source: a source #PinosPort
|
||||
* @destination: a destination #PinosPort
|
||||
*
|
||||
* Link two ports together.
|
||||
*
|
||||
* Returns: %TRUE if ports could be linked.
|
||||
*/
|
||||
gboolean
|
||||
pinos_port_link (PinosPort *source, PinosPort *destination)
|
||||
{
|
||||
gboolean res = TRUE;
|
||||
|
||||
g_return_val_if_fail (PINOS_IS_PORT (source), FALSE);
|
||||
g_return_val_if_fail (PINOS_IS_PORT (destination), FALSE);
|
||||
g_return_val_if_fail (source->priv->direction != destination->priv->direction, FALSE);
|
||||
|
||||
if (source->priv->peers->len >= source->priv->max_peers)
|
||||
return FALSE;
|
||||
if (destination->priv->peers->len >= destination->priv->max_peers)
|
||||
return FALSE;
|
||||
|
||||
if (source->priv->direction != PINOS_DIRECTION_OUTPUT) {
|
||||
PinosPort *tmp;
|
||||
tmp = source;
|
||||
source = destination;
|
||||
destination = tmp;
|
||||
}
|
||||
|
||||
g_signal_emit (source, signals[SIGNAL_LINKED], 0, destination, &res);
|
||||
if (!res)
|
||||
return FALSE;
|
||||
g_signal_emit (destination, signals[SIGNAL_LINKED], 0, source, &res);
|
||||
if (!res)
|
||||
return FALSE;
|
||||
|
||||
g_debug ("port %p: linked to %p", source, destination);
|
||||
g_ptr_array_add (source->priv->peers, destination);
|
||||
g_ptr_array_add (destination->priv->peers, source);
|
||||
|
||||
update_peer_paths (source);
|
||||
update_peer_paths (destination);
|
||||
|
||||
|
||||
if (source->priv->format) {
|
||||
PinosBufferBuilder builder;
|
||||
PinosBuffer pbuf;
|
||||
PinosPacketFormatChange fc;
|
||||
GError *error = NULL;
|
||||
|
||||
pinos_port_buffer_builder_init (destination, &builder);
|
||||
fc.id = 0;
|
||||
fc.format = g_bytes_get_data (source->priv->format, NULL);
|
||||
pinos_buffer_builder_add_format_change (&builder, &fc);
|
||||
pinos_buffer_builder_end (&builder, &pbuf);
|
||||
|
||||
if (!pinos_port_receive_buffer (destination, &pbuf, &error)) {
|
||||
g_warning ("port %p: counld not receive format: %s", destination, error->message);
|
||||
g_clear_error (&error);
|
||||
}
|
||||
pinos_buffer_unref (&pbuf);
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* pinos_port_unlink:
|
||||
* @source: a source #PinosPort
|
||||
* @destination: a destination #PinosPort
|
||||
*
|
||||
* Link two ports together.
|
||||
*
|
||||
* Returns: %TRUE if ports could be linked.
|
||||
*/
|
||||
gboolean
|
||||
pinos_port_unlink (PinosPort *source, PinosPort *destination)
|
||||
{
|
||||
g_return_val_if_fail (PINOS_IS_PORT (source), FALSE);
|
||||
g_return_val_if_fail (PINOS_IS_PORT (destination), FALSE);
|
||||
|
||||
g_ptr_array_remove (source->priv->peers, destination);
|
||||
g_ptr_array_remove (destination->priv->peers, source);
|
||||
|
||||
update_peer_paths (source);
|
||||
update_peer_paths (destination);
|
||||
|
||||
g_debug ("port %p: unlinked from %p", source, destination);
|
||||
g_signal_emit (source, signals[SIGNAL_UNLINKED], 0, destination);
|
||||
g_signal_emit (destination, signals[SIGNAL_UNLINKED], 0, source);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
static void
|
||||
pinos_port_unlink_all (PinosPort *port)
|
||||
{
|
||||
PinosPortPrivate *priv = port->priv;
|
||||
guint i;
|
||||
|
||||
for (i = 0; i < priv->peers->len; i++) {
|
||||
PinosPort *peer = g_ptr_array_index (priv->peers, i);
|
||||
|
||||
g_ptr_array_remove (peer->priv->peers, port);
|
||||
g_ptr_array_index (priv->peers, i) = NULL;
|
||||
|
||||
g_signal_emit (port, signals[SIGNAL_UNLINKED], 0, peer);
|
||||
g_signal_emit (peer, signals[SIGNAL_UNLINKED], 0, port);
|
||||
}
|
||||
g_ptr_array_set_size (priv->peers, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* pinos_port_get_links:
|
||||
* @port: a #PinosPort
|
||||
* @n_linkes: location to hold the result number of links
|
||||
*
|
||||
* Get the links and number of links on this port
|
||||
*
|
||||
* Returns: an array of @n_links elements of type #PinosPort.
|
||||
*/
|
||||
PinosPort *
|
||||
pinos_port_get_links (PinosPort *port, guint *n_links)
|
||||
{
|
||||
PinosPortPrivate *priv;
|
||||
|
||||
g_return_val_if_fail (PINOS_IS_PORT (port), NULL);
|
||||
priv = port->priv;
|
||||
|
||||
if (n_links)
|
||||
*n_links = priv->peers->len;
|
||||
|
||||
return (PinosPort *) priv->peers->pdata;
|
||||
}
|
||||
/**
|
||||
* pinos_port_peek_buffer:
|
||||
* @port: a #PinosPort
|
||||
*
|
||||
* Peek the buffer on @port.
|
||||
*
|
||||
* Returns: a #PinosBuffer or %NULL when no buffer has arrived on the pad.
|
||||
*/
|
||||
PinosBuffer *
|
||||
pinos_port_peek_buffer (PinosPort *port)
|
||||
{
|
||||
PinosPortPrivate *priv;
|
||||
|
||||
g_return_val_if_fail (PINOS_IS_PORT (port), NULL);
|
||||
priv = port->priv;
|
||||
|
||||
return priv->buffer;
|
||||
}
|
||||
|
||||
void
|
||||
pinos_port_buffer_builder_init (PinosPort *port,
|
||||
PinosBufferBuilder *builder)
|
||||
pinos_port_activate (PinosPort *port)
|
||||
{
|
||||
PinosPortPrivate *priv;
|
||||
|
||||
g_return_if_fail (PINOS_IS_PORT (port));
|
||||
priv = port->priv;
|
||||
g_return_if_fail (priv->active_count >= 0);
|
||||
|
||||
pinos_buffer_builder_init_into (builder,
|
||||
priv->send_data, MAX_BUFFER_SIZE,
|
||||
priv->send_fds, MAX_FDS);
|
||||
g_debug ("port %p: activate count now %d", port, priv->active_count);
|
||||
|
||||
if (priv->active_count++ == 0)
|
||||
g_signal_emit (port, signals[SIGNAL_ACTIVATE], 0, NULL);
|
||||
}
|
||||
|
||||
void
|
||||
pinos_port_deactivate (PinosPort *port)
|
||||
{
|
||||
PinosPortPrivate *priv;
|
||||
|
||||
g_return_if_fail (PINOS_IS_PORT (port));
|
||||
priv = port->priv;
|
||||
g_return_if_fail (priv->active_count > 0);
|
||||
|
||||
g_debug ("port %p: deactivate count now %d", port, priv->active_count);
|
||||
|
||||
if (--priv->active_count == 0)
|
||||
g_signal_emit (port, signals[SIGNAL_DEACTIVATE], 0, NULL);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* pinos_port_receive_buffer:
|
||||
* @port: a #PinosPort
|
||||
* @buffer: a #PinosBuffer
|
||||
* @error: a #GError or %NULL
|
||||
*
|
||||
* Receive @buffer on @port
|
||||
*
|
||||
* Returns: %TRUE on success. @error is set when %FALSE is returned.
|
||||
*/
|
||||
gboolean
|
||||
pinos_port_receive_buffer (PinosPort *port,
|
||||
PinosBuffer *buffer,
|
||||
GError **error)
|
||||
{
|
||||
gboolean res = TRUE;
|
||||
PinosPortPrivate *priv = port->priv;
|
||||
|
||||
PINOS_DEBUG_TRANSPORT ("port %p: receive buffer %p", port, buffer);
|
||||
if (pinos_buffer_get_flags (buffer) & PINOS_BUFFER_FLAG_CONTROL)
|
||||
parse_control_buffer (port, buffer);
|
||||
|
||||
if (priv->received_buffer_cb)
|
||||
res = priv->received_buffer_cb (port, buffer, error, priv->received_buffer_data);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -851,7 +711,7 @@ pinos_port_buffer_builder_init (PinosPort *port,
|
|||
* @buffer: a #PinosBuffer
|
||||
* @error: a #GError or %NULL
|
||||
*
|
||||
* Send @buffer to ports connected to @port
|
||||
* Send @buffer out on @port.
|
||||
*
|
||||
* Returns: %TRUE on success. @error is set when %FALSE is returned.
|
||||
*/
|
||||
|
@ -860,29 +720,21 @@ pinos_port_send_buffer (PinosPort *port,
|
|||
PinosBuffer *buffer,
|
||||
GError **error)
|
||||
{
|
||||
PinosPortPrivate *priv;
|
||||
PinosPort *peer;
|
||||
gboolean res = TRUE;
|
||||
guint i;
|
||||
GError *err = NULL;
|
||||
PinosPortPrivate *priv;
|
||||
GList *walk;
|
||||
|
||||
g_return_val_if_fail (PINOS_IS_PORT (port), FALSE);
|
||||
priv = port->priv;
|
||||
|
||||
PINOS_DEBUG_TRANSPORT ("port %p: send buffer %p", port, buffer);
|
||||
if (pinos_buffer_get_flags (buffer) & PINOS_BUFFER_FLAG_CONTROL)
|
||||
parse_control_buffer (port, buffer);
|
||||
|
||||
PINOS_DEBUG_TRANSPORT ("port %p: send buffer %p", port, buffer);
|
||||
priv = port->priv;
|
||||
|
||||
for (i = 0; i < priv->peers->len; i++) {
|
||||
peer = g_ptr_array_index (priv->peers, i);
|
||||
res = pinos_port_receive_buffer (peer, buffer, &err);
|
||||
for (walk = priv->send_datas; walk; walk = g_list_next (walk)) {
|
||||
SendData *data = walk->data;
|
||||
data->send_buffer_cb (port, buffer, error, data->send_buffer_data);
|
||||
}
|
||||
if (!res) {
|
||||
if (error == NULL)
|
||||
g_warning ("could not send buffer: %s", err->message);
|
||||
g_propagate_error (error, err);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
|
|
@ -30,6 +30,7 @@ typedef struct _PinosPortPrivate PinosPortPrivate;
|
|||
|
||||
#include <pinos/client/introspect.h>
|
||||
#include <pinos/client/buffer.h>
|
||||
#include <pinos/server/channel.h>
|
||||
#include <pinos/server/daemon.h>
|
||||
|
||||
#define PINOS_TYPE_PORT (pinos_port_get_type ())
|
||||
|
@ -61,48 +62,50 @@ struct _PinosPortClass {
|
|||
GObjectClass parent_class;
|
||||
};
|
||||
|
||||
typedef void (*PinosReceivedBufferCallback) (PinosPort *port, gpointer user_data);
|
||||
typedef gboolean (*PinosBufferCallback) (PinosPort *port, PinosBuffer *buffer, GError **error, gpointer user_data);
|
||||
|
||||
/* normal GObject stuff */
|
||||
GType pinos_port_get_type (void);
|
||||
|
||||
void pinos_port_set_received_buffer_cb (PinosPort *port,
|
||||
PinosReceivedBufferCallback cb,
|
||||
PinosBufferCallback cb,
|
||||
gpointer user_data,
|
||||
GDestroyNotify notify);
|
||||
gulong pinos_port_add_send_buffer_cb (PinosPort *port,
|
||||
PinosBufferCallback cb,
|
||||
gpointer user_data,
|
||||
GDestroyNotify notify);
|
||||
void pinos_port_remove_send_buffer_cb (PinosPort *port,
|
||||
gulong id);
|
||||
|
||||
void pinos_port_remove (PinosPort *port);
|
||||
|
||||
PinosNode * pinos_port_get_node (PinosPort *port);
|
||||
GSocket * pinos_port_get_socket (PinosPort *port);
|
||||
const gchar * pinos_port_get_name (PinosPort *port);
|
||||
PinosDirection pinos_port_get_direction (PinosPort *port);
|
||||
GBytes * pinos_port_get_possible_formats (PinosPort *port);
|
||||
GBytes * pinos_port_get_format (PinosPort *port);
|
||||
PinosProperties * pinos_port_get_properties (PinosPort *port);
|
||||
|
||||
GBytes * pinos_port_get_possible_formats (PinosPort *port);
|
||||
GBytes * pinos_port_filter_formats (PinosPort *port,
|
||||
GBytes *filter,
|
||||
GError **error);
|
||||
GBytes * pinos_port_get_format (PinosPort *port);
|
||||
|
||||
GSocket * pinos_port_get_socket_pair (PinosPort *port,
|
||||
GError **error);
|
||||
void pinos_port_activate (PinosPort *port);
|
||||
void pinos_port_deactivate (PinosPort *port);
|
||||
|
||||
gboolean pinos_port_link (PinosPort *source,
|
||||
PinosPort *destination);
|
||||
gboolean pinos_port_unlink (PinosPort *source,
|
||||
PinosPort *destination);
|
||||
PinosPort * pinos_port_get_links (PinosPort *port,
|
||||
guint *n_links);
|
||||
|
||||
PinosBuffer * pinos_port_peek_buffer (PinosPort *port);
|
||||
|
||||
void pinos_port_buffer_builder_init (PinosPort *port,
|
||||
PinosBufferBuilder *builder);
|
||||
PinosChannel * pinos_port_create_channel (PinosPort *port,
|
||||
const gchar *client_path,
|
||||
GBytes *format_filter,
|
||||
PinosProperties *props,
|
||||
GError **error);
|
||||
|
||||
gboolean pinos_port_send_buffer (PinosPort *port,
|
||||
PinosBuffer *buffer,
|
||||
GError **error);
|
||||
gboolean pinos_port_receive_buffer (PinosPort *port,
|
||||
PinosBuffer *buffer,
|
||||
GError **error);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
|
|
84
pinos/server/utils.c
Normal file
84
pinos/server/utils.c
Normal file
|
@ -0,0 +1,84 @@
|
|||
/* Pinos
|
||||
* Copyright (C) 2015 Wim Taymans <wim.taymans@gmail.com>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the
|
||||
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
||||
* Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <gst/gst.h>
|
||||
#include <gio/gio.h>
|
||||
|
||||
#include "pinos/server/utils.h"
|
||||
|
||||
GBytes *
|
||||
pinos_format_filter (GBytes *format,
|
||||
GBytes *filter,
|
||||
GError **error)
|
||||
{
|
||||
GstCaps *tmp, *caps, *cfilter;
|
||||
gchar *str;
|
||||
GBytes *res;
|
||||
|
||||
if (filter) {
|
||||
cfilter = gst_caps_from_string (g_bytes_get_data (filter, NULL));
|
||||
if (cfilter == NULL)
|
||||
goto invalid_filter;
|
||||
} else {
|
||||
cfilter = NULL;
|
||||
}
|
||||
|
||||
if (format)
|
||||
caps = gst_caps_from_string (g_bytes_get_data (format, NULL));
|
||||
else
|
||||
caps = gst_caps_new_any ();
|
||||
|
||||
if (caps && cfilter) {
|
||||
tmp = gst_caps_intersect_full (caps, cfilter, GST_CAPS_INTERSECT_FIRST);
|
||||
gst_caps_take (&caps, tmp);
|
||||
}
|
||||
g_clear_pointer (&cfilter, gst_caps_unref);
|
||||
|
||||
if (caps == NULL || gst_caps_is_empty (caps))
|
||||
goto no_format;
|
||||
|
||||
str = gst_caps_to_string (caps);
|
||||
gst_caps_unref (caps);
|
||||
res = g_bytes_new_take (str, strlen (str) + 1);
|
||||
|
||||
return res;
|
||||
|
||||
invalid_filter:
|
||||
{
|
||||
g_set_error (error,
|
||||
G_IO_ERROR,
|
||||
G_IO_ERROR_INVALID_ARGUMENT,
|
||||
"Invalid filter received");
|
||||
return NULL;
|
||||
}
|
||||
no_format:
|
||||
{
|
||||
g_set_error (error,
|
||||
G_IO_ERROR,
|
||||
G_IO_ERROR_NOT_FOUND,
|
||||
"No compatible format found");
|
||||
if (cfilter)
|
||||
gst_caps_unref (cfilter);
|
||||
if (caps)
|
||||
gst_caps_unref (caps);
|
||||
return NULL;
|
||||
}
|
||||
}
|
31
pinos/server/utils.h
Normal file
31
pinos/server/utils.h
Normal file
|
@ -0,0 +1,31 @@
|
|||
/* Pinos
|
||||
* Copyright (C) 2015 Wim Taymans <wim.taymans@gmail.com>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the
|
||||
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
||||
* Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#ifndef __PINOS_UTILS_H__
|
||||
#define __PINOS_UTILS_H__
|
||||
|
||||
#include <glib-object.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
GBytes * pinos_format_filter (GBytes *format1, GBytes *format2, GError **error);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /* __PINOS_UTILS_H__ */
|
Loading…
Reference in a new issue