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

namespace Gst.RtspServerSharp {

	using System;
	using System.Runtime.InteropServices;

#region Autogenerated code
	[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
	internal delegate int RTSPSessionFilterFuncNative(IntPtr sess, IntPtr media, IntPtr user_data);

	internal class RTSPSessionFilterFuncInvoker {

		RTSPSessionFilterFuncNative native_cb;
		IntPtr __data;
		GLib.DestroyNotify __notify;

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

		internal RTSPSessionFilterFuncInvoker (RTSPSessionFilterFuncNative native_cb) : this (native_cb, IntPtr.Zero, null) {}

		internal RTSPSessionFilterFuncInvoker (RTSPSessionFilterFuncNative native_cb, IntPtr data) : this (native_cb, data, null) {}

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

		internal Gst.RtspServer.RTSPSessionFilterFunc Handler {
			get {
				return new Gst.RtspServer.RTSPSessionFilterFunc(InvokeNative);
			}
		}

		Gst.RtspServer.RTSPFilterResult InvokeNative (Gst.RtspServer.RTSPSession sess, Gst.RtspServer.RTSPSessionMedia media)
		{
			Gst.RtspServer.RTSPFilterResult __result = (Gst.RtspServer.RTSPFilterResult) native_cb (sess == null ? IntPtr.Zero : sess.Handle, media == null ? IntPtr.Zero : media.Handle, __data);
			return __result;
		}
	}

	internal class RTSPSessionFilterFuncWrapper {

		public int NativeCallback (IntPtr sess, IntPtr media, IntPtr user_data)
		{
			try {
				Gst.RtspServer.RTSPFilterResult __ret = managed (GLib.Object.GetObject(sess) as Gst.RtspServer.RTSPSession, GLib.Object.GetObject(media) as Gst.RtspServer.RTSPSessionMedia);
				if (release_on_call)
					gch.Free ();
				return (int) __ret;
			} catch (Exception e) {
				GLib.ExceptionManager.RaiseUnhandledException (e, true);
				// NOTREACHED: Above call does not return.
				throw e;
			}
		}

		bool release_on_call = false;
		GCHandle gch;

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

		internal RTSPSessionFilterFuncNative NativeDelegate;
		Gst.RtspServer.RTSPSessionFilterFunc managed;

		public RTSPSessionFilterFuncWrapper (Gst.RtspServer.RTSPSessionFilterFunc managed)
		{
			this.managed = managed;
			if (managed != null)
				NativeDelegate = new RTSPSessionFilterFuncNative (NativeCallback);
		}

		public static Gst.RtspServer.RTSPSessionFilterFunc GetManagedDelegate (RTSPSessionFilterFuncNative native)
		{
			if (native == null)
				return null;
			RTSPSessionFilterFuncWrapper wrapper = (RTSPSessionFilterFuncWrapper) native.Target;
			if (wrapper == null)
				return null;
			return wrapper.managed;
		}
	}
#endregion
}
