1 /* GStreamer taglib-based muxer base class
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>
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.
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.
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.
27 #include <gst/gsttagsetter.h>
28 #include <gst/tag/tag.h>
30 #include "gsttaglibmux.h"
32 GST_DEBUG_CATEGORY_STATIC (gst_tag_lib_mux_debug);
33 #define GST_CAT_DEFAULT gst_tag_lib_mux_debug
35 static GstStaticPadTemplate gst_tag_lib_mux_sink_template =
36 GST_STATIC_PAD_TEMPLATE ("sink",
39 GST_STATIC_CAPS ("ANY"));
42 #define gst_tag_lib_mux_parent_class parent_class
43 G_DEFINE_TYPE_WITH_CODE (GstTagLibMux, gst_tag_lib_mux, GST_TYPE_ELEMENT,
44 G_IMPLEMENT_INTERFACE (GST_TYPE_TAG_SETTER, NULL));
46 static GstStateChangeReturn
47 gst_tag_lib_mux_change_state (GstElement * element, GstStateChange transition);
48 static GstFlowReturn gst_tag_lib_mux_chain (GstPad * pad, GstObject * parent,
50 static gboolean gst_tag_lib_mux_sink_event (GstPad * pad, GstObject * parent,
54 gst_tag_lib_mux_finalize (GObject * obj)
56 GstTagLibMux *mux = GST_TAG_LIB_MUX (obj);
58 if (mux->newsegment_ev) {
59 gst_event_unref (mux->newsegment_ev);
60 mux->newsegment_ev = NULL;
63 if (mux->event_tags) {
64 gst_tag_list_free (mux->event_tags);
65 mux->event_tags = NULL;
68 G_OBJECT_CLASS (parent_class)->finalize (obj);
72 gst_tag_lib_mux_class_init (GstTagLibMuxClass * klass)
74 GObjectClass *gobject_class;
75 GstElementClass *gstelement_class;
77 gobject_class = (GObjectClass *) klass;
78 gstelement_class = (GstElementClass *) klass;
80 gobject_class->finalize = gst_tag_lib_mux_finalize;
81 gstelement_class->change_state =
82 GST_DEBUG_FUNCPTR (gst_tag_lib_mux_change_state);
84 gst_element_class_add_pad_template (gstelement_class,
85 gst_static_pad_template_get (&gst_tag_lib_mux_sink_template));
87 GST_DEBUG_CATEGORY_INIT (gst_tag_lib_mux_debug, "taglibmux", 0,
88 "taglib-based muxer");
92 gst_tag_lib_mux_init (GstTagLibMux * mux)
94 GstTagLibMuxClass *mux_class = GST_TAG_LIB_MUX_GET_CLASS (mux);
95 GstElementClass *element_klass = GST_ELEMENT_CLASS (mux_class);
98 /* pad through which data comes in to the element */
100 gst_pad_new_from_static_template (&gst_tag_lib_mux_sink_template, "sink");
101 gst_pad_set_chain_function (mux->sinkpad,
102 GST_DEBUG_FUNCPTR (gst_tag_lib_mux_chain));
103 gst_pad_set_event_function (mux->sinkpad,
104 GST_DEBUG_FUNCPTR (gst_tag_lib_mux_sink_event));
105 gst_element_add_pad (GST_ELEMENT (mux), mux->sinkpad);
107 /* pad through which data goes out of the element */
108 tmpl = gst_element_class_get_pad_template (element_klass, "src");
110 mux->srcpad = gst_pad_new_from_template (tmpl, "src");
111 gst_pad_use_fixed_caps (mux->srcpad);
112 gst_pad_set_caps (mux->srcpad, gst_pad_template_get_caps (tmpl));
113 gst_element_add_pad (GST_ELEMENT (mux), mux->srcpad);
116 mux->render_tag = TRUE;
120 gst_tag_lib_mux_render_tag (GstTagLibMux * mux)
122 GstTagLibMuxClass *klass;
123 GstTagMergeMode merge_mode;
124 GstTagSetter *tagsetter;
126 const GstTagList *tagsetter_tags;
131 tagsetter = GST_TAG_SETTER (mux);
133 tagsetter_tags = gst_tag_setter_get_tag_list (tagsetter);
134 merge_mode = gst_tag_setter_get_tag_merge_mode (tagsetter);
136 GST_LOG_OBJECT (mux, "merging tags, merge mode = %d", merge_mode);
137 GST_LOG_OBJECT (mux, "event tags: %" GST_PTR_FORMAT, mux->event_tags);
138 GST_LOG_OBJECT (mux, "set tags: %" GST_PTR_FORMAT, tagsetter_tags);
140 taglist = gst_tag_list_merge (tagsetter_tags, mux->event_tags, merge_mode);
142 GST_LOG_OBJECT (mux, "final tags: %" GST_PTR_FORMAT, taglist);
144 klass = GST_TAG_LIB_MUX_CLASS (G_OBJECT_GET_CLASS (mux));
146 if (klass->render_tag == NULL)
149 buffer = klass->render_tag (mux, taglist);
154 mux->tag_size = gst_buffer_get_size (buffer);
155 GST_LOG_OBJECT (mux, "tag size = %" G_GSIZE_FORMAT " bytes", mux->tag_size);
157 /* Send newsegment event from byte position 0, so the tag really gets
158 * written to the start of the file, independent of the upstream segment */
159 gst_segment_init (&segment, GST_FORMAT_BYTES);
160 gst_pad_push_event (mux->srcpad, gst_event_new_segment (&segment));
162 /* Send an event about the new tags to downstream elements */
163 /* gst_event_new_tag takes ownership of the list, so no need to unref it */
164 event = gst_event_new_tag (taglist);
165 gst_pad_push_event (mux->srcpad, event);
167 GST_BUFFER_OFFSET (buffer) = 0;
173 GST_ERROR_OBJECT (mux, "Subclass does not implement render_tag vfunc!");
174 gst_tag_list_free (taglist);
180 GST_ERROR_OBJECT (mux, "Failed to render tag");
181 gst_tag_list_free (taglist);
187 gst_tag_lib_mux_adjust_event_offsets (GstTagLibMux * mux,
188 const GstEvent * newsegment_event)
190 gint64 start, stop, cur;
193 gst_event_copy_segment ((GstEvent *) newsegment_event, &segment);
195 g_assert (segment.format == GST_FORMAT_BYTES);
197 if (segment.start != -1)
198 start += mux->tag_size;
199 if (segment.stop != -1)
200 stop += mux->tag_size;
201 if (segment.time != -1)
202 cur += mux->tag_size;
204 GST_DEBUG_OBJECT (mux, "adjusting newsegment event offsets to start=%"
205 G_GUINT64_FORMAT ", stop=%" G_GUINT64_FORMAT ", cur=%" G_GUINT64_FORMAT
206 " (delta = +%" G_GSIZE_FORMAT ")",
207 segment.start, segment.stop, segment.time, mux->tag_size);
209 return gst_event_new_segment (&segment);
213 gst_tag_lib_mux_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
215 GstTagLibMux *mux = GST_TAG_LIB_MUX (parent);
217 if (mux->render_tag) {
219 GstBuffer *tag_buffer;
222 GST_INFO_OBJECT (mux, "Adding tags to stream");
223 tag_buffer = gst_tag_lib_mux_render_tag (mux);
224 if (tag_buffer == NULL)
226 ret = gst_pad_push (mux->srcpad, tag_buffer);
227 if (ret != GST_FLOW_OK) {
228 GST_DEBUG_OBJECT (mux, "flow: %s", gst_flow_get_name (ret));
229 gst_buffer_unref (buffer);
233 /* Now send the cached newsegment event that we got from upstream */
234 if (mux->newsegment_ev) {
235 GST_DEBUG_OBJECT (mux, "sending cached newsegment event");
236 gst_pad_push_event (mux->srcpad,
237 gst_tag_lib_mux_adjust_event_offsets (mux, mux->newsegment_ev));
238 gst_event_unref (mux->newsegment_ev);
239 mux->newsegment_ev = NULL;
241 /* upstream sent no newsegment event or only one in a non-BYTE format */
244 mux->render_tag = FALSE;
246 /* we have data flow, so pad is active and caps can be set */
247 tcaps = gst_pad_get_pad_template_caps (mux->srcpad);
248 gst_pad_set_caps (mux->srcpad, tcaps);
249 gst_caps_unref (tcaps);
252 buffer = gst_buffer_make_writable (buffer);
254 if (GST_BUFFER_OFFSET (buffer) != GST_BUFFER_OFFSET_NONE) {
255 GST_LOG_OBJECT (mux, "Adjusting buffer offset from %" G_GUINT64_FORMAT
256 " to %" G_GUINT64_FORMAT, GST_BUFFER_OFFSET (buffer),
257 GST_BUFFER_OFFSET (buffer) + mux->tag_size);
258 GST_BUFFER_OFFSET (buffer) += mux->tag_size;
261 return gst_pad_push (mux->srcpad, buffer);
266 GST_ELEMENT_ERROR (mux, LIBRARY, ENCODE, (NULL), (NULL));
267 return GST_FLOW_ERROR;
272 gst_tag_lib_mux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
277 mux = GST_TAG_LIB_MUX (parent);
280 switch (GST_EVENT_TYPE (event)) {
284 gst_event_parse_tag (event, &tags);
286 GST_INFO_OBJECT (mux, "Got tag event: %" GST_PTR_FORMAT, tags);
288 if (mux->event_tags != NULL) {
289 gst_tag_list_insert (mux->event_tags, tags, GST_TAG_MERGE_REPLACE);
291 mux->event_tags = gst_tag_list_copy (tags);
294 GST_INFO_OBJECT (mux, "Event tags are now: %" GST_PTR_FORMAT,
297 /* just drop the event, we'll push a new tag event in render_tag */
298 gst_event_unref (event);
302 case GST_EVENT_SEGMENT:{
303 const GstSegment *segment;
305 gst_event_parse_segment (event, &segment);
307 if (segment->format != GST_FORMAT_BYTES) {
308 GST_WARNING_OBJECT (mux, "dropping newsegment event in %s format",
309 gst_format_get_name (segment->format));
310 gst_event_unref (event);
314 if (mux->render_tag) {
315 /* we have not rendered the tag yet, which means that we don't know
316 * how large it is going to be yet, so we can't adjust the offsets
317 * here at this point and need to cache the newsegment event for now
318 * (also, there could be tag events coming after this newsegment event
319 * and before the first buffer). */
320 if (mux->newsegment_ev) {
321 GST_WARNING_OBJECT (mux, "discarding old cached newsegment event");
322 gst_event_unref (mux->newsegment_ev);
325 GST_LOG_OBJECT (mux, "caching newsegment event for later");
326 mux->newsegment_ev = event;
328 GST_DEBUG_OBJECT (mux, "got newsegment event, adjusting offsets");
329 gst_pad_push_event (mux->srcpad,
330 gst_tag_lib_mux_adjust_event_offsets (mux, event));
331 gst_event_unref (event);
338 result = gst_pad_event_default (pad, parent, event);
346 static GstStateChangeReturn
347 gst_tag_lib_mux_change_state (GstElement * element, GstStateChange transition)
350 GstStateChangeReturn result;
352 mux = GST_TAG_LIB_MUX (element);
354 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
355 if (result != GST_STATE_CHANGE_SUCCESS) {
359 switch (transition) {
360 case GST_STATE_CHANGE_PAUSED_TO_READY:{
361 if (mux->newsegment_ev) {
362 gst_event_unref (mux->newsegment_ev);
363 mux->newsegment_ev = NULL;
365 if (mux->event_tags) {
366 gst_tag_list_free (mux->event_tags);
367 mux->event_tags = NULL;
370 mux->render_tag = TRUE;
381 plugin_init (GstPlugin * plugin)
383 return (gst_id3v2_mux_plugin_init (plugin)
384 && gst_apev2_mux_plugin_init (plugin));
387 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
390 "Tag writing plug-in based on taglib",
391 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);