Compare commits

...

11 Commits
v5.7 ... v5.13

Author SHA1 Message Date
Maxim Devaev
f11d390b22 Bump version: 5.12 → 5.13 2022-07-05 08:08:20 +03:00
Maxim Devaev
f1e50b6f9b refactoring, using h264 5.1 profile for resolutions > 1920x1080 2022-07-05 07:53:41 +03:00
Maxim Devaev
fdf3340a7d Bump version: 5.11 → 5.12 2022-07-05 00:50:48 +03:00
Maxim Devaev
02513be220 don't assert if m2m encoder is not successfully prepared 2022-07-05 00:48:22 +03:00
Maxim Devaev
d29ce42f08 Bump version: 5.10 → 5.11 2022-06-28 23:12:59 +03:00
Maxim Devaev
aa6fc7fe04 Bump version: 5.9 → 5.10 2022-06-28 23:03:46 +03:00
Maxim Devaev
c91341a375 fixed missing frame_encoding_begin() for noop encoder 2022-06-28 18:51:30 +03:00
Maxim Devaev
3de7e26a36 Bump version: 5.8 → 5.9 2022-06-09 03:37:10 +03:00
Maxim Devaev
63cc66e8a7 improved logging 2022-06-09 03:30:35 +03:00
Maxim Devaev
92a090dec3 Bump version: 5.7 → 5.8 2022-06-07 07:53:00 +03:00
Maxim Devaev
8b0ef8a271 renambed memsink.object to video.sink 2022-06-07 07:48:48 +03:00
12 changed files with 107 additions and 108 deletions

View File

@@ -1,7 +1,7 @@
[bumpversion]
commit = True
tag = True
current_version = 5.7
current_version = 5.13
parse = (?P<major>\d+)\.(?P<minor>\d+)
serialize =
{major}.{minor}

View File

