From d7e2b323d260e14f449670ec10afb7b0b4c99d85 Mon Sep 17 00:00:00 2001 From: evgeny Date: Wed, 28 Oct 2026 17:45:23 +0100 Subject: [PATCH] Synchronize radio discontinuity regression with the first capture frame --- .../com/utun/chat/data/RadioAudioEngine.kt | 64 +++++++++---------- .../chat/data/RadioWorkerLifecycleTest.kt | 16 ++--- 2 files changed, 36 insertions(+), 44 deletions(-) diff --git a/tools/chatgui-android/app/src/main/java/com/utun/chat/data/RadioAudioEngine.kt b/tools/chatgui-android/app/src/main/java/com/utun/chat/data/RadioAudioEngine.kt index 42f57dbde..dd0530c25 100644 --- a/tools/chatgui-android/app/src/main/java/com/utun/chat/data/RadioAudioEngine.kt +++ b/tools/chatgui-android/app/src/main/java/com/utun/chat/data/RadioAudioEngine.kt @@ -415,40 +415,40 @@ class RadioAudioEngine { /** Оба детектора используют одно окно по экрану/фону; измеритель не продлевает это окно. */ private fun updateCapture() { try { - mainHandler.removeCallbacks(vadTimeout) - if (!settingsReady) return - if (!active || groupId != audioGroup) { stopCapture(); return } - val now = SystemClock.elapsedRealtime() - vadWindow.setEnabled(autoSettings.enabled, now) - var mode = radioCaptureMode(active, screenInteractive, vadWindow.isAllowed(now), autoSettings, requestedTalk, levelMonitoring) - LogManager.addLog("DEBUG", "radio", "Auto capture gate group=$audioGroup desired=$mode requested=${autoSettings.mode} " + - "enabled=${autoSettings.enabled} screen=$screenInteractive foreground=${vadWindow.foreground} " + - "backgroundLeftMs=${vadWindow.timeLeftMs(now)} manual=$requestedTalk monitor=$levelMonitoring capture=$captureRunning") - if (mode != null && mode != RadioAutoSettings.OFF && !vadWindow.foreground && - !mainHandler.postDelayed(vadTimeout, vadWindow.timeLeftMs(now).coerceAtLeast(1L))) { - vadWindow.close() - mode = radioCaptureMode(active, screenInteractive, false, autoSettings, requestedTalk, levelMonitoring) - LogManager.addLog("ERROR", "radio", "automatic timeout rejected group=$audioGroup; background window closed " + - "manual=$requestedTalk monitor=$levelMonitoring desired=$mode") - } - if (mode == null) { stopCapture(); return } - val detectorFailed = mode != RadioAutoSettings.OFF && autoMode == RadioAutoSettings.OFF - if (captureSubscription != null && (captureRequestedMode != mode || (detectorFailed && !requestedTalk && !levelMonitoring))) stopCapture() - if (audioTrack == null) return - if (captureSubscription == null && nativeCaptureOwned) stopCapture() - if (captureSubscription == null && !startCapture(mode)) { - requestedTalk = false - _talkingFlow.value = false - return - } - if (requestedTalk != capturePttRequested) { - if (captureSubscription?.ptt(requestedTalk) != true) { - LogManager.addLog("ERROR", "radio", "PTT command rejected; stopping capture group=$audioGroup") + mainHandler.removeCallbacks(vadTimeout) + if (!settingsReady) return + if (!active || groupId != audioGroup) { stopCapture(); return } + val now = SystemClock.elapsedRealtime() + vadWindow.setEnabled(autoSettings.enabled, now) + var mode = radioCaptureMode(active, screenInteractive, vadWindow.isAllowed(now), autoSettings, requestedTalk, levelMonitoring) + LogManager.addLog("DEBUG", "radio", "Auto capture gate group=$audioGroup desired=$mode requested=${autoSettings.mode} " + + "enabled=${autoSettings.enabled} screen=$screenInteractive foreground=${vadWindow.foreground} " + + "backgroundLeftMs=${vadWindow.timeLeftMs(now)} manual=$requestedTalk monitor=$levelMonitoring capture=$captureRunning") + if (mode != null && mode != RadioAutoSettings.OFF && !vadWindow.foreground && + !mainHandler.postDelayed(vadTimeout, vadWindow.timeLeftMs(now).coerceAtLeast(1L))) { + vadWindow.close() + mode = radioCaptureMode(active, screenInteractive, false, autoSettings, requestedTalk, levelMonitoring) + LogManager.addLog("ERROR", "radio", "automatic timeout rejected group=$audioGroup; background window closed " + + "manual=$requestedTalk monitor=$levelMonitoring desired=$mode") + } + if (mode == null) { stopCapture(); return } + val detectorFailed = mode != RadioAutoSettings.OFF && autoMode == RadioAutoSettings.OFF + if (captureSubscription != null && (captureRequestedMode != mode || (detectorFailed && !requestedTalk && !levelMonitoring))) stopCapture() + if (audioTrack == null) return + if (captureSubscription == null && nativeCaptureOwned) stopCapture() + if (captureSubscription == null && !startCapture(mode)) { requestedTalk = false - stopCapture(); return + _talkingFlow.value = false + return + } + if (requestedTalk != capturePttRequested) { + if (captureSubscription?.ptt(requestedTalk) != true) { + LogManager.addLog("ERROR", "radio", "PTT command rejected; stopping capture group=$audioGroup") + requestedTalk = false + stopCapture(); return + } + capturePttRequested = requestedTalk } - capturePttRequested = requestedTalk - } } catch (e: Exception) { requestedTalk = false _talkingFlow.value = nativeCaptureOwned && _transmittingGroup.value != null diff --git a/tools/chatgui-android/router-tests/src/test/kotlin/com/utun/chat/data/RadioWorkerLifecycleTest.kt b/tools/chatgui-android/router-tests/src/test/kotlin/com/utun/chat/data/RadioWorkerLifecycleTest.kt index d865100a2..a2fd35a97 100644 --- a/tools/chatgui-android/router-tests/src/test/kotlin/com/utun/chat/data/RadioWorkerLifecycleTest.kt +++ b/tools/chatgui-android/router-tests/src/test/kotlin/com/utun/chat/data/RadioWorkerLifecycleTest.kt @@ -7,7 +7,6 @@ import android.os.Handler import android.os.Looper import com.utun.chat.ChatApplication import java.util.concurrent.CountDownLatch -import java.util.concurrent.ExecutionException import java.util.concurrent.ExecutorService import java.util.concurrent.Executors import java.util.concurrent.TimeUnit @@ -47,16 +46,7 @@ class RadioWorkerLifecycleTest { io.shutdown(); assertTrue(io.awaitTermination(5, TimeUnit.SECONDS)) val barrier = Executors.newSingleThreadExecutor() try { - try { barrier.submit { MediaAudioController.awaitIdle() }.get(5, TimeUnit.SECONDS) } catch (e: ExecutionException) { - // Отрицательный контроль старого кода: закрыть подписки, потерянные при исключении до присваивания поля. - val shared = MediaAudioSession::class.java.getDeclaredField("shared").apply { isAccessible = true } - .get(MediaAudioController) as? MicrophoneCapture ?: throw e - val field = MicrophoneCapture::class.java.getDeclaredField("consumers").apply { isAccessible = true } - @Suppress("UNCHECKED_CAST") - val consumers = synchronized(shared) { (field.get(shared) as List).toList() } - consumers.forEach { it.close() } - barrier.submit { MediaAudioController.awaitIdle() }.get(5, TimeUnit.SECONDS) - } + barrier.submit { MediaAudioController.awaitIdle() }.get(5, TimeUnit.SECONDS) } finally { barrier.shutdownNow() } assertTrue(AudioTrack.created.all { it.released && it.accessAfterRelease == 0 }) assertTrue(AndroidMicrophoneCapture.opened.all { it.closed }) @@ -218,7 +208,9 @@ class RadioWorkerLifecycleTest { } @Test fun nativeBoundaryFailureEndsCaptureAndDoesNotLeakItsWorker() { - begin(); NativeLib.radioFailures = setOf("boundary") + begin() + await("initial capture frame") { synchronized(NativeLib.radioFeeds) { NativeLib.radioFeeds.isNotEmpty() } } + NativeLib.radioFailures = setOf("boundary") requireNotNull(subscription()).deliver(MicrophoneCapture.Frame(ShortArray(1920), System.nanoTime(), Long.MAX_VALUE, generation = 99)) await("boundary diagnostic") { diagnostic("capture failed operation=discontinuity") } captureEnded()