ebefb15b85d1802fc84648a9ee36ef6f7181ff41
[platform/upstream/gstreamer.git] / gst / gsttaglist.c
1 /* GStreamer
2  * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
3  *
4  * gsttaglist.c: tag support (aka metadata)
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 /**
23  * SECTION:gsttaglist
24  * @short_description: List of tags and values used to describe media metadata
25  *
26  * List of tags and values used to describe media metadata.
27  *
28  * Strings must be in ASCII or UTF-8 encoding. No other encodings are allowed.
29  *
30  * Last reviewed on 2009-06-09 (0.10.23)
31  */
32
33 #ifdef HAVE_CONFIG_H
34 #  include "config.h"
35 #endif
36
37 #include "gst_private.h"
38 #include "math-compat.h"
39 #include "gst-i18n-lib.h"
40 #include "gsttaglist.h"
41 #include "gstinfo.h"
42 #include "gstvalue.h"
43 #include "gstbuffer.h"
44 #include "gstquark.h"
45 #include "gststructure.h"
46
47 #include <gobject/gvaluecollector.h>
48 #include <string.h>
49
50 /* FIXME: add category for tags */
51 #define GST_CAT_TAGS GST_CAT_DEFAULT
52
53 #define GST_TAG_IS_VALID(tag)           (gst_tag_get_info (tag) != NULL)
54
55 /* FIXME 0.11: make taglists refcounted maybe? */
56 /* a tag list is basically a structure, but we don't make this fact public */
57 typedef struct _GstTagListImpl
58 {
59   GstTagList taglist;
60
61   GstStructure *structure;
62 } GstTagListImpl;
63
64 #define GST_TAG_LIST_STRUCTURE(taglist)  ((GstTagListImpl*)(taglist))->structure
65
66
67 /* FIXME 0.11: use GParamSpecs or something similar for tag registrations,
68  * possibly even gst_tag_register(). Especially value ranges might be
69  * useful for some tags. */
70
71 typedef struct
72 {
73   GType type;                   /* type the data is in */
74
75   const gchar *nick;            /* translated short description */
76   const gchar *blurb;           /* translated long description  */
77
78   GstTagMergeFunc merge_func;   /* functions to merge the values */
79   GstTagFlag flag;              /* type of tag */
80   GQuark name_quark;            /* quark for the name */
81 }
82 GstTagInfo;
83
84 #define g_value_get_char g_value_get_schar
85
86 static GMutex __tag_mutex;
87 #define TAG_LOCK g_mutex_lock (&__tag_mutex)
88 #define TAG_UNLOCK g_mutex_unlock (&__tag_mutex)
89
90 /* tags hash table: maps tag name string => GstTagInfo */
91 static GHashTable *__tags;
92
93 GST_DEFINE_MINI_OBJECT_TYPE (GstTagList, gst_tag_list);
94
95 static void __gst_tag_list_free (GstTagList * list);
96 static GstTagList *__gst_tag_list_copy (const GstTagList * list);
97
98 /* FIXME: had code:
99  *    g_value_register_transform_func (_gst_tag_list_type, G_TYPE_STRING,
100  *      _gst_structure_transform_to_string);
101  */
102 void
103 _priv_gst_tag_initialize (void)
104 {
105   g_mutex_init (&__tag_mutex);
106
107   __tags = g_hash_table_new (g_str_hash, g_str_equal);
108   gst_tag_register_static (GST_TAG_TITLE, GST_TAG_FLAG_META,
109       G_TYPE_STRING,
110       _("title"), _("commonly used title"), gst_tag_merge_strings_with_comma);
111   gst_tag_register_static (GST_TAG_TITLE_SORTNAME, GST_TAG_FLAG_META,
112       G_TYPE_STRING,
113       _("title sortname"), _("commonly used title for sorting purposes"), NULL);
114   gst_tag_register_static (GST_TAG_ARTIST, GST_TAG_FLAG_META,
115       G_TYPE_STRING,
116       _("artist"),
117       _("person(s) responsible for the recording"),
118       gst_tag_merge_strings_with_comma);
119   gst_tag_register_static (GST_TAG_ARTIST_SORTNAME, GST_TAG_FLAG_META,
120       G_TYPE_STRING,
121       _("artist sortname"),
122       _("person(s) responsible for the recording for sorting purposes"), NULL);
123   gst_tag_register_static (GST_TAG_ALBUM, GST_TAG_FLAG_META,
124       G_TYPE_STRING,
125       _("album"),
126       _("album containing this data"), gst_tag_merge_strings_with_comma);
127   gst_tag_register_static (GST_TAG_ALBUM_SORTNAME, GST_TAG_FLAG_META,
128       G_TYPE_STRING,
129       _("album sortname"),
130       _("album containing this data for sorting purposes"), NULL);
131   gst_tag_register_static (GST_TAG_ALBUM_ARTIST, GST_TAG_FLAG_META,
132       G_TYPE_STRING,
133       _("album artist"),
134       _("The artist of the entire album, as it should be displayed"),
135       gst_tag_merge_strings_with_comma);
136   gst_tag_register_static (GST_TAG_ALBUM_ARTIST_SORTNAME, GST_TAG_FLAG_META,
137       G_TYPE_STRING,
138       _("album artist sortname"),
139       _("The artist of the entire album, as it should be sorted"), NULL);
140   gst_tag_register_static (GST_TAG_DATE, GST_TAG_FLAG_META, G_TYPE_DATE,
141       _("date"), _("date the data was created (as a GDate structure)"), NULL);
142   gst_tag_register_static (GST_TAG_DATE_TIME, GST_TAG_FLAG_META,
143       GST_TYPE_DATE_TIME, _("datetime"),
144       _("date and time the data was created (as a GstDateTime structure)"),
145       NULL);
146   gst_tag_register_static (GST_TAG_GENRE, GST_TAG_FLAG_META,
147       G_TYPE_STRING,
148       _("genre"),
149       _("genre this data belongs to"), gst_tag_merge_strings_with_comma);
150   gst_tag_register_static (GST_TAG_COMMENT, GST_TAG_FLAG_META,
151       G_TYPE_STRING,
152       _("comment"),
153       _("free text commenting the data"), gst_tag_merge_use_first);
154   gst_tag_register_static (GST_TAG_EXTENDED_COMMENT, GST_TAG_FLAG_META,
155       G_TYPE_STRING,
156       _("extended comment"),
157       _("free text commenting the data in key=value or key[en]=comment form"),
158       gst_tag_merge_use_first);
159   gst_tag_register_static (GST_TAG_TRACK_NUMBER, GST_TAG_FLAG_META,
160       G_TYPE_UINT,
161       _("track number"),
162       _("track number inside a collection"), gst_tag_merge_use_first);
163   gst_tag_register_static (GST_TAG_TRACK_COUNT, GST_TAG_FLAG_META,
164       G_TYPE_UINT,
165       _("track count"),
166       _("count of tracks inside collection this track belongs to"),
167       gst_tag_merge_use_first);
168   gst_tag_register_static (GST_TAG_ALBUM_VOLUME_NUMBER, GST_TAG_FLAG_META,
169       G_TYPE_UINT,
170       _("disc number"),
171       _("disc number inside a collection"), gst_tag_merge_use_first);
172   gst_tag_register_static (GST_TAG_ALBUM_VOLUME_COUNT, GST_TAG_FLAG_META,
173       G_TYPE_UINT,
174       _("disc count"),
175       _("count of discs inside collection this disc belongs to"),
176       gst_tag_merge_use_first);
177   gst_tag_register_static (GST_TAG_LOCATION, GST_TAG_FLAG_META,
178       G_TYPE_STRING,
179       _("location"), _("Origin of media as a URI (location, where the "
180           "original of the file or stream is hosted)"),
181       gst_tag_merge_strings_with_comma);
182   gst_tag_register_static (GST_TAG_HOMEPAGE, GST_TAG_FLAG_META,
183       G_TYPE_STRING,
184       _("homepage"),
185       _("Homepage for this media (i.e. artist or movie homepage)"),
186       gst_tag_merge_strings_with_comma);
187   gst_tag_register_static (GST_TAG_DESCRIPTION, GST_TAG_FLAG_META,
188       G_TYPE_STRING, _("description"),
189       _("short text describing the content of the data"),
190       gst_tag_merge_strings_with_comma);
191   gst_tag_register_static (GST_TAG_VERSION, GST_TAG_FLAG_META, G_TYPE_STRING,
192       _("version"), _("version of this data"), NULL);
193   gst_tag_register_static (GST_TAG_ISRC, GST_TAG_FLAG_META, G_TYPE_STRING,
194       _("ISRC"),
195       _
196       ("International Standard Recording Code - see http://www.ifpi.org/isrc/"),
197       NULL);
198   /* FIXME: organization (fix what? tpm) */
199   gst_tag_register_static (GST_TAG_ORGANIZATION, GST_TAG_FLAG_META,
200       G_TYPE_STRING, _("organization"), _("organization"),
201       gst_tag_merge_strings_with_comma);
202   gst_tag_register_static (GST_TAG_COPYRIGHT, GST_TAG_FLAG_META,
203       G_TYPE_STRING, _("copyright"), _("copyright notice of the data"), NULL);
204   gst_tag_register_static (GST_TAG_COPYRIGHT_URI, GST_TAG_FLAG_META,
205       G_TYPE_STRING, _("copyright uri"),
206       _("URI to the copyright notice of the data"), NULL);
207   gst_tag_register_static (GST_TAG_ENCODED_BY, GST_TAG_FLAG_META, G_TYPE_STRING,
208       _("encoded by"), _("name of the encoding person or organization"),
209       gst_tag_merge_strings_with_comma);
210   gst_tag_register_static (GST_TAG_CONTACT, GST_TAG_FLAG_META,
211       G_TYPE_STRING,
212       _("contact"), _("contact information"), gst_tag_merge_strings_with_comma);
213   gst_tag_register_static (GST_TAG_LICENSE, GST_TAG_FLAG_META,
214       G_TYPE_STRING, _("license"), _("license of data"), NULL);
215   gst_tag_register_static (GST_TAG_LICENSE_URI, GST_TAG_FLAG_META,
216       G_TYPE_STRING, _("license uri"),
217       _("URI to the license of the data"), NULL);
218   gst_tag_register_static (GST_TAG_PERFORMER, GST_TAG_FLAG_META,
219       G_TYPE_STRING,
220       _("performer"),
221       _("person(s) performing"), gst_tag_merge_strings_with_comma);
222   gst_tag_register_static (GST_TAG_COMPOSER, GST_TAG_FLAG_META,
223       G_TYPE_STRING,
224       _("composer"),
225       _("person(s) who composed the recording"),
226       gst_tag_merge_strings_with_comma);
227   gst_tag_register_static (GST_TAG_DURATION, GST_TAG_FLAG_DECODED,
228       G_TYPE_UINT64,
229       _("duration"), _("length in GStreamer time units (nanoseconds)"), NULL);
230   gst_tag_register_static (GST_TAG_CODEC, GST_TAG_FLAG_ENCODED,
231       G_TYPE_STRING,
232       _("codec"),
233       _("codec the data is stored in"), gst_tag_merge_strings_with_comma);
234   gst_tag_register_static (GST_TAG_VIDEO_CODEC, GST_TAG_FLAG_ENCODED,
235       G_TYPE_STRING,
236       _("video codec"), _("codec the video data is stored in"), NULL);
237   gst_tag_register_static (GST_TAG_AUDIO_CODEC, GST_TAG_FLAG_ENCODED,
238       G_TYPE_STRING,
239       _("audio codec"), _("codec the audio data is stored in"), NULL);
240   gst_tag_register_static (GST_TAG_SUBTITLE_CODEC, GST_TAG_FLAG_ENCODED,
241       G_TYPE_STRING,
242       _("subtitle codec"), _("codec the subtitle data is stored in"), NULL);
243   gst_tag_register_static (GST_TAG_CONTAINER_FORMAT, GST_TAG_FLAG_ENCODED,
244       G_TYPE_STRING, _("container format"),
245       _("container format the data is stored in"), NULL);
246   gst_tag_register_static (GST_TAG_BITRATE, GST_TAG_FLAG_ENCODED,
247       G_TYPE_UINT, _("bitrate"), _("exact or average bitrate in bits/s"), NULL);
248   gst_tag_register_static (GST_TAG_NOMINAL_BITRATE, GST_TAG_FLAG_ENCODED,
249       G_TYPE_UINT, _("nominal bitrate"), _("nominal bitrate in bits/s"), NULL);
250   gst_tag_register_static (GST_TAG_MINIMUM_BITRATE, GST_TAG_FLAG_ENCODED,
251       G_TYPE_UINT, _("minimum bitrate"), _("minimum bitrate in bits/s"), NULL);
252   gst_tag_register_static (GST_TAG_MAXIMUM_BITRATE, GST_TAG_FLAG_ENCODED,
253       G_TYPE_UINT, _("maximum bitrate"), _("maximum bitrate in bits/s"), NULL);
254   gst_tag_register_static (GST_TAG_ENCODER, GST_TAG_FLAG_ENCODED,
255       G_TYPE_STRING,
256       _("encoder"), _("encoder used to encode this stream"), NULL);
257   gst_tag_register_static (GST_TAG_ENCODER_VERSION, GST_TAG_FLAG_ENCODED,
258       G_TYPE_UINT,
259       _("encoder version"),
260       _("version of the encoder used to encode this stream"), NULL);
261   gst_tag_register_static (GST_TAG_SERIAL, GST_TAG_FLAG_ENCODED,
262       G_TYPE_UINT, _("serial"), _("serial number of track"), NULL);
263   gst_tag_register_static (GST_TAG_TRACK_GAIN, GST_TAG_FLAG_META,
264       G_TYPE_DOUBLE, _("replaygain track gain"), _("track gain in db"), NULL);
265   gst_tag_register_static (GST_TAG_TRACK_PEAK, GST_TAG_FLAG_META,
266       G_TYPE_DOUBLE, _("replaygain track peak"), _("peak of the track"), NULL);
267   gst_tag_register_static (GST_TAG_ALBUM_GAIN, GST_TAG_FLAG_META,
268       G_TYPE_DOUBLE, _("replaygain album gain"), _("album gain in db"), NULL);
269   gst_tag_register_static (GST_TAG_ALBUM_PEAK, GST_TAG_FLAG_META,
270       G_TYPE_DOUBLE, _("replaygain album peak"), _("peak of the album"), NULL);
271   gst_tag_register_static (GST_TAG_REFERENCE_LEVEL, GST_TAG_FLAG_META,
272       G_TYPE_DOUBLE, _("replaygain reference level"),
273       _("reference level of track and album gain values"), NULL);
274   gst_tag_register_static (GST_TAG_LANGUAGE_CODE, GST_TAG_FLAG_META,
275       G_TYPE_STRING, _("language code"),
276       _("language code for this stream, conforming to ISO-639-1 or ISO-639-2"),
277       NULL);
278   gst_tag_register_static (GST_TAG_LANGUAGE_NAME, GST_TAG_FLAG_META,
279       G_TYPE_STRING, _("language name"),
280       _("freeform name of the language this stream is in"), NULL);
281   gst_tag_register_static (GST_TAG_IMAGE, GST_TAG_FLAG_META, GST_TYPE_SAMPLE,
282       _("image"), _("image related to this stream"), gst_tag_merge_use_first);
283   gst_tag_register_static (GST_TAG_PREVIEW_IMAGE, GST_TAG_FLAG_META,
284       GST_TYPE_SAMPLE,
285       /* TRANSLATORS: 'preview image' = image that shows a preview of the full image */
286       _("preview image"), _("preview image related to this stream"), NULL);
287   gst_tag_register_static (GST_TAG_ATTACHMENT, GST_TAG_FLAG_META,
288       GST_TYPE_SAMPLE, _("attachment"), _("file attached to this stream"),
289       gst_tag_merge_use_first);
290   gst_tag_register_static (GST_TAG_BEATS_PER_MINUTE, GST_TAG_FLAG_META,
291       G_TYPE_DOUBLE, _("beats per minute"),
292       _("number of beats per minute in audio"), NULL);
293   gst_tag_register_static (GST_TAG_KEYWORDS, GST_TAG_FLAG_META, G_TYPE_STRING,
294       _("keywords"), _("comma separated keywords describing the content"),
295       gst_tag_merge_strings_with_comma);
296   gst_tag_register_static (GST_TAG_GEO_LOCATION_NAME, GST_TAG_FLAG_META,
297       G_TYPE_STRING, _("geo location name"),
298       _("human readable descriptive location of where "
299           "the media has been recorded or produced"), NULL);
300   gst_tag_register_static (GST_TAG_GEO_LOCATION_LATITUDE, GST_TAG_FLAG_META,
301       G_TYPE_DOUBLE, _("geo location latitude"),
302       _("geo latitude location of where the media has been recorded or "
303           "produced in degrees according to WGS84 (zero at the equator, "
304           "negative values for southern latitudes)"), NULL);
305   gst_tag_register_static (GST_TAG_GEO_LOCATION_LONGITUDE, GST_TAG_FLAG_META,
306       G_TYPE_DOUBLE, _("geo location longitude"),
307       _("geo longitude location of where the media has been recorded or "
308           "produced in degrees according to WGS84 (zero at the prime meridian "
309           "in Greenwich/UK,  negative values for western longitudes)"), NULL);
310   gst_tag_register_static (GST_TAG_GEO_LOCATION_ELEVATION, GST_TAG_FLAG_META,
311       G_TYPE_DOUBLE, _("geo location elevation"),
312       _("geo elevation of where the media has been recorded or produced in "
313           "meters according to WGS84 (zero is average sea level)"), NULL);
314   gst_tag_register_static (GST_TAG_GEO_LOCATION_COUNTRY, GST_TAG_FLAG_META,
315       G_TYPE_STRING, _("geo location country"),
316       _("country (english name) where the media has been recorded "
317           "or produced"), NULL);
318   gst_tag_register_static (GST_TAG_GEO_LOCATION_CITY, GST_TAG_FLAG_META,
319       G_TYPE_STRING, _("geo location city"),
320       _("city (english name) where the media has been recorded "
321           "or produced"), NULL);
322   gst_tag_register_static (GST_TAG_GEO_LOCATION_SUBLOCATION, GST_TAG_FLAG_META,
323       G_TYPE_STRING, _("geo location sublocation"),
324       _("a location whithin a city where the media has been produced "
325           "or created (e.g. the neighborhood)"), NULL);
326   gst_tag_register_static (GST_TAG_GEO_LOCATION_HORIZONTAL_ERROR,
327       GST_TAG_FLAG_META, G_TYPE_DOUBLE, _("geo location horizontal error"),
328       _("expected error of the horizontal positioning measures (in meters)"),
329       NULL);
330   gst_tag_register_static (GST_TAG_GEO_LOCATION_MOVEMENT_SPEED,
331       GST_TAG_FLAG_META, G_TYPE_DOUBLE, _("geo location movement speed"),
332       _("movement speed of the capturing device while performing the capture "
333           "in m/s"), NULL);
334   gst_tag_register_static (GST_TAG_GEO_LOCATION_MOVEMENT_DIRECTION,
335       GST_TAG_FLAG_META, G_TYPE_DOUBLE, _("geo location movement direction"),
336       _("indicates the movement direction of the device performing the capture"
337           " of a media. It is represented as degrees in floating point "
338           "representation, 0 means the geographic north, and increases "
339           "clockwise"), NULL);
340   gst_tag_register_static (GST_TAG_GEO_LOCATION_CAPTURE_DIRECTION,
341       GST_TAG_FLAG_META, G_TYPE_DOUBLE, _("geo location capture direction"),
342       _("indicates the direction the device is pointing to when capturing "
343           " a media. It is represented as degrees in floating point "
344           " representation, 0 means the geographic north, and increases "
345           "clockwise"), NULL);
346   gst_tag_register_static (GST_TAG_SHOW_NAME, GST_TAG_FLAG_META, G_TYPE_STRING,
347       /* TRANSLATORS: 'show name' = 'TV/radio/podcast show name' here */
348       _("show name"),
349       _("Name of the tv/podcast/series show the media is from"),
350       gst_tag_merge_strings_with_comma);
351   gst_tag_register_static (GST_TAG_SHOW_SORTNAME, GST_TAG_FLAG_META,
352       G_TYPE_STRING,
353       /* TRANSLATORS: 'show sortname' = 'TV/radio/podcast show name as used for sorting purposes' here */
354       _("show sortname"),
355       _("Name of the tv/podcast/series show the media is from, for sorting "
356           "purposes"), NULL);
357   gst_tag_register_static (GST_TAG_SHOW_EPISODE_NUMBER, GST_TAG_FLAG_META,
358       G_TYPE_UINT, _("episode number"),
359       _("The episode number in the season the media is part of"),
360       gst_tag_merge_use_first);
361   gst_tag_register_static (GST_TAG_SHOW_SEASON_NUMBER, GST_TAG_FLAG_META,
362       G_TYPE_UINT, _("season number"),
363       _("The season number of the show the media is part of"),
364       gst_tag_merge_use_first);
365   gst_tag_register_static (GST_TAG_LYRICS, GST_TAG_FLAG_META, G_TYPE_STRING,
366       _("lyrics"), _("The lyrics of the media, commonly used for songs"),
367       gst_tag_merge_strings_with_comma);
368   gst_tag_register_static (GST_TAG_COMPOSER_SORTNAME, GST_TAG_FLAG_META,
369       G_TYPE_STRING, _("composer sortname"),
370       _("person(s) who composed the recording, for sorting purposes"), NULL);
371   gst_tag_register_static (GST_TAG_GROUPING, GST_TAG_FLAG_META, G_TYPE_STRING,
372       _("grouping"),
373       _("Groups related media that spans multiple tracks, like the different "
374           "pieces of a concerto. It is a higher level than a track, "
375           "but lower than an album"), NULL);
376   gst_tag_register_static (GST_TAG_USER_RATING, GST_TAG_FLAG_META, G_TYPE_UINT,
377       _("user rating"),
378       _("Rating attributed by a user. The higher the rank, "
379           "the more the user likes this media"), NULL);
380   gst_tag_register_static (GST_TAG_DEVICE_MANUFACTURER, GST_TAG_FLAG_META,
381       G_TYPE_STRING, _("device manufacturer"),
382       _("Manufacturer of the device used to create this media"), NULL);
383   gst_tag_register_static (GST_TAG_DEVICE_MODEL, GST_TAG_FLAG_META,
384       G_TYPE_STRING, _("device model"),
385       _("Model of the device used to create this media"), NULL);
386   gst_tag_register_static (GST_TAG_APPLICATION_NAME, GST_TAG_FLAG_META,
387       G_TYPE_STRING, _("application name"),
388       _("Application used to create the media"), NULL);
389   gst_tag_register_static (GST_TAG_APPLICATION_DATA, GST_TAG_FLAG_META,
390       GST_TYPE_BUFFER, _("application data"),
391       _("Arbitrary application data to be serialized into the media"), NULL);
392   gst_tag_register_static (GST_TAG_IMAGE_ORIENTATION, GST_TAG_FLAG_META,
393       G_TYPE_STRING, _("image orientation"),
394       _("How the image should be rotated or flipped before display"), NULL);
395 }
396
397 /**
398  * gst_tag_merge_use_first:
399  * @dest: (out caller-allocates): uninitialized GValue to store result in
400  * @src: GValue to copy from
401  *
402  * This is a convenience function for the func argument of gst_tag_register().
403  * It creates a copy of the first value from the list.
404  */
405 void
406 gst_tag_merge_use_first (GValue * dest, const GValue * src)
407 {
408   const GValue *ret = gst_value_list_get_value (src, 0);
409
410   g_value_init (dest, G_VALUE_TYPE (ret));
411   g_value_copy (ret, dest);
412 }
413
414 /**
415  * gst_tag_merge_strings_with_comma:
416  * @dest: (out caller-allocates): uninitialized GValue to store result in
417  * @src: GValue to copy from
418  *
419  * This is a convenience function for the func argument of gst_tag_register().
420  * It concatenates all given strings using a comma. The tag must be registered
421  * as a G_TYPE_STRING or this function will fail.
422  */
423 void
424 gst_tag_merge_strings_with_comma (GValue * dest, const GValue * src)
425 {
426   GString *str;
427   gint i, count;
428
429   count = gst_value_list_get_size (src);
430   str = g_string_new (g_value_get_string (gst_value_list_get_value (src, 0)));
431   for (i = 1; i < count; i++) {
432     /* separator between two strings */
433     g_string_append (str, _(", "));
434     g_string_append (str,
435         g_value_get_string (gst_value_list_get_value (src, i)));
436   }
437
438   g_value_init (dest, G_TYPE_STRING);
439   g_value_take_string (dest, str->str);
440   g_string_free (str, FALSE);
441 }
442
443 static GstTagInfo *
444 gst_tag_lookup (const gchar * tag_name)
445 {
446   GstTagInfo *ret;
447
448   TAG_LOCK;
449   ret = g_hash_table_lookup (__tags, (gpointer) tag_name);
450   TAG_UNLOCK;
451
452   return ret;
453 }
454
455 /**
456  * gst_tag_register:
457  * @name: the name or identifier string
458  * @flag: a flag describing the type of tag info
459  * @type: the type this data is in
460  * @nick: human-readable name
461  * @blurb: a human-readable description about this tag
462  * @func: function for merging multiple values of this tag, or NULL
463  *
464  * Registers a new tag type for the use with GStreamer's type system. If a type
465  * with that name is already registered, that one is used.
466  * The old registration may have used a different type however. So don't rely
467  * on your supplied values.
468  *
469  * Important: if you do not supply a merge function the implication will be
470  * that there can only be one single value for this tag in a tag list and
471  * any additional values will silenty be discarded when being added (unless
472  * #GST_TAG_MERGE_REPLACE, #GST_TAG_MERGE_REPLACE_ALL, or
473  * #GST_TAG_MERGE_PREPEND is used as merge mode, in which case the new
474  * value will replace the old one in the list).
475  *
476  * The merge function will be called from gst_tag_list_copy_value() when
477  * it is required that one or more values for a tag be condensed into
478  * one single value. This may happen from gst_tag_list_get_string(),
479  * gst_tag_list_get_int(), gst_tag_list_get_double() etc. What will happen
480  * exactly in that case depends on how the tag was registered and if a
481  * merge function was supplied and if so which one.
482  *
483  * Two default merge functions are provided: gst_tag_merge_use_first() and
484  * gst_tag_merge_strings_with_comma().
485  */
486 void
487 gst_tag_register (const gchar * name, GstTagFlag flag, GType type,
488     const gchar * nick, const gchar * blurb, GstTagMergeFunc func)
489 {
490   g_return_if_fail (name != NULL);
491   g_return_if_fail (nick != NULL);
492   g_return_if_fail (blurb != NULL);
493   g_return_if_fail (type != 0 && type != GST_TYPE_LIST);
494
495   return gst_tag_register_static (g_intern_string (name), flag, type,
496       g_intern_string (nick), g_intern_string (blurb), func);
497 }
498
499 /**
500  * gst_tag_register_static:
501  * @name: the name or identifier string (string constant)
502  * @flag: a flag describing the type of tag info
503  * @type: the type this data is in
504  * @nick: human-readable name or short description (string constant)
505  * @blurb: a human-readable description for this tag (string constant)
506  * @func: function for merging multiple values of this tag, or NULL
507  *
508  * Registers a new tag type for the use with GStreamer's type system.
509  *
510  * Same as gst_tag_register(), but @name, @nick, and @blurb must be
511  * static strings or inlined strings, as they will not be copied. (GStreamer
512  * plugins will be made resident once loaded, so this function can be used
513  * even from dynamically loaded plugins.)
514  */
515 void
516 gst_tag_register_static (const gchar * name, GstTagFlag flag, GType type,
517     const gchar * nick, const gchar * blurb, GstTagMergeFunc func)
518 {
519   GstTagInfo *info;
520
521   g_return_if_fail (name != NULL);
522   g_return_if_fail (nick != NULL);
523   g_return_if_fail (blurb != NULL);
524   g_return_if_fail (type != 0 && type != GST_TYPE_LIST);
525
526   info = gst_tag_lookup (name);
527
528   if (info) {
529     g_return_if_fail (info->type == type);
530     return;
531   }
532
533   info = g_slice_new (GstTagInfo);
534   info->flag = flag;
535   info->type = type;
536   info->name_quark = g_quark_from_static_string (name);
537   info->nick = nick;
538   info->blurb = blurb;
539   info->merge_func = func;
540
541   TAG_LOCK;
542   g_hash_table_insert (__tags, (gpointer) name, info);
543   TAG_UNLOCK;
544 }
545
546 /**
547  * gst_tag_exists:
548  * @tag: name of the tag
549  *
550  * Checks if the given type is already registered.
551  *
552  * Returns: TRUE if the type is already registered
553  */
554 gboolean
555 gst_tag_exists (const gchar * tag)
556 {
557   g_return_val_if_fail (tag != NULL, FALSE);
558
559   return gst_tag_lookup (tag) != NULL;
560 }
561
562 /**
563  * gst_tag_get_type:
564  * @tag: the tag
565  *
566  * Gets the #GType used for this tag.
567  *
568  * Returns: the #GType of this tag
569  */
570 GType
571 gst_tag_get_type (const gchar * tag)
572 {
573   GstTagInfo *info;
574
575   g_return_val_if_fail (tag != NULL, 0);
576   info = gst_tag_lookup (tag);
577   g_return_val_if_fail (info != NULL, 0);
578
579   return info->type;
580 }
581
582 /**
583  * gst_tag_get_nick:
584  * @tag: the tag
585  *
586  * Returns the human-readable name of this tag, You must not change or free
587  * this string.
588  *
589  * Returns: the human-readable name of this tag
590  */
591 const gchar *
592 gst_tag_get_nick (const gchar * tag)
593 {
594   GstTagInfo *info;
595
596   g_return_val_if_fail (tag != NULL, NULL);
597   info = gst_tag_lookup (tag);
598   g_return_val_if_fail (info != NULL, NULL);
599
600   return info->nick;
601 }
602
603 /**
604  * gst_tag_get_description:
605  * @tag: the tag
606  *
607  * Returns the human-readable description of this tag, You must not change or
608  * free this string.
609  *
610  * Returns: the human-readable description of this tag
611  */
612 const gchar *
613 gst_tag_get_description (const gchar * tag)
614 {
615   GstTagInfo *info;
616
617   g_return_val_if_fail (tag != NULL, NULL);
618   info = gst_tag_lookup (tag);
619   g_return_val_if_fail (info != NULL, NULL);
620
621   return info->blurb;
622 }
623
624 /**
625  * gst_tag_get_flag:
626  * @tag: the tag
627  *
628  * Gets the flag of @tag.
629  *
630  * Returns: the flag of this tag.
631  */
632 GstTagFlag
633 gst_tag_get_flag (const gchar * tag)
634 {
635   GstTagInfo *info;
636
637   g_return_val_if_fail (tag != NULL, GST_TAG_FLAG_UNDEFINED);
638   info = gst_tag_lookup (tag);
639   g_return_val_if_fail (info != NULL, GST_TAG_FLAG_UNDEFINED);
640
641   return info->flag;
642 }
643
644 /**
645  * gst_tag_is_fixed:
646  * @tag: tag to check
647  *
648  * Checks if the given tag is fixed. A fixed tag can only contain one value.
649  * Unfixed tags can contain lists of values.
650  *
651  * Returns: TRUE, if the given tag is fixed.
652  */
653 gboolean
654 gst_tag_is_fixed (const gchar * tag)
655 {
656   GstTagInfo *info;
657
658   g_return_val_if_fail (tag != NULL, FALSE);
659   info = gst_tag_lookup (tag);
660   g_return_val_if_fail (info != NULL, FALSE);
661
662   return info->merge_func == NULL;
663 }
664
665 static void
666 gst_tag_list_init (GstTagList * taglist)
667 {
668   gst_mini_object_init (GST_MINI_OBJECT_CAST (taglist),
669       gst_tag_list_get_type ());
670
671   taglist->mini_object.copy = (GstMiniObjectCopyFunction) __gst_tag_list_copy;
672   taglist->mini_object.dispose = NULL;
673   taglist->mini_object.free = (GstMiniObjectFreeFunction) __gst_tag_list_free;
674 }
675
676 /* takes ownership of the structure */
677 static GstTagList *
678 gst_tag_list_new_internal (GstStructure * s)
679 {
680   GstTagList *tag_list;
681
682   g_assert (s != NULL);
683
684   tag_list = (GstTagList *) g_slice_new (GstTagListImpl);
685
686   gst_tag_list_init (tag_list);
687
688   GST_TAG_LIST_STRUCTURE (tag_list) = s;
689
690 #ifdef DEBUG_REFCOUNT
691   GST_CAT_TRACE (GST_CAT_TAGS, "created taglist %p", tag_list);
692 #endif
693
694   return tag_list;
695 }
696
697 static void
698 __gst_tag_list_free (GstTagList * list)
699 {
700   g_return_if_fail (GST_IS_TAG_LIST (list));
701
702 #ifdef DEBUG_REFCOUNT
703   GST_CAT_TRACE (GST_CAT_TAGS, "freeing caps %p", list);
704 #endif
705
706   gst_structure_free (GST_TAG_LIST_STRUCTURE (list));
707
708   g_slice_free1 (sizeof (GstTagListImpl), list);
709 }
710
711 static GstTagList *
712 __gst_tag_list_copy (const GstTagList * list)
713 {
714   const GstStructure *s;
715
716   g_return_val_if_fail (GST_IS_TAG_LIST (list), NULL);
717
718   s = GST_TAG_LIST_STRUCTURE (list);
719   return gst_tag_list_new_internal (gst_structure_copy (s));
720 }
721
722 /**
723  * gst_tag_list_new_empty:
724  *
725  * Creates a new empty GstTagList.
726  *
727  * Free-function: gst_tag_list_unref
728  *
729  * Returns: (transfer full): An empty tag list
730  */
731 GstTagList *
732 gst_tag_list_new_empty (void)
733 {
734   GstStructure *s;
735   GstTagList *tag_list;
736
737   s = gst_structure_new_id_empty (GST_QUARK (TAGLIST));
738   tag_list = gst_tag_list_new_internal (s);
739   return tag_list;
740 }
741
742 /**
743  * gst_tag_list_new:
744  * @tag: tag
745  * @...: NULL-terminated list of values to set
746  *
747  * Creates a new taglist and appends the values for the given tags. It expects
748  * tag-value pairs like gst_tag_list_add(), and a NULL terminator after the
749  * last pair. The type of the values is implicit and is documented in the API
750  * reference, but can also be queried at runtime with gst_tag_get_type(). It
751  * is an error to pass a value of a type not matching the tag type into this
752  * function. The tag list will make copies of any arguments passed
753  * (e.g. strings, buffers).
754  *
755  * Free-function: gst_tag_list_unref
756  *
757  * Returns: (transfer full): a new #GstTagList. Free with gst_tag_list_unref()
758  *     when no longer needed.
759  *
760  * Since: 0.10.24
761  */
762 GstTagList *
763 gst_tag_list_new (const gchar * tag, ...)
764 {
765   GstTagList *list;
766   va_list args;
767
768   g_return_val_if_fail (tag != NULL, NULL);
769
770   list = gst_tag_list_new_empty ();
771   va_start (args, tag);
772   gst_tag_list_add_valist (list, GST_TAG_MERGE_APPEND, tag, args);
773   va_end (args);
774
775   return list;
776 }
777
778 /**
779  * gst_tag_list_new_valist:
780  * @var_args: tag / value pairs to set
781  *
782  * Just like gst_tag_list_new(), only that it takes a va_list argument.
783  * Useful mostly for language bindings.
784  *
785  * Free-function: gst_tag_list_unref
786  *
787  * Returns: (transfer full): a new #GstTagList. Free with gst_tag_list_unref()
788  *     when no longer needed.
789  *
790  * Since: 0.10.24
791  */
792 GstTagList *
793 gst_tag_list_new_valist (va_list var_args)
794 {
795   GstTagList *list;
796   const gchar *tag;
797
798   list = gst_tag_list_new_empty ();
799
800   tag = va_arg (var_args, gchar *);
801   gst_tag_list_add_valist (list, GST_TAG_MERGE_APPEND, tag, var_args);
802
803   return list;
804 }
805
806 /**
807  * gst_tag_list_to_string:
808  * @list: a #GstTagList
809  *
810  * Serializes a tag list to a string.
811  *
812  * Returns: a newly-allocated string, or NULL in case of an error. The
813  *    string must be freed with g_free() when no longer needed.
814  *
815  * Since: 0.10.36
816  */
817 gchar *
818 gst_tag_list_to_string (const GstTagList * list)
819 {
820   g_return_val_if_fail (GST_IS_TAG_LIST (list), NULL);
821
822   return gst_structure_to_string (GST_TAG_LIST_STRUCTURE (list));
823 }
824
825 /**
826  * gst_tag_list_new_from_string:
827  * @str: a string created with gst_tag_list_to_string()
828  *
829  * Deserializes a tag list.
830  *
831  * Returns: a new #GstTagList, or NULL in case of an error.
832  *
833  * Since: 0.10.36
834  */
835 GstTagList *
836 gst_tag_list_new_from_string (const gchar * str)
837 {
838   GstTagList *tag_list;
839
840   g_return_val_if_fail (str != NULL, NULL);
841   g_return_val_if_fail (g_str_has_prefix (str, "taglist"), NULL);
842
843   tag_list = gst_tag_list_new_internal (gst_structure_from_string (str, NULL));
844   return tag_list;
845 }
846
847 /**
848  * gst_tag_list_n_tags:
849  * @list: A #GstTagList.
850  *
851  * Get the number of tags in @list.
852  *
853  * Returns: The number of tags in @list.
854  */
855 gint
856 gst_tag_list_n_tags (const GstTagList * list)
857 {
858   g_return_val_if_fail (list != NULL, 0);
859   g_return_val_if_fail (GST_IS_TAG_LIST (list), 0);
860
861   return gst_structure_n_fields (GST_TAG_LIST_STRUCTURE (list));
862 }
863
864 /**
865  * gst_tag_list_nth_tag_name:
866  * @list: A #GstTagList.
867  * @index: the index
868  *
869  * Get the name of the tag in @list at @index.
870  *
871  * Returns: The name of the tag at @index.
872  */
873 const gchar *
874 gst_tag_list_nth_tag_name (const GstTagList * list, guint index)
875 {
876   g_return_val_if_fail (list != NULL, 0);
877   g_return_val_if_fail (GST_IS_TAG_LIST (list), 0);
878
879   return gst_structure_nth_field_name (GST_TAG_LIST_STRUCTURE (list), index);
880 }
881
882 /**
883  * gst_tag_list_is_empty:
884  * @list: A #GstTagList.
885  *
886  * Checks if the given taglist is empty.
887  *
888  * Returns: TRUE if the taglist is empty, otherwise FALSE.
889  *
890  * Since: 0.10.11
891  */
892 gboolean
893 gst_tag_list_is_empty (const GstTagList * list)
894 {
895   g_return_val_if_fail (list != NULL, FALSE);
896   g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE);
897
898   return (gst_structure_n_fields (GST_TAG_LIST_STRUCTURE (list)) == 0);
899 }
900
901 static gboolean
902 gst_tag_list_fields_equal (const GValue * value1, const GValue * value2)
903 {
904   gdouble d1, d2;
905
906   if (gst_value_compare (value1, value2) == GST_VALUE_EQUAL)
907     return TRUE;
908
909   /* fields not equal: add some tolerance for doubles, otherwise bail out */
910   if (!G_VALUE_HOLDS_DOUBLE (value1) || !G_VALUE_HOLDS_DOUBLE (value2))
911     return FALSE;
912
913   d1 = g_value_get_double (value1);
914   d2 = g_value_get_double (value2);
915
916   /* This will only work for 'normal' values and values around 0,
917    * which should be good enough for our purposes here
918    * FIXME: maybe add this to gst_value_compare_double() ? */
919   return (fabs (d1 - d2) < 0.0000001);
920 }
921
922 /**
923  * gst_tag_list_is_equal:
924  * @list1: a #GstTagList.
925  * @list2: a #GstTagList.
926  *
927  * Checks if the two given taglists are equal.
928  *
929  * Returns: TRUE if the taglists are equal, otherwise FALSE
930  *
931  * Since: 0.10.36
932  */
933 gboolean
934 gst_tag_list_is_equal (const GstTagList * list1, const GstTagList * list2)
935 {
936   const GstStructure *s1, *s2;
937   gint num_fields1, num_fields2, i;
938
939   g_return_val_if_fail (GST_IS_TAG_LIST (list1), FALSE);
940   g_return_val_if_fail (GST_IS_TAG_LIST (list2), FALSE);
941
942   /* we don't just use gst_structure_is_equal() here so we can add some
943    * tolerance for doubles, though maybe we should just add that to
944    * gst_value_compare_double() as well? */
945   s1 = GST_TAG_LIST_STRUCTURE (list1);
946   s2 = GST_TAG_LIST_STRUCTURE (list2);
947
948   num_fields1 = gst_structure_n_fields (s1);
949   num_fields2 = gst_structure_n_fields (s2);
950
951   if (num_fields1 != num_fields2)
952     return FALSE;
953
954   for (i = 0; i < num_fields1; i++) {
955     const GValue *value1, *value2;
956     const gchar *tag_name;
957
958     tag_name = gst_structure_nth_field_name (s1, i);
959     value1 = gst_structure_get_value (s1, tag_name);
960     value2 = gst_structure_get_value (s2, tag_name);
961
962     if (value2 == NULL)
963       return FALSE;
964
965     if (!gst_tag_list_fields_equal (value1, value2))
966       return FALSE;
967   }
968
969   return TRUE;
970 }
971
972 typedef struct
973 {
974   GstTagList *list;
975   GstTagMergeMode mode;
976 }
977 GstTagCopyData;
978
979 static void
980 gst_tag_list_add_value_internal (GstTagList * tag_list, GstTagMergeMode mode,
981     const gchar * tag, const GValue * value, GstTagInfo * info)
982 {
983   GstStructure *list = GST_TAG_LIST_STRUCTURE (tag_list);
984   const GValue *value2;
985   GQuark tag_quark;
986
987   if (info == NULL) {
988     info = gst_tag_lookup (tag);
989     if (G_UNLIKELY (info == NULL)) {
990       g_warning ("unknown tag '%s'", tag);
991       return;
992     }
993   }
994
995   tag_quark = info->name_quark;
996
997   if (info->merge_func
998       && (value2 = gst_structure_id_get_value (list, tag_quark)) != NULL) {
999     GValue dest = { 0, };
1000
1001     switch (mode) {
1002       case GST_TAG_MERGE_REPLACE_ALL:
1003       case GST_TAG_MERGE_REPLACE:
1004         gst_structure_id_set_value (list, tag_quark, value);
1005         break;
1006       case GST_TAG_MERGE_PREPEND:
1007         gst_value_list_merge (&dest, value, value2);
1008         gst_structure_id_set_value (list, tag_quark, &dest);
1009         g_value_unset (&dest);
1010         break;
1011       case GST_TAG_MERGE_APPEND:
1012         gst_value_list_merge (&dest, value2, value);
1013         gst_structure_id_set_value (list, tag_quark, &dest);
1014         g_value_unset (&dest);
1015         break;
1016       case GST_TAG_MERGE_KEEP:
1017       case GST_TAG_MERGE_KEEP_ALL:
1018         break;
1019       default:
1020         g_assert_not_reached ();
1021         break;
1022     }
1023   } else {
1024     switch (mode) {
1025       case GST_TAG_MERGE_APPEND:
1026       case GST_TAG_MERGE_KEEP:
1027         if (gst_structure_id_get_value (list, tag_quark) != NULL)
1028           break;
1029         /* fall through */
1030       case GST_TAG_MERGE_REPLACE_ALL:
1031       case GST_TAG_MERGE_REPLACE:
1032       case GST_TAG_MERGE_PREPEND:
1033         gst_structure_id_set_value (list, tag_quark, value);
1034         break;
1035       case GST_TAG_MERGE_KEEP_ALL:
1036         break;
1037       default:
1038         g_assert_not_reached ();
1039         break;
1040     }
1041   }
1042 }
1043
1044 static gboolean
1045 gst_tag_list_copy_foreach (GQuark tag_quark, const GValue * value,
1046     gpointer user_data)
1047 {
1048   GstTagCopyData *copy = (GstTagCopyData *) user_data;
1049   const gchar *tag;
1050
1051   tag = g_quark_to_string (tag_quark);
1052   gst_tag_list_add_value_internal (copy->list, copy->mode, tag, value, NULL);
1053
1054   return TRUE;
1055 }
1056
1057 /**
1058  * gst_tag_list_insert:
1059  * @into: list to merge into
1060  * @from: list to merge from
1061  * @mode: the mode to use
1062  *
1063  * Inserts the tags of the @from list into the first list using the given mode.
1064  */
1065 void
1066 gst_tag_list_insert (GstTagList * into, const GstTagList * from,
1067     GstTagMergeMode mode)
1068 {
1069   GstTagCopyData data;
1070
1071   g_return_if_fail (GST_IS_TAG_LIST (into));
1072   g_return_if_fail (gst_tag_list_is_writable (into));
1073   g_return_if_fail (GST_IS_TAG_LIST (from));
1074   g_return_if_fail (GST_TAG_MODE_IS_VALID (mode));
1075
1076   data.list = into;
1077   data.mode = mode;
1078   if (mode == GST_TAG_MERGE_REPLACE_ALL) {
1079     gst_structure_remove_all_fields (GST_TAG_LIST_STRUCTURE (into));
1080   }
1081   gst_structure_foreach (GST_TAG_LIST_STRUCTURE (from),
1082       gst_tag_list_copy_foreach, &data);
1083 }
1084
1085 /**
1086  * gst_tag_list_merge:
1087  * @list1: first list to merge
1088  * @list2: second list to merge
1089  * @mode: the mode to use
1090  *
1091  * Merges the two given lists into a new list. If one of the lists is NULL, a
1092  * copy of the other is returned. If both lists are NULL, NULL is returned.
1093  *
1094  * Free-function: gst_tag_list_unref
1095  *
1096  * Returns: (transfer full): the new list
1097  */
1098 GstTagList *
1099 gst_tag_list_merge (const GstTagList * list1, const GstTagList * list2,
1100     GstTagMergeMode mode)
1101 {
1102   GstTagList *list1_cp;
1103   const GstTagList *list2_cp;
1104
1105   g_return_val_if_fail (list1 == NULL || GST_IS_TAG_LIST (list1), NULL);
1106   g_return_val_if_fail (list2 == NULL || GST_IS_TAG_LIST (list2), NULL);
1107   g_return_val_if_fail (GST_TAG_MODE_IS_VALID (mode), NULL);
1108
1109   /* nothing to merge */
1110   if (!list1 && !list2) {
1111     return NULL;
1112   }
1113
1114   /* create empty list, we need to do this to correctly handling merge modes */
1115   list1_cp = (list1) ? gst_tag_list_copy (list1) : gst_tag_list_new_empty ();
1116   list2_cp = (list2) ? list2 : gst_tag_list_new_empty ();
1117
1118   gst_tag_list_insert (list1_cp, list2_cp, mode);
1119
1120   if (!list2)
1121     gst_tag_list_unref ((GstTagList *) list2_cp);
1122
1123   return list1_cp;
1124 }
1125
1126 /**
1127  * gst_tag_list_get_tag_size:
1128  * @list: a taglist
1129  * @tag: the tag to query
1130  *
1131  * Checks how many value are stored in this tag list for the given tag.
1132  *
1133  * Returns: The number of tags stored
1134  */
1135 guint
1136 gst_tag_list_get_tag_size (const GstTagList * list, const gchar * tag)
1137 {
1138   const GValue *value;
1139
1140   g_return_val_if_fail (GST_IS_TAG_LIST (list), 0);
1141
1142   value = gst_structure_get_value (GST_TAG_LIST_STRUCTURE (list), tag);
1143   if (value == NULL)
1144     return 0;
1145   if (G_VALUE_TYPE (value) != GST_TYPE_LIST)
1146     return 1;
1147
1148   return gst_value_list_get_size (value);
1149 }
1150
1151 /**
1152  * gst_tag_list_add:
1153  * @list: list to set tags in
1154  * @mode: the mode to use
1155  * @tag: tag
1156  * @...: NULL-terminated list of values to set
1157  *
1158  * Sets the values for the given tags using the specified mode.
1159  */
1160 void
1161 gst_tag_list_add (GstTagList * list, GstTagMergeMode mode, const gchar * tag,
1162     ...)
1163 {
1164   va_list args;
1165
1166   g_return_if_fail (GST_IS_TAG_LIST (list));
1167   g_return_if_fail (gst_tag_list_is_writable (list));
1168   g_return_if_fail (GST_TAG_MODE_IS_VALID (mode));
1169   g_return_if_fail (tag != NULL);
1170
1171   va_start (args, tag);
1172   gst_tag_list_add_valist (list, mode, tag, args);
1173   va_end (args);
1174 }
1175
1176 /**
1177  * gst_tag_list_add_values:
1178  * @list: list to set tags in
1179  * @mode: the mode to use
1180  * @tag: tag
1181  * @...: GValues to set
1182  *
1183  * Sets the GValues for the given tags using the specified mode.
1184  */
1185 void
1186 gst_tag_list_add_values (GstTagList * list, GstTagMergeMode mode,
1187     const gchar * tag, ...)
1188 {
1189   va_list args;
1190
1191   g_return_if_fail (GST_IS_TAG_LIST (list));
1192   g_return_if_fail (gst_tag_list_is_writable (list));
1193   g_return_if_fail (GST_TAG_MODE_IS_VALID (mode));
1194   g_return_if_fail (tag != NULL);
1195
1196   va_start (args, tag);
1197   gst_tag_list_add_valist_values (list, mode, tag, args);
1198   va_end (args);
1199 }
1200
1201 /**
1202  * gst_tag_list_add_valist:
1203  * @list: list to set tags in
1204  * @mode: the mode to use
1205  * @tag: tag
1206  * @var_args: tag / value pairs to set
1207  *
1208  * Sets the values for the given tags using the specified mode.
1209  */
1210 void
1211 gst_tag_list_add_valist (GstTagList * list, GstTagMergeMode mode,
1212     const gchar * tag, va_list var_args)
1213 {
1214   GstTagInfo *info;
1215   gchar *error = NULL;
1216
1217   g_return_if_fail (GST_IS_TAG_LIST (list));
1218   g_return_if_fail (gst_tag_list_is_writable (list));
1219   g_return_if_fail (GST_TAG_MODE_IS_VALID (mode));
1220   g_return_if_fail (tag != NULL);
1221
1222   if (mode == GST_TAG_MERGE_REPLACE_ALL) {
1223     gst_structure_remove_all_fields (GST_TAG_LIST_STRUCTURE (list));
1224   }
1225
1226   while (tag != NULL) {
1227     GValue value = { 0, };
1228
1229     info = gst_tag_lookup (tag);
1230     if (G_UNLIKELY (info == NULL)) {
1231       g_warning ("unknown tag '%s'", tag);
1232       return;
1233     }
1234     G_VALUE_COLLECT_INIT (&value, info->type, var_args, 0, &error);
1235     if (error) {
1236       g_warning ("%s: %s", G_STRLOC, error);
1237       g_free (error);
1238       /* we purposely leak the value here, it might not be
1239        * in a sane state if an error condition occoured
1240        */
1241       return;
1242     }
1243     gst_tag_list_add_value_internal (list, mode, tag, &value, info);
1244     g_value_unset (&value);
1245     tag = va_arg (var_args, gchar *);
1246   }
1247 }
1248
1249 /**
1250  * gst_tag_list_add_valist_values:
1251  * @list: list to set tags in
1252  * @mode: the mode to use
1253  * @tag: tag
1254  * @var_args: tag / GValue pairs to set
1255  *
1256  * Sets the GValues for the given tags using the specified mode.
1257  */
1258 void
1259 gst_tag_list_add_valist_values (GstTagList * list, GstTagMergeMode mode,
1260     const gchar * tag, va_list var_args)
1261 {
1262   g_return_if_fail (GST_IS_TAG_LIST (list));
1263   g_return_if_fail (gst_tag_list_is_writable (list));
1264   g_return_if_fail (GST_TAG_MODE_IS_VALID (mode));
1265   g_return_if_fail (tag != NULL);
1266
1267   if (mode == GST_TAG_MERGE_REPLACE_ALL) {
1268     gst_structure_remove_all_fields (GST_TAG_LIST_STRUCTURE (list));
1269   }
1270
1271   while (tag != NULL) {
1272     GstTagInfo *info;
1273
1274     info = gst_tag_lookup (tag);
1275     if (G_UNLIKELY (info == NULL)) {
1276       g_warning ("unknown tag '%s'", tag);
1277       return;
1278     }
1279     gst_tag_list_add_value_internal (list, mode, tag, va_arg (var_args,
1280             GValue *), info);
1281     tag = va_arg (var_args, gchar *);
1282   }
1283 }
1284
1285 /**
1286  * gst_tag_list_add_value:
1287  * @list: list to set tags in
1288  * @mode: the mode to use
1289  * @tag: tag
1290  * @value: GValue for this tag
1291  *
1292  * Sets the GValue for a given tag using the specified mode.
1293  *
1294  * Since: 0.10.24
1295  */
1296 void
1297 gst_tag_list_add_value (GstTagList * list, GstTagMergeMode mode,
1298     const gchar * tag, const GValue * value)
1299 {
1300   g_return_if_fail (GST_IS_TAG_LIST (list));
1301   g_return_if_fail (gst_tag_list_is_writable (list));
1302   g_return_if_fail (GST_TAG_MODE_IS_VALID (mode));
1303   g_return_if_fail (tag != NULL);
1304
1305   gst_tag_list_add_value_internal (list, mode, tag, value, NULL);
1306 }
1307
1308 /**
1309  * gst_tag_list_remove_tag:
1310  * @list: list to remove tag from
1311  * @tag: tag to remove
1312  *
1313  * Removes the given tag from the taglist.
1314  */
1315 void
1316 gst_tag_list_remove_tag (GstTagList * list, const gchar * tag)
1317 {
1318   g_return_if_fail (GST_IS_TAG_LIST (list));
1319   g_return_if_fail (tag != NULL);
1320
1321   gst_structure_remove_field (GST_TAG_LIST_STRUCTURE (list), tag);
1322 }
1323
1324 typedef struct
1325 {
1326   GstTagForeachFunc func;
1327   const GstTagList *tag_list;
1328   gpointer data;
1329 }
1330 TagForeachData;
1331
1332 static int
1333 structure_foreach_wrapper (GQuark field_id, const GValue * value,
1334     gpointer user_data)
1335 {
1336   TagForeachData *data = (TagForeachData *) user_data;
1337
1338   data->func (data->tag_list, g_quark_to_string (field_id), data->data);
1339   return TRUE;
1340 }
1341
1342 /**
1343  * gst_tag_list_foreach:
1344  * @list: list to iterate over
1345  * @func: (scope call): function to be called for each tag
1346  * @user_data: (closure): user specified data
1347  *
1348  * Calls the given function for each tag inside the tag list. Note that if there
1349  * is no tag, the function won't be called at all.
1350  */
1351 void
1352 gst_tag_list_foreach (const GstTagList * list, GstTagForeachFunc func,
1353     gpointer user_data)
1354 {
1355   TagForeachData data;
1356
1357   g_return_if_fail (GST_IS_TAG_LIST (list));
1358   g_return_if_fail (func != NULL);
1359
1360   data.func = func;
1361   data.tag_list = list;
1362   data.data = user_data;
1363   gst_structure_foreach (GST_TAG_LIST_STRUCTURE (list),
1364       structure_foreach_wrapper, &data);
1365 }
1366
1367 /**
1368  * gst_tag_list_get_value_index:
1369  * @list: a #GstTagList
1370  * @tag: tag to read out
1371  * @index: number of entry to read out
1372  *
1373  * Gets the value that is at the given index for the given tag in the given
1374  * list.
1375  *
1376  * Returns: (transfer none): The GValue for the specified entry or NULL if the
1377  *          tag wasn't available or the tag doesn't have as many entries
1378  */
1379 const GValue *
1380 gst_tag_list_get_value_index (const GstTagList * list, const gchar * tag,
1381     guint index)
1382 {
1383   const GValue *value;
1384
1385   g_return_val_if_fail (GST_IS_TAG_LIST (list), NULL);
1386   g_return_val_if_fail (tag != NULL, NULL);
1387
1388   value = gst_structure_get_value (GST_TAG_LIST_STRUCTURE (list), tag);
1389   if (value == NULL)
1390     return NULL;
1391
1392   if (GST_VALUE_HOLDS_LIST (value)) {
1393     if (index >= gst_value_list_get_size (value))
1394       return NULL;
1395     return gst_value_list_get_value (value, index);
1396   } else {
1397     if (index > 0)
1398       return NULL;
1399     return value;
1400   }
1401 }
1402
1403 /**
1404  * gst_tag_list_copy_value:
1405  * @dest: (out caller-allocates): uninitialized #GValue to copy into
1406  * @list: list to get the tag from
1407  * @tag: tag to read out
1408  *
1409  * Copies the contents for the given tag into the value,
1410  * merging multiple values into one if multiple values are associated
1411  * with the tag.
1412  * You must g_value_unset() the value after use.
1413  *
1414  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1415  *          given list.
1416  */
1417 gboolean
1418 gst_tag_list_copy_value (GValue * dest, const GstTagList * list,
1419     const gchar * tag)
1420 {
1421   const GValue *src;
1422
1423   g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE);
1424   g_return_val_if_fail (tag != NULL, FALSE);
1425   g_return_val_if_fail (dest != NULL, FALSE);
1426   g_return_val_if_fail (G_VALUE_TYPE (dest) == 0, FALSE);
1427
1428   src = gst_structure_get_value (GST_TAG_LIST_STRUCTURE (list), tag);
1429   if (!src)
1430     return FALSE;
1431
1432   if (G_VALUE_TYPE (src) == GST_TYPE_LIST) {
1433     GstTagInfo *info = gst_tag_lookup (tag);
1434
1435     if (!info)
1436       return FALSE;
1437
1438     /* must be there or lists aren't allowed */
1439     g_assert (info->merge_func);
1440     info->merge_func (dest, src);
1441   } else {
1442     g_value_init (dest, G_VALUE_TYPE (src));
1443     g_value_copy (src, dest);
1444   }
1445   return TRUE;
1446 }
1447
1448 /* FIXME 0.11: this whole merge function business is overdesigned, and the
1449  * _get_foo() API is misleading as well - how many application developers will
1450  * expect gst_tag_list_get_string (list, GST_TAG_ARTIST, &val) might return a
1451  * string with multiple comma-separated artists? _get_foo() should just be
1452  * a convenience wrapper around _get_foo_index (list, tag, 0, &val),
1453  * supplemented by a special _tag_list_get_string_merged() function if needed
1454  * (unless someone can actually think of real use cases where the merge
1455  * function is not 'use first' for non-strings and merge for strings) */
1456
1457 /***** evil macros to get all the gst_tag_list_get_*() functions right *****/
1458
1459 #define TAG_MERGE_FUNCS(name,type,ret)                                  \
1460 gboolean                                                                \
1461 gst_tag_list_get_ ## name (const GstTagList *list, const gchar *tag,    \
1462                            type *value)                                 \
1463 {                                                                       \
1464   GValue v = { 0, };                                                    \
1465                                                                         \
1466   g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE);                 \
1467   g_return_val_if_fail (tag != NULL, FALSE);                            \
1468   g_return_val_if_fail (value != NULL, FALSE);                          \
1469                                                                         \
1470   if (!gst_tag_list_copy_value (&v, list, tag))                         \
1471       return FALSE;                                                     \
1472   *value = COPY_FUNC (g_value_get_ ## name (&v));                       \
1473   g_value_unset (&v);                                                   \
1474   return ret;                                                           \
1475 }                                                                       \
1476                                                                         \
1477 gboolean                                                                \
1478 gst_tag_list_get_ ## name ## _index (const GstTagList *list,            \
1479                                      const gchar *tag,                  \
1480                                      guint index, type *value)          \
1481 {                                                                       \
1482   const GValue *v;                                                      \
1483                                                                         \
1484   g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE);                 \
1485   g_return_val_if_fail (tag != NULL, FALSE);                            \
1486   g_return_val_if_fail (value != NULL, FALSE);                          \
1487                                                                         \
1488   if ((v = gst_tag_list_get_value_index (list, tag, index)) == NULL)    \
1489       return FALSE;                                                     \
1490   *value = COPY_FUNC (g_value_get_ ## name (v));                        \
1491   return ret;                                                           \
1492 }
1493
1494 #define COPY_FUNC /**/
1495 /**
1496  * gst_tag_list_get_boolean:
1497  * @list: a #GstTagList to get the tag from
1498  * @tag: tag to read out
1499  * @value: (out): location for the result
1500  *
1501  * Copies the contents for the given tag into the value, merging multiple values
1502  * into one if multiple values are associated with the tag.
1503  *
1504  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1505  *              given list.
1506  */
1507 /**
1508  * gst_tag_list_get_boolean_index:
1509  * @list: a #GstTagList to get the tag from
1510  * @tag: tag to read out
1511  * @index: number of entry to read out
1512  * @value: (out): location for the result
1513  *
1514  * Gets the value that is at the given index for the given tag in the given
1515  * list.
1516  *
1517  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1518  *              given list.
1519  */
1520 TAG_MERGE_FUNCS (boolean, gboolean, TRUE);
1521 /**
1522  * gst_tag_list_get_int:
1523  * @list: a #GstTagList to get the tag from
1524  * @tag: tag to read out
1525  * @value: (out): location for the result
1526  *
1527  * Copies the contents for the given tag into the value, merging multiple values
1528  * into one if multiple values are associated with the tag.
1529  *
1530  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1531  *              given list.
1532  */
1533 /**
1534  * gst_tag_list_get_int_index:
1535  * @list: a #GstTagList to get the tag from
1536  * @tag: tag to read out
1537  * @index: number of entry to read out
1538  * @value: (out): location for the result
1539  *
1540  * Gets the value that is at the given index for the given tag in the given
1541  * list.
1542  *
1543  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1544  *              given list.
1545  */
1546 TAG_MERGE_FUNCS (int, gint, TRUE);
1547 /**
1548  * gst_tag_list_get_uint:
1549  * @list: a #GstTagList to get the tag from
1550  * @tag: tag to read out
1551  * @value: (out): location for the result
1552  *
1553  * Copies the contents for the given tag into the value, merging multiple values
1554  * into one if multiple values are associated with the tag.
1555  *
1556  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1557  *              given list.
1558  */
1559 /**
1560  * gst_tag_list_get_uint_index:
1561  * @list: a #GstTagList to get the tag from
1562  * @tag: tag to read out
1563  * @index: number of entry to read out
1564  * @value: (out): location for the result
1565  *
1566  * Gets the value that is at the given index for the given tag in the given
1567  * list.
1568  *
1569  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1570  *              given list.
1571  */
1572 TAG_MERGE_FUNCS (uint, guint, TRUE);
1573 /**
1574  * gst_tag_list_get_int64_index:
1575  * @list: a #GstTagList to get the tag from
1576  * @tag: tag to read out
1577  * @index: number of entry to read out
1578  * @value: (out): location for the result
1579  *
1580  * Gets the value that is at the given index for the given tag in the given
1581  * list.
1582  *
1583  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1584  *              given list.
1585  */
1586 TAG_MERGE_FUNCS (int64, gint64, TRUE);
1587 /**
1588  * gst_tag_list_get_uint64:
1589  * @list: a #GstTagList to get the tag from
1590  * @tag: tag to read out
1591  * @value: (out): location for the result
1592  *
1593  * Copies the contents for the given tag into the value, merging multiple values
1594  * into one if multiple values are associated with the tag.
1595  *
1596  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1597  *              given list.
1598  */
1599 /**
1600  * gst_tag_list_get_uint64_index:
1601  * @list: a #GstTagList to get the tag from
1602  * @tag: tag to read out
1603  * @index: number of entry to read out
1604  * @value: (out): location for the result
1605  *
1606  * Gets the value that is at the given index for the given tag in the given
1607  * list.
1608  *
1609  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1610  *              given list.
1611  */
1612 TAG_MERGE_FUNCS (uint64, guint64, TRUE);
1613 /**
1614  * gst_tag_list_get_float:
1615  * @list: a #GstTagList to get the tag from
1616  * @tag: tag to read out
1617  * @value: (out): location for the result
1618  *
1619  * Copies the contents for the given tag into the value, merging multiple values
1620  * into one if multiple values are associated with the tag.
1621  *
1622  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1623  *              given list.
1624  */
1625 /**
1626  * gst_tag_list_get_float_index:
1627  * @list: a #GstTagList to get the tag from
1628  * @tag: tag to read out
1629  * @index: number of entry to read out
1630  * @value: (out): location for the result
1631  *
1632  * Gets the value that is at the given index for the given tag in the given
1633  * list.
1634  *
1635  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1636  *              given list.
1637  */
1638 TAG_MERGE_FUNCS (float, gfloat, TRUE);
1639 /**
1640  * gst_tag_list_get_double:
1641  * @list: a #GstTagList to get the tag from
1642  * @tag: tag to read out
1643  * @value: (out): location for the result
1644  *
1645  * Copies the contents for the given tag into the value, merging multiple values
1646  * into one if multiple values are associated with the tag.
1647  *
1648  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1649  *              given list.
1650  */
1651 /**
1652  * gst_tag_list_get_double_index:
1653  * @list: a #GstTagList to get the tag from
1654  * @tag: tag to read out
1655  * @index: number of entry to read out
1656  * @value: (out): location for the result
1657  *
1658  * Gets the value that is at the given index for the given tag in the given
1659  * list.
1660  *
1661  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1662  *              given list.
1663  */
1664 TAG_MERGE_FUNCS (double, gdouble, TRUE);
1665 /**
1666  * gst_tag_list_get_pointer:
1667  * @list: a #GstTagList to get the tag from
1668  * @tag: tag to read out
1669  * @value: (out) (transfer none): location for the result
1670  *
1671  * Copies the contents for the given tag into the value, merging multiple values
1672  * into one if multiple values are associated with the tag.
1673  *
1674  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1675  *              given list.
1676  */
1677 /**
1678  * gst_tag_list_get_pointer_index:
1679  * @list: a #GstTagList to get the tag from
1680  * @tag: tag to read out
1681  * @index: number of entry to read out
1682  * @value: (out) (transfer none): location for the result
1683  *
1684  * Gets the value that is at the given index for the given tag in the given
1685  * list.
1686  *
1687  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1688  *              given list.
1689  */
1690 TAG_MERGE_FUNCS (pointer, gpointer, (*value != NULL));
1691
1692 static inline gchar *
1693 _gst_strdup0 (const gchar * s)
1694 {
1695   if (s == NULL || *s == '\0')
1696     return NULL;
1697
1698   return g_strdup (s);
1699 }
1700
1701 #undef COPY_FUNC
1702 #define COPY_FUNC _gst_strdup0
1703
1704 /**
1705  * gst_tag_list_get_string:
1706  * @list: a #GstTagList to get the tag from
1707  * @tag: tag to read out
1708  * @value: (out callee-allocates) (transfer full): location for the result
1709  *
1710  * Copies the contents for the given tag into the value, possibly merging
1711  * multiple values into one if multiple values are associated with the tag.
1712  *
1713  * Use gst_tag_list_get_string_index (list, tag, 0, value) if you want
1714  * to retrieve the first string associated with this tag unmodified.
1715  *
1716  * The resulting string in @value will be in UTF-8 encoding and should be
1717  * freed by the caller using g_free when no longer needed. Since 0.10.24 the
1718  * returned string is also guaranteed to be non-NULL and non-empty.
1719  *
1720  * Free-function: g_free
1721  *
1722  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1723  *              given list.
1724  */
1725 /**
1726  * gst_tag_list_get_string_index:
1727  * @list: a #GstTagList to get the tag from
1728  * @tag: tag to read out
1729  * @index: number of entry to read out
1730  * @value: (out callee-allocates) (transfer full): location for the result
1731  *
1732  * Gets the value that is at the given index for the given tag in the given
1733  * list.
1734  *
1735  * The resulting string in @value will be in UTF-8 encoding and should be
1736  * freed by the caller using g_free when no longer needed. Since 0.10.24 the
1737  * returned string is also guaranteed to be non-NULL and non-empty.
1738  *
1739  * Free-function: g_free
1740  *
1741  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1742  *              given list.
1743  */
1744 TAG_MERGE_FUNCS (string, gchar *, (*value != NULL));
1745
1746 /*
1747  *FIXME 0.11: Instead of _peek (non-copy) and _get (copy), we could have
1748  *            _get (non-copy) and _dup (copy) for strings, seems more
1749  *            widely used
1750  */
1751 /**
1752  * gst_tag_list_peek_string_index:
1753  * @list: a #GstTagList to get the tag from
1754  * @tag: tag to read out
1755  * @index: number of entry to read out
1756  * @value: (out) (transfer none): location for the result
1757  *
1758  * Peeks at the value that is at the given index for the given tag in the given
1759  * list.
1760  *
1761  * The resulting string in @value will be in UTF-8 encoding and doesn't need
1762  * to be freed by the caller. The returned string is also guaranteed to
1763  * be non-NULL and non-empty.
1764  *
1765  * Returns: TRUE, if a value was set, FALSE if the tag didn't exist in the
1766  *              given list.
1767  */
1768 gboolean
1769 gst_tag_list_peek_string_index (const GstTagList * list,
1770     const gchar * tag, guint index, const gchar ** value)
1771 {
1772   const GValue *v;
1773
1774   g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE);
1775   g_return_val_if_fail (tag != NULL, FALSE);
1776   g_return_val_if_fail (value != NULL, FALSE);
1777
1778   if ((v = gst_tag_list_get_value_index (list, tag, index)) == NULL)
1779     return FALSE;
1780   *value = g_value_get_string (v);
1781   return *value != NULL && **value != '\0';
1782 }
1783
1784 /**
1785  * gst_tag_list_get_date:
1786  * @list: a #GstTagList to get the tag from
1787  * @tag: tag to read out
1788  * @value: (out callee-allocates) (transfer full): address of a GDate pointer
1789  *     variable to store the result into
1790  *
1791  * Copies the first date for the given tag in the taglist into the variable
1792  * pointed to by @value. Free the date with g_date_free() when it is no longer
1793  * needed.
1794  *
1795  * Free-function: g_date_free
1796  *
1797  * Returns: TRUE, if a date was copied, FALSE if the tag didn't exist in the
1798  *              given list or if it was #NULL.
1799  */
1800 gboolean
1801 gst_tag_list_get_date (const GstTagList * list, const gchar * tag,
1802     GDate ** value)
1803 {
1804   GValue v = { 0, };
1805
1806   g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE);
1807   g_return_val_if_fail (tag != NULL, FALSE);
1808   g_return_val_if_fail (value != NULL, FALSE);
1809
1810   if (!gst_tag_list_copy_value (&v, list, tag))
1811     return FALSE;
1812   *value = (GDate *) g_value_dup_boxed (&v);
1813   g_value_unset (&v);
1814   return (*value != NULL);
1815 }
1816
1817 /**
1818  * gst_tag_list_get_date_index:
1819  * @list: a #GstTagList to get the tag from
1820  * @tag: tag to read out
1821  * @index: number of entry to read out
1822  * @value: (out callee-allocates) (transfer full): location for the result
1823  *
1824  * Gets the date that is at the given index for the given tag in the given
1825  * list and copies it into the variable pointed to by @value. Free the date
1826  * with g_date_free() when it is no longer needed.
1827  *
1828  * Free-function: g_date_free
1829  *
1830  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1831  *              given list or if it was #NULL.
1832  */
1833 gboolean
1834 gst_tag_list_get_date_index (const GstTagList * list,
1835     const gchar * tag, guint index, GDate ** value)
1836 {
1837   const GValue *v;
1838
1839   g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE);
1840   g_return_val_if_fail (tag != NULL, FALSE);
1841   g_return_val_if_fail (value != NULL, FALSE);
1842
1843   if ((v = gst_tag_list_get_value_index (list, tag, index)) == NULL)
1844     return FALSE;
1845   *value = (GDate *) g_value_dup_boxed (v);
1846   return (*value != NULL);
1847 }
1848
1849 /**
1850  * gst_tag_list_get_date_time:
1851  * @list: a #GstTagList to get the tag from
1852  * @tag: tag to read out
1853  * @value: (out callee-allocates) (transfer full): address of a #GstDateTime
1854  *     pointer variable to store the result into
1855  *
1856  * Copies the first datetime for the given tag in the taglist into the variable
1857  * pointed to by @value. Unref the date with gst_date_time_unref() when
1858  * it is no longer needed.
1859  *
1860  * Free-function: gst_date_time_unref
1861  *
1862  * Returns: TRUE, if a datetime was copied, FALSE if the tag didn't exist in
1863  *              thegiven list or if it was #NULL.
1864  *
1865  * Since: 0.10.31
1866  */
1867 gboolean
1868 gst_tag_list_get_date_time (const GstTagList * list, const gchar * tag,
1869     GstDateTime ** value)
1870 {
1871   GValue v = { 0, };
1872
1873   g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE);
1874   g_return_val_if_fail (tag != NULL, FALSE);
1875   g_return_val_if_fail (value != NULL, FALSE);
1876
1877   if (!gst_tag_list_copy_value (&v, list, tag))
1878     return FALSE;
1879
1880   g_return_val_if_fail (GST_VALUE_HOLDS_DATE_TIME (&v), FALSE);
1881
1882   *value = (GstDateTime *) g_value_dup_boxed (&v);
1883   g_value_unset (&v);
1884   return (*value != NULL);
1885 }
1886
1887 /**
1888  * gst_tag_list_get_date_time_index:
1889  * @list: a #GstTagList to get the tag from
1890  * @tag: tag to read out
1891  * @index: number of entry to read out
1892  * @value: (out callee-allocates) (transfer full): location for the result
1893  *
1894  * Gets the datetime that is at the given index for the given tag in the given
1895  * list and copies it into the variable pointed to by @value. Unref the datetime
1896  * with gst_date_time_unref() when it is no longer needed.
1897  *
1898  * Free-function: gst_date_time_unref
1899  *
1900  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1901  *              given list or if it was #NULL.
1902  *
1903  * Since: 0.10.31
1904  */
1905 gboolean
1906 gst_tag_list_get_date_time_index (const GstTagList * list,
1907     const gchar * tag, guint index, GstDateTime ** value)
1908 {
1909   const GValue *v;
1910
1911   g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE);
1912   g_return_val_if_fail (tag != NULL, FALSE);
1913   g_return_val_if_fail (value != NULL, FALSE);
1914
1915   if ((v = gst_tag_list_get_value_index (list, tag, index)) == NULL)
1916     return FALSE;
1917   *value = (GstDateTime *) g_value_dup_boxed (v);
1918   return (*value != NULL);
1919 }
1920
1921 /**
1922  * gst_tag_list_get_buffer:
1923  * @list: a #GstTagList to get the tag from
1924  * @tag: tag to read out
1925  * @value: (out callee-allocates) (transfer full): address of a GstBuffer
1926  *     pointer variable to store the result into
1927  *
1928  * Copies the first buffer for the given tag in the taglist into the variable
1929  * pointed to by @value. Free the buffer with gst_buffer_unref() when it is
1930  * no longer needed.
1931  *
1932  * Free-function: gst_buffer_unref
1933  *
1934  * Returns: TRUE, if a buffer was copied, FALSE if the tag didn't exist in the
1935  *              given list or if it was #NULL.
1936  *
1937  * Since: 0.10.23
1938  */
1939 gboolean
1940 gst_tag_list_get_buffer (const GstTagList * list, const gchar * tag,
1941     GstBuffer ** value)
1942 {
1943   GValue v = { 0, };
1944
1945   g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE);
1946   g_return_val_if_fail (tag != NULL, FALSE);
1947   g_return_val_if_fail (value != NULL, FALSE);
1948
1949   if (!gst_tag_list_copy_value (&v, list, tag))
1950     return FALSE;
1951   *value = g_value_dup_boxed (&v);
1952   g_value_unset (&v);
1953   return (*value != NULL);
1954 }
1955
1956 /**
1957  * gst_tag_list_get_buffer_index:
1958  * @list: a #GstTagList to get the tag from
1959  * @tag: tag to read out
1960  * @index: number of entry to read out
1961  * @value: (out callee-allocates) (transfer full): address of a GstBuffer
1962  *     pointer variable to store the result into
1963  *
1964  * Gets the buffer that is at the given index for the given tag in the given
1965  * list and copies it into the variable pointed to by @value. Free the buffer
1966  * with gst_buffer_unref() when it is no longer needed.
1967  *
1968  * Free-function: gst_buffer_unref
1969  *
1970  * Returns: TRUE, if a buffer was copied, FALSE if the tag didn't exist in the
1971  *              given list or if it was #NULL.
1972  *
1973  * Since: 0.10.23
1974  */
1975 gboolean
1976 gst_tag_list_get_buffer_index (const GstTagList * list,
1977     const gchar * tag, guint index, GstBuffer ** value)
1978 {
1979   const GValue *v;
1980
1981   g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE);
1982   g_return_val_if_fail (tag != NULL, FALSE);
1983   g_return_val_if_fail (value != NULL, FALSE);
1984
1985   if ((v = gst_tag_list_get_value_index (list, tag, index)) == NULL)
1986     return FALSE;
1987   *value = g_value_dup_boxed (v);
1988   return (*value != NULL);
1989 }