libcamera  v0.5.0+49-e5442c31
Supporting cameras in Linux since 2019
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 <string>
12 #include <vector>
13 
14 #include <linux/media.h>
15 
16 #include <libcamera/base/log.h>
17 #include <libcamera/base/signal.h>
19 
21 
22 namespace libcamera {
23 
24 class MediaDevice : protected Loggable
25 {
26 public:
27  MediaDevice(const std::string &deviceNode);
28  ~MediaDevice();
29 
30  bool acquire();
31  void release();
32  bool busy() const { return acquired_; }
33 
34  bool lock();
35  void unlock();
36 
37  int populate();
38  bool isValid() const { return valid_; }
39 
40  const std::string &driver() const { return driver_; }
41  const std::string &deviceNode() const { return deviceNode_; }
42  const std::string &model() const { return model_; }
43  unsigned int version() const { return version_; }
44  unsigned int hwRevision() const { return hwRevision_; }
45 
46  const std::vector<MediaEntity *> &entities() const { return entities_; }
47  MediaEntity *getEntityByName(const std::string &name) const;
48 
49  MediaLink *link(const std::string &sourceName, unsigned int sourceIdx,
50  const std::string &sinkName, unsigned int sinkIdx);
51  MediaLink *link(const MediaEntity *source, unsigned int sourceIdx,
52  const MediaEntity *sink, unsigned int sinkIdx);
53  MediaLink *link(const MediaPad *source, const MediaPad *sink);
54  int disableLinks();
55 
57 
58  std::vector<MediaEntity *> locateEntities(unsigned int function);
59 
60 protected:
61  std::string logPrefix() const override;
62 
63 private:
64  int open();
65  void close();
66 
67  MediaObject *object(unsigned int id);
68  bool addObject(MediaObject *object);
69  void clear();
70 
71  struct media_v2_interface *findInterface(const struct media_v2_topology &topology,
72  unsigned int entityId);
73  bool populateEntities(const struct media_v2_topology &topology);
74  bool populatePads(const struct media_v2_topology &topology);
75  bool populateLinks(const struct media_v2_topology &topology);
76  void fixupEntityFlags(struct media_v2_entity *entity);
77 
78  friend int MediaLink::setEnabled(bool enable);
79  int setupLink(const MediaLink *link, unsigned int flags);
80 
81  std::string driver_;
82  std::string deviceNode_;
83  std::string model_;
84  unsigned int version_;
85  unsigned int hwRevision_;
86 
87  UniqueFD fd_;
88  bool valid_;
89  bool acquired_;
90 
91  std::map<unsigned int, MediaObject *> objects_;
92  std::vector<MediaEntity *> entities_;
93 };
94 
95 } /* namespace libcamera */
bool lock()
Lock the device to prevent it from being used by other instances of libcamera.
Definition: media_device.cpp:143
Provides a class hierarchy that represents the media objects exposed by the Linux kernel Media Contro...
const std::string & driver() const
Retrieve the media device driver name.
Definition: media_device.h:40
The MediaDevice represents a Media Controller device with its full graph of connected objects...
Definition: media_device.h:24
MediaDevice(const std::string &deviceNode)
Construct a MediaDevice.
Definition: media_device.cpp:66
bool acquire()
Claim a device for exclusive use.
Definition: media_device.cpp:104
bool isValid() const
Query whether the media graph has been populated and is valid.
Definition: media_device.h:38
unsigned int hwRevision() const
Retrieve the media device hardware revision.
Definition: media_device.h:44
File descriptor wrapper that owns a file descriptor.
MediaEntity * getEntityByName(const std::string &name) const
Return the MediaEntity with name name.
Definition: media_device.cpp:335
Base class to support log message extensions.
Definition: log.h:91
Top-level libcamera namespace.
Definition: backtrace.h:17
int disableLinks()
Disable all links in the media device.
Definition: media_device.cpp:436
int populate()
Populate the MediaDevice with device information and media objects.
Definition: media_device.cpp:194
std::string logPrefix() const override
Retrieve a string to be prefixed to the log message.
Definition: media_device.cpp:77
Base class for all media objects.
Definition: media_object.h:23
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:363
Signal disconnected
Signal emitted when the media device is disconnected from the system.
Definition: media_device.h:56
The MediaEntity represents an entity in the media graph.
Definition: media_object.h:96
const std::vector< MediaEntity * > & entities() const
Retrieve the list of entities in the media graph.
Definition: media_device.h:46
void release()
Release a device previously claimed for exclusive use.
Definition: media_device.cpp:120
std::vector< MediaEntity * > locateEntities(unsigned int function)
Identify all entities of a common function in the MediaDevice.
Definition: media_device.cpp:841
Signal & slot implementation.
unique_ptr-like wrapper for a file descriptor
Definition: unique_fd.h:16
void unlock()
Unlock the device and free it for use for libcamera instances.
Definition: media_device.cpp:163
Generic signal and slot communication mechanism.
Definition: object.h:22
const std::string & model() const
Retrieve the media device model name.
Definition: media_device.h:42
unsigned int version() const
Retrieve the media device API version.
Definition: media_device.h:43
Logging infrastructure.
const std::string & deviceNode() const
Retrieve the media device node path.
Definition: media_device.h:41
bool busy() const
Check if a device is in use.
Definition: media_device.h:32
The MediaPad represents a pad of an entity in the media graph.
Definition: media_object.h:68