Skip to content

Commit

Permalink
[MediaToolbox] Make P/Invokes in MTAudioProcessingTap.cs have blittab…
Browse files Browse the repository at this point in the history
…le signatures. (#20642)

Contributes towards #15684.
  • Loading branch information
rolfbjarne authored May 28, 2024
1 parent 4d75d06 commit 3c50f16
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
54 changes: 37 additions & 17 deletions src/MediaToolbox/MTAudioProcessingTap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#nullable enable

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using Foundation;
Expand Down Expand Up @@ -64,11 +65,11 @@ unsafe struct Callbacks
public /* MTAudioProcessingTapUnprepareCallback */ delegate* unmanaged<IntPtr, void> unprepare;
public /* MTAudioProcessingTapProcessCallback */ delegate* unmanaged<IntPtr, IntPtr, MTAudioProcessingTapFlags, IntPtr, IntPtr*, MTAudioProcessingTapFlags*, void> process;
#else
public /* MTAudioProcessingTapInitCallback */ MTAudioProcessingTapInitCallbackProxy init;
public /* MTAudioProcessingTapFinalizeCallback */ Action_IntPtr finalize;
public /* MTAudioProcessingTapPrepareCallback */ MTAudioProcessingTapPrepareCallbackProxy prepare;
public /* MTAudioProcessingTapUnprepareCallback */ Action_IntPtr unprepare;
public /* MTAudioProcessingTapProcessCallback */ MTAudioProcessingTapProcessCallbackProxy process;
public /* MTAudioProcessingTapInitCallback */ IntPtr init;
public /* MTAudioProcessingTapFinalizeCallback */ IntPtr finalize;
public /* MTAudioProcessingTapPrepareCallback */ IntPtr prepare;
public /* MTAudioProcessingTapUnprepareCallback */ IntPtr unprepare;
public /* MTAudioProcessingTapProcessCallback */ IntPtr process;
#endif
#pragma warning restore 169
}
Expand Down Expand Up @@ -96,11 +97,11 @@ delegate void MTAudioProcessingTapProcessCallbackProxy (/* MTAudioProcessingTapR
}

[DllImport (Constants.MediaToolboxLibrary)]
extern static /* OSStatus */ MTAudioProcessingTapError MTAudioProcessingTapCreate (
unsafe extern static /* OSStatus */ MTAudioProcessingTapError MTAudioProcessingTapCreate (
/* CFAllocatorRef*/ IntPtr allocator,
/* const MTAudioProcessingTapCallbacks* */ ref Callbacks callbacks,
/* const MTAudioProcessingTapCallbacks* */ Callbacks* callbacks,
MTAudioProcessingTapCreationFlags flags,
/* MTAudioProcessingTapRef* */ out IntPtr tapOut);
/* MTAudioProcessingTapRef* */ IntPtr* tapOut);

public MTAudioProcessingTap (MTAudioProcessingTapCallbacks callbacks, MTAudioProcessingTapCreationFlags flags)
{
Expand All @@ -127,14 +128,14 @@ public MTAudioProcessingTap (MTAudioProcessingTapCallbacks callbacks, MTAudioPro
c.process = &ProcessProxy;
#else
if (callbacks.Initialize is not null)
c.init = InitializeProxy;
c.init = Marshal.GetFunctionPointerForDelegate (InitializeProxyCallback);
if (callbacks.Finalize is not null)
c.finalize = FinalizeProxy;
c.finalize = Marshal.GetFunctionPointerForDelegate (FinalizeProxyCallback);
if (callbacks.Prepare is not null)
c.prepare = PrepareProxy;
c.prepare = Marshal.GetFunctionPointerForDelegate (PrepareProxyCallback);
if (callbacks.Unprepare is not null)
c.unprepare = UnprepareProxy;
c.process = ProcessProxy;
c.unprepare = Marshal.GetFunctionPointerForDelegate (UnprepareProxyCallback);
c.process = Marshal.GetFunctionPointerForDelegate (ProcessProxyCallback);
#endif

}
Expand All @@ -145,7 +146,11 @@ public MTAudioProcessingTap (MTAudioProcessingTapCallbacks callbacks, MTAudioPro
var gch = GCHandle.Alloc (this);
c.clientInfo = (IntPtr)gch;

var res = MTAudioProcessingTapCreate (IntPtr.Zero, ref c, flags, out var handle);
IntPtr handle;
MTAudioProcessingTapError res;
unsafe {
res = MTAudioProcessingTapCreate (IntPtr.Zero, &c, flags, &handle);
}

// we won't need the GCHandle after the Create call
gch.Free ();
Expand Down Expand Up @@ -177,18 +182,28 @@ protected override void Dispose (bool disposing)
}

[DllImport (Constants.MediaToolboxLibrary)]
extern static /* OSStatus */ MTAudioProcessingTapError MTAudioProcessingTapGetSourceAudio (
unsafe extern static /* OSStatus */ MTAudioProcessingTapError MTAudioProcessingTapGetSourceAudio (
/* MTAudioProcessingTapRef */ IntPtr tap, IntPtr numberFrames,
/* AudioBufferList* */ IntPtr bufferListInOut,
out MTAudioProcessingTapFlags flagsOut, out CMTimeRange timeRangeOut, out IntPtr numberFramesOut);
MTAudioProcessingTapFlags* flagsOut, CMTimeRange* timeRangeOut, IntPtr* numberFramesOut);

public MTAudioProcessingTapError GetSourceAudio (nint frames, AudioBuffers bufferList, out MTAudioProcessingTapFlags flags, out CMTimeRange timeRange, out nint framesProvided)
{
if (bufferList is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (bufferList));

IntPtr result;
var r = MTAudioProcessingTapGetSourceAudio (Handle, (IntPtr) frames, (IntPtr) bufferList, out flags, out timeRange, out result);
MTAudioProcessingTapError r;
flags = default;
timeRange = default;
unsafe {
r = MTAudioProcessingTapGetSourceAudio (Handle,
(IntPtr) frames,
(IntPtr) bufferList,
(MTAudioProcessingTapFlags *) Unsafe.AsPointer<MTAudioProcessingTapFlags> (ref flags),
(CMTimeRange *) Unsafe.AsPointer<CMTimeRange> (ref timeRange),
&result);
}
framesProvided = (nint) result;
return r;
}
Expand All @@ -201,6 +216,7 @@ public MTAudioProcessingTapError GetSourceAudio (nint frames, AudioBuffers buffe
[UnmanagedCallersOnly]
unsafe static void InitializeProxy (IntPtr tap, IntPtr /*void**/ clientInfo, void** tapStorage)
#else
unsafe static MTAudioProcessingTapInitCallbackProxy InitializeProxyCallback = InitializeProxy;
[MonoPInvokeCallback (typeof (MTAudioProcessingTapInitCallbackProxy))]
unsafe static void InitializeProxy (IntPtr tap, IntPtr /*void**/ clientInfo, out void* tapStorage)
#endif
Expand Down Expand Up @@ -229,6 +245,7 @@ unsafe static void InitializeProxy (IntPtr tap, IntPtr /*void**/ clientInfo, out
static unsafe void ProcessProxy (IntPtr tap, IntPtr numberFrames, MTAudioProcessingTapFlags flags,
IntPtr bufferList, IntPtr* numberFramesOut, MTAudioProcessingTapFlags* flagsOut)
#else
static MTAudioProcessingTapProcessCallbackProxy ProcessProxyCallback = ProcessProxy;
[MonoPInvokeCallback (typeof (MTAudioProcessingTapProcessCallbackProxy))]
static void ProcessProxy (IntPtr tap, IntPtr numberFrames, MTAudioProcessingTapFlags flags,
IntPtr bufferList, out IntPtr numberFramesOut, out MTAudioProcessingTapFlags flagsOut)
Expand Down Expand Up @@ -261,6 +278,7 @@ static void ProcessProxy (IntPtr tap, IntPtr numberFrames, MTAudioProcessingTapF
#if NET
[UnmanagedCallersOnly]
#else
static Action_IntPtr FinalizeProxyCallback = FinalizeProxy;
[MonoPInvokeCallback (typeof (Action_IntPtr))]
#endif
static void FinalizeProxy (IntPtr tap)
Expand All @@ -275,6 +293,7 @@ static void FinalizeProxy (IntPtr tap)
[UnmanagedCallersOnly]
static unsafe void PrepareProxy (IntPtr tap, IntPtr maxFrames, AudioStreamBasicDescription* processingFormat)
#else
static MTAudioProcessingTapPrepareCallbackProxy PrepareProxyCallback = PrepareProxy;
[MonoPInvokeCallback (typeof (MTAudioProcessingTapPrepareCallbackProxy))]
static void PrepareProxy (IntPtr tap, IntPtr maxFrames, ref AudioStreamBasicDescription processingFormat)
#endif
Expand All @@ -293,6 +312,7 @@ static void PrepareProxy (IntPtr tap, IntPtr maxFrames, ref AudioStreamBasicDesc
#if NET
[UnmanagedCallersOnly]
#else
static Action_IntPtr UnprepareProxyCallback = UnprepareProxy;
[MonoPInvokeCallback (typeof (Action_IntPtr))]
#endif
static void UnprepareProxy (IntPtr tap)
Expand Down
2 changes: 0 additions & 2 deletions tests/cecil-tests/BlittablePInvokes.KnownFailures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ public partial class BlittablePInvokes {
"AVFoundation.AVSampleCursorSyncInfo ObjCRuntime.Messaging::AVSampleCursorSyncInfo_objc_msgSend(System.IntPtr,System.IntPtr)",
"AVFoundation.AVSampleCursorSyncInfo ObjCRuntime.Messaging::AVSampleCursorSyncInfo_objc_msgSendSuper_stret(System.IntPtr,System.IntPtr)",
"AVFoundation.AVSampleCursorSyncInfo ObjCRuntime.Messaging::AVSampleCursorSyncInfo_objc_msgSendSuper(System.IntPtr,System.IntPtr)",
"MediaToolbox.MTAudioProcessingTapError MediaToolbox.MTAudioProcessingTap::MTAudioProcessingTapCreate(System.IntPtr,MediaToolbox.MTAudioProcessingTap/Callbacks&,MediaToolbox.MTAudioProcessingTapCreationFlags,System.IntPtr&)",
"MediaToolbox.MTAudioProcessingTapError MediaToolbox.MTAudioProcessingTap::MTAudioProcessingTapGetSourceAudio(System.IntPtr,System.IntPtr,System.IntPtr,MediaToolbox.MTAudioProcessingTapFlags&,CoreMedia.CMTimeRange&,System.IntPtr&)",
"System.Boolean Network.NWAdvertiseDescriptor::nw_advertise_descriptor_get_no_auto_rename(System.IntPtr)",
"System.Boolean Network.NWBrowserDescriptor::nw_browse_descriptor_get_include_txt_record(System.IntPtr)",
"System.Boolean Network.NWConnectionGroup::nw_connection_group_reinsert_extracted_connection(System.IntPtr,System.IntPtr)",
Expand Down

13 comments on commit 3c50f16

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

Please sign in to comment.