mirror of
https://github.com/pikvm/ustreamer.git
synced 2026-07-17 17:21:55 +00:00
refactoring
This commit is contained in:
@@ -120,6 +120,12 @@ Delay before trying to connect to the device again after an error (timeout for e
|
|||||||
.TP
|
.TP
|
||||||
.BR \-\-m2m\-device\ \fI/dev/path
|
.BR \-\-m2m\-device\ \fI/dev/path
|
||||||
Path to V4L2 mem-to-mem encoder device. Default: auto-select.
|
Path to V4L2 mem-to-mem encoder device. Default: auto-select.
|
||||||
|
.TP
|
||||||
|
.BR \-\-media\-device \fI/dev/path
|
||||||
|
Path to V4L2 /dev/media* device for setting subdevices (currently necessary for Raspberry Pi 5). Default: unset.
|
||||||
|
.TP
|
||||||
|
.BR \-\-media\-entity\-name \fIname
|
||||||
|
Name of the V4L2 entity to grab video from, will be router to \-\-device, for example "tc358743 10-000f". Default: unset.
|
||||||
|
|
||||||
.SS "Image control options"
|
.SS "Image control options"
|
||||||
.TP
|
.TP
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ static const struct {
|
|||||||
static const struct {
|
static const struct {
|
||||||
const char *name; // cppcheck-suppress unusedStructMember
|
const char *name; // cppcheck-suppress unusedStructMember
|
||||||
const uint format; // cppcheck-suppress unusedStructMember
|
const uint format; // cppcheck-suppress unusedStructMember
|
||||||
const uint subdev_format; // cppcheck-suppress unusedStructMember
|
const uint media_format; // cppcheck-suppress unusedStructMember
|
||||||
} _FORMATS[] = {
|
} _FORMATS[] = {
|
||||||
{"YUYV", V4L2_PIX_FMT_YUYV, MEDIA_BUS_FMT_YUYV8_2X8},
|
{"YUYV", V4L2_PIX_FMT_YUYV, MEDIA_BUS_FMT_YUYV8_2X8},
|
||||||
{"YVYU", V4L2_PIX_FMT_YVYU, MEDIA_BUS_FMT_YVYU8_2X8},
|
{"YVYU", V4L2_PIX_FMT_YVYU, MEDIA_BUS_FMT_YVYU8_2X8},
|
||||||
@@ -91,11 +91,11 @@ static const struct {
|
|||||||
};
|
};
|
||||||
|
|
||||||
static int _capture_wait_buffer(us_capture_s *cap);
|
static int _capture_wait_buffer(us_capture_s *cap);
|
||||||
static int _capture_consume_event(int fd);
|
static int _v4l2_consume_event(int fd);
|
||||||
static void _v4l2_buffer_copy(const struct v4l2_buffer *src, struct v4l2_buffer *dest);
|
static void _v4l2_buffer_copy(const struct v4l2_buffer *src, struct v4l2_buffer *dest);
|
||||||
static bool _capture_is_buffer_valid(const us_capture_s *cap, const struct v4l2_buffer *buf, const u8 *data);
|
static bool _capture_is_buffer_valid(const us_capture_s *cap, const struct v4l2_buffer *buf, const u8 *data);
|
||||||
static int _capture_open_check_cap(us_capture_s *cap);
|
static int _capture_open_check_cap(us_capture_s *cap);
|
||||||
static int _capture_find_and_open_v4l_subdev(const us_capture_s *cap, us_media_pad_s media_pads[3]);
|
static int _capture_open_media_pads(const us_capture_s *cap);
|
||||||
static int _capture_open_media_format(const us_capture_s *cap, const us_media_pad_s *pad);
|
static int _capture_open_media_format(const us_capture_s *cap, const us_media_pad_s *pad);
|
||||||
static int _capture_open_dv_timings(us_capture_s *cap, bool apply);
|
static int _capture_open_dv_timings(us_capture_s *cap, bool apply);
|
||||||
static int _capture_open_format(us_capture_s *cap, bool first);
|
static int _capture_open_format(us_capture_s *cap, bool first);
|
||||||
@@ -129,7 +129,7 @@ static const char *_format_to_string_nullable(uint format);
|
|||||||
static const char *_format_to_string_supported(uint format);
|
static const char *_format_to_string_supported(uint format);
|
||||||
static const char *_standard_to_string(v4l2_std_id standard);
|
static const char *_standard_to_string(v4l2_std_id standard);
|
||||||
static const char *_io_method_to_string_supported(enum v4l2_memory io_method);
|
static const char *_io_method_to_string_supported(enum v4l2_memory io_method);
|
||||||
static int _capture_format_v4l_to_subdev(uint format);
|
static uint _format_to_media(uint format);
|
||||||
|
|
||||||
|
|
||||||
#define _LOG_ERROR(x_msg, ...) US_LOG_ERROR("CAP: " x_msg, ##__VA_ARGS__)
|
#define _LOG_ERROR(x_msg, ...) US_LOG_ERROR("CAP: " x_msg, ##__VA_ARGS__)
|
||||||
@@ -212,10 +212,10 @@ int us_capture_open(us_capture_s *cap) {
|
|||||||
_LOG_DEBUG("Capture device fd=%d opened", run->fd);
|
_LOG_DEBUG("Capture device fd=%d opened", run->fd);
|
||||||
|
|
||||||
if (cap->dv_timings && cap->media_path) {
|
if (cap->dv_timings && cap->media_path) {
|
||||||
if ((run->dv_timings_fd = _capture_find_and_open_v4l_subdev(cap, run->media_pads)) < 0) {
|
if (_capture_open_media_pads(cap) < 0) {
|
||||||
_LOG_PERROR("Can't open v4l subdevice");
|
|
||||||
goto error_no_device;
|
goto error_no_device;
|
||||||
}
|
}
|
||||||
|
run->dv_timings_fd = run->media_pads[0].fd;
|
||||||
} else {
|
} else {
|
||||||
run->dv_timings_fd = run->fd;
|
run->dv_timings_fd = run->fd;
|
||||||
}
|
}
|
||||||
@@ -248,8 +248,8 @@ int us_capture_open(us_capture_s *cap) {
|
|||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
if (cap->dv_timings && cap->media_path) {
|
if (cap->dv_timings && cap->media_path) {
|
||||||
US_ARRAY_ITERATE(run->media_pads, 0, item, {
|
US_ARRAY_ITERATE(run->media_pads, 0, pad, {
|
||||||
if (item->fd >= -1 && _capture_open_media_format(cap, item) < 0) {
|
if (pad->fd >= -1 && _capture_open_media_format(cap, pad) < 0) {
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -363,11 +363,9 @@ void us_capture_close(us_capture_s *cap) {
|
|||||||
run->n_bufs = 0;
|
run->n_bufs = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (run->dv_timings_fd != run->fd) {
|
US_ARRAY_ITERATE(run->media_pads, 0, pad, { US_CLOSE_FD(pad->fd); });
|
||||||
US_CLOSE_FD(run->dv_timings_fd);
|
|
||||||
}
|
|
||||||
US_ARRAY_ITERATE(run->media_pads, 0, item, { US_CLOSE_FD(item->fd); });
|
|
||||||
US_CLOSE_FD(run->fd);
|
US_CLOSE_FD(run->fd);
|
||||||
|
run->dv_timings_fd = 0; // It's just a copy of the media or device descriptor
|
||||||
|
|
||||||
if (say) {
|
if (say) {
|
||||||
_LOG_INFO("Capturing stopped");
|
_LOG_INFO("Capturing stopped");
|
||||||
@@ -545,6 +543,7 @@ int _capture_wait_buffer(us_capture_s *cap) {
|
|||||||
bool has_dv_error = false;
|
bool has_dv_error = false;
|
||||||
const int max_fd = US_MAX(run->fd, run->dv_timings_fd);
|
const int max_fd = US_MAX(run->fd, run->dv_timings_fd);
|
||||||
const int selected = select(max_fd + 1, &read_fds, NULL, &error_fds, &timeout);
|
const int selected = select(max_fd + 1, &read_fds, NULL, &error_fds, &timeout);
|
||||||
|
|
||||||
if (selected > 0) {
|
if (selected > 0) {
|
||||||
has_read = FD_ISSET(run->fd, &read_fds);
|
has_read = FD_ISSET(run->fd, &read_fds);
|
||||||
has_error = FD_ISSET(run->fd, &error_fds);
|
has_error = FD_ISSET(run->fd, &error_fds);
|
||||||
@@ -563,17 +562,17 @@ int _capture_wait_buffer(us_capture_s *cap) {
|
|||||||
_LOG_ERROR("Device select() timeout");
|
_LOG_ERROR("Device select() timeout");
|
||||||
return -1;
|
return -1;
|
||||||
} else {
|
} else {
|
||||||
if (has_error && _capture_consume_event(run->fd) < 0) {
|
if (has_error && _v4l2_consume_event(run->fd) < 0) {
|
||||||
return -1; // Restart required
|
return -1; // Restart required
|
||||||
}
|
}
|
||||||
if (has_dv_error && _capture_consume_event(run->dv_timings_fd) < 0) {
|
if (has_dv_error && _v4l2_consume_event(run->dv_timings_fd) < 0) {
|
||||||
return -1; // Restart required
|
return -1; // Restart required
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int _capture_consume_event(int fd) {
|
static int _v4l2_consume_event(int fd) {
|
||||||
struct v4l2_event event;
|
struct v4l2_event event;
|
||||||
if (us_xioctl(fd, VIDIOC_DQEVENT, &event) < 0) {
|
if (us_xioctl(fd, VIDIOC_DQEVENT, &event) < 0) {
|
||||||
_LOG_PERROR("Can't consume V4L2 event");
|
_LOG_PERROR("Can't consume V4L2 event");
|
||||||
@@ -700,56 +699,62 @@ static int _capture_open_check_cap(us_capture_s *cap) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int _capture_media_lookup_link(struct media_v2_topology *topology, u32 link_type, int source_id, int sink_id, u32 *link_flags) {
|
static int _media_find_link(
|
||||||
|
struct media_v2_topology *topology,
|
||||||
|
u32 link_type,
|
||||||
|
int source_id,
|
||||||
|
int sink_id,
|
||||||
|
u32 *link_flags
|
||||||
|
) {
|
||||||
struct media_v2_link *links = (struct media_v2_link *)topology->ptr_links;
|
struct media_v2_link *links = (struct media_v2_link *)topology->ptr_links;
|
||||||
|
|
||||||
for (uint i = 0; i < topology->num_links; i++) {
|
for (uint i = 0; i < topology->num_links; ++i) {
|
||||||
if ((links[i].flags & MEDIA_LNK_FL_LINK_TYPE) != link_type)
|
if ((links[i].flags & MEDIA_LNK_FL_LINK_TYPE) != link_type) {
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
if (source_id >= 0 && (u32)source_id == links[i].source_id) {
|
if (source_id >= 0 && (u32)source_id == links[i].source_id) {
|
||||||
if (link_flags)
|
if (link_flags) {
|
||||||
*link_flags = links[i].flags;
|
*link_flags = links[i].flags;
|
||||||
|
}
|
||||||
return links[i].sink_id;
|
return links[i].sink_id;
|
||||||
}
|
}
|
||||||
if (sink_id >= 0 && (u32)sink_id == links[i].sink_id) {
|
if (sink_id >= 0 && (u32)sink_id == links[i].sink_id) {
|
||||||
if (link_flags)
|
if (link_flags) {
|
||||||
*link_flags = links[i].flags;
|
*link_flags = links[i].flags;
|
||||||
|
}
|
||||||
return links[i].source_id;
|
return links[i].source_id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const struct media_v2_pad *_capture_media_lookup_pad(const struct media_v2_topology *topology, u32 pad_type, u32 pad_id) {
|
static int _media_find_entity_by_name(
|
||||||
const struct media_v2_pad *pads = (const struct media_v2_pad *)topology->ptr_pads;
|
const struct media_v2_topology *topology,
|
||||||
|
const char *name
|
||||||
for (uint i = 0; i < topology->num_pads; i++) {
|
) {
|
||||||
if ((pads[i].flags & pad_type) && pads[i].id == pad_id) {
|
|
||||||
return &pads[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int _capture_media_lookup_entity_by_name(const struct media_v2_topology *topology, const char *entity_name) {
|
|
||||||
const struct media_v2_entity *entities = (struct media_v2_entity *)topology->ptr_entities;
|
const struct media_v2_entity *entities = (struct media_v2_entity *)topology->ptr_entities;
|
||||||
|
|
||||||
for (uint i = 0; i < topology->num_entities; i++) {
|
for (uint i = 0; i < topology->num_entities; ++i) {
|
||||||
if (!strcmp(entities[i].name, entity_name)) {
|
if (!strcmp(entities[i].name, name)) {
|
||||||
return entities[i].id;
|
return entities[i].id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int _capture_media_lookup_entity_by_devnode(struct media_v2_topology *topology, const dev_t devnode) {
|
static int _media_find_entity_by_devnode(
|
||||||
|
struct media_v2_topology *topology,
|
||||||
|
const dev_t devnode
|
||||||
|
) {
|
||||||
const struct media_v2_interface *interfaces = (struct media_v2_interface *)topology->ptr_interfaces;
|
const struct media_v2_interface *interfaces = (struct media_v2_interface *)topology->ptr_interfaces;
|
||||||
|
|
||||||
for (uint i = 0; i < topology->num_interfaces; i++) {
|
for (uint i = 0; i < topology->num_interfaces; ++i) {
|
||||||
if (interfaces[i].intf_type == MEDIA_INTF_T_V4L_VIDEO &&
|
if (
|
||||||
interfaces[i].devnode.major == major(devnode) &&
|
interfaces[i].intf_type == MEDIA_INTF_T_V4L_VIDEO
|
||||||
interfaces[i].devnode.minor == minor(devnode)) {
|
&& interfaces[i].devnode.major == major(devnode)
|
||||||
return _capture_media_lookup_link(
|
&& interfaces[i].devnode.minor == minor(devnode)
|
||||||
|
) {
|
||||||
|
return _media_find_link(
|
||||||
topology, MEDIA_LNK_FL_INTERFACE_LINK,
|
topology, MEDIA_LNK_FL_INTERFACE_LINK,
|
||||||
interfaces[i].id, -1, NULL);
|
interfaces[i].id, -1, NULL);
|
||||||
}
|
}
|
||||||
@@ -757,58 +762,99 @@ static int _capture_media_lookup_entity_by_devnode(struct media_v2_topology *top
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static const struct media_v2_pad *_media_find_pad_by_entity(
|
||||||
|
const struct media_v2_topology *topology,
|
||||||
|
u32 pad_type,
|
||||||
|
u32 entity_id
|
||||||
|
) {
|
||||||
|
const struct media_v2_pad *pads = (const struct media_v2_pad *)topology->ptr_pads;
|
||||||
|
|
||||||
static int _capture_media_configure_link(int fd, const struct media_v2_pad *source_pad, const struct media_v2_pad *sink_pad, u32 link_flags) {
|
for (uint i = 0; i < topology->num_pads; ++i) {
|
||||||
struct media_link_desc link_desc = {0};
|
if ((pads[i].flags & pad_type) && pads[i].entity_id == entity_id) {
|
||||||
|
return &pads[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
link_desc.source.entity = source_pad->entity_id;
|
static const struct media_v2_pad *_media_find_pad(
|
||||||
link_desc.source.index = source_pad->index;
|
const struct media_v2_topology *topology,
|
||||||
link_desc.sink.entity = sink_pad->entity_id;
|
u32 pad_type,
|
||||||
link_desc.sink.index = sink_pad->index;
|
u32 pad_id
|
||||||
link_desc.flags = link_flags;
|
) {
|
||||||
|
const struct media_v2_pad *pads = (const struct media_v2_pad *)topology->ptr_pads;
|
||||||
|
|
||||||
|
for (uint i = 0; i < topology->num_pads; ++i) {
|
||||||
|
if ((pads[i].flags & pad_type) && pads[i].id == pad_id) {
|
||||||
|
return &pads[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int _media_xioctl_setup_link(
|
||||||
|
int fd,
|
||||||
|
const struct media_v2_pad *source_pad,
|
||||||
|
const struct media_v2_pad *sink_pad,
|
||||||
|
u32 link_flags
|
||||||
|
) {
|
||||||
|
struct media_link_desc link_desc = {
|
||||||
|
.source = {
|
||||||
|
.entity = source_pad->entity_id,
|
||||||
|
.index = source_pad->index,
|
||||||
|
},
|
||||||
|
.sink = {
|
||||||
|
.entity = sink_pad->entity_id,
|
||||||
|
.index = sink_pad->index,
|
||||||
|
},
|
||||||
|
.flags = link_flags,
|
||||||
|
};
|
||||||
return us_xioctl(fd, MEDIA_IOC_SETUP_LINK, &link_desc);
|
return us_xioctl(fd, MEDIA_IOC_SETUP_LINK, &link_desc);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int _open_subdev_for_entity(struct media_v2_topology *topology, int entity_id) {
|
static int _open_subdev_for_entity(
|
||||||
|
struct media_v2_topology *topology,
|
||||||
|
int entity_id
|
||||||
|
) {
|
||||||
struct media_v2_interface *interfaces = (struct media_v2_interface *)topology->ptr_interfaces;
|
struct media_v2_interface *interfaces = (struct media_v2_interface *)topology->ptr_interfaces;
|
||||||
int subdev_intf = _capture_media_lookup_link(topology, MEDIA_LNK_FL_INTERFACE_LINK, -1, entity_id, NULL);
|
int subdev_intf = _media_find_link(topology, MEDIA_LNK_FL_INTERFACE_LINK, -1, entity_id, NULL);
|
||||||
int dev_major = -1, dev_minor = -1;
|
int dev_major = -1;
|
||||||
char dev_path[sizeof("/dev/char/256:256")];
|
int dev_minor = -1;
|
||||||
|
char *dev_path = NULL;
|
||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
if (subdev_intf < 0) {
|
if (subdev_intf < 0) {
|
||||||
_LOG_ERROR("Failed to find interface for entity %d",
|
_LOG_ERROR("Failed to find interface for entity %d", entity_id);
|
||||||
entity_id);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (uint i = 0; i < topology->num_interfaces; i++) {
|
for (uint i = 0; i < topology->num_interfaces; ++i) {
|
||||||
if (interfaces[i].intf_type == MEDIA_INTF_T_V4L_SUBDEV &&
|
if (
|
||||||
interfaces[i].id == (u32)subdev_intf) {
|
interfaces[i].intf_type == MEDIA_INTF_T_V4L_SUBDEV
|
||||||
|
&& interfaces[i].id == (u32)subdev_intf
|
||||||
|
) {
|
||||||
dev_major = interfaces[i].devnode.major;
|
dev_major = interfaces[i].devnode.major;
|
||||||
dev_minor = interfaces[i].devnode.minor;
|
dev_minor = interfaces[i].devnode.minor;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dev_major == -1 || dev_minor == -1) {
|
if (dev_major < 0 || dev_minor < 0) {
|
||||||
_LOG_ERROR("Cannot find interface %d", subdev_intf);
|
_LOG_ERROR("Cannot find interface %d", subdev_intf);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
US_ASPRINTF(dev_path, "/dev/char/%d:%d", dev_major, dev_minor);
|
||||||
US_SNPRINTF(dev_path, sizeof(dev_path)-1, "/dev/char/%d:%d",
|
|
||||||
dev_major, dev_minor);
|
|
||||||
|
|
||||||
if ((fd = open(dev_path, O_RDWR | O_NONBLOCK)) < 0) {
|
if ((fd = open(dev_path, O_RDWR | O_NONBLOCK)) < 0) {
|
||||||
_LOG_PERROR("Failed to open %s", dev_path);
|
_LOG_PERROR("Failed to open %s", dev_path);
|
||||||
|
free(dev_path);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
free(dev_path);
|
||||||
|
|
||||||
return fd;
|
return fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int _capture_find_and_open_v4l_subdev(const us_capture_s *cap, us_media_pad_s media_pads[3]) {
|
static int _capture_open_media_pads(const us_capture_s *cap) {
|
||||||
// This function needs to:
|
// This function needs to:
|
||||||
// 1. Find entity related to actual input signal
|
// 1. Find entity related to actual input signal
|
||||||
// 2. Find entity related to the selected v4l2 device (cap->path)
|
// 2. Find entity related to the selected v4l2 device (cap->path)
|
||||||
@@ -823,127 +869,104 @@ static int _capture_find_and_open_v4l_subdev(const us_capture_s *cap, us_media_p
|
|||||||
// - sink_pad
|
// - sink_pad
|
||||||
// - sink_entity_id
|
// - sink_entity_id
|
||||||
// - sink_intf_id (points at cap->path / run->fd)
|
// - sink_intf_id (points at cap->path / run->fd)
|
||||||
// Pads need saving both ID (for links lookup) and index (for
|
// Pads need saving both ID (for links lookup) and index (for MEDIA_IOC_SETUP_LINK ioctl),
|
||||||
// MEDIA_IOC_SETUP_LINK ioctl), so lookup relevant media_v2_pad structure.
|
// so lookup relevant media_v2_pad structure.
|
||||||
// Walk the relevant paths from both ends in parallel to reduce numbers of
|
// Walk the relevant paths from both ends in parallel to reduce numbers of loops.
|
||||||
// loops.
|
|
||||||
const us_capture_runtime_s *const run = cap->run;
|
us_capture_runtime_s *const run = cap->run;
|
||||||
int media_fd;
|
struct media_v2_entity *ptr_entities = NULL;
|
||||||
struct stat statbuf = {0};
|
struct media_v2_link *ptr_links = NULL;
|
||||||
int source_subdev_fd = -1;
|
struct media_v2_interface *ptr_interfaces = NULL;
|
||||||
|
struct media_v2_pad *ptr_pads = NULL;
|
||||||
|
int media_fd = -1;
|
||||||
int mux_subdev_fd = -1;
|
int mux_subdev_fd = -1;
|
||||||
struct media_device_info dev_info = {0};
|
int source_subdev_fd = -1;
|
||||||
struct media_v2_topology topology_info = {0};
|
|
||||||
u32 source_link_flags = 0, sink_link_flags = 0;
|
|
||||||
|
|
||||||
if (!cap->media_entity_name) {
|
if (!cap->media_entity_name) {
|
||||||
_LOG_ERROR("Media entity name not set");
|
_LOG_ERROR("Media entity name not set");
|
||||||
return -1;
|
goto error;
|
||||||
}
|
|
||||||
|
|
||||||
if (fstat(run->fd, &statbuf) < 0) {
|
|
||||||
_LOG_PERROR("Failed to stat video device");
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((media_fd = open(cap->media_path, O_RDWR | O_NONBLOCK)) < 0) {
|
if ((media_fd = open(cap->media_path, O_RDWR | O_NONBLOCK)) < 0) {
|
||||||
_LOG_PERROR("Can't open media device");
|
_LOG_PERROR("Can't open media device");
|
||||||
return -1;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
struct media_device_info dev_info = {0};
|
||||||
if (us_xioctl(media_fd, MEDIA_IOC_DEVICE_INFO, &dev_info) < 0) {
|
if (us_xioctl(media_fd, MEDIA_IOC_DEVICE_INFO, &dev_info) < 0) {
|
||||||
_LOG_PERROR("Failed to query device info");
|
_LOG_PERROR("Can't to query media device info");
|
||||||
close(media_fd);
|
goto error;
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!MEDIA_V2_PAD_HAS_INDEX(dev_info.media_version)) {
|
if (!MEDIA_V2_PAD_HAS_INDEX(dev_info.media_version)) {
|
||||||
_LOG_ERROR("Media topology doesn't have pad indices, too old kernel?");
|
_LOG_ERROR("Media topology doesn't have pad indices, too old kernel?");
|
||||||
close(media_fd);
|
goto error;
|
||||||
return -1;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct media_v2_topology topology = {0};
|
||||||
if (us_xioctl(media_fd, MEDIA_IOC_G_TOPOLOGY, &topology_info) < 0) {
|
if (us_xioctl(media_fd, MEDIA_IOC_G_TOPOLOGY, &topology) < 0) {
|
||||||
_LOG_PERROR("Failed to query topology info");
|
_LOG_PERROR("Can't query media topology info");
|
||||||
close(media_fd);
|
goto error;
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
# define ALLOCATE_TP(x_field, x_type) { \
|
||||||
struct media_v2_entity entities[topology_info.num_entities]; // cppcheck-suppress constVariable
|
ptr_##x_field = calloc(topology.num_##x_field, sizeof(x_type)); \
|
||||||
struct media_v2_link links[topology_info.num_links]; // cppcheck-suppress constVariable
|
US_A(ptr_##x_field); \
|
||||||
struct media_v2_interface interfaces[topology_info.num_interfaces]; // cppcheck-suppress constVariable
|
topology.ptr_##x_field = (uintptr_t)ptr_##x_field; \
|
||||||
struct media_v2_pad pads[topology_info.num_pads]; // cppcheck-suppress constVariable
|
}
|
||||||
|
ALLOCATE_TP(entities, struct media_v2_entity);
|
||||||
topology_info.ptr_entities = (uintptr_t)entities;
|
ALLOCATE_TP(links, struct media_v2_link);
|
||||||
topology_info.ptr_links = (uintptr_t)links;
|
ALLOCATE_TP(interfaces, struct media_v2_interface);
|
||||||
topology_info.ptr_interfaces = (uintptr_t)interfaces;
|
ALLOCATE_TP(pads, struct media_v2_pad);
|
||||||
topology_info.ptr_pads = (uintptr_t)pads;
|
# undef ALLOCATE_TP
|
||||||
|
if (us_xioctl(media_fd, MEDIA_IOC_G_TOPOLOGY, &topology) < 0) {
|
||||||
if (us_xioctl(media_fd, MEDIA_IOC_G_TOPOLOGY, &topology_info) < 0) {
|
|
||||||
_LOG_PERROR("Failed to retrieve topology info");
|
_LOG_PERROR("Failed to retrieve topology info");
|
||||||
close(media_fd);
|
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
close(media_fd);
|
const int source_entity_id = _media_find_entity_by_name(&topology, cap->media_entity_name);
|
||||||
|
|
||||||
const int source_entity_id = _capture_media_lookup_entity_by_name(
|
|
||||||
&topology_info, cap->media_entity_name);
|
|
||||||
if (source_entity_id == -1) {
|
if (source_entity_id == -1) {
|
||||||
_LOG_ERROR("Failed to find media entity '%s'", cap->media_entity_name);
|
_LOG_ERROR("Can't find source media entity '%s'", cap->media_entity_name);
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
const struct media_v2_pad *source_pad = _media_find_pad_by_entity(&topology, MEDIA_PAD_FL_SOURCE, source_entity_id);
|
||||||
|
if (source_pad == NULL) {
|
||||||
|
_LOG_ERROR("Failed to find pad for entity %d ('%s')", source_entity_id, cap->media_entity_name);
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
const int sink_entity_id = _capture_media_lookup_entity_by_devnode(
|
struct stat st = {0};
|
||||||
&topology_info, statbuf.st_rdev);
|
if (fstat(run->fd, &st) < 0) {
|
||||||
|
_LOG_PERROR("Can't stat() video device");
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
const int sink_entity_id = _media_find_entity_by_devnode(&topology, st.st_rdev);
|
||||||
if (sink_entity_id == -1) {
|
if (sink_entity_id == -1) {
|
||||||
_LOG_ERROR("Cannot find media entity for %s", cap->path);
|
_LOG_ERROR("Can't find sink media entity '%s'", cap->path);
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
const struct media_v2_pad *sink_pad = _media_find_pad_by_entity(&topology, MEDIA_PAD_FL_SINK, sink_entity_id);
|
||||||
|
if (sink_pad == NULL) {
|
||||||
|
_LOG_ERROR("Failed to find pad for entity %d ('%s')", sink_entity_id, cap->path);
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
const struct media_v2_pad *source_pad = NULL;
|
u32 source_link_flags = 0;
|
||||||
const struct media_v2_pad *sink_pad = NULL;
|
u32 sink_link_flags = 0;
|
||||||
|
int mux_source_pad_id = _media_find_link(&topology, MEDIA_LNK_FL_DATA_LINK, source_pad->id, -1, &source_link_flags);
|
||||||
|
int mux_sink_pad_id = _media_find_link(&topology, MEDIA_LNK_FL_DATA_LINK, -1, sink_pad->id, &sink_link_flags);
|
||||||
|
|
||||||
for (uint i = 0; i < topology_info.num_pads; i++) {
|
const struct media_v2_pad *mux_source_pad = _media_find_pad(&topology, MEDIA_PAD_FL_SINK, mux_source_pad_id);
|
||||||
if (pads[i].flags & MEDIA_PAD_FL_SOURCE &&
|
const struct media_v2_pad *mux_sink_pad = _media_find_pad(&topology, MEDIA_PAD_FL_SOURCE, mux_sink_pad_id);
|
||||||
pads[i].entity_id == (u32)source_entity_id) {
|
|
||||||
source_pad = &pads[i];
|
|
||||||
}
|
|
||||||
if (pads[i].flags & MEDIA_PAD_FL_SINK &&
|
|
||||||
pads[i].entity_id == (u32)sink_entity_id) {
|
|
||||||
sink_pad = &pads[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!sink_pad) {
|
|
||||||
_LOG_ERROR("Failed to find pad for entity %d ('%s')",
|
|
||||||
sink_entity_id, cap->path);
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!source_pad) {
|
|
||||||
_LOG_ERROR("Failed to find pad for entity %d ('%s')",
|
|
||||||
source_entity_id, cap->media_entity_name);
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
int mux_source_pad_id = _capture_media_lookup_link(
|
|
||||||
&topology_info, MEDIA_LNK_FL_DATA_LINK, source_pad->id, -1, &source_link_flags);
|
|
||||||
int mux_sink_pad_id = _capture_media_lookup_link(
|
|
||||||
&topology_info, MEDIA_LNK_FL_DATA_LINK, -1, sink_pad->id, &sink_link_flags);
|
|
||||||
|
|
||||||
const struct media_v2_pad *mux_source_pad = _capture_media_lookup_pad(
|
|
||||||
&topology_info, MEDIA_PAD_FL_SINK, mux_source_pad_id);
|
|
||||||
const struct media_v2_pad *mux_sink_pad = _capture_media_lookup_pad(
|
|
||||||
&topology_info, MEDIA_PAD_FL_SOURCE, mux_sink_pad_id);
|
|
||||||
|
|
||||||
// Those were referenced by relevant links, not finding them means kernel gave us incomplete info
|
// Those were referenced by relevant links, not finding them means kernel gave us incomplete info
|
||||||
US_A(mux_source_pad);
|
US_A(mux_source_pad);
|
||||||
US_A(mux_sink_pad);
|
US_A(mux_sink_pad);
|
||||||
|
|
||||||
if (mux_source_pad->entity_id != mux_sink_pad->entity_id) {
|
if (mux_source_pad->entity_id != mux_sink_pad->entity_id) {
|
||||||
_LOG_ERROR("Media source entity %d '%s' connected to a different entity (%d) than the sink entity %d '%s' (connected to %d). Select a different device.",
|
_LOG_ERROR(
|
||||||
|
"Media source entity %d '%s' connected to a different entity (%d) "
|
||||||
|
"than the sink entity %d '%s' (connected to %d). Select a different device.",
|
||||||
source_entity_id, cap->media_entity_name, mux_source_pad->entity_id,
|
source_entity_id, cap->media_entity_name, mux_source_pad->entity_id,
|
||||||
sink_entity_id, cap->path, mux_sink_pad->entity_id);
|
sink_entity_id, cap->path, mux_sink_pad->entity_id);
|
||||||
goto error;
|
goto error;
|
||||||
@@ -959,51 +982,59 @@ static int _capture_find_and_open_v4l_subdev(const us_capture_s *cap, us_media_p
|
|||||||
// this one is usually MEDIA_LNK_FL_IMMUTABLE, so it's always enabled,
|
// this one is usually MEDIA_LNK_FL_IMMUTABLE, so it's always enabled,
|
||||||
// but check anyway
|
// but check anyway
|
||||||
if (!(source_link_flags & MEDIA_LNK_FL_ENABLED)) {
|
if (!(source_link_flags & MEDIA_LNK_FL_ENABLED)) {
|
||||||
if (_capture_media_configure_link(media_fd,
|
if (_media_xioctl_setup_link(
|
||||||
|
media_fd,
|
||||||
source_pad, mux_source_pad,
|
source_pad, mux_source_pad,
|
||||||
source_link_flags | MEDIA_LNK_FL_ENABLED) < 0) {
|
source_link_flags | MEDIA_LNK_FL_ENABLED
|
||||||
|
) < 0) {
|
||||||
_LOG_PERROR("Failed to enable source link");
|
_LOG_PERROR("Failed to enable source link");
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(sink_link_flags & MEDIA_LNK_FL_ENABLED)) {
|
if (!(sink_link_flags & MEDIA_LNK_FL_ENABLED)) {
|
||||||
if (_capture_media_configure_link(media_fd,
|
if (_media_xioctl_setup_link(
|
||||||
|
media_fd,
|
||||||
mux_sink_pad, sink_pad,
|
mux_sink_pad, sink_pad,
|
||||||
sink_link_flags | MEDIA_LNK_FL_ENABLED) < 0) {
|
sink_link_flags | MEDIA_LNK_FL_ENABLED
|
||||||
|
) < 0) {
|
||||||
_LOG_PERROR("Failed to enable sink link");
|
_LOG_PERROR("Failed to enable sink link");
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((source_subdev_fd = _open_subdev_for_entity(&topology_info, source_entity_id)) < 0) {
|
if ((source_subdev_fd = _open_subdev_for_entity(&topology, source_entity_id)) < 0) {
|
||||||
_LOG_PERROR("Failed to open source v4l2 subdevice");
|
_LOG_PERROR("Failed to open source v4l2 subdevice");
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((mux_subdev_fd = _open_subdev_for_entity(&topology_info, mux_entity_id)) < 0) {
|
if ((mux_subdev_fd = _open_subdev_for_entity(&topology, mux_entity_id)) < 0) {
|
||||||
_LOG_PERROR("Failed to open mux v4l2 subdevice");
|
_LOG_PERROR("Failed to open mux v4l2 subdevice");
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save PAD indices for VIDIOC_SUBDEV_S_FMT
|
// Save PAD indices for VIDIOC_SUBDEV_S_FMT
|
||||||
media_pads[0].fd = source_subdev_fd;
|
run->media_pads[0].fd = source_subdev_fd;
|
||||||
media_pads[0].pad = source_pad->index;
|
run->media_pads[0].pad = source_pad->index;
|
||||||
media_pads[1].fd = mux_subdev_fd;
|
run->media_pads[1].fd = mux_subdev_fd;
|
||||||
media_pads[1].pad = mux_source_pad->index;
|
run->media_pads[1].pad = mux_source_pad->index;
|
||||||
media_pads[2].fd = mux_subdev_fd;
|
run->media_pads[2].fd = mux_subdev_fd;
|
||||||
media_pads[2].pad = mux_sink_pad->index;
|
run->media_pads[2].pad = mux_sink_pad->index;
|
||||||
|
|
||||||
return source_subdev_fd;
|
goto ok;
|
||||||
|
|
||||||
error:
|
error:
|
||||||
if (source_subdev_fd >= 0) {
|
US_CLOSE_FD(source_subdev_fd);
|
||||||
close(source_subdev_fd);
|
US_CLOSE_FD(mux_subdev_fd); // cppcheck-suppress unreadVariable
|
||||||
}
|
source_subdev_fd = -1; // cppcheck-suppress redundantAssignment
|
||||||
if (mux_subdev_fd >= 0) {
|
|
||||||
close(mux_subdev_fd);
|
ok:
|
||||||
}
|
US_CLOSE_FD(media_fd); // cppcheck-suppress unreadVariable
|
||||||
return -1;
|
US_DELETE(ptr_pads, free);
|
||||||
|
US_DELETE(ptr_interfaces, free);
|
||||||
|
US_DELETE(ptr_links, free);
|
||||||
|
US_DELETE(ptr_entities, free);
|
||||||
|
return (source_subdev_fd >= 0 ? 0 : -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int _capture_open_dv_timings(us_capture_s *cap, bool apply) {
|
static int _capture_open_dv_timings(us_capture_s *cap, bool apply) {
|
||||||
@@ -1090,35 +1121,38 @@ probe_only:
|
|||||||
|
|
||||||
static int _capture_open_media_format(const us_capture_s *cap, const us_media_pad_s *pad) {
|
static int _capture_open_media_format(const us_capture_s *cap, const us_media_pad_s *pad) {
|
||||||
const us_capture_runtime_s *const run = cap->run;
|
const us_capture_runtime_s *const run = cap->run;
|
||||||
struct v4l2_subdev_format fmt = {0};
|
|
||||||
uint format_code = _capture_format_v4l_to_subdev(cap->format);
|
|
||||||
|
|
||||||
if (!format_code) {
|
uint media_format = _format_to_media(cap->format);
|
||||||
_LOG_ERROR("Format not supported for v4l-subdev: %#x", cap->format);
|
if (media_format == 0) {
|
||||||
|
_LOG_ERROR("Format not supported for V4L2-subdev: %#x", cap->format);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
|
|
||||||
fmt.pad = pad->pad;
|
struct v4l2_subdev_format fmt = {
|
||||||
fmt.format.width = run->width;
|
.which = V4L2_SUBDEV_FORMAT_ACTIVE,
|
||||||
fmt.format.height = run->height;
|
.pad = pad->pad,
|
||||||
fmt.format.code = format_code;
|
.format.width = run->width,
|
||||||
fmt.format.field = V4L2_FIELD_NONE;
|
.format.height = run->height,
|
||||||
|
.format.code = media_format,
|
||||||
|
.format.field = V4L2_FIELD_NONE,
|
||||||
|
};
|
||||||
// Thankfully fmt.stream seems to be irrelevant on RPi5:
|
// Thankfully fmt.stream seems to be irrelevant on RPi5:
|
||||||
// # v4l2-ctl -d /dev/v4l-subdev0 --get-routing
|
// # v4l2-ctl -d /dev/v4l-subdev0 --get-routing
|
||||||
// Streams API not supported.
|
// Streams API not supported.
|
||||||
|
|
||||||
if (us_xioctl(pad->fd, VIDIOC_SUBDEV_S_FMT, &fmt) < 0) {
|
if (us_xioctl(pad->fd, VIDIOC_SUBDEV_S_FMT, &fmt) < 0) {
|
||||||
_LOG_PERROR("Can't set device format");
|
_LOG_PERROR("Can't set device format");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// This could use negotiation similar to _capture_open_format
|
// This could use negotiation similar to _capture_open_format
|
||||||
if (fmt.format.width != run->width ||
|
if (
|
||||||
|
fmt.format.width != run->width ||
|
||||||
fmt.format.height != run->height ||
|
fmt.format.height != run->height ||
|
||||||
fmt.format.code != format_code) {
|
fmt.format.code != media_format
|
||||||
|
) {
|
||||||
_LOG_ERROR("Format not accepted by pad %u: attempted %ux%u format %#x, got %ux%u format %#x",
|
_LOG_ERROR("Format not accepted by pad %u: attempted %ux%u format %#x, got %ux%u format %#x",
|
||||||
pad->pad,
|
pad->pad,
|
||||||
run->width, run->height, format_code,
|
run->width, run->height, media_format,
|
||||||
fmt.format.width, fmt.format.height, fmt.format.code);
|
fmt.format.width, fmt.format.height, fmt.format.code);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -1614,10 +1648,10 @@ static const char *_io_method_to_string_supported(enum v4l2_memory io_method) {
|
|||||||
return "unsupported";
|
return "unsupported";
|
||||||
}
|
}
|
||||||
|
|
||||||
static int _capture_format_v4l_to_subdev(uint format) {
|
static uint _format_to_media(uint format) {
|
||||||
US_ARRAY_ITERATE(_FORMATS, 0, item, {
|
US_ARRAY_ITERATE(_FORMATS, 0, item, {
|
||||||
if (item->format == format) {
|
if (item->format == format) {
|
||||||
return item->subdev_format;
|
return item->media_format;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -83,7 +83,7 @@ typedef enum {
|
|||||||
CTL_MODE_DEFAULT,
|
CTL_MODE_DEFAULT,
|
||||||
} us_control_mode_e;
|
} us_control_mode_e;
|
||||||
|
|
||||||
typedef struct _FD{
|
typedef struct {
|
||||||
us_control_mode_e mode;
|
us_control_mode_e mode;
|
||||||
int value;
|
int value;
|
||||||
} us_control_s;
|
} us_control_s;
|
||||||
|
|||||||
@@ -744,8 +744,10 @@ static void _help(
|
|||||||
SAY(" --device-error-delay <sec> ────────── Delay before trying to connect to the device again");
|
SAY(" --device-error-delay <sec> ────────── Delay before trying to connect to the device again");
|
||||||
SAY(" after an error (timeout for example). Default: %u.\n", stream->error_delay);
|
SAY(" after an error (timeout for example). Default: %u.\n", stream->error_delay);
|
||||||
SAY(" --m2m-device </dev/path> ──────────── Path to V4L2 M2M encoder device. Default: auto select.\n");
|
SAY(" --m2m-device </dev/path> ──────────── Path to V4L2 M2M encoder device. Default: auto select.\n");
|
||||||
SAY(" --media-device </dev/path> ────────── Path to V4L2 /dev/media* device for setting subdevices (currently necessary for RPi5). Default: none.\n");
|
SAY(" --media-device </dev/path> ────────── Path to V4L2 /dev/media* device for setting subdevices");
|
||||||
SAY(" --media-entity-name <name> ────────── Name of the V4L2 entity to grab video from, will be routed to --device. Default: none.\n");
|
SAY(" (currently necessary for RPi5). Default: unset.\n");
|
||||||
|
SAY(" --media-entity-name <name> ────────── Name of the V4L2 entity to grab video from,");
|
||||||
|
SAY(" will be routed to --device. Default: none.\n");
|
||||||
SAY("Image control options:");
|
SAY("Image control options:");
|
||||||
SAY("══════════════════════");
|
SAY("══════════════════════");
|
||||||
SAY(" --image-default ────────────────────── Reset all image settings below to default. Default: no change.\n");
|
SAY(" --image-default ────────────────────── Reset all image settings below to default. Default: no change.\n");
|
||||||
|
|||||||
Reference in New Issue
Block a user