summaryrefslogtreecommitdiff
path: root/gst-libs/gst/tag/xmpwriter.c
blob: d714e0a03434fac5bc4d9d3495076268a3d94865 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* GStreamer TagXmpWriter
 * Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

/**
 * SECTION:gsttagxmpwriter
 * @title: GstTagXmpWriter
 * @short_description: Interface for elements that provide XMP serialization
 *
 * This interface is implemented by elements that are able to do XMP serialization. Examples for
 * such elements are #jifmux and #qtmux.
 *
 * Applications can use this interface to configure which XMP schemas should be used when serializing
 * tags into XMP. Schemas are represented by their names, a full list of the supported schemas can be
 * obtained from gst_tag_xmp_list_schemas(). By default, all schemas are used.
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "xmpwriter.h"
#include <string.h>
#include <gst/tag/tag.h>

static GQuark tag_xmp_writer_key;

typedef struct
{
  GSList *schemas;
  GMutex lock;
} GstTagXmpWriterData;

#define GST_TAG_XMP_WRITER_DATA_LOCK(data) g_mutex_lock(&data->lock)
#define GST_TAG_XMP_WRITER_DATA_UNLOCK(data) g_mutex_unlock(&data->lock)

GType
gst_tag_xmp_writer_get_type (void)
{
  static volatile gsize xmp_config_type = 0;

  if (g_once_init_enter (&xmp_config_type)) {
    GType _type;
    static const GTypeInfo xmp_config_info = {
      sizeof (GstTagXmpWriterInterface),        /* class_size */
      NULL,                     /* base_init */
      NULL,                     /* base_finalize */
      NULL,
      NULL,                     /* class_finalize */
      NULL,                     /* class_data */
      0,
      0,
      NULL
    };

    _type = g_type_register_static (G_TYPE_INTERFACE, "GstTagXmpWriter",
        &xmp_config_info, 0);
    tag_xmp_writer_key = g_quark_from_static_string ("GST_TAG_XMP_WRITER");
    g_type_interface_add_prerequisite (_type, GST_TYPE_ELEMENT);

    g_once_init_leave (&xmp_config_type, _type);
  }

  return xmp_config_type;
}

static void
gst_tag_xmp_writer_data_add_schema_unlocked (GstTagXmpWriterData * data,
    const gchar * schema)
{
  if (!g_slist_find_custom (data->schemas, schema, (GCompareFunc) strcmp)) {
    data->schemas = g_slist_prepend (data->schemas, g_strdup (schema));
  }
}

static void
gst_tag_xmp_writer_data_add_all_schemas_unlocked (GstTagXmpWriterData * data)
{
  const gchar **schemas;
  gint i = 0;

  /* initialize it with all schemas */
  schemas = gst_tag_xmp_list_schemas ();
  while (schemas[i] != NULL) {
    gst_tag_xmp_writer_data_add_schema_unlocked (data, schemas[i]);
    i++;
  }
}


static void
gst_tag_xmp_writer_data_free (gpointer p)
{
  GstTagXmpWriterData *data = (GstTagXmpWriterData *) p;
  GSList *iter;

  if (data->schemas) {
    for (iter = data->schemas; iter; iter = g_slist_next (iter)) {
      g_free (iter->data);
    }
    g_slist_free (data->schemas);
  }
  g_mutex_clear (&data->lock);

  g_slice_free (GstTagXmpWriterData, data);
}

static GstTagXmpWriterData *
gst_tag_xmp_writer_get_data (GstTagXmpWriter * xmpconfig)
{
  GstTagXmpWriterData *data;

  data = g_object_get_qdata (G_OBJECT (xmpconfig), tag_xmp_writer_key);
  if (!data) {
    /* make sure no other thread is creating a GstTagData at the same time */
    static GMutex create_mutex; /* no initialisation required */

    g_mutex_lock (&create_mutex);

    data = g_object_get_qdata (G_OBJECT (xmpconfig), tag_xmp_writer_key);
    if (!data) {
      data = g_slice_new (GstTagXmpWriterData);

      g_mutex_init (&data->lock);
      data->schemas = NULL;
      gst_tag_xmp_writer_data_add_all_schemas_unlocked (data);

      g_object_set_qdata_full (G_OBJECT (xmpconfig), tag_xmp_writer_key, data,
          gst_tag_xmp_writer_data_free);
    }
    g_mutex_unlock (&create_mutex);
  }

  return data;
}

/**
 * gst_tag_xmp_writer_add_all_schemas:
 * @config: a #GstTagXmpWriter
 *
 * Adds all available XMP schemas to the configuration. Meaning that
 * all will be used.
 */
void
gst_tag_xmp_writer_add_all_schemas (GstTagXmpWriter * config)
{
  GstTagXmpWriterData *data;

  g_return_if_fail (GST_IS_TAG_XMP_WRITER (config));

  data = gst_tag_xmp_writer_get_data (config);

  GST_TAG_XMP_WRITER_DATA_LOCK (data);
  gst_tag_xmp_writer_data_add_all_schemas_unlocked (data);
  GST_TAG_XMP_WRITER_DATA_UNLOCK (data);
}

