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