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