[libcamera-devel,v4,6/8] android: Guard access to the camera state
diff mbox series

Message ID 20210527220359.30127-7-jacopo@jmondi.org
State Superseded
Delegated to: Jacopo Mondi
Headers show
Series
  • Implement flush() camera operation
Related show

Commit Message

Jacopo Mondi May 27, 2021, 10:03 p.m. UTC
Guard access to the camera state and the start/stop sequences
with a mutex.

Currently only stop() and the first call to processCaptureRequest()
start and stop the camera, and they're not meant to race with each
other. With the introduction of flush() the camera can be stopped
concurrently to a processCaptureRequest() call, hence access to the
camera state will need to be protected.

Prepare for that by guarding the existing paths with a mutex.

Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>
Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
Reviewed-by: Hirokazu Honda <hiroh@chromium.org>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 src/android/camera_device.cpp | 23 ++++++++++++++---------
 src/android/camera_device.h   |  1 +
 2 files changed, 15 insertions(+), 9 deletions(-)

Patch
diff mbox series

diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp
index 68c5e93b62fb..63f7f9d26ab6 100644
--- a/src/android/camera_device.cpp
+++ b/src/android/camera_device.cpp
@@ -799,6 +799,7 @@  void CameraDevice::close()
 
 void CameraDevice::stop()
 {
+	MutexLocker stateLock(stateMutex_);
 	if (state_ == State::Stopped)
 		return;
 
@@ -1898,17 +1899,21 @@  int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques
 	if (!isValidRequest(camera3Request))
 		return -EINVAL;
 
-	/* Start the camera if that's the first request we handle. */
-	if (state_ == State::Stopped) {
-		worker_.start();
+	{
+		MutexLocker stateLock(stateMutex_);
 
-		int ret = camera_->start();
-		if (ret) {
-			LOG(HAL, Error) << "Failed to start camera";
-			return ret;
-		}
+		/* Start the camera if that's the first request we handle. */
+		if (state_ == State::Stopped) {
+			worker_.start();
 
-		state_ = State::Running;
+			int ret = camera_->start();
+			if (ret) {
+				LOG(HAL, Error) << "Failed to start camera";
+				return ret;
+			}
+
+			state_ = State::Running;
+		}
 	}
 
 	/*
diff --git a/src/android/camera_device.h b/src/android/camera_device.h
index 70c29755bcfc..117d829ec932 100644
--- a/src/android/camera_device.h
+++ b/src/android/camera_device.h
@@ -120,6 +120,7 @@  private:
 
 	CameraWorker worker_;
 
+	libcamera::Mutex stateMutex_; /* Protects access to the camera state. */
 	State state_;
 
 	std::shared_ptr<libcamera::Camera> camera_;