@@ -279,21 +279,26 @@ impl AudioContext {
279
279
/// is currently not implemented.
280
280
#[ allow( clippy:: needless_collect, clippy:: missing_panics_doc) ]
281
281
pub fn set_sink_id_sync ( & self , sink_id : String ) -> Result < ( ) , Box < dyn Error > > {
282
+ log:: debug!( "SinkChange requested" ) ;
282
283
if self . sink_id ( ) == sink_id {
284
+ log:: debug!( "SinkChange: no-op" ) ;
283
285
return Ok ( ( ) ) ; // sink is already active
284
286
}
285
287
286
288
if !is_valid_sink_id ( & sink_id) {
287
289
Err ( format ! ( "NotFoundError: invalid sinkId {sink_id}" ) ) ?;
288
290
} ;
289
291
292
+ log:: debug!( "SinkChange: locking backend manager" ) ;
290
293
let mut backend_manager_guard = self . backend_manager . lock ( ) . unwrap ( ) ;
291
294
let original_state = self . state ( ) ;
292
295
if original_state == AudioContextState :: Closed {
296
+ log:: debug!( "SinkChange: context is closed" ) ;
293
297
return Ok ( ( ) ) ;
294
298
}
295
299
296
300
// Acquire exclusive lock on ctrl msg sender
301
+ log:: debug!( "SinkChange: locking message channel" ) ;
297
302
let ctrl_msg_send = self . base . lock_control_msg_sender ( ) ;
298
303
299
304
// Flush out the ctrl msg receiver, cache
@@ -303,13 +308,17 @@ impl AudioContext {
303
308
let graph = if matches ! ( pending_msgs. first( ) , Some ( ControlMessage :: Startup { .. } ) ) {
304
309
// Handle the edge case where the previous backend was suspended for its entire lifetime.
305
310
// In this case, the `Startup` control message was never processed.
311
+ log:: debug!( "SinkChange: recover unstarted graph" ) ;
312
+
306
313
let msg = pending_msgs. remove ( 0 ) ;
307
314
match msg {
308
315
ControlMessage :: Startup { graph } => graph,
309
316
_ => unreachable ! ( ) ,
310
317
}
311
318
} else {
312
319
// Acquire the audio graph from the current render thread, shutting it down
320
+ log:: debug!( "SinkChange: recover graph from render thread" ) ;
321
+
313
322
let ( graph_send, graph_recv) = crossbeam_channel:: bounded ( 1 ) ;
314
323
let message = ControlMessage :: CloseAndRecycle { sender : graph_send } ;
315
324
ctrl_msg_send. send ( message) . unwrap ( ) ;
@@ -321,6 +330,7 @@ impl AudioContext {
321
330
graph_recv. recv ( ) . unwrap ( )
322
331
} ;
323
332
333
+ log:: debug!( "SinkChange: closing audio stream" ) ;
324
334
backend_manager_guard. close ( ) ;
325
335
326
336
// hotswap the backend
@@ -330,10 +340,12 @@ impl AudioContext {
330
340
sink_id,
331
341
render_size_hint : AudioContextRenderSizeCategory :: default ( ) , // todo reuse existing setting
332
342
} ;
343
+ log:: debug!( "SinkChange: starting audio stream" ) ;
333
344
* backend_manager_guard = io:: build_output ( options, self . render_thread_init . clone ( ) ) ;
334
345
335
346
// if the previous backend state was suspend, suspend the new one before shipping the graph
336
347
if original_state == AudioContextState :: Suspended {
348
+ log:: debug!( "SinkChange: suspending audio stream" ) ;
337
349
backend_manager_guard. suspend ( ) ;
338
350
}
339
351
@@ -352,6 +364,7 @@ impl AudioContext {
352
364
// trigger event when all the work is done
353
365
let _ = self . base . send_event ( EventDispatch :: sink_change ( ) ) ;
354
366
367
+ log:: debug!( "SinkChange: done" ) ;
355
368
Ok ( ( ) )
356
369
}
357
370
@@ -422,6 +435,7 @@ impl AudioContext {
422
435
/// * The audio device is not available
423
436
/// * For a `BackendSpecificError`
424
437
pub async fn suspend ( & self ) {
438
+ log:: debug!( "Suspend called" ) ;
425
439
// First, pause rendering via a control message
426
440
let ( sender, receiver) = oneshot:: channel ( ) ;
427
441
let notify = OneshotNotify :: Async ( sender) ;
@@ -430,10 +444,13 @@ impl AudioContext {
430
444
431
445
// Wait for the render thread to have processed the suspend message.
432
446
// The AudioContextState will be updated by the render thread.
447
+ log:: debug!( "Suspending audio graph, waiting for signal.." ) ;
433
448
receiver. await . unwrap ( ) ;
434
449
435
450
// Then ask the audio host to suspend the stream
451
+ log:: debug!( "Suspended audio graph. Suspending audio stream.." ) ;
436
452
self . backend_manager . lock ( ) . unwrap ( ) . suspend ( ) ;
453
+ log:: debug!( "Suspended audio stream" ) ;
437
454
}
438
455
439
456
/// Resumes the progression of time in an audio context that has previously been
@@ -446,10 +463,12 @@ impl AudioContext {
446
463
/// * The audio device is not available
447
464
/// * For a `BackendSpecificError`
448
465
pub async fn resume ( & self ) {
466
+ log:: debug!( "Resume called" ) ;
449
467
// First ask the audio host to resume the stream
450
468
self . backend_manager . lock ( ) . unwrap ( ) . resume ( ) ;
451
469
452
470
// Then, ask to resume rendering via a control message
471
+ log:: debug!( "Resumed audio stream, waking audio graph" ) ;
453
472
let ( sender, receiver) = oneshot:: channel ( ) ;
454
473
let notify = OneshotNotify :: Async ( sender) ;
455
474
self . base
@@ -458,6 +477,7 @@ impl AudioContext {
458
477
// Wait for the render thread to have processed the resume message
459
478
// The AudioContextState will be updated by the render thread.
460
479
receiver. await . unwrap ( ) ;
480
+ log:: debug!( "Resumed audio graph" ) ;
461
481
}
462
482
463
483
/// Closes the `AudioContext`, releasing the system resources being used.
@@ -469,22 +489,26 @@ impl AudioContext {
469
489
///
470
490
/// Will panic when this function is called multiple times
471
491
pub async fn close ( & self ) {
492
+ log:: debug!( "Close called" ) ;
493
+
472
494
// First, stop rendering via a control message
473
495
let ( sender, receiver) = oneshot:: channel ( ) ;
474
496
let notify = OneshotNotify :: Async ( sender) ;
475
497
self . base . send_control_msg ( ControlMessage :: Close { notify } ) ;
476
498
477
499
// Wait for the render thread to have processed the suspend message.
478
500
// The AudioContextState will be updated by the render thread.
501
+ log:: debug!( "Suspending audio graph, waiting for signal.." ) ;
479
502
receiver. await . unwrap ( ) ;
480
503
481
504
// Then ask the audio host to close the stream
505
+ log:: debug!( "Suspended audio graph. Closing audio stream.." ) ;
482
506
self . backend_manager . lock ( ) . unwrap ( ) . close ( ) ;
483
507
484
508
// Stop the AudioRenderCapacity collection thread
485
509
self . render_capacity . stop ( ) ;
486
510
487
- // TODO stop the event loop <https://github.com/orottier/web- audio-api-rs/issues/421>
511
+ log :: debug! ( "Closed audio stream" ) ;
488
512
}
489
513
490
514
/// Suspends the progression of time in the audio context.
@@ -502,6 +526,7 @@ impl AudioContext {
502
526
/// * The audio device is not available
503
527
/// * For a `BackendSpecificError`
504
528
pub fn suspend_sync ( & self ) {
529
+ log:: debug!( "Suspend_sync called" ) ;
505
530
// First, pause rendering via a control message
506
531
let ( sender, receiver) = crossbeam_channel:: bounded ( 0 ) ;
507
532
let notify = OneshotNotify :: Sync ( sender) ;
@@ -510,10 +535,13 @@ impl AudioContext {
510
535
511
536
// Wait for the render thread to have processed the suspend message.
512
537
// The AudioContextState will be updated by the render thread.
538
+ log:: debug!( "Suspending audio graph, waiting for signal.." ) ;
513
539
receiver. recv ( ) . ok ( ) ;
540
+ log:: debug!( "Suspended audio graph. Suspending audio stream.." ) ;
514
541
515
542
// Then ask the audio host to suspend the stream
516
543
self . backend_manager . lock ( ) . unwrap ( ) . suspend ( ) ;
544
+ log:: debug!( "Suspended audio stream" ) ;
517
545
}
518
546
519
547
/// Resumes the progression of time in an audio context that has previously been
@@ -529,10 +557,12 @@ impl AudioContext {
529
557
/// * The audio device is not available
530
558
/// * For a `BackendSpecificError`
531
559
pub fn resume_sync ( & self ) {
560
+ log:: debug!( "Resume_sync called" ) ;
532
561
// First ask the audio host to resume the stream
533
562
self . backend_manager . lock ( ) . unwrap ( ) . resume ( ) ;
534
563
535
564
// Then, ask to resume rendering via a control message
565
+ log:: debug!( "Resumed audio stream, waking audio graph" ) ;
536
566
let ( sender, receiver) = crossbeam_channel:: bounded ( 0 ) ;
537
567
let notify = OneshotNotify :: Sync ( sender) ;
538
568
self . base
@@ -541,6 +571,7 @@ impl AudioContext {
541
571
// Wait for the render thread to have processed the resume message
542
572
// The AudioContextState will be updated by the render thread.
543
573
receiver. recv ( ) . ok ( ) ;
574
+ log:: debug!( "Resumed audio graph" ) ;
544
575
}
545
576
546
577
/// Closes the `AudioContext`, releasing the system resources being used.
@@ -555,22 +586,26 @@ impl AudioContext {
555
586
///
556
587
/// Will panic when this function is called multiple times
557
588
pub fn close_sync ( & self ) {
589
+ log:: debug!( "Close_sync called" ) ;
590
+
558
591
// First, stop rendering via a control message
559
592
let ( sender, receiver) = crossbeam_channel:: bounded ( 0 ) ;
560
593
let notify = OneshotNotify :: Sync ( sender) ;
561
594
self . base . send_control_msg ( ControlMessage :: Close { notify } ) ;
562
595
563
596
// Wait for the render thread to have processed the suspend message.
564
597
// The AudioContextState will be updated by the render thread.
598
+ log:: debug!( "Suspending audio graph, waiting for signal.." ) ;
565
599
receiver. recv ( ) . ok ( ) ;
566
600
567
601
// Then ask the audio host to close the stream
602
+ log:: debug!( "Suspended audio graph. Closing audio stream.." ) ;
568
603
self . backend_manager . lock ( ) . unwrap ( ) . close ( ) ;
569
604
570
605
// Stop the AudioRenderCapacity collection thread
571
606
self . render_capacity . stop ( ) ;
572
607
573
- // TODO stop the event loop <https://github.com/orottier/web- audio-api-rs/issues/421>
608
+ log :: debug! ( "Closed audio stream" ) ;
574
609
}
575
610
576
611
/// Creates a [`MediaStreamAudioSourceNode`](node::MediaStreamAudioSourceNode) from a
0 commit comments