mirror of
https://gitlab.freedesktop.org/pipewire/pipewire.git
synced 2025-11-02 09:01:50 -05:00
device: add device object
Make a device object, let the v4l2 monitor create device objects The device object is responsible for dynamically creating nodes.
This commit is contained in:
parent
79253c6d46
commit
e1bd12e599
43 changed files with 1784 additions and 237 deletions
|
|
@ -1,4 +1,5 @@
|
|||
v4l2_sources = ['v4l2.c',
|
||||
'v4l2-device.c',
|
||||
'v4l2-monitor.c',
|
||||
'v4l2-source.c']
|
||||
|
||||
|
|
|
|||
218
spa/plugins/v4l2/v4l2-device.c
Normal file
218
spa/plugins/v4l2/v4l2-device.c
Normal file
|
|
@ -0,0 +1,218 @@
|
|||
/* Spa V4l2 Source
|
||||
*
|
||||
* 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 <stddef.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include <linux/videodev2.h>
|
||||
|
||||
#include <spa/support/log.h>
|
||||
#include <spa/support/loop.h>
|
||||
#include <spa/monitor/device.h>
|
||||
#include <spa/debug/pod.h>
|
||||
|
||||
#include "v4l2.h"
|
||||
|
||||
#define NAME "v4l2-device"
|
||||
|
||||
static const char default_device[] = "/dev/video0";
|
||||
|
||||
extern const struct spa_handle_factory spa_v4l2_source_factory;
|
||||
|
||||
struct props {
|
||||
char device[64];
|
||||
char device_name[128];
|
||||
int device_fd;
|
||||
};
|
||||
|
||||
static void reset_props(struct props *props)
|
||||
{
|
||||
strncpy(props->device, default_device, 64);
|
||||
}
|
||||
|
||||
struct impl {
|
||||
struct spa_handle handle;
|
||||
struct spa_device device;
|
||||
|
||||
struct spa_log *log;
|
||||
struct spa_loop *main_loop;
|
||||
|
||||
struct props props;
|
||||
|
||||
const struct spa_device_callbacks *callbacks;
|
||||
void *callbacks_data;
|
||||
|
||||
struct spa_v4l2_device dev;
|
||||
};
|
||||
|
||||
static int impl_device_set_callbacks(struct spa_device *device,
|
||||
const struct spa_device_callbacks *callbacks,
|
||||
void *data)
|
||||
{
|
||||
struct impl *this;
|
||||
struct spa_dict_item items[1];
|
||||
|
||||
spa_return_val_if_fail(device != NULL, -EINVAL);
|
||||
|
||||
this = SPA_CONTAINER_OF(device, struct impl, device);
|
||||
|
||||
this->callbacks = callbacks;
|
||||
this->callbacks_data = data;
|
||||
|
||||
if (callbacks) {
|
||||
if (spa_v4l2_is_capture(&this->dev)) {
|
||||
items[0] = SPA_DICT_ITEM_INIT("device.path", this->props.device);
|
||||
|
||||
if (callbacks->add)
|
||||
callbacks->add(data, 0,
|
||||
&spa_v4l2_source_factory,
|
||||
SPA_TYPE_INTERFACE_Node,
|
||||
&SPA_DICT_INIT(items, 1));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct spa_dict_item info_items[] = {
|
||||
{ "media.class", "Video/Device" },
|
||||
};
|
||||
|
||||
static const struct spa_dict info = {
|
||||
info_items,
|
||||
SPA_N_ELEMENTS(info_items)
|
||||
};
|
||||
|
||||
static const struct spa_device impl_device = {
|
||||
SPA_VERSION_DEVICE,
|
||||
&info,
|
||||
impl_device_set_callbacks,
|
||||
};
|
||||
|
||||
static int impl_get_interface(struct spa_handle *handle, uint32_t type, void **interface)
|
||||
{
|
||||
struct impl *this;
|
||||
|
||||
spa_return_val_if_fail(handle != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(interface != NULL, -EINVAL);
|
||||
|
||||
this = (struct impl *) handle;
|
||||
|
||||
if (type == SPA_TYPE_INTERFACE_Device)
|
||||
*interface = &this->device;
|
||||
else
|
||||
return -ENOENT;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int impl_clear(struct spa_handle *handle)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static size_t
|
||||
impl_get_size(const struct spa_handle_factory *factory,
|
||||
const struct spa_dict *params)
|
||||
{
|
||||
return sizeof(struct impl);
|
||||
}
|
||||
|
||||
static int
|
||||
impl_init(const struct spa_handle_factory *factory,
|
||||
struct spa_handle *handle,
|
||||
const struct spa_dict *info,
|
||||
const struct spa_support *support,
|
||||
uint32_t n_support)
|
||||
{
|
||||
struct impl *this;
|
||||
uint32_t i;
|
||||
const char *str;
|
||||
int res;
|
||||
|
||||
spa_return_val_if_fail(factory != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(handle != NULL, -EINVAL);
|
||||
|
||||
handle->get_interface = impl_get_interface;
|
||||
handle->clear = impl_clear, this = (struct impl *) handle;
|
||||
|
||||
for (i = 0; i < n_support; i++) {
|
||||
if (support[i].type == SPA_TYPE_INTERFACE_Log)
|
||||
this->log = support[i].data;
|
||||
else if (support[i].type == SPA_TYPE_INTERFACE_MainLoop)
|
||||
this->main_loop = support[i].data;
|
||||
}
|
||||
if (this->main_loop == NULL) {
|
||||
spa_log_error(this->log, "a main_loop is needed");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
this->device = impl_device;
|
||||
this->dev.log = this->log;
|
||||
this->dev.fd = -1;
|
||||
|
||||
reset_props(&this->props);
|
||||
|
||||
if (info && (str = spa_dict_lookup(info, "device.path"))) {
|
||||
strncpy(this->props.device, str, 63);
|
||||
if ((res = spa_v4l2_open(&this->dev, this->props.device)) < 0)
|
||||
return res;
|
||||
spa_v4l2_close(&this->dev);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct spa_interface_info impl_interfaces[] = {
|
||||
{SPA_TYPE_INTERFACE_Device,},
|
||||
};
|
||||
|
||||
static int impl_enum_interface_info(const struct spa_handle_factory *factory,
|
||||
const struct spa_interface_info **info,
|
||||
uint32_t *index)
|
||||
{
|
||||
spa_return_val_if_fail(factory != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(info != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(index != NULL, -EINVAL);
|
||||
|
||||
if (*index >= SPA_N_ELEMENTS(impl_interfaces))
|
||||
return 0;
|
||||
|
||||
*info = &impl_interfaces[(*index)++];
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
const struct spa_handle_factory spa_v4l2_device_factory = {
|
||||
SPA_VERSION_HANDLE_FACTORY,
|
||||
NAME,
|
||||
NULL,
|
||||
impl_get_size,
|
||||
impl_init,
|
||||
impl_enum_interface_info,
|
||||
};
|
||||
|
|
@ -39,7 +39,7 @@
|
|||
|
||||
#define NAME "v4l2-monitor"
|
||||
|
||||
extern const struct spa_handle_factory spa_v4l2_source_factory;
|
||||
extern const struct spa_handle_factory spa_v4l2_device_factory;
|
||||
|
||||
struct item {
|
||||
struct udev_device *udevice;
|
||||
|
|
@ -114,8 +114,9 @@ static void fill_item(struct impl *this, struct item *item, struct udev_device *
|
|||
SPA_MONITOR_ITEM_flags, &SPA_POD_Id(SPA_MONITOR_ITEM_FLAG_NONE),
|
||||
SPA_MONITOR_ITEM_state, &SPA_POD_Id(SPA_MONITOR_ITEM_STATE_Available),
|
||||
SPA_MONITOR_ITEM_name, &SPA_POD_Stringv(name),
|
||||
SPA_MONITOR_ITEM_class, &SPA_POD_Stringc("Video/Source"),
|
||||
SPA_MONITOR_ITEM_factory, &SPA_POD_Pointer(SPA_TYPE_INTERFACE_HandleFactory, &spa_v4l2_source_factory),
|
||||
SPA_MONITOR_ITEM_class, &SPA_POD_Stringc("Video/Device"),
|
||||
SPA_MONITOR_ITEM_factory, &SPA_POD_Pointer(SPA_TYPE_INTERFACE_HandleFactory, &spa_v4l2_device_factory),
|
||||
SPA_MONITOR_ITEM_type, &SPA_POD_Id(SPA_TYPE_INTERFACE_Device),
|
||||
0);
|
||||
|
||||
spa_pod_builder_prop(builder, SPA_MONITOR_ITEM_info, 0);
|
||||
|
|
|
|||
|
|
@ -40,6 +40,8 @@
|
|||
#include <spa/control/control.h>
|
||||
#include <spa/debug/pod.h>
|
||||
|
||||
#include "v4l2.h"
|
||||
|
||||
#define NAME "v4l2-source"
|
||||
|
||||
static const char default_device[] = "/dev/video0";
|
||||
|
|
@ -82,7 +84,6 @@ struct port {
|
|||
struct impl *impl;
|
||||
|
||||
bool export_buf;
|
||||
bool started;
|
||||
|
||||
bool next_fmtdesc;
|
||||
struct v4l2_fmtdesc fmtdesc;
|
||||
|
|
@ -94,10 +95,9 @@ struct port {
|
|||
struct spa_video_info current_format;
|
||||
struct spa_fraction rate;
|
||||
|
||||
int fd;
|
||||
bool opened;
|
||||
struct spa_v4l2_device dev;
|
||||
|
||||
bool have_query_ext_ctrl;
|
||||
struct v4l2_capability cap;
|
||||
struct v4l2_format fmt;
|
||||
enum v4l2_buf_type type;
|
||||
enum v4l2_memory memtype;
|
||||
|
|
@ -128,6 +128,7 @@ struct impl {
|
|||
|
||||
struct props props;
|
||||
|
||||
|
||||
const struct spa_node_callbacks *callbacks;
|
||||
void *callbacks_data;
|
||||
|
||||
|
|
@ -604,7 +605,7 @@ static int port_set_format(struct spa_node *node,
|
|||
spa_v4l2_stream_off(this);
|
||||
spa_v4l2_clear_buffers(this);
|
||||
port->have_format = false;
|
||||
spa_v4l2_close(this);
|
||||
spa_v4l2_close(&port->dev);
|
||||
return 0;
|
||||
} else {
|
||||
if ((res = spa_format_parse(format, &info.media_type, &info.media_subtype)) < 0)
|
||||
|
|
@ -839,7 +840,7 @@ static void set_control(struct impl *this, struct port *port, uint32_t control_i
|
|||
spa_zero(c);
|
||||
c.id = control_id;
|
||||
c.value = value;
|
||||
if (ioctl(port->fd, VIDIOC_S_CTRL, &c) < 0)
|
||||
if (ioctl(port->dev.fd, VIDIOC_S_CTRL, &c) < 0)
|
||||
spa_log_error(this->log, "VIDIOC_S_CTRL %m");
|
||||
}
|
||||
|
||||
|
|
@ -1029,12 +1030,14 @@ impl_init(const struct spa_handle_factory *factory,
|
|||
SPA_PORT_INFO_FLAG_TERMINAL;
|
||||
port->export_buf = true;
|
||||
port->have_query_ext_ctrl = true;
|
||||
port->dev.log = this->log;
|
||||
port->dev.fd = -1;
|
||||
|
||||
if (info && (str = spa_dict_lookup(info, "device.path"))) {
|
||||
strncpy(this->props.device, str, 63);
|
||||
if ((res = spa_v4l2_open(this)) < 0)
|
||||
if ((res = spa_v4l2_open(&port->dev, this->props.device)) < 0)
|
||||
return res;
|
||||
spa_v4l2_close(this);
|
||||
spa_v4l2_close(&port->dev);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
|
|||
|
|
@ -44,67 +44,81 @@ static int xioctl(int fd, int request, void *arg)
|
|||
}
|
||||
|
||||
|
||||
static int spa_v4l2_open(struct impl *this)
|
||||
int spa_v4l2_open(struct spa_v4l2_device *dev, const char *path)
|
||||
{
|
||||
struct port *port = &this->out_ports[0];
|
||||
struct stat st;
|
||||
struct props *props = &this->props;
|
||||
int err;
|
||||
|
||||
if (port->opened)
|
||||
if (dev->fd != -1)
|
||||
return 0;
|
||||
|
||||
if (props->device[0] == '\0') {
|
||||
spa_log_error(this->log, "v4l2: Device property not set");
|
||||
if (path == NULL) {
|
||||
spa_log_error(dev->log, "v4l2: Device property not set");
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
spa_log_info(this->log, "v4l2: Playback device is '%s'", props->device);
|
||||
spa_log_info(dev->log, "v4l2: Playback device is '%s'", path);
|
||||
|
||||
if (stat(props->device, &st) < 0) {
|
||||
if (stat(path, &st) < 0) {
|
||||
err = errno;
|
||||
spa_log_error(this->log, "v4l2: Cannot identify '%s': %d, %s",
|
||||
props->device, err, strerror(err));
|
||||
return -err;
|
||||
spa_log_error(dev->log, "v4l2: Cannot identify '%s': %d, %s",
|
||||
path, err, strerror(err));
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (!S_ISCHR(st.st_mode)) {
|
||||
spa_log_error(this->log, "v4l2: %s is no device", props->device);
|
||||
return -ENODEV;
|
||||
spa_log_error(dev->log, "v4l2: %s is no device", path);
|
||||
err = ENODEV;
|
||||
goto error;
|
||||
}
|
||||
|
||||
port->fd = open(props->device, O_RDWR | O_NONBLOCK, 0);
|
||||
dev->fd = open(path, O_RDWR | O_NONBLOCK, 0);
|
||||
|
||||
if (port->fd == -1) {
|
||||
if (dev->fd == -1) {
|
||||
err = errno;
|
||||
spa_log_error(this->log, "v4l2: Cannot open '%s': %d, %s",
|
||||
props->device, err, strerror(err));
|
||||
return -err;
|
||||
spa_log_error(dev->log, "v4l2: Cannot open '%s': %d, %s",
|
||||
path, err, strerror(err));
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (xioctl(port->fd, VIDIOC_QUERYCAP, &port->cap) < 0) {
|
||||
if (xioctl(dev->fd, VIDIOC_QUERYCAP, &dev->cap) < 0) {
|
||||
err = errno;
|
||||
spa_log_error(this->log, "QUERYCAP: %m");
|
||||
return -err;
|
||||
spa_log_error(dev->log, "QUERYCAP: %m");
|
||||
goto error_close;
|
||||
}
|
||||
return 0;
|
||||
|
||||
if ((port->cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0 ||
|
||||
((port->cap.capabilities & V4L2_CAP_DEVICE_CAPS) &&
|
||||
(port->cap.device_caps & V4L2_CAP_VIDEO_CAPTURE) == 0)) {
|
||||
spa_log_error(this->log, "v4l2: %s is no video capture device", props->device);
|
||||
close(port->fd);
|
||||
port->fd = -1;
|
||||
return -ENODEV;
|
||||
error_close:
|
||||
close(dev->fd);
|
||||
dev->fd = -1;
|
||||
error:
|
||||
return -err;
|
||||
}
|
||||
|
||||
int spa_v4l2_is_capture(struct spa_v4l2_device *dev)
|
||||
{
|
||||
if ((dev->cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) == 0 ||
|
||||
((dev->cap.capabilities & V4L2_CAP_DEVICE_CAPS) &&
|
||||
(dev->cap.device_caps & V4L2_CAP_VIDEO_CAPTURE) == 0)) {
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
port->source.func = v4l2_on_fd_events;
|
||||
port->source.data = this;
|
||||
port->source.fd = port->fd;
|
||||
port->source.mask = SPA_IO_IN | SPA_IO_ERR;
|
||||
port->source.rmask = 0;
|
||||
int spa_v4l2_close(struct spa_v4l2_device *dev)
|
||||
{
|
||||
if (dev->fd == -1)
|
||||
return 0;
|
||||
|
||||
port->opened = true;
|
||||
if (dev->active)
|
||||
return 0;
|
||||
|
||||
spa_log_info(dev->log, "v4l2: close");
|
||||
|
||||
if (close(dev->fd))
|
||||
spa_log_warn(dev->log, "close: %m");
|
||||
|
||||
dev->fd = -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -112,6 +126,7 @@ static int spa_v4l2_buffer_recycle(struct impl *this, uint32_t buffer_id)
|
|||
{
|
||||
struct port *port = &this->out_ports[0];
|
||||
struct buffer *b = &port->buffers[buffer_id];
|
||||
struct spa_v4l2_device *dev = &port->dev;
|
||||
int err;
|
||||
|
||||
if (!SPA_FLAG_CHECK(b->flags, BUFFER_FLAG_OUTSTANDING))
|
||||
|
|
@ -120,7 +135,7 @@ static int spa_v4l2_buffer_recycle(struct impl *this, uint32_t buffer_id)
|
|||
SPA_FLAG_UNSET(b->flags, BUFFER_FLAG_OUTSTANDING);
|
||||
spa_log_trace(this->log, "v4l2 %p: recycle buffer %d", this, buffer_id);
|
||||
|
||||
if (xioctl(port->fd, VIDIOC_QBUF, &b->v4l2_buffer) < 0) {
|
||||
if (xioctl(dev->fd, VIDIOC_QBUF, &b->v4l2_buffer) < 0) {
|
||||
err = errno;
|
||||
spa_log_error(this->log, "VIDIOC_QBUF: %m");
|
||||
return -err;
|
||||
|
|
@ -164,7 +179,7 @@ static int spa_v4l2_clear_buffers(struct impl *this)
|
|||
reqbuf.memory = port->memtype;
|
||||
reqbuf.count = 0;
|
||||
|
||||
if (xioctl(port->fd, VIDIOC_REQBUFS, &reqbuf) < 0) {
|
||||
if (xioctl(port->dev.fd, VIDIOC_REQBUFS, &reqbuf) < 0) {
|
||||
spa_log_warn(this->log, "VIDIOC_REQBUFS: %m");
|
||||
}
|
||||
port->n_buffers = 0;
|
||||
|
|
@ -172,26 +187,6 @@ static int spa_v4l2_clear_buffers(struct impl *this)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int spa_v4l2_close(struct impl *this)
|
||||
{
|
||||
struct port *port = &this->out_ports[0];
|
||||
|
||||
if (!port->opened)
|
||||
return 0;
|
||||
|
||||
if (port->have_format)
|
||||
return 0;
|
||||
|
||||
spa_log_info(this->log, "v4l2: close");
|
||||
|
||||
if (close(port->fd))
|
||||
spa_log_warn(this->log, "close: %m");
|
||||
|
||||
port->fd = -1;
|
||||
port->opened = false;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct format_info {
|
||||
uint32_t fourcc;
|
||||
|
|
@ -536,8 +531,9 @@ spa_v4l2_enum_format(struct impl *this,
|
|||
const struct format_info *info;
|
||||
struct spa_pod_choice *choice;
|
||||
uint32_t filter_media_type, filter_media_subtype, video_format;
|
||||
struct spa_v4l2_device *dev = &port->dev;
|
||||
|
||||
if ((res = spa_v4l2_open(this)) < 0)
|
||||
if ((res = spa_v4l2_open(dev, this->props.device)) < 0)
|
||||
return res;
|
||||
|
||||
if (*index == 0) {
|
||||
|
|
@ -578,7 +574,7 @@ spa_v4l2_enum_format(struct impl *this,
|
|||
|
||||
port->fmtdesc.pixelformat = info->fourcc;
|
||||
} else {
|
||||
if ((res = xioctl(port->fd, VIDIOC_ENUM_FMT, &port->fmtdesc)) < 0) {
|
||||
if ((res = xioctl(dev->fd, VIDIOC_ENUM_FMT, &port->fmtdesc)) < 0) {
|
||||
res = -errno;
|
||||
if (errno != EINVAL)
|
||||
spa_log_error(this->log, "VIDIOC_ENUM_FMT: %m");
|
||||
|
|
@ -621,7 +617,7 @@ spa_v4l2_enum_format(struct impl *this,
|
|||
}
|
||||
}
|
||||
do_frmsize:
|
||||
if ((res = xioctl(port->fd, VIDIOC_ENUM_FRAMESIZES, &port->frmsize)) < 0) {
|
||||
if ((res = xioctl(dev->fd, VIDIOC_ENUM_FRAMESIZES, &port->frmsize)) < 0) {
|
||||
if (errno == EINVAL)
|
||||
goto next_fmtdesc;
|
||||
|
||||
|
|
@ -706,7 +702,7 @@ spa_v4l2_enum_format(struct impl *this,
|
|||
port->frmival.index = 0;
|
||||
|
||||
while (true) {
|
||||
if ((res = xioctl(port->fd, VIDIOC_ENUM_FRAMEINTERVALS, &port->frmival)) < 0) {
|
||||
if ((res = xioctl(dev->fd, VIDIOC_ENUM_FRAMEINTERVALS, &port->frmival)) < 0) {
|
||||
res = -errno;
|
||||
if (errno == EINVAL) {
|
||||
port->frmsize.index++;
|
||||
|
|
@ -812,7 +808,7 @@ spa_v4l2_enum_format(struct impl *this,
|
|||
res = 1;
|
||||
|
||||
exit:
|
||||
spa_v4l2_close(this);
|
||||
spa_v4l2_close(dev);
|
||||
|
||||
return res;
|
||||
|
||||
|
|
@ -824,6 +820,7 @@ spa_v4l2_enum_format(struct impl *this,
|
|||
static int spa_v4l2_set_format(struct impl *this, struct spa_video_info *format, bool try_only)
|
||||
{
|
||||
struct port *port = &this->out_ports[0];
|
||||
struct spa_v4l2_device *dev = &port->dev;
|
||||
int res, cmd;
|
||||
struct v4l2_format reqfmt, fmt;
|
||||
struct v4l2_streamparm streamparm;
|
||||
|
|
@ -882,18 +879,18 @@ static int spa_v4l2_set_format(struct impl *this, struct spa_video_info *format,
|
|||
|
||||
reqfmt = fmt;
|
||||
|
||||
if ((res = spa_v4l2_open(this)) < 0)
|
||||
if ((res = spa_v4l2_open(dev, this->props.device)) < 0)
|
||||
return res;
|
||||
|
||||
cmd = try_only ? VIDIOC_TRY_FMT : VIDIOC_S_FMT;
|
||||
if (xioctl(port->fd, cmd, &fmt) < 0) {
|
||||
if (xioctl(dev->fd, cmd, &fmt) < 0) {
|
||||
res = -errno;
|
||||
spa_log_error(this->log, "VIDIOC_S_FMT: %m");
|
||||
return res;
|
||||
}
|
||||
|
||||
/* some cheap USB cam's won't accept any change */
|
||||
if (xioctl(port->fd, VIDIOC_S_PARM, &streamparm) < 0)
|
||||
if (xioctl(dev->fd, VIDIOC_S_PARM, &streamparm) < 0)
|
||||
spa_log_warn(this->log, "VIDIOC_S_PARM: %m");
|
||||
|
||||
spa_log_info(this->log, "v4l2: got %08x %dx%d %d/%d", fmt.fmt.pix.pixelformat,
|
||||
|
|
@ -927,17 +924,18 @@ static int spa_v4l2_set_format(struct impl *this, struct spa_video_info *format,
|
|||
|
||||
static int query_ext_ctrl_ioctl(struct port *port, struct v4l2_query_ext_ctrl *qctrl)
|
||||
{
|
||||
struct spa_v4l2_device *dev = &port->dev;
|
||||
struct v4l2_queryctrl qc;
|
||||
int res;
|
||||
|
||||
if (port->have_query_ext_ctrl) {
|
||||
res = ioctl(port->fd, VIDIOC_QUERY_EXT_CTRL, qctrl);
|
||||
res = xioctl(dev->fd, VIDIOC_QUERY_EXT_CTRL, qctrl);
|
||||
if (errno != ENOTTY)
|
||||
return res;
|
||||
port->have_query_ext_ctrl = false;
|
||||
}
|
||||
qc.id = qctrl->id;
|
||||
res = ioctl(port->fd, VIDIOC_QUERYCTRL, &qc);
|
||||
res = xioctl(dev->fd, VIDIOC_QUERYCTRL, &qc);
|
||||
if (res == 0) {
|
||||
qctrl->type = qc.type;
|
||||
memcpy(qctrl->name, qc.name, sizeof(qctrl->name));
|
||||
|
|
@ -1003,6 +1001,7 @@ spa_v4l2_enum_controls(struct impl *this,
|
|||
struct spa_pod_builder *builder)
|
||||
{
|
||||
struct port *port = &this->out_ports[0];
|
||||
struct spa_v4l2_device *dev = &port->dev;
|
||||
struct v4l2_query_ext_ctrl queryctrl;
|
||||
struct spa_pod *param;
|
||||
struct spa_pod_builder b = { 0 };
|
||||
|
|
@ -1011,7 +1010,7 @@ spa_v4l2_enum_controls(struct impl *this,
|
|||
int res;
|
||||
const unsigned next_fl = V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND;
|
||||
|
||||
if ((res = spa_v4l2_open(this)) < 0)
|
||||
if ((res = spa_v4l2_open(dev, this->props.device)) < 0)
|
||||
return res;
|
||||
|
||||
next:
|
||||
|
|
@ -1109,7 +1108,7 @@ spa_v4l2_enum_controls(struct impl *this,
|
|||
for (querymenu.index = queryctrl.minimum;
|
||||
querymenu.index <= queryctrl.maximum;
|
||||
querymenu.index++) {
|
||||
if (ioctl(port->fd, VIDIOC_QUERYMENU, &querymenu) == 0) {
|
||||
if (xioctl(dev->fd, VIDIOC_QUERYMENU, &querymenu) == 0) {
|
||||
spa_pod_builder_int(&b, querymenu.index);
|
||||
spa_pod_builder_string(&b, (const char *)querymenu.name);
|
||||
}
|
||||
|
|
@ -1133,7 +1132,7 @@ spa_v4l2_enum_controls(struct impl *this,
|
|||
res = 1;
|
||||
|
||||
exit:
|
||||
spa_v4l2_close(this);
|
||||
spa_v4l2_close(dev);
|
||||
|
||||
return res;
|
||||
|
||||
|
|
@ -1145,6 +1144,7 @@ spa_v4l2_enum_controls(struct impl *this,
|
|||
static int mmap_read(struct impl *this)
|
||||
{
|
||||
struct port *port = &this->out_ports[0];
|
||||
struct spa_v4l2_device *dev = &port->dev;
|
||||
struct v4l2_buffer buf;
|
||||
struct buffer *b;
|
||||
struct spa_data *d;
|
||||
|
|
@ -1154,7 +1154,7 @@ static int mmap_read(struct impl *this)
|
|||
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
||||
buf.memory = port->memtype;
|
||||
|
||||
if (xioctl(port->fd, VIDIOC_DQBUF, &buf) < 0)
|
||||
if (xioctl(dev->fd, VIDIOC_DQBUF, &buf) < 0)
|
||||
return -errno;
|
||||
|
||||
pts = SPA_TIMEVAL_TO_NSEC(&buf.timestamp);
|
||||
|
|
@ -1212,6 +1212,7 @@ static void v4l2_on_fd_events(struct spa_source *source)
|
|||
static int spa_v4l2_use_buffers(struct impl *this, struct spa_buffer **buffers, uint32_t n_buffers)
|
||||
{
|
||||
struct port *port = &this->out_ports[0];
|
||||
struct spa_v4l2_device *dev = &port->dev;
|
||||
struct v4l2_requestbuffers reqbuf;
|
||||
int i;
|
||||
struct spa_data *d;
|
||||
|
|
@ -1235,7 +1236,7 @@ static int spa_v4l2_use_buffers(struct impl *this, struct spa_buffer **buffers,
|
|||
reqbuf.memory = port->memtype;
|
||||
reqbuf.count = n_buffers;
|
||||
|
||||
if (xioctl(port->fd, VIDIOC_REQBUFS, &reqbuf) < 0) {
|
||||
if (xioctl(dev->fd, VIDIOC_REQBUFS, &reqbuf) < 0) {
|
||||
spa_log_error(this->log, "v4l2: VIDIOC_REQBUFS %m");
|
||||
return -errno;
|
||||
}
|
||||
|
|
@ -1308,6 +1309,7 @@ mmap_init(struct impl *this,
|
|||
uint32_t *n_buffers)
|
||||
{
|
||||
struct port *port = &this->out_ports[0];
|
||||
struct spa_v4l2_device *dev = &port->dev;
|
||||
struct v4l2_requestbuffers reqbuf;
|
||||
int i;
|
||||
|
||||
|
|
@ -1318,7 +1320,7 @@ mmap_init(struct impl *this,
|
|||
reqbuf.memory = port->memtype;
|
||||
reqbuf.count = *n_buffers;
|
||||
|
||||
if (xioctl(port->fd, VIDIOC_REQBUFS, &reqbuf) < 0) {
|
||||
if (xioctl(dev->fd, VIDIOC_REQBUFS, &reqbuf) < 0) {
|
||||
spa_log_error(this->log, "VIDIOC_REQBUFS: %m");
|
||||
return -errno;
|
||||
}
|
||||
|
|
@ -1352,7 +1354,7 @@ mmap_init(struct impl *this,
|
|||
b->v4l2_buffer.memory = port->memtype;
|
||||
b->v4l2_buffer.index = i;
|
||||
|
||||
if (xioctl(port->fd, VIDIOC_QUERYBUF, &b->v4l2_buffer) < 0) {
|
||||
if (xioctl(dev->fd, VIDIOC_QUERYBUF, &b->v4l2_buffer) < 0) {
|
||||
spa_log_error(this->log, "VIDIOC_QUERYBUF: %m");
|
||||
return -errno;
|
||||
}
|
||||
|
|
@ -1371,7 +1373,7 @@ mmap_init(struct impl *this,
|
|||
expbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
||||
expbuf.index = i;
|
||||
expbuf.flags = O_CLOEXEC | O_RDONLY;
|
||||
if (xioctl(port->fd, VIDIOC_EXPBUF, &expbuf) < 0) {
|
||||
if (xioctl(dev->fd, VIDIOC_EXPBUF, &expbuf) < 0) {
|
||||
spa_log_error(this->log, "VIDIOC_EXPBUF: %m");
|
||||
continue;
|
||||
}
|
||||
|
|
@ -1385,7 +1387,7 @@ mmap_init(struct impl *this,
|
|||
d[0].data = mmap(NULL,
|
||||
b->v4l2_buffer.length,
|
||||
PROT_READ, MAP_SHARED,
|
||||
port->fd,
|
||||
dev->fd,
|
||||
b->v4l2_buffer.m.offset);
|
||||
if (d[0].data == MAP_FAILED) {
|
||||
spa_log_error(this->log, "mmap: %m");
|
||||
|
|
@ -1420,15 +1422,16 @@ spa_v4l2_alloc_buffers(struct impl *this,
|
|||
{
|
||||
int res;
|
||||
struct port *port = &this->out_ports[0];
|
||||
struct spa_v4l2_device *dev = &port->dev;
|
||||
|
||||
if (port->n_buffers > 0)
|
||||
return -EIO;
|
||||
|
||||
if (port->cap.capabilities & V4L2_CAP_STREAMING) {
|
||||
if (dev->cap.capabilities & V4L2_CAP_STREAMING) {
|
||||
if ((res = mmap_init(this, params, n_params, buffers, n_buffers)) < 0)
|
||||
if ((res = userptr_init(this)) < 0)
|
||||
return res;
|
||||
} else if (port->cap.capabilities & V4L2_CAP_READWRITE) {
|
||||
} else if (dev->cap.capabilities & V4L2_CAP_READWRITE) {
|
||||
if ((res = read_init(this)) < 0)
|
||||
return res;
|
||||
} else
|
||||
|
|
@ -1440,25 +1443,31 @@ spa_v4l2_alloc_buffers(struct impl *this,
|
|||
static int spa_v4l2_stream_on(struct impl *this)
|
||||
{
|
||||
struct port *port = &this->out_ports[0];
|
||||
struct spa_v4l2_device *dev = &port->dev;
|
||||
enum v4l2_buf_type type;
|
||||
|
||||
if (!port->opened)
|
||||
if (dev->fd == -1)
|
||||
return -EIO;
|
||||
|
||||
if (port->started)
|
||||
if (dev->active)
|
||||
return 0;
|
||||
|
||||
spa_log_debug(this->log, "starting");
|
||||
|
||||
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
||||
if (xioctl(port->fd, VIDIOC_STREAMON, &type) < 0) {
|
||||
if (xioctl(dev->fd, VIDIOC_STREAMON, &type) < 0) {
|
||||
spa_log_error(this->log, "VIDIOC_STREAMON: %m");
|
||||
return -errno;
|
||||
}
|
||||
|
||||
port->source.func = v4l2_on_fd_events;
|
||||
port->source.data = this;
|
||||
port->source.fd = dev->fd;
|
||||
port->source.mask = SPA_IO_IN | SPA_IO_ERR;
|
||||
port->source.rmask = 0;
|
||||
spa_loop_add_source(this->data_loop, &port->source);
|
||||
|
||||
port->started = true;
|
||||
dev->active = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1479,13 +1488,14 @@ static int do_remove_source(struct spa_loop *loop,
|
|||
static int spa_v4l2_stream_off(struct impl *this)
|
||||
{
|
||||
struct port *port = &this->out_ports[0];
|
||||
struct spa_v4l2_device *dev = &port->dev;
|
||||
enum v4l2_buf_type type;
|
||||
int i;
|
||||
|
||||
if (!port->opened)
|
||||
if (dev->fd == -1)
|
||||
return -EIO;
|
||||
|
||||
if (!port->started)
|
||||
if (!dev->active)
|
||||
return 0;
|
||||
|
||||
spa_log_debug(this->log, "stopping");
|
||||
|
|
@ -1493,7 +1503,7 @@ static int spa_v4l2_stream_off(struct impl *this)
|
|||
spa_loop_invoke(this->data_loop, do_remove_source, 0, NULL, 0, true, port);
|
||||
|
||||
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
||||
if (xioctl(port->fd, VIDIOC_STREAMOFF, &type) < 0) {
|
||||
if (xioctl(dev->fd, VIDIOC_STREAMOFF, &type) < 0) {
|
||||
spa_log_error(this->log, "VIDIOC_STREAMOFF: %m");
|
||||
return -errno;
|
||||
}
|
||||
|
|
@ -1502,12 +1512,12 @@ static int spa_v4l2_stream_off(struct impl *this)
|
|||
|
||||
b = &port->buffers[i];
|
||||
if (!SPA_FLAG_CHECK(b->flags, BUFFER_FLAG_OUTSTANDING)) {
|
||||
if (xioctl(port->fd, VIDIOC_QBUF, &b->v4l2_buffer) < 0)
|
||||
if (xioctl(dev->fd, VIDIOC_QBUF, &b->v4l2_buffer) < 0)
|
||||
spa_log_warn(this->log, "VIDIOC_QBUF: %s", strerror(errno));
|
||||
}
|
||||
}
|
||||
spa_list_init(&port->queue);
|
||||
port->started = false;
|
||||
dev->active = false;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@
|
|||
|
||||
extern const struct spa_handle_factory spa_v4l2_source_factory;
|
||||
extern const struct spa_handle_factory spa_v4l2_monitor_factory;
|
||||
extern const struct spa_handle_factory spa_v4l2_device_factory;
|
||||
|
||||
int
|
||||
spa_handle_factory_enum(const struct spa_handle_factory **factory,
|
||||
|
|
@ -43,6 +44,9 @@ spa_handle_factory_enum(const struct spa_handle_factory **factory,
|
|||
case 1:
|
||||
*factory = &spa_v4l2_monitor_factory;
|
||||
break;
|
||||
case 2:
|
||||
*factory = &spa_v4l2_device_factory;
|
||||
break;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
36
spa/plugins/v4l2/v4l2.h
Normal file
36
spa/plugins/v4l2/v4l2.h
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
/* Spa V4l2 support
|
||||
*
|
||||
* 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 <errno.h>
|
||||
|
||||
struct spa_v4l2_device {
|
||||
struct spa_log *log;
|
||||
int fd;
|
||||
struct v4l2_capability cap;
|
||||
bool active;
|
||||
};
|
||||
|
||||
int spa_v4l2_open(struct spa_v4l2_device *dev, const char *path);
|
||||
int spa_v4l2_close(struct spa_v4l2_device *dev);
|
||||
int spa_v4l2_is_capture(struct spa_v4l2_device *dev);
|
||||
Loading…
Add table
Add a link
Reference in a new issue