pad: remove getcaps and use caps query
[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 /**
967  * gst_query_new_formats:
968  *
969  * Constructs a new query object for querying formats of
970  * the stream.
971  *
972  * Free-function: gst_query_unref
973  *
974  * Returns: (transfer full): a new #GstQuery
975  *
976  * Since: 0.10.4
977  */
978 GstQuery *
979 gst_query_new_formats (void)
980 {
981   GstQuery *query;
982   GstStructure *structure;
983
984   structure = gst_structure_new_id_empty (GST_QUARK (QUERY_FORMATS));
985   query = gst_query_new (GST_QUERY_FORMATS, structure);
986
987   return query;
988 }
989
990 static void
991 gst_query_list_add_format (GValue * list, GstFormat format)
992 {
993   GValue item = { 0, };
994
995   g_value_init (&item, GST_TYPE_FORMAT);
996   g_value_set_enum (&item, format);
997   gst_value_list_append_value (list, &item);
998   g_value_unset (&item);
999 }
1000
1001 /**
1002  * gst_query_set_formats:
1003  * @query: a #GstQuery
1004  * @n_formats: the number of formats to set.
1005  * @...: A number of @GstFormats equal to @n_formats.
1006  *
1007  * Set the formats query result fields in @query. The number of formats passed
1008  * must be equal to @n_formats.
1009  */
1010 void
1011 gst_query_set_formats (GstQuery * query, gint n_formats, ...)
1012 {
1013   va_list ap;
1014   GValue list = { 0, };
1015   gint i;
1016   GstStructure *structure;
1017
1018   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_FORMATS);
1019   g_return_if_fail (gst_query_is_writable (query));
1020
1021   g_value_init (&list, GST_TYPE_LIST);
1022
1023   va_start (ap, n_formats);
1024   for (i = 0; i < n_formats; i++) {
1025     gst_query_list_add_format (&list, va_arg (ap, GstFormat));
1026   }
1027   va_end (ap);
1028
1029   structure = GST_QUERY_STRUCTURE (query);
1030   gst_structure_set_value (structure, "formats", &list);
1031
1032   g_value_unset (&list);
1033
1034 }
1035
1036 /**
1037  * gst_query_set_formatsv:
1038  * @query: a #GstQuery
1039  * @n_formats: the number of formats to set.
1040  * @formats: (in) (array length=n_formats): an array containing @n_formats
1041  *     @GstFormat values.
1042  *
1043  * Set the formats query result fields in @query. The number of formats passed
1044  * in the @formats array must be equal to @n_formats.
1045  *
1046  * Since: 0.10.4
1047  */
1048 void
1049 gst_query_set_formatsv (GstQuery * query, gint n_formats,
1050     const GstFormat * formats)
1051 {
1052   GValue list = { 0, };
1053   gint i;
1054   GstStructure *structure;
1055
1056   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_FORMATS);
1057   g_return_if_fail (gst_query_is_writable (query));
1058
1059   g_value_init (&list, GST_TYPE_LIST);
1060   for (i = 0; i < n_formats; i++) {
1061     gst_query_list_add_format (&list, formats[i]);
1062   }
1063   structure = GST_QUERY_STRUCTURE (query);
1064   gst_structure_set_value (structure, "formats", &list);
1065
1066   g_value_unset (&list);
1067 }
1068
1069 /**
1070  * gst_query_parse_n_formats:
1071  * @query: a #GstQuery
1072  * @n_formats: (out): the number of formats in this query.
1073  *
1074  * Parse the number of formats in the formats @query.
1075  *
1076  * Since: 0.10.4
1077  */
1078 void
1079 gst_query_parse_n_formats (GstQuery * query, guint * n_formats)
1080 {
1081   GstStructure *structure;
1082
1083   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_FORMATS);
1084
1085   if (n_formats) {
1086     const GValue *list;
1087
1088     structure = GST_QUERY_STRUCTURE (query);
1089     list = gst_structure_get_value (structure, "formats");
1090     if (list == NULL)
1091       *n_formats = 0;
1092     else
1093       *n_formats = gst_value_list_get_size (list);
1094   }
1095 }
1096
1097 /**
1098  * gst_query_parse_nth_format:
1099  * @query: a #GstQuery
1100  * @nth: (out): the nth format to retrieve.
1101  * @format: (out): a pointer to store the nth format
1102  *
1103  * Parse the format query and retrieve the @nth format from it into
1104  * @format. If the list contains less elements than @nth, @format will be
1105  * set to GST_FORMAT_UNDEFINED.
1106  */
1107 void
1108 gst_query_parse_nth_format (GstQuery * query, guint nth, GstFormat * format)
1109 {
1110   GstStructure *structure;
1111
1112   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_FORMATS);
1113
1114   if (format) {
1115     const GValue *list;
1116
1117     structure = GST_QUERY_STRUCTURE (query);
1118     list = gst_structure_get_value (structure, "formats");
1119     if (list == NULL) {
1120       *format = GST_FORMAT_UNDEFINED;
1121     } else {
1122       if (nth < gst_value_list_get_size (list)) {
1123         *format =
1124             (GstFormat) g_value_get_enum (gst_value_list_get_value (list, nth));
1125       } else
1126         *format = GST_FORMAT_UNDEFINED;
1127     }
1128   }
1129 }
1130
1131 /**
1132  * gst_query_new_buffering
1133  * @format: the default #GstFormat for the new query
1134  *
1135  * Constructs a new query object for querying the buffering status of
1136  * a stream.
1137  *
1138  * Free-function: gst_query_unref
1139  *
1140  * Returns: (transfer full): a new #GstQuery
1141  *
1142  * Since: 0.10.20
1143  */
1144 GstQuery *
1145 gst_query_new_buffering (GstFormat format)
1146 {
1147   GstQuery *query;
1148   GstStructure *structure;
1149
1150   /* by default, we configure the answer as no buffering with a 100% buffering
1151    * progress */
1152   structure = gst_structure_new_id (GST_QUARK (QUERY_BUFFERING),
1153       GST_QUARK (BUSY), G_TYPE_BOOLEAN, FALSE,
1154       GST_QUARK (BUFFER_PERCENT), G_TYPE_INT, 100,
1155       GST_QUARK (BUFFERING_MODE), GST_TYPE_BUFFERING_MODE, GST_BUFFERING_STREAM,
1156       GST_QUARK (AVG_IN_RATE), G_TYPE_INT, -1,
1157       GST_QUARK (AVG_OUT_RATE), G_TYPE_INT, -1,
1158       GST_QUARK (BUFFERING_LEFT), G_TYPE_INT64, G_GINT64_CONSTANT (0),
1159       GST_QUARK (ESTIMATED_TOTAL), G_TYPE_INT64, G_GINT64_CONSTANT (-1),
1160       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
1161       GST_QUARK (START_VALUE), G_TYPE_INT64, G_GINT64_CONSTANT (-1),
1162       GST_QUARK (STOP_VALUE), G_TYPE_INT64, G_GINT64_CONSTANT (-1), NULL);
1163
1164   query = gst_query_new (GST_QUERY_BUFFERING, structure);
1165
1166   return query;
1167 }
1168
1169 /**
1170  * gst_query_set_buffering_percent
1171  * @query: A valid #GstQuery of type GST_QUERY_BUFFERING.
1172  * @busy: if buffering is busy
1173  * @percent: a buffering percent
1174  *
1175  * Set the percentage of buffered data. This is a value between 0 and 100.
1176  * The @busy indicator is %TRUE when the buffering is in progress.
1177  *
1178  * Since: 0.10.20
1179  */
1180 void
1181 gst_query_set_buffering_percent (GstQuery * query, gboolean busy, gint percent)
1182 {
1183   GstStructure *structure;
1184
1185   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING);
1186   g_return_if_fail (gst_query_is_writable (query));
1187   g_return_if_fail (percent >= 0 && percent <= 100);
1188
1189   structure = GST_QUERY_STRUCTURE (query);
1190   gst_structure_id_set (structure,
1191       GST_QUARK (BUSY), G_TYPE_BOOLEAN, busy,
1192       GST_QUARK (BUFFER_PERCENT), G_TYPE_INT, percent, NULL);
1193 }
1194
1195 /**
1196  * gst_query_parse_buffering_percent
1197  * @query: A valid #GstQuery of type GST_QUERY_BUFFERING.
1198  * @busy: (out) (allow-none): if buffering is busy, or NULL
1199  * @percent: (out) (allow-none): a buffering percent, or NULL
1200  *
1201  * Get the percentage of buffered data. This is a value between 0 and 100.
1202  * The @busy indicator is %TRUE when the buffering is in progress.
1203  *
1204  * Since: 0.10.20
1205  */
1206 void
1207 gst_query_parse_buffering_percent (GstQuery * query, gboolean * busy,
1208     gint * percent)
1209 {
1210   GstStructure *structure;
1211
1212   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING);
1213
1214   structure = GST_QUERY_STRUCTURE (query);
1215   if (busy)
1216     *busy = g_value_get_boolean (gst_structure_id_get_value (structure,
1217             GST_QUARK (BUSY)));
1218   if (percent)
1219     *percent = g_value_get_int (gst_structure_id_get_value (structure,
1220             GST_QUARK (BUFFER_PERCENT)));
1221 }
1222
1223 /**
1224  * gst_query_set_buffering_stats:
1225  * @query: A valid #GstQuery of type GST_QUERY_BUFFERING.
1226  * @mode: a buffering mode
1227  * @avg_in: the average input rate
1228  * @avg_out: the average output rate
1229  * @buffering_left: amount of buffering time left
1230  *
1231  * Configures the buffering stats values in @query.
1232  *
1233  * Since: 0.10.20
1234  */
1235 void
1236 gst_query_set_buffering_stats (GstQuery * query, GstBufferingMode mode,
1237     gint avg_in, gint avg_out, gint64 buffering_left)
1238 {
1239   GstStructure *structure;
1240
1241   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING);
1242   g_return_if_fail (gst_query_is_writable (query));
1243
1244   structure = GST_QUERY_STRUCTURE (query);
1245   gst_structure_id_set (structure,
1246       GST_QUARK (BUFFERING_MODE), GST_TYPE_BUFFERING_MODE, mode,
1247       GST_QUARK (AVG_IN_RATE), G_TYPE_INT, avg_in,
1248       GST_QUARK (AVG_OUT_RATE), G_TYPE_INT, avg_out,
1249       GST_QUARK (BUFFERING_LEFT), G_TYPE_INT64, buffering_left, NULL);
1250 }
1251
1252 /**
1253  * gst_query_parse_buffering_stats:
1254  * @query: A valid #GstQuery of type GST_QUERY_BUFFERING.
1255  * @mode: (out) (allow-none): a buffering mode, or NULL
1256  * @avg_in: (out) (allow-none): the average input rate, or NULL
1257  * @avg_out: (out) (allow-none): the average output rat, or NULLe
1258  * @buffering_left: (out) (allow-none): amount of buffering time left, or NULL
1259  *
1260  * Extracts the buffering stats values from @query.
1261  *
1262  * Since: 0.10.20
1263  */
1264 void
1265 gst_query_parse_buffering_stats (GstQuery * query,
1266     GstBufferingMode * mode, gint * avg_in, gint * avg_out,
1267     gint64 * buffering_left)
1268 {
1269   GstStructure *structure;
1270
1271   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING);
1272
1273   structure = GST_QUERY_STRUCTURE (query);
1274   if (mode)
1275     *mode = (GstBufferingMode)
1276         g_value_get_enum (gst_structure_id_get_value (structure,
1277             GST_QUARK (BUFFERING_MODE)));
1278   if (avg_in)
1279     *avg_in = g_value_get_int (gst_structure_id_get_value (structure,
1280             GST_QUARK (AVG_IN_RATE)));
1281   if (avg_out)
1282     *avg_out = g_value_get_int (gst_structure_id_get_value (structure,
1283             GST_QUARK (AVG_OUT_RATE)));
1284   if (buffering_left)
1285     *buffering_left =
1286         g_value_get_int64 (gst_structure_id_get_value (structure,
1287             GST_QUARK (BUFFERING_LEFT)));
1288 }
1289
1290
1291 /**
1292  * gst_query_set_buffering_range:
1293  * @query: a #GstQuery
1294  * @format: the format to set for the @start and @stop values
1295  * @start: the start to set
1296  * @stop: the stop to set
1297  * @estimated_total: estimated total amount of download time
1298  *
1299  * Set the available query result fields in @query.
1300  *
1301  * Since: 0.10.20
1302  */
1303 void
1304 gst_query_set_buffering_range (GstQuery * query, GstFormat format,
1305     gint64 start, gint64 stop, gint64 estimated_total)
1306 {
1307   GstStructure *structure;
1308
1309   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING);
1310   g_return_if_fail (gst_query_is_writable (query));
1311
1312   structure = GST_QUERY_STRUCTURE (query);
1313   gst_structure_id_set (structure,
1314       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
1315       GST_QUARK (START_VALUE), G_TYPE_INT64, start,
1316       GST_QUARK (STOP_VALUE), G_TYPE_INT64, stop,
1317       GST_QUARK (ESTIMATED_TOTAL), G_TYPE_INT64, estimated_total, NULL);
1318 }
1319
1320 /**
1321  * gst_query_parse_buffering_range:
1322  * @query: a GST_QUERY_BUFFERING type query #GstQuery
1323  * @format: (out) (allow-none): the format to set for the @segment_start
1324  *     and @segment_end values, or NULL
1325  * @start: (out) (allow-none): the start to set, or NULL
1326  * @stop: (out) (allow-none): the stop to set, or NULL
1327  * @estimated_total: (out) (allow-none): estimated total amount of download
1328  *     time, or NULL
1329  *
1330  * Parse an available query, writing the format into @format, and
1331  * other results into the passed parameters, if the respective parameters
1332  * are non-NULL
1333  *
1334  * Since: 0.10.20
1335  */
1336 void
1337 gst_query_parse_buffering_range (GstQuery * query, GstFormat * format,
1338     gint64 * start, gint64 * stop, gint64 * estimated_total)
1339 {
1340   GstStructure *structure;
1341
1342   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING);
1343
1344   structure = GST_QUERY_STRUCTURE (query);
1345   if (format)
1346     *format =
1347         (GstFormat) g_value_get_enum (gst_structure_id_get_value (structure,
1348             GST_QUARK (FORMAT)));
1349   if (start)
1350     *start = g_value_get_int64 (gst_structure_id_get_value (structure,
1351             GST_QUARK (START_VALUE)));
1352   if (stop)
1353     *stop = g_value_get_int64 (gst_structure_id_get_value (structure,
1354             GST_QUARK (STOP_VALUE)));
1355   if (estimated_total)
1356     *estimated_total =
1357         g_value_get_int64 (gst_structure_id_get_value (structure,
1358             GST_QUARK (ESTIMATED_TOTAL)));
1359 }
1360
1361 /**
1362  * gst_query_add_buffering_range
1363  * @query: a GST_QUERY_BUFFERING type query #GstQuery
1364  * @start: start position of the range
1365  * @stop: stop position of the range
1366  *
1367  * Set the buffering-ranges array field in @query. The current last
1368  * start position of the array should be inferior to @start.
1369  *
1370  * Returns: a #gboolean indicating if the range was added or not.
1371  *
1372  * Since: 0.10.31
1373  */
1374 gboolean
1375 gst_query_add_buffering_range (GstQuery * query, gint64 start, gint64 stop)
1376 {
1377   GValueArray *array;
1378   GValue *last_array_value;
1379   const GValue *value;
1380   GValue range_value = { 0 };
1381   GstStructure *structure;
1382
1383   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING, FALSE);
1384   g_return_val_if_fail (gst_query_is_writable (query), FALSE);
1385
1386   if (G_UNLIKELY (start >= stop))
1387     return FALSE;
1388
1389   structure = GST_QUERY_STRUCTURE (query);
1390   value = gst_structure_id_get_value (structure, GST_QUARK (BUFFERING_RANGES));
1391   if (value) {
1392     array = (GValueArray *) g_value_get_boxed (value);
1393     last_array_value = g_value_array_get_nth (array, array->n_values - 1);
1394     if (G_UNLIKELY (start <= gst_value_get_int64_range_min (last_array_value)))
1395       return FALSE;
1396   } else {
1397     GValue new_array_val = { 0, };
1398
1399     array = g_value_array_new (0);
1400
1401     g_value_init (&new_array_val, G_TYPE_VALUE_ARRAY);
1402     g_value_take_boxed (&new_array_val, array);
1403
1404     /* set the value array only once, so we then modify (append to) the
1405      * existing value array owned by the GstStructure / the field's GValue */
1406     gst_structure_id_take_value (structure, GST_QUARK (BUFFERING_RANGES),
1407         &new_array_val);
1408   }
1409
1410   g_value_init (&range_value, GST_TYPE_INT64_RANGE);
1411   gst_value_set_int64_range (&range_value, start, stop);
1412   g_value_array_append (array, &range_value);
1413   /* skip the g_value_unset(&range_value) here, we know it's not needed */
1414
1415   return TRUE;
1416 }
1417
1418 /**
1419  * gst_query_get_n_buffering_ranges
1420  * @query: a GST_QUERY_BUFFERING type query #GstQuery
1421  *
1422  * Retrieve the number of values currently stored in the
1423  * buffered-ranges array of the query's structure.
1424  *
1425  * Returns: the range array size as a #guint.
1426  *
1427  * Since: 0.10.31
1428  */
1429 guint
1430 gst_query_get_n_buffering_ranges (GstQuery * query)
1431 {
1432   GValueArray *array;
1433   const GValue *value;
1434   guint size = 0;
1435   GstStructure *structure;
1436
1437   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING, 0);
1438
1439   structure = GST_QUERY_STRUCTURE (query);
1440   value = gst_structure_id_get_value (structure, GST_QUARK (BUFFERING_RANGES));
1441   if (value) {
1442     array = (GValueArray *) g_value_get_boxed (value);
1443     size = array->n_values;
1444   }
1445   return size;
1446 }
1447
1448
1449 /**
1450  * gst_query_parse_nth_buffering_range
1451  * @query: a GST_QUERY_BUFFERING type query #GstQuery
1452  * @index: position in the buffered-ranges array to read
1453  * @start: (out) (allow-none): the start position to set, or NULL
1454  * @stop: (out) (allow-none): the stop position to set, or NULL
1455  *
1456  * Parse an available query and get the start and stop values stored
1457  * at the @index of the buffered ranges array.
1458  *
1459  * Returns: a #gboolean indicating if the parsing succeeded.
1460  *
1461  * Since: 0.10.31
1462  */
1463 gboolean
1464 gst_query_parse_nth_buffering_range (GstQuery * query, guint index,
1465     gint64 * start, gint64 * stop)
1466 {
1467   const GValue *value;
1468   GValueArray *ranges;
1469   GValue *range_value;
1470   gboolean ret = FALSE;
1471   GstStructure *structure;
1472
1473   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING, ret);
1474
1475   structure = GST_QUERY_STRUCTURE (query);
1476   value = gst_structure_id_get_value (structure, GST_QUARK (BUFFERING_RANGES));
1477   ranges = (GValueArray *) g_value_get_boxed (value);
1478   range_value = g_value_array_get_nth (ranges, index);
1479   if (range_value) {
1480     if (start)
1481       *start = gst_value_get_int64_range_min (range_value);
1482     if (stop)
1483       *stop = gst_value_get_int64_range_max (range_value);
1484     ret = TRUE;
1485   }
1486
1487   return ret;
1488 }
1489
1490
1491 /**
1492  * gst_query_new_uri:
1493  *
1494  * Constructs a new query URI query object. Use gst_query_unref()
1495  * when done with it. An URI query is used to query the current URI
1496  * that is used by the source or sink.
1497  *
1498  * Free-function: gst_query_unref
1499  *
1500  * Returns: (transfer full): a new #GstQuery
1501  *
1502  * Since: 0.10.22
1503  */
1504 GstQuery *
1505 gst_query_new_uri (void)
1506 {
1507   GstQuery *query;
1508   GstStructure *structure;
1509
1510   structure = gst_structure_new_id (GST_QUARK (QUERY_URI),
1511       GST_QUARK (URI), G_TYPE_STRING, NULL, NULL);
1512
1513   query = gst_query_new (GST_QUERY_URI, structure);
1514
1515   return query;
1516 }
1517
1518 /**
1519  * gst_query_set_uri:
1520  * @query: a #GstQuery with query type GST_QUERY_URI
1521  * @uri: the URI to set
1522  *
1523  * Answer a URI query by setting the requested URI.
1524  *
1525  * Since: 0.10.22
1526  */
1527 void
1528 gst_query_set_uri (GstQuery * query, const gchar * uri)
1529 {
1530   GstStructure *structure;
1531
1532   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_URI);
1533   g_return_if_fail (gst_query_is_writable (query));
1534   g_return_if_fail (gst_uri_is_valid (uri));
1535
1536   structure = GST_QUERY_STRUCTURE (query);
1537   gst_structure_id_set (structure, GST_QUARK (URI), G_TYPE_STRING, uri, NULL);
1538 }
1539
1540 /**
1541  * gst_query_parse_uri:
1542  * @query: a #GstQuery
1543  * @uri: (out callee-allocates) (allow-none): the storage for the current URI
1544  *     (may be NULL)
1545  *
1546  * Parse an URI query, writing the URI into @uri as a newly
1547  * allocated string, if the respective parameters are non-NULL.
1548  * Free the string with g_free() after usage.
1549  *
1550  * Since: 0.10.22
1551  */
1552 void
1553 gst_query_parse_uri (GstQuery * query, gchar ** uri)
1554 {
1555   GstStructure *structure;
1556
1557   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_URI);
1558
1559   structure = GST_QUERY_STRUCTURE (query);
1560   if (uri)
1561     *uri = g_value_dup_string (gst_structure_id_get_value (structure,
1562             GST_QUARK (URI)));
1563 }
1564
1565 /**
1566  * gst_query_new_allocation
1567  * @caps: the negotiated caps
1568  * @need_pool: return a pool
1569  *
1570  * Constructs a new query object for querying the allocation properties.
1571  *
1572  * Free-function: gst_query_unref
1573  *
1574  * Returns: (transfer full): a new #GstQuery
1575  */
1576 GstQuery *
1577 gst_query_new_allocation (GstCaps * caps, gboolean need_pool)
1578 {
1579   GstQuery *query;
1580   GstStructure *structure;
1581
1582   structure = gst_structure_new_id (GST_QUARK (QUERY_ALLOCATION),
1583       GST_QUARK (CAPS), GST_TYPE_CAPS, caps,
1584       GST_QUARK (NEED_POOL), G_TYPE_BOOLEAN, need_pool,
1585       GST_QUARK (SIZE), G_TYPE_UINT, 0,
1586       GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, 0,
1587       GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, 0,
1588       GST_QUARK (PREFIX), G_TYPE_UINT, 0,
1589       GST_QUARK (ALIGN), G_TYPE_UINT, 0,
1590       GST_QUARK (POOL), GST_TYPE_BUFFER_POOL, NULL, NULL);
1591
1592   query = gst_query_new (GST_QUERY_ALLOCATION, structure);
1593
1594   return query;
1595 }
1596
1597 void
1598 gst_query_parse_allocation (GstQuery * query, GstCaps ** caps,
1599     gboolean * need_pool)
1600 {
1601   GstStructure *structure;
1602
1603   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
1604
1605   structure = GST_QUERY_STRUCTURE (query);
1606   gst_structure_id_get (structure,
1607       GST_QUARK (CAPS), GST_TYPE_CAPS, caps,
1608       GST_QUARK (NEED_POOL), G_TYPE_BOOLEAN, need_pool, NULL);
1609 }
1610
1611 /**
1612  * gst_query_set_allocation_params
1613  * @query: A valid #GstQuery of type GST_QUERY_ALLOCATION.
1614  * @size: the size
1615  * @min_buffers: the min buffers
1616  * @max_buffers: the max buffers
1617  * @prefix: the prefix
1618  * @alignment: the alignment
1619  * @pool: the #GstBufferPool
1620  *
1621  * Set the allocation parameters in @query.
1622  */
1623 void
1624 gst_query_set_allocation_params (GstQuery * query, guint size,
1625     guint min_buffers, guint max_buffers, guint prefix,
1626     guint alignment, GstBufferPool * pool)
1627 {
1628   GstStructure *structure;
1629
1630   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
1631   g_return_if_fail (gst_query_is_writable (query));
1632   g_return_if_fail (((alignment + 1) & alignment) == 0);
1633   g_return_if_fail (size != 0 || pool == NULL);
1634
1635   structure = GST_QUERY_STRUCTURE (query);
1636   gst_structure_id_set (structure,
1637       GST_QUARK (SIZE), G_TYPE_UINT, size,
1638       GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
1639       GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers,
1640       GST_QUARK (PREFIX), G_TYPE_UINT, prefix,
1641       GST_QUARK (ALIGN), G_TYPE_UINT, alignment,
1642       GST_QUARK (POOL), GST_TYPE_BUFFER_POOL, pool, NULL);
1643 }
1644
1645 /**
1646  * gst_query_parse_allocation_params
1647  * @query: A valid #GstQuery of type GST_QUERY_ALLOCATION.
1648  * @size: the size
1649  * @min_buffers: the min buffers
1650  * @max_buffers: the max buffers
1651  * @prefix: the prefix
1652  * @alignment: the alignment
1653  * @pool: the #GstBufferPool
1654  *
1655  * Get the allocation parameters in @query.
1656  */
1657 void
1658 gst_query_parse_allocation_params (GstQuery * query, guint * size,
1659     guint * min_buffers, guint * max_buffers, guint * prefix,
1660     guint * alignment, GstBufferPool ** pool)
1661 {
1662   GstStructure *structure;
1663
1664   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
1665
1666   structure = GST_QUERY_STRUCTURE (query);
1667   gst_structure_id_get (structure,
1668       GST_QUARK (SIZE), G_TYPE_UINT, size,
1669       GST_QUARK (MIN_BUFFERS), G_TYPE_UINT, min_buffers,
1670       GST_QUARK (MAX_BUFFERS), G_TYPE_UINT, max_buffers,
1671       GST_QUARK (PREFIX), G_TYPE_UINT, prefix,
1672       GST_QUARK (ALIGN), G_TYPE_UINT, alignment,
1673       GST_QUARK (POOL), GST_TYPE_BUFFER_POOL, pool, NULL);
1674 }
1675
1676 /**
1677  * gst_query_add_allocation_meta
1678  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1679  * @api: the metadata API
1680  *
1681  * Add @api as aone of the supported metadata API to @query.
1682  */
1683 void
1684 gst_query_add_allocation_meta (GstQuery * query, const gchar * api)
1685 {
1686   GValueArray *array;
1687   const GValue *value;
1688   GValue api_value = { 0 };
1689   GstStructure *structure;
1690
1691   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
1692   g_return_if_fail (api != NULL);
1693   g_return_if_fail (gst_query_is_writable (query));
1694
1695   structure = GST_QUERY_STRUCTURE (query);
1696   value = gst_structure_id_get_value (structure, GST_QUARK (META));
1697   if (value) {
1698     array = (GValueArray *) g_value_get_boxed (value);
1699   } else {
1700     GValue new_array_val = { 0, };
1701
1702     array = g_value_array_new (0);
1703
1704     g_value_init (&new_array_val, G_TYPE_VALUE_ARRAY);
1705     g_value_take_boxed (&new_array_val, array);
1706
1707     gst_structure_id_take_value (structure, GST_QUARK (META), &new_array_val);
1708   }
1709
1710   g_value_init (&api_value, G_TYPE_STRING);
1711   g_value_set_string (&api_value, api);
1712   g_value_array_append (array, &api_value);
1713   g_value_unset (&api_value);
1714 }
1715
1716 /**
1717  * gst_query_get_n_allocation_metas:
1718  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1719  *
1720  * Retrieve the number of values currently stored in the
1721  * meta API array of the query's structure.
1722  *
1723  * Returns: the metadata API array size as a #guint.
1724  */
1725 guint
1726 gst_query_get_n_allocation_metas (GstQuery * query)
1727 {
1728   GValueArray *array;
1729   const GValue *value;
1730   guint size = 0;
1731   GstStructure *structure;
1732
1733   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION, 0);
1734
1735   structure = GST_QUERY_STRUCTURE (query);
1736   value = gst_structure_id_get_value (structure, GST_QUARK (META));
1737   if (value) {
1738     array = (GValueArray *) g_value_get_boxed (value);
1739     size = array->n_values;
1740   }
1741   return size;
1742 }
1743
1744 /**
1745  * gst_query_parse_nth_allocation_meta
1746  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1747  * @index: position in the metadata API array to read
1748  *
1749  * Parse an available query and get the metadata API
1750  * at @index of the metadata API array.
1751  *
1752  * Returns: a #gchar of the metadata API at @index.
1753  */
1754 const gchar *
1755 gst_query_parse_nth_allocation_meta (GstQuery * query, guint index)
1756 {
1757   const GValue *value;
1758   const gchar *ret = NULL;
1759   GstStructure *structure;
1760
1761   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION, NULL);
1762
1763   structure = GST_QUERY_STRUCTURE (query);
1764   value = gst_structure_id_get_value (structure, GST_QUARK (META));
1765   if (value) {
1766     GValueArray *meta;
1767     GValue *api_value;
1768
1769     meta = (GValueArray *) g_value_get_boxed (value);
1770     api_value = g_value_array_get_nth (meta, index);
1771
1772     if (api_value)
1773       ret = g_value_get_string (api_value);
1774   }
1775   return ret;
1776 }
1777
1778 /**
1779  * gst_query_has_allocation_meta
1780  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1781  * @api: the metadata API
1782  *
1783  * Check if @query has metadata @api set.
1784  *
1785  * Returns: TRUE when @api is in the list of metadata.
1786  */
1787 gboolean
1788 gst_query_has_allocation_meta (GstQuery * query, const gchar * api)
1789 {
1790   const GValue *value;
1791   GstStructure *structure;
1792
1793   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION, FALSE);
1794   g_return_val_if_fail (api != NULL, FALSE);
1795
1796   structure = GST_QUERY_STRUCTURE (query);
1797   value = gst_structure_id_get_value (structure, GST_QUARK (META));
1798   if (value) {
1799     GValueArray *array;
1800     GValue *api_value;
1801     guint i;
1802
1803     array = (GValueArray *) g_value_get_boxed (value);
1804     for (i = 0; i < array->n_values; i++) {
1805       api_value = g_value_array_get_nth (array, i);
1806       if (!strcmp (api, g_value_get_string (api_value)))
1807         return TRUE;
1808     }
1809   }
1810   return FALSE;
1811 }
1812
1813 /**
1814  * gst_query_add_allocation_memory
1815  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1816  * @alloc: the memory allocator
1817  *
1818  * Add @alloc as a supported memory allocator.
1819  */
1820 void
1821 gst_query_add_allocation_memory (GstQuery * query, const gchar * alloc)
1822 {
1823   GValueArray *array;
1824   const GValue *value;
1825   GValue alloc_value = { 0 };
1826   GstStructure *structure;
1827
1828   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
1829   g_return_if_fail (gst_query_is_writable (query));
1830
1831   structure = GST_QUERY_STRUCTURE (query);
1832   value = gst_structure_id_get_value (structure, GST_QUARK (ALLOCATOR));
1833   if (value) {
1834     array = (GValueArray *) g_value_get_boxed (value);
1835   } else {
1836     GValue new_array_val = { 0, };
1837
1838     array = g_value_array_new (0);
1839
1840     g_value_init (&new_array_val, G_TYPE_VALUE_ARRAY);
1841     g_value_take_boxed (&new_array_val, array);
1842
1843     gst_structure_id_take_value (structure, GST_QUARK (ALLOCATOR),
1844         &new_array_val);
1845   }
1846
1847   g_value_init (&alloc_value, G_TYPE_STRING);
1848   g_value_set_string (&alloc_value, alloc);
1849   g_value_array_append (array, &alloc_value);
1850   g_value_unset (&alloc_value);
1851 }
1852
1853 /**
1854  * gst_query_get_n_allocation_memories:
1855  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1856  *
1857  * Retrieve the number of values currently stored in the
1858  * allocator array of the query's structure.
1859  *
1860  * If no memory allocator is specified, the downstream element can handle
1861  * the default memory allocator.
1862  *
1863  * Returns: the allocator array size as a #guint.
1864  */
1865 guint
1866 gst_query_get_n_allocation_memories (GstQuery * query)
1867 {
1868   GValueArray *array;
1869   const GValue *value;
1870   guint size = 0;
1871   GstStructure *structure;
1872
1873   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION, 0);
1874
1875   structure = GST_QUERY_STRUCTURE (query);
1876   value = gst_structure_id_get_value (structure, GST_QUARK (ALLOCATOR));
1877   if (value) {
1878     array = (GValueArray *) g_value_get_boxed (value);
1879     size = array->n_values;
1880   }
1881   return size;
1882 }
1883
1884 /**
1885  * gst_query_parse_nth_allocation_memory
1886  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1887  * @index: position in the allocator array to read
1888  *
1889  * Parse an available query and get the alloctor
1890  * at @index of the allocator array.
1891  *
1892  * Returns: the name of the allocator at @index.
1893  */
1894 const gchar *
1895 gst_query_parse_nth_allocation_memory (GstQuery * query, guint index)
1896 {
1897   const GValue *value;
1898   const gchar *ret = NULL;
1899   GstStructure *structure;
1900
1901   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION, NULL);
1902
1903   structure = GST_QUERY_STRUCTURE (query);
1904   value = gst_structure_id_get_value (structure, GST_QUARK (ALLOCATOR));
1905   if (value) {
1906     GValueArray *memory;
1907     GValue *alloc_value;
1908
1909     memory = (GValueArray *) g_value_get_boxed (value);
1910     alloc_value = g_value_array_get_nth (memory, index);
1911
1912     if (alloc_value)
1913       ret = g_value_get_string (alloc_value);
1914   }
1915   return ret;
1916 }
1917
1918 /**
1919  * gst_query_new_scheduling
1920  *
1921  * Constructs a new query object for querying the scheduling properties.
1922  *
1923  * Free-function: gst_query_unref
1924  *
1925  * Returns: (transfer full): a new #GstQuery
1926  */
1927 GstQuery *
1928 gst_query_new_scheduling (void)
1929 {
1930   GstQuery *query;
1931   GstStructure *structure;
1932
1933   structure = gst_structure_new_id (GST_QUARK (QUERY_SCHEDULING),
1934       GST_QUARK (PULL_MODE), G_TYPE_BOOLEAN, FALSE,
1935       GST_QUARK (RANDOM_ACCESS), G_TYPE_BOOLEAN, FALSE,
1936       GST_QUARK (SEQUENTIAL), G_TYPE_BOOLEAN, TRUE,
1937       GST_QUARK (MINSIZE), G_TYPE_INT, 1,
1938       GST_QUARK (MAXSIZE), G_TYPE_INT, -1,
1939       GST_QUARK (ALIGN), G_TYPE_INT, 1, NULL);
1940   query = gst_query_new (GST_QUERY_SCHEDULING, structure);
1941
1942   return query;
1943 }
1944
1945 /**
1946  * gst_query_set_scheduling
1947  * @query: A valid #GstQuery of type GST_QUERY_SCHEDULING.
1948  * @pull_mode: if pull mode scheduling is supported
1949  * @random_access: if random access is possible
1950  * @sequential: if sequential access is recommended
1951  * @minsize: the suggested minimum size of pull requests
1952  * @maxsize: the suggested maximum size of pull requests
1953  * @align: the suggested alignment of pull requests
1954  *
1955  * Set the scheduling properties.
1956  */
1957 void
1958 gst_query_set_scheduling (GstQuery * query, gboolean pull_mode,
1959     gboolean random_access, gboolean sequential,
1960     gint minsize, gint maxsize, gint align)
1961 {
1962   GstStructure *structure;
1963
1964   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SCHEDULING);
1965   g_return_if_fail (gst_query_is_writable (query));
1966
1967   structure = GST_QUERY_STRUCTURE (query);
1968   gst_structure_id_set (structure,
1969       GST_QUARK (PULL_MODE), G_TYPE_BOOLEAN, pull_mode,
1970       GST_QUARK (RANDOM_ACCESS), G_TYPE_BOOLEAN, random_access,
1971       GST_QUARK (SEQUENTIAL), G_TYPE_BOOLEAN, sequential,
1972       GST_QUARK (MINSIZE), G_TYPE_INT, minsize,
1973       GST_QUARK (MAXSIZE), G_TYPE_INT, maxsize,
1974       GST_QUARK (ALIGN), G_TYPE_INT, align, NULL);
1975 }
1976
1977 /**
1978  * gst_query_parse_scheduling
1979  * @query: A valid #GstQuery of type GST_QUERY_SCHEDULING.
1980  * @pull_mode: if pull mode scheduling is supported
1981  * @random_access: if random access is possible
1982  * @sequential: if sequential access is recommended
1983  * @minsize: the suggested minimum size of pull requests
1984  * @maxsize: the suggested maximum size of pull requests:
1985  * @align: the suggested alignment of pull requests
1986  *
1987  * Set the scheduling properties.
1988  */
1989 void
1990 gst_query_parse_scheduling (GstQuery * query, gboolean * pull_mode,
1991     gboolean * random_access, gboolean * sequential,
1992     gint * minsize, gint * maxsize, gint * align)
1993 {
1994   GstStructure *structure;
1995
1996   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SCHEDULING);
1997
1998   structure = GST_QUERY_STRUCTURE (query);
1999   gst_structure_id_get (structure,
2000       GST_QUARK (PULL_MODE), G_TYPE_BOOLEAN, pull_mode,
2001       GST_QUARK (RANDOM_ACCESS), G_TYPE_BOOLEAN, random_access,
2002       GST_QUARK (SEQUENTIAL), G_TYPE_BOOLEAN, sequential,
2003       GST_QUARK (MINSIZE), G_TYPE_INT, minsize,
2004       GST_QUARK (MAXSIZE), G_TYPE_INT, maxsize,
2005       GST_QUARK (ALIGN), G_TYPE_INT, align, NULL);
2006 }
2007
2008 /**
2009  * gst_query_new_accept_caps
2010  * @caps: a #GstCaps
2011  *
2012  * Constructs a new query object for querying if @caps are accepted.
2013  *
2014  * Free-function: gst_query_unref
2015  *
2016  * Returns: (transfer full): a new #GstQuery
2017  */
2018 GstQuery *
2019 gst_query_new_accept_caps (GstCaps * caps)
2020 {
2021   GstQuery *query;
2022   GstStructure *structure;
2023
2024   structure = gst_structure_new_id (GST_QUARK (QUERY_ACCEPT_CAPS),
2025       GST_QUARK (CAPS), GST_TYPE_CAPS, caps,
2026       GST_QUARK (RESULT), G_TYPE_BOOLEAN, FALSE, NULL);
2027   query = gst_query_new (GST_QUERY_ACCEPT_CAPS, structure);
2028
2029   return query;
2030 }
2031
2032 /**
2033  * gst_query_parse_accept_caps:
2034  * @query: The query to parse
2035  * @caps: (out): A pointer to the caps
2036  *
2037  * Get the caps from @query. The caps remains valid as long as @query remains
2038  * valid.
2039  */
2040 void
2041 gst_query_parse_accept_caps (GstQuery * query, GstCaps ** caps)
2042 {
2043   GstStructure *structure;
2044
2045   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ACCEPT_CAPS);
2046
2047   structure = GST_QUERY_STRUCTURE (query);
2048   *caps = g_value_get_boxed (gst_structure_id_get_value (structure,
2049           GST_QUARK (CAPS)));
2050 }
2051
2052 void
2053 gst_query_set_accept_caps_result (GstQuery * query, gboolean result)
2054 {
2055   GstStructure *structure;
2056
2057   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ACCEPT_CAPS);
2058   g_return_if_fail (gst_query_is_writable (query));
2059
2060   structure = GST_QUERY_STRUCTURE (query);
2061   gst_structure_id_set (structure,
2062       GST_QUARK (RESULT), G_TYPE_BOOLEAN, result, NULL);
2063 }
2064
2065 void
2066 gst_query_parse_accept_caps_result (GstQuery * query, gboolean * result)
2067 {
2068   GstStructure *structure;
2069
2070   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ACCEPT_CAPS);
2071
2072   structure = GST_QUERY_STRUCTURE (query);
2073   gst_structure_id_get (structure,
2074       GST_QUARK (RESULT), G_TYPE_BOOLEAN, result, NULL);
2075 }
2076
2077 /**
2078  * gst_query_new_caps
2079  * @filer: a filter
2080  *
2081  * Constructs a new query object for querying the caps.
2082  *
2083  * The CAPS query should return the* allowable caps for a pad in the context
2084  * of the element's state, its link to other elements, and the devices or files
2085  * it has opened. These caps must be a subset of the pad template caps. In the
2086  * NULL state with no links, the CAPS query should ideally return the same caps
2087  * as the pad template. In rare circumstances, an object property can affect
2088  * the caps returned by the CAPS query, but this is discouraged.
2089  *
2090  * For most filters, the caps returned by CAPS query is directly affected by the
2091  * allowed caps on other pads. For demuxers and decoders, the caps returned by
2092  * the srcpad's getcaps function is directly related to the stream data. Again,
2093  * the CAPS query should return the most specific caps it reasonably can, since this
2094  * helps with autoplugging.
2095  *
2096  * Free-function: gst_query_unref
2097  *
2098  * Returns: (transfer full): a new #GstQuery
2099  */
2100 GstQuery *
2101 gst_query_new_caps (GstCaps * filter)
2102 {
2103   GstQuery *query;
2104   GstStructure *structure;
2105
2106   structure = gst_structure_new_id (GST_QUARK (QUERY_CAPS),
2107       GST_QUARK (FILTER), GST_TYPE_CAPS, filter,
2108       GST_QUARK (CAPS), GST_TYPE_CAPS, NULL, NULL);
2109   query = gst_query_new (GST_QUERY_CAPS, structure);
2110
2111   return query;
2112 }
2113
2114 /**
2115  * gst_query_parse_caps:
2116  * @query: The query to parse
2117  * @filter: (out): A pointer to the caps filter
2118  *
2119  * Get the filter from the caps @query. The caps remains valid as long as
2120  * @query remains valid.
2121  */
2122 void
2123 gst_query_parse_caps (GstQuery * query, GstCaps ** filter)
2124 {
2125   GstStructure *structure;
2126
2127   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CAPS);
2128
2129   structure = GST_QUERY_STRUCTURE (query);
2130   *filter = g_value_get_boxed (gst_structure_id_get_value (structure,
2131           GST_QUARK (FILTER)));
2132 }
2133
2134 /**
2135  * gst_query_set_caps_result:
2136  * @query: The query to use
2137  * @caps: (in): A pointer to the caps
2138  *
2139  * Set the @caps result in @query.
2140  */
2141 void
2142 gst_query_set_caps_result (GstQuery * query, GstCaps * caps)
2143 {
2144   GstStructure *structure;
2145
2146   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CAPS);
2147   g_return_if_fail (gst_query_is_writable (query));
2148
2149   structure = GST_QUERY_STRUCTURE (query);
2150   gst_structure_id_set (structure, GST_QUARK (CAPS), GST_TYPE_CAPS, caps, NULL);
2151 }
2152
2153 /**
2154  * gst_query_parse_caps_result:
2155  * @query: The query to parse
2156  * @caps: (out): A pointer to the caps
2157  *
2158  * Get the caps result from @query. The caps remains valid as long as
2159  * @query remains valid.
2160  */
2161 void
2162 gst_query_parse_caps_result (GstQuery * query, GstCaps ** caps)
2163 {
2164   GstStructure *structure;
2165
2166   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CAPS);
2167
2168   structure = GST_QUERY_STRUCTURE (query);
2169   *caps = g_value_get_boxed (gst_structure_id_get_value (structure,
2170           GST_QUARK (CAPS)));
2171 }
2172
2173 void
2174 gst_query_intersect_caps_result (GstQuery * query, GstCaps * filter,
2175     GstCapsIntersectMode mode)
2176 {
2177   GstCaps *res, *caps;
2178
2179   gst_query_parse_caps_result (query, &caps);
2180   res = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
2181   gst_query_set_caps_result (query, res);
2182   gst_caps_unref (res);
2183 }