summaryrefslogtreecommitdiff
path: root/include/libcamera/internal/v4l2_device.h
blob: a52a5f2c99f9d8de0a1f6495b254187e4e65c1c6 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2019, Google Inc.
 *
 * v4l2_device.h - Common base for V4L2 video devices and subdevices
 */

#pragma once

#include <map>
#include <memory>
#include <optional>
#include <vector>

#include <linux/videodev2.h>

#include <libcamera/base/log.h>
#include <libcamera/base/signal.h>
#include <libcamera/base/span.h>
#include <libcamera/base/unique_fd.h>

#include <libcamera/color_space.h>
#include <libcamera/controls.h>

namespace libcamera {

class EventNotifier;

class V4L2Device : protected Loggable
{
public:
	void close();
	bool isOpen() const { return fd_.isValid(); }

	const ControlInfoMap &controls() const { return controls_; }

	ControlList getControls(const std::vector<uint32_t> &ids);
	int setControls(ControlList *ctrls);

	const struct v4l2_query_ext_ctrl *controlInfo(uint32_t id) const;

	const std::string &deviceNode() const { return deviceNode_; }
	std::string devicePath() const;

	int setFrameStartEnabled(bool enable);
	Signal<uint32_t> frameStart;

	void updateControlInfo();

protected:
	V4L2Device(const std::string &deviceNode);
	~V4L2Device();

	int open(unsigned int flags);
	int setFd(UniqueFD fd);

	int ioctl(unsigned long request, void *argp);

	int fd() const { return fd_.get(); }

	template<typename T>
	static std::optional<ColorSpace> toColorSpace(const T &v4l2Format);

	template<typename T>
	static int fromColorSpace(const std::optional<ColorSpace> &colorSpace, T &v4l2Format);

private:
	static ControlType v4l2CtrlType(uint32_t ctrlType);
	static std::unique_ptr<ControlId> v4l2ControlId(const v4l2_query_ext_ctrl &ctrl);
	ControlInfo v4l2ControlInfo(const v4l2_query_ext_ctrl &ctrl);
	ControlInfo v4l2MenuControlInfo(const v4l2_query_ext_ctrl &ctrl);

	void listControls();
	void updateControls(ControlList *ctrls,
			    Span<const v4l2_ext_control> v4l2Ctrls);

	void eventAvailable();

	std::map<unsigned int, struct v4l2_query_ext_ctrl> controlInfo_;
	std::vector<std::unique_ptr<ControlId>> controlIds_;
	ControlIdMap controlIdMap_;
	ControlInfoMap controls_;
	std::string deviceNode_;
	UniqueFD fd_;

	EventNotifier *fdEventNotifier_;
	bool frameStartEnabled_;
};

} /* namespace libcamera */