Add an in-session microphone toggle (#1506)

This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-10-25 08:37:49 -04:00
1 parent 1f42f512e2
commit 0cf5b42788
13 files changed
+347 -12

No files matched your search

@@ -818,6 +818,9 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
lossless = lossless,
oscMode = oscMode, onScreenButtonsVisible = onScreenButtonsVisible, suspendPolicy = suspendPolicy,
steamDownloadsInBackground = steamDownloadsInBackground, touchMode = touchMode,
micAvailable = SessionState.micAvailable && !SessionState.suspended,
micEnabled = SessionState.micEnabled,
onMicEnabled = { SessionService.setMicEnabled(this@SessionActivity, it) },
touchAuto = getString(if (usingTouchpad()) R.string.session_touch_auto_touchpad else R.string.session_touch_auto_direct),
fexPreset = fexPreset,
secondScreenMode = secondScreenMode,
@@ -3,6 +3,7 @@ package com.droiddeck.launcher.audio;
import android.os.Process;
import android.util.Log;
import com.droiddeck.launcher.core.FileUtils;
import com.droiddeck.launcher.core.SessionPart;
import com.droiddeck.launcher.core.HostProcess;
@@ -19,10 +20,13 @@ public class DirectAudioRelayComponent extends SessionPart {
private volatile int pid = -1;
private final Object micLock = new Object();
private boolean micHot;
private final File micControl;
public java.util.function.Consumer<Boolean> onMicEnabled;
public DirectAudioRelayComponent(File socketPath, File micFifoPath) {
this.socketPath = socketPath;
this.micFifoPath = micFifoPath;
this.micControl = micFifoPath == null ? null : new File(micFifoPath.getParentFile(), "mic-enabled");
}
public void setLogFile(File file) {
@@ -63,8 +67,13 @@ public class DirectAudioRelayComponent extends SessionPart {
StringBuilder command = new StringBuilder(binary.getAbsolutePath());
command.append(" --socket ").append(socketPath.getAbsolutePath());
command.append(" --log");
if (micFifoPath != null) {
final boolean withMic = micFifoPath != null && setMicEnabled(true);
if (withMic) {
command.append(" --mic-fifo ").append(micFifoPath.getAbsolutePath());
command.append(" --mic-control ").append(micControl.getAbsolutePath());
} else if (micFifoPath != null) {
Log.w(TAG, "microphone control unavailable; starting with capture disabled");
command.append(" --mic-control /dev/null");
}
ArrayList<String> env = new ArrayList<>();
env.add("HOME=" + app().getFilesDir());
@@ -73,7 +82,7 @@ public class DirectAudioRelayComponent extends SessionPart {
try {
out = new java.io.PrintWriter(new java.io.FileWriter(logFile, true));
out.println("== DirectAudio relay helper starting"
+ (micFifoPath != null ? " with the microphone" : " (no microphone)"));
+ (withMic ? " with the microphone" : " (no microphone)"));
out.flush();
} catch (Exception e) {
Log.w(TAG, "could not open " + logFile, e);
@@ -84,14 +93,23 @@ public class DirectAudioRelayComponent extends SessionPart {
app().getFilesDir(), null,
line -> {
Log.i(TAG, line);
trackMic(line);
if (withMic) 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)"));
+ (withMic ? " mic=" + micFifoPath : " (no microphone)"));
}
public synchronized boolean setMicEnabled(boolean enabled) {
if (micControl == null) return false;
File next = new File(micControl.getPath() + ".new");
return FileUtils.writeString(next, enabled ? "1" : "0") && next.renameTo(micControl);
}
private void trackMic(String line) {
if (line.startsWith("DA-Relay: mic enabled=") && onMicEnabled != null) {
onMicEnabled.accept(line.equals("DA-Relay: mic enabled=1"));
}
boolean start = line.startsWith("DA-Relay: mic start ");
if (!start && !line.startsWith("DA-Relay: mic stop ")) return;
synchronized (micLock) {
@@ -278,6 +278,16 @@ class SessionService : Service() {
updateSuspendPolicy()
return START_NOT_STICKY
}
ACTION_MIC_CHANGED -> {
if (SessionState.running && SessionState.micAvailable && !SessionState.suspended) {
val enabled = intent.getBooleanExtra("enabled", false)
val relay = components.firstNotNullOfOrNull { it as? DirectAudioRelayComponent }
if (relay?.setMicEnabled(enabled) != true) {
android.widget.Toast.makeText(this, R.string.drawer_mic_failed, android.widget.Toast.LENGTH_LONG).show()
}
}
return START_NOT_STICKY
}
ACTION_SUSPEND_POLICY_CHANGED -> {
if (!SessionState.running) return START_NOT_STICKY
suspendPolicy = SessionPrefs.suspendPolicy(this, SessionState.mode)
@@ -329,6 +339,8 @@ class SessionService : Service() {
freezePending = false
suspendAttemptFailed = false
suspendController = null
SessionState.micAvailable = false
SessionState.micEnabled = false
SessionState.suspended = false
SessionState.resumeRequested = false
SessionState.resuming = false
@@ -761,6 +773,15 @@ class SessionService : Service() {
components.add(pulse)
if (wantsDirectAudio || wantsMic) {
val relay = DirectAudioRelayComponent(relaySocket, micFifo)
val gen = sessionGen
relay.onMicEnabled = java.util.function.Consumer { enabled ->
mainHandler.post {
if (gen == sessionGen && SessionState.running && wantsMic) {
SessionState.micAvailable = true
SessionState.micEnabled = enabled
}
}
}
relay.setLogFile(audioLog)
relay.attach(this)
components.add(relay)
@@ -1656,6 +1677,8 @@ class SessionService : Service() {
SessionState.running = false
SessionPaths.take() to SessionState.steamClientArch
}
SessionState.micAvailable = false
SessionState.micEnabled = false
xrActivityVisible = false
updateDownloadMonitoring()
val stoppedGen = sessionGen
@@ -1948,6 +1971,7 @@ class SessionService : Service() {
const val ACTION_LAUNCH_GAME = "com.droiddeck.launcher.LAUNCH_STEAM_GAME"
const val ACTION_HOME_GUIDE = "com.droiddeck.launcher.HOME_GUIDE"
const val ACTION_AGENT_START = "com.droiddeck.launcher.AGENT_START"
private const val ACTION_MIC_CHANGED = "com.droiddeck.launcher.MIC_CHANGED"
private const val ACTION_SUSPEND_POLICY_CHANGED = "com.droiddeck.launcher.SUSPEND_POLICY_CHANGED"
private const val ACTION_ACTIVITY_VISIBLE = "com.droiddeck.launcher.ACTIVITY_VISIBLE"
private const val ACTION_ACTIVITY_HIDDEN = "com.droiddeck.launcher.ACTIVITY_HIDDEN"
@@ -2078,6 +2102,11 @@ class SessionService : Service() {
}.getOrDefault(false)
}
fun setMicEnabled(context: Context, enabled: Boolean) {
context.startService(Intent(context, SessionService::class.java).setAction(ACTION_MIC_CHANGED)
.putExtra("enabled", enabled))
}
fun suspendPolicyChanged(context: Context) {
if (!SessionState.running) return
context.startService(Intent(context, SessionService::class.java).setAction(ACTION_SUSPEND_POLICY_CHANGED))
@@ -46,6 +46,8 @@ object SessionState {
var activityDisplayId = 0
@Volatile
var runtimeDriver: String? = null
var micAvailable by mutableStateOf(false)
var micEnabled by mutableStateOf(false)
var suspended by mutableStateOf(false)
var resumeRequested by mutableStateOf(false)
var resuming by mutableStateOf(false)
@@ -208,6 +208,9 @@ class DrawerActions(
val onScreenButtonsVisible: Boolean,
val suspendPolicy: String,
val steamDownloadsInBackground: Boolean = false,
val micAvailable: Boolean = false,
val micEnabled: Boolean = false,
val onMicEnabled: (Boolean) -> Unit = {},
val backActionsInverted: Boolean,
val padType: String? = null,
val onPadType: (String) -> Unit = {},
@@ -669,6 +672,9 @@ internal fun SessionDrawer(open: Boolean, requestedPage: SessionDrawerPage, cont
}
}
SettingsGroup(stringResource(R.string.drawer_session_behavior)) {
if (a.micAvailable) ToggleRow(host, "microphone", stringResource(R.string.mode_mic),
stringResource(R.string.drawer_mic_hint), a.micEnabled,
chipModifier = focus.track(page, "microphone"), onChange = a.onMicEnabled)
ChoiceRow(
host, "suspend", stringResource(R.string.mode_suspend),
null,
+2
View File
@@ -389,6 +389,8 @@
<string name="mode_directaudio_hint">Bypasses PulseAudio for lower latency.</string>
<string name="mode_mic">Microphone</string>
<string name="mode_mic_hint">For voice chat.</string>
<string name="drawer_mic_hint">Turn microphone capture on or off for this session.</string>
<string name="drawer_mic_failed">Could not change the microphone. Please try again.</string>
<string name="mode_network">Network</string>
<string name="mode_wifi_discovery">Wi-Fi discovery</string>
<string name="mode_wifi_discovery_hint">Show Wi-Fi names and nearby networks in Steam.</string>
@@ -0,0 +1,86 @@
package com.droiddeck.launcher.audio
import org.junit.Assert.*
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.rules.TemporaryFolder
import org.robolectric.RuntimeEnvironment
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import org.robolectric.util.ReflectionHelpers
import java.io.File
import java.util.function.Consumer
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28])
class DirectAudioMicrophoneTest {
@get:Rule val folder = TemporaryFolder()
@Test fun toggleOnlyChangesTheSessionCaptureControl() {
val fifo = File(folder.root, "mic.fifo")
val relay = DirectAudioRelayComponent(File(folder.root, "relay.sock"), fifo)
val changes = mutableListOf<Boolean>()
relay.onMicEnabled = Consumer { changes.add(it) }
val control = File(folder.root, "mic-enabled")
assertTrue(relay.setMicEnabled(false))
assertEquals("0", control.readText())
assertTrue(changes.isEmpty())
log(relay, "DA-Relay: mic enabled=0")
assertEquals(listOf(false), changes)
assertTrue(relay.setMicEnabled(true))
assertEquals("1", control.readText())
log(relay, "DA-Relay: mic enabled=1")
assertEquals(listOf(false, true), changes)
assertFalse(fifo.exists())
}
@Test fun sessionWithoutCaptureCannotEnableIt() {
val relay = DirectAudioRelayComponent(File(folder.root, "relay.sock"), null)
assertFalse(relay.setMicEnabled(true))
assertFalse(File(folder.root, "mic-enabled").exists())
}
@Test fun captureDemandDoesNotOverrideTheAcknowledgedToggle() {
val relay = DirectAudioRelayComponent(File(folder.root, "relay.sock"), File(folder.root, "mic.fifo"))
val changes = mutableListOf<Boolean>()
relay.onMicEnabled = Consumer { changes.add(it) }
log(relay, "DA-Relay: mic enabled=0")
log(relay, "DA-Relay: mic start (game wants it, 1 consumer(s))")
log(relay, "DA-Relay: mic stop (game)")
assertEquals(listOf(false), changes)
assertTrue(relay.awaitMicReleased(0))
}
@Test fun controlWriteFailureStillStartsPlaybackWithCaptureDisabled() {
val context = RuntimeEnvironment.getApplication()
val info = context.applicationInfo
val previous = info.nativeLibraryDir
val nativeDir = folder.newFolder("native")
val args = File(folder.root, "arguments")
File(nativeDir, DirectAudioRelayComponent.BINARY).apply {
writeText("#!/bin/sh\nprintf '%s\\n' \"\$@\" > '${args.path}.new'\nmv '${args.path}.new' '${args.path}'\n")
setExecutable(true)
}
val fifo = folder.newFile("mic.fifo")
File(folder.root, "mic-enabled.new").mkdir()
val relay = DirectAudioRelayComponent(File(folder.root, "relay.sock"), fifo)
relay.attach(context)
try {
info.nativeLibraryDir = nativeDir.path
relay.start()
val deadline = System.nanoTime() + 3_000_000_000L
while (!args.isFile && System.nanoTime() < deadline) Thread.sleep(10)
assertTrue("Playback helper did not start", args.isFile)
val command = args.readLines()
assertTrue(command.contains("--socket"))
assertFalse(command.contains("--mic-fifo"))
assertEquals(listOf("--mic-control", "/dev/null"), command.takeLast(2))
} finally {
info.nativeLibraryDir = previous
}
}
private fun log(relay: DirectAudioRelayComponent, line: String) =
ReflectionHelpers.callInstanceMethod<Unit>(relay, "trackMic", ReflectionHelpers.ClassParameter.from(String::class.java, line))
}
@@ -9,6 +9,7 @@ import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.compose.ui.test.*
import androidx.compose.ui.semantics.SemanticsProperties.StateDescription
import androidx.compose.runtime.MutableState
import com.droiddeck.launcher.R
import com.droiddeck.launcher.SessionActivity
@@ -97,6 +98,42 @@ class SessionSecondScreenTest {
}
}
@Test fun sessionMicrophoneCanBeTurnedOff() {
val changes = mutableListOf<Boolean>()
compose.setContent {
DroidDeckTheme {
SessionDrawer(true, SessionDrawerPage.SESSION, false, {},
actions(true, emptyList(), micAvailable = true, micEnabled = true, onMic = changes::add))
}
}
compose.onNodeWithTag("setting-microphone").assertIsEnabled()
.assert(SemanticsMatcher.expectValue(StateDescription, context.getString(R.string.widgets_on))).performClick()
assertEquals(listOf(false), changes)
}
@Test fun sessionMicrophoneCanBeTurnedBackOn() {
val changes = mutableListOf<Boolean>()
compose.setContent {
DroidDeckTheme {
SessionDrawer(true, SessionDrawerPage.SESSION, false, {},
actions(true, emptyList(), micAvailable = true, onMic = changes::add))
}
}
compose.onNodeWithTag("setting-microphone").assertIsEnabled()
.assert(SemanticsMatcher.expectValue(StateDescription, context.getString(R.string.widgets_off))).performClick()
assertEquals(listOf(true), changes)
}
@Test fun sessionWithoutMicrophoneOmitsCaptureControl() {
compose.setContent {
DroidDeckTheme {
SessionDrawer(true, SessionDrawerPage.SESSION, false, {}, actions(false, emptyList()))
}
}
compose.onNodeWithTag("setting-microphone").assertDoesNotExist()
compose.onNodeWithText(context.getString(R.string.mode_mic)).assertDoesNotExist()
}
private fun checkModes(steam: Boolean) {
SessionState.mode = if (steam) SessionService.MODE_STEAM else SessionService.MODE_DESKTOP
val selected = mutableListOf<SecondScreenMode>()
@@ -145,8 +182,11 @@ class SessionSecondScreenTest {
if (view is ViewGroup) for (index in 0 until view.childCount) yieldAll(descendants(view.getChildAt(index)))
}
private fun actions(steam: Boolean, displays: List<SecondScreenDisplay>, select: (SecondScreenMode) -> Unit = {}) =
private fun actions(steam: Boolean, displays: List<SecondScreenDisplay>,
micAvailable: Boolean = false, micEnabled: Boolean = false, onMic: (Boolean) -> Unit = {},
select: (SecondScreenMode) -> Unit = {}) =
DrawerActions(steam = steam, isHomeApp = false, androidApps = emptyList(), hudOn = false,
micAvailable = micAvailable, micEnabled = micEnabled, onMicEnabled = onMic,
frameGen = FrameGen.Mode.OFF, lossless = Lossless.State.NONE, oscMode = "never",
onScreenButtonsVisible = false, suspendPolicy = "auto", backActionsInverted = false,
touchMode = "auto", touchAuto = "trackpad", fexPreset = "default",