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 /* offset will increase again as fragment is processed/parsed */
2157 parse->priv->last_offset = offset;
2159 gst_adapter_push (parse->adapter, buffer);
2160 ret = gst_base_parse_process_fragment (parse, FALSE);
2161 if (ret != GST_FLOW_OK)
2164 /* force previous fragment */
2165 parse->priv->offset = -1;
2172 * pull and scan for next frame starting from current offset
2173 * ajusts sync, drain and offset going along */
2174 static GstFlowReturn
2175 gst_base_parse_scan_frame (GstBaseParse * parse, GstBaseParseClass * klass,
2176 GstBuffer ** buf, gboolean full)
2178 GstBuffer *buffer, *outbuf;
2179 GstFlowReturn ret = GST_FLOW_OK;
2180 guint fsize = 0, min_size;
2183 g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
2187 min_size = parse->priv->min_frame_size;
2189 ret = gst_base_parse_pull_range (parse, min_size, &buffer);
2190 if (ret != GST_FLOW_OK)
2193 if (parse->priv->discont) {
2194 GST_DEBUG_OBJECT (parse, "marking DISCONT");
2195 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
2198 /* if we got a short read, inform subclass we are draining leftover
2199 * and no more is to be expected */
2200 if (GST_BUFFER_SIZE (buffer) < min_size)
2201 parse->priv->drain = TRUE;
2204 if (klass->check_valid_frame (parse, buffer, &fsize, &skip)) {
2205 parse->priv->drain = FALSE;
2208 parse->priv->drain = FALSE;
2212 GST_LOG_OBJECT (parse, "finding sync, skipping %d bytes", skip);
2213 if (full && parse->segment.rate < 0.0 && !parse->priv->buffers_queued) {
2214 /* reverse playback, and no frames found yet, so we are skipping
2215 * the leading part of a fragment, which may form the tail of
2216 * fragment coming later, hopefully subclass skips efficiently ... */
2217 outbuf = gst_buffer_create_sub (buffer, 0, skip);
2218 parse->priv->buffers_pending =
2219 g_slist_prepend (parse->priv->buffers_pending, outbuf);
2222 parse->priv->offset += skip;
2223 if (!parse->priv->discont)
2224 parse->priv->sync_offset = parse->priv->offset;
2225 parse->priv->discont = TRUE;
2227 /* skip == 0 should imply subclass set min_size to need more data ... */
2228 GST_DEBUG_OBJECT (parse, "finding sync...");
2229 gst_buffer_unref (buffer);
2230 if ((ret = gst_base_parse_check_sync (parse)) != GST_FLOW_OK) {
2235 if (fsize <= GST_BUFFER_SIZE (buffer)) {
2236 outbuf = gst_buffer_create_sub (buffer, 0, fsize);
2237 GST_BUFFER_OFFSET (outbuf) = GST_BUFFER_OFFSET (buffer);
2238 GST_BUFFER_TIMESTAMP (outbuf) = GST_CLOCK_TIME_NONE;
2239 gst_buffer_unref (buffer);
2241 gst_buffer_unref (buffer);
2242 ret = gst_base_parse_pull_range (parse, fsize, &outbuf);
2243 if (ret != GST_FLOW_OK)
2245 if (GST_BUFFER_SIZE (outbuf) < fsize) {
2246 gst_buffer_unref (outbuf);
2247 ret = GST_FLOW_UNEXPECTED;
2251 parse->priv->offset += fsize;
2253 /* Does the subclass want to skip too? */
2255 parse->priv->offset += skip;
2264 * gst_base_parse_loop:
2267 * Loop that is used in pull mode to retrieve data from upstream.
2270 gst_base_parse_loop (GstPad * pad)
2272 GstBaseParse *parse;
2273 GstBaseParseClass *klass;
2275 GstFlowReturn ret = GST_FLOW_OK;
2277 parse = GST_BASE_PARSE (gst_pad_get_parent (pad));
2278 klass = GST_BASE_PARSE_GET_CLASS (parse);
2280 /* reverse playback:
2281 * first fragment (closest to stop time) is handled normally below,
2282 * then we pull in fragments going backwards */
2283 if (parse->segment.rate < 0.0) {
2284 /* check if we jumped back to a previous fragment,
2285 * which is a post-first fragment */
2286 if (parse->priv->offset < 0) {
2287 ret = gst_base_parse_handle_previous_fragment (parse);
2292 ret = gst_base_parse_scan_frame (parse, klass, &outbuf, TRUE);
2293 if (ret != GST_FLOW_OK)
2296 /* This always unrefs the outbuf, even if error occurs */
2297 ret = gst_base_parse_handle_and_push_buffer (parse, klass, outbuf);
2299 /* eat expected eos signalling past segment in reverse playback */
2300 if (parse->segment.rate < 0.0 && ret == GST_FLOW_UNEXPECTED &&
2301 parse->segment.last_stop >= parse->segment.stop) {
2302 GST_DEBUG_OBJECT (parse, "downstream has reached end of segment");
2303 /* push what was accumulated during loop run */
2304 gst_base_parse_process_fragment (parse, TRUE);
2305 /* force previous fragment */
2306 parse->priv->offset = -1;
2311 if (ret == GST_FLOW_UNEXPECTED)
2313 else if (ret != GST_FLOW_OK)
2316 gst_object_unref (parse);
2322 ret = GST_FLOW_UNEXPECTED;
2323 GST_DEBUG_OBJECT (parse, "eos");
2328 gboolean push_eos = FALSE;
2330 GST_DEBUG_OBJECT (parse, "pausing task, reason %s",
2331 gst_flow_get_name (ret));
2332 gst_pad_pause_task (parse->sinkpad);
2334 if (ret == GST_FLOW_UNEXPECTED) {
2335 /* handle end-of-stream/segment */
2336 if (parse->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2339 if ((stop = parse->segment.stop) == -1)
2340 stop = parse->segment.duration;
2342 GST_DEBUG_OBJECT (parse, "sending segment_done");
2344 gst_element_post_message
2345 (GST_ELEMENT_CAST (parse),
2346 gst_message_new_segment_done (GST_OBJECT_CAST (parse),
2347 GST_FORMAT_TIME, stop));
2349 /* If we STILL have zero frames processed, fire an error */
2350 if (parse->priv->framecount == 0) {
2351 GST_ELEMENT_ERROR (parse, STREAM, WRONG_TYPE,
2352 ("No valid frames found before end of stream"), (NULL));
2356 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_UNEXPECTED) {
2357 /* for fatal errors we post an error message, wrong-state is
2358 * not fatal because it happens due to flushes and only means
2359 * that we should stop now. */
2360 GST_ELEMENT_ERROR (parse, STREAM, FAILED, (NULL),
2361 ("streaming stopped, reason %s", gst_flow_get_name (ret)));
2365 /* newsegment before eos */
2366 if (parse->pending_segment) {
2367 gst_pad_push_event (parse->srcpad, parse->pending_segment);
2368 parse->pending_segment = NULL;
2370 gst_pad_push_event (parse->srcpad, gst_event_new_eos ());
2372 gst_object_unref (parse);
2378 * gst_base_parse_sink_activate:
2379 * @sinkpad: #GstPad to be activated.
2381 * Returns: TRUE if activation succeeded.
2384 gst_base_parse_sink_activate (GstPad * sinkpad)
2386 GstBaseParse *parse;
2387 gboolean result = TRUE;
2389 parse = GST_BASE_PARSE (gst_pad_get_parent (sinkpad));
2391 GST_DEBUG_OBJECT (parse, "sink activate");
2393 if (gst_pad_check_pull_range (sinkpad)) {
2394 GST_DEBUG_OBJECT (parse, "trying to activate in pull mode");
2395 result = gst_pad_activate_pull (sinkpad, TRUE);
2397 GST_DEBUG_OBJECT (parse, "trying to activate in push mode");
2398 result = gst_pad_activate_push (sinkpad, TRUE);
2401 GST_DEBUG_OBJECT (parse, "sink activate return %d", result);
2402 gst_object_unref (parse);
2408 * gst_base_parse_activate:
2409 * @parse: #GstBaseParse.
2410 * @active: TRUE if element will be activated, FALSE if deactivated.
2412 * Returns: TRUE if the operation succeeded.
2415 gst_base_parse_activate (GstBaseParse * parse, gboolean active)
2417 GstBaseParseClass *klass;
2418 gboolean result = FALSE;
2420 GST_DEBUG_OBJECT (parse, "activate");
2422 klass = GST_BASE_PARSE_GET_CLASS (parse);
2425 if (parse->priv->pad_mode == GST_ACTIVATE_NONE && klass->start)
2426 result = klass->start (parse);
2428 /* We must make sure streaming has finished before resetting things
2429 * and calling the ::stop vfunc */
2430 GST_PAD_STREAM_LOCK (parse->sinkpad);
2431 GST_PAD_STREAM_UNLOCK (parse->sinkpad);
2433 if (parse->priv->pad_mode != GST_ACTIVATE_NONE && klass->stop)
2434 result = klass->stop (parse);
2436 parse->priv->pad_mode = GST_ACTIVATE_NONE;
2438 GST_DEBUG_OBJECT (parse, "activate: %d", result);
2444 * gst_base_parse_sink_activate_push:
2445 * @pad: #GstPad to be (de)activated.
2446 * @active: TRUE when activating, FALSE when deactivating.
2448 * Returns: TRUE if (de)activation succeeded.
2451 gst_base_parse_sink_activate_push (GstPad * pad, gboolean active)
2453 gboolean result = TRUE;
2454 GstBaseParse *parse;
2456 parse = GST_BASE_PARSE (gst_pad_get_parent (pad));
2458 GST_DEBUG_OBJECT (parse, "sink activate push");
2460 result = gst_base_parse_activate (parse, active);
2463 parse->priv->pad_mode = active ? GST_ACTIVATE_PUSH : GST_ACTIVATE_NONE;
2465 GST_DEBUG_OBJECT (parse, "sink activate push: %d", result);
2467 gst_object_unref (parse);
2473 * gst_base_parse_sink_activate_pull:
2474 * @sinkpad: #GstPad to be (de)activated.
2475 * @active: TRUE when activating, FALSE when deactivating.
2477 * Returns: TRUE if (de)activation succeeded.
2480 gst_base_parse_sink_activate_pull (GstPad * sinkpad, gboolean active)
2482 gboolean result = FALSE;
2483 GstBaseParse *parse;
2485 parse = GST_BASE_PARSE (gst_pad_get_parent (sinkpad));
2487 GST_DEBUG_OBJECT (parse, "activate pull");
2489 result = gst_base_parse_activate (parse, active);
2493 parse->pending_segment = gst_event_new_new_segment (FALSE,
2494 parse->segment.rate, parse->segment.format,
2495 parse->segment.start, parse->segment.stop, parse->segment.last_stop);
2496 result &= gst_pad_start_task (sinkpad,
2497 (GstTaskFunction) gst_base_parse_loop, sinkpad);
2499 result &= gst_pad_stop_task (sinkpad);
2504 parse->priv->pad_mode = active ? GST_ACTIVATE_PULL : GST_ACTIVATE_NONE;
2506 GST_DEBUG_OBJECT (parse, "sink activate pull: %d", result);
2508 gst_object_unref (parse);
2514 * gst_base_parse_set_duration:
2515 * @parse: #GstBaseParse.
2517 * @duration: duration value.
2519 * Sets the duration of the currently playing media. Subclass can use this
2520 * when it is able to determine duration and/or notices a change in the media
2521 * duration. Alternatively, if @interval is non-zero (default), then stream
2522 * duration is determined based on estimated bitrate, and updated every @interval
2525 gst_base_parse_set_duration (GstBaseParse * parse,
2526 GstFormat fmt, gint64 duration, gint interval)
2528 g_return_if_fail (parse != NULL);
2530 if (parse->priv->upstream_has_duration) {
2531 GST_DEBUG_OBJECT (parse, "using upstream duration; discarding update");
2535 if (duration != parse->priv->duration) {
2538 m = gst_message_new_duration (GST_OBJECT (parse), fmt, duration);
2539 gst_element_post_message (GST_ELEMENT (parse), m);
2541 /* TODO: what about duration tag? */
2543 parse->priv->duration = duration;
2544 parse->priv->duration_fmt = fmt;
2545 GST_DEBUG_OBJECT (parse, "set duration: %" G_GINT64_FORMAT, duration);
2546 if (fmt == GST_FORMAT_TIME && GST_CLOCK_TIME_IS_VALID (duration)) {
2547 if (interval != 0) {
2548 GST_DEBUG_OBJECT (parse, "valid duration provided, disabling estimate");
2552 GST_DEBUG_OBJECT (parse, "set update interval: %d", interval);
2553 parse->priv->update_interval = interval;
2559 * gst_base_parse_set_seek:
2560 * @parse: #GstBaseParse.
2561 * @seek: #GstBaseParseSeekable.
2562 * @abitrate: average bitrate.
2564 * Sets whether and how the media is seekable (in time).
2565 * Also optionally provides average bitrate detected in media (if non-zero),
2566 * e.g. based on metadata, as it will be posted to the application.
2568 * By default, announced average bitrate is estimated, and seekability is assumed
2569 * possible based on estimated bitrate.
2572 gst_base_parse_set_seek (GstBaseParse * parse,
2573 GstBaseParseSeekable seek, guint bitrate)
2575 parse->priv->seekable = seek;
2576 parse->priv->bitrate = bitrate;
2577 GST_DEBUG_OBJECT (parse, "seek %d, bitrate %d", seek, bitrate);
2582 * gst_base_parse_set_min_frame_size:
2583 * @parse: #GstBaseParse.
2584 * @min_size: Minimum size of the data that this base class should give to
2587 * Subclass can use this function to tell the base class that it needs to
2588 * give at least #min_size buffers.
2591 gst_base_parse_set_min_frame_size (GstBaseParse * parse, guint min_size)
2593 g_return_if_fail (parse != NULL);
2595 parse->priv->min_frame_size = min_size;
2596 GST_LOG_OBJECT (parse, "set frame_min_size: %d", min_size);
2600 * gst_base_parse_set_passthrough:
2601 * @parse: the #GstBaseParse to set
2602 * @passthrough: boolean indicating passthrough mode.
2604 * Set passthrough mode for this parser. If operating in passthrough,
2605 * incoming buffers are pushed through unmodified.
2608 gst_base_parse_set_passthrough (GstBaseParse * parse, gboolean passthrough)
2610 g_return_if_fail (parse != NULL);
2612 parse->priv->passthrough = passthrough;
2613 GST_LOG_OBJECT (parse, "set passthrough: %d", passthrough);
2617 * gst_base_parse_set_frame_props:
2618 * @parse: the #GstBaseParse to set
2619 * @fps_num: frames per second (numerator).
2620 * @fps_den: frames per second (denominator).
2621 * @lead_in: frames needed before a segment for subsequent decode
2622 * @lead_out: frames needed after a segment
2624 * If frames per second is configured, parser can take care of buffer duration
2625 * and timestamping. When performing segment clipping, or seeking to a specific
2626 * location, a corresponding decoder might need an initial @lead_in and a
2627 * following @lead_out number of frames to ensure the desired segment is
2628 * entirely filled upon decoding.
2631 gst_base_parse_set_frame_props (GstBaseParse * parse, guint fps_num,
2632 guint fps_den, guint lead_in, guint lead_out)
2634 g_return_if_fail (parse != NULL);
2636 parse->priv->fps_num = fps_num;
2637 parse->priv->fps_den = fps_den;
2638 if (!fps_num || !fps_den) {
2639 GST_DEBUG_OBJECT (parse, "invalid fps (%d/%d), ignoring parameters",
2641 fps_num = fps_den = 0;
2642 parse->priv->frame_duration = GST_CLOCK_TIME_NONE;
2643 parse->priv->lead_in = parse->priv->lead_out = 0;
2644 parse->priv->lead_in_ts = parse->priv->lead_out_ts = 0;
2646 parse->priv->frame_duration =
2647 gst_util_uint64_scale (GST_SECOND, fps_den, fps_num);
2648 parse->priv->lead_in = lead_in;
2649 parse->priv->lead_out = lead_out;
2650 parse->priv->lead_in_ts =
2651 gst_util_uint64_scale (GST_SECOND, fps_den * lead_in, fps_num);
2652 parse->priv->lead_out_ts =
2653 gst_util_uint64_scale (GST_SECOND, fps_den * lead_out, fps_num);
2655 GST_LOG_OBJECT (parse, "set fps: %d/%d => duration: %" G_GINT64_FORMAT " ms",
2656 fps_num, fps_den, parse->priv->frame_duration / GST_MSECOND);
2657 GST_LOG_OBJECT (parse, "set lead in: %d frames = %" G_GUINT64_FORMAT " ms, "
2658 "lead out: %d frames = %" G_GUINT64_FORMAT " ms",
2659 lead_in, parse->priv->lead_in_ts / GST_MSECOND,
2660 lead_out, parse->priv->lead_out_ts / GST_MSECOND);
2664 * gst_base_parse_get_sync:
2665 * @parse: the #GstBaseParse to query
2667 * Returns: TRUE if parser is considered 'in sync'. That is, frames have been
2668 * continuously successfully parsed and pushed.
2671 gst_base_parse_get_sync (GstBaseParse * parse)
2675 g_return_val_if_fail (parse != NULL, FALSE);
2677 /* losing sync is pretty much a discont (and vice versa), no ? */
2678 ret = !parse->priv->discont;
2680 GST_DEBUG_OBJECT (parse, "sync: %d", ret);
2685 * gst_base_parse_get_drain:
2686 * @parse: the #GstBaseParse to query
2688 * Returns: TRUE if parser is currently 'draining'. That is, leftover data
2689 * (e.g. in FLUSH or EOS situation) is being parsed.
2692 gst_base_parse_get_drain (GstBaseParse * parse)
2696 g_return_val_if_fail (parse != NULL, FALSE);
2698 /* losing sync is pretty much a discont (and vice versa), no ? */
2699 ret = parse->priv->drain;
2701 GST_DEBUG_OBJECT (parse, "drain: %d", ret);
2706 gst_base_parse_get_duration (GstBaseParse * parse, GstFormat format,
2707 GstClockTime * duration)
2709 gboolean res = FALSE;
2711 g_return_val_if_fail (duration != NULL, FALSE);
2713 *duration = GST_CLOCK_TIME_NONE;
2714 if (parse->priv->duration != -1 && format == parse->priv->duration_fmt) {
2715 GST_LOG_OBJECT (parse, "using provided duration");
2716 *duration = parse->priv->duration;
2718 } else if (parse->priv->duration != -1) {
2719 GST_LOG_OBJECT (parse, "converting provided duration");
2720 res = gst_base_parse_convert (parse, parse->priv->duration_fmt,
2721 parse->priv->duration, format, (gint64 *) duration);
2722 } else if (format == GST_FORMAT_TIME && parse->priv->estimated_duration != -1) {
2723 GST_LOG_OBJECT (parse, "using estimated duration");
2724 *duration = parse->priv->estimated_duration;
2728 GST_LOG_OBJECT (parse, "res: %d, duration %" GST_TIME_FORMAT, res,
2729 GST_TIME_ARGS (*duration));
2734 * gst_base_parse_get_querytypes:
2737 * Returns: A table of #GstQueryType items describing supported query types.
2739 static const GstQueryType *
2740 gst_base_parse_get_querytypes (GstPad * pad)
2742 static const GstQueryType list[] = {
2756 * gst_base_parse_query:
2758 * @query: #GstQuery.
2760 * Returns: TRUE on success.
2763 gst_base_parse_query (GstPad * pad, GstQuery * query)
2765 GstBaseParse *parse;
2766 gboolean res = FALSE;
2768 parse = GST_BASE_PARSE (GST_PAD_PARENT (pad));
2770 GST_LOG_OBJECT (parse, "handling query: %" GST_PTR_FORMAT, query);
2772 switch (GST_QUERY_TYPE (query)) {
2773 case GST_QUERY_POSITION:
2778 GST_DEBUG_OBJECT (parse, "position query");
2779 gst_query_parse_position (query, &format, NULL);
2781 GST_OBJECT_LOCK (parse);
2782 if (format == GST_FORMAT_BYTES) {
2783 dest_value = parse->priv->offset;
2785 } else if (format == parse->segment.format &&
2786 GST_CLOCK_TIME_IS_VALID (parse->segment.last_stop)) {
2787 dest_value = parse->segment.last_stop;
2790 GST_OBJECT_UNLOCK (parse);
2793 gst_query_set_position (query, format, dest_value);
2795 res = gst_pad_query_default (pad, query);
2797 /* no precise result, upstream no idea either, then best estimate */
2798 /* priv->offset is updated in both PUSH/PULL modes */
2799 res = gst_base_parse_convert (parse,
2800 GST_FORMAT_BYTES, parse->priv->offset, format, &dest_value);
2805 case GST_QUERY_DURATION:
2808 GstClockTime duration;
2810 GST_DEBUG_OBJECT (parse, "duration query");
2811 gst_query_parse_duration (query, &format, NULL);
2813 /* consult upstream */
2814 res = gst_pad_query_default (pad, query);
2816 /* otherwise best estimate from us */
2818 res = gst_base_parse_get_duration (parse, format, &duration);
2820 gst_query_set_duration (query, format, duration);
2824 case GST_QUERY_SEEKING:
2827 GstClockTime duration = GST_CLOCK_TIME_NONE;
2828 gboolean seekable = FALSE;
2830 GST_DEBUG_OBJECT (parse, "seeking query");
2831 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
2833 /* consult upstream */
2834 res = gst_pad_query_default (pad, query);
2836 /* we may be able to help if in TIME */
2837 if (fmt == GST_FORMAT_TIME &&
2838 parse->priv->seekable > GST_BASE_PARSE_SEEK_NONE) {
2839 gst_query_parse_seeking (query, &fmt, &seekable, NULL, NULL);
2840 /* already OK if upstream takes care */
2841 GST_LOG_OBJECT (parse, "upstream handled %d, seekable %d",
2843 if (!(res && seekable)) {
2844 if (!gst_base_parse_get_duration (parse, GST_FORMAT_TIME, &duration)
2845 || duration == -1) {
2848 seekable = parse->priv->upstream_seekable;
2849 GST_LOG_OBJECT (parse, "already determine upstream seekabled: %d",
2852 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable, 0, duration);
2858 case GST_QUERY_FORMATS:
2859 gst_query_set_formatsv (query, 3, fmtlist);
2862 case GST_QUERY_CONVERT:
2864 GstFormat src_format, dest_format;
2865 gint64 src_value, dest_value;
2867 gst_query_parse_convert (query, &src_format, &src_value,
2868 &dest_format, &dest_value);
2870 res = gst_base_parse_convert (parse, src_format, src_value,
2871 dest_format, &dest_value);
2873 gst_query_set_convert (query, src_format, src_value,
2874 dest_format, dest_value);
2879 res = gst_pad_query_default (pad, query);
2885 /* scans for a cluster start from @pos,
2886 * return GST_FLOW_OK and frame position/time in @pos/@time if found */
2887 static GstFlowReturn
2888 gst_base_parse_find_frame (GstBaseParse * parse, gint64 * pos,
2889 GstClockTime * time, GstClockTime * duration)
2891 GstBaseParseClass *klass;
2893 gboolean orig_drain, orig_discont;
2894 GstFlowReturn ret = GST_FLOW_OK;
2895 GstBuffer *buf = NULL;
2897 g_return_val_if_fail (GST_FLOW_ERROR, pos != NULL);
2898 g_return_val_if_fail (GST_FLOW_ERROR, time != NULL);
2899 g_return_val_if_fail (GST_FLOW_ERROR, duration != NULL);
2901 klass = GST_BASE_PARSE_GET_CLASS (parse);
2903 *time = GST_CLOCK_TIME_NONE;
2904 *duration = GST_CLOCK_TIME_NONE;
2907 orig_offset = parse->priv->offset;
2908 orig_discont = parse->priv->discont;
2909 orig_drain = parse->priv->drain;
2911 GST_DEBUG_OBJECT (parse, "scanning for frame starting at %" G_GINT64_FORMAT
2912 " (%#" G_GINT64_MODIFIER "x)", *pos, *pos);
2914 /* jump elsewhere and locate next frame */
2915 parse->priv->offset = *pos;
2916 ret = gst_base_parse_scan_frame (parse, klass, &buf, FALSE);
2917 if (ret != GST_FLOW_OK)
2920 GST_LOG_OBJECT (parse,
2921 "peek parsing frame at offset %" G_GUINT64_FORMAT
2922 " (%#" G_GINT64_MODIFIER "x) of size %d",
2923 GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET (buf), GST_BUFFER_SIZE (buf));
2925 /* get offset first, subclass parsing might dump other stuff in there */
2926 *pos = GST_BUFFER_OFFSET (buf);
2927 ret = klass->parse_frame (parse, buf);
2929 /* but it should provide proper time */
2930 *time = GST_BUFFER_TIMESTAMP (buf);
2931 *duration = GST_BUFFER_DURATION (buf);
2932 gst_buffer_unref (buf);
2934 GST_LOG_OBJECT (parse,
2935 "frame with time %" GST_TIME_FORMAT " at offset %" G_GINT64_FORMAT,
2936 GST_TIME_ARGS (*time), *pos);
2940 parse->priv->offset = orig_offset;
2941 parse->priv->discont = orig_discont;
2942 parse->priv->drain = orig_drain;
2947 /* bisect and scan through file for frame starting before @time,
2948 * returns OK and @time/@offset if found, NONE and/or error otherwise
2949 * If @time == G_MAXINT64, scan for duration ( == last frame) */
2950 static GstFlowReturn
2951 gst_base_parse_locate_time (GstBaseParse * parse, GstClockTime * _time,
2954 GstFlowReturn ret = GST_FLOW_OK;
2955 gint64 lpos, hpos, newpos;
2956 GstClockTime time, ltime, htime, newtime, dur;
2957 gboolean cont = TRUE;
2958 const GstClockTime tolerance = TARGET_DIFFERENCE;
2959 const guint chunk = 4 * 1024;
2961 g_return_val_if_fail (_time != NULL, GST_FLOW_ERROR);
2962 g_return_val_if_fail (_offset != NULL, GST_FLOW_ERROR);
2964 /* TODO also make keyframe aware if useful some day */
2979 /* do not know at first */
2981 *_time = GST_CLOCK_TIME_NONE;
2983 /* need initial positions; start and end */
2984 lpos = parse->priv->first_frame_offset;
2985 ltime = parse->priv->first_frame_ts;
2986 htime = parse->priv->duration;
2987 hpos = parse->priv->upstream_size;
2989 /* check preconditions are satisfied;
2990 * start and end are needed, except for special case where we scan for
2991 * last frame to determine duration */
2992 if (parse->priv->pad_mode != GST_ACTIVATE_PULL || !hpos ||
2993 !GST_CLOCK_TIME_IS_VALID (ltime) ||
2994 (!GST_CLOCK_TIME_IS_VALID (htime) && time != G_MAXINT64)) {
2998 /* shortcut cases */
3001 } else if (time < ltime + tolerance) {
3005 } else if (time >= htime) {
3011 while (htime > ltime && cont) {
3012 GST_LOG_OBJECT (parse,
3013 "lpos: %" G_GUINT64_FORMAT ", ltime: %" GST_TIME_FORMAT, lpos,
3014 GST_TIME_ARGS (ltime));
3015 GST_LOG_OBJECT (parse,
3016 "hpos: %" G_GUINT64_FORMAT ", htime: %" GST_TIME_FORMAT, hpos,
3017 GST_TIME_ARGS (htime));
3018 if (G_UNLIKELY (time == G_MAXINT64)) {
3020 } else if (G_LIKELY (hpos > lpos)) {
3022 gst_util_uint64_scale (hpos - lpos, time - ltime, htime - ltime) +
3025 /* should mean lpos == hpos, since lpos <= hpos is invariant */
3027 /* we check this case once, but not forever, so break loop */
3032 newpos = CLAMP (newpos, lpos, hpos);
3033 GST_LOG_OBJECT (parse,
3034 "estimated _offset for %" GST_TIME_FORMAT ": %" G_GINT64_FORMAT,
3035 GST_TIME_ARGS (time), newpos);
3037 ret = gst_base_parse_find_frame (parse, &newpos, &newtime, &dur);
3038 if (ret == GST_FLOW_UNEXPECTED) {
3039 /* heuristic HACK */
3040 hpos = MAX (lpos, hpos - chunk);
3042 } else if (ret != GST_FLOW_OK) {
3046 if (newtime == -1 || newpos == -1) {
3047 GST_DEBUG_OBJECT (parse, "subclass did not provide metadata; aborting");
3051 if (G_UNLIKELY (time == G_MAXINT64)) {
3054 if (GST_CLOCK_TIME_IS_VALID (dur))
3057 } else if (newtime > time) {
3059 hpos = (newpos >= hpos) ? MAX (lpos, hpos - chunk) : MAX (lpos, newpos);
3061 } else if (newtime + tolerance > time) {
3062 /* close enough undershoot */
3066 } else if (newtime < ltime) {
3067 /* so a position beyond lpos resulted in earlier time than ltime ... */
3068 GST_DEBUG_OBJECT (parse, "non-ascending time; aborting");
3071 /* undershoot too far */
3072 newpos += newpos == lpos ? chunk : 0;
3073 lpos = CLAMP (newpos, lpos, hpos);
3079 GST_LOG_OBJECT (parse, "return offset %" G_GINT64_FORMAT ", time %"
3080 GST_TIME_FORMAT, *_offset, GST_TIME_ARGS (*_time));
3085 gst_base_parse_find_offset (GstBaseParse * parse, GstClockTime time,
3086 gboolean before, GstClockTime * _ts)
3088 gint64 bytes = 0, ts = 0;
3089 GstIndexEntry *entry = NULL;
3091 if (time == GST_CLOCK_TIME_NONE) {
3097 GST_OBJECT_LOCK (parse);
3098 if (parse->priv->index) {
3099 /* Let's check if we have an index entry for that time */
3100 entry = gst_index_get_assoc_entry (parse->priv->index,
3101 parse->priv->index_id,
3102 before ? GST_INDEX_LOOKUP_BEFORE : GST_INDEX_LOOKUP_AFTER,
3103 GST_ASSOCIATION_FLAG_KEY_UNIT, GST_FORMAT_TIME, time);
3107 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &bytes);
3108 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &ts);
3110 GST_DEBUG_OBJECT (parse, "found index entry for %" GST_TIME_FORMAT
3111 " at %" GST_TIME_FORMAT ", offset %" G_GINT64_FORMAT,
3112 GST_TIME_ARGS (time), GST_TIME_ARGS (ts), bytes);
3114 GST_DEBUG_OBJECT (parse, "no index entry found for %" GST_TIME_FORMAT,
3115 GST_TIME_ARGS (time));
3118 ts = GST_CLOCK_TIME_NONE;
3121 GST_OBJECT_UNLOCK (parse);
3132 * gst_base_parse_handle_seek:
3133 * @parse: #GstBaseParse.
3134 * @event: #GstEvent.
3136 * Returns: TRUE if seek succeeded.
3139 gst_base_parse_handle_seek (GstBaseParse * parse, GstEvent * event)
3144 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
3145 gboolean flush, update, res = TRUE, accurate;
3146 gint64 cur, stop, seekpos, seekstop;
3147 GstSegment seeksegment = { 0, };
3148 GstFormat dstformat;
3149 GstClockTime start_ts;
3151 gst_event_parse_seek (event, &rate, &format, &flags,
3152 &cur_type, &cur, &stop_type, &stop);
3154 GST_DEBUG_OBJECT (parse, "seek to format %s, rate %f, "
3155 "start type %d at %" GST_TIME_FORMAT ", end type %d at %"
3156 GST_TIME_FORMAT, gst_format_get_name (format), rate,
3157 cur_type, GST_TIME_ARGS (cur), stop_type, GST_TIME_ARGS (stop));
3159 /* no negative rates in push mode */
3160 if (rate < 0.0 && parse->priv->pad_mode == GST_ACTIVATE_PUSH)
3163 if (cur_type != GST_SEEK_TYPE_SET ||
3164 (stop_type != GST_SEEK_TYPE_SET && stop_type != GST_SEEK_TYPE_NONE))
3167 /* For any format other than TIME, see if upstream handles
3168 * it directly or fail. For TIME, try upstream, but do it ourselves if
3169 * it fails upstream */
3170 if (format != GST_FORMAT_TIME) {
3171 /* default action delegates to upstream */
3174 gst_event_ref (event);
3175 if (gst_pad_push_event (parse->sinkpad, event)) {
3180 /* get flush flag */
3181 flush = flags & GST_SEEK_FLAG_FLUSH;
3183 /* copy segment, we need this because we still need the old
3184 * segment when we close the current segment. */
3185 memcpy (&seeksegment, &parse->segment, sizeof (GstSegment));
3187 GST_DEBUG_OBJECT (parse, "configuring seek");
3188 gst_segment_set_seek (&seeksegment, rate, format, flags,
3189 cur_type, cur, stop_type, stop, &update);
3191 /* accurate seeking implies seek tables are used to obtain position,
3192 * and the requested segment is maintained exactly, not adjusted any way */
3193 accurate = flags & GST_SEEK_FLAG_ACCURATE;
3195 /* maybe we can be accurate for (almost) free */
3196 gst_base_parse_find_offset (parse, seeksegment.last_stop, TRUE, &start_ts);
3197 if (seeksegment.last_stop <= start_ts + TARGET_DIFFERENCE) {
3198 GST_DEBUG_OBJECT (parse, "accurate seek possible");
3202 GstClockTime startpos = seeksegment.last_stop;
3204 /* accurate requested, so ... seek a bit before target */
3205 if (startpos < parse->priv->lead_in_ts)
3208 startpos -= parse->priv->lead_in_ts;
3209 seekpos = gst_base_parse_find_offset (parse, startpos, TRUE, &start_ts);
3210 seekstop = gst_base_parse_find_offset (parse, seeksegment.stop, FALSE,
3213 start_ts = seeksegment.last_stop;
3214 dstformat = GST_FORMAT_BYTES;
3215 if (!gst_pad_query_convert (parse->srcpad, format, seeksegment.last_stop,
3216 &dstformat, &seekpos))
3217 goto convert_failed;
3218 if (!gst_pad_query_convert (parse->srcpad, format, seeksegment.stop,
3219 &dstformat, &seekstop))
3220 goto convert_failed;
3223 GST_DEBUG_OBJECT (parse,
3224 "seek position %" G_GINT64_FORMAT " in bytes: %" G_GINT64_FORMAT,
3226 GST_DEBUG_OBJECT (parse,
3227 "seek stop %" G_GINT64_FORMAT " in bytes: %" G_GINT64_FORMAT,
3228 seeksegment.stop, seekstop);
3230 if (parse->priv->pad_mode == GST_ACTIVATE_PULL) {
3233 GST_DEBUG_OBJECT (parse, "seek in PULL mode");
3236 if (parse->srcpad) {
3237 GST_DEBUG_OBJECT (parse, "sending flush start");
3238 gst_pad_push_event (parse->srcpad, gst_event_new_flush_start ());
3239 /* unlock upstream pull_range */
3240 gst_pad_push_event (parse->sinkpad, gst_event_new_flush_start ());
3243 gst_pad_pause_task (parse->sinkpad);
3246 /* we should now be able to grab the streaming thread because we stopped it
3247 * with the above flush/pause code */
3248 GST_PAD_STREAM_LOCK (parse->sinkpad);
3250 /* save current position */
3251 last_stop = parse->segment.last_stop;
3252 GST_DEBUG_OBJECT (parse, "stopped streaming at %" G_GINT64_FORMAT,
3255 /* now commit to new position */
3257 /* prepare for streaming again */
3259 GST_DEBUG_OBJECT (parse, "sending flush stop");
3260 gst_pad_push_event (parse->srcpad, gst_event_new_flush_stop ());
3261 gst_pad_push_event (parse->sinkpad, gst_event_new_flush_stop ());
3262 gst_base_parse_clear_queues (parse);
3264 if (parse->close_segment)
3265 gst_event_unref (parse->close_segment);
3267 parse->close_segment = gst_event_new_new_segment (TRUE,
3268 parse->segment.rate, parse->segment.format,
3269 parse->segment.accum, parse->segment.last_stop, parse->segment.accum);
3271 /* keep track of our last_stop */
3272 seeksegment.accum = parse->segment.last_stop;
3274 GST_DEBUG_OBJECT (parse, "Created close seg format %d, "
3275 "start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT
3276 ", pos = %" GST_TIME_FORMAT, format,
3277 GST_TIME_ARGS (parse->segment.accum),
3278 GST_TIME_ARGS (parse->segment.last_stop),
3279 GST_TIME_ARGS (parse->segment.accum));
3282 memcpy (&parse->segment, &seeksegment, sizeof (GstSegment));
3284 /* store the newsegment event so it can be sent from the streaming thread. */
3285 if (parse->pending_segment)
3286 gst_event_unref (parse->pending_segment);
3288 /* This will be sent later in _loop() */
3289 parse->pending_segment =
3290 gst_event_new_new_segment (FALSE, parse->segment.rate,
3291 parse->segment.format, parse->segment.start, parse->segment.stop,
3292 parse->segment.start);
3294 GST_DEBUG_OBJECT (parse, "Created newseg format %d, "
3295 "start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT
3296 ", pos = %" GST_TIME_FORMAT, format,
3297 GST_TIME_ARGS (parse->segment.start),
3298 GST_TIME_ARGS (parse->segment.stop),
3299 GST_TIME_ARGS (parse->segment.start));
3301 /* one last chance in pull mode to stay accurate;
3302 * maybe scan and subclass can find where to go */
3305 GstClockTime ts = seeksegment.last_stop;
3307 gst_base_parse_locate_time (parse, &ts, &scanpos);
3311 /* running collected index now consists of several intervals,
3312 * so optimized check no longer possible */
3313 parse->priv->index_last_valid = FALSE;
3314 parse->priv->index_last_offset = 0;
3315 parse->priv->index_last_ts = 0;
3319 /* mark discont if we are going to stream from another position. */
3320 if (seekpos != parse->priv->offset) {
3321 GST_DEBUG_OBJECT (parse,
3322 "mark DISCONT, we did a seek to another position");
3323 parse->priv->offset = seekpos;
3324 parse->priv->last_offset = seekpos;
3325 parse->priv->seen_keyframe = FALSE;
3326 parse->priv->discont = TRUE;
3327 parse->priv->next_ts = start_ts;
3328 parse->priv->last_ts = GST_CLOCK_TIME_NONE;
3329 parse->priv->sync_offset = seekpos;
3330 parse->priv->exact_position = accurate;
3333 /* Start streaming thread if paused */
3334 gst_pad_start_task (parse->sinkpad,
3335 (GstTaskFunction) gst_base_parse_loop, parse->sinkpad);
3337 GST_PAD_STREAM_UNLOCK (parse->sinkpad);
3339 GstEvent *new_event;
3340 GstBaseParseSeek *seek;
3342 /* The only thing we need to do in PUSH-mode is to send the
3343 seek event (in bytes) to upstream. Segment / flush handling happens
3344 in corresponding src event handlers */
3345 GST_DEBUG_OBJECT (parse, "seek in PUSH mode");
3346 if (seekstop >= 0 && seekpos <= seekpos)
3348 new_event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flush,
3349 GST_SEEK_TYPE_SET, seekpos, stop_type, seekstop);
3351 /* store segment info so its precise details can be reconstructed when
3352 * receiving newsegment;
3353 * this matters for all details when accurate seeking,
3354 * is most useful to preserve NONE stop time otherwise */
3355 seek = g_new0 (GstBaseParseSeek, 1);
3356 seek->segment = seeksegment;
3357 seek->accurate = accurate;
3358 seek->offset = seekpos;
3359 seek->start_ts = start_ts;
3360 GST_OBJECT_LOCK (parse);
3361 /* less optimal, but preserves order */
3362 parse->priv->pending_seeks =
3363 g_slist_append (parse->priv->pending_seeks, seek);
3364 GST_OBJECT_UNLOCK (parse);
3366 res = gst_pad_push_event (parse->sinkpad, new_event);
3369 GST_OBJECT_LOCK (parse);
3370 parse->priv->pending_seeks =
3371 g_slist_remove (parse->priv->pending_seeks, seek);
3372 GST_OBJECT_UNLOCK (parse);
3383 GST_DEBUG_OBJECT (parse, "negative playback rates are not supported yet.");
3389 GST_DEBUG_OBJECT (parse, "unsupported seek type.");
3395 GST_DEBUG_OBJECT (parse, "conversion TIME to BYTES failed.");
3402 * gst_base_parse_handle_tag:
3403 * @parse: #GstBaseParse.
3404 * @event: #GstEvent.
3406 * Checks if bitrates are available from upstream tags so that we don't
3407 * override them later
3410 gst_base_parse_handle_tag (GstBaseParse * parse, GstEvent * event)
3412 GstTagList *taglist = NULL;
3415 gst_event_parse_tag (event, &taglist);
3417 if (gst_tag_list_get_uint (taglist, GST_TAG_MINIMUM_BITRATE, &tmp)) {
3418 GST_DEBUG_OBJECT (parse, "upstream min bitrate %d", tmp);
3419 parse->priv->post_min_bitrate = FALSE;
3421 if (gst_tag_list_get_uint (taglist, GST_TAG_BITRATE, &tmp)) {
3422 GST_DEBUG_OBJECT (parse, "upstream avg bitrate %d", tmp);
3423 parse->priv->post_avg_bitrate = FALSE;
3425 if (gst_tag_list_get_uint (taglist, GST_TAG_MAXIMUM_BITRATE, &tmp)) {
3426 GST_DEBUG_OBJECT (parse, "upstream max bitrate %d", tmp);
3427 parse->priv->post_max_bitrate = FALSE;
3432 * gst_base_parse_sink_setcaps:
3436 * Returns: TRUE if caps were accepted.
3439 gst_base_parse_sink_setcaps (GstPad * pad, GstCaps * caps)
3441 GstBaseParse *parse;
3442 GstBaseParseClass *klass;
3443 gboolean res = TRUE;
3445 parse = GST_BASE_PARSE (GST_PAD_PARENT (pad));
3446 klass = GST_BASE_PARSE_GET_CLASS (parse);
3448 GST_DEBUG_OBJECT (parse, "caps: %" GST_PTR_FORMAT, caps);
3450 if (klass->set_sink_caps)
3451 res = klass->set_sink_caps (parse, caps);
3453 return res && gst_pad_set_caps (pad, caps);
3457 gst_base_parse_set_index (GstElement * element, GstIndex * index)
3459 GstBaseParse *parse = GST_BASE_PARSE (element);
3461 GST_OBJECT_LOCK (parse);
3462 if (parse->priv->index)
3463 gst_object_unref (parse->priv->index);
3465 parse->priv->index = gst_object_ref (index);
3466 gst_index_get_writer_id (index, GST_OBJECT (element),
3467 &parse->priv->index_id);
3468 parse->priv->own_index = FALSE;
3470 parse->priv->index = NULL;
3471 GST_OBJECT_UNLOCK (parse);
3475 gst_base_parse_get_index (GstElement * element)
3477 GstBaseParse *parse = GST_BASE_PARSE (element);
3478 GstIndex *result = NULL;
3480 GST_OBJECT_LOCK (parse);
3481 if (parse->priv->index)
3482 result = gst_object_ref (parse->priv->index);
3483 GST_OBJECT_UNLOCK (parse);
3488 static GstStateChangeReturn
3489 gst_base_parse_change_state (GstElement * element, GstStateChange transition)
3491 GstBaseParse *parse;
3492 GstStateChangeReturn result;
3494 parse = GST_BASE_PARSE (element);
3496 switch (transition) {
3497 case GST_STATE_CHANGE_READY_TO_PAUSED:
3498 /* If this is our own index destroy it as the
3499 * old entries might be wrong for the new stream */
3500 if (parse->priv->own_index) {
3501 gst_object_unref (parse->priv->index);
3502 parse->priv->index = NULL;
3503 parse->priv->own_index = FALSE;
3506 /* If no index was created, generate one */
3507 if (G_UNLIKELY (!parse->priv->index)) {
3508 GST_DEBUG_OBJECT (parse, "no index provided creating our own");
3510 parse->priv->index = gst_index_factory_make ("memindex");
3511 gst_index_get_writer_id (parse->priv->index, GST_OBJECT (parse),
3512 &parse->priv->index_id);
3513 parse->priv->own_index = TRUE;
3520 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3522 switch (transition) {
3523 case GST_STATE_CHANGE_PAUSED_TO_READY:
3524 gst_base_parse_reset (parse);