2 * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
4 * gsttagsetter.c: interface for tag setting on elements
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., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * SECTION:gsttagsetter
24 * @short_description: Element interface that allows setting and retrieval
27 * Element interface that allows setting of media metadata.
29 * Elements that support changing a stream's metadata will implement this
30 * interface. Examples of such elements are 'vorbisenc', 'theoraenc' and
33 * If you just want to retrieve metadata in your application then all you
34 * need to do is watch for tag messages on your pipeline's bus. This
35 * interface is only for setting metadata, not for extracting it. To set tags
36 * from the application, find tagsetter elements and set tags using e.g.
37 * gst_tag_setter_merge_tags() or gst_tag_setter_add_tags(). Also consider
38 * setting the #GstTagMergeMode that is used for tag events that arrive at the
39 * tagsetter element (default mode is to keep existing tags).
40 * The application should do that before the element goes to %GST_STATE_PAUSED.
42 * Elements implementing the #GstTagSetter interface often have to merge
43 * any tags received from upstream and the tags set by the application via
44 * the interface. This can be done like this:
47 * GstTagMergeMode merge_mode;
48 * const GstTagList *application_tags;
49 * const GstTagList *event_tags;
50 * GstTagSetter *tagsetter;
53 * tagsetter = GST_TAG_SETTER (element);
55 * merge_mode = gst_tag_setter_get_tag_merge_mode (tagsetter);
56 * application_tags = gst_tag_setter_get_tag_list (tagsetter);
57 * event_tags = (const GstTagList *) element->event_tags;
59 * GST_LOG_OBJECT (tagsetter, "merging tags, merge mode = %d", merge_mode);
60 * GST_LOG_OBJECT (tagsetter, "event tags: %" GST_PTR_FORMAT, event_tags);
61 * GST_LOG_OBJECT (tagsetter, "set tags: %" GST_PTR_FORMAT, application_tags);
63 * result = gst_tag_list_merge (application_tags, event_tags, merge_mode);
65 * GST_LOG_OBJECT (tagsetter, "final tags: %" GST_PTR_FORMAT, result);
68 * Last reviewed on 2006-05-18 (0.10.6)
75 #include "gst_private.h"
76 #include "gsttagsetter.h"
77 #include <gobject/gvaluecollector.h>
80 static GQuark gst_tag_key;
89 #define GST_TAG_DATA_LOCK(data) g_mutex_lock(&data->lock)
90 #define GST_TAG_DATA_UNLOCK(data) g_mutex_unlock(&data->lock)
93 gst_tag_setter_get_type (void)
95 static volatile gsize tag_setter_type = 0;
97 if (g_once_init_enter (&tag_setter_type)) {
99 static const GTypeInfo tag_setter_info = {
100 sizeof (GstTagSetterInterface), /* class_size */
101 NULL, /* base_init */
102 NULL, /* base_finalize */
104 NULL, /* class_finalize */
105 NULL, /* class_data */
111 _type = g_type_register_static (G_TYPE_INTERFACE, "GstTagSetter",
112 &tag_setter_info, 0);
114 g_type_interface_add_prerequisite (_type, GST_TYPE_ELEMENT);
116 gst_tag_key = g_quark_from_static_string ("GST_TAG_SETTER");
117 g_once_init_leave (&tag_setter_type, _type);
120 return tag_setter_type;
124 gst_tag_data_free (gpointer p)
126 GstTagData *data = (GstTagData *) p;
129 gst_tag_list_free (data->list);
131 g_mutex_clear (&data->lock);
133 g_slice_free (GstTagData, data);
137 gst_tag_setter_get_data (GstTagSetter * setter)
141 data = g_object_get_qdata (G_OBJECT (setter), gst_tag_key);
143 /* make sure no other thread is creating a GstTagData at the same time */
144 static GMutex create_mutex; /* no initialisation required */
146 g_mutex_lock (&create_mutex);
148 data = g_object_get_qdata (G_OBJECT (setter), gst_tag_key);
150 data = g_slice_new (GstTagData);
151 g_mutex_init (&data->lock);
153 data->mode = GST_TAG_MERGE_KEEP;
154 g_object_set_qdata_full (G_OBJECT (setter), gst_tag_key, data,
158 g_mutex_unlock (&create_mutex);
165 * gst_tag_setter_reset_tags:
166 * @setter: a #GstTagSetter
168 * Reset the internal taglist. Elements should call this from within the
169 * state-change handler.
174 gst_tag_setter_reset_tags (GstTagSetter * setter)
178 g_return_if_fail (GST_IS_TAG_SETTER (setter));
180 data = gst_tag_setter_get_data (setter);
182 GST_TAG_DATA_LOCK (data);
184 gst_tag_list_free (data->list);
187 GST_TAG_DATA_UNLOCK (data);
191 * gst_tag_setter_merge_tags:
192 * @setter: a #GstTagSetter
193 * @list: a tag list to merge from
194 * @mode: the mode to merge with
196 * Merges the given list into the setter's list using the given mode.
199 gst_tag_setter_merge_tags (GstTagSetter * setter, const GstTagList * list,
200 GstTagMergeMode mode)
204 g_return_if_fail (GST_IS_TAG_SETTER (setter));
205 g_return_if_fail (GST_TAG_MODE_IS_VALID (mode));
206 g_return_if_fail (GST_IS_TAG_LIST (list));
208 data = gst_tag_setter_get_data (setter);
210 GST_TAG_DATA_LOCK (data);
211 if (data->list == NULL) {
212 if (mode != GST_TAG_MERGE_KEEP_ALL)
213 data->list = gst_tag_list_copy (list);
215 gst_tag_list_insert (data->list, list, mode);
217 GST_TAG_DATA_UNLOCK (data);
221 * gst_tag_setter_add_tags:
222 * @setter: a #GstTagSetter
223 * @mode: the mode to use
225 * @...: more tag / value pairs to set
227 * Adds the given tag / value pairs on the setter using the given merge mode.
228 * The list must be terminated with NULL.
231 gst_tag_setter_add_tags (GstTagSetter * setter, GstTagMergeMode mode,
232 const gchar * tag, ...)
236 g_return_if_fail (GST_IS_TAG_SETTER (setter));
237 g_return_if_fail (GST_TAG_MODE_IS_VALID (mode));
239 va_start (args, tag);
240 gst_tag_setter_add_tag_valist (setter, mode, tag, args);
245 * gst_tag_setter_add_tag_values:
246 * @setter: a #GstTagSetter
247 * @mode: the mode to use
249 * @...: more tag / GValue pairs to set
251 * Adds the given tag / GValue pairs on the setter using the given merge mode.
252 * The list must be terminated with NULL.
255 gst_tag_setter_add_tag_values (GstTagSetter * setter, GstTagMergeMode mode,
256 const gchar * tag, ...)
260 g_return_if_fail (GST_IS_TAG_SETTER (setter));
261 g_return_if_fail (GST_TAG_MODE_IS_VALID (mode));
263 va_start (args, tag);
264 gst_tag_setter_add_tag_valist_values (setter, mode, tag, args);
269 * gst_tag_setter_add_tag_valist:
270 * @setter: a #GstTagSetter
271 * @mode: the mode to use
273 * @var_args: tag / value pairs to set
275 * Adds the given tag / value pairs on the setter using the given merge mode.
276 * The list must be terminated with NULL.
279 gst_tag_setter_add_tag_valist (GstTagSetter * setter, GstTagMergeMode mode,
280 const gchar * tag, va_list var_args)
284 g_return_if_fail (GST_IS_TAG_SETTER (setter));
285 g_return_if_fail (GST_TAG_MODE_IS_VALID (mode));
287 data = gst_tag_setter_get_data (setter);
289 GST_TAG_DATA_LOCK (data);
291 data->list = gst_tag_list_new_empty ();
293 gst_tag_list_add_valist (data->list, mode, tag, var_args);
295 GST_TAG_DATA_UNLOCK (data);
299 * gst_tag_setter_add_tag_valist_values:
300 * @setter: a #GstTagSetter
301 * @mode: the mode to use
303 * @var_args: tag / GValue pairs to set
305 * Adds the given tag / GValue pairs on the setter using the given merge mode.
306 * The list must be terminated with NULL.
309 gst_tag_setter_add_tag_valist_values (GstTagSetter * setter,
310 GstTagMergeMode mode, const gchar * tag, va_list var_args)
314 g_return_if_fail (GST_IS_TAG_SETTER (setter));
315 g_return_if_fail (GST_TAG_MODE_IS_VALID (mode));
317 data = gst_tag_setter_get_data (setter);
319 GST_TAG_DATA_LOCK (data);
322 data->list = gst_tag_list_new_empty ();
324 gst_tag_list_add_valist_values (data->list, mode, tag, var_args);
326 GST_TAG_DATA_UNLOCK (data);
330 * gst_tag_setter_add_tag_value:
331 * @setter: a #GstTagSetter
332 * @mode: the mode to use
334 * @value: GValue to set for the tag
336 * Adds the given tag / GValue pair on the setter using the given merge mode.
341 gst_tag_setter_add_tag_value (GstTagSetter * setter,
342 GstTagMergeMode mode, const gchar * tag, const GValue * value)
346 g_return_if_fail (GST_IS_TAG_SETTER (setter));
347 g_return_if_fail (GST_TAG_MODE_IS_VALID (mode));
349 data = gst_tag_setter_get_data (setter);
351 GST_TAG_DATA_LOCK (data);
354 data->list = gst_tag_list_new_empty ();
356 gst_tag_list_add_value (data->list, mode, tag, value);
358 GST_TAG_DATA_UNLOCK (data);
362 * gst_tag_setter_get_tag_list:
363 * @setter: a #GstTagSetter
365 * Returns the current list of tags the setter uses. The list should not be
368 * This function is not thread-safe.
370 * Returns: (transfer none): a current snapshot of the taglist used in the
371 * setter or NULL if none is used.
374 gst_tag_setter_get_tag_list (GstTagSetter * setter)
376 g_return_val_if_fail (GST_IS_TAG_SETTER (setter), NULL);
378 return gst_tag_setter_get_data (setter)->list;
382 * gst_tag_setter_set_tag_merge_mode:
383 * @setter: a #GstTagSetter
384 * @mode: The mode with which tags are added
386 * Sets the given merge mode that is used for adding tags from events to tags
387 * specified by this interface. The default is #GST_TAG_MERGE_KEEP, which keeps
388 * the tags set with this interface and discards tags from events.
391 gst_tag_setter_set_tag_merge_mode (GstTagSetter * setter, GstTagMergeMode mode)
395 g_return_if_fail (GST_IS_TAG_SETTER (setter));
396 g_return_if_fail (GST_TAG_MODE_IS_VALID (mode));
398 data = gst_tag_setter_get_data (setter);
400 GST_TAG_DATA_LOCK (data);
402 GST_TAG_DATA_UNLOCK (data);
406 * gst_tag_setter_get_tag_merge_mode:
407 * @setter: a #GstTagSetter
409 * Queries the mode by which tags inside the setter are overwritten by tags
412 * Returns: the merge mode used inside the element.
415 gst_tag_setter_get_tag_merge_mode (GstTagSetter * setter)
417 GstTagMergeMode mode;
420 g_return_val_if_fail (GST_IS_TAG_SETTER (setter), GST_TAG_MERGE_UNDEFINED);
422 data = gst_tag_setter_get_data (setter);
424 GST_TAG_DATA_LOCK (data);
426 GST_TAG_DATA_UNLOCK (data);