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>
6 * gstquery.c: GstQueryType registration and Query parsing/creation
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.
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.
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.
26 * @short_description: Dynamically register new query types. Provide functions
27 * to create queries, and to set and parse values in them.
28 * @see_also: #GstPad, #GstElement
30 * GstQuery functions are used to register new query types to the gstreamer
32 * Queries can be performed on pads (gst_pad_query()) and elements
33 * (gst_element_query()). Please note that some queries might need a running
36 * Queries can be created using the gst_query_new_*() functions.
37 * Query values can be set using gst_query_set_*(), and parsed using
38 * gst_query_parse_*() helpers.
40 * The following example shows how to query the duration of a pipeline:
43 * <title>Query duration on a pipeline</title>
47 * query = gst_query_new_duration (GST_FORMAT_TIME);
48 * res = gst_element_query (pipeline, query);
51 * gst_query_parse_duration (query, NULL, &duration);
52 * g_print ("duration = %"GST_TIME_FORMAT, GST_TIME_ARGS (duration));
55 * g_print ("duration query failed...");
57 * gst_query_unref (query);
61 * Last reviewed on 2006-02-14 (0.10.4)
65 /* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
66 * with newer GLib versions (>= 2.31.0) */
67 #define GLIB_DISABLE_DEPRECATION_WARNINGS
69 #include "gst_private.h"
73 #include "gstenumtypes.h"
77 GST_DEBUG_CATEGORY_STATIC (gst_query_debug);
78 #define GST_CAT_DEFAULT gst_query_debug
80 static void gst_query_finalize (GstQuery * query);
81 static GstQuery *_gst_query_copy (GstQuery * query);
83 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
84 static GList *_gst_queries = NULL;
85 static GHashTable *_nick_to_query = NULL;
86 static GHashTable *_query_type_to_nick = NULL;
87 static guint32 _n_values = 1; /* we start from 1 because 0 reserved for NONE */
89 static GstMiniObjectClass *parent_class = NULL;
91 static GstQueryTypeDefinition standard_definitions[] = {
92 {GST_QUERY_POSITION, "position", "Current position", 0},
93 {GST_QUERY_DURATION, "duration", "Total duration", 0},
94 {GST_QUERY_LATENCY, "latency", "Latency", 0},
95 {GST_QUERY_JITTER, "jitter", "Jitter", 0},
96 {GST_QUERY_RATE, "rate", "Configured rate 1000000 = 1", 0},
97 {GST_QUERY_SEEKING, "seeking", "Seeking capabilities and parameters", 0},
98 {GST_QUERY_SEGMENT, "segment", "currently configured segment", 0},
99 {GST_QUERY_CONVERT, "convert", "Converting between formats", 0},
100 {GST_QUERY_FORMATS, "formats", "Supported formats for conversion", 0},
101 {GST_QUERY_BUFFERING, "buffering", "Buffering status", 0},
102 {GST_QUERY_CUSTOM, "custom", "Custom query", 0},
103 {GST_QUERY_URI, "uri", "URI of the source or sink", 0},
104 {GST_QUERY_NONE, NULL, NULL, 0}
108 _gst_query_initialize (void)
110 GstQueryTypeDefinition *standards = standard_definitions;
112 GST_CAT_INFO (GST_CAT_GST_INIT, "init queries");
114 GST_DEBUG_CATEGORY_INIT (gst_query_debug, "query", 0, "query system");
116 g_static_mutex_lock (&mutex);
117 if (_nick_to_query == NULL) {
118 _nick_to_query = g_hash_table_new (g_str_hash, g_str_equal);
119 _query_type_to_nick = g_hash_table_new (NULL, NULL);
122 while (standards->nick) {
123 standards->quark = g_quark_from_static_string (standards->nick);
124 g_hash_table_insert (_nick_to_query, (gpointer) standards->nick, standards);
125 g_hash_table_insert (_query_type_to_nick,
126 GINT_TO_POINTER (standards->value), standards);
128 _gst_queries = g_list_append (_gst_queries, standards);
132 g_static_mutex_unlock (&mutex);
134 g_type_class_ref (gst_query_get_type ());
138 * gst_query_type_get_name:
139 * @query: the query type
141 * Get a printable name for the given query type. Do not modify or free.
143 * Returns: a reference to the static name of the query.
146 gst_query_type_get_name (GstQueryType query)
148 const GstQueryTypeDefinition *def;
150 def = gst_query_type_get_details (query);
156 * gst_query_type_to_quark:
157 * @query: the query type
159 * Get the unique quark for the given query type.
161 * Returns: the quark associated with the query type
164 gst_query_type_to_quark (GstQueryType query)
166 const GstQueryTypeDefinition *def;
168 def = gst_query_type_get_details (query);
173 G_DEFINE_TYPE (GstQuery, gst_query, GST_TYPE_MINI_OBJECT);
176 gst_query_class_init (GstQueryClass * klass)
178 parent_class = g_type_class_peek_parent (klass);
180 klass->mini_object_class.copy = (GstMiniObjectCopyFunction) _gst_query_copy;
181 klass->mini_object_class.finalize =
182 (GstMiniObjectFinalizeFunction) gst_query_finalize;
187 gst_query_init (GstQuery * query)
192 gst_query_finalize (GstQuery * query)
194 g_return_if_fail (query != NULL);
196 if (query->structure) {
197 gst_structure_set_parent_refcount (query->structure, NULL);
198 gst_structure_free (query->structure);
201 /* GST_MINI_OBJECT_CLASS (parent_class)->finalize (GST_MINI_OBJECT (query)); */
205 _gst_query_copy (GstQuery * query)
209 copy = (GstQuery *) gst_mini_object_new (GST_TYPE_QUERY);
211 copy->type = query->type;
213 if (query->structure) {
214 copy->structure = gst_structure_copy (query->structure);
215 gst_structure_set_parent_refcount (copy->structure,
216 &query->mini_object.refcount);
225 * gst_query_type_register:
226 * @nick: The nick of the new query
227 * @description: The description of the new query
229 * Create a new GstQueryType based on the nick or return an
230 * already registered query with that nick
232 * Returns: A new GstQueryType or an already registered query
233 * with the same nick.
236 gst_query_type_register (const gchar * nick, const gchar * description)
238 GstQueryTypeDefinition *query;
241 g_return_val_if_fail (nick != NULL, GST_QUERY_NONE);
242 g_return_val_if_fail (description != NULL, GST_QUERY_NONE);
244 lookup = gst_query_type_get_by_nick (nick);
245 if (lookup != GST_QUERY_NONE)
248 query = g_slice_new (GstQueryTypeDefinition);
249 query->value = (GstQueryType) _n_values;
250 query->nick = g_strdup (nick);
251 query->description = g_strdup (description);
252 query->quark = g_quark_from_static_string (query->nick);
254 g_static_mutex_lock (&mutex);
255 g_hash_table_insert (_nick_to_query, (gpointer) query->nick, query);
256 g_hash_table_insert (_query_type_to_nick, GINT_TO_POINTER (query->value),
258 _gst_queries = g_list_append (_gst_queries, query);
260 g_static_mutex_unlock (&mutex);
266 * gst_query_type_get_by_nick:
267 * @nick: The nick of the query
269 * Get the query type registered with @nick.
271 * Returns: The query registered with @nick or #GST_QUERY_NONE
272 * if the query was not registered.
275 gst_query_type_get_by_nick (const gchar * nick)
277 GstQueryTypeDefinition *query;
279 g_return_val_if_fail (nick != NULL, GST_QUERY_NONE);
281 g_static_mutex_lock (&mutex);
282 query = g_hash_table_lookup (_nick_to_query, nick);
283 g_static_mutex_unlock (&mutex);
288 return GST_QUERY_NONE;
292 * gst_query_types_contains:
293 * @types: The query array to search
294 * @type: the #GstQueryType to find
296 * See if the given #GstQueryType is inside the @types query types array.
298 * Returns: TRUE if the type is found inside the array
301 gst_query_types_contains (const GstQueryType * types, GstQueryType type)
317 * gst_query_type_get_details:
318 * @type: a #GstQueryType
320 * Get details about the given #GstQueryType.
322 * Returns: The #GstQueryTypeDefinition for @type or NULL on failure.
324 const GstQueryTypeDefinition *
325 gst_query_type_get_details (GstQueryType type)
327 const GstQueryTypeDefinition *result;
329 g_static_mutex_lock (&mutex);
330 result = g_hash_table_lookup (_query_type_to_nick, GINT_TO_POINTER (type));
331 g_static_mutex_unlock (&mutex);
337 * gst_query_type_iterate_definitions:
339 * Get a #GstIterator of all the registered query types. The definitions
340 * iterated over are read only.
342 * Free-function: gst_iterator_free
344 * Returns: (transfer full): a #GstIterator of #GstQueryTypeDefinition.
347 gst_query_type_iterate_definitions (void)
351 g_static_mutex_lock (&mutex);
352 /* FIXME: register a boxed type for GstQueryTypeDefinition */
353 result = gst_iterator_new_list (G_TYPE_POINTER,
354 g_static_mutex_get_mutex (&mutex), &_n_values, &_gst_queries,
356 g_static_mutex_unlock (&mutex);
362 gst_query_new (GstQueryType type, GstStructure * structure)
366 query = (GstQuery *) gst_mini_object_new (GST_TYPE_QUERY);
368 GST_DEBUG ("creating new query %p %s", query, gst_query_type_get_name (type));
373 query->structure = structure;
374 gst_structure_set_parent_refcount (query->structure,
375 &query->mini_object.refcount);
377 query->structure = NULL;
384 * gst_query_new_position:
385 * @format: the default #GstFormat for the new query
387 * Constructs a new query stream position query object. Use gst_query_unref()
388 * when done with it. A position query is used to query the current position
389 * of playback in the streams, in some format.
391 * Free-function: gst_query_unref
393 * Returns: (transfer full): a new #GstQuery
396 gst_query_new_position (GstFormat format)
399 GstStructure *structure;
401 structure = gst_structure_id_new (GST_QUARK (QUERY_POSITION),
402 GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
403 GST_QUARK (CURRENT), G_TYPE_INT64, G_GINT64_CONSTANT (-1), NULL);
405 query = gst_query_new (GST_QUERY_POSITION, structure);
411 * gst_query_set_position:
412 * @query: a #GstQuery with query type GST_QUERY_POSITION
413 * @format: the requested #GstFormat
414 * @cur: the position to set
416 * Answer a position query by setting the requested value in the given format.
419 gst_query_set_position (GstQuery * query, GstFormat format, gint64 cur)
421 g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_POSITION);
423 gst_structure_id_set (query->structure,
424 GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
425 GST_QUARK (CURRENT), G_TYPE_INT64, cur, NULL);
429 * gst_query_parse_position:
430 * @query: a #GstQuery
431 * @format: (out) (allow-none): the storage for the #GstFormat of the
432 * position values (may be NULL)
433 * @cur: (out) (allow-none): the storage for the current position (may be NULL)
435 * Parse a position query, writing the format into @format, and the position
436 * into @cur, if the respective parameters are non-NULL.
439 gst_query_parse_position (GstQuery * query, GstFormat * format, gint64 * cur)
441 GstStructure *structure;
443 g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_POSITION);
445 structure = query->structure;
448 (GstFormat) g_value_get_enum (gst_structure_id_get_value (structure,
449 GST_QUARK (FORMAT)));
451 *cur = g_value_get_int64 (gst_structure_id_get_value (structure,
452 GST_QUARK (CURRENT)));
457 * gst_query_new_duration:
458 * @format: the #GstFormat for this duration query
460 * Constructs a new stream duration query object to query in the given format.
461 * Use gst_query_unref() when done with it. A duration query will give the
462 * total length of the stream.
464 * Free-function: gst_query_unref
466 * Returns: (transfer full): a new #GstQuery
469 gst_query_new_duration (GstFormat format)
472 GstStructure *structure;
474 structure = gst_structure_id_new (GST_QUARK (QUERY_DURATION),
475 GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
476 GST_QUARK (DURATION), G_TYPE_INT64, G_GINT64_CONSTANT (-1), NULL);
478 query = gst_query_new (GST_QUERY_DURATION, structure);
484 * gst_query_set_duration:
485 * @query: a #GstQuery
486 * @format: the #GstFormat for the duration
487 * @duration: the duration of the stream
489 * Answer a duration query by setting the requested value in the given format.
492 gst_query_set_duration (GstQuery * query, GstFormat format, gint64 duration)
494 g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_DURATION);
496 gst_structure_id_set (query->structure,
497 GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
498 GST_QUARK (DURATION), G_TYPE_INT64, duration, NULL);
502 * gst_query_parse_duration:
503 * @query: a #GstQuery
504 * @format: (out) (allow-none): the storage for the #GstFormat of the duration
506 * @duration: (out) (allow-none): the storage for the total duration, or NULL.
508 * Parse a duration query answer. Write the format of the duration into @format,
509 * and the value into @duration, if the respective variables are non-NULL.
512 gst_query_parse_duration (GstQuery * query, GstFormat * format,
515 GstStructure *structure;
517 g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_DURATION);
519 structure = query->structure;
522 (GstFormat) g_value_get_enum (gst_structure_id_get_value (structure,
523 GST_QUARK (FORMAT)));
525 *duration = g_value_get_int64 (gst_structure_id_get_value (structure,
526 GST_QUARK (DURATION)));
530 * gst_query_new_latency:
532 * Constructs a new latency query object.
533 * Use gst_query_unref() when done with it. A latency query is usually performed
534 * by sinks to compensate for additional latency introduced by elements in the
537 * Free-function: gst_query_unref
539 * Returns: (transfer full): a #GstQuery
544 gst_query_new_latency (void)
547 GstStructure *structure;
549 structure = gst_structure_id_new (GST_QUARK (QUERY_LATENCY),
550 GST_QUARK (LIVE), G_TYPE_BOOLEAN, FALSE,
551 GST_QUARK (MIN_LATENCY), G_TYPE_UINT64, G_GUINT64_CONSTANT (0),
552 GST_QUARK (MAX_LATENCY), G_TYPE_UINT64, G_GUINT64_CONSTANT (-1), NULL);
554 query = gst_query_new (GST_QUERY_LATENCY, structure);
560 * gst_query_set_latency:
561 * @query: a #GstQuery
562 * @live: if there is a live element upstream
563 * @min_latency: the minimal latency of the live element
564 * @max_latency: the maximal latency of the live element
566 * Answer a latency query by setting the requested values in the given format.
571 gst_query_set_latency (GstQuery * query, gboolean live,
572 GstClockTime min_latency, GstClockTime max_latency)
574 g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_LATENCY);
576 gst_structure_id_set (query->structure,
577 GST_QUARK (LIVE), G_TYPE_BOOLEAN, live,
578 GST_QUARK (MIN_LATENCY), G_TYPE_UINT64, min_latency,
579 GST_QUARK (MAX_LATENCY), G_TYPE_UINT64, max_latency, NULL);
583 * gst_query_parse_latency:
584 * @query: a #GstQuery
585 * @live: (out) (allow-none): storage for live or NULL
586 * @min_latency: (out) (allow-none): the storage for the min latency or NULL
587 * @max_latency: (out) (allow-none): the storage for the max latency or NULL
589 * Parse a latency query answer.
594 gst_query_parse_latency (GstQuery * query, gboolean * live,
595 GstClockTime * min_latency, GstClockTime * max_latency)
597 GstStructure *structure;
599 g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_LATENCY);
601 structure = query->structure;
604 g_value_get_boolean (gst_structure_id_get_value (structure,
607 *min_latency = g_value_get_uint64 (gst_structure_id_get_value (structure,
608 GST_QUARK (MIN_LATENCY)));
610 *max_latency = g_value_get_uint64 (gst_structure_id_get_value (structure,
611 GST_QUARK (MAX_LATENCY)));
615 * gst_query_new_convert:
616 * @src_format: the source #GstFormat for the new query
617 * @value: the value to convert
618 * @dest_format: the target #GstFormat
620 * Constructs a new convert query object. Use gst_query_unref()
621 * when done with it. A convert query is used to ask for a conversion between
622 * one format and another.
624 * Free-function: gst_query_unref
626 * Returns: (transfer full): a #GstQuery
629 gst_query_new_convert (GstFormat src_format, gint64 value,
630 GstFormat dest_format)
633 GstStructure *structure;
635 structure = gst_structure_id_new (GST_QUARK (QUERY_CONVERT),
636 GST_QUARK (SRC_FORMAT), GST_TYPE_FORMAT, src_format,
637 GST_QUARK (SRC_VALUE), G_TYPE_INT64, value,
638 GST_QUARK (DEST_FORMAT), GST_TYPE_FORMAT, dest_format,
639 GST_QUARK (DEST_VALUE), G_TYPE_INT64, G_GINT64_CONSTANT (-1), NULL);
641 query = gst_query_new (GST_QUERY_CONVERT, structure);
647 * gst_query_set_convert:
648 * @query: a #GstQuery
649 * @src_format: the source #GstFormat
650 * @src_value: the source value
651 * @dest_format: the destination #GstFormat
652 * @dest_value: the destination value
654 * Answer a convert query by setting the requested values.
657 gst_query_set_convert (GstQuery * query, GstFormat src_format, gint64 src_value,
658 GstFormat dest_format, gint64 dest_value)
660 g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CONVERT);
662 gst_structure_id_set (query->structure,
663 GST_QUARK (SRC_FORMAT), GST_TYPE_FORMAT, src_format,
664 GST_QUARK (SRC_VALUE), G_TYPE_INT64, src_value,
665 GST_QUARK (DEST_FORMAT), GST_TYPE_FORMAT, dest_format,
666 GST_QUARK (DEST_VALUE), G_TYPE_INT64, dest_value, NULL);
670 * gst_query_parse_convert:
671 * @query: a #GstQuery
672 * @src_format: (out) (allow-none): the storage for the #GstFormat of the
673 * source value, or NULL
674 * @src_value: (out) (allow-none): the storage for the source value, or NULL
675 * @dest_format: (out) (allow-none): the storage for the #GstFormat of the
676 * destination value, or NULL
677 * @dest_value: (out) (allow-none): the storage for the destination value,
680 * Parse a convert query answer. Any of @src_format, @src_value, @dest_format,
681 * and @dest_value may be NULL, in which case that value is omitted.
684 gst_query_parse_convert (GstQuery * query, GstFormat * src_format,
685 gint64 * src_value, GstFormat * dest_format, gint64 * dest_value)
687 GstStructure *structure;
689 g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CONVERT);
691 structure = query->structure;
694 (GstFormat) g_value_get_enum (gst_structure_id_get_value (structure,
695 GST_QUARK (SRC_FORMAT)));
697 *src_value = g_value_get_int64 (gst_structure_id_get_value (structure,
698 GST_QUARK (SRC_VALUE)));
701 (GstFormat) g_value_get_enum (gst_structure_id_get_value (structure,
702 GST_QUARK (DEST_FORMAT)));
704 *dest_value = g_value_get_int64 (gst_structure_id_get_value (structure,
705 GST_QUARK (DEST_VALUE)));
709 * gst_query_new_segment:
710 * @format: the #GstFormat for the new query
712 * Constructs a new segment query object. Use gst_query_unref()
713 * when done with it. A segment query is used to discover information about the
714 * currently configured segment for playback.
716 * Free-function: gst_query_unref
718 * Returns: (transfer full): a new #GstQuery
721 gst_query_new_segment (GstFormat format)
724 GstStructure *structure;
726 structure = gst_structure_id_new (GST_QUARK (QUERY_SEGMENT),
727 GST_QUARK (RATE), G_TYPE_DOUBLE, (gdouble) 0.0,
728 GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
729 GST_QUARK (START_VALUE), G_TYPE_INT64, G_GINT64_CONSTANT (-1),
730 GST_QUARK (STOP_VALUE), G_TYPE_INT64, G_GINT64_CONSTANT (-1), NULL);
732 query = gst_query_new (GST_QUERY_SEGMENT, structure);
738 * gst_query_set_segment:
739 * @query: a #GstQuery
740 * @rate: the rate of the segment
741 * @format: the #GstFormat of the segment values (@start_value and @stop_value)
742 * @start_value: the start value
743 * @stop_value: the stop value
745 * Answer a segment query by setting the requested values. The normal
746 * playback segment of a pipeline is 0 to duration at the default rate of
747 * 1.0. If a seek was performed on the pipeline to play a different
748 * segment, this query will return the range specified in the last seek.
750 * @start_value and @stop_value will respectively contain the configured
751 * playback range start and stop values expressed in @format.
752 * The values are always between 0 and the duration of the media and
753 * @start_value <= @stop_value. @rate will contain the playback rate. For
754 * negative rates, playback will actually happen from @stop_value to
758 gst_query_set_segment (GstQuery * query, gdouble rate, GstFormat format,
759 gint64 start_value, gint64 stop_value)
761 g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SEGMENT);
763 gst_structure_id_set (query->structure,
764 GST_QUARK (RATE), G_TYPE_DOUBLE, rate,
765 GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
766 GST_QUARK (START_VALUE), G_TYPE_INT64, start_value,
767 GST_QUARK (STOP_VALUE), G_TYPE_INT64, stop_value, NULL);
771 * gst_query_parse_segment:
772 * @query: a #GstQuery
773 * @rate: (out) (allow-none): the storage for the rate of the segment, or NULL
774 * @format: (out) (allow-none): the storage for the #GstFormat of the values,
776 * @start_value: (out) (allow-none): the storage for the start value, or NULL
777 * @stop_value: (out) (allow-none): the storage for the stop value, or NULL
779 * Parse a segment query answer. Any of @rate, @format, @start_value, and
780 * @stop_value may be NULL, which will cause this value to be omitted.
782 * See gst_query_set_segment() for an explanation of the function arguments.
785 gst_query_parse_segment (GstQuery * query, gdouble * rate, GstFormat * format,
786 gint64 * start_value, gint64 * stop_value)
788 GstStructure *structure;
790 g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SEGMENT);
792 structure = query->structure;
794 *rate = g_value_get_double (gst_structure_id_get_value (structure,
798 (GstFormat) g_value_get_enum (gst_structure_id_get_value (structure,
799 GST_QUARK (FORMAT)));
801 *start_value = g_value_get_int64 (gst_structure_id_get_value (structure,
802 GST_QUARK (START_VALUE)));
804 *stop_value = g_value_get_int64 (gst_structure_id_get_value (structure,
805 GST_QUARK (STOP_VALUE)));
809 * gst_query_new_application:
810 * @type: the query type
811 * @structure: a structure for the query
813 * Constructs a new custom application query object. Use gst_query_unref()
816 * Free-function: gst_query_unref
818 * Returns: (transfer full): a new #GstQuery
821 gst_query_new_application (GstQueryType type, GstStructure * structure)
823 g_return_val_if_fail (gst_query_type_get_details (type) != NULL, NULL);
824 g_return_val_if_fail (structure != NULL, NULL);
826 return gst_query_new (type, structure);
830 * gst_query_get_structure:
831 * @query: a #GstQuery
833 * Get the structure of a query.
835 * Returns: (transfer none): the #GstStructure of the query. The structure is
836 * still owned by the query and will therefore be freed when the query
840 gst_query_get_structure (GstQuery * query)
842 g_return_val_if_fail (GST_IS_QUERY (query), NULL);
844 return query->structure;
848 * gst_query_new_seeking:
849 * @format: the default #GstFormat for the new query
851 * Constructs a new query object for querying seeking properties of
854 * Free-function: gst_query_unref
856 * Returns: (transfer full): a new #GstQuery
859 gst_query_new_seeking (GstFormat format)
862 GstStructure *structure;
864 structure = gst_structure_id_new (GST_QUARK (QUERY_SEEKING),
865 GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
866 GST_QUARK (SEEKABLE), G_TYPE_BOOLEAN, FALSE,
867 GST_QUARK (SEGMENT_START), G_TYPE_INT64, G_GINT64_CONSTANT (-1),
868 GST_QUARK (SEGMENT_END), G_TYPE_INT64, G_GINT64_CONSTANT (-1), NULL);
870 query = gst_query_new (GST_QUERY_SEEKING, structure);
876 * gst_query_set_seeking:
877 * @query: a #GstQuery
878 * @format: the format to set for the @segment_start and @segment_end values
879 * @seekable: the seekable flag to set
880 * @segment_start: the segment_start to set
881 * @segment_end: the segment_end to set
883 * Set the seeking query result fields in @query.
886 gst_query_set_seeking (GstQuery * query, GstFormat format,
887 gboolean seekable, gint64 segment_start, gint64 segment_end)
889 g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SEEKING);
891 gst_structure_id_set (query->structure,
892 GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
893 GST_QUARK (SEEKABLE), G_TYPE_BOOLEAN, seekable,
894 GST_QUARK (SEGMENT_START), G_TYPE_INT64, segment_start,
895 GST_QUARK (SEGMENT_END), G_TYPE_INT64, segment_end, NULL);
899 * gst_query_parse_seeking:
900 * @query: a GST_QUERY_SEEKING type query #GstQuery
901 * @format: (out) (allow-none): the format to set for the @segment_start
902 * and @segment_end values, or NULL
903 * @seekable: (out) (allow-none): the seekable flag to set, or NULL
904 * @segment_start: (out) (allow-none): the segment_start to set, or NULL
905 * @segment_end: (out) (allow-none): the segment_end to set, or NULL
907 * Parse a seeking query, writing the format into @format, and
908 * other results into the passed parameters, if the respective parameters
912 gst_query_parse_seeking (GstQuery * query, GstFormat * format,
913 gboolean * seekable, gint64 * segment_start, gint64 * segment_end)
915 GstStructure *structure;
917 g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SEEKING);
919 structure = query->structure;
922 (GstFormat) g_value_get_enum (gst_structure_id_get_value (structure,
923 GST_QUARK (FORMAT)));
925 *seekable = g_value_get_boolean (gst_structure_id_get_value (structure,
926 GST_QUARK (SEEKABLE)));
928 *segment_start = g_value_get_int64 (gst_structure_id_get_value (structure,
929 GST_QUARK (SEGMENT_START)));
931 *segment_end = g_value_get_int64 (gst_structure_id_get_value (structure,
932 GST_QUARK (SEGMENT_END)));
936 * gst_query_new_formats:
938 * Constructs a new query object for querying formats of
941 * Free-function: gst_query_unref
943 * Returns: (transfer full): a new #GstQuery
948 gst_query_new_formats (void)
951 GstStructure *structure;
953 structure = gst_structure_id_empty_new (GST_QUARK (QUERY_FORMATS));
954 query = gst_query_new (GST_QUERY_FORMATS, structure);
960 gst_query_list_add_format (GValue * list, GstFormat format)
962 GValue item = { 0, };
964 g_value_init (&item, GST_TYPE_FORMAT);
965 g_value_set_enum (&item, format);
966 gst_value_list_append_value (list, &item);
967 g_value_unset (&item);
971 * gst_query_set_formats:
972 * @query: a #GstQuery
973 * @n_formats: the number of formats to set.
974 * @...: A number of @GstFormats equal to @n_formats.
976 * Set the formats query result fields in @query. The number of formats passed
977 * must be equal to @n_formats.
980 gst_query_set_formats (GstQuery * query, gint n_formats, ...)
983 GValue list = { 0, };
986 g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_FORMATS);
988 g_value_init (&list, GST_TYPE_LIST);
990 va_start (ap, n_formats);
991 for (i = 0; i < n_formats; i++) {
992 gst_query_list_add_format (&list, va_arg (ap, GstFormat));
996 gst_structure_set_value (query->structure, "formats", &list);
998 g_value_unset (&list);
1003 * gst_query_set_formatsv:
1004 * @query: a #GstQuery
1005 * @n_formats: the number of formats to set.
1006 * @formats: (in) (array length=n_formats): an array containing @n_formats
1007 * @GstFormat values.
1009 * Set the formats query result fields in @query. The number of formats passed
1010 * in the @formats array must be equal to @n_formats.
1015 gst_query_set_formatsv (GstQuery * query, gint n_formats,
1016 const GstFormat * formats)
1018 GValue list = { 0, };
1021 g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_FORMATS);
1023 g_value_init (&list, GST_TYPE_LIST);
1024 for (i = 0; i < n_formats; i++) {
1025 gst_query_list_add_format (&list, formats[i]);
1027 gst_structure_set_value (query->structure, "formats", &list);
1029 g_value_unset (&list);
1033 * gst_query_parse_formats_length:
1034 * @query: a #GstQuery
1035 * @n_formats: (out): the number of formats in this query.
1037 * Parse the number of formats in the formats @query.
1042 gst_query_parse_formats_length (GstQuery * query, guint * n_formats)
1044 g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_FORMATS);
1049 list = gst_structure_get_value (query->structure, "formats");
1053 *n_formats = gst_value_list_get_size (list);
1058 * gst_query_parse_formats_nth:
1059 * @query: a #GstQuery
1060 * @nth: (out): the nth format to retrieve.
1061 * @format: (out): a pointer to store the nth format
1063 * Parse the format query and retrieve the @nth format from it into
1064 * @format. If the list contains less elements than @nth, @format will be
1065 * set to GST_FORMAT_UNDEFINED.
1070 gst_query_parse_formats_nth (GstQuery * query, guint nth, GstFormat * format)
1072 g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_FORMATS);
1077 list = gst_structure_get_value (query->structure, "formats");
1079 *format = GST_FORMAT_UNDEFINED;
1081 if (nth < gst_value_list_get_size (list)) {
1083 (GstFormat) g_value_get_enum (gst_value_list_get_value (list, nth));
1085 *format = GST_FORMAT_UNDEFINED;
1091 * gst_query_new_buffering
1092 * @format: the default #GstFormat for the new query
1094 * Constructs a new query object for querying the buffering status of
1097 * Free-function: gst_query_new
1099 * Returns: (transfer full): a new #GstQuery
1104 gst_query_new_buffering (GstFormat format)
1107 GstStructure *structure;
1109 /* by default, we configure the answer as no buffering with a 100% buffering
1111 structure = gst_structure_id_new (GST_QUARK (QUERY_BUFFERING),
1112 GST_QUARK (BUSY), G_TYPE_BOOLEAN, FALSE,
1113 GST_QUARK (BUFFER_PERCENT), G_TYPE_INT, 100,
1114 GST_QUARK (BUFFERING_MODE), GST_TYPE_BUFFERING_MODE, GST_BUFFERING_STREAM,
1115 GST_QUARK (AVG_IN_RATE), G_TYPE_INT, -1,
1116 GST_QUARK (AVG_OUT_RATE), G_TYPE_INT, -1,
1117 GST_QUARK (BUFFERING_LEFT), G_TYPE_INT64, G_GINT64_CONSTANT (0),
1118 GST_QUARK (ESTIMATED_TOTAL), G_TYPE_INT64, G_GINT64_CONSTANT (-1),
1119 GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
1120 GST_QUARK (START_VALUE), G_TYPE_INT64, G_GINT64_CONSTANT (-1),
1121 GST_QUARK (STOP_VALUE), G_TYPE_INT64, G_GINT64_CONSTANT (-1), NULL);
1123 query = gst_query_new (GST_QUERY_BUFFERING, structure);
1129 * gst_query_set_buffering_percent
1130 * @query: A valid #GstQuery of type GST_QUERY_BUFFERING.
1131 * @busy: if buffering is busy
1132 * @percent: a buffering percent
1134 * Set the percentage of buffered data. This is a value between 0 and 100.
1135 * The @busy indicator is %TRUE when the buffering is in progress.
1140 gst_query_set_buffering_percent (GstQuery * query, gboolean busy, gint percent)
1142 g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING);
1143 g_return_if_fail (percent >= 0 && percent <= 100);
1145 gst_structure_id_set (query->structure,
1146 GST_QUARK (BUSY), G_TYPE_BOOLEAN, busy,
1147 GST_QUARK (BUFFER_PERCENT), G_TYPE_INT, percent, NULL);
1151 * gst_query_parse_buffering_percent
1152 * @query: A valid #GstQuery of type GST_QUERY_BUFFERING.
1153 * @busy: (out) (allow-none): if buffering is busy, or NULL
1154 * @percent: (out) (allow-none): a buffering percent, or NULL
1156 * Get the percentage of buffered data. This is a value between 0 and 100.
1157 * The @busy indicator is %TRUE when the buffering is in progress.
1162 gst_query_parse_buffering_percent (GstQuery * query, gboolean * busy,
1165 g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING);
1168 *busy = g_value_get_boolean (gst_structure_id_get_value (query->structure,
1171 *percent = g_value_get_int (gst_structure_id_get_value (query->structure,
1172 GST_QUARK (BUFFER_PERCENT)));
1176 * gst_query_set_buffering_stats:
1177 * @query: A valid #GstQuery of type GST_QUERY_BUFFERING.
1178 * @mode: a buffering mode
1179 * @avg_in: the average input rate
1180 * @avg_out: the average output rate
1181 * @buffering_left: amount of buffering time left
1183 * Configures the buffering stats values in @query.
1188 gst_query_set_buffering_stats (GstQuery * query, GstBufferingMode mode,
1189 gint avg_in, gint avg_out, gint64 buffering_left)
1191 g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING);
1193 gst_structure_id_set (query->structure,
1194 GST_QUARK (BUFFERING_MODE), GST_TYPE_BUFFERING_MODE, mode,
1195 GST_QUARK (AVG_IN_RATE), G_TYPE_INT, avg_in,
1196 GST_QUARK (AVG_OUT_RATE), G_TYPE_INT, avg_out,
1197 GST_QUARK (BUFFERING_LEFT), G_TYPE_INT64, buffering_left, NULL);
1201 * gst_query_parse_buffering_stats:
1202 * @query: A valid #GstQuery of type GST_QUERY_BUFFERING.
1203 * @mode: (out) (allow-none): a buffering mode, or NULL
1204 * @avg_in: (out) (allow-none): the average input rate, or NULL
1205 * @avg_out: (out) (allow-none): the average output rat, or NULLe
1206 * @buffering_left: (out) (allow-none): amount of buffering time left, or NULL
1208 * Extracts the buffering stats values from @query.
1213 gst_query_parse_buffering_stats (GstQuery * query,
1214 GstBufferingMode * mode, gint * avg_in, gint * avg_out,
1215 gint64 * buffering_left)
1217 g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING);
1220 *mode = (GstBufferingMode)
1221 g_value_get_enum (gst_structure_id_get_value (query->structure,
1222 GST_QUARK (BUFFERING_MODE)));
1224 *avg_in = g_value_get_int (gst_structure_id_get_value (query->structure,
1225 GST_QUARK (AVG_IN_RATE)));
1227 *avg_out = g_value_get_int (gst_structure_id_get_value (query->structure,
1228 GST_QUARK (AVG_OUT_RATE)));
1231 g_value_get_int64 (gst_structure_id_get_value (query->structure,
1232 GST_QUARK (BUFFERING_LEFT)));
1237 * gst_query_set_buffering_range:
1238 * @query: a #GstQuery
1239 * @format: the format to set for the @start and @stop values
1240 * @start: the start to set
1241 * @stop: the stop to set
1242 * @estimated_total: estimated total amount of download time
1244 * Set the available query result fields in @query.
1249 gst_query_set_buffering_range (GstQuery * query, GstFormat format,
1250 gint64 start, gint64 stop, gint64 estimated_total)
1252 g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING);
1254 gst_structure_id_set (query->structure,
1255 GST_QUARK (FORMAT), GST_TYPE_FORMAT, format,
1256 GST_QUARK (START_VALUE), G_TYPE_INT64, start,
1257 GST_QUARK (STOP_VALUE), G_TYPE_INT64, stop,
1258 GST_QUARK (ESTIMATED_TOTAL), G_TYPE_INT64, estimated_total, NULL);
1262 * gst_query_parse_buffering_range:
1263 * @query: a GST_QUERY_BUFFERING type query #GstQuery
1264 * @format: (out) (allow-none): the format to set for the @segment_start
1265 * and @segment_end values, or NULL
1266 * @start: (out) (allow-none): the start to set, or NULL
1267 * @stop: (out) (allow-none): the stop to set, or NULL
1268 * @estimated_total: (out) (allow-none): estimated total amount of download
1271 * Parse an available query, writing the format into @format, and
1272 * other results into the passed parameters, if the respective parameters
1278 gst_query_parse_buffering_range (GstQuery * query, GstFormat * format,
1279 gint64 * start, gint64 * stop, gint64 * estimated_total)
1281 GstStructure *structure;
1283 g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING);
1285 structure = query->structure;
1288 (GstFormat) g_value_get_enum (gst_structure_id_get_value (structure,
1289 GST_QUARK (FORMAT)));
1291 *start = g_value_get_int64 (gst_structure_id_get_value (structure,
1292 GST_QUARK (START_VALUE)));
1294 *stop = g_value_get_int64 (gst_structure_id_get_value (structure,
1295 GST_QUARK (STOP_VALUE)));
1296 if (estimated_total)
1298 g_value_get_int64 (gst_structure_id_get_value (structure,
1299 GST_QUARK (ESTIMATED_TOTAL)));
1303 * gst_query_add_buffering_range
1304 * @query: a GST_QUERY_BUFFERING type query #GstQuery
1305 * @start: start position of the range
1306 * @stop: stop position of the range
1308 * Set the buffering-ranges array field in @query. The current last
1309 * start position of the array should be inferior to @start.
1311 * Returns: a #gboolean indicating if the range was added or not.
1316 gst_query_add_buffering_range (GstQuery * query, gint64 start, gint64 stop)
1319 GValue *last_array_value;
1320 const GValue *value;
1321 GValue range_value = { 0 };
1323 g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING, FALSE);
1325 if (G_UNLIKELY (start >= stop))
1329 gst_structure_id_get_value (query->structure,
1330 GST_QUARK (BUFFERING_RANGES));
1332 array = (GValueArray *) g_value_get_boxed (value);
1333 last_array_value = g_value_array_get_nth (array, array->n_values - 1);
1334 if (G_UNLIKELY (start <= gst_value_get_int64_range_min (last_array_value)))
1337 GValue new_array_val = { 0, };
1339 array = g_value_array_new (0);
1341 g_value_init (&new_array_val, G_TYPE_VALUE_ARRAY);
1342 g_value_take_boxed (&new_array_val, array);
1344 /* set the value array only once, so we then modify (append to) the
1345 * existing value array owned by the GstStructure / the field's GValue */
1346 gst_structure_id_take_value (query->structure, GST_QUARK (BUFFERING_RANGES),
1350 g_value_init (&range_value, GST_TYPE_INT64_RANGE);
1351 gst_value_set_int64_range (&range_value, start, stop);
1352 g_value_array_append (array, &range_value);
1353 /* skip the g_value_unset(&range_value) here, we know it's not needed */
1359 * gst_query_get_n_buffering_ranges
1360 * @query: a GST_QUERY_BUFFERING type query #GstQuery
1362 * Retrieve the number of values currently stored in the
1363 * buffered-ranges array of the query's structure.
1365 * Returns: the range array size as a #guint.
1370 gst_query_get_n_buffering_ranges (GstQuery * query)
1373 const GValue *value;
1376 g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING, 0);
1379 gst_structure_id_get_value (query->structure,
1380 GST_QUARK (BUFFERING_RANGES));
1382 array = (GValueArray *) g_value_get_boxed (value);
1383 size = array->n_values;
1390 * gst_query_parse_nth_buffering_range
1391 * @query: a GST_QUERY_BUFFERING type query #GstQuery
1392 * @index: position in the buffered-ranges array to read
1393 * @start: (out) (allow-none): the start position to set, or NULL
1394 * @stop: (out) (allow-none): the stop position to set, or NULL
1396 * Parse an available query and get the start and stop values stored
1397 * at the @index of the buffered ranges array.
1399 * Returns: a #gboolean indicating if the parsing succeeded.
1404 gst_query_parse_nth_buffering_range (GstQuery * query, guint index,
1405 gint64 * start, gint64 * stop)
1407 const GValue *value;
1408 GValueArray *ranges;
1409 GValue *range_value;
1410 gboolean ret = FALSE;
1412 g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_BUFFERING, ret);
1415 gst_structure_id_get_value (query->structure,
1416 GST_QUARK (BUFFERING_RANGES));
1417 ranges = (GValueArray *) g_value_get_boxed (value);
1418 range_value = g_value_array_get_nth (ranges, index);
1421 *start = gst_value_get_int64_range_min (range_value);
1423 *stop = gst_value_get_int64_range_max (range_value);
1432 * gst_query_new_uri:
1434 * Constructs a new query URI query object. Use gst_query_unref()
1435 * when done with it. An URI query is used to query the current URI
1436 * that is used by the source or sink.
1438 * Free-function: gst_query_unref
1440 * Returns: (transfer full): a new #GstQuery
1445 gst_query_new_uri (void)
1448 GstStructure *structure;
1450 structure = gst_structure_id_new (GST_QUARK (QUERY_URI),
1451 GST_QUARK (URI), G_TYPE_STRING, NULL, NULL);
1453 query = gst_query_new (GST_QUERY_URI, structure);
1459 * gst_query_set_uri:
1460 * @query: a #GstQuery with query type GST_QUERY_URI
1461 * @uri: the URI to set
1463 * Answer a URI query by setting the requested URI.
1468 gst_query_set_uri (GstQuery * query, const gchar * uri)
1470 g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_URI);
1471 g_return_if_fail (gst_uri_is_valid (uri));
1473 gst_structure_id_set (query->structure, GST_QUARK (URI), G_TYPE_STRING, uri,
1478 * gst_query_parse_uri:
1479 * @query: a #GstQuery
1480 * @uri: (out callee-allocates) (allow-none): the storage for the current URI
1483 * Parse an URI query, writing the URI into @uri as a newly
1484 * allocated string, if the respective parameters are non-NULL.
1485 * Free the string with g_free() after usage.
1490 gst_query_parse_uri (GstQuery * query, gchar ** uri)
1492 g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_URI);
1495 *uri = g_value_dup_string (gst_structure_id_get_value (query->structure,