026b13417bef4f9a0a6987b18a1c90cc3e5fb24c
[platform/upstream/gst-plugins-good.git] / gst / aacparse / gstbaseparse.c
1 /* GStreamer
2  * Copyright (C) 2008 Nokia Corporation. All rights reserved.
3  *   Contact: Stefan Kost <stefan.kost@nokia.com>
4  * Copyright (C) 2008 Sebastian Dröge <sebastian.droege@collabora.co.uk>.
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 /**
23  * SECTION:gstbaseparse
24  * @short_description: Base class for stream parsers
25  * @see_also: #GstBaseTransform
26  *
27  * This base class is for parser elements that process data and splits it 
28  * into separate audio/video/whatever frames.
29  *
30  * It provides for:
31  * <itemizedlist>
32  *   <listitem><para>One sinkpad and one srcpad</para></listitem>
33  *   <listitem><para>Handles state changes</para></listitem>
34  *   <listitem><para>Does flushing</para></listitem>
35  *   <listitem><para>Push mode</para></listitem>
36  *   <listitem><para>Pull mode</para></listitem>
37  *   <listitem><para>Handles events (NEWSEGMENT/EOS/FLUSH)</para></listitem>
38  *   <listitem><para>Handles seeking in both modes</para></listitem>
39  *   <listitem><para>
40  *        Handles POSITION/DURATION/SEEKING/FORMAT/CONVERT queries
41  *   </para></listitem>
42  * </itemizedlist>
43  *
44  * The purpose of this base class is to provide a basic functionality of
45  * a parser and share a lot of rather complex code.
46  *
47  * Description of the parsing mechanism:
48  * <orderedlist>
49  * <listitem>
50  *   <itemizedlist><title>Set-up phase</title>
51  *   <listitem><para>
52  *     GstBaseParse class calls @set_sink_caps to inform the subclass about
53  *     incoming sinkpad caps. Subclass should set the srcpad caps accordingly.
54  *   </para></listitem>
55  *   <listitem><para>
56  *     GstBaseParse calls @start to inform subclass that data processing is
57  *     about to start now.
58  *   </para></listitem>
59  *   <listitem><para>
60  *      At least in this point subclass needs to tell the GstBaseParse class
61  *      how big data chunks it wants to receive (min_frame_size). It can do 
62  *      this with @gst_base_parse_set_min_frame_size.
63  *   </para></listitem>
64  *   <listitem><para>
65  *      GstBaseParse class sets up appropriate data passing mode (pull/push)
66  *      and starts to process the data.
67  *   </para></listitem>
68  *   </itemizedlist>
69  * </listitem>
70  * <listitem>
71  *   <itemizedlist>
72  *   <title>Parsing phase</title>
73  *     <listitem><para>
74  *       GstBaseParse gathers at least min_frame_size bytes of data either 
75  *       by pulling it from upstream or collecting buffers into internal
76  *       #GstAdapter.
77  *     </para></listitem>
78  *     <listitem><para>
79  *       A buffer of min_frame_size bytes is passed to subclass with
80  *       @check_valid_frame. Subclass checks the contents and returns TRUE
81  *       if the buffer contains a valid frame. It also needs to set the
82  *       @framesize according to the detected frame size. If buffer didn't
83  *       contain a valid frame, this call must return FALSE and optionally
84  *       set the @skipsize value to inform base class that how many bytes
85  *       it needs to skip in order to find a valid frame. The passed buffer
86  *       is read-only.
87  *     </para></listitem>
88  *     <listitem><para>
89  *       After valid frame is found, it will be passed again to subclass with
90  *       @parse_frame call. Now subclass is responsible for parsing the
91  *       frame contents and setting the buffer timestamp, duration and caps.
92  *     </para></listitem>
93  *     <listitem><para>
94  *       Finally the buffer can be pushed downstream and parsing loop starts
95  *       over again.
96  *     </para></listitem>
97  *     <listitem><para>
98  *       During the parsing process GstBaseClass will handle both srcpad and
99  *       sinkpad events. They will be passed to subclass if @event or
100  *       @src_event callbacks have been provided.
101  *     </para></listitem>
102  *   </itemizedlist>
103  * </listitem>
104  * <listitem>
105  *   <itemizedlist><title>Shutdown phase</title>
106  *   <listitem><para>
107  *     GstBaseParse class calls @stop to inform the subclass that data
108  *     parsing will be stopped.
109  *   </para></listitem>
110  *   </itemizedlist>
111  * </listitem>
112  * </orderedlist>
113  *
114  * Subclass is responsible for providing pad template caps for
115  * source and sink pads. The pads need to be named "sink" and "src". It also 
116  * needs to set the fixed caps on srcpad, when the format is ensured (e.g. 
117  * when base class calls subclass' @set_sink_caps function).
118  *
119  * This base class uses GST_FORMAT_DEFAULT as a meaning of frames. So,
120  * subclass conversion routine needs to know that conversion from
121  * GST_FORMAT_TIME to GST_FORMAT_DEFAULT must return the
122  * frame number that can be found from the given byte position.
123  *
124  * GstBaseParse uses subclasses conversion methods also for seeking. If
125  * subclass doesn't provide @convert function, seeking will get disabled.
126  *
127  * Subclass @start and @stop functions will be called to inform the beginning
128  * and end of data processing.
129  *
130  * Things that subclass need to take care of:
131  * <itemizedlist>
132  *   <listitem><para>Provide pad templates</para></listitem>
133  *   <listitem><para>
134  *      Fixate the source pad caps when appropriate
135  *   </para></listitem>
136  *   <listitem><para>
137  *      Inform base class how big data chunks should be retrieved. This is
138  *      done with @gst_base_parse_set_min_frame_size function.
139  *   </para></listitem>
140  *   <listitem><para>
141  *      Examine data chunks passed to subclass with @check_valid_frame
142  *      and tell if they contain a valid frame
143  *   </para></listitem>
144  *   <listitem><para>
145  *      Set the caps and timestamp to frame that is passed to subclass with
146  *      @parse_frame function.
147  *   </para></listitem>
148  *   <listitem><para>Provide conversion functions</para></listitem>
149  *   <listitem><para>
150  *      Update the duration information with @gst_base_parse_set_duration
151  *   </para></listitem>
152  * </itemizedlist>
153  *
154  */
155
156 /* TODO:
157  *  - Better segment handling:
158  *    - NEWSEGMENT for gaps
159  *    - Not NEWSEGMENT starting at 0 but at first frame timestamp
160  *  - GstIndex support
161  *  - Seek table generation and subclass seek entry injection
162  *  - Accurate seeking
163  *  - In push mode provide a queue of adapter-"queued" buffers for upstream
164  *    buffer metadata
165  *  - Timestamp tracking and setting
166  *  - Handle upstream timestamps
167  *  - Queue buffers/events until caps are set
168  *  - Bitrate tracking => inaccurate seeking, inaccurate duration calculation
169  *  - Let subclass decide if frames outside the segment should be dropped
170  *  - Send queries upstream
171  */
172
173 #ifdef HAVE_CONFIG_H
174 #  include "config.h"
175 #endif
176
177 #include <stdlib.h>
178 #include <string.h>
179
180 #include "gstbaseparse.h"
181
182 GST_DEBUG_CATEGORY_STATIC (gst_base_parse_debug);
183 #define GST_CAT_DEFAULT gst_base_parse_debug
184
185 /* Supported formats */
186 static GstFormat fmtlist[] = {
187   GST_FORMAT_DEFAULT,
188   GST_FORMAT_BYTES,
189   GST_FORMAT_TIME,
190   0
191 };
192
193 #define GST_BASE_PARSE_GET_PRIVATE(obj)  \
194     (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_PARSE, GstBaseParsePrivate))
195
196 struct _GstBaseParsePrivate
197 {
198   GstActivateMode pad_mode;
199
200   gint64 duration;
201   GstFormat duration_fmt;
202
203   guint min_frame_size;
204
205   gboolean discont;
206   gboolean flushing;
207
208   gint64 offset;
209
210   GList *pending_events;
211
212   GstBuffer *cache;
213 };
214
215 struct _GstBaseParseClassPrivate
216 {
217   gpointer _padding;
218 };
219
220 static GstElementClass *parent_class = NULL;
221
222 static void gst_base_parse_base_init (gpointer g_class);
223 static void gst_base_parse_base_finalize (gpointer g_class);
224 static void gst_base_parse_class_init (GstBaseParseClass * klass);
225 static void gst_base_parse_init (GstBaseParse * parse,
226     GstBaseParseClass * klass);
227
228 GType
229 gst_base_parse_get_type (void)
230 {
231   static GType base_parse_type = 0;
232
233   if (!base_parse_type) {
234     static const GTypeInfo base_parse_info = {
235       sizeof (GstBaseParseClass),
236       (GBaseInitFunc) gst_base_parse_base_init,
237       (GBaseFinalizeFunc) gst_base_parse_base_finalize,
238       (GClassInitFunc) gst_base_parse_class_init,
239       NULL,
240       NULL,
241       sizeof (GstBaseParse),
242       0,
243       (GInstanceInitFunc) gst_base_parse_init,
244     };
245
246     base_parse_type = g_type_register_static (GST_TYPE_ELEMENT,
247         "GstAacBaseParse", &base_parse_info, G_TYPE_FLAG_ABSTRACT);
248   }
249   return base_parse_type;
250 }
251
252 static void gst_base_parse_finalize (GObject * object);
253
254 static gboolean gst_base_parse_sink_activate (GstPad * sinkpad);
255 static gboolean gst_base_parse_sink_activate_push (GstPad * pad,
256     gboolean active);
257 static gboolean gst_base_parse_sink_activate_pull (GstPad * pad,
258     gboolean active);
259 static gboolean gst_base_parse_handle_seek (GstBaseParse * parse,
260     GstEvent * event);
261
262 static gboolean gst_base_parse_src_event (GstPad * pad, GstEvent * event);
263 static gboolean gst_base_parse_sink_event (GstPad * pad, GstEvent * event);
264 static gboolean gst_base_parse_query (GstPad * pad, GstQuery * query);
265 static gboolean gst_base_parse_sink_setcaps (GstPad * pad, GstCaps * caps);
266 static const GstQueryType *gst_base_parse_get_querytypes (GstPad * pad);
267
268 static GstFlowReturn gst_base_parse_chain (GstPad * pad, GstBuffer * buffer);
269 static void gst_base_parse_loop (GstPad * pad);
270
271 static gboolean gst_base_parse_check_frame (GstBaseParse * parse,
272     GstBuffer * buffer, guint * framesize, gint * skipsize);
273
274 static GstFlowReturn gst_base_parse_parse_frame (GstBaseParse * parse,
275     GstBuffer * buffer);
276
277 static gboolean gst_base_parse_sink_eventfunc (GstBaseParse * parse,
278     GstEvent * event);
279
280 static gboolean gst_base_parse_src_eventfunc (GstBaseParse * parse,
281     GstEvent * event);
282
283 static gboolean gst_base_parse_is_seekable (GstBaseParse * parse);
284
285 static void gst_base_parse_drain (GstBaseParse * parse);
286
287 static void
288 gst_base_parse_base_init (gpointer g_class)
289 {
290   GstBaseParseClass *klass = GST_BASE_PARSE_CLASS (g_class);
291   GstBaseParseClassPrivate *priv;
292
293   GST_DEBUG_CATEGORY_INIT (gst_base_parse_debug, "aacbaseparse", 0,
294       "baseparse element");
295
296   /* TODO: Remove this once GObject supports class private data */
297   priv = g_slice_new0 (GstBaseParseClassPrivate);
298   if (klass->priv)
299     memcpy (priv, klass->priv, sizeof (GstBaseParseClassPrivate));
300   klass->priv = priv;
301 }
302
303 static void
304 gst_base_parse_base_finalize (gpointer g_class)
305 {
306   GstBaseParseClass *klass = GST_BASE_PARSE_CLASS (g_class);
307
308   g_slice_free (GstBaseParseClassPrivate, klass->priv);
309   klass->priv = NULL;
310 }
311
312 static void
313 gst_base_parse_finalize (GObject * object)
314 {
315   GstBaseParse *parse = GST_BASE_PARSE (object);
316
317   g_mutex_free (parse->parse_lock);
318   g_object_unref (parse->adapter);
319
320   if (parse->pending_segment) {
321     gst_event_replace (&parse->pending_segment, NULL);
322   }
323   if (parse->close_segment) {
324     gst_event_replace (&parse->close_segment, NULL);
325   }
326
327   if (parse->priv->cache) {
328     gst_buffer_unref (parse->priv->cache);
329     parse->priv->cache = NULL;
330   }
331
332   g_list_foreach (parse->priv->pending_events, (GFunc) gst_mini_object_unref,
333       NULL);
334   g_list_free (parse->priv->pending_events);
335   parse->priv->pending_events = NULL;
336
337   G_OBJECT_CLASS (parent_class)->finalize (object);
338 }
339
340 static void
341 gst_base_parse_class_init (GstBaseParseClass * klass)
342 {
343   GObjectClass *gobject_class;
344
345   gobject_class = G_OBJECT_CLASS (klass);
346   g_type_class_add_private (klass, sizeof (GstBaseParsePrivate));
347   parent_class = g_type_class_peek_parent (klass);
348   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_parse_finalize);
349
350   /* Default handlers */
351   klass->check_valid_frame = gst_base_parse_check_frame;
352   klass->parse_frame = gst_base_parse_parse_frame;
353   klass->event = gst_base_parse_sink_eventfunc;
354   klass->src_event = gst_base_parse_src_eventfunc;
355   klass->is_seekable = gst_base_parse_is_seekable;
356 }
357
358 static void
359 gst_base_parse_init (GstBaseParse * parse, GstBaseParseClass * bclass)
360 {
361   GstPadTemplate *pad_template;
362
363   GST_DEBUG_OBJECT (parse, "gst_base_parse_init");
364
365   parse->priv = GST_BASE_PARSE_GET_PRIVATE (parse);
366
367   pad_template =
368       gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "sink");
369   g_return_if_fail (pad_template != NULL);
370   parse->sinkpad = gst_pad_new_from_template (pad_template, "sink");
371   gst_pad_set_event_function (parse->sinkpad,
372       GST_DEBUG_FUNCPTR (gst_base_parse_sink_event));
373   gst_pad_set_setcaps_function (parse->sinkpad,
374       GST_DEBUG_FUNCPTR (gst_base_parse_sink_setcaps));
375   gst_pad_set_chain_function (parse->sinkpad,
376       GST_DEBUG_FUNCPTR (gst_base_parse_chain));
377   gst_pad_set_activate_function (parse->sinkpad,
378       GST_DEBUG_FUNCPTR (gst_base_parse_sink_activate));
379   gst_pad_set_activatepush_function (parse->sinkpad,
380       GST_DEBUG_FUNCPTR (gst_base_parse_sink_activate_push));
381   gst_pad_set_activatepull_function (parse->sinkpad,
382       GST_DEBUG_FUNCPTR (gst_base_parse_sink_activate_pull));
383   gst_element_add_pad (GST_ELEMENT (parse), parse->sinkpad);
384
385   GST_DEBUG_OBJECT (parse, "sinkpad created");
386
387   pad_template =
388       gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
389   g_return_if_fail (pad_template != NULL);
390   parse->srcpad = gst_pad_new_from_template (pad_template, "src");
391   gst_pad_set_event_function (parse->srcpad,
392       GST_DEBUG_FUNCPTR (gst_base_parse_src_event));
393   gst_pad_set_query_type_function (parse->srcpad,
394       GST_DEBUG_FUNCPTR (gst_base_parse_get_querytypes));
395   gst_pad_set_query_function (parse->srcpad,
396       GST_DEBUG_FUNCPTR (gst_base_parse_query));
397   gst_element_add_pad (GST_ELEMENT (parse), parse->srcpad);
398   GST_DEBUG_OBJECT (parse, "src created");
399
400   parse->parse_lock = g_mutex_new ();
401   parse->adapter = gst_adapter_new ();
402   parse->pending_segment = NULL;
403   parse->close_segment = NULL;
404
405   parse->priv->pad_mode = GST_ACTIVATE_NONE;
406   parse->priv->duration = -1;
407   parse->priv->min_frame_size = 1;
408   parse->priv->discont = FALSE;
409   parse->priv->flushing = FALSE;
410   parse->priv->offset = 0;
411   GST_DEBUG_OBJECT (parse, "init ok");
412 }
413
414
415
416 /**
417  * gst_base_parse_check_frame:
418  * @parse: #GstBaseParse.
419  * @buffer: GstBuffer.
420  * @framesize: This will be set to tell the found frame size in bytes.
421  * @skipsize: Output parameter that tells how much data needs to be skipped
422  *            in order to find the following frame header.
423  *
424  * Default callback for check_valid_frame.
425  * 
426  * Returns: Always TRUE.
427  */
428 static gboolean
429 gst_base_parse_check_frame (GstBaseParse * parse,
430     GstBuffer * buffer, guint * framesize, gint * skipsize)
431 {
432   *framesize = GST_BUFFER_SIZE (buffer);
433   *skipsize = 0;
434   return TRUE;
435 }
436
437
438 /**
439  * gst_base_parse_parse_frame:
440  * @parse: #GstBaseParse.
441  * @buffer: #GstBuffer.
442  *
443  * Default callback for parse_frame.
444  */
445 static GstFlowReturn
446 gst_base_parse_parse_frame (GstBaseParse * parse, GstBuffer * buffer)
447 {
448   /* FIXME: Could we even _try_ to do something clever here? */
449   GST_BUFFER_TIMESTAMP (buffer) = GST_CLOCK_TIME_NONE;
450   GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
451   return GST_FLOW_OK;
452 }
453
454
455 /**
456  * gst_base_parse_bytepos_to_time:
457  * @parse: #GstBaseParse.
458  * @bytepos: Position (in bytes) to be converted.
459  * @pos_in_time: #GstClockTime pointer where the result is set.
460  *
461  * Convert given byte position into #GstClockTime format.
462  * 
463  * Returns: TRUE if conversion succeeded.
464  */
465 static gboolean
466 gst_base_parse_bytepos_to_time (GstBaseParse * parse, gint64 bytepos,
467     GstClockTime * pos_in_time)
468 {
469   GstBaseParseClass *klass;
470   gboolean res = FALSE;
471
472   klass = GST_BASE_PARSE_GET_CLASS (parse);
473
474   if (klass->convert) {
475     res = klass->convert (parse, GST_FORMAT_BYTES, bytepos,
476         GST_FORMAT_TIME, (gint64 *) pos_in_time);
477   }
478   return res;
479 }
480
481
482 /**
483  * gst_base_parse_sink_event:
484  * @pad: #GstPad that received the event.
485  * @event: #GstEvent to be handled.
486  *
487  * Handler for sink pad events.
488  *
489  * Returns: TRUE if the event was handled.
490  */
491 static gboolean
492 gst_base_parse_sink_event (GstPad * pad, GstEvent * event)
493 {
494   GstBaseParse *parse;
495   GstBaseParseClass *bclass;
496   gboolean handled = FALSE;
497   gboolean ret = TRUE;
498
499
500   parse = GST_BASE_PARSE (gst_pad_get_parent (pad));
501   bclass = GST_BASE_PARSE_GET_CLASS (parse);
502
503   GST_DEBUG_OBJECT (parse, "handling event %d", GST_EVENT_TYPE (event));
504
505   /* Cache all events except EOS, NEWSEGMENT and FLUSH_STOP if we have a
506    * pending segment */
507   if (parse->pending_segment && GST_EVENT_TYPE (event) != GST_EVENT_EOS
508       && GST_EVENT_TYPE (event) != GST_EVENT_NEWSEGMENT
509       && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_START
510       && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
511     parse->priv->pending_events =
512         g_list_append (parse->priv->pending_events, event);
513     ret = TRUE;
514   } else {
515
516     if (bclass->event)
517       handled = bclass->event (parse, event);
518
519     if (!handled)
520       ret = gst_pad_event_default (pad, event);
521   }
522
523   gst_object_unref (parse);
524   GST_DEBUG_OBJECT (parse, "event handled");
525   return ret;
526 }
527
528
529 /**
530  * gst_base_parse_sink_eventfunc:
531  * @parse: #GstBaseParse.
532  * @event: #GstEvent to be handled.
533  *
534  * Element-level event handler function.
535  *
536  * Returns: TRUE if the event was handled and not need forwarding.
537  */
538 static gboolean
539 gst_base_parse_sink_eventfunc (GstBaseParse * parse, GstEvent * event)
540 {
541   gboolean handled = FALSE;
542   GstEvent **eventp;
543
544   switch (GST_EVENT_TYPE (event)) {
545     case GST_EVENT_NEWSEGMENT:
546     {
547       gdouble rate, applied_rate;
548       GstFormat format;
549       gint64 start, stop, pos, offset = 0;
550       gboolean update;
551
552       gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
553           &format, &start, &stop, &pos);
554
555
556       if (format == GST_FORMAT_BYTES) {
557         GstClockTime seg_start, seg_stop, seg_pos;
558
559         /* stop time is allowed to be open-ended, but not start & pos */
560         seg_stop = GST_CLOCK_TIME_NONE;
561         offset = pos;
562
563         if (gst_base_parse_bytepos_to_time (parse, start, &seg_start) &&
564             gst_base_parse_bytepos_to_time (parse, pos, &seg_pos)) {
565           gst_event_unref (event);
566           event = gst_event_new_new_segment_full (update, rate, applied_rate,
567               GST_FORMAT_TIME, seg_start, seg_stop, seg_pos);
568           format = GST_FORMAT_TIME;
569           GST_DEBUG_OBJECT (parse, "Converted incoming segment to TIME. "
570               "start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT
571               ", pos = %" GST_TIME_FORMAT, GST_TIME_ARGS (seg_start),
572               GST_TIME_ARGS (seg_stop), GST_TIME_ARGS (seg_pos));
573         }
574       }
575
576       if (format != GST_FORMAT_TIME) {
577         /* Unknown incoming segment format. Output a default open-ended 
578          * TIME segment */
579         gst_event_unref (event);
580         event = gst_event_new_new_segment_full (update, rate, applied_rate,
581             GST_FORMAT_TIME, 0, GST_CLOCK_TIME_NONE, 0);
582       }
583
584       gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
585           &format, &start, &stop, &pos);
586
587       gst_segment_set_newsegment_full (&parse->segment, update, rate,
588           applied_rate, format, start, stop, pos);
589
590       GST_DEBUG_OBJECT (parse, "Created newseg rate %g, applied rate %g, "
591           "format %d, start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT
592           ", pos = %" GST_TIME_FORMAT, rate, applied_rate, format,
593           GST_TIME_ARGS (start), GST_TIME_ARGS (stop), GST_TIME_ARGS (pos));
594
595       /* save the segment for later, right before we push a new buffer so that
596        * the caps are fixed and the next linked element can receive
597        * the segment. */
598       eventp = &parse->pending_segment;
599       gst_event_replace (eventp, event);
600       gst_event_unref (event);
601       handled = TRUE;
602
603       /* but finish the current segment */
604       GST_DEBUG_OBJECT (parse, "draining current segment");
605       gst_base_parse_drain (parse);
606       gst_adapter_clear (parse->adapter);
607       parse->priv->offset = offset;
608       break;
609     }
610
611     case GST_EVENT_FLUSH_START:
612       parse->priv->flushing = TRUE;
613       handled = gst_pad_push_event (parse->srcpad, event);
614       /* Wait for _chain() to exit by taking the srcpad STREAM_LOCK */
615       GST_PAD_STREAM_LOCK (parse->srcpad);
616       GST_PAD_STREAM_UNLOCK (parse->srcpad);
617
618       break;
619
620     case GST_EVENT_FLUSH_STOP:
621       gst_adapter_clear (parse->adapter);
622       parse->priv->flushing = FALSE;
623       parse->priv->discont = TRUE;
624       break;
625
626     case GST_EVENT_EOS:
627       gst_base_parse_drain (parse);
628       break;
629
630     default:
631       break;
632   }
633
634   return handled;
635 }
636
637
638 /**
639  * gst_base_parse_src_event:
640  * @pad: #GstPad that received the event.
641  * @event: #GstEvent that was received.
642  *
643  * Handler for source pad events.
644  *
645  * Returns: TRUE if the event was handled.
646  */
647 static gboolean
648 gst_base_parse_src_event (GstPad * pad, GstEvent * event)
649 {
650   GstBaseParse *parse;
651   GstBaseParseClass *bclass;
652   gboolean handled = FALSE;
653   gboolean ret = TRUE;
654
655   parse = GST_BASE_PARSE (gst_pad_get_parent (pad));
656   bclass = GST_BASE_PARSE_GET_CLASS (parse);
657
658   GST_DEBUG_OBJECT (parse, "event %d, %s", GST_EVENT_TYPE (event),
659       GST_EVENT_TYPE_NAME (event));
660
661   if (bclass->src_event)
662     handled = bclass->src_event (parse, event);
663
664   if (!handled)
665     ret = gst_pad_event_default (pad, event);
666
667   gst_object_unref (parse);
668   return ret;
669 }
670
671
672 /**
673  * gst_base_parse_src_eventfunc:
674  * @parse: #GstBaseParse.
675  * @event: #GstEvent that was received.
676  *
677  * Default srcpad event handler.
678  *
679  * Returns: TRUE if the event was handled and can be dropped.
680  */
681 static gboolean
682 gst_base_parse_src_eventfunc (GstBaseParse * parse, GstEvent * event)
683 {
684   gboolean handled = FALSE;
685   GstBaseParseClass *bclass;
686
687   bclass = GST_BASE_PARSE_GET_CLASS (parse);
688
689   switch (GST_EVENT_TYPE (event)) {
690     case GST_EVENT_SEEK:
691     {
692       if (bclass->is_seekable (parse)) {
693         handled = gst_base_parse_handle_seek (parse, event);
694         gst_event_unref (event);
695       }
696       break;
697     }
698     default:
699       break;
700   }
701   return handled;
702 }
703
704
705 /**
706  * gst_base_parse_is_seekable:
707  * @parse: #GstBaseParse.
708  *
709  * Default handler for is_seekable.
710  *
711  * Returns: Always TRUE.
712  */
713 static gboolean
714 gst_base_parse_is_seekable (GstBaseParse * parse)
715 {
716   return TRUE;
717 }
718
719
720 /**
721  * gst_base_parse_handle_and_push_buffer:
722  * @parse: #GstBaseParse.
723  * @klass: #GstBaseParseClass.
724  * @buffer: #GstBuffer.
725  *
726  * Parses the frame from given buffer and pushes it forward. Also performs
727  * timestamp handling and checks the segment limits.
728  *
729  * This is called with srcpad STREAM_LOCK held.
730  *
731  * Returns: #GstFlowReturn
732  */
733 static GstFlowReturn
734 gst_base_parse_handle_and_push_buffer (GstBaseParse * parse,
735     GstBaseParseClass * klass, GstBuffer * buffer)
736 {
737   GstFlowReturn ret;
738   GstClockTime last_stop = GST_CLOCK_TIME_NONE;
739
740   if (parse->priv->discont) {
741     GST_DEBUG_OBJECT (parse, "marking DISCONT");
742     GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
743     parse->priv->discont = FALSE;
744   }
745
746   ret = klass->parse_frame (parse, buffer);
747
748   /* FIXME: Check the output buffer for any missing metadata,
749    *        keep track of timestamp and calculate everything possible
750    *        if not set already */
751
752   if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer))
753     last_stop = GST_BUFFER_TIMESTAMP (buffer);
754   if (last_stop != GST_CLOCK_TIME_NONE && GST_BUFFER_DURATION_IS_VALID (buffer))
755     last_stop += GST_BUFFER_DURATION (buffer);
756
757   /* should have caps by now */
758   g_return_val_if_fail (GST_PAD_CAPS (parse->srcpad), GST_FLOW_ERROR);
759
760   gst_buffer_set_caps (buffer, GST_PAD_CAPS (parse->srcpad));
761
762   /* and should then also be linked downstream, so safe to send some events */
763   if (parse->priv->pad_mode == GST_ACTIVATE_PULL) {
764     if (G_UNLIKELY (parse->close_segment)) {
765       GST_DEBUG_OBJECT (parse, "loop sending close segment");
766       gst_pad_push_event (parse->srcpad, parse->close_segment);
767       parse->close_segment = NULL;
768     }
769
770     if (G_UNLIKELY (parse->pending_segment)) {
771       GST_DEBUG_OBJECT (parse, "loop push pending segment");
772       gst_pad_push_event (parse->srcpad, parse->pending_segment);
773       parse->pending_segment = NULL;
774     }
775   } else {
776     if (G_UNLIKELY (parse->pending_segment)) {
777       GST_DEBUG_OBJECT (parse, "chain pushing a pending segment");
778       gst_pad_push_event (parse->srcpad, parse->pending_segment);
779       parse->pending_segment = NULL;
780     }
781   }
782
783   if (G_UNLIKELY (parse->priv->pending_events)) {
784     GList *l;
785
786     for (l = parse->priv->pending_events; l != NULL; l = l->next) {
787       gst_pad_push_event (parse->srcpad, GST_EVENT (l->data));
788     }
789     g_list_free (parse->priv->pending_events);
790     parse->priv->pending_events = NULL;
791   }
792
793   /* TODO: Add to seek table */
794
795   if (ret == GST_FLOW_OK) {
796     if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
797         GST_CLOCK_TIME_IS_VALID (parse->segment.stop) &&
798         GST_BUFFER_TIMESTAMP (buffer) > parse->segment.stop) {
799       GST_LOG_OBJECT (parse, "Dropped frame, after segment");
800       gst_buffer_unref (buffer);
801     } else if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
802         GST_BUFFER_DURATION_IS_VALID (buffer) &&
803         GST_CLOCK_TIME_IS_VALID (parse->segment.start) &&
804         GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer)
805         < parse->segment.start) {
806       /* FIXME: subclass needs way to override the start as downstream might
807        * need frames before for proper decoding */
808       GST_LOG_OBJECT (parse, "Dropped frame, before segment");
809       gst_buffer_unref (buffer);
810     } else {
811       ret = gst_pad_push (parse->srcpad, buffer);
812       GST_LOG_OBJECT (parse, "frame (%d bytes) pushed: %d",
813           GST_BUFFER_SIZE (buffer), ret);
814     }
815   } else {
816     gst_buffer_unref (buffer);
817   }
818
819   /* Update current running segment position */
820   if (ret == GST_FLOW_OK && last_stop != GST_CLOCK_TIME_NONE)
821     gst_segment_set_last_stop (&parse->segment, GST_FORMAT_TIME, last_stop);
822
823   /* convert internal flow to OK and mark discont for the next buffer. */
824   if (ret == GST_BASE_PARSE_FLOW_DROPPED) {
825     parse->priv->discont = TRUE;
826     ret = GST_FLOW_OK;
827   }
828   return ret;
829 }
830
831
832 /**
833  * gst_base_parse_drain:
834  * @parse: #GstBaseParse.
835  *
836  * Drains the adapter until it is empty. It decreases the min_frame_size to
837  * match the current adapter size and calls chain method until the adapter
838  * is emptied or chain returns with error.
839  */
840 static void
841 gst_base_parse_drain (GstBaseParse * parse)
842 {
843   guint avail;
844
845   for (;;) {
846     avail = gst_adapter_available (parse->adapter);
847     if (!avail)
848       break;
849
850     gst_base_parse_set_min_frame_size (parse, avail);
851     if (gst_base_parse_chain (parse->sinkpad, NULL) != GST_FLOW_OK) {
852       break;
853     }
854   }
855 }
856
857
858 /**
859  * gst_base_parse_chain:
860  * @pad: #GstPad.
861  * @buffer: #GstBuffer.
862  *
863  * Returns: #GstFlowReturn.
864  */
865 static GstFlowReturn
866 gst_base_parse_chain (GstPad * pad, GstBuffer * buffer)
867 {
868   GstBaseParseClass *bclass;
869   GstBaseParse *parse;
870   GstFlowReturn ret = GST_FLOW_OK;
871   GstBuffer *outbuf = NULL;
872   GstBuffer *tmpbuf = NULL;
873   guint fsize = 0;
874   gint skip = -1;
875   const guint8 *data;
876   guint min_size;
877
878   parse = GST_BASE_PARSE (GST_OBJECT_PARENT (pad));
879   bclass = GST_BASE_PARSE_GET_CLASS (parse);
880
881   if (G_LIKELY (buffer)) {
882     GST_LOG_OBJECT (parse, "buffer size: %d, offset = %lld",
883         GST_BUFFER_SIZE (buffer), GST_BUFFER_OFFSET (buffer));
884     gst_adapter_push (parse->adapter, buffer);
885   }
886
887   /* Parse and push as many frames as possible */
888   /* Stop either when adapter is empty or we are flushing */
889   while (!parse->priv->flushing) {
890     tmpbuf = gst_buffer_new ();
891
892     /* Synchronization loop */
893     for (;;) {
894       GST_BASE_PARSE_LOCK (parse);
895       min_size = parse->priv->min_frame_size;
896       GST_BASE_PARSE_UNLOCK (parse);
897
898       /* Collect at least min_frame_size bytes */
899       if (gst_adapter_available (parse->adapter) < min_size) {
900         GST_DEBUG_OBJECT (parse, "not enough data available (only %d bytes)",
901             gst_adapter_available (parse->adapter));
902         gst_buffer_unref (tmpbuf);
903         goto done;
904       }
905
906       data = gst_adapter_peek (parse->adapter, min_size);
907       GST_BUFFER_DATA (tmpbuf) = (guint8 *) data;
908       GST_BUFFER_SIZE (tmpbuf) = min_size;
909       GST_BUFFER_OFFSET (tmpbuf) = parse->priv->offset;
910       GST_BUFFER_FLAG_SET (tmpbuf, GST_MINI_OBJECT_FLAG_READONLY);
911
912       if (parse->priv->discont) {
913         GST_DEBUG_OBJECT (parse, "marking DISCONT");
914         GST_BUFFER_FLAG_SET (tmpbuf, GST_BUFFER_FLAG_DISCONT);
915       }
916
917       skip = -1;
918       if (bclass->check_valid_frame (parse, tmpbuf, &fsize, &skip)) {
919         if (gst_adapter_available (parse->adapter) < fsize) {
920           GST_DEBUG_OBJECT (parse,
921               "found valid frame but not enough data available (only %d bytes)",
922               gst_adapter_available (parse->adapter));
923           goto done;
924         }
925         break;
926       }
927       if (skip > 0) {
928         GST_LOG_OBJECT (parse, "finding sync, skipping %d bytes", skip);
929         gst_adapter_flush (parse->adapter, skip);
930         parse->priv->offset += skip;
931       } else if (skip == -1) {
932         /* subclass didn't touch this value. By default we skip 1 byte */
933         GST_LOG_OBJECT (parse, "finding sync, skipping 1 byte");
934         gst_adapter_flush (parse->adapter, 1);
935         parse->priv->offset++;
936       }
937       /* There is a possibility that subclass set the skip value to zero.
938          This means that it has probably found a frame but wants to ask
939          more data (by increasing the min_size) to be sure of this. */
940     }
941     gst_buffer_unref (tmpbuf);
942     tmpbuf = NULL;
943
944     if (skip > 0) {
945       /* Subclass found the sync, but still wants to skip some data */
946       GST_LOG_OBJECT (parse, "skipping %d bytes", skip);
947       gst_adapter_flush (parse->adapter, skip);
948       parse->priv->offset += skip;
949     }
950
951     /* Grab lock to prevent a race with FLUSH_START handler */
952     GST_PAD_STREAM_LOCK (parse->srcpad);
953
954     /* FLUSH_START event causes the "flushing" flag to be set. In this
955      * case we can leave the frame pushing loop */
956     if (parse->priv->flushing) {
957       GST_PAD_STREAM_UNLOCK (parse->srcpad);
958       break;
959     }
960
961     /* FIXME: Would it be more efficient to make a subbuffer instead? */
962     outbuf = gst_adapter_take_buffer (parse->adapter, fsize);
963
964     /* Subclass may want to know the data offset */
965     GST_BUFFER_OFFSET (outbuf) = parse->priv->offset;
966     parse->priv->offset += fsize;
967
968     ret = gst_base_parse_handle_and_push_buffer (parse, bclass, outbuf);
969     GST_PAD_STREAM_UNLOCK (parse->srcpad);
970
971     if (ret != GST_FLOW_OK) {
972       GST_LOG_OBJECT (parse, "push returned %d", ret);
973       break;
974     }
975   }
976
977 done:
978   GST_LOG_OBJECT (parse, "chain leaving");
979   return ret;
980 }
981
982 static GstFlowReturn
983 gst_base_parse_pull_range (GstBaseParse * parse, guint size,
984     GstBuffer ** buffer)
985 {
986   GstFlowReturn ret = GST_FLOW_OK;
987
988   g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
989
990   /* Caching here actually makes much less difference than one would expect.
991    * We do it mainly to avoid pulling buffers of 1 byte all the time */
992   if (parse->priv->cache) {
993     guint64 cache_offset = GST_BUFFER_OFFSET (parse->priv->cache);
994     guint cache_size = GST_BUFFER_SIZE (parse->priv->cache);
995
996     if (cache_offset <= parse->priv->offset &&
997         (parse->priv->offset + size) < (cache_offset + cache_size)) {
998       *buffer = gst_buffer_create_sub (parse->priv->cache,
999           parse->priv->offset - cache_offset, size);
1000       GST_BUFFER_OFFSET (*buffer) = parse->priv->offset;
1001       return GST_FLOW_OK;
1002     }
1003     /* not enough data in the cache, free cache and get a new one */
1004     gst_buffer_unref (parse->priv->cache);
1005     parse->priv->cache = NULL;
1006   }
1007
1008   /* refill the cache */
1009   ret =
1010       gst_pad_pull_range (parse->sinkpad, parse->priv->offset, MAX (size,
1011           64 * 1024), &parse->priv->cache);
1012   if (ret != GST_FLOW_OK) {
1013     parse->priv->cache = NULL;
1014     return ret;
1015   }
1016
1017   if (GST_BUFFER_SIZE (parse->priv->cache) >= size) {
1018     *buffer = gst_buffer_create_sub (parse->priv->cache, 0, size);
1019     GST_BUFFER_OFFSET (*buffer) = parse->priv->offset;
1020     return GST_FLOW_OK;
1021   }
1022
1023   /* Not possible to get enough data, try a last time with
1024    * requesting exactly the size we need */
1025   gst_buffer_unref (parse->priv->cache);
1026   parse->priv->cache = NULL;
1027
1028   ret = gst_pad_pull_range (parse->sinkpad, parse->priv->offset, size,
1029       &parse->priv->cache);
1030
1031   if (ret != GST_FLOW_OK) {
1032     GST_DEBUG_OBJECT (parse, "pull_range returned %d", ret);
1033     *buffer = NULL;
1034     return ret;
1035   }
1036
1037   if (GST_BUFFER_SIZE (parse->priv->cache) < size) {
1038     GST_WARNING_OBJECT (parse, "Dropping short buffer at offset %"
1039         G_GUINT64_FORMAT ": wanted %u bytes, got %u bytes", parse->priv->offset,
1040         size, GST_BUFFER_SIZE (parse->priv->cache));
1041
1042     gst_buffer_unref (parse->priv->cache);
1043     parse->priv->cache = NULL;
1044
1045     *buffer = NULL;
1046     return GST_FLOW_UNEXPECTED;
1047   }
1048
1049   *buffer = gst_buffer_create_sub (parse->priv->cache, 0, size);
1050   GST_BUFFER_OFFSET (*buffer) = parse->priv->offset;
1051
1052   return GST_FLOW_OK;
1053 }
1054
1055 /**
1056  * gst_base_parse_loop:
1057  * @pad: GstPad
1058  *
1059  * Loop that is used in pull mode to retrieve data from upstream.
1060  */
1061 static void
1062 gst_base_parse_loop (GstPad * pad)
1063 {
1064   GstBaseParse *parse;
1065   GstBaseParseClass *klass;
1066   GstBuffer *buffer, *outbuf;
1067   gboolean ret = FALSE;
1068   guint fsize = 0, min_size;
1069   gint skip = 0;
1070
1071   parse = GST_BASE_PARSE (gst_pad_get_parent (pad));
1072   klass = GST_BASE_PARSE_GET_CLASS (parse);
1073
1074   /* TODO: Check if we reach segment stop limit */
1075
1076   while (TRUE) {
1077
1078     GST_BASE_PARSE_LOCK (parse);
1079     min_size = parse->priv->min_frame_size;
1080     GST_BASE_PARSE_UNLOCK (parse);
1081
1082     ret = gst_base_parse_pull_range (parse, min_size, &buffer);
1083
1084     if (ret == GST_FLOW_UNEXPECTED)
1085       goto eos;
1086     else if (ret != GST_FLOW_OK)
1087       goto need_pause;
1088
1089     if (parse->priv->discont) {
1090       GST_DEBUG_OBJECT (parse, "marking DISCONT");
1091       GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1092     }
1093
1094     skip = -1;
1095     if (klass->check_valid_frame (parse, buffer, &fsize, &skip)) {
1096       break;
1097     }
1098     if (skip > 0) {
1099       GST_LOG_OBJECT (parse, "finding sync, skipping %d bytes", skip);
1100       parse->priv->offset += skip;
1101     } else if (skip == -1) {
1102       GST_LOG_OBJECT (parse, "finding sync, skipping 1 byte");
1103       parse->priv->offset++;
1104     }
1105     GST_DEBUG_OBJECT (parse, "finding sync...");
1106     gst_buffer_unref (buffer);
1107   }
1108
1109   if (fsize <= GST_BUFFER_SIZE (buffer)) {
1110     outbuf = gst_buffer_create_sub (buffer, 0, fsize);
1111     GST_BUFFER_OFFSET (outbuf) = GST_BUFFER_OFFSET (buffer);
1112     gst_buffer_unref (buffer);
1113   } else {
1114     gst_buffer_unref (buffer);
1115     ret = gst_base_parse_pull_range (parse, fsize, &outbuf);
1116
1117     if (ret == GST_FLOW_UNEXPECTED)
1118       goto eos;
1119     else if (ret != GST_FLOW_OK)
1120       goto need_pause;
1121   }
1122
1123   parse->priv->offset += fsize;
1124
1125   /* Does the subclass want to skip too? */
1126   if (skip > 0)
1127     parse->priv->offset += skip;
1128
1129   /* This always unrefs the outbuf, even if error occurs */
1130   ret = gst_base_parse_handle_and_push_buffer (parse, klass, outbuf);
1131
1132   if (ret != GST_FLOW_OK) {
1133     GST_DEBUG_OBJECT (parse, "flow: %s", gst_flow_get_name (ret));
1134     if (GST_FLOW_IS_FATAL (ret)) {
1135       GST_ELEMENT_ERROR (parse, STREAM, FAILED, (NULL),
1136           ("streaming task paused, reason: %s", gst_flow_get_name (ret)));
1137       gst_pad_push_event (parse->srcpad, gst_event_new_eos ());
1138     }
1139     goto need_pause;
1140   }
1141
1142   gst_object_unref (parse);
1143   return;
1144
1145 need_pause:
1146   {
1147     GST_LOG_OBJECT (parse, "pausing task");
1148     gst_pad_pause_task (pad);
1149     gst_object_unref (parse);
1150     return;
1151   }
1152 eos:
1153   {
1154     GST_LOG_OBJECT (parse, "pausing task %d", ret);
1155     gst_pad_push_event (parse->srcpad, gst_event_new_eos ());
1156     gst_pad_pause_task (pad);
1157     gst_object_unref (parse);
1158     return;
1159   }
1160 }
1161
1162
1163 /**
1164  * gst_base_parse_sink_activate:
1165  * @sinkpad: #GstPad to be activated.
1166  *
1167  * Returns: TRUE if activation succeeded.
1168  */
1169 static gboolean
1170 gst_base_parse_sink_activate (GstPad * sinkpad)
1171 {
1172   GstBaseParse *parse;
1173   gboolean result = TRUE;
1174
1175   parse = GST_BASE_PARSE (gst_pad_get_parent (sinkpad));
1176
1177   GST_DEBUG_OBJECT (parse, "sink activate");
1178
1179   if (gst_pad_check_pull_range (sinkpad)) {
1180     GST_DEBUG_OBJECT (parse, "trying to activate in pull mode");
1181     result = gst_pad_activate_pull (sinkpad, TRUE);
1182   } else {
1183     GST_DEBUG_OBJECT (parse, "trying to activate in push mode");
1184     result = gst_pad_activate_push (sinkpad, TRUE);
1185   }
1186
1187   GST_DEBUG_OBJECT (parse, "sink activate return %d", result);
1188   gst_object_unref (parse);
1189   return result;
1190 }
1191
1192
1193 /**
1194  * gst_base_parse_activate:
1195  * @parse: #GstBaseParse.
1196  * @active: TRUE if element will be activated, FALSE if disactivated.
1197  *
1198  * Returns: TRUE if the operation succeeded.
1199  */
1200 static gboolean
1201 gst_base_parse_activate (GstBaseParse * parse, gboolean active)
1202 {
1203   GstBaseParseClass *klass;
1204   gboolean result = FALSE;
1205
1206   GST_DEBUG_OBJECT (parse, "activate");
1207
1208   klass = GST_BASE_PARSE_GET_CLASS (parse);
1209
1210   if (active) {
1211     if (parse->priv->pad_mode == GST_ACTIVATE_NONE && klass->start)
1212       result = klass->start (parse);
1213
1214     GST_OBJECT_LOCK (parse);
1215     gst_segment_init (&parse->segment, GST_FORMAT_TIME);
1216     parse->priv->duration = -1;
1217     parse->priv->discont = FALSE;
1218     parse->priv->flushing = FALSE;
1219     parse->priv->offset = 0;
1220
1221     if (parse->pending_segment)
1222       gst_event_unref (parse->pending_segment);
1223
1224     parse->pending_segment =
1225         gst_event_new_new_segment (FALSE, parse->segment.rate,
1226         parse->segment.format,
1227         parse->segment.start, parse->segment.stop, parse->segment.last_stop);
1228
1229     GST_OBJECT_UNLOCK (parse);
1230   } else {
1231     /* We must make sure streaming has finished before resetting things
1232      * and calling the ::stop vfunc */
1233     GST_PAD_STREAM_LOCK (parse->sinkpad);
1234     GST_PAD_STREAM_UNLOCK (parse->sinkpad);
1235
1236     if (parse->priv->pad_mode != GST_ACTIVATE_NONE && klass->stop)
1237       result = klass->stop (parse);
1238
1239     g_list_foreach (parse->priv->pending_events, (GFunc) gst_mini_object_unref,
1240         NULL);
1241     g_list_free (parse->priv->pending_events);
1242     parse->priv->pending_events = NULL;
1243
1244     if (parse->priv->cache) {
1245       gst_buffer_unref (parse->priv->cache);
1246       parse->priv->cache = NULL;
1247     }
1248
1249     parse->priv->pad_mode = GST_ACTIVATE_NONE;
1250   }
1251   GST_DEBUG_OBJECT (parse, "activate: %d", result);
1252   return result;
1253 }
1254
1255
1256 /**
1257  * gst_base_parse_sink_activate_push:
1258  * @pad: #GstPad to be (de)activated.
1259  * @active: TRUE when activating, FALSE when deactivating.
1260  *
1261  * Returns: TRUE if (de)activation succeeded.
1262  */
1263 static gboolean
1264 gst_base_parse_sink_activate_push (GstPad * pad, gboolean active)
1265 {
1266   gboolean result = TRUE;
1267   GstBaseParse *parse;
1268
1269   parse = GST_BASE_PARSE (gst_pad_get_parent (pad));
1270
1271   GST_DEBUG_OBJECT (parse, "sink activate push");
1272
1273   result = gst_base_parse_activate (parse, active);
1274
1275   if (result)
1276     parse->priv->pad_mode = active ? GST_ACTIVATE_PUSH : GST_ACTIVATE_NONE;
1277
1278   GST_DEBUG_OBJECT (parse, "sink activate push: %d", result);
1279
1280   gst_object_unref (parse);
1281   return result;
1282 }
1283
1284
1285 /**
1286  * gst_base_parse_sink_activate_pull:
1287  * @sinkpad: #GstPad to be (de)activated.
1288  * @active: TRUE when activating, FALSE when deactivating.
1289  *
1290  * Returns: TRUE if (de)activation succeeded.
1291  */
1292 static gboolean
1293 gst_base_parse_sink_activate_pull (GstPad * sinkpad, gboolean active)
1294 {
1295   gboolean result = FALSE;
1296   GstBaseParse *parse;
1297
1298   parse = GST_BASE_PARSE (gst_pad_get_parent (sinkpad));
1299
1300   GST_DEBUG_OBJECT (parse, "activate pull");
1301
1302   result = gst_base_parse_activate (parse, active);
1303
1304   if (result) {
1305     if (active) {
1306       result &= gst_pad_start_task (sinkpad,
1307           (GstTaskFunction) gst_base_parse_loop, sinkpad);
1308     } else {
1309       result &= gst_pad_stop_task (sinkpad);
1310     }
1311   }
1312
1313   if (result)
1314     parse->priv->pad_mode = active ? GST_ACTIVATE_PULL : GST_ACTIVATE_NONE;
1315
1316   GST_DEBUG_OBJECT (parse, "sink activate pull: %d", result);
1317
1318   gst_object_unref (parse);
1319   return result;
1320 }
1321
1322
1323 /**
1324  * gst_base_parse_set_duration:
1325  * @parse: #GstBaseParse.
1326  * @fmt: #GstFormat.
1327  * @duration: duration value.
1328  *
1329  * Sets the duration of the currently playing media. Subclass can use this
1330  * when it notices a change in the media duration.
1331  */
1332 void
1333 gst_base_parse_set_duration (GstBaseParse * parse,
1334     GstFormat fmt, gint64 duration)
1335 {
1336   g_return_if_fail (parse != NULL);
1337
1338   GST_BASE_PARSE_LOCK (parse);
1339   if (duration != parse->priv->duration) {
1340     GstMessage *m;
1341
1342     m = gst_message_new_duration (GST_OBJECT (parse), fmt, duration);
1343     gst_element_post_message (GST_ELEMENT (parse), m);
1344
1345     /* TODO: what about duration tag? */
1346   }
1347   parse->priv->duration = duration;
1348   parse->priv->duration_fmt = fmt;
1349   GST_DEBUG_OBJECT (parse, "set duration: %lld", duration);
1350   GST_BASE_PARSE_UNLOCK (parse);
1351 }
1352
1353
1354 /**
1355  * gst_base_parse_set_min_frame_size:
1356  * @parse: #GstBaseParse.
1357  * @min_size: Minimum size of the data that this base class should give to
1358  *            subclass.
1359  *
1360  * Subclass can use this function to tell the base class that it needs to
1361  * give at least #min_size buffers.
1362  */
1363 void
1364 gst_base_parse_set_min_frame_size (GstBaseParse * parse, guint min_size)
1365 {
1366   g_return_if_fail (parse != NULL);
1367
1368   GST_BASE_PARSE_LOCK (parse);
1369   parse->priv->min_frame_size = min_size;
1370   GST_LOG_OBJECT (parse, "set frame_min_size: %d", min_size);
1371   GST_BASE_PARSE_UNLOCK (parse);
1372 }
1373
1374
1375 /**
1376  * gst_base_parse_get_querytypes:
1377  * @pad: GstPad
1378  *
1379  * Returns: A table of #GstQueryType items describing supported query types.
1380  */
1381 static const GstQueryType *
1382 gst_base_parse_get_querytypes (GstPad * pad)
1383 {
1384   static const GstQueryType list[] = {
1385     GST_QUERY_POSITION,
1386     GST_QUERY_DURATION,
1387     GST_QUERY_FORMATS,
1388     GST_QUERY_SEEKING,
1389     GST_QUERY_CONVERT,
1390     0
1391   };
1392
1393   return list;
1394 }
1395
1396
1397 /**
1398  * gst_base_parse_query:
1399  * @pad: #GstPad.
1400  * @query: #GstQuery.
1401  *
1402  * Returns: TRUE on success.
1403  */
1404 static gboolean
1405 gst_base_parse_query (GstPad * pad, GstQuery * query)
1406 {
1407   GstBaseParse *parse;
1408   GstBaseParseClass *klass;
1409   gboolean res = FALSE;
1410
1411   parse = GST_BASE_PARSE (GST_PAD_PARENT (pad));
1412   klass = GST_BASE_PARSE_GET_CLASS (parse);
1413
1414   /* If subclass doesn't provide conversion function we can't reply
1415      to the query either */
1416   if (!klass->convert) {
1417     return FALSE;
1418   }
1419
1420   switch (GST_QUERY_TYPE (query)) {
1421     case GST_QUERY_POSITION:
1422     {
1423       gint64 dest_value;
1424       GstFormat format;
1425
1426       GST_DEBUG_OBJECT (parse, "position query");
1427
1428       gst_query_parse_position (query, &format, NULL);
1429
1430       g_mutex_lock (parse->parse_lock);
1431
1432       if (format == GST_FORMAT_BYTES) {
1433         dest_value = parse->priv->offset;
1434         res = TRUE;
1435       } else if (format == parse->segment.format &&
1436           GST_CLOCK_TIME_IS_VALID (parse->segment.last_stop)) {
1437         dest_value = parse->segment.last_stop;
1438         res = TRUE;
1439       } else {
1440         /* priv->offset is updated in both PUSH/PULL modes */
1441         res = klass->convert (parse, GST_FORMAT_BYTES, parse->priv->offset,
1442             format, &dest_value);
1443       }
1444       g_mutex_unlock (parse->parse_lock);
1445
1446       if (res)
1447         gst_query_set_position (query, format, dest_value);
1448       else
1449         res = gst_pad_query_default (pad, query);
1450
1451       break;
1452     }
1453     case GST_QUERY_DURATION:
1454     {
1455       GstFormat format;
1456       gint64 dest_value;
1457
1458       GST_DEBUG_OBJECT (parse, "duration query");
1459
1460       gst_query_parse_duration (query, &format, NULL);
1461
1462       g_mutex_lock (parse->parse_lock);
1463
1464       if (format == GST_FORMAT_BYTES) {
1465         res = gst_pad_query_peer_duration (parse->sinkpad, &format,
1466             &dest_value);
1467       } else if (parse->priv->duration != -1 &&
1468           format == parse->priv->duration_fmt) {
1469         dest_value = parse->priv->duration;
1470         res = TRUE;
1471       } else if (parse->priv->duration != -1) {
1472         res = klass->convert (parse, parse->priv->duration_fmt,
1473             parse->priv->duration, format, &dest_value);
1474       }
1475
1476       g_mutex_unlock (parse->parse_lock);
1477
1478       if (res)
1479         gst_query_set_duration (query, format, dest_value);
1480       else
1481         res = gst_pad_query_default (pad, query);
1482       break;
1483     }
1484     case GST_QUERY_SEEKING:
1485     {
1486       GstFormat fmt;
1487       gboolean seekable = FALSE;
1488
1489       GST_DEBUG_OBJECT (parse, "seeking query");
1490
1491       gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
1492
1493       if (fmt != GST_FORMAT_TIME) {
1494         return gst_pad_query_default (pad, query);
1495       }
1496
1497       seekable = klass->is_seekable (parse);
1498
1499       /* TODO: could this duration be calculated/converted if subclass
1500          hasn't given it? */
1501       gst_query_set_seeking (query, GST_FORMAT_TIME, seekable, 0,
1502           (parse->priv->duration == -1) ?
1503           GST_CLOCK_TIME_NONE : parse->priv->duration);
1504
1505       GST_DEBUG_OBJECT (parse, "seekable: %d", seekable);
1506       res = TRUE;
1507       break;
1508     }
1509     case GST_QUERY_FORMATS:
1510       gst_query_set_formatsv (query, 3, fmtlist);
1511       res = TRUE;
1512       break;
1513
1514     case GST_QUERY_CONVERT:
1515     {
1516       GstFormat src_format, dest_format;
1517       gint64 src_value, dest_value;
1518
1519       gst_query_parse_convert (query, &src_format, &src_value,
1520           &dest_format, &dest_value);
1521
1522       /* FIXME: hm? doesn't make sense 
1523        * We require all those values to be given
1524        if (src_format && src_value && dest_format && dest_value ) { */
1525       res = klass->convert (parse, src_format, src_value,
1526           dest_format, &dest_value);
1527       if (res) {
1528         gst_query_set_convert (query, src_format, src_value,
1529             dest_format, dest_value);
1530       }
1531       /*} */
1532       break;
1533     }
1534     default:
1535       res = gst_pad_query_default (pad, query);
1536       break;
1537   }
1538   return res;
1539 }
1540
1541
1542 /**
1543  * gst_base_parse_handle_seek:
1544  * @parse: #GstBaseParse.
1545  * @event: #GstEvent.
1546  *
1547  * Returns: TRUE if seek succeeded.
1548  */
1549 static gboolean
1550 gst_base_parse_handle_seek (GstBaseParse * parse, GstEvent * event)
1551 {
1552   GstBaseParseClass *klass;
1553   gdouble rate;
1554   GstFormat format;
1555   GstSeekFlags flags;
1556   GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
1557   gboolean flush, update, res = TRUE;
1558   gint64 cur, stop, seekpos;
1559   GstSegment seeksegment = { 0, };
1560   GstFormat dstformat;
1561
1562   klass = GST_BASE_PARSE_GET_CLASS (parse);
1563
1564   gst_event_parse_seek (event, &rate, &format, &flags,
1565       &cur_type, &cur, &stop_type, &stop);
1566
1567   /* no negative rates yet */
1568   if (rate < 0.0)
1569     goto negative_rate;
1570
1571   if (cur_type != GST_SEEK_TYPE_SET)
1572     goto wrong_type;
1573
1574   /* For any format other than TIME, see if upstream handles
1575    * it directly or fail. For TIME, try upstream, but do it ourselves if
1576    * it fails upstream */
1577   if (format != GST_FORMAT_TIME) {
1578     gst_event_ref (event);
1579     return gst_pad_push_event (parse->sinkpad, event);
1580   } else {
1581     gst_event_ref (event);
1582     if (gst_pad_push_event (parse->sinkpad, event))
1583       return TRUE;
1584   }
1585
1586   /* get flush flag */
1587   flush = flags & GST_SEEK_FLAG_FLUSH;
1588
1589   dstformat = GST_FORMAT_BYTES;
1590   if (!gst_pad_query_convert (parse->srcpad, format, cur, &dstformat, &seekpos)) {
1591     GST_DEBUG_OBJECT (parse, "conversion failed");
1592     return FALSE;
1593   }
1594
1595   GST_DEBUG_OBJECT (parse, "seek position %lld in bytes: %lld", cur, seekpos);
1596
1597   if (parse->priv->pad_mode == GST_ACTIVATE_PULL) {
1598     gint64 last_stop;
1599
1600     GST_DEBUG_OBJECT (parse, "seek in PULL mode");
1601
1602     if (flush) {
1603       if (parse->srcpad) {
1604         GST_DEBUG_OBJECT (parse, "sending flush start");
1605         gst_pad_push_event (parse->srcpad, gst_event_new_flush_start ());
1606       }
1607     } else {
1608       gst_pad_pause_task (parse->sinkpad);
1609     }
1610
1611     /* we should now be able to grab the streaming thread because we stopped it
1612      * with the above flush/pause code */
1613     GST_PAD_STREAM_LOCK (parse->sinkpad);
1614
1615     /* save current position */
1616     last_stop = parse->segment.last_stop;
1617     GST_DEBUG_OBJECT (parse, "stopped streaming at %" G_GINT64_FORMAT,
1618         last_stop);
1619
1620     /* copy segment, we need this because we still need the old
1621      * segment when we close the current segment. */
1622     memcpy (&seeksegment, &parse->segment, sizeof (GstSegment));
1623
1624     GST_DEBUG_OBJECT (parse, "configuring seek");
1625     gst_segment_set_seek (&seeksegment, rate, format, flags,
1626         cur_type, cur, stop_type, stop, &update);
1627
1628     /* figure out the last position we need to play. If it's configured (stop !=
1629      * -1), use that, else we play until the total duration of the file */
1630     if ((stop = seeksegment.stop) == -1)
1631       stop = seeksegment.duration;
1632
1633     parse->priv->offset = seekpos;
1634
1635     /* prepare for streaming again */
1636     if (flush) {
1637       GST_DEBUG_OBJECT (parse, "sending flush stop");
1638       gst_pad_push_event (parse->srcpad, gst_event_new_flush_stop ());
1639     } else {
1640       if (parse->close_segment)
1641         gst_event_unref (parse->close_segment);
1642
1643       parse->close_segment = gst_event_new_new_segment (TRUE,
1644           parse->segment.rate, parse->segment.format,
1645           parse->segment.accum, parse->segment.last_stop, parse->segment.accum);
1646
1647       /* keep track of our last_stop */
1648       seeksegment.accum = parse->segment.last_stop;
1649
1650       GST_DEBUG_OBJECT (parse, "Created close seg format %d, "
1651           "start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT
1652           ", pos = %" GST_TIME_FORMAT, format,
1653           GST_TIME_ARGS (parse->segment.accum),
1654           GST_TIME_ARGS (parse->segment.last_stop),
1655           GST_TIME_ARGS (parse->segment.accum));
1656     }
1657
1658     memcpy (&parse->segment, &seeksegment, sizeof (GstSegment));
1659
1660     /* store the newsegment event so it can be sent from the streaming thread. */
1661     if (parse->pending_segment)
1662       gst_event_unref (parse->pending_segment);
1663
1664     /* This will be sent later in _loop() */
1665     parse->pending_segment =
1666         gst_event_new_new_segment (FALSE, parse->segment.rate,
1667         parse->segment.format,
1668         parse->segment.last_stop, stop, parse->segment.last_stop);
1669
1670     GST_DEBUG_OBJECT (parse, "Created newseg format %d, "
1671         "start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT
1672         ", pos = %" GST_TIME_FORMAT, format,
1673         GST_TIME_ARGS (parse->segment.last_stop),
1674         GST_TIME_ARGS (stop), GST_TIME_ARGS (parse->segment.last_stop));
1675
1676     /* mark discont if we are going to stream from another position. */
1677     if (last_stop != parse->segment.last_stop) {
1678       GST_DEBUG_OBJECT (parse,
1679           "mark DISCONT, we did a seek to another position");
1680       parse->priv->discont = TRUE;
1681     }
1682
1683     /* Start streaming thread if paused */
1684     gst_pad_start_task (parse->sinkpad,
1685         (GstTaskFunction) gst_base_parse_loop, parse->sinkpad);
1686
1687     GST_PAD_STREAM_UNLOCK (parse->sinkpad);
1688   } else {
1689     GstEvent *new_event;
1690     /* The only thing we need to do in PUSH-mode is to send the
1691        seek event (in bytes) to upstream. Segment / flush handling happens
1692        in corresponding src event handlers */
1693     GST_DEBUG_OBJECT (parse, "seek in PUSH mode");
1694     new_event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flush,
1695         GST_SEEK_TYPE_SET, seekpos, stop_type, stop);
1696
1697     res = gst_pad_push_event (parse->sinkpad, new_event);
1698   }
1699
1700 done:
1701   return res;
1702
1703   /* ERRORS */
1704 negative_rate:
1705   {
1706     GST_DEBUG_OBJECT (parse, "negative playback rates are not supported yet.");
1707     res = FALSE;
1708     goto done;
1709   }
1710 wrong_type:
1711   {
1712     GST_DEBUG_OBJECT (parse, "unsupported seek type.");
1713     res = FALSE;
1714     goto done;
1715   }
1716 }
1717
1718
1719 /**
1720  * gst_base_parse_sink_setcaps:
1721  * @pad: #GstPad.
1722  * @caps: #GstCaps.
1723  *
1724  * Returns: TRUE if caps were accepted.
1725  */
1726 static gboolean
1727 gst_base_parse_sink_setcaps (GstPad * pad, GstCaps * caps)
1728 {
1729   GstBaseParse *parse;
1730   GstBaseParseClass *klass;
1731   gboolean res = TRUE;
1732
1733   parse = GST_BASE_PARSE (GST_PAD_PARENT (pad));
1734   klass = GST_BASE_PARSE_GET_CLASS (parse);
1735
1736   GST_DEBUG_OBJECT (parse, "caps: %" GST_PTR_FORMAT, caps);
1737
1738   if (klass->set_sink_caps)
1739     res = klass->set_sink_caps (parse, caps);
1740
1741   parse->negotiated = res;
1742   return res && gst_pad_set_caps (pad, caps);
1743 }