Skip to content

Commit

Permalink
Merge pull request #504 from b-ma/fix/owned-render-capacity
Browse files Browse the repository at this point in the history
RenderCapacity - return owned value
  • Loading branch information
orottier authored Apr 29, 2024
2 parents cc7a6c1 + 41ca025 commit 50f26fa
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 6 deletions.
65 changes: 65 additions & 0 deletions src/capacity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ impl AudioRenderCapacityEvent {
/// Ideally the load value is below 1.0, meaning that it took less time to render the audio than it
/// took to play it out. An audio buffer underrun happens when this load value is greater than 1.0: the
/// system could not render audio fast enough for real-time.
#[derive(Clone)]
pub struct AudioRenderCapacity {
context: ConcreteBaseAudioContext,
receiver: Receiver<AudioRenderCapacityLoad>,
Expand Down Expand Up @@ -194,3 +195,67 @@ impl AudioRenderCapacity {
self.context.clear_event_handler(EventType::RenderCapacity);
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::context::{AudioContext, AudioContextOptions};

#[test]
fn test_same_instance() {
let options = AudioContextOptions {
sink_id: "none".into(),
..AudioContextOptions::default()
};
let context = AudioContext::new(options);

let rc1 = context.render_capacity();
let rc2 = context.render_capacity();
let rc3 = rc2.clone();

// assert all items are actually the same instance
assert!(Arc::ptr_eq(&rc1.stop_send, &rc2.stop_send));
assert!(Arc::ptr_eq(&rc1.stop_send, &rc3.stop_send));
}

#[test]
fn test_stop_when_not_running() {
let options = AudioContextOptions {
sink_id: "none".into(),
..AudioContextOptions::default()
};
let context = AudioContext::new(options);

let rc = context.render_capacity();
rc.stop();
}

#[test]
fn test_render_capacity() {
let options = AudioContextOptions {
sink_id: "none".into(),
..AudioContextOptions::default()
};
let context = AudioContext::new(options);

let rc = context.render_capacity();
let (send, recv) = crossbeam_channel::bounded(1);
rc.set_onupdate(move |e| send.send(e).unwrap());
rc.start(AudioRenderCapacityOptions {
update_interval: 0.05,
});
let event = recv.recv().unwrap();

assert!(event.timestamp >= 0.);
assert!(event.average_load >= 0.);
assert!(event.peak_load >= 0.);
assert!(event.underrun_ratio >= 0.);

assert!(event.timestamp.is_finite());
assert!(event.average_load.is_finite());
assert!(event.peak_load.is_finite());
assert!(event.underrun_ratio.is_finite());

assert_eq!(event.event.type_, "AudioRenderCapacityEvent");
}
}
12 changes: 6 additions & 6 deletions src/context/online.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,12 @@ impl AudioContext {
self.backend_manager.lock().unwrap().sink_id().to_owned()
}

/// Returns an [`AudioRenderCapacity`] instance associated with an AudioContext.
#[must_use]
pub fn render_capacity(&self) -> AudioRenderCapacity {
self.render_capacity.clone()
}

/// Update the current audio output device.
///
/// The provided `sink_id` string must match a device name `enumerate_devices_sync`.
Expand Down Expand Up @@ -712,12 +718,6 @@ impl AudioContext {
let opts = node::MediaElementAudioSourceOptions { media_element };
node::MediaElementAudioSourceNode::new(self, opts)
}

/// Returns an [`AudioRenderCapacity`] instance associated with an AudioContext.
#[must_use]
pub fn render_capacity(&self) -> &AudioRenderCapacity {
&self.render_capacity
}
}

#[cfg(test)]
Expand Down

0 comments on commit 50f26fa

Please sign in to comment.