1
/* GStreamer taglib-based ID3 muxer
2
* (c) 2006 Christophe Fergeau <teuf@gnome.org>
4
* This library is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU Library General Public
6
* License as published by the Free Software Foundation; either
7
* version 2 of the License, or (at your option) any later version.
9
* This library is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* Library General Public License for more details.
14
* You should have received a copy of the GNU Library General Public
15
* License along with this library; if not, write to the
16
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17
* Boston, MA 02111-1307, USA.
21
* SECTION:element-tagid3v2mux
22
* @see_also: #GstID3Demux, #GstTagSetter
26
* This element adds ID3v2 tags to the beginning of a stream using the taglib
27
* library. More precisely, the tags written are ID3 version 2.4.0 tags (which
28
* means in practice that some hardware players or outdated programs might not
29
* be able to read them properly).
32
* Applications can set the tags to write using the #GstTagSetter interface.
33
* Tags sent by upstream elements will be picked up automatically (and merged
34
* according to the merge mode set via the tag setter interface).
37
* Here is a simple pipeline that transcodes a file from Ogg/Vorbis to mp3
38
* format with an ID3v2 that contains the same as the the Ogg/Vorbis file:
40
* gst-launch -v filesrc location=foo.ogg ! decodebin ! audioconvert ! lame ! tagid3v2mux ! filesink location=foo.mp3
52
#include <textidentificationframe.h>
53
#include <uniquefileidentifierframe.h>
55
#include <gst/gsttagsetter.h>
56
#include <gst/tag/tag.h>
57
#include "gsttaglib.h"
59
using namespace TagLib;
61
GST_DEBUG_CATEGORY_STATIC (sj_tag_lib_mux_debug);
62
#define GST_CAT_DEFAULT sj_tag_lib_mux_debug
65
sj_tag_lib_mux_iface_init (GType taglib_type)
67
static const GInterfaceInfo tag_setter_info = {
73
g_type_add_interface_static (taglib_type, GST_TYPE_TAG_SETTER,
77
GST_BOILERPLATE_FULL (SjTagLibMux, sj_tag_lib_mux,
78
GstElement, GST_TYPE_ELEMENT, sj_tag_lib_mux_iface_init);
81
static GstStateChangeReturn
82
sj_tag_lib_mux_change_state (GstElement * element, GstStateChange transition);
83
static GstFlowReturn sj_tag_lib_mux_chain (GstPad * pad, GstBuffer * buffer);
84
static gboolean sj_tag_lib_mux_sink_event (GstPad * pad, GstEvent * event);
88
sj_tag_lib_mux_finalize (GObject * obj)
90
SjTagLibMux *taglib = SJ_TAGLIB_MUX (obj);
93
gst_tag_list_free (taglib->tags);
96
G_OBJECT_CLASS (parent_class)->finalize (obj);
100
static GstStaticPadTemplate sj_tag_lib_mux_sink_template =
101
GST_STATIC_PAD_TEMPLATE ("sink",
104
GST_STATIC_CAPS ("audio/mpeg"));
107
static GstStaticPadTemplate sj_tag_lib_mux_src_template =
108
GST_STATIC_PAD_TEMPLATE ("src",
111
GST_STATIC_CAPS ("application/x-id3"));
115
sj_tag_lib_mux_base_init (gpointer g_class)
117
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
119
static GstElementDetails sj_tag_lib_mux_details = {
121
"Formatter/Metadata",
122
"Adds an ID3v2 header to the beginning of MP3 files",
123
"Christophe Fergeau <teuf@gnome.org>"
127
gst_element_class_add_pad_template (element_class,
128
gst_static_pad_template_get (&sj_tag_lib_mux_src_template));
129
gst_element_class_add_pad_template (element_class,
130
gst_static_pad_template_get (&sj_tag_lib_mux_sink_template));
131
gst_element_class_set_details (element_class, &sj_tag_lib_mux_details);
135
sj_tag_lib_mux_class_init (SjTagLibMuxClass * klass)
137
GObjectClass *gobject_class;
138
GstElementClass *gstelement_class;
140
gobject_class = (GObjectClass *) klass;
141
gstelement_class = (GstElementClass *) klass;
143
gobject_class->finalize = GST_DEBUG_FUNCPTR (sj_tag_lib_mux_finalize);
144
gstelement_class->change_state =
145
GST_DEBUG_FUNCPTR (sj_tag_lib_mux_change_state);
149
sj_tag_lib_mux_init (SjTagLibMux * taglib,
150
SjTagLibMuxClass * taglibmux_class)
152
GstElementClass *klass = GST_ELEMENT_CLASS (taglibmux_class);
154
/* pad through which data comes in to the element */
156
gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
158
gst_pad_set_setcaps_function (taglib->sinkpad,
159
GST_DEBUG_FUNCPTR (gst_pad_proxy_setcaps));
160
gst_pad_set_chain_function (taglib->sinkpad,
161
GST_DEBUG_FUNCPTR (sj_tag_lib_mux_chain));
162
gst_pad_set_event_function (taglib->sinkpad,
163
GST_DEBUG_FUNCPTR (sj_tag_lib_mux_sink_event));
164
gst_element_add_pad (GST_ELEMENT (taglib), taglib->sinkpad);
166
/* pad through which data goes out of the element */
168
gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
170
gst_element_add_pad (GST_ELEMENT (taglib), taglib->srcpad);
172
taglib->render_tag = TRUE;
176
add_one_txxx_musicbrainz_tag (ID3v2::Tag * id3v2tag, const gchar * spec_id,
177
const gchar * realworld_id, const gchar * id_str)
179
ID3v2::UserTextIdentificationFrame * frame;
184
GST_DEBUG ("Setting %s to %s", GST_STR_NULL (spec_id), id_str);
187
frame = new ID3v2::UserTextIdentificationFrame (String::Latin1);
188
id3v2tag->addFrame (frame);
189
frame->setDescription (spec_id);
190
frame->setText (id_str);
194
frame = new ID3v2::UserTextIdentificationFrame (String::Latin1);
195
id3v2tag->addFrame (frame);
196
frame->setDescription (realworld_id);
197
frame->setText (id_str);
202
add_one_tag (const GstTagList * list, const gchar * tag, gpointer user_data)
204
ID3v2::Tag * id3v2tag = (ID3v2::Tag *) user_data;
207
/* FIXME: if there are several values set for the same tag, this won't
208
* work, only the first value will be taken into account
210
if (strcmp (tag, GST_TAG_TITLE) == 0) {
213
result = gst_tag_list_get_string_index (list, tag, 0, &title);
214
if (result != FALSE) {
215
GST_DEBUG ("Setting title to %s", title);
216
id3v2tag->setTitle (String::String (title, String::UTF8));
219
} else if (strcmp (tag, GST_TAG_ALBUM) == 0) {
222
result = gst_tag_list_get_string_index (list, tag, 0, &album);
223
if (result != FALSE) {
224
GST_DEBUG ("Setting album to %s", album);
225
id3v2tag->setAlbum (String::String (album, String::UTF8));
228
} else if (strcmp (tag, GST_TAG_ARTIST) == 0) {
231
result = gst_tag_list_get_string_index (list, tag, 0, &artist);
232
if (result != FALSE) {
233
GST_DEBUG ("Setting artist to %s", artist);
234
id3v2tag->setArtist (String::String (artist, String::UTF8));
237
} else if (strcmp (tag, GST_TAG_GENRE) == 0) {
240
result = gst_tag_list_get_string_index (list, tag, 0, &genre);
241
if (result != FALSE) {
242
GST_DEBUG ("Setting genre to %s", genre);
243
id3v2tag->setGenre (String::String (genre, String::UTF8));
246
} else if (strcmp (tag, GST_TAG_COMMENT) == 0) {
249
result = gst_tag_list_get_string_index (list, tag, 0, &comment);
250
if (result != FALSE) {
251
GST_DEBUG ("Setting comment to %s", comment);
252
id3v2tag->setComment (String::String (comment, String::UTF8));
255
} else if (strcmp (tag, GST_TAG_DATE) == 0) {
258
result = gst_tag_list_get_date_index (list, tag, 0, &date);
259
if (result != FALSE) {
262
year = g_date_get_year (date);
263
GST_DEBUG ("Setting track year to %d", year);
264
id3v2tag->setYear (year);
267
} else if (strcmp (tag, GST_TAG_TRACK_NUMBER) == 0) {
270
result = gst_tag_list_get_uint_index (list, tag, 0, &track_number);
271
if (result != FALSE) {
274
result = gst_tag_list_get_uint_index (list, GST_TAG_TRACK_COUNT,
279
ID3v2::TextIdentificationFrame * frame;
281
frame = new ID3v2::TextIdentificationFrame ("TRCK", String::UTF8);
282
tag_str = g_strdup_printf ("%d/%d", track_number, total_tracks);
283
GST_DEBUG ("Setting track number to %s", tag_str);
284
id3v2tag->addFrame (frame);
285
frame->setText (tag_str);
288
GST_DEBUG ("Setting track number to %d", track_number);
289
id3v2tag->setTrack (track_number);
292
} else if (strcmp (tag, GST_TAG_ALBUM_VOLUME_NUMBER) == 0) {
295
result = gst_tag_list_get_uint_index (list, tag, 0, &volume_number);
297
if (result != FALSE) {
301
ID3v2::TextIdentificationFrame * frame;
303
frame = new ID3v2::TextIdentificationFrame ("TPOS", String::UTF8);
304
result = gst_tag_list_get_uint_index (list, GST_TAG_ALBUM_VOLUME_COUNT,
307
tag_str = g_strdup_printf ("%d/%d", volume_number, volume_count);
309
tag_str = g_strdup_printf ("%d", volume_number);
312
id3v2tag->addFrame (frame);
313
frame->setText (tag_str);
315
GST_DEBUG ("Setting album number to %s", tag_str);
317
} else if (strcmp (tag, GST_TAG_COPYRIGHT) == 0) {
320
result = gst_tag_list_get_string_index (list, tag, 0, ©right);
322
if (result != FALSE) {
323
ID3v2::TextIdentificationFrame * frame;
325
frame = new ID3v2::TextIdentificationFrame ("TCOP", String::UTF8);
327
id3v2tag->addFrame (frame);
328
frame->setText (copyright);
330
GST_DEBUG ("Setting copyright to %s", copyright);
332
} else if (strcmp (tag, GST_TAG_MUSICBRAINZ_ARTISTID) == 0) {
335
if (gst_tag_list_get_string_index (list, tag, 0, &id_str) && id_str) {
336
add_one_txxx_musicbrainz_tag (id3v2tag, "MusicBrainz Artist Id",
337
"musicbrainz_artistid", id_str);
340
} else if (strcmp (tag, GST_TAG_MUSICBRAINZ_ALBUMID) == 0) {
343
if (gst_tag_list_get_string_index (list, tag, 0, &id_str) && id_str) {
344
add_one_txxx_musicbrainz_tag (id3v2tag, "MusicBrainz Album Id",
345
"musicbrainz_albumid", id_str);
348
} else if (strcmp (tag, GST_TAG_MUSICBRAINZ_ALBUMARTISTID) == 0) {
351
if (gst_tag_list_get_string_index (list, tag, 0, &id_str) && id_str) {
352
add_one_txxx_musicbrainz_tag (id3v2tag, "MusicBrainz Album Artist Id",
353
"musicbrainz_albumartistid", id_str);
356
} else if (strcmp (tag, GST_TAG_MUSICBRAINZ_TRMID) == 0) {
359
if (gst_tag_list_get_string_index (list, tag, 0, &id_str) && id_str) {
360
add_one_txxx_musicbrainz_tag (id3v2tag, "MusicBrainz TRM Id",
361
"musicbrainz_trmid", id_str);
364
} else if (strcmp (tag, GST_TAG_MUSICBRAINZ_TRACKID) == 0) {
367
if (gst_tag_list_get_string_index (list, tag, 0, &id_str) && id_str) {
368
ID3v2::UniqueFileIdentifierFrame * frame;
370
GST_DEBUG ("Setting Musicbrainz Track Id to %s", id_str);
372
frame = new ID3v2::UniqueFileIdentifierFrame ("http://musicbrainz.org",
374
id3v2tag->addFrame (frame);
378
GST_WARNING ("Unsupported tag: %s", tag);
384
sj_tag_lib_mux_render_tag (SjTagLibMux * taglib)
387
ByteVector rendered_tag;
389
GstTagSetter *tagsetter = GST_TAG_SETTER (taglib);
393
if (taglib->tags != NULL) {
394
taglist = gst_tag_list_copy (taglib->tags);
396
taglist = gst_tag_list_new ();
399
if (gst_tag_setter_get_tag_list (tagsetter)) {
400
gst_tag_list_insert (taglist,
401
gst_tag_setter_get_tag_list (tagsetter),
402
gst_tag_setter_get_tag_merge_mode (tagsetter));
407
gst_tag_list_foreach (taglist, add_one_tag, &id3v2tag);
408
rendered_tag = id3v2tag.render ();
409
taglib->tag_size = rendered_tag.size ();
410
buffer = gst_buffer_new_and_alloc (rendered_tag.size ());
411
memcpy (GST_BUFFER_DATA (buffer), rendered_tag.data (), rendered_tag.size ());
412
gst_buffer_set_caps (buffer, GST_PAD_CAPS (taglib->srcpad));
413
/* gst_util_dump_mem (GST_BUFFER_DATA (buffer), rendered_tag.size()); */
415
/* Send an event about the new tags to downstream elements */
416
/* gst_event_new_tag takes ownership of the list, so no need to unref it */
417
event = gst_event_new_tag (taglist);
418
gst_pad_push_event (taglib->srcpad, event);
425
sj_tag_lib_mux_chain (GstPad * pad, GstBuffer * buffer)
427
SjTagLibMux *taglib = SJ_TAGLIB_MUX (GST_OBJECT_PARENT (pad));
429
if (taglib->render_tag) {
432
GST_INFO ("Adding tags to stream");
433
ret = gst_pad_push (taglib->srcpad, sj_tag_lib_mux_render_tag (taglib));
434
if (ret != GST_FLOW_OK) {
435
gst_buffer_unref (buffer);
438
taglib->render_tag = FALSE;
441
gst_buffer_set_caps (buffer, GST_PAD_CAPS (taglib->srcpad));
442
return gst_pad_push (taglib->srcpad, buffer);
446
sj_tag_lib_mux_sink_event (GstPad * pad, GstEvent * event)
451
taglib = SJ_TAGLIB_MUX (gst_pad_get_parent (pad));
454
switch (GST_EVENT_TYPE (event)) {
459
GST_INFO ("Got tag event");
461
gst_event_parse_tag (event, &tags);
462
if (taglib->tags != NULL) {
463
/* FIXME: which policy is the best here? PREPEND or something else? */
464
gst_tag_list_insert (taglib->tags, tags, GST_TAG_MERGE_PREPEND);
466
taglib->tags = gst_tag_list_copy (tags);
468
/* We'll push a new tag event in render_tag */
469
gst_event_unref (event);
473
case GST_EVENT_NEWSEGMENT:
474
if (taglib->tag_size == 0) {
475
result = gst_pad_push_event (taglib->srcpad, event);
480
gint64 value, end_value, base;
482
gst_event_parse_new_segment (event, &update, &rate, &format,
483
&value, &end_value, &base);
484
gst_event_unref (event);
485
if (format == GST_FORMAT_BYTES && gst_pad_is_linked (taglib->srcpad)) {
488
GST_INFO ("Adjusting NEW_SEGMENT event by %d", taglib->tag_size);
489
value += taglib->tag_size;
490
if (end_value != -1) {
491
end_value += taglib->tag_size;
494
new_event = gst_event_new_new_segment (update, rate, format,
495
value, end_value, base);
496
result = gst_pad_push_event (taglib->srcpad, new_event);
504
result = gst_pad_event_default (pad, event);
507
gst_object_unref (GST_OBJECT (taglib));
513
static GstStateChangeReturn
514
sj_tag_lib_mux_change_state (GstElement * element, GstStateChange transition)
517
GstStateChangeReturn result;
519
taglib = SJ_TAGLIB_MUX (element);
521
result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
522
if (result != GST_STATE_CHANGE_SUCCESS) {
526
switch (transition) {
527
case GST_STATE_CHANGE_PAUSED_TO_READY:
529
gst_tag_list_free (taglib->tags);
532
taglib->tag_size = 0;
533
taglib->render_tag = TRUE;
544
plugin_init (GstPlugin * plugin)
546
if (!gst_element_register (plugin, "id3mux", GST_RANK_NONE,
550
GST_DEBUG_CATEGORY_INIT (sj_tag_lib_mux_debug, "taglibmux", 0,
553
gst_tag_register_musicbrainz_tags ();
558
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
561
"Tag-writing plug-in based on taglib",
562
plugin_init, VERSION, "LGPL", PACKAGE_NAME, PACKAGE);
565
id3mux_register(void)
567
_gst_plugin_register_static (&gst_plugin_desc);