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