libcamera  v0.0.0
Supporting cameras in Linux since 2019
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.h - Media device handler
6  */
7 #ifndef __LIBCAMERA_INTERNAL_MEDIA_DEVICE_H__
8 #define __LIBCAMERA_INTERNAL_MEDIA_DEVICE_H__
9 
10 #include <map>
11 #include <sstream>
12 #include <string>
13 #include <vector>
14 
15 #include <linux/media.h>
16 
17 #include <libcamera/signal.h>
18 
19 #include "libcamera/internal/log.h"
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 valid() 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 
45  const std::vector<MediaEntity *> &entities() const { return entities_; }
46  MediaEntity *getEntityByName(const std::string &name) const;
47 
48  MediaLink *link(const std::string &sourceName, unsigned int sourceIdx,
49  const std::string &sinkName, unsigned int sinkIdx);
50  MediaLink *link(const MediaEntity *source, unsigned int sourceIdx,
51  const MediaEntity *sink, unsigned int sinkIdx);
52  MediaLink *link(const MediaPad *source, const MediaPad *sink);
53  int disableLinks();
54 
56 
57 protected:
58  std::string logPrefix() const override;
59 
60 private:
61  int open();
62  void close();
63 
64  MediaObject *object(unsigned int id);
65  bool addObject(MediaObject *object);
66  void clear();
67 
68  struct media_v2_interface *findInterface(const struct media_v2_topology &topology,
69  unsigned int entityId);
70  bool populateEntities(const struct media_v2_topology &topology);
71  bool populatePads(const struct media_v2_topology &topology);
72  bool populateLinks(const struct media_v2_topology &topology);
73  void fixupEntityFlags(struct media_v2_entity *entity);
74 
75  friend int MediaLink::setEnabled(bool enable);
76  int setupLink(const MediaLink *link, unsigned int flags);
77 
78  std::string driver_;
79  std::string deviceNode_;
80  std::string model_;
81  unsigned int version_;
82 
83  int fd_;
84  bool valid_;
85  bool acquired_;
86  bool lockOwner_;
87 
88  std::map<unsigned int, MediaObject *> objects_;
89  std::vector<MediaEntity *> entities_;
90 };
91 
92 } /* namespace libcamera */
93 
94 #endif /* __LIBCAMERA_INTERNAL_MEDIA_DEVICE_H__ */
Base class to support log message extensions.
Definition: log.h:83
The MediaDevice represents a Media Controller device with its full graph of connected objects.
Definition: media_device.h:25
bool acquire()
Claim a device for exclusive use.
Definition: media_device.cpp:105
std::string logPrefix() const override
Retrieve a string to be prefixed to the log message.
Definition: media_device.cpp:78
int disableLinks()
Disable all links in the media device.
Definition: media_device.cpp:434
void release()
Release a device previously claimed for exclusive use.
Definition: media_device.cpp:121
Signal< MediaDevice * > disconnected
Signal emitted when the media device is disconnected from the system.
Definition: media_device.h:55
const std::string driver() const
Retrieve the media device driver name.
Definition: media_device.h:40
const std::string model() const
Retrieve the media device model name.
Definition: media_device.h:42
int populate()
Populate the MediaDevice with device information and media objects.
Definition: media_device.cpp:206
const std::string deviceNode() const
Retrieve the media device node path.
Definition: media_device.h:41
bool lock()
Lock the device to prevent it from being used by other instances of libcamera.
Definition: media_device.cpp:144
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
MediaDevice(const std::string &deviceNode)
Construct a MediaDevice.
Definition: media_device.cpp:65
const std::vector< MediaEntity * > & entities() const
Retrieve the list of entities in the media graph.
Definition: media_device.h:45
MediaEntity * getEntityByName(const std::string &name) const
Return the MediaEntity with name name.
Definition: media_device.cpp:337
bool busy() const
Check if a device is in use.
Definition: media_device.h:32
bool valid() const
Query whether the media graph has been populated and is valid.
Definition: media_device.h:38
void unlock()
Unlock the device and free it for use for libcamera instances.
Definition: media_device.cpp:170
unsigned int version() const
Retrieve the media device API version.
Definition: media_device.h:43
The MediaEntity represents an entity in the media graph.
Definition: media_object.h:86
Base class for all media objects.
Definition: media_object.h:22
The MediaPad represents a pad of an entity in the media graph.
Definition: media_object.h:62
Generic signal and slot communication mechanism.
Definition: signal.h:39
Logging infrastructure.
Provides a class hierarchy that represents the media objects exposed by the Linux kernel Media Contro...
Signal & slot implementation.