mirror of
https://github.com/pikvm/ustreamer.git
synced 2026-03-11 10:03:43 +00:00
refactoring
This commit is contained in:
@@ -273,7 +273,9 @@ int device_select(device_s *dev, bool *has_read, bool *has_write, bool *has_erro
|
|||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
int device_grab_buffer(device_s *dev) {
|
int device_grab_buffer(device_s *dev, hw_buffer_s **hw) {
|
||||||
|
*hw = NULL;
|
||||||
|
|
||||||
struct v4l2_buffer buf_info;
|
struct v4l2_buffer buf_info;
|
||||||
MEMSET_ZERO(buf_info);
|
MEMSET_ZERO(buf_info);
|
||||||
buf_info.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
buf_info.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
||||||
@@ -330,24 +332,22 @@ int device_grab_buffer(device_s *dev) {
|
|||||||
HW(raw.grab_ts) = get_now_monotonic();
|
HW(raw.grab_ts) = get_now_monotonic();
|
||||||
|
|
||||||
# undef HW
|
# undef HW
|
||||||
|
*hw = &RUN(hw_buffers[buf_info.index]);
|
||||||
return buf_info.index;
|
return buf_info.index;
|
||||||
}
|
}
|
||||||
|
|
||||||
int device_release_buffer(device_s *dev, unsigned index) {
|
int device_release_buffer(device_s *dev, hw_buffer_s *hw) {
|
||||||
# define HW(_next) RUN(hw_buffers)[index]._next
|
const unsigned index = hw->buf_info.index;
|
||||||
|
|
||||||
LOG_DEBUG("Releasing device buffer index=%u ...", index);
|
LOG_DEBUG("Releasing device buffer index=%u ...", index);
|
||||||
|
|
||||||
A_MUTEX_LOCK(&HW(grabbed_mutex));
|
A_MUTEX_LOCK(&hw->grabbed_mutex);
|
||||||
if (xioctl(RUN(fd), VIDIOC_QBUF, &HW(buf_info)) < 0) {
|
if (xioctl(RUN(fd), VIDIOC_QBUF, &hw->buf_info) < 0) {
|
||||||
LOG_PERROR("Unable to release device buffer index=%u", index);
|
LOG_PERROR("Unable to release device buffer index=%u", index);
|
||||||
A_MUTEX_UNLOCK(&HW(grabbed_mutex));
|
A_MUTEX_UNLOCK(&hw->grabbed_mutex);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
HW(grabbed) = false;
|
hw->grabbed = false;
|
||||||
A_MUTEX_UNLOCK(&HW(grabbed_mutex));
|
A_MUTEX_UNLOCK(&hw->grabbed_mutex);
|
||||||
|
|
||||||
# undef HW
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -155,6 +155,6 @@ void device_close(device_s *dev);
|
|||||||
|
|
||||||
int device_switch_capturing(device_s *dev, bool enable);
|
int device_switch_capturing(device_s *dev, bool enable);
|
||||||
int device_select(device_s *dev, bool *has_read, bool *has_write, bool *has_error);
|
int device_select(device_s *dev, bool *has_read, bool *has_write, bool *has_error);
|
||||||
int device_grab_buffer(device_s *dev);
|
int device_grab_buffer(device_s *dev, hw_buffer_s **hw);
|
||||||
int device_release_buffer(device_s *dev, unsigned index);
|
int device_release_buffer(device_s *dev, hw_buffer_s *hw);
|
||||||
int device_consume_event(device_s *dev);
|
int device_consume_event(device_s *dev);
|
||||||
|
|||||||
@@ -26,7 +26,7 @@
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
device_s *dev;
|
device_s *dev;
|
||||||
encoder_s *encoder;
|
encoder_s *encoder;
|
||||||
unsigned buf_index;
|
hw_buffer_s *hw;
|
||||||
char *dest_role;
|
char *dest_role;
|
||||||
frame_s *dest;
|
frame_s *dest;
|
||||||
} _job_s;
|
} _job_s;
|
||||||
@@ -73,10 +73,8 @@ void stream_destroy(stream_s *stream) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void stream_loop(stream_s *stream) {
|
void stream_loop(stream_s *stream) {
|
||||||
# define DEV(_next) stream->dev->_next
|
LOG_INFO("Using V4L2 device: %s", stream->dev->path);
|
||||||
|
LOG_INFO("Using desired FPS: %u", stream->dev->desired_fps);
|
||||||
LOG_INFO("Using V4L2 device: %s", DEV(path));
|
|
||||||
LOG_INFO("Using desired FPS: %u", DEV(desired_fps));
|
|
||||||
|
|
||||||
for (workers_pool_s *pool; (pool = _stream_init_loop(stream)) != NULL;) {
|
for (workers_pool_s *pool; (pool = _stream_init_loop(stream)) != NULL;) {
|
||||||
long double grab_after = 0;
|
long double grab_after = 0;
|
||||||
@@ -137,13 +135,15 @@ void stream_loop(stream_s *stream) {
|
|||||||
const long double now = get_now_monotonic();
|
const long double now = get_now_monotonic();
|
||||||
const long long now_second = floor_ms(now);
|
const long long now_second = floor_ms(now);
|
||||||
|
|
||||||
int buf_index = device_grab_buffer(stream->dev);
|
hw_buffer_s *hw;
|
||||||
|
int buf_index = device_grab_buffer(stream->dev, &hw);
|
||||||
|
|
||||||
if (buf_index >= 0) {
|
if (buf_index >= 0) {
|
||||||
if (now < grab_after) {
|
if (now < grab_after) {
|
||||||
fluency_passed += 1;
|
fluency_passed += 1;
|
||||||
LOG_VERBOSE("Passed %u frames for fluency: now=%.03Lf, grab_after=%.03Lf",
|
LOG_VERBOSE("Passed %u frames for fluency: now=%.03Lf, grab_after=%.03Lf",
|
||||||
fluency_passed, now, grab_after);
|
fluency_passed, now, grab_after);
|
||||||
if (device_release_buffer(stream->dev, buf_index) < 0) {
|
if (device_release_buffer(stream->dev, hw) < 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -163,14 +163,14 @@ void stream_loop(stream_s *stream) {
|
|||||||
|
|
||||||
# ifdef WITH_MEMSINK
|
# ifdef WITH_MEMSINK
|
||||||
if (stream->raw_sink) {
|
if (stream->raw_sink) {
|
||||||
if (memsink_server_put(stream->raw_sink, &DEV(run->hw_buffers[buf_index].raw)) < 0) {
|
if (memsink_server_put(stream->raw_sink, &hw->raw) < 0) {
|
||||||
stream->raw_sink = NULL;
|
stream->raw_sink = NULL;
|
||||||
LOG_ERROR("RAW sink completely disabled due error");
|
LOG_ERROR("RAW sink completely disabled due error");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
# endif
|
# endif
|
||||||
|
|
||||||
((_job_s *)ready_wr->job)->buf_index = buf_index;
|
((_job_s *)ready_wr->job)->hw = hw;
|
||||||
workers_pool_assign(pool, ready_wr);
|
workers_pool_assign(pool, ready_wr);
|
||||||
LOG_DEBUG("Assigned new frame in buffer %d to worker %s", buf_index, ready_wr->name);
|
LOG_DEBUG("Assigned new frame in buffer %d to worker %s", buf_index, ready_wr->name);
|
||||||
}
|
}
|
||||||
@@ -201,8 +201,6 @@ void stream_loop(stream_s *stream) {
|
|||||||
gpio_set_stream_online(false);
|
gpio_set_stream_online(false);
|
||||||
# endif
|
# endif
|
||||||
}
|
}
|
||||||
|
|
||||||
# undef DEV
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void stream_loop_break(stream_s *stream) {
|
void stream_loop_break(stream_s *stream) {
|
||||||
@@ -367,22 +365,18 @@ static void _worker_job_destroy(void *v_job) {
|
|||||||
static bool _worker_run_job(worker_s *wr) {
|
static bool _worker_run_job(worker_s *wr) {
|
||||||
_job_s *job = (_job_s *)wr->job;
|
_job_s *job = (_job_s *)wr->job;
|
||||||
|
|
||||||
LOG_DEBUG("Worker %s compressing JPEG from buffer %u ...", wr->name, job->buf_index);
|
LOG_DEBUG("Worker %s compressing JPEG from buffer %u ...", wr->name, job->hw->buf_info.index);
|
||||||
bool ok = !encoder_compress(
|
bool ok = !encoder_compress(job->encoder, wr->number, &job->hw->raw, job->dest);
|
||||||
job->encoder,
|
|
||||||
wr->number,
|
|
||||||
&job->dev->run->hw_buffers[job->buf_index].raw,
|
|
||||||
job->dest);
|
|
||||||
|
|
||||||
if (device_release_buffer(job->dev, job->buf_index) == 0) {
|
if (device_release_buffer(job->dev, job->hw) == 0) {
|
||||||
if (ok) {
|
if (ok) {
|
||||||
LOG_VERBOSE("Compressed new JPEG: size=%zu, time=%0.3Lf, worker=%s, buffer=%u",
|
LOG_VERBOSE("Compressed new JPEG: size=%zu, time=%0.3Lf, worker=%s, buffer=%u",
|
||||||
job->dest->used,
|
job->dest->used,
|
||||||
job->dest->encode_end_ts - job->dest->encode_begin_ts,
|
job->dest->encode_end_ts - job->dest->encode_begin_ts,
|
||||||
wr->name,
|
wr->name,
|
||||||
job->buf_index);
|
job->hw->buf_info.index);
|
||||||
} else {
|
} else {
|
||||||
LOG_VERBOSE("Compression failed: worker=%s, buffer=%u", wr->name, job->buf_index);
|
LOG_VERBOSE("Compression failed: worker=%s, buffer=%u", wr->name, job->hw->buf_info.index);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ok = false;
|
ok = false;
|
||||||
|
|||||||
Reference in New Issue
Block a user