Skip to content

Commit

Permalink
App/Service: Add nnstreamer to ml_inference_offloading service
Browse files Browse the repository at this point in the history
This patch adds nnstreamer to android service.

Signed-off-by: Yelin Jeong <[email protected]>
  • Loading branch information
niley7464 committed Apr 22, 2024
1 parent 57d4da5 commit ed95754
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
4 changes: 4 additions & 0 deletions ml_inference_offloading/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ dependencies {
implementation(libs.androidx.ui.graphics)
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.material3)
implementation(fileTree(mapOf(
"dir" to "libs",
"include" to listOf("*.aar", "*.jar")
)))
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
Expand Down
1 change: 1 addition & 0 deletions ml_inference_offloading/libs/Readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
To run this sample, copy nnstreamer-api library file into 'libs' directory.
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,7 @@ class MainService : Service() {
}

override fun onCreate() {
initNNStreamer()
handlerThread = HandlerThread("ServiceStartArguments", Process.THREAD_PRIORITY_BACKGROUND).apply {
start()
}
Expand Down Expand Up @@ -102,4 +107,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")
}
}
}
}

0 comments on commit ed95754

Please sign in to comment.