Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mdevaev committed Mar 29, 2024
1 parent caf9ed7 commit 8e6c374
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 92 deletions.
56 changes: 47 additions & 9 deletions src/libs/drm/drm.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ us_drm_s *us_drm_init(void) {
run->fd = -1;
run->status_fd = -1;
run->dpms_state = -1;
run->opened = -1;
run->has_vsync = true;
run->exposing_dma_fd = -1;
run->ft = us_frametext_init();
Expand All @@ -82,6 +83,7 @@ us_drm_s *us_drm_init(void) {
drm->path = "/dev/dri/by-path/platform-gpu-card";
drm->port = "HDMI-A-2"; // OUT2 on PiKVM V4 Plus
drm->timeout = 5;
drm->blank_after = 5;
drm->run = run;
return drm;
}
Expand All @@ -97,6 +99,8 @@ int us_drm_open(us_drm_s *drm, const us_capture_s *cap) {

assert(run->fd < 0);

_LOG_INFO("Using passthrough: %s[%s]", drm->path, drm->port);

switch (_drm_check_status(drm)) {
case 0: break;
case US_ERROR_NO_DEVICE: goto unplugged;
Expand Down Expand Up @@ -164,22 +168,24 @@ int us_drm_open(us_drm_s *drm, const us_capture_s *cap) {
goto error;
}

run->opened_for_stub = (stub > 0);
_LOG_INFO("Opened for %s ...", (stub > 0 ? "STUB" : "DMA"));
run->exposing_dma_fd = -1;
run->unplugged_once = 0;
_LOG_INFO("Opened for %s ...", (run->opened_for_stub ? "STUB" : "DMA"));
return stub;
run->blank_at_ts = 0;
run->opened = stub;
run->once = 0;
return run->opened;

error:
us_drm_close(drm);
return -1;
return run->opened; // -1 after us_drm_close()

unplugged:
US_ONCE_FOR(run->unplugged_once, __LINE__, {
US_ONCE_FOR(run->once, __LINE__, {
_LOG_ERROR("Display is not plugged");
});
us_drm_close(drm);
return US_ERROR_NO_DEVICE;
run->opened = US_ERROR_NO_DEVICE;
return run->opened;
}

void us_drm_close(us_drm_s *drm) {
Expand Down Expand Up @@ -233,6 +239,7 @@ void us_drm_close(us_drm_s *drm) {

run->crtc_id = 0;
run->dpms_state = -1;
run->opened = -1;
run->has_vsync = true;
run->stub_n_buf = 0;

Expand All @@ -241,6 +248,34 @@ void us_drm_close(us_drm_s *drm) {
}
}

int us_drm_ensure_no_signal(us_drm_s *drm) {
us_drm_runtime_s *const run = drm->run;

assert(run->fd >= 0);
assert(run->opened > 0);

const ldf now_ts = us_get_now_monotonic();
if (run->blank_at_ts == 0) {
run->blank_at_ts = now_ts + drm->blank_after;
}
const ldf saved_ts = run->blank_at_ts; // us_drm*() rewrites it to 0

int retval;
if (now_ts <= run->blank_at_ts) {
retval = us_drm_wait_for_vsync(drm);
if (retval == 0) {
retval = us_drm_expose_stub(drm, US_DRM_STUB_NO_SIGNAL, NULL);
}
} else {
US_ONCE_FOR(run->once, __LINE__, {
_LOG_INFO("Turning off the display by timeout ...");
});
retval = us_drm_dpms_power_off(drm);
}
run->blank_at_ts = saved_ts;
return retval;
}

int us_drm_dpms_power_off(us_drm_s *drm) {
assert(drm->run->fd >= 0);
switch (_drm_check_status(drm)) {
Expand All @@ -259,6 +294,7 @@ int us_drm_wait_for_vsync(us_drm_s *drm) {
us_drm_runtime_s *const run = drm->run;

assert(run->fd >= 0);
run->blank_at_ts = 0;

switch (_drm_check_status(drm)) {
case 0: break;
Expand Down Expand Up @@ -313,7 +349,8 @@ int us_drm_expose_stub(us_drm_s *drm, us_drm_stub_e stub, const us_capture_s *ca
us_drm_runtime_s *const run = drm->run;

assert(run->fd >= 0);
assert(run->opened_for_stub);
assert(run->opened > 0);
run->blank_at_ts = 0;

switch (_drm_check_status(drm)) {
case 0: break;
Expand Down Expand Up @@ -377,7 +414,8 @@ int us_drm_expose_dma(us_drm_s *drm, const us_capture_hwbuf_s *hw) {
us_drm_buffer_s *const buf = &run->bufs[hw->buf.index];

assert(run->fd >= 0);
assert(!run->opened_for_stub);
assert(run->opened == 0);
run->blank_at_ts = 0;

switch (_drm_check_status(drm)) {
case 0: break;
Expand Down
9 changes: 7 additions & 2 deletions src/libs/drm/drm.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,22 @@ typedef struct {
uint n_bufs;
drmModeCrtc *saved_crtc;
int dpms_state;
bool opened_for_stub;
int opened;

bool has_vsync;
int exposing_dma_fd;
uint stub_n_buf;
int unplugged_once;
ldf blank_at_ts;

int once;
us_frametext_s *ft;
} us_drm_runtime_s;

typedef struct {
char *path;
char *port;
uint timeout;
uint blank_after;

us_drm_runtime_s *run;
} us_drm_s;
Expand All @@ -90,3 +94,4 @@ int us_drm_dpms_power_off(us_drm_s *drm);
int us_drm_wait_for_vsync(us_drm_s *drm);
int us_drm_expose_stub(us_drm_s *drm, us_drm_stub_e stub, const us_capture_s *cap);
int us_drm_expose_dma(us_drm_s *drm, const us_capture_hwbuf_s *hw);
int us_drm_ensure_no_signal(us_drm_s *drm);
8 changes: 7 additions & 1 deletion src/ustreamer/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,9 @@ void us_options_destroy(us_options_s *options) {
US_DELETE(options->jpeg_sink, us_memsink_destroy);
US_DELETE(options->raw_sink, us_memsink_destroy);
US_DELETE(options->h264_sink, us_memsink_destroy);
# ifdef WITH_V4P
US_DELETE(options->drm, us_drm_destroy);
# endif

for (unsigned index = 0; index < options->argc; ++index) {
free(options->argv_copy[index]);
Expand Down Expand Up @@ -463,7 +466,10 @@ int options_parse(us_options_s *options, us_capture_s *cap, us_encoder_s *enc, u
case _O_H264_M2M_DEVICE: OPT_SET(stream->h264_m2m_path, optarg);

# ifdef WITH_V4P
case _O_V4P: OPT_SET(stream->v4p, true);
case _O_V4P:
options->drm = us_drm_init();
stream->drm = options->drm;
break;
# endif

# ifdef WITH_GPIO
Expand Down
6 changes: 6 additions & 0 deletions src/ustreamer/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
#include "../libs/memsink.h"
#include "../libs/options.h"
#include "../libs/capture.h"
#ifdef WITH_V4P
# include "../libs/drm/drm.h"
#endif

#include "encoder.h"
#include "stream.h"
Expand All @@ -56,6 +59,9 @@ typedef struct {
us_memsink_s *jpeg_sink;
us_memsink_s *raw_sink;
us_memsink_s *h264_sink;
# ifdef WITH_V4P
us_drm_s *drm;
# endif
} us_options_s;


Expand Down
69 changes: 20 additions & 49 deletions src/ustreamer/stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,6 @@ void us_stream_loop(us_stream_s *stream) {
run->h264 = us_h264_stream_init(stream->h264_sink, stream->h264_m2m_path, stream->h264_bitrate, stream->h264_gop);
}

# ifdef WITH_V4P
if (stream->v4p) {
run->drm = us_drm_init();
run->drm_opened = -1;
US_LOG_INFO("Using passthrough: %s[%s]", run->drm->path, run->drm->port);
}
# endif

while (!_stream_init_loop(stream)) {
atomic_bool threads_stop;
atomic_init(&threads_stop, false);
Expand Down Expand Up @@ -177,7 +169,7 @@ void us_stream_loop(us_stream_s *stream) {
CREATE_WORKER((run->h264 != NULL), h264_ctx, _h264_thread, cap->run->n_bufs);
CREATE_WORKER((stream->raw_sink != NULL), raw_ctx, _raw_thread, 2);
# ifdef WITH_V4P
CREATE_WORKER((run->drm != NULL), drm_ctx, _drm_thread, cap->run->n_bufs); // cppcheck-suppress assertWithSideEffect
CREATE_WORKER((stream->drm != NULL), drm_ctx, _drm_thread, cap->run->n_bufs); // cppcheck-suppress assertWithSideEffect
# endif
# undef CREATE_WORKER

Expand Down Expand Up @@ -267,9 +259,6 @@ void us_stream_loop(us_stream_s *stream) {
}
}

# ifdef WITH_V4P
US_DELETE(run->drm, us_drm_destroy);
# endif
US_DELETE(run->h264, us_h264_stream_destroy);
}

Expand Down Expand Up @@ -453,11 +442,10 @@ static void *_raw_thread(void *v_ctx) {
static void *_drm_thread(void *v_ctx) {
US_THREAD_SETTLE("str_drm");
_worker_context_s *ctx = v_ctx;
us_stream_runtime_s *run = ctx->stream->run;
us_stream_s *stream = ctx->stream;

// Close previously opened DRM for a stub
us_drm_close(run->drm);
run->drm_opened = -1;
us_drm_close(stream->drm);

us_capture_hwbuf_s *prev_hw = NULL;
while (!atomic_load(ctx->stop)) {
Expand All @@ -472,32 +460,31 @@ static void *_drm_thread(void *v_ctx) {
} \
}

CHECK(run->drm_opened = us_drm_open(run->drm, ctx->stream->cap));
CHECK(us_drm_open(stream->drm, ctx->stream->cap));

while (!atomic_load(ctx->stop)) {
CHECK(us_drm_wait_for_vsync(run->drm));
CHECK(us_drm_wait_for_vsync(stream->drm));
US_DELETE(prev_hw, us_capture_hwbuf_decref);

us_capture_hwbuf_s *hw = _get_latest_hw(ctx->queue);
if (hw == NULL) {
continue;
}

if (run->drm_opened == 0) {
CHECK(us_drm_expose_dma(run->drm, hw));
if (stream->drm->run->opened == 0) {
CHECK(us_drm_expose_dma(stream->drm, hw));
prev_hw = hw;
continue;
}

CHECK(us_drm_expose_stub(run->drm, run->drm_opened, ctx->stream->cap));
CHECK(us_drm_expose_stub(stream->drm, stream->drm->run->opened, ctx->stream->cap));
us_capture_hwbuf_decref(hw);

SLOWDOWN;
}

close:
us_drm_close(run->drm);
run->drm_opened = -1;
us_drm_close(stream->drm);
US_DELETE(prev_hw, us_capture_hwbuf_decref);
SLOWDOWN;

Expand Down Expand Up @@ -536,7 +523,7 @@ static bool _stream_has_any_clients_cached(us_stream_s *stream) {
|| (run->h264 != NULL && atomic_load(&run->h264->sink->has_clients))
|| (stream->raw_sink != NULL && atomic_load(&stream->raw_sink->has_clients))
# ifdef WITH_V4P
|| (run->drm != NULL)
|| (stream->drm != NULL)
# endif
);
}
Expand Down Expand Up @@ -568,7 +555,7 @@ static int _stream_init_loop(us_stream_s *stream) {
stream->enc->type == US_ENCODER_TYPE_M2M_VIDEO
|| stream->enc->type == US_ENCODER_TYPE_M2M_IMAGE
|| run->h264 != NULL
|| run->drm != NULL
|| stream->drm != NULL
);
switch (us_capture_open(stream->cap)) {
case 0: break;
Expand Down Expand Up @@ -618,37 +605,21 @@ static int _stream_init_loop(us_stream_s *stream) {

#ifdef WITH_V4P
static void _stream_drm_ensure_no_signal(us_stream_s *stream) {
us_stream_runtime_s *const run = stream->run;

if (run->drm == NULL) {
if (stream->drm == NULL) {
return;
}

# define CHECK(x_arg) if ((x_arg) < 0) { goto close; }
if (run->drm_opened <= 0) {
us_drm_close(run->drm);
run->drm_blank_at_ts = 0;
CHECK(run->drm_opened = us_drm_open(run->drm, NULL));
if (stream->drm->run->opened <= 0) {
us_drm_close(stream->drm);
if (us_drm_open(stream->drm, NULL) < 0) {
goto close;
}
}

ldf now_ts = us_get_now_monotonic();
if (run->drm_blank_at_ts == 0) {
run->drm_blank_at_ts = now_ts + 5;
}

if (now_ts <= run->drm_blank_at_ts) {
CHECK(us_drm_wait_for_vsync(run->drm));
CHECK(us_drm_expose_stub(run->drm, US_DRM_STUB_NO_SIGNAL, NULL));
} else {
// US_ONCE({ US_LOG_INFO("DRM: Turning off the display by timeout ..."); });
CHECK(us_drm_dpms_power_off(run->drm));
if (us_drm_ensure_no_signal(stream->drm) < 0) {
goto close;
}
return;
# undef CHECK

close:
us_drm_close(run->drm);
run->drm_opened = -1;
us_drm_close(stream->drm);
}
#endif

Expand Down
8 changes: 1 addition & 7 deletions src/ustreamer/stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@
typedef struct {
us_h264_stream_s *h264;

# ifdef WITH_V4P
us_drm_s *drm;
int drm_opened;
ldf drm_blank_at_ts;
# endif

us_ring_s *http_jpeg_ring;
atomic_bool http_has_clients;
atomic_uint http_snapshot_requested;
Expand Down Expand Up @@ -78,7 +72,7 @@ typedef struct {
char *h264_m2m_path;

# ifdef WITH_V4P
bool v4p;
us_drm_s *drm;
# endif

us_stream_runtime_s *run;
Expand Down
Loading

0 comments on commit 8e6c374

Please sign in to comment.