Skip to content

Commit

Permalink
App: Seperate pipeline pause and close
Browse files Browse the repository at this point in the history
This patch seperates pipeline pause and close.
Also, now we can see pipeline status in main activity.

Signed-off-by: Yelin Jeong <[email protected]>
  • Loading branch information
niley7464 committed Jun 10, 2024
1 parent 2aa41cd commit 1ce6bb1
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,23 @@ class MainActivity : ComponentActivity() {

startForegroundService(Intent(this, MainService::class.java))

val portTextView = findViewById<TextView>(R.id.status)
val start = findViewById<Button>(R.id.start)
start.setOnClickListener(View.OnClickListener {
val serverPort = mService?.startServer()
val portTextView = findViewById<TextView>(R.id.port)
portTextView.text = "Listening on port: " + serverPort.toString();
portTextView.text = "Listening on port: " + serverPort.toString()
})

val stop = findViewById<Button>(R.id.stop)
stop.setOnClickListener(View.OnClickListener {
val pause = findViewById<Button>(R.id.stop)
pause.setOnClickListener(View.OnClickListener {
mService?.stopServer()
portTextView.text = resources.getString(R.string.server_paused)
})

val close = findViewById<Button>(R.id.close)
close.setOnClickListener(View.OnClickListener {
mService?.closeServer()
portTextView.text = resources.getString(R.string.server_unknown)
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class MainService : Service() {
private var initialized = false
private var port = -1
private lateinit var tensorQueryServer: Pipeline
private var tensorQueryServerStatus = Pipeline.State.UNKNOWN
private lateinit var model: File

private fun startForeground() {
Expand Down Expand Up @@ -155,6 +156,7 @@ class MainService : Service() {
}

override fun onDestroy() {
closeServer()
Toast.makeText(this, "The MainService has been gone", Toast.LENGTH_SHORT).show()
}

Expand Down Expand Up @@ -273,22 +275,43 @@ class MainService : Service() {
}

fun startServer(): Int {
val hostAddress = getIpAddress()
if (!isPortAvailable(port)) {
port = findPort()
when (tensorQueryServerStatus) {
Pipeline.State.UNKNOWN -> {
val hostAddress = getIpAddress()
if (!isPortAvailable(port)) {
port = findPort()
}

val desc =
"tensor_query_serversrc host=" + hostAddress + " port=" + port.toString() + " ! " +
"other/tensor,format=static,dimension=(string)3:224:224:1,type=uint8,framerate=0/1 ! " +
"tensor_filter framework=tensorflow-lite model=" + model.getAbsolutePath() + " ! " +
"other/tensor,format=static,dimension=(string)1001:1,type=uint8,framerate=0/1 ! tensor_query_serversink async=false"
tensorQueryServer = Pipeline(desc, null)
tensorQueryServerStatus = Pipeline.State.NULL
}
Pipeline.State.PLAYING -> {
Log.i(TAG, "Tensor query server is already playing")
return port
}
else -> {}
}

val desc = "tensor_query_serversrc host=" + hostAddress + " port=" + port.toString() + " ! " +
"other/tensor,format=static,dimension=(string)3:224:224:1,type=uint8,framerate=0/1 ! " +
"tensor_filter framework=tensorflow-lite model=" + model.getAbsolutePath() + " ! " +
"other/tensor,format=static,dimension=(string)1001:1,type=uint8,framerate=0/1 ! tensor_query_serversink async=false"
tensorQueryServer = Pipeline(desc, null)
tensorQueryServer.start()
tensorQueryServerStatus = Pipeline.State.PLAYING

return port
}

fun stopServer() {
if (tensorQueryServerStatus != Pipeline.State.UNKNOWN) {
tensorQueryServer.stop()
tensorQueryServerStatus = Pipeline.State.PAUSED
}
}

fun closeServer() {
tensorQueryServer.close()
tensorQueryServerStatus = Pipeline.State.UNKNOWN
}
}
10 changes: 8 additions & 2 deletions ml_inference_offloading/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop" />
<Button
android:id="@+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Close" />
<TextView
android:id="@+id/port"
android:id="@+id/status"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_height="wrap_content"
android:text="@string/server_unknown"/>
</LinearLayout>
4 changes: 3 additions & 1 deletion ml_inference_offloading/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<resources>
<string name="app_name">ML Inference Offloading</string>
<string name="title_activity_main">MainActivity</string>
</resources>
<string name="server_paused">Server is paused</string>
<string name="server_unknown">There is no tensor query server</string>
</resources>

0 comments on commit 1ce6bb1

Please sign in to comment.