mirror of
https://gitlab.freedesktop.org/pipewire/pipewire.git
synced 2025-10-29 05:40:27 -04:00
Make buffer structure sharable
Use stucture offsets instead of pointers so that we can share the buffer structure between client and server. We can then pass an fd wrapping the memory of the buffer to the client. Add beginnings of a memory pool
This commit is contained in:
parent
3ace7e9648
commit
98993c680b
23 changed files with 456 additions and 583 deletions
|
|
@ -164,7 +164,6 @@ pinos_monitor_LDFLAGS = $(AM_LDFLAGS) $(BINLDFLAGS)
|
|||
pinosgstsource = gst/gsttmpfileallocator.h gst/gsttmpfileallocator.c
|
||||
|
||||
pinosinclude_HEADERS = \
|
||||
client/client-port.h \
|
||||
client/context.h \
|
||||
client/enumtypes.h \
|
||||
client/introspect.h \
|
||||
|
|
|
|||
|
|
@ -1,373 +0,0 @@
|
|||
/* 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 <gio/gunixfdlist.h>
|
||||
|
||||
#include "pinos/client/pinos.h"
|
||||
#include "pinos/client/enumtypes.h"
|
||||
|
||||
#include "pinos/client/client-port.h"
|
||||
|
||||
#define PINOS_CLIENT_PORT_GET_PRIVATE(obj) \
|
||||
(G_TYPE_INSTANCE_GET_PRIVATE ((obj), PINOS_TYPE_CLIENT_PORT, PinosClientPortPrivate))
|
||||
|
||||
struct _PinosClientPortPrivate
|
||||
{
|
||||
GDBusProxy *proxy;
|
||||
};
|
||||
|
||||
G_DEFINE_TYPE (PinosClientPort, pinos_client_port, PINOS_TYPE_PORT);
|
||||
|
||||
enum
|
||||
{
|
||||
PROP_0,
|
||||
PROP_PROXY,
|
||||
};
|
||||
|
||||
static void
|
||||
on_ringbuffer (GObject *source_object,
|
||||
GAsyncResult *res,
|
||||
gpointer user_data)
|
||||
{
|
||||
GTask *task = user_data;
|
||||
PinosClientPort *port = g_task_get_source_object (task);
|
||||
PinosClientPortPrivate *priv = port->priv;
|
||||
GVariant *ret;
|
||||
GError *error = NULL;
|
||||
GUnixFDList *fdlist;
|
||||
gint fd, semfd, fd_idx, sem_idx;
|
||||
guint fdsize;
|
||||
PinosRingbuffer *rbuf;
|
||||
|
||||
g_assert (priv->proxy == G_DBUS_PROXY (source_object));
|
||||
|
||||
ret = g_dbus_proxy_call_with_unix_fd_list_finish (priv->proxy, &fdlist, res, &error);
|
||||
if (ret == NULL)
|
||||
goto create_failed;
|
||||
|
||||
g_variant_get (ret, "(huh)", &fd_idx, &fdsize, &sem_idx);
|
||||
g_variant_unref (ret);
|
||||
|
||||
fd = g_unix_fd_list_get (fdlist, fd_idx, &error);
|
||||
semfd = g_unix_fd_list_get (fdlist, sem_idx, &error);
|
||||
g_object_unref (fdlist);
|
||||
|
||||
if (fd == -1 || semfd == -1)
|
||||
goto create_failed;
|
||||
|
||||
rbuf = pinos_ringbuffer_new_import (PINOS_RINGBUFFER_MODE_WRITE,
|
||||
fd, fdsize, semfd);
|
||||
|
||||
g_task_return_pointer (task, rbuf, (GDestroyNotify) g_object_unref);
|
||||
g_object_unref (task);
|
||||
|
||||
return;
|
||||
|
||||
/* ERRORS */
|
||||
create_failed:
|
||||
{
|
||||
g_warning ("failed to get ringbuffer: %s", error->message);
|
||||
g_task_return_error (task, error);
|
||||
g_object_unref (task);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
pinos_client_port_get_ringbuffer (PinosPort *port,
|
||||
PinosProperties *props,
|
||||
GTask *task)
|
||||
{
|
||||
PinosClientPortPrivate *priv = PINOS_CLIENT_PORT (port)->priv;
|
||||
|
||||
g_dbus_proxy_call (priv->proxy,
|
||||
"GetRingbuffer",
|
||||
g_variant_new ("(@a{sv})",
|
||||
pinos_properties_to_variant (props)),
|
||||
G_DBUS_CALL_FLAGS_NONE,
|
||||
-1,
|
||||
NULL, /* GCancellable *cancellable */
|
||||
on_ringbuffer,
|
||||
task);
|
||||
}
|
||||
|
||||
static void
|
||||
pinos_client_port_get_property (GObject *_object,
|
||||
guint prop_id,
|
||||
GValue *value,
|
||||
GParamSpec *pspec)
|
||||
{
|
||||
PinosClientPort *port = PINOS_CLIENT_PORT (_object);
|
||||
PinosClientPortPrivate *priv = port->priv;
|
||||
|
||||
switch (prop_id) {
|
||||
case PROP_PROXY:
|
||||
g_value_set_object (value, priv->proxy);
|
||||
break;
|
||||
|
||||
default:
|
||||
G_OBJECT_WARN_INVALID_PROPERTY_ID (port, prop_id, pspec);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
pinos_client_port_set_property (GObject *_object,
|
||||
guint prop_id,
|
||||
const GValue *value,
|
||||
GParamSpec *pspec)
|
||||
{
|
||||
PinosClientPort *port = PINOS_CLIENT_PORT (_object);
|
||||
PinosClientPortPrivate *priv = port->priv;
|
||||
|
||||
switch (prop_id) {
|
||||
case PROP_PROXY:
|
||||
priv->proxy = g_value_dup_object (value);
|
||||
break;
|
||||
|
||||
default:
|
||||
G_OBJECT_WARN_INVALID_PROPERTY_ID (port, prop_id, pspec);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
proxy_g_properties_changed (GDBusProxy *_proxy,
|
||||
GVariant *changed_properties,
|
||||
GStrv invalidated_properties,
|
||||
gpointer user_data)
|
||||
{
|
||||
PinosClientPort *port = user_data;
|
||||
GVariantIter *iter;
|
||||
gchar *key;
|
||||
|
||||
g_variant_get (changed_properties, "a{sv}", &iter);
|
||||
while (g_variant_iter_next (iter, "{&sv}", &key, NULL)) {
|
||||
GVariant *variant;
|
||||
gsize size;
|
||||
gpointer data;
|
||||
GBytes *bytes;
|
||||
|
||||
variant = g_dbus_proxy_get_cached_property (_proxy, key);
|
||||
if (variant == NULL)
|
||||
continue;
|
||||
|
||||
if (strcmp (key, "PossibleFormats") == 0) {
|
||||
data = g_variant_dup_string (variant, &size);
|
||||
bytes = g_bytes_new_take (data, size);
|
||||
g_object_set (port, "possible-formats", bytes, NULL);
|
||||
g_bytes_unref (bytes);
|
||||
}
|
||||
g_variant_unref (variant);
|
||||
}
|
||||
g_variant_iter_free (iter);
|
||||
}
|
||||
|
||||
static void
|
||||
proxy_set_property_cb (GDBusProxy *proxy,
|
||||
GAsyncResult *res,
|
||||
gpointer user_data)
|
||||
{
|
||||
GVariant *ret;
|
||||
GError *error = NULL;
|
||||
|
||||
ret = g_dbus_proxy_call_finish (proxy, res, &error);
|
||||
if (ret == NULL) {
|
||||
g_warning ("Error setting property: %s", error->message);
|
||||
g_error_free (error);
|
||||
} else
|
||||
g_variant_unref (ret);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
on_property_notify (GObject *obj,
|
||||
GParamSpec *pspec,
|
||||
gpointer user_data)
|
||||
{
|
||||
PinosPort *port = PINOS_PORT (obj);
|
||||
PinosClientPortPrivate *priv = PINOS_CLIENT_PORT (port)->priv;
|
||||
const gchar *prop_name = NULL;
|
||||
GVariant *variant;
|
||||
|
||||
if (pspec == NULL || strcmp (g_param_spec_get_name (pspec), "possible-formats") == 0) {
|
||||
GBytes *bytes = pinos_port_get_possible_formats (port);
|
||||
prop_name = "PossibleFormats";
|
||||
variant = bytes ? g_variant_new_string (g_bytes_get_data (bytes, NULL)) : NULL;
|
||||
}
|
||||
if (prop_name) {
|
||||
g_dbus_proxy_call (G_DBUS_PROXY (priv->proxy),
|
||||
"org.freedesktop.DBus.Properties.Set",
|
||||
g_variant_new ("(ssv)",
|
||||
"org.pinos.Port1",
|
||||
prop_name,
|
||||
variant),
|
||||
G_DBUS_CALL_FLAGS_NONE,
|
||||
-1,
|
||||
NULL,
|
||||
(GAsyncReadyCallback) proxy_set_property_cb,
|
||||
port);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
pinos_client_port_constructed (GObject * object)
|
||||
{
|
||||
PinosClientPort *port = PINOS_CLIENT_PORT (object);
|
||||
PinosClientPortPrivate *priv = port->priv;
|
||||
|
||||
g_debug ("client-port %p: constructed", port);
|
||||
g_signal_connect (priv->proxy,
|
||||
"g-properties-changed",
|
||||
(GCallback) proxy_g_properties_changed,
|
||||
port);
|
||||
g_signal_connect (port, "notify", (GCallback) on_property_notify, port);
|
||||
|
||||
G_OBJECT_CLASS (pinos_client_port_parent_class)->constructed (object);
|
||||
}
|
||||
|
||||
static void
|
||||
pinos_client_port_dispose (GObject * object)
|
||||
{
|
||||
PinosClientPort *port = PINOS_CLIENT_PORT (object);
|
||||
|
||||
g_debug ("client-port %p: dispose", port);
|
||||
|
||||
G_OBJECT_CLASS (pinos_client_port_parent_class)->dispose (object);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
pinos_client_port_finalize (GObject * object)
|
||||
{
|
||||
PinosClientPort *port = PINOS_CLIENT_PORT (object);
|
||||
PinosClientPortPrivate *priv = port->priv;
|
||||
|
||||
g_debug ("client-port %p: finalize", port);
|
||||
g_clear_object (&priv->proxy);
|
||||
|
||||
G_OBJECT_CLASS (pinos_client_port_parent_class)->finalize (object);
|
||||
}
|
||||
|
||||
static void
|
||||
pinos_client_port_class_init (PinosClientPortClass * klass)
|
||||
{
|
||||
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
||||
PinosPortClass *port_class = PINOS_PORT_CLASS (klass);
|
||||
|
||||
g_type_class_add_private (klass, sizeof (PinosClientPortPrivate));
|
||||
|
||||
gobject_class->constructed = pinos_client_port_constructed;
|
||||
gobject_class->dispose = pinos_client_port_dispose;
|
||||
gobject_class->finalize = pinos_client_port_finalize;
|
||||
gobject_class->set_property = pinos_client_port_set_property;
|
||||
gobject_class->get_property = pinos_client_port_get_property;
|
||||
|
||||
g_object_class_install_property (gobject_class,
|
||||
PROP_PROXY,
|
||||
g_param_spec_object ("proxy",
|
||||
"Proxy",
|
||||
"The Proxy",
|
||||
G_TYPE_DBUS_PROXY,
|
||||
G_PARAM_READWRITE |
|
||||
G_PARAM_CONSTRUCT_ONLY |
|
||||
G_PARAM_STATIC_STRINGS));
|
||||
|
||||
port_class->get_ringbuffer = pinos_client_port_get_ringbuffer;
|
||||
}
|
||||
|
||||
static void
|
||||
pinos_client_port_init (PinosClientPort * port)
|
||||
{
|
||||
port->priv = PINOS_CLIENT_PORT_GET_PRIVATE (port);
|
||||
}
|
||||
|
||||
/**
|
||||
* pinos_client_port_new:
|
||||
* @node: a #PinosClientNode
|
||||
* @id: an id
|
||||
* @socket: a socket with the server port
|
||||
*
|
||||
* Create a new client port.
|
||||
*
|
||||
* Returns: a new client port
|
||||
*/
|
||||
PinosClientPort *
|
||||
pinos_client_port_new (PinosClientNode *node,
|
||||
gpointer id,
|
||||
GSocket *socket)
|
||||
{
|
||||
PinosClientPort *port;
|
||||
GDBusProxy *proxy = id;
|
||||
GVariant *variant;
|
||||
PinosDirection direction = PINOS_DIRECTION_INVALID;
|
||||
const gchar *name = "unknown";
|
||||
GBytes *possible_formats = NULL;
|
||||
GBytes *format = NULL;
|
||||
PinosProperties *properties = NULL;
|
||||
|
||||
variant = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (proxy), "Direction");
|
||||
if (variant != NULL) {
|
||||
direction = g_variant_get_uint32 (variant);
|
||||
g_variant_unref (variant);
|
||||
}
|
||||
variant = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (proxy), "Name");
|
||||
if (variant != NULL) {
|
||||
name = g_variant_get_string (variant, NULL);
|
||||
g_variant_unref (variant);
|
||||
}
|
||||
variant = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (proxy), "PossibleFormats");
|
||||
if (variant != NULL) {
|
||||
gsize size;
|
||||
gpointer data;
|
||||
data = g_variant_dup_string (variant, &size);
|
||||
possible_formats = g_bytes_new_take (data, size);
|
||||
g_variant_unref (variant);
|
||||
}
|
||||
variant = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (proxy), "Format");
|
||||
if (variant != NULL) {
|
||||
gsize size;
|
||||
gpointer data;
|
||||
data = g_variant_dup_string (variant, &size);
|
||||
format = g_bytes_new_take (data, size);
|
||||
g_variant_unref (variant);
|
||||
}
|
||||
variant = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (proxy), "Properties");
|
||||
if (variant != NULL) {
|
||||
properties = pinos_properties_from_variant (variant);
|
||||
g_variant_unref (variant);
|
||||
}
|
||||
|
||||
port = g_object_new (PINOS_TYPE_CLIENT_PORT,
|
||||
"node", node,
|
||||
"direction", direction,
|
||||
"name", name,
|
||||
"possible-formats", possible_formats,
|
||||
"format", format,
|
||||
"properties", properties,
|
||||
"proxy", proxy,
|
||||
"socket", socket,
|
||||
NULL);
|
||||
return port;
|
||||
}
|
||||
|
|
@ -1,80 +0,0 @@
|
|||
/* 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_PORT_H__
|
||||
#define __PINOS_CLIENT_PORT_H__
|
||||
|
||||
#include <glib-object.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
typedef struct _PinosClientPort PinosClientPort;
|
||||
typedef struct _PinosClientPortClass PinosClientPortClass;
|
||||
typedef struct _PinosClientPortPrivate PinosClientPortPrivate;
|
||||
|
||||
#include <pinos/client/introspect.h>
|
||||
#include <pinos/client/port.h>
|
||||
#include <pinos/client/client-node.h>
|
||||
|
||||
#define PINOS_TYPE_CLIENT_PORT (pinos_client_port_get_type ())
|
||||
#define PINOS_IS_CLIENT_PORT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PINOS_TYPE_CLIENT_PORT))
|
||||
#define PINOS_IS_CLIENT_PORT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PINOS_TYPE_CLIENT_PORT))
|
||||
#define PINOS_CLIENT_PORT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), PINOS_TYPE_CLIENT_PORT, PinosClientPortClass))
|
||||
#define PINOS_CLIENT_PORT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), PINOS_TYPE_CLIENT_PORT, PinosClientPort))
|
||||
#define PINOS_CLIENT_PORT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PINOS_TYPE_CLIENT_PORT, PinosClientPortClass))
|
||||
#define PINOS_CLIENT_PORT_CAST(obj) ((PinosClientPort*)(obj))
|
||||
#define PINOS_CLIENT_PORT_CLASS_CAST(klass) ((PinosClientPortClass*)(klass))
|
||||
|
||||
/**
|
||||
* PinosClientPort:
|
||||
*
|
||||
* Pinos client port object class.
|
||||
*/
|
||||
struct _PinosClientPort {
|
||||
PinosPort object;
|
||||
|
||||
PinosClientPortPrivate *priv;
|
||||
};
|
||||
|
||||
/**
|
||||
* PinosClientPortClass:
|
||||
* @get_formats: called to get a list of supported formats from the port
|
||||
* @create_channel: called to create a new channel object
|
||||
* @release_channel: called to release a channel object
|
||||
*
|
||||
* Pinos client port object class.
|
||||
*/
|
||||
struct _PinosClientPortClass {
|
||||
PinosPortClass parent_class;
|
||||
};
|
||||
|
||||
/* normal GObject stuff */
|
||||
GType pinos_client_port_get_type (void);
|
||||
|
||||
PinosClientPort * pinos_client_port_new (PinosClientNode *node,
|
||||
gpointer id,
|
||||
GSocket *socket);
|
||||
|
||||
GBytes * pinos_client_port_get_formats (PinosClientPort *port,
|
||||
GBytes *filter,
|
||||
GError **error);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /* __PINOS_CLIENT_PORT_H__ */
|
||||
|
|
@ -19,6 +19,7 @@
|
|||
|
||||
#include <sys/socket.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
#include <gio/gio.h>
|
||||
#include <gio/gunixfdlist.h>
|
||||
|
|
@ -52,14 +53,19 @@ clear_mem_id (MemId *id)
|
|||
typedef struct {
|
||||
bool cleanup;
|
||||
uint32_t id;
|
||||
int fd;
|
||||
off_t offset;
|
||||
size_t size;
|
||||
SpaBuffer *buf;
|
||||
} BufferId;
|
||||
|
||||
static void
|
||||
clear_buffer_id (BufferId *id)
|
||||
{
|
||||
free (id->buf);
|
||||
munmap (id->buf, id->size);
|
||||
id->buf = NULL;
|
||||
close (id->fd);
|
||||
id->fd = -1;
|
||||
}
|
||||
|
||||
struct _PinosStreamPrivate
|
||||
|
|
@ -696,10 +702,18 @@ parse_control (PinosStream *stream,
|
|||
if (spa_control_iter_parse_cmd (&it, &p) < 0)
|
||||
break;
|
||||
|
||||
g_debug ("add buffer %d", p.buffer->id);
|
||||
bid.fd = spa_control_get_fd (ctrl, p.fd_index, false);
|
||||
if (bid.fd == -1)
|
||||
break;
|
||||
|
||||
g_debug ("add buffer %d", p.buffer_id);
|
||||
bid.cleanup = false;
|
||||
bid.id = p.buffer->id;
|
||||
bid.buf = p.buffer;
|
||||
bid.id = p.buffer_id;
|
||||
bid.offset = p.offset;
|
||||
bid.size = p.size;
|
||||
bid.buf = mmap (NULL, p.size, PROT_READ | PROT_WRITE, MAP_SHARED, bid.fd, p.offset);
|
||||
spa_debug_buffer (bid.buf);
|
||||
|
||||
g_array_append_val (priv->buffer_ids, bid);
|
||||
break;
|
||||
}
|
||||
|
|
@ -725,20 +739,18 @@ parse_control (PinosStream *stream,
|
|||
if (spa_control_iter_parse_cmd (&it, &p) < 0)
|
||||
break;
|
||||
|
||||
g_debug ("process buffer %d", p.buffer_id);
|
||||
if ((bid = find_buffer (stream, p.buffer_id))) {
|
||||
SpaBuffer *b = bid->buf;
|
||||
SpaData *d = SPA_BUFFER_DATAS (b);
|
||||
|
||||
for (i = 0; i < b->n_datas; i++) {
|
||||
SpaData *d = &b->datas[i];
|
||||
|
||||
if (d->type == SPA_DATA_TYPE_MEMID) {
|
||||
int id = *((int*)(d->ptr));
|
||||
if (d[i].type == SPA_DATA_TYPE_MEMID) {
|
||||
uint32_t id = SPA_PTR_TO_UINT32 (d[i].ptr);
|
||||
MemId *mid;
|
||||
|
||||
if ((mid = find_mem (stream, id))) {
|
||||
d->type = SPA_DATA_TYPE_FD;
|
||||
*((int *)(d->ptr)) = mid->fd;
|
||||
d[i].type = SPA_DATA_TYPE_FD;
|
||||
d[i].ptr = SPA_INT_TO_PTR (mid->fd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -496,15 +496,6 @@ typedef struct {
|
|||
int fd;
|
||||
} SinkBuffer;
|
||||
|
||||
static void
|
||||
sink_buffer_free (void *user_data)
|
||||
{
|
||||
SinkBuffer *b = user_data;
|
||||
gst_memory_unref (b->mem);
|
||||
gst_object_unref (b->pinossink);
|
||||
g_slice_free (SinkBuffer, user_data);
|
||||
}
|
||||
|
||||
static GstFlowReturn
|
||||
gst_pinos_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
|
||||
{
|
||||
|
|
@ -526,11 +517,11 @@ gst_pinos_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
|
|||
|
||||
b = g_slice_new (SinkBuffer);
|
||||
b->buffer.id = pinos_fd_manager_get_id (pinossink->fdmanager);
|
||||
b->buffer.size = size;
|
||||
b->buffer.size = sizeof (SinkBuffer);
|
||||
b->buffer.n_metas = 1;
|
||||
b->buffer.metas = b->metas;
|
||||
b->buffer.metas = offsetof (SinkBuffer, metas);
|
||||
b->buffer.n_datas = 1;
|
||||
b->buffer.datas = b->datas;
|
||||
b->buffer.datas = offsetof (SinkBuffer, datas);
|
||||
|
||||
pts = GST_BUFFER_PTS (buffer);
|
||||
dts = GST_BUFFER_DTS (buffer);
|
||||
|
|
@ -544,7 +535,7 @@ gst_pinos_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
|
|||
b->header.pts = GST_CLOCK_TIME_IS_VALID (pts) ? pts + base : base;
|
||||
b->header.dts_offset = GST_CLOCK_TIME_IS_VALID (dts) && GST_CLOCK_TIME_IS_VALID (pts) ? pts - dts : 0;
|
||||
b->metas[0].type = SPA_META_TYPE_HEADER;
|
||||
b->metas[0].data = &b->header;
|
||||
b->metas[0].offset = offsetof (SinkBuffer, header);
|
||||
b->metas[0].size = sizeof (b->header);
|
||||
|
||||
if (gst_buffer_n_memory (buffer) == 1
|
||||
|
|
|
|||
|
|
@ -362,12 +362,12 @@ on_new_buffer (GObject *gobject,
|
|||
process_mem_data_destroy);
|
||||
|
||||
for (i = 0; i < b->n_metas; i++) {
|
||||
SpaMeta *m = &b->metas[i];
|
||||
SpaMeta *m = &SPA_BUFFER_METAS(b)[i];
|
||||
|
||||
switch (m->type) {
|
||||
case SPA_META_TYPE_HEADER:
|
||||
{
|
||||
SpaMetaHeader *h = m->data;
|
||||
SpaMetaHeader *h = SPA_MEMBER (b, m->offset, SpaMetaHeader);
|
||||
|
||||
GST_INFO ("pts %" G_GUINT64_FORMAT ", dts_offset %"G_GUINT64_FORMAT, h->pts, h->dts_offset);
|
||||
|
||||
|
|
@ -384,7 +384,7 @@ on_new_buffer (GObject *gobject,
|
|||
}
|
||||
}
|
||||
for (i = 0; i < b->n_datas; i++) {
|
||||
SpaData *d = &b->datas[i];
|
||||
SpaData *d = &SPA_BUFFER_DATAS (b)[i];
|
||||
|
||||
switch (d->type) {
|
||||
case SPA_DATA_TYPE_MEMPTR:
|
||||
|
|
@ -397,7 +397,7 @@ on_new_buffer (GObject *gobject,
|
|||
case SPA_DATA_TYPE_FD:
|
||||
{
|
||||
GstMemory *fdmem = NULL;
|
||||
int fd = *((int *) d->ptr);
|
||||
int fd = SPA_PTR_TO_INT (d->ptr);
|
||||
|
||||
fdmem = gst_fd_allocator_alloc (pinossrc->fd_allocator, dup (fd),
|
||||
d->offset + d->size, GST_FD_MEMORY_FLAG_NONE);
|
||||
|
|
|
|||
|
|
@ -437,16 +437,16 @@ on_received_buffer (PinosPort *port,
|
|||
SpaBuffer *buffer = NULL;
|
||||
|
||||
for (i = 0; i < buffer->n_datas; i++) {
|
||||
SpaData *d = &buffer->datas[i];
|
||||
SpaData *d = SPA_BUFFER_DATAS (buffer);
|
||||
PinosRingbufferArea areas[2];
|
||||
uint8_t *data;
|
||||
size_t size, towrite, total;
|
||||
|
||||
if (d->type != SPA_DATA_TYPE_MEMPTR)
|
||||
if (d[i].type != SPA_DATA_TYPE_MEMPTR)
|
||||
continue;
|
||||
|
||||
size = d->size;
|
||||
data = (guint8*)d->ptr + d->offset;
|
||||
size = d[i].size;
|
||||
data = (guint8*)d[i].ptr + d[i].offset;
|
||||
|
||||
pinos_ringbuffer_get_write_areas (priv->ringbuffer, areas);
|
||||
|
||||
|
|
|
|||
|
|
@ -218,7 +218,7 @@ setup_node (PinosSpaV4l2Source *this)
|
|||
g_debug ("got get_props error %d", res);
|
||||
|
||||
value.type = SPA_PROP_TYPE_STRING;
|
||||
value.value = "/dev/video1";
|
||||
value.value = "/dev/video0";
|
||||
value.size = strlen (value.value)+1;
|
||||
spa_props_set_prop (props, spa_props_index_for_name (props, "device"), &value);
|
||||
|
||||
|
|
|
|||
|
|
@ -89,12 +89,12 @@ typedef struct {
|
|||
/**
|
||||
* SpaMeta:
|
||||
* @type: metadata type
|
||||
* @data: pointer to metadata
|
||||
* @offset: offset of metadata
|
||||
* @size: size of metadata
|
||||
*/
|
||||
typedef struct {
|
||||
SpaMetaType type;
|
||||
void *data;
|
||||
off_t offset;
|
||||
size_t size;
|
||||
} SpaMeta;
|
||||
|
||||
|
|
@ -121,7 +121,7 @@ typedef enum {
|
|||
* SpaData:
|
||||
* @id: user id
|
||||
* @type: the type of data
|
||||
* @ptr_type: more info abouut the type of @ptr
|
||||
* @ptr_type: more info about the type of @ptr
|
||||
* @ptr: pointer to data or fd
|
||||
* @offset: offset of data
|
||||
* @size: size of data
|
||||
|
|
@ -139,21 +139,25 @@ typedef struct {
|
|||
/**
|
||||
* SpaBuffer:
|
||||
* @id: buffer id
|
||||
* @size: total size of the buffer data
|
||||
* @size: total size of the buffer structure
|
||||
* @n_metas: number of metadata
|
||||
* @metas: array of @n_metas metadata
|
||||
* @metas: offset of array of @n_metas metadata
|
||||
* @n_datas: number of data pointers
|
||||
* @datas: array of @n_datas data pointers
|
||||
* @datas: offset of array of @n_datas data pointers
|
||||
*/
|
||||
struct _SpaBuffer {
|
||||
uint32_t id;
|
||||
size_t size;
|
||||
unsigned int n_metas;
|
||||
SpaMeta *metas;
|
||||
off_t metas;
|
||||
unsigned int n_datas;
|
||||
SpaData *datas;
|
||||
off_t datas;
|
||||
};
|
||||
|
||||
#define SPA_BUFFER_METAS(b) (SPA_MEMBER ((b), (b)->metas, SpaMeta))
|
||||
#define SPA_BUFFER_DATAS(b) (SPA_MEMBER ((b), (b)->datas, SpaData))
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -178,7 +178,10 @@ typedef struct {
|
|||
/* SPA_CONTROL_CMD_ADD_BUFFER */
|
||||
typedef struct {
|
||||
uint32_t port_id;
|
||||
SpaBuffer *buffer;
|
||||
uint32_t buffer_id;
|
||||
int fd_index;
|
||||
uint64_t offset;
|
||||
uint64_t size;
|
||||
} SpaControlCmdAddBuffer;
|
||||
|
||||
/* SPA_CONTROL_CMD_REMOVE_BUFFER */
|
||||
|
|
|
|||
|
|
@ -26,8 +26,10 @@ extern "C" {
|
|||
|
||||
#include <spa/defs.h>
|
||||
#include <spa/port.h>
|
||||
#include <spa/buffer.h>
|
||||
|
||||
SpaResult spa_debug_port_info (const SpaPortInfo *info);
|
||||
SpaResult spa_debug_buffer (const SpaBuffer *buffer);
|
||||
SpaResult spa_debug_dump_mem (void *data, size_t size);
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@ extern "C" {
|
|||
#endif
|
||||
#include <inttypes.h>
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
|
||||
typedef enum {
|
||||
SPA_RESULT_OK = 0,
|
||||
|
|
@ -71,6 +72,14 @@ typedef void (*SpaNotify) (void *data);
|
|||
#define SPA_MIN(a,b) ((a)<(b) ? (a) : (b))
|
||||
#define SPA_MAX(a,b) ((a)>(b) ? (a) : (b))
|
||||
|
||||
#define SPA_MEMBER(b,o,t) ((t*)((uint8_t*)(b) + (o)))
|
||||
|
||||
#define SPA_PTR_TO_INT(p) ((int) ((intptr_t) (p)))
|
||||
#define SPA_INT_TO_PTR(u) ((void*) ((intptr_t) (u)))
|
||||
|
||||
#define SPA_PTR_TO_UINT32(p) ((uint32_t) ((uintptr_t) (p)))
|
||||
#define SPA_UINT32_TO_PTR(u) ((void*) ((uintptr_t) (u)))
|
||||
|
||||
#define SPA_ID_INVALID ((uint32_t)0xffffffff)
|
||||
|
||||
|
||||
|
|
|
|||
81
spa/include/spa/memory.h
Normal file
81
spa/include/spa/memory.h
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
/* 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 __SPA_MEMORY_H__
|
||||
#define __SPA_MEMORY_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct _SpaMemory SpaMemory;
|
||||
typedef struct _SpaMemoryPool SpaMemoryPool;
|
||||
|
||||
#include <spa/defs.h>
|
||||
|
||||
/**
|
||||
* SpaMemoryFlags:
|
||||
* @SPA_MEMORY_FLAG_NONE: no flag
|
||||
* @SPA_MEMORY_FLAG_READABLE: memory is writable
|
||||
* @SPA_MEMORY_FLAG_WRITABLE: memory is readable
|
||||
*/
|
||||
typedef enum {
|
||||
SPA_MEMORY_FLAG_NONE = 0,
|
||||
SPA_MEMORY_FLAG_READABLE = (1 << 0),
|
||||
SPA_MEMORY_FLAG_WRITABLE = (1 << 1),
|
||||
} SpaMemoryFlags;
|
||||
|
||||
/**
|
||||
* SpaMemory:
|
||||
* @refcount: a refcount
|
||||
* @notify: notify when refcount is 0
|
||||
* @pool_id: the id of the pool
|
||||
* @id: the memory id
|
||||
* @flags: extra memory flags
|
||||
* @type: memory type
|
||||
* @fd: fd of memory of -1 if not known
|
||||
* @ptr: ptr to memory accessible with CPU
|
||||
* @size: size of memory
|
||||
*/
|
||||
struct _SpaMemory {
|
||||
int refcount;
|
||||
SpaNotify notify;
|
||||
uint32_t pool_id;
|
||||
uint32_t id;
|
||||
SpaMemoryFlags flags;
|
||||
const char *type;
|
||||
int fd;
|
||||
void *ptr;
|
||||
size_t size;
|
||||
};
|
||||
|
||||
uint32_t spa_memory_pool_get (uint32_t type);
|
||||
uint32_t spa_memory_pool_new (void);
|
||||
void spa_memory_pool_free (uint32_t);
|
||||
|
||||
SpaMemory * spa_memory_alloc (uint32_t pool_id);
|
||||
SpaResult spa_memory_free (uint32_t pool_id, uint32_t id);
|
||||
|
||||
SpaMemory * spa_memory_find (uint32_t pool_id, uint32_t id);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif /* __SPA_MEMORY_H__ */
|
||||
|
|
@ -160,7 +160,6 @@ spa_control_clear (SpaControl *control)
|
|||
free (sc->free_data);
|
||||
for (i = 0; i < sc->n_fds; i++) {
|
||||
if (sc->fds[i] > 0) {
|
||||
SPA_DEBUG_CONTROL ("%p: close %d %d", control, i, sc->fds[i]);
|
||||
if (close (sc->fds[i]) < 0)
|
||||
perror ("close");
|
||||
}
|
||||
|
|
@ -340,6 +339,7 @@ spa_control_iter_get_data (SpaControlIter *iter, size_t *size)
|
|||
return si->data;
|
||||
}
|
||||
|
||||
#if 0
|
||||
typedef struct {
|
||||
SpaBuffer buffer;
|
||||
SpaMeta metas[16];
|
||||
|
|
@ -360,16 +360,16 @@ parse_add_buffer (struct stack_iter *si,
|
|||
b->buffer.id = *(uint32_t *)p++;
|
||||
b->buffer.size = *(uint32_t *)p++;
|
||||
b->buffer.n_metas = *(uint32_t *)p++;
|
||||
b->buffer.metas = b->metas;
|
||||
b->buffer.metas = offsetof (MyBuffer, metas);
|
||||
b->buffer.n_datas = *(uint32_t *)p++;
|
||||
b->buffer.datas = b->datas;
|
||||
b->buffer.datas = offsetof (MyBuffer, datas);
|
||||
|
||||
for (i = 0; i < b->buffer.n_metas; i++) {
|
||||
SpaMeta *m = &b->metas[i];
|
||||
m->type = *p++;
|
||||
m->size = *p++;
|
||||
m->data = p;
|
||||
p = p + m->size / 4;
|
||||
m->offset = 0;
|
||||
p = p + (m->size + 3) / 4;
|
||||
}
|
||||
for (i = 0; i < b->buffer.n_datas; i++) {
|
||||
SpaData *d = &b->datas[i];
|
||||
|
|
@ -385,6 +385,7 @@ parse_add_buffer (struct stack_iter *si,
|
|||
|
||||
return SPA_RESULT_OK;
|
||||
}
|
||||
#endif
|
||||
|
||||
SpaResult
|
||||
spa_control_iter_parse_cmd (SpaControlIter *iter,
|
||||
|
|
@ -477,7 +478,9 @@ spa_control_iter_parse_cmd (SpaControlIter *iter,
|
|||
break;
|
||||
|
||||
case SPA_CONTROL_CMD_ADD_BUFFER:
|
||||
res = parse_add_buffer (si, command);
|
||||
if (si->size < sizeof (SpaControlCmdAddBuffer))
|
||||
return SPA_RESULT_ERROR;
|
||||
memcpy (command, si->data, sizeof (SpaControlCmdAddBuffer));
|
||||
break;
|
||||
|
||||
case SPA_CONTROL_CMD_REMOVE_BUFFER:
|
||||
|
|
@ -716,6 +719,7 @@ builder_add_cmd (struct stack_builder *sb, SpaControlCmd cmd, size_t size)
|
|||
return p;
|
||||
}
|
||||
|
||||
#if 0
|
||||
static SpaResult
|
||||
build_add_buffer (struct stack_builder *sb,
|
||||
SpaControlCmdAddBuffer *command)
|
||||
|
|
@ -767,6 +771,7 @@ build_add_buffer (struct stack_builder *sb,
|
|||
}
|
||||
return SPA_RESULT_OK;
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* spa_control_builder_add_cmd:
|
||||
|
|
@ -869,7 +874,8 @@ spa_control_builder_add_cmd (SpaControlBuilder *builder,
|
|||
break;
|
||||
|
||||
case SPA_CONTROL_CMD_ADD_BUFFER:
|
||||
res = build_add_buffer (sb, (SpaControlCmdAddBuffer *) command);
|
||||
p = builder_add_cmd (sb, cmd, sizeof (SpaControlCmdAddBuffer));
|
||||
memcpy (p, command, sizeof (SpaControlCmdAddBuffer));
|
||||
break;
|
||||
|
||||
case SPA_CONTROL_CMD_REMOVE_BUFFER:
|
||||
|
|
@ -980,7 +986,7 @@ spa_control_read (SpaControl *control,
|
|||
}
|
||||
sc->magic = SSC_MAGIC;
|
||||
|
||||
SPA_DEBUG_CONTROL ("read %zd bytes and %d fds\n", len, sc->n_fds);
|
||||
SPA_DEBUG_CONTROL ("control %p: read %zd bytes and %d fds\n", sc, len, sc->n_fds);
|
||||
|
||||
return SPA_RESULT_OK;
|
||||
|
||||
|
|
@ -1009,16 +1015,21 @@ spa_control_write (SpaControl *control,
|
|||
iov[0].iov_len = sc->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);
|
||||
cm = (int*)CMSG_DATA (cmsg);
|
||||
for (i = 0; i < fds_len; i++)
|
||||
cm[i] = sc->fds[i] > 0 ? sc->fds[i] : -sc->fds[i];
|
||||
msg.msg_controllen = cmsg->cmsg_len;
|
||||
if (sc->n_fds > 0) {
|
||||
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);
|
||||
cm = (int*)CMSG_DATA (cmsg);
|
||||
for (i = 0; i < sc->n_fds; i++)
|
||||
cm[i] = sc->fds[i] > 0 ? sc->fds[i] : -sc->fds[i];
|
||||
msg.msg_controllen = cmsg->cmsg_len;
|
||||
} else {
|
||||
msg.msg_control = NULL;
|
||||
msg.msg_controllen = 0;
|
||||
}
|
||||
|
||||
while (true) {
|
||||
len = sendmsg (fd, &msg, 0);
|
||||
|
|
@ -1033,7 +1044,7 @@ spa_control_write (SpaControl *control,
|
|||
if (len != (ssize_t) sc->size)
|
||||
return SPA_RESULT_ERROR;
|
||||
|
||||
SPA_DEBUG_CONTROL ("written %zd bytes\n", len);
|
||||
SPA_DEBUG_CONTROL ("control %p: written %zd bytes and %d fds\n", sc, len, sc->n_fds);
|
||||
|
||||
return SPA_RESULT_OK;
|
||||
|
||||
|
|
|
|||
|
|
@ -79,6 +79,72 @@ spa_debug_port_info (const SpaPortInfo *info)
|
|||
return SPA_RESULT_OK;
|
||||
}
|
||||
|
||||
SpaResult
|
||||
spa_debug_buffer (const SpaBuffer *buffer)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (buffer == NULL)
|
||||
return SPA_RESULT_INVALID_ARGUMENTS;
|
||||
|
||||
fprintf (stderr, "SpaBuffer %p:\n", buffer);
|
||||
fprintf (stderr, " id: \t%08x\n", buffer->id);
|
||||
fprintf (stderr, " size: \t%zd\n", buffer->size);
|
||||
fprintf (stderr, " n_metas: \t%u (offset %zd)\n", buffer->n_metas, buffer->metas);
|
||||
for (i = 0; i < buffer->n_metas; i++) {
|
||||
SpaMeta *m = &SPA_BUFFER_METAS (buffer)[i];
|
||||
fprintf (stderr, " meta %d: type %d, offset %zd, size %zd:\n", i, m->type, m->offset, m->size);
|
||||
switch (m->type) {
|
||||
case SPA_META_TYPE_HEADER:
|
||||
{
|
||||
SpaMetaHeader *h = SPA_MEMBER (buffer, m->offset, SpaMetaHeader);
|
||||
fprintf (stderr, " SpaMetaHeader:\n");
|
||||
fprintf (stderr, " flags: %08x\n", h->flags);
|
||||
fprintf (stderr, " seq: %u\n", h->seq);
|
||||
fprintf (stderr, " pts: %"PRIi64"\n", h->pts);
|
||||
fprintf (stderr, " dts_offset: %"PRIi64"\n", h->dts_offset);
|
||||
break;
|
||||
}
|
||||
case SPA_META_TYPE_POINTER:
|
||||
fprintf (stderr, " SpaMetaPointer:\n");
|
||||
spa_debug_dump_mem (SPA_MEMBER (buffer, m->offset, void), m->size);
|
||||
break;
|
||||
case SPA_META_TYPE_VIDEO_CROP:
|
||||
fprintf (stderr, " SpaMetaVideoCrop:\n");
|
||||
spa_debug_dump_mem (SPA_MEMBER (buffer, m->offset, void), m->size);
|
||||
break;
|
||||
default:
|
||||
spa_debug_dump_mem (SPA_MEMBER (buffer, m->offset, void), m->size);
|
||||
break;
|
||||
}
|
||||
}
|
||||
fprintf (stderr, " n_datas: \t%u (offset %zd)\n", buffer->n_datas, buffer->datas);
|
||||
for (i = 0; i < buffer->n_datas; i++) {
|
||||
SpaData *d = &SPA_BUFFER_DATAS (buffer)[i];
|
||||
fprintf (stderr, " data %d: type %d\n", i, d->type);
|
||||
switch (d->type) {
|
||||
case SPA_DATA_TYPE_MEMPTR:
|
||||
fprintf (stderr, " memptr %p\n", d->ptr);
|
||||
break;
|
||||
case SPA_DATA_TYPE_FD:
|
||||
fprintf (stderr, " fd %d\n", SPA_PTR_TO_INT (d->ptr));
|
||||
break;
|
||||
case SPA_DATA_TYPE_MEMID:
|
||||
fprintf (stderr, " memid %d\n", SPA_PTR_TO_UINT32 (d->ptr));
|
||||
break;
|
||||
case SPA_DATA_TYPE_POINTER:
|
||||
fprintf (stderr, " pointer %p\n", d->ptr);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
fprintf (stderr, " offset %zd:\n", d->offset);
|
||||
fprintf (stderr, " size %zd:\n", d->size);
|
||||
fprintf (stderr, " stride %zd:\n", d->stride);
|
||||
}
|
||||
return SPA_RESULT_OK;
|
||||
}
|
||||
|
||||
SpaResult
|
||||
spa_debug_dump_mem (void *mem, size_t size)
|
||||
{
|
||||
|
|
@ -90,7 +156,7 @@ spa_debug_dump_mem (void *mem, size_t size)
|
|||
|
||||
for (i = 0; i < size; i++) {
|
||||
printf ("%02x ", t[i]);
|
||||
if (i % 16 == 8 || i == size - 1)
|
||||
if (i % 16 == 15 || i == size - 1)
|
||||
printf ("\n");
|
||||
}
|
||||
return SPA_RESULT_OK;
|
||||
|
|
|
|||
141
spa/lib/memory.c
Normal file
141
spa/lib/memory.c
Normal file
|
|
@ -0,0 +1,141 @@
|
|||
/* 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 <stdio.h>
|
||||
|
||||
#include "spa/memory.h"
|
||||
|
||||
#define MAX_POOLS 16
|
||||
#define MAX_MEMORIES 1024
|
||||
|
||||
struct _SpaMemoryPool {
|
||||
bool valid;
|
||||
uint32_t id;
|
||||
SpaMemory memories[MAX_MEMORIES];
|
||||
unsigned int n_free;
|
||||
uint32_t free_mem[MAX_MEMORIES];
|
||||
};
|
||||
|
||||
static SpaMemoryPool pools[MAX_POOLS];
|
||||
|
||||
static void
|
||||
spa_memory_pool_init (SpaMemoryPool *pool, uint32_t id)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < MAX_MEMORIES; i++)
|
||||
pool->free_mem[i] = MAX_MEMORIES - 1 - i;
|
||||
pool->n_free = MAX_MEMORIES;
|
||||
pool->id = id;
|
||||
pool->valid = true;
|
||||
}
|
||||
|
||||
void
|
||||
spa_memory_init (void)
|
||||
{
|
||||
static bool initialized = false;
|
||||
|
||||
if (!initialized) {
|
||||
spa_memory_pool_init (&pools[0], 0);
|
||||
initialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t
|
||||
spa_memory_pool_get (uint32_t type)
|
||||
{
|
||||
return pools[type].id;
|
||||
}
|
||||
|
||||
uint32_t
|
||||
spa_memory_pool_new (void)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < MAX_POOLS; i++) {
|
||||
if (!pools[i].valid) {
|
||||
spa_memory_pool_init (&pools[i], i);
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return SPA_ID_INVALID;
|
||||
}
|
||||
|
||||
void
|
||||
spa_memory_pool_free (uint32_t pool_id)
|
||||
{
|
||||
pools[pool_id].valid = false;
|
||||
}
|
||||
|
||||
SpaMemory *
|
||||
spa_memory_alloc (uint32_t pool_id)
|
||||
{
|
||||
SpaMemory *mem;
|
||||
SpaMemoryPool *pool;
|
||||
uint32_t id;
|
||||
|
||||
if (pool_id >= MAX_POOLS || !pools[pool_id].valid)
|
||||
return NULL;
|
||||
|
||||
pool = &pools[pool_id];
|
||||
|
||||
if (pool->n_free == 0)
|
||||
return NULL;
|
||||
|
||||
id = pool->free_mem[pool->n_free - 1];
|
||||
pool->n_free--;
|
||||
|
||||
mem = &pool->memories[id];
|
||||
mem->refcount = 1;
|
||||
mem->pool_id = pool_id;
|
||||
mem->id = id;
|
||||
|
||||
return mem;
|
||||
}
|
||||
|
||||
SpaResult
|
||||
spa_memory_free (uint32_t pool_id, uint32_t id)
|
||||
{
|
||||
SpaMemoryPool *pool;
|
||||
|
||||
if (pool_id >= MAX_POOLS || !pools[pool_id].valid)
|
||||
return SPA_RESULT_INVALID_ARGUMENTS;
|
||||
|
||||
pool = &pools[pool_id];
|
||||
pool->free_mem[pool->n_free] = id;
|
||||
pool->n_free++;
|
||||
|
||||
return SPA_RESULT_OK;
|
||||
}
|
||||
|
||||
SpaMemory *
|
||||
spa_memory_find (uint32_t pool_id, uint32_t id)
|
||||
{
|
||||
SpaMemoryPool *pool;
|
||||
|
||||
if (pool_id >= MAX_POOLS || !pools[pool_id].valid)
|
||||
return NULL;
|
||||
|
||||
pool = &pools[pool_id];
|
||||
|
||||
if (id >= MAX_MEMORIES || pool->memories[id].refcount <= 0)
|
||||
return NULL;
|
||||
|
||||
return &pool->memories[id];
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
spalib_sources = ['audio-raw.c',
|
||||
'control.c',
|
||||
'debug.c',
|
||||
'memory.c',
|
||||
'props.c',
|
||||
'ringbuffer.c',
|
||||
'video-raw.c']
|
||||
|
|
|
|||
|
|
@ -588,17 +588,19 @@ add_port_data (SpaAudioMixer *this, SpaBuffer *out, SpaAudioMixerPort *port)
|
|||
int i, oi = 0;
|
||||
uint8_t *op, *ip;
|
||||
size_t os, is, chunk;
|
||||
SpaData *odatas = SPA_BUFFER_DATAS (out);
|
||||
SpaData *idatas = SPA_BUFFER_DATAS (port->buffer);
|
||||
|
||||
op = ip = NULL;
|
||||
|
||||
while (true) {
|
||||
if (op == NULL) {
|
||||
op = out->datas[oi].ptr;
|
||||
os = out->datas[oi].size;
|
||||
op = odatas[oi].ptr;
|
||||
os = odatas[oi].size;
|
||||
}
|
||||
if (ip == NULL) {
|
||||
ip = port->buffer->datas[port->buffer_index].ptr;
|
||||
is = port->buffer->datas[port->buffer_index].size;
|
||||
ip = idatas[port->buffer_index].ptr;
|
||||
is = idatas[port->buffer_index].size;
|
||||
ip += port->buffer_offset;
|
||||
is -= port->buffer_offset;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -55,8 +55,9 @@ typedef struct {
|
|||
SpaPortInfo info;
|
||||
SpaPortStatus status;
|
||||
SpaFormat formats[2];
|
||||
SpaBuffer **buffers;
|
||||
unsigned int n_buffers;
|
||||
SpaBuffer **buffers;
|
||||
SpaBuffer *remote_buffers[128];
|
||||
} SpaProxyPort;
|
||||
|
||||
struct _SpaProxy {
|
||||
|
|
@ -566,13 +567,6 @@ tmpfile_create (void *data, size_t size)
|
|||
return fd;
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
SpaBuffer buffer;
|
||||
SpaData datas[16];
|
||||
int idx[16];
|
||||
SpaBuffer *orig;
|
||||
} MyBuffer;
|
||||
|
||||
static SpaResult
|
||||
add_buffer (SpaProxy *this, uint32_t port_id, SpaBuffer *buffer)
|
||||
{
|
||||
|
|
@ -581,51 +575,53 @@ add_buffer (SpaProxy *this, uint32_t port_id, SpaBuffer *buffer)
|
|||
uint8_t buf[1024];
|
||||
int fds[16];
|
||||
SpaControlCmdAddBuffer ab;
|
||||
bool tmpfile = false;
|
||||
unsigned int i;
|
||||
MyBuffer b;
|
||||
int fd, i;
|
||||
SpaResult res;
|
||||
SpaBuffer *b;
|
||||
|
||||
spa_control_builder_init_into (&builder, buf, sizeof (buf), fds, sizeof (fds));
|
||||
|
||||
b.buffer.id = buffer->id;
|
||||
b.buffer.size = buffer->size;
|
||||
b.buffer.n_metas = buffer->n_metas;
|
||||
b.buffer.metas = buffer->metas;
|
||||
b.buffer.n_datas = buffer->n_datas;
|
||||
b.buffer.datas = b.datas;
|
||||
fd = tmpfile_create (buffer, buffer->size);
|
||||
|
||||
for (i = 0; i < buffer->n_datas; i++) {
|
||||
SpaData *d = &buffer->datas[i];
|
||||
b = mmap (NULL, buffer->size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
for (i = 0; i < b->n_datas; i++) {
|
||||
SpaData *d = &SPA_BUFFER_DATAS (b)[i];
|
||||
int fd;
|
||||
SpaControlCmdAddMem am;
|
||||
bool tmpfile;
|
||||
|
||||
if (d->type == SPA_DATA_TYPE_FD) {
|
||||
fd = *((int *)d->ptr);
|
||||
} else {
|
||||
fd = SPA_PTR_TO_INT (d->ptr);
|
||||
tmpfile = false;
|
||||
} else if (d->type == SPA_DATA_TYPE_MEMPTR) {
|
||||
fd = tmpfile_create (d->ptr, d->size + d->offset);
|
||||
tmpfile = true;
|
||||
} else {
|
||||
fprintf (stderr, "proxy %p: invalid mem type received %d\n", this, d->type);
|
||||
continue;
|
||||
}
|
||||
|
||||
am.port_id = port_id;
|
||||
am.mem_id = buffer->id * 64 + i;
|
||||
am.mem_id = b->id * 64 + i;
|
||||
am.mem_type = 0;
|
||||
am.fd_index = spa_control_builder_add_fd (&builder, fd, tmpfile ? true : false);
|
||||
am.offset = 0;
|
||||
am.size = d->offset + d->size;
|
||||
am.offset = d->offset;
|
||||
am.size = d->size;
|
||||
spa_control_builder_add_cmd (&builder, SPA_CONTROL_CMD_ADD_MEM, &am);
|
||||
|
||||
b.idx[i] = am.mem_id;
|
||||
b.datas[i].type = SPA_DATA_TYPE_MEMID;
|
||||
b.datas[i].ptr_type = NULL;
|
||||
b.datas[i].ptr = &b.idx[i];
|
||||
b.datas[i].offset = d->offset;
|
||||
b.datas[i].size = d->size;
|
||||
b.datas[i].stride = d->stride;
|
||||
d->type = SPA_DATA_TYPE_MEMID;
|
||||
d->ptr_type = NULL;
|
||||
d->ptr = SPA_UINT32_TO_PTR (am.mem_id);
|
||||
d->offset = 0;
|
||||
}
|
||||
ab.port_id = port_id;
|
||||
ab.buffer = &b.buffer;
|
||||
fprintf (stderr, "proxy %p: add buffer %d\n", this, b.buffer.id);
|
||||
ab.buffer_id = b->id;
|
||||
ab.fd_index = spa_control_builder_add_fd (&builder, fd, true);
|
||||
ab.offset = 0;
|
||||
ab.size = b->size;
|
||||
spa_control_builder_add_cmd (&builder, SPA_CONTROL_CMD_ADD_BUFFER, &ab);
|
||||
munmap (b, buffer->size);
|
||||
|
||||
spa_control_builder_end (&builder, &control);
|
||||
|
||||
if ((res = spa_control_write (&control, this->fds[0].fd)) < 0)
|
||||
|
|
@ -654,7 +650,7 @@ remove_buffer (SpaProxy *this, uint32_t port_id, SpaBuffer *buffer)
|
|||
for (i = 0; i < buffer->n_datas; i++) {
|
||||
SpaControlCmdRemoveMem rm;
|
||||
rm.port_id = port_id;
|
||||
rm.mem_id = i;
|
||||
rm.mem_id = buffer->id * 64 + i;
|
||||
spa_control_builder_add_cmd (&builder, SPA_CONTROL_CMD_REMOVE_MEM, &rm);
|
||||
}
|
||||
spa_control_builder_end (&builder, &control);
|
||||
|
|
@ -868,8 +864,6 @@ parse_control (SpaProxy *this,
|
|||
while (spa_control_iter_next (&it) == SPA_RESULT_OK) {
|
||||
SpaControlCmd cmd = spa_control_iter_get_cmd (&it);
|
||||
|
||||
fprintf (stderr, "proxy %p: got control %d\n", this, cmd);
|
||||
|
||||
switch (cmd) {
|
||||
case SPA_CONTROL_CMD_ADD_PORT:
|
||||
case SPA_CONTROL_CMD_REMOVE_PORT:
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@
|
|||
|
||||
#include <spa/node.h>
|
||||
#include <spa/video/format.h>
|
||||
#include <spa/debug.h>
|
||||
|
||||
typedef struct _SpaV4l2Source SpaV4l2Source;
|
||||
|
||||
|
|
@ -721,7 +722,7 @@ v4l2_source_init (const SpaHandleFactory *factory,
|
|||
this->state[0].info.flags = SPA_PORT_INFO_FLAG_NONE;
|
||||
this->state[0].status.flags = SPA_PORT_STATUS_FLAG_NONE;
|
||||
|
||||
this->state[0].export_buf = false;
|
||||
this->state[0].export_buf = true;
|
||||
|
||||
return SPA_RESULT_OK;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -531,12 +531,7 @@ spa_v4l2_import_buffers (SpaV4l2Source *this, SpaBuffer **buffers, uint32_t n_bu
|
|||
fprintf (stderr, "import buffer %p\n", buffers[i]);
|
||||
|
||||
b->source = this;
|
||||
b->buffer.id = buffers[i]->id;
|
||||
b->buffer.size = buffers[i]->size;
|
||||
b->buffer.n_metas = buffers[i]->n_metas;
|
||||
b->buffer.metas = buffers[i]->metas;
|
||||
b->buffer.n_datas = buffers[i]->n_datas;
|
||||
b->buffer.datas = buffers[i]->datas;
|
||||
b->buffer.id = SPA_ID_INVALID;
|
||||
b->imported = buffers[i];
|
||||
b->outstanding = true;
|
||||
|
||||
|
|
@ -544,8 +539,8 @@ spa_v4l2_import_buffers (SpaV4l2Source *this, SpaBuffer **buffers, uint32_t n_bu
|
|||
b->v4l2_buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
||||
b->v4l2_buffer.memory = state->memtype;
|
||||
b->v4l2_buffer.index = i;
|
||||
b->v4l2_buffer.m.userptr = (unsigned long) b->buffer.datas[0].ptr;
|
||||
b->v4l2_buffer.length = b->buffer.datas[0].size;
|
||||
b->v4l2_buffer.m.userptr = (unsigned long) SPA_BUFFER_DATAS (buffers[i])[0].ptr;
|
||||
b->v4l2_buffer.length = SPA_BUFFER_DATAS (buffers[i])[0].size;
|
||||
|
||||
spa_v4l2_buffer_recycle (this, buffers[i]->id);
|
||||
}
|
||||
|
|
@ -609,11 +604,11 @@ mmap_init (SpaV4l2Source *this,
|
|||
|
||||
b->source = this;
|
||||
b->buffer.id = i;
|
||||
b->buffer.size = buf.length;
|
||||
b->buffer.size = sizeof (V4l2Buffer);
|
||||
b->buffer.n_metas = 1;
|
||||
b->buffer.metas = b->metas;
|
||||
b->buffer.metas = offsetof (V4l2Buffer, metas);
|
||||
b->buffer.n_datas = 1;
|
||||
b->buffer.datas = b->datas;
|
||||
b->buffer.datas = offsetof (V4l2Buffer, datas);
|
||||
|
||||
b->header.flags = 0;
|
||||
b->header.seq = 0;
|
||||
|
|
@ -621,7 +616,7 @@ mmap_init (SpaV4l2Source *this,
|
|||
b->header.dts_offset = 0;
|
||||
|
||||
b->metas[0].type = SPA_META_TYPE_HEADER;
|
||||
b->metas[0].data = &b->header;
|
||||
b->metas[0].offset = offsetof (V4l2Buffer, header);
|
||||
b->metas[0].size = sizeof (b->header);
|
||||
|
||||
if (state->export_buf) {
|
||||
|
|
@ -637,12 +632,13 @@ mmap_init (SpaV4l2Source *this,
|
|||
|
||||
b->dmafd = expbuf.fd;
|
||||
b->datas[0].type = SPA_DATA_TYPE_FD;
|
||||
b->datas[0].ptr = &b->dmafd;
|
||||
b->datas[0].ptr = SPA_INT_TO_PTR (b->dmafd);
|
||||
b->datas[0].ptr_type = "dmabuf";
|
||||
b->datas[0].offset = 0;
|
||||
b->datas[0].size = buf.length;
|
||||
b->datas[0].stride = state->fmt.fmt.pix.bytesperline;
|
||||
} else {
|
||||
#if 1
|
||||
b->datas[0].type = SPA_DATA_TYPE_MEMPTR;
|
||||
b->datas[0].ptr_type = "sysmem";
|
||||
b->datas[0].ptr = mmap (NULL,
|
||||
|
|
@ -653,11 +649,18 @@ mmap_init (SpaV4l2Source *this,
|
|||
buf.m.offset);
|
||||
b->datas[0].offset = 0;
|
||||
b->datas[0].size = buf.length;
|
||||
b->datas[0].stride = state->fmt.fmt.pix.bytesperline;
|
||||
if (b->datas[0].ptr == MAP_FAILED) {
|
||||
perror ("mmap");
|
||||
continue;
|
||||
}
|
||||
#else
|
||||
b->datas[0].type = SPA_DATA_TYPE_FD;
|
||||
b->datas[0].ptr = &state->fd;
|
||||
b->datas[0].ptr_type = "dmabuf";
|
||||
b->datas[0].offset = buf.m.offset;
|
||||
b->datas[0].size = buf.length;
|
||||
b->datas[0].stride = state->fmt.fmt.pix.bytesperline;
|
||||
#endif
|
||||
}
|
||||
b->imported = &b->buffer;
|
||||
b->outstanding = true;
|
||||
|
|
@ -667,6 +670,8 @@ mmap_init (SpaV4l2Source *this,
|
|||
b->v4l2_buffer.memory = state->memtype;
|
||||
b->v4l2_buffer.index = i;
|
||||
|
||||
spa_debug_buffer (&b->buffer);
|
||||
|
||||
spa_v4l2_buffer_recycle (this, i);
|
||||
}
|
||||
state->have_buffers = true;
|
||||
|
|
|
|||
|
|
@ -578,8 +578,8 @@ spa_volume_node_port_pull_output (SpaNode *node,
|
|||
if (si == sbuf->n_datas || di == dbuf->n_datas)
|
||||
break;
|
||||
|
||||
sd = &sbuf->datas[si];
|
||||
dd = &dbuf->datas[di];
|
||||
sd = &SPA_BUFFER_DATAS (sbuf)[si];
|
||||
dd = &SPA_BUFFER_DATAS (dbuf)[di];
|
||||
|
||||
if (sd->type != SPA_DATA_TYPE_MEMPTR) {
|
||||
si++;
|
||||
|
|
|
|||
|
|
@ -121,16 +121,20 @@ on_source_event (SpaNode *node, SpaEvent *event, void *user_data)
|
|||
int sstride, dstride;
|
||||
int i;
|
||||
uint8_t *src, *dst;
|
||||
SpaMeta *metas;
|
||||
SpaData *datas;
|
||||
|
||||
if ((res = spa_node_port_pull_output (data->source, 1, info)) < 0)
|
||||
printf ("got pull error %d\n", res);
|
||||
|
||||
b = data->bp[info->buffer_id];
|
||||
metas = SPA_BUFFER_METAS (b);
|
||||
datas = SPA_BUFFER_DATAS (b);
|
||||
|
||||
if (b->metas[1].type == SPA_META_TYPE_POINTER &&
|
||||
strcmp (((SpaMetaPointer*)b->metas[1].data)->ptr_type, "SDL_Texture") == 0) {
|
||||
if (metas[1].type == SPA_META_TYPE_POINTER &&
|
||||
strcmp (SPA_MEMBER (b, metas[1].offset, SpaMetaPointer)->ptr_type, "SDL_Texture") == 0) {
|
||||
SDL_Texture *texture;
|
||||
texture = ((SpaMetaPointer*)b->metas[1].data)->ptr;
|
||||
texture = SPA_MEMBER (b, metas[1].offset, SpaMetaPointer)->ptr;
|
||||
|
||||
SDL_UnlockTexture(texture);
|
||||
|
||||
|
|
@ -142,17 +146,17 @@ on_source_event (SpaNode *node, SpaEvent *event, void *user_data)
|
|||
fprintf (stderr, "Couldn't lock texture: %s\n", SDL_GetError());
|
||||
return;
|
||||
}
|
||||
b->datas[0].ptr = sdata;
|
||||
b->datas[0].ptr_type = "sysmem";
|
||||
b->datas[0].size = sstride * 240;
|
||||
b->datas[0].stride = sstride;
|
||||
datas[0].ptr = sdata;
|
||||
datas[0].ptr_type = "sysmem";
|
||||
datas[0].size = sstride * 240;
|
||||
datas[0].stride = sstride;
|
||||
} else {
|
||||
if (SDL_LockTexture (data->texture, NULL, &ddata, &dstride) < 0) {
|
||||
fprintf (stderr, "Couldn't lock texture: %s\n", SDL_GetError());
|
||||
return;
|
||||
}
|
||||
sdata = b->datas[0].ptr;
|
||||
sstride = b->datas[0].stride;
|
||||
sdata = datas[0].ptr;
|
||||
sstride = datas[0].stride;
|
||||
|
||||
for (i = 0; i < 240; i++) {
|
||||
src = ((uint8_t*)sdata + i * sstride);
|
||||
|
|
@ -238,7 +242,7 @@ alloc_buffers (AppData *data)
|
|||
}
|
||||
|
||||
b->buffer.id = i;
|
||||
b->buffer.size = stride * 240;
|
||||
b->buffer.size = sizeof (SDLBuffer);
|
||||
b->buffer.n_metas = 2;
|
||||
b->buffer.metas = b->metas;
|
||||
b->buffer.n_datas = 1;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue