refactoring

This commit is contained in:
Maxim Devaev 2024-02-26 20:28:09 +02:00
parent 260619923a
commit 2d6716aa47
42 changed files with 506 additions and 371 deletions

View File

@ -22,6 +22,22 @@
#include "client.h"
#include <stdlib.h>
#include <stdatomic.h>
#include <string.h>
#include <pthread.h>
#include <janus/plugins/plugin.h>
#include "uslibs/types.h"
#include "uslibs/tools.h"
#include "uslibs/threading.h"
#include "uslibs/list.h"
#include "logging.h"
#include "queue.h"
#include "rtp.h"
static void *_video_thread(void *v_client);
static void *_audio_thread(void *v_client);

View File

@ -22,19 +22,14 @@
#pragma once
#include <stdlib.h>
#include <stdbool.h>
#include <stdatomic.h>
#include <string.h>
#include <pthread.h>
#include <janus/plugins/plugin.h>
#include "uslibs/tools.h"
#include "uslibs/threading.h"
#include "uslibs/types.h"
#include "uslibs/list.h"
#include "logging.h"
#include "queue.h"
#include "rtp.h"

View File

@ -22,6 +22,17 @@
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <janus/config.h>
#include <janus/plugins/plugin.h>
#include "uslibs/tools.h"
#include "const.h"
#include "logging.h"
static char *_get_value(janus_config *jcfg, const char *section, const char *option);
// static bool _get_bool(janus_config *jcfg, const char *section, const char *option, bool def);

View File

