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