[1/6] libcamera: Move DeviceMatch::match() to MediaDevice
diff mbox series

Message ID 20261119-multicontext-prep-v1-1-f68ea7ef960f@ideasonboard.com
State New
Headers show
Series
  • libcamera: Prepare to add MediaContext
Related show

Commit Message

Jacopo Mondi Nov. 19, 2026, 4:39 p.m. UTC
The DeviceMatch class offers a match() function that compares its
search criteria with the MediaDevice driver name and all its entities
names.

To prepare to remove all users of the entities() function from the
DeviceEnumerator and MediaDevice users, move the match() function
to the MediaDevice class. Add accessors to the DeviceMatch class
so that the MediaDevice can compare itself to the search patterns.

While at it perform a little rename of the DeviceMatch class members to
allow to have more expressive function names, to maintain the convention
that accessors have the same name as the fields they return.

Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
---
 include/libcamera/internal/device_enumerator.h | 10 +--
 include/libcamera/internal/media_device.h      |  4 ++
 src/libcamera/device_enumerator.cpp            | 89 ++++++--------------------
 src/libcamera/media_device.cpp                 | 69 ++++++++++++++++++++
 4 files changed, 100 insertions(+), 72 deletions(-)

Patch
diff mbox series

diff --git a/include/libcamera/internal/device_enumerator.h b/include/libcamera/internal/device_enumerator.h
index 5e1aec9924c5..5d142c2e0b31 100644
--- a/include/libcamera/internal/device_enumerator.h
+++ b/include/libcamera/internal/device_enumerator.h
@@ -23,15 +23,17 @@  class DeviceMatch
 public:
 	DeviceMatch(const std::string &driver);
 
+	const std::string &driver() const { return driver_; }
+	const std::vector<std::string> &entitiesNames() const { return entitiesNames_; }
+	const std::vector<std::regex> &entitiesRegexs() const { return entitiesRegexs_; }
+
 	void add(const std::string &entity);
 	void add(std::regex entity);
 
-	bool match(const MediaDevice *device) const;
-
 private:
 	std::string driver_;
-	std::vector<std::string> entities_;
-	std::vector<std::regex> entityRegexs_;
+	std::vector<std::string> entitiesNames_;
+	std::vector<std::regex> entitiesRegexs_;
 };
 
 class DeviceEnumerator
