diff --git a/README.md b/README.md index 24a3072f25..fc862761fb 100644 --- a/README.md +++ b/README.md @@ -172,6 +172,30 @@ nothing. The client rewrites those keys when it exits, so the app writes them ag session start. Games with always-online DRM will not start offline, and nothing can be installed or updated. +## Audio + +The Steam client always plays through **PulseAudio** — it is a native Linux program with no other +way to make a sound, and its menus, music and voice chat all go through it. **Audio** on the main +screen adds two things on top, each applying at the next session start: + +- **DirectAudio for games** — replaces Wine's audio driver *inside the games the client launches* + with Bannerlator's DirectAudio, which talks straight to Android over a socket instead of through + PulseAudio: lower latency, and no resampling in between. The client is untouched. It only pairs + with a Wine 11 Proton (Valve's ARM64 builds, GE-Proton, proton-cachyos); on anything else the + wrapper leaves it off and says so in the log, because a mismatch would be silence rather than an + error. A game's first launch after turning it on still uses Proton's own audio — the driver is + named in the game's prefix, which does not exist until that first launch — and the second uses + DirectAudio. +- **Microphone** — the relay helper opens Android's input stream under the app's own uid (which is + what the recording permission is checked against) and PulseAudio exposes it to the client as a + source named **DirectAudioMic**, so Steam voice chat works. Asks for the recording permission when + turned on; off, nothing in the session can record and Android's indicator stays off. + +The helper is a small Android program the app runs beside the session; the driver is a glibc build +staged into the runtime at every launch like the session scripts, so a fix reaches an installed +runtime without re-hosting it. Proven in Bannerlator's Linux session with voice in Steam's own +tester; ⚠️ not yet exercised in this app. + ## Compatibility tools **Compatibility tools** (main screen and drawer) installs GE-Proton or proton-cachyos — native diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 3dd7ae4e0d..895c35c55c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -17,6 +17,10 @@ itself with the phone via boot receivers and signs this client out seconds after every login; a normal permission, and it cannot touch anything in the foreground. --> + + diff --git a/app/src/main/assets/directaudio/linux-wine11/README.md b/app/src/main/assets/directaudio/linux-wine11/README.md new file mode 100644 index 0000000000..7a7935c407 --- /dev/null +++ b/app/src/main/assets/directaudio/linux-wine11/README.md @@ -0,0 +1,115 @@ +# DirectAudio for the Linux Steam client (relay build) + +This is the driver for games that run under **Valve's ARM64 Proton inside a Linux +runtime on Android** - Bannerlator's linuxfs session with the native Steam client - +where the game process is a glibc process and cannot touch Android's audio +libraries itself. + +Two pieces, both required: + +| piece | what it is | where it runs | +|---|---|---| +| `aarch64-unix/winedirectaudio.so` + the two PE shells | the Wine `mmdevapi` driver, glibc build, **no AAudio inside** | in the game, inside the Linux rootfs | +| `directaudio-relay` | a small bionic program that owns the AAudio output and microphone streams | on the Android side, under the app's uid | + +They meet over a unix socket. The driver connects, sends its launch config, and +gets back two shared-memory rings: one it fills with mixed game audio, one the +helper fills with microphone audio. The helper plays and records; the driver +keeps doing everything else it always did. + +## Which Proton + +Built inside `ValveSoftware/wine` at `proton_11.0`. The private `mmdevapi` +interface it implements is byte-identical across **Proton 11.0 (ARM64)**, +**Proton Experimental (ARM64)** and our own Wine-11 layers (checked 2026-09-19), +so this one build serves any of them. A future Proton on a Wine 12 base will +need a rebuild - the interface is not versioned, and a mismatch is silence, not +an error. + +## Installing the driver next to Proton (not into it) + +Steam verifies and updates its Proton depots, so files added under +`steamapps/common/Proton .../files/lib/wine/` will not survive. Put the driver +in a directory of its own and point Wine at it: + +``` +/directaudio/lib/wine/aarch64-unix/winedirectaudio.so +/directaudio/lib/wine/aarch64-windows/winedirectaudio.drv +/directaudio/lib/wine/i386-windows/winedirectaudio.drv +``` + +and set, for the game process: + +``` +WINEDLLPATH=/directaudio/lib/wine +``` + +Wine searches `WINEDLLPATH` entries with the per-arch subdirectories appended, +so both the PE shell and the unixlib are found there. In Bannerlator's runtime +the natural place to export it is the `bannerlator-proton` compatibility-tool +wrapper, which every Proton launch goes through. + +## Selecting the driver + +`mmdevapi` only tries the drivers named in the registry (or its built-in +`pulse,alsa,oss,coreaudio` list, which does not know about this one). Set, in the +game's prefix: + +``` +wine reg add "HKCU\Software\Wine\Drivers" /v Audio /d directaudio /f +``` + +Under Proton the prefix is `compatdata//pfx`; run the command with that +prefix as `WINEPREFIX` using Proton's own `bin-arm64/wine`, or write the key from +the wrapper before it hands off to Proton. + +## Running the helper + +``` +directaudio-relay --socket [--log] +``` + +- Run it **under the app's uid**, the way the app already runs its PulseAudio + daemon. Android checks `RECORD_AUDIO` against the calling uid, so a helper + started any other way can play but cannot record. +- Start it **before** the game: the driver asks "is a helper there?" once, when + Wine picks an audio driver, and falls back to unavailable if not. +- The socket path must be reachable from inside the Linux rootfs. Bannerlator + binds the app's files directory and its runtime directory into the session at + their own paths, so any socket under either works unchanged on both sides. +- The helper serves any number of game processes; each gets its own streams. + It exits on `SIGTERM`; a game exiting tears down that game's streams only. + +## Telling the driver where the helper is + +``` +BANNER_AUDIO_DIRECT_RELAY= +``` + +Unset, the driver looks for `$XDG_RUNTIME_DIR/directaudio-relay`. + +## Everything else is unchanged + +All `BANNER_AUDIO_DIRECT_*` knobs mean what they mean in the in-process build: +`_PERF`, `_ADAPTIVE`, `_DECAY`, `_MS`, `_MAXMS`, `_BF`, `_MBF`, `_EXCLUSIVE`, +`_WATCHDOG`, `_STALL_MS`, `_DECAY_*`, `_LOG`, `_PERIOD_MS`, `_MINPERIOD_MS`, and +the live-config mailbox `_RUNTIME` (the helper watches the file; it must be a +path the helper can read on the Android side). **`BANNER_AUDIO_DIRECT_MIC=1`** +exposes the capture endpoint exactly as before: the helper opens the input +stream (`VOICE_COMMUNICATION` preset) when the game connects and makes it hot on +the game's first capture start. + +One thing is different: there is one extra hand-off, the ring. The helper's +callback pulls a burst from it, so the driver keeps two bursts queued (about 8 ms +on a 192-frame device) on top of the AAudio buffer the presets control, and the +helper raises that if a callback ever finds the ring short. `get_latency` reports +the sum, so a game that sizes its own buffers from it still sees the truth. + +## Proving it is running + +- Helper side: `logcat -s DA-Relay:I` shows `hello from `, then `open: buffer + ... ring target ...`, then `capture open` / `capture start` for the mic. +- Game side: the session log carries `DirectAudio: relay: connected - ...`. +- The game process's `/proc//maps` shows `winedirectaudio.so` and **no** + `libaaudio.so` (it is in the helper's maps instead). The AudioFlinger track is + owned by the helper's pid. diff --git a/app/src/main/assets/directaudio/linux-wine11/aarch64-unix/winedirectaudio.so b/app/src/main/assets/directaudio/linux-wine11/aarch64-unix/winedirectaudio.so new file mode 100644 index 0000000000..7d3083052a Binary files /dev/null and b/app/src/main/assets/directaudio/linux-wine11/aarch64-unix/winedirectaudio.so differ diff --git a/app/src/main/assets/directaudio/linux-wine11/aarch64-windows/winedirectaudio.drv b/app/src/main/assets/directaudio/linux-wine11/aarch64-windows/winedirectaudio.drv new file mode 100644 index 0000000000..d078149dc9 Binary files /dev/null and b/app/src/main/assets/directaudio/linux-wine11/aarch64-windows/winedirectaudio.drv differ diff --git a/app/src/main/assets/directaudio/linux-wine11/i386-windows/winedirectaudio.drv b/app/src/main/assets/directaudio/linux-wine11/i386-windows/winedirectaudio.drv new file mode 100644 index 0000000000..93efd051f7 Binary files /dev/null and b/app/src/main/assets/directaudio/linux-wine11/i386-windows/winedirectaudio.drv differ diff --git a/app/src/main/assets/directaudio/linux-wine11/version.txt b/app/src/main/assets/directaudio/linux-wine11/version.txt new file mode 100644 index 0000000000..a32f892380 --- /dev/null +++ b/app/src/main/assets/directaudio/linux-wine11/version.txt @@ -0,0 +1,4 @@ +directaudio linux-wine11 relay build +source 3548a4a730e8d01cb0cb0268d51a2fb8f23dcd33 +wine ValveSoftware/wine@proton_11.0 +unixlib.h sha256 fdbed263338d0719c4d12e02b045c23721de33219ec8878e4c2d7ebc9efcbe09 diff --git a/app/src/main/java/com/steamdeck/launcher/MainActivity.kt b/app/src/main/java/com/steamdeck/launcher/MainActivity.kt index 29d57cf9d1..5f1537659a 100644 --- a/app/src/main/java/com/steamdeck/launcher/MainActivity.kt +++ b/app/src/main/java/com/steamdeck/launcher/MainActivity.kt @@ -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>(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>(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.", diff --git a/app/src/main/java/com/steamdeck/launcher/audio/DirectAudioRelayComponent.java b/app/src/main/java/com/steamdeck/launcher/audio/DirectAudioRelayComponent.java new file mode 100644 index 0000000000..9f872e1d43 --- /dev/null +++ b/app/src/main/java/com/steamdeck/launcher/audio/DirectAudioRelayComponent.java @@ -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. + * + *

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. + * + *

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. + * + *

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 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; + } + } +} diff --git a/app/src/main/java/com/steamdeck/launcher/audio/PulseAudioComponent.java b/app/src/main/java/com/steamdeck/launcher/audio/PulseAudioComponent.java index 54d696ffd3..7ea51058df 100644 --- a/app/src/main/java/com/steamdeck/launcher/audio/PulseAudioComponent.java +++ b/app/src/main/java/com/steamdeck/launcher/audio/PulseAudioComponent.java @@ -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 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 env = new ArrayList<>(); diff --git a/app/src/main/java/com/steamdeck/launcher/session/SessionFiles.kt b/app/src/main/java/com/steamdeck/launcher/session/SessionFiles.kt index 89094434c3..0719803d4b 100644 --- a/app/src/main/java/com/steamdeck/launcher/session/SessionFiles.kt +++ b/app/src/main/java/com/steamdeck/launcher/session/SessionFiles.kt @@ -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. diff --git a/app/src/main/java/com/steamdeck/launcher/session/SessionPrefs.kt b/app/src/main/java/com/steamdeck/launcher/session/SessionPrefs.kt index b8c74fea84..ae989e1f6a 100644 --- a/app/src/main/java/com/steamdeck/launcher/session/SessionPrefs.kt +++ b/app/src/main/java/com/steamdeck/launcher/session/SessionPrefs.kt @@ -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", "") ?: "" diff --git a/app/src/main/java/com/steamdeck/launcher/session/SessionService.kt b/app/src/main/java/com/steamdeck/launcher/session/SessionService.kt index 648c6ea04a..493df896cf 100644 --- a/app/src/main/java/com/steamdeck/launcher/session/SessionService.kt +++ b/app/src/main/java/com/steamdeck/launcher/session/SessionService.kt @@ -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) diff --git a/app/src/main/java/com/steamdeck/launcher/ui/AudioDialog.kt b/app/src/main/java/com/steamdeck/launcher/ui/AudioDialog.kt new file mode 100644 index 0000000000..c2a6d13259 --- /dev/null +++ b/app/src/main/java/com/steamdeck/launcher/ui/AudioDialog.kt @@ -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") } }, + ) +} diff --git a/app/src/main/java/com/steamdeck/launcher/ui/MainScreen.kt b/app/src/main/java/com/steamdeck/launcher/ui/MainScreen.kt index a51109338a..888b55c05a 100644 --- a/app/src/main/java/com/steamdeck/launcher/ui/MainScreen.kt +++ b/app/src/main/java/com/steamdeck/launcher/ui/MainScreen.kt @@ -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", diff --git a/app/src/main/jniLibs/arm64-v8a/libdirectaudiorelay.so b/app/src/main/jniLibs/arm64-v8a/libdirectaudiorelay.so new file mode 100644 index 0000000000..d530b848ff Binary files /dev/null and b/app/src/main/jniLibs/arm64-v8a/libdirectaudiorelay.so differ diff --git a/tools/linuxfs/overlay/usr/local/bin/bannerlator-steam-compat b/tools/linuxfs/overlay/usr/local/bin/bannerlator-steam-compat index 7669773638..b7c0659ad1 100755 --- a/tools/linuxfs/overlay/usr/local/bin/bannerlator-steam-compat +++ b/tools/linuxfs/overlay/usr/local/bin/bannerlator-steam-compat @@ -70,6 +70,58 @@ TOOL_MANIFEST_VDF = """"manifest" } """ % LAUNCHER +# Resolves the depot at launch time, so the tool can be registered before the depot exists and +# survives the client updating or replacing it. Proton takes its base directory from +# dirname(sys.argv[0]) without resolving it, so it is run from the depot itself. +# Cores for a game the client launches, kept separate from the client's own. One mask suits the +# client's menus and another suits a game, and they are wanted at the same time - so the game's is +# applied here, in the launch path every game goes through, rather than to the session as a whole. +BL_GAME_CPUS_SETUP = """ +bl_game_cpus() { + [ -n "${BL_GAME_CPUS:-}" ] || return 0 + command -v taskset >/dev/null 2>&1 || return 0 + # exec'd through taskset rather than set afterwards: a mask applied to a pid races the process + # into spawning its own threads, and this way every thread inherits it from the first instruction. + BL_TASKSET="taskset -c $BL_GAME_CPUS" + echo "bannerlator-proton: game cores $BL_GAME_CPUS" >&2 +} +""" + +BL_DIRECTAUDIO_SETUP = """ +bl_directaudio() { + base=$1 + [ -n "${BL_DIRECTAUDIO:-}" ] || return 0 + [ -d "$BL_DIRECTAUDIO/lib/wine" ] || return 0 + version=$("$base/files/bin-arm64/wine" --version 2>/dev/null) + major=${version#wine-} + major=${major%%.*} + if [ "$major" != "11" ]; then + echo "bannerlator-proton: DirectAudio left off - it is built for Wine 11 and this Proton is '${version:-unknown}'. Pairing them would be silent rather than broken, so it is not worth guessing." >&2 + return 0 + fi + # Wine appends the per-architecture subdirectory to each entry, so the directory holding + # aarch64-unix/ and aarch64-windows/ is what goes here. + WINEDLLPATH="$BL_DIRECTAUDIO/lib/wine${WINEDLLPATH:+:$WINEDLLPATH}" + export WINEDLLPATH + # mmdevapi only tries the drivers named in the registry; its built-in list is pulse, alsa, oss and + # coreaudio and knows nothing about this one. The key is appended to the prefix's user.reg rather + # than written with `wine reg`, which would start a wineserver here just to set one value. + reg="${STEAM_COMPAT_DATA_PATH:-}/pfx/user.reg" + if [ -f "$reg" ]; then + if ! grep -q '"Audio"="directaudio"' "$reg" 2>/dev/null; then + printf '\n[Software\\\\Wine\\\\Drivers] %s\n"Audio"="directaudio"\n' "$(date +%s)" >> "$reg" + echo "bannerlator-proton: DirectAudio selected in the prefix" >&2 + fi + else + # A prefix that does not exist yet is created by Proton moments from now, after this runs, so + # the key lands on the NEXT launch of that game. Said out loud because silent audio on a first + # launch would otherwise look like a fault. + echo "bannerlator-proton: no prefix yet - DirectAudio takes effect the next time this game starts" >&2 + fi + echo "bannerlator-proton: DirectAudio ready (Wine $major)" >&2 +} +""" + # Resolves the depot at launch time, so the tool can be registered before the depot exists and # survives the client updating or replacing it. Proton takes its base directory from # dirname(sys.argv[0]) without resolving it, so it is run from the depot itself. @@ -98,12 +150,19 @@ fi for name in %s; do depot="$steam_root/steamapps/common/$name" if [ -d "$depot/files/bin-arm64" ] && [ -x "$depot/proton" ]; then - exec "$depot/proton" "$verb" "$@" + bl_directaudio "$depot" + bl_game_cpus + exec ${BL_TASKSET:-} "$depot/proton" "$verb" "$@" fi done echo "bannerlator-proton: no ARM64 Proton depot yet - the client downloads one as this tool's dependency; retry the launch once it has" >&2 exit 1 """ % " ".join('"%s"' % n for n in SOURCES) +# Spliced in after the formatting above, never through it: the function body contains ${major%%.*} +# and a % format would eat that into a single %, leaving a shell expansion that silently does +# something else. +LAUNCHER_SH = LAUNCHER_SH.replace( + "verb=$1; shift", "verb=$1; shift\n" + BL_DIRECTAUDIO_SETUP + BL_GAME_CPUS_SETUP, 1) # A third-party Proton dropped into compatibilitytools.d - GE-Proton, proton-cachyos, anything of # that shape - is adopted rather than used as shipped, for the same two reasons Valve's ARM64 depot @@ -135,9 +194,11 @@ if [ -f "$fake" ]; then *) LD_PRELOAD="$fake${LD_PRELOAD:+:$LD_PRELOAD}"; export LD_PRELOAD ;; esac fi +bl_directaudio "$here" +bl_game_cpus # Proton takes its base directory from dirname(sys.argv[0]) without resolving it, so it has to be # started from its own tree. -exec "$here/%s" "$verb" "$@" +exec ${BL_TASKSET:-} "$here/%s" "$verb" "$@" """ EXTRA_COMPAT_TOOL_VDF = """"compatibilitytools" { @@ -292,8 +353,13 @@ def adopt_extras(tools): name = first_key(compat_vdf, ["compatibilitytools", "compat_tools"]) or entry wrapper = os.path.join(tool_dir, EXTRA_WRAPPER) try: + # Formatted first, spliced second: the function carries a printf %s and a + # ${major%%.*} of its own, and putting it through the format would eat both. + body = (EXTRA_WRAPPER_SH % entry_point).replace( + 'here=$(dirname "$0")', + 'here=$(dirname "$0")\n' + BL_DIRECTAUDIO_SETUP + BL_GAME_CPUS_SETUP, 1) with open(wrapper, "w", encoding="utf-8") as f: - f.write(EXTRA_WRAPPER_SH % entry_point) + f.write(body) os.chmod(wrapper, 0o755) with open(manifest, "w", encoding="utf-8") as f: f.write(EXTRA_MANIFEST_VDF)