libcamera  v0.5.0+43-37dccb45
Supporting cameras in Linux since 2019
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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  * 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 <stdint.h>
14 #include <vector>
15 
16 #include <linux/videodev2.h>
17 
18 #include <libcamera/base/log.h>
19 #include <libcamera/base/signal.h>
20 #include <libcamera/base/span.h>
22 
23 #include <libcamera/color_space.h>
24 #include <libcamera/controls.h>
25 
27 
28 namespace libcamera {
29 
30 class EventNotifier;
31 
32 class V4L2Device : protected Loggable
33 {
34 public:
35  void close();
36  bool isOpen() const { return fd_.isValid(); }
37 
38  const ControlInfoMap &controls() const { return controls_; }
39 
40  ControlList getControls(const std::vector<uint32_t> &ids);
41  int setControls(ControlList *ctrls);
42 
43  const struct v4l2_query_ext_ctrl *controlInfo(uint32_t id) const;
44 
45  const std::string &deviceNode() const { return deviceNode_; }
46  std::string devicePath() const;
47 
49  int setFrameStartEnabled(bool enable);
51 
52  void updateControlInfo();
53 
54 protected:
55  V4L2Device(const std::string &deviceNode);
56  ~V4L2Device();
57 
58  int open(unsigned int flags);
59  int setFd(UniqueFD fd);
60 
61  int ioctl(unsigned long request, void *argp);
62 
63  int fd() const { return fd_.get(); }
64 
65  template<typename T>
66  static std::optional<ColorSpace> toColorSpace(const T &v4l2Format,
67  PixelFormatInfo::ColourEncoding colourEncoding);
68 
69  template<typename T>
70  static int fromColorSpace(const std::optional<ColorSpace> &colorSpace, T &v4l2Format);
71 
72 private:
73  static ControlType v4l2CtrlType(uint32_t ctrlType);
74  static std::unique_ptr<ControlId> v4l2ControlId(const v4l2_query_ext_ctrl &ctrl);
75  std::optional<ControlInfo> v4l2ControlInfo(const v4l2_query_ext_ctrl &ctrl);
76  std::optional<ControlInfo> v4l2MenuControlInfo(const v4l2_query_ext_ctrl &ctrl);
77 
78  void listControls();
79  void updateControls(ControlList *ctrls,
80  Span<const v4l2_ext_control> v4l2Ctrls);
81 
82  void eventAvailable();
83 
84  std::map<unsigned int, struct v4l2_query_ext_ctrl> controlInfo_;
85  std::vector<std::unique_ptr<ControlId>> controlIds_;
86  ControlIdMap controlIdMap_;
87  ControlInfoMap controls_;
88  std::string deviceNode_;
89  UniqueFD fd_;
90 
91  EventNotifier *fdEventNotifier_;
92  bool frameStartEnabled_;
93 };
94 
95 } /* namespace libcamera */
int fd() const
Retrieve the V4L2 device file descriptor.
Definition: v4l2_device.h:63
int open(unsigned int flags)
Open a V4L2 device node.
Definition: v4l2_device.cpp:81
Base class for V4L2VideoDevice and V4L2Subdevice.
Definition: v4l2_device.h:32
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:1012
Class and enums to represent color spaces.
File descriptor wrapper that owns a file descriptor.
int get() const
Retrieve the managed file descriptor.
Definition: unique_fd.h:59
Base class to support log message extensions.
Definition: log.h:91
Top-level libcamera namespace.
Definition: backtrace.h:17
void close()
Close the device node.
Definition: v4l2_device.cpp:140
std::string devicePath() const
Retrieve the device path in sysfs.
Definition: v4l2_device.cpp:435
bool isOpen() const
Check if the V4L2 device node is open.
Definition: v4l2_device.h:36
static std::optional< ColorSpace > toColorSpace(const T &v4l2Format, PixelFormatInfo::ColourEncoding colourEncoding)
Convert the color space fields in a V4L2 format to a ColorSpace.
Definition: v4l2_device.cpp:933
Signal< uint32_t > frameStart
A Signal emitted when capture of a frame has started.
Definition: v4l2_device.h:50
const std::string & deviceNode() const
Retrieve the device node path.
Definition: v4l2_device.h:45
A map of ControlId to ControlInfo.
Definition: controls.h:366
ColourEncoding
The colour encoding type.
Definition: formats.h:23
~V4L2Device()
Destroy a V4L2Device.
Definition: v4l2_device.cpp:68
Signal & slot implementation.
ControlType
Define the data type of a Control.
Definition: controls.h:29
unique_ptr-like wrapper for a file descriptor
Definition: unique_fd.h:16
void updateControlInfo()
Update the information for all device controls.
Definition: v4l2_device.cpp:759
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:415
int setControls(ControlList *ctrls)
Write controls to the device.
Definition: v4l2_device.cpp:295
int setFd(UniqueFD fd)
Set the file descriptor of a V4L2 device.
Definition: v4l2_device.cpp:119
Notify of activity on a file descriptor.
Definition: event_notifier.h:19
int ioctl(unsigned long request, void *argp)
Perform an IOCTL system call on the device node.
Definition: v4l2_device.cpp:514
const ControlInfoMap & controls() const
Retrieve the supported V4L2 controls and their information.
Definition: v4l2_device.h:38
bool supportsFrameStartEvent()
Check if frame start event is supported.
Definition: v4l2_device.cpp:461
V4L2Device(const std::string &deviceNode)
Construct a V4L2Device.
Definition: v4l2_device.cpp:59
ControlList getControls(const std::vector< uint32_t > &ids)
Read controls from the device.
Definition: v4l2_device.cpp:177
Associate a list of ControlId with their values for an object.
Definition: controls.h:410
Framework to manage controls related to an object.
bool isValid() const
Check if the UniqueFD owns a valid file descriptor.
Definition: unique_fd.h:60
Types and helper functions to handle libcamera image formats.
Logging infrastructure.
std::unordered_map< unsigned int, const ControlId * > ControlIdMap
A map of numerical control ID to ControlId.
Definition: controls.h:364
int setFrameStartEnabled(bool enable)
Enable or disable frame start event notification.
Definition: v4l2_device.cpp:483