gstid3mux.c 7.12 KB
Newer Older
1 2 3
/* GStreamer ID3 v1 and v2 muxer
 *
 * Copyright (C) 2006 Christophe Fergeau <teuf@gnome.org>
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
4
 * Copyright (C) 2006-2012 Tim-Philipp Müller <tim centricular net>
5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * Copyright (C) 2009 Pioneers of the Inevitable <songbird@songbirdnest.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
19 20
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
21 22 23
 */

/**
24
 * SECTION:element-id3mux
25
 * @title: id3mux
26 27 28 29
 * @see_also: #GstID3Demux, #GstTagSetter
 *
 * This element adds ID3v2 tags to the beginning of a stream, and ID3v1 tags
 * to the end.
30
 *
31 32 33 34 35 36 37
 * It defaults to writing ID3 version 2.3.0 tags (since those are the most
 * widely supported), but can optionally write version 2.4.0 tags.
 *
 * Applications can set the tags to write using the #GstTagSetter interface.
 * Tags sent by upstream elements will be picked up automatically (and merged
 * according to the merge mode set via the tag setter interface).
 *
38
 * ## Example pipelines
39
 * |[
40
 * gst-launch-1.0 -v filesrc location=foo.ogg ! decodebin ! audioconvert ! id3mux ! filesink location=foo.mp3
41 42 43 44
 * ]| A pipeline that transcodes a file from Ogg/Vorbis to mp3 format with
 * ID3 tags that contain the same metadata as the the Ogg/Vorbis file.
 * Make sure the Ogg/Vorbis file actually has comments to preserve.
 * |[
45
 * gst-launch-1.0 -m filesrc location=foo.mp3 ! id3demux ! fakesink silent=TRUE
46
 * ]| Verify that tags have been written.
47
 *
48 49 50 51 52 53
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

54
#include "gstid3mux.h"
55 56 57 58
#include <gst/tag/tag.h>

#include <string.h>

59 60
GST_DEBUG_CATEGORY (gst_id3_mux_debug);
#define GST_CAT_DEFAULT gst_id3_mux_debug
61 62 63

enum
{
64 65 66 67
  PROP_0,
  PROP_WRITE_V1,
  PROP_WRITE_V2,
  PROP_V2_MAJOR_VERSION
68 69
};

70
#define DEFAULT_WRITE_V1 FALSE
71 72 73
#define DEFAULT_WRITE_V2 TRUE
#define DEFAULT_V2_MAJOR_VERSION 3

74 75 76 77 78
static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("ANY"));

79 80 81 82 83
static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("application/x-id3"));

Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
84
G_DEFINE_TYPE (GstId3Mux, gst_id3_mux, GST_TYPE_TAG_MUX);
85

86
static GstBuffer *gst_id3_mux_render_v2_tag (GstTagMux * mux,
87
    const GstTagList * taglist);
88
static GstBuffer *gst_id3_mux_render_v1_tag (GstTagMux * mux,
89
    const GstTagList * taglist);
90

91
static void gst_id3_mux_set_property (GObject * object, guint prop_id,
92
    const GValue * value, GParamSpec * pspec);
93
static void gst_id3_mux_get_property (GObject * object, guint prop_id,
94 95 96
    GValue * value, GParamSpec * pspec);

static void
97
gst_id3_mux_class_init (GstId3MuxClass * klass)
98
{
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
99
  GstElementClass *element_class = (GstElementClass *) klass;
100 101
  GObjectClass *gobject_class = (GObjectClass *) klass;

102 103
  gobject_class->set_property = gst_id3_mux_set_property;
  gobject_class->get_property = gst_id3_mux_get_property;
104

105
  g_object_class_install_property (gobject_class, PROP_WRITE_V1,
106 107
      g_param_spec_boolean ("write-v1", "Write id3v1 tag",
          "Write an id3v1 tag at the end of the file", DEFAULT_WRITE_V1,
108
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
109

110
  g_object_class_install_property (gobject_class, PROP_WRITE_V2,
111 112
      g_param_spec_boolean ("write-v2", "Write id3v2 tag",
          "Write an id3v2 tag at the start of the file", DEFAULT_WRITE_V2,
113
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
114

115
  g_object_class_install_property (gobject_class, PROP_V2_MAJOR_VERSION,
116 117 118
      g_param_spec_int ("v2-version", "Version (3 or 4) of id3v2 tag",
          "Set version (3 for id3v2.3, 4 for id3v2.4) of id3v2 tags",
          3, 4, DEFAULT_V2_MAJOR_VERSION,
119
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
120 121

  GST_TAG_MUX_CLASS (klass)->render_start_tag =
122
      GST_DEBUG_FUNCPTR (gst_id3_mux_render_v2_tag);
123 124
  GST_TAG_MUX_CLASS (klass)->render_end_tag =
      GST_DEBUG_FUNCPTR (gst_id3_mux_render_v1_tag);
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
125

126
  gst_element_class_set_static_metadata (element_class,
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
127 128 129 130 131
      "ID3 v1 and v2 Muxer", "Formatter/Metadata",
      "Adds an ID3v2 header and ID3v1 footer to a file",
      "Michael Smith <msmith@songbirdnest.com>, "
      "Tim-Philipp Müller <tim centricular net>");

132 133
  gst_element_class_add_static_pad_template (element_class, &sink_template);
  gst_element_class_add_static_pad_template (element_class, &src_template);
134 135 136
}

static void
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
137
gst_id3_mux_init (GstId3Mux * id3mux)
138 139 140 141 142 143 144 145
{
  id3mux->write_v1 = DEFAULT_WRITE_V1;
  id3mux->write_v2 = DEFAULT_WRITE_V2;

  id3mux->v2_major_version = DEFAULT_V2_MAJOR_VERSION;
}

static void
146
gst_id3_mux_set_property (GObject * object, guint prop_id,
147 148
    const GValue * value, GParamSpec * pspec)
{
149
  GstId3Mux *mux = GST_ID3_MUX (object);
150 151

  switch (prop_id) {
152
    case PROP_WRITE_V1:
153 154
      mux->write_v1 = g_value_get_boolean (value);
      break;
155
    case PROP_WRITE_V2:
156 157
      mux->write_v2 = g_value_get_boolean (value);
      break;
158
    case PROP_V2_MAJOR_VERSION:
159 160 161 162 163 164 165 166 167
      mux->v2_major_version = g_value_get_int (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
168
gst_id3_mux_get_property (GObject * object, guint prop_id,
169 170
    GValue * value, GParamSpec * pspec)
{
171
  GstId3Mux *mux = GST_ID3_MUX (object);
172 173

  switch (prop_id) {
174
    case PROP_WRITE_V1:
175 176
      g_value_set_boolean (value, mux->write_v1);
      break;
177
    case PROP_WRITE_V2:
178 179
      g_value_set_boolean (value, mux->write_v2);
      break;
180
    case PROP_V2_MAJOR_VERSION:
181 182 183 184 185 186 187 188 189
      g_value_set_int (value, mux->v2_major_version);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static GstBuffer *
190
gst_id3_mux_render_v2_tag (GstTagMux * mux, const GstTagList * taglist)
191
{
192
  GstId3Mux *id3mux = GST_ID3_MUX (mux);
193 194

  if (id3mux->write_v2)
195
    return id3_mux_render_v2_tag (mux, taglist, id3mux->v2_major_version);
196 197 198 199 200
  else
    return NULL;
}

static GstBuffer *
201
gst_id3_mux_render_v1_tag (GstTagMux * mux, const GstTagList * taglist)
202
{
203
  GstId3Mux *id3mux = GST_ID3_MUX (mux);
204 205

  if (id3mux->write_v1)
206
    return id3_mux_render_v1_tag (mux, taglist);
207 208 209 210 211 212 213
  else
    return NULL;
}

static gboolean
plugin_init (GstPlugin * plugin)
{
214 215 216
  GST_DEBUG_CATEGORY_INIT (gst_id3_mux_debug, "id3mux", 0,
      "ID3 v1 and v2 tag muxer");

217 218
  if (!gst_element_register (plugin, "id3mux", GST_RANK_PRIMARY,
          GST_TYPE_ID3_MUX))
219 220 221 222 223 224 225 226 227
    return FALSE;

  gst_tag_register_musicbrainz_tags ();

  return TRUE;
}

GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
    GST_VERSION_MINOR,
228
    id3tag,
229 230
    "ID3 v1 and v2 muxing plugin",
    plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);