meta: add method to check for a tag
[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  * Last reviewed on December 17th, 2009 (0.10.26)
27  */
28 #include "gst_private.h"
29
30 #include "gstbuffer.h"
31 #include "gstmeta.h"
32 #include "gstinfo.h"
33 #include "gstutils.h"
34
35 static GHashTable *metainfo = NULL;
36 static GRWLock lock;
37
38 GQuark _gst_meta_transform_copy;
39
40 void
41 _priv_gst_meta_initialize (void)
42 {
43   g_rw_lock_init (&lock);
44   metainfo = g_hash_table_new (g_str_hash, g_str_equal);
45
46   _gst_meta_transform_copy = g_quark_from_static_string ("copy");
47 }
48
49 /**
50  * gst_meta_register:
51  * @api: the name of the #GstMeta API
52  * @impl: the name of the #GstMeta implementation
53  * @size: the size of the #GstMeta structure
54  * @init_func: a #GstMetaInitFunction
55  * @free_func: a #GstMetaFreeFunction
56  * @transform_func: a #GstMetaTransformFunction
57  * @tags: a NULL terminated array of strings describing what the metadata
58  *        contains info about.
59  *
60  * Register a new #GstMeta implementation.
61  *
62  * The same @info can be retrieved later with gst_meta_get_info() by using
63  * @impl as the key.
64  *
65  * Returns: (transfer none): a #GstMetaInfo that can be used to access metadata.
66  */
67
68 const GstMetaInfo *
69 gst_meta_register (const gchar * api, const gchar * impl, gsize size,
70     GstMetaInitFunction init_func, GstMetaFreeFunction free_func,
71     GstMetaTransformFunction transform_func, const gchar ** tags)
72 {
73   GstMetaInfo *info;
74   guint len, i;
75   GQuark *qtags;
76
77   g_return_val_if_fail (api != NULL, NULL);
78   g_return_val_if_fail (impl != NULL, NULL);
79   g_return_val_if_fail (size != 0, NULL);
80   g_return_val_if_fail (tags != NULL, NULL);
81
82   info = g_slice_new (GstMetaInfo);
83   info->api = g_quark_from_string (api);
84   info->type = g_pointer_type_register_static (impl);
85   info->size = size;
86   info->init_func = init_func;
87   info->free_func = free_func;
88   info->transform_func = transform_func;
89
90   len = g_strv_length ((gchar **) tags);
91   qtags = g_new0 (GQuark, len);
92   for (i = 0; i < len; i++)
93     qtags[i] = g_quark_from_static_string (tags[i]);
94   info->tags = qtags;
95
96   GST_DEBUG ("register \"%s\" implementing \"%s\" of size %" G_GSIZE_FORMAT,
97       api, impl, size);
98
99   g_rw_lock_writer_lock (&lock);
100   g_hash_table_insert (metainfo, (gpointer) impl, (gpointer) info);
101   g_rw_lock_writer_unlock (&lock);
102
103   return info;
104 }
105
106 /**
107  * gst_meta_get_info:
108  * @impl: the name
109  *
110  * Lookup a previously registered meta info structure by its implementation name
111  * @impl.
112  *
113  * Returns: (transfer none): a #GstMetaInfo with @impl, or #NULL when no such
114  * metainfo exists.
115  */
116 const GstMetaInfo *
117 gst_meta_get_info (const gchar * impl)
118 {
119   GstMetaInfo *info;
120
121   g_return_val_if_fail (impl != NULL, NULL);
122
123   g_rw_lock_reader_lock (&lock);
124   info = g_hash_table_lookup (metainfo, impl);
125   g_rw_lock_reader_unlock (&lock);
126
127   return info;
128 }
129
130 /*
131  * gst_meta_info_has_tag:
132  * @info: a #GstMetaInfo
133  * @tag: a #GQuark
134  *
135  * Check if @info contains @tag.
136  *
137  * Returns: %TRUE when @info contains @tag.
138  */
139 gboolean
140 gst_meta_info_has_tag (const GstMetaInfo * info, GQuark tag)
141 {
142   gint i;
143
144   g_return_val_if_fail (info != NULL, FALSE);
145   g_return_val_if_fail (tag != 0, FALSE);
146
147   for (i = 0; info->tags[i]; i++)
148     if (info->tags[i] == tag)
149       return TRUE;
150
151   return FALSE;
152 }