libcamera v0.6.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/signal.h>
19#include <libcamera/base/unique_fd.h>
20
21#include "libcamera/internal/media_object.h"
22#include "libcamera/internal/v4l2_request.h"
23
24 namespace libcamera {
25
26 class MediaDevice : protected Loggable
27{
28 public:
29 MediaDevice(const std::string &deviceNode);
30 ~MediaDevice();
31
32 bool acquire();
33 void release();
34 bool busy() const { return acquired_; }
35
36 bool lock();
37 void unlock();
38
39 int populate();
40 bool isValid() const { return valid_; }
41
42 const std::string &driver() const { return driver_; }
43 const std::string &deviceNode() const { return deviceNode_; }
44 const std::string &model() const { return model_; }
45 unsigned int version() const { return version_; }
46 unsigned int hwRevision() const { return hwRevision_; }
47
48 const std::vector<MediaEntity *> &entities() const { return entities_; }
49 MediaEntity *getEntityByName(const std::string &name) const;
50
51 MediaLink *link(const std::string &sourceName, unsigned int sourceIdx,
52 const std::string &sinkName, unsigned int sinkIdx);
53 MediaLink *link(const MediaEntity *source, unsigned int sourceIdx,
54 const MediaEntity *sink, unsigned int sinkIdx);
55 MediaLink *link(const MediaPad *source, const MediaPad *sink);
56 int disableLinks();
57
58 Signal<> disconnected;
59
60 std::vector<MediaEntity *> locateEntities(unsigned int function);
61
62 int allocateRequests(unsigned int count,
63 std::vector<std::unique_ptr<V4L2Request>> *requests);
64
65 bool supportsRequests();
66
67 protected:
68 std::string logPrefix() const override;
69
70 private:
71 int open();
72 void close();
73
74 MediaObject *object(unsigned int id);
75 bool addObject(MediaObject *object);
76 void clear();
77
78 struct media_v2_interface *findInterface(const struct media_v2_topology &topology,
79 unsigned int entityId);
80 bool populateEntities(const struct media_v2_topology &topology);
81 bool populatePads(const struct media_v2_topology &topology);
82 bool populateLinks(const struct media_v2_topology &topology);
83 void fixupEntityFlags(struct media_v2_entity *entity);
84
85 friend int MediaLink::setEnabled(bool enable);
86 int setupLink(const MediaLink *link, unsigned int flags);
87
88 std::string driver_;
89 std::string deviceNode_;
90 std::string model_;
91 unsigned int version_;
92 unsigned int hwRevision_;
93
94 UniqueFD fd_;
95 bool valid_;
96 bool acquired_;
97 std::optional<bool> supportsRequests_;
98
99 std::map<unsigned int, MediaObject *> objects_;
100 std::vector<MediaEntity *> entities_;
101};
102
103} /* 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:865
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:437
libcamera::MediaDevice::isValid
bool isValid() const
Query whether the media graph has been populated and is valid.
Definition media_device.h:40
libcamera::MediaDevice::entities
const std::vector< MediaEntity * > & entities() const
Retrieve the list of entities in the media graph.
Definition media_device.h:48
libcamera::MediaDevice::busy
bool busy() const
Check if a device is in use.
Definition media_device.h:34
libcamera::MediaDevice::supportsRequests
bool supportsRequests()
Check if requests are supported.
Definition media_device.cpp:890
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:43
libcamera::MediaDevice::locateEntities
std::vector< MediaEntity * > locateEntities(unsigned int function)
Identify all entities of a common function in the MediaDevice.
Definition media_device.cpp:842
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:42
libcamera::MediaDevice::hwRevision
unsigned int hwRevision() const
Retrieve the media device hardware revision.
Definition media_device.h:46
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:364
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:58
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:44
libcamera::MediaDevice::version
unsigned int version() const
Retrieve the media device API version.
Definition media_device.h:45
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.