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 core.
29 * Query types can be used to perform queries on pads and elements.
31 * Query answer can be parsed using gst_query_parse_xxx() helpers.
35 #include "gst_private.h"
37 #include "gstenumtypes.h"
39 GST_DEBUG_CATEGORY_STATIC (gst_query_debug);
40 #define GST_CAT_DEFAULT gst_query_debug
42 static void gst_query_init (GTypeInstance * instance, gpointer g_class);
43 static void gst_query_class_init (gpointer g_class, gpointer class_data);
44 static void gst_query_finalize (GstQuery * query);
45 static GstQuery *_gst_query_copy (GstQuery * query);
48 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
49 static GList *_gst_queries = NULL;
50 static GHashTable *_nick_to_query = NULL;
51 static GHashTable *_query_type_to_nick = NULL;
52 static guint32 _n_values = 1; /* we start from 1 because 0 reserved for NONE */
54 static GstQueryTypeDefinition standard_definitions[] = {
55 {GST_QUERY_POSITION, "position", "Current position and total duration"},
56 {GST_QUERY_LATENCY, "latency", "Latency"},
57 {GST_QUERY_JITTER, "jitter", "Jitter"},
58 {GST_QUERY_RATE, "rate", "Configured rate 1000000 = 1"},
59 {GST_QUERY_SEEKING, "seeking", "Seeking capabilities and parameters"},
60 {GST_QUERY_SEGMENT, "segment", "currently configured segment"},
61 {GST_QUERY_CONVERT, "convert", "Converting between formats"},
62 {GST_QUERY_FORMATS, "formats", "Supported formats for conversion"},
67 _gst_query_initialize (void)
69 GstQueryTypeDefinition *standards = standard_definitions;
71 GST_CAT_INFO (GST_CAT_GST_INIT, "init queries");
73 GST_DEBUG_CATEGORY_INIT (gst_query_debug, "query", 0, "query system");
75 g_static_mutex_lock (&mutex);
76 if (_nick_to_query == NULL) {
77 _nick_to_query = g_hash_table_new (g_str_hash, g_str_equal);
78 _query_type_to_nick = g_hash_table_new (NULL, NULL);
81 while (standards->nick) {
82 g_hash_table_insert (_nick_to_query, standards->nick, standards);
83 g_hash_table_insert (_query_type_to_nick,
84 GINT_TO_POINTER (standards->value), standards);
86 _gst_queries = g_list_append (_gst_queries, standards);
90 g_static_mutex_unlock (&mutex);
92 gst_query_get_type ();
96 gst_query_get_type (void)
98 static GType _gst_query_type;
100 if (G_UNLIKELY (_gst_query_type == 0)) {
101 static const GTypeInfo query_info = {
102 sizeof (GstQueryClass),
105 gst_query_class_init,
114 _gst_query_type = g_type_register_static (GST_TYPE_MINI_OBJECT,
115 "GstQuery", &query_info, 0);
117 return _gst_query_type;
121 gst_query_class_init (gpointer g_class, gpointer class_data)
123 GstQueryClass *query_class = GST_QUERY_CLASS (g_class);
125 query_class->mini_object_class.copy =
126 (GstMiniObjectCopyFunction) _gst_query_copy;
127 query_class->mini_object_class.finalize =
128 (GstMiniObjectFinalizeFunction) gst_query_finalize;
133 gst_query_finalize (GstQuery * query)
135 g_return_if_fail (query != NULL);
137 if (query->structure) {
138 gst_structure_set_parent_refcount (query->structure, NULL);
139 gst_structure_free (query->structure);
144 gst_query_init (GTypeInstance * instance, gpointer g_class)
150 _gst_query_copy (GstQuery * query)
154 copy = (GstQuery *) gst_mini_object_new (GST_TYPE_QUERY);
156 copy->type = query->type;
158 if (query->structure) {
159 copy->structure = gst_structure_copy (query->structure);
160 gst_structure_set_parent_refcount (copy->structure,
161 &query->mini_object.refcount);
170 * gst_query_type_register:
171 * @nick: The nick of the new query
172 * @description: The description of the new query
174 * Create a new GstQueryType based on the nick or return an
175 * allrady registered query with that nick
177 * Returns: A new GstQueryType or an already registered query
178 * with the same nick.
181 gst_query_type_register (const gchar * nick, const gchar * description)
183 GstQueryTypeDefinition *query;
186 g_return_val_if_fail (nick != NULL, 0);
187 g_return_val_if_fail (description != NULL, 0);
189 lookup = gst_query_type_get_by_nick (nick);
190 if (lookup != GST_QUERY_NONE)
193 query = g_new0 (GstQueryTypeDefinition, 1);
194 query->value = _n_values;
195 query->nick = g_strdup (nick);
196 query->description = g_strdup (description);
198 g_static_mutex_lock (&mutex);
199 g_hash_table_insert (_nick_to_query, query->nick, query);
200 g_hash_table_insert (_query_type_to_nick, GINT_TO_POINTER (query->value),
202 _gst_queries = g_list_append (_gst_queries, query);
204 g_static_mutex_unlock (&mutex);
210 * gst_query_type_get_by_nick:
211 * @nick: The nick of the query
213 * Return the query registered with the given nick.
215 * Returns: The query with @nick or GST_QUERY_NONE
216 * if the query was not registered.
219 gst_query_type_get_by_nick (const gchar * nick)
221 GstQueryTypeDefinition *query;
223 g_return_val_if_fail (nick != NULL, 0);
225 g_static_mutex_lock (&mutex);
226 query = g_hash_table_lookup (_nick_to_query, nick);
227 g_static_mutex_unlock (&mutex);
232 return GST_QUERY_NONE;
236 * gst_query_types_contains:
237 * @types: The query array to search
238 * @type: the querytype to find
240 * See if the given query is inside the query array.
242 * Returns: TRUE if the query is found inside the array
245 gst_query_types_contains (const GstQueryType * types, GstQueryType type)
261 * gst_query_type_get_details:
262 * @type: The query to get details of
264 * Get details about the given query.
266 * Returns: The #GstQueryTypeDefinition for @query or NULL on failure.
268 const GstQueryTypeDefinition *
269 gst_query_type_get_details (GstQueryType type)
271 const GstQueryTypeDefinition *result;
273 g_static_mutex_lock (&mutex);
274 result = g_hash_table_lookup (_query_type_to_nick, GINT_TO_POINTER (type));
275 g_static_mutex_unlock (&mutex);
281 * gst_query_type_iterate_definitions:
283 * Get an Iterator of all the registered query types. The querytype
284 * definition is read only.
286 * Returns: A #GstIterator of #GstQueryTypeDefinition.
289 gst_query_type_iterate_definitions (void)
293 g_static_mutex_lock (&mutex);
294 result = gst_iterator_new_list (g_static_mutex_get_mutex (&mutex),
295 &_n_values, &_gst_queries, NULL, NULL, NULL);
296 g_static_mutex_unlock (&mutex);
302 gst_query_new (GstQueryType type, GstStructure * structure)
306 query = (GstQuery *) gst_mini_object_new (GST_TYPE_QUERY);
308 GST_DEBUG ("creating new query %p %d", query, type);
313 query->structure = structure;
314 gst_structure_set_parent_refcount (query->structure,
315 &query->mini_object.refcount);
317 query->structure = NULL;
324 * gst_query_new_position:
325 * @format: the default #GstFormat for the new query
327 * Constructs a new query stream position query object. Use gst_query_unref()
330 * Returns: A new #GstQuery
333 gst_query_new_position (GstFormat format)
336 GstStructure *structure;
338 structure = gst_structure_new ("GstQueryPosition",
339 "format", GST_TYPE_FORMAT, format,
340 "cur", G_TYPE_INT64, (gint64) - 1,
341 "end", G_TYPE_INT64, (gint64) - 1, NULL);
342 query = gst_query_new (GST_QUERY_POSITION, structure);
348 * gst_query_set_position:
349 * @query: the query to fill in
350 * @format: the requested #GstFormat
351 * @cur: the current position
352 * @end: the end position
354 * Answer a position query by setting the requested values.
357 gst_query_set_position (GstQuery * query, GstFormat format,
358 gint64 cur, gint64 end)
360 GstStructure *structure;
362 g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_POSITION);
364 structure = gst_query_get_structure (query);
365 gst_structure_set (structure,
366 "format", GST_TYPE_FORMAT, format,
367 "cur", G_TYPE_INT64, cur, "end", G_TYPE_INT64, end, NULL);
371 * gst_query_parse_position:
372 * @query: the query to parse
373 * @format: the storage for the #GstFormat of the position values
374 * @cur: the storage for the current position
375 * @end: the storage for the end position
377 * Parse a position query answer.
380 gst_query_parse_position (GstQuery * query, GstFormat * format,
381 gint64 * cur, gint64 * end)
383 GstStructure *structure;
385 g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_POSITION);
387 structure = gst_query_get_structure (query);
389 *format = g_value_get_enum (gst_structure_get_value (structure, "format"));
391 *cur = g_value_get_int64 (gst_structure_get_value (structure, "cur"));
393 *end = g_value_get_int64 (gst_structure_get_value (structure, "end"));
397 * gst_query_new_convert:
398 * @src_fmt: the source #GstFormat for the new query
399 * @value: the value to convert
400 * @dest_fmt: the target #GstFormat
402 * Constructs a new query convert object. Use gst_query_unref()
405 * Returns: A new #GstQuery
408 gst_query_new_convert (GstFormat src_fmt, gint64 value, GstFormat dest_fmt)
411 GstStructure *structure;
413 g_return_val_if_fail (value >= 0, NULL);
415 structure = gst_structure_new ("GstQueryConvert",
416 "src_format", GST_TYPE_FORMAT, src_fmt,
417 "src_value", G_TYPE_INT64, value,
418 "dest_format", GST_TYPE_FORMAT, dest_fmt,
419 "dest_value", G_TYPE_INT64, (gint64) - 1, NULL);
420 query = gst_query_new (GST_QUERY_CONVERT, structure);
426 * gst_query_set_convert:
427 * @query: the query to fill in
428 * @src_format: the source #GstFormat
429 * @src_value: the source value
430 * @dest_format: the destination #GstFormat
431 * @dest_value: the destination value
433 * Answer a convert query by setting the requested values.
436 gst_query_set_convert (GstQuery * query, GstFormat src_format, gint64 src_value,
437 GstFormat dest_format, gint64 dest_value)
439 GstStructure *structure;
441 g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CONVERT);
443 structure = gst_query_get_structure (query);
444 gst_structure_set (structure,
445 "src_format", GST_TYPE_FORMAT, src_format,
446 "src_value", G_TYPE_INT64, src_value,
447 "dest_format", GST_TYPE_FORMAT, dest_format,
448 "dest_value", G_TYPE_INT64, dest_value, NULL);
452 * gst_query_parse_convert:
453 * @query: the query to parse
454 * @src_format: the storage for the #GstFormat of the source value
455 * @src_value: the storage for the source value
456 * @dest_format: the storage for the #GstFormat of the destination value
457 * @dest_value: the storage for the destination value
459 * Parse a convert query answer.
462 gst_query_parse_convert (GstQuery * query, GstFormat * src_format,
463 gint64 * src_value, GstFormat * dest_format, gint64 * dest_value)
465 GstStructure *structure;
467 g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CONVERT);
469 structure = gst_query_get_structure (query);
472 g_value_get_enum (gst_structure_get_value (structure, "src_format"));
475 g_value_get_int64 (gst_structure_get_value (structure, "src_value"));
478 g_value_get_enum (gst_structure_get_value (structure, "dest_format"));
481 g_value_get_int64 (gst_structure_get_value (structure, "dest_value"));
485 * gst_query_new_segment:
486 * @format: the #GstFormat for the new query
488 * Constructs a new query segment object. Use gst_query_unref()
491 * Returns: A new #GstQuery
494 gst_query_new_segment (GstFormat format)
497 GstStructure *structure;
499 structure = gst_structure_new ("GstQuerySegment",
500 "format", GST_TYPE_FORMAT, format, NULL);
501 query = gst_query_new (GST_QUERY_SEGMENT, structure);
507 * gst_query_set_segment:
508 * @query: the query to fill in
509 * @rate: the rate of the segment
510 * @format: the #GstFormat of the segment values
511 * @start_value: the start value
512 * @stop_value: the stop value
513 * @base: the base value
515 * Answer a segment query by setting the requested values.
518 gst_query_set_segment (GstQuery * query, gdouble rate, GstFormat format,
519 gint64 start_value, gint64 stop_value, gint64 base)
521 GstStructure *structure;
523 g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SEGMENT);
525 structure = gst_query_get_structure (query);
526 gst_structure_set (structure,
527 "rate", G_TYPE_DOUBLE, rate,
528 "format", GST_TYPE_FORMAT, format,
529 "start_value", G_TYPE_INT64, start_value,
530 "stop_value", G_TYPE_INT64, stop_value, "base", G_TYPE_INT64, base, NULL);
534 * gst_query_parse_segment:
535 * @query: the query to parse
536 * @rate: the storage for the rate of the segment
537 * @format: the storage for the #GstFormat of the values
538 * @start_value: the storage for the start value
539 * @stop_value: the storage for the stop value
540 * @base: the storage for the base value
542 * Parse a segment query answer.
545 gst_query_parse_segment (GstQuery * query, gdouble * rate, GstFormat * format,
546 gint64 * start_value, gint64 * stop_value, gint64 * base)
548 GstStructure *structure;
550 g_return_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_SEGMENT);
552 structure = gst_query_get_structure (query);
554 *rate = g_value_get_double (gst_structure_get_value (structure, "rate"));
556 *format = g_value_get_enum (gst_structure_get_value (structure, "format"));
559 g_value_get_int64 (gst_structure_get_value (structure, "start_value"));
562 g_value_get_int64 (gst_structure_get_value (structure, "stop_value"));
564 *base = g_value_get_int64 (gst_structure_get_value (structure, "base"));
568 * gst_query_new_application:
569 * @type: the query type
570 * @structure: a structure for the query
572 * Constructs a new custom application query object. Use gst_query_unref()
575 * Returns: A new #GstQuery
578 gst_query_new_application (GstQueryType type, GstStructure * structure)
580 g_return_val_if_fail (gst_query_type_get_details (type) != NULL, NULL);
581 g_return_val_if_fail (structure != NULL, NULL);
583 return gst_query_new (type, structure);
587 * gst_query_get_structure:
588 * @query: the query to parse
590 * Get the structure of a query.
592 * Returns: The #GstStructure of the query. The structure is still owned
593 * by the query and will therefore be freed when the query is unreffed.
596 gst_query_get_structure (GstQuery * query)
598 g_return_val_if_fail (GST_IS_QUERY (query), NULL);
600 return query->structure;