Deps: Replace OnceCell with std::sync::OnceLock (#207)

This commit is contained in:
Gnome!
2023-12-02 17:22:00 +00:00
committed by Kyle Simpson
parent 2146846df4
commit 743a1d262e
11 changed files with 59 additions and 58 deletions

View File

@@ -2,7 +2,7 @@ use super::{compressed_cost_per_sec, default_config, CodecCacheError, ToAudioByt
use crate::{
constants::*,
input::{
codecs::{dca::*, CODEC_REGISTRY, PROBE},
codecs::{dca::*, get_codec_registry, get_probe},
AudioStream,
Input,
LiveInput,
@@ -52,17 +52,13 @@ use tracing::{debug, trace};
pub struct Config {
/// Registry of audio codecs supported by the driver.
///
/// Defaults to [`CODEC_REGISTRY`], which adds audiopus-based Opus codec support
/// Defaults to [`get_codec_registry`], which adds audiopus-based Opus codec support
/// to all of Symphonia's default codecs.
///
/// [`CODEC_REGISTRY`]: static@CODEC_REGISTRY
pub codec_registry: &'static CodecRegistry,
/// Registry of the muxers and container formats supported by the driver.
///
/// Defaults to [`PROBE`], which includes all of Symphonia's default format handlers
/// Defaults to [`get_probe`], which includes all of Symphonia's default format handlers
/// and DCA format support.
///
/// [`PROBE`]: static@PROBE
pub format_registry: &'static Probe,
/// Configuration for the inner streamcatcher instance.
///
@@ -73,8 +69,8 @@ pub struct Config {
impl Default for Config {
fn default() -> Self {
Self {
codec_registry: &CODEC_REGISTRY,
format_registry: &PROBE,
codec_registry: get_codec_registry(),
format_registry: get_probe(),
streamcatcher: ScConfig::default(),
}
}

View File

@@ -4,26 +4,33 @@ pub(crate) mod dca;
mod opus;
mod raw;
use std::sync::OnceLock;
pub use self::{dca::DcaReader, opus::OpusDecoder, raw::*};
use once_cell::sync::Lazy;
use symphonia::{
core::{codecs::CodecRegistry, probe::Probe},
default::*,
};
/// Default Symphonia [`CodecRegistry`], including the (audiopus-backed) Opus codec.
pub static CODEC_REGISTRY: Lazy<CodecRegistry> = Lazy::new(|| {
let mut registry = CodecRegistry::new();
register_enabled_codecs(&mut registry);
registry.register_all::<OpusDecoder>();
registry
});
pub fn get_codec_registry() -> &'static CodecRegistry {
static CODEC_REGISTRY: OnceLock<CodecRegistry> = OnceLock::new();
CODEC_REGISTRY.get_or_init(|| {
let mut registry = CodecRegistry::new();
register_enabled_codecs(&mut registry);
registry.register_all::<OpusDecoder>();
registry
})
}
/// Default Symphonia Probe, including DCA format support.
pub static PROBE: Lazy<Probe> = Lazy::new(|| {
let mut probe = Probe::default();
probe.register_all::<DcaReader>();
probe.register_all::<RawReader>();
register_enabled_formats(&mut probe);
probe
});
pub fn get_probe() -> &'static Probe {
static PROBE: OnceLock<Probe> = OnceLock::new();
PROBE.get_or_init(|| {
let mut probe = Probe::default();
probe.register_all::<DcaReader>();
probe.register_all::<RawReader>();
register_enabled_formats(&mut probe);
probe
})
}

View File

@@ -160,7 +160,7 @@ mod tests {
// finds the audio on a non-default track via `LiveInput::promote`.
let input = Input::from(File::new(FILE_VID_TARGET));
input
.make_playable_async(&CODEC_REGISTRY, &PROBE)
.make_playable_async(get_codec_registry(), get_probe())
.await
.unwrap();
}

View File

@@ -49,8 +49,8 @@
//! [`OpusDecoder`]: codecs::OpusDecoder
//! [`DcaReader`]: codecs::DcaReader
//! [`RawReader`]: codecs::RawReader
//! [format]: static@codecs::PROBE
//! [codec registries]: static@codecs::CODEC_REGISTRY
//! [format]: codecs::get_probe
//! [codec registries]: codecs::get_codec_registry
mod adapters;
mod audiostream;
@@ -147,7 +147,7 @@ use tokio::runtime::Handle as TokioHandle;
/// //
/// // We can access it on a live track using `TrackHandle::action()`.
/// in_memory_input = in_memory_input
/// .make_playable_async(&CODEC_REGISTRY, &PROBE)
/// .make_playable_async(get_codec_registry(), get_probe())
/// .await
/// .expect("WAV support is included, and this file is good!");
///