summaryrefslogtreecommitdiff
path: root/test/ipa
AgeCommit message (Expand)Author
2020-07-17libcamera: ipa_interface: Add support for custom IPA data to configure()Laurent Pinchart
2020-06-08libcamera: IPAManager: remove instance() and make createIPA() staticPaul Elder
2020-06-08libcamera: IPAManager: make IPAManager lifetime explicitly managedPaul Elder
2020-05-16libcamera: Move IPA headers from include/ipa/ to include/libcamera/ipa/Laurent Pinchart
2020-05-16libcamera: Move internal headers to include/libcamera/internal/Laurent Pinchart
2020-05-13licenses: License all meson files under CC0-1.0Laurent Pinchart
2020-04-28libcamera: ipa: Add support for CameraSensorInfoJacopo Mondi
2020-04-28test: ipa: ipa_interface: Pass configuration file to IPA init()Laurent Pinchart
2020-04-28ipa: Pass IPA initialization settings to IPAInterface::init()Laurent Pinchart
2020-04-28ipa: Name IPA modules after their source directoryLaurent Pinchart
2020-04-14libcamera: ipa: Remove IPAModuleInfo license fieldLaurent Pinchart
2020-04-14ipa: Add start() and stop() operationsNiklas Söderlund
2020-03-26test: ipa: Fix path to VIMC IPANiklas Söderlund
2020-01-14libcamera: Switch from utils::make_unique to std::make_uniqueLaurent Pinchart
2020-01-12ipa: Switch to FrameBuffer interfaceNiklas Söderlund
2019-11-20test: ipa: Add IPA wrappers testLaurent Pinchart
2019-10-23libcamera: Standardise on C compatibility headersLaurent Pinchart
2019-10-08test: ipa: Add test for the IPA InterfaceJacopo Mondi
2019-10-08test: ipa: Rename the ipa_test to ipa_module_testJacopo Mondi
2019-10-08ipa: vimc: Rename ipa_dummy to ipa_vimcJacopo Mondi
2019-07-12libcamera: ipa_module_info: add license fieldPaul Elder
2019-07-04test: Allow self-contained tests to run in parallelNiklas Söderlund
2019-06-05libcamera: test: remove test IPA and use dummy IPA insteadPaul Elder
2019-06-05libcamera: ipa_module_info: update struct to allow IPA matchingPaul Elder
2019-06-05libcamera: ipa_interface: add headerPaul Elder
2019-05-23meson: Create and use a dependency for libcamera and its headersLaurent Pinchart
2019-05-23meson: Fix coding style in meson.build filesLaurent Pinchart
2019-05-21test: ipa: add IPAModule testsPaul Elder
88' href='#n188'>188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2018, Google Inc.
 *
 * camera_manager.h - Camera management
 */

#include <libcamera/camera.h>
#include <libcamera/camera_manager.h>
#include <libcamera/event_dispatcher.h>

#include "device_enumerator.h"
#include "event_dispatcher_poll.h"
#include "log.h"
#include "pipeline_handler.h"
#include "utils.h"

/**
 * \file camera_manager.h
 * \brief The camera manager
 */

namespace libcamera {

LOG_DEFINE_CATEGORY(Camera)

/**
 * \class CameraManager
 * \brief Provide access and manage all cameras in the system
 *
 * The camera manager is the entry point to libcamera. It enumerates devices,
 * associates them with pipeline managers, and provides access to the cameras
 * in the system to applications. The manager owns all Camera objects and
 * handles hot-plugging and hot-unplugging to manage the lifetime of cameras.
 *
 * To interact with libcamera, an application retrieves the camera manager
 * instance with CameraManager::instance(). The manager is initially stopped,
 * and shall be configured before being started. In particular a custom event
 * dispatcher shall be installed if needed with
 * CameraManager::setEventDispatcher().
 *
 * Once the camera manager is configured, it shall be started with start().
 * This will enumerate all the cameras present in the system, which can then be
 * listed with list() and retrieved with get().
 *
 * Cameras are shared through std::shared_ptr<>, ensuring that a camera will
 * stay valid until the last reference is released without requiring any special
 * action from the application. Once the application has released all the
 * references it held to cameras, the camera manager can be stopped with
 * stop().
 *
 * \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.
 */

CameraManager::CameraManager()
	: enumerator_(nullptr), dispatcher_(nullptr)
{
}

CameraManager::~CameraManager()
{
}

/**
 * \brief Start the camera manager
 *
 * Start the camera manager and enumerate all devices in the system. Once
 * the start has 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 0 on success or a negative error code otherwise
 */
int CameraManager::start()
{
	if (enumerator_)
		return -EBUSY;

	enumerator_ = DeviceEnumerator::create();
	if (enumerator_->enumerate())
		return -ENODEV;

	/*
	 * TODO: Try to read handlers and order from configuration
	 * file and only fallback on all handlers if there is no
	 * configuration file.
	 */
	std::vector<PipelineHandlerFactory *> &factories = PipelineHandlerFactory::factories();

	for (PipelineHandlerFactory *factory : factories) {
		/*
		 * Try each pipeline handler until it exhaust
		 * all pipelines it can provide.
		 */
		while (1) {
			std::shared_ptr<PipelineHandler> pipe = factory->create(this);
			if (!pipe->match(enumerator_.get()))
				break;

			LOG(Camera, Debug)
				<< "Pipeline handler \"" << factory->name()
				<< "\" matched";
			pipes_.push_back(std::move(pipe));
		}
	}

	/* TODO: register hot-plug callback here */

	return 0;
}

/**
 * \brief Stop the camera manager
 *
 * Before stopping the camera manager the caller is responsible for making
 * sure all cameras provided by the manager are returned to the manager.
 *
 * After the manager has been stopped no resource provided by 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 */

