From 795c0176a739d7300b2ecb94c36fd932069d6b3c Mon Sep 17 00:00:00 2001 From: MyungJoo Ham Date: Mon, 25 Mar 2019 15:01:51 +0900 Subject: [PATCH] [Conf] Custom conf keys Any subplugins (/ext/) may use this to make custom configuration keys. To support custom filters, the header should be exported. However, for now, let's forget about the need from custom filters. Signed-off-by: MyungJoo Ham --- gst/nnstreamer/nnstreamer_conf.c | 76 ++++++++++++++++++++++++++++++++++++++++ gst/nnstreamer/nnstreamer_conf.h | 39 +++++++++++++++++++++ 2 files changed, 115 insertions(+) diff --git a/gst/nnstreamer/nnstreamer_conf.c b/gst/nnstreamer/nnstreamer_conf.c index a70ff01..9cde03a 100644 --- a/gst/nnstreamer/nnstreamer_conf.c +++ b/gst/nnstreamer/nnstreamer_conf.c @@ -395,3 +395,79 @@ nnsconf_get_value_bool (nnsconf_type_value type) return ret; } + +/** + * @brief Internal cache for the custom key-values + */ +static GHashTable *custom_table = NULL; + +/** @brief Public function defined in the header */ +gchar * +nnsconf_get_custom_value_string (const gchar * group, const gchar * key) +{ + gchar *hashkey = g_strdup_printf ("[%s]%s", group, key); + gchar *value = NULL; + + nnsconf_loadconf (FALSE); /* Load .ini file path */ + + if (NULL == custom_table) + custom_table = + g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); + + value = g_hash_table_lookup (custom_table, hashkey); + + if (NULL == value) { + gchar *envkey = g_strdup_printf ("NNSTREAMER_%s_%s", group, key); + + /* 1. Read envvar */ + value = _strdup_getenv (envkey); + g_free (envkey); + + /* 2. Read ini */ + if (NULL == value && conf.conffile) { + g_autoptr (GError) error = NULL; + g_autoptr (GKeyFile) key_file = g_key_file_new (); + + if (g_key_file_load_from_file (key_file, conf.conffile, G_KEY_FILE_NONE, + &error)) { + value = g_key_file_get_string (key_file, group, key, &error); + } + + g_key_file_free (key_file); + + } + + if (value) + g_hash_table_insert (custom_table, hashkey, value); + } + + if (NULL == value) + return NULL; + + return g_strdup (value); +} + +/** @brief Public function defined in the header */ +gboolean +nnsconf_get_custom_value_bool (const gchar * group, const gchar * key, + gboolean def) +{ + gchar *strval = nnsconf_get_custom_value_string (group, key); + gboolean ret = def; + + if (NULL == strval) + return ret; + + /** 1/0, true/false, t/f, yes/no, on/off. case incensitive. */ + if (strval[0] == '1' || strval[0] == 't' || strval[0] == 'T' || + strval[0] == 'y' || strval[0] == 'Y' || + !g_ascii_strncasecmp ("on", strval, 2)) + ret = TRUE; + if (strval[0] == '0' || strval[0] == 'f' || strval[0] == 'F' || + strval[0] == 'n' || strval[0] == 'N' || + !g_ascii_strncasecmp ("of", strval, 2)) + ret = FALSE; + + g_free (strval); + return ret; +} diff --git a/gst/nnstreamer/nnstreamer_conf.h b/gst/nnstreamer/nnstreamer_conf.h index 1f27c36..dfabcda 100644 --- a/gst/nnstreamer/nnstreamer_conf.h +++ b/gst/nnstreamer/nnstreamer_conf.h @@ -115,4 +115,43 @@ nnsconf_get_fullpath (const gchar *subpluginname, nnsconf_type_path type); extern const gboolean nnsconf_get_value_bool (nnsconf_type_value type); +/** + * @brief Get the custom configuration value from .ini and envvar. + * @detail For predefined configurations defined in this header, + * use the given enum for faster configuration processing. + * For custom configurations not defined in this header, + * you may use this API to access your own custom configurations. + * Configuration values may be loaded only once during runtime, + * thus, if the values are changed in run-time, the changes are + * not guaranteed to be reflected. + * The ENVVAR is supposed to be NNSTREAMER_${group}_${key}, which + * has higher priority than the .ini configuration. + * Be careful not to use special characters in group name ([, ], _). + * @param[in] group The group name, [group], in .ini file. + * @param[in] key The key name, key = value, in .ini file. + * @return The newly allocated string. A caller must free it. NULL if it's not available. + */ +extern gchar * +nnsconf_get_custom_value_string (const gchar *group, const gchar *key); + +/** + * @brief Get the custom configuration value from .ini and envvar. + * @detail For predefined configurations defined in this header, + * use the given enum for faster configuration processing. + * For custom configurations not defined in this header, + * you may use this API to access your own custom configurations. + * Configuration values may be loaded only once during runtime, + * thus, if the values are changed in run-time, the changes are + * not guaranteed to be reflected. + * The ENVVAR is supposed to be NNSTREAMER_${group}_${key}, which + * has higher priority than the .ini configuration. + * Be careful not to use special characters in group name ([, ], _). + * @param[in] group The group name, [group], in .ini file. + * @param[in] key The key name, key = value, in .ini file. + * @param[in] def The default return value in case there is no value available. + * @return The value interpreted as TRUE/FALSE. + */ +extern gboolean +nnsconf_get_custom_value_bool (const gchar *group, const gchar *key, gboolean def); + #endif /* __GST_NNSTREAMER_CONF_H__ */ -- 2.7.4