@@ -27,7 +27,6 @@ namespace libcamera {
class EventNotifier;
class MediaDevice;
-class MediaEntity;
class DeviceEnumeratorUdev final : public DeviceEnumerator
{
@@ -39,7 +38,7 @@ public:
int enumerate();
private:
- using DependencyMap = std::map<dev_t, std::list<MediaEntity *>>;
+ using DependencyMap = std::map<dev_t, std::list<unsigned int>>;
struct MediaDeviceDeps {
MediaDeviceDeps(std::unique_ptr<MediaDevice> media,
@@ -42,6 +42,7 @@ public:
void unlock();
int populate();
+ int createTopology();
bool isValid() const { return valid_; }
bool match(const DeviceMatch &device) const;
@@ -63,6 +64,12 @@ public:
return devnumsMap_.at(devnum);
}
+ void setEntityDeviceNode(unsigned int id, const std::string &devnode)
+ {
+ ASSERT(!devnodesMap_.contains(id));
+ devnodesMap_[id] = devnode;
+ }
+
const std::vector<MediaEntity *> &entities() const { return entities_; }
MediaEntity *getEntityByName(const std::string &name) const;
MediaEntity *getEntityByName(const std::regex &name) const;
@@ -157,6 +164,7 @@ private:
} mediaV2Topology_;
std::map<dev_t, std::list<unsigned int>> devnumsMap_;
+ std::map<unsigned int, std::string> devnodesMap_;
UniqueFD fd_;
bool valid_;
@@ -248,6 +248,8 @@ void DeviceEnumerator::addDevice(std::unique_ptr<MediaDevice> media)
LOG(DeviceEnumerator, Debug)
<< "Added device " << media->deviceNode() << ": " << media->driver();
+ media->createTopology();
+
devices_.push_back(std::move(media));
/* \todo To batch multiple additions, emit with a small delay here. */
@@ -96,18 +96,17 @@ int DeviceEnumeratorSysfs::enumerate()
int DeviceEnumeratorSysfs::populateMediaDevice(MediaDevice *media)
{
/* Associate entities to device node paths. */
- for (MediaEntity *entity : media->entities()) {
- if (entity->deviceMajor() == 0 && entity->deviceMinor() == 0)
- continue;
+ for (dev_t devnum : media->entitiesDevnums()) {
+ unsigned int major = major(devnum);
+ unsigned int minor = minor(devnum);
- std::string deviceNode = lookupDeviceNode(entity->deviceMajor(),
- entity->deviceMinor());
+ std::string deviceNode = lookupDeviceNode(major, minor);
if (deviceNode.empty())
return -EINVAL;
- int ret = entity->setDeviceNode(deviceNode);
- if (ret)
- return ret;
+ const std::list<unsigned int> &entitiesIds = media->entitiesIdsByDevNum(devnum);
+ for (unsigned int id : entitiesIds)
+ media->setEntityDeviceNode(id, deviceNode);
}
return 0;
@@ -221,19 +221,20 @@ int DeviceEnumeratorUdev::populateMediaDevice(MediaDevice *media, DependencyMap
std::set<dev_t> children;
/* Associate entities to device node paths. */
- for (MediaEntity *entity : media->entities()) {
- if (entity->deviceMajor() == 0 && entity->deviceMinor() == 0)
- continue;
+ for (dev_t devnum : media->entitiesDevnums()) {
- dev_t devnum = makedev(entity->deviceMajor(),
- entity->deviceMinor());
+ const std::list<unsigned int> &entitiesIds =
+ media->entitiesIdsByDevNum(devnum);
/*
- * If the devnum isn't in the orphans list, add it to the unmet
- * dependencies.
+ * If the devnum isn't in the orphans list, add all the entities
+ * ids to the list of dependencies.
*/
if (orphans_.find(devnum) == orphans_.end()) {
- (*deps)[devnum].push_back(entity);
+ auto &it = (*deps)[devnum];
+ for (unsigned int id : entitiesIds)
+ it.push_back(id);
+
continue;
}
@@ -246,9 +247,8 @@ int DeviceEnumeratorUdev::populateMediaDevice(MediaDevice *media, DependencyMap
if (deviceNode.empty())
return -EINVAL;
- int ret = entity->setDeviceNode(deviceNode);
- if (ret)
- return ret;
+ for (unsigned int id : entitiesIds)
+ media->setEntityDeviceNode(id, deviceNode);
children.insert(devnum);
}
@@ -326,11 +326,8 @@ int DeviceEnumeratorUdev::addV4L2Device(dev_t devnum)
return -EINVAL;
MediaDeviceDeps *deps = it->second;
- for (MediaEntity *entity : deps->deps_[devnum]) {
- int ret = entity->setDeviceNode(deviceNode);
- if (ret)
- return ret;
- }
+ for (unsigned int id : deps->deps_[devnum])
+ deps->media_->setEntityDeviceNode(id, deviceNode);
/*
* Remove the devnum from the unmet dependencies for this media device.
@@ -260,12 +260,7 @@ int MediaDevice::populate()
it->second.push_back(ent.id);
}
- /* Populate entities, pads and links. */
- if (populateEntities(mediaV2Topology_.topology_) &&
- populatePads(mediaV2Topology_.topology_) &&
- populateLinks(mediaV2Topology_.topology_))
- valid_ = true;
-
+ valid_ = true;
ret = 0;
done:
close();
@@ -278,6 +273,27 @@ done:
return ret;
}
+/**
+ * \brief Create MediaObjects from a populated topology
+ *
+ * Populate the MediaObjects list and create MediaEntities, MediaPads and
+ * MediaLinks by parsing the topology populated at MediaDevice::populate()
+ * time.
+ *
+ * Requires the topology to be valid.
+ *
+ * \todo This function will be move to a new MediaContext type which will
+ * be created at MediaDevice::acquire() time.
+ */
+int MediaDevice::createTopology()
+{
+ ASSERT(valid_);
+
+ return populateEntities(mediaV2Topology_.topology_) &&
+ populatePads(mediaV2Topology_.topology_) &&
+ populateLinks(mediaV2Topology_.topology_);
+}
+
/**
* \fn MediaDevice::isValid()
* \brief Query whether the media graph has been populated and is valid
@@ -409,6 +425,16 @@ bool MediaDevice::match(const DeviceMatch &device) const
* \return A list of media entities ids
*/
+/**
+ * \fn MediaDevice::setEntityDeviceNode(unsigned in id, const std::string &devnode)
+ * \brief Associate the device node path \a devnode to the entity \a id
+ * \param[in] id The media entity id
+ * \param[in] devnode The device node path
+ *
+ * Associate to the media entity identified by \a id the path of its device
+ * node \a devnode.
+ */
+
/**
* \fn MediaDevice::entities()
* \brief Retrieve the list of entities in the media graph
@@ -757,6 +783,14 @@ bool MediaDevice::populateEntities(const struct media_v2_topology &topology)
findInterface(topology, ent->id);
MediaEntity *entity = new MediaEntity(this, ent, iface);
+ /* Inject the devnode path for entities with a devnum. */
+ auto it = devnodesMap_.find(entity->id());
+ if (it != devnodesMap_.end()) {
+ int ret = entity->setDeviceNode(it->second);
+ if (ret)
+ return false;
+ }
+
if (!addObject(entity)) {
delete entity;
return false;
The DeviceEnumeratorUdev and DeviceEnumeratorSysfs populates the media graph in two steps. Initially, at DeviceEnumerator::createDevice() time, the media device is created and its media topology enumerated in MediaDevice::populate(). Then the device node path is injected in the corresponding MediaEntity. To progress with the de-coupling of the DeviceEnumerator hierarchy from the MediaObject class, remove all usages of MediaEntity from the DeviceEnumerator and replace it with entities ids and the devnum-based interface of the MediaDevice class. The MediaDevice collects a list of media entities ids for each devnum it identifies during topology enumeration. The device enumerator uses those devnums to calculate the device node paths and passes them to the MediaDevice which stores them in a map of entity ids to devnode paths. When the MediaObjects are created later on the devnode paths are fetched from the map and injected in the MediaEntity. This change requires to delay the creation of MediaObjects after the enumerator has populated the devnode paths so that a valid path is available for each MediaEntity. Split the MediaObject creation to a dedicated function MediaDevice::createTopology() and call it in DeviceEnumerator just before adding the new MediaDevice. This nicely fit with the design goal of delaying MediaObject creation to a later point in time in the next patches, precisely when the MediaDevice is acquired. Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com> --- .../libcamera/internal/device_enumerator_udev.h | 3 +- include/libcamera/internal/media_device.h | 8 ++++ src/libcamera/device_enumerator.cpp | 2 + src/libcamera/device_enumerator_sysfs.cpp | 15 ++++--- src/libcamera/device_enumerator_udev.cpp | 29 ++++++-------- src/libcamera/media_device.cpp | 46 +++++++++++++++++++--- 6 files changed, 71 insertions(+), 32 deletions(-)