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