/**
 * gst_tag_xmp_writer_add_schema:
 * @config: a #GstTagXmpWriter
 * @schema: the schema to be added
 *
 * Adds @schema to the list schemas
 */
void
gst_tag_xmp_writer_add_schema (GstTagXmpWriter * config, const gchar * schema)
{
  GstTagXmpWriterData *data;

  g_return_if_fail (GST_IS_TAG_XMP_WRITER (config));

  data = gst_tag_xmp_writer_get_data (config);

  GST_TAG_XMP_WRITER_DATA_LOCK (data);
  gst_tag_xmp_writer_data_add_schema_unlocked (data, schema);
  GST_TAG_XMP_WRITER_DATA_UNLOCK (data);
}

/**
 * gst_tag_xmp_writer_has_schema:
 * @config: a #GstTagXmpWriter
 * @schema: the schema to test
 *
 * Checks if @schema is going to be used
 *
 * Returns: %TRUE if it is going to be used
 */
gboolean
gst_tag_xmp_writer_has_schema (GstTagXmpWriter * config, const gchar * schema)
{
  GstTagXmpWriterData *data;
  gboolean ret = FALSE;
  GSList *iter;

  g_return_val_if_fail (GST_IS_TAG_XMP_WRITER (config), FALSE);

  data = gst_tag_xmp_writer_get_data (config);

  GST_TAG_XMP_WRITER_DATA_LOCK (data);
  for (iter = data->schemas; iter; iter = g_slist_next (iter)) {
    if (strcmp ((const gchar *) iter->data, schema) == 0) {
      ret = TRUE;
      break;
    }
  }
  GST_TAG_XMP_WRITER_DATA_UNLOCK (data);

  return ret;
}

/**
 * gst_tag_xmp_writer_remove_schema:
 * @config: a #GstTagXmpWriter
 * @schema: the schema to remove
 *
 * Removes a schema from the list of schemas to use. Nothing is done if
 * the schema wasn't in the list
 */
void
gst_tag_xmp_writer_remove_schema (GstTagXmpWriter * config,
    const gchar * schema)
{
  GstTagXmpWriterData *data;
  GSList *iter = NULL;

  g_return_if_fail (GST_IS_TAG_XMP_WRITER (config));

  data = gst_tag_xmp_writer_get_data (config);

  GST_TAG_XMP_WRITER_DATA_LOCK (data);
  for (iter = data->schemas; iter; iter = g_slist_next (iter)) {
    if (strcmp ((const gchar *) iter->data, schema) == 0) {
      g_free (iter->data);
      data->schemas = g_slist_delete_link (data->schemas, iter);
      break;
    }
  }
  GST_TAG_XMP_WRITER_DATA_UNLOCK (data);
}

/**
 * gst_tag_xmp_writer_remove_all_schemas:
 * @config: a #GstTagXmpWriter
 *
 * Removes all schemas from the list of schemas to use. Meaning that no
 * XMP will be generated.
 */
void
gst_tag_xmp_writer_remove_all_schemas (GstTagXmpWriter * config)
{
  GstTagXmpWriterData *data;
  GSList *iter;

  g_return_if_fail (GST_IS_TAG_XMP_WRITER (config));

  data = gst_tag_xmp_writer_get_data (config);

  GST_TAG_XMP_WRITER_DATA_LOCK (data);
  if (data->schemas) {
    for (iter = data->schemas; iter; iter = g_slist_next (iter)) {
      g_free (iter->data);
    }
    g_slist_free (data->schemas);
  }
  data->schemas = NULL;
  GST_TAG_XMP_WRITER_DATA_UNLOCK (data);
}

GstBuffer *
gst_tag_xmp_writer_tag_list_to_xmp_buffer (GstTagXmpWriter * config,
    const GstTagList * taglist, gboolean read_only)
{
  GstTagXmpWriterData *data;
  GstBuffer *buf = NULL;
  gint i = 0;
  GSList *iter;

  g_return_val_if_fail (GST_IS_TAG_XMP_WRITER (config), NULL);

  data = gst_tag_xmp_writer_get_data (config);

  GST_TAG_XMP_WRITER_DATA_LOCK (data);
  if (data->schemas) {
    gchar **array = g_new0 (gchar *, g_slist_length (data->schemas) + 1);
    if (array) {
      for (iter = data->schemas; iter; iter = g_slist_next (iter)) {
        array[i++] = (gchar *) iter->data;
      }
      buf = gst_tag_list_to_xmp_buffer (taglist, read_only,
          (const gchar **) array);
      g_free (array);
    }
  }
  GST_TAG_XMP_WRITER_DATA_UNLOCK (data);

  return buf;
}