From d3e495135a20a4aa13ab427b75b363f334c04c5f Mon Sep 17 00:00:00 2001 From: Jaeyun Date: Thu, 8 Aug 2019 20:57:45 +0900 Subject: [PATCH] [Api/Pipeline] element restriction Handles element restriction (whitelist for API release) If needs restriction in pipeline elements, simply set the list in meson option. e.g. meson -Denable-capi=true -Denable-element-restriction=true -Drestricted-elements='videotestsrc videoconvert' build this allows videotestsrc and videoconvert with nnstreamer elements. Signed-off-by: Jaeyun Jung --- api/capi/src/nnstreamer-capi-util.c | 40 +++++++++++++++++++++++++++++++------ meson.build | 12 +++++++++++ meson_options.txt | 2 ++ nnstreamer.ini.in | 2 ++ 4 files changed, 50 insertions(+), 6 deletions(-) diff --git a/api/capi/src/nnstreamer-capi-util.c b/api/capi/src/nnstreamer-capi-util.c index 3907eba..7dd6d77 100644 --- a/api/capi/src/nnstreamer-capi-util.c +++ b/api/capi/src/nnstreamer-capi-util.c @@ -26,6 +26,7 @@ #include "nnstreamer-capi-private.h" #include "nnstreamer_plugin_api.h" #include "nnstreamer_plugin_api_filter.h" +#include "nnstreamer_conf.h" #if defined(__TIZEN__) #include @@ -932,16 +933,43 @@ ml_set_feature_status (int status) int ml_check_plugin_availability (const char *plugin_name, const char *element_name) { -#ifdef __TIZEN__ + static gboolean list_loaded = FALSE; + static gchar **restricted_elements = NULL; + if (!plugin_name || !element_name) { ml_loge ("The name is invalid, failed to check the availability."); return ML_ERROR_INVALID_PARAMETER; } - /** - * @todo check white-list of available plugins - * e.g, plugin name 'nnstreamer', element name 'tensor_converter' - */ -#endif /* __TIZEN__ */ + if (!list_loaded) { + gboolean restricted; + + restricted = nnsconf_get_custom_value_bool ("element-restriction", "enable_element_restriction", FALSE); + if (restricted) { + gchar *elements; + + /* check white-list of available plugins */ + elements = nnsconf_get_custom_value_string ("element-restriction", "restricted_elements"); + if (elements) { + restricted_elements = g_strsplit_set (elements, " ,;", -1); + g_free (elements); + } + } + + list_loaded = TRUE; + } + + /* nnstreamer elements */ + if (g_str_equal (plugin_name, "nnstreamer") && + g_str_has_prefix (element_name, "tensor_")) { + return ML_ERROR_NONE; + } + + if (restricted_elements && + find_key_strv ((const gchar **) restricted_elements, element_name) < 0) { + ml_logw ("The element %s is restricted.", element_name); + return ML_ERROR_NOT_SUPPORTED; + } + return ML_ERROR_NONE; } diff --git a/meson.build b/meson.build index d7b3dc3..a3defb9 100644 --- a/meson.build +++ b/meson.build @@ -236,6 +236,7 @@ if get_option('enable-test') nnstreamer_test_conf.set('ENABLE_ENV_VAR', true) nnstreamer_test_conf.set('ENABLE_SYMBOLIC_LINK', false) nnstreamer_test_conf.set('TORCH_USE_GPU', false) + nnstreamer_test_conf.set('ELEMENT_RESTRICTION_CONFIG', '') # meson 0.50 supports install argument in configure_file() if get_option('install-test') @@ -258,6 +259,17 @@ nnstreamer_install_conf.set('ENABLE_ENV_VAR', get_option('enable-env-var')) nnstreamer_install_conf.set('ENABLE_SYMBOLIC_LINK', get_option('enable-symbolic-link')) nnstreamer_install_conf.set('TORCH_USE_GPU', get_option('enable-pytorch-use-gpu')) +# Element restriction +restriction_config = '' + +if get_option('enable-element-restriction') + restriction_config = '''[element-restriction] +enable_element_restriction=True +restricted_elements=''' + get_option('restricted-elements') +endif + +nnstreamer_install_conf.set('ELEMENT_RESTRICTION_CONFIG', restriction_config) + # Install .ini configure_file(input: 'nnstreamer.ini.in', output: 'nnstreamer.ini', install_dir: nnstreamer_inidir, diff --git a/meson_options.txt b/meson_options.txt index ebf8129..d842d3f 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -15,3 +15,5 @@ option('enable-symbolic-link', type: 'boolean', value: true) option('enable-capi', type: 'boolean', value: false) option('enable-python', type: 'boolean', value: true) option('enable-tizen', type: 'boolean', value: false) +option('enable-element-restriction', type: 'boolean', value: false) # true to restrict gst-elements in api +option('restricted-elements', type: 'string', value: '') diff --git a/nnstreamer.ini.in b/nnstreamer.ini.in index f613156..c52cea8 100644 --- a/nnstreamer.ini.in +++ b/nnstreamer.ini.in @@ -21,3 +21,5 @@ enable_nnapi=False # Set 1 or True if you want to use GPU with pytorch for computation. [pytorch] enable_use_gpu=@TORCH_USE_GPU@ + +@ELEMENT_RESTRICTION_CONFIG@ -- 2.7.4