2 * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
4 * gsttag.c: tag support (aka metadata)
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.
26 #include "gst_private.h"
31 #include <gobject/gvaluecollector.h>
34 #define GST_TAG_IS_VALID(tag) (gst_tag_get_info (tag) != NULL)
37 GType type; /* type the data is in */
39 gchar * nick; /* translated name */
40 gchar * blurb; /* translated description of type */
42 GstTagMergeFunc merge_func; /* functions to merge the values */
45 #define TAGLIST "taglist"
46 static GQuark gst_tag_list_quark;
47 static GMutex *__tag_mutex;
48 static GHashTable *__tags;
49 #define TAG_LOCK g_mutex_lock (__tag_mutex)
50 #define TAG_UNLOCK g_mutex_unlock (__tag_mutex)
53 _gst_tag_initialize (void)
55 gst_tag_list_quark = g_quark_from_static_string (TAGLIST);
56 __tag_mutex = g_mutex_new ();
57 __tags = g_hash_table_new (g_direct_hash, g_direct_equal);
58 gst_tag_register (GST_TAG_TITLE,
61 _("commonly used title"),
62 gst_tag_merge_strings_with_comma);
63 gst_tag_register (GST_TAG_ARTIST,
66 _("person(s) resposible for the recording"),
67 gst_tag_merge_strings_with_comma);
68 gst_tag_register (GST_TAG_ALBUM,
71 _("album containing this data"),
72 gst_tag_merge_strings_with_comma);
73 gst_tag_register (GST_TAG_DATE,
74 G_TYPE_UINT, /* FIXME: own data type for dates? */
76 _("date the data was created in julien days"),
78 gst_tag_register (GST_TAG_GENRE,
81 _("genre this data belongs to"),
82 gst_tag_merge_strings_with_comma);
83 gst_tag_register (GST_TAG_COMMENT,
86 _("free text commenting the data"),
87 gst_tag_merge_strings_with_comma);
88 gst_tag_register (GST_TAG_TRACK_NUMBER,
91 _("track number inside a collection"),
92 gst_tag_merge_use_first);
93 gst_tag_register (GST_TAG_TRACK_COUNT,
96 _("count of tracks inside collection this track belongs to"),
97 gst_tag_merge_use_first);
98 gst_tag_register (GST_TAG_LOCATION,
101 _("original location of file as a URI"),
102 gst_tag_merge_strings_with_comma);
103 gst_tag_register (GST_TAG_DESCRIPTION,
106 _("short text describing the content of the data"),
107 gst_tag_merge_strings_with_comma);
108 gst_tag_register (GST_TAG_VERSION,
111 _("version of this data"),
113 gst_tag_register (GST_TAG_ISRC,
116 _("International Standard Recording Code - see http://www.ifpi.org/isrc/"),
118 gst_tag_register (GST_TAG_ORGANIZATION,
121 _("organization"), /* FIXME */
122 gst_tag_merge_strings_with_comma);
123 gst_tag_register (GST_TAG_COPYRIGHT,
126 _("copyright notice of the data"),
128 gst_tag_register (GST_TAG_CONTACT,
131 _("contact information"),
132 gst_tag_merge_strings_with_comma);
133 gst_tag_register (GST_TAG_LICENSE,
136 _("license of data"),
138 gst_tag_register (GST_TAG_PERFORMER,
141 _("person(s) performing"),
142 gst_tag_merge_strings_with_comma);
143 gst_tag_register (GST_TAG_DURATION,
146 _("length in GStreamer time units (nanoseconds)"),
148 gst_tag_register (GST_TAG_CODEC,
151 _("codec the data is stored in"),
152 gst_tag_merge_strings_with_comma);
153 gst_tag_register (GST_TAG_MINIMUM_BITRATE,
155 _("minimum bitrate"),
156 _("minimum bitrate in bits/s"),
158 gst_tag_register (GST_TAG_BITRATE,
161 _("exact or average bitrate in bits/s"),
163 gst_tag_register (GST_TAG_MAXIMUM_BITRATE,
165 _("maximum bitrate"),
166 _("maximum bitrate in bits/s"),
170 * gst_tag_merge_use_first:
171 * @dest: uninitialized GValue to store result in
172 * @src: GValue to copy from
174 * This is a convenience function for the func argument of gst_tag_register().
175 * It creates a copy of the first value from the list.
178 gst_tag_merge_use_first (GValue *dest, const GValue *src)
180 const GValue *ret = gst_value_list_get_value (src, 0);
182 g_value_init (dest, G_VALUE_TYPE (ret));
183 g_value_copy (ret, dest);
186 * gst_tag_merge_strings_with_comma:
187 * @dest: uninitialized GValue to store result in
188 * @src: GValue to copy from
190 * This is a convenience function for the func argument of gst_tag_register().
191 * It concatenates all given strings using a comma. The tag must be registered
192 * as a G_TYPE_STRING or this function will fail.
195 gst_tag_merge_strings_with_comma (GValue *dest, const GValue *src)
200 count = gst_value_list_get_size (src);
201 str = g_string_new (g_value_get_string (gst_value_list_get_value (src, 0)));
202 for (i = 1; i < count; i++) {
203 /* seperator between two string */
204 str = g_string_append (str, _(", "));
205 str = g_string_append (str, g_value_get_string (gst_value_list_get_value (src, 1)));
208 g_value_init (dest, G_TYPE_STRING);
209 g_value_set_string_take_ownership (dest, str->str);
210 g_string_free (str, FALSE);
213 gst_tag_lookup (GQuark entry)
218 ret = g_hash_table_lookup (__tags, GUINT_TO_POINTER (entry));
225 * @name: the name or identifier string
226 * @type: the type this data is in
227 * @nick: human-readable name
228 * @blurb: a human-readable description about this tag
229 * @func: function for merging multiple values of this tag
231 * Registers a new tag type for the use with GStreamer's type system. If a type
232 * with that name is already registered, that one is used.
233 * The old registration may have used a different type however. So don't rely
234 * on youre supplied values.
235 * If you know the type is already registered, use gst_tag_lookup instead.
236 * This function takes ownership of all supplied variables.
239 gst_tag_register (gchar *name, GType type, gchar *nick, gchar *blurb,
240 GstTagMergeFunc func)
245 g_return_if_fail (name != NULL);
246 g_return_if_fail (nick != NULL);
247 g_return_if_fail (blurb != NULL);
248 g_return_if_fail (type != 0 && type != GST_TYPE_LIST);
250 key = g_quark_from_string (name);
251 info = gst_tag_lookup (key);
252 g_return_if_fail (info == NULL);
254 info = g_new (GstTagInfo, 1);
258 info->merge_func = func;
261 g_hash_table_insert (__tags, GUINT_TO_POINTER (key), info);
266 * @tag: name of the tag
268 * Checks if the given type is already registered.
270 * Returns: TRUE if the type is already registered
273 gst_tag_exists (const gchar *tag)
275 g_return_val_if_fail (tag != NULL, FALSE);
277 return gst_tag_lookup (g_quark_from_string (tag)) != NULL;
283 * Gets the #GType used for this tag.
285 * Returns: the #GType of this tag
288 gst_tag_get_type (const gchar *tag)
292 g_return_val_if_fail (tag != NULL, 0);
293 info = gst_tag_lookup (g_quark_from_string (tag));
294 g_return_val_if_fail (info != NULL, 0);
302 * Returns the human-readable name of this tag, You must not change or free
305 * Returns: the human-readable name of this tag
308 gst_tag_get_nick (const gchar *tag)
312 g_return_val_if_fail (tag != NULL, NULL);
313 info = gst_tag_lookup (g_quark_from_string (tag));
314 g_return_val_if_fail (info != NULL, NULL);
319 * gst_tag_get_description:
322 * Returns the human-readable description of this tag, You must not change or
325 * Return the human-readable description of this tag
328 gst_tag_get_description (const gchar *tag)
332 g_return_val_if_fail (tag != NULL, NULL);
333 info = gst_tag_lookup (g_quark_from_string (tag));
334 g_return_val_if_fail (info != NULL, NULL);
339 * gst_tag_list_is_fixed:
342 * Checks if the given tag is fixed. A fixed tag can only contain one value.
343 * Unfixed tags can contain lists of values.
345 * Returns: TRUE, if the given tag is fixed.
348 gst_tag_is_fixed (const gchar *tag)
352 g_return_val_if_fail (tag != NULL, FALSE);
353 info = gst_tag_lookup (g_quark_from_string (tag));
354 g_return_val_if_fail (info != NULL, FALSE);
356 return info->merge_func == NULL;
361 * Creates a new empty GstTagList.
363 * Returns: An empty tag list
366 gst_tag_list_new (void)
368 return GST_TAG_LIST (gst_structure_new (TAGLIST, NULL));
372 * @p: Object that might be a taglist
374 * Checks if the given pointer is a taglist.
376 * Returns: TRUE, if the given pointer is a taglist
379 gst_is_tag_list (gconstpointer p)
381 g_return_val_if_fail (p != NULL, FALSE);
383 return ((GstStructure *) p)->name == gst_tag_list_quark;
387 GstTagMergeMode mode;
390 gst_tag_list_add_value_internal (GstStructure *list, GstTagMergeMode mode, GQuark tag, GValue *value)
392 GstTagInfo *info = gst_tag_lookup (tag);
393 const GValue *value2;
395 g_assert (info != NULL);
397 if (info->merge_func && (value2 = gst_structure_id_get_value (list, tag)) != NULL) {
398 GValue dest = { 0, };
400 case GST_TAG_MERGE_REPLACE_ALL:
401 case GST_TAG_MERGE_REPLACE:
402 gst_structure_id_set_value (list, tag, value);
404 case GST_TAG_MERGE_PREPEND:
405 gst_value_list_concat (&dest, value, value2);
406 gst_structure_id_set_value (list, tag, &dest);
407 g_value_unset (&dest);
409 case GST_TAG_MERGE_APPEND:
410 gst_value_list_concat (&dest, value2, value);
411 gst_structure_id_set_value (list, tag, &dest);
412 g_value_unset (&dest);
414 case GST_TAG_MERGE_KEEP:
415 case GST_TAG_MERGE_KEEP_ALL:
418 g_assert_not_reached ();
423 case GST_TAG_MERGE_APPEND:
424 case GST_TAG_MERGE_KEEP:
425 if (gst_structure_id_get_value (list, tag) != NULL)
428 case GST_TAG_MERGE_REPLACE_ALL:
429 case GST_TAG_MERGE_REPLACE:
430 case GST_TAG_MERGE_PREPEND:
431 gst_structure_id_set_value (list, tag, value);
433 case GST_TAG_MERGE_KEEP_ALL:
436 g_assert_not_reached ();
442 gst_tag_list_copy_foreach (GQuark tag, GValue *value, gpointer user_data)
444 GstTagCopyData *copy = (GstTagCopyData *) user_data;
446 gst_tag_list_add_value_internal (copy->list, copy->mode, tag, value);
451 * gst_tag_list_insert:
452 * @into: list to merge into
453 * @from: list to merge from
454 * @mode: the mode to use
456 * Inserts the tags of the second list into the first list using the given mode.
459 gst_tag_list_insert (GstTagList *into, const GstTagList *from, GstTagMergeMode mode)
463 g_return_if_fail (GST_IS_TAG_LIST (into));
464 g_return_if_fail (GST_IS_TAG_LIST (from));
465 g_return_if_fail (GST_TAG_MODE_IS_VALID (mode));
467 data.list = (GstStructure *) into;
469 if (mode == GST_TAG_MERGE_REPLACE_ALL) {
470 gst_structure_remove_all_fields (data.list);
472 gst_structure_foreach ((GstStructure *) from, gst_tag_list_copy_foreach, &data);
476 * @list: list to copy
478 * Copies a given #GstTagList.
480 * Returns: copy of the given list
483 gst_tag_list_copy (const GstTagList *list)
485 g_return_val_if_fail (GST_IS_TAG_LIST (list), NULL);
487 return GST_TAG_LIST (gst_structure_copy ((GstStructure *) list));
490 * gst_tag_list_merge:
491 * @list1: first list to merge
492 * @list2: second list to merge
493 * @mode: the mode to use
495 * Merges the two given lists into a new list. If one of the lists is NULL, a
496 * copy of the other is returned. If both lists are NULL, NULL is returned.
498 * Returns: the new list
501 gst_tag_list_merge (const GstTagList *list1, const GstTagList *list2, GstTagMergeMode mode)
503 g_return_val_if_fail (list1 == NULL || GST_IS_TAG_LIST (list1), NULL);
504 g_return_val_if_fail (list2 == NULL || GST_IS_TAG_LIST (list2), NULL);
505 g_return_val_if_fail (GST_TAG_MODE_IS_VALID (mode), NULL);
507 if (!list1 && !list2) {
510 return gst_tag_list_copy (list2);
512 return gst_tag_list_copy (list1);
516 ret = gst_tag_list_copy (list1);
517 gst_tag_list_insert (ret, list2, mode);
523 * @list: the list to free
525 * Frees the given list and all associated values.
528 gst_tag_list_free (GstTagList *list)
530 g_return_if_fail (GST_IS_TAG_LIST (list));
531 gst_structure_free ((GstStructure *) list);
534 * gst_tag_list_get_tag_size:
536 * @tag: the tag to query
538 * Checks how many value are stored in this tag list for the given tag.
540 * Returns: The number of tags stored
543 gst_tag_list_get_tag_size (const GstTagList *list, const gchar *tag)
547 g_return_val_if_fail (GST_IS_TAG_LIST (list), 0);
549 value = gst_structure_get_value ((GstStructure *) list, tag);
552 if (G_VALUE_TYPE (value) != GST_TYPE_LIST)
555 return gst_value_list_get_size (value);
559 * @list: list to set tags in
560 * @mode: the mode to use
562 * @...: values to set
564 * Sets the values for the given tags using the specified mode.
567 gst_tag_list_add (GstTagList *list, GstTagMergeMode mode, const gchar *tag, ...)
571 g_return_if_fail (GST_IS_TAG_LIST (list));
572 g_return_if_fail (GST_TAG_MODE_IS_VALID (mode));
573 g_return_if_fail (tag != NULL);
575 va_start (args, tag);
576 gst_tag_list_add_valist (list, mode, tag, args);
580 * gst_tag_list_add_values:
581 * @list: list to set tags in
582 * @mode: the mode to use
584 * @...: GValues to set
586 * Sets the GValues for the given tags using the specified mode.
589 gst_tag_list_add_values (GstTagList *list, GstTagMergeMode mode, const gchar *tag, ...)
593 g_return_if_fail (GST_IS_TAG_LIST (list));
594 g_return_if_fail (GST_TAG_MODE_IS_VALID (mode));
595 g_return_if_fail (tag != NULL);
597 va_start (args, tag);
598 gst_tag_list_add_valist_values (list, mode, tag, args);
602 * gst_tag_list_add_valist:
603 * @list: list to set tags in
604 * @mode: the mode to use
606 * @var_args: tag / value pairs to set
608 * Sets the values for the given tags using the specified mode.
611 gst_tag_list_add_valist (GstTagList *list, GstTagMergeMode mode, const gchar *tag, va_list var_args)
617 g_return_if_fail (GST_IS_TAG_LIST (list));
618 g_return_if_fail (GST_TAG_MODE_IS_VALID (mode));
619 g_return_if_fail (tag != NULL);
621 while (tag != NULL) {
622 GValue value = { 0, };
623 quark = g_quark_from_string (tag);
624 info = gst_tag_lookup (quark);
625 g_return_if_fail (info != NULL);
626 g_value_init (&value, info->type);
627 G_VALUE_COLLECT (&value, var_args, 0, &error);
629 g_warning ("%s: %s", G_STRLOC, error);
631 /* we purposely leak the value here, it might not be
632 * in a sane state if an error condition occoured
636 gst_tag_list_add_value_internal (list, mode, quark, &value);
637 g_value_unset (&value);
638 tag = va_arg (var_args, gchar *);
642 * gst_tag_list_add_valist_values:
643 * @list: list to set tags in
644 * @mode: the mode to use
646 * @var_args: tag / GValue pairs to set
648 * Sets the GValues for the given tags using the specified mode.
651 gst_tag_list_add_valist_values (GstTagList *list, GstTagMergeMode mode, const gchar *tag, va_list var_args)
656 g_return_if_fail (GST_IS_TAG_LIST (list));
657 g_return_if_fail (GST_TAG_MODE_IS_VALID (mode));
658 g_return_if_fail (tag != NULL);
660 while (tag != NULL) {
661 quark = g_quark_from_string (tag);
662 info = gst_tag_lookup (quark);
663 g_return_if_fail (info != NULL);
664 gst_tag_list_add_value_internal (list, mode, quark, va_arg (var_args, GValue *));
665 tag = va_arg (var_args, gchar *);
669 * gst_tag_list_remove_tag:
670 * @list: list to remove tag from
671 * @tag: tag to remove
673 * Removes the goven tag from the taglist.
676 gst_tag_list_remove_tag (GstTagList *list, const gchar *tag)
678 g_return_if_fail (GST_IS_TAG_LIST (list));
679 g_return_if_fail (tag != NULL);
681 gst_structure_remove_field ((GstStructure *) list, tag);
684 GstTagForeachFunc func;
685 GstTagList * tag_list;
689 structure_foreach_wrapper (GQuark field_id,
690 GValue *value, gpointer user_data)
692 TagForeachData *data = (TagForeachData *) user_data;
693 data->func (data->tag_list, g_quark_to_string (field_id), data->data);
697 * gst_tag_list_foreach:
698 * @list: list to iterate over
699 * @func: function to be called for each tag
700 * @user_data: user specified data
702 * Calls the given function for each tag inside the tag list. Note that if there
703 * is no tag, the function won't be called at all.
706 gst_tag_list_foreach (GstTagList *list, GstTagForeachFunc func, gpointer user_data)
710 g_return_if_fail (GST_IS_TAG_LIST (list));
711 g_return_if_fail (func != NULL);
714 data.tag_list = list;
715 data.data = user_data;
716 gst_structure_foreach ((GstStructure *) list, structure_foreach_wrapper, &data);
719 /***** tag events *****/
723 * @list: the tag list to put into the event or NULL for an empty list
725 * Creates a new tag event with the given list and takes ownership of it.
727 * Returns: a new tag event
730 gst_event_new_tag (GstTagList *list)
734 g_return_val_if_fail (list == NULL || GST_IS_TAG_LIST (list), NULL);
736 ret = gst_event_new (GST_EVENT_TAG);
738 list = gst_tag_list_new ();
739 ret->event_data.structure.structure = (GstStructure *) list;
744 * get_event_tag_get_list:
745 * @tag_event: a tagging #GstEvent
747 * Gets the taglist from a given tagging event.
749 * Returns: The #GstTagList of the event
752 gst_event_tag_get_list (GstEvent *tag_event)
754 g_return_val_if_fail (GST_IS_EVENT (tag_event), NULL);
755 g_return_val_if_fail (GST_EVENT_TYPE (tag_event) == GST_EVENT_TAG, NULL);
757 return GST_TAG_LIST (tag_event->event_data.structure.structure);
761 * gst_tag_list_get_value_index:
762 * @list: a #GStTagList
763 * @tag: tag to read out
764 * @index: number of entry to read out
766 * Gets the value that is at the given index for the given tag in the given
769 * Returns: The GValue for the specified entry or NULL if the tag wasn't available
770 * or the tag doesn't have as many entries
772 G_CONST_RETURN GValue *
773 gst_tag_list_get_value_index (const GstTagList *list, const gchar *tag, guint index)
777 g_return_val_if_fail (GST_IS_TAG_LIST (list), NULL);
778 g_return_val_if_fail (tag != NULL, NULL);
780 value = gst_structure_get_value ((GstStructure *) list, tag);
781 if (value == NULL) return NULL;
783 if (GST_VALUE_HOLDS_LIST (value)) {
784 if (index >= gst_value_list_get_size (value)) return NULL;
785 return gst_value_list_get_value (value, index);
787 if (index > 0) return NULL;
793 * gst_tag_list_copy_value:
794 * @dest: uninitialized #GValue to copy into
795 * @list: list to get the tag from
796 * @tag: tag to read out
798 * Copies the contents for the given tag into the value, merging multiple values
799 * into one if multiple values are associated with the tag.
800 * You must g_value_unset() the value after use.
802 * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
806 gst_tag_list_copy_value (GValue *dest, const GstTagList *list, const gchar *tag)
810 g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE);
811 g_return_val_if_fail (tag != NULL, FALSE);
812 g_return_val_if_fail (dest != NULL, FALSE);
813 g_return_val_if_fail (G_VALUE_TYPE (dest) == 0, FALSE);
815 src = gst_structure_get_value ((GstStructure *) list, tag);
816 if (!src) return FALSE;
818 if (G_VALUE_TYPE (src) == GST_TYPE_LIST) {
819 GstTagInfo *info = gst_tag_lookup (g_quark_from_string (tag));
820 /* must be there or lists aren't allowed */
821 g_assert (info->merge_func);
822 info->merge_func (dest, src);
824 g_value_init (dest, G_VALUE_TYPE (src));
825 g_value_copy (src, dest);
830 /***** evil macros to get all the gst_tag_list_get_*() functions right *****/
832 #define TAG_MERGE_FUNCS(name,type) \
834 gst_tag_list_get_ ## name (const GstTagList *list, const gchar *tag, \
839 g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE); \
840 g_return_val_if_fail (tag != NULL, FALSE); \
841 g_return_val_if_fail (value != NULL, FALSE); \
843 if (!gst_tag_list_copy_value (&v, list, tag)) \
845 *value = COPY_FUNC (g_value_get_ ## name (&v)); \
846 g_value_unset (&v); \
851 gst_tag_list_get_ ## name ## _index (const GstTagList *list, const gchar *tag, \
852 guint index, type *value) \
856 g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE); \
857 g_return_val_if_fail (tag != NULL, FALSE); \
858 g_return_val_if_fail (value != NULL, FALSE); \
860 if ((v = gst_tag_list_get_value_index (list, tag, index)) == NULL) \
862 *value = COPY_FUNC (g_value_get_ ## name (v)); \
866 #define COPY_FUNC /**/
867 TAG_MERGE_FUNCS (char, gchar)
868 TAG_MERGE_FUNCS (uchar, guchar)
869 TAG_MERGE_FUNCS (boolean, gboolean)
870 TAG_MERGE_FUNCS (int, gint)
871 TAG_MERGE_FUNCS (uint, guint)
872 TAG_MERGE_FUNCS (long, glong)
873 TAG_MERGE_FUNCS (ulong, gulong)
874 TAG_MERGE_FUNCS (int64, gint64)
875 TAG_MERGE_FUNCS (uint64, guint64)
876 TAG_MERGE_FUNCS (float, gfloat)
877 TAG_MERGE_FUNCS (double, gdouble)
880 #define COPY_FUNC g_strdup
881 TAG_MERGE_FUNCS (string, gchar *)