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 gst_tag_lib_mux_iface_init (GType taglib_type)
44 static const GInterfaceInfo tag_setter_info = {
50 g_type_add_interface_static (taglib_type, GST_TYPE_TAG_SETTER,
54 GST_BOILERPLATE_FULL (GstTagLibMux, gst_tag_lib_mux,
55 GstElement, GST_TYPE_ELEMENT, gst_tag_lib_mux_iface_init);
58 static GstStateChangeReturn
59 gst_tag_lib_mux_change_state (GstElement * element, GstStateChange transition);
60 static GstFlowReturn gst_tag_lib_mux_chain (GstPad * pad, GstBuffer * buffer);
61 static gboolean gst_tag_lib_mux_sink_event (GstPad * pad, GstEvent * event);
64 gst_tag_lib_mux_finalize (GObject * obj)
66 GstTagLibMux *mux = GST_TAG_LIB_MUX (obj);
68 if (mux->newsegment_ev) {
69 gst_event_unref (mux->newsegment_ev);
70 mux->newsegment_ev = NULL;
73 if (mux->event_tags) {
74 gst_tag_list_free (mux->event_tags);
75 mux->event_tags = NULL;
78 G_OBJECT_CLASS (parent_class)->finalize (obj);
82 gst_tag_lib_mux_base_init (gpointer g_class)
84 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
86 gst_element_class_add_pad_template (element_class,
87 gst_static_pad_template_get (&gst_tag_lib_mux_sink_template));
89 GST_DEBUG_CATEGORY_INIT (gst_tag_lib_mux_debug, "taglibmux", 0,
90 "taglib-based muxer");
94 gst_tag_lib_mux_class_init (GstTagLibMuxClass * klass)
96 GObjectClass *gobject_class;
97 GstElementClass *gstelement_class;
99 gobject_class = (GObjectClass *) klass;
100 gstelement_class = (GstElementClass *) klass;
102 gobject_class->finalize = gst_tag_lib_mux_finalize;
103 gstelement_class->change_state =
104 GST_DEBUG_FUNCPTR (gst_tag_lib_mux_change_state);
108 gst_tag_lib_mux_init (GstTagLibMux * mux, GstTagLibMuxClass * mux_class)
110 GstElementClass *element_klass = GST_ELEMENT_CLASS (mux_class);
111 GstPadTemplate *tmpl;
113 /* pad through which data comes in to the element */
115 gst_pad_new_from_static_template (&gst_tag_lib_mux_sink_template, "sink");
116 gst_pad_set_chain_function (mux->sinkpad,
117 GST_DEBUG_FUNCPTR (gst_tag_lib_mux_chain));
118 gst_pad_set_event_function (mux->sinkpad,
119 GST_DEBUG_FUNCPTR (gst_tag_lib_mux_sink_event));
120 gst_element_add_pad (GST_ELEMENT (mux), mux->sinkpad);
122 /* pad through which data goes out of the element */
123 tmpl = gst_element_class_get_pad_template (element_klass, "src");
125 mux->srcpad = gst_pad_new_from_template (tmpl, "src");
126 gst_pad_use_fixed_caps (mux->srcpad);
127 gst_pad_set_caps (mux->srcpad, gst_pad_template_get_caps (tmpl));
128 gst_element_add_pad (GST_ELEMENT (mux), mux->srcpad);
131 mux->render_tag = TRUE;
135 gst_tag_lib_mux_render_tag (GstTagLibMux * mux)
137 GstTagLibMuxClass *klass;
138 GstTagMergeMode merge_mode;
139 GstTagSetter *tagsetter;
141 const GstTagList *tagsetter_tags;
145 tagsetter = GST_TAG_SETTER (mux);
147 tagsetter_tags = gst_tag_setter_get_tag_list (tagsetter);
148 merge_mode = gst_tag_setter_get_tag_merge_mode (tagsetter);
150 GST_LOG_OBJECT (mux, "merging tags, merge mode = %d", merge_mode);
151 GST_LOG_OBJECT (mux, "event tags: %" GST_PTR_FORMAT, mux->event_tags);
152 GST_LOG_OBJECT (mux, "set tags: %" GST_PTR_FORMAT, tagsetter_tags);
154 taglist = gst_tag_list_merge (tagsetter_tags, mux->event_tags, merge_mode);
156 GST_LOG_OBJECT (mux, "final tags: %" GST_PTR_FORMAT, taglist);
158 klass = GST_TAG_LIB_MUX_CLASS (G_OBJECT_GET_CLASS (mux));
160 if (klass->render_tag == NULL)
163 buffer = klass->render_tag (mux, taglist);
168 mux->tag_size = GST_BUFFER_SIZE (buffer);
169 GST_LOG_OBJECT (mux, "tag size = %" G_GSIZE_FORMAT " bytes", mux->tag_size);
171 /* Send newsegment event from byte position 0, so the tag really gets
172 * written to the start of the file, independent of the upstream segment */
173 gst_pad_push_event (mux->srcpad,
174 gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_BYTES, 0, -1, 0));
176 /* Send an event about the new tags to downstream elements */
177 /* gst_event_new_tag takes ownership of the list, so no need to unref it */
178 event = gst_event_new_tag (taglist);
179 gst_pad_push_event (mux->srcpad, event);
181 GST_BUFFER_OFFSET (buffer) = 0;
187 GST_ERROR_OBJECT (mux, "Subclass does not implement render_tag vfunc!");
188 gst_tag_list_free (taglist);
194 GST_ERROR_OBJECT (mux, "Failed to render tag");
195 gst_tag_list_free (taglist);
201 gst_tag_lib_mux_adjust_event_offsets (GstTagLibMux * mux,
202 const GstEvent * newsegment_event)
205 gint64 start, stop, cur;
207 gst_event_parse_new_segment ((GstEvent *) newsegment_event, NULL, NULL,
208 &format, &start, &stop, &cur);
210 g_assert (format == GST_FORMAT_BYTES);
213 start += mux->tag_size;
215 stop += mux->tag_size;
217 cur += mux->tag_size;
219 GST_DEBUG_OBJECT (mux, "adjusting newsegment event offsets to start=%"
220 G_GINT64_FORMAT ", stop=%" G_GINT64_FORMAT ", cur=%" G_GINT64_FORMAT
221 " (delta = +%" G_GSIZE_FORMAT ")", start, stop, cur, mux->tag_size);
223 return gst_event_new_new_segment (TRUE, 1.0, format, start, stop, cur);
227 gst_tag_lib_mux_chain (GstPad * pad, GstBuffer * buffer)
229 GstTagLibMux *mux = GST_TAG_LIB_MUX (GST_OBJECT_PARENT (pad));
231 if (mux->render_tag) {
233 GstBuffer *tag_buffer;
235 GST_INFO_OBJECT (mux, "Adding tags to stream");
236 tag_buffer = gst_tag_lib_mux_render_tag (mux);
237 if (tag_buffer == NULL)
239 ret = gst_pad_push (mux->srcpad, tag_buffer);
240 if (ret != GST_FLOW_OK) {
241 GST_DEBUG_OBJECT (mux, "flow: %s", gst_flow_get_name (ret));
242 gst_buffer_unref (buffer);
246 /* Now send the cached newsegment event that we got from upstream */
247 if (mux->newsegment_ev) {
248 GST_DEBUG_OBJECT (mux, "sending cached newsegment event");
249 gst_pad_push_event (mux->srcpad,
250 gst_tag_lib_mux_adjust_event_offsets (mux, mux->newsegment_ev));
251 gst_event_unref (mux->newsegment_ev);
252 mux->newsegment_ev = NULL;
254 /* upstream sent no newsegment event or only one in a non-BYTE format */
257 mux->render_tag = FALSE;
260 buffer = gst_buffer_make_metadata_writable (buffer);
262 if (GST_BUFFER_OFFSET (buffer) != GST_BUFFER_OFFSET_NONE) {
263 GST_LOG_OBJECT (mux, "Adjusting buffer offset from %" G_GINT64_FORMAT
264 " to %" G_GINT64_FORMAT, GST_BUFFER_OFFSET (buffer),
265 GST_BUFFER_OFFSET (buffer) + mux->tag_size);
266 GST_BUFFER_OFFSET (buffer) += mux->tag_size;
269 gst_buffer_set_caps (buffer, GST_PAD_CAPS (mux->srcpad));
270 return gst_pad_push (mux->srcpad, buffer);
275 GST_ELEMENT_ERROR (mux, LIBRARY, ENCODE, (NULL), (NULL));
276 return GST_FLOW_ERROR;
281 gst_tag_lib_mux_sink_event (GstPad * pad, GstEvent * event)
286 mux = GST_TAG_LIB_MUX (gst_pad_get_parent (pad));
289 switch (GST_EVENT_TYPE (event)) {
293 gst_event_parse_tag (event, &tags);
295 GST_INFO_OBJECT (mux, "Got tag event: %" GST_PTR_FORMAT, tags);
297 if (mux->event_tags != NULL) {
298 gst_tag_list_insert (mux->event_tags, tags, GST_TAG_MERGE_REPLACE);
300 mux->event_tags = gst_tag_list_copy (tags);
303 GST_INFO_OBJECT (mux, "Event tags are now: %" GST_PTR_FORMAT,
306 /* just drop the event, we'll push a new tag event in render_tag */
307 gst_event_unref (event);
311 case GST_EVENT_NEWSEGMENT:{
314 gst_event_parse_new_segment (event, NULL, NULL, &fmt, NULL, NULL, NULL);
316 if (fmt != GST_FORMAT_BYTES) {
317 GST_WARNING_OBJECT (mux, "dropping newsegment event in %s format",
318 gst_format_get_name (fmt));
319 gst_event_unref (event);
323 if (mux->render_tag) {
324 /* we have not rendered the tag yet, which means that we don't know
325 * how large it is going to be yet, so we can't adjust the offsets
326 * here at this point and need to cache the newsegment event for now
327 * (also, there could be tag events coming after this newsegment event
328 * and before the first buffer). */
329 if (mux->newsegment_ev) {
330 GST_WARNING_OBJECT (mux, "discarding old cached newsegment event");
331 gst_event_unref (mux->newsegment_ev);
334 GST_LOG_OBJECT (mux, "caching newsegment event for later");
335 mux->newsegment_ev = event;
337 GST_DEBUG_OBJECT (mux, "got newsegment event, adjusting offsets");
338 gst_pad_push_event (mux->srcpad,
339 gst_tag_lib_mux_adjust_event_offsets (mux, event));
340 gst_event_unref (event);
347 result = gst_pad_event_default (pad, event);
351 gst_object_unref (mux);
357 static GstStateChangeReturn
358 gst_tag_lib_mux_change_state (GstElement * element, GstStateChange transition)
361 GstStateChangeReturn result;
363 mux = GST_TAG_LIB_MUX (element);
365 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
366 if (result != GST_STATE_CHANGE_SUCCESS) {
370 switch (transition) {
371 case GST_STATE_CHANGE_PAUSED_TO_READY:{
372 if (mux->newsegment_ev) {
373 gst_event_unref (mux->newsegment_ev);
374 mux->newsegment_ev = NULL;
376 if (mux->event_tags) {
377 gst_tag_list_free (mux->event_tags);
378 mux->event_tags = NULL;
381 mux->render_tag = TRUE;
392 plugin_init (GstPlugin * plugin)
394 return (gst_id3v2_mux_plugin_init (plugin)
395 && gst_apev2_mux_plugin_init (plugin));
398 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
401 "Tag writing plug-in based on taglib",
402 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);