Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove once_cell dependency #800

Merged
merged 4 commits into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ documentation = "https://docs.rs/cpal"
license = "Apache-2.0"
keywords = ["audio", "sound"]
edition = "2021"
rust-version = "1.70"

[features]
asio = ["asio-sys", "num-traits"] # Only available on Windows. See README for setup instructions.
Expand Down Expand Up @@ -41,7 +42,6 @@ windows = { version = "0.52.0", features = [
asio-sys = { version = "0.2", path = "asio-sys", optional = true }
num-traits = { version = "0.2.6", optional = true }
parking_lot = "0.12"
once_cell = "1.12"

[target.'cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd"))'.dependencies]
alsa = "0.8"
Expand Down
42 changes: 23 additions & 19 deletions src/host/wasapi/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ use crate::{
SupportedBufferSize, SupportedStreamConfig, SupportedStreamConfigRange,
SupportedStreamConfigsError, COMMON_SAMPLE_RATES,
};
use once_cell::sync::Lazy;
use std::ffi::OsString;
use std::fmt;
use std::mem;
use std::ops::{Deref, DerefMut};
use std::os::windows::ffi::OsStringExt;
use std::ptr;
use std::slice;
use std::sync::OnceLock;
use std::sync::{Arc, Mutex, MutexGuard};
use std::time::Duration;

Expand Down Expand Up @@ -881,23 +881,27 @@ impl Endpoint {
}
}

static ENUMERATOR: Lazy<Enumerator> = Lazy::new(|| {
// COM initialization is thread local, but we only need to have COM initialized in the
// thread we create the objects in
com::com_initialized();
static ENUMERATOR: OnceLock<Enumerator> = OnceLock::new();

// building the devices enumerator object
unsafe {
let enumerator = Com::CoCreateInstance::<_, Audio::IMMDeviceEnumerator>(
&Audio::MMDeviceEnumerator,
None,
Com::CLSCTX_ALL,
)
.unwrap();

Enumerator(enumerator)
}
});
fn get_enumerator() -> &'static Enumerator {
ENUMERATOR.get_or_init(|| {
// 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 enumerator = Com::CoCreateInstance::<_, Audio::IMMDeviceEnumerator>(
&Audio::MMDeviceEnumerator,
None,
Com::CLSCTX_ALL,
)
.unwrap();

Enumerator(enumerator)
}
})
}

/// Send/Sync wrapper around `IMMDeviceEnumerator`.
struct Enumerator(Audio::IMMDeviceEnumerator);
Expand All @@ -916,7 +920,7 @@ impl Devices {
pub fn new() -> Result<Self, DevicesError> {
unsafe {
// can fail because of wrong parameters (should never happen) or out of memory
let collection = ENUMERATOR
let collection = get_enumerator()
.0
.EnumAudioEndpoints(Audio::eAll, Audio::DEVICE_STATE_ACTIVE)
.map_err(BackendSpecificError::from)?;
Expand Down Expand Up @@ -960,7 +964,7 @@ impl Iterator for Devices {

fn default_device(data_flow: Audio::EDataFlow) -> Option<Device> {
unsafe {
let device = ENUMERATOR
let device = get_enumerator()
.0
.GetDefaultAudioEndpoint(data_flow, Audio::eConsole)
.ok()?;
Expand Down
Loading