Skip to content

Commit

Permalink
Remove once_cell dependency (#800)
Browse files Browse the repository at this point in the history
* Remove once_cell dependency.

* Add rust-version and set it to 1.70
  • Loading branch information
james7132 authored Dec 17, 2023
1 parent 76b0406 commit 310160f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
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

0 comments on commit 310160f

Please sign in to comment.