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

App/Service: Add nnstreamer to ml_inference_offloading service #13

Merged
merged 1 commit into from
Apr 24, 2024
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
1 change: 1 addition & 0 deletions ml_inference_offloading/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -73,6 +77,11 @@ class MainService : Service() {
}

override fun onCreate() {
initNNStreamer()
niley7464 marked this conversation as resolved.
Show resolved Hide resolved
if (!this.initialized) {
Log.e(TAG, "Failed to initialize nnstreamer")
stopSelf()
}
handlerThread = HandlerThread("ServiceStartArguments", Process.THREAD_PRIORITY_BACKGROUND).apply {
start()
}
Expand Down Expand Up @@ -102,4 +111,22 @@ class MainService : Service() {
override fun onDestroy() {
Toast.makeText(this, "The MainService has been gone", Toast.LENGTH_SHORT).show()
}
}

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")
}
}
}
niley7464 marked this conversation as resolved.
Show resolved Hide resolved
}
22 changes: 22 additions & 0 deletions nnstreamer-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}