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
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.
25 * @short_description: Dynamically register new query types and parse results
26 * @see_also: #GstPad, #GstElement
28 * GstQuery functions are used to register a new query types to the gstreamer
30 * Query types can be used to perform queries on pads and elements.
32 * Query answer can be parsed using gst_query_parse_xxx() helpers.
36 #include "gst_private.h"
39 #include "gstenumtypes.h"
41 GST_DEBUG_CATEGORY_STATIC (gst_query_debug);
42 #define GST_CAT_DEFAULT gst_query_debug
44 static void gst_query_init (GTypeInstance * instance, gpointer g_class);
45 static void gst_query_class_init (gpointer g_class, gpointer class_data);
46 static void gst_query_finalize (GstQuery * query);
47 static GstQuery *_gst_query_copy (GstQuery * query);
50 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
51 static GList *_gst_queries = NULL;
52 static GHashTable *_nick_to_query = NULL;
53 static GHashTable *_query_type_to_nick = NULL;
54 static guint32 _n_values = 1; /* we start from 1 because 0 reserved for NONE */
56 static GstQueryTypeDefinition standard_definitions[] = {
57 {GST_QUERY_POSITION, "position", "Current position and total duration"},
58 {GST_QUERY_LATENCY, "latency", "Latency"},
59 {GST_QUERY_JITTER, "jitter", "Jitter"},
60 {GST_QUERY_RATE, "rate", "Configured rate 1000000 = 1"},
61 {GST_QUERY_SEEKING, "seeking", "Seeking capabilities and parameters"},
62 {GST_QUERY_SEGMENT, "segment", "currently configured segment"},
63 {GST_QUERY_CONVERT, "convert", "Converting between formats"},
64 {GST_QUERY_FORMATS, "formats", "Supported formats for conversion"},
69 _gst_query_initialize (void)
71 GstQueryTypeDefinition *standards = standard_definitions;
73 GST_CAT_INFO (GST_CAT_GST_INIT, "init queries");
75 GST_DEBUG_CATEGORY_INIT (gst_query_debug, "query", 0, "query system");
77 g_static_mutex_lock (&mutex);
78 if (_nick_to_query == NULL) {
79 _nick_to_query = g_hash_table_new (g_str_hash, g_str_equal);
80 _query_type_to_nick = g_hash_table_new (NULL, NULL);
83 while (standards->nick) {
84 g_hash_table_insert (_nick_to_query, standards->nick, standards);
85 g_hash_table_insert (_query_type_to_nick,
86 GINT_TO_POINTER (standards->value), standards);
88 _gst_queries = g_list_append (_gst_queries, standards);
92 g_static_mutex_unlock (&mutex);
94 gst_query_get_type ();
98 gst_query_get_type (void)
100 static GType _gst_query_type;
102 if (G_UNLIKELY (_gst_query_type == 0)) {
103 static const GTypeInfo query_info = {
104 sizeof (GstQueryClass),
107 gst_query_class_init,
116 _gst_query_type = g_type_register_static (GST_TYPE_MINI_OBJECT,
117 "GstQuery", &query_info, 0);
119 return _gst_query_type;
123 gst_query_class_init (gpointer g_class, gpointer class_data)
125 GstQueryClass *query_class = GST_QUERY_CLASS (g_class);
127 query_class->mini_object_class.copy =
128 (GstMiniObjectCopyFunction) _gst_query_copy;
129 query_class->mini_object_class.finalize =
130 (GstMiniObjectFinalizeFunction) gst_query_finalize;
135 gst_query_finalize (GstQuery * query)
137 g_return_if_fail (query != NULL);
139 if (query->structure) {
140 gst_structure_set_parent_refcount (query->structure, NULL);
141 gst_structure_free (query->structure);
146 gst_query_init (GTypeInstance * instance, gpointer g_class)
152 _gst_query_copy (GstQuery * query)
156 copy = (GstQuery *) gst_mini_object_new (GST_TYPE_QUERY);
158 copy->type = query->type;
160 if (query->structure) {
161 copy->structure = gst_structure_copy (query->structure);
162 gst_structure_set_parent_refcount (copy->structure,
163 &query->mini_object.refcount);
172 * gst_query_type_register:
173 * @nick: The nick of the new query
174 * @description: The description of the new query
176 * Create a new GstQueryType based on the nick or return an
177 * allrady registered query with that nick
179 * Returns: A new GstQueryType or an already registered query
180 * with the same nick.
183 gst_query_type_register (const gchar * nick, const gchar * description)
185 GstQueryTypeDefinition *query;
188 g_return_val_if_fail (nick != NULL, 0);
189 g_return_val_if_fail (description != NULL, 0);
191 lookup = gst_query_type_get_by_nick (nick);
192 if (lookup != GST_QUERY_NONE)
195 query = g_new0 (GstQueryTypeDefinition, 1);
196 query->value = _n_values;
197 query->nick = g_strdup (nick);
198 query->description = g_strdup (description);
200 g_static_mutex_lock (&mutex);
201 g_hash_table_insert (_nick_to_query, query->nick, query);
202 g_hash_table_insert (_query_type_to_nick, GINT_TO_POINTER (query->value),
204 _gst_queries = g_list_append (_gst_queries, query);
206 g_static_mutex_unlock (&mutex);
212 * gst_query_type_get_by_nick:
213 * @nick: The nick of the query
215 * Return the query registered with the given nick.
217 * Returns: The query with @nick or GST_QUERY_NONE
218 * if the query was not registered.
221 gst_query_type_get_by_nick (const gchar * nick)
223 GstQueryTypeDefinition *query;
225 g_return_val_if_fail (nick != NULL, 0);
227 g_static_mutex_lock (&mutex);
228 query = g_hash_table_lookup (_nick_to_query, nick);
229 g_static_mutex_unlock (&mutex);
234 return GST_QUERY_NONE;
238 * gst_query_types_contains:
239 * @types: The query array to search
240 * @type: the querytype to find
242 * See if the given query is inside the query array.
244 * Returns: TRUE if the query is found inside the array
247 gst_query_types_contains (const GstQueryType * types, GstQueryType type)
263 * gst_query_type_get_details:
264 * @type: The query to get details of
266 * Get details about the given query.
268 * Returns: The #GstQueryTypeDefinition for @query or NULL on failure.
270 const GstQueryTypeDefinition *
271 gst_query_type_get_details (GstQueryType type)
273 const GstQueryTypeDefinition *result;
275 g_static_mutex_lock (&mutex);
276 result = g_hash_table_lookup (_query_type_to_nick, GINT_TO_POINTER (type));
277 g_static_mutex_unlock (&mutex);
283 * gst_query_type_iterate_definitions:
285 * Get an Iterator of all the registered query types. The querytype
286 * definition is read only.
288 * Returns: A #GstIterator of #GstQueryTypeDefinition.
291 gst_query_type_iterate_definitions (void)
295 g_static_mutex_lock (&mutex);
296 /* FIXME: register a boxed type for GstQueryTypeDefinition */
297 result = gst_iterator_new_list (G_TYPE_POINTER,
298 g_static_mutex_get_mutex (&mutex), &_n_values, &_gst_queries,
300 g_static_mutex_unlock (&mutex);
306 gst_query_new (GstQueryType type, GstStructure * structure)
310 query = (GstQuery *) gst_mini_object_new (GST_TYPE_QUERY);
312 GST_DEBUG ("creating new query %p %d", query, type);
317 query->structure = structure;
318 gst_structure_set_parent_refcount (query->structure,
319 &query->mini_object.refcount);
321 query->structure = NULL;
328 * gst_query_new_position:
329 * @format: the default #GstFormat for the new query
331 * Constructs a new query stream position query object. Use gst_query_unref()
334 * Returns: A new #GstQuery
337 gst_query_new_position (GstFormat format)
340 GstStructure *structure;
342 structure = gst_structure_new ("GstQueryPosition",
343 "format", GST_TYPE_FORMAT, format,
344 "cur", G_TYPE_INT64, (gint64) - 1,
345 "end", G_TYPE_INT64, (gint64) - 1, NULL);
346 query = gst_query_new (GST_QUERY_POSITION, structure);
352 * gst_query_set_position:
353 * @query: the query to fill in
354 * @format: the requested #GstFormat
355 * @cur: the current position
356 * @end: the end position
358 * Answer a position query by setting the requested values.
361 gst_query_set_position (GstQuery * query, GstFormat format,
362 gint64 cur, gint64 end)
364 GstStructure *structure;
366 g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_POSITION);
368 structure = gst_query_get_structure (query);
369 gst_structure_set (structure,
370 "format", GST_TYPE_FORMAT, format,
371 "cur", G_TYPE_INT64, cur, "end", G_TYPE_INT64, end, NULL);
375 * gst_query_parse_position:
376 * @query: the query to parse
377 * @format: the storage for the #GstFormat of the position values
378 * @cur: the storage for the current position
379 * @end: the storage for the end position
381 * Parse a position query answer.
384 gst_query_parse_position (GstQuery * query, GstFormat * format,
385 gint64 * cur, gint64 * end)
387 GstStructure *structure;
389 g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_POSITION);
391 structure = gst_query_get_structure (query);
393 *format = g_value_get_enum (gst_structure_get_value (structure, "format"));
395 *cur = g_value_get_int64 (gst_structure_get_value (structure, "cur"));
397 *end = g_value_get_int64 (gst_structure_get_value (structure, "end"));
401 * gst_query_new_convert:
402 * @src_fmt: the source #GstFormat for the new query
403 * @value: the value to convert
404 * @dest_fmt: the target #GstFormat
406 * Constructs a new query convert object. Use gst_query_unref()
409 * Returns: A new #GstQuery
412 gst_query_new_convert (GstFormat src_fmt, gint64 value, GstFormat dest_fmt)
415 GstStructure *structure;
417 g_return_val_if_fail (value >= 0, NULL);
419 structure = gst_structure_new ("GstQueryConvert",
420 "src_format", GST_TYPE_FORMAT, src_fmt,
421 "src_value", G_TYPE_INT64, value,
422 "dest_format", GST_TYPE_FORMAT, dest_fmt,
423 "dest_value", G_TYPE_INT64, (gint64) - 1, NULL);
424 query = gst_query_new (GST_QUERY_CONVERT, structure);
430 * gst_query_set_convert:
431 * @query: the query to fill in
432 * @src_format: the source #GstFormat
433 * @src_value: the source value
434 * @dest_format: the destination #GstFormat
435 * @dest_value: the destination value
437 * Answer a convert query by setting the requested values.
440 gst_query_set_convert (GstQuery * query, GstFormat src_format, gint64 src_value,
441 GstFormat dest_format, gint64 dest_value)
443 GstStructure *structure;
445 g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CONVERT);
447 structure = gst_query_get_structure (query);
448 gst_structure_set (structure,
449 "src_format", GST_TYPE_FORMAT, src_format,
450 "src_value", G_TYPE_INT64, src_value,
451 "dest_format", GST_TYPE_FORMAT, dest_format,
452 "dest_value", G_TYPE_INT64, dest_value, NULL);
456 * gst_query_parse_convert:
457 * @query: the query to parse
458 * @src_format: the storage for the #GstFormat of the source value
459 * @src_value: the storage for the source value
460 * @dest_format: the storage for the #GstFormat of the destination value
461 * @dest_value: the storage for the destination value
463 * Parse a convert query answer.
466 gst_query_parse_convert (GstQuery * query, GstFormat * src_format,
467 gint64 * src_value, GstFormat * dest_format, gint64 * dest_value)
469 GstStructure *structure;
471 g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CONVERT);
473 structure = gst_query_get_structure (query);
476 g_value_get_enum (gst_structure_get_value (structure, "src_format"));
479 g_value_get_int64 (gst_structure_get_value (structure, "src_value"));
482 g_value_get_enum (gst_structure_get_value (structure, "dest_format"));
485 g_value_get_int64 (gst_structure_get_value (structure, "dest_value"));
489 * gst_query_new_segment:
490 * @format: the #GstFormat for the new query
492 * Constructs a new query segment object. Use gst_query_unref()
495 * Returns: A new #GstQuery
498 gst_query_new_segment (GstFormat format)
501 GstStructure *structure;
503 structure = gst_structure_new ("GstQuerySegment",
504 "format", GST_TYPE_FORMAT, format, NULL);
505 query = gst_query_new (GST_QUERY_SEGMENT, structure);
511 * gst_query_set_segment:
512 * @query: the query to fill in
513 * @rate: the rate of the segment
514 * @format: the #GstFormat of the segment values
515 * @start_value: the start value
516 * @stop_value: the stop value
517 * @base: the base value
519 * Answer a segment query by setting the requested values.
522 gst_query_set_segment (GstQuery * query, gdouble rate, GstFormat format,
523 gint64 start_value, gint64 stop_value, gint64 base)
525 GstStructure *structure;
527 g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SEGMENT);
529 structure = gst_query_get_structure (query);
530 gst_structure_set (structure,
531 "rate", G_TYPE_DOUBLE, rate,
532 "format", GST_TYPE_FORMAT, format,
533 "start_value", G_TYPE_INT64, start_value,
534 "stop_value", G_TYPE_INT64, stop_value, "base", G_TYPE_INT64, base, NULL);
538 * gst_query_parse_segment:
539 * @query: the query to parse
540 * @rate: the storage for the rate of the segment
541 * @format: the storage for the #GstFormat of the values
542 * @start_value: the storage for the start value
543 * @stop_value: the storage for the stop value
544 * @base: the storage for the base value
546 * Parse a segment query answer.
549 gst_query_parse_segment (GstQuery * query, gdouble * rate, GstFormat * format,
550 gint64 * start_value, gint64 * stop_value, gint64 * base)
552 GstStructure *structure;
554 g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SEGMENT);
556 structure = gst_query_get_structure (query);
558 *rate = g_value_get_double (gst_structure_get_value (structure, "rate"));
560 *format = g_value_get_enum (gst_structure_get_value (structure, "format"));
563 g_value_get_int64 (gst_structure_get_value (structure, "start_value"));
566 g_value_get_int64 (gst_structure_get_value (structure, "stop_value"));
568 *base = g_value_get_int64 (gst_structure_get_value (structure, "base"));
572 * gst_query_new_application:
573 * @type: the query type
574 * @structure: a structure for the query
576 * Constructs a new custom application query object. Use gst_query_unref()
579 * Returns: A new #GstQuery
582 gst_query_new_application (GstQueryType type, GstStructure * structure)
584 g_return_val_if_fail (gst_query_type_get_details (type) != NULL, NULL);
585 g_return_val_if_fail (structure != NULL, NULL);
587 return gst_query_new (type, structure);
591 * gst_query_get_structure:
592 * @query: the query to parse
594 * Get the structure of a query.
596 * Returns: The #GstStructure of the query. The structure is still owned
597 * by the query and will therefore be freed when the query is unreffed.
600 gst_query_get_structure (GstQuery * query)
602 g_return_val_if_fail (GST_IS_QUERY (query), NULL);
604 return query->structure;
608 gst_query_set_seeking (GstQuery * query, GstFormat format,
609 gboolean seekable, gint64 segment_start, gint64 segment_end)
611 GstStructure *structure;
613 g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SEEKING);
615 structure = gst_query_get_structure (query);
616 gst_structure_set (structure,
617 "format", GST_TYPE_FORMAT, format,
618 "seekable", G_TYPE_BOOLEAN, seekable,
619 "segment-start", G_TYPE_INT64, segment_start,
620 "segment-end", G_TYPE_INT64, segment_end, NULL);
624 gst_query_set_formats (GstQuery * query, gint n_formats, ...)
627 GValue list = { 0, };
628 GValue item = { 0, };
629 GstStructure *structure;
632 g_value_init (&list, GST_TYPE_LIST);
634 va_start (ap, n_formats);
636 for (i = 0; i < n_formats; i++) {
637 g_value_init (&item, GST_TYPE_FORMAT);
638 g_value_set_enum (&item, va_arg (ap, GstFormat));
639 gst_value_list_append_value (&list, &item);
640 g_value_unset (&item);
645 structure = gst_query_get_structure (query);
646 gst_structure_set_value (structure, "formats", &list);