Fix buffer leaking on connection disconnect

This commit is contained in:
Kamil Trzcinski 2022-04-06 14:27:35 +02:00
parent aad601d575
commit 899204916f
5 changed files with 13 additions and 5 deletions

View File

@ -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) "$@"

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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;

View File

@ -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 ) "$@"