Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] parse app context #599

Merged
merged 2 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 0 additions & 31 deletions java/android/nnstreamer/src/main/jni/nnstreamer-android.h

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @bug No known bugs except for NYI items
*/

#include "nnstreamer-native.h"
#include "nnstreamer-native-internal.h"

/**
* @brief Attach thread with Java VM.
Expand Down
152 changes: 137 additions & 15 deletions java/android/nnstreamer/src/main/jni/nnstreamer-native-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,22 @@
* @bug No known bugs except for NYI items
*/

#include "nnstreamer-native.h"
#include "nnstreamer-native-internal.h"

/**
* @brief Global lock for native functions.
*/
G_LOCK_DEFINE_STATIC (nns_native_lock);

/**
* @brief The flag to check initialization of native library.
*/
static gboolean g_nns_is_initialized = FALSE;

/**
* @brief The data path of an application.
*/
static gchar *g_files_dir = NULL;

#if defined(__ANDROID__)
/* nnstreamer plugins and sub-plugins declaration */
Expand Down Expand Up @@ -79,19 +94,27 @@ extern void gst_android_init (JNIEnv * env, jobject context);
#if defined(ENABLE_QNN) || defined(ENABLE_SNPE) || defined(ENABLE_TFLITE_QNN_DELEGATE)
#define ANDROID_QC_ENV 1
#endif
#endif /* __ANDROID__ */

/**
* @brief Global lock for native functions.
* @brief Internal function to check exception.
*/
G_LOCK_DEFINE_STATIC (nns_native_lock);
static gboolean
_env_check_exception (JNIEnv * env) {
if ((*env)->ExceptionCheck (env)) {
(*env)->ExceptionDescribe (env);
(*env)->ExceptionClear (env);
return TRUE;
}

return FALSE;
}

#if defined(ANDROID_QC_ENV)
/**
* @brief Set additional environment (ADSP_LIBRARY_PATH) for Qualcomm Android.
*/
static gboolean
_qc_android_set_env (JNIEnv *env, jobject context)
_qc_android_set_env (JNIEnv * env, jobject context)
{
gboolean is_done = FALSE;
jclass context_class = NULL;
Expand Down Expand Up @@ -121,9 +144,7 @@ _qc_android_set_env (JNIEnv *env, jobject context)
}

application_info_object = (*env)->CallObjectMethod (env, context, get_application_info_method_id);
if ((*env)->ExceptionCheck (env)) {
(*env)->ExceptionDescribe (env);
(*env)->ExceptionClear (env);
if (_env_check_exception (env)) {
_ml_loge ("Failed to call method `ApplicationInfo()`.");
goto done;
}
Expand All @@ -149,9 +170,7 @@ _qc_android_set_env (JNIEnv *env, jobject context)
}

native_library_dir_path_str = (*env)->GetStringUTFChars (env, native_library_dir_path, NULL);
if ((*env)->ExceptionCheck (env)) {
(*env)->ExceptionDescribe (env);
(*env)->ExceptionClear (env);
if (_env_check_exception (env)) {
_ml_loge ("Failed to get string `nativeLibraryDir`");
goto done;
}
Expand Down Expand Up @@ -193,14 +212,95 @@ _qc_android_set_env (JNIEnv *env, jobject context)
}
#endif /* ANDROID_QC_ENV */

/**
* @brief Get additional data from application context.
*/
static gboolean
_load_app_context (JNIEnv * env, jobject context)
{
jclass cls_context = NULL;
jclass cls_file = NULL;
jmethodID mid_get_files_dir = NULL;
jmethodID mid_get_absolute_path = NULL;
jobject dir;

g_return_val_if_fail (env, FALSE);
g_return_val_if_fail (context, FALSE);

/* Clear old value. */
g_free (g_files_dir);
g_files_dir = NULL;

cls_context = (*env)->GetObjectClass (env, context);
if (!cls_context) {
goto error;
}

mid_get_files_dir = (*env)->GetMethodID (env, cls_context, "getFilesDir", "()Ljava/io/File;");
if (!mid_get_files_dir) {
goto error;
}

cls_file = (*env)->FindClass (env, "java/io/File");
if (!cls_file) {
goto error;
}

mid_get_absolute_path = (*env)->GetMethodID (env, cls_file, "getAbsolutePath", "()Ljava/lang/String;");
if (!mid_get_absolute_path) {
goto error;
}

/* Get files directory. */
dir = (*env)->CallObjectMethod (env, context, mid_get_files_dir);
if (_env_check_exception (env)) {
goto error;
}

if (dir) {
jstring abs_path;
const gchar *abs_path_str;

abs_path = (*env)->CallObjectMethod (env, dir, mid_get_absolute_path);
if (_env_check_exception (env)) {
(*env)->DeleteLocalRef (env, dir);
goto error;
}

abs_path_str = (*env)->GetStringUTFChars (env, abs_path, NULL);
if (_env_check_exception (env)) {
(*env)->DeleteLocalRef (env, abs_path);
(*env)->DeleteLocalRef (env, dir);
goto error;
}

g_files_dir = g_strdup (abs_path_str);

(*env)->ReleaseStringUTFChars (env, abs_path, abs_path_str);
(*env)->DeleteLocalRef (env, abs_path);
(*env)->DeleteLocalRef (env, dir);
}

error:
if (cls_context) {
(*env)->DeleteLocalRef (env, cls_context);
}

if (cls_file) {
(*env)->DeleteLocalRef (env, cls_file);
}

return (g_files_dir != NULL);
}
#endif /* __ANDROID__ */

/**
* @brief Initialize NNStreamer, register required plugins.
*/
jboolean
nnstreamer_native_initialize (JNIEnv * env, jobject context)
{
gchar *gst_ver, *nns_ver;
static gboolean nns_is_initilaized = FALSE;

_ml_logi ("Called native initialize.");

Expand All @@ -227,7 +327,7 @@ nnstreamer_native_initialize (JNIEnv * env, jobject context)
}
#endif

if (nns_is_initilaized == FALSE) {
if (g_nns_is_initialized == FALSE) {
#if defined(__ANDROID__)
/* register nnstreamer plugins */
#if !defined(NNS_SINGLE_ONLY)
Expand Down Expand Up @@ -302,8 +402,14 @@ nnstreamer_native_initialize (JNIEnv * env, jobject context)
#if defined(ENABLE_LLAMACPP)
init_filter_llamacpp ();
#endif

if (!_load_app_context (env, context)) {
_ml_loge ("Cannot load application context.");
goto done;
}
#endif /* __ANDROID__ */
nns_is_initilaized = TRUE;

g_nns_is_initialized = TRUE;
}

/* print version info */
Expand All @@ -318,5 +424,21 @@ nnstreamer_native_initialize (JNIEnv * env, jobject context)

done:
G_UNLOCK (nns_native_lock);
return (nns_is_initilaized == TRUE);
return (g_nns_is_initialized == TRUE);
}

/**
* @brief Get the data path of an application, extracted using getFilesDir() for Android.
*/
const char *
nnstreamer_native_get_data_path (void)
{
char *data_path = NULL;

G_LOCK (nns_native_lock);
g_assert (g_nns_is_initialized);
data_path = g_files_dir;
G_UNLOCK (nns_native_lock);

return data_path;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @bug No known bugs except for NYI items
*/

#include "nnstreamer-native.h"
#include "nnstreamer-native-internal.h"

/**
* @brief Private data for CustomFilter class.
Expand Down
Loading
Loading