Open default camera of a given type

This commit is contained in:
Kamil Trzcinski 2022-07-12 21:59:02 +02:00
parent 1857cd8d77
commit 4e641dea1e
3 changed files with 37 additions and 16 deletions

View File

@ -90,7 +90,7 @@ http_method_t http_methods[] = {
}; };
camera_options_t camera_options = { camera_options_t camera_options = {
.path = "/dev/video0", .path = "",
.width = 1920, .width = 1920,
.height = 1080, .height = 1080,
.format = 0, .format = 0,
@ -154,12 +154,12 @@ option_value_t camera_type[] = {
}; };
option_t all_options[] = { option_t all_options[] = {
DEFINE_OPTION_PTR(camera, path, string, "Chooses the camera to use."), DEFINE_OPTION_PTR(camera, path, string, "Chooses the camera to use. If empty connect to default."),
DEFINE_OPTION_VALUES(camera, type, camera_type, "Select camera type."),
DEFINE_OPTION(camera, width, uint, "Set the camera capture width."), DEFINE_OPTION(camera, width, uint, "Set the camera capture width."),
DEFINE_OPTION(camera, height, uint, "Set the camera capture height."), DEFINE_OPTION(camera, height, uint, "Set the camera capture height."),
DEFINE_OPTION_VALUES(camera, format, camera_formats, "Set the camera capture format."), DEFINE_OPTION_VALUES(camera, format, camera_formats, "Set the camera capture format."),
DEFINE_OPTION(camera, nbufs, uint, "Set number of capture buffers. Preferred 2 or 3."), DEFINE_OPTION(camera, nbufs, uint, "Set number of capture buffers. Preferred 2 or 3."),
DEFINE_OPTION_VALUES(camera, type, camera_type, "Select camera capture."),
DEFINE_OPTION(camera, fps, uint, "Set the desired capture framerate."), DEFINE_OPTION(camera, fps, uint, "Set the desired capture framerate."),
DEFINE_OPTION_DEFAULT(camera, allow_dma, bool, "1", "Prefer to use DMA access to reduce memory copy."), DEFINE_OPTION_DEFAULT(camera, allow_dma, bool, "1", "Prefer to use DMA access to reduce memory copy."),
DEFINE_OPTION(camera, high_res_factor, float, "Set the desired high resolution output scale factor."), DEFINE_OPTION(camera, high_res_factor, float, "Set the desired high resolution output scale factor."),

View File

@ -9,7 +9,13 @@
static int camera_configure_input_v4l2(camera_t *camera) static int camera_configure_input_v4l2(camera_t *camera)
{ {
camera->camera = device_v4l2_open(camera->name, camera->options.path); const char *path = camera->options.path;
if (!*path) {
path = "/dev/video0";
}
camera->camera = device_v4l2_open(camera->name, path);
if (!camera->camera) { if (!camera->camera) {
LOG_INFO(camera, "Listing available v4l2 devices:"); LOG_INFO(camera, "Listing available v4l2 devices:");
system("v4l2-ctl --list-devices"); system("v4l2-ctl --list-devices");

View File

@ -58,6 +58,19 @@ void libcamera_device_dump_options(device_t *dev, FILE *stream)
fprintf(stream, "\n"); fprintf(stream, "\n");
} }
void libcamera_print_cameras(device_t *dev)
{
if (dev->libcamera->camera_manager->cameras().size()) {
LOG_INFO(dev, "Available cameras (%zu)", dev->libcamera->camera_manager->cameras().size());
for (auto const &camera : dev->libcamera->camera_manager->cameras()) {
LOG_INFO(dev, "- %s", camera->id().c_str());
}
} else {
LOG_INFO(dev, "No available cameras");
}
}
int libcamera_device_open(device_t *dev) int libcamera_device_open(device_t *dev)
{ {
dev->libcamera = new device_libcamera_t{}; dev->libcamera = new device_libcamera_t{};
@ -68,25 +81,27 @@ int libcamera_device_open(device_t *dev)
LOG_ERROR(dev, "Cannot start camera_manager."); LOG_ERROR(dev, "Cannot start camera_manager.");
} }
dev->libcamera->camera = dev->libcamera->camera_manager->get(dev->path); if (strlen(dev->path) == 0) {
if (!dev->libcamera->camera) { if (dev->libcamera->camera_manager->cameras().size() != 1) {
if (dev->libcamera->camera_manager->cameras().size()) { libcamera_print_cameras(dev);
LOG_INFO(dev, "Available cameras (%zu)", dev->libcamera->camera_manager->cameras().size()); LOG_ERROR(dev, "Too many cameras was found. Cannot select default.");
for (auto const &camera : dev->libcamera->camera_manager->cameras()) {
LOG_INFO(dev, "- %s", camera->id().c_str());
}
} else {
LOG_INFO(dev, "No available cameras");
} }
dev->libcamera->camera = dev->libcamera->camera_manager->cameras().front();
} else {
dev->libcamera->camera = dev->libcamera->camera_manager->get(dev->path);
}
if (!dev->libcamera->camera) {
libcamera_print_cameras(dev);
LOG_ERROR(dev, "Camera `%s` was not found.", dev->path); LOG_ERROR(dev, "Camera `%s` was not found.", dev->path);
} }
if (dev->libcamera->camera->acquire()) { if (dev->libcamera->camera->acquire()) {
LOG_ERROR(dev, "Failed to acquire `%s` camera.", dev->path); LOG_ERROR(dev, "Failed to acquire `%s` camera.", dev->libcamera->camera->id().c_str());
} }
LOG_INFO(dev, "Device path=%s opened", dev->path); LOG_INFO(dev, "Device path=%s opened", dev->libcamera->camera->id().c_str());
return 0; return 0;
error: error: