From 41c43053b10628515a0f528263b7fbf42e3190c7 Mon Sep 17 00:00:00 2001 From: emirduran707 Date: Tue, 24 Nov 2026 15:13:39 +0300 Subject: [PATCH] XAudio2: an XMA voice reports no bytes required, as the docs say XMA source voices play through a Windows PCM16 voice, and the callback wrapper passed that voice's OnVoiceProcessingPassStart BytesRequired (the PCM16 it needs) on to the title. Microsoft's documentation: "For xWMA and XMA data BytesRequired will always be zero". A title that streams just in time from this value would see its XMA voice as starving and submit more than it needs. The voice state now records whether the title submits XMA, and only then is 0 passed on. Opus voices, which the documentation does not cover, keep the count as before. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/hle/xaudio2.rs | 58 ++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/hle/xaudio2.rs b/crates/xcl/src/hle/xaudio2.rs index fae020df15..6136b75a97 100644 --- a/crates/xcl/src/hle/xaudio2.rs +++ b/crates/xcl/src/hle/xaudio2.rs @@ -533,6 +533,8 @@ struct DecodedVoiceState { rate: u32, avg_bytes: u32, title_cb: usize, + /// The title submits XMA (not Opus): its BytesRequired is always 0 + xma: bool, /// Maps stable token (`&*DecodedBuffer as usize`) → owned `DecodedBuffer`. live_buffers: Mutex>>, } @@ -572,10 +574,12 @@ unsafe fn title_cb_vtbl(cb: usize) -> Option<(*mut c_void, *const VoiceCallbackV Some((obj, vtbl)) } +/// `bytes` counts the PCM16 the Windows voice needs. For XMA the title submitted, BytesRequired +/// is always 0 (IXAudio2VoiceCallback::OnVoiceProcessingPassStart docs); Opus keeps the count. unsafe extern "system" fn decoded_cb_pass_start(this: *mut c_void, bytes: u32) { let cb = &*(this as *const DecodedCallback); if let Some((obj, vt)) = title_cb_vtbl(cb.state.title_cb) { - ((*vt).pass_start)(obj, bytes); + ((*vt).pass_start)(obj, if cb.state.xma { 0 } else { bytes }); } } @@ -777,6 +781,7 @@ unsafe fn create_decoded_voice( label: &str, ) -> HRESULT { let pcm = pcm16_for_rate(channels, rate); + let xma = matches!(decoder, VoiceDecoder::Xma { .. }); let state = Arc::new(DecodedVoiceState { decoder: Mutex::new(decoder), block_align, @@ -784,6 +789,7 @@ unsafe fn create_decoded_voice( rate: pcm.rate, avg_bytes, title_cb: cb as usize, + xma, live_buffers: Mutex::new(HashMap::new()), }); let wrapped_cb = Box::into_raw(Box::new(DecodedCallback { @@ -1631,6 +1637,56 @@ mod tests { } } + #[test] + fn decoded_voice_reports_no_bytes_required_to_the_title() { + static SEEN: AtomicUsize = AtomicUsize::new(usize::MAX); + + unsafe extern "system" fn on_pass_start(_: *mut c_void, bytes: u32) { + SEEN.store(bytes as usize, Ordering::SeqCst); + } + unsafe extern "system" fn on_nothing(_: *mut c_void) {} + unsafe extern "system" fn on_ctx(_: *mut c_void, _: *mut c_void) {} + unsafe extern "system" fn on_err(_: *mut c_void, _: *mut c_void, _: HRESULT) {} + + static TEST_VTBL: VoiceCallbackVtbl = VoiceCallbackVtbl { + pass_start: on_pass_start, + pass_end: on_nothing, + stream_end: on_nothing, + buffer_start: on_ctx, + buffer_end: on_ctx, + loop_end: on_ctx, + voice_error: on_err, + }; + #[repr(C)] + struct TestCb { + vtbl: &'static VoiceCallbackVtbl, + } + static TEST_CB: TestCb = TestCb { vtbl: &TEST_VTBL }; + + let f = xma2(2, 48_000, 0, 0x8000, 1); + let fmt = &f as *const Xma2WaveFormatEx as *const WaveFormatEx; + let (cfg, info) = unsafe { (xma_stream_config(fmt).unwrap().unwrap(), xma_info(fmt)) }; + let decoder = VoiceDecoder::Xma { decoder: xma2::Decoder::new(cfg).unwrap(), info }; + let state = Arc::new(DecodedVoiceState { + decoder: Mutex::new(decoder), + block_align: 1, + channels: 2, + rate: 48_000, + avg_bytes: 0, + title_cb: &TEST_CB as *const TestCb as usize, + xma: true, + live_buffers: Mutex::new(HashMap::new()), + }); + let mut wrapped = DecodedCallback { vtbl: &DECODED_CALLBACK_VTBL, state }; + // The Windows PCM16 voice asks for 1920 bytes (10 ms of 48 kHz stereo). + unsafe { (wrapped.vtbl.pass_start)(&mut wrapped as *mut DecodedCallback as *mut c_void, 1920) }; + assert_eq!(SEEN.load(Ordering::SeqCst), 0); + // a voice that is not XMA (Opus) keeps the count + let mut opus = DecodedCallback { vtbl: &DECODED_CALLBACK_VTBL, state: Arc::new(DecodedVoiceState { xma: false, ..Arc::try_unwrap(wrapped.state).ok().unwrap() }) }; + unsafe { (opus.vtbl.pass_start)(&mut opus as *mut DecodedCallback as *mut c_void, 1920) }; + assert_eq!(SEEN.load(Ordering::SeqCst), 1920); + } + #[test] fn clamp_regions_enforces_play_end_and_loop_invariants() { // Loop extending past PlayBegin + PlayLength is clamped to play_end (300).