Rework transport protocol

Use a more simple tranport protocol for the realtime data. Use a piece
of shared memory and a socket where we use a 1byte read/write to trigger
remote actions. Also use a ringbuffer for events.
This commit is contained in:
Wim Taymans 2016-11-07 10:24:13 +01:00
parent 7e46f9e3ad
commit b774b99db5
22 changed files with 797 additions and 364 deletions

View file

@ -26,8 +26,6 @@ extern "C" {
typedef struct _PinosArray PinosArray;
#include <string.h>
#include <spa/defs.h>
struct _PinosArray {
@ -52,7 +50,8 @@ struct _PinosArray {
static inline void
pinos_array_init (PinosArray *arr)
{
memset (arr, 0, sizeof (PinosArray));
arr->data = NULL;
arr->size = arr->alloc = 0;
}
static inline void

View file

@ -630,6 +630,12 @@ pinos_connection_parse_message (PinosConnection *conn,
connection_parse_port_command (conn, message);
break;
case PINOS_MESSAGE_TRANSPORT_UPDATE:
if (conn->in.size < sizeof (PinosMessageTransportUpdate))
return false;
memcpy (message, conn->in.data, sizeof (PinosMessageTransportUpdate));
break;
case PINOS_MESSAGE_INVALID:
return false;
}
@ -781,6 +787,11 @@ pinos_connection_add_message (PinosConnection *conn,
connection_add_port_command (conn, message);
break;
case PINOS_MESSAGE_TRANSPORT_UPDATE:
p = connection_add_message (conn, type, sizeof (PinosMessageTransportUpdate));
memcpy (p, message, sizeof (PinosMessageTransportUpdate));
break;
case PINOS_MESSAGE_INVALID:
return false;
}

View file

@ -43,18 +43,21 @@ typedef enum {
PINOS_MESSAGE_NODE_EVENT = 5,
/* server to client */
PINOS_MESSAGE_ADD_PORT = 32,
PINOS_MESSAGE_REMOVE_PORT = 33,
PINOS_MESSAGE_TRANSPORT_UPDATE = 32,
PINOS_MESSAGE_SET_FORMAT = 34,
PINOS_MESSAGE_SET_PROPERTY = 35,
PINOS_MESSAGE_ADD_PORT = 33,
PINOS_MESSAGE_REMOVE_PORT = 34,
PINOS_MESSAGE_NODE_COMMAND = 36,
PINOS_MESSAGE_PORT_COMMAND = 37,
PINOS_MESSAGE_SET_FORMAT = 35,
PINOS_MESSAGE_SET_PROPERTY = 36,
PINOS_MESSAGE_NODE_COMMAND = 37,
PINOS_MESSAGE_PORT_COMMAND = 38,
/* both */
PINOS_MESSAGE_ADD_MEM = 64,
PINOS_MESSAGE_USE_BUFFERS = 66,
PINOS_MESSAGE_PROCESS_BUFFER = 67,
} PinosMessageType;
@ -142,6 +145,13 @@ typedef struct {
SpaNodeCommand *command;
} PinosMessagePortCommand;
/* PINOS_MESSAGE_TRANSPORT_UPDATE */
typedef struct {
unsigned int memfd_index;
off_t offset;
size_t size;
} PinosMessageTransportUpdate;
/* PINOS_MESSAGE_ADD_MEM */
typedef struct {
SpaDirection direction;

View file

@ -21,6 +21,8 @@
#include <pinos/client/log.h>
SpaLogLevel pinos_log_level = SPA_LOG_LEVEL_DEBUG;
static void
do_logv (SpaLog *log,
SpaLogLevel level,

View file

@ -26,6 +26,8 @@
extern "C" {
#endif
extern SpaLogLevel pinos_log_level;
SpaLog * pinos_log_get (void);
@ -43,21 +45,27 @@ void pinos_log_logv (SpaLogLevel level,
#if __STDC_VERSION__ >= 199901L
#define pinos_log_error(...) pinos_log_log(SPA_LOG_LEVEL_ERROR,__FILE__,__LINE__,__func__,__VA_ARGS__)
#define pinos_log_warn(...) pinos_log_log(SPA_LOG_LEVEL_WARN,__FILE__,__LINE__,__func__,__VA_ARGS__)
#define pinos_log_info(...) pinos_log_log(SPA_LOG_LEVEL_INFO,__FILE__,__LINE__,__func__,__VA_ARGS__)
#define pinos_log_debug(...) pinos_log_log(SPA_LOG_LEVEL_DEBUG,__FILE__,__LINE__,__func__,__VA_ARGS__)
#define pinos_log_trace(...) pinos_log_log(SPA_LOG_LEVEL_TRACE,__FILE__,__LINE__,__func__,__VA_ARGS__)
#define pinos_log_logc(lev,...) \
if (SPA_UNLIKELY (lev >= pinos_log_level)) \
pinos_log_log(lev,__VA_ARGS__)
#define pinos_log_error(...) pinos_log_logc(SPA_LOG_LEVEL_ERROR,__FILE__,__LINE__,__func__,__VA_ARGS__)
#define pinos_log_warn(...) pinos_log_logc(SPA_LOG_LEVEL_WARN,__FILE__,__LINE__,__func__,__VA_ARGS__)
#define pinos_log_info(...) pinos_log_logc(SPA_LOG_LEVEL_INFO,__FILE__,__LINE__,__func__,__VA_ARGS__)
#define pinos_log_debug(...) pinos_log_logc(SPA_LOG_LEVEL_DEBUG,__FILE__,__LINE__,__func__,__VA_ARGS__)
#define pinos_log_trace(...) pinos_log_logc(SPA_LOG_LEVEL_TRACE,__FILE__,__LINE__,__func__,__VA_ARGS__)
#else
#define PINOS_LOG_FUNC(name,lev) \
static inline void pinos_log_##name (const char *format, ...) \
{ \
va_list varargs; \
va_start (varargs, format); \
pinos_log_logv (lev,__FILE__,__LINE__,__func__,format,varargs); \
va_end (varargs); \
if (lev >= pinos_log_level) { \
va_list varargs; \
va_start (varargs, format); \
pinos_log_logv (lev,__FILE__,__LINE__,__func__,format,varargs); \
va_end (varargs); \
} \
}
PINOS_LOG_FUNC(error, SPA_LOG_LEVEL_ERROR)
PINOS_LOG_FUNC(warn, SPA_LOG_LEVEL_WARN)

150
pinos/client/mem.c Normal file
View file

@ -0,0 +1,150 @@
/* Pinos
* Copyright (C) 2016 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 <stddef.h>
#include <stdio.h>
#include <errno.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <pinos/client/log.h>
#include <pinos/client/mem.h>
/*
* No glibc wrappers exist for memfd_create(2), so provide our own.
*
* Also define memfd fcntl sealing macros. While they are already
* defined in the kernel header file <linux/fcntl.h>, that file as
* a whole conflicts with the original glibc header <fnctl.h>.
*/
static inline int memfd_create(const char *name, unsigned int flags) {
return syscall(SYS_memfd_create, name, flags);
}
/* memfd_create(2) flags */
#ifndef MFD_CLOEXEC
#define MFD_CLOEXEC 0x0001U
#endif
#ifndef MFD_ALLOW_SEALING
#define MFD_ALLOW_SEALING 0x0002U
#endif
/* fcntl() seals-related flags */
#ifndef F_LINUX_SPECIFIC_BASE
#define F_LINUX_SPECIFIC_BASE 1024
#endif
#ifndef F_ADD_SEALS
#define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
#define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
#define F_SEAL_SEAL 0x0001 /* prevent further seals from being set */
#define F_SEAL_SHRINK 0x0002 /* prevent file from shrinking */
#define F_SEAL_GROW 0x0004 /* prevent file from growing */
#define F_SEAL_WRITE 0x0008 /* prevent writes */
#endif
#undef USE_MEMFD
SpaResult
pinos_memblock_alloc (PinosMemblockFlags flags,
size_t size,
PinosMemblock *mem)
{
if (mem == NULL || size == 0)
return SPA_RESULT_INVALID_ARGUMENTS;
mem->flags = flags;
mem->size = size;
if (flags & PINOS_MEMBLOCK_FLAG_WITH_FD) {
#ifdef USE_MEMFD
mem->fd = memfd_create ("pinos-memfd", MFD_CLOEXEC | MFD_ALLOW_SEALING);
if (mem->fd == -1) {
pinos_log_error ("Failed to create memfd: %s\n", strerror (errno));
return SPA_RESULT_ERROR;
}
#else
char filename[] = "/dev/shm/spa-tmpfile.XXXXXX";
mem->fd = mkostemp (filename, O_CLOEXEC);
if (mem->fd == -1) {
pinos_log_error ("Failed to create temporary file: %s\n", strerror (errno));
return SPA_RESULT_ERROR;
}
unlink (filename);
#endif
if (ftruncate (mem->fd, size) < 0) {
pinos_log_warn ("Failed to truncate temporary file: %s", strerror (errno));
close (mem->fd);
return SPA_RESULT_ERROR;
}
#ifdef USE_MEMFD
if (flags & PINOS_MEMBLOCK_FLAG_SEAL) {
unsigned int seals = F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL;
if (fcntl (mem->fd, F_ADD_SEALS, seals) == -1) {
pinos_log_warn ("Failed to add seals: %s", strerror (errno));
}
}
#endif
if (flags & PINOS_MEMBLOCK_FLAG_MAP_READWRITE) {
int prot = 0;
if (flags & PINOS_MEMBLOCK_FLAG_MAP_READ)
prot |= PROT_READ;
if (flags & PINOS_MEMBLOCK_FLAG_MAP_WRITE)
prot |= PROT_WRITE;
mem->ptr = mmap (NULL, size, prot, MAP_SHARED, mem->fd, 0);
} else {
mem->ptr = NULL;
}
} else {
mem->ptr = malloc (size);
mem->fd = -1;
}
return SPA_RESULT_OK;
}
void
pinos_memblock_free (PinosMemblock *mem)
{
if (mem == NULL)
return;
if (mem->flags & PINOS_MEMBLOCK_FLAG_WITH_FD) {
if (mem->ptr)
munmap (mem->ptr, mem->size);
if (mem->fd != -1)
close (mem->fd);
} else {
free (mem->ptr);
}
mem->ptr = NULL;
mem->fd = -1;
}

57
pinos/client/mem.h Normal file
View file

@ -0,0 +1,57 @@
/* Pinos
* Copyright (C) 2016 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_MEM_H__
#define __PINOS_MEM_H__
#include <spa/defs.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct _PinosMemblock PinosMemblock;
typedef enum {
PINOS_MEMBLOCK_FLAG_NONE = 0,
PINOS_MEMBLOCK_FLAG_WITH_FD = (1 << 0),
PINOS_MEMBLOCK_FLAG_MAP_READ = (1 << 1),
PINOS_MEMBLOCK_FLAG_MAP_WRITE = (1 << 2),
PINOS_MEMBLOCK_FLAG_SEAL = (1 << 3),
} PinosMemblockFlags;
#define PINOS_MEMBLOCK_FLAG_MAP_READWRITE (PINOS_MEMBLOCK_FLAG_MAP_READ | PINOS_MEMBLOCK_FLAG_MAP_WRITE)
struct _PinosMemblock {
PinosMemblockFlags flags;
int fd;
void *ptr;
size_t size;
};
SpaResult pinos_memblock_alloc (PinosMemblockFlags flags,
size_t size,
PinosMemblock *mem);
void pinos_memblock_free (PinosMemblock *mem);
#ifdef __cplusplus
}
#endif
#endif /* __PINOS_MEM_H__ */

View file

@ -0,0 +1,59 @@
/* Simple Plugin API
* Copyright (C) 2016 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/syscall.h>
#include <fcntl.h>
/*
* No glibc wrappers exist for memfd_create(2), so provide our own.
*
* Also define memfd fcntl sealing macros. While they are already
* defined in the kernel header file <linux/fcntl.h>, that file as
* a whole conflicts with the original glibc header <fnctl.h>.
*/
static inline int memfd_create(const char *name, unsigned int flags) {
return syscall(SYS_memfd_create, name, flags);
}
/* memfd_create(2) flags */
#ifndef MFD_CLOEXEC
#define MFD_CLOEXEC 0x0001U
#endif
#ifndef MFD_ALLOW_SEALING
#define MFD_ALLOW_SEALING 0x0002U
#endif
/* fcntl() seals-related flags */
#ifndef F_LINUX_SPECIFIC_BASE
#define F_LINUX_SPECIFIC_BASE 1024
#endif
#ifndef F_ADD_SEALS
#define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
#define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
#define F_SEAL_SEAL 0x0001 /* prevent further seals from being set */
#define F_SEAL_SHRINK 0x0002 /* prevent file from shrinking */
#define F_SEAL_GROW 0x0004 /* prevent file from growing */
#define F_SEAL_WRITE 0x0008 /* prevent writes */
#endif

View file

@ -5,6 +5,7 @@ pinos_headers = [
'introspect.h',
'log.h',
'map.h',
'mem.h',
'pinos.h',
'properties.h',
'ringbuffer.h',
@ -12,6 +13,7 @@ pinos_headers = [
'stream.h',
'subscribe.h',
'thread-mainloop.h',
'transport.h',
]
pinos_sources = [
@ -21,6 +23,7 @@ pinos_sources = [
'introspect.c',
'log.c',
'mapper.c',
'mem.c',
'properties.c',
'serialize.c',
'stream.c',
@ -29,6 +32,7 @@ pinos_sources = [
'rtkit.c',
'subscribe.c',
'thread-mainloop.c',
'transport.c',
gdbus_target,
]

View file

@ -26,11 +26,13 @@ extern const char g_log_domain_pinos[];
#include <pinos/client/enumtypes.h>
#include <pinos/client/introspect.h>
#include <pinos/client/log.h>
#include <pinos/client/mem.h>
#include <pinos/client/thread-mainloop.h>
#include <pinos/client/properties.h>
#include <pinos/client/ringbuffer.h>
#include <pinos/client/stream.h>
#include <pinos/client/subscribe.h>
#include <spa/include/spa/id-map.h>
#define PINOS_DBUS_SERVICE "org.pinos"

View file

@ -38,6 +38,7 @@
#include "pinos/client/format.h"
#include "pinos/client/private.h"
#include "pinos/client/serialize.h"
#include "pinos/client/transport.h"
#define MAX_BUFFER_SIZE 4096
#define MAX_FDS 32
@ -90,12 +91,13 @@ struct _PinosStreamPrivate
GSocket *socket;
GSource *socket_source;
int fd;
GSocket *rtsocket;
GSource *rtsocket_source;
int rtfd;
PinosConnection *conn;
PinosConnection *rtconn;
PinosTransport *trans;
GSource *timeout_source;
@ -662,21 +664,11 @@ add_port_update (PinosStream *stream, uint32_t change_mask)
pinos_connection_add_message (priv->conn, PINOS_MESSAGE_PORT_UPDATE, &pu);
}
static void
send_need_input (PinosStream *stream, uint32_t port_id)
static inline void
send_need_input (PinosStream *stream)
{
PinosStreamPrivate *priv = stream->priv;
PinosMessageNodeEvent cne;
SpaNodeEventNeedInput ni;
cne.event = &ni.event;
ni.event.type = SPA_NODE_EVENT_TYPE_NEED_INPUT;
ni.event.size = sizeof (ni);
ni.port_id = port_id;
pinos_connection_add_message (priv->rtconn, PINOS_MESSAGE_NODE_EVENT, &cne);
if (!pinos_connection_flush (priv->rtconn))
pinos_log_warn ("stream %p: error writing connection", stream);
pinos_transport_write_cmd (priv->trans, PINOS_TRANSPORT_CMD_NEED_DATA);
}
static void
@ -712,47 +704,6 @@ add_async_complete (PinosStream *stream,
pinos_connection_add_message (priv->conn, PINOS_MESSAGE_NODE_EVENT, &cne);
}
static void
send_reuse_buffer (PinosStream *stream, uint32_t port_id, uint32_t buffer_id)
{
PinosStreamPrivate *priv = stream->priv;
PinosMessageNodeEvent cne;
SpaNodeEventReuseBuffer rb;
cne.event = &rb.event;
rb.event.type = SPA_NODE_EVENT_TYPE_REUSE_BUFFER;
rb.event.size = sizeof (rb);
rb.port_id = port_id;
rb.buffer_id = buffer_id;
pinos_connection_add_message (priv->rtconn, PINOS_MESSAGE_NODE_EVENT, &cne);
if (!pinos_connection_flush (priv->rtconn))
pinos_log_warn ("stream %p: error writing connection", stream);
}
static void
send_process_buffer (PinosStream *stream, uint32_t port_id, uint32_t buffer_id)
{
PinosStreamPrivate *priv = stream->priv;
PinosMessageProcessBuffer pb;
PinosMessageNodeEvent cne;
SpaNodeEventHaveOutput ho;
pb.direction = priv->direction;
pb.port_id = port_id;
pb.buffer_id = buffer_id;
pinos_connection_add_message (priv->rtconn, PINOS_MESSAGE_PROCESS_BUFFER, &pb);
cne.event = &ho.event;
ho.event.type = SPA_NODE_EVENT_TYPE_HAVE_OUTPUT;
ho.event.size = sizeof (ho);
ho.port_id = port_id;
pinos_connection_add_message (priv->rtconn, PINOS_MESSAGE_NODE_EVENT, &cne);
if (!pinos_connection_flush (priv->rtconn))
pinos_log_warn ("stream %p: error writing connection", stream);
}
static void
do_node_init (PinosStream *stream)
{
@ -822,47 +773,6 @@ handle_node_event (PinosStream *stream,
return TRUE;
}
static gboolean
handle_rtnode_event (PinosStream *stream,
SpaNodeEvent *event)
{
PinosStreamPrivate *priv = stream->priv;
switch (event->type) {
case SPA_NODE_EVENT_TYPE_INVALID:
case SPA_NODE_EVENT_TYPE_ASYNC_COMPLETE:
case SPA_NODE_EVENT_TYPE_ERROR:
case SPA_NODE_EVENT_TYPE_BUFFERING:
case SPA_NODE_EVENT_TYPE_REQUEST_REFRESH:
case SPA_NODE_EVENT_TYPE_REQUEST_CLOCK_UPDATE:
pinos_log_warn ("unexpected node event %d", event->type);
break;
case SPA_NODE_EVENT_TYPE_HAVE_OUTPUT:
case SPA_NODE_EVENT_TYPE_NEED_INPUT:
pinos_log_warn ("unhandled node event %d", event->type);
break;
case SPA_NODE_EVENT_TYPE_REUSE_BUFFER:
{
SpaNodeEventReuseBuffer *p = (SpaNodeEventReuseBuffer *) event;
BufferId *bid;
if (p->port_id != priv->port_id)
break;
if (priv->direction != SPA_DIRECTION_OUTPUT)
break;
if ((bid = find_buffer (stream, p->buffer_id))) {
bid->used = FALSE;
g_signal_emit (stream, signals[SIGNAL_NEW_BUFFER], 0, p->buffer_id);
}
break;
}
}
return TRUE;
}
static gboolean
handle_node_command (PinosStream *stream,
uint32_t seq,
@ -896,7 +806,7 @@ handle_node_command (PinosStream *stream,
pinos_log_warn ("stream %p: error writing connection", stream);
if (priv->direction == SPA_DIRECTION_INPUT)
send_need_input (stream, priv->port_id);
send_need_input (stream);
stream_set_state (stream, PINOS_STREAM_STATE_STREAMING, NULL);
break;
@ -1145,6 +1055,29 @@ parse_connection (PinosStream *stream)
break;
}
case PINOS_MESSAGE_TRANSPORT_UPDATE:
{
PinosMessageTransportUpdate p;
PinosTransportInfo info;
if (!pinos_connection_parse_message (conn, &p))
break;
info.memfd = pinos_connection_get_fd (conn, p.memfd_index, false);
if (info.memfd == -1)
break;
info.offset = p.offset;
info.size = p.size;
info.fd = priv->rtfd;
pinos_log_debug ("transport update %d %p", priv->rtfd, priv->trans);
if (priv->trans)
pinos_transport_free (priv->trans);
priv->trans = pinos_transport_new_from_info (&info);
break;
}
case PINOS_MESSAGE_INVALID:
pinos_log_warn ("unhandled message %d", type);
break;
@ -1153,73 +1086,6 @@ parse_connection (PinosStream *stream)
return TRUE;
}
static gboolean
parse_rtconnection (PinosStream *stream)
{
PinosStreamPrivate *priv = stream->priv;
PinosConnection *conn = priv->rtconn;
while (pinos_connection_has_next (conn)) {
PinosMessageType type = pinos_connection_get_type (conn);
switch (type) {
case PINOS_MESSAGE_INVALID:
case PINOS_MESSAGE_NODE_UPDATE:
case PINOS_MESSAGE_PORT_UPDATE:
case PINOS_MESSAGE_PORT_STATUS_CHANGE:
case PINOS_MESSAGE_NODE_STATE_CHANGE:
case PINOS_MESSAGE_ADD_PORT:
case PINOS_MESSAGE_REMOVE_PORT:
case PINOS_MESSAGE_SET_FORMAT:
case PINOS_MESSAGE_SET_PROPERTY:
case PINOS_MESSAGE_ADD_MEM:
case PINOS_MESSAGE_USE_BUFFERS:
case PINOS_MESSAGE_NODE_COMMAND:
pinos_log_warn ("got unexpected command %d", type);
break;
case PINOS_MESSAGE_PROCESS_BUFFER:
{
PinosMessageProcessBuffer p;
guint i;
BufferId *bid;
if (priv->direction != SPA_DIRECTION_INPUT)
break;
if (!pinos_connection_parse_message (conn, &p))
break;
if ((bid = find_buffer (stream, p.buffer_id))) {
for (i = 0; i < bid->buf->n_datas; i++) {
bid->buf->datas[i].size = bid->datas[i].size;
}
g_signal_emit (stream, signals[SIGNAL_NEW_BUFFER], 0, p.buffer_id);
send_need_input (stream, priv->port_id);
}
break;
}
case PINOS_MESSAGE_NODE_EVENT:
{
PinosMessageNodeEvent p;
if (!pinos_connection_parse_message (conn, &p))
break;
handle_rtnode_event (stream, p.event);
break;
}
case PINOS_MESSAGE_PORT_COMMAND:
{
break;
}
}
}
return TRUE;
}
static gboolean
on_socket_condition (GSocket *socket,
GIOCondition condition,
@ -1244,22 +1110,95 @@ on_socket_condition (GSocket *socket,
return TRUE;
}
static gboolean
handle_rtnode_event (PinosStream *stream,
SpaNodeEvent *event)
{
PinosStreamPrivate *priv = stream->priv;
switch (event->type) {
case SPA_NODE_EVENT_TYPE_INVALID:
case SPA_NODE_EVENT_TYPE_ASYNC_COMPLETE:
case SPA_NODE_EVENT_TYPE_ERROR:
case SPA_NODE_EVENT_TYPE_BUFFERING:
case SPA_NODE_EVENT_TYPE_REQUEST_REFRESH:
case SPA_NODE_EVENT_TYPE_REQUEST_CLOCK_UPDATE:
pinos_log_warn ("unexpected node event %d", event->type);
break;
case SPA_NODE_EVENT_TYPE_HAVE_OUTPUT:
case SPA_NODE_EVENT_TYPE_NEED_INPUT:
pinos_log_warn ("unhandled node event %d", event->type);
break;
case SPA_NODE_EVENT_TYPE_REUSE_BUFFER:
{
SpaNodeEventReuseBuffer *p = (SpaNodeEventReuseBuffer *) event;
BufferId *bid;
if (p->port_id != priv->port_id)
break;
if (priv->direction != SPA_DIRECTION_OUTPUT)
break;
if ((bid = find_buffer (stream, p->buffer_id))) {
bid->used = FALSE;
g_signal_emit (stream, signals[SIGNAL_NEW_BUFFER], 0, p->buffer_id);
}
break;
}
}
return TRUE;
}
static gboolean
on_rtsocket_condition (GSocket *socket,
GIOCondition condition,
gpointer user_data)
{
PinosStream *stream = user_data;
PinosStreamPrivate *priv = stream->priv;
switch (condition) {
case G_IO_IN:
{
parse_rtconnection (stream);
uint8_t cmd;
int i;
pinos_transport_read_cmd (priv->trans, &cmd);
if (cmd & PINOS_TRANSPORT_CMD_HAVE_DATA) {
BufferId *bid;
for (i = 0; i < priv->trans->area->n_input_info; i++) {
SpaPortInputInfo *info = &priv->trans->input_info[i];
if (info->buffer_id == SPA_ID_INVALID)
continue;
if ((bid = find_buffer (stream, info->buffer_id))) {
for (i = 0; i < bid->buf->n_datas; i++) {
bid->buf->datas[i].size = bid->datas[i].size;
}
g_signal_emit (stream, signals[SIGNAL_NEW_BUFFER], 0, bid->id);
}
info->buffer_id = SPA_ID_INVALID;
}
send_need_input (stream);
}
if (cmd & PINOS_TRANSPORT_CMD_HAVE_EVENT) {
SpaNodeEvent event;
while (pinos_transport_next_event (priv->trans, &event) == SPA_RESULT_OK) {
SpaNodeEvent *ev = alloca (event.size);
pinos_transport_parse_event (priv->trans, ev);
handle_rtnode_event (stream, ev);
}
}
break;
}
case G_IO_OUT:
pinos_log_warn ("can do IO\n");
pinos_log_warn ("can do IO");
break;
default:
@ -1305,7 +1244,8 @@ handle_socket (PinosStream *stream, gint fd, gint rtfd)
priv->rtsocket_source = g_socket_create_source (priv->rtsocket, G_IO_IN, NULL);
g_source_set_callback (priv->rtsocket_source, (GSourceFunc) on_rtsocket_condition, stream, NULL);
g_source_attach (priv->rtsocket_source, priv->context->priv->context);
priv->rtconn = pinos_connection_new (priv->rtfd);
pinos_log_debug ("sockets %d %d", priv->fd, priv->rtfd);
priv->timeout_source = g_timeout_source_new (100);
g_source_set_callback (priv->timeout_source, (GSourceFunc) on_timeout, stream, NULL);
@ -1863,13 +1803,19 @@ pinos_stream_recycle_buffer (PinosStream *stream,
guint id)
{
PinosStreamPrivate *priv;
SpaNodeEventReuseBuffer rb;
g_return_val_if_fail (PINOS_IS_STREAM (stream), FALSE);
g_return_val_if_fail (id != SPA_ID_INVALID, FALSE);
priv = stream->priv;
g_return_val_if_fail (priv->direction == SPA_DIRECTION_INPUT, FALSE);
send_reuse_buffer (stream, priv->port_id, id);
rb.event.type = SPA_NODE_EVENT_TYPE_REUSE_BUFFER;
rb.event.size = sizeof (rb);
rb.port_id = priv->port_id;
rb.buffer_id = id;
pinos_transport_add_event (priv->trans, &rb.event);
pinos_transport_write_cmd (priv->trans, PINOS_TRANSPORT_CMD_HAVE_EVENT);
return TRUE;
}
@ -1929,7 +1875,9 @@ pinos_stream_send_buffer (PinosStream *stream,
for (i = 0; i < bid->buf->n_datas; i++) {
bid->datas[i].size = bid->buf->datas[i].size;
}
send_process_buffer (stream, priv->port_id, id);
priv->trans->output_info[0].buffer_id = id;
priv->trans->output_info[0].status = SPA_RESULT_OK;
pinos_transport_write_cmd (priv->trans, PINOS_TRANSPORT_CMD_HAVE_DATA);
return TRUE;
} else {
return FALSE;

251
pinos/client/transport.c Normal file
View file

@ -0,0 +1,251 @@
/* Simple Plugin API
* Copyright (C) 2016 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 <unistd.h>
#include <sys/mman.h>
#include <pinos/client/transport.h>
#define INPUT_BUFFER_SIZE (1<<12)
#define OUTPUT_BUFFER_SIZE (1<<12)
#define CMD_NONE 0
#define CMD_PROCESS_DATA (1<<0)
#define CMD_PROCESS_EVENTS (1<<1)
#define CMD_PROCESS_SYNC (1<<2)
typedef struct {
PinosTransport trans;
PinosMemblock mem;
size_t offset;
SpaRingbufferArea areas[2];
SpaNodeEvent current;
} PinosTransportImpl;
static size_t
transport_area_get_size (PinosTransportArea *area)
{
size_t size;
size = sizeof (PinosTransportArea);
size += area->max_input_info * sizeof (SpaPortInputInfo);
size += area->max_output_info * sizeof (SpaPortOutputInfo);
size += sizeof (SpaRingbuffer);
size += INPUT_BUFFER_SIZE;
size += sizeof (SpaRingbuffer);
size += OUTPUT_BUFFER_SIZE;
return size;
}
static void
transport_setup_area (void *p, PinosTransport *trans)
{
PinosTransportArea *a;
trans->area = a = p;
p = SPA_MEMBER (p, sizeof (PinosTransportArea), SpaPortInputInfo);
trans->input_info = p;
p = SPA_MEMBER (p, a->max_input_info * sizeof (SpaPortInputInfo), void);
trans->output_info = p;
p = SPA_MEMBER (p, a->max_output_info * sizeof (SpaPortOutputInfo), void);
trans->input_buffer = p;
spa_ringbuffer_init (trans->input_buffer, INPUT_BUFFER_SIZE);
p = SPA_MEMBER (p, sizeof (SpaRingbuffer), void);
trans->input_data = p;
p = SPA_MEMBER (p, INPUT_BUFFER_SIZE, void);
trans->output_buffer = p;
spa_ringbuffer_init (trans->output_buffer, OUTPUT_BUFFER_SIZE);
p = SPA_MEMBER (p, sizeof (SpaRingbuffer), void);
trans->output_data = p;
p = SPA_MEMBER (p, OUTPUT_BUFFER_SIZE, void);
}
PinosTransport *
pinos_transport_new (unsigned int max_input_info,
unsigned int max_output_info,
int fd)
{
PinosTransportImpl *impl;
PinosTransport *trans;
PinosTransportArea area;
area.max_input_info = max_input_info;
area.n_input_info = 0;
area.max_output_info = max_output_info;
area.n_output_info = 0;
impl = calloc (1, sizeof (PinosTransportImpl));
impl->offset = 0;
trans = &impl->trans;
trans->fd = fd;
pinos_memblock_alloc (PINOS_MEMBLOCK_FLAG_WITH_FD |
PINOS_MEMBLOCK_FLAG_MAP_READWRITE |
PINOS_MEMBLOCK_FLAG_SEAL,
transport_area_get_size (&area),
&impl->mem);
memcpy (impl->mem.ptr, &area, sizeof (PinosTransportArea));
transport_setup_area (impl->mem.ptr, trans);
return trans;
}
PinosTransport *
pinos_transport_new_from_info (PinosTransportInfo *info)
{
PinosTransportImpl *impl;
PinosTransport *trans;
void *tmp;
if (info == NULL)
return NULL;
impl = calloc (1, sizeof (PinosTransportImpl));
trans = &impl->trans;
trans->fd = info->fd;
impl->mem.flags = PINOS_MEMBLOCK_FLAG_MAP_READWRITE |
PINOS_MEMBLOCK_FLAG_WITH_FD;
impl->mem.fd = info->memfd;
impl->mem.size = info->size;
impl->mem.ptr = mmap (NULL, info->size, PROT_READ | PROT_WRITE, MAP_SHARED, info->memfd, info->offset);
impl->offset = info->offset;
transport_setup_area (impl->mem.ptr, trans);
tmp = trans->output_buffer;
trans->output_buffer = trans->input_buffer;
trans->input_buffer = tmp;
tmp = trans->output_data;
trans->output_data = trans->input_data;
trans->input_data = tmp;
return trans;
}
void
pinos_transport_free (PinosTransport *trans)
{
PinosTransportImpl *impl = (PinosTransportImpl *) trans;
if (impl == NULL)
return;
pinos_memblock_free (&impl->mem);
if (trans->fd != -1)
close (trans->fd);
free (impl);
}
SpaResult
pinos_transport_get_info (PinosTransport *trans,
PinosTransportInfo *info)
{
PinosTransportImpl *impl = (PinosTransportImpl *) trans;
if (impl == NULL)
return SPA_RESULT_INVALID_ARGUMENTS;
info->memfd = impl->mem.fd;
info->offset = impl->offset;
info->size = impl->mem.size;
info->fd = trans->fd;
return SPA_RESULT_OK;
}
SpaResult
pinos_transport_add_event (PinosTransport *trans,
SpaNodeEvent *event)
{
PinosTransportImpl *impl = (PinosTransportImpl *) trans;
SpaRingbufferArea areas[2];
size_t avail;
if (impl == NULL || event == NULL)
return SPA_RESULT_INVALID_ARGUMENTS;
avail = spa_ringbuffer_get_write_areas (trans->output_buffer, areas);
if (avail < event->size)
return SPA_RESULT_ERROR;
spa_ringbuffer_write_data (trans->output_buffer,
trans->output_data,
areas,
event,
event->size);
spa_ringbuffer_write_advance (trans->output_buffer, event->size);
return SPA_RESULT_OK;
}
SpaResult
pinos_transport_next_event (PinosTransport *trans,
SpaNodeEvent *event)
{
PinosTransportImpl *impl = (PinosTransportImpl *) trans;
size_t avail;
if (impl == NULL || event == NULL)
return SPA_RESULT_INVALID_ARGUMENTS;
avail = spa_ringbuffer_get_read_areas (trans->input_buffer, impl->areas);
if (avail < sizeof (SpaNodeEvent))
return SPA_RESULT_ENUM_END;
spa_ringbuffer_read_data (trans->input_buffer,
trans->input_data,
impl->areas,
&impl->current,
sizeof (SpaNodeEvent));
*event = impl->current;
return SPA_RESULT_OK;
}
SpaResult
pinos_transport_parse_event (PinosTransport *trans,
void *event)
{
PinosTransportImpl *impl = (PinosTransportImpl *) trans;
if (impl == NULL || event == NULL)
return SPA_RESULT_INVALID_ARGUMENTS;
spa_ringbuffer_read_data (trans->input_buffer,
trans->input_data,
impl->areas,
event,
impl->current.size);
spa_ringbuffer_read_advance (trans->input_buffer, impl->current.size);
return SPA_RESULT_OK;
}

115
pinos/client/transport.h Normal file
View file

@ -0,0 +1,115 @@
/* Simple Plugin API
* Copyright (C) 2016 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_TRANSPORT_H__
#define __PINOS_TRANSPORT_H__
#ifdef __cplusplus
extern "C" {
#endif
typedef struct _PinosTransport PinosTransport;
typedef struct _PinosTransportArea PinosTransportArea;
#include <string.h>
#include <spa/defs.h>
#include <spa/port.h>
#include <spa/node.h>
#include <pinos/client/connection.h>
#include <pinos/client/mem.h>
#define PINOS_TRANSPORT_CMD_NONE 0
#define PINOS_TRANSPORT_CMD_NEED_DATA (1<<0)
#define PINOS_TRANSPORT_CMD_HAVE_DATA (1<<1)
#define PINOS_TRANSPORT_CMD_HAVE_EVENT (1<<2)
#define PINOS_TRANSPORT_CMD_SYNC (1<<3)
typedef struct {
int memfd;
off_t offset;
size_t size;
int fd;
} PinosTransportInfo;
struct _PinosTransportArea {
unsigned int max_input_info;
unsigned int n_input_info;
unsigned int max_output_info;
unsigned int n_output_info;
};
struct _PinosTransport {
PinosTransportArea *area;
SpaPortInputInfo *input_info;
SpaPortOutputInfo *output_info;
void *input_data;
SpaRingbuffer *input_buffer;
void *output_data;
SpaRingbuffer *output_buffer;
int fd;
};
PinosTransport * pinos_transport_new (unsigned int max_input_info,
unsigned int max_output_info,
int fd);
PinosTransport * pinos_transport_new_from_info (PinosTransportInfo *info);
void pinos_transport_free (PinosTransport *trans);
SpaResult pinos_transport_get_info (PinosTransport *trans,
PinosTransportInfo *info);
SpaResult pinos_transport_add_event (PinosTransport *trans,
SpaNodeEvent *event);
SpaResult pinos_transport_next_event (PinosTransport *trans,
SpaNodeEvent *event);
SpaResult pinos_transport_parse_event (PinosTransport *trans,
void *event);
static inline void
pinos_transport_write_cmd (PinosTransport *trans,
uint8_t cmd)
{
write (trans->fd, &cmd, 1);
}
static inline void
pinos_transport_read_cmd (PinosTransport *trans,
uint8_t *cmd)
{
read (trans->fd, cmd, 1);
}
static inline void
pinos_transport_sync_cmd (PinosTransport *trans,
uint8_t out_cmd,
uint8_t *in_cmd)
{
write (trans->fd, &out_cmd, 1);
read (trans->fd, in_cmd, 1);
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* __PINOS_TRANSPORT_H__ */