Skip to content

Add Support for IAudioClient2 and IAudioClient3 - #70

Merged
HEnquist merged 9 commits into
HEnquist:masterfrom
LastExceed:lxc
Oct 11, 2026
Merged

HEnquist merged 9 commits into
HEnquist:masterfrom
LastExceed:lxc

Conversation

@LastExceed

Copy link
Copy Markdown
Contributor

closes #68

Turns out there were a couple more methods missing, which I have also added while I was at it.

Unresolved questions:

  1. The PR currently uses the new RangeInclusive<T> type that will become the default in Rust 2027, which is recommended practice for new APIs to prevent future legacy burdens. This also allows SharedModeEnginePeriod to derive Copy. However, it raises the MSRV to 1.96. Is that a problem?

  2. What kind of tests should I add (if any)?

  3. The docs of the bEventDriven parameter in IAudioClient2::GetBufferSizeLimits() states:

Boolean value to indicate whether or not the stream can be event-driven.

The word "can" in that sentence confuses me, given that bEventDriven is an [in] parameter. Does this imply that the returned range is describing the union of both modes when the parameter is set to true? If yes, then representing it with the TimingMode enum would be incorrect, as that implies mutual exclusivity. But I'd find WASAPI's API design rather strange in that case.

@HEnquist

HEnquist commented Oct 6, 2026

Copy link
Copy Markdown
Owner

Thanks, this fills a real gap. Answers to your questions first, then a few things to fix.

1. The range type. Please drop it and use a tuple. get_device_period() already returns
WasapiRes<(i64, i64)> for exactly this, and DataRange spells out min_*/max_* as separate
documented fields. So get_buffer_size_limits -> (i64, i64), and SharedModeEnginePeriod gets
min_period/max_period fields. That keeps the MSRV at 1.85 and drops both .into() calls with
their "redundant in 2027" comments. As it stands CI fails twice: the msrv job errors with
unresolved import std::range on 1.85, and clippy flags incompatible_msrv under -D warnings.
(For the record clippy says the type is stable since 1.95, not 1.96.)

2. Tests. There is now a device backed suite in tests/device/, behind the device-tests
feature, running on VB-Cable in CI. A new tests/device/audioclient23.rs plus a mod line in
main.rs is the place. IAudioClient3 may be missing on the cable, so each test needs an
E_NOINTERFACE skip through support::is_hresult and support::skip. Worth having:

  • get_buffer_size_limits on a client straight out of get_iaudioclient, for both timing modes,
    asserting min > 0 && min <= max. Don't cross-check against get_device_period(), the relation
    is not documented.
  • is_offload_capable for every StreamCategory, cross-checked against
    set_properties(... .set_offload(true).set_category(cat)) plus initialize_client. The two
    should agree. This is the one I'd most like to see, since it ties the new query to the existing
    setter.
  • get_shared_mode_engine_period invariants: min <= default <= max, fundamental > 0, and
    default and min multiples of fundamental.
  • initialize_shared_audio_stream with that default, then support::render_for and the existing
    rate check. It takes an already initialised client, so no new helper needed. Then assert
    get_current_shared_mode_engine_period returns the same period, and that get_sharemode and
    get_timing_mode match. That last part matters because the method sets those fields by hand
    instead of going through initialize_client.
  • Asking for default + 1 when fundamental > 1 should fail. Cheap, and it proves the period is
    passed as frames and not hns.

3. bEventDriven. I read the whole page and that sentence is all there is, so the docs don't
settle it. My reading is the same as yours would be from the signature: it is an [in] parameter,
so the caller declares the intended mode and gets the limits for that mode. "can be" is just loose
wording. TimingMode is the right type, keep it. Since it is an inference, say so in the docstring
rather than leaving it implied. The Remarks also have a line worth capturing: "a device-facing
method and does not require prior audio stream initialization".

One bug. In get_current_shared_mode_engine_period, WaveFormat::parse is fallible (short
cbSize gives UnsupportedFormat), so on that path the ? returns and the COM allocated format
never reaches CoTaskMemFree. get_mixformat and is_supported free in every branch. Free first,
then parse the copy.

Smaller things.

  • initialize_shared_audio_stream has no docstring, and it is the one that needs one most: no
    loopback and no autoconvert, unlike initialize_client. Also worth taking wavefmt first, as
    initialize_client does.
  • The other four have one line docs but no MSDN links. The rest of the file links consistently.
  • SharedModeEnginePeriod fields need doc comments, DataRange documents every field.
  • &raw mut for out params is new here, the file uses &mut x and Some(&mut x) throughout.

@LastExceed

Copy link
Copy Markdown
Contributor Author

Out of curiosity, why the complete abstinence from ranges? I recognize that we have to do it now to remain consistent with existing APIs, so I will of course follow through, but why was this choice ever made in the first place? DataRanges literally has "Range" in the name after all

