gstfunnel: avoid access of freed pad
[platform/upstream/gstreamer.git] / gst / gstmeta.c
1 /* GStreamer
2  * Copyright (C) 2011 Wim Taymans <wim.taymans@gmail.com>
3  *
4  * gstmeta.c: metadata operations
5  *
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.
10  *
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.
15  *
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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 /**
23  * SECTION:gstmeta
24  * @short_description: Buffer metadata
25  *
26  * The #GstMeta structure should be included as the first member of a #GstBuffer
27  * metadata structure. The structure defines the API of the metadata and should
28  * be accessible to all elements using the metadata.
29  *
30  * A metadata API is registered with gst_meta_api_type_register() which takes a
31  * name for the metadata API and some tags associated with the metadata.
32  * With gst_meta_api_type_has_tag() one can check if a certain metadata API
33  * contains a given tag.
34  *
35  * Multiple implementations of a metadata API can be registered.
36  * To implement a metadata API, gst_meta_register() should be used. This
37  * function takes all parameters needed to create, free and transform metadata
38  * along with the size of the metadata. The function returns a #GstMetaInfo
39  * structure that contains the information for the implementation of the API.
40  *
41  * A specific implementation can be retrieved by name with gst_meta_get_info().
42  *
43  * See #GstBuffer for how the metadata can be added, retrieved and removed from
44  * buffers.
45  *
46  * Last reviewed on 2012-03-28 (0.11.3)
47  */
48 #include "gst_private.h"
49
50 #include "gstbuffer.h"
51 #include "gstmeta.h"
52 #include "gstinfo.h"
53 #include "gstutils.h"
54
55 static GHashTable *metainfo = NULL;
56 static GRWLock lock;
57
58 GQuark _gst_meta_transform_copy;
59 GQuark _gst_meta_tag_memory;
60
61 void
62 _priv_gst_meta_initialize (void)
63 {
64   g_rw_lock_init (&lock);
65   metainfo = g_hash_table_new (g_str_hash, g_str_equal);
66
67   _gst_meta_transform_copy = g_quark_from_static_string ("gst-copy");
68   _gst_meta_tag_memory = g_quark_from_static_string ("memory");
69 }
70
71 /**
72  * gst_meta_api_type_register:
73  * @api: an API to register
74  * @tags: tags for @api
75  *
76  * Register and return a GType for the @api and associate it with
77  * @tags.
78  *
79  * Returns: a unique GType for @api.
80  */
81 GType
82 gst_meta_api_type_register (const gchar * api, const gchar ** tags)
83 {
84   GType type;
85
86   g_return_val_if_fail (api != NULL, 0);
87   g_return_val_if_fail (tags != NULL, 0);
88
89   GST_CAT_DEBUG (GST_CAT_META, "register API \"%s\"", api);
90   type = g_pointer_type_register_static (api);
91
92   if (type != 0) {
93     gint i;
94
95     for (i = 0; tags[i]; i++) {
96       GST_CAT_DEBUG (GST_CAT_META, "  adding tag \"%s\"", tags[i]);
97       g_type_set_qdata (type, g_quark_from_string (tags[i]),
98           GINT_TO_POINTER (TRUE));
99     }
100   }
101   return type;
102 }
103
104 /**
105  * gst_meta_api_type_has_tag:
106  * @api: an API
107  * @tag: the tag to check
108  *
109  * Check if @api was registered with @tag.
110  *
111  * Returns: %TRUE if @api was registered with @tag.
112  */
113 gboolean
114 gst_meta_api_type_has_tag (GType api, GQuark tag)
115 {
116   g_return_val_if_fail (api != 0, FALSE);
117   g_return_val_if_fail (tag != 0, FALSE);
118
119   return g_type_get_qdata (api, tag) != NULL;
120 }
121
122 /**
123  * gst_meta_register:
124  * @api: the type of the #GstMeta API
125  * @impl: the name of the #GstMeta implementation
126  * @size: the size of the #GstMeta structure
127  * @init_func: a #GstMetaInitFunction
128  * @free_func: a #GstMetaFreeFunction
129  * @transform_func: a #GstMetaTransformFunction
130  *
131  * Register a new #GstMeta implementation.
132  *
133  * The same @info can be retrieved later with gst_meta_get_info() by using
134  * @impl as the key.
135  *
136  * Returns: (transfer none): a #GstMetaInfo that can be used to access metadata.
137  */
138
139 const GstMetaInfo *
140 gst_meta_register (GType api, const gchar * impl, gsize size,
141     GstMetaInitFunction init_func, GstMetaFreeFunction free_func,
142     GstMetaTransformFunction transform_func)
143 {
144   GstMetaInfo *info;
145
146   g_return_val_if_fail (api != 0, NULL);
147   g_return_val_if_fail (impl != NULL, NULL);
148   g_return_val_if_fail (size != 0, NULL);
149
150   info = g_slice_new (GstMetaInfo);
151   info->api = api;
152   info->type = g_pointer_type_register_static (impl);
153   info->size = size;
154   info->init_func = init_func;
155   info->free_func = free_func;
156   info->transform_func = transform_func;
157
158   GST_CAT_DEBUG (GST_CAT_META,
159       "register \"%s\" implementing \"%s\" of size %" G_GSIZE_FORMAT, impl,
160       g_type_name (api), size);
161
162   g_rw_lock_writer_lock (&lock);
163   g_hash_table_insert (metainfo, (gpointer) impl, (gpointer) info);
164   g_rw_lock_writer_unlock (&lock);
165
166   return info;
167 }
168
169 /**
170  * gst_meta_get_info:
171  * @impl: the name
172  *
173  * Lookup a previously registered meta info structure by its implementation name
174  * @impl.
175  *
176  * Returns: (transfer none): a #GstMetaInfo with @impl, or #NULL when no such
177  * metainfo exists.
178  */
179 const GstMetaInfo *
180 gst_meta_get_info (const gchar * impl)
181 {
182   GstMetaInfo *info;
183
184   g_return_val_if_fail (impl != NULL, NULL);
185
186   g_rw_lock_reader_lock (&lock);
187   info = g_hash_table_lookup (metainfo, impl);
188   g_rw_lock_reader_unlock (&lock);
189
190   return info;
191 }