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"
27 #include "gst-i18n-lib.h"
32 #include <gobject/gvaluecollector.h>
35 #define GST_TAG_IS_VALID(tag) (gst_tag_get_info (tag) != NULL)
38 GType type; /* type the data is in */
40 gchar * nick; /* translated name */
41 gchar * blurb; /* translated description of type */
43 GstTagMergeFunc merge_func; /* functions to merge the values */
46 #define TAGLIST "taglist"
47 static GQuark gst_tag_list_quark;
48 static GMutex *__tag_mutex;
49 static GHashTable *__tags;
50 #define TAG_LOCK g_mutex_lock (__tag_mutex)
51 #define TAG_UNLOCK g_mutex_unlock (__tag_mutex)
54 _gst_tag_initialize (void)
56 gst_tag_list_quark = g_quark_from_static_string (TAGLIST);
57 __tag_mutex = g_mutex_new ();
58 __tags = g_hash_table_new (g_direct_hash, g_direct_equal);
59 gst_tag_register (GST_TAG_TITLE,
62 _("commonly used title"),
63 gst_tag_merge_strings_with_comma);
64 gst_tag_register (GST_TAG_ARTIST,
67 _("person(s) responsible for the recording"),
68 gst_tag_merge_strings_with_comma);
69 gst_tag_register (GST_TAG_ALBUM,
72 _("album containing this data"),
73 gst_tag_merge_strings_with_comma);
74 gst_tag_register (GST_TAG_DATE,
75 G_TYPE_UINT, /* FIXME: own data type for dates? */
77 _("date the data was created (in Julian calendar days)"),
79 gst_tag_register (GST_TAG_GENRE,
82 _("genre this data belongs to"),
83 gst_tag_merge_strings_with_comma);
84 gst_tag_register (GST_TAG_COMMENT,
87 _("free text commenting the data"),
88 gst_tag_merge_strings_with_comma);
89 gst_tag_register (GST_TAG_TRACK_NUMBER,
92 _("track number inside a collection"),
93 gst_tag_merge_use_first);
94 gst_tag_register (GST_TAG_TRACK_COUNT,
97 _("count of tracks inside collection this track belongs to"),
98 gst_tag_merge_use_first);
99 gst_tag_register (GST_TAG_LOCATION,
102 _("original location of file as a URI"),
103 gst_tag_merge_strings_with_comma);
104 gst_tag_register (GST_TAG_DESCRIPTION,
107 _("short text describing the content of the data"),
108 gst_tag_merge_strings_with_comma);
109 gst_tag_register (GST_TAG_VERSION,
112 _("version of this data"),
114 gst_tag_register (GST_TAG_ISRC,
117 _("International Standard Recording Code - see http://www.ifpi.org/isrc/"),
119 gst_tag_register (GST_TAG_ORGANIZATION,
122 _("organization"), /* FIXME */
123 gst_tag_merge_strings_with_comma);
124 gst_tag_register (GST_TAG_COPYRIGHT,
127 _("copyright notice of the data"),
129 gst_tag_register (GST_TAG_CONTACT,
132 _("contact information"),
133 gst_tag_merge_strings_with_comma);
134 gst_tag_register (GST_TAG_LICENSE,
137 _("license of data"),
139 gst_tag_register (GST_TAG_PERFORMER,
142 _("person(s) performing"),
143 gst_tag_merge_strings_with_comma);
144 gst_tag_register (GST_TAG_DURATION,
147 _("length in GStreamer time units (nanoseconds)"),
149 gst_tag_register (GST_TAG_CODEC,
152 _("codec the data is stored in"),
153 gst_tag_merge_strings_with_comma);
154 gst_tag_register (GST_TAG_MINIMUM_BITRATE,
156 _("minimum bitrate"),
157 _("minimum bitrate in bits/s"),
159 gst_tag_register (GST_TAG_BITRATE,
162 _("exact or average bitrate in bits/s"),
164 gst_tag_register (GST_TAG_MAXIMUM_BITRATE,
166 _("maximum bitrate"),
167 _("maximum bitrate in bits/s"),
171 * gst_tag_merge_use_first:
172 * @dest: uninitialized GValue to store result in
173 * @src: GValue to copy from
175 * This is a convenience function for the func argument of gst_tag_register().
176 * It creates a copy of the first value from the list.
179 gst_tag_merge_use_first (GValue *dest, const GValue *src)
181 const GValue *ret = gst_value_list_get_value (src, 0);
183 g_value_init (dest, G_VALUE_TYPE (ret));
184 g_value_copy (ret, dest);
187 * gst_tag_merge_strings_with_comma:
188 * @dest: uninitialized GValue to store result in
189 * @src: GValue to copy from
191 * This is a convenience function for the func argument of gst_tag_register().
192 * It concatenates all given strings using a comma. The tag must be registered
193 * as a G_TYPE_STRING or this function will fail.
196 gst_tag_merge_strings_with_comma (GValue *dest, const GValue *src)
201 count = gst_value_list_get_size (src);
202 str = g_string_new (g_value_get_string (gst_value_list_get_value (src, 0)));
203 for (i = 1; i < count; i++) {
204 /* seperator between two string */
205 str = g_string_append (str, _(", "));
206 str = g_string_append (str, g_value_get_string (gst_value_list_get_value (src, 1)));
209 g_value_init (dest, G_TYPE_STRING);
210 g_value_set_string_take_ownership (dest, str->str);
211 g_string_free (str, FALSE);
214 gst_tag_lookup (GQuark entry)
219 ret = g_hash_table_lookup (__tags, GUINT_TO_POINTER (entry));
226 * @name: the name or identifier string
227 * @type: the type this data is in
228 * @nick: human-readable name
229 * @blurb: a human-readable description about this tag
230 * @func: function for merging multiple values of this tag
232 * Registers a new tag type for the use with GStreamer's type system. If a type
233 * with that name is already registered, that one is used.
234 * The old registration may have used a different type however. So don't rely
235 * on youre supplied values.
236 * If you know the type is already registered, use gst_tag_lookup instead.
237 * This function takes ownership of all supplied variables.
240 gst_tag_register (gchar *name, GType type, gchar *nick, gchar *blurb,
241 GstTagMergeFunc func)
246 g_return_if_fail (name != NULL);
247 g_return_if_fail (nick != NULL);
248 g_return_if_fail (blurb != NULL);
249 g_return_if_fail (type != 0 && type != GST_TYPE_LIST);
251 key = g_quark_from_string (name);
252 info = gst_tag_lookup (key);
253 g_return_if_fail (info == NULL);
255 info = g_new (GstTagInfo, 1);
259 info->merge_func = func;
262 g_hash_table_insert (__tags, GUINT_TO_POINTER (key), info);
267 * @tag: name of the tag
269 * Checks if the given type is already registered.
271 * Returns: TRUE if the type is already registered
274 gst_tag_exists (const gchar *tag)
276 g_return_val_if_fail (tag != NULL, FALSE);
278 return gst_tag_lookup (g_quark_from_string (tag)) != NULL;
284 * Gets the #GType used for this tag.
286 * Returns: the #GType of this tag
289 gst_tag_get_type (const gchar *tag)
293 g_return_val_if_fail (tag != NULL, 0);
294 info = gst_tag_lookup (g_quark_from_string (tag));
295 g_return_val_if_fail (info != NULL, 0);
303 * Returns the human-readable name of this tag, You must not change or free
306 * Returns: the human-readable name of this tag
309 gst_tag_get_nick (const gchar *tag)
313 g_return_val_if_fail (tag != NULL, NULL);
314 info = gst_tag_lookup (g_quark_from_string (tag));
315 g_return_val_if_fail (info != NULL, NULL);
320 * gst_tag_get_description:
323 * Returns the human-readable description of this tag, You must not change or
326 * Return the human-readable description of this tag
329 gst_tag_get_description (const gchar *tag)
333 g_return_val_if_fail (tag != NULL, NULL);
334 info = gst_tag_lookup (g_quark_from_string (tag));
335 g_return_val_if_fail (info != NULL, NULL);
340 * gst_tag_list_is_fixed:
343 * Checks if the given tag is fixed. A fixed tag can only contain one value.
344 * Unfixed tags can contain lists of values.
346 * Returns: TRUE, if the given tag is fixed.
349 gst_tag_is_fixed (const gchar *tag)
353 g_return_val_if_fail (tag != NULL, FALSE);
354 info = gst_tag_lookup (g_quark_from_string (tag));
355 g_return_val_if_fail (info != NULL, FALSE);
357 return info->merge_func == NULL;
362 * Creates a new empty GstTagList.
364 * Returns: An empty tag list
367 gst_tag_list_new (void)
369 return GST_TAG_LIST (gst_structure_new (TAGLIST, NULL));
373 * @p: Object that might be a taglist
375 * Checks if the given pointer is a taglist.
377 * Returns: TRUE, if the given pointer is a taglist
380 gst_is_tag_list (gconstpointer p)
382 g_return_val_if_fail (p != NULL, FALSE);
384 return ((GstStructure *) p)->name == gst_tag_list_quark;
388 GstTagMergeMode mode;
391 gst_tag_list_add_value_internal (GstStructure *list, GstTagMergeMode mode, GQuark tag, GValue *value)
393 GstTagInfo *info = gst_tag_lookup (tag);
394 const GValue *value2;
396 g_assert (info != NULL);
398 if (info->merge_func && (value2 = gst_structure_id_get_value (list, tag)) != NULL) {
399 GValue dest = { 0, };
401 case GST_TAG_MERGE_REPLACE_ALL:
402 case GST_TAG_MERGE_REPLACE:
403 gst_structure_id_set_value (list, tag, value);
405 case GST_TAG_MERGE_PREPEND:
406 gst_value_list_concat (&dest, value, value2);
407 gst_structure_id_set_value (list, tag, &dest);
408 g_value_unset (&dest);
410 case GST_TAG_MERGE_APPEND:
411 gst_value_list_concat (&dest, value2, value);
412 gst_structure_id_set_value (list, tag, &dest);
413 g_value_unset (&dest);
415 case GST_TAG_MERGE_KEEP:
416 case GST_TAG_MERGE_KEEP_ALL:
419 g_assert_not_reached ();
424 case GST_TAG_MERGE_APPEND:
425 case GST_TAG_MERGE_KEEP:
426 if (gst_structure_id_get_value (list, tag) != NULL)
429 case GST_TAG_MERGE_REPLACE_ALL:
430 case GST_TAG_MERGE_REPLACE:
431 case GST_TAG_MERGE_PREPEND:
432 gst_structure_id_set_value (list, tag, value);
434 case GST_TAG_MERGE_KEEP_ALL:
437 g_assert_not_reached ();
443 gst_tag_list_copy_foreach (GQuark tag, GValue *value, gpointer user_data)
445 GstTagCopyData *copy = (GstTagCopyData *) user_data;
447 gst_tag_list_add_value_internal (copy->list, copy->mode, tag, value);
452 * gst_tag_list_insert:
453 * @into: list to merge into
454 * @from: list to merge from
455 * @mode: the mode to use
457 * Inserts the tags of the second list into the first list using the given mode.
460 gst_tag_list_insert (GstTagList *into, const GstTagList *from, GstTagMergeMode mode)
464 g_return_if_fail (GST_IS_TAG_LIST (into));
465 g_return_if_fail (GST_IS_TAG_LIST (from));
466 g_return_if_fail (GST_TAG_MODE_IS_VALID (mode));
468 data.list = (GstStructure *) into;
470 if (mode == GST_TAG_MERGE_REPLACE_ALL) {
471 gst_structure_remove_all_fields (data.list);
473 gst_structure_foreach ((GstStructure *) from, gst_tag_list_copy_foreach, &data);
477 * @list: list to copy
479 * Copies a given #GstTagList.
481 * Returns: copy of the given list
484 gst_tag_list_copy (const GstTagList *list)
486 g_return_val_if_fail (GST_IS_TAG_LIST (list), NULL);
488 return GST_TAG_LIST (gst_structure_copy ((GstStructure *) list));
491 * gst_tag_list_merge:
492 * @list1: first list to merge
493 * @list2: second list to merge
494 * @mode: the mode to use
496 * Merges the two given lists into a new list. If one of the lists is NULL, a
497 * copy of the other is returned. If both lists are NULL, NULL is returned.
499 * Returns: the new list
502 gst_tag_list_merge (const GstTagList *list1, const GstTagList *list2, GstTagMergeMode mode)
504 g_return_val_if_fail (list1 == NULL || GST_IS_TAG_LIST (list1), NULL);
505 g_return_val_if_fail (list2 == NULL || GST_IS_TAG_LIST (list2), NULL);
506 g_return_val_if_fail (GST_TAG_MODE_IS_VALID (mode), NULL);
508 if (!list1 && !list2) {
511 return gst_tag_list_copy (list2);
513 return gst_tag_list_copy (list1);
517 ret = gst_tag_list_copy (list1);
518 gst_tag_list_insert (ret, list2, mode);
524 * @list: the list to free
526 * Frees the given list and all associated values.
529 gst_tag_list_free (GstTagList *list)
531 g_return_if_fail (GST_IS_TAG_LIST (list));
532 gst_structure_free ((GstStructure *) list);
535 * gst_tag_list_get_tag_size:
537 * @tag: the tag to query
539 * Checks how many value are stored in this tag list for the given tag.
541 * Returns: The number of tags stored
544 gst_tag_list_get_tag_size (const GstTagList *list, const gchar *tag)
548 g_return_val_if_fail (GST_IS_TAG_LIST (list), 0);
550 value = gst_structure_get_value ((GstStructure *) list, tag);
553 if (G_VALUE_TYPE (value) != GST_TYPE_LIST)
556 return gst_value_list_get_size (value);
560 * @list: list to set tags in
561 * @mode: the mode to use
563 * @...: values to set
565 * Sets the values for the given tags using the specified mode.
568 gst_tag_list_add (GstTagList *list, GstTagMergeMode mode, const gchar *tag, ...)
572 g_return_if_fail (GST_IS_TAG_LIST (list));
573 g_return_if_fail (GST_TAG_MODE_IS_VALID (mode));
574 g_return_if_fail (tag != NULL);
576 va_start (args, tag);
577 gst_tag_list_add_valist (list, mode, tag, args);
581 * gst_tag_list_add_values:
582 * @list: list to set tags in
583 * @mode: the mode to use
585 * @...: GValues to set
587 * Sets the GValues for the given tags using the specified mode.
590 gst_tag_list_add_values (GstTagList *list, GstTagMergeMode mode, const gchar *tag, ...)
594 g_return_if_fail (GST_IS_TAG_LIST (list));
595 g_return_if_fail (GST_TAG_MODE_IS_VALID (mode));
596 g_return_if_fail (tag != NULL);
598 va_start (args, tag);
599 gst_tag_list_add_valist_values (list, mode, tag, args);
603 * gst_tag_list_add_valist:
604 * @list: list to set tags in
605 * @mode: the mode to use
607 * @var_args: tag / value pairs to set
609 * Sets the values for the given tags using the specified mode.
612 gst_tag_list_add_valist (GstTagList *list, GstTagMergeMode mode, const gchar *tag, va_list var_args)
618 g_return_if_fail (GST_IS_TAG_LIST (list));
619 g_return_if_fail (GST_TAG_MODE_IS_VALID (mode));
620 g_return_if_fail (tag != NULL);
622 while (tag != NULL) {
623 GValue value = { 0, };
624 quark = g_quark_from_string (tag);
625 info = gst_tag_lookup (quark);
626 g_return_if_fail (info != NULL);
627 g_value_init (&value, info->type);
628 G_VALUE_COLLECT (&value, var_args, 0, &error);
630 g_warning ("%s: %s", G_STRLOC, error);
632 /* we purposely leak the value here, it might not be
633 * in a sane state if an error condition occoured
637 gst_tag_list_add_value_internal (list, mode, quark, &value);
638 g_value_unset (&value);
639 tag = va_arg (var_args, gchar *);
643 * gst_tag_list_add_valist_values:
644 * @list: list to set tags in
645 * @mode: the mode to use
647 * @var_args: tag / GValue pairs to set
649 * Sets the GValues for the given tags using the specified mode.
652 gst_tag_list_add_valist_values (GstTagList *list, GstTagMergeMode mode, const gchar *tag, va_list var_args)
657 g_return_if_fail (GST_IS_TAG_LIST (list));
658 g_return_if_fail (GST_TAG_MODE_IS_VALID (mode));
659 g_return_if_fail (tag != NULL);
661 while (tag != NULL) {
662 quark = g_quark_from_string (tag);
663 info = gst_tag_lookup (quark);
664 g_return_if_fail (info != NULL);
665 gst_tag_list_add_value_internal (list, mode, quark, va_arg (var_args, GValue *));
666 tag = va_arg (var_args, gchar *);
670 * gst_tag_list_remove_tag:
671 * @list: list to remove tag from
672 * @tag: tag to remove
674 * Removes the goven tag from the taglist.
677 gst_tag_list_remove_tag (GstTagList *list, const gchar *tag)
679 g_return_if_fail (GST_IS_TAG_LIST (list));
680 g_return_if_fail (tag != NULL);
682 gst_structure_remove_field ((GstStructure *) list, tag);
685 GstTagForeachFunc func;
686 GstTagList * tag_list;
690 structure_foreach_wrapper (GQuark field_id,
691 GValue *value, gpointer user_data)
693 TagForeachData *data = (TagForeachData *) user_data;
694 data->func (data->tag_list, g_quark_to_string (field_id), data->data);
698 * gst_tag_list_foreach:
699 * @list: list to iterate over
700 * @func: function to be called for each tag
701 * @user_data: user specified data
703 * Calls the given function for each tag inside the tag list. Note that if there
704 * is no tag, the function won't be called at all.
707 gst_tag_list_foreach (GstTagList *list, GstTagForeachFunc func, gpointer user_data)
711 g_return_if_fail (GST_IS_TAG_LIST (list));
712 g_return_if_fail (func != NULL);
715 data.tag_list = list;
716 data.data = user_data;
717 gst_structure_foreach ((GstStructure *) list, structure_foreach_wrapper, &data);
720 /***** tag events *****/
724 * @list: the tag list to put into the event or NULL for an empty list
726 * Creates a new tag event with the given list and takes ownership of it.
728 * Returns: a new tag event
731 gst_event_new_tag (GstTagList *list)
735 g_return_val_if_fail (list == NULL || GST_IS_TAG_LIST (list), NULL);
737 ret = gst_event_new (GST_EVENT_TAG);
739 list = gst_tag_list_new ();
740 ret->event_data.structure.structure = (GstStructure *) list;
745 * get_event_tag_get_list:
746 * @tag_event: a tagging #GstEvent
748 * Gets the taglist from a given tagging event.
750 * Returns: The #GstTagList of the event
753 gst_event_tag_get_list (GstEvent *tag_event)
755 g_return_val_if_fail (GST_IS_EVENT (tag_event), NULL);
756 g_return_val_if_fail (GST_EVENT_TYPE (tag_event) == GST_EVENT_TAG, NULL);
758 return GST_TAG_LIST (tag_event->event_data.structure.structure);
762 * gst_tag_list_get_value_index:
763 * @list: a #GStTagList
764 * @tag: tag to read out
765 * @index: number of entry to read out
767 * Gets the value that is at the given index for the given tag in the given
770 * Returns: The GValue for the specified entry or NULL if the tag wasn't available
771 * or the tag doesn't have as many entries
773 G_CONST_RETURN GValue *
774 gst_tag_list_get_value_index (const GstTagList *list, const gchar *tag, guint index)
778 g_return_val_if_fail (GST_IS_TAG_LIST (list), NULL);
779 g_return_val_if_fail (tag != NULL, NULL);
781 value = gst_structure_get_value ((GstStructure *) list, tag);
782 if (value == NULL) return NULL;
784 if (GST_VALUE_HOLDS_LIST (value)) {
785 if (index >= gst_value_list_get_size (value)) return NULL;
786 return gst_value_list_get_value (value, index);
788 if (index > 0) return NULL;
794 * gst_tag_list_copy_value:
795 * @dest: uninitialized #GValue to copy into
796 * @list: list to get the tag from
797 * @tag: tag to read out
799 * Copies the contents for the given tag into the value, merging multiple values
800 * into one if multiple values are associated with the tag.
801 * You must g_value_unset() the value after use.
803 * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
807 gst_tag_list_copy_value (GValue *dest, const GstTagList *list, const gchar *tag)
811 g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE);
812 g_return_val_if_fail (tag != NULL, FALSE);
813 g_return_val_if_fail (dest != NULL, FALSE);
814 g_return_val_if_fail (G_VALUE_TYPE (dest) == 0, FALSE);
816 src = gst_structure_get_value ((GstStructure *) list, tag);
817 if (!src) return FALSE;
819 if (G_VALUE_TYPE (src) == GST_TYPE_LIST) {
820 GstTagInfo *info = gst_tag_lookup (g_quark_from_string (tag));
821 /* must be there or lists aren't allowed */
822 g_assert (info->merge_func);
823 info->merge_func (dest, src);
825 g_value_init (dest, G_VALUE_TYPE (src));
826 g_value_copy (src, dest);
831 /***** evil macros to get all the gst_tag_list_get_*() functions right *****/
833 #define TAG_MERGE_FUNCS(name,type) \
835 gst_tag_list_get_ ## name (const GstTagList *list, const gchar *tag, \
840 g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE); \
841 g_return_val_if_fail (tag != NULL, FALSE); \
842 g_return_val_if_fail (value != NULL, FALSE); \
844 if (!gst_tag_list_copy_value (&v, list, tag)) \
846 *value = COPY_FUNC (g_value_get_ ## name (&v)); \
847 g_value_unset (&v); \
852 gst_tag_list_get_ ## name ## _index (const GstTagList *list, const gchar *tag, \
853 guint index, type *value) \
857 g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE); \
858 g_return_val_if_fail (tag != NULL, FALSE); \
859 g_return_val_if_fail (value != NULL, FALSE); \
861 if ((v = gst_tag_list_get_value_index (list, tag, index)) == NULL) \
863 *value = COPY_FUNC (g_value_get_ ## name (v)); \
867 #define COPY_FUNC /**/
868 TAG_MERGE_FUNCS (char, gchar)
869 TAG_MERGE_FUNCS (uchar, guchar)
870 TAG_MERGE_FUNCS (boolean, gboolean)
871 TAG_MERGE_FUNCS (int, gint)
872 TAG_MERGE_FUNCS (uint, guint)
873 TAG_MERGE_FUNCS (long, glong)
874 TAG_MERGE_FUNCS (ulong, gulong)
875 TAG_MERGE_FUNCS (int64, gint64)
876 TAG_MERGE_FUNCS (uint64, guint64)
877 TAG_MERGE_FUNCS (float, gfloat)
878 TAG_MERGE_FUNCS (double, gdouble)
881 #define COPY_FUNC g_strdup
882 TAG_MERGE_FUNCS (string, gchar *)