libcamera v0.7.0+1-4ceceb68
Supporting cameras in Linux since 2019
Loading...
Searching...
No Matches
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>
20#include <libcamera/base/span.h>
22
24#include <libcamera/controls.h>
25
28
29namespace libcamera {
30
31class EventNotifier;
32
33class V4L2Device : protected Loggable
34{
35public:
36 void close();
37 bool isOpen() const { return fd_.isValid(); }
38
39 const ControlInfoMap &controls() const { return controls_; }
40
41 ControlList getControls(Span<const uint32_t> ids, const V4L2Request *request = nullptr);
42 int setControls(ControlList *ctrls, const V4L2Request *request = nullptr);
43
44 const struct v4l2_query_ext_ctrl *controlInfo(uint32_t id) const;
45
46 const std::string &deviceNode() const { return deviceNode_; }
47 std::string devicePath() const;
48
50 int setFrameStartEnabled(bool enable);
52
53 void updateControlInfo();
54
55protected:
56 V4L2Device(const std::string &deviceNode);
58
59 int open(unsigned int flags);
60 int setFd(UniqueFD fd);
61
62 int ioctl(unsigned long request, void *argp);
63
64 int fd() const { return fd_.get(); }
65
66 template<typename T>
67 static std::optional<ColorSpace> toColorSpace(const T &v4l2Format,
68 PixelFormatInfo::ColourEncoding colourEncoding);
69
70 template<typename T>
71 static int fromColorSpace(const std::optional<ColorSpace> &colorSpace, T &v4l2Format);
72
73private:
74 static ControlType v4l2CtrlType(uint32_t ctrlType);
75 static std::unique_ptr<ControlId> v4l2ControlId(const v4l2_query_ext_ctrl &ctrl);
76 std::optional<ControlInfo> v4l2ControlInfo(const v4l2_query_ext_ctrl &ctrl);
77 std::optional<ControlInfo> v4l2MenuControlInfo(const v4l2_query_ext_ctrl &ctrl);
78
79 void listControls();
80 void updateControls(ControlList *ctrls,
81 Span<const v4l2_ext_control> v4l2Ctrls);
82
83 void eventAvailable();
84
85 std::map<unsigned int, struct v4l2_query_ext_ctrl> controlInfo_;
86 std::vector<std::unique_ptr<ControlId>> controlIds_;
87 ControlIdMap controlIdMap_;
88 ControlInfoMap controls_;
89 std::string deviceNode_;
90 UniqueFD fd_;
91
92 EventNotifier *fdEventNotifier_;
93 bool frameStartEnabled_;
94};
95
96} /* namespace libcamera */
A map of ControlId to ControlInfo.
Definition controls.h:365
Associate a list of ControlId with their values for an object.
Definition controls.h:409
Notify of activity on a file descriptor.
Definition event_notifier.h:20
Base class to support log message extensions.
Definition log.h:92
ColourEncoding
The colour encoding type.
Definition formats.h:23
Generic signal and slot communication mechanism.
Definition signal.h:39
unique_ptr-like wrapper for a file descriptor
Definition unique_fd.h:17
int get() const
Retrieve the managed file descriptor.
Definition unique_fd.h:59
bool isValid() const
Check if the UniqueFD owns a valid file descriptor.
Definition unique_fd.h:60
Base class for V4L2VideoDevice and V4L2Subdevice.
Definition v4l2_device.h:34
~V4L2Device()
Destroy a V4L2Device.
Definition v4l2_device.cpp:68
bool isOpen() const
Check if the V4L2 device node is open.
Definition v4l2_device.h:37
int setFrameStartEnabled(bool enable)
Enable or disable frame start event notification.
Definition v4l2_device.cpp:504
int ioctl(unsigned long request, void *argp)
Perform an IOCTL system call on the device node.
Definition v4l2_device.cpp:535
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:1033
const ControlInfoMap & controls() const
Retrieve the supported V4L2 controls and their information.
Definition v4l2_device.h:39
void close()
Close the device node.
Definition v4l2_device.cpp:140
void updateControlInfo()
Update the information for all device controls.
Definition v4l2_device.cpp:780
Signal< uint32_t > frameStart
A Signal emitted when capture of a frame has started.
Definition v4l2_device.h:51
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:64
bool supportsFrameStartEvent()
Check if frame start event is supported.
Definition v4l2_device.cpp:482
std::string devicePath() const
Retrieve the device path in sysfs.
Definition v4l2_device.cpp:456
ControlList getControls(Span< const uint32_t > ids, const V4L2Request *request=nullptr)
Read controls from the device.
Definition v4l2_device.cpp:180
int setFd(UniqueFD fd)
Set the file descriptor of a V4L2 device.
Definition v4l2_device.cpp:119
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:954
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:436
int setControls(ControlList *ctrls, const V4L2Request *request=nullptr)
Write controls to the device.
Definition v4l2_device.cpp:310
const std::string & deviceNode() const
Retrieve the device node path.
Definition v4l2_device.h:46
V4L2Request object and API.
Definition v4l2_request.h:22
Class and enums to represent color spaces.
Framework to manage controls related to an object.
Types and helper functions to handle libcamera image formats.
Logging infrastructure.
Top-level libcamera namespace.
Definition backtrace.h:17
ControlType
Define the data type of a Control.
Definition controls.h:29
std::unordered_map< unsigned int, const ControlId * > ControlIdMap
A map of numerical control ID to ControlId.
Definition controls.h:362
Signal & slot implementation.
File descriptor wrapper that owns a file descriptor.
V4L2 Request.