diff --git a/janus/src/audio.h b/janus/src/audio.h index d8c478d..e9e114f 100644 --- a/janus/src/audio.h +++ b/janus/src/audio.h @@ -36,6 +36,7 @@ #include #include "uslibs/tools.h" +#include "uslibs/array.h" #include "uslibs/threading.h" #include "logging.h" diff --git a/janus/src/uslibs/array.h b/janus/src/uslibs/array.h new file mode 120000 index 0000000..87ed614 --- /dev/null +++ b/janus/src/uslibs/array.h @@ -0,0 +1 @@ +../../../src/libs/array.h \ No newline at end of file diff --git a/src/libs/array.h b/src/libs/array.h new file mode 100644 index 0000000..4f10a65 --- /dev/null +++ b/src/libs/array.h @@ -0,0 +1,37 @@ +/***************************************************************************** +# # +# uStreamer - Lightweight and fast MJPEG-HTTP streamer. # +# # +# Copyright (C) 2018-2022 Maxim Devaev # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +*****************************************************************************/ + + +#pragma once + +#include + + +#define US_ARRAY_LEN(x_array) (sizeof(x_array) / sizeof((x_array)[0])) + +#define US_ARRAY_ITERATE(x_array, x_start, x_item_ptr, ...) { \ + const int m_len = US_ARRAY_LEN(x_array); \ + assert(x_start <= m_len); \ + for (int m_index = x_start; m_index < m_len; ++m_index) { \ + __typeof__((x_array)[0]) *const x_item_ptr = &x_array[m_index]; \ + __VA_ARGS__ \ + } \ + } diff --git a/src/libs/tools.h b/src/libs/tools.h index c071ad3..c543947 100644 --- a/src/libs/tools.h +++ b/src/libs/tools.h @@ -60,8 +60,6 @@ #define US_ASPRINTF(x_dest, x_fmt, ...) assert(asprintf(&(x_dest), (x_fmt), ##__VA_ARGS__) >= 0) -#define US_ARRAY_LEN(x_array) (sizeof(x_array) / sizeof((x_array)[0])) - INLINE char *us_strdup(const char *str) { char *const new = strdup(str); diff --git a/src/ustreamer/device.c b/src/ustreamer/device.c index 2847c39..d737305 100644 --- a/src/ustreamer/device.c +++ b/src/ustreamer/device.c @@ -111,29 +111,29 @@ void us_device_destroy(us_device_s *dev) { } int us_device_parse_format(const char *str) { - for (unsigned index = 0; index < US_ARRAY_LEN(_FORMATS); ++index) { - if (!strcasecmp(str, _FORMATS[index].name)) { - return _FORMATS[index].format; + US_ARRAY_ITERATE(_FORMATS, 0, item, { + if (!strcasecmp(item->name, str)) { + return item->format; } - } + }); return US_FORMAT_UNKNOWN; } v4l2_std_id us_device_parse_standard(const char *str) { - for (unsigned index = 1; index < US_ARRAY_LEN(_STANDARDS); ++index) { - if (!strcasecmp(str, _STANDARDS[index].name)) { - return _STANDARDS[index].standard; + US_ARRAY_ITERATE(_STANDARDS, 1, item, { + if (!strcasecmp(item->name, str)) { + return item->standard; } - } + }); return US_STANDARD_UNKNOWN; } int us_device_parse_io_method(const char *str) { - for (unsigned index = 0; index < US_ARRAY_LEN(_IO_METHODS); ++index) { - if (!strcasecmp(str, _IO_METHODS[index].name)) { - return _IO_METHODS[index].io_method; + US_ARRAY_ITERATE(_IO_METHODS, 0, item, { + if (!strcasecmp(item->name, str)) { + return item->io_method; } - } + }); return US_IO_METHOD_UNKNOWN; } @@ -868,11 +868,11 @@ static void _device_set_control( } static const char *_format_to_string_nullable(unsigned format) { - for (unsigned index = 0; index < US_ARRAY_LEN(_FORMATS); ++index) { - if (format == _FORMATS[index].format) { - return _FORMATS[index].name; + US_ARRAY_ITERATE(_FORMATS, 0, item, { + if (item->format == format) { + return item->name; } - } + }); return NULL; } @@ -882,19 +882,19 @@ static const char *_format_to_string_supported(unsigned format) { } static const char *_standard_to_string(v4l2_std_id standard) { - for (unsigned index = 0; index < US_ARRAY_LEN(_STANDARDS); ++index) { - if (standard == _STANDARDS[index].standard) { - return _STANDARDS[index].name; + US_ARRAY_ITERATE(_STANDARDS, 0, item, { + if (item->standard == standard) { + return item->name; } - } + }); return _STANDARDS[0].name; } static const char *_io_method_to_string_supported(enum v4l2_memory io_method) { - for (unsigned index = 0; index < US_ARRAY_LEN(_IO_METHODS); ++index) { - if (io_method == _IO_METHODS[index].io_method) { - return _IO_METHODS[index].name; + US_ARRAY_ITERATE(_IO_METHODS, 0, item, { + if (item->io_method == io_method) { + return item->name; } - } + }); return "unsupported"; } diff --git a/src/ustreamer/device.h b/src/ustreamer/device.h index 244bcb5..252ba23 100644 --- a/src/ustreamer/device.h +++ b/src/ustreamer/device.h @@ -42,6 +42,7 @@ #include #include "../libs/tools.h" +#include "../libs/array.h" #include "../libs/logging.h" #include "../libs/threading.h" #include "../libs/frame.h" diff --git a/src/ustreamer/encoder.c b/src/ustreamer/encoder.c index d2931aa..681ed37 100644 --- a/src/ustreamer/encoder.c +++ b/src/ustreamer/encoder.c @@ -74,20 +74,20 @@ void us_encoder_destroy(us_encoder_s *enc) { } us_encoder_type_e us_encoder_parse_type(const char *str) { - for (unsigned index = 0; index < US_ARRAY_LEN(_ENCODER_TYPES); ++index) { - if (!strcasecmp(str, _ENCODER_TYPES[index].name)) { - return _ENCODER_TYPES[index].type; + US_ARRAY_ITERATE(_ENCODER_TYPES, 0, item, { + if (!strcasecmp(item->name, str)) { + return item->type; } - } + }); return US_ENCODER_TYPE_UNKNOWN; } const char *us_encoder_type_to_string(us_encoder_type_e type) { - for (unsigned index = 0; index < US_ARRAY_LEN(_ENCODER_TYPES); ++index) { - if (_ENCODER_TYPES[index].type == type) { - return _ENCODER_TYPES[index].name; + US_ARRAY_ITERATE(_ENCODER_TYPES, 0, item, { + if (item->type == type) { + return item->name; } - } + }); return _ENCODER_TYPES[0].name; } diff --git a/src/ustreamer/encoder.h b/src/ustreamer/encoder.h index 41268e7..4750520 100644 --- a/src/ustreamer/encoder.h +++ b/src/ustreamer/encoder.h @@ -31,6 +31,7 @@ #include #include "../libs/tools.h" +#include "../libs/array.h" #include "../libs/threading.h" #include "../libs/logging.h" #include "../libs/frame.h" diff --git a/src/ustreamer/http/mime.c b/src/ustreamer/http/mime.c index f38e255..542eff8 100644 --- a/src/ustreamer/http/mime.c +++ b/src/ustreamer/http/mime.c @@ -54,11 +54,11 @@ const char *us_guess_mime_type(const char *path) { } const char *ext = dot + 1; - for (unsigned index = 0; index < US_ARRAY_LEN(_MIME_TYPES); ++index) { - if (!evutil_ascii_strcasecmp(ext, _MIME_TYPES[index].ext)) { - return _MIME_TYPES[index].mime; + US_ARRAY_ITERATE(_MIME_TYPES, 0, item, { + if (!evutil_ascii_strcasecmp(item->ext, ext)) { + return item->mime; } - } + }); misc: return "application/misc"; diff --git a/src/ustreamer/http/mime.h b/src/ustreamer/http/mime.h index 60cf61e..4a708a3 100644 --- a/src/ustreamer/http/mime.h +++ b/src/ustreamer/http/mime.h @@ -27,6 +27,7 @@ #include #include "../../libs/tools.h" +#include "../../libs/array.h" const char *us_guess_mime_type(const char *str);