summaryrefslogtreecommitdiff
path: root/gstreamer-sharp/Structure.custom
blob: 43af59d2821dd75af499f3864cf42f8cd995bd24 (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
internal bool FreeNative = true;

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

protected override void Free (IntPtr raw) {
  if (!FreeNative)
    return;

  gst_structure_free (raw);
}

class FinalizerInfo {
  IntPtr handle;

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

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

~Structure () {
  if (!Owned || !FreeNative)
    return;
  FinalizerInfo info = new FinalizerInfo (Handle);
  GLib.Timeout.Add (50, new GLib.TimeoutHandler (info.Handler));
}

[DllImport ("gstreamer-0.10.dll") ]
static extern IntPtr gst_structure_get_name (IntPtr raw);

[DllImport ("gstreamer-0.10.dll") ]
static extern void gst_structure_set_name (IntPtr raw, IntPtr name);

public string Name {
  get  {
    IntPtr raw_ret = gst_structure_get_name (Handle);
    string ret = GLib.Marshaller.Utf8PtrToString (raw_ret);
    return ret;
  }
  set  {
    if (!IsMutable)
      throw new ApplicationException ();

    IntPtr native_value = GLib.Marshaller.StringToPtrGStrdup (value);
    gst_structure_set_name (Handle, native_value);
    GLib.Marshaller.Free (native_value);
  }
}

[DllImport ("gstreamer-0.10.dll") ]
static extern void gst_structure_set_value (IntPtr raw, IntPtr fieldname, IntPtr value);

public void SetValue (string fieldname, GLib.Value value) {
  if (!IsMutable)
    throw new ApplicationException ();

  IntPtr native_fieldname = GLib.Marshaller.StringToPtrGStrdup (fieldname);
  IntPtr native_value = GLib.Marshaller.StructureToPtrAlloc (value);
  gst_structure_set_value (Handle, native_fieldname, native_value);
  GLib.Marshaller.Free (native_fieldname);
  value = (GLib.Value) Marshal.PtrToStructure (native_value, typeof (GLib.Value));
  Marshal.FreeHGlobal (native_value);
}

[DllImport ("gstreamer-0.10.dll") ]
static extern bool gst_structure_fixate_field_boolean (IntPtr raw, IntPtr field_name, bool target);

public bool FixateFieldBoolean (string field_name, bool target) {
  if (!IsMutable)
    throw new ApplicationException ();

  IntPtr native_field_name = GLib.Marshaller.StringToPtrGStrdup (field_name);
  bool raw_ret = gst_structure_fixate_field_boolean (Handle, native_field_name, target);
  bool ret = raw_ret;
  GLib.Marshaller.Free (native_field_name);
  return ret;
}

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

public void RemoveAllFields() {
  if (!IsMutable)
    throw new ApplicationException ();

  gst_structure_remove_all_fields (Handle);
}

[DllImport ("gstreamer-0.10.dll") ]
static extern void gst_structure_remove_field (IntPtr raw, IntPtr fieldname);

public void RemoveField (string fieldname) {
  if (!IsMutable)
    throw new ApplicationException ();

  IntPtr native_fieldname = GLib.Marshaller.StringToPtrGStrdup (fieldname);
  gst_structure_remove_field (Handle, native_fieldname);
  GLib.Marshaller.Free (native_fieldname);
}

[DllImport ("gstreamer-0.10.dll") ]
static extern bool gst_structure_fixate_field_nearest_double (IntPtr raw, IntPtr field_name, double target);

public bool FixateFieldNearestDouble (string field_name, double target) {
  if (!IsMutable)
    throw new ApplicationException ();

  IntPtr native_field_name = GLib.Marshaller.StringToPtrGStrdup (field_name);
  bool raw_ret = gst_structure_fixate_field_nearest_double (Handle, native_field_name, target);
  bool ret = raw_ret;
  GLib.Marshaller.Free (native_field_name);
  return ret;
}

[DllImport ("gstreamer-0.10.dll") ]
static extern bool gst_structure_fixate_field_nearest_int (IntPtr raw, IntPtr field_name, int target);

public bool FixateFieldNearestInt (string field_name, int target) {
  if (!IsMutable)
    throw new ApplicationException ();

  IntPtr native_field_name = GLib.Marshaller.StringToPtrGStrdup (field_name);
  bool raw_ret = gst_structure_fixate_field_nearest_int (Handle, native_field_name, target);
  bool ret = raw_ret;
  GLib.Marshaller.Free (native_field_name);
  return ret;
}

[DllImport ("gstreamer-0.10.dll") ]
static extern bool gst_structure_fixate_field_nearest_fraction (IntPtr raw, IntPtr field_name, int target_numerator, int target_denominator);

public bool FixateFieldNearestFraction (string field_name, int target_numerator, int target_denominator) {
  if (!IsMutable)
    throw new ApplicationException ();

  IntPtr native_field_name = GLib.Marshaller.StringToPtrGStrdup (field_name);
  bool raw_ret = gst_structure_fixate_field_nearest_fraction (Handle, native_field_name, target_numerator, target_denominator);
  bool ret = raw_ret;
  GLib.Marshaller.Free (native_field_name);
  return ret;
}


public Structure (string name, params object[] fields) : this (name) {
  Set (fields);
}

public object Get (string field) {
  GLib.Value v;

  v = GetValue (field);
  return v.Val;
}

public void Set (string field, object value) {
  SetValue (field, new GLib.Value (value));
}

public void Set (params object[] fields) {
  int i, length = fields.Length;

  if (length % 2 != 0)
    throw new ArgumentException ();

  for (i = 0; i < length; i += 2) {
    if (fields[i].GetType () != typeof (string))
      throw new ArgumentException ();

    SetValue (fields[i] as string, new GLib.Value (fields[i+1]));
  }
}

public object this [string field] {
  set {
    if (field == null)
      throw new ArgumentNullException ();

    Set (field, value);
  }
  get {
    if (field == null)
      throw new ArgumentNullException ();

    return Get (field);
  }
}

public string[] Fields {
  get {
    string[] fields = new string[Count];
    for (uint i = 0; i < Count; i++)
      fields[i] = NthFieldName (i);

    return fields;
  }
}

public static Structure NewFromString (string structure) {
  IntPtr raw_string = GLib.Marshaller.StringToPtrGStrdup (structure);
  IntPtr raw_ret = gst_structure_from_string (raw_string, IntPtr.Zero);
  GLib.Marshaller.Free (raw_string);
  Gst.Structure ret = raw_ret == IntPtr.Zero ? null : (Gst.Structure) GLib.Opaque.GetOpaque (raw_ret, typeof (Gst.Structure), true);
  return ret;
}

[DllImport ("gstreamer-0.10.dll") ]
private static extern IntPtr gst_structure_from_string (IntPtr structure, IntPtr end);

public bool FixateFieldNearestFraction (string field_name, Fraction target) {
  return FixateFieldNearestFraction (field_name, target.Numerator, target.Denominator);
}


[DllImport ("gstreamersharpglue-0.10") ]
extern static uint gstsharp_gst_structure_get_parent_refcount_offset ();

static uint parent_refcount_offset = gstsharp_gst_structure_get_parent_refcount_offset ();

public bool IsMutable {
  get {
    unsafe {
      int **parent_refcount = (int **) ( ( (byte*) Handle) + parent_refcount_offset);

      if (*parent_refcount == (int *) IntPtr.Zero)
        return true;
      if (**parent_refcount == 1)
        return true;

      return false;
    }
  }
}

internal void CreateNativeCopy () {
  FreeNative = false;
  Raw = gst_structure_copy (Raw);
  FreeNative = true;
}