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

[AudioUnit] Make P/Invokes in AudioComponent.cs have blittable signatures. #20146

Merged
50 changes: 34 additions & 16 deletions src/AudioUnit/AudioComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using ObjCRuntime;
using AudioToolbox;
Expand Down Expand Up @@ -261,7 +262,9 @@ public AudioUnit CreateAudioUnit ()
public static AudioComponent? FindNextComponent (AudioComponent? cmp, ref AudioComponentDescription cd)
{
var handle = cmp.GetHandle ();
handle = AudioComponentFindNext (handle, ref cd);
unsafe {
handle = AudioComponentFindNext (handle, (AudioComponentDescription*) Unsafe.AsPointer<AudioComponentDescription> (ref cd));
}
return (handle != IntPtr.Zero) ? new AudioComponent (handle, false) : null;
}

Expand Down Expand Up @@ -313,38 +316,47 @@ public AudioUnit CreateAudioUnit ()
}

[DllImport (Constants.AudioUnitLibrary)]
static extern IntPtr AudioComponentFindNext (IntPtr inComponent, ref AudioComponentDescription inDesc);
unsafe static extern IntPtr AudioComponentFindNext (IntPtr inComponent, AudioComponentDescription* inDesc);

[DllImport (Constants.AudioUnitLibrary, EntryPoint = "AudioComponentCopyName")]
static extern int /* OSStatus */ AudioComponentCopyName (IntPtr component, out IntPtr cfstr);
unsafe static extern int /* OSStatus */ AudioComponentCopyName (IntPtr component, IntPtr* cfstr);

public string? Name {
get {
if (AudioComponentCopyName (Handle, out var r) == 0)
return CFString.FromHandle (r);
IntPtr r;
unsafe {
if (AudioComponentCopyName (Handle, &r) == 0)
return CFString.FromHandle (r);
}
return null;
}
}

[DllImport (Constants.AudioUnitLibrary)]
static extern int /* OSStatus */ AudioComponentGetDescription (IntPtr component, out AudioComponentDescription desc);
unsafe static extern int /* OSStatus */ AudioComponentGetDescription (IntPtr component, AudioComponentDescription* desc);

public AudioComponentDescription? Description {
get {
if (AudioComponentGetDescription (Handle, out var desc) == 0)
return desc;
AudioComponentDescription desc;
unsafe {
if (AudioComponentGetDescription (Handle, &desc) == 0)
return desc;
}

return null;
}
}

[DllImport (Constants.AudioUnitLibrary)]
static extern int /* OSStatus */ AudioComponentGetVersion (IntPtr component, out int /* UInt32* */ version);
unsafe static extern int /* OSStatus */ AudioComponentGetVersion (IntPtr component, int* /* UInt32* */ version);

