Loading...
Searching...
No Matches
v4l2_device.h
Go to the documentation of this file.
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2019, Google Inc.
*
* Common base for V4L2 video devices and subdevices
*/
#pragma once
#include <map>
#include <memory>
#include <optional>
#include <stdint.h>
#include <vector>
#include <linux/videodev2.h>
#include <libcamera/base/log.h>
#include <libcamera/base/signal.h>
#include <libcamera/base/span.h>
#include <libcamera/base/unique_fd.h>
#include <libcamera/color_space.h>
#include <libcamera/controls.h>
#include "libcamera/internal/formats.h"
#include "libcamera/internal/v4l2_request.h"
namespace libcamera {
class EventNotifier;
class V4L2Device : protected Loggable
{
public:
void close();
bool isOpen() const { return fd_.isValid(); }
const ControlInfoMap &controls() const { return controls_; }
ControlList getControls(Span<const uint32_t> ids, const V4L2Request *request = nullptr);
int setControls(ControlList *ctrls, const V4L2Request *request = nullptr);
const struct v4l2_query_ext_ctrl *controlInfo(uint32_t id) const;
const std::string &deviceNode() const { return deviceNode_; }
std::string devicePath() const;
bool supportsFrameStartEvent();
int setFrameStartEnabled(bool enable);
Signal<uint32_t> frameStart;
void updateControlInfo();
protected:
V4L2Device(const std::string &deviceNode);
~V4L2Device();
int open(unsigned int flags);
int setFd(UniqueFD fd);
int ioctl(unsigned long request, void *argp);
int fd() const { return fd_.get(); }
template<typename T>
static std::optional<ColorSpace> toColorSpace(const T &v4l2Format,
PixelFormatInfo::ColourEncoding colourEncoding);
template<typename T>
static int fromColorSpace(const std::optional<ColorSpace> &colorSpace, T &v4l2Format);
private:
static ControlType v4l2CtrlType(uint32_t ctrlType);
static std::unique_ptr<ControlId> v4l2ControlId(const v4l2_query_ext_ctrl &ctrl);
std::optional<ControlInfo> v4l2ControlInfo(const v4l2_query_ext_ctrl &ctrl);
std::optional<ControlInfo> v4l2MenuControlInfo(const v4l2_query_ext_ctrl &ctrl);
void listControls();
void updateControls(ControlList *ctrls,
Span<const v4l2_ext_control> v4l2Ctrls);
void eventAvailable();
std::map<unsigned int, struct v4l2_query_ext_ctrl> controlInfo_;
std::vector<std::unique_ptr<ControlId>> controlIds_;
ControlIdMap controlIdMap_;
ControlInfoMap controls_;
std::string deviceNode_;
UniqueFD fd_;
EventNotifier *fdEventNotifier_;
bool frameStartEnabled_;
};
} /* namespace libcamera */
libcamera::ControlInfoMap
A map of ControlId to ControlInfo.
Definition controls.h:365
libcamera::ControlList
Associate a list of ControlId with their values for an object.
Definition controls.h:409
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:92
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:17
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:51
libcamera::V4L2Device::setFrameStartEnabled
int setFrameStartEnabled(bool enable)
Enable or disable frame start event notification.
Definition v4l2_device.cpp:504
libcamera::V4L2Device::ioctl
int ioctl(unsigned long request, void *argp)
Perform an IOCTL system call on the device node.
Definition v4l2_device.cpp:535
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:1033
libcamera::V4L2Device::controls
const ControlInfoMap & controls() const
Retrieve the supported V4L2 controls and their information.
Definition v4l2_device.h:39
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:780
libcamera::V4L2Device::open
int open(unsigned int flags)
Open a V4L2 device node.
Definition v4l2_device.cpp:81
libcamera::V4L2Device::supportsFrameStartEvent
bool supportsFrameStartEvent()
Check if frame start event is supported.
Definition v4l2_device.cpp:482
libcamera::V4L2Device::devicePath
std::string devicePath() const
Retrieve the device path in sysfs.
Definition v4l2_device.cpp:456
libcamera::V4L2Device::deviceNode
const std::string & deviceNode() const
Retrieve the device node path.
Definition v4l2_device.h:46
libcamera::V4L2Device::getControls
ControlList getControls(Span< const uint32_t > ids, const V4L2Request *request=nullptr)
Read controls from the device.
Definition v4l2_device.cpp:180
libcamera::V4L2Device::fd
int fd() const
Retrieve the V4L2 device file descriptor.
Definition v4l2_device.h:64
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:954
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:436
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:37
libcamera::V4L2Device::setControls
int setControls(ControlList *ctrls, const V4L2Request *request=nullptr)
Write controls to the device.
Definition v4l2_device.cpp:310
libcamera::V4L2Request
V4L2Request object and API.
Definition v4l2_request.h:22
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:362
signal.h
Signal & slot implementation.
unique_fd.h
File descriptor wrapper that owns a file descriptor.
v4l2_request.h
V4L2 Request.