2016-07-05 20:54:03 +00:00
|
|
|
extern crate coreaudio;
|
2018-01-26 12:49:47 +00:00
|
|
|
extern crate core_foundation_sys;
|
2015-02-28 18:50:29 +00:00
|
|
|
|
2018-02-04 12:02:16 +00:00
|
|
|
use ChannelCount;
|
2019-06-20 22:22:30 +00:00
|
|
|
use BackendSpecificError;
|
2019-06-20 19:31:15 +00:00
|
|
|
use BuildStreamError;
|
2018-02-12 13:10:24 +00:00
|
|
|
use DefaultFormatError;
|
2015-09-24 03:02:28 +00:00
|
|
|
use Format;
|
2019-06-20 19:16:39 +00:00
|
|
|
use SupportedFormatsError;
|
2016-08-12 07:49:13 +00:00
|
|
|
use Sample;
|
2015-09-24 03:02:28 +00:00
|
|
|
use SampleFormat;
|
2018-02-04 12:02:16 +00:00
|
|
|
use SampleRate;
|
2018-02-12 13:10:24 +00:00
|
|
|
use StreamData;
|
2017-10-20 19:18:40 +00:00
|
|
|
use SupportedFormat;
|
2018-02-12 13:10:24 +00:00
|
|
|
use UnknownTypeInputBuffer;
|
|
|
|
use UnknownTypeOutputBuffer;
|
2016-08-12 07:49:13 +00:00
|
|
|
|
2018-01-26 12:49:47 +00:00
|
|
|
use std::ffi::CStr;
|
2019-03-22 20:27:17 +00:00
|
|
|
use std::fmt;
|
2017-10-18 18:24:05 +00:00
|
|
|
use std::mem;
|
2018-01-26 12:49:47 +00:00
|
|
|
use std::os::raw::c_char;
|
|
|
|
use std::ptr::null;
|
2016-08-12 07:49:13 +00:00
|
|
|
use std::sync::{Arc, Mutex};
|
2017-04-07 10:06:00 +00:00
|
|
|
use std::thread;
|
|
|
|
use std::time::Duration;
|
2017-11-03 09:51:02 +00:00
|
|
|
use std::slice;
|
2016-08-12 07:49:13 +00:00
|
|
|
|
2017-11-03 09:51:02 +00:00
|
|
|
use self::coreaudio::audio_unit::{AudioUnit, Scope, Element};
|
2016-08-12 07:49:13 +00:00
|
|
|
use self::coreaudio::audio_unit::render_callback::{self, data};
|
2018-01-26 12:49:47 +00:00
|
|
|
use self::coreaudio::sys::{
|
2017-11-03 09:51:02 +00:00
|
|
|
AudioBuffer,
|
2018-01-26 12:49:47 +00:00
|
|
|
AudioBufferList,
|
|
|
|
AudioDeviceID,
|
2018-04-01 11:25:46 +00:00
|
|
|
AudioObjectAddPropertyListener,
|
2018-01-26 12:49:47 +00:00
|
|
|
AudioObjectGetPropertyData,
|
|
|
|
AudioObjectGetPropertyDataSize,
|
2018-04-01 11:25:46 +00:00
|
|
|
AudioObjectID,
|
2018-01-26 12:49:47 +00:00
|
|
|
AudioObjectPropertyAddress,
|
2018-02-12 13:10:24 +00:00
|
|
|
AudioObjectPropertyScope,
|
2018-04-01 11:25:46 +00:00
|
|
|
AudioObjectRemovePropertyListener,
|
|
|
|
AudioObjectSetPropertyData,
|
2018-01-26 12:49:47 +00:00
|
|
|
AudioStreamBasicDescription,
|
|
|
|
AudioValueRange,
|
|
|
|
kAudioDevicePropertyAvailableNominalSampleRates,
|
|
|
|
kAudioDevicePropertyDeviceNameCFString,
|
2018-04-01 11:25:46 +00:00
|
|
|
kAudioDevicePropertyNominalSampleRate,
|
2018-02-12 13:10:24 +00:00
|
|
|
kAudioObjectPropertyScopeInput,
|
2018-04-01 11:25:46 +00:00
|
|
|
kAudioObjectPropertyScopeGlobal,
|
2018-01-26 12:49:47 +00:00
|
|
|
kAudioDevicePropertyScopeOutput,
|
|
|
|
kAudioDevicePropertyStreamConfiguration,
|
2018-02-12 13:10:24 +00:00
|
|
|
kAudioDevicePropertyStreamFormat,
|
2017-11-03 09:51:02 +00:00
|
|
|
kAudioFormatFlagIsFloat,
|
|
|
|
kAudioFormatFlagIsPacked,
|
2018-01-26 12:49:47 +00:00
|
|
|
kAudioFormatLinearPCM,
|
|
|
|
kAudioHardwareNoError,
|
|
|
|
kAudioObjectPropertyElementMaster,
|
|
|
|
kAudioObjectPropertyScopeOutput,
|
|
|
|
kAudioOutputUnitProperty_CurrentDevice,
|
2018-02-12 13:10:24 +00:00
|
|
|
kAudioOutputUnitProperty_EnableIO,
|
2018-01-26 12:49:47 +00:00
|
|
|
kAudioUnitProperty_StreamFormat,
|
|
|
|
kCFStringEncodingUTF8,
|
2018-02-12 13:10:24 +00:00
|
|
|
OSStatus,
|
2018-01-26 12:49:47 +00:00
|
|
|
};
|
|
|
|
use self::core_foundation_sys::string::{
|
|
|
|
CFStringRef,
|
|
|
|
CFStringGetCStringPtr,
|
2017-11-03 09:51:02 +00:00
|
|
|
};
|
2015-02-28 18:50:29 +00:00
|
|
|
|
2015-09-24 03:02:28 +00:00
|
|
|
mod enumerate;
|
|
|
|
|
2018-02-12 13:10:24 +00:00
|
|
|
pub use self::enumerate::{Devices, SupportedInputFormats, SupportedOutputFormats, default_input_device, default_output_device};
|
2015-09-24 03:02:28 +00:00
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq)]
|
2018-02-12 13:10:24 +00:00
|
|
|
pub struct Device {
|
2018-01-26 12:49:47 +00:00
|
|
|
audio_device_id: AudioDeviceID,
|
|
|
|
}
|
2015-09-24 03:02:28 +00:00
|
|
|
|
2018-02-12 13:10:24 +00:00
|
|
|
impl Device {
|
|
|
|
pub fn name(&self) -> String {
|
|
|
|
let property_address = AudioObjectPropertyAddress {
|
|
|
|
mSelector: kAudioDevicePropertyDeviceNameCFString,
|
|
|
|
mScope: kAudioDevicePropertyScopeOutput,
|
|
|
|
mElement: kAudioObjectPropertyElementMaster,
|
|
|
|
};
|
|
|
|
let device_name: CFStringRef = null();
|
|
|
|
let data_size = mem::size_of::<CFStringRef>();
|
|
|
|
let c_str = unsafe {
|
|
|
|
let status = AudioObjectGetPropertyData(
|
|
|
|
self.audio_device_id,
|
|
|
|
&property_address as *const _,
|
|
|
|
0,
|
|
|
|
null(),
|
|
|
|
&data_size as *const _ as *mut _,
|
|
|
|
&device_name as *const _ as *mut _,
|
|
|
|
);
|
|
|
|
if status != kAudioHardwareNoError as i32 {
|
|
|
|
return format!("<OSStatus: {:?}>", status);
|
|
|
|
}
|
|
|
|
let c_string: *const c_char = CFStringGetCStringPtr(device_name, kCFStringEncodingUTF8);
|
|
|
|
if c_string == null() {
|
|
|
|
return "<null>".into();
|
|
|
|
}
|
|
|
|
CStr::from_ptr(c_string as *mut _)
|
|
|
|
};
|
|
|
|
c_str.to_string_lossy().into_owned()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Logic re-used between `supported_input_formats` and `supported_output_formats`.
|
|
|
|
fn supported_formats(
|
|
|
|
&self,
|
|
|
|
scope: AudioObjectPropertyScope,
|
2019-06-20 19:16:39 +00:00
|
|
|
) -> Result<SupportedOutputFormats, SupportedFormatsError>
|
2018-02-12 13:10:24 +00:00
|
|
|
{
|
2018-01-26 12:49:47 +00:00
|
|
|
let mut property_address = AudioObjectPropertyAddress {
|
|
|
|
mSelector: kAudioDevicePropertyStreamConfiguration,
|
2018-02-12 13:10:24 +00:00
|
|
|
mScope: scope,
|
2018-01-26 12:49:47 +00:00
|
|
|
mElement: kAudioObjectPropertyElementMaster,
|
|
|
|
};
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
// Retrieve the devices audio buffer list.
|
|
|
|
let data_size = 0u32;
|
|
|
|
let status = AudioObjectGetPropertyDataSize(
|
|
|
|
self.audio_device_id,
|
|
|
|
&property_address as *const _,
|
|
|
|
0,
|
|
|
|
null(),
|
|
|
|
&data_size as *const _ as *mut _,
|
|
|
|
);
|
2019-06-20 22:22:30 +00:00
|
|
|
check_os_status(status)?;
|
|
|
|
|
2018-01-26 12:49:47 +00:00
|
|
|
let mut audio_buffer_list: Vec<u8> = vec![];
|
|
|
|
audio_buffer_list.reserve_exact(data_size as usize);
|
|
|
|
let status = AudioObjectGetPropertyData(
|
|
|
|
self.audio_device_id,
|
|
|
|
&property_address as *const _,
|
|
|
|
0,
|
|
|
|
null(),
|
|
|
|
&data_size as *const _ as *mut _,
|
|
|
|
audio_buffer_list.as_mut_ptr() as *mut _,
|
|
|
|
);
|
2019-06-20 22:22:30 +00:00
|
|
|
check_os_status(status)?;
|
|
|
|
|
2018-01-26 12:49:47 +00:00
|
|
|
let audio_buffer_list = audio_buffer_list.as_mut_ptr() as *mut AudioBufferList;
|
|
|
|
|
|
|
|
// If there's no buffers, skip.
|
|
|
|
if (*audio_buffer_list).mNumberBuffers == 0 {
|
|
|
|
return Ok(vec![].into_iter());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Count the number of channels as the sum of all channels in all output buffers.
|
|
|
|
let n_buffers = (*audio_buffer_list).mNumberBuffers as usize;
|
|
|
|
let first: *const AudioBuffer = (*audio_buffer_list).mBuffers.as_ptr();
|
|
|
|
let buffers: &'static [AudioBuffer] = slice::from_raw_parts(first, n_buffers);
|
|
|
|
let mut n_channels = 0;
|
|
|
|
for buffer in buffers {
|
|
|
|
n_channels += buffer.mNumberChannels as usize;
|
|
|
|
}
|
|
|
|
|
|
|
|
// AFAIK the sample format should always be f32 on macos and i16 on iOS? Feel free to
|
|
|
|
// fix this if more pcm formats are supported.
|
|
|
|
let sample_format = if cfg!(target_os = "ios") {
|
|
|
|
SampleFormat::I16
|
|
|
|
} else {
|
|
|
|
SampleFormat::F32
|
|
|
|
};
|
|
|
|
|
|
|
|
// Get available sample rate ranges.
|
|
|
|
property_address.mSelector = kAudioDevicePropertyAvailableNominalSampleRates;
|
|
|
|
let data_size = 0u32;
|
|
|
|
let status = AudioObjectGetPropertyDataSize(
|
|
|
|
self.audio_device_id,
|
|
|
|
&property_address as *const _,
|
|
|
|
0,
|
|
|
|
null(),
|
|
|
|
&data_size as *const _ as *mut _,
|
|
|
|
);
|
2019-06-20 22:22:30 +00:00
|
|
|
check_os_status(status)?;
|
|
|
|
|
2018-01-26 12:49:47 +00:00
|
|
|
let n_ranges = data_size as usize / mem::size_of::<AudioValueRange>();
|
|
|
|
let mut ranges: Vec<u8> = vec![];
|
|
|
|
ranges.reserve_exact(data_size as usize);
|
|
|
|
let status = AudioObjectGetPropertyData(
|
|
|
|
self.audio_device_id,
|
|
|
|
&property_address as *const _,
|
|
|
|
0,
|
|
|
|
null(),
|
|
|
|
&data_size as *const _ as *mut _,
|
|
|
|
ranges.as_mut_ptr() as *mut _,
|
|
|
|
);
|
2019-06-20 22:22:30 +00:00
|
|
|
check_os_status(status)?;
|
|
|
|
|
2018-01-26 12:49:47 +00:00
|
|
|
let ranges: *mut AudioValueRange = ranges.as_mut_ptr() as *mut _;
|
|
|
|
let ranges: &'static [AudioValueRange] = slice::from_raw_parts(ranges, n_ranges);
|
|
|
|
|
|
|
|
// Collect the supported formats for the device.
|
|
|
|
let mut fmts = vec![];
|
|
|
|
for range in ranges {
|
|
|
|
let fmt = SupportedFormat {
|
2018-02-04 12:02:16 +00:00
|
|
|
channels: n_channels as ChannelCount,
|
|
|
|
min_sample_rate: SampleRate(range.mMinimum as _),
|
|
|
|
max_sample_rate: SampleRate(range.mMaximum as _),
|
2018-01-26 12:49:47 +00:00
|
|
|
data_type: sample_format,
|
|
|
|
};
|
|
|
|
fmts.push(fmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(fmts.into_iter())
|
|
|
|
}
|
2015-09-24 03:02:28 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 19:16:39 +00:00
|
|
|
pub fn supported_input_formats(&self) -> Result<SupportedOutputFormats, SupportedFormatsError> {
|
2018-02-12 13:10:24 +00:00
|
|
|
self.supported_formats(kAudioObjectPropertyScopeInput)
|
|
|
|
}
|
|
|
|
|
2019-06-20 19:16:39 +00:00
|
|
|
pub fn supported_output_formats(&self) -> Result<SupportedOutputFormats, SupportedFormatsError> {
|
2018-02-12 13:10:24 +00:00
|
|
|
self.supported_formats(kAudioObjectPropertyScopeOutput)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn default_format(
|
|
|
|
&self,
|
|
|
|
scope: AudioObjectPropertyScope,
|
|
|
|
) -> Result<Format, DefaultFormatError>
|
|
|
|
{
|
|
|
|
fn default_format_error_from_os_status(status: OSStatus) -> Option<DefaultFormatError> {
|
|
|
|
let err = match coreaudio::Error::from_os_status(status) {
|
|
|
|
Err(err) => err,
|
|
|
|
Ok(_) => return None,
|
|
|
|
};
|
|
|
|
match err {
|
|
|
|
coreaudio::Error::RenderCallbackBufferFormatDoesNotMatchAudioUnitStreamFormat |
|
|
|
|
coreaudio::Error::NoKnownSubtype |
|
|
|
|
coreaudio::Error::AudioUnit(coreaudio::error::AudioUnitError::FormatNotSupported) |
|
|
|
|
coreaudio::Error::AudioCodec(_) |
|
|
|
|
coreaudio::Error::AudioFormat(_) => Some(DefaultFormatError::StreamTypeNotSupported),
|
|
|
|
_ => Some(DefaultFormatError::DeviceNotAvailable),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-26 12:49:47 +00:00
|
|
|
let property_address = AudioObjectPropertyAddress {
|
2018-02-12 13:10:24 +00:00
|
|
|
mSelector: kAudioDevicePropertyStreamFormat,
|
|
|
|
mScope: scope,
|
2018-01-26 12:49:47 +00:00
|
|
|
mElement: kAudioObjectPropertyElementMaster,
|
|
|
|
};
|
2018-02-12 13:10:24 +00:00
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let asbd: AudioStreamBasicDescription = mem::uninitialized();
|
|
|
|
let data_size = mem::size_of::<AudioStreamBasicDescription>() as u32;
|
2018-01-26 12:49:47 +00:00
|
|
|
let status = AudioObjectGetPropertyData(
|
|
|
|
self.audio_device_id,
|
|
|
|
&property_address as *const _,
|
|
|
|
0,
|
|
|
|
null(),
|
|
|
|
&data_size as *const _ as *mut _,
|
2018-02-12 13:10:24 +00:00
|
|
|
&asbd as *const _ as *mut _,
|
2018-01-26 12:49:47 +00:00
|
|
|
);
|
2018-02-12 13:10:24 +00:00
|
|
|
|
2018-01-26 12:49:47 +00:00
|
|
|
if status != kAudioHardwareNoError as i32 {
|
2018-02-12 13:10:24 +00:00
|
|
|
let err = default_format_error_from_os_status(status)
|
2018-04-01 11:25:46 +00:00
|
|
|
.expect("no known error for OSStatus");
|
2018-02-12 13:10:24 +00:00
|
|
|
return Err(err);
|
2018-01-26 12:49:47 +00:00
|
|
|
}
|
2018-02-12 13:10:24 +00:00
|
|
|
|
|
|
|
let sample_format = {
|
|
|
|
let audio_format = coreaudio::audio_unit::AudioFormat::from_format_and_flag(
|
|
|
|
asbd.mFormatID,
|
|
|
|
Some(asbd.mFormatFlags),
|
|
|
|
);
|
|
|
|
let flags = match audio_format {
|
|
|
|
Some(coreaudio::audio_unit::AudioFormat::LinearPCM(flags)) => flags,
|
|
|
|
_ => return Err(DefaultFormatError::StreamTypeNotSupported),
|
|
|
|
};
|
|
|
|
let maybe_sample_format =
|
|
|
|
coreaudio::audio_unit::SampleFormat::from_flags_and_bytes_per_frame(
|
|
|
|
flags,
|
|
|
|
asbd.mBytesPerFrame,
|
|
|
|
);
|
|
|
|
match maybe_sample_format {
|
|
|
|
Some(coreaudio::audio_unit::SampleFormat::F32) => SampleFormat::F32,
|
|
|
|
Some(coreaudio::audio_unit::SampleFormat::I16) => SampleFormat::I16,
|
|
|
|
_ => return Err(DefaultFormatError::StreamTypeNotSupported),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let format = Format {
|
|
|
|
sample_rate: SampleRate(asbd.mSampleRate as _),
|
|
|
|
channels: asbd.mChannelsPerFrame as _,
|
|
|
|
data_type: sample_format,
|
|
|
|
};
|
|
|
|
Ok(format)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn default_input_format(&self) -> Result<Format, DefaultFormatError> {
|
|
|
|
self.default_format(kAudioObjectPropertyScopeInput)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn default_output_format(&self) -> Result<Format, DefaultFormatError> {
|
|
|
|
self.default_format(kAudioObjectPropertyScopeOutput)
|
2015-09-24 03:02:28 +00:00
|
|
|
}
|
2015-02-28 18:50:29 +00:00
|
|
|
}
|
|
|
|
|
2019-03-22 20:27:17 +00:00
|
|
|
impl fmt::Debug for Device {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.debug_struct("Device")
|
|
|
|
.field("audio_device_id", &self.audio_device_id)
|
|
|
|
.field("name", &self.name())
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-12 13:10:24 +00:00
|
|
|
// The ID of a stream is its index within the `streams` array of the events loop.
|
2017-10-18 18:24:05 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2018-02-12 13:10:24 +00:00
|
|
|
pub struct StreamId(usize);
|
2017-10-18 18:24:05 +00:00
|
|
|
|
|
|
|
pub struct EventLoop {
|
|
|
|
// This `Arc` is shared with all the callbacks of coreaudio.
|
|
|
|
active_callbacks: Arc<ActiveCallbacks>,
|
2018-02-12 13:10:24 +00:00
|
|
|
streams: Mutex<Vec<Option<StreamInner>>>,
|
2016-08-12 07:49:13 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 18:24:05 +00:00
|
|
|
struct ActiveCallbacks {
|
|
|
|
// Whenever the `run()` method is called with a callback, this callback is put in this list.
|
2018-02-12 13:10:24 +00:00
|
|
|
callbacks: Mutex<Vec<&'static mut (FnMut(StreamId, StreamData) + Send)>>,
|
2015-02-28 18:50:29 +00:00
|
|
|
}
|
|
|
|
|
2018-02-12 13:10:24 +00:00
|
|
|
struct StreamInner {
|
2017-10-18 18:24:05 +00:00
|
|
|
playing: bool,
|
|
|
|
audio_unit: AudioUnit,
|
2018-04-01 11:25:46 +00:00
|
|
|
// Track the device with which the audio unit was spawned.
|
|
|
|
//
|
|
|
|
// We must do this so that we can avoid changing the device sample rate if there is already
|
|
|
|
// a stream associated with the device.
|
|
|
|
device_id: AudioDeviceID,
|
2017-10-18 18:24:05 +00:00
|
|
|
}
|
2015-02-28 18:50:29 +00:00
|
|
|
|
2017-11-03 09:50:02 +00:00
|
|
|
// TODO need stronger error identification
|
2019-06-20 19:31:15 +00:00
|
|
|
impl From<coreaudio::Error> for BuildStreamError {
|
|
|
|
fn from(err: coreaudio::Error) -> BuildStreamError {
|
2017-11-03 09:50:02 +00:00
|
|
|
match err {
|
|
|
|
coreaudio::Error::RenderCallbackBufferFormatDoesNotMatchAudioUnitStreamFormat |
|
|
|
|
coreaudio::Error::NoKnownSubtype |
|
|
|
|
coreaudio::Error::AudioUnit(coreaudio::error::AudioUnitError::FormatNotSupported) |
|
|
|
|
coreaudio::Error::AudioCodec(_) |
|
2019-06-20 19:31:15 +00:00
|
|
|
coreaudio::Error::AudioFormat(_) => BuildStreamError::FormatNotSupported,
|
|
|
|
_ => BuildStreamError::DeviceNotAvailable,
|
2017-11-03 09:50:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-12 13:10:24 +00:00
|
|
|
// Create a coreaudio AudioStreamBasicDescription from a CPAL Format.
|
|
|
|
fn asbd_from_format(format: &Format) -> AudioStreamBasicDescription {
|
|
|
|
let n_channels = format.channels as usize;
|
|
|
|
let sample_rate = format.sample_rate.0;
|
|
|
|
let bytes_per_channel = format.data_type.sample_size();
|
|
|
|
let bits_per_channel = bytes_per_channel * 8;
|
|
|
|
let bytes_per_frame = n_channels * bytes_per_channel;
|
|
|
|
let frames_per_packet = 1;
|
|
|
|
let bytes_per_packet = frames_per_packet * bytes_per_frame;
|
|
|
|
let sample_format = format.data_type;
|
|
|
|
let format_flags = match sample_format {
|
|
|
|
SampleFormat::F32 => (kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked) as u32,
|
|
|
|
_ => kAudioFormatFlagIsPacked as u32,
|
|
|
|
};
|
|
|
|
let asbd = AudioStreamBasicDescription {
|
|
|
|
mBitsPerChannel: bits_per_channel as _,
|
|
|
|
mBytesPerFrame: bytes_per_frame as _,
|
|
|
|
mChannelsPerFrame: n_channels as _,
|
|
|
|
mBytesPerPacket: bytes_per_packet as _,
|
|
|
|
mFramesPerPacket: frames_per_packet as _,
|
|
|
|
mFormatFlags: format_flags,
|
|
|
|
mFormatID: kAudioFormatLinearPCM,
|
|
|
|
mSampleRate: sample_rate as _,
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
asbd
|
|
|
|
}
|
|
|
|
|
|
|
|
fn audio_unit_from_device(device: &Device, input: bool) -> Result<AudioUnit, coreaudio::Error> {
|
|
|
|
let mut audio_unit = {
|
|
|
|
let au_type = if cfg!(target_os = "ios") {
|
|
|
|
// The HalOutput unit isn't available in iOS unfortunately.
|
|
|
|
// RemoteIO is a sensible replacement.
|
|
|
|
// See https://goo.gl/CWwRTx
|
|
|
|
coreaudio::audio_unit::IOType::RemoteIO
|
|
|
|
} else {
|
|
|
|
coreaudio::audio_unit::IOType::HalOutput
|
|
|
|
};
|
|
|
|
AudioUnit::new(au_type)?
|
|
|
|
};
|
|
|
|
|
|
|
|
if input {
|
|
|
|
// Enable input processing.
|
|
|
|
let enable_input = 1u32;
|
|
|
|
audio_unit.set_property(
|
|
|
|
kAudioOutputUnitProperty_EnableIO,
|
|
|
|
Scope::Input,
|
|
|
|
Element::Input,
|
|
|
|
Some(&enable_input),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// Disable output processing.
|
|
|
|
let disable_output = 0u32;
|
|
|
|
audio_unit.set_property(
|
|
|
|
kAudioOutputUnitProperty_EnableIO,
|
|
|
|
Scope::Output,
|
|
|
|
Element::Output,
|
|
|
|
Some(&disable_output),
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
audio_unit.set_property(
|
|
|
|
kAudioOutputUnitProperty_CurrentDevice,
|
|
|
|
Scope::Global,
|
|
|
|
Element::Output,
|
|
|
|
Some(&device.audio_device_id),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok(audio_unit)
|
|
|
|
}
|
|
|
|
|
2017-10-18 18:24:05 +00:00
|
|
|
impl EventLoop {
|
2015-09-11 08:55:29 +00:00
|
|
|
#[inline]
|
2017-10-18 18:24:05 +00:00
|
|
|
pub fn new() -> EventLoop {
|
|
|
|
EventLoop {
|
2017-10-23 14:41:38 +00:00
|
|
|
active_callbacks: Arc::new(ActiveCallbacks { callbacks: Mutex::new(Vec::new()) }),
|
2018-02-12 13:10:24 +00:00
|
|
|
streams: Mutex::new(Vec::new()),
|
2017-10-18 18:24:05 +00:00
|
|
|
}
|
2015-09-24 03:02:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-10-18 18:24:05 +00:00
|
|
|
pub fn run<F>(&self, mut callback: F) -> !
|
2018-02-12 13:10:24 +00:00
|
|
|
where F: FnMut(StreamId, StreamData) + Send
|
2017-10-18 18:24:05 +00:00
|
|
|
{
|
2018-02-12 13:10:24 +00:00
|
|
|
{
|
|
|
|
let callback: &mut (FnMut(StreamId, StreamData) + Send) = &mut callback;
|
|
|
|
self.active_callbacks
|
|
|
|
.callbacks
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.push(unsafe { mem::transmute(callback) });
|
|
|
|
}
|
2016-08-12 07:49:13 +00:00
|
|
|
|
2017-10-18 18:24:05 +00:00
|
|
|
loop {
|
|
|
|
// So the loop does not get optimised out in --release
|
|
|
|
thread::sleep(Duration::new(1u64, 0u32));
|
2015-09-24 03:02:28 +00:00
|
|
|
}
|
2016-08-12 07:49:13 +00:00
|
|
|
|
2017-10-18 18:24:05 +00:00
|
|
|
// Note: if we ever change this API so that `run` can return, then it is critical that
|
|
|
|
// we remove the callback from `active_callbacks`.
|
2016-08-12 07:49:13 +00:00
|
|
|
}
|
|
|
|
|
2018-02-12 13:10:24 +00:00
|
|
|
fn next_stream_id(&self) -> usize {
|
|
|
|
let streams_lock = self.streams.lock().unwrap();
|
|
|
|
let stream_id = streams_lock
|
|
|
|
.iter()
|
|
|
|
.position(|n| n.is_none())
|
|
|
|
.unwrap_or(streams_lock.len());
|
|
|
|
stream_id
|
|
|
|
}
|
2017-10-18 18:24:05 +00:00
|
|
|
|
2018-02-12 13:10:24 +00:00
|
|
|
// Add the stream to the list of streams within `self`.
|
2018-04-01 11:25:46 +00:00
|
|
|
fn add_stream(&self, stream_id: usize, au: AudioUnit, device_id: AudioDeviceID) {
|
2018-02-12 13:10:24 +00:00
|
|
|
let inner = StreamInner {
|
|
|
|
playing: true,
|
|
|
|
audio_unit: au,
|
2018-04-01 11:25:46 +00:00
|
|
|
device_id: device_id,
|
2017-10-10 16:24:53 +00:00
|
|
|
};
|
2017-10-18 18:24:05 +00:00
|
|
|
|
2018-02-12 13:10:24 +00:00
|
|
|
let mut streams_lock = self.streams.lock().unwrap();
|
|
|
|
if stream_id == streams_lock.len() {
|
|
|
|
streams_lock.push(Some(inner));
|
|
|
|
} else {
|
|
|
|
streams_lock[stream_id] = Some(inner);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn build_input_stream(
|
|
|
|
&self,
|
|
|
|
device: &Device,
|
|
|
|
format: &Format,
|
2019-06-20 19:31:15 +00:00
|
|
|
) -> Result<StreamId, BuildStreamError>
|
2018-02-12 13:10:24 +00:00
|
|
|
{
|
2018-04-01 11:25:46 +00:00
|
|
|
// The scope and element for working with a device's input stream.
|
2018-02-12 13:10:24 +00:00
|
|
|
let scope = Scope::Output;
|
|
|
|
let element = Element::Input;
|
2017-11-03 09:51:02 +00:00
|
|
|
|
2018-04-01 11:25:46 +00:00
|
|
|
// Check whether or not we need to change the device sample rate to suit the one specified for the stream.
|
|
|
|
unsafe {
|
|
|
|
// Get the current sample rate.
|
|
|
|
let mut property_address = AudioObjectPropertyAddress {
|
|
|
|
mSelector: kAudioDevicePropertyNominalSampleRate,
|
|
|
|
mScope: kAudioObjectPropertyScopeGlobal,
|
|
|
|
mElement: kAudioObjectPropertyElementMaster,
|
|
|
|
};
|
|
|
|
let sample_rate: f64 = 0.0;
|
|
|
|
let data_size = mem::size_of::<f64>() as u32;
|
|
|
|
let status = AudioObjectGetPropertyData(
|
|
|
|
device.audio_device_id,
|
|
|
|
&property_address as *const _,
|
|
|
|
0,
|
|
|
|
null(),
|
|
|
|
&data_size as *const _ as *mut _,
|
|
|
|
&sample_rate as *const _ as *mut _,
|
|
|
|
);
|
|
|
|
coreaudio::Error::from_os_status(status)?;
|
|
|
|
|
|
|
|
// If the requested sample rate is different to the device sample rate, update the device.
|
|
|
|
if sample_rate as u32 != format.sample_rate.0 {
|
|
|
|
|
|
|
|
// In order to avoid breaking existing input streams we `panic!` if there is already an
|
|
|
|
// active input stream for this device with the actual sample rate.
|
|
|
|
for stream in &*self.streams.lock().unwrap() {
|
|
|
|
if let Some(stream) = stream.as_ref() {
|
|
|
|
if stream.device_id == device.audio_device_id {
|
|
|
|
panic!("cannot change device sample rate for stream as an existing stream \
|
|
|
|
is already running at the current sample rate.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get available sample rate ranges.
|
|
|
|
property_address.mSelector = kAudioDevicePropertyAvailableNominalSampleRates;
|
|
|
|
let data_size = 0u32;
|
|
|
|
let status = AudioObjectGetPropertyDataSize(
|
|
|
|
device.audio_device_id,
|
|
|
|
&property_address as *const _,
|
|
|
|
0,
|
|
|
|
null(),
|
|
|
|
&data_size as *const _ as *mut _,
|
|
|
|
);
|
|
|
|
coreaudio::Error::from_os_status(status)?;
|
|
|
|
let n_ranges = data_size as usize / mem::size_of::<AudioValueRange>();
|
|
|
|
let mut ranges: Vec<u8> = vec![];
|
|
|
|
ranges.reserve_exact(data_size as usize);
|
|
|
|
let status = AudioObjectGetPropertyData(
|
|
|
|
device.audio_device_id,
|
|
|
|
&property_address as *const _,
|
|
|
|
0,
|
|
|
|
null(),
|
|
|
|
&data_size as *const _ as *mut _,
|
|
|
|
ranges.as_mut_ptr() as *mut _,
|
|
|
|
);
|
|
|
|
coreaudio::Error::from_os_status(status)?;
|
|
|
|
let ranges: *mut AudioValueRange = ranges.as_mut_ptr() as *mut _;
|
|
|
|
let ranges: &'static [AudioValueRange] = slice::from_raw_parts(ranges, n_ranges);
|
|
|
|
|
|
|
|
// Now that we have the available ranges, pick the one matching the desired rate.
|
|
|
|
let sample_rate = format.sample_rate.0;
|
|
|
|
let maybe_index = ranges
|
|
|
|
.iter()
|
|
|
|
.position(|r| r.mMinimum as u32 == sample_rate && r.mMaximum as u32 == sample_rate);
|
|
|
|
let range_index = match maybe_index {
|
2019-06-20 19:31:15 +00:00
|
|
|
None => return Err(BuildStreamError::FormatNotSupported),
|
2018-04-01 11:25:46 +00:00
|
|
|
Some(i) => i,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Update the property selector to specify the nominal sample rate.
|
|
|
|
property_address.mSelector = kAudioDevicePropertyNominalSampleRate;
|
|
|
|
|
|
|
|
// Setting the sample rate of a device is an asynchronous process in coreaudio.
|
|
|
|
//
|
|
|
|
// Thus we are required to set a `listener` so that we may be notified when the
|
|
|
|
// change occurs.
|
|
|
|
unsafe extern "C" fn rate_listener(
|
|
|
|
device_id: AudioObjectID,
|
|
|
|
_n_addresses: u32,
|
|
|
|
_properties: *const AudioObjectPropertyAddress,
|
|
|
|
rate_ptr: *mut ::std::os::raw::c_void,
|
|
|
|
) -> OSStatus {
|
|
|
|
let rate_ptr: *const f64 = rate_ptr as *const _;
|
|
|
|
let data_size = mem::size_of::<f64>();
|
|
|
|
let property_address = AudioObjectPropertyAddress {
|
|
|
|
mSelector: kAudioDevicePropertyNominalSampleRate,
|
|
|
|
mScope: kAudioObjectPropertyScopeGlobal,
|
|
|
|
mElement: kAudioObjectPropertyElementMaster,
|
|
|
|
};
|
|
|
|
AudioObjectGetPropertyData(
|
|
|
|
device_id,
|
|
|
|
&property_address as *const _,
|
|
|
|
0,
|
|
|
|
null(),
|
|
|
|
&data_size as *const _ as *mut _,
|
|
|
|
rate_ptr as *const _ as *mut _,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add our sample rate change listener callback.
|
|
|
|
let reported_rate: f64 = 0.0;
|
|
|
|
let status = AudioObjectAddPropertyListener(
|
|
|
|
device.audio_device_id,
|
|
|
|
&property_address as *const _,
|
|
|
|
Some(rate_listener),
|
|
|
|
&reported_rate as *const _ as *mut _,
|
|
|
|
);
|
|
|
|
coreaudio::Error::from_os_status(status)?;
|
|
|
|
|
|
|
|
// Finally, set the sample rate.
|
|
|
|
let sample_rate = sample_rate as f64;
|
|
|
|
let status = AudioObjectSetPropertyData(
|
|
|
|
device.audio_device_id,
|
|
|
|
&property_address as *const _,
|
|
|
|
0,
|
|
|
|
null(),
|
|
|
|
data_size,
|
|
|
|
&ranges[range_index] as *const _ as *const _,
|
|
|
|
);
|
|
|
|
coreaudio::Error::from_os_status(status)?;
|
|
|
|
|
|
|
|
// Wait for the reported_rate to change.
|
|
|
|
//
|
|
|
|
// This should not take longer than a few ms, but we timeout after 1 sec just in case.
|
|
|
|
let timer = ::std::time::Instant::now();
|
|
|
|
while sample_rate != reported_rate {
|
|
|
|
if timer.elapsed() > ::std::time::Duration::from_secs(1) {
|
|
|
|
panic!("timeout waiting for sample rate update for device");
|
|
|
|
}
|
|
|
|
::std::thread::sleep(::std::time::Duration::from_millis(5));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the `rate_listener` callback.
|
|
|
|
let status = AudioObjectRemovePropertyListener(
|
|
|
|
device.audio_device_id,
|
|
|
|
&property_address as *const _,
|
|
|
|
Some(rate_listener),
|
|
|
|
&reported_rate as *const _ as *mut _,
|
|
|
|
);
|
|
|
|
coreaudio::Error::from_os_status(status)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut audio_unit = audio_unit_from_device(device, true)?;
|
|
|
|
|
2017-11-03 09:51:02 +00:00
|
|
|
// Set the stream in interleaved mode.
|
2018-02-12 13:10:24 +00:00
|
|
|
let asbd = asbd_from_format(format);
|
|
|
|
audio_unit.set_property(kAudioUnitProperty_StreamFormat, scope, element, Some(&asbd))?;
|
|
|
|
|
|
|
|
// Determine the future ID of the stream.
|
|
|
|
let stream_id = self.next_stream_id();
|
|
|
|
|
|
|
|
// Register the callback that is being called by coreaudio whenever it needs data to be
|
|
|
|
// fed to the audio buffer.
|
|
|
|
let active_callbacks = self.active_callbacks.clone();
|
2018-01-23 05:58:37 +00:00
|
|
|
let sample_format = format.data_type;
|
2018-02-12 13:10:24 +00:00
|
|
|
let bytes_per_channel = format.data_type.sample_size();
|
|
|
|
type Args = render_callback::Args<data::Raw>;
|
|
|
|
audio_unit.set_input_callback(move |args: Args| unsafe {
|
|
|
|
let ptr = (*args.data.data).mBuffers.as_ptr() as *const AudioBuffer;
|
|
|
|
let len = (*args.data.data).mNumberBuffers as usize;
|
|
|
|
let buffers: &[AudioBuffer] = slice::from_raw_parts(ptr, len);
|
2017-11-03 09:51:02 +00:00
|
|
|
|
2018-02-12 13:10:24 +00:00
|
|
|
// TODO: Perhaps loop over all buffers instead?
|
|
|
|
let AudioBuffer {
|
|
|
|
mNumberChannels: _num_channels,
|
|
|
|
mDataByteSize: data_byte_size,
|
|
|
|
mData: data
|
|
|
|
} = buffers[0];
|
|
|
|
|
|
|
|
let mut callbacks = active_callbacks.callbacks.lock().unwrap();
|
|
|
|
|
|
|
|
// A small macro to simplify handling the callback for different sample types.
|
|
|
|
macro_rules! try_callback {
|
|
|
|
($SampleFormat:ident, $SampleType:ty) => {{
|
|
|
|
let data_len = (data_byte_size as usize / bytes_per_channel) as usize;
|
|
|
|
let data_slice = slice::from_raw_parts(data as *const $SampleType, data_len);
|
|
|
|
let callback = match callbacks.get_mut(0) {
|
|
|
|
Some(cb) => cb,
|
|
|
|
None => return Ok(()),
|
|
|
|
};
|
2019-04-30 06:43:47 +00:00
|
|
|
let unknown_type_buffer = UnknownTypeInputBuffer::$SampleFormat(::InputBuffer { buffer: data_slice });
|
2018-02-12 13:10:24 +00:00
|
|
|
let stream_data = StreamData::Input { buffer: unknown_type_buffer };
|
|
|
|
callback(StreamId(stream_id), stream_data);
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
match sample_format {
|
|
|
|
SampleFormat::F32 => try_callback!(F32, f32),
|
|
|
|
SampleFormat::I16 => try_callback!(I16, i16),
|
|
|
|
SampleFormat::U16 => try_callback!(U16, u16),
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})?;
|
|
|
|
|
|
|
|
// TODO: start playing now? is that consistent with the other backends?
|
|
|
|
audio_unit.start()?;
|
|
|
|
|
|
|
|
// Add the stream to the list of streams within `self`.
|
2018-04-01 11:25:46 +00:00
|
|
|
self.add_stream(stream_id, audio_unit, device.audio_device_id);
|
2018-02-12 13:10:24 +00:00
|
|
|
|
|
|
|
Ok(StreamId(stream_id))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn build_output_stream(
|
|
|
|
&self,
|
|
|
|
device: &Device,
|
|
|
|
format: &Format,
|
2019-06-20 19:31:15 +00:00
|
|
|
) -> Result<StreamId, BuildStreamError>
|
2018-02-12 13:10:24 +00:00
|
|
|
{
|
|
|
|
let mut audio_unit = audio_unit_from_device(device, false)?;
|
|
|
|
|
|
|
|
// The scope and element for working with a device's output stream.
|
|
|
|
let scope = Scope::Input;
|
|
|
|
let element = Element::Output;
|
|
|
|
|
|
|
|
// Set the stream in interleaved mode.
|
|
|
|
let asbd = asbd_from_format(format);
|
|
|
|
audio_unit.set_property(kAudioUnitProperty_StreamFormat, scope, element, Some(&asbd))?;
|
|
|
|
|
|
|
|
// Determine the future ID of the stream.
|
|
|
|
let stream_id = self.next_stream_id();
|
2015-09-24 21:24:12 +00:00
|
|
|
|
2017-10-18 18:24:05 +00:00
|
|
|
// Register the callback that is being called by coreaudio whenever it needs data to be
|
|
|
|
// fed to the audio buffer.
|
|
|
|
let active_callbacks = self.active_callbacks.clone();
|
2018-02-12 13:10:24 +00:00
|
|
|
let sample_format = format.data_type;
|
|
|
|
let bytes_per_channel = format.data_type.sample_size();
|
|
|
|
type Args = render_callback::Args<data::Raw>;
|
|
|
|
audio_unit.set_render_callback(move |args: Args| unsafe {
|
2017-10-18 18:24:05 +00:00
|
|
|
// If `run()` is currently running, then a callback will be available from this list.
|
|
|
|
// Otherwise, we just fill the buffer with zeroes and return.
|
2017-11-03 09:51:02 +00:00
|
|
|
|
|
|
|
let AudioBuffer {
|
|
|
|
mNumberChannels: _num_channels,
|
|
|
|
mDataByteSize: data_byte_size,
|
|
|
|
mData: data
|
|
|
|
} = (*args.data.data).mBuffers[0];
|
2018-01-23 05:58:37 +00:00
|
|
|
|
2017-10-18 18:24:05 +00:00
|
|
|
let mut callbacks = active_callbacks.callbacks.lock().unwrap();
|
2017-11-03 09:51:02 +00:00
|
|
|
|
2018-01-23 05:58:37 +00:00
|
|
|
// A small macro to simplify handling the callback for different sample types.
|
|
|
|
macro_rules! try_callback {
|
|
|
|
($SampleFormat:ident, $SampleType:ty, $equilibrium:expr) => {{
|
2018-01-24 10:34:18 +00:00
|
|
|
let data_len = (data_byte_size as usize / bytes_per_channel) as usize;
|
2018-01-23 05:58:37 +00:00
|
|
|
let data_slice = slice::from_raw_parts_mut(data as *mut $SampleType, data_len);
|
|
|
|
let callback = match callbacks.get_mut(0) {
|
|
|
|
Some(cb) => cb,
|
|
|
|
None => {
|
|
|
|
for sample in data_slice.iter_mut() {
|
|
|
|
*sample = $equilibrium;
|
|
|
|
}
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
};
|
2019-04-30 06:43:47 +00:00
|
|
|
let unknown_type_buffer = UnknownTypeOutputBuffer::$SampleFormat(::OutputBuffer { buffer: data_slice });
|
2018-02-12 13:10:24 +00:00
|
|
|
let stream_data = StreamData::Output { buffer: unknown_type_buffer };
|
|
|
|
callback(StreamId(stream_id), stream_data);
|
2018-01-23 05:58:37 +00:00
|
|
|
}};
|
|
|
|
}
|
2017-10-18 18:24:05 +00:00
|
|
|
|
2018-01-23 05:58:37 +00:00
|
|
|
match sample_format {
|
|
|
|
SampleFormat::F32 => try_callback!(F32, f32, 0.0),
|
|
|
|
SampleFormat::I16 => try_callback!(I16, i16, 0),
|
2018-01-24 10:34:18 +00:00
|
|
|
SampleFormat::U16 => try_callback!(U16, u16, ::std::u16::MAX / 2),
|
2018-01-23 05:58:37 +00:00
|
|
|
}
|
2016-08-12 07:49:13 +00:00
|
|
|
|
2017-10-18 18:24:05 +00:00
|
|
|
Ok(())
|
2017-11-03 09:50:02 +00:00
|
|
|
})?;
|
2015-09-24 03:02:28 +00:00
|
|
|
|
2017-10-18 18:24:05 +00:00
|
|
|
// TODO: start playing now? is that consistent with the other backends?
|
2017-11-03 09:50:02 +00:00
|
|
|
audio_unit.start()?;
|
2017-10-18 18:24:05 +00:00
|
|
|
|
2018-02-12 13:10:24 +00:00
|
|
|
// Add the stream to the list of streams within `self`.
|
2018-04-01 11:25:46 +00:00
|
|
|
self.add_stream(stream_id, audio_unit, device.audio_device_id);
|
2016-01-12 16:06:14 +00:00
|
|
|
|
2018-02-12 13:10:24 +00:00
|
|
|
Ok(StreamId(stream_id))
|
2017-10-18 18:24:05 +00:00
|
|
|
}
|
2015-02-28 18:50:29 +00:00
|
|
|
|
2018-02-12 13:10:24 +00:00
|
|
|
pub fn destroy_stream(&self, stream_id: StreamId) {
|
|
|
|
let mut streams = self.streams.lock().unwrap();
|
|
|
|
streams[stream_id.0] = None;
|
2017-10-18 18:24:05 +00:00
|
|
|
}
|
2017-04-19 10:44:42 +00:00
|
|
|
|
2018-02-12 13:10:24 +00:00
|
|
|
pub fn play_stream(&self, stream: StreamId) {
|
|
|
|
let mut streams = self.streams.lock().unwrap();
|
|
|
|
let stream = streams[stream.0].as_mut().unwrap();
|
2016-08-12 07:49:13 +00:00
|
|
|
|
2018-02-12 13:10:24 +00:00
|
|
|
if !stream.playing {
|
|
|
|
stream.audio_unit.start().unwrap();
|
|
|
|
stream.playing = true;
|
2017-10-18 18:24:05 +00:00
|
|
|
}
|
2015-02-28 18:50:29 +00:00
|
|
|
}
|
|
|
|
|
2018-02-12 13:10:24 +00:00
|
|
|
pub fn pause_stream(&self, stream: StreamId) {
|
|
|
|
let mut streams = self.streams.lock().unwrap();
|
|
|
|
let stream = streams[stream.0].as_mut().unwrap();
|
2017-10-18 18:24:05 +00:00
|
|
|
|
2018-02-12 13:10:24 +00:00
|
|
|
if stream.playing {
|
|
|
|
stream.audio_unit.stop().unwrap();
|
|
|
|
stream.playing = false;
|
2016-10-18 06:20:40 +00:00
|
|
|
}
|
2015-02-28 18:50:29 +00:00
|
|
|
}
|
2017-10-18 18:24:05 +00:00
|
|
|
}
|
2019-06-20 22:22:30 +00:00
|
|
|
|
|
|
|
fn check_os_status(os_status: OSStatus) -> Result<(), BackendSpecificError> {
|
|
|
|
match coreaudio::Error::from_os_status(os_status) {
|
|
|
|
Ok(()) => Ok(()),
|
|
|
|
Err(err) => {
|
|
|
|
let description = std::error::Error::description(&err).to_string();
|
|
|
|
Err(BackendSpecificError { description })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|