node: improve callbacks

Make separate callbacks for events and RT notifications.
This commit is contained in:
Wim Taymans 2017-05-11 10:29:20 +02:00
parent fb0919b8b7
commit 3b33e3d362
32 changed files with 557 additions and 481 deletions

View file

@ -217,52 +217,61 @@ make_node (AppData *data, SpaNode **node, const char *lib, const char *name, boo
static void
on_sink_event (SpaNode *node, SpaEvent *event, void *user_data)
{
printf ("got event %d\n", SPA_EVENT_TYPE (event));
}
static void
on_sink_need_input (SpaNode *node, void *user_data)
{
AppData *data = user_data;
SpaResult res;
if (SPA_EVENT_TYPE (event) == data->type.event_node.NeedInput) {
res = spa_node_process_output (data->mix);
if (res == SPA_RESULT_NEED_BUFFER) {
if (data->source1_mix_io[0].status == SPA_RESULT_NEED_BUFFER) {
res = spa_node_process_output (data->source1);
if (res != SPA_RESULT_HAVE_BUFFER)
printf ("got process_output error from source1 %d\n", res);
}
if (data->source2_mix_io[0].status == SPA_RESULT_NEED_BUFFER) {
res = spa_node_process_output (data->source2);
if (res != SPA_RESULT_HAVE_BUFFER)
printf ("got process_output error from source2 %d\n", res);
}
res = spa_node_process_input (data->mix);
if (res == SPA_RESULT_HAVE_BUFFER)
goto push;
else
printf ("got process_input error from mixer %d\n", res);
} else if (res == SPA_RESULT_HAVE_BUFFER) {
push:
if ((res = spa_node_process_input (data->sink)) < 0)
printf ("got process_input error from sink %d\n", res);
} else {
printf ("got process_output error from mixer %d\n", res);
res = spa_node_process_output (data->mix);
if (res == SPA_RESULT_NEED_BUFFER) {
if (data->source1_mix_io[0].status == SPA_RESULT_NEED_BUFFER) {
res = spa_node_process_output (data->source1);
if (res != SPA_RESULT_HAVE_BUFFER)
printf ("got process_output error from source1 %d\n", res);
}
}
else if (SPA_EVENT_TYPE (event) == data->type.event_node.ReuseBuffer) {
SpaEventNodeReuseBuffer *rb = (SpaEventNodeReuseBuffer *) event;
data->mix_sink_io[0].buffer_id = rb->body.buffer_id.value;
}
else {
printf ("got event %d\n", SPA_EVENT_TYPE (event));
if (data->source2_mix_io[0].status == SPA_RESULT_NEED_BUFFER) {
res = spa_node_process_output (data->source2);
if (res != SPA_RESULT_HAVE_BUFFER)
printf ("got process_output error from source2 %d\n", res);
}
res = spa_node_process_input (data->mix);
if (res == SPA_RESULT_HAVE_BUFFER)
goto push;
else
printf ("got process_input error from mixer %d\n", res);
} else if (res == SPA_RESULT_HAVE_BUFFER) {
push:
if ((res = spa_node_process_input (data->sink)) < 0)
printf ("got process_input error from sink %d\n", res);
} else {
printf ("got process_output error from mixer %d\n", res);
}
}
static void
on_sink_reuse_buffer (SpaNode *node, uint32_t port_id, uint32_t buffer_id, void *user_data)
{
AppData *data = user_data;
data->mix_sink_io[0].buffer_id = buffer_id;
}
static const SpaNodeCallbacks sink_callbacks =
{
&on_sink_event,
&on_sink_need_input,
NULL,
&on_sink_reuse_buffer
};
static SpaResult
do_add_source (SpaLoop *loop,
SpaSource *source)
@ -313,7 +322,7 @@ make_nodes (AppData *data, const char *device)
printf ("can't create alsa-sink: %d\n", res);
return res;
}
spa_node_set_event_callback (data->sink, on_sink_event, data);
spa_node_set_callbacks (data->sink, &sink_callbacks, sizeof (sink_callbacks), data);
spa_pod_builder_init (&b, buffer, sizeof (buffer));
spa_pod_builder_props (&b, &f[0], data->type.props,

View file

@ -209,28 +209,38 @@ make_node (AppData *data, SpaNode **node, const char *lib, const char *name, boo
static void
on_sink_event (SpaNode *node, SpaEvent *event, void *user_data)
{
printf ("got event %d\n", SPA_EVENT_TYPE (event));
}
static void
on_sink_need_input (SpaNode *node, void *user_data)
{
AppData *data = user_data;
SpaResult res;
if (SPA_EVENT_TYPE (event) == data->type.event_node.NeedInput) {
res = spa_node_process_output (data->source);
if (res != SPA_RESULT_HAVE_BUFFER)
printf ("got process_output error from source %d\n", res);
res = spa_node_process_output (data->source);
if (res != SPA_RESULT_HAVE_BUFFER)
printf ("got process_output error from source %d\n", res);
if ((res = spa_node_process_input (data->sink)) < 0)
printf ("got process_input error from sink %d\n", res);
}
else if (SPA_EVENT_TYPE (event) == data->type.event_node.ReuseBuffer) {
SpaEventNodeReuseBuffer *rb = (SpaEventNodeReuseBuffer *) event;
data->source_sink_io[0].buffer_id = rb->body.buffer_id.value;
}
else {
printf ("got event %d\n", SPA_EVENT_TYPE (event));
}
if ((res = spa_node_process_input (data->sink)) < 0)
printf ("got process_input error from sink %d\n", res);
}
static void
on_sink_reuse_buffer (SpaNode *node, uint32_t port_id, uint32_t buffer_id, void *user_data)
{
AppData *data = user_data;
data->source_sink_io[0].buffer_id = buffer_id;
}
static const SpaNodeCallbacks sink_callbacks = {
&on_sink_event,
&on_sink_need_input,
NULL,
&on_sink_reuse_buffer
};
static SpaResult
do_add_source (SpaLoop *loop,
SpaSource *source)
@ -281,7 +291,7 @@ make_nodes (AppData *data)
printf ("can't create alsa-sink: %d\n", res);
return res;
}
spa_node_set_event_callback (data->sink, on_sink_event, data);
spa_node_set_callbacks (data->sink, &sink_callbacks, sizeof (sink_callbacks), data);
spa_pod_builder_init (&b, buffer, sizeof (buffer));
spa_pod_builder_props (&b, &f[0], data->type.props,

View file

@ -183,76 +183,87 @@ on_source_event (SpaNode *node, SpaEvent *event, void *user_data)
handle_events (data);
if (SPA_EVENT_TYPE (event) == data->type.event_node.HaveOutput) {
SpaResult res;
SpaBuffer *b;
void *sdata, *ddata;
int sstride, dstride;
int i;
uint8_t *src, *dst;
SpaMeta *metas;
SpaData *datas;
SpaPortIO *io = &data->source_output[0];
b = data->bp[io->buffer_id];
metas = b->metas;
datas = b->datas;
if (metas[1].type == data->type.meta.Pointer &&
((SpaMetaPointer *)metas[1].data)->type == data->type.SDL_Texture) {
SDL_Texture *texture;
texture = ((SpaMetaPointer *)metas[1].data)->ptr;
SDL_UnlockTexture(texture);
SDL_RenderClear (data->renderer);
SDL_RenderCopy (data->renderer, texture, NULL, NULL);
SDL_RenderPresent (data->renderer);
if (SDL_LockTexture (texture, NULL, &sdata, &sstride) < 0) {
fprintf (stderr, "Couldn't lock texture: %s\n", SDL_GetError());
return;
}
datas[0].type = data->type.data.MemPtr;
datas[0].flags = 0;
datas[0].fd = -1;
datas[0].mapoffset = 0;
datas[0].maxsize = sstride * 240;
datas[0].data = sdata;
datas[0].chunk->offset = 0;
datas[0].chunk->size = sstride * 240;
datas[0].chunk->stride = sstride;
} else {
if (SDL_LockTexture (data->texture, NULL, &ddata, &dstride) < 0) {
fprintf (stderr, "Couldn't lock texture: %s\n", SDL_GetError());
return;
}
sdata = datas[0].data;
sstride = datas[0].chunk->stride;
for (i = 0; i < 240; i++) {
src = ((uint8_t*)sdata + i * sstride);
dst = ((uint8_t*)ddata + i * dstride);
memcpy (dst, src, SPA_MIN (sstride, dstride));
}
SDL_UnlockTexture(data->texture);
SDL_RenderClear (data->renderer);
SDL_RenderCopy (data->renderer, data->texture, NULL, NULL);
SDL_RenderPresent (data->renderer);
}
io->status = SPA_RESULT_NEED_BUFFER;
if ((res = spa_node_process_output (data->source)) < 0)
printf ("got pull error %d\n", res);
}
else {
printf ("got event %d\n", SPA_EVENT_TYPE (event));
}
printf ("got event %d\n", SPA_EVENT_TYPE (event));
}
static void
on_source_have_output (SpaNode *node, void *user_data)
{
AppData *data = user_data;
SpaResult res;
SpaBuffer *b;
void *sdata, *ddata;
int sstride, dstride;
int i;
uint8_t *src, *dst;
SpaMeta *metas;
SpaData *datas;
SpaPortIO *io = &data->source_output[0];
handle_events (data);
b = data->bp[io->buffer_id];
metas = b->metas;
datas = b->datas;
if (metas[1].type == data->type.meta.Pointer &&
((SpaMetaPointer *)metas[1].data)->type == data->type.SDL_Texture) {
SDL_Texture *texture;
texture = ((SpaMetaPointer *)metas[1].data)->ptr;
SDL_UnlockTexture(texture);
SDL_RenderClear (data->renderer);
SDL_RenderCopy (data->renderer, texture, NULL, NULL);
SDL_RenderPresent (data->renderer);
if (SDL_LockTexture (texture, NULL, &sdata, &sstride) < 0) {
fprintf (stderr, "Couldn't lock texture: %s\n", SDL_GetError());
return;
}
datas[0].type = data->type.data.MemPtr;
datas[0].flags = 0;
datas[0].fd = -1;
datas[0].mapoffset = 0;
datas[0].maxsize = sstride * 240;
datas[0].data = sdata;
datas[0].chunk->offset = 0;
datas[0].chunk->size = sstride * 240;
datas[0].chunk->stride = sstride;
} else {
if (SDL_LockTexture (data->texture, NULL, &ddata, &dstride) < 0) {
fprintf (stderr, "Couldn't lock texture: %s\n", SDL_GetError());
return;
}
sdata = datas[0].data;
sstride = datas[0].chunk->stride;
for (i = 0; i < 240; i++) {
src = ((uint8_t*)sdata + i * sstride);
dst = ((uint8_t*)ddata + i * dstride);
memcpy (dst, src, SPA_MIN (sstride, dstride));
}
SDL_UnlockTexture(data->texture);
SDL_RenderClear (data->renderer);
SDL_RenderCopy (data->renderer, data->texture, NULL, NULL);
SDL_RenderPresent (data->renderer);
}
io->status = SPA_RESULT_NEED_BUFFER;
if ((res = spa_node_process_output (data->source)) < 0)
printf ("got pull error %d\n", res);
}
static const SpaNodeCallbacks source_callbacks = {
&on_source_event,
NULL,
&on_source_have_output,
NULL
};
static SpaResult
do_add_source (SpaLoop *loop,
SpaSource *source)
@ -301,7 +312,7 @@ make_nodes (AppData *data, const char *device)
printf ("can't create v4l2-source: %d\n", res);
return res;
}
spa_node_set_event_callback (data->source, on_source_event, data);
spa_node_set_callbacks (data->source, &source_callbacks, sizeof (source_callbacks), data);
spa_pod_builder_init (&b, buffer, sizeof (buffer));
spa_pod_builder_props (&b, &f[0], data->type.props,