COM can prevent undefined behavior in either concurrency model by
performing marshaling when necessary. As a result, CoInitializeEx can be
called with either concurrency model, and in this case STA provides better
compatibility with other code requiring STA, e.g. ASIO backend or winit
drag-and-drop.
To dive into the detail, the entry point of WASAPI, MMDeviceEnumerator, is
registered with "both" threading model, which means that COM objects are
created with whatever the thread's concurrency model is set to. This raises
the concern that when STA is used, marshaling might make audio buffer
operations block on the main thread, breaking continuous audio processing.
However, the implementation actually uses free-threaded marshaller for
interfaces dealing with buffer operations, which effectively bypasses COM's
compatibility marshaling behavior and perform any API calls on the caller's
thread instead. Therefore, the interfaces would operate just fine on either
concurrency model.
For more details on COM's threading model, see [1].
[1] https://thrysoee.dk/InsideCOM+/ch04d.htm
Co-Authored-By: Henrik Rydgård <hrydgard@gmail.com>
Close#59Close#348Close#504Close#530Close#538Close#572
* Always set period before setting buffer size, even if
`BufferSize::Fixed`.
* Set `start_threshold` based on `alsa::Direction` to avoid underruns on
playback streams.
Also refactor the return values of the set params functions to reflect their
actual use and resolve some clippy warnings (including a logic error in
`stream_timestamp()`).
With rust-jack 0.7, the buffer_size callback is moved to
ProcessHandler. This reflects how it is actually called by jack:
it is called synchronously on the process thread, but it is allowed to
perform non-real-time-safe operations. In CPAL, this means that
it is fine to reallocate the temporary interlacing buffers when buffer
size changes.
This removes the need for buffer splitting on the output stream type,
and prevents panics when buffer size is made larger than initially on
the input stream type. (for some reason, buffer splitting was not
implemented for input previously)
This was introduced in 6b91485 and could cause issues with some alsa
configurations like that of pipewire-alsa which caused the audio to be
become weirdly distorted
With my previous patch, after an underrun occurred sometimes polling the
alsa FDs would always return POLLIN immediately, but avail_frames would
be zero. Eventually avail_frames would become nonzero and audio output
would work, but poll would still return immediately afterwards.
Calling `stream.prepare()` when this happens seems to fix the issue after
a few loops.