Merge branch 'upstream/1.16' into tizen_gst_1.16.2
[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., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23
24 /**
25  * SECTION:gstquery
26  * @title: GstQuery
27  * @short_description: Provide functions to create queries, and to set and parse
28  *                     values in them.
29  * @see_also: #GstPad, #GstElement
30  *
31  * Queries can be performed on pads (gst_pad_query()) and elements
32  * (gst_element_query()). Please note that some queries might need a running
33  * pipeline to work.
34  *
35  * Queries can be created using the gst_query_new_*() functions.
36  * Query values can be set using gst_query_set_*(), and parsed using
37  * gst_query_parse_*() helpers.
38  *
39  * The following example shows how to query the duration of a pipeline:
40  * |[<!-- language="C" -->
41  *   GstQuery *query;
42  *   gboolean res;
43  *   query = gst_query_new_duration (GST_FORMAT_TIME);
44  *   res = gst_element_query (pipeline, query);
45  *   if (res) {
46  *     gint64 duration;
47  *     gst_query_parse_duration (query, NULL, &amp;duration);
48  *     g_print ("duration = %"GST_TIME_FORMAT, GST_TIME_ARGS (duration));
49  *   } else {
50  *     g_print ("duration query failed...");
51  *   }
52  *   gst_query_unref (query);
53  * ]|
54  */
55
56
57 #include "gst_private.h"
58 #include "gstinfo.h"
59 #include "gstquery.h"
60 #include "gstvalue.h"
61 #include "gstenumtypes.h"
62 #include "gstquark.h"
63 #include "gsturi.h"
64 #include "gstbufferpool.h"
65 #ifdef TIZEN_PROFILE_TV
66 #include <stdio.h>
67 #endif
68
69 GST_DEBUG_CATEGORY_STATIC (gst_query_debug);
70 #define GST_CAT_DEFAULT gst_query_debug
71
72 GType _gst_query_type = 0;
73
74 typedef struct
75 {
76   GstQuery query;
77
78   GstStructure *structure;
79 } GstQueryImpl;
80
81 #define GST_QUERY_STRUCTURE(q)  (((GstQueryImpl *)(q))->structure)
82
83
84 typedef struct
85 {
86   const gint type;
87   const gchar *name;
88   GQuark quark;
89 } GstQueryQuarks;
90
91 static GstQueryQuarks query_quarks[] = {
92   {GST_QUERY_UNKNOWN, "unknown", 0},
93   {GST_QUERY_POSITION, "position", 0},
94   {GST_QUERY_DURATION, "duration", 0},
95   {GST_QUERY_LATENCY, "latency", 0},
96   {GST_QUERY_JITTER, "jitter", 0},
97   {GST_QUERY_RATE, "rate", 0},
98   {GST_QUERY_SEEKING, "seeking", 0},
99   {GST_QUERY_SEGMENT, "segment", 0},
100   {GST_QUERY_CONVERT, "convert", 0},
101   {GST_QUERY_FORMATS, "formats", 0},
102   {GST_QUERY_BUFFERING, "buffering", 0},
103   {GST_QUERY_CUSTOM, "custom", 0},
104   {GST_QUERY_URI, "uri", 0},
105   {GST_QUERY_ALLOCATION, "allocation", 0},
106   {GST_QUERY_SCHEDULING, "scheduling", 0},
107   {GST_QUERY_ACCEPT_CAPS, "accept-caps", 0},
108   {GST_QUERY_CAPS, "caps", 0},
109   {GST_QUERY_DRAIN, "drain", 0},
110   {GST_QUERY_CONTEXT, "context", 0},
111   {GST_QUERY_BITRATE, "bitrate", 0},
112 #ifdef TIZEN_PROFILE_TV
113   {GST_QUERY_RESOURCE, "resource", 0},
114 #endif
115   {0, NULL, 0}
116 };
117
118 GST_DEFINE_MINI_OBJECT_TYPE (GstQuery, gst_query);
119
120 void
121 _priv_gst_query_initialize (void)
122 {
123   gint i;
124
125   _gst_query_type = gst_query_get_type ();
126
127   GST_DEBUG_CATEGORY_INIT (gst_query_debug, "query", 0, "query system");
128
129   for (i = 0; query_quarks[i].name; i++) {
130     query_quarks[i].quark = g_quark_from_static_string (query_quarks[i].name);
131   }
132 }
133
134 /**
135  * gst_query_type_get_name:
136  * @type: the query type
137  *
138  * Get a printable name for the given query type. Do not modify or free.
139  *
140  * Returns: a reference to the static name of the query.
141  */
142 const gchar *
143 gst_query_type_get_name (GstQueryType type)
144 {
145   gint i;
146
147   for (i = 0; query_quarks[i].name; i++) {
148     if (type == query_quarks[i].type)
149       return query_quarks[i].name;
150   }
151   return "unknown";
152 }
153
154 /**
155  * gst_query_type_to_quark:
156  * @type: the query type
157  *
158  * Get the unique quark for the given query type.
159  *
160  * Returns: the quark associated with the query type
161  */
162 GQuark
163 gst_query_type_to_quark (GstQueryType type)
164 {
165   gint i;
166
167   for (i = 0; query_quarks[i].name; i++) {
168     if (type == query_quarks[i].type)
169       return query_quarks[i].quark;
170   }
171   return 0;
172 }
173
174 /**
175  * gst_query_type_get_flags:
176  * @type: a #GstQueryType
177  *
178  * Gets the #GstQueryTypeFlags associated with @type.
179  *
180  * Returns: a #GstQueryTypeFlags.
181  */
182 GstQueryTypeFlags
183 gst_query_type_get_flags (GstQueryType type)
184 {
185   GstQueryTypeFlags ret;
186
187   ret = type & ((1 << GST_QUERY_NUM_SHIFT) - 1);
188
189   return ret;
190 }
191
192 static void
193 _gst_query_free (GstQuery * query)
194 {
195   GstStructure *s;
196
197   g_return_if_fail (query != NULL);
198
199   s = GST_QUERY_STRUCTURE (query);
200   if (s) {
201     gst_structure_set_parent_refcount (s, NULL);
202     gst_structure_free (s);
203   }
204 #ifdef USE_POISONING
205   memset (query, 0xff, sizeof (GstQueryImpl));
206 #endif
207
208   g_slice_free1 (sizeof (GstQueryImpl), query);
209 }
210
211 static GstQuery *
212 _gst_query_copy (GstQuery * query)
213 {
214   GstQuery *copy;
215   GstStructure *s;
216
217   s = GST_QUERY_STRUCTURE (query);
218   if (s) {
219     s = gst_structure_copy (s);
220   }
221   copy = gst_query_new_custom (query->type, s);
222
223   return copy;
224 }
225
226 /**
227  * gst_query_new_position:
228  * @format: the default #GstFormat for the new query
229  *
230  * Constructs a new query stream position query object. Use gst_query_unref()
231  * when done with it. A position query is used to query the current position
232  * of playback in the streams, in some format.
233  *
234  * Free-function: gst_query_unref()
235  *
236  * Returns: (transfer full): a new #GstQuery
237  */
238 GstQuery *
239 gst_query_new_position (GstFormat format)
240 {
241   GstQuery *query;
242   GstStructure *structure;
243
244   structure = gst_structure_new_id (GST_QUARK (QUERY_POSITION),
245       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
246       GST_QUARK (CURRENT), G_TYPE_INT64, G_GINT64_CONSTANT (-1), NULL);
247
248   query = gst_query_new_custom (GST_QUERY_POSITION, structure);
249
250   return query;
251 }
252
253 /**
254  * gst_query_set_position:
255  * @query: a #GstQuery with query type GST_QUERY_POSITION
256  * @format: the requested #GstFormat
257  * @cur: the position to set
258  *
259  * Answer a position query by setting the requested value in the given format.
260  */
261 void
262 gst_query_set_position (GstQuery * query, GstFormat format, gint64 cur)
263 {
264   GstStructure *s;
265
266   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_POSITION);
267
268   s = GST_QUERY_STRUCTURE (query);
269   g_return_if_fail (format == g_value_get_enum (gst_structure_id_get_value (s,
270               GST_QUARK (FORMAT))));
271
272   gst_structure_id_set (s,
273       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
274       GST_QUARK (CURRENT), G_TYPE_INT64, cur, NULL);
275 }
276
277 /**
278  * gst_query_parse_position:
279  * @query: a #GstQuery
280  * @format: (out) (allow-none): the storage for the #GstFormat of the
281  *     position values (may be %NULL)
282  * @cur: (out) (allow-none): the storage for the current position (may be %NULL)
283  *
284  * Parse a position query, writing the format into @format, and the position
285  * into @cur, if the respective parameters are non-%NULL.
286  */
287 void
288 gst_query_parse_position (GstQuery * query, GstFormat * format, gint64 * cur)
289 {
290   GstStructure *structure;
291
292   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_POSITION);
293
294   structure = GST_QUERY_STRUCTURE (query);
295   if (format)
296     *format =
297         (GstFormat) g_value_get_enum (gst_structure_id_get_value (structure,
298             GST_QUARK (FORMAT)));
299   if (cur)
300     *cur = g_value_get_int64 (gst_structure_id_get_value (structure,
301             GST_QUARK (CURRENT)));
302 }
303
304
305 /**
306  * gst_query_new_duration:
307  * @format: the #GstFormat for this duration query
308  *
309  * Constructs a new stream duration query object to query in the given format.
310  * Use gst_query_unref() when done with it. A duration query will give the
311  * total length of the stream.
312  *
313  * Free-function: gst_query_unref()
314  *
315  * Returns: (transfer full): a new #GstQuery
316  */
317 GstQuery *
318 gst_query_new_duration (GstFormat format)
319 {
320   GstQuery *query;
321   GstStructure *structure;
322
323   structure = gst_structure_new_id (GST_QUARK (QUERY_DURATION),
324       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
325       GST_QUARK (DURATION), G_TYPE_INT64, G_GINT64_CONSTANT (-1), NULL);
326
327   query = gst_query_new_custom (GST_QUERY_DURATION, structure);
328
329   return query;
330 }
331
332 /**
333  * gst_query_set_duration:
334  * @query: a #GstQuery
335  * @format: the #GstFormat for the duration
336  * @duration: the duration of the stream
337  *
338  * Answer a duration query by setting the requested value in the given format.
339  */
340 void
341 gst_query_set_duration (GstQuery * query, GstFormat format, gint64 duration)
342 {
343   GstStructure *s;
344
345   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_DURATION);
346
347   s = GST_QUERY_STRUCTURE (query);
348   g_return_if_fail (format == g_value_get_enum (gst_structure_id_get_value (s,
349               GST_QUARK (FORMAT))));
350   gst_structure_id_set (s, GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
351       GST_QUARK (DURATION), G_TYPE_INT64, duration, NULL);
352 }
353
354 /**
355  * gst_query_parse_duration:
356  * @query: a #GstQuery
357  * @format: (out) (allow-none): the storage for the #GstFormat of the duration
358  *     value, or %NULL.
359  * @duration: (out) (allow-none): the storage for the total duration, or %NULL.
360  *
361  * Parse a duration query answer. Write the format of the duration into @format,
362  * and the value into @duration, if the respective variables are non-%NULL.
363  */
364 void
365 gst_query_parse_duration (GstQuery * query, GstFormat * format,
366     gint64 * duration)
367 {
368   GstStructure *structure;
369
370   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_DURATION);
371
372   structure = GST_QUERY_STRUCTURE (query);
373   if (format)
374     *format =
375         (GstFormat) g_value_get_enum (gst_structure_id_get_value (structure,
376             GST_QUARK (FORMAT)));
377   if (duration)
378     *duration = g_value_get_int64 (gst_structure_id_get_value (structure,
379             GST_QUARK (DURATION)));
380 }
381
382 /**
383  * gst_query_new_latency:
384  *
385  * Constructs a new latency query object.
386  * Use gst_query_unref() when done with it. A latency query is usually performed
387  * by sinks to compensate for additional latency introduced by elements in the
388  * pipeline.
389  *
390  * Free-function: gst_query_unref()
391  *
392  * Returns: (transfer full): a #GstQuery
393  */
394 GstQuery *
395 gst_query_new_latency (void)
396 {
397   GstQuery *query;
398   GstStructure *structure;
399
400   structure = gst_structure_new_id (GST_QUARK (QUERY_LATENCY),
401       GST_QUARK (LIVE), G_TYPE_BOOLEAN, FALSE,
402       GST_QUARK (MIN_LATENCY), G_TYPE_UINT64, G_GUINT64_CONSTANT (0),
403       GST_QUARK (MAX_LATENCY), G_TYPE_UINT64, GST_CLOCK_TIME_NONE, NULL);
404
405   query = gst_query_new_custom (GST_QUERY_LATENCY, structure);
406
407   return query;
408 }
409
410 /**
411  * gst_query_set_latency:
412  * @query: a #GstQuery
413  * @live: if there is a live element upstream
414  * @min_latency: the minimal latency of the upstream elements
415  * @max_latency: the maximal latency of the upstream elements
416  *
417  * Answer a latency query by setting the requested values in the given format.
418  */
419 void
420 gst_query_set_latency (GstQuery * query, gboolean live,
421     GstClockTime min_latency, GstClockTime max_latency)
422 {
423   GstStructure *structure;
424
425   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_LATENCY);
426   g_return_if_fail (GST_CLOCK_TIME_IS_VALID (min_latency));
427
428   structure = GST_QUERY_STRUCTURE (query);
429   gst_structure_id_set (structure,
430       GST_QUARK (LIVE), G_TYPE_BOOLEAN, live,
431       GST_QUARK (MIN_LATENCY), G_TYPE_UINT64, min_latency,
432       GST_QUARK (MAX_LATENCY), G_TYPE_UINT64, max_latency, NULL);
433 }
434
435 /**
436  * gst_query_parse_latency:
437  * @query: a #GstQuery
438  * @live: (out) (allow-none): storage for live or %NULL
439  * @min_latency: (out) (allow-none): the storage for the min latency or %NULL
440  * @max_latency: (out) (allow-none): the storage for the max latency or %NULL
441  *
442  * Parse a latency query answer.
443  */
444 void
445 gst_query_parse_latency (GstQuery * query, gboolean * live,
446     GstClockTime * min_latency, GstClockTime * max_latency)
447 {
448   GstStructure *structure;
449
450   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_LATENCY);
451
452   structure = GST_QUERY_STRUCTURE (query);
453   if (live)
454     *live =
455         g_value_get_boolean (gst_structure_id_get_value (structure,
456             GST_QUARK (LIVE)));
457   if (min_latency)
458     *min_latency = g_value_get_uint64 (gst_structure_id_get_value (structure,
459             GST_QUARK (MIN_LATENCY)));
460   if (max_latency)
461     *max_latency = g_value_get_uint64 (gst_structure_id_get_value (structure,
462             GST_QUARK (MAX_LATENCY)));
463 }
464
465 /**
466  * gst_query_new_convert:
467  * @src_format: the source #GstFormat for the new query
468  * @value: the value to convert
469  * @dest_format: the target #GstFormat
470  *
471  * Constructs a new convert query object. Use gst_query_unref()
472  * when done with it. A convert query is used to ask for a conversion between
473  * one format and another.
474  *
475  * Free-function: gst_query_unref()
476  *
477  * Returns: (transfer full): a #GstQuery
478  */
479 GstQuery *
480 gst_query_new_convert (GstFormat src_format, gint64 value,
481     GstFormat dest_format)
482 {
483   GstQuery *query;
484   GstStructure *structure;
485
486   structure = gst_structure_new_id (GST_QUARK (QUERY_CONVERT),
487       GST_QUARK (SRC_FORMAT), GST_TYPE_FORMAT, src_format,
488       GST_QUARK (SRC_VALUE), G_TYPE_INT64, value,
489       GST_QUARK (DEST_FORMAT), GST_TYPE_FORMAT, dest_format,
490       GST_QUARK (DEST_VALUE), G_TYPE_INT64, G_GINT64_CONSTANT (-1), NULL);
491
492   query = gst_query_new_custom (GST_QUERY_CONVERT, structure);
493
494   return query;
495 }
496
497 /**
498  * gst_query_set_convert:
499  * @query: a #GstQuery
500  * @src_format: the source #GstFormat
501  * @src_value: the source value
502  * @dest_format: the destination #GstFormat
503  * @dest_value: the destination value
504  *
505  * Answer a convert query by setting the requested values.
506  */
507 void
508 gst_query_set_convert (GstQuery * query, GstFormat src_format, gint64 src_value,
509     GstFormat dest_format, gint64 dest_value)
510 {
511   GstStructure *structure;
512
513   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CONVERT);
514
515   structure = GST_QUERY_STRUCTURE (query);
516   gst_structure_id_set (structure,
517       GST_QUARK (SRC_FORMAT), GST_TYPE_FORMAT, src_format,
518       GST_QUARK (SRC_VALUE), G_TYPE_INT64, src_value,
519       GST_QUARK (DEST_FORMAT), GST_TYPE_FORMAT, dest_format,
520       GST_QUARK (DEST_VALUE), G_TYPE_INT64, dest_value, NULL);
521 }
522
523 /**
524  * gst_query_parse_convert:
525  * @query: a #GstQuery
526  * @src_format: (out) (allow-none): the storage for the #GstFormat of the
527  *     source value, or %NULL
528  * @src_value: (out) (allow-none): the storage for the source value, or %NULL
529  * @dest_format: (out) (allow-none): the storage for the #GstFormat of the
530  *     destination value, or %NULL
531  * @dest_value: (out) (allow-none): the storage for the destination value,
532  *     or %NULL
533  *
534  * Parse a convert query answer. Any of @src_format, @src_value, @dest_format,
535  * and @dest_value may be %NULL, in which case that value is omitted.
536  */
537 void
538 gst_query_parse_convert (GstQuery * query, GstFormat * src_format,
539     gint64 * src_value, GstFormat * dest_format, gint64 * dest_value)
540 {
541   GstStructure *structure;
542
543   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CONVERT);
544
545   structure = GST_QUERY_STRUCTURE (query);
546   if (src_format)
547     *src_format =
548         (GstFormat) g_value_get_enum (gst_structure_id_get_value (structure,
549             GST_QUARK (SRC_FORMAT)));
550   if (src_value)
551     *src_value = g_value_get_int64 (gst_structure_id_get_value (structure,
552             GST_QUARK (SRC_VALUE)));
553   if (dest_format)
554     *dest_format =
555         (GstFormat) g_value_get_enum (gst_structure_id_get_value (structure,
556             GST_QUARK (DEST_FORMAT)));
557   if (dest_value)
558     *dest_value = g_value_get_int64 (gst_structure_id_get_value (structure,
559             GST_QUARK (DEST_VALUE)));
560 }
561
562 /**
563  * gst_query_new_segment:
564  * @format: the #GstFormat for the new query
565  *
566  * Constructs a new segment query object. Use gst_query_unref()
567  * when done with it. A segment query is used to discover information about the
568  * currently configured segment for playback.
569  *
570  * Free-function: gst_query_unref()
571  *
572  * Returns: (transfer full): a new #GstQuery
573  */
574 GstQuery *
575 gst_query_new_segment (GstFormat format)
576 {
577   GstQuery *query;
578   GstStructure *structure;
579
580   structure = gst_structure_new_id (GST_QUARK (QUERY_SEGMENT),
581       GST_QUARK (RATE), G_TYPE_DOUBLE, (gdouble) 0.0,
582       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
583       GST_QUARK (START_VALUE), G_TYPE_INT64, G_GINT64_CONSTANT (-1),
584       GST_QUARK (STOP_VALUE), G_TYPE_INT64, G_GINT64_CONSTANT (-1), NULL);
585
586   query = gst_query_new_custom (GST_QUERY_SEGMENT, structure);
587
588   return query;
589 }
590
591 #ifdef TIZEN_PROFILE_TV
592 /**
593  * gst_query_new_resource:
594  * @resources: pointer to store the resources
595  *
596  * Constructs a new query stream position query object. Use
597  * gst_query_unref()
598  * when done with it. A resource query is used to query the resources used
599  * by the element/bin/pipelein
600  *
601  * Free-function: gst_query_unref
602  *
603  * Returns: (transfer full): a new #GstQuery
604  */
605 GstQuery *
606 gst_query_new_resource (gchar * resources)
607 {
608   GstQuery *query;
609   GstStructure *structure;
610
611   structure = gst_structure_new_id (GST_QUARK (QUERY_RESOURCE),
612       GST_QUARK (QUERY_RESOURCE), G_TYPE_POINTER, resources, NULL);
613
614   query = gst_query_new_custom (GST_QUERY_RESOURCE, structure);
615
616   return query;
617 }
618
619 /**
620  * gst_query_add_resource:
621  * @query: a #GstQuery
622  * @resource: resource data to add
623  */
624 void
625 gst_query_add_resource (GstQuery * query, gint resource_id)
626 {
627   gchar *resource_info;
628   resource_info = gst_query_parse_resource (query);
629   sprintf (resource_info, "%d", resource_id);
630 }
631
632 /* gst_query_parse_resources:
633  * @query: a #GstQuery
634  * @resources: (out)  the storage for the resource
635  *   value, or NULL.
636  */
637 gchar *
638 gst_query_parse_resource (GstQuery * query)
639 {
640   GstStructure *structure;
641   void *resources;
642   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_RESOURCE, NULL);
643
644   structure = GST_QUERY_STRUCTURE (query);
645   resources = g_value_get_pointer (gst_structure_id_get_value (structure,
646           GST_QUARK (QUERY_RESOURCE)));
647   return resources;
648 }
649 #endif
650
651 /**
652  * gst_query_set_segment:
653  * @query: a #GstQuery
654  * @rate: the rate of the segment
655  * @format: the #GstFormat of the segment values (@start_value and @stop_value)
656  * @start_value: the start value
657  * @stop_value: the stop value
658  *
659  * Answer a segment query by setting the requested values. The normal
660  * playback segment of a pipeline is 0 to duration at the default rate of
661  * 1.0. If a seek was performed on the pipeline to play a different
662  * segment, this query will return the range specified in the last seek.
663  *
664  * @start_value and @stop_value will respectively contain the configured
665  * playback range start and stop values expressed in @format.
666  * The values are always between 0 and the duration of the media and
667  * @start_value <= @stop_value. @rate will contain the playback rate. For
668  * negative rates, playback will actually happen from @stop_value to
669  * @start_value.
670  */
671 void
672 gst_query_set_segment (GstQuery * query, gdouble rate, GstFormat format,
673     gint64 start_value, gint64 stop_value)
674 {
675   GstStructure *structure;
676
677   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SEGMENT);
678
679   structure = GST_QUERY_STRUCTURE (query);
680   gst_structure_id_set (structure,
681       GST_QUARK (RATE), G_TYPE_DOUBLE, rate,
682       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
683       GST_QUARK (START_VALUE), G_TYPE_INT64, start_value,
684       GST_QUARK (STOP_VALUE), G_TYPE_INT64, stop_value, NULL);
685 }
686
687 /**
688  * gst_query_parse_segment:
689  * @query: a #GstQuery
690  * @rate: (out) (allow-none): the storage for the rate of the segment, or %NULL
691  * @format: (out) (allow-none): the storage for the #GstFormat of the values,
692  *     or %NULL
693  * @start_value: (out) (allow-none): the storage for the start value, or %NULL
694  * @stop_value: (out) (allow-none): the storage for the stop value, or %NULL
695  *
696  * Parse a segment query answer. Any of @rate, @format, @start_value, and
697  * @stop_value may be %NULL, which will cause this value to be omitted.
698  *
699  * See gst_query_set_segment() for an explanation of the function arguments.
700  */
701 void
702 gst_query_parse_segment (GstQuery * query, gdouble * rate, GstFormat * format,
703     gint64 * start_value, gint64 * stop_value)
704 {
705   GstStructure *structure;
706
707   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SEGMENT);
708
709   structure = GST_QUERY_STRUCTURE (query);
710   if (rate)
711     *rate = g_value_get_double (gst_structure_id_get_value (structure,
712             GST_QUARK (RATE)));
713   if (format)
714     *format =
715         (GstFormat) g_value_get_enum (gst_structure_id_get_value (structure,
716             GST_QUARK (FORMAT)));
717   if (start_value)
718     *start_value = g_value_get_int64 (gst_structure_id_get_value (structure,
719             GST_QUARK (START_VALUE)));
720   if (stop_value)
721     *stop_value = g_value_get_int64 (gst_structure_id_get_value (structure,
722             GST_QUARK (STOP_VALUE)));
723 }
724
725 /**
726  * gst_query_new_custom:
727  * @type: the query type
728  * @structure: (allow-none) (transfer full): a structure for the query
729  *
730  * Constructs a new custom query object. Use gst_query_unref()
731  * when done with it.
732  *
733  * Free-function: gst_query_unref()
734  *
735  * Returns: (transfer full) (nullable): a new #GstQuery
736  */
737 GstQuery *
738 gst_query_new_custom (GstQueryType type, GstStructure * structure)
739 {
740   GstQueryImpl *query;
741
742   query = g_slice_new0 (GstQueryImpl);
743
744   GST_DEBUG ("creating new query %p %s", query, gst_query_type_get_name (type));
745
746   if (structure) {
747     /* structure must not have a parent */
748     if (!gst_structure_set_parent_refcount (structure,
749             &query->query.mini_object.refcount))
750       goto had_parent;
751   }
752
753   gst_mini_object_init (GST_MINI_OBJECT_CAST (query), 0, _gst_query_type,
754       (GstMiniObjectCopyFunction) _gst_query_copy, NULL,
755       (GstMiniObjectFreeFunction) _gst_query_free);
756
757   GST_QUERY_TYPE (query) = type;
758   GST_QUERY_STRUCTURE (query) = structure;
759
760   return GST_QUERY_CAST (query);
761
762   /* ERRORS */
763 had_parent:
764   {
765     g_slice_free1 (sizeof (GstQueryImpl), query);
766     g_warning ("structure is already owned by another object");
767     return NULL;
768   }
769 }
770
771 /**
772  * gst_query_get_structure:
773  * @query: a #GstQuery
774  *
775  * Get the structure of a query.
776  *
777  * Returns: (transfer none) (nullable): the #GstStructure of the query. The
778  *     structure is still owned by the query and will therefore be freed when the
779  *     query is unreffed.
780  */
781 const GstStructure *
782 gst_query_get_structure (GstQuery * query)
783 {
784   g_return_val_if_fail (GST_IS_QUERY (query), NULL);
785
786   return GST_QUERY_STRUCTURE (query);
787 }
788
789 /**
790  * gst_query_writable_structure:
791  * @query: a #GstQuery
792  *
793  * Get the structure of a query. This method should be called with a writable
794  * @query so that the returned structure is guaranteed to be writable.
795  *
796  * Returns: (transfer none): the #GstStructure of the query. The structure is
797  *     still owned by the query and will therefore be freed when the query
798  *     is unreffed.
799  */
800 GstStructure *
801 gst_query_writable_structure (GstQuery * query)
802 {
803   GstStructure *structure;
804
805   g_return_val_if_fail (GST_IS_QUERY (query), NULL);
806   g_return_val_if_fail (gst_query_is_writable (query), NULL);
807
808   structure = GST_QUERY_STRUCTURE (query);
809
810   if (structure == NULL) {
811     structure =
812         gst_structure_new_id_empty (gst_query_type_to_quark (GST_QUERY_TYPE
813             (query)));
814     gst_structure_set_parent_refcount (structure, &query->mini_object.refcount);
815     GST_QUERY_STRUCTURE (query) = structure;
816   }
817   return structure;
818 }
819
820 /**
821  * gst_query_new_seeking:
822  * @format: the default #GstFormat for the new query
823  *
824  * Constructs a new query object for querying seeking properties of
825  * the stream.
826  *
827  * Free-function: gst_query_unref()
828  *
829  * Returns: (transfer full): a new #GstQuery
830  */
831 GstQuery *
832 gst_query_new_seeking (GstFormat format)
833 {
834   GstQuery *query;
835   GstStructure *structure;
836
837   structure = gst_structure_new_id (GST_QUARK (QUERY_SEEKING),
838       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
839       GST_QUARK (SEEKABLE), G_TYPE_BOOLEAN, FALSE,
840       GST_QUARK (SEGMENT_START), G_TYPE_INT64, G_GINT64_CONSTANT (-1),
841       GST_QUARK (SEGMENT_END), G_TYPE_INT64, G_GINT64_CONSTANT (-1), NULL);
842
843   query = gst_query_new_custom (GST_QUERY_SEEKING, structure);
844
845   return query;
846 }
847
848 /**
849  * gst_query_set_seeking:
850  * @query: a #GstQuery
851  * @format: the format to set for the @segment_start and @segment_end values
852  * @seekable: the seekable flag to set
853  * @segment_start: the segment_start to set
854  * @segment_end: the segment_end to set
855  *
856  * Set the seeking query result fields in @query.
857  */
858 void
859 gst_query_set_seeking (GstQuery * query, GstFormat format,
860     gboolean seekable, gint64 segment_start, gint64 segment_end)
861 {
862   GstStructure *structure;
863
864   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SEEKING);
865   g_return_if_fail (gst_query_is_writable (query));
866
867   structure = GST_QUERY_STRUCTURE (query);
868   gst_structure_id_set (structure,
869       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
870       GST_QUARK (SEEKABLE), G_TYPE_BOOLEAN, seekable,
871       GST_QUARK (SEGMENT_START), G_TYPE_INT64, segment_start,
872       GST_QUARK (SEGMENT_END), G_TYPE_INT64, segment_end, NULL);
873 }
874
875 /**
876  * gst_query_parse_seeking:
877  * @query: a GST_QUERY_SEEKING type query #GstQuery
878  * @format: (out) (allow-none): the format to set for the @segment_start
879  *     and @segment_end values, or %NULL
880  * @seekable: (out) (allow-none): the seekable flag to set, or %NULL
881  * @segment_start: (out) (allow-none): the segment_start to set, or %NULL
882  * @segment_end: (out) (allow-none): the segment_end to set, or %NULL
883  *
884  * Parse a seeking query, writing the format into @format, and
885  * other results into the passed parameters, if the respective parameters
886  * are non-%NULL
887  */
888 void
889 gst_query_parse_seeking (GstQuery * query, GstFormat * format,
890     gboolean * seekable, gint64 * segment_start, gint64 * segment_end)
891 {
892   GstStructure *structure;
893
894   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SEEKING);
895
896   structure = GST_QUERY_STRUCTURE (query);
897   if (format)
898     *format =
899         (GstFormat) g_value_get_enum (gst_structure_id_get_value (structure,
900             GST_QUARK (FORMAT)));
901   if (seekable)
902     *seekable = g_value_get_boolean (gst_structure_id_get_value (structure,
903             GST_QUARK (SEEKABLE)));
904   if (segment_start)
905     *segment_start = g_value_get_int64 (gst_structure_id_get_value (structure,
906             GST_QUARK (SEGMENT_START)));
907   if (segment_end)
908     *segment_end = g_value_get_int64 (gst_structure_id_get_value (structure,
909             GST_QUARK (SEGMENT_END)));
910 }
911
912 static GArray *
913 ensure_array (GstStructure * s, GQuark quark, gsize element_size,
914     GDestroyNotify clear_func)
915 {
916   GArray *array;
917   const GValue *value;
918
919   value = gst_structure_id_get_value (s, quark);
920   if (value) {
921     array = (GArray *) g_value_get_boxed (value);
922   } else {
923     GValue new_array_val = { 0, };
924
925     array = g_array_new (FALSE, TRUE, element_size);
926     if (clear_func)
927       g_array_set_clear_func (array, clear_func);
928
929     g_value_init (&new_array_val, G_TYPE_ARRAY);
930     g_value_take_boxed (&new_array_val, array);
931
932     gst_structure_id_take_value (s, quark, &new_array_val);
933   }
934   return array;
935 }
936
937 /**
938  * gst_query_new_formats:
939  *
940  * Constructs a new query object for querying formats of
941  * the stream.
942  *
943  * Free-function: gst_query_unref()
944  *
945  * Returns: (transfer full): a new #GstQuery
946  */
947 GstQuery *
948 gst_query_new_formats (void)
949 {
950   GstQuery *query;
951   GstStructure *structure;
952
953   structure = gst_structure_new_id_empty (GST_QUARK (QUERY_FORMATS));
954   query = gst_query_new_custom (GST_QUERY_FORMATS, structure);
955
956   return query;
957 }
958
959 static void
960 gst_query_list_add_format (GValue * list, GstFormat format)
961 {
962   GValue item = { 0, };
963
964   g_value_init (&item, GST_TYPE_FORMAT);
965   g_value_set_enum (&item, format);
966   gst_value_list_append_value (list, &item);
967   g_value_unset (&item);
968 }
969
970 /**
971  * gst_query_set_formats:
972  * @query: a #GstQuery
973  * @n_formats: the number of formats to set.
974  * @...: A number of @GstFormats equal to @n_formats.
975  *
976  * Set the formats query result fields in @query. The number of formats passed
977  * must be equal to @n_formats.
978  */
979 void
980 gst_query_set_formats (GstQuery * query, gint n_formats, ...)
981 {
982   va_list ap;
983   GValue list = { 0, };
984   gint i;
985   GstStructure *structure;
986
987   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_FORMATS);
988   g_return_if_fail (gst_query_is_writable (query));
989
990   g_value_init (&list, GST_TYPE_LIST);
991
992   va_start (ap, n_formats);
993   for (i = 0; i < n_formats; i++) {
994     gst_query_list_add_format (&list, va_arg (ap, GstFormat));
995   }
996   va_end (ap);
997
998   structure = GST_QUERY_STRUCTURE (query);
999   gst_structure_set_value (structure, "formats", &list);
1000
1001   g_value_unset (&list);
1002
1003 }
1004
1005 /**
1006  * gst_query_set_formatsv:
1007  * @query: a #GstQuery
1008  * @n_formats: the number of formats to set.
1009  * @formats: (in) (array length=n_formats): an array containing @n_formats
1010  *     @GstFormat values.
1011  *
1012  * Set the formats query result fields in @query. The number of formats passed
1013  * in the @formats array must be equal to @n_formats.
1014  */
1015 void
1016 gst_query_set_formatsv (GstQuery * query, gint n_formats,
1017     const GstFormat * formats)
1018 {
1019   GValue list = { 0, };
1020   gint i;
1021   GstStructure *structure;
1022
1023   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_FORMATS);
1024   g_return_if_fail (gst_query_is_writable (query));
1025
1026   g_value_init (&list, GST_TYPE_LIST);
1027   for (i = 0; i < n_formats; i++) {
1028     gst_query_list_add_format (&list, formats[i]);
1029   }
1030   structure = GST_QUERY_STRUCTURE (query);
1031   gst_structure_set_value (structure, "formats", &list);
1032
1033   g_value_unset (&list);
1034 }
1035
1036 /**
1037  * gst_query_parse_n_formats:
1038  * @query: a #GstQuery
1039  * @n_formats: (out) (allow-none): the number of formats in this query.
1040  *
1041  * Parse the number of formats in the formats @query.
1042  */
1043 void
1044 gst_query_parse_n_formats (GstQuery * query, guint * n_formats)
1045 {
1046   GstStructure *structure;
1047
1048   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_FORMATS);
1049
1050   if (n_formats) {
1051     const GValue *list;
1052
1053     structure = GST_QUERY_STRUCTURE (query);
1054     list = gst_structure_get_value (structure, "formats");
1055     if (list == NULL)
1056       *n_formats = 0;
1057     else
1058       *n_formats = gst_value_list_get_size (list);
1059   }
1060 }
1061
1062 /**
1063  * gst_query_parse_nth_format:
1064  * @query: a #GstQuery
1065  * @nth: the nth format to retrieve.
1066  * @format: (out) (allow-none): a pointer to store the nth format
1067  *
1068  * Parse the format query and retrieve the @nth format from it into
1069  * @format. If the list contains less elements than @nth, @format will be
1070  * set to GST_FORMAT_UNDEFINED.
1071  */
1072 void
1073 gst_query_parse_nth_format (GstQuery * query, guint nth, GstFormat * format)
1074 {
1075   GstStructure *structure;
1076
1077   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_FORMATS);
1078
1079   if (format) {
1080     const GValue *list;
1081
1082     structure = GST_QUERY_STRUCTURE (query);
1083     list = gst_structure_get_value (structure, "formats");
1084     if (list == NULL) {
1085       *format = GST_FORMAT_UNDEFINED;
1086     } else {
1087       if (nth < gst_value_list_get_size (list)) {
1088         *format =
1089             (GstFormat) g_value_get_enum (gst_value_list_get_value (list, nth));
1090       } else
1091         *format = GST_FORMAT_UNDEFINED;
1092     }
1093   }
1094 }
1095
1096 /**
1097  * gst_query_new_buffering:
1098  * @format: the default #GstFormat for the new query
1099  *
1100  * Constructs a new query object for querying the buffering status of
1101  * a stream.
1102  *
1103  * Free-function: gst_query_unref()
1104  *
1105  * Returns: (transfer full): a new #GstQuery
1106  */
1107 GstQuery *
1108 gst_query_new_buffering (GstFormat format)
1109 {
1110   GstQuery *query;
1111   GstStructure *structure;
1112
1113   /* by default, we configure the answer as no buffering with a 100% buffering
1114    * progress */
1115   structure = gst_structure_new_id (GST_QUARK (QUERY_BUFFERING),
1116       GST_QUARK (BUSY), G_TYPE_BOOLEAN, FALSE,
1117       GST_QUARK (BUFFER_PERCENT), G_TYPE_INT, 100,
1118       GST_QUARK (BUFFERING_MODE), GST_TYPE_BUFFERING_MODE, GST_BUFFERING_STREAM,
1119       GST_QUARK (AVG_IN_RATE), G_TYPE_INT, -1,
1120       GST_QUARK (AVG_OUT_RATE), G_TYPE_INT, -1,
1121       GST_QUARK (BUFFERING_LEFT), G_TYPE_INT64, G_GINT64_CONSTANT (0),
1122       GST_QUARK (ESTIMATED_TOTAL), G_TYPE_INT64, G_GINT64_CONSTANT (-1),
1123       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
1124       GST_QUARK (START_VALUE), G_TYPE_INT64, G_GINT64_CONSTANT (-1),
1125       GST_QUARK (STOP_VALUE), G_TYPE_INT64, G_GINT64_CONSTANT (-1), NULL);
1126
1127   query = gst_query_new_custom (GST_QUERY_BUFFERING, structure);
1128
1129   return query;
1130 }
1131
1132 /**
1133  * gst_query_set_buffering_percent:
1134  * @query: A valid #GstQuery of type GST_QUERY_BUFFERING.
1135  * @busy: if buffering is busy
1136  * @percent: a buffering percent
1137  *
1138  * Set the percentage of buffered data. This is a value between 0 and 100.
1139  * The @busy indicator is %TRUE when the buffering is in progress.
1140  */
1141 void
1142 gst_query_set_buffering_percent (GstQuery * query, gboolean busy, gint percent)
1143 {
1144   GstStructure *structure;
1145
1146   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING);
1147   g_return_if_fail (gst_query_is_writable (query));
1148   g_return_if_fail (percent >= 0 && percent <= 100);
1149
1150   structure = GST_QUERY_STRUCTURE (query);
1151   gst_structure_id_set (structure,
1152       GST_QUARK (BUSY), G_TYPE_BOOLEAN, busy,
1153       GST_QUARK (BUFFER_PERCENT), G_TYPE_INT, percent, NULL);
1154 }
1155
1156 /**
1157  * gst_query_parse_buffering_percent:
1158  * @query: A valid #GstQuery of type GST_QUERY_BUFFERING.
1159  * @busy: (out) (allow-none): if buffering is busy, or %NULL
1160  * @percent: (out) (allow-none): a buffering percent, or %NULL
1161  *
1162  * Get the percentage of buffered data. This is a value between 0 and 100.
1163  * The @busy indicator is %TRUE when the buffering is in progress.
1164  */
1165 void
1166 gst_query_parse_buffering_percent (GstQuery * query, gboolean * busy,
1167     gint * percent)
1168 {
1169   GstStructure *structure;
1170
1171   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING);
1172
1173   structure = GST_QUERY_STRUCTURE (query);
1174   if (busy)
1175     *busy = g_value_get_boolean (gst_structure_id_get_value (structure,
1176             GST_QUARK (BUSY)));
1177   if (percent)
1178     *percent = g_value_get_int (gst_structure_id_get_value (structure,
1179             GST_QUARK (BUFFER_PERCENT)));
1180 }
1181
1182 /**
1183  * gst_query_set_buffering_stats:
1184  * @query: A valid #GstQuery of type GST_QUERY_BUFFERING.
1185  * @mode: a buffering mode
1186  * @avg_in: the average input rate
1187  * @avg_out: the average output rate
1188  * @buffering_left: amount of buffering time left in milliseconds
1189  *
1190  * Configures the buffering stats values in @query.
1191  */
1192 void
1193 gst_query_set_buffering_stats (GstQuery * query, GstBufferingMode mode,
1194     gint avg_in, gint avg_out, gint64 buffering_left)
1195 {
1196   GstStructure *structure;
1197
1198   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING);
1199   g_return_if_fail (gst_query_is_writable (query));
1200
1201   structure = GST_QUERY_STRUCTURE (query);
1202   gst_structure_id_set (structure,
1203       GST_QUARK (BUFFERING_MODE), GST_TYPE_BUFFERING_MODE, mode,
1204       GST_QUARK (AVG_IN_RATE), G_TYPE_INT, avg_in,
1205       GST_QUARK (AVG_OUT_RATE), G_TYPE_INT, avg_out,
1206       GST_QUARK (BUFFERING_LEFT), G_TYPE_INT64, buffering_left, NULL);
1207 }
1208
1209 /**
1210  * gst_query_parse_buffering_stats:
1211  * @query: A valid #GstQuery of type GST_QUERY_BUFFERING.
1212  * @mode: (out) (allow-none): a buffering mode, or %NULL
1213  * @avg_in: (out) (allow-none): the average input rate, or %NULL
1214  * @avg_out: (out) (allow-none): the average output rat, or %NULL
1215  * @buffering_left: (out) (allow-none): amount of buffering time left in
1216  *     milliseconds, or %NULL
1217  *
1218  * Extracts the buffering stats values from @query.
1219  */
1220 void
1221 gst_query_parse_buffering_stats (GstQuery * query,
1222     GstBufferingMode * mode, gint * avg_in, gint * avg_out,
1223     gint64 * buffering_left)
1224 {
1225   GstStructure *structure;
1226
1227   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING);
1228
1229   structure = GST_QUERY_STRUCTURE (query);
1230   if (mode)
1231     *mode = (GstBufferingMode)
1232         g_value_get_enum (gst_structure_id_get_value (structure,
1233             GST_QUARK (BUFFERING_MODE)));
1234   if (avg_in)
1235     *avg_in = g_value_get_int (gst_structure_id_get_value (structure,
1236             GST_QUARK (AVG_IN_RATE)));
1237   if (avg_out)
1238     *avg_out = g_value_get_int (gst_structure_id_get_value (structure,
1239             GST_QUARK (AVG_OUT_RATE)));
1240   if (buffering_left)
1241     *buffering_left =
1242         g_value_get_int64 (gst_structure_id_get_value (structure,
1243             GST_QUARK (BUFFERING_LEFT)));
1244 }
1245
1246 /**
1247  * gst_query_set_buffering_range:
1248  * @query: a #GstQuery
1249  * @format: the format to set for the @start and @stop values
1250  * @start: the start to set
1251  * @stop: the stop to set
1252  * @estimated_total: estimated total amount of download time remaining in
1253  *     milliseconds
1254  *
1255  * Set the available query result fields in @query.
1256  */
1257 void
1258 gst_query_set_buffering_range (GstQuery * query, GstFormat format,
1259     gint64 start, gint64 stop, gint64 estimated_total)
1260 {
1261   GstStructure *structure;
1262
1263   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING);
1264   g_return_if_fail (gst_query_is_writable (query));
1265
1266   structure = GST_QUERY_STRUCTURE (query);
1267   gst_structure_id_set (structure,
1268       GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
1269       GST_QUARK (START_VALUE), G_TYPE_INT64, start,
1270       GST_QUARK (STOP_VALUE), G_TYPE_INT64, stop,
1271       GST_QUARK (ESTIMATED_TOTAL), G_TYPE_INT64, estimated_total, NULL);
1272 }
1273
1274 /**
1275  * gst_query_parse_buffering_range:
1276  * @query: a GST_QUERY_BUFFERING type query #GstQuery
1277  * @format: (out) (allow-none): the format to set for the @segment_start
1278  *     and @segment_end values, or %NULL
1279  * @start: (out) (allow-none): the start to set, or %NULL
1280  * @stop: (out) (allow-none): the stop to set, or %NULL
1281  * @estimated_total: (out) (allow-none): estimated total amount of download
1282  *     time remaining in milliseconds, or %NULL
1283  *
1284  * Parse an available query, writing the format into @format, and
1285  * other results into the passed parameters, if the respective parameters
1286  * are non-%NULL
1287  */
1288 void
1289 gst_query_parse_buffering_range (GstQuery * query, GstFormat * format,
1290     gint64 * start, gint64 * stop, gint64 * estimated_total)
1291 {
1292   GstStructure *structure;
1293
1294   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING);
1295
1296   structure = GST_QUERY_STRUCTURE (query);
1297   if (format)
1298     *format =
1299         (GstFormat) g_value_get_enum (gst_structure_id_get_value (structure,
1300             GST_QUARK (FORMAT)));
1301   if (start)
1302     *start = g_value_get_int64 (gst_structure_id_get_value (structure,
1303             GST_QUARK (START_VALUE)));
1304   if (stop)
1305     *stop = g_value_get_int64 (gst_structure_id_get_value (structure,
1306             GST_QUARK (STOP_VALUE)));
1307   if (estimated_total)
1308     *estimated_total =
1309         g_value_get_int64 (gst_structure_id_get_value (structure,
1310             GST_QUARK (ESTIMATED_TOTAL)));
1311 }
1312
1313 /* GstQueryBufferingRange: internal struct for GArray */
1314 typedef struct
1315 {
1316   gint64 start;
1317   gint64 stop;
1318 } GstQueryBufferingRange;
1319
1320 /**
1321  * gst_query_add_buffering_range:
1322  * @query: a GST_QUERY_BUFFERING type query #GstQuery
1323  * @start: start position of the range
1324  * @stop: stop position of the range
1325  *
1326  * Set the buffering-ranges array field in @query. The current last
1327  * start position of the array should be inferior to @start.
1328  *
1329  * Returns: a #gboolean indicating if the range was added or not.
1330  */
1331 gboolean
1332 gst_query_add_buffering_range (GstQuery * query, gint64 start, gint64 stop)
1333 {
1334   GstQueryBufferingRange range;
1335   GstStructure *structure;
1336   GArray *array;
1337
1338   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING, FALSE);
1339   g_return_val_if_fail (gst_query_is_writable (query), FALSE);
1340
1341   if (G_UNLIKELY (start >= stop))
1342     return FALSE;
1343
1344   structure = GST_QUERY_STRUCTURE (query);
1345   array = ensure_array (structure, GST_QUARK (BUFFERING_RANGES),
1346       sizeof (GstQueryBufferingRange), NULL);
1347
1348   if (array->len > 1) {
1349     GstQueryBufferingRange *last;
1350
1351     last = &g_array_index (array, GstQueryBufferingRange, array->len - 1);
1352
1353     if (G_UNLIKELY (start <= last->start))
1354       return FALSE;
1355   }
1356
1357   range.start = start;
1358   range.stop = stop;
1359   g_array_append_val (array, range);
1360
1361   return TRUE;
1362 }
1363
1364 /**
1365  * gst_query_get_n_buffering_ranges:
1366  * @query: a GST_QUERY_BUFFERING type query #GstQuery
1367  *
1368  * Retrieve the number of values currently stored in the
1369  * buffered-ranges array of the query's structure.
1370  *
1371  * Returns: the range array size as a #guint.
1372  */
1373 guint
1374 gst_query_get_n_buffering_ranges (GstQuery * query)
1375 {
1376   GstStructure *structure;
1377   GArray *array;
1378
1379   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING, 0);
1380
1381   structure = GST_QUERY_STRUCTURE (query);
1382   array = ensure_array (structure, GST_QUARK (BUFFERING_RANGES),
1383       sizeof (GstQueryBufferingRange), NULL);
1384
1385   return array->len;
1386 }
1387
1388
1389 /**
1390  * gst_query_parse_nth_buffering_range:
1391  * @query: a GST_QUERY_BUFFERING type query #GstQuery
1392  * @index: position in the buffered-ranges array to read
1393  * @start: (out) (allow-none): the start position to set, or %NULL
1394  * @stop: (out) (allow-none): the stop position to set, or %NULL
1395  *
1396  * Parse an available query and get the start and stop values stored
1397  * at the @index of the buffered ranges array.
1398  *
1399  * Returns: a #gboolean indicating if the parsing succeeded.
1400  */
1401 gboolean
1402 gst_query_parse_nth_buffering_range (GstQuery * query, guint index,
1403     gint64 * start, gint64 * stop)
1404 {
1405   GstQueryBufferingRange *range;
1406   GstStructure *structure;
1407   GArray *array;
1408
1409   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING, FALSE);
1410
1411   structure = GST_QUERY_STRUCTURE (query);
1412
1413   array = ensure_array (structure, GST_QUARK (BUFFERING_RANGES),
1414       sizeof (GstQueryBufferingRange), NULL);
1415   g_return_val_if_fail (index < array->len, FALSE);
1416
1417   range = &g_array_index (array, GstQueryBufferingRange, index);
1418
1419   if (start)
1420     *start = range->start;
1421   if (stop)
1422     *stop = range->stop;
1423
1424   return TRUE;
1425 }
1426
1427
1428 /**
1429  * gst_query_new_uri:
1430  *
1431  * Constructs a new query URI query object. Use gst_query_unref()
1432  * when done with it. An URI query is used to query the current URI
1433  * that is used by the source or sink.
1434  *
1435  * Free-function: gst_query_unref()
1436  *
1437  * Returns: (transfer full): a new #GstQuery
1438  */
1439 GstQuery *
1440 gst_query_new_uri (void)
1441 {
1442   GstQuery *query;
1443   GstStructure *structure;
1444
1445   structure = gst_structure_new_id (GST_QUARK (QUERY_URI),
1446       GST_QUARK (URI), G_TYPE_STRING, NULL, NULL);
1447
1448   query = gst_query_new_custom (GST_QUERY_URI, structure);
1449
1450   return query;
1451 }
1452
1453 /**
1454  * gst_query_set_uri:
1455  * @query: a #GstQuery with query type GST_QUERY_URI
1456  * @uri: the URI to set
1457  *
1458  * Answer a URI query by setting the requested URI.
1459  */
1460 void
1461 gst_query_set_uri (GstQuery * query, const gchar * uri)
1462 {
1463   GstStructure *structure;
1464
1465   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_URI);
1466   g_return_if_fail (gst_query_is_writable (query));
1467   g_return_if_fail (gst_uri_is_valid (uri));
1468
1469   structure = GST_QUERY_STRUCTURE (query);
1470   gst_structure_id_set (structure, GST_QUARK (URI), G_TYPE_STRING, uri, NULL);
1471 }
1472
1473 /**
1474  * gst_query_parse_uri:
1475  * @query: a #GstQuery
1476  * @uri: (out) (transfer full) (allow-none): the storage for the current URI
1477  *     (may be %NULL)
1478  *
1479  * Parse an URI query, writing the URI into @uri as a newly
1480  * allocated string, if the respective parameters are non-%NULL.
1481  * Free the string with g_free() after usage.
1482  */
1483 void
1484 gst_query_parse_uri (GstQuery * query, gchar ** uri)
1485 {
1486   GstStructure *structure;
1487
1488   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_URI);
1489
1490   structure = GST_QUERY_STRUCTURE (query);
1491   if (uri)
1492     *uri = g_value_dup_string (gst_structure_id_get_value (structure,
1493             GST_QUARK (URI)));
1494 }
1495
1496 /**
1497  * gst_query_set_uri_redirection:
1498  * @query: a #GstQuery with query type GST_QUERY_URI
1499  * @uri: the URI to set
1500  *
1501  * Answer a URI query by setting the requested URI redirection.
1502  *
1503  * Since: 1.2
1504  */
1505 void
1506 gst_query_set_uri_redirection (GstQuery * query, const gchar * uri)
1507 {
1508   GstStructure *structure;
1509
1510   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_URI);
1511   g_return_if_fail (gst_query_is_writable (query));
1512   g_return_if_fail (gst_uri_is_valid (uri));
1513
1514   structure = GST_QUERY_STRUCTURE (query);
1515   gst_structure_id_set (structure, GST_QUARK (URI_REDIRECTION),
1516       G_TYPE_STRING, uri, NULL);
1517 }
1518
1519 /**
1520  * gst_query_parse_uri_redirection:
1521  * @query: a #GstQuery
1522  * @uri: (out) (transfer full) (allow-none): the storage for the redirect URI
1523  *     (may be %NULL)
1524  *
1525  * Parse an URI query, writing the URI into @uri as a newly
1526  * allocated string, if the respective parameters are non-%NULL.
1527  * Free the string with g_free() after usage.
1528  *
1529  * Since: 1.2
1530  */
1531 void
1532 gst_query_parse_uri_redirection (GstQuery * query, gchar ** uri)
1533 {
1534   GstStructure *structure;
1535
1536   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_URI);
1537
1538   structure = GST_QUERY_STRUCTURE (query);
1539   if (uri) {
1540     if (!gst_structure_id_get (structure, GST_QUARK (URI_REDIRECTION),
1541             G_TYPE_STRING, uri, NULL))
1542       *uri = NULL;
1543   }
1544 }
1545
1546 /**
1547  * gst_query_set_uri_redirection_permanent:
1548  * @query: a #GstQuery with query type %GST_QUERY_URI
1549  * @permanent: whether the redirect is permanent or not
1550  *
1551  * Answer a URI query by setting the requested URI redirection
1552  * to permanent or not.
1553  *
1554  * Since: 1.4
1555  */
1556 void
1557 gst_query_set_uri_redirection_permanent (GstQuery * query, gboolean permanent)
1558 {
1559   GstStructure *structure;
1560
1561   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_URI);
1562   g_return_if_fail (gst_query_is_writable (query));
1563
1564   structure = GST_QUERY_STRUCTURE (query);
1565   gst_structure_id_set (structure, GST_QUARK (URI_REDIRECTION_PERMANENT),
1566       G_TYPE_BOOLEAN, permanent, NULL);
1567 }
1568
1569 /**
1570  * gst_query_parse_uri_redirection_permanent:
1571  * @query: a #GstQuery
1572  * @permanent: (out) (allow-none): if the URI redirection is permanent
1573  *     (may be %NULL)
1574  *
1575  * Parse an URI query, and set @permanent to %TRUE if there is a redirection
1576  * and it should be considered permanent. If a redirection is permanent,
1577  * applications should update their internal storage of the URI, otherwise
1578  * they should make all future requests to the original URI.
1579  *
1580  * Since: 1.4
1581  */
1582 void
1583 gst_query_parse_uri_redirection_permanent (GstQuery * query,
1584     gboolean * permanent)
1585 {
1586   GstStructure *structure;
1587
1588   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_URI);
1589
1590   structure = GST_QUERY_STRUCTURE (query);
1591   if (permanent) {
1592     if (!gst_structure_id_get (structure, GST_QUARK (URI_REDIRECTION_PERMANENT),
1593             G_TYPE_BOOLEAN, permanent, NULL))
1594       *permanent = FALSE;
1595   }
1596 }
1597
1598 /**
1599  * gst_query_new_allocation:
1600  * @caps: the negotiated caps
1601  * @need_pool: return a pool
1602  *
1603  * Constructs a new query object for querying the allocation properties.
1604  *
1605  * Free-function: gst_query_unref()
1606  *
1607  * Returns: (transfer full): a new #GstQuery
1608  */
1609 GstQuery *
1610 gst_query_new_allocation (GstCaps * caps, gboolean need_pool)
1611 {
1612   GstQuery *query;
1613   GstStructure *structure;
1614
1615   structure = gst_structure_new_id (GST_QUARK (QUERY_ALLOCATION),
1616       GST_QUARK (CAPS), GST_TYPE_CAPS, caps,
1617       GST_QUARK (NEED_POOL), G_TYPE_BOOLEAN, need_pool, NULL);
1618
1619   query = gst_query_new_custom (GST_QUERY_ALLOCATION, structure);
1620
1621   return query;
1622 }
1623
1624 /**
1625  * gst_query_parse_allocation:
1626  * @query: a #GstQuery
1627  * @caps: (out) (transfer none) (allow-none): The #GstCaps
1628  * @need_pool: (out) (allow-none): Whether a #GstBufferPool is needed
1629  *
1630  * Parse an allocation query, writing the requested caps in @caps and
1631  * whether a pool is needed in @need_pool, if the respective parameters
1632  * are non-%NULL.
1633  *
1634  * Pool details can be retrieved using gst_query_get_n_allocation_pools() and
1635  * gst_query_parse_nth_allocation_pool().
1636  */
1637 void
1638 gst_query_parse_allocation (GstQuery * query, GstCaps ** caps,
1639     gboolean * need_pool)
1640 {
1641   GstStructure *structure;
1642
1643   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
1644
1645   structure = GST_QUERY_STRUCTURE (query);
1646   if (caps) {
1647     *caps = g_value_get_boxed (gst_structure_id_get_value (structure,
1648             GST_QUARK (CAPS)));
1649   }
1650   gst_structure_id_get (structure,
1651       GST_QUARK (NEED_POOL), G_TYPE_BOOLEAN, need_pool, NULL);
1652 }
1653
1654 typedef struct
1655 {
1656   GstBufferPool *pool;
1657   guint size;
1658   guint min_buffers;
1659   guint max_buffers;
1660 } AllocationPool;
1661
1662 static void
1663 allocation_pool_free (AllocationPool * ap)
1664 {
1665   if (ap->pool)
1666     gst_object_unref (ap->pool);
1667 }
1668
1669 /**
1670  * gst_query_add_allocation_pool:
1671  * @query: A valid #GstQuery of type GST_QUERY_ALLOCATION.
1672  * @pool: (transfer none) (allow-none): the #GstBufferPool
1673  * @size: the buffer size
1674  * @min_buffers: the min buffers
1675  * @max_buffers: the max buffers
1676  *
1677  * Set the pool parameters in @query.
1678  */
1679 void
1680 gst_query_add_allocation_pool (GstQuery * query, GstBufferPool * pool,
1681     guint size, guint min_buffers, guint max_buffers)
1682 {
1683   GArray *array;
1684   GstStructure *structure;
1685   AllocationPool ap;
1686
1687   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
1688   g_return_if_fail (gst_query_is_writable (query));
1689
1690   structure = GST_QUERY_STRUCTURE (query);
1691   array = ensure_array (structure, GST_QUARK (POOL),
1692       sizeof (AllocationPool), (GDestroyNotify) allocation_pool_free);
1693
1694   if ((ap.pool = pool))
1695     gst_object_ref (pool);
1696   ap.size = size;
1697   ap.min_buffers = min_buffers;
1698   ap.max_buffers = max_buffers;
1699
1700   g_array_append_val (array, ap);
1701 }
1702
1703 /**
1704  * gst_query_get_n_allocation_pools:
1705  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1706  *
1707  * Retrieve the number of values currently stored in the
1708  * pool array of the query's structure.
1709  *
1710  * Returns: the pool array size as a #guint.
1711  */
1712 guint
1713 gst_query_get_n_allocation_pools (GstQuery * query)
1714 {
1715   GArray *array;
1716   GstStructure *structure;
1717
1718   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION, 0);
1719
1720   structure = GST_QUERY_STRUCTURE (query);
1721   array = ensure_array (structure, GST_QUARK (POOL),
1722       sizeof (AllocationPool), (GDestroyNotify) allocation_pool_free);
1723
1724   return array->len;
1725 }
1726
1727 /**
1728  * gst_query_parse_nth_allocation_pool:
1729  * @query: A valid #GstQuery of type GST_QUERY_ALLOCATION.
1730  * @index: index to parse
1731  * @pool: (out) (allow-none) (transfer full): the #GstBufferPool
1732  * @size: (out) (allow-none): the buffer size
1733  * @min_buffers: (out) (allow-none): the min buffers
1734  * @max_buffers: (out) (allow-none): the max buffers
1735  *
1736  * Get the pool parameters in @query.
1737  *
1738  * Unref @pool with gst_object_unref() when it's not needed any more.
1739  */
1740 void
1741 gst_query_parse_nth_allocation_pool (GstQuery * query, guint index,
1742     GstBufferPool ** pool, guint * size, guint * min_buffers,
1743     guint * max_buffers)
1744 {
1745   GArray *array;
1746   GstStructure *structure;
1747   AllocationPool *ap;
1748
1749   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
1750
1751   structure = GST_QUERY_STRUCTURE (query);
1752   array = ensure_array (structure, GST_QUARK (POOL),
1753       sizeof (AllocationPool), (GDestroyNotify) allocation_pool_free);
1754   g_return_if_fail (index < array->len);
1755
1756   ap = &g_array_index (array, AllocationPool, index);
1757
1758   if (pool)
1759     if ((*pool = ap->pool))
1760       gst_object_ref (*pool);
1761   if (size)
1762     *size = ap->size;
1763   if (min_buffers)
1764     *min_buffers = ap->min_buffers;
1765   if (max_buffers)
1766     *max_buffers = ap->max_buffers;
1767 }
1768
1769 /**
1770  * gst_query_set_nth_allocation_pool:
1771  * @index: index to modify
1772  * @query: A valid #GstQuery of type GST_QUERY_ALLOCATION.
1773  * @pool: (transfer none) (allow-none): the #GstBufferPool
1774  * @size: the buffer size
1775  * @min_buffers: the min buffers
1776  * @max_buffers: the max buffers
1777  *
1778  * Set the pool parameters in @query.
1779  */
1780 void
1781 gst_query_set_nth_allocation_pool (GstQuery * query, guint index,
1782     GstBufferPool * pool, guint size, guint min_buffers, guint max_buffers)
1783 {
1784   GArray *array;
1785   GstStructure *structure;
1786   AllocationPool *oldap, ap;
1787
1788   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
1789
1790   structure = GST_QUERY_STRUCTURE (query);
1791   array = ensure_array (structure, GST_QUARK (POOL),
1792       sizeof (AllocationPool), (GDestroyNotify) allocation_pool_free);
1793   g_return_if_fail (index < array->len);
1794
1795   oldap = &g_array_index (array, AllocationPool, index);
1796   allocation_pool_free (oldap);
1797
1798   if ((ap.pool = pool))
1799     gst_object_ref (pool);
1800   ap.size = size;
1801   ap.min_buffers = min_buffers;
1802   ap.max_buffers = max_buffers;
1803   g_array_index (array, AllocationPool, index) = ap;
1804 }
1805
1806 /**
1807  * gst_query_remove_nth_allocation_pool:
1808  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1809  * @index: position in the allocation pool array to remove
1810  *
1811  * Remove the allocation pool at @index of the allocation pool array.
1812  *
1813  * Since: 1.2
1814  */
1815 void
1816 gst_query_remove_nth_allocation_pool (GstQuery * query, guint index)
1817 {
1818   GArray *array;
1819   GstStructure *structure;
1820
1821   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
1822   g_return_if_fail (gst_query_is_writable (query));
1823
1824   structure = GST_QUERY_STRUCTURE (query);
1825   array =
1826       ensure_array (structure, GST_QUARK (POOL), sizeof (AllocationPool),
1827       (GDestroyNotify) allocation_pool_free);
1828   g_return_if_fail (index < array->len);
1829
1830   g_array_remove_index (array, index);
1831 }
1832
1833 typedef struct
1834 {
1835   GType api;
1836   GstStructure *params;
1837 } AllocationMeta;
1838
1839 static void
1840 allocation_meta_free (AllocationMeta * am)
1841 {
1842   if (am->params)
1843     gst_structure_free (am->params);
1844 }
1845
1846 /**
1847  * gst_query_add_allocation_meta:
1848  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1849  * @api: the metadata API
1850  * @params: (transfer none) (allow-none): API specific parameters
1851  *
1852  * Add @api with @params as one of the supported metadata API to @query.
1853  */
1854 void
1855 gst_query_add_allocation_meta (GstQuery * query, GType api,
1856     const GstStructure * params)
1857 {
1858   GArray *array;
1859   GstStructure *structure;
1860   AllocationMeta am;
1861
1862   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
1863   g_return_if_fail (api != 0);
1864   g_return_if_fail (gst_query_is_writable (query));
1865
1866   structure = GST_QUERY_STRUCTURE (query);
1867   array =
1868       ensure_array (structure, GST_QUARK (META), sizeof (AllocationMeta),
1869       (GDestroyNotify) allocation_meta_free);
1870
1871   am.api = api;
1872   am.params = (params ? gst_structure_copy (params) : NULL);
1873
1874   g_array_append_val (array, am);
1875 }
1876
1877 /**
1878  * gst_query_get_n_allocation_metas:
1879  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1880  *
1881  * Retrieve the number of values currently stored in the
1882  * meta API array of the query's structure.
1883  *
1884  * Returns: the metadata API array size as a #guint.
1885  */
1886 guint
1887 gst_query_get_n_allocation_metas (GstQuery * query)
1888 {
1889   GArray *array;
1890   GstStructure *structure;
1891
1892   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION, 0);
1893
1894   structure = GST_QUERY_STRUCTURE (query);
1895   array =
1896       ensure_array (structure, GST_QUARK (META), sizeof (AllocationMeta),
1897       (GDestroyNotify) allocation_meta_free);
1898
1899   return array->len;
1900 }
1901
1902 /**
1903  * gst_query_parse_nth_allocation_meta:
1904  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1905  * @index: position in the metadata API array to read
1906  * @params: (out) (transfer none) (allow-none): API specific parameters
1907  *
1908  * Parse an available query and get the metadata API
1909  * at @index of the metadata API array.
1910  *
1911  * Returns: a #GType of the metadata API at @index.
1912  */
1913 GType
1914 gst_query_parse_nth_allocation_meta (GstQuery * query, guint index,
1915     const GstStructure ** params)
1916 {
1917   GArray *array;
1918   GstStructure *structure;
1919   AllocationMeta *am;
1920
1921   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION, 0);
1922
1923   structure = GST_QUERY_STRUCTURE (query);
1924   array =
1925       ensure_array (structure, GST_QUARK (META), sizeof (AllocationMeta),
1926       (GDestroyNotify) allocation_meta_free);
1927
1928   g_return_val_if_fail (index < array->len, 0);
1929
1930   am = &g_array_index (array, AllocationMeta, index);
1931
1932   if (params)
1933     *params = am->params;
1934
1935   return am->api;
1936 }
1937
1938 /**
1939  * gst_query_remove_nth_allocation_meta:
1940  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1941  * @index: position in the metadata API array to remove
1942  *
1943  * Remove the metadata API at @index of the metadata API array.
1944  */
1945 void
1946 gst_query_remove_nth_allocation_meta (GstQuery * query, guint index)
1947 {
1948   GArray *array;
1949   GstStructure *structure;
1950
1951   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
1952   g_return_if_fail (gst_query_is_writable (query));
1953
1954   structure = GST_QUERY_STRUCTURE (query);
1955   array =
1956       ensure_array (structure, GST_QUARK (META), sizeof (AllocationMeta),
1957       (GDestroyNotify) allocation_meta_free);
1958   g_return_if_fail (index < array->len);
1959
1960   g_array_remove_index (array, index);
1961 }
1962
1963 /**
1964  * gst_query_find_allocation_meta:
1965  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
1966  * @api: the metadata API
1967  * @index: (out) (transfer none) (allow-none): the index
1968  *
1969  * Check if @query has metadata @api set. When this function returns %TRUE,
1970  * @index will contain the index where the requested API and the parameters
1971  * can be found.
1972  *
1973  * Returns: %TRUE when @api is in the list of metadata.
1974  */
1975 gboolean
1976 gst_query_find_allocation_meta (GstQuery * query, GType api, guint * index)
1977 {
1978   GArray *array;
1979   GstStructure *structure;
1980   guint i, len;
1981
1982   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION, FALSE);
1983   g_return_val_if_fail (api != 0, FALSE);
1984
1985   structure = GST_QUERY_STRUCTURE (query);
1986   array =
1987       ensure_array (structure, GST_QUARK (META), sizeof (AllocationMeta),
1988       (GDestroyNotify) allocation_meta_free);
1989
1990   len = array->len;
1991   for (i = 0; i < len; i++) {
1992     AllocationMeta *am = &g_array_index (array, AllocationMeta, i);
1993     if (am->api == api) {
1994       if (index)
1995         *index = i;
1996       return TRUE;
1997     }
1998   }
1999   return FALSE;
2000 }
2001
2002 typedef struct
2003 {
2004   GstAllocator *allocator;
2005   GstAllocationParams params;
2006 } AllocationParam;
2007
2008 static void
2009 allocation_param_free (AllocationParam * ap)
2010 {
2011   if (ap->allocator)
2012     gst_object_unref (ap->allocator);
2013 }
2014
2015 /**
2016  * gst_query_add_allocation_param:
2017  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
2018  * @allocator: (transfer none) (allow-none): the memory allocator
2019  * @params: (transfer none) (allow-none): a #GstAllocationParams
2020  *
2021  * Add @allocator and its @params as a supported memory allocator.
2022  */
2023 void
2024 gst_query_add_allocation_param (GstQuery * query, GstAllocator * allocator,
2025     const GstAllocationParams * params)
2026 {
2027   GArray *array;
2028   GstStructure *structure;
2029   AllocationParam ap;
2030
2031   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
2032   g_return_if_fail (gst_query_is_writable (query));
2033   g_return_if_fail (allocator != NULL || params != NULL);
2034
2035   structure = GST_QUERY_STRUCTURE (query);
2036   array = ensure_array (structure, GST_QUARK (ALLOCATOR),
2037       sizeof (AllocationParam), (GDestroyNotify) allocation_param_free);
2038
2039   if ((ap.allocator = allocator))
2040     gst_object_ref (allocator);
2041   if (params)
2042     ap.params = *params;
2043   else
2044     gst_allocation_params_init (&ap.params);
2045
2046   g_array_append_val (array, ap);
2047 }
2048
2049 /**
2050  * gst_query_get_n_allocation_params:
2051  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
2052  *
2053  * Retrieve the number of values currently stored in the
2054  * allocator params array of the query's structure.
2055  *
2056  * If no memory allocator is specified, the downstream element can handle
2057  * the default memory allocator. The first memory allocator in the query
2058  * should be generic and allow mapping to system memory, all following
2059  * allocators should be ordered by preference with the preferred one first.
2060  *
2061  * Returns: the allocator array size as a #guint.
2062  */
2063 guint
2064 gst_query_get_n_allocation_params (GstQuery * query)
2065 {
2066   GArray *array;
2067   GstStructure *structure;
2068
2069   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION, 0);
2070
2071   structure = GST_QUERY_STRUCTURE (query);
2072   array = ensure_array (structure, GST_QUARK (ALLOCATOR),
2073       sizeof (AllocationParam), (GDestroyNotify) allocation_param_free);
2074
2075   return array->len;
2076 }
2077
2078 /**
2079  * gst_query_parse_nth_allocation_param:
2080  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
2081  * @index: position in the allocator array to read
2082  * @allocator: (out) (transfer full) (allow-none): variable to hold the result
2083  * @params: (out) (allow-none): parameters for the allocator
2084  *
2085  * Parse an available query and get the allocator and its params
2086  * at @index of the allocator array.
2087  */
2088 void
2089 gst_query_parse_nth_allocation_param (GstQuery * query, guint index,
2090     GstAllocator ** allocator, GstAllocationParams * params)
2091 {
2092   GArray *array;
2093   GstStructure *structure;
2094   AllocationParam *ap;
2095
2096   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
2097
2098   structure = GST_QUERY_STRUCTURE (query);
2099   array = ensure_array (structure, GST_QUARK (ALLOCATOR),
2100       sizeof (AllocationParam), (GDestroyNotify) allocation_param_free);
2101   g_return_if_fail (index < array->len);
2102
2103   ap = &g_array_index (array, AllocationParam, index);
2104
2105   if (allocator)
2106     if ((*allocator = ap->allocator))
2107       gst_object_ref (*allocator);
2108   if (params)
2109     *params = ap->params;
2110 }
2111
2112 /**
2113  * gst_query_set_nth_allocation_param:
2114  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
2115  * @index: position in the allocator array to set
2116  * @allocator: (transfer none) (allow-none): new allocator to set
2117  * @params: (transfer none) (allow-none): parameters for the allocator
2118  *
2119  * Parse an available query and get the allocator and its params
2120  * at @index of the allocator array.
2121  */
2122 void
2123 gst_query_set_nth_allocation_param (GstQuery * query, guint index,
2124     GstAllocator * allocator, const GstAllocationParams * params)
2125 {
2126   GArray *array;
2127   GstStructure *structure;
2128   AllocationParam *old, ap;
2129
2130   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
2131
2132   structure = GST_QUERY_STRUCTURE (query);
2133   array = ensure_array (structure, GST_QUARK (ALLOCATOR),
2134       sizeof (AllocationParam), (GDestroyNotify) allocation_param_free);
2135   g_return_if_fail (index < array->len);
2136
2137   old = &g_array_index (array, AllocationParam, index);
2138   allocation_param_free (old);
2139
2140   if ((ap.allocator = allocator))
2141     gst_object_ref (allocator);
2142   if (params)
2143     ap.params = *params;
2144   else
2145     gst_allocation_params_init (&ap.params);
2146
2147   g_array_index (array, AllocationParam, index) = ap;
2148 }
2149
2150 /**
2151  * gst_query_remove_nth_allocation_param:
2152  * @query: a GST_QUERY_ALLOCATION type query #GstQuery
2153  * @index: position in the allocation param array to remove
2154  *
2155  * Remove the allocation param at @index of the allocation param array.
2156  *
2157  * Since: 1.2
2158  */
2159 void
2160 gst_query_remove_nth_allocation_param (GstQuery * query, guint index)
2161 {
2162   GArray *array;
2163   GstStructure *structure;
2164
2165   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ALLOCATION);
2166   g_return_if_fail (gst_query_is_writable (query));
2167
2168   structure = GST_QUERY_STRUCTURE (query);
2169   array =
2170       ensure_array (structure, GST_QUARK (ALLOCATOR), sizeof (AllocationParam),
2171       (GDestroyNotify) allocation_param_free);
2172   g_return_if_fail (index < array->len);
2173
2174   g_array_remove_index (array, index);
2175 }
2176
2177 /**
2178  * gst_query_new_scheduling:
2179  *
2180  * Constructs a new query object for querying the scheduling properties.
2181  *
2182  * Free-function: gst_query_unref()
2183  *
2184  * Returns: (transfer full): a new #GstQuery
2185  */
2186 GstQuery *
2187 gst_query_new_scheduling (void)
2188 {
2189   GstQuery *query;
2190   GstStructure *structure;
2191
2192   structure = gst_structure_new_id (GST_QUARK (QUERY_SCHEDULING),
2193       GST_QUARK (FLAGS), GST_TYPE_SCHEDULING_FLAGS, 0,
2194       GST_QUARK (MINSIZE), G_TYPE_INT, 1,
2195       GST_QUARK (MAXSIZE), G_TYPE_INT, -1,
2196       GST_QUARK (ALIGN), G_TYPE_INT, 0, NULL);
2197   query = gst_query_new_custom (GST_QUERY_SCHEDULING, structure);
2198
2199   return query;
2200 }
2201
2202 /**
2203  * gst_query_set_scheduling:
2204  * @query: A valid #GstQuery of type GST_QUERY_SCHEDULING.
2205  * @flags: #GstSchedulingFlags
2206  * @minsize: the suggested minimum size of pull requests
2207  * @maxsize: the suggested maximum size of pull requests
2208  * @align: the suggested alignment of pull requests
2209  *
2210  * Set the scheduling properties.
2211  */
2212 void
2213 gst_query_set_scheduling (GstQuery * query, GstSchedulingFlags flags,
2214     gint minsize, gint maxsize, gint align)
2215 {
2216   GstStructure *structure;
2217
2218   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SCHEDULING);
2219   g_return_if_fail (gst_query_is_writable (query));
2220
2221   structure = GST_QUERY_STRUCTURE (query);
2222   gst_structure_id_set (structure,
2223       GST_QUARK (FLAGS), GST_TYPE_SCHEDULING_FLAGS, flags,
2224       GST_QUARK (MINSIZE), G_TYPE_INT, minsize,
2225       GST_QUARK (MAXSIZE), G_TYPE_INT, maxsize,
2226       GST_QUARK (ALIGN), G_TYPE_INT, align, NULL);
2227 }
2228
2229 /**
2230  * gst_query_parse_scheduling:
2231  * @query: A valid #GstQuery of type GST_QUERY_SCHEDULING.
2232  * @flags: (out) (allow-none): #GstSchedulingFlags
2233  * @minsize: (out) (allow-none): the suggested minimum size of pull requests
2234  * @maxsize: (out) (allow-none): the suggested maximum size of pull requests:
2235  * @align: (out) (allow-none): the suggested alignment of pull requests
2236  *
2237  * Set the scheduling properties.
2238  */
2239 void
2240 gst_query_parse_scheduling (GstQuery * query, GstSchedulingFlags * flags,
2241     gint * minsize, gint * maxsize, gint * align)
2242 {
2243   GstStructure *structure;
2244
2245   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SCHEDULING);
2246
2247   structure = GST_QUERY_STRUCTURE (query);
2248   gst_structure_id_get (structure,
2249       GST_QUARK (FLAGS), GST_TYPE_SCHEDULING_FLAGS, flags,
2250       GST_QUARK (MINSIZE), G_TYPE_INT, minsize,
2251       GST_QUARK (MAXSIZE), G_TYPE_INT, maxsize,
2252       GST_QUARK (ALIGN), G_TYPE_INT, align, NULL);
2253 }
2254
2255 /**
2256  * gst_query_add_scheduling_mode:
2257  * @query: a GST_QUERY_SCHEDULING type query #GstQuery
2258  * @mode: a #GstPadMode
2259  *
2260  * Add @mode as one of the supported scheduling modes to @query.
2261  */
2262 void
2263 gst_query_add_scheduling_mode (GstQuery * query, GstPadMode mode)
2264 {
2265   GstStructure *structure;
2266   GArray *array;
2267
2268   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SCHEDULING);
2269   g_return_if_fail (gst_query_is_writable (query));
2270
2271   structure = GST_QUERY_STRUCTURE (query);
2272   array =
2273       ensure_array (structure, GST_QUARK (MODES), sizeof (GstPadMode), NULL);
2274
2275   g_array_append_val (array, mode);
2276 }
2277
2278 /**
2279  * gst_query_get_n_scheduling_modes:
2280  * @query: a GST_QUERY_SCHEDULING type query #GstQuery
2281  *
2282  * Retrieve the number of values currently stored in the
2283  * scheduling mode array of the query's structure.
2284  *
2285  * Returns: the scheduling mode array size as a #guint.
2286  */
2287 guint
2288 gst_query_get_n_scheduling_modes (GstQuery * query)
2289 {
2290   GArray *array;
2291   GstStructure *structure;
2292
2293   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SCHEDULING, 0);
2294
2295   structure = GST_QUERY_STRUCTURE (query);
2296   array =
2297       ensure_array (structure, GST_QUARK (MODES), sizeof (GstPadMode), NULL);
2298
2299   return array->len;
2300 }
2301
2302 /**
2303  * gst_query_parse_nth_scheduling_mode:
2304  * @query: a GST_QUERY_SCHEDULING type query #GstQuery
2305  * @index: position in the scheduling modes array to read
2306  *
2307  * Parse an available query and get the scheduling mode
2308  * at @index of the scheduling modes array.
2309  *
2310  * Returns: a #GstPadMode of the scheduling mode at @index.
2311  */
2312 GstPadMode
2313 gst_query_parse_nth_scheduling_mode (GstQuery * query, guint index)
2314 {
2315   GstStructure *structure;
2316   GArray *array;
2317
2318   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SCHEDULING,
2319       GST_PAD_MODE_NONE);
2320
2321   structure = GST_QUERY_STRUCTURE (query);
2322   array =
2323       ensure_array (structure, GST_QUARK (MODES), sizeof (GstPadMode), NULL);
2324   g_return_val_if_fail (index < array->len, GST_PAD_MODE_NONE);
2325
2326   return g_array_index (array, GstPadMode, index);
2327 }
2328
2329 /**
2330  * gst_query_has_scheduling_mode:
2331  * @query: a GST_QUERY_SCHEDULING type query #GstQuery
2332  * @mode: the scheduling mode
2333  *
2334  * Check if @query has scheduling mode set.
2335  *
2336  * > When checking if upstream supports pull mode, it is usually not
2337  * > enough to just check for GST_PAD_MODE_PULL with this function, you
2338  * > also want to check whether the scheduling flags returned by
2339  * > gst_query_parse_scheduling() have the seeking flag set (meaning
2340  * > random access is supported, not only sequential pulls).
2341  *
2342  * Returns: %TRUE when @mode is in the list of scheduling modes.
2343  */
2344 gboolean
2345 gst_query_has_scheduling_mode (GstQuery * query, GstPadMode mode)
2346 {
2347   GstStructure *structure;
2348   GArray *array;
2349   guint i, len;
2350
2351   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SCHEDULING, FALSE);
2352
2353   structure = GST_QUERY_STRUCTURE (query);
2354   array =
2355       ensure_array (structure, GST_QUARK (MODES), sizeof (GstPadMode), NULL);
2356
2357   len = array->len;
2358   for (i = 0; i < len; i++) {
2359     if (mode == g_array_index (array, GstPadMode, i))
2360       return TRUE;
2361   }
2362   return FALSE;
2363 }
2364
2365 /**
2366  * gst_query_has_scheduling_mode_with_flags:
2367  * @query: a GST_QUERY_SCHEDULING type query #GstQuery
2368  * @mode: the scheduling mode
2369  * @flags: #GstSchedulingFlags
2370  *
2371  * Check if @query has scheduling mode set and @flags is set in
2372  * query scheduling flags.
2373  *
2374  * Returns: %TRUE when @mode is in the list of scheduling modes
2375  *    and @flags are compatible with query flags.
2376  */
2377 gboolean
2378 gst_query_has_scheduling_mode_with_flags (GstQuery * query, GstPadMode mode,
2379     GstSchedulingFlags flags)
2380 {
2381   GstSchedulingFlags sched_flags;
2382
2383   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SCHEDULING, FALSE);
2384
2385   gst_query_parse_scheduling (query, &sched_flags, NULL, NULL, NULL);
2386
2387   return ((flags & sched_flags) == flags) &&
2388       gst_query_has_scheduling_mode (query, mode);
2389 }
2390
2391 /**
2392  * gst_query_new_accept_caps:
2393  * @caps: a fixed #GstCaps
2394  *
2395  * Constructs a new query object for querying if @caps are accepted.
2396  *
2397  * Free-function: gst_query_unref()
2398  *
2399  * Returns: (transfer full): a new #GstQuery
2400  */
2401 GstQuery *
2402 gst_query_new_accept_caps (GstCaps * caps)
2403 {
2404   GstQuery *query;
2405   GstStructure *structure;
2406
2407   g_return_val_if_fail (gst_caps_is_fixed (caps), NULL);
2408
2409   structure = gst_structure_new_id (GST_QUARK (QUERY_ACCEPT_CAPS),
2410       GST_QUARK (CAPS), GST_TYPE_CAPS, caps,
2411       GST_QUARK (RESULT), G_TYPE_BOOLEAN, FALSE, NULL);
2412   query = gst_query_new_custom (GST_QUERY_ACCEPT_CAPS, structure);
2413
2414   return query;
2415 }
2416
2417 /**
2418  * gst_query_parse_accept_caps:
2419  * @query: The query to parse
2420  * @caps: (out) (transfer none): A pointer to the caps
2421  *
2422  * Get the caps from @query. The caps remains valid as long as @query remains
2423  * valid.
2424  */
2425 void
2426 gst_query_parse_accept_caps (GstQuery * query, GstCaps ** caps)
2427 {
2428   GstStructure *structure;
2429
2430   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ACCEPT_CAPS);
2431   g_return_if_fail (caps != NULL);
2432
2433   structure = GST_QUERY_STRUCTURE (query);
2434   *caps = g_value_get_boxed (gst_structure_id_get_value (structure,
2435           GST_QUARK (CAPS)));
2436 }
2437
2438 /**
2439  * gst_query_set_accept_caps_result:
2440  * @query: a GST_QUERY_ACCEPT_CAPS type query #GstQuery
2441  * @result: the result to set
2442  *
2443  * Set @result as the result for the @query.
2444  */
2445 void
2446 gst_query_set_accept_caps_result (GstQuery * query, gboolean result)
2447 {
2448   GstStructure *structure;
2449
2450   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ACCEPT_CAPS);
2451   g_return_if_fail (gst_query_is_writable (query));
2452
2453   structure = GST_QUERY_STRUCTURE (query);
2454   gst_structure_id_set (structure,
2455       GST_QUARK (RESULT), G_TYPE_BOOLEAN, result, NULL);
2456 }
2457
2458 /**
2459  * gst_query_parse_accept_caps_result:
2460  * @query: a GST_QUERY_ACCEPT_CAPS type query #GstQuery
2461  * @result: (out) (allow-none): location for the result
2462  *
2463  * Parse the result from @query and store in @result.
2464  */
2465 void
2466 gst_query_parse_accept_caps_result (GstQuery * query, gboolean * result)
2467 {
2468   GstStructure *structure;
2469
2470   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_ACCEPT_CAPS);
2471
2472   structure = GST_QUERY_STRUCTURE (query);
2473   gst_structure_id_get (structure,
2474       GST_QUARK (RESULT), G_TYPE_BOOLEAN, result, NULL);
2475 }
2476
2477 /**
2478  * gst_query_new_caps:
2479  * @filter: a filter
2480  *
2481  * Constructs a new query object for querying the caps.
2482  *
2483  * The CAPS query should return the allowable caps for a pad in the context
2484  * of the element's state, its link to other elements, and the devices or files
2485  * it has opened. These caps must be a subset of the pad template caps. In the
2486  * NULL state with no links, the CAPS query should ideally return the same caps
2487  * as the pad template. In rare circumstances, an object property can affect
2488  * the caps returned by the CAPS query, but this is discouraged.
2489  *
2490  * For most filters, the caps returned by CAPS query is directly affected by the
2491  * allowed caps on other pads. For demuxers and decoders, the caps returned by
2492  * the srcpad's getcaps function is directly related to the stream data. Again,
2493  * the CAPS query should return the most specific caps it reasonably can, since this
2494  * helps with autoplugging.
2495  *
2496  * The @filter is used to restrict the result caps, only the caps matching
2497  * @filter should be returned from the CAPS query. Specifying a filter might
2498  * greatly reduce the amount of processing an element needs to do.
2499  *
2500  * Free-function: gst_query_unref()
2501  *
2502  * Returns: (transfer full): a new #GstQuery
2503  */
2504 GstQuery *
2505 gst_query_new_caps (GstCaps * filter)
2506 {
2507   GstQuery *query;
2508   GstStructure *structure;
2509
2510   structure = gst_structure_new_id (GST_QUARK (QUERY_CAPS),
2511       GST_QUARK (FILTER), GST_TYPE_CAPS, filter,
2512       GST_QUARK (CAPS), GST_TYPE_CAPS, NULL, NULL);
2513   query = gst_query_new_custom (GST_QUERY_CAPS, structure);
2514
2515   return query;
2516 }
2517
2518 /**
2519  * gst_query_parse_caps:
2520  * @query: The query to parse
2521  * @filter: (out) (transfer none): A pointer to the caps filter
2522  *
2523  * Get the filter from the caps @query. The caps remains valid as long as
2524  * @query remains valid.
2525  */
2526 void
2527 gst_query_parse_caps (GstQuery * query, GstCaps ** filter)
2528 {
2529   GstStructure *structure;
2530
2531   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CAPS);
2532   g_return_if_fail (filter != NULL);
2533
2534   structure = GST_QUERY_STRUCTURE (query);
2535   *filter = g_value_get_boxed (gst_structure_id_get_value (structure,
2536           GST_QUARK (FILTER)));
2537 }
2538
2539 /**
2540  * gst_query_set_caps_result:
2541  * @query: The query to use
2542  * @caps: (in): A pointer to the caps
2543  *
2544  * Set the @caps result in @query.
2545  */
2546 void
2547 gst_query_set_caps_result (GstQuery * query, GstCaps * caps)
2548 {
2549   GstStructure *structure;
2550
2551   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CAPS);
2552   g_return_if_fail (gst_query_is_writable (query));
2553
2554   structure = GST_QUERY_STRUCTURE (query);
2555   gst_structure_id_set (structure, GST_QUARK (CAPS), GST_TYPE_CAPS, caps, NULL);
2556 }
2557
2558 /**
2559  * gst_query_parse_caps_result:
2560  * @query: The query to parse
2561  * @caps: (out) (transfer none): A pointer to the caps
2562  *
2563  * Get the caps result from @query. The caps remains valid as long as
2564  * @query remains valid.
2565  */
2566 void
2567 gst_query_parse_caps_result (GstQuery * query, GstCaps ** caps)
2568 {
2569   GstStructure *structure;
2570
2571   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CAPS);
2572   g_return_if_fail (caps != NULL);
2573
2574   structure = GST_QUERY_STRUCTURE (query);
2575   *caps = g_value_get_boxed (gst_structure_id_get_value (structure,
2576           GST_QUARK (CAPS)));
2577 }
2578
2579 #if 0
2580 void
2581 gst_query_intersect_caps_result (GstQuery * query, GstCaps * filter,
2582     GstCapsIntersectMode mode)
2583 {
2584   GstCaps *res, *caps = NULL;
2585
2586   gst_query_parse_caps_result (query, &caps);
2587   res = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
2588   gst_query_set_caps_result (query, res);
2589   gst_caps_unref (res);
2590 }
2591 #endif
2592
2593 /**
2594  * gst_query_new_drain:
2595  *
2596  * Constructs a new query object for querying the drain state.
2597  *
2598  * Free-function: gst_query_unref()
2599  *
2600  * Returns: (transfer full): a new #GstQuery
2601  */
2602 GstQuery *
2603 gst_query_new_drain (void)
2604 {
2605   GstQuery *query;
2606   GstStructure *structure;
2607
2608   structure = gst_structure_new_id_empty (GST_QUARK (QUERY_DRAIN));
2609   query = gst_query_new_custom (GST_QUERY_DRAIN, structure);
2610
2611   return query;
2612 }
2613
2614 /**
2615  * gst_query_new_context:
2616  * @context_type: Context type to query
2617  *
2618  * Constructs a new query object for querying the pipeline-local context.
2619  *
2620  * Free-function: gst_query_unref()
2621  *
2622  * Returns: (transfer full): a new #GstQuery
2623  *
2624  * Since: 1.2
2625  */
2626 GstQuery *
2627 gst_query_new_context (const gchar * context_type)
2628 {
2629   GstQuery *query;
2630   GstStructure *structure;
2631
2632   g_return_val_if_fail (context_type != NULL, NULL);
2633
2634   structure = gst_structure_new_id (GST_QUARK (QUERY_CONTEXT),
2635       GST_QUARK (CONTEXT_TYPE), G_TYPE_STRING, context_type, NULL);
2636   query = gst_query_new_custom (GST_QUERY_CONTEXT, structure);
2637
2638   return query;
2639 }
2640
2641 /**
2642  * gst_query_set_context:
2643  * @query: a #GstQuery with query type GST_QUERY_CONTEXT
2644  * @context: the requested #GstContext
2645  *
2646  * Answer a context query by setting the requested context.
2647  *
2648  * Since: 1.2
2649  */
2650 void
2651 gst_query_set_context (GstQuery * query, GstContext * context)
2652 {
2653   GstStructure *s;
2654   const gchar *context_type;
2655
2656   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CONTEXT);
2657
2658   gst_query_parse_context_type (query, &context_type);
2659   g_return_if_fail (strcmp (gst_context_get_context_type (context),
2660           context_type) == 0);
2661
2662   s = GST_QUERY_STRUCTURE (query);
2663
2664   gst_structure_id_set (s,
2665       GST_QUARK (CONTEXT), GST_TYPE_CONTEXT, context, NULL);
2666 }
2667
2668 /**
2669  * gst_query_parse_context:
2670  * @query: The query to parse
2671  * @context: (out) (transfer none): A pointer to store the #GstContext
2672  *
2673  * Get the context from the context @query. The context remains valid as long as
2674  * @query remains valid.
2675  *
2676  * Since: 1.2
2677  */
2678 void
2679 gst_query_parse_context (GstQuery * query, GstContext ** context)
2680 {
2681   GstStructure *structure;
2682   const GValue *v;
2683
2684   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CONTEXT);
2685   g_return_if_fail (context != NULL);
2686
2687   structure = GST_QUERY_STRUCTURE (query);
2688   v = gst_structure_id_get_value (structure, GST_QUARK (CONTEXT));
2689   if (v)
2690     *context = g_value_get_boxed (v);
2691   else
2692     *context = NULL;
2693 }
2694
2695 /**
2696  * gst_query_parse_context_type:
2697  * @query: a GST_QUERY_CONTEXT type query
2698  * @context_type: (out) (transfer none) (allow-none): the context type, or %NULL
2699  *
2700  * Parse a context type from an existing GST_QUERY_CONTEXT query.
2701  *
2702  * Returns: a #gboolean indicating if the parsing succeeded.
2703  *
2704  * Since: 1.2
2705  */
2706 gboolean
2707 gst_query_parse_context_type (GstQuery * query, const gchar ** context_type)
2708 {
2709   GstStructure *structure;
2710   const GValue *value;
2711
2712   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CONTEXT, FALSE);
2713
2714   structure = GST_QUERY_STRUCTURE (query);
2715
2716   if (context_type) {
2717     value = gst_structure_id_get_value (structure, GST_QUARK (CONTEXT_TYPE));
2718     *context_type = g_value_get_string (value);
2719   }
2720
2721   return TRUE;
2722 }
2723
2724 /**
2725  * gst_query_new_bitrate:
2726  *
2727  * Constructs a new query object for querying the bitrate.
2728  *
2729  * Free-function: gst_query_unref()
2730  *
2731  * Returns: (transfer full): a new #GstQuery
2732  *
2733  * Since: 1.16
2734  */
2735 GstQuery *
2736 gst_query_new_bitrate (void)
2737 {
2738   GstQuery *query;
2739   GstStructure *structure;
2740
2741   structure = gst_structure_new_id_empty (GST_QUARK (QUERY_BITRATE));
2742   query = gst_query_new_custom (GST_QUERY_BITRATE, structure);
2743
2744   return query;
2745 }
2746
2747 /**
2748  * gst_query_set_bitrate:
2749  * @query: a GST_QUERY_BITRATE type #GstQuery
2750  * @nominal_bitrate: the nominal bitrate in bits per second
2751  *
2752  * Set the results of a bitrate query.  The nominal bitrate is the average
2753  * bitrate expected over the length of the stream as advertised in file
2754  * headers (or similar).
2755  *
2756  * Since: 1.16
2757  */
2758 void
2759 gst_query_set_bitrate (GstQuery * query, guint nominal_bitrate)
2760 {
2761   GstStructure *s;
2762
2763   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BITRATE);
2764
2765   s = GST_QUERY_STRUCTURE (query);
2766
2767   gst_structure_id_set (s,
2768       GST_QUARK (NOMINAL_BITRATE), G_TYPE_UINT, nominal_bitrate, NULL);
2769 }
2770
2771 /**
2772  * gst_query_parse_bitrate:
2773  * @query: a GST_QUERY_BITRATE type #GstQuery
2774  * @nominal_bitrate: (out) (allow-none): The resulting bitrate in bits per second
2775  *
2776  * Get the results of a bitrate query. See also gst_query_set_bitrate().
2777  *
2778  * Since: 1.16
2779  */
2780 void
2781 gst_query_parse_bitrate (GstQuery * query, guint * nominal_bitrate)
2782 {
2783   GstStructure *structure;
2784   const GValue *value;
2785
2786   g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BITRATE);
2787
2788   structure = GST_QUERY_STRUCTURE (query);
2789
2790   if (nominal_bitrate) {
2791     value = gst_structure_id_get_value (structure, GST_QUARK (NOMINAL_BITRATE));
2792     *nominal_bitrate = g_value_get_uint (value);
2793   }
2794 }