[Android] Improve the start/stop/restart remote access server asynchronicity

Nicolas Pomepuy git at videolan.org
Thu Apr 25 12:22:35 UTC 2024


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Thu Apr 25 07:18:42 2024 +0200| [5d579db8f2f6321c78905691764c18ef6d64af39] | committer: Duncan McNamara

Improve the start/stop/restart remote access server asynchronicity

Fixes #3036

> https://code.videolan.org/videolan/vlc-android/commit/5d579db8f2f6321c78905691764c18ef6d64af39
---

 .../videolan/vlc/webserver/RemoteAccessService.kt  | 50 +++++++++++++++-------
 1 file changed, 34 insertions(+), 16 deletions(-)

diff --git a/application/webserver/src/main/java/org/videolan/vlc/webserver/RemoteAccessService.kt b/application/webserver/src/main/java/org/videolan/vlc/webserver/RemoteAccessService.kt
index 11b093a1e9..e237ee6af1 100644
--- a/application/webserver/src/main/java/org/videolan/vlc/webserver/RemoteAccessService.kt
+++ b/application/webserver/src/main/java/org/videolan/vlc/webserver/RemoteAccessService.kt
@@ -31,8 +31,11 @@ import androidx.annotation.RequiresApi
 import androidx.lifecycle.LifecycleService
 import androidx.lifecycle.Observer
 import androidx.lifecycle.lifecycleScope
-import io.ktor.server.netty.*
+import kotlinx.coroutines.CoroutineScope
 import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.MainScope
+import kotlinx.coroutines.channels.Channel
+import kotlinx.coroutines.channels.actor
 import kotlinx.coroutines.launch
 import kotlinx.coroutines.withContext
 import org.videolan.libvlc.util.AndroidUtil
@@ -49,16 +52,42 @@ import org.videolan.tools.putSingle
 import org.videolan.vlc.gui.helpers.NotificationHelper
 
 
-class RemoteAccessService : LifecycleService() {
+class RemoteAccessService : LifecycleService(), CoroutineScope by MainScope() {
 
     private lateinit var server: RemoteAccessServer
+    private val startServerActor = actor<String>(capacity = Channel.CONFLATED) {
+        for (entry in channel) {
+            when (entry) {
+                ACTION_STOP_SERVER -> {
+                    server.stop()
+                }
+                ACTION_START_SERVER -> {
+                    server.start()
+                }
+                ACTION_RESTART_SERVER ->{
+                    val observer = object : Observer<ServerStatus> {
+                        override fun onChanged(serverStatus: ServerStatus) {
+                            if (serverStatus == ServerStatus.STOPPED) {
+                                lifecycleScope.launch { server.start() }
+                                server.serverStatus.removeObserver(this)
+                            }
+                        }
+                    }
+                    server.serverStatus.observe(this at RemoteAccessService, observer)
+                    server.stop()
+                }
+            }
+        }
+    }
+
+
     private val receiver = object : BroadcastReceiver() {
         @SuppressLint("WakelockTimeout")
         override fun onReceive(context: Context, intent: Intent) {
             if (!::server.isInitialized) return
             when (intent.action) {
                 ACTION_STOP_SERVER -> {
-                    lifecycleScope.launch { server.stop() }
+                    startServerActor.trySend(ACTION_STOP_SERVER)
                 }
                 ACTION_DISABLE_SERVER -> {
                     lifecycleScope.launch {
@@ -68,21 +97,10 @@ class RemoteAccessService : LifecycleService() {
                     }
                 }
                 ACTION_START_SERVER -> {
-                    lifecycleScope.launch { server.start() }
+                    startServerActor.trySend(ACTION_START_SERVER)
                 }
                 ACTION_RESTART_SERVER -> {
-                    lifecycleScope.launch {
-                        val observer = object : Observer<ServerStatus> {
-                            override fun onChanged(serverStatus: ServerStatus) {
-                                if (serverStatus == ServerStatus.STOPPED) {
-                                    lifecycleScope.launch { server.start() }
-                                    server.serverStatus.removeObserver(this)
-                                }
-                            }
-                        }
-                        server.serverStatus.observe(this at RemoteAccessService, observer)
-                        server.stop()
-                    }
+                    startServerActor.trySend(ACTION_RESTART_SERVER)
                 }
             }
         }



More information about the Android mailing list