Run rustfmt on the code (#162)
This commit is contained in:
parent
cdcef96279
commit
2028d5907f
13 changed files with 622 additions and 399 deletions
|
|
@ -2,23 +2,23 @@ extern crate winapi;
|
|||
extern crate ole32;
|
||||
extern crate kernel32;
|
||||
|
||||
use std::io::Error as IoError;
|
||||
use std::os::windows::ffi::OsStringExt;
|
||||
use std::ffi::OsString;
|
||||
use std::sync::{Arc, Mutex, MutexGuard};
|
||||
use std::ptr;
|
||||
use std::io::Error as IoError;
|
||||
use std::mem;
|
||||
use std::os::windows::ffi::OsStringExt;
|
||||
use std::ptr;
|
||||
use std::slice;
|
||||
use std::sync::{Arc, Mutex, MutexGuard};
|
||||
|
||||
use ChannelPosition;
|
||||
use Format;
|
||||
use FormatsEnumerationError;
|
||||
use ChannelPosition;
|
||||
use SamplesRate;
|
||||
use SampleFormat;
|
||||
use SamplesRate;
|
||||
|
||||
pub use std::option::IntoIter as OptionIntoIter;
|
||||
pub use self::enumerate::{EndpointsIterator, get_default_endpoint};
|
||||
pub use self::voice::{Voice, Buffer, EventLoop, SamplesStream};
|
||||
pub use self::voice::{Buffer, EventLoop, SamplesStream, Voice};
|
||||
pub use std::option::IntoIter as OptionIntoIter;
|
||||
|
||||
pub type SupportedFormatsIterator = OptionIntoIter<Format>;
|
||||
|
||||
|
|
@ -38,8 +38,10 @@ fn check_result(result: winapi::HRESULT) -> Result<(), IoError> {
|
|||
/// Wrapper because of that stupid decision to remove `Send` and `Sync` from raw pointers.
|
||||
#[derive(Copy, Clone)]
|
||||
struct IAudioClientWrapper(*mut winapi::IAudioClient);
|
||||
unsafe impl Send for IAudioClientWrapper {}
|
||||
unsafe impl Sync for IAudioClientWrapper {}
|
||||
unsafe impl Send for IAudioClientWrapper {
|
||||
}
|
||||
unsafe impl Sync for IAudioClientWrapper {
|
||||
}
|
||||
|
||||
/// An opaque type that identifies an end point.
|
||||
pub struct Endpoint {
|
||||
|
|
@ -47,11 +49,13 @@ pub struct Endpoint {
|
|||
|
||||
/// We cache an uninitialized `IAudioClient` so that we can call functions from it without
|
||||
/// having to create/destroy audio clients all the time.
|
||||
future_audio_client: Arc<Mutex<Option<IAudioClientWrapper>>>, // TODO: add NonZero around the ptr
|
||||
future_audio_client: Arc<Mutex<Option<IAudioClientWrapper>>>, // TODO: add NonZero around the ptr
|
||||
}
|
||||
|
||||
unsafe impl Send for Endpoint {}
|
||||
unsafe impl Sync for Endpoint {}
|
||||
unsafe impl Send for Endpoint {
|
||||
}
|
||||
unsafe impl Sync for Endpoint {
|
||||
}
|
||||
|
||||
impl Endpoint {
|
||||
// TODO: this function returns a GUID of the endpoin
|
||||
|
|
@ -87,7 +91,8 @@ impl Endpoint {
|
|||
}
|
||||
|
||||
/// Ensures that `future_audio_client` contains a `Some` and returns a locked mutex to it.
|
||||
fn ensure_future_audio_client(&self) -> Result<MutexGuard<Option<IAudioClientWrapper>>, IoError> {
|
||||
fn ensure_future_audio_client(&self)
|
||||
-> Result<MutexGuard<Option<IAudioClientWrapper>>, IoError> {
|
||||
let mut lock = self.future_audio_client.lock().unwrap();
|
||||
if lock.is_some() {
|
||||
return Ok(lock);
|
||||
|
|
@ -95,12 +100,14 @@ impl Endpoint {
|
|||
|
||||
let audio_client: *mut winapi::IAudioClient = unsafe {
|
||||
let mut audio_client = mem::uninitialized();
|
||||
let hresult = (*self.device).Activate(&winapi::IID_IAudioClient, winapi::CLSCTX_ALL,
|
||||
ptr::null_mut(), &mut audio_client);
|
||||
let hresult = (*self.device).Activate(&winapi::IID_IAudioClient,
|
||||
winapi::CLSCTX_ALL,
|
||||
ptr::null_mut(),
|
||||
&mut audio_client);
|
||||
|
||||
// can fail if the device has been disconnected since we enumerated it, or if
|
||||
// the device doesn't support playback for some reason
|
||||
try!(check_result(hresult));
|
||||
check_result(hresult)?;
|
||||
assert!(!audio_client.is_null());
|
||||
audio_client as *mut _
|
||||
};
|
||||
|
|
@ -112,15 +119,15 @@ impl Endpoint {
|
|||
/// Returns an uninitialized `IAudioClient`.
|
||||
#[inline]
|
||||
fn build_audioclient(&self) -> Result<*mut winapi::IAudioClient, IoError> {
|
||||
let mut lock = try!(self.ensure_future_audio_client());
|
||||
let mut lock = self.ensure_future_audio_client()?;
|
||||
let client = lock.unwrap().0;
|
||||
*lock = None;
|
||||
Ok(client)
|
||||
}
|
||||
|
||||
pub fn get_supported_formats_list(&self)
|
||||
-> Result<SupportedFormatsIterator, FormatsEnumerationError>
|
||||
{
|
||||
pub fn get_supported_formats_list(
|
||||
&self)
|
||||
-> Result<SupportedFormatsIterator, FormatsEnumerationError> {
|
||||
// We always create voices in shared mode, therefore all samples go through an audio
|
||||
// processor to mix them together.
|
||||
// However there is no way to query the list of all formats that are supported by the
|
||||
|
|
@ -150,10 +157,8 @@ impl Endpoint {
|
|||
let format = {
|
||||
let (channels, data_type) = match (*format_ptr).wFormatTag {
|
||||
winapi::WAVE_FORMAT_PCM => {
|
||||
(
|
||||
vec![ChannelPosition::FrontLeft, ChannelPosition::FrontRight],
|
||||
SampleFormat::I16
|
||||
)
|
||||
(vec![ChannelPosition::FrontLeft, ChannelPosition::FrontRight],
|
||||
SampleFormat::I16)
|
||||
},
|
||||
winapi::WAVE_FORMAT_EXTENSIBLE => {
|
||||
let format_ptr = format_ptr as *const winapi::WAVEFORMATEXTENSIBLE;
|
||||
|
|
@ -162,24 +167,60 @@ impl Endpoint {
|
|||
let mut channels = Vec::new();
|
||||
|
||||
let mask = (*format_ptr).dwChannelMask;
|
||||
if (mask & winapi::SPEAKER_FRONT_LEFT) != 0 { channels.push(ChannelPosition::FrontLeft); }
|
||||
if (mask & winapi::SPEAKER_FRONT_RIGHT) != 0 { channels.push(ChannelPosition::FrontRight); }
|
||||
if (mask & winapi::SPEAKER_FRONT_CENTER) != 0 { channels.push(ChannelPosition::FrontCenter); }
|
||||
if (mask & winapi::SPEAKER_LOW_FREQUENCY) != 0 { channels.push(ChannelPosition::LowFrequency); }
|
||||
if (mask & winapi::SPEAKER_BACK_LEFT) != 0 { channels.push(ChannelPosition::BackLeft); }
|
||||
if (mask & winapi::SPEAKER_BACK_RIGHT) != 0 { channels.push(ChannelPosition::BackRight); }
|
||||
if (mask & winapi::SPEAKER_FRONT_LEFT_OF_CENTER) != 0 { channels.push(ChannelPosition::FrontLeftOfCenter); }
|
||||
if (mask & winapi::SPEAKER_FRONT_RIGHT_OF_CENTER) != 0 { channels.push(ChannelPosition::FrontRightOfCenter); }
|
||||
if (mask & winapi::SPEAKER_BACK_CENTER) != 0 { channels.push(ChannelPosition::BackCenter); }
|
||||
if (mask & winapi::SPEAKER_SIDE_LEFT) != 0 { channels.push(ChannelPosition::SideLeft); }
|
||||
if (mask & winapi::SPEAKER_SIDE_RIGHT) != 0 { channels.push(ChannelPosition::SideRight); }
|
||||
if (mask & winapi::SPEAKER_TOP_CENTER) != 0 { channels.push(ChannelPosition::TopCenter); }
|
||||
if (mask & winapi::SPEAKER_TOP_FRONT_LEFT) != 0 { channels.push(ChannelPosition::TopFrontLeft); }
|
||||
if (mask & winapi::SPEAKER_TOP_FRONT_CENTER) != 0 { channels.push(ChannelPosition::TopFrontCenter); }
|
||||
if (mask & winapi::SPEAKER_TOP_FRONT_RIGHT) != 0 { channels.push(ChannelPosition::TopFrontRight); }
|
||||
if (mask & winapi::SPEAKER_TOP_BACK_LEFT) != 0 { channels.push(ChannelPosition::TopBackLeft); }
|
||||
if (mask & winapi::SPEAKER_TOP_BACK_CENTER) != 0 { channels.push(ChannelPosition::TopBackCenter); }
|
||||
if (mask & winapi::SPEAKER_TOP_BACK_RIGHT) != 0 { channels.push(ChannelPosition::TopBackRight); }
|
||||
if (mask & winapi::SPEAKER_FRONT_LEFT) != 0 {
|
||||
channels.push(ChannelPosition::FrontLeft);
|
||||
}
|
||||
if (mask & winapi::SPEAKER_FRONT_RIGHT) != 0 {
|
||||
channels.push(ChannelPosition::FrontRight);
|
||||
}
|
||||
if (mask & winapi::SPEAKER_FRONT_CENTER) != 0 {
|
||||
channels.push(ChannelPosition::FrontCenter);
|
||||
}
|
||||
if (mask & winapi::SPEAKER_LOW_FREQUENCY) != 0 {
|
||||
channels.push(ChannelPosition::LowFrequency);
|
||||
}
|
||||
if (mask & winapi::SPEAKER_BACK_LEFT) != 0 {
|
||||
channels.push(ChannelPosition::BackLeft);
|
||||
}
|
||||
if (mask & winapi::SPEAKER_BACK_RIGHT) != 0 {
|
||||
channels.push(ChannelPosition::BackRight);
|
||||
}
|
||||
if (mask & winapi::SPEAKER_FRONT_LEFT_OF_CENTER) != 0 {
|
||||
channels.push(ChannelPosition::FrontLeftOfCenter);
|
||||
}
|
||||
if (mask & winapi::SPEAKER_FRONT_RIGHT_OF_CENTER) != 0 {
|
||||
channels.push(ChannelPosition::FrontRightOfCenter);
|
||||
}
|
||||
if (mask & winapi::SPEAKER_BACK_CENTER) != 0 {
|
||||
channels.push(ChannelPosition::BackCenter);
|
||||
}
|
||||
if (mask & winapi::SPEAKER_SIDE_LEFT) != 0 {
|
||||
channels.push(ChannelPosition::SideLeft);
|
||||
}
|
||||
if (mask & winapi::SPEAKER_SIDE_RIGHT) != 0 {
|
||||
channels.push(ChannelPosition::SideRight);
|
||||
}
|
||||
if (mask & winapi::SPEAKER_TOP_CENTER) != 0 {
|
||||
channels.push(ChannelPosition::TopCenter);
|
||||
}
|
||||
if (mask & winapi::SPEAKER_TOP_FRONT_LEFT) != 0 {
|
||||
channels.push(ChannelPosition::TopFrontLeft);
|
||||
}
|
||||
if (mask & winapi::SPEAKER_TOP_FRONT_CENTER) != 0 {
|
||||
channels.push(ChannelPosition::TopFrontCenter);
|
||||
}
|
||||
if (mask & winapi::SPEAKER_TOP_FRONT_RIGHT) != 0 {
|
||||
channels.push(ChannelPosition::TopFrontRight);
|
||||
}
|
||||
if (mask & winapi::SPEAKER_TOP_BACK_LEFT) != 0 {
|
||||
channels.push(ChannelPosition::TopBackLeft);
|
||||
}
|
||||
if (mask & winapi::SPEAKER_TOP_BACK_CENTER) != 0 {
|
||||
channels.push(ChannelPosition::TopBackCenter);
|
||||
}
|
||||
if (mask & winapi::SPEAKER_TOP_BACK_RIGHT) != 0 {
|
||||
channels.push(ChannelPosition::TopBackRight);
|
||||
}
|
||||
|
||||
assert_eq!((*format_ptr).Format.nChannels as usize, channels.len());
|
||||
channels
|
||||
|
|
@ -187,8 +228,8 @@ impl Endpoint {
|
|||
|
||||
let format = {
|
||||
fn cmp_guid(a: &winapi::GUID, b: &winapi::GUID) -> bool {
|
||||
a.Data1 == b.Data1 && a.Data2 == b.Data2 &&
|
||||
a.Data3 == b.Data3 && a.Data4 == b.Data4
|
||||
a.Data1 == b.Data1 && a.Data2 == b.Data2 && a.Data3 == b.Data3 &&
|
||||
a.Data4 == b.Data4
|
||||
}
|
||||
if cmp_guid(&(*format_ptr).SubFormat,
|
||||
&winapi::KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)
|
||||
|
|
@ -207,7 +248,7 @@ impl Endpoint {
|
|||
(channels, format)
|
||||
},
|
||||
|
||||
f => panic!("Unknown data format returned by GetMixFormat: {:?}", f)
|
||||
f => panic!("Unknown data format returned by GetMixFormat: {:?}", f),
|
||||
};
|
||||
|
||||
Format {
|
||||
|
|
@ -231,12 +272,15 @@ impl PartialEq for Endpoint {
|
|||
}
|
||||
}
|
||||
|
||||
impl Eq for Endpoint {}
|
||||
impl Eq for Endpoint {
|
||||
}
|
||||
|
||||
impl Clone for Endpoint {
|
||||
#[inline]
|
||||
fn clone(&self) -> Endpoint {
|
||||
unsafe { (*self.device).AddRef(); }
|
||||
unsafe {
|
||||
(*self.device).AddRef();
|
||||
}
|
||||
|
||||
Endpoint {
|
||||
device: self.device,
|
||||
|
|
@ -248,10 +292,14 @@ impl Clone for Endpoint {
|
|||
impl Drop for Endpoint {
|
||||
#[inline]
|
||||
fn drop(&mut self) {
|
||||
unsafe { (*self.device).Release(); }
|
||||
unsafe {
|
||||
(*self.device).Release();
|
||||
}
|
||||
|
||||
if let Some(client) = self.future_audio_client.lock().unwrap().take() {
|
||||
unsafe { (*client.0).Release(); }
|
||||
unsafe {
|
||||
(*client.0).Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue