summaryrefslogtreecommitdiff
path: root/gstreamer-sharp/Caps.custom
blob: 4ff992ded902f4c2b88d598c446fb686c9536237 (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
public static Gst.Caps NewEmpty () {
  return new Gst.Caps ();
}

public Caps (Structure s) : this () {
  Append (s);
}

public Caps (Structure[] s) : this () {
  foreach (Structure o in s)
    Append (o);
}

public Caps (string mediaType, params object[] fields) : this () {
  Append (new Structure (mediaType, fields));
}

[DllImport ("gstreamersharpglue-0.10.dll") ]
extern static uint gstsharp_gst_caps_get_refcount_offset ();

static uint refcount_offset = gstsharp_gst_caps_get_refcount_offset ();
private int Refcount {
  get {
    unsafe {
      int* raw_ptr = (int*) ( ( (byte*) Handle) + refcount_offset);
      return (*raw_ptr);
    }
  }
}

public bool IsWritable {
  get {
    return (Refcount == 1);
  }
}

/* FIXME: This is not optimal! */
public void MakeWritable() {
  if (IsWritable)
    return;

  RemoveStructureReferences ();
  IntPtr copy = gst_caps_copy (Raw);
  Raw = copy;
  /* ^--- Takes a second ref, not good */
  Unref (Raw);
  /* ^--- Sets Owned = false, wrong! */
  Owned = true;
}

private Hashtable structures = new Hashtable ();

private void RemoveStructureReference (Structure s) {
  structures.Remove (s.Handle);
  s.CreateNativeCopy ();
}

private void RemoveStructureReferences () {
  foreach (Structure s in structures.Values) {
    s.CreateNativeCopy ();
  }
  structures.Clear ();
}

[DllImport ("libgstreamer-0.10.dll") ]
private static extern IntPtr gst_caps_get_structure (IntPtr handle, uint index);

public Structure this [uint index] {
  get {
    if (index >= Size)
      throw new ArgumentOutOfRangeException ();

    IntPtr raw_ptr = gst_caps_get_structure (Handle, (uint) index);

    if (structures.Contains (raw_ptr)) {
      Structure ret = (Gst.Structure) structures[raw_ptr];

      return ret;
    } else {
      Structure ret = new Gst.Structure (raw_ptr);

      ret.FreeNative = false;
      structures.Add (raw_ptr, ret);

      return ret;
    }
  }
}

private class StructureEnumerator : IEnumerator {
  Gst.Caps caps;
  long index;

  public StructureEnumerator (Gst.Caps caps) {
    this.caps = caps;
    index = -1;
  }

  public object Current {
    get {
      if (index >= caps.Size)
        throw new ArgumentOutOfRangeException ();
      if (index == -1)
        throw new ArgumentException ();

      return caps[ (uint) index];
    }
  }

  public bool MoveNext () {
    index += 1;
    return (index < caps.Size);
  }

  public void Reset () {
    index = -1;
  }
}

public IEnumerator GetEnumerator() {
  return new StructureEnumerator (this);
}

[DllImport ("libgstreamer-0.10.dll") ]
static extern void gst_caps_append_structure (IntPtr caps, IntPtr structure);

[DllImport ("libgstreamer-0.10.dll") ]
static extern IntPtr gst_structure_copy (IntPtr raw);

public void Append (Structure s) {
  if (!IsWritable)
    throw new ApplicationException ();

  gst_caps_append_structure (Handle, gst_structure_copy (s.Handle));
}

[DllImport ("libgstreamer-0.10.dll") ]
static extern void gst_caps_append (IntPtr caps, IntPtr caps2);

public void Append (Caps caps) {
  if (!IsWritable)
    throw new ApplicationException ();

  gst_caps_append (Handle, gst_caps_copy (caps.Handle));
}

[DllImport ("libgstreamer-0.10.dll") ]
static extern void gst_caps_merge_structure (IntPtr caps, IntPtr structure);

public void Merge (Structure s) {
  if (!IsWritable)
    throw new ApplicationException ();

  gst_caps_merge_structure (Handle, gst_structure_copy (s.Handle));
}

[DllImport ("libgstreamer-0.10.dll") ]
static extern void gst_caps_merge (IntPtr caps, IntPtr caps2);

public void Merge (Caps caps) {
  if (!IsWritable)
    throw new ApplicationException ();

  /* Removes all structures! */
  if (caps.IsAny) {
    RemoveStructureReferences ();
  }
  gst_caps_merge (Handle, gst_caps_copy (caps.Handle));
}

[DllImport ("libgstreamer-0.10.dll") ]
static extern void gst_caps_remove_structure (IntPtr caps, uint index);

public void RemoveStructure (uint index) {
  if (!IsWritable)
    throw new ApplicationException ();
  if (index >= Size)
    throw new ArgumentOutOfRangeException ();

  if (structures.Contains (this[index].Handle)) {
    RemoveStructureReference (this[index]);
  }

  gst_caps_remove_structure (Handle, index);
}

[DllImport ("libgstreamer-0.10.dll") ]
static extern bool gst_caps_do_simplify (IntPtr caps);

public bool DoSimplify () {
  if (!IsWritable)
    throw new ApplicationException ();

  /* FIXME: This is not optimal but we don't know before
   * which structures will be removed */
  RemoveStructureReferences ();

  return gst_caps_do_simplify (Handle);
}

[DllImport ("libgstreamer-0.10.dll") ]
static extern void gst_caps_truncate (IntPtr caps);

public void Truncate () {
  if (!IsWritable)
    throw new ApplicationException ();

  for (uint i = 1; i < Size; i++)
    RemoveStructureReference (this[i]);

  gst_caps_truncate (Handle);
}

[DllImport ("libgstreamer-0.10.dll") ]
static extern void gst_caps_unref (IntPtr raw);

protected override void Unref (IntPtr raw) {
  RemoveStructureReferences ();
  if (Owned) {
    gst_caps_unref (raw);
    Owned = false;
  }
}

class FinalizerInfo {
  IntPtr handle;

  public FinalizerInfo (IntPtr handle) {
    this.handle = handle;
  }

  public bool Handler () {
    gst_caps_unref (handle);
    return false;
  }
}

~Caps () {
  if (!Owned)
    return;
  RemoveStructureReferences ();

  FinalizerInfo info = new FinalizerInfo (Handle);
  Gst.GLib.Timeout.Add (50, new Gst.GLib.TimeoutHandler (info.Handler));
}

protected override Gst.GLib.Opaque Copy (IntPtr raw) {
  return Gst.GLib.Opaque.GetOpaque (gst_caps_ref (raw), typeof (Caps), true);
}