diff --git a/cmd/camera-streamer/status.cc b/cmd/camera-streamer/status.cc index a3dbc98..23076d5 100644 --- a/cmd/camera-streamer/status.cc +++ b/cmd/camera-streamer/status.cc @@ -85,7 +85,7 @@ static nlohmann::json links_status_json() nlohmann::json link_json; link_json["source"] = link->capture_list->name; - for (int j = 0; link->output_lists[j]; j++) { + for (int j = 0; j < link->n_output_lists; j++) { link_json["sinks"][j] = link->output_lists[j]->name; } for (int j = 0; j < link->n_callbacks; j++) { diff --git a/device/camera/camera.c b/device/camera/camera.c index 21074e5..805cab2 100644 --- a/device/camera/camera.c +++ b/device/camera/camera.c @@ -82,16 +82,14 @@ link_t *camera_ensure_capture(camera_t *camera, buffer_list_t *capture) void camera_capture_add_output(camera_t *camera, buffer_list_t *capture, buffer_list_t *output) { link_t *link = camera_ensure_capture(camera, capture); - - int nsinks; - for (nsinks = 0; link->output_lists[nsinks]; nsinks++); - link->output_lists[nsinks] = output; + ARRAY_APPEND(link->output_lists, link->n_output_lists, output); } void camera_capture_add_callbacks(camera_t *camera, buffer_list_t *capture, link_callbacks_t callbacks) { link_t *link = camera_ensure_capture(camera, capture); - link->callbacks[link->n_callbacks++] = callbacks; + if (!ARRAY_APPEND(link->callbacks, link->n_callbacks, callbacks)) + return; if (callbacks.buf_lock) { callbacks.buf_lock->buf_list = capture; diff --git a/device/links.c b/device/links.c index 0cd1f93..563870f 100644 --- a/device/links.c +++ b/device/links.c @@ -63,7 +63,7 @@ static int links_build_fds(link_t *all_links, link_pool_t *link_pool) paused = false; } - for (int j = 0; link->output_lists[j]; j++) { + for (int j = 0; j < link->n_output_lists; j++) { buffer_list_t *sink = link->output_lists[j]; if (n >= N_FDS) { @@ -150,7 +150,7 @@ static int links_enqueue_from_capture_list(buffer_list_t *capture_list, link_t * bool dropped = false; - for (int j = 0; link->output_lists[j]; j++) { + for (int j = 0; j < link->n_output_lists; j++) { if (link->output_lists[j]->dev->paused) { continue; } @@ -314,7 +314,7 @@ static int links_stream(link_t *all_links, bool do_stream) LOG_ERROR(link->capture_list, "Failed to start streaming"); } - for (int j = 0; link->output_lists[j]; j++) { + for (int j = 0; j < link->n_output_lists; j++) { if (buffer_list_set_stream(link->output_lists[j], streaming) < 0) { LOG_ERROR(link->output_lists[j], "Failed to start streaming"); } @@ -412,7 +412,7 @@ void links_dump(link_t *all_links) line[0] = 0; links_dump_buf_list(line, link->capture_list); strcat(line, " => ["); - for (int j = 0; link->output_lists[j]; j++) { + for (int j = 0; j < link->n_output_lists; j++) { if (j > 0) strcat(line, ", "); links_dump_buf_list(line, link->output_lists[j]); diff --git a/device/links.h b/device/links.h index 532e64b..bfa66c0 100644 --- a/device/links.h +++ b/device/links.h @@ -4,6 +4,8 @@ #include #define LINKS_LOOP_INTERVAL 100 +#define MAX_OUTPUT_LISTS 10 +#define MAX_CALLBACKS 10 typedef struct buffer_s buffer_t; typedef struct buffer_list_s buffer_list_t; @@ -22,8 +24,9 @@ typedef struct link_callbacks_s { typedef struct link_s { buffer_list_t *capture_list; - buffer_list_t *output_lists[10]; - link_callbacks_t callbacks[10]; + buffer_list_t *output_lists[MAX_OUTPUT_LISTS]; + int n_output_lists; + link_callbacks_t callbacks[MAX_CALLBACKS]; int n_callbacks; } link_t; diff --git a/util/opts/log.h b/util/opts/log.h index acc8988..75d75c5 100644 --- a/util/opts/log.h +++ b/util/opts/log.h @@ -60,3 +60,8 @@ int ioctl_retried(const char *name, int fd, int request, void *arg); LOG_ERROR(dev, "ioctl(ret=%d, errno=%d): " _msg, ret, errno, ##__VA_ARGS__); \ } \ } while(0) + +#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) +#define ARRAY_APPEND(arr, n_arr, item) ((n_arr) < ARRAY_SIZE(arr) ? ((arr[n_arr++] = item), true) : false) +#define ARRAY_FOREACH(type, key, arr, n_arr) \ + for (type *key = &arr[0]; key < &arr[n_arr]; key++)