libcamera: include/libcamera/internal/v4l2_device.h Source File

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