// 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 AudioFormatPackNative(IntPtr info, int flags, byte[] src, byte[] data, int n_length);

	internal class AudioFormatPackInvoker {

		AudioFormatPackNative native_cb;
		IntPtr __data;
		GLib.DestroyNotify __notify;

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

		internal AudioFormatPackInvoker (AudioFormatPackNative native_cb) : this (native_cb, IntPtr.Zero, null) {}

		internal AudioFormatPackInvoker (AudioFormatPackNative native_cb, IntPtr data) : this (native_cb, data, null) {}

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

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

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

	internal class AudioFormatPackWrapper {

		public void NativeCallback (IntPtr info, int flags, byte[] src, byte[] data, int n_length)
		{
			try {
				managed (Gst.Audio.AudioFormatInfo.New (info), (Gst.Audio.AudioPackFlags) flags, src, 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 AudioFormatPackNative NativeDelegate;
		Gst.Audio.AudioFormatPack managed;

		public AudioFormatPackWrapper (Gst.Audio.AudioFormatPack managed)
		{
			this.managed = managed;
			if (managed != null)
				NativeDelegate = new AudioFormatPackNative (NativeCallback);
		}

		public static Gst.Audio.AudioFormatPack GetManagedDelegate (AudioFormatPackNative native)
		{
			if (native == null)
				return null;
			AudioFormatPackWrapper wrapper = (AudioFormatPackWrapper) native.Target;
			if (wrapper == null)
				return null;
			return wrapper.managed;
		}
	}
#endregion
}
