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 AUGraph.cs have blittable signatures. #20636

Merged
Merged
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
107 changes: 73 additions & 34 deletions src/AudioUnit/AUGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;

Expand Down Expand Up @@ -110,19 +111,28 @@ public AUGraph ()

public bool IsInitialized {
get {
return AUGraphIsInitialized (Handle, out var b) == AUGraphError.OK && b;
byte b;
unsafe {
return AUGraphIsInitialized (Handle, &b) == AUGraphError.OK && b != 0;
}
}
}

public bool IsOpen {
get {
return AUGraphIsOpen (Handle, out var b) == AUGraphError.OK && b;
byte b;
unsafe {
return AUGraphIsOpen (Handle, &b) == AUGraphError.OK && b != 0;
}
}
}

public bool IsRunning {
get {
return AUGraphIsRunning (Handle, out var b) == AUGraphError.OK && b;
byte b;
unsafe {
return AUGraphIsRunning (Handle, &b) == AUGraphError.OK && b != 0;
}
}
}

Expand Down Expand Up @@ -239,7 +249,11 @@ public int TryOpen ()

public int AddNode (AudioComponentDescription description)
{
var err = AUGraphAddNode (Handle, ref description, out var node);
AUGraphError err;
int node;
unsafe {
err = AUGraphAddNode (Handle, &description, &node);
}
if (err != 0)
throw new ArgumentException (String.Format ("Error code: {0}", err));

Expand All @@ -253,22 +267,34 @@ public AUGraphError RemoveNode (int node)

public AUGraphError GetCPULoad (out float averageCPULoad)
{
return AUGraphGetCPULoad (Handle, out averageCPULoad);
averageCPULoad = default;
unsafe {
return AUGraphGetCPULoad (Handle, (float*) Unsafe.AsPointer<float> (ref averageCPULoad));
}
}

public AUGraphError GetMaxCPULoad (out float maxCPULoad)
{
return AUGraphGetMaxCPULoad (Handle, out maxCPULoad);
maxCPULoad = default;
unsafe {
return AUGraphGetMaxCPULoad (Handle, (float*) Unsafe.AsPointer<float> (ref maxCPULoad));
}
}

public AUGraphError GetNode (uint index, out int node)
{
return AUGraphGetIndNode (Handle, index, out node);
node = default;
unsafe {
return AUGraphGetIndNode (Handle, index, (int*) Unsafe.AsPointer<int> (ref node));
}
}

public AUGraphError GetNodeCount (out int count)
{
return AUGraphGetNodeCount (Handle, out count);
count = default;
unsafe {
return AUGraphGetNodeCount (Handle, (int*) Unsafe.AsPointer<int> (ref count));
}
}

public AudioUnit GetNodeInfo (int node)
Expand All @@ -287,7 +313,10 @@ public AudioUnit GetNodeInfo (int node)

public AudioUnit? GetNodeInfo (int node, out AUGraphError error)
{
error = AUGraphNodeInfo (GetCheckedHandle (), node, IntPtr.Zero, out var ptr);
IntPtr ptr;
unsafe {
error = AUGraphNodeInfo (GetCheckedHandle (), node, null, &ptr);
}

if (error != AUGraphError.OK || ptr == IntPtr.Zero)
return null;
Expand All @@ -299,7 +328,11 @@ public AudioUnit GetNodeInfo (int node)
// Following current Api behaviour of returning an AudioUnit instead of an error
public AudioUnit? GetNodeInfo (int node, out AudioComponentDescription cd, out AUGraphError error)
{
error = AUGraphNodeInfo (GetCheckedHandle (), node, out cd, out var ptr);
IntPtr ptr;
cd = default;
unsafe {
error = AUGraphNodeInfo (GetCheckedHandle (), node, (AudioComponentDescription*) Unsafe.AsPointer<AudioComponentDescription> (ref cd), &ptr);
}

if (error != AUGraphError.OK || ptr == IntPtr.Zero)
return null;
Expand All @@ -309,12 +342,18 @@ public AudioUnit GetNodeInfo (int node)

public AUGraphError GetNumberOfInteractions (out uint interactionsCount)
{
return AUGraphGetNumberOfInteractions (Handle, out interactionsCount);
interactionsCount = default;
unsafe {
return AUGraphGetNumberOfInteractions (Handle, (uint*) Unsafe.AsPointer<uint> (ref interactionsCount));
}
}

public AUGraphError GetNumberOfInteractions (int node, out uint interactionsCount)
{
return AUGraphCountNodeInteractions (Handle, node, out interactionsCount);
interactionsCount = default;
unsafe {
return AUGraphCountNodeInteractions (Handle, node, (uint*) Unsafe.AsPointer<uint> (ref interactionsCount));
}
}

/*
Expand Down Expand Up @@ -357,15 +396,15 @@ public AUGraphError SetNodeInputCallback (int destNode, uint destInputNumber, Re
nodesCallbacks [destInputNumber] = renderDelegate;

var cb = new AURenderCallbackStruct ();
#if NET
unsafe {
#if NET
cb.Proc = &RenderCallbackImpl;
}
#else
cb.Proc = Marshal.GetFunctionPointerForDelegate (CreateRenderCallback);
cb.Proc = Marshal.GetFunctionPointerForDelegate (CreateRenderCallback);
#endif
cb.ProcRefCon = GCHandle.ToIntPtr (gcHandle);
return AUGraphSetNodeInputCallback (Handle, destNode, destInputNumber, ref cb);
cb.ProcRefCon = GCHandle.ToIntPtr (gcHandle);
return AUGraphSetNodeInputCallback (Handle, destNode, destInputNumber, &cb);
}
}
#if NET
[UnmanagedCallersOnly]
Expand Down Expand Up @@ -415,7 +454,10 @@ public AUGraphError Initialize ()

public bool Update ()
{
return AUGraphUpdate (Handle, out var isUpdated) == AUGraphError.OK && isUpdated;
byte isUpdated;
unsafe {
return AUGraphUpdate (Handle, &isUpdated) == AUGraphError.OK && isUpdated != 0;
}
}

// Quote from Learning CoreAudio Book:
Expand Down Expand Up @@ -447,22 +489,19 @@ protected override void Dispose (bool disposing)
static extern int /* OSStatus */ AUGraphOpen (IntPtr inGraph);

[DllImport (Constants.AudioToolboxLibrary)]
static extern AUGraphError AUGraphAddNode (IntPtr inGraph, ref AudioComponentDescription inDescription, out int /* AUNode = SInt32* */ outNode);
unsafe static extern AUGraphError AUGraphAddNode (IntPtr inGraph, AudioComponentDescription* inDescription, int* /* AUNode = SInt32* */ outNode);

[DllImport (Constants.AudioToolboxLibrary)]
static extern AUGraphError AUGraphRemoveNode (IntPtr inGraph, int /* AUNode = SInt32 */ inNode);

[DllImport (Constants.AudioToolboxLibrary)]
static extern AUGraphError AUGraphGetNodeCount (IntPtr inGraph, out int /* UInt32* */ outNumberOfNodes);

[DllImport (Constants.AudioToolboxLibrary)]
static extern AUGraphError AUGraphGetIndNode (IntPtr inGraph, uint /* UInt32 */ inIndex, out int /* AUNode = SInt32* */ outNode);
unsafe static extern AUGraphError AUGraphGetNodeCount (IntPtr inGraph, int* /* UInt32* */ outNumberOfNodes);

[DllImport (Constants.AudioToolboxLibrary)]
static extern AUGraphError AUGraphNodeInfo (IntPtr inGraph, int /* AUNode = SInt32 */ inNode, IntPtr outDescription, out IntPtr outAudioUnit);
unsafe static extern AUGraphError AUGraphGetIndNode (IntPtr inGraph, uint /* UInt32 */ inIndex, int* /* AUNode = SInt32* */ outNode);

[DllImport (Constants.AudioToolboxLibrary)]
static extern AUGraphError AUGraphNodeInfo (IntPtr inGraph, int /* AUNode = SInt32 */ inNode, out AudioComponentDescription outDescription, out IntPtr outAudioUnit);
unsafe static extern AUGraphError AUGraphNodeInfo (IntPtr inGraph, int /* AUNode = SInt32 */ inNode, AudioComponentDescription* outDescription, IntPtr* outAudioUnit);

[DllImport (Constants.AudioToolboxLibrary)]
static extern AUGraphError AUGraphClearConnections (IntPtr inGraph);
Expand All @@ -474,10 +513,10 @@ protected override void Dispose (bool disposing)
static extern AUGraphError AUGraphDisconnectNodeInput (IntPtr inGraph, int /* AUNode = SInt32 */ inDestNode, uint /* UInt32 */ inDestInputNumber);

[DllImport (Constants.AudioToolboxLibrary)]
static extern AUGraphError AUGraphGetNumberOfInteractions (IntPtr inGraph, out uint /* UInt32* */ outNumInteractions);
unsafe static extern AUGraphError AUGraphGetNumberOfInteractions (IntPtr inGraph, uint* /* UInt32* */ outNumInteractions);

[DllImport (Constants.AudioToolboxLibrary)]
static extern AUGraphError AUGraphCountNodeInteractions (IntPtr inGraph, int /* AUNode = SInt32 */ inNode, out uint /* UInt32* */ outNumInteractions);
unsafe static extern AUGraphError AUGraphCountNodeInteractions (IntPtr inGraph, int /* AUNode = SInt32 */ inNode, uint* /* UInt32* */ outNumInteractions);

[DllImport (Constants.AudioToolboxLibrary)]
static extern AUGraphError AUGraphInitialize (IntPtr inGraph);
Expand Down Expand Up @@ -513,25 +552,25 @@ protected override void Dispose (bool disposing)
static extern int /* OSStatus */ DisposeAUGraph (IntPtr inGraph);

[DllImport (Constants.AudioToolboxLibrary)]
static extern AUGraphError AUGraphIsOpen (IntPtr inGraph, [MarshalAs (UnmanagedType.I1)] out bool outIsOpen);
unsafe static extern AUGraphError AUGraphIsOpen (IntPtr inGraph, byte* outIsOpen);

[DllImport (Constants.AudioToolboxLibrary)]
static extern AUGraphError AUGraphIsInitialized (IntPtr inGraph, [MarshalAs (UnmanagedType.I1)] out bool outIsInitialized);
unsafe static extern AUGraphError AUGraphIsInitialized (IntPtr inGraph, byte* outIsInitialized);

[DllImport (Constants.AudioToolboxLibrary)]
static extern AUGraphError AUGraphIsRunning (IntPtr inGraph, [MarshalAs (UnmanagedType.I1)] out bool outIsRunning);
unsafe static extern AUGraphError AUGraphIsRunning (IntPtr inGraph, byte* outIsRunning);

[DllImport (Constants.AudioToolboxLibrary)]
static extern AUGraphError AUGraphGetCPULoad (IntPtr inGraph, out float /* Float32* */ outAverageCPULoad);
unsafe static extern AUGraphError AUGraphGetCPULoad (IntPtr inGraph, float* /* Float32* */ outAverageCPULoad);

[DllImport (Constants.AudioToolboxLibrary)]
static extern AUGraphError AUGraphGetMaxCPULoad (IntPtr inGraph, out float /* Float32* */ outMaxLoad);
unsafe static extern AUGraphError AUGraphGetMaxCPULoad (IntPtr inGraph, float* /* Float32* */ outMaxLoad);

[DllImport (Constants.AudioToolboxLibrary)]
static extern AUGraphError AUGraphSetNodeInputCallback (IntPtr inGraph, int /* AUNode = SInt32 */ inDestNode, uint /* UInt32 */ inDestInputNumber, ref AURenderCallbackStruct inInputCallback);
unsafe static extern AUGraphError AUGraphSetNodeInputCallback (IntPtr inGraph, int /* AUNode = SInt32 */ inDestNode, uint /* UInt32 */ inDestInputNumber, AURenderCallbackStruct* inInputCallback);

[DllImport (Constants.AudioToolboxLibrary)]
static extern AUGraphError AUGraphUpdate (IntPtr inGraph, [MarshalAs (UnmanagedType.I1)] out bool outIsUpdated);
unsafe static extern AUGraphError AUGraphUpdate (IntPtr inGraph, byte* outIsUpdated);

[DllImport (Constants.AudioToolboxLibrary)]
static extern void CAShow (IntPtr handle);
Expand Down
14 changes: 0 additions & 14 deletions tests/cecil-tests/BlittablePInvokes.KnownFailures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,6 @@
namespace Cecil.Tests {
public partial class BlittablePInvokes {
static HashSet<string> knownFailuresPInvokes = new HashSet<string> {
"AudioUnit.AUGraphError AudioUnit.AUGraph::AUGraphAddNode(System.IntPtr,AudioUnit.AudioComponentDescription&,System.Int32&)",
"AudioUnit.AUGraphError AudioUnit.AUGraph::AUGraphCountNodeInteractions(System.IntPtr,System.Int32,System.UInt32&)",
"AudioUnit.AUGraphError AudioUnit.AUGraph::AUGraphGetCPULoad(System.IntPtr,System.Single&)",
"AudioUnit.AUGraphError AudioUnit.AUGraph::AUGraphGetIndNode(System.IntPtr,System.UInt32,System.Int32&)",
"AudioUnit.AUGraphError AudioUnit.AUGraph::AUGraphGetMaxCPULoad(System.IntPtr,System.Single&)",
"AudioUnit.AUGraphError AudioUnit.AUGraph::AUGraphGetNodeCount(System.IntPtr,System.Int32&)",
"AudioUnit.AUGraphError AudioUnit.AUGraph::AUGraphGetNumberOfInteractions(System.IntPtr,System.UInt32&)",
"AudioUnit.AUGraphError AudioUnit.AUGraph::AUGraphIsInitialized(System.IntPtr,System.Boolean&)",
"AudioUnit.AUGraphError AudioUnit.AUGraph::AUGraphIsOpen(System.IntPtr,System.Boolean&)",
"AudioUnit.AUGraphError AudioUnit.AUGraph::AUGraphIsRunning(System.IntPtr,System.Boolean&)",
"AudioUnit.AUGraphError AudioUnit.AUGraph::AUGraphNodeInfo(System.IntPtr,System.Int32,AudioUnit.AudioComponentDescription&,System.IntPtr&)",
"AudioUnit.AUGraphError AudioUnit.AUGraph::AUGraphNodeInfo(System.IntPtr,System.Int32,System.IntPtr,System.IntPtr&)",
"AudioUnit.AUGraphError AudioUnit.AUGraph::AUGraphSetNodeInputCallback(System.IntPtr,System.Int32,System.UInt32,AudioUnit.AURenderCallbackStruct&)",
"AudioUnit.AUGraphError AudioUnit.AUGraph::AUGraphUpdate(System.IntPtr,System.Boolean&)",
"AVFoundation.AVSampleCursorAudioDependencyInfo ObjCRuntime.Messaging::AVSampleCursorAudioDependencyInfo_objc_msgSend(System.IntPtr,System.IntPtr)",
"AVFoundation.AVSampleCursorAudioDependencyInfo ObjCRuntime.Messaging::AVSampleCursorAudioDependencyInfo_objc_msgSendSuper(System.IntPtr,System.IntPtr)",
"AVFoundation.AVSampleCursorChunkInfo ObjCRuntime.Messaging::AVSampleCursorChunkInfo_objc_msgSend_stret(System.IntPtr,System.IntPtr)",
Expand Down
Loading