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