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