[RFC,v1,04/20] libcamera: device_enumerator: Add non-acquiring searchAll()
diff mbox series

Message ID 20260918080734.1228227-5-naush@raspberrypi.com
State New
Headers show
Series
  • libcamera: New enumeration API
Related show

Commit Message

Naushir Patuck Sept. 18, 2026, 7:59 a.m. UTC
Add a searchAll() function that returns all media devices matching a
DeviceMatch instead of the first match only.

Unlike the acquire and advance behaviour used with search() in pipeline
handler match() implementations, searchAll() allows callers to inspect
every matching media device without acquiring any of them. This will be
used by future commits for device enumeration.

Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
---
 .../libcamera/internal/device_enumerator.h    |  1 +
 src/libcamera/device_enumerator.cpp           | 31 +++++++++++++++++++
 2 files changed, 32 insertions(+)

Comments

Jacopo Mondi Oct. 16, 2026, 11:24 a.m. UTC | #1
Hi Naush

On Fri, Sep 18, 2026 at 08:59:42AM +0100, Naushir Patuck wrote:
> Add a searchAll() function that returns all media devices matching a
> DeviceMatch instead of the first match only.
>
> Unlike the acquire and advance behaviour used with search() in pipeline
> handler match() implementations, searchAll() allows callers to inspect
> every matching media device without acquiring any of them. This will be
> used by future commits for device enumeration.
>
> Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
> ---
>  .../libcamera/internal/device_enumerator.h    |  1 +
>  src/libcamera/device_enumerator.cpp           | 31 +++++++++++++++++++
>  2 files changed, 32 insertions(+)
>
> diff --git a/include/libcamera/internal/device_enumerator.h b/include/libcamera/internal/device_enumerator.h
> index 5e1aec9924c5..14fd5750a0ab 100644
> --- a/include/libcamera/internal/device_enumerator.h
> +++ b/include/libcamera/internal/device_enumerator.h
> @@ -45,6 +45,7 @@ public:
>  	virtual int enumerate() = 0;
>
>  	std::shared_ptr<MediaDevice> search(const DeviceMatch &dm);
> +	std::vector<std::shared_ptr<MediaDevice>> searchAll(const DeviceMatch &dm) const;

I'll probably have more interesting comments once I see this used, but
for now let me bikeshed.

Should we rename search() to find() and searchAll to list() ? or maybe
keep search() but maybe use list() for the new function ?

>
>  	Signal<> devicesAdded;
>
> diff --git a/src/libcamera/device_enumerator.cpp b/src/libcamera/device_enumerator.cpp
> index 0ce65e89a67f..6eaa52604a50 100644
> --- a/src/libcamera/device_enumerator.cpp
> +++ b/src/libcamera/device_enumerator.cpp
> @@ -362,4 +362,35 @@ std::shared_ptr<MediaDevice> DeviceEnumerator::search(const DeviceMatch &dm)
>  	return nullptr;
>  }
>
> +/**
> + * \brief Search available media devices for all pattern matches

Depending on the above suggestion:
        List all the available media devices that match \a dm

> + * \param[in] dm Search pattern
> + *
> + * Search in the enumerated media devices that are not already in use for
> + * matches described in \a dm. Unlike search(), all matching media devices are

Maybe:

 * List all the enumerated media devices that are not already in use
 * that match the pattern described by \a dm.

Just a suggestion

> + * returned instead of the first match only. The caller shall not use the

I would avoid comparing to search() if we use a different name.

> + * returned devices for capture without acquiring them first.

I would however here use the same text as search()

 * If the caller intends to use a MediaDevice from the returned list,
 * the caller is responsible for acquiring the MediaDevice object and
 * releasing it when done with it.

> + *
> + * \return A vector of matching MediaDevice instances, empty if no match is
> + * found
> + */
> +std::vector<std::shared_ptr<MediaDevice>> DeviceEnumerator::searchAll(const DeviceMatch &dm) const
> +{
> +	std::vector<std::shared_ptr<MediaDevice>> matches;
> +
> +	for (const std::shared_ptr<MediaDevice> &media : devices_) {
> +		if (media->busy())
> +			continue;
> +
> +		if (dm.match(media.get())) {
> +			LOG(DeviceEnumerator, Debug)
> +				<< "Successful match for media device \""
> +				<< media->driver() << "\"";
> +			matches.push_back(media);
> +		}
> +	}
> +
> +	return matches;
> +}
> +
>  } /* namespace libcamera */
> --
> 2.53.0
>

Patch
diff mbox series

diff --git a/include/libcamera/internal/device_enumerator.h b/include/libcamera/internal/device_enumerator.h
index 5e1aec9924c5..14fd5750a0ab 100644
--- a/include/libcamera/internal/device_enumerator.h
+++ b/include/libcamera/internal/device_enumerator.h
@@ -45,6 +45,7 @@  public:
 	virtual int enumerate() = 0;
 
 	std::shared_ptr<MediaDevice> search(const DeviceMatch &dm);
+	std::vector<std::shared_ptr<MediaDevice>> searchAll(const DeviceMatch &dm) const;
 
 	Signal<> devicesAdded;
 
diff --git a/src/libcamera/device_enumerator.cpp b/src/libcamera/device_enumerator.cpp
index 0ce65e89a67f..6eaa52604a50 100644
--- a/src/libcamera/device_enumerator.cpp
+++ b/src/libcamera/device_enumerator.cpp
@@ -362,4 +362,35 @@  std::shared_ptr<MediaDevice> DeviceEnumerator::search(const DeviceMatch &dm)
 	return nullptr;
 }
 
+/**
+ * \brief Search available media devices for all pattern matches
+ * \param[in] dm Search pattern
+ *
+ * Search in the enumerated media devices that are not already in use for
+ * matches described in \a dm. Unlike search(), all matching media devices are
+ * returned instead of the first match only. The caller shall not use the
+ * returned devices for capture without acquiring them first.
+ *
+ * \return A vector of matching MediaDevice instances, empty if no match is
+ * found
+ */
+std::vector<std::shared_ptr<MediaDevice>> DeviceEnumerator::searchAll(const DeviceMatch &dm) const
+{
+	std::vector<std::shared_ptr<MediaDevice>> matches;
+
+	for (const std::shared_ptr<MediaDevice> &media : devices_) {
+		if (media->busy())
+			continue;
+
+		if (dm.match(media.get())) {
+			LOG(DeviceEnumerator, Debug)
+				<< "Successful match for media device \""
+				<< media->driver() << "\"";
+			matches.push_back(media);
+		}
+	}
+
+	return matches;
+}
+
 } /* namespace libcamera */