From patchwork Sat Dec 22 23:00:39 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Niklas_S=C3=B6derlund?= X-Patchwork-Id: 86 Return-Path: Received: from bin-mail-out-06.binero.net (bin-mail-out-06.binero.net [195.74.38.229]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 5231060B2E for ; Sun, 23 Dec 2018 00:02:35 +0100 (CET) X-Halon-ID: 9ae16192-063d-11e9-9adf-005056917a89 Authorized-sender: niklas@soderlund.pp.se Received: from wyvern.dyn.berto.se (unknown [217.31.177.236]) by bin-vsp-out-01.atm.binero.net (Halon) with ESMTPA id 9ae16192-063d-11e9-9adf-005056917a89; Sun, 23 Dec 2018 00:02:08 +0100 (CET) From: =?utf-8?q?Niklas_S=C3=B6derlund?= To: libcamera-devel@lists.libcamera.org Date: Sun, 23 Dec 2018 00:00:39 +0100 Message-Id: <20181222230041.29999-11-niklas.soderlund@ragnatech.se> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20181222230041.29999-1-niklas.soderlund@ragnatech.se> References: <20181222230041.29999-1-niklas.soderlund@ragnatech.se> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 10/12] libcamera: cameramanager: add CameraManager class X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 22 Dec 2018 23:02:35 -0000 Provide a CameraManager class which will handle listing, instancing, destruction and lifetime management of cameras. Signed-off-by: Niklas Söderlund --- include/libcamera/cameramanager.h | 38 +++++++ include/libcamera/libcamera.h | 1 + include/libcamera/meson.build | 1 + src/libcamera/cameramanager.cpp | 173 ++++++++++++++++++++++++++++++ src/libcamera/meson.build | 1 + 5 files changed, 214 insertions(+) create mode 100644 include/libcamera/cameramanager.h create mode 100644 src/libcamera/cameramanager.cpp diff --git a/include/libcamera/cameramanager.h b/include/libcamera/cameramanager.h new file mode 100644 index 0000000000000000..be078a75f9e5aefc --- /dev/null +++ b/include/libcamera/cameramanager.h @@ -0,0 +1,38 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2018, Google Inc. + * + * cameramanager.h - Camera management + */ +#ifndef __LIBCAMERA_CAMERAMANAGER_H__ +#define __LIBCAMERA_CAMERAMANAGER_H__ + +#include +#include + +namespace libcamera { + +class Camera; +class DeviceEnumerator; +class PipelineHandler; + +class CameraManager +{ +public: + CameraManager(); + + bool start(); + void stop(); + + std::vector list() const; + Camera *get(const std::string &name); + void put(Camera *camera); + +private: + DeviceEnumerator *enumerator_; + std::vector pipes_; +}; + +} /* namespace libcamera */ + +#endif /* __LIBCAMERA_CAMERAMANAGER_H__ */ diff --git a/include/libcamera/libcamera.h b/include/libcamera/libcamera.h index 44c094d92feed5ba..286b3d4d28083b01 100644 --- a/include/libcamera/libcamera.h +++ b/include/libcamera/libcamera.h @@ -8,6 +8,7 @@ #define __LIBCAMERA_LIBCAMERA_H__ #include +#include namespace libcamera { diff --git a/include/libcamera/meson.build b/include/libcamera/meson.build index 9b266ad926681db9..b7ba1f124aada003 100644 --- a/include/libcamera/meson.build +++ b/include/libcamera/meson.build @@ -1,5 +1,6 @@ libcamera_api = files([ 'camera.h', + 'cameramanager.h', 'libcamera.h', ]) diff --git a/src/libcamera/cameramanager.cpp b/src/libcamera/cameramanager.cpp new file mode 100644 index 0000000000000000..53209e32d88e3ed3 --- /dev/null +++ b/src/libcamera/cameramanager.cpp @@ -0,0 +1,173 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2018, Google Inc. + * + * cameramanager.h - Camera management + */ + +#include + +#include "deviceenumerator.h" +#include "pipelinehandler.h" + +/** + * \file cameramanager.h + * \brief Manage all cameras handled by libcamera + * + * The responsibility of the camera manager is to control the lifetime + * management of objects provided by libcamera. + * + * When a user wish to interact with libcamera it creates and starts a + * CameraManager object. Once confirmed the camera manager is running + * the application can list all cameras detected by the library, get + * one or more of the cameras and interact with them. + * + * When the user is done with the camera it should be returned to the + * camera manager. Once all cameras are returned to the camera manager + * the user is free to stop the manager. + * + * \todo Add ability to add and remove media devices based on + * hot-(un)plug events coming from the device enumerator. + * + * \todo Add interface to register a notification callback to the user + * to be able to inform it new cameras have been hot-plugged or + * cameras have been removed due to hot-unplug. + */ + +namespace libcamera { + +CameraManager::CameraManager() + : enumerator_(NULL) +{ +} + +/** + * \brief Start the camera manager + * + * Start the camera manager and enumerate all devices in the system. Once + * the stat have been confirmed the user is free to list and otherwise + * interact with cameras in the system until either the camera manager + * is stopped or the camera is unplugged from the system. + * + * \return true on successful start false otherwise + */ +bool CameraManager::start() +{ + std::vector handlers; + + if (enumerator_) + return false; + + enumerator_ = DeviceEnumerator::create(); + + if (enumerator_->enumerate()) + return false; + + /* TODO: Try to read handlers and order from configuration + * file and only fallback on all handlers if there is no + * configuration file. + */ + PipelineHandler::handlers(handlers); + + for (std::string const &handler : handlers) { + PipelineHandler *pipe; + + /* Try each pipeline handler until it exhaust + * all pipelines it can provide. */ + do { + pipe = PipelineHandler::create(handler, enumerator_); + if (pipe) + pipes_.push_back(pipe); + } while (pipe); + } + + /* TODO: register hot-plug callback here */ + + return true; +} + +/** + * \brief Stop the camera manager + * + * Before stopping the camera manger the caller is responsible for making + * sure all cameras provided by the manager are returned to the manager. + * + * After the manager have been stopped no resource provided my the camera + * manager should be consider valid or functional even if they for one + * reason or another have yet to be deleted. + */ +void CameraManager::stop() +{ + /* TODO: unregister hot-plug callback here */ + + for (PipelineHandler *pipe : pipes_) + delete pipe; + + if (enumerator_) + delete enumerator_; + + enumerator_ = NULL; +} + +/** + * \brief List all detected cameras + * + * Before calling this function the caller is responsible to make sure + * the camera manger is running. + * + * \return List of names for all detected cameras + */ +std::vector CameraManager::list() const +{ + std::vector list; + + for (PipelineHandler *pipe : pipes_) { + for (unsigned int i = 0; i < pipe->count(); i++) { + Camera *cam = pipe->camera(i); + list.push_back(cam->name()); + } + } + + return list; +} + +/** + * \brief Get a camera based on name + * + * \param[in] name Name of camera to get + * + * Before calling this function the caller is responsible to make sure + * the camera manger is running. A camera fetched this way should be + * returned to the camera manger once the caller is done with it. + * + * \return Pointer to Camera object or NULL if camera not found + */ +Camera *CameraManager::get(const std::string &name) +{ + for (PipelineHandler *pipe : pipes_) { + for (unsigned int i = 0; i < pipe->count(); i++) { + Camera *cam = pipe->camera(i); + if (cam->name() == name) { + cam->get(); + return cam; + } + } + } + + return NULL; +} + +/** + * \brief Return camera to the camera manager + * + * \param[in] camera Camera to return to the manager + * + * After the camera have been returned to the camera manager the caller + * should not use the pointer to the camera object it has returned. + */ +void CameraManager::put(Camera *camera) +{ + camera->put(); +} + +} /* namespace libcamera */ diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build index 0776643d3064129d..8457e57939b862ed 100644 --- a/src/libcamera/meson.build +++ b/src/libcamera/meson.build @@ -1,5 +1,6 @@ libcamera_sources = files([ 'camera.cpp', + 'cameramanager.cpp', 'deviceenumerator.cpp', 'log.cpp', 'main.cpp',