Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions docs/runtime_imports.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,45 @@ calls succeed and do nothing: there is no audio processor, so the game gets no s
fail hard, which is why every call succeeds. `acphal_test` covers the call shapes.
The table under [The retail executable](#the-retail-executable-rt-5) was measured before this shim and still lists these nine as unimplemented; they are served now.

## Audio output off the console (WASAPI)

Off the console the title's sound goes out through WASAPI, not through the `AcpHal` path above: it creates an
`MMDeviceEnumerator` with `CoCreateInstance` (one call, `CLSCTX_ALL`, `IID_IMMDeviceEnumerator`), asks it for the
default render endpoint and activates `ISpatialAudioClient` on it (measured: the first interface the title asks the
device for). A console always has an output. A host with none
answers `GetDefaultAudioEndpoint(eRender, ...)` with `0x80070490` (`ERROR_NOT_FOUND`), and the title's device start
then takes an error path that leaves its own output-device lock held by a job worker: the audio engine never mixes, a
main-thread command buffer fills up, and the title stops on an `int3` about 47 s into the boot (measured under Wine with
its audio drivers disabled; refusing the enumerator itself stops it the same way, and a host null sink removes the stop).

So `CoCreateInstance` of that class goes to `runtime/shims/silent_audio.cpp` (hooked in `shims/com_shim.cpp`). When
the host's enumerator has a default render endpoint it is returned unchanged. When it has none, or cannot be made, or
`LEONIDA_AUDIO=silent` is set, the title gets an enumerator whose render answers are a **silent output endpoint** of the
runtime's own: `GetDefaultAudioEndpoint(eRender)`, `EnumAudioEndpoints(eRender)` (active devices) and `GetDevice` with
its ID give an active `IMMDevice` whose property store has a friendly name and the device format; `Activate` gives
`ISpatialAudioClient`, `ISpatialAudioClient2`, `IAudioClient`, `IAudioClient2` or `IAudioClient3` (any other interface
is `E_NOINTERFACE`, logged once). The spatial client has a 7.1 bed of static objects and no dynamic objects, as an
endpoint with no spatial sound format turned on; its objects are mono 48 kHz float, and its
`ISpatialAudioObjectRenderStream` signals the stream's event every 10 ms pass (480 frames) while it runs and hands
each object a scratch buffer between `BeginUpdatingAudioObjects` and `EndUpdatingAudioObjects`. The client
takes any PCM or float format in shared mode (event-driven or polled), and `GetService` gives `IAudioRenderClient`
(a scratch buffer), `IAudioClock`, `ISimpleAudioVolume` and `IAudioStreamVolume`. A clock consumes the written frames
in real time, so `GetCurrentPadding` drains, and an event-driven stream's event is signalled every 10 ms period
while it runs, so a title that paces its mixer on the event keeps running. Capture questions keep the host's answer
(with `LEONIDA_AUDIO=silent` the host's enumerator is only made when one comes). Endpoint notification callbacks are
accepted and never called. Nothing opens or touches a host audio device, and every sample is discarded. The first
silent device handed out prints one `[rt] audio: serving the silent output endpoint (<reason>)` line, the reason being
the host's failed call or `LEONIDA_AUDIO=silent`.

The mix format is 48 kHz, 32-bit float, 7.1 (eight channels, mask `0x63F`). 48 kHz float is the shared-mode
format of the Windows audio engine and of the console's. 7.1 is the console's case, and a hypothesis, not measured
here: the console's system mixer takes a 7.1 bed whatever output is plugged in and downmixes it for stereo, so a
console title is built for eight channels, where stereo would be the usual answer of a PC with headphones. The stream
itself takes any channel count, so a title that asks for fewer is not refused. `silent_audio_test`
covers the enumerator over stand-in hosts with and without an endpoint and with none, a render stream run for a
few periods with its event and padding checked, a spatial stream run for ten passes, and the `CoCreateInstance` hook
with `LEONIDA_AUDIO=silent`.

## Console paths and the write overlay

A title's file calls name the console's drives, so `GAME:\data\x.sps` is a file inside the installed game folder.
Expand Down
18 changes: 18 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ if(WIN32 AND LEONIDA_BUILD_WINDOWS_RUNTIME)
shims/library_loader.cpp
shims/process.cpp
shims/com_shim.cpp
shims/silent_audio.cpp
shims/bcrypt_rng.cpp
shims/xapu_chad.cpp
shims/dstorage.cpp
Expand Down Expand Up @@ -460,13 +461,27 @@ if(WIN32 AND LEONIDA_BUILD_WINDOWS_RUNTIME)
add_executable(com_shim_test
test/com_shim_test.cpp
shims/com_shim.cpp
shims/silent_audio.cpp
shims/registry.cpp
)
target_include_directories(com_shim_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/shims)
target_compile_definitions(com_shim_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
target_compile_options(com_shim_test PRIVATE /W4 /permissive- /utf-8)
target_link_options(com_shim_test PRIVATE /OPT:NOREF)

# The silent output endpoint (MMDeviceEnumerator through CoCreateInstance): stand-in hosts, a render stream on
# its clock, and the CoCreateInstance hook with LEONIDA_AUDIO=silent. No host audio device is opened.
add_executable(silent_audio_test
test/silent_audio_test.cpp
shims/silent_audio.cpp
shims/com_shim.cpp
shims/registry.cpp
)
target_include_directories(silent_audio_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/shims)
target_compile_definitions(silent_audio_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
target_compile_options(silent_audio_test PRIVATE /W4 /permissive- /utf-8)
target_link_options(silent_audio_test PRIVATE /OPT:NOREF)

# BCrypt RNG and algorithm provider shims.
add_executable(bcrypt_rng_test
test/bcrypt_rng_test.cpp
Expand Down Expand Up @@ -1504,6 +1519,9 @@ if(BUILD_TESTING)
if(WIN32 AND TARGET com_shim_test)
add_test(NAME com_shim COMMAND com_shim_test)
endif()
if(WIN32 AND TARGET silent_audio_test)
add_test(NAME silent_audio COMMAND silent_audio_test)
endif()
if(WIN32 AND TARGET bcrypt_rng_test)
add_test(NAME bcrypt_rng COMMAND bcrypt_rng_test)
add_test(NAME bcrypt_rng_seeded COMMAND bcrypt_rng_test --seeded)
Expand Down
1 change: 1 addition & 0 deletions runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ ones marked *diagnostic* only add log lines or exist to compare a run with and w
| `LEONIDA_WINDOW` | unset | `WxH` resizes the title's window when the presenter attaches to it. |
| `LEONIDA_PRESENT_DUMP` | unset | *Diagnostic.* `DIR` or `DIR,N` writes every Nth presented frame (60 by default) as a PNG into `DIR` (`src/present_dump.h`). The files are pictures of the title: keep them out of commits, issues and pull requests. |
| `LEONIDA_LOCAL_SPEECH` | unset | `1` forwards the title's speech-synthesis calls to the host's installed `xgameruntime.dll` where there is one. Unset, and wherever that library is missing, the silent voice answers. No profile sets it. |
| `LEONIDA_AUDIO` | unset | `silent` (any case) makes the title's `MMDeviceEnumerator` answer every render question with the runtime's silent output endpoint, even on a host that has an output (`shims/silent_audio.cpp`, read at each `CoCreateInstance`): no host audio device is opened and every sample is thrown away, for muted test runs. Unset, or any other value, the host's endpoint answers when there is one and the silent endpoint only when the host has none. Capture keeps the host's answer either way. See [docs/runtime_imports.md](../docs/runtime_imports.md#audio-output-off-the-console-wasapi). |

The two serializer functions and `D3D12XboxCreateDevice` are the only `d3d12_xs` imports used by the
shipped DLLs (measured; see `docs/design.md` and `docs/d3d12_vulkan.md`). The game executable's own
Expand Down
8 changes: 7 additions & 1 deletion runtime/shims/com_shim.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// COM lifecycle and helper shims for api-ms-win-core-com-l1-1-0..3.dll.
//
// game_scarlett_master.exe imports CoInitializeEx, CoUninitialize, CoCreateInstance, CoCreateGuid, CoTaskMemFree,
// PropVariantClear, and StringFromGUID2 through api-ms-win-core-com-l1-1-0.dll. When the host (or a host library) has
// PropVariantClear, and StringFromGUID2 through api-ms-win-core-com-l1-1-0.dll. CoCreateInstance of the
// MMDeviceEnumerator goes to silent_audio.cpp, which serves a silent output endpoint when the host has none. When the host (or a host library) has
// already initialized a thread as STA, a guest call to CoInitializeEx(nullptr, COINIT_MULTITHREADED) fails with
// RPC_E_CHANGED_MODE (0x80010106) and an unbalanced CoUninitialize can tear down host COM early.
//
Expand All @@ -15,6 +16,7 @@
#include <new>

#include "shims.h"
#include "silent_audio.h"

namespace rt::com_shim {
namespace {
Expand Down Expand Up @@ -128,6 +130,10 @@ void WINAPI co_uninitialize() {
HRESULT WINAPI co_create_instance(REFCLSID rclsid, LPUNKNOWN outer, DWORD cls_context, REFIID riid, LPVOID* ppv) {
if (!ppv) return E_POINTER;
*ppv = nullptr;
// The audio device enumerator: the host's when it has an output, else (or with LEONIDA_AUDIO=silent) one that
// also answers with the silent output endpoint (silent_audio.h).
if (!outer && IsEqualCLSID(rclsid, rt::silent_audio::kClsidMMDeviceEnumerator))
return rt::silent_audio::create_enumerator(cls_context, riid, ppv);
return ::CoCreateInstance(rclsid, outer, cls_context, riid, ppv);
}

Expand Down
Loading
Loading