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

Prevent the wpt-runner from crashing by avoiding some panics #403

Merged
merged 3 commits into from
Dec 2, 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
1 change: 1 addition & 0 deletions src/node/channel_merger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ impl AudioNode for ChannelMergerNode {
impl ChannelMergerNode {
pub fn new<C: BaseAudioContext>(context: &C, mut options: ChannelMergerOptions) -> Self {
context.register(move |registration| {
crate::assert_valid_number_of_channels(options.number_of_inputs);
options.channel_config.count = options.number_of_inputs;

let node = ChannelMergerNode {
Expand Down
1 change: 1 addition & 0 deletions src/node/channel_splitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ impl AudioNode for ChannelSplitterNode {
impl ChannelSplitterNode {
pub fn new<C: BaseAudioContext>(context: &C, mut options: ChannelSplitterOptions) -> Self {
context.register(move |registration| {
crate::assert_valid_number_of_channels(options.number_of_outputs);
options.channel_config.count = options.number_of_outputs;

let node = ChannelSplitterNode {
Expand Down
2 changes: 2 additions & 0 deletions src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ impl ChannelConfig {

impl From<ChannelConfigOptions> for ChannelConfig {
fn from(opts: ChannelConfigOptions) -> Self {
crate::assert_valid_number_of_channels(opts.count);

let inner = ChannelConfigInner {
count: AtomicUsize::from(opts.count),
count_mode: AtomicU32::from(opts.count_mode as u32),
Expand Down
36 changes: 21 additions & 15 deletions src/node/panner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,27 @@ use super::{
pub(crate) fn load_hrtf_processor(sample_rate: u32) -> (HrtfProcessor, usize) {
static INSTANCE: OnceLock<Mutex<HashMap<u32, (HrtfProcessor, usize)>>> = OnceLock::new();
let cache = INSTANCE.get_or_init(|| Mutex::new(HashMap::new()));
let mut guard = cache.lock().unwrap();
guard
.entry(sample_rate)
.or_insert_with(|| {
let resource = include_bytes!("../../resources/IRC_1003_C.bin");
let hrir_sphere = HrirSphere::new(&resource[..], sample_rate).unwrap();
let len = hrir_sphere.len();

let interpolation_steps = 1; // TODO?
let samples_per_step = RENDER_QUANTUM_SIZE / interpolation_steps;
let processor = HrtfProcessor::new(hrir_sphere, interpolation_steps, samples_per_step);

(processor, len)
})
.clone()

// To avoid poisening the cache mutex, don't use the `entry()` API on HashMap
{
if let Some(value) = cache.lock().unwrap().get(&sample_rate) {
return value.clone();
}
}

// The following snippet might panic
let resource = include_bytes!("../../resources/IRC_1003_C.bin");
let hrir_sphere = HrirSphere::new(&resource[..], sample_rate).unwrap();
let len = hrir_sphere.len();

let interpolation_steps = 1; // TODO?
let samples_per_step = RENDER_QUANTUM_SIZE / interpolation_steps;
let processor = HrtfProcessor::new(hrir_sphere, interpolation_steps, samples_per_step);

let value = (processor, len);
cache.lock().unwrap().insert(sample_rate, value.clone());

value
}

/// Spatialization algorithm used to position the audio in 3D space
Expand Down
Loading