This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-10-13 21:27:42 -04:00
1 parent 5fd47421f9
commit aecd2ebe1d
852 files changed
+4844 -46144

No files matched your search

@@ -9,34 +9,12 @@ import com.droiddeck.launcher.core.HostProcess;
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 SessionPart {
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;
/** Shared with PulseAudioComponent: one audio.log per session, appended by both. */
private File logFile;
/** Null when the microphone was not asked for; the helper then opens no input stream. */
private final File micFifoPath;
private volatile int pid = -1;
@@ -45,7 +23,6 @@ public class DirectAudioRelayComponent extends SessionPart {
this.micFifoPath = micFifoPath;
}
/** Send the helper's output to this file as well as logcat. Set before {@link #start()}. */
public void setLogFile(File file) {
this.logFile = file;
}
@@ -60,21 +37,10 @@ public class DirectAudioRelayComponent extends SessionPart {
}
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) {
@@ -91,12 +57,8 @@ public class DirectAudioRelayComponent extends SessionPart {
StringBuilder command = new StringBuilder(binary.getAbsolutePath());
command.append(" --socket ").append(socketPath.getAbsolutePath());
// Diagnostics into the session's audio.log: every 1000 callbacks the device buffer,
// Android's own xrun count and the ring's fill, plus every buffer change.
command.append(" --log");
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<>();
@@ -126,8 +88,6 @@ public class DirectAudioRelayComponent extends SessionPart {
@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;
}
@@ -17,30 +17,15 @@ import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
/**
* PulseAudio 13.0 running in the app process, with Android's AAudio as its sink, so the guest's
* libpulse clients (Steam and everything it launches) have a server to talk to. The socket lives
* in the app's files directory and is bound into the session at its own path, so PULSE_SERVER
* needs no translating.
*/
public class PulseAudioComponent extends SessionPart {
private static final String TAG = "PulseAudio";
/** Where the guest reaches the daemon; the session exports PULSE_SERVER=unix:<this>. */
public static final String SOCKET_NAME = "PS0";
/** Identifies the bundled pulseaudio.tzst; a change here re-unpacks it over what a device has. */
private static final String BUNDLE_STAMP = "2026-10-08-pa13-directaudio-native";
private final File workingDir;
/** Where the daemon's own output is kept for this session, or null for logcat only. */
private File logFile;
/**
* 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 volatile int pid = -1;
@@ -48,13 +33,11 @@ public class PulseAudioComponent extends SessionPart {
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;
}
/** CRC-32 of the bundled pulseaudio.tzst, or "?" if it cannot be read. About 600 KB; cheap. */
private String bundleChecksum() {
java.util.zip.CRC32 crc = new java.util.zip.CRC32();
byte[] buf = new byte[64 * 1024];
@@ -67,7 +50,6 @@ public class PulseAudioComponent extends SessionPart {
return Long.toHexString(crc.getValue());
}
/** Send the daemon's output to this file as well as logcat. Set before {@link #start()}. */
public void setLogFile(File file) {
this.logFile = file;
}
@@ -80,18 +62,9 @@ public class PulseAudioComponent extends SessionPart {
public void start() {
stop();
if (!workingDir.isDirectory()) {
//noinspection ResultOfMethodCallIgnored
workingDir.mkdirs();
FileUtils.chmod(workingDir, 0771);
}
// The loadable modules (module-aaudio-sink - the app's own, from tools/aaudio-sink - the
// native protocol, the pipe modules) ride in the apk; the daemon and its libraries come from the native library directory, the one
// place an app may execute a file from. The bundle is unpacked once per BUNDLE_STAMP, not
// once ever: an installed app kept the modules it unpacked on its first run, so a bundle
// fixed in a later build never reached the device - which is how a 17.0 glibc build of
// module-pipe-source sat beside the 13.0 daemon, failed to dlopen, and the microphone
// never appeared. The stamp carries a checksum of the bundle itself, so any change to it -
// a rebuilt sink module included - re-unpacks, whether or not BUNDLE_STAMP was bumped.
File modulesDir = new File(workingDir, "modules");
File stamp = new File(modulesDir, ".bundle");
String have = FileUtils.readString(stamp);
@@ -112,38 +85,18 @@ public class PulseAudioComponent extends SessionPart {
if (pactl.isFile()) FileUtils.chmod(pactl, 0771);
copyFromLibraryDir();
//noinspection ResultOfMethodCallIgnored
socket().delete();
// 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.
// The sink's name is what the client's Audio settings show as the output device, so it
// says which road the sound takes.
// The client's sound: DirectAudio's own engine, in this process, one step from Android.
// decay=0: shrinking the device buffer after a quiet spell probes below what works and that
// probe is a click; the client plays menus. Between .nofail and .fail a module that cannot
// open its stream does not take the daemon down; ensureClientSink() then loads the plain
// AAudio sink in its place.
config.add(".nofail");
config.add("load-module module-directaudio-native-sink sink_name=DirectAudio performance_mode=1 adaptive=1 decay=0 limiter=1 watchdog=1 volume=1.0");
config.add("set-default-sink DirectAudio");
config.add(".fail");
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");
@@ -159,12 +112,7 @@ public class PulseAudioComponent extends SessionPart {
String command = workingDir.getAbsolutePath() + "/libpulseaudio.so"
+ " --system=false --disable-shm=true --fail=false"
+ " -n --file=default.pa --daemonize=false --use-pid-file=false --exit-idle-time=-1"
// Info level: the classic sink reports the stream Android granted (burst, capacity,
// starting buffer) only there, and that is what a stutter report needs.
+ " --log-level=info";
// A module that refuses to load, a pipe that could not be made, the daemon exiting at
// startup: all of it used to reach logcat and nothing else, so a user's folder said nothing
// at all about sound. Four separate faults hid behind "no input device" in one night.
final java.io.PrintWriter out = openLog();
pid = HostProcess.start(command, env.toArray(new String[0]), workingDir, null,
line -> {
@@ -176,11 +124,6 @@ public class PulseAudioComponent extends SessionPart {
guard.start();
}
/**
* The fallback: once the daemon answers, if the DirectAudio sink is not
* there (the module failed to load or to open its stream), load the plain AAudio sink so the
* client is never left without an output. Says so in the audio log either way.
*/
private void ensureClientSink(java.io.PrintWriter out) {
try {
long deadline = System.currentTimeMillis() + 15000L;
@@ -208,7 +151,6 @@ public class PulseAudioComponent extends SessionPart {
}
}
/** Runs pactl against this daemon; its stdout, or null when it failed or timed out. */
private String pactl(String... args) {
File pactl = new File(workingDir, "pactl");
if (!pactl.isFile() || pid <= 1) return null;
@@ -298,7 +240,6 @@ public class PulseAudioComponent extends SessionPart {
}
}
/** The session's audio log, appended to by both the daemon and the relay helper. */
private java.io.PrintWriter openLog() {
if (logFile == null) return null;
try {