	/*
	 * Release all references to cameras and pipeline handlers to ensure
	 * they all get destroyed before the device enumerator deletes the
	 * media devices.
	 */
	pipes_.clear();
	cameras_.clear();

	enumerator_.reset(nullptr);
}

/**
 * \fn CameraManager::cameras()
 * \brief Retrieve all available cameras
 *
 * Before calling this function the caller is responsible for ensuring that
 * the camera manger is running.
 *
 * \return List of all available cameras
 */

/**
 * \brief Get a camera based on name
 *
 * \param[in] name Name of camera to get
 *
 * Before calling this function the caller is responsible for ensuring that
 * the camera manger is running.
 *
 * \return Shared pointer to Camera object or nullptr if camera not found
 */
std::shared_ptr<Camera> CameraManager::get(const std::string &name)
{
	for (std::shared_ptr<Camera> camera : cameras_) {
		if (camera->name() == name)
			return camera;
	}

	return nullptr;
}

/**
 * \brief Add a camera to the camera manager
 * \param[in] camera The camera to be added
 *
 * This function is called by pipeline handlers to register the cameras they
 * handle with the camera manager. Registered cameras are immediately made
 * available to the system.
 */
void CameraManager::addCamera(std::shared_ptr<Camera> camera)
{
	for (std::shared_ptr<Camera> c : cameras_) {
		if (c->name() == camera->name()) {
			LOG(Camera, Warning)
				<< "Registering camera with duplicate name '"
				<< camera->name() << "'";
			break;
		}
	}

	cameras_.push_back(std::move(camera));
}

/**
 * \brief Remove a camera from the camera manager
 * \param[in] camera The camera to be removed
 *
 * This function is called by pipeline handlers to unregister cameras from the
 * camera manager. Unregistered cameras won't be reported anymore by the
 * cameras() and get() calls, but references may still exist in applications.
 */
void CameraManager::removeCamera(Camera *camera)
{
	for (auto iter = cameras_.begin(); iter != cameras_.end(); ++iter) {
		if (iter->get() == camera) {
			LOG(Camera, Debug)
				<< "Unregistering camera '"
				<< camera->name() << "'";
			cameras_.erase(iter);
			return;
		}
	}
}

/**
 * \brief Retrieve the camera manager instance
 *
 * The CameraManager is a singleton and can't be constructed manually. This
 * function shall instead be used to retrieve the single global instance of the
 * manager.
 *
 * \return The camera manager instance
 */
CameraManager *CameraManager::instance()
{
	static CameraManager manager;
	return &manager;
}

/**
 * \brief Set the event dispatcher
 * \param dispatcher Pointer to the event dispatcher
 *
 * libcamera requires an event dispatcher to integrate event notification and
 * timers with the application event loop. Applications that want to provide
 * their own event dispatcher shall call this function once and only once before
 * the camera manager is started with start(). If no event dispatcher is
 * provided, a default poll-based implementation will be used.
 *
 * The CameraManager takes ownership of the event dispatcher and will delete it
 * when the application terminates.
 */
void CameraManager::setEventDispatcher(std::unique_ptr<EventDispatcher> dispatcher)
{
	if (dispatcher_) {
		LOG(Camera, Warning) << "Event dispatcher is already set";
		return;
	}

	dispatcher_ = std::move(dispatcher);
}

/**
 * \brief Retrieve the event dispatcher
 *
 * This function retrieves the event dispatcher set with setEventDispatcher().
 * If no dispatcher has been set, a default poll-based implementation is created
 * and returned, and no custom event dispatcher may be installed anymore.
 *
 * The returned event dispatcher is valid until the camera manager is destroyed.
 *
 * \return Pointer to the event dispatcher
 */
EventDispatcher *CameraManager::eventDispatcher()
{
	if (!dispatcher_)
		dispatcher_ = utils::make_unique<EventDispatcherPoll>();

	return dispatcher_.get();
}

} /* namespace libcamera */