Skip to content

Commit

Permalink
Clean up warnings when the camera is stopped
Browse files Browse the repository at this point in the history
Previously other threads might be trying to queue new requests while
the camera is being stopped. This could produce warnings if
queueRequest fails. Now the two procedures are protected from one
another with the camera_stop_mutex_.

Signed-off-by: David Plowman <[email protected]>
  • Loading branch information
davidplowman committed Jul 6, 2021
1 parent 6fd1804 commit e077b31
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
23 changes: 18 additions & 5 deletions libcamera_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,17 @@ void LibcameraApp::StartCamera()

void LibcameraApp::StopCamera()
{
{
// We don't want QueueRequest to run asynchronously while we stop the camera.
std::lock_guard<std::mutex> lock(camera_stop_mutex_);
if (camera_started_)
{
if (camera_->stop())
throw std::runtime_error("failed to stop camera");
camera_started_ = false;
}
}

if (camera_started_)
{
if (camera_->stop())
Expand Down Expand Up @@ -377,6 +388,12 @@ LibcameraApp::Msg LibcameraApp::Wait()

void LibcameraApp::QueueRequest(CompletedRequest const &completed_request)
{
// This function may run asynchronously so needs protection from the
// camera stopping at the same time.
std::lock_guard<std::mutex> stop_lock(camera_stop_mutex_);
if (!camera_started_)
return;

Request *request = nullptr;
{
std::lock_guard<std::mutex> lock(free_requests_mutex_);
Expand Down Expand Up @@ -404,11 +421,7 @@ void LibcameraApp::QueueRequest(CompletedRequest const &completed_request)
}

if (camera_->queueRequest(request) < 0)
{
// Don't make this fatal, some apps may stop the camera while the preview
// might still call us. (Arguably we should fix this better...)
std::cout << "(failed to queue request)" << std::endl;
}
throw std::runtime_error("failed to queue request");
}

void LibcameraApp::PostMessage(MsgType &t, MsgPayload &p)
Expand Down
1 change: 1 addition & 0 deletions libcamera_app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ class LibcameraApp
std::queue<Request *> free_requests_;
std::vector<std::unique_ptr<Request>> requests_;
bool camera_started_ = false;
std::mutex camera_stop_mutex_;
MessageQueue<Msg> msg_queue_;
// Related to the preview window.
std::unique_ptr<Preview> preview_;
Expand Down

0 comments on commit e077b31

Please sign in to comment.