config structure

This commit is contained in:
Maxim Devaev
2022-07-16 11:27:27 +03:00
parent 2f86f818cc
commit 28f13f7514
4 changed files with 51 additions and 43 deletions

View File

@@ -23,35 +23,36 @@
#include "config.h" #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) { plugin_config_s *plugin_config_init(const char *config_dir_path) {
int retval = 0; plugin_config_s *config;
A_CALLOC(config, 1);
char *config_file_path; 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); A_ASPRINTF(config_file_path, "%s/%s.jcfg", config_dir_path, PLUGIN_PACKAGE);
JLOG_INFO("config", "Reading config file '%s' ...", config_file_path); JLOG_INFO("config", "Reading config file '%s' ...", config_file_path);
config = janus_config_parse(config_file_path); jcfg = janus_config_parse(config_file_path);
if (config == NULL) { if (jcfg == NULL) {
JLOG_ERROR("config", "Can't read config"); JLOG_ERROR("config", "Can't read config");
goto error; goto error;
} }
janus_config_print(config); janus_config_print(jcfg);
if ( if (
(*video_sink_name = _get_value(config, "memsink", "object")) == NULL (config->video_sink_name = _get_value(jcfg, "memsink", "object")) == NULL
&& (*video_sink_name = _get_value(config, "video", "sink")) == NULL && (config->video_sink_name = _get_value(jcfg, "video", "sink")) == NULL
) { ) {
JLOG_ERROR("config", "Missing config value: video.sink (ex. memsink.object)"); JLOG_ERROR("config", "Missing config value: video.sink (ex. memsink.object)");
goto error; 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"); 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"); JLOG_INFO("config", "Missing config value: audio.tc358743");
goto error; goto error;
} }
@@ -59,18 +60,26 @@ int read_config(const char *config_dir_path, char **video_sink_name, char **audi
goto ok; goto ok;
error: error:
retval = -1; plugin_config_destroy(config);
config = NULL;
ok: ok:
if (config) { if (jcfg) {
janus_config_destroy(config); janus_config_destroy(jcfg);
} }
free(config_file_path); free(config_file_path);
return retval; return config;
} }
static char *_get_value(janus_config *config, const char *section, const char *option) { void plugin_config_destroy(plugin_config_s *config) {
janus_config_category *section_obj = janus_config_get_create(config, NULL, janus_config_type_category, section); DELETE(config->video_sink_name, free);
janus_config_item *option_obj = janus_config_get(config, section_obj, janus_config_type_item, option); 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') { if (option_obj == NULL || option_obj->value == NULL || option_obj->value[0] == '\0') {
return NULL; return NULL;
} }

View File

@@ -34,4 +34,13 @@
#include "logging.h" #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);

View File

