From 28f13f75140878bf192165023bf3968fa4bb099d Mon Sep 17 00:00:00 2001 From: Maxim Devaev Date: Sat, 16 Jul 2022 11:27:27 +0300 Subject: [PATCH] config structure --- janus/src/config.c | 45 +++++++++++++++++++++++++++------------------ janus/src/config.h | 11 ++++++++++- janus/src/plugin.c | 37 +++++++++++++------------------------ src/libs/tools.h | 1 + 4 files changed, 51 insertions(+), 43 deletions(-) diff --git a/janus/src/config.c b/janus/src/config.c index d6057c4..1bfef2d 100644 --- a/janus/src/config.c +++ b/janus/src/config.c @@ -23,35 +23,36 @@ #include "config.h" -static char *_get_value(janus_config *config, const char *section, const char *option); +static char *_get_value(janus_config *jcfg, const char *section, const char *option); -int read_config(const char *config_dir_path, char **video_sink_name, char **audio_dev_name, char **tc358743_dev_path) { - int retval = 0; +plugin_config_s *plugin_config_init(const char *config_dir_path) { + plugin_config_s *config; + A_CALLOC(config, 1); char *config_file_path; - janus_config *config = NULL; + janus_config *jcfg = NULL; A_ASPRINTF(config_file_path, "%s/%s.jcfg", config_dir_path, PLUGIN_PACKAGE); JLOG_INFO("config", "Reading config file '%s' ...", config_file_path); - config = janus_config_parse(config_file_path); - if (config == NULL) { + jcfg = janus_config_parse(config_file_path); + if (jcfg == NULL) { JLOG_ERROR("config", "Can't read config"); goto error; } - janus_config_print(config); + janus_config_print(jcfg); if ( - (*video_sink_name = _get_value(config, "memsink", "object")) == NULL - && (*video_sink_name = _get_value(config, "video", "sink")) == NULL + (config->video_sink_name = _get_value(jcfg, "memsink", "object")) == NULL + && (config->video_sink_name = _get_value(jcfg, "video", "sink")) == NULL ) { JLOG_ERROR("config", "Missing config value: video.sink (ex. memsink.object)"); goto error; } - if ((*audio_dev_name = _get_value(config, "audio", "device")) != NULL) { + if ((config->audio_dev_name = _get_value(jcfg, "audio", "device")) != NULL) { JLOG_INFO("config", "Enabled the experimental AUDIO feature"); - if ((*tc358743_dev_path = _get_value(config, "audio", "tc358743")) == NULL) { + if ((config->tc358743_dev_path = _get_value(jcfg, "audio", "tc358743")) == NULL) { JLOG_INFO("config", "Missing config value: audio.tc358743"); goto error; } @@ -59,18 +60,26 @@ int read_config(const char *config_dir_path, char **video_sink_name, char **audi goto ok; error: - retval = -1; + plugin_config_destroy(config); + config = NULL; ok: - if (config) { - janus_config_destroy(config); + if (jcfg) { + janus_config_destroy(jcfg); } free(config_file_path); - return retval; + return config; } -static char *_get_value(janus_config *config, const char *section, const char *option) { - janus_config_category *section_obj = janus_config_get_create(config, NULL, janus_config_type_category, section); - janus_config_item *option_obj = janus_config_get(config, section_obj, janus_config_type_item, option); +void plugin_config_destroy(plugin_config_s *config) { + DELETE(config->video_sink_name, free); + DELETE(config->audio_dev_name, free); + DELETE(config->tc358743_dev_path, free); + free(config); +} + +static char *_get_value(janus_config *jcfg, const char *section, const char *option) { + janus_config_category *section_obj = janus_config_get_create(jcfg, NULL, janus_config_type_category, section); + janus_config_item *option_obj = janus_config_get(jcfg, section_obj, janus_config_type_item, option); if (option_obj == NULL || option_obj->value == NULL || option_obj->value[0] == '\0') { return NULL; } diff --git a/janus/src/config.h b/janus/src/config.h index a01549c..69668f8 100644 --- a/janus/src/config.h +++ b/janus/src/config.h @@ -34,4 +34,13 @@ #include "logging.h" -int read_config(const char *config_dir_path, char **video_sink_name, char **audio_dev_name, char **tc358743_dev_path); +typedef struct { + char *video_sink_name; + char *audio_dev_name; + char *tc358743_dev_path; +} plugin_config_s; + + +// config_init() conflicts with something +plugin_config_s *plugin_config_init(const char *config_dir_path); +void plugin_config_destroy(plugin_config_s *config); diff --git a/janus/src/plugin.c b/janus/src/plugin.c index aed5edc..acfe6f7 100644 --- a/janus/src/plugin.c +++ b/janus/src/plugin.c @@ -55,10 +55,7 @@ #include "config.h" -static char *_g_video_sink_name = NULL; -static char *_g_audio_dev_name = NULL; -static char *_g_tc358743_dev_path = NULL; - +static plugin_config_s *_g_config = NULL; const useconds_t _g_watchers_polling = 100000; static client_s *_g_clients = NULL; @@ -136,7 +133,7 @@ static void *_video_sink_thread(UNUSED void *arg) { int fd = -1; memsink_shared_s *mem = NULL; - if ((fd = shm_open(_g_video_sink_name, O_RDWR, 0)) <= 0) { + if ((fd = shm_open(_g_config->video_sink_name, O_RDWR, 0)) <= 0) { IF_NOT_REPORTED({ JLOG_PERROR("video", "Can't open memsink"); }); goto close_memsink; } @@ -183,8 +180,8 @@ static void *_video_sink_thread(UNUSED void *arg) { static void *_audio_thread(UNUSED void *arg) { A_THREAD_RENAME("us_audio"); atomic_store(&_g_audio_tid_created, true); - assert(_g_audio_dev_name); - assert(_g_tc358743_dev_path); + assert(_g_config->audio_dev_name); + assert(_g_config->tc358743_dev_path); unsigned error_reported = 0; @@ -197,7 +194,7 @@ static void *_audio_thread(UNUSED void *arg) { tc358743_info_s info = {0}; audio_s *audio = NULL; - if (tc358743_read_info(_g_tc358743_dev_path, &info) < 0) { + if (tc358743_read_info(_g_config->tc358743_dev_path, &info) < 0) { goto close_audio; } if (!info.has_audio) { @@ -205,7 +202,7 @@ static void *_audio_thread(UNUSED void *arg) { goto close_audio; } IF_NOT_REPORTED({ JLOG_INFO("audio", "Detected host audio"); }); - if ((audio = audio_init(_g_audio_dev_name, info.audio_hz)) == NULL) { + if ((audio = audio_init(_g_config->audio_dev_name, info.audio_hz)) == NULL) { goto close_audio; } @@ -213,7 +210,7 @@ static void *_audio_thread(UNUSED void *arg) { while (!STOP && HAS_WATCHERS) { if ( - tc358743_read_info(_g_tc358743_dev_path, &info) < 0 + tc358743_read_info(_g_config->tc358743_dev_path, &info) < 0 || !info.has_audio || audio->pcm_hz != info.audio_hz ) { @@ -258,18 +255,14 @@ static int _plugin_init(janus_callbacks *gw, const char *config_dir_path) { // sysctl -w net.core.wmem_max=1000000 JLOG_INFO("main", "Initializing plugin ..."); - if (gw == NULL || config_dir_path == NULL || read_config(config_dir_path, - &_g_video_sink_name, - &_g_audio_dev_name, - &_g_tc358743_dev_path - ) < 0) { + if (gw == NULL || config_dir_path == NULL || ((_g_config = plugin_config_init(config_dir_path)) == NULL)) { return -1; } _g_gw = gw; _g_video_queue = queue_init(1024); _g_rtpv = rtpv_init(_relay_rtp_clients); - if (_g_audio_dev_name) { + if (_g_config->audio_dev_name) { _g_rtpa = rtpa_init(_relay_rtp_clients); A_THREAD_CREATE(&_g_audio_tid, _audio_thread, NULL); } @@ -297,13 +290,9 @@ static void _plugin_destroy(void) { QUEUE_FREE_ITEMS_AND_DESTROY(_g_video_queue, frame_destroy); -# define DEL(_func, _var) { if (_var) { _func(_var); } } - DEL(rtpa_destroy, _g_rtpa); - DEL(rtpv_destroy, _g_rtpv); - DEL(free, _g_tc358743_dev_path); - DEL(free, _g_audio_dev_name); - DEL(free, _g_video_sink_name); -# undef DEL + DELETE(_g_rtpa, rtpa_destroy); + DELETE(_g_rtpv, rtpv_destroy); + DELETE(_g_config, plugin_config_destroy); } #define IF_DISABLED(...) { if (!READY || STOP) { __VA_ARGS__ } } @@ -312,7 +301,7 @@ static void _plugin_create_session(janus_plugin_session *session, int *err) { IF_DISABLED({ *err = -1; return; }); LOCK_ALL; JLOG_INFO("main", "Creating session %p ...", session); - client_s *client = client_init(_g_gw, session, (_g_audio_dev_name != NULL)); + client_s *client = client_init(_g_gw, session, (_g_config->audio_dev_name != NULL)); LIST_APPEND(_g_clients, client); atomic_store(&_g_has_watchers, true); UNLOCK_ALL; diff --git a/src/libs/tools.h b/src/libs/tools.h index f91f9cf..ec59c8b 100644 --- a/src/libs/tools.h +++ b/src/libs/tools.h @@ -55,6 +55,7 @@ #define A_CALLOC(_dest, _nmemb) assert((_dest = calloc(_nmemb, sizeof(*(_dest))))) #define A_REALLOC(_dest, _nmemb) assert((_dest = realloc(_dest, _nmemb * sizeof(*(_dest))))) +#define DELETE(_dest, _free) { if (_dest) { _free(_dest); } } #define MEMSET_ZERO(_obj) memset(&(_obj), 0, sizeof(_obj)) #define A_ASPRINTF(_dest, _fmt, ...) assert(asprintf(&(_dest), _fmt, ##__VA_ARGS__) >= 0)