From 76d2ff732e8af140a40570f7de38289a16861380 Mon Sep 17 00:00:00 2001 From: ShortSide <83917259+shortside@users.noreply.github.com> Date: Tue, 24 Nov 2026 08:43:15 +0100 Subject: [PATCH] XAudio2: GetState names the oldest queued buffer when the host gives none Under Wine (CrossOver 26.3), GetState on a decoded (XMA / Opus) source voice that has buffers queued but has not started returns BuffersQueued = 2 and pCurrentBufferContext = NULL. Windows names the first queued buffer there, and xma2_source_voice_decodes_and_manages_lifetime checks for it (left 0, right 0x3003). XCL submits its decoded buffers with a non-null token as the context, so a null current context with buffers queued means the host did not say. Each decoded buffer now carries its submission number, and GetState names the oldest one that has not ended. When the host returns a context, the mapping is unchanged, so Windows is not affected. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/hle/xaudio2.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/crates/xcl/src/hle/xaudio2.rs b/crates/xcl/src/hle/xaudio2.rs index fae020df15..4a8a178a01 100644 --- a/crates/xcl/src/hle/xaudio2.rs +++ b/crates/xcl/src/hle/xaudio2.rs @@ -509,9 +509,13 @@ pub fn silent_buffer(i: &XmaInfo, b: &XAudio2Buffer, silence: *const u8, max_byt /// or `DestroyVoice`. struct DecodedBuffer { orig_context: usize, + /// Submission order (`NEXT_BUFFER_SEQ`), so `GetState` can name the oldest queued buffer. + seq: u64, _pcm: Box<[u8]>, } +static NEXT_BUFFER_SEQ: AtomicU64 = AtomicU64::new(0); + enum VoiceDecoder { Opus(opus::Decoder), Xma { decoder: xma2::Decoder, info: XmaInfo }, @@ -1075,7 +1079,8 @@ unsafe fn submit_decoded_buffer( let pcm_ptr = pcm_bytes.as_ptr(); let pcm_len = pcm_bytes.len() as u32; - let boxed = Box::new(DecodedBuffer { orig_context: orig_buf.context as usize, _pcm: pcm_bytes }); + let seq = NEXT_BUFFER_SEQ.fetch_add(1, Ordering::Relaxed); + let boxed = Box::new(DecodedBuffer { orig_context: orig_buf.context as usize, seq, _pcm: pcm_bytes }); let token = &*boxed as *const DecodedBuffer as usize; state.live_buffers.lock().unwrap().insert(token, boxed); @@ -1147,8 +1152,17 @@ unsafe extern "system" fn get_state(this: *mut c_void, state_out: *mut XAudio2Vo .and_then(|m| m.get(&(this as usize)).map(|h| Arc::clone(&h.state))); if let Some(voice_state) = voice_state { let cur = (*state_out).current_buffer_context as usize; + let live = voice_state.live_buffers.lock().unwrap(); if cur != 0 { - if let Some(buf) = voice_state.live_buffers.lock().unwrap().get(&cur) { + if let Some(buf) = live.get(&cur) { + (*state_out).current_buffer_context = buf.orig_context as *mut c_void; + } + } else if (*state_out).buffers_queued != 0 { + // Wine's XAudio2 (seen under CrossOver 26.3) counts the buffers of a voice that + // has not started yet but reports no current one; Windows names the first + // queued buffer. Our tokens are never null, so a null here means the host did not + // say: name the oldest buffer we submitted that has not ended. + if let Some(buf) = live.values().min_by_key(|b| b.seq) { (*state_out).current_buffer_context = buf.orig_context as *mut c_void; } }