From 4520dbadcf2419458d6422a868471ea6aa6af268 Mon Sep 17 00:00:00 2001 From: Yelin Jeong Date: Fri, 19 Apr 2024 13:54:10 +0900 Subject: [PATCH] App/Service: Add nnstreamer to ml_inference_offloading service This patch adds nnstreamer to android service. Signed-off-by: Yelin Jeong --- ml_inference_offloading/build.gradle.kts | 1 + .../ml/inference/offloading/MainService.kt | 28 ++++++++++++++++++- nnstreamer-api/build.gradle.kts | 22 +++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/ml_inference_offloading/build.gradle.kts b/ml_inference_offloading/build.gradle.kts index 91d01d6..c5de3c5 100644 --- a/ml_inference_offloading/build.gradle.kts +++ b/ml_inference_offloading/build.gradle.kts @@ -58,6 +58,7 @@ dependencies { implementation(libs.androidx.ui.graphics) implementation(libs.androidx.ui.tooling.preview) implementation(libs.androidx.material3) + implementation(project(":nnstreamer-api")) testImplementation(libs.junit) androidTestImplementation(libs.androidx.junit) androidTestImplementation(libs.androidx.espresso.core) diff --git a/ml_inference_offloading/src/main/java/ai/nnstreamer/ml/inference/offloading/MainService.kt b/ml_inference_offloading/src/main/java/ai/nnstreamer/ml/inference/offloading/MainService.kt index 494801a..8db8fa2 100644 --- a/ml_inference_offloading/src/main/java/ai/nnstreamer/ml/inference/offloading/MainService.kt +++ b/ml_inference_offloading/src/main/java/ai/nnstreamer/ml/inference/offloading/MainService.kt @@ -13,10 +13,13 @@ import android.os.IBinder import android.os.Looper import android.os.Message import android.os.Process +import android.util.Log import android.widget.Toast import androidx.core.app.NotificationCompat import androidx.core.app.ServiceCompat import androidx.core.content.ContextCompat +import org.nnsuite.nnstreamer.NNStreamer + class MainService : Service() { private inner class MainHandler(looper: Looper) : Handler(looper) { @@ -36,6 +39,7 @@ class MainService : Service() { private lateinit var serviceHandler : MainHandler private lateinit var serviceLooper : Looper private lateinit var handlerThread: HandlerThread + private var initialized = false private fun startForeground() { // Get NotificationManager @@ -73,6 +77,10 @@ class MainService : Service() { } override fun onCreate() { + initNNStreamer() + if (!this.initialized) { + Toast.makeText(this, "Failed to initialize nnstreamer", Toast.LENGTH_SHORT).show() + } handlerThread = HandlerThread("ServiceStartArguments", Process.THREAD_PRIORITY_BACKGROUND).apply { start() } @@ -102,4 +110,22 @@ class MainService : Service() { override fun onDestroy() { Toast.makeText(this, "The MainService has been gone", Toast.LENGTH_SHORT).show() } -} \ No newline at end of file + + private fun initNNStreamer() { + if (this.initialized) { + return + } + try { + initialized = NNStreamer.initialize(this) + } catch (e: Exception) { + e.printStackTrace() + Log.e(TAG, e.message!!) + } finally { + if (initialized) { + Log.i(TAG, "Version: " + NNStreamer.getVersion()) + } else { + Log.e(TAG, "Failed to initialize NNStreamer") + } + } + } +} diff --git a/nnstreamer-api/build.gradle.kts b/nnstreamer-api/build.gradle.kts index bc4a9c6..020dcb2 100644 --- a/nnstreamer-api/build.gradle.kts +++ b/nnstreamer-api/build.gradle.kts @@ -94,3 +94,25 @@ android { } } } + +afterEvaluate { + val compileJavaDebug = project.getTasksByName("compileDebugJavaWithJavac", false) + val nativeBuildDebug = project.getTasksByName("externalNativeBuildDebug", false) + + if (compileJavaDebug.size >= 1 && nativeBuildDebug.size >= 1) { + val compileJavaTask = compileJavaDebug.first() + val nativeBuildTask = nativeBuildDebug.first() + + compileJavaTask.dependsOn(nativeBuildTask) + } + + val compileJavaRelease = project.getTasksByName("compileReleaseJavaWithJavac", false) + val nativeBuildRelease = project.getTasksByName("externalNativeBuildRelease", false) + + if (compileJavaRelease.size >= 1 && nativeBuildRelease.size >= 1) { + val compileJavaTask = compileJavaDebug.first() + val nativeBuildTask = nativeBuildDebug.first() + + compileJavaTask.dependsOn(nativeBuildTask) + } +}