d978b2d440d4f1fa670f7eeb8c1931c3bc0ea743
[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 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: 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: 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
857   g_return_val_if_fail (str != NULL, NULL);
858   g_return_val_if_fail (g_str_has_prefix (str, "taglist"), NULL);
859
860   tag_list = gst_tag_list_new_internal (gst_structure_from_string (str, NULL));
861   return tag_list;
862 }
863
864 /**
865  * gst_tag_list_n_tags:
866  * @list: A #GstTagList.
867  *
868  * Get the number of tags in @list.
869  *
870  * Returns: The number of tags in @list.
871  */
872 gint
873 gst_tag_list_n_tags (const GstTagList * list)
874 {
875   g_return_val_if_fail (list != NULL, 0);
876   g_return_val_if_fail (GST_IS_TAG_LIST (list), 0);
877
878   return gst_structure_n_fields (GST_TAG_LIST_STRUCTURE (list));
879 }
880
881 /**
882  * gst_tag_list_nth_tag_name:
883  * @list: A #GstTagList.
884  * @index: the index
885  *
886  * Get the name of the tag in @list at @index.
887  *
888  * Returns: The name of the tag at @index.
889  */
890 const gchar *
891 gst_tag_list_nth_tag_name (const GstTagList * list, guint index)
892 {
893   g_return_val_if_fail (list != NULL, 0);
894   g_return_val_if_fail (GST_IS_TAG_LIST (list), 0);
895
896   return gst_structure_nth_field_name (GST_TAG_LIST_STRUCTURE (list), index);
897 }
898
899 /**
900  * gst_tag_list_is_empty:
901  * @list: A #GstTagList.
902  *
903  * Checks if the given taglist is empty.
904  *
905  * Returns: TRUE if the taglist is empty, otherwise FALSE.
906  */
907 gboolean
908 gst_tag_list_is_empty (const GstTagList * list)
909 {
910   g_return_val_if_fail (list != NULL, FALSE);
911   g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE);
912
913   return (gst_structure_n_fields (GST_TAG_LIST_STRUCTURE (list)) == 0);
914 }
915
916 static gboolean
917 gst_tag_list_fields_equal (const GValue * value1, const GValue * value2)
918 {
919   gdouble d1, d2;
920
921   if (gst_value_compare (value1, value2) == GST_VALUE_EQUAL)
922     return TRUE;
923
924   /* fields not equal: add some tolerance for doubles, otherwise bail out */
925   if (!G_VALUE_HOLDS_DOUBLE (value1) || !G_VALUE_HOLDS_DOUBLE (value2))
926     return FALSE;
927
928   d1 = g_value_get_double (value1);
929   d2 = g_value_get_double (value2);
930
931   /* This will only work for 'normal' values and values around 0,
932    * which should be good enough for our purposes here
933    * FIXME: maybe add this to gst_value_compare_double() ? */
934   return (fabs (d1 - d2) < 0.0000001);
935 }
936
937 /**
938  * gst_tag_list_is_equal:
939  * @list1: a #GstTagList.
940  * @list2: a #GstTagList.
941  *
942  * Checks if the two given taglists are equal.
943  *
944  * Returns: TRUE if the taglists are equal, otherwise FALSE
945  */
946 gboolean
947 gst_tag_list_is_equal (const GstTagList * list1, const GstTagList * list2)
948 {
949   const GstStructure *s1, *s2;
950   gint num_fields1, num_fields2, i;
951
952   g_return_val_if_fail (GST_IS_TAG_LIST (list1), FALSE);
953   g_return_val_if_fail (GST_IS_TAG_LIST (list2), FALSE);
954
955   /* we don't just use gst_structure_is_equal() here so we can add some
956    * tolerance for doubles, though maybe we should just add that to
957    * gst_value_compare_double() as well? */
958   s1 = GST_TAG_LIST_STRUCTURE (list1);
959   s2 = GST_TAG_LIST_STRUCTURE (list2);
960
961   num_fields1 = gst_structure_n_fields (s1);
962   num_fields2 = gst_structure_n_fields (s2);
963
964   if (num_fields1 != num_fields2)
965     return FALSE;
966
967   for (i = 0; i < num_fields1; i++) {
968     const GValue *value1, *value2;
969     const gchar *tag_name;
970
971     tag_name = gst_structure_nth_field_name (s1, i);
972     value1 = gst_structure_get_value (s1, tag_name);
973     value2 = gst_structure_get_value (s2, tag_name);
974
975     if (value2 == NULL)
976       return FALSE;
977
978     if (!gst_tag_list_fields_equal (value1, value2))
979       return FALSE;
980   }
981
982   return TRUE;
983 }
984
985 typedef struct
986 {
987   GstTagList *list;
988   GstTagMergeMode mode;
989 }
990 GstTagCopyData;
991
992 static void
993 gst_tag_list_add_value_internal (GstTagList * tag_list, GstTagMergeMode mode,
994     const gchar * tag, const GValue * value, GstTagInfo * info)
995 {
996   GstStructure *list = GST_TAG_LIST_STRUCTURE (tag_list);
997   const GValue *value2;
998   GQuark tag_quark;
999
1000   if (info == NULL) {
1001     info = gst_tag_lookup (tag);
1002     if (G_UNLIKELY (info == NULL)) {
1003       g_warning ("unknown tag '%s'", tag);
1004       return;
1005     }
1006   }
1007
1008   if (G_UNLIKELY (!G_VALUE_HOLDS (value, info->type) &&
1009           !GST_VALUE_HOLDS_LIST (value))) {
1010     g_warning ("tag '%s' should hold value of type '%s', but value of "
1011         "type '%s' passed", info->nick, g_type_name (info->type),
1012         g_type_name (G_VALUE_TYPE (value)));
1013     return;
1014   }
1015
1016   tag_quark = info->name_quark;
1017
1018   if (info->merge_func
1019       && (value2 = gst_structure_id_get_value (list, tag_quark)) != NULL) {
1020     GValue dest = { 0, };
1021
1022     switch (mode) {
1023       case GST_TAG_MERGE_REPLACE_ALL:
1024       case GST_TAG_MERGE_REPLACE:
1025         gst_structure_id_set_value (list, tag_quark, value);
1026         break;
1027       case GST_TAG_MERGE_PREPEND:
1028         gst_value_list_merge (&dest, value, value2);
1029         gst_structure_id_set_value (list, tag_quark, &dest);
1030         g_value_unset (&dest);
1031         break;
1032       case GST_TAG_MERGE_APPEND:
1033         gst_value_list_merge (&dest, value2, value);
1034         gst_structure_id_set_value (list, tag_quark, &dest);
1035         g_value_unset (&dest);
1036         break;
1037       case GST_TAG_MERGE_KEEP:
1038       case GST_TAG_MERGE_KEEP_ALL:
1039         break;
1040       default:
1041         g_assert_not_reached ();
1042         break;
1043     }
1044   } else {
1045     switch (mode) {
1046       case GST_TAG_MERGE_APPEND:
1047       case GST_TAG_MERGE_KEEP:
1048         if (gst_structure_id_get_value (list, tag_quark) != NULL)
1049           break;
1050         /* fall through */
1051       case GST_TAG_MERGE_REPLACE_ALL:
1052       case GST_TAG_MERGE_REPLACE:
1053       case GST_TAG_MERGE_PREPEND:
1054         gst_structure_id_set_value (list, tag_quark, value);
1055         break;
1056       case GST_TAG_MERGE_KEEP_ALL:
1057         break;
1058       default:
1059         g_assert_not_reached ();
1060         break;
1061     }
1062   }
1063 }
1064
1065 static gboolean
1066 gst_tag_list_copy_foreach (GQuark tag_quark, const GValue * value,
1067     gpointer user_data)
1068 {
1069   GstTagCopyData *copy = (GstTagCopyData *) user_data;
1070   const gchar *tag;
1071
1072   tag = g_quark_to_string (tag_quark);
1073   gst_tag_list_add_value_internal (copy->list, copy->mode, tag, value, NULL);
1074
1075   return TRUE;
1076 }
1077
1078 /**
1079  * gst_tag_list_insert:
1080  * @into: list to merge into
1081  * @from: list to merge from
1082  * @mode: the mode to use
1083  *
1084  * Inserts the tags of the @from list into the first list using the given mode.
1085  */
1086 void
1087 gst_tag_list_insert (GstTagList * into, const GstTagList * from,
1088     GstTagMergeMode mode)
1089 {
1090   GstTagCopyData data;
1091
1092   g_return_if_fail (GST_IS_TAG_LIST (into));
1093   g_return_if_fail (gst_tag_list_is_writable (into));
1094   g_return_if_fail (GST_IS_TAG_LIST (from));
1095   g_return_if_fail (GST_TAG_MODE_IS_VALID (mode));
1096
1097   data.list = into;
1098   data.mode = mode;
1099   if (mode == GST_TAG_MERGE_REPLACE_ALL) {
1100     gst_structure_remove_all_fields (GST_TAG_LIST_STRUCTURE (into));
1101   }
1102   gst_structure_foreach (GST_TAG_LIST_STRUCTURE (from),
1103       gst_tag_list_copy_foreach, &data);
1104 }
1105
1106 /**
1107  * gst_tag_list_merge:
1108  * @list1: first list to merge
1109  * @list2: second list to merge
1110  * @mode: the mode to use
1111  *
1112  * Merges the two given lists into a new list. If one of the lists is NULL, a
1113  * copy of the other is returned. If both lists are NULL, NULL is returned.
1114  *
1115  * Free-function: gst_tag_list_unref
1116  *
1117  * Returns: (transfer full): the new list
1118  */
1119 GstTagList *
1120 gst_tag_list_merge (const GstTagList * list1, const GstTagList * list2,
1121     GstTagMergeMode mode)
1122 {
1123   GstTagList *list1_cp;
1124   const GstTagList *list2_cp;
1125
1126   g_return_val_if_fail (list1 == NULL || GST_IS_TAG_LIST (list1), NULL);
1127   g_return_val_if_fail (list2 == NULL || GST_IS_TAG_LIST (list2), NULL);
1128   g_return_val_if_fail (GST_TAG_MODE_IS_VALID (mode), NULL);
1129
1130   /* nothing to merge */
1131   if (!list1 && !list2) {
1132     return NULL;
1133   }
1134
1135   /* create empty list, we need to do this to correctly handling merge modes */
1136   list1_cp = (list1) ? gst_tag_list_copy (list1) : gst_tag_list_new_empty ();
1137   list2_cp = (list2) ? list2 : gst_tag_list_new_empty ();
1138
1139   gst_tag_list_insert (list1_cp, list2_cp, mode);
1140
1141   if (!list2)
1142     gst_tag_list_unref ((GstTagList *) list2_cp);
1143
1144   return list1_cp;
1145 }
1146
1147 /**
1148  * gst_tag_list_get_tag_size:
1149  * @list: a taglist
1150  * @tag: the tag to query
1151  *
1152  * Checks how many value are stored in this tag list for the given tag.
1153  *
1154  * Returns: The number of tags stored
1155  */
1156 guint
1157 gst_tag_list_get_tag_size (const GstTagList * list, const gchar * tag)
1158 {
1159   const GValue *value;
1160
1161   g_return_val_if_fail (GST_IS_TAG_LIST (list), 0);
1162
1163   value = gst_structure_get_value (GST_TAG_LIST_STRUCTURE (list), tag);
1164   if (value == NULL)
1165     return 0;
1166   if (G_VALUE_TYPE (value) != GST_TYPE_LIST)
1167     return 1;
1168
1169   return gst_value_list_get_size (value);
1170 }
1171
1172 /**
1173  * gst_tag_list_add:
1174  * @list: list to set tags in
1175  * @mode: the mode to use
1176  * @tag: tag
1177  * @...: NULL-terminated list of values to set
1178  *
1179  * Sets the values for the given tags using the specified mode.
1180  */
1181 void
1182 gst_tag_list_add (GstTagList * list, GstTagMergeMode mode, const gchar * tag,
1183     ...)
1184 {
1185   va_list args;
1186
1187   g_return_if_fail (GST_IS_TAG_LIST (list));
1188   g_return_if_fail (gst_tag_list_is_writable (list));
1189   g_return_if_fail (GST_TAG_MODE_IS_VALID (mode));
1190   g_return_if_fail (tag != NULL);
1191
1192   va_start (args, tag);
1193   gst_tag_list_add_valist (list, mode, tag, args);
1194   va_end (args);
1195 }
1196
1197 /**
1198  * gst_tag_list_add_values:
1199  * @list: list to set tags in
1200  * @mode: the mode to use
1201  * @tag: tag
1202  * @...: GValues to set
1203  *
1204  * Sets the GValues for the given tags using the specified mode.
1205  */
1206 void
1207 gst_tag_list_add_values (GstTagList * list, GstTagMergeMode mode,
1208     const gchar * tag, ...)
1209 {
1210   va_list args;
1211
1212   g_return_if_fail (GST_IS_TAG_LIST (list));
1213   g_return_if_fail (gst_tag_list_is_writable (list));
1214   g_return_if_fail (GST_TAG_MODE_IS_VALID (mode));
1215   g_return_if_fail (tag != NULL);
1216
1217   va_start (args, tag);
1218   gst_tag_list_add_valist_values (list, mode, tag, args);
1219   va_end (args);
1220 }
1221
1222 /**
1223  * gst_tag_list_add_valist:
1224  * @list: list to set tags in
1225  * @mode: the mode to use
1226  * @tag: tag
1227  * @var_args: tag / value pairs to set
1228  *
1229  * Sets the values for the given tags using the specified mode.
1230  */
1231 void
1232 gst_tag_list_add_valist (GstTagList * list, GstTagMergeMode mode,
1233     const gchar * tag, va_list var_args)
1234 {
1235   GstTagInfo *info;
1236   gchar *error = NULL;
1237
1238   g_return_if_fail (GST_IS_TAG_LIST (list));
1239   g_return_if_fail (gst_tag_list_is_writable (list));
1240   g_return_if_fail (GST_TAG_MODE_IS_VALID (mode));
1241   g_return_if_fail (tag != NULL);
1242
1243   if (mode == GST_TAG_MERGE_REPLACE_ALL) {
1244     gst_structure_remove_all_fields (GST_TAG_LIST_STRUCTURE (list));
1245   }
1246
1247   while (tag != NULL) {
1248     GValue value = { 0, };
1249
1250     info = gst_tag_lookup (tag);
1251     if (G_UNLIKELY (info == NULL)) {
1252       g_warning ("unknown tag '%s'", tag);
1253       return;
1254     }
1255     G_VALUE_COLLECT_INIT (&value, info->type, var_args, 0, &error);
1256     if (error) {
1257       g_warning ("%s: %s", G_STRLOC, error);
1258       g_free (error);
1259       /* we purposely leak the value here, it might not be
1260        * in a sane state if an error condition occoured
1261        */
1262       return;
1263     }
1264     /* Facilitate GstBuffer -> GstSample transition */
1265     if (G_UNLIKELY (info->type == GST_TYPE_SAMPLE &&
1266             !GST_IS_SAMPLE (value.data[0].v_pointer))) {
1267       g_warning ("Expected GstSample argument for tag '%s'", tag);
1268     } else {
1269       gst_tag_list_add_value_internal (list, mode, tag, &value, info);
1270     }
1271     g_value_unset (&value);
1272     tag = va_arg (var_args, gchar *);
1273   }
1274 }
1275
1276 /**
1277  * gst_tag_list_add_valist_values:
1278  * @list: list to set tags in
1279  * @mode: the mode to use
1280  * @tag: tag
1281  * @var_args: tag / GValue pairs to set
1282  *
1283  * Sets the GValues for the given tags using the specified mode.
1284  */
1285 void
1286 gst_tag_list_add_valist_values (GstTagList * list, GstTagMergeMode mode,
1287     const gchar * tag, va_list var_args)
1288 {
1289   g_return_if_fail (GST_IS_TAG_LIST (list));
1290   g_return_if_fail (gst_tag_list_is_writable (list));
1291   g_return_if_fail (GST_TAG_MODE_IS_VALID (mode));
1292   g_return_if_fail (tag != NULL);
1293
1294   if (mode == GST_TAG_MERGE_REPLACE_ALL) {
1295     gst_structure_remove_all_fields (GST_TAG_LIST_STRUCTURE (list));
1296   }
1297
1298   while (tag != NULL) {
1299     GstTagInfo *info;
1300
1301     info = gst_tag_lookup (tag);
1302     if (G_UNLIKELY (info == NULL)) {
1303       g_warning ("unknown tag '%s'", tag);
1304       return;
1305     }
1306     gst_tag_list_add_value_internal (list, mode, tag, va_arg (var_args,
1307             GValue *), info);
1308     tag = va_arg (var_args, gchar *);
1309   }
1310 }
1311
1312 /**
1313  * gst_tag_list_add_value:
1314  * @list: list to set tags in
1315  * @mode: the mode to use
1316  * @tag: tag
1317  * @value: GValue for this tag
1318  *
1319  * Sets the GValue for a given tag using the specified mode.
1320  */
1321 void
1322 gst_tag_list_add_value (GstTagList * list, GstTagMergeMode mode,
1323     const gchar * tag, const GValue * value)
1324 {
1325   g_return_if_fail (GST_IS_TAG_LIST (list));
1326   g_return_if_fail (gst_tag_list_is_writable (list));
1327   g_return_if_fail (GST_TAG_MODE_IS_VALID (mode));
1328   g_return_if_fail (tag != NULL);
1329
1330   gst_tag_list_add_value_internal (list, mode, tag, value, NULL);
1331 }
1332
1333 /**
1334  * gst_tag_list_remove_tag:
1335  * @list: list to remove tag from
1336  * @tag: tag to remove
1337  *
1338  * Removes the given tag from the taglist.
1339  */
1340 void
1341 gst_tag_list_remove_tag (GstTagList * list, const gchar * tag)
1342 {
1343   g_return_if_fail (GST_IS_TAG_LIST (list));
1344   g_return_if_fail (tag != NULL);
1345
1346   gst_structure_remove_field (GST_TAG_LIST_STRUCTURE (list), tag);
1347 }
1348
1349 typedef struct
1350 {
1351   GstTagForeachFunc func;
1352   const GstTagList *tag_list;
1353   gpointer data;
1354 }
1355 TagForeachData;
1356
1357 static int
1358 structure_foreach_wrapper (GQuark field_id, const GValue * value,
1359     gpointer user_data)
1360 {
1361   TagForeachData *data = (TagForeachData *) user_data;
1362
1363   data->func (data->tag_list, g_quark_to_string (field_id), data->data);
1364   return TRUE;
1365 }
1366
1367 /**
1368  * gst_tag_list_foreach:
1369  * @list: list to iterate over
1370  * @func: (scope call): function to be called for each tag
1371  * @user_data: (closure): user specified data
1372  *
1373  * Calls the given function for each tag inside the tag list. Note that if there
1374  * is no tag, the function won't be called at all.
1375  */
1376 void
1377 gst_tag_list_foreach (const GstTagList * list, GstTagForeachFunc func,
1378     gpointer user_data)
1379 {
1380   TagForeachData data;
1381
1382   g_return_if_fail (GST_IS_TAG_LIST (list));
1383   g_return_if_fail (func != NULL);
1384
1385   data.func = func;
1386   data.tag_list = list;
1387   data.data = user_data;
1388   gst_structure_foreach (GST_TAG_LIST_STRUCTURE (list),
1389       structure_foreach_wrapper, &data);
1390 }
1391
1392 /**
1393  * gst_tag_list_get_value_index:
1394  * @list: a #GstTagList
1395  * @tag: tag to read out
1396  * @index: number of entry to read out
1397  *
1398  * Gets the value that is at the given index for the given tag in the given
1399  * list.
1400  *
1401  * Returns: (transfer none): The GValue for the specified entry or NULL if the
1402  *          tag wasn't available or the tag doesn't have as many entries
1403  */
1404 const GValue *
1405 gst_tag_list_get_value_index (const GstTagList * list, const gchar * tag,
1406     guint index)
1407 {
1408   const GValue *value;
1409
1410   g_return_val_if_fail (GST_IS_TAG_LIST (list), NULL);
1411   g_return_val_if_fail (tag != NULL, NULL);
1412
1413   value = gst_structure_get_value (GST_TAG_LIST_STRUCTURE (list), tag);
1414   if (value == NULL)
1415     return NULL;
1416
1417   if (GST_VALUE_HOLDS_LIST (value)) {
1418     if (index >= gst_value_list_get_size (value))
1419       return NULL;
1420     return gst_value_list_get_value (value, index);
1421   } else {
1422     if (index > 0)
1423       return NULL;
1424     return value;
1425   }
1426 }
1427
1428 /**
1429  * gst_tag_list_copy_value:
1430  * @dest: (out caller-allocates): uninitialized #GValue to copy into
1431  * @list: list to get the tag from
1432  * @tag: tag to read out
1433  *
1434  * Copies the contents for the given tag into the value,
1435  * merging multiple values into one if multiple values are associated
1436  * with the tag.
1437  * You must g_value_unset() the value after use.
1438  *
1439  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1440  *          given list.
1441  */
1442 gboolean
1443 gst_tag_list_copy_value (GValue * dest, const GstTagList * list,
1444     const gchar * tag)
1445 {
1446   const GValue *src;
1447
1448   g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE);
1449   g_return_val_if_fail (tag != NULL, FALSE);
1450   g_return_val_if_fail (dest != NULL, FALSE);
1451   g_return_val_if_fail (G_VALUE_TYPE (dest) == 0, FALSE);
1452
1453   src = gst_structure_get_value (GST_TAG_LIST_STRUCTURE (list), tag);
1454   if (!src)
1455     return FALSE;
1456
1457   if (G_VALUE_TYPE (src) == GST_TYPE_LIST) {
1458     GstTagInfo *info = gst_tag_lookup (tag);
1459
1460     if (!info)
1461       return FALSE;
1462
1463     /* must be there or lists aren't allowed */
1464     g_assert (info->merge_func);
1465     info->merge_func (dest, src);
1466   } else {
1467     g_value_init (dest, G_VALUE_TYPE (src));
1468     g_value_copy (src, dest);
1469   }
1470   return TRUE;
1471 }
1472
1473 /* FIXME 0.11: this whole merge function business is overdesigned, and the
1474  * _get_foo() API is misleading as well - how many application developers will
1475  * expect gst_tag_list_get_string (list, GST_TAG_ARTIST, &val) might return a
1476  * string with multiple comma-separated artists? _get_foo() should just be
1477  * a convenience wrapper around _get_foo_index (list, tag, 0, &val),
1478  * supplemented by a special _tag_list_get_string_merged() function if needed
1479  * (unless someone can actually think of real use cases where the merge
1480  * function is not 'use first' for non-strings and merge for strings) */
1481
1482 /***** evil macros to get all the gst_tag_list_get_*() functions right *****/
1483
1484 #define TAG_MERGE_FUNCS(name,type,ret)                                  \
1485 gboolean                                                                \
1486 gst_tag_list_get_ ## name (const GstTagList *list, const gchar *tag,    \
1487                            type *value)                                 \
1488 {                                                                       \
1489   GValue v = { 0, };                                                    \
1490                                                                         \
1491   g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE);                 \
1492   g_return_val_if_fail (tag != NULL, FALSE);                            \
1493   g_return_val_if_fail (value != NULL, FALSE);                          \
1494                                                                         \
1495   if (!gst_tag_list_copy_value (&v, list, tag))                         \
1496       return FALSE;                                                     \
1497   *value = COPY_FUNC (g_value_get_ ## name (&v));                       \
1498   g_value_unset (&v);                                                   \
1499   return ret;                                                           \
1500 }                                                                       \
1501                                                                         \
1502 gboolean                                                                \
1503 gst_tag_list_get_ ## name ## _index (const GstTagList *list,            \
1504                                      const gchar *tag,                  \
1505                                      guint index, type *value)          \
1506 {                                                                       \
1507   const GValue *v;                                                      \
1508                                                                         \
1509   g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE);                 \
1510   g_return_val_if_fail (tag != NULL, FALSE);                            \
1511   g_return_val_if_fail (value != NULL, FALSE);                          \
1512                                                                         \
1513   if ((v = gst_tag_list_get_value_index (list, tag, index)) == NULL)    \
1514       return FALSE;                                                     \
1515   *value = COPY_FUNC (g_value_get_ ## name (v));                        \
1516   return ret;                                                           \
1517 }
1518
1519 #define COPY_FUNC /**/
1520 /**
1521  * gst_tag_list_get_boolean:
1522  * @list: a #GstTagList to get the tag from
1523  * @tag: tag to read out
1524  * @value: (out): location for the result
1525  *
1526  * Copies the contents for the given tag into the value, merging multiple values
1527  * into one if multiple values are associated with the tag.
1528  *
1529  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1530  *              given list.
1531  */
1532 /**
1533  * gst_tag_list_get_boolean_index:
1534  * @list: a #GstTagList to get the tag from
1535  * @tag: tag to read out
1536  * @index: number of entry to read out
1537  * @value: (out): location for the result
1538  *
1539  * Gets the value that is at the given index for the given tag in the given
1540  * list.
1541  *
1542  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1543  *              given list.
1544  */
1545 TAG_MERGE_FUNCS (boolean, gboolean, TRUE);
1546 /**
1547  * gst_tag_list_get_int:
1548  * @list: a #GstTagList to get the tag from
1549  * @tag: tag to read out
1550  * @value: (out): location for the result
1551  *
1552  * Copies the contents for the given tag into the value, merging multiple values
1553  * into one if multiple values are associated with the tag.
1554  *
1555  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1556  *              given list.
1557  */
1558 /**
1559  * gst_tag_list_get_int_index:
1560  * @list: a #GstTagList to get the tag from
1561  * @tag: tag to read out
1562  * @index: number of entry to read out
1563  * @value: (out): location for the result
1564  *
1565  * Gets the value that is at the given index for the given tag in the given
1566  * list.
1567  *
1568  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1569  *              given list.
1570  */
1571 TAG_MERGE_FUNCS (int, gint, TRUE);
1572 /**
1573  * gst_tag_list_get_uint:
1574  * @list: a #GstTagList to get the tag from
1575  * @tag: tag to read out
1576  * @value: (out): location for the result
1577  *
1578  * Copies the contents for the given tag into the value, merging multiple values
1579  * into one if multiple values are associated with the tag.
1580  *
1581  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1582  *              given list.
1583  */
1584 /**
1585  * gst_tag_list_get_uint_index:
1586  * @list: a #GstTagList to get the tag from
1587  * @tag: tag to read out
1588  * @index: number of entry to read out
1589  * @value: (out): location for the result
1590  *
1591  * Gets the value that is at the given index for the given tag in the given
1592  * list.
1593  *
1594  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1595  *              given list.
1596  */
1597 TAG_MERGE_FUNCS (uint, guint, TRUE);
1598 /**
1599  * gst_tag_list_get_int64_index:
1600  * @list: a #GstTagList to get the tag from
1601  * @tag: tag to read out
1602  * @index: number of entry to read out
1603  * @value: (out): location for the result
1604  *
1605  * Gets the value that is at the given index for the given tag in the given
1606  * list.
1607  *
1608  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1609  *              given list.
1610  */
1611 TAG_MERGE_FUNCS (int64, gint64, TRUE);
1612 /**
1613  * gst_tag_list_get_uint64:
1614  * @list: a #GstTagList to get the tag from
1615  * @tag: tag to read out
1616  * @value: (out): location for the result
1617  *
1618  * Copies the contents for the given tag into the value, merging multiple values
1619  * into one if multiple values are associated with the tag.
1620  *
1621  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1622  *              given list.
1623  */
1624 /**
1625  * gst_tag_list_get_uint64_index:
1626  * @list: a #GstTagList to get the tag from
1627  * @tag: tag to read out
1628  * @index: number of entry to read out
1629  * @value: (out): location for the result
1630  *
1631  * Gets the value that is at the given index for the given tag in the given
1632  * list.
1633  *
1634  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1635  *              given list.
1636  */
1637 TAG_MERGE_FUNCS (uint64, guint64, TRUE);
1638 /**
1639  * gst_tag_list_get_float:
1640  * @list: a #GstTagList to get the tag from
1641  * @tag: tag to read out
1642  * @value: (out): location for the result
1643  *
1644  * Copies the contents for the given tag into the value, merging multiple values
1645  * into one if multiple values are associated with the tag.
1646  *
1647  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1648  *              given list.
1649  */
1650 /**
1651  * gst_tag_list_get_float_index:
1652  * @list: a #GstTagList to get the tag from
1653  * @tag: tag to read out
1654  * @index: number of entry to read out
1655  * @value: (out): location for the result
1656  *
1657  * Gets the value that is at the given index for the given tag in the given
1658  * list.
1659  *
1660  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1661  *              given list.
1662  */
1663 TAG_MERGE_FUNCS (float, gfloat, TRUE);
1664 /**
1665  * gst_tag_list_get_double:
1666  * @list: a #GstTagList to get the tag from
1667  * @tag: tag to read out
1668  * @value: (out): location for the result
1669  *
1670  * Copies the contents for the given tag into the value, merging multiple values
1671  * into one if multiple values are associated with the tag.
1672  *
1673  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1674  *              given list.
1675  */
1676 /**
1677  * gst_tag_list_get_double_index:
1678  * @list: a #GstTagList to get the tag from
1679  * @tag: tag to read out
1680  * @index: number of entry to read out
1681  * @value: (out): location for the result
1682  *
1683  * Gets the value that is at the given index for the given tag in the given
1684  * list.
1685  *
1686  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1687  *              given list.
1688  */
1689 TAG_MERGE_FUNCS (double, gdouble, TRUE);
1690 /**
1691  * gst_tag_list_get_pointer:
1692  * @list: a #GstTagList to get the tag from
1693  * @tag: tag to read out
1694  * @value: (out) (transfer none): location for the result
1695  *
1696  * Copies the contents for the given tag into the value, merging multiple values
1697  * into one if multiple values are associated with the tag.
1698  *
1699  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1700  *              given list.
1701  */
1702 /**
1703  * gst_tag_list_get_pointer_index:
1704  * @list: a #GstTagList to get the tag from
1705  * @tag: tag to read out
1706  * @index: number of entry to read out
1707  * @value: (out) (transfer none): location for the result
1708  *
1709  * Gets the value that is at the given index for the given tag in the given
1710  * list.
1711  *
1712  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1713  *              given list.
1714  */
1715 TAG_MERGE_FUNCS (pointer, gpointer, (*value != NULL));
1716
1717 static inline gchar *
1718 _gst_strdup0 (const gchar * s)
1719 {
1720   if (s == NULL || *s == '\0')
1721     return NULL;
1722
1723   return g_strdup (s);
1724 }
1725
1726 #undef COPY_FUNC
1727 #define COPY_FUNC _gst_strdup0
1728
1729 /**
1730  * gst_tag_list_get_string:
1731  * @list: a #GstTagList to get the tag from
1732  * @tag: tag to read out
1733  * @value: (out callee-allocates) (transfer full): location for the result
1734  *
1735  * Copies the contents for the given tag into the value, possibly merging
1736  * multiple values into one if multiple values are associated with the tag.
1737  *
1738  * Use gst_tag_list_get_string_index (list, tag, 0, value) if you want
1739  * to retrieve the first string associated with this tag unmodified.
1740  *
1741  * The resulting string in @value will be in UTF-8 encoding and should be
1742  * freed by the caller using g_free when no longer needed. The
1743  * returned string is also guaranteed to be non-NULL and non-empty.
1744  *
1745  * Free-function: g_free
1746  *
1747  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1748  *              given list.
1749  */
1750 /**
1751  * gst_tag_list_get_string_index:
1752  * @list: a #GstTagList to get the tag from
1753  * @tag: tag to read out
1754  * @index: number of entry to read out
1755  * @value: (out callee-allocates) (transfer full): location for the result
1756  *
1757  * Gets the value that is at the given index for the given tag in the given
1758  * list.
1759  *
1760  * The resulting string in @value will be in UTF-8 encoding and should be
1761  * freed by the caller using g_free when no longer needed. The
1762  * returned string is also guaranteed to be non-NULL and non-empty.
1763  *
1764  * Free-function: g_free
1765  *
1766  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1767  *              given list.
1768  */
1769 TAG_MERGE_FUNCS (string, gchar *, (*value != NULL));
1770
1771 /*
1772  *FIXME 0.11: Instead of _peek (non-copy) and _get (copy), we could have
1773  *            _get (non-copy) and _dup (copy) for strings, seems more
1774  *            widely used
1775  */
1776 /**
1777  * gst_tag_list_peek_string_index:
1778  * @list: a #GstTagList to get the tag from
1779  * @tag: tag to read out
1780  * @index: number of entry to read out
1781  * @value: (out) (transfer none): location for the result
1782  *
1783  * Peeks at the value that is at the given index for the given tag in the given
1784  * list.
1785  *
1786  * The resulting string in @value will be in UTF-8 encoding and doesn't need
1787  * to be freed by the caller. The returned string is also guaranteed to
1788  * be non-NULL and non-empty.
1789  *
1790  * Returns: TRUE, if a value was set, FALSE if the tag didn't exist in the
1791  *              given list.
1792  */
1793 gboolean
1794 gst_tag_list_peek_string_index (const GstTagList * list,
1795     const gchar * tag, guint index, const gchar ** value)
1796 {
1797   const GValue *v;
1798
1799   g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE);
1800   g_return_val_if_fail (tag != NULL, FALSE);
1801   g_return_val_if_fail (value != NULL, FALSE);
1802
1803   if ((v = gst_tag_list_get_value_index (list, tag, index)) == NULL)
1804     return FALSE;
1805   *value = g_value_get_string (v);
1806   return *value != NULL && **value != '\0';
1807 }
1808
1809 /**
1810  * gst_tag_list_get_date:
1811  * @list: a #GstTagList to get the tag from
1812  * @tag: tag to read out
1813  * @value: (out callee-allocates) (transfer full): address of a GDate pointer
1814  *     variable to store the result into
1815  *
1816  * Copies the first date for the given tag in the taglist into the variable
1817  * pointed to by @value. Free the date with g_date_free() when it is no longer
1818  * needed.
1819  *
1820  * Free-function: g_date_free
1821  *
1822  * Returns: TRUE, if a date was copied, FALSE if the tag didn't exist in the
1823  *              given list or if it was #NULL.
1824  */
1825 gboolean
1826 gst_tag_list_get_date (const GstTagList * list, const gchar * tag,
1827     GDate ** value)
1828 {
1829   GValue v = { 0, };
1830
1831   g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE);
1832   g_return_val_if_fail (tag != NULL, FALSE);
1833   g_return_val_if_fail (value != NULL, FALSE);
1834
1835   if (!gst_tag_list_copy_value (&v, list, tag))
1836     return FALSE;
1837   *value = (GDate *) g_value_dup_boxed (&v);
1838   g_value_unset (&v);
1839   return (*value != NULL);
1840 }
1841
1842 /**
1843  * gst_tag_list_get_date_index:
1844  * @list: a #GstTagList to get the tag from
1845  * @tag: tag to read out
1846  * @index: number of entry to read out
1847  * @value: (out callee-allocates) (transfer full): location for the result
1848  *
1849  * Gets the date that is at the given index for the given tag in the given
1850  * list and copies it into the variable pointed to by @value. Free the date
1851  * with g_date_free() when it is no longer needed.
1852  *
1853  * Free-function: g_date_free
1854  *
1855  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1856  *              given list or if it was #NULL.
1857  */
1858 gboolean
1859 gst_tag_list_get_date_index (const GstTagList * list,
1860     const gchar * tag, guint index, GDate ** value)
1861 {
1862   const GValue *v;
1863
1864   g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE);
1865   g_return_val_if_fail (tag != NULL, FALSE);
1866   g_return_val_if_fail (value != NULL, FALSE);
1867
1868   if ((v = gst_tag_list_get_value_index (list, tag, index)) == NULL)
1869     return FALSE;
1870   *value = (GDate *) g_value_dup_boxed (v);
1871   return (*value != NULL);
1872 }
1873
1874 /**
1875  * gst_tag_list_get_date_time:
1876  * @list: a #GstTagList to get the tag from
1877  * @tag: tag to read out
1878  * @value: (out callee-allocates) (transfer full): address of a #GstDateTime
1879  *     pointer variable to store the result into
1880  *
1881  * Copies the first datetime for the given tag in the taglist into the variable
1882  * pointed to by @value. Unref the date with gst_date_time_unref() when
1883  * it is no longer needed.
1884  *
1885  * Free-function: gst_date_time_unref
1886  *
1887  * Returns: TRUE, if a datetime was copied, FALSE if the tag didn't exist in
1888  *              thegiven list or if it was #NULL.
1889  */
1890 gboolean
1891 gst_tag_list_get_date_time (const GstTagList * list, const gchar * tag,
1892     GstDateTime ** value)
1893 {
1894   GValue v = { 0, };
1895
1896   g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE);
1897   g_return_val_if_fail (tag != NULL, FALSE);
1898   g_return_val_if_fail (value != NULL, FALSE);
1899
1900   if (!gst_tag_list_copy_value (&v, list, tag))
1901     return FALSE;
1902
1903   g_return_val_if_fail (GST_VALUE_HOLDS_DATE_TIME (&v), FALSE);
1904
1905   *value = (GstDateTime *) g_value_dup_boxed (&v);
1906   g_value_unset (&v);
1907   return (*value != NULL);
1908 }
1909
1910 /**
1911  * gst_tag_list_get_date_time_index:
1912  * @list: a #GstTagList to get the tag from
1913  * @tag: tag to read out
1914  * @index: number of entry to read out
1915  * @value: (out callee-allocates) (transfer full): location for the result
1916  *
1917  * Gets the datetime that is at the given index for the given tag in the given
1918  * list and copies it into the variable pointed to by @value. Unref the datetime
1919  * with gst_date_time_unref() when it is no longer needed.
1920  *
1921  * Free-function: gst_date_time_unref
1922  *
1923  * Returns: TRUE, if a value was copied, FALSE if the tag didn't exist in the
1924  *              given list or if it was #NULL.
1925  */
1926 gboolean
1927 gst_tag_list_get_date_time_index (const GstTagList * list,
1928     const gchar * tag, guint index, GstDateTime ** value)
1929 {
1930   const GValue *v;
1931
1932   g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE);
1933   g_return_val_if_fail (tag != NULL, FALSE);
1934   g_return_val_if_fail (value != NULL, FALSE);
1935
1936   if ((v = gst_tag_list_get_value_index (list, tag, index)) == NULL)
1937     return FALSE;
1938   *value = (GstDateTime *) g_value_dup_boxed (v);
1939   return (*value != NULL);
1940 }
1941
1942 /**
1943  * gst_tag_list_get_sample:
1944  * @list: a #GstTagList to get the tag from
1945  * @tag: tag to read out
1946  * @sample: (out callee-allocates) (transfer full): address of a GstSample
1947  *     pointer variable to store the result into
1948  *
1949  * Copies the first sample for the given tag in the taglist into the variable
1950  * pointed to by @sample. Free the sample with gst_sample_unref() when it is
1951  * no longer needed. You can retrieve the buffer from the sample using
1952  * gst_sample_get_buffer() and the associated caps (if any) with
1953  * gst_sample_get_caps().
1954  *
1955  * Free-function: gst_sample_unref
1956  *
1957  * Returns: TRUE, if a sample was returned, FALSE if the tag didn't exist in
1958  *              the given list or if it was #NULL.
1959  */
1960 gboolean
1961 gst_tag_list_get_sample (const GstTagList * list, const gchar * tag,
1962     GstSample ** sample)
1963 {
1964   GValue v = { 0, };
1965
1966   g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE);
1967   g_return_val_if_fail (tag != NULL, FALSE);
1968   g_return_val_if_fail (sample != NULL, FALSE);
1969
1970   if (!gst_tag_list_copy_value (&v, list, tag))
1971     return FALSE;
1972   *sample = g_value_dup_boxed (&v);
1973   g_value_unset (&v);
1974   return (*sample != NULL);
1975 }
1976
1977 /**
1978  * gst_tag_list_get_sample_index:
1979  * @list: a #GstTagList to get the tag from
1980  * @tag: tag to read out
1981  * @index: number of entry to read out
1982  * @sample: (out callee-allocates) (transfer full): address of a GstSample
1983  *     pointer variable to store the result into
1984  *
1985  * Gets the sample that is at the given index for the given tag in the given
1986  * list and copies it into the variable pointed to by @smple. Free the sample
1987  * with gst_sample_unref() when it is no longer needed. You can retrieve the
1988  * buffer from the sample using gst_sample_get_buffer() and the associated
1989  * caps (if any) with gst_sample_get_caps().
1990  *
1991  * Free-function: gst_sample_unref
1992  *
1993  * Returns: TRUE, if a sample was copied, FALSE if the tag didn't exist in the
1994  *              given list or if it was #NULL.
1995  */
1996 gboolean
1997 gst_tag_list_get_sample_index (const GstTagList * list,
1998     const gchar * tag, guint index, GstSample ** sample)
1999 {
2000   const GValue *v;
2001
2002   g_return_val_if_fail (GST_IS_TAG_LIST (list), FALSE);
2003   g_return_val_if_fail (tag != NULL, FALSE);
2004   g_return_val_if_fail (sample != NULL, FALSE);
2005
2006   if ((v = gst_tag_list_get_value_index (list, tag, index)) == NULL)
2007     return FALSE;
2008   *sample = g_value_dup_boxed (v);
2009   return (*sample != NULL);
2010 }