mirror of
https://gitlab.freedesktop.org/pipewire/pipewire.git
synced 2025-11-01 22:58:50 -04:00
v4l2: continue work on the capture device
Remove activate and deactivate commands. Add STEP property range type for v4l2 frame sizes later v4l2: implement negotiation and data capture
This commit is contained in:
parent
beedb65f00
commit
a1a27328e2
17 changed files with 976 additions and 308 deletions
|
|
@ -2,3 +2,8 @@ executable('test-mixer', 'test-mixer.c',
|
|||
include_directories : inc,
|
||||
dependencies : [dl_lib],
|
||||
install : false)
|
||||
|
||||
executable('test-v4l2', 'test-v4l2.c',
|
||||
include_directories : inc,
|
||||
dependencies : [dl_lib],
|
||||
install : false)
|
||||
|
|
|
|||
|
|
@ -239,72 +239,34 @@ negotiate_formats (AppData *data)
|
|||
if ((res = props->set_prop (props, spa_props_index_for_id (props, SPA_PROP_ID_AUDIO_CHANNELS), &value)) < 0)
|
||||
return res;
|
||||
|
||||
if ((res = data->sink_node->set_port_format (data->sink, 0, 0, format)) < 0)
|
||||
if ((res = data->sink_node->set_port_format (data->sink, 0, false, format)) < 0)
|
||||
return res;
|
||||
|
||||
if ((res = data->mix_node->set_port_format (data->mix, 0, 0, format)) < 0)
|
||||
if ((res = data->mix_node->set_port_format (data->mix, 0, false, format)) < 0)
|
||||
return res;
|
||||
|
||||
if ((res = data->mix_node->add_port (data->mix, SPA_DIRECTION_INPUT, &data->mix_ports[0])) < 0)
|
||||
return res;
|
||||
|
||||
if ((res = data->mix_node->set_port_format (data->mix, data->mix_ports[0], 0, format)) < 0)
|
||||
if ((res = data->mix_node->set_port_format (data->mix, data->mix_ports[0], false, format)) < 0)
|
||||
return res;
|
||||
|
||||
if ((res = data->source1_node->set_port_format (data->source1, 0, 0, format)) < 0)
|
||||
if ((res = data->source1_node->set_port_format (data->source1, 0, false, format)) < 0)
|
||||
return res;
|
||||
|
||||
if ((res = data->mix_node->add_port (data->mix, SPA_DIRECTION_INPUT, &data->mix_ports[1])) < 0)
|
||||
return res;
|
||||
|
||||
if ((res = data->mix_node->set_port_format (data->mix, data->mix_ports[1], 0, format)) < 0)
|
||||
if ((res = data->mix_node->set_port_format (data->mix, data->mix_ports[1], false, format)) < 0)
|
||||
return res;
|
||||
|
||||
if ((res = data->source2_node->set_port_format (data->source2, 0, 0, format)) < 0)
|
||||
if ((res = data->source2_node->set_port_format (data->source2, 0, false, format)) < 0)
|
||||
return res;
|
||||
|
||||
|
||||
return SPA_RESULT_OK;
|
||||
}
|
||||
|
||||
static SpaResult
|
||||
start_nodes (AppData *data)
|
||||
{
|
||||
SpaResult res;
|
||||
SpaCommand cmd;
|
||||
|
||||
cmd.type = SPA_COMMAND_ACTIVATE;
|
||||
if ((res = data->sink_node->send_command (data->sink, &cmd)) < 0)
|
||||
return res;
|
||||
if ((res = data->mix_node->send_command (data->mix, &cmd)) < 0)
|
||||
return res;
|
||||
if ((res = data->source1_node->send_command (data->source1, &cmd)) < 0)
|
||||
return res;
|
||||
if ((res = data->source2_node->send_command (data->source1, &cmd)) < 0)
|
||||
return res;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static SpaResult
|
||||
stop_nodes (AppData *data)
|
||||
{
|
||||
SpaResult res;
|
||||
SpaCommand cmd;
|
||||
|
||||
cmd.type = SPA_COMMAND_DEACTIVATE;
|
||||
if ((res = data->sink_node->send_command (data->sink, &cmd)) < 0)
|
||||
return res;
|
||||
if ((res = data->mix_node->send_command (data->mix, &cmd)) < 0)
|
||||
return res;
|
||||
if ((res = data->source1_node->send_command (data->source1, &cmd)) < 0)
|
||||
return res;
|
||||
if ((res = data->source2_node->send_command (data->source1, &cmd)) < 0)
|
||||
return res;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static void
|
||||
run_async_sink (AppData *data)
|
||||
{
|
||||
|
|
@ -337,17 +299,7 @@ main (int argc, char *argv[])
|
|||
printf ("can't negotiate nodes: %d\n", res);
|
||||
return -1;
|
||||
}
|
||||
if ((res = start_nodes (&data)) < 0) {
|
||||
printf ("can't start nodes: %d\n", res);
|
||||
return -1;
|
||||
}
|
||||
|
||||
run_async_sink (&data);
|
||||
|
||||
if ((res = stop_nodes (&data)) < 0) {
|
||||
printf ("can't stop nodes: %d\n", res);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
198
spa/tests/test-v4l2.c
Normal file
198
spa/tests/test-v4l2.c
Normal file
|
|
@ -0,0 +1,198 @@
|
|||
/* 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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue