From e0f09f65a180f7b255809dd466c0487ac8ebba30 Mon Sep 17 00:00:00 2001 From: Maxim Devaev Date: Fri, 29 Mar 2024 01:02:36 +0200 Subject: [PATCH] new macro US_ONCE_FOR() --- src/libs/capture.c | 15 ++++++--------- src/libs/capture.h | 2 +- src/libs/drm/drm.c | 7 +++---- src/libs/drm/drm.h | 2 +- src/libs/tools.h | 10 ++++++---- src/ustreamer/stream.c | 9 +++------ 6 files changed, 20 insertions(+), 25 deletions(-) diff --git a/src/libs/capture.c b/src/libs/capture.c index 54e1abf8..3a0eee0f 100644 --- a/src/libs/capture.c +++ b/src/libs/capture.c @@ -176,10 +176,9 @@ int us_capture_open(us_capture_s *cap) { us_capture_runtime_s *const run = cap->run; if (access(cap->path, R_OK | W_OK) < 0) { - if (run->open_error_reported != -errno) { - run->open_error_reported = -errno; // Don't confuse it with __LINE__ + US_ONCE_FOR(run->open_error_once, -errno, { US_LOG_PERROR("No access to capture device"); - } + }); goto error_no_device; } @@ -193,11 +192,9 @@ int us_capture_open(us_capture_s *cap) { if (cap->dv_timings && cap->persistent) { _LOG_DEBUG("Probing DV-timings or QuerySTD ..."); if (_capture_open_dv_timings(cap, false) < 0) { - const int line = __LINE__; - if (run->open_error_reported != line) { - run->open_error_reported = line; + US_ONCE_FOR(run->open_error_once, __LINE__, { _LOG_ERROR("No signal from source"); - } + }); goto error_no_signal; } } @@ -238,7 +235,7 @@ int us_capture_open(us_capture_s *cap) { } run->streamon = true; - run->open_error_reported = 0; + run->open_error_once = 0; _LOG_INFO("Capturing started"); return 0; @@ -251,7 +248,7 @@ int us_capture_open(us_capture_s *cap) { return US_ERROR_NO_DATA; error: - run->open_error_reported = 0; + run->open_error_once = 0; us_capture_close(cap); return -1; } diff --git a/src/libs/capture.h b/src/libs/capture.h index cf587a47..087e16b7 100644 --- a/src/libs/capture.h +++ b/src/libs/capture.h @@ -67,7 +67,7 @@ typedef struct { enum v4l2_buf_type capture_type; bool capture_mplane; bool streamon; - int open_error_reported; + int open_error_once; } us_capture_runtime_s; typedef enum { diff --git a/src/libs/drm/drm.c b/src/libs/drm/drm.c index de560a31..124b009e 100644 --- a/src/libs/drm/drm.c +++ b/src/libs/drm/drm.c @@ -166,7 +166,7 @@ int us_drm_open(us_drm_s *drm, const us_capture_s *cap) { run->opened_for_stub = (stub > 0); run->exposing_dma_fd = -1; - run->unplugged_reported = false; + run->unplugged_once = 0; _LOG_INFO("Opened for %s ...", (run->opened_for_stub ? "STUB" : "DMA")); return stub; @@ -175,10 +175,9 @@ int us_drm_open(us_drm_s *drm, const us_capture_s *cap) { return -1; unplugged: - if (!run->unplugged_reported) { + US_ONCE_FOR(run->unplugged_once, __LINE__, { _LOG_ERROR("Display is not plugged"); - run->unplugged_reported = true; - } + }); us_drm_close(drm); return US_ERROR_NO_DEVICE; } diff --git a/src/libs/drm/drm.h b/src/libs/drm/drm.h index ed5f773e..a45c1d79 100644 --- a/src/libs/drm/drm.h +++ b/src/libs/drm/drm.h @@ -67,7 +67,7 @@ typedef struct { bool has_vsync; int exposing_dma_fd; uint stub_n_buf; - bool unplugged_reported; + int unplugged_once; us_frametext_s *ft; } us_drm_runtime_s; diff --git a/src/libs/tools.h b/src/libs/tools.h index eed365f5..8d54dcfc 100644 --- a/src/libs/tools.h +++ b/src/libs/tools.h @@ -72,14 +72,16 @@ (m_a > m_b ? m_a : m_b); \ }) -#define US_ONCE(...) { \ - const int m_reported = __LINE__; \ - if (m_reported != once) { \ +#define US_ONCE_FOR(x_once, x_value, ...) { \ + const int m_reported = (x_value); \ + if (m_reported != (x_once)) { \ __VA_ARGS__; \ - once = m_reported; \ + (x_once) = m_reported; \ } \ } +#define US_ONCE(...) US_ONCE_FOR(once, __LINE__, ##__VA_ARGS__) + INLINE char *us_strdup(const char *str) { char *const new = strdup(str); diff --git a/src/ustreamer/stream.c b/src/ustreamer/stream.c index ccd2e62b..90da9e02 100644 --- a/src/ustreamer/stream.c +++ b/src/ustreamer/stream.c @@ -576,7 +576,7 @@ static bool _stream_has_any_clients_cached(us_stream_s *stream) { static int _stream_init_loop(us_stream_s *stream) { us_stream_runtime_s *const run = stream->run; - bool waiting_reported = false; + int once = 0; while (!atomic_load(&stream->run->stop)) { # ifdef WITH_GPIO us_gpio_set_stream_online(false); @@ -605,13 +605,10 @@ static int _stream_init_loop(us_stream_s *stream) { case 0: break; case US_ERROR_NO_DEVICE: case US_ERROR_NO_DATA: - if (!waiting_reported) { - waiting_reported = true; - US_LOG_INFO("Waiting for the capture device ..."); - } + US_ONCE({ US_LOG_INFO("Waiting for the capture device ..."); }); goto offline_and_retry; default: - waiting_reported = false; + once = 0; goto offline_and_retry; } us_encoder_open(stream->enc, stream->cap);