libcamera v0.0.0+1-ab0bf965
Supporting cameras in Linux since 2019
v4l2_device.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2/*
3 * Copyright (C) 2019, Google Inc.
4 *
5 * v4l2_device.h - Common base for V4L2 video devices and subdevices
6 */
7#ifndef __LIBCAMERA_INTERNAL_V4L2_DEVICE_H__
8#define __LIBCAMERA_INTERNAL_V4L2_DEVICE_H__
9
10#include <map>
11#include <memory>
12#include <vector>
13
14#include <linux/videodev2.h>
15
16#include <libcamera/base/log.h>
18#include <libcamera/base/span.h>
19
20#include <libcamera/controls.h>
21
22namespace libcamera {
23
24class EventNotifier;
25
26class V4L2Device : protected Loggable
27{
28public:
29 void close();
30 bool isOpen() const { return fd_ != -1; }
31
32 const ControlInfoMap &controls() const { return controls_; }
33
34 ControlList getControls(const std::vector<uint32_t> &ids);
35 int setControls(ControlList *ctrls);
36
37 const struct v4l2_query_ext_ctrl *controlInfo(uint32_t id) const;
38
39 const std::string &deviceNode() const { return deviceNode_; }
40 std::string devicePath() const;
41
42 int setFrameStartEnabled(bool enable);
44
45 void updateControlInfo();
46
47protected:
48 V4L2Device(const std::string &deviceNode);
50
51 int open(unsigned int flags);
52 int setFd(int fd);
53
54 int ioctl(unsigned long request, void *argp);
55
56 int fd() const { return fd_; }
57
58private:
59 static ControlType v4l2CtrlType(uint32_t ctrlType);
60 static std::unique_ptr<ControlId> v4l2ControlId(const v4l2_query_ext_ctrl &ctrl);
61 ControlInfo v4l2ControlInfo(const v4l2_query_ext_ctrl &ctrl);
62 ControlInfo v4l2MenuControlInfo(const v4l2_query_ext_ctrl &ctrl);
63
64 void listControls();
65 void updateControls(ControlList *ctrls,
66 Span<const v4l2_ext_control> v4l2Ctrls);
67
68 void eventAvailable();
69
70 std::map<unsigned int, struct v4l2_query_ext_ctrl> controlInfo_;
71 std::vector<std::unique_ptr<ControlId>> controlIds_;
72 ControlIdMap controlIdMap_;
73 ControlInfoMap controls_;
74 std::string deviceNode_;
75 int fd_;
76
77 EventNotifier *fdEventNotifier_;
78 bool frameStartEnabled_;
79};
80
81} /* namespace libcamera */
82
83#endif /* __LIBCAMERA_INTERNAL_V4L2_DEVICE_H__ */
A map of ControlId to ControlInfo.
Definition: controls.h:306
Describe the limits of valid values for a Control.
Definition: controls.h:269
Associate a list of ControlId with their values for an object.
Definition: controls.h:350
Notify of activity on a file descriptor.
Definition: event_notifier.h:20
Base class to support log message extensions.
Definition: log.h:86
Generic signal and slot communication mechanism.
Definition: signal.h:39
Base class for V4L2VideoDevice and V4L2Subdevice.
Definition: v4l2_device.h:27
~V4L2Device()
Destroy a V4L2Device.
Definition: v4l2_device.cpp:64
bool isOpen() const
Check if the V4L2 device node is open.
Definition: v4l2_device.h:30
int setFrameStartEnabled(bool enable)
Enable or disable frame start event notification.
Definition: v4l2_device.cpp:406
int ioctl(unsigned long request, void *argp)
Perform an IOCTL system call on the device node.
Definition: v4l2_device.cpp:437
const ControlInfoMap & controls() const
Retrieve the supported V4L2 controls and their information.
Definition: v4l2_device.h:32
void close()
Close the device node.
Definition: v4l2_device.cpp:134
void updateControlInfo()
Update the information for all device controls.
Definition: v4l2_device.cpp:638
Signal< uint32_t > frameStart
A Signal emitted when capture of a frame has started.
Definition: v4l2_device.h:43
int open(unsigned int flags)
Open a V4L2 device node.
Definition: v4l2_device.cpp:77
int setFd(int fd)
Set the file descriptor of a V4L2 device.
Definition: v4l2_device.cpp:115
int fd() const
Retrieve the V4L2 device file descriptor.
Definition: v4l2_device.h:56
std::string devicePath() const
Retrieve the device path in sysfs.
Definition: v4l2_device.cpp:380
ControlList getControls(const std::vector< uint32_t > &ids)
Read controls from the device.
Definition: v4l2_device.cpp:174
int setControls(ControlList *ctrls)
Write controls to the device.
Definition: v4l2_device.cpp:277
const struct v4l2_query_ext_ctrl * controlInfo(uint32_t id) const
Retrieve the v4l2_query_ext_ctrl information for the given control.
Definition: v4l2_device.cpp:360
V4L2Device(const std::string &deviceNode)
Construct a V4L2Device.
Definition: v4l2_device.cpp:55
const std::string & deviceNode() const
Retrieve the device node path.
Definition: v4l2_device.h:39
Framework to manage controls related to an object.
Logging infrastructure.
Top-level libcamera namespace.
Definition: bound_method.h:15
ControlType
Define the data type of a Control.
Definition: controls.h:27
std::unordered_map< unsigned int, const ControlId * > ControlIdMap
A map of numerical control ID to ControlId.
Definition: controls.h:303
Signal & slot implementation.