Skip to content

Commit

Permalink
emit error signal in case of driver errors (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 authored Nov 30, 2024
1 parent e2ceff4 commit b62a9cb
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 23 deletions.
20 changes: 16 additions & 4 deletions camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ static PixelFormat mode_to_pixel_format(sensor_mode_t *mode) {
struct CameraPriv {
const parameters_t *params;
camera_frame_cb frame_cb;
camera_error_cb error_cb;
std::unique_ptr<CameraManager> camera_manager;
std::shared_ptr<Camera> camera;
Stream *video_stream;
Expand All @@ -107,6 +108,7 @@ struct CameraPriv {
std::map<FrameBuffer *, uint8_t *> mapped_buffers;
bool ts_initialized;
uint64_t ts_start;
bool in_error;
};

static int get_v4l2_colorspace(std::optional<ColorSpace> const &cs) {
Expand All @@ -133,7 +135,11 @@ static void set_hdr(bool hdr) {
}
}

bool camera_create(const parameters_t *params, camera_frame_cb frame_cb, camera_t **cam) {
bool camera_create(
const parameters_t *params,
camera_frame_cb frame_cb,
camera_error_cb error_cb,
camera_t **cam) {
std::unique_ptr<CameraPriv> camp = std::make_unique<CameraPriv>();

set_hdr(params->hdr);
Expand Down Expand Up @@ -242,7 +248,6 @@ bool camera_create(const parameters_t *params, camera_frame_cb frame_cb, camera_
// this improves performance by a lot.
// https://forums.raspberrypi.com/viewtopic.php?t=352554
// https://github.com/raspberrypi/rpicam-apps/blob/6de1ab6a899df35f929b2a15c0831780bd8e750e/core/rpicam_app.cpp#L1012

int allocator_fd = create_dma_allocator();
if (allocator_fd < 0) {
set_error("failed to open dma heap allocator");
Expand Down Expand Up @@ -288,6 +293,7 @@ bool camera_create(const parameters_t *params, camera_frame_cb frame_cb, camera_

camp->params = params;
camp->frame_cb = frame_cb;
camp->error_cb = error_cb;
*cam = camp.release();

return true;
Expand All @@ -302,11 +308,17 @@ static int buffer_size(const std::vector<FrameBuffer::Plane> &planes) {
}

static void on_request_complete(Request *request) {
if (request->status() == Request::RequestCancelled) {
CameraPriv *camp = (CameraPriv *)request->cookie();

if (camp->in_error) {
return;
}

CameraPriv *camp = (CameraPriv *)request->cookie();
if (request->status() == Request::RequestCancelled) {
camp->in_error = true;
camp->error_cb();
return;
}

FrameBuffer *buffer = request->buffers().at(camp->video_stream);

Expand Down
8 changes: 7 additions & 1 deletion camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@ typedef void (*camera_frame_cb)(
uint64_t size,
uint64_t timestamp);

typedef void (*camera_error_cb)();

#ifdef __cplusplus
extern "C" {
#endif

const char *camera_get_error();
bool camera_create(const parameters_t *params, camera_frame_cb frame_cb, camera_t **cam);
bool camera_create(
const parameters_t *params,
camera_frame_cb frame_cb,
camera_error_cb error_cb,
camera_t **cam);
int camera_get_stride(camera_t *cam);
int camera_get_colorspace(camera_t *cam);
bool camera_start(camera_t *cam);
Expand Down
43 changes: 25 additions & 18 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
#include "text.h"
#include "encoder.h"

static int pipe_video_fd;
static pthread_mutex_t pipe_video_mutex;
static int pipe_out_fd;
static pthread_mutex_t pipe_out_mutex;
static text_t *text;
static encoder_t *enc;

Expand All @@ -40,9 +40,15 @@ static void on_frame(
}

static void on_encoder_output(const uint8_t *mapped, uint64_t size, uint64_t ts) {
pthread_mutex_lock(&pipe_video_mutex);
pipe_write_buf(pipe_video_fd, mapped, size, ts);
pthread_mutex_unlock(&pipe_video_mutex);
pthread_mutex_lock(&pipe_out_mutex);
pipe_write_buf(pipe_out_fd, mapped, size, ts);
pthread_mutex_unlock(&pipe_out_mutex);
}

static void on_error() {
pthread_mutex_lock(&pipe_out_mutex);
pipe_write_error(pipe_out_fd, "camera driver exited");
pthread_mutex_unlock(&pipe_out_mutex);
}

int main() {
Expand All @@ -51,30 +57,31 @@ int main() {
return 0;
}

int pipe_conf_fd = atoi(getenv("PIPE_CONF_FD"));
pipe_video_fd = atoi(getenv("PIPE_VIDEO_FD"));
int pipe_in_fd = atoi(getenv("PIPE_CONF_FD"));
pipe_out_fd = atoi(getenv("PIPE_VIDEO_FD"));

uint8_t *buf;
uint32_t n = pipe_read(pipe_conf_fd, &buf);
uint32_t n = pipe_read(pipe_in_fd, &buf);

parameters_t params;
bool ok = parameters_unserialize(&params, &buf[1], n-1);
free(buf);
if (!ok) {
pipe_write_error(pipe_video_fd, "parameters_unserialize(): %s", parameters_get_error());
pipe_write_error(pipe_out_fd, "parameters_unserialize(): %s", parameters_get_error());
return -1;
}

pthread_mutex_init(&pipe_video_mutex, NULL);
pthread_mutex_lock(&pipe_video_mutex);
pthread_mutex_init(&pipe_out_mutex, NULL);
pthread_mutex_lock(&pipe_out_mutex);

camera_t *cam;
ok = camera_create(
&params,
on_frame,
on_error,
&cam);
if (!ok) {
pipe_write_error(pipe_video_fd, "camera_create(): %s", camera_get_error());
pipe_write_error(pipe_out_fd, "camera_create(): %s", camera_get_error());
return -1;
}

Expand All @@ -83,7 +90,7 @@ int main() {
camera_get_stride(cam),
&text);
if (!ok) {
pipe_write_error(pipe_video_fd, "text_create(): %s", text_get_error());
pipe_write_error(pipe_out_fd, "text_create(): %s", text_get_error());
return -1;
}

Expand All @@ -94,22 +101,22 @@ int main() {
on_encoder_output,
&enc);
if (!ok) {
pipe_write_error(pipe_video_fd, "encoder_create(): %s", encoder_get_error());
pipe_write_error(pipe_out_fd, "encoder_create(): %s", encoder_get_error());
return -1;
}

ok = camera_start(cam);
if (!ok) {
pipe_write_error(pipe_video_fd, "camera_start(): %s", camera_get_error());
pipe_write_error(pipe_out_fd, "camera_start(): %s", camera_get_error());
return -1;
}

pipe_write_ready(pipe_video_fd);
pthread_mutex_unlock(&pipe_video_mutex);
pipe_write_ready(pipe_out_fd);
pthread_mutex_unlock(&pipe_out_mutex);

while (true) {
uint8_t *buf;
uint32_t n = pipe_read(pipe_conf_fd, &buf);
uint32_t n = pipe_read(pipe_in_fd, &buf);

switch (buf[0]) {
case 'e':
Expand Down

0 comments on commit b62a9cb

Please sign in to comment.