Skip to content

Commit

Permalink
Handle camera disconnect and reconnect. (#15)
Browse files Browse the repository at this point in the history
LibCameraJNI.startCamera() now truly returns whether starting the camera was successful or not. Logs properly showed "[ERROR] Couldn't start a zero copy Pi Camera while switching video modes".
  • Loading branch information
Juniormunk authored Jun 7, 2024
1 parent 2b7036f commit fb1eafb
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:

- uses: pguyot/arm-runner-action@v2
with:
base_image: https://downloads.raspberrypi.com/raspios_lite_arm64/images/raspios_lite_arm64-2023-12-11/2023-12-11-raspios-bookworm-arm64-lite.img.xz
base_image: https://downloads.raspberrypi.com/raspios_lite_arm64/images/raspios_lite_arm64-2024-03-15/2024-03-15-raspios-bookworm-arm64-lite.img.xz
cpu: cortex-a7
image_additional_mb: 1500
bind_mount_repository: true
Expand Down
2 changes: 1 addition & 1 deletion include/camera_grabber.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class CameraGrabber {

// Note: these 3 functions must be protected by mutual exclusion.
// Failure to do so will result in UB.
void startAndQueue();
bool startAndQueue();
void stop();
void requeueRequest(libcamera::Request *request);

Expand Down
2 changes: 1 addition & 1 deletion include/camera_runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class CameraRunner {

// Note: start and stop are not reenterant. Starting and stopping a camera
// repeatedly should work, but has not been thoroughly tested.
void start();
bool start();
void stop();

// Note: this is public but is a footgun. Destructing this class while a
Expand Down
7 changes: 4 additions & 3 deletions src/camera_grabber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,19 +206,20 @@ void CameraGrabber::setControls(libcamera::Request *request) {
}
}

void CameraGrabber::startAndQueue() {
bool CameraGrabber::startAndQueue() {
running = true;
if (m_camera->start()) {
throw std::runtime_error("failed to start camera");
return false;// failed to start camera
}

// TODO: HANDLE THIS BETTER
for (auto &request : m_requests) {
setControls(request.get());
if (m_camera->queueRequest(request.get()) < 0) {
throw std::runtime_error("failed to queue request");
return false; // failed to queue request
}
}
return true;
}

void CameraGrabber::stop() {
Expand Down
4 changes: 2 additions & 2 deletions src/camera_runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void CameraRunner::setCopyOptions(bool copyIn, bool copyOut) {
m_copyOutput = copyOut;
}

void CameraRunner::start() {
bool CameraRunner::start() {
unsigned int stride = grabber.streamConfiguration().stride;

latch start_frame_grabber{2};
Expand Down Expand Up @@ -242,7 +242,7 @@ void CameraRunner::start() {

{
std::lock_guard<std::mutex> lock{camera_stop_mutex};
grabber.startAndQueue();
return grabber.startAndQueue();
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/libcamera_jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ Java_org_photonvision_raspi_LibCameraJNI_startCamera
{
CameraRunner *runner = reinterpret_cast<CameraRunner *>(runner_);

runner->start();
return true;

return runner->start();
}

/*
Expand Down

0 comments on commit fb1eafb

Please sign in to comment.