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

@@ -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<>();