Improve clocks support

This commit is contained in:
Kamil Trzcinski
2022-04-06 13:22:49 +02:00
parent 8eb514c6b0
commit 3fe66378aa
8 changed files with 29 additions and 12 deletions

View File

@ -35,7 +35,7 @@ void http_video(http_worker_t *worker, FILE *stream)
buffer_lock_use(&http_h264, 1);
while (!feof(stream)) {
buffer_t *buf = buffer_lock_get(&http_h264, 3, &counter);
buffer_t *buf = buffer_lock_get(&http_h264, 0, &counter);
if (!buf) {
goto error;
}

View File

@ -43,7 +43,7 @@ void http_snapshot(http_worker_t *worker, FILE *stream)
{
int counter = 0;
buffer_lock_use(&http_jpeg, 1);
buffer_t *buf = buffer_lock_get(&http_jpeg, 1, &counter);
buffer_t *buf = buffer_lock_get(&http_jpeg, 0, &counter);
if (!buf) {
http_404_header(worker, stream);
@ -68,7 +68,7 @@ void http_stream(http_worker_t *worker, FILE *stream)
buffer_lock_use(&http_jpeg, 1);
while (!feof(stream)) {
buffer_t *buf = buffer_lock_get(&http_jpeg, 3, &counter);
buffer_t *buf = buffer_lock_get(&http_jpeg, 0, &counter);
if (!buf) {
fprintf(stream, STREAM_ERROR, -1, "No frames.");

View File

@ -49,14 +49,15 @@ void buffer_lock_capture(buffer_lock_t *buf_lock, buffer_t *buf)
pthread_mutex_unlock(&buf_lock->lock);
}
buffer_t *buffer_lock_get(buffer_lock_t *buf_lock, int timeout_s, int *counter)
buffer_t *buffer_lock_get(buffer_lock_t *buf_lock, int timeout_ms, int *counter)
{
buffer_t *buf = NULL;
struct timeval now;
struct timespec timeout;
gettimeofday(&now, NULL);
timeout.tv_nsec = now.tv_usec;
timeout.tv_sec = now.tv_sec + timeout_s;
if(!timeout_ms)
timeout_ms = DEFAULT_BUFFER_LOCK_GET_TIMEOUT;
get_time_us(CLOCK_REALTIME, &timeout, NULL, timeout_ms * 1000LL * 1000LL);
pthread_mutex_lock(&buf_lock->lock);
if (*counter == buf_lock->counter || !buf_lock->buf) {

View File

@ -16,6 +16,7 @@ typedef struct buffer_lock_s {
} buffer_lock_t;
#define DEFAULT_BUFFER_LOCK_TIMEOUT 16 // ~60fps
#define DEFAULT_BUFFER_LOCK_GET_TIMEOUT 2000 // 2s
#define DEFINE_BUFFER_LOCK(_name, _timeout_ms) static buffer_lock_t _name = { \
.name = #_name, \
@ -25,7 +26,7 @@ typedef struct buffer_lock_s {
};
void buffer_lock_capture(buffer_lock_t *buf_lock, buffer_t *buf);
buffer_t *buffer_lock_get(buffer_lock_t *buf_lock, int timeout_s, int *counter);
buffer_t *buffer_lock_get(buffer_lock_t *buf_lock, int timeout_ms, int *counter);
bool buffer_lock_needs_buffer(buffer_lock_t *buf_lock);
void buffer_lock_use(buffer_lock_t *buf_lock, int ref);
bool buffer_lock_is_used(buffer_lock_t *buf_lock);

View File

@ -220,7 +220,7 @@ int links_loop(link_t *all_links, bool *running)
}
while(*running) {
if (links_step(all_links, 1000) < 0) {
if (links_step(all_links, LINKS_LOOP_INTERVAL) < 0) {
links_stream(all_links, false);
return -1;
}

View File

@ -2,6 +2,8 @@
#include "v4l2.h"
#define LINKS_LOOP_INTERVAL 100
typedef struct buffer_s buffer_t;
typedef struct buffer_list_s buffer_list_t;

View File

@ -80,10 +80,17 @@ int shrink_to_block(int size, int block)
return size / block * block;
}
uint64_t get_monotonic_time_us(struct timespec *ts, struct timeval *tv)
uint64_t get_time_us(clockid_t clock, struct timespec *ts, struct timeval *tv, int64_t delays_us)
{
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
clock_gettime(clock, &now);
if (delays_us > 0) {
now.tv_nsec += delays_us * 1000LL;
now.tv_sec += now.tv_nsec / (1000LL * 1000LL * 1000LL);
now.tv_nsec %= 1000LL * 1000LL * 1000LL;
}
if (ts) {
*ts = now;
}
@ -93,3 +100,8 @@ uint64_t get_monotonic_time_us(struct timespec *ts, struct timeval *tv)
}
return now.tv_sec * 1000000LL + now.tv_nsec / 1000;
}
uint64_t get_monotonic_time_us(struct timespec *ts, struct timeval *tv)
{
return get_time_us(CLOCK_MONOTONIC, ts, tv, 0);
}

View File

@ -34,6 +34,7 @@ fourcc_string fourcc_to_string(unsigned format);
unsigned fourcc_to_stride(unsigned width, unsigned format);
int xioctl(const char *name, int fd, int request, void *arg);
uint64_t get_monotonic_time_us(struct timespec *ts, struct timeval *tv);
uint64_t get_time_us(clockid_t clock, struct timespec *ts, struct timeval *tv, int64_t delays_us);
int shrink_to_block(int size, int block);
#define E_XIOCTL(dev, _fd, _request, _value, _msg, ...) do { \