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