From 9f2ff42da56ee19d789af9eff1d514059b506163 Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Fri, 8 Oct 2021 07:58:22 +0200 Subject: [PATCH] [.NET/AudioUnit] Use [UnmanagedCallersOnly] instead of [MonoPInvokeCallback] for the ScheduledAudioFileRegionCallback function. (#12948) Ref https://github.com/xamarin/xamarin-macios/issues/10470. --- src/AudioUnit/AUScheduledAudioFileRegion.cs | 23 ++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/AudioUnit/AUScheduledAudioFileRegion.cs b/src/AudioUnit/AUScheduledAudioFileRegion.cs index f181b1198207..c62ea6c130e8 100644 --- a/src/AudioUnit/AUScheduledAudioFileRegion.cs +++ b/src/AudioUnit/AUScheduledAudioFileRegion.cs @@ -24,7 +24,11 @@ public class AUScheduledAudioFileRegion : IDisposable { internal struct ScheduledAudioFileRegion { public AudioTimeStamp TimeStamp; +#if NET + public unsafe delegate* unmanaged CompletionHandler; +#else public ScheduledAudioFileRegionCompletionHandler CompletionHandler; +#endif public /* void * */ IntPtr CompletionHandlerUserData; public IntPtr AudioFile; public uint LoopCount; @@ -51,9 +55,10 @@ public AUScheduledAudioFileRegion (AudioFile audioFile, AUScheduledAudioFileRegi this.completionHandler = completionHandler; } +#if !NET internal delegate void ScheduledAudioFileRegionCompletionHandler ( /* void * */IntPtr userData, - /* ScheduledAudioFileRegion * */ref ScheduledAudioFileRegion fileRegion, + /* ScheduledAudioFileRegion * */ IntPtr fileRegion, /* OSStatus */ AudioUnitStatus result); static readonly ScheduledAudioFileRegionCompletionHandler static_ScheduledAudioFileRegionCompletionHandler = new ScheduledAudioFileRegionCompletionHandler (ScheduledAudioFileRegionCallback); @@ -61,7 +66,10 @@ internal delegate void ScheduledAudioFileRegionCompletionHandler ( #if !MONOMAC [MonoPInvokeCallback (typeof (ScheduledAudioFileRegionCompletionHandler))] #endif - static void ScheduledAudioFileRegionCallback (IntPtr userData, ref ScheduledAudioFileRegion fileRegion, AudioUnitStatus status) +#else + [UnmanagedCallersOnly] +#endif + static void ScheduledAudioFileRegionCallback (IntPtr userData, IntPtr fileRegion, AudioUnitStatus status) { if (userData == IntPtr.Zero) return; @@ -85,13 +93,22 @@ internal ScheduledAudioFileRegion GetAudioFileRegion () var ret = new ScheduledAudioFileRegion { TimeStamp = TimeStamp, CompletionHandlerUserData = ptr, - CompletionHandler = ptr != IntPtr.Zero ? static_ScheduledAudioFileRegionCompletionHandler : null, AudioFile = AudioFile.Handle, LoopCount = LoopCount, StartFrame = StartFrame, FramesToPlay = FramesToPlay }; + if (ptr != IntPtr.Zero) { + unsafe { +#if NET + ret.CompletionHandler = &ScheduledAudioFileRegionCallback; +#else + ret.CompletionHandler = static_ScheduledAudioFileRegionCompletionHandler; +#endif + } + } + alreadyUsed = true; return ret; }