For the record clippy says the type is stable since 1.95, not 1.96.

Interesting. I just checked, and turns out the new RangeInclusive got stabilized earlier than the new Range (not that it matters now)

will take care of the rest

@HEnquist

HEnquist commented Oct 6, 2026

Copy link
Copy Markdown
Owner

Nothing against ranges for modelling ranges. This just isn't one.

PeriodInFrames has to be an integral multiple of the fundamental period as well as within min and
max, so the valid values are a grid, not an interval. The docs give an example: with min 48 and
fundamental 4, allowed values include 48, 96, 128 and 448, but 98 is not, and min + 1 gets
rejected with AUDCLNT_E_INVALID_DEVICE_PERIOD.

Named fields are what fits. min_period and max_period next to default and fundamental, four
related numbers you need all of to work out a valid period.

If you don't like the tuple for get_buffer_size_limits, use a small struct with named fields there
too. It is nicer, and I'd rather have that than a range. The existing tuple returns stay as they are
for now, but I'll move them the same way whenever something else forces a breaking release.

@LastExceed

Copy link
Copy Markdown
Contributor Author

Applied some of the feedback. Still not sure how to approach the tests, so I'll leave that to you.

PS: The AI responses are demotivating.

@HEnquist

HEnquist commented Oct 6, 2026

Copy link
Copy Markdown
Owner

Sorry about the AI written comments. I'm short on time and used AI to cut a corner there. The points looked good to me, so I posted the suggestions as they were. But I should have written the feedback myself.

I'm happy to finish the rest if you'd rather be done with it.
What is left is the empty docstring, a few MSDN links and the device tests.
The tests here are very new (there were none a week ago) so the exact format etc are still somewhat fluid. I basically want to test this library, not the Windows APIs. And I want to avoid making very complicated tests, for example an elaborate playback test that turns into a poor music player.

@LastExceed

Copy link
Copy Markdown
Contributor Author

empty docstring

Dunno what to write there😅

a few MSDN links

Added. FYI MSDN no longer exists, it was merged with TechNet into what is now MS Learn

test this library, not the Windows APIs

Yeah this is what I am stuck on, I feel like the suggested tests would be mostly testing WASAPI itself. The library doesn't add any new invariants (at least in this case), so IMO there is nothing to test, except maybe the mutation of internal state in the init function, which is rather trivial.

@HEnquist

HEnquist commented Oct 6, 2026

Copy link
Copy Markdown
Owner

Dunno what to write there😅

How about:

    /// Initialize an [AudioClient] for a shared mode stream with the given periodicity.
    ///
    /// Take `period_in_frames` from [AudioClient::get_shared_mode_engine_period].
    /// Unlike [AudioClient::initialize_client], loopback capture and automatic format conversion
    /// are not available here.
    ///
    /// WASAPI docs: [IAudioClient3::InitializeSharedAudioStream](https://learn.microsoft.com/en-us/windows/win32/api/audioclient/nf-audioclient-iaudioclient3-initializesharedaudiostream)
    pub fn initialize_shared_audio_stream(

Added. FYI MSDN no longer exists, it was merged with TechNet into what is now MS Learn

Yeah I know, it was called MSDN when I first started reading it and the name got stuck in my brain :)

For get_buffer_size_limits, I found that it fails with AUDCLNT_E_OFFLOAD_MODE_ONLY unless the
endpoint supports hardware offload. Hardware offload seems rare, I tried on all the devices I have and none of them support it.
Suggested docstring:

    /// The buffer size limits of the hardware audio engine in 100-nanosecond units.
    ///
    /// These are the limits of a hardware audio engine, so they are only available on an endpoint
    /// that supports hardware offloaded streams.
    /// An endpoint without one has nothing to report, and the call then fails with
    /// `AUDCLNT_E_OFFLOAD_MODE_ONLY`.
    /// Use [AudioClient::is_offload_capable] to check beforehand.
    ///
    /// This function does not require the [AudioClient] to be initialized.
    ///
    /// WASAPI docs: [IAudioClient2::GetBufferSizeLimits](https://learn.microsoft.com/en-us/windows/win32/api/audioclient/nf-audioclient-iaudioclient2-getbuffersizelimits)

And for tests, I agree there isn't much to test. I would add something for calling some method on the IAudioClient2&3, just to know the cast works as expected. You can skip it, I'll add something simple later.

@LastExceed

Copy link
Copy Markdown
Contributor Author

Done.

@HEnquist

HEnquist commented Oct 8, 2026

Copy link
Copy Markdown
Owner

Thanks! I hope to make a new release including this in the next few days.

@HEnquist
HEnquist merged commit b7b02be into HEnquist:master Oct 11, 2026
@LastExceed
LastExceed deleted the lxc branch October 11, 2026 16:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for IAudioClient3::InitializeSharedAudioStream()

2 participants