Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use objc2 crates #128

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Use objc2 crates #128

wants to merge 1 commit into from

Conversation

madsmtm
Copy link

@madsmtm madsmtm commented Jan 26, 2025

Use the objc2-* crates instead of coreaudio-sys (you knew it was coming @simlay 😉):

  • objc2-audio-toolbox.
  • objc2-core-audio.
  • objc2-core-audio-types.
  • objc2-core-midi.

Resolves every single coreaudio-sys issue1 and PR by no longer invoking bindgen, and instead generating the headers ahead of time. We should probably deprecate coreaudio-sys and archive the repo once this lands.

The bindings generated by objc2's tool header-translator are also a bit more type-safe as they include for example null-ability information, and is able to merge some enum constants into constants on the enum type.

Tested with sine example on:

  • macOS 10.12.6
  • macOS 14.7.1

Note: We've experienced problems with linking AudioUnix in the past, see RustAudio/coreaudio-sys#51, but that should be irrelevant since we now just always link AudioToolbox. This is fine, since AudioUnit has been an empty re-export of parts of AudioToolbox since macOS 10.10 (and rustc's minimum supported macOS version is 10.12) 2.

Note: I am unsure of your MSRV, but this will at least bump it to 1.71 (required for extern "C-unwind").

Footnotes

  1. Okay, maybe Docs failed to build for 0.2.5. coreaudio-sys#40 isn't quite resolved, since objc2-core-audio also failed building docs, but that's a minor issue.

  2. If you really want to, we could add #[link(name = "AudioUnit", kind = "framework")] extern "C" {} to still support older macOS versions.

Comment on lines +20 to +32
pub mod sys {
#[cfg(feature = "audio_toolbox")]
pub use objc2_audio_toolbox::*;
#[cfg(feature = "core_audio")]
pub use objc2_core_audio::*;
#[cfg(feature = "core_audio")]
pub use objc2_core_audio_types::*;
#[cfg(feature = "core_midi")]
pub use objc2_core_midi::*;

// MacTypes.h
pub type OSStatus = i32;
}
Copy link
Author

Choose a reason for hiding this comment

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

I'm not a big fan of this module, ideally users should probably import the crates themselves, but it's useful to not break too much at once.

Comment on lines +52 to 63
let mut audio_device_id: AudioDeviceID = 0;
let data_size = mem::size_of::<AudioDeviceID>() as u32;
let status = unsafe {
AudioObjectGetPropertyData(
kAudioObjectSystemObject,
&property_address as *const _,
kAudioObjectSystemObject as AudioObjectID,
NonNull::from(&property_address),
0,
null(),
&data_size as *const _ as *mut _,
&audio_device_id as *const _ as *mut _,
NonNull::from(&data_size),
NonNull::from(&mut audio_device_id).cast(),
)
};
Copy link
Author

@madsmtm madsmtm Jan 26, 2025

Choose a reason for hiding this comment

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

I made sure to use &mut on the out pointer audio_device_id here (and in similar cases below), this was UB before (AudioObjectGetPropertyData was writing through an immutable reference).

Comment on lines +34 to +109
objc2-core-foundation = { version = "0.3", optional = true, default-features = false, features = [
"std",
"CFBase",
"CFString",
] }
objc2-audio-toolbox = { version = "0.3", optional = true, default-features = false, features = [
"std",
"bitflags",
"libc",
"objc2-core-foundation",
"AUAudioUnit",
"AUAudioUnitImplementation",
"AUCocoaUIView",
"AUComponent",
"AUGraph",
"AUParameters",
"AudioCodec",
"AudioComponent",
"AudioConverter",
"AudioFile",
"AudioFileStream",
"AudioFormat",
"AudioOutputUnit",
"AudioQueue",
"AudioServices",
"AudioSession",
"AudioUnit",
"AudioUnitCarbonView",
"AudioUnitParameters",
"AudioUnitProperties",
"AudioUnitUtilities",
"AudioWorkInterval",
"CAFFile",
"CAShow",
"DefaultAudioOutput",
"ExtendedAudioFile",
"MusicDevice",
"MusicPlayer",
"objc2-core-audio",
"objc2-core-audio-types",
] }
objc2-core-audio = { version = "0.3", optional = true, default-features = false, features = [
"std",
"objc2-core-audio-types",
"AudioHardware",
"AudioHardwareDeprecated",
"AudioServerPlugIn",
"HostTime",
] }
objc2-core-audio-types = { version = "0.3", optional = true, default-features = false, features = [
"std",
"bitflags",
"AudioSessionTypes",
"CoreAudioBaseTypes",
] }
objc2-core-midi = { version = "0.3", optional = true, default-features = false, features = [
"std",
"objc2-core-foundation",
"MIDIBluetoothConnection",
"MIDICIDevice",
"MIDICIDeviceManager",
"MIDICapabilityInquiry",
"MIDIDriver",
"MIDIMessages",
"MIDINetworkSession",
"MIDIServices",
"MIDISetup",
"MIDIThruConnection",
"MIDIUMPCI",
"MIDIUMPCIProfile",
"MIDIUMPEndpoint",
"MIDIUMPEndpointManager",
"MIDIUMPFunctionBlock",
"MIDIUMPMutableEndpoint",
"MIDIUMPMutableFunctionBlock",
] }
Copy link
Author

Choose a reason for hiding this comment

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

It is unclear what features we want to enable here, so for now, I went with roughly "all features except block2 and objc2".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant