@@ -65,6 +65,7 @@ impl AudioRenderCapacityEvent {
65
65
/// Ideally the load value is below 1.0, meaning that it took less time to render the audio than it
66
66
/// took to play it out. An audio buffer underrun happens when this load value is greater than 1.0: the
67
67
/// system could not render audio fast enough for real-time.
68
+ #[ derive( Clone ) ]
68
69
pub struct AudioRenderCapacity {
69
70
context : ConcreteBaseAudioContext ,
70
71
receiver : Receiver < AudioRenderCapacityLoad > ,
@@ -194,3 +195,67 @@ impl AudioRenderCapacity {
194
195
self . context . clear_event_handler ( EventType :: RenderCapacity ) ;
195
196
}
196
197
}
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
+ }
0 commit comments