refactoring

This commit is contained in:
Devaev Maxim
2019-03-13 22:10:36 +03:00
parent 8a2a0474b2
commit 966f226dde
9 changed files with 49 additions and 81 deletions

View File

@@ -20,12 +20,10 @@
*****************************************************************************/ *****************************************************************************/
const unsigned BLANK_JPG_WIDTH = 640; const unsigned BLANK_JPEG_WIDTH = 640;
const unsigned BLANK_JPG_HEIGHT = 480; const unsigned BLANK_JPEG_HEIGHT = 480;
const size_t BLANK_JPG_SIZE = 13845; const unsigned char BLANK_JPEG_DATA[] = {
const unsigned char BLANK_JPG_DATA[] = {
0xFF, 0xD8, 0xFF, 0xE1, 0x09, 0x50, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x6E, 0x73, 0x2E, 0x61, 0x64, 0x6F, 0x62, 0x65, 0xFF, 0xD8, 0xFF, 0xE1, 0x09, 0x50, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x6E, 0x73, 0x2E, 0x61, 0x64, 0x6F, 0x62, 0x65,
0x2E, 0x63, 0x6F, 0x6D, 0x2F, 0x78, 0x61, 0x70, 0x2F, 0x31, 0x2E, 0x30, 0x2F, 0x00, 0x3C, 0x3F, 0x78, 0x70, 0x61, 0x63, 0x6B, 0x2E, 0x63, 0x6F, 0x6D, 0x2F, 0x78, 0x61, 0x70, 0x2F, 0x31, 0x2E, 0x30, 0x2F, 0x00, 0x3C, 0x3F, 0x78, 0x70, 0x61, 0x63, 0x6B,
0x65, 0x74, 0x20, 0x62, 0x65, 0x67, 0x69, 0x6E, 0x3D, 0x22, 0xEF, 0xBB, 0xBF, 0x22, 0x20, 0x69, 0x64, 0x3D, 0x22, 0x57, 0x35, 0x65, 0x74, 0x20, 0x62, 0x65, 0x67, 0x69, 0x6E, 0x3D, 0x22, 0xEF, 0xBB, 0xBF, 0x22, 0x20, 0x69, 0x64, 0x3D, 0x22, 0x57, 0x35,

View File

@@ -25,7 +25,7 @@
#include "../config.h" #include "../config.h"
const char *HTML_INDEX_PAGE = " \ const char HTML_INDEX_PAGE[] = " \
<!DOCTYPE html> \ <!DOCTYPE html> \
\ \
<html> \ <html> \

View File

@@ -271,12 +271,10 @@ static int _device_apply_dv_timings(struct device_t *dev) {
LOG_DEBUG("Calling ioctl(VIDIOC_QUERY_DV_TIMINGS) ..."); LOG_DEBUG("Calling ioctl(VIDIOC_QUERY_DV_TIMINGS) ...");
if (xioctl(dev->run->fd, VIDIOC_QUERY_DV_TIMINGS, &dv_timings) == 0) { if (xioctl(dev->run->fd, VIDIOC_QUERY_DV_TIMINGS, &dv_timings) == 0) {
LOG_INFO( LOG_INFO("Got new DV timings: resolution=%ux%u; pixclk=%llu",
"Got new DV timings: resolution=%ux%u; pixclk=%llu",
dv_timings.bt.width, dv_timings.bt.width,
dv_timings.bt.height, dv_timings.bt.height,
dv_timings.bt.pixelclock dv_timings.bt.pixelclock);
);
LOG_DEBUG("Calling ioctl(VIDIOC_S_DV_TIMINGS) ..."); LOG_DEBUG("Calling ioctl(VIDIOC_S_DV_TIMINGS) ...");
if (xioctl(dev->run->fd, VIDIOC_S_DV_TIMINGS, &dv_timings) < 0) { if (xioctl(dev->run->fd, VIDIOC_S_DV_TIMINGS, &dv_timings) < 0) {
@@ -314,12 +312,10 @@ static int _device_open_format(struct device_t *dev) {
// Set format // Set format
LOG_DEBUG("Calling ioctl(VIDIOC_S_FMT) ..."); LOG_DEBUG("Calling ioctl(VIDIOC_S_FMT) ...");
if (xioctl(dev->run->fd, VIDIOC_S_FMT, &fmt) < 0) { if (xioctl(dev->run->fd, VIDIOC_S_FMT, &fmt) < 0) {
LOG_PERROR( LOG_PERROR("Unable to set pixelformat=%s; resolution=%ux%u",
"Unable to set pixelformat=%s; resolution=%ux%u",
_format_to_string_supported(dev->format), _format_to_string_supported(dev->format),
dev->run->width, dev->run->width,
dev->run->height dev->run->height);
);
return -1; return -1;
} }
@@ -337,23 +333,15 @@ static int _device_open_format(struct device_t *dev) {
char format_obtained_str[8]; char format_obtained_str[8];
char *format_str_nullable; char *format_str_nullable;
LOG_ERROR( LOG_ERROR("Could not obtain the requested pixelformat=%s; driver gave us %s",
"Could not obtain the requested pixelformat=%s; driver gave us %s",
_format_to_string_supported(dev->format), _format_to_string_supported(dev->format),
_format_to_string_supported(fmt.fmt.pix.pixelformat) _format_to_string_supported(fmt.fmt.pix.pixelformat));
);
if ((format_str_nullable = (char *)_format_to_string_nullable(fmt.fmt.pix.pixelformat)) != NULL) { if ((format_str_nullable = (char *)_format_to_string_nullable(fmt.fmt.pix.pixelformat)) != NULL) {
LOG_INFO( LOG_INFO("Falling back to pixelformat=%s", format_str_nullable);
"Falling back to %s mode (consider using '--format=%s' option)",
format_str_nullable,
format_str_nullable
);
} else { } else {
LOG_ERROR( LOG_ERROR("Unsupported pixelformat=%s (fourcc)",
"Unsupported pixelformat=%s (fourcc)", _format_to_string_fourcc(format_obtained_str, 8, fmt.fmt.pix.pixelformat));
_format_to_string_fourcc(format_obtained_str, 8, fmt.fmt.pix.pixelformat)
);
return -1; return -1;
} }
} }

View File

@@ -83,10 +83,7 @@ void encoder_prepare(struct encoder_t *encoder, struct device_t *dev) {
LOG_DEBUG("Preparing OMX JPEG encoder ..."); LOG_DEBUG("Preparing OMX JPEG encoder ...");
if (dev->n_workers > OMX_MAX_ENCODERS) { if (dev->n_workers > OMX_MAX_ENCODERS) {
LOG_INFO( LOG_INFO("OMX JPEG encoder sets limit for worker threads: %u", OMX_MAX_ENCODERS);
"OMX-based encoder can only work with %u worker threads; forced --workers=%u",
OMX_MAX_ENCODERS, OMX_MAX_ENCODERS
);
dev->n_workers = OMX_MAX_ENCODERS; dev->n_workers = OMX_MAX_ENCODERS;
} }
encoder->run->n_omxs = dev->n_workers; encoder->run->n_omxs = dev->n_workers;

View File

@@ -29,8 +29,9 @@
#include "../../device.h" #include "../../device.h"
#ifndef OMX_MAX_ENCODERS
#define OMX_MAX_ENCODERS 3 # define OMX_MAX_ENCODERS 3 // Raspberry Pi limitation
#endif
struct omx_encoder_t { struct omx_encoder_t {

View File

@@ -407,16 +407,13 @@ static void _http_callback_stream(struct evhttp_request *request, void *v_server
server->run->stream_clients_count += 1; server->run->stream_clients_count += 1;
evhttp_connection_get_peer(conn, &client_addr, &client_port); evhttp_connection_get_peer(conn, &client_addr, &client_port);
LOG_INFO( LOG_INFO("HTTP: Registered the new stream client: [%s]:%u; id=%s; advance_headers=%s; dual_final_frames=%s; clients now: %u",
"HTTP: Registered the new stream client: [%s]:%u; id=%s;"
" advance_headers=%s; dual_final_frames=%s; clients now: %u",
client_addr, client_addr,
client_port, client_port,
client->id, client->id,
bool_to_string(client->advance_headers), bool_to_string(client->advance_headers),
bool_to_string(client->dual_final_frames), bool_to_string(client->dual_final_frames),
server->run->stream_clients_count server->run->stream_clients_count);
);
buf_event = evhttp_connection_get_bufferevent(conn); buf_event = evhttp_connection_get_bufferevent(conn);
bufferevent_setcb(buf_event, NULL, NULL, _http_callback_stream_error, (void *)client); bufferevent_setcb(buf_event, NULL, NULL, _http_callback_stream_error, (void *)client);
@@ -568,10 +565,8 @@ static void _http_callback_stream_error(UNUSED struct bufferevent *buf_event, UN
if (conn != NULL) { if (conn != NULL) {
evhttp_connection_get_peer(conn, &client_addr, &client_port); evhttp_connection_get_peer(conn, &client_addr, &client_port);
} }
LOG_INFO( LOG_INFO("HTTP: Disconnected the stream client: [%s]:%u; clients now: %u",
"HTTP: Disconnected the stream client: [%s]:%u; clients now: %u", client_addr, client_port, client->server->run->stream_clients_count);
client_addr, client_port, client->server->run->stream_clients_count
);
if (conn != NULL) { if (conn != NULL) {
evhttp_connection_free(conn); evhttp_connection_free(conn);
} }
@@ -688,18 +683,14 @@ static bool _expose_new_picture(struct http_server_t *server) {
) { ) {
EXPOSED(expose_cmp_time) = get_now_monotonic(); EXPOSED(expose_cmp_time) = get_now_monotonic();
EXPOSED(expose_end_time) = EXPOSED(expose_cmp_time); EXPOSED(expose_end_time) = EXPOSED(expose_cmp_time);
LOG_VERBOSE( LOG_VERBOSE("HTTP: dropped same frame number %u; comparsion time = %.06Lf",
"HTTP: dropped same frame number %u; comparsion time = %.06Lf", EXPOSED(dropped), EXPOSED(expose_cmp_time) - EXPOSED(expose_begin_time));
EXPOSED(dropped), EXPOSED(expose_cmp_time) - EXPOSED(expose_begin_time)
);
EXPOSED(dropped) += 1; EXPOSED(dropped) += 1;
return false; // Not updated return false; // Not updated
} else { } else {
EXPOSED(expose_cmp_time) = get_now_monotonic(); EXPOSED(expose_cmp_time) = get_now_monotonic();
LOG_VERBOSE( LOG_VERBOSE("HTTP: passed same frame check (frames are differ); comparsion time = %.06Lf",
"HTTP: passed same frame check (frames are differ); comparsion time = %.06Lf", EXPOSED(expose_cmp_time) - EXPOSED(expose_begin_time));
EXPOSED(expose_cmp_time) - EXPOSED(expose_begin_time)
);
} }
} }
@@ -725,10 +716,8 @@ static bool _expose_new_picture(struct http_server_t *server) {
EXPOSED(expose_cmp_time) = EXPOSED(expose_begin_time); EXPOSED(expose_cmp_time) = EXPOSED(expose_begin_time);
EXPOSED(expose_end_time) = get_now_monotonic(); EXPOSED(expose_end_time) = get_now_monotonic();
LOG_VERBOSE( LOG_VERBOSE("HTTP: exposed new frame; full exposition time = %.06Lf",
"HTTP: exposed new frame; full exposition time = %.06Lf", EXPOSED(expose_end_time) - EXPOSED(expose_begin_time));
EXPOSED(expose_end_time) - EXPOSED(expose_begin_time)
);
# undef EXPOSED # undef EXPOSED
# undef STREAM # undef STREAM
@@ -736,30 +725,28 @@ static bool _expose_new_picture(struct http_server_t *server) {
} }
static bool _expose_blank_picture(struct http_server_t *server) { static bool _expose_blank_picture(struct http_server_t *server) {
# define EXPOSED(_next) server->run->exposed->_next # define EXPOSED(_next) server->run->exposed->_next
# define BLANK_JPEG_LEN ARRAY_LEN(BLANK_JPEG_DATA)
EXPOSED(expose_begin_time) = get_now_monotonic(); EXPOSED(expose_begin_time) = get_now_monotonic();
EXPOSED(expose_cmp_time) = EXPOSED(expose_begin_time); EXPOSED(expose_cmp_time) = EXPOSED(expose_begin_time);
if (EXPOSED(online) || EXPOSED(picture.size) == 0) { if (EXPOSED(online) || EXPOSED(picture.size) == 0) {
if (EXPOSED(picture.allocated) < BLANK_JPG_SIZE) { if (EXPOSED(picture.allocated) < BLANK_JPEG_LEN) {
A_REALLOC(EXPOSED(picture.data), BLANK_JPG_SIZE); A_REALLOC(EXPOSED(picture.data), BLANK_JPEG_LEN);
EXPOSED(picture.allocated) = BLANK_JPG_SIZE; EXPOSED(picture.allocated) = BLANK_JPEG_LEN;
} }
memcpy( memcpy(EXPOSED(picture.data), BLANK_JPEG_DATA, BLANK_JPEG_LEN * sizeof(*EXPOSED(picture.data)));
EXPOSED(picture.data), BLANK_JPG_DATA,
BLANK_JPG_SIZE * sizeof(*EXPOSED(picture.data))
);
EXPOSED(picture.size) = BLANK_JPG_SIZE; EXPOSED(picture.size) = BLANK_JPEG_LEN;
EXPOSED(picture.grab_time) = 0; EXPOSED(picture.grab_time) = 0;
EXPOSED(picture.encode_begin_time) = 0; EXPOSED(picture.encode_begin_time) = 0;
EXPOSED(picture.encode_end_time) = 0; EXPOSED(picture.encode_end_time) = 0;
EXPOSED(width) = BLANK_JPG_WIDTH; EXPOSED(width) = BLANK_JPEG_WIDTH;
EXPOSED(height) = BLANK_JPG_HEIGHT; EXPOSED(height) = BLANK_JPEG_HEIGHT;
EXPOSED(captured_fps) = 0; EXPOSED(captured_fps) = 0;
EXPOSED(online) = false; EXPOSED(online) = false;
goto updated; goto updated;
@@ -777,5 +764,6 @@ static bool _expose_blank_picture(struct http_server_t *server) {
EXPOSED(expose_end_time) = get_now_monotonic(); EXPOSED(expose_end_time) = get_now_monotonic();
return true; // Updated return true; // Updated
# undef BLANK_JPEG_LEN
# undef EXPOSED # undef EXPOSED
} }

View File

@@ -302,10 +302,9 @@ static void _stream_expose_picture(struct stream_t *stream, unsigned buf_index)
stream->picture.size = PICTURE(size); stream->picture.size = PICTURE(size);
stream->picture.allocated = PICTURE(allocated); stream->picture.allocated = PICTURE(allocated);
memcpy(
stream->picture.data, PICTURE(data), memcpy(stream->picture.data, PICTURE(data), stream->picture.size * sizeof(*stream->picture.data));
stream->picture.size * sizeof(*stream->picture.data)
);
stream->picture.grab_time = PICTURE(grab_time); stream->picture.grab_time = PICTURE(grab_time);
stream->picture.encode_begin_time = PICTURE(encode_begin_time); stream->picture.encode_begin_time = PICTURE(encode_begin_time);
stream->picture.encode_end_time = PICTURE(encode_end_time); stream->picture.encode_end_time = PICTURE(encode_end_time);
@@ -466,10 +465,8 @@ static void *_stream_worker_thread(void *v_ctx) {
*ctx->last_comp_time = last_comp_time; *ctx->last_comp_time = last_comp_time;
A_PTHREAD_M_UNLOCK(ctx->last_comp_time_mutex); A_PTHREAD_M_UNLOCK(ctx->last_comp_time_mutex);
LOG_VERBOSE( LOG_VERBOSE("Compressed JPEG size=%zu; time=%0.3Lf; worker=%u; buffer=%u",
"Compressed JPEG size=%zu; time=%0.3Lf; worker=%u; buffer=%u", PICTURE(size), last_comp_time, ctx->number, ctx->buf_index);
PICTURE(size), last_comp_time, ctx->number, ctx->buf_index
);
} else { } else {
*ctx->job_failed = true; *ctx->job_failed = true;
*ctx->has_job = false; *ctx->has_job = false;

View File

@@ -41,7 +41,7 @@ def main():
text = text.replace("%VERSION%", "\" VERSION \"") text = text.replace("%VERSION%", "\" VERSION \"")
text = textwrap.indent(text, "\t", (lambda line: True)) text = textwrap.indent(text, "\t", (lambda line: True))
text = "\n".join(("%s \\" if line.strip() else "%s\\") % (line) for line in text.split("\n")) text = "\n".join(("%s \\" if line.strip() else "%s\\") % (line) for line in text.split("\n"))
text = "const char *%s = \" \\\n%s\n\";\n" % (name, text) text = "const char %s[] = \" \\\n%s\n\";\n" % (name, text)
text = textwrap.dedent(""" text = textwrap.dedent("""
/***************************************************************************** /*****************************************************************************
# # # #

View File

@@ -35,20 +35,19 @@ def main():
width = int(sys.argv[4]) width = int(sys.argv[4])
height = int(sys.argv[5]) height = int(sys.argv[5])
with open(src, "rb") as jpg_file: with open(src, "rb") as jpeg_file:
jpg_data = jpg_file.read() jpeg_data = jpeg_file.read()
rows = [[]] rows = [[]]
for ch in jpg_data: for ch in jpeg_data:
if len(rows[-1]) > 20: if len(rows[-1]) > 20:
rows.append([]) rows.append([])
rows[-1].append("0x%.2X" % (ch)) rows[-1].append("0x%.2X" % (ch))
text = ",\n\t".join(", ".join(row) for row in rows) text = ",\n\t".join(", ".join(row) for row in rows)
text = "const unsigned char %s_JPG_DATA[] = {\n\t%s\n};\n" % (prefix, text) text = "const unsigned char %s_JPEG_DATA[] = {\n\t%s\n};\n" % (prefix, text)
text = "const size_t %s_JPG_SIZE = %d;\n\n" % (prefix, len(jpg_data)) + text text = "const unsigned %s_JPEG_HEIGHT = %d;\n\n" % (prefix, height) + text
text = "const unsigned %s_JPG_HEIGHT = %d;\n\n" % (prefix, height) + text text = "const unsigned %s_JPEG_WIDTH = %d;\n" % (prefix, width) + text
text = "const unsigned %s_JPG_WIDTH = %d;\n" % (prefix, width) + text
text = textwrap.dedent(""" text = textwrap.dedent("""
/***************************************************************************** /*****************************************************************************
# # # #