@ -22,17 +22,6 @@
#pragma once
#include <stdlib.h>
#include <string.h>
#include <janus/config.h>
#include <janus/plugins/plugin.h>
#include "uslibs/tools.h"
#include "const.h"
#include "logging.h"
typedef struct {
char *video_sink_name;

View File

@ -22,13 +22,24 @@
#include "memsinkfd.h"
#include <unistd.h>
int us_memsink_fd_wait_frame(int fd, us_memsink_shared_s* mem, uint64_t last_id) {
const long double deadline_ts = us_get_now_monotonic() + 1; // wait_timeout
long double now;
#include <linux/videodev2.h>
#include "uslibs/types.h"
#include "uslibs/tools.h"
#include "uslibs/frame.h"
#include "uslibs/memsinksh.h"
#include "logging.h"
int us_memsink_fd_wait_frame(int fd, us_memsink_shared_s* mem, u64 last_id) {
const ldf deadline_ts = us_get_now_monotonic() + 1; // wait_timeout
ldf now_ts;
do {
const int result = us_flock_timedwait_monotonic(fd, 1); // lock_timeout
now = us_get_now_monotonic();
now_ts = us_get_now_monotonic();
if (result < 0 && errno != EWOULDBLOCK) {
US_JLOG_PERROR("video", "Can't lock memsink");
return -1;
@ -42,11 +53,11 @@ int us_memsink_fd_wait_frame(int fd, us_memsink_shared_s* mem, uint64_t last_id)
}
}
usleep(1000); // lock_polling
} while (now < deadline_ts);
} while (now_ts < deadline_ts);
return -2;
}
us_frame_s *us_memsink_fd_get_frame(int fd, us_memsink_shared_s *mem, uint64_t *frame_id, bool key_required) {
us_frame_s *us_memsink_fd_get_frame(int fd, us_memsink_shared_s *mem, u64 *frame_id, bool key_required) {
us_frame_s *frame = us_frame_init();
us_frame_set_data(frame, mem->data, mem->used);
US_FRAME_COPY_META(mem, frame);

View File

@ -22,18 +22,10 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>
#include <unistd.h>
#include <linux/videodev2.h>
#include "uslibs/tools.h"
#include "uslibs/types.h"
#include "uslibs/frame.h"
#include "uslibs/memsinksh.h"
#include "logging.h"
int us_memsink_fd_wait_frame(int fd, us_memsink_shared_s* mem, uint64_t last_id);
us_frame_s *us_memsink_fd_get_frame(int fd, us_memsink_shared_s *mem, uint64_t *frame_id, bool key_required);
int us_memsink_fd_wait_frame(int fd, us_memsink_shared_s* mem, u64 last_id);
us_frame_s *us_memsink_fd_get_frame(int fd, us_memsink_shared_s *mem, u64 *frame_id, bool key_required);

View File

@ -20,8 +20,6 @@
*****************************************************************************/
#include <stdint.h>
#include <stdbool.h>
#include <stdatomic.h>
#include <stdlib.h>
#include <inttypes.h>
@ -37,6 +35,7 @@
#include <janus/plugins/plugin.h>
#include <janus/rtcp.h>
#include "uslibs/types.h"
#include "uslibs/const.h"
#include "uslibs/tools.h"
#include "uslibs/threading.h"
@ -124,7 +123,7 @@ static void *_video_sink_thread(void *arg) {
US_THREAD_RENAME("us_video_sink");
atomic_store(&_g_video_sink_tid_created, true);
uint64_t frame_id = 0;
u64 frame_id = 0;
int once = 0;
while (!_STOP) {
@ -220,9 +219,9 @@ static void *_audio_thread(void *arg) {
goto close_audio;
}
size_t size = US_RTP_DATAGRAM_SIZE - US_RTP_HEADER_SIZE;
uint8_t data[size];
uint64_t pts;
uz size = US_RTP_DATAGRAM_SIZE - US_RTP_HEADER_SIZE;
u8 data[size];
u64 pts;
const int result = us_audio_get_encoded(audio, data, &size, &pts);
if (result == 0) {
_LOCK_AUDIO;

View File

@ -22,8 +22,18 @@
#include "queue.h"
#include <errno.h>
#include <time.h>
#include <assert.h>
us_queue_s *us_queue_init(unsigned capacity) {
#include <pthread.h>
#include "uslibs/types.h"
#include "uslibs/tools.h"
#include "uslibs/threading.h"
us_queue_s *us_queue_init(uint capacity) {
us_queue_s *queue;
US_CALLOC(queue, 1);
US_CALLOC(queue->items, capacity);
@ -61,7 +71,7 @@ void us_queue_destroy(us_queue_s *queue) {
} \
}
int us_queue_put(us_queue_s *queue, void *item, long double timeout) {
int us_queue_put(us_queue_s *queue, void *item, ldf timeout) {
US_MUTEX_LOCK(queue->mutex);
if (timeout == 0) {
if (queue->size == queue->capacity) {
@ -80,7 +90,7 @@ int us_queue_put(us_queue_s *queue, void *item, long double timeout) {
return 0;
}
int us_queue_get(us_queue_s *queue, void **item, long double timeout) {
int us_queue_get(us_queue_s *queue, void **item, ldf timeout) {
US_MUTEX_LOCK(queue->mutex);
_WAIT_OR_UNLOCK(queue->size == 0, queue->empty_cond);
*item = queue->items[queue->out];
@ -96,7 +106,7 @@ int us_queue_get(us_queue_s *queue, void **item, long double timeout) {
int us_queue_get_free(us_queue_s *queue) {
US_MUTEX_LOCK(queue->mutex);
const unsigned size = queue->size;
const uint size = queue->size;
US_MUTEX_UNLOCK(queue->mutex);
return queue->capacity - size;
}

View File

@ -22,24 +22,20 @@
#pragma once
#include <errno.h>
#include <time.h>
#include <assert.h>
#include <pthread.h>
#include "uslibs/types.h"
#include "uslibs/tools.h"
#include "uslibs/threading.h"
// Based on https://github.com/seifzadeh/c-pthread-queue/blob/master/queue.h
typedef struct {
void **items;
unsigned size;
unsigned capacity;
unsigned in;
unsigned out;
uint size;
uint capacity;
uint in;
uint out;
pthread_mutex_t mutex;
pthread_cond_t full_cond;
@ -60,9 +56,9 @@ typedef struct {
}
us_queue_s *us_queue_init(unsigned capacity);
us_queue_s *us_queue_init(uint capacity);
void us_queue_destroy(us_queue_s *queue);
int us_queue_put(us_queue_s *queue, void *item, long double timeout);
int us_queue_get(us_queue_s *queue, void **item, long double timeout);
int us_queue_put(us_queue_s *queue, void *item, ldf timeout);
int us_queue_get(us_queue_s *queue, void **item, ldf timeout);
int us_queue_get_free(us_queue_s *queue);

View File

@ -25,8 +25,13 @@
#include "rtp.h"
#include <stdlib.h>
us_rtp_s *us_rtp_init(unsigned payload, bool video) {
#include "uslibs/types.h"
#include "uslibs/tools.h"
us_rtp_s *us_rtp_init(uint payload, bool video) {
us_rtp_s *rtp;
US_CALLOC(rtp, 1);
rtp->payload = payload;
@ -46,8 +51,8 @@ void us_rtp_destroy(us_rtp_s *rtp) {
free(rtp);
}
void us_rtp_write_header(us_rtp_s *rtp, uint32_t pts, bool marked) {
uint32_t word0 = 0x80000000;
void us_rtp_write_header(us_rtp_s *rtp, u32 pts, bool marked) {
u32 word0 = 0x80000000;
if (marked) {
word0 |= 1 << 23;
}
@ -56,7 +61,7 @@ void us_rtp_write_header(us_rtp_s *rtp, uint32_t pts, bool marked) {
++rtp->seq;
# define WRITE_BE_U32(x_offset, x_value) \
*((uint32_t *)(rtp->datagram + x_offset)) = __builtin_bswap32(x_value)
*((u32 *)(rtp->datagram + x_offset)) = __builtin_bswap32(x_value)
WRITE_BE_U32(0, word0);
WRITE_BE_U32(4, pts);
WRITE_BE_U32(8, rtp->ssrc);

View File

@ -22,13 +22,7 @@
#pragma once
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <sys/types.h>
#include "uslibs/tools.h"
#include "uslibs/types.h"
// https://stackoverflow.com/questions/47635545/why-webrtc-chose-rtp-max-packet-size-to-1200-bytes
@ -37,21 +31,21 @@
typedef struct {
unsigned payload;
bool video;
uint32_t ssrc;
uint payload;
bool video;
u32 ssrc;
uint16_t seq;
uint8_t datagram[US_RTP_DATAGRAM_SIZE];
size_t used;
bool zero_playout_delay;
u16 seq;
u8 datagram[US_RTP_DATAGRAM_SIZE];
uz used;
bool zero_playout_delay;
} us_rtp_s;
typedef void (*us_rtp_callback_f)(const us_rtp_s *rtp);
us_rtp_s *us_rtp_init(unsigned payload, bool video);
us_rtp_s *us_rtp_init(uint payload, bool video);
us_rtp_s *us_rtp_dup(const us_rtp_s *rtp);
void us_rtp_destroy(us_rtp_s *rtp);
void us_rtp_write_header(us_rtp_s *rtp, uint32_t pts, bool marked);
void us_rtp_write_header(us_rtp_s *rtp, u32 pts, bool marked);

View File

@ -22,6 +22,12 @@
#include "rtpa.h"
#include <stdlib.h>
#include <inttypes.h>
#include "uslibs/types.h"
#include "uslibs/tools.h"
us_rtpa_s *us_rtpa_init(us_rtp_callback_f callback) {
us_rtpa_s *rtpa;
@ -37,7 +43,7 @@ void us_rtpa_destroy(us_rtpa_s *rtpa) {
}
char *us_rtpa_make_sdp(us_rtpa_s *rtpa) {
const unsigned pl = rtpa->rtp->payload;
const uint pl = rtpa->rtp->payload;
char *sdp;
US_ASPRINTF(sdp,
"m=audio 1 RTP/SAVPF %u" RN
@ -55,7 +61,7 @@ char *us_rtpa_make_sdp(us_rtpa_s *rtpa) {
return sdp;
}
void us_rtpa_wrap(us_rtpa_s *rtpa, const uint8_t *data, size_t size, uint32_t pts) {
void us_rtpa_wrap(us_rtpa_s *rtpa, const u8 *data, uz size, u32 pts) {
if (size + US_RTP_HEADER_SIZE <= US_RTP_DATAGRAM_SIZE) {
us_rtp_write_header(rtpa->rtp, pts, false);
memcpy(rtpa->rtp->datagram + US_RTP_HEADER_SIZE, data, size);

View File

@ -22,15 +22,7 @@
#pragma once
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <inttypes.h>
#include <sys/types.h>
#include "uslibs/tools.h"
#include "uslibs/threading.h"
#include "uslibs/types.h"
#include "rtp.h"
@ -45,4 +37,4 @@ us_rtpa_s *us_rtpa_init(us_rtp_callback_f callback);
void us_rtpa_destroy(us_rtpa_s *rtpa);
char *us_rtpa_make_sdp(us_rtpa_s *rtpa);
void us_rtpa_wrap(us_rtpa_s *rtpa, const uint8_t *data, size_t size, uint32_t pts);
void us_rtpa_wrap(us_rtpa_s *rtpa, const u8 *data, uz size, u32 pts);

View File

@ -25,10 +25,20 @@
#include "rtpv.h"
#include <stdlib.h>
#include <inttypes.h>
#include <assert.h>
void _rtpv_process_nalu(us_rtpv_s *rtpv, const uint8_t *data, size_t size, uint32_t pts, bool marked);
#include <linux/videodev2.h>
static ssize_t _find_annexb(const uint8_t *data, size_t size);
#include "uslibs/types.h"
#include "uslibs/tools.h"
#include "uslibs/frame.h"
void _rtpv_process_nalu(us_rtpv_s *rtpv, const u8 *data, uz size, u32 pts, bool marked);
static sz _find_annexb(const u8 *data, uz size);
us_rtpv_s *us_rtpv_init(us_rtp_callback_f callback) {
@ -47,7 +57,7 @@ void us_rtpv_destroy(us_rtpv_s *rtpv) {
char *us_rtpv_make_sdp(us_rtpv_s *rtpv) {
// https://tools.ietf.org/html/rfc6184
// https://github.com/meetecho/janus-gateway/issues/2443
const unsigned pl = rtpv->rtp->payload;
const uint pl = rtpv->rtp->payload;
char *sdp;
US_ASPRINTF(sdp,
"m=video 1 RTP/SAVPF %u" RN
@ -78,20 +88,20 @@ void us_rtpv_wrap(us_rtpv_s *rtpv, const us_frame_s *frame, bool zero_playout_de
rtpv->rtp->zero_playout_delay = zero_playout_delay;
const uint32_t pts = us_get_now_monotonic_u64() * 9 / 100; // PTS units are in 90 kHz
ssize_t last_offset = -_PRE;
const u32 pts = us_get_now_monotonic_u64() * 9 / 100; // PTS units are in 90 kHz
sz last_offset = -_PRE;
while (true) { // Find and iterate by nalus
const size_t next_start = last_offset + _PRE;
ssize_t offset = _find_annexb(frame->data + next_start, frame->used - next_start);
const uz next_start = last_offset + _PRE;
sz offset = _find_annexb(frame->data + next_start, frame->used - next_start);
if (offset < 0) {
break;
}
offset += next_start;
if (last_offset >= 0) {
const uint8_t *const data = frame->data + last_offset + _PRE;
size_t size = offset - last_offset - _PRE;
const u8 *const data = frame->data + last_offset + _PRE;
uz size = offset - last_offset - _PRE;
if (data[size - 1] == 0) { // Check for extra 00
--size;
}
@ -102,16 +112,16 @@ void us_rtpv_wrap(us_rtpv_s *rtpv, const us_frame_s *frame, bool zero_playout_de
}
if (last_offset >= 0) {
const uint8_t *const data = frame->data + last_offset + _PRE;
size_t size = frame->used - last_offset - _PRE;
const u8 *const data = frame->data + last_offset + _PRE;
uz size = frame->used - last_offset - _PRE;
_rtpv_process_nalu(rtpv, data, size, pts, true);
}
}
void _rtpv_process_nalu(us_rtpv_s *rtpv, const uint8_t *data, size_t size, uint32_t pts, bool marked) {
const unsigned ref_idc = (data[0] >> 5) & 3;
const unsigned type = data[0] & 0x1F;
uint8_t *dg = rtpv->rtp->datagram;
void _rtpv_process_nalu(us_rtpv_s *rtpv, const u8 *data, uz size, u32 pts, bool marked) {
const uint ref_idc = (data[0] >> 5) & 3;
const uint type = data[0] & 0x1F;
u8 *dg = rtpv->rtp->datagram;
if (size + US_RTP_HEADER_SIZE <= US_RTP_DATAGRAM_SIZE) {
us_rtp_write_header(rtpv->rtp, pts, marked);
@ -121,14 +131,14 @@ void _rtpv_process_nalu(us_rtpv_s *rtpv, const uint8_t *data, size_t size, uint3
return;
}
const size_t fu_overhead = US_RTP_HEADER_SIZE + 2; // FU-A overhead
const uz fu_overhead = US_RTP_HEADER_SIZE + 2; // FU-A overhead
const uint8_t *src = data + 1;
ssize_t remaining = size - 1;
const u8 *src = data + 1;
sz remaining = size - 1;
bool first = true;
while (remaining > 0) {
ssize_t frag_size = US_RTP_DATAGRAM_SIZE - fu_overhead;
sz frag_size = US_RTP_DATAGRAM_SIZE - fu_overhead;
const bool last = (remaining <= frag_size);
if (last) {
frag_size = remaining;
@ -138,7 +148,7 @@ void _rtpv_process_nalu(us_rtpv_s *rtpv, const uint8_t *data, size_t size, uint3
dg[US_RTP_HEADER_SIZE] = 28 | (ref_idc << 5);
uint8_t fu = type;
u8 fu = type;
if (first) {
fu |= 0x80;
}
@ -157,10 +167,10 @@ void _rtpv_process_nalu(us_rtpv_s *rtpv, const uint8_t *data, size_t size, uint3
}
}
static ssize_t _find_annexb(const uint8_t *data, size_t size) {
static sz _find_annexb(const u8 *data, uz size) {
// Parses buffer for 00 00 01 start codes
if (size >= _PRE) {
for (size_t index = 0; index <= size - _PRE; ++index) {
for (uz index = 0; index <= size - _PRE; ++index) {
if (data[index] == 0 && data[index + 1] == 0 && data[index + 2] == 1) {
return index;
}

View File

@ -22,16 +22,7 @@
#pragma once
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <inttypes.h>
#include <assert.h>
#include <sys/types.h>
#include <linux/videodev2.h>
#include "uslibs/tools.h"
#include "uslibs/types.h"
#include "uslibs/frame.h"
#include "rtp.h"

View File

@ -22,6 +22,18 @@
#include "tc358743.h"
#include <unistd.h>
#include <fcntl.h>
#include <linux/videodev2.h>
#include <linux/v4l2-controls.h>
#include "uslibs/types.h"
#include "uslibs/tools.h"
#include "uslibs/xioctl.h"
#include "logging.h"
#ifndef V4L2_CID_USER_TC358743_BASE
# define V4L2_CID_USER_TC358743_BASE (V4L2_CID_USER_BASE + 0x1080)

View File

@ -22,24 +22,12 @@
#pragma once
#include <unistd.h>
#include <fcntl.h>
#include <stdbool.h>
#include <sys/types.h>
#include <linux/videodev2.h>
#include <linux/v4l2-controls.h>
#include "uslibs/tools.h"
#include "uslibs/xioctl.h"
#include "logging.h"
#include "uslibs/types.h"
typedef struct {
bool has_audio;
unsigned audio_hz;
bool has_audio;
uint audio_hz;
} us_tc358743_info_s;

44
janus/src/uslibs/types.h Normal file
View File

@ -0,0 +1,44 @@
/*****************************************************************************
# #
# uStreamer - Lightweight and fast MJPEG-HTTP streamer. #
# #
# Copyright (C) 2018-2023 Maxim Devaev <mdevaev@gmail.com> #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
# #
*****************************************************************************/
#pragma once
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
typedef long long sll;
typedef ssize_t sz;
typedef int8_t s8;
typedef int16_t s16;
typedef int32_t s32;
typedef int64_t s64;
typedef unsigned uint;
typedef unsigned long long ull;
typedef size_t uz;
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef long double ldf;

44
python/src/uslibs/types.h Normal file
View File

@ -0,0 +1,44 @@
/*****************************************************************************
# #
# uStreamer - Lightweight and fast MJPEG-HTTP streamer. #
# #
# Copyright (C) 2018-2023 Maxim Devaev <mdevaev@gmail.com> #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
# #
*****************************************************************************/
#pragma once
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
typedef long long sll;
typedef ssize_t sz;
typedef int8_t s8;
typedef int16_t s16;
typedef int32_t s32;
typedef int64_t s64;
typedef unsigned uint;
typedef unsigned long long ull;
typedef size_t uz;
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef long double ldf;

View File

@ -13,6 +13,7 @@
#include <Python.h>
#include "uslibs/types.h"
#include "uslibs/tools.h"
#include "uslibs/frame.h"
#include "uslibs/memsinksh.h"
@ -29,8 +30,8 @@ typedef struct {
int fd;
us_memsink_shared_s *mem;
uint64_t frame_id;
long double frame_ts;
u64 frame_id;
ldf frame_ts;
us_frame_s *frame;
} _MemsinkObject;
@ -106,7 +107,7 @@ static PyObject *_MemsinkObject_exit(_MemsinkObject *self, PyObject *Py_UNUSED(i
}
static int _wait_frame(_MemsinkObject *self) {
const long double deadline_ts = us_get_now_monotonic() + self->wait_timeout;
const ldf deadline_ts = us_get_now_monotonic() + self->wait_timeout;
# define RETURN_OS_ERROR { \
Py_BLOCK_THREADS \
@ -114,12 +115,12 @@ static int _wait_frame(_MemsinkObject *self) {
return -1; \
}
long double now;
ldf now_ts;
do {
Py_BEGIN_ALLOW_THREADS
const int retval = us_flock_timedwait_monotonic(self->fd, self->lock_timeout);
now = us_get_now_monotonic();
now_ts = us_get_now_monotonic();
if (retval < 0 && errno != EWOULDBLOCK) {
RETURN_OS_ERROR;
@ -130,7 +131,7 @@ static int _wait_frame(_MemsinkObject *self) {
if (self->drop_same_frames > 0) {
if (
US_FRAME_COMPARE_META_USED_NOTS(self->mem, self->frame)
&& (self->frame_ts + self->drop_same_frames > now)
&& (self->frame_ts + self->drop_same_frames > now_ts)
&& !memcmp(self->frame->data, mem->data, mem->used)
) {
self->frame_id = mem->id;
@ -155,7 +156,7 @@ static int _wait_frame(_MemsinkObject *self) {
if (PyErr_CheckSignals() < 0) {
return -1;
}
} while (now < deadline_ts);
} while (now_ts < deadline_ts);
return -2;
# undef RETURN_OS_ERROR

View File

@ -22,6 +22,13 @@
#include "base64.h"
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "types.h"
#include "tools.h"
static const char _ENCODING_TABLE[] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
@ -34,11 +41,11 @@ static const char _ENCODING_TABLE[] = {
'4', '5', '6', '7', '8', '9', '+', '/',
};
static const unsigned _MOD_TABLE[] = {0, 2, 1};
static const uint _MOD_TABLE[] = {0, 2, 1};
void us_base64_encode(const uint8_t *data, size_t size, char **encoded, size_t *allocated) {
const size_t encoded_size = 4 * ((size + 2) / 3) + 1; // +1 for '\0'
void us_base64_encode(const u8 *data, uz size, char **encoded, uz *allocated) {
const uz encoded_size = 4 * ((size + 2) / 3) + 1; // +1 for '\0'
if (*encoded == NULL || (allocated && *allocated < encoded_size)) {
US_REALLOC(*encoded, encoded_size);
@ -47,14 +54,14 @@ void us_base64_encode(const uint8_t *data, size_t size, char **encoded, size_t *
}
}
for (unsigned data_index = 0, encoded_index = 0; data_index < size;) {
# define OCTET(_name) unsigned _name = (data_index < size ? (uint8_t)data[data_index++] : 0)
for (uint data_index = 0, encoded_index = 0; data_index < size;) {
# define OCTET(_name) uint _name = (data_index < size ? (u8)data[data_index++] : 0)
OCTET(octet_a);
OCTET(octet_b);
OCTET(octet_c);
# undef OCTET
const unsigned triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
const uint triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
# define ENCODE(_offset) (*encoded)[encoded_index++] = _ENCODING_TABLE[(triple >> _offset * 6) & 0x3F]
ENCODE(3);
@ -64,7 +71,7 @@ void us_base64_encode(const uint8_t *data, size_t size, char **encoded, size_t *
# undef ENCODE
}
for (unsigned index = 0; index < _MOD_TABLE[size % 3]; index++) {
for (uint index = 0; index < _MOD_TABLE[size % 3]; index++) {
(*encoded)[encoded_size - 2 - index] = '=';
}

View File

@ -22,13 +22,7 @@
#pragma once
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <sys/types.h>
#include "tools.h"
#include "types.h"
void us_base64_encode(const uint8_t *data, size_t size, char **encoded, size_t *allocated);
void us_base64_encode(const u8 *data, uz size, char **encoded, uz *allocated);

View File

@ -22,6 +22,9 @@
#pragma once
#include "types.h"
#define US_VERSION_MAJOR 5
#define US_VERSION_MINOR 51
@ -29,4 +32,4 @@
#define US_MAKE_VERSION1(_major, _minor) US_MAKE_VERSION2(_major, _minor)
#define US_VERSION US_MAKE_VERSION1(US_VERSION_MAJOR, US_VERSION_MINOR)
#define US_VERSION_U ((unsigned)(US_VERSION_MAJOR * 1000 + US_VERSION_MINOR))
#define US_VERSION_U ((uint)(US_VERSION_MAJOR * 1000 + US_VERSION_MINOR))

View File

@ -22,6 +22,31 @@
#include "device.h"
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <assert.h>
#include <sys/select.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <pthread.h>
#include <linux/videodev2.h>
#include <linux/v4l2-controls.h>
#include "types.h"
#include "tools.h"
#include "array.h"
#include "logging.h"
#include "threading.h"
#include "frame.h"
#include "xioctl.h"
static const struct {
const char *name;
@ -35,7 +60,7 @@ static const struct {
static const struct {
const char *name; // cppcheck-suppress unusedStructMember
const unsigned format; // cppcheck-suppress unusedStructMember
const uint format; // cppcheck-suppress unusedStructMember
} _FORMATS[] = {
{"YUYV", V4L2_PIX_FMT_YUYV},
{"YVYU", V4L2_PIX_FMT_YVYU},
@ -57,7 +82,7 @@ static const struct {
static void _v4l2_buffer_copy(const struct v4l2_buffer *src, struct v4l2_buffer *dest);
static bool _device_is_buffer_valid(us_device_s *dev, const struct v4l2_buffer *buf, const uint8_t *data);
static bool _device_is_buffer_valid(us_device_s *dev, const struct v4l2_buffer *buf, const u8 *data);
static int _device_open_check_cap(us_device_s *dev);
static int _device_open_dv_timings(us_device_s *dev);
static int _device_apply_dv_timings(us_device_s *dev);
@ -68,18 +93,18 @@ static int _device_open_io_method(us_device_s *dev);
static int _device_open_io_method_mmap(us_device_s *dev);
static int _device_open_io_method_userptr(us_device_s *dev);
static int _device_open_queue_buffers(us_device_s *dev);
static int _device_apply_resolution(us_device_s *dev, unsigned width, unsigned height, float hz);
static int _device_apply_resolution(us_device_s *dev, uint width, uint height, float hz);
static void _device_apply_controls(us_device_s *dev);
static int _device_query_control(
us_device_s *dev, struct v4l2_queryctrl *query,
const char *name, unsigned cid, bool quiet);
const char *name, uint cid, bool quiet);
static void _device_set_control(
us_device_s *dev, const struct v4l2_queryctrl *query,
const char *name, unsigned cid, int value, bool quiet);
const char *name, uint cid, int value, bool quiet);
static const char *_format_to_string_nullable(unsigned format);
static const char *_format_to_string_supported(unsigned format);
static const char *_format_to_string_nullable(uint format);
static const char *_format_to_string_supported(uint format);
static const char *_standard_to_string(v4l2_std_id standard);
static const char *_io_method_to_string_supported(enum v4l2_memory io_method);
@ -187,7 +212,7 @@ void us_device_close(us_device_s *dev) {
if (run->hw_bufs != NULL) {
_D_LOG_DEBUG("Releasing device buffers ...");
for (unsigned index = 0; index < run->n_bufs; ++index) {
for (uint index = 0; index < run->n_bufs; ++index) {
us_hw_buffer_s *hw = &run->hw_bufs[index];
US_CLOSE_FD(hw->dma_fd, close);
@ -224,7 +249,7 @@ void us_device_close(us_device_s *dev) {
int us_device_export_to_dma(us_device_s *dev) {
us_device_runtime_s *const run = dev->run;
for (unsigned index = 0; index < run->n_bufs; ++index) {
for (uint index = 0; index < run->n_bufs; ++index) {
struct v4l2_exportbuffer exp = {
.type = run->capture_type,
.index = index,
@ -239,7 +264,7 @@ int us_device_export_to_dma(us_device_s *dev) {
return 0;
error:
for (unsigned index = 0; index < run->n_bufs; ++index) {
for (uint index = 0; index < run->n_bufs; ++index) {
US_CLOSE_FD(run->hw_bufs[index].dma_fd, close);
}
return -1;
@ -321,7 +346,7 @@ int us_device_grab_buffer(us_device_s *dev, us_hw_buffer_s **hw) {
}
bool buf_got = false;
unsigned skipped = 0;
uint skipped = 0;
bool broken = false;
_D_LOG_DEBUG("Grabbing device buffer ...");
@ -406,7 +431,7 @@ int us_device_grab_buffer(us_device_s *dev, us_hw_buffer_s **hw) {
(*hw)->raw.stride = run->stride;
(*hw)->raw.online = true;
_v4l2_buffer_copy(&buf, &(*hw)->buf);
(*hw)->raw.grab_ts = (long double)((buf.timestamp.tv_sec * (uint64_t)1000) + (buf.timestamp.tv_usec / 1000)) / 1000;
(*hw)->raw.grab_ts = (ldf)((buf.timestamp.tv_sec * (u64)1000) + (buf.timestamp.tv_usec / 1000)) / 1000;
_D_LOG_DEBUG("Grabbed new frame: buffer=%u, bytesused=%u, grab_ts=%.3Lf, latency=%.3Lf, skipped=%u",
buf.index, buf.bytesused, (*hw)->raw.grab_ts, us_get_now_monotonic() - (*hw)->raw.grab_ts, skipped);
@ -414,7 +439,7 @@ int us_device_grab_buffer(us_device_s *dev, us_hw_buffer_s **hw) {
}
int us_device_release_buffer(us_device_s *dev, us_hw_buffer_s *hw) {
const unsigned index = hw->buf.index;
const uint index = hw->buf.index;
_D_LOG_DEBUG("Releasing device buffer=%u ...", index);
if (us_xioctl(dev->run->fd, VIDIOC_QBUF, &hw->buf) < 0) {
_D_LOG_PERROR("Can't release device buffer=%u", index);
@ -452,7 +477,7 @@ static void _v4l2_buffer_copy(const struct v4l2_buffer *src, struct v4l2_buffer
}
}
bool _device_is_buffer_valid(us_device_s *dev, const struct v4l2_buffer *buf, const uint8_t *data) {
bool _device_is_buffer_valid(us_device_s *dev, const struct v4l2_buffer *buf, const u8 *data) {
// Workaround for broken, corrupted frames:
// Under low light conditions corrupted frames may get captured.
// The good thing is such frames are quite small compared to the regular frames.
@ -479,9 +504,9 @@ bool _device_is_buffer_valid(us_device_s *dev, const struct v4l2_buffer *buf, co
return false;
}
const uint8_t *const end_ptr = data + buf->bytesused;
const uint8_t *const eoi_ptr = end_ptr - 2;
const uint16_t eoi_marker = (((uint16_t)(eoi_ptr[0]) << 8) | eoi_ptr[1]);
const u8 *const end_ptr = data + buf->bytesused;
const u8 *const eoi_ptr = end_ptr - 2;
const u16 eoi_marker = (((u16)(eoi_ptr[0]) << 8) | eoi_ptr[1]);
if (eoi_marker != 0xFFD9 && eoi_marker != 0xD900 && eoi_marker != 0x0000) {
_D_LOG_DEBUG("Discarding truncated JPEG frame: eoi_marker=0x%04x, bytesused=%u", eoi_marker, buf->bytesused);
return false;
@ -569,17 +594,17 @@ static int _device_apply_dv_timings(us_device_s *dev) {
float hz = 0;
if (dv.type == V4L2_DV_BT_656_1120) {
// See v4l2_print_dv_timings() in the kernel
const unsigned htot = V4L2_DV_BT_FRAME_WIDTH(&dv.bt);
const unsigned vtot = V4L2_DV_BT_FRAME_HEIGHT(&dv.bt) / (dv.bt.interlaced ? 2 : 1);
const unsigned fps = ((htot * vtot) > 0 ? ((100 * (uint64_t)dv.bt.pixelclock)) / (htot * vtot) : 0);
const uint htot = V4L2_DV_BT_FRAME_WIDTH(&dv.bt);
const uint vtot = V4L2_DV_BT_FRAME_HEIGHT(&dv.bt) / (dv.bt.interlaced ? 2 : 1);
const uint fps = ((htot * vtot) > 0 ? ((100 * (u64)dv.bt.pixelclock)) / (htot * vtot) : 0);
hz = (fps / 100) + (fps % 100) / 100.0;
_D_LOG_INFO("Got new DV-timings: %ux%u%s%.02f, pixclk=%llu, vsync=%u, hsync=%u",
dv.bt.width, dv.bt.height, (dv.bt.interlaced ? "i" : "p"), hz,
(unsigned long long)dv.bt.pixelclock, dv.bt.vsync, dv.bt.hsync); // See #11 about %llu
(ull)dv.bt.pixelclock, dv.bt.vsync, dv.bt.hsync); // See #11 about %llu
} else {
_D_LOG_INFO("Got new DV-timings: %ux%u, pixclk=%llu, vsync=%u, hsync=%u",
dv.bt.width, dv.bt.height,
(unsigned long long)dv.bt.pixelclock, dv.bt.vsync, dv.bt.hsync);
(ull)dv.bt.pixelclock, dv.bt.vsync, dv.bt.hsync);
}
_D_LOG_DEBUG("Calling us_xioctl(VIDIOC_S_DV_TIMINGS) ...");
@ -608,7 +633,7 @@ static int _device_apply_dv_timings(us_device_s *dev) {
static int _device_open_format(us_device_s *dev, bool first) {
us_device_runtime_s *const run = dev->run;
const unsigned stride = us_align_size(run->width, 32) << 1;
const uint stride = us_align_size(run->width, 32) << 1;
struct v4l2_format fmt = {0};
fmt.type = run->capture_type;
@ -742,7 +767,7 @@ static void _device_open_hw_fps(us_device_s *dev) {
static void _device_open_jpeg_quality(us_device_s *dev) {
us_device_runtime_s *const run = dev->run;
unsigned quality = 0;
uint quality = 0;
if (us_is_jpeg(run->format)) {
struct v4l2_jpegcompression comp = {0};
if (us_xioctl(run->fd, VIDIOC_G_JPEGCOMP, &comp) < 0) {
@ -812,7 +837,7 @@ static int _device_open_io_method_mmap(us_device_s *dev) {
}
us_hw_buffer_s *hw = &run->hw_bufs[run->n_bufs];
const size_t buf_size = (run->capture_mplane ? buf.m.planes[0].length : buf.length);
const uz buf_size = (run->capture_mplane ? buf.m.planes[0].length : buf.length);
const off_t buf_offset = (run->capture_mplane ? buf.m.planes[0].m.mem_offset : buf.m.offset);
_D_LOG_DEBUG("Mapping device buffer=%u ...", run->n_bufs);
@ -861,8 +886,8 @@ static int _device_open_io_method_userptr(us_device_s *dev) {
US_CALLOC(run->hw_bufs, req.count);
const unsigned page_size = getpagesize();
const unsigned buf_size = us_align_size(run->raw_size, page_size);
const uint page_size = getpagesize();
const uint buf_size = us_align_size(run->raw_size, page_size);
for (run->n_bufs = 0; run->n_bufs < req.count; ++run->n_bufs) {
us_hw_buffer_s *hw = &run->hw_bufs[run->n_bufs];
@ -879,7 +904,7 @@ static int _device_open_io_method_userptr(us_device_s *dev) {
static int _device_open_queue_buffers(us_device_s *dev) {
us_device_runtime_s *const run = dev->run;
for (unsigned index = 0; index < run->n_bufs; ++index) {
for (uint index = 0; index < run->n_bufs; ++index) {
struct v4l2_buffer buf = {0};
struct v4l2_plane planes[VIDEO_MAX_PLANES] = {0};
buf.type = run->capture_type;
@ -906,7 +931,7 @@ static int _device_open_queue_buffers(us_device_s *dev) {
return 0;
}
static int _device_apply_resolution(us_device_s *dev, unsigned width, unsigned height, float hz) {
static int _device_apply_resolution(us_device_s *dev, uint width, uint height, float hz) {
// Тут VIDEO_MIN_* не используются из-за странностей минимального разрешения при отсутствии сигнала
// у некоторых устройств, например TC358743
if (
@ -981,7 +1006,7 @@ static void _device_apply_controls(us_device_s *dev) {
static int _device_query_control(
us_device_s *dev, struct v4l2_queryctrl *query,
const char *name, unsigned cid, bool quiet) {
const char *name, uint cid, bool quiet) {
// cppcheck-suppress redundantPointerOp
US_MEMSET_ZERO(*query);
@ -998,7 +1023,7 @@ static int _device_query_control(
static void _device_set_control(
us_device_s *dev, const struct v4l2_queryctrl *query,
const char *name, unsigned cid, int value, bool quiet) {
const char *name, uint cid, int value, bool quiet) {
if (value < query->minimum || value > query->maximum || value % query->step != 0) {
if (!quiet) {
@ -1021,7 +1046,7 @@ static void _device_set_control(
}
}
static const char *_format_to_string_nullable(unsigned format) {
static const char *_format_to_string_nullable(uint format) {
US_ARRAY_ITERATE(_FORMATS, 0, item, {
if (item->format == format) {
return item->name;
@ -1030,7 +1055,7 @@ static const char *_format_to_string_nullable(unsigned format) {
return NULL;
}
static const char *_format_to_string_supported(unsigned format) {
static const char *_format_to_string_supported(uint format) {
const char *const format_str = _format_to_string_nullable(format);
return (format_str == NULL ? "unsupported" : format_str);
}

View File

@ -22,40 +22,19 @@
#pragma once
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <assert.h>
#include <sys/select.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <pthread.h>
#include <linux/videodev2.h>
#include <linux/v4l2-controls.h>
#include "tools.h"
#include "array.h"
#include "logging.h"
#include "threading.h"
#include "types.h"
#include "frame.h"
#include "xioctl.h"
#define US_VIDEO_MIN_WIDTH ((unsigned)160)
#define US_VIDEO_MAX_WIDTH ((unsigned)15360)
#define US_VIDEO_MIN_WIDTH ((uint)160)
#define US_VIDEO_MAX_WIDTH ((uint)15360)
#define US_VIDEO_MIN_HEIGHT ((unsigned)120)
#define US_VIDEO_MAX_HEIGHT ((unsigned)8640)
#define US_VIDEO_MIN_HEIGHT ((uint)120)
#define US_VIDEO_MAX_HEIGHT ((uint)8640)
#define US_VIDEO_MAX_FPS ((unsigned)120)
#define US_VIDEO_MAX_FPS ((uint)120)
#define US_STANDARD_UNKNOWN V4L2_STD_UNKNOWN
#define US_STANDARDS_STR "PAL, NTSC, SECAM"
@ -76,15 +55,15 @@ typedef struct {
typedef struct {
int fd;
unsigned width;
unsigned height;
unsigned format;
unsigned stride;
uint width;
uint height;
uint format;
uint stride;
float hz;
unsigned hw_fps;
unsigned jpeg_quality;
size_t raw_size;
unsigned n_bufs;
uint hw_fps;
uint jpeg_quality;
uz raw_size;
uint n_bufs;
us_hw_buffer_s *hw_bufs;
enum v4l2_buf_type capture_type;
bool capture_mplane;
@ -122,19 +101,19 @@ typedef struct {
typedef struct {
char *path;
unsigned input;
unsigned width;
unsigned height;
unsigned format;
unsigned jpeg_quality;
uint input;
uint width;
uint height;
uint format;
uint jpeg_quality;
v4l2_std_id standard;
enum v4l2_memory io_method;
bool dv_timings;
unsigned n_bufs;
unsigned desired_fps;
size_t min_frame_size;
uint n_bufs;
uint desired_fps;
uz min_frame_size;
bool persistent;
unsigned timeout;
uint timeout;
us_controls_s ctl;
us_device_runtime_s *run;
} us_device_s;

View File

@ -22,6 +22,16 @@
#include "frame.h"
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <linux/videodev2.h>
#include "types.h"
#include "tools.h"
us_frame_s *us_frame_init(void) {
us_frame_s *frame;
@ -36,21 +46,21 @@ void us_frame_destroy(us_frame_s *frame) {
free(frame);
}
void us_frame_realloc_data(us_frame_s *frame, size_t size) {
void us_frame_realloc_data(us_frame_s *frame, uz size) {
if (frame->allocated < size) {
US_REALLOC(frame->data, size);
frame->allocated = size;
}
}
void us_frame_set_data(us_frame_s *frame, const uint8_t *data, size_t size) {
void us_frame_set_data(us_frame_s *frame, const u8 *data, uz size) {
us_frame_realloc_data(frame, size);
memcpy(frame->data, data, size);
frame->used = size;
}
void us_frame_append_data(us_frame_s *frame, const uint8_t *data, size_t size) {
const size_t new_used = frame->used + size;
void us_frame_append_data(us_frame_s *frame, const u8 *data, uz size) {
const uz new_used = frame->used + size;
us_frame_realloc_data(frame, new_used);
memcpy(frame->data + frame->used, data, size);
frame->used = new_used;
@ -69,8 +79,8 @@ bool us_frame_compare(const us_frame_s *a, const us_frame_s *b) {
);
}
unsigned us_frame_get_padding(const us_frame_s *frame) {
unsigned bytes_per_pixel = 0;
uint us_frame_get_padding(const us_frame_s *frame) {
uint bytes_per_pixel = 0;
switch (frame->format) {
case V4L2_PIX_FMT_YUYV:
case V4L2_PIX_FMT_YVYU:
@ -89,13 +99,13 @@ unsigned us_frame_get_padding(const us_frame_s *frame) {
return 0;
}
const char *us_fourcc_to_string(unsigned format, char *buf, size_t size) {
const char *us_fourcc_to_string(uint format, char *buf, uz size) {
assert(size >= 8);
buf[0] = format & 0x7F;
buf[1] = (format >> 8) & 0x7F;
buf[2] = (format >> 16) & 0x7F;
buf[3] = (format >> 24) & 0x7F;
if (format & ((unsigned)1 << 31)) {
if (format & ((uint)1 << 31)) {
buf[4] = '-';
buf[5] = 'B';
buf[6] = 'E';

View File

@ -22,39 +22,33 @@
#pragma once
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <linux/videodev2.h>
#include "types.h"
#include "tools.h"
typedef struct {
uint8_t *data;
size_t used;
size_t allocated;
int dma_fd;
u8 *data;
uz used;
uz allocated;
int dma_fd;
unsigned width;
unsigned height;
unsigned format;
unsigned stride;
uint width;
uint height;
uint format;
uint stride;
// Stride is a bytesperline in V4L2
// https://www.kernel.org/doc/html/v4.14/media/uapi/v4l/pixfmt-v4l2.html
// https://medium.com/@oleg.shipitko/what-does-stride-mean-in-image-processing-bba158a72bcd
bool online;
bool key;
unsigned gop;
bool online;
bool key;
uint gop;
long double grab_ts;
long double encode_begin_ts;
long double encode_end_ts;
ldf grab_ts;
ldf encode_begin_ts;
ldf encode_end_ts;
} us_frame_s;
@ -87,7 +81,7 @@ static inline void us_frame_copy_meta(const us_frame_s *src, us_frame_s *dest) {
)
static inline void us_frame_encoding_begin(const us_frame_s *src, us_frame_s *dest, unsigned format) {
static inline void us_frame_encoding_begin(const us_frame_s *src, us_frame_s *dest, uint format) {
assert(src->used > 0);
us_frame_copy_meta(src, dest);
dest->encode_begin_ts = us_get_now_monotonic();
@ -105,17 +99,17 @@ static inline void us_frame_encoding_end(us_frame_s *dest) {
us_frame_s *us_frame_init(void);
void us_frame_destroy(us_frame_s *frame);
void us_frame_realloc_data(us_frame_s *frame, size_t size);
void us_frame_set_data(us_frame_s *frame, const uint8_t *data, size_t size);
void us_frame_append_data(us_frame_s *frame, const uint8_t *data, size_t size);
void us_frame_realloc_data(us_frame_s *frame, uz size);
void us_frame_set_data(us_frame_s *frame, const u8 *data, uz size);
void us_frame_append_data(us_frame_s *frame, const u8 *data, uz size);
void us_frame_copy(const us_frame_s *src, us_frame_s *dest);
bool us_frame_compare(const us_frame_s *a, const us_frame_s *b);
unsigned us_frame_get_padding(const us_frame_s *frame);
uint us_frame_get_padding(const us_frame_s *frame);
const char *us_fourcc_to_string(unsigned format, char *buf, size_t size);
const char *us_fourcc_to_string(uint format, char *buf, uz size);
static inline bool us_is_jpeg(unsigned format) {
static inline bool us_is_jpeg(uint format) {
return (format == V4L2_PIX_FMT_JPEG || format == V4L2_PIX_FMT_MJPEG);
}

View File

@ -22,6 +22,10 @@
#include "logging.h"
#include <stdbool.h>
#include <pthread.h>
enum us_log_level_t us_g_log_level;

View File

@ -23,7 +23,6 @@
#pragma once
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
@ -33,6 +32,7 @@
#include <pthread.h>
#include "types.h"
#include "tools.h"
#include "threading.h"

View File

@ -22,10 +22,26 @@
#include "memsink.h"
#include <stdatomic.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <assert.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include "types.h"
#include "tools.h"
#include "logging.h"
#include "frame.h"
#include "memsinksh.h"
us_memsink_s *us_memsink_init(
const char *name, const char *obj, bool server,
mode_t mode, bool rm, unsigned client_ttl, unsigned timeout) {
mode_t mode, bool rm, uint client_ttl, uint timeout) {
us_memsink_s *sink;
US_CALLOC(sink, 1);
@ -116,7 +132,7 @@ bool us_memsink_server_check(us_memsink_s *sink, const us_frame_s *frame) {
return (has_clients || !US_FRAME_COMPARE_META_USED_NOTS(sink->mem, frame));;
}
int us_memsink_server_put(us_memsink_s *sink, const us_frame_s *frame, bool *const key_requested) {
int us_memsink_server_put(us_memsink_s *sink, const us_frame_s *frame, bool *key_requested) {
assert(sink->server);
const long double now = us_get_now_monotonic();
@ -163,7 +179,7 @@ int us_memsink_server_put(us_memsink_s *sink, const us_frame_s *frame, bool *con
return 0;
}
int us_memsink_client_get(us_memsink_s *sink, us_frame_s *frame, bool *const key_requested, bool key_required) { // cppcheck-suppress unusedFunction
int us_memsink_client_get(us_memsink_s *sink, us_frame_s *frame, bool *key_requested, bool key_required) { // cppcheck-suppress unusedFunction
assert(!sink->server); // Client only
if (us_flock_timedwait_monotonic(sink->fd, sink->timeout) < 0) {

View File

@ -22,47 +22,37 @@
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include <stdatomic.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include "tools.h"
#include "logging.h"
#include "types.h"
#include "frame.h"
#include "memsinksh.h"
typedef struct {
const char *name;
const char *obj;
bool server;
bool rm;
unsigned client_ttl; // Only for server
unsigned timeout;
const char *name;
const char *obj;
bool server;
bool rm;
uint client_ttl; // Only for server
uint timeout;
int fd;
us_memsink_shared_s *mem;
uint64_t last_id;
u64 last_id;
atomic_bool has_clients; // Only for server
} us_memsink_s;
us_memsink_s *us_memsink_init(
const char *name, const char *obj, bool server,
mode_t mode, bool rm, unsigned client_ttl, unsigned timeout);
mode_t mode, bool rm, uint client_ttl, uint timeout);
void us_memsink_destroy(us_memsink_s *sink);
bool us_memsink_server_check(us_memsink_s *sink, const us_frame_s *frame);
int us_memsink_server_put(us_memsink_s *sink, const us_frame_s *frame, bool *const key_requested);
int us_memsink_server_put(us_memsink_s *sink, const us_frame_s *frame, bool *key_requested);
int us_memsink_client_get(us_memsink_s *sink, us_frame_s *frame, bool *const key_requested, bool key_required);
int us_memsink_client_get(us_memsink_s *sink, us_frame_s *frame, bool *key_requested, bool key_required);

View File

@ -22,45 +22,43 @@
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/mman.h>
#include "types.h"
#define US_MEMSINK_MAGIC ((uint64_t)0xCAFEBABECAFEBABE)
#define US_MEMSINK_VERSION ((uint32_t)4)
#define US_MEMSINK_MAGIC ((u64)0xCAFEBABECAFEBABE)
#define US_MEMSINK_VERSION ((u32)4)
#ifndef US_CFG_MEMSINK_MAX_DATA
# define US_CFG_MEMSINK_MAX_DATA 33554432
#endif
#define US_MEMSINK_MAX_DATA ((size_t)(US_CFG_MEMSINK_MAX_DATA))
#define US_MEMSINK_MAX_DATA ((uz)(US_CFG_MEMSINK_MAX_DATA))
typedef struct {
uint64_t magic;
uint32_t version;
u64 magic;
u32 version;
uint64_t id;
u64 id;
size_t used;
unsigned width;
unsigned height;
unsigned format;
unsigned stride;
bool online;
bool key;
unsigned gop;
uz used;
uint width;
uint height;
uint format;
uint stride;
bool online;
bool key;
uint gop;
long double grab_ts;
long double encode_begin_ts;
long double encode_end_ts;
ldf grab_ts;
ldf encode_begin_ts;
ldf encode_end_ts;
long double last_client_ts;
bool key_requested;
ldf last_client_ts;
bool key_requested;
uint8_t data[US_MEMSINK_MAX_DATA];
u8 data[US_MEMSINK_MAX_DATA];
} us_memsink_shared_s;

View File

@ -22,10 +22,17 @@
#include "options.h"
#include <string.h>
#include <ctype.h>
#include <getopt.h>
#include <assert.h>
void us_build_short_options(const struct option opts[], char *short_opts, size_t size) {
#include "types.h"
void us_build_short_options(const struct option opts[], char *short_opts, uz size) {
memset(short_opts, 0, size);
for (unsigned short_index = 0, opt_index = 0; opts[opt_index].name != NULL; ++opt_index) {
for (uint short_index = 0, opt_index = 0; opts[opt_index].name != NULL; ++opt_index) {
assert(short_index < size - 3);
if (isalpha(opts[opt_index].val)) {
short_opts[short_index] = opts[opt_index].val;

View File

@ -22,12 +22,9 @@
#pragma once
#include <string.h>
#include <ctype.h>
#include <getopt.h>
#include <assert.h>
#include <sys/types.h>
#include "types.h"
void us_build_short_options(const struct option opts[], char *short_opts, size_t size);
void us_build_short_options(const struct option opts[], char *short_opts, uz size);

View File

@ -25,8 +25,6 @@
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#if defined(__linux__)
# define HAS_PDEATHSIG
@ -58,6 +56,7 @@
# include <sys/procctl.h>
# endif
#endif
#include "types.h"
#ifdef WITH_SETPROCTITLE
# include "tools.h"
#endif
@ -102,14 +101,14 @@ INLINE void us_process_set_name_prefix(int argc, char *argv[], const char *prefi
# pragma GCC diagnostic pop
char *cmdline = NULL;
size_t allocated = 2048;
size_t used = 0;
uz allocated = 2048;
uz used = 0;
US_REALLOC(cmdline, allocated);
cmdline[0] = '\0';
for (int index = 0; index < argc; ++index) {
size_t arg_len = strlen(argv[index]);
uz arg_len = strlen(argv[index]);
if (used + arg_len + 16 >= allocated) {
allocated += arg_len + 2048;
US_REALLOC(cmdline, allocated); // cppcheck-suppress memleakOnRealloc // False-positive (ok with assert)

View File

@ -26,7 +26,6 @@
#include <unistd.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <pthread.h>
@ -37,13 +36,14 @@
# endif
#endif
#include "types.h"
#include "tools.h"
#ifdef PTHREAD_MAX_NAMELEN_NP
# define US_THREAD_NAME_SIZE ((size_t)(PTHREAD_MAX_NAMELEN_NP))
# define US_THREAD_NAME_SIZE ((uz)(PTHREAD_MAX_NAMELEN_NP))
#else
# define US_THREAD_NAME_SIZE ((size_t)16)
# define US_THREAD_NAME_SIZE ((uz)16)
#endif
#define US_THREAD_CREATE(x_tid, x_func, x_arg) assert(!pthread_create(&(x_tid), NULL, (x_func), (x_arg)))

View File

@ -23,9 +23,7 @@
#pragma once
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
@ -34,7 +32,6 @@
#include <time.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/file.h>
#if defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 32
@ -43,6 +40,8 @@
# include <signal.h>
#endif
#include "types.h"
#ifdef NDEBUG
# error WTF dude? Asserts are good things!
@ -89,15 +88,15 @@ INLINE const char *us_bool_to_string(bool flag) {
return (flag ? "true" : "false");
}
INLINE size_t us_align_size(size_t size, size_t to) {
INLINE uz us_align_size(uz size, uz to) {
return ((size + (to - 1)) & ~(to - 1));
}
INLINE long long us_floor_ms(long double now) {
return (long long)now - (now < (long long)now); // floor()
INLINE sll us_floor_ms(ldf now) {
return (sll)now - (now < (sll)now); // floor()
}
INLINE uint32_t us_triple_u32(uint32_t x) {
INLINE u32 us_triple_u32(u32 x) {
// https://nullprogram.com/blog/2018/07/31/
x ^= x >> 17;
x *= UINT32_C(0xED5AD4BB);
@ -121,38 +120,38 @@ INLINE void us_get_now(clockid_t clk_id, time_t *sec, long *msec) {
}
}
INLINE long double us_get_now_monotonic(void) {
INLINE ldf us_get_now_monotonic(void) {
time_t sec;
long msec;
us_get_now(CLOCK_MONOTONIC, &sec, &msec);
return (long double)sec + ((long double)msec) / 1000;
return (ldf)sec + ((ldf)msec) / 1000;
}
INLINE uint64_t us_get_now_monotonic_u64(void) {
INLINE u64 us_get_now_monotonic_u64(void) {
struct timespec ts;
assert(!clock_gettime(CLOCK_MONOTONIC, &ts));
return (uint64_t)(ts.tv_nsec / 1000) + (uint64_t)ts.tv_sec * 1000000;
return (u64)(ts.tv_nsec / 1000) + (u64)ts.tv_sec * 1000000;
}
INLINE uint64_t us_get_now_id(void) {
const uint64_t now = us_get_now_monotonic_u64();
return (uint64_t)us_triple_u32(now) | ((uint64_t)us_triple_u32(now + 12345) << 32);
INLINE u64 us_get_now_id(void) {
const u64 now = us_get_now_monotonic_u64();
return (u64)us_triple_u32(now) | ((u64)us_triple_u32(now + 12345) << 32);
}
INLINE long double us_get_now_real(void) {
INLINE ldf us_get_now_real(void) {
time_t sec;
long msec;
us_get_now(CLOCK_REALTIME, &sec, &msec);
return (long double)sec + ((long double)msec) / 1000;
return (ldf)sec + ((ldf)msec) / 1000;
}
INLINE unsigned us_get_cores_available(void) {
INLINE uint us_get_cores_available(void) {
long cores_sysconf = sysconf(_SC_NPROCESSORS_ONLN);
cores_sysconf = (cores_sysconf < 0 ? 0 : cores_sysconf);
return US_MAX(US_MIN(cores_sysconf, 4), 1);
}
INLINE void us_ld_to_timespec(long double ld, struct timespec *ts) {
INLINE void us_ld_to_timespec(ldf ld, struct timespec *ts) {
ts->tv_sec = (long)ld;
ts->tv_nsec = (ld - ts->tv_sec) * 1000000000L;
if (ts->tv_nsec > 999999999L) {
@ -161,12 +160,12 @@ INLINE void us_ld_to_timespec(long double ld, struct timespec *ts) {
}
}
INLINE long double us_timespec_to_ld(const struct timespec *ts) {
return ts->tv_sec + ((long double)ts->tv_nsec) / 1000000000;
INLINE ldf us_timespec_to_ld(const struct timespec *ts) {
return ts->tv_sec + ((ldf)ts->tv_nsec) / 1000000000;
}
INLINE int us_flock_timedwait_monotonic(int fd, long double timeout) {
const long double deadline_ts = us_get_now_monotonic() + timeout;
INLINE int us_flock_timedwait_monotonic(int fd, ldf timeout) {
const ldf deadline_ts = us_get_now_monotonic() + timeout;
int retval = -1;
while (true) {
@ -183,7 +182,7 @@ INLINE int us_flock_timedwait_monotonic(int fd, long double timeout) {
INLINE char *us_errno_to_string(int error) {
char buf[2048];
const size_t max_len = sizeof(buf) - 1;
const uz max_len = sizeof(buf) - 1;
# if (_POSIX_C_SOURCE >= 200112L) && ! _GNU_SOURCE
if (strerror_r(error, buf, max_len) != 0) {
US_SNPRINTF(buf, max_len, "Errno = %d", error);

View File

@ -26,6 +26,7 @@
#include <stdint.h>
typedef long long sll;
typedef ssize_t sz;
typedef int8_t s8;
typedef int16_t s16;
@ -33,8 +34,11 @@ typedef int32_t s32;
typedef int64_t s64;
typedef unsigned uint;
typedef unsigned long long ull;
typedef size_t uz;
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef long double ldf;

View File

@ -22,6 +22,17 @@
#include "unjpeg.h"
#include <stdio.h>
#include <setjmp.h>
#include <assert.h>
#include <jpeglib.h>
#include <linux/videodev2.h>
#include "types.h"
#include "logging.h"
#include "frame.h"
typedef struct {
struct jpeg_error_mgr mgr; // Default manager

View File

@ -22,18 +22,7 @@
#pragma once
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <setjmp.h>
#include <assert.h>
#include <sys/types.h>
#include <jpeglib.h>
#include <linux/videodev2.h>
#include "logging.h"
#include "types.h"
#include "frame.h"

View File

@ -26,11 +26,13 @@
#include <sys/ioctl.h>
#include "types.h"
#ifndef US_CFG_XIOCTL_RETRIES
# define US_CFG_XIOCTL_RETRIES 4
#endif
#define _XIOCTL_RETRIES ((unsigned)(US_CFG_XIOCTL_RETRIES))
#define _XIOCTL_RETRIES ((uint)(US_CFG_XIOCTL_RETRIES))
INLINE int us_xioctl(int fd, int request, void *arg) {

View File

@ -27,6 +27,7 @@
#include <stdbool.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
#include <limits.h>
#include <getopt.h>
#include <errno.h>