// This file was generated by the Gtk# code generator.
// Any changes made will be lost if regenerated.

namespace Gst.AudioSharp {

	using System;
	using System.Runtime.InteropServices;

#region Autogenerated code
	[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
	internal delegate void AudioFormatUnpackNative(IntPtr info, int flags, byte[] dest, byte[] data, int n_length);

	internal class AudioFormatUnpackInvoker {

		AudioFormatUnpackNative native_cb;
		IntPtr __data;
		GLib.DestroyNotify __notify;

		~AudioFormatUnpackInvoker ()
		{
			if (__notify == null)
				return;
			__notify (__data);
		}

		internal AudioFormatUnpackInvoker (AudioFormatUnpackNative native_cb) : this (native_cb, IntPtr.Zero, null) {}

		internal AudioFormatUnpackInvoker (AudioFormatUnpackNative native_cb, IntPtr data) : this (native_cb, data, null) {}

		internal AudioFormatUnpackInvoker (AudioFormatUnpackNative native_cb, IntPtr data, GLib.DestroyNotify notify)
		{
			this.native_cb = native_cb;
			__data = data;
			__notify = notify;
		}

		internal Gst.Audio.AudioFormatUnpack Handler {
			get {
				return new Gst.Audio.AudioFormatUnpack(InvokeNative);
			}
		}

		void InvokeNative (Gst.Audio.AudioFormatInfo info, Gst.Audio.AudioPackFlags flags, byte[] dest, byte[] data)
		{
			IntPtr native_info = GLib.Marshaller.StructureToPtrAlloc (info);
			native_cb (native_info, (int) flags, dest, data, (data == null ? 0 : data.Length));
			Marshal.FreeHGlobal (native_info);
		}
	}

	internal class AudioFormatUnpackWrapper {

		public void NativeCallback (IntPtr info, int flags, byte[] dest, byte[] data, int n_length)
		{
			try {
				managed (Gst.Audio.AudioFormatInfo.New (info), (Gst.Audio.AudioPackFlags) flags, dest, data);
				if (release_on_call)
					gch.Free ();
			} catch (Exception e) {
				GLib.ExceptionManager.RaiseUnhandledException (e, false);
			}
		}

		bool release_on_call = false;
		GCHandle gch;

		public void PersistUntilCalled ()
		{
			release_on_call = true;
			gch = GCHandle.Alloc (this);
		}

		internal AudioFormatUnpackNative NativeDelegate;
		Gst.Audio.AudioFormatUnpack managed;

		public AudioFormatUnpackWrapper (Gst.Audio.AudioFormatUnpack managed)
		{
			this.managed = managed;
			if (managed != null)
				NativeDelegate = new AudioFormatUnpackNative (NativeCallback);
		}

		public static Gst.Audio.AudioFormatUnpack GetManagedDelegate (AudioFormatUnpackNative native)
		{
			if (native == null)
				return null;
			AudioFormatUnpackWrapper wrapper = (AudioFormatUnpackWrapper) native.Target;
			if (wrapper == null)
				return null;
			return wrapper.managed;
		}
	}
#endregion
}
