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