From 619389970a7f7b09f4abe94e3d027f4f4c9b917d Mon Sep 17 00:00:00 2001 From: Devaev Maxim Date: Sun, 3 Jan 2021 10:22:29 +0300 Subject: [PATCH] initializing global object in main() --- src/libs/h264/encoder.c | 30 ++++++++----------------- src/libs/h264/encoder.h | 2 -- src/recorder/main.c | 4 ++++ src/ustreamer/encoders/omx/encoder.c | 26 ---------------------- src/ustreamer/encoders/omx/encoder.h | 1 - src/ustreamer/main.c | 33 ++++++++++++++++++++++++++++ 6 files changed, 46 insertions(+), 50 deletions(-) diff --git a/src/libs/h264/encoder.c b/src/libs/h264/encoder.c index b98ae77..d9e7f52 100644 --- a/src/libs/h264/encoder.c +++ b/src/libs/h264/encoder.c @@ -57,9 +57,6 @@ void h264_encoder_destroy(h264_encoder_s *encoder) { if (RUN(i_handler_sem)) { vcos_semaphore_delete(&RUN(handler_sem)); } - if (RUN(i_bcm_host)) { - bcm_host_deinit(); - } frame_destroy(RUN(tmp)); free(encoder); } @@ -70,14 +67,7 @@ int h264_encoder_compress(h264_encoder_s *encoder, const frame_s *src, frame_s * assert(src->height > 0); assert(src->format > 0); - if (!RUN(i_bcm_host)) { - LOG_INFO("Initializing BCM ..."); - bcm_host_init(); - RUN(i_bcm_host) = true; - } - if (!RUN(i_handler_sem)) { - LOG_INFO("Creating VCOS semaphore ..."); if (vcos_semaphore_create(&RUN(handler_sem), "h264_handler_sem", 0) != VCOS_SUCCESS) { LOG_PERROR("Can't create VCOS semaphore"); return -1; @@ -156,18 +146,16 @@ static int _h264_encoder_configure(h264_encoder_s *encoder, const frame_s *frame # define IFMT(_next) RUN(input_port->format->_next) IFMT(type) = MMAL_ES_TYPE_VIDEO; - if (frame->format == V4L2_PIX_FMT_UYVY) { - LOG_INFO("Using pixelformat: UYVY"); - IFMT(encoding) = MMAL_ENCODING_UYVY; - } else if (frame->format == V4L2_PIX_FMT_RGB24) { - LOG_INFO("Using pixelformat: RGB24"); - IFMT(encoding) = MMAL_ENCODING_RGB24; - } else { - char fourcc_buf[8]; - LOG_ERROR("Unsupported pixelformat (fourcc): %s", fourcc_to_string(frame->format, fourcc_buf, 8)); - goto error; + switch (frame->format) { + case V4L2_PIX_FMT_YUYV: IFMT(encoding) = MMAL_ENCODING_YUYV; break; + case V4L2_PIX_FMT_UYVY: IFMT(encoding) = MMAL_ENCODING_UYVY; break; + case V4L2_PIX_FMT_RGB565: IFMT(encoding) = MMAL_ENCODING_RGB16; break; + case V4L2_PIX_FMT_RGB24: IFMT(encoding) = MMAL_ENCODING_RGB24; break; + default: + char fourcc_buf[8]; + LOG_ERROR("Unsupported input format for MMAL (fourcc): %s", fourcc_to_string(frame->format, fourcc_buf, 8)); + goto error; } - LOG_INFO("Using resolution: %ux%u", frame->width, frame->height); IFMT(es->video.width) = align_size(frame->width, 32); IFMT(es->video.height) = align_size(frame->height, 16); IFMT(es->video.crop.x) = 0; diff --git a/src/libs/h264/encoder.h b/src/libs/h264/encoder.h index 32175d1..a8eff15 100644 --- a/src/libs/h264/encoder.h +++ b/src/libs/h264/encoder.h @@ -28,7 +28,6 @@ #include -#include #include #include #include @@ -46,7 +45,6 @@ typedef struct { MMAL_PORT_T *input_port; MMAL_PORT_T *output_port; VCOS_SEMAPHORE_T handler_sem; - bool i_bcm_host; bool i_handler_sem; unsigned width; diff --git a/src/recorder/main.c b/src/recorder/main.c index f04e786..6004676 100644 --- a/src/recorder/main.c +++ b/src/recorder/main.c @@ -1,6 +1,8 @@ #include #include +#include + #include "../common/logging.h" #include "../common/frame.h" #include "../memsink/memsink.h" @@ -10,6 +12,8 @@ int main(void) { LOGGING_INIT; log_level = 3; + bcm_host_init(); + frame_s *src = frame_init("src"); frame_s *dest = frame_init("dest"); h264_encoder_s *encoder = h264_encoder_init(); diff --git a/src/ustreamer/encoders/omx/encoder.c b/src/ustreamer/encoders/omx/encoder.c index 0ecfe12..29e56e8 100644 --- a/src/ustreamer/encoders/omx/encoder.c +++ b/src/ustreamer/encoders/omx/encoder.c @@ -27,9 +27,6 @@ static const OMX_U32 _INPUT_PORT = 340; static const OMX_U32 _OUTPUT_PORT = 341; -static int _i_omx = 0; - - static int _vcos_semwait(VCOS_SEMAPHORE_T *sem); static int _omx_init_component(omx_encoder_s *omx); static int _omx_init_disable_ports(omx_encoder_s *omx); @@ -65,19 +62,6 @@ omx_encoder_s *omx_encoder_init(void) { omx_encoder_s *omx; A_CALLOC(omx, 1); - assert(_i_omx >= 0); - if (_i_omx == 0) { - LOG_INFO("Initializing BCM ..."); - bcm_host_init(); - - LOG_INFO("Initializing OMX ..."); - if ((OMX_ERRORTYPE error = OMX_Init()) != OMX_ErrorNone) { - LOG_ERROR_OMX(error, "Can't initialize OMX"); - goto error; - } - } - _i_omx += 1; - LOG_INFO("Initializing OMX encoder ..."); if (vcos_semaphore_create(&omx->handler_sem, "handler_sem", 0) != VCOS_SUCCESS) { @@ -118,16 +102,6 @@ void omx_encoder_destroy(omx_encoder_s *omx) { } } - assert(_i_omx >= 0); - _i_omx -= 1; - if (_i_omx == 0) { - LOG_INFO("Destroying OMX ..."); - OMX_Deinit(); - - LOG_INFO("Destroying BCM ..."); - bcm_host_deinit(); - } - free(omx); } diff --git a/src/ustreamer/encoders/omx/encoder.h b/src/ustreamer/encoders/omx/encoder.h index bcf1087..6c6b9d7 100644 --- a/src/ustreamer/encoders/omx/encoder.h +++ b/src/ustreamer/encoders/omx/encoder.h @@ -30,7 +30,6 @@ #include -#include #include #include #include diff --git a/src/ustreamer/main.c b/src/ustreamer/main.c index 31d8c00..2fc8b2b 100644 --- a/src/ustreamer/main.c +++ b/src/ustreamer/main.c @@ -35,6 +35,10 @@ #include #include +#ifdef WITH_OMX +# include +# include +#endif #include "../libs/common/tools.h" #include "../libs/common/threading.h" @@ -123,6 +127,25 @@ int main(int argc, char *argv[]) { int exit_code = 0; if ((exit_code = options_parse(options, dev, encoder, stream, server)) == 0) { +# ifdef WITH_OMX + bool i_bcm_host = false; + if (encoder->type == ENCODER_TYPE_OMX) { + LOG_INFO("Initializing BCM Host ..."); + bcm_host_init(); + i_bcm_host = true; + } + bool i_omx_core = false + if (encodeer->type == ENCODER_TYPE_OMX) { + LOG_INFO("Initializing OMX Core ..."); + if ((OMX_ERRORTYPE error = OMX_Init()) != OMX_ErrorNone) { + LOG_ERROR_OMX(error, "Can't initialize OMX Core"); + exit_code = -1; + goto omx_error; + } + i_omx_core = true; + } +# endif + # ifdef WITH_GPIO gpio_init(); # endif @@ -151,6 +174,16 @@ int main(int argc, char *argv[]) { gpio_set_prog_running(false); gpio_destroy(); # endif + +# ifdef WITH_OMX + omx_error: + if (i_omx_core) { + OMX_Deinit(); + } + if (i_bcm_host) { + bcm_host_deinit(); + } +# endif } server_destroy(server);