1 /*
2  * Copyright (C) 2006 James Livingston <doclivingston@gmail.com>
3  *
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.
8  *
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.
13  *
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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 /**
21  * SECTION:element-vorbistag
22  * @title: vorbistag
23  * @see_also: #oggdemux, #oggmux, #vorbisparse, #GstTagSetter
24  *
25  * The vorbistags element can change the tag contained within a raw
26  * vorbis stream. Specifically, it modifies the comments header packet
27  * of the vorbis stream.
28  *
29  * The element will also process the stream as the #vorbisparse element does
30  * so it can be used when remuxing an Ogg Vorbis stream, without additional
31  * elements.
32  *
33  * Applications can set the tags to write using the #GstTagSetter interface.
34  * Tags contained withing the vorbis bitstream will be picked up
35  * automatically (and merged according to the merge mode set via the tag
36  * setter interface).
37  *
38  * ## Example pipelines
39  * |[
40  * gst-launch-1.0 -v filesrc location=foo.ogg ! oggdemux ! vorbistag ! oggmux ! filesink location=bar.ogg
41  * ]|
42  *  This element is not useful with gst-launch-1.0, because it does not support
43  * setting the tags on a #GstTagSetter interface. Conceptually, the element
44  * will usually be used in this order though.
45  *
46  */
47 
48 #ifdef HAVE_CONFIG_H
49 #  include "config.h"
50 #endif
51 
52 #include <glib.h>
53 #include <gst/tag/tag.h>
54 #include <gst/gsttagsetter.h>
55 
56 #include <vorbis/codec.h>
57 
58 #include "gstvorbistag.h"
59 
60 
61 GST_DEBUG_CATEGORY_EXTERN (vorbisparse_debug);
62 #define GST_CAT_DEFAULT vorbisparse_debug
63 
64 static GstFlowReturn gst_vorbis_tag_parse_packet (GstVorbisParse * parse,
65     GstBuffer * buffer);
66 
67 #define gst_vorbis_tag_parent_class parent_class
68 G_DEFINE_TYPE_WITH_CODE (GstVorbisTag, gst_vorbis_tag,
69     GST_TYPE_VORBIS_PARSE, G_IMPLEMENT_INTERFACE (GST_TYPE_TAG_SETTER, NULL));
70 
71 static void
gst_vorbis_tag_class_init(GstVorbisTagClass * klass)72 gst_vorbis_tag_class_init (GstVorbisTagClass * klass)
73 {
74   GstVorbisParseClass *vorbisparse_class = GST_VORBIS_PARSE_CLASS (klass);
75   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
76 
77   gst_element_class_set_static_metadata (element_class,
78       "VorbisTag", "Formatter/Metadata",
79       "Retags vorbis streams", "James Livingston <doclivingston@gmail.com>");
80 
81   vorbisparse_class->parse_packet = gst_vorbis_tag_parse_packet;
82 }
83 
84 static void
gst_vorbis_tag_init(GstVorbisTag * tagger)85 gst_vorbis_tag_init (GstVorbisTag * tagger)
86 {
87   /* nothing to do */
88 }
89 
90 
91 static GstFlowReturn
gst_vorbis_tag_parse_packet(GstVorbisParse * parse,GstBuffer * buffer)92 gst_vorbis_tag_parse_packet (GstVorbisParse * parse, GstBuffer * buffer)
93 {
94   GstTagList *old_tags, *new_tags;
95   const GstTagList *user_tags;
96   GstVorbisTag *tagger;
97   gchar *encoder = NULL;
98   GstBuffer *new_buf;
99   GstMapInfo map;
100   gboolean do_parse = FALSE;
101 
102   gst_buffer_map (buffer, &map, GST_MAP_READ);
103   /* just pass everything except the comments packet */
104   if (map.size >= 1 && map.data[0] != 0x03)
105     do_parse = TRUE;
106   gst_buffer_unmap (buffer, &map);
107 
108   if (do_parse) {
109     return GST_VORBIS_PARSE_CLASS (parent_class)->parse_packet (parse, buffer);
110   }
111 
112   tagger = GST_VORBIS_TAG (parse);
113 
114   old_tags =
115       gst_tag_list_from_vorbiscomment_buffer (buffer, (guint8 *) "\003vorbis",
116       7, &encoder);
117   user_tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (tagger));
118 
119   /* build new tag list */
120   new_tags = gst_tag_list_merge (user_tags, old_tags,
121       gst_tag_setter_get_tag_merge_mode (GST_TAG_SETTER (tagger)));
122   gst_tag_list_unref (old_tags);
123 
124   new_buf =
125       gst_tag_list_to_vorbiscomment_buffer (new_tags, (guint8 *) "\003vorbis",
126       7, encoder);
127   gst_buffer_copy_into (new_buf, buffer, GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
128 
129   gst_tag_list_unref (new_tags);
130   g_free (encoder);
131   gst_buffer_unref (buffer);
132 
133   return GST_VORBIS_PARSE_CLASS (parent_class)->parse_packet (parse, new_buf);
134 }
135