From fd65ed006a8a669d4876c4a6a5cf048cbbfb9c25 Mon Sep 17 00:00:00 2001 From: Maxim Devaev Date: Fri, 19 Nov 2021 18:06:49 +0300 Subject: [PATCH] simplified m2m usage --- src/ustreamer/h264/stream.c | 23 +++-------------------- src/ustreamer/m2m.c | 33 ++++++++++++++++++--------------- src/ustreamer/m2m.h | 3 +-- 3 files changed, 22 insertions(+), 37 deletions(-) diff --git a/src/ustreamer/h264/stream.c b/src/ustreamer/h264/stream.c index 7513785..03930b7 100644 --- a/src/ustreamer/h264/stream.c +++ b/src/ustreamer/h264/stream.c @@ -67,38 +67,21 @@ void h264_stream_process(h264_stream_s *h264, const frame_s *frame, bool force_k return; } - long double now = get_now_monotonic(); - bool dma = false; - if (is_jpeg(frame->format)) { - assert(frame->dma_fd <= 0); + long double now = get_now_monotonic(); LOG_DEBUG("H264: Input frame is JPEG; decoding ..."); if (unjpeg(frame, h264->tmp_src, true) < 0) { return; } frame = h264->tmp_src; LOG_VERBOSE("H264: JPEG decoded; time=%.3Lf", get_now_monotonic() - now); - } else if (frame->dma_fd > 0) { - LOG_DEBUG("H264: DMA available for the input"); - dma = true; - } else { - LOG_DEBUG("H264: Copying source to tmp buffer ..."); - frame_copy(frame, h264->tmp_src); - frame = h264->tmp_src; - LOG_VERBOSE("H264: Source copied; time=%.3Lf", get_now_monotonic() - now); } bool online = false; - - if (!m2m_encoder_is_prepared_for(h264->enc, frame, dma)) { - m2m_encoder_prepare(h264->enc, frame, dma); - } - - if (h264->enc->ready) { - if (m2m_encoder_compress(h264->enc, frame, h264->dest, force_key) == 0) { + if (!m2m_encoder_ensure_ready(h264->enc, frame)) { + if (!m2m_encoder_compress(h264->enc, frame, h264->dest, force_key)) { online = !memsink_server_put(h264->sink, h264->dest); } } - atomic_store(&h264->online, online); } diff --git a/src/ustreamer/m2m.c b/src/ustreamer/m2m.c index d6531c4..9c4e892 100644 --- a/src/ustreamer/m2m.c +++ b/src/ustreamer/m2m.c @@ -23,15 +23,16 @@ #include "m2m.h" +static bool _m2m_encoder_is_prepared_for(m2m_encoder_s *enc, const frame_s *frame); +static int _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, m2m_buffer_s **bufs_ptr, unsigned *n_bufs_ptr, bool dma); static void _m2m_encoder_cleanup(m2m_encoder_s *enc); -static int _m2m_encoder_compress_raw( - m2m_encoder_s *enc, const frame_s *src, - frame_s *dest, bool force_key); +static int _m2m_encoder_compress_raw(m2m_encoder_s *enc, const frame_s *src, frame_s *dest, bool force_key); #define E_LOG_ERROR(_msg, ...) LOG_ERROR("%s: " _msg, enc->name, ##__VA_ARGS__) @@ -70,9 +71,16 @@ void m2m_encoder_destroy(m2m_encoder_s *enc) { free(enc); } -bool m2m_encoder_is_prepared_for(m2m_encoder_s *enc, const frame_s *frame, bool dma) { +int m2m_encoder_ensure_ready(m2m_encoder_s *enc, const frame_s *frame) { + if (!_m2m_encoder_is_prepared_for(enc, frame)) { + return _m2m_encoder_prepare(enc, frame); + } + return 0; +} + +static bool _m2m_encoder_is_prepared_for(m2m_encoder_s *enc, const frame_s *frame) { # define EQ(_field) (enc->_field == frame->_field) - return (EQ(width) && EQ(height) && EQ(format) && EQ(stride) && (enc->dma == dma)); + return (EQ(width) && EQ(height) && EQ(format) && EQ(stride) && (enc->dma == (frame->dma_fd >= 0))); # undef EQ } @@ -83,7 +91,9 @@ bool m2m_encoder_is_prepared_for(m2m_encoder_s *enc, const frame_s *frame, bool } \ } -int m2m_encoder_prepare(m2m_encoder_s *enc, const frame_s *frame, bool dma) { +static int _m2m_encoder_prepare(m2m_encoder_s *enc, const frame_s *frame) { + bool dma = (frame->dma_fd >= 0); + E_LOG_INFO("Configuring encoder: DMA=%d ...", dma); _m2m_encoder_cleanup(enc); @@ -299,11 +309,7 @@ int m2m_encoder_compress(m2m_encoder_s *enc, const frame_s *src, frame_s *dest, assert(enc->height == src->height); assert(enc->format == src->format); assert(enc->stride == src->stride); - if (enc->dma) { - assert(src->dma_fd >= 0); - } else { - assert(src->dma_fd < 0); - } + assert(enc->dma == (src->dma_fd >= 0)); frame_copy_meta(src, dest); dest->encode_begin_ts = get_now_monotonic(); @@ -326,10 +332,7 @@ int m2m_encoder_compress(m2m_encoder_s *enc, const frame_s *src, frame_s *dest, return 0; } -static int _m2m_encoder_compress_raw( - m2m_encoder_s *enc, const frame_s *src, - frame_s *dest, bool force_key) { - +static int _m2m_encoder_compress_raw(m2m_encoder_s *enc, const frame_s *src, frame_s *dest, bool force_key) { E_LOG_DEBUG("Compressing new frame; force_key=%d ...", force_key); if (force_key) { diff --git a/src/ustreamer/m2m.h b/src/ustreamer/m2m.h index 76d6c56..80a7584 100644 --- a/src/ustreamer/m2m.h +++ b/src/ustreamer/m2m.h @@ -81,6 +81,5 @@ typedef struct { m2m_encoder_s *m2m_encoder_init(const char *name, const char *path, unsigned format, unsigned fps, m2m_option_s *options); void m2m_encoder_destroy(m2m_encoder_s *enc); -bool m2m_encoder_is_prepared_for(m2m_encoder_s *enc, const frame_s *frame, bool dma); -int m2m_encoder_prepare(m2m_encoder_s *enc, const frame_s *frame, bool dma); +int m2m_encoder_ensure_ready(m2m_encoder_s *enc, const frame_s *frame); int m2m_encoder_compress(m2m_encoder_s *enc, const frame_s *src, frame_s *dest, bool force_key);