docs: convert NULL, TRUE, and FALSE to %NULL, %TRUE, and %FALSE
[platform/upstream/gstreamer.git] / gst / gstquery.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wim.taymans@chello.be>
4  *                    2005 Wim Taymans <wim@fluendo.com>
5  *
6  * gstquery.c: GstQueryType registration and Query parsing/creation
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23
24 /**
25  * SECTION:gstquery
26  * @short_description: Provide functions to create queries, and to set and parse
27  *                     values in them.
28  * @see_also: #GstPad, #GstElement
29  *
30  * Queries can be performed on pads (gst_pad_query()) and elements
31  * (gst_element_query()). Please note that some queries might need a running
32  * pipeline to work.
33  *
34  * Queries can be created using the gst_query_new_*() functions.
35  * Query values can be set using gst_query_set_*(), and parsed using
36  * gst_query_parse_*() helpers.
37  *
38  * The following example shows how to query the duration of a pipeline:
39  * |[
40  *   GstQuery *query;
41  *   gboolean res;
42  *   query = gst_query_new_duration (GST_FORMAT_TIME);
43  *   res = gst_element_query (pipeline, query);
44  *   if (res) {
45  *     gint64 duration;
46  *     gst_query_parse_duration (query, NULL, &amp;duration);
47  *     g_print ("duration = %"GST_TIME_FORMAT, GST_TIME_ARGS (duration));
48  *   } else {
49  *     g_print ("duration query failed...");
50  *   }
51  *   gst_query_unref (query);
52  * ]|
53  */
54
55
56 #include "gst_private.h"
57 #include "gstinfo.h"
58 #include "gstquery.h"
59 #include "gstvalue.h"
60 #include "gstenumtypes.h"
61 #include "gstquark.h"
62 #include "gsturi.h"
63 #include "gstbufferpool.h"
64
65 GST_DEBUG_CATEGORY_STATIC (gst_query_debug);
66 #define GST_CAT_DEFAULT gst_query_debug
67
68 static GType _gst_query_type = 0;
69
70 typedef struct
71 {
72   GstQuery query;
73
74   GstStructure *structure;
75 } GstQueryImpl;
76
77 #define GST_QUERY_STRUCTURE(q)  (((GstQueryImpl *)(q))->structure)
78
79
80 typedef struct
81 {
82   const gint type;
83   const gchar *name;
84   GQuark quark;
85 } GstQueryQuarks;
86
87 static GstQueryQuarks query_quarks[] = {
88   {GST_QUERY_UNKNOWN, "unknown", 0},
89   {GST_QUERY_POSITION, "position", 0},
90   {GST_QUERY_DURATION, "duration", 0},
91   {GST_QUERY_LATENCY, "latency", 0},
92   {GST_QUERY_JITTER, "jitter", 0},
93   {GST_QUERY_RATE, "rate", 0},
94   {GST_QUERY_SEEKING, "seeking", 0},
95   {GST_QUERY_SEGMENT, "segment", 0},
96   {GST_QUERY_CONVERT, "convert", 0},
97   {GST_QUERY_FORMATS, "formats", 0},
98   {GST_QUERY_BUFFERING, "buffering", 0},
99   {GST_QUERY_CUSTOM, "custom", 0},
100   {GST_QUERY_URI, "uri", 0},
101   {GST_QUERY_ALLOCATION, "allocation", 0},
102   {GST_QUERY_SCHEDULING, "scheduling", 0},
103   {GST_QUERY_ACCEPT_CAPS, "accept-caps", 0},
104   {GST_QUERY_CAPS, "caps", 0},
105   {GST_QUERY_DRAIN, "drain", 0},
106   {GST_QUERY_CONTEXT, "context", 0},
107
108   {0, NULL, 0}
109 };
110
111 GST_DEFINE_MINI_OBJECT_TYPE (GstQuery, gst_query);
112
113 void
114 _priv_gst_query_initialize (void)
115 {
116   gint i;
117
118   _gst_query_type = gst_query_get_type ();
119
120   GST_DEBUG_CATEGORY_INIT (gst_query_debug, "query", 0, "query system");
121
122   for (i = 0; query_quarks[i].name; i++) {
123     query_quarks[i].quark = g_quark_from_static_string (query_quarks[i].name);
124   }
125 }
126
127 /**
128  * gst_query_type_get_name:
129  * @type: the query type
130  *
131  * Get a printable name for the given query type. Do not modify or free.
132  *
133  * Returns: a reference to the static name of the query.
134  */
135 const gchar *
136 gst_query_type_get_name (GstQueryType type)
137 {
138   gint i;
139
140   for (i = 0; query_quarks[i].name; i++) {
141     if (type == query_quarks[i].type)
142       return query_quarks[i].name;
143   }
144   return "unknown";
145 }
146
147 /**
148  * gst_query_type_to_quark:
149  * @type: the query type
150  *
151  * Get the unique quark for the given query type.
152  *
153  * Returns: the quark associated with the query type
154  */
155 GQuark
156 gst_query_type_to_quark (GstQueryType type)
157 {
158   gint i;
159
160   for (i = 0; query_quarks[i].name; i++) {
161     if (type == query_quarks[i].type)
162       return query_quarks[i].quark;
163   }
164   return 0;
165 }
166
167 /**
168  * gst_query_type_get_flags:
169  * @type: a #GstQueryType
170  *
171  * Gets the #GstQueryTypeFlags associated with @type.
172  *
173  * Returns: a #GstQueryTypeFlags.
174  */
175 GstQueryTypeFlags
176 gst_query_type_get_flags (GstQueryType type)
177 {
178   GstQueryTypeFlags ret;
179
180   ret = type & ((1 << GST_QUERY_NUM_SHIFT) - 1);
181
182   return ret;
183 }
184
185 static void
186 _gst_query_free (GstQuery * query)
187 {
188   GstStructure *s;
189
190   g_return_if_fail (query != NULL);
191
192   s = GST_QUERY_STRUCTURE (query);
193   if (s) {
194     gst_structure_set_parent_refcount (s, NULL);
195     gst_structure_free (s);
196   }
197
198   g_slice_free1 (sizeof (GstQueryImpl), query);
199 }
200
201 static GstQuery *
202 _gst_query_copy (GstQuery * query)
203 {
204   GstQuery *copy;
205   GstStructure *s;
206
207   s = GST_QUERY_STRUCTURE (query);
208   if (s) {
209     s = gst_structure_copy (s);
210   }
211   copy = gst_query_new_custom (query->type, s);
212
213   return copy;
214 }
215
216 /**
217  * gst_query_new_position:
218  * @format: the default #GstFormat for the new query
219  *
220  * Constructs a new query stream position query object. Use gst_query_unref()
221  * when done with it. A position query is used to query the current position
222  * of playback in the streams, in some format.
223  *
224  * Free-function: gst_query_unref
225  *
226  * Returns: (transfer full): a new #GstQuery
227  */
228 GstQuery *
229 gst_query_new_position (GstFormat format)
230 {
231   GstQuery *query;
232   GstStructure *structure;
233
234   structure = gst_structure_new_id (GST_QUARK (QUERY_POSITION),
235       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
236       GST_QUARK (CURRENT), G_TYPE_INT64, G_GINT64_CONSTANT (-1), NULL);
237
238   query = gst_query_new_custom (GST_QUERY_POSITION, structure);
239
240   return query;
241 }
242
243 /**
244  * gst_query_set_position:
245  * @query: a #GstQuery with query type GST_QUERY_POSITION
246  * @format: the requested #GstFormat
247  * @cur: the position to set
248  *
249  * Answer a position query by setting the requested value in the given format.
250  */
251 void
252 gst_query_set_position (GstQuery * query, GstFormat format, gint64 cur)
253 {
254   GstStructure *s;
255
256   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_POSITION);
257
258   s = GST_QUERY_STRUCTURE (query);
259   g_return_if_fail (format == g_value_get_enum (gst_structure_id_get_value (s,
260               GST_QUARK (FORMAT))));
261
262   gst_structure_id_set (s,
263       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
264       GST_QUARK (CURRENT), G_TYPE_INT64, cur, NULL);
265 }
266
267 /**
268  * gst_query_parse_position:
269  * @query: a #GstQuery
270  * @format: (out) (allow-none): the storage for the #GstFormat of the
271  *     position values (may be %NULL)
272  * @cur: (out) (allow-none): the storage for the current position (may be %NULL)
273  *
274  * Parse a position query, writing the format into @format, and the position
275  * into @cur, if the respective parameters are non-%NULL.
276  */
277 void
278 gst_query_parse_position (GstQuery * query, GstFormat * format, gint64 * cur)
279 {
280   GstStructure *structure;
281
282   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_POSITION);
283
284   structure = GST_QUERY_STRUCTURE (query);
285   if (format)
286     *format =
287         (GstFormat) g_value_get_enum (gst_structure_id_get_value (structure,
288             GST_QUARK (FORMAT)));
289   if (cur)
290     *cur = g_value_get_int64 (gst_structure_id_get_value (structure,
291             GST_QUARK (CURRENT)));
292 }
293
294
295 /**
296  * gst_query_new_duration:
297  * @format: the #GstFormat for this duration query
298  *
299  * Constructs a new stream duration query object to query in the given format.
300  * Use gst_query_unref() when done with it. A duration query will give the
301  * total length of the stream.
302  *
303  * Free-function: gst_query_unref
304  *
305  * Returns: (transfer full): a new #GstQuery
306  */
307 GstQuery *
308 gst_query_new_duration (GstFormat format)
309 {
310   GstQuery *query;
311   GstStructure *structure;
312
313   structure = gst_structure_new_id (GST_QUARK (QUERY_DURATION),
314       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
315       GST_QUARK (DURATION), G_TYPE_INT64, G_GINT64_CONSTANT (-1), NULL);
316
317   query = gst_query_new_custom (GST_QUERY_DURATION, structure);
318
319   return query;
320 }
321
322 /**
323  * gst_query_set_duration:
324  * @query: a #GstQuery
325  * @format: the #GstFormat for the duration
326  * @duration: the duration of the stream
327  *
328  * Answer a duration query by setting the requested value in the given format.
329  */
330 void
331 gst_query_set_duration (GstQuery * query, GstFormat format, gint64 duration)
332 {
333   GstStructure *s;
334
335   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_DURATION);
336
337   s = GST_QUERY_STRUCTURE (query);
338   g_return_if_fail (format == g_value_get_enum (gst_structure_id_get_value (s,
339               GST_QUARK (FORMAT))));
340   gst_structure_id_set (s, GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
341       GST_QUARK (DURATION), G_TYPE_INT64, duration, NULL);
342 }
343
344 /**
345  * gst_query_parse_duration:
346  * @query: a #GstQuery
347  * @format: (out) (allow-none): the storage for the #GstFormat of the duration
348  *     value, or %NULL.
349  * @duration: (out) (allow-none): the storage for the total duration, or %NULL.
350  *
351  * Parse a duration query answer. Write the format of the duration into @format,
352  * and the value into @duration, if the respective variables are non-%NULL.
353  */
354 void
355 gst_query_parse_duration (GstQuery * query, GstFormat * format,
356     gint64 * duration)
357 {
358   GstStructure *structure;
359
360   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_DURATION);
361
362   structure = GST_QUERY_STRUCTURE (query);
363   if (format)
364     *format =
365         (GstFormat) g_value_get_enum (gst_structure_id_get_value (structure,
366             GST_QUARK (FORMAT)));
367   if (duration)
368     *duration = g_value_get_int64 (gst_structure_id_get_value (structure,
369             GST_QUARK (DURATION)));
370 }
371
372 /**
373  * gst_query_new_latency:
374  *
375  * Constructs a new latency query object.
376  * Use gst_query_unref() when done with it. A latency query is usually performed
377  * by sinks to compensate for additional latency introduced by elements in the
378  * pipeline.
379  *
380  * Free-function: gst_query_unref
381  *
382  * Returns: (transfer full): a #GstQuery
383  */
384 GstQuery *
385 gst_query_new_latency (void)
386 {
387   GstQuery *query;
388   GstStructure *structure;
389
390   structure = gst_structure_new_id (GST_QUARK (QUERY_LATENCY),
391       GST_QUARK (LIVE), G_TYPE_BOOLEAN, FALSE,
392       GST_QUARK (MIN_LATENCY), G_TYPE_UINT64, G_GUINT64_CONSTANT (0),
393       GST_QUARK (MAX_LATENCY), G_TYPE_UINT64, G_GUINT64_CONSTANT (-1), NULL);
394
395   query = gst_query_new_custom (GST_QUERY_LATENCY, structure);
396
397   return query;
398 }
399
400 /**
401  * gst_query_set_latency:
402  * @query: a #GstQuery
403  * @live: if there is a live element upstream
404  * @min_latency: the minimal latency of the upstream elements
405  * @max_latency: the maximal latency of the upstream elements
406  *
407  * Answer a latency query by setting the requested values in the given format.
408  */
409 void
410 gst_query_set_latency (GstQuery * query, gboolean live,
411     GstClockTime min_latency, GstClockTime max_latency)
412 {
413   GstStructure *structure;
414
415   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_LATENCY);
416
417   structure = GST_QUERY_STRUCTURE (query);
418   gst_structure_id_set (structure,
419       GST_QUARK (LIVE), G_TYPE_BOOLEAN, live,
420       GST_QUARK (MIN_LATENCY), G_TYPE_UINT64, min_latency,
421       GST_QUARK (MAX_LATENCY), G_TYPE_UINT64, max_latency, NULL);
422 }
423
424 /**
425  * gst_query_parse_latency:
426  * @query: a #GstQuery
427  * @live: (out) (allow-none): storage for live or %NULL
428  * @min_latency: (out) (allow-none): the storage for the min latency or %NULL
429  * @max_latency: (out) (allow-none): the storage for the max latency or %NULL
430  *
431  * Parse a latency query answer.
432  */
433 void
434 gst_query_parse_latency (GstQuery * query, gboolean * live,
435     GstClockTime * min_latency, GstClockTime * max_latency)
436 {
437   GstStructure *structure;
438
439   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_LATENCY);
440
441   structure = GST_QUERY_STRUCTURE (query);
442   if (live)
443     *live =
444         g_value_get_boolean (gst_structure_id_get_value (structure,
445             GST_QUARK (LIVE)));
446   if (min_latency)
447     *min_latency = g_value_get_uint64 (gst_structure_id_get_value (structure,
448             GST_QUARK (MIN_LATENCY)));
449   if (max_latency)
450     *max_latency = g_value_get_uint64 (gst_structure_id_get_value (structure,
451             GST_QUARK (MAX_LATENCY)));
452 }
453
454 /**
455  * gst_query_new_convert:
456  * @src_format: the source #GstFormat for the new query
457  * @value: the value to convert
458  * @dest_format: the target #GstFormat
459  *
460  * Constructs a new convert query object. Use gst_query_unref()
461  * when done with it. A convert query is used to ask for a conversion between
462  * one format and another.
463  *
464  * Free-function: gst_query_unref
465  *
466  * Returns: (transfer full): a #GstQuery
467  */
468 GstQuery *
469 gst_query_new_convert (GstFormat src_format, gint64 value,
470     GstFormat dest_format)
471 {
472   GstQuery *query;
473   GstStructure *structure;
474
475   structure = gst_structure_new_id (GST_QUARK (QUERY_CONVERT),
476       GST_QUARK (SRC_FORMAT), GST_TYPE_FORMAT, src_format,
477       GST_QUARK (SRC_VALUE), G_TYPE_INT64, value,
478       GST_QUARK (DEST_FORMAT), GST_TYPE_FORMAT, dest_format,
479       GST_QUARK (DEST_VALUE), G_TYPE_INT64, G_GINT64_CONSTANT (-1), NULL);
480
481   query = gst_query_new_custom (GST_QUERY_CONVERT, structure);
482
483   return query;
484 }
485
486 /**
487  * gst_query_set_convert:
488  * @query: a #GstQuery
489  * @src_format: the source #GstFormat
490  * @src_value: the source value
491  * @dest_format: the destination #GstFormat
492  * @dest_value: the destination value
493  *
494  * Answer a convert query by setting the requested values.
495  */
496 void
497 gst_query_set_convert (GstQuery * query, GstFormat src_format, gint64 src_value,
498     GstFormat dest_format, gint64 dest_value)
499 {
500   GstStructure *structure;
501
502   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CONVERT);
503
504   structure = GST_QUERY_STRUCTURE (query);
505   gst_structure_id_set (structure,
506       GST_QUARK (SRC_FORMAT), GST_TYPE_FORMAT, src_format,
507       GST_QUARK (SRC_VALUE), G_TYPE_INT64, src_value,
508       GST_QUARK (DEST_FORMAT), GST_TYPE_FORMAT, dest_format,
509       GST_QUARK (DEST_VALUE), G_TYPE_INT64, dest_value, NULL);
510 }
511
512 /**
513  * gst_query_parse_convert:
514  * @query: a #GstQuery
515  * @src_format: (out) (allow-none): the storage for the #GstFormat of the
516  *     source value, or %NULL
517  * @src_value: (out) (allow-none): the storage for the source value, or %NULL
518  * @dest_format: (out) (allow-none): the storage for the #GstFormat of the
519  *     destination value, or %NULL
520  * @dest_value: (out) (allow-none): the storage for the destination value,
521  *     or %NULL
522  *
523  * Parse a convert query answer. Any of @src_format, @src_value, @dest_format,
524  * and @dest_value may be %NULL, in which case that value is omitted.
525  */
526 void
527 gst_query_parse_convert (GstQuery * query, GstFormat * src_format,
528     gint64 * src_value, GstFormat * dest_format, gint64 * dest_value)
529 {
530   GstStructure *structure;
531
532   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CONVERT);
533
534   structure = GST_QUERY_STRUCTURE (query);
535   if (src_format)
536     *src_format =
537         (GstFormat) g_value_get_enum (gst_structure_id_get_value (structure,
538             GST_QUARK (SRC_FORMAT)));
539   if (src_value)
540     *src_value = g_value_get_int64 (gst_structure_id_get_value (structure,
541             GST_QUARK (SRC_VALUE)));
542   if (dest_format)
543     *dest_format =
544         (GstFormat) g_value_get_enum (gst_structure_id_get_value (structure,
545             GST_QUARK (DEST_FORMAT)));
546   if (dest_value)
547     *dest_value = g_value_get_int64 (gst_structure_id_get_value (structure,
548             GST_QUARK (DEST_VALUE)));
549 }
550
551 /**
552  * gst_query_new_segment:
553  * @format: the #GstFormat for the new query
554  *
555  * Constructs a new segment query object. Use gst_query_unref()
556  * when done with it. A segment query is used to discover information about the
557  * currently configured segment for playback.
558  *
559  * Free-function: gst_query_unref
560  *
561  * Returns: (transfer full): a new #GstQuery
562  */
563 GstQuery *
564 gst_query_new_segment (GstFormat format)
565 {
566   GstQuery *query;
567   GstStructure *structure;
568
569   structure = gst_structure_new_id (GST_QUARK (QUERY_SEGMENT),
570       GST_QUARK (RATE), G_TYPE_DOUBLE, (gdouble) 0.0,
571       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
572       GST_QUARK (START_VALUE), G_TYPE_INT64, G_GINT64_CONSTANT (-1),
573       GST_QUARK (STOP_VALUE), G_TYPE_INT64, G_GINT64_CONSTANT (-1), NULL);
574
575   query = gst_query_new_custom (GST_QUERY_SEGMENT, structure);
576
577   return query;
578 }
579
580 /**
581  * gst_query_set_segment:
582  * @query: a #GstQuery
583  * @rate: the rate of the segment
584  * @format: the #GstFormat of the segment values (@start_value and @stop_value)
585  * @start_value: the start value
586  * @stop_value: the stop value
587  *
588  * Answer a segment query by setting the requested values. The normal
589  * playback segment of a pipeline is 0 to duration at the default rate of
590  * 1.0. If a seek was performed on the pipeline to play a different
591  * segment, this query will return the range specified in the last seek.
592  *
593  * @start_value and @stop_value will respectively contain the configured
594  * playback range start and stop values expressed in @format.
595  * The values are always between 0 and the duration of the media and
596  * @start_value <= @stop_value. @rate will contain the playback rate. For
597  * negative rates, playback will actually happen from @stop_value to
598  * @start_value.
599  */
600 void
601 gst_query_set_segment (GstQuery * query, gdouble rate, GstFormat format,
602     gint64 start_value, gint64 stop_value)
603 {
604   GstStructure *structure;
605
606   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SEGMENT);
607
608   structure = GST_QUERY_STRUCTURE (query);
609   gst_structure_id_set (structure,
610       GST_QUARK (RATE), G_TYPE_DOUBLE, rate,
611       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
612       GST_QUARK (START_VALUE), G_TYPE_INT64, start_value,
613       GST_QUARK (STOP_VALUE), G_TYPE_INT64, stop_value, NULL);
614 }
615
616 /**
617  * gst_query_parse_segment:
618  * @query: a #GstQuery
619  * @rate: (out) (allow-none): the storage for the rate of the segment, or %NULL
620  * @format: (out) (allow-none): the storage for the #GstFormat of the values,
621  *     or %NULL
622  * @start_value: (out) (allow-none): the storage for the start value, or %NULL
623  * @stop_value: (out) (allow-none): the storage for the stop value, or %NULL
624  *
625  * Parse a segment query answer. Any of @rate, @format, @start_value, and
626  * @stop_value may be %NULL, which will cause this value to be omitted.
627  *
628  * See gst_query_set_segment() for an explanation of the function arguments.
629  */
630 void
631 gst_query_parse_segment (GstQuery * query, gdouble * rate, GstFormat * format,
632     gint64 * start_value, gint64 * stop_value)
633 {
634   GstStructure *structure;
635
636   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SEGMENT);
637
638   structure = GST_QUERY_STRUCTURE (query);
639   if (rate)
640     *rate = g_value_get_double (gst_structure_id_get_value (structure,
641             GST_QUARK (RATE)));
642   if (format)
643     *format =
644         (GstFormat) g_value_get_enum (gst_structure_id_get_value (structure,
645             GST_QUARK (FORMAT)));
646   if (start_value)
647     *start_value = g_value_get_int64 (gst_structure_id_get_value (structure,
648             GST_QUARK (START_VALUE)));
649   if (stop_value)
650     *stop_value = g_value_get_int64 (gst_structure_id_get_value (structure,
651             GST_QUARK (STOP_VALUE)));
652 }
653
654 /**
655  * gst_query_new_custom:
656  * @type: the query type
657  * @structure: (allow-none) (transfer full): a structure for the query
658  *
659  * Constructs a new custom query object. Use gst_query_unref()
660  * when done with it.
661  *
662  * Free-function: gst_query_unref
663  *
664  * Returns: (transfer full): a new #GstQuery
665  */
666 GstQuery *
667 gst_query_new_custom (GstQueryType type, GstStructure * structure)
668 {
669   GstQueryImpl *query;
670
671   query = g_slice_new0 (GstQueryImpl);
672
673   GST_DEBUG ("creating new query %p %s", query, gst_query_type_get_name (type));
674
675   if (structure) {
676     /* structure must not have a parent */
677     if (!gst_structure_set_parent_refcount (structure,
678             &query->query.mini_object.refcount))
679       goto had_parent;
680   }
681
682   gst_mini_object_init (GST_MINI_OBJECT_CAST (query), 0, _gst_query_type,
683       (GstMiniObjectCopyFunction) _gst_query_copy, NULL,
684       (GstMiniObjectFreeFunction) _gst_query_free);
685
686   GST_QUERY_TYPE (query) = type;
687   GST_QUERY_STRUCTURE (query) = structure;
688
689   return GST_QUERY_CAST (query);
690
691   /* ERRORS */
692 had_parent:
693   {
694     g_slice_free1 (sizeof (GstQueryImpl), query);
695     g_warning ("structure is already owned by another object");
696     return NULL;
697   }
698 }
699
700 /**
701  * gst_query_get_structure:
702  * @query: a #GstQuery
703  *
704  * Get the structure of a query.
705  *
706  * Returns: (transfer none): the #GstStructure of the query. The structure is
707  *     still owned by the query and will therefore be freed when the query
708  *     is unreffed.
709  */
710 const GstStructure *
711 gst_query_get_structure (GstQuery * query)
712 {
713   g_return_val_if_fail (GST_IS_QUERY (query), NULL);
714
715   return GST_QUERY_STRUCTURE (query);
716 }
717
718 /**
719  * gst_query_writable_structure:
720  * @query: a #GstQuery
721  *
722  * Get the structure of a query. This method should be called with a writable
723  * @query so that the returned structure is guaranteed to be writable.
724  *
725  * Returns: (transfer none): the #GstStructure of the query. The structure is
726  *     still owned by the query and will therefore be freed when the query
727  *     is unreffed.
728  */
729 GstStructure *
730 gst_query_writable_structure (GstQuery * query)
731 {
732   g_return_val_if_fail (GST_IS_QUERY (query), NULL);
733   g_return_val_if_fail (gst_query_is_writable (query), NULL);
734
735   return GST_QUERY_STRUCTURE (query);
736 }
737
738 /**
739  * gst_query_new_seeking:
740  * @format: the default #GstFormat for the new query
741  *
742  * Constructs a new query object for querying seeking properties of
743  * the stream.
744  *
745  * Free-function: gst_query_unref
746  *
747  * Returns: (transfer full): a new #GstQuery
748  */
749 GstQuery *
750 gst_query_new_seeking (GstFormat format)
751 {
752   GstQuery *query;
753   GstStructure *structure;
754
755   structure = gst_structure_new_id (GST_QUARK (QUERY_SEEKING),
756       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
757       GST_QUARK (SEEKABLE), G_TYPE_BOOLEAN, FALSE,
758       GST_QUARK (SEGMENT_START), G_TYPE_INT64, G_GINT64_CONSTANT (-1),
759       GST_QUARK (SEGMENT_END), G_TYPE_INT64, G_GINT64_CONSTANT (-1), NULL);
760
761   query = gst_query_new_custom (GST_QUERY_SEEKING, structure);
762
763   return query;
764 }
765
766 /**
767  * gst_query_set_seeking:
768  * @query: a #GstQuery
769  * @format: the format to set for the @segment_start and @segment_end values
770  * @seekable: the seekable flag to set
771  * @segment_start: the segment_start to set
772  * @segment_end: the segment_end to set
773  *
774  * Set the seeking query result fields in @query.
775  */
776 void
777 gst_query_set_seeking (GstQuery * query, GstFormat format,
778     gboolean seekable, gint64 segment_start, gint64 segment_end)
779 {
780   GstStructure *structure;
781
782   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SEEKING);
783   g_return_if_fail (gst_query_is_writable (query));
784
785   structure = GST_QUERY_STRUCTURE (query);
786   gst_structure_id_set (structure,
787       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
788       GST_QUARK (SEEKABLE), G_TYPE_BOOLEAN, seekable,
789       GST_QUARK (SEGMENT_START), G_TYPE_INT64, segment_start,
790       GST_QUARK (SEGMENT_END), G_TYPE_INT64, segment_end, NULL);
791 }
792
793 /**
794  * gst_query_parse_seeking:
795  * @query: a GST_QUERY_SEEKING type query #GstQuery
796  * @format: (out) (allow-none): the format to set for the @segment_start
797  *     and @segment_end values, or %NULL
798  * @seekable: (out) (allow-none): the seekable flag to set, or %NULL
799  * @segment_start: (out) (allow-none): the segment_start to set, or %NULL
800  * @segment_end: (out) (allow-none): the segment_end to set, or %NULL
801  *
802  * Parse a seeking query, writing the format into @format, and
803  * other results into the passed parameters, if the respective parameters
804  * are non-%NULL
805  */
806 void
807 gst_query_parse_seeking (GstQuery * query, GstFormat * format,
808     gboolean * seekable, gint64 * segment_start, gint64 * segment_end)
809 {
810   GstStructure *structure;
811
812   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SEEKING);
813
814   structure = GST_QUERY_STRUCTURE (query);
815   if (format)
816     *format =
817         (GstFormat) g_value_get_enum (gst_structure_id_get_value (structure,
818             GST_QUARK (FORMAT)));
819   if (seekable)
820     *seekable = g_value_get_boolean (gst_structure_id_get_value (structure,
821             GST_QUARK (SEEKABLE)));
822   if (segment_start)
823     *segment_start = g_value_get_int64 (gst_structure_id_get_value (structure,
824             GST_QUARK (SEGMENT_START)));
825   if (segment_end)
826     *segment_end = g_value_get_int64 (gst_structure_id_get_value (structure,
827             GST_QUARK (SEGMENT_END)));
828 }
829
830 static GArray *
831 ensure_array (GstStructure * s, GQuark quark, gsize element_size,
832     GDestroyNotify clear_func)
833 {
834   GArray *array;
835   const GValue *value;
836
837   value = gst_structure_id_get_value (s, quark);
838   if (value) {
839     array = (GArray *) g_value_get_boxed (value);
840   } else {
841     GValue new_array_val = { 0, };
842
843     array = g_array_new (FALSE, TRUE, element_size);
844     if (clear_func)
845       g_array_set_clear_func (array, clear_func);
846
847     g_value_init (&new_array_val, G_TYPE_ARRAY);
848     g_value_take_boxed (&new_array_val, array);
849
850     gst_structure_id_take_value (s, quark, &new_array_val);
851   }
852   return array;
853 }
854
855 /**
856  * gst_query_new_formats:
857  *
858  * Constructs a new query object for querying formats of
859  * the stream.
860  *
861  * Free-function: gst_query_unref
862  *
863  * Returns: (transfer full): a new #GstQuery
864  */
865 GstQuery *
866 gst_query_new_formats (void)
867 {
868   GstQuery *query;
869   GstStructure *structure;
870
871   structure = gst_structure_new_id_empty (GST_QUARK (QUERY_FORMATS));
872   query = gst_query_new_custom (GST_QUERY_FORMATS, structure);
873
874   return query;
875 }
876
877 static void
878 gst_query_list_add_format (GValue * list, GstFormat format)
879 {
880   GValue item = { 0, };
881
882   g_value_init (&item, GST_TYPE_FORMAT);
883   g_value_set_enum (&item, format);
884   gst_value_list_append_value (list, &item);
885   g_value_unset (&item);
886 }
887
888 /**
889  * gst_query_set_formats:
890  * @query: a #GstQuery
891  * @n_formats: the number of formats to set.
892  * @...: A number of @GstFormats equal to @n_formats.
893  *
894  * Set the formats query result fields in @query. The number of formats passed
895  * must be equal to @n_formats.
896  */
897 void
898 gst_query_set_formats (GstQuery * query, gint n_formats, ...)
899 {
900   va_list ap;
901   GValue list = { 0, };
902   gint i;
903   GstStructure *structure;
904
905   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_FORMATS);
906   g_return_if_fail (gst_query_is_writable (query));
907
908   g_value_init (&list, GST_TYPE_LIST);
909
910   va_start (ap, n_formats);
911   for (i = 0; i < n_formats; i++) {
912     gst_query_list_add_format (&list, va_arg (ap, GstFormat));
913   }
914   va_end (ap);
915
916   structure = GST_QUERY_STRUCTURE (query);
917   gst_structure_set_value (structure, "formats", &list);
918
919   g_value_unset (&list);
920
921 }
922
923 /**
924  * gst_query_set_formatsv:
925  * @query: a #GstQuery
926  * @n_formats: the number of formats to set.
927  * @formats: (in) (array length=n_formats): an array containing @n_formats
928  *     @GstFormat values.
929  *
930  * Set the formats query result fields in @query. The number of formats passed
931  * in the @formats array must be equal to @n_formats.
932  */
933 void
934 gst_query_set_formatsv (GstQuery * query, gint n_formats,
935     const GstFormat * formats)
936 {
937   GValue list = { 0, };
938   gint i;
939   GstStructure *structure;
940
941   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_FORMATS);
942   g_return_if_fail (gst_query_is_writable (query));
943
944   g_value_init (&list, GST_TYPE_LIST);
945   for (i = 0; i < n_formats; i++) {
946     gst_query_list_add_format (&list, formats[i]);
947   }
948   structure = GST_QUERY_STRUCTURE (query);
949   gst_structure_set_value (structure, "formats", &list);
950
951   g_value_unset (&list);
952 }
953
954 /**
955  * gst_query_parse_n_formats:
956  * @query: a #GstQuery
957  * @n_formats: (out) (allow-none): the number of formats in this query.
958  *
959  * Parse the number of formats in the formats @query.
960  */
961 void
962 gst_query_parse_n_formats (GstQuery * query, guint * n_formats)
963 {
964   GstStructure *structure;
965
966   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_FORMATS);
967
968   if (n_formats) {
969     const GValue *list;
970
971     structure = GST_QUERY_STRUCTURE (query);
972     list = gst_structure_get_value (structure, "formats");
973     if (list == NULL)
974       *n_formats = 0;
975     else
976       *n_formats = gst_value_list_get_size (list);
977   }
978 }
979
980 /**
981  * gst_query_parse_nth_format:
982  * @query: a #GstQuery
983  * @nth: (out): the nth format to retrieve.
984  * @format: (out) (allow-none): a pointer to store the nth format
985  *
986  * Parse the format query and retrieve the @nth format from it into
987  * @format. If the list contains less elements than @nth, @format will be
988  * set to GST_FORMAT_UNDEFINED.
989  */
990 void
991 gst_query_parse_nth_format (GstQuery * query, guint nth, GstFormat * format)
992 {
993   GstStructure *structure;
994
995   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_FORMATS);
996
997   if (format) {
998     const GValue *list;
999
1000     structure = GST_QUERY_STRUCTURE (query);
1001     list = gst_structure_get_value (structure, "formats");
1002     if (list == NULL) {
1003       *format = GST_FORMAT_UNDEFINED;
1004     } else {
1005       if (nth < gst_value_list_get_size (list)) {
1006         *format =
1007             (GstFormat) g_value_get_enum (gst_value_list_get_value (list, nth));
1008       } else
1009         *format = GST_FORMAT_UNDEFINED;
1010     }
1011   }
1012 }
1013
1014 /**
1015  * gst_query_new_buffering:
1016  * @format: the default #GstFormat for the new query
1017  *
1018  * Constructs a new query object for querying the buffering status of
1019  * a stream.
1020  *
1021  * Free-function: gst_query_unref
1022  *
1023  * Returns: (transfer full): a new #GstQuery
1024  */
1025 GstQuery *
1026 gst_query_new_buffering (GstFormat format)
1027 {
1028   GstQuery *query;
1029   GstStructure *structure;
1030
1031   /* by default, we configure the answer as no buffering with a 100% buffering
1032    * progress */
1033   structure = gst_structure_new_id (GST_QUARK (QUERY_BUFFERING),
1034       GST_QUARK (BUSY), G_TYPE_BOOLEAN, FALSE,
1035       GST_QUARK (BUFFER_PERCENT), G_TYPE_INT, 100,
1036       GST_QUARK (BUFFERING_MODE), GST_TYPE_BUFFERING_MODE, GST_BUFFERING_STREAM,
1037       GST_QUARK (AVG_IN_RATE), G_TYPE_INT, -1,
1038       GST_QUARK (AVG_OUT_RATE), G_TYPE_INT, -1,
1039       GST_QUARK (BUFFERING_LEFT), G_TYPE_INT64, G_GINT64_CONSTANT (0),
1040       GST_QUARK (ESTIMATED_TOTAL), G_TYPE_INT64, G_GINT64_CONSTANT (-1),
1041       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
1042       GST_QUARK (START_VALUE), G_TYPE_INT64, G_GINT64_CONSTANT (-1),
1043       GST_QUARK (STOP_VALUE), G_TYPE_INT64, G_GINT64_CONSTANT (-1), NULL);
1044
1045   query = gst_query_new_custom (GST_QUERY_BUFFERING, structure);
1046
1047   return query;
1048 }
1049
1050 /**
1051  * gst_query_set_buffering_percent:
1052  * @query: A valid #GstQuery of type GST_QUERY_BUFFERING.
1053  * @busy: if buffering is busy
1054  * @percent: a buffering percent
1055  *
1056  * Set the percentage of buffered data. This is a value between 0 and 100.
1057  * The @busy indicator is %TRUE when the buffering is in progress.
1058  */
1059 void
1060 gst_query_set_buffering_percent (GstQuery * query, gboolean busy, gint percent)
1061 {
1062   GstStructure *structure;
1063
1064   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING);
1065   g_return_if_fail (gst_query_is_writable (query));
1066   g_return_if_fail (percent >= 0 && percent <= 100);
1067
1068   structure = GST_QUERY_STRUCTURE (query);
1069   gst_structure_id_set (structure,
1070       GST_QUARK (BUSY), G_TYPE_BOOLEAN, busy,
1071       GST_QUARK (BUFFER_PERCENT), G_TYPE_INT, percent, NULL);
1072 }
1073
1074 /**
1075  * gst_query_parse_buffering_percent:
1076  * @query: A valid #GstQuery of type GST_QUERY_BUFFERING.
1077  * @busy: (out) (allow-none): if buffering is busy, or %NULL
1078  * @percent: (out) (allow-none): a buffering percent, or %NULL
1079  *
1080  * Get the percentage of buffered data. This is a value between 0 and 100.
1081  * The @busy indicator is %TRUE when the buffering is in progress.
1082  */
1083 void
1084 gst_query_parse_buffering_percent (GstQuery * query, gboolean * busy,
1085     gint * percent)
1086 {
1087   GstStructure *structure;
1088
1089   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING);
1090
1091   structure = GST_QUERY_STRUCTURE (query);
1092   if (busy)
1093     *busy = g_value_get_boolean (gst_structure_id_get_value (structure,
1094             GST_QUARK (BUSY)));
1095   if (percent)
1096     *percent = g_value_get_int (gst_structure_id_get_value (structure,
1097             GST_QUARK (BUFFER_PERCENT)));
1098 }
1099
1100 /**
1101  * gst_query_set_buffering_stats:
1102  * @query: A valid #GstQuery of type GST_QUERY_BUFFERING.
1103  * @mode: a buffering mode
1104  * @avg_in: the average input rate
1105  * @avg_out: the average output rate
1106  * @buffering_left: amount of buffering time left in milliseconds
1107  *
1108  * Configures the buffering stats values in @query.
1109  */
1110 void
1111 gst_query_set_buffering_stats (GstQuery * query, GstBufferingMode mode,
1112     gint avg_in, gint avg_out, gint64 buffering_left)
1113 {
1114   GstStructure *structure;
1115
1116   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING);
1117   g_return_if_fail (gst_query_is_writable (query));
1118
1119   structure = GST_QUERY_STRUCTURE (query);
1120   gst_structure_id_set (structure,
1121       GST_QUARK (BUFFERING_MODE), GST_TYPE_BUFFERING_MODE, mode,
1122       GST_QUARK (AVG_IN_RATE), G_TYPE_INT, avg_in,
1123       GST_QUARK (AVG_OUT_RATE), G_TYPE_INT, avg_out,
1124       GST_QUARK (BUFFERING_LEFT), G_TYPE_INT64, buffering_left, NULL);
1125 }
1126
1127 /**
1128  * gst_query_parse_buffering_stats:
1129  * @query: A valid #GstQuery of type GST_QUERY_BUFFERING.
1130  * @mode: (out) (allow-none): a buffering mode, or %NULL
1131  * @avg_in: (out) (allow-none): the average input rate, or %NULL
1132  * @avg_out: (out) (allow-none): the average output rat, or %NULL
1133  * @buffering_left: (out) (allow-none): amount of buffering time left in
1134  *     milliseconds, or %NULL
1135  *
1136  * Extracts the buffering stats values from @query.
1137  */
1138 void
1139 gst_query_parse_buffering_stats (GstQuery * query,
1140     GstBufferingMode * mode, gint * avg_in, gint * avg_out,
1141     gint64 * buffering_left)
1142 {
1143   GstStructure *structure;
1144
1145   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING);
1146
1147   structure = GST_QUERY_STRUCTURE (query);
1148   if (mode)
1149     *mode = (GstBufferingMode)
1150         g_value_get_enum (gst_structure_id_get_value (structure,
1151             GST_QUARK (BUFFERING_MODE)));
1152   if (avg_in)
1153     *avg_in = g_value_get_int (gst_structure_id_get_value (structure,
1154             GST_QUARK (AVG_IN_RATE)));
1155   if (avg_out)
1156     *avg_out = g_value_get_int (gst_structure_id_get_value (structure,
1157             GST_QUARK (AVG_OUT_RATE)));
1158   if (buffering_left)
1159     *buffering_left =
1160         g_value_get_int64 (gst_structure_id_get_value (structure,
1161             GST_QUARK (BUFFERING_LEFT)));
1162 }
1163
1164 /**
1165  * gst_query_set_buffering_range:
1166  * @query: a #GstQuery
1167  * @format: the format to set for the @start and @stop values
1168  * @start: the start to set
1169  * @stop: the stop to set
1170  * @estimated_total: estimated total amount of download time remaining in
1171  *     milliseconds
1172  *
1173  * Set the available query result fields in @query.
1174  */
1175 void
1176 gst_query_set_buffering_range (GstQuery * query, GstFormat format,
1177     gint64 start, gint64 stop, gint64 estimated_total)
1178 {
1179   GstStructure *structure;
1180
1181   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING);
1182   g_return_if_fail (gst_query_is_writable (query));
1183
1184   structure = GST_QUERY_STRUCTURE (query);
1185   gst_structure_id_set (structure,
1186       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
1187       GST_QUARK (START_VALUE), G_TYPE_INT64, start,
1188       GST_QUARK (STOP_VALUE), G_TYPE_INT64, stop,
1189       GST_QUARK (ESTIMATED_TOTAL), G_TYPE_INT64, estimated_total, NULL);
1190 }
1191
1192 /**
1193  * gst_query_parse_buffering_range:
1194  * @query: a GST_QUERY_BUFFERING type query #GstQuery
1195  * @format: (out) (allow-none): the format to set for the @segment_start
1196  *     and @segment_end values, or %NULL
1197  * @start: (out) (allow-none): the start to set, or %NULL
1198  * @stop: (out) (allow-none): the stop to set, or %NULL
1199  * @estimated_total: (out) (allow-none): estimated total amount of download
1200  *     time remaining in milliseconds, or %NULL
1201  *
1202  * Parse an available query, writing the format into @format, and
1203  * other results into the passed parameters, if the respective parameters
1204  * are non-%NULL
1205  */
1206 void
1207 gst_query_parse_buffering_range (GstQuery * query, GstFormat * format,
1208     gint64 * start, gint64 * stop, gint64 * estimated_total)
1209 {
1210   GstStructure *structure;
1211
1212   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING);
1213
1214   structure = GST_QUERY_STRUCTURE (query);
1215   if (format)
1216     *format =
1217         (GstFormat) g_value_get_enum (gst_structure_id_get_value (structure,
1218             GST_QUARK (FORMAT)));
1219   if (start)
1220     *start = g_value_get_int64 (gst_structure_id_get_value (structure,
1221             GST_QUARK (START_VALUE)));
1222   if (stop)
1223     *stop = g_value_get_int64 (gst_structure_id_get_value (structure,
1224             GST_QUARK (STOP_VALUE)));
1225   if (estimated_total)
1226     *estimated_total =
1227         g_value_get_int64 (gst_structure_id_get_value (structure,
1228             GST_QUARK (ESTIMATED_TOTAL)));
1229 }
1230
1231 /* GstQueryBufferingRange: internal struct for GArray */
1232 typedef struct
1233 {
1234   gint64 start;
1235   gint64 stop;
1236 } GstQueryBufferingRange;
1237
1238 /**
1239  * gst_query_add_buffering_range:
1240  * @query: a GST_QUERY_BUFFERING type query #GstQuery
1241  * @start: start position of the range
1242  * @stop: stop position of the range
1243  *
1244  * Set the buffering-ranges array field in @query. The current last
1245  * start position of the array should be inferior to @start.
1246  *
1247  * Returns: a #gboolean indicating if the range was added or not.
1248  */
1249 gboolean
1250 gst_query_add_buffering_range (GstQuery * query, gint64 start, gint64 stop)
1251 {
1252   GstQueryBufferingRange range;
1253   GstStructure *structure;
1254   GArray *array;
1255
1256   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING, FALSE);
1257   g_return_val_if_fail (gst_query_is_writable (query), FALSE);
1258
1259   if (G_UNLIKELY (start >= stop))
1260     return FALSE;
1261
1262   structure = GST_QUERY_STRUCTURE (query);
1263   array = ensure_array (structure, GST_QUARK (BUFFERING_RANGES),
1264       sizeof (GstQueryBufferingRange), NULL);
1265
1266   if (array->len > 1) {
1267     GstQueryBufferingRange *last;
1268
1269     last = &g_array_index (array, GstQueryBufferingRange, array->len - 1);
1270
1271     if (G_UNLIKELY (start <= last->start))
1272       return FALSE;
1273   }
1274
1275   range.start = start;
1276   range.stop = stop;
1277   g_array_append_val (array, range);
1278
1279   return TRUE;
1280 }
1281
1282 /**
1283  * gst_query_get_n_buffering_ranges:
1284  * @query: a GST_QUERY_BUFFERING type query #GstQuery
1285  *
1286  * Retrieve the number of values currently stored in the
1287  * buffered-ranges array of the query's structure.
1288  *
1289  * Returns: the range array size as a #guint.
1290  */
1291 guint
1292 gst_query_get_n_buffering_ranges (GstQuery * query)
1293 {
1294   GstStructure *structure;
1295   GArray *array;
1296
1297   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING, 0);
1298
1299   structure = GST_QUERY_STRUCTURE (query);
1300   array = ensure_array (structure, GST_QUARK (BUFFERING_RANGES),
1301       sizeof (GstQueryBufferingRange), NULL);
1302
1303   return array->len;
1304 }
1305
1306
1307 /**
1308  * gst_query_parse_nth_buffering_range:
1309  * @query: a GST_QUERY_BUFFERING type query #GstQuery
1310  * @index: position in the buffered-ranges array to read
1311  * @start: (out) (allow-none): the start position to set, or %NULL
1312  * @stop: (out) (allow-none): the stop position to set, or %NULL
1313  *
1314  * Parse an available query and get the start and stop values stored
1315  * at the @index of the buffered ranges array.
1316  *
1317  * Returns: a #gboolean indicating if the parsing succeeded.
1318  */
1319 gboolean
1320 gst_query_parse_nth_buffering_range (GstQuery * query, guint index,
1321     gint64 * start, gint64 * stop)
1322 {
1323   GstQueryBufferingRange *range;
1324   GstStructure *structure;
1325   GArray *array;
1326
1327   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING, FALSE);
1328
1329   structure = GST_QUERY_STRUCTURE (query);
1330
1331   array = ensure_array (structure, GST_QUARK (BUFFERING_RANGES),
1332       sizeof (GstQueryBufferingRange), NULL);
1333   g_return_val_if_fail (index < array->len, FALSE);
1334
1335   range = &g_array_index (array, GstQueryBufferingRange, index);
1336
1337   if (start)
1338     *start = range->start;
1339   if (stop)
1340     *stop = range->stop;
1341
1342   return TRUE;
1343 }
1344
1345
1346 /**
1347  * gst_query_new_uri:
1348  *
1349  * Constructs a new query URI query object. Use gst_query_unref()
1350  * when done with it. An URI query is used to query the current URI
1351  * that is used by the source or sink.
1352  *
1353  * Free-function: gst_query_unref
1354  *
1355  * Returns: (transfer full): a new #GstQuery
1356  */
1357 GstQuery *
1358 gst_query_new_uri (void)
1359 {
1360   GstQuery *query;
1361   GstStructure *structure;
1362
1363   structure = gst_structure_new_id (GST_QUARK (QUERY_URI),
1364       GST_QUARK (URI), G_TYPE_STRING, NULL, NULL);
1365
1366   query = gst_query_new_custom (GST_QUERY_URI, structure);
1367
1368   return query;
1369 }
1370
1371 /**
1372  * gst_query_set_uri:
1373  * @query: a #GstQuery with query type GST_QUERY_URI
1374  * @uri: the URI to set
1375  *
1376  * Answer a URI query by setting the requested URI.
1377  */
1378 void
1379 gst_query_set_uri (GstQuery * query, const gchar * uri)
1380 {
1381   GstStructure *structure;
1382
1383   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_URI);
1384   g_return_if_fail (gst_query_is_writable (query));
1385   g_return_if_fail (gst_uri_is_valid (uri));
1386
1387   structure = GST_QUERY_STRUCTURE (query);
1388   gst_structure_id_set (structure, GST_QUARK (URI), G_TYPE_STRING, uri, NULL);
1389 }
1390
1391 /**
1392  * gst_query_parse_uri:
1393  * @query: a #GstQuery
1394  * @uri: (out) (transfer full) (allow-none): the storage for the current URI
1395  *     (may be %NULL)
1396  *
1397  * Parse an URI query, writing the URI into @uri as a newly
1398  * allocated string, if the respective parameters are non-%NULL.
1399  * Free the string with g_free() after usage.
1400  */
1401 void
1402 gst_query_parse_uri (GstQuery * query, gchar ** uri)
1403 {
1404   GstStructure *structure;
1405
1406   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_URI);
1407
1408   structure = GST_QUERY_STRUCTURE (query);
1409   if (uri)
1410     *uri = g_value_dup_string (gst_structure_id_get_value (structure,
1411             GST_QUARK (URI)));
1412 }
1413
1414 /**
1415  * gst_query_set_uri_redirection:
1416  * @query: a #GstQuery with query type GST_QUERY_URI
1417  * @uri: the URI to set
1418  *
1419  * Answer a URI query by setting the requested URI redirection.
1420  *
1421  * Since: 1.2
1422  */
1423 void
1424 gst_query_set_uri_redirection (GstQuery * query, const gchar * uri)
1425 {
1426   GstStructure *structure;
1427
1428   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_URI);
1429   g_return_if_fail (gst_query_is_writable (query));
1430   g_return_if_fail (gst_uri_is_valid (uri));
1431
1432   structure = GST_QUERY_STRUCTURE (query);
1433   gst_structure_id_set (structure, GST_QUARK (URI_REDIRECTION),
1434       G_TYPE_STRING, uri, NULL);
1435 }
1436
1437 /**
1438  * gst_query_parse_uri_redirection:
1439  * @query: a #GstQuery
1440  * @uri: (out) (transfer full) (allow-none): the storage for the redirect URI
1441  *     (may be %NULL)
1442  *
1443  * Parse an URI query, writing the URI into @uri as a newly
1444  * allocated string, if the respective parameters are non-%NULL.
1445  * Free the string with g_free() after usage.
1446  *
1447  * Since: 1.2
1448  */
1449 void
1450 gst_query_parse_uri_redirection (GstQuery * query, gchar ** uri)
1451 {
1452   GstStructure *structure;
1453
1454   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_URI);
1455
1456   structure = GST_QUERY_STRUCTURE (query);
1457   if (uri) {
1458     if (!gst_structure_id_get (structure, GST_QUARK (URI_REDIRECTION),
1459             G_TYPE_STRING, uri, NULL))
1460       *uri = NULL;
1461   }
1462 }
1463
1464 /**
1465  * gst_query_set_uri_redirection_permanent:
1466  * @query: a #GstQuery with query type GST_QUERY_URI
1467  * @permanent: whether the redirect is permanent or not
1468  *
1469  * Answer a URI query by setting the requested URI redirection
1470  * to permanent or not.
1471  *
1472  * Since: 1.4
1473  */
1474 void
1475 gst_query_set_uri_redirection_permanent (GstQuery * query, gboolean permanent)
1476 {
1477   GstStructure *structure;
1478
1479   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_URI);
1480   g_return_if_fail (gst_query_is_writable (query));
1481
1482   structure = GST_QUERY_STRUCTURE (query);
1483   gst_structure_id_set (structure, GST_QUARK (URI_REDIRECTION_PERMANENT),
1484       G_TYPE_BOOLEAN, permanent, NULL);
1485 }
1486
1487 /**
1488  * gst_query_parse_uri_redirection_permanent:
1489  * @query: a #GstQuery
1490  * @permanent: (out) (allow-none): if the URI redirection is permanent
1491  *     (may be %NULL)
1492  *
1493  * Parse an URI query, and set @permanent to %TRUE if there is a redirection
1494  * and it should be considered permanent. If a redirection is permanent,
1495  * applications should update their internal storage of the URI, otherwise
1496  * they should make all future requests to the original URI.
1497  *
1498  * Since: 1.4
1499  */
1500 void
1501 gst_query_parse_uri_redirection_permanent (GstQuery * query,
1502     gboolean * permanent)
1503 {
1504   GstStructure *structure;
1505
1506   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_URI);
1507
1508   structure = GST_QUERY_STRUCTURE (query);
1509   if (permanent) {
1510     if (!gst_structure_id_get (structure, GST_QUARK (URI_REDIRECTION_PERMANENT),
1511             G_TYPE_BOOLEAN, permanent, NULL))
1512       *permanent = FALSE;
1513   }
1514 }
1515
1516 /**
1517  * gst_query_new_allocation:
1518  * @caps: the negotiated caps
1519  * @need_pool: return a pool
1520  *
1521  * Constructs a new query object for querying the allocation properties.
1522  *
1523  * Free-function: gst_query_unref
1524  *
1525  * Returns: (transfer full): a new #GstQuery
1526  */
1527 GstQuery *
1528 gst_query_new_allocation (GstCaps * caps, gboolean need_pool)
1529 {
1530   GstQuery *query;
1531   GstStructure *structure;
1532
1533   structure = gst_structure_new_id (GST_QUARK (QUERY_ALLOCATION),
1534       GST_QUARK (CAPS), GST_TYPE_CAPS, caps,
1535       GST_QUARK (NEED_POOL), G_TYPE_BOOLEAN, need_pool, NULL);
1536
1537   query = gst_query_new_custom (GST_QUERY_ALLOCATION, structure);
1538
1539   return query;
1540 }
1541
1542 /**
1543  * gst_query_parse_allocation:
1544  * @query: a #GstQuery
1545  * @caps: (out) (transfer none) (allow-none): The #GstCaps
1546  * @need_pool: (out) (allow-none): Whether a #GstBufferPool is needed
1547  *
1548  * Parse an allocation query, writing the requested caps in @caps and
1549  * whether a pool is needed in @need_pool, if the respective parameters
1550  * are non-%NULL.
1551  */
1552 void
1553 gst_query_parse_allocation (GstQuery * query, GstCaps ** caps,
1554     gboolean * need_pool)
1555 {
1556   GstStructure *structure;
1557
1558   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
1559
1560   structure = GST_QUERY_STRUCTURE (query);
1561   if (caps) {
1562     *caps = g_value_get_boxed (gst_structure_id_get_value (structure,
1563             GST_QUARK (CAPS)));
1564   }
1565   gst_structure_id_get (structure,
1566       GST_QUARK (NEED_POOL), G_TYPE_BOOLEAN, need_pool, NULL);
1567 }
1568
1569 typedef struct
1570 {
1571   GstBufferPool *pool;
1572   guint size;
1573   guint min_buffers;
1574   guint max_buffers;
1575 } AllocationPool;
1576
1577 static void
1578 allocation_pool_free (AllocationPool * ap)
1579 {
1580   if (ap->pool)
1581     gst_object_unref (ap->pool);
1582 }
1583
1584 /**
1585  * gst_query_add_allocation_pool:
1586  * @query: A valid #GstQuery of type GST_QUERY_ALLOCATION.
1587  * @pool: the #GstBufferPool
1588  * @size: the size
1589  * @min_buffers: the min buffers
1590  * @max_buffers: the max buffers
1591  *
1592  * Set the pool parameters in @query.
1593  */
1594 void
1595 gst_query_add_allocation_pool (GstQuery * query, GstBufferPool * pool,
1596     guint size, guint min_buffers, guint max_buffers)
1597 {
1598   GArray *array;
1599   GstStructure *structure;
1600   AllocationPool ap;
1601
1602   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
1603   g_return_if_fail (gst_query_is_writable (query));
1604   g_return_if_fail (size != 0);
1605
1606   structure = GST_QUERY_STRUCTURE (query);
1607   array = ensure_array (structure, GST_QUARK (POOL),
1608       sizeof (AllocationPool), (GDestroyNotify) allocation_pool_free);
1609
1610   if ((ap.pool = pool))
1611     gst_object_ref (pool);
1612   ap.size = size;
1613   ap.min_buffers = min_buffers;
1614   ap.max_buffers = max_buffers;
1615
1616   g_array_append_val (array, ap);
1617 }
1618
1619
1620 /**
1621  * gst_query_get_n_allocation_pools:
1622  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1623  *
1624  * Retrieve the number of values currently stored in the
1625  * pool array of the query's structure.
1626  *
1627  * Returns: the pool array size as a #guint.
1628  */
1629 guint
1630 gst_query_get_n_allocation_pools (GstQuery * query)
1631 {
1632   GArray *array;
1633   GstStructure *structure;
1634
1635   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION, 0);
1636
1637   structure = GST_QUERY_STRUCTURE (query);
1638   array = ensure_array (structure, GST_QUARK (POOL),
1639       sizeof (AllocationPool), (GDestroyNotify) allocation_pool_free);
1640
1641   return array->len;
1642 }
1643
1644 /**
1645  * gst_query_parse_nth_allocation_pool:
1646  * @query: A valid #GstQuery of type GST_QUERY_ALLOCATION.
1647  * @index: index to parse
1648  * @pool: (out) (allow-none) (transfer full): the #GstBufferPool
1649  * @size: (out) (allow-none): the size
1650  * @min_buffers: (out) (allow-none): the min buffers
1651  * @max_buffers: (out) (allow-none): the max buffers
1652  *
1653  * Get the pool parameters in @query.
1654  *
1655  * Unref @pool with gst_object_unref() when it's not needed any more.
1656  */
1657 void
1658 gst_query_parse_nth_allocation_pool (GstQuery * query, guint index,
1659     GstBufferPool ** pool, guint * size, guint * min_buffers,
1660     guint * max_buffers)
1661 {
1662   GArray *array;
1663   GstStructure *structure;
1664   AllocationPool *ap;
1665
1666   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
1667
1668   structure = GST_QUERY_STRUCTURE (query);
1669   array = ensure_array (structure, GST_QUARK (POOL),
1670       sizeof (AllocationPool), (GDestroyNotify) allocation_pool_free);
1671   g_return_if_fail (index < array->len);
1672
1673   ap = &g_array_index (array, AllocationPool, index);
1674
1675   if (pool)
1676     if ((*pool = ap->pool))
1677       gst_object_ref (*pool);
1678   if (size)
1679     *size = ap->size;
1680   if (min_buffers)
1681     *min_buffers = ap->min_buffers;
1682   if (max_buffers)
1683     *max_buffers = ap->max_buffers;
1684 }
1685
1686 /**
1687  * gst_query_set_nth_allocation_pool:
1688  * @index: index to modify
1689  * @query: A valid #GstQuery of type GST_QUERY_ALLOCATION.
1690  * @pool: the #GstBufferPool
1691  * @size: the size
1692  * @min_buffers: the min buffers
1693  * @max_buffers: the max buffers
1694  *
1695  * Set the pool parameters in @query.
1696  */
1697 void
1698 gst_query_set_nth_allocation_pool (GstQuery * query, guint index,
1699     GstBufferPool * pool, guint size, guint min_buffers, guint max_buffers)
1700 {
1701   GArray *array;
1702   GstStructure *structure;
1703   AllocationPool *oldap, ap;
1704
1705   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
1706
1707   structure = GST_QUERY_STRUCTURE (query);
1708   array = ensure_array (structure, GST_QUARK (POOL),
1709       sizeof (AllocationPool), (GDestroyNotify) allocation_pool_free);
1710   g_return_if_fail (index < array->len);
1711
1712   oldap = &g_array_index (array, AllocationPool, index);
1713   allocation_pool_free (oldap);
1714
1715   if ((ap.pool = pool))
1716     gst_object_ref (pool);
1717   ap.size = size;
1718   ap.min_buffers = min_buffers;
1719   ap.max_buffers = max_buffers;
1720   g_array_index (array, AllocationPool, index) = ap;
1721 }
1722
1723 /**
1724  * gst_query_remove_nth_allocation_pool:
1725  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1726  * @index: position in the allocation pool array to remove
1727  *
1728  * Remove the allocation pool at @index of the allocation pool array.
1729  *
1730  * Since: 1.2
1731  */
1732 void
1733 gst_query_remove_nth_allocation_pool (GstQuery * query, guint index)
1734 {
1735   GArray *array;
1736   GstStructure *structure;
1737
1738   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
1739   g_return_if_fail (gst_query_is_writable (query));
1740
1741   structure = GST_QUERY_STRUCTURE (query);
1742   array =
1743       ensure_array (structure, GST_QUARK (POOL), sizeof (AllocationPool),
1744       (GDestroyNotify) allocation_pool_free);
1745   g_return_if_fail (index < array->len);
1746
1747   g_array_remove_index (array, index);
1748 }
1749
1750 typedef struct
1751 {
1752   GType api;
1753   GstStructure *params;
1754 } AllocationMeta;
1755
1756 static void
1757 allocation_meta_free (AllocationMeta * am)
1758 {
1759   if (am->params)
1760     gst_structure_free (am->params);
1761 }
1762
1763 /**
1764  * gst_query_add_allocation_meta:
1765  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1766  * @api: the metadata API
1767  * @params: (transfer none) (allow-none): API specific parameters
1768  *
1769  * Add @api with @params as one of the supported metadata API to @query.
1770  */
1771 void
1772 gst_query_add_allocation_meta (GstQuery * query, GType api,
1773     const GstStructure * params)
1774 {
1775   GArray *array;
1776   GstStructure *structure;
1777   AllocationMeta am;
1778
1779   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
1780   g_return_if_fail (api != 0);
1781   g_return_if_fail (gst_query_is_writable (query));
1782
1783   structure = GST_QUERY_STRUCTURE (query);
1784   array =
1785       ensure_array (structure, GST_QUARK (META), sizeof (AllocationMeta),
1786       (GDestroyNotify) allocation_meta_free);
1787
1788   am.api = api;
1789   am.params = (params ? gst_structure_copy (params) : NULL);
1790
1791   g_array_append_val (array, am);
1792 }
1793
1794 /**
1795  * gst_query_get_n_allocation_metas:
1796  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1797  *
1798  * Retrieve the number of values currently stored in the
1799  * meta API array of the query's structure.
1800  *
1801  * Returns: the metadata API array size as a #guint.
1802  */
1803 guint
1804 gst_query_get_n_allocation_metas (GstQuery * query)
1805 {
1806   GArray *array;
1807   GstStructure *structure;
1808
1809   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION, 0);
1810
1811   structure = GST_QUERY_STRUCTURE (query);
1812   array =
1813       ensure_array (structure, GST_QUARK (META), sizeof (AllocationMeta),
1814       (GDestroyNotify) allocation_meta_free);
1815
1816   return array->len;
1817 }
1818
1819 /**
1820  * gst_query_parse_nth_allocation_meta:
1821  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1822  * @index: position in the metadata API array to read
1823  * @params: (out) (transfer none) (allow-none): API specific flags
1824  *
1825  * Parse an available query and get the metadata API
1826  * at @index of the metadata API array.
1827  *
1828  * Returns: a #GType of the metadata API at @index.
1829  */
1830 GType
1831 gst_query_parse_nth_allocation_meta (GstQuery * query, guint index,
1832     const GstStructure ** params)
1833 {
1834   GArray *array;
1835   GstStructure *structure;
1836   AllocationMeta *am;
1837
1838   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION, 0);
1839
1840   structure = GST_QUERY_STRUCTURE (query);
1841   array =
1842       ensure_array (structure, GST_QUARK (META), sizeof (AllocationMeta),
1843       (GDestroyNotify) allocation_meta_free);
1844
1845   g_return_val_if_fail (index < array->len, 0);
1846
1847   am = &g_array_index (array, AllocationMeta, index);
1848
1849   if (params)
1850     *params = am->params;
1851
1852   return am->api;
1853 }
1854
1855 /**
1856  * gst_query_remove_nth_allocation_meta:
1857  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1858  * @index: position in the metadata API array to remove
1859  *
1860  * Remove the metadata API at @index of the metadata API array.
1861  */
1862 void
1863 gst_query_remove_nth_allocation_meta (GstQuery * query, guint index)
1864 {
1865   GArray *array;
1866   GstStructure *structure;
1867
1868   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
1869   g_return_if_fail (gst_query_is_writable (query));
1870
1871   structure = GST_QUERY_STRUCTURE (query);
1872   array =
1873       ensure_array (structure, GST_QUARK (META), sizeof (AllocationMeta),
1874       (GDestroyNotify) allocation_meta_free);
1875   g_return_if_fail (index < array->len);
1876
1877   g_array_remove_index (array, index);
1878 }
1879
1880 /**
1881  * gst_query_find_allocation_meta:
1882  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1883  * @api: the metadata API
1884  * @index: (out) (transfer none) (allow-none): the index
1885  *
1886  * Check if @query has metadata @api set. When this function returns %TRUE,
1887  * @index will contain the index where the requested API and the flags can be
1888  * found.
1889  *
1890  * Returns: %TRUE when @api is in the list of metadata.
1891  */
1892 gboolean
1893 gst_query_find_allocation_meta (GstQuery * query, GType api, guint * index)
1894 {
1895   GArray *array;
1896   GstStructure *structure;
1897   guint i, len;
1898
1899   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION, FALSE);
1900   g_return_val_if_fail (api != 0, FALSE);
1901
1902   structure = GST_QUERY_STRUCTURE (query);
1903   array =
1904       ensure_array (structure, GST_QUARK (META), sizeof (AllocationMeta),
1905       (GDestroyNotify) allocation_meta_free);
1906
1907   len = array->len;
1908   for (i = 0; i < len; i++) {
1909     AllocationMeta *am = &g_array_index (array, AllocationMeta, i);
1910     if (am->api == api) {
1911       if (index)
1912         *index = i;
1913       return TRUE;
1914     }
1915   }
1916   return FALSE;
1917 }
1918
1919 typedef struct
1920 {
1921   GstAllocator *allocator;
1922   GstAllocationParams params;
1923 } AllocationParam;
1924
1925 static void
1926 allocation_param_free (AllocationParam * ap)
1927 {
1928   if (ap->allocator)
1929     gst_object_unref (ap->allocator);
1930 }
1931
1932 /**
1933  * gst_query_add_allocation_param:
1934  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1935  * @allocator: (transfer none) (allow-none): the memory allocator
1936  * @params: (transfer none) (allow-none): a #GstAllocationParams
1937  *
1938  * Add @allocator and its @params as a supported memory allocator.
1939  */
1940 void
1941 gst_query_add_allocation_param (GstQuery * query, GstAllocator * allocator,
1942     const GstAllocationParams * params)
1943 {
1944   GArray *array;
1945   GstStructure *structure;
1946   AllocationParam ap;
1947
1948   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
1949   g_return_if_fail (gst_query_is_writable (query));
1950   g_return_if_fail (allocator != NULL || params != NULL);
1951
1952   structure = GST_QUERY_STRUCTURE (query);
1953   array = ensure_array (structure, GST_QUARK (ALLOCATOR),
1954       sizeof (AllocationParam), (GDestroyNotify) allocation_param_free);
1955
1956   if ((ap.allocator = allocator))
1957     gst_object_ref (allocator);
1958   if (params)
1959     ap.params = *params;
1960   else
1961     gst_allocation_params_init (&ap.params);
1962
1963   g_array_append_val (array, ap);
1964 }
1965
1966 /**
1967  * gst_query_get_n_allocation_params:
1968  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1969  *
1970  * Retrieve the number of values currently stored in the
1971  * allocator params array of the query's structure.
1972  *
1973  * If no memory allocator is specified, the downstream element can handle
1974  * the default memory allocator. The first memory allocator in the query
1975  * should be generic and allow mapping to system memory, all following
1976  * allocators should be ordered by preference with the preferred one first.
1977  *
1978  * Returns: the allocator array size as a #guint.
1979  */
1980 guint
1981 gst_query_get_n_allocation_params (GstQuery * query)
1982 {
1983   GArray *array;
1984   GstStructure *structure;
1985
1986   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION, 0);
1987
1988   structure = GST_QUERY_STRUCTURE (query);
1989   array = ensure_array (structure, GST_QUARK (ALLOCATOR),
1990       sizeof (AllocationParam), (GDestroyNotify) allocation_param_free);
1991
1992   return array->len;
1993 }
1994
1995 /**
1996  * gst_query_parse_nth_allocation_param:
1997  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1998  * @index: position in the allocator array to read
1999  * @allocator: (out) (transfer full) (allow-none): variable to hold the result
2000  * @params: (out) (allow-none): parameters for the allocator
2001  *
2002  * Parse an available query and get the allocator and its params
2003  * at @index of the allocator array.
2004  */
2005 void
2006 gst_query_parse_nth_allocation_param (GstQuery * query, guint index,
2007     GstAllocator ** allocator, GstAllocationParams * params)
2008 {
2009   GArray *array;
2010   GstStructure *structure;
2011   AllocationParam *ap;
2012
2013   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
2014
2015   structure = GST_QUERY_STRUCTURE (query);
2016   array = ensure_array (structure, GST_QUARK (ALLOCATOR),
2017       sizeof (AllocationParam), (GDestroyNotify) allocation_param_free);
2018   g_return_if_fail (index < array->len);
2019
2020   ap = &g_array_index (array, AllocationParam, index);
2021
2022   if (allocator)
2023     if ((*allocator = ap->allocator))
2024       gst_object_ref (*allocator);
2025   if (params)
2026     *params = ap->params;
2027 }
2028
2029 /**
2030  * gst_query_set_nth_allocation_param:
2031  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
2032  * @index: position in the allocator array to set
2033  * @allocator: (transfer none) (allow-none): new allocator to set
2034  * @params: (transfer none) (allow-none): parameters for the allocator
2035  *
2036  * Parse an available query and get the allocator and its params
2037  * at @index of the allocator array.
2038  */
2039 void
2040 gst_query_set_nth_allocation_param (GstQuery * query, guint index,
2041     GstAllocator * allocator, const GstAllocationParams * params)
2042 {
2043   GArray *array;
2044   GstStructure *structure;
2045   AllocationParam *old, ap;
2046
2047   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
2048
2049   structure = GST_QUERY_STRUCTURE (query);
2050   array = ensure_array (structure, GST_QUARK (ALLOCATOR),
2051       sizeof (AllocationParam), (GDestroyNotify) allocation_param_free);
2052   g_return_if_fail (index < array->len);
2053
2054   old = &g_array_index (array, AllocationParam, index);
2055   allocation_param_free (old);
2056
2057   if ((ap.allocator = allocator))
2058     gst_object_ref (allocator);
2059   if (params)
2060     ap.params = *params;
2061   else
2062     gst_allocation_params_init (&ap.params);
2063
2064   g_array_index (array, AllocationParam, index) = ap;
2065 }
2066
2067 /**
2068  * gst_query_remove_nth_allocation_param:
2069  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
2070  * @index: position in the allocation param array to remove
2071  *
2072  * Remove the allocation param at @index of the allocation param array.
2073  *
2074  * Since: 1.2
2075  */
2076 void
2077 gst_query_remove_nth_allocation_param (GstQuery * query, guint index)
2078 {
2079   GArray *array;
2080   GstStructure *structure;
2081
2082   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
2083   g_return_if_fail (gst_query_is_writable (query));
2084
2085   structure = GST_QUERY_STRUCTURE (query);
2086   array =
2087       ensure_array (structure, GST_QUARK (ALLOCATOR), sizeof (AllocationParam),
2088       (GDestroyNotify) allocation_param_free);
2089   g_return_if_fail (index < array->len);
2090
2091   g_array_remove_index (array, index);
2092 }
2093
2094 /**
2095  * gst_query_new_scheduling:
2096  *
2097  * Constructs a new query object for querying the scheduling properties.
2098  *
2099  * Free-function: gst_query_unref
2100  *
2101  * Returns: (transfer full): a new #GstQuery
2102  */
2103 GstQuery *
2104 gst_query_new_scheduling (void)
2105 {
2106   GstQuery *query;
2107   GstStructure *structure;
2108
2109   structure = gst_structure_new_id (GST_QUARK (QUERY_SCHEDULING),
2110       GST_QUARK (FLAGS), GST_TYPE_SCHEDULING_FLAGS, 0,
2111       GST_QUARK (MINSIZE), G_TYPE_INT, 1,
2112       GST_QUARK (MAXSIZE), G_TYPE_INT, -1,
2113       GST_QUARK (ALIGN), G_TYPE_INT, 0, NULL);
2114   query = gst_query_new_custom (GST_QUERY_SCHEDULING, structure);
2115
2116   return query;
2117 }
2118
2119 /**
2120  * gst_query_set_scheduling:
2121  * @query: A valid #GstQuery of type GST_QUERY_SCHEDULING.
2122  * @flags: #GstSchedulingFlags
2123  * @minsize: the suggested minimum size of pull requests
2124  * @maxsize: the suggested maximum size of pull requests
2125  * @align: the suggested alignment of pull requests
2126  *
2127  * Set the scheduling properties.
2128  */
2129 void
2130 gst_query_set_scheduling (GstQuery * query, GstSchedulingFlags flags,
2131     gint minsize, gint maxsize, gint align)
2132 {
2133   GstStructure *structure;
2134
2135   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SCHEDULING);
2136   g_return_if_fail (gst_query_is_writable (query));
2137
2138   structure = GST_QUERY_STRUCTURE (query);
2139   gst_structure_id_set (structure,
2140       GST_QUARK (FLAGS), GST_TYPE_SCHEDULING_FLAGS, flags,
2141       GST_QUARK (MINSIZE), G_TYPE_INT, minsize,
2142       GST_QUARK (MAXSIZE), G_TYPE_INT, maxsize,
2143       GST_QUARK (ALIGN), G_TYPE_INT, align, NULL);
2144 }
2145
2146 /**
2147  * gst_query_parse_scheduling:
2148  * @query: A valid #GstQuery of type GST_QUERY_SCHEDULING.
2149  * @flags: (out) (allow-none): #GstSchedulingFlags
2150  * @minsize: (out) (allow-none): the suggested minimum size of pull requests
2151  * @maxsize: (out) (allow-none): the suggested maximum size of pull requests:
2152  * @align: (out) (allow-none): the suggested alignment of pull requests
2153  *
2154  * Set the scheduling properties.
2155  */
2156 void
2157 gst_query_parse_scheduling (GstQuery * query, GstSchedulingFlags * flags,
2158     gint * minsize, gint * maxsize, gint * align)
2159 {
2160   GstStructure *structure;
2161
2162   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SCHEDULING);
2163
2164   structure = GST_QUERY_STRUCTURE (query);
2165   gst_structure_id_get (structure,
2166       GST_QUARK (FLAGS), GST_TYPE_SCHEDULING_FLAGS, flags,
2167       GST_QUARK (MINSIZE), G_TYPE_INT, minsize,
2168       GST_QUARK (MAXSIZE), G_TYPE_INT, maxsize,
2169       GST_QUARK (ALIGN), G_TYPE_INT, align, NULL);
2170 }
2171
2172 /**
2173  * gst_query_add_scheduling_mode:
2174  * @query: a GST_QUERY_SCHEDULING type query #GstQuery
2175  * @mode: a #GstPadMode
2176  *
2177  * Add @mode as one of the supported scheduling modes to @query.
2178  */
2179 void
2180 gst_query_add_scheduling_mode (GstQuery * query, GstPadMode mode)
2181 {
2182   GstStructure *structure;
2183   GArray *array;
2184
2185   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SCHEDULING);
2186   g_return_if_fail (gst_query_is_writable (query));
2187
2188   structure = GST_QUERY_STRUCTURE (query);
2189   array =
2190       ensure_array (structure, GST_QUARK (MODES), sizeof (GstPadMode), NULL);
2191
2192   g_array_append_val (array, mode);
2193 }
2194
2195 /**
2196  * gst_query_get_n_scheduling_modes:
2197  * @query: a GST_QUERY_SCHEDULING type query #GstQuery
2198  *
2199  * Retrieve the number of values currently stored in the
2200  * scheduling mode array of the query's structure.
2201  *
2202  * Returns: the scheduling mode array size as a #guint.
2203  */
2204 guint
2205 gst_query_get_n_scheduling_modes (GstQuery * query)
2206 {
2207   GArray *array;
2208   GstStructure *structure;
2209
2210   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SCHEDULING, 0);
2211
2212   structure = GST_QUERY_STRUCTURE (query);
2213   array =
2214       ensure_array (structure, GST_QUARK (MODES), sizeof (GstPadMode), NULL);
2215
2216   return array->len;
2217 }
2218
2219 /**
2220  * gst_query_parse_nth_scheduling_mode:
2221  * @query: a GST_QUERY_SCHEDULING type query #GstQuery
2222  * @index: position in the scheduling modes array to read
2223  *
2224  * Parse an available query and get the scheduling mode
2225  * at @index of the scheduling modes array.
2226  *
2227  * Returns: a #GstPadMode of the scheduling mode at @index.
2228  */
2229 GstPadMode
2230 gst_query_parse_nth_scheduling_mode (GstQuery * query, guint index)
2231 {
2232   GstStructure *structure;
2233   GArray *array;
2234
2235   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SCHEDULING,
2236       GST_PAD_MODE_NONE);
2237
2238   structure = GST_QUERY_STRUCTURE (query);
2239   array =
2240       ensure_array (structure, GST_QUARK (MODES), sizeof (GstPadMode), NULL);
2241   g_return_val_if_fail (index < array->len, GST_PAD_MODE_NONE);
2242
2243   return g_array_index (array, GstPadMode, index);
2244 }
2245
2246 /**
2247  * gst_query_has_scheduling_mode:
2248  * @query: a GST_QUERY_SCHEDULING type query #GstQuery
2249  * @mode: the scheduling mode
2250  *
2251  * Check if @query has scheduling mode set.
2252  *
2253  * <note>
2254  *   <para>
2255  *     When checking if upstream supports pull mode, it is usually not
2256  *     enough to just check for GST_PAD_MODE_PULL with this function, you
2257  *     also want to check whether the scheduling flags returned by
2258  *     gst_query_parse_scheduling() have the seeking flag set (meaning
2259  *     random access is supported, not only sequential pulls).
2260  *   </para>
2261  * </note>
2262  *
2263  * Returns: %TRUE when @mode is in the list of scheduling modes.
2264  */
2265 gboolean
2266 gst_query_has_scheduling_mode (GstQuery * query, GstPadMode mode)
2267 {
2268   GstStructure *structure;
2269   GArray *array;
2270   guint i, len;
2271
2272   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SCHEDULING, FALSE);
2273
2274   structure = GST_QUERY_STRUCTURE (query);
2275   array =
2276       ensure_array (structure, GST_QUARK (MODES), sizeof (GstPadMode), NULL);
2277
2278   len = array->len;
2279   for (i = 0; i < len; i++) {
2280     if (mode == g_array_index (array, GstPadMode, i))
2281       return TRUE;
2282   }
2283   return FALSE;
2284 }
2285
2286 /**
2287  * gst_query_has_scheduling_mode_with_flags:
2288  * @query: a GST_QUERY_SCHEDULING type query #GstQuery
2289  * @mode: the scheduling mode
2290  * @flags: #GstSchedulingFlags
2291  *
2292  * Check if @query has scheduling mode set and @flags is set in
2293  * query scheduling flags.
2294  *
2295  * Returns: %TRUE when @mode is in the list of scheduling modes
2296  *    and @flags are compatible with query flags.
2297  */
2298 gboolean
2299 gst_query_has_scheduling_mode_with_flags (GstQuery * query, GstPadMode mode,
2300     GstSchedulingFlags flags)
2301 {
2302   GstSchedulingFlags sched_flags;
2303
2304   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SCHEDULING, FALSE);
2305
2306   gst_query_parse_scheduling (query, &sched_flags, NULL, NULL, NULL);
2307
2308   return ((flags & sched_flags) == flags) &&
2309       gst_query_has_scheduling_mode (query, mode);
2310 }
2311
2312 /**
2313  * gst_query_new_accept_caps:
2314  * @caps: a fixed #GstCaps
2315  *
2316  * Constructs a new query object for querying if @caps are accepted.
2317  *
2318  * Free-function: gst_query_unref
2319  *
2320  * Returns: (transfer full): a new #GstQuery
2321  */
2322 GstQuery *
2323 gst_query_new_accept_caps (GstCaps * caps)
2324 {
2325   GstQuery *query;
2326   GstStructure *structure;
2327
2328   g_return_val_if_fail (gst_caps_is_fixed (caps), NULL);
2329
2330   structure = gst_structure_new_id (GST_QUARK (QUERY_ACCEPT_CAPS),
2331       GST_QUARK (CAPS), GST_TYPE_CAPS, caps,
2332       GST_QUARK (RESULT), G_TYPE_BOOLEAN, FALSE, NULL);
2333   query = gst_query_new_custom (GST_QUERY_ACCEPT_CAPS, structure);
2334
2335   return query;
2336 }
2337
2338 /**
2339  * gst_query_parse_accept_caps:
2340  * @query: The query to parse
2341  * @caps: (out) (transfer none): A pointer to the caps
2342  *
2343  * Get the caps from @query. The caps remains valid as long as @query remains
2344  * valid.
2345  */
2346 void
2347 gst_query_parse_accept_caps (GstQuery * query, GstCaps ** caps)
2348 {
2349   GstStructure *structure;
2350
2351   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ACCEPT_CAPS);
2352   g_return_if_fail (caps != NULL);
2353
2354   structure = GST_QUERY_STRUCTURE (query);
2355   *caps = g_value_get_boxed (gst_structure_id_get_value (structure,
2356           GST_QUARK (CAPS)));
2357 }
2358
2359 /**
2360  * gst_query_set_accept_caps_result:
2361  * @query: a GST_QUERY_ACCEPT_CAPS type query #GstQuery
2362  * @result: the result to set
2363  *
2364  * Set @result as the result for the @query.
2365  */
2366 void
2367 gst_query_set_accept_caps_result (GstQuery * query, gboolean result)
2368 {
2369   GstStructure *structure;
2370
2371   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ACCEPT_CAPS);
2372   g_return_if_fail (gst_query_is_writable (query));
2373
2374   structure = GST_QUERY_STRUCTURE (query);
2375   gst_structure_id_set (structure,
2376       GST_QUARK (RESULT), G_TYPE_BOOLEAN, result, NULL);
2377 }
2378
2379 /**
2380  * gst_query_parse_accept_caps_result:
2381  * @query: a GST_QUERY_ACCEPT_CAPS type query #GstQuery
2382  * @result: location for the result
2383  *
2384  * Parse the result from @query and store in @result.
2385  */
2386 void
2387 gst_query_parse_accept_caps_result (GstQuery * query, gboolean * result)
2388 {
2389   GstStructure *structure;
2390
2391   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ACCEPT_CAPS);
2392
2393   structure = GST_QUERY_STRUCTURE (query);
2394   gst_structure_id_get (structure,
2395       GST_QUARK (RESULT), G_TYPE_BOOLEAN, result, NULL);
2396 }
2397
2398 /**
2399  * gst_query_new_caps:
2400  * @filter: a filter
2401  *
2402  * Constructs a new query object for querying the caps.
2403  *
2404  * The CAPS query should return the allowable caps for a pad in the context
2405  * of the element's state, its link to other elements, and the devices or files
2406  * it has opened. These caps must be a subset of the pad template caps. In the
2407  * NULL state with no links, the CAPS query should ideally return the same caps
2408  * as the pad template. In rare circumstances, an object property can affect
2409  * the caps returned by the CAPS query, but this is discouraged.
2410  *
2411  * For most filters, the caps returned by CAPS query is directly affected by the
2412  * allowed caps on other pads. For demuxers and decoders, the caps returned by
2413  * the srcpad's getcaps function is directly related to the stream data. Again,
2414  * the CAPS query should return the most specific caps it reasonably can, since this
2415  * helps with autoplugging.
2416  *
2417  * The @filter is used to restrict the result caps, only the caps matching
2418  * @filter should be returned from the CAPS query. Specifying a filter might
2419  * greatly reduce the amount of processing an element needs to do.
2420  *
2421  * Free-function: gst_query_unref
2422  *
2423  * Returns: (transfer full): a new #GstQuery
2424  */
2425 GstQuery *
2426 gst_query_new_caps (GstCaps * filter)
2427 {
2428   GstQuery *query;
2429   GstStructure *structure;
2430
2431   structure = gst_structure_new_id (GST_QUARK (QUERY_CAPS),
2432       GST_QUARK (FILTER), GST_TYPE_CAPS, filter,
2433       GST_QUARK (CAPS), GST_TYPE_CAPS, NULL, NULL);
2434   query = gst_query_new_custom (GST_QUERY_CAPS, structure);
2435
2436   return query;
2437 }
2438
2439 /**
2440  * gst_query_parse_caps:
2441  * @query: The query to parse
2442  * @filter: (out) (transfer none): A pointer to the caps filter
2443  *
2444  * Get the filter from the caps @query. The caps remains valid as long as
2445  * @query remains valid.
2446  */
2447 void
2448 gst_query_parse_caps (GstQuery * query, GstCaps ** filter)
2449 {
2450   GstStructure *structure;
2451
2452   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CAPS);
2453   g_return_if_fail (filter != NULL);
2454
2455   structure = GST_QUERY_STRUCTURE (query);
2456   *filter = g_value_get_boxed (gst_structure_id_get_value (structure,
2457           GST_QUARK (FILTER)));
2458 }
2459
2460 /**
2461  * gst_query_set_caps_result:
2462  * @query: The query to use
2463  * @caps: (in): A pointer to the caps
2464  *
2465  * Set the @caps result in @query.
2466  */
2467 void
2468 gst_query_set_caps_result (GstQuery * query, GstCaps * caps)
2469 {
2470   GstStructure *structure;
2471
2472   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CAPS);
2473   g_return_if_fail (gst_query_is_writable (query));
2474
2475   structure = GST_QUERY_STRUCTURE (query);
2476   gst_structure_id_set (structure, GST_QUARK (CAPS), GST_TYPE_CAPS, caps, NULL);
2477 }
2478
2479 /**
2480  * gst_query_parse_caps_result:
2481  * @query: The query to parse
2482  * @caps: (out) (transfer none): A pointer to the caps
2483  *
2484  * Get the caps result from @query. The caps remains valid as long as
2485  * @query remains valid.
2486  */
2487 void
2488 gst_query_parse_caps_result (GstQuery * query, GstCaps ** caps)
2489 {
2490   GstStructure *structure;
2491
2492   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CAPS);
2493   g_return_if_fail (caps != NULL);
2494
2495   structure = GST_QUERY_STRUCTURE (query);
2496   *caps = g_value_get_boxed (gst_structure_id_get_value (structure,
2497           GST_QUARK (CAPS)));
2498 }
2499
2500 #if 0
2501 void
2502 gst_query_intersect_caps_result (GstQuery * query, GstCaps * filter,
2503     GstCapsIntersectMode mode)
2504 {
2505   GstCaps *res, *caps = NULL;
2506
2507   gst_query_parse_caps_result (query, &caps);
2508   res = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
2509   gst_query_set_caps_result (query, res);
2510   gst_caps_unref (res);
2511 }
2512 #endif
2513
2514 /**
2515  * gst_query_new_drain:
2516  *
2517  * Constructs a new query object for querying the drain state.
2518  *
2519  * Free-function: gst_query_unref
2520  *
2521  * Returns: (transfer full): a new #GstQuery
2522  */
2523 GstQuery *
2524 gst_query_new_drain (void)
2525 {
2526   GstQuery *query;
2527   GstStructure *structure;
2528
2529   structure = gst_structure_new_id_empty (GST_QUARK (QUERY_DRAIN));
2530   query = gst_query_new_custom (GST_QUERY_DRAIN, structure);
2531
2532   return query;
2533 }
2534
2535 /**
2536  * gst_query_new_context:
2537  * @context_type: Context type to query
2538  *
2539  * Constructs a new query object for querying the pipeline-local context.
2540  *
2541  * Free-function: gst_query_unref
2542  *
2543  * Returns: (transfer full): a new #GstQuery
2544  *
2545  * Since: 1.2
2546  */
2547 GstQuery *
2548 gst_query_new_context (const gchar * context_type)
2549 {
2550   GstQuery *query;
2551   GstStructure *structure;
2552
2553   g_return_val_if_fail (context_type != NULL, NULL);
2554
2555   structure = gst_structure_new_id (GST_QUARK (QUERY_CONTEXT),
2556       GST_QUARK (CONTEXT_TYPE), G_TYPE_STRING, context_type, NULL);
2557   query = gst_query_new_custom (GST_QUERY_CONTEXT, structure);
2558
2559   return query;
2560 }
2561
2562 /**
2563  * gst_query_set_context:
2564  * @query: a #GstQuery with query type GST_QUERY_CONTEXT
2565  * @context: the requested #GstContext
2566  *
2567  * Answer a context query by setting the requested context.
2568  *
2569  * Since: 1.2
2570  */
2571 void
2572 gst_query_set_context (GstQuery * query, GstContext * context)
2573 {
2574   GstStructure *s;
2575   const gchar *context_type;
2576
2577   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CONTEXT);
2578
2579   gst_query_parse_context_type (query, &context_type);
2580   g_return_if_fail (strcmp (gst_context_get_context_type (context),
2581           context_type) == 0);
2582
2583   s = GST_QUERY_STRUCTURE (query);
2584
2585   gst_structure_id_set (s,
2586       GST_QUARK (CONTEXT), GST_TYPE_CONTEXT, context, NULL);
2587 }
2588
2589 /**
2590  * gst_query_parse_context:
2591  * @query: The query to parse
2592  * @context: (out) (transfer none): A pointer to store the #GstContext
2593  *
2594  * Get the context from the context @query. The context remains valid as long as
2595  * @query remains valid.
2596  *
2597  * Since: 1.2
2598  */
2599 void
2600 gst_query_parse_context (GstQuery * query, GstContext ** context)
2601 {
2602   GstStructure *structure;
2603   const GValue *v;
2604
2605   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CONTEXT);
2606   g_return_if_fail (context != NULL);
2607
2608   structure = GST_QUERY_STRUCTURE (query);
2609   v = gst_structure_id_get_value (structure, GST_QUARK (CONTEXT));
2610   if (v)
2611     *context = g_value_get_boxed (v);
2612   else
2613     *context = NULL;
2614 }
2615
2616 /**
2617  * gst_query_parse_context_type:
2618  * @query: a GST_QUERY_CONTEXT type query
2619  * @context_type: (out) (transfer none) (allow-none): the context type, or %NULL
2620  *
2621  * Parse a context type from an existing GST_QUERY_CONTEXT query.
2622  *
2623  * Returns: a #gboolean indicating if the parsing succeeded.
2624  *
2625  * Since: 1.2
2626  */
2627 gboolean
2628 gst_query_parse_context_type (GstQuery * query, const gchar ** context_type)
2629 {
2630   GstStructure *structure;
2631   const GValue *value;
2632
2633   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CONTEXT, FALSE);
2634
2635   structure = GST_QUERY_STRUCTURE (query);
2636
2637   if (context_type) {
2638     value = gst_structure_id_get_value (structure, GST_QUARK (CONTEXT_TYPE));
2639     *context_type = g_value_get_string (value);
2640   }
2641
2642   return TRUE;
2643 }