From 7113f0a6f4137891f9e810b1abcb74c54cb043c3 Mon Sep 17 00:00:00 2001 From: Yelin Jeong Date: Wed, 24 Apr 2024 10:22:02 +0900 Subject: [PATCH] App/Service: Use tensor query server in service This patch implements tensor query server related functions. HOST_ADDR is now for internal network only. Signed-off-by: Yelin Jeong --- .../src/main/AndroidManifest.xml | 1 + .../src/main/assets/models/Readme.txt | 1 + .../ml/inference/offloading/MainService.kt | 102 +++++++++++++++++- 3 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 ml_inference_offloading/src/main/assets/models/Readme.txt diff --git a/ml_inference_offloading/src/main/AndroidManifest.xml b/ml_inference_offloading/src/main/AndroidManifest.xml index fa91404..e334201 100644 --- a/ml_inference_offloading/src/main/AndroidManifest.xml +++ b/ml_inference_offloading/src/main/AndroidManifest.xml @@ -5,6 +5,7 @@ + ? = null + + try { + files = am.list("models/") + } catch (e: java.lang.Exception) { + Log.e("TAG", "#### Failed to get asset file list") + e.printStackTrace() + return + } + + // Copy files into app-specific directory. + for (filename in files!!) { + try { + val inFile = am.open("models/$filename") + val outDir = getExternalFilesDir(null)!!.absolutePath + val outFile = File(outDir, filename) + val out: OutputStream = FileOutputStream(outFile) + + val buffer = ByteArray(1024) + var read: Int + while ((inFile.read(buffer).also { read = it }) != -1) { + out.write(buffer, 0, read) + } + + inFile.close() + out.flush() + out.close() + } catch (e: IOException) { + Log.e("TAG", "Failed to copy file: $filename") + e.printStackTrace() + return + } + } + } + private fun initNNStreamer() { if (this.initialized) { return @@ -131,11 +185,55 @@ class MainService : Service() { } } + private fun isPortAvailable(port: Int): Boolean { + var result = false + if (port < 0) { + return result + } + + val portChecker = thread() { + try { + val serverSocket = ServerSocket(port) + serverSocket.close() + result = true + } catch (e: Exception) { + Log.e(TAG, e.toString()) + } + } + portChecker.join() + return result + } + + private fun findPort(): Int { + var port = -1 + val portFinder = thread() { + try { + val serverSocket = ServerSocket(0) + Log.i(TAG, "listening on port: " + serverSocket.localPort) + port = serverSocket.localPort + serverSocket.close() + } catch (e: Exception) { + Log.e(TAG, e.toString()) + } + } + portFinder.join() + return port + } + fun startServer() { - TODO("Not yet implemented") + if (!isPortAvailable(port)) { + port = findPort() + } + + val desc = "tensor_query_serversrc host=" + HOST_ADDR + " 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() } fun stopServer() { - TODO("Not yet implemented") + tensorQueryServer.stop() } }