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