2 * Copyright (C) 2011 Wim Taymans <wim.taymans@gmail.com>
4 * gstmeta.c: metadata operations
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
25 * @short_description: Buffer metadata
27 * The #GstMeta structure should be included as the first member of a #GstBuffer
28 * metadata structure. The structure defines the API of the metadata and should
29 * be accessible to all elements using the metadata.
31 * A metadata API is registered with gst_meta_api_type_register() which takes a
32 * name for the metadata API and some tags associated with the metadata.
33 * With gst_meta_api_type_has_tag() one can check if a certain metadata API
34 * contains a given tag.
36 * Multiple implementations of a metadata API can be registered.
37 * To implement a metadata API, gst_meta_register() should be used. This
38 * function takes all parameters needed to create, free and transform metadata
39 * along with the size of the metadata. The function returns a #GstMetaInfo
40 * structure that contains the information for the implementation of the API.
42 * A specific implementation can be retrieved by name with gst_meta_get_info().
44 * See #GstBuffer for how the metadata can be added, retrieved and removed from
47 #include "gst_private.h"
49 #include "gstbuffer.h"
54 static GHashTable *metainfo = NULL;
57 GQuark _gst_meta_transform_copy;
58 GQuark _gst_meta_tag_memory;
61 _priv_gst_meta_initialize (void)
63 g_rw_lock_init (&lock);
64 metainfo = g_hash_table_new (g_str_hash, g_str_equal);
66 _gst_meta_transform_copy = g_quark_from_static_string ("gst-copy");
67 _gst_meta_tag_memory = g_quark_from_static_string ("memory");
71 * gst_meta_api_type_register:
72 * @api: an API to register
73 * @tags: tags for @api
75 * Register and return a GType for the @api and associate it with
78 * Returns: a unique GType for @api.
81 gst_meta_api_type_register (const gchar * api, const gchar ** tags)
85 g_return_val_if_fail (api != NULL, 0);
86 g_return_val_if_fail (tags != NULL, 0);
88 GST_CAT_DEBUG (GST_CAT_META, "register API \"%s\"", api);
89 type = g_pointer_type_register_static (api);
94 for (i = 0; tags[i]; i++) {
95 GST_CAT_DEBUG (GST_CAT_META, " adding tag \"%s\"", tags[i]);
96 g_type_set_qdata (type, g_quark_from_string (tags[i]),
97 GINT_TO_POINTER (TRUE));
101 g_type_set_qdata (type, g_quark_from_string ("tags"),
102 g_strdupv ((gchar **) tags));
108 * gst_meta_api_type_has_tag:
110 * @tag: the tag to check
112 * Check if @api was registered with @tag.
114 * Returns: %TRUE if @api was registered with @tag.
117 gst_meta_api_type_has_tag (GType api, GQuark tag)
119 g_return_val_if_fail (api != 0, FALSE);
120 g_return_val_if_fail (tag != 0, FALSE);
122 return g_type_get_qdata (api, tag) != NULL;
126 * gst_meta_api_type_get_tags:
129 * Returns: (transfer none) (array zero-terminated=1) (element-type utf8): an array of tags as strings.
134 gst_meta_api_type_get_tags (GType api)
137 g_return_val_if_fail (api != 0, FALSE);
139 tags = g_type_get_qdata (api, g_quark_from_string ("tags"));
144 return (const gchar * const *) tags;
149 * @api: the type of the #GstMeta API
150 * @impl: the name of the #GstMeta implementation
151 * @size: the size of the #GstMeta structure
152 * @init_func: (scope async): a #GstMetaInitFunction
153 * @free_func: (scope async): a #GstMetaFreeFunction
154 * @transform_func: (scope async): a #GstMetaTransformFunction
156 * Register a new #GstMeta implementation.
158 * The same @info can be retrieved later with gst_meta_get_info() by using
161 * Returns: (transfer none): a #GstMetaInfo that can be used to access metadata.
165 gst_meta_register (GType api, const gchar * impl, gsize size,
166 GstMetaInitFunction init_func, GstMetaFreeFunction free_func,
167 GstMetaTransformFunction transform_func)
172 g_return_val_if_fail (api != 0, NULL);
173 g_return_val_if_fail (impl != NULL, NULL);
174 g_return_val_if_fail (size != 0, NULL);
176 if (init_func == NULL)
177 g_critical ("Registering meta implementation '%s' without init function",
180 /* first try to register the implementation name. It's possible
181 * that this fails because it was already registered. Don't warn,
182 * glib did this for us already. */
183 type = g_pointer_type_register_static (impl);
187 info = g_slice_new (GstMetaInfo);
191 info->init_func = init_func;
192 info->free_func = free_func;
193 info->transform_func = transform_func;
195 GST_CAT_DEBUG (GST_CAT_META,
196 "register \"%s\" implementing \"%s\" of size %" G_GSIZE_FORMAT, impl,
197 g_type_name (api), size);
199 g_rw_lock_writer_lock (&lock);
200 g_hash_table_insert (metainfo, (gpointer) impl, (gpointer) info);
201 g_rw_lock_writer_unlock (&lock);
210 * Lookup a previously registered meta info structure by its implementation name
213 * Returns: (transfer none) (nullable): a #GstMetaInfo with @impl, or
214 * %NULL when no such metainfo exists.
217 gst_meta_get_info (const gchar * impl)
221 g_return_val_if_fail (impl != NULL, NULL);
223 g_rw_lock_reader_lock (&lock);
224 info = g_hash_table_lookup (metainfo, impl);
225 g_rw_lock_reader_unlock (&lock);