summaryrefslogtreecommitdiff
path: root/test/v4l2_videodevice/v4l2_videodevice_test.h
blob: d2de1a6de29fd6588141ce5a69aba1928a3174d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2018, Google Inc.
 *
 * vl42device_test.h - libcamera v4l2device test base class
 */

#pragma once

#include <memory>

#include <libcamera/framebuffer.h>

#include "libcamera/internal/camera_sensor.h"
#include "libcamera/internal/device_enumerator.h"
#include "libcamera/internal/media_device.h"
#include "libcamera/internal/v4l2_subdevice.h"
#include "libcamera/internal/v4l2_videodevice.h"

#include "test.h"

class V4L2VideoDeviceTest : public Test
{
public:
	V4L2VideoDeviceTest(const char *driver, const char *entity)
		: driver_(driver), entity_(entity), sensor_(nullptr),
		  debayer_(nullptr), capture_(nullptr)
	{
	}

protected:
	int init();
	void cleanup();

	std::string driver_;
	std::string entity_;
	std::unique_ptr<libcamera::DeviceEnumerator> enumerator_;
	std::shared_ptr<libcamera::MediaDevice> media_;
	libcamera::CameraSensor *sensor_;
	libcamera::V4L2Subdevice *debayer_;
	libcamera::V4L2VideoDevice *capture_;
	std::vector<std::unique_ptr<libcamera::FrameBuffer>> buffers_;
};
id='n188' 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 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2022, Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
 *
 * Python bindings
 */

#include "py_main.h"

#include <memory>
#include <stdexcept>
#include <string>
#include <vector>

#include <libcamera/base/log.h>

#include <libcamera/libcamera.h>

#include <pybind11/functional.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/stl_bind.h>

#include "py_camera_manager.h"
#include "py_helpers.h"

namespace py = pybind11;

using namespace libcamera;

namespace libcamera {

LOG_DEFINE_CATEGORY(Python)

}

/*
 * This is a holder class used only for the Camera class, for the sole purpose
 * of avoiding the compilation issue with Camera's private destructor.
 *
 * pybind11 requires a public destructor for classes held with shared_ptrs, even
 * in cases where the public destructor is not strictly needed. The current
 * understanding is that there are the following options to solve the problem:
 *
 * - Use pybind11 'smart_holder' branch. The downside is that 'smart_holder'
 *   is not the mainline branch, and not available in distributions.
 * - https://github.com/pybind/pybind11/pull/2067
 * - Make the Camera destructor public
 * - Something like the PyCameraSmartPtr here, which adds a layer, hiding the
 *   issue.
 */
template<typename T>
class PyCameraSmartPtr
{
public:
	using element_type = T;

	PyCameraSmartPtr()
	{
	}

	explicit PyCameraSmartPtr(T *)
	{
		throw std::runtime_error("invalid SmartPtr constructor call");
	}

	explicit PyCameraSmartPtr(std::shared_ptr<T> p)
		: ptr_(p)
	{
	}

	T *get() const { return ptr_.get(); }

	operator std::shared_ptr<T>() const { return ptr_; }

private:
	std::shared_ptr<T> ptr_;
};

PYBIND11_DECLARE_HOLDER_TYPE(T, PyCameraSmartPtr<T>)

/*
 * Note: global C++ destructors can be ran on this before the py module is
 * destructed.
 */
static std::weak_ptr<PyCameraManager> gCameraManager;

void init_py_color_space(py::module &m);
void init_py_controls_generated(py::module &m);
void init_py_enums(py::module &m);
void init_py_formats_generated(py::module &m);
void init_py_geometry(py::module &m);
void init_py_properties_generated(py::module &m);
void init_py_transform(py::module &m);

