Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `InputStreamTimestamp`/`OutputStreamTimestamp` merged into `StreamTimestamp`; `capture`/`playback` renamed `device`.
- Renamed the `wasm-beep` and `audioworklet-beep` examples to `webaudio` and `audioworklet`.
- **ALSA**: Update `alsa` dependency to 0.12.
- **CoreAudio**: `DeviceDescription::interface_type()` now reports the device transport (built-in, USB, Bluetooth, Thunderbolt, HDMI, and others) instead of only marking aggregate devices.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be a bit shorter by removing the examples in the parentheses.

- **Linux**: `realtime` can now promote threads without requiring `realtime-dbus`.

### Deprecated
Expand Down
71 changes: 66 additions & 5 deletions src/host/coreaudio/macos/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ use objc2_core_audio::{
kAudioDevicePropertyBufferFrameSizeRange, kAudioDevicePropertyDeviceUID,
kAudioDevicePropertyLatency, kAudioDevicePropertyNominalSampleRate,
kAudioDevicePropertySafetyOffset, kAudioDevicePropertyStreamConfiguration,
kAudioDevicePropertyStreamFormat, kAudioObjectPropertyClass, kAudioObjectPropertyElementMain,
kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyScopeInput,
kAudioObjectPropertyScopeOutput,
kAudioDevicePropertyStreamFormat, kAudioDevicePropertyTransportType, kAudioObjectPropertyClass,
kAudioObjectPropertyElementMain, kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyScopeInput, kAudioObjectPropertyScopeOutput,
};
use objc2_core_audio_types::{
AudioBuffer, AudioBufferList, AudioStreamBasicDescription, AudioValueRange,
Expand Down Expand Up @@ -405,6 +405,64 @@ impl Device {
status == 0 && class_id == kAudioAggregateDeviceClassID
}

/// `None` when the property is unavailable or names a transport with no
/// `InterfaceType` counterpart, so the field is left unset rather than wrong.
fn transport_interface_type(&self) -> Option<InterfaceType> {
let property_address = AudioObjectPropertyAddress {
mSelector: kAudioDevicePropertyTransportType,
mScope: kAudioObjectPropertyScopeGlobal,
mElement: kAudioObjectPropertyElementMain,
};

let mut transport: u32 = 0;
let data_size = size_of::<u32>() as u32;

// SAFETY: AudioObjectGetPropertyData writes a UInt32 for
// kAudioDevicePropertyTransportType. The status is checked before use.
let status = unsafe {
AudioObjectGetPropertyData(
self.audio_device_id,
NonNull::from(&property_address),
0,
null(),
NonNull::from(&data_size),
Comment on lines +418 to +428
NonNull::from(&mut transport).cast(),
)
};
if status != 0 {
return None;
}

// Four-character codes from AudioHardwareBase.h.
const BUILT_IN: u32 = u32::from_be_bytes(*b"bltn");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following are already exported by objc2_core_audio:

pub const kAudioDeviceTransportTypeUnknown: u32 = 0;
pub const kAudioDeviceTransportTypeBuiltIn: u32 = 0x626c746e;
pub const kAudioDeviceTransportTypeAggregate: u32 = 0x67727570;
pub const kAudioDeviceTransportTypeVirtual: u32 = 0x76697274;
pub const kAudioDeviceTransportTypePCI: u32 = 0x70636920;
pub const kAudioDeviceTransportTypeUSB: u32 = 0x75736220;
pub const kAudioDeviceTransportTypeFireWire: u32 = 0x31333934;
pub const kAudioDeviceTransportTypeBluetooth: u32 = 0x626c7565;
pub const kAudioDeviceTransportTypeBluetoothLE: u32 = 0x626c6561;
pub const kAudioDeviceTransportTypeHDMI: u32 = 0x68646d69;
pub const kAudioDeviceTransportTypeDisplayPort: u32 = 0x64707274;
pub const kAudioDeviceTransportTypeAirPlay: u32 = 0x61697270;
pub const kAudioDeviceTransportTypeAVB: u32 = 0x65617662;
pub const kAudioDeviceTransportTypeThunderbolt: u32 = 0x7468756e;
pub const kAudioDeviceTransportTypeContinuityCaptureWired: u32 = 0x63637764;
pub const kAudioDeviceTransportTypeContinuityCaptureWireless: u32 = 0x6363776c;
pub const kAudioDeviceTransportTypeContinuityCapture: u32 = 0x63636170;

const USB: u32 = u32::from_be_bytes(*b"usb ");
const BLUETOOTH: u32 = u32::from_be_bytes(*b"blue");
const BLUETOOTH_LE: u32 = u32::from_be_bytes(*b"blea");
const VIRTUAL: u32 = u32::from_be_bytes(*b"virt");
const AGGREGATE: u32 = u32::from_be_bytes(*b"grup");
const THUNDERBOLT: u32 = u32::from_be_bytes(*b"thun");
const HDMI: u32 = u32::from_be_bytes(*b"hdmi");
const DISPLAY_PORT: u32 = u32::from_be_bytes(*b"dprt");
const FIREWIRE: u32 = u32::from_be_bytes(*b"1394");
const PCI: u32 = u32::from_be_bytes(*b"pci ");
const AIRPLAY: u32 = u32::from_be_bytes(*b"airp");

match transport {
BUILT_IN => Some(InterfaceType::BuiltIn),
USB => Some(InterfaceType::Usb),
BLUETOOTH | BLUETOOTH_LE => Some(InterfaceType::Bluetooth),
VIRTUAL => Some(InterfaceType::Virtual),
AGGREGATE => Some(InterfaceType::Aggregate),
THUNDERBOLT => Some(InterfaceType::Thunderbolt),
HDMI => Some(InterfaceType::Hdmi),
DISPLAY_PORT => Some(InterfaceType::DisplayPort),
FIREWIRE => Some(InterfaceType::FireWire),
PCI => Some(InterfaceType::Pci),
AIRPLAY => Some(InterfaceType::Network),
_ => None,
Comment on lines +448 to +462
}
}

fn description(&self) -> Result<crate::DeviceDescription, Error> {
let name = get_device_name(self.audio_device_id).context("Failed to get device name")?;

Expand All @@ -422,8 +480,11 @@ impl Device {

let mut builder = DeviceDescriptionBuilder::new(name).direction(direction);

// Check if this is an aggregate device
if self.is_aggregate_device() {
// TransportType also reports "grup" for aggregates; the class check
// remains for devices that do not expose the property.
if let Some(interface_type) = self.transport_interface_type() {
builder = builder.interface_type(interface_type);
} else if self.is_aggregate_device() {
builder = builder.interface_type(InterfaceType::Aggregate);
}

Expand Down
Loading