Skip to content

Commit

Permalink
new macro US_ONCE_FOR()
Browse files Browse the repository at this point in the history
  • Loading branch information
mdevaev committed Mar 28, 2024
1 parent 4e1f62b commit e0f09f6
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 25 deletions.
15 changes: 6 additions & 9 deletions src/libs/capture.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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;

Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/libs/capture.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 3 additions & 4 deletions src/libs/drm/drm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/libs/drm/drm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
10 changes: 6 additions & 4 deletions src/libs/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 3 additions & 6 deletions src/ustreamer/stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit e0f09f6

Please sign in to comment.