[WIP] Introduce a Host API
This is an implementation of the API described at #204. Please see that issue for more details on the motivation. ----- A **Host** provides access to the available audio devices on the system. Some platforms have more than one host available, e.g. wasapi/asio/dsound on windows, alsa/pulse/jack on linux and so on. As a result, some audio devices are only available on certain hosts, while others are only available on other hosts. Every platform supported by CPAL has at least one **DefaultHost** that is guaranteed to be available (alsa, wasapi and coreaudio). Currently, the default hosts are the only hosts supported by CPAL, however this will change as of landing #221 (cc @freesig). These changes should also accommodate support for other hosts such as jack #250 (cc @derekdreery) and pulseaudio (cc @knappador) #259. This introduces a suite of traits allowing for both compile time and runtime dispatch of different hosts and their uniquely associated device and event loop types. A new private **host** module has been added containing the individual host implementations, each in their own submodule gated to the platforms on which they are available. A new **platform** module has been added containing platform-specific items, including a dynamically dispatched host type that allows for easily switching between hosts at runtime. The **ALL_HOSTS** slice contains a **HostId** for each host supported on the current platform. The **available_hosts** function produces a **HostId** for each host that is currently *available* on the platform. The **host_from_id** function allows for initialising a host from its associated ID, failing with a **HostUnavailable** error. The **default_host** function returns the default host and should never fail. Please see the examples for a demonstration of the change in usage. For the most part, things look the same at the surface level, however the role of device enumeration and creating the event loop have been moved from global functions to host methods. The enumerate.rs example has been updated to enumerate all devices for each host, not just the default. **TODO** - [x] Add the new **Host** API - [x] Update examples for the new API. - [x] ALSA host - [ ] WASAPI host - [ ] CoreAudio host - [ ] Emscripten host **Follow-up PR** - [ ] ASIO host #221 cc @ishitatsuyuki more to review for you if you're interested, but it might be easier after #288 lands and this gets rebased.
This commit is contained in:
parent
2b9e2e0b2c
commit
e8a05379c2
17 changed files with 989 additions and 447 deletions
149
src/host/alsa/enumerate.rs
Normal file
149
src/host/alsa/enumerate.rs
Normal file
|
|
@ -0,0 +1,149 @@
|
|||
use {BackendSpecificError, DevicesError};
|
||||
use super::Device;
|
||||
use super::alsa;
|
||||
use super::check_errors;
|
||||
use std::ffi::CString;
|
||||
use std::mem;
|
||||
|
||||
/// ALSA implementation for `Devices`.
|
||||
pub struct Devices {
|
||||
// we keep the original list so that we can pass it to the free function
|
||||
global_list: *const *const u8,
|
||||
|
||||
// pointer to the next string ; contained within `global_list`
|
||||
next_str: *const *const u8,
|
||||
}
|
||||
|
||||
impl Devices {
|
||||
pub fn new() -> Result<Self, DevicesError> {
|
||||
unsafe {
|
||||
// TODO: check in which situation this can fail.
|
||||
let card = -1; // -1 means all cards.
|
||||
let iface = b"pcm\0"; // Interface identification.
|
||||
let mut hints = mem::uninitialized(); // Array of device name hints.
|
||||
let res = alsa::snd_device_name_hint(card, iface.as_ptr() as *const _, &mut hints);
|
||||
if let Err(description) = check_errors(res) {
|
||||
let err = BackendSpecificError { description };
|
||||
return Err(err.into());
|
||||
}
|
||||
let hints = hints as *const *const u8;
|
||||
let devices = Devices {
|
||||
global_list: hints,
|
||||
next_str: hints,
|
||||
};
|
||||
Ok(devices)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl Send for Devices {
|
||||
}
|
||||
unsafe impl Sync for Devices {
|
||||
}
|
||||
|
||||
impl Drop for Devices {
|
||||
#[inline]
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
alsa::snd_device_name_free_hint(self.global_list as *mut _);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for Devices {
|
||||
type Item = Device;
|
||||
|
||||
fn next(&mut self) -> Option<Device> {
|
||||
loop {
|
||||
unsafe {
|
||||
if (*self.next_str).is_null() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let name = {
|
||||
let n_ptr = alsa::snd_device_name_get_hint(*self.next_str as *const _,
|
||||
b"NAME\0".as_ptr() as *const _);
|
||||
if !n_ptr.is_null() {
|
||||
let bytes = CString::from_raw(n_ptr).into_bytes();
|
||||
let string = String::from_utf8(bytes).unwrap();
|
||||
Some(string)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
let io = {
|
||||
let n_ptr = alsa::snd_device_name_get_hint(*self.next_str as *const _,
|
||||
b"IOID\0".as_ptr() as *const _);
|
||||
if !n_ptr.is_null() {
|
||||
let bytes = CString::from_raw(n_ptr).into_bytes();
|
||||
let string = String::from_utf8(bytes).unwrap();
|
||||
Some(string)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
self.next_str = self.next_str.offset(1);
|
||||
|
||||
if let Some(io) = io {
|
||||
if io != "Output" {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
let name = match name {
|
||||
Some(name) => {
|
||||
// Ignoring the `null` device.
|
||||
if name == "null" {
|
||||
continue;
|
||||
}
|
||||
name
|
||||
},
|
||||
_ => continue,
|
||||
};
|
||||
|
||||
// trying to open the PCM device to see if it can be opened
|
||||
let name_zeroed = CString::new(&name[..]).unwrap();
|
||||
|
||||
// See if the device has an available output stream.
|
||||
let mut playback_handle = mem::uninitialized();
|
||||
let has_available_output = alsa::snd_pcm_open(
|
||||
&mut playback_handle,
|
||||
name_zeroed.as_ptr() as *const _,
|
||||
alsa::SND_PCM_STREAM_PLAYBACK,
|
||||
alsa::SND_PCM_NONBLOCK,
|
||||
) == 0;
|
||||
if has_available_output {
|
||||
alsa::snd_pcm_close(playback_handle);
|
||||
}
|
||||
|
||||
// See if the device has an available input stream.
|
||||
let mut capture_handle = mem::uninitialized();
|
||||
let has_available_input = alsa::snd_pcm_open(
|
||||
&mut capture_handle,
|
||||
name_zeroed.as_ptr() as *const _,
|
||||
alsa::SND_PCM_STREAM_CAPTURE,
|
||||
alsa::SND_PCM_NONBLOCK,
|
||||
) == 0;
|
||||
if has_available_input {
|
||||
alsa::snd_pcm_close(capture_handle);
|
||||
}
|
||||
|
||||
if has_available_output || has_available_input {
|
||||
return Some(Device(name));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn default_input_device() -> Option<Device> {
|
||||
Some(Device("default".to_owned()))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn default_output_device() -> Option<Device> {
|
||||
Some(Device("default".to_owned()))
|
||||
}
|
||||
1240
src/host/alsa/mod.rs
Normal file
1240
src/host/alsa/mod.rs
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue