pipewire/spa/tests/test-v4l2.c

199 lines
5.1 KiB
C
Raw Normal View History

/* Spa
* Copyright (C) 2016 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <dlfcn.h>
#include <spa/node.h>
#include <spa/video/format.h>
typedef struct {
SpaHandle *source;
const SpaNode *source_node;
} AppData;
static SpaResult
make_node (SpaHandle **handle, const SpaNode **node, const char *lib, const char *name)
{
SpaResult res;
void *hnd;
SpaEnumHandleFactoryFunc enum_func;
unsigned int i;
if ((hnd = dlopen (lib, RTLD_NOW)) == NULL) {
printf ("can't load %s: %s\n", lib, dlerror());
return SPA_RESULT_ERROR;
}
if ((enum_func = dlsym (hnd, "spa_enum_handle_factory")) == NULL) {
printf ("can't find enum function\n");
return SPA_RESULT_ERROR;
}
for (i = 0; ;i++) {
const SpaHandleFactory *factory;
const void *iface;
if ((res = enum_func (i, &factory)) < 0) {
if (res != SPA_RESULT_ENUM_END)
printf ("can't enumerate factories: %d\n", res);
break;
}
if (strcmp (factory->name, name))
continue;
if ((res = factory->instantiate (factory, handle)) < 0) {
printf ("can't make factory instance: %d\n", res);
return res;
}
if ((res = (*handle)->get_interface (*handle, SPA_INTERFACE_ID_NODE, &iface)) < 0) {
printf ("can't get interface %d\n", res);
return res;
}
*node = iface;
return SPA_RESULT_OK;
}
return SPA_RESULT_ERROR;
}
static void
on_source_event (SpaHandle *handle, SpaEvent *event, void *user_data)
{
AppData *data = user_data;
switch (event->type) {
case SPA_EVENT_TYPE_CAN_PULL_OUTPUT:
{
SpaOutputInfo info[1] = { 0, };
SpaResult res;
if ((res = data->source_node->pull_port_output (data->source, 1, info)) < 0)
printf ("got pull error %d\n", res);
if (info[0].buffer) {
spa_buffer_unref (info[0].buffer);
}
break;
}
default:
printf ("got event %d\n", event->type);
break;
}
}
static SpaResult
make_nodes (AppData *data)
{
SpaResult res;
SpaProps *props;
SpaPropValue value;
if ((res = make_node (&data->source, &data->source_node, "plugins/v4l2/libspa-v4l2.so", "v4l2-source")) < 0) {
printf ("can't create v4l2-source: %d\n", res);
return res;
}
data->source_node->set_event_callback (data->source, on_source_event, data);
if ((res = data->source_node->get_props (data->source, &props)) < 0)
printf ("got get_props error %d\n", res);
value.type = SPA_PROP_TYPE_STRING;
value.value = "/dev/video1";
value.size = strlen (value.value)+1;
props->set_prop (props, spa_props_index_for_name (props, "device"), &value);
if ((res = data->source_node->set_props (data->source, props)) < 0)
printf ("got set_props error %d\n", res);
return res;
}
static SpaResult
negotiate_formats (AppData *data)
{
SpaResult res;
SpaFormat *format;
SpaProps *props;
uint32_t val;
SpaPropValue value;
if ((res = data->source_node->enum_port_formats (data->source, 0, 0, &format)) < 0)
return res;
props = &format->props;
value.type = SPA_PROP_TYPE_UINT32;
value.size = sizeof (uint32_t);
value.value = &val;
val = SPA_VIDEO_FORMAT_YUY2;
if ((res = props->set_prop (props, spa_props_index_for_id (props, SPA_PROP_ID_VIDEO_FORMAT), &value)) < 0)
return res;
val = 320;
if ((res = props->set_prop (props, spa_props_index_for_id (props, SPA_PROP_ID_VIDEO_WIDTH), &value)) < 0)
return res;
val = 240;
if ((res = props->set_prop (props, spa_props_index_for_id (props, SPA_PROP_ID_VIDEO_HEIGHT), &value)) < 0)
return res;
if ((res = data->source_node->set_port_format (data->source, 0, false, format)) < 0)
return res;
return SPA_RESULT_OK;
}
static void
run_async_source (AppData *data)
{
SpaResult res;
SpaCommand cmd;
cmd.type = SPA_COMMAND_START;
if ((res = data->source_node->send_command (data->source, &cmd)) < 0)
printf ("got error %d\n", res);
printf ("sleeping for 10 seconds\n");
sleep (10);
cmd.type = SPA_COMMAND_STOP;
if ((res = data->source_node->send_command (data->source, &cmd)) < 0)
printf ("got error %d\n", res);
}
int
main (int argc, char *argv[])
{
AppData data;
SpaResult res;
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_async_source (&data);
return 0;
}