Skip to content

Commit 50f26fa

Browse files
authored
Merge pull request #504 from b-ma/fix/owned-render-capacity
RenderCapacity - return owned value
2 parents cc7a6c1 + 41ca025 commit 50f26fa

File tree

2 files changed

+71
-6
lines changed

2 files changed

+71
-6
lines changed

src/capacity.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ impl AudioRenderCapacityEvent {
6565
/// Ideally the load value is below 1.0, meaning that it took less time to render the audio than it
6666
/// took to play it out. An audio buffer underrun happens when this load value is greater than 1.0: the
6767
/// system could not render audio fast enough for real-time.
68+
#[derive(Clone)]
6869
pub struct AudioRenderCapacity {
6970
context: ConcreteBaseAudioContext,
7071
receiver: Receiver<AudioRenderCapacityLoad>,
@@ -194,3 +195,67 @@ impl AudioRenderCapacity {
194195
self.context.clear_event_handler(EventType::RenderCapacity);
195196
}
196197
}
198+
199+
#[cfg(test)]
200+
mod tests {
201+
use super::*;
202+
use crate::context::{AudioContext, AudioContextOptions};
203+
204+
#[test]
205+
fn test_same_instance() {
206+
let options = AudioContextOptions {
207+
sink_id: "none".into(),
208+
..AudioContextOptions::default()
209+
};
210+
let context = AudioContext::new(options);
211+
212+
let rc1 = context.render_capacity();
213+
let rc2 = context.render_capacity();
214+
let rc3 = rc2.clone();
215+
216+
// assert all items are actually the same instance
217+
assert!(Arc::ptr_eq(&rc1.stop_send, &rc2.stop_send));
218+
assert!(Arc::ptr_eq(&rc1.stop_send, &rc3.stop_send));
219+
}
220+
221+
#[test]
222+
fn test_stop_when_not_running() {
223+
let options = AudioContextOptions {
224+
sink_id: "none".into(),
225+
..AudioContextOptions::default()
226+
};
227+
let context = AudioContext::new(options);
228+
229+
let rc = context.render_capacity();
230+
rc.stop();
231+
}
232+
233+
#[test]
234+
fn test_render_capacity() {
235+
let options = AudioContextOptions {
236+
sink_id: "none".into(),
237+
..AudioContextOptions::default()
238+
};
239+
let context = AudioContext::new(options);
240+
241+
let rc = context.render_capacity();
242+
let (send, recv) = crossbeam_channel::bounded(1);
243+
rc.set_onupdate(move |e| send.send(e).unwrap());
244+
rc.start(AudioRenderCapacityOptions {
245+
update_interval: 0.05,
246+
});
247+
let event = recv.recv().unwrap();
248+
249+
assert!(event.timestamp >= 0.);
250+
assert!(event.average_load >= 0.);
251+
assert!(event.peak_load >= 0.);
252+
assert!(event.underrun_ratio >= 0.);
253+
254+
assert!(event.timestamp.is_finite());
255+
assert!(event.average_load.is_finite());
256+
assert!(event.peak_load.is_finite());
257+
assert!(event.underrun_ratio.is_finite());
258+
259+
assert_eq!(event.event.type_, "AudioRenderCapacityEvent");
260+
}
261+
}

src/context/online.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,12 @@ impl AudioContext {
268268
self.backend_manager.lock().unwrap().sink_id().to_owned()
269269
}
270270

271+
/// Returns an [`AudioRenderCapacity`] instance associated with an AudioContext.
272+
#[must_use]
273+
pub fn render_capacity(&self) -> AudioRenderCapacity {
274+
self.render_capacity.clone()
275+
}
276+
271277
/// Update the current audio output device.
272278
///
273279
/// The provided `sink_id` string must match a device name `enumerate_devices_sync`.
@@ -712,12 +718,6 @@ impl AudioContext {
712718
let opts = node::MediaElementAudioSourceOptions { media_element };
713719
node::MediaElementAudioSourceNode::new(self, opts)
714720
}
715-
716-
/// Returns an [`AudioRenderCapacity`] instance associated with an AudioContext.
717-
#[must_use]
718-
pub fn render_capacity(&self) -> &AudioRenderCapacity {
719-
&self.render_capacity
720-
}
721721
}
722722

723723
#[cfg(test)]

0 commit comments

Comments
 (0)