Use new gst_element_class_set_static_metadata()
[platform/upstream/gst-plugins-good.git] / ext / taglib / gstapev2mux.cc
1 /* GStreamer taglib-based APEv2 muxer
2  * Copyright (C) 2006 Christophe Fergeau <teuf@gnome.org>
3  * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
4  * Copyright (C) 2006 Sebastian Dröge <slomo@circular-chaos.org>
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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 /**
23  * SECTION:element-apev2mux
24  * @see_also: #GstTagSetter
25  *
26  * This element adds APEv2 tags to the beginning of a stream using the taglib
27  * library.
28  *
29  * Applications can set the tags to write using the #GstTagSetter interface.
30  * Tags sent by upstream elements will be picked up automatically (and merged
31  * according to the merge mode set via the tag setter interface).
32  *
33  * <refsect2>
34  * <title>Example pipelines</title>
35  * |[
36  * gst-launch -v filesrc location=foo.ogg ! decodebin ! audioconvert ! lame ! apev2mux ! filesink location=foo.mp3
37  * ]| A pipeline that transcodes a file from Ogg/Vorbis to mp3 format with an
38  * APEv2 that contains the same as the the Ogg/Vorbis file. Make sure the
39  * Ogg/Vorbis file actually has comments to preserve.
40  * |[
41  * gst-launch -m filesrc location=foo.mp3 ! apedemux ! fakesink silent=TRUE 2&gt; /dev/null | grep taglist
42  * ]| Verify that tags have been written.
43  * </refsect2>
44  */
45
46 #ifdef HAVE_CONFIG_H
47 #include <config.h>
48 #endif
49
50 #include "gstapev2mux.h"
51
52 #include <string.h>
53
54 #include <apetag.h>
55 #include <gst/tag/tag.h>
56
57 using namespace TagLib;
58
59 GST_DEBUG_CATEGORY_STATIC (gst_apev2_mux_debug);
60 #define GST_CAT_DEFAULT gst_apev2_mux_debug
61
62 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
63     GST_PAD_SRC,
64     GST_PAD_ALWAYS,
65     GST_STATIC_CAPS ("application/x-apetag"));
66
67 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
68     GST_PAD_SINK,
69     GST_PAD_ALWAYS,
70     GST_STATIC_CAPS ("ANY"));
71
72 G_DEFINE_TYPE (GstApev2Mux, gst_apev2_mux, GST_TYPE_TAG_MUX);
73
74 static GstBuffer *gst_apev2_mux_render_tag (GstTagMux * mux,
75     const GstTagList * taglist);
76 static GstBuffer *gst_apev2_mux_render_end_tag (GstTagMux * mux,
77     const GstTagList * taglist);
78
79 static void
80 gst_apev2_mux_class_init (GstApev2MuxClass * klass)
81 {
82   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
83
84   GST_TAG_MUX_CLASS (klass)->render_start_tag =
85       GST_DEBUG_FUNCPTR (gst_apev2_mux_render_tag);
86   GST_TAG_MUX_CLASS (klass)->render_end_tag =
87       GST_DEBUG_FUNCPTR (gst_apev2_mux_render_end_tag);
88
89   gst_element_class_add_pad_template (element_class,
90       gst_static_pad_template_get (&sink_template));
91   gst_element_class_add_pad_template (element_class,
92       gst_static_pad_template_get (&src_template));
93
94   gst_element_class_set_static_metadata (element_class,
95       "TagLib-based APEv2 Muxer", "Formatter/Metadata",
96       "Adds an APEv2 header to the beginning of files using taglib",
97       "Sebastian Dröge <slomo@circular-chaos.org>");
98
99   GST_DEBUG_CATEGORY_INIT (gst_apev2_mux_debug, "apev2mux", 0,
100       "taglib-based APEv2 tag muxer");
101 }
102
103 static void
104 gst_apev2_mux_init (GstApev2Mux * apev2mux)
105 {
106   /* nothing to do */
107 }
108
109 static void
110 add_one_tag (const GstTagList * list, const gchar * tag, gpointer user_data)
111 {
112   APE::Tag * apev2tag = (APE::Tag *) user_data;
113   gboolean result;
114
115   /* FIXME: if there are several values set for the same tag, this won't
116    * work, only the first value will be taken into account
117    */
118   if (strcmp (tag, GST_TAG_TITLE) == 0) {
119     const char *title;
120
121     result = gst_tag_list_peek_string_index (list, tag, 0, &title);
122     if (result != FALSE) {
123       GST_DEBUG ("Setting title to %s", title);
124       apev2tag->setTitle (String (title, String::UTF8));
125     }
126   } else if (strcmp (tag, GST_TAG_ALBUM) == 0) {
127     const char *album;
128
129     result = gst_tag_list_peek_string_index (list, tag, 0, &album);
130     if (result != FALSE) {
131       GST_DEBUG ("Setting album to %s", album);
132       apev2tag->setAlbum (String (album, String::UTF8));
133     }
134   } else if (strcmp (tag, GST_TAG_ARTIST) == 0) {
135     const char *artist;
136
137     result = gst_tag_list_peek_string_index (list, tag, 0, &artist);
138     if (result != FALSE) {
139       GST_DEBUG ("Setting artist to %s", artist);
140       apev2tag->setArtist (String (artist, String::UTF8));
141     }
142   } else if (strcmp (tag, GST_TAG_COMPOSER) == 0) {
143     const char *composer;
144
145     result = gst_tag_list_peek_string_index (list, tag, 0, &composer);
146     if (result != FALSE) {
147       GST_DEBUG ("Setting composer to %s", composer);
148       apev2tag->addValue (String ("COMPOSER", String::UTF8),
149           String (composer, String::UTF8));
150     }
151   } else if (strcmp (tag, GST_TAG_GENRE) == 0) {
152     const char *genre;
153
154     result = gst_tag_list_peek_string_index (list, tag, 0, &genre);
155     if (result != FALSE) {
156       GST_DEBUG ("Setting genre to %s", genre);
157       apev2tag->setGenre (String (genre, String::UTF8));
158     }
159   } else if (strcmp (tag, GST_TAG_COMMENT) == 0) {
160     const char *comment;
161
162     result = gst_tag_list_peek_string_index (list, tag, 0, &comment);
163     if (result != FALSE) {
164       GST_DEBUG ("Setting comment to %s", comment);
165       apev2tag->setComment (String (comment, String::UTF8));
166     }
167   } else if (strcmp (tag, GST_TAG_DATE) == 0) {
168     GDate *date;
169
170     result = gst_tag_list_get_date_index (list, tag, 0, &date);
171     if (result != FALSE) {
172       GDateYear year;
173
174       year = g_date_get_year (date);
175       GST_DEBUG ("Setting track year to %d", year);
176       apev2tag->setYear (year);
177       g_date_free (date);
178     }
179   } else if (strcmp (tag, GST_TAG_TRACK_NUMBER) == 0) {
180     guint track_number;
181
182     result = gst_tag_list_get_uint_index (list, tag, 0, &track_number);
183     if (result != FALSE) {
184       guint total_tracks;
185
186       result = gst_tag_list_get_uint_index (list, GST_TAG_TRACK_COUNT,
187           0, &total_tracks);
188       if (result) {
189         gchar *tag_str;
190
191         tag_str = g_strdup_printf ("%d/%d", track_number, total_tracks);
192         GST_DEBUG ("Setting track number to %s", tag_str);
193         apev2tag->addValue (String ("TRACK", String::UTF8),
194             String (tag_str, String::UTF8), true);
195         g_free (tag_str);
196       } else {
197         GST_DEBUG ("Setting track number to %d", track_number);
198         apev2tag->setTrack (track_number);
199       }
200     }
201   } else if (strcmp (tag, GST_TAG_TRACK_COUNT) == 0) {
202     guint n;
203
204     if (gst_tag_list_get_uint_index (list, GST_TAG_TRACK_NUMBER, 0, &n)) {
205       GST_DEBUG ("track-count handled with track-number, skipping");
206     } else if (gst_tag_list_get_uint_index (list, GST_TAG_TRACK_COUNT, 0, &n)) {
207       gchar *tag_str;
208
209       tag_str = g_strdup_printf ("0/%d", n);
210       GST_DEBUG ("Setting track number to %s", tag_str);
211       apev2tag->addValue (String ("TRACK", String::UTF8),
212           String (tag_str, String::UTF8), true);
213       g_free (tag_str);
214     }
215 #if 0
216   } else if (strcmp (tag, GST_TAG_ALBUM_VOLUME_NUMBER) == 0) {
217     guint volume_number;
218
219     result = gst_tag_list_get_uint_index (list, tag, 0, &volume_number);
220
221     if (result != FALSE) {
222       guint volume_count;
223       gchar *tag_str;
224
225       result = gst_tag_list_get_uint_index (list, GST_TAG_ALBUM_VOLUME_COUNT,
226           0, &volume_count);
227
228       if (result) {
229         tag_str = g_strdup_printf ("CD %d/%d", volume_number, volume_count);
230       } else {
231         tag_str = g_strdup_printf ("CD %d", volume_number);
232       }
233
234       GST_DEBUG ("Setting album number to %s", tag_str);
235
236       apev2tag->addValue (String ("MEDIA", String::UTF8),
237           String (tag_str, String::UTF8), true);
238       g_free (tag_str);
239     }
240   } else if (strcmp (tag, GST_TAG_ALBUM_VOLUME_COUNT) == 0) {
241     guint n;
242
243     if (gst_tag_list_get_uint_index (list, GST_TAG_ALBUM_VOLUME_NUMBER, 0, &n)) {
244       GST_DEBUG ("volume-count handled with volume-number, skipping");
245     } else if (gst_tag_list_get_uint_index (list, GST_TAG_ALBUM_VOLUME_COUNT,
246             0, &n)) {
247       gchar *tag_str;
248
249       tag_str = g_strdup_printf ("CD 0/%u", n);
250       GST_DEBUG ("Setting album volume number/count to %s", tag_str);
251
252       apev2tag->addValue (String ("MEDIA", String::UTF8),
253           String (tag_str, String::UTF8), true);
254       g_free (tag_str);
255     }
256 #endif
257   } else if (strcmp (tag, GST_TAG_COPYRIGHT) == 0) {
258     const gchar *copyright;
259
260     result = gst_tag_list_peek_string_index (list, tag, 0, &copyright);
261
262     if (result != FALSE) {
263       GST_DEBUG ("Setting copyright to %s", copyright);
264       apev2tag->addValue (String ("COPYRIGHT", String::UTF8),
265           String (copyright, String::UTF8), true);
266     }
267   } else if (strcmp (tag, GST_TAG_LOCATION) == 0) {
268     const gchar *location;
269
270     result = gst_tag_list_peek_string_index (list, tag, 0, &location);
271
272     if (result != FALSE) {
273       GST_DEBUG ("Setting location to %s", location);
274       apev2tag->addValue (String ("FILE", String::UTF8),
275           String (location, String::UTF8), true);
276     }
277   } else if (strcmp (tag, GST_TAG_ISRC) == 0) {
278     const gchar *isrc;
279
280     result = gst_tag_list_peek_string_index (list, tag, 0, &isrc);
281
282     if (result != FALSE) {
283       GST_DEBUG ("Setting ISRC to %s", isrc);
284       apev2tag->addValue (String ("ISRC", String::UTF8),
285           String (isrc, String::UTF8), true);
286     }
287   } else if (strcmp (tag, GST_TAG_TRACK_GAIN) == 0) {
288     gdouble value;
289
290     result = gst_tag_list_get_double_index (list, tag, 0, &value);
291
292     if (result != FALSE) {
293       gchar *track_gain = (gchar *) g_malloc0 (G_ASCII_DTOSTR_BUF_SIZE);
294
295       track_gain = g_ascii_dtostr (track_gain, G_ASCII_DTOSTR_BUF_SIZE, value);
296       GST_DEBUG ("Setting track gain to %s", track_gain);
297       apev2tag->addValue (String ("REPLAYGAIN_TRACK_GAIN",
298               String::UTF8), String (track_gain, String::UTF8), true);
299       g_free (track_gain);
300     }
301   } else if (strcmp (tag, GST_TAG_TRACK_PEAK) == 0) {
302     gdouble value;
303
304     result = gst_tag_list_get_double_index (list, tag, 0, &value);
305
306     if (result != FALSE) {
307       gchar *track_peak = (gchar *) g_malloc0 (G_ASCII_DTOSTR_BUF_SIZE);
308
309       track_peak = g_ascii_dtostr (track_peak, G_ASCII_DTOSTR_BUF_SIZE, value);
310       GST_DEBUG ("Setting track peak to %s", track_peak);
311       apev2tag->addValue (String ("REPLAYGAIN_TRACK_PEAK",
312               String::UTF8), String (track_peak, String::UTF8), true);
313       g_free (track_peak);
314     }
315   } else if (strcmp (tag, GST_TAG_ALBUM_GAIN) == 0) {
316     gdouble value;
317
318     result = gst_tag_list_get_double_index (list, tag, 0, &value);
319
320     if (result != FALSE) {
321       gchar *album_gain = (gchar *) g_malloc0 (G_ASCII_DTOSTR_BUF_SIZE);
322
323       album_gain = g_ascii_dtostr (album_gain, G_ASCII_DTOSTR_BUF_SIZE, value);
324       GST_DEBUG ("Setting album gain to %s", album_gain);
325       apev2tag->addValue (String ("REPLAYGAIN_ALBUM_GAIN",
326               String::UTF8), String (album_gain, String::UTF8), true);
327       g_free (album_gain);
328     }
329   } else if (strcmp (tag, GST_TAG_ALBUM_PEAK) == 0) {
330     gdouble value;
331
332     result = gst_tag_list_get_double_index (list, tag, 0, &value);
333
334     if (result != FALSE) {
335       gchar *album_peak = (gchar *) g_malloc0 (G_ASCII_DTOSTR_BUF_SIZE);
336
337       album_peak = g_ascii_dtostr (album_peak, G_ASCII_DTOSTR_BUF_SIZE, value);
338       GST_DEBUG ("Setting album peak to %s", album_peak);
339       apev2tag->addValue (String ("REPLAYGAIN_ALBUM_PEAK",
340               String::UTF8), String (album_peak, String::UTF8), true);
341       g_free (album_peak);
342     }
343   } else {
344     GST_WARNING ("Unsupported tag: %s", tag);
345   }
346 }
347
348 static GstBuffer *
349 gst_apev2_mux_render_tag (GstTagMux * mux, const GstTagList * taglist)
350 {
351   APE::Tag apev2tag;
352   ByteVector rendered_tag;
353   GstBuffer *buf;
354   guint tag_size;
355
356   /* Render the tag */
357   gst_tag_list_foreach (taglist, add_one_tag, &apev2tag);
358
359   rendered_tag = apev2tag.render ();
360   tag_size = rendered_tag.size ();
361
362   GST_LOG_OBJECT (mux, "tag size = %d bytes", tag_size);
363
364   /* Create buffer with tag */
365   buf = gst_buffer_new_and_alloc (tag_size);
366   gst_buffer_fill (buf, 0, rendered_tag.data (), tag_size);
367
368   return buf;
369 }
370
371 static GstBuffer *
372 gst_apev2_mux_render_end_tag (GstTagMux * mux, const GstTagList * taglist)
373 {
374   return NULL;
375 }