Git init
[framework/multimedia/gst-plugins-good0.10.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 static void
42 gst_tag_lib_mux_iface_init (GType taglib_type)
43 {
44   static const GInterfaceInfo tag_setter_info = {
45     NULL,
46     NULL,
47     NULL
48   };
49
50   g_type_add_interface_static (taglib_type, GST_TYPE_TAG_SETTER,
51       &tag_setter_info);
52 }
53
54 GST_BOILERPLATE_FULL (GstTagLibMux, gst_tag_lib_mux,
55     GstElement, GST_TYPE_ELEMENT, gst_tag_lib_mux_iface_init);
56
57
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);
62
63 static void
64 gst_tag_lib_mux_finalize (GObject * obj)
65 {
66   GstTagLibMux *mux = GST_TAG_LIB_MUX (obj);
67
68   if (mux->newsegment_ev) {
69     gst_event_unref (mux->newsegment_ev);
70     mux->newsegment_ev = NULL;
71   }
72
73   if (mux->event_tags) {
74     gst_tag_list_free (mux->event_tags);
75     mux->event_tags = NULL;
76   }
77
78   G_OBJECT_CLASS (parent_class)->finalize (obj);
79 }
80
81 static void
82 gst_tag_lib_mux_base_init (gpointer g_class)
83 {
84   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
85
86   gst_element_class_add_pad_template (element_class,
87       gst_static_pad_template_get (&gst_tag_lib_mux_sink_template));
88
89   GST_DEBUG_CATEGORY_INIT (gst_tag_lib_mux_debug, "taglibmux", 0,
90       "taglib-based muxer");
91 }
92
93 static void
94 gst_tag_lib_mux_class_init (GstTagLibMuxClass * klass)
95 {
96   GObjectClass *gobject_class;
97   GstElementClass *gstelement_class;
98
99   gobject_class = (GObjectClass *) klass;
100   gstelement_class = (GstElementClass *) klass;
101
102   gobject_class->finalize = gst_tag_lib_mux_finalize;
103   gstelement_class->change_state =
104       GST_DEBUG_FUNCPTR (gst_tag_lib_mux_change_state);
105 }
106
107 static void
108 gst_tag_lib_mux_init (GstTagLibMux * mux, GstTagLibMuxClass * mux_class)
109 {
110   GstElementClass *element_klass = GST_ELEMENT_CLASS (mux_class);
111   GstPadTemplate *tmpl;
112
113   /* pad through which data comes in to the element */
114   mux->sinkpad =
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);
121
122   /* pad through which data goes out of the element */
123   tmpl = gst_element_class_get_pad_template (element_klass, "src");
124   if (tmpl) {
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);
129   }
130
131   mux->render_tag = TRUE;
132 }
133
134 static GstBuffer *
135 gst_tag_lib_mux_render_tag (GstTagLibMux * mux)
136 {
137   GstTagLibMuxClass *klass;
138   GstTagMergeMode merge_mode;
139   GstTagSetter *tagsetter;
140   GstBuffer *buffer;
141   const GstTagList *tagsetter_tags;
142   GstTagList *taglist;
143   GstEvent *event;
144
145   tagsetter = GST_TAG_SETTER (mux);
146
147   tagsetter_tags = gst_tag_setter_get_tag_list (tagsetter);
148   merge_mode = gst_tag_setter_get_tag_merge_mode (tagsetter);
149
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);
153
154   taglist = gst_tag_list_merge (tagsetter_tags, mux->event_tags, merge_mode);
155
156   GST_LOG_OBJECT (mux, "final tags: %" GST_PTR_FORMAT, taglist);
157
158   klass = GST_TAG_LIB_MUX_CLASS (G_OBJECT_GET_CLASS (mux));
159
160   if (klass->render_tag == NULL)
161     goto no_vfunc;
162
163   buffer = klass->render_tag (mux, taglist);
164
165   if (buffer == NULL)
166     goto render_error;
167
168   mux->tag_size = GST_BUFFER_SIZE (buffer);
169   GST_LOG_OBJECT (mux, "tag size = %" G_GSIZE_FORMAT " bytes", mux->tag_size);
170
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));
175
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);
180
181   GST_BUFFER_OFFSET (buffer) = 0;
182
183   return buffer;
184
185 no_vfunc:
186   {
187     GST_ERROR_OBJECT (mux, "Subclass does not implement render_tag vfunc!");
188     gst_tag_list_free (taglist);
189     return NULL;
190   }
191
192 render_error:
193   {
194     GST_ERROR_OBJECT (mux, "Failed to render tag");
195     gst_tag_list_free (taglist);
196     return NULL;
197   }
198 }
199
200 static GstEvent *
201 gst_tag_lib_mux_adjust_event_offsets (GstTagLibMux * mux,
202     const GstEvent * newsegment_event)
203 {
204   GstFormat format;
205   gint64 start, stop, cur;
206
207   gst_event_parse_new_segment ((GstEvent *) newsegment_event, NULL, NULL,
208       &format, &start, &stop, &cur);
209
210   g_assert (format == GST_FORMAT_BYTES);
211
212   if (start != -1)
213     start += mux->tag_size;
214   if (stop != -1)
215     stop += mux->tag_size;
216   if (cur != -1)
217     cur += mux->tag_size;
218
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);
222
223   return gst_event_new_new_segment (TRUE, 1.0, format, start, stop, cur);
224 }
225
226 static GstFlowReturn
227 gst_tag_lib_mux_chain (GstPad * pad, GstBuffer * buffer)
228 {
229   GstTagLibMux *mux = GST_TAG_LIB_MUX (GST_OBJECT_PARENT (pad));
230
231   if (mux->render_tag) {
232     GstFlowReturn ret;
233     GstBuffer *tag_buffer;
234
235     GST_INFO_OBJECT (mux, "Adding tags to stream");
236     tag_buffer = gst_tag_lib_mux_render_tag (mux);
237     if (tag_buffer == NULL)
238       goto no_tag_buffer;
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);
243       return ret;
244     }
245
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;
253     } else {
254       /* upstream sent no newsegment event or only one in a non-BYTE format */
255     }
256
257     mux->render_tag = FALSE;
258   }
259
260   buffer = gst_buffer_make_metadata_writable (buffer);
261
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;
267   }
268
269   gst_buffer_set_caps (buffer, GST_PAD_CAPS (mux->srcpad));
270   return gst_pad_push (mux->srcpad, buffer);
271
272 /* ERRORS */
273 no_tag_buffer:
274   {
275     GST_ELEMENT_ERROR (mux, LIBRARY, ENCODE, (NULL), (NULL));
276     return GST_FLOW_ERROR;
277   }
278 }
279
280 static gboolean
281 gst_tag_lib_mux_sink_event (GstPad * pad, GstEvent * event)
282 {
283   GstTagLibMux *mux;
284   gboolean result;
285
286   mux = GST_TAG_LIB_MUX (gst_pad_get_parent (pad));
287   result = FALSE;
288
289   switch (GST_EVENT_TYPE (event)) {
290     case GST_EVENT_TAG:{
291       GstTagList *tags;
292
293       gst_event_parse_tag (event, &tags);
294
295       GST_INFO_OBJECT (mux, "Got tag event: %" GST_PTR_FORMAT, tags);
296
297       if (mux->event_tags != NULL) {
298         gst_tag_list_insert (mux->event_tags, tags, GST_TAG_MERGE_REPLACE);
299       } else {
300         mux->event_tags = gst_tag_list_copy (tags);
301       }
302
303       GST_INFO_OBJECT (mux, "Event tags are now: %" GST_PTR_FORMAT,
304           mux->event_tags);
305
306       /* just drop the event, we'll push a new tag event in render_tag */
307       gst_event_unref (event);
308       result = TRUE;
309       break;
310     }
311     case GST_EVENT_NEWSEGMENT:{
312       GstFormat fmt;
313
314       gst_event_parse_new_segment (event, NULL, NULL, &fmt, NULL, NULL, NULL);
315
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);
320         break;
321       }
322
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);
332         }
333
334         GST_LOG_OBJECT (mux, "caching newsegment event for later");
335         mux->newsegment_ev = event;
336       } else {
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);
341       }
342       event = NULL;
343       result = TRUE;
344       break;
345     }
346     default:
347       result = gst_pad_event_default (pad, event);
348       break;
349   }
350
351   gst_object_unref (mux);
352
353   return result;
354 }
355
356
357 static GstStateChangeReturn
358 gst_tag_lib_mux_change_state (GstElement * element, GstStateChange transition)
359 {
360   GstTagLibMux *mux;
361   GstStateChangeReturn result;
362
363   mux = GST_TAG_LIB_MUX (element);
364
365   result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
366   if (result != GST_STATE_CHANGE_SUCCESS) {
367     return result;
368   }
369
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;
375       }
376       if (mux->event_tags) {
377         gst_tag_list_free (mux->event_tags);
378         mux->event_tags = NULL;
379       }
380       mux->tag_size = 0;
381       mux->render_tag = TRUE;
382       break;
383     }
384     default:
385       break;
386   }
387
388   return result;
389 }
390
391 static gboolean
392 plugin_init (GstPlugin * plugin)
393 {
394   return (gst_id3v2_mux_plugin_init (plugin)
395       && gst_apev2_mux_plugin_init (plugin));
396 }
397
398 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
399     GST_VERSION_MINOR,
400     "taglib",
401     "Tag writing plug-in based on taglib",
402     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);