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