[libcamera-devel,v3,2/5] libcamera: device_enumerator: Emit a signal when a new devices are added

Message ID 20200521135416.13685-3-email@uajain.com
State Superseded
Headers show
Series
  • Introduce UVC hotplugging support
Related show

Commit Message

Umang Jain May 21, 2020, 1:54 p.m. UTC
Emit a signal whenever a new MediaDevice(s) are added to the
DeviceEnumerator. This will allow CameraManager to be notified
about the new devices and it can re-emumerate all the devices
currently present on the system.

Device enumeration by the CameraManger is a expensive operation hence,
we want one signal emission per 'x' milliseconds to notify multiple
devices additions as a single batch, by the DeviceEnumerator.
Add a \todo to investigate the support for that.

Signed-off-by: Umang Jain <email@uajain.com>
---
 include/libcamera/internal/device_enumerator.h |  4 ++++
 src/libcamera/camera_manager.cpp               |  4 ++--
 src/libcamera/device_enumerator.cpp            | 13 +++++++++++++
 3 files changed, 19 insertions(+), 2 deletions(-)

Comments

Laurent Pinchart June 7, 2020, 4:28 p.m. UTC | #1
Hi Umang,

Thank you for the patch.

On Thu, May 21, 2020 at 01:54:25PM +0000, Umang Jain wrote:
> Emit a signal whenever a new MediaDevice(s) are added to the

s/a new MediaDevice(s)/new MediaDevices/

