Synchronize radio discontinuity regression with the first capture frame

This commit is contained in:
evgeny committed 2026-10-29 13:34:23 +01:00
1 parent 8809c55397
commit d7e2b323d2
2 files changed
+36 -44

No files matched your search

@@ -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
@@ -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<MicrophoneCapture.Subscription>).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()