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