Ensure that /snapshot is always "up-to date"

This commit is contained in:
Kamil Trzcinski 2023-04-13 11:09:20 +02:00
parent cdb62efd93
commit 6e89ac9663
5 changed files with 52 additions and 8 deletions

View File

@ -16,6 +16,9 @@
<ul> <ul>
<li>Get a high-resolution snapshot image from the server.</li> <li>Get a high-resolution snapshot image from the server.</li>
<li>Uses resolution specified by <i>-camera-snapshot.height=</i>.</li> <li>Uses resolution specified by <i>-camera-snapshot.height=</i>.</li>
<br>
<li><a href="snapshot?max_delay=0">/snapshot?max_delay=0</a> to get a snapshot captured exactly now.</li>
<li><a href="snapshot?max_delay=300">/snapshot?max_delay=300</a> (default) to get a cached snapshot captured up-to 300 ms in the past.</li>
</ul> </ul>
</li> </li>
<br> <br>

View File

@ -3,9 +3,13 @@
#include "output.h" #include "output.h"
#include "util/http/http.h" #include "util/http/http.h"
#include "util/opts/log.h"
#include "device/buffer.h" #include "device/buffer.h"
#include "device/buffer_lock.h" #include "device/buffer_lock.h"
#define SNAPSHOT_TIMEOUT_MS 3000
#define SNAPSHOT_DEFAULT_DELAY_PARAM "300"
#define PART_BOUNDARY "123456789000000000000987654321" #define PART_BOUNDARY "123456789000000000000987654321"
#define CONTENT_TYPE "image/jpeg" #define CONTENT_TYPE "image/jpeg"
#define CONTENT_LENGTH "Content-Length" #define CONTENT_LENGTH "Content-Length"
@ -20,19 +24,42 @@ static const char *const STREAM_PART = "Content-Type: " CONTENT_TYPE "\r\n" CONT
static const char *const STREAM_BOUNDARY = "\r\n" static const char *const STREAM_BOUNDARY = "\r\n"
"--" PART_BOUNDARY "\r\n"; "--" PART_BOUNDARY "\r\n";
int http_snapshot_buf_part(buffer_lock_t *buf_lock, buffer_t *buf, int frame, FILE *stream) typedef struct
{ {
fprintf(stream, "HTTP/1.1 200 OK\r\n"); FILE *stream;
fprintf(stream, "Content-Type: image/jpeg\r\n"); uint64_t start_time_us;
fprintf(stream, "Content-Length: %zu\r\n", buf->used); } http_snapshot_t;
fprintf(stream, "\r\n");
fwrite(buf->start, buf->used, 1, stream); int http_snapshot_buf_part(buffer_lock_t *buf_lock, buffer_t *buf, int frame, http_snapshot_t *snapshot)
{
// Ignore frames that are captured
if (buf->captured_time_us < snapshot->start_time_us) {
return 0;
}
fprintf(snapshot->stream, "HTTP/1.1 200 OK\r\n");
fprintf(snapshot->stream, "Content-Type: image/jpeg\r\n");
fprintf(snapshot->stream, "Content-Length: %zu\r\n", buf->used);
fprintf(snapshot->stream, "\r\n");
fwrite(buf->start, buf->used, 1, snapshot->stream);
return 1; return 1;
} }
void http_snapshot(http_worker_t *worker, FILE *stream) void http_snapshot(http_worker_t *worker, FILE *stream)
{ {
int n = buffer_lock_write_loop(&snapshot_lock, 1, 0, (buffer_write_fn)http_snapshot_buf_part, stream); // passing the max_delay=0 will ensure that frame is capture at this exact moment
const char *max_delay = http_get_param(worker, "max_delay");
if (!max_delay) {
max_delay = SNAPSHOT_DEFAULT_DELAY_PARAM;
}
http_snapshot_t snapshot = {
.stream = stream,
.start_time_us = get_monotonic_time_us(NULL, NULL) - atoi(max_delay) * 1000
};
int n = buffer_lock_write_loop(&snapshot_lock, 1, SNAPSHOT_TIMEOUT_MS,
(buffer_write_fn)http_snapshot_buf_part, &snapshot);
if (n <= 0) { if (n <= 0) {
http_500(stream, NULL); http_500(stream, NULL);

View File

@ -1,5 +1,5 @@
#include "device/buffer_lock.h" #include "device/buffer_lock.h"
DEFINE_BUFFER_LOCK(snapshot_lock, 1000); DEFINE_BUFFER_LOCK(snapshot_lock, 0);
DEFINE_BUFFER_LOCK(stream_lock, 0); DEFINE_BUFFER_LOCK(stream_lock, 0);
DEFINE_BUFFER_LOCK(video_lock, 0); DEFINE_BUFFER_LOCK(video_lock, 0);

View File

@ -84,6 +84,19 @@ void *http_enum_params(http_worker_t *worker, FILE *stream, http_param_fn fn, vo
return ret; return ret;
} }
static void *http_get_param_fn(struct http_worker_s *worker, FILE *stream, const char *key, const char *value, void *opaque)
{
if (!strcmp(key, opaque))
return (void*)value;
return NULL;
}
const char *http_get_param(http_worker_t *worker, const char *key)
{
return http_enum_params(worker, NULL, http_get_param_fn, (void*)key);
}
static void http_process(http_worker_t *worker, FILE *stream) static void http_process(http_worker_t *worker, FILE *stream)
{ {
// Read headers // Read headers

View File

@ -62,3 +62,4 @@ void http_400(FILE *stream, const char *data);
void http_404(FILE *stream, const char *data); void http_404(FILE *stream, const char *data);
void http_500(FILE *stream, const char *data); void http_500(FILE *stream, const char *data);
void *http_enum_params(http_worker_t *worker, FILE *stream, http_param_fn fn, void *opaque); void *http_enum_params(http_worker_t *worker, FILE *stream, http_param_fn fn, void *opaque);
const char *http_get_param(http_worker_t *worker, const char *key);