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 (at least) 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. @framesize can always
86 * indicate a new minimum for current frame parsing. The passed buffer
87 * is read-only. Note that @check_valid_frame might receive any small
88 * amount of input data when leftover data is being drained (e.g. at EOS).
91 * After valid frame is found, it will be passed again to subclass with
92 * @parse_frame call. Now subclass is responsible for parsing the
93 * frame contents and setting the caps, and buffer metadata (e.g.
94 * buffer timestamp and duration, or keyframe if applicable).
95 * (although the latter can also be done by GstBaseParse if it is
96 * appropriately configured, see below). Frame is provided with
97 * timestamp derived from upstream (as much as generally possible),
98 * duration obtained form configuration (see below), and offset
99 * if meaningful (in pull mode).
102 * Finally the buffer can be pushed downstream and parsing loop starts
103 * over again. Just prior to actually pushing the buffer in question,
104 * it is passed to @pre_push_buffer which gives subclass yet one
105 * last chance to examine buffer metadata, or to send some custom (tag)
106 * events, or to perform custom (segment) filtering.
109 * During the parsing process GstBaseParseClass will handle both srcpad and
110 * sinkpad events. They will be passed to subclass if @event or
111 * @src_event callbacks have been provided.
116 * <itemizedlist><title>Shutdown phase</title>
118 * GstBaseParse class calls @stop to inform the subclass that data
119 * parsing will be stopped.
125 * Subclass is responsible for providing pad template caps for
126 * source and sink pads. The pads need to be named "sink" and "src". It also
127 * needs to set the fixed caps on srcpad, when the format is ensured (e.g.
128 * when base class calls subclass' @set_sink_caps function).
130 * This base class uses GST_FORMAT_DEFAULT as a meaning of frames. So,
131 * subclass conversion routine needs to know that conversion from
132 * GST_FORMAT_TIME to GST_FORMAT_DEFAULT must return the
133 * frame number that can be found from the given byte position.
135 * GstBaseParse uses subclasses conversion methods also for seeking (or otherwise
136 * uses its own default one, see also below).
138 * Subclass @start and @stop functions will be called to inform the beginning
139 * and end of data processing.
141 * Things that subclass need to take care of:
143 * <listitem><para>Provide pad templates</para></listitem>
145 * Fixate the source pad caps when appropriate
148 * Inform base class how big data chunks should be retrieved. This is
149 * done with @gst_base_parse_set_min_frame_size function.
152 * Examine data chunks passed to subclass with @check_valid_frame
153 * and tell if they contain a valid frame
156 * Set the caps and timestamp to frame that is passed to subclass with
157 * @parse_frame function.
159 * <listitem><para>Provide conversion functions</para></listitem>
161 * Update the duration information with @gst_base_parse_set_duration
164 * Optionally passthrough using @gst_base_parse_set_format
167 * Configure various baseparse parameters using @gst_base_parse_set_seek and
168 * @gst_base_parse_set_frame_props.
171 * In particular, if subclass is unable to determine a duration, but
172 * parsing (or specs) yields a frames per seconds rate, then this can be
173 * provided to GstBaseParse to enable it to cater for
174 * buffer time metadata (which will be taken from upstream as much as possible).
175 * Internally keeping track of frame durations and respective
176 * sizes that have been pushed provides GstBaseParse with an estimated bitrate.
177 * A default @convert (used if not overriden) will then use these
178 * rates to perform obvious conversions. These rates are also used to update
179 * (estimated) duration at regular frame intervals.
186 * - In push mode provide a queue of adapter-"queued" buffers for upstream
188 * - Queue buffers/events until caps are set
198 #include "gstbaseparse.h"
200 #define MIN_FRAMES_TO_POST_BITRATE 10
201 #define TARGET_DIFFERENCE (20 * GST_SECOND)
203 GST_DEBUG_CATEGORY_STATIC (gst_base_parse_debug);
204 #define GST_CAT_DEFAULT gst_base_parse_debug
206 /* Supported formats */
207 static GstFormat fmtlist[] = {
214 #define GST_BASE_PARSE_GET_PRIVATE(obj) \
215 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_PARSE, GstBaseParsePrivate))
217 struct _GstBaseParsePrivate
219 GstActivateMode pad_mode;
222 GstFormat duration_fmt;
223 gint64 estimated_duration;
225 guint min_frame_size;
227 guint fps_num, fps_den;
228 guint update_interval;
230 guint lead_in, lead_out;
231 GstClockTime lead_in_ts, lead_out_ts;
232 GstBaseParseSeekable seekable;
240 GstClockTime next_ts;
241 GstClockTime prev_ts;
242 GstClockTime frame_duration;
243 gboolean seen_keyframe;
248 guint64 data_bytecount;
249 guint64 acc_duration;
250 GstClockTime first_frame_ts;
251 gint64 first_frame_offset;
253 gboolean post_min_bitrate;
254 gboolean post_avg_bitrate;
255 gboolean post_max_bitrate;
259 guint posted_avg_bitrate;
261 GList *pending_events;
265 /* index entry storage, either ours or provided */
269 /* seek table entries only maintained if upstream is BYTE seekable */
270 gboolean upstream_seekable;
271 gboolean upstream_has_duration;
272 gint64 upstream_size;
273 /* minimum distance between two index entries */
274 GstClockTimeDiff idx_interval;
275 /* ts and offset of last entry added */
276 GstClockTime index_last_ts;
277 gint64 index_last_offset;
278 gboolean index_last_valid;
280 /* timestamps currently produced are accurate, e.g. started from 0 onwards */
281 gboolean exact_position;
282 /* seek events are temporarily kept to match them with newsegments */
283 GSList *pending_seeks;
285 /* reverse playback */
286 GSList *buffers_pending;
287 GSList *buffers_queued;
288 GstClockTime last_ts;
292 typedef struct _GstBaseParseSeek
297 GstClockTime start_ts;
300 #define GST_BASE_PARSE_PASSTHROUGH(parse) \
301 (parse->priv->format & GST_BASE_PARSE_FORMAT_PASSTHROUGH)
302 #define GST_BASE_PARSE_HAS_TIME(parse) \
303 (parse->priv->format & GST_BASE_PARSE_FORMAT_HAS_TIME)
306 static GstElementClass *parent_class = NULL;
308 static void gst_base_parse_class_init (GstBaseParseClass * klass);
309 static void gst_base_parse_init (GstBaseParse * parse,
310 GstBaseParseClass * klass);
313 gst_base_parse_get_type (void)
315 static GType base_parse_type = 0;
317 if (!base_parse_type) {
318 static const GTypeInfo base_parse_info = {
319 sizeof (GstBaseParseClass),
320 (GBaseInitFunc) NULL,
321 (GBaseFinalizeFunc) NULL,
322 (GClassInitFunc) gst_base_parse_class_init,
325 sizeof (GstBaseParse),
327 (GInstanceInitFunc) gst_base_parse_init,
330 base_parse_type = g_type_register_static (GST_TYPE_ELEMENT,
331 "GstAudioBaseParseBad", &base_parse_info, G_TYPE_FLAG_ABSTRACT);
333 return base_parse_type;
336 static void gst_base_parse_finalize (GObject * object);
338 static GstStateChangeReturn gst_base_parse_change_state (GstElement * element,
339 GstStateChange transition);
340 static void gst_base_parse_reset (GstBaseParse * parse);
342 static void gst_base_parse_set_index (GstElement * element, GstIndex * index);
343 static GstIndex *gst_base_parse_get_index (GstElement * element);
345 static gboolean gst_base_parse_sink_activate (GstPad * sinkpad);
346 static gboolean gst_base_parse_sink_activate_push (GstPad * pad,
348 static gboolean gst_base_parse_sink_activate_pull (GstPad * pad,
350 static gboolean gst_base_parse_handle_seek (GstBaseParse * parse,
352 static void gst_base_parse_handle_tag (GstBaseParse * parse, GstEvent * event);
354 static gboolean gst_base_parse_src_event (GstPad * pad, GstEvent * event);
355 static gboolean gst_base_parse_sink_event (GstPad * pad, GstEvent * event);
356 static gboolean gst_base_parse_query (GstPad * pad, GstQuery * query);
357 static gboolean gst_base_parse_sink_setcaps (GstPad * pad, GstCaps * caps);
358 static const GstQueryType *gst_base_parse_get_querytypes (GstPad * pad);
360 static GstFlowReturn gst_base_parse_chain (GstPad * pad, GstBuffer * buffer);
361 static void gst_base_parse_loop (GstPad * pad);
363 static gboolean gst_base_parse_check_frame (GstBaseParse * parse,
364 GstBaseParseFrame * frame, guint * framesize, gint * skipsize);
365 static GstFlowReturn gst_base_parse_parse_frame (GstBaseParse * parse,
366 GstBaseParseFrame * frame);
368 static gboolean gst_base_parse_sink_eventfunc (GstBaseParse * parse,
371 static gboolean gst_base_parse_src_eventfunc (GstBaseParse * parse,
374 static void gst_base_parse_drain (GstBaseParse * parse);
376 static void gst_base_parse_post_bitrates (GstBaseParse * parse,
377 gboolean post_min, gboolean post_avg, gboolean post_max);
379 static gint64 gst_base_parse_find_offset (GstBaseParse * parse,
380 GstClockTime time, gboolean before, GstClockTime * _ts);
381 static GstFlowReturn gst_base_parse_locate_time (GstBaseParse * parse,
382 GstClockTime * _time, gint64 * _offset);
384 static GstFlowReturn gst_base_parse_process_fragment (GstBaseParse * parse,
388 gst_base_parse_clear_queues (GstBaseParse * parse)
390 g_slist_foreach (parse->priv->buffers_queued, (GFunc) gst_buffer_unref, NULL);
391 g_slist_free (parse->priv->buffers_queued);
392 parse->priv->buffers_queued = NULL;
393 g_slist_foreach (parse->priv->buffers_pending, (GFunc) gst_buffer_unref,
395 g_slist_free (parse->priv->buffers_pending);
396 parse->priv->buffers_pending = NULL;
400 gst_base_parse_finalize (GObject * object)
402 GstBaseParse *parse = GST_BASE_PARSE (object);
405 g_object_unref (parse->adapter);
407 if (parse->pending_segment) {
408 p_ev = &parse->pending_segment;
409 gst_event_replace (p_ev, NULL);
411 if (parse->close_segment) {
412 p_ev = &parse->close_segment;
413 gst_event_replace (p_ev, NULL);
416 if (parse->priv->cache) {
417 gst_buffer_unref (parse->priv->cache);
418 parse->priv->cache = NULL;
421 g_list_foreach (parse->priv->pending_events, (GFunc) gst_mini_object_unref,
423 g_list_free (parse->priv->pending_events);
424 parse->priv->pending_events = NULL;
426 if (parse->priv->index) {
427 gst_object_unref (parse->priv->index);
428 parse->priv->index = NULL;
431 gst_base_parse_clear_queues (parse);
433 G_OBJECT_CLASS (parent_class)->finalize (object);
437 gst_base_parse_class_init (GstBaseParseClass * klass)
439 GObjectClass *gobject_class;
440 GstElementClass *gstelement_class;
442 gobject_class = G_OBJECT_CLASS (klass);
443 g_type_class_add_private (klass, sizeof (GstBaseParsePrivate));
444 parent_class = g_type_class_peek_parent (klass);
445 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_parse_finalize);
447 gstelement_class = (GstElementClass *) klass;
448 gstelement_class->change_state =
449 GST_DEBUG_FUNCPTR (gst_base_parse_change_state);
450 gstelement_class->set_index = GST_DEBUG_FUNCPTR (gst_base_parse_set_index);
451 gstelement_class->get_index = GST_DEBUG_FUNCPTR (gst_base_parse_get_index);
453 /* Default handlers */
454 klass->check_valid_frame = gst_base_parse_check_frame;
455 klass->parse_frame = gst_base_parse_parse_frame;
456 klass->src_event = gst_base_parse_src_eventfunc;
457 klass->convert = gst_base_parse_convert_default;
459 GST_DEBUG_CATEGORY_INIT (gst_base_parse_debug, "baseparse", 0,
460 "baseparse element");
464 gst_base_parse_init (GstBaseParse * parse, GstBaseParseClass * bclass)
466 GstPadTemplate *pad_template;
468 GST_DEBUG_OBJECT (parse, "gst_base_parse_init");
470 parse->priv = GST_BASE_PARSE_GET_PRIVATE (parse);
473 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "sink");
474 g_return_if_fail (pad_template != NULL);
475 parse->sinkpad = gst_pad_new_from_template (pad_template, "sink");
476 gst_pad_set_event_function (parse->sinkpad,
477 GST_DEBUG_FUNCPTR (gst_base_parse_sink_event));
478 gst_pad_set_setcaps_function (parse->sinkpad,
479 GST_DEBUG_FUNCPTR (gst_base_parse_sink_setcaps));
480 gst_pad_set_chain_function (parse->sinkpad,
481 GST_DEBUG_FUNCPTR (gst_base_parse_chain));
482 gst_pad_set_activate_function (parse->sinkpad,
483 GST_DEBUG_FUNCPTR (gst_base_parse_sink_activate));
484 gst_pad_set_activatepush_function (parse->sinkpad,
485 GST_DEBUG_FUNCPTR (gst_base_parse_sink_activate_push));
486 gst_pad_set_activatepull_function (parse->sinkpad,
487 GST_DEBUG_FUNCPTR (gst_base_parse_sink_activate_pull));
488 gst_element_add_pad (GST_ELEMENT (parse), parse->sinkpad);
490 GST_DEBUG_OBJECT (parse, "sinkpad created");
493 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
494 g_return_if_fail (pad_template != NULL);
495 parse->srcpad = gst_pad_new_from_template (pad_template, "src");
496 gst_pad_set_event_function (parse->srcpad,
497 GST_DEBUG_FUNCPTR (gst_base_parse_src_event));
498 gst_pad_set_query_type_function (parse->srcpad,
499 GST_DEBUG_FUNCPTR (gst_base_parse_get_querytypes));
500 gst_pad_set_query_function (parse->srcpad,
501 GST_DEBUG_FUNCPTR (gst_base_parse_query));
502 gst_pad_use_fixed_caps (parse->srcpad);
503 gst_element_add_pad (GST_ELEMENT (parse), parse->srcpad);
504 GST_DEBUG_OBJECT (parse, "src created");
506 parse->adapter = gst_adapter_new ();
508 parse->priv->pad_mode = GST_ACTIVATE_NONE;
511 gst_base_parse_reset (parse);
512 GST_DEBUG_OBJECT (parse, "init ok");
516 * gst_base_parse_frame_init:
517 * @parse: #GstBaseParse.
518 * @fmt: #GstBaseParseFrame.
520 * Sets a #GstBaseParseFrame to initial state. Currently this means
521 * all fields are zero-ed.
524 gst_base_parse_frame_init (GstBaseParse * parse, GstBaseParseFrame * frame)
526 memset (frame, 0, sizeof (*frame));
529 /* clear == frame no longer to be used following this */
531 gst_base_parse_frame_clear (GstBaseParse * parse, GstBaseParseFrame * frame)
533 /* limited for now */
535 gst_buffer_unref (frame->buffer);
536 frame->buffer = NULL;
541 gst_base_parse_frame_update (GstBaseParse * parse, GstBaseParseFrame * frame,
544 gst_buffer_replace (&frame->buffer, buf);
545 if (parse->priv->drain) {
546 frame->flags |= GST_BASE_PARSE_FRAME_FLAG_DRAIN;
548 frame->flags &= ~(GST_BASE_PARSE_FRAME_FLAG_DRAIN);
550 /* losing sync is pretty much a discont (and vice versa), no ? */
551 if (!parse->priv->discont) {
552 frame->flags |= GST_BASE_PARSE_FRAME_FLAG_SYNC;
554 frame->flags &= ~(GST_BASE_PARSE_FRAME_FLAG_SYNC);
559 gst_base_parse_reset (GstBaseParse * parse)
561 GST_OBJECT_LOCK (parse);
562 gst_segment_init (&parse->segment, GST_FORMAT_TIME);
563 parse->priv->duration = -1;
564 parse->priv->min_frame_size = 1;
565 parse->priv->discont = TRUE;
566 parse->priv->flushing = FALSE;
567 parse->priv->offset = 0;
568 parse->priv->sync_offset = 0;
569 parse->priv->update_interval = 50;
570 parse->priv->fps_num = parse->priv->fps_den = 0;
571 parse->priv->frame_duration = GST_CLOCK_TIME_NONE;
572 parse->priv->lead_in = parse->priv->lead_out = 0;
573 parse->priv->lead_in_ts = parse->priv->lead_out_ts = 0;
574 parse->priv->seekable = GST_BASE_PARSE_SEEK_DEFAULT;
575 parse->priv->bitrate = 0;
576 parse->priv->framecount = 0;
577 parse->priv->bytecount = 0;
578 parse->priv->acc_duration = 0;
579 parse->priv->first_frame_ts = GST_CLOCK_TIME_NONE;
580 parse->priv->first_frame_offset = -1;
581 parse->priv->estimated_duration = -1;
582 parse->priv->next_ts = 0;
583 parse->priv->format = 0;
584 parse->priv->post_min_bitrate = TRUE;
585 parse->priv->post_avg_bitrate = TRUE;
586 parse->priv->post_max_bitrate = TRUE;
587 parse->priv->min_bitrate = G_MAXUINT;
588 parse->priv->max_bitrate = 0;
589 parse->priv->avg_bitrate = 0;
590 parse->priv->posted_avg_bitrate = 0;
592 parse->priv->index_last_ts = GST_CLOCK_TIME_NONE;
593 parse->priv->index_last_offset = -1;
594 parse->priv->index_last_valid = TRUE;
595 parse->priv->upstream_seekable = FALSE;
596 parse->priv->upstream_size = 0;
597 parse->priv->upstream_has_duration = FALSE;
598 parse->priv->idx_interval = 0;
599 parse->priv->exact_position = TRUE;
600 parse->priv->seen_keyframe = FALSE;
602 parse->priv->last_ts = GST_CLOCK_TIME_NONE;
603 parse->priv->last_offset = 0;
605 if (parse->pending_segment) {
606 gst_event_unref (parse->pending_segment);
607 parse->pending_segment = NULL;
610 g_list_foreach (parse->priv->pending_events, (GFunc) gst_mini_object_unref,
612 g_list_free (parse->priv->pending_events);
613 parse->priv->pending_events = NULL;
615 if (parse->priv->cache) {
616 gst_buffer_unref (parse->priv->cache);
617 parse->priv->cache = NULL;
620 g_slist_foreach (parse->priv->pending_seeks, (GFunc) g_free, NULL);
621 g_slist_free (parse->priv->pending_seeks);
622 parse->priv->pending_seeks = NULL;
624 GST_OBJECT_UNLOCK (parse);
628 * gst_base_parse_check_frame:
629 * @parse: #GstBaseParse.
630 * @buffer: GstBuffer.
631 * @framesize: This will be set to tell the found frame size in bytes.
632 * @skipsize: Output parameter that tells how much data needs to be skipped
633 * in order to find the following frame header.
635 * Default callback for check_valid_frame.
637 * Returns: Always TRUE.
640 gst_base_parse_check_frame (GstBaseParse * parse,
641 GstBaseParseFrame * frame, guint * framesize, gint * skipsize)
643 *framesize = GST_BUFFER_SIZE (frame->buffer);
650 * gst_base_parse_parse_frame:
651 * @parse: #GstBaseParse.
652 * @buffer: #GstBuffer.
654 * Default callback for parse_frame.
657 gst_base_parse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
659 GstBuffer *buffer = frame->buffer;
661 if (!GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
662 GST_CLOCK_TIME_IS_VALID (parse->priv->next_ts)) {
663 GST_BUFFER_TIMESTAMP (buffer) = parse->priv->next_ts;
665 if (!GST_BUFFER_DURATION_IS_VALID (buffer) &&
666 GST_CLOCK_TIME_IS_VALID (parse->priv->frame_duration)) {
667 GST_BUFFER_DURATION (buffer) = parse->priv->frame_duration;
673 * gst_base_parse_convert:
674 * @parse: #GstBaseParse.
675 * @src_format: #GstFormat describing the source format.
676 * @src_value: Source value to be converted.
677 * @dest_format: #GstFormat defining the converted format.
678 * @dest_value: Pointer where the conversion result will be put.
680 * Converts using configured "convert" vmethod in #GstBaseParse class.
682 * Returns: TRUE if conversion was successful.
685 gst_base_parse_convert (GstBaseParse * parse,
686 GstFormat src_format,
687 gint64 src_value, GstFormat dest_format, gint64 * dest_value)
689 GstBaseParseClass *klass = GST_BASE_PARSE_GET_CLASS (parse);
692 g_return_val_if_fail (dest_value != NULL, FALSE);
697 ret = klass->convert (parse, src_format, src_value, dest_format, dest_value);
699 #ifndef GST_DISABLE_GST_DEBUG
702 if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES) {
703 GST_LOG_OBJECT (parse,
704 "TIME -> BYTES: %" GST_TIME_FORMAT " -> %" G_GINT64_FORMAT,
705 GST_TIME_ARGS (src_value), *dest_value);
706 } else if (dest_format == GST_FORMAT_TIME &&
707 src_format == GST_FORMAT_BYTES) {
708 GST_LOG_OBJECT (parse,
709 "BYTES -> TIME: %" G_GINT64_FORMAT " -> %" GST_TIME_FORMAT,
710 src_value, GST_TIME_ARGS (*dest_value));
712 GST_LOG_OBJECT (parse,
713 "%s -> %s: %" G_GINT64_FORMAT " -> %" G_GINT64_FORMAT,
714 GST_STR_NULL (gst_format_get_name (src_format)),
715 GST_STR_NULL (gst_format_get_name (dest_format)),
716 src_value, *dest_value);
719 GST_DEBUG_OBJECT (parse, "conversion failed");
728 * gst_base_parse_sink_event:
729 * @pad: #GstPad that received the event.
730 * @event: #GstEvent to be handled.
732 * Handler for sink pad events.
734 * Returns: TRUE if the event was handled.
737 gst_base_parse_sink_event (GstPad * pad, GstEvent * event)
740 GstBaseParseClass *bclass;
741 gboolean handled = FALSE;
744 parse = GST_BASE_PARSE (gst_pad_get_parent (pad));
745 bclass = GST_BASE_PARSE_GET_CLASS (parse);
747 GST_DEBUG_OBJECT (parse, "handling event %d, %s", GST_EVENT_TYPE (event),
748 GST_EVENT_TYPE_NAME (event));
750 /* Cache all events except EOS, NEWSEGMENT and FLUSH_STOP if we have a
752 if (parse->pending_segment && GST_EVENT_TYPE (event) != GST_EVENT_EOS
753 && GST_EVENT_TYPE (event) != GST_EVENT_NEWSEGMENT
754 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_START
755 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
757 if (GST_EVENT_TYPE (event) == GST_EVENT_TAG)
758 /* See if any bitrate tags were posted */
759 gst_base_parse_handle_tag (parse, event);
761 parse->priv->pending_events =
762 g_list_append (parse->priv->pending_events, event);
766 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS &&
767 parse->priv->framecount < MIN_FRAMES_TO_POST_BITRATE)
768 /* We've not posted bitrate tags yet - do so now */
769 gst_base_parse_post_bitrates (parse, TRUE, TRUE, TRUE);
772 handled = bclass->event (parse, event);
775 handled = gst_base_parse_sink_eventfunc (parse, event);
778 ret = gst_pad_event_default (pad, event);
781 gst_object_unref (parse);
782 GST_DEBUG_OBJECT (parse, "event handled");
788 * gst_base_parse_sink_eventfunc:
789 * @parse: #GstBaseParse.
790 * @event: #GstEvent to be handled.
792 * Element-level event handler function.
794 * The event will be unreffed only if it has been handled and this
795 * function returns %TRUE
797 * Returns: %TRUE if the event was handled and not need forwarding.
800 gst_base_parse_sink_eventfunc (GstBaseParse * parse, GstEvent * event)
802 gboolean handled = FALSE;
805 switch (GST_EVENT_TYPE (event)) {
806 case GST_EVENT_NEWSEGMENT:
808 gdouble rate, applied_rate;
810 gint64 start, stop, pos, next_ts, offset = 0;
813 gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
814 &format, &start, &stop, &pos);
816 GST_DEBUG_OBJECT (parse, "newseg rate %g, applied rate %g, "
817 "format %d, start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT
818 ", pos = %" GST_TIME_FORMAT, rate, applied_rate, format,
819 GST_TIME_ARGS (start), GST_TIME_ARGS (stop), GST_TIME_ARGS (pos));
821 if (format == GST_FORMAT_BYTES) {
822 GstClockTime seg_start, seg_stop;
823 GstBaseParseSeek *seek = NULL;
826 /* stop time is allowed to be open-ended, but not start & pos */
827 seg_stop = GST_CLOCK_TIME_NONE;
831 GST_OBJECT_LOCK (parse);
832 for (node = parse->priv->pending_seeks; node; node = node->next) {
833 GstBaseParseSeek *tmp = node->data;
835 if (tmp->offset == pos) {
840 parse->priv->pending_seeks =
841 g_slist_remove (parse->priv->pending_seeks, seek);
842 GST_OBJECT_UNLOCK (parse);
845 GST_DEBUG_OBJECT (parse,
846 "Matched newsegment to%s seek: %" GST_SEGMENT_FORMAT,
847 seek->accurate ? " accurate" : "", &seek->segment);
848 seg_start = seek->segment.start;
849 seg_stop = seek->segment.stop;
850 next_ts = seek->start_ts;
851 parse->priv->exact_position = seek->accurate;
854 /* best attempt convert */
855 /* as these are only estimates, stop is kept open-ended to avoid
856 * premature cutting */
857 gst_base_parse_convert (parse, GST_FORMAT_BYTES, start,
858 GST_FORMAT_TIME, (gint64 *) & seg_start);
859 parse->priv->exact_position = (start == 0);
863 gst_event_unref (event);
864 event = gst_event_new_new_segment_full (update, rate, applied_rate,
865 GST_FORMAT_TIME, seg_start, seg_stop, seg_start);
866 format = GST_FORMAT_TIME;
869 GST_DEBUG_OBJECT (parse, "Converted incoming segment to TIME. "
870 "start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT,
871 GST_TIME_ARGS (seg_start), GST_TIME_ARGS (seg_stop));
872 } else if (format != GST_FORMAT_TIME) {
873 /* Unknown incoming segment format. Output a default open-ended
875 gst_event_unref (event);
876 event = gst_event_new_new_segment_full (update, rate, applied_rate,
877 GST_FORMAT_TIME, 0, GST_CLOCK_TIME_NONE, 0);
878 format = GST_FORMAT_TIME;
880 stop = GST_CLOCK_TIME_NONE;
882 /* not considered BYTE seekable if it is talking to us in TIME,
883 * whatever else it might claim */
884 parse->priv->upstream_seekable = FALSE;
888 gst_segment_set_newsegment_full (&parse->segment, update, rate,
889 applied_rate, format, start, stop, start);
891 /* save the segment for later, right before we push a new buffer so that
892 * the caps are fixed and the next linked element can receive
894 eventp = &parse->pending_segment;
895 gst_event_replace (eventp, event);
896 gst_event_unref (event);
899 /* but finish the current segment */
900 GST_DEBUG_OBJECT (parse, "draining current segment");
901 if (parse->segment.rate > 0.0)
902 gst_base_parse_drain (parse);
904 gst_base_parse_process_fragment (parse, FALSE);
905 gst_adapter_clear (parse->adapter);
906 parse->priv->offset = offset;
907 parse->priv->sync_offset = offset;
908 parse->priv->next_ts = next_ts;
909 parse->priv->last_ts = GST_CLOCK_TIME_NONE;
910 parse->priv->discont = TRUE;
911 parse->priv->seen_keyframe = FALSE;
915 case GST_EVENT_FLUSH_START:
916 parse->priv->flushing = TRUE;
917 handled = gst_pad_push_event (parse->srcpad, gst_event_ref (event));
919 gst_event_unref (event);
920 /* Wait for _chain() to exit by taking the srcpad STREAM_LOCK */
921 GST_PAD_STREAM_LOCK (parse->srcpad);
922 GST_PAD_STREAM_UNLOCK (parse->srcpad);
926 case GST_EVENT_FLUSH_STOP:
927 gst_adapter_clear (parse->adapter);
928 gst_base_parse_clear_queues (parse);
929 parse->priv->flushing = FALSE;
930 parse->priv->discont = TRUE;
931 parse->priv->last_ts = GST_CLOCK_TIME_NONE;
935 if (parse->segment.rate > 0.0)
936 gst_base_parse_drain (parse);
938 gst_base_parse_process_fragment (parse, FALSE);
940 /* If we STILL have zero frames processed, fire an error */
941 if (parse->priv->framecount == 0) {
942 GST_ELEMENT_ERROR (parse, STREAM, WRONG_TYPE,
943 ("No valid frames found before end of stream"), (NULL));
945 /* newsegment before eos */
946 if (parse->pending_segment) {
947 gst_pad_push_event (parse->srcpad, parse->pending_segment);
948 parse->pending_segment = NULL;
961 * gst_base_parse_src_event:
962 * @pad: #GstPad that received the event.
963 * @event: #GstEvent that was received.
965 * Handler for source pad events.
967 * Returns: TRUE if the event was handled.
970 gst_base_parse_src_event (GstPad * pad, GstEvent * event)
973 GstBaseParseClass *bclass;
974 gboolean handled = FALSE;
977 parse = GST_BASE_PARSE (gst_pad_get_parent (pad));
978 bclass = GST_BASE_PARSE_GET_CLASS (parse);
980 GST_DEBUG_OBJECT (parse, "event %d, %s", GST_EVENT_TYPE (event),
981 GST_EVENT_TYPE_NAME (event));
983 if (bclass->src_event)
984 handled = bclass->src_event (parse, event);
987 ret = gst_pad_event_default (pad, event);
989 gst_object_unref (parse);
995 * gst_base_parse_src_eventfunc:
996 * @parse: #GstBaseParse.
997 * @event: #GstEvent that was received.
999 * Default srcpad event handler.
1001 * Returns: TRUE if the event was handled and can be dropped.
1004 gst_base_parse_src_eventfunc (GstBaseParse * parse, GstEvent * event)
1006 gboolean handled = FALSE;
1008 switch (GST_EVENT_TYPE (event)) {
1009 case GST_EVENT_SEEK:
1011 if (parse->priv->seekable > GST_BASE_PARSE_SEEK_NONE) {
1012 handled = gst_base_parse_handle_seek (parse, event);
1024 * gst_base_parse_convert_default:
1025 * @parse: #GstBaseParse.
1026 * @src_format: #GstFormat describing the source format.
1027 * @src_value: Source value to be converted.
1028 * @dest_format: #GstFormat defining the converted format.
1029 * @dest_value: Pointer where the conversion result will be put.
1031 * Default implementation of "convert" vmethod in #GstBaseParse class.
1033 * Returns: TRUE if conversion was successful.
1036 gst_base_parse_convert_default (GstBaseParse * parse,
1037 GstFormat src_format,
1038 gint64 src_value, GstFormat dest_format, gint64 * dest_value)
1040 gboolean ret = FALSE;
1041 guint64 bytes, duration;
1043 if (G_UNLIKELY (src_format == dest_format)) {
1044 *dest_value = src_value;
1048 if (G_UNLIKELY (src_value == -1)) {
1053 if (G_UNLIKELY (src_value == 0)) {
1058 /* need at least some frames */
1059 if (!parse->priv->framecount)
1062 duration = parse->priv->acc_duration / GST_MSECOND;
1063 bytes = parse->priv->bytecount;
1065 if (G_UNLIKELY (!duration || !bytes))
1068 if (src_format == GST_FORMAT_BYTES) {
1069 if (dest_format == GST_FORMAT_TIME) {
1070 /* BYTES -> TIME conversion */
1071 GST_DEBUG_OBJECT (parse, "converting bytes -> time");
1072 *dest_value = gst_util_uint64_scale (src_value, duration, bytes);
1073 *dest_value *= GST_MSECOND;
1074 GST_DEBUG_OBJECT (parse, "conversion result: %" G_GINT64_FORMAT " ms",
1075 *dest_value / GST_MSECOND);
1078 } else if (src_format == GST_FORMAT_TIME) {
1079 if (dest_format == GST_FORMAT_BYTES) {
1080 GST_DEBUG_OBJECT (parse, "converting time -> bytes");
1081 *dest_value = gst_util_uint64_scale (src_value / GST_MSECOND, bytes,
1083 GST_DEBUG_OBJECT (parse,
1084 "time %" G_GINT64_FORMAT " ms in bytes = %" G_GINT64_FORMAT,
1085 src_value / GST_MSECOND, *dest_value);
1088 } else if (src_format == GST_FORMAT_DEFAULT) {
1089 /* DEFAULT == frame-based */
1090 if (dest_format == GST_FORMAT_TIME) {
1091 if (parse->priv->fps_den) {
1092 *dest_value = gst_util_uint64_scale (src_value,
1093 GST_SECOND * parse->priv->fps_den, parse->priv->fps_num);
1096 } else if (dest_format == GST_FORMAT_BYTES) {
1104 * gst_base_parse_update_duration:
1105 * @parse: #GstBaseParse.
1109 gst_base_parse_update_duration (GstBaseParse * aacparse)
1112 GstBaseParse *parse;
1114 parse = GST_BASE_PARSE (aacparse);
1116 peer = gst_pad_get_peer (parse->sinkpad);
1118 GstFormat pformat = GST_FORMAT_BYTES;
1119 gboolean qres = FALSE;
1120 gint64 ptot, dest_value;
1122 qres = gst_pad_query_duration (peer, &pformat, &ptot);
1123 gst_object_unref (GST_OBJECT (peer));
1125 if (gst_base_parse_convert (parse, pformat, ptot,
1126 GST_FORMAT_TIME, &dest_value))
1127 parse->priv->estimated_duration = dest_value;
1133 gst_base_parse_post_bitrates (GstBaseParse * parse, gboolean post_min,
1134 gboolean post_avg, gboolean post_max)
1136 GstTagList *taglist = gst_tag_list_new ();
1138 if (post_min && parse->priv->post_min_bitrate)
1139 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE,
1140 GST_TAG_MINIMUM_BITRATE, parse->priv->min_bitrate, NULL);
1142 if (post_avg && parse->priv->post_avg_bitrate) {
1143 parse->priv->posted_avg_bitrate = parse->priv->avg_bitrate;
1144 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_BITRATE,
1145 parse->priv->avg_bitrate, NULL);
1148 if (post_max && parse->priv->post_max_bitrate)
1149 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE,
1150 GST_TAG_MAXIMUM_BITRATE, parse->priv->max_bitrate, NULL);
1152 GST_DEBUG_OBJECT (parse, "Updated bitrates. Min: %u, Avg: %u, Max: %u",
1153 parse->priv->min_bitrate, parse->priv->avg_bitrate,
1154 parse->priv->max_bitrate);
1156 gst_element_found_tags_for_pad (GST_ELEMENT (parse), parse->srcpad, taglist);
1160 * gst_base_parse_update_bitrates:
1161 * @parse: #GstBaseParse.
1162 * @buffer: Current frame as a #GstBuffer
1164 * Keeps track of the minimum and maximum bitrates, and also maintains a
1165 * running average bitrate of the stream so far.
1168 gst_base_parse_update_bitrates (GstBaseParse * parse, GstBaseParseFrame * frame)
1170 /* Only update the tag on a 10 kbps delta */
1171 static const gint update_threshold = 10000;
1173 GstBaseParseClass *klass;
1174 guint64 data_len, frame_dur;
1175 gint overhead, frame_bitrate, old_avg_bitrate;
1176 gboolean update_min = FALSE, update_avg = FALSE, update_max = FALSE;
1177 GstBuffer *buffer = frame->buffer;
1179 klass = GST_BASE_PARSE_GET_CLASS (parse);
1181 overhead = frame->overhead;
1185 data_len = GST_BUFFER_SIZE (buffer) - overhead;
1186 parse->priv->data_bytecount += data_len;
1188 /* duration should be valid by now,
1189 * either set by subclass or maybe based on fps settings */
1190 if (GST_BUFFER_DURATION_IS_VALID (buffer) && parse->priv->acc_duration != 0) {
1191 /* Calculate duration of a frame from buffer properties */
1192 frame_dur = GST_BUFFER_DURATION (buffer);
1193 parse->priv->avg_bitrate = (8 * parse->priv->data_bytecount * GST_SECOND) /
1194 parse->priv->acc_duration;
1197 /* No way to figure out frame duration (is this even possible?) */
1201 /* override if subclass provided bitrate, e.g. metadata based */
1202 if (parse->priv->bitrate) {
1203 parse->priv->avg_bitrate = parse->priv->bitrate;
1204 /* spread this (confirmed) info ASAP */
1205 if (parse->priv->posted_avg_bitrate != parse->priv->avg_bitrate)
1206 gst_base_parse_post_bitrates (parse, FALSE, TRUE, FALSE);
1210 frame_bitrate = (8 * data_len * GST_SECOND) / frame_dur;
1214 GST_LOG_OBJECT (parse, "frame bitrate %u, avg bitrate %u", frame_bitrate,
1215 parse->priv->avg_bitrate);
1217 if (parse->priv->framecount < MIN_FRAMES_TO_POST_BITRATE) {
1219 } else if (parse->priv->framecount == MIN_FRAMES_TO_POST_BITRATE) {
1220 /* always post all at threshold time */
1221 update_min = update_max = update_avg = TRUE;
1223 if (frame_bitrate < parse->priv->min_bitrate) {
1224 parse->priv->min_bitrate = frame_bitrate;
1228 if (frame_bitrate > parse->priv->max_bitrate) {
1229 parse->priv->max_bitrate = frame_bitrate;
1233 old_avg_bitrate = parse->priv->posted_avg_bitrate;
1234 if ((gint) (old_avg_bitrate - parse->priv->avg_bitrate) > update_threshold
1235 || (gint) (parse->priv->avg_bitrate - old_avg_bitrate) >
1240 if ((update_min || update_avg || update_max))
1241 gst_base_parse_post_bitrates (parse, update_min, update_avg, update_max);
1243 /* If average bitrate changes that much and no valid (time) duration provided,
1244 * then post a new duration message so applications can update their cached
1246 if (update_avg && !(parse->priv->duration_fmt == GST_FORMAT_TIME &&
1247 GST_CLOCK_TIME_IS_VALID (parse->priv->duration)))
1248 gst_element_post_message (GST_ELEMENT (parse),
1249 gst_message_new_duration (GST_OBJECT (parse), GST_FORMAT_TIME, -1));
1256 * gst_base_parse_add_index_entry:
1257 * @parse: #GstBaseParse.
1258 * @offset: offset of entry
1259 * @ts: timestamp associated with offset
1260 * @key: whether entry refers to keyframe
1261 * @force: add entry disregarding sanity checks
1263 * Adds an entry to the index associating @offset to @ts. It is recommended
1264 * to only add keyframe entries. @force allows to bypass checks, such as
1265 * whether the stream is (upstream) seekable, another entry is already "close"
1266 * to the new entry, etc.
1268 * Returns: #gboolean indicating whether entry was added
1271 gst_base_parse_add_index_entry (GstBaseParse * parse, guint64 offset,
1272 GstClockTime ts, gboolean key, gboolean force)
1274 gboolean ret = FALSE;
1275 GstIndexAssociation associations[2];
1277 GST_LOG_OBJECT (parse, "Adding key=%d index entry %" GST_TIME_FORMAT
1278 " @ offset 0x%08" G_GINT64_MODIFIER "x", key, GST_TIME_ARGS (ts), offset);
1280 if (G_LIKELY (!force)) {
1282 if (!parse->priv->upstream_seekable) {
1283 GST_DEBUG_OBJECT (parse, "upstream not seekable; discarding");
1287 /* FIXME need better helper data structure that handles these issues
1288 * related to ongoing collecting of index entries */
1289 if (parse->priv->index_last_offset >= (gint64) offset) {
1290 GST_DEBUG_OBJECT (parse, "already have entries up to offset "
1291 "0x%08" G_GINT64_MODIFIER "x", parse->priv->index_last_offset);
1295 if (GST_CLOCK_TIME_IS_VALID (parse->priv->index_last_ts) &&
1296 GST_CLOCK_DIFF (parse->priv->index_last_ts, ts) <
1297 parse->priv->idx_interval) {
1298 GST_DEBUG_OBJECT (parse, "entry too close to last time %" GST_TIME_FORMAT,
1299 GST_TIME_ARGS (parse->priv->index_last_ts));
1303 /* if last is not really the last one */
1304 if (!parse->priv->index_last_valid) {
1305 GstClockTime prev_ts;
1307 gst_base_parse_find_offset (parse, ts, TRUE, &prev_ts);
1308 if (GST_CLOCK_DIFF (prev_ts, ts) < parse->priv->idx_interval) {
1309 GST_DEBUG_OBJECT (parse,
1310 "entry too close to existing entry %" GST_TIME_FORMAT,
1311 GST_TIME_ARGS (prev_ts));
1312 parse->priv->index_last_offset = offset;
1313 parse->priv->index_last_ts = ts;
1319 associations[0].format = GST_FORMAT_TIME;
1320 associations[0].value = ts;
1321 associations[1].format = GST_FORMAT_BYTES;
1322 associations[1].value = offset;
1324 /* index might change on-the-fly, although that would be nutty app ... */
1325 GST_OBJECT_LOCK (parse);
1326 gst_index_add_associationv (parse->priv->index, parse->priv->index_id,
1327 (key) ? GST_ASSOCIATION_FLAG_KEY_UNIT : GST_ASSOCIATION_FLAG_NONE,
1328 2, (const GstIndexAssociation *) &associations);
1329 GST_OBJECT_UNLOCK (parse);
1332 parse->priv->index_last_offset = offset;
1333 parse->priv->index_last_ts = ts;
1342 /* check for seekable upstream, above and beyond a mere query */
1344 gst_base_parse_check_seekability (GstBaseParse * parse)
1347 gboolean seekable = FALSE;
1348 gint64 start = -1, stop = -1;
1349 guint idx_interval = 0;
1351 query = gst_query_new_seeking (GST_FORMAT_BYTES);
1352 if (!gst_pad_peer_query (parse->sinkpad, query)) {
1353 GST_DEBUG_OBJECT (parse, "seeking query failed");
1357 gst_query_parse_seeking (query, NULL, &seekable, &start, &stop);
1359 /* try harder to query upstream size if we didn't get it the first time */
1360 if (seekable && stop == -1) {
1361 GstFormat fmt = GST_FORMAT_BYTES;
1363 GST_DEBUG_OBJECT (parse, "doing duration query to fix up unset stop");
1364 gst_pad_query_peer_duration (parse->sinkpad, &fmt, &stop);
1367 /* if upstream doesn't know the size, it's likely that it's not seekable in
1368 * practice even if it technically may be seekable */
1369 if (seekable && (start != 0 || stop <= start)) {
1370 GST_DEBUG_OBJECT (parse, "seekable but unknown start/stop -> disable");
1374 /* let's not put every single frame into our index */
1376 if (stop < 10 * 1024 * 1024)
1378 else if (stop < 100 * 1024 * 1024)
1381 idx_interval = 1000;
1385 gst_query_unref (query);
1387 GST_DEBUG_OBJECT (parse, "seekable: %d (%" G_GUINT64_FORMAT " - %"
1388 G_GUINT64_FORMAT ")", seekable, start, stop);
1389 parse->priv->upstream_seekable = seekable;
1390 parse->priv->upstream_size = seekable ? stop : 0;
1392 GST_DEBUG_OBJECT (parse, "idx_interval: %ums", idx_interval);
1393 parse->priv->idx_interval = idx_interval * GST_MSECOND;
1396 /* some misc checks on upstream */
1398 gst_base_parse_check_upstream (GstBaseParse * parse)
1400 GstFormat fmt = GST_FORMAT_TIME;
1403 if (gst_pad_query_peer_duration (parse->sinkpad, &fmt, &stop))
1404 if (GST_CLOCK_TIME_IS_VALID (stop) && stop) {
1405 /* upstream has one, accept it also, and no further updates */
1406 gst_base_parse_set_duration (parse, GST_FORMAT_TIME, stop, 0);
1407 parse->priv->upstream_has_duration = TRUE;
1410 GST_DEBUG_OBJECT (parse, "upstream_has_duration: %d",
1411 parse->priv->upstream_has_duration);
1414 /* checks src caps to determine if dealing with audio or video */
1415 /* TODO maybe forego automagic stuff and let subclass configure it ? */
1417 gst_base_parse_check_media (GstBaseParse * parse)
1422 caps = GST_PAD_CAPS (parse->srcpad);
1423 if (G_LIKELY (caps) && (s = gst_caps_get_structure (caps, 0))) {
1424 parse->priv->is_video =
1425 g_str_has_prefix (gst_structure_get_name (s), "video");
1427 /* historical default */
1428 parse->priv->is_video = FALSE;
1431 GST_DEBUG_OBJECT (parse, "media is video == %d", parse->priv->is_video);
1435 * gst_base_parse_handle_and_push_buffer:
1436 * @parse: #GstBaseParse.
1437 * @klass: #GstBaseParseClass.
1438 * @buffer: #GstBuffer.
1440 * Parses the frame from given buffer and pushes it forward. Also performs
1441 * timestamp handling and checks the segment limits.
1443 * This is called with srcpad STREAM_LOCK held.
1445 * Returns: #GstFlowReturn
1447 static GstFlowReturn
1448 gst_base_parse_handle_and_push_frame (GstBaseParse * parse,
1449 GstBaseParseClass * klass, GstBaseParseFrame * frame)
1455 g_return_val_if_fail (frame != NULL, GST_FLOW_ERROR);
1457 buffer = frame->buffer;
1459 if (parse->priv->discont) {
1460 GST_DEBUG_OBJECT (parse, "marking DISCONT");
1461 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1462 parse->priv->discont = FALSE;
1465 /* some one-time start-up */
1466 if (G_UNLIKELY (!parse->priv->framecount)) {
1467 gst_base_parse_check_seekability (parse);
1468 gst_base_parse_check_upstream (parse);
1471 GST_LOG_OBJECT (parse,
1472 "parsing frame at offset %" G_GUINT64_FORMAT
1473 " (%#" G_GINT64_MODIFIER "x) of size %d",
1474 GST_BUFFER_OFFSET (buffer), GST_BUFFER_OFFSET (buffer),
1475 GST_BUFFER_SIZE (buffer));
1477 /* use default handler to provide initial (upstream) metadata */
1478 gst_base_parse_parse_frame (parse, frame);
1480 /* store offset as it might get overwritten */
1481 offset = GST_BUFFER_OFFSET (buffer);
1482 ret = klass->parse_frame (parse, frame);
1484 buffer = frame->buffer;
1485 /* subclass must play nice */
1486 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
1488 /* check if subclass/format can provide ts.
1489 * If so, that allows and enables extra seek and duration determining options */
1490 if (G_UNLIKELY (parse->priv->first_frame_offset < 0 && ret == GST_FLOW_OK)) {
1491 if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
1492 GST_BASE_PARSE_HAS_TIME (parse) &&
1493 parse->priv->pad_mode == GST_ACTIVATE_PULL) {
1494 parse->priv->first_frame_offset = offset;
1495 parse->priv->first_frame_ts = GST_BUFFER_TIMESTAMP (buffer);
1496 GST_DEBUG_OBJECT (parse, "subclass provided ts %" GST_TIME_FORMAT
1497 " for first frame at offset %" G_GINT64_FORMAT,
1498 GST_TIME_ARGS (parse->priv->first_frame_ts),
1499 parse->priv->first_frame_offset);
1500 if (!GST_CLOCK_TIME_IS_VALID (parse->priv->duration)) {
1502 GstClockTime last_ts = G_MAXINT64;
1504 GST_DEBUG_OBJECT (parse, "no duration; trying scan to determine");
1505 gst_base_parse_locate_time (parse, &last_ts, &off);
1506 if (GST_CLOCK_TIME_IS_VALID (last_ts))
1507 gst_base_parse_set_duration (parse, GST_FORMAT_TIME, last_ts, 0);
1510 /* disable further checks */
1511 parse->priv->first_frame_offset = 0;
1515 /* again use default handler to add missing metadata;
1516 * we may have new information on frame properties */
1517 gst_base_parse_parse_frame (parse, frame);
1518 if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
1519 GST_BUFFER_DURATION_IS_VALID (buffer)) {
1520 parse->priv->next_ts =
1521 GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer);
1523 /* we lost track, do not produce bogus time next time around
1524 * (probably means parser subclass has given up on parsing as well) */
1525 GST_DEBUG_OBJECT (parse, "no next fallback timestamp");
1526 parse->priv->next_ts = GST_CLOCK_TIME_NONE;
1529 if (parse->priv->upstream_seekable && parse->priv->exact_position &&
1530 GST_BUFFER_TIMESTAMP_IS_VALID (buffer))
1531 gst_base_parse_add_index_entry (parse, offset,
1532 GST_BUFFER_TIMESTAMP (buffer),
1533 !GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT), FALSE);
1535 /* First buffers are dropped, this means that the subclass needs more
1536 * frames to decide on the format and queues them internally */
1537 /* convert internal flow to OK and mark discont for the next buffer. */
1538 if (ret == GST_BASE_PARSE_FLOW_DROPPED) {
1539 gst_base_parse_frame_clear (parse, frame);
1541 } else if (ret != GST_FLOW_OK) {
1545 return gst_base_parse_push_frame (parse, frame);
1549 * gst_base_parse_push_frame:
1550 * @parse: #GstBaseParse.
1551 * @frame: #GstBaseParseFrame.
1553 * Pushes the frame downstream, sends any pending events and
1554 * does some timestamp and segment handling.
1556 * This must be called with sinkpad STREAM_LOCK held.
1558 * Returns: #GstFlowReturn
1561 gst_base_parse_push_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
1563 GstFlowReturn ret = GST_FLOW_OK;
1564 GstClockTime last_start = GST_CLOCK_TIME_NONE;
1565 GstClockTime last_stop = GST_CLOCK_TIME_NONE;
1566 GstBaseParseClass *klass = GST_BASE_PARSE_GET_CLASS (parse);
1569 g_return_val_if_fail (frame != NULL, GST_FLOW_ERROR);
1570 g_return_val_if_fail (frame->buffer != NULL, GST_FLOW_ERROR);
1572 buffer = frame->buffer;
1574 GST_LOG_OBJECT (parse,
1575 "processing buffer of size %d with ts %" GST_TIME_FORMAT
1576 ", duration %" GST_TIME_FORMAT, GST_BUFFER_SIZE (buffer),
1577 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
1578 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
1581 parse->priv->bytecount += GST_BUFFER_SIZE (buffer);
1582 if (G_LIKELY (!(frame->flags & GST_BASE_PARSE_FRAME_FLAG_NO_FRAME))) {
1583 parse->priv->framecount++;
1584 if (GST_BUFFER_DURATION_IS_VALID (buffer)) {
1585 parse->priv->acc_duration += GST_BUFFER_DURATION (buffer);
1588 if (parse->priv->update_interval &&
1589 (parse->priv->framecount % parse->priv->update_interval) == 0)
1590 gst_base_parse_update_duration (parse);
1592 if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer))
1593 last_start = last_stop = GST_BUFFER_TIMESTAMP (buffer);
1594 if (last_start != GST_CLOCK_TIME_NONE
1595 && GST_BUFFER_DURATION_IS_VALID (buffer))
1596 last_stop = last_start + GST_BUFFER_DURATION (buffer);
1598 /* should have caps by now */
1599 g_return_val_if_fail (GST_PAD_CAPS (parse->srcpad), GST_FLOW_ERROR);
1601 /* segment adjustment magic; only if we are running the whole show */
1602 if (!GST_BASE_PARSE_PASSTHROUGH (parse) && parse->segment.rate > 0.0 &&
1603 (parse->priv->pad_mode == GST_ACTIVATE_PULL ||
1604 parse->priv->upstream_seekable)) {
1605 /* segment times are typically estimates,
1606 * actual frame data might lead subclass to different timestamps,
1607 * so override segment start from what is supplied there */
1608 if (G_UNLIKELY (parse->pending_segment && !parse->priv->exact_position &&
1609 GST_CLOCK_TIME_IS_VALID (last_start))) {
1610 gst_event_unref (parse->pending_segment);
1611 parse->segment.start =
1612 MIN ((guint64) last_start, (guint64) parse->segment.stop);
1613 GST_DEBUG_OBJECT (parse,
1614 "adjusting pending segment start to %" GST_TIME_FORMAT,
1615 GST_TIME_ARGS (parse->segment.start));
1616 parse->pending_segment =
1617 gst_event_new_new_segment (FALSE, parse->segment.rate,
1618 parse->segment.format, parse->segment.start, parse->segment.stop,
1619 parse->segment.start);
1621 /* handle gaps, e.g. non-zero start-time, in as much not handled by above */
1622 if (GST_CLOCK_TIME_IS_VALID (parse->segment.last_stop) &&
1623 GST_CLOCK_TIME_IS_VALID (last_start)) {
1624 GstClockTimeDiff diff;
1626 /* only send newsegments with increasing start times,
1627 * otherwise if these go back and forth downstream (sinks) increase
1628 * accumulated time and running_time */
1629 diff = GST_CLOCK_DIFF (parse->segment.last_stop, last_start);
1630 if (G_UNLIKELY (diff > 2 * GST_SECOND && last_start > parse->segment.start
1631 && (!GST_CLOCK_TIME_IS_VALID (parse->segment.stop) ||
1632 last_start < parse->segment.stop))) {
1633 GST_DEBUG_OBJECT (parse,
1634 "Gap of %" G_GINT64_FORMAT " ns detected in stream "
1635 "(%" GST_TIME_FORMAT " -> %" GST_TIME_FORMAT "). "
1636 "Sending updated NEWSEGMENT events", diff,
1637 GST_TIME_ARGS (parse->segment.last_stop),
1638 GST_TIME_ARGS (last_start));
1639 if (G_UNLIKELY (parse->pending_segment)) {
1640 gst_event_unref (parse->pending_segment);
1641 parse->segment.start = last_start;
1642 parse->pending_segment =
1643 gst_event_new_new_segment (FALSE, parse->segment.rate,
1644 parse->segment.format, parse->segment.start, parse->segment.stop,
1645 parse->segment.start);
1647 /* send newsegment events such that the gap is not accounted in
1648 * accum time, hence running_time */
1649 /* close ahead of gap */
1650 gst_pad_push_event (parse->srcpad,
1651 gst_event_new_new_segment (TRUE, parse->segment.rate,
1652 parse->segment.format, parse->segment.last_stop,
1653 parse->segment.last_stop, parse->segment.last_stop));
1655 gst_pad_push_event (parse->srcpad,
1656 gst_event_new_new_segment (FALSE, parse->segment.rate,
1657 parse->segment.format, last_start, parse->segment.stop,
1660 /* align segment view with downstream,
1661 * prevents double-counting accum when closing segment */
1662 gst_segment_set_newsegment (&parse->segment, FALSE,
1663 parse->segment.rate, parse->segment.format, last_start,
1664 parse->segment.stop, last_start);
1665 parse->segment.last_stop = last_start;
1670 /* and should then also be linked downstream, so safe to send some events */
1671 if (G_UNLIKELY (parse->close_segment)) {
1672 /* only set up by loop */
1673 GST_DEBUG_OBJECT (parse, "loop sending close segment");
1674 gst_pad_push_event (parse->srcpad, parse->close_segment);
1675 parse->close_segment = NULL;
1677 if (G_UNLIKELY (parse->pending_segment)) {
1678 GST_DEBUG_OBJECT (parse, "%s push pending segment",
1679 parse->priv->pad_mode == GST_ACTIVATE_PULL ? "loop" : "chain");
1680 gst_pad_push_event (parse->srcpad, parse->pending_segment);
1681 parse->pending_segment = NULL;
1683 /* have caps; check identity */
1684 gst_base_parse_check_media (parse);
1687 /* update bitrates and optionally post corresponding tags
1688 * (following newsegment) */
1689 gst_base_parse_update_bitrates (parse, frame);
1691 if (G_UNLIKELY (parse->priv->pending_events)) {
1694 for (l = parse->priv->pending_events; l != NULL; l = l->next) {
1695 gst_pad_push_event (parse->srcpad, GST_EVENT (l->data));
1697 g_list_free (parse->priv->pending_events);
1698 parse->priv->pending_events = NULL;
1701 if (klass->pre_push_frame) {
1702 ret = klass->pre_push_frame (parse, frame);
1704 frame->flags |= GST_BASE_PARSE_FRAME_FLAG_CLIP;
1707 /* take final ownership of frame buffer */
1708 buffer = frame->buffer;
1709 frame->buffer = NULL;
1711 /* subclass must play nice */
1712 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
1715 gst_buffer_set_caps (buffer, GST_PAD_CAPS (parse->srcpad));
1717 parse->priv->seen_keyframe |= parse->priv->is_video &&
1718 !GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
1720 if (frame->flags & GST_BASE_PARSE_FRAME_FLAG_CLIP) {
1721 if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
1722 GST_CLOCK_TIME_IS_VALID (parse->segment.stop) &&
1723 GST_BUFFER_TIMESTAMP (buffer) >
1724 parse->segment.stop + parse->priv->lead_out_ts) {
1725 GST_LOG_OBJECT (parse, "Dropped frame, after segment");
1726 ret = GST_FLOW_UNEXPECTED;
1727 } else if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
1728 GST_BUFFER_DURATION_IS_VALID (buffer) &&
1729 GST_CLOCK_TIME_IS_VALID (parse->segment.start) &&
1730 GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer) +
1731 parse->priv->lead_in_ts < parse->segment.start) {
1732 if (parse->priv->seen_keyframe) {
1733 GST_LOG_OBJECT (parse, "Frame before segment, after keyframe");
1736 GST_LOG_OBJECT (parse, "Dropped frame, before segment");
1737 ret = GST_BASE_PARSE_FLOW_DROPPED;
1744 if (ret == GST_BASE_PARSE_FLOW_DROPPED) {
1745 GST_LOG_OBJECT (parse, "frame (%d bytes) dropped",
1746 GST_BUFFER_SIZE (buffer));
1747 gst_buffer_unref (buffer);
1749 } else if (ret == GST_FLOW_OK) {
1750 if (parse->segment.rate > 0.0) {
1751 ret = gst_pad_push (parse->srcpad, buffer);
1752 GST_LOG_OBJECT (parse, "frame (%d bytes) pushed: %s",
1753 GST_BUFFER_SIZE (buffer), gst_flow_get_name (ret));
1755 GST_LOG_OBJECT (parse, "frame (%d bytes) queued for now",
1756 GST_BUFFER_SIZE (buffer));
1757 parse->priv->buffers_queued =
1758 g_slist_prepend (parse->priv->buffers_queued, buffer);
1762 gst_buffer_unref (buffer);
1763 GST_LOG_OBJECT (parse, "frame (%d bytes) not pushed: %s",
1764 GST_BUFFER_SIZE (buffer), gst_flow_get_name (ret));
1765 /* if we are not sufficiently in control, let upstream decide on EOS */
1766 if (ret == GST_FLOW_UNEXPECTED &&
1767 (GST_BASE_PARSE_PASSTHROUGH (parse) ||
1768 (parse->priv->pad_mode == GST_ACTIVATE_PUSH &&
1769 !parse->priv->upstream_seekable)))
1773 /* Update current running segment position */
1774 if (ret == GST_FLOW_OK && last_stop != GST_CLOCK_TIME_NONE &&
1775 parse->segment.last_stop < last_stop)
1776 gst_segment_set_last_stop (&parse->segment, GST_FORMAT_TIME, last_stop);
1778 gst_base_parse_frame_clear (parse, frame);
1785 * gst_base_parse_drain:
1786 * @parse: #GstBaseParse.
1788 * Drains the adapter until it is empty. It decreases the min_frame_size to
1789 * match the current adapter size and calls chain method until the adapter
1790 * is emptied or chain returns with error.
1793 gst_base_parse_drain (GstBaseParse * parse)
1797 GST_DEBUG_OBJECT (parse, "draining");
1798 parse->priv->drain = TRUE;
1801 avail = gst_adapter_available (parse->adapter);
1805 if (gst_base_parse_chain (parse->sinkpad, NULL) != GST_FLOW_OK) {
1809 /* nothing changed, maybe due to truncated frame; break infinite loop */
1810 if (avail == gst_adapter_available (parse->adapter)) {
1811 GST_DEBUG_OBJECT (parse, "no change during draining; flushing");
1812 gst_adapter_clear (parse->adapter);
1816 parse->priv->drain = FALSE;
1820 * gst_base_parse_process_fragment:
1821 * @parse: #GstBaseParse.
1823 * Processes a reverse playback (forward) fragment:
1824 * - append head of last fragment that was skipped to current fragment data
1825 * - drain the resulting current fragment data (i.e. repeated chain)
1826 * - add time/duration (if needed) to frames queued by chain
1827 * - push queued data
1829 static GstFlowReturn
1830 gst_base_parse_process_fragment (GstBaseParse * parse, gboolean push_only)
1833 GstFlowReturn ret = GST_FLOW_OK;
1834 GSList *send = NULL;
1840 parse->priv->buffers_pending = g_slist_reverse (parse->priv->buffers_pending);
1841 while (parse->priv->buffers_pending) {
1842 buf = GST_BUFFER_CAST (parse->priv->buffers_pending->data);
1843 GST_LOG_OBJECT (parse, "adding pending buffer (size %d)",
1844 GST_BUFFER_SIZE (buf));
1845 gst_adapter_push (parse->adapter, buf);
1846 parse->priv->buffers_pending =
1847 g_slist_delete_link (parse->priv->buffers_pending,
1848 parse->priv->buffers_pending);
1851 /* invalidate so no fall-back timestamping is performed;
1852 * ok if taken from subclass or upstream */
1853 parse->priv->next_ts = GST_CLOCK_TIME_NONE;
1854 /* prevent it hanging around stop all the time */
1855 parse->segment.last_stop = GST_CLOCK_TIME_NONE;
1857 parse->priv->discont = TRUE;
1859 /* chain looks for frames and queues resulting ones (in stead of pushing) */
1860 /* initial skipped data is added to buffers_pending */
1861 gst_base_parse_drain (parse);
1864 /* add metadata (if needed to queued buffers */
1865 GST_LOG_OBJECT (parse, "last timestamp: %" GST_TIME_FORMAT,
1866 GST_TIME_ARGS (parse->priv->last_ts));
1867 while (parse->priv->buffers_queued) {
1868 buf = GST_BUFFER_CAST (parse->priv->buffers_queued->data);
1870 /* no touching if upstream or parsing provided time */
1871 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
1872 GST_LOG_OBJECT (parse, "buffer has time %" GST_TIME_FORMAT,
1873 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
1874 } else if (GST_CLOCK_TIME_IS_VALID (parse->priv->last_ts) &&
1875 GST_BUFFER_DURATION_IS_VALID (buf)) {
1876 if (G_LIKELY (GST_BUFFER_DURATION (buf) <= parse->priv->last_ts))
1877 parse->priv->last_ts -= GST_BUFFER_DURATION (buf);
1879 parse->priv->last_ts = 0;
1880 GST_BUFFER_TIMESTAMP (buf) = parse->priv->last_ts;
1881 GST_LOG_OBJECT (parse, "applied time %" GST_TIME_FORMAT,
1882 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
1884 /* no idea, very bad */
1885 GST_WARNING_OBJECT (parse, "could not determine time for buffer");
1888 /* reverse order for ascending sending */
1889 send = g_slist_prepend (send, buf);
1890 parse->priv->buffers_queued =
1891 g_slist_delete_link (parse->priv->buffers_queued,
1892 parse->priv->buffers_queued);
1897 buf = GST_BUFFER_CAST (send->data);
1898 GST_LOG_OBJECT (parse, "pushing buffer %p, timestamp %"
1899 GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT
1900 ", offset %" G_GINT64_FORMAT, buf,
1901 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
1902 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)), GST_BUFFER_OFFSET (buf));
1904 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (parse->priv->last_ts)))
1905 parse->priv->last_ts = GST_BUFFER_TIMESTAMP (buf);
1907 /* iterate output queue an push downstream */
1908 ret = gst_pad_push (parse->srcpad, buf);
1909 send = g_slist_delete_link (send, send);
1911 /* clear any leftover if error */
1912 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
1914 buf = GST_BUFFER_CAST (send->data);
1915 gst_buffer_unref (buf);
1916 send = g_slist_delete_link (send, send);
1921 /* any trailing unused no longer usable (ideally none) */
1922 if (G_UNLIKELY (gst_adapter_available (parse->adapter))) {
1923 GST_DEBUG_OBJECT (parse, "discarding %d trailing bytes",
1924 gst_adapter_available (parse->adapter));
1925 gst_adapter_clear (parse->adapter);
1931 /* small helper that checks whether we have been trying to resync too long */
1932 static inline GstFlowReturn
1933 gst_base_parse_check_sync (GstBaseParse * parse)
1935 if (G_UNLIKELY (parse->priv->discont &&
1936 parse->priv->offset - parse->priv->sync_offset > 2 * 1024 * 1024)) {
1937 GST_ELEMENT_ERROR (parse, STREAM, DECODE,
1938 ("Failed to parse stream"), (NULL));
1939 return GST_FLOW_ERROR;
1947 * gst_base_parse_chain:
1949 * @buffer: #GstBuffer.
1951 * Returns: #GstFlowReturn.
1953 static GstFlowReturn
1954 gst_base_parse_chain (GstPad * pad, GstBuffer * buffer)
1956 GstBaseParseClass *bclass;
1957 GstBaseParse *parse;
1958 GstFlowReturn ret = GST_FLOW_OK;
1959 GstBuffer *outbuf = NULL;
1960 GstBuffer *tmpbuf = NULL;
1964 guint old_min_size = 0, min_size, av;
1965 GstClockTime timestamp;
1966 GstBaseParseFrame _frame = { 0, };
1967 GstBaseParseFrame *frame;
1969 parse = GST_BASE_PARSE (GST_OBJECT_PARENT (pad));
1970 bclass = GST_BASE_PARSE_GET_CLASS (parse);
1973 if (G_LIKELY (buffer)) {
1974 GST_LOG_OBJECT (parse, "buffer size: %d, offset = %" G_GINT64_FORMAT,
1975 GST_BUFFER_SIZE (buffer), GST_BUFFER_OFFSET (buffer));
1976 if (G_UNLIKELY (GST_BASE_PARSE_PASSTHROUGH (parse))) {
1977 frame->buffer = gst_buffer_make_metadata_writable (buffer);
1978 return gst_base_parse_push_frame (parse, frame);
1980 /* upstream feeding us in reverse playback;
1981 * gather each fragment, then process it in single run */
1982 if (parse->segment.rate < 0.0) {
1983 if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT))) {
1984 GST_DEBUG_OBJECT (parse, "buffer starts new reverse playback fragment");
1985 ret = gst_base_parse_process_fragment (parse, FALSE);
1987 gst_adapter_push (parse->adapter, buffer);
1990 gst_adapter_push (parse->adapter, buffer);
1993 /* Parse and push as many frames as possible */
1994 /* Stop either when adapter is empty or we are flushing */
1995 while (!parse->priv->flushing) {
1998 tmpbuf = gst_buffer_new ();
2001 /* Synchronization loop */
2003 min_size = MAX (parse->priv->min_frame_size, fsize);
2004 av = gst_adapter_available (parse->adapter);
2006 /* loop safety check */
2007 if (G_UNLIKELY (old_min_size >= min_size))
2009 old_min_size = min_size;
2011 if (G_UNLIKELY (parse->priv->drain)) {
2013 GST_DEBUG_OBJECT (parse, "draining, data left: %d", min_size);
2014 if (G_UNLIKELY (!min_size)) {
2015 gst_buffer_unref (tmpbuf);
2020 /* Collect at least min_frame_size bytes */
2021 if (av < min_size) {
2022 GST_DEBUG_OBJECT (parse, "not enough data available (only %d bytes)",
2024 gst_buffer_unref (tmpbuf);
2028 /* always pass all available data */
2029 data = gst_adapter_peek (parse->adapter, av);
2030 GST_BUFFER_DATA (tmpbuf) = (guint8 *) data;
2031 GST_BUFFER_SIZE (tmpbuf) = min_size;
2032 GST_BUFFER_OFFSET (tmpbuf) = parse->priv->offset;
2033 GST_BUFFER_FLAG_SET (tmpbuf, GST_MINI_OBJECT_FLAG_READONLY);
2035 if (parse->priv->discont) {
2036 GST_DEBUG_OBJECT (parse, "marking DISCONT");
2037 GST_BUFFER_FLAG_SET (tmpbuf, GST_BUFFER_FLAG_DISCONT);
2041 gst_base_parse_frame_update (parse, frame, tmpbuf);
2042 res = bclass->check_valid_frame (parse, frame, &fsize, &skip);
2043 gst_buffer_replace (&frame->buffer, NULL);
2045 if (gst_adapter_available (parse->adapter) < fsize) {
2046 GST_DEBUG_OBJECT (parse,
2047 "found valid frame but not enough data available (only %d bytes)",
2048 gst_adapter_available (parse->adapter));
2049 gst_buffer_unref (tmpbuf);
2052 GST_LOG_OBJECT (parse, "valid frame of size %d at pos %d", fsize, skip);
2056 /* subclass didn't touch this value. By default we skip 1 byte */
2060 GST_LOG_OBJECT (parse, "finding sync, skipping %d bytes", skip);
2061 if (parse->segment.rate < 0.0 && !parse->priv->buffers_queued) {
2062 /* reverse playback, and no frames found yet, so we are skipping
2063 * the leading part of a fragment, which may form the tail of
2064 * fragment coming later, hopefully subclass skips efficiently ... */
2065 timestamp = gst_adapter_prev_timestamp (parse->adapter, NULL);
2066 outbuf = gst_adapter_take_buffer (parse->adapter, skip);
2067 outbuf = gst_buffer_make_metadata_writable (outbuf);
2068 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
2069 parse->priv->buffers_pending =
2070 g_slist_prepend (parse->priv->buffers_pending, outbuf);
2073 gst_adapter_flush (parse->adapter, skip);
2075 parse->priv->offset += skip;
2076 if (!parse->priv->discont)
2077 parse->priv->sync_offset = parse->priv->offset;
2078 parse->priv->discont = TRUE;
2079 /* something changed least; nullify loop check */
2082 /* skip == 0 should imply subclass set min_size to need more data;
2083 * we check this shortly */
2084 if ((ret = gst_base_parse_check_sync (parse)) != GST_FLOW_OK) {
2085 gst_buffer_unref (tmpbuf);
2089 gst_buffer_unref (tmpbuf);
2093 /* Subclass found the sync, but still wants to skip some data */
2094 GST_LOG_OBJECT (parse, "skipping %d bytes", skip);
2095 gst_adapter_flush (parse->adapter, skip);
2096 parse->priv->offset += skip;
2099 /* Grab lock to prevent a race with FLUSH_START handler */
2100 GST_PAD_STREAM_LOCK (parse->srcpad);
2102 /* FLUSH_START event causes the "flushing" flag to be set. In this
2103 * case we can leave the frame pushing loop */
2104 if (parse->priv->flushing) {
2105 GST_PAD_STREAM_UNLOCK (parse->srcpad);
2109 /* move along with upstream timestamp (if any),
2110 * but interpolate in between */
2111 timestamp = gst_adapter_prev_timestamp (parse->adapter, NULL);
2112 if (GST_CLOCK_TIME_IS_VALID (timestamp) &&
2113 (parse->priv->prev_ts != timestamp)) {
2114 parse->priv->prev_ts = parse->priv->next_ts = timestamp;
2117 /* FIXME: Would it be more efficient to make a subbuffer instead? */
2118 outbuf = gst_adapter_take_buffer (parse->adapter, fsize);
2119 outbuf = gst_buffer_make_metadata_writable (outbuf);
2121 /* Subclass may want to know the data offset */
2122 GST_BUFFER_OFFSET (outbuf) = parse->priv->offset;
2123 parse->priv->offset += fsize;
2124 GST_BUFFER_TIMESTAMP (outbuf) = GST_CLOCK_TIME_NONE;
2125 GST_BUFFER_DURATION (outbuf) = GST_CLOCK_TIME_NONE;
2127 frame->buffer = outbuf;
2128 ret = gst_base_parse_handle_and_push_frame (parse, bclass, frame);
2129 GST_PAD_STREAM_UNLOCK (parse->srcpad);
2131 if (ret != GST_FLOW_OK) {
2132 GST_LOG_OBJECT (parse, "push returned %d", ret);
2138 GST_LOG_OBJECT (parse, "chain leaving");
2144 GST_ELEMENT_ERROR (parse, STREAM, FAILED, (NULL),
2145 ("min_size evolution %d -> %d; breaking to avoid looping",
2146 old_min_size, min_size));
2147 return GST_FLOW_ERROR;
2151 /* pull @size bytes at current offset,
2152 * i.e. at least try to and possibly return a shorter buffer if near the end */
2153 static GstFlowReturn
2154 gst_base_parse_pull_range (GstBaseParse * parse, guint size,
2155 GstBuffer ** buffer)
2157 GstFlowReturn ret = GST_FLOW_OK;
2159 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
2161 /* Caching here actually makes much less difference than one would expect.
2162 * We do it mainly to avoid pulling buffers of 1 byte all the time */
2163 if (parse->priv->cache) {
2164 gint64 cache_offset = GST_BUFFER_OFFSET (parse->priv->cache);
2165 gint cache_size = GST_BUFFER_SIZE (parse->priv->cache);
2167 if (cache_offset <= parse->priv->offset &&
2168 (parse->priv->offset + size) <= (cache_offset + cache_size)) {
2169 *buffer = gst_buffer_create_sub (parse->priv->cache,
2170 parse->priv->offset - cache_offset, size);
2171 GST_BUFFER_OFFSET (*buffer) = parse->priv->offset;
2174 /* not enough data in the cache, free cache and get a new one */
2175 gst_buffer_unref (parse->priv->cache);
2176 parse->priv->cache = NULL;
2179 /* refill the cache */
2181 gst_pad_pull_range (parse->sinkpad, parse->priv->offset, MAX (size,
2182 64 * 1024), &parse->priv->cache);
2183 if (ret != GST_FLOW_OK) {
2184 parse->priv->cache = NULL;
2188 if (GST_BUFFER_SIZE (parse->priv->cache) >= size) {
2189 *buffer = gst_buffer_create_sub (parse->priv->cache, 0, size);
2190 GST_BUFFER_OFFSET (*buffer) = parse->priv->offset;
2194 /* Not possible to get enough data, try a last time with
2195 * requesting exactly the size we need */
2196 gst_buffer_unref (parse->priv->cache);
2197 parse->priv->cache = NULL;
2199 ret = gst_pad_pull_range (parse->sinkpad, parse->priv->offset, size,
2200 &parse->priv->cache);
2202 if (ret != GST_FLOW_OK) {
2203 GST_DEBUG_OBJECT (parse, "pull_range returned %d", ret);
2208 if (GST_BUFFER_SIZE (parse->priv->cache) < size) {
2209 GST_DEBUG_OBJECT (parse, "Returning short buffer at offset %"
2210 G_GUINT64_FORMAT ": wanted %u bytes, got %u bytes", parse->priv->offset,
2211 size, GST_BUFFER_SIZE (parse->priv->cache));
2213 *buffer = parse->priv->cache;
2214 parse->priv->cache = NULL;
2219 *buffer = gst_buffer_create_sub (parse->priv->cache, 0, size);
2220 GST_BUFFER_OFFSET (*buffer) = parse->priv->offset;
2225 static GstFlowReturn
2226 gst_base_parse_handle_previous_fragment (GstBaseParse * parse)
2229 GstClockTime ts = 0;
2233 GST_DEBUG_OBJECT (parse, "fragment ended; last_ts = %" GST_TIME_FORMAT
2234 ", last_offset = %" G_GINT64_FORMAT, GST_TIME_ARGS (parse->priv->last_ts),
2235 parse->priv->last_offset);
2237 if (!parse->priv->last_offset || parse->priv->last_ts <= parse->segment.start) {
2238 GST_DEBUG_OBJECT (parse, "past start of segment %" GST_TIME_FORMAT,
2239 GST_TIME_ARGS (parse->segment.start));
2240 ret = GST_FLOW_UNEXPECTED;
2244 /* last fragment started at last_offset / last_ts;
2245 * seek back 10s capped at 1MB */
2246 if (parse->priv->last_ts >= 10 * GST_SECOND)
2247 ts = parse->priv->last_ts - 10 * GST_SECOND;
2248 /* if we are exact now, we will be more so going backwards */
2249 if (parse->priv->exact_position) {
2250 offset = gst_base_parse_find_offset (parse, ts, TRUE, NULL);
2252 GstFormat dstformat = GST_FORMAT_BYTES;
2254 if (!gst_pad_query_convert (parse->srcpad, GST_FORMAT_TIME, ts,
2255 &dstformat, &offset)) {
2256 GST_DEBUG_OBJECT (parse, "conversion failed, only BYTE based");
2259 offset = CLAMP (offset, parse->priv->last_offset - 1024 * 1024,
2260 parse->priv->last_offset - 1024);
2261 offset = MAX (0, offset);
2263 GST_DEBUG_OBJECT (parse, "next fragment from offset %" G_GINT64_FORMAT,
2265 parse->priv->offset = offset;
2267 ret = gst_base_parse_pull_range (parse, parse->priv->last_offset - offset,
2269 if (ret != GST_FLOW_OK)
2272 /* offset will increase again as fragment is processed/parsed */
2273 parse->priv->last_offset = offset;
2275 gst_adapter_push (parse->adapter, buffer);
2276 ret = gst_base_parse_process_fragment (parse, FALSE);
2277 if (ret != GST_FLOW_OK)
2280 /* force previous fragment */
2281 parse->priv->offset = -1;
2288 * pull and scan for next frame starting from current offset
2289 * ajusts sync, drain and offset going along */
2290 static GstFlowReturn
2291 gst_base_parse_scan_frame (GstBaseParse * parse, GstBaseParseClass * klass,
2292 GstBaseParseFrame * frame, gboolean full)
2294 GstBuffer *buffer, *outbuf;
2295 GstFlowReturn ret = GST_FLOW_OK;
2296 guint fsize = 0, min_size, old_min_size = 0;
2299 g_return_val_if_fail (frame != NULL, GST_FLOW_ERROR);
2301 GST_LOG_OBJECT (parse, "scanning for frame at offset %" G_GUINT64_FORMAT
2302 " (%#" G_GINT64_MODIFIER "x)", parse->priv->offset, parse->priv->offset);
2307 min_size = MAX (parse->priv->min_frame_size, fsize);
2308 /* loop safety check */
2309 if (G_UNLIKELY (old_min_size >= min_size))
2311 old_min_size = min_size;
2313 ret = gst_base_parse_pull_range (parse, min_size, &buffer);
2314 if (ret != GST_FLOW_OK)
2317 if (parse->priv->discont) {
2318 GST_DEBUG_OBJECT (parse, "marking DISCONT");
2319 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
2322 /* if we got a short read, inform subclass we are draining leftover
2323 * and no more is to be expected */
2324 if (GST_BUFFER_SIZE (buffer) < min_size)
2325 parse->priv->drain = TRUE;
2328 gst_base_parse_frame_update (parse, frame, buffer);
2329 res = klass->check_valid_frame (parse, frame, &fsize, &skip);
2330 gst_buffer_replace (&frame->buffer, NULL);
2332 parse->priv->drain = FALSE;
2333 GST_LOG_OBJECT (parse, "valid frame of size %d at pos %d", fsize, skip);
2336 parse->priv->drain = FALSE;
2340 GST_LOG_OBJECT (parse, "finding sync, skipping %d bytes", skip);
2341 if (full && parse->segment.rate < 0.0 && !parse->priv->buffers_queued) {
2342 /* reverse playback, and no frames found yet, so we are skipping
2343 * the leading part of a fragment, which may form the tail of
2344 * fragment coming later, hopefully subclass skips efficiently ... */
2345 outbuf = gst_buffer_create_sub (buffer, 0, skip);
2346 parse->priv->buffers_pending =
2347 g_slist_prepend (parse->priv->buffers_pending, outbuf);
2350 parse->priv->offset += skip;
2351 if (!parse->priv->discont)
2352 parse->priv->sync_offset = parse->priv->offset;
2353 parse->priv->discont = TRUE;
2354 /* something changed least; nullify loop check */
2357 /* skip == 0 should imply subclass set min_size to need more data;
2358 * we check this shortly */
2359 GST_DEBUG_OBJECT (parse, "finding sync...");
2360 gst_buffer_unref (buffer);
2361 if ((ret = gst_base_parse_check_sync (parse)) != GST_FLOW_OK) {
2366 /* Does the subclass want to skip too? */
2368 parse->priv->offset += skip;
2372 if (fsize + skip <= GST_BUFFER_SIZE (buffer)) {
2373 outbuf = gst_buffer_create_sub (buffer, skip, fsize);
2374 GST_BUFFER_OFFSET (outbuf) = GST_BUFFER_OFFSET (buffer) + skip;
2375 GST_BUFFER_TIMESTAMP (outbuf) = GST_CLOCK_TIME_NONE;
2376 gst_buffer_unref (buffer);
2378 gst_buffer_unref (buffer);
2379 ret = gst_base_parse_pull_range (parse, fsize, &outbuf);
2380 if (ret != GST_FLOW_OK)
2382 if (GST_BUFFER_SIZE (outbuf) < fsize) {
2383 gst_buffer_unref (outbuf);
2384 ret = GST_FLOW_UNEXPECTED;
2388 parse->priv->offset += fsize;
2390 frame->buffer = outbuf;
2398 GST_ELEMENT_ERROR (parse, STREAM, FAILED, (NULL),
2399 ("min_size evolution %d -> %d; breaking to avoid looping",
2400 old_min_size, min_size));
2401 return GST_FLOW_ERROR;
2406 * gst_base_parse_loop:
2409 * Loop that is used in pull mode to retrieve data from upstream.
2412 gst_base_parse_loop (GstPad * pad)
2414 GstBaseParse *parse;
2415 GstBaseParseClass *klass;
2416 GstFlowReturn ret = GST_FLOW_OK;
2417 GstBaseParseFrame frame = { 0, };
2419 parse = GST_BASE_PARSE (gst_pad_get_parent (pad));
2420 klass = GST_BASE_PARSE_GET_CLASS (parse);
2422 /* reverse playback:
2423 * first fragment (closest to stop time) is handled normally below,
2424 * then we pull in fragments going backwards */
2425 if (parse->segment.rate < 0.0) {
2426 /* check if we jumped back to a previous fragment,
2427 * which is a post-first fragment */
2428 if (parse->priv->offset < 0) {
2429 ret = gst_base_parse_handle_previous_fragment (parse);
2434 ret = gst_base_parse_scan_frame (parse, klass, &frame, TRUE);
2435 if (ret != GST_FLOW_OK)
2438 /* This always cleans up frame, even if error occurs */
2439 ret = gst_base_parse_handle_and_push_frame (parse, klass, &frame);
2441 /* eat expected eos signalling past segment in reverse playback */
2442 if (parse->segment.rate < 0.0 && ret == GST_FLOW_UNEXPECTED &&
2443 parse->segment.last_stop >= parse->segment.stop) {
2444 GST_DEBUG_OBJECT (parse, "downstream has reached end of segment");
2445 /* push what was accumulated during loop run */
2446 gst_base_parse_process_fragment (parse, TRUE);
2447 /* force previous fragment */
2448 parse->priv->offset = -1;
2453 if (ret == GST_FLOW_UNEXPECTED)
2455 else if (ret != GST_FLOW_OK)
2458 gst_object_unref (parse);
2464 ret = GST_FLOW_UNEXPECTED;
2465 GST_DEBUG_OBJECT (parse, "eos");
2470 gboolean push_eos = FALSE;
2472 GST_DEBUG_OBJECT (parse, "pausing task, reason %s",
2473 gst_flow_get_name (ret));
2474 gst_pad_pause_task (parse->sinkpad);
2476 if (ret == GST_FLOW_UNEXPECTED) {
2477 /* handle end-of-stream/segment */
2478 if (parse->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2481 if ((stop = parse->segment.stop) == -1)
2482 stop = parse->segment.duration;
2484 GST_DEBUG_OBJECT (parse, "sending segment_done");
2486 gst_element_post_message
2487 (GST_ELEMENT_CAST (parse),
2488 gst_message_new_segment_done (GST_OBJECT_CAST (parse),
2489 GST_FORMAT_TIME, stop));
2491 /* If we STILL have zero frames processed, fire an error */
2492 if (parse->priv->framecount == 0) {
2493 GST_ELEMENT_ERROR (parse, STREAM, WRONG_TYPE,
2494 ("No valid frames found before end of stream"), (NULL));
2498 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_UNEXPECTED) {
2499 /* for fatal errors we post an error message, wrong-state is
2500 * not fatal because it happens due to flushes and only means
2501 * that we should stop now. */
2502 GST_ELEMENT_ERROR (parse, STREAM, FAILED, (NULL),
2503 ("streaming stopped, reason %s", gst_flow_get_name (ret)));
2507 /* newsegment before eos */
2508 if (parse->pending_segment) {
2509 gst_pad_push_event (parse->srcpad, parse->pending_segment);
2510 parse->pending_segment = NULL;
2512 gst_pad_push_event (parse->srcpad, gst_event_new_eos ());
2514 gst_object_unref (parse);
2520 * gst_base_parse_sink_activate:
2521 * @sinkpad: #GstPad to be activated.
2523 * Returns: TRUE if activation succeeded.
2526 gst_base_parse_sink_activate (GstPad * sinkpad)
2528 GstBaseParse *parse;
2529 gboolean result = TRUE;
2531 parse = GST_BASE_PARSE (gst_pad_get_parent (sinkpad));
2533 GST_DEBUG_OBJECT (parse, "sink activate");
2535 if (gst_pad_check_pull_range (sinkpad)) {
2536 GST_DEBUG_OBJECT (parse, "trying to activate in pull mode");
2537 result = gst_pad_activate_pull (sinkpad, TRUE);
2539 GST_DEBUG_OBJECT (parse, "trying to activate in push mode");
2540 result = gst_pad_activate_push (sinkpad, TRUE);
2543 GST_DEBUG_OBJECT (parse, "sink activate return %d", result);
2544 gst_object_unref (parse);
2550 * gst_base_parse_activate:
2551 * @parse: #GstBaseParse.
2552 * @active: TRUE if element will be activated, FALSE if deactivated.
2554 * Returns: TRUE if the operation succeeded.
2557 gst_base_parse_activate (GstBaseParse * parse, gboolean active)
2559 GstBaseParseClass *klass;
2560 gboolean result = FALSE;
2562 GST_DEBUG_OBJECT (parse, "activate %d", active);
2564 klass = GST_BASE_PARSE_GET_CLASS (parse);
2567 if (parse->priv->pad_mode == GST_ACTIVATE_NONE && klass->start)
2568 result = klass->start (parse);
2570 /* We must make sure streaming has finished before resetting things
2571 * and calling the ::stop vfunc */
2572 GST_PAD_STREAM_LOCK (parse->sinkpad);
2573 GST_PAD_STREAM_UNLOCK (parse->sinkpad);
2575 if (parse->priv->pad_mode != GST_ACTIVATE_NONE && klass->stop)
2576 result = klass->stop (parse);
2578 parse->priv->pad_mode = GST_ACTIVATE_NONE;
2580 GST_DEBUG_OBJECT (parse, "activate return: %d", result);
2586 * gst_base_parse_sink_activate_push:
2587 * @pad: #GstPad to be (de)activated.
2588 * @active: TRUE when activating, FALSE when deactivating.
2590 * Returns: TRUE if (de)activation succeeded.
2593 gst_base_parse_sink_activate_push (GstPad * pad, gboolean active)
2595 gboolean result = TRUE;
2596 GstBaseParse *parse;
2598 parse = GST_BASE_PARSE (gst_pad_get_parent (pad));
2600 GST_DEBUG_OBJECT (parse, "sink activate push %d", active);
2602 result = gst_base_parse_activate (parse, active);
2605 parse->priv->pad_mode = active ? GST_ACTIVATE_PUSH : GST_ACTIVATE_NONE;
2607 GST_DEBUG_OBJECT (parse, "sink activate push return: %d", result);
2609 gst_object_unref (parse);
2615 * gst_base_parse_sink_activate_pull:
2616 * @sinkpad: #GstPad to be (de)activated.
2617 * @active: TRUE when activating, FALSE when deactivating.
2619 * Returns: TRUE if (de)activation succeeded.
2622 gst_base_parse_sink_activate_pull (GstPad * sinkpad, gboolean active)
2624 gboolean result = FALSE;
2625 GstBaseParse *parse;
2627 parse = GST_BASE_PARSE (gst_pad_get_parent (sinkpad));
2629 GST_DEBUG_OBJECT (parse, "activate pull %d", active);
2631 result = gst_base_parse_activate (parse, active);
2635 parse->pending_segment = gst_event_new_new_segment (FALSE,
2636 parse->segment.rate, parse->segment.format,
2637 parse->segment.start, parse->segment.stop, parse->segment.last_stop);
2638 result &= gst_pad_start_task (sinkpad,
2639 (GstTaskFunction) gst_base_parse_loop, sinkpad);
2641 result &= gst_pad_stop_task (sinkpad);
2646 parse->priv->pad_mode = active ? GST_ACTIVATE_PULL : GST_ACTIVATE_NONE;
2648 GST_DEBUG_OBJECT (parse, "sink activate pull return: %d", result);
2650 gst_object_unref (parse);
2656 * gst_base_parse_set_duration:
2657 * @parse: #GstBaseParse.
2659 * @duration: duration value.
2661 * Sets the duration of the currently playing media. Subclass can use this
2662 * when it is able to determine duration and/or notices a change in the media
2663 * duration. Alternatively, if @interval is non-zero (default), then stream
2664 * duration is determined based on estimated bitrate, and updated every @interval
2667 gst_base_parse_set_duration (GstBaseParse * parse,
2668 GstFormat fmt, gint64 duration, gint interval)
2670 g_return_if_fail (parse != NULL);
2672 if (parse->priv->upstream_has_duration) {
2673 GST_DEBUG_OBJECT (parse, "using upstream duration; discarding update");
2677 if (duration != parse->priv->duration) {
2680 m = gst_message_new_duration (GST_OBJECT (parse), fmt, duration);
2681 gst_element_post_message (GST_ELEMENT (parse), m);
2683 /* TODO: what about duration tag? */
2685 parse->priv->duration = duration;
2686 parse->priv->duration_fmt = fmt;
2687 GST_DEBUG_OBJECT (parse, "set duration: %" G_GINT64_FORMAT, duration);
2688 if (fmt == GST_FORMAT_TIME && GST_CLOCK_TIME_IS_VALID (duration)) {
2689 if (interval != 0) {
2690 GST_DEBUG_OBJECT (parse, "valid duration provided, disabling estimate");
2694 GST_DEBUG_OBJECT (parse, "set update interval: %d", interval);
2695 parse->priv->update_interval = interval;
2701 * gst_base_parse_set_seek:
2702 * @parse: #GstBaseParse.
2703 * @seek: #GstBaseParseSeekable.
2704 * @abitrate: average bitrate.
2706 * Sets whether and how the media is seekable (in time).
2707 * Also optionally provides average bitrate detected in media (if non-zero),
2708 * e.g. based on metadata, as it will be posted to the application.
2710 * By default, announced average bitrate is estimated, and seekability is assumed
2711 * possible based on estimated bitrate.
2714 gst_base_parse_set_seek (GstBaseParse * parse,
2715 GstBaseParseSeekable seek, guint bitrate)
2717 parse->priv->seekable = seek;
2718 parse->priv->bitrate = bitrate;
2719 GST_DEBUG_OBJECT (parse, "seek %d, bitrate %d", seek, bitrate);
2724 * gst_base_parse_set_min_frame_size:
2725 * @parse: #GstBaseParse.
2726 * @min_size: Minimum size of the data that this base class should give to
2729 * Subclass can use this function to tell the base class that it needs to
2730 * give at least #min_size buffers.
2733 gst_base_parse_set_min_frame_size (GstBaseParse * parse, guint min_size)
2735 g_return_if_fail (parse != NULL);
2737 parse->priv->min_frame_size = min_size;
2738 GST_LOG_OBJECT (parse, "set frame_min_size: %d", min_size);
2742 * gst_base_parse_set_format:
2743 * @parse: the #GstBaseParseFormat to set or unset
2744 * @flags: format flag to enable or disable
2745 * @on: whether or not to enable
2747 * Set flags describing characteristics of parsed format.
2750 gst_base_parse_set_format (GstBaseParse * parse, GstBaseParseFormat flag,
2753 g_return_if_fail (parse != NULL);
2755 GST_LOG_OBJECT (parse, "set flag %d to %d", flag, on);
2757 parse->priv->format |= flag;
2759 parse->priv->format &= ~flag;
2763 * gst_base_parse_set_frame_props:
2764 * @parse: the #GstBaseParse to set
2765 * @fps_num: frames per second (numerator).
2766 * @fps_den: frames per second (denominator).
2767 * @lead_in: frames needed before a segment for subsequent decode
2768 * @lead_out: frames needed after a segment
2770 * If frames per second is configured, parser can take care of buffer duration
2771 * and timestamping. When performing segment clipping, or seeking to a specific
2772 * location, a corresponding decoder might need an initial @lead_in and a
2773 * following @lead_out number of frames to ensure the desired segment is
2774 * entirely filled upon decoding.
2777 gst_base_parse_set_frame_props (GstBaseParse * parse, guint fps_num,
2778 guint fps_den, guint lead_in, guint lead_out)
2780 g_return_if_fail (parse != NULL);
2782 parse->priv->fps_num = fps_num;
2783 parse->priv->fps_den = fps_den;
2784 if (!fps_num || !fps_den) {
2785 GST_DEBUG_OBJECT (parse, "invalid fps (%d/%d), ignoring parameters",
2787 fps_num = fps_den = 0;
2788 parse->priv->frame_duration = GST_CLOCK_TIME_NONE;
2789 parse->priv->lead_in = parse->priv->lead_out = 0;
2790 parse->priv->lead_in_ts = parse->priv->lead_out_ts = 0;
2792 parse->priv->frame_duration =
2793 gst_util_uint64_scale (GST_SECOND, fps_den, fps_num);
2794 parse->priv->lead_in = lead_in;
2795 parse->priv->lead_out = lead_out;
2796 parse->priv->lead_in_ts =
2797 gst_util_uint64_scale (GST_SECOND, fps_den * lead_in, fps_num);
2798 parse->priv->lead_out_ts =
2799 gst_util_uint64_scale (GST_SECOND, fps_den * lead_out, fps_num);
2801 GST_LOG_OBJECT (parse, "set fps: %d/%d => duration: %" G_GINT64_FORMAT " ms",
2802 fps_num, fps_den, parse->priv->frame_duration / GST_MSECOND);
2803 GST_LOG_OBJECT (parse, "set lead in: %d frames = %" G_GUINT64_FORMAT " ms, "
2804 "lead out: %d frames = %" G_GUINT64_FORMAT " ms",
2805 lead_in, parse->priv->lead_in_ts / GST_MSECOND,
2806 lead_out, parse->priv->lead_out_ts / GST_MSECOND);
2810 gst_base_parse_get_duration (GstBaseParse * parse, GstFormat format,
2811 GstClockTime * duration)
2813 gboolean res = FALSE;
2815 g_return_val_if_fail (duration != NULL, FALSE);
2817 *duration = GST_CLOCK_TIME_NONE;
2818 if (parse->priv->duration != -1 && format == parse->priv->duration_fmt) {
2819 GST_LOG_OBJECT (parse, "using provided duration");
2820 *duration = parse->priv->duration;
2822 } else if (parse->priv->duration != -1) {
2823 GST_LOG_OBJECT (parse, "converting provided duration");
2824 res = gst_base_parse_convert (parse, parse->priv->duration_fmt,
2825 parse->priv->duration, format, (gint64 *) duration);
2826 } else if (format == GST_FORMAT_TIME && parse->priv->estimated_duration != -1) {
2827 GST_LOG_OBJECT (parse, "using estimated duration");
2828 *duration = parse->priv->estimated_duration;
2832 GST_LOG_OBJECT (parse, "res: %d, duration %" GST_TIME_FORMAT, res,
2833 GST_TIME_ARGS (*duration));
2838 * gst_base_parse_get_querytypes:
2841 * Returns: A table of #GstQueryType items describing supported query types.
2843 static const GstQueryType *
2844 gst_base_parse_get_querytypes (GstPad * pad)
2846 static const GstQueryType list[] = {
2860 * gst_base_parse_query:
2862 * @query: #GstQuery.
2864 * Returns: TRUE on success.
2867 gst_base_parse_query (GstPad * pad, GstQuery * query)
2869 GstBaseParse *parse;
2870 gboolean res = FALSE;
2872 parse = GST_BASE_PARSE (GST_PAD_PARENT (pad));
2874 GST_LOG_OBJECT (parse, "handling query: %" GST_PTR_FORMAT, query);
2876 switch (GST_QUERY_TYPE (query)) {
2877 case GST_QUERY_POSITION:
2882 GST_DEBUG_OBJECT (parse, "position query");
2883 gst_query_parse_position (query, &format, NULL);
2885 GST_OBJECT_LOCK (parse);
2886 if (format == GST_FORMAT_BYTES) {
2887 dest_value = parse->priv->offset;
2889 } else if (format == parse->segment.format &&
2890 GST_CLOCK_TIME_IS_VALID (parse->segment.last_stop)) {
2891 dest_value = parse->segment.last_stop;
2894 GST_OBJECT_UNLOCK (parse);
2897 gst_query_set_position (query, format, dest_value);
2899 res = gst_pad_query_default (pad, query);
2901 /* no precise result, upstream no idea either, then best estimate */
2902 /* priv->offset is updated in both PUSH/PULL modes */
2903 res = gst_base_parse_convert (parse,
2904 GST_FORMAT_BYTES, parse->priv->offset, format, &dest_value);
2909 case GST_QUERY_DURATION:
2912 GstClockTime duration;
2914 GST_DEBUG_OBJECT (parse, "duration query");
2915 gst_query_parse_duration (query, &format, NULL);
2917 /* consult upstream */
2918 res = gst_pad_query_default (pad, query);
2920 /* otherwise best estimate from us */
2922 res = gst_base_parse_get_duration (parse, format, &duration);
2924 gst_query_set_duration (query, format, duration);
2928 case GST_QUERY_SEEKING:
2931 GstClockTime duration = GST_CLOCK_TIME_NONE;
2932 gboolean seekable = FALSE;
2934 GST_DEBUG_OBJECT (parse, "seeking query");
2935 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
2937 /* consult upstream */
2938 res = gst_pad_query_default (pad, query);
2940 /* we may be able to help if in TIME */
2941 if (fmt == GST_FORMAT_TIME &&
2942 parse->priv->seekable > GST_BASE_PARSE_SEEK_NONE) {
2943 gst_query_parse_seeking (query, &fmt, &seekable, NULL, NULL);
2944 /* already OK if upstream takes care */
2945 GST_LOG_OBJECT (parse, "upstream handled %d, seekable %d",
2947 if (!(res && seekable)) {
2948 if (!gst_base_parse_get_duration (parse, GST_FORMAT_TIME, &duration)
2949 || duration == -1) {
2952 seekable = parse->priv->upstream_seekable;
2953 GST_LOG_OBJECT (parse, "already determine upstream seekabled: %d",
2956 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable, 0, duration);
2962 case GST_QUERY_FORMATS:
2963 gst_query_set_formatsv (query, 3, fmtlist);
2966 case GST_QUERY_CONVERT:
2968 GstFormat src_format, dest_format;
2969 gint64 src_value, dest_value;
2971 gst_query_parse_convert (query, &src_format, &src_value,
2972 &dest_format, &dest_value);
2974 res = gst_base_parse_convert (parse, src_format, src_value,
2975 dest_format, &dest_value);
2977 gst_query_set_convert (query, src_format, src_value,
2978 dest_format, dest_value);
2983 res = gst_pad_query_default (pad, query);
2989 /* scans for a cluster start from @pos,
2990 * return GST_FLOW_OK and frame position/time in @pos/@time if found */
2991 static GstFlowReturn
2992 gst_base_parse_find_frame (GstBaseParse * parse, gint64 * pos,
2993 GstClockTime * time, GstClockTime * duration)
2995 GstBaseParseClass *klass;
2997 gboolean orig_drain, orig_discont;
2998 GstFlowReturn ret = GST_FLOW_OK;
2999 GstBuffer *buf = NULL;
3000 GstBaseParseFrame frame = { 0, };
3002 g_return_val_if_fail (GST_FLOW_ERROR, pos != NULL);
3003 g_return_val_if_fail (GST_FLOW_ERROR, time != NULL);
3004 g_return_val_if_fail (GST_FLOW_ERROR, duration != NULL);
3006 klass = GST_BASE_PARSE_GET_CLASS (parse);
3008 *time = GST_CLOCK_TIME_NONE;
3009 *duration = GST_CLOCK_TIME_NONE;
3012 orig_offset = parse->priv->offset;
3013 orig_discont = parse->priv->discont;
3014 orig_drain = parse->priv->drain;
3016 GST_DEBUG_OBJECT (parse, "scanning for frame starting at %" G_GINT64_FORMAT
3017 " (%#" G_GINT64_MODIFIER "x)", *pos, *pos);
3019 /* jump elsewhere and locate next frame */
3020 parse->priv->offset = *pos;
3021 ret = gst_base_parse_scan_frame (parse, klass, &frame, FALSE);
3022 if (ret != GST_FLOW_OK)
3026 GST_LOG_OBJECT (parse,
3027 "peek parsing frame at offset %" G_GUINT64_FORMAT
3028 " (%#" G_GINT64_MODIFIER "x) of size %d",
3029 GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET (buf), GST_BUFFER_SIZE (buf));
3031 /* get offset first, subclass parsing might dump other stuff in there */
3032 *pos = GST_BUFFER_OFFSET (buf);
3033 ret = klass->parse_frame (parse, &frame);
3036 /* but it should provide proper time */
3037 *time = GST_BUFFER_TIMESTAMP (buf);
3038 *duration = GST_BUFFER_DURATION (buf);
3039 gst_base_parse_frame_clear (parse, &frame);
3041 GST_LOG_OBJECT (parse,
3042 "frame with time %" GST_TIME_FORMAT " at offset %" G_GINT64_FORMAT,
3043 GST_TIME_ARGS (*time), *pos);
3047 parse->priv->offset = orig_offset;
3048 parse->priv->discont = orig_discont;
3049 parse->priv->drain = orig_drain;
3054 /* bisect and scan through file for frame starting before @time,
3055 * returns OK and @time/@offset if found, NONE and/or error otherwise
3056 * If @time == G_MAXINT64, scan for duration ( == last frame) */
3057 static GstFlowReturn
3058 gst_base_parse_locate_time (GstBaseParse * parse, GstClockTime * _time,
3061 GstFlowReturn ret = GST_FLOW_OK;
3062 gint64 lpos, hpos, newpos;
3063 GstClockTime time, ltime, htime, newtime, dur;
3064 gboolean cont = TRUE;
3065 const GstClockTime tolerance = TARGET_DIFFERENCE;
3066 const guint chunk = 4 * 1024;
3068 g_return_val_if_fail (_time != NULL, GST_FLOW_ERROR);
3069 g_return_val_if_fail (_offset != NULL, GST_FLOW_ERROR);
3071 /* TODO also make keyframe aware if useful some day */
3086 /* do not know at first */
3088 *_time = GST_CLOCK_TIME_NONE;
3090 /* need initial positions; start and end */
3091 lpos = parse->priv->first_frame_offset;
3092 ltime = parse->priv->first_frame_ts;
3093 htime = parse->priv->duration;
3094 hpos = parse->priv->upstream_size;
3096 /* check preconditions are satisfied;
3097 * start and end are needed, except for special case where we scan for
3098 * last frame to determine duration */
3099 if (parse->priv->pad_mode != GST_ACTIVATE_PULL || !hpos ||
3100 !GST_CLOCK_TIME_IS_VALID (ltime) ||
3101 (!GST_CLOCK_TIME_IS_VALID (htime) && time != G_MAXINT64)) {
3105 /* shortcut cases */
3108 } else if (time < ltime + tolerance) {
3112 } else if (time >= htime) {
3118 while (htime > ltime && cont) {
3119 GST_LOG_OBJECT (parse,
3120 "lpos: %" G_GUINT64_FORMAT ", ltime: %" GST_TIME_FORMAT, lpos,
3121 GST_TIME_ARGS (ltime));
3122 GST_LOG_OBJECT (parse,
3123 "hpos: %" G_GUINT64_FORMAT ", htime: %" GST_TIME_FORMAT, hpos,
3124 GST_TIME_ARGS (htime));
3125 if (G_UNLIKELY (time == G_MAXINT64)) {
3127 } else if (G_LIKELY (hpos > lpos)) {
3129 gst_util_uint64_scale (hpos - lpos, time - ltime, htime - ltime) +
3132 /* should mean lpos == hpos, since lpos <= hpos is invariant */
3134 /* we check this case once, but not forever, so break loop */
3139 newpos = CLAMP (newpos, lpos, hpos);
3140 GST_LOG_OBJECT (parse,
3141 "estimated _offset for %" GST_TIME_FORMAT ": %" G_GINT64_FORMAT,
3142 GST_TIME_ARGS (time), newpos);
3144 ret = gst_base_parse_find_frame (parse, &newpos, &newtime, &dur);
3145 if (ret == GST_FLOW_UNEXPECTED) {
3146 /* heuristic HACK */
3147 hpos = MAX (lpos, hpos - chunk);
3149 } else if (ret != GST_FLOW_OK) {
3153 if (newtime == -1 || newpos == -1) {
3154 GST_DEBUG_OBJECT (parse, "subclass did not provide metadata; aborting");
3158 if (G_UNLIKELY (time == G_MAXINT64)) {
3161 if (GST_CLOCK_TIME_IS_VALID (dur))
3164 } else if (newtime > time) {
3166 hpos = (newpos >= hpos) ? MAX (lpos, hpos - chunk) : MAX (lpos, newpos);
3168 } else if (newtime + tolerance > time) {
3169 /* close enough undershoot */
3173 } else if (newtime < ltime) {
3174 /* so a position beyond lpos resulted in earlier time than ltime ... */
3175 GST_DEBUG_OBJECT (parse, "non-ascending time; aborting");
3178 /* undershoot too far */
3179 newpos += newpos == lpos ? chunk : 0;
3180 lpos = CLAMP (newpos, lpos, hpos);
3186 GST_LOG_OBJECT (parse, "return offset %" G_GINT64_FORMAT ", time %"
3187 GST_TIME_FORMAT, *_offset, GST_TIME_ARGS (*_time));
3192 gst_base_parse_find_offset (GstBaseParse * parse, GstClockTime time,
3193 gboolean before, GstClockTime * _ts)
3195 gint64 bytes = 0, ts = 0;
3196 GstIndexEntry *entry = NULL;
3198 if (time == GST_CLOCK_TIME_NONE) {
3204 GST_OBJECT_LOCK (parse);
3205 if (parse->priv->index) {
3206 /* Let's check if we have an index entry for that time */
3207 entry = gst_index_get_assoc_entry (parse->priv->index,
3208 parse->priv->index_id,
3209 before ? GST_INDEX_LOOKUP_BEFORE : GST_INDEX_LOOKUP_AFTER,
3210 GST_ASSOCIATION_FLAG_KEY_UNIT, GST_FORMAT_TIME, time);
3214 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &bytes);
3215 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &ts);
3217 GST_DEBUG_OBJECT (parse, "found index entry for %" GST_TIME_FORMAT
3218 " at %" GST_TIME_FORMAT ", offset %" G_GINT64_FORMAT,
3219 GST_TIME_ARGS (time), GST_TIME_ARGS (ts), bytes);
3221 GST_DEBUG_OBJECT (parse, "no index entry found for %" GST_TIME_FORMAT,
3222 GST_TIME_ARGS (time));
3225 ts = GST_CLOCK_TIME_NONE;
3228 GST_OBJECT_UNLOCK (parse);
3239 * gst_base_parse_handle_seek:
3240 * @parse: #GstBaseParse.
3241 * @event: #GstEvent.
3243 * Returns: TRUE if seek succeeded.
3246 gst_base_parse_handle_seek (GstBaseParse * parse, GstEvent * event)
3251 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
3252 gboolean flush, update, res = TRUE, accurate;
3253 gint64 cur, stop, seekpos, seekstop;
3254 GstSegment seeksegment = { 0, };
3255 GstFormat dstformat;
3256 GstClockTime start_ts;
3258 gst_event_parse_seek (event, &rate, &format, &flags,
3259 &cur_type, &cur, &stop_type, &stop);
3261 GST_DEBUG_OBJECT (parse, "seek to format %s, rate %f, "
3262 "start type %d at %" GST_TIME_FORMAT ", end type %d at %"
3263 GST_TIME_FORMAT, gst_format_get_name (format), rate,
3264 cur_type, GST_TIME_ARGS (cur), stop_type, GST_TIME_ARGS (stop));
3266 /* no negative rates in push mode */
3267 if (rate < 0.0 && parse->priv->pad_mode == GST_ACTIVATE_PUSH)
3270 if (cur_type != GST_SEEK_TYPE_SET ||
3271 (stop_type != GST_SEEK_TYPE_SET && stop_type != GST_SEEK_TYPE_NONE))
3274 /* For any format other than TIME, see if upstream handles
3275 * it directly or fail. For TIME, try upstream, but do it ourselves if
3276 * it fails upstream */
3277 if (format != GST_FORMAT_TIME) {
3278 /* default action delegates to upstream */
3282 gst_event_ref (event);
3283 if ((res = gst_pad_push_event (parse->sinkpad, event))) {
3288 /* get flush flag */
3289 flush = flags & GST_SEEK_FLAG_FLUSH;
3291 /* copy segment, we need this because we still need the old
3292 * segment when we close the current segment. */
3293 memcpy (&seeksegment, &parse->segment, sizeof (GstSegment));
3295 GST_DEBUG_OBJECT (parse, "configuring seek");
3296 gst_segment_set_seek (&seeksegment, rate, format, flags,
3297 cur_type, cur, stop_type, stop, &update);
3299 /* accurate seeking implies seek tables are used to obtain position,
3300 * and the requested segment is maintained exactly, not adjusted any way */
3301 accurate = flags & GST_SEEK_FLAG_ACCURATE;
3303 /* maybe we can be accurate for (almost) free */
3304 gst_base_parse_find_offset (parse, seeksegment.last_stop, TRUE, &start_ts);
3305 if (seeksegment.last_stop <= start_ts + TARGET_DIFFERENCE) {
3306 GST_DEBUG_OBJECT (parse, "accurate seek possible");
3310 GstClockTime startpos = seeksegment.last_stop;
3312 /* accurate requested, so ... seek a bit before target */
3313 if (startpos < parse->priv->lead_in_ts)
3316 startpos -= parse->priv->lead_in_ts;
3317 seekpos = gst_base_parse_find_offset (parse, startpos, TRUE, &start_ts);
3318 seekstop = gst_base_parse_find_offset (parse, seeksegment.stop, FALSE,
3321 start_ts = seeksegment.last_stop;
3322 dstformat = GST_FORMAT_BYTES;
3323 if (!gst_pad_query_convert (parse->srcpad, format, seeksegment.last_stop,
3324 &dstformat, &seekpos))
3325 goto convert_failed;
3326 if (!gst_pad_query_convert (parse->srcpad, format, seeksegment.stop,
3327 &dstformat, &seekstop))
3328 goto convert_failed;
3331 GST_DEBUG_OBJECT (parse,
3332 "seek position %" G_GINT64_FORMAT " in bytes: %" G_GINT64_FORMAT,
3334 GST_DEBUG_OBJECT (parse,
3335 "seek stop %" G_GINT64_FORMAT " in bytes: %" G_GINT64_FORMAT,
3336 seeksegment.stop, seekstop);
3338 if (parse->priv->pad_mode == GST_ACTIVATE_PULL) {
3341 GST_DEBUG_OBJECT (parse, "seek in PULL mode");
3344 if (parse->srcpad) {
3345 GST_DEBUG_OBJECT (parse, "sending flush start");
3346 gst_pad_push_event (parse->srcpad, gst_event_new_flush_start ());
3347 /* unlock upstream pull_range */
3348 gst_pad_push_event (parse->sinkpad, gst_event_new_flush_start ());
3351 gst_pad_pause_task (parse->sinkpad);
3354 /* we should now be able to grab the streaming thread because we stopped it
3355 * with the above flush/pause code */
3356 GST_PAD_STREAM_LOCK (parse->sinkpad);
3358 /* save current position */
3359 last_stop = parse->segment.last_stop;
3360 GST_DEBUG_OBJECT (parse, "stopped streaming at %" G_GINT64_FORMAT,
3363 /* now commit to new position */
3365 /* prepare for streaming again */
3367 GST_DEBUG_OBJECT (parse, "sending flush stop");
3368 gst_pad_push_event (parse->srcpad, gst_event_new_flush_stop ());
3369 gst_pad_push_event (parse->sinkpad, gst_event_new_flush_stop ());
3370 gst_base_parse_clear_queues (parse);
3372 if (parse->close_segment)
3373 gst_event_unref (parse->close_segment);
3375 parse->close_segment = gst_event_new_new_segment (TRUE,
3376 parse->segment.rate, parse->segment.format,
3377 parse->segment.accum, parse->segment.last_stop, parse->segment.accum);
3379 /* keep track of our last_stop */
3380 seeksegment.accum = parse->segment.last_stop;
3382 GST_DEBUG_OBJECT (parse, "Created close seg format %d, "
3383 "start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT
3384 ", pos = %" GST_TIME_FORMAT, format,
3385 GST_TIME_ARGS (parse->segment.accum),
3386 GST_TIME_ARGS (parse->segment.last_stop),
3387 GST_TIME_ARGS (parse->segment.accum));
3390 memcpy (&parse->segment, &seeksegment, sizeof (GstSegment));
3392 /* store the newsegment event so it can be sent from the streaming thread. */
3393 if (parse->pending_segment)
3394 gst_event_unref (parse->pending_segment);
3396 /* This will be sent later in _loop() */
3397 parse->pending_segment =
3398 gst_event_new_new_segment (FALSE, parse->segment.rate,
3399 parse->segment.format, parse->segment.start, parse->segment.stop,
3400 parse->segment.start);
3402 GST_DEBUG_OBJECT (parse, "Created newseg format %d, "
3403 "start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT
3404 ", pos = %" GST_TIME_FORMAT, format,
3405 GST_TIME_ARGS (parse->segment.start),
3406 GST_TIME_ARGS (parse->segment.stop),
3407 GST_TIME_ARGS (parse->segment.start));
3409 /* one last chance in pull mode to stay accurate;
3410 * maybe scan and subclass can find where to go */
3413 GstClockTime ts = seeksegment.last_stop;
3415 gst_base_parse_locate_time (parse, &ts, &scanpos);
3419 /* running collected index now consists of several intervals,
3420 * so optimized check no longer possible */
3421 parse->priv->index_last_valid = FALSE;
3422 parse->priv->index_last_offset = 0;
3423 parse->priv->index_last_ts = 0;
3427 /* mark discont if we are going to stream from another position. */
3428 if (seekpos != parse->priv->offset) {
3429 GST_DEBUG_OBJECT (parse,
3430 "mark DISCONT, we did a seek to another position");
3431 parse->priv->offset = seekpos;
3432 parse->priv->last_offset = seekpos;
3433 parse->priv->seen_keyframe = FALSE;
3434 parse->priv->discont = TRUE;
3435 parse->priv->next_ts = start_ts;
3436 parse->priv->last_ts = GST_CLOCK_TIME_NONE;
3437 parse->priv->sync_offset = seekpos;
3438 parse->priv->exact_position = accurate;
3441 /* Start streaming thread if paused */
3442 gst_pad_start_task (parse->sinkpad,
3443 (GstTaskFunction) gst_base_parse_loop, parse->sinkpad);
3445 GST_PAD_STREAM_UNLOCK (parse->sinkpad);
3450 GstEvent *new_event;
3451 GstBaseParseSeek *seek;
3453 /* The only thing we need to do in PUSH-mode is to send the
3454 seek event (in bytes) to upstream. Segment / flush handling happens
3455 in corresponding src event handlers */
3456 GST_DEBUG_OBJECT (parse, "seek in PUSH mode");
3457 if (seekstop >= 0 && seekpos <= seekpos)
3459 new_event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flush,
3460 GST_SEEK_TYPE_SET, seekpos, stop_type, seekstop);
3462 /* store segment info so its precise details can be reconstructed when
3463 * receiving newsegment;
3464 * this matters for all details when accurate seeking,
3465 * is most useful to preserve NONE stop time otherwise */
3466 seek = g_new0 (GstBaseParseSeek, 1);
3467 seek->segment = seeksegment;
3468 seek->accurate = accurate;
3469 seek->offset = seekpos;
3470 seek->start_ts = start_ts;
3471 GST_OBJECT_LOCK (parse);
3472 /* less optimal, but preserves order */
3473 parse->priv->pending_seeks =
3474 g_slist_append (parse->priv->pending_seeks, seek);
3475 GST_OBJECT_UNLOCK (parse);
3477 res = gst_pad_push_event (parse->sinkpad, new_event);
3480 GST_OBJECT_LOCK (parse);
3481 parse->priv->pending_seeks =
3482 g_slist_remove (parse->priv->pending_seeks, seek);
3483 GST_OBJECT_UNLOCK (parse);
3489 /* handled event is ours to free */
3491 gst_event_unref (event);
3497 GST_DEBUG_OBJECT (parse, "negative playback rates are not supported yet.");
3503 GST_DEBUG_OBJECT (parse, "unsupported seek type.");
3509 GST_DEBUG_OBJECT (parse, "conversion TIME to BYTES failed.");
3516 * gst_base_parse_handle_tag:
3517 * @parse: #GstBaseParse.
3518 * @event: #GstEvent.
3520 * Checks if bitrates are available from upstream tags so that we don't
3521 * override them later
3524 gst_base_parse_handle_tag (GstBaseParse * parse, GstEvent * event)
3526 GstTagList *taglist = NULL;
3529 gst_event_parse_tag (event, &taglist);
3531 if (gst_tag_list_get_uint (taglist, GST_TAG_MINIMUM_BITRATE, &tmp)) {
3532 GST_DEBUG_OBJECT (parse, "upstream min bitrate %d", tmp);
3533 parse->priv->post_min_bitrate = FALSE;
3535 if (gst_tag_list_get_uint (taglist, GST_TAG_BITRATE, &tmp)) {
3536 GST_DEBUG_OBJECT (parse, "upstream avg bitrate %d", tmp);
3537 parse->priv->post_avg_bitrate = FALSE;
3539 if (gst_tag_list_get_uint (taglist, GST_TAG_MAXIMUM_BITRATE, &tmp)) {
3540 GST_DEBUG_OBJECT (parse, "upstream max bitrate %d", tmp);
3541 parse->priv->post_max_bitrate = FALSE;
3546 * gst_base_parse_sink_setcaps:
3550 * Returns: TRUE if caps were accepted.
3553 gst_base_parse_sink_setcaps (GstPad * pad, GstCaps * caps)
3555 GstBaseParse *parse;
3556 GstBaseParseClass *klass;
3557 gboolean res = TRUE;
3559 parse = GST_BASE_PARSE (GST_PAD_PARENT (pad));
3560 klass = GST_BASE_PARSE_GET_CLASS (parse);
3562 GST_DEBUG_OBJECT (parse, "caps: %" GST_PTR_FORMAT, caps);
3564 if (klass->set_sink_caps)
3565 res = klass->set_sink_caps (parse, caps);
3571 gst_base_parse_set_index (GstElement * element, GstIndex * index)
3573 GstBaseParse *parse = GST_BASE_PARSE (element);
3575 GST_OBJECT_LOCK (parse);
3576 if (parse->priv->index)
3577 gst_object_unref (parse->priv->index);
3579 parse->priv->index = gst_object_ref (index);
3580 gst_index_get_writer_id (index, GST_OBJECT (element),
3581 &parse->priv->index_id);
3582 parse->priv->own_index = FALSE;
3584 parse->priv->index = NULL;
3585 GST_OBJECT_UNLOCK (parse);
3589 gst_base_parse_get_index (GstElement * element)
3591 GstBaseParse *parse = GST_BASE_PARSE (element);
3592 GstIndex *result = NULL;
3594 GST_OBJECT_LOCK (parse);
3595 if (parse->priv->index)
3596 result = gst_object_ref (parse->priv->index);
3597 GST_OBJECT_UNLOCK (parse);
3602 static GstStateChangeReturn
3603 gst_base_parse_change_state (GstElement * element, GstStateChange transition)
3605 GstBaseParse *parse;
3606 GstStateChangeReturn result;
3608 parse = GST_BASE_PARSE (element);
3610 switch (transition) {
3611 case GST_STATE_CHANGE_READY_TO_PAUSED:
3612 /* If this is our own index destroy it as the
3613 * old entries might be wrong for the new stream */
3614 if (parse->priv->own_index) {
3615 gst_object_unref (parse->priv->index);
3616 parse->priv->index = NULL;
3617 parse->priv->own_index = FALSE;
3620 /* If no index was created, generate one */
3621 if (G_UNLIKELY (!parse->priv->index)) {
3622 GST_DEBUG_OBJECT (parse, "no index provided creating our own");
3624 parse->priv->index = gst_index_factory_make ("memindex");
3625 gst_index_get_writer_id (parse->priv->index, GST_OBJECT (parse),
3626 &parse->priv->index_id);
3627 parse->priv->own_index = TRUE;
3634 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3636 switch (transition) {
3637 case GST_STATE_CHANGE_PAUSED_TO_READY:
3638 gst_base_parse_reset (parse);