taglib: port to 0.11
[platform/upstream/gstreamer.git] / ext / taglib / gsttaglibmux.c
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>
5
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.
10  *
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.
15  *
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.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27 #include <gst/gsttagsetter.h>
28 #include <gst/tag/tag.h>
29
30 #include "gsttaglibmux.h"
31
32 GST_DEBUG_CATEGORY_STATIC (gst_tag_lib_mux_debug);
33 #define GST_CAT_DEFAULT gst_tag_lib_mux_debug
34
35 static GstStaticPadTemplate gst_tag_lib_mux_sink_template =
36 GST_STATIC_PAD_TEMPLATE ("sink",
37     GST_PAD_SINK,
38     GST_PAD_ALWAYS,
39     GST_STATIC_CAPS ("ANY"));
40
41
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));
45
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,
49     GstBuffer * buffer);
50 static gboolean gst_tag_lib_mux_sink_event (GstPad * pad, GstObject * parent,
51     GstEvent * event);
52
53 static void
54 gst_tag_lib_mux_finalize (GObject * obj)
55 {
56   GstTagLibMux *mux = GST_TAG_LIB_MUX (obj);
57
58   if (mux->newsegment_ev) {
59     gst_event_unref (mux->newsegment_ev);
60     mux->newsegment_ev = NULL;
61   }
62
63   if (mux->event_tags) {
64     gst_tag_list_free (mux->event_tags);
65     mux->event_tags = NULL;
66   }
67
68   G_OBJECT_CLASS (parent_class)->finalize (obj);
69 }
70
71 static void
72 gst_tag_lib_mux_class_init (GstTagLibMuxClass * klass)
73 {
74   GObjectClass *gobject_class;
75   GstElementClass *gstelement_class;
76
77   gobject_class = (GObjectClass *) klass;
78   gstelement_class = (GstElementClass *) klass;
79
80   gobject_class->finalize = gst_tag_lib_mux_finalize;
81   gstelement_class->change_state =
82       GST_DEBUG_FUNCPTR (gst_tag_lib_mux_change_state);
83
84   gst_element_class_add_pad_template (gstelement_class,
85       gst_static_pad_template_get (&gst_tag_lib_mux_sink_template));
86
87   GST_DEBUG_CATEGORY_INIT (gst_tag_lib_mux_debug, "taglibmux", 0,
88       "taglib-based muxer");
89 }
90
91 static void
92 gst_tag_lib_mux_init (GstTagLibMux * mux)
93 {
94   GstTagLibMuxClass *mux_class = GST_TAG_LIB_MUX_GET_CLASS (mux);
95   GstElementClass *element_klass = GST_ELEMENT_CLASS (mux_class);
96   GstPadTemplate *tmpl;
97
98   /* pad through which data comes in to the element */
99   mux->sinkpad =
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);
106
107   /* pad through which data goes out of the element */
108   tmpl = gst_element_class_get_pad_template (element_klass, "src");
109   if (tmpl) {
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);
114   }
115
116   mux->render_tag = TRUE;
117 }
118
119 static GstBuffer *
120 gst_tag_lib_mux_render_tag (GstTagLibMux * mux)
121 {
122   GstTagLibMuxClass *klass;
123   GstTagMergeMode merge_mode;
124   GstTagSetter *tagsetter;
125   GstBuffer *buffer;
126   const GstTagList *tagsetter_tags;
127   GstTagList *taglist;
128   GstEvent *event;
129   GstSegment segment;
130
131   tagsetter = GST_TAG_SETTER (mux);
132
133   tagsetter_tags = gst_tag_setter_get_tag_list (tagsetter);
134   merge_mode = gst_tag_setter_get_tag_merge_mode (tagsetter);
135
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);
139
140   taglist = gst_tag_list_merge (tagsetter_tags, mux->event_tags, merge_mode);
141
142   GST_LOG_OBJECT (mux, "final tags: %" GST_PTR_FORMAT, taglist);
143
144   klass = GST_TAG_LIB_MUX_CLASS (G_OBJECT_GET_CLASS (mux));
145
146   if (klass->render_tag == NULL)
147     goto no_vfunc;
148
149   buffer = klass->render_tag (mux, taglist);
150
151   if (buffer == NULL)
152     goto render_error;
153
154   mux->tag_size = gst_buffer_get_size (buffer);
155   GST_LOG_OBJECT (mux, "tag size = %" G_GSIZE_FORMAT " bytes", mux->tag_size);
156
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));
161
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);
166
167   GST_BUFFER_OFFSET (buffer) = 0;
168
169   return buffer;
170
171 no_vfunc:
172   {
173     GST_ERROR_OBJECT (mux, "Subclass does not implement render_tag vfunc!");
174     gst_tag_list_free (taglist);
175     return NULL;
176   }
177
178 render_error:
179   {
180     GST_ERROR_OBJECT (mux, "Failed to render tag");
181     gst_tag_list_free (taglist);
182     return NULL;
183   }
184 }
185
186 static GstEvent *
187 gst_tag_lib_mux_adjust_event_offsets (GstTagLibMux * mux,
188     const GstEvent * newsegment_event)
189 {
190   gint64 start, stop, cur;
191   GstSegment segment;
192
193   gst_event_copy_segment ((GstEvent *) newsegment_event, &segment);
194
195   g_assert (segment.format == GST_FORMAT_BYTES);
196
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;
203
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);
208
209   return gst_event_new_segment (&segment);
210 }
211
212 static GstFlowReturn
213 gst_tag_lib_mux_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
214 {
215   GstTagLibMux *mux = GST_TAG_LIB_MUX (parent);
216
217   if (mux->render_tag) {
218     GstFlowReturn ret;
219     GstBuffer *tag_buffer;
220     GstCaps *tcaps;
221
222     GST_INFO_OBJECT (mux, "Adding tags to stream");
223     tag_buffer = gst_tag_lib_mux_render_tag (mux);
224     if (tag_buffer == NULL)
225       goto no_tag_buffer;
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);
230       return ret;
231     }
232
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;
240     } else {
241       /* upstream sent no newsegment event or only one in a non-BYTE format */
242     }
243
244     mux->render_tag = FALSE;
245
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);
250   }
251
252   buffer = gst_buffer_make_writable (buffer);
253
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;
259   }
260
261   return gst_pad_push (mux->srcpad, buffer);
262
263 /* ERRORS */
264 no_tag_buffer:
265   {
266     GST_ELEMENT_ERROR (mux, LIBRARY, ENCODE, (NULL), (NULL));
267     return GST_FLOW_ERROR;
268   }
269 }
270
271 static gboolean
272 gst_tag_lib_mux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
273 {
274   GstTagLibMux *mux;
275   gboolean result;
276
277   mux = GST_TAG_LIB_MUX (parent);
278   result = FALSE;
279
280   switch (GST_EVENT_TYPE (event)) {
281     case GST_EVENT_TAG:{
282       GstTagList *tags;
283
284       gst_event_parse_tag (event, &tags);
285
286       GST_INFO_OBJECT (mux, "Got tag event: %" GST_PTR_FORMAT, tags);
287
288       if (mux->event_tags != NULL) {
289         gst_tag_list_insert (mux->event_tags, tags, GST_TAG_MERGE_REPLACE);
290       } else {
291         mux->event_tags = gst_tag_list_copy (tags);
292       }
293
294       GST_INFO_OBJECT (mux, "Event tags are now: %" GST_PTR_FORMAT,
295           mux->event_tags);
296
297       /* just drop the event, we'll push a new tag event in render_tag */
298       gst_event_unref (event);
299       result = TRUE;
300       break;
301     }
302     case GST_EVENT_SEGMENT:{
303       const GstSegment *segment;
304
305       gst_event_parse_segment (event, &segment);
306
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);
311         break;
312       }
313
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);
323         }
324
325         GST_LOG_OBJECT (mux, "caching newsegment event for later");
326         mux->newsegment_ev = event;
327       } else {
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);
332       }
333       event = NULL;
334       result = TRUE;
335       break;
336     }
337     default:
338       result = gst_pad_event_default (pad, parent, event);
339       break;
340   }
341
342   return result;
343 }
344
345
346 static GstStateChangeReturn
347 gst_tag_lib_mux_change_state (GstElement * element, GstStateChange transition)
348 {
349   GstTagLibMux *mux;
350   GstStateChangeReturn result;
351
352   mux = GST_TAG_LIB_MUX (element);
353
354   result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
355   if (result != GST_STATE_CHANGE_SUCCESS) {
356     return result;
357   }
358
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;
364       }
365       if (mux->event_tags) {
366         gst_tag_list_free (mux->event_tags);
367         mux->event_tags = NULL;
368       }
369       mux->tag_size = 0;
370       mux->render_tag = TRUE;
371       break;
372     }
373     default:
374       break;
375   }
376
377   return result;
378 }
379
380 static gboolean
381 plugin_init (GstPlugin * plugin)
382 {
383   return (gst_id3v2_mux_plugin_init (plugin)
384       && gst_apev2_mux_plugin_init (plugin));
385 }
386
387 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
388     GST_VERSION_MINOR,
389     "taglib",
390     "Tag writing plug-in based on taglib",
391     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);