88pub use self :: action_flags:: ActionFlags ;
99pub use self :: data:: Data ;
1010
11+ #[ cfg( target_os = "ios" ) ]
12+ use audio_unit:: audio_session_get_property;
13+
1114
1215/// When `set_render_callback` is called, a closure of this type will be used to wrap the given
1316/// render callback function.
@@ -398,7 +401,7 @@ impl AudioUnit {
398401 // First, we'll retrieve the stream format so that we can ensure that the given callback
399402 // format matches the audio unit's format.
400403 let id = sys:: kAudioUnitProperty_StreamFormat;
401- let asbd = try!( self . get_property ( id, Scope :: Output , Element :: Output ) ) ;
404+ let asbd = try!( self . get_property ( id, Scope :: Input , Element :: Output ) ) ;
402405 let stream_format = super :: StreamFormat :: from_asbd ( asbd) ?;
403406
404407 // If the stream format does not match, return an error indicating this.
@@ -471,7 +474,7 @@ impl AudioUnit {
471474 // First, we'll retrieve the stream format so that we can ensure that the given callback
472475 // format matches the audio unit's format.
473476 let id = sys:: kAudioUnitProperty_StreamFormat;
474- let asbd = self . get_property ( id, Scope :: Input , Element :: Input ) ?;
477+ let asbd = self . get_property ( id, Scope :: Output , Element :: Input ) ?;
475478 let stream_format = super :: StreamFormat :: from_asbd ( asbd) ?;
476479
477480 // If the stream format does not match, return an error indicating this.
@@ -482,8 +485,20 @@ impl AudioUnit {
482485 // Pre-allocate a buffer list for input stream.
483486 //
484487 // First, get the current buffer size for pre-allocating the `AudioBuffer`s.
485- let id = sys:: kAudioDevicePropertyBufferFrameSize;
486- let mut buffer_frame_size: u32 = self . get_property ( id, Scope :: Global , Element :: Output ) ?;
488+ #[ cfg( target_os = "macos" ) ]
489+ let mut buffer_frame_size: u32 = {
490+ let id = sys:: kAudioDevicePropertyBufferFrameSize;
491+ let buffer_frame_size: u32 = self . get_property ( id, Scope :: Global , Element :: Output ) ?;
492+ buffer_frame_size
493+ } ;
494+ #[ cfg( target_os = "ios" ) ]
495+ let mut buffer_frame_size: u32 = {
496+ let id = sys:: kAudioSessionProperty_CurrentHardwareIOBufferDuration;
497+ let seconds: f32 = super :: audio_session_get_property ( id) ?;
498+ let id = sys:: kAudioSessionProperty_CurrentHardwareSampleRate;
499+ let sample_rate: f64 = super :: audio_session_get_property ( id) ?;
500+ ( sample_rate * seconds as f64 ) . round ( ) as u32
501+ } ;
487502 let mut data: Vec < u8 > = vec ! [ ] ;
488503 let sample_bytes = stream_format. sample_format . size_in_bytes ( ) ;
489504 let n_channels = stream_format. channels_per_frame ;
@@ -525,7 +540,7 @@ impl AudioUnit {
525540 unsafe {
526541 // Retrieve the up-to-date stream format.
527542 let id = sys:: kAudioUnitProperty_StreamFormat;
528- let asbd = match super :: get_property ( audio_unit, id, Scope :: Input , Element :: Output ) {
543+ let asbd = match super :: get_property ( audio_unit, id, Scope :: Output , Element :: Input ) {
529544 Err ( err) => return err. to_os_status ( ) ,
530545 Ok ( asbd) => asbd,
531546 } ;
0 commit comments