[WIP] Introduce a Host API
This is an implementation of the API described at #204. Please see that issue for more details on the motivation. ----- A **Host** provides access to the available audio devices on the system. Some platforms have more than one host available, e.g. wasapi/asio/dsound on windows, alsa/pulse/jack on linux and so on. As a result, some audio devices are only available on certain hosts, while others are only available on other hosts. Every platform supported by CPAL has at least one **DefaultHost** that is guaranteed to be available (alsa, wasapi and coreaudio). Currently, the default hosts are the only hosts supported by CPAL, however this will change as of landing #221 (cc @freesig). These changes should also accommodate support for other hosts such as jack #250 (cc @derekdreery) and pulseaudio (cc @knappador) #259. This introduces a suite of traits allowing for both compile time and runtime dispatch of different hosts and their uniquely associated device and event loop types. A new private **host** module has been added containing the individual host implementations, each in their own submodule gated to the platforms on which they are available. A new **platform** module has been added containing platform-specific items, including a dynamically dispatched host type that allows for easily switching between hosts at runtime. The **ALL_HOSTS** slice contains a **HostId** for each host supported on the current platform. The **available_hosts** function produces a **HostId** for each host that is currently *available* on the platform. The **host_from_id** function allows for initialising a host from its associated ID, failing with a **HostUnavailable** error. The **default_host** function returns the default host and should never fail. Please see the examples for a demonstration of the change in usage. For the most part, things look the same at the surface level, however the role of device enumeration and creating the event loop have been moved from global functions to host methods. The enumerate.rs example has been updated to enumerate all devices for each host, not just the default. **TODO** - [x] Add the new **Host** API - [x] Update examples for the new API. - [x] ALSA host - [ ] WASAPI host - [ ] CoreAudio host - [ ] Emscripten host **Follow-up PR** - [ ] ASIO host #221 cc @ishitatsuyuki more to review for you if you're interested, but it might be easier after #288 lands and this gets rebased.
This commit is contained in:
parent
2b9e2e0b2c
commit
e8a05379c2
17 changed files with 989 additions and 447 deletions
35
src/host/wasapi/com.rs
Normal file
35
src/host/wasapi/com.rs
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
//! Handles COM initialization and cleanup.
|
||||
|
||||
use super::check_result;
|
||||
use std::ptr;
|
||||
|
||||
use super::winapi::um::objbase::{COINIT_MULTITHREADED};
|
||||
use super::winapi::um::combaseapi::{CoInitializeEx, CoUninitialize};
|
||||
|
||||
thread_local!(static COM_INITIALIZED: ComInitialized = {
|
||||
unsafe {
|
||||
// this call can fail if another library initialized COM in single-threaded mode
|
||||
// handling this situation properly would make the API more annoying, so we just don't care
|
||||
check_result(CoInitializeEx(ptr::null_mut(), COINIT_MULTITHREADED)).unwrap();
|
||||
ComInitialized(ptr::null_mut())
|
||||
}
|
||||
});
|
||||
|
||||
/// RAII object that guards the fact that COM is initialized.
|
||||
///
|
||||
// We store a raw pointer because it's the only way at the moment to remove `Send`/`Sync` from the
|
||||
// object.
|
||||
struct ComInitialized(*mut ());
|
||||
|
||||
impl Drop for ComInitialized {
|
||||
#[inline]
|
||||
fn drop(&mut self) {
|
||||
unsafe { CoUninitialize() };
|
||||
}
|
||||
}
|
||||
|
||||
/// Ensures that COM is initialized in this thread.
|
||||
#[inline]
|
||||
pub fn com_initialized() {
|
||||
COM_INITIALIZED.with(|_| {});
|
||||
}
|
||||
818
src/host/wasapi/device.rs
Normal file
818
src/host/wasapi/device.rs
Normal file
|
|
@ -0,0 +1,818 @@
|
|||
use std;
|
||||
use std::ffi::OsString;
|
||||
use std::fmt;
|
||||
use std::io::Error as IoError;
|
||||
use std::mem;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use std::os::windows::ffi::OsStringExt;
|
||||
use std::ptr;
|
||||
use std::slice;
|
||||
use std::sync::{Arc, Mutex, MutexGuard};
|
||||
|
||||
use BackendSpecificError;
|
||||
use DefaultFormatError;
|
||||
use DeviceNameError;
|
||||
use DevicesError;
|
||||
use Format;
|
||||
use SupportedFormatsError;
|
||||
use SampleFormat;
|
||||
use SampleRate;
|
||||
use SupportedFormat;
|
||||
use COMMON_SAMPLE_RATES;
|
||||
|
||||
use super::check_result;
|
||||
use super::check_result_backend_specific;
|
||||
use super::com;
|
||||
use super::winapi::Interface;
|
||||
use super::winapi::ctypes::c_void;
|
||||
use super::winapi::shared::devpkey;
|
||||
use super::winapi::shared::ksmedia;
|
||||
use super::winapi::shared::guiddef::{
|
||||
GUID,
|
||||
};
|
||||
use super::winapi::shared::winerror;
|
||||
use super::winapi::shared::minwindef::{
|
||||
DWORD,
|
||||
};
|
||||
use super::winapi::shared::mmreg;
|
||||
use super::winapi::shared::wtypes;
|
||||
// https://msdn.microsoft.com/en-us/library/cc230355.aspx
|
||||
use super::winapi::um::winnt::LPWSTR;
|
||||
use super::winapi::um::winnt::WCHAR;
|
||||
use super::winapi::um::coml2api;
|
||||
use super::winapi::um::audioclient::{
|
||||
IAudioClient,
|
||||
IID_IAudioClient,
|
||||
AUDCLNT_E_DEVICE_INVALIDATED,
|
||||
};
|
||||
use super::winapi::um::audiosessiontypes::{
|
||||
AUDCLNT_SHAREMODE_SHARED,
|
||||
};
|
||||
use super::winapi::um::combaseapi::{
|
||||
CoCreateInstance,
|
||||
CoTaskMemFree,
|
||||
CLSCTX_ALL,
|
||||
PropVariantClear,
|
||||
};
|
||||
use super::winapi::um::mmdeviceapi::{
|
||||
eAll,
|
||||
eCapture,
|
||||
eConsole,
|
||||
eRender,
|
||||
CLSID_MMDeviceEnumerator,
|
||||
DEVICE_STATE_ACTIVE,
|
||||
EDataFlow,
|
||||
IMMDevice,
|
||||
IMMDeviceCollection,
|
||||
IMMDeviceEnumerator,
|
||||
IMMEndpoint,
|
||||
};
|
||||
|
||||
pub type SupportedInputFormats = std::vec::IntoIter<SupportedFormat>;
|
||||
pub type SupportedOutputFormats = std::vec::IntoIter<SupportedFormat>;
|
||||
|
||||
/// Wrapper because of that stupid decision to remove `Send` and `Sync` from raw pointers.
|
||||
#[derive(Copy, Clone)]
|
||||
struct IAudioClientWrapper(*mut IAudioClient);
|
||||
unsafe impl Send for IAudioClientWrapper {
|
||||
}
|
||||
unsafe impl Sync for IAudioClientWrapper {
|
||||
}
|
||||
|
||||
/// An opaque type that identifies an end point.
|
||||
pub struct Device {
|
||||
device: *mut IMMDevice,
|
||||
/// We cache an uninitialized `IAudioClient` so that we can call functions from it without
|
||||
/// having to create/destroy audio clients all the time.
|
||||
future_audio_client: Arc<Mutex<Option<IAudioClientWrapper>>>, // TODO: add NonZero around the ptr
|
||||
}
|
||||
|
||||
struct Endpoint {
|
||||
endpoint: *mut IMMEndpoint,
|
||||
}
|
||||
|
||||
enum WaveFormat {
|
||||
Ex(mmreg::WAVEFORMATEX),
|
||||
Extensible(mmreg::WAVEFORMATEXTENSIBLE),
|
||||
}
|
||||
|
||||
// Use RAII to make sure CoTaskMemFree is called when we are responsible for freeing.
|
||||
struct WaveFormatExPtr(*mut mmreg::WAVEFORMATEX);
|
||||
|
||||
impl Drop for WaveFormatExPtr {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
CoTaskMemFree(self.0 as *mut _);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl WaveFormat {
|
||||
// Given a pointer to some format, returns a valid copy of the format.
|
||||
pub fn copy_from_waveformatex_ptr(ptr: *const mmreg::WAVEFORMATEX) -> Option<Self> {
|
||||
unsafe {
|
||||
match (*ptr).wFormatTag {
|
||||
mmreg::WAVE_FORMAT_PCM | mmreg::WAVE_FORMAT_IEEE_FLOAT => {
|
||||
Some(WaveFormat::Ex(*ptr))
|
||||
},
|
||||
mmreg::WAVE_FORMAT_EXTENSIBLE => {
|
||||
let extensible_ptr = ptr as *const mmreg::WAVEFORMATEXTENSIBLE;
|
||||
Some(WaveFormat::Extensible(*extensible_ptr))
|
||||
},
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get the pointer to the WAVEFORMATEX struct.
|
||||
pub fn as_ptr(&self) -> *const mmreg::WAVEFORMATEX {
|
||||
self.deref() as *const _
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for WaveFormat {
|
||||
type Target = mmreg::WAVEFORMATEX;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
match *self {
|
||||
WaveFormat::Ex(ref f) => f,
|
||||
WaveFormat::Extensible(ref f) => &f.Format,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DerefMut for WaveFormat {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
match *self {
|
||||
WaveFormat::Ex(ref mut f) => f,
|
||||
WaveFormat::Extensible(ref mut f) => &mut f.Format,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
unsafe fn immendpoint_from_immdevice(device: *const IMMDevice) -> *mut IMMEndpoint {
|
||||
let mut endpoint: *mut IMMEndpoint = mem::uninitialized();
|
||||
check_result((*device).QueryInterface(&IMMEndpoint::uuidof(), &mut endpoint as *mut _ as *mut _))
|
||||
.expect("could not query IMMDevice interface for IMMEndpoint");
|
||||
endpoint
|
||||
}
|
||||
|
||||
unsafe fn data_flow_from_immendpoint(endpoint: *const IMMEndpoint) -> EDataFlow {
|
||||
let mut data_flow = mem::uninitialized();
|
||||
check_result((*endpoint).GetDataFlow(&mut data_flow))
|
||||
.expect("could not get endpoint data_flow");
|
||||
data_flow
|
||||
}
|
||||
|
||||
// Given the audio client and format, returns whether or not the format is supported.
|
||||
pub unsafe fn is_format_supported(
|
||||
client: *const IAudioClient,
|
||||
waveformatex_ptr: *const mmreg::WAVEFORMATEX,
|
||||
) -> Result<bool, SupportedFormatsError>
|
||||
{
|
||||
|
||||
|
||||
/*
|
||||
// `IsFormatSupported` checks whether the format is supported and fills
|
||||
// a `WAVEFORMATEX`
|
||||
let mut dummy_fmt_ptr: *mut mmreg::WAVEFORMATEX = mem::uninitialized();
|
||||
let hresult =
|
||||
(*audio_client)
|
||||
.IsFormatSupported(share_mode, &format_attempt.Format, &mut dummy_fmt_ptr);
|
||||
// we free that `WAVEFORMATEX` immediately after because we don't need it
|
||||
if !dummy_fmt_ptr.is_null() {
|
||||
CoTaskMemFree(dummy_fmt_ptr as *mut _);
|
||||
}
|
||||
|
||||
// `IsFormatSupported` can return `S_FALSE` (which means that a compatible format
|
||||
// has been found) but we also treat this as an error
|
||||
match (hresult, check_result(hresult)) {
|
||||
(_, Err(ref e))
|
||||
if e.raw_os_error() == Some(AUDCLNT_E_DEVICE_INVALIDATED) => {
|
||||
(*audio_client).Release();
|
||||
return Err(BuildStreamError::DeviceNotAvailable);
|
||||
},
|
||||
(_, Err(e)) => {
|
||||
(*audio_client).Release();
|
||||
panic!("{:?}", e);
|
||||
},
|
||||
(winerror::S_FALSE, _) => {
|
||||
(*audio_client).Release();
|
||||
return Err(BuildStreamError::FormatNotSupported);
|
||||
},
|
||||
(_, Ok(())) => (),
|
||||
};
|
||||
*/
|
||||
|
||||
|
||||
// Check if the given format is supported.
|
||||
let is_supported = |waveformatex_ptr, mut closest_waveformatex_ptr| {
|
||||
let result = (*client).IsFormatSupported(
|
||||
AUDCLNT_SHAREMODE_SHARED,
|
||||
waveformatex_ptr,
|
||||
&mut closest_waveformatex_ptr,
|
||||
);
|
||||
// `IsFormatSupported` can return `S_FALSE` (which means that a compatible format
|
||||
// has been found, but not an exact match) so we also treat this as unsupported.
|
||||
match (result, check_result(result)) {
|
||||
(_, Err(ref e)) if e.raw_os_error() == Some(AUDCLNT_E_DEVICE_INVALIDATED) => {
|
||||
return Err(SupportedFormatsError::DeviceNotAvailable);
|
||||
},
|
||||
(_, Err(_)) => {
|
||||
Ok(false)
|
||||
},
|
||||
(winerror::S_FALSE, _) => {
|
||||
Ok(false)
|
||||
},
|
||||
(_, Ok(())) => {
|
||||
Ok(true)
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
// First we want to retrieve a pointer to the `WAVEFORMATEX`.
|
||||
// Although `GetMixFormat` writes the format to a given `WAVEFORMATEX` pointer,
|
||||
// the pointer itself may actually point to a `WAVEFORMATEXTENSIBLE` structure.
|
||||
// We check the wFormatTag to determine this and get a pointer to the correct type.
|
||||
match (*waveformatex_ptr).wFormatTag {
|
||||
mmreg::WAVE_FORMAT_PCM | mmreg::WAVE_FORMAT_IEEE_FLOAT => {
|
||||
let mut closest_waveformatex = *waveformatex_ptr;
|
||||
let closest_waveformatex_ptr = &mut closest_waveformatex as *mut _;
|
||||
is_supported(waveformatex_ptr, closest_waveformatex_ptr)
|
||||
},
|
||||
mmreg::WAVE_FORMAT_EXTENSIBLE => {
|
||||
let waveformatextensible_ptr =
|
||||
waveformatex_ptr as *const mmreg::WAVEFORMATEXTENSIBLE;
|
||||
let mut closest_waveformatextensible = *waveformatextensible_ptr;
|
||||
let closest_waveformatextensible_ptr =
|
||||
&mut closest_waveformatextensible as *mut _;
|
||||
let closest_waveformatex_ptr =
|
||||
closest_waveformatextensible_ptr as *mut mmreg::WAVEFORMATEX;
|
||||
is_supported(waveformatex_ptr, closest_waveformatex_ptr)
|
||||
},
|
||||
_ => Ok(false),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Get a cpal Format from a WAVEFORMATEX.
|
||||
unsafe fn format_from_waveformatex_ptr(
|
||||
waveformatex_ptr: *const mmreg::WAVEFORMATEX,
|
||||
) -> Option<Format>
|
||||
{
|
||||
fn cmp_guid(a: &GUID, b: &GUID) -> bool {
|
||||
a.Data1 == b.Data1
|
||||
&& a.Data2 == b.Data2
|
||||
&& a.Data3 == b.Data3
|
||||
&& a.Data4 == b.Data4
|
||||
}
|
||||
let data_type = match ((*waveformatex_ptr).wBitsPerSample, (*waveformatex_ptr).wFormatTag) {
|
||||
(16, mmreg::WAVE_FORMAT_PCM) => SampleFormat::I16,
|
||||
(32, mmreg::WAVE_FORMAT_IEEE_FLOAT) => SampleFormat::F32,
|
||||
(n_bits, mmreg::WAVE_FORMAT_EXTENSIBLE) => {
|
||||
let waveformatextensible_ptr = waveformatex_ptr as *const mmreg::WAVEFORMATEXTENSIBLE;
|
||||
let sub = (*waveformatextensible_ptr).SubFormat;
|
||||
if n_bits == 16 && cmp_guid(&sub, &ksmedia::KSDATAFORMAT_SUBTYPE_PCM) {
|
||||
SampleFormat::I16
|
||||
} else if n_bits == 32 && cmp_guid(&sub, &ksmedia::KSDATAFORMAT_SUBTYPE_IEEE_FLOAT) {
|
||||
SampleFormat::F32
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
},
|
||||
// Unknown data format returned by GetMixFormat.
|
||||
_ => return None,
|
||||
};
|
||||
let format = Format {
|
||||
channels: (*waveformatex_ptr).nChannels as _,
|
||||
sample_rate: SampleRate((*waveformatex_ptr).nSamplesPerSec),
|
||||
data_type: data_type,
|
||||
};
|
||||
Some(format)
|
||||
}
|
||||
|
||||
unsafe impl Send for Device {
|
||||
}
|
||||
unsafe impl Sync for Device {
|
||||
}
|
||||
|
||||
impl Device {
|
||||
pub fn name(&self) -> Result<String, DeviceNameError> {
|
||||
unsafe {
|
||||
// Open the device's property store.
|
||||
let mut property_store = ptr::null_mut();
|
||||
(*self.device).OpenPropertyStore(coml2api::STGM_READ, &mut property_store);
|
||||
|
||||
// Get the endpoint's friendly-name property.
|
||||
let mut property_value = mem::zeroed();
|
||||
if let Err(err) = check_result(
|
||||
(*property_store).GetValue(
|
||||
&devpkey::DEVPKEY_Device_FriendlyName as *const _ as *const _,
|
||||
&mut property_value
|
||||
)
|
||||
) {
|
||||
let description = format!("failed to retrieve name from property store: {}", err);
|
||||
let err = BackendSpecificError { description };
|
||||
return Err(err.into());
|
||||
}
|
||||
|
||||
// Read the friendly-name from the union data field, expecting a *const u16.
|
||||
if property_value.vt != wtypes::VT_LPWSTR as _ {
|
||||
let description =
|
||||
format!("property store produced invalid data: {:?}", property_value.vt);
|
||||
let err = BackendSpecificError { description };
|
||||
return Err(err.into());
|
||||
}
|
||||
let ptr_usize: usize = *(&property_value.data as *const _ as *const usize);
|
||||
let ptr_utf16 = ptr_usize as *const u16;
|
||||
|
||||
// Find the length of the friendly name.
|
||||
let mut len = 0;
|
||||
while *ptr_utf16.offset(len) != 0 {
|
||||
len += 1;
|
||||
}
|
||||
|
||||
// Create the utf16 slice and covert it into a string.
|
||||
let name_slice = slice::from_raw_parts(ptr_utf16, len as usize);
|
||||
let name_os_string: OsString = OsStringExt::from_wide(name_slice);
|
||||
let name_string = match name_os_string.into_string() {
|
||||
Ok(string) => string,
|
||||
Err(os_string) => os_string.to_string_lossy().into(),
|
||||
};
|
||||
|
||||
// Clean up the property.
|
||||
PropVariantClear(&mut property_value);
|
||||
|
||||
Ok(name_string)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn from_immdevice(device: *mut IMMDevice) -> Self {
|
||||
Device {
|
||||
device: device,
|
||||
future_audio_client: Arc::new(Mutex::new(None)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Ensures that `future_audio_client` contains a `Some` and returns a locked mutex to it.
|
||||
fn ensure_future_audio_client(&self)
|
||||
-> Result<MutexGuard<Option<IAudioClientWrapper>>, IoError> {
|
||||
let mut lock = self.future_audio_client.lock().unwrap();
|
||||
if lock.is_some() {
|
||||
return Ok(lock);
|
||||
}
|
||||
|
||||
let audio_client: *mut IAudioClient = unsafe {
|
||||
let mut audio_client = mem::uninitialized();
|
||||
let hresult = (*self.device).Activate(&IID_IAudioClient,
|
||||
CLSCTX_ALL,
|
||||
ptr::null_mut(),
|
||||
&mut audio_client);
|
||||
|
||||
// can fail if the device has been disconnected since we enumerated it, or if
|
||||
// the device doesn't support playback for some reason
|
||||
check_result(hresult)?;
|
||||
assert!(!audio_client.is_null());
|
||||
audio_client as *mut _
|
||||
};
|
||||
|
||||
*lock = Some(IAudioClientWrapper(audio_client));
|
||||
Ok(lock)
|
||||
}
|
||||
|
||||
/// Returns an uninitialized `IAudioClient`.
|
||||
#[inline]
|
||||
pub(crate) fn build_audioclient(&self) -> Result<*mut IAudioClient, IoError> {
|
||||
let mut lock = self.ensure_future_audio_client()?;
|
||||
let client = lock.unwrap().0;
|
||||
*lock = None;
|
||||
Ok(client)
|
||||
}
|
||||
|
||||
// There is no way to query the list of all formats that are supported by the
|
||||
// audio processor, so instead we just trial some commonly supported formats.
|
||||
//
|
||||
// Common formats are trialed by first getting the default format (returned via
|
||||
// `GetMixFormat`) and then mutating that format with common sample rates and
|
||||
// querying them via `IsFormatSupported`.
|
||||
//
|
||||
// When calling `IsFormatSupported` with the shared-mode audio engine, only the default
|
||||
// number of channels seems to be supported. Any more or less returns an invalid
|
||||
// parameter error. Thus we just assume that the default number of channels is the only
|
||||
// number supported.
|
||||
fn supported_formats(&self) -> Result<SupportedInputFormats, SupportedFormatsError> {
|
||||
// initializing COM because we call `CoTaskMemFree` to release the format.
|
||||
com::com_initialized();
|
||||
|
||||
// Retrieve the `IAudioClient`.
|
||||
let lock = match self.ensure_future_audio_client() {
|
||||
Ok(lock) => lock,
|
||||
Err(ref e) if e.raw_os_error() == Some(AUDCLNT_E_DEVICE_INVALIDATED) => {
|
||||
return Err(SupportedFormatsError::DeviceNotAvailable)
|
||||
}
|
||||
Err(e) => {
|
||||
let description = format!("{}", e);
|
||||
let err = BackendSpecificError { description };
|
||||
return Err(err.into());
|
||||
},
|
||||
};
|
||||
let client = lock.unwrap().0;
|
||||
|
||||
unsafe {
|
||||
// Retrieve the pointer to the default WAVEFORMATEX.
|
||||
let mut default_waveformatex_ptr = WaveFormatExPtr(mem::uninitialized());
|
||||
match check_result((*client).GetMixFormat(&mut default_waveformatex_ptr.0)) {
|
||||
Ok(()) => (),
|
||||
Err(ref e) if e.raw_os_error() == Some(AUDCLNT_E_DEVICE_INVALIDATED) => {
|
||||
return Err(SupportedFormatsError::DeviceNotAvailable);
|
||||
},
|
||||
Err(e) => {
|
||||
let description = format!("{}", e);
|
||||
let err = BackendSpecificError { description };
|
||||
return Err(err.into());
|
||||
},
|
||||
};
|
||||
|
||||
// If the default format can't succeed we have no hope of finding other formats.
|
||||
assert_eq!(try!(is_format_supported(client, default_waveformatex_ptr.0)), true);
|
||||
|
||||
// Copy the format to use as a test format (as to avoid mutating the original format).
|
||||
let mut test_format = {
|
||||
match WaveFormat::copy_from_waveformatex_ptr(default_waveformatex_ptr.0) {
|
||||
Some(f) => f,
|
||||
// If the format is neither EX or EXTENSIBLE we don't know how to work with it.
|
||||
None => return Ok(vec![].into_iter()),
|
||||
}
|
||||
};
|
||||
|
||||
// Begin testing common sample rates.
|
||||
//
|
||||
// NOTE: We should really be testing for whole ranges here, but it is infeasible to
|
||||
// test every sample rate up to the overflow limit as the `IsFormatSupported` method is
|
||||
// quite slow.
|
||||
let mut supported_sample_rates: Vec<u32> = Vec::new();
|
||||
for &rate in COMMON_SAMPLE_RATES {
|
||||
let rate = rate.0 as DWORD;
|
||||
test_format.nSamplesPerSec = rate;
|
||||
test_format.nAvgBytesPerSec =
|
||||
rate * (*default_waveformatex_ptr.0).nBlockAlign as DWORD;
|
||||
if try!(is_format_supported(client, test_format.as_ptr())) {
|
||||
supported_sample_rates.push(rate);
|
||||
}
|
||||
}
|
||||
|
||||
// If the common rates don't include the default one, add the default.
|
||||
let default_sr = (*default_waveformatex_ptr.0).nSamplesPerSec as _;
|
||||
if !supported_sample_rates.iter().any(|&r| r == default_sr) {
|
||||
supported_sample_rates.push(default_sr);
|
||||
}
|
||||
|
||||
// Reset the sample rate on the test format now that we're done.
|
||||
test_format.nSamplesPerSec = (*default_waveformatex_ptr.0).nSamplesPerSec;
|
||||
test_format.nAvgBytesPerSec = (*default_waveformatex_ptr.0).nAvgBytesPerSec;
|
||||
|
||||
// TODO: Test the different sample formats?
|
||||
|
||||
// Create the supported formats.
|
||||
let mut format = match format_from_waveformatex_ptr(default_waveformatex_ptr.0) {
|
||||
Some(fmt) => fmt,
|
||||
None => {
|
||||
let description =
|
||||
"could not create a `cpal::Format` from a `WAVEFORMATEX`".to_string();
|
||||
let err = BackendSpecificError { description };
|
||||
return Err(err.into());
|
||||
}
|
||||
};
|
||||
let mut supported_formats = Vec::with_capacity(supported_sample_rates.len());
|
||||
for rate in supported_sample_rates {
|
||||
format.sample_rate = SampleRate(rate as _);
|
||||
supported_formats.push(SupportedFormat::from(format.clone()));
|
||||
}
|
||||
|
||||
Ok(supported_formats.into_iter())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn supported_input_formats(&self) -> Result<SupportedInputFormats, SupportedFormatsError> {
|
||||
if self.data_flow() == eCapture {
|
||||
self.supported_formats()
|
||||
// If it's an output device, assume no input formats.
|
||||
} else {
|
||||
Ok(vec![].into_iter())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn supported_output_formats(&self) -> Result<SupportedOutputFormats, SupportedFormatsError> {
|
||||
if self.data_flow() == eRender {
|
||||
self.supported_formats()
|
||||
// If it's an input device, assume no output formats.
|
||||
} else {
|
||||
Ok(vec![].into_iter())
|
||||
}
|
||||
}
|
||||
|
||||
// We always create voices in shared mode, therefore all samples go through an audio
|
||||
// processor to mix them together.
|
||||
//
|
||||
// One format is guaranteed to be supported, the one returned by `GetMixFormat`.
|
||||
fn default_format(&self) -> Result<Format, DefaultFormatError> {
|
||||
// initializing COM because we call `CoTaskMemFree`
|
||||
com::com_initialized();
|
||||
|
||||
let lock = match self.ensure_future_audio_client() {
|
||||
Ok(lock) => lock,
|
||||
Err(ref e) if e.raw_os_error() == Some(AUDCLNT_E_DEVICE_INVALIDATED) => {
|
||||
return Err(DefaultFormatError::DeviceNotAvailable)
|
||||
}
|
||||
Err(e) => {
|
||||
let description = format!("{}", e);
|
||||
let err = BackendSpecificError { description };
|
||||
return Err(err.into());
|
||||
}
|
||||
};
|
||||
let client = lock.unwrap().0;
|
||||
|
||||
unsafe {
|
||||
let mut format_ptr = WaveFormatExPtr(mem::uninitialized());
|
||||
match check_result((*client).GetMixFormat(&mut format_ptr.0)) {
|
||||
Err(ref e) if e.raw_os_error() == Some(AUDCLNT_E_DEVICE_INVALIDATED) => {
|
||||
return Err(DefaultFormatError::DeviceNotAvailable);
|
||||
},
|
||||
Err(e) => {
|
||||
let description = format!("{}", e);
|
||||
let err = BackendSpecificError { description };
|
||||
return Err(err.into());
|
||||
},
|
||||
Ok(()) => (),
|
||||
};
|
||||
|
||||
format_from_waveformatex_ptr(format_ptr.0)
|
||||
.ok_or(DefaultFormatError::StreamTypeNotSupported)
|
||||
}
|
||||
}
|
||||
|
||||
fn data_flow(&self) -> EDataFlow {
|
||||
let endpoint = Endpoint::from(self.device as *const _);
|
||||
endpoint.data_flow()
|
||||
}
|
||||
|
||||
pub fn default_input_format(&self) -> Result<Format, DefaultFormatError> {
|
||||
if self.data_flow() == eCapture {
|
||||
self.default_format()
|
||||
} else {
|
||||
Err(DefaultFormatError::StreamTypeNotSupported)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn default_output_format(&self) -> Result<Format, DefaultFormatError> {
|
||||
let data_flow = self.data_flow();
|
||||
if data_flow == eRender {
|
||||
self.default_format()
|
||||
} else {
|
||||
Err(DefaultFormatError::StreamTypeNotSupported)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for Device {
|
||||
#[inline]
|
||||
fn eq(&self, other: &Device) -> bool {
|
||||
// Use case: In oder to check whether the default device has changed
|
||||
// the client code might need to compare the previous default device with the current one.
|
||||
// The pointer comparison (`self.device == other.device`) don't work there,
|
||||
// because the pointers are different even when the default device stays the same.
|
||||
//
|
||||
// In this code section we're trying to use the GetId method for the device comparison, cf.
|
||||
// https://docs.microsoft.com/en-us/windows/desktop/api/mmdeviceapi/nf-mmdeviceapi-immdevice-getid
|
||||
unsafe {
|
||||
struct IdRAII (LPWSTR);
|
||||
/// RAII for device IDs.
|
||||
impl Drop for IdRAII {
|
||||
fn drop(&mut self) {
|
||||
unsafe {CoTaskMemFree(self.0 as *mut c_void)}
|
||||
}
|
||||
}
|
||||
let mut id1: LPWSTR = ptr::null_mut();
|
||||
let rc1 = (*self.device).GetId(&mut id1);
|
||||
// GetId only fails with E_OUTOFMEMORY and if it does, we're probably dead already.
|
||||
// Plus it won't do to change the device comparison logic unexpectedly.
|
||||
if rc1 != winerror::S_OK {panic! ("cpal: GetId failure: {}", rc1)}
|
||||
let id1 = IdRAII(id1);
|
||||
let mut id2: LPWSTR = ptr::null_mut();
|
||||
let rc2 = (*other.device).GetId(&mut id2);
|
||||
if rc2 != winerror::S_OK {panic! ("cpal: GetId failure: {}", rc1)}
|
||||
let id2 = IdRAII(id2);
|
||||
// 16-bit null-terminated comparison.
|
||||
let mut offset = 0;
|
||||
loop {
|
||||
let w1: WCHAR = *id1.0.offset(offset);
|
||||
let w2: WCHAR = *id2.0.offset(offset);
|
||||
if w1 == 0 && w2 == 0 {return true}
|
||||
if w1 != w2 {return false}
|
||||
offset += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for Device {
|
||||
}
|
||||
|
||||
impl Clone for Device {
|
||||
#[inline]
|
||||
fn clone(&self) -> Device {
|
||||
unsafe {
|
||||
(*self.device).AddRef();
|
||||
}
|
||||
|
||||
Device {
|
||||
device: self.device,
|
||||
future_audio_client: self.future_audio_client.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Device {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.debug_struct("Device")
|
||||
.field("device", &self.device)
|
||||
.field("name", &self.name())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Device {
|
||||
#[inline]
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
(*self.device).Release();
|
||||
}
|
||||
|
||||
if let Some(client) = self.future_audio_client.lock().unwrap().take() {
|
||||
unsafe {
|
||||
(*client.0).Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Endpoint {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
(*self.endpoint).Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<*const IMMDevice> for Endpoint {
|
||||
fn from(device: *const IMMDevice) -> Self {
|
||||
unsafe {
|
||||
let endpoint = immendpoint_from_immdevice(device);
|
||||
Endpoint { endpoint: endpoint }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Endpoint {
|
||||
fn data_flow(&self) -> EDataFlow {
|
||||
unsafe {
|
||||
data_flow_from_immendpoint(self.endpoint)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref ENUMERATOR: Enumerator = {
|
||||
// COM initialization is thread local, but we only need to have COM initialized in the
|
||||
// thread we create the objects in
|
||||
com::com_initialized();
|
||||
|
||||
// building the devices enumerator object
|
||||
unsafe {
|
||||
let mut enumerator: *mut IMMDeviceEnumerator = mem::uninitialized();
|
||||
|
||||
let hresult = CoCreateInstance(
|
||||
&CLSID_MMDeviceEnumerator,
|
||||
ptr::null_mut(),
|
||||
CLSCTX_ALL,
|
||||
&IMMDeviceEnumerator::uuidof(),
|
||||
&mut enumerator as *mut *mut IMMDeviceEnumerator as *mut _,
|
||||
);
|
||||
|
||||
check_result(hresult).unwrap();
|
||||
Enumerator(enumerator)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// RAII object around `IMMDeviceEnumerator`.
|
||||
struct Enumerator(*mut IMMDeviceEnumerator);
|
||||
|
||||
unsafe impl Send for Enumerator {
|
||||
}
|
||||
unsafe impl Sync for Enumerator {
|
||||
}
|
||||
|
||||
impl Drop for Enumerator {
|
||||
#[inline]
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
(*self.0).Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// WASAPI implementation for `Devices`.
|
||||
pub struct Devices {
|
||||
collection: *mut IMMDeviceCollection,
|
||||
total_count: u32,
|
||||
next_item: u32,
|
||||
}
|
||||
|
||||
impl Devices {
|
||||
pub fn new() -> Result<Self, DevicesError> {
|
||||
unsafe {
|
||||
let mut collection: *mut IMMDeviceCollection = mem::uninitialized();
|
||||
// can fail because of wrong parameters (should never happen) or out of memory
|
||||
check_result_backend_specific(
|
||||
(*ENUMERATOR.0).EnumAudioEndpoints(
|
||||
eAll,
|
||||
DEVICE_STATE_ACTIVE,
|
||||
&mut collection,
|
||||
)
|
||||
)?;
|
||||
|
||||
let mut count = mem::uninitialized();
|
||||
// can fail if the parameter is null, which should never happen
|
||||
check_result_backend_specific((*collection).GetCount(&mut count))?;
|
||||
|
||||
Ok(Devices {
|
||||
collection: collection,
|
||||
total_count: count,
|
||||
next_item: 0,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl Send for Devices {
|
||||
}
|
||||
unsafe impl Sync for Devices {
|
||||
}
|
||||
|
||||
impl Drop for Devices {
|
||||
#[inline]
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
(*self.collection).Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for Devices {
|
||||
type Item = Device;
|
||||
|
||||
fn next(&mut self) -> Option<Device> {
|
||||
if self.next_item >= self.total_count {
|
||||
return None;
|
||||
}
|
||||
|
||||
unsafe {
|
||||
let mut device = mem::uninitialized();
|
||||
// can fail if out of range, which we just checked above
|
||||
check_result((*self.collection).Item(self.next_item, &mut device)).unwrap();
|
||||
|
||||
self.next_item += 1;
|
||||
Some(Device::from_immdevice(device))
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
let num = self.total_count - self.next_item;
|
||||
let num = num as usize;
|
||||
(num, Some(num))
|
||||
}
|
||||
}
|
||||
|
||||
fn default_device(data_flow: EDataFlow) -> Option<Device> {
|
||||
unsafe {
|
||||
let mut device = mem::uninitialized();
|
||||
let hres = (*ENUMERATOR.0)
|
||||
.GetDefaultAudioEndpoint(data_flow, eConsole, &mut device);
|
||||
if let Err(_err) = check_result(hres) {
|
||||
return None; // TODO: check specifically for `E_NOTFOUND`, and panic otherwise
|
||||
}
|
||||
Some(Device::from_immdevice(device))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn default_input_device() -> Option<Device> {
|
||||
default_device(eCapture)
|
||||
}
|
||||
|
||||
pub fn default_output_device() -> Option<Device> {
|
||||
default_device(eRender)
|
||||
}
|
||||
30
src/host/wasapi/mod.rs
Normal file
30
src/host/wasapi/mod.rs
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
extern crate winapi;
|
||||
|
||||
use BackendSpecificError;
|
||||
use self::winapi::um::winnt::HRESULT;
|
||||
use std::io::Error as IoError;
|
||||
pub use self::device::{Device, Devices, SupportedInputFormats, SupportedOutputFormats, default_input_device, default_output_device};
|
||||
pub use self::stream::{EventLoop, StreamId};
|
||||
|
||||
mod com;
|
||||
mod device;
|
||||
mod stream;
|
||||
|
||||
#[inline]
|
||||
fn check_result(result: HRESULT) -> Result<(), IoError> {
|
||||
if result < 0 {
|
||||
Err(IoError::from_raw_os_error(result))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn check_result_backend_specific(result: HRESULT) -> Result<(), BackendSpecificError> {
|
||||
match check_result(result) {
|
||||
Ok(()) => Ok(()),
|
||||
Err(err) => {
|
||||
let description = format!("{}", err);
|
||||
return Err(BackendSpecificError { description });
|
||||
}
|
||||
}
|
||||
}
|
||||
859
src/host/wasapi/stream.rs
Normal file
859
src/host/wasapi/stream.rs
Normal file
|
|
@ -0,0 +1,859 @@
|
|||
use super::Device;
|
||||
use super::check_result;
|
||||
use super::com;
|
||||
use super::winapi::shared::basetsd::UINT32;
|
||||
use super::winapi::shared::ksmedia;
|
||||
use super::winapi::shared::minwindef::{BYTE, DWORD, FALSE, WORD};
|
||||
use super::winapi::shared::mmreg;
|
||||
use super::winapi::um::audioclient::{self, AUDCLNT_E_DEVICE_INVALIDATED, AUDCLNT_S_BUFFER_EMPTY};
|
||||
use super::winapi::um::audiosessiontypes::{AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_EVENTCALLBACK};
|
||||
use super::winapi::um::handleapi;
|
||||
use super::winapi::um::synchapi;
|
||||
use super::winapi::um::winbase;
|
||||
use super::winapi::um::winnt;
|
||||
|
||||
use std::mem;
|
||||
use std::ptr;
|
||||
use std::slice;
|
||||
use std::sync::Mutex;
|
||||
use std::sync::mpsc::{channel, Sender, Receiver};
|
||||
use std::sync::atomic::AtomicUsize;
|
||||
use std::sync::atomic::Ordering;
|
||||
|
||||
use BackendSpecificError;
|
||||
use BuildStreamError;
|
||||
use Format;
|
||||
use PauseStreamError;
|
||||
use PlayStreamError;
|
||||
use SampleFormat;
|
||||
use StreamData;
|
||||
use StreamDataResult;
|
||||
use StreamError;
|
||||
use UnknownTypeOutputBuffer;
|
||||
use UnknownTypeInputBuffer;
|
||||
|
||||
pub struct EventLoop {
|
||||
// Data used by the `run()` function implementation. The mutex is kept lock permanently by
|
||||
// `run()`. This ensures that two `run()` invocations can't run at the same time, and also
|
||||
// means that we shouldn't try to lock this field from anywhere else but `run()`.
|
||||
run_context: Mutex<RunContext>,
|
||||
|
||||
// Identifier of the next stream to create. Each new stream increases this counter. If the
|
||||
// counter overflows, there's a panic.
|
||||
// TODO: use AtomicU64 instead
|
||||
next_stream_id: AtomicUsize,
|
||||
|
||||
// Commands processed by the `run()` method that is currently running.
|
||||
// `pending_scheduled_event` must be signalled whenever a command is added here, so that it
|
||||
// will get picked up.
|
||||
commands: Sender<Command>,
|
||||
|
||||
// This event is signalled after a new entry is added to `commands`, so that the `run()`
|
||||
// method can be notified.
|
||||
pending_scheduled_event: winnt::HANDLE,
|
||||
}
|
||||
|
||||
struct RunContext {
|
||||
// Streams that have been created in this event loop.
|
||||
streams: Vec<StreamInner>,
|
||||
|
||||
// Handles corresponding to the `event` field of each element of `voices`. Must always be in
|
||||
// sync with `voices`, except that the first element is always `pending_scheduled_event`.
|
||||
handles: Vec<winnt::HANDLE>,
|
||||
|
||||
commands: Receiver<Command>,
|
||||
}
|
||||
|
||||
enum Command {
|
||||
NewStream(StreamInner),
|
||||
DestroyStream(StreamId),
|
||||
PlayStream(StreamId),
|
||||
PauseStream(StreamId),
|
||||
}
|
||||
|
||||
enum AudioClientFlow {
|
||||
Render {
|
||||
render_client: *mut audioclient::IAudioRenderClient,
|
||||
},
|
||||
Capture {
|
||||
capture_client: *mut audioclient::IAudioCaptureClient,
|
||||
},
|
||||
}
|
||||
|
||||
struct StreamInner {
|
||||
id: StreamId,
|
||||
audio_client: *mut audioclient::IAudioClient,
|
||||
client_flow: AudioClientFlow,
|
||||
// Event that is signalled by WASAPI whenever audio data must be written.
|
||||
event: winnt::HANDLE,
|
||||
// True if the stream is currently playing. False if paused.
|
||||
playing: bool,
|
||||
// Number of frames of audio data in the underlying buffer allocated by WASAPI.
|
||||
max_frames_in_buffer: UINT32,
|
||||
// Number of bytes that each frame occupies.
|
||||
bytes_per_frame: WORD,
|
||||
// The sample format with which the stream was created.
|
||||
sample_format: SampleFormat,
|
||||
}
|
||||
|
||||
impl EventLoop {
|
||||
pub fn new() -> EventLoop {
|
||||
let pending_scheduled_event =
|
||||
unsafe { synchapi::CreateEventA(ptr::null_mut(), 0, 0, ptr::null()) };
|
||||
|
||||
let (tx, rx) = channel();
|
||||
|
||||
EventLoop {
|
||||
pending_scheduled_event: pending_scheduled_event,
|
||||
run_context: Mutex::new(RunContext {
|
||||
streams: Vec::new(),
|
||||
handles: vec![pending_scheduled_event],
|
||||
commands: rx,
|
||||
}),
|
||||
next_stream_id: AtomicUsize::new(0),
|
||||
commands: tx,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build_input_stream(
|
||||
&self,
|
||||
device: &Device,
|
||||
format: &Format,
|
||||
) -> Result<StreamId, BuildStreamError>
|
||||
{
|
||||
unsafe {
|
||||
// Making sure that COM is initialized.
|
||||
// It's not actually sure that this is required, but when in doubt do it.
|
||||
com::com_initialized();
|
||||
|
||||
// Obtaining a `IAudioClient`.
|
||||
let audio_client = match device.build_audioclient() {
|
||||
Ok(client) => client,
|
||||
Err(ref e) if e.raw_os_error() == Some(AUDCLNT_E_DEVICE_INVALIDATED) =>
|
||||
return Err(BuildStreamError::DeviceNotAvailable),
|
||||
Err(e) => {
|
||||
let description = format!("{}", e);
|
||||
let err = BackendSpecificError { description };
|
||||
return Err(err.into());
|
||||
}
|
||||
};
|
||||
|
||||
// Computing the format and initializing the device.
|
||||
let waveformatex = {
|
||||
let format_attempt = format_to_waveformatextensible(format)
|
||||
.ok_or(BuildStreamError::FormatNotSupported)?;
|
||||
let share_mode = AUDCLNT_SHAREMODE_SHARED;
|
||||
|
||||
// Ensure the format is supported.
|
||||
match super::device::is_format_supported(audio_client, &format_attempt.Format) {
|
||||
Ok(false) => return Err(BuildStreamError::FormatNotSupported),
|
||||
Err(_) => return Err(BuildStreamError::DeviceNotAvailable),
|
||||
_ => (),
|
||||
}
|
||||
|
||||
// finally initializing the audio client
|
||||
let hresult = (*audio_client).Initialize(
|
||||
share_mode,
|
||||
AUDCLNT_STREAMFLAGS_EVENTCALLBACK,
|
||||
0,
|
||||
0,
|
||||
&format_attempt.Format,
|
||||
ptr::null(),
|
||||
);
|
||||
match check_result(hresult) {
|
||||
Err(ref e)
|
||||
if e.raw_os_error() == Some(AUDCLNT_E_DEVICE_INVALIDATED) => {
|
||||
(*audio_client).Release();
|
||||
return Err(BuildStreamError::DeviceNotAvailable);
|
||||
},
|
||||
Err(e) => {
|
||||
(*audio_client).Release();
|
||||
let description = format!("{}", e);
|
||||
let err = BackendSpecificError { description };
|
||||
return Err(err.into());
|
||||
},
|
||||
Ok(()) => (),
|
||||
};
|
||||
|
||||
format_attempt.Format
|
||||
};
|
||||
|
||||
// obtaining the size of the samples buffer in number of frames
|
||||
let max_frames_in_buffer = {
|
||||
let mut max_frames_in_buffer = mem::uninitialized();
|
||||
let hresult = (*audio_client).GetBufferSize(&mut max_frames_in_buffer);
|
||||
|
||||
match check_result(hresult) {
|
||||
Err(ref e)
|
||||
if e.raw_os_error() == Some(AUDCLNT_E_DEVICE_INVALIDATED) => {
|
||||
(*audio_client).Release();
|
||||
return Err(BuildStreamError::DeviceNotAvailable);
|
||||
},
|
||||
Err(e) => {
|
||||
(*audio_client).Release();
|
||||
let description = format!("{}", e);
|
||||
let err = BackendSpecificError { description };
|
||||
return Err(err.into());
|
||||
},
|
||||
Ok(()) => (),
|
||||
};
|
||||
|
||||
max_frames_in_buffer
|
||||
};
|
||||
|
||||
// Creating the event that will be signalled whenever we need to submit some samples.
|
||||
let event = {
|
||||
let event = synchapi::CreateEventA(ptr::null_mut(), 0, 0, ptr::null());
|
||||
if event == ptr::null_mut() {
|
||||
(*audio_client).Release();
|
||||
let description = format!("failed to create event");
|
||||
let err = BackendSpecificError { description };
|
||||
return Err(err.into());
|
||||
}
|
||||
|
||||
if let Err(e) = check_result((*audio_client).SetEventHandle(event)) {
|
||||
(*audio_client).Release();
|
||||
let description = format!("failed to call SetEventHandle: {}", e);
|
||||
let err = BackendSpecificError { description };
|
||||
return Err(err.into());
|
||||
}
|
||||
|
||||
event
|
||||
};
|
||||
|
||||
// Building a `IAudioCaptureClient` that will be used to read captured samples.
|
||||
let capture_client = {
|
||||
let mut capture_client: *mut audioclient::IAudioCaptureClient = mem::uninitialized();
|
||||
let hresult = (*audio_client).GetService(
|
||||
&audioclient::IID_IAudioCaptureClient,
|
||||
&mut capture_client as *mut *mut audioclient::IAudioCaptureClient as *mut _,
|
||||
);
|
||||
|
||||
match check_result(hresult) {
|
||||
Err(ref e)
|
||||
if e.raw_os_error() == Some(AUDCLNT_E_DEVICE_INVALIDATED) => {
|
||||
(*audio_client).Release();
|
||||
return Err(BuildStreamError::DeviceNotAvailable);
|
||||
},
|
||||
Err(e) => {
|
||||
(*audio_client).Release();
|
||||
let description = format!("failed to build capture client: {}", e);
|
||||
let err = BackendSpecificError { description };
|
||||
return Err(err.into());
|
||||
},
|
||||
Ok(()) => (),
|
||||
};
|
||||
|
||||
&mut *capture_client
|
||||
};
|
||||
|
||||
let new_stream_id = StreamId(self.next_stream_id.fetch_add(1, Ordering::Relaxed));
|
||||
if new_stream_id.0 == usize::max_value() {
|
||||
return Err(BuildStreamError::StreamIdOverflow);
|
||||
}
|
||||
|
||||
// Once we built the `StreamInner`, we add a command that will be picked up by the
|
||||
// `run()` method and added to the `RunContext`.
|
||||
{
|
||||
let client_flow = AudioClientFlow::Capture {
|
||||
capture_client: capture_client,
|
||||
};
|
||||
let inner = StreamInner {
|
||||
id: new_stream_id.clone(),
|
||||
audio_client: audio_client,
|
||||
client_flow: client_flow,
|
||||
event: event,
|
||||
playing: false,
|
||||
max_frames_in_buffer: max_frames_in_buffer,
|
||||
bytes_per_frame: waveformatex.nBlockAlign,
|
||||
sample_format: format.data_type,
|
||||
};
|
||||
|
||||
self.push_command(Command::NewStream(inner));
|
||||
};
|
||||
|
||||
Ok(new_stream_id)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build_output_stream(
|
||||
&self,
|
||||
device: &Device,
|
||||
format: &Format,
|
||||
) -> Result<StreamId, BuildStreamError>
|
||||
{
|
||||
unsafe {
|
||||
// Making sure that COM is initialized.
|
||||
// It's not actually sure that this is required, but when in doubt do it.
|
||||
com::com_initialized();
|
||||
|
||||
// Obtaining a `IAudioClient`.
|
||||
let audio_client = match device.build_audioclient() {
|
||||
Ok(client) => client,
|
||||
Err(ref e) if e.raw_os_error() == Some(AUDCLNT_E_DEVICE_INVALIDATED) =>
|
||||
return Err(BuildStreamError::DeviceNotAvailable),
|
||||
Err(e) => {
|
||||
let description = format!("{}", e);
|
||||
let err = BackendSpecificError { description };
|
||||
return Err(err.into());
|
||||
}
|
||||
};
|
||||
|
||||
// Computing the format and initializing the device.
|
||||
let waveformatex = {
|
||||
let format_attempt = format_to_waveformatextensible(format)
|
||||
.ok_or(BuildStreamError::FormatNotSupported)?;
|
||||
let share_mode = AUDCLNT_SHAREMODE_SHARED;
|
||||
|
||||
// Ensure the format is supported.
|
||||
match super::device::is_format_supported(audio_client, &format_attempt.Format) {
|
||||
Ok(false) => return Err(BuildStreamError::FormatNotSupported),
|
||||
Err(_) => return Err(BuildStreamError::DeviceNotAvailable),
|
||||
_ => (),
|
||||
}
|
||||
|
||||
// finally initializing the audio client
|
||||
let hresult = (*audio_client).Initialize(share_mode,
|
||||
AUDCLNT_STREAMFLAGS_EVENTCALLBACK,
|
||||
0,
|
||||
0,
|
||||
&format_attempt.Format,
|
||||
ptr::null());
|
||||
match check_result(hresult) {
|
||||
Err(ref e)
|
||||
if e.raw_os_error() == Some(AUDCLNT_E_DEVICE_INVALIDATED) => {
|
||||
(*audio_client).Release();
|
||||
return Err(BuildStreamError::DeviceNotAvailable);
|
||||
},
|
||||
Err(e) => {
|
||||
(*audio_client).Release();
|
||||
let description = format!("{}", e);
|
||||
let err = BackendSpecificError { description };
|
||||
return Err(err.into());
|
||||
},
|
||||
Ok(()) => (),
|
||||
};
|
||||
|
||||
format_attempt.Format
|
||||
};
|
||||
|
||||
// Creating the event that will be signalled whenever we need to submit some samples.
|
||||
let event = {
|
||||
let event = synchapi::CreateEventA(ptr::null_mut(), 0, 0, ptr::null());
|
||||
if event == ptr::null_mut() {
|
||||
(*audio_client).Release();
|
||||
let description = format!("failed to create event");
|
||||
let err = BackendSpecificError { description };
|
||||
return Err(err.into());
|
||||
}
|
||||
|
||||
match check_result((*audio_client).SetEventHandle(event)) {
|
||||
Err(e) => {
|
||||
(*audio_client).Release();
|
||||
let description = format!("failed to call SetEventHandle: {}", e);
|
||||
let err = BackendSpecificError { description };
|
||||
return Err(err.into());
|
||||
},
|
||||
Ok(_) => (),
|
||||
};
|
||||
|
||||
event
|
||||
};
|
||||
|
||||
// obtaining the size of the samples buffer in number of frames
|
||||
let max_frames_in_buffer = {
|
||||
let mut max_frames_in_buffer = mem::uninitialized();
|
||||
let hresult = (*audio_client).GetBufferSize(&mut max_frames_in_buffer);
|
||||
|
||||
match check_result(hresult) {
|
||||
Err(ref e)
|
||||
if e.raw_os_error() == Some(AUDCLNT_E_DEVICE_INVALIDATED) => {
|
||||
(*audio_client).Release();
|
||||
return Err(BuildStreamError::DeviceNotAvailable);
|
||||
},
|
||||
Err(e) => {
|
||||
(*audio_client).Release();
|
||||
let description = format!("failed to obtain buffer size: {}", e);
|
||||
let err = BackendSpecificError { description };
|
||||
return Err(err.into());
|
||||
},
|
||||
Ok(()) => (),
|
||||
};
|
||||
|
||||
max_frames_in_buffer
|
||||
};
|
||||
|
||||
// Building a `IAudioRenderClient` that will be used to fill the samples buffer.
|
||||
let render_client = {
|
||||
let mut render_client: *mut audioclient::IAudioRenderClient = mem::uninitialized();
|
||||
let hresult = (*audio_client).GetService(&audioclient::IID_IAudioRenderClient,
|
||||
&mut render_client as
|
||||
*mut *mut audioclient::IAudioRenderClient as
|
||||
*mut _);
|
||||
|
||||
match check_result(hresult) {
|
||||
Err(ref e)
|
||||
if e.raw_os_error() == Some(AUDCLNT_E_DEVICE_INVALIDATED) => {
|
||||
(*audio_client).Release();
|
||||
return Err(BuildStreamError::DeviceNotAvailable);
|
||||
},
|
||||
Err(e) => {
|
||||
(*audio_client).Release();
|
||||
let description = format!("failed to build render client: {}", e);
|
||||
let err = BackendSpecificError { description };
|
||||
return Err(err.into());
|
||||
},
|
||||
Ok(()) => (),
|
||||
};
|
||||
|
||||
&mut *render_client
|
||||
};
|
||||
|
||||
let new_stream_id = StreamId(self.next_stream_id.fetch_add(1, Ordering::Relaxed));
|
||||
if new_stream_id.0 == usize::max_value() {
|
||||
return Err(BuildStreamError::StreamIdOverflow);
|
||||
}
|
||||
|
||||
// Once we built the `StreamInner`, we add a command that will be picked up by the
|
||||
// `run()` method and added to the `RunContext`.
|
||||
{
|
||||
let client_flow = AudioClientFlow::Render {
|
||||
render_client: render_client,
|
||||
};
|
||||
let inner = StreamInner {
|
||||
id: new_stream_id.clone(),
|
||||
audio_client: audio_client,
|
||||
client_flow: client_flow,
|
||||
event: event,
|
||||
playing: false,
|
||||
max_frames_in_buffer: max_frames_in_buffer,
|
||||
bytes_per_frame: waveformatex.nBlockAlign,
|
||||
sample_format: format.data_type,
|
||||
};
|
||||
|
||||
self.push_command(Command::NewStream(inner));
|
||||
};
|
||||
|
||||
Ok(new_stream_id)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn destroy_stream(&self, stream_id: StreamId) {
|
||||
self.push_command(Command::DestroyStream(stream_id));
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn run<F>(&self, mut callback: F) -> !
|
||||
where F: FnMut(StreamId, StreamDataResult)
|
||||
{
|
||||
self.run_inner(&mut callback);
|
||||
}
|
||||
|
||||
fn run_inner(&self, callback: &mut dyn FnMut(StreamId, StreamDataResult)) -> ! {
|
||||
unsafe {
|
||||
// We keep `run_context` locked forever, which guarantees that two invocations of
|
||||
// `run()` cannot run simultaneously.
|
||||
let mut run_context = self.run_context.lock().unwrap();
|
||||
// Force a deref so that borrow checker can operate on each field independently.
|
||||
// Shadow the name because we don't use (or drop) it otherwise.
|
||||
let run_context = &mut *run_context;
|
||||
|
||||
// Keep track of the set of streams that should be removed due to some error occurring.
|
||||
//
|
||||
// Checked at the start of each loop.
|
||||
let mut streams_to_remove: Vec<(StreamId, StreamError)> = vec![];
|
||||
|
||||
'stream_loop: loop {
|
||||
// Remove any failed streams.
|
||||
for (stream_id, err) in streams_to_remove.drain(..) {
|
||||
match run_context.streams.iter().position(|s| s.id == stream_id) {
|
||||
None => continue,
|
||||
Some(p) => {
|
||||
run_context.handles.remove(p + 1);
|
||||
run_context.streams.remove(p);
|
||||
callback(stream_id, Err(err.into()));
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Process queued commands.
|
||||
process_commands(run_context, callback);
|
||||
|
||||
// Wait for any of the handles to be signalled.
|
||||
let handle_idx = match wait_for_handle_signal(&run_context.handles) {
|
||||
Ok(idx) => idx,
|
||||
Err(err) => {
|
||||
for stream in &run_context.streams {
|
||||
callback(stream.id.clone(), Err(err.clone().into()));
|
||||
}
|
||||
run_context.streams.clear();
|
||||
run_context.handles.truncate(1);
|
||||
break 'stream_loop;
|
||||
}
|
||||
};
|
||||
|
||||
// If `handle_idx` is 0, then it's `pending_scheduled_event` that was signalled in
|
||||
// order for us to pick up the pending commands. Otherwise, a stream needs data.
|
||||
if handle_idx == 0 {
|
||||
continue;
|
||||
}
|
||||
|
||||
let stream_idx = handle_idx - 1;
|
||||
let stream = &mut run_context.streams[stream_idx];
|
||||
|
||||
// The number of frames available for reading/writing.
|
||||
let mut frames_available = match get_available_frames(stream) {
|
||||
Ok(0) => continue, // TODO: Can this happen?
|
||||
Ok(n) => n,
|
||||
Err(err) => {
|
||||
streams_to_remove.push((stream.id.clone(), err));
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
let sample_size = stream.sample_format.sample_size();
|
||||
|
||||
// Obtaining a pointer to the buffer.
|
||||
match stream.client_flow {
|
||||
|
||||
AudioClientFlow::Capture { capture_client } => {
|
||||
// Get the available data in the shared buffer.
|
||||
let mut buffer: *mut BYTE = mem::uninitialized();
|
||||
let mut flags = mem::uninitialized();
|
||||
let hresult = (*capture_client).GetBuffer(
|
||||
&mut buffer,
|
||||
&mut frames_available,
|
||||
&mut flags,
|
||||
ptr::null_mut(),
|
||||
ptr::null_mut(),
|
||||
);
|
||||
|
||||
// TODO: Can this happen?
|
||||
if hresult == AUDCLNT_S_BUFFER_EMPTY {
|
||||
continue;
|
||||
} else if let Err(err) = stream_error_from_hresult(hresult) {
|
||||
streams_to_remove.push((stream.id.clone(), err));
|
||||
continue;
|
||||
}
|
||||
|
||||
debug_assert!(!buffer.is_null());
|
||||
|
||||
let buffer_len = frames_available as usize
|
||||
* stream.bytes_per_frame as usize / sample_size;
|
||||
|
||||
// Simplify the capture callback sample format branches.
|
||||
macro_rules! capture_callback {
|
||||
($T:ty, $Variant:ident) => {{
|
||||
let buffer_data = buffer as *mut _ as *const $T;
|
||||
let slice = slice::from_raw_parts(buffer_data, buffer_len);
|
||||
let unknown_buffer = UnknownTypeInputBuffer::$Variant(::InputBuffer {
|
||||
buffer: slice,
|
||||
});
|
||||
let data = StreamData::Input { buffer: unknown_buffer };
|
||||
callback(stream.id.clone(), Ok(data));
|
||||
// Release the buffer.
|
||||
let hresult = (*capture_client).ReleaseBuffer(frames_available);
|
||||
if let Err(err) = stream_error_from_hresult(hresult) {
|
||||
streams_to_remove.push((stream.id.clone(), err));
|
||||
continue;
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
||||
match stream.sample_format {
|
||||
SampleFormat::F32 => capture_callback!(f32, F32),
|
||||
SampleFormat::I16 => capture_callback!(i16, I16),
|
||||
SampleFormat::U16 => capture_callback!(u16, U16),
|
||||
}
|
||||
},
|
||||
|
||||
AudioClientFlow::Render { render_client } => {
|
||||
let mut buffer: *mut BYTE = mem::uninitialized();
|
||||
let hresult = (*render_client).GetBuffer(
|
||||
frames_available,
|
||||
&mut buffer as *mut *mut _,
|
||||
);
|
||||
|
||||
if let Err(err) = stream_error_from_hresult(hresult) {
|
||||
streams_to_remove.push((stream.id.clone(), err));
|
||||
continue;
|
||||
}
|
||||
|
||||
debug_assert!(!buffer.is_null());
|
||||
let buffer_len = frames_available as usize
|
||||
* stream.bytes_per_frame as usize / sample_size;
|
||||
|
||||
// Simplify the render callback sample format branches.
|
||||
macro_rules! render_callback {
|
||||
($T:ty, $Variant:ident) => {{
|
||||
let buffer_data = buffer as *mut $T;
|
||||
let slice = slice::from_raw_parts_mut(buffer_data, buffer_len);
|
||||
let unknown_buffer = UnknownTypeOutputBuffer::$Variant(::OutputBuffer {
|
||||
buffer: slice
|
||||
});
|
||||
let data = StreamData::Output { buffer: unknown_buffer };
|
||||
callback(stream.id.clone(), Ok(data));
|
||||
let hresult = (*render_client)
|
||||
.ReleaseBuffer(frames_available as u32, 0);
|
||||
if let Err(err) = stream_error_from_hresult(hresult) {
|
||||
streams_to_remove.push((stream.id.clone(), err));
|
||||
continue;
|
||||
}
|
||||
}}
|
||||
}
|
||||
|
||||
match stream.sample_format {
|
||||
SampleFormat::F32 => render_callback!(f32, F32),
|
||||
SampleFormat::I16 => render_callback!(i16, I16),
|
||||
SampleFormat::U16 => render_callback!(u16, U16),
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
panic!("`cpal::EventLoop::run` API currently disallows returning");
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn play_stream(&self, stream: StreamId) -> Result<(), PlayStreamError> {
|
||||
self.push_command(Command::PlayStream(stream));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn pause_stream(&self, stream: StreamId) -> Result<(), PauseStreamError> {
|
||||
self.push_command(Command::PauseStream(stream));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn push_command(&self, command: Command) {
|
||||
// Safe to unwrap: sender outlives receiver.
|
||||
self.commands.send(command).unwrap();
|
||||
unsafe {
|
||||
let result = synchapi::SetEvent(self.pending_scheduled_event);
|
||||
assert!(result != 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for EventLoop {
|
||||
#[inline]
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
handleapi::CloseHandle(self.pending_scheduled_event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl Send for EventLoop {
|
||||
}
|
||||
unsafe impl Sync for EventLoop {
|
||||
}
|
||||
|
||||
// The content of a stream ID is a number that was fetched from `next_stream_id`.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct StreamId(usize);
|
||||
|
||||
impl Drop for AudioClientFlow {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
match *self {
|
||||
AudioClientFlow::Capture { capture_client } => (*capture_client).Release(),
|
||||
AudioClientFlow::Render { render_client } => (*render_client).Release(),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for StreamInner {
|
||||
#[inline]
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
(*self.audio_client).Release();
|
||||
handleapi::CloseHandle(self.event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Turns a `Format` into a `WAVEFORMATEXTENSIBLE`.
|
||||
//
|
||||
// Returns `None` if the WAVEFORMATEXTENSIBLE does not support the given format.
|
||||
fn format_to_waveformatextensible(format: &Format) -> Option<mmreg::WAVEFORMATEXTENSIBLE> {
|
||||
let format_tag = match format.data_type {
|
||||
SampleFormat::I16 => mmreg::WAVE_FORMAT_PCM,
|
||||
SampleFormat::F32 => mmreg::WAVE_FORMAT_EXTENSIBLE,
|
||||
SampleFormat::U16 => return None,
|
||||
};
|
||||
let channels = format.channels as WORD;
|
||||
let sample_rate = format.sample_rate.0 as DWORD;
|
||||
let sample_bytes = format.data_type.sample_size() as WORD;
|
||||
let avg_bytes_per_sec = channels as DWORD * sample_rate * sample_bytes as DWORD;
|
||||
let block_align = channels * sample_bytes;
|
||||
let bits_per_sample = 8 * sample_bytes;
|
||||
let cb_size = match format.data_type {
|
||||
SampleFormat::I16 => 0,
|
||||
SampleFormat::F32 => {
|
||||
let extensible_size = mem::size_of::<mmreg::WAVEFORMATEXTENSIBLE>();
|
||||
let ex_size = mem::size_of::<mmreg::WAVEFORMATEX>();
|
||||
(extensible_size - ex_size) as WORD
|
||||
},
|
||||
SampleFormat::U16 => return None,
|
||||
};
|
||||
let waveformatex = mmreg::WAVEFORMATEX {
|
||||
wFormatTag: format_tag,
|
||||
nChannels: channels,
|
||||
nSamplesPerSec: sample_rate,
|
||||
nAvgBytesPerSec: avg_bytes_per_sec,
|
||||
nBlockAlign: block_align,
|
||||
wBitsPerSample: bits_per_sample,
|
||||
cbSize: cb_size,
|
||||
};
|
||||
|
||||
// CPAL does not care about speaker positions, so pass audio straight through.
|
||||
// TODO: This constant should be defined in winapi but is missing.
|
||||
const KSAUDIO_SPEAKER_DIRECTOUT: DWORD = 0;
|
||||
let channel_mask = KSAUDIO_SPEAKER_DIRECTOUT;
|
||||
|
||||
let sub_format = match format.data_type {
|
||||
SampleFormat::I16 => ksmedia::KSDATAFORMAT_SUBTYPE_PCM,
|
||||
SampleFormat::F32 => ksmedia::KSDATAFORMAT_SUBTYPE_IEEE_FLOAT,
|
||||
SampleFormat::U16 => return None,
|
||||
};
|
||||
let waveformatextensible = mmreg::WAVEFORMATEXTENSIBLE {
|
||||
Format: waveformatex,
|
||||
Samples: bits_per_sample as WORD,
|
||||
dwChannelMask: channel_mask,
|
||||
SubFormat: sub_format,
|
||||
};
|
||||
|
||||
Some(waveformatextensible)
|
||||
}
|
||||
|
||||
// Process any pending commands that are queued within the `RunContext`.
|
||||
fn process_commands(
|
||||
run_context: &mut RunContext,
|
||||
callback: &mut dyn FnMut(StreamId, StreamDataResult),
|
||||
) {
|
||||
// Process the pending commands.
|
||||
for command in run_context.commands.try_iter() {
|
||||
match command {
|
||||
Command::NewStream(stream_inner) => {
|
||||
let event = stream_inner.event;
|
||||
run_context.streams.push(stream_inner);
|
||||
run_context.handles.push(event);
|
||||
},
|
||||
Command::DestroyStream(stream_id) => {
|
||||
match run_context.streams.iter().position(|s| s.id == stream_id) {
|
||||
None => continue,
|
||||
Some(p) => {
|
||||
run_context.handles.remove(p + 1);
|
||||
run_context.streams.remove(p);
|
||||
},
|
||||
}
|
||||
},
|
||||
Command::PlayStream(stream_id) => {
|
||||
match run_context.streams.iter().position(|s| s.id == stream_id) {
|
||||
None => continue,
|
||||
Some(p) => {
|
||||
if !run_context.streams[p].playing {
|
||||
let hresult = unsafe {
|
||||
(*run_context.streams[p].audio_client).Start()
|
||||
};
|
||||
match stream_error_from_hresult(hresult) {
|
||||
Ok(()) => {
|
||||
run_context.streams[p].playing = true;
|
||||
}
|
||||
Err(err) => {
|
||||
callback(stream_id, Err(err.into()));
|
||||
run_context.handles.remove(p + 1);
|
||||
run_context.streams.remove(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
Command::PauseStream(stream_id) => {
|
||||
match run_context.streams.iter().position(|s| s.id == stream_id) {
|
||||
None => continue,
|
||||
Some(p) => {
|
||||
if run_context.streams[p].playing {
|
||||
let hresult = unsafe {
|
||||
(*run_context.streams[p].audio_client).Stop()
|
||||
};
|
||||
match stream_error_from_hresult(hresult) {
|
||||
Ok(()) => {
|
||||
run_context.streams[p].playing = false;
|
||||
}
|
||||
Err(err) => {
|
||||
callback(stream_id, Err(err.into()));
|
||||
run_context.handles.remove(p + 1);
|
||||
run_context.streams.remove(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Wait for any of the given handles to be signalled.
|
||||
//
|
||||
// Returns the index of the `handle` that was signalled, or an `Err` if
|
||||
// `WaitForMultipleObjectsEx` fails.
|
||||
//
|
||||
// This is called when the `run` thread is ready to wait for the next event. The
|
||||
// next event might be some command submitted by the user (the first handle) or
|
||||
// might indicate that one of the streams is ready to deliver or receive audio.
|
||||
fn wait_for_handle_signal(handles: &[winnt::HANDLE]) -> Result<usize, BackendSpecificError> {
|
||||
debug_assert!(handles.len() <= winnt::MAXIMUM_WAIT_OBJECTS as usize);
|
||||
let result = unsafe {
|
||||
synchapi::WaitForMultipleObjectsEx(
|
||||
handles.len() as u32,
|
||||
handles.as_ptr(),
|
||||
FALSE, // Don't wait for all, just wait for the first
|
||||
winbase::INFINITE, // TODO: allow setting a timeout
|
||||
FALSE, // irrelevant parameter here
|
||||
)
|
||||
};
|
||||
if result == winbase::WAIT_FAILED {
|
||||
let err = unsafe {
|
||||
winapi::um::errhandlingapi::GetLastError()
|
||||
};
|
||||
let description = format!("`WaitForMultipleObjectsEx failed: {}", err);
|
||||
let err = BackendSpecificError { description };
|
||||
return Err(err);
|
||||
}
|
||||
// Notifying the corresponding task handler.
|
||||
debug_assert!(result >= winbase::WAIT_OBJECT_0);
|
||||
let handle_idx = (result - winbase::WAIT_OBJECT_0) as usize;
|
||||
Ok(handle_idx)
|
||||
}
|
||||
|
||||
// Get the number of available frames that are available for writing/reading.
|
||||
fn get_available_frames(stream: &StreamInner) -> Result<u32, StreamError> {
|
||||
unsafe {
|
||||
let mut padding = mem::uninitialized();
|
||||
let hresult = (*stream.audio_client).GetCurrentPadding(&mut padding);
|
||||
stream_error_from_hresult(hresult)?;
|
||||
Ok(stream.max_frames_in_buffer - padding)
|
||||
}
|
||||
}
|
||||
|
||||
// Convert the given `HRESULT` into a `StreamError` if it does indicate an error.
|
||||
fn stream_error_from_hresult(hresult: winnt::HRESULT) -> Result<(), StreamError> {
|
||||
if hresult == AUDCLNT_E_DEVICE_INVALIDATED {
|
||||
return Err(StreamError::DeviceNotAvailable);
|
||||
}
|
||||
if let Err(err) = check_result(hresult) {
|
||||
let description = format!("{}", err);
|
||||
let err = BackendSpecificError { description };
|
||||
return Err(err.into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue