Work on memory allocation

We now only allow per port preallocated buffers. We exchange the index
into the array instead of passing the buffers around. We still use the
refcount to track when a buffer can be reused.
Improve API a little, allow passing the node as the first argument of
the interface call.
Implement alloc_buffer in v4l2 and improve the test.
This commit is contained in:
Wim Taymans 2016-07-30 20:35:34 +02:00
parent 7cfd1eb8ee
commit 05829f33e6
29 changed files with 2266 additions and 800 deletions

View file

@ -30,15 +30,11 @@
#include <spa/audio/format.h>
typedef struct {
SpaHandle *sink;
const SpaNode *sink_node;
SpaHandle *mix;
const SpaNode *mix_node;
SpaNode *sink;
SpaNode *mix;
uint32_t mix_ports[2];
SpaHandle *source1;
const SpaNode *source1_node;
SpaHandle *source2;
const SpaNode *source2_node;
SpaNode *source1;
SpaNode *source2;
bool running;
pthread_t thread;
SpaPollFd fds[16];
@ -47,8 +43,9 @@ typedef struct {
} AppData;
static SpaResult
make_node (SpaHandle **handle, const SpaNode **node, const char *lib, const char *name)
make_node (SpaNode **node, const char *lib, const char *name)
{
SpaHandle *handle;
SpaResult res;
void *hnd;
SpaEnumHandleFactoryFunc enum_func;
@ -66,7 +63,7 @@ make_node (SpaHandle **handle, const SpaNode **node, const char *lib, const char
for (i = 0; ;i++) {
const SpaHandleFactory *factory;
const void *iface;
void *iface;
if ((res = enum_func (&factory, &state)) < 0) {
if (res != SPA_RESULT_ENUM_END)
@ -76,12 +73,12 @@ make_node (SpaHandle **handle, const SpaNode **node, const char *lib, const char
if (strcmp (factory->name, name))
continue;
*handle = calloc (1, factory->size);
if ((res = factory->init (factory, *handle)) < 0) {
handle = calloc (1, factory->size);
if ((res = factory->init (factory, handle)) < 0) {
printf ("can't make factory instance: %d\n", res);
return res;
}
if ((res = (*handle)->get_interface (*handle, SPA_INTERFACE_ID_NODE, &iface)) < 0) {
if ((res = handle->get_interface (handle, SPA_INTERFACE_ID_NODE, &iface)) < 0) {
printf ("can't get interface %d\n", res);
return res;
}
@ -92,39 +89,38 @@ make_node (SpaHandle **handle, const SpaNode **node, const char *lib, const char
}
static void
on_mix_event (SpaHandle *handle, SpaEvent *event, void *user_data)
on_mix_event (SpaNode *node, SpaEvent *event, void *user_data)
{
AppData *data = user_data;
switch (event->type) {
case SPA_EVENT_TYPE_PULL_INPUT:
{
SpaBuffer *buf;
SpaInputInfo iinfo;
SpaOutputInfo oinfo;
SpaResult res;
SpaEventPullInput *pi;
buf = event->data;
pi = event->data;
oinfo.port_id = 0;
oinfo.flags = SPA_OUTPUT_FLAG_NONE;
oinfo.buffer = buf;
oinfo.event = NULL;
oinfo.size = pi->size;
oinfo.offset = pi->offset;
if (event->port_id == data->mix_ports[0]) {
if ((res = data->source1_node->port_pull_output (data->source1, 1, &oinfo)) < 0)
if ((res = spa_node_port_pull_output (data->source1, 1, &oinfo)) < 0)
printf ("got error %d\n", res);
} else {
if ((res = data->source2_node->port_pull_output (data->source2, 1, &oinfo)) < 0)
if ((res = spa_node_port_pull_output (data->source2, 1, &oinfo)) < 0)
printf ("got error %d\n", res);
}
iinfo.port_id = event->port_id;
iinfo.flags = SPA_INPUT_FLAG_NONE;
iinfo.buffer = oinfo.buffer;
iinfo.event = oinfo.event;
iinfo.id = oinfo.id;
if ((res = data->mix_node->port_push_input (data->mix, 1, &iinfo)) < 0)
if ((res = spa_node_port_push_input (data->mix, 1, &iinfo)) < 0)
printf ("got error from mixer %d\n", res);
break;
}
@ -135,34 +131,33 @@ on_mix_event (SpaHandle *handle, SpaEvent *event, void *user_data)
}
static void
on_sink_event (SpaHandle *handle, SpaEvent *event, void *user_data)
on_sink_event (SpaNode *node, SpaEvent *event, void *user_data)
{
AppData *data = user_data;
switch (event->type) {
case SPA_EVENT_TYPE_PULL_INPUT:
{
SpaBuffer *buf;
SpaInputInfo iinfo;
SpaOutputInfo oinfo;
SpaResult res;
SpaEventPullInput *pi;
buf = event->data;
pi = event->data;
oinfo.port_id = 0;
oinfo.flags = SPA_OUTPUT_FLAG_PULL;
oinfo.buffer = buf;
oinfo.event = NULL;
oinfo.offset = pi->offset;
oinfo.size = pi->size;
if ((res = data->mix_node->port_pull_output (data->mix, 1, &oinfo)) < 0)
if ((res = spa_node_port_pull_output (data->mix, 1, &oinfo)) < 0)
printf ("got error %d\n", res);
iinfo.port_id = event->port_id;
iinfo.flags = SPA_INPUT_FLAG_NONE;
iinfo.buffer = oinfo.buffer;
iinfo.event = oinfo.event;
iinfo.id = oinfo.id;
if ((res = data->sink_node->port_push_input (data->sink, 1, &iinfo)) < 0)
if ((res = spa_node_port_push_input (data->sink, 1, &iinfo)) < 0)
printf ("got error %d\n", res);
break;
}
@ -192,13 +187,13 @@ make_nodes (AppData *data)
SpaProps *props;
SpaPropValue value;
if ((res = make_node (&data->sink, &data->sink_node, "plugins/alsa/libspa-alsa.so", "alsa-sink")) < 0) {
if ((res = make_node (&data->sink, "plugins/alsa/libspa-alsa.so", "alsa-sink")) < 0) {
printf ("can't create alsa-sink: %d\n", res);
return res;
}
data->sink_node->set_event_callback (data->sink, on_sink_event, data);
spa_node_set_event_callback (data->sink, on_sink_event, data);
if ((res = data->sink_node->get_props (data->sink, &props)) < 0)
if ((res = spa_node_get_props (data->sink, &props)) < 0)
printf ("got get_props error %d\n", res);
value.type = SPA_PROP_TYPE_STRING;
@ -206,21 +201,21 @@ make_nodes (AppData *data)
value.size = strlen (value.value)+1;
props->set_prop (props, spa_props_index_for_name (props, "device"), &value);
if ((res = data->sink_node->set_props (data->sink, props)) < 0)
if ((res = spa_node_set_props (data->sink, props)) < 0)
printf ("got set_props error %d\n", res);
if ((res = make_node (&data->mix, &data->mix_node, "plugins/audiomixer/libspa-audiomixer.so", "audiomixer")) < 0) {
if ((res = make_node (&data->mix, "plugins/audiomixer/libspa-audiomixer.so", "audiomixer")) < 0) {
printf ("can't create audiomixer: %d\n", res);
return res;
}
data->mix_node->set_event_callback (data->mix, on_mix_event, data);
spa_node_set_event_callback (data->mix, on_mix_event, data);
if ((res = make_node (&data->source1, &data->source1_node, "plugins/audiotestsrc/libspa-audiotestsrc.so", "audiotestsrc")) < 0) {
if ((res = make_node (&data->source1, "plugins/audiotestsrc/libspa-audiotestsrc.so", "audiotestsrc")) < 0) {
printf ("can't create audiotestsrc: %d\n", res);
return res;
}
if ((res = make_node (&data->source2, &data->source2_node, "plugins/audiotestsrc/libspa-audiotestsrc.so", "audiotestsrc")) < 0) {
if ((res = make_node (&data->source2, "plugins/audiotestsrc/libspa-audiotestsrc.so", "audiotestsrc")) < 0) {
printf ("can't create audiotestsrc: %d\n", res);
return res;
}
@ -237,7 +232,7 @@ negotiate_formats (AppData *data)
SpaPropValue value;
void *state = NULL;
if ((res = data->sink_node->port_enum_formats (data->sink, 0, &format, NULL, &state)) < 0)
if ((res = spa_node_port_enum_formats (data->sink, 0, &format, NULL, &state)) < 0)
return res;
props = &format->props;
@ -259,28 +254,30 @@ negotiate_formats (AppData *data)
if ((res = props->set_prop (props, spa_props_index_for_id (props, SPA_PROP_ID_AUDIO_CHANNELS), &value)) < 0)
return res;
if ((res = data->sink_node->port_set_format (data->sink, 0, false, format)) < 0)
if ((res = spa_node_port_set_format (data->sink, 0, false, format)) < 0)
return res;
if ((res = data->mix_node->port_set_format (data->mix, 0, false, format)) < 0)
if ((res = spa_node_port_set_format (data->mix, 0, false, format)) < 0)
return res;
if ((res = data->mix_node->add_port (data->mix, SPA_DIRECTION_INPUT, &data->mix_ports[0])) < 0)
data->mix_ports[0] = 0;
if ((res = spa_node_add_port (data->mix, SPA_DIRECTION_INPUT, 0)) < 0)
return res;
if ((res = data->mix_node->port_set_format (data->mix, data->mix_ports[0], false, format)) < 0)
if ((res = spa_node_port_set_format (data->mix, data->mix_ports[0], false, format)) < 0)
return res;
if ((res = data->source1_node->port_set_format (data->source1, 0, false, format)) < 0)
if ((res = spa_node_port_set_format (data->source1, 0, false, format)) < 0)
return res;
if ((res = data->mix_node->add_port (data->mix, SPA_DIRECTION_INPUT, &data->mix_ports[1])) < 0)
data->mix_ports[1] = 0;
if ((res = spa_node_add_port (data->mix, SPA_DIRECTION_INPUT, 1)) < 0)
return res;
if ((res = data->mix_node->port_set_format (data->mix, data->mix_ports[1], false, format)) < 0)
if ((res = spa_node_port_set_format (data->mix, data->mix_ports[1], false, format)) < 0)
return res;
if ((res = data->source2_node->port_set_format (data->source2, 0, false, format)) < 0)
if ((res = spa_node_port_set_format (data->source2, 0, false, format)) < 0)
return res;
@ -327,7 +324,7 @@ run_async_sink (AppData *data)
int err;
cmd.type = SPA_COMMAND_START;
if ((res = data->sink_node->send_command (data->sink, &cmd)) < 0)
if ((res = spa_node_send_command (data->sink, &cmd)) < 0)
printf ("got error %d\n", res);
data->running = true;
@ -345,7 +342,7 @@ run_async_sink (AppData *data)
}
cmd.type = SPA_COMMAND_STOP;
if ((res = data->sink_node->send_command (data->sink, &cmd)) < 0)
if ((res = spa_node_send_command (data->sink, &cmd)) < 0)
printf ("got error %d\n", res);
}

View file

@ -32,7 +32,7 @@
#include <spa/debug.h>
#include <spa/video/format.h>
#define USE_BUFFER
#undef USE_BUFFER
#define MAX_BUFFERS 8
@ -45,8 +45,7 @@ typedef struct {
} SDLBuffer;
typedef struct {
SpaHandle *source;
const SpaNode *source_node;
SpaNode *source;
SDL_Renderer *renderer;
SDL_Window *window;
SDL_Texture *texture;
@ -55,13 +54,16 @@ typedef struct {
SpaPollFd fds[16];
unsigned int n_fds;
SpaPollItem poll;
SpaBuffer *bp[MAX_BUFFERS];
SDLBuffer buffers[MAX_BUFFERS];
unsigned int n_buffers;
} AppData;
static SpaResult
make_node (SpaHandle **handle, const SpaNode **node, const char *lib, const char *name)
make_node (SpaNode **node, const char *lib, const char *name)
{
SpaHandle *handle;
SpaResult res;
void *hnd;
SpaEnumHandleFactoryFunc enum_func;
@ -79,7 +81,7 @@ make_node (SpaHandle **handle, const SpaNode **node, const char *lib, const char
for (i = 0; ;i++) {
const SpaHandleFactory *factory;
const void *iface;
void *iface;
if ((res = enum_func (&factory, &state)) < 0) {
if (res != SPA_RESULT_ENUM_END)
@ -89,12 +91,12 @@ make_node (SpaHandle **handle, const SpaNode **node, const char *lib, const char
if (strcmp (factory->name, name))
continue;
*handle = calloc (1, factory->size);
if ((res = factory->init (factory, *handle)) < 0) {
handle = calloc (1, factory->size);
if ((res = factory->init (factory, handle)) < 0) {
printf ("can't make factory instance: %d\n", res);
return res;
}
if ((res = (*handle)->get_interface (*handle, SPA_INTERFACE_ID_NODE, &iface)) < 0) {
if ((res = handle->get_interface (handle, SPA_INTERFACE_ID_NODE, &iface)) < 0) {
printf ("can't get interface %d\n", res);
return res;
}
@ -103,10 +105,9 @@ make_node (SpaHandle **handle, const SpaNode **node, const char *lib, const char
}
return SPA_RESULT_ERROR;
}
#define MIN(a,b) (a < b ? a : b)
static void
on_source_event (SpaHandle *handle, SpaEvent *event, void *user_data)
on_source_event (SpaNode *node, SpaEvent *event, void *user_data)
{
AppData *data = user_data;
@ -121,10 +122,10 @@ on_source_event (SpaHandle *handle, SpaEvent *event, void *user_data)
int i;
uint8_t *src, *dst;
if ((res = data->source_node->port_pull_output (data->source, 1, info)) < 0)
if ((res = spa_node_port_pull_output (data->source, 1, info)) < 0)
printf ("got pull error %d\n", res);
b = info[0].buffer;
b = data->bp[info->id];
if (b->metas[1].type == SPA_META_TYPE_POINTER &&
strcmp (((SpaMetaPointer*)b->metas[1].data)->ptr_type, "SDL_Texture") == 0) {
@ -156,7 +157,7 @@ on_source_event (SpaHandle *handle, SpaEvent *event, void *user_data)
for (i = 0; i < 240; i++) {
src = ((uint8_t*)sdata + i * sstride);
dst = ((uint8_t*)ddata + i * dstride);
memcpy (dst, src, MIN (sstride, dstride));
memcpy (dst, src, SPA_MIN (sstride, dstride));
}
SDL_UnlockTexture(data->texture);
@ -190,13 +191,13 @@ make_nodes (AppData *data, const char *device)
SpaProps *props;
SpaPropValue value;
if ((res = make_node (&data->source, &data->source_node, "plugins/v4l2/libspa-v4l2.so", "v4l2-source")) < 0) {
if ((res = make_node (&data->source, "plugins/v4l2/libspa-v4l2.so", "v4l2-source")) < 0) {
printf ("can't create v4l2-source: %d\n", res);
return res;
}
data->source_node->set_event_callback (data->source, on_source_event, data);
spa_node_set_event_callback (data->source, on_source_event, data);
if ((res = data->source_node->get_props (data->source, &props)) < 0)
if ((res = spa_node_get_props (data->source, &props)) < 0)
printf ("got get_props error %d\n", res);
value.type = SPA_PROP_TYPE_STRING;
@ -204,7 +205,7 @@ make_nodes (AppData *data, const char *device)
value.size = strlen (value.value)+1;
props->set_prop (props, spa_props_index_for_name (props, "device"), &value);
if ((res = data->source_node->set_props (data->source, props)) < 0)
if ((res = spa_node_set_props (data->source, props)) < 0)
printf ("got set_props error %d\n", res);
return res;
}
@ -238,6 +239,7 @@ alloc_buffers (AppData *data)
b->buffer.refcount = 1;
b->buffer.notify = NULL;
b->buffer.id = i;
b->buffer.size = stride * 240;
b->buffer.n_metas = 2;
b->buffer.metas = b->metas;
@ -265,7 +267,9 @@ alloc_buffers (AppData *data)
b->datas[0].size = stride * 240;
b->datas[0].stride = stride;
}
data->source_node->port_use_buffers (data->source, 0, data->bp, MAX_BUFFERS);
data->n_buffers = MAX_BUFFERS;
spa_node_port_use_buffers (data->source, 0, data->bp, MAX_BUFFERS);
}
#endif
@ -287,7 +291,7 @@ negotiate_formats (AppData *data)
#if 0
void *state = NULL;
if ((res = data->source_node->port_enum_formats (data->source, 0, &format, NULL, &state)) < 0)
if ((res = spa_node_port_enum_formats (data->source, 0, &format, NULL, &state)) < 0)
return res;
#else
f.fmt.media_type = SPA_MEDIA_TYPE_VIDEO;
@ -315,10 +319,10 @@ negotiate_formats (AppData *data)
f.framerate.denom = 1;
#endif
if ((res = data->source_node->port_set_format (data->source, 0, false, &f.fmt)) < 0)
if ((res = spa_node_port_set_format (data->source, 0, false, &f.fmt)) < 0)
return res;
if ((res = data->source_node->port_get_info (data->source, 0, &info)) < 0)
if ((res = spa_node_port_get_info (data->source, 0, &info)) < 0)
return res;
spa_debug_port_info (info);
@ -326,13 +330,23 @@ negotiate_formats (AppData *data)
#ifdef USE_BUFFER
alloc_buffers (data);
#else
data->texture = SDL_CreateTexture (data->renderer,
SDL_PIXELFORMAT_YUY2,
SDL_TEXTUREACCESS_STREAMING,
320, 240);
if (!data->texture) {
printf ("can't create texture: %s\n", SDL_GetError ());
return -1;
{
unsigned int n_buffers;
data->texture = SDL_CreateTexture (data->renderer,
SDL_PIXELFORMAT_YUY2,
SDL_TEXTUREACCESS_STREAMING,
320, 240);
if (!data->texture) {
printf ("can't create texture: %s\n", SDL_GetError ());
return -1;
}
n_buffers = MAX_BUFFERS;
if ((res = spa_node_port_alloc_buffers (data->source, 0, NULL, 0, data->bp, &n_buffers)) < 0) {
printf ("can't allocate buffers: %s\n", SDL_GetError ());
return -1;
}
data->n_buffers = n_buffers;
}
#endif
@ -379,7 +393,7 @@ run_async_source (AppData *data)
int err;
cmd.type = SPA_COMMAND_START;
if ((res = data->source_node->send_command (data->source, &cmd)) < 0)
if ((res = spa_node_send_command (data->source, &cmd)) < 0)
printf ("got error %d\n", res);
data->running = true;
@ -412,7 +426,7 @@ run_async_source (AppData *data)
}
cmd.type = SPA_COMMAND_STOP;
if ((res = data->source_node->send_command (data->source, &cmd)) < 0)
if ((res = spa_node_send_command (data->source, &cmd)) < 0)
printf ("got error %d\n", res);
}