Debian Codegraph @

gstreamer/src/structure.hg

raw
1 /* gstreamermm - a C++ wrapper for gstreamer
2 *
3 * Copyright 2008-2016 The gstreamermm Development Team
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free
17 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <gstreamermm/clock.h>
21 #include <gstreamermm/enums.h>
22 #include <gstreamermm/value.h>
23 #include <glibmm/date.h>
24 #include <glibmm/datetime.h>
25
26 _DEFS(gstreamermm,gst)
27
28 namespace Gst
29 {
30
31 /**
32 * Generic class containing fields of names and values.
33 * A Gst::Structure is a collection of key/value pairs. The keys are expressed
34 * as GQuarks and the values can be of any GType.
35 *
36 * In addition to the key/value pairs, a Gst::Structure also has a name. The name
37 * starts with a letter and can be filled by letters, numbers and any of "/-_.:".
38 *
39 * Gst::Structure is used by various GStreamer subsystems to store information
40 * in a flexible and extensible way. A Gst::Structure does not have a refcount
41 * because it usually is part of a higher level object such as Gst::Caps,
42 * Gst::Message, Gst::Event, Gst::Query.
43 *
44 * A Gst::Structure can be created with constructor, which take a name
45 * and an optional set of key/value pairs along with the types of the values.
46 *
47 * Field values can be changed with set_field() or set_fields().
48 *
49 * Field values can be retrieved with gst_get_field().
50 *
51 * Fields can be removed with remove_field().
52 *
53 * Strings in structures must be ASCII or UTF-8 encoded.
54 *
55 * Be aware that the current Gst::Caps / Gst::Structure serialization into string
56 * has limited support for nested Gst::Caps / Gst::Structure fields. It can only
57 * support one level of nesting. Using more levels will lead to unexpected
58 * behavior when using serialization features, such as Gst::Caps::to_string() or
59 * gst_value_serialize() and their counterparts.
60 *
61 * Last reviewed on 2016-07-12 (1.8.0)
62 */
63 class Structure
64 {
65 _CLASS_BOXEDTYPE(Structure, GstStructure, NONE, gst_structure_copy, gst_structure_free)
66 _IGNORE(gst_structure_copy, gst_structure_free, gst_structure_set_parent_refcount)
67
68 public:
69 /** Creates a Structure with the given @a name. Parses the list of variable
70 * arguments and sets fields to the values listed. Variable arguments
71 * should be passed as field name and value
72 */
73 template<class ...DataTypes>
74 explicit Structure(const Glib::ustring &name, DataTypes... data);
75
76 /** Creates a Gst::Structure from a string representation.
77 *
78 * @param the_string A string representation of a Gst::Structure. See to_string().
79 * @return A Structure. This will be invalid (see operator=) when the string
80 * could not be parsed.
81 */
82 static Structure create_from_string(const Glib::ustring& the_string);
83 _IGNORE(gst_structure_from_string)
84
85 /** Use this to discover if the Structure is a valid object.
86 */
87 operator const void*() const;
88
89 /** Release the ownership of underlying GstStructure instance.
90 *
91 * Gst::Structure's underlying instance is set to nullptr, therefore underlying
92 * object can't be accessed through this Gst::Structure anymore.
93 * @return an underlying instance of GstStructure object.
94 *
95 * Most users should not use release(). It can spoil the automatic destruction
96 * of the managed object.
97 */
98 GstStructure* release() noexcept;
99
100 _WRAP_METHOD(Glib::ustring get_name() const, gst_structure_get_name)
101 _WRAP_METHOD(bool has_name(const Glib::ustring& name) const, gst_structure_has_name)
102 _WRAP_METHOD(void set_name(const Glib::ustring& name), gst_structure_set_name)
103 _WRAP_METHOD(Glib::QueryQuark get_name_id() const, gst_structure_get_name_id)
104 _WRAP_METHOD(void remove_all_fields(), gst_structure_remove_all_fields)
105 _WRAP_METHOD(GType get_field_type(const Glib::ustring& fieldname) const, gst_structure_get_field_type)
106 _WRAP_METHOD(int size() const, gst_structure_n_fields)
107 _WRAP_METHOD(Glib::ustring get_nth_field_name(guint index) const, gst_structure_nth_field_name)
108 _WRAP_METHOD(Glib::ustring to_string() const, gst_structure_to_string)
109 _WRAP_METHOD(bool fixate_field_nearest_int(const Glib::ustring& name, int target), gst_structure_fixate_field_nearest_int)
110 _WRAP_METHOD(bool fixate_field_nearest_double(const Glib::ustring& name, double target), gst_structure_fixate_field_nearest_double)
111 _WRAP_METHOD(bool fixate_field_string(const Glib::ustring& name, const Glib::ustring& target), gst_structure_fixate_field_string)
112 _WRAP_METHOD(bool fixate_field(const Glib::ustring& name), gst_structure_fixate_field)
113 _WRAP_METHOD(bool fixate_field_boolean(const Glib::ustring& name, bool target), gst_structure_fixate_field_boolean)
114 _WRAP_METHOD(Gst::Structure intersect(const Gst::Structure& struct2) const, gst_structure_intersect)
115 _WRAP_METHOD(bool is_equal(const Gst::Structure& struct2) const, gst_structure_is_equal)
116 _WRAP_METHOD(bool is_subset(const Gst::Structure& superset) const, gst_structure_is_subset)
117 _WRAP_METHOD(bool can_intersect(const Gst::Structure& struct2) const, gst_structure_can_intersect)
118 _WRAP_METHOD(void fixate(), gst_structure_fixate)
119 _WRAP_METHOD(bool get_flagset(const Glib::ustring& fieldname, guint& value_flags, guint& value_mask) const, gst_structure_get_flagset)
120
121 _WRAP_METHOD(bool has_field(const Glib::ustring& fieldname) const, gst_structure_has_field)
122 _IGNORE(gst_structure_id_has_field)
123
124 _WRAP_METHOD(bool has_field(const Glib::ustring& fieldname, GType type) const, gst_structure_has_field_typed)
125 _IGNORE(gst_structure_id_has_field_typed)
126
127 /** Get the value of the field with name @a fieldname.
128 *
129 * @param fieldname The name of the field to get.
130 * @param value The Value class in which to store the value.
131 */
132 void get_field(const Glib::ustring& fieldname, Glib::ValueBase& value) const;
133
134 /** Get the value of the field with name @a fieldname.
135 *
136 * @param fieldname The name of the field to get.
137 * @param value The Value class in which to store the value.
138 */
139 template<typename DataType>
140 void get_field(const Glib::ustring& fieldname, Glib::Value<DataType>& value) const;
141
142 /** Gets the value of field @a fieldname with GType enum type @a enumtype
143 * into integer @a value. Caller is responsible for making sure the
144 * field exists and has the correct type. A call to this method would look
145 * like so:
146 * @code
147 * ...
148 * int state;
149 * structure.get_field("state", Glib::Value<Gst::State>::value_type(),
150 * state);
151 * ...
152 * @endcode
153 *
154 * @param fieldname The name of a field.
155 * @param enumtype The enum GType of the field.
156 * @param value An output parameter that will be set with the value.
157 * @return true if @a value could be set correctly. If there was no field
158 * with @a fieldname or the existing field did not contain an enum, this
159 * function returns false.
160 */
161 bool get_field(const Glib::ustring& fieldname, GType enumtype, int& value) const;
162 _IGNORE(gst_structure_get_enum)
163
164 /** Gets the value of field @a fieldname into DataType @a value.
165 * The caller is responsible for making sure the field exists and has the
166 * correct type.
167 *
168 * @param fieldname The name of a field.
169 * @param value The DataType to set.
170 */
171 template<typename DataType>
172 bool get_field(const Glib::ustring& fieldname, DataType& value) const;
173
174 // These are ignored because they are useful in the C API but are either
175 // variable argument functions or their functionality is already provided.
176 _IGNORE(gst_structure_id_get_value,
177 gst_structure_get_value,
178 gst_structure_id_get_valist,
179 gst_structure_get,
180 gst_structure_id_get,
181 gst_structure_get_valist,
182 gst_structure_get_int64,
183 gst_structure_get_fraction,
184 gst_structure_get_date_time,
185 gst_structure_get_clock_time,
186 gst_structure_get_int,
187 gst_structure_get_boolean,
188 gst_structure_get_double,
189 gst_structure_get_uint,
190 gst_structure_get_date,
191 gst_structure_get_uint64,
192 gst_structure_get_string
193 )
194
195 /** Sets the field with name @a fieldname to value. If the field does not
196 * exist, it is created. If the field exists, the previous value is replaced
197 * and freed.
198 *
199 * @param fieldname The name of the field to set.
200 * @param value The value to set the field to.
201 */
202 void set_field(const Glib::ustring& fieldname, const Glib::ValueBase& value);
203 _IGNORE(gst_structure_id_set_value,
204 gst_structure_set_value,
205 gst_structure_id_set_valist,
206 gst_structure_set_valist
207 )
208
209 /** Sets the field with name @a fieldname to value. If the field does not
210 * exist, it is created. If the field exists, the previous value is replaced
211 * and freed.
212 *
213 * @param fieldname The name of the field to set.
214 * @param value The value to set the field to.
215 */
216 template<typename DataType>
217 void set_field(const Glib::ustring& fieldname, const Glib::Value<DataType>& value);
218
219 /** Sets the field with name @a fieldname to value. If the field does not
220 * exist, it is created. If the field exists, the previous value is replaced
221 * and freed.
222 *
223 * @param fieldname The name of the field to set.
224 * @param value The value to set the field to.
225 */
226 void set_field(const Glib::ustring& fieldname, const char* value);
227
228 /** Sets the fields with name @a fieldname to values. If one of the fields does not
229 * exist, it is created. If the field exists, the previous value is replaced
230 * and freed.
231 *
232 * @param fieldname The name of the field to set.
233 * @param value The value to set the field to.
234 * @param further_data List of pairs of the format: fieldname, value.
235 */
236 template<class DataType, class ...DataTypes>
237 void set_fields(const Glib::ustring & fieldname, const DataType& value, DataTypes ...further_data);
238
239 /** Sets the field with name @a fieldname to the value @a value of the enum
240 * type @a enum_type. If the field does not exist, it is created. If the
241 * field exists, the previous value is replaced and freed. Caller is
242 * responsible for making sure that @a value is a valid @a enumtype. A call
243 * to this method would look like so:
244 * @code
245 * ...
246 * structure.set_field("state", Glib::Value<Gst::State>::value_type(),
247 * Gst::STATE_PAUSED);
248 * ...
249 * @endcode
250 *
251 * @param fieldname The name of the field to set.
252 * @param enumtype The enum GType that @a value should be treated as.
253 * @param value The value to set the field to.
254 */
255 void set_field(const Glib::ustring& fieldname, GType enumtype, int value);
256
257 /** Sets the field with name @a fieldname field to the DataType @a value.
258 * If the field does not exist, it is created. If the field exists,
259 * the previous value is replaced and freed. Please note that when setting
260 * fields to special types such as Gst::Fourcc and Gst::Fraction and
261 * Glib::Date they are converted to the GStreamer GTypes and thus when
262 * attempting to get these fields back as GValues, they will have the
263 * GStreamer GType.
264 *
265 * @param fieldname The name of the field to set.
266 * @param value The value to set the field to.
267 */
268 template<typename DataType>
269 void set_field(const Glib::ustring& fieldname, const DataType& value);
270
271 // These take ownership of the GValue so they are not wrapped.
272 _IGNORE(gst_structure_take_value, gst_structure_id_take_value)
273
274 /** Removes the field with name @a fieldname. If the field with the given
275 * name does not exist, the structure is unchanged.
276 *
277 * @param fieldname The name of the field to remove.
278 */
279 void remove_field(const Glib::ustring& fieldname);
280 _IGNORE(gst_structure_remove_field, gst_structure_remove_fields, gst_structure_remove_fields_valist)
281
282 /** For example,
283 * bool on_foreach(const Glib::ustring& id, const Glib::ValueBase& value);.
284 * The on_foreach function should return true if the foreach operation should
285 * continue, or false if the operation should stop with false.
286 */
287 typedef sigc::slot<bool, const Glib::ustring&, const Glib::ValueBase&> SlotForeach;
288
289 /** Calls the provided slot once for each field in the Gst::Structure. The
290 * slot must not modify the fields. See also map_in_place().
291 *
292 * @param slot A slot to call for each field.
293 * @return true if the supplied slot returns true For each of the fields,
294 * false otherwise.
295 */
296 bool foreach(const SlotForeach& slot);
297 _IGNORE(gst_structure_foreach)
298
299 /** For example,
300 * bool on_map(const Glib::ustring& id, Glib::ValueBase& value);.
301 * The map function should return true if the map operation should continue,
302 * or false if the operation should stop with false.
303 */
304 typedef sigc::slot<bool, const Glib::ustring&, Glib::ValueBase&> SlotMap;
305
306 /** Calls the provided slot once for each field in the Gst::Structure. In
307 * contrast to foreach(), the function may modify but not delete the fields.
308 * The structure must be mutable.
309 *
310 * @param slot A slot to call for each field.
311 * @return true if the supplied slot returns true For each of the fields,
312 * false otherwise.
313 */
314 bool map_in_place(const SlotMap& slot);
315 _IGNORE(gst_structure_map_in_place)
316
317 /** Fixates a Gst::Structure by changing the given field to the nearest
318 * fraction to given Gst::Fraction that is a subset of the existing field.
319 *
320 * @param field_name A field in structure.
321 * @param target The target value of the fixation.
322 * @return true If the structure could be fixated.
323 */
324 bool fixate_nearest_fraction(const Glib::ustring& field_name, const Gst::Fraction& target);
325 _IGNORE(gst_structure_fixate_field_nearest_fraction)
326
327 //Variable argument functions are ignored.
328 _IGNORE(gst_structure_set, gst_structure_id_set)
329
330
331
332 private:
333 // This method is used for varadic template recursion
334 void set_fields() {}
335 };
336
337 template<class ...DataTypes>
338 Structure::Structure(const Glib::ustring &name, DataTypes... data)
339 {
340 gobject_ = gst_structure_new_empty(name.c_str());
341 set_fields(data...);
342 }
343
344 template<class DataType, class ...DataTypes>
345 void Structure::set_fields(const Glib::ustring & name, const DataType& data, DataTypes ...further_data)
346 {
347 this->set_field(name, data);
348 this->set_fields(further_data...);
349 }
350
351 template<typename DataType>
352 bool Structure::get_field(const Glib::ustring& fieldname, DataType& value) const
353 {
354 typedef Glib::Value<DataType> ValueType;
355
356 bool ret = ValueType::value_type() == get_field_type(fieldname);
357
358 if (ret)
359 {
360 ValueType v;
361 this->get_field(fieldname, reinterpret_cast<Glib::ValueBase&>(v));
362 value = v.get();
363 }
364
365 return ret;
366 }
367
368 template<typename DataType>
369 void Structure::set_field(const Glib::ustring& fieldname, const DataType& value)
370 {
371 typedef Glib::Value<DataType> ValueType;
372 ValueType v;
373 v.init(ValueType::value_type());
374 v.set(value);
375 this->set_field(fieldname, reinterpret_cast<const Glib::ValueBase&>(v));
376 }
377
378 template<typename DataType>
379 void Structure::get_field(const Glib::ustring& fieldname, Glib::Value<DataType>& value) const
380 {
381 get_field(fieldname, reinterpret_cast<Glib::ValueBase&>(value));
382 }
383
384 template<typename DataType>
385 void Structure::set_field(const Glib::ustring& fieldname, const Glib::Value<DataType>& value)
386 {
387 set_field(fieldname, reinterpret_cast<const Glib::ValueBase&>(value));
388 }
389
390 } //namespace Gst