2016-07-06 17:43:37 +00:00
|
|
|
/* Spa
|
|
|
|
* 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 <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <dlfcn.h>
|
2016-07-07 14:59:23 +00:00
|
|
|
#include <poll.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <errno.h>
|
2016-07-06 17:43:37 +00:00
|
|
|
|
2016-07-07 09:31:52 +00:00
|
|
|
#include <SDL2/SDL.h>
|
|
|
|
|
2016-07-06 17:43:37 +00:00
|
|
|
#include <spa/node.h>
|
2016-07-13 16:29:55 +00:00
|
|
|
#include <spa/debug.h>
|
2016-08-03 19:33:57 +00:00
|
|
|
#include <spa/memory.h>
|
2016-07-06 17:43:37 +00:00
|
|
|
#include <spa/video/format.h>
|
|
|
|
|
2016-07-08 18:12:56 +00:00
|
|
|
#define MAX_BUFFERS 8
|
|
|
|
|
|
|
|
typedef struct {
|
2016-07-11 15:45:54 +00:00
|
|
|
SpaBuffer buffer;
|
|
|
|
SpaMeta metas[2];
|
2016-07-08 18:12:56 +00:00
|
|
|
SpaMetaHeader header;
|
2016-07-11 15:45:54 +00:00
|
|
|
SpaMetaPointer ptr;
|
|
|
|
SpaData datas[1];
|
|
|
|
} SDLBuffer;
|
2016-07-08 18:12:56 +00:00
|
|
|
|
2016-07-06 17:43:37 +00:00
|
|
|
typedef struct {
|
2016-07-30 18:35:34 +00:00
|
|
|
SpaNode *source;
|
2016-08-03 20:11:03 +00:00
|
|
|
|
2016-07-07 09:31:52 +00:00
|
|
|
SDL_Renderer *renderer;
|
|
|
|
SDL_Window *window;
|
|
|
|
SDL_Texture *texture;
|
2016-08-03 20:11:03 +00:00
|
|
|
|
|
|
|
bool use_buffer;
|
|
|
|
|
2016-07-07 14:59:23 +00:00
|
|
|
bool running;
|
|
|
|
pthread_t thread;
|
2016-07-08 10:18:01 +00:00
|
|
|
SpaPollFd fds[16];
|
|
|
|
unsigned int n_fds;
|
|
|
|
SpaPollItem poll;
|
2016-07-30 18:35:34 +00:00
|
|
|
|
2016-07-11 15:45:54 +00:00
|
|
|
SpaBuffer *bp[MAX_BUFFERS];
|
|
|
|
SDLBuffer buffers[MAX_BUFFERS];
|
2016-07-30 18:35:34 +00:00
|
|
|
unsigned int n_buffers;
|
2016-07-06 17:43:37 +00:00
|
|
|
} AppData;
|
|
|
|
|
|
|
|
static SpaResult
|
2016-07-30 18:35:34 +00:00
|
|
|
make_node (SpaNode **node, const char *lib, const char *name)
|
2016-07-06 17:43:37 +00:00
|
|
|
{
|
2016-07-30 18:35:34 +00:00
|
|
|
SpaHandle *handle;
|
2016-07-06 17:43:37 +00:00
|
|
|
SpaResult res;
|
|
|
|
void *hnd;
|
|
|
|
SpaEnumHandleFactoryFunc enum_func;
|
|
|
|
unsigned int i;
|
2016-07-15 16:22:29 +00:00
|
|
|
void *state = NULL;
|
2016-07-06 17:43:37 +00:00
|
|
|
|
|
|
|
if ((hnd = dlopen (lib, RTLD_NOW)) == NULL) {
|
|
|
|
printf ("can't load %s: %s\n", lib, dlerror());
|
|
|
|
return SPA_RESULT_ERROR;
|
|
|
|
}
|
|
|
|
if ((enum_func = dlsym (hnd, "spa_enum_handle_factory")) == NULL) {
|
|
|
|
printf ("can't find enum function\n");
|
|
|
|
return SPA_RESULT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; ;i++) {
|
|
|
|
const SpaHandleFactory *factory;
|
2016-07-30 18:35:34 +00:00
|
|
|
void *iface;
|
2016-07-06 17:43:37 +00:00
|
|
|
|
2016-07-15 16:22:29 +00:00
|
|
|
if ((res = enum_func (&factory, &state)) < 0) {
|
2016-07-06 17:43:37 +00:00
|
|
|
if (res != SPA_RESULT_ENUM_END)
|
|
|
|
printf ("can't enumerate factories: %d\n", res);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (strcmp (factory->name, name))
|
|
|
|
continue;
|
|
|
|
|
2016-07-30 18:35:34 +00:00
|
|
|
handle = calloc (1, factory->size);
|
|
|
|
if ((res = factory->init (factory, handle)) < 0) {
|
2016-07-06 17:43:37 +00:00
|
|
|
printf ("can't make factory instance: %d\n", res);
|
|
|
|
return res;
|
|
|
|
}
|
2016-07-30 18:35:34 +00:00
|
|
|
if ((res = handle->get_interface (handle, SPA_INTERFACE_ID_NODE, &iface)) < 0) {
|
2016-07-06 17:43:37 +00:00
|
|
|
printf ("can't get interface %d\n", res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
*node = iface;
|
|
|
|
return SPA_RESULT_OK;
|
|
|
|
}
|
|
|
|
return SPA_RESULT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-07-30 18:35:34 +00:00
|
|
|
on_source_event (SpaNode *node, SpaEvent *event, void *user_data)
|
2016-07-06 17:43:37 +00:00
|
|
|
{
|
|
|
|
AppData *data = user_data;
|
|
|
|
|
|
|
|
switch (event->type) {
|
2016-08-24 14:26:58 +00:00
|
|
|
case SPA_EVENT_TYPE_HAVE_OUTPUT:
|
2016-07-06 17:43:37 +00:00
|
|
|
{
|
|
|
|
SpaOutputInfo info[1] = { 0, };
|
|
|
|
SpaResult res;
|
2016-07-08 18:12:56 +00:00
|
|
|
SpaBuffer *b;
|
2016-07-07 09:31:52 +00:00
|
|
|
void *sdata, *ddata;
|
|
|
|
int sstride, dstride;
|
|
|
|
int i;
|
|
|
|
uint8_t *src, *dst;
|
2016-08-03 13:59:17 +00:00
|
|
|
SpaMeta *metas;
|
|
|
|
SpaData *datas;
|
2016-08-03 19:33:57 +00:00
|
|
|
SpaMemory *mem;
|
2016-07-06 17:43:37 +00:00
|
|
|
|
2016-07-30 18:35:34 +00:00
|
|
|
if ((res = spa_node_port_pull_output (data->source, 1, info)) < 0)
|
2016-07-06 17:43:37 +00:00
|
|
|
printf ("got pull error %d\n", res);
|
|
|
|
|
2016-08-02 14:34:44 +00:00
|
|
|
b = data->bp[info->buffer_id];
|
2016-08-03 13:59:17 +00:00
|
|
|
metas = SPA_BUFFER_METAS (b);
|
|
|
|
datas = SPA_BUFFER_DATAS (b);
|
2016-07-07 09:31:52 +00:00
|
|
|
|
2016-08-03 13:59:17 +00:00
|
|
|
if (metas[1].type == SPA_META_TYPE_POINTER &&
|
|
|
|
strcmp (SPA_MEMBER (b, metas[1].offset, SpaMetaPointer)->ptr_type, "SDL_Texture") == 0) {
|
2016-07-11 15:45:54 +00:00
|
|
|
SDL_Texture *texture;
|
2016-08-03 13:59:17 +00:00
|
|
|
texture = SPA_MEMBER (b, metas[1].offset, SpaMetaPointer)->ptr;
|
2016-07-11 15:45:54 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2016-08-05 14:39:26 +00:00
|
|
|
mem = spa_memory_find (&datas[0].mem.mem);
|
2016-08-03 19:33:57 +00:00
|
|
|
mem->ptr = sdata;
|
|
|
|
mem->type = "sysmem";
|
|
|
|
mem->size = sstride * 240;
|
|
|
|
|
2016-08-05 14:39:26 +00:00
|
|
|
datas[0].mem.size = sstride * 240;
|
2016-08-03 13:59:17 +00:00
|
|
|
datas[0].stride = sstride;
|
2016-07-11 15:45:54 +00:00
|
|
|
} else {
|
|
|
|
if (SDL_LockTexture (data->texture, NULL, &ddata, &dstride) < 0) {
|
|
|
|
fprintf (stderr, "Couldn't lock texture: %s\n", SDL_GetError());
|
|
|
|
return;
|
|
|
|
}
|
2016-08-05 14:39:26 +00:00
|
|
|
mem = spa_memory_find (&datas[0].mem.mem);
|
2016-08-03 19:33:57 +00:00
|
|
|
|
|
|
|
sdata = spa_memory_ensure_ptr (mem);
|
2016-08-03 13:59:17 +00:00
|
|
|
sstride = datas[0].stride;
|
2016-07-11 15:45:54 +00:00
|
|
|
|
|
|
|
for (i = 0; i < 240; i++) {
|
|
|
|
src = ((uint8_t*)sdata + i * sstride);
|
|
|
|
dst = ((uint8_t*)ddata + i * dstride);
|
2016-07-30 18:35:34 +00:00
|
|
|
memcpy (dst, src, SPA_MIN (sstride, dstride));
|
2016-07-11 15:45:54 +00:00
|
|
|
}
|
|
|
|
SDL_UnlockTexture(data->texture);
|
|
|
|
|
|
|
|
SDL_RenderClear (data->renderer);
|
|
|
|
SDL_RenderCopy (data->renderer, data->texture, NULL, NULL);
|
|
|
|
SDL_RenderPresent (data->renderer);
|
2016-07-06 17:43:37 +00:00
|
|
|
}
|
2016-08-24 14:26:58 +00:00
|
|
|
spa_node_port_reuse_buffer (data->source, 0, info->buffer_id);
|
2016-07-06 17:43:37 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-07-07 14:59:23 +00:00
|
|
|
case SPA_EVENT_TYPE_ADD_POLL:
|
2016-07-08 10:18:01 +00:00
|
|
|
{
|
|
|
|
SpaPollItem *poll = event->data;
|
|
|
|
|
|
|
|
data->poll = *poll;
|
|
|
|
data->fds[0] = poll->fds[0];
|
|
|
|
data->n_fds = 1;
|
|
|
|
data->poll.fds = data->fds;
|
2016-07-07 14:59:23 +00:00
|
|
|
break;
|
2016-07-08 10:18:01 +00:00
|
|
|
}
|
2016-07-06 17:43:37 +00:00
|
|
|
default:
|
|
|
|
printf ("got event %d\n", event->type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static SpaResult
|
2016-07-07 15:33:41 +00:00
|
|
|
make_nodes (AppData *data, const char *device)
|
2016-07-06 17:43:37 +00:00
|
|
|
{
|
|
|
|
SpaResult res;
|
|
|
|
SpaProps *props;
|
|
|
|
SpaPropValue value;
|
|
|
|
|
2016-07-30 18:35:34 +00:00
|
|
|
if ((res = make_node (&data->source, "plugins/v4l2/libspa-v4l2.so", "v4l2-source")) < 0) {
|
2016-07-06 17:43:37 +00:00
|
|
|
printf ("can't create v4l2-source: %d\n", res);
|
|
|
|
return res;
|
|
|
|
}
|
2016-07-30 18:35:34 +00:00
|
|
|
spa_node_set_event_callback (data->source, on_source_event, data);
|
2016-07-06 17:43:37 +00:00
|
|
|
|
2016-07-30 18:35:34 +00:00
|
|
|
if ((res = spa_node_get_props (data->source, &props)) < 0)
|
2016-07-06 17:43:37 +00:00
|
|
|
printf ("got get_props error %d\n", res);
|
|
|
|
|
|
|
|
value.type = SPA_PROP_TYPE_STRING;
|
2016-07-07 15:33:41 +00:00
|
|
|
value.value = device ? device : "/dev/video0";
|
2016-07-06 17:43:37 +00:00
|
|
|
value.size = strlen (value.value)+1;
|
2016-08-03 20:11:03 +00:00
|
|
|
spa_props_set_prop (props, spa_props_index_for_name (props, "device"), &value);
|
2016-07-06 17:43:37 +00:00
|
|
|
|
2016-07-30 18:35:34 +00:00
|
|
|
if ((res = spa_node_set_props (data->source, props)) < 0)
|
2016-07-06 17:43:37 +00:00
|
|
|
printf ("got set_props error %d\n", res);
|
2016-08-03 20:11:03 +00:00
|
|
|
|
2016-07-06 17:43:37 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2016-07-08 18:12:56 +00:00
|
|
|
static void
|
|
|
|
alloc_buffers (AppData *data)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_BUFFERS; i++) {
|
2016-07-11 15:45:54 +00:00
|
|
|
SDLBuffer *b = &data->buffers[i];
|
|
|
|
SDL_Texture *texture;
|
2016-08-03 20:11:03 +00:00
|
|
|
SpaMemory *mem;
|
|
|
|
void *ptr;
|
2016-07-08 18:12:56 +00:00
|
|
|
int stride;
|
|
|
|
|
2016-07-11 15:45:54 +00:00
|
|
|
data->bp[i] = &b->buffer;
|
|
|
|
|
|
|
|
texture = SDL_CreateTexture (data->renderer,
|
|
|
|
SDL_PIXELFORMAT_YUY2,
|
|
|
|
SDL_TEXTUREACCESS_STREAMING,
|
|
|
|
320, 240);
|
|
|
|
if (!texture) {
|
2016-07-08 18:12:56 +00:00
|
|
|
printf ("can't create texture: %s\n", SDL_GetError ());
|
|
|
|
return;
|
|
|
|
}
|
2016-08-03 20:11:03 +00:00
|
|
|
if (SDL_LockTexture (texture, NULL, &ptr, &stride) < 0) {
|
2016-07-08 18:12:56 +00:00
|
|
|
fprintf (stderr, "Couldn't lock texture: %s\n", SDL_GetError());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-30 18:35:34 +00:00
|
|
|
b->buffer.id = i;
|
2016-08-05 14:39:26 +00:00
|
|
|
b->buffer.mem.mem.pool_id = SPA_ID_INVALID;
|
|
|
|
b->buffer.mem.mem.id = SPA_ID_INVALID;
|
|
|
|
b->buffer.mem.offset = 0;
|
|
|
|
b->buffer.mem.size = sizeof (SDLBuffer);
|
2016-07-11 15:45:54 +00:00
|
|
|
b->buffer.n_metas = 2;
|
2016-08-03 20:11:03 +00:00
|
|
|
b->buffer.metas = offsetof (SDLBuffer, metas);
|
2016-07-11 15:45:54 +00:00
|
|
|
b->buffer.n_datas = 1;
|
2016-08-03 20:11:03 +00:00
|
|
|
b->buffer.datas = offsetof (SDLBuffer, datas);
|
2016-07-11 15:45:54 +00:00
|
|
|
|
|
|
|
b->header.flags = 0;
|
|
|
|
b->header.seq = 0;
|
|
|
|
b->header.pts = 0;
|
|
|
|
b->header.dts_offset = 0;
|
|
|
|
b->metas[0].type = SPA_META_TYPE_HEADER;
|
2016-08-03 20:11:03 +00:00
|
|
|
b->metas[0].offset = offsetof (SDLBuffer, header);
|
2016-07-11 15:45:54 +00:00
|
|
|
b->metas[0].size = sizeof (b->header);
|
|
|
|
|
2016-07-15 16:22:29 +00:00
|
|
|
b->ptr.ptr_type = "SDL_Texture";
|
2016-07-11 15:45:54 +00:00
|
|
|
b->ptr.ptr = texture;
|
|
|
|
b->metas[1].type = SPA_META_TYPE_POINTER;
|
2016-08-03 20:11:03 +00:00
|
|
|
b->metas[1].offset = offsetof (SDLBuffer, ptr);
|
2016-07-11 15:45:54 +00:00
|
|
|
b->metas[1].size = sizeof (b->ptr);
|
|
|
|
|
2016-08-04 17:24:16 +00:00
|
|
|
mem = spa_memory_alloc (SPA_MEMORY_POOL_LOCAL);
|
2016-08-03 20:11:03 +00:00
|
|
|
mem->flags = SPA_MEMORY_FLAG_READWRITE;
|
|
|
|
mem->type = "sysmem";
|
|
|
|
mem->fd = -1;
|
|
|
|
mem->ptr = ptr;
|
|
|
|
mem->size = stride * 240;
|
|
|
|
|
2016-08-05 14:39:26 +00:00
|
|
|
b->datas[0].mem.mem = mem->mem;
|
|
|
|
b->datas[0].mem.offset = 0;
|
|
|
|
b->datas[0].mem.size = mem->size;
|
2016-07-11 15:45:54 +00:00
|
|
|
b->datas[0].stride = stride;
|
2016-07-08 18:12:56 +00:00
|
|
|
}
|
2016-07-30 18:35:34 +00:00
|
|
|
data->n_buffers = MAX_BUFFERS;
|
|
|
|
|
|
|
|
spa_node_port_use_buffers (data->source, 0, data->bp, MAX_BUFFERS);
|
2016-07-08 18:12:56 +00:00
|
|
|
}
|
|
|
|
|
2016-07-28 19:19:20 +00:00
|
|
|
typedef struct {
|
|
|
|
SpaFormat fmt;
|
|
|
|
SpaPropInfo infos[3];
|
|
|
|
SpaVideoFormat format;
|
|
|
|
SpaRectangle size;
|
|
|
|
SpaFraction framerate;
|
|
|
|
} VideoFormat;
|
|
|
|
|
2016-07-06 17:43:37 +00:00
|
|
|
static SpaResult
|
|
|
|
negotiate_formats (AppData *data)
|
|
|
|
{
|
|
|
|
SpaResult res;
|
2016-07-11 15:45:54 +00:00
|
|
|
const SpaPortInfo *info;
|
2016-07-28 19:19:20 +00:00
|
|
|
VideoFormat f;
|
|
|
|
|
|
|
|
#if 0
|
2016-07-15 16:22:29 +00:00
|
|
|
void *state = NULL;
|
2016-07-06 17:43:37 +00:00
|
|
|
|
2016-07-30 18:35:34 +00:00
|
|
|
if ((res = spa_node_port_enum_formats (data->source, 0, &format, NULL, &state)) < 0)
|
2016-07-06 17:43:37 +00:00
|
|
|
return res;
|
2016-07-28 19:19:20 +00:00
|
|
|
#else
|
|
|
|
f.fmt.media_type = SPA_MEDIA_TYPE_VIDEO;
|
|
|
|
f.fmt.media_subtype = SPA_MEDIA_SUBTYPE_RAW;
|
|
|
|
f.fmt.props.n_prop_info = 3;
|
|
|
|
f.fmt.props.prop_info = f.infos;
|
|
|
|
|
2016-08-26 15:43:48 +00:00
|
|
|
spa_prop_info_fill_video (&f.infos[0],
|
|
|
|
SPA_PROP_ID_VIDEO_FORMAT,
|
|
|
|
offsetof (VideoFormat, format));
|
2016-07-28 19:19:20 +00:00
|
|
|
f.format = SPA_VIDEO_FORMAT_YUY2;
|
|
|
|
|
2016-08-26 15:43:48 +00:00
|
|
|
spa_prop_info_fill_video (&f.infos[1],
|
|
|
|
SPA_PROP_ID_VIDEO_SIZE,
|
|
|
|
offsetof (VideoFormat, size));
|
2016-07-28 19:19:20 +00:00
|
|
|
f.size.width = 320;
|
|
|
|
f.size.height = 240;
|
|
|
|
|
2016-08-26 15:43:48 +00:00
|
|
|
spa_prop_info_fill_video (&f.infos[2],
|
|
|
|
SPA_PROP_ID_VIDEO_FRAMERATE,
|
|
|
|
offsetof (VideoFormat, framerate));
|
2016-07-28 19:19:20 +00:00
|
|
|
f.framerate.num = 25;
|
|
|
|
f.framerate.denom = 1;
|
|
|
|
#endif
|
2016-07-06 17:43:37 +00:00
|
|
|
|
2016-07-30 18:35:34 +00:00
|
|
|
if ((res = spa_node_port_set_format (data->source, 0, false, &f.fmt)) < 0)
|
2016-07-06 17:43:37 +00:00
|
|
|
return res;
|
|
|
|
|
2016-07-30 18:35:34 +00:00
|
|
|
if ((res = spa_node_port_get_info (data->source, 0, &info)) < 0)
|
2016-07-11 15:45:54 +00:00
|
|
|
return res;
|
|
|
|
|
2016-07-13 16:29:55 +00:00
|
|
|
spa_debug_port_info (info);
|
2016-07-11 15:45:54 +00:00
|
|
|
|
2016-08-03 20:11:03 +00:00
|
|
|
if (data->use_buffer) {
|
|
|
|
alloc_buffers (data);
|
|
|
|
} else {
|
2016-07-30 18:35:34 +00:00
|
|
|
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;
|
2016-07-08 18:12:56 +00:00
|
|
|
}
|
2016-07-06 17:43:37 +00:00
|
|
|
return SPA_RESULT_OK;
|
|
|
|
}
|
|
|
|
|
2016-07-07 14:59:23 +00:00
|
|
|
static void *
|
|
|
|
loop (void *user_data)
|
|
|
|
{
|
|
|
|
AppData *data = user_data;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
printf ("enter thread\n");
|
|
|
|
while (data->running) {
|
2016-07-08 10:18:01 +00:00
|
|
|
SpaPollNotifyData ndata;
|
|
|
|
|
|
|
|
r = poll ((struct pollfd *) data->fds, data->n_fds, -1);
|
2016-07-07 14:59:23 +00:00
|
|
|
if (r < 0) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (r == 0) {
|
|
|
|
fprintf (stderr, "select timeout\n");
|
|
|
|
break;
|
|
|
|
}
|
2016-07-08 10:18:01 +00:00
|
|
|
if (data->poll.after_cb) {
|
|
|
|
ndata.fds = data->poll.fds;
|
|
|
|
ndata.n_fds = data->poll.n_fds;
|
|
|
|
ndata.user_data = data->poll.user_data;
|
|
|
|
data->poll.after_cb (&ndata);
|
|
|
|
}
|
2016-07-07 14:59:23 +00:00
|
|
|
}
|
|
|
|
printf ("leave thread\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-07-06 17:43:37 +00:00
|
|
|
static void
|
|
|
|
run_async_source (AppData *data)
|
|
|
|
{
|
|
|
|
SpaResult res;
|
|
|
|
SpaCommand cmd;
|
2016-07-07 09:31:52 +00:00
|
|
|
bool done = false;
|
2016-07-07 14:59:23 +00:00
|
|
|
int err;
|
2016-07-06 17:43:37 +00:00
|
|
|
|
|
|
|
cmd.type = SPA_COMMAND_START;
|
2016-07-30 18:35:34 +00:00
|
|
|
if ((res = spa_node_send_command (data->source, &cmd)) < 0)
|
2016-07-06 17:43:37 +00:00
|
|
|
printf ("got error %d\n", res);
|
|
|
|
|
2016-07-07 14:59:23 +00:00
|
|
|
data->running = true;
|
|
|
|
if ((err = pthread_create (&data->thread, NULL, loop, data)) != 0) {
|
|
|
|
printf ("can't create thread: %d %s", err, strerror (err));
|
|
|
|
data->running = false;
|
|
|
|
}
|
2016-07-07 09:31:52 +00:00
|
|
|
|
|
|
|
while (!done) {
|
|
|
|
SDL_Event event;
|
|
|
|
|
|
|
|
while (SDL_PollEvent (&event)) {
|
|
|
|
switch (event.type) {
|
|
|
|
case SDL_KEYDOWN:
|
|
|
|
if (event.key.keysym.sym == SDLK_ESCAPE) {
|
|
|
|
done = SDL_TRUE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SDL_QUIT:
|
|
|
|
done = SDL_TRUE;
|
|
|
|
break;
|
|
|
|
}
|
2016-07-07 15:33:41 +00:00
|
|
|
sleep (1);
|
2016-07-07 09:31:52 +00:00
|
|
|
}
|
2016-07-07 14:59:23 +00:00
|
|
|
}
|
2016-07-07 09:31:52 +00:00
|
|
|
|
2016-07-07 14:59:23 +00:00
|
|
|
if (data->running) {
|
|
|
|
data->running = false;
|
|
|
|
pthread_join (data->thread, NULL);
|
2016-07-07 09:31:52 +00:00
|
|
|
}
|
2016-07-06 17:43:37 +00:00
|
|
|
|
2016-08-25 12:10:38 +00:00
|
|
|
cmd.type = SPA_COMMAND_PAUSE;
|
2016-07-30 18:35:34 +00:00
|
|
|
if ((res = spa_node_send_command (data->source, &cmd)) < 0)
|
2016-07-06 17:43:37 +00:00
|
|
|
printf ("got error %d\n", res);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
|
|
|
AppData data;
|
|
|
|
SpaResult res;
|
|
|
|
|
2016-08-03 19:33:57 +00:00
|
|
|
spa_memory_init ();
|
|
|
|
|
2016-08-03 20:11:03 +00:00
|
|
|
data.use_buffer = true;
|
|
|
|
|
2016-07-07 09:31:52 +00:00
|
|
|
if (SDL_Init (SDL_INIT_VIDEO) < 0) {
|
|
|
|
printf ("can't initialize SDL: %s\n", SDL_GetError ());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SDL_CreateWindowAndRenderer (320, 240, SDL_WINDOW_RESIZABLE, &data.window, &data.renderer)) {
|
|
|
|
printf ("can't create window: %s\n", SDL_GetError ());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-07 15:33:41 +00:00
|
|
|
if ((res = make_nodes (&data, argv[1])) < 0) {
|
2016-07-06 17:43:37 +00:00
|
|
|
printf ("can't make nodes: %d\n", res);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ((res = negotiate_formats (&data)) < 0) {
|
|
|
|
printf ("can't negotiate nodes: %d\n", res);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
run_async_source (&data);
|
|
|
|
|
2016-07-07 09:31:52 +00:00
|
|
|
SDL_DestroyRenderer (data.renderer);
|
|
|
|
|
2016-07-06 17:43:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|