From 966f226dde733091a301e120948dcdee4c1cf17b Mon Sep 17 00:00:00 2001 From: Devaev Maxim Date: Wed, 13 Mar 2019 22:10:36 +0300 Subject: [PATCH] refactoring --- src/data/blank_jpeg.h | 8 +++--- src/data/index_html.h | 2 +- src/device.c | 30 +++++++--------------- src/encoder.c | 5 +--- src/encoders/omx/encoder.h | 5 ++-- src/http.c | 52 +++++++++++++++----------------------- src/stream.c | 13 ++++------ tools/make-html-h.py | 2 +- tools/make-jpeg-h.py | 13 +++++----- 9 files changed, 49 insertions(+), 81 deletions(-) diff --git a/src/data/blank_jpeg.h b/src/data/blank_jpeg.h index a7471d8..f83fb52 100644 --- a/src/data/blank_jpeg.h +++ b/src/data/blank_jpeg.h @@ -20,12 +20,10 @@ *****************************************************************************/ -const unsigned BLANK_JPG_WIDTH = 640; -const unsigned BLANK_JPG_HEIGHT = 480; +const unsigned BLANK_JPEG_WIDTH = 640; +const unsigned BLANK_JPEG_HEIGHT = 480; -const size_t BLANK_JPG_SIZE = 13845; - -const unsigned char BLANK_JPG_DATA[] = { +const unsigned char BLANK_JPEG_DATA[] = { 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, 0x65, 0x74, 0x20, 0x62, 0x65, 0x67, 0x69, 0x6E, 0x3D, 0x22, 0xEF, 0xBB, 0xBF, 0x22, 0x20, 0x69, 0x64, 0x3D, 0x22, 0x57, 0x35, diff --git a/src/data/index_html.h b/src/data/index_html.h index 0dc16a3..bab2b91 100644 --- a/src/data/index_html.h +++ b/src/data/index_html.h @@ -25,7 +25,7 @@ #include "../config.h" -const char *HTML_INDEX_PAGE = " \ +const char HTML_INDEX_PAGE[] = " \ \ \ \ diff --git a/src/device.c b/src/device.c index 2e4c157..2622b9e 100644 --- a/src/device.c +++ b/src/device.c @@ -271,12 +271,10 @@ static int _device_apply_dv_timings(struct device_t *dev) { LOG_DEBUG("Calling ioctl(VIDIOC_QUERY_DV_TIMINGS) ..."); if (xioctl(dev->run->fd, VIDIOC_QUERY_DV_TIMINGS, &dv_timings) == 0) { - LOG_INFO( - "Got new DV timings: resolution=%ux%u; pixclk=%llu", + LOG_INFO("Got new DV timings: resolution=%ux%u; pixclk=%llu", dv_timings.bt.width, dv_timings.bt.height, - dv_timings.bt.pixelclock - ); + dv_timings.bt.pixelclock); LOG_DEBUG("Calling ioctl(VIDIOC_S_DV_TIMINGS) ..."); 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 LOG_DEBUG("Calling ioctl(VIDIOC_S_FMT) ..."); if (xioctl(dev->run->fd, VIDIOC_S_FMT, &fmt) < 0) { - LOG_PERROR( - "Unable to set pixelformat=%s; resolution=%ux%u", + LOG_PERROR("Unable to set pixelformat=%s; resolution=%ux%u", _format_to_string_supported(dev->format), dev->run->width, - dev->run->height - ); + dev->run->height); return -1; } @@ -337,23 +333,15 @@ static int _device_open_format(struct device_t *dev) { char format_obtained_str[8]; char *format_str_nullable; - LOG_ERROR( - "Could not obtain the requested pixelformat=%s; driver gave us %s", + LOG_ERROR("Could not obtain the requested pixelformat=%s; driver gave us %s", _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) { - LOG_INFO( - "Falling back to %s mode (consider using '--format=%s' option)", - format_str_nullable, - format_str_nullable - ); + LOG_INFO("Falling back to pixelformat=%s", format_str_nullable); } else { - LOG_ERROR( - "Unsupported pixelformat=%s (fourcc)", - _format_to_string_fourcc(format_obtained_str, 8, fmt.fmt.pix.pixelformat) - ); + LOG_ERROR("Unsupported pixelformat=%s (fourcc)", + _format_to_string_fourcc(format_obtained_str, 8, fmt.fmt.pix.pixelformat)); return -1; } } diff --git a/src/encoder.c b/src/encoder.c index f77f008..141b11c 100644 --- a/src/encoder.c +++ b/src/encoder.c @@ -83,10 +83,7 @@ void encoder_prepare(struct encoder_t *encoder, struct device_t *dev) { LOG_DEBUG("Preparing OMX JPEG encoder ..."); if (dev->n_workers > OMX_MAX_ENCODERS) { - LOG_INFO( - "OMX-based encoder can only work with %u worker threads; forced --workers=%u", - OMX_MAX_ENCODERS, OMX_MAX_ENCODERS - ); + LOG_INFO("OMX JPEG encoder sets limit for worker threads: %u", OMX_MAX_ENCODERS); dev->n_workers = OMX_MAX_ENCODERS; } encoder->run->n_omxs = dev->n_workers; diff --git a/src/encoders/omx/encoder.h b/src/encoders/omx/encoder.h index 828300f..9719973 100644 --- a/src/encoders/omx/encoder.h +++ b/src/encoders/omx/encoder.h @@ -29,8 +29,9 @@ #include "../../device.h" - -#define OMX_MAX_ENCODERS 3 +#ifndef OMX_MAX_ENCODERS +# define OMX_MAX_ENCODERS 3 // Raspberry Pi limitation +#endif struct omx_encoder_t { diff --git a/src/http.c b/src/http.c index ea70511..a155143 100644 --- a/src/http.c +++ b/src/http.c @@ -407,16 +407,13 @@ static void _http_callback_stream(struct evhttp_request *request, void *v_server server->run->stream_clients_count += 1; evhttp_connection_get_peer(conn, &client_addr, &client_port); - LOG_INFO( - "HTTP: Registered the new stream client: [%s]:%u; id=%s;" - " advance_headers=%s; dual_final_frames=%s; clients now: %u", + LOG_INFO("HTTP: Registered the new stream client: [%s]:%u; id=%s; advance_headers=%s; dual_final_frames=%s; clients now: %u", client_addr, client_port, client->id, bool_to_string(client->advance_headers), bool_to_string(client->dual_final_frames), - server->run->stream_clients_count - ); + server->run->stream_clients_count); buf_event = evhttp_connection_get_bufferevent(conn); 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) { evhttp_connection_get_peer(conn, &client_addr, &client_port); } - LOG_INFO( - "HTTP: Disconnected the stream client: [%s]:%u; clients now: %u", - client_addr, client_port, client->server->run->stream_clients_count - ); + LOG_INFO("HTTP: Disconnected the stream client: [%s]:%u; clients now: %u", + client_addr, client_port, client->server->run->stream_clients_count); if (conn != NULL) { 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_end_time) = EXPOSED(expose_cmp_time); - LOG_VERBOSE( - "HTTP: dropped same frame number %u; comparsion time = %.06Lf", - EXPOSED(dropped), EXPOSED(expose_cmp_time) - EXPOSED(expose_begin_time) - ); + LOG_VERBOSE("HTTP: dropped same frame number %u; comparsion time = %.06Lf", + EXPOSED(dropped), EXPOSED(expose_cmp_time) - EXPOSED(expose_begin_time)); EXPOSED(dropped) += 1; return false; // Not updated } else { EXPOSED(expose_cmp_time) = get_now_monotonic(); - LOG_VERBOSE( - "HTTP: passed same frame check (frames are differ); comparsion time = %.06Lf", - EXPOSED(expose_cmp_time) - EXPOSED(expose_begin_time) - ); + LOG_VERBOSE("HTTP: passed same frame check (frames are differ); comparsion time = %.06Lf", + 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_end_time) = get_now_monotonic(); - LOG_VERBOSE( - "HTTP: exposed new frame; full exposition time = %.06Lf", - EXPOSED(expose_end_time) - EXPOSED(expose_begin_time) - ); + LOG_VERBOSE("HTTP: exposed new frame; full exposition time = %.06Lf", + EXPOSED(expose_end_time) - EXPOSED(expose_begin_time)); # undef EXPOSED # 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) { -# 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_cmp_time) = EXPOSED(expose_begin_time); if (EXPOSED(online) || EXPOSED(picture.size) == 0) { - if (EXPOSED(picture.allocated) < BLANK_JPG_SIZE) { - A_REALLOC(EXPOSED(picture.data), BLANK_JPG_SIZE); - EXPOSED(picture.allocated) = BLANK_JPG_SIZE; + if (EXPOSED(picture.allocated) < BLANK_JPEG_LEN) { + A_REALLOC(EXPOSED(picture.data), BLANK_JPEG_LEN); + EXPOSED(picture.allocated) = BLANK_JPEG_LEN; } - memcpy( - EXPOSED(picture.data), BLANK_JPG_DATA, - BLANK_JPG_SIZE * sizeof(*EXPOSED(picture.data)) - ); + memcpy(EXPOSED(picture.data), BLANK_JPEG_DATA, BLANK_JPEG_LEN * sizeof(*EXPOSED(picture.data))); - EXPOSED(picture.size) = BLANK_JPG_SIZE; + EXPOSED(picture.size) = BLANK_JPEG_LEN; EXPOSED(picture.grab_time) = 0; EXPOSED(picture.encode_begin_time) = 0; EXPOSED(picture.encode_end_time) = 0; - EXPOSED(width) = BLANK_JPG_WIDTH; - EXPOSED(height) = BLANK_JPG_HEIGHT; + EXPOSED(width) = BLANK_JPEG_WIDTH; + EXPOSED(height) = BLANK_JPEG_HEIGHT; EXPOSED(captured_fps) = 0; EXPOSED(online) = false; goto updated; @@ -777,5 +764,6 @@ static bool _expose_blank_picture(struct http_server_t *server) { EXPOSED(expose_end_time) = get_now_monotonic(); return true; // Updated +# undef BLANK_JPEG_LEN # undef EXPOSED } diff --git a/src/stream.c b/src/stream.c index 4e45ac1..d1cbc2f 100644 --- a/src/stream.c +++ b/src/stream.c @@ -302,10 +302,9 @@ static void _stream_expose_picture(struct stream_t *stream, unsigned buf_index) stream->picture.size = PICTURE(size); stream->picture.allocated = PICTURE(allocated); - memcpy( - stream->picture.data, PICTURE(data), - stream->picture.size * sizeof(*stream->picture.data) - ); + + memcpy(stream->picture.data, PICTURE(data), stream->picture.size * sizeof(*stream->picture.data)); + stream->picture.grab_time = PICTURE(grab_time); stream->picture.encode_begin_time = PICTURE(encode_begin_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; A_PTHREAD_M_UNLOCK(ctx->last_comp_time_mutex); - LOG_VERBOSE( - "Compressed JPEG size=%zu; time=%0.3Lf; worker=%u; buffer=%u", - PICTURE(size), last_comp_time, ctx->number, ctx->buf_index - ); + LOG_VERBOSE("Compressed JPEG size=%zu; time=%0.3Lf; worker=%u; buffer=%u", + PICTURE(size), last_comp_time, ctx->number, ctx->buf_index); } else { *ctx->job_failed = true; *ctx->has_job = false; diff --git a/tools/make-html-h.py b/tools/make-html-h.py index 343f26e..72ef4d4 100755 --- a/tools/make-html-h.py +++ b/tools/make-html-h.py @@ -41,7 +41,7 @@ def main(): text = text.replace("%VERSION%", "\" VERSION \"") 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 = "const char *%s = \" \\\n%s\n\";\n" % (name, text) + text = "const char %s[] = \" \\\n%s\n\";\n" % (name, text) text = textwrap.dedent(""" /***************************************************************************** # # diff --git a/tools/make-jpeg-h.py b/tools/make-jpeg-h.py index c2d6fb8..cc6d0fc 100755 --- a/tools/make-jpeg-h.py +++ b/tools/make-jpeg-h.py @@ -35,20 +35,19 @@ def main(): width = int(sys.argv[4]) height = int(sys.argv[5]) - with open(src, "rb") as jpg_file: - jpg_data = jpg_file.read() + with open(src, "rb") as jpeg_file: + jpeg_data = jpeg_file.read() rows = [[]] - for ch in jpg_data: + for ch in jpeg_data: if len(rows[-1]) > 20: rows.append([]) rows[-1].append("0x%.2X" % (ch)) 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 size_t %s_JPG_SIZE = %d;\n\n" % (prefix, len(jpg_data)) + text - text = "const unsigned %s_JPG_HEIGHT = %d;\n\n" % (prefix, height) + text - text = "const unsigned %s_JPG_WIDTH = %d;\n" % (prefix, width) + text + text = "const unsigned char %s_JPEG_DATA[] = {\n\t%s\n};\n" % (prefix, text) + text = "const unsigned %s_JPEG_HEIGHT = %d;\n\n" % (prefix, height) + text + text = "const unsigned %s_JPEG_WIDTH = %d;\n" % (prefix, width) + text text = textwrap.dedent(""" /***************************************************************************** # #