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