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
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,17 @@ if(X2W_WITH_OPUS)
add_dependencies(startup_xapu_opus_check startup_stub_xapu)
add_test(NAME startup_xapu_opus COMMAND startup_xapu_opus_check "$<TARGET_FILE:startup_stub_xapu>")
set_tests_properties(startup_xapu_opus PROPERTIES TIMEOUT 60)

# Bounded telemetry for the same optional decode path: decoded packets, dequeued completions and
# refusals by reason. Generates its tones with libopus; no captured packet is stored.
add_executable(startup_xapu_opus_count_check tests/startup_xapu_opus_count_check.cpp)
target_compile_features(startup_xapu_opus_count_check PRIVATE cxx_std_17)
target_include_directories(startup_xapu_opus_count_check PRIVATE src)
target_link_libraries(startup_xapu_opus_count_check PRIVATE Opus::opus)
set_target_properties(startup_xapu_opus_count_check PROPERTIES MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
add_dependencies(startup_xapu_opus_count_check startup_stub_xapu)
add_test(NAME startup_xapu_opus_count COMMAND startup_xapu_opus_count_check "$<TARGET_FILE:startup_stub_xapu>")
set_tests_properties(startup_xapu_opus_count PROPERTIES TIMEOUT 60)
endif()

add_executable(startup_chad_check tests/startup_chad_check.cpp)
Expand Down
65 changes: 65 additions & 0 deletions docs/experiments/startup-opus-completion-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Startup Opus completion counters

Refs [#1145](https://github.com/R0Y00/Xbox2Windows/issues/1145) and
[#870](https://github.com/R0Y00/Xbox2Windows/issues/870). This note covers the optional Opus
decode path built with `-DX2W_WITH_OPUS=ON` and `X2W_XAPU_TYPED_COMPLETIONS=1`.

## What is counted

`xapu.dll` keeps process-wide saturating counters for:

| Counter | Meaning |
|---|---|
| `type3` | readable type 3 commands seen while typed completions are on |
| `packets` | commands libopus decoded successfully |
| `completions` | type 3 results dequeued by the caller |
| `refusals` | sum of the refusal counters below |
| `invalid_argument` | a field or output range failed before libopus |
| `opus_error` | libopus rejected the packet |
| `capacity` | libopus reported `OPUS_BUFFER_TOO_SMALL` |
| `queue_full` | the per-connection queue was full |
| `short_command` | the decode fields crossed the readable header |
| `unknown_handle` | the command named a handle that was not connected |
| `out_of_memory` | a new decoder could not be created |
| `header` | the command header was null or unreadable |

The counters start on the first readable type 3 command. A run with typed completions off reads
and writes nothing here.

The audio sink also reports `accepted` and `not_open` submissions, so a run can distinguish an
endpoint that never opened from one that accepted decoded blocks.

## Summary line

At the last `XApuDisconnect`, and from `atexit` when a run never disconnects, the stand-in writes
one stable line when the counters moved since the previous summary:

```text
x2w-audio XApu opus summary: type3=10 packets=2 completions=2 refusals=9 invalid_argument=3 opus_error=1 capacity=1 queue_full=1 short_command=1 unknown_handle=1 out_of_memory=0 header=1
```

The line is written to stderr and to `X2W_TRAP_LOG`, like the other audio lines. It is a snapshot;
two threads can move different fields between their loads. Each field is exact on its own.

## Test

```powershell
cmake --preset dev -DX2W_WITH_OPUS=ON -DOpus_DIR=<libopus install>/lib/cmake/Opus
cmake --build --preset dev --target startup_stub_xapu startup_xapu_opus_count_check
ctest --preset dev --no-tests=error -R '^startup_xapu_opus_count$' -V
```

The fixture encodes synthetic 1 kHz tones with libopus and drives the real stand-in exports. It
checks two successful decodes and dequeues, a malformed packet, insufficient capacity, an invalid
rate, an out-of-range slot in both the legacy and typed-stream paths, a short command, an unknown
handle, a full queue, a null header, saturation at `UINT64_MAX`, one summary after the last
disconnect, and that typed completions off neither decode nor add a summary line. It passed in
Debug with libopus 1.5.2 on Windows 11 build 26300.

## Limits

The counters prove that the optional decode path ran and that the caller consumed the results.
They do not prove that the output was audible, that the desktop sink accepted the same block, or
that XMA/AcpHal works. The `startup_audio_output` fixture covers the sink's accepted/not-open
counters separately. The title run that produced the 27/11 audio observation was not available
locally, so this change adds the measurement, not the run receipt.
10 changes: 8 additions & 2 deletions docs/startup-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -2998,8 +2998,8 @@ are copied from scratch only after the result is queued.

```powershell
cmake --preset dev -DX2W_WITH_OPUS=ON -DOpus_DIR=<libopus install>/lib/cmake/Opus
cmake --build --preset dev --target startup_stub_xapu startup_xapu_queue_check startup_xapu_opus_check
ctest --preset dev --no-tests=error -R '^startup_xapu_(queue|opus)$' -V
cmake --build --preset dev --target startup_stub_xapu startup_xapu_queue_check startup_xapu_opus_check startup_xapu_opus_count_check
ctest --preset dev --no-tests=error -R '^startup_xapu_(queue|opus|opus_count)$' -V
```

`startup_xapu_opus` encodes generated 1 kHz tones with the same libopus and checks exact byte counts (3840 for
Expand All @@ -3009,6 +3009,12 @@ an untouched output and an empty queue, a full queue that leaves the slot's stre
starting a fresh decoder, a rate change on a live slot, and that type `3` decodes nothing with typed completions
off. It is registered only in the optional build.

`startup_xapu_opus_count` uses the same libopus tones to check the bounded telemetry: decoded
packets, dequeued completions and each refusal reason. The stand-in writes one
`x2w-audio XApu opus summary:` line at the last disconnect or at process exit, and the audio sink
reports `accepted` and `not_open` submissions. See
[the completion-count note](experiments/startup-opus-completion-count.md).

`src/startup_stubs/audio_sink.{h,cpp}` is a desktop WASAPI shared-mode render sink. It opens the
default render endpoint, takes the mix format from `GetMixFormat` (32-bit float or 16-bit PCM only),
converts each submitted block (format, frame count, bytes) to it, and writes it through
Expand Down
21 changes: 17 additions & 4 deletions src/startup_stubs/audio_sink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,21 @@ void AudioSink::observe(bool withholding)
Submit AudioSink::submit(const BlockFormat& in, uint32_t frames, const void* bytes)
{
const uint32_t call = calls_++;
if (!p_) return Submit::NotOpen;
if (!p_) {
++r_.not_open;
return Submit::NotOpen;
}
ResampleState trial = resample_; // committed only once the block is accepted
if (!convert_block(in, bytes, frames, mix_, conv_buf_, &trial)) {
++r_.unsupported;
return Submit::Unsupported;
}
const uint32_t out_frames = static_cast<uint32_t>(conv_buf_.size() / mix_.frame_bytes());
const uint32_t pad = padding();
if (pad == padding_failed) return Submit::NotOpen;
if (pad == padding_failed) {
++r_.not_open;
return Submit::NotOpen;
}
if (out_frames > buffer_frames_ - pad) {
++r_.rejected_full;
return Submit::Full;
Expand All @@ -141,9 +147,15 @@ Submit AudioSink::submit(const BlockFormat& in, uint32_t frames, const void* byt
if (timer_.drained_start >= 0) timer_.resume(now_ms(), r_.consumed_frames); // the producer is back
if (out_frames > 0) {
BYTE* dst = nullptr;
if (FAILED(p_->render->GetBuffer(out_frames, &dst)) || !dst) return Submit::NotOpen;
if (FAILED(p_->render->GetBuffer(out_frames, &dst)) || !dst) {
++r_.not_open;
return Submit::NotOpen;
}
std::memcpy(dst, conv_buf_.data(), conv_buf_.size());
if (FAILED(p_->render->ReleaseBuffer(out_frames, 0))) return Submit::NotOpen;
if (FAILED(p_->render->ReleaseBuffer(out_frames, 0))) {
++r_.not_open;
return Submit::NotOpen;
}
}
resample_ = std::move(trial);
if (r_.first_block < 0) {
Expand All @@ -153,6 +165,7 @@ Submit AudioSink::submit(const BlockFormat& in, uint32_t frames, const void* byt
r_.frames.push_back(out_frames);
r_.peaks.push_back(block_peak(in, bytes, frames));
r_.submitted_frames += out_frames;
++r_.accepted;
if (!started_ && SUCCEEDED(p_->client->Start())) started_ = true;
return Submit::Accepted;
}
Expand Down
3 changes: 2 additions & 1 deletion src/startup_stubs/audio_sink.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@ struct Report {
uint64_t submitted_frames = 0; // frames written to the endpoint
uint64_t consumed_frames = 0; // submitted frames minus frames still queued
uint64_t clock_frames = 0; // IAudioClock position converted to frames
uint32_t unsupported = 0, rejected_full = 0;
uint64_t accepted = 0; // submit() calls that returned Submit::Accepted
uint32_t unsupported = 0, rejected_full = 0, not_open = 0;
uint32_t padding_errors = 0; // GetCurrentPadding failed (e.g. the endpoint went away): not a measurement
double drained_ms = 0, recovery_ms = -1; // recovery_ms < 0: not measured
// Glitches: the render API exposes no discontinuity flag, so this stays "not available".
Expand Down
97 changes: 97 additions & 0 deletions src/startup_stubs/audio_telemetry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Experimental (docs/startup-runtime.md): bounded counters for the optional Opus decode path.
//
// Every counter saturates at UINT64_MAX instead of wrapping. The values are process-wide and are
// read without a single coherent snapshot: a summary taken while another thread enqueues can show
// one field from before that enqueue and another from after it. Each field is exact on its own.
#pragma once

#include <atomic>
#include <cstdint>
#include <limits>

namespace x2w_audio {

inline void saturating_add(std::atomic<std::uint64_t>& counter, std::uint64_t amount = 1) noexcept
{
std::uint64_t seen = counter.load(std::memory_order_relaxed);
for (;;) {
const std::uint64_t next = amount > (std::numeric_limits<std::uint64_t>::max)() - seen
? (std::numeric_limits<std::uint64_t>::max)()
: seen + amount;
if (next == seen || counter.compare_exchange_weak(seen, next, std::memory_order_relaxed,
std::memory_order_relaxed))
return;
}
}

// Counters for one process's type-3 Opus path. `type3_commands` counts every readable type-3
// command while typed completions are on. `packets_decoded` counts calls libopus accepted with a
// non-empty packet. `decoded_completions` counts type-3 results dequeued by the caller, so a packet
// that decodes but is never consumed is not in it. The refusal fields name the first check that
// refused the command; `header` covers a null or unreadable XApuEnqueueCommand header.
struct OpusCounters {
std::atomic<std::uint64_t> type3_commands{0};
std::atomic<std::uint64_t> packets_decoded{0};
std::atomic<std::uint64_t> decoded_completions{0};
std::atomic<std::uint64_t> refused_header{0};
std::atomic<std::uint64_t> refused_short_command{0};
std::atomic<std::uint64_t> refused_unknown_handle{0};
std::atomic<std::uint64_t> refused_queue_full{0};
std::atomic<std::uint64_t> refused_invalid_argument{0};
std::atomic<std::uint64_t> refused_opus_error{0};
std::atomic<std::uint64_t> refused_capacity{0};
std::atomic<std::uint64_t> refused_out_of_memory{0};
};

struct OpusCounterSnapshot {
std::uint64_t type3_commands = 0;
std::uint64_t packets_decoded = 0;
std::uint64_t decoded_completions = 0;
std::uint64_t refused_header = 0;
std::uint64_t refused_short_command = 0;
std::uint64_t refused_unknown_handle = 0;
std::uint64_t refused_queue_full = 0;
std::uint64_t refused_invalid_argument = 0;
std::uint64_t refused_opus_error = 0;
std::uint64_t refused_capacity = 0;
std::uint64_t refused_out_of_memory = 0;
};

inline OpusCounterSnapshot snapshot(const OpusCounters& counters) noexcept
{
return {counters.type3_commands.load(std::memory_order_relaxed),
counters.packets_decoded.load(std::memory_order_relaxed),
counters.decoded_completions.load(std::memory_order_relaxed),
counters.refused_header.load(std::memory_order_relaxed),
counters.refused_short_command.load(std::memory_order_relaxed),
counters.refused_unknown_handle.load(std::memory_order_relaxed),
counters.refused_queue_full.load(std::memory_order_relaxed),
counters.refused_invalid_argument.load(std::memory_order_relaxed),
counters.refused_opus_error.load(std::memory_order_relaxed),
counters.refused_capacity.load(std::memory_order_relaxed),
counters.refused_out_of_memory.load(std::memory_order_relaxed)};
}

inline std::uint64_t saturating_total(std::uint64_t& total, std::uint64_t value) noexcept
{
total = value > (std::numeric_limits<std::uint64_t>::max)() - total
? (std::numeric_limits<std::uint64_t>::max)()
: total + value;
return total;
}

inline std::uint64_t refused_total(const OpusCounterSnapshot& s) noexcept
{
std::uint64_t total = 0;
saturating_total(total, s.refused_header);
saturating_total(total, s.refused_short_command);
saturating_total(total, s.refused_unknown_handle);
saturating_total(total, s.refused_queue_full);
saturating_total(total, s.refused_invalid_argument);
saturating_total(total, s.refused_opus_error);
saturating_total(total, s.refused_capacity);
saturating_total(total, s.refused_out_of_memory);
return total;
}

} // namespace x2w_audio
Loading
Loading