android: Guard access to the camera state

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>
This commit is contained in:
Jacopo Mondi 2021-05-10 12:05:59 +02:00
parent ebf8b5e7e0
commit 10b31904d8
2 changed files with 15 additions and 9 deletions

View file

@ -800,6 +800,7 @@ void CameraDevice::close()
void CameraDevice::stop()
{
MutexLocker stateLock(stateMutex_);
if (state_ == State::Stopped)
return;
@ -1900,6 +1901,9 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques
if (!isValidRequest(camera3Request))
return -EINVAL;
{
MutexLocker stateLock(stateMutex_);
/* Start the camera if that's the first request we handle. */
if (state_ == State::Stopped) {
worker_.start();
@ -1912,6 +1916,7 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques
state_ = State::Running;
}
}
/*
* Save the request descriptors for use at completion time.

View file

@ -120,6 +120,7 @@ private:
CameraWorker worker_;
libcamera::Mutex stateMutex_; /* Protects access to the camera state. */
State state_;
std::shared_ptr<libcamera::Camera> camera_;