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

libcamera v0.7.0 Supporting cameras in Linux since 2019

Loading...
Searching...
No Matches
media_device.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2/*
3 * Copyright (C) 2018, Google Inc.
4 *
5 * Media device handler
6 */
7
8#pragma once
9
10#include <map>
11#include <optional>
12#include <string>
13#include <vector>
14
15#include <linux/media.h>
16
17#include <libcamera/base/log.h>
18#include <libcamera/base/regex.h>
19#include <libcamera/base/signal.h>
20#include <libcamera/base/unique_fd.h>
21
22#include "libcamera/internal/media_object.h"
23#include "libcamera/internal/v4l2_request.h"
24
25namespace libcamera {
26
27 class MediaDevice : protected Loggable
28{
29public:
30 MediaDevice(const std::string &deviceNode);
31 ~MediaDevice();
32
33 bool acquire();
34 void release();
35 bool busy() const { return acquired_; }
36
37 bool lock();
38 void unlock();
39
40 int populate();
41 bool isValid() const { return valid_; }
42
43 const std::string &driver() const { return driver_; }
44 const std::string &deviceNode() const { return deviceNode_; }
45 const std::string &model() const { return model_; }
46 unsigned int version() const { return version_; }
47 unsigned int hwRevision() const { return hwRevision_; }
48
49 const std::vector<MediaEntity *> &entities() const { return entities_; }
50 MediaEntity *getEntityByName(const std::string &name) const;
51 MediaEntity *getEntityByName(const std::regex &name) const;
52
53 MediaLink *link(const std::string &sourceName, unsigned int sourceIdx,
54 const std::string &sinkName, unsigned int sinkIdx);
55 MediaLink *link(const MediaEntity *source, unsigned int sourceIdx,
56 const MediaEntity *sink, unsigned int sinkIdx);
57 MediaLink *link(const MediaPad *source, const MediaPad *sink);
58 int disableLinks();
59
60 Signal<> disconnected;
61
62 std::vector<MediaEntity *> locateEntities(unsigned int function);
63
64 int allocateRequests(unsigned int count,
65 std::vector<std::unique_ptr<V4L2Request>> *requests);
66
67 bool supportsRequests();
68
69protected:
70 std::string logPrefix() const override;
71
72private:
73 int open();
74 void close();
75
76 MediaObject *object(unsigned int id);
77 bool addObject(MediaObject *object);
78 void clear();
79
80 struct media_v2_interface *findInterface(const struct media_v2_topology &topology,
81 unsigned int entityId);
82 bool populateEntities(const struct media_v2_topology &topology);
83 bool populatePads(const struct media_v2_topology &topology);
84 bool populateLinks(const struct media_v2_topology &topology);
85 void fixupEntityFlags(struct media_v2_entity *entity);
86
87 friend int MediaLink::setEnabled(bool enable);
88 int setupLink(const MediaLink *link, unsigned int flags);
89
90 std::string driver_;
91 std::string deviceNode_;
92 std::string model_;
93 unsigned int version_;
94 unsigned int hwRevision_;
95
96 UniqueFD fd_;
97 bool valid_;
98 bool acquired_;
99 std::optional<bool> supportsRequests_;
100
101 std::map<unsigned int, MediaObject *> objects_;
102 std::vector<MediaEntity *> entities_;
103};
104
105} /* namespace libcamera */
libcamera::Loggable
Base class to support log message extensions.
Definition log.h:92
libcamera::MediaDevice::acquire
bool acquire()
Claim a device for exclusive use.
Definition media_device.cpp:105
libcamera::MediaDevice::allocateRequests
int allocateRequests(unsigned int count, std::vector< std::unique_ptr< V4L2Request > > *requests)
Allocate requests.
Definition media_device.cpp:891
libcamera::MediaDevice::logPrefix
std::string logPrefix() const override
Retrieve a string to be prefixed to the log message.
Definition media_device.cpp:78
libcamera::MediaDevice::disableLinks
int disableLinks()
Disable all links in the media device.
Definition media_device.cpp:463
libcamera::MediaDevice::isValid
bool isValid() const
Query whether the media graph has been populated and is valid.
Definition media_device.h:41
libcamera::MediaDevice::entities
const std::vector< MediaEntity * > & entities() const
Retrieve the list of entities in the media graph.
Definition media_device.h:49
libcamera::MediaDevice::busy
bool busy() const
Check if a device is in use.
Definition media_device.h:35
libcamera::MediaDevice::supportsRequests
bool supportsRequests()
Check if requests are supported.
Definition media_device.cpp:916
libcamera::MediaDevice::release
void release()
Release a device previously claimed for exclusive use.
Definition media_device.cpp:121
libcamera::MediaDevice::deviceNode
const std::string & deviceNode() const
Retrieve the media device node path.
Definition media_device.h:44
libcamera::MediaDevice::locateEntities
std::vector< MediaEntity * > locateEntities(unsigned int function)
Identify all entities of a common function in the MediaDevice.
Definition media_device.cpp:868
libcamera::MediaDevice::populate
int populate()
Populate the MediaDevice with device information and media objects.
Definition media_device.cpp:195
libcamera::MediaDevice::driver
const std::string & driver() const
Retrieve the media device driver name.
Definition media_device.h:43
libcamera::MediaDevice::hwRevision
unsigned int hwRevision() const
Retrieve the media device hardware revision.
Definition media_device.h:47
libcamera::MediaDevice::lock
bool lock()
Lock the device to prevent it from being used by other instances of libcamera.
Definition media_device.cpp:144
libcamera::MediaDevice::link
MediaLink * link(const std::string &sourceName, unsigned int sourceIdx, const std::string &sinkName, unsigned int sinkIdx)
Retrieve the MediaLink connecting two pads, identified by entity names and pad indexes.
Definition media_device.cpp:390
libcamera::MediaDevice::MediaDevice
MediaDevice(const std::string &deviceNode)
Construct a MediaDevice.
Definition media_device.cpp:67
libcamera::MediaDevice::disconnected
Signal disconnected
Signal emitted when the media device is disconnected from the system.
Definition media_device.h:60
libcamera::MediaDevice::getEntityByName
MediaEntity * getEntityByName(const std::string &name) const
Return the MediaEntity with name name.
Definition media_device.cpp:336
libcamera::MediaDevice::model
const std::string & model() const
Retrieve the media device model name.
Definition media_device.h:45
libcamera::MediaDevice::version
unsigned int version() const
Retrieve the media device API version.
Definition media_device.h:46
libcamera::MediaDevice::unlock
void unlock()
Unlock the device and free it for use for libcamera instances.
Definition media_device.cpp:164
libcamera::MediaEntity
The MediaEntity represents an entity in the media graph.
Definition media_object.h:97
libcamera::MediaLink
The MediaLink represents a link between two pads in the media graph.
Definition media_object.h:44
libcamera::MediaLink::setEnabled
int setEnabled(bool enable)
Enable or disable a link.
Definition media_object.cpp:122
libcamera::MediaObject
Base class for all media objects.
Definition media_object.h:24
libcamera::MediaPad
The MediaPad represents a pad of an entity in the media graph.
Definition media_object.h:69
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
log.h
Logging infrastructure.
media_object.h
Provides a class hierarchy that represents the media objects exposed by the Linux kernel Media Contro...
libcamera
Top-level libcamera namespace.
Definition backtrace.h:17
signal.h
Signal & slot implementation.
unique_fd.h
File descriptor wrapper that owns a file descriptor.
v4l2_request.h
V4L2 Request.