libcamera  v0.0.0
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 
8 #pragma once
9 
10 #include <map>
11 #include <memory>
12 #include <optional>
13 #include <vector>
14 
15 #include <linux/videodev2.h>
16 
17 #include <libcamera/base/log.h>
18 #include <libcamera/base/signal.h>
19 #include <libcamera/base/span.h>
21 
22 #include <libcamera/color_space.h>
23 #include <libcamera/controls.h>
24 
25 namespace libcamera {
26 
27 class EventNotifier;
28 
29 class V4L2Device : protected Loggable
30 {
31 public:
32  void close();
33  bool isOpen() const { return fd_.isValid(); }
34 
35  const ControlInfoMap &controls() const { return controls_; }
36 
37  ControlList getControls(const std::vector<uint32_t> &ids);
38  int setControls(ControlList *ctrls);
39 
40  const struct v4l2_query_ext_ctrl *controlInfo(uint32_t id) const;
41 
42  const std::string &deviceNode() const { return deviceNode_; }
43  std::string devicePath() const;
44 
45  int setFrameStartEnabled(bool enable);
47 
48  void updateControlInfo();
49 
50 protected:
51  V4L2Device(const std::string &deviceNode);
52  ~V4L2Device();
53 
54  int open(unsigned int flags);
55  int setFd(UniqueFD fd);
56 
57  int ioctl(unsigned long request, void *argp);
58 
59  int fd() const { return fd_.get(); }
60 
61  template<typename T>
62  static std::optional<ColorSpace> toColorSpace(const T &v4l2Format);
63 
64  template<typename T>
65  static int fromColorSpace(const std::optional<ColorSpace> &colorSpace, T &v4l2Format);
66 
67 private:
68  static ControlType v4l2CtrlType(uint32_t ctrlType);
69  static std::unique_ptr<ControlId> v4l2ControlId(const v4l2_query_ext_ctrl &ctrl);
70  ControlInfo v4l2ControlInfo(const v4l2_query_ext_ctrl &ctrl);
71  ControlInfo v4l2MenuControlInfo(const v4l2_query_ext_ctrl &ctrl);
72 
73  void listControls();
74  void updateControls(ControlList *ctrls,
75  Span<const v4l2_ext_control> v4l2Ctrls);
76 
77  void eventAvailable();
78 
79  std::map<unsigned int, struct v4l2_query_ext_ctrl> controlInfo_;
80  std::vector<std::unique_ptr<ControlId>> controlIds_;
81  ControlIdMap controlIdMap_;
82  ControlInfoMap controls_;
83  std::string deviceNode_;
84  UniqueFD fd_;
85 
86  EventNotifier *fdEventNotifier_;
87  bool frameStartEnabled_;
88 };
89 
90 } /* namespace libcamera */
A map of ControlId to ControlInfo.
Definition: controls.h:305
Describe the limits of valid values for a Control.
Definition: controls.h:268
Associate a list of ControlId with their values for an object.
Definition: controls.h:349
Notify of activity on a file descriptor.
Definition: event_notifier.h:20
Base class to support log message extensions.
Definition: log.h:86
unique_ptr-like wrapper for a file descriptor
Definition: unique_fd.h:18
int get() const
Retrieve the managed file descriptor.
Definition: unique_fd.h:60
bool isValid() const
Check if the UniqueFD owns a valid file descriptor.
Definition: unique_fd.h:61
Base class for V4L2VideoDevice and V4L2Subdevice.
Definition: v4l2_device.h:30
~V4L2Device()
Destroy a V4L2Device.
Definition: v4l2_device.cpp:68
bool isOpen() const
Check if the V4L2 device node is open.
Definition: v4l2_device.h:33
static std::optional< ColorSpace > toColorSpace(const T &v4l2Format)
Convert the color space fields in a V4L2 format to a ColorSpace.
Definition: v4l2_device.cpp:819
int setFrameStartEnabled(bool enable)
Enable or disable frame start event notification.
Definition: v4l2_device.cpp:419
int ioctl(unsigned long request, void *argp)
Perform an IOCTL system call on the device node.
Definition: v4l2_device.cpp:450
static int fromColorSpace(const std::optional< ColorSpace > &colorSpace, T &v4l2Format)
Fill in the color space fields of a V4L2 format from a ColorSpace.
Definition: v4l2_device.cpp:879
const ControlInfoMap & controls() const
Retrieve the supported V4L2 controls and their information.
Definition: v4l2_device.h:35
void close()
Close the device node.
Definition: v4l2_device.cpp:138
void updateControlInfo()
Update the information for all device controls.
Definition: v4l2_device.cpp:651
Signal< uint32_t > frameStart
A Signal emitted when capture of a frame has started.
Definition: v4l2_device.h:46
int open(unsigned int flags)
Open a V4L2 device node.
Definition: v4l2_device.cpp:81
int fd() const
Retrieve the V4L2 device file descriptor.
Definition: v4l2_device.h:59
std::string devicePath() const
Retrieve the device path in sysfs.
Definition: v4l2_device.cpp:393
ControlList getControls(const std::vector< uint32_t > &ids)
Read controls from the device.
Definition: v4l2_device.cpp:175
int setControls(ControlList *ctrls)
Write controls to the device.
Definition: v4l2_device.cpp:278
int setFd(UniqueFD fd)
Set the file descriptor of a V4L2 device.
Definition: v4l2_device.cpp:119
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:373
V4L2Device(const std::string &deviceNode)
Construct a V4L2Device.
Definition: v4l2_device.cpp:59
const std::string & deviceNode() const
Retrieve the device node path.
Definition: v4l2_device.h:42
Class and enums to represent color spaces.
Framework to manage controls related to an object.
Logging infrastructure.
Top-level libcamera namespace.
Definition: backtrace.h:17
ControlType
Define the data type of a Control.
Definition: controls.h:26
std::unordered_map< unsigned int, const ControlId * > ControlIdMap
A map of numerical control ID to ControlId.
Definition: controls.h:302
Signal & slot implementation.
File descriptor wrapper that owns a file descriptor.