@@ -169,6 +169,7 @@ private:
UniqueFD fd_;
bool valid_;
bool acquired_;
+ bool enumerated_;
std::optional<bool> supportsRequests_;
std::map<unsigned int, MediaObject *> objects_;
@@ -248,8 +248,6 @@ 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. */
@@ -66,7 +66,7 @@ LOG_DEFINE_CATEGORY(MediaDevice)
* populate() before the media graph can be queried.
*/
MediaDevice::MediaDevice(const std::string &deviceNode)
- : deviceNode_(deviceNode), valid_(false), acquired_(false)
+ : deviceNode_(deviceNode), valid_(false), acquired_(false), enumerated_(false)
{
}
@@ -111,7 +111,13 @@ bool MediaDevice::acquire()
if (open())
return false;
+ if (!enumerated_ && !createTopology()) {
+ close();
+ return false;
+ }
+
acquired_ = true;
+ enumerated_ = true;
return true;
}
@@ -322,13 +328,14 @@ bool MediaDevice::match(const DeviceMatch &device) const
const auto &searchFn, bool breakEarly) {
bool found = false;
- for (const MediaEntity *entity : entities()) {
- if (!searchFn(searchPattern, entity->name()))
+ for (const auto &ent : mediaV2Topology_.entities()) {
+ if (!searchFn(searchPattern, ent.name))
continue;
- if (entity->deviceNode().empty()) {
+ const auto &it = devnodesMap_.find(ent.id);
+ if (it == devnodesMap_.end()) {
LOG(MediaDevice, Debug)
- << "Skip " << entity->name()
+ << "Skip " << ent.name
<< ": no device node";
continue;
}
@@ -336,7 +343,7 @@ bool MediaDevice::match(const DeviceMatch &device) const
if (found) {
LOG(MediaDevice, Error)
<< "Multiple entities match "
- << entity->name();
+ << ent.name;
return false;
}
@@ -710,6 +717,7 @@ void MediaDevice::clear()
objects_.clear();
entities_.clear();
valid_ = false;
+ enumerated_ = false;
}
/**
@@ -46,6 +46,12 @@ protected:
return TestSkip;
}
+ if (!media_->acquire()) {
+ cerr << "Unable to acquire media device "
+ << media_->driver() << endl;
+ return TestFail;
+ }
+
MediaEntity *entity = media_->getEntityByName("Sensor A");
if (!entity) {
cerr << "Unable to find media entity 'Sensor A'" << endl;
@@ -121,6 +127,8 @@ protected:
void cleanup()
{
+ if (media_)
+ media_->release();
}
private:
@@ -47,6 +47,12 @@ protected:
return TestSkip;
}
+ if (!media_->acquire()) {
+ cerr << "Unable to acquire media device "
+ << media_->driver() << endl;
+ return TestFail;
+ }
+
dev_ = V4L2VideoDevice::fromEntityName(media_.get(), "vivid-000-vid-cap");
if (dev_->open()) {
cerr << "Failed to open video device" << endl;
@@ -70,6 +76,12 @@ protected:
return TestPass;
}
+ void cleanup() override
+ {
+ if (media_)
+ media_->release();
+ }
+
int singleControlNoDelay()
{
std::unordered_map<uint32_t, DelayedControls::ControlParams> delays = {
@@ -52,6 +52,12 @@ int BufferSource::allocate(const StreamConfiguration &config)
return TestSkip;
}
+ if (!media_->acquire()) {
+ std::cerr << "Unable to acquire media device "
+ << media_->driver() << std::endl;
+ return TestFail;
+ }
+
std::unique_ptr<V4L2VideoDevice> video = V4L2VideoDevice::fromEntityName(media_.get(),
videoDeviceName);
if (!video) {
@@ -46,6 +46,11 @@ int V4L2SubdeviceTest::init()
return TestSkip;
}
+ if (!media_->acquire()) {
+ cerr << "Unable to acquire media device " << media_->driver() << endl;
+ return TestFail;
+ }
+
MediaEntity *videoEntity = media_->getEntityByName("Scaler");
if (!videoEntity) {
cerr << "Unable to find media entity 'Scaler'" << endl;
@@ -64,5 +69,7 @@ int V4L2SubdeviceTest::init()
void V4L2SubdeviceTest::cleanup()
{
+ if (media_)
+ media_->release();
delete scaler_;
}
@@ -75,6 +75,12 @@ protected:
return TestSkip;
}
+ if (!media_->acquire()) {
+ cerr << "Unable to acquire media device "
+ << media_->driver() << endl;
+ return TestFail;
+ }
+
return TestPass;
}
@@ -193,6 +199,8 @@ protected:
void cleanup()
{
+ if (media_)
+ media_->release();
delete vim2m_;
}
Now that we have removed all external dependencies on the MediaObject class hierarchy, we can delay its creation at MediaDevice::acquire() time. The only remaining user is MediaDevice::match() which runs before acquire(). As this is an internal user, modify it to use the raw kernel types to perform matching so that it can be run before MediaObjects are created from the raw topology. Add an enumerated_ flag to avoid double enumeration on acquire/release/acquire cycles, which is effectivelly a use case only for test units. The additional flag is not an ideal solution but will only stay until the introduction of the MediaContext class. Now that MediaObjects are only available after acquire(), update all tests that create V4L2Devices by entity name to aquire the media device first. Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com> --- include/libcamera/internal/media_device.h | 1 + src/libcamera/device_enumerator.cpp | 2 -- src/libcamera/media_device.cpp | 20 ++++++++++++++------ test/camera-sensor.cpp | 8 ++++++++ test/delayed_controls.cpp | 12 ++++++++++++ test/libtest/buffer_source.cpp | 6 ++++++ test/v4l2_subdevice/v4l2_subdevice_test.cpp | 7 +++++++ test/v4l2_videodevice/v4l2_m2mdevice.cpp | 8 ++++++++ 8 files changed, 56 insertions(+), 8 deletions(-)