query: use more generic structure for meta params
[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   GstStructure *params;
1665 } AllocationMeta;
1666
1667 static void
1668 allocation_meta_free (AllocationMeta * am)
1669 {
1670   if (am->params)
1671     gst_structure_free (am->params);
1672 }
1673
1674 /**
1675  * gst_query_add_allocation_meta:
1676  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1677  * @api: the metadata API
1678  * @params: (transfer full) (allow-none): API specific parameters
1679  *
1680  * Add @api with @params as one of the supported metadata API to @query.
1681  */
1682 void
1683 gst_query_add_allocation_meta (GstQuery * query, GType api,
1684     GstStructure * params)
1685 {
1686   GArray *array;
1687   GstStructure *structure;
1688   AllocationMeta am;
1689
1690   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
1691   g_return_if_fail (api != 0);
1692   g_return_if_fail (gst_query_is_writable (query));
1693
1694   structure = GST_QUERY_STRUCTURE (query);
1695   array =
1696       ensure_array (structure, GST_QUARK (META), sizeof (AllocationMeta),
1697       (GDestroyNotify) allocation_meta_free);
1698
1699   am.api = api;
1700   am.params = params;
1701
1702   g_array_append_val (array, am);
1703 }
1704
1705 /**
1706  * gst_query_get_n_allocation_metas:
1707  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1708  *
1709  * Retrieve the number of values currently stored in the
1710  * meta API array of the query's structure.
1711  *
1712  * Returns: the metadata API array size as a #guint.
1713  */
1714 guint
1715 gst_query_get_n_allocation_metas (GstQuery * query)
1716 {
1717   GArray *array;
1718   GstStructure *structure;
1719
1720   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION, 0);
1721
1722   structure = GST_QUERY_STRUCTURE (query);
1723   array =
1724       ensure_array (structure, GST_QUARK (META), sizeof (AllocationMeta),
1725       (GDestroyNotify) allocation_meta_free);
1726
1727   return array->len;
1728 }
1729
1730 /**
1731  * gst_query_parse_nth_allocation_meta:
1732  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1733  * @index: position in the metadata API array to read
1734  * @params: (out) (allow-none): API specific flags
1735  *
1736  * Parse an available query and get the metadata API
1737  * at @index of the metadata API array.
1738  *
1739  * Returns: a #GType of the metadata API at @index.
1740  */
1741 GType
1742 gst_query_parse_nth_allocation_meta (GstQuery * query, guint index,
1743     const GstStructure ** params)
1744 {
1745   GArray *array;
1746   GstStructure *structure;
1747   AllocationMeta *am;
1748
1749   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION, 0);
1750
1751   structure = GST_QUERY_STRUCTURE (query);
1752   array =
1753       ensure_array (structure, GST_QUARK (META), sizeof (AllocationMeta),
1754       (GDestroyNotify) allocation_meta_free);
1755
1756   g_return_val_if_fail (index < array->len, 0);
1757
1758   am = &g_array_index (array, AllocationMeta, index);
1759
1760   if (params)
1761     *params = am->params;
1762
1763   return am->api;
1764 }
1765
1766 /**
1767  * gst_query_remove_nth_allocation_meta:
1768  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1769  * @index: position in the metadata API array to remove
1770  *
1771  * Remove the metadata API at @index of the metadata API array.
1772  */
1773 void
1774 gst_query_remove_nth_allocation_meta (GstQuery * query, guint index)
1775 {
1776   GArray *array;
1777   GstStructure *structure;
1778
1779   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
1780   g_return_if_fail (gst_query_is_writable (query));
1781
1782   structure = GST_QUERY_STRUCTURE (query);
1783   array =
1784       ensure_array (structure, GST_QUARK (META), sizeof (AllocationMeta),
1785       (GDestroyNotify) allocation_meta_free);
1786   g_return_if_fail (index < array->len);
1787
1788   g_array_remove_index (array, index);
1789 }
1790
1791 /**
1792  * gst_query_find_allocation_meta:
1793  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1794  * @api: the metadata API
1795  * @index: (out) (allow-none): the index
1796  *
1797  * Check if @query has metadata @api set. When this function returns TRUE,
1798  * @index will contain the index where the requested API and the flags can be
1799  * found.
1800  *
1801  * Returns: TRUE when @api is in the list of metadata.
1802  */
1803 gboolean
1804 gst_query_find_allocation_meta (GstQuery * query, GType api, guint * index)
1805 {
1806   GArray *array;
1807   GstStructure *structure;
1808   guint i, len;
1809
1810   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION, FALSE);
1811   g_return_val_if_fail (api != 0, FALSE);
1812
1813   structure = GST_QUERY_STRUCTURE (query);
1814   array =
1815       ensure_array (structure, GST_QUARK (META), sizeof (AllocationMeta),
1816       (GDestroyNotify) allocation_meta_free);
1817
1818   len = array->len;
1819   for (i = 0; i < len; i++) {
1820     AllocationMeta *am = &g_array_index (array, AllocationMeta, i);
1821     if (am->api == api) {
1822       if (index)
1823         *index = i;
1824       return TRUE;
1825     }
1826   }
1827   return FALSE;
1828 }
1829
1830 typedef struct
1831 {
1832   GstAllocator *allocator;
1833   GstAllocationParams params;
1834 } AllocationParam;
1835
1836 static void
1837 allocation_param_free (AllocationParam * ap)
1838 {
1839   if (ap->allocator)
1840     gst_allocator_unref (ap->allocator);
1841 }
1842
1843 /**
1844  * gst_query_add_allocation_param:
1845  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1846  * @allocator: (transfer none) (allow-none): the memory allocator
1847  * @params: (transfer none) (allow-none): a #GstAllocationParams
1848  *
1849  * Add @allocator and its @params as a supported memory allocator.
1850  */
1851 void
1852 gst_query_add_allocation_param (GstQuery * query, GstAllocator * allocator,
1853     const GstAllocationParams * params)
1854 {
1855   GArray *array;
1856   GstStructure *structure;
1857   AllocationParam ap;
1858
1859   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
1860   g_return_if_fail (gst_query_is_writable (query));
1861   g_return_if_fail (allocator != NULL || params != NULL);
1862
1863   structure = GST_QUERY_STRUCTURE (query);
1864   array = ensure_array (structure, GST_QUARK (ALLOCATOR),
1865       sizeof (AllocationParam), (GDestroyNotify) allocation_param_free);
1866
1867   if ((ap.allocator = allocator))
1868     gst_allocator_ref (allocator);
1869   if (params)
1870     ap.params = *params;
1871   else
1872     gst_allocation_params_init (&ap.params);
1873
1874   g_array_append_val (array, ap);
1875 }
1876
1877 /**
1878  * gst_query_get_n_allocation_params:
1879  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1880  *
1881  * Retrieve the number of values currently stored in the
1882  * allocator params array of the query's structure.
1883  *
1884  * If no memory allocator is specified, the downstream element can handle
1885  * the default memory allocator.
1886  *
1887  * Returns: the allocator array size as a #guint.
1888  */
1889 guint
1890 gst_query_get_n_allocation_params (GstQuery * query)
1891 {
1892   GArray *array;
1893   GstStructure *structure;
1894
1895   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION, 0);
1896
1897   structure = GST_QUERY_STRUCTURE (query);
1898   array = ensure_array (structure, GST_QUARK (ALLOCATOR),
1899       sizeof (AllocationParam), (GDestroyNotify) allocation_param_free);
1900
1901   return array->len;
1902 }
1903
1904 /**
1905  * gst_query_parse_nth_allocation_param:
1906  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1907  * @index: position in the allocator array to read
1908  * @allocator: (out) (transfer none) (allow-none): variable to hold the result
1909  * @params: (out) (allow-none): parameters for the allocator
1910  *
1911  * Parse an available query and get the alloctor and its params
1912  * at @index of the allocator array.
1913  */
1914 void
1915 gst_query_parse_nth_allocation_param (GstQuery * query, guint index,
1916     GstAllocator ** allocator, GstAllocationParams * params)
1917 {
1918   GArray *array;
1919   GstStructure *structure;
1920   AllocationParam *ap;
1921
1922   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
1923
1924   structure = GST_QUERY_STRUCTURE (query);
1925   array = ensure_array (structure, GST_QUARK (ALLOCATOR),
1926       sizeof (AllocationParam), (GDestroyNotify) allocation_param_free);
1927   g_return_if_fail (index < array->len);
1928
1929   ap = &g_array_index (array, AllocationParam, index);
1930
1931   if (allocator)
1932     if ((*allocator = ap->allocator))
1933       gst_allocator_ref (*allocator);
1934   if (params)
1935     *params = ap->params;
1936 }
1937
1938 /**
1939  * gst_query_set_nth_allocation_param:
1940  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1941  * @index: position in the allocator array to set
1942  * @allocator: (transfer none) (allow-none): new allocator to set
1943  * @params: (transfer none) (allow-none): parameters for the allocator
1944  *
1945  * Parse an available query and get the alloctor and its params
1946  * at @index of the allocator array.
1947  */
1948 void
1949 gst_query_set_nth_allocation_param (GstQuery * query, guint index,
1950     GstAllocator * allocator, const GstAllocationParams * params)
1951 {
1952   GArray *array;
1953   GstStructure *structure;
1954   AllocationParam *old, ap;
1955
1956   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
1957
1958   structure = GST_QUERY_STRUCTURE (query);
1959   array = ensure_array (structure, GST_QUARK (ALLOCATOR),
1960       sizeof (AllocationParam), (GDestroyNotify) allocation_param_free);
1961   g_return_if_fail (index < array->len);
1962
1963   old = &g_array_index (array, AllocationParam, index);
1964   allocation_param_free (old);
1965
1966   if ((ap.allocator = allocator))
1967     gst_allocator_ref (allocator);
1968   if (params)
1969     ap.params = *params;
1970   else
1971     gst_allocation_params_init (&ap.params);
1972
1973   g_array_index (array, AllocationParam, index) = ap;
1974 }
1975
1976 /**
1977  * gst_query_new_scheduling:
1978  *
1979  * Constructs a new query object for querying the scheduling properties.
1980  *
1981  * Free-function: gst_query_unref
1982  *
1983  * Returns: (transfer full): a new #GstQuery
1984  */
1985 GstQuery *
1986 gst_query_new_scheduling (void)
1987 {
1988   GstQuery *query;
1989   GstStructure *structure;
1990
1991   structure = gst_structure_new_id (GST_QUARK (QUERY_SCHEDULING),
1992       GST_QUARK (FLAGS), GST_TYPE_SCHEDULING_FLAGS, 0,
1993       GST_QUARK (MINSIZE), G_TYPE_INT, 1,
1994       GST_QUARK (MAXSIZE), G_TYPE_INT, -1,
1995       GST_QUARK (ALIGN), G_TYPE_INT, 0, NULL);
1996   query = gst_query_new_custom (GST_QUERY_SCHEDULING, structure);
1997
1998   return query;
1999 }
2000
2001 /**
2002  * gst_query_set_scheduling:
2003  * @query: A valid #GstQuery of type GST_QUERY_SCHEDULING.
2004  * @flags: #GstSchedulingFlags
2005  * @minsize: the suggested minimum size of pull requests
2006  * @maxsize: the suggested maximum size of pull requests
2007  * @align: the suggested alignment of pull requests
2008  *
2009  * Set the scheduling properties.
2010  */
2011 void
2012 gst_query_set_scheduling (GstQuery * query, GstSchedulingFlags flags,
2013     gint minsize, gint maxsize, gint align)
2014 {
2015   GstStructure *structure;
2016
2017   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SCHEDULING);
2018   g_return_if_fail (gst_query_is_writable (query));
2019
2020   structure = GST_QUERY_STRUCTURE (query);
2021   gst_structure_id_set (structure,
2022       GST_QUARK (FLAGS), GST_TYPE_SCHEDULING_FLAGS, flags,
2023       GST_QUARK (MINSIZE), G_TYPE_INT, minsize,
2024       GST_QUARK (MAXSIZE), G_TYPE_INT, maxsize,
2025       GST_QUARK (ALIGN), G_TYPE_INT, align, NULL);
2026 }
2027
2028 /**
2029  * gst_query_parse_scheduling:
2030  * @query: A valid #GstQuery of type GST_QUERY_SCHEDULING.
2031  * @flags: (out) (allow-none): #GstSchedulingFlags
2032  * @minsize: (out) (allow-none): the suggested minimum size of pull requests
2033  * @maxsize: (out) (allow-none): the suggested maximum size of pull requests:
2034  * @align: (out) (allow-none): the suggested alignment of pull requests
2035  *
2036  * Set the scheduling properties.
2037  */
2038 void
2039 gst_query_parse_scheduling (GstQuery * query, GstSchedulingFlags * flags,
2040     gint * minsize, gint * maxsize, gint * align)
2041 {
2042   GstStructure *structure;
2043
2044   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SCHEDULING);
2045
2046   structure = GST_QUERY_STRUCTURE (query);
2047   gst_structure_id_get (structure,
2048       GST_QUARK (FLAGS), GST_TYPE_SCHEDULING_FLAGS, flags,
2049       GST_QUARK (MINSIZE), G_TYPE_INT, minsize,
2050       GST_QUARK (MAXSIZE), G_TYPE_INT, maxsize,
2051       GST_QUARK (ALIGN), G_TYPE_INT, align, NULL);
2052 }
2053
2054 /**
2055  * gst_query_add_scheduling_mode:
2056  * @query: a GST_QUERY_SCHEDULING type query #GstQuery
2057  * @mode: a #GstPadMode
2058  *
2059  * Add @mode as aone of the supported scheduling modes to @query.
2060  */
2061 void
2062 gst_query_add_scheduling_mode (GstQuery * query, GstPadMode mode)
2063 {
2064   GstStructure *structure;
2065   GArray *array;
2066
2067   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SCHEDULING);
2068   g_return_if_fail (gst_query_is_writable (query));
2069
2070   structure = GST_QUERY_STRUCTURE (query);
2071   array =
2072       ensure_array (structure, GST_QUARK (MODES), sizeof (GstPadMode), NULL);
2073
2074   g_array_append_val (array, mode);
2075 }
2076
2077 /**
2078  * gst_query_get_n_scheduling_modes:
2079  * @query: a GST_QUERY_SCHEDULING type query #GstQuery
2080  *
2081  * Retrieve the number of values currently stored in the
2082  * scheduling mode array of the query's structure.
2083  *
2084  * Returns: the scheduling mode array size as a #guint.
2085  */
2086 guint
2087 gst_query_get_n_scheduling_modes (GstQuery * query)
2088 {
2089   GArray *array;
2090   GstStructure *structure;
2091
2092   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SCHEDULING, 0);
2093
2094   structure = GST_QUERY_STRUCTURE (query);
2095   array =
2096       ensure_array (structure, GST_QUARK (MODES), sizeof (GstPadMode), NULL);
2097
2098   return array->len;
2099 }
2100
2101 /**
2102  * gst_query_parse_nth_scheduling_mode:
2103  * @query: a GST_QUERY_SCHEDULING type query #GstQuery
2104  * @index: position in the scheduling modes array to read
2105  *
2106  * Parse an available query and get the scheduling mode
2107  * at @index of the scheduling modes array.
2108  *
2109  * Returns: a #GstPadMode of the scheduling mode at @index.
2110  */
2111 GstPadMode
2112 gst_query_parse_nth_scheduling_mode (GstQuery * query, guint index)
2113 {
2114   GstStructure *structure;
2115   GArray *array;
2116
2117   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SCHEDULING,
2118       GST_PAD_MODE_NONE);
2119
2120   structure = GST_QUERY_STRUCTURE (query);
2121   array =
2122       ensure_array (structure, GST_QUARK (MODES), sizeof (GstPadMode), NULL);
2123   g_return_val_if_fail (index < array->len, GST_PAD_MODE_NONE);
2124
2125   return g_array_index (array, GstPadMode, index);
2126 }
2127
2128 /**
2129  * gst_query_has_scheduling_mode:
2130  * @query: a GST_QUERY_SCHEDULING type query #GstQuery
2131  * @mode: the scheduling mode
2132  *
2133  * Check if @query has scheduling mode set.
2134  *
2135  * Returns: TRUE when @mode is in the list of scheduling modes.
2136  */
2137 gboolean
2138 gst_query_has_scheduling_mode (GstQuery * query, GstPadMode mode)
2139 {
2140   GstStructure *structure;
2141   GArray *array;
2142   guint i, len;
2143
2144   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SCHEDULING, FALSE);
2145
2146   structure = GST_QUERY_STRUCTURE (query);
2147   array =
2148       ensure_array (structure, GST_QUARK (MODES), sizeof (GstPadMode), NULL);
2149
2150   len = array->len;
2151   for (i = 0; i < len; i++) {
2152     if (mode == g_array_index (array, GstPadMode, i))
2153       return TRUE;
2154   }
2155   return FALSE;
2156 }
2157
2158 /**
2159  * gst_query_new_accept_caps:
2160  * @caps: a fixed #GstCaps
2161  *
2162  * Constructs a new query object for querying if @caps are accepted.
2163  *
2164  * Free-function: gst_query_unref
2165  *
2166  * Returns: (transfer full): a new #GstQuery
2167  */
2168 GstQuery *
2169 gst_query_new_accept_caps (GstCaps * caps)
2170 {
2171   GstQuery *query;
2172   GstStructure *structure;
2173
2174   g_return_val_if_fail (gst_caps_is_fixed (caps), NULL);
2175
2176   structure = gst_structure_new_id (GST_QUARK (QUERY_ACCEPT_CAPS),
2177       GST_QUARK (CAPS), GST_TYPE_CAPS, caps,
2178       GST_QUARK (RESULT), G_TYPE_BOOLEAN, FALSE, NULL);
2179   query = gst_query_new_custom (GST_QUERY_ACCEPT_CAPS, structure);
2180
2181   return query;
2182 }
2183
2184 /**
2185  * gst_query_parse_accept_caps:
2186  * @query: The query to parse
2187  * @caps: (out): A pointer to the caps
2188  *
2189  * Get the caps from @query. The caps remains valid as long as @query remains
2190  * valid.
2191  */
2192 void
2193 gst_query_parse_accept_caps (GstQuery * query, GstCaps ** caps)
2194 {
2195   GstStructure *structure;
2196
2197   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ACCEPT_CAPS);
2198   g_return_if_fail (caps != NULL);
2199
2200   structure = GST_QUERY_STRUCTURE (query);
2201   *caps = g_value_get_boxed (gst_structure_id_get_value (structure,
2202           GST_QUARK (CAPS)));
2203 }
2204
2205 /**
2206  * gst_query_set_accept_caps_result:
2207  * @query: a GST_QUERY_ACCEPT_CAPS type query #GstQuery
2208  * @result: the result to set
2209  *
2210  * Set @result as the result for the @query.
2211  */
2212 void
2213 gst_query_set_accept_caps_result (GstQuery * query, gboolean result)
2214 {
2215   GstStructure *structure;
2216
2217   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ACCEPT_CAPS);
2218   g_return_if_fail (gst_query_is_writable (query));
2219
2220   structure = GST_QUERY_STRUCTURE (query);
2221   gst_structure_id_set (structure,
2222       GST_QUARK (RESULT), G_TYPE_BOOLEAN, result, NULL);
2223 }
2224
2225 /**
2226  * gst_query_parse_accept_caps_result:
2227  * @query: a GST_QUERY_ACCEPT_CAPS type query #GstQuery
2228  * @result: location for the result
2229  *
2230  * Parse the result from @query and store in @result.
2231  */
2232 void
2233 gst_query_parse_accept_caps_result (GstQuery * query, gboolean * result)
2234 {
2235   GstStructure *structure;
2236
2237   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ACCEPT_CAPS);
2238
2239   structure = GST_QUERY_STRUCTURE (query);
2240   gst_structure_id_get (structure,
2241       GST_QUARK (RESULT), G_TYPE_BOOLEAN, result, NULL);
2242 }
2243
2244 /**
2245  * gst_query_new_caps:
2246  * @filter: a filter
2247  *
2248  * Constructs a new query object for querying the caps.
2249  *
2250  * The CAPS query should return the allowable caps for a pad in the context
2251  * of the element's state, its link to other elements, and the devices or files
2252  * it has opened. These caps must be a subset of the pad template caps. In the
2253  * NULL state with no links, the CAPS query should ideally return the same caps
2254  * as the pad template. In rare circumstances, an object property can affect
2255  * the caps returned by the CAPS query, but this is discouraged.
2256  *
2257  * For most filters, the caps returned by CAPS query is directly affected by the
2258  * allowed caps on other pads. For demuxers and decoders, the caps returned by
2259  * the srcpad's getcaps function is directly related to the stream data. Again,
2260  * the CAPS query should return the most specific caps it reasonably can, since this
2261  * helps with autoplugging.
2262  *
2263  * The @filter is used to restrict the result caps, only the caps matching
2264  * @filter should be returned from the CAPS query. Specifying a filter might
2265  * greatly reduce the amount of processing an element needs to do.
2266  *
2267  * Free-function: gst_query_unref
2268  *
2269  * Returns: (transfer full): a new #GstQuery
2270  */
2271 GstQuery *
2272 gst_query_new_caps (GstCaps * filter)
2273 {
2274   GstQuery *query;
2275   GstStructure *structure;
2276
2277   structure = gst_structure_new_id (GST_QUARK (QUERY_CAPS),
2278       GST_QUARK (FILTER), GST_TYPE_CAPS, filter,
2279       GST_QUARK (CAPS), GST_TYPE_CAPS, NULL, NULL);
2280   query = gst_query_new_custom (GST_QUERY_CAPS, structure);
2281
2282   return query;
2283 }
2284
2285 /**
2286  * gst_query_parse_caps:
2287  * @query: The query to parse
2288  * @filter: (out): A pointer to the caps filter
2289  *
2290  * Get the filter from the caps @query. The caps remains valid as long as
2291  * @query remains valid.
2292  */
2293 void
2294 gst_query_parse_caps (GstQuery * query, GstCaps ** filter)
2295 {
2296   GstStructure *structure;
2297
2298   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CAPS);
2299   g_return_if_fail (filter != NULL);
2300
2301   structure = GST_QUERY_STRUCTURE (query);
2302   *filter = g_value_get_boxed (gst_structure_id_get_value (structure,
2303           GST_QUARK (FILTER)));
2304 }
2305
2306 /**
2307  * gst_query_set_caps_result:
2308  * @query: The query to use
2309  * @caps: (in): A pointer to the caps
2310  *
2311  * Set the @caps result in @query.
2312  */
2313 void
2314 gst_query_set_caps_result (GstQuery * query, GstCaps * caps)
2315 {
2316   GstStructure *structure;
2317
2318   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CAPS);
2319   g_return_if_fail (gst_query_is_writable (query));
2320
2321   structure = GST_QUERY_STRUCTURE (query);
2322   gst_structure_id_set (structure, GST_QUARK (CAPS), GST_TYPE_CAPS, caps, NULL);
2323 }
2324
2325 /**
2326  * gst_query_parse_caps_result:
2327  * @query: The query to parse
2328  * @caps: (out): A pointer to the caps
2329  *
2330  * Get the caps result from @query. The caps remains valid as long as
2331  * @query remains valid.
2332  */
2333 void
2334 gst_query_parse_caps_result (GstQuery * query, GstCaps ** caps)
2335 {
2336   GstStructure *structure;
2337
2338   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CAPS);
2339   g_return_if_fail (caps != NULL);
2340
2341   structure = GST_QUERY_STRUCTURE (query);
2342   *caps = g_value_get_boxed (gst_structure_id_get_value (structure,
2343           GST_QUARK (CAPS)));
2344 }
2345
2346 #if 0
2347 void
2348 gst_query_intersect_caps_result (GstQuery * query, GstCaps * filter,
2349     GstCapsIntersectMode mode)
2350 {
2351   GstCaps *res, *caps = NULL;
2352
2353   gst_query_parse_caps_result (query, &caps);
2354   res = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
2355   gst_query_set_caps_result (query, res);
2356   gst_caps_unref (res);
2357 }
2358 #endif
2359
2360 /**
2361  * gst_query_new_drain:
2362  *
2363  * Constructs a new query object for querying the drain state.
2364  *
2365  * Free-function: gst_query_unref
2366  *
2367  * Returns: (transfer full): a new #GstQuery
2368  */
2369 GstQuery *
2370 gst_query_new_drain (void)
2371 {
2372   GstQuery *query;
2373   GstStructure *structure;
2374
2375   structure = gst_structure_new_id_empty (GST_QUARK (QUERY_DRAIN));
2376   query = gst_query_new_custom (GST_QUERY_DRAIN, structure);
2377
2378   return query;
2379 }