From 5023b54ba76325bb0b5598d59714bdad2d55d81e Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Mon, 18 Mar 2019 15:20:09 +0100 Subject: video: add support for camera modules Issue: #910 --- video/camera.cpp | 66 +++++++++++++++++++++++++++++++++++++++ video/camera.hpp | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 161 insertions(+) create mode 100644 video/camera.cpp create mode 100644 video/camera.hpp (limited to 'video') diff --git a/video/camera.cpp b/video/camera.cpp new file mode 100644 index 00000000..c33ab13a --- /dev/null +++ b/video/camera.cpp @@ -0,0 +1,66 @@ +#include "camera.hpp" + +#include +#include +#include + +static std::vector> metadata; +static QMutex mtx; + +namespace video::impl { + +camera_::camera_() = default; +camera_::~camera_() = default; + +camera::camera() = default; +camera::~camera() = default; + +void register_camera(std::unique_ptr camera) +{ + QMutexLocker l(&mtx); + metadata.push_back(std::move(camera)); +} + +} // ns video::impl + +namespace video { + +bool show_dialog(const QString& camera_name) +{ + QMutexLocker l(&mtx); + + for (auto& camera : metadata) + for (const QString& name : camera->camera_names()) + if (name == camera_name) + return camera->show_dialog(camera_name); + + return false; +} + +std::unique_ptr make_camera(const QString& name) +{ + QMutexLocker l(&mtx); + + for (auto& camera : metadata) + for (const QString& name_ : camera->camera_names()) + if (name_ == name) + return camera->make_camera(name); + + return nullptr; +} + +std::vector camera_names() +{ + QMutexLocker l(&mtx); + std::vector names; names.reserve(32); + + for (auto& camera : metadata) + for (const QString& name : camera->camera_names()) + if (std::find(names.cbegin(), names.cend(), name) == names.cend()) + names.push_back(name); + + std::sort(names.begin(), names.end()); + return names; +} + +} // ns video diff --git a/video/camera.hpp b/video/camera.hpp new file mode 100644 index 00000000..c9577933 --- /dev/null +++ b/video/camera.hpp @@ -0,0 +1,95 @@ +/* Copyright (c) 2019 Stanislaw Halik + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + */ + +#pragma once + +#include "export.hpp" + +#include +#include + +#include + +namespace video +{ + +struct frame final +{ + unsigned char* data = nullptr; + int width = 0, height = 0, stride = 0, channels = 0; +}; + +} // ns video + +namespace video::impl { + +using namespace video; + +struct camera; + +struct OTR_VIDEO_EXPORT camera_ +{ + camera_(); + virtual ~camera_(); + + virtual std::vector camera_names() const = 0; + virtual std::unique_ptr make_camera(const QString& name) = 0; + [[nodiscard]] virtual bool show_dialog(const QString& camera_name) = 0; + virtual bool can_show_dialog(const QString& camera_name) = 0; +}; + +struct OTR_VIDEO_EXPORT camera +{ + struct info final + { + int width = 0, height = 0, fps = 0; + }; + + camera(); + virtual ~camera(); + + [[nodiscard]] virtual bool start(const info& args) = 0; + virtual void stop() = 0; + virtual bool is_open() = 0; + + virtual std::tuple get_frame() = 0; + [[nodiscard]] virtual bool show_dialog() = 0; +}; + +OTR_VIDEO_EXPORT +void register_camera(std::unique_ptr metadata); + +} // ns video::impl + +#define OTR_REGISTER_CAMERA2(type, ctr) \ + namespace { \ + struct init_##ctr \ + { \ + static char fuzz; \ + }; \ + char init_##ctr :: fuzz = \ + (::video::impl::register_camera(std::make_unique()), 0); \ + } // anon ns + +#define OTR_REGISTER_CAMERA(type) \ + OTR_REGISTER_CAMERA2(type, __COUNTER__) + +namespace video +{ +using camera_impl = typename impl::camera; + +OTR_VIDEO_EXPORT +std::unique_ptr make_camera(const QString& name); + +OTR_VIDEO_EXPORT +std::vector camera_names(); + +[[nodiscard]] +OTR_VIDEO_EXPORT +bool show_dialog(const QString& camera_name); + +} // ns video -- cgit v1.2.3