[libcamera-devel] [PATCH v2 4/4] android: camera_hal_manager: Handle hot-unplug of currently open camera

Umang Jain email at uajain.com
Mon Aug 10 14:04:14 CEST 2020


Maintain an extra ref for the currently open camera in CameraHalManager.
This will ensure we have an graceful handling if the currently open
camera is hot-unplugged.

Signed-off-by: Umang Jain <email at uajain.com>
---
 src/android/camera_device.h        |  1 +
 src/android/camera_hal_manager.cpp | 18 +++++++++++++++---
 src/android/camera_hal_manager.h   |  2 ++
 3 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/src/android/camera_device.h b/src/android/camera_device.h
index 7f9e010..2dc0a20 100644
--- a/src/android/camera_device.h
+++ b/src/android/camera_device.h
@@ -61,6 +61,7 @@ public:
 
 	int facing() const { return facing_; }
 	int orientation() const { return orientation_; }
+	bool running() { return running_; }
 
 	void setCallbacks(const camera3_callback_ops_t *callbacks);
 	const camera_metadata_t *getStaticMetadata();
diff --git a/src/android/camera_hal_manager.cpp b/src/android/camera_hal_manager.cpp
index fdde2c0..9db5954 100644
--- a/src/android/camera_hal_manager.cpp
+++ b/src/android/camera_hal_manager.cpp
@@ -38,6 +38,8 @@ CameraHalManager::~CameraHalManager()
 	cameras_.clear();
 	camerasMap_.clear();
 
+	cameraInUse_.reset();
+
 	if (cameraManager_) {
 		cameraManager_->stop();
 		delete cameraManager_;
@@ -55,6 +57,7 @@ int CameraHalManager::init()
 
 	cameraCounter_ = 0;
 	externalCameraCounter_ = 1000;
+	cameraInUse_ = nullptr;
 
 	int ret = cameraManager_->start();
 	if (ret) {
@@ -82,14 +85,17 @@ CameraDevice *CameraHalManager::open(unsigned int id,
 		return nullptr;
 	}
 
-	CameraDevice *camera = iter->get();
+	if (cameraInUse_)
+		cameraInUse_.reset();
+
+	cameraInUse_ = std::move(*iter);
 
-	if (camera->open(hardwareModule))
+	if (cameraInUse_->open(hardwareModule))
 		return nullptr;
 
 	LOG(HAL, Info) << "Open camera '" << id << "'";
 
-	return camera;
+	return cameraInUse_.get();
 }
 
 void CameraHalManager::cameraAdded(std::shared_ptr<Camera> cam)
@@ -169,6 +175,12 @@ void CameraHalManager::cameraRemoved(std::shared_ptr<Camera> cam)
 	unsigned int id = (*iter)->id();
 	callbacks_->camera_device_status_change(callbacks_, id,
 						CAMERA_DEVICE_STATUS_NOT_PRESENT);
+
+	if (*iter == cameraInUse_ && cameraInUse_->running()) {
+		cameraInUse_->close();
+		cameraInUse_.reset();
+	}
+
 	cameras_.erase(iter);
 
 	LOG(HAL, Debug) << "Camera ID: " << id << " removed successfully.";
diff --git a/src/android/camera_hal_manager.h b/src/android/camera_hal_manager.h
index 7c481d4..974d53d 100644
--- a/src/android/camera_hal_manager.h
+++ b/src/android/camera_hal_manager.h
@@ -48,6 +48,8 @@ private:
 	std::map<std::string, unsigned int> camerasMap_;
 	Mutex mutex_;
 
+	std::shared_ptr<CameraDevice> cameraInUse_;
+
 	unsigned int externalCameraCounter_;
 	unsigned int cameraCounter_;
 };
-- 
2.26.2



More information about the libcamera-devel mailing list