miniobject: fix debug
[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
72 GST_DEBUG_CATEGORY_STATIC (gst_query_debug);
73 #define GST_CAT_DEFAULT gst_query_debug
74
75 static GType _gst_query_type = 0;
76
77 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
78 static GList *_gst_queries = NULL;
79 static GHashTable *_nick_to_query = NULL;
80 static GHashTable *_query_type_to_nick = NULL;
81 static guint32 _n_values = 1;   /* we start from 1 because 0 reserved for NONE */
82
83 static GstQueryTypeDefinition standard_definitions[] = {
84   {GST_QUERY_POSITION, "position", "Current position", 0},
85   {GST_QUERY_DURATION, "duration", "Total duration", 0},
86   {GST_QUERY_LATENCY, "latency", "Latency", 0},
87   {GST_QUERY_JITTER, "jitter", "Jitter", 0},
88   {GST_QUERY_RATE, "rate", "Configured rate 1000000 = 1", 0},
89   {GST_QUERY_SEEKING, "seeking", "Seeking capabilities and parameters", 0},
90   {GST_QUERY_SEGMENT, "segment", "currently configured segment", 0},
91   {GST_QUERY_CONVERT, "convert", "Converting between formats", 0},
92   {GST_QUERY_FORMATS, "formats", "Supported formats for conversion", 0},
93   {GST_QUERY_BUFFERING, "buffering", "Buffering status", 0},
94   {GST_QUERY_CUSTOM, "custom", "Custom query", 0},
95   {GST_QUERY_URI, "uri", "URI of the source or sink", 0},
96   {0, NULL, NULL, 0}
97 };
98
99 void
100 _gst_query_initialize (void)
101 {
102   GstQueryTypeDefinition *standards = standard_definitions;
103
104   GST_CAT_INFO (GST_CAT_GST_INIT, "init queries");
105
106   GST_DEBUG_CATEGORY_INIT (gst_query_debug, "query", 0, "query system");
107
108   g_static_mutex_lock (&mutex);
109   if (_nick_to_query == NULL) {
110     _nick_to_query = g_hash_table_new (g_str_hash, g_str_equal);
111     _query_type_to_nick = g_hash_table_new (NULL, NULL);
112   }
113
114   while (standards->nick) {
115     standards->quark = g_quark_from_static_string (standards->nick);
116     g_hash_table_insert (_nick_to_query, (gpointer) standards->nick, standards);
117     g_hash_table_insert (_query_type_to_nick,
118         GINT_TO_POINTER (standards->value), standards);
119
120     _gst_queries = g_list_append (_gst_queries, standards);
121     standards++;
122     _n_values++;
123   }
124   g_static_mutex_unlock (&mutex);
125
126   gst_query_get_type ();
127 }
128
129 /**
130  * gst_query_type_get_name:
131  * @query: the query type
132  *
133  * Get a printable name for the given query type. Do not modify or free.
134  *
135  * Returns: a reference to the static name of the query.
136  */
137 const gchar *
138 gst_query_type_get_name (GstQueryType query)
139 {
140   const GstQueryTypeDefinition *def;
141
142   def = gst_query_type_get_details (query);
143
144   return def->nick;
145 }
146
147 /**
148  * gst_query_type_to_quark:
149  * @query: the query type
150  *
151  * Get the unique quark for the given query type.
152  *
153  * Returns: the quark associated with the query type
154  */
155 GQuark
156 gst_query_type_to_quark (GstQueryType query)
157 {
158   const GstQueryTypeDefinition *def;
159
160   def = gst_query_type_get_details (query);
161
162   return def->quark;
163 }
164
165 GType
166 gst_query_get_type (void)
167 {
168   if (G_UNLIKELY (_gst_query_type == 0)) {
169     _gst_query_type = gst_mini_object_register ("GstQuery");
170   }
171   return _gst_query_type;
172 }
173
174
175 /**
176  * gst_query_type_register:
177  * @nick: The nick of the new query
178  * @description: The description of the new query
179  *
180  * Create a new GstQueryType based on the nick or return an
181  * already registered query with that nick
182  *
183  * Returns: A new GstQueryType or an already registered query
184  * with the same nick.
185  */
186 GstQueryType
187 gst_query_type_register (const gchar * nick, const gchar * description)
188 {
189   GstQueryTypeDefinition *query;
190   GstQueryType lookup;
191
192   g_return_val_if_fail (nick != NULL, 0);
193   g_return_val_if_fail (description != NULL, 0);
194
195   lookup = gst_query_type_get_by_nick (nick);
196   if (lookup != GST_QUERY_NONE)
197     return lookup;
198
199   query = g_slice_new (GstQueryTypeDefinition);
200   query->value = _n_values;
201   query->nick = g_strdup (nick);
202   query->description = g_strdup (description);
203   query->quark = g_quark_from_static_string (query->nick);
204
205   g_static_mutex_lock (&mutex);
206   g_hash_table_insert (_nick_to_query, (gpointer) query->nick, query);
207   g_hash_table_insert (_query_type_to_nick, GINT_TO_POINTER (query->value),
208       query);
209   _gst_queries = g_list_append (_gst_queries, query);
210   _n_values++;
211   g_static_mutex_unlock (&mutex);
212
213   return query->value;
214 }
215
216 /**
217  * gst_query_type_get_by_nick:
218  * @nick: The nick of the query
219  *
220  * Get the query type registered with @nick.
221  *
222  * Returns: The query registered with @nick or #GST_QUERY_NONE
223  * if the query was not registered.
224  */
225 GstQueryType
226 gst_query_type_get_by_nick (const gchar * nick)
227 {
228   GstQueryTypeDefinition *query;
229
230   g_return_val_if_fail (nick != NULL, 0);
231
232   g_static_mutex_lock (&mutex);
233   query = g_hash_table_lookup (_nick_to_query, nick);
234   g_static_mutex_unlock (&mutex);
235
236   if (query != NULL)
237     return query->value;
238   else
239     return GST_QUERY_NONE;
240 }
241
242 /**
243  * gst_query_types_contains:
244  * @types: The query array to search
245  * @type: the #GstQueryType to find
246  *
247  * See if the given #GstQueryType is inside the @types query types array.
248  *
249  * Returns: TRUE if the type is found inside the array
250  */
251 gboolean
252 gst_query_types_contains (const GstQueryType * types, GstQueryType type)
253 {
254   if (!types)
255     return FALSE;
256
257   while (*types) {
258     if (*types == type)
259       return TRUE;
260
261     types++;
262   }
263   return FALSE;
264 }
265
266
267 /**
268  * gst_query_type_get_details:
269  * @type: a #GstQueryType
270  *
271  * Get details about the given #GstQueryType.
272  *
273  * Returns: The #GstQueryTypeDefinition for @type or NULL on failure.
274  */
275 const GstQueryTypeDefinition *
276 gst_query_type_get_details (GstQueryType type)
277 {
278   const GstQueryTypeDefinition *result;
279
280   g_static_mutex_lock (&mutex);
281   result = g_hash_table_lookup (_query_type_to_nick, GINT_TO_POINTER (type));
282   g_static_mutex_unlock (&mutex);
283
284   return result;
285 }
286
287 /**
288  * gst_query_type_iterate_definitions:
289  *
290  * Get a #GstIterator of all the registered query types. The definitions
291  * iterated over are read only.
292  *
293  * Free-function: gst_iterator_free
294  *
295  * Returns: (transfer full): a #GstIterator of #GstQueryTypeDefinition.
296  */
297 GstIterator *
298 gst_query_type_iterate_definitions (void)
299 {
300   GstIterator *result;
301
302   g_static_mutex_lock (&mutex);
303   /* FIXME: register a boxed type for GstQueryTypeDefinition */
304   result = gst_iterator_new_list (G_TYPE_POINTER,
305       g_static_mutex_get_mutex (&mutex), &_n_values, &_gst_queries,
306       NULL, NULL, NULL);
307   g_static_mutex_unlock (&mutex);
308
309   return result;
310 }
311
312 static void
313 _gst_query_free (GstQuery * query)
314 {
315   g_return_if_fail (query != NULL);
316
317   if (query->structure) {
318     gst_structure_set_parent_refcount (query->structure, NULL);
319     gst_structure_free (query->structure);
320   }
321
322   g_slice_free1 (GST_MINI_OBJECT_SIZE (query), query);
323 }
324
325 static GstQuery *gst_query_new (GstQueryType type, GstStructure * structure);
326
327 static GstQuery *
328 _gst_query_copy (GstQuery * query)
329 {
330   GstQuery *copy;
331
332   copy = gst_query_new (query->type, query->structure);
333
334   return copy;
335 }
336
337 static GstQuery *
338 gst_query_new (GstQueryType type, GstStructure * structure)
339 {
340   GstQuery *query;
341
342   query = g_slice_new0 (GstQuery);
343
344   gst_mini_object_init (GST_MINI_OBJECT_CAST (query),
345       _gst_query_type, sizeof (GstQuery));
346
347   query->mini_object.copy = (GstMiniObjectCopyFunction) _gst_query_copy;
348   query->mini_object.free = (GstMiniObjectFreeFunction) _gst_query_free;
349
350   GST_DEBUG ("creating new query %p %d", query, type);
351
352   query->type = type;
353   query->structure = structure;
354
355   if (structure)
356     gst_structure_set_parent_refcount (query->structure,
357         &query->mini_object.refcount);
358
359   return query;
360 }
361
362 /**
363  * gst_query_new_position:
364  * @format: the default #GstFormat for the new query
365  *
366  * Constructs a new query stream position query object. Use gst_query_unref()
367  * when done with it. A position query is used to query the current position
368  * of playback in the streams, in some format.
369  *
370  * Free-function: gst_query_unref
371  *
372  * Returns: (transfer full): a new #GstQuery
373  */
374 GstQuery *
375 gst_query_new_position (GstFormat format)
376 {
377   GstQuery *query;
378   GstStructure *structure;
379
380   structure = gst_structure_id_new (GST_QUARK (QUERY_POSITION),
381       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
382       GST_QUARK (CURRENT), G_TYPE_INT64, G_GINT64_CONSTANT (-1), NULL);
383
384   query = gst_query_new (GST_QUERY_POSITION, structure);
385
386   return query;
387 }
388
389 /**
390  * gst_query_set_position:
391  * @query: a #GstQuery with query type GST_QUERY_POSITION
392  * @format: the requested #GstFormat
393  * @cur: the position to set
394  *
395  * Answer a position query by setting the requested value in the given format.
396  */
397 void
398 gst_query_set_position (GstQuery * query, GstFormat format, gint64 cur)
399 {
400   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_POSITION);
401
402   gst_structure_id_set (query->structure,
403       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
404       GST_QUARK (CURRENT), G_TYPE_INT64, cur, NULL);
405 }
406
407 /**
408  * gst_query_parse_position:
409  * @query: a #GstQuery
410  * @format: (out) (allow-none): the storage for the #GstFormat of the
411  *     position values (may be NULL)
412  * @cur: (out) (allow-none): the storage for the current position (may be NULL)
413  *
414  * Parse a position query, writing the format into @format, and the position
415  * into @cur, if the respective parameters are non-NULL.
416  */
417 void
418 gst_query_parse_position (GstQuery * query, GstFormat * format, gint64 * cur)
419 {
420   GstStructure *structure;
421
422   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_POSITION);
423
424   structure = query->structure;
425   if (format)
426     *format = g_value_get_enum (gst_structure_id_get_value (structure,
427             GST_QUARK (FORMAT)));
428   if (cur)
429     *cur = g_value_get_int64 (gst_structure_id_get_value (structure,
430             GST_QUARK (CURRENT)));
431 }
432
433
434 /**
435  * gst_query_new_duration:
436  * @format: the #GstFormat for this duration query
437  *
438  * Constructs a new stream duration query object to query in the given format.
439  * Use gst_query_unref() when done with it. A duration query will give the
440  * total length of the stream.
441  *
442  * Free-function: gst_query_unref
443  *
444  * Returns: (transfer full): a new #GstQuery
445  */
446 GstQuery *
447 gst_query_new_duration (GstFormat format)
448 {
449   GstQuery *query;
450   GstStructure *structure;
451
452   structure = gst_structure_id_new (GST_QUARK (QUERY_DURATION),
453       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
454       GST_QUARK (DURATION), G_TYPE_INT64, G_GINT64_CONSTANT (-1), NULL);
455
456   query = gst_query_new (GST_QUERY_DURATION, structure);
457
458   return query;
459 }
460
461 /**
462  * gst_query_set_duration:
463  * @query: a #GstQuery
464  * @format: the #GstFormat for the duration
465  * @duration: the duration of the stream
466  *
467  * Answer a duration query by setting the requested value in the given format.
468  */
469 void
470 gst_query_set_duration (GstQuery * query, GstFormat format, gint64 duration)
471 {
472   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_DURATION);
473
474   gst_structure_id_set (query->structure,
475       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
476       GST_QUARK (DURATION), G_TYPE_INT64, duration, NULL);
477 }
478
479 /**
480  * gst_query_parse_duration:
481  * @query: a #GstQuery
482  * @format: (out) (allow-none): the storage for the #GstFormat of the duration
483  *     value, or NULL.
484  * @duration: (out) (allow-none): the storage for the total duration, or NULL.
485  *
486  * Parse a duration query answer. Write the format of the duration into @format,
487  * and the value into @duration, if the respective variables are non-NULL.
488  */
489 void
490 gst_query_parse_duration (GstQuery * query, GstFormat * format,
491     gint64 * duration)
492 {
493   GstStructure *structure;
494
495   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_DURATION);
496
497   structure = query->structure;
498   if (format)
499     *format = g_value_get_enum (gst_structure_id_get_value (structure,
500             GST_QUARK (FORMAT)));
501   if (duration)
502     *duration = g_value_get_int64 (gst_structure_id_get_value (structure,
503             GST_QUARK (DURATION)));
504 }
505
506 /**
507  * gst_query_new_latency:
508  *
509  * Constructs a new latency query object.
510  * Use gst_query_unref() when done with it. A latency query is usually performed
511  * by sinks to compensate for additional latency introduced by elements in the
512  * pipeline.
513  *
514  * Free-function: gst_query_unref
515  *
516  * Returns: (transfer full): a #GstQuery
517  *
518  * Since: 0.10.12
519  */
520 GstQuery *
521 gst_query_new_latency (void)
522 {
523   GstQuery *query;
524   GstStructure *structure;
525
526   structure = gst_structure_id_new (GST_QUARK (QUERY_LATENCY),
527       GST_QUARK (LIVE), G_TYPE_BOOLEAN, FALSE,
528       GST_QUARK (MIN_LATENCY), G_TYPE_UINT64, G_GUINT64_CONSTANT (0),
529       GST_QUARK (MAX_LATENCY), G_TYPE_UINT64, G_GUINT64_CONSTANT (-1), NULL);
530
531   query = gst_query_new (GST_QUERY_LATENCY, structure);
532
533   return query;
534 }
535
536 /**
537  * gst_query_set_latency:
538  * @query: a #GstQuery
539  * @live: if there is a live element upstream
540  * @min_latency: the minimal latency of the live element
541  * @max_latency: the maximal latency of the live element
542  *
543  * Answer a latency query by setting the requested values in the given format.
544  *
545  * Since: 0.10.12
546  */
547 void
548 gst_query_set_latency (GstQuery * query, gboolean live,
549     GstClockTime min_latency, GstClockTime max_latency)
550 {
551   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_LATENCY);
552
553   gst_structure_id_set (query->structure,
554       GST_QUARK (LIVE), G_TYPE_BOOLEAN, live,
555       GST_QUARK (MIN_LATENCY), G_TYPE_UINT64, min_latency,
556       GST_QUARK (MAX_LATENCY), G_TYPE_UINT64, max_latency, NULL);
557 }
558
559 /**
560  * gst_query_parse_latency:
561  * @query: a #GstQuery
562  * @live: (out) (allow-none): storage for live or NULL
563  * @min_latency: (out) (allow-none): the storage for the min latency or NULL
564  * @max_latency: (out) (allow-none): the storage for the max latency or NULL
565  *
566  * Parse a latency query answer.
567  *
568  * Since: 0.10.12
569  */
570 void
571 gst_query_parse_latency (GstQuery * query, gboolean * live,
572     GstClockTime * min_latency, GstClockTime * max_latency)
573 {
574   GstStructure *structure;
575
576   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_LATENCY);
577
578   structure = query->structure;
579   if (live)
580     *live =
581         g_value_get_boolean (gst_structure_id_get_value (structure,
582             GST_QUARK (LIVE)));
583   if (min_latency)
584     *min_latency = g_value_get_uint64 (gst_structure_id_get_value (structure,
585             GST_QUARK (MIN_LATENCY)));
586   if (max_latency)
587     *max_latency = g_value_get_uint64 (gst_structure_id_get_value (structure,
588             GST_QUARK (MAX_LATENCY)));
589 }
590
591 /**
592  * gst_query_new_convert:
593  * @src_format: the source #GstFormat for the new query
594  * @value: the value to convert
595  * @dest_format: the target #GstFormat
596  *
597  * Constructs a new convert query object. Use gst_query_unref()
598  * when done with it. A convert query is used to ask for a conversion between
599  * one format and another.
600  *
601  * Free-function: gst_query_unref
602  *
603  * Returns: (transfer full): a #GstQuery
604  */
605 GstQuery *
606 gst_query_new_convert (GstFormat src_format, gint64 value,
607     GstFormat dest_format)
608 {
609   GstQuery *query;
610   GstStructure *structure;
611
612   structure = gst_structure_id_new (GST_QUARK (QUERY_CONVERT),
613       GST_QUARK (SRC_FORMAT), GST_TYPE_FORMAT, src_format,
614       GST_QUARK (SRC_VALUE), G_TYPE_INT64, value,
615       GST_QUARK (DEST_FORMAT), GST_TYPE_FORMAT, dest_format,
616       GST_QUARK (DEST_VALUE), G_TYPE_INT64, G_GINT64_CONSTANT (-1), NULL);
617
618   query = gst_query_new (GST_QUERY_CONVERT, structure);
619
620   return query;
621 }
622
623 /**
624  * gst_query_set_convert:
625  * @query: a #GstQuery
626  * @src_format: the source #GstFormat
627  * @src_value: the source value
628  * @dest_format: the destination #GstFormat
629  * @dest_value: the destination value
630  *
631  * Answer a convert query by setting the requested values.
632  */
633 void
634 gst_query_set_convert (GstQuery * query, GstFormat src_format, gint64 src_value,
635     GstFormat dest_format, gint64 dest_value)
636 {
637   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CONVERT);
638
639   gst_structure_id_set (query->structure,
640       GST_QUARK (SRC_FORMAT), GST_TYPE_FORMAT, src_format,
641       GST_QUARK (SRC_VALUE), G_TYPE_INT64, src_value,
642       GST_QUARK (DEST_FORMAT), GST_TYPE_FORMAT, dest_format,
643       GST_QUARK (DEST_VALUE), G_TYPE_INT64, dest_value, NULL);
644 }
645
646 /**
647  * gst_query_parse_convert:
648  * @query: a #GstQuery
649  * @src_format: (out) (allow-none): the storage for the #GstFormat of the
650  *     source value, or NULL
651  * @src_value: (out) (allow-none): the storage for the source value, or NULL
652  * @dest_format: (out) (allow-none): the storage for the #GstFormat of the
653  *     destination value, or NULL
654  * @dest_value: (out) (allow-none): the storage for the destination value,
655  *     or NULL
656  *
657  * Parse a convert query answer. Any of @src_format, @src_value, @dest_format,
658  * and @dest_value may be NULL, in which case that value is omitted.
659  */
660 void
661 gst_query_parse_convert (GstQuery * query, GstFormat * src_format,
662     gint64 * src_value, GstFormat * dest_format, gint64 * dest_value)
663 {
664   GstStructure *structure;
665
666   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CONVERT);
667
668   structure = query->structure;
669   if (src_format)
670     *src_format = g_value_get_enum (gst_structure_id_get_value (structure,
671             GST_QUARK (SRC_FORMAT)));
672   if (src_value)
673     *src_value = g_value_get_int64 (gst_structure_id_get_value (structure,
674             GST_QUARK (SRC_VALUE)));
675   if (dest_format)
676     *dest_format = g_value_get_enum (gst_structure_id_get_value (structure,
677             GST_QUARK (DEST_FORMAT)));
678   if (dest_value)
679     *dest_value = g_value_get_int64 (gst_structure_id_get_value (structure,
680             GST_QUARK (DEST_VALUE)));
681 }
682
683 /**
684  * gst_query_new_segment:
685  * @format: the #GstFormat for the new query
686  *
687  * Constructs a new segment query object. Use gst_query_unref()
688  * when done with it. A segment query is used to discover information about the
689  * currently configured segment for playback.
690  *
691  * Free-function: gst_query_unref
692  *
693  * Returns: (transfer full): a new #GstQuery
694  */
695 GstQuery *
696 gst_query_new_segment (GstFormat format)
697 {
698   GstQuery *query;
699   GstStructure *structure;
700
701   structure = gst_structure_id_new (GST_QUARK (QUERY_SEGMENT),
702       GST_QUARK (RATE), G_TYPE_DOUBLE, (gdouble) 0.0,
703       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
704       GST_QUARK (START_VALUE), G_TYPE_INT64, G_GINT64_CONSTANT (-1),
705       GST_QUARK (STOP_VALUE), G_TYPE_INT64, G_GINT64_CONSTANT (-1), NULL);
706
707   query = gst_query_new (GST_QUERY_SEGMENT, structure);
708
709   return query;
710 }
711
712 /**
713  * gst_query_set_segment:
714  * @query: a #GstQuery
715  * @rate: the rate of the segment
716  * @format: the #GstFormat of the segment values (@start_value and @stop_value)
717  * @start_value: the start value
718  * @stop_value: the stop value
719  *
720  * Answer a segment query by setting the requested values. The normal
721  * playback segment of a pipeline is 0 to duration at the default rate of
722  * 1.0. If a seek was performed on the pipeline to play a different
723  * segment, this query will return the range specified in the last seek.
724  *
725  * @start_value and @stop_value will respectively contain the configured
726  * playback range start and stop values expressed in @format.
727  * The values are always between 0 and the duration of the media and
728  * @start_value <= @stop_value. @rate will contain the playback rate. For
729  * negative rates, playback will actually happen from @stop_value to
730  * @start_value.
731  */
732 void
733 gst_query_set_segment (GstQuery * query, gdouble rate, GstFormat format,
734     gint64 start_value, gint64 stop_value)
735 {
736   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SEGMENT);
737
738   gst_structure_id_set (query->structure,
739       GST_QUARK (RATE), G_TYPE_DOUBLE, rate,
740       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
741       GST_QUARK (START_VALUE), G_TYPE_INT64, start_value,
742       GST_QUARK (STOP_VALUE), G_TYPE_INT64, stop_value, NULL);
743 }
744
745 /**
746  * gst_query_parse_segment:
747  * @query: a #GstQuery
748  * @rate: (out) (allow-none): the storage for the rate of the segment, or NULL
749  * @format: (out) (allow-none): the storage for the #GstFormat of the values,
750  *     or NULL
751  * @start_value: (out) (allow-none): the storage for the start value, or NULL
752  * @stop_value: (out) (allow-none): the storage for the stop value, or NULL
753  *
754  * Parse a segment query answer. Any of @rate, @format, @start_value, and
755  * @stop_value may be NULL, which will cause this value to be omitted.
756  *
757  * See gst_query_set_segment() for an explanation of the function arguments.
758  */
759 void
760 gst_query_parse_segment (GstQuery * query, gdouble * rate, GstFormat * format,
761     gint64 * start_value, gint64 * stop_value)
762 {
763   GstStructure *structure;
764
765   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SEGMENT);
766
767   structure = query->structure;
768   if (rate)
769     *rate = g_value_get_double (gst_structure_id_get_value (structure,
770             GST_QUARK (RATE)));
771   if (format)
772     *format = g_value_get_enum (gst_structure_id_get_value (structure,
773             GST_QUARK (FORMAT)));
774   if (start_value)
775     *start_value = g_value_get_int64 (gst_structure_id_get_value (structure,
776             GST_QUARK (START_VALUE)));
777   if (stop_value)
778     *stop_value = g_value_get_int64 (gst_structure_id_get_value (structure,
779             GST_QUARK (STOP_VALUE)));
780 }
781
782 /**
783  * gst_query_new_application:
784  * @type: the query type
785  * @structure: a structure for the query
786  *
787  * Constructs a new custom application query object. Use gst_query_unref()
788  * when done with it.
789  *
790  * Free-function: gst_query_unref
791  *
792  * Returns: (transfer full): a new #GstQuery
793  */
794 GstQuery *
795 gst_query_new_application (GstQueryType type, GstStructure * structure)
796 {
797   g_return_val_if_fail (gst_query_type_get_details (type) != NULL, NULL);
798   g_return_val_if_fail (structure != NULL, NULL);
799
800   return gst_query_new (type, structure);
801 }
802
803 /**
804  * gst_query_get_structure:
805  * @query: a #GstQuery
806  *
807  * Get the structure of a query.
808  *
809  * Returns: (transfer none): the #GstStructure of the query. The structure is
810  *     still owned by the query and will therefore be freed when the query
811  *     is unreffed.
812  */
813 GstStructure *
814 gst_query_get_structure (GstQuery * query)
815 {
816   g_return_val_if_fail (GST_IS_QUERY (query), NULL);
817
818   return query->structure;
819 }
820
821 /**
822  * gst_query_new_seeking:
823  * @format: the default #GstFormat for the new query
824  *
825  * Constructs a new query object for querying seeking properties of
826  * the stream.
827  *
828  * Free-function: gst_query_unref
829  *
830  * Returns: (transfer full): a new #GstQuery
831  */
832 GstQuery *
833 gst_query_new_seeking (GstFormat format)
834 {
835   GstQuery *query;
836   GstStructure *structure;
837
838   structure = gst_structure_id_new (GST_QUARK (QUERY_SEEKING),
839       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
840       GST_QUARK (SEEKABLE), G_TYPE_BOOLEAN, FALSE,
841       GST_QUARK (SEGMENT_START), G_TYPE_INT64, G_GINT64_CONSTANT (-1),
842       GST_QUARK (SEGMENT_END), G_TYPE_INT64, G_GINT64_CONSTANT (-1), NULL);
843
844   query = gst_query_new (GST_QUERY_SEEKING, structure);
845
846   return query;
847 }
848
849 /**
850  * gst_query_set_seeking:
851  * @query: a #GstQuery
852  * @format: the format to set for the @segment_start and @segment_end values
853  * @seekable: the seekable flag to set
854  * @segment_start: the segment_start to set
855  * @segment_end: the segment_end to set
856  *
857  * Set the seeking query result fields in @query.
858  */
859 void
860 gst_query_set_seeking (GstQuery * query, GstFormat format,
861     gboolean seekable, gint64 segment_start, gint64 segment_end)
862 {
863   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SEEKING);
864
865   gst_structure_id_set (query->structure,
866       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
867       GST_QUARK (SEEKABLE), G_TYPE_BOOLEAN, seekable,
868       GST_QUARK (SEGMENT_START), G_TYPE_INT64, segment_start,
869       GST_QUARK (SEGMENT_END), G_TYPE_INT64, segment_end, NULL);
870 }
871
872 /**
873  * gst_query_parse_seeking:
874  * @query: a GST_QUERY_SEEKING type query #GstQuery
875  * @format: (out) (allow-none): the format to set for the @segment_start
876  *     and @segment_end values, or NULL
877  * @seekable: (out) (allow-none): the seekable flag to set, or NULL
878  * @segment_start: (out) (allow-none): the segment_start to set, or NULL
879  * @segment_end: (out) (allow-none): the segment_end to set, or NULL
880  *
881  * Parse a seeking query, writing the format into @format, and
882  * other results into the passed parameters, if the respective parameters
883  * are non-NULL
884  */
885 void
886 gst_query_parse_seeking (GstQuery * query, GstFormat * format,
887     gboolean * seekable, gint64 * segment_start, gint64 * segment_end)
888 {
889   GstStructure *structure;
890
891   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SEEKING);
892
893   structure = query->structure;
894   if (format)
895     *format = g_value_get_enum (gst_structure_id_get_value (structure,
896             GST_QUARK (FORMAT)));
897   if (seekable)
898     *seekable = g_value_get_boolean (gst_structure_id_get_value (structure,
899             GST_QUARK (SEEKABLE)));
900   if (segment_start)
901     *segment_start = g_value_get_int64 (gst_structure_id_get_value (structure,
902             GST_QUARK (SEGMENT_START)));
903   if (segment_end)
904     *segment_end = g_value_get_int64 (gst_structure_id_get_value (structure,
905             GST_QUARK (SEGMENT_END)));
906 }
907
908 /**
909  * gst_query_new_formats:
910  *
911  * Constructs a new query object for querying formats of
912  * the stream.
913  *
914  * Free-function: gst_query_unref
915  *
916  * Returns: (transfer full): a new #GstQuery
917  *
918  * Since: 0.10.4
919  */
920 GstQuery *
921 gst_query_new_formats (void)
922 {
923   GstQuery *query;
924   GstStructure *structure;
925
926   structure = gst_structure_id_empty_new (GST_QUARK (QUERY_FORMATS));
927   query = gst_query_new (GST_QUERY_FORMATS, structure);
928
929   return query;
930 }
931
932 static void
933 gst_query_list_add_format (GValue * list, GstFormat format)
934 {
935   GValue item = { 0, };
936
937   g_value_init (&item, GST_TYPE_FORMAT);
938   g_value_set_enum (&item, format);
939   gst_value_list_append_value (list, &item);
940   g_value_unset (&item);
941 }
942
943 /**
944  * gst_query_set_formats:
945  * @query: a #GstQuery
946  * @n_formats: the number of formats to set.
947  * @...: A number of @GstFormats equal to @n_formats.
948  *
949  * Set the formats query result fields in @query. The number of formats passed
950  * must be equal to @n_formats.
951  */
952 void
953 gst_query_set_formats (GstQuery * query, gint n_formats, ...)
954 {
955   va_list ap;
956   GValue list = { 0, };
957   gint i;
958
959   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_FORMATS);
960
961   g_value_init (&list, GST_TYPE_LIST);
962
963   va_start (ap, n_formats);
964   for (i = 0; i < n_formats; i++) {
965     gst_query_list_add_format (&list, va_arg (ap, GstFormat));
966   }
967   va_end (ap);
968
969   gst_structure_set_value (query->structure, "formats", &list);
970
971   g_value_unset (&list);
972
973 }
974
975 /**
976  * gst_query_set_formatsv:
977  * @query: a #GstQuery
978  * @n_formats: the number of formats to set.
979  * @formats: (in) (array length=n_formats): an array containing @n_formats
980  *     @GstFormat values.
981  *
982  * Set the formats query result fields in @query. The number of formats passed
983  * in the @formats array must be equal to @n_formats.
984  *
985  * Since: 0.10.4
986  */
987 void
988 gst_query_set_formatsv (GstQuery * query, gint n_formats, GstFormat * formats)
989 {
990   GValue list = { 0, };
991   gint i;
992
993   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_FORMATS);
994
995   g_value_init (&list, GST_TYPE_LIST);
996   for (i = 0; i < n_formats; i++) {
997     gst_query_list_add_format (&list, formats[i]);
998   }
999   gst_structure_set_value (query->structure, "formats", &list);
1000
1001   g_value_unset (&list);
1002 }
1003
1004 /**
1005  * gst_query_parse_formats_length:
1006  * @query: a #GstQuery
1007  * @n_formats: (out): the number of formats in this query.
1008  *
1009  * Parse the number of formats in the formats @query.
1010  *
1011  * Since: 0.10.4
1012  */
1013 void
1014 gst_query_parse_formats_length (GstQuery * query, guint * n_formats)
1015 {
1016   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_FORMATS);
1017
1018   if (n_formats) {
1019     const GValue *list;
1020
1021     list = gst_structure_get_value (query->structure, "formats");
1022     if (list == NULL)
1023       *n_formats = 0;
1024     else
1025       *n_formats = gst_value_list_get_size (list);
1026   }
1027 }
1028
1029 /**
1030  * gst_query_parse_formats_nth:
1031  * @query: a #GstQuery
1032  * @nth: (out): the nth format to retrieve.
1033  * @format: (out): a pointer to store the nth format
1034  *
1035  * Parse the format query and retrieve the @nth format from it into
1036  * @format. If the list contains less elements than @nth, @format will be
1037  * set to GST_FORMAT_UNDEFINED.
1038  *
1039  * Since: 0.10.4
1040  */
1041 void
1042 gst_query_parse_formats_nth (GstQuery * query, guint nth, GstFormat * format)
1043 {
1044   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_FORMATS);
1045
1046   if (format) {
1047     const GValue *list;
1048
1049     list = gst_structure_get_value (query->structure, "formats");
1050     if (list == NULL) {
1051       *format = GST_FORMAT_UNDEFINED;
1052     } else {
1053       if (nth < gst_value_list_get_size (list)) {
1054         *format = g_value_get_enum (gst_value_list_get_value (list, nth));
1055       } else
1056         *format = GST_FORMAT_UNDEFINED;
1057     }
1058   }
1059 }
1060
1061 /**
1062  * gst_query_new_buffering
1063  * @format: the default #GstFormat for the new query
1064  *
1065  * Constructs a new query object for querying the buffering status of
1066  * a stream.
1067  *
1068  * Free-function: gst_query_new
1069  *
1070  * Returns: (transfer full): a new #GstQuery
1071  *
1072  * Since: 0.10.20
1073  */
1074 GstQuery *
1075 gst_query_new_buffering (GstFormat format)
1076 {
1077   GstQuery *query;
1078   GstStructure *structure;
1079
1080   /* by default, we configure the answer as no buffering with a 100% buffering
1081    * progress */
1082   structure = gst_structure_id_new (GST_QUARK (QUERY_BUFFERING),
1083       GST_QUARK (BUSY), G_TYPE_BOOLEAN, FALSE,
1084       GST_QUARK (BUFFER_PERCENT), G_TYPE_INT, 100,
1085       GST_QUARK (BUFFERING_MODE), GST_TYPE_BUFFERING_MODE, GST_BUFFERING_STREAM,
1086       GST_QUARK (AVG_IN_RATE), G_TYPE_INT, -1,
1087       GST_QUARK (AVG_OUT_RATE), G_TYPE_INT, -1,
1088       GST_QUARK (BUFFERING_LEFT), G_TYPE_INT64, G_GINT64_CONSTANT (0),
1089       GST_QUARK (ESTIMATED_TOTAL), G_TYPE_INT64, G_GINT64_CONSTANT (-1),
1090       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
1091       GST_QUARK (START_VALUE), G_TYPE_INT64, G_GINT64_CONSTANT (-1),
1092       GST_QUARK (STOP_VALUE), G_TYPE_INT64, G_GINT64_CONSTANT (-1), NULL);
1093
1094   query = gst_query_new (GST_QUERY_BUFFERING, structure);
1095
1096   return query;
1097 }
1098
1099 /**
1100  * gst_query_set_buffering_percent
1101  * @query: A valid #GstQuery of type GST_QUERY_BUFFERING.
1102  * @busy: if buffering is busy
1103  * @percent: a buffering percent
1104  *
1105  * Set the percentage of buffered data. This is a value between 0 and 100.
1106  * The @busy indicator is %TRUE when the buffering is in progress.
1107  *
1108  * Since: 0.10.20
1109  */
1110 void
1111 gst_query_set_buffering_percent (GstQuery * query, gboolean busy, gint percent)
1112 {
1113   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING);
1114   g_return_if_fail (percent >= 0 && percent <= 100);
1115
1116   gst_structure_id_set (query->structure,
1117       GST_QUARK (BUSY), G_TYPE_BOOLEAN, busy,
1118       GST_QUARK (BUFFER_PERCENT), G_TYPE_INT, percent, NULL);
1119 }
1120
1121 /**
1122  * gst_query_parse_buffering_percent
1123  * @query: A valid #GstQuery of type GST_QUERY_BUFFERING.
1124  * @busy: (out) (allow-none): if buffering is busy, or NULL
1125  * @percent: (out) (allow-none): a buffering percent, or NULL
1126  *
1127  * Get the percentage of buffered data. This is a value between 0 and 100.
1128  * The @busy indicator is %TRUE when the buffering is in progress.
1129  *
1130  * Since: 0.10.20
1131  */
1132 void
1133 gst_query_parse_buffering_percent (GstQuery * query, gboolean * busy,
1134     gint * percent)
1135 {
1136   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING);
1137
1138   if (busy)
1139     *busy = g_value_get_boolean (gst_structure_id_get_value (query->structure,
1140             GST_QUARK (BUSY)));
1141   if (percent)
1142     *percent = g_value_get_int (gst_structure_id_get_value (query->structure,
1143             GST_QUARK (BUFFER_PERCENT)));
1144 }
1145
1146 /**
1147  * gst_query_set_buffering_stats:
1148  * @query: A valid #GstQuery of type GST_QUERY_BUFFERING.
1149  * @mode: a buffering mode
1150  * @avg_in: the average input rate
1151  * @avg_out: the average output rate
1152  * @buffering_left: amount of buffering time left
1153  *
1154  * Configures the buffering stats values in @query.
1155  *
1156  * Since: 0.10.20
1157  */
1158 void
1159 gst_query_set_buffering_stats (GstQuery * query, GstBufferingMode mode,
1160     gint avg_in, gint avg_out, gint64 buffering_left)
1161 {
1162   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING);
1163
1164   gst_structure_id_set (query->structure,
1165       GST_QUARK (BUFFERING_MODE), GST_TYPE_BUFFERING_MODE, mode,
1166       GST_QUARK (AVG_IN_RATE), G_TYPE_INT, avg_in,
1167       GST_QUARK (AVG_OUT_RATE), G_TYPE_INT, avg_out,
1168       GST_QUARK (BUFFERING_LEFT), G_TYPE_INT64, buffering_left, NULL);
1169 }
1170
1171 /**
1172  * gst_query_parse_buffering_stats:
1173  * @query: A valid #GstQuery of type GST_QUERY_BUFFERING.
1174  * @mode: (out) (allow-none): a buffering mode, or NULL
1175  * @avg_in: (out) (allow-none): the average input rate, or NULL
1176  * @avg_out: (out) (allow-none): the average output rat, or NULLe
1177  * @buffering_left: (out) (allow-none): amount of buffering time left, or NULL
1178  *
1179  * Extracts the buffering stats values from @query.
1180  *
1181  * Since: 0.10.20
1182  */
1183 void
1184 gst_query_parse_buffering_stats (GstQuery * query,
1185     GstBufferingMode * mode, gint * avg_in, gint * avg_out,
1186     gint64 * buffering_left)
1187 {
1188   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING);
1189
1190   if (mode)
1191     *mode = g_value_get_enum (gst_structure_id_get_value (query->structure,
1192             GST_QUARK (BUFFERING_MODE)));
1193   if (avg_in)
1194     *avg_in = g_value_get_int (gst_structure_id_get_value (query->structure,
1195             GST_QUARK (AVG_IN_RATE)));
1196   if (avg_out)
1197     *avg_out = g_value_get_int (gst_structure_id_get_value (query->structure,
1198             GST_QUARK (AVG_OUT_RATE)));
1199   if (buffering_left)
1200     *buffering_left =
1201         g_value_get_int64 (gst_structure_id_get_value (query->structure,
1202             GST_QUARK (BUFFERING_LEFT)));
1203 }
1204
1205
1206 /**
1207  * gst_query_set_buffering_range:
1208  * @query: a #GstQuery
1209  * @format: the format to set for the @start and @stop values
1210  * @start: the start to set
1211  * @stop: the stop to set
1212  * @estimated_total: estimated total amount of download time
1213  *
1214  * Set the available query result fields in @query.
1215  *
1216  * Since: 0.10.20
1217  */
1218 void
1219 gst_query_set_buffering_range (GstQuery * query, GstFormat format,
1220     gint64 start, gint64 stop, gint64 estimated_total)
1221 {
1222   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING);
1223
1224   gst_structure_id_set (query->structure,
1225       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
1226       GST_QUARK (START_VALUE), G_TYPE_INT64, start,
1227       GST_QUARK (STOP_VALUE), G_TYPE_INT64, stop,
1228       GST_QUARK (ESTIMATED_TOTAL), G_TYPE_INT64, estimated_total, NULL);
1229 }
1230
1231 /**
1232  * gst_query_parse_buffering_range:
1233  * @query: a GST_QUERY_BUFFERING type query #GstQuery
1234  * @format: (out) (allow-none): the format to set for the @segment_start
1235  *     and @segment_end values, or NULL
1236  * @start: (out) (allow-none): the start to set, or NULL
1237  * @stop: (out) (allow-none): the stop to set, or NULL
1238  * @estimated_total: (out) (allow-none): estimated total amount of download
1239  *     time, or NULL
1240  *
1241  * Parse an available query, writing the format into @format, and
1242  * other results into the passed parameters, if the respective parameters
1243  * are non-NULL
1244  *
1245  * Since: 0.10.20
1246  */
1247 void
1248 gst_query_parse_buffering_range (GstQuery * query, GstFormat * format,
1249     gint64 * start, gint64 * stop, gint64 * estimated_total)
1250 {
1251   GstStructure *structure;
1252
1253   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING);
1254
1255   structure = query->structure;
1256   if (format)
1257     *format = g_value_get_enum (gst_structure_id_get_value (structure,
1258             GST_QUARK (FORMAT)));
1259   if (start)
1260     *start = g_value_get_int64 (gst_structure_id_get_value (structure,
1261             GST_QUARK (START_VALUE)));
1262   if (stop)
1263     *stop = g_value_get_int64 (gst_structure_id_get_value (structure,
1264             GST_QUARK (STOP_VALUE)));
1265   if (estimated_total)
1266     *estimated_total =
1267         g_value_get_int64 (gst_structure_id_get_value (structure,
1268             GST_QUARK (ESTIMATED_TOTAL)));
1269 }
1270
1271 /**
1272  * gst_query_add_buffering_range
1273  * @query: a GST_QUERY_BUFFERING type query #GstQuery
1274  * @start: start position of the range
1275  * @stop: stop position of the range
1276  *
1277  * Set the buffering-ranges array field in @query. The current last
1278  * start position of the array should be inferior to @start.
1279  *
1280  * Returns: a #gboolean indicating if the range was added or not.
1281  *
1282  * Since: 0.10.31
1283  */
1284 gboolean
1285 gst_query_add_buffering_range (GstQuery * query, gint64 start, gint64 stop)
1286 {
1287   GValueArray *array;
1288   GValue *last_array_value;
1289   const GValue *value;
1290   GValue range_value = { 0 };
1291
1292   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING, FALSE);
1293
1294   if (G_UNLIKELY (start >= stop))
1295     return FALSE;
1296
1297   value =
1298       gst_structure_id_get_value (query->structure,
1299       GST_QUARK (BUFFERING_RANGES));
1300   if (value) {
1301     array = (GValueArray *) g_value_get_boxed (value);
1302     last_array_value = g_value_array_get_nth (array, array->n_values - 1);
1303     if (G_UNLIKELY (start <= gst_value_get_int64_range_min (last_array_value)))
1304       return FALSE;
1305   } else {
1306     GValue new_array_val = { 0, };
1307
1308     array = g_value_array_new (0);
1309
1310     g_value_init (&new_array_val, G_TYPE_VALUE_ARRAY);
1311     g_value_take_boxed (&new_array_val, array);
1312
1313     /* set the value array only once, so we then modify (append to) the
1314      * existing value array owned by the GstStructure / the field's GValue */
1315     gst_structure_id_take_value (query->structure, GST_QUARK (BUFFERING_RANGES),
1316         &new_array_val);
1317   }
1318
1319   g_value_init (&range_value, GST_TYPE_INT64_RANGE);
1320   gst_value_set_int64_range (&range_value, start, stop);
1321   g_value_array_append (array, &range_value);
1322   /* skip the g_value_unset(&range_value) here, we know it's not needed */
1323
1324   return TRUE;
1325 }
1326
1327 /**
1328  * gst_query_get_n_buffering_ranges
1329  * @query: a GST_QUERY_BUFFERING type query #GstQuery
1330  *
1331  * Retrieve the number of values currently stored in the
1332  * buffered-ranges array of the query's structure.
1333  *
1334  * Returns: the range array size as a #guint.
1335  *
1336  * Since: 0.10.31
1337  */
1338 guint
1339 gst_query_get_n_buffering_ranges (GstQuery * query)
1340 {
1341   GValueArray *array;
1342   const GValue *value;
1343   guint size = 0;
1344
1345   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING, 0);
1346
1347   value =
1348       gst_structure_id_get_value (query->structure,
1349       GST_QUARK (BUFFERING_RANGES));
1350   if (value) {
1351     array = (GValueArray *) g_value_get_boxed (value);
1352     size = array->n_values;
1353   }
1354   return size;
1355 }
1356
1357
1358 /**
1359  * gst_query_parse_nth_buffering_range
1360  * @query: a GST_QUERY_BUFFERING type query #GstQuery
1361  * @index: position in the buffered-ranges array to read
1362  * @start: (out) (allow-none): the start position to set, or NULL
1363  * @stop: (out) (allow-none): the stop position to set, or NULL
1364  *
1365  * Parse an available query and get the start and stop values stored
1366  * at the @index of the buffered ranges array.
1367  *
1368  * Returns: a #gboolean indicating if the parsing succeeded.
1369  *
1370  * Since: 0.10.31
1371  */
1372 gboolean
1373 gst_query_parse_nth_buffering_range (GstQuery * query, guint index,
1374     gint64 * start, gint64 * stop)
1375 {
1376   const GValue *value;
1377   GValueArray *ranges;
1378   GValue *range_value;
1379   gboolean ret = FALSE;
1380
1381   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING, ret);
1382
1383   value =
1384       gst_structure_id_get_value (query->structure,
1385       GST_QUARK (BUFFERING_RANGES));
1386   ranges = (GValueArray *) g_value_get_boxed (value);
1387   range_value = g_value_array_get_nth (ranges, index);
1388   if (range_value) {
1389     if (start)
1390       *start = gst_value_get_int64_range_min (range_value);
1391     if (stop)
1392       *stop = gst_value_get_int64_range_max (range_value);
1393     ret = TRUE;
1394   }
1395
1396   return ret;
1397 }
1398
1399
1400 /**
1401  * gst_query_new_uri:
1402  *
1403  * Constructs a new query URI query object. Use gst_query_unref()
1404  * when done with it. An URI query is used to query the current URI
1405  * that is used by the source or sink.
1406  *
1407  * Free-function: gst_query_unref
1408  *
1409  * Returns: (transfer full): a new #GstQuery
1410  *
1411  * Since: 0.10.22
1412  */
1413 GstQuery *
1414 gst_query_new_uri (void)
1415 {
1416   GstQuery *query;
1417   GstStructure *structure;
1418
1419   structure = gst_structure_id_new (GST_QUARK (QUERY_URI),
1420       GST_QUARK (URI), G_TYPE_STRING, NULL, NULL);
1421
1422   query = gst_query_new (GST_QUERY_URI, structure);
1423
1424   return query;
1425 }
1426
1427 /**
1428  * gst_query_set_uri:
1429  * @query: a #GstQuery with query type GST_QUERY_URI
1430  * @uri: the URI to set
1431  *
1432  * Answer a URI query by setting the requested URI.
1433  *
1434  * Since: 0.10.22
1435  */
1436 void
1437 gst_query_set_uri (GstQuery * query, const gchar * uri)
1438 {
1439   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_URI);
1440   g_return_if_fail (gst_uri_is_valid (uri));
1441
1442   gst_structure_id_set (query->structure, GST_QUARK (URI), G_TYPE_STRING, uri,
1443       NULL);
1444 }
1445
1446 /**
1447  * gst_query_parse_uri:
1448  * @query: a #GstQuery
1449  * @uri: (out callee-allocates) (allow-none): the storage for the current URI
1450  *     (may be NULL)
1451  *
1452  * Parse an URI query, writing the URI into @uri as a newly
1453  * allocated string, if the respective parameters are non-NULL.
1454  * Free the string with g_free() after usage.
1455  *
1456  * Since: 0.10.22
1457  */
1458 void
1459 gst_query_parse_uri (GstQuery * query, gchar ** uri)
1460 {
1461   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_URI);
1462
1463   if (uri)
1464     *uri = g_value_dup_string (gst_structure_id_get_value (query->structure,
1465             GST_QUARK (URI)));
1466 }