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