4
4
// accompanying file LICENSE for details.
5
5
#![ allow( unused_assignments) ]
6
6
#![ allow( unused_must_use) ]
7
-
8
7
extern crate coreaudio_sys_utils;
9
8
extern crate libc;
10
9
extern crate ringbuf;
@@ -106,16 +105,6 @@ bitflags! {
106
105
}
107
106
}
108
107
109
- lazy_static ! {
110
- static ref HOST_TIME_TO_NS_RATIO : ( u32 , u32 ) = {
111
- let mut timebase_info = mach_timebase_info { numer: 0 , denom: 0 } ;
112
- unsafe {
113
- mach_timebase_info( & mut timebase_info) ;
114
- }
115
- ( timebase_info. numer, timebase_info. denom)
116
- } ;
117
- }
118
-
119
108
#[ cfg( feature = "audio-dump" ) ]
120
109
fn dump_audio ( stream : cubeb_audio_dump_stream_t , audio_samples : * mut c_void , count : u32 ) {
121
110
unsafe {
@@ -682,10 +671,10 @@ extern "C" fn audiounit_input_callback(
682
671
}
683
672
}
684
673
685
- fn host_time_to_ns ( host_time : u64 ) -> u64 {
674
+ fn host_time_to_ns ( ctx : & AudioUnitContext , host_time : u64 ) -> u64 {
686
675
let mut rv: f64 = host_time as f64 ;
687
- rv *= HOST_TIME_TO_NS_RATIO . 0 as f64 ;
688
- rv /= HOST_TIME_TO_NS_RATIO . 1 as f64 ;
676
+ rv *= ctx . host_time_to_ns_ratio . 0 as f64 ;
677
+ rv /= ctx . host_time_to_ns_ratio . 1 as f64 ;
689
678
rv as u64
690
679
}
691
680
@@ -697,7 +686,7 @@ fn compute_output_latency(stm: &AudioUnitStream, audio_output_time: u64, now: u6
697
686
// The total output latency is the timestamp difference + the stream latency + the hardware
698
687
// latency.
699
688
let total_output_latency_ns =
700
- fixed_latency_ns + host_time_to_ns ( audio_output_time. saturating_sub ( now) ) ;
689
+ fixed_latency_ns + host_time_to_ns ( stm . context , audio_output_time. saturating_sub ( now) ) ;
701
690
702
691
( total_output_latency_ns * output_hw_rate / NS2S ) as u32
703
692
}
@@ -710,7 +699,7 @@ fn compute_input_latency(stm: &AudioUnitStream, audio_input_time: u64, now: u64)
710
699
// The total input latency is the timestamp difference + the stream latency +
711
700
// the hardware latency.
712
701
let total_input_latency_ns =
713
- host_time_to_ns ( now. saturating_sub ( audio_input_time) ) + fixed_latency_ns;
702
+ host_time_to_ns ( stm . context , now. saturating_sub ( audio_input_time) ) + fixed_latency_ns;
714
703
715
704
( total_input_latency_ns * input_hw_rate / NS2S ) as u32
716
705
}
@@ -2483,6 +2472,7 @@ pub struct AudioUnitContext {
2483
2472
serial_queue : Queue ,
2484
2473
latency_controller : Mutex < LatencyController > ,
2485
2474
devices : Mutex < SharedDevices > ,
2475
+ host_time_to_ns_ratio : ( u32 , u32 ) ,
2486
2476
// Storage for a context-global vpio unit. Duplex streams that need one will take this
2487
2477
// and return it when done.
2488
2478
shared_voice_processing_unit : SharedVoiceProcessingUnitManager ,
@@ -2497,11 +2487,19 @@ impl AudioUnitContext {
2497
2487
format ! ( "{}.context.shared_vpio" , DISPATCH_QUEUE_LABEL ) . as_str ( ) ,
2498
2488
& serial_queue,
2499
2489
) ;
2490
+ let host_time_to_ns_ratio = {
2491
+ let mut timebase_info = mach_timebase_info { numer : 0 , denom : 0 } ;
2492
+ unsafe {
2493
+ mach_timebase_info ( & mut timebase_info) ;
2494
+ }
2495
+ ( timebase_info. numer , timebase_info. denom )
2496
+ } ;
2500
2497
Self {
2501
2498
_ops : & OPS as * const _ ,
2502
2499
serial_queue,
2503
2500
latency_controller : Mutex :: new ( LatencyController :: default ( ) ) ,
2504
2501
devices : Mutex :: new ( SharedDevices :: default ( ) ) ,
2502
+ host_time_to_ns_ratio,
2505
2503
shared_voice_processing_unit : SharedVoiceProcessingUnitManager :: new ( shared_vp_queue) ,
2506
2504
}
2507
2505
}
@@ -4931,7 +4929,7 @@ impl<'ctx> StreamOps for AudioUnitStream<'ctx> {
4931
4929
let now = unsafe { mach_absolute_time ( ) } ;
4932
4930
let diff = now - timestamp;
4933
4931
let interpolated_frames = cmp:: min (
4934
- host_time_to_ns ( diff)
4932
+ host_time_to_ns ( self . context , diff)
4935
4933
* self . core_stream_data . output_stream_params . rate ( ) as u64
4936
4934
/ NS2S ,
4937
4935
buffer_size,
0 commit comments