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