> DeviceEnumerator. This will allow CameraManager to be notified
> about the new devices and it can re-emumerate all the devices
> currently present on the system.
> 
> Device enumeration by the CameraManger is a expensive operation hence,
> we want one signal emission per 'x' milliseconds to notify multiple
> devices additions as a single batch, by the DeviceEnumerator.
> Add a \todo to investigate the support for that.
> 
> Signed-off-by: Umang Jain <email@uajain.com>
> ---
>  include/libcamera/internal/device_enumerator.h |  4 ++++
>  src/libcamera/camera_manager.cpp               |  4 ++--
>  src/libcamera/device_enumerator.cpp            | 13 +++++++++++++
>  3 files changed, 19 insertions(+), 2 deletions(-)
> 
> diff --git a/include/libcamera/internal/device_enumerator.h b/include/libcamera/internal/device_enumerator.h
> index 433e357..aa6a6fe 100644
> --- a/include/libcamera/internal/device_enumerator.h
> +++ b/include/libcamera/internal/device_enumerator.h
> @@ -13,6 +13,8 @@
>  
>  #include <linux/media.h>
>  
> +#include <libcamera/signal.h>
> +
>  namespace libcamera {
>  
>  class MediaDevice;
> @@ -43,6 +45,8 @@ public:
>  
>  	std::shared_ptr<MediaDevice> search(const DeviceMatch &dm);
>  
> +	Signal<> deviceAdded;

Maybe s/deviceAdded/devicesAdded/ to reflect there can be multiple
devices ?

> +
>  protected:
>  	std::unique_ptr<MediaDevice> createDevice(const std::string &deviceNode);
>  	void addDevice(std::unique_ptr<MediaDevice> &&media);
> diff --git a/src/libcamera/camera_manager.cpp b/src/libcamera/camera_manager.cpp
> index 8d9cb02..27064d2 100644
> --- a/src/libcamera/camera_manager.cpp
> +++ b/src/libcamera/camera_manager.cpp
> @@ -153,12 +153,12 @@ void CameraManager::Private::enumerateDevices()
>  		}
>  	}
>  
> -	/* \todo register hot-plug callback here */
> +	enumerator_->deviceAdded.connect(this, &Private::enumerateDevices);
>  }
>  
>  void CameraManager::Private::cleanup()
>  {
> -	/* \todo unregister hot-plug callback here */
> +	enumerator_->deviceAdded.disconnect(this, &Private::enumerateDevices);
>  
>  	/*
>  	 * Release all references to cameras and pipeline handlers to ensure
> diff --git a/src/libcamera/device_enumerator.cpp b/src/libcamera/device_enumerator.cpp
> index e21a2a7..6c5ebb7 100644
> --- a/src/libcamera/device_enumerator.cpp
> +++ b/src/libcamera/device_enumerator.cpp
> @@ -227,6 +227,16 @@ std::unique_ptr<MediaDevice> DeviceEnumerator::createDevice(const std::string &d
>  	return media;
>  }
>  
> +/**
> +* \var DeviceEnumerator::deviceAdded
> +* \brief Notify of new media devices being found
> +*
> +* This signal is emitted when the device enumerator finds new media devices in
> +* the system. It may be emitted for every newly detected device, or once for
> +* multiple of devices, at the discretion of the device enumerator. Not all
> +* device enumerator types may support dynamic detection of new devices.
> +*/
> +
>  /**
>   * \brief Add a media device to the enumerator
>   * \param[in] media media device instance to add
> @@ -242,6 +252,9 @@ void DeviceEnumerator::addDevice(std::unique_ptr<MediaDevice> &&media)
>  		<< "Added device " << media->deviceNode() << ": " << media->driver();
>  
>  	devices_.push_back(std::move(media));
> +
> +	/* \todo: To batch multiple additions, emit with a small delay here. */

s/todo:/todo/

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

> +	deviceAdded.emit();
>  }
>  
>  /**

Patch

diff --git a/include/libcamera/internal/device_enumerator.h b/include/libcamera/internal/device_enumerator.h
index 433e357..aa6a6fe 100644
--- a/include/libcamera/internal/device_enumerator.h
+++ b/include/libcamera/internal/device_enumerator.h
@@ -13,6 +13,8 @@ 
 
 #include <linux/media.h>
 
+#include <libcamera/signal.h>
+
 namespace libcamera {
 
 class MediaDevice;
@@ -43,6 +45,8 @@  public:
 
 	std::shared_ptr<MediaDevice> search(const DeviceMatch &dm);
 
+	Signal<> deviceAdded;
+
 protected:
 	std::unique_ptr<MediaDevice> createDevice(const std::string &deviceNode);
 	void addDevice(std::unique_ptr<MediaDevice> &&media);
diff --git a/src/libcamera/camera_manager.cpp b/src/libcamera/camera_manager.cpp
index 8d9cb02..27064d2 100644
--- a/src/libcamera/camera_manager.cpp
+++ b/src/libcamera/camera_manager.cpp
@@ -153,12 +153,12 @@  void CameraManager::Private::enumerateDevices()
 		}
 	}
 
-	/* \todo register hot-plug callback here */
+	enumerator_->deviceAdded.connect(this, &Private::enumerateDevices);
 }
 
 void CameraManager::Private::cleanup()
 {
-	/* \todo unregister hot-plug callback here */
+	enumerator_->deviceAdded.disconnect(this, &Private::enumerateDevices);
 
 	/*
 	 * Release all references to cameras and pipeline handlers to ensure
diff --git a/src/libcamera/device_enumerator.cpp b/src/libcamera/device_enumerator.cpp
index e21a2a7..6c5ebb7 100644
--- a/src/libcamera/device_enumerator.cpp
+++ b/src/libcamera/device_enumerator.cpp
@@ -227,6 +227,16 @@  std::unique_ptr<MediaDevice> DeviceEnumerator::createDevice(const std::string &d
 	return media;
 }
 
+/**
+* \var DeviceEnumerator::deviceAdded
+* \brief Notify of new media devices being found
+*
+* This signal is emitted when the device enumerator finds new media devices in
+* the system. It may be emitted for every newly detected device, or once for
+* multiple of devices, at the discretion of the device enumerator. Not all
+* device enumerator types may support dynamic detection of new devices.
+*/
+
 /**
  * \brief Add a media device to the enumerator
  * \param[in] media media device instance to add
@@ -242,6 +252,9 @@  void DeviceEnumerator::addDevice(std::unique_ptr<MediaDevice> &&media)
 		<< "Added device " << media->deviceNode() << ": " << media->driver();
 
 	devices_.push_back(std::move(media));
+
+	/* \todo: To batch multiple additions, emit with a small delay here. */
+	deviceAdded.emit();
 }
 
 /**