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