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