diff --git a/include/libcamera/internal/media_device.h b/include/libcamera/internal/media_device.h
index 5c1f14b63823..c1405142f726 100644
--- a/include/libcamera/internal/media_device.h
+++ b/include/libcamera/internal/media_device.h
@@ -24,6 +24,8 @@ 
 
 namespace libcamera {
 
+class DeviceMatch;
+
 class MediaDevice : protected Loggable
 {
 public:
@@ -40,6 +42,8 @@  public:
 	int populate();
 	bool isValid() const { return valid_; }
 
+	bool match(const DeviceMatch &device) const;
+
 	const std::string &driver() const { return driver_; }
 	const std::string &deviceNode() const { return deviceNode_; }
 	const std::string &model() const { return model_; }
diff --git a/src/libcamera/device_enumerator.cpp b/src/libcamera/device_enumerator.cpp
index 0ce65e89a67f..2b1056425f61 100644
--- a/src/libcamera/device_enumerator.cpp
+++ b/src/libcamera/device_enumerator.cpp
@@ -73,13 +73,31 @@  DeviceMatch::DeviceMatch(const std::string &driver)
 {
 }
 
+/**
+ * \fn DeviceMatch::driver()
+ * \brief Retrieve the target media device driver name
+ * \return The media device driver name
+ */
+
+/**
+ * \fn DeviceMatch::entitiesNames()
+ * \brief Retrieve the list of target media entities names
+ * \return A vector of media entities names
+ */
+
+/**
+ * \fn DeviceMatch::entitiesRegexs()
+ * \brief Retrieve the list of target media entities search patterns
+ * \return A vector of media entities search patterns
+ */
+
 /**
  * \brief Add a media entity name to the search pattern
  * \param[in] entity The name of the entity in the media graph
  */
 void DeviceMatch::add(const std::string &entity)
 {
-	entities_.push_back(entity);
+	entitiesNames_.push_back(entity);
 }
 
 /**
@@ -88,72 +106,7 @@  void DeviceMatch::add(const std::string &entity)
  */
 void DeviceMatch::add(std::regex entity)
 {
-	entityRegexs_.push_back(std::move(entity));
-}
-
-/**
- * \brief Compare a search pattern with a media device
- * \param[in] device The media device
- *
- * Matching is performed on the Linux device driver name and entity names from
- * the media graph. A match is found if both the driver name matches and the
- * media device contains all the entities listed in the search pattern.
- *
- * \return true if the media device matches the search pattern, false otherwise
- */
-bool DeviceMatch::match(const MediaDevice *device) const
-{
-	if (driver_ != device->driver())
-		return false;
-
-	for (const std::string &name : entities_) {
-		bool found = false;
-
-		for (const MediaEntity *entity : device->entities()) {
-			if (name == entity->name()) {
-				if (!entity->deviceNode().empty()) {
-					found = true;
-					break;
-				} else {
-					LOG(DeviceEnumerator, Debug)
-						<< "Skip " << entity->name()
-						<< ": no device node";
-				}
-			}
-		}
-
-		if (!found)
-			return false;
-	}
-
-	for (const std::regex &nameRegex : entityRegexs_) {
-		bool found = false;
-
-		for (const MediaEntity *entity : device->entities()) {
-			if (!std::regex_search(entity->name(), nameRegex))
-				continue;
-
-			if (found) {
-				LOG(DeviceEnumerator, Error)
-					<< "Multiple entities match regex";
-				return false;
-			}
-
-			if (entity->deviceNode().empty()) {
-				LOG(DeviceEnumerator, Debug)
-					<< "Skip " << entity->name()
-					<< ": no device node";
-				continue;
-			}
-
-			found = true;
-		}
-
-		if (!found)
-			return false;
-	}
-
-	return true;
+	entitiesRegexs_.push_back(std::move(entity));
 }
 
 /**
@@ -351,7 +304,7 @@  std::shared_ptr<MediaDevice> DeviceEnumerator::search(const DeviceMatch &dm)
 		if (media->busy())
 			continue;
 
-		if (dm.match(media.get())) {
+		if (media->match(dm)) {
 			LOG(DeviceEnumerator, Debug)
 				<< "Successful match for media device \""
 				<< media->driver() << "\"";
diff --git a/src/libcamera/media_device.cpp b/src/libcamera/media_device.cpp
index 3f974eaabd67..b50d7328e47c 100644
--- a/src/libcamera/media_device.cpp
+++ b/src/libcamera/media_device.cpp
@@ -20,6 +20,7 @@ 
 #include <linux/media.h>
 
 #include <libcamera/base/log.h>
+#include "libcamera/internal/device_enumerator.h"
 #include "libcamera/internal/v4l2_request.h"
 
 /**
@@ -286,6 +287,74 @@  done:
  * \return true if the media graph is valid, false otherwise
  */
 
+/**
+ * \brief Compare a media device with a search pattern
+ * \param[in] device The media device search pattern
+ *
+ * Matching is performed on the Linux device driver name and entity names from
+ * the \a device search pattern. A match is found if both the driver name
+ * matches and the media device contains all the entities listed in the search
+ * pattern.
+ *
+ * \return True if the media device matches the search pattern, false otherwise
+ */
+bool MediaDevice::match(const DeviceMatch &device) const
+{
+	ASSERT(isValid());
+
+	if (driver_ != device.driver())
+		return false;
+
+	for (const std::string &name : device.entitiesNames()) {
+		bool found = false;
+
+		for (const MediaEntity *entity : entities()) {
+			if (name == entity->name()) {
+				if (!entity->deviceNode().empty()) {
+					found = true;
+					break;
+				} else {
+					LOG(MediaDevice, Debug)
+						<< "Skip " << entity->name()
+						<< ": no device node";
+				}
+			}
+		}
+
+		if (!found)
+			return false;
+	}
+
+	for (const std::regex &nameRegex : device.entitiesRegexs()) {
+		bool found = false;
+
+		for (const MediaEntity *entity : entities()) {
+			if (!std::regex_search(entity->name(), nameRegex))
+				continue;
+
+			if (found) {
+				LOG(MediaDevice, Error)
+					<< "Multiple entities match regex";
+				return false;
+			}
+
+			if (entity->deviceNode().empty()) {
+				LOG(MediaDevice, Debug)
+					<< "Skip " << entity->name()
+					<< ": no device node";
+				continue;
+			}
+
+			found = true;
+		}
+
+		if (!found)
+			return false;
+	}
+
+	return true;
+}
+
 /**
  * \fn MediaDevice::driver()
  * \brief Retrieve the media device driver name