summaryrefslogtreecommitdiff
path: root/gstreamer-sharp/TypeFindDelegates.cs
blob: 8125ae67a37777fc4ef3c540f3a29f66462ee7c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
using System;
using System.Runtime.InteropServices;
using System.Collections;

namespace Gst {
  public delegate byte[] TypeFindPeekFunction (long offset, uint size);
  public delegate void TypeFindSuggestFunction (uint propability, Gst.Caps caps);
  public delegate ulong TypeFindGetLengthFunction ();
}

namespace GstSharp {

  [GLib.CDeclCallback]
  internal delegate IntPtr TypeFindPeekFunctionNative (IntPtr data, long offset, uint size);

  internal class TypeFindPeekFunctionInvoker {

    TypeFindPeekFunctionNative native_cb;
    IntPtr __data;
    GLib.DestroyNotify __notify;

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

    internal TypeFindPeekFunctionInvoker (TypeFindPeekFunctionNative native_cb) : this (native_cb, IntPtr.Zero, null) {}

    internal TypeFindPeekFunctionInvoker (TypeFindPeekFunctionNative native_cb, IntPtr data) : this (native_cb, data, null) {}

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

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

    byte[] InvokeNative (long offset, uint size) {
      IntPtr raw_ret = native_cb (IntPtr.Zero, offset, size);
      if (raw_ret == IntPtr.Zero)
        return new byte[] {};

      byte[] ret = new byte[size];
      Marshal.Copy (raw_ret, ret, 0, (int) size);

      return ret;
    }
  }

  internal class TypeFindPeekFunctionWrapper {
    /* FIXME: We should do something more intelligent here */
    private ArrayList data_cache = new ArrayList ();

    public IntPtr NativeCallback (IntPtr data, long offset, uint size) {
      try {
        byte[] __ret = managed (offset, size);
        if (release_on_call)
          gch.Free ();
        if (__ret.Length == 0)
          return IntPtr.Zero;

        IntPtr raw_ret = Marshal.AllocHGlobal (__ret.Length);
        Marshal.Copy (__ret, 0, raw_ret, (int) size);
        data_cache.Add (raw_ret);
        return raw_ret;
      } catch (Exception e) {
        GLib.ExceptionManager.RaiseUnhandledException (e, true);
        // NOTREACHED: Above call does not return.
        throw e;
      }
    }

    ~TypeFindPeekFunctionWrapper() {
      foreach (IntPtr raw in data_cache)
        Marshal.FreeHGlobal (raw);
      data_cache = null;
    }

    bool release_on_call = false;
    GCHandle gch;

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

    internal TypeFindPeekFunctionNative NativeDelegate;
    Gst.TypeFindPeekFunction managed;

    public TypeFindPeekFunctionWrapper (Gst.TypeFindPeekFunction managed) {
      this.managed = managed;
      if (managed != null)
        NativeDelegate = new TypeFindPeekFunctionNative (NativeCallback);
    }

    public static Gst.TypeFindPeekFunction GetManagedDelegate (TypeFindPeekFunctionNative native) {
      if (native == null)
        return null;
      TypeFindPeekFunctionWrapper wrapper = (TypeFindPeekFunctionWrapper) native.Target;
      if (wrapper == null)
        return null;
      return wrapper.managed;
    }
  }

  [GLib.CDeclCallback]
  internal delegate void TypeFindSuggestFunctionNative (IntPtr data, uint propability, IntPtr caps);

  internal class TypeFindSuggestFunctionInvoker {

    TypeFindSuggestFunctionNative native_cb;
    IntPtr __data;
    GLib.DestroyNotify __notify;

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

    internal TypeFindSuggestFunctionInvoker (TypeFindSuggestFunctionNative native_cb) : this (native_cb, IntPtr.Zero, null) {}

    internal TypeFindSuggestFunctionInvoker (TypeFindSuggestFunctionNative native_cb, IntPtr data) : this (native_cb, data, null) {}

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

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

    void InvokeNative (uint propability, Gst.Caps caps) {
      native_cb (IntPtr.Zero, propability, caps.Handle);
    }
  }

  internal class TypeFindSuggestFunctionWrapper {
    public void NativeCallback (IntPtr data, uint propability, IntPtr caps) {
      try {
        managed (propability, (Gst.Caps) GLib.Opaque.GetOpaque (caps, typeof (Gst.Caps), false));
        if (release_on_call)
          gch.Free ();
      } 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 TypeFindSuggestFunctionNative NativeDelegate;
    Gst.TypeFindSuggestFunction managed;

    public TypeFindSuggestFunctionWrapper (Gst.TypeFindSuggestFunction managed) {
      this.managed = managed;
      if (managed != null)
        NativeDelegate = new TypeFindSuggestFunctionNative (NativeCallback);
    }

    public static Gst.TypeFindSuggestFunction GetManagedDelegate (TypeFindSuggestFunctionNative native) {
      if (native == null)
        return null;
      TypeFindSuggestFunctionWrapper wrapper = (TypeFindSuggestFunctionWrapper) native.Target;
      if (wrapper == null)
        return null;
      return wrapper.managed;
    }
  }

  [GLib.CDeclCallback]
  internal delegate ulong TypeFindGetLengthFunctionNative (IntPtr data);

  internal class TypeFindGetLengthFunctionInvoker {

    TypeFindGetLengthFunctionNative native_cb;
    IntPtr __data;
    GLib.DestroyNotify __notify;

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

    internal TypeFindGetLengthFunctionInvoker (TypeFindGetLengthFunctionNative native_cb) : this (native_cb, IntPtr.Zero, null) {}

    internal TypeFindGetLengthFunctionInvoker (TypeFindGetLengthFunctionNative native_cb, IntPtr data) : this (native_cb, data, null) {}

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

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

    ulong InvokeNative () {
      ulong ret = native_cb (IntPtr.Zero);
      return ret;
    }
  }

  internal class TypeFindGetLengthFunctionWrapper {
    public ulong NativeCallback (IntPtr data) {
      try {
        ulong ret = managed ();
        if (release_on_call)
          gch.Free ();
        return 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 TypeFindGetLengthFunctionNative NativeDelegate;
    Gst.TypeFindGetLengthFunction managed;

    public TypeFindGetLengthFunctionWrapper (Gst.TypeFindGetLengthFunction managed) {
      this.managed = managed;
      if (managed != null)
        NativeDelegate = new TypeFindGetLengthFunctionNative (NativeCallback);
    }

    public static Gst.TypeFindGetLengthFunction GetManagedDelegate (TypeFindGetLengthFunctionNative native) {
      if (native == null)
        return null;
      TypeFindGetLengthFunctionWrapper wrapper = (TypeFindGetLengthFunctionWrapper) native.Target;
      if (wrapper == null)
        return null;
      return wrapper.managed;
    }
  }
}