query: adjust test logic for scheduling mode with flagS
[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 GstQuery *
391 gst_query_new_latency (void)
392 {
393   GstQuery *query;
394   GstStructure *structure;
395
396   structure = gst_structure_new_id (GST_QUARK (QUERY_LATENCY),
397       GST_QUARK (LIVE), G_TYPE_BOOLEAN, FALSE,
398       GST_QUARK (MIN_LATENCY), G_TYPE_UINT64, G_GUINT64_CONSTANT (0),
399       GST_QUARK (MAX_LATENCY), G_TYPE_UINT64, G_GUINT64_CONSTANT (-1), NULL);
400
401   query = gst_query_new_custom (GST_QUERY_LATENCY, structure);
402
403   return query;
404 }
405
406 /**
407  * gst_query_set_latency:
408  * @query: a #GstQuery
409  * @live: if there is a live element upstream
410  * @min_latency: the minimal latency of the upstream elements
411  * @max_latency: the maximal latency of the upstream elements
412  *
413  * Answer a latency query by setting the requested values in the given format.
414  */
415 void
416 gst_query_set_latency (GstQuery * query, gboolean live,
417     GstClockTime min_latency, GstClockTime max_latency)
418 {
419   GstStructure *structure;
420
421   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_LATENCY);
422
423   structure = GST_QUERY_STRUCTURE (query);
424   gst_structure_id_set (structure,
425       GST_QUARK (LIVE), G_TYPE_BOOLEAN, live,
426       GST_QUARK (MIN_LATENCY), G_TYPE_UINT64, min_latency,
427       GST_QUARK (MAX_LATENCY), G_TYPE_UINT64, max_latency, NULL);
428 }
429
430 /**
431  * gst_query_parse_latency:
432  * @query: a #GstQuery
433  * @live: (out) (allow-none): storage for live or NULL
434  * @min_latency: (out) (allow-none): the storage for the min latency or NULL
435  * @max_latency: (out) (allow-none): the storage for the max latency or NULL
436  *
437  * Parse a latency query answer.
438  */
439 void
440 gst_query_parse_latency (GstQuery * query, gboolean * live,
441     GstClockTime * min_latency, GstClockTime * max_latency)
442 {
443   GstStructure *structure;
444
445   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_LATENCY);
446
447   structure = GST_QUERY_STRUCTURE (query);
448   if (live)
449     *live =
450         g_value_get_boolean (gst_structure_id_get_value (structure,
451             GST_QUARK (LIVE)));
452   if (min_latency)
453     *min_latency = g_value_get_uint64 (gst_structure_id_get_value (structure,
454             GST_QUARK (MIN_LATENCY)));
455   if (max_latency)
456     *max_latency = g_value_get_uint64 (gst_structure_id_get_value (structure,
457             GST_QUARK (MAX_LATENCY)));
458 }
459
460 /**
461  * gst_query_new_convert:
462  * @src_format: the source #GstFormat for the new query
463  * @value: the value to convert
464  * @dest_format: the target #GstFormat
465  *
466  * Constructs a new convert query object. Use gst_query_unref()
467  * when done with it. A convert query is used to ask for a conversion between
468  * one format and another.
469  *
470  * Free-function: gst_query_unref
471  *
472  * Returns: (transfer full): a #GstQuery
473  */
474 GstQuery *
475 gst_query_new_convert (GstFormat src_format, gint64 value,
476     GstFormat dest_format)
477 {
478   GstQuery *query;
479   GstStructure *structure;
480
481   structure = gst_structure_new_id (GST_QUARK (QUERY_CONVERT),
482       GST_QUARK (SRC_FORMAT), GST_TYPE_FORMAT, src_format,
483       GST_QUARK (SRC_VALUE), G_TYPE_INT64, value,
484       GST_QUARK (DEST_FORMAT), GST_TYPE_FORMAT, dest_format,
485       GST_QUARK (DEST_VALUE), G_TYPE_INT64, G_GINT64_CONSTANT (-1), NULL);
486
487   query = gst_query_new_custom (GST_QUERY_CONVERT, structure);
488
489   return query;
490 }
491
492 /**
493  * gst_query_set_convert:
494  * @query: a #GstQuery
495  * @src_format: the source #GstFormat
496  * @src_value: the source value
497  * @dest_format: the destination #GstFormat
498  * @dest_value: the destination value
499  *
500  * Answer a convert query by setting the requested values.
501  */
502 void
503 gst_query_set_convert (GstQuery * query, GstFormat src_format, gint64 src_value,
504     GstFormat dest_format, gint64 dest_value)
505 {
506   GstStructure *structure;
507
508   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CONVERT);
509
510   structure = GST_QUERY_STRUCTURE (query);
511   gst_structure_id_set (structure,
512       GST_QUARK (SRC_FORMAT), GST_TYPE_FORMAT, src_format,
513       GST_QUARK (SRC_VALUE), G_TYPE_INT64, src_value,
514       GST_QUARK (DEST_FORMAT), GST_TYPE_FORMAT, dest_format,
515       GST_QUARK (DEST_VALUE), G_TYPE_INT64, dest_value, NULL);
516 }
517
518 /**
519  * gst_query_parse_convert:
520  * @query: a #GstQuery
521  * @src_format: (out) (allow-none): the storage for the #GstFormat of the
522  *     source value, or NULL
523  * @src_value: (out) (allow-none): the storage for the source value, or NULL
524  * @dest_format: (out) (allow-none): the storage for the #GstFormat of the
525  *     destination value, or NULL
526  * @dest_value: (out) (allow-none): the storage for the destination value,
527  *     or NULL
528  *
529  * Parse a convert query answer. Any of @src_format, @src_value, @dest_format,
530  * and @dest_value may be NULL, in which case that value is omitted.
531  */
532 void
533 gst_query_parse_convert (GstQuery * query, GstFormat * src_format,
534     gint64 * src_value, GstFormat * dest_format, gint64 * dest_value)
535 {
536   GstStructure *structure;
537
538   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CONVERT);
539
540   structure = GST_QUERY_STRUCTURE (query);
541   if (src_format)
542     *src_format =
543         (GstFormat) g_value_get_enum (gst_structure_id_get_value (structure,
544             GST_QUARK (SRC_FORMAT)));
545   if (src_value)
546     *src_value = g_value_get_int64 (gst_structure_id_get_value (structure,
547             GST_QUARK (SRC_VALUE)));
548   if (dest_format)
549     *dest_format =
550         (GstFormat) g_value_get_enum (gst_structure_id_get_value (structure,
551             GST_QUARK (DEST_FORMAT)));
552   if (dest_value)
553     *dest_value = g_value_get_int64 (gst_structure_id_get_value (structure,
554             GST_QUARK (DEST_VALUE)));
555 }
556
557 /**
558  * gst_query_new_segment:
559  * @format: the #GstFormat for the new query
560  *
561  * Constructs a new segment query object. Use gst_query_unref()
562  * when done with it. A segment query is used to discover information about the
563  * currently configured segment for playback.
564  *
565  * Free-function: gst_query_unref
566  *
567  * Returns: (transfer full): a new #GstQuery
568  */
569 GstQuery *
570 gst_query_new_segment (GstFormat format)
571 {
572   GstQuery *query;
573   GstStructure *structure;
574
575   structure = gst_structure_new_id (GST_QUARK (QUERY_SEGMENT),
576       GST_QUARK (RATE), G_TYPE_DOUBLE, (gdouble) 0.0,
577       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
578       GST_QUARK (START_VALUE), G_TYPE_INT64, G_GINT64_CONSTANT (-1),
579       GST_QUARK (STOP_VALUE), G_TYPE_INT64, G_GINT64_CONSTANT (-1), NULL);
580
581   query = gst_query_new_custom (GST_QUERY_SEGMENT, structure);
582
583   return query;
584 }
585
586 /**
587  * gst_query_set_segment:
588  * @query: a #GstQuery
589  * @rate: the rate of the segment
590  * @format: the #GstFormat of the segment values (@start_value and @stop_value)
591  * @start_value: the start value
592  * @stop_value: the stop value
593  *
594  * Answer a segment query by setting the requested values. The normal
595  * playback segment of a pipeline is 0 to duration at the default rate of
596  * 1.0. If a seek was performed on the pipeline to play a different
597  * segment, this query will return the range specified in the last seek.
598  *
599  * @start_value and @stop_value will respectively contain the configured
600  * playback range start and stop values expressed in @format.
601  * The values are always between 0 and the duration of the media and
602  * @start_value <= @stop_value. @rate will contain the playback rate. For
603  * negative rates, playback will actually happen from @stop_value to
604  * @start_value.
605  */
606 void
607 gst_query_set_segment (GstQuery * query, gdouble rate, GstFormat format,
608     gint64 start_value, gint64 stop_value)
609 {
610   GstStructure *structure;
611
612   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SEGMENT);
613
614   structure = GST_QUERY_STRUCTURE (query);
615   gst_structure_id_set (structure,
616       GST_QUARK (RATE), G_TYPE_DOUBLE, rate,
617       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
618       GST_QUARK (START_VALUE), G_TYPE_INT64, start_value,
619       GST_QUARK (STOP_VALUE), G_TYPE_INT64, stop_value, NULL);
620 }
621
622 /**
623  * gst_query_parse_segment:
624  * @query: a #GstQuery
625  * @rate: (out) (allow-none): the storage for the rate of the segment, or NULL
626  * @format: (out) (allow-none): the storage for the #GstFormat of the values,
627  *     or NULL
628  * @start_value: (out) (allow-none): the storage for the start value, or NULL
629  * @stop_value: (out) (allow-none): the storage for the stop value, or NULL
630  *
631  * Parse a segment query answer. Any of @rate, @format, @start_value, and
632  * @stop_value may be NULL, which will cause this value to be omitted.
633  *
634  * See gst_query_set_segment() for an explanation of the function arguments.
635  */
636 void
637 gst_query_parse_segment (GstQuery * query, gdouble * rate, GstFormat * format,
638     gint64 * start_value, gint64 * stop_value)
639 {
640   GstStructure *structure;
641
642   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SEGMENT);
643
644   structure = GST_QUERY_STRUCTURE (query);
645   if (rate)
646     *rate = g_value_get_double (gst_structure_id_get_value (structure,
647             GST_QUARK (RATE)));
648   if (format)
649     *format =
650         (GstFormat) g_value_get_enum (gst_structure_id_get_value (structure,
651             GST_QUARK (FORMAT)));
652   if (start_value)
653     *start_value = g_value_get_int64 (gst_structure_id_get_value (structure,
654             GST_QUARK (START_VALUE)));
655   if (stop_value)
656     *stop_value = g_value_get_int64 (gst_structure_id_get_value (structure,
657             GST_QUARK (STOP_VALUE)));
658 }
659
660 /**
661  * gst_query_new_custom:
662  * @type: the query type
663  * @structure: a structure for the query
664  *
665  * Constructs a new custom query object. Use gst_query_unref()
666  * when done with it.
667  *
668  * Free-function: gst_query_unref
669  *
670  * Returns: (transfer full): a new #GstQuery
671  */
672 GstQuery *
673 gst_query_new_custom (GstQueryType type, GstStructure * structure)
674 {
675   GstQueryImpl *query;
676
677   query = g_slice_new0 (GstQueryImpl);
678
679   GST_DEBUG ("creating new query %p %s", query, gst_query_type_get_name (type));
680
681   if (structure) {
682     /* structure must not have a parent */
683     if (!gst_structure_set_parent_refcount (structure,
684             &query->query.mini_object.refcount))
685       goto had_parent;
686   }
687
688   gst_mini_object_init (GST_MINI_OBJECT_CAST (query), 0, _gst_query_type,
689       (GstMiniObjectCopyFunction) _gst_query_copy, NULL,
690       (GstMiniObjectFreeFunction) _gst_query_free);
691
692   GST_QUERY_TYPE (query) = type;
693   GST_QUERY_STRUCTURE (query) = structure;
694
695   return GST_QUERY_CAST (query);
696
697   /* ERRORS */
698 had_parent:
699   {
700     g_slice_free1 (sizeof (GstQueryImpl), query);
701     g_warning ("structure is already owned by another object");
702     return NULL;
703   }
704 }
705
706 /**
707  * gst_query_get_structure:
708  * @query: a #GstQuery
709  *
710  * Get the structure of a query.
711  *
712  * Returns: (transfer none): the #GstStructure of the query. The structure is
713  *     still owned by the query and will therefore be freed when the query
714  *     is unreffed.
715  */
716 const GstStructure *
717 gst_query_get_structure (GstQuery * query)
718 {
719   g_return_val_if_fail (GST_IS_QUERY (query), NULL);
720
721   return GST_QUERY_STRUCTURE (query);
722 }
723
724 /**
725  * gst_query_writable_structure:
726  * @query: a #GstQuery
727  *
728  * Get the structure of a query. This method should be called with a writable
729  * @query so that the returned structure is guranteed to be writable.
730  *
731  * Returns: (transfer none): the #GstStructure of the query. The structure is
732  *     still owned by the query and will therefore be freed when the query
733  *     is unreffed.
734  */
735 GstStructure *
736 gst_query_writable_structure (GstQuery * query)
737 {
738   g_return_val_if_fail (GST_IS_QUERY (query), NULL);
739   g_return_val_if_fail (gst_query_is_writable (query), NULL);
740
741   return GST_QUERY_STRUCTURE (query);
742 }
743
744 /**
745  * gst_query_new_seeking:
746  * @format: the default #GstFormat for the new query
747  *
748  * Constructs a new query object for querying seeking properties of
749  * the stream.
750  *
751  * Free-function: gst_query_unref
752  *
753  * Returns: (transfer full): a new #GstQuery
754  */
755 GstQuery *
756 gst_query_new_seeking (GstFormat format)
757 {
758   GstQuery *query;
759   GstStructure *structure;
760
761   structure = gst_structure_new_id (GST_QUARK (QUERY_SEEKING),
762       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
763       GST_QUARK (SEEKABLE), G_TYPE_BOOLEAN, FALSE,
764       GST_QUARK (SEGMENT_START), G_TYPE_INT64, G_GINT64_CONSTANT (-1),
765       GST_QUARK (SEGMENT_END), G_TYPE_INT64, G_GINT64_CONSTANT (-1), NULL);
766
767   query = gst_query_new_custom (GST_QUERY_SEEKING, structure);
768
769   return query;
770 }
771
772 /**
773  * gst_query_set_seeking:
774  * @query: a #GstQuery
775  * @format: the format to set for the @segment_start and @segment_end values
776  * @seekable: the seekable flag to set
777  * @segment_start: the segment_start to set
778  * @segment_end: the segment_end to set
779  *
780  * Set the seeking query result fields in @query.
781  */
782 void
783 gst_query_set_seeking (GstQuery * query, GstFormat format,
784     gboolean seekable, gint64 segment_start, gint64 segment_end)
785 {
786   GstStructure *structure;
787
788   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SEEKING);
789   g_return_if_fail (gst_query_is_writable (query));
790
791   structure = GST_QUERY_STRUCTURE (query);
792   gst_structure_id_set (structure,
793       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
794       GST_QUARK (SEEKABLE), G_TYPE_BOOLEAN, seekable,
795       GST_QUARK (SEGMENT_START), G_TYPE_INT64, segment_start,
796       GST_QUARK (SEGMENT_END), G_TYPE_INT64, segment_end, NULL);
797 }
798
799 /**
800  * gst_query_parse_seeking:
801  * @query: a GST_QUERY_SEEKING type query #GstQuery
802  * @format: (out) (allow-none): the format to set for the @segment_start
803  *     and @segment_end values, or NULL
804  * @seekable: (out) (allow-none): the seekable flag to set, or NULL
805  * @segment_start: (out) (allow-none): the segment_start to set, or NULL
806  * @segment_end: (out) (allow-none): the segment_end to set, or NULL
807  *
808  * Parse a seeking query, writing the format into @format, and
809  * other results into the passed parameters, if the respective parameters
810  * are non-NULL
811  */
812 void
813 gst_query_parse_seeking (GstQuery * query, GstFormat * format,
814     gboolean * seekable, gint64 * segment_start, gint64 * segment_end)
815 {
816   GstStructure *structure;
817
818   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SEEKING);
819
820   structure = GST_QUERY_STRUCTURE (query);
821   if (format)
822     *format =
823         (GstFormat) g_value_get_enum (gst_structure_id_get_value (structure,
824             GST_QUARK (FORMAT)));
825   if (seekable)
826     *seekable = g_value_get_boolean (gst_structure_id_get_value (structure,
827             GST_QUARK (SEEKABLE)));
828   if (segment_start)
829     *segment_start = g_value_get_int64 (gst_structure_id_get_value (structure,
830             GST_QUARK (SEGMENT_START)));
831   if (segment_end)
832     *segment_end = g_value_get_int64 (gst_structure_id_get_value (structure,
833             GST_QUARK (SEGMENT_END)));
834 }
835
836 static GArray *
837 ensure_array (GstStructure * s, GQuark quark, gsize element_size,
838     GDestroyNotify clear_func)
839 {
840   GArray *array;
841   const GValue *value;
842
843   value = gst_structure_id_get_value (s, quark);
844   if (value) {
845     array = (GArray *) g_value_get_boxed (value);
846   } else {
847     GValue new_array_val = { 0, };
848
849     array = g_array_new (FALSE, TRUE, element_size);
850     if (clear_func)
851       g_array_set_clear_func (array, clear_func);
852
853     g_value_init (&new_array_val, G_TYPE_ARRAY);
854     g_value_take_boxed (&new_array_val, array);
855
856     gst_structure_id_take_value (s, quark, &new_array_val);
857   }
858   return array;
859 }
860
861 /**
862  * gst_query_new_formats:
863  *
864  * Constructs a new query object for querying formats of
865  * the stream.
866  *
867  * Free-function: gst_query_unref
868  *
869  * Returns: (transfer full): a new #GstQuery
870  */
871 GstQuery *
872 gst_query_new_formats (void)
873 {
874   GstQuery *query;
875   GstStructure *structure;
876
877   structure = gst_structure_new_id_empty (GST_QUARK (QUERY_FORMATS));
878   query = gst_query_new_custom (GST_QUERY_FORMATS, structure);
879
880   return query;
881 }
882
883 static void
884 gst_query_list_add_format (GValue * list, GstFormat format)
885 {
886   GValue item = { 0, };
887
888   g_value_init (&item, GST_TYPE_FORMAT);
889   g_value_set_enum (&item, format);
890   gst_value_list_append_value (list, &item);
891   g_value_unset (&item);
892 }
893
894 /**
895  * gst_query_set_formats:
896  * @query: a #GstQuery
897  * @n_formats: the number of formats to set.
898  * @...: A number of @GstFormats equal to @n_formats.
899  *
900  * Set the formats query result fields in @query. The number of formats passed
901  * must be equal to @n_formats.
902  */
903 void
904 gst_query_set_formats (GstQuery * query, gint n_formats, ...)
905 {
906   va_list ap;
907   GValue list = { 0, };
908   gint i;
909   GstStructure *structure;
910
911   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_FORMATS);
912   g_return_if_fail (gst_query_is_writable (query));
913
914   g_value_init (&list, GST_TYPE_LIST);
915
916   va_start (ap, n_formats);
917   for (i = 0; i < n_formats; i++) {
918     gst_query_list_add_format (&list, va_arg (ap, GstFormat));
919   }
920   va_end (ap);
921
922   structure = GST_QUERY_STRUCTURE (query);
923   gst_structure_set_value (structure, "formats", &list);
924
925   g_value_unset (&list);
926
927 }
928
929 /**
930  * gst_query_set_formatsv:
931  * @query: a #GstQuery
932  * @n_formats: the number of formats to set.
933  * @formats: (in) (array length=n_formats): an array containing @n_formats
934  *     @GstFormat values.
935  *
936  * Set the formats query result fields in @query. The number of formats passed
937  * in the @formats array must be equal to @n_formats.
938  */
939 void
940 gst_query_set_formatsv (GstQuery * query, gint n_formats,
941     const GstFormat * formats)
942 {
943   GValue list = { 0, };
944   gint i;
945   GstStructure *structure;
946
947   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_FORMATS);
948   g_return_if_fail (gst_query_is_writable (query));
949
950   g_value_init (&list, GST_TYPE_LIST);
951   for (i = 0; i < n_formats; i++) {
952     gst_query_list_add_format (&list, formats[i]);
953   }
954   structure = GST_QUERY_STRUCTURE (query);
955   gst_structure_set_value (structure, "formats", &list);
956
957   g_value_unset (&list);
958 }
959
960 /**
961  * gst_query_parse_n_formats:
962  * @query: a #GstQuery
963  * @n_formats: (out) (allow-none): the number of formats in this query.
964  *
965  * Parse the number of formats in the formats @query.
966  */
967 void
968 gst_query_parse_n_formats (GstQuery * query, guint * n_formats)
969 {
970   GstStructure *structure;
971
972   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_FORMATS);
973
974   if (n_formats) {
975     const GValue *list;
976
977     structure = GST_QUERY_STRUCTURE (query);
978     list = gst_structure_get_value (structure, "formats");
979     if (list == NULL)
980       *n_formats = 0;
981     else
982       *n_formats = gst_value_list_get_size (list);
983   }
984 }
985
986 /**
987  * gst_query_parse_nth_format:
988  * @query: a #GstQuery
989  * @nth: (out): the nth format to retrieve.
990  * @format: (out) (allow-none): a pointer to store the nth format
991  *
992  * Parse the format query and retrieve the @nth format from it into
993  * @format. If the list contains less elements than @nth, @format will be
994  * set to GST_FORMAT_UNDEFINED.
995  */
996 void
997 gst_query_parse_nth_format (GstQuery * query, guint nth, GstFormat * format)
998 {
999   GstStructure *structure;
1000
1001   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_FORMATS);
1002
1003   if (format) {
1004     const GValue *list;
1005
1006     structure = GST_QUERY_STRUCTURE (query);
1007     list = gst_structure_get_value (structure, "formats");
1008     if (list == NULL) {
1009       *format = GST_FORMAT_UNDEFINED;
1010     } else {
1011       if (nth < gst_value_list_get_size (list)) {
1012         *format =
1013             (GstFormat) g_value_get_enum (gst_value_list_get_value (list, nth));
1014       } else
1015         *format = GST_FORMAT_UNDEFINED;
1016     }
1017   }
1018 }
1019
1020 /**
1021  * gst_query_new_buffering:
1022  * @format: the default #GstFormat for the new query
1023  *
1024  * Constructs a new query object for querying the buffering status of
1025  * a stream.
1026  *
1027  * Free-function: gst_query_unref
1028  *
1029  * Returns: (transfer full): a new #GstQuery
1030  */
1031 GstQuery *
1032 gst_query_new_buffering (GstFormat format)
1033 {
1034   GstQuery *query;
1035   GstStructure *structure;
1036
1037   /* by default, we configure the answer as no buffering with a 100% buffering
1038    * progress */
1039   structure = gst_structure_new_id (GST_QUARK (QUERY_BUFFERING),
1040       GST_QUARK (BUSY), G_TYPE_BOOLEAN, FALSE,
1041       GST_QUARK (BUFFER_PERCENT), G_TYPE_INT, 100,
1042       GST_QUARK (BUFFERING_MODE), GST_TYPE_BUFFERING_MODE, GST_BUFFERING_STREAM,
1043       GST_QUARK (AVG_IN_RATE), G_TYPE_INT, -1,
1044       GST_QUARK (AVG_OUT_RATE), G_TYPE_INT, -1,
1045       GST_QUARK (BUFFERING_LEFT), G_TYPE_INT64, G_GINT64_CONSTANT (0),
1046       GST_QUARK (ESTIMATED_TOTAL), G_TYPE_INT64, G_GINT64_CONSTANT (-1),
1047       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
1048       GST_QUARK (START_VALUE), G_TYPE_INT64, G_GINT64_CONSTANT (-1),
1049       GST_QUARK (STOP_VALUE), G_TYPE_INT64, G_GINT64_CONSTANT (-1), NULL);
1050
1051   query = gst_query_new_custom (GST_QUERY_BUFFERING, structure);
1052
1053   return query;
1054 }
1055
1056 /**
1057  * gst_query_set_buffering_percent:
1058  * @query: A valid #GstQuery of type GST_QUERY_BUFFERING.
1059  * @busy: if buffering is busy
1060  * @percent: a buffering percent
1061  *
1062  * Set the percentage of buffered data. This is a value between 0 and 100.
1063  * The @busy indicator is %TRUE when the buffering is in progress.
1064  */
1065 void
1066 gst_query_set_buffering_percent (GstQuery * query, gboolean busy, gint percent)
1067 {
1068   GstStructure *structure;
1069
1070   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING);
1071   g_return_if_fail (gst_query_is_writable (query));
1072   g_return_if_fail (percent >= 0 && percent <= 100);
1073
1074   structure = GST_QUERY_STRUCTURE (query);
1075   gst_structure_id_set (structure,
1076       GST_QUARK (BUSY), G_TYPE_BOOLEAN, busy,
1077       GST_QUARK (BUFFER_PERCENT), G_TYPE_INT, percent, NULL);
1078 }
1079
1080 /**
1081  * gst_query_parse_buffering_percent:
1082  * @query: A valid #GstQuery of type GST_QUERY_BUFFERING.
1083  * @busy: (out) (allow-none): if buffering is busy, or NULL
1084  * @percent: (out) (allow-none): a buffering percent, or NULL
1085  *
1086  * Get the percentage of buffered data. This is a value between 0 and 100.
1087  * The @busy indicator is %TRUE when the buffering is in progress.
1088  */
1089 void
1090 gst_query_parse_buffering_percent (GstQuery * query, gboolean * busy,
1091     gint * percent)
1092 {
1093   GstStructure *structure;
1094
1095   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING);
1096
1097   structure = GST_QUERY_STRUCTURE (query);
1098   if (busy)
1099     *busy = g_value_get_boolean (gst_structure_id_get_value (structure,
1100             GST_QUARK (BUSY)));
1101   if (percent)
1102     *percent = g_value_get_int (gst_structure_id_get_value (structure,
1103             GST_QUARK (BUFFER_PERCENT)));
1104 }
1105
1106 /**
1107  * gst_query_set_buffering_stats:
1108  * @query: A valid #GstQuery of type GST_QUERY_BUFFERING.
1109  * @mode: a buffering mode
1110  * @avg_in: the average input rate
1111  * @avg_out: the average output rate
1112  * @buffering_left: amount of buffering time left
1113  *
1114  * Configures the buffering stats values in @query.
1115  */
1116 void
1117 gst_query_set_buffering_stats (GstQuery * query, GstBufferingMode mode,
1118     gint avg_in, gint avg_out, gint64 buffering_left)
1119 {
1120   GstStructure *structure;
1121
1122   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING);
1123   g_return_if_fail (gst_query_is_writable (query));
1124
1125   structure = GST_QUERY_STRUCTURE (query);
1126   gst_structure_id_set (structure,
1127       GST_QUARK (BUFFERING_MODE), GST_TYPE_BUFFERING_MODE, mode,
1128       GST_QUARK (AVG_IN_RATE), G_TYPE_INT, avg_in,
1129       GST_QUARK (AVG_OUT_RATE), G_TYPE_INT, avg_out,
1130       GST_QUARK (BUFFERING_LEFT), G_TYPE_INT64, buffering_left, NULL);
1131 }
1132
1133 /**
1134  * gst_query_parse_buffering_stats:
1135  * @query: A valid #GstQuery of type GST_QUERY_BUFFERING.
1136  * @mode: (out) (allow-none): a buffering mode, or NULL
1137  * @avg_in: (out) (allow-none): the average input rate, or NULL
1138  * @avg_out: (out) (allow-none): the average output rat, or NULLe
1139  * @buffering_left: (out) (allow-none): amount of buffering time left, or NULL
1140  *
1141  * Extracts the buffering stats values from @query.
1142  */
1143 void
1144 gst_query_parse_buffering_stats (GstQuery * query,
1145     GstBufferingMode * mode, gint * avg_in, gint * avg_out,
1146     gint64 * buffering_left)
1147 {
1148   GstStructure *structure;
1149
1150   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING);
1151
1152   structure = GST_QUERY_STRUCTURE (query);
1153   if (mode)
1154     *mode = (GstBufferingMode)
1155         g_value_get_enum (gst_structure_id_get_value (structure,
1156             GST_QUARK (BUFFERING_MODE)));
1157   if (avg_in)
1158     *avg_in = g_value_get_int (gst_structure_id_get_value (structure,
1159             GST_QUARK (AVG_IN_RATE)));
1160   if (avg_out)
1161     *avg_out = g_value_get_int (gst_structure_id_get_value (structure,
1162             GST_QUARK (AVG_OUT_RATE)));
1163   if (buffering_left)
1164     *buffering_left =
1165         g_value_get_int64 (gst_structure_id_get_value (structure,
1166             GST_QUARK (BUFFERING_LEFT)));
1167 }
1168
1169 /**
1170  * gst_query_set_buffering_range:
1171  * @query: a #GstQuery
1172  * @format: the format to set for the @start and @stop values
1173  * @start: the start to set
1174  * @stop: the stop to set
1175  * @estimated_total: estimated total amount of download time
1176  *
1177  * Set the available query result fields in @query.
1178  */
1179 void
1180 gst_query_set_buffering_range (GstQuery * query, GstFormat format,
1181     gint64 start, gint64 stop, gint64 estimated_total)
1182 {
1183   GstStructure *structure;
1184
1185   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING);
1186   g_return_if_fail (gst_query_is_writable (query));
1187
1188   structure = GST_QUERY_STRUCTURE (query);
1189   gst_structure_id_set (structure,
1190       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
1191       GST_QUARK (START_VALUE), G_TYPE_INT64, start,
1192       GST_QUARK (STOP_VALUE), G_TYPE_INT64, stop,
1193       GST_QUARK (ESTIMATED_TOTAL), G_TYPE_INT64, estimated_total, NULL);
1194 }
1195
1196 /**
1197  * gst_query_parse_buffering_range:
1198  * @query: a GST_QUERY_BUFFERING type query #GstQuery
1199  * @format: (out) (allow-none): the format to set for the @segment_start
1200  *     and @segment_end values, or NULL
1201  * @start: (out) (allow-none): the start to set, or NULL
1202  * @stop: (out) (allow-none): the stop to set, or NULL
1203  * @estimated_total: (out) (allow-none): estimated total amount of download
1204  *     time, or NULL
1205  *
1206  * Parse an available query, writing the format into @format, and
1207  * other results into the passed parameters, if the respective parameters
1208  * are non-NULL
1209  */
1210 void
1211 gst_query_parse_buffering_range (GstQuery * query, GstFormat * format,
1212     gint64 * start, gint64 * stop, gint64 * estimated_total)
1213 {
1214   GstStructure *structure;
1215
1216   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING);
1217
1218   structure = GST_QUERY_STRUCTURE (query);
1219   if (format)
1220     *format =
1221         (GstFormat) g_value_get_enum (gst_structure_id_get_value (structure,
1222             GST_QUARK (FORMAT)));
1223   if (start)
1224     *start = g_value_get_int64 (gst_structure_id_get_value (structure,
1225             GST_QUARK (START_VALUE)));
1226   if (stop)
1227     *stop = g_value_get_int64 (gst_structure_id_get_value (structure,
1228             GST_QUARK (STOP_VALUE)));
1229   if (estimated_total)
1230     *estimated_total =
1231         g_value_get_int64 (gst_structure_id_get_value (structure,
1232             GST_QUARK (ESTIMATED_TOTAL)));
1233 }
1234
1235 /* GstQueryBufferingRange: internal struct for GArray */
1236 typedef struct
1237 {
1238   gint64 start;
1239   gint64 stop;
1240 } GstQueryBufferingRange;
1241
1242 /**
1243  * gst_query_add_buffering_range:
1244  * @query: a GST_QUERY_BUFFERING type query #GstQuery
1245  * @start: start position of the range
1246  * @stop: stop position of the range
1247  *
1248  * Set the buffering-ranges array field in @query. The current last
1249  * start position of the array should be inferior to @start.
1250  *
1251  * Returns: a #gboolean indicating if the range was added or not.
1252  */
1253 gboolean
1254 gst_query_add_buffering_range (GstQuery * query, gint64 start, gint64 stop)
1255 {
1256   GstQueryBufferingRange range;
1257   GstStructure *structure;
1258   GArray *array;
1259
1260   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING, FALSE);
1261   g_return_val_if_fail (gst_query_is_writable (query), FALSE);
1262
1263   if (G_UNLIKELY (start >= stop))
1264     return FALSE;
1265
1266   structure = GST_QUERY_STRUCTURE (query);
1267   array = ensure_array (structure, GST_QUARK (BUFFERING_RANGES),
1268       sizeof (GstQueryBufferingRange), NULL);
1269
1270   if (array->len > 1) {
1271     GstQueryBufferingRange *last;
1272
1273     last = &g_array_index (array, GstQueryBufferingRange, array->len - 1);
1274
1275     if (G_UNLIKELY (start <= last->start))
1276       return FALSE;
1277   }
1278
1279   range.start = start;
1280   range.stop = stop;
1281   g_array_append_val (array, range);
1282
1283   return TRUE;
1284 }
1285
1286 /**
1287  * gst_query_get_n_buffering_ranges:
1288  * @query: a GST_QUERY_BUFFERING type query #GstQuery
1289  *
1290  * Retrieve the number of values currently stored in the
1291  * buffered-ranges array of the query's structure.
1292  *
1293  * Returns: the range array size as a #guint.
1294  */
1295 guint
1296 gst_query_get_n_buffering_ranges (GstQuery * query)
1297 {
1298   GstStructure *structure;
1299   GArray *array;
1300
1301   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING, 0);
1302
1303   structure = GST_QUERY_STRUCTURE (query);
1304   array = ensure_array (structure, GST_QUARK (BUFFERING_RANGES),
1305       sizeof (GstQueryBufferingRange), NULL);
1306
1307   return array->len;
1308 }
1309
1310
1311 /**
1312  * gst_query_parse_nth_buffering_range:
1313  * @query: a GST_QUERY_BUFFERING type query #GstQuery
1314  * @index: position in the buffered-ranges array to read
1315  * @start: (out) (allow-none): the start position to set, or NULL
1316  * @stop: (out) (allow-none): the stop position to set, or NULL
1317  *
1318  * Parse an available query and get the start and stop values stored
1319  * at the @index of the buffered ranges array.
1320  *
1321  * Returns: a #gboolean indicating if the parsing succeeded.
1322  */
1323 gboolean
1324 gst_query_parse_nth_buffering_range (GstQuery * query, guint index,
1325     gint64 * start, gint64 * stop)
1326 {
1327   GstQueryBufferingRange *range;
1328   GstStructure *structure;
1329   GArray *array;
1330
1331   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING, FALSE);
1332
1333   structure = GST_QUERY_STRUCTURE (query);
1334
1335   array = ensure_array (structure, GST_QUARK (BUFFERING_RANGES),
1336       sizeof (GstQueryBufferingRange), NULL);
1337   g_return_val_if_fail (index < array->len, FALSE);
1338
1339   range = &g_array_index (array, GstQueryBufferingRange, index);
1340
1341   if (start)
1342     *start = range->start;
1343   if (stop)
1344     *stop = range->stop;
1345
1346   return TRUE;
1347 }
1348
1349
1350 /**
1351  * gst_query_new_uri:
1352  *
1353  * Constructs a new query URI query object. Use gst_query_unref()
1354  * when done with it. An URI query is used to query the current URI
1355  * that is used by the source or sink.
1356  *
1357  * Free-function: gst_query_unref
1358  *
1359  * Returns: (transfer full): a new #GstQuery
1360  */
1361 GstQuery *
1362 gst_query_new_uri (void)
1363 {
1364   GstQuery *query;
1365   GstStructure *structure;
1366
1367   structure = gst_structure_new_id (GST_QUARK (QUERY_URI),
1368       GST_QUARK (URI), G_TYPE_STRING, NULL, NULL);
1369
1370   query = gst_query_new_custom (GST_QUERY_URI, structure);
1371
1372   return query;
1373 }
1374
1375 /**
1376  * gst_query_set_uri:
1377  * @query: a #GstQuery with query type GST_QUERY_URI
1378  * @uri: the URI to set
1379  *
1380  * Answer a URI query by setting the requested URI.
1381  */
1382 void
1383 gst_query_set_uri (GstQuery * query, const gchar * uri)
1384 {
1385   GstStructure *structure;
1386
1387   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_URI);
1388   g_return_if_fail (gst_query_is_writable (query));
1389   g_return_if_fail (gst_uri_is_valid (uri));
1390
1391   structure = GST_QUERY_STRUCTURE (query);
1392   gst_structure_id_set (structure, GST_QUARK (URI), G_TYPE_STRING, uri, NULL);
1393 }
1394
1395 /**
1396  * gst_query_parse_uri:
1397  * @query: a #GstQuery
1398  * @uri: (out callee-allocates) (allow-none): the storage for the current URI
1399  *     (may be NULL)
1400  *
1401  * Parse an URI query, writing the URI into @uri as a newly
1402  * allocated string, if the respective parameters are non-NULL.
1403  * Free the string with g_free() after usage.
1404  */
1405 void
1406 gst_query_parse_uri (GstQuery * query, gchar ** uri)
1407 {
1408   GstStructure *structure;
1409
1410   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_URI);
1411
1412   structure = GST_QUERY_STRUCTURE (query);
1413   if (uri)
1414     *uri = g_value_dup_string (gst_structure_id_get_value (structure,
1415             GST_QUARK (URI)));
1416 }
1417
1418 /**
1419  * gst_query_new_allocation:
1420  * @caps: the negotiated caps
1421  * @need_pool: return a pool
1422  *
1423  * Constructs a new query object for querying the allocation properties.
1424  *
1425  * Free-function: gst_query_unref
1426  *
1427  * Returns: (transfer full): a new #GstQuery
1428  */
1429 GstQuery *
1430 gst_query_new_allocation (GstCaps * caps, gboolean need_pool)
1431 {
1432   GstQuery *query;
1433   GstStructure *structure;
1434
1435   structure = gst_structure_new_id (GST_QUARK (QUERY_ALLOCATION),
1436       GST_QUARK (CAPS), GST_TYPE_CAPS, caps,
1437       GST_QUARK (NEED_POOL), G_TYPE_BOOLEAN, need_pool, NULL);
1438
1439   query = gst_query_new_custom (GST_QUERY_ALLOCATION, structure);
1440
1441   return query;
1442 }
1443
1444 /**
1445  * gst_query_parse_allocation:
1446  * @query: a #GstQuery
1447  * @caps: (out) (transfer none) (allow-none): The #GstCaps
1448  * @need_pool: (out) (allow-none): Whether a #GstBufferPool is needed
1449  *
1450  * Parse an allocation query, writing the requested caps in @caps and
1451  * whether a pool is needed in @need_pool, if the respective parameters
1452  * are non-NULL.
1453  */
1454 void
1455 gst_query_parse_allocation (GstQuery * query, GstCaps ** caps,
1456     gboolean * need_pool)
1457 {
1458   GstStructure *structure;
1459
1460   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
1461
1462   structure = GST_QUERY_STRUCTURE (query);
1463   if (caps) {
1464     *caps = g_value_get_boxed (gst_structure_id_get_value (structure,
1465             GST_QUARK (CAPS)));
1466   }
1467   gst_structure_id_get (structure,
1468       GST_QUARK (NEED_POOL), G_TYPE_BOOLEAN, need_pool, NULL);
1469 }
1470
1471 typedef struct
1472 {
1473   GstBufferPool *pool;
1474   guint size;
1475   guint min_buffers;
1476   guint max_buffers;
1477 } AllocationPool;
1478
1479 static void
1480 allocation_pool_free (AllocationPool * ap)
1481 {
1482   if (ap->pool)
1483     gst_object_unref (ap->pool);
1484 }
1485
1486 /**
1487  * gst_query_add_allocation_pool:
1488  * @query: A valid #GstQuery of type GST_QUERY_ALLOCATION.
1489  * @pool: the #GstBufferPool
1490  * @size: the size
1491  * @min_buffers: the min buffers
1492  * @max_buffers: the max buffers
1493  *
1494  * Set the pool parameters in @query.
1495  */
1496 void
1497 gst_query_add_allocation_pool (GstQuery * query, GstBufferPool * pool,
1498     guint size, guint min_buffers, guint max_buffers)
1499 {
1500   GArray *array;
1501   GstStructure *structure;
1502   AllocationPool ap;
1503
1504   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
1505   g_return_if_fail (gst_query_is_writable (query));
1506   g_return_if_fail (size != 0);
1507
1508   structure = GST_QUERY_STRUCTURE (query);
1509   array = ensure_array (structure, GST_QUARK (POOL),
1510       sizeof (AllocationPool), (GDestroyNotify) allocation_pool_free);
1511
1512   if ((ap.pool = pool))
1513     gst_object_ref (pool);
1514   ap.size = size;
1515   ap.min_buffers = min_buffers;
1516   ap.max_buffers = max_buffers;
1517
1518   g_array_append_val (array, ap);
1519 }
1520
1521
1522 /**
1523  * gst_query_get_n_allocation_pools:
1524  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1525  *
1526  * Retrieve the number of values currently stored in the
1527  * pool array of the query's structure.
1528  *
1529  * Returns: the pool array size as a #guint.
1530  */
1531 guint
1532 gst_query_get_n_allocation_pools (GstQuery * query)
1533 {
1534   GArray *array;
1535   GstStructure *structure;
1536
1537   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION, 0);
1538
1539   structure = GST_QUERY_STRUCTURE (query);
1540   array = ensure_array (structure, GST_QUARK (POOL),
1541       sizeof (AllocationPool), (GDestroyNotify) allocation_pool_free);
1542
1543   return array->len;
1544 }
1545
1546 /**
1547  * gst_query_parse_nth_allocation_pool:
1548  * @query: A valid #GstQuery of type GST_QUERY_ALLOCATION.
1549  * @index: index to parse
1550  * @pool: (out) (allow-none) (transfer full): the #GstBufferPool
1551  * @size: (out) (allow-none): the size
1552  * @min_buffers: (out) (allow-none): the min buffers
1553  * @max_buffers: (out) (allow-none): the max buffers
1554  *
1555  * Get the pool parameters in @query.
1556  *
1557  * Unref @pool with gst_object_unref() when it's not needed any more.
1558  */
1559 void
1560 gst_query_parse_nth_allocation_pool (GstQuery * query, guint index,
1561     GstBufferPool ** pool, guint * size, guint * min_buffers,
1562     guint * max_buffers)
1563 {
1564   GArray *array;
1565   GstStructure *structure;
1566   AllocationPool *ap;
1567
1568   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
1569
1570   structure = GST_QUERY_STRUCTURE (query);
1571   array = ensure_array (structure, GST_QUARK (POOL),
1572       sizeof (AllocationPool), (GDestroyNotify) allocation_pool_free);
1573   g_return_if_fail (index < array->len);
1574
1575   ap = &g_array_index (array, AllocationPool, index);
1576
1577   if (pool)
1578     if ((*pool = ap->pool))
1579       gst_object_ref (*pool);
1580   if (size)
1581     *size = ap->size;
1582   if (min_buffers)
1583     *min_buffers = ap->min_buffers;
1584   if (max_buffers)
1585     *max_buffers = ap->max_buffers;
1586 }
1587
1588 /**
1589  * gst_query_set_nth_allocation_pool:
1590  * @index: index to modify
1591  * @query: A valid #GstQuery of type GST_QUERY_ALLOCATION.
1592  * @pool: the #GstBufferPool
1593  * @size: the size
1594  * @min_buffers: the min buffers
1595  * @max_buffers: the max buffers
1596  *
1597  * Set the pool parameters in @query.
1598  */
1599 void
1600 gst_query_set_nth_allocation_pool (GstQuery * query, guint index,
1601     GstBufferPool * pool, guint size, guint min_buffers, guint max_buffers)
1602 {
1603   GArray *array;
1604   GstStructure *structure;
1605   AllocationPool *oldap, ap;
1606
1607   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
1608
1609   structure = GST_QUERY_STRUCTURE (query);
1610   array = ensure_array (structure, GST_QUARK (POOL),
1611       sizeof (AllocationPool), (GDestroyNotify) allocation_pool_free);
1612   g_return_if_fail (index < array->len);
1613
1614   oldap = &g_array_index (array, AllocationPool, index);
1615   allocation_pool_free (oldap);
1616
1617   if ((ap.pool = pool))
1618     gst_object_ref (pool);
1619   ap.size = size;
1620   ap.min_buffers = min_buffers;
1621   ap.max_buffers = max_buffers;
1622   g_array_index (array, AllocationPool, index) = ap;
1623 }
1624
1625 typedef struct
1626 {
1627   GType api;
1628   GstStructure *params;
1629 } AllocationMeta;
1630
1631 static void
1632 allocation_meta_free (AllocationMeta * am)
1633 {
1634   if (am->params)
1635     gst_structure_free (am->params);
1636 }
1637
1638 /**
1639  * gst_query_add_allocation_meta:
1640  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1641  * @api: the metadata API
1642  * @params: (transfer none) (allow-none): API specific parameters
1643  *
1644  * Add @api with @params as one of the supported metadata API to @query.
1645  */
1646 void
1647 gst_query_add_allocation_meta (GstQuery * query, GType api,
1648     const GstStructure * params)
1649 {
1650   GArray *array;
1651   GstStructure *structure;
1652   AllocationMeta am;
1653
1654   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
1655   g_return_if_fail (api != 0);
1656   g_return_if_fail (gst_query_is_writable (query));
1657
1658   structure = GST_QUERY_STRUCTURE (query);
1659   array =
1660       ensure_array (structure, GST_QUARK (META), sizeof (AllocationMeta),
1661       (GDestroyNotify) allocation_meta_free);
1662
1663   am.api = api;
1664   am.params = (params ? gst_structure_copy (params) : NULL);
1665
1666   g_array_append_val (array, am);
1667 }
1668
1669 /**
1670  * gst_query_get_n_allocation_metas:
1671  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1672  *
1673  * Retrieve the number of values currently stored in the
1674  * meta API array of the query's structure.
1675  *
1676  * Returns: the metadata API array size as a #guint.
1677  */
1678 guint
1679 gst_query_get_n_allocation_metas (GstQuery * query)
1680 {
1681   GArray *array;
1682   GstStructure *structure;
1683
1684   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION, 0);
1685
1686   structure = GST_QUERY_STRUCTURE (query);
1687   array =
1688       ensure_array (structure, GST_QUARK (META), sizeof (AllocationMeta),
1689       (GDestroyNotify) allocation_meta_free);
1690
1691   return array->len;
1692 }
1693
1694 /**
1695  * gst_query_parse_nth_allocation_meta:
1696  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1697  * @index: position in the metadata API array to read
1698  * @params: (out) (allow-none): API specific flags
1699  *
1700  * Parse an available query and get the metadata API
1701  * at @index of the metadata API array.
1702  *
1703  * Returns: a #GType of the metadata API at @index.
1704  */
1705 GType
1706 gst_query_parse_nth_allocation_meta (GstQuery * query, guint index,
1707     const GstStructure ** params)
1708 {
1709   GArray *array;
1710   GstStructure *structure;
1711   AllocationMeta *am;
1712
1713   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION, 0);
1714
1715   structure = GST_QUERY_STRUCTURE (query);
1716   array =
1717       ensure_array (structure, GST_QUARK (META), sizeof (AllocationMeta),
1718       (GDestroyNotify) allocation_meta_free);
1719
1720   g_return_val_if_fail (index < array->len, 0);
1721
1722   am = &g_array_index (array, AllocationMeta, index);
1723
1724   if (params)
1725     *params = am->params;
1726
1727   return am->api;
1728 }
1729
1730 /**
1731  * gst_query_remove_nth_allocation_meta:
1732  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1733  * @index: position in the metadata API array to remove
1734  *
1735  * Remove the metadata API at @index of the metadata API array.
1736  */
1737 void
1738 gst_query_remove_nth_allocation_meta (GstQuery * query, guint index)
1739 {
1740   GArray *array;
1741   GstStructure *structure;
1742
1743   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
1744   g_return_if_fail (gst_query_is_writable (query));
1745
1746   structure = GST_QUERY_STRUCTURE (query);
1747   array =
1748       ensure_array (structure, GST_QUARK (META), sizeof (AllocationMeta),
1749       (GDestroyNotify) allocation_meta_free);
1750   g_return_if_fail (index < array->len);
1751
1752   g_array_remove_index (array, index);
1753 }
1754
1755 /**
1756  * gst_query_find_allocation_meta:
1757  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1758  * @api: the metadata API
1759  * @index: (out) (allow-none): the index
1760  *
1761  * Check if @query has metadata @api set. When this function returns TRUE,
1762  * @index will contain the index where the requested API and the flags can be
1763  * found.
1764  *
1765  * Returns: TRUE when @api is in the list of metadata.
1766  */
1767 gboolean
1768 gst_query_find_allocation_meta (GstQuery * query, GType api, guint * index)
1769 {
1770   GArray *array;
1771   GstStructure *structure;
1772   guint i, len;
1773
1774   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION, FALSE);
1775   g_return_val_if_fail (api != 0, FALSE);
1776
1777   structure = GST_QUERY_STRUCTURE (query);
1778   array =
1779       ensure_array (structure, GST_QUARK (META), sizeof (AllocationMeta),
1780       (GDestroyNotify) allocation_meta_free);
1781
1782   len = array->len;
1783   for (i = 0; i < len; i++) {
1784     AllocationMeta *am = &g_array_index (array, AllocationMeta, i);
1785     if (am->api == api) {
1786       if (index)
1787         *index = i;
1788       return TRUE;
1789     }
1790   }
1791   return FALSE;
1792 }
1793
1794 typedef struct
1795 {
1796   GstAllocator *allocator;
1797   GstAllocationParams params;
1798 } AllocationParam;
1799
1800 static void
1801 allocation_param_free (AllocationParam * ap)
1802 {
1803   if (ap->allocator)
1804     gst_object_unref (ap->allocator);
1805 }
1806
1807 /**
1808  * gst_query_add_allocation_param:
1809  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1810  * @allocator: (transfer none) (allow-none): the memory allocator
1811  * @params: (transfer none) (allow-none): a #GstAllocationParams
1812  *
1813  * Add @allocator and its @params as a supported memory allocator.
1814  */
1815 void
1816 gst_query_add_allocation_param (GstQuery * query, GstAllocator * allocator,
1817     const GstAllocationParams * params)
1818 {
1819   GArray *array;
1820   GstStructure *structure;
1821   AllocationParam ap;
1822
1823   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
1824   g_return_if_fail (gst_query_is_writable (query));
1825   g_return_if_fail (allocator != NULL || params != NULL);
1826
1827   structure = GST_QUERY_STRUCTURE (query);
1828   array = ensure_array (structure, GST_QUARK (ALLOCATOR),
1829       sizeof (AllocationParam), (GDestroyNotify) allocation_param_free);
1830
1831   if ((ap.allocator = allocator))
1832     gst_object_ref (allocator);
1833   if (params)
1834     ap.params = *params;
1835   else
1836     gst_allocation_params_init (&ap.params);
1837
1838   g_array_append_val (array, ap);
1839 }
1840
1841 /**
1842  * gst_query_get_n_allocation_params:
1843  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1844  *
1845  * Retrieve the number of values currently stored in the
1846  * allocator params array of the query's structure.
1847  *
1848  * If no memory allocator is specified, the downstream element can handle
1849  * the default memory allocator.
1850  *
1851  * Returns: the allocator array size as a #guint.
1852  */
1853 guint
1854 gst_query_get_n_allocation_params (GstQuery * query)
1855 {
1856   GArray *array;
1857   GstStructure *structure;
1858
1859   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION, 0);
1860
1861   structure = GST_QUERY_STRUCTURE (query);
1862   array = ensure_array (structure, GST_QUARK (ALLOCATOR),
1863       sizeof (AllocationParam), (GDestroyNotify) allocation_param_free);
1864
1865   return array->len;
1866 }
1867
1868 /**
1869  * gst_query_parse_nth_allocation_param:
1870  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1871  * @index: position in the allocator array to read
1872  * @allocator: (out) (transfer none) (allow-none): variable to hold the result
1873  * @params: (out) (allow-none): parameters for the allocator
1874  *
1875  * Parse an available query and get the alloctor and its params
1876  * at @index of the allocator array.
1877  */
1878 void
1879 gst_query_parse_nth_allocation_param (GstQuery * query, guint index,
1880     GstAllocator ** allocator, GstAllocationParams * params)
1881 {
1882   GArray *array;
1883   GstStructure *structure;
1884   AllocationParam *ap;
1885
1886   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
1887
1888   structure = GST_QUERY_STRUCTURE (query);
1889   array = ensure_array (structure, GST_QUARK (ALLOCATOR),
1890       sizeof (AllocationParam), (GDestroyNotify) allocation_param_free);
1891   g_return_if_fail (index < array->len);
1892
1893   ap = &g_array_index (array, AllocationParam, index);
1894
1895   if (allocator)
1896     if ((*allocator = ap->allocator))
1897       gst_object_ref (*allocator);
1898   if (params)
1899     *params = ap->params;
1900 }
1901
1902 /**
1903  * gst_query_set_nth_allocation_param:
1904  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1905  * @index: position in the allocator array to set
1906  * @allocator: (transfer none) (allow-none): new allocator to set
1907  * @params: (transfer none) (allow-none): parameters for the allocator
1908  *
1909  * Parse an available query and get the alloctor and its params
1910  * at @index of the allocator array.
1911  */
1912 void
1913 gst_query_set_nth_allocation_param (GstQuery * query, guint index,
1914     GstAllocator * allocator, const GstAllocationParams * params)
1915 {
1916   GArray *array;
1917   GstStructure *structure;
1918   AllocationParam *old, ap;
1919
1920   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
1921
1922   structure = GST_QUERY_STRUCTURE (query);
1923   array = ensure_array (structure, GST_QUARK (ALLOCATOR),
1924       sizeof (AllocationParam), (GDestroyNotify) allocation_param_free);
1925   g_return_if_fail (index < array->len);
1926
1927   old = &g_array_index (array, AllocationParam, index);
1928   allocation_param_free (old);
1929
1930   if ((ap.allocator = allocator))
1931     gst_object_ref (allocator);
1932   if (params)
1933     ap.params = *params;
1934   else
1935     gst_allocation_params_init (&ap.params);
1936
1937   g_array_index (array, AllocationParam, index) = ap;
1938 }
1939
1940 /**
1941  * gst_query_new_scheduling:
1942  *
1943  * Constructs a new query object for querying the scheduling properties.
1944  *
1945  * Free-function: gst_query_unref
1946  *
1947  * Returns: (transfer full): a new #GstQuery
1948  */
1949 GstQuery *
1950 gst_query_new_scheduling (void)
1951 {
1952   GstQuery *query;
1953   GstStructure *structure;
1954
1955   structure = gst_structure_new_id (GST_QUARK (QUERY_SCHEDULING),
1956       GST_QUARK (FLAGS), GST_TYPE_SCHEDULING_FLAGS, 0,
1957       GST_QUARK (MINSIZE), G_TYPE_INT, 1,
1958       GST_QUARK (MAXSIZE), G_TYPE_INT, -1,
1959       GST_QUARK (ALIGN), G_TYPE_INT, 0, NULL);
1960   query = gst_query_new_custom (GST_QUERY_SCHEDULING, structure);
1961
1962   return query;
1963 }
1964
1965 /**
1966  * gst_query_set_scheduling:
1967  * @query: A valid #GstQuery of type GST_QUERY_SCHEDULING.
1968  * @flags: #GstSchedulingFlags
1969  * @minsize: the suggested minimum size of pull requests
1970  * @maxsize: the suggested maximum size of pull requests
1971  * @align: the suggested alignment of pull requests
1972  *
1973  * Set the scheduling properties.
1974  */
1975 void
1976 gst_query_set_scheduling (GstQuery * query, GstSchedulingFlags flags,
1977     gint minsize, gint maxsize, gint align)
1978 {
1979   GstStructure *structure;
1980
1981   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SCHEDULING);
1982   g_return_if_fail (gst_query_is_writable (query));
1983
1984   structure = GST_QUERY_STRUCTURE (query);
1985   gst_structure_id_set (structure,
1986       GST_QUARK (FLAGS), GST_TYPE_SCHEDULING_FLAGS, flags,
1987       GST_QUARK (MINSIZE), G_TYPE_INT, minsize,
1988       GST_QUARK (MAXSIZE), G_TYPE_INT, maxsize,
1989       GST_QUARK (ALIGN), G_TYPE_INT, align, NULL);
1990 }
1991
1992 /**
1993  * gst_query_parse_scheduling:
1994  * @query: A valid #GstQuery of type GST_QUERY_SCHEDULING.
1995  * @flags: (out) (allow-none): #GstSchedulingFlags
1996  * @minsize: (out) (allow-none): the suggested minimum size of pull requests
1997  * @maxsize: (out) (allow-none): the suggested maximum size of pull requests:
1998  * @align: (out) (allow-none): the suggested alignment of pull requests
1999  *
2000  * Set the scheduling properties.
2001  */
2002 void
2003 gst_query_parse_scheduling (GstQuery * query, GstSchedulingFlags * flags,
2004     gint * minsize, gint * maxsize, gint * align)
2005 {
2006   GstStructure *structure;
2007
2008   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SCHEDULING);
2009
2010   structure = GST_QUERY_STRUCTURE (query);
2011   gst_structure_id_get (structure,
2012       GST_QUARK (FLAGS), GST_TYPE_SCHEDULING_FLAGS, flags,
2013       GST_QUARK (MINSIZE), G_TYPE_INT, minsize,
2014       GST_QUARK (MAXSIZE), G_TYPE_INT, maxsize,
2015       GST_QUARK (ALIGN), G_TYPE_INT, align, NULL);
2016 }
2017
2018 /**
2019  * gst_query_add_scheduling_mode:
2020  * @query: a GST_QUERY_SCHEDULING type query #GstQuery
2021  * @mode: a #GstPadMode
2022  *
2023  * Add @mode as aone of the supported scheduling modes to @query.
2024  */
2025 void
2026 gst_query_add_scheduling_mode (GstQuery * query, GstPadMode mode)
2027 {
2028   GstStructure *structure;
2029   GArray *array;
2030
2031   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SCHEDULING);
2032   g_return_if_fail (gst_query_is_writable (query));
2033
2034   structure = GST_QUERY_STRUCTURE (query);
2035   array =
2036       ensure_array (structure, GST_QUARK (MODES), sizeof (GstPadMode), NULL);
2037
2038   g_array_append_val (array, mode);
2039 }
2040
2041 /**
2042  * gst_query_get_n_scheduling_modes:
2043  * @query: a GST_QUERY_SCHEDULING type query #GstQuery
2044  *
2045  * Retrieve the number of values currently stored in the
2046  * scheduling mode array of the query's structure.
2047  *
2048  * Returns: the scheduling mode array size as a #guint.
2049  */
2050 guint
2051 gst_query_get_n_scheduling_modes (GstQuery * query)
2052 {
2053   GArray *array;
2054   GstStructure *structure;
2055
2056   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SCHEDULING, 0);
2057
2058   structure = GST_QUERY_STRUCTURE (query);
2059   array =
2060       ensure_array (structure, GST_QUARK (MODES), sizeof (GstPadMode), NULL);
2061
2062   return array->len;
2063 }
2064
2065 /**
2066  * gst_query_parse_nth_scheduling_mode:
2067  * @query: a GST_QUERY_SCHEDULING type query #GstQuery
2068  * @index: position in the scheduling modes array to read
2069  *
2070  * Parse an available query and get the scheduling mode
2071  * at @index of the scheduling modes array.
2072  *
2073  * Returns: a #GstPadMode of the scheduling mode at @index.
2074  */
2075 GstPadMode
2076 gst_query_parse_nth_scheduling_mode (GstQuery * query, guint index)
2077 {
2078   GstStructure *structure;
2079   GArray *array;
2080
2081   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SCHEDULING,
2082       GST_PAD_MODE_NONE);
2083
2084   structure = GST_QUERY_STRUCTURE (query);
2085   array =
2086       ensure_array (structure, GST_QUARK (MODES), sizeof (GstPadMode), NULL);
2087   g_return_val_if_fail (index < array->len, GST_PAD_MODE_NONE);
2088
2089   return g_array_index (array, GstPadMode, index);
2090 }
2091
2092 /**
2093  * gst_query_has_scheduling_mode:
2094  * @query: a GST_QUERY_SCHEDULING type query #GstQuery
2095  * @mode: the scheduling mode
2096  *
2097  * Check if @query has scheduling mode set.
2098  *
2099  * <note>
2100  *   <para>
2101  *     When checking if upstream supports pull mode, it is usually not
2102  *     enough to just check for GST_PAD_MODE_PULL with this function, you
2103  *     also want to check whether the scheduling flags returned by
2104  *     gst_query_parse_scheduling() have the seeking flag set (meaning
2105  *     random access is supported, not only sequential pulls).
2106  *   </para>
2107  * </note>
2108  *
2109  * Returns: TRUE when @mode is in the list of scheduling modes.
2110  */
2111 gboolean
2112 gst_query_has_scheduling_mode (GstQuery * query, GstPadMode mode)
2113 {
2114   GstStructure *structure;
2115   GArray *array;
2116   guint i, len;
2117
2118   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SCHEDULING, FALSE);
2119
2120   structure = GST_QUERY_STRUCTURE (query);
2121   array =
2122       ensure_array (structure, GST_QUARK (MODES), sizeof (GstPadMode), NULL);
2123
2124   len = array->len;
2125   for (i = 0; i < len; i++) {
2126     if (mode == g_array_index (array, GstPadMode, i))
2127       return TRUE;
2128   }
2129   return FALSE;
2130 }
2131
2132 /**
2133  * gst_query_has_scheduling_mode_with_flags:
2134  * @query: a GST_QUERY_SCHEDULING type query #GstQuery
2135  * @mode: the scheduling mode
2136  * @flags: #GstSchedulingFlags
2137  *
2138  * Check if @query has scheduling mode set and @flags is set in
2139  * query scheduling flags.
2140  *
2141  * Returns: TRUE when @mode is in the list of scheduling modes
2142  *    and @flags are compatible with query flags.
2143  */
2144 gboolean
2145 gst_query_has_scheduling_mode_with_flags (GstQuery * query, GstPadMode mode,
2146     GstSchedulingFlags flags)
2147 {
2148   GstSchedulingFlags sched_flags;
2149
2150   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SCHEDULING, FALSE);
2151
2152   gst_query_parse_scheduling (query, &sched_flags, NULL, NULL, NULL);
2153
2154   return ((flags & sched_flags) == flags) &&
2155       gst_query_has_scheduling_mode (query, mode);
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 }