Audio: DirectAudio for games, and the microphone, from Bannerlator's Linux session

Two opt-ins on a new Audio screen, both ported from Bannerlator's gamescope
runtime where they are proven with voice in Steam's own tester.

DirectAudio replaces the audio driver inside the games the client launches -
Wine's mmdevapi driver, glibc build - with one that talks to a helper on the
Android side over a unix socket. The client keeps PulseAudio either way: it is
a native Linux program with no other way to make a sound. The helper runs here
because nothing inside the session can open Android's audio: a game is a glibc
process in a rootfs that binds no /system, and the uid the helper runs under is
what the recording permission is checked against.

The microphone is its own opt-in. The helper opens the input stream only when
asked and writes it to a pipe; PulseAudio's module-pipe-source turns that into
a source the client sees as DirectAudioMic. The daemon makes the pipe and the
helper waits for it - the two used to race, and the module fails on EEXIST.

The Proton wrappers gain Bannerlator's bl_directaudio, which adds the driver's
directory to WINEDLLPATH and names the driver in the prefix, and refuses on any
Proton that is not Wine 11 rather than pair a driver with an interface it was
not built against, which would be silence rather than an error. bl_game_cpus
comes along in the same splice and is inert until BL_GAME_CPUS is set.

The driver is staged into the runtime at every launch like the session scripts;
the helper ships as libdirectaudiorelay.so beside the PulseAudio daemon. Both
are the binaries Bannerlator carries, built by The412Banner/directaudio's relay
workflow (linux-wine11 build against ValveSoftware/wine proton_11.0).
This commit is contained in:
The412Banner committed 2026-09-21 09:13:33 -04:00
1 parent 3182ba780e
commit f542e59a20
17 files changed
+534 -11

No files matched your search