@@ -55,10 +55,7 @@
#include "config.h" #include "config.h"
static char *_g_video_sink_name = NULL; static plugin_config_s *_g_config = NULL;
static char *_g_audio_dev_name = NULL;
static char *_g_tc358743_dev_path = NULL;
const useconds_t _g_watchers_polling = 100000; const useconds_t _g_watchers_polling = 100000;
static client_s *_g_clients = NULL; static client_s *_g_clients = NULL;
@@ -136,7 +133,7 @@ static void *_video_sink_thread(UNUSED void *arg) {
int fd = -1; int fd = -1;
memsink_shared_s *mem = NULL; 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"); }); IF_NOT_REPORTED({ JLOG_PERROR("video", "Can't open memsink"); });
goto close_memsink; goto close_memsink;
} }
@@ -183,8 +180,8 @@ static void *_video_sink_thread(UNUSED void *arg) {
static void *_audio_thread(UNUSED void *arg) { static void *_audio_thread(UNUSED void *arg) {
A_THREAD_RENAME("us_audio"); A_THREAD_RENAME("us_audio");
atomic_store(&_g_audio_tid_created, true); atomic_store(&_g_audio_tid_created, true);
assert(_g_audio_dev_name); assert(_g_config->audio_dev_name);
assert(_g_tc358743_dev_path); assert(_g_config->tc358743_dev_path);
unsigned error_reported = 0; unsigned error_reported = 0;
@@ -197,7 +194,7 @@ static void *_audio_thread(UNUSED void *arg) {
tc358743_info_s info = {0}; tc358743_info_s info = {0};
audio_s *audio = NULL; 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; goto close_audio;
} }
if (!info.has_audio) { if (!info.has_audio) {
@@ -205,7 +202,7 @@ static void *_audio_thread(UNUSED void *arg) {
goto close_audio; goto close_audio;
} }
IF_NOT_REPORTED({ JLOG_INFO("audio", "Detected host 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; goto close_audio;
} }
@@ -213,7 +210,7 @@ static void *_audio_thread(UNUSED void *arg) {
while (!STOP && HAS_WATCHERS) { while (!STOP && HAS_WATCHERS) {
if ( if (
tc358743_read_info(_g_tc358743_dev_path, &info) < 0 tc358743_read_info(_g_config->tc358743_dev_path, &info) < 0
|| !info.has_audio || !info.has_audio
|| audio->pcm_hz != info.audio_hz || 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 // sysctl -w net.core.wmem_max=1000000
JLOG_INFO("main", "Initializing plugin ..."); JLOG_INFO("main", "Initializing plugin ...");
if (gw == NULL || config_dir_path == NULL || read_config(config_dir_path, if (gw == NULL || config_dir_path == NULL || ((_g_config = plugin_config_init(config_dir_path)) == NULL)) {
&_g_video_sink_name,
&_g_audio_dev_name,
&_g_tc358743_dev_path
) < 0) {
return -1; return -1;
} }
_g_gw = gw; _g_gw = gw;
_g_video_queue = queue_init(1024); _g_video_queue = queue_init(1024);
_g_rtpv = rtpv_init(_relay_rtp_clients); _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); _g_rtpa = rtpa_init(_relay_rtp_clients);
A_THREAD_CREATE(&_g_audio_tid, _audio_thread, NULL); 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); QUEUE_FREE_ITEMS_AND_DESTROY(_g_video_queue, frame_destroy);
# define DEL(_func, _var) { if (_var) { _func(_var); } } DELETE(_g_rtpa, rtpa_destroy);
DEL(rtpa_destroy, _g_rtpa); DELETE(_g_rtpv, rtpv_destroy);
DEL(rtpv_destroy, _g_rtpv); DELETE(_g_config, plugin_config_destroy);
DEL(free, _g_tc358743_dev_path);
DEL(free, _g_audio_dev_name);
DEL(free, _g_video_sink_name);
# undef DEL
} }
#define IF_DISABLED(...) { if (!READY || STOP) { __VA_ARGS__ } } #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; }); IF_DISABLED({ *err = -1; return; });
LOCK_ALL; LOCK_ALL;
JLOG_INFO("main", "Creating session %p ...", session); 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); LIST_APPEND(_g_clients, client);
atomic_store(&_g_has_watchers, true); atomic_store(&_g_has_watchers, true);
UNLOCK_ALL; UNLOCK_ALL;

View File

@@ -55,6 +55,7 @@
#define A_CALLOC(_dest, _nmemb) assert((_dest = calloc(_nmemb, sizeof(*(_dest))))) #define A_CALLOC(_dest, _nmemb) assert((_dest = calloc(_nmemb, sizeof(*(_dest)))))
#define A_REALLOC(_dest, _nmemb) assert((_dest = realloc(_dest, _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 MEMSET_ZERO(_obj) memset(&(_obj), 0, sizeof(_obj))
#define A_ASPRINTF(_dest, _fmt, ...) assert(asprintf(&(_dest), _fmt, ##__VA_ARGS__) >= 0) #define A_ASPRINTF(_dest, _fmt, ...) assert(asprintf(&(_dest), _fmt, ##__VA_ARGS__) >= 0)