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