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