mirror of
https://gitlab.freedesktop.org/pipewire/pipewire.git
synced 2025-10-31 22:25:38 -04:00
spa: Update tests and examples
Keep tests in spa/tests Make spa/examples Remove some old examples
This commit is contained in:
parent
fa2d877925
commit
3ee463ed3a
23 changed files with 266 additions and 5271 deletions
|
|
@ -1,18 +0,0 @@
|
|||
test_buffer = executable('test-buffer', 'test-buffer.c',
|
||||
include_directories : [spa_inc ],
|
||||
dependencies : [],
|
||||
install : false)
|
||||
test_utils = executable('test-utils', 'test-utils.c',
|
||||
include_directories : [spa_inc ],
|
||||
dependencies : [],
|
||||
install : false)
|
||||
if have_cpp
|
||||
test_cpp = executable('test-cpp', 'test-cpp.cpp',
|
||||
include_directories : [spa_inc ],
|
||||
dependencies : [],
|
||||
install : false)
|
||||
test('test-cpp', test_cpp)
|
||||
endif
|
||||
|
||||
test('test-buffer', test_buffer)
|
||||
test('test-utils', test_utils)
|
||||
|
|
@ -1,65 +1,26 @@
|
|||
subdir('auto')
|
||||
test_apps = [
|
||||
'test-buffer',
|
||||
'test-pod',
|
||||
'test-utils',
|
||||
'stress-ringbuffer',
|
||||
]
|
||||
|
||||
executable('test-mixer', 'test-mixer.c',
|
||||
include_directories : [spa_inc ],
|
||||
dependencies : [dl_lib, pthread_lib, mathlib],
|
||||
install : false)
|
||||
executable('test-convert', 'test-convert.c',
|
||||
include_directories : [spa_inc ],
|
||||
dependencies : [dl_lib, pthread_lib, mathlib],
|
||||
install : false)
|
||||
executable('test-convert2', 'test-convert2.c',
|
||||
include_directories : [spa_inc ],
|
||||
dependencies : [dl_lib, pthread_lib, mathlib],
|
||||
install : false)
|
||||
executable('test-bluez5', 'test-bluez5.c',
|
||||
include_directories : [spa_inc ],
|
||||
dependencies : [dl_lib, pthread_lib, mathlib, dbus_dep],
|
||||
install : false)
|
||||
executable('test-ringbuffer', 'test-ringbuffer.c',
|
||||
include_directories : [spa_inc ],
|
||||
dependencies : [dl_lib, pthread_lib],
|
||||
install : false)
|
||||
executable('test-graph', 'test-graph.c',
|
||||
include_directories : [spa_inc ],
|
||||
dependencies : [dl_lib, pthread_lib],
|
||||
install : false)
|
||||
executable('test-graph2', 'test-graph2.c',
|
||||
include_directories : [spa_inc ],
|
||||
dependencies : [dl_lib, pthread_lib],
|
||||
install : false)
|
||||
executable('test-perf', 'test-perf.c',
|
||||
include_directories : [spa_inc ],
|
||||
dependencies : [dl_lib, pthread_lib],
|
||||
install : false)
|
||||
executable('stress-ringbuffer', 'stress-ringbuffer.c',
|
||||
include_directories : [spa_inc ],
|
||||
c_args : [ '-D_GNU_SOURCE' ],
|
||||
dependencies : [dl_lib, pthread_lib],
|
||||
install : false)
|
||||
if sdl_dep.found()
|
||||
executable('test-v4l2', 'test-v4l2.c',
|
||||
include_directories : [spa_inc ],
|
||||
dependencies : [dl_lib, sdl_dep, pthread_lib],
|
||||
install : false)
|
||||
foreach a : test_apps
|
||||
test(a,
|
||||
executable(a, a + '.c',
|
||||
dependencies : [dl_lib, pthread_lib],
|
||||
include_directories : [spa_inc ],
|
||||
c_args : [ '-D_GNU_SOURCE' ],
|
||||
install : false),
|
||||
env : [
|
||||
'SPA_PLUGIN_DIR=@0@/spa/plugins/'.format(meson.build_root()),
|
||||
])
|
||||
endforeach
|
||||
|
||||
if have_cpp
|
||||
test_cpp = executable('test-cpp', 'test-cpp.cpp',
|
||||
include_directories : [spa_inc ],
|
||||
dependencies : [],
|
||||
install : false)
|
||||
test('test-cpp', test_cpp)
|
||||
endif
|
||||
executable('test-props', 'test-props.c',
|
||||
include_directories : [spa_inc ],
|
||||
dependencies : [],
|
||||
install : false)
|
||||
executable('test-props2', 'test-props2.c',
|
||||
include_directories : [spa_inc ],
|
||||
dependencies : [],
|
||||
install : false)
|
||||
#executable('test-props4', 'test-props4.c',
|
||||
# include_directories : [spa_inc ],
|
||||
# dependencies : [],
|
||||
# install : false)
|
||||
#executable('test-props5', 'test-props5.c',
|
||||
# include_directories : [spa_inc ],
|
||||
# dependencies : [],
|
||||
# install : false)
|
||||
executable('test-control', 'test-control.c',
|
||||
include_directories : [spa_inc ],
|
||||
dependencies : [dl_lib, pthread_lib, mathlib],
|
||||
install : false)
|
||||
|
|
|
|||
|
|
@ -5,11 +5,12 @@
|
|||
|
||||
#include <spa/utils/ringbuffer.h>
|
||||
|
||||
#define ARRAY_SIZE 64
|
||||
#define DEFAULT_SIZE 0x2000
|
||||
#define ARRAY_SIZE 63
|
||||
#define MAX_VALUE 0x10000
|
||||
|
||||
struct spa_ringbuffer rb;
|
||||
uint32_t size;
|
||||
int32_t size;
|
||||
uint8_t *data;
|
||||
|
||||
static int fill_int_array(int *array, int start, int count)
|
||||
|
|
@ -37,7 +38,6 @@ static int cmp_array(int *array1, int *array2, int count)
|
|||
static void *reader_start(void *arg)
|
||||
{
|
||||
int i = 0, a[ARRAY_SIZE], b[ARRAY_SIZE];
|
||||
unsigned long j = 0, nfailures = 0;
|
||||
|
||||
printf("reader started on cpu: %d\n", sched_getcpu());
|
||||
|
||||
|
|
@ -45,22 +45,16 @@ static void *reader_start(void *arg)
|
|||
|
||||
while (1) {
|
||||
uint32_t index;
|
||||
int32_t avail;
|
||||
|
||||
if (spa_ringbuffer_get_read_index(&rb, &index) >= (int32_t)(ARRAY_SIZE * sizeof(int))) {
|
||||
spa_ringbuffer_read_data(&rb, data, size, index & (size - 1), b,
|
||||
ARRAY_SIZE * sizeof(int));
|
||||
avail = spa_ringbuffer_get_read_index(&rb, &index);
|
||||
|
||||
if (!cmp_array(a, b, ARRAY_SIZE)) {
|
||||
nfailures++;
|
||||
printf
|
||||
("failure in chunk %lu - probability: %lu/%lu = %.3f per million\n",
|
||||
j, nfailures, j, (float) nfailures / (j + 1) * 1000000);
|
||||
i = (b[0] + ARRAY_SIZE) % MAX_VALUE;
|
||||
}
|
||||
if (avail >= (int32_t)(sizeof(b))) {
|
||||
spa_ringbuffer_read_data(&rb, data, size, index % size, b, sizeof(b));
|
||||
spa_ringbuffer_read_update(&rb, index + sizeof(b));
|
||||
|
||||
spa_assert(cmp_array(a, b, ARRAY_SIZE));
|
||||
i = fill_int_array(a, i, ARRAY_SIZE);
|
||||
j++;
|
||||
|
||||
spa_ringbuffer_read_update(&rb, index + ARRAY_SIZE * sizeof(int));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -76,11 +70,13 @@ static void *writer_start(void *arg)
|
|||
|
||||
while (1) {
|
||||
uint32_t index;
|
||||
int32_t avail;
|
||||
|
||||
if (spa_ringbuffer_get_write_index(&rb, &index) >= (int32_t)(ARRAY_SIZE * sizeof(int))) {
|
||||
spa_ringbuffer_write_data(&rb, data, size, index & (size - 1), a,
|
||||
ARRAY_SIZE * sizeof(int));
|
||||
spa_ringbuffer_write_update(&rb, index + ARRAY_SIZE * sizeof(int));
|
||||
avail = size - spa_ringbuffer_get_write_index(&rb, &index);
|
||||
|
||||
if (avail >= (int32_t)(sizeof(a))) {
|
||||
spa_ringbuffer_write_data(&rb, data, size, index % size, a, sizeof(a));
|
||||
spa_ringbuffer_write_update(&rb, index + sizeof(a));
|
||||
|
||||
i = fill_int_array(a, i, ARRAY_SIZE);
|
||||
}
|
||||
|
|
@ -93,7 +89,10 @@ int main(int argc, char *argv[])
|
|||
{
|
||||
printf("starting ringbuffer stress test\n");
|
||||
|
||||
sscanf(argv[1], "%d", &size);
|
||||
if (argc > 1)
|
||||
sscanf(argv[1], "%d", &size);
|
||||
else
|
||||
size = DEFAULT_SIZE;
|
||||
|
||||
printf("buffer size (bytes): %d\n", size);
|
||||
printf("array size (bytes): %ld\n", sizeof(int) * ARRAY_SIZE);
|
||||
|
|
@ -105,8 +104,9 @@ int main(int argc, char *argv[])
|
|||
pthread_create(&reader_thread, NULL, reader_start, NULL);
|
||||
pthread_create(&writer_thread, NULL, writer_start, NULL);
|
||||
|
||||
while (1)
|
||||
sleep(1);
|
||||
sleep(5);
|
||||
|
||||
printf("read %u, written %u\n", rb.readindex, rb.writeindex);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,264 +0,0 @@
|
|||
/* Spa
|
||||
*
|
||||
* Copyright © 2018 Wim Taymans
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice (including the next
|
||||
* paragraph) shall be included in all copies or substantial portions of the
|
||||
* Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <math.h>
|
||||
#include <error.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <dlfcn.h>
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
#include <poll.h>
|
||||
|
||||
#include <spa/support/loop.h>
|
||||
#include <spa/support/log.h>
|
||||
#include <spa/support/dbus.h>
|
||||
#include <spa/monitor/monitor.h>
|
||||
#include <spa/node/node.h>
|
||||
#include <spa/node/io.h>
|
||||
#include <spa/param/param.h>
|
||||
#include <spa/param/props.h>
|
||||
#include <spa/param/audio/format-utils.h>
|
||||
|
||||
#define M_PI_M2 ( M_PI + M_PI )
|
||||
|
||||
static struct spa_log *logger;
|
||||
|
||||
#define spa_debug(f,...) spa_log_trace(logger, f, __VA_ARGS__)
|
||||
|
||||
#include <spa/graph/graph.h>
|
||||
|
||||
#include <spa/debug/pod.h>
|
||||
#include <spa/debug/types.h>
|
||||
|
||||
struct buffer {
|
||||
struct spa_buffer buffer;
|
||||
struct spa_meta metas[1];
|
||||
struct spa_meta_header header;
|
||||
struct spa_data datas[1];
|
||||
struct spa_chunk chunks[1];
|
||||
};
|
||||
|
||||
struct data {
|
||||
struct spa_log *log;
|
||||
|
||||
struct spa_loop *loop;
|
||||
struct spa_loop_control *loop_control;
|
||||
struct spa_loop_utils *loop_utils;
|
||||
bool running;
|
||||
|
||||
struct spa_dbus *dbus;
|
||||
|
||||
struct spa_support support[7];
|
||||
uint32_t n_support;
|
||||
|
||||
struct spa_monitor *monitor;
|
||||
|
||||
struct spa_graph graph;
|
||||
struct spa_graph_state graph_state;
|
||||
struct spa_graph_node source_node;
|
||||
struct spa_graph_port source_out;
|
||||
struct spa_graph_port sink_in;
|
||||
struct spa_graph_node sink_node;
|
||||
|
||||
struct spa_node *sink;
|
||||
struct spa_node *source;
|
||||
|
||||
struct spa_io_buffers source_sink_io[1];
|
||||
struct spa_buffer *source_buffers[1];
|
||||
struct buffer source_buffer[1];
|
||||
};
|
||||
|
||||
static void inspect_item(struct data *data, struct spa_pod *item)
|
||||
{
|
||||
spa_debug_pod(0, NULL, item);
|
||||
}
|
||||
|
||||
static void monitor_event(void *_data, struct spa_event *event)
|
||||
{
|
||||
struct data *data = _data;
|
||||
|
||||
switch (SPA_MONITOR_EVENT_ID(event)) {
|
||||
case SPA_MONITOR_EVENT_Added:
|
||||
fprintf(stderr, "added:\n");
|
||||
inspect_item(data, SPA_POD_CONTENTS(struct spa_event, event));
|
||||
break;
|
||||
case SPA_MONITOR_EVENT_Removed:
|
||||
fprintf(stderr, "removed:\n");
|
||||
inspect_item(data, SPA_POD_CONTENTS(struct spa_event, event));
|
||||
break;
|
||||
case SPA_MONITOR_EVENT_Changed:
|
||||
fprintf(stderr, "changed:\n");
|
||||
inspect_item(data, SPA_POD_CONTENTS(struct spa_event, event));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static struct spa_monitor_callbacks monitor_callbacks = {
|
||||
SPA_VERSION_MONITOR_CALLBACKS,
|
||||
.event = monitor_event,
|
||||
};
|
||||
|
||||
static int get_handle(struct data *data,
|
||||
struct spa_handle **handle,
|
||||
const char *lib,
|
||||
const char *name)
|
||||
{
|
||||
int res;
|
||||
void *hnd;
|
||||
spa_handle_factory_enum_func_t enum_func;
|
||||
uint32_t i;
|
||||
|
||||
if ((hnd = dlopen(lib, RTLD_NOW)) == NULL) {
|
||||
printf("can't load %s: %s\n", lib, dlerror());
|
||||
return -errno;
|
||||
}
|
||||
if ((enum_func = dlsym(hnd, SPA_HANDLE_FACTORY_ENUM_FUNC_NAME)) == NULL) {
|
||||
printf("can't find enum function\n");
|
||||
return -errno;
|
||||
}
|
||||
|
||||
for (i = 0;;) {
|
||||
const struct spa_handle_factory *factory;
|
||||
|
||||
if ((res = enum_func(&factory, &i)) <= 0) {
|
||||
if (res != 0)
|
||||
printf("can't enumerate factories: %s\n", spa_strerror(res));
|
||||
break;
|
||||
}
|
||||
if (strcmp(factory->name, name))
|
||||
continue;
|
||||
|
||||
*handle = calloc(1, spa_handle_factory_get_size(factory, NULL));
|
||||
if ((res = spa_handle_factory_init(factory, *handle, NULL,
|
||||
data->support,
|
||||
data->n_support)) < 0) {
|
||||
printf("can't make factory instance: %d\n", res);
|
||||
free(*handle);
|
||||
return res;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
return -ENOENT;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct data data;
|
||||
int res;
|
||||
const char *str;
|
||||
struct spa_handle *handle;
|
||||
void *iface;
|
||||
|
||||
spa_zero(data);
|
||||
if ((res = get_handle(&data, &handle,
|
||||
"build/spa/plugins/support/libspa-support.so",
|
||||
"logger")) < 0) {
|
||||
error(-1, res, "can't create logger");
|
||||
}
|
||||
|
||||
if ((res = spa_handle_get_interface(handle,
|
||||
SPA_TYPE_INTERFACE_Log,
|
||||
&iface)) < 0)
|
||||
error(-1, res, "can't get log interface");
|
||||
|
||||
data.log = iface;
|
||||
data.support[0] = SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_Log, data.log);
|
||||
data.n_support = 1;
|
||||
|
||||
if ((str = getenv("SPA_DEBUG")))
|
||||
data.log->level = atoi(str);
|
||||
|
||||
if ((res = get_handle(&data, &handle,
|
||||
"build/spa/plugins/support/libspa-support.so",
|
||||
"loop")) < 0) {
|
||||
error(-1, res, "can't create loop");
|
||||
}
|
||||
if ((res = spa_handle_get_interface(handle,
|
||||
SPA_TYPE_INTERFACE_Loop,
|
||||
&iface)) < 0)
|
||||
error(-1, res, "can't get loop interface");
|
||||
data.loop = iface;
|
||||
|
||||
if ((res = spa_handle_get_interface(handle,
|
||||
SPA_TYPE_INTERFACE_LoopControl,
|
||||
&iface)) < 0)
|
||||
error(-1, res, "can't get loopcontrol interface");
|
||||
data.loop_control = iface;
|
||||
|
||||
if ((res = spa_handle_get_interface(handle,
|
||||
SPA_TYPE_INTERFACE_LoopUtils,
|
||||
&iface)) < 0)
|
||||
error(-1, res, "can't get looputils interface");
|
||||
data.loop_utils = iface;
|
||||
|
||||
data.support[1] = SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_DataLoop, data.loop);
|
||||
data.support[2] = SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_MainLoop, data.loop);
|
||||
data.support[3] = SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_LoopControl, data.loop_control);
|
||||
data.support[4] = SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_LoopUtils, data.loop_utils);
|
||||
data.n_support = 5;
|
||||
|
||||
if ((res = get_handle(&data, &handle,
|
||||
"build/spa/plugins/support/libspa-dbus.so",
|
||||
"dbus")) < 0) {
|
||||
error(-1, res, "can't create dbus");
|
||||
}
|
||||
|
||||
if ((res = spa_handle_get_interface(handle,
|
||||
SPA_TYPE_INTERFACE_DBus,
|
||||
&iface)) < 0)
|
||||
error(-1, res, "can't get dbus interface");
|
||||
|
||||
data.dbus = iface;
|
||||
data.support[5] = SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_DBus, data.dbus);
|
||||
data.n_support = 6;
|
||||
|
||||
if ((res = get_handle(&data, &handle,
|
||||
"build/spa/plugins/bluez5/libspa-bluez5.so",
|
||||
"bluez5-monitor")) < 0) {
|
||||
error(-1, res, "can't create bluez5-monitor");
|
||||
}
|
||||
|
||||
if ((res = spa_handle_get_interface(handle,
|
||||
SPA_TYPE_INTERFACE_Monitor,
|
||||
&iface)) < 0)
|
||||
error(-1, res, "can't get monitor interface");
|
||||
|
||||
data.monitor = iface;
|
||||
|
||||
spa_graph_init(&data.graph, &data.graph_state);
|
||||
|
||||
spa_monitor_set_callbacks(data.monitor, &monitor_callbacks, &data);
|
||||
|
||||
data.running = true;
|
||||
spa_loop_control_enter(data.loop_control);
|
||||
while (data.running) {
|
||||
spa_loop_control_iterate(data.loop_control, -1);
|
||||
}
|
||||
spa_loop_control_leave(data.loop_control);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
|
@ -1,541 +0,0 @@
|
|||
/* Spa
|
||||
*
|
||||
* Copyright © 2018 Wim Taymans
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice (including the next
|
||||
* paragraph) shall be included in all copies or substantial portions of the
|
||||
* Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <math.h>
|
||||
#include <error.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <dlfcn.h>
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
#include <poll.h>
|
||||
|
||||
#include <spa/support/log-impl.h>
|
||||
#include <spa/support/loop.h>
|
||||
#include <spa/node/node.h>
|
||||
#include <spa/node/io.h>
|
||||
#include <spa/param/param.h>
|
||||
#include <spa/param/props.h>
|
||||
#include <spa/param/audio/format-utils.h>
|
||||
#include <spa/control/control.h>
|
||||
|
||||
#define M_PI_M2 ( M_PI + M_PI )
|
||||
|
||||
static SPA_LOG_IMPL(default_log);
|
||||
|
||||
#define spa_debug(f,...) spa_log_trace(&default_log.log, f, __VA_ARGS__)
|
||||
|
||||
#include <spa/graph/graph.h>
|
||||
|
||||
#include <spa/debug/pod.h>
|
||||
|
||||
struct buffer {
|
||||
struct spa_buffer buffer;
|
||||
struct spa_meta metas[1];
|
||||
struct spa_meta_header header;
|
||||
struct spa_data datas[1];
|
||||
struct spa_chunk chunks[1];
|
||||
};
|
||||
|
||||
struct data {
|
||||
struct spa_log *log;
|
||||
struct spa_loop data_loop;
|
||||
|
||||
struct spa_support support[4];
|
||||
uint32_t n_support;
|
||||
|
||||
struct spa_graph graph;
|
||||
struct spa_graph_state graph_state;
|
||||
struct spa_graph_node source_node;
|
||||
struct spa_graph_state source_state;
|
||||
struct spa_graph_port source_out;
|
||||
struct spa_graph_port sink_in;
|
||||
struct spa_graph_node sink_node;
|
||||
struct spa_graph_state sink_state;
|
||||
|
||||
struct spa_node *sink;
|
||||
|
||||
struct spa_node *source;
|
||||
struct spa_io_buffers source_sink_io[1];
|
||||
struct spa_buffer *source_buffers[1];
|
||||
struct buffer source_buffer[1];
|
||||
|
||||
uint8_t ctrl[1024];
|
||||
double freq_accum;
|
||||
double volume_accum;
|
||||
|
||||
bool running;
|
||||
pthread_t thread;
|
||||
|
||||
struct spa_source sources[16];
|
||||
unsigned int n_sources;
|
||||
|
||||
bool rebuild_fds;
|
||||
struct pollfd fds[16];
|
||||
unsigned int n_fds;
|
||||
};
|
||||
|
||||
#define MIN_LATENCY 1024
|
||||
|
||||
#define BUFFER_SIZE MIN_LATENCY
|
||||
|
||||
static void
|
||||
init_buffer(struct data *data, struct spa_buffer **bufs, struct buffer *ba, int n_buffers,
|
||||
size_t size)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < n_buffers; i++) {
|
||||
struct buffer *b = &ba[i];
|
||||
bufs[i] = &b->buffer;
|
||||
|
||||
b->buffer.metas = b->metas;
|
||||
b->buffer.n_metas = 1;
|
||||
b->buffer.datas = b->datas;
|
||||
b->buffer.n_datas = 1;
|
||||
|
||||
b->header.flags = 0;
|
||||
b->header.seq = 0;
|
||||
b->header.pts = 0;
|
||||
b->header.dts_offset = 0;
|
||||
b->metas[0].type = SPA_META_Header;
|
||||
b->metas[0].data = &b->header;
|
||||
b->metas[0].size = sizeof(b->header);
|
||||
|
||||
b->datas[0].type = SPA_DATA_MemPtr;
|
||||
b->datas[0].flags = 0;
|
||||
b->datas[0].fd = -1;
|
||||
b->datas[0].mapoffset = 0;
|
||||
b->datas[0].maxsize = size;
|
||||
b->datas[0].data = malloc(size);
|
||||
b->datas[0].chunk = &b->chunks[0];
|
||||
b->datas[0].chunk->offset = 0;
|
||||
b->datas[0].chunk->size = 0;
|
||||
b->datas[0].chunk->stride = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static int make_node(struct data *data, struct spa_node **node, const char *lib, const char *name)
|
||||
{
|
||||
struct spa_handle *handle;
|
||||
int res;
|
||||
void *hnd;
|
||||
spa_handle_factory_enum_func_t enum_func;
|
||||
uint32_t i;
|
||||
|
||||
if ((hnd = dlopen(lib, RTLD_NOW)) == NULL) {
|
||||
printf("can't load %s: %s\n", lib, dlerror());
|
||||
return -errno;
|
||||
}
|
||||
if ((enum_func = dlsym(hnd, SPA_HANDLE_FACTORY_ENUM_FUNC_NAME)) == NULL) {
|
||||
printf("can't find enum function\n");
|
||||
return -errno;
|
||||
}
|
||||
|
||||
for (i = 0;;) {
|
||||
const struct spa_handle_factory *factory;
|
||||
void *iface;
|
||||
|
||||
if ((res = enum_func(&factory, &i)) <= 0) {
|
||||
if (res != 0)
|
||||
printf("can't enumerate factories: %s\n", spa_strerror(res));
|
||||
break;
|
||||
}
|
||||
if (strcmp(factory->name, name))
|
||||
continue;
|
||||
|
||||
handle = calloc(1, spa_handle_factory_get_size(factory, NULL));
|
||||
if ((res =
|
||||
spa_handle_factory_init(factory, handle, NULL, data->support,
|
||||
data->n_support)) < 0) {
|
||||
printf("can't make factory instance: %d\n", res);
|
||||
return res;
|
||||
}
|
||||
if ((res = spa_handle_get_interface(handle, SPA_TYPE_INTERFACE_Node, &iface)) < 0) {
|
||||
printf("can't get interface %d\n", res);
|
||||
return res;
|
||||
}
|
||||
*node = iface;
|
||||
return 0;
|
||||
}
|
||||
return -EBADF;
|
||||
}
|
||||
|
||||
static void on_sink_done(void *data, int seq, int res)
|
||||
{
|
||||
printf("got done %d %d\n", seq, res);
|
||||
}
|
||||
|
||||
static void on_sink_event(void *data, struct spa_event *event)
|
||||
{
|
||||
printf("got event %d\n", SPA_EVENT_TYPE(event));
|
||||
}
|
||||
|
||||
static void update_props(struct data *data)
|
||||
{
|
||||
struct spa_pod_builder b;
|
||||
struct spa_pod *pod;
|
||||
|
||||
spa_pod_builder_init(&b, data->ctrl, sizeof(data->ctrl));
|
||||
|
||||
#if 0
|
||||
spa_pod_builder_push_sequence(&b, 0);
|
||||
spa_pod_builder_control_header(&b, 0, SPA_CONTROL_Properties);
|
||||
spa_pod_builder_push_object(&b, SPA_TYPE_OBJECT_Props, 0);
|
||||
spa_pod_builder_push_prop(&b, SPA_PROP_frequency, 0);
|
||||
spa_pod_builder_float(&b, ((sin(data->freq_accum) + 1.0) * 200.0) + 440.0);
|
||||
spa_pod_builder_pop(&b);
|
||||
spa_pod_builder_push_prop(&b, SPA_PROP_volume, 0);
|
||||
spa_pod_builder_float(&b, (sin(data->volume_accum) / 2.0) + 0.5);
|
||||
spa_pod_builder_pop(&b);
|
||||
spa_pod_builder_pop(&b);
|
||||
pod = spa_pod_builder_pop(&b);
|
||||
#else
|
||||
spa_pod_builder_push_sequence(&b, 0);
|
||||
spa_pod_builder_control_header(&b, 0, SPA_CONTROL_Properties);
|
||||
spa_pod_builder_add_object(&b,
|
||||
SPA_TYPE_OBJECT_Props, 0,
|
||||
SPA_PROP_frequency, SPA_POD_Float(((sin(data->freq_accum) + 1.0) * 200.0) + 440.0),
|
||||
SPA_PROP_volume, SPA_POD_Float((sin(data->volume_accum) / 2.0) + 0.5));
|
||||
pod = spa_pod_builder_pop(&b);
|
||||
#endif
|
||||
|
||||
spa_debug_pod(0, NULL, pod);
|
||||
|
||||
data->freq_accum += M_PI_M2 / 880.0;
|
||||
if (data->freq_accum >= M_PI_M2)
|
||||
data->freq_accum -= M_PI_M2;
|
||||
|
||||
data->volume_accum += M_PI_M2 / 2000.0;
|
||||
if (data->volume_accum >= M_PI_M2)
|
||||
data->volume_accum -= M_PI_M2;
|
||||
}
|
||||
|
||||
static void on_sink_process(void *_data, int status)
|
||||
{
|
||||
struct data *data = _data;
|
||||
|
||||
update_props(data);
|
||||
|
||||
spa_graph_node_process(&data->source_node);
|
||||
spa_graph_node_process(&data->sink_node);
|
||||
}
|
||||
|
||||
static void
|
||||
on_sink_reuse_buffer(void *_data, uint32_t port_id, uint32_t buffer_id)
|
||||
{
|
||||
struct data *data = _data;
|
||||
|
||||
data->source_sink_io[0].buffer_id = buffer_id;
|
||||
}
|
||||
|
||||
static const struct spa_node_callbacks sink_callbacks = {
|
||||
SPA_VERSION_NODE_CALLBACKS,
|
||||
.done = on_sink_done,
|
||||
.event = on_sink_event,
|
||||
.process = on_sink_process,
|
||||
.reuse_buffer = on_sink_reuse_buffer
|
||||
};
|
||||
|
||||
static int do_add_source(struct spa_loop *loop, struct spa_source *source)
|
||||
{
|
||||
struct data *data = SPA_CONTAINER_OF(loop, struct data, data_loop);
|
||||
|
||||
data->sources[data->n_sources] = *source;
|
||||
data->n_sources++;
|
||||
data->rebuild_fds = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int do_update_source(struct spa_source *source)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void do_remove_source(struct spa_source *source)
|
||||
{
|
||||
}
|
||||
|
||||
static int
|
||||
do_invoke(struct spa_loop *loop,
|
||||
spa_invoke_func_t func, uint32_t seq, const void *data, size_t size, bool block, void *user_data)
|
||||
{
|
||||
return func(loop, false, seq, data, size, user_data);
|
||||
}
|
||||
|
||||
static int make_nodes(struct data *data, const char *device)
|
||||
{
|
||||
int res;
|
||||
struct spa_pod *props;
|
||||
struct spa_pod_builder b = { 0 };
|
||||
uint8_t buffer[512];
|
||||
//uint32_t idx;
|
||||
|
||||
if ((res = make_node(data, &data->sink,
|
||||
"build/spa/plugins/alsa/libspa-alsa.so", "alsa-sink")) < 0) {
|
||||
printf("can't create alsa-sink: %d\n", res);
|
||||
return res;
|
||||
}
|
||||
spa_node_set_callbacks(data->sink, &sink_callbacks, data);
|
||||
|
||||
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
||||
props = spa_pod_builder_add_object(&b,
|
||||
SPA_TYPE_OBJECT_Props, 0,
|
||||
SPA_PROP_device, SPA_POD_String(device ? device : "hw:0"),
|
||||
SPA_PROP_minLatency, SPA_POD_Int(MIN_LATENCY));
|
||||
|
||||
spa_debug_pod(0, NULL, props);
|
||||
|
||||
if ((res = spa_node_set_param(data->sink, SPA_PARAM_Props, 0, props)) < 0)
|
||||
printf("got set_props error %d\n", res);
|
||||
|
||||
if ((res = make_node(data, &data->source,
|
||||
"build/spa/plugins/audiotestsrc/libspa-audiotestsrc.so",
|
||||
"audiotestsrc")) < 0) {
|
||||
printf("can't create audiotestsrc: %d\n", res);
|
||||
return res;
|
||||
}
|
||||
|
||||
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
||||
props = spa_pod_builder_add_object(&b,
|
||||
SPA_TYPE_OBJECT_Props, 0,
|
||||
SPA_PROP_frequency, SPA_POD_Float(600.0),
|
||||
SPA_PROP_volume, SPA_POD_Float(0.5),
|
||||
SPA_PROP_live, SPA_POD_Bool(false));
|
||||
|
||||
if ((res = spa_node_set_param(data->source, SPA_PARAM_Props, 0, props)) < 0)
|
||||
printf("got set_props error %d\n", res);
|
||||
|
||||
if ((res = spa_node_port_set_io(data->source,
|
||||
SPA_DIRECTION_OUTPUT, 0,
|
||||
SPA_IO_Control,
|
||||
&data->ctrl, sizeof(data->ctrl))) < 0)
|
||||
error(0, -res, "set_io freq");
|
||||
|
||||
data->source_sink_io[0] = SPA_IO_BUFFERS_INIT;
|
||||
|
||||
spa_node_port_set_io(data->source,
|
||||
SPA_DIRECTION_OUTPUT, 0,
|
||||
SPA_IO_Buffers,
|
||||
&data->source_sink_io[0], sizeof(data->source_sink_io[0]));
|
||||
spa_node_port_set_io(data->sink,
|
||||
SPA_DIRECTION_INPUT, 0,
|
||||
SPA_IO_Buffers,
|
||||
&data->source_sink_io[0], sizeof(data->source_sink_io[0]));
|
||||
|
||||
spa_graph_node_init(&data->source_node, &data->source_state);
|
||||
spa_graph_node_set_callbacks(&data->source_node, &spa_graph_node_impl_default, data->source);
|
||||
spa_graph_node_add(&data->graph, &data->source_node);
|
||||
spa_graph_port_init(&data->source_out, SPA_DIRECTION_OUTPUT, 0, 0);
|
||||
spa_graph_port_add(&data->source_node, &data->source_out);
|
||||
|
||||
spa_graph_node_init(&data->sink_node, &data->sink_state);
|
||||
spa_graph_node_set_callbacks(&data->sink_node, &spa_graph_node_impl_default, data->sink);
|
||||
spa_graph_node_add(&data->graph, &data->sink_node);
|
||||
spa_graph_port_init(&data->sink_in, SPA_DIRECTION_INPUT, 0, 0);
|
||||
spa_graph_port_add(&data->sink_node, &data->sink_in);
|
||||
|
||||
spa_graph_port_link(&data->source_out, &data->sink_in);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static int negotiate_formats(struct data *data)
|
||||
{
|
||||
int res;
|
||||
struct spa_pod *format, *filter;
|
||||
uint32_t state = 0;
|
||||
struct spa_pod_builder b = { 0 };
|
||||
uint8_t buffer[4096];
|
||||
|
||||
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
||||
filter = spa_format_audio_raw_build(&b, 0,
|
||||
&SPA_AUDIO_INFO_RAW_INIT(
|
||||
.format = SPA_AUDIO_FORMAT_S16,
|
||||
.rate = 44100,
|
||||
.channels = 2 ));
|
||||
|
||||
spa_debug_pod(0, NULL, filter);
|
||||
|
||||
spa_log_debug(&default_log.log, "enum_params");
|
||||
if ((res = spa_node_port_enum_params(data->sink,
|
||||
SPA_DIRECTION_INPUT, 0,
|
||||
SPA_PARAM_EnumFormat, &state,
|
||||
filter, &format, &b)) <= 0)
|
||||
return -EBADF;
|
||||
|
||||
spa_debug_pod(0, NULL, format);
|
||||
|
||||
spa_log_debug(&default_log.log, "sink set_param");
|
||||
if ((res = spa_node_port_set_param(data->sink,
|
||||
SPA_DIRECTION_INPUT, 0,
|
||||
SPA_PARAM_Format, 0, format)) < 0)
|
||||
return res;
|
||||
|
||||
if ((res = spa_node_port_set_param(data->source,
|
||||
SPA_DIRECTION_OUTPUT, 0,
|
||||
SPA_PARAM_Format, 0, format)) < 0)
|
||||
return res;
|
||||
|
||||
init_buffer(data, data->source_buffers, data->source_buffer, 1, BUFFER_SIZE);
|
||||
if ((res =
|
||||
spa_node_port_use_buffers(data->sink, SPA_DIRECTION_INPUT, 0, data->source_buffers,
|
||||
1)) < 0)
|
||||
return res;
|
||||
if ((res =
|
||||
spa_node_port_use_buffers(data->source, SPA_DIRECTION_OUTPUT, 0, data->source_buffers,
|
||||
1)) < 0)
|
||||
return res;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void *loop(void *user_data)
|
||||
{
|
||||
struct data *data = user_data;
|
||||
|
||||
printf("enter thread %d\n", data->n_sources);
|
||||
while (data->running) {
|
||||
int r;
|
||||
unsigned int i;
|
||||
|
||||
/* rebuild */
|
||||
if (data->rebuild_fds) {
|
||||
for (i = 0; i < data->n_sources; i++) {
|
||||
struct spa_source *p = &data->sources[i];
|
||||
data->fds[i].fd = p->fd;
|
||||
data->fds[i].events = p->mask;
|
||||
}
|
||||
data->n_fds = data->n_sources;
|
||||
data->rebuild_fds = false;
|
||||
}
|
||||
|
||||
r = poll(data->fds, data->n_fds, -1);
|
||||
if (r < 0) {
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
break;
|
||||
}
|
||||
if (r == 0) {
|
||||
fprintf(stderr, "select timeout");
|
||||
break;
|
||||
}
|
||||
|
||||
/* after */
|
||||
for (i = 0; i < data->n_sources; i++) {
|
||||
struct spa_source *p = &data->sources[i];
|
||||
p->rmask = 0;
|
||||
if (data->fds[i].revents & POLLIN)
|
||||
p->rmask |= SPA_IO_IN;
|
||||
if (data->fds[i].revents & POLLOUT)
|
||||
p->rmask |= SPA_IO_OUT;
|
||||
if (data->fds[i].revents & POLLHUP)
|
||||
p->rmask |= SPA_IO_HUP;
|
||||
if (data->fds[i].revents & POLLERR)
|
||||
p->rmask |= SPA_IO_ERR;
|
||||
}
|
||||
for (i = 0; i < data->n_sources; i++) {
|
||||
struct spa_source *p = &data->sources[i];
|
||||
if (p->rmask)
|
||||
p->func(p);
|
||||
}
|
||||
}
|
||||
printf("leave thread\n");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void run_async_sink(struct data *data)
|
||||
{
|
||||
int res;
|
||||
int err;
|
||||
|
||||
{
|
||||
struct spa_command cmd = SPA_NODE_COMMAND_INIT(SPA_NODE_COMMAND_Start);
|
||||
if ((res = spa_node_send_command(data->source, &cmd)) < 0)
|
||||
printf("got source error %d\n", res);
|
||||
if ((res = spa_node_send_command(data->sink, &cmd)) < 0)
|
||||
printf("got sink error %d\n", res);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
printf("sleeping for 1000 seconds\n");
|
||||
sleep(1000);
|
||||
|
||||
if (data->running) {
|
||||
data->running = false;
|
||||
pthread_join(data->thread, NULL);
|
||||
}
|
||||
|
||||
{
|
||||
struct spa_command cmd = SPA_NODE_COMMAND_INIT(SPA_NODE_COMMAND_Pause);
|
||||
if ((res = spa_node_send_command(data->sink, &cmd)) < 0)
|
||||
printf("got error %d\n", res);
|
||||
if ((res = spa_node_send_command(data->source, &cmd)) < 0)
|
||||
printf("got source error %d\n", res);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct data data = { NULL };
|
||||
int res;
|
||||
const char *str;
|
||||
|
||||
spa_graph_init(&data.graph, &data.graph_state);
|
||||
|
||||
data.log = &default_log.log;
|
||||
data.data_loop.version = SPA_VERSION_LOOP;
|
||||
data.data_loop.add_source = do_add_source;
|
||||
data.data_loop.update_source = do_update_source;
|
||||
data.data_loop.remove_source = do_remove_source;
|
||||
data.data_loop.invoke = do_invoke;
|
||||
|
||||
if ((str = getenv("SPA_DEBUG")))
|
||||
data.log->level = atoi(str);
|
||||
|
||||
data.support[0] = SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_Log, data.log);
|
||||
data.support[1] = SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_MainLoop, &data.data_loop);
|
||||
data.support[2] = SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_DataLoop, &data.data_loop);
|
||||
data.n_support = 3;
|
||||
|
||||
if ((res = make_nodes(&data, argc > 1 ? argv[1] : NULL)) < 0) {
|
||||
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_sink(&data);
|
||||
}
|
||||
|
|
@ -1,371 +0,0 @@
|
|||
/* Spa
|
||||
*
|
||||
* Copyright © 2018 Wim Taymans
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice (including the next
|
||||
* paragraph) shall be included in all copies or substantial portions of the
|
||||
* Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <dlfcn.h>
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
#include <poll.h>
|
||||
|
||||
#include <spa/support/log-impl.h>
|
||||
#include <spa/support/loop.h>
|
||||
#include <spa/node/node.h>
|
||||
#include <spa/node/io.h>
|
||||
#include <spa/param/param.h>
|
||||
#include <spa/param/props.h>
|
||||
#include <spa/param/audio/format-utils.h>
|
||||
#include <spa/debug/pod.h>
|
||||
#include <spa/debug/mem.h>
|
||||
#include <spa/debug/types.h>
|
||||
|
||||
static SPA_LOG_IMPL(default_log);
|
||||
|
||||
struct buffer {
|
||||
struct spa_buffer buffer;
|
||||
struct spa_meta metas[1];
|
||||
struct spa_meta_header header;
|
||||
struct spa_data datas[8];
|
||||
struct spa_chunk chunks[8];
|
||||
};
|
||||
|
||||
struct data {
|
||||
struct spa_log *log;
|
||||
|
||||
struct spa_support support[4];
|
||||
uint32_t n_support;
|
||||
|
||||
struct spa_node *conv;
|
||||
struct spa_io_buffers io_in[1];
|
||||
struct spa_io_buffers io_out[1];
|
||||
|
||||
struct spa_buffer *in_buffers[1];
|
||||
struct buffer in_buffer[1];
|
||||
struct spa_buffer *out_buffers[1];
|
||||
struct buffer out_buffer[1];
|
||||
};
|
||||
|
||||
#define BUFFER_SIZE 128
|
||||
|
||||
static void
|
||||
init_buffer(struct data *data, struct spa_buffer **bufs, struct buffer *ba, int n_buffers,
|
||||
size_t size, int n_datas)
|
||||
{
|
||||
int i, j;
|
||||
void *d;
|
||||
|
||||
for (i = 0; i < n_buffers; i++) {
|
||||
struct buffer *b = &ba[i];
|
||||
bufs[i] = &b->buffer;
|
||||
|
||||
b->buffer.metas = b->metas;
|
||||
b->buffer.n_metas = 1;
|
||||
b->buffer.datas = b->datas;
|
||||
b->buffer.n_datas = n_datas;
|
||||
|
||||
b->header.flags = 0;
|
||||
b->header.seq = 0;
|
||||
b->header.pts = 0;
|
||||
b->header.dts_offset = 0;
|
||||
b->metas[0].type = SPA_META_Header;
|
||||
b->metas[0].data = &b->header;
|
||||
b->metas[0].size = sizeof(b->header);
|
||||
|
||||
d = malloc(size * n_datas);
|
||||
|
||||
for (j = 0; j < n_datas; j++) {
|
||||
b->datas[j].type = SPA_DATA_MemPtr;
|
||||
b->datas[j].flags = 0;
|
||||
b->datas[j].fd = -1;
|
||||
b->datas[j].mapoffset = 0;
|
||||
b->datas[j].maxsize = size;
|
||||
b->datas[j].data = SPA_MEMBER(d, size * j, void);
|
||||
b->datas[j].chunk = &b->chunks[0];
|
||||
b->datas[j].chunk->offset = 0;
|
||||
b->datas[j].chunk->size = 0;
|
||||
b->datas[j].chunk->stride = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int make_node(struct data *data, struct spa_node **node, const char *lib, const char *name)
|
||||
{
|
||||
struct spa_handle *handle;
|
||||
int res;
|
||||
void *hnd;
|
||||
spa_handle_factory_enum_func_t enum_func;
|
||||
uint32_t i;
|
||||
|
||||
if ((hnd = dlopen(lib, RTLD_NOW)) == NULL) {
|
||||
printf("can't load %s: %s\n", lib, dlerror());
|
||||
return -errno;
|
||||
}
|
||||
if ((enum_func = dlsym(hnd, SPA_HANDLE_FACTORY_ENUM_FUNC_NAME)) == NULL) {
|
||||
printf("can't find enum function\n");
|
||||
return -errno;
|
||||
}
|
||||
|
||||
for (i = 0;;) {
|
||||
const struct spa_handle_factory *factory;
|
||||
void *iface;
|
||||
|
||||
if ((res = enum_func(&factory, &i)) <= 0) {
|
||||
if (res != 0)
|
||||
printf("can't enumerate factories: %s\n", spa_strerror(res));
|
||||
break;
|
||||
}
|
||||
if (strcmp(factory->name, name))
|
||||
continue;
|
||||
|
||||
handle = calloc(1, spa_handle_factory_get_size(factory, NULL));
|
||||
if ((res =
|
||||
spa_handle_factory_init(factory, handle, NULL, data->support,
|
||||
data->n_support)) < 0) {
|
||||
printf("can't make factory instance: %d\n", res);
|
||||
return res;
|
||||
}
|
||||
if ((res = spa_handle_get_interface(handle, SPA_TYPE_INTERFACE_Node, &iface)) < 0) {
|
||||
printf("can't get interface %d\n", res);
|
||||
return res;
|
||||
}
|
||||
*node = iface;
|
||||
return 0;
|
||||
}
|
||||
return -EBADF;
|
||||
}
|
||||
|
||||
static void on_conv_done(void *data, int seq, int res)
|
||||
{
|
||||
printf("got done %d %d\n", seq, res);
|
||||
}
|
||||
|
||||
static void on_conv_event(void *data, struct spa_event *event)
|
||||
{
|
||||
printf("got event %d\n", SPA_EVENT_TYPE(event));
|
||||
}
|
||||
|
||||
static void on_conv_process(void *_data, int status)
|
||||
{
|
||||
printf("got process\n");
|
||||
}
|
||||
|
||||
static void
|
||||
on_conv_reuse_buffer(void *_data,
|
||||
uint32_t port_id,
|
||||
uint32_t buffer_id)
|
||||
{
|
||||
printf("got reuse buffer\n");
|
||||
}
|
||||
|
||||
static const struct spa_node_callbacks conv_callbacks = {
|
||||
SPA_VERSION_NODE_CALLBACKS,
|
||||
.done = on_conv_done,
|
||||
.event = on_conv_event,
|
||||
.process = on_conv_process,
|
||||
.reuse_buffer = on_conv_reuse_buffer
|
||||
};
|
||||
|
||||
static int make_nodes(struct data *data, const char *device)
|
||||
{
|
||||
int res;
|
||||
|
||||
if ((res = make_node(data, &data->conv,
|
||||
"build/spa/plugins/audioconvert/libspa-audioconvert.so", "fmtconvert")) < 0) {
|
||||
printf("can't create fmtconvert: %d\n", res);
|
||||
return res;
|
||||
}
|
||||
spa_node_set_callbacks(data->conv, &conv_callbacks, data);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static int negotiate_formats(struct data *data)
|
||||
{
|
||||
int res;
|
||||
struct spa_pod *format;
|
||||
struct spa_pod_builder b = { 0 };
|
||||
uint8_t buffer[4096];
|
||||
|
||||
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
||||
|
||||
format = spa_format_audio_raw_build(&b, 0,
|
||||
&SPA_AUDIO_INFO_RAW_INIT(
|
||||
.format = SPA_AUDIO_FORMAT_S16,
|
||||
.rate = 44100,
|
||||
.channels = 2 ));
|
||||
|
||||
if ((res = spa_node_port_set_param(data->conv,
|
||||
SPA_DIRECTION_INPUT, 0,
|
||||
SPA_PARAM_Format, 0,
|
||||
format)) < 0)
|
||||
return res;
|
||||
|
||||
data->io_in[0] = SPA_IO_BUFFERS_INIT;
|
||||
data->io_out[0] = SPA_IO_BUFFERS_INIT;
|
||||
|
||||
spa_node_port_set_io(data->conv,
|
||||
SPA_DIRECTION_INPUT, 0,
|
||||
SPA_IO_Buffers,
|
||||
&data->io_in[0], sizeof(data->io_in[0]));
|
||||
spa_node_port_set_io(data->conv,
|
||||
SPA_DIRECTION_OUTPUT, 0,
|
||||
SPA_IO_Buffers,
|
||||
&data->io_out[0], sizeof(data->io_out[0]));
|
||||
|
||||
|
||||
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
||||
format = spa_format_audio_raw_build(&b, 0,
|
||||
&SPA_AUDIO_INFO_RAW_INIT(
|
||||
.format = SPA_AUDIO_FORMAT_F32P,
|
||||
.rate = 44100,
|
||||
.channels = 2 ));
|
||||
|
||||
if ((res = spa_node_port_set_param(data->conv,
|
||||
SPA_DIRECTION_OUTPUT, 0,
|
||||
SPA_PARAM_Format, 0,
|
||||
format)) < 0)
|
||||
return res;
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int negotiate_buffers(struct data *data)
|
||||
{
|
||||
int res;
|
||||
uint32_t state;
|
||||
struct spa_pod *param;
|
||||
struct spa_pod_builder b = { 0 };
|
||||
uint8_t buffer[4096];
|
||||
|
||||
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
||||
state = 0;
|
||||
if ((res = spa_node_port_enum_params(data->conv,
|
||||
SPA_DIRECTION_INPUT, 0,
|
||||
SPA_PARAM_Buffers, &state,
|
||||
NULL, ¶m, &b)) <= 0)
|
||||
return -EBADF;
|
||||
|
||||
spa_debug_pod(0, NULL, param);
|
||||
|
||||
init_buffer(data, data->in_buffers, data->in_buffer, 1, BUFFER_SIZE, 1);
|
||||
if ((res =
|
||||
spa_node_port_use_buffers(data->conv, SPA_DIRECTION_INPUT, 0, data->in_buffers,
|
||||
1)) < 0)
|
||||
return res;
|
||||
|
||||
|
||||
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
||||
state = 0;
|
||||
if ((res = spa_node_port_enum_params(data->conv,
|
||||
SPA_DIRECTION_OUTPUT, 0,
|
||||
SPA_PARAM_Buffers, &state,
|
||||
NULL, ¶m, &b)) <= 0)
|
||||
return -EBADF;
|
||||
|
||||
spa_debug_pod(0, NULL, param);
|
||||
|
||||
init_buffer(data, data->out_buffers, data->out_buffer, 1, BUFFER_SIZE, 2);
|
||||
if ((res =
|
||||
spa_node_port_use_buffers(data->conv, SPA_DIRECTION_OUTPUT, 0, data->out_buffers,
|
||||
1)) < 0)
|
||||
return res;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void fill_buffer(struct data *data, struct spa_buffer *buffers[], int id)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < BUFFER_SIZE; i++) {
|
||||
*SPA_MEMBER(buffers[id]->datas[0].data, i, uint8_t) = i;
|
||||
}
|
||||
buffers[id]->datas[0].chunk->size = BUFFER_SIZE;
|
||||
}
|
||||
|
||||
static void run_convert(struct data *data)
|
||||
{
|
||||
int res;
|
||||
|
||||
{
|
||||
struct spa_command cmd = SPA_NODE_COMMAND_INIT(SPA_NODE_COMMAND_Start);
|
||||
if ((res = spa_node_send_command(data->conv, &cmd)) < 0)
|
||||
printf("got convert error %d\n", res);
|
||||
}
|
||||
|
||||
fill_buffer(data, data->in_buffers, 0);
|
||||
|
||||
data->io_in[0].status = SPA_STATUS_HAVE_BUFFER;
|
||||
data->io_in[0].buffer_id = 0;
|
||||
|
||||
data->io_out[0].status = SPA_STATUS_NEED_BUFFER;
|
||||
data->io_out[0].buffer_id = 0;
|
||||
|
||||
spa_debug_mem(0, data->in_buffers[0]->datas[0].data, BUFFER_SIZE);
|
||||
|
||||
res = spa_node_process(data->conv);
|
||||
printf("called process %d\n", res);
|
||||
|
||||
spa_debug_mem(0, data->out_buffers[0]->datas[0].data, BUFFER_SIZE);
|
||||
spa_debug_mem(0, data->out_buffers[0]->datas[1].data, BUFFER_SIZE);
|
||||
|
||||
{
|
||||
struct spa_command cmd = SPA_NODE_COMMAND_INIT(SPA_NODE_COMMAND_Pause);
|
||||
if ((res = spa_node_send_command(data->conv, &cmd)) < 0)
|
||||
printf("got convert error %d\n", res);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct data data = { NULL };
|
||||
int res;
|
||||
const char *str;
|
||||
|
||||
data.log = &default_log.log;
|
||||
|
||||
if ((str = getenv("SPA_DEBUG")))
|
||||
data.log->level = atoi(str);
|
||||
|
||||
data.support[0] = SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_Log, data.log);
|
||||
data.n_support = 1;
|
||||
|
||||
if ((res = make_nodes(&data, argc > 1 ? argv[1] : NULL)) < 0) {
|
||||
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;
|
||||
}
|
||||
if ((res = negotiate_buffers(&data)) < 0) {
|
||||
printf("can't negotiate buffers: %d\n", res);
|
||||
return -1;
|
||||
}
|
||||
|
||||
run_convert(&data);
|
||||
}
|
||||
|
|
@ -1,512 +0,0 @@
|
|||
/* Spa
|
||||
*
|
||||
* Copyright © 2018 Wim Taymans
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice (including the next
|
||||
* paragraph) shall be included in all copies or substantial portions of the
|
||||
* Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <dlfcn.h>
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
#include <poll.h>
|
||||
|
||||
#include <spa/support/log-impl.h>
|
||||
#include <spa/support/loop.h>
|
||||
#include <spa/node/node.h>
|
||||
#include <spa/node/io.h>
|
||||
#include <spa/buffer/alloc.h>
|
||||
#include <spa/param/param.h>
|
||||
#include <spa/param/props.h>
|
||||
#include <spa/param/audio/format-utils.h>
|
||||
#include <spa/debug/pod.h>
|
||||
#include <spa/debug/format.h>
|
||||
#include <spa/debug/mem.h>
|
||||
#include <spa/debug/types.h>
|
||||
|
||||
static SPA_LOG_IMPL(default_log);
|
||||
|
||||
struct buffer {
|
||||
struct spa_buffer buffer;
|
||||
struct spa_meta metas[1];
|
||||
struct spa_meta_header header;
|
||||
struct spa_data datas[8];
|
||||
struct spa_chunk chunks[8];
|
||||
};
|
||||
|
||||
struct node {
|
||||
struct spa_node *node;
|
||||
};
|
||||
|
||||
struct link {
|
||||
struct node *out_node;
|
||||
uint32_t out_port;
|
||||
const struct spa_port_info *out_info;
|
||||
struct node *in_node;
|
||||
uint32_t in_port;
|
||||
const struct spa_port_info *in_info;
|
||||
struct spa_io_buffers io;
|
||||
uint32_t n_buffers;
|
||||
struct spa_buffer **buffers;
|
||||
};
|
||||
|
||||
struct data {
|
||||
struct spa_log *log;
|
||||
|
||||
struct spa_support support[4];
|
||||
uint32_t n_support;
|
||||
|
||||
struct node nodes[4];
|
||||
struct link links[5];
|
||||
};
|
||||
|
||||
static int make_node(struct data *data, struct spa_node **node, const char *lib, const char *name)
|
||||
{
|
||||
struct spa_handle *handle;
|
||||
int res;
|
||||
void *hnd;
|
||||
spa_handle_factory_enum_func_t enum_func;
|
||||
uint32_t i;
|
||||
|
||||
if ((hnd = dlopen(lib, RTLD_NOW)) == NULL) {
|
||||
printf("can't load %s: %s\n", lib, dlerror());
|
||||
return -errno;
|
||||
}
|
||||
if ((enum_func = dlsym(hnd, SPA_HANDLE_FACTORY_ENUM_FUNC_NAME)) == NULL) {
|
||||
printf("can't find enum function\n");
|
||||
return -errno;
|
||||
}
|
||||
|
||||
for (i = 0;;) {
|
||||
const struct spa_handle_factory *factory;
|
||||
void *iface;
|
||||
|
||||
if ((res = enum_func(&factory, &i)) <= 0) {
|
||||
if (res != 0)
|
||||
printf("can't enumerate factories: %s\n", spa_strerror(res));
|
||||
break;
|
||||
}
|
||||
if (strcmp(factory->name, name))
|
||||
continue;
|
||||
|
||||
handle = calloc(1, spa_handle_factory_get_size(factory, NULL));
|
||||
if ((res =
|
||||
spa_handle_factory_init(factory, handle, NULL, data->support,
|
||||
data->n_support)) < 0) {
|
||||
printf("can't make factory instance: %d\n", res);
|
||||
return res;
|
||||
}
|
||||
if ((res = spa_handle_get_interface(handle, SPA_TYPE_INTERFACE_Node, &iface)) < 0) {
|
||||
printf("can't get interface %d\n", res);
|
||||
return res;
|
||||
}
|
||||
*node = iface;
|
||||
return 0;
|
||||
}
|
||||
return -EBADF;
|
||||
}
|
||||
|
||||
static int make_nodes(struct data *data, const char *device)
|
||||
{
|
||||
int res;
|
||||
|
||||
if ((res = make_node(data, &data->nodes[0].node,
|
||||
"build/spa/plugins/audioconvert/libspa-audioconvert.so",
|
||||
"fmtconvert")) < 0) {
|
||||
printf("can't create fmtconvert: %d\n", res);
|
||||
return res;
|
||||
}
|
||||
if ((res = make_node(data, &data->nodes[1].node,
|
||||
"build/spa/plugins/audioconvert/libspa-audioconvert.so",
|
||||
"channelmix")) < 0) {
|
||||
printf("can't create channelmix: %d\n", res);
|
||||
return res;
|
||||
}
|
||||
if ((res = make_node(data, &data->nodes[2].node,
|
||||
"build/spa/plugins/audioconvert/libspa-audioconvert.so",
|
||||
"resample")) < 0) {
|
||||
printf("can't create resample: %d\n", res);
|
||||
return res;
|
||||
}
|
||||
if ((res = make_node(data, &data->nodes[3].node,
|
||||
"build/spa/plugins/audioconvert/libspa-audioconvert.so",
|
||||
"fmtconvert")) < 0) {
|
||||
printf("can't create fmtconvert: %d\n", res);
|
||||
return res;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static int make_link(struct data *data, struct link *link,
|
||||
struct node *out_node, uint32_t out_port,
|
||||
struct node *in_node, uint32_t in_port)
|
||||
{
|
||||
link->out_node = out_node;
|
||||
link->out_port = out_port;
|
||||
link->in_node = in_node;
|
||||
link->in_port = in_port;
|
||||
link->io = SPA_IO_BUFFERS_INIT;
|
||||
link->n_buffers = 0;
|
||||
link->buffers = NULL;
|
||||
link->in_info = NULL;
|
||||
link->out_info = NULL;
|
||||
|
||||
if (out_node != NULL) {
|
||||
spa_node_port_get_info(out_node->node,
|
||||
SPA_DIRECTION_OUTPUT, out_port,
|
||||
&link->out_info);
|
||||
spa_node_port_set_io(out_node->node,
|
||||
SPA_DIRECTION_OUTPUT, out_port,
|
||||
SPA_IO_Buffers,
|
||||
&link->io, sizeof(link->io));
|
||||
}
|
||||
if (in_node != NULL) {
|
||||
spa_node_port_get_info(in_node->node,
|
||||
SPA_DIRECTION_INPUT, in_port,
|
||||
&link->in_info);
|
||||
spa_node_port_set_io(in_node->node,
|
||||
SPA_DIRECTION_INPUT, in_port,
|
||||
SPA_IO_Buffers,
|
||||
&link->io, sizeof(link->io));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int link_nodes(struct data *data)
|
||||
{
|
||||
make_link(data, &data->links[0], NULL, 0, &data->nodes[0], 0);
|
||||
make_link(data, &data->links[1], &data->nodes[0], 0, &data->nodes[1], 0);
|
||||
make_link(data, &data->links[2], &data->nodes[1], 0, &data->nodes[2], 0);
|
||||
make_link(data, &data->links[3], &data->nodes[2], 0, &data->nodes[3], 0);
|
||||
make_link(data, &data->links[4], &data->nodes[3], 0, NULL, 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int negotiate_link_format(struct data *data, struct link *link, struct spa_pod *filter)
|
||||
{
|
||||
struct spa_pod_builder b = { 0 };
|
||||
uint8_t buffer[4096];
|
||||
uint32_t state;
|
||||
struct spa_pod *format;
|
||||
int res;
|
||||
|
||||
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
||||
|
||||
if (link->out_node != NULL) {
|
||||
state = 0;
|
||||
if ((res = spa_node_port_enum_params(link->out_node->node,
|
||||
SPA_DIRECTION_OUTPUT, link->out_port,
|
||||
SPA_PARAM_EnumFormat, &state,
|
||||
filter, &format, &b)) <= 0)
|
||||
return -ENOTSUP;
|
||||
|
||||
filter = format;
|
||||
}
|
||||
if (link->in_node != NULL) {
|
||||
state = 0;
|
||||
if ((res = spa_node_port_enum_params(link->in_node->node,
|
||||
SPA_DIRECTION_INPUT, link->in_port,
|
||||
SPA_PARAM_EnumFormat, &state,
|
||||
filter, &format, &b)) <= 0)
|
||||
return -ENOTSUP;
|
||||
|
||||
filter = format;
|
||||
}
|
||||
|
||||
spa_pod_fixate(filter);
|
||||
spa_debug_format(0, NULL, filter);
|
||||
|
||||
if (link->out_node != NULL) {
|
||||
if ((res = spa_node_port_set_param(link->out_node->node,
|
||||
SPA_DIRECTION_OUTPUT, link->out_port,
|
||||
SPA_PARAM_Format, 0,
|
||||
filter)) < 0)
|
||||
return res;
|
||||
}
|
||||
if (link->in_node != NULL) {
|
||||
if ((res = spa_node_port_set_param(link->in_node->node,
|
||||
SPA_DIRECTION_INPUT, link->in_port,
|
||||
SPA_PARAM_Format, 0,
|
||||
filter)) < 0)
|
||||
return res;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int negotiate_formats(struct data *data)
|
||||
{
|
||||
int res;
|
||||
struct spa_pod *format;
|
||||
struct spa_pod_builder b = { 0 };
|
||||
uint8_t buffer[4096];
|
||||
|
||||
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
||||
format = spa_format_audio_raw_build(&b, 0,
|
||||
&SPA_AUDIO_INFO_RAW_INIT(
|
||||
.format = SPA_AUDIO_FORMAT_S16,
|
||||
.rate = 44100,
|
||||
.channels = 2,
|
||||
.position = { SPA_AUDIO_CHANNEL_FL, SPA_AUDIO_CHANNEL_FR, }));
|
||||
|
||||
if ((res = negotiate_link_format(data, &data->links[0], format)) < 0)
|
||||
return res;
|
||||
|
||||
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
||||
format = spa_format_audio_raw_build(&b, 0,
|
||||
&SPA_AUDIO_INFO_RAW_INIT(
|
||||
.format = SPA_AUDIO_FORMAT_S16P,
|
||||
.rate = 48000,
|
||||
.channels = 1,
|
||||
.position = { SPA_AUDIO_CHANNEL_MONO, }));
|
||||
|
||||
if ((res = negotiate_link_format(data, &data->links[4], format)) < 0)
|
||||
return res;
|
||||
|
||||
if ((res = negotiate_link_format(data, &data->links[3], NULL)) < 0)
|
||||
return res;
|
||||
|
||||
if ((res = negotiate_link_format(data, &data->links[1], NULL)) < 0)
|
||||
return res;
|
||||
|
||||
if ((res = negotiate_link_format(data, &data->links[2], NULL)) < 0)
|
||||
return res;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int negotiate_link_buffers(struct data *data, struct link *link)
|
||||
{
|
||||
uint8_t buffer[4096];
|
||||
struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buffer, sizeof(buffer));
|
||||
uint32_t state;
|
||||
struct spa_pod *param = NULL;
|
||||
int res, i;
|
||||
bool in_alloc, out_alloc;
|
||||
int32_t size, buffers, blocks, align, flags;
|
||||
uint32_t *aligns;
|
||||
struct spa_data *datas;
|
||||
|
||||
if (link->out_node != NULL) {
|
||||
state = 0;
|
||||
if ((res = spa_node_port_enum_params(link->out_node->node,
|
||||
SPA_DIRECTION_OUTPUT, link->out_port,
|
||||
SPA_PARAM_Buffers, &state,
|
||||
param, ¶m, &b)) <= 0)
|
||||
return -ENOTSUP;
|
||||
}
|
||||
if (link->in_node != NULL) {
|
||||
state = 0;
|
||||
if ((res = spa_node_port_enum_params(link->in_node->node,
|
||||
SPA_DIRECTION_INPUT, link->in_port,
|
||||
SPA_PARAM_Buffers, &state,
|
||||
param, ¶m, &b)) <= 0)
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
spa_pod_fixate(param);
|
||||
spa_debug_pod(0, NULL, param);
|
||||
|
||||
if (link->in_info)
|
||||
in_alloc = SPA_FLAG_CHECK(link->in_info->flags, SPA_PORT_INFO_FLAG_CAN_ALLOC_BUFFERS);
|
||||
else
|
||||
in_alloc = false;
|
||||
|
||||
if (link->out_info)
|
||||
out_alloc = SPA_FLAG_CHECK(link->out_info->flags, SPA_PORT_INFO_FLAG_CAN_ALLOC_BUFFERS);
|
||||
else
|
||||
out_alloc = false;
|
||||
|
||||
flags = 0;
|
||||
if (out_alloc || in_alloc) {
|
||||
flags |= SPA_BUFFER_ALLOC_FLAG_NO_DATA;
|
||||
if (out_alloc)
|
||||
in_alloc = false;
|
||||
}
|
||||
|
||||
if (spa_pod_parse_object(param,
|
||||
SPA_TYPE_OBJECT_ParamBuffers, NULL,
|
||||
SPA_PARAM_BUFFERS_buffers, SPA_POD_Int(&buffers),
|
||||
SPA_PARAM_BUFFERS_blocks, SPA_POD_Int(&blocks),
|
||||
SPA_PARAM_BUFFERS_size, SPA_POD_Int(&size),
|
||||
SPA_PARAM_BUFFERS_align, SPA_POD_Int(&align)) < 0)
|
||||
return -EINVAL;
|
||||
|
||||
datas = alloca(sizeof(struct spa_data) * blocks);
|
||||
aligns = alloca(sizeof(uint32_t) * blocks);
|
||||
for (i = 0; i < blocks; i++) {
|
||||
datas[i].type = SPA_DATA_MemPtr;
|
||||
datas[i].maxsize = size;
|
||||
aligns[i] = align;
|
||||
}
|
||||
|
||||
link->buffers = spa_buffer_alloc_array(buffers, flags, 0, NULL, blocks, datas, aligns);
|
||||
if (link->buffers == NULL)
|
||||
return -ENOMEM;
|
||||
|
||||
link->n_buffers = buffers;
|
||||
|
||||
if (link->out_node != NULL) {
|
||||
if (out_alloc) {
|
||||
if ((res = spa_node_port_alloc_buffers(link->out_node->node,
|
||||
SPA_DIRECTION_OUTPUT, link->out_port,
|
||||
NULL, 0,
|
||||
link->buffers, &link->n_buffers)) < 0)
|
||||
return res;
|
||||
}
|
||||
else {
|
||||
if ((res = spa_node_port_use_buffers(link->out_node->node,
|
||||
SPA_DIRECTION_OUTPUT, link->out_port,
|
||||
link->buffers, link->n_buffers)) < 0)
|
||||
return res;
|
||||
}
|
||||
}
|
||||
if (link->in_node != NULL) {
|
||||
if (in_alloc) {
|
||||
if ((res = spa_node_port_alloc_buffers(link->in_node->node,
|
||||
SPA_DIRECTION_INPUT, link->in_port,
|
||||
NULL, 0,
|
||||
link->buffers, &link->n_buffers)) < 0)
|
||||
return res;
|
||||
}
|
||||
else {
|
||||
if ((res = spa_node_port_use_buffers(link->in_node->node,
|
||||
SPA_DIRECTION_INPUT, link->in_port,
|
||||
link->buffers, link->n_buffers)) < 0)
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int negotiate_buffers(struct data *data)
|
||||
{
|
||||
int res;
|
||||
|
||||
if ((res = negotiate_link_buffers(data, &data->links[0])) < 0)
|
||||
return res;
|
||||
if ((res = negotiate_link_buffers(data, &data->links[1])) < 0)
|
||||
return res;
|
||||
if ((res = negotiate_link_buffers(data, &data->links[2])) < 0)
|
||||
return res;
|
||||
if ((res = negotiate_link_buffers(data, &data->links[3])) < 0)
|
||||
return res;
|
||||
if ((res = negotiate_link_buffers(data, &data->links[4])) < 0)
|
||||
return res;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void fill_buffer(struct data *data, struct spa_buffer *buffers[], int id)
|
||||
{
|
||||
uint32_t i;
|
||||
struct spa_buffer *b = buffers[id];
|
||||
|
||||
for (i = 0; i < b->datas[0].maxsize; i++) {
|
||||
*SPA_MEMBER(b->datas[0].data, i, uint8_t) = i;
|
||||
}
|
||||
b->datas[0].chunk->size = b->datas[0].maxsize;
|
||||
}
|
||||
|
||||
static void run_convert(struct data *data)
|
||||
{
|
||||
struct spa_buffer *b;
|
||||
int res;
|
||||
uint32_t i, j;
|
||||
|
||||
{
|
||||
struct spa_command cmd = SPA_NODE_COMMAND_INIT(SPA_NODE_COMMAND_Start);
|
||||
for (i = 0; i < 4; i++) {
|
||||
if ((res = spa_node_send_command(data->nodes[i].node, &cmd)) < 0)
|
||||
printf("got command error %d\n", res);
|
||||
}
|
||||
}
|
||||
|
||||
fill_buffer(data, data->links[0].buffers, 0);
|
||||
|
||||
for (i = 0; i < 5; i++) {
|
||||
data->links[i].io.status = SPA_STATUS_NEED_BUFFER;
|
||||
data->links[i].io.buffer_id = SPA_ID_INVALID;
|
||||
}
|
||||
|
||||
|
||||
b = data->links[0].buffers[0];
|
||||
for (i = 0; i < b->n_datas; i++)
|
||||
spa_debug_mem(0, b->datas[i].data, b->datas[i].maxsize);
|
||||
|
||||
for (j = 0; j < 2; j++) {
|
||||
data->links[0].io.status = SPA_STATUS_HAVE_BUFFER;
|
||||
data->links[0].io.buffer_id = 0;
|
||||
for (i = 0; i < 4; i++) {
|
||||
res = spa_node_process(data->nodes[i].node);
|
||||
printf("called process %d\n", res);
|
||||
}
|
||||
}
|
||||
|
||||
b = data->links[3].buffers[0];
|
||||
for (i = 0; i < b->n_datas; i++)
|
||||
spa_debug_mem(0, b->datas[i].data, b->datas[i].maxsize);
|
||||
|
||||
{
|
||||
struct spa_command cmd = SPA_NODE_COMMAND_INIT(SPA_NODE_COMMAND_Pause);
|
||||
for (i = 0; i < 4; i++) {
|
||||
if ((res = spa_node_send_command(data->nodes[i].node, &cmd)) < 0)
|
||||
printf("got command error %d\n", res);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct data data = { NULL };
|
||||
int res;
|
||||
const char *str;
|
||||
|
||||
data.log = &default_log.log;
|
||||
|
||||
if ((str = getenv("SPA_DEBUG")))
|
||||
data.log->level = atoi(str);
|
||||
|
||||
data.support[0] = SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_Log, data.log);
|
||||
data.n_support = 1;
|
||||
|
||||
if ((res = make_nodes(&data, argc > 1 ? argv[1] : NULL)) < 0) {
|
||||
printf("can't make nodes: %d\n", res);
|
||||
return -1;
|
||||
}
|
||||
if ((res = link_nodes(&data)) < 0) {
|
||||
printf("can't link nodes: %d\n", res);
|
||||
return -1;
|
||||
}
|
||||
if ((res = negotiate_formats(&data)) < 0) {
|
||||
printf("can't negotiate nodes: %d\n", res);
|
||||
return -1;
|
||||
}
|
||||
if ((res = negotiate_buffers(&data)) < 0) {
|
||||
printf("can't negotiate buffers: %d\n", res);
|
||||
return -1;
|
||||
}
|
||||
|
||||
run_convert(&data);
|
||||
}
|
||||
|
|
@ -1,540 +0,0 @@
|
|||
/* Spa
|
||||
*
|
||||
* Copyright © 2018 Wim Taymans
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice (including the next
|
||||
* paragraph) shall be included in all copies or substantial portions of the
|
||||
* Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <dlfcn.h>
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
#include <poll.h>
|
||||
|
||||
#include <spa/support/log-impl.h>
|
||||
#include <spa/support/loop.h>
|
||||
#include <spa/node/node.h>
|
||||
#include <spa/node/io.h>
|
||||
#include <spa/param/param.h>
|
||||
#include <spa/param/props.h>
|
||||
#include <spa/param/audio/format-utils.h>
|
||||
|
||||
static SPA_LOG_IMPL(default_log);
|
||||
|
||||
#define spa_debug(f,...) spa_log_trace(&default_log.log, f, __VA_ARGS__)
|
||||
|
||||
#include <spa/graph/graph.h>
|
||||
|
||||
#include <spa/debug/pod.h>
|
||||
#include <spa/debug/types.h>
|
||||
|
||||
struct buffer {
|
||||
struct spa_buffer buffer;
|
||||
struct spa_meta metas[1];
|
||||
struct spa_meta_header header;
|
||||
struct spa_data datas[1];
|
||||
struct spa_chunk chunks[1];
|
||||
};
|
||||
|
||||
struct data {
|
||||
struct spa_log *log;
|
||||
struct spa_loop data_loop;
|
||||
|
||||
struct spa_support support[4];
|
||||
uint32_t n_support;
|
||||
|
||||
struct spa_graph graph;
|
||||
struct spa_graph_state graph_state;
|
||||
struct spa_graph_node source_node;
|
||||
struct spa_graph_state source_state;
|
||||
struct spa_graph_port source_out;
|
||||
struct spa_graph_port volume_in;
|
||||
struct spa_graph_node volume_node;
|
||||
struct spa_graph_state volume_state;
|
||||
struct spa_graph_port volume_out;
|
||||
struct spa_graph_port sink_in;
|
||||
struct spa_graph_node sink_node;
|
||||
struct spa_graph_state sink_state;
|
||||
|
||||
struct spa_node *sink;
|
||||
struct spa_io_buffers volume_sink_io[1];
|
||||
|
||||
struct spa_node *volume;
|
||||
struct spa_buffer *volume_buffers[1];
|
||||
struct buffer volume_buffer[1];
|
||||
|
||||
struct spa_node *source;
|
||||
struct spa_io_buffers source_volume_io[1];
|
||||
struct spa_buffer *source_buffers[1];
|
||||
struct buffer source_buffer[1];
|
||||
|
||||
bool running;
|
||||
pthread_t thread;
|
||||
|
||||
struct spa_source sources[16];
|
||||
unsigned int n_sources;
|
||||
|
||||
bool rebuild_fds;
|
||||
struct pollfd fds[16];
|
||||
unsigned int n_fds;
|
||||
};
|
||||
|
||||
#define MIN_LATENCY 64
|
||||
|
||||
#define BUFFER_SIZE (MIN_LATENCY * sizeof(int16_t) * 2)
|
||||
|
||||
static void
|
||||
init_buffer(struct data *data, struct spa_buffer **bufs, struct buffer *ba, int n_buffers,
|
||||
size_t size)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < n_buffers; i++) {
|
||||
struct buffer *b = &ba[i];
|
||||
bufs[i] = &b->buffer;
|
||||
|
||||
b->buffer.metas = b->metas;
|
||||
b->buffer.n_metas = 1;
|
||||
b->buffer.datas = b->datas;
|
||||
b->buffer.n_datas = 1;
|
||||
|
||||
b->header.flags = 0;
|
||||
b->header.seq = 0;
|
||||
b->header.pts = 0;
|
||||
b->header.dts_offset = 0;
|
||||
b->metas[0].type = SPA_META_Header;
|
||||
b->metas[0].data = &b->header;
|
||||
b->metas[0].size = sizeof(b->header);
|
||||
|
||||
b->datas[0].type = SPA_DATA_MemPtr;
|
||||
b->datas[0].flags = 0;
|
||||
b->datas[0].fd = -1;
|
||||
b->datas[0].mapoffset = 0;
|
||||
b->datas[0].maxsize = size;
|
||||
b->datas[0].data = malloc(size);
|
||||
b->datas[0].chunk = &b->chunks[0];
|
||||
b->datas[0].chunk->offset = 0;
|
||||
b->datas[0].chunk->size = 0;
|
||||
b->datas[0].chunk->stride = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static int make_node(struct data *data, struct spa_node **node, const char *lib, const char *name)
|
||||
{
|
||||
struct spa_handle *handle;
|
||||
int res;
|
||||
void *hnd;
|
||||
spa_handle_factory_enum_func_t enum_func;
|
||||
uint32_t i;
|
||||
|
||||
if ((hnd = dlopen(lib, RTLD_NOW)) == NULL) {
|
||||
printf("can't load %s: %s\n", lib, dlerror());
|
||||
return -errno;
|
||||
}
|
||||
if ((enum_func = dlsym(hnd, SPA_HANDLE_FACTORY_ENUM_FUNC_NAME)) == NULL) {
|
||||
printf("can't find enum function\n");
|
||||
return -errno;
|
||||
}
|
||||
|
||||
for (i = 0;;) {
|
||||
const struct spa_handle_factory *factory;
|
||||
void *iface;
|
||||
|
||||
if ((res = enum_func(&factory, &i)) <= 0) {
|
||||
if (res != 0)
|
||||
printf("can't enumerate factories: %s\n", spa_strerror(res));
|
||||
break;
|
||||
}
|
||||
if (strcmp(factory->name, name))
|
||||
continue;
|
||||
|
||||
handle = calloc(1, spa_handle_factory_get_size(factory, NULL));
|
||||
if ((res =
|
||||
spa_handle_factory_init(factory, handle, NULL, data->support,
|
||||
data->n_support)) < 0) {
|
||||
printf("can't make factory instance: %d\n", res);
|
||||
return res;
|
||||
}
|
||||
if ((res = spa_handle_get_interface(handle, SPA_TYPE_INTERFACE_Node, &iface)) < 0) {
|
||||
printf("can't get interface %d\n", res);
|
||||
return res;
|
||||
}
|
||||
*node = iface;
|
||||
return 0;
|
||||
}
|
||||
return -EBADF;
|
||||
}
|
||||
|
||||
static void on_sink_done(void *data, int seq, int res)
|
||||
{
|
||||
printf("got done %d %d\n", seq, res);
|
||||
}
|
||||
|
||||
static void on_sink_event(void *data, struct spa_event *event)
|
||||
{
|
||||
printf("got event %d\n", SPA_EVENT_TYPE(event));
|
||||
}
|
||||
|
||||
static void on_sink_process(void *_data, int status)
|
||||
{
|
||||
struct data *data = _data;
|
||||
spa_graph_node_process(&data->sink_node);
|
||||
}
|
||||
|
||||
static void
|
||||
on_sink_reuse_buffer(void *_data, uint32_t port_id, uint32_t buffer_id)
|
||||
{
|
||||
struct data *data = _data;
|
||||
data->volume_sink_io[0].buffer_id = buffer_id;
|
||||
}
|
||||
|
||||
static const struct spa_node_callbacks sink_callbacks = {
|
||||
SPA_VERSION_NODE_CALLBACKS,
|
||||
.done = on_sink_done,
|
||||
.event = on_sink_event,
|
||||
.process = on_sink_process,
|
||||
.reuse_buffer = on_sink_reuse_buffer
|
||||
};
|
||||
|
||||
static int do_add_source(struct spa_loop *loop, struct spa_source *source)
|
||||
{
|
||||
struct data *data = SPA_CONTAINER_OF(loop, struct data, data_loop);
|
||||
|
||||
data->sources[data->n_sources] = *source;
|
||||
data->n_sources++;
|
||||
data->rebuild_fds = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int do_update_source(struct spa_source *source)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void do_remove_source(struct spa_source *source)
|
||||
{
|
||||
}
|
||||
|
||||
static int
|
||||
do_invoke(struct spa_loop *loop,
|
||||
spa_invoke_func_t func, uint32_t seq, const void *data, size_t size, bool block, void *user_data)
|
||||
{
|
||||
return func(loop, false, seq, data, size, user_data);
|
||||
}
|
||||
|
||||
static int make_nodes(struct data *data, const char *device)
|
||||
{
|
||||
int res;
|
||||
struct spa_pod *props;
|
||||
struct spa_pod_builder b = { 0 };
|
||||
uint8_t buffer[128];
|
||||
|
||||
if ((res = make_node(data, &data->sink,
|
||||
"build/spa/plugins/alsa/libspa-alsa.so", "alsa-sink")) < 0) {
|
||||
printf("can't create alsa-sink: %d\n", res);
|
||||
return res;
|
||||
}
|
||||
spa_node_set_callbacks(data->sink, &sink_callbacks, data);
|
||||
|
||||
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
||||
props = spa_pod_builder_add_object(&b,
|
||||
SPA_TYPE_OBJECT_Props, 0,
|
||||
SPA_PROP_device, SPA_POD_String(device ? device : "hw:0"),
|
||||
SPA_PROP_minLatency, SPA_POD_Int(MIN_LATENCY));
|
||||
|
||||
spa_debug_pod(0, NULL, props);
|
||||
|
||||
if ((res = spa_node_set_param(data->sink, SPA_PARAM_Props, 0, props)) < 0)
|
||||
printf("got set_props error %d\n", res);
|
||||
|
||||
if ((res = make_node(data, &data->volume,
|
||||
"build/spa/plugins/volume/libspa-volume.so", "volume")) < 0) {
|
||||
printf("can't create volume: %d\n", res);
|
||||
return res;
|
||||
}
|
||||
|
||||
if ((res = make_node(data, &data->source,
|
||||
"build/spa/plugins/audiotestsrc/libspa-audiotestsrc.so",
|
||||
"audiotestsrc")) < 0) {
|
||||
printf("can't create audiotestsrc: %d\n", res);
|
||||
return res;
|
||||
}
|
||||
|
||||
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
||||
props = spa_pod_builder_add_object(&b,
|
||||
SPA_TYPE_OBJECT_Props, 0,
|
||||
SPA_PROP_frequency, SPA_POD_Float(600.0),
|
||||
SPA_PROP_volume, SPA_POD_Float(0.5),
|
||||
SPA_PROP_live, SPA_POD_Bool(false));
|
||||
|
||||
if ((res = spa_node_set_param(data->source, SPA_PARAM_Props, 0, props)) < 0)
|
||||
printf("got set_props error %d\n", res);
|
||||
|
||||
data->source_volume_io[0] = SPA_IO_BUFFERS_INIT;
|
||||
data->volume_sink_io[0] = SPA_IO_BUFFERS_INIT;
|
||||
|
||||
spa_node_port_set_io(data->source,
|
||||
SPA_DIRECTION_OUTPUT, 0,
|
||||
SPA_IO_Buffers,
|
||||
&data->source_volume_io[0], sizeof(data->source_volume_io[0]));
|
||||
spa_node_port_set_io(data->volume,
|
||||
SPA_DIRECTION_INPUT, 0,
|
||||
SPA_IO_Buffers,
|
||||
&data->source_volume_io[0], sizeof(data->source_volume_io[0]));
|
||||
spa_node_port_set_io(data->volume,
|
||||
SPA_DIRECTION_OUTPUT, 0,
|
||||
SPA_IO_Buffers,
|
||||
&data->volume_sink_io[0], sizeof(data->volume_sink_io[0]));
|
||||
spa_node_port_set_io(data->sink,
|
||||
SPA_DIRECTION_INPUT, 0,
|
||||
SPA_IO_Buffers,
|
||||
&data->volume_sink_io[0], sizeof(data->volume_sink_io[0]));
|
||||
|
||||
spa_graph_node_init(&data->source_node, &data->source_state);
|
||||
spa_graph_node_set_callbacks(&data->source_node, &spa_graph_node_impl_default, data->source);
|
||||
spa_graph_node_add(&data->graph, &data->source_node);
|
||||
spa_graph_port_init(&data->source_out, SPA_DIRECTION_OUTPUT, 0, 0);
|
||||
spa_graph_port_add(&data->source_node, &data->source_out);
|
||||
|
||||
spa_graph_node_init(&data->volume_node, &data->volume_state);
|
||||
spa_graph_node_set_callbacks(&data->volume_node, &spa_graph_node_impl_default, data->volume);
|
||||
spa_graph_node_add(&data->graph, &data->volume_node);
|
||||
spa_graph_port_init(&data->volume_in, SPA_DIRECTION_INPUT, 0, 0);
|
||||
spa_graph_port_add(&data->volume_node, &data->volume_in);
|
||||
|
||||
spa_graph_port_link(&data->source_out, &data->volume_in);
|
||||
|
||||
spa_graph_port_init(&data->volume_out, SPA_DIRECTION_OUTPUT, 0, 0);
|
||||
spa_graph_port_add(&data->volume_node, &data->volume_out);
|
||||
|
||||
spa_graph_node_init(&data->sink_node, &data->sink_state);
|
||||
spa_graph_node_set_callbacks(&data->sink_node, &spa_graph_node_impl_default, data->sink);
|
||||
spa_graph_node_add(&data->graph, &data->sink_node);
|
||||
spa_graph_port_init(&data->sink_in, SPA_DIRECTION_INPUT, 0, 0);
|
||||
spa_graph_port_add(&data->sink_node, &data->sink_in);
|
||||
|
||||
spa_graph_port_link(&data->volume_out, &data->sink_in);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static int negotiate_formats(struct data *data)
|
||||
{
|
||||
int res;
|
||||
struct spa_pod *format, *filter;
|
||||
uint32_t state = 0;
|
||||
struct spa_pod_builder b = { 0 };
|
||||
uint8_t buffer[4096];
|
||||
|
||||
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
||||
filter = spa_format_audio_raw_build(&b, 0,
|
||||
&SPA_AUDIO_INFO_RAW_INIT(
|
||||
.format = SPA_AUDIO_FORMAT_S16,
|
||||
.rate = 44100,
|
||||
.channels = 2 ));
|
||||
|
||||
spa_debug_pod(0, NULL, filter);
|
||||
|
||||
spa_log_debug(&default_log.log, "enum_params");
|
||||
if ((res = spa_node_port_enum_params(data->sink,
|
||||
SPA_DIRECTION_INPUT, 0,
|
||||
SPA_PARAM_EnumFormat, &state,
|
||||
filter, &format, &b)) <= 0)
|
||||
return -EBADF;
|
||||
|
||||
spa_debug_pod(0, NULL, format);
|
||||
|
||||
spa_log_debug(&default_log.log, "sink set_param");
|
||||
if ((res = spa_node_port_set_param(data->sink,
|
||||
SPA_DIRECTION_INPUT, 0,
|
||||
SPA_PARAM_Format, 0, format)) < 0)
|
||||
return res;
|
||||
|
||||
spa_log_debug(&default_log.log, "volume set_param");
|
||||
if ((res = spa_node_port_set_param(data->volume,
|
||||
SPA_DIRECTION_OUTPUT, 0,
|
||||
SPA_PARAM_Format, 0, format)) < 0)
|
||||
return res;
|
||||
|
||||
init_buffer(data, data->volume_buffers, data->volume_buffer, 1, BUFFER_SIZE);
|
||||
if ((res =
|
||||
spa_node_port_use_buffers(data->sink, SPA_DIRECTION_INPUT, 0, data->volume_buffers,
|
||||
1)) < 0)
|
||||
return res;
|
||||
if ((res =
|
||||
spa_node_port_use_buffers(data->volume, SPA_DIRECTION_OUTPUT, 0, data->volume_buffers,
|
||||
1)) < 0)
|
||||
return res;
|
||||
|
||||
if ((res = spa_node_port_set_param(data->volume,
|
||||
SPA_DIRECTION_INPUT, 0,
|
||||
SPA_PARAM_Format, 0, format)) < 0)
|
||||
return res;
|
||||
if ((res = spa_node_port_set_param(data->source,
|
||||
SPA_DIRECTION_OUTPUT, 0,
|
||||
SPA_PARAM_Format, 0, format)) < 0)
|
||||
return res;
|
||||
|
||||
init_buffer(data, data->source_buffers, data->source_buffer, 1, BUFFER_SIZE);
|
||||
if ((res =
|
||||
spa_node_port_use_buffers(data->volume, SPA_DIRECTION_INPUT, 0, data->source_buffers,
|
||||
1)) < 0)
|
||||
return res;
|
||||
if ((res =
|
||||
spa_node_port_use_buffers(data->source, SPA_DIRECTION_OUTPUT, 0, data->source_buffers,
|
||||
1)) < 0)
|
||||
return res;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void *loop(void *user_data)
|
||||
{
|
||||
struct data *data = user_data;
|
||||
|
||||
printf("enter thread %d\n", data->n_sources);
|
||||
while (data->running) {
|
||||
int r;
|
||||
unsigned int i;
|
||||
|
||||
/* rebuild */
|
||||
if (data->rebuild_fds) {
|
||||
for (i = 0; i < data->n_sources; i++) {
|
||||
struct spa_source *p = &data->sources[i];
|
||||
data->fds[i].fd = p->fd;
|
||||
data->fds[i].events = p->mask;
|
||||
}
|
||||
data->n_fds = data->n_sources;
|
||||
data->rebuild_fds = false;
|
||||
}
|
||||
|
||||
r = poll(data->fds, data->n_fds, -1);
|
||||
if (r < 0) {
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
break;
|
||||
}
|
||||
if (r == 0) {
|
||||
fprintf(stderr, "select timeout");
|
||||
break;
|
||||
}
|
||||
|
||||
/* after */
|
||||
for (i = 0; i < data->n_sources; i++) {
|
||||
struct spa_source *p = &data->sources[i];
|
||||
p->rmask = 0;
|
||||
if (data->fds[i].revents & POLLIN)
|
||||
p->rmask |= SPA_IO_IN;
|
||||
if (data->fds[i].revents & POLLOUT)
|
||||
p->rmask |= SPA_IO_OUT;
|
||||
if (data->fds[i].revents & POLLHUP)
|
||||
p->rmask |= SPA_IO_HUP;
|
||||
if (data->fds[i].revents & POLLERR)
|
||||
p->rmask |= SPA_IO_ERR;
|
||||
}
|
||||
for (i = 0; i < data->n_sources; i++) {
|
||||
struct spa_source *p = &data->sources[i];
|
||||
if (p->rmask)
|
||||
p->func(p);
|
||||
}
|
||||
}
|
||||
printf("leave thread\n");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void run_async_sink(struct data *data)
|
||||
{
|
||||
int res;
|
||||
int err;
|
||||
|
||||
{
|
||||
struct spa_command cmd = SPA_NODE_COMMAND_INIT(SPA_NODE_COMMAND_Start);
|
||||
if ((res = spa_node_send_command(data->source, &cmd)) < 0)
|
||||
printf("got source error %d\n", res);
|
||||
if ((res = spa_node_send_command(data->volume, &cmd)) < 0)
|
||||
printf("got volume error %d\n", res);
|
||||
if ((res = spa_node_send_command(data->sink, &cmd)) < 0)
|
||||
printf("got sink error %d\n", res);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
printf("sleeping for 1000 seconds\n");
|
||||
sleep(1000);
|
||||
|
||||
if (data->running) {
|
||||
data->running = false;
|
||||
pthread_join(data->thread, NULL);
|
||||
}
|
||||
|
||||
{
|
||||
struct spa_command cmd = SPA_NODE_COMMAND_INIT(SPA_NODE_COMMAND_Pause);
|
||||
if ((res = spa_node_send_command(data->sink, &cmd)) < 0)
|
||||
printf("got error %d\n", res);
|
||||
if ((res = spa_node_send_command(data->volume, &cmd)) < 0)
|
||||
printf("got volume error %d\n", res);
|
||||
if ((res = spa_node_send_command(data->source, &cmd)) < 0)
|
||||
printf("got source error %d\n", res);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct data data = { NULL };
|
||||
int res;
|
||||
const char *str;
|
||||
|
||||
spa_graph_init(&data.graph, &data.graph_state);
|
||||
|
||||
data.log = &default_log.log;
|
||||
data.data_loop.version = SPA_VERSION_LOOP;
|
||||
data.data_loop.add_source = do_add_source;
|
||||
data.data_loop.update_source = do_update_source;
|
||||
data.data_loop.remove_source = do_remove_source;
|
||||
data.data_loop.invoke = do_invoke;
|
||||
|
||||
if ((str = getenv("SPA_DEBUG")))
|
||||
data.log->level = atoi(str);
|
||||
|
||||
data.support[0] = SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_Log, data.log);
|
||||
data.support[1] = SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_MainLoop, &data.data_loop);
|
||||
data.support[2] = SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_DataLoop, &data.data_loop);
|
||||
data.n_support = 3;
|
||||
|
||||
if ((res = make_nodes(&data, argc > 1 ? argv[1] : NULL)) < 0) {
|
||||
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_sink(&data);
|
||||
}
|
||||
|
|
@ -1,247 +0,0 @@
|
|||
/* Spa
|
||||
*
|
||||
* Copyright © 2018 Wim Taymans
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice (including the next
|
||||
* paragraph) shall be included in all copies or substantial portions of the
|
||||
* Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <dlfcn.h>
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
#include <poll.h>
|
||||
|
||||
#include <spa/support/log-impl.h>
|
||||
#include <spa/support/loop.h>
|
||||
#include <spa/node/node.h>
|
||||
#include <spa/param/param.h>
|
||||
#include <spa/param/props.h>
|
||||
#include <spa/param/audio/format-utils.h>
|
||||
#include <spa/graph/graph.h>
|
||||
|
||||
static SPA_LOG_IMPL(default_log);
|
||||
|
||||
struct version {
|
||||
uint16_t current;
|
||||
uint16_t pending;
|
||||
};
|
||||
|
||||
struct data {
|
||||
struct spa_log *log;
|
||||
struct spa_loop data_loop;
|
||||
|
||||
int writers;
|
||||
struct version version;
|
||||
struct spa_graph graph[2];
|
||||
struct spa_graph_state graph_state[2];
|
||||
|
||||
struct spa_graph_node source_node[2];
|
||||
struct spa_graph_port source_out[2];
|
||||
struct spa_graph_port volume_in[2];
|
||||
struct spa_graph_node volume_node[2];
|
||||
struct spa_graph_port volume_out[2];
|
||||
struct spa_graph_port sink_in[2];
|
||||
struct spa_graph_node sink_node[2];
|
||||
};
|
||||
|
||||
static int copy_graph(struct data *data, int current)
|
||||
{
|
||||
int c = (current)&1;
|
||||
int v = (current+1)&1;
|
||||
struct spa_graph *ng, *og;
|
||||
struct spa_graph_node *nn, *on;
|
||||
struct spa_graph_port *np, *op;
|
||||
int d;
|
||||
|
||||
d = (v - c);
|
||||
|
||||
og = &data->graph[c];
|
||||
ng = &data->graph[v];
|
||||
spa_list_init(&ng->nodes);
|
||||
|
||||
printf("copy graph %d -> %d\n", c, v);
|
||||
spa_list_for_each(on, &og->nodes, link) {
|
||||
nn = &on[d];
|
||||
*nn = *on;
|
||||
spa_list_append(&ng->nodes, &nn->link);
|
||||
|
||||
spa_list_init(&nn->ports[SPA_DIRECTION_INPUT]);
|
||||
spa_list_for_each(op, &on->ports[SPA_DIRECTION_INPUT], link) {
|
||||
np = &op[d];
|
||||
*np = *op;
|
||||
np->node = nn;
|
||||
np->peer = &op->peer[d];
|
||||
spa_list_append(&nn->ports[SPA_DIRECTION_INPUT], &np->link);
|
||||
}
|
||||
|
||||
spa_list_init(&nn->ports[SPA_DIRECTION_OUTPUT]);
|
||||
spa_list_for_each(op, &on->ports[SPA_DIRECTION_OUTPUT], link) {
|
||||
np = &op[d];
|
||||
*np = *op;
|
||||
np->node = nn;
|
||||
np->peer = &op->peer[d];
|
||||
spa_list_append(&nn->ports[SPA_DIRECTION_OUTPUT], &np->link);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int start_write(struct data *data)
|
||||
{
|
||||
if (data->writers++ == 0) {
|
||||
printf("writer start %d %d\n", data->version.current, data->version.pending);
|
||||
if (data->version.current == data->version.pending)
|
||||
copy_graph(data, data->version.current);
|
||||
data->version.pending = data->version.current;
|
||||
}
|
||||
return (data->version.current + 1) & 1;
|
||||
}
|
||||
|
||||
static int end_write(struct data *data)
|
||||
{
|
||||
if (--data->writers == 0) {
|
||||
data->version.pending++;
|
||||
printf("writer end %d %d\n", data->version.current, data->version.pending);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool switch_graph(struct data *data)
|
||||
{
|
||||
bool res = data->version.current != data->version.pending;
|
||||
if (res) {
|
||||
printf("switch graph %d -> %d\n", data->version.current, data->version.pending);
|
||||
data->version.current = data->version.pending;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
static int print_graph(struct data *data, int v)
|
||||
{
|
||||
struct spa_graph *g;
|
||||
struct spa_graph_node *n;
|
||||
struct spa_graph_port *p;
|
||||
|
||||
g = &data->graph[v];
|
||||
printf("graph %p (version %d):\n", g, v);
|
||||
|
||||
spa_list_for_each(n, &g->nodes, link) {
|
||||
printf(" node %p\n", n);
|
||||
spa_list_for_each(p, &n->ports[SPA_DIRECTION_INPUT], link) {
|
||||
printf(" in: %p -> %p\n", p, p->peer);
|
||||
}
|
||||
spa_list_for_each(p, &n->ports[SPA_DIRECTION_OUTPUT], link) {
|
||||
printf(" out: %p -> %p\n", p, p->peer);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int make_graph1(struct data *data)
|
||||
{
|
||||
int v = start_write(data);
|
||||
|
||||
spa_graph_node_init(&data->source_node[v], NULL);
|
||||
spa_graph_node_add(&data->graph[v], &data->source_node[v]);
|
||||
spa_graph_port_add(&data->source_node[v], &data->source_out[v]);
|
||||
|
||||
spa_graph_node_init(&data->volume_node[v], NULL);
|
||||
spa_graph_node_add(&data->graph[v], &data->volume_node[v]);
|
||||
spa_graph_port_add(&data->volume_node[v], &data->volume_in[v]);
|
||||
|
||||
spa_graph_port_link(&data->source_out[v], &data->volume_in[v]);
|
||||
|
||||
spa_graph_port_add(&data->volume_node[v], &data->volume_out[v]);
|
||||
|
||||
spa_graph_node_init(&data->sink_node[v], NULL);
|
||||
spa_graph_node_add(&data->graph[v], &data->sink_node[v]);
|
||||
spa_graph_port_add(&data->sink_node[v], &data->sink_in[v]);
|
||||
|
||||
spa_graph_port_link(&data->volume_out[v], &data->sink_in[v]);
|
||||
|
||||
end_write(data);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int make_graph2(struct data *data)
|
||||
{
|
||||
int v = start_write(data);
|
||||
|
||||
spa_graph_port_unlink(&data->volume_in[v]);
|
||||
spa_graph_port_unlink(&data->volume_out[v]);
|
||||
spa_graph_node_remove(&data->volume_node[v]);
|
||||
|
||||
spa_graph_port_link(&data->source_out[v], &data->sink_in[v]);
|
||||
|
||||
end_write(data);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int make_graph3(struct data *data)
|
||||
{
|
||||
int v = start_write(data);
|
||||
|
||||
spa_graph_port_unlink(&data->source_out[v]);
|
||||
|
||||
spa_graph_node_add(&data->graph[v], &data->volume_node[v]);
|
||||
|
||||
spa_graph_port_link(&data->source_out[v], &data->volume_in[v]);
|
||||
spa_graph_port_link(&data->volume_out[v], &data->sink_in[v]);
|
||||
|
||||
end_write(data);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct data data = { NULL };
|
||||
const char *str;
|
||||
|
||||
spa_graph_init(&data.graph[0], &data.graph_state[0]);
|
||||
spa_graph_init(&data.graph[1], &data.graph_state[1]);
|
||||
|
||||
data.log = &default_log.log;
|
||||
|
||||
if ((str = getenv("SPA_DEBUG")))
|
||||
data.log->level = atoi(str);
|
||||
|
||||
print_graph(&data, 0);
|
||||
print_graph(&data, 1);
|
||||
make_graph1(&data);
|
||||
print_graph(&data, 0);
|
||||
print_graph(&data, 1);
|
||||
switch_graph(&data);
|
||||
print_graph(&data, 0);
|
||||
print_graph(&data, 1);
|
||||
make_graph2(&data);
|
||||
print_graph(&data, 0);
|
||||
print_graph(&data, 1);
|
||||
switch_graph(&data);
|
||||
make_graph3(&data);
|
||||
print_graph(&data, 0);
|
||||
print_graph(&data, 1);
|
||||
|
||||
}
|
||||
|
|
@ -1,691 +0,0 @@
|
|||
/* Spa
|
||||
*
|
||||
* Copyright © 2018 Wim Taymans
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice (including the next
|
||||
* paragraph) shall be included in all copies or substantial portions of the
|
||||
* Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <math.h>
|
||||
#include <error.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <dlfcn.h>
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
#include <poll.h>
|
||||
|
||||
#include <spa/support/log.h>
|
||||
#include <spa/support/log-impl.h>
|
||||
#include <spa/support/loop.h>
|
||||
#include <spa/node/node.h>
|
||||
#include <spa/node/io.h>
|
||||
#include <spa/param/param.h>
|
||||
#include <spa/param/props.h>
|
||||
#include <spa/param/audio/format-utils.h>
|
||||
|
||||
#define USE_GRAPH
|
||||
|
||||
#define M_PI_M2 ( M_PI + M_PI )
|
||||
|
||||
static SPA_LOG_IMPL(default_log);
|
||||
|
||||
#define spa_debug(...) spa_log_trace(&default_log.log,__VA_ARGS__)
|
||||
|
||||
#include <spa/graph/graph.h>
|
||||
|
||||
struct buffer {
|
||||
struct spa_buffer buffer;
|
||||
struct spa_meta metas[1];
|
||||
struct spa_meta_header header;
|
||||
struct spa_data datas[1];
|
||||
struct spa_chunk chunks[1];
|
||||
};
|
||||
|
||||
struct data {
|
||||
struct spa_log *log;
|
||||
struct spa_loop data_loop;
|
||||
|
||||
struct spa_support support[4];
|
||||
uint32_t n_support;
|
||||
|
||||
struct spa_graph graph;
|
||||
struct spa_graph_state graph_state;
|
||||
struct spa_graph_node source1_node;
|
||||
struct spa_graph_state source1_state;
|
||||
struct spa_graph_port source1_out;
|
||||
struct spa_graph_node source2_node;
|
||||
struct spa_graph_state source2_state;
|
||||
struct spa_graph_port source2_out;
|
||||
struct spa_graph_port mix_in[2];
|
||||
struct spa_graph_node mix_node;
|
||||
struct spa_graph_state mix_state;
|
||||
struct spa_graph_port mix_out;
|
||||
struct spa_graph_port sink_in;
|
||||
struct spa_graph_node sink_node;
|
||||
struct spa_graph_state sink_state;
|
||||
|
||||
struct spa_node *sink;
|
||||
struct spa_io_buffers mix_sink_io[1];
|
||||
|
||||
struct spa_node *mix;
|
||||
uint32_t mix_ports[2];
|
||||
struct spa_buffer *mix_buffers[1];
|
||||
struct buffer mix_buffer[1];
|
||||
struct spa_pod_double ctrl_volume[2];
|
||||
double volume_accum;
|
||||
|
||||
struct spa_node *source1;
|
||||
struct spa_io_buffers source1_mix_io[1];
|
||||
struct spa_buffer *source1_buffers[2];
|
||||
struct buffer source1_buffer[2];
|
||||
|
||||
struct spa_node *source2;
|
||||
struct spa_io_buffers source2_mix_io[1];
|
||||
struct spa_buffer *source2_buffers[2];
|
||||
struct buffer source2_buffer[2];
|
||||
|
||||
bool running;
|
||||
pthread_t thread;
|
||||
|
||||
struct spa_source sources[16];
|
||||
unsigned int n_sources;
|
||||
|
||||
bool rebuild_fds;
|
||||
struct pollfd fds[16];
|
||||
unsigned int n_fds;
|
||||
};
|
||||
|
||||
#define MIN_LATENCY 512
|
||||
|
||||
#define BUFFER_SIZE1 MIN_LATENCY
|
||||
#define BUFFER_SIZE2 MIN_LATENCY - 4
|
||||
|
||||
static void
|
||||
init_buffer(struct data *data, struct spa_buffer **bufs, struct buffer *ba, int n_buffers,
|
||||
size_t size)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < n_buffers; i++) {
|
||||
struct buffer *b = &ba[i];
|
||||
bufs[i] = &b->buffer;
|
||||
|
||||
b->buffer.metas = b->metas;
|
||||
b->buffer.n_metas = 1;
|
||||
b->buffer.datas = b->datas;
|
||||
b->buffer.n_datas = 1;
|
||||
|
||||
b->header.flags = 0;
|
||||
b->header.seq = 0;
|
||||
b->header.pts = 0;
|
||||
b->header.dts_offset = 0;
|
||||
b->metas[0].type = SPA_META_Header;
|
||||
b->metas[0].data = &b->header;
|
||||
b->metas[0].size = sizeof(b->header);
|
||||
|
||||
b->datas[0].type = SPA_DATA_MemPtr;
|
||||
b->datas[0].flags = 0;
|
||||
b->datas[0].fd = -1;
|
||||
b->datas[0].mapoffset = 0;
|
||||
b->datas[0].maxsize = size;
|
||||
b->datas[0].data = malloc(size);
|
||||
b->datas[0].chunk = &b->chunks[0];
|
||||
b->datas[0].chunk->offset = 0;
|
||||
b->datas[0].chunk->size = 0;
|
||||
b->datas[0].chunk->stride = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static int make_node(struct data *data, struct spa_node **node, const char *lib, const char *name)
|
||||
{
|
||||
struct spa_handle *handle;
|
||||
int res;
|
||||
void *hnd;
|
||||
spa_handle_factory_enum_func_t enum_func;
|
||||
uint32_t i;
|
||||
|
||||
if ((hnd = dlopen(lib, RTLD_NOW)) == NULL) {
|
||||
printf("can't load %s: %s\n", lib, dlerror());
|
||||
return -errno;
|
||||
}
|
||||
if ((enum_func = dlsym(hnd, SPA_HANDLE_FACTORY_ENUM_FUNC_NAME)) == NULL) {
|
||||
printf("can't find enum function\n");
|
||||
return -errno;
|
||||
}
|
||||
|
||||
for (i = 0;;) {
|
||||
const struct spa_handle_factory *factory;
|
||||
void *iface;
|
||||
|
||||
if ((res = enum_func(&factory, &i)) <= 0) {
|
||||
if (res != 0)
|
||||
printf("can't enumerate factories: %s\n", spa_strerror(res));
|
||||
break;
|
||||
}
|
||||
if (strcmp(factory->name, name))
|
||||
continue;
|
||||
|
||||
handle = calloc(1, spa_handle_factory_get_size(factory, NULL));
|
||||
if ((res =
|
||||
spa_handle_factory_init(factory, handle, NULL, data->support,
|
||||
data->n_support)) < 0) {
|
||||
printf("can't make factory instance: %d\n", res);
|
||||
return res;
|
||||
}
|
||||
if ((res = spa_handle_get_interface(handle, SPA_TYPE_INTERFACE_Node, &iface)) < 0) {
|
||||
printf("can't get interface %d\n", res);
|
||||
return res;
|
||||
}
|
||||
*node = iface;
|
||||
return 0;
|
||||
}
|
||||
return -EBADF;
|
||||
}
|
||||
|
||||
static void on_sink_done(void *data, int seq, int res)
|
||||
{
|
||||
printf("got done %d %d\n", seq, res);
|
||||
}
|
||||
|
||||
static void on_sink_event(void *data, struct spa_event *event)
|
||||
{
|
||||
printf("got event %d\n", SPA_EVENT_TYPE(event));
|
||||
}
|
||||
|
||||
static void update_props(struct data *data)
|
||||
{
|
||||
data->ctrl_volume[0].value = ((sin(data->volume_accum) + 1.0) * 0.5);
|
||||
data->volume_accum += M_PI_M2 / 8800.0;
|
||||
if (data->volume_accum >= M_PI_M2)
|
||||
data->volume_accum -= M_PI_M2;
|
||||
|
||||
data->ctrl_volume[1].value = 1.0 - data->ctrl_volume[0].value;
|
||||
}
|
||||
|
||||
static void on_sink_process(void *_data, int status)
|
||||
{
|
||||
struct data *data = _data;
|
||||
|
||||
#ifdef USE_GRAPH
|
||||
spa_graph_node_process(&data->sink_node);
|
||||
#else
|
||||
int res;
|
||||
|
||||
res = spa_node_process_output(data->mix);
|
||||
if (res == SPA_STATUS_NEED_BUFFER) {
|
||||
if (data->source1_mix_io[0].status == SPA_STATUS_NEED_BUFFER) {
|
||||
res = spa_node_process_output(data->source1);
|
||||
if (res != SPA_STATUS_HAVE_BUFFER)
|
||||
printf("got process_output error from source1 %d\n", res);
|
||||
}
|
||||
|
||||
if (data->source2_mix_io[0].status == SPA_STATUS_NEED_BUFFER) {
|
||||
res = spa_node_process_output(data->source2);
|
||||
if (res != SPA_STATUS_HAVE_BUFFER)
|
||||
printf("got process_output error from source2 %d\n", res);
|
||||
}
|
||||
|
||||
res = spa_node_process_input(data->mix);
|
||||
if (res == SPA_STATUS_HAVE_BUFFER)
|
||||
goto push;
|
||||
else
|
||||
printf("got process_input error from mixer %d\n", res);
|
||||
|
||||
} else if (res == SPA_STATUS_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);
|
||||
}
|
||||
#endif
|
||||
update_props(data);
|
||||
}
|
||||
|
||||
static void
|
||||
on_sink_reuse_buffer(void *_data,
|
||||
uint32_t port_id,
|
||||
uint32_t buffer_id)
|
||||
{
|
||||
struct data *data = _data;
|
||||
data->mix_sink_io[0].buffer_id = buffer_id;
|
||||
}
|
||||
|
||||
static const struct spa_node_callbacks sink_callbacks = {
|
||||
SPA_VERSION_NODE_CALLBACKS,
|
||||
.done = on_sink_done,
|
||||
.event = on_sink_event,
|
||||
.process = &on_sink_process,
|
||||
.reuse_buffer = on_sink_reuse_buffer
|
||||
};
|
||||
|
||||
static int do_add_source(struct spa_loop *loop, struct spa_source *source)
|
||||
{
|
||||
struct data *data = SPA_CONTAINER_OF(loop, struct data, data_loop);
|
||||
|
||||
data->sources[data->n_sources] = *source;
|
||||
data->n_sources++;
|
||||
data->rebuild_fds = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int do_update_source(struct spa_source *source)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void do_remove_source(struct spa_source *source)
|
||||
{
|
||||
}
|
||||
|
||||
static int
|
||||
do_invoke(struct spa_loop *loop,
|
||||
spa_invoke_func_t func, uint32_t seq, const void *data, size_t size, bool block, void *user_data)
|
||||
{
|
||||
return func(loop, false, seq, data, size, user_data);
|
||||
}
|
||||
|
||||
static int make_nodes(struct data *data, const char *device)
|
||||
{
|
||||
int res;
|
||||
struct spa_pod *props;
|
||||
struct spa_pod_builder b = { 0 };
|
||||
uint8_t buffer[128];
|
||||
|
||||
if ((res = make_node(data, &data->sink,
|
||||
"build/spa/plugins/alsa/libspa-alsa.so", "alsa-sink")) < 0) {
|
||||
printf("can't create alsa-sink: %d\n", res);
|
||||
return res;
|
||||
}
|
||||
spa_node_set_callbacks(data->sink, &sink_callbacks, data);
|
||||
|
||||
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
||||
props = spa_pod_builder_add_object(&b,
|
||||
SPA_TYPE_OBJECT_Props, 0,
|
||||
SPA_PROP_device, SPA_POD_String(device ? device : "hw:0"),
|
||||
SPA_PROP_minLatency, SPA_POD_Int(MIN_LATENCY));
|
||||
|
||||
if ((res = spa_node_set_param(data->sink, SPA_PARAM_Props, 0, props)) < 0)
|
||||
error(0, -res, "set_param props");
|
||||
|
||||
if ((res = make_node(data, &data->mix,
|
||||
"build/spa/plugins/audiomixer/libspa-audiomixer.so",
|
||||
"audiomixer")) < 0) {
|
||||
printf("can't create audiomixer: %d\n", res);
|
||||
return res;
|
||||
}
|
||||
|
||||
if ((res = make_node(data, &data->source1,
|
||||
"build/spa/plugins/audiotestsrc/libspa-audiotestsrc.so",
|
||||
"audiotestsrc")) < 0) {
|
||||
printf("can't create audiotestsrc: %d\n", res);
|
||||
return res;
|
||||
}
|
||||
|
||||
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
||||
props = spa_pod_builder_add_object(&b,
|
||||
SPA_TYPE_OBJECT_Props, SPA_PARAM_Props,
|
||||
SPA_PROP_frequency, SPA_POD_Float(600.0),
|
||||
SPA_PROP_volume, SPA_POD_Float(1.0),
|
||||
SPA_PROP_live, SPA_POD_Bool(false));
|
||||
|
||||
if ((res = spa_node_set_param(data->source1, SPA_PARAM_Props, 0, props)) < 0)
|
||||
printf("got set_props error %d\n", res);
|
||||
|
||||
if ((res = make_node(data, &data->source2,
|
||||
"build/spa/plugins/audiotestsrc/libspa-audiotestsrc.so",
|
||||
"audiotestsrc")) < 0) {
|
||||
printf("can't create audiotestsrc: %d\n", res);
|
||||
return res;
|
||||
}
|
||||
|
||||
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
||||
props = spa_pod_builder_add_object(&b,
|
||||
SPA_TYPE_OBJECT_Props, SPA_PARAM_Props,
|
||||
SPA_PROP_frequency, SPA_POD_Float(440.0),
|
||||
SPA_PROP_volume, SPA_POD_Float(1.0),
|
||||
SPA_PROP_live, SPA_POD_Bool(false));
|
||||
|
||||
if ((res = spa_node_set_param(data->source2, SPA_PARAM_Props, 0, props)) < 0)
|
||||
printf("got set_props error %d\n", res);
|
||||
|
||||
data->mix_ports[0] = 0;
|
||||
if ((res = spa_node_add_port(data->mix, SPA_DIRECTION_INPUT, 0)) < 0)
|
||||
return res;
|
||||
|
||||
data->mix_ports[1] = 1;
|
||||
if ((res = spa_node_add_port(data->mix, SPA_DIRECTION_INPUT, 1)) < 0)
|
||||
return res;
|
||||
|
||||
data->source1_mix_io[0] = SPA_IO_BUFFERS_INIT;
|
||||
data->source2_mix_io[0] = SPA_IO_BUFFERS_INIT;
|
||||
data->mix_sink_io[0] = SPA_IO_BUFFERS_INIT;
|
||||
|
||||
spa_node_port_set_io(data->source1,
|
||||
SPA_DIRECTION_OUTPUT, 0,
|
||||
SPA_IO_Buffers,
|
||||
&data->source1_mix_io[0], sizeof(data->source1_mix_io[0]));
|
||||
spa_node_port_set_io(data->source2,
|
||||
SPA_DIRECTION_OUTPUT, 0,
|
||||
SPA_IO_Buffers,
|
||||
&data->source2_mix_io[0], sizeof(data->source2_mix_io[0]));
|
||||
spa_node_port_set_io(data->mix,
|
||||
SPA_DIRECTION_INPUT, data->mix_ports[0],
|
||||
SPA_IO_Buffers,
|
||||
&data->source1_mix_io[0], sizeof(data->source1_mix_io[0]));
|
||||
spa_node_port_set_io(data->mix,
|
||||
SPA_DIRECTION_INPUT, data->mix_ports[1],
|
||||
SPA_IO_Buffers,
|
||||
&data->source2_mix_io[0], sizeof(data->source2_mix_io[0]));
|
||||
spa_node_port_set_io(data->mix,
|
||||
SPA_DIRECTION_OUTPUT, 0,
|
||||
SPA_IO_Buffers,
|
||||
&data->mix_sink_io[0], sizeof(data->mix_sink_io[0]));
|
||||
spa_node_port_set_io(data->sink,
|
||||
SPA_DIRECTION_INPUT, 0,
|
||||
SPA_IO_Buffers,
|
||||
&data->mix_sink_io[0], sizeof(data->mix_sink_io[0]));
|
||||
|
||||
data->ctrl_volume[0] = SPA_POD_INIT_Double(0.5);
|
||||
data->ctrl_volume[1] = SPA_POD_INIT_Double(0.5);
|
||||
|
||||
#if 0
|
||||
if ((res = spa_node_port_set_io(data->mix,
|
||||
SPA_DIRECTION_INPUT, data->mix_ports[0],
|
||||
data->type.io_inprop_volume,
|
||||
&data->ctrl_volume[0], sizeof(data->ctrl_volume[0]))) < 0)
|
||||
error(0, -res, "set_io volume 0");
|
||||
|
||||
if ((res = spa_node_port_set_io(data->mix,
|
||||
SPA_DIRECTION_INPUT, data->mix_ports[1],
|
||||
data->type.io_inprop_volume,
|
||||
&data->ctrl_volume[1], sizeof(data->ctrl_volume[1]))) < 0)
|
||||
error(0, -res, "set_io volume 1");
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef USE_GRAPH
|
||||
spa_graph_node_init(&data->source1_node, &data->source1_state);
|
||||
spa_graph_node_set_callbacks(&data->source1_node, &spa_graph_node_impl_default, data->source1);
|
||||
spa_graph_port_init(&data->source1_out, SPA_DIRECTION_OUTPUT, 0, 0);
|
||||
spa_graph_port_add(&data->source1_node, &data->source1_out);
|
||||
spa_graph_node_add(&data->graph, &data->source1_node);
|
||||
|
||||
spa_graph_node_init(&data->source2_node, &data->source2_state);
|
||||
spa_graph_node_set_callbacks(&data->source2_node, &spa_graph_node_impl_default, data->source2);
|
||||
spa_graph_port_init(&data->source2_out, SPA_DIRECTION_OUTPUT, 0, 0);
|
||||
spa_graph_port_add(&data->source2_node, &data->source2_out);
|
||||
spa_graph_node_add(&data->graph, &data->source2_node);
|
||||
|
||||
spa_graph_node_init(&data->mix_node, &data->mix_state);
|
||||
spa_graph_node_set_callbacks(&data->mix_node, &spa_graph_node_impl_default, data->mix);
|
||||
spa_graph_port_init(&data->mix_in[0], SPA_DIRECTION_INPUT,
|
||||
data->mix_ports[0], 0);
|
||||
spa_graph_port_add(&data->mix_node, &data->mix_in[0]);
|
||||
spa_graph_port_init(&data->mix_in[1], SPA_DIRECTION_INPUT,
|
||||
data->mix_ports[1], 0);
|
||||
spa_graph_port_add(&data->mix_node, &data->mix_in[1]);
|
||||
spa_graph_node_add(&data->graph, &data->mix_node);
|
||||
|
||||
spa_graph_port_link(&data->source1_out, &data->mix_in[0]);
|
||||
spa_graph_port_link(&data->source2_out, &data->mix_in[1]);
|
||||
|
||||
spa_graph_port_init(&data->mix_out, SPA_DIRECTION_OUTPUT, 0, 0);
|
||||
spa_graph_port_add(&data->mix_node, &data->mix_out);
|
||||
|
||||
spa_graph_node_init(&data->sink_node, &data->sink_state);
|
||||
spa_graph_node_set_callbacks(&data->sink_node, &spa_graph_node_impl_default, data->sink);
|
||||
spa_graph_port_init(&data->sink_in, SPA_DIRECTION_INPUT, 0, 0);
|
||||
spa_graph_port_add(&data->sink_node, &data->sink_in);
|
||||
spa_graph_node_add(&data->graph, &data->sink_node);
|
||||
|
||||
spa_graph_port_link(&data->mix_out, &data->sink_in);
|
||||
#endif
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static int negotiate_formats(struct data *data)
|
||||
{
|
||||
int res;
|
||||
struct spa_pod *format, *filter;
|
||||
uint32_t state = 0;
|
||||
struct spa_pod_builder b = { 0 };
|
||||
uint8_t buffer[2048];
|
||||
|
||||
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
||||
filter = spa_format_audio_raw_build(&b, 0,
|
||||
&SPA_AUDIO_INFO_RAW_INIT(
|
||||
.format = SPA_AUDIO_FORMAT_S16,
|
||||
.rate = 44100,
|
||||
.channels = 2 ));
|
||||
|
||||
if ((res =
|
||||
spa_node_port_enum_params(data->sink,
|
||||
SPA_DIRECTION_INPUT, 0,
|
||||
SPA_PARAM_EnumFormat, &state,
|
||||
filter, &format, &b)) <= 0)
|
||||
return -EBADF;
|
||||
|
||||
if ((res = spa_node_port_set_param(data->sink,
|
||||
SPA_DIRECTION_INPUT, 0,
|
||||
SPA_PARAM_Format, 0,
|
||||
format)) < 0)
|
||||
return res;
|
||||
|
||||
if ((res = spa_node_port_set_param(data->mix,
|
||||
SPA_DIRECTION_OUTPUT, 0,
|
||||
SPA_PARAM_Format, 0,
|
||||
format)) < 0)
|
||||
return res;
|
||||
|
||||
init_buffer(data, data->mix_buffers, data->mix_buffer, 1, BUFFER_SIZE2);
|
||||
if ((res =
|
||||
spa_node_port_use_buffers(data->sink, SPA_DIRECTION_INPUT, 0, data->mix_buffers,
|
||||
1)) < 0)
|
||||
return res;
|
||||
if ((res = spa_node_port_use_buffers(data->mix,
|
||||
SPA_DIRECTION_OUTPUT, 0, data->mix_buffers, 1)) < 0)
|
||||
return res;
|
||||
|
||||
if ((res = spa_node_port_set_param(data->mix,
|
||||
SPA_DIRECTION_INPUT, data->mix_ports[0],
|
||||
SPA_PARAM_Format, 0,
|
||||
format)) < 0)
|
||||
return res;
|
||||
|
||||
if ((res = spa_node_port_set_param(data->source1,
|
||||
SPA_DIRECTION_OUTPUT, 0,
|
||||
SPA_PARAM_Format, 0,
|
||||
format)) < 0)
|
||||
return res;
|
||||
|
||||
init_buffer(data, data->source1_buffers, data->source1_buffer, 2, BUFFER_SIZE1);
|
||||
if ((res =
|
||||
spa_node_port_use_buffers(data->mix, SPA_DIRECTION_INPUT, data->mix_ports[0],
|
||||
data->source1_buffers, 2)) < 0)
|
||||
return res;
|
||||
if ((res =
|
||||
spa_node_port_use_buffers(data->source1, SPA_DIRECTION_OUTPUT, 0,
|
||||
data->source1_buffers, 2)) < 0)
|
||||
return res;
|
||||
|
||||
if ((res =
|
||||
spa_node_port_set_param(data->mix,
|
||||
SPA_DIRECTION_INPUT, data->mix_ports[1],
|
||||
SPA_PARAM_Format, 0,
|
||||
format)) < 0)
|
||||
return res;
|
||||
|
||||
if ((res = spa_node_port_set_param(data->source2,
|
||||
SPA_DIRECTION_OUTPUT, 0,
|
||||
SPA_PARAM_Format, 0,
|
||||
format)) < 0)
|
||||
return res;
|
||||
|
||||
init_buffer(data, data->source2_buffers, data->source2_buffer, 2, BUFFER_SIZE2);
|
||||
if ((res =
|
||||
spa_node_port_use_buffers(data->mix, SPA_DIRECTION_INPUT, data->mix_ports[1],
|
||||
data->source2_buffers, 2)) < 0)
|
||||
return res;
|
||||
if ((res =
|
||||
spa_node_port_use_buffers(data->source2, SPA_DIRECTION_OUTPUT, 0,
|
||||
data->source2_buffers, 2)) < 0)
|
||||
return res;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void *loop(void *user_data)
|
||||
{
|
||||
struct data *data = user_data;
|
||||
|
||||
printf("enter thread %d\n", data->n_sources);
|
||||
while (data->running) {
|
||||
int r;
|
||||
unsigned int i;
|
||||
|
||||
/* rebuild */
|
||||
if (data->rebuild_fds) {
|
||||
for (i = 0; i < data->n_sources; i++) {
|
||||
struct spa_source *p = &data->sources[i];
|
||||
data->fds[i].fd = p->fd;
|
||||
data->fds[i].events = p->mask;
|
||||
}
|
||||
data->n_fds = data->n_sources;
|
||||
data->rebuild_fds = false;
|
||||
}
|
||||
|
||||
r = poll((struct pollfd *) data->fds, data->n_fds, -1);
|
||||
if (r < 0) {
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
break;
|
||||
}
|
||||
if (r == 0) {
|
||||
fprintf(stderr, "select timeout");
|
||||
break;
|
||||
}
|
||||
|
||||
/* after */
|
||||
for (i = 0; i < data->n_sources; i++) {
|
||||
struct spa_source *p = &data->sources[i];
|
||||
p->rmask = 0;
|
||||
if (data->fds[i].revents & POLLIN)
|
||||
p->rmask |= SPA_IO_IN;
|
||||
if (data->fds[i].revents & POLLOUT)
|
||||
p->rmask |= SPA_IO_OUT;
|
||||
if (data->fds[i].revents & POLLHUP)
|
||||
p->rmask |= SPA_IO_HUP;
|
||||
if (data->fds[i].revents & POLLERR)
|
||||
p->rmask |= SPA_IO_ERR;
|
||||
}
|
||||
for (i = 0; i < data->n_sources; i++) {
|
||||
struct spa_source *p = &data->sources[i];
|
||||
if (p->rmask)
|
||||
p->func(p);
|
||||
}
|
||||
}
|
||||
printf("leave thread\n");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void run_async_sink(struct data *data)
|
||||
{
|
||||
int res;
|
||||
int err;
|
||||
|
||||
{
|
||||
struct spa_command cmd = SPA_NODE_COMMAND_INIT(SPA_NODE_COMMAND_Start);
|
||||
if ((res = spa_node_send_command(data->source1, &cmd)) < 0)
|
||||
printf("got source1 error %d\n", res);
|
||||
if ((res = spa_node_send_command(data->source2, &cmd)) < 0)
|
||||
printf("got source2 error %d\n", res);
|
||||
if ((res = spa_node_send_command(data->mix, &cmd)) < 0)
|
||||
printf("got mix error %d\n", res);
|
||||
if ((res = spa_node_send_command(data->sink, &cmd)) < 0)
|
||||
printf("got sink error %d\n", res);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
printf("sleeping for 30 seconds\n");
|
||||
sleep(30);
|
||||
|
||||
if (data->running) {
|
||||
data->running = false;
|
||||
pthread_join(data->thread, NULL);
|
||||
}
|
||||
|
||||
{
|
||||
struct spa_command cmd = SPA_NODE_COMMAND_INIT(SPA_NODE_COMMAND_Pause);
|
||||
if ((res = spa_node_send_command(data->sink, &cmd)) < 0)
|
||||
printf("got error %d\n", res);
|
||||
if ((res = spa_node_send_command(data->mix, &cmd)) < 0)
|
||||
printf("got mix error %d\n", res);
|
||||
if ((res = spa_node_send_command(data->source1, &cmd)) < 0)
|
||||
printf("got source1 error %d\n", res);
|
||||
if ((res = spa_node_send_command(data->source2, &cmd)) < 0)
|
||||
printf("got source2 error %d\n", res);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct data data = { NULL };
|
||||
int res;
|
||||
const char *str;
|
||||
|
||||
data.log = &default_log.log;
|
||||
data.data_loop.version = SPA_VERSION_LOOP;
|
||||
data.data_loop.add_source = do_add_source;
|
||||
data.data_loop.update_source = do_update_source;
|
||||
data.data_loop.remove_source = do_remove_source;
|
||||
data.data_loop.invoke = do_invoke;
|
||||
|
||||
spa_graph_init(&data.graph, &data.graph_state);
|
||||
|
||||
if ((str = getenv("SPA_DEBUG")))
|
||||
data.log->level = atoi(str);
|
||||
|
||||
data.support[0] = SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_Log, data.log);
|
||||
data.support[1] = SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_MainLoop, &data.data_loop);
|
||||
data.support[2] = SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_DataLoop, &data.data_loop);
|
||||
data.n_support = 3;
|
||||
|
||||
if ((res = make_nodes(&data, argc > 1 ? argv[1] : NULL)) < 0) {
|
||||
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_sink(&data);
|
||||
}
|
||||
|
|
@ -1,538 +0,0 @@
|
|||
/* Spa
|
||||
*
|
||||
* Copyright © 2018 Wim Taymans
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice (including the next
|
||||
* paragraph) shall be included in all copies or substantial portions of the
|
||||
* Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <dlfcn.h>
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
#include <poll.h>
|
||||
|
||||
#include <spa/support/log-impl.h>
|
||||
#include <spa/support/loop.h>
|
||||
#include <spa/node/node.h>
|
||||
#include <spa/node/io.h>
|
||||
#include <spa/param/param.h>
|
||||
#include <spa/param/props.h>
|
||||
#include <spa/param/audio/format-utils.h>
|
||||
#include <spa/graph/graph.h>
|
||||
|
||||
#define MODE_SYNC_PUSH (1<<0)
|
||||
#define MODE_SYNC_PULL (1<<1)
|
||||
#define MODE_ASYNC_PUSH (1<<2)
|
||||
#define MODE_ASYNC_PULL (1<<3)
|
||||
#define MODE_ASYNC_BOTH (MODE_ASYNC_PUSH|MODE_ASYNC_PULL)
|
||||
#define MODE_DIRECT (1<<4)
|
||||
|
||||
static SPA_LOG_IMPL(default_log);
|
||||
|
||||
struct buffer {
|
||||
struct spa_buffer buffer;
|
||||
struct spa_meta metas[1];
|
||||
struct spa_meta_header header;
|
||||
struct spa_data datas[1];
|
||||
struct spa_chunk chunks[1];
|
||||
};
|
||||
|
||||
struct data {
|
||||
struct spa_log *log;
|
||||
struct spa_loop data_loop;
|
||||
|
||||
int mode;
|
||||
|
||||
struct spa_support support[4];
|
||||
uint32_t n_support;
|
||||
|
||||
int iterations;
|
||||
|
||||
struct spa_graph graph;
|
||||
struct spa_graph_state graph_state;
|
||||
struct spa_graph_node source_node;
|
||||
struct spa_graph_state source_state;
|
||||
struct spa_graph_port source_out;
|
||||
struct spa_graph_port sink_in;
|
||||
struct spa_graph_node sink_node;
|
||||
struct spa_graph_state sink_state;
|
||||
|
||||
struct spa_node *sink;
|
||||
struct spa_io_buffers source_sink_io[1];
|
||||
|
||||
struct spa_node *source;
|
||||
struct spa_buffer *source_buffers[1];
|
||||
struct buffer source_buffer[1];
|
||||
|
||||
bool running;
|
||||
pthread_t thread;
|
||||
|
||||
struct spa_source sources[16];
|
||||
unsigned int n_sources;
|
||||
|
||||
bool rebuild_fds;
|
||||
struct pollfd fds[16];
|
||||
unsigned int n_fds;
|
||||
|
||||
void *hnd;
|
||||
};
|
||||
|
||||
#define MIN_LATENCY 64
|
||||
|
||||
#define BUFFER_SIZE MIN_LATENCY
|
||||
|
||||
static void
|
||||
init_buffer(struct data *data, struct spa_buffer **bufs, struct buffer *ba, int n_buffers,
|
||||
size_t size)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < n_buffers; i++) {
|
||||
struct buffer *b = &ba[i];
|
||||
bufs[i] = &b->buffer;
|
||||
|
||||
b->buffer.metas = b->metas;
|
||||
b->buffer.n_metas = 1;
|
||||
b->buffer.datas = b->datas;
|
||||
b->buffer.n_datas = 1;
|
||||
|
||||
b->header.flags = 0;
|
||||
b->header.seq = 0;
|
||||
b->header.pts = 0;
|
||||
b->header.dts_offset = 0;
|
||||
b->metas[0].type = SPA_META_Header;
|
||||
b->metas[0].data = &b->header;
|
||||
b->metas[0].size = sizeof(b->header);
|
||||
|
||||
b->datas[0].type = SPA_DATA_MemPtr;
|
||||
b->datas[0].flags = 0;
|
||||
b->datas[0].fd = -1;
|
||||
b->datas[0].mapoffset = 0;
|
||||
b->datas[0].maxsize = size;
|
||||
b->datas[0].data = malloc(size);
|
||||
b->datas[0].chunk = &b->chunks[0];
|
||||
b->datas[0].chunk->offset = 0;
|
||||
b->datas[0].chunk->size = size;
|
||||
b->datas[0].chunk->stride = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static int make_node(struct data *data, struct spa_node **node, const char *lib, const char *name)
|
||||
{
|
||||
struct spa_handle *handle;
|
||||
int res;
|
||||
spa_handle_factory_enum_func_t enum_func;
|
||||
uint32_t i;
|
||||
|
||||
if (data->hnd == NULL) {
|
||||
if ((data->hnd = dlopen(lib, RTLD_NOW)) == NULL) {
|
||||
printf("can't load %s: %s\n", lib, dlerror());
|
||||
return -errno;
|
||||
}
|
||||
}
|
||||
if ((enum_func = dlsym(data->hnd, SPA_HANDLE_FACTORY_ENUM_FUNC_NAME)) == NULL) {
|
||||
printf("can't find enum function\n");
|
||||
return -errno;
|
||||
}
|
||||
|
||||
for (i = 0;;) {
|
||||
const struct spa_handle_factory *factory;
|
||||
void *iface;
|
||||
|
||||
if ((res = enum_func(&factory, &i)) <= 0) {
|
||||
if (res != 0)
|
||||
printf("can't enumerate factories: %s\n", spa_strerror(res));
|
||||
break;
|
||||
}
|
||||
if (strcmp(factory->name, name))
|
||||
continue;
|
||||
|
||||
handle = calloc(1, spa_handle_factory_get_size(factory, NULL));
|
||||
if ((res =
|
||||
spa_handle_factory_init(factory, handle, NULL, data->support,
|
||||
data->n_support)) < 0) {
|
||||
printf("can't make factory instance: %d\n", res);
|
||||
return res;
|
||||
}
|
||||
if ((res = spa_handle_get_interface(handle, SPA_TYPE_INTERFACE_Node, &iface)) < 0) {
|
||||
printf("can't get interface %d\n", res);
|
||||
return res;
|
||||
}
|
||||
*node = iface;
|
||||
return 0;
|
||||
}
|
||||
return -EBADF;
|
||||
}
|
||||
|
||||
static void on_sink_pull(struct data *data)
|
||||
{
|
||||
spa_log_trace(data->log, "do sink pull");
|
||||
if (data->mode & MODE_DIRECT) {
|
||||
spa_node_process(data->source);
|
||||
spa_node_process(data->sink);
|
||||
} else {
|
||||
spa_graph_node_trigger(&data->sink_node);
|
||||
}
|
||||
}
|
||||
|
||||
static void on_source_push(struct data *data)
|
||||
{
|
||||
spa_log_trace(data->log, "do source push");
|
||||
if (data->mode & MODE_DIRECT) {
|
||||
spa_node_process(data->source);
|
||||
spa_node_process(data->sink);
|
||||
} else {
|
||||
spa_graph_node_trigger(&data->source_node);
|
||||
}
|
||||
}
|
||||
|
||||
static void on_sink_done(void *_data, int seq, int res)
|
||||
{
|
||||
struct data *data = _data;
|
||||
spa_log_trace(data->log, "got sink done %d %d", seq, res);
|
||||
}
|
||||
|
||||
static void on_sink_event(void *_data, struct spa_event *event)
|
||||
{
|
||||
struct data *data = _data;
|
||||
spa_log_trace(data->log, "got sink event %d", SPA_EVENT_TYPE(event));
|
||||
}
|
||||
|
||||
static void on_sink_process(void *_data, int status)
|
||||
{
|
||||
struct data *data = _data;
|
||||
spa_log_trace(data->log, "need input");
|
||||
on_sink_pull(data);
|
||||
if (--data->iterations == 0)
|
||||
data->running = false;
|
||||
}
|
||||
|
||||
static void
|
||||
on_sink_reuse_buffer(void *_data, uint32_t port_id, uint32_t buffer_id)
|
||||
{
|
||||
struct data *data = _data;
|
||||
|
||||
data->source_sink_io[0].buffer_id = buffer_id;
|
||||
}
|
||||
|
||||
static const struct spa_node_callbacks sink_callbacks = {
|
||||
SPA_VERSION_NODE_CALLBACKS,
|
||||
.done = on_sink_done,
|
||||
.event = on_sink_event,
|
||||
.process = on_sink_process,
|
||||
.reuse_buffer = on_sink_reuse_buffer
|
||||
};
|
||||
|
||||
static void on_source_done(void *_data, int seq, int res)
|
||||
{
|
||||
struct data *data = _data;
|
||||
spa_log_trace(data->log, "got source done %d %d", seq, res);
|
||||
}
|
||||
|
||||
static void on_source_event(void *_data, struct spa_event *event)
|
||||
{
|
||||
struct data *data = _data;
|
||||
spa_log_trace(data->log, "got source event %d", SPA_EVENT_TYPE(event));
|
||||
}
|
||||
|
||||
static void on_source_process(void *_data, int status)
|
||||
{
|
||||
struct data *data = _data;
|
||||
spa_log_trace(data->log, "have_output");
|
||||
on_source_push(data);
|
||||
if (--data->iterations == 0)
|
||||
data->running = false;
|
||||
}
|
||||
|
||||
static const struct spa_node_callbacks source_callbacks = {
|
||||
SPA_VERSION_NODE_CALLBACKS,
|
||||
.done = on_source_done,
|
||||
.event = on_source_event,
|
||||
.process = on_source_process
|
||||
};
|
||||
|
||||
|
||||
static int do_add_source(struct spa_loop *loop, struct spa_source *source)
|
||||
{
|
||||
struct data *data = SPA_CONTAINER_OF(loop, struct data, data_loop);
|
||||
|
||||
data->sources[data->n_sources] = *source;
|
||||
data->n_sources++;
|
||||
data->rebuild_fds = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int do_update_source(struct spa_source *source)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void do_remove_source(struct spa_source *source)
|
||||
{
|
||||
}
|
||||
|
||||
static int
|
||||
do_invoke(struct spa_loop *loop,
|
||||
spa_invoke_func_t func, uint32_t seq, const void *data, size_t size, bool block, void *user_data)
|
||||
{
|
||||
return func(loop, false, seq, data, size, user_data);
|
||||
}
|
||||
|
||||
static int make_nodes(struct data *data)
|
||||
{
|
||||
int res;
|
||||
|
||||
if ((res = make_node(data, &data->sink,
|
||||
"build/spa/plugins/test/libspa-test.so", "fakesink")) < 0) {
|
||||
printf("can't create fakesink: %d\n", res);
|
||||
return res;
|
||||
}
|
||||
|
||||
if (data->mode & MODE_ASYNC_PULL)
|
||||
spa_node_set_callbacks(data->sink, &sink_callbacks, data);
|
||||
|
||||
if ((res = make_node(data, &data->source,
|
||||
"build/spa/plugins/test/libspa-test.so", "fakesrc")) < 0) {
|
||||
printf("can't create fakesrc: %d\n", res);
|
||||
return res;
|
||||
}
|
||||
|
||||
if (data->mode & MODE_ASYNC_PUSH)
|
||||
spa_node_set_callbacks(data->source, &source_callbacks, data);
|
||||
|
||||
data->source_sink_io[0] = SPA_IO_BUFFERS_INIT;
|
||||
data->source_sink_io[0].status = SPA_STATUS_NEED_BUFFER;
|
||||
|
||||
spa_node_port_set_io(data->source,
|
||||
SPA_DIRECTION_OUTPUT, 0,
|
||||
SPA_IO_Buffers,
|
||||
&data->source_sink_io[0], sizeof(data->source_sink_io[0]));
|
||||
spa_node_port_set_io(data->sink,
|
||||
SPA_DIRECTION_INPUT, 0,
|
||||
SPA_IO_Buffers,
|
||||
&data->source_sink_io[0], sizeof(data->source_sink_io[0]));
|
||||
|
||||
spa_graph_node_init(&data->source_node, &data->source_state);
|
||||
spa_graph_node_set_callbacks(&data->source_node, &spa_graph_node_impl_default, data->source);
|
||||
spa_graph_node_add(&data->graph, &data->source_node);
|
||||
|
||||
spa_graph_port_init( &data->source_out, SPA_DIRECTION_OUTPUT, 0, 0);
|
||||
spa_graph_port_add(&data->source_node, &data->source_out);
|
||||
|
||||
spa_graph_node_init(&data->sink_node, &data->sink_state);
|
||||
spa_graph_node_set_callbacks(&data->sink_node, &spa_graph_node_impl_default, data->sink);
|
||||
spa_graph_node_add(&data->graph, &data->sink_node);
|
||||
|
||||
spa_graph_port_init(&data->sink_in, SPA_DIRECTION_INPUT, 0, 0);
|
||||
spa_graph_port_add(&data->sink_node, &data->sink_in);
|
||||
|
||||
spa_graph_port_link(&data->source_out, &data->sink_in);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static int negotiate_formats(struct data *data)
|
||||
{
|
||||
int res;
|
||||
struct spa_pod *format;
|
||||
struct spa_pod_builder b = { 0 };
|
||||
uint8_t buffer[256];
|
||||
|
||||
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
||||
format = spa_pod_builder_add_object(&b,
|
||||
SPA_TYPE_OBJECT_Format, 0,
|
||||
SPA_FORMAT_mediaType, SPA_POD_Id(SPA_MEDIA_TYPE_binary),
|
||||
SPA_FORMAT_mediaSubtype, SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw));
|
||||
|
||||
if ((res = spa_node_port_set_param(data->sink,
|
||||
SPA_DIRECTION_INPUT, 0,
|
||||
SPA_PARAM_Format, 0,
|
||||
format)) < 0)
|
||||
return res;
|
||||
|
||||
if ((res = spa_node_port_set_param(data->source,
|
||||
SPA_DIRECTION_OUTPUT, 0,
|
||||
SPA_PARAM_Format, 0,
|
||||
format)) < 0)
|
||||
return res;
|
||||
|
||||
init_buffer(data, data->source_buffers, data->source_buffer, 1, BUFFER_SIZE);
|
||||
|
||||
if ((res =
|
||||
spa_node_port_use_buffers(data->sink, SPA_DIRECTION_INPUT, 0, data->source_buffers,
|
||||
1)) < 0)
|
||||
return res;
|
||||
if ((res =
|
||||
spa_node_port_use_buffers(data->source, SPA_DIRECTION_OUTPUT, 0, data->source_buffers,
|
||||
1)) < 0)
|
||||
return res;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void *loop(void *user_data)
|
||||
{
|
||||
struct data *data = user_data;
|
||||
|
||||
printf("enter thread %d\n", data->n_sources);
|
||||
while (data->running) {
|
||||
int r;
|
||||
unsigned int i;
|
||||
|
||||
/* rebuild */
|
||||
if (data->rebuild_fds) {
|
||||
for (i = 0; i < data->n_sources; i++) {
|
||||
struct spa_source *p = &data->sources[i];
|
||||
data->fds[i].fd = p->fd;
|
||||
data->fds[i].events = p->mask;
|
||||
}
|
||||
data->n_fds = data->n_sources;
|
||||
data->rebuild_fds = false;
|
||||
}
|
||||
|
||||
r = poll(data->fds, data->n_fds, -1);
|
||||
if (r < 0) {
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
break;
|
||||
}
|
||||
if (r == 0) {
|
||||
fprintf(stderr, "select timeout");
|
||||
break;
|
||||
}
|
||||
|
||||
/* after */
|
||||
for (i = 0; i < data->n_sources; i++) {
|
||||
struct spa_source *p = &data->sources[i];
|
||||
p->rmask = 0;
|
||||
if (data->fds[i].revents & POLLIN)
|
||||
p->rmask |= SPA_IO_IN;
|
||||
if (data->fds[i].revents & POLLOUT)
|
||||
p->rmask |= SPA_IO_OUT;
|
||||
if (data->fds[i].revents & POLLHUP)
|
||||
p->rmask |= SPA_IO_HUP;
|
||||
if (data->fds[i].revents & POLLERR)
|
||||
p->rmask |= SPA_IO_ERR;
|
||||
}
|
||||
for (i = 0; i < data->n_sources; i++) {
|
||||
struct spa_source *p = &data->sources[i];
|
||||
if (p->rmask)
|
||||
p->func(p);
|
||||
}
|
||||
}
|
||||
printf("leave thread\n");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void run_graph(struct data *data)
|
||||
{
|
||||
int res;
|
||||
int err, i;
|
||||
struct timespec now;
|
||||
int64_t start, stop;
|
||||
|
||||
{
|
||||
struct spa_command cmd = SPA_NODE_COMMAND_INIT(SPA_NODE_COMMAND_Start);
|
||||
if ((res = spa_node_send_command(data->source, &cmd)) < 0)
|
||||
printf("got source error %d\n", res);
|
||||
if ((res = spa_node_send_command(data->sink, &cmd)) < 0)
|
||||
printf("got sink error %d\n", res);
|
||||
}
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
start = SPA_TIMESPEC_TO_NSEC(&now);
|
||||
|
||||
printf("running\n");
|
||||
|
||||
if (data->mode & MODE_SYNC_PUSH) {
|
||||
for (i = 0; i < data->iterations; i++)
|
||||
on_source_push(data);
|
||||
} else if (data->mode & MODE_SYNC_PULL) {
|
||||
for (i = 0; i < data->iterations; i++)
|
||||
on_sink_pull(data);
|
||||
} else {
|
||||
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;
|
||||
}
|
||||
if (data->running) {
|
||||
pthread_join(data->thread, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
stop = SPA_TIMESPEC_TO_NSEC(&now);
|
||||
|
||||
printf("stopping, elapsed %" PRIi64 "\n", stop - start);
|
||||
|
||||
{
|
||||
struct spa_command cmd = SPA_NODE_COMMAND_INIT(SPA_NODE_COMMAND_Pause);
|
||||
if ((res = spa_node_send_command(data->sink, &cmd)) < 0)
|
||||
printf("got error %d\n", res);
|
||||
if ((res = spa_node_send_command(data->source, &cmd)) < 0)
|
||||
printf("got source error %d\n", res);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct data data = { NULL };
|
||||
int res;
|
||||
const char *str;
|
||||
|
||||
spa_graph_init(&data.graph, &data.graph_state);
|
||||
|
||||
data.log = &default_log.log;
|
||||
data.data_loop.version = SPA_VERSION_LOOP;
|
||||
data.data_loop.add_source = do_add_source;
|
||||
data.data_loop.update_source = do_update_source;
|
||||
data.data_loop.remove_source = do_remove_source;
|
||||
data.data_loop.invoke = do_invoke;
|
||||
|
||||
if ((str = getenv("SPA_DEBUG")))
|
||||
data.log->level = atoi(str);
|
||||
|
||||
data.mode = argc > 1 ? atoi(argv[1]) : MODE_SYNC_PUSH;
|
||||
data.iterations = argc > 2 ? atoi(argv[2]) : 100000;
|
||||
|
||||
printf("mode %08x\n", data.mode);
|
||||
|
||||
data.support[0] = SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_Log, data.log);
|
||||
data.support[1] = SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_MainLoop, &data.data_loop);
|
||||
data.support[2] = SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_DataLoop, &data.data_loop);
|
||||
data.n_support = 3;
|
||||
|
||||
if ((res = make_nodes(&data)) < 0) {
|
||||
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_graph(&data);
|
||||
}
|
||||
205
spa/tests/test-pod.c
Normal file
205
spa/tests/test-pod.c
Normal file
|
|
@ -0,0 +1,205 @@
|
|||
/* Simple Plugin API
|
||||
* Copyright © 2019 Wim Taymans <wim.taymans@gmail.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice (including the next
|
||||
* paragraph) shall be included in all copies or substantial portions of the
|
||||
* Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <spa/pod/pod.h>
|
||||
#include <spa/pod/builder.h>
|
||||
#include <spa/pod/command.h>
|
||||
#include <spa/pod/event.h>
|
||||
#include <spa/pod/iter.h>
|
||||
#include <spa/pod/parser.h>
|
||||
|
||||
static void test_abi(void)
|
||||
{
|
||||
/* pod */
|
||||
spa_assert(sizeof(struct spa_pod) == 8);
|
||||
spa_assert(sizeof(struct spa_pod_bool) == 16);
|
||||
spa_assert(sizeof(struct spa_pod_id) == 16);
|
||||
spa_assert(sizeof(struct spa_pod_int) == 16);
|
||||
spa_assert(sizeof(struct spa_pod_long) == 16);
|
||||
spa_assert(sizeof(struct spa_pod_float) == 16);
|
||||
spa_assert(sizeof(struct spa_pod_double) == 16);
|
||||
spa_assert(sizeof(struct spa_pod_string) == 8);
|
||||
spa_assert(sizeof(struct spa_pod_bytes) == 8);
|
||||
spa_assert(sizeof(struct spa_pod_rectangle) == 16);
|
||||
spa_assert(sizeof(struct spa_pod_fraction) == 16);
|
||||
spa_assert(sizeof(struct spa_pod_bitmap) == 8);
|
||||
spa_assert(sizeof(struct spa_pod_array_body) == 8);
|
||||
spa_assert(sizeof(struct spa_pod_array) == 16);
|
||||
|
||||
spa_assert(SPA_CHOICE_None == 0);
|
||||
spa_assert(SPA_CHOICE_Range == 1);
|
||||
spa_assert(SPA_CHOICE_Step == 2);
|
||||
spa_assert(SPA_CHOICE_Enum == 3);
|
||||
spa_assert(SPA_CHOICE_Flags == 4);
|
||||
|
||||
spa_assert(sizeof(struct spa_pod_choice_body) == 16);
|
||||
spa_assert(sizeof(struct spa_pod_choice) == 24);
|
||||
spa_assert(sizeof(struct spa_pod_struct) == 8);
|
||||
spa_assert(sizeof(struct spa_pod_object_body) == 8);
|
||||
spa_assert(sizeof(struct spa_pod_object) == 16);
|
||||
spa_assert(sizeof(struct spa_pod_pointer_body) == 16);
|
||||
spa_assert(sizeof(struct spa_pod_pointer) == 24);
|
||||
spa_assert(sizeof(struct spa_pod_fd) == 16);
|
||||
spa_assert(sizeof(struct spa_pod_prop) == 16);
|
||||
spa_assert(sizeof(struct spa_pod_control) == 16);
|
||||
spa_assert(sizeof(struct spa_pod_sequence_body) == 8);
|
||||
spa_assert(sizeof(struct spa_pod_sequence) == 16);
|
||||
|
||||
/* builder */
|
||||
spa_assert(sizeof(struct spa_pod_frame) == 16);
|
||||
spa_assert(sizeof(struct spa_pod_builder_state) == 16);
|
||||
spa_assert(sizeof(struct spa_pod_builder) == 312);
|
||||
|
||||
/* command */
|
||||
spa_assert(sizeof(struct spa_command_body) == 8);
|
||||
spa_assert(sizeof(struct spa_command) == 16);
|
||||
|
||||
/* event */
|
||||
spa_assert(sizeof(struct spa_event_body) == 8);
|
||||
spa_assert(sizeof(struct spa_event) == 16);
|
||||
|
||||
/* iter */
|
||||
spa_assert(sizeof(struct spa_pod_iter) == 16);
|
||||
|
||||
/* parser */
|
||||
spa_assert(sizeof(struct spa_pod_parser) == 264);
|
||||
|
||||
}
|
||||
|
||||
static void test_init(void)
|
||||
{
|
||||
{
|
||||
struct spa_pod pod = SPA_POD_INIT(sizeof(int64_t), SPA_TYPE_Long);
|
||||
|
||||
spa_assert(SPA_POD_SIZE(&pod) == sizeof(int64_t) + 8);
|
||||
spa_assert(SPA_POD_TYPE(&pod) == SPA_TYPE_Long);
|
||||
spa_assert(SPA_POD_BODY_SIZE(&pod) == sizeof(int64_t));
|
||||
spa_assert(SPA_POD_CONTENTS_SIZE(struct spa_pod, &pod) == sizeof(int64_t));
|
||||
|
||||
pod = SPA_POD_INIT(sizeof(int32_t), SPA_TYPE_Int);
|
||||
spa_assert(SPA_POD_SIZE(&pod) == sizeof(int32_t) + 8);
|
||||
spa_assert(SPA_POD_TYPE(&pod) == SPA_TYPE_Int);
|
||||
spa_assert(SPA_POD_BODY_SIZE(&pod) == sizeof(int32_t));
|
||||
spa_assert(SPA_POD_CONTENTS_SIZE(struct spa_pod, &pod) == sizeof(int32_t));
|
||||
}
|
||||
{
|
||||
struct spa_pod pod = SPA_POD_INIT_None();
|
||||
|
||||
spa_assert(SPA_POD_SIZE(&pod) == 8);
|
||||
spa_assert(SPA_POD_TYPE(&pod) == SPA_TYPE_None);
|
||||
spa_assert(SPA_POD_BODY_SIZE(&pod) == 0);
|
||||
spa_assert(SPA_POD_CONTENTS_SIZE(struct spa_pod, &pod) == 0);
|
||||
}
|
||||
{
|
||||
struct spa_pod_bool pod = SPA_POD_INIT_Bool(true);
|
||||
|
||||
spa_assert(SPA_POD_SIZE(&pod) == 12);
|
||||
spa_assert(SPA_POD_TYPE(&pod) == SPA_TYPE_Bool);
|
||||
spa_assert(SPA_POD_BODY_SIZE(&pod) == 4);
|
||||
spa_assert(SPA_POD_VALUE(struct spa_pod_bool, &pod) == true);
|
||||
|
||||
pod = SPA_POD_INIT_Bool(false);
|
||||
spa_assert(SPA_POD_SIZE(&pod) == 12);
|
||||
spa_assert(SPA_POD_TYPE(&pod) == SPA_TYPE_Bool);
|
||||
spa_assert(SPA_POD_BODY_SIZE(&pod) == 4);
|
||||
spa_assert(SPA_POD_VALUE(struct spa_pod_bool, &pod) == false);
|
||||
}
|
||||
{
|
||||
struct spa_pod_id pod = SPA_POD_INIT_Id(SPA_TYPE_Int);
|
||||
|
||||
spa_assert(SPA_POD_SIZE(&pod) == 12);
|
||||
spa_assert(SPA_POD_TYPE(&pod) == SPA_TYPE_Id);
|
||||
spa_assert(SPA_POD_BODY_SIZE(&pod) == 4);
|
||||
spa_assert(SPA_POD_VALUE(struct spa_pod_id, &pod) == SPA_TYPE_Int);
|
||||
|
||||
pod = SPA_POD_INIT_Id(SPA_TYPE_Long);
|
||||
spa_assert(SPA_POD_SIZE(&pod) == 12);
|
||||
spa_assert(SPA_POD_TYPE(&pod) == SPA_TYPE_Id);
|
||||
spa_assert(SPA_POD_BODY_SIZE(&pod) == 4);
|
||||
spa_assert(SPA_POD_VALUE(struct spa_pod_id, &pod) == SPA_TYPE_Long);
|
||||
}
|
||||
{
|
||||
struct spa_pod_int pod = SPA_POD_INIT_Int(23);
|
||||
|
||||
spa_assert(SPA_POD_SIZE(&pod) == 12);
|
||||
spa_assert(SPA_POD_TYPE(&pod) == SPA_TYPE_Int);
|
||||
spa_assert(SPA_POD_BODY_SIZE(&pod) == 4);
|
||||
spa_assert(SPA_POD_VALUE(struct spa_pod_int, &pod) == 23);
|
||||
|
||||
pod = SPA_POD_INIT_Int(-123);
|
||||
spa_assert(SPA_POD_SIZE(&pod) == 12);
|
||||
spa_assert(SPA_POD_TYPE(&pod) == SPA_TYPE_Int);
|
||||
spa_assert(SPA_POD_BODY_SIZE(&pod) == 4);
|
||||
spa_assert(SPA_POD_VALUE(struct spa_pod_int, &pod) == -123);
|
||||
}
|
||||
{
|
||||
struct spa_pod_long pod = SPA_POD_INIT_Long(-23);
|
||||
|
||||
spa_assert(SPA_POD_SIZE(&pod) == 16);
|
||||
spa_assert(SPA_POD_TYPE(&pod) == SPA_TYPE_Long);
|
||||
spa_assert(SPA_POD_BODY_SIZE(&pod) == 8);
|
||||
spa_assert(SPA_POD_VALUE(struct spa_pod_long, &pod) == -23);
|
||||
|
||||
pod = SPA_POD_INIT_Long(123);
|
||||
spa_assert(SPA_POD_SIZE(&pod) == 16);
|
||||
spa_assert(SPA_POD_TYPE(&pod) == SPA_TYPE_Long);
|
||||
spa_assert(SPA_POD_BODY_SIZE(&pod) == 8);
|
||||
spa_assert(SPA_POD_VALUE(struct spa_pod_long, &pod) == 123);
|
||||
}
|
||||
{
|
||||
struct spa_pod_float pod = SPA_POD_INIT_Float(0.67f);
|
||||
|
||||
spa_assert(SPA_POD_SIZE(&pod) == 12);
|
||||
spa_assert(SPA_POD_TYPE(&pod) == SPA_TYPE_Float);
|
||||
spa_assert(SPA_POD_BODY_SIZE(&pod) == 4);
|
||||
spa_assert(SPA_POD_VALUE(struct spa_pod_float, &pod) == 0.67f);
|
||||
|
||||
pod = SPA_POD_INIT_Float(134.8f);
|
||||
spa_assert(SPA_POD_SIZE(&pod) == 12);
|
||||
spa_assert(SPA_POD_TYPE(&pod) == SPA_TYPE_Float);
|
||||
spa_assert(SPA_POD_BODY_SIZE(&pod) == 4);
|
||||
spa_assert(SPA_POD_VALUE(struct spa_pod_float, &pod) == 134.8f);
|
||||
}
|
||||
{
|
||||
struct spa_pod_double pod = SPA_POD_INIT_Double(0.67);
|
||||
|
||||
spa_assert(SPA_POD_SIZE(&pod) == 16);
|
||||
spa_assert(SPA_POD_TYPE(&pod) == SPA_TYPE_Double);
|
||||
spa_assert(SPA_POD_BODY_SIZE(&pod) == 8);
|
||||
spa_assert(SPA_POD_VALUE(struct spa_pod_double, &pod) == 0.67);
|
||||
|
||||
pod = SPA_POD_INIT_Double(134.8);
|
||||
spa_assert(SPA_POD_SIZE(&pod) == 16);
|
||||
spa_assert(SPA_POD_TYPE(&pod) == SPA_TYPE_Double);
|
||||
spa_assert(SPA_POD_BODY_SIZE(&pod) == 8);
|
||||
spa_assert(SPA_POD_VALUE(struct spa_pod_double, &pod) == 134.8);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
test_abi();
|
||||
test_init();
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,461 +0,0 @@
|
|||
/* Spa
|
||||
*
|
||||
* Copyright © 2018 Wim Taymans
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice (including the next
|
||||
* paragraph) shall be included in all copies or substantial portions of the
|
||||
* Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <spa/support/log-impl.h>
|
||||
#include <spa/pod/pod.h>
|
||||
#include <spa/pod/builder.h>
|
||||
#include <spa/pod/parser.h>
|
||||
#include <spa/param/video/format-utils.h>
|
||||
#include <spa/debug/pod.h>
|
||||
#include <spa/debug/format.h>
|
||||
#include <spa/debug/types.h>
|
||||
|
||||
#if 0
|
||||
/* { video/raw,
|
||||
* format: (int 1) { 1 2 },
|
||||
* size: (rect (320 240)) { (1 1) (MAX MAX) },
|
||||
* framerate: (frac (25 1)) { (0 1) (MAX 1) },
|
||||
*
|
||||
* "(object $format
|
||||
* (id $video) (id $raw)
|
||||
* (prop $format (int $I420) { $I420 $YUY2 } )
|
||||
* (prop $size (rect (320 240)) [ (1 1) (MAX MAX) ] )
|
||||
* (prop $framerate (frac (25 1))))
|
||||
* "
|
||||
*
|
||||
*/
|
||||
|
||||
bool: true | false
|
||||
int: <num>
|
||||
float: <num.frac>
|
||||
string: "<string>"
|
||||
bytes: "$<base64>"
|
||||
pointer: "@<pointer>"
|
||||
rectangle: "<width>x<height>"
|
||||
fraction: "<num>/<denom>"
|
||||
bitmask: "|<bits>"
|
||||
array: "[<values>,...]"
|
||||
object: "{<key>: <value>,...}"
|
||||
key: "<name>"
|
||||
|
||||
|
||||
"format": "S16LE",
|
||||
"format": [ "S16LE", "S24LE" ],
|
||||
"size": "320x240",
|
||||
"+size": [ "320x240", { "min": "320x240", "max": "640x480", "step": "8x8" ],
|
||||
"-size": [ "320x240", ["320x240", "640x480", "512x400"] ],
|
||||
|
||||
"schema.size": { "default": "320x240",
|
||||
"flags": ["range", "optional"],
|
||||
"values": ["320x240", "640x480", "8x8"] }
|
||||
|
||||
|
||||
"size-schema":
|
||||
{ "default": "320x240",
|
||||
"values": ["320x240", "640x480"]
|
||||
},
|
||||
"size-schema":
|
||||
{ "default": "320x240",
|
||||
"values": { "min": "320x240", "max": "640x480", "step": "8x8" }
|
||||
},
|
||||
|
||||
|
||||
"size-alt": { "def": "320x240", "type": "range", "vals": ["320x240", "640x480", "8x8"] },
|
||||
"size-alt": { "def": "320x240", "type": "enum", "vals": ["320x240", "640x480"],
|
||||
|
||||
{
|
||||
"type": "audio/raw",
|
||||
"format": ["S16LE", "enum", "S16LE", "S24LE" ],
|
||||
"size": [ "320x240", "range", "320x240", "640x480" ],
|
||||
"framerate": [ "25/1", "range", "25/1", "30/1" ]
|
||||
}
|
||||
|
||||
{ "type": "audio", "subtype": "raw",
|
||||
"format": "S16LE",
|
||||
"size": { "min": "320x240", "max": "640x480", "step": "8x8" },
|
||||
|
||||
"framerate": [ "25/1", "30/1" ] }
|
||||
|
||||
* JSON based format description
|
||||
|
||||
[ <type>,
|
||||
[ <media-type>, <media-subtype> ],
|
||||
{
|
||||
<key> : [ <type>, <value>, [ <value>, ... ] ],
|
||||
...
|
||||
}
|
||||
]
|
||||
|
||||
<type> = "123.."
|
||||
|
||||
1: s = string : "value"
|
||||
i = int : <number>
|
||||
f = float : <float>
|
||||
b = bool : true | false
|
||||
R = rectangle : [ <width>, <height> ]
|
||||
F = fraction : [ <num>, <denom> ]
|
||||
|
||||
2: - = default (only default value present)
|
||||
e = enum : [ <value>, ... ]
|
||||
f = flags : [ <number> ]
|
||||
m = min/max : [ <min>, <max> ]
|
||||
s = min/max/step : [ <min>, <max>, <step> ]
|
||||
|
||||
3: u = unset : value is unset, choose from options or default
|
||||
o = optional : value does not need to be set
|
||||
r = readonly : value is read only
|
||||
d = deprecated : value is deprecated
|
||||
|
||||
[ "Format",
|
||||
[ "video", "raw"],
|
||||
{
|
||||
"format" : [ "se", "I420", [ "I420", "YUY2" ] ],
|
||||
"size" : [ "Rmu", [320, 240], [[ 640, 480], [1024,786]]],
|
||||
"framerate" : [ "Fsu", [25, 1], [[ 0, 1], [65536,1]]]
|
||||
}
|
||||
]
|
||||
|
||||
[ "Format",
|
||||
[ "audio", "raw"],
|
||||
{
|
||||
"format" : [ "se", "S16LE", [ "F32LE", "S16LE" ] ],
|
||||
"rate" : [ "imu", 44100, [8000, 96000]],
|
||||
"channels" : [ "imu", 1, [1, 4096]]
|
||||
"interleaved" : [ "beo", true ]
|
||||
}
|
||||
]
|
||||
|
||||
( "Format",
|
||||
("video", "raw" ),
|
||||
{
|
||||
"format": ( "seu", "I420", ( "I420","YUY2" ) ),
|
||||
"size": ( "Rru", (320, 242), ( (1,1), (MAX, MAX)) ),
|
||||
"framerate": ( "Fru", (25, 1), ( (0,1), (MAX, 1)) )
|
||||
}
|
||||
)
|
||||
|
||||
{ "Type" : "Format", "Id" : 0,
|
||||
"mediaType": "video",
|
||||
"mediaSubtype": "raw",
|
||||
"Video:Format": [ "enum", "I420", "YUY2" ],
|
||||
"Video:Size": [ "range", [320,242], [1,1], [MAX,MAX] ],
|
||||
"Video:Framerate": [ "range", [25,1], [0,1], [MAX,1] ]
|
||||
}
|
||||
|
||||
spa_build(SPA_MEDIA_TYPE_VIDEO, SPA_MEDIA_SUBTYPE_RAW,
|
||||
SPA_FORMAT_VIDEO_format, SPA_PROP_TYPE_ID,
|
||||
video_format.I420
|
||||
SPA_POD_PROP_FLAG_UNSET |
|
||||
SPA_CHOICE_ENUM, 2,
|
||||
video_format.I420,
|
||||
video_format.YUY2,
|
||||
SPA_FORMAT_VIDEO_size, SPA_PROP_TYPE_RECTANGLE,
|
||||
320, 240,
|
||||
SPA_POD_PROP_FLAG_UNSET |
|
||||
SPA_CHOICE_RANGE,
|
||||
1, 1,
|
||||
INT32_MAX, INT32_MAX,
|
||||
SPA_FORMAT_VIDEO_framerate, SPA_PROP_TYPE_FRACTION, 25, 1,
|
||||
SPA_POD_PROP_FLAG_UNSET | SPA_CHOICE_RANGE, 0, 1, INT32_MAX, 1, 0);
|
||||
#endif
|
||||
|
||||
static void do_static_struct(void)
|
||||
{
|
||||
struct _test_format {
|
||||
struct spa_pod_object fmt;
|
||||
|
||||
struct {
|
||||
struct spa_pod_prop prop_media_type SPA_ALIGNED(8);
|
||||
uint32_t media_type;
|
||||
|
||||
struct spa_pod_prop prop_media_subtype SPA_ALIGNED(8);
|
||||
uint32_t media_subtype;
|
||||
|
||||
struct spa_pod_prop prop_format SPA_ALIGNED(8);
|
||||
struct {
|
||||
struct spa_pod_choice_body choice;
|
||||
uint32_t def_format;
|
||||
uint32_t enum_format[2];
|
||||
} format_vals;
|
||||
|
||||
struct spa_pod_prop prop_size SPA_ALIGNED(8);
|
||||
struct {
|
||||
struct spa_pod_choice_body choice;
|
||||
struct spa_rectangle def_size;
|
||||
struct spa_rectangle min_size;
|
||||
struct spa_rectangle max_size;
|
||||
} size_vals;
|
||||
|
||||
struct spa_pod_prop prop_framerate SPA_ALIGNED(8);
|
||||
struct {
|
||||
struct spa_pod_choice_body choice;
|
||||
struct spa_fraction def_framerate;
|
||||
struct spa_fraction min_framerate;
|
||||
struct spa_fraction max_framerate;
|
||||
} framerate_vals;
|
||||
} props;
|
||||
} test_format = {
|
||||
SPA_POD_INIT_Object(sizeof(test_format.props) + sizeof(struct spa_pod_object_body),
|
||||
SPA_TYPE_OBJECT_Format, 0),
|
||||
{
|
||||
SPA_POD_INIT_Prop(SPA_FORMAT_mediaType, 0,
|
||||
sizeof(test_format.props.media_type), SPA_TYPE_Id),
|
||||
SPA_MEDIA_TYPE_video,
|
||||
|
||||
SPA_POD_INIT_Prop(SPA_FORMAT_mediaSubtype, 0,
|
||||
sizeof(test_format.props.media_subtype), SPA_TYPE_Id),
|
||||
SPA_MEDIA_SUBTYPE_raw,
|
||||
|
||||
SPA_POD_INIT_Prop(SPA_FORMAT_VIDEO_format, 0,
|
||||
sizeof(test_format.props.format_vals), SPA_TYPE_Choice),
|
||||
{
|
||||
SPA_POD_INIT_CHOICE_BODY(SPA_CHOICE_Enum, 0,
|
||||
sizeof(uint32_t), SPA_TYPE_Id),
|
||||
SPA_VIDEO_FORMAT_I420,
|
||||
{ SPA_VIDEO_FORMAT_I420, SPA_VIDEO_FORMAT_YUY2 }
|
||||
},
|
||||
SPA_POD_INIT_Prop(SPA_FORMAT_VIDEO_size, 0,
|
||||
sizeof(test_format.props.size_vals), SPA_TYPE_Choice),
|
||||
|
||||
{
|
||||
SPA_POD_INIT_CHOICE_BODY(SPA_CHOICE_Range, 0,
|
||||
sizeof(struct spa_rectangle), SPA_TYPE_Rectangle),
|
||||
SPA_RECTANGLE(320,243),
|
||||
SPA_RECTANGLE(1,1), SPA_RECTANGLE(INT32_MAX, INT32_MAX)
|
||||
},
|
||||
SPA_POD_INIT_Prop(SPA_FORMAT_VIDEO_framerate, 0,
|
||||
sizeof(test_format.props.framerate_vals), SPA_TYPE_Choice),
|
||||
{
|
||||
SPA_POD_INIT_CHOICE_BODY(SPA_CHOICE_Range, 0,
|
||||
sizeof(struct spa_fraction), SPA_TYPE_Fraction),
|
||||
SPA_FRACTION(25,1),
|
||||
SPA_FRACTION(0,1), SPA_FRACTION(INT32_MAX,1)
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
fprintf(stderr, "static:\n");
|
||||
|
||||
spa_debug_pod(0, NULL, &test_format.fmt.pod);
|
||||
spa_debug_format(0, NULL, &test_format.fmt.pod);
|
||||
|
||||
{
|
||||
uint32_t format = -1;
|
||||
int res;
|
||||
struct spa_fraction frac = { -1, -1 };
|
||||
|
||||
res = spa_pod_parse_object(&test_format.fmt.pod,
|
||||
SPA_TYPE_OBJECT_Format, NULL,
|
||||
SPA_FORMAT_VIDEO_format, SPA_POD_Id(&format),
|
||||
SPA_FORMAT_VIDEO_framerate, SPA_POD_Fraction(&frac));
|
||||
|
||||
printf("%d format %d num %d denom %d\n", res, format, frac.num, frac.denom);
|
||||
|
||||
spa_pod_fixate(&test_format.fmt.pod);
|
||||
|
||||
res = spa_pod_parse_object(&test_format.fmt.pod,
|
||||
SPA_TYPE_OBJECT_Format, NULL,
|
||||
SPA_FORMAT_VIDEO_format, SPA_POD_Id(&format),
|
||||
SPA_FORMAT_VIDEO_framerate, SPA_POD_Fraction(&frac));
|
||||
|
||||
printf("%d format %d num %d denom %d\n", res, format, frac.num, frac.denom);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#define ITER 10000000
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct spa_pod_builder b = { NULL, };
|
||||
uint8_t buffer[1024];
|
||||
struct spa_pod_object *fmt;
|
||||
int i;
|
||||
struct timespec ts1, ts2;
|
||||
|
||||
fprintf(stderr, "build 1: ");
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts1);
|
||||
for (i = 0 ; i < ITER; i++) {
|
||||
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
||||
|
||||
spa_pod_builder_push_object(&b, SPA_TYPE_OBJECT_Format, 0);
|
||||
|
||||
spa_pod_builder_prop(&b, SPA_FORMAT_mediaType, 0);
|
||||
spa_pod_builder_id(&b, SPA_MEDIA_TYPE_video);
|
||||
spa_pod_builder_prop(&b, SPA_FORMAT_mediaSubtype, 0);
|
||||
spa_pod_builder_id(&b, SPA_MEDIA_SUBTYPE_raw);
|
||||
|
||||
spa_pod_builder_prop(&b, SPA_FORMAT_VIDEO_format, 0);
|
||||
spa_pod_builder_push_choice(&b, SPA_CHOICE_Enum, 0);
|
||||
spa_pod_builder_id(&b, SPA_VIDEO_FORMAT_I420);
|
||||
spa_pod_builder_id(&b, SPA_VIDEO_FORMAT_I420);
|
||||
spa_pod_builder_id(&b, SPA_VIDEO_FORMAT_YUY2);
|
||||
spa_pod_builder_pop(&b);
|
||||
|
||||
struct spa_rectangle size_min_max[] = { {1, 1}, {INT32_MAX, INT32_MAX} };
|
||||
spa_pod_builder_prop(&b, SPA_FORMAT_VIDEO_size, 0);
|
||||
spa_pod_builder_push_choice(&b, SPA_CHOICE_Range, 0);
|
||||
spa_pod_builder_rectangle(&b, 320, 240);
|
||||
spa_pod_builder_raw(&b, size_min_max, sizeof(size_min_max));
|
||||
spa_pod_builder_pop(&b);
|
||||
|
||||
struct spa_fraction rate_min_max[] = { {0, 1}, {INT32_MAX, 1} };
|
||||
spa_pod_builder_prop(&b, SPA_FORMAT_VIDEO_framerate, 0);
|
||||
spa_pod_builder_push_choice(&b, SPA_CHOICE_Range, 0);
|
||||
spa_pod_builder_fraction(&b, 25, 1);
|
||||
spa_pod_builder_raw(&b, rate_min_max, sizeof(rate_min_max));
|
||||
spa_pod_builder_pop(&b);
|
||||
|
||||
fmt = spa_pod_builder_pop(&b);
|
||||
}
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts2);
|
||||
fprintf(stderr, "elapsed %lld\n", SPA_TIMESPEC_TO_NSEC(&ts2) - SPA_TIMESPEC_TO_NSEC(&ts1));
|
||||
|
||||
spa_debug_pod(0, NULL, &fmt->pod);
|
||||
fprintf(stderr, "build 2: ");
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts1);
|
||||
for (i = 0 ; i < ITER; i++) {
|
||||
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
||||
|
||||
fmt = spa_pod_builder_add_object(&b,
|
||||
SPA_TYPE_OBJECT_Format, 0,
|
||||
SPA_FORMAT_mediaType, "I", SPA_MEDIA_TYPE_video,
|
||||
SPA_FORMAT_mediaSubtype, "I", SPA_MEDIA_SUBTYPE_raw,
|
||||
SPA_FORMAT_VIDEO_format, "?eI", 3, SPA_VIDEO_FORMAT_I420,
|
||||
SPA_VIDEO_FORMAT_I420,
|
||||
SPA_VIDEO_FORMAT_YUY2,
|
||||
SPA_FORMAT_VIDEO_size, "?rR", 3, &SPA_RECTANGLE(320,241),
|
||||
&SPA_RECTANGLE(1,1),
|
||||
&SPA_RECTANGLE(INT32_MAX,INT32_MAX),
|
||||
SPA_FORMAT_VIDEO_framerate, "?rF", 3, &SPA_FRACTION(25,1),
|
||||
&SPA_FRACTION(0,1),
|
||||
&SPA_FRACTION(INT32_MAX,1));
|
||||
}
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts2);
|
||||
fprintf(stderr, "elapsed %lld\n", SPA_TIMESPEC_TO_NSEC(&ts2) - SPA_TIMESPEC_TO_NSEC(&ts1));
|
||||
|
||||
spa_debug_pod(0, NULL, &fmt->pod);
|
||||
spa_debug_format(0, NULL, &fmt->pod);
|
||||
|
||||
fprintf(stderr, "build 3: ");
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts1);
|
||||
for (i = 0 ; i < ITER; i++) {
|
||||
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
||||
|
||||
fmt = spa_pod_builder_add(&b,
|
||||
"{", SPA_TYPE_OBJECT_Format, 0,
|
||||
SPA_FORMAT_mediaType, "I", SPA_MEDIA_TYPE_video,
|
||||
SPA_FORMAT_mediaSubtype, "I", SPA_MEDIA_SUBTYPE_raw,
|
||||
SPA_FORMAT_VIDEO_format, SPA_POD_CHOICE_ENUM_Id(3,
|
||||
SPA_VIDEO_FORMAT_I420,
|
||||
SPA_VIDEO_FORMAT_I420,
|
||||
SPA_VIDEO_FORMAT_YUY2),
|
||||
SPA_FORMAT_VIDEO_size, SPA_POD_CHOICE_RANGE_Rectangle(
|
||||
&SPA_RECTANGLE(320,242),
|
||||
&SPA_RECTANGLE(1,1),
|
||||
&SPA_RECTANGLE(INT32_MAX,INT32_MAX)),
|
||||
SPA_FORMAT_VIDEO_framerate, SPA_POD_CHOICE_RANGE_Fraction(
|
||||
&SPA_FRACTION(25,1),
|
||||
&SPA_FRACTION(0,1),
|
||||
&SPA_FRACTION(INT32_MAX,1)),
|
||||
0,
|
||||
"}", NULL);
|
||||
}
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts2);
|
||||
fprintf(stderr, "elapsed %lld\n", SPA_TIMESPEC_TO_NSEC(&ts2) - SPA_TIMESPEC_TO_NSEC(&ts1));
|
||||
|
||||
spa_debug_pod(0, NULL, &fmt->pod);
|
||||
spa_debug_format(0, NULL, &fmt->pod);
|
||||
|
||||
do_static_struct();
|
||||
|
||||
fprintf(stderr, "build 4: ");
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts1);
|
||||
for (i = 0 ; i < ITER; i++) {
|
||||
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
||||
spa_pod_builder_push_object(&b, SPA_TYPE_OBJECT_Format, 0);
|
||||
spa_pod_builder_prop(&b, SPA_FORMAT_mediaType, 0);
|
||||
spa_pod_builder_id(&b, SPA_MEDIA_TYPE_video);
|
||||
spa_pod_builder_prop(&b, SPA_FORMAT_mediaSubtype, 0);
|
||||
spa_pod_builder_id(&b, SPA_MEDIA_SUBTYPE_raw);
|
||||
spa_pod_builder_prop(&b, SPA_FORMAT_VIDEO_format, 0);
|
||||
spa_pod_builder_primitive(&b, (struct spa_pod*)&SPA_POD_INIT_Choice(
|
||||
SPA_CHOICE_Enum, uint32_t, SPA_TYPE_Id, 3,
|
||||
SPA_VIDEO_FORMAT_I420, SPA_VIDEO_FORMAT_I420, SPA_VIDEO_FORMAT_YUY2));
|
||||
spa_pod_builder_prop(&b, SPA_FORMAT_VIDEO_size, 0);
|
||||
spa_pod_builder_primitive(&b, (struct spa_pod*)&SPA_POD_INIT_Choice(
|
||||
SPA_CHOICE_Range, struct spa_rectangle, SPA_TYPE_Rectangle, 3,
|
||||
SPA_RECTANGLE(320,242),
|
||||
SPA_RECTANGLE(1,1),
|
||||
SPA_RECTANGLE(INT32_MAX,INT32_MAX)));
|
||||
spa_pod_builder_prop(&b, SPA_FORMAT_VIDEO_framerate, 0);
|
||||
spa_pod_builder_primitive(&b, (struct spa_pod*)&SPA_POD_INIT_Choice(
|
||||
SPA_CHOICE_Range, struct spa_fraction, SPA_TYPE_Fraction, 3,
|
||||
SPA_FRACTION(25,1),
|
||||
SPA_FRACTION(0,1),
|
||||
SPA_FRACTION(INT32_MAX,1)));
|
||||
fmt = spa_pod_builder_pop(&b);
|
||||
}
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts2);
|
||||
fprintf(stderr, "elapsed %lld\n", SPA_TIMESPEC_TO_NSEC(&ts2) - SPA_TIMESPEC_TO_NSEC(&ts1));
|
||||
|
||||
spa_debug_pod(0, NULL, &fmt->pod);
|
||||
spa_debug_format(0, NULL, &fmt->pod);
|
||||
|
||||
fprintf(stderr, "build 5: ");
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts1);
|
||||
for (i = 0 ; i < ITER; i++) {
|
||||
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
||||
fmt = spa_pod_builder_add_object(&b,
|
||||
SPA_TYPE_OBJECT_Format, 0,
|
||||
SPA_FORMAT_mediaType, SPA_POD_Id(SPA_MEDIA_TYPE_video),
|
||||
SPA_FORMAT_mediaSubtype, SPA_POD_Id(SPA_MEDIA_SUBTYPE_raw),
|
||||
SPA_FORMAT_VIDEO_format, SPA_POD_CHOICE_ENUM_Id(3,
|
||||
SPA_VIDEO_FORMAT_I420,
|
||||
SPA_VIDEO_FORMAT_I420,
|
||||
SPA_VIDEO_FORMAT_YUY2),
|
||||
SPA_FORMAT_VIDEO_size, SPA_POD_CHOICE_RANGE_Rectangle(
|
||||
&SPA_RECTANGLE(320,242),
|
||||
&SPA_RECTANGLE(1,1),
|
||||
&SPA_RECTANGLE(INT32_MAX,INT32_MAX)),
|
||||
SPA_FORMAT_VIDEO_framerate,SPA_POD_CHOICE_RANGE_Fraction(
|
||||
&SPA_FRACTION(25,1),
|
||||
&SPA_FRACTION(0,1),
|
||||
&SPA_FRACTION(INT32_MAX,1)));
|
||||
}
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts2);
|
||||
fprintf(stderr, "elapsed %lld\n", SPA_TIMESPEC_TO_NSEC(&ts2) - SPA_TIMESPEC_TO_NSEC(&ts1));
|
||||
|
||||
spa_debug_pod(0, NULL, &fmt->pod);
|
||||
spa_debug_format(0, NULL, &fmt->pod);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,121 +0,0 @@
|
|||
/* Spa
|
||||
*
|
||||
* Copyright © 2018 Wim Taymans
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice (including the next
|
||||
* paragraph) shall be included in all copies or substantial portions of the
|
||||
* Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <spa/support/log-impl.h>
|
||||
#include <spa/pod/pod.h>
|
||||
#include <spa/pod/builder.h>
|
||||
#include <spa/pod/parser.h>
|
||||
#include <spa/param/video/format.h>
|
||||
|
||||
#include <spa/debug/pod.h>
|
||||
#include <spa/debug/types.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct spa_pod_builder b = { NULL, };
|
||||
uint8_t buffer[1024];
|
||||
uint32_t ref;
|
||||
struct spa_pod *obj;
|
||||
struct spa_pod_parser prs;
|
||||
|
||||
b.data = buffer;
|
||||
b.size = 1024;
|
||||
|
||||
spa_pod_builder_push_object(&b, 0, 0);
|
||||
|
||||
uint32_t formats[] = { 1, 2 };
|
||||
spa_pod_builder_prop(&b, 1, 0);
|
||||
spa_pod_builder_push_array(&b);
|
||||
spa_pod_builder_int(&b, 1);
|
||||
spa_pod_builder_int(&b, formats[0]);
|
||||
spa_pod_builder_int(&b, formats[1]);
|
||||
spa_pod_builder_pop(&b);
|
||||
|
||||
spa_pod_builder_prop(&b, 2, 0);
|
||||
spa_pod_builder_int(&b, 42);
|
||||
|
||||
struct spa_rectangle sizes[] = { {0, 0}, {1024, 1024} };
|
||||
spa_pod_builder_prop(&b, 3, 0);
|
||||
spa_pod_builder_push_array(&b);
|
||||
spa_pod_builder_rectangle(&b, 320, 240);
|
||||
spa_pod_builder_raw(&b, sizes, sizeof(sizes));
|
||||
spa_pod_builder_pop(&b);
|
||||
|
||||
spa_pod_builder_prop(&b, 4, 0);
|
||||
ref = spa_pod_builder_push_struct(&b);
|
||||
spa_pod_builder_int(&b, 4);
|
||||
spa_pod_builder_long(&b, 6000);
|
||||
spa_pod_builder_float(&b, 4.0);
|
||||
spa_pod_builder_double(&b, 3.14);
|
||||
spa_pod_builder_string(&b, "test123");
|
||||
spa_pod_builder_rectangle(&b, 320, 240);
|
||||
spa_pod_builder_fraction(&b, 25, 1);
|
||||
spa_pod_builder_push_array(&b);
|
||||
spa_pod_builder_int(&b, 4);
|
||||
spa_pod_builder_int(&b, 5);
|
||||
spa_pod_builder_int(&b, 6);
|
||||
spa_pod_builder_pop(&b);
|
||||
spa_pod_builder_pop(&b);
|
||||
obj = spa_pod_builder_pop(&b);
|
||||
|
||||
spa_debug_pod(0, NULL, obj);
|
||||
|
||||
struct spa_pod_prop *p = spa_pod_find_prop(obj, 4);
|
||||
spa_debug_pod(0, NULL, &p->value);
|
||||
|
||||
obj = spa_pod_builder_deref(&b, ref);
|
||||
|
||||
int32_t vi, *pi;
|
||||
int64_t vl;
|
||||
float vf;
|
||||
double vd;
|
||||
char *vs;
|
||||
struct spa_rectangle vr;
|
||||
struct spa_fraction vfr;
|
||||
struct spa_pod_array *va;
|
||||
spa_pod_parser_pod(&prs, obj);
|
||||
spa_pod_parser_get(&prs,
|
||||
"["
|
||||
"i", &vi,
|
||||
"l", &vl,
|
||||
"f", &vf,
|
||||
"d", &vd,
|
||||
"s", &vs,
|
||||
"R", &vr,
|
||||
"F", &vfr,
|
||||
"P", &va, 0);
|
||||
|
||||
printf("%u %lu %f %g %s %ux%u %u/%u\n", vi, vl, vf, vd, vs, vr.width, vr.height, vfr.num,
|
||||
vfr.denom);
|
||||
SPA_POD_ARRAY_BODY_FOREACH(&va->body, SPA_POD_BODY_SIZE(va), pi) {
|
||||
printf("%d\n", *pi);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,865 +0,0 @@
|
|||
/* Spa
|
||||
*
|
||||
* Copyright © 2018 Wim Taymans
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice (including the next
|
||||
* paragraph) shall be included in all copies or substantial portions of the
|
||||
* Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <spa/support/log.h>
|
||||
#include <spa/pod/iter.h>
|
||||
|
||||
#if 0
|
||||
/*
|
||||
( "Format",
|
||||
( "video", "raw" ),
|
||||
{
|
||||
"format": ( "seu", "I420", ( "I420", "YUY2" ) ),
|
||||
"size": ( "Rru", R(320, 242), ( R(1,1), R(MAX, MAX)) ),
|
||||
"framerate": ( "Fru", F(25, 1), ( F(0,1), F(MAX, 1)) )
|
||||
}
|
||||
)
|
||||
|
||||
( struct
|
||||
{ object
|
||||
[ array
|
||||
|
||||
1: s = string : "value"
|
||||
i = int : <number>
|
||||
l = long : <number>
|
||||
f = float : <float>
|
||||
d = double : <float>
|
||||
b = bool : true | false
|
||||
R = rectangle : [ <width>, <height> ]
|
||||
F = fraction : [ <num>, <denom> ]
|
||||
|
||||
2: - = default (only default value present)
|
||||
e = enum : [ <value>, ... ]
|
||||
f = flags : [ <number> ]
|
||||
m = min/max : [ <min>, <max> ]
|
||||
s = min/max/step : [ <min>, <max>, <step> ]
|
||||
|
||||
3: u = unset : value is unset, choose from options or default
|
||||
o = optional : value does not need to be set
|
||||
r = readonly : value is read only
|
||||
d = deprecated : value is deprecated
|
||||
*/
|
||||
#endif
|
||||
|
||||
#define SPA_POD_MAX_DEPTH 16
|
||||
|
||||
struct spa_pod_maker {
|
||||
struct spa_pod_builder b;
|
||||
struct spa_pod_frame frame[SPA_POD_MAX_DEPTH];
|
||||
int depth;
|
||||
};
|
||||
|
||||
static inline void spa_pod_maker_init(struct spa_pod_maker *maker,
|
||||
char *data, int size)
|
||||
{
|
||||
spa_pod_builder_init(&maker->b, data, size);
|
||||
maker->depth = 0;
|
||||
}
|
||||
|
||||
static const struct {
|
||||
char *pat;
|
||||
int len;
|
||||
int64_t val;
|
||||
} spa_constants[] = {
|
||||
{ "#I_MAX#", strlen("#I_MAX#"), INT32_MAX },
|
||||
{ "#I_MIN#", strlen("#I_MIN#"), INT32_MIN },
|
||||
{ "#L_MAX#", strlen("#L_MAX#"), INT64_MAX },
|
||||
{ "#L_MIN#", strlen("#L_MIN#"), INT64_MIN }
|
||||
};
|
||||
|
||||
static inline int64_t spa_parse_int(const char *str, char **endptr)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (*str != '#')
|
||||
return strtoll(str, endptr, 10);
|
||||
|
||||
for (i = 0; i < SPA_N_ELEMENTS(spa_constants); i++) {
|
||||
if (strncmp(str, spa_constants[i].pat, spa_constants[i].len) == 0) {
|
||||
*endptr = (char *) (str + spa_constants[i].len);
|
||||
return spa_constants[i].val;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline int spa_parse_string(const char *str, char **endptr)
|
||||
{
|
||||
int len;
|
||||
for (*endptr = (char *)str+1; **endptr != '\"' && **endptr != '\0'; (*endptr)++);
|
||||
len = (*endptr)++ - (str + 1);
|
||||
return len;
|
||||
}
|
||||
|
||||
static inline void *
|
||||
spa_pod_maker_build(struct spa_pod_maker *maker,
|
||||
const char *format, ...)
|
||||
{
|
||||
va_list args;
|
||||
const char *start, *strval;
|
||||
int64_t intval;
|
||||
double doubleval;
|
||||
char last;
|
||||
struct spa_rectangle *rectval;
|
||||
struct spa_fraction *fracval;
|
||||
int len;
|
||||
|
||||
va_start(args, format);
|
||||
while (*format != '\0') {
|
||||
switch (*format) {
|
||||
case '[':
|
||||
spa_pod_builder_push_struct(&maker->b, &maker->frame[maker->depth++]);
|
||||
break;
|
||||
case '(':
|
||||
spa_pod_builder_push_array(&maker->b, &maker->frame[maker->depth++]);
|
||||
break;
|
||||
case '{':
|
||||
spa_pod_builder_push_object(&maker->b, &maker->frame[maker->depth++], 0, 0);
|
||||
break;
|
||||
case ']': case '}': case ')':
|
||||
spa_pod_builder_pop(&maker->b, &maker->frame[--maker->depth]);
|
||||
break;
|
||||
case '\"':
|
||||
start = format + 1;
|
||||
if ((len = spa_parse_string(format, (char **) &format)) < 0)
|
||||
return NULL;
|
||||
format += strspn(format, " \t\r\n");
|
||||
if (*format == ':')
|
||||
spa_pod_builder_key_len(&maker->b, start, len);
|
||||
else
|
||||
spa_pod_builder_string_len(&maker->b, start, len);
|
||||
continue;
|
||||
case '@':
|
||||
case '%':
|
||||
last = *format;
|
||||
format++;
|
||||
switch (*format) {
|
||||
case 's':
|
||||
strval = va_arg(args, char *);
|
||||
spa_pod_builder_string_len(&maker->b, strval, strlen(strval));
|
||||
break;
|
||||
case 'i':
|
||||
spa_pod_builder_int(&maker->b, va_arg(args, int));
|
||||
break;
|
||||
case 'I':
|
||||
spa_pod_builder_id(&maker->b, va_arg(args, int));
|
||||
break;
|
||||
case 'l':
|
||||
spa_pod_builder_long(&maker->b, va_arg(args, int64_t));
|
||||
break;
|
||||
case 'f':
|
||||
spa_pod_builder_float(&maker->b, va_arg(args, double));
|
||||
break;
|
||||
case 'd':
|
||||
spa_pod_builder_double(&maker->b, va_arg(args, double));
|
||||
break;
|
||||
case 'b':
|
||||
spa_pod_builder_bool(&maker->b, va_arg(args, int));
|
||||
break;
|
||||
case 'z':
|
||||
{
|
||||
void *ptr = va_arg(args, void *);
|
||||
int len = va_arg(args, int);
|
||||
spa_pod_builder_bytes(&maker->b, ptr, len);
|
||||
break;
|
||||
}
|
||||
case 'p':
|
||||
spa_pod_builder_pointer(&maker->b, 0, va_arg(args, void *));
|
||||
break;
|
||||
case 'h':
|
||||
spa_pod_builder_fd(&maker->b, va_arg(args, int));
|
||||
break;
|
||||
case 'a':
|
||||
{
|
||||
int child_size = va_arg(args, int);
|
||||
int child_type = va_arg(args, int);
|
||||
int n_elems = va_arg(args, int);
|
||||
void *elems = va_arg(args, void *);
|
||||
spa_pod_builder_array(&maker->b, child_size, child_type, n_elems, elems);
|
||||
break;
|
||||
}
|
||||
case 'P':
|
||||
spa_pod_builder_primitive(&maker->b, va_arg(args, struct spa_pod *));
|
||||
break;
|
||||
case 'R':
|
||||
rectval = va_arg(args, struct spa_rectangle *);
|
||||
spa_pod_builder_rectangle(&maker->b, rectval->width, rectval->height);
|
||||
break;
|
||||
case 'F':
|
||||
fracval = va_arg(args, struct spa_fraction *);
|
||||
spa_pod_builder_fraction(&maker->b, fracval->num, fracval->denom);
|
||||
break;
|
||||
}
|
||||
if (last == '@') {
|
||||
format = va_arg(args, const char *);
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
case '0' ... '9': case '-': case '+': case '#':
|
||||
start = format;
|
||||
intval = spa_parse_int(start, (char **) &format);
|
||||
if (*format == '.') {
|
||||
doubleval = strtod(start, (char **) &format);
|
||||
if (*format == 'f')
|
||||
spa_pod_builder_float(&maker->b, doubleval);
|
||||
else
|
||||
spa_pod_builder_double(&maker->b, doubleval);
|
||||
continue;
|
||||
}
|
||||
switch (*format) {
|
||||
case 'x':
|
||||
spa_pod_builder_rectangle(&maker->b, intval,
|
||||
spa_parse_int(format+1, (char **) &format));
|
||||
break;
|
||||
case '/':
|
||||
spa_pod_builder_fraction(&maker->b, intval,
|
||||
spa_parse_int(format+1, (char **) &format));
|
||||
break;
|
||||
case 'l':
|
||||
spa_pod_builder_long(&maker->b, intval);
|
||||
format++;
|
||||
break;
|
||||
default:
|
||||
spa_pod_builder_int(&maker->b, intval);
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
format++;
|
||||
}
|
||||
va_end(args);
|
||||
|
||||
return SPA_POD_BUILDER_DEREF(&maker->b, maker->frame[maker->depth].ref, void);
|
||||
}
|
||||
|
||||
static inline int spa_pod_id_to_type(char id)
|
||||
{
|
||||
switch (id) {
|
||||
case 'n':
|
||||
return SPA_ID_None;
|
||||
case 'b':
|
||||
return SPA_ID_Bool;
|
||||
case 'I':
|
||||
return SPA_ID_Enum;
|
||||
case 'i':
|
||||
return SPA_ID_INT;
|
||||
case 'l':
|
||||
return SPA_ID_LONG;
|
||||
case 'f':
|
||||
return SPA_ID_FLOAT;
|
||||
case 'd':
|
||||
return SPA_ID_DOUBLE;
|
||||
case 's':
|
||||
return SPA_ID_STRING;
|
||||
case 'k':
|
||||
return SPA_ID_KEY;
|
||||
case 'z':
|
||||
return SPA_ID_BYTES;
|
||||
case 'R':
|
||||
return SPA_ID_RECTANGLE;
|
||||
case 'F':
|
||||
return SPA_ID_FRACTION;
|
||||
case 'B':
|
||||
return SPA_ID_BITMASK;
|
||||
case 'A':
|
||||
return SPA_ID_ARRAY;
|
||||
case 'S':
|
||||
return SPA_ID_STRUCT;
|
||||
case 'O':
|
||||
return SPA_ID_OBJECT;
|
||||
case 'M':
|
||||
return SPA_ID_MAP;
|
||||
case 'p':
|
||||
return SPA_ID_POINTER;
|
||||
case 'h':
|
||||
return SPA_ID_FD;
|
||||
case 'V': case 'v':
|
||||
return SPA_ID_PROP;
|
||||
case 'P':
|
||||
return SPA_ID_POD;
|
||||
default:
|
||||
return SPA_ID_INVALID;
|
||||
}
|
||||
}
|
||||
|
||||
enum spa_pod_prop_range {
|
||||
SPA_POD_PROP2_RANGE_NONE = '-',
|
||||
SPA_POD_PROP2_RANGE_MIN_MAX = 'r',
|
||||
SPA_POD_PROP2_RANGE_STEP = 's',
|
||||
SPA_POD_PROP2_RANGE_ENUM = 'e',
|
||||
SPA_POD_PROP2_RANGE_FLAGS = 'f'
|
||||
};
|
||||
|
||||
enum spa_pod_prop_flags {
|
||||
SPA_POD_PROP2_FLAG_UNSET = (1 << 0),
|
||||
SPA_POD_PROP2_FLAG_OPTIONAL = (1 << 1),
|
||||
SPA_POD_PROP2_FLAG_READONLY = (1 << 2),
|
||||
SPA_POD_PROP2_FLAG_DEPRECATED = (1 << 3),
|
||||
};
|
||||
|
||||
struct spa_pod_prop2 {
|
||||
enum spa_pod_type type;
|
||||
enum spa_pod_prop_range range;
|
||||
enum spa_pod_prop_flags flags;
|
||||
struct spa_pod *value;
|
||||
struct spa_pod *alternatives;
|
||||
};
|
||||
|
||||
static inline int spa_pod_match(struct spa_pod *pod, const char *templ, ...);
|
||||
|
||||
|
||||
static inline int
|
||||
spa_pod_parse_prop(struct spa_pod *pod, enum spa_pod_type type, struct spa_pod_prop2 *prop)
|
||||
{
|
||||
int res;
|
||||
|
||||
if (SPA_POD_TYPE(pod) == SPA_ID_STRUCT) {
|
||||
const char *flags;
|
||||
char ch;
|
||||
|
||||
if ((res = spa_pod_match(pod,
|
||||
"[ %s, %P, %P ]",
|
||||
&flags,
|
||||
&prop->value,
|
||||
&prop->alternatives)) < 0) {
|
||||
printf("can't parse prop chunk %d\n", res);
|
||||
return res;
|
||||
}
|
||||
prop->type = spa_pod_id_to_type(*flags++);
|
||||
if (type != SPA_ID_POD && type != SPA_POD_TYPE(prop->value)) {
|
||||
printf("prop chunk of wrong type %d != %d\n", SPA_POD_TYPE(prop->value), type);
|
||||
return -1;
|
||||
}
|
||||
prop->range = *flags++;
|
||||
/* flags */
|
||||
prop->flags = 0;
|
||||
while ((ch = *flags++) != '\0') {
|
||||
switch (ch) {
|
||||
case 'u':
|
||||
prop->flags |= SPA_POD_PROP2_FLAG_UNSET;
|
||||
break;
|
||||
case 'o':
|
||||
prop->flags |= SPA_POD_PROP2_FLAG_OPTIONAL;
|
||||
break;
|
||||
case 'r':
|
||||
prop->flags |= SPA_POD_PROP2_FLAG_READONLY;
|
||||
break;
|
||||
case 'd':
|
||||
prop->flags |= SPA_POD_PROP2_FLAG_DEPRECATED;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
/* a single value */
|
||||
if (type != SPA_ID_POD && type != SPA_POD_TYPE(pod)) {
|
||||
printf("prop chunk of wrong type %d != %d\n", SPA_POD_TYPE(prop->value), type);
|
||||
return -1;
|
||||
}
|
||||
prop->type = SPA_POD_TYPE(pod);
|
||||
prop->range = SPA_POD_PROP2_RANGE_NONE;
|
||||
prop->flags = 0;
|
||||
prop->value = pod;
|
||||
prop->alternatives = pod;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline int
|
||||
spa_pod_match(struct spa_pod *pod,
|
||||
const char *templ, ...)
|
||||
{
|
||||
struct spa_pod_iter it[SPA_POD_MAX_DEPTH];
|
||||
int depth = 0, collected = 0;
|
||||
va_list args;
|
||||
const char *start;
|
||||
int64_t intval, int2val;
|
||||
double doubleval;
|
||||
char last;
|
||||
struct spa_rectangle *rectval;
|
||||
struct spa_fraction *fracval;
|
||||
int type, len;
|
||||
struct spa_pod *current = pod;
|
||||
struct spa_pod_prop2 prop;
|
||||
bool store, maybe;
|
||||
|
||||
va_start(args, templ);
|
||||
while (*templ != '\0') {
|
||||
switch (*templ) {
|
||||
case '[':
|
||||
depth++;
|
||||
if (current == NULL ||
|
||||
!spa_pod_iter_struct(&it[depth], current, SPA_POD_SIZE(current)))
|
||||
goto done;
|
||||
break;
|
||||
case '(':
|
||||
break;
|
||||
case '{':
|
||||
depth++;
|
||||
if (current == NULL ||
|
||||
!spa_pod_iter_map(&it[depth], current, SPA_POD_SIZE(current)))
|
||||
goto done;
|
||||
break;
|
||||
case ']': case '}': case ')':
|
||||
if (depth == 0)
|
||||
return -1;
|
||||
if (--depth == 0)
|
||||
goto done;
|
||||
break;
|
||||
case '\"':
|
||||
start = templ + 1;
|
||||
if ((len = spa_parse_string(templ, (char **) &templ)) < 0)
|
||||
return -1;
|
||||
templ += strspn(templ, " \t\r\n");
|
||||
if (*templ == ':') {
|
||||
if (SPA_POD_TYPE(it[depth].data) != SPA_ID_MAP)
|
||||
return -1;
|
||||
it[depth].offset = sizeof(struct spa_pod_map);
|
||||
/* move to key */
|
||||
while (spa_pod_iter_has_next(&it[depth])) {
|
||||
current = spa_pod_iter_next(&it[depth]);
|
||||
if (SPA_POD_TYPE(current) == SPA_ID_KEY &&
|
||||
strncmp(SPA_POD_CONTENTS_CONST(struct spa_pod_key, current),
|
||||
start, len) == 0)
|
||||
break;
|
||||
current = NULL;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (current == NULL || SPA_POD_TYPE(current) != SPA_ID_STRING ||
|
||||
strncmp(SPA_POD_CONTENTS_CONST(struct spa_pod_string, current),
|
||||
start, len) != 0)
|
||||
goto done;
|
||||
}
|
||||
break;
|
||||
case '@':
|
||||
case '%':
|
||||
last = *templ;
|
||||
if (*++templ == '\0')
|
||||
return -1;
|
||||
|
||||
store = *templ != '*';
|
||||
if (!store)
|
||||
if (*++templ == '\0')
|
||||
return -1;
|
||||
|
||||
maybe = *templ == '?';
|
||||
if (maybe)
|
||||
if (*++templ == '\0')
|
||||
return -1;
|
||||
|
||||
if (*templ == 'V' || *templ == 'v') {
|
||||
char t = *templ;
|
||||
templ++;
|
||||
type = spa_pod_id_to_type(*templ);
|
||||
|
||||
if (current == NULL)
|
||||
goto no_current;
|
||||
|
||||
if (spa_pod_parse_prop(current, type, &prop) < 0)
|
||||
return -1;
|
||||
|
||||
if (t == 'v') {
|
||||
if (prop.flags & SPA_POD_PROP2_FLAG_UNSET) {
|
||||
if (store)
|
||||
va_arg(args, void *);
|
||||
goto skip;
|
||||
}
|
||||
else
|
||||
current = prop.value;
|
||||
}
|
||||
else {
|
||||
collected++;
|
||||
*va_arg(args, struct spa_pod_prop2 *) = prop;
|
||||
goto skip;
|
||||
}
|
||||
}
|
||||
|
||||
no_current:
|
||||
type = spa_pod_id_to_type(*templ);
|
||||
if (current == NULL || (type != SPA_ID_POD && type != SPA_POD_TYPE(current))) {
|
||||
if (!maybe)
|
||||
return -1;
|
||||
if (store)
|
||||
va_arg(args, void *);
|
||||
goto skip;
|
||||
}
|
||||
if (!store)
|
||||
goto skip;
|
||||
|
||||
collected++;
|
||||
|
||||
switch (*templ) {
|
||||
case 'n': case 'A': case 'S': case 'O': case 'M': case 'P':
|
||||
*va_arg(args, struct spa_pod **) = current;
|
||||
break;
|
||||
case 'b':
|
||||
case 'i':
|
||||
case 'I':
|
||||
*va_arg(args, int32_t *) = SPA_POD_VALUE(struct spa_pod_int, current);
|
||||
break;
|
||||
case 'l':
|
||||
*va_arg(args, int64_t *) = SPA_POD_VALUE(struct spa_pod_long, current);
|
||||
break;
|
||||
case 'f':
|
||||
*va_arg(args, float *) = SPA_POD_VALUE(struct spa_pod_float, current);
|
||||
break;
|
||||
case 'd':
|
||||
*va_arg(args, double *) = SPA_POD_VALUE(struct spa_pod_double, current);
|
||||
break;
|
||||
case 's':
|
||||
case 'k':
|
||||
*va_arg(args, char **) = SPA_POD_CONTENTS(struct spa_pod_string, current);
|
||||
break;
|
||||
case 'z':
|
||||
*va_arg(args, void **) = SPA_POD_CONTENTS(struct spa_pod_bytes, current);
|
||||
*va_arg(args, uint32_t *) = SPA_POD_BODY_SIZE(current);
|
||||
break;
|
||||
case 'R':
|
||||
*va_arg(args, struct spa_rectangle *) =
|
||||
SPA_POD_VALUE(struct spa_pod_rectangle, current);
|
||||
break;
|
||||
case 'F':
|
||||
*va_arg(args, struct spa_fraction *) =
|
||||
SPA_POD_VALUE(struct spa_pod_fraction, current);
|
||||
break;
|
||||
case 'p':
|
||||
{
|
||||
struct spa_pod_pointer_body *b = SPA_POD_BODY(current);
|
||||
*va_arg(args, void **) = b->value;
|
||||
break;
|
||||
}
|
||||
case 'h':
|
||||
*va_arg(args, int *) = SPA_POD_VALUE(struct spa_pod_fd, current);
|
||||
break;
|
||||
default:
|
||||
va_arg(args, void *);
|
||||
break;
|
||||
}
|
||||
|
||||
skip:
|
||||
if (last == '@') {
|
||||
templ = va_arg(args, void *);
|
||||
goto next;
|
||||
}
|
||||
break;
|
||||
case '0' ... '9': case '-': case '+': case '#':
|
||||
start = templ;
|
||||
intval = spa_parse_int(start, (char **) &templ);
|
||||
if (*templ == '.') {
|
||||
doubleval = strtod(start, (char **) &templ);
|
||||
if (*templ == 'f') {
|
||||
if (current == NULL ||
|
||||
SPA_POD_TYPE(current) != SPA_ID_FLOAT ||
|
||||
doubleval != SPA_POD_VALUE(struct spa_pod_float, current))
|
||||
goto done;
|
||||
break;
|
||||
}
|
||||
else if (current == NULL ||
|
||||
SPA_POD_TYPE(current) != SPA_ID_DOUBLE ||
|
||||
doubleval != SPA_POD_VALUE(struct spa_pod_double, current))
|
||||
goto done;
|
||||
goto next;
|
||||
}
|
||||
switch (*templ) {
|
||||
case 'x':
|
||||
if (current == NULL ||
|
||||
SPA_POD_TYPE(current) != SPA_ID_RECTANGLE)
|
||||
goto done;
|
||||
|
||||
rectval = &SPA_POD_VALUE(struct spa_pod_rectangle, current);
|
||||
int2val = spa_parse_int(templ+1, (char **) &templ);
|
||||
if (rectval->width != intval || rectval->height != int2val)
|
||||
goto done;
|
||||
goto next;
|
||||
case '/':
|
||||
if (current == NULL ||
|
||||
SPA_POD_TYPE(current) != SPA_ID_FRACTION)
|
||||
goto done;
|
||||
|
||||
fracval = &SPA_POD_VALUE(struct spa_pod_fraction, current);
|
||||
int2val = spa_parse_int(templ+1, (char **) &templ);
|
||||
if (fracval->num != intval || fracval->denom != int2val)
|
||||
goto done;
|
||||
goto next;
|
||||
case 'l':
|
||||
if (current == NULL ||
|
||||
SPA_POD_TYPE(current) != SPA_ID_LONG ||
|
||||
SPA_POD_VALUE(struct spa_pod_long, current) != intval)
|
||||
goto done;
|
||||
break;
|
||||
default:
|
||||
if (current == NULL ||
|
||||
SPA_POD_TYPE(current) != SPA_ID_INT ||
|
||||
SPA_POD_VALUE(struct spa_pod_int, current) != intval)
|
||||
goto done;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case ' ': case '\n': case '\t': case '\r': case ',':
|
||||
templ++;
|
||||
continue;
|
||||
}
|
||||
templ++;
|
||||
|
||||
next:
|
||||
if (spa_pod_iter_has_next(&it[depth]))
|
||||
current = spa_pod_iter_next(&it[depth]);
|
||||
else
|
||||
current = NULL;
|
||||
}
|
||||
va_end(args);
|
||||
|
||||
done:
|
||||
return collected;
|
||||
}
|
||||
|
||||
static int test_match(const char *fmt)
|
||||
{
|
||||
const char *media_type, *media_subtype, *format;
|
||||
int rate = -1, res;
|
||||
struct spa_pod_prop2 channels;
|
||||
struct spa_pod *pod;
|
||||
struct spa_pod_maker m = { 0, };
|
||||
char buffer[4096];
|
||||
|
||||
spa_pod_maker_init(&m, buffer, sizeof(buffer));
|
||||
pod = spa_pod_maker_build(&m, fmt);
|
||||
spa_debug_pod(pod);
|
||||
|
||||
res = spa_pod_match(pod,
|
||||
"[ \"Format\", "
|
||||
" [ @s",&media_type," @s",&media_subtype," ], "
|
||||
" { "
|
||||
" \"rate\": @vi", &rate,
|
||||
" \"format\": @vs", &format,
|
||||
" \"channels\": @VP", &channels,
|
||||
" \"foo\": @?VP", &channels,
|
||||
" } "
|
||||
"]");
|
||||
|
||||
printf("collected %d\n", res);
|
||||
printf("media type %s\n", media_type);
|
||||
printf("media subtype %s\n", media_subtype);
|
||||
printf("media rate %d\n", rate);
|
||||
printf("media format %s\n", format);
|
||||
printf("media channels: %d %c %04x\n",channels.type, channels.range, channels.flags);
|
||||
spa_debug_pod(channels.value);
|
||||
spa_debug_pod(channels.alternatives);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct spa_pod_maker m = { 0, };
|
||||
char buffer[4096];
|
||||
struct spa_pod *fmt;
|
||||
|
||||
spa_pod_maker_init(&m, buffer, sizeof(buffer));
|
||||
fmt = spa_pod_maker_build(&m,
|
||||
"[ \"Format\", "
|
||||
" [\"video\", \"raw\" ], "
|
||||
" { "
|
||||
" \"format\": [ \"eu\", \"I420\", [ \"I420\",\"YUY2\" ] ], "
|
||||
" \"size\": [ \"ru\", 320x242, [ 1x1, #I_MAX#x#I_MAX# ] ], "
|
||||
" \"framerate\": [ \"ru\", 25/1, [ 0/1, #I_MAX#/1 ] ] "
|
||||
" } "
|
||||
"] ");
|
||||
spa_debug_pod(fmt);
|
||||
|
||||
spa_pod_maker_init(&m, buffer, sizeof(buffer));
|
||||
fmt = spa_pod_maker_build(&m,
|
||||
"[ \"Format\", "
|
||||
" [\"video\", %s ], "
|
||||
" { "
|
||||
" \"format\": [ \"eu\", \"I420\", [ %s, \"YUY2\" ] ], "
|
||||
" \"size\": [ \"ru\", 320x242, [ %R, #I_MAX#x#I_MAX# ] ], "
|
||||
" \"framerate\": [ \"ru\", %F, [ 0/1, #I_MAX#/1 ] ] "
|
||||
" } "
|
||||
"] ",
|
||||
"raw",
|
||||
"I420",
|
||||
&(struct spa_rectangle){ 1, 1 },
|
||||
&(struct spa_fraction){ 25, 1 }
|
||||
);
|
||||
spa_debug_pod(fmt);
|
||||
|
||||
{
|
||||
const char *format = "S16";
|
||||
int rate = 44100, channels = 2;
|
||||
|
||||
spa_pod_maker_init(&m, buffer, sizeof(buffer));
|
||||
fmt = spa_pod_maker_build(&m,
|
||||
"[ \"Format\", "
|
||||
" [\"audio\", \"raw\" ], "
|
||||
" { "
|
||||
" \"format\": [@s", format, "] "
|
||||
" \"rate\": [@i", rate, "] "
|
||||
" \"channels\": [@i", channels, "] "
|
||||
" \"rect\": [@R", &SPA_RECTANGLE(32, 22), "] "
|
||||
" } "
|
||||
"] ");
|
||||
spa_debug_pod(fmt);
|
||||
}
|
||||
|
||||
{
|
||||
const char *format = "S16";
|
||||
int rate = 44100, channels = 2;
|
||||
struct spa_rectangle rects[3] = { { 1, 1 }, { 2, 2}, {3, 3}};
|
||||
struct spa_pod_int pod = SPA_POD_INT_INIT(12);
|
||||
uint8_t bytes[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
|
||||
|
||||
spa_pod_maker_init(&m, buffer, sizeof(buffer));
|
||||
spa_pod_maker_build(&m,
|
||||
"[ \"Format\", "
|
||||
" [\"audio\", \"raw\" ], ");
|
||||
fmt = spa_pod_maker_build(&m,
|
||||
" { "
|
||||
" \"format\": [ %s ] "
|
||||
" \"rate\": [ %i, ( 44100, 48000, 96000 ) ]"
|
||||
" \"foo\": %i, ( 1.1, 2.2, 3.2 )"
|
||||
" \"baz\": ( 1.1f, 2.2f, 3.2f )"
|
||||
" \"bar\": ( 1x1, 2x2, 3x2 )"
|
||||
" \"faz\": ( 1/1, 2/2, 3/2 )"
|
||||
" \"wha\": %a, "
|
||||
" \"fuz\": %P, "
|
||||
// " \"fur\": ( (1, 2), (7, 8), (7, 5) ) "
|
||||
// " \"fur\": ( [1, 2], [7, 8], [7, 5] ) "
|
||||
" \"buz\": %z, "
|
||||
" \"boo\": %p, "
|
||||
" \"foz\": %h, "
|
||||
" } "
|
||||
"] ", format, rate, channels,
|
||||
sizeof(struct spa_rectangle), SPA_ID_RECTANGLE, 3, rects,
|
||||
&pod,
|
||||
bytes, sizeof(bytes),
|
||||
fmt,
|
||||
STDOUT_FILENO);
|
||||
spa_debug_pod(fmt);
|
||||
}
|
||||
|
||||
spa_pod_maker_init(&m, buffer, sizeof(buffer));
|
||||
fmt = spa_pod_maker_build(&m,
|
||||
"[ \"Format\", "
|
||||
" [\"video\", %s ], "
|
||||
" { "
|
||||
" \"format\": [ \"eu\", \"I420\", [ %s, \"YUY2\" ] ], "
|
||||
" \"size\": [ \"ru\", 320x242, [ %R, #I_MAX#x#I_MAX# ] ], "
|
||||
" \"framerate\": [ \"ru\", %F, [ 0/1, #I_MAX#/1 ] ] "
|
||||
" } "
|
||||
"] ",
|
||||
"raw",
|
||||
"I420",
|
||||
&(struct spa_rectangle){ 1, 1 },
|
||||
&(struct spa_fraction){ 25, 1 }
|
||||
);
|
||||
spa_debug_pod(fmt);
|
||||
|
||||
{
|
||||
const char *subtype = NULL, *format = NULL;
|
||||
struct spa_pod *pod = NULL;
|
||||
struct spa_rectangle rect = { 0, 0 };
|
||||
struct spa_fraction frac = { 0, 0 };
|
||||
int res;
|
||||
|
||||
res = spa_pod_match(fmt,
|
||||
"[ \"Format\", "
|
||||
" [\"video\", %s ], "
|
||||
" { "
|
||||
" \"format\": [ %*s, %*s, [ %s, %*s ] ], "
|
||||
" \"size\": [ \"ru\", 320x242, [ %R, %P ] ], "
|
||||
" \"framerate\": [ %*P, %F, %*S ] "
|
||||
" } "
|
||||
"] ",
|
||||
&subtype,
|
||||
&format,
|
||||
&rect,
|
||||
&pod,
|
||||
&frac);
|
||||
|
||||
printf("collected %d\n", res);
|
||||
printf("media type %s\n", subtype);
|
||||
printf("media format %s\n", format);
|
||||
printf("media size %dx%d\n", rect.width, rect.height);
|
||||
printf("media size pod\n");
|
||||
spa_debug_pod(pod);
|
||||
printf("media framerate %d/%d\n", frac.num, frac.denom);
|
||||
|
||||
res = spa_pod_match(fmt,
|
||||
"[ \"Format\", "
|
||||
" [\"video\", @s", &subtype, " ], "
|
||||
" { "
|
||||
" \"format\": [ %*s, %*s, [ @s", &format, ", %*s ] ], "
|
||||
" \"size\": [ \"ru\", 320x242, [ @R",&rect,", @P",&pod," ] ], "
|
||||
" \"framerate\": [ %*P, @F",&frac,", %*S ] "
|
||||
" } "
|
||||
"] ");
|
||||
|
||||
printf("collected %d\n", res);
|
||||
printf("media type %s\n", subtype);
|
||||
printf("media format %s\n", format);
|
||||
printf("media size %dx%d\n", rect.width, rect.height);
|
||||
printf("media size pod\n");
|
||||
spa_debug_pod(pod);
|
||||
printf("media framerate %d/%d\n", frac.num, frac.denom);
|
||||
|
||||
res = spa_pod_match(fmt,
|
||||
"[ \"Format\", "
|
||||
" [\"video\", @s", &subtype, " ], "
|
||||
" { "
|
||||
" \"format\": [ %*s, %*s, [ @s", &format, ", %*s ] ], "
|
||||
" \"size\": [ \"ru\", 320x242, [ @R",&rect,", @P",&pod," ] ], "
|
||||
" \"framerate\": [ %*P, @F",&frac,", %*S ] "
|
||||
" } "
|
||||
"] ");
|
||||
|
||||
}
|
||||
|
||||
test_match("[ \"Format\", "
|
||||
" [\"audio\", \"raw\" ], "
|
||||
" { "
|
||||
" \"format\": [ \"se\", \"S16\", [ \"S16\", \"F32\" ] ], "
|
||||
" \"rate\": [ \"iru\", 44100, [ 1, 192000 ] ], "
|
||||
" \"channels\": [ \"ir\", 2, [ 1, #I_MAX# ]] "
|
||||
" } "
|
||||
"] ");
|
||||
|
||||
test_match(
|
||||
"[ \"Format\", "
|
||||
" [ \"audio\", \"raw\"], "
|
||||
" { "
|
||||
" \"format\": \"S16LE\", "
|
||||
" \"rate\": 44100, "
|
||||
" \"channels\": 2 "
|
||||
" }"
|
||||
"]");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,97 +0,0 @@
|
|||
/* Spa
|
||||
*
|
||||
* Copyright © 2018 Wim Taymans
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice (including the next
|
||||
* paragraph) shall be included in all copies or substantial portions of the
|
||||
* Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <spa/pod/parser.h>
|
||||
#include <spa/pod/builder.h>
|
||||
|
||||
#include <spa/debug/pod.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
char buffer[4096];
|
||||
struct spa_pod_builder b = { 0, };
|
||||
struct spa_pod_parser prs;
|
||||
struct spa_pod *fmt, *pod = NULL, *pod2 = NULL;
|
||||
uint32_t format = 1, video = 2, raw = 3;
|
||||
uint32_t size = 4, framerate = 5, I420 = 6, YUY2 = 7;
|
||||
uint32_t media_type = -1, media_subtype = -1;
|
||||
uint32_t fmt_value = -1;
|
||||
int res;
|
||||
|
||||
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
||||
fmt = spa_pod_builder_add(&b,
|
||||
"{",0,format,
|
||||
" [ i", video, "i",raw,"]"
|
||||
" :", format, "ieu", I420,
|
||||
2, I420, YUY2,
|
||||
" :", size, "Rru", &SPA_RECTANGLE(320,240),
|
||||
2, &SPA_RECTANGLE(1,1),
|
||||
&SPA_RECTANGLE(INT32_MAX, INT32_MAX),
|
||||
" :", framerate, "Fru", &SPA_FRACTION(25,1),
|
||||
2, &SPA_FRACTION(0,1),
|
||||
&SPA_FRACTION(INT32_MAX, 1),
|
||||
"}", NULL);
|
||||
spa_debug_pod(0, NULL, fmt);
|
||||
|
||||
spa_pod_parser_pod(&prs, fmt);
|
||||
res = spa_pod_parser_get(&prs,
|
||||
"{"
|
||||
" [ i",&media_type,"*i"/*,&media_subtype,*/" ]"
|
||||
" :", framerate, "V", &pod,
|
||||
" :", 10, "?V", &pod2,
|
||||
" :", format, "?i", &fmt_value,
|
||||
"}", NULL);
|
||||
|
||||
printf("res :%d\n", res);
|
||||
printf("media-type:%d media-subtype:%d\n", media_type, media_subtype);
|
||||
printf("framerate:\n");
|
||||
if (pod)
|
||||
spa_debug_pod(0, NULL, pod);
|
||||
printf("format: %d\n", fmt_value);
|
||||
|
||||
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
||||
pod = spa_pod_builder_add(&b,
|
||||
"{",0,format,
|
||||
" P", NULL,
|
||||
" [ i", 44, "i",45,"]"
|
||||
"}", NULL);
|
||||
spa_debug_pod(0, NULL, pod);
|
||||
|
||||
spa_pod_parser_pod(&prs, pod);
|
||||
res = spa_pod_parser_get(&prs,
|
||||
"{"
|
||||
" ?[ i",&media_type,"i",&media_subtype," ]"
|
||||
" [ i", &video, "i",&raw,"]"
|
||||
"}", NULL);
|
||||
printf("res :%d\n", res);
|
||||
printf("video:%d raw:%d\n", video, raw);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,447 +0,0 @@
|
|||
/* Spa
|
||||
*
|
||||
* Copyright © 2018 Wim Taymans
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice (including the next
|
||||
* paragraph) shall be included in all copies or substantial portions of the
|
||||
* Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <dlfcn.h>
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
#include <poll.h>
|
||||
|
||||
#include <spa/support/log-impl.h>
|
||||
#include <spa/support/loop.h>
|
||||
#include <spa/node/node.h>
|
||||
#include <spa/node/io.h>
|
||||
#include <spa/param/param.h>
|
||||
#include <spa/param/props.h>
|
||||
#include <spa/param/audio/format-utils.h>
|
||||
|
||||
static SPA_LOG_IMPL(default_log);
|
||||
|
||||
struct buffer {
|
||||
struct spa_buffer buffer;
|
||||
struct spa_meta metas[1];
|
||||
struct spa_meta_header header;
|
||||
struct spa_data datas[1];
|
||||
struct spa_chunk chunks[1];
|
||||
};
|
||||
|
||||
struct data {
|
||||
struct spa_log *log;
|
||||
struct spa_loop data_loop;
|
||||
|
||||
struct spa_support support[4];
|
||||
uint32_t n_support;
|
||||
|
||||
struct spa_node *sink;
|
||||
struct spa_io_buffers source_sink_io[1];
|
||||
|
||||
struct spa_node *source;
|
||||
struct spa_buffer *source_buffers[1];
|
||||
struct buffer source_buffer[1];
|
||||
|
||||
bool running;
|
||||
pthread_t thread;
|
||||
|
||||
struct spa_source sources[16];
|
||||
unsigned int n_sources;
|
||||
|
||||
bool rebuild_fds;
|
||||
struct pollfd fds[16];
|
||||
unsigned int n_fds;
|
||||
};
|
||||
|
||||
#define BUFFER_SIZE 4096
|
||||
|
||||
static void
|
||||
init_buffer(struct data *data, struct spa_buffer **bufs, struct buffer *ba, int n_buffers,
|
||||
size_t size)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < n_buffers; i++) {
|
||||
struct buffer *b = &ba[i];
|
||||
bufs[i] = &b->buffer;
|
||||
|
||||
b->buffer.metas = b->metas;
|
||||
b->buffer.n_metas = 1;
|
||||
b->buffer.datas = b->datas;
|
||||
b->buffer.n_datas = 1;
|
||||
|
||||
b->header.flags = 0;
|
||||
b->header.seq = 0;
|
||||
b->header.pts = 0;
|
||||
b->header.dts_offset = 0;
|
||||
b->metas[0].type = SPA_META_Header;
|
||||
b->metas[0].data = &b->header;
|
||||
b->metas[0].size = sizeof(b->header);
|
||||
|
||||
b->datas[0].type = SPA_DATA_MemPtr;
|
||||
b->datas[0].flags = 0;
|
||||
b->datas[0].fd = -1;
|
||||
b->datas[0].mapoffset = 0;
|
||||
b->datas[0].maxsize = size;
|
||||
b->datas[0].data = malloc(size);
|
||||
b->datas[0].chunk = &b->chunks[0];
|
||||
b->datas[0].chunk->offset = 0;
|
||||
b->datas[0].chunk->size = 0;
|
||||
b->datas[0].chunk->stride = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static int make_node(struct data *data, struct spa_node **node, const char *lib, const char *name)
|
||||
{
|
||||
struct spa_handle *handle;
|
||||
int res;
|
||||
void *hnd;
|
||||
spa_handle_factory_enum_func_t enum_func;
|
||||
uint32_t i;
|
||||
|
||||
if ((hnd = dlopen(lib, RTLD_NOW)) == NULL) {
|
||||
printf("can't load %s: %s\n", lib, dlerror());
|
||||
return -errno;
|
||||
}
|
||||
if ((enum_func = dlsym(hnd, SPA_HANDLE_FACTORY_ENUM_FUNC_NAME)) == NULL) {
|
||||
printf("can't find enum function\n");
|
||||
return -errno;
|
||||
}
|
||||
|
||||
for (i = 0;;) {
|
||||
const struct spa_handle_factory *factory;
|
||||
void *iface;
|
||||
|
||||
if ((res = enum_func(&factory, &i)) <= 0) {
|
||||
if (res != 0)
|
||||
printf("can't enumerate factories: %s\n", spa_strerror(res));
|
||||
break;
|
||||
}
|
||||
if (strcmp(factory->name, name))
|
||||
continue;
|
||||
|
||||
handle = calloc(1, spa_handle_factory_get_size(factory, NULL));
|
||||
if ((res =
|
||||
spa_handle_factory_init(factory, handle, NULL, data->support,
|
||||
data->n_support)) < 0) {
|
||||
printf("can't make factory instance: %d\n", res);
|
||||
return res;
|
||||
}
|
||||
if ((res = spa_handle_get_interface(handle, SPA_TYPE_INTERFACE_Node, &iface)) < 0) {
|
||||
printf("can't get interface %d\n", res);
|
||||
return res;
|
||||
}
|
||||
*node = iface;
|
||||
return 0;
|
||||
}
|
||||
return -EBADF;
|
||||
}
|
||||
|
||||
static void on_sink_done(void *data, int seq, int res)
|
||||
{
|
||||
printf("got done %d %d\n", seq, res);
|
||||
}
|
||||
|
||||
static void on_sink_event(void *data, struct spa_event *event)
|
||||
{
|
||||
printf("got event %d\n", SPA_EVENT_TYPE(event));
|
||||
}
|
||||
|
||||
static void on_sink_process(void *_data, int status)
|
||||
{
|
||||
struct data *data = _data;
|
||||
int res;
|
||||
|
||||
res = spa_node_process(data->source);
|
||||
if (res != SPA_STATUS_HAVE_BUFFER)
|
||||
printf("got process error from source %d\n", res);
|
||||
if ((res = spa_node_process(data->sink)) < 0)
|
||||
printf("got process error from sink %d\n", res);
|
||||
}
|
||||
|
||||
static void
|
||||
on_sink_reuse_buffer(void *_data,
|
||||
uint32_t port_id,
|
||||
uint32_t buffer_id)
|
||||
{
|
||||
struct data *data = _data;
|
||||
data->source_sink_io[0].buffer_id = buffer_id;
|
||||
}
|
||||
|
||||
static const struct spa_node_callbacks sink_callbacks = {
|
||||
SPA_VERSION_NODE_CALLBACKS,
|
||||
.done = on_sink_done,
|
||||
.event = on_sink_event,
|
||||
.process = on_sink_process,
|
||||
.reuse_buffer = on_sink_reuse_buffer
|
||||
};
|
||||
|
||||
static int do_add_source(struct spa_loop *loop, struct spa_source *source)
|
||||
{
|
||||
struct data *data = SPA_CONTAINER_OF(loop, struct data, data_loop);
|
||||
|
||||
data->sources[data->n_sources] = *source;
|
||||
data->n_sources++;
|
||||
data->rebuild_fds = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int do_update_source(struct spa_source *source)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void do_remove_source(struct spa_source *source)
|
||||
{
|
||||
}
|
||||
|
||||
static int
|
||||
do_invoke(struct spa_loop *loop,
|
||||
spa_invoke_func_t func, uint32_t seq, const void *data, size_t size, bool block, void *user_data)
|
||||
{
|
||||
return func(loop, false, seq, data, size, user_data);
|
||||
}
|
||||
|
||||
static int make_nodes(struct data *data, const char *device)
|
||||
{
|
||||
int res;
|
||||
struct spa_pod *props;
|
||||
struct spa_pod_builder b = { 0 };
|
||||
uint8_t buffer[128];
|
||||
|
||||
if ((res = make_node(data, &data->sink,
|
||||
"build/spa/plugins/alsa/libspa-alsa.so", "alsa-sink")) < 0) {
|
||||
printf("can't create alsa-sink: %d\n", res);
|
||||
return res;
|
||||
}
|
||||
spa_node_set_callbacks(data->sink, &sink_callbacks, data);
|
||||
|
||||
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
||||
props = spa_pod_builder_add_object(&b,
|
||||
SPA_TYPE_OBJECT_Props, 0,
|
||||
SPA_PROP_device, SPA_POD_String(device ? device : "hw:0"),
|
||||
SPA_PROP_minLatency, SPA_POD_Int(64));
|
||||
|
||||
if ((res = spa_node_set_param(data->sink, SPA_PARAM_Props, 0, props)) < 0)
|
||||
printf("got set_props error %d\n", res);
|
||||
|
||||
if ((res = make_node(data, &data->source,
|
||||
"build/spa/plugins/audiotestsrc/libspa-audiotestsrc.so",
|
||||
"audiotestsrc")) < 0) {
|
||||
printf("can't create audiotestsrc: %d\n", res);
|
||||
return res;
|
||||
}
|
||||
|
||||
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
||||
props = spa_pod_builder_add_object(&b,
|
||||
SPA_TYPE_OBJECT_Props, 0,
|
||||
SPA_PROP_live, SPA_POD_Bool(false));
|
||||
|
||||
if ((res = spa_node_set_param(data->source, SPA_PARAM_Props, 0, props)) < 0)
|
||||
printf("got set_props error %d\n", res);
|
||||
return res;
|
||||
}
|
||||
|
||||
static int negotiate_formats(struct data *data)
|
||||
{
|
||||
int res;
|
||||
struct spa_pod *format, *filter;
|
||||
uint32_t state = 0;
|
||||
struct spa_pod_builder b = { 0 };
|
||||
uint8_t buffer[4096];
|
||||
|
||||
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
||||
|
||||
filter = spa_format_audio_raw_build(&b, 0,
|
||||
&SPA_AUDIO_INFO_RAW_INIT(
|
||||
.format = SPA_AUDIO_FORMAT_S16,
|
||||
.rate = 44100,
|
||||
.channels = 2 ));
|
||||
|
||||
if ((res = spa_node_port_enum_params(data->sink,
|
||||
SPA_DIRECTION_INPUT, 0,
|
||||
SPA_PARAM_EnumFormat, &state,
|
||||
filter, &format, &b)) <= 0)
|
||||
return -EBADF;
|
||||
|
||||
if ((res = spa_node_port_set_param(data->sink,
|
||||
SPA_DIRECTION_INPUT, 0,
|
||||
SPA_PARAM_Format, 0,
|
||||
format)) < 0)
|
||||
return res;
|
||||
|
||||
data->source_sink_io[0] = SPA_IO_BUFFERS_INIT;
|
||||
|
||||
spa_node_port_set_io(data->source,
|
||||
SPA_DIRECTION_OUTPUT, 0,
|
||||
SPA_IO_Buffers,
|
||||
&data->source_sink_io[0], sizeof(data->source_sink_io[0]));
|
||||
spa_node_port_set_io(data->sink,
|
||||
SPA_DIRECTION_INPUT, 0,
|
||||
SPA_IO_Buffers,
|
||||
&data->source_sink_io[0], sizeof(data->source_sink_io[0]));
|
||||
|
||||
if ((res = spa_node_port_set_param(data->source,
|
||||
SPA_DIRECTION_OUTPUT, 0,
|
||||
SPA_PARAM_Format, 0,
|
||||
format)) < 0)
|
||||
return res;
|
||||
|
||||
init_buffer(data, data->source_buffers, data->source_buffer, 1, BUFFER_SIZE);
|
||||
if ((res =
|
||||
spa_node_port_use_buffers(data->sink, SPA_DIRECTION_INPUT, 0, data->source_buffers,
|
||||
1)) < 0)
|
||||
return res;
|
||||
if ((res =
|
||||
spa_node_port_use_buffers(data->source, SPA_DIRECTION_OUTPUT, 0, data->source_buffers,
|
||||
1)) < 0)
|
||||
return res;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void *loop(void *user_data)
|
||||
{
|
||||
struct data *data = user_data;
|
||||
|
||||
printf("enter thread %d\n", data->n_sources);
|
||||
while (data->running) {
|
||||
uint32_t i;
|
||||
int r;
|
||||
|
||||
/* rebuild */
|
||||
if (data->rebuild_fds) {
|
||||
for (i = 0; i < data->n_sources; i++) {
|
||||
struct spa_source *p = &data->sources[i];
|
||||
data->fds[i].fd = p->fd;
|
||||
data->fds[i].events = p->mask;
|
||||
}
|
||||
data->n_fds = data->n_sources;
|
||||
data->rebuild_fds = false;
|
||||
}
|
||||
|
||||
r = poll((struct pollfd *) data->fds, data->n_fds, -1);
|
||||
if (r < 0) {
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
break;
|
||||
}
|
||||
if (r == 0) {
|
||||
fprintf(stderr, "select timeout");
|
||||
break;
|
||||
}
|
||||
|
||||
/* after */
|
||||
for (i = 0; i < data->n_sources; i++) {
|
||||
struct spa_source *p = &data->sources[i];
|
||||
p->rmask = 0;
|
||||
if (data->fds[i].revents & POLLIN)
|
||||
p->rmask |= SPA_IO_IN;
|
||||
if (data->fds[i].revents & POLLOUT)
|
||||
p->rmask |= SPA_IO_OUT;
|
||||
if (data->fds[i].revents & POLLHUP)
|
||||
p->rmask |= SPA_IO_HUP;
|
||||
if (data->fds[i].revents & POLLERR)
|
||||
p->rmask |= SPA_IO_ERR;
|
||||
}
|
||||
for (i = 0; i < data->n_sources; i++) {
|
||||
struct spa_source *p = &data->sources[i];
|
||||
if (p->rmask)
|
||||
p->func(p);
|
||||
}
|
||||
}
|
||||
printf("leave thread\n");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void run_async_sink(struct data *data)
|
||||
{
|
||||
int res;
|
||||
int err;
|
||||
|
||||
{
|
||||
struct spa_command cmd = SPA_NODE_COMMAND_INIT(SPA_NODE_COMMAND_Start);
|
||||
if ((res = spa_node_send_command(data->source, &cmd)) < 0)
|
||||
printf("got source error %d\n", res);
|
||||
if ((res = spa_node_send_command(data->sink, &cmd)) < 0)
|
||||
printf("got sink error %d\n", res);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
printf("sleeping for 1000 seconds\n");
|
||||
sleep(1000);
|
||||
|
||||
if (data->running) {
|
||||
data->running = false;
|
||||
pthread_join(data->thread, NULL);
|
||||
}
|
||||
|
||||
{
|
||||
struct spa_command cmd = SPA_NODE_COMMAND_INIT(SPA_NODE_COMMAND_Pause);
|
||||
if ((res = spa_node_send_command(data->sink, &cmd)) < 0)
|
||||
printf("got sink error %d\n", res);
|
||||
if ((res = spa_node_send_command(data->source, &cmd)) < 0)
|
||||
printf("got source error %d\n", res);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct data data = { NULL };
|
||||
int res;
|
||||
const char *str;
|
||||
|
||||
data.log = &default_log.log;
|
||||
data.data_loop.version = SPA_VERSION_LOOP;
|
||||
data.data_loop.add_source = do_add_source;
|
||||
data.data_loop.update_source = do_update_source;
|
||||
data.data_loop.remove_source = do_remove_source;
|
||||
data.data_loop.invoke = do_invoke;
|
||||
|
||||
if ((str = getenv("SPA_DEBUG")))
|
||||
data.log->level = atoi(str);
|
||||
|
||||
data.support[0] = SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_Log, data.log);
|
||||
data.support[1] = SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_MainLoop, &data.data_loop);
|
||||
data.support[2] = SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_DataLoop, &data.data_loop);
|
||||
data.n_support = 3;
|
||||
|
||||
if ((res = make_nodes(&data, argc > 1 ? argv[1] : NULL)) < 0) {
|
||||
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_sink(&data);
|
||||
}
|
||||
|
|
@ -1,581 +0,0 @@
|
|||
/* Spa
|
||||
*
|
||||
* Copyright © 2018 Wim Taymans
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice (including the next
|
||||
* paragraph) shall be included in all copies or substantial portions of the
|
||||
* Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <dlfcn.h>
|
||||
#include <poll.h>
|
||||
#include <pthread.h>
|
||||
#include <errno.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
#include <spa/support/log-impl.h>
|
||||
#include <spa/support/loop.h>
|
||||
#include <spa/node/node.h>
|
||||
#include <spa/node/io.h>
|
||||
#include <spa/param/param.h>
|
||||
#include <spa/param/props.h>
|
||||
#include <spa/param/video/format-utils.h>
|
||||
|
||||
static SPA_LOG_IMPL(default_log);
|
||||
|
||||
|
||||
#define MAX_BUFFERS 8
|
||||
|
||||
struct buffer {
|
||||
struct spa_buffer buffer;
|
||||
struct spa_meta metas[1];
|
||||
struct spa_meta_header header;
|
||||
struct spa_data datas[1];
|
||||
struct spa_chunk chunks[1];
|
||||
SDL_Texture *texture;
|
||||
};
|
||||
|
||||
struct data {
|
||||
struct spa_log *log;
|
||||
struct spa_loop data_loop;
|
||||
|
||||
struct spa_support support[4];
|
||||
uint32_t n_support;
|
||||
|
||||
struct spa_node *source;
|
||||
struct spa_io_buffers source_output[1];
|
||||
|
||||
SDL_Renderer *renderer;
|
||||
SDL_Window *window;
|
||||
SDL_Texture *texture;
|
||||
|
||||
bool use_buffer;
|
||||
|
||||
bool running;
|
||||
pthread_t thread;
|
||||
|
||||
struct spa_source sources[16];
|
||||
unsigned int n_sources;
|
||||
|
||||
bool rebuild_fds;
|
||||
struct pollfd fds[16];
|
||||
unsigned int n_fds;
|
||||
|
||||
struct spa_buffer *bp[MAX_BUFFERS];
|
||||
struct buffer buffers[MAX_BUFFERS];
|
||||
unsigned int n_buffers;
|
||||
};
|
||||
|
||||
static int make_node(struct data *data, struct spa_node **node, const char *lib, const char *name)
|
||||
{
|
||||
struct spa_handle *handle;
|
||||
int res;
|
||||
void *hnd;
|
||||
spa_handle_factory_enum_func_t enum_func;
|
||||
uint32_t i;
|
||||
|
||||
if ((hnd = dlopen(lib, RTLD_NOW)) == NULL) {
|
||||
printf("can't load %s: %s\n", lib, dlerror());
|
||||
return -errno;
|
||||
}
|
||||
if ((enum_func = dlsym(hnd, SPA_HANDLE_FACTORY_ENUM_FUNC_NAME)) == NULL) {
|
||||
printf("can't find enum function\n");
|
||||
return -errno;
|
||||
}
|
||||
|
||||
for (i = 0;;) {
|
||||
const struct spa_handle_factory *factory;
|
||||
void *iface;
|
||||
|
||||
if ((res = enum_func(&factory, &i)) <= 0) {
|
||||
if (res != 0)
|
||||
printf("can't enumerate factories: %s\n", spa_strerror(res));
|
||||
break;
|
||||
}
|
||||
if (strcmp(factory->name, name))
|
||||
continue;
|
||||
|
||||
handle = calloc(1, spa_handle_factory_get_size(factory, NULL));
|
||||
if ((res =
|
||||
spa_handle_factory_init(factory, handle, NULL, data->support,
|
||||
data->n_support)) < 0) {
|
||||
printf("can't make factory instance: %d\n", res);
|
||||
return res;
|
||||
}
|
||||
if ((res = spa_handle_get_interface(handle, SPA_TYPE_INTERFACE_Node, &iface)) < 0) {
|
||||
printf("can't get interface %d\n", res);
|
||||
return res;
|
||||
}
|
||||
*node = iface;
|
||||
return 0;
|
||||
}
|
||||
return -EBADF;
|
||||
}
|
||||
|
||||
static void handle_events(struct data *data)
|
||||
{
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event)) {
|
||||
switch (event.type) {
|
||||
case SDL_QUIT:
|
||||
exit(0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void on_source_done(void *data, int seq, int res)
|
||||
{
|
||||
printf("got done %d %d\n", seq, res);
|
||||
}
|
||||
|
||||
static void on_source_event(void *_data, struct spa_event *event)
|
||||
{
|
||||
struct data *data = _data;
|
||||
|
||||
handle_events(data);
|
||||
|
||||
printf("got event %d\n", SPA_EVENT_TYPE(event));
|
||||
}
|
||||
|
||||
static void on_source_process(void *_data, int status)
|
||||
{
|
||||
struct data *data = _data;
|
||||
int res;
|
||||
struct buffer *b;
|
||||
void *sdata, *ddata;
|
||||
int sstride, dstride;
|
||||
int i;
|
||||
uint8_t *src, *dst;
|
||||
struct spa_data *datas;
|
||||
struct spa_io_buffers *io = &data->source_output[0];
|
||||
|
||||
handle_events(data);
|
||||
|
||||
if ((res = spa_node_process(data->source)) < 0)
|
||||
printf("got process error %d\n", res);
|
||||
|
||||
if (io->buffer_id > MAX_BUFFERS)
|
||||
return;
|
||||
|
||||
b = &data->buffers[io->buffer_id];
|
||||
|
||||
datas = b->buffer.datas;
|
||||
|
||||
if (b->texture) {
|
||||
SDL_Texture *texture = b->texture;
|
||||
|
||||
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 = SPA_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 {
|
||||
uint8_t *map;
|
||||
|
||||
if (SDL_LockTexture(data->texture, NULL, &ddata, &dstride) < 0) {
|
||||
fprintf(stderr, "Couldn't lock texture: %s\n", SDL_GetError());
|
||||
return;
|
||||
}
|
||||
sdata = datas[0].data;
|
||||
if (datas[0].type == SPA_DATA_MemFd ||
|
||||
datas[0].type == SPA_DATA_DmaBuf) {
|
||||
map = mmap(NULL, datas[0].maxsize + datas[0].mapoffset, PROT_READ,
|
||||
MAP_PRIVATE, datas[0].fd, 0);
|
||||
sdata = SPA_MEMBER(map, datas[0].mapoffset, uint8_t);
|
||||
} else if (datas[0].type == SPA_DATA_MemPtr) {
|
||||
map = NULL;
|
||||
sdata = datas[0].data;
|
||||
} else
|
||||
return;
|
||||
|
||||
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);
|
||||
|
||||
if (map)
|
||||
munmap(map, datas[0].maxsize + datas[0].mapoffset);
|
||||
}
|
||||
|
||||
io->status = SPA_STATUS_NEED_BUFFER;
|
||||
}
|
||||
|
||||
static const struct spa_node_callbacks source_callbacks = {
|
||||
SPA_VERSION_NODE_CALLBACKS,
|
||||
.done = on_source_done,
|
||||
.event = on_source_event,
|
||||
.process = on_source_process
|
||||
};
|
||||
|
||||
static int do_add_source(struct spa_loop *loop, struct spa_source *source)
|
||||
{
|
||||
struct data *data = SPA_CONTAINER_OF(loop, struct data, data_loop);
|
||||
|
||||
data->sources[data->n_sources] = *source;
|
||||
data->n_sources++;
|
||||
data->rebuild_fds = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int do_update_source(struct spa_source *source)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void do_remove_source(struct spa_source *source)
|
||||
{
|
||||
}
|
||||
|
||||
static int
|
||||
do_invoke(struct spa_loop *loop,
|
||||
spa_invoke_func_t func, uint32_t seq, const void *data, size_t size, bool block, void *user_data)
|
||||
{
|
||||
return func(loop, false, seq, data, size, user_data);
|
||||
}
|
||||
|
||||
static int make_nodes(struct data *data, const char *device)
|
||||
{
|
||||
int res;
|
||||
struct spa_pod *props;
|
||||
struct spa_pod_builder b = { 0 };
|
||||
uint8_t buffer[256];
|
||||
|
||||
if ((res =
|
||||
make_node(data, &data->source, "build/spa/plugins/v4l2/libspa-v4l2.so",
|
||||
"v4l2-source")) < 0) {
|
||||
printf("can't create v4l2-source: %d\n", res);
|
||||
return res;
|
||||
}
|
||||
|
||||
spa_node_set_callbacks(data->source, &source_callbacks, data);
|
||||
|
||||
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
||||
props = spa_pod_builder_add_object(&b,
|
||||
SPA_TYPE_OBJECT_Props, 0,
|
||||
SPA_PROP_device, SPA_POD_String(device ? device : "/dev/video0"));
|
||||
|
||||
if ((res = spa_node_set_param(data->source, SPA_PARAM_Props, 0, props)) < 0)
|
||||
printf("got set_props error %d\n", res);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static int setup_buffers(struct data *data)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < MAX_BUFFERS; i++) {
|
||||
struct buffer *b = &data->buffers[i];
|
||||
|
||||
data->bp[i] = &b->buffer;
|
||||
|
||||
b->texture = NULL;
|
||||
|
||||
b->buffer.metas = b->metas;
|
||||
b->buffer.n_metas = 1;
|
||||
b->buffer.datas = b->datas;
|
||||
b->buffer.n_datas = 1;
|
||||
|
||||
b->header.flags = 0;
|
||||
b->header.seq = 0;
|
||||
b->header.pts = 0;
|
||||
b->header.dts_offset = 0;
|
||||
b->metas[0].type = SPA_META_Header;
|
||||
b->metas[0].data = &b->header;
|
||||
b->metas[0].size = sizeof(b->header);
|
||||
|
||||
b->datas[0].type = 0;
|
||||
b->datas[0].flags = 0;
|
||||
b->datas[0].fd = -1;
|
||||
b->datas[0].mapoffset = 0;
|
||||
b->datas[0].maxsize = 0;
|
||||
b->datas[0].data = NULL;
|
||||
b->datas[0].chunk = &b->chunks[0];
|
||||
b->datas[0].chunk->offset = 0;
|
||||
b->datas[0].chunk->size = 0;
|
||||
b->datas[0].chunk->stride = 0;
|
||||
}
|
||||
data->n_buffers = MAX_BUFFERS;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int sdl_alloc_buffers(struct data *data)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < MAX_BUFFERS; i++) {
|
||||
struct buffer *b = &data->buffers[i];
|
||||
SDL_Texture *texture;
|
||||
void *ptr;
|
||||
int stride;
|
||||
|
||||
texture = SDL_CreateTexture(data->renderer,
|
||||
SDL_PIXELFORMAT_YUY2,
|
||||
SDL_TEXTUREACCESS_STREAMING, 320, 240);
|
||||
if (!texture) {
|
||||
printf("can't create texture: %s\n", SDL_GetError());
|
||||
return -ENOMEM;
|
||||
}
|
||||
if (SDL_LockTexture(texture, NULL, &ptr, &stride) < 0) {
|
||||
fprintf(stderr, "Couldn't lock texture: %s\n", SDL_GetError());
|
||||
return -EIO;
|
||||
}
|
||||
b->texture = texture;
|
||||
|
||||
b->datas[0].type = SPA_DATA_MemPtr;
|
||||
b->datas[0].maxsize = stride * 240;
|
||||
b->datas[0].data = ptr;
|
||||
b->datas[0].chunk->offset = 0;
|
||||
b->datas[0].chunk->size = stride * 240;
|
||||
b->datas[0].chunk->stride = stride;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int negotiate_formats(struct data *data)
|
||||
{
|
||||
int res;
|
||||
const struct spa_port_info *info;
|
||||
struct spa_pod *format;
|
||||
uint8_t buffer[256];
|
||||
struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buffer, sizeof(buffer));
|
||||
|
||||
data->source_output[0] = SPA_IO_BUFFERS_INIT;
|
||||
|
||||
if ((res =
|
||||
spa_node_port_set_io(data->source,
|
||||
SPA_DIRECTION_OUTPUT, 0,
|
||||
SPA_IO_Buffers,
|
||||
&data->source_output[0], sizeof(data->source_output[0]))) < 0)
|
||||
return res;
|
||||
|
||||
format = spa_format_video_raw_build(&b, 0,
|
||||
&SPA_VIDEO_INFO_RAW_INIT(
|
||||
.format = SPA_VIDEO_FORMAT_YUY2,
|
||||
.size = SPA_RECTANGLE(320, 240),
|
||||
.framerate = SPA_FRACTION(25,1)));
|
||||
|
||||
if ((res = spa_node_port_set_param(data->source,
|
||||
SPA_DIRECTION_OUTPUT, 0,
|
||||
SPA_PARAM_Format, 0,
|
||||
format)) < 0)
|
||||
return res;
|
||||
|
||||
if ((res = spa_node_port_get_info(data->source, SPA_DIRECTION_OUTPUT, 0, &info)) < 0)
|
||||
return res;
|
||||
|
||||
|
||||
setup_buffers(data);
|
||||
|
||||
if (data->use_buffer) {
|
||||
if ((res = sdl_alloc_buffers(data)) < 0)
|
||||
return res;
|
||||
|
||||
if ((res = spa_node_port_use_buffers(data->source, SPA_DIRECTION_OUTPUT, 0, data->bp,
|
||||
data->n_buffers)) < 0) {
|
||||
printf("can't allocate buffers: %s\n", spa_strerror(res));
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
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, SPA_DIRECTION_OUTPUT, 0, NULL, 0,
|
||||
data->bp, &n_buffers)) < 0) {
|
||||
printf("can't allocate buffers: %s\n", spa_strerror(res));
|
||||
return -1;
|
||||
}
|
||||
data->n_buffers = n_buffers;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void *loop(void *user_data)
|
||||
{
|
||||
struct data *data = user_data;
|
||||
|
||||
printf("enter thread\n");
|
||||
while (data->running) {
|
||||
int r;
|
||||
unsigned int i;
|
||||
|
||||
/* rebuild */
|
||||
if (data->rebuild_fds) {
|
||||
for (i = 0; i < data->n_sources; i++) {
|
||||
struct spa_source *p = &data->sources[i];
|
||||
data->fds[i].fd = p->fd;
|
||||
data->fds[i].events = p->mask;
|
||||
}
|
||||
data->n_fds = data->n_sources;
|
||||
data->rebuild_fds = false;
|
||||
}
|
||||
|
||||
r = poll((struct pollfd *) data->fds, data->n_fds, -1);
|
||||
if (r < 0) {
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
break;
|
||||
}
|
||||
if (r == 0) {
|
||||
fprintf(stderr, "select timeout");
|
||||
break;
|
||||
}
|
||||
|
||||
/* after */
|
||||
for (i = 0; i < data->n_sources; i++) {
|
||||
struct spa_source *p = &data->sources[i];
|
||||
p->rmask = 0;
|
||||
if (data->fds[i].revents & POLLIN)
|
||||
p->rmask |= SPA_IO_IN;
|
||||
if (data->fds[i].revents & POLLOUT)
|
||||
p->rmask |= SPA_IO_OUT;
|
||||
if (data->fds[i].revents & POLLHUP)
|
||||
p->rmask |= SPA_IO_HUP;
|
||||
if (data->fds[i].revents & POLLERR)
|
||||
p->rmask |= SPA_IO_ERR;
|
||||
}
|
||||
for (i = 0; i < data->n_sources; i++) {
|
||||
struct spa_source *p = &data->sources[i];
|
||||
if (p->rmask)
|
||||
p->func(p);
|
||||
}
|
||||
}
|
||||
printf("leave thread\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void run_async_source(struct data *data)
|
||||
{
|
||||
int res;
|
||||
int err;
|
||||
|
||||
{
|
||||
struct spa_command cmd = SPA_NODE_COMMAND_INIT(SPA_NODE_COMMAND_Start);
|
||||
if ((res = spa_node_send_command(data->source, &cmd)) < 0)
|
||||
printf("got error %d\n", res);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
sleep(10000);
|
||||
|
||||
if (data->running) {
|
||||
data->running = false;
|
||||
pthread_join(data->thread, NULL);
|
||||
}
|
||||
|
||||
{
|
||||
struct spa_command cmd = SPA_NODE_COMMAND_INIT(SPA_NODE_COMMAND_Pause);
|
||||
if ((res = spa_node_send_command(data->source, &cmd)) < 0)
|
||||
printf("got error %d\n", res);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct data data = { 0 };
|
||||
int res;
|
||||
const char *str;
|
||||
|
||||
data.use_buffer = true;
|
||||
|
||||
data.log = &default_log.log;
|
||||
|
||||
if ((str = getenv("SPA_DEBUG")))
|
||||
data.log->level = atoi(str);
|
||||
|
||||
data.data_loop.version = SPA_VERSION_LOOP;
|
||||
data.data_loop.add_source = do_add_source;
|
||||
data.data_loop.update_source = do_update_source;
|
||||
data.data_loop.remove_source = do_remove_source;
|
||||
data.data_loop.invoke = do_invoke;
|
||||
|
||||
data.support[0] = SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_Log, data.log);
|
||||
data.support[1] = SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_MainLoop, &data.data_loop);
|
||||
data.support[2] = SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_DataLoop, &data.data_loop);
|
||||
data.n_support = 3;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
if ((res = make_nodes(&data, argv[1])) < 0) {
|
||||
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);
|
||||
|
||||
SDL_DestroyRenderer(data.renderer);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue