From 899204916f87734a9a41b083ed851acea8fa5b2b Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Wed, 6 Apr 2022 14:27:35 +0200 Subject: [PATCH] Fix buffer leaking on connection disconnect --- csi_camera.sh | 2 +- http/http_h264.c | 5 ++++- http/http_jpeg.c | 5 ++++- hw/links.c | 4 +++- usb_camera.sh | 2 +- 5 files changed, 13 insertions(+), 5 deletions(-) diff --git a/csi_camera.sh b/csi_camera.sh index f674d81..df0d12b 100755 --- a/csi_camera.sh +++ b/csi_camera.sh @@ -5,4 +5,4 @@ cd "$SCRIPT_DIR" set -xeo pipefail make -./camera_stream -camera-path=$(echo /dev/v4l/by-path/*.csi-video-index0) "$@" +$GDB ./camera_stream -camera-path=$(echo /dev/v4l/by-path/*.csi-video-index0) "$@" diff --git a/http/http_h264.c b/http/http_h264.c index 4dae763..074115f 100644 --- a/http/http_h264.c +++ b/http/http_h264.c @@ -30,12 +30,13 @@ void http_video(http_worker_t *worker, FILE *stream) bool had_key_frame = false; bool requested_key_frame = false; int counter = 0; + buffer_t *buf = NULL; fprintf(stream, VIDEO_HEADER); buffer_lock_use(&http_h264, 1); while (!feof(stream)) { - buffer_t *buf = buffer_lock_get(&http_h264, 0, &counter); + buf = buffer_lock_get(&http_h264, 0, &counter); if (!buf) { goto error; } @@ -59,8 +60,10 @@ void http_video(http_worker_t *worker, FILE *stream) requested_key_frame = true; } buffer_consumed(buf, "h264-stream"); + buf = NULL; } error: + buffer_consumed(buf, "error"); buffer_lock_use(&http_h264, -1); } diff --git a/http/http_jpeg.c b/http/http_jpeg.c index d3daa09..4d75ae5 100644 --- a/http/http_jpeg.c +++ b/http/http_jpeg.c @@ -64,11 +64,12 @@ error: void http_stream(http_worker_t *worker, FILE *stream) { int counter = 0; + buffer_t *buf = NULL; fprintf(stream, STREAM_HEADER); buffer_lock_use(&http_jpeg, 1); while (!feof(stream)) { - buffer_t *buf = buffer_lock_get(&http_jpeg, 0, &counter); + buf = buffer_lock_get(&http_jpeg, 0, &counter); if (!buf) { fprintf(stream, STREAM_ERROR, -1, "No frames."); @@ -86,8 +87,10 @@ void http_stream(http_worker_t *worker, FILE *stream) } buffer_consumed(buf, "jpeg-stream"); + buf = NULL; } error: + buffer_consumed(buf, "error"); buffer_lock_use(&http_jpeg, -1); } diff --git a/hw/links.c b/hw/links.c index 0f76d53..cb5cdde 100644 --- a/hw/links.c +++ b/hw/links.c @@ -65,9 +65,11 @@ int _build_fds(link_t *all_links, struct pollfd *fds, link_t **links, buffer_lis source->device->output_device->paused = paused; } + int count_enqueued = buffer_list_count_enqueued(source); + fds[n].fd = source->device->fd; fds[n].events = POLLHUP; - if (!source->device->paused) + if (!source->device->paused && count_enqueued > 0) fds[n].events |= POLLIN; fds[n].revents = 0; buf_lists[n] = source; diff --git a/usb_camera.sh b/usb_camera.sh index 6915826..ff90b48 100755 --- a/usb_camera.sh +++ b/usb_camera.sh @@ -5,4 +5,4 @@ cd "$SCRIPT_DIR" set -xeo pipefail make -./camera_stream -camera-path=$(echo /dev/v4l/by-id/usb-*-video-index0 ) "$@" +$GDB ./camera_stream -camera-path=$(echo /dev/v4l/by-id/usb-*-video-index0 ) "$@"