PYBIND11_MODULE(_libcamera, m)
{
	init_py_enums(m);
	init_py_controls_generated(m);
	init_py_geometry(m);
	init_py_properties_generated(m);
	init_py_color_space(m);
	init_py_transform(m);

	/* Forward declarations */

	/*
	 * We need to declare all the classes here so that Python docstrings
	 * can be generated correctly.
	 * https://pybind11.readthedocs.io/en/latest/advanced/misc.html#avoiding-c-types-in-docstrings
	 */

	auto pyCameraManager = py::class_<PyCameraManager, std::shared_ptr<PyCameraManager>>(m, "CameraManager");
	auto pyCamera = py::class_<Camera, PyCameraSmartPtr<Camera>>(m, "Camera");
	auto pyCameraConfiguration = py::class_<CameraConfiguration>(m, "CameraConfiguration");
	auto pyCameraConfigurationStatus = py::enum_<CameraConfiguration::Status>(pyCameraConfiguration, "Status");
	auto pyStreamConfiguration = py::class_<StreamConfiguration>(m, "StreamConfiguration");
	auto pyStreamFormats = py::class_<StreamFormats>(m, "StreamFormats");
	auto pyFrameBufferAllocator = py::class_<FrameBufferAllocator>(m, "FrameBufferAllocator");
	auto pyFrameBuffer = py::class_<FrameBuffer>(m, "FrameBuffer");
	auto pyFrameBufferPlane = py::class_<FrameBuffer::Plane>(pyFrameBuffer, "Plane");
	auto pyStream = py::class_<Stream>(m, "Stream");
	auto pyControlId = py::class_<ControlId>(m, "ControlId");
	auto pyControlInfo = py::class_<ControlInfo>(m, "ControlInfo");
	auto pyRequest = py::class_<Request>(m, "Request");
	auto pyRequestStatus = py::enum_<Request::Status>(pyRequest, "Status");
	auto pyRequestReuse = py::enum_<Request::ReuseFlag>(pyRequest, "Reuse");
	auto pyFrameMetadata = py::class_<FrameMetadata>(m, "FrameMetadata");
	auto pyFrameMetadataStatus = py::enum_<FrameMetadata::Status>(pyFrameMetadata, "Status");
	auto pyFrameMetadataPlane = py::class_<FrameMetadata::Plane>(pyFrameMetadata, "Plane");
	auto pyPixelFormat = py::class_<PixelFormat>(m, "PixelFormat");

	init_py_formats_generated(m);

	/* Global functions */
	m.def("log_set_level", &logSetLevel);

	/* Classes */
	pyCameraManager
		.def_static("singleton", []() {
			std::shared_ptr<PyCameraManager> cm = gCameraManager.lock();

			if (!cm) {
				cm = std::make_shared<PyCameraManager>();
				gCameraManager = cm;
			}

			return cm;
		})

		.def_property_readonly_static("version", [](py::object /* self */) { return PyCameraManager::version(); })
		.def("get", &PyCameraManager::get, py::keep_alive<0, 1>())
		.def_property_readonly("cameras", &PyCameraManager::cameras)

		.def_property_readonly("event_fd", &PyCameraManager::eventFd)
		.def("get_ready_requests", &PyCameraManager::getReadyRequests);

	pyCamera
		.def_property_readonly("id", &Camera::id)
		.def("acquire", [](Camera &self) {
			int ret = self.acquire();
			if (ret)
				throw std::system_error(-ret, std::generic_category(),
							"Failed to acquire camera");
		})
		.def("release", [](Camera &self) {
			int ret = self.release();
			if (ret)
				throw std::system_error(-ret, std::generic_category(),
							"Failed to release camera");
		})
		.def("start", [](Camera &self,
		                 const std::unordered_map<const ControlId *, py::object> &controls) {
			/* \todo What happens if someone calls start() multiple times? */

			auto cm = gCameraManager.lock();
			ASSERT(cm);

			self.requestCompleted.connect(cm.get(), &PyCameraManager::handleRequestCompleted);

			ControlList controlList(self.controls());

			for (const auto& [id, obj]: controls) {
				auto val = pyToControlValue(obj, id->type());
				controlList.set(id->id(), val);
			}

			int ret = self.start(&controlList);
			if (ret) {
				self.requestCompleted.disconnect();
				throw std::system_error(-ret, std::generic_category(),
							"Failed to start camera");
			}
		}, py::arg("controls") = std::unordered_map<const ControlId *, py::object>())

		.def("stop", [](Camera &self) {
			int ret = self.stop();

			self.requestCompleted.disconnect();

			if (ret)
				throw std::system_error(-ret, std::generic_category(),
							"Failed to stop camera");
		})

		.def("__str__", [](Camera &self) {
			return "<libcamera.Camera '" + self.id() + "'>";
		})

		/* Keep the camera alive, as StreamConfiguration contains a Stream* */
		.def("generate_configuration", &Camera::generateConfiguration, py::keep_alive<0, 1>())
		.def("configure", [](Camera &self, CameraConfiguration *config) {
			int ret = self.configure(config);
			if (ret)