@@ -33,6 +33,7 @@ import com.steamdeck.launcher.session.ProtonExtras
import com.steamdeck.launcher.session.SessionPrefs
import com.steamdeck.launcher.ui.ProtonDialog
import com.steamdeck.launcher.ui.ProtonRow
import com.steamdeck.launcher.ui.AudioDialog
import com.steamdeck.launcher.ui.DriverDialog
import com.steamdeck.launcher.ui.DriverRow
import com.steamdeck.launcher.ui.ConfirmDialog
@@ -73,6 +74,9 @@ class MainActivity : ComponentActivity() {
private var offline by mutableStateOf(false)
private var protonRows by mutableStateOf<List<ProtonRow>>(emptyList())
private var showDrivers by mutableStateOf(false)
private var showAudio by mutableStateOf(false)
private var directAudio by mutableStateOf(false)
private var mic by mutableStateOf(false)
private var linuxRows by mutableStateOf<List<DriverRow>>(emptyList())
private var linuxSteam by mutableStateOf("")
private var linuxDesktop by mutableStateOf("")
@@ -116,6 +120,7 @@ class MainActivity : ComponentActivity() {
onFrameGen = { showFrameGen = true },
onProtons = { refreshProtons(); showProtons = true },
onDrivers = { refreshDrivers(); showDrivers = true },
onAudio = { directAudio = SessionPrefs.directAudio(this); mic = SessionPrefs.micEnabled(this); showAudio = true },
onCredits = { showCredits = true },
)
if (showApps) DesktopAppsDialog(
@@ -142,6 +147,20 @@ class MainActivity : ComponentActivity() {
onRemoveAndroid = { id -> TurnipDriver(this).remove(id); refreshDrivers() },
onDismiss = { showDrivers = false },
)
if (showAudio) AudioDialog(
directAudio = directAudio, mic = mic,
onDirectAudio = { on -> SessionPrefs.setDirectAudio(this, on); directAudio = on },
onMic = { on ->
SessionPrefs.setMicEnabled(this, on)
mic = on
// The session checks the grant itself at start; asking here means the
// answer is in before the first session that wants it.
if (on && checkSelfPermission(Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(arrayOf(Manifest.permission.RECORD_AUDIO), 2)
}
},
onDismiss = { showAudio = false },
)
if (showRemove) ConfirmDialog(
title = "Remove Linux runtime",
text = "This deletes the runtime, the Steam client inside it, and every game installed there.",
@@ -0,0 +1,108 @@
package com.steamdeck.launcher.audio;
import android.os.Process;
import android.util.Log;
import com.steamdeck.launcher.core.EnvironmentComponent;
import com.steamdeck.launcher.core.ProcessHelper;
import java.io.File;
import java.util.ArrayList;
/**
* The DirectAudio relay helper, which owns Android's speaker and microphone on behalf of a Linux
* session.
*
* <p>Nothing inside the session can open Android's audio itself. A game is a glibc process in a
* rootfs that binds no {@code /system}, and a glibc process cannot call libaaudio at all; the
* session is also never given a way to spawn a bionic helper of its own. So the helper runs out
* here, under the app's own uid, and the driver inside Wine talks to it over a unix socket. The uid
* matters for more than tidiness: Android checks the microphone permission against whoever opens
* the stream, so a helper started any other way could play but never record.
*
* <p>It is executed from the app's native library directory because Android will not execute a
* binary from anywhere else - the same reason the PulseAudio daemon ships as {@code
* libpulseaudio.so} despite being a program rather than a library.
*
* <p>Started before the client, because the driver asks once whether a helper is there, when Wine
* picks an audio driver, and reports itself unavailable if not. Ported from Bannerlator (GPL-3.0);
* the helper binary is the one built by The412Banner/directaudio's relay workflow.
*/
public class DirectAudioRelayComponent extends EnvironmentComponent {
private static final String TAG = "DirectAudioRelay";
/** The helper, named as a library so Android will run it. */
public static final String BINARY = "libdirectaudiorelay.so";
private final File socketPath;
/** Null when the microphone was not asked for; the helper then opens no input stream. */
private final File micFifoPath;
private int pid = -1;
public DirectAudioRelayComponent(File socketPath, File micFifoPath) {
this.socketPath = socketPath;
this.micFifoPath = micFifoPath;
}
@Override
public void start() {
stop();
File binary = new File(context.getApplicationInfo().nativeLibraryDir, BINARY);
if (!binary.isFile()) {
Log.w(TAG, "helper missing at " + binary + "; DirectAudio will report itself unavailable");
return;
}
File parent = socketPath.getParentFile();
if (parent != null) {
//noinspection ResultOfMethodCallIgnored
parent.mkdirs();
}
// A socket left by a session that did not shut down cleanly is not a listener, and the
// helper would fail to bind onto it.
//noinspection ResultOfMethodCallIgnored
socketPath.delete();
// PulseAudio's pipe module insists on creating the pipe itself and fails if one is already
// there. The helper creates one too when the path is missing, and it starts a few
// milliseconds after the daemon is exec'd - well before the daemon has read its config. So
// left to itself the helper won its own race, the daemon's mkfifo failed with EEXIST, and
// the microphone was lost while everything else came up. Wait for the daemon to make the
// pipe before the helper runs; it appears within a second, and if it never does the helper
// starts anyway and creates it, which is the right thing when the daemon is not involved.
if (micFifoPath != null) {
long deadline = System.currentTimeMillis() + 3000L;
while (!micFifoPath.exists() && System.currentTimeMillis() < deadline) {
try {
Thread.sleep(50L);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
}
Log.i(TAG, micFifoPath.exists() ? "microphone pipe made by the daemon; starting helper"
: "daemon did not make the microphone pipe in 3 s; helper will make it");
}
StringBuilder command = new StringBuilder(binary.getAbsolutePath());
command.append(" --socket ").append(socketPath.getAbsolutePath());
if (micFifoPath != null) {
// The helper creates the pipe if it is not there. PulseAudio is told the same path and
// reads from the other end of it.
command.append(" --mic-fifo ").append(micFifoPath.getAbsolutePath());
}
ArrayList<String> env = new ArrayList<>();
env.add("HOME=" + context.getFilesDir());
pid = ProcessHelper.exec(command.toString(), env.toArray(new String[0]), context.getFilesDir());
Log.i(TAG, "started pid=" + pid + " socket=" + socketPath
+ (micFifoPath != null ? " mic=" + micFifoPath : " (no microphone)"));
}
@Override
public void stop() {
if (pid != -1) {
// The helper exits on a signal and tears down its streams; there is nothing below it to
// orphan, unlike the proot tree.
Process.killProcess(pid);
pid = -1;
}
}
}
@@ -32,10 +32,23 @@ public class PulseAudioComponent extends EnvironmentComponent {
public static final String SOCKET_NAME = "PS0";
private final File workingDir;
/**
* A named pipe carrying microphone audio, or null for no microphone. The bundle ships
* module-aaudio-sink but no matching source, and the daemon runs where Android permits
* recording - so pointing module-pipe-source at a pipe the DirectAudio relay helper writes
* turns that one stream into a source the client can see, named DirectAudioMic.
*/
private final String micFifoPath;
private int pid = -1;
public PulseAudioComponent(Context context) {
this(context, null);
}
/** As above, with a microphone fed from {@code micFifoPath}; null for output only. */
public PulseAudioComponent(Context context, String micFifoPath) {
this.workingDir = new File(context.getFilesDir(), "pulseaudio");
this.micFifoPath = micFifoPath;
}
public File socket() {
@@ -60,13 +73,32 @@ public class PulseAudioComponent extends EnvironmentComponent {
//noinspection ResultOfMethodCallIgnored
socket().delete();
FileUtils.writeString(new File(workingDir, "default.pa"), String.join("\n",
"load-module module-native-protocol-unix auth-anonymous=1 auth-cookie-enabled=0 socket=\""
+ socket().getAbsolutePath() + "\"",
// volume=1.0 is not optional: with no volume argument module-aaudio-sink defaults
// the sink to 0% and the session plays silence.
"load-module module-aaudio-sink performance_mode=1 adaptive=1 volume=1.0",
"set-default-sink AAudioSink"));
// module-pipe-source creates the pipe with mkfifo and fails outright if one is already
// there - EEXIST, reported as "Unknown error 17" - and the module then does not load at
// all, so the source never appears and the client reports no microphone. Ours lives in the
// app's files directory and survives a session, so after the very first run the path would
// always be occupied. Removed here, before the daemon reads this config: the daemon makes
// it, and the relay helper starts afterwards and is content to find one already made.
if (micFifoPath != null && !micFifoPath.isEmpty()) {
//noinspection ResultOfMethodCallIgnored
new File(micFifoPath).delete();
}
ArrayList<String> config = new ArrayList<>();
config.add("load-module module-native-protocol-unix auth-anonymous=1 auth-cookie-enabled=0 socket=\""
+ socket().getAbsolutePath() + "\"");
// volume=1.0 is not optional: with no volume argument module-aaudio-sink defaults
// the sink to 0% and the session plays silence.
config.add("load-module module-aaudio-sink performance_mode=1 adaptive=1 volume=1.0");
config.add("set-default-sink AAudioSink");
if (micFifoPath != null && !micFifoPath.isEmpty()) {
// The format is the helper's, fixed at s16le/48000/mono: it resamples when the device
// grants another input rate, so the daemon is never told a rate the bytes are not.
// A pipe has no clock, so nothing here corrects drift - acceptable for voice.
config.add("load-module module-pipe-source source_name=DirectAudioMic file=\""
+ micFifoPath + "\" format=s16le rate=48000 channels=1");
config.add("set-default-source DirectAudioMic");
}
FileUtils.writeString(new File(workingDir, "default.pa"), String.join("\n", config));
File modules = new File(workingDir, "modules/arm64");
ArrayList<String> env = new ArrayList<>();
@@ -11,6 +11,8 @@ import java.io.File
object SessionFiles {
private const val TAG = "SessionFiles"
private const val NO_PAD_SWITCH = "Download/steamdeck-no-pad"
/** Where the DirectAudio driver lives inside the runtime; the wrappers get it as BL_DIRECTAUDIO. */
const val DIRECTAUDIO_DIR = "usr/local/lib/directaudio"
/**
* The libraries and scripts the session runs, refreshed from the apk at every launch.
@@ -59,6 +61,31 @@ object SessionFiles {
}
if (!installed) Log.e(TAG, "$relative NOT staged")
}
// The DirectAudio driver for games under Proton: the glibc build of winedirectaudio, which
// the Proton wrappers add to WINEDLLPATH when the session asks for it (BL_DIRECTAUDIO).
// Staged like the scripts, so a driver fix reaches an installed runtime without re-hosting.
val directAudio = arrayOf(
"aarch64-unix/winedirectaudio.so",
"aarch64-windows/winedirectaudio.drv",
"i386-windows/winedirectaudio.drv",
)
for (relative in directAudio) {
val target = File(root, "$DIRECTAUDIO_DIR/lib/wine/$relative")
val staged = File(target.parentFile, target.name + ".staged")
var installed = false
try {
target.parentFile?.mkdirs()
context.assets.open("directaudio/linux-wine11/$relative").use { input ->
staged.outputStream().use { output -> FileUtils.copy(input, output) }
}
installed = staged.setReadable(true, false) && staged.renameTo(target)
} catch (e: Exception) {
Log.w(TAG, "could not stage DirectAudio $relative", e)
} finally {
if (!installed) staged.delete()
}
if (!installed) Log.e(TAG, "DirectAudio $relative NOT staged")
}
// What every process in the session preloads. LD_PRELOAD in the environment would not
// survive: the Steam client rebuilds it for each process it starts and appends its own
// overlay entry without a separator, which silently drops whatever was there.
@@ -63,6 +63,23 @@ object SessionPrefs {
prefs(context).edit().putString("linuxDriver.$mode", id).apply()
}
/**
* DirectAudio for the games the client launches: Wine's audio driver inside them replaced by
* ours, which talks to a helper on this side. The client itself keeps PulseAudio either way.
*/
fun directAudio(context: Context): Boolean = prefs(context).getBoolean("directAudio", false)
fun setDirectAudio(context: Context, on: Boolean) {
prefs(context).edit().putBoolean("directAudio", on).apply()
}
/** The microphone, its own opt-in: the helper opens an input stream only when asked. */
fun micEnabled(context: Context): Boolean = prefs(context).getBoolean("mic", false)
fun setMicEnabled(context: Context, on: Boolean) {
prefs(context).edit().putBoolean("mic", on).apply()
}
/** The Android driver the compositor loads: "" = pick by GPU, else a bundled or imported id. */
@JvmStatic
fun androidDriver(context: Context): String = prefs(context).getString("androidDriver", "") ?: ""
@@ -10,6 +10,7 @@ import android.app.PendingIntent
import android.app.Service
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.wifi.WifiManager
import android.os.Build
import android.os.Environment
@@ -18,6 +19,7 @@ import android.os.PowerManager
import android.util.Log
import com.steamdeck.launcher.R
import com.steamdeck.launcher.SessionActivity
import com.steamdeck.launcher.audio.DirectAudioRelayComponent
import com.steamdeck.launcher.audio.PulseAudioComponent
import com.steamdeck.launcher.core.EnvVars
import com.steamdeck.launcher.core.EnvironmentComponent
@@ -133,10 +135,41 @@ class SessionService : Service() {
// its authors advise for those GPUs and what nothing else in the list needs.
tuDebug(linuxDriverId)?.let { guest.add("TU_DEBUG=$it") }
val pulse = PulseAudioComponent(this)
// PulseAudio always: the client is a native Linux program and has no other way to make a
// sound - its menus, its music and its voice chat all go through here. DirectAudio is not
// an alternative to it on this path: it replaces the audio driver INSIDE Wine, so it
// changes what games do and leaves the client alone. The microphone is its own opt-in on
// top, and the helper only opens an input stream when asked - so a user who wants game
// sound but no recording gets exactly that, and Android's recording indicator stays off.
val wantsDirectAudio = SessionState.mode == MODE_STEAM && SessionPrefs.directAudio(this)
val wantsMic = SessionPrefs.micEnabled(this) &&
checkSelfPermission(android.Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED
// Both paths sit under the app's files directory, which the session binds at its own path,
// so the same string is valid on both sides and nothing has to be translated.
val audioDir = File(filesDir, "directaudio").apply { mkdirs() }
val relaySocket = File(audioDir, "relay.sock")
val micFifo = if (wantsMic) File(audioDir, "mic.fifo") else null
val pulse = PulseAudioComponent(this, micFifo?.absolutePath)
pulse.setContext(this)
guest.add("PULSE_SERVER=unix:" + pulse.socket().absolutePath)
components.add(pulse)
if (wantsDirectAudio || wantsMic) {
// After the daemon in the list, so it can wait for the pipe the daemon makes.
val relay = DirectAudioRelayComponent(relaySocket, micFifo)
relay.setContext(this)
components.add(relay)
}
if (wantsDirectAudio) {
// Read by the Proton wrappers, which point Wine at the driver and name it in the
// prefix. Absent, they take an early return and the game uses Proton's own audio - so
// this variable is the whole of the selection.
guest.add("BL_DIRECTAUDIO=/" + SessionFiles.DIRECTAUDIO_DIR)
guest.add("BANNER_AUDIO_DIRECT_RELAY=" + relaySocket.absolutePath)
}
Log.i(TAG, "audio: PulseAudio" + (if (wantsDirectAudio) " + DirectAudio for games" else "")
+ (if (wantsMic) " + microphone" else "") +
(if (SessionPrefs.micEnabled(this) && !wantsMic) " (microphone wanted but RECORD_AUDIO not granted)" else ""))
guest.add("BL_WIDTH=" + size.first)
guest.add("BL_HEIGHT=" + size.second)
@@ -0,0 +1,69 @@
package com.steamdeck.launcher.ui
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
/**
* The two audio opt-ins. The client always has PulseAudio; these change what the games get and
* whether anything can record. Both apply at the next session start.
*/
@Composable
fun AudioDialog(
directAudio: Boolean,
mic: Boolean,
onDirectAudio: (Boolean) -> Unit,
onMic: (Boolean) -> Unit,
onDismiss: () -> Unit,
) {
AlertDialog(
onDismissRequest = onDismiss,
title = { Text("Audio") },
text = {
Column {
Text(
"The Steam client itself always plays through PulseAudio. These two change what happens around it, at the next session start.",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
Spacer(Modifier.height(12.dp))
Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth().padding(vertical = 4.dp)) {
Column(modifier = Modifier.weight(1f)) {
Text("DirectAudio for games", style = MaterialTheme.typography.bodyMedium)
Text(
"Replaces Wine's audio driver inside the games the client launches with one that talks straight to Android — lower latency, no PulseAudio in between. Wine 11 Protons only (Valve's ARM64, GE, cachyos); on anything else it stays off rather than play silence. A game's first launch after turning this on still uses Proton's own audio; the second uses this.",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
}
Switch(checked = directAudio, onCheckedChange = onDirectAudio)
}
Spacer(Modifier.height(8.dp))
Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth().padding(vertical = 4.dp)) {
Column(modifier = Modifier.weight(1f)) {
Text("Microphone", style = MaterialTheme.typography.bodyMedium)
Text(
"Gives the session Android's microphone: the Steam client shows an input device named DirectAudioMic, for voice chat. Asks for the recording permission; with it off, nothing in the session can record and Android's indicator stays off.",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
}
Switch(checked = mic, onCheckedChange = onMic)
}
}
},
confirmButton = { TextButton(onClick = onDismiss) { Text("OK") } },
)
}
@@ -67,6 +67,7 @@ fun MainScreen(
onFrameGen: () -> Unit,
onProtons: () -> Unit,
onDrivers: () -> Unit,
onAudio: () -> Unit,
onOffline: () -> Unit,
onCredits: () -> Unit,
) {
@@ -182,6 +183,10 @@ fun MainScreen(
onClick = onDrivers,
modifier = Modifier.fillMaxWidth().padding(top = 5.dp * k).height(buttonHeight + 5.dp * k),
) { Text("Graphics drivers", fontSize = 13.sp * k) }
OutlinedButton(
onClick = onAudio,
modifier = Modifier.fillMaxWidth().padding(top = 5.dp * k).height(buttonHeight + 5.dp * k),
) { Text("Audio", fontSize = 13.sp * k) }
Spacer(Modifier.height(10.dp * k))
Text(
"by The412Banner and maxjivi05 · credits",