Fix FSF address
[platform/upstream/gstreamer.git] / gst-libs / gst / tag / gsttagmux.c
1 /* GStreamer tag 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  * Copyright (C) 2009 Pioneers of the Inevitable <songbird@songbirdnest.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 /**
24  * SECTION:gsttagmux
25  * @see_also: GstApeMux, GstId3Mux
26  * @short_description: Base class for adding tags that are in one single chunk
27  *                     directly at the beginning or at the end of a file
28  *
29  * <refsect2>
30  * <para>
31  * Provides a base class for adding tags at the beginning or end of a
32  * stream.
33  * </para>
34  * <title>Deriving from GstTagMux</title>
35  * <para>
36  * Subclasses have to do the following things:
37  * <itemizedlist>
38  *  <listitem><para>
39  *  In their base init function, they must add pad templates for the sink
40  *  pad and the source pad to the element class, describing the media type
41  *  they accept and output in the caps of the pad template.
42  *  </para></listitem>
43  *  <listitem><para>
44  *  In their class init function, they must override the
45  *  GST_TAG_MUX_CLASS(mux_klass)->render_start_tag and/or
46  *  GST_TAG_MUX_CLASS(mux_klass)->render_end_tag vfuncs and set up a render
47  *  function.
48  *  </para></listitem>
49  * </itemizedlist>
50  * </para>
51  * </refsect2>
52  */
53 #ifdef HAVE_CONFIG_H
54 #include <config.h>
55 #endif
56
57 #include <string.h>
58 #include <gst/gsttagsetter.h>
59 #include <gst/tag/tag.h>
60
61 #include "gsttagmux.h"
62
63 struct _GstTagMuxPrivate
64 {
65   GstPad *srcpad;
66   GstPad *sinkpad;
67   GstTagList *event_tags;       /* tags received from upstream elements */
68   GstTagList *final_tags;       /* Final set of tags used for muxing */
69   gsize start_tag_size;
70   gsize end_tag_size;
71   gboolean render_start_tag;
72   gboolean render_end_tag;
73
74   gint64 current_offset;
75   gint64 max_offset;
76
77   GstEvent *newsegment_ev;      /* cached newsegment event from upstream */
78 };
79
80 GST_DEBUG_CATEGORY_STATIC (gst_tag_mux_debug);
81 #define GST_CAT_DEFAULT gst_tag_mux_debug
82
83 static GstElementClass *parent_class;
84
85 static void gst_tag_mux_class_init (GstTagMuxClass * klass);
86 static void gst_tag_mux_init (GstTagMux * mux, GstTagMuxClass * mux_class);
87 static GstStateChangeReturn
88 gst_tag_mux_change_state (GstElement * element, GstStateChange transition);
89 static GstFlowReturn gst_tag_mux_chain (GstPad * pad, GstObject * parent,
90     GstBuffer * buffer);
91 static gboolean gst_tag_mux_sink_event (GstPad * pad, GstObject * parent,
92     GstEvent * event);
93
94 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
95  * method to get to the padtemplates */
96 GType
97 gst_tag_mux_get_type (void)
98 {
99   static volatile gsize tag_mux_type = 0;
100
101   if (g_once_init_enter (&tag_mux_type)) {
102     const GInterfaceInfo interface_info = { NULL, NULL, NULL };
103     GType _type;
104
105     _type = g_type_register_static_simple (GST_TYPE_ELEMENT,
106         "GstTagMux", sizeof (GstTagMuxClass),
107         (GClassInitFunc) gst_tag_mux_class_init, sizeof (GstTagMux),
108         (GInstanceInitFunc) gst_tag_mux_init, G_TYPE_FLAG_ABSTRACT);
109
110     g_type_add_interface_static (_type, GST_TYPE_TAG_SETTER, &interface_info);
111
112     g_once_init_leave (&tag_mux_type, _type);
113   }
114   return tag_mux_type;
115 }
116
117 static void
118 gst_tag_mux_finalize (GObject * obj)
119 {
120   GstTagMux *mux = GST_TAG_MUX (obj);
121
122   if (mux->priv->newsegment_ev) {
123     gst_event_unref (mux->priv->newsegment_ev);
124     mux->priv->newsegment_ev = NULL;
125   }
126
127   if (mux->priv->event_tags) {
128     gst_tag_list_unref (mux->priv->event_tags);
129     mux->priv->event_tags = NULL;
130   }
131
132   if (mux->priv->final_tags) {
133     gst_tag_list_unref (mux->priv->final_tags);
134     mux->priv->final_tags = NULL;
135   }
136
137   G_OBJECT_CLASS (parent_class)->finalize (obj);
138 }
139
140 static void
141 gst_tag_mux_class_init (GstTagMuxClass * klass)
142 {
143   GObjectClass *gobject_class;
144   GstElementClass *gstelement_class;
145
146   gobject_class = (GObjectClass *) klass;
147   gstelement_class = (GstElementClass *) klass;
148
149   parent_class = g_type_class_peek_parent (klass);
150
151   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_tag_mux_finalize);
152   gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_tag_mux_change_state);
153
154   g_type_class_add_private (klass, sizeof (GstTagMuxPrivate));
155
156   GST_DEBUG_CATEGORY_INIT (gst_tag_mux_debug, "tagmux", 0,
157       "tag muxer base class");
158 }
159
160 static void
161 gst_tag_mux_init (GstTagMux * mux, GstTagMuxClass * mux_class)
162 {
163   GstElementClass *element_klass = GST_ELEMENT_CLASS (mux_class);
164   GstPadTemplate *tmpl;
165
166   mux->priv =
167       G_TYPE_INSTANCE_GET_PRIVATE (mux, GST_TYPE_TAG_MUX, GstTagMuxPrivate);
168
169   /* pad through which data comes in to the element */
170   tmpl = gst_element_class_get_pad_template (element_klass, "sink");
171   if (tmpl) {
172     mux->priv->sinkpad = gst_pad_new_from_template (tmpl, "sink");
173   } else {
174     g_warning ("GstTagMux subclass '%s' did not install a %s pad template!\n",
175         G_OBJECT_CLASS_NAME (element_klass), "sink");
176     mux->priv->sinkpad = gst_pad_new ("sink", GST_PAD_SINK);
177   }
178   gst_pad_set_chain_function (mux->priv->sinkpad,
179       GST_DEBUG_FUNCPTR (gst_tag_mux_chain));
180   gst_pad_set_event_function (mux->priv->sinkpad,
181       GST_DEBUG_FUNCPTR (gst_tag_mux_sink_event));
182   gst_element_add_pad (GST_ELEMENT (mux), mux->priv->sinkpad);
183
184   /* pad through which data goes out of the element */
185   tmpl = gst_element_class_get_pad_template (element_klass, "src");
186   if (tmpl) {
187     GstCaps *tmpl_caps = gst_pad_template_get_caps (tmpl);
188
189     mux->priv->srcpad = gst_pad_new_from_template (tmpl, "src");
190     gst_pad_use_fixed_caps (mux->priv->srcpad);
191     if (tmpl_caps != NULL && gst_caps_is_fixed (tmpl_caps)) {
192       gst_pad_set_caps (mux->priv->srcpad, tmpl_caps);
193     }
194   } else {
195     g_warning ("GstTagMux subclass '%s' did not install a %s pad template!\n",
196         G_OBJECT_CLASS_NAME (element_klass), "source");
197     mux->priv->srcpad = gst_pad_new ("src", GST_PAD_SRC);
198   }
199   gst_element_add_pad (GST_ELEMENT (mux), mux->priv->srcpad);
200
201   mux->priv->render_start_tag = TRUE;
202   mux->priv->render_end_tag = TRUE;
203 }
204
205 static GstTagList *
206 gst_tag_mux_get_tags (GstTagMux * mux)
207 {
208   GstTagSetter *tagsetter = GST_TAG_SETTER (mux);
209   const GstTagList *tagsetter_tags;
210   GstTagMergeMode merge_mode;
211
212   if (mux->priv->final_tags)
213     return mux->priv->final_tags;
214
215   tagsetter_tags = gst_tag_setter_get_tag_list (tagsetter);
216   merge_mode = gst_tag_setter_get_tag_merge_mode (tagsetter);
217
218   GST_LOG_OBJECT (mux, "merging tags, merge mode = %d", merge_mode);
219   GST_LOG_OBJECT (mux, "event tags: %" GST_PTR_FORMAT, mux->priv->event_tags);
220   GST_LOG_OBJECT (mux, "set   tags: %" GST_PTR_FORMAT, tagsetter_tags);
221
222   mux->priv->final_tags =
223       gst_tag_list_merge (tagsetter_tags, mux->priv->event_tags, merge_mode);
224
225   GST_LOG_OBJECT (mux, "final tags: %" GST_PTR_FORMAT, mux->priv->final_tags);
226
227   return mux->priv->final_tags;
228 }
229
230 static GstFlowReturn
231 gst_tag_mux_render_start_tag (GstTagMux * mux)
232 {
233   GstTagMuxClass *klass;
234   GstBuffer *buffer;
235   GstTagList *taglist;
236   GstEvent *event;
237   GstFlowReturn ret;
238   GstSegment segment;
239
240   taglist = gst_tag_mux_get_tags (mux);
241
242   klass = GST_TAG_MUX_CLASS (G_OBJECT_GET_CLASS (mux));
243
244   if (klass->render_start_tag == NULL)
245     goto no_vfunc;
246
247   buffer = klass->render_start_tag (mux, taglist);
248
249   /* Null buffer is ok, just means we're not outputting anything */
250   if (buffer == NULL) {
251     GST_INFO_OBJECT (mux, "No start tag generated");
252     mux->priv->start_tag_size = 0;
253     return GST_FLOW_OK;
254   }
255
256   mux->priv->start_tag_size = gst_buffer_get_size (buffer);
257   GST_LOG_OBJECT (mux, "tag size = %" G_GSIZE_FORMAT " bytes",
258       mux->priv->start_tag_size);
259
260   /* Send newsegment event from byte position 0, so the tag really gets
261    * written to the start of the file, independent of the upstream segment */
262   gst_segment_init (&segment, GST_FORMAT_BYTES);
263   gst_pad_push_event (mux->priv->srcpad, gst_event_new_segment (&segment));
264
265   /* Send an event about the new tags to downstream elements */
266   /* gst_event_new_tag takes ownership of the list, so use a copy */
267   event = gst_event_new_tag (gst_tag_list_ref (taglist));
268   gst_pad_push_event (mux->priv->srcpad, event);
269
270   GST_BUFFER_OFFSET (buffer) = 0;
271   ret = gst_pad_push (mux->priv->srcpad, buffer);
272
273   mux->priv->current_offset = mux->priv->start_tag_size;
274   mux->priv->max_offset =
275       MAX (mux->priv->max_offset, mux->priv->current_offset);
276
277   return ret;
278
279 no_vfunc:
280   {
281     GST_ERROR_OBJECT (mux, "Subclass does not implement "
282         "render_start_tag vfunc!");
283     return GST_FLOW_ERROR;
284   }
285 }
286
287 static GstFlowReturn
288 gst_tag_mux_render_end_tag (GstTagMux * mux)
289 {
290   GstTagMuxClass *klass;
291   GstBuffer *buffer;
292   GstTagList *taglist;
293   GstFlowReturn ret;
294   GstSegment segment;
295
296   taglist = gst_tag_mux_get_tags (mux);
297
298   klass = GST_TAG_MUX_CLASS (G_OBJECT_GET_CLASS (mux));
299
300   if (klass->render_end_tag == NULL)
301     goto no_vfunc;
302
303   buffer = klass->render_end_tag (mux, taglist);
304
305   if (buffer == NULL) {
306     GST_INFO_OBJECT (mux, "No end tag generated");
307     mux->priv->end_tag_size = 0;
308     return GST_FLOW_OK;
309   }
310
311   mux->priv->end_tag_size = gst_buffer_get_size (buffer);
312   GST_LOG_OBJECT (mux, "tag size = %" G_GSIZE_FORMAT " bytes",
313       mux->priv->end_tag_size);
314
315   /* Send newsegment event from the end of the file, so it gets written there,
316      independent of whatever new segment events upstream has sent us */
317   gst_segment_init (&segment, GST_FORMAT_BYTES);
318   segment.start = mux->priv->max_offset;
319   gst_pad_push_event (mux->priv->srcpad, gst_event_new_segment (&segment));
320
321   GST_BUFFER_OFFSET (buffer) = mux->priv->max_offset;
322   ret = gst_pad_push (mux->priv->srcpad, buffer);
323
324   return ret;
325
326 no_vfunc:
327   {
328     GST_ERROR_OBJECT (mux, "Subclass does not implement "
329         "render_end_tag vfunc!");
330     return GST_FLOW_ERROR;
331   }
332 }
333
334 static GstEvent *
335 gst_tag_mux_adjust_event_offsets (GstTagMux * mux,
336     const GstEvent * newsegment_event)
337 {
338   GstSegment segment;
339
340   gst_event_copy_segment ((GstEvent *) newsegment_event, &segment);
341
342   g_assert (segment.format == GST_FORMAT_BYTES);
343
344   if (segment.start != -1)
345     segment.start += mux->priv->start_tag_size;
346   if (segment.stop != -1)
347     segment.stop += mux->priv->start_tag_size;
348   if (segment.time != -1)
349     segment.time += mux->priv->start_tag_size;
350
351   GST_DEBUG_OBJECT (mux, "adjusting newsegment event offsets to start=%"
352       G_GINT64_FORMAT ", stop=%" G_GINT64_FORMAT ", cur=%" G_GINT64_FORMAT
353       " (delta = +%" G_GSIZE_FORMAT ")", segment.start, segment.stop,
354       segment.time, mux->priv->start_tag_size);
355
356   return gst_event_new_segment (&segment);
357 }
358
359 static GstFlowReturn
360 gst_tag_mux_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
361 {
362   GstTagMux *mux = GST_TAG_MUX (parent);
363   GstFlowReturn ret;
364   int length;
365
366   if (mux->priv->render_start_tag) {
367
368     GST_INFO_OBJECT (mux, "Adding tags to stream");
369     ret = gst_tag_mux_render_start_tag (mux);
370     if (ret != GST_FLOW_OK) {
371       GST_DEBUG_OBJECT (mux, "flow: %s", gst_flow_get_name (ret));
372       gst_buffer_unref (buffer);
373       return ret;
374     }
375
376     /* Now send the cached newsegment event that we got from upstream */
377     if (mux->priv->newsegment_ev) {
378       GstEvent *newseg;
379       GstSegment segment;
380
381       GST_DEBUG_OBJECT (mux, "sending cached newsegment event");
382       newseg = gst_tag_mux_adjust_event_offsets (mux, mux->priv->newsegment_ev);
383       gst_event_unref (mux->priv->newsegment_ev);
384       mux->priv->newsegment_ev = NULL;
385
386       gst_event_copy_segment (newseg, &segment);
387
388       gst_pad_push_event (mux->priv->srcpad, newseg);
389       mux->priv->current_offset = segment.start;
390       mux->priv->max_offset =
391           MAX (mux->priv->max_offset, mux->priv->current_offset);
392     } else {
393       /* upstream sent no newsegment event or only one in a non-BYTE format */
394     }
395
396     mux->priv->render_start_tag = FALSE;
397   }
398
399   buffer = gst_buffer_make_writable (buffer);
400
401   if (GST_BUFFER_OFFSET (buffer) != GST_BUFFER_OFFSET_NONE) {
402     GST_LOG_OBJECT (mux, "Adjusting buffer offset from %" G_GINT64_FORMAT
403         " to %" G_GINT64_FORMAT, GST_BUFFER_OFFSET (buffer),
404         GST_BUFFER_OFFSET (buffer) + mux->priv->start_tag_size);
405     GST_BUFFER_OFFSET (buffer) += mux->priv->start_tag_size;
406   }
407
408   length = gst_buffer_get_size (buffer);
409
410   ret = gst_pad_push (mux->priv->srcpad, buffer);
411
412   mux->priv->current_offset += length;
413   mux->priv->max_offset =
414       MAX (mux->priv->max_offset, mux->priv->current_offset);
415
416   return ret;
417 }
418
419 static gboolean
420 gst_tag_mux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
421 {
422   GstTagMux *mux;
423   gboolean result;
424
425   mux = GST_TAG_MUX (parent);
426   result = FALSE;
427
428   switch (GST_EVENT_TYPE (event)) {
429     case GST_EVENT_TAG:{
430       GstTagList *tags;
431
432       gst_event_parse_tag (event, &tags);
433
434       GST_INFO_OBJECT (mux, "Got tag event: %" GST_PTR_FORMAT, tags);
435
436       if (mux->priv->event_tags != NULL) {
437         gst_tag_list_insert (mux->priv->event_tags, tags,
438             GST_TAG_MERGE_REPLACE);
439       } else {
440         mux->priv->event_tags = gst_tag_list_copy (tags);
441       }
442
443       GST_INFO_OBJECT (mux, "Event tags are now: %" GST_PTR_FORMAT,
444           mux->priv->event_tags);
445
446       /* just drop the event, we'll push a new tag event in render_start_tag */
447       gst_event_unref (event);
448       result = TRUE;
449       break;
450     }
451     case GST_EVENT_SEGMENT:
452     {
453       GstSegment segment;
454
455       gst_event_copy_segment (event, &segment);
456
457       if (segment.format != GST_FORMAT_BYTES) {
458         GST_WARNING_OBJECT (mux, "dropping newsegment event in %s format",
459             gst_format_get_name (segment.format));
460         gst_event_unref (event);
461         /* drop it quietly, so it is not seen as a failure to push event,
462          * which will turn into failure to push data as it is sticky */
463         result = TRUE;
464         break;
465       }
466
467       if (mux->priv->render_start_tag) {
468         /* we have not rendered the tag yet, which means that we don't know
469          * how large it is going to be yet, so we can't adjust the offsets
470          * here at this point and need to cache the newsegment event for now
471          * (also, there could be tag events coming after this newsegment event
472          *  and before the first buffer). */
473         if (mux->priv->newsegment_ev) {
474           GST_WARNING_OBJECT (mux, "discarding old cached newsegment event");
475           gst_event_unref (mux->priv->newsegment_ev);
476         }
477
478         GST_LOG_OBJECT (mux, "caching newsegment event for later");
479         mux->priv->newsegment_ev = event;
480       } else {
481         GST_DEBUG_OBJECT (mux, "got newsegment event, adjusting offsets");
482         gst_pad_push_event (mux->priv->srcpad,
483             gst_tag_mux_adjust_event_offsets (mux, event));
484         gst_event_unref (event);
485
486         mux->priv->current_offset = segment.start;
487         mux->priv->max_offset =
488             MAX (mux->priv->max_offset, mux->priv->current_offset);
489       }
490       event = NULL;
491       result = TRUE;
492       break;
493     }
494     case GST_EVENT_EOS:{
495       if (mux->priv->render_end_tag) {
496         GstFlowReturn ret;
497
498         GST_INFO_OBJECT (mux, "Adding tags to stream");
499         ret = gst_tag_mux_render_end_tag (mux);
500         if (ret != GST_FLOW_OK) {
501           GST_DEBUG_OBJECT (mux, "flow: %s", gst_flow_get_name (ret));
502           return ret;
503         }
504
505         mux->priv->render_end_tag = FALSE;
506       }
507
508       /* Now forward EOS */
509       result = gst_pad_event_default (pad, parent, event);
510       break;
511     }
512     default:
513       result = gst_pad_event_default (pad, parent, event);
514       break;
515   }
516
517   return result;
518 }
519
520
521 static GstStateChangeReturn
522 gst_tag_mux_change_state (GstElement * element, GstStateChange transition)
523 {
524   GstTagMux *mux;
525   GstStateChangeReturn result;
526
527   mux = GST_TAG_MUX (element);
528
529   result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
530   if (result != GST_STATE_CHANGE_SUCCESS) {
531     return result;
532   }
533
534   switch (transition) {
535     case GST_STATE_CHANGE_PAUSED_TO_READY:{
536       if (mux->priv->newsegment_ev) {
537         gst_event_unref (mux->priv->newsegment_ev);
538         mux->priv->newsegment_ev = NULL;
539       }
540       if (mux->priv->event_tags) {
541         gst_tag_list_unref (mux->priv->event_tags);
542         mux->priv->event_tags = NULL;
543       }
544       mux->priv->start_tag_size = 0;
545       mux->priv->end_tag_size = 0;
546       mux->priv->render_start_tag = TRUE;
547       mux->priv->render_end_tag = TRUE;
548       mux->priv->current_offset = 0;
549       mux->priv->max_offset = 0;
550       break;
551     }
552     default:
553       break;
554   }
555
556   return result;
557 }