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 caps, buffer timestamp and duration
93 * (although the latter can also be done by GstBaseParse if it is
94 * appropriately configured, see below).
97 * Finally the buffer can be pushed downstream and parsing loop starts
98 * over again. Just prior to actually pushing the buffer in question,
99 * it is passed to @pre_push_buffer which gives subclass yet one
100 * last chance to examine buffer metadata, or to send some custom (tag)
101 * events, or to perform custom (segment) filtering.
104 * During the parsing process GstBaseParseClass will handle both srcpad and
105 * sinkpad events. They will be passed to subclass if @event or
106 * @src_event callbacks have been provided.
111 * <itemizedlist><title>Shutdown phase</title>
113 * GstBaseParse class calls @stop to inform the subclass that data
114 * parsing will be stopped.
120 * Subclass is responsible for providing pad template caps for
121 * source and sink pads. The pads need to be named "sink" and "src". It also
122 * needs to set the fixed caps on srcpad, when the format is ensured (e.g.
123 * when base class calls subclass' @set_sink_caps function).
125 * This base class uses GST_FORMAT_DEFAULT as a meaning of frames. So,
126 * subclass conversion routine needs to know that conversion from
127 * GST_FORMAT_TIME to GST_FORMAT_DEFAULT must return the
128 * frame number that can be found from the given byte position.
130 * GstBaseParse uses subclasses conversion methods also for seeking (or otherwise
131 * uses its own default one, see also below).
133 * Subclass @start and @stop functions will be called to inform the beginning
134 * and end of data processing.
136 * Things that subclass need to take care of:
138 * <listitem><para>Provide pad templates</para></listitem>
140 * Fixate the source pad caps when appropriate
143 * Inform base class how big data chunks should be retrieved. This is
144 * done with @gst_base_parse_set_min_frame_size function.
147 * Examine data chunks passed to subclass with @check_valid_frame
148 * and tell if they contain a valid frame
151 * Set the caps and timestamp to frame that is passed to subclass with
152 * @parse_frame function.
154 * <listitem><para>Provide conversion functions</para></listitem>
156 * Update the duration information with @gst_base_parse_set_duration
159 * Optionally passthrough using @gst_base_parse_set_passthrough
162 * Configure various baseparse parameters using @gst_base_parse_set_seek and
163 * @gst_base_parse_set_frame_props.
166 * In particular, if subclass is unable to determine a duration, but
167 * parsing (or specs) yields a frames per seconds rate, then this can be
168 * provided to GstBaseParse to enable it to cater for
169 * buffer time metadata (which will be taken from upstream as much as possible).
170 * Internally keeping track of frame durations and respective
171 * sizes that have been pushed provides GstBaseParse with an estimated bitrate.
172 * A default @convert (used if not overriden) will then use these
173 * rates to perform obvious conversions. These rates are also used to update
174 * (estimated) duration at regular frame intervals.
181 * - In push mode provide a queue of adapter-"queued" buffers for upstream
183 * - Queue buffers/events until caps are set
193 #include "gstbaseparse.h"
195 #define MIN_FRAMES_TO_POST_BITRATE 10
196 #define TARGET_DIFFERENCE (20 * GST_SECOND)
198 GST_DEBUG_CATEGORY_STATIC (gst_base_parse_debug);
199 #define GST_CAT_DEFAULT gst_base_parse_debug
201 /* Supported formats */
202 static GstFormat fmtlist[] = {
209 #define GST_BASE_PARSE_GET_PRIVATE(obj) \
210 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_PARSE, GstBaseParsePrivate))
212 struct _GstBaseParsePrivate
214 GstActivateMode pad_mode;
217 GstFormat duration_fmt;
218 gint64 estimated_duration;
220 guint min_frame_size;
221 gboolean passthrough;
222 guint fps_num, fps_den;
223 guint update_interval;
225 guint lead_in, lead_out;
226 GstClockTime lead_in_ts, lead_out_ts;
227 GstBaseParseSeekable seekable;
235 GstClockTime next_ts;
236 GstClockTime prev_ts;
237 GstClockTime frame_duration;
238 gboolean seen_keyframe;
243 guint64 data_bytecount;
244 guint64 acc_duration;
245 GstClockTime first_frame_ts;
246 gint64 first_frame_offset;
248 gboolean post_min_bitrate;
249 gboolean post_avg_bitrate;
250 gboolean post_max_bitrate;
254 guint posted_avg_bitrate;
256 GList *pending_events;
260 /* index entry storage, either ours or provided */
264 /* seek table entries only maintained if upstream is BYTE seekable */
265 gboolean upstream_seekable;
266 gboolean upstream_has_duration;
267 gint64 upstream_size;
268 /* minimum distance between two index entries */
269 GstClockTimeDiff idx_interval;
270 /* ts and offset of last entry added */
271 GstClockTime index_last_ts;
272 guint64 index_last_offset;
273 gboolean index_last_valid;
275 /* timestamps currently produced are accurate, e.g. started from 0 onwards */
276 gboolean exact_position;
277 /* seek events are temporarily kept to match them with newsegments */
278 GSList *pending_seeks;
280 /* reverse playback */
281 GSList *buffers_pending;
282 GSList *buffers_queued;
283 GstClockTime last_ts;
287 typedef struct _GstBaseParseSeek
292 GstClockTime start_ts;
295 static GstElementClass *parent_class = NULL;
297 static void gst_base_parse_class_init (GstBaseParseClass * klass);
298 static void gst_base_parse_init (GstBaseParse * parse,
299 GstBaseParseClass * klass);
302 gst_base_parse_get_type (void)
304 static GType base_parse_type = 0;
306 if (!base_parse_type) {
307 static const GTypeInfo base_parse_info = {
308 sizeof (GstBaseParseClass),
309 (GBaseInitFunc) NULL,
310 (GBaseFinalizeFunc) NULL,
311 (GClassInitFunc) gst_base_parse_class_init,
314 sizeof (GstBaseParse),
316 (GInstanceInitFunc) gst_base_parse_init,
319 base_parse_type = g_type_register_static (GST_TYPE_ELEMENT,
320 "GstAudioBaseParseBad", &base_parse_info, G_TYPE_FLAG_ABSTRACT);
322 return base_parse_type;
325 static void gst_base_parse_finalize (GObject * object);
327 static GstStateChangeReturn gst_base_parse_change_state (GstElement * element,
328 GstStateChange transition);
329 static void gst_base_parse_reset (GstBaseParse * parse);
331 static void gst_base_parse_set_index (GstElement * element, GstIndex * index);
332 static GstIndex *gst_base_parse_get_index (GstElement * element);
334 static gboolean gst_base_parse_sink_activate (GstPad * sinkpad);
335 static gboolean gst_base_parse_sink_activate_push (GstPad * pad,
337 static gboolean gst_base_parse_sink_activate_pull (GstPad * pad,
339 static gboolean gst_base_parse_handle_seek (GstBaseParse * parse,
341 static void gst_base_parse_handle_tag (GstBaseParse * parse, GstEvent * event);
343 static gboolean gst_base_parse_src_event (GstPad * pad, GstEvent * event);
344 static gboolean gst_base_parse_sink_event (GstPad * pad, GstEvent * event);
345 static gboolean gst_base_parse_query (GstPad * pad, GstQuery * query);
346 static gboolean gst_base_parse_sink_setcaps (GstPad * pad, GstCaps * caps);
347 static const GstQueryType *gst_base_parse_get_querytypes (GstPad * pad);
349 static GstFlowReturn gst_base_parse_chain (GstPad * pad, GstBuffer * buffer);
350 static void gst_base_parse_loop (GstPad * pad);
352 static gboolean gst_base_parse_check_frame (GstBaseParse * parse,
353 GstBuffer * buffer, guint * framesize, gint * skipsize);
355 static GstFlowReturn gst_base_parse_parse_frame (GstBaseParse * parse,
358 static gboolean gst_base_parse_sink_eventfunc (GstBaseParse * parse,
361 static gboolean gst_base_parse_src_eventfunc (GstBaseParse * parse,
364 static void gst_base_parse_drain (GstBaseParse * parse);
366 static void gst_base_parse_post_bitrates (GstBaseParse * parse,
367 gboolean post_min, gboolean post_avg, gboolean post_max);
369 static gint64 gst_base_parse_find_offset (GstBaseParse * parse,
370 GstClockTime time, gboolean before, GstClockTime * _ts);
371 static GstFlowReturn gst_base_parse_locate_time (GstBaseParse * parse,
372 GstClockTime * _time, gint64 * _offset);
374 static GstFlowReturn gst_base_parse_process_fragment (GstBaseParse * parse,
378 gst_base_parse_clear_queues (GstBaseParse * parse)
380 g_slist_foreach (parse->priv->buffers_queued, (GFunc) gst_buffer_unref, NULL);
381 g_slist_free (parse->priv->buffers_queued);
382 parse->priv->buffers_queued = NULL;
383 g_slist_foreach (parse->priv->buffers_pending, (GFunc) gst_buffer_unref,
385 g_slist_free (parse->priv->buffers_pending);
386 parse->priv->buffers_pending = NULL;
390 gst_base_parse_finalize (GObject * object)
392 GstBaseParse *parse = GST_BASE_PARSE (object);
395 g_object_unref (parse->adapter);
397 if (parse->pending_segment) {
398 p_ev = &parse->pending_segment;
399 gst_event_replace (p_ev, NULL);
401 if (parse->close_segment) {
402 p_ev = &parse->close_segment;
403 gst_event_replace (p_ev, NULL);
406 if (parse->priv->cache) {
407 gst_buffer_unref (parse->priv->cache);
408 parse->priv->cache = NULL;
411 g_list_foreach (parse->priv->pending_events, (GFunc) gst_mini_object_unref,
413 g_list_free (parse->priv->pending_events);
414 parse->priv->pending_events = NULL;
416 if (parse->priv->index) {
417 gst_object_unref (parse->priv->index);
418 parse->priv->index = NULL;
421 gst_base_parse_clear_queues (parse);
423 G_OBJECT_CLASS (parent_class)->finalize (object);
427 gst_base_parse_class_init (GstBaseParseClass * klass)
429 GObjectClass *gobject_class;
430 GstElementClass *gstelement_class;
432 gobject_class = G_OBJECT_CLASS (klass);
433 g_type_class_add_private (klass, sizeof (GstBaseParsePrivate));
434 parent_class = g_type_class_peek_parent (klass);
435 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_parse_finalize);
437 gstelement_class = (GstElementClass *) klass;
438 gstelement_class->change_state =
439 GST_DEBUG_FUNCPTR (gst_base_parse_change_state);
440 gstelement_class->set_index = GST_DEBUG_FUNCPTR (gst_base_parse_set_index);
441 gstelement_class->get_index = GST_DEBUG_FUNCPTR (gst_base_parse_get_index);
443 /* Default handlers */
444 klass->check_valid_frame = gst_base_parse_check_frame;
445 klass->parse_frame = gst_base_parse_parse_frame;
446 klass->src_event = gst_base_parse_src_eventfunc;
447 klass->convert = gst_base_parse_convert_default;
449 GST_DEBUG_CATEGORY_INIT (gst_base_parse_debug, "baseparse", 0,
450 "baseparse element");
454 gst_base_parse_init (GstBaseParse * parse, GstBaseParseClass * bclass)
456 GstPadTemplate *pad_template;
458 GST_DEBUG_OBJECT (parse, "gst_base_parse_init");
460 parse->priv = GST_BASE_PARSE_GET_PRIVATE (parse);
463 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "sink");
464 g_return_if_fail (pad_template != NULL);
465 parse->sinkpad = gst_pad_new_from_template (pad_template, "sink");
466 gst_pad_set_event_function (parse->sinkpad,
467 GST_DEBUG_FUNCPTR (gst_base_parse_sink_event));
468 gst_pad_set_setcaps_function (parse->sinkpad,
469 GST_DEBUG_FUNCPTR (gst_base_parse_sink_setcaps));
470 gst_pad_set_chain_function (parse->sinkpad,
471 GST_DEBUG_FUNCPTR (gst_base_parse_chain));
472 gst_pad_set_activate_function (parse->sinkpad,
473 GST_DEBUG_FUNCPTR (gst_base_parse_sink_activate));
474 gst_pad_set_activatepush_function (parse->sinkpad,
475 GST_DEBUG_FUNCPTR (gst_base_parse_sink_activate_push));
476 gst_pad_set_activatepull_function (parse->sinkpad,
477 GST_DEBUG_FUNCPTR (gst_base_parse_sink_activate_pull));
478 gst_element_add_pad (GST_ELEMENT (parse), parse->sinkpad);
480 GST_DEBUG_OBJECT (parse, "sinkpad created");
483 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
484 g_return_if_fail (pad_template != NULL);
485 parse->srcpad = gst_pad_new_from_template (pad_template, "src");
486 gst_pad_set_event_function (parse->srcpad,
487 GST_DEBUG_FUNCPTR (gst_base_parse_src_event));
488 gst_pad_set_query_type_function (parse->srcpad,
489 GST_DEBUG_FUNCPTR (gst_base_parse_get_querytypes));
490 gst_pad_set_query_function (parse->srcpad,
491 GST_DEBUG_FUNCPTR (gst_base_parse_query));
492 gst_pad_use_fixed_caps (parse->srcpad);
493 gst_element_add_pad (GST_ELEMENT (parse), parse->srcpad);
494 GST_DEBUG_OBJECT (parse, "src created");
496 parse->adapter = gst_adapter_new ();
498 parse->priv->pad_mode = GST_ACTIVATE_NONE;
501 gst_base_parse_reset (parse);
502 GST_DEBUG_OBJECT (parse, "init ok");
506 gst_base_parse_reset (GstBaseParse * parse)
508 GST_OBJECT_LOCK (parse);
509 gst_segment_init (&parse->segment, GST_FORMAT_TIME);
510 parse->priv->duration = -1;
511 parse->priv->min_frame_size = 1;
512 parse->priv->discont = TRUE;
513 parse->priv->flushing = FALSE;
514 parse->priv->offset = 0;
515 parse->priv->sync_offset = 0;
516 parse->priv->update_interval = 50;
517 parse->priv->fps_num = parse->priv->fps_den = 0;
518 parse->priv->frame_duration = GST_CLOCK_TIME_NONE;
519 parse->priv->lead_in = parse->priv->lead_out = 0;
520 parse->priv->lead_in_ts = parse->priv->lead_out_ts = 0;
521 parse->priv->seekable = GST_BASE_PARSE_SEEK_DEFAULT;
522 parse->priv->bitrate = 0;
523 parse->priv->framecount = 0;
524 parse->priv->bytecount = 0;
525 parse->priv->acc_duration = 0;
526 parse->priv->first_frame_ts = GST_CLOCK_TIME_NONE;
527 parse->priv->first_frame_offset = -1;
528 parse->priv->estimated_duration = -1;
529 parse->priv->next_ts = 0;
530 parse->priv->passthrough = FALSE;
531 parse->priv->post_min_bitrate = TRUE;
532 parse->priv->post_avg_bitrate = TRUE;
533 parse->priv->post_max_bitrate = TRUE;
534 parse->priv->min_bitrate = G_MAXUINT;
535 parse->priv->max_bitrate = 0;
536 parse->priv->avg_bitrate = 0;
537 parse->priv->posted_avg_bitrate = 0;
539 parse->priv->index_last_ts = 0;
540 parse->priv->index_last_offset = 0;
541 parse->priv->index_last_valid = TRUE;
542 parse->priv->upstream_seekable = FALSE;
543 parse->priv->upstream_size = 0;
544 parse->priv->upstream_has_duration = FALSE;
545 parse->priv->idx_interval = 0;
546 parse->priv->exact_position = TRUE;
547 parse->priv->seen_keyframe = FALSE;
549 parse->priv->last_ts = GST_CLOCK_TIME_NONE;
550 parse->priv->last_offset = 0;
552 if (parse->pending_segment) {
553 gst_event_unref (parse->pending_segment);
554 parse->pending_segment = NULL;
557 g_list_foreach (parse->priv->pending_events, (GFunc) gst_mini_object_unref,
559 g_list_free (parse->priv->pending_events);
560 parse->priv->pending_events = NULL;
562 if (parse->priv->cache) {
563 gst_buffer_unref (parse->priv->cache);
564 parse->priv->cache = NULL;
567 g_slist_foreach (parse->priv->pending_seeks, (GFunc) g_free, NULL);
568 g_slist_free (parse->priv->pending_seeks);
569 parse->priv->pending_seeks = NULL;
571 GST_OBJECT_UNLOCK (parse);
575 * gst_base_parse_check_frame:
576 * @parse: #GstBaseParse.
577 * @buffer: GstBuffer.
578 * @framesize: This will be set to tell the found frame size in bytes.
579 * @skipsize: Output parameter that tells how much data needs to be skipped
580 * in order to find the following frame header.
582 * Default callback for check_valid_frame.
584 * Returns: Always TRUE.
587 gst_base_parse_check_frame (GstBaseParse * parse,
588 GstBuffer * buffer, guint * framesize, gint * skipsize)
590 *framesize = GST_BUFFER_SIZE (buffer);
597 * gst_base_parse_parse_frame:
598 * @parse: #GstBaseParse.
599 * @buffer: #GstBuffer.
601 * Default callback for parse_frame.
604 gst_base_parse_parse_frame (GstBaseParse * parse, GstBuffer * buffer)
606 if (!GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
607 GST_CLOCK_TIME_IS_VALID (parse->priv->next_ts)) {
608 GST_BUFFER_TIMESTAMP (buffer) = parse->priv->next_ts;
610 if (!GST_BUFFER_DURATION_IS_VALID (buffer) &&
611 GST_CLOCK_TIME_IS_VALID (parse->priv->frame_duration)) {
612 GST_BUFFER_DURATION (buffer) = parse->priv->frame_duration;
618 * gst_base_parse_convert:
619 * @parse: #GstBaseParse.
620 * @src_format: #GstFormat describing the source format.
621 * @src_value: Source value to be converted.
622 * @dest_format: #GstFormat defining the converted format.
623 * @dest_value: Pointer where the conversion result will be put.
625 * Converts using configured "convert" vmethod in #GstBaseParse class.
627 * Returns: TRUE if conversion was successful.
630 gst_base_parse_convert (GstBaseParse * parse,
631 GstFormat src_format,
632 gint64 src_value, GstFormat dest_format, gint64 * dest_value)
634 GstBaseParseClass *klass = GST_BASE_PARSE_GET_CLASS (parse);
637 g_return_val_if_fail (dest_value != NULL, FALSE);
642 ret = klass->convert (parse, src_format, src_value, dest_format, dest_value);
644 #ifndef GST_DISABLE_GST_DEBUG
647 if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES) {
648 GST_LOG_OBJECT (parse,
649 "TIME -> BYTES: %" GST_TIME_FORMAT " -> %" G_GINT64_FORMAT,
650 GST_TIME_ARGS (src_value), *dest_value);
651 } else if (dest_format == GST_FORMAT_TIME &&
652 src_format == GST_FORMAT_BYTES) {
653 GST_LOG_OBJECT (parse,
654 "BYTES -> TIME: %" G_GINT64_FORMAT " -> %" GST_TIME_FORMAT,
655 src_value, GST_TIME_ARGS (*dest_value));
657 GST_LOG_OBJECT (parse,
658 "%s -> %s: %" G_GINT64_FORMAT " -> %" G_GINT64_FORMAT,
659 GST_STR_NULL (gst_format_get_name (src_format)),
660 GST_STR_NULL (gst_format_get_name (dest_format)),
661 src_value, *dest_value);
664 GST_DEBUG_OBJECT (parse, "conversion failed");
673 * gst_base_parse_sink_event:
674 * @pad: #GstPad that received the event.
675 * @event: #GstEvent to be handled.
677 * Handler for sink pad events.
679 * Returns: TRUE if the event was handled.
682 gst_base_parse_sink_event (GstPad * pad, GstEvent * event)
685 GstBaseParseClass *bclass;
686 gboolean handled = FALSE;
689 parse = GST_BASE_PARSE (gst_pad_get_parent (pad));
690 bclass = GST_BASE_PARSE_GET_CLASS (parse);
692 GST_DEBUG_OBJECT (parse, "handling event %d, %s", GST_EVENT_TYPE (event),
693 GST_EVENT_TYPE_NAME (event));
695 /* Cache all events except EOS, NEWSEGMENT and FLUSH_STOP if we have a
697 if (parse->pending_segment && GST_EVENT_TYPE (event) != GST_EVENT_EOS
698 && GST_EVENT_TYPE (event) != GST_EVENT_NEWSEGMENT
699 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_START
700 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
702 if (GST_EVENT_TYPE (event) == GST_EVENT_TAG)
703 /* See if any bitrate tags were posted */
704 gst_base_parse_handle_tag (parse, event);
706 parse->priv->pending_events =
707 g_list_append (parse->priv->pending_events, event);
711 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS &&
712 parse->priv->framecount < MIN_FRAMES_TO_POST_BITRATE)
713 /* We've not posted bitrate tags yet - do so now */
714 gst_base_parse_post_bitrates (parse, TRUE, TRUE, TRUE);
717 handled = bclass->event (parse, event);
720 handled = gst_base_parse_sink_eventfunc (parse, event);
723 ret = gst_pad_event_default (pad, event);
726 gst_object_unref (parse);
727 GST_DEBUG_OBJECT (parse, "event handled");
733 * gst_base_parse_sink_eventfunc:
734 * @parse: #GstBaseParse.
735 * @event: #GstEvent to be handled.
737 * Element-level event handler function.
739 * The event will be unreffed only if it has been handled and this
740 * function returns %TRUE
742 * Returns: %TRUE if the event was handled and not need forwarding.
745 gst_base_parse_sink_eventfunc (GstBaseParse * parse, GstEvent * event)
747 gboolean handled = FALSE;
750 switch (GST_EVENT_TYPE (event)) {
751 case GST_EVENT_NEWSEGMENT:
753 gdouble rate, applied_rate;
755 gint64 start, stop, pos, next_ts, offset = 0;
758 gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
759 &format, &start, &stop, &pos);
761 GST_DEBUG_OBJECT (parse, "newseg rate %g, applied rate %g, "
762 "format %d, start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT
763 ", pos = %" GST_TIME_FORMAT, rate, applied_rate, format,
764 GST_TIME_ARGS (start), GST_TIME_ARGS (stop), GST_TIME_ARGS (pos));
766 if (format == GST_FORMAT_BYTES) {
767 GstClockTime seg_start, seg_stop;
768 GstBaseParseSeek *seek = NULL;
771 /* stop time is allowed to be open-ended, but not start & pos */
772 seg_stop = GST_CLOCK_TIME_NONE;
776 GST_OBJECT_LOCK (parse);
777 for (node = parse->priv->pending_seeks; node; node = node->next) {
778 GstBaseParseSeek *tmp = node->data;
780 if (tmp->offset == pos) {
785 parse->priv->pending_seeks =
786 g_slist_remove (parse->priv->pending_seeks, seek);
787 GST_OBJECT_UNLOCK (parse);
790 GST_DEBUG_OBJECT (parse,
791 "Matched newsegment to%s seek: %" GST_SEGMENT_FORMAT,
792 seek->accurate ? " accurate" : "", &seek->segment);
793 seg_start = seek->segment.start;
794 seg_stop = seek->segment.stop;
795 next_ts = seek->start_ts;
796 parse->priv->exact_position = seek->accurate;
799 /* best attempt convert */
800 /* as these are only estimates, stop is kept open-ended to avoid
801 * premature cutting */
802 gst_base_parse_convert (parse, GST_FORMAT_BYTES, start,
803 GST_FORMAT_TIME, (gint64 *) & seg_start);
804 parse->priv->exact_position = (start == 0);
808 gst_event_unref (event);
809 event = gst_event_new_new_segment_full (update, rate, applied_rate,
810 GST_FORMAT_TIME, seg_start, seg_stop, seg_start);
811 format = GST_FORMAT_TIME;
814 GST_DEBUG_OBJECT (parse, "Converted incoming segment to TIME. "
815 "start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT,
816 GST_TIME_ARGS (seg_start), GST_TIME_ARGS (seg_stop));
817 } else if (format != GST_FORMAT_TIME) {
818 /* Unknown incoming segment format. Output a default open-ended
820 gst_event_unref (event);
821 event = gst_event_new_new_segment_full (update, rate, applied_rate,
822 GST_FORMAT_TIME, 0, GST_CLOCK_TIME_NONE, 0);
823 format = GST_FORMAT_TIME;
825 stop = GST_CLOCK_TIME_NONE;
827 /* not considered BYTE seekable if it is talking to us in TIME,
828 * whatever else it might claim */
829 parse->priv->upstream_seekable = FALSE;
833 gst_segment_set_newsegment_full (&parse->segment, update, rate,
834 applied_rate, format, start, stop, start);
836 /* save the segment for later, right before we push a new buffer so that
837 * the caps are fixed and the next linked element can receive
839 eventp = &parse->pending_segment;
840 gst_event_replace (eventp, event);
841 gst_event_unref (event);
844 /* but finish the current segment */
845 GST_DEBUG_OBJECT (parse, "draining current segment");
846 if (parse->segment.rate > 0.0)
847 gst_base_parse_drain (parse);
849 gst_base_parse_process_fragment (parse, FALSE);
850 gst_adapter_clear (parse->adapter);
851 parse->priv->offset = offset;
852 parse->priv->sync_offset = offset;
853 parse->priv->next_ts = next_ts;
854 parse->priv->last_ts = GST_CLOCK_TIME_NONE;
855 parse->priv->discont = TRUE;
856 parse->priv->seen_keyframe = FALSE;
860 case GST_EVENT_FLUSH_START:
861 parse->priv->flushing = TRUE;
862 handled = gst_pad_push_event (parse->srcpad, gst_event_ref (event));
864 gst_event_unref (event);
865 /* Wait for _chain() to exit by taking the srcpad STREAM_LOCK */
866 GST_PAD_STREAM_LOCK (parse->srcpad);
867 GST_PAD_STREAM_UNLOCK (parse->srcpad);
871 case GST_EVENT_FLUSH_STOP:
872 gst_adapter_clear (parse->adapter);
873 gst_base_parse_clear_queues (parse);
874 parse->priv->flushing = FALSE;
875 parse->priv->discont = TRUE;
876 parse->priv->last_ts = GST_CLOCK_TIME_NONE;
880 if (parse->segment.rate > 0.0)
881 gst_base_parse_drain (parse);
883 gst_base_parse_process_fragment (parse, FALSE);
885 /* If we STILL have zero frames processed, fire an error */
886 if (parse->priv->framecount == 0) {
887 GST_ELEMENT_ERROR (parse, STREAM, WRONG_TYPE,
888 ("No valid frames found before end of stream"), (NULL));
890 /* newsegment before eos */
891 if (parse->pending_segment) {
892 gst_pad_push_event (parse->srcpad, parse->pending_segment);
893 parse->pending_segment = NULL;
906 * gst_base_parse_src_event:
907 * @pad: #GstPad that received the event.
908 * @event: #GstEvent that was received.
910 * Handler for source pad events.
912 * Returns: TRUE if the event was handled.
915 gst_base_parse_src_event (GstPad * pad, GstEvent * event)
918 GstBaseParseClass *bclass;
919 gboolean handled = FALSE;
922 parse = GST_BASE_PARSE (gst_pad_get_parent (pad));
923 bclass = GST_BASE_PARSE_GET_CLASS (parse);
925 GST_DEBUG_OBJECT (parse, "event %d, %s", GST_EVENT_TYPE (event),
926 GST_EVENT_TYPE_NAME (event));
928 if (bclass->src_event)
929 handled = bclass->src_event (parse, event);
932 ret = gst_pad_event_default (pad, event);
934 gst_event_unref (event);
936 gst_object_unref (parse);
942 * gst_base_parse_src_eventfunc:
943 * @parse: #GstBaseParse.
944 * @event: #GstEvent that was received.
946 * Default srcpad event handler.
948 * Returns: TRUE if the event was handled and can be dropped.
951 gst_base_parse_src_eventfunc (GstBaseParse * parse, GstEvent * event)
953 gboolean handled = FALSE;
955 switch (GST_EVENT_TYPE (event)) {
958 if (parse->priv->seekable > GST_BASE_PARSE_SEEK_NONE) {
959 handled = gst_base_parse_handle_seek (parse, event);
971 * gst_base_parse_convert_default:
972 * @parse: #GstBaseParse.
973 * @src_format: #GstFormat describing the source format.
974 * @src_value: Source value to be converted.
975 * @dest_format: #GstFormat defining the converted format.
976 * @dest_value: Pointer where the conversion result will be put.
978 * Default implementation of "convert" vmethod in #GstBaseParse class.
980 * Returns: TRUE if conversion was successful.
983 gst_base_parse_convert_default (GstBaseParse * parse,
984 GstFormat src_format,
985 gint64 src_value, GstFormat dest_format, gint64 * dest_value)
987 gboolean ret = FALSE;
988 guint64 bytes, duration;
990 if (G_UNLIKELY (src_format == dest_format)) {
991 *dest_value = src_value;
995 if (G_UNLIKELY (src_value == -1)) {
1000 if (G_UNLIKELY (src_value == 0)) {
1005 /* need at least some frames */
1006 if (!parse->priv->framecount)
1009 duration = parse->priv->acc_duration / GST_MSECOND;
1010 bytes = parse->priv->bytecount;
1012 if (G_UNLIKELY (!duration || !bytes))
1015 if (src_format == GST_FORMAT_BYTES) {
1016 if (dest_format == GST_FORMAT_TIME) {
1017 /* BYTES -> TIME conversion */
1018 GST_DEBUG_OBJECT (parse, "converting bytes -> time");
1019 *dest_value = gst_util_uint64_scale (src_value, duration, bytes);
1020 *dest_value *= GST_MSECOND;
1021 GST_DEBUG_OBJECT (parse, "conversion result: %" G_GINT64_FORMAT " ms",
1022 *dest_value / GST_MSECOND);
1025 } else if (src_format == GST_FORMAT_TIME) {
1026 if (dest_format == GST_FORMAT_BYTES) {
1027 GST_DEBUG_OBJECT (parse, "converting time -> bytes");
1028 *dest_value = gst_util_uint64_scale (src_value / GST_MSECOND, bytes,
1030 GST_DEBUG_OBJECT (parse,
1031 "time %" G_GINT64_FORMAT " ms in bytes = %" G_GINT64_FORMAT,
1032 src_value / GST_MSECOND, *dest_value);
1035 } else if (src_format == GST_FORMAT_DEFAULT) {
1036 /* DEFAULT == frame-based */
1037 if (dest_format == GST_FORMAT_TIME) {
1038 if (parse->priv->fps_den) {
1039 *dest_value = gst_util_uint64_scale (src_value,
1040 GST_SECOND * parse->priv->fps_den, parse->priv->fps_num);
1043 } else if (dest_format == GST_FORMAT_BYTES) {
1051 * gst_base_parse_update_duration:
1052 * @parse: #GstBaseParse.
1056 gst_base_parse_update_duration (GstBaseParse * aacparse)
1059 GstBaseParse *parse;
1061 parse = GST_BASE_PARSE (aacparse);
1063 peer = gst_pad_get_peer (parse->sinkpad);
1065 GstFormat pformat = GST_FORMAT_BYTES;
1066 gboolean qres = FALSE;
1067 gint64 ptot, dest_value;
1069 qres = gst_pad_query_duration (peer, &pformat, &ptot);
1070 gst_object_unref (GST_OBJECT (peer));
1072 if (gst_base_parse_convert (parse, pformat, ptot,
1073 GST_FORMAT_TIME, &dest_value))
1074 parse->priv->estimated_duration = dest_value;
1080 gst_base_parse_post_bitrates (GstBaseParse * parse, gboolean post_min,
1081 gboolean post_avg, gboolean post_max)
1083 GstTagList *taglist = gst_tag_list_new ();
1085 if (post_min && parse->priv->post_min_bitrate)
1086 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE,
1087 GST_TAG_MINIMUM_BITRATE, parse->priv->min_bitrate, NULL);
1089 if (post_avg && parse->priv->post_avg_bitrate) {
1090 parse->priv->posted_avg_bitrate = parse->priv->avg_bitrate;
1091 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_BITRATE,
1092 parse->priv->avg_bitrate, NULL);
1095 if (post_max && parse->priv->post_max_bitrate)
1096 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE,
1097 GST_TAG_MAXIMUM_BITRATE, parse->priv->max_bitrate, NULL);
1099 GST_DEBUG_OBJECT (parse, "Updated bitrates. Min: %u, Avg: %u, Max: %u",
1100 parse->priv->min_bitrate, parse->priv->avg_bitrate,
1101 parse->priv->max_bitrate);
1103 gst_element_found_tags_for_pad (GST_ELEMENT (parse), parse->srcpad, taglist);
1107 * gst_base_parse_update_bitrates:
1108 * @parse: #GstBaseParse.
1109 * @buffer: Current frame as a #GstBuffer
1111 * Keeps track of the minimum and maximum bitrates, and also maintains a
1112 * running average bitrate of the stream so far.
1115 gst_base_parse_update_bitrates (GstBaseParse * parse, GstBuffer * buffer)
1117 /* Only update the tag on a 10 kbps delta */
1118 static const gint update_threshold = 10000;
1120 GstBaseParseClass *klass;
1121 guint64 data_len, frame_dur;
1122 gint overhead = 0, frame_bitrate, old_avg_bitrate;
1123 gboolean update_min = FALSE, update_avg = FALSE, update_max = FALSE;
1125 klass = GST_BASE_PARSE_GET_CLASS (parse);
1127 if (klass->get_frame_overhead) {
1128 overhead = klass->get_frame_overhead (parse, buffer);
1133 data_len = GST_BUFFER_SIZE (buffer) - overhead;
1134 parse->priv->data_bytecount += data_len;
1136 /* duration should be valid by now,
1137 * either set by subclass or maybe based on fps settings */
1138 if (GST_BUFFER_DURATION_IS_VALID (buffer) && parse->priv->acc_duration != 0) {
1139 /* Calculate duration of a frame from buffer properties */
1140 frame_dur = GST_BUFFER_DURATION (buffer);
1141 parse->priv->avg_bitrate = (8 * parse->priv->data_bytecount * GST_SECOND) /
1142 parse->priv->acc_duration;
1145 /* No way to figure out frame duration (is this even possible?) */
1149 /* override if subclass provided bitrate, e.g. metadata based */
1150 if (parse->priv->bitrate) {
1151 parse->priv->avg_bitrate = parse->priv->bitrate;
1152 /* spread this (confirmed) info ASAP */
1153 if (parse->priv->posted_avg_bitrate != parse->priv->avg_bitrate)
1154 gst_base_parse_post_bitrates (parse, FALSE, TRUE, FALSE);
1157 frame_bitrate = (8 * data_len * GST_SECOND) / frame_dur;
1159 GST_LOG_OBJECT (parse, "frame bitrate %u, avg bitrate %u", frame_bitrate,
1160 parse->priv->avg_bitrate);
1162 if (parse->priv->framecount < MIN_FRAMES_TO_POST_BITRATE) {
1164 } else if (parse->priv->framecount == MIN_FRAMES_TO_POST_BITRATE) {
1165 /* always post all at threshold time */
1166 update_min = update_max = update_avg = TRUE;
1168 if (frame_bitrate < parse->priv->min_bitrate) {
1169 parse->priv->min_bitrate = frame_bitrate;
1173 if (frame_bitrate > parse->priv->max_bitrate) {
1174 parse->priv->max_bitrate = frame_bitrate;
1178 old_avg_bitrate = parse->priv->posted_avg_bitrate;
1179 if ((gint) (old_avg_bitrate - parse->priv->avg_bitrate) > update_threshold
1180 || (gint) (parse->priv->avg_bitrate - old_avg_bitrate) >
1185 if ((update_min || update_avg || update_max))
1186 gst_base_parse_post_bitrates (parse, update_min, update_avg, update_max);
1188 /* If average bitrate changes that much and no valid (time) duration provided,
1189 * then post a new duration message so applications can update their cached
1191 if (update_avg && !(parse->priv->duration_fmt == GST_FORMAT_TIME &&
1192 GST_CLOCK_TIME_IS_VALID (parse->priv->duration)))
1193 gst_element_post_message (GST_ELEMENT (parse),
1194 gst_message_new_duration (GST_OBJECT (parse), GST_FORMAT_TIME, -1));
1201 * gst_base_parse_add_index_entry:
1202 * @parse: #GstBaseParse.
1203 * @offset: offset of entry
1204 * @ts: timestamp associated with offset
1205 * @key: whether entry refers to keyframe
1206 * @force: add entry disregarding sanity checks
1208 * Adds an entry to the index associating @offset to @ts. It is recommended
1209 * to only add keyframe entries. @force allows to bypass checks, such as
1210 * whether the stream is (upstream) seekable, another entry is already "close"
1211 * to the new entry, etc.
1213 * Returns: #gboolean indicating whether entry was added
1216 gst_base_parse_add_index_entry (GstBaseParse * parse, guint64 offset,
1217 GstClockTime ts, gboolean key, gboolean force)
1219 gboolean ret = FALSE;
1220 GstIndexAssociation associations[2];
1222 GST_LOG_OBJECT (parse, "Adding key=%d index entry %" GST_TIME_FORMAT
1223 " @ offset 0x%08" G_GINT64_MODIFIER "x", key, GST_TIME_ARGS (ts), offset);
1225 if (G_LIKELY (!force)) {
1227 if (!parse->priv->upstream_seekable) {
1228 GST_DEBUG_OBJECT (parse, "upstream not seekable; discarding");
1232 /* FIXME need better helper data structure that handles these issues
1233 * related to ongoing collecting of index entries */
1234 if (parse->priv->index_last_offset >= offset) {
1235 GST_DEBUG_OBJECT (parse, "already have entries up to offset "
1236 "0x%08" G_GINT64_MODIFIER "x", parse->priv->index_last_offset);
1240 if (GST_CLOCK_DIFF (parse->priv->index_last_ts, ts) <
1241 parse->priv->idx_interval) {
1242 GST_DEBUG_OBJECT (parse, "entry too close to last time %" GST_TIME_FORMAT,
1243 GST_TIME_ARGS (parse->priv->index_last_ts));
1247 /* if last is not really the last one */
1248 if (!parse->priv->index_last_valid) {
1249 GstClockTime prev_ts;
1251 gst_base_parse_find_offset (parse, ts, TRUE, &prev_ts);
1252 if (GST_CLOCK_DIFF (prev_ts, ts) < parse->priv->idx_interval) {
1253 GST_DEBUG_OBJECT (parse,
1254 "entry too close to existing entry %" GST_TIME_FORMAT,
1255 GST_TIME_ARGS (prev_ts));
1256 parse->priv->index_last_offset = offset;
1257 parse->priv->index_last_ts = ts;
1263 associations[0].format = GST_FORMAT_TIME;
1264 associations[0].value = ts;
1265 associations[1].format = GST_FORMAT_BYTES;
1266 associations[1].value = offset;
1268 /* index might change on-the-fly, although that would be nutty app ... */
1269 GST_OBJECT_LOCK (parse);
1270 gst_index_add_associationv (parse->priv->index, parse->priv->index_id,
1271 (key) ? GST_ASSOCIATION_FLAG_KEY_UNIT : GST_ASSOCIATION_FLAG_NONE,
1272 2, (const GstIndexAssociation *) &associations);
1273 GST_OBJECT_UNLOCK (parse);
1276 parse->priv->index_last_offset = offset;
1277 parse->priv->index_last_ts = ts;
1286 /* check for seekable upstream, above and beyond a mere query */
1288 gst_base_parse_check_seekability (GstBaseParse * parse)
1291 gboolean seekable = FALSE;
1292 gint64 start = -1, stop = -1;
1293 guint idx_interval = 0;
1295 query = gst_query_new_seeking (GST_FORMAT_BYTES);
1296 if (!gst_pad_peer_query (parse->sinkpad, query)) {
1297 GST_DEBUG_OBJECT (parse, "seeking query failed");
1301 gst_query_parse_seeking (query, NULL, &seekable, &start, &stop);
1303 /* try harder to query upstream size if we didn't get it the first time */
1304 if (seekable && stop == -1) {
1305 GstFormat fmt = GST_FORMAT_BYTES;
1307 GST_DEBUG_OBJECT (parse, "doing duration query to fix up unset stop");
1308 gst_pad_query_peer_duration (parse->sinkpad, &fmt, &stop);
1311 /* if upstream doesn't know the size, it's likely that it's not seekable in
1312 * practice even if it technically may be seekable */
1313 if (seekable && (start != 0 || stop <= start)) {
1314 GST_DEBUG_OBJECT (parse, "seekable but unknown start/stop -> disable");
1318 /* let's not put every single frame into our index */
1320 if (stop < 10 * 1024 * 1024)
1322 else if (stop < 100 * 1024 * 1024)
1325 idx_interval = 1000;
1329 gst_query_unref (query);
1331 GST_DEBUG_OBJECT (parse, "seekable: %d (%" G_GUINT64_FORMAT " - %"
1332 G_GUINT64_FORMAT ")", seekable, start, stop);
1333 parse->priv->upstream_seekable = seekable;
1334 parse->priv->upstream_size = seekable ? stop : 0;
1336 GST_DEBUG_OBJECT (parse, "idx_interval: %ums", idx_interval);
1337 parse->priv->idx_interval = idx_interval * GST_MSECOND;
1340 /* some misc checks on upstream */
1342 gst_base_parse_check_upstream (GstBaseParse * parse)
1344 GstFormat fmt = GST_FORMAT_TIME;
1347 if (gst_pad_query_peer_duration (parse->sinkpad, &fmt, &stop))
1348 if (GST_CLOCK_TIME_IS_VALID (stop) && stop) {
1349 /* upstream has one, accept it also, and no further updates */
1350 gst_base_parse_set_duration (parse, GST_FORMAT_TIME, stop, 0);
1351 parse->priv->upstream_has_duration = TRUE;
1354 GST_DEBUG_OBJECT (parse, "upstream_has_duration: %d",
1355 parse->priv->upstream_has_duration);
1358 /* checks src caps to determine if dealing with audio or video */
1359 /* TODO maybe forego automagic stuff and let subclass configure it ? */
1361 gst_base_parse_check_media (GstBaseParse * parse)
1366 caps = GST_PAD_CAPS (parse->srcpad);
1367 if (G_LIKELY (caps) && (s = gst_caps_get_structure (caps, 0))) {
1368 parse->priv->is_video =
1369 g_str_has_prefix (gst_structure_get_name (s), "video");
1371 /* historical default */
1372 parse->priv->is_video = FALSE;
1375 GST_DEBUG_OBJECT (parse, "media is video == %d", parse->priv->is_video);
1379 * gst_base_parse_handle_and_push_buffer:
1380 * @parse: #GstBaseParse.
1381 * @klass: #GstBaseParseClass.
1382 * @buffer: #GstBuffer.
1384 * Parses the frame from given buffer and pushes it forward. Also performs
1385 * timestamp handling and checks the segment limits.
1387 * This is called with srcpad STREAM_LOCK held.
1389 * Returns: #GstFlowReturn
1391 static GstFlowReturn
1392 gst_base_parse_handle_and_push_buffer (GstBaseParse * parse,
1393 GstBaseParseClass * klass, GstBuffer * buffer)
1398 if (parse->priv->discont) {
1399 GST_DEBUG_OBJECT (parse, "marking DISCONT");
1400 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1401 parse->priv->discont = FALSE;
1404 /* some one-time start-up */
1405 if (G_UNLIKELY (!parse->priv->framecount)) {
1406 gst_base_parse_check_seekability (parse);
1407 gst_base_parse_check_upstream (parse);
1410 GST_LOG_OBJECT (parse,
1411 "parsing frame at offset %" G_GUINT64_FORMAT
1412 " (%#" G_GINT64_MODIFIER "x) of size %d",
1413 GST_BUFFER_OFFSET (buffer), GST_BUFFER_OFFSET (buffer),
1414 GST_BUFFER_SIZE (buffer));
1416 /* store offset as it might get overwritten */
1417 offset = GST_BUFFER_OFFSET (buffer);
1418 ret = klass->parse_frame (parse, buffer);
1420 /* check initial frame to determine if subclass/format can provide ts.
1421 * If so, that allows and enables extra seek and duration determining options */
1422 if (G_UNLIKELY (parse->priv->first_frame_offset < 0 && ret == GST_FLOW_OK)) {
1423 if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer)) {
1424 parse->priv->first_frame_offset = offset;
1425 parse->priv->first_frame_ts = GST_BUFFER_TIMESTAMP (buffer);
1426 GST_DEBUG_OBJECT (parse, "subclass provided ts %" GST_TIME_FORMAT
1427 " for first frame at offset %" G_GINT64_FORMAT,
1428 GST_TIME_ARGS (parse->priv->first_frame_ts),
1429 parse->priv->first_frame_offset);
1430 if (!GST_CLOCK_TIME_IS_VALID (parse->priv->duration)) {
1432 GstClockTime last_ts = G_MAXINT64;
1434 GST_DEBUG_OBJECT (parse, "no duration; trying scan to determine");
1435 gst_base_parse_locate_time (parse, &last_ts, &off);
1436 if (GST_CLOCK_TIME_IS_VALID (last_ts))
1437 gst_base_parse_set_duration (parse, GST_FORMAT_TIME, last_ts, 0);
1440 /* disable further checks */
1441 parse->priv->first_frame_offset = 0;
1445 /* re-use default handler to add missing metadata as-much-as-possible */
1446 gst_base_parse_parse_frame (parse, buffer);
1447 if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
1448 GST_BUFFER_DURATION_IS_VALID (buffer)) {
1449 parse->priv->next_ts =
1450 GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer);
1452 /* we lost track, do not produce bogus time next time around
1453 * (probably means parser subclass has given up on parsing as well) */
1454 GST_DEBUG_OBJECT (parse, "no next fallback timestamp");
1455 parse->priv->next_ts = GST_CLOCK_TIME_NONE;
1458 if (parse->priv->upstream_seekable && parse->priv->exact_position &&
1459 GST_BUFFER_TIMESTAMP_IS_VALID (buffer))
1460 gst_base_parse_add_index_entry (parse, offset,
1461 GST_BUFFER_TIMESTAMP (buffer),
1462 !GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT), FALSE);
1464 /* First buffers are dropped, this means that the subclass needs more
1465 * frames to decide on the format and queues them internally */
1466 /* convert internal flow to OK and mark discont for the next buffer. */
1467 if (ret == GST_BASE_PARSE_FLOW_DROPPED) {
1468 gst_buffer_unref (buffer);
1470 } else if (ret != GST_FLOW_OK) {
1474 return gst_base_parse_push_buffer (parse, buffer);
1478 * gst_base_parse_push_buffer:
1479 * @parse: #GstBaseParse.
1480 * @buffer: #GstBuffer.
1482 * Pushes the buffer downstream, sends any pending events and
1483 * does some timestamp and segment handling.
1485 * This must be called with srcpad STREAM_LOCK held.
1487 * Returns: #GstFlowReturn
1490 gst_base_parse_push_buffer (GstBaseParse * parse, GstBuffer * buffer)
1492 GstFlowReturn ret = GST_FLOW_OK;
1493 GstClockTime last_start = GST_CLOCK_TIME_NONE;
1494 GstClockTime last_stop = GST_CLOCK_TIME_NONE;
1495 GstBaseParseClass *klass = GST_BASE_PARSE_GET_CLASS (parse);
1497 GST_LOG_OBJECT (parse,
1498 "processing buffer of size %d with ts %" GST_TIME_FORMAT
1499 ", duration %" GST_TIME_FORMAT, GST_BUFFER_SIZE (buffer),
1500 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
1501 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
1504 parse->priv->bytecount += GST_BUFFER_SIZE (buffer);
1505 if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BASE_PARSE_BUFFER_FLAG_NO_FRAME)) {
1506 parse->priv->framecount++;
1507 if (GST_BUFFER_DURATION_IS_VALID (buffer)) {
1508 parse->priv->acc_duration += GST_BUFFER_DURATION (buffer);
1511 GST_BUFFER_FLAG_UNSET (buffer, GST_BASE_PARSE_BUFFER_FLAG_NO_FRAME);
1512 if (parse->priv->update_interval &&
1513 (parse->priv->framecount % parse->priv->update_interval) == 0)
1514 gst_base_parse_update_duration (parse);
1516 if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer))
1517 last_start = last_stop = GST_BUFFER_TIMESTAMP (buffer);
1518 if (last_start != GST_CLOCK_TIME_NONE
1519 && GST_BUFFER_DURATION_IS_VALID (buffer))
1520 last_stop = last_start + GST_BUFFER_DURATION (buffer);
1522 /* should have caps by now */
1523 g_return_val_if_fail (GST_PAD_CAPS (parse->srcpad), GST_FLOW_ERROR);
1525 gst_buffer_set_caps (buffer, GST_PAD_CAPS (parse->srcpad));
1527 /* segment adjustment magic; only if we are running the whole show */
1528 if (!parse->priv->passthrough && parse->segment.rate > 0.0 &&
1529 (parse->priv->pad_mode == GST_ACTIVATE_PULL ||
1530 parse->priv->upstream_seekable)) {
1531 /* segment times are typically estimates,
1532 * actual frame data might lead subclass to different timestamps,
1533 * so override segment start from what is supplied there */
1534 if (G_UNLIKELY (parse->pending_segment && !parse->priv->exact_position &&
1535 GST_CLOCK_TIME_IS_VALID (last_start))) {
1536 gst_event_unref (parse->pending_segment);
1537 parse->segment.start =
1538 MIN ((guint64) last_start, (guint64) parse->segment.stop);
1539 GST_DEBUG_OBJECT (parse,
1540 "adjusting pending segment start to %" GST_TIME_FORMAT,
1541 GST_TIME_ARGS (parse->segment.start));
1542 parse->pending_segment =
1543 gst_event_new_new_segment (FALSE, parse->segment.rate,
1544 parse->segment.format, parse->segment.start, parse->segment.stop,
1545 parse->segment.start);
1547 /* handle gaps, e.g. non-zero start-time, in as much not handled by above */
1548 if (GST_CLOCK_TIME_IS_VALID (parse->segment.last_stop) &&
1549 GST_CLOCK_TIME_IS_VALID (last_start)) {
1550 GstClockTimeDiff diff;
1552 /* only send newsegments with increasing start times,
1553 * otherwise if these go back and forth downstream (sinks) increase
1554 * accumulated time and running_time */
1555 diff = GST_CLOCK_DIFF (parse->segment.last_stop, last_start);
1556 if (G_UNLIKELY (diff > 2 * GST_SECOND && last_start > parse->segment.start
1557 && (!GST_CLOCK_TIME_IS_VALID (parse->segment.stop) ||
1558 last_start < parse->segment.stop))) {
1559 GST_DEBUG_OBJECT (parse,
1560 "Gap of %" G_GINT64_FORMAT " ns detected in stream "
1561 "(%" GST_TIME_FORMAT " -> %" GST_TIME_FORMAT "). "
1562 "Sending updated NEWSEGMENT events", diff,
1563 GST_TIME_ARGS (parse->segment.last_stop),
1564 GST_TIME_ARGS (last_start));
1565 if (G_UNLIKELY (parse->pending_segment)) {
1566 gst_event_unref (parse->pending_segment);
1567 parse->segment.start = last_start;
1568 parse->pending_segment =
1569 gst_event_new_new_segment (FALSE, parse->segment.rate,
1570 parse->segment.format, parse->segment.start, parse->segment.stop,
1571 parse->segment.start);
1573 /* send newsegment events such that the gap is not accounted in
1574 * accum time, hence running_time */
1575 /* close ahead of gap */
1576 gst_pad_push_event (parse->srcpad,
1577 gst_event_new_new_segment (TRUE, parse->segment.rate,
1578 parse->segment.format, parse->segment.last_stop,
1579 parse->segment.last_stop, parse->segment.last_stop));
1581 gst_pad_push_event (parse->srcpad,
1582 gst_event_new_new_segment (FALSE, parse->segment.rate,
1583 parse->segment.format, last_start, parse->segment.stop,
1586 /* align segment view with downstream,
1587 * prevents double-counting accum when closing segment */
1588 gst_segment_set_newsegment (&parse->segment, FALSE,
1589 parse->segment.rate, parse->segment.format, last_start,
1590 parse->segment.stop, last_start);
1591 parse->segment.last_stop = last_start;
1596 /* and should then also be linked downstream, so safe to send some events */
1597 if (G_UNLIKELY (parse->close_segment)) {
1598 /* only set up by loop */
1599 GST_DEBUG_OBJECT (parse, "loop sending close segment");
1600 gst_pad_push_event (parse->srcpad, parse->close_segment);
1601 parse->close_segment = NULL;
1603 if (G_UNLIKELY (parse->pending_segment)) {
1604 GST_DEBUG_OBJECT (parse, "%s push pending segment",
1605 parse->priv->pad_mode == GST_ACTIVATE_PULL ? "loop" : "chain");
1606 gst_pad_push_event (parse->srcpad, parse->pending_segment);
1607 parse->pending_segment = NULL;
1609 /* have caps; check identity */
1610 gst_base_parse_check_media (parse);
1613 /* update bitrates and optionally post corresponding tags
1614 * (following newsegment) */
1615 gst_base_parse_update_bitrates (parse, buffer);
1617 if (G_UNLIKELY (parse->priv->pending_events)) {
1620 for (l = parse->priv->pending_events; l != NULL; l = l->next) {
1621 gst_pad_push_event (parse->srcpad, GST_EVENT (l->data));
1623 g_list_free (parse->priv->pending_events);
1624 parse->priv->pending_events = NULL;
1627 if (klass->pre_push_buffer)
1628 ret = klass->pre_push_buffer (parse, buffer);
1630 ret = GST_BASE_PARSE_FLOW_CLIP;
1632 parse->priv->seen_keyframe |= parse->priv->is_video &&
1633 !GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
1635 if (ret == GST_BASE_PARSE_FLOW_CLIP) {
1636 if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
1637 GST_CLOCK_TIME_IS_VALID (parse->segment.stop) &&
1638 GST_BUFFER_TIMESTAMP (buffer) >
1639 parse->segment.stop + parse->priv->lead_out_ts) {
1640 GST_LOG_OBJECT (parse, "Dropped frame, after segment");
1641 ret = GST_FLOW_UNEXPECTED;
1642 } else if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
1643 GST_BUFFER_DURATION_IS_VALID (buffer) &&
1644 GST_CLOCK_TIME_IS_VALID (parse->segment.start) &&
1645 GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer) +
1646 parse->priv->lead_in_ts < parse->segment.start) {
1647 if (parse->priv->seen_keyframe) {
1648 GST_LOG_OBJECT (parse, "Frame before segment, after keyframe");
1651 GST_LOG_OBJECT (parse, "Dropped frame, before segment");
1652 ret = GST_BASE_PARSE_FLOW_DROPPED;
1659 if (ret == GST_BASE_PARSE_FLOW_DROPPED) {
1660 GST_LOG_OBJECT (parse, "frame (%d bytes) dropped",
1661 GST_BUFFER_SIZE (buffer));
1662 gst_buffer_unref (buffer);
1664 } else if (ret == GST_FLOW_OK) {
1665 if (parse->segment.rate > 0.0) {
1666 ret = gst_pad_push (parse->srcpad, buffer);
1667 GST_LOG_OBJECT (parse, "frame (%d bytes) pushed: %s",
1668 GST_BUFFER_SIZE (buffer), gst_flow_get_name (ret));
1670 GST_LOG_OBJECT (parse, "frame (%d bytes) queued for now",
1671 GST_BUFFER_SIZE (buffer));
1672 parse->priv->buffers_queued =
1673 g_slist_prepend (parse->priv->buffers_queued, buffer);
1677 gst_buffer_unref (buffer);
1678 GST_LOG_OBJECT (parse, "frame (%d bytes) not pushed: %s",
1679 GST_BUFFER_SIZE (buffer), gst_flow_get_name (ret));
1680 /* if we are not sufficiently in control, let upstream decide on EOS */
1681 if (ret == GST_FLOW_UNEXPECTED &&
1682 (parse->priv->passthrough ||
1683 (parse->priv->pad_mode == GST_ACTIVATE_PUSH &&
1684 !parse->priv->upstream_seekable)))
1688 /* Update current running segment position */
1689 if (ret == GST_FLOW_OK && last_stop != GST_CLOCK_TIME_NONE &&
1690 parse->segment.last_stop < last_stop)
1691 gst_segment_set_last_stop (&parse->segment, GST_FORMAT_TIME, last_stop);
1698 * gst_base_parse_drain:
1699 * @parse: #GstBaseParse.
1701 * Drains the adapter until it is empty. It decreases the min_frame_size to
1702 * match the current adapter size and calls chain method until the adapter
1703 * is emptied or chain returns with error.
1706 gst_base_parse_drain (GstBaseParse * parse)
1710 GST_DEBUG_OBJECT (parse, "draining");
1711 parse->priv->drain = TRUE;
1714 avail = gst_adapter_available (parse->adapter);
1718 if (gst_base_parse_chain (parse->sinkpad, NULL) != GST_FLOW_OK) {
1722 /* nothing changed, maybe due to truncated frame; break infinite loop */
1723 if (avail == gst_adapter_available (parse->adapter)) {
1724 GST_DEBUG_OBJECT (parse, "no change during draining; flushing");
1725 gst_adapter_clear (parse->adapter);
1729 parse->priv->drain = FALSE;
1733 * gst_base_parse_process_fragment:
1734 * @parse: #GstBaseParse.
1736 * Processes a reverse playback (forward) fragment:
1737 * - append head of last fragment that was skipped to current fragment data
1738 * - drain the resulting current fragment data (i.e. repeated chain)
1739 * - add time/duration (if needed) to frames queued by chain
1740 * - push queued data
1742 static GstFlowReturn
1743 gst_base_parse_process_fragment (GstBaseParse * parse, gboolean push_only)
1746 GstFlowReturn ret = GST_FLOW_OK;
1747 GSList *send = NULL;
1753 parse->priv->buffers_pending = g_slist_reverse (parse->priv->buffers_pending);
1754 while (parse->priv->buffers_pending) {
1755 buf = GST_BUFFER_CAST (parse->priv->buffers_pending->data);
1756 GST_LOG_OBJECT (parse, "adding pending buffer (size %d)",
1757 GST_BUFFER_SIZE (buf));
1758 gst_adapter_push (parse->adapter, buf);
1759 parse->priv->buffers_pending =
1760 g_slist_delete_link (parse->priv->buffers_pending,
1761 parse->priv->buffers_pending);
1764 /* invalidate so no fall-back timestamping is performed;
1765 * ok if taken from subclass or upstream */
1766 parse->priv->next_ts = GST_CLOCK_TIME_NONE;
1767 /* prevent it hanging around stop all the time */
1768 parse->segment.last_stop = GST_CLOCK_TIME_NONE;
1770 parse->priv->discont = TRUE;
1772 /* chain looks for frames and queues resulting ones (in stead of pushing) */
1773 /* initial skipped data is added to buffers_pending */
1774 gst_base_parse_drain (parse);
1777 /* add metadata (if needed to queued buffers */
1778 GST_LOG_OBJECT (parse, "last timestamp: %" GST_TIME_FORMAT,
1779 GST_TIME_ARGS (parse->priv->last_ts));
1780 while (parse->priv->buffers_queued) {
1781 buf = GST_BUFFER_CAST (parse->priv->buffers_queued->data);
1783 /* no touching if upstream or parsing provided time */
1784 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
1785 GST_LOG_OBJECT (parse, "buffer has time %" GST_TIME_FORMAT,
1786 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
1787 } else if (GST_CLOCK_TIME_IS_VALID (parse->priv->last_ts) &&
1788 GST_BUFFER_DURATION_IS_VALID (buf)) {
1789 if (G_LIKELY (GST_BUFFER_DURATION (buf) <= parse->priv->last_ts))
1790 parse->priv->last_ts -= GST_BUFFER_DURATION (buf);
1792 parse->priv->last_ts = 0;
1793 GST_BUFFER_TIMESTAMP (buf) = parse->priv->last_ts;
1794 GST_LOG_OBJECT (parse, "applied time %" GST_TIME_FORMAT,
1795 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
1797 /* no idea, very bad */
1798 GST_WARNING_OBJECT (parse, "could not determine time for buffer");
1801 /* reverse order for ascending sending */
1802 send = g_slist_prepend (send, buf);
1803 parse->priv->buffers_queued =
1804 g_slist_delete_link (parse->priv->buffers_queued,
1805 parse->priv->buffers_queued);
1810 buf = GST_BUFFER_CAST (send->data);
1811 GST_LOG_OBJECT (parse, "pushing buffer %p, timestamp %"
1812 GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT
1813 ", offset %" G_GINT64_FORMAT, buf,
1814 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
1815 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)), GST_BUFFER_OFFSET (buf));
1817 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (parse->priv->last_ts)))
1818 parse->priv->last_ts = GST_BUFFER_TIMESTAMP (buf);
1820 /* iterate output queue an push downstream */
1821 ret = gst_pad_push (parse->srcpad, buf);
1822 send = g_slist_delete_link (send, send);
1824 /* clear any leftover if error */
1825 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
1827 buf = GST_BUFFER_CAST (send->data);
1828 gst_buffer_unref (buf);
1829 send = g_slist_delete_link (send, send);
1834 /* any trailing unused no longer usable (ideally none) */
1835 if (G_UNLIKELY (gst_adapter_available (parse->adapter))) {
1836 GST_DEBUG_OBJECT (parse, "discarding %d trailing bytes",
1837 gst_adapter_available (parse->adapter));
1838 gst_adapter_clear (parse->adapter);
1844 /* small helper that checks whether we have been trying to resync too long */
1845 static inline GstFlowReturn
1846 gst_base_parse_check_sync (GstBaseParse * parse)
1848 if (G_UNLIKELY (parse->priv->discont &&
1849 parse->priv->offset - parse->priv->sync_offset > 2 * 1024 * 1024)) {
1850 GST_ELEMENT_ERROR (parse, STREAM, DECODE,
1851 ("Failed to parse stream"), (NULL));
1852 return GST_FLOW_ERROR;
1860 * gst_base_parse_chain:
1862 * @buffer: #GstBuffer.
1864 * Returns: #GstFlowReturn.
1866 static GstFlowReturn
1867 gst_base_parse_chain (GstPad * pad, GstBuffer * buffer)
1869 GstBaseParseClass *bclass;
1870 GstBaseParse *parse;
1871 GstFlowReturn ret = GST_FLOW_OK;
1872 GstBuffer *outbuf = NULL;
1873 GstBuffer *tmpbuf = NULL;
1878 GstClockTime timestamp;
1880 parse = GST_BASE_PARSE (GST_OBJECT_PARENT (pad));
1881 bclass = GST_BASE_PARSE_GET_CLASS (parse);
1883 if (G_LIKELY (buffer)) {
1884 GST_LOG_OBJECT (parse, "buffer size: %d, offset = %" G_GINT64_FORMAT,
1885 GST_BUFFER_SIZE (buffer), GST_BUFFER_OFFSET (buffer));
1886 if (G_UNLIKELY (parse->priv->passthrough)) {
1887 buffer = gst_buffer_make_metadata_writable (buffer);
1888 return gst_base_parse_push_buffer (parse, buffer);
1890 /* upstream feeding us in reverse playback;
1891 * gather each fragment, then process it in single run */
1892 if (parse->segment.rate < 0.0) {
1893 if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT))) {
1894 GST_DEBUG_OBJECT (parse, "buffer starts new reverse playback fragment");
1895 ret = gst_base_parse_process_fragment (parse, FALSE);
1897 gst_adapter_push (parse->adapter, buffer);
1900 gst_adapter_push (parse->adapter, buffer);
1903 /* Parse and push as many frames as possible */
1904 /* Stop either when adapter is empty or we are flushing */
1905 while (!parse->priv->flushing) {
1906 tmpbuf = gst_buffer_new ();
1908 /* Synchronization loop */
1910 min_size = parse->priv->min_frame_size;
1912 if (G_UNLIKELY (parse->priv->drain)) {
1913 min_size = gst_adapter_available (parse->adapter);
1914 GST_DEBUG_OBJECT (parse, "draining, data left: %d", min_size);
1915 if (G_UNLIKELY (!min_size)) {
1916 gst_buffer_unref (tmpbuf);
1921 /* Collect at least min_frame_size bytes */
1922 if (gst_adapter_available (parse->adapter) < min_size) {
1923 GST_DEBUG_OBJECT (parse, "not enough data available (only %d bytes)",
1924 gst_adapter_available (parse->adapter));
1925 gst_buffer_unref (tmpbuf);
1929 data = gst_adapter_peek (parse->adapter, min_size);
1930 GST_BUFFER_DATA (tmpbuf) = (guint8 *) data;
1931 GST_BUFFER_SIZE (tmpbuf) = min_size;
1932 GST_BUFFER_OFFSET (tmpbuf) = parse->priv->offset;
1933 GST_BUFFER_FLAG_SET (tmpbuf, GST_MINI_OBJECT_FLAG_READONLY);
1935 if (parse->priv->discont) {
1936 GST_DEBUG_OBJECT (parse, "marking DISCONT");
1937 GST_BUFFER_FLAG_SET (tmpbuf, GST_BUFFER_FLAG_DISCONT);
1941 if (bclass->check_valid_frame (parse, tmpbuf, &fsize, &skip)) {
1942 if (gst_adapter_available (parse->adapter) < fsize) {
1943 GST_DEBUG_OBJECT (parse,
1944 "found valid frame but not enough data available (only %d bytes)",
1945 gst_adapter_available (parse->adapter));
1946 gst_buffer_unref (tmpbuf);
1952 /* subclass didn't touch this value. By default we skip 1 byte */
1956 GST_LOG_OBJECT (parse, "finding sync, skipping %d bytes", skip);
1957 if (parse->segment.rate < 0.0 && !parse->priv->buffers_queued) {
1958 /* reverse playback, and no frames found yet, so we are skipping
1959 * the leading part of a fragment, which may form the tail of
1960 * fragment coming later, hopefully subclass skips efficiently ... */
1961 timestamp = gst_adapter_prev_timestamp (parse->adapter, NULL);
1962 outbuf = gst_adapter_take_buffer (parse->adapter, skip);
1963 outbuf = gst_buffer_make_metadata_writable (outbuf);
1964 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
1965 parse->priv->buffers_pending =
1966 g_slist_prepend (parse->priv->buffers_pending, outbuf);
1969 gst_adapter_flush (parse->adapter, skip);
1971 parse->priv->offset += skip;
1972 if (!parse->priv->discont)
1973 parse->priv->sync_offset = parse->priv->offset;
1974 parse->priv->discont = TRUE;
1976 /* There is a possibility that subclass set the skip value to zero.
1977 This means that it has probably found a frame but wants to ask
1978 more data (by increasing the min_size) to be sure of this. */
1979 if ((ret = gst_base_parse_check_sync (parse)) != GST_FLOW_OK) {
1980 gst_buffer_unref (tmpbuf);
1984 gst_buffer_unref (tmpbuf);
1988 /* Subclass found the sync, but still wants to skip some data */
1989 GST_LOG_OBJECT (parse, "skipping %d bytes", skip);
1990 gst_adapter_flush (parse->adapter, skip);
1991 parse->priv->offset += skip;
1994 /* Grab lock to prevent a race with FLUSH_START handler */
1995 GST_PAD_STREAM_LOCK (parse->srcpad);
1997 /* FLUSH_START event causes the "flushing" flag to be set. In this
1998 * case we can leave the frame pushing loop */
1999 if (parse->priv->flushing) {
2000 GST_PAD_STREAM_UNLOCK (parse->srcpad);
2004 /* FIXME: Would it be more efficient to make a subbuffer instead? */
2005 outbuf = gst_adapter_take_buffer (parse->adapter, fsize);
2006 outbuf = gst_buffer_make_metadata_writable (outbuf);
2008 /* Subclass may want to know the data offset */
2009 GST_BUFFER_OFFSET (outbuf) = parse->priv->offset;
2010 parse->priv->offset += fsize;
2011 GST_BUFFER_TIMESTAMP (outbuf) = GST_CLOCK_TIME_NONE;
2013 /* move along with upstream timestamp (if any),
2014 * but interpolate in between */
2015 timestamp = gst_adapter_prev_timestamp (parse->adapter, NULL);
2016 if (GST_CLOCK_TIME_IS_VALID (timestamp) &&
2017 (parse->priv->prev_ts != timestamp)) {
2018 parse->priv->prev_ts = parse->priv->next_ts = timestamp;
2021 ret = gst_base_parse_handle_and_push_buffer (parse, bclass, outbuf);
2022 GST_PAD_STREAM_UNLOCK (parse->srcpad);
2024 if (ret != GST_FLOW_OK) {
2025 GST_LOG_OBJECT (parse, "push returned %d", ret);
2031 GST_LOG_OBJECT (parse, "chain leaving");
2035 /* pull @size bytes at current offset,
2036 * i.e. at least try to and possibly return a shorter buffer if near the end */
2037 static GstFlowReturn
2038 gst_base_parse_pull_range (GstBaseParse * parse, guint size,
2039 GstBuffer ** buffer)
2041 GstFlowReturn ret = GST_FLOW_OK;
2043 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
2045 /* Caching here actually makes much less difference than one would expect.
2046 * We do it mainly to avoid pulling buffers of 1 byte all the time */
2047 if (parse->priv->cache) {
2048 gint64 cache_offset = GST_BUFFER_OFFSET (parse->priv->cache);
2049 gint cache_size = GST_BUFFER_SIZE (parse->priv->cache);
2051 if (cache_offset <= parse->priv->offset &&
2052 (parse->priv->offset + size) <= (cache_offset + cache_size)) {
2053 *buffer = gst_buffer_create_sub (parse->priv->cache,
2054 parse->priv->offset - cache_offset, size);
2055 GST_BUFFER_OFFSET (*buffer) = parse->priv->offset;
2058 /* not enough data in the cache, free cache and get a new one */
2059 gst_buffer_unref (parse->priv->cache);
2060 parse->priv->cache = NULL;
2063 /* refill the cache */
2065 gst_pad_pull_range (parse->sinkpad, parse->priv->offset, MAX (size,
2066 64 * 1024), &parse->priv->cache);
2067 if (ret != GST_FLOW_OK) {
2068 parse->priv->cache = NULL;
2072 if (GST_BUFFER_SIZE (parse->priv->cache) >= size) {
2073 *buffer = gst_buffer_create_sub (parse->priv->cache, 0, size);
2074 GST_BUFFER_OFFSET (*buffer) = parse->priv->offset;
2078 /* Not possible to get enough data, try a last time with
2079 * requesting exactly the size we need */
2080 gst_buffer_unref (parse->priv->cache);
2081 parse->priv->cache = NULL;
2083 ret = gst_pad_pull_range (parse->sinkpad, parse->priv->offset, size,
2084 &parse->priv->cache);
2086 if (ret != GST_FLOW_OK) {
2087 GST_DEBUG_OBJECT (parse, "pull_range returned %d", ret);
2092 if (GST_BUFFER_SIZE (parse->priv->cache) < size) {
2093 GST_DEBUG_OBJECT (parse, "Returning short buffer at offset %"
2094 G_GUINT64_FORMAT ": wanted %u bytes, got %u bytes", parse->priv->offset,
2095 size, GST_BUFFER_SIZE (parse->priv->cache));
2097 *buffer = parse->priv->cache;
2098 parse->priv->cache = NULL;
2103 *buffer = gst_buffer_create_sub (parse->priv->cache, 0, size);
2104 GST_BUFFER_OFFSET (*buffer) = parse->priv->offset;
2109 static GstFlowReturn
2110 gst_base_parse_handle_previous_fragment (GstBaseParse * parse)
2113 GstClockTime ts = 0;
2117 GST_DEBUG_OBJECT (parse, "fragment ended; last_ts = %" GST_TIME_FORMAT
2118 ", last_offset = %" G_GINT64_FORMAT, GST_TIME_ARGS (parse->priv->last_ts),
2119 parse->priv->last_offset);
2121 if (!parse->priv->last_offset || parse->priv->last_ts <= parse->segment.start) {
2122 GST_DEBUG_OBJECT (parse, "past start of segment %" GST_TIME_FORMAT,
2123 GST_TIME_ARGS (parse->segment.start));
2124 ret = GST_FLOW_UNEXPECTED;
2128 /* last fragment started at last_offset / last_ts;
2129 * seek back 10s capped at 1MB */
2130 if (parse->priv->last_ts >= 10 * GST_SECOND)
2131 ts = parse->priv->last_ts - 10 * GST_SECOND;
2132 /* if we are exact now, we will be more so going backwards */
2133 if (parse->priv->exact_position) {
2134 offset = gst_base_parse_find_offset (parse, ts, TRUE, NULL);
2136 GstFormat dstformat = GST_FORMAT_BYTES;
2138 if (!gst_pad_query_convert (parse->srcpad, GST_FORMAT_TIME, ts,
2139 &dstformat, &offset)) {
2140 GST_DEBUG_OBJECT (parse, "conversion failed, only BYTE based");
2143 offset = CLAMP (offset, parse->priv->last_offset - 1024 * 1024,
2144 parse->priv->last_offset - 1024);
2145 offset = MAX (0, offset);
2147 GST_DEBUG_OBJECT (parse, "next fragment from offset %" G_GINT64_FORMAT,
2149 parse->priv->offset = offset;
2151 ret = gst_base_parse_pull_range (parse, parse->priv->last_offset - offset,
2153 if (ret != GST_FLOW_OK)
2156 gst_adapter_push (parse->adapter, buffer);
2157 ret = gst_base_parse_process_fragment (parse, FALSE);
2158 if (ret != GST_FLOW_OK)
2166 * pull and scan for next frame starting from current offset
2167 * ajusts sync, drain and offset going along */
2168 static GstFlowReturn
2169 gst_base_parse_scan_frame (GstBaseParse * parse, GstBaseParseClass * klass,
2170 GstBuffer ** buf, gboolean full)
2172 GstBuffer *buffer, *outbuf;
2173 GstFlowReturn ret = GST_FLOW_OK;
2174 guint fsize = 0, min_size;
2177 g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
2181 min_size = parse->priv->min_frame_size;
2183 ret = gst_base_parse_pull_range (parse, min_size, &buffer);
2184 if (ret != GST_FLOW_OK)
2187 if (parse->priv->discont) {
2188 GST_DEBUG_OBJECT (parse, "marking DISCONT");
2189 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
2192 /* if we got a short read, inform subclass we are draining leftover
2193 * and no more is to be expected */
2194 if (GST_BUFFER_SIZE (buffer) < min_size)
2195 parse->priv->drain = TRUE;
2198 if (klass->check_valid_frame (parse, buffer, &fsize, &skip)) {
2199 parse->priv->drain = FALSE;
2202 parse->priv->drain = FALSE;
2206 GST_LOG_OBJECT (parse, "finding sync, skipping %d bytes", skip);
2207 if (full && parse->segment.rate < 0.0 && !parse->priv->buffers_queued) {
2208 /* reverse playback, and no frames found yet, so we are skipping
2209 * the leading part of a fragment, which may form the tail of
2210 * fragment coming later, hopefully subclass skips efficiently ... */
2211 outbuf = gst_buffer_create_sub (buffer, 0, skip);
2212 parse->priv->buffers_pending =
2213 g_slist_prepend (parse->priv->buffers_pending, outbuf);
2216 parse->priv->offset += skip;
2217 if (!parse->priv->discont)
2218 parse->priv->sync_offset = parse->priv->offset;
2219 parse->priv->discont = TRUE;
2221 /* skip == 0 should imply subclass set min_size to need more data ... */
2222 GST_DEBUG_OBJECT (parse, "finding sync...");
2223 gst_buffer_unref (buffer);
2224 if ((ret = gst_base_parse_check_sync (parse)) != GST_FLOW_OK) {
2229 if (fsize <= GST_BUFFER_SIZE (buffer)) {
2230 outbuf = gst_buffer_create_sub (buffer, 0, fsize);
2231 GST_BUFFER_OFFSET (outbuf) = GST_BUFFER_OFFSET (buffer);
2232 GST_BUFFER_TIMESTAMP (outbuf) = GST_CLOCK_TIME_NONE;
2233 gst_buffer_unref (buffer);
2235 gst_buffer_unref (buffer);
2236 ret = gst_base_parse_pull_range (parse, fsize, &outbuf);
2237 if (ret != GST_FLOW_OK)
2239 if (GST_BUFFER_SIZE (outbuf) < fsize) {
2240 gst_buffer_unref (outbuf);
2241 ret = GST_FLOW_UNEXPECTED;
2245 parse->priv->offset += fsize;
2247 /* Does the subclass want to skip too? */
2249 parse->priv->offset += skip;
2258 * gst_base_parse_loop:
2261 * Loop that is used in pull mode to retrieve data from upstream.
2264 gst_base_parse_loop (GstPad * pad)
2266 GstBaseParse *parse;
2267 GstBaseParseClass *klass;
2269 GstFlowReturn ret = GST_FLOW_OK;
2271 parse = GST_BASE_PARSE (gst_pad_get_parent (pad));
2272 klass = GST_BASE_PARSE_GET_CLASS (parse);
2274 /* reverse playback:
2275 * first fragment (closest to stop time) is handled normally below,
2276 * then we pull in fragments going backwards */
2277 if (parse->segment.rate < 0.0) {
2278 if (GST_CLOCK_TIME_IS_VALID (parse->priv->last_ts)) {
2279 ret = gst_base_parse_handle_previous_fragment (parse);
2284 ret = gst_base_parse_scan_frame (parse, klass, &outbuf, TRUE);
2285 if (ret != GST_FLOW_OK)
2288 /* This always unrefs the outbuf, even if error occurs */
2289 ret = gst_base_parse_handle_and_push_buffer (parse, klass, outbuf);
2291 /* eat expected eos signalling past segment in reverse playback */
2292 if (parse->segment.rate < 0.0 && ret == GST_FLOW_UNEXPECTED &&
2293 parse->segment.last_stop >= parse->segment.stop) {
2294 GST_DEBUG_OBJECT (parse, "downstream has reached end of segment");
2295 /* push what was accumulated during loop run */
2296 gst_base_parse_process_fragment (parse, TRUE);
2301 if (ret == GST_FLOW_UNEXPECTED)
2303 else if (ret != GST_FLOW_OK)
2306 gst_object_unref (parse);
2312 ret = GST_FLOW_UNEXPECTED;
2313 GST_DEBUG_OBJECT (parse, "eos");
2318 gboolean push_eos = FALSE;
2320 GST_DEBUG_OBJECT (parse, "pausing task, reason %s",
2321 gst_flow_get_name (ret));
2322 gst_pad_pause_task (parse->sinkpad);
2324 if (ret == GST_FLOW_UNEXPECTED) {
2325 /* handle end-of-stream/segment */
2326 if (parse->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2329 if ((stop = parse->segment.stop) == -1)
2330 stop = parse->segment.duration;
2332 GST_DEBUG_OBJECT (parse, "sending segment_done");
2334 gst_element_post_message
2335 (GST_ELEMENT_CAST (parse),
2336 gst_message_new_segment_done (GST_OBJECT_CAST (parse),
2337 GST_FORMAT_TIME, stop));
2339 /* If we STILL have zero frames processed, fire an error */
2340 if (parse->priv->framecount == 0) {
2341 GST_ELEMENT_ERROR (parse, STREAM, WRONG_TYPE,
2342 ("No valid frames found before end of stream"), (NULL));
2346 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_UNEXPECTED) {
2347 /* for fatal errors we post an error message, wrong-state is
2348 * not fatal because it happens due to flushes and only means
2349 * that we should stop now. */
2350 GST_ELEMENT_ERROR (parse, STREAM, FAILED, (NULL),
2351 ("streaming stopped, reason %s", gst_flow_get_name (ret)));
2355 /* newsegment before eos */
2356 if (parse->pending_segment) {
2357 gst_pad_push_event (parse->srcpad, parse->pending_segment);
2358 parse->pending_segment = NULL;
2360 gst_pad_push_event (parse->srcpad, gst_event_new_eos ());
2362 gst_object_unref (parse);
2368 * gst_base_parse_sink_activate:
2369 * @sinkpad: #GstPad to be activated.
2371 * Returns: TRUE if activation succeeded.
2374 gst_base_parse_sink_activate (GstPad * sinkpad)
2376 GstBaseParse *parse;
2377 gboolean result = TRUE;
2379 parse = GST_BASE_PARSE (gst_pad_get_parent (sinkpad));
2381 GST_DEBUG_OBJECT (parse, "sink activate");
2383 if (gst_pad_check_pull_range (sinkpad)) {
2384 GST_DEBUG_OBJECT (parse, "trying to activate in pull mode");
2385 result = gst_pad_activate_pull (sinkpad, TRUE);
2387 GST_DEBUG_OBJECT (parse, "trying to activate in push mode");
2388 result = gst_pad_activate_push (sinkpad, TRUE);
2391 GST_DEBUG_OBJECT (parse, "sink activate return %d", result);
2392 gst_object_unref (parse);
2398 * gst_base_parse_activate:
2399 * @parse: #GstBaseParse.
2400 * @active: TRUE if element will be activated, FALSE if deactivated.
2402 * Returns: TRUE if the operation succeeded.
2405 gst_base_parse_activate (GstBaseParse * parse, gboolean active)
2407 GstBaseParseClass *klass;
2408 gboolean result = FALSE;
2410 GST_DEBUG_OBJECT (parse, "activate");
2412 klass = GST_BASE_PARSE_GET_CLASS (parse);
2415 if (parse->priv->pad_mode == GST_ACTIVATE_NONE && klass->start)
2416 result = klass->start (parse);
2418 /* We must make sure streaming has finished before resetting things
2419 * and calling the ::stop vfunc */
2420 GST_PAD_STREAM_LOCK (parse->sinkpad);
2421 GST_PAD_STREAM_UNLOCK (parse->sinkpad);
2423 if (parse->priv->pad_mode != GST_ACTIVATE_NONE && klass->stop)
2424 result = klass->stop (parse);
2426 parse->priv->pad_mode = GST_ACTIVATE_NONE;
2428 GST_DEBUG_OBJECT (parse, "activate: %d", result);
2434 * gst_base_parse_sink_activate_push:
2435 * @pad: #GstPad to be (de)activated.
2436 * @active: TRUE when activating, FALSE when deactivating.
2438 * Returns: TRUE if (de)activation succeeded.
2441 gst_base_parse_sink_activate_push (GstPad * pad, gboolean active)
2443 gboolean result = TRUE;
2444 GstBaseParse *parse;
2446 parse = GST_BASE_PARSE (gst_pad_get_parent (pad));
2448 GST_DEBUG_OBJECT (parse, "sink activate push");
2450 result = gst_base_parse_activate (parse, active);
2453 parse->priv->pad_mode = active ? GST_ACTIVATE_PUSH : GST_ACTIVATE_NONE;
2455 GST_DEBUG_OBJECT (parse, "sink activate push: %d", result);
2457 gst_object_unref (parse);
2463 * gst_base_parse_sink_activate_pull:
2464 * @sinkpad: #GstPad to be (de)activated.
2465 * @active: TRUE when activating, FALSE when deactivating.
2467 * Returns: TRUE if (de)activation succeeded.
2470 gst_base_parse_sink_activate_pull (GstPad * sinkpad, gboolean active)
2472 gboolean result = FALSE;
2473 GstBaseParse *parse;
2475 parse = GST_BASE_PARSE (gst_pad_get_parent (sinkpad));
2477 GST_DEBUG_OBJECT (parse, "activate pull");
2479 result = gst_base_parse_activate (parse, active);
2483 parse->pending_segment = gst_event_new_new_segment (FALSE,
2484 parse->segment.rate, parse->segment.format,
2485 parse->segment.start, parse->segment.stop, parse->segment.last_stop);
2486 result &= gst_pad_start_task (sinkpad,
2487 (GstTaskFunction) gst_base_parse_loop, sinkpad);
2489 result &= gst_pad_stop_task (sinkpad);
2494 parse->priv->pad_mode = active ? GST_ACTIVATE_PULL : GST_ACTIVATE_NONE;
2496 GST_DEBUG_OBJECT (parse, "sink activate pull: %d", result);
2498 gst_object_unref (parse);
2504 * gst_base_parse_set_duration:
2505 * @parse: #GstBaseParse.
2507 * @duration: duration value.
2509 * Sets the duration of the currently playing media. Subclass can use this
2510 * when it is able to determine duration and/or notices a change in the media
2511 * duration. Alternatively, if @interval is non-zero (default), then stream
2512 * duration is determined based on estimated bitrate, and updated every @interval
2515 gst_base_parse_set_duration (GstBaseParse * parse,
2516 GstFormat fmt, gint64 duration, gint interval)
2518 g_return_if_fail (parse != NULL);
2520 if (parse->priv->upstream_has_duration) {
2521 GST_DEBUG_OBJECT (parse, "using upstream duration; discarding update");
2525 if (duration != parse->priv->duration) {
2528 m = gst_message_new_duration (GST_OBJECT (parse), fmt, duration);
2529 gst_element_post_message (GST_ELEMENT (parse), m);
2531 /* TODO: what about duration tag? */
2533 parse->priv->duration = duration;
2534 parse->priv->duration_fmt = fmt;
2535 GST_DEBUG_OBJECT (parse, "set duration: %" G_GINT64_FORMAT, duration);
2536 if (fmt == GST_FORMAT_TIME && GST_CLOCK_TIME_IS_VALID (duration)) {
2537 if (interval != 0) {
2538 GST_DEBUG_OBJECT (parse, "valid duration provided, disabling estimate");
2542 GST_DEBUG_OBJECT (parse, "set update interval: %d", interval);
2543 parse->priv->update_interval = interval;
2549 * gst_base_parse_set_seek:
2550 * @parse: #GstBaseParse.
2551 * @seek: #GstBaseParseSeekable.
2552 * @abitrate: average bitrate.
2554 * Sets whether and how the media is seekable (in time).
2555 * Also optionally provides average bitrate detected in media (if non-zero),
2556 * e.g. based on metadata, as it will be posted to the application.
2558 * By default, announced average bitrate is estimated, and seekability is assumed
2559 * possible based on estimated bitrate.
2562 gst_base_parse_set_seek (GstBaseParse * parse,
2563 GstBaseParseSeekable seek, guint bitrate)
2565 parse->priv->seekable = seek;
2566 parse->priv->bitrate = bitrate;
2567 GST_DEBUG_OBJECT (parse, "seek %d, bitrate %d", seek, bitrate);
2572 * gst_base_parse_set_min_frame_size:
2573 * @parse: #GstBaseParse.
2574 * @min_size: Minimum size of the data that this base class should give to
2577 * Subclass can use this function to tell the base class that it needs to
2578 * give at least #min_size buffers.
2581 gst_base_parse_set_min_frame_size (GstBaseParse * parse, guint min_size)
2583 g_return_if_fail (parse != NULL);
2585 parse->priv->min_frame_size = min_size;
2586 GST_LOG_OBJECT (parse, "set frame_min_size: %d", min_size);
2590 * gst_base_parse_set_passthrough:
2591 * @parse: the #GstBaseParse to set
2592 * @passthrough: boolean indicating passthrough mode.
2594 * Set passthrough mode for this parser. If operating in passthrough,
2595 * incoming buffers are pushed through unmodified.
2598 gst_base_parse_set_passthrough (GstBaseParse * parse, gboolean passthrough)
2600 g_return_if_fail (parse != NULL);
2602 parse->priv->passthrough = passthrough;
2603 GST_LOG_OBJECT (parse, "set passthrough: %d", passthrough);
2607 * gst_base_parse_set_frame_props:
2608 * @parse: the #GstBaseParse to set
2609 * @fps_num: frames per second (numerator).
2610 * @fps_den: frames per second (denominator).
2611 * @lead_in: frames needed before a segment for subsequent decode
2612 * @lead_out: frames needed after a segment
2614 * If frames per second is configured, parser can take care of buffer duration
2615 * and timestamping. When performing segment clipping, or seeking to a specific
2616 * location, a corresponding decoder might need an initial @lead_in and a
2617 * following @lead_out number of frames to ensure the desired segment is
2618 * entirely filled upon decoding.
2621 gst_base_parse_set_frame_props (GstBaseParse * parse, guint fps_num,
2622 guint fps_den, guint lead_in, guint lead_out)
2624 g_return_if_fail (parse != NULL);
2626 parse->priv->fps_num = fps_num;
2627 parse->priv->fps_den = fps_den;
2628 if (!fps_num || !fps_den) {
2629 GST_DEBUG_OBJECT (parse, "invalid fps (%d/%d), ignoring parameters",
2631 fps_num = fps_den = 0;
2632 parse->priv->frame_duration = GST_CLOCK_TIME_NONE;
2633 parse->priv->lead_in = parse->priv->lead_out = 0;
2634 parse->priv->lead_in_ts = parse->priv->lead_out_ts = 0;
2636 parse->priv->frame_duration =
2637 gst_util_uint64_scale (GST_SECOND, fps_den, fps_num);
2638 parse->priv->lead_in = lead_in;
2639 parse->priv->lead_out = lead_out;
2640 parse->priv->lead_in_ts =
2641 gst_util_uint64_scale (GST_SECOND, fps_den * lead_in, fps_num);
2642 parse->priv->lead_out_ts =
2643 gst_util_uint64_scale (GST_SECOND, fps_den * lead_out, fps_num);
2645 GST_LOG_OBJECT (parse, "set fps: %d/%d => duration: %" G_GINT64_FORMAT " ms",
2646 fps_num, fps_den, parse->priv->frame_duration / GST_MSECOND);
2647 GST_LOG_OBJECT (parse, "set lead in: %d frames = %" G_GUINT64_FORMAT " ms, "
2648 "lead out: %d frames = %" G_GUINT64_FORMAT " ms",
2649 lead_in, parse->priv->lead_in_ts / GST_MSECOND,
2650 lead_out, parse->priv->lead_out_ts / GST_MSECOND);
2654 * gst_base_parse_get_sync:
2655 * @parse: the #GstBaseParse to query
2657 * Returns: TRUE if parser is considered 'in sync'. That is, frames have been
2658 * continuously successfully parsed and pushed.
2661 gst_base_parse_get_sync (GstBaseParse * parse)
2665 g_return_val_if_fail (parse != NULL, FALSE);
2667 /* losing sync is pretty much a discont (and vice versa), no ? */
2668 ret = !parse->priv->discont;
2670 GST_DEBUG_OBJECT (parse, "sync: %d", ret);
2675 * gst_base_parse_get_drain:
2676 * @parse: the #GstBaseParse to query
2678 * Returns: TRUE if parser is currently 'draining'. That is, leftover data
2679 * (e.g. in FLUSH or EOS situation) is being parsed.
2682 gst_base_parse_get_drain (GstBaseParse * parse)
2686 g_return_val_if_fail (parse != NULL, FALSE);
2688 /* losing sync is pretty much a discont (and vice versa), no ? */
2689 ret = parse->priv->drain;
2691 GST_DEBUG_OBJECT (parse, "drain: %d", ret);
2696 gst_base_parse_get_duration (GstBaseParse * parse, GstFormat format,
2697 GstClockTime * duration)
2699 gboolean res = FALSE;
2701 g_return_val_if_fail (duration != NULL, FALSE);
2703 *duration = GST_CLOCK_TIME_NONE;
2704 if (parse->priv->duration != -1 && format == parse->priv->duration_fmt) {
2705 GST_LOG_OBJECT (parse, "using provided duration");
2706 *duration = parse->priv->duration;
2708 } else if (parse->priv->duration != -1) {
2709 GST_LOG_OBJECT (parse, "converting provided duration");
2710 res = gst_base_parse_convert (parse, parse->priv->duration_fmt,
2711 parse->priv->duration, format, (gint64 *) duration);
2712 } else if (format == GST_FORMAT_TIME && parse->priv->estimated_duration != -1) {
2713 GST_LOG_OBJECT (parse, "using estimated duration");
2714 *duration = parse->priv->estimated_duration;
2718 GST_LOG_OBJECT (parse, "res: %d, duration %" GST_TIME_FORMAT, res,
2719 GST_TIME_ARGS (*duration));
2724 * gst_base_parse_get_querytypes:
2727 * Returns: A table of #GstQueryType items describing supported query types.
2729 static const GstQueryType *
2730 gst_base_parse_get_querytypes (GstPad * pad)
2732 static const GstQueryType list[] = {
2746 * gst_base_parse_query:
2748 * @query: #GstQuery.
2750 * Returns: TRUE on success.
2753 gst_base_parse_query (GstPad * pad, GstQuery * query)
2755 GstBaseParse *parse;
2756 gboolean res = FALSE;
2758 parse = GST_BASE_PARSE (GST_PAD_PARENT (pad));
2760 GST_LOG_OBJECT (parse, "handling query: %" GST_PTR_FORMAT, query);
2762 switch (GST_QUERY_TYPE (query)) {
2763 case GST_QUERY_POSITION:
2768 GST_DEBUG_OBJECT (parse, "position query");
2769 gst_query_parse_position (query, &format, NULL);
2771 GST_OBJECT_LOCK (parse);
2772 if (format == GST_FORMAT_BYTES) {
2773 dest_value = parse->priv->offset;
2775 } else if (format == parse->segment.format &&
2776 GST_CLOCK_TIME_IS_VALID (parse->segment.last_stop)) {
2777 dest_value = parse->segment.last_stop;
2780 GST_OBJECT_UNLOCK (parse);
2783 gst_query_set_position (query, format, dest_value);
2785 res = gst_pad_query_default (pad, query);
2787 /* no precise result, upstream no idea either, then best estimate */
2788 /* priv->offset is updated in both PUSH/PULL modes */
2789 res = gst_base_parse_convert (parse,
2790 GST_FORMAT_BYTES, parse->priv->offset, format, &dest_value);
2795 case GST_QUERY_DURATION:
2798 GstClockTime duration;
2800 GST_DEBUG_OBJECT (parse, "duration query");
2801 gst_query_parse_duration (query, &format, NULL);
2803 /* consult upstream */
2804 res = gst_pad_query_default (pad, query);
2806 /* otherwise best estimate from us */
2808 res = gst_base_parse_get_duration (parse, format, &duration);
2810 gst_query_set_duration (query, format, duration);
2814 case GST_QUERY_SEEKING:
2817 GstClockTime duration = GST_CLOCK_TIME_NONE;
2818 gboolean seekable = FALSE;
2820 GST_DEBUG_OBJECT (parse, "seeking query");
2821 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
2823 /* consult upstream */
2824 res = gst_pad_query_default (pad, query);
2826 /* we may be able to help if in TIME */
2827 if (fmt == GST_FORMAT_TIME &&
2828 parse->priv->seekable > GST_BASE_PARSE_SEEK_NONE) {
2829 gst_query_parse_seeking (query, &fmt, &seekable, NULL, NULL);
2830 /* already OK if upstream takes care */
2831 GST_LOG_OBJECT (parse, "upstream handled %d, seekable %d",
2833 if (!(res && seekable)) {
2834 if (!gst_base_parse_get_duration (parse, GST_FORMAT_TIME, &duration)
2835 || duration == -1) {
2838 seekable = parse->priv->upstream_seekable;
2839 GST_LOG_OBJECT (parse, "already determine upstream seekabled: %d",
2842 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable, 0, duration);
2848 case GST_QUERY_FORMATS:
2849 gst_query_set_formatsv (query, 3, fmtlist);
2852 case GST_QUERY_CONVERT:
2854 GstFormat src_format, dest_format;
2855 gint64 src_value, dest_value;
2857 gst_query_parse_convert (query, &src_format, &src_value,
2858 &dest_format, &dest_value);
2860 res = gst_base_parse_convert (parse, src_format, src_value,
2861 dest_format, &dest_value);
2863 gst_query_set_convert (query, src_format, src_value,
2864 dest_format, dest_value);
2869 res = gst_pad_query_default (pad, query);
2875 /* scans for a cluster start from @pos,
2876 * return GST_FLOW_OK and frame position/time in @pos/@time if found */
2877 static GstFlowReturn
2878 gst_base_parse_find_frame (GstBaseParse * parse, gint64 * pos,
2879 GstClockTime * time, GstClockTime * duration)
2881 GstBaseParseClass *klass;
2883 gboolean orig_drain, orig_discont;
2884 GstFlowReturn ret = GST_FLOW_OK;
2885 GstBuffer *buf = NULL;
2887 g_return_val_if_fail (GST_FLOW_ERROR, pos != NULL);
2888 g_return_val_if_fail (GST_FLOW_ERROR, time != NULL);
2889 g_return_val_if_fail (GST_FLOW_ERROR, duration != NULL);
2891 klass = GST_BASE_PARSE_GET_CLASS (parse);
2893 *time = GST_CLOCK_TIME_NONE;
2894 *duration = GST_CLOCK_TIME_NONE;
2897 orig_offset = parse->priv->offset;
2898 orig_discont = parse->priv->discont;
2899 orig_drain = parse->priv->drain;
2901 GST_DEBUG_OBJECT (parse, "scanning for frame starting at %" G_GINT64_FORMAT
2902 " (%#" G_GINT64_MODIFIER "x)", *pos, *pos);
2904 /* jump elsewhere and locate next frame */
2905 parse->priv->offset = *pos;
2906 ret = gst_base_parse_scan_frame (parse, klass, &buf, FALSE);
2907 if (ret != GST_FLOW_OK)
2910 GST_LOG_OBJECT (parse,
2911 "peek parsing frame at offset %" G_GUINT64_FORMAT
2912 " (%#" G_GINT64_MODIFIER "x) of size %d",
2913 GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET (buf), GST_BUFFER_SIZE (buf));
2915 /* get offset first, subclass parsing might dump other stuff in there */
2916 *pos = GST_BUFFER_OFFSET (buf);
2917 ret = klass->parse_frame (parse, buf);
2919 /* but it should provide proper time */
2920 *time = GST_BUFFER_TIMESTAMP (buf);
2921 *duration = GST_BUFFER_DURATION (buf);
2922 gst_buffer_unref (buf);
2924 GST_LOG_OBJECT (parse,
2925 "frame with time %" GST_TIME_FORMAT " at offset %" G_GINT64_FORMAT,
2926 GST_TIME_ARGS (*time), *pos);
2930 parse->priv->offset = orig_offset;
2931 parse->priv->discont = orig_discont;
2932 parse->priv->drain = orig_drain;
2937 /* bisect and scan through file for frame starting before @time,
2938 * returns OK and @time/@offset if found, NONE and/or error otherwise
2939 * If @time == G_MAXINT64, scan for duration ( == last frame) */
2940 static GstFlowReturn
2941 gst_base_parse_locate_time (GstBaseParse * parse, GstClockTime * _time,
2944 GstFlowReturn ret = GST_FLOW_OK;
2945 gint64 lpos, hpos, newpos;
2946 GstClockTime time, ltime, htime, newtime, dur;
2947 gboolean cont = TRUE;
2948 const GstClockTime tolerance = TARGET_DIFFERENCE;
2949 const guint chunk = 4 * 1024;
2951 g_return_val_if_fail (_time != NULL, GST_FLOW_ERROR);
2952 g_return_val_if_fail (_offset != NULL, GST_FLOW_ERROR);
2954 /* TODO also make keyframe aware if useful some day */
2969 /* do not know at first */
2971 *_time = GST_CLOCK_TIME_NONE;
2973 /* need initial positions; start and end */
2974 lpos = parse->priv->first_frame_offset;
2975 ltime = parse->priv->first_frame_ts;
2976 htime = parse->priv->duration;
2977 hpos = parse->priv->upstream_size;
2979 /* check preconditions are satisfied;
2980 * start and end are needed, except for special case where we scan for
2981 * last frame to determine duration */
2982 if (parse->priv->pad_mode != GST_ACTIVATE_PULL || !hpos ||
2983 !GST_CLOCK_TIME_IS_VALID (ltime) ||
2984 (!GST_CLOCK_TIME_IS_VALID (htime) && time != G_MAXINT64)) {
2988 /* shortcut cases */
2991 } else if (time < ltime + tolerance) {
2995 } else if (time >= htime) {
3001 while (htime > ltime && cont) {
3002 GST_LOG_OBJECT (parse,
3003 "lpos: %" G_GUINT64_FORMAT ", ltime: %" GST_TIME_FORMAT, lpos,
3004 GST_TIME_ARGS (ltime));
3005 GST_LOG_OBJECT (parse,
3006 "hpos: %" G_GUINT64_FORMAT ", htime: %" GST_TIME_FORMAT, hpos,
3007 GST_TIME_ARGS (htime));
3008 if (G_UNLIKELY (time == G_MAXINT64)) {
3010 } else if (G_LIKELY (hpos > lpos)) {
3012 gst_util_uint64_scale (hpos - lpos, time - ltime, htime - ltime) +
3015 /* should mean lpos == hpos, since lpos <= hpos is invariant */
3017 /* we check this case once, but not forever, so break loop */
3022 newpos = CLAMP (newpos, lpos, hpos);
3023 GST_LOG_OBJECT (parse,
3024 "estimated _offset for %" GST_TIME_FORMAT ": %" G_GINT64_FORMAT,
3025 GST_TIME_ARGS (time), newpos);
3027 ret = gst_base_parse_find_frame (parse, &newpos, &newtime, &dur);
3028 if (ret == GST_FLOW_UNEXPECTED) {
3029 /* heuristic HACK */
3030 hpos = MAX (lpos, hpos - chunk);
3032 } else if (ret != GST_FLOW_OK) {
3036 if (newtime == -1 || newpos == -1) {
3037 GST_DEBUG_OBJECT (parse, "subclass did not provide metadata; aborting");
3041 if (G_UNLIKELY (time == G_MAXINT64)) {
3044 if (GST_CLOCK_TIME_IS_VALID (dur))
3047 } else if (newtime > time) {
3049 hpos = (newpos >= hpos) ? MAX (lpos, hpos - chunk) : MAX (lpos, newpos);
3051 } else if (newtime + tolerance > time) {
3052 /* close enough undershoot */
3056 } else if (newtime < ltime) {
3057 /* so a position beyond lpos resulted in earlier time than ltime ... */
3058 GST_DEBUG_OBJECT (parse, "non-ascending time; aborting");
3061 /* undershoot too far */
3062 newpos += newpos == lpos ? chunk : 0;
3063 lpos = CLAMP (newpos, lpos, hpos);
3069 GST_LOG_OBJECT (parse, "return offset %" G_GINT64_FORMAT ", time %"
3070 GST_TIME_FORMAT, *_offset, GST_TIME_ARGS (*_time));
3075 gst_base_parse_find_offset (GstBaseParse * parse, GstClockTime time,
3076 gboolean before, GstClockTime * _ts)
3078 gint64 bytes = 0, ts = 0;
3079 GstIndexEntry *entry = NULL;
3081 if (time == GST_CLOCK_TIME_NONE) {
3087 GST_OBJECT_LOCK (parse);
3088 if (parse->priv->index) {
3089 /* Let's check if we have an index entry for that time */
3090 entry = gst_index_get_assoc_entry (parse->priv->index,
3091 parse->priv->index_id,
3092 before ? GST_INDEX_LOOKUP_BEFORE : GST_INDEX_LOOKUP_AFTER,
3093 GST_ASSOCIATION_FLAG_KEY_UNIT, GST_FORMAT_TIME, time);
3097 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &bytes);
3098 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &ts);
3100 GST_DEBUG_OBJECT (parse, "found index entry for %" GST_TIME_FORMAT
3101 " at %" GST_TIME_FORMAT ", offset %" G_GINT64_FORMAT,
3102 GST_TIME_ARGS (time), GST_TIME_ARGS (ts), bytes);
3104 GST_DEBUG_OBJECT (parse, "no index entry found for %" GST_TIME_FORMAT,
3105 GST_TIME_ARGS (time));
3108 ts = GST_CLOCK_TIME_NONE;
3111 GST_OBJECT_UNLOCK (parse);
3122 * gst_base_parse_handle_seek:
3123 * @parse: #GstBaseParse.
3124 * @event: #GstEvent.
3126 * Returns: TRUE if seek succeeded.
3129 gst_base_parse_handle_seek (GstBaseParse * parse, GstEvent * event)
3134 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
3135 gboolean flush, update, res = TRUE, accurate;
3136 gint64 cur, stop, seekpos, seekstop;
3137 GstSegment seeksegment = { 0, };
3138 GstFormat dstformat;
3139 GstClockTime start_ts;
3141 gst_event_parse_seek (event, &rate, &format, &flags,
3142 &cur_type, &cur, &stop_type, &stop);
3144 GST_DEBUG_OBJECT (parse, "seek to format %s, rate %f, "
3145 "start type %d at %" GST_TIME_FORMAT ", end type %d at %"
3146 GST_TIME_FORMAT, gst_format_get_name (format), rate,
3147 cur_type, GST_TIME_ARGS (cur), stop_type, GST_TIME_ARGS (stop));
3149 /* no negative rates in push mode */
3150 if (rate < 0.0 && parse->priv->pad_mode == GST_ACTIVATE_PUSH)
3153 if (cur_type != GST_SEEK_TYPE_SET ||
3154 (stop_type != GST_SEEK_TYPE_SET && stop_type != GST_SEEK_TYPE_NONE))
3157 /* For any format other than TIME, see if upstream handles
3158 * it directly or fail. For TIME, try upstream, but do it ourselves if
3159 * it fails upstream */
3160 if (format != GST_FORMAT_TIME) {
3161 /* default action delegates to upstream */
3164 gst_event_ref (event);
3165 if (gst_pad_push_event (parse->sinkpad, event)) {
3170 /* get flush flag */
3171 flush = flags & GST_SEEK_FLAG_FLUSH;
3173 /* copy segment, we need this because we still need the old
3174 * segment when we close the current segment. */
3175 memcpy (&seeksegment, &parse->segment, sizeof (GstSegment));
3177 GST_DEBUG_OBJECT (parse, "configuring seek");
3178 gst_segment_set_seek (&seeksegment, rate, format, flags,
3179 cur_type, cur, stop_type, stop, &update);
3181 /* accurate seeking implies seek tables are used to obtain position,
3182 * and the requested segment is maintained exactly, not adjusted any way */
3183 accurate = flags & GST_SEEK_FLAG_ACCURATE;
3185 /* maybe we can be accurate for (almost) free */
3186 gst_base_parse_find_offset (parse, seeksegment.last_stop, TRUE, &start_ts);
3187 if (seeksegment.last_stop <= start_ts + TARGET_DIFFERENCE) {
3188 GST_DEBUG_OBJECT (parse, "accurate seek possible");
3192 GstClockTime startpos = seeksegment.last_stop;
3194 /* accurate requested, so ... seek a bit before target */
3195 if (startpos < parse->priv->lead_in_ts)
3198 startpos -= parse->priv->lead_in_ts;
3199 seekpos = gst_base_parse_find_offset (parse, startpos, TRUE, &start_ts);
3200 seekstop = gst_base_parse_find_offset (parse, seeksegment.stop, FALSE,
3203 start_ts = seeksegment.last_stop;
3204 dstformat = GST_FORMAT_BYTES;
3205 if (!gst_pad_query_convert (parse->srcpad, format, seeksegment.last_stop,
3206 &dstformat, &seekpos))
3207 goto convert_failed;
3208 if (!gst_pad_query_convert (parse->srcpad, format, seeksegment.stop,
3209 &dstformat, &seekstop))
3210 goto convert_failed;
3213 GST_DEBUG_OBJECT (parse,
3214 "seek position %" G_GINT64_FORMAT " in bytes: %" G_GINT64_FORMAT,
3216 GST_DEBUG_OBJECT (parse,
3217 "seek stop %" G_GINT64_FORMAT " in bytes: %" G_GINT64_FORMAT,
3218 seeksegment.stop, seekstop);
3220 if (parse->priv->pad_mode == GST_ACTIVATE_PULL) {
3223 GST_DEBUG_OBJECT (parse, "seek in PULL mode");
3226 if (parse->srcpad) {
3227 GST_DEBUG_OBJECT (parse, "sending flush start");
3228 gst_pad_push_event (parse->srcpad, gst_event_new_flush_start ());
3229 /* unlock upstream pull_range */
3230 gst_pad_push_event (parse->sinkpad, gst_event_new_flush_start ());
3233 gst_pad_pause_task (parse->sinkpad);
3236 /* we should now be able to grab the streaming thread because we stopped it
3237 * with the above flush/pause code */
3238 GST_PAD_STREAM_LOCK (parse->sinkpad);
3240 /* save current position */
3241 last_stop = parse->segment.last_stop;
3242 GST_DEBUG_OBJECT (parse, "stopped streaming at %" G_GINT64_FORMAT,
3245 /* now commit to new position */
3247 /* prepare for streaming again */
3249 GST_DEBUG_OBJECT (parse, "sending flush stop");
3250 gst_pad_push_event (parse->srcpad, gst_event_new_flush_stop ());
3251 gst_pad_push_event (parse->sinkpad, gst_event_new_flush_stop ());
3252 gst_base_parse_clear_queues (parse);
3254 if (parse->close_segment)
3255 gst_event_unref (parse->close_segment);
3257 parse->close_segment = gst_event_new_new_segment (TRUE,
3258 parse->segment.rate, parse->segment.format,
3259 parse->segment.accum, parse->segment.last_stop, parse->segment.accum);
3261 /* keep track of our last_stop */
3262 seeksegment.accum = parse->segment.last_stop;
3264 GST_DEBUG_OBJECT (parse, "Created close seg format %d, "
3265 "start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT
3266 ", pos = %" GST_TIME_FORMAT, format,
3267 GST_TIME_ARGS (parse->segment.accum),
3268 GST_TIME_ARGS (parse->segment.last_stop),
3269 GST_TIME_ARGS (parse->segment.accum));
3272 memcpy (&parse->segment, &seeksegment, sizeof (GstSegment));
3274 /* store the newsegment event so it can be sent from the streaming thread. */
3275 if (parse->pending_segment)
3276 gst_event_unref (parse->pending_segment);
3278 /* This will be sent later in _loop() */
3279 parse->pending_segment =
3280 gst_event_new_new_segment (FALSE, parse->segment.rate,
3281 parse->segment.format, parse->segment.start, parse->segment.stop,
3282 parse->segment.start);
3284 GST_DEBUG_OBJECT (parse, "Created newseg format %d, "
3285 "start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT
3286 ", pos = %" GST_TIME_FORMAT, format,
3287 GST_TIME_ARGS (parse->segment.start),
3288 GST_TIME_ARGS (parse->segment.stop),
3289 GST_TIME_ARGS (parse->segment.start));
3291 /* one last chance in pull mode to stay accurate;
3292 * maybe scan and subclass can find where to go */
3295 GstClockTime ts = seeksegment.last_stop;
3297 gst_base_parse_locate_time (parse, &ts, &scanpos);
3301 /* running collected index now consists of several intervals,
3302 * so optimized check no longer possible */
3303 parse->priv->index_last_valid = FALSE;
3304 parse->priv->index_last_offset = 0;
3305 parse->priv->index_last_ts = 0;
3309 /* mark discont if we are going to stream from another position. */
3310 if (seekpos != parse->priv->offset) {
3311 GST_DEBUG_OBJECT (parse,
3312 "mark DISCONT, we did a seek to another position");
3313 parse->priv->offset = seekpos;
3314 parse->priv->last_offset = seekpos;
3315 parse->priv->seen_keyframe = FALSE;
3316 parse->priv->discont = TRUE;
3317 parse->priv->next_ts = start_ts;
3318 parse->priv->last_ts = GST_CLOCK_TIME_NONE;
3319 parse->priv->sync_offset = seekpos;
3320 parse->priv->exact_position = accurate;
3323 /* Start streaming thread if paused */
3324 gst_pad_start_task (parse->sinkpad,
3325 (GstTaskFunction) gst_base_parse_loop, parse->sinkpad);
3327 GST_PAD_STREAM_UNLOCK (parse->sinkpad);
3329 GstEvent *new_event;
3330 GstBaseParseSeek *seek;
3332 /* The only thing we need to do in PUSH-mode is to send the
3333 seek event (in bytes) to upstream. Segment / flush handling happens
3334 in corresponding src event handlers */
3335 GST_DEBUG_OBJECT (parse, "seek in PUSH mode");
3336 if (seekstop >= 0 && seekpos <= seekpos)
3338 new_event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flush,
3339 GST_SEEK_TYPE_SET, seekpos, stop_type, seekstop);
3341 /* store segment info so its precise details can be reconstructed when
3342 * receiving newsegment;
3343 * this matters for all details when accurate seeking,
3344 * is most useful to preserve NONE stop time otherwise */
3345 seek = g_new0 (GstBaseParseSeek, 1);
3346 seek->segment = seeksegment;
3347 seek->accurate = accurate;
3348 seek->offset = seekpos;
3349 seek->start_ts = start_ts;
3350 GST_OBJECT_LOCK (parse);
3351 /* less optimal, but preserves order */
3352 parse->priv->pending_seeks =
3353 g_slist_append (parse->priv->pending_seeks, seek);
3354 GST_OBJECT_UNLOCK (parse);
3356 res = gst_pad_push_event (parse->sinkpad, new_event);
3359 GST_OBJECT_LOCK (parse);
3360 parse->priv->pending_seeks =
3361 g_slist_remove (parse->priv->pending_seeks, seek);
3362 GST_OBJECT_UNLOCK (parse);
3373 GST_DEBUG_OBJECT (parse, "negative playback rates are not supported yet.");
3379 GST_DEBUG_OBJECT (parse, "unsupported seek type.");
3385 GST_DEBUG_OBJECT (parse, "conversion TIME to BYTES failed.");
3392 * gst_base_parse_handle_tag:
3393 * @parse: #GstBaseParse.
3394 * @event: #GstEvent.
3396 * Checks if bitrates are available from upstream tags so that we don't
3397 * override them later
3400 gst_base_parse_handle_tag (GstBaseParse * parse, GstEvent * event)
3402 GstTagList *taglist = NULL;
3405 gst_event_parse_tag (event, &taglist);
3407 if (gst_tag_list_get_uint (taglist, GST_TAG_MINIMUM_BITRATE, &tmp)) {
3408 GST_DEBUG_OBJECT (parse, "upstream min bitrate %d", tmp);
3409 parse->priv->post_min_bitrate = FALSE;
3411 if (gst_tag_list_get_uint (taglist, GST_TAG_BITRATE, &tmp)) {
3412 GST_DEBUG_OBJECT (parse, "upstream avg bitrate %d", tmp);
3413 parse->priv->post_avg_bitrate = FALSE;
3415 if (gst_tag_list_get_uint (taglist, GST_TAG_MAXIMUM_BITRATE, &tmp)) {
3416 GST_DEBUG_OBJECT (parse, "upstream max bitrate %d", tmp);
3417 parse->priv->post_max_bitrate = FALSE;
3422 * gst_base_parse_sink_setcaps:
3426 * Returns: TRUE if caps were accepted.
3429 gst_base_parse_sink_setcaps (GstPad * pad, GstCaps * caps)
3431 GstBaseParse *parse;
3432 GstBaseParseClass *klass;
3433 gboolean res = TRUE;
3435 parse = GST_BASE_PARSE (GST_PAD_PARENT (pad));
3436 klass = GST_BASE_PARSE_GET_CLASS (parse);
3438 GST_DEBUG_OBJECT (parse, "caps: %" GST_PTR_FORMAT, caps);
3440 if (klass->set_sink_caps)
3441 res = klass->set_sink_caps (parse, caps);
3443 return res && gst_pad_set_caps (pad, caps);
3447 gst_base_parse_set_index (GstElement * element, GstIndex * index)
3449 GstBaseParse *parse = GST_BASE_PARSE (element);
3451 GST_OBJECT_LOCK (parse);
3452 if (parse->priv->index)
3453 gst_object_unref (parse->priv->index);
3455 parse->priv->index = gst_object_ref (index);
3456 gst_index_get_writer_id (index, GST_OBJECT (element),
3457 &parse->priv->index_id);
3458 parse->priv->own_index = FALSE;
3460 parse->priv->index = NULL;
3461 GST_OBJECT_UNLOCK (parse);
3465 gst_base_parse_get_index (GstElement * element)
3467 GstBaseParse *parse = GST_BASE_PARSE (element);
3468 GstIndex *result = NULL;
3470 GST_OBJECT_LOCK (parse);
3471 if (parse->priv->index)
3472 result = gst_object_ref (parse->priv->index);
3473 GST_OBJECT_UNLOCK (parse);
3478 static GstStateChangeReturn
3479 gst_base_parse_change_state (GstElement * element, GstStateChange transition)
3481 GstBaseParse *parse;
3482 GstStateChangeReturn result;
3484 parse = GST_BASE_PARSE (element);
3486 switch (transition) {
3487 case GST_STATE_CHANGE_READY_TO_PAUSED:
3488 /* If this is our own index destroy it as the
3489 * old entries might be wrong for the new stream */
3490 if (parse->priv->own_index) {
3491 gst_object_unref (parse->priv->index);
3492 parse->priv->index = NULL;
3493 parse->priv->own_index = FALSE;
3496 /* If no index was created, generate one */
3497 if (G_UNLIKELY (!parse->priv->index)) {
3498 GST_DEBUG_OBJECT (parse, "no index provided creating our own");
3500 parse->priv->index = gst_index_factory_make ("memindex");
3501 gst_index_get_writer_id (parse->priv->index, GST_OBJECT (parse),
3502 &parse->priv->index_id);
3503 parse->priv->own_index = TRUE;
3510 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3512 switch (transition) {
3513 case GST_STATE_CHANGE_PAUSED_TO_READY:
3514 gst_base_parse_reset (parse);