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>.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * SECTION:gstbaseparse
24 * @short_description: Base class for stream parsers
25 * @see_also: #GstBaseTransform
27 * This base class is for parser elements that process data and splits it
28 * into separate audio/video/whatever frames.
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>
40 * Handles POSITION/DURATION/SEEKING/FORMAT/CONVERT queries
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.
47 * Description of the parsing mechanism:
50 * <itemizedlist><title>Set-up phase</title>
52 * GstBaseParse class calls @set_sink_caps to inform the subclass about
53 * incoming sinkpad caps. Subclass should set the srcpad caps accordingly.
56 * GstBaseParse calls @start to inform subclass that data processing is
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.
65 * GstBaseParse class sets up appropriate data passing mode (pull/push)
66 * and starts to process the data.
72 * <title>Parsing phase</title>
74 * GstBaseParse gathers at least min_frame_size bytes of data either
75 * by pulling it from upstream or collecting buffers into internal
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. Note that @check_valid_frame might receive any small
87 * amount of input data when leftover data is being drained (e.g. at EOS).
90 * After valid frame is found, it will be passed again to subclass with
91 * @parse_frame call. Now subclass is responsible for parsing the
92 * frame contents and setting the buffer timestamp, duration and caps.
95 * Finally the buffer can be pushed downstream and parsing loop starts
99 * During the parsing process GstBaseParseClass will handle both srcpad and
100 * sinkpad events. They will be passed to subclass if @event or
101 * @src_event callbacks have been provided.
106 * <itemizedlist><title>Shutdown phase</title>
108 * GstBaseParse class calls @stop to inform the subclass that data
109 * parsing will be stopped.
115 * Subclass is responsible for providing pad template caps for
116 * source and sink pads. The pads need to be named "sink" and "src". It also
117 * needs to set the fixed caps on srcpad, when the format is ensured (e.g.
118 * when base class calls subclass' @set_sink_caps function).
120 * This base class uses GST_FORMAT_DEFAULT as a meaning of frames. So,
121 * subclass conversion routine needs to know that conversion from
122 * GST_FORMAT_TIME to GST_FORMAT_DEFAULT must return the
123 * frame number that can be found from the given byte position.
125 * GstBaseParse uses subclasses conversion methods also for seeking. If
126 * subclass doesn't provide @convert function, seeking will get disabled.
128 * Subclass @start and @stop functions will be called to inform the beginning
129 * and end of data processing.
131 * Things that subclass need to take care of:
133 * <listitem><para>Provide pad templates</para></listitem>
135 * Fixate the source pad caps when appropriate
138 * Inform base class how big data chunks should be retrieved. This is
139 * done with @gst_base_parse_set_min_frame_size function.
142 * Examine data chunks passed to subclass with @check_valid_frame
143 * and tell if they contain a valid frame
146 * Set the caps and timestamp to frame that is passed to subclass with
147 * @parse_frame function.
149 * <listitem><para>Provide conversion functions</para></listitem>
151 * Update the duration information with @gst_base_parse_set_duration
154 * Alternatively, parsing (or specs) might yield a frames per seconds rate
155 * which can be provided to GstBaseParse to enable it to cater for
156 * buffer time metadata (which will be taken from upstream as much as possible).
157 * Internally keeping track of frames and respective
158 * sizes that have been pushed provides GstBaseParse with a bytes per frame
159 * rate. A default @convert (used if not overriden) will then use these
160 * rates to perform obvious conversions. These rates are also used to update
161 * (estimated) duration at regular frame intervals.
162 * If no (fixed) frames per second rate applies, default conversion will be
163 * based on (estimated) bytes per second (but no default buffer metadata
164 * can be provided in this case).
171 * - Better segment handling:
172 * - NEWSEGMENT for gaps
173 * - Not NEWSEGMENT starting at 0 but at first frame timestamp
175 * - Seek table generation and subclass seek entry injection
177 * - In push mode provide a queue of adapter-"queued" buffers for upstream
179 * - Queue buffers/events until caps are set
180 * - Let subclass decide if frames outside the segment should be dropped
181 * - Send queries upstream
191 #include "gstbaseparse.h"
193 #define MIN_FRAMES_TO_POST_BITRATE 10
195 GST_DEBUG_CATEGORY_STATIC (gst_base_parse_debug);
196 #define GST_CAT_DEFAULT gst_base_parse_debug
198 /* Supported formats */
199 static GstFormat fmtlist[] = {
206 #define GST_BASE_PARSE_GET_PRIVATE(obj) \
207 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_PARSE, GstBaseParsePrivate))
209 struct _GstBaseParsePrivate
211 GstActivateMode pad_mode;
214 GstFormat duration_fmt;
215 gint64 estimated_duration;
217 guint min_frame_size;
218 gboolean passthrough;
219 guint fps_num, fps_den;
220 guint update_interval;
227 GstClockTime next_ts;
228 GstClockTime prev_ts;
229 GstClockTime frame_duration;
233 guint64 data_bytecount;
234 guint64 acc_duration;
236 gboolean post_min_bitrate;
237 gboolean post_avg_bitrate;
238 gboolean post_max_bitrate;
243 GList *pending_events;
248 static GstElementClass *parent_class = NULL;
250 static void gst_base_parse_class_init (GstBaseParseClass * klass);
251 static void gst_base_parse_init (GstBaseParse * parse,
252 GstBaseParseClass * klass);
255 gst_base_parse_get_type (void)
257 static GType base_parse_type = 0;
259 if (!base_parse_type) {
260 static const GTypeInfo base_parse_info = {
261 sizeof (GstBaseParseClass),
262 (GBaseInitFunc) NULL,
263 (GBaseFinalizeFunc) NULL,
264 (GClassInitFunc) gst_base_parse_class_init,
267 sizeof (GstBaseParse),
269 (GInstanceInitFunc) gst_base_parse_init,
272 base_parse_type = g_type_register_static (GST_TYPE_ELEMENT,
273 "GstAudioBaseParseBad", &base_parse_info, G_TYPE_FLAG_ABSTRACT);
275 return base_parse_type;
278 static void gst_base_parse_finalize (GObject * object);
280 static gboolean gst_base_parse_sink_activate (GstPad * sinkpad);
281 static gboolean gst_base_parse_sink_activate_push (GstPad * pad,
283 static gboolean gst_base_parse_sink_activate_pull (GstPad * pad,
285 static gboolean gst_base_parse_handle_seek (GstBaseParse * parse,
287 static void gst_base_parse_handle_tag (GstBaseParse * parse, GstEvent * event);
289 static gboolean gst_base_parse_src_event (GstPad * pad, GstEvent * event);
290 static gboolean gst_base_parse_sink_event (GstPad * pad, GstEvent * event);
291 static gboolean gst_base_parse_query (GstPad * pad, GstQuery * query);
292 static gboolean gst_base_parse_sink_setcaps (GstPad * pad, GstCaps * caps);
293 static const GstQueryType *gst_base_parse_get_querytypes (GstPad * pad);
295 static GstFlowReturn gst_base_parse_chain (GstPad * pad, GstBuffer * buffer);
296 static void gst_base_parse_loop (GstPad * pad);
298 static gboolean gst_base_parse_check_frame (GstBaseParse * parse,
299 GstBuffer * buffer, guint * framesize, gint * skipsize);
301 static GstFlowReturn gst_base_parse_parse_frame (GstBaseParse * parse,
304 static gboolean gst_base_parse_sink_eventfunc (GstBaseParse * parse,
307 static gboolean gst_base_parse_src_eventfunc (GstBaseParse * parse,
310 static gboolean gst_base_parse_is_seekable (GstBaseParse * parse);
312 gboolean gst_base_parse_convert (GstBaseParse * parse, GstFormat src_format,
313 gint64 src_value, GstFormat dest_format, gint64 * dest_value);
315 static void gst_base_parse_drain (GstBaseParse * parse);
317 static void gst_base_parse_post_bitrates (GstBaseParse * parse,
318 gboolean post_min, gboolean post_avg, gboolean post_max);
321 gst_base_parse_finalize (GObject * object)
323 GstBaseParse *parse = GST_BASE_PARSE (object);
326 g_mutex_free (parse->parse_lock);
327 g_object_unref (parse->adapter);
329 if (parse->pending_segment) {
330 p_ev = &parse->pending_segment;
331 gst_event_replace (p_ev, NULL);
333 if (parse->close_segment) {
334 p_ev = &parse->close_segment;
335 gst_event_replace (p_ev, NULL);
338 if (parse->priv->cache) {
339 gst_buffer_unref (parse->priv->cache);
340 parse->priv->cache = NULL;
343 g_list_foreach (parse->priv->pending_events, (GFunc) gst_mini_object_unref,
345 g_list_free (parse->priv->pending_events);
346 parse->priv->pending_events = NULL;
348 G_OBJECT_CLASS (parent_class)->finalize (object);
352 gst_base_parse_class_init (GstBaseParseClass * klass)
354 GObjectClass *gobject_class;
356 gobject_class = G_OBJECT_CLASS (klass);
357 g_type_class_add_private (klass, sizeof (GstBaseParsePrivate));
358 parent_class = g_type_class_peek_parent (klass);
359 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_parse_finalize);
361 /* Default handlers */
362 klass->check_valid_frame = gst_base_parse_check_frame;
363 klass->parse_frame = gst_base_parse_parse_frame;
364 klass->event = gst_base_parse_sink_eventfunc;
365 klass->src_event = gst_base_parse_src_eventfunc;
366 klass->is_seekable = gst_base_parse_is_seekable;
367 klass->convert = gst_base_parse_convert;
369 GST_DEBUG_CATEGORY_INIT (gst_base_parse_debug, "baseparse", 0,
370 "baseparse element");
374 gst_base_parse_init (GstBaseParse * parse, GstBaseParseClass * bclass)
376 GstPadTemplate *pad_template;
378 GST_DEBUG_OBJECT (parse, "gst_base_parse_init");
380 parse->priv = GST_BASE_PARSE_GET_PRIVATE (parse);
383 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "sink");
384 g_return_if_fail (pad_template != NULL);
385 parse->sinkpad = gst_pad_new_from_template (pad_template, "sink");
386 gst_pad_set_event_function (parse->sinkpad,
387 GST_DEBUG_FUNCPTR (gst_base_parse_sink_event));
388 gst_pad_set_setcaps_function (parse->sinkpad,
389 GST_DEBUG_FUNCPTR (gst_base_parse_sink_setcaps));
390 gst_pad_set_chain_function (parse->sinkpad,
391 GST_DEBUG_FUNCPTR (gst_base_parse_chain));
392 gst_pad_set_activate_function (parse->sinkpad,
393 GST_DEBUG_FUNCPTR (gst_base_parse_sink_activate));
394 gst_pad_set_activatepush_function (parse->sinkpad,
395 GST_DEBUG_FUNCPTR (gst_base_parse_sink_activate_push));
396 gst_pad_set_activatepull_function (parse->sinkpad,
397 GST_DEBUG_FUNCPTR (gst_base_parse_sink_activate_pull));
398 gst_element_add_pad (GST_ELEMENT (parse), parse->sinkpad);
400 GST_DEBUG_OBJECT (parse, "sinkpad created");
403 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
404 g_return_if_fail (pad_template != NULL);
405 parse->srcpad = gst_pad_new_from_template (pad_template, "src");
406 gst_pad_set_event_function (parse->srcpad,
407 GST_DEBUG_FUNCPTR (gst_base_parse_src_event));
408 gst_pad_set_query_type_function (parse->srcpad,
409 GST_DEBUG_FUNCPTR (gst_base_parse_get_querytypes));
410 gst_pad_set_query_function (parse->srcpad,
411 GST_DEBUG_FUNCPTR (gst_base_parse_query));
412 gst_pad_use_fixed_caps (parse->srcpad);
413 gst_element_add_pad (GST_ELEMENT (parse), parse->srcpad);
414 GST_DEBUG_OBJECT (parse, "src created");
416 parse->parse_lock = g_mutex_new ();
417 parse->adapter = gst_adapter_new ();
418 parse->pending_segment = NULL;
419 parse->close_segment = NULL;
421 parse->priv->pad_mode = GST_ACTIVATE_NONE;
422 parse->priv->duration = -1;
423 parse->priv->min_frame_size = 1;
424 parse->priv->passthrough = FALSE;
425 parse->priv->discont = FALSE;
426 parse->priv->flushing = FALSE;
427 parse->priv->offset = 0;
428 GST_DEBUG_OBJECT (parse, "init ok");
434 * gst_base_parse_check_frame:
435 * @parse: #GstBaseParse.
436 * @buffer: GstBuffer.
437 * @framesize: This will be set to tell the found frame size in bytes.
438 * @skipsize: Output parameter that tells how much data needs to be skipped
439 * in order to find the following frame header.
441 * Default callback for check_valid_frame.
443 * Returns: Always TRUE.
446 gst_base_parse_check_frame (GstBaseParse * parse,
447 GstBuffer * buffer, guint * framesize, gint * skipsize)
449 *framesize = GST_BUFFER_SIZE (buffer);
456 * gst_base_parse_parse_frame:
457 * @parse: #GstBaseParse.
458 * @buffer: #GstBuffer.
460 * Default callback for parse_frame.
463 gst_base_parse_parse_frame (GstBaseParse * parse, GstBuffer * buffer)
465 if (!GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
466 GST_CLOCK_TIME_IS_VALID (parse->priv->next_ts)) {
467 GST_BUFFER_TIMESTAMP (buffer) = parse->priv->next_ts;
469 if (!GST_BUFFER_DURATION_IS_VALID (buffer) &&
470 GST_CLOCK_TIME_IS_VALID (parse->priv->frame_duration)) {
471 GST_BUFFER_DURATION (buffer) = parse->priv->frame_duration;
478 * gst_base_parse_bytepos_to_time:
479 * @parse: #GstBaseParse.
480 * @bytepos: Position (in bytes) to be converted.
481 * @pos_in_time: #GstClockTime pointer where the result is set.
483 * Convert given byte position into #GstClockTime format.
485 * Returns: TRUE if conversion succeeded.
488 gst_base_parse_bytepos_to_time (GstBaseParse * parse, gint64 bytepos,
489 GstClockTime * pos_in_time)
491 GstBaseParseClass *klass;
492 gboolean res = FALSE;
494 klass = GST_BASE_PARSE_GET_CLASS (parse);
496 if (klass->convert) {
497 res = klass->convert (parse, GST_FORMAT_BYTES, bytepos,
498 GST_FORMAT_TIME, (gint64 *) pos_in_time);
505 * gst_base_parse_sink_event:
506 * @pad: #GstPad that received the event.
507 * @event: #GstEvent to be handled.
509 * Handler for sink pad events.
511 * Returns: TRUE if the event was handled.
514 gst_base_parse_sink_event (GstPad * pad, GstEvent * event)
517 GstBaseParseClass *bclass;
518 gboolean handled = FALSE;
522 parse = GST_BASE_PARSE (gst_pad_get_parent (pad));
523 bclass = GST_BASE_PARSE_GET_CLASS (parse);
525 GST_DEBUG_OBJECT (parse, "handling event %d", GST_EVENT_TYPE (event));
527 /* Cache all events except EOS, NEWSEGMENT and FLUSH_STOP if we have a
529 if (parse->pending_segment && GST_EVENT_TYPE (event) != GST_EVENT_EOS
530 && GST_EVENT_TYPE (event) != GST_EVENT_NEWSEGMENT
531 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_START
532 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
534 if (GST_EVENT_TYPE (event) == GST_EVENT_TAG)
535 /* See if any bitrate tags were posted */
536 gst_base_parse_handle_tag (parse, event);
538 parse->priv->pending_events =
539 g_list_append (parse->priv->pending_events, event);
543 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS &&
544 parse->priv->framecount < MIN_FRAMES_TO_POST_BITRATE)
545 /* We've not posted bitrate tags yet - do so now */
546 gst_base_parse_post_bitrates (parse, TRUE, TRUE, TRUE);
549 handled = bclass->event (parse, event);
552 ret = gst_pad_event_default (pad, event);
555 gst_object_unref (parse);
556 GST_DEBUG_OBJECT (parse, "event handled");
562 * gst_base_parse_sink_eventfunc:
563 * @parse: #GstBaseParse.
564 * @event: #GstEvent to be handled.
566 * Element-level event handler function.
568 * Returns: TRUE if the event was handled and not need forwarding.
571 gst_base_parse_sink_eventfunc (GstBaseParse * parse, GstEvent * event)
573 gboolean handled = FALSE;
576 switch (GST_EVENT_TYPE (event)) {
577 case GST_EVENT_NEWSEGMENT:
579 gdouble rate, applied_rate;
581 gint64 start, stop, pos, offset = 0;
584 gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
585 &format, &start, &stop, &pos);
588 if (format == GST_FORMAT_BYTES) {
589 GstClockTime seg_start, seg_stop, seg_pos;
591 /* stop time is allowed to be open-ended, but not start & pos */
592 seg_stop = GST_CLOCK_TIME_NONE;
595 if (gst_base_parse_bytepos_to_time (parse, start, &seg_start) &&
596 gst_base_parse_bytepos_to_time (parse, pos, &seg_pos)) {
597 gst_event_unref (event);
598 event = gst_event_new_new_segment_full (update, rate, applied_rate,
599 GST_FORMAT_TIME, seg_start, seg_stop, seg_pos);
600 format = GST_FORMAT_TIME;
601 GST_DEBUG_OBJECT (parse, "Converted incoming segment to TIME. "
602 "start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT
603 ", pos = %" GST_TIME_FORMAT, GST_TIME_ARGS (seg_start),
604 GST_TIME_ARGS (seg_stop), GST_TIME_ARGS (seg_pos));
608 if (format != GST_FORMAT_TIME) {
609 /* Unknown incoming segment format. Output a default open-ended
611 gst_event_unref (event);
612 event = gst_event_new_new_segment_full (update, rate, applied_rate,
613 GST_FORMAT_TIME, 0, GST_CLOCK_TIME_NONE, 0);
616 gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
617 &format, &start, &stop, &pos);
619 gst_segment_set_newsegment_full (&parse->segment, update, rate,
620 applied_rate, format, start, stop, pos);
622 GST_DEBUG_OBJECT (parse, "Created newseg rate %g, applied rate %g, "
623 "format %d, start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT
624 ", pos = %" GST_TIME_FORMAT, rate, applied_rate, format,
625 GST_TIME_ARGS (start), GST_TIME_ARGS (stop), GST_TIME_ARGS (pos));
627 /* save the segment for later, right before we push a new buffer so that
628 * the caps are fixed and the next linked element can receive
630 eventp = &parse->pending_segment;
631 gst_event_replace (eventp, event);
632 gst_event_unref (event);
635 /* but finish the current segment */
636 GST_DEBUG_OBJECT (parse, "draining current segment");
637 gst_base_parse_drain (parse);
638 gst_adapter_clear (parse->adapter);
639 parse->priv->offset = offset;
640 parse->priv->next_ts = start;
644 case GST_EVENT_FLUSH_START:
645 parse->priv->flushing = TRUE;
646 handled = gst_pad_push_event (parse->srcpad, event);
647 /* Wait for _chain() to exit by taking the srcpad STREAM_LOCK */
648 GST_PAD_STREAM_LOCK (parse->srcpad);
649 GST_PAD_STREAM_UNLOCK (parse->srcpad);
653 case GST_EVENT_FLUSH_STOP:
654 gst_adapter_clear (parse->adapter);
655 parse->priv->flushing = FALSE;
656 parse->priv->discont = TRUE;
660 gst_base_parse_drain (parse);
672 * gst_base_parse_src_event:
673 * @pad: #GstPad that received the event.
674 * @event: #GstEvent that was received.
676 * Handler for source pad events.
678 * Returns: TRUE if the event was handled.
681 gst_base_parse_src_event (GstPad * pad, GstEvent * event)
684 GstBaseParseClass *bclass;
685 gboolean handled = FALSE;
688 parse = GST_BASE_PARSE (gst_pad_get_parent (pad));
689 bclass = GST_BASE_PARSE_GET_CLASS (parse);
691 GST_DEBUG_OBJECT (parse, "event %d, %s", GST_EVENT_TYPE (event),
692 GST_EVENT_TYPE_NAME (event));
694 if (bclass->src_event)
695 handled = bclass->src_event (parse, event);
698 ret = gst_pad_event_default (pad, event);
700 gst_event_unref (event);
702 gst_object_unref (parse);
708 * gst_base_parse_src_eventfunc:
709 * @parse: #GstBaseParse.
710 * @event: #GstEvent that was received.
712 * Default srcpad event handler.
714 * Returns: TRUE if the event was handled and can be dropped.
717 gst_base_parse_src_eventfunc (GstBaseParse * parse, GstEvent * event)
719 gboolean handled = FALSE;
720 GstBaseParseClass *bclass;
722 bclass = GST_BASE_PARSE_GET_CLASS (parse);
724 switch (GST_EVENT_TYPE (event)) {
727 if (bclass->is_seekable (parse)) {
728 handled = gst_base_parse_handle_seek (parse, event);
740 * gst_base_parse_is_seekable:
741 * @parse: #GstBaseParse.
743 * Default handler for is_seekable.
745 * Returns: Always TRUE.
748 gst_base_parse_is_seekable (GstBaseParse * parse)
754 * gst_base_parse_convert:
755 * @parse: #GstBaseParse.
756 * @src_format: #GstFormat describing the source format.
757 * @src_value: Source value to be converted.
758 * @dest_format: #GstFormat defining the converted format.
759 * @dest_value: Pointer where the conversion result will be put.
761 * Implementation of "convert" vmethod in #GstBaseParse class.
763 * Returns: TRUE if conversion was successful.
766 gst_base_parse_convert (GstBaseParse * parse,
767 GstFormat src_format,
768 gint64 src_value, GstFormat dest_format, gint64 * dest_value)
770 gboolean ret = FALSE;
771 guint64 bytes, duration;
773 if (G_UNLIKELY (src_format == dest_format)) {
774 *dest_value = src_value;
778 if (G_UNLIKELY (src_value == -1)) {
783 /* need at least some frames */
784 if (!parse->priv->framecount)
787 /* either frame info (having num means den also ok) or use average bitrate */
788 if (parse->priv->fps_num) {
789 duration = parse->priv->framecount * parse->priv->fps_den * 1000;
790 bytes = parse->priv->bytecount * parse->priv->fps_num;
792 duration = parse->priv->acc_duration / GST_MSECOND;
793 bytes = parse->priv->bytecount;
796 if (G_UNLIKELY (!duration || !bytes))
799 if (src_format == GST_FORMAT_BYTES) {
800 if (dest_format == GST_FORMAT_TIME) {
801 /* BYTES -> TIME conversion */
802 GST_DEBUG_OBJECT (parse, "converting bytes -> time");
804 *dest_value = gst_util_uint64_scale (src_value, duration, bytes);
805 *dest_value *= GST_MSECOND;
806 GST_DEBUG_OBJECT (parse, "conversion result: %" G_GINT64_FORMAT " ms",
807 *dest_value / GST_MSECOND);
810 } else if (src_format == GST_FORMAT_TIME) {
811 GST_DEBUG_OBJECT (parse, "converting time -> bytes");
812 if (dest_format == GST_FORMAT_BYTES) {
813 *dest_value = gst_util_uint64_scale (src_value / GST_MSECOND, bytes,
815 GST_DEBUG_OBJECT (parse,
816 "time %" G_GINT64_FORMAT " ms in bytes = %" G_GINT64_FORMAT,
817 src_value / GST_MSECOND, *dest_value);
820 } else if (src_format == GST_FORMAT_DEFAULT) {
821 /* DEFAULT == frame-based */
822 if (dest_format == GST_FORMAT_TIME) {
823 if (parse->priv->fps_den) {
824 *dest_value = gst_util_uint64_scale (src_value,
825 GST_SECOND * parse->priv->fps_den, parse->priv->fps_num);
828 } else if (dest_format == GST_FORMAT_BYTES) {
836 * gst_base_parse_update_duration:
837 * @parse: #GstBaseParse.
841 gst_base_parse_update_duration (GstBaseParse * aacparse)
845 GstBaseParseClass *klass;
847 parse = GST_BASE_PARSE (aacparse);
848 klass = GST_BASE_PARSE_GET_CLASS (parse);
850 /* must be able to convert */
854 peer = gst_pad_get_peer (parse->sinkpad);
856 GstFormat pformat = GST_FORMAT_BYTES;
857 gboolean qres = FALSE;
858 gint64 ptot, dest_value;
860 qres = gst_pad_query_duration (peer, &pformat, &ptot);
861 gst_object_unref (GST_OBJECT (peer));
863 if (klass->convert (parse, pformat, ptot, GST_FORMAT_TIME, &dest_value))
864 parse->priv->estimated_duration = dest_value;
870 gst_base_parse_post_bitrates (GstBaseParse * parse, gboolean post_min,
871 gboolean post_avg, gboolean post_max)
873 GstTagList *taglist = gst_tag_list_new ();
875 if (post_min && parse->priv->post_min_bitrate)
876 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE,
877 GST_TAG_MINIMUM_BITRATE, parse->priv->min_bitrate, NULL);
879 if (post_avg && parse->priv->post_min_bitrate)
880 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_BITRATE,
881 parse->priv->avg_bitrate, NULL);
883 if (post_max && parse->priv->post_max_bitrate)
884 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE,
885 GST_TAG_MAXIMUM_BITRATE, parse->priv->max_bitrate, NULL);
887 GST_DEBUG_OBJECT (parse, "Updated bitrates. Min: %u, Avg: %u, Max: %u",
888 parse->priv->min_bitrate, parse->priv->avg_bitrate,
889 parse->priv->max_bitrate);
891 gst_element_found_tags_for_pad (GST_ELEMENT (parse), parse->srcpad, taglist);
895 * gst_base_parse_update_bitrates:
896 * @parse: #GstBaseParse.
897 * @buffer: Current frame as a #GstBuffer
899 * Keeps track of the minimum and maximum bitrates, and also maintains a
900 * running average bitrate of the stream so far.
903 gst_base_parse_update_bitrates (GstBaseParse * parse, GstBuffer * buffer)
905 /* Only update the tag on a 10 kbps delta */
906 static const gint update_threshold = 10000;
908 GstBaseParseClass *klass;
909 guint64 data_len, frame_dur;
910 gint overhead = 0, frame_bitrate, old_avg_bitrate = parse->priv->avg_bitrate;
911 gboolean update_min = FALSE, update_avg = FALSE, update_max = FALSE;
913 klass = GST_BASE_PARSE_GET_CLASS (parse);
915 if (klass->get_frame_overhead) {
916 overhead = klass->get_frame_overhead (parse, buffer);
921 data_len = GST_BUFFER_SIZE (buffer) - overhead;
922 parse->priv->data_bytecount += data_len;
924 if (parse->priv->fps_num) {
925 /* Calculate duration of a frame from frame properties */
926 frame_dur = (GST_SECOND * parse->priv->fps_den) / parse->priv->fps_num;
927 parse->priv->avg_bitrate = (8 * parse->priv->data_bytecount * GST_SECOND) /
928 (parse->priv->framecount * frame_dur);
930 } else if (GST_BUFFER_DURATION_IS_VALID (buffer)) {
931 /* Calculate duration of a frame from buffer properties */
932 frame_dur = GST_BUFFER_DURATION (buffer);
933 parse->priv->avg_bitrate = (8 * parse->priv->data_bytecount * GST_SECOND) /
934 parse->priv->acc_duration;
937 /* No way to figure out frame duration (is this even possible?) */
941 frame_bitrate = (8 * data_len * GST_SECOND) / frame_dur;
943 if (frame_bitrate < parse->priv->min_bitrate) {
944 parse->priv->min_bitrate = frame_bitrate;
948 if (frame_bitrate > parse->priv->max_bitrate) {
949 parse->priv->max_bitrate = frame_bitrate;
953 if (old_avg_bitrate / update_threshold !=
954 parse->priv->avg_bitrate / update_threshold)
957 if (parse->priv->framecount >= MIN_FRAMES_TO_POST_BITRATE &&
958 (update_min || update_avg || update_max))
959 gst_base_parse_post_bitrates (parse, update_min, update_avg, update_max);
963 * gst_base_parse_handle_and_push_buffer:
964 * @parse: #GstBaseParse.
965 * @klass: #GstBaseParseClass.
966 * @buffer: #GstBuffer.
968 * Parses the frame from given buffer and pushes it forward. Also performs
969 * timestamp handling and checks the segment limits.
971 * This is called with srcpad STREAM_LOCK held.
973 * Returns: #GstFlowReturn
976 gst_base_parse_handle_and_push_buffer (GstBaseParse * parse,
977 GstBaseParseClass * klass, GstBuffer * buffer)
981 if (parse->priv->discont) {
982 GST_DEBUG_OBJECT (parse, "marking DISCONT");
983 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
984 parse->priv->discont = FALSE;
987 GST_LOG_OBJECT (parse,
988 "parsing frame at offset %" G_GUINT64_FORMAT
989 " (%#" G_GINT64_MODIFIER "x) of size %d",
990 GST_BUFFER_OFFSET (buffer), GST_BUFFER_OFFSET (buffer),
991 GST_BUFFER_SIZE (buffer));
993 ret = klass->parse_frame (parse, buffer);
995 /* re-use default handler to add missing metadata as-much-as-possible */
996 gst_base_parse_parse_frame (parse, buffer);
997 if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
998 GST_BUFFER_DURATION_IS_VALID (buffer)) {
999 parse->priv->next_ts =
1000 GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer);
1002 /* we lost track, do not produce bogus time next time around
1003 * (probably means parser subclass has given up on parsing as well) */
1004 GST_DEBUG_OBJECT (parse, "no next fallback timestamp");
1005 parse->priv->next_ts = GST_CLOCK_TIME_NONE;
1008 /* First buffers are dropped, this means that the subclass needs more
1009 * frames to decide on the format and queues them internally */
1010 /* convert internal flow to OK and mark discont for the next buffer. */
1011 if (ret == GST_BASE_PARSE_FLOW_DROPPED) {
1012 gst_buffer_unref (buffer);
1014 } else if (ret != GST_FLOW_OK) {
1018 return gst_base_parse_push_buffer (parse, buffer);
1022 * gst_base_parse_push_buffer:
1023 * @parse: #GstBaseParse.
1024 * @buffer: #GstBuffer.
1026 * Pushes the buffer downstream, sends any pending events and
1027 * does some timestamp and segment handling.
1029 * This must be called with srcpad STREAM_LOCK held.
1031 * Returns: #GstFlowReturn
1034 gst_base_parse_push_buffer (GstBaseParse * parse, GstBuffer * buffer)
1036 GstFlowReturn ret = GST_FLOW_OK;
1037 GstClockTime last_start = GST_CLOCK_TIME_NONE;
1038 GstClockTime last_stop = GST_CLOCK_TIME_NONE;
1040 GST_LOG_OBJECT (parse,
1041 "processing buffer of size %d with ts %" GST_TIME_FORMAT
1042 ", duration %" GST_TIME_FORMAT, GST_BUFFER_SIZE (buffer),
1043 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
1044 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
1047 parse->priv->bytecount += GST_BUFFER_SIZE (buffer);
1048 if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BASE_PARSE_BUFFER_FLAG_NO_FRAME)) {
1049 parse->priv->framecount++;
1050 if (GST_BUFFER_DURATION_IS_VALID (buffer)) {
1051 parse->priv->acc_duration += GST_BUFFER_DURATION (buffer);
1054 GST_BUFFER_FLAG_UNSET (buffer, GST_BASE_PARSE_BUFFER_FLAG_NO_FRAME);
1055 if (parse->priv->update_interval &&
1056 (parse->priv->framecount % parse->priv->update_interval) == 0)
1057 gst_base_parse_update_duration (parse);
1059 gst_base_parse_update_bitrates (parse, buffer);
1061 if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer))
1062 last_start = last_stop = GST_BUFFER_TIMESTAMP (buffer);
1063 if (last_start != GST_CLOCK_TIME_NONE
1064 && GST_BUFFER_DURATION_IS_VALID (buffer))
1065 last_stop = last_start + GST_BUFFER_DURATION (buffer);
1067 /* should have caps by now */
1068 g_return_val_if_fail (GST_PAD_CAPS (parse->srcpad), GST_FLOW_ERROR);
1070 gst_buffer_set_caps (buffer, GST_PAD_CAPS (parse->srcpad));
1072 /* segment times are typically estimates,
1073 * actual frame data might lead subclass to different timestamps,
1074 * so override segment start from what is supplied there */
1075 if (G_UNLIKELY (parse->pending_segment && !parse->priv->passthrough &&
1076 GST_CLOCK_TIME_IS_VALID (last_start))) {
1077 gst_event_unref (parse->pending_segment);
1078 /* stop time possibly lost this way,
1079 * but unlikely and not really supported */
1080 parse->pending_segment =
1081 gst_event_new_new_segment (FALSE, parse->segment.rate,
1082 parse->segment.format, last_start, -1, last_start);
1085 /* and should then also be linked downstream, so safe to send some events */
1086 if (parse->priv->pad_mode == GST_ACTIVATE_PULL) {
1087 if (G_UNLIKELY (parse->close_segment)) {
1088 GST_DEBUG_OBJECT (parse, "loop sending close segment");
1089 gst_pad_push_event (parse->srcpad, parse->close_segment);
1090 parse->close_segment = NULL;
1093 if (G_UNLIKELY (parse->pending_segment)) {
1094 GST_DEBUG_OBJECT (parse, "loop push pending segment");
1095 gst_pad_push_event (parse->srcpad, parse->pending_segment);
1096 parse->pending_segment = NULL;
1099 if (G_UNLIKELY (parse->pending_segment)) {
1100 GST_DEBUG_OBJECT (parse, "chain pushing a pending segment");
1101 gst_pad_push_event (parse->srcpad, parse->pending_segment);
1102 parse->pending_segment = NULL;
1106 if (G_UNLIKELY (parse->priv->pending_events)) {
1109 for (l = parse->priv->pending_events; l != NULL; l = l->next) {
1110 gst_pad_push_event (parse->srcpad, GST_EVENT (l->data));
1112 g_list_free (parse->priv->pending_events);
1113 parse->priv->pending_events = NULL;
1116 /* TODO: Add to seek table */
1118 if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
1119 GST_CLOCK_TIME_IS_VALID (parse->segment.stop) &&
1120 GST_BUFFER_TIMESTAMP (buffer) > parse->segment.stop) {
1121 GST_LOG_OBJECT (parse, "Dropped frame, after segment");
1122 gst_buffer_unref (buffer);
1123 } else if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
1124 GST_BUFFER_DURATION_IS_VALID (buffer) &&
1125 GST_CLOCK_TIME_IS_VALID (parse->segment.start) &&
1126 GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer)
1127 < parse->segment.start) {
1128 /* FIXME: subclass needs way to override the start as downstream might
1129 * need frames before for proper decoding */
1130 GST_LOG_OBJECT (parse, "Dropped frame, before segment");
1131 gst_buffer_unref (buffer);
1133 ret = gst_pad_push (parse->srcpad, buffer);
1134 GST_LOG_OBJECT (parse, "frame (%d bytes) pushed: %d",
1135 GST_BUFFER_SIZE (buffer), ret);
1138 /* Update current running segment position */
1139 if (ret == GST_FLOW_OK && last_stop != GST_CLOCK_TIME_NONE)
1140 gst_segment_set_last_stop (&parse->segment, GST_FORMAT_TIME, last_stop);
1147 * gst_base_parse_drain:
1148 * @parse: #GstBaseParse.
1150 * Drains the adapter until it is empty. It decreases the min_frame_size to
1151 * match the current adapter size and calls chain method until the adapter
1152 * is emptied or chain returns with error.
1155 gst_base_parse_drain (GstBaseParse * parse)
1159 GST_DEBUG_OBJECT (parse, "draining");
1160 parse->priv->drain = TRUE;
1163 avail = gst_adapter_available (parse->adapter);
1167 if (gst_base_parse_chain (parse->sinkpad, NULL) != GST_FLOW_OK) {
1171 /* nothing changed, maybe due to truncated frame; break infinite loop */
1172 if (avail == gst_adapter_available (parse->adapter)) {
1173 GST_DEBUG_OBJECT (parse, "no change during draining; flushing");
1174 gst_adapter_clear (parse->adapter);
1178 parse->priv->drain = FALSE;
1183 * gst_base_parse_chain:
1185 * @buffer: #GstBuffer.
1187 * Returns: #GstFlowReturn.
1189 static GstFlowReturn
1190 gst_base_parse_chain (GstPad * pad, GstBuffer * buffer)
1192 GstBaseParseClass *bclass;
1193 GstBaseParse *parse;
1194 GstFlowReturn ret = GST_FLOW_OK;
1195 GstBuffer *outbuf = NULL;
1196 GstBuffer *tmpbuf = NULL;
1201 GstClockTime timestamp;
1203 parse = GST_BASE_PARSE (GST_OBJECT_PARENT (pad));
1204 bclass = GST_BASE_PARSE_GET_CLASS (parse);
1206 if (G_LIKELY (buffer)) {
1207 GST_LOG_OBJECT (parse, "buffer size: %d, offset = %" G_GINT64_FORMAT,
1208 GST_BUFFER_SIZE (buffer), GST_BUFFER_OFFSET (buffer));
1209 if (G_UNLIKELY (parse->priv->passthrough)) {
1210 buffer = gst_buffer_make_metadata_writable (buffer);
1211 return gst_base_parse_push_buffer (parse, buffer);
1213 gst_adapter_push (parse->adapter, buffer);
1216 /* Parse and push as many frames as possible */
1217 /* Stop either when adapter is empty or we are flushing */
1218 while (!parse->priv->flushing) {
1219 tmpbuf = gst_buffer_new ();
1221 /* Synchronization loop */
1223 GST_BASE_PARSE_LOCK (parse);
1224 min_size = parse->priv->min_frame_size;
1225 GST_BASE_PARSE_UNLOCK (parse);
1227 if (G_UNLIKELY (parse->priv->drain)) {
1228 min_size = gst_adapter_available (parse->adapter);
1229 GST_DEBUG_OBJECT (parse, "draining, data left: %d", min_size);
1230 if (G_UNLIKELY (!min_size)) {
1231 gst_buffer_unref (tmpbuf);
1236 /* Collect at least min_frame_size bytes */
1237 if (gst_adapter_available (parse->adapter) < min_size) {
1238 GST_DEBUG_OBJECT (parse, "not enough data available (only %d bytes)",
1239 gst_adapter_available (parse->adapter));
1240 gst_buffer_unref (tmpbuf);
1244 data = gst_adapter_peek (parse->adapter, min_size);
1245 GST_BUFFER_DATA (tmpbuf) = (guint8 *) data;
1246 GST_BUFFER_SIZE (tmpbuf) = min_size;
1247 GST_BUFFER_OFFSET (tmpbuf) = parse->priv->offset;
1248 GST_BUFFER_FLAG_SET (tmpbuf, GST_MINI_OBJECT_FLAG_READONLY);
1250 if (parse->priv->discont) {
1251 GST_DEBUG_OBJECT (parse, "marking DISCONT");
1252 GST_BUFFER_FLAG_SET (tmpbuf, GST_BUFFER_FLAG_DISCONT);
1256 if (bclass->check_valid_frame (parse, tmpbuf, &fsize, &skip)) {
1257 if (gst_adapter_available (parse->adapter) < fsize) {
1258 GST_DEBUG_OBJECT (parse,
1259 "found valid frame but not enough data available (only %d bytes)",
1260 gst_adapter_available (parse->adapter));
1261 gst_buffer_unref (tmpbuf);
1267 GST_LOG_OBJECT (parse, "finding sync, skipping %d bytes", skip);
1268 gst_adapter_flush (parse->adapter, skip);
1269 parse->priv->offset += skip;
1270 parse->priv->discont = TRUE;
1271 } else if (skip == -1) {
1272 /* subclass didn't touch this value. By default we skip 1 byte */
1273 GST_LOG_OBJECT (parse, "finding sync, skipping 1 byte");
1274 gst_adapter_flush (parse->adapter, 1);
1275 parse->priv->offset++;
1276 parse->priv->discont = TRUE;
1278 /* There is a possibility that subclass set the skip value to zero.
1279 This means that it has probably found a frame but wants to ask
1280 more data (by increasing the min_size) to be sure of this. */
1282 gst_buffer_unref (tmpbuf);
1286 /* Subclass found the sync, but still wants to skip some data */
1287 GST_LOG_OBJECT (parse, "skipping %d bytes", skip);
1288 gst_adapter_flush (parse->adapter, skip);
1289 parse->priv->offset += skip;
1292 /* Grab lock to prevent a race with FLUSH_START handler */
1293 GST_PAD_STREAM_LOCK (parse->srcpad);
1295 /* FLUSH_START event causes the "flushing" flag to be set. In this
1296 * case we can leave the frame pushing loop */
1297 if (parse->priv->flushing) {
1298 GST_PAD_STREAM_UNLOCK (parse->srcpad);
1302 /* FIXME: Would it be more efficient to make a subbuffer instead? */
1303 outbuf = gst_adapter_take_buffer (parse->adapter, fsize);
1304 outbuf = gst_buffer_make_metadata_writable (outbuf);
1306 /* Subclass may want to know the data offset */
1307 GST_BUFFER_OFFSET (outbuf) = parse->priv->offset;
1308 parse->priv->offset += fsize;
1310 /* move along with upstream timestamp (if any),
1311 * but interpolate in between */
1312 timestamp = gst_adapter_prev_timestamp (parse->adapter, NULL);
1313 if (GST_CLOCK_TIME_IS_VALID (timestamp) &&
1314 (parse->priv->prev_ts != timestamp)) {
1315 parse->priv->prev_ts = parse->priv->next_ts = timestamp;
1318 ret = gst_base_parse_handle_and_push_buffer (parse, bclass, outbuf);
1319 GST_PAD_STREAM_UNLOCK (parse->srcpad);
1321 if (ret != GST_FLOW_OK) {
1322 GST_LOG_OBJECT (parse, "push returned %d", ret);
1328 GST_LOG_OBJECT (parse, "chain leaving");
1332 /* pull @size bytes at current offset,
1333 * i.e. at least try to and possibly return a shorter buffer if near the end */
1334 static GstFlowReturn
1335 gst_base_parse_pull_range (GstBaseParse * parse, guint size,
1336 GstBuffer ** buffer)
1338 GstFlowReturn ret = GST_FLOW_OK;
1340 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
1342 /* Caching here actually makes much less difference than one would expect.
1343 * We do it mainly to avoid pulling buffers of 1 byte all the time */
1344 if (parse->priv->cache) {
1345 gint64 cache_offset = GST_BUFFER_OFFSET (parse->priv->cache);
1346 gint cache_size = GST_BUFFER_SIZE (parse->priv->cache);
1348 if (cache_offset <= parse->priv->offset &&
1349 (parse->priv->offset + size) <= (cache_offset + cache_size)) {
1350 *buffer = gst_buffer_create_sub (parse->priv->cache,
1351 parse->priv->offset - cache_offset, size);
1352 GST_BUFFER_OFFSET (*buffer) = parse->priv->offset;
1355 /* not enough data in the cache, free cache and get a new one */
1356 gst_buffer_unref (parse->priv->cache);
1357 parse->priv->cache = NULL;
1360 /* refill the cache */
1362 gst_pad_pull_range (parse->sinkpad, parse->priv->offset, MAX (size,
1363 64 * 1024), &parse->priv->cache);
1364 if (ret != GST_FLOW_OK) {
1365 parse->priv->cache = NULL;
1369 if (GST_BUFFER_SIZE (parse->priv->cache) >= size) {
1370 *buffer = gst_buffer_create_sub (parse->priv->cache, 0, size);
1371 GST_BUFFER_OFFSET (*buffer) = parse->priv->offset;
1375 /* Not possible to get enough data, try a last time with
1376 * requesting exactly the size we need */
1377 gst_buffer_unref (parse->priv->cache);
1378 parse->priv->cache = NULL;
1380 ret = gst_pad_pull_range (parse->sinkpad, parse->priv->offset, size,
1381 &parse->priv->cache);
1383 if (ret != GST_FLOW_OK) {
1384 GST_DEBUG_OBJECT (parse, "pull_range returned %d", ret);
1389 if (GST_BUFFER_SIZE (parse->priv->cache) < size) {
1390 GST_DEBUG_OBJECT (parse, "Returning short buffer at offset %"
1391 G_GUINT64_FORMAT ": wanted %u bytes, got %u bytes", parse->priv->offset,
1392 size, GST_BUFFER_SIZE (parse->priv->cache));
1394 *buffer = parse->priv->cache;
1395 parse->priv->cache = NULL;
1400 *buffer = gst_buffer_create_sub (parse->priv->cache, 0, size);
1401 GST_BUFFER_OFFSET (*buffer) = parse->priv->offset;
1407 * gst_base_parse_loop:
1410 * Loop that is used in pull mode to retrieve data from upstream.
1413 gst_base_parse_loop (GstPad * pad)
1415 GstBaseParse *parse;
1416 GstBaseParseClass *klass;
1417 GstBuffer *buffer, *outbuf;
1418 gboolean ret = FALSE;
1419 guint fsize = 0, min_size;
1422 parse = GST_BASE_PARSE (gst_pad_get_parent (pad));
1423 klass = GST_BASE_PARSE_GET_CLASS (parse);
1425 /* TODO: Check if we reach segment stop limit */
1429 GST_BASE_PARSE_LOCK (parse);
1430 min_size = parse->priv->min_frame_size;
1431 GST_BASE_PARSE_UNLOCK (parse);
1433 ret = gst_base_parse_pull_range (parse, min_size, &buffer);
1435 if (ret == GST_FLOW_UNEXPECTED)
1437 else if (ret != GST_FLOW_OK)
1440 if (parse->priv->discont) {
1441 GST_DEBUG_OBJECT (parse, "marking DISCONT");
1442 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1445 /* if we got a short read, inform subclass we are draining leftover
1446 * and no more is to be expected */
1447 if (GST_BUFFER_SIZE (buffer) < min_size)
1448 parse->priv->drain = TRUE;
1451 if (klass->check_valid_frame (parse, buffer, &fsize, &skip)) {
1452 parse->priv->drain = FALSE;
1455 parse->priv->drain = FALSE;
1457 GST_LOG_OBJECT (parse, "finding sync, skipping %d bytes", skip);
1458 parse->priv->offset += skip;
1459 parse->priv->discont = TRUE;
1460 } else if (skip == -1) {
1461 GST_LOG_OBJECT (parse, "finding sync, skipping 1 byte");
1462 parse->priv->offset++;
1463 parse->priv->discont = TRUE;
1465 GST_DEBUG_OBJECT (parse, "finding sync...");
1466 gst_buffer_unref (buffer);
1469 if (fsize <= GST_BUFFER_SIZE (buffer)) {
1470 outbuf = gst_buffer_create_sub (buffer, 0, fsize);
1471 GST_BUFFER_OFFSET (outbuf) = GST_BUFFER_OFFSET (buffer);
1472 gst_buffer_unref (buffer);
1474 gst_buffer_unref (buffer);
1475 ret = gst_base_parse_pull_range (parse, fsize, &outbuf);
1477 if (ret == GST_FLOW_UNEXPECTED)
1479 else if (ret != GST_FLOW_OK)
1481 if (GST_BUFFER_SIZE (outbuf) < fsize)
1485 parse->priv->offset += fsize;
1487 /* Does the subclass want to skip too? */
1489 parse->priv->offset += skip;
1491 /* This always unrefs the outbuf, even if error occurs */
1492 ret = gst_base_parse_handle_and_push_buffer (parse, klass, outbuf);
1494 if (ret != GST_FLOW_OK) {
1495 GST_DEBUG_OBJECT (parse, "flow: %s", gst_flow_get_name (ret));
1496 if (ret == GST_FLOW_UNEXPECTED) {
1497 gst_pad_push_event (parse->srcpad, gst_event_new_eos ());
1498 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_UNEXPECTED) {
1499 GST_ELEMENT_ERROR (parse, STREAM, FAILED, (NULL),
1500 ("streaming task paused, reason: %s", gst_flow_get_name (ret)));
1501 gst_pad_push_event (parse->srcpad, gst_event_new_eos ());
1506 gst_object_unref (parse);
1511 GST_LOG_OBJECT (parse, "pausing task %d", ret);
1512 gst_pad_pause_task (pad);
1513 gst_object_unref (parse);
1518 GST_LOG_OBJECT (parse, "sending eos");
1519 gst_pad_push_event (parse->srcpad, gst_event_new_eos ());
1526 * gst_base_parse_sink_activate:
1527 * @sinkpad: #GstPad to be activated.
1529 * Returns: TRUE if activation succeeded.
1532 gst_base_parse_sink_activate (GstPad * sinkpad)
1534 GstBaseParse *parse;
1535 gboolean result = TRUE;
1537 parse = GST_BASE_PARSE (gst_pad_get_parent (sinkpad));
1539 GST_DEBUG_OBJECT (parse, "sink activate");
1541 if (gst_pad_check_pull_range (sinkpad)) {
1542 GST_DEBUG_OBJECT (parse, "trying to activate in pull mode");
1543 result = gst_pad_activate_pull (sinkpad, TRUE);
1545 GST_DEBUG_OBJECT (parse, "trying to activate in push mode");
1546 result = gst_pad_activate_push (sinkpad, TRUE);
1549 GST_DEBUG_OBJECT (parse, "sink activate return %d", result);
1550 gst_object_unref (parse);
1556 * gst_base_parse_activate:
1557 * @parse: #GstBaseParse.
1558 * @active: TRUE if element will be activated, FALSE if disactivated.
1560 * Returns: TRUE if the operation succeeded.
1563 gst_base_parse_activate (GstBaseParse * parse, gboolean active)
1565 GstBaseParseClass *klass;
1566 gboolean result = FALSE;
1568 GST_DEBUG_OBJECT (parse, "activate");
1570 klass = GST_BASE_PARSE_GET_CLASS (parse);
1573 if (parse->priv->pad_mode == GST_ACTIVATE_NONE && klass->start)
1574 result = klass->start (parse);
1576 GST_OBJECT_LOCK (parse);
1577 gst_segment_init (&parse->segment, GST_FORMAT_TIME);
1578 parse->priv->duration = -1;
1579 parse->priv->discont = TRUE;
1580 parse->priv->flushing = FALSE;
1581 parse->priv->offset = 0;
1582 parse->priv->update_interval = 0;
1583 parse->priv->fps_num = parse->priv->fps_den = 0;
1584 parse->priv->frame_duration = GST_CLOCK_TIME_NONE;
1585 parse->priv->framecount = 0;
1586 parse->priv->bytecount = 0;
1587 parse->priv->acc_duration = 0;
1588 parse->priv->estimated_duration = -1;
1589 parse->priv->next_ts = 0;
1590 parse->priv->passthrough = FALSE;
1591 parse->priv->post_min_bitrate = TRUE;
1592 parse->priv->post_avg_bitrate = TRUE;
1593 parse->priv->post_max_bitrate = TRUE;
1594 parse->priv->min_bitrate = G_MAXUINT;
1595 parse->priv->max_bitrate = 0;
1596 parse->priv->max_bitrate = 0;
1598 if (parse->pending_segment)
1599 gst_event_unref (parse->pending_segment);
1601 parse->pending_segment =
1602 gst_event_new_new_segment (FALSE, parse->segment.rate,
1603 parse->segment.format,
1604 parse->segment.start, parse->segment.stop, parse->segment.last_stop);
1606 GST_OBJECT_UNLOCK (parse);
1608 /* We must make sure streaming has finished before resetting things
1609 * and calling the ::stop vfunc */
1610 GST_PAD_STREAM_LOCK (parse->sinkpad);
1611 GST_PAD_STREAM_UNLOCK (parse->sinkpad);
1613 if (parse->priv->pad_mode != GST_ACTIVATE_NONE && klass->stop)
1614 result = klass->stop (parse);
1616 g_list_foreach (parse->priv->pending_events, (GFunc) gst_mini_object_unref,
1618 g_list_free (parse->priv->pending_events);
1619 parse->priv->pending_events = NULL;
1621 if (parse->priv->cache) {
1622 gst_buffer_unref (parse->priv->cache);
1623 parse->priv->cache = NULL;
1626 parse->priv->pad_mode = GST_ACTIVATE_NONE;
1628 GST_DEBUG_OBJECT (parse, "activate: %d", result);
1634 * gst_base_parse_sink_activate_push:
1635 * @pad: #GstPad to be (de)activated.
1636 * @active: TRUE when activating, FALSE when deactivating.
1638 * Returns: TRUE if (de)activation succeeded.
1641 gst_base_parse_sink_activate_push (GstPad * pad, gboolean active)
1643 gboolean result = TRUE;
1644 GstBaseParse *parse;
1646 parse = GST_BASE_PARSE (gst_pad_get_parent (pad));
1648 GST_DEBUG_OBJECT (parse, "sink activate push");
1650 result = gst_base_parse_activate (parse, active);
1653 parse->priv->pad_mode = active ? GST_ACTIVATE_PUSH : GST_ACTIVATE_NONE;
1655 GST_DEBUG_OBJECT (parse, "sink activate push: %d", result);
1657 gst_object_unref (parse);
1663 * gst_base_parse_sink_activate_pull:
1664 * @sinkpad: #GstPad to be (de)activated.
1665 * @active: TRUE when activating, FALSE when deactivating.
1667 * Returns: TRUE if (de)activation succeeded.
1670 gst_base_parse_sink_activate_pull (GstPad * sinkpad, gboolean active)
1672 gboolean result = FALSE;
1673 GstBaseParse *parse;
1675 parse = GST_BASE_PARSE (gst_pad_get_parent (sinkpad));
1677 GST_DEBUG_OBJECT (parse, "activate pull");
1679 result = gst_base_parse_activate (parse, active);
1683 result &= gst_pad_start_task (sinkpad,
1684 (GstTaskFunction) gst_base_parse_loop, sinkpad);
1686 result &= gst_pad_stop_task (sinkpad);
1691 parse->priv->pad_mode = active ? GST_ACTIVATE_PULL : GST_ACTIVATE_NONE;
1693 GST_DEBUG_OBJECT (parse, "sink activate pull: %d", result);
1695 gst_object_unref (parse);
1701 * gst_base_parse_set_duration:
1702 * @parse: #GstBaseParse.
1704 * @duration: duration value.
1706 * Sets the duration of the currently playing media. Subclass can use this
1707 * when it notices a change in the media duration.
1710 gst_base_parse_set_duration (GstBaseParse * parse,
1711 GstFormat fmt, gint64 duration)
1713 g_return_if_fail (parse != NULL);
1715 GST_BASE_PARSE_LOCK (parse);
1716 if (duration != parse->priv->duration) {
1719 m = gst_message_new_duration (GST_OBJECT (parse), fmt, duration);
1720 gst_element_post_message (GST_ELEMENT (parse), m);
1722 /* TODO: what about duration tag? */
1724 parse->priv->duration = duration;
1725 parse->priv->duration_fmt = fmt;
1726 GST_DEBUG_OBJECT (parse, "set duration: %" G_GINT64_FORMAT, duration);
1727 GST_BASE_PARSE_UNLOCK (parse);
1732 * gst_base_parse_set_min_frame_size:
1733 * @parse: #GstBaseParse.
1734 * @min_size: Minimum size of the data that this base class should give to
1737 * Subclass can use this function to tell the base class that it needs to
1738 * give at least #min_size buffers.
1741 gst_base_parse_set_min_frame_size (GstBaseParse * parse, guint min_size)
1743 g_return_if_fail (parse != NULL);
1745 GST_BASE_PARSE_LOCK (parse);
1746 parse->priv->min_frame_size = min_size;
1747 GST_LOG_OBJECT (parse, "set frame_min_size: %d", min_size);
1748 GST_BASE_PARSE_UNLOCK (parse);
1752 * gst_base_transform_set_passthrough:
1753 * @trans: the #GstBaseParse to set
1754 * @passthrough: boolean indicating passthrough mode.
1756 * Set passthrough mode for this parser. If operating in passthrough,
1757 * incoming buffers are pushed through unmodified.
1760 gst_base_parse_set_passthrough (GstBaseParse * parse, gboolean passthrough)
1762 g_return_if_fail (parse != NULL);
1764 GST_BASE_PARSE_LOCK (parse);
1765 parse->priv->passthrough = passthrough;
1766 GST_LOG_OBJECT (parse, "set passthrough: %d", passthrough);
1767 GST_BASE_PARSE_UNLOCK (parse);
1771 * gst_base_transform_set_frame_props:
1772 * @parse: the #GstBaseParse to set
1773 * @fps_num: frames per second (numerator).
1774 * @fps_den: frames per second (denominator).
1775 * @interval: duration update interval in frames.
1777 * If frames per second is configured, parser can provide for default @convert
1778 * between GST_FORMAT_TIME and GST_FORMAT_BYTES, as well as buffer duration
1779 * and timestamping. However, even if this frame information is provided,
1780 * subclass can still choose to provide for a @convert and set buffer metadata.
1781 * If #interval is non-zero (default), then stream duration is determined
1782 * based on frame and byte counts, and updated every #interval frames.
1785 gst_base_parse_set_frame_props (GstBaseParse * parse, guint fps_num,
1786 guint fps_den, gint interval)
1788 g_return_if_fail (parse != NULL);
1790 GST_BASE_PARSE_LOCK (parse);
1791 parse->priv->fps_num = fps_num;
1792 parse->priv->fps_den = fps_den;
1793 parse->priv->update_interval = interval;
1794 if (!fps_num || !fps_den) {
1795 GST_DEBUG_OBJECT (parse, "invalid fps (%d/%d), ignoring parameters",
1797 fps_num = fps_den = 0;
1799 parse->priv->frame_duration = GST_CLOCK_TIME_NONE;
1801 parse->priv->frame_duration =
1802 gst_util_uint64_scale (GST_SECOND, parse->priv->fps_den,
1803 parse->priv->fps_num);
1805 GST_LOG_OBJECT (parse, "set fps: %d/%d => duration: %" G_GINT64_FORMAT " ms",
1806 fps_num, fps_den, parse->priv->frame_duration / GST_MSECOND);
1807 GST_LOG_OBJECT (parse, "set update interval: %d", interval);
1808 GST_BASE_PARSE_UNLOCK (parse);
1812 * gst_base_transform_get_sync:
1813 * @parse: the #GstBaseParse to query
1815 * Returns: TRUE if parser is considered 'in sync'. That is, frames have been
1816 * continuously successfully parsed and pushed.
1819 gst_base_parse_get_sync (GstBaseParse * parse)
1823 g_return_val_if_fail (parse != NULL, FALSE);
1825 GST_BASE_PARSE_LOCK (parse);
1826 /* losing sync is pretty much a discont (and vice versa), no ? */
1827 ret = !parse->priv->discont;
1828 GST_BASE_PARSE_UNLOCK (parse);
1830 GST_DEBUG_OBJECT (parse, "sync: %d", ret);
1835 * gst_base_transform_get_drain:
1836 * @parse: the #GstBaseParse to query
1838 * Returns: TRUE if parser is currently 'draining'. That is, leftover data
1839 * (e.g. in FLUSH or EOS situation) is being parsed.
1842 gst_base_parse_get_drain (GstBaseParse * parse)
1846 g_return_val_if_fail (parse != NULL, FALSE);
1848 GST_BASE_PARSE_LOCK (parse);
1849 /* losing sync is pretty much a discont (and vice versa), no ? */
1850 ret = parse->priv->drain;
1851 GST_BASE_PARSE_UNLOCK (parse);
1853 GST_DEBUG_OBJECT (parse, "drain: %d", ret);
1858 * gst_base_parse_get_querytypes:
1861 * Returns: A table of #GstQueryType items describing supported query types.
1863 static const GstQueryType *
1864 gst_base_parse_get_querytypes (GstPad * pad)
1866 static const GstQueryType list[] = {
1880 * gst_base_parse_query:
1882 * @query: #GstQuery.
1884 * Returns: TRUE on success.
1887 gst_base_parse_query (GstPad * pad, GstQuery * query)
1889 GstBaseParse *parse;
1890 GstBaseParseClass *klass;
1891 gboolean res = FALSE;
1893 parse = GST_BASE_PARSE (GST_PAD_PARENT (pad));
1894 klass = GST_BASE_PARSE_GET_CLASS (parse);
1896 /* If subclass doesn't provide conversion function we can't reply
1897 to the query either */
1898 if (!klass->convert) {
1902 switch (GST_QUERY_TYPE (query)) {
1903 case GST_QUERY_POSITION:
1908 GST_DEBUG_OBJECT (parse, "position query");
1910 gst_query_parse_position (query, &format, NULL);
1912 g_mutex_lock (parse->parse_lock);
1914 if (format == GST_FORMAT_BYTES) {
1915 dest_value = parse->priv->offset;
1917 } else if (format == parse->segment.format &&
1918 GST_CLOCK_TIME_IS_VALID (parse->segment.last_stop)) {
1919 dest_value = parse->segment.last_stop;
1922 /* priv->offset is updated in both PUSH/PULL modes */
1923 res = klass->convert (parse, GST_FORMAT_BYTES, parse->priv->offset,
1924 format, &dest_value);
1926 g_mutex_unlock (parse->parse_lock);
1929 gst_query_set_position (query, format, dest_value);
1931 res = gst_pad_query_default (pad, query);
1935 case GST_QUERY_DURATION:
1940 GST_DEBUG_OBJECT (parse, "duration query");
1942 gst_query_parse_duration (query, &format, NULL);
1944 g_mutex_lock (parse->parse_lock);
1946 if (format == GST_FORMAT_BYTES) {
1947 res = gst_pad_query_peer_duration (parse->sinkpad, &format,
1949 } else if (parse->priv->duration != -1 &&
1950 format == parse->priv->duration_fmt) {
1951 dest_value = parse->priv->duration;
1953 } else if (parse->priv->duration != -1) {
1954 res = klass->convert (parse, parse->priv->duration_fmt,
1955 parse->priv->duration, format, &dest_value);
1956 } else if (parse->priv->estimated_duration != -1) {
1957 dest_value = parse->priv->estimated_duration;
1961 g_mutex_unlock (parse->parse_lock);
1964 gst_query_set_duration (query, format, dest_value);
1966 res = gst_pad_query_default (pad, query);
1969 case GST_QUERY_SEEKING:
1972 gboolean seekable = FALSE;
1974 GST_DEBUG_OBJECT (parse, "seeking query");
1976 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
1978 if (fmt != GST_FORMAT_TIME) {
1979 return gst_pad_query_default (pad, query);
1982 seekable = klass->is_seekable (parse);
1984 /* TODO: could this duration be calculated/converted if subclass
1986 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable, 0,
1987 (parse->priv->duration == -1) ?
1988 GST_CLOCK_TIME_NONE : parse->priv->duration);
1990 GST_DEBUG_OBJECT (parse, "seekable: %d", seekable);
1994 case GST_QUERY_FORMATS:
1995 gst_query_set_formatsv (query, 3, fmtlist);
1999 case GST_QUERY_CONVERT:
2001 GstFormat src_format, dest_format;
2002 gint64 src_value, dest_value;
2004 gst_query_parse_convert (query, &src_format, &src_value,
2005 &dest_format, &dest_value);
2007 /* FIXME: hm? doesn't make sense
2008 * We require all those values to be given
2009 if (src_format && src_value && dest_format && dest_value ) { */
2010 res = klass->convert (parse, src_format, src_value,
2011 dest_format, &dest_value);
2013 gst_query_set_convert (query, src_format, src_value,
2014 dest_format, dest_value);
2020 res = gst_pad_query_default (pad, query);
2028 * gst_base_parse_handle_seek:
2029 * @parse: #GstBaseParse.
2030 * @event: #GstEvent.
2032 * Returns: TRUE if seek succeeded.
2035 gst_base_parse_handle_seek (GstBaseParse * parse, GstEvent * event)
2037 GstBaseParseClass *klass;
2041 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
2042 gboolean flush, update, res = TRUE;
2043 gint64 cur, stop, seekpos;
2044 GstSegment seeksegment = { 0, };
2045 GstFormat dstformat;
2047 klass = GST_BASE_PARSE_GET_CLASS (parse);
2049 gst_event_parse_seek (event, &rate, &format, &flags,
2050 &cur_type, &cur, &stop_type, &stop);
2052 GST_DEBUG_OBJECT (parse, "seek to format %s, "
2053 "start type %d at %" GST_TIME_FORMAT ", end type %d at %"
2054 GST_TIME_FORMAT, gst_format_get_name (format),
2055 cur_type, GST_TIME_ARGS (cur), stop_type, GST_TIME_ARGS (stop));
2057 /* no negative rates yet */
2061 if (cur_type != GST_SEEK_TYPE_SET)
2064 /* For any format other than TIME, see if upstream handles
2065 * it directly or fail. For TIME, try upstream, but do it ourselves if
2066 * it fails upstream */
2067 if (format != GST_FORMAT_TIME) {
2068 /* default action delegates to upstream */
2071 gst_event_ref (event);
2072 if (gst_pad_push_event (parse->sinkpad, event)) {
2077 /* too much estimating going on to support this sensibly,
2078 * and no eos/end-of-segment loop handling either ... */
2079 if ((stop_type == GST_SEEK_TYPE_SET && stop != GST_CLOCK_TIME_NONE) ||
2080 (stop_type != GST_SEEK_TYPE_NONE && stop_type != GST_SEEK_TYPE_SET) ||
2081 (flags & GST_SEEK_FLAG_SEGMENT))
2085 /* get flush flag */
2086 flush = flags & GST_SEEK_FLAG_FLUSH;
2088 /* copy segment, we need this because we still need the old
2089 * segment when we close the current segment. */
2090 memcpy (&seeksegment, &parse->segment, sizeof (GstSegment));
2092 GST_DEBUG_OBJECT (parse, "configuring seek");
2093 gst_segment_set_seek (&seeksegment, rate, format, flags,
2094 cur_type, cur, stop_type, stop, &update);
2096 /* figure out the last position we need to play. If it's configured (stop !=
2097 * -1), use that, else we play until the total duration of the file */
2098 if ((stop = seeksegment.stop) == -1)
2099 stop = seeksegment.duration;
2101 dstformat = GST_FORMAT_BYTES;
2102 if (!gst_pad_query_convert (parse->srcpad, format, seeksegment.last_stop,
2103 &dstformat, &seekpos)) {
2104 GST_DEBUG_OBJECT (parse, "conversion failed");
2108 GST_DEBUG_OBJECT (parse,
2109 "seek position %" G_GINT64_FORMAT " in bytes: %" G_GINT64_FORMAT, cur,
2112 if (parse->priv->pad_mode == GST_ACTIVATE_PULL) {
2115 GST_DEBUG_OBJECT (parse, "seek in PULL mode");
2118 if (parse->srcpad) {
2119 GST_DEBUG_OBJECT (parse, "sending flush start");
2120 gst_pad_push_event (parse->srcpad, gst_event_new_flush_start ());
2123 gst_pad_pause_task (parse->sinkpad);
2126 /* we should now be able to grab the streaming thread because we stopped it
2127 * with the above flush/pause code */
2128 GST_PAD_STREAM_LOCK (parse->sinkpad);
2130 /* save current position */
2131 last_stop = parse->segment.last_stop;
2132 GST_DEBUG_OBJECT (parse, "stopped streaming at %" G_GINT64_FORMAT,
2135 /* now commit to new position */
2136 parse->priv->offset = seekpos;
2138 /* prepare for streaming again */
2140 GST_DEBUG_OBJECT (parse, "sending flush stop");
2141 gst_pad_push_event (parse->srcpad, gst_event_new_flush_stop ());
2143 if (parse->close_segment)
2144 gst_event_unref (parse->close_segment);
2146 parse->close_segment = gst_event_new_new_segment (TRUE,
2147 parse->segment.rate, parse->segment.format,
2148 parse->segment.accum, parse->segment.last_stop, parse->segment.accum);
2150 /* keep track of our last_stop */
2151 seeksegment.accum = parse->segment.last_stop;
2153 GST_DEBUG_OBJECT (parse, "Created close seg format %d, "
2154 "start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT
2155 ", pos = %" GST_TIME_FORMAT, format,
2156 GST_TIME_ARGS (parse->segment.accum),
2157 GST_TIME_ARGS (parse->segment.last_stop),
2158 GST_TIME_ARGS (parse->segment.accum));
2161 memcpy (&parse->segment, &seeksegment, sizeof (GstSegment));
2163 /* store the newsegment event so it can be sent from the streaming thread. */
2164 if (parse->pending_segment)
2165 gst_event_unref (parse->pending_segment);
2167 /* This will be sent later in _loop() */
2168 parse->pending_segment =
2169 gst_event_new_new_segment (FALSE, parse->segment.rate,
2170 parse->segment.format,
2171 parse->segment.last_stop, stop, parse->segment.last_stop);
2173 GST_DEBUG_OBJECT (parse, "Created newseg format %d, "
2174 "start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT
2175 ", pos = %" GST_TIME_FORMAT, format,
2176 GST_TIME_ARGS (parse->segment.last_stop),
2177 GST_TIME_ARGS (stop), GST_TIME_ARGS (parse->segment.last_stop));
2179 /* mark discont if we are going to stream from another position. */
2180 if (last_stop != parse->segment.last_stop) {
2181 GST_DEBUG_OBJECT (parse,
2182 "mark DISCONT, we did a seek to another position");
2183 parse->priv->discont = TRUE;
2184 parse->priv->next_ts = parse->segment.last_stop;
2187 /* Start streaming thread if paused */
2188 gst_pad_start_task (parse->sinkpad,
2189 (GstTaskFunction) gst_base_parse_loop, parse->sinkpad);
2191 GST_PAD_STREAM_UNLOCK (parse->sinkpad);
2193 GstEvent *new_event;
2194 /* The only thing we need to do in PUSH-mode is to send the
2195 seek event (in bytes) to upstream. Segment / flush handling happens
2196 in corresponding src event handlers */
2197 GST_DEBUG_OBJECT (parse, "seek in PUSH mode");
2198 new_event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flush,
2199 GST_SEEK_TYPE_SET, seekpos, stop_type, stop);
2201 res = gst_pad_push_event (parse->sinkpad, new_event);
2210 GST_DEBUG_OBJECT (parse, "negative playback rates are not supported yet.");
2216 GST_DEBUG_OBJECT (parse, "unsupported seek type.");
2223 * gst_base_parse_handle_tag:
2224 * @parse: #GstBaseParse.
2225 * @event: #GstEvent.
2227 * Checks if bitrates are available from upstream tags so that we don't
2228 * override them later
2231 gst_base_parse_handle_tag (GstBaseParse * parse, GstEvent * event)
2233 GstTagList *taglist = NULL;
2236 gst_event_parse_tag (event, &taglist);
2238 if (gst_tag_list_get_uint (taglist, GST_TAG_MINIMUM_BITRATE, &tmp))
2239 parse->priv->post_min_bitrate = FALSE;
2240 if (gst_tag_list_get_uint (taglist, GST_TAG_BITRATE, &tmp))
2241 parse->priv->post_avg_bitrate = FALSE;
2242 if (gst_tag_list_get_uint (taglist, GST_TAG_MAXIMUM_BITRATE, &tmp))
2243 parse->priv->post_max_bitrate = FALSE;
2247 * gst_base_parse_sink_setcaps:
2251 * Returns: TRUE if caps were accepted.
2254 gst_base_parse_sink_setcaps (GstPad * pad, GstCaps * caps)
2256 GstBaseParse *parse;
2257 GstBaseParseClass *klass;
2258 gboolean res = TRUE;
2260 parse = GST_BASE_PARSE (GST_PAD_PARENT (pad));
2261 klass = GST_BASE_PARSE_GET_CLASS (parse);
2263 GST_DEBUG_OBJECT (parse, "caps: %" GST_PTR_FORMAT, caps);
2265 if (klass->set_sink_caps)
2266 res = klass->set_sink_caps (parse, caps);
2268 parse->negotiated = res;
2269 return res && gst_pad_set_caps (pad, caps);