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