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