1 /* GStreamer non-core tag registration and tag utility functions
2 * Copyright (C) 2005 Ross Burton <ross@burtonini.com>
3 * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
25 #include <gst/gst-i18n-plugin.h>
33 * @short_description: additional tag definitions for plugins and applications
34 * @see_also: #GstTagList
38 * Contains additional standardized GStreamer tag definitions for plugins
39 * and applications, and functions to register them with the GStreamer
47 gst_tag_register_tags_internal (gpointer unused)
50 GST_DEBUG ("binding text domain %s to locale dir %s", GETTEXT_PACKAGE,
52 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
55 /* musicbrainz tags */
56 gst_tag_register (GST_TAG_MUSICBRAINZ_TRACKID, GST_TAG_FLAG_META,
57 G_TYPE_STRING, _("track ID"), _("MusicBrainz track ID"), NULL);
58 gst_tag_register (GST_TAG_MUSICBRAINZ_ARTISTID, GST_TAG_FLAG_META,
59 G_TYPE_STRING, _("artist ID"), _("MusicBrainz artist ID"), NULL);
60 gst_tag_register (GST_TAG_MUSICBRAINZ_ALBUMID, GST_TAG_FLAG_META,
61 G_TYPE_STRING, _("album ID"), _("MusicBrainz album ID"), NULL);
62 gst_tag_register (GST_TAG_MUSICBRAINZ_ALBUMARTISTID, GST_TAG_FLAG_META,
64 _("album artist ID"), _("MusicBrainz album artist ID"), NULL);
65 gst_tag_register (GST_TAG_MUSICBRAINZ_TRMID, GST_TAG_FLAG_META,
66 G_TYPE_STRING, _("track TRM ID"), _("MusicBrainz TRM ID"), NULL);
67 gst_tag_register (GST_TAG_MUSICBRAINZ_SORTNAME, GST_TAG_FLAG_META,
69 _("artist sortname"), _("MusicBrainz artist sortname"), NULL);
72 gst_tag_register (GST_TAG_CDDA_CDDB_DISCID, GST_TAG_FLAG_META,
73 G_TYPE_STRING, "discid", "CDDB discid for metadata retrieval",
74 gst_tag_merge_use_first);
76 gst_tag_register (GST_TAG_CDDA_CDDB_DISCID_FULL, GST_TAG_FLAG_META,
77 G_TYPE_STRING, "discid full",
78 "CDDB discid for metadata retrieval (full)", gst_tag_merge_use_first);
80 gst_tag_register (GST_TAG_CDDA_MUSICBRAINZ_DISCID, GST_TAG_FLAG_META,
81 G_TYPE_STRING, "musicbrainz-discid",
82 "Musicbrainz discid for metadata retrieval", gst_tag_merge_use_first);
84 gst_tag_register (GST_TAG_CDDA_MUSICBRAINZ_DISCID_FULL, GST_TAG_FLAG_META,
85 G_TYPE_STRING, "musicbrainz-discid-full",
86 "Musicbrainz discid for metadata retrieval (full)",
87 gst_tag_merge_use_first);
92 /* FIXME 0.11: rename this to gst_tag_init() or gst_tag_register_tags() */
94 * gst_tag_register_musicbrainz_tags
96 * Registers additional musicbrainz-specific tags with the GStreamer tag
97 * system. Plugins and applications that use these tags should call this
98 * function before using them. Can be called multiple times.
101 gst_tag_register_musicbrainz_tags (void)
103 static GOnce mb_once = G_ONCE_INIT;
105 g_once (&mb_once, gst_tag_register_tags_internal, NULL);
109 register_tag_image_type_enum (GType * id)
111 static const GEnumValue image_types[] = {
112 {GST_TAG_IMAGE_TYPE_UNDEFINED, "GST_TAG_IMAGE_TYPE_UNDEFINED", "undefined"},
113 {GST_TAG_IMAGE_TYPE_FRONT_COVER, "GST_TAG_IMAGE_TYPE_FRONT_COVER",
115 {GST_TAG_IMAGE_TYPE_BACK_COVER, "GST_TAG_IMAGE_TYPE_BACK_COVER",
117 {GST_TAG_IMAGE_TYPE_LEAFLET_PAGE, "GST_TAG_IMAGE_TYPE_LEAFLET_PAGE",
119 {GST_TAG_IMAGE_TYPE_MEDIUM, "GST_TAG_IMAGE_TYPE_MEDIUM", "medium"},
120 {GST_TAG_IMAGE_TYPE_LEAD_ARTIST, "GST_TAG_IMAGE_TYPE_LEAD_ARTIST",
122 {GST_TAG_IMAGE_TYPE_ARTIST, "GST_TAG_IMAGE_TYPE_ARTIST", "artist"},
123 {GST_TAG_IMAGE_TYPE_CONDUCTOR, "GST_TAG_IMAGE_TYPE_CONDUCTOR", "conductor"},
124 {GST_TAG_IMAGE_TYPE_BAND_ORCHESTRA, "GST_TAG_IMAGE_TYPE_BAND_ORCHESTRA",
126 {GST_TAG_IMAGE_TYPE_COMPOSER, "GST_TAG_IMAGE_TYPE_COMPOSER", "composer"},
127 {GST_TAG_IMAGE_TYPE_LYRICIST, "GST_TAG_IMAGE_TYPE_LYRICIST", "lyricist"},
128 {GST_TAG_IMAGE_TYPE_RECORDING_LOCATION,
129 "GST_TAG_IMAGE_TYPE_RECORDING_LOCATION",
130 "recording-location"},
131 {GST_TAG_IMAGE_TYPE_DURING_RECORDING, "GST_TAG_IMAGE_TYPE_DURING_RECORDING",
133 {GST_TAG_IMAGE_TYPE_DURING_PERFORMANCE,
134 "GST_TAG_IMAGE_TYPE_DURING_PERFORMANCE",
135 "during-performance"},
136 {GST_TAG_IMAGE_TYPE_VIDEO_CAPTURE, "GST_TAG_IMAGE_TYPE_VIDEO_CAPTURE",
138 {GST_TAG_IMAGE_TYPE_FISH, "GST_TAG_IMAGE_TYPE_FISH", "fish"},
139 {GST_TAG_IMAGE_TYPE_ILLUSTRATION, "GST_TAG_IMAGE_TYPE_ILLUSTRATION",
141 {GST_TAG_IMAGE_TYPE_BAND_ARTIST_LOGO, "GST_TAG_IMAGE_TYPE_BAND_ARTIST_LOGO",
143 {GST_TAG_IMAGE_TYPE_PUBLISHER_STUDIO_LOGO,
144 "GST_TAG_IMAGE_TYPE_PUBLISHER_STUDIO_LOGO",
145 "publisher-studio-logo"},
149 *id = g_enum_register_static ("GstTagImageType", image_types);
153 gst_tag_image_type_get_type (void)
156 static GOnce once = G_ONCE_INIT;
158 g_once (&once, (GThreadFunc) register_tag_image_type_enum, &id);
163 * gst_tag_parse_extended_comment:
164 * @ext_comment: an extended comment string, see #GST_TAG_EXTENDED_COMMENT
165 * @key: return location for the comment description key, or NULL
166 * @lang: return location for the comment ISO-639 language code, or NULL
167 * @value: return location for the actual comment string, or NULL
168 * @fail_if_no_key: whether to fail if strings are not in key=value form
170 * Convenience function to parse a GST_TAG_EXTENDED_COMMENT string and
171 * separate it into its components.
173 * If successful, @key, @lang and/or @value will be set to newly allocated
174 * strings that you need to free with g_free() when done. @key and @lang
175 * may also be set to NULL by this function if there is no key or no language
176 * code in the extended comment string.
178 * Returns: TRUE if the string could be parsed, otherwise FALSE
183 gst_tag_parse_extended_comment (const gchar * ext_comment, gchar ** key,
184 gchar ** lang, gchar ** value, gboolean fail_if_no_key)
186 const gchar *div, *bop, *bcl;
188 g_return_val_if_fail (ext_comment != NULL, FALSE);
189 g_return_val_if_fail (g_utf8_validate (ext_comment, -1, NULL), FALSE);
196 div = strchr (ext_comment, '=');
197 bop = strchr (ext_comment, '[');
198 bcl = strchr (ext_comment, ']');
204 *value = g_strdup (ext_comment);
208 if (bop != NULL && bop < div) {
209 if (bcl < bop || bcl > div)
212 *key = g_strndup (ext_comment, bop - ext_comment);
214 *lang = g_strndup (bop + 1, bcl - bop - 1);
217 *key = g_strndup (ext_comment, div - ext_comment);
221 *value = g_strdup (div + 1);
227 * gst_tag_freeform_string_to_utf8:
229 * @size: length of string data, or -1 if the string is NUL-terminated
230 * @env_vars: a NULL-terminated string array of environment variable names,
233 * Convenience function to read a string with unknown character encoding. If
234 * the string is already in UTF-8 encoding, it will be returned right away.
235 * Otherwise, the environment will be searched for a number of environment
236 * variables (whose names are specified in the NULL-terminated string array
237 * @env_vars) containing a list of character encodings to try/use. If none
238 * are specified, the current locale will be tried. If that also doesn't work,
239 * ISO-8859-1 is assumed (which will almost always succeed).
241 * Returns: a newly-allocated string in UTF-8 encoding, or NULL
246 gst_tag_freeform_string_to_utf8 (const gchar * data, gint size,
247 const gchar ** env_vars)
249 const gchar *cur_loc = NULL;
253 g_return_val_if_fail (data != NULL, NULL);
256 size = strlen (data);
258 /* chop off trailing string terminators to make sure utf8_validate doesn't
259 * get to see them (since that would make the utf8 check fail) */
260 while (size > 0 && data[size - 1] == '\0')
263 /* Should we try the charsets specified
264 * via environment variables FIRST ? */
265 if (g_utf8_validate (data, size, NULL)) {
266 utf8 = g_strndup (data, size);
267 GST_LOG ("String '%s' is valid UTF-8 already", utf8);
271 while (env_vars && *env_vars != NULL) {
272 const gchar *env = NULL;
274 /* Try charsets specified via the environment */
275 env = g_getenv (*env_vars);
276 if (env != NULL && *env != '\0') {
279 csets = g_strsplit (env, G_SEARCHPATH_SEPARATOR_S, -1);
281 for (c = csets; c && *c; ++c) {
282 GST_LOG ("Trying to convert freeform string to UTF-8 from '%s'", *c);
284 g_convert (data, size, "UTF-8", *c, &bytes_read, NULL, NULL))) {
285 if (bytes_read == size) {
299 /* Try current locale (if not UTF-8) */
300 if (!g_get_charset (&cur_loc)) {
301 GST_LOG ("Trying to convert freeform string using locale ('%s')", cur_loc);
302 if ((utf8 = g_locale_to_utf8 (data, size, &bytes_read, NULL, NULL))) {
303 if (bytes_read == size) {
312 GST_LOG ("Trying to convert freeform string using ISO-8859-1 fallback");
313 utf8 = g_convert (data, size, "UTF-8", "ISO-8859-1", &bytes_read, NULL, NULL);
314 if (utf8 != NULL && bytes_read == size) {
324 if (utf8 && utf8[0] != '\0') {
325 GST_LOG ("Returning '%s'", utf8);