public Version? Version {
get {
if (AudioComponentGetVersion (Handle, out var ret) == 0)
return new Version (ret >> 16, (ret >> 8) & 0xff, ret & 0xff);
int ret;
unsafe {
if (AudioComponentGetVersion (Handle, &ret) == 0)
return new Version (ret >> 16, (ret >> 8) & 0xff, ret & 0xff);
}

return null;
}
Expand Down Expand Up @@ -495,7 +507,7 @@ public double LastActiveTime {
[iOS (16,0)]
#endif
[DllImport (Constants.AudioUnitLibrary)]
static extern int AudioComponentCopyConfigurationInfo (IntPtr /* AudioComponent */ inComponent, out /* CFDictionaryRef** */ IntPtr outConfigurationInfo);
unsafe static extern int AudioComponentCopyConfigurationInfo (IntPtr /* AudioComponent */ inComponent, /* CFDictionaryRef** */ IntPtr* outConfigurationInfo);

#if NET
[SupportedOSPlatform ("macos13.0")]
Expand All @@ -509,7 +521,10 @@ public double LastActiveTime {
[iOS (16,0)]
#endif
public NSDictionary? GetConfigurationInfo (out int resultCode) {
resultCode = AudioComponentCopyConfigurationInfo (GetCheckedHandle (), out var dictPtr);
IntPtr dictPtr;
unsafe {
resultCode = AudioComponentCopyConfigurationInfo (GetCheckedHandle (), &dictPtr);
}
if (resultCode == 0) {
return Runtime.GetNSObject<NSDictionary> (dictPtr, owns: true);
}
Expand Down Expand Up @@ -542,8 +557,8 @@ public double LastActiveTime {
[MacCatalyst (16,0)]
#endif
[DllImport (Constants.AudioUnitLibrary)]
static extern int AudioComponentValidate (IntPtr /* AudioComponent* */ inComponent, IntPtr /* CFDictionaryRef* */ inValidationParameters,
out AudioComponentValidationResult outValidationResult);
unsafe static extern int AudioComponentValidate (IntPtr /* AudioComponent* */ inComponent, IntPtr /* CFDictionaryRef* */ inValidationParameters,
AudioComponentValidationResult* outValidationResult);

#if NET
[SupportedOSPlatform ("macos13.0")]
Expand All @@ -558,7 +573,10 @@ static extern int AudioComponentValidate (IntPtr /* AudioComponent* */ inCompone
[MacCatalyst (16,0)]
#endif
public AudioComponentValidationResult Validate (NSDictionary? validationParameters, out int resultCode) {
resultCode = AudioComponentValidate (GetCheckedHandle (), validationParameters.GetHandle (), out var result);
AudioComponentValidationResult result;
unsafe {
resultCode = AudioComponentValidate (GetCheckedHandle (), validationParameters.GetHandle (), &result);
}
if (resultCode == 0)
return result;
return AudioComponentValidationResult.Unknown;
Expand Down
6 changes: 0 additions & 6 deletions tests/cecil-tests/BlittablePInvokes.KnownFailures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,6 @@ public partial class BlittablePInvokes {
"System.Boolean SystemConfiguration.NetworkReachability::SCNetworkReachabilitySetDispatchQueue(System.IntPtr,System.IntPtr)",
"System.Byte Security.SecProtocolMetadata::sec_protocol_metadata_access_supported_signature_algorithms(System.IntPtr,ObjCRuntime.BlockLiteral*)",
"System.Byte* Network.NWEndpoint::nw_endpoint_get_signature(System.IntPtr,System.UIntPtr&)",
"System.Int32 AudioUnit.AudioComponent::AudioComponentCopyConfigurationInfo(System.IntPtr,System.IntPtr&)",
"System.Int32 AudioUnit.AudioComponent::AudioComponentCopyName(System.IntPtr,System.IntPtr&)",
"System.Int32 AudioUnit.AudioComponent::AudioComponentGetDescription(System.IntPtr,AudioUnit.AudioComponentDescription&)",
"System.Int32 AudioUnit.AudioComponent::AudioComponentGetVersion(System.IntPtr,System.Int32&)",
"System.Int32 AudioUnit.AudioComponent::AudioComponentValidate(System.IntPtr,System.IntPtr,AudioUnit.AudioComponentValidationResult&)",
"System.Int32 AudioUnit.AudioUnit::AudioObjectGetPropertyData(System.UInt32,AudioUnit.AudioObjectPropertyAddress&,System.UInt32&,System.IntPtr&,System.UInt32&,System.UInt32&)",
"System.Int32 AudioUnit.AudioUnit::AudioUnitGetProperty(System.IntPtr,AudioUnit.AudioUnitPropertyIDType,AudioUnit.AudioUnitScopeType,System.UInt32,System.UInt32&,System.UInt32&)",
"System.Int32 AudioUnit.AudioUnit::AudioUnitSetProperty(System.IntPtr,AudioUnit.AudioUnitPropertyIDType,AudioUnit.AudioUnitScopeType,System.UInt32,AudioToolbox.AudioStreamBasicDescription&,System.UInt32)",
Expand All @@ -394,7 +389,6 @@ public partial class BlittablePInvokes {
"System.Int32 Security.SslContext::SSLCopyALPNProtocols(System.IntPtr,System.IntPtr&)",
"System.Int32 Security.SslContext::SSLSetSessionTicketsEnabled(System.IntPtr,System.Boolean)",
"System.Int32 SystemConfiguration.NetworkReachability::SCNetworkReachabilityGetFlags(System.IntPtr,SystemConfiguration.NetworkReachabilityFlags&)",
"System.IntPtr AudioUnit.AudioComponent::AudioComponentFindNext(System.IntPtr,AudioUnit.AudioComponentDescription&)",
"System.IntPtr CoreGraphics.CGEvent::CGEventCreateKeyboardEvent(System.IntPtr,System.UInt16,System.Boolean)",
"System.IntPtr CoreGraphics.CGFunction::CGFunctionCreate(System.IntPtr,System.IntPtr,System.Runtime.InteropServices.NFloat*,System.IntPtr,System.Runtime.InteropServices.NFloat*,CoreGraphics.CGFunction/CGFunctionCallbacks&)",
"System.IntPtr CoreGraphics.CGImage::CGImageCreate(System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr,CoreGraphics.CGBitmapFlags,System.IntPtr,System.Runtime.InteropServices.NFloat*,System.Boolean,CoreGraphics.CGColorRenderingIntent)",
Expand Down
Loading