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