From f3b5cadb7b186a75d584b380f71df181190caec8 Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Fri, 23 Oct 2026 07:57:34 -0400 Subject: [PATCH] fix: release the microphone before freezing a session so the SoC can sleep --- .../audio/DirectAudioRelayComponent.java | 33 +++++++++++++++++++ .../launcher/audio/PulseAudioComponent.java | 25 +++++++++----- .../launcher/session/SessionService.kt | 7 ++++ 3 files changed, 57 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/droiddeck/launcher/audio/DirectAudioRelayComponent.java b/app/src/main/java/com/droiddeck/launcher/audio/DirectAudioRelayComponent.java index 15ffd7bdb0..f1fa534927 100644 --- a/app/src/main/java/com/droiddeck/launcher/audio/DirectAudioRelayComponent.java +++ b/app/src/main/java/com/droiddeck/launcher/audio/DirectAudioRelayComponent.java @@ -17,6 +17,8 @@ public class DirectAudioRelayComponent extends SessionPart { private File logFile; private final File micFifoPath; private volatile int pid = -1; + private final Object micLock = new Object(); + private boolean micHot; public DirectAudioRelayComponent(File socketPath, File micFifoPath) { this.socketPath = socketPath; @@ -30,6 +32,9 @@ public class DirectAudioRelayComponent extends SessionPart { @Override public void start() { stop(); + synchronized (micLock) { + micHot = false; + } File binary = new File(app().getApplicationInfo().nativeLibraryDir, BINARY); if (!binary.isFile()) { Log.w(TAG, "helper missing at " + binary + "; DirectAudio will report itself unavailable"); @@ -79,12 +84,40 @@ public class DirectAudioRelayComponent extends SessionPart { app().getFilesDir(), null, line -> { Log.i(TAG, line); + trackMic(line); if (log != null) synchronized (log) { log.println(line); log.flush(); } }); Log.i(TAG, "started pid=" + pid + " socket=" + socketPath + (micFifoPath != null ? " mic=" + micFifoPath : " (no microphone)")); } + private void trackMic(String line) { + boolean start = line.startsWith("DA-Relay: mic start "); + if (!start && !line.startsWith("DA-Relay: mic stop ")) return; + synchronized (micLock) { + micHot = start; + micLock.notifyAll(); + } + } + + // Android keeps the input stream's wake lock while a frozen helper still has it started. + public boolean awaitMicReleased(long timeoutMs) { + long deadline = System.currentTimeMillis() + timeoutMs; + synchronized (micLock) { + while (micHot) { + long left = deadline - System.currentTimeMillis(); + if (left <= 0) return false; + try { + micLock.wait(left); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + return false; + } + } + return true; + } + } + @Override public void stop() { if (pid != -1) { diff --git a/app/src/main/java/com/droiddeck/launcher/audio/PulseAudioComponent.java b/app/src/main/java/com/droiddeck/launcher/audio/PulseAudioComponent.java index 849455b7a0..3b73b6a3c9 100644 --- a/app/src/main/java/com/droiddeck/launcher/audio/PulseAudioComponent.java +++ b/app/src/main/java/com/droiddeck/launcher/audio/PulseAudioComponent.java @@ -20,6 +20,7 @@ import java.util.concurrent.TimeUnit; public class PulseAudioComponent extends SessionPart { private static final String TAG = "PulseAudio"; public static final String SOCKET_NAME = "PS0"; + private static final String MIC_SOURCE = "DirectAudioMic"; private static final String BUNDLE_STAMP = "2026-10-08-pa13-directaudio-native"; @@ -97,9 +98,9 @@ public class PulseAudioComponent extends SessionPart { config.add("set-default-sink DirectAudio"); config.add(".fail"); if (micFifoPath != null && !micFifoPath.isEmpty()) { - config.add("load-module module-pipe-source source_name=DirectAudioMic file=\"" + config.add("load-module module-pipe-source source_name=" + MIC_SOURCE + " file=\"" + micFifoPath + "\" format=s16le rate=48000 channels=1"); - config.add("set-default-source DirectAudioMic"); + config.add("set-default-source " + MIC_SOURCE); } FileUtils.writeString(new File(workingDir, "default.pa"), String.join("\n", config)); @@ -202,16 +203,24 @@ public class PulseAudioComponent extends SessionPart { } public boolean setSinkSuspended(boolean suspended) { + return suspend("sink", "@DEFAULT_SINK@", suspended); + } + + public boolean setMicSuspended(boolean suspended) { + return micFifoPath == null || micFifoPath.isEmpty() || suspend("source", MIC_SOURCE, suspended); + } + + private boolean suspend(String kind, String name, boolean suspended) { File pactl = new File(workingDir, "pactl"); if (!pactl.isFile() || pid <= 1) { - Log.w(TAG, "cannot change sink state: PulseAudio control client or server unavailable"); + Log.w(TAG, "cannot change " + kind + " state: PulseAudio control client or server unavailable"); return false; } java.lang.Process process = null; try { File modules = new File(workingDir, "modules/arm64"); ProcessBuilder builder = new ProcessBuilder( - pactl.getAbsolutePath(), "suspend-sink", "@DEFAULT_SINK@", Boolean.toString(suspended)); + pactl.getAbsolutePath(), "suspend-" + kind, name, Boolean.toString(suspended)); builder.directory(workingDir); builder.redirectErrorStream(true); builder.redirectOutput(new File("/dev/null")); @@ -222,18 +231,18 @@ public class PulseAudioComponent extends SessionPart { process = builder.start(); if (!process.waitFor(5, TimeUnit.SECONDS)) { process.destroyForcibly(); - Log.w(TAG, "timed out changing sink state to suspended=" + suspended); + Log.w(TAG, "timed out changing " + kind + " state to suspended=" + suspended); return false; } int status = process.exitValue(); - if (status != 0) Log.w(TAG, "sink state change failed with exit " + status); + if (status != 0) Log.w(TAG, kind + " state change failed with exit " + status); return status == 0; } catch (InterruptedException e) { Thread.currentThread().interrupt(); - Log.w(TAG, "interrupted changing sink state", e); + Log.w(TAG, "interrupted changing " + kind + " state", e); return false; } catch (Exception e) { - Log.w(TAG, "could not change sink state", e); + Log.w(TAG, "could not change " + kind + " state", e); return false; } finally { if (process != null && process.isAlive()) process.destroyForcibly(); diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt index 5cb8fede7f..31c63ff469 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt @@ -615,9 +615,15 @@ class SessionService : Service() { helperRoots = { components.mapNotNull { it.suspendPid().takeIf { pid -> pid > 1 } } }, suspendAudio = { if (!pulse.setSinkSuspended(true)) Log.w(TAG, "could not suspend audio sink") + if (!pulse.setMicSuspended(true)) Log.w(TAG, "could not suspend microphone source") + val relay = components.firstNotNullOfOrNull { it as? DirectAudioRelayComponent } + if (relay != null && !relay.awaitMicReleased(MIC_RELEASE_TIMEOUT_MS)) { + Log.w(TAG, "microphone still open after ${MIC_RELEASE_TIMEOUT_MS} ms; suspending anyway") + } }, resumeAudio = { if (!pulse.setSinkSuspended(false)) Log.w(TAG, "could not resume audio sink") + if (!pulse.setMicSuspended(false)) Log.w(TAG, "could not resume microphone source") }, zramControl = File(filesDir, "${LinuxRuntime.DIR}/tmp/.bl-zram"), pagedOut = { processes, unacknowledged, residentKb, swapKb, swapBeforeKb, millis -> @@ -1884,6 +1890,7 @@ class SessionService : Service() { companion object { private const val TAG = "SessionService" private const val TRACER_NICE = -6 + private const val MIC_RELEASE_TIMEOUT_MS = 5_000L private const val TU_DEBUG_SWITCH = "Download/droiddeck-tu-debug" private const val GAME_TU_DEBUG = "DROIDDECK_GAME_TU_DEBUG" private const val LEGACY_ENV_SWITCH = "Download/droiddeck-env"