More codestyle

This commit is contained in:
Martijn Braam 2020-12-06 16:03:25 +01:00
parent 5703ecc786
commit 4df62aa21e
2 changed files with 198 additions and 164 deletions

View File

@ -6,180 +6,205 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
double get_time() double
get_time()
{ {
struct timeval t; struct timeval t;
struct timezone tzp; struct timezone tzp;
gettimeofday(&t, &tzp); gettimeofday(&t, &tzp);
return t.tv_sec + t.tv_usec*1e-6; return t.tv_sec + t.tv_usec * 1e-6;
} }
void on_capture(MPImage image, void *user_data) void
on_capture(MPImage image, void *user_data)
{ {
size_t num_bytes = mp_pixel_format_width_to_bytes(image.pixel_format, image.width) * image.height; size_t num_bytes =
uint8_t *data = malloc(num_bytes); mp_pixel_format_width_to_bytes(image.pixel_format, image.width) *
memcpy(data, image.data, num_bytes); image.height;
uint8_t *data = malloc(num_bytes);
memcpy(data, image.data, num_bytes);
printf(" first byte: %d.", data[0]); printf(" first byte: %d.", data[0]);
free(data); free(data);
} }
int main(int argc, char *argv[]) int
main(int argc, char *argv[])
{ {
if (argc != 2 && argc != 3) { if (argc != 2 && argc != 3) {
printf("Usage: %s <media_device_name> [<sub_device_name>]\n", argv[0]); printf("Usage: %s <media_device_name> [<sub_device_name>]\n",
return 1; argv[0]);
} return 1;
}
char *video_name = argv[1]; char *video_name = argv[1];
char *subdev_name = NULL; char *subdev_name = NULL;
if (argc == 3) { if (argc == 3) {
subdev_name = argv[2]; subdev_name = argv[2];
} }
double find_start = get_time(); double find_start = get_time();
// First find the device // First find the device
MPDevice *device = mp_device_find(video_name); MPDevice *device = mp_device_find(video_name);
if (!device) { if (!device) {
printf("Device not found\n"); printf("Device not found\n");
return 1; return 1;
} }
double find_end = get_time(); double find_end = get_time();
printf("Finding the device took %fms\n", (find_end - find_start) * 1000); printf("Finding the device took %fms\n", (find_end - find_start) * 1000);
int video_fd; int video_fd;
uint32_t video_entity_id; uint32_t video_entity_id;
{ {
const struct media_v2_entity *entity = mp_device_find_entity(device, video_name); const struct media_v2_entity *entity =
if (!entity) { mp_device_find_entity(device, video_name);
printf("Unable to find video device interface\n"); if (!entity) {
return 1; printf("Unable to find video device interface\n");
} return 1;
}
video_entity_id = entity->id; video_entity_id = entity->id;
const struct media_v2_interface *iface = mp_device_find_entity_interface(device, video_entity_id); const struct media_v2_interface *iface =
mp_device_find_entity_interface(device, video_entity_id);
char buf[256]; char buf[256];
if (!mp_find_device_path(iface->devnode, buf, 256)) { if (!mp_find_device_path(iface->devnode, buf, 256)) {
printf("Unable to find video device path\n"); printf("Unable to find video device path\n");
return 1; return 1;
} }
video_fd = open(buf, O_RDWR); video_fd = open(buf, O_RDWR);
if (video_fd == -1) { if (video_fd == -1) {
printf("Unable to open video device\n"); printf("Unable to open video device\n");
return 1; return 1;
} }
} }
int subdev_fd = -1; int subdev_fd = -1;
if (subdev_name) if (subdev_name) {
{ const struct media_v2_entity *entity =
const struct media_v2_entity *entity = mp_device_find_entity(device, subdev_name); mp_device_find_entity(device, subdev_name);
if (!entity) { if (!entity) {
printf("Unable to find sub-device\n"); printf("Unable to find sub-device\n");
return 1; return 1;
} }
const struct media_v2_pad *source_pad = mp_device_get_pad_from_entity(device, entity->id); const struct media_v2_pad *source_pad =
const struct media_v2_pad *sink_pad = mp_device_get_pad_from_entity(device, video_entity_id); mp_device_get_pad_from_entity(device, entity->id);
const struct media_v2_pad *sink_pad =
mp_device_get_pad_from_entity(device, video_entity_id);
// Disable other links // Disable other links
const struct media_v2_entity *entities = mp_device_get_entities(device); const struct media_v2_entity *entities =
for (int i = 0; i < mp_device_get_num_entities(device); ++i) { mp_device_get_entities(device);
if (entities[i].id != video_entity_id && entities[i].id != entity->id) { for (int i = 0; i < mp_device_get_num_entities(device); ++i) {
const struct media_v2_pad *pad = mp_device_get_pad_from_entity(device, entities[i].id); if (entities[i].id != video_entity_id &&
mp_device_setup_link(device, pad->id, sink_pad->id, false); entities[i].id != entity->id) {
} const struct media_v2_pad *pad =
} mp_device_get_pad_from_entity(
device, entities[i].id);
mp_device_setup_link(device, pad->id, sink_pad->id,
false);
}
}
// Then enable ours // Then enable ours
mp_device_setup_link(device, source_pad->id, sink_pad->id, true); mp_device_setup_link(device, source_pad->id, sink_pad->id, true);
const struct media_v2_interface *iface = mp_device_find_entity_interface(device, entity->id); const struct media_v2_interface *iface =
mp_device_find_entity_interface(device, entity->id);
char buf[256]; char buf[256];
if (!mp_find_device_path(iface->devnode, buf, 256)) { if (!mp_find_device_path(iface->devnode, buf, 256)) {
printf("Unable to find sub-device path\n"); printf("Unable to find sub-device path\n");
return 1; return 1;
} }
subdev_fd = open(buf, O_RDWR); subdev_fd = open(buf, O_RDWR);
if (subdev_fd == -1) { if (subdev_fd == -1) {
printf("Unable to open sub-device\n"); printf("Unable to open sub-device\n");
return 1; return 1;
} }
} }
double open_end = get_time(); double open_end = get_time();
printf("Opening the device took %fms\n", (open_end - find_end) * 1000); printf("Opening the device took %fms\n", (open_end - find_end) * 1000);
MPCamera *camera = mp_camera_new(video_fd, subdev_fd); MPCamera *camera = mp_camera_new(video_fd, subdev_fd);
MPControlList *controls = mp_camera_list_controls(camera); MPControlList *controls = mp_camera_list_controls(camera);
double control_list_end = get_time(); double control_list_end = get_time();
printf("Available controls: (took %fms)\n", (control_list_end - open_end) * 1000); printf("Available controls: (took %fms)\n",
for (MPControlList *list = controls; list; list = mp_control_list_next(list)) { (control_list_end - open_end) * 1000);
MPControl *c = mp_control_list_get(list); for (MPControlList *list = controls; list;
list = mp_control_list_next(list)) {
MPControl *c = mp_control_list_get(list);
printf(" %32s id:%s type:%s default:%d\n", c->name, mp_control_id_to_str(c->id), mp_control_type_to_str(c->type), c->default_value); printf(" %32s id:%s type:%s default:%d\n", c->name,
} mp_control_id_to_str(c->id), mp_control_type_to_str(c->type),
c->default_value);
}
double mode_list_begin = get_time(); double mode_list_begin = get_time();
MPCameraModeList *modes = mp_camera_list_available_modes(camera); MPCameraModeList *modes = mp_camera_list_available_modes(camera);
double mode_list_end = get_time(); double mode_list_end = get_time();
printf("Available modes: (took %fms)\n", (mode_list_end - mode_list_begin) * 1000); printf("Available modes: (took %fms)\n",
for (MPCameraModeList *list = modes; list; list = mp_camera_mode_list_next(list)) { (mode_list_end - mode_list_begin) * 1000);
MPCameraMode *m = mp_camera_mode_list_get(list); for (MPCameraModeList *list = modes; list;
printf(" %dx%d interval:%d/%d fmt:%s\n", m->width, m->height, m->frame_interval.numerator, m->frame_interval.denominator, mp_pixel_format_to_str(m->pixel_format)); list = mp_camera_mode_list_next(list)) {
MPCameraMode *m = mp_camera_mode_list_get(list);
printf(" %dx%d interval:%d/%d fmt:%s\n", m->width, m->height,
m->frame_interval.numerator, m->frame_interval.denominator,
mp_pixel_format_to_str(m->pixel_format));
// Skip really slow framerates // Skip really slow framerates
if (m->frame_interval.denominator < 15) { if (m->frame_interval.denominator < 15) {
printf(" Skipping…\n"); printf(" Skipping…\n");
continue; continue;
} }
double start_capture = get_time(); double start_capture = get_time();
mp_camera_set_mode(camera, m); mp_camera_set_mode(camera, m);
mp_camera_start_capture(camera); mp_camera_start_capture(camera);
double last = get_time(); double last = get_time();
printf(" Testing 10 captures, starting took %fms\n", (last - start_capture) * 1000); printf(" Testing 10 captures, starting took %fms\n",
(last - start_capture) * 1000);
for (int i = 0; i < 10; ++i) { for (int i = 0; i < 10; ++i) {
mp_camera_capture_image(camera, on_capture, NULL); mp_camera_capture_image(camera, on_capture, NULL);
double now = get_time(); double now = get_time();
printf(" capture took %fms\n", (now - last) * 1000); printf(" capture took %fms\n", (now - last) * 1000);
last = now; last = now;
} }
mp_camera_stop_capture(camera); mp_camera_stop_capture(camera);
} }
double cleanup_start = get_time(); double cleanup_start = get_time();
mp_camera_free(camera); mp_camera_free(camera);
close(video_fd); close(video_fd);
if (subdev_fd != -1) if (subdev_fd != -1)
close(subdev_fd); close(subdev_fd);
mp_device_close(device); mp_device_close(device);
double cleanup_end = get_time(); double cleanup_end = get_time();
printf("Cleanup took %fms\n", (cleanup_end - cleanup_start) * 1000); printf("Cleanup took %fms\n", (cleanup_end - cleanup_start) * 1000);
} }