@@ -191,10 +191,10 @@ static void *_pcm_thread(void *v_audio) {
while (!atomic_load(&audio->stop)) {
int frames = snd_pcm_readi(audio->pcm, in, audio->pcm_frames);
if (frames < 0) {
JLOG_PERROR_ALSA(frames, "audio", "Can't capture PCM frames; breaking audio ...");
JLOG_PERROR_ALSA(frames, "audio", "Fatal: Can't capture PCM frames");
break;
} else if (frames < (int)audio->pcm_frames) {
JLOG_ERROR("audio", "Too few PCM frames captured; breaking audio ...");
JLOG_ERROR("audio", "Fatal: Too few PCM frames captured");
break;
}
@@ -238,7 +238,7 @@ static void *_encoder_thread(void *v_audio) {
int size = opus_encode(audio->enc, in_ptr, HZ_TO_FRAMES(ENCODER_INPUT_HZ), out->data, ARRAY_LEN(out->data));
free(in);
if (size < 0) {
JLOG_PERROR_OPUS(size, "audio", "Can't encode PCM frame to OPUS; breaking audio ...");
JLOG_PERROR_OPUS(size, "audio", "Fatal: Can't encode PCM frame to OPUS");
free(out);
break;
}

View File

@@ -116,14 +116,15 @@ typedef struct _client_sx {
} _client_s;
static char *_g_memsink_obj = NULL;
const long double _g_wait_timeout = 1;
const long double _g_lock_timeout = 1;
const useconds_t _g_lock_polling = 1000;
const useconds_t _g_watchers_polling = 100000;
static char *_g_video_sink_name = NULL;
const long double _g_sink_wait_timeout = 1;
const long double _g_sink_lock_timeout = 1;
const useconds_t _g_sink_lock_polling = 1000;
static char *_g_audio_dev = NULL;
static char *_g_tc358743_dev = NULL;
static char *_g_audio_dev_name = NULL;
static char *_g_tc358743_dev_path = NULL;
const useconds_t _g_watchers_polling = 100000;
static _client_s *_g_clients = NULL;
static janus_callbacks *_g_gw = NULL;
@@ -149,10 +150,10 @@ static atomic_bool _g_has_watchers = false;
static int _wait_frame(int fd, memsink_shared_s* mem, uint64_t last_id) {
long double deadline_ts = get_now_monotonic() + _g_wait_timeout;
long double deadline_ts = get_now_monotonic() + _g_sink_wait_timeout;
long double now;
do {
int result = flock_timedwait_monotonic(fd, _g_lock_timeout);
int result = flock_timedwait_monotonic(fd, _g_sink_lock_timeout);
now = get_now_monotonic();
if (result < 0 && errno != EWOULDBLOCK) {
JLOG_PERROR("video", "Can't lock memsink");
@@ -166,7 +167,7 @@ static int _wait_frame(int fd, memsink_shared_s* mem, uint64_t last_id) {
return -1;
}
}
usleep(_g_lock_polling);
usleep(_g_sink_lock_polling);
} while (now < deadline_ts);
return -2;
}
@@ -201,6 +202,11 @@ static void _relay_rtp_clients(const rtp_s *rtp) {
});
}
#define IF_NOT_REPORTED(...) { \
unsigned _error_code = __LINE__; \
if (error_reported != _error_code) { __VA_ARGS__; error_reported = _error_code; } \
}
static void *_clients_video_thread(UNUSED void *arg) {
A_THREAD_RENAME("us_v_clients");
atomic_store(&_g_video_tid_created, true);
@@ -211,13 +217,9 @@ static void *_clients_video_thread(UNUSED void *arg) {
unsigned error_reported = 0;
# define IF_NOT_REPORTED(_error, ...) { \
if (error_reported != _error) { __VA_ARGS__; error_reported = _error; } \
}
while (!STOP) {
if (!HAS_WATCHERS) {
IF_NOT_REPORTED(1, { JLOG_INFO("video", "No active watchers, memsink disconnected"); });
IF_NOT_REPORTED({ JLOG_INFO("video", "No active watchers, memsink disconnected"); });
usleep(_g_watchers_polling);
continue;
}
@@ -225,13 +227,13 @@ static void *_clients_video_thread(UNUSED void *arg) {
int fd = -1;
memsink_shared_s *mem = NULL;
if ((fd = shm_open(_g_memsink_obj, O_RDWR, 0)) <= 0) {
IF_NOT_REPORTED(2, { JLOG_PERROR("video", "Can't open memsink"); });
if ((fd = shm_open(_g_video_sink_name, O_RDWR, 0)) <= 0) {
IF_NOT_REPORTED({ JLOG_PERROR("video", "Can't open memsink"); });
goto close_memsink;
}
if ((mem = memsink_shared_map(fd)) == NULL) {
IF_NOT_REPORTED(3, { JLOG_PERROR("video", "Can't map memsink"); });
IF_NOT_REPORTED({ JLOG_PERROR("video", "Can't map memsink"); });
goto close_memsink;
}
@@ -265,8 +267,6 @@ static void *_clients_video_thread(UNUSED void *arg) {
sleep(1); // error_delay
}
# undef IF_NOT_REPORTED
frame_destroy(frame);
return NULL;
}
@@ -274,8 +274,10 @@ static void *_clients_video_thread(UNUSED void *arg) {
static void *_clients_audio_thread(UNUSED void *arg) {
A_THREAD_RENAME("us_a_clients");
atomic_store(&_g_audio_tid_created, true);
assert(_g_audio_dev);
assert(_g_tc358743_dev);
assert(_g_audio_dev_name);
assert(_g_tc358743_dev_path);
unsigned error_reported = 0;
while (!STOP) {
if (!HAS_WATCHERS) {
@@ -286,17 +288,23 @@ static void *_clients_audio_thread(UNUSED void *arg) {
tc358743_info_s info = {0};
audio_s *audio = NULL;
if (
tc358743_read_info(_g_tc358743_dev, &info) < 0
|| !info.has_audio
|| (audio = audio_init(_g_audio_dev, info.audio_hz)) == NULL
) {
if (tc358743_read_info(_g_tc358743_dev_path, &info) < 0) {
goto close_audio;
}
if (!info.has_audio) {
IF_NOT_REPORTED({ JLOG_INFO("audio", "No audio presented from the host"); });
goto close_audio;
}
IF_NOT_REPORTED({ JLOG_INFO("audio", "Detected host audio"); });
if ((audio = audio_init(_g_audio_dev_name, info.audio_hz)) == NULL) {
goto close_audio;
}
error_reported = 0;
while (!STOP && HAS_WATCHERS) {
if (
tc358743_read_info(_g_tc358743_dev, &info) < 0
tc358743_read_info(_g_tc358743_dev_path, &info) < 0
|| !info.has_audio
|| audio->pcm_hz != info.audio_hz
) {
@@ -325,6 +333,8 @@ static void *_clients_audio_thread(UNUSED void *arg) {
return NULL;
}
#undef IF_NOT_REPORTED
static char *_get_config_value(janus_config *config, const char *section, const char *option) {
janus_config_category *section_obj = janus_config_get_create(config, NULL, janus_config_type_category, section);
janus_config_item *option_obj = janus_config_get(config, section_obj, janus_config_type_item, option);
@@ -335,6 +345,8 @@ static char *_get_config_value(janus_config *config, const char *section, const
}
static int _read_config(const char *config_dir_path) {
int retval = 0;
char *config_file_path;
janus_config *config = NULL;
@@ -348,19 +360,21 @@ static int _read_config(const char *config_dir_path) {
}
janus_config_print(config);
if ((_g_memsink_obj = _get_config_value(config, "memsink", "object")) == NULL) {
JLOG_ERROR("main", "Missing config value: memsink.object");
if (
(_g_video_sink_name = _get_config_value(config, "memsink", "object")) == NULL
&& (_g_video_sink_name = _get_config_value(config, "video", "sink")) == NULL
) {
JLOG_ERROR("main", "Missing config value: video.sink (ex. memsink.object)");
goto error;
}
if ((_g_audio_dev = _get_config_value(config, "audio", "device")) != NULL) {
if ((_g_audio_dev_name = _get_config_value(config, "audio", "device")) != NULL) {
JLOG_INFO("main", "Enabled the experimental AUDIO feature");
if ((_g_tc358743_dev = _get_config_value(config, "audio", "tc358743")) == NULL) {
if ((_g_tc358743_dev_path = _get_config_value(config, "audio", "tc358743")) == NULL) {
JLOG_INFO("main", "Missing config value: audio.tc358743");
goto error;
}
}
int retval = 0;
goto ok;
error:
retval = -1;
@@ -389,7 +403,7 @@ static int _plugin_init(janus_callbacks *gw, const char *config_dir_path) {
}
_g_gw = gw;
_g_rtpv = rtpv_init(_relay_rtp_clients);
if (_g_audio_dev) {
if (_g_audio_dev_name) {
_g_rtpa = rtpa_init(_relay_rtp_clients);
A_THREAD_CREATE(&_g_audio_tid, _clients_audio_thread, NULL);
}
@@ -417,9 +431,9 @@ static void _plugin_destroy(void) {
DEL(rtpa_destroy, _g_rtpa);
DEL(rtpv_destroy, _g_rtpv);
_g_gw = NULL;
DEL(free, _g_tc358743_dev);
DEL(free, _g_audio_dev);
DEL(free, _g_memsink_obj);
DEL(free, _g_tc358743_dev_path);
DEL(free, _g_audio_dev_name);
DEL(free, _g_video_sink_name);
# undef DEL
}

View File

@@ -1,6 +1,6 @@
.\" Manpage for ustreamer-dump.
.\" Open an issue or pull request to https://github.com/pikvm/ustreamer to correct errors or typos
.TH USTREAMER-DUMP 1 "version 5.7" "January 2021"
.TH USTREAMER-DUMP 1 "version 5.13" "January 2021"
.SH NAME
ustreamer-dump \- Dump uStreamer's memory sink to file

View File

@@ -1,6 +1,6 @@
.\" Manpage for ustreamer.
.\" Open an issue or pull request to https://github.com/pikvm/ustreamer to correct errors or typos
.TH USTREAMER 1 "version 5.7" "November 2020"
.TH USTREAMER 1 "version 5.13" "November 2020"
.SH NAME
ustreamer \- stream MJPEG video from any V4L2 device to the network

View File

@@ -3,7 +3,7 @@
pkgname=ustreamer
pkgver=5.7
pkgver=5.13
pkgrel=1
pkgdesc="Lightweight and fast MJPEG-HTTP streamer"
url="https://github.com/pikvm/ustreamer"

View File

@@ -6,7 +6,7 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=ustreamer
PKG_VERSION:=5.7
PKG_VERSION:=5.13
PKG_RELEASE:=1
PKG_MAINTAINER:=Maxim Devaev <mdevaev@gmail.com>

View File

@@ -19,7 +19,7 @@ def _find_sources(suffix: str) -> List[str]:
if __name__ == "__main__":
setup(
name="ustreamer",
version="5.7",
version="5.13",
description="uStreamer tools",
author="Maxim Devaev",
author_email="mdevaev@gmail.com",

View File

@@ -23,7 +23,7 @@
#pragma once
#define VERSION_MAJOR 5
#define VERSION_MINOR 7
#define VERSION_MINOR 13
#define MAKE_VERSION2(_major, _minor) #_major "." #_minor
#define MAKE_VERSION1(_major, _minor) MAKE_VERSION2(_major, _minor)

View File

@@ -224,8 +224,9 @@ static bool _worker_run_job(worker_s *wr) {
} else if (ER(type) == ENCODER_TYPE_NOOP) {
LOG_VERBOSE("Compressing JPEG using NOOP (do nothing): worker=%s, buffer=%u",
wr->name, job->hw->buf.index);
frame_encoding_begin(src, dest, V4L2_PIX_FMT_JPEG);
usleep(5000); // Просто чтобы работала логика desired_fps
dest->encode_end_ts = get_now_monotonic();
dest->encode_end_ts = get_now_monotonic(); // frame_encoding_end()
}
LOG_VERBOSE("Compressed new JPEG: size=%zu, time=%0.3Lf, worker=%s, buffer=%u",

View File

@@ -25,9 +25,9 @@
static m2m_encoder_s *_m2m_encoder_init(
const char *name, const char *path, unsigned output_format,
unsigned fps, bool allow_dma, m2m_option_s *options);
unsigned fps, unsigned bitrate, unsigned gop, unsigned quality, bool allow_dma);
static int _m2m_encoder_prepare(m2m_encoder_s *enc, const frame_s *frame);
static void _m2m_encoder_prepare(m2m_encoder_s *enc, const frame_s *frame);
static int _m2m_encoder_init_buffers(
m2m_encoder_s *enc, const char *name, enum v4l2_buf_type type,
@@ -46,26 +46,11 @@ static int _m2m_encoder_compress_raw(m2m_encoder_s *enc, const frame_s *src, fra
m2m_encoder_s *m2m_h264_encoder_init(const char *name, const char *path, unsigned bitrate, unsigned gop) {
# define OPTION(_key, _value) {#_key, V4L2_CID_MPEG_VIDEO_##_key, _value}
m2m_option_s options[] = {
OPTION(BITRATE, bitrate * 1000),
// OPTION(BITRATE_PEAK, bitrate * 1000),
OPTION(H264_I_PERIOD, gop),
OPTION(H264_PROFILE, V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE),
OPTION(H264_LEVEL, V4L2_MPEG_VIDEO_H264_LEVEL_4_0),
OPTION(REPEAT_SEQ_HEADER, 1),
OPTION(H264_MIN_QP, 16),
OPTION(H264_MAX_QP, 32),
{NULL, 0, 0},
};
# undef OPTION
// FIXME: 30 or 0? https://github.com/6by9/yavta/blob/master/yavta.c#L2100
// По логике вещей правильно 0, но почему-то на низких разрешениях типа 640x480
// енкодер через несколько секунд перестает производить корректные фреймы.
return _m2m_encoder_init(name, path, V4L2_PIX_FMT_H264, 30, true, options);
bitrate *= 1000; // From Kbps
return _m2m_encoder_init(name, path, V4L2_PIX_FMT_H264, 30, bitrate, gop, 0, true);
}
m2m_encoder_s *m2m_mjpeg_encoder_init(const char *name, const char *path, unsigned quality) {
@@ -76,30 +61,18 @@ m2m_encoder_s *m2m_mjpeg_encoder_init(const char *name, const char *path, unsign
bitrate = step * round(bitrate / step);
bitrate *= 1000; // From Kbps
assert(bitrate > 0);
m2m_option_s options[] = {
{"BITRATE", V4L2_CID_MPEG_VIDEO_BITRATE, bitrate},
{NULL, 0, 0},
};
// FIXME: То же самое про 30 or 0, но еще даже не проверено на низких разрешениях
return _m2m_encoder_init(name, path, V4L2_PIX_FMT_MJPEG, 30, true, options);
return _m2m_encoder_init(name, path, V4L2_PIX_FMT_MJPEG, 30, bitrate, 0, 0, true);
}
m2m_encoder_s *m2m_jpeg_encoder_init(const char *name, const char *path, unsigned quality) {
m2m_option_s options[] = {
{"QUALITY", V4L2_CID_JPEG_COMPRESSION_QUALITY, quality},
{NULL, 0, 0},
};
// FIXME: DMA не работает
return _m2m_encoder_init(name, path, V4L2_PIX_FMT_JPEG, 30, false, options);
return _m2m_encoder_init(name, path, V4L2_PIX_FMT_JPEG, 30, 0, 0, quality, false);
}
void m2m_encoder_destroy(m2m_encoder_s *enc) {
E_LOG_INFO("Destroying encoder ...");
_m2m_encoder_cleanup(enc);
free(enc->options);
free(enc->path);
free(enc->name);
free(enc);
@@ -117,9 +90,10 @@ int m2m_encoder_compress(m2m_encoder_s *enc, const frame_s *src, frame_s *dest,
|| RUN(stride) != src->stride
|| RUN(dma) != (enc->allow_dma && src->dma_fd >= 0)
) {
if (_m2m_encoder_prepare(enc, src) < 0) {
return -1;
}
_m2m_encoder_prepare(enc, src);
}
if (!RUN(ready)) { // Already prepared but failed
return -1;
}
force_key = (enc->output_format == V4L2_PIX_FMT_H264 && (force_key || RUN(last_online) != src->online));
@@ -141,7 +115,7 @@ int m2m_encoder_compress(m2m_encoder_s *enc, const frame_s *src, frame_s *dest,
static m2m_encoder_s *_m2m_encoder_init(
const char *name, const char *path, unsigned output_format,
unsigned fps, bool allow_dma, m2m_option_s *options) {
unsigned fps, unsigned bitrate, unsigned gop, unsigned quality, bool allow_dma) {
LOG_INFO("%s: Initializing encoder ...", name);
@@ -160,15 +134,11 @@ static m2m_encoder_s *_m2m_encoder_init(
}
enc->output_format = output_format;
enc->fps = fps;
enc->bitrate = bitrate;
enc->gop = gop;
enc->quality = quality;
enc->allow_dma = allow_dma;
enc->run = run;
unsigned count = 0;
for (; options[count].name != NULL; ++count);
++count;
A_CALLOC(enc->options, count);
memcpy(enc->options, options, sizeof(m2m_option_s) * count);
return enc;
}
@@ -179,7 +149,7 @@ static m2m_encoder_s *_m2m_encoder_init(
} \
}
static int _m2m_encoder_prepare(m2m_encoder_s *enc, const frame_s *frame) {
static void _m2m_encoder_prepare(m2m_encoder_s *enc, const frame_s *frame) {
bool dma = (enc->allow_dma && frame->dma_fd >= 0);
E_LOG_INFO("Configuring encoder: DMA=%d ...", dma);
@@ -198,15 +168,34 @@ static int _m2m_encoder_prepare(m2m_encoder_s *enc, const frame_s *frame) {
}
E_LOG_DEBUG("Encoder device fd=%d opened", RUN(fd));
for (m2m_option_s *option = enc->options; option->name != NULL; ++option) {
struct v4l2_control ctl = {0};
ctl.id = option->id;
ctl.value = option->value;
# define SET_OPTION(_cid, _value) { \
struct v4l2_control _ctl = {0}; \
_ctl.id = _cid; \
_ctl.value = _value; \
E_LOG_DEBUG("Configuring option " #_cid " ..."); \
E_XIOCTL(VIDIOC_S_CTRL, &_ctl, "Can't set option " #_cid); \
}
E_LOG_DEBUG("Configuring option %s ...", option->name);
E_XIOCTL(VIDIOC_S_CTRL, &ctl, "Can't set option %s", option->name);
if (enc->output_format == V4L2_PIX_FMT_H264) {
SET_OPTION(V4L2_CID_MPEG_VIDEO_BITRATE, enc->bitrate);
SET_OPTION(V4L2_CID_MPEG_VIDEO_H264_I_PERIOD, enc->gop);
SET_OPTION(V4L2_CID_MPEG_VIDEO_H264_PROFILE, V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE);
if (RUN(width) * RUN(height) <= 1920 * 1080) { // https://forums.raspberrypi.com/viewtopic.php?t=291447#p1762296
SET_OPTION(V4L2_CID_MPEG_VIDEO_H264_LEVEL, V4L2_MPEG_VIDEO_H264_LEVEL_4_0);
} else {
SET_OPTION(V4L2_CID_MPEG_VIDEO_H264_LEVEL, V4L2_MPEG_VIDEO_H264_LEVEL_5_1);
}
SET_OPTION(V4L2_CID_MPEG_VIDEO_REPEAT_SEQ_HEADER, 1);
SET_OPTION(V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 16);
SET_OPTION(V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 32);
} else if (enc->output_format == V4L2_PIX_FMT_MJPEG) {
SET_OPTION(V4L2_CID_MPEG_VIDEO_BITRATE, enc->bitrate);
} else if (enc->output_format == V4L2_PIX_FMT_JPEG) {
SET_OPTION(V4L2_CID_JPEG_COMPRESSION_QUALITY, enc->quality);
}
# undef SET_OPTION
{
struct v4l2_format fmt = {0};
fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
@@ -273,12 +262,11 @@ static int _m2m_encoder_prepare(m2m_encoder_s *enc, const frame_s *frame) {
RUN(ready) = true;
E_LOG_DEBUG("Encoder state: *** READY ***");
return 0;
return;
error:
_m2m_encoder_cleanup(enc);
E_LOG_ERROR("Encoder destroyed due an error (prepare)");
return -1;
}
static int _m2m_encoder_init_buffers(

View File

@@ -47,12 +47,6 @@ typedef struct {
size_t allocated;
} m2m_buffer_s;
typedef struct {
char *name;
uint32_t id;
int32_t value;
} m2m_option_s;
typedef struct {
int fd;
m2m_buffer_s *input_bufs;
@@ -75,8 +69,10 @@ typedef struct {
char *path;
unsigned output_format;
unsigned fps;
unsigned bitrate;
unsigned gop;
unsigned quality;
bool allow_dma;
m2m_option_s *options;
m2m_encoder_runtime_s *run;
} m2m_encoder_s;