Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / gst-libs / gst / tag / xmpwriter.c
1 /* GStreamer XmpConfig
2  * Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /**
21  * SECTION:gstxmpconfig
22  * @short_description: Interface for elements that provide XMP serialization
23  *
24  * <refsect2>
25  * <para>
26  * This interface is implemented by elements that are able to do XMP serialization. Examples for
27  * such elements are #jifmux and #qtmux.
28  * </para>
29  * <para>
30  * Applications can use this interface to configure which XMP schemas should be used when serializing
31  * tags into XMP. Schemas are represented by their names, a full list of the supported schemas can be
32  * obtained from gst_tag_xmp_list_schemas(). By default, all schemas are used.
33  * </para>
34  * </refsect2>
35  */
36
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40
41 #include "xmpwriter.h"
42 #include <string.h>
43 #include <gst/tag/tag.h>
44
45 static GQuark tag_xmp_writer_key;
46
47 typedef struct
48 {
49   GSList *schemas;
50   GStaticMutex lock;
51 } GstTagXmpWriterData;
52
53 GType
54 gst_tag_xmp_writer_get_type (void)
55 {
56   static volatile gsize xmp_config_type = 0;
57
58   if (g_once_init_enter (&xmp_config_type)) {
59     GType _type;
60     static const GTypeInfo xmp_config_info = {
61       sizeof (GstTagXmpWriterInterface),        /* class_size */
62       NULL,                     /* base_init */
63       NULL,                     /* base_finalize */
64       NULL,
65       NULL,                     /* class_finalize */
66       NULL,                     /* class_data */
67       0,
68       0,
69       NULL
70     };
71
72     _type = g_type_register_static (G_TYPE_INTERFACE, "GstTagXmpWriter",
73         &xmp_config_info, 0);
74     tag_xmp_writer_key = g_quark_from_static_string ("GST_TAG_XMP_WRITER");
75     g_type_interface_add_prerequisite (_type, GST_TYPE_ELEMENT);
76
77     g_once_init_leave (&xmp_config_type, _type);
78   }
79
80   return xmp_config_type;
81 }
82
83 static void
84 gst_tag_xmp_writer_data_add_schema_unlocked (GstTagXmpWriterData * data,
85     const gchar * schema)
86 {
87   if (!g_slist_find_custom (data->schemas, schema, (GCompareFunc) strcmp)) {
88     data->schemas = g_slist_prepend (data->schemas, g_strdup (schema));
89   }
90 }
91
92 static void
93 gst_tag_xmp_writer_data_add_all_schemas_unlocked (GstTagXmpWriterData * data)
94 {
95   const gchar **schemas;
96   gint i = 0;
97
98   /* initialize it with all schemas */
99   schemas = gst_tag_xmp_list_schemas ();
100   while (schemas[i] != NULL) {
101     gst_tag_xmp_writer_data_add_schema_unlocked (data, schemas[i]);
102     i++;
103   }
104 }
105
106
107 static void
108 gst_tag_xmp_writer_data_free (gpointer p)
109 {
110   GstTagXmpWriterData *data = (GstTagXmpWriterData *) p;
111   GSList *iter;
112
113   if (data->schemas) {
114     for (iter = data->schemas; iter; iter = g_slist_next (iter)) {
115       g_free (iter->data);
116     }
117     g_slist_free (data->schemas);
118   }
119
120   g_static_mutex_free (&data->lock);
121
122   g_slice_free (GstTagXmpWriterData, data);
123 }
124
125 static GstTagXmpWriterData *
126 gst_tag_xmp_writer_get_data (GstTagXmpWriter * xmpconfig)
127 {
128   GstTagXmpWriterData *data;
129
130   data = g_object_get_qdata (G_OBJECT (xmpconfig), tag_xmp_writer_key);
131   if (!data) {
132     static GStaticMutex create_mutex = G_STATIC_MUTEX_INIT;
133
134     /* make sure no other thread is creating a GstTagXmpWriterData at the same time */
135     g_static_mutex_lock (&create_mutex);
136     data = g_object_get_qdata (G_OBJECT (xmpconfig), tag_xmp_writer_key);
137     if (!data) {
138       data = g_slice_new (GstTagXmpWriterData);
139       g_static_mutex_init (&data->lock);
140
141       data->schemas = NULL;
142       gst_tag_xmp_writer_data_add_all_schemas_unlocked (data);
143
144       g_object_set_qdata_full (G_OBJECT (xmpconfig), tag_xmp_writer_key, data,
145           gst_tag_xmp_writer_data_free);
146     }
147     g_static_mutex_unlock (&create_mutex);
148   }
149
150   return data;
151 }
152
153 /**
154  * gst_tag_xmp_writer_add_all_schemas:
155  * @config: a #GstTagXmpWriter
156  *
157  * Adds all available XMP schemas to the configuration. Meaning that
158  * all will be used.
159  *
160  * Since: 0.10.33
161  */
162 void
163 gst_tag_xmp_writer_add_all_schemas (GstTagXmpWriter * config)
164 {
165   GstTagXmpWriterData *data;
166
167   g_return_if_fail (GST_IS_TAG_XMP_WRITER (config));
168
169   data = gst_tag_xmp_writer_get_data (config);
170
171   g_static_mutex_lock (&data->lock);
172   gst_tag_xmp_writer_data_add_all_schemas_unlocked (data);
173   g_static_mutex_unlock (&data->lock);
174 }
175
176 /**
177  * gst_tag_xmp_writer_add_schema:
178  * @config: a #GstTagXmpWriter
179  * @schema: the schema to be added
180  *
181  * Adds @schema to the list schemas
182  *
183  * Since: 0.10.33
184  */
185 void
186 gst_tag_xmp_writer_add_schema (GstTagXmpWriter * config, const gchar * schema)
187 {
188   GstTagXmpWriterData *data;
189
190   g_return_if_fail (GST_IS_TAG_XMP_WRITER (config));
191
192   data = gst_tag_xmp_writer_get_data (config);
193
194   g_static_mutex_lock (&data->lock);
195   gst_tag_xmp_writer_data_add_schema_unlocked (data, schema);
196   g_static_mutex_unlock (&data->lock);
197 }
198
199 /**
200  * gst_tag_xmp_writer_has_schema:
201  * @config: a #GstTagXmpWriter
202  * @schema: the schema to test
203  *
204  * Checks if @schema is going to be used
205  *
206  * Returns: %TRUE if it is going to be used
207  * Since: 0.10.33
208  */
209 gboolean
210 gst_tag_xmp_writer_has_schema (GstTagXmpWriter * config, const gchar * schema)
211 {
212   GstTagXmpWriterData *data;
213   gboolean ret = FALSE;
214   GSList *iter;
215
216   g_return_val_if_fail (GST_IS_TAG_XMP_WRITER (config), FALSE);
217
218   data = gst_tag_xmp_writer_get_data (config);
219
220   g_static_mutex_lock (&data->lock);
221   for (iter = data->schemas; iter; iter = g_slist_next (iter)) {
222     if (strcmp ((const gchar *) iter->data, schema) == 0) {
223       ret = TRUE;
224       break;
225     }
226   }
227   g_static_mutex_unlock (&data->lock);
228
229   return ret;
230 }
231
232 /**
233  * gst_tag_xmp_writer_remove_schema:
234  * @config: a #GstTagXmpWriter
235  * @schema: the schema to remove
236  *
237  * Removes a schema from the list of schemas to use. Nothing is done if
238  * the schema wasn't in the list
239  *
240  * Since: 0.10.33
241  */
242 void
243 gst_tag_xmp_writer_remove_schema (GstTagXmpWriter * config,
244     const gchar * schema)
245 {
246   GstTagXmpWriterData *data;
247   GSList *iter = NULL;
248
249   g_return_if_fail (GST_IS_TAG_XMP_WRITER (config));
250
251   data = gst_tag_xmp_writer_get_data (config);
252
253   g_static_mutex_lock (&data->lock);
254   for (iter = data->schemas; iter; iter = g_slist_next (iter)) {
255     if (strcmp ((const gchar *) iter->data, schema) == 0) {
256       g_free (iter->data);
257       data->schemas = g_slist_delete_link (data->schemas, iter);
258       break;
259     }
260   }
261   g_static_mutex_unlock (&data->lock);
262 }
263
264 /**
265  * gst_tag_xmp_writer_remove_all_schemas:
266  * @config: a #GstTagXmpWriter
267  *
268  * Removes all schemas from the list of schemas to use. Meaning that no
269  * XMP will be generated.
270  *
271  * Since: 0.10.33
272  */
273 void
274 gst_tag_xmp_writer_remove_all_schemas (GstTagXmpWriter * config)
275 {
276   GstTagXmpWriterData *data;
277   GSList *iter;
278
279   g_return_if_fail (GST_IS_TAG_XMP_WRITER (config));
280
281   data = gst_tag_xmp_writer_get_data (config);
282
283   g_static_mutex_lock (&data->lock);
284   if (data->schemas) {
285     for (iter = data->schemas; iter; iter = g_slist_next (iter)) {
286       g_free (iter->data);
287     }
288     g_slist_free (data->schemas);
289   }
290   data->schemas = NULL;
291   g_static_mutex_unlock (&data->lock);
292 }
293
294 GstBuffer *
295 gst_tag_xmp_writer_tag_list_to_xmp_buffer (GstTagXmpWriter * config,
296     const GstTagList * taglist, gboolean read_only)
297 {
298   GstTagXmpWriterData *data;
299   GstBuffer *buf = NULL;
300   gint i = 0;
301   GSList *iter;
302
303   g_return_val_if_fail (GST_IS_TAG_XMP_WRITER (config), NULL);
304
305   data = gst_tag_xmp_writer_get_data (config);
306
307   g_static_mutex_lock (&data->lock);
308   if (data->schemas) {
309     gchar **array = g_new0 (gchar *, g_slist_length (data->schemas) + 1);
310     if (array) {
311       for (iter = data->schemas; iter; iter = g_slist_next (iter)) {
312         array[i++] = (gchar *) iter->data;
313       }
314       buf = gst_tag_list_to_xmp_buffer_full (taglist, read_only,
315           (const gchar **) array);
316       g_free (array);
317     }
318   }
319   g_static_mutex_unlock (&data->lock);
320
321   return buf;
322 }