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