View File

@ -2,50 +2,59 @@
#include <linux/media.h> #include <linux/media.h>
#include <stdio.h> #include <stdio.h>
int main(int argc, char *argv[]) { int
MPDeviceList *list = mp_device_list_new(); main(int argc, char *argv[])
{
MPDeviceList *list = mp_device_list_new();
while (list) { while (list) {
MPDevice *device = mp_device_list_get(list); MPDevice *device = mp_device_list_get(list);
const struct media_device_info *info = mp_device_get_info(device); const struct media_device_info *info = mp_device_get_info(device);
printf("%s (%s) %s\n", info->model, info->driver, info->serial); printf("%s (%s) %s\n", info->model, info->driver, info->serial);
printf(" Bus Info: %s\n", info->bus_info); printf(" Bus Info: %s\n", info->bus_info);
printf(" Media Version: %d\n", info->media_version); printf(" Media Version: %d\n", info->media_version);
printf(" HW Revision: %d\n", info->hw_revision); printf(" HW Revision: %d\n", info->hw_revision);
printf(" Driver Version: %d\n", info->driver_version); printf(" Driver Version: %d\n", info->driver_version);
const struct media_v2_entity *entities =
mp_device_get_entities(device);
size_t num = mp_device_get_num_entities(device);
printf(" Entities (%ld):\n", num);
for (int i = 0; i < num; ++i) {
printf(" %d %s (%d)\n", entities[i].id, entities[i].name,
entities[i].function);
}
const struct media_v2_entity *entities = mp_device_get_entities(device); const struct media_v2_interface *interfaces =
size_t num = mp_device_get_num_entities(device); mp_device_get_interfaces(device);
printf(" Entities (%ld):\n", num); num = mp_device_get_num_interfaces(device);
for (int i = 0; i < num; ++i) { printf(" Interfaces (%ld):\n", num);
printf(" %d %s (%d)\n", entities[i].id, entities[i].name, entities[i].function); for (int i = 0; i < num; ++i) {
} printf(" %d (%d - %d) devnode %d:%d\n", interfaces[i].id,
interfaces[i].intf_type, interfaces[i].flags,
interfaces[i].devnode.major,
interfaces[i].devnode.minor);
}
const struct media_v2_interface *interfaces = mp_device_get_interfaces(device); const struct media_v2_pad *pads = mp_device_get_pads(device);
num = mp_device_get_num_interfaces(device); num = mp_device_get_num_pads(device);
printf(" Interfaces (%ld):\n", num); printf(" Pads (%ld):\n", num);
for (int i = 0; i < num; ++i) { for (int i = 0; i < num; ++i) {
printf(" %d (%d - %d) devnode %d:%d\n", interfaces[i].id, interfaces[i].intf_type, interfaces[i].flags, interfaces[i].devnode.major, interfaces[i].devnode.minor); printf(" %d for device:%d (%d)\n", pads[i].id,
} pads[i].entity_id, pads[i].flags);
}
const struct media_v2_pad *pads = mp_device_get_pads(device); const struct media_v2_link *links = mp_device_get_links(device);
num = mp_device_get_num_pads(device); num = mp_device_get_num_links(device);
printf(" Pads (%ld):\n", num); printf(" Links (%ld):\n", num);
for (int i = 0; i < num; ++i) { for (int i = 0; i < num; ++i) {
printf(" %d for device:%d (%d)\n", pads[i].id, pads[i].entity_id, pads[i].flags); printf(" %d from:%d to:%d (%d)\n", links[i].id,
} links[i].source_id, links[i].sink_id, links[i].flags);
}
const struct media_v2_link *links = mp_device_get_links(device); list = mp_device_list_next(list);
num = mp_device_get_num_links(device); }
printf(" Links (%ld):\n", num);
for (int i = 0; i < num; ++i) {
printf(" %d from:%d to:%d (%d)\n", links[i].id, links[i].source_id, links[i].sink_id, links[i].flags);
}
list = mp_device_list_next(list); mp_device_list_free(list);
}
mp_device_list_free(list);
} }