2 * Copyright (C) 2008 Nokia Corporation. All rights reserved.
3 * Contact: Stefan Kost <stefan.kost@nokia.com>
4 * Copyright (C) 2008 Sebastian Dröge <sebastian.droege@collabora.co.uk>.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * SECTION:gstbaseparse
24 * @short_description: Base class for stream parsers
25 * @see_also: #GstBaseTransform
27 * This base class is for parser elements that process data and splits it
28 * into separate audio/video/whatever frames.
32 * <listitem><para>One sinkpad and one srcpad</para></listitem>
33 * <listitem><para>Handles state changes</para></listitem>
34 * <listitem><para>Does flushing</para></listitem>
35 * <listitem><para>Push mode</para></listitem>
36 * <listitem><para>Pull mode</para></listitem>
37 * <listitem><para>Handles events (NEWSEGMENT/EOS/FLUSH)</para></listitem>
38 * <listitem><para>Handles seeking in both modes</para></listitem>
40 * Handles POSITION/DURATION/SEEKING/FORMAT/CONVERT queries
44 * The purpose of this base class is to provide a basic functionality of
45 * a parser and share a lot of rather complex code.
47 * Description of the parsing mechanism:
50 * <itemizedlist><title>Set-up phase</title>
52 * GstBaseParse class calls @set_sink_caps to inform the subclass about
53 * incoming sinkpad caps. Subclass should set the srcpad caps accordingly.
56 * GstBaseParse calls @start to inform subclass that data processing is
60 * At least in this point subclass needs to tell the GstBaseParse class
61 * how big data chunks it wants to receive (min_frame_size). It can do
62 * this with @gst_base_parse_set_min_frame_size.
65 * GstBaseParse class sets up appropriate data passing mode (pull/push)
66 * and starts to process the data.
72 * <title>Parsing phase</title>
74 * GstBaseParse gathers at least min_frame_size bytes of data either
75 * by pulling it from upstream or collecting buffers into internal
79 * A buffer of min_frame_size bytes is passed to subclass with
80 * @check_valid_frame. Subclass checks the contents and returns TRUE
81 * if the buffer contains a valid frame. It also needs to set the
82 * @framesize according to the detected frame size. If buffer didn't
83 * contain a valid frame, this call must return FALSE and optionally
84 * set the @skipsize value to inform base class that how many bytes
85 * it needs to skip in order to find a valid frame. The passed buffer
86 * is read-only. Note that @check_valid_frame might receive any small
87 * amount of input data when leftover data is being drained (e.g. at EOS).
90 * After valid frame is found, it will be passed again to subclass with
91 * @parse_frame call. Now subclass is responsible for parsing the
92 * frame contents and setting the caps, buffer timestamp and duration
93 * (although the latter can also be done by GstBaseParse if it is
94 * appropriately configured, see below).
97 * Finally the buffer can be pushed downstream and parsing loop starts
98 * over again. Just prior to actually pushing the buffer in question,
99 * it is passed to @pre_push_buffer which gives subclass yet one
100 * last chance to examine buffer metadata, or to send some custom (tag)
101 * events, or to perform custom (segment) filtering.
104 * During the parsing process GstBaseParseClass will handle both srcpad and
105 * sinkpad events. They will be passed to subclass if @event or
106 * @src_event callbacks have been provided.
111 * <itemizedlist><title>Shutdown phase</title>
113 * GstBaseParse class calls @stop to inform the subclass that data
114 * parsing will be stopped.
120 * Subclass is responsible for providing pad template caps for
121 * source and sink pads. The pads need to be named "sink" and "src". It also
122 * needs to set the fixed caps on srcpad, when the format is ensured (e.g.
123 * when base class calls subclass' @set_sink_caps function).
125 * This base class uses GST_FORMAT_DEFAULT as a meaning of frames. So,
126 * subclass conversion routine needs to know that conversion from
127 * GST_FORMAT_TIME to GST_FORMAT_DEFAULT must return the
128 * frame number that can be found from the given byte position.
130 * GstBaseParse uses subclasses conversion methods also for seeking (or otherwise
131 * uses its own default one, see also below).
133 * Subclass @start and @stop functions will be called to inform the beginning
134 * and end of data processing.
136 * Things that subclass need to take care of:
138 * <listitem><para>Provide pad templates</para></listitem>
140 * Fixate the source pad caps when appropriate
143 * Inform base class how big data chunks should be retrieved. This is
144 * done with @gst_base_parse_set_min_frame_size function.
147 * Examine data chunks passed to subclass with @check_valid_frame
148 * and tell if they contain a valid frame
151 * Set the caps and timestamp to frame that is passed to subclass with
152 * @parse_frame function.
154 * <listitem><para>Provide conversion functions</para></listitem>
156 * Update the duration information with @gst_base_parse_set_duration
159 * Optionally passthrough using @gst_base_parse_set_passthrough
162 * Configure various baseparse parameters using @gst_base_parse_set_seek and
163 * @gst_base_parse_set_frame_props.
166 * In particular, if subclass is unable to determine a duration, but
167 * parsing (or specs) yields a frames per seconds rate, then this can be
168 * provided to GstBaseParse to enable it to cater for
169 * buffer time metadata (which will be taken from upstream as much as possible).
170 * Internally keeping track of frame durations and respective
171 * sizes that have been pushed provides GstBaseParse with an estimated bitrate.
172 * A default @convert (used if not overriden) will then use these
173 * rates to perform obvious conversions. These rates are also used to update
174 * (estimated) duration at regular frame intervals.
181 * - In push mode provide a queue of adapter-"queued" buffers for upstream
183 * - Queue buffers/events until caps are set
193 #include "gstbaseparse.h"
195 #define MIN_FRAMES_TO_POST_BITRATE 10
197 GST_DEBUG_CATEGORY_STATIC (gst_base_parse_debug);
198 #define GST_CAT_DEFAULT gst_base_parse_debug
200 /* Supported formats */
201 static GstFormat fmtlist[] = {
208 #define GST_BASE_PARSE_GET_PRIVATE(obj) \
209 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_PARSE, GstBaseParsePrivate))
211 struct _GstBaseParsePrivate
213 GstActivateMode pad_mode;
216 GstFormat duration_fmt;
217 gint64 estimated_duration;
219 guint min_frame_size;
220 gboolean passthrough;
221 guint fps_num, fps_den;
222 guint update_interval;
224 guint lead_in, lead_out;
225 GstClockTime lead_in_ts, lead_out_ts;
226 GstBaseParseSeekable seekable;
234 GstClockTime next_ts;
235 GstClockTime prev_ts;
236 GstClockTime frame_duration;
240 guint64 data_bytecount;
241 guint64 acc_duration;
243 gboolean post_min_bitrate;
244 gboolean post_avg_bitrate;
245 gboolean post_max_bitrate;
249 guint posted_avg_bitrate;
251 GList *pending_events;
255 /* index entry storage, either ours or provided */
259 /* seek table entries only maintained if upstream is BYTE seekable */
260 gboolean upstream_seekable;
261 /* minimum distance between two index entries */
262 GstClockTimeDiff idx_interval;
263 /* ts and offset of last entry added */
264 GstClockTime index_last_ts;
265 guint64 index_last_offset;
267 /* timestamps currently produced are accurate, e.g. started from 0 onwards */
268 gboolean exact_position;
269 /* seek events are temporarily kept to match them with newsegments */
270 GSList *pending_seeks;
272 /* reverse playback */
273 GSList *buffers_pending;
274 GSList *buffers_queued;
275 GstClockTime last_ts;
279 typedef struct _GstBaseParseSeek
284 GstClockTime start_ts;
287 static GstElementClass *parent_class = NULL;
289 static void gst_base_parse_class_init (GstBaseParseClass * klass);
290 static void gst_base_parse_init (GstBaseParse * parse,
291 GstBaseParseClass * klass);
294 gst_base_parse_get_type (void)
296 static GType base_parse_type = 0;
298 if (!base_parse_type) {
299 static const GTypeInfo base_parse_info = {
300 sizeof (GstBaseParseClass),
301 (GBaseInitFunc) NULL,
302 (GBaseFinalizeFunc) NULL,
303 (GClassInitFunc) gst_base_parse_class_init,
306 sizeof (GstBaseParse),
308 (GInstanceInitFunc) gst_base_parse_init,
311 base_parse_type = g_type_register_static (GST_TYPE_ELEMENT,
312 "GstAudioBaseParseBad", &base_parse_info, G_TYPE_FLAG_ABSTRACT);
314 return base_parse_type;
317 static void gst_base_parse_finalize (GObject * object);
319 static GstStateChangeReturn gst_base_parse_change_state (GstElement * element,
320 GstStateChange transition);
321 static void gst_base_parse_reset (GstBaseParse * parse);
323 static void gst_base_parse_set_index (GstElement * element, GstIndex * index);
324 static GstIndex *gst_base_parse_get_index (GstElement * element);
326 static gboolean gst_base_parse_sink_activate (GstPad * sinkpad);
327 static gboolean gst_base_parse_sink_activate_push (GstPad * pad,
329 static gboolean gst_base_parse_sink_activate_pull (GstPad * pad,
331 static gboolean gst_base_parse_handle_seek (GstBaseParse * parse,
333 static void gst_base_parse_handle_tag (GstBaseParse * parse, GstEvent * event);
335 static gboolean gst_base_parse_src_event (GstPad * pad, GstEvent * event);
336 static gboolean gst_base_parse_sink_event (GstPad * pad, GstEvent * event);
337 static gboolean gst_base_parse_query (GstPad * pad, GstQuery * query);
338 static gboolean gst_base_parse_sink_setcaps (GstPad * pad, GstCaps * caps);
339 static const GstQueryType *gst_base_parse_get_querytypes (GstPad * pad);
341 static GstFlowReturn gst_base_parse_chain (GstPad * pad, GstBuffer * buffer);
342 static void gst_base_parse_loop (GstPad * pad);
344 static gboolean gst_base_parse_check_frame (GstBaseParse * parse,
345 GstBuffer * buffer, guint * framesize, gint * skipsize);
347 static GstFlowReturn gst_base_parse_parse_frame (GstBaseParse * parse,
350 static gboolean gst_base_parse_sink_eventfunc (GstBaseParse * parse,
353 static gboolean gst_base_parse_src_eventfunc (GstBaseParse * parse,
356 static void gst_base_parse_drain (GstBaseParse * parse);
358 static void gst_base_parse_post_bitrates (GstBaseParse * parse,
359 gboolean post_min, gboolean post_avg, gboolean post_max);
361 static gint64 gst_base_parse_find_offset (GstBaseParse * parse,
362 GstClockTime time, gboolean before, GstClockTime * _ts);
364 static GstFlowReturn gst_base_parse_process_fragment (GstBaseParse * parse,
368 gst_base_parse_clear_queues (GstBaseParse * parse)
370 g_slist_foreach (parse->priv->buffers_queued, (GFunc) gst_buffer_unref, NULL);
371 g_slist_free (parse->priv->buffers_queued);
372 parse->priv->buffers_queued = NULL;
373 g_slist_foreach (parse->priv->buffers_pending, (GFunc) gst_buffer_unref,
375 g_slist_free (parse->priv->buffers_pending);
376 parse->priv->buffers_pending = NULL;
380 gst_base_parse_finalize (GObject * object)
382 GstBaseParse *parse = GST_BASE_PARSE (object);
385 g_mutex_free (parse->parse_lock);
386 g_object_unref (parse->adapter);
388 if (parse->pending_segment) {
389 p_ev = &parse->pending_segment;
390 gst_event_replace (p_ev, NULL);
392 if (parse->close_segment) {
393 p_ev = &parse->close_segment;
394 gst_event_replace (p_ev, NULL);
397 if (parse->priv->cache) {
398 gst_buffer_unref (parse->priv->cache);
399 parse->priv->cache = NULL;
402 g_list_foreach (parse->priv->pending_events, (GFunc) gst_mini_object_unref,
404 g_list_free (parse->priv->pending_events);
405 parse->priv->pending_events = NULL;
407 if (parse->priv->index) {
408 gst_object_unref (parse->priv->index);
409 parse->priv->index = NULL;
412 gst_base_parse_clear_queues (parse);
414 G_OBJECT_CLASS (parent_class)->finalize (object);
418 gst_base_parse_class_init (GstBaseParseClass * klass)
420 GObjectClass *gobject_class;
421 GstElementClass *gstelement_class;
423 gobject_class = G_OBJECT_CLASS (klass);
424 g_type_class_add_private (klass, sizeof (GstBaseParsePrivate));
425 parent_class = g_type_class_peek_parent (klass);
426 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_parse_finalize);
428 gstelement_class = (GstElementClass *) klass;
429 gstelement_class->change_state =
430 GST_DEBUG_FUNCPTR (gst_base_parse_change_state);
431 gstelement_class->set_index = GST_DEBUG_FUNCPTR (gst_base_parse_set_index);
432 gstelement_class->get_index = GST_DEBUG_FUNCPTR (gst_base_parse_get_index);
434 /* Default handlers */
435 klass->check_valid_frame = gst_base_parse_check_frame;
436 klass->parse_frame = gst_base_parse_parse_frame;
437 klass->src_event = gst_base_parse_src_eventfunc;
438 klass->convert = gst_base_parse_convert_default;
440 GST_DEBUG_CATEGORY_INIT (gst_base_parse_debug, "baseparse", 0,
441 "baseparse element");
445 gst_base_parse_init (GstBaseParse * parse, GstBaseParseClass * bclass)
447 GstPadTemplate *pad_template;
449 GST_DEBUG_OBJECT (parse, "gst_base_parse_init");
451 parse->priv = GST_BASE_PARSE_GET_PRIVATE (parse);
454 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "sink");
455 g_return_if_fail (pad_template != NULL);
456 parse->sinkpad = gst_pad_new_from_template (pad_template, "sink");
457 gst_pad_set_event_function (parse->sinkpad,
458 GST_DEBUG_FUNCPTR (gst_base_parse_sink_event));
459 gst_pad_set_setcaps_function (parse->sinkpad,
460 GST_DEBUG_FUNCPTR (gst_base_parse_sink_setcaps));
461 gst_pad_set_chain_function (parse->sinkpad,
462 GST_DEBUG_FUNCPTR (gst_base_parse_chain));
463 gst_pad_set_activate_function (parse->sinkpad,
464 GST_DEBUG_FUNCPTR (gst_base_parse_sink_activate));
465 gst_pad_set_activatepush_function (parse->sinkpad,
466 GST_DEBUG_FUNCPTR (gst_base_parse_sink_activate_push));
467 gst_pad_set_activatepull_function (parse->sinkpad,
468 GST_DEBUG_FUNCPTR (gst_base_parse_sink_activate_pull));
469 gst_element_add_pad (GST_ELEMENT (parse), parse->sinkpad);
471 GST_DEBUG_OBJECT (parse, "sinkpad created");
474 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
475 g_return_if_fail (pad_template != NULL);
476 parse->srcpad = gst_pad_new_from_template (pad_template, "src");
477 gst_pad_set_event_function (parse->srcpad,
478 GST_DEBUG_FUNCPTR (gst_base_parse_src_event));
479 gst_pad_set_query_type_function (parse->srcpad,
480 GST_DEBUG_FUNCPTR (gst_base_parse_get_querytypes));
481 gst_pad_set_query_function (parse->srcpad,
482 GST_DEBUG_FUNCPTR (gst_base_parse_query));
483 gst_pad_use_fixed_caps (parse->srcpad);
484 gst_element_add_pad (GST_ELEMENT (parse), parse->srcpad);
485 GST_DEBUG_OBJECT (parse, "src created");
487 parse->parse_lock = g_mutex_new ();
488 parse->adapter = gst_adapter_new ();
490 parse->priv->pad_mode = GST_ACTIVATE_NONE;
493 gst_base_parse_reset (parse);
494 GST_DEBUG_OBJECT (parse, "init ok");
498 gst_base_parse_reset (GstBaseParse * parse)
500 GST_OBJECT_LOCK (parse);
501 gst_segment_init (&parse->segment, GST_FORMAT_TIME);
502 parse->priv->duration = -1;
503 parse->priv->min_frame_size = 1;
504 parse->priv->discont = TRUE;
505 parse->priv->flushing = FALSE;
506 parse->priv->offset = 0;
507 parse->priv->sync_offset = 0;
508 parse->priv->update_interval = 50;
509 parse->priv->fps_num = parse->priv->fps_den = 0;
510 parse->priv->frame_duration = GST_CLOCK_TIME_NONE;
511 parse->priv->lead_in = parse->priv->lead_out = 0;
512 parse->priv->lead_in_ts = parse->priv->lead_out_ts = 0;
513 parse->priv->seekable = GST_BASE_PARSE_SEEK_DEFAULT;
514 parse->priv->bitrate = 0;
515 parse->priv->framecount = 0;
516 parse->priv->bytecount = 0;
517 parse->priv->acc_duration = 0;
518 parse->priv->estimated_duration = -1;
519 parse->priv->next_ts = 0;
520 parse->priv->passthrough = FALSE;
521 parse->priv->post_min_bitrate = TRUE;
522 parse->priv->post_avg_bitrate = TRUE;
523 parse->priv->post_max_bitrate = TRUE;
524 parse->priv->min_bitrate = G_MAXUINT;
525 parse->priv->max_bitrate = 0;
526 parse->priv->avg_bitrate = 0;
527 parse->priv->posted_avg_bitrate = 0;
529 parse->priv->index_last_ts = 0;
530 parse->priv->index_last_offset = 0;
531 parse->priv->upstream_seekable = FALSE;
532 parse->priv->idx_interval = 0;
533 parse->priv->exact_position = TRUE;
535 parse->priv->last_ts = GST_CLOCK_TIME_NONE;
536 parse->priv->last_offset = 0;
538 if (parse->pending_segment)
539 gst_event_unref (parse->pending_segment);
541 g_list_foreach (parse->priv->pending_events, (GFunc) gst_mini_object_unref,
543 g_list_free (parse->priv->pending_events);
544 parse->priv->pending_events = NULL;
546 if (parse->priv->cache) {
547 gst_buffer_unref (parse->priv->cache);
548 parse->priv->cache = NULL;
551 g_slist_foreach (parse->priv->pending_seeks, (GFunc) g_free, NULL);
552 g_slist_free (parse->priv->pending_seeks);
553 parse->priv->pending_seeks = NULL;
555 GST_OBJECT_UNLOCK (parse);
559 * gst_base_parse_check_frame:
560 * @parse: #GstBaseParse.
561 * @buffer: GstBuffer.
562 * @framesize: This will be set to tell the found frame size in bytes.
563 * @skipsize: Output parameter that tells how much data needs to be skipped
564 * in order to find the following frame header.
566 * Default callback for check_valid_frame.
568 * Returns: Always TRUE.
571 gst_base_parse_check_frame (GstBaseParse * parse,
572 GstBuffer * buffer, guint * framesize, gint * skipsize)
574 *framesize = GST_BUFFER_SIZE (buffer);
581 * gst_base_parse_parse_frame:
582 * @parse: #GstBaseParse.
583 * @buffer: #GstBuffer.
585 * Default callback for parse_frame.
588 gst_base_parse_parse_frame (GstBaseParse * parse, GstBuffer * buffer)
590 if (!GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
591 GST_CLOCK_TIME_IS_VALID (parse->priv->next_ts)) {
592 GST_BUFFER_TIMESTAMP (buffer) = parse->priv->next_ts;
594 if (!GST_BUFFER_DURATION_IS_VALID (buffer) &&
595 GST_CLOCK_TIME_IS_VALID (parse->priv->frame_duration)) {
596 GST_BUFFER_DURATION (buffer) = parse->priv->frame_duration;
602 * gst_base_parse_convert:
603 * @parse: #GstBaseParse.
604 * @src_format: #GstFormat describing the source format.
605 * @src_value: Source value to be converted.
606 * @dest_format: #GstFormat defining the converted format.
607 * @dest_value: Pointer where the conversion result will be put.
609 * Converts using configured "convert" vmethod in #GstBaseParse class.
611 * Returns: TRUE if conversion was successful.
614 gst_base_parse_convert (GstBaseParse * parse,
615 GstFormat src_format,
616 gint64 src_value, GstFormat dest_format, gint64 * dest_value)
618 GstBaseParseClass *klass = GST_BASE_PARSE_GET_CLASS (parse);
621 g_return_val_if_fail (dest_value != NULL, FALSE);
626 ret = klass->convert (parse, src_format, src_value, dest_format, dest_value);
628 #ifndef GST_DISABLE_GST_DEBUG
631 if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES) {
632 GST_LOG_OBJECT (parse,
633 "TIME -> BYTES: %" GST_TIME_FORMAT " -> %" G_GINT64_FORMAT,
634 GST_TIME_ARGS (src_value), *dest_value);
635 } else if (dest_format == GST_FORMAT_TIME &&
636 src_format == GST_FORMAT_BYTES) {
637 GST_LOG_OBJECT (parse,
638 "BYTES -> TIME: %" G_GINT64_FORMAT " -> %" GST_TIME_FORMAT,
639 src_value, GST_TIME_ARGS (*dest_value));
641 GST_LOG_OBJECT (parse,
642 "%s -> %s: %" G_GINT64_FORMAT " -> %" G_GINT64_FORMAT,
643 GST_STR_NULL (gst_format_get_name (src_format)),
644 GST_STR_NULL (gst_format_get_name (dest_format)),
645 src_value, *dest_value);
648 GST_DEBUG_OBJECT (parse, "conversion failed");
657 * gst_base_parse_sink_event:
658 * @pad: #GstPad that received the event.
659 * @event: #GstEvent to be handled.
661 * Handler for sink pad events.
663 * Returns: TRUE if the event was handled.
666 gst_base_parse_sink_event (GstPad * pad, GstEvent * event)
669 GstBaseParseClass *bclass;
670 gboolean handled = FALSE;
674 parse = GST_BASE_PARSE (gst_pad_get_parent (pad));
675 bclass = GST_BASE_PARSE_GET_CLASS (parse);
677 GST_DEBUG_OBJECT (parse, "handling event %d", GST_EVENT_TYPE (event));
679 /* Cache all events except EOS, NEWSEGMENT and FLUSH_STOP if we have a
681 if (parse->pending_segment && GST_EVENT_TYPE (event) != GST_EVENT_EOS
682 && GST_EVENT_TYPE (event) != GST_EVENT_NEWSEGMENT
683 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_START
684 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
686 if (GST_EVENT_TYPE (event) == GST_EVENT_TAG)
687 /* See if any bitrate tags were posted */
688 gst_base_parse_handle_tag (parse, event);
690 parse->priv->pending_events =
691 g_list_append (parse->priv->pending_events, event);
695 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS &&
696 parse->priv->framecount < MIN_FRAMES_TO_POST_BITRATE)
697 /* We've not posted bitrate tags yet - do so now */
698 gst_base_parse_post_bitrates (parse, TRUE, TRUE, TRUE);
701 handled = bclass->event (parse, event);
704 handled = gst_base_parse_sink_eventfunc (parse, event);
707 ret = gst_pad_event_default (pad, event);
710 gst_object_unref (parse);
711 GST_DEBUG_OBJECT (parse, "event handled");
717 * gst_base_parse_sink_eventfunc:
718 * @parse: #GstBaseParse.
719 * @event: #GstEvent to be handled.
721 * Element-level event handler function.
723 * Returns: TRUE if the event was handled and not need forwarding.
726 gst_base_parse_sink_eventfunc (GstBaseParse * parse, GstEvent * event)
728 gboolean handled = FALSE;
731 switch (GST_EVENT_TYPE (event)) {
732 case GST_EVENT_NEWSEGMENT:
734 gdouble rate, applied_rate;
736 gint64 start, stop, pos, next_ts, offset = 0;
739 gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
740 &format, &start, &stop, &pos);
742 GST_DEBUG_OBJECT (parse, "newseg rate %g, applied rate %g, "
743 "format %d, start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT
744 ", pos = %" GST_TIME_FORMAT, rate, applied_rate, format,
745 GST_TIME_ARGS (start), GST_TIME_ARGS (stop), GST_TIME_ARGS (pos));
747 if (format == GST_FORMAT_BYTES) {
748 GstClockTime seg_start, seg_stop;
749 GstBaseParseSeek *seek = NULL;
752 /* stop time is allowed to be open-ended, but not start & pos */
753 seg_stop = GST_CLOCK_TIME_NONE;
757 GST_OBJECT_LOCK (parse);
758 for (node = parse->priv->pending_seeks; node; node = node->next) {
759 GstBaseParseSeek *tmp = node->data;
761 if (tmp->offset == pos) {
766 parse->priv->pending_seeks =
767 g_slist_remove (parse->priv->pending_seeks, seek);
768 GST_OBJECT_UNLOCK (parse);
771 GST_DEBUG_OBJECT (parse,
772 "Matched newsegment to%s seek: %" GST_SEGMENT_FORMAT,
773 seek->accurate ? " accurate" : "", &seek->segment);
774 seg_start = seek->segment.start;
775 seg_stop = seek->segment.stop;
776 next_ts = seek->start_ts;
777 parse->priv->exact_position = seek->accurate;
780 /* best attempt convert */
781 /* as these are only estimates, stop is kept open-ended to avoid
782 * premature cutting */
783 gst_base_parse_convert (parse, GST_FORMAT_BYTES, start,
784 GST_FORMAT_TIME, (gint64 *) & seg_start);
785 parse->priv->exact_position = (start == 0);
789 gst_event_unref (event);
790 event = gst_event_new_new_segment_full (update, rate, applied_rate,
791 GST_FORMAT_TIME, seg_start, seg_stop, seg_start);
792 format = GST_FORMAT_TIME;
795 GST_DEBUG_OBJECT (parse, "Converted incoming segment to TIME. "
796 "start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT,
797 GST_TIME_ARGS (seg_start), GST_TIME_ARGS (seg_stop));
798 } else if (format != GST_FORMAT_TIME) {
799 /* Unknown incoming segment format. Output a default open-ended
801 gst_event_unref (event);
802 event = gst_event_new_new_segment_full (update, rate, applied_rate,
803 GST_FORMAT_TIME, 0, GST_CLOCK_TIME_NONE, 0);
804 format = GST_FORMAT_TIME;
806 stop = GST_CLOCK_TIME_NONE;
808 /* not considered BYTE seekable if it is talking to us in TIME,
809 * whatever else it might claim */
810 parse->priv->upstream_seekable = FALSE;
814 gst_segment_set_newsegment_full (&parse->segment, update, rate,
815 applied_rate, format, start, stop, start);
817 /* save the segment for later, right before we push a new buffer so that
818 * the caps are fixed and the next linked element can receive
820 eventp = &parse->pending_segment;
821 gst_event_replace (eventp, event);
822 gst_event_unref (event);
825 /* but finish the current segment */
826 GST_DEBUG_OBJECT (parse, "draining current segment");
827 if (parse->segment.rate > 0.0)
828 gst_base_parse_drain (parse);
830 gst_base_parse_process_fragment (parse, FALSE);
831 gst_adapter_clear (parse->adapter);
832 parse->priv->offset = offset;
833 parse->priv->sync_offset = offset;
834 parse->priv->next_ts = next_ts;
835 parse->priv->last_ts = GST_CLOCK_TIME_NONE;
836 parse->priv->discont = TRUE;
840 case GST_EVENT_FLUSH_START:
841 parse->priv->flushing = TRUE;
842 handled = gst_pad_push_event (parse->srcpad, event);
843 /* Wait for _chain() to exit by taking the srcpad STREAM_LOCK */
844 GST_PAD_STREAM_LOCK (parse->srcpad);
845 GST_PAD_STREAM_UNLOCK (parse->srcpad);
849 case GST_EVENT_FLUSH_STOP:
850 gst_adapter_clear (parse->adapter);
851 gst_base_parse_clear_queues (parse);
852 parse->priv->flushing = FALSE;
853 parse->priv->discont = TRUE;
854 parse->priv->last_ts = GST_CLOCK_TIME_NONE;
858 if (parse->segment.rate > 0.0)
859 gst_base_parse_drain (parse);
861 gst_base_parse_process_fragment (parse, FALSE);
863 /* If we STILL have zero frames processed, fire an error */
864 if (parse->priv->framecount == 0) {
865 GST_ELEMENT_ERROR (parse, STREAM, WRONG_TYPE,
866 ("No valid frames found before end of stream"), (NULL));
868 /* newsegment before eos */
869 if (parse->pending_segment) {
870 gst_pad_push_event (parse->srcpad, parse->pending_segment);
871 parse->pending_segment = NULL;
884 * gst_base_parse_src_event:
885 * @pad: #GstPad that received the event.
886 * @event: #GstEvent that was received.
888 * Handler for source pad events.
890 * Returns: TRUE if the event was handled.
893 gst_base_parse_src_event (GstPad * pad, GstEvent * event)
896 GstBaseParseClass *bclass;
897 gboolean handled = FALSE;
900 parse = GST_BASE_PARSE (gst_pad_get_parent (pad));
901 bclass = GST_BASE_PARSE_GET_CLASS (parse);
903 GST_DEBUG_OBJECT (parse, "event %d, %s", GST_EVENT_TYPE (event),
904 GST_EVENT_TYPE_NAME (event));
906 if (bclass->src_event)
907 handled = bclass->src_event (parse, event);
910 ret = gst_pad_event_default (pad, event);
912 gst_event_unref (event);
914 gst_object_unref (parse);
920 * gst_base_parse_src_eventfunc:
921 * @parse: #GstBaseParse.
922 * @event: #GstEvent that was received.
924 * Default srcpad event handler.
926 * Returns: TRUE if the event was handled and can be dropped.
929 gst_base_parse_src_eventfunc (GstBaseParse * parse, GstEvent * event)
931 gboolean handled = FALSE;
932 GstBaseParseClass *bclass;
934 bclass = GST_BASE_PARSE_GET_CLASS (parse);
936 switch (GST_EVENT_TYPE (event)) {
939 if (parse->priv->seekable > GST_BASE_PARSE_SEEK_NONE) {
940 handled = gst_base_parse_handle_seek (parse, event);
952 * gst_base_parse_convert_default:
953 * @parse: #GstBaseParse.
954 * @src_format: #GstFormat describing the source format.
955 * @src_value: Source value to be converted.
956 * @dest_format: #GstFormat defining the converted format.
957 * @dest_value: Pointer where the conversion result will be put.
959 * Default implementation of "convert" vmethod in #GstBaseParse class.
961 * Returns: TRUE if conversion was successful.
964 gst_base_parse_convert_default (GstBaseParse * parse,
965 GstFormat src_format,
966 gint64 src_value, GstFormat dest_format, gint64 * dest_value)
968 gboolean ret = FALSE;
969 guint64 bytes, duration;
971 if (G_UNLIKELY (src_format == dest_format)) {
972 *dest_value = src_value;
976 if (G_UNLIKELY (src_value == -1)) {
981 if (G_UNLIKELY (src_value == 0)) {
986 /* need at least some frames */
987 if (!parse->priv->framecount)
990 duration = parse->priv->acc_duration / GST_MSECOND;
991 bytes = parse->priv->bytecount;
993 if (G_UNLIKELY (!duration || !bytes))
996 if (src_format == GST_FORMAT_BYTES) {
997 if (dest_format == GST_FORMAT_TIME) {
998 /* BYTES -> TIME conversion */
999 GST_DEBUG_OBJECT (parse, "converting bytes -> time");
1000 *dest_value = gst_util_uint64_scale (src_value, duration, bytes);
1001 *dest_value *= GST_MSECOND;
1002 GST_DEBUG_OBJECT (parse, "conversion result: %" G_GINT64_FORMAT " ms",
1003 *dest_value / GST_MSECOND);
1006 } else if (src_format == GST_FORMAT_TIME) {
1007 if (dest_format == GST_FORMAT_BYTES) {
1008 GST_DEBUG_OBJECT (parse, "converting time -> bytes");
1009 *dest_value = gst_util_uint64_scale (src_value / GST_MSECOND, bytes,
1011 GST_DEBUG_OBJECT (parse,
1012 "time %" G_GINT64_FORMAT " ms in bytes = %" G_GINT64_FORMAT,
1013 src_value / GST_MSECOND, *dest_value);
1016 } else if (src_format == GST_FORMAT_DEFAULT) {
1017 /* DEFAULT == frame-based */
1018 if (dest_format == GST_FORMAT_TIME) {
1019 if (parse->priv->fps_den) {
1020 *dest_value = gst_util_uint64_scale (src_value,
1021 GST_SECOND * parse->priv->fps_den, parse->priv->fps_num);
1024 } else if (dest_format == GST_FORMAT_BYTES) {
1032 * gst_base_parse_update_duration:
1033 * @parse: #GstBaseParse.
1037 gst_base_parse_update_duration (GstBaseParse * aacparse)
1040 GstBaseParse *parse;
1042 parse = GST_BASE_PARSE (aacparse);
1044 peer = gst_pad_get_peer (parse->sinkpad);
1046 GstFormat pformat = GST_FORMAT_BYTES;
1047 gboolean qres = FALSE;
1048 gint64 ptot, dest_value;
1050 qres = gst_pad_query_duration (peer, &pformat, &ptot);
1051 gst_object_unref (GST_OBJECT (peer));
1053 if (gst_base_parse_convert (parse, pformat, ptot,
1054 GST_FORMAT_TIME, &dest_value))
1055 parse->priv->estimated_duration = dest_value;
1061 gst_base_parse_post_bitrates (GstBaseParse * parse, gboolean post_min,
1062 gboolean post_avg, gboolean post_max)
1064 GstTagList *taglist = gst_tag_list_new ();
1066 if (post_min && parse->priv->post_min_bitrate)
1067 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE,
1068 GST_TAG_MINIMUM_BITRATE, parse->priv->min_bitrate, NULL);
1070 if (post_avg && parse->priv->post_avg_bitrate) {
1071 parse->priv->posted_avg_bitrate = parse->priv->avg_bitrate;
1072 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_BITRATE,
1073 parse->priv->avg_bitrate, NULL);
1076 if (post_max && parse->priv->post_max_bitrate)
1077 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE,
1078 GST_TAG_MAXIMUM_BITRATE, parse->priv->max_bitrate, NULL);
1080 GST_DEBUG_OBJECT (parse, "Updated bitrates. Min: %u, Avg: %u, Max: %u",
1081 parse->priv->min_bitrate, parse->priv->avg_bitrate,
1082 parse->priv->max_bitrate);
1084 gst_element_found_tags_for_pad (GST_ELEMENT (parse), parse->srcpad, taglist);
1088 * gst_base_parse_update_bitrates:
1089 * @parse: #GstBaseParse.
1090 * @buffer: Current frame as a #GstBuffer
1092 * Keeps track of the minimum and maximum bitrates, and also maintains a
1093 * running average bitrate of the stream so far.
1096 gst_base_parse_update_bitrates (GstBaseParse * parse, GstBuffer * buffer)
1098 /* Only update the tag on a 10 kbps delta */
1099 static const gint update_threshold = 10000;
1101 GstBaseParseClass *klass;
1102 guint64 data_len, frame_dur;
1103 gint overhead = 0, frame_bitrate, old_avg_bitrate;
1104 gboolean update_min = FALSE, update_avg = FALSE, update_max = FALSE;
1106 klass = GST_BASE_PARSE_GET_CLASS (parse);
1108 if (klass->get_frame_overhead) {
1109 overhead = klass->get_frame_overhead (parse, buffer);
1114 data_len = GST_BUFFER_SIZE (buffer) - overhead;
1115 parse->priv->data_bytecount += data_len;
1117 /* duration should be valid by now,
1118 * either set by subclass or maybe based on fps settings */
1119 if (GST_BUFFER_DURATION_IS_VALID (buffer)) {
1120 /* Calculate duration of a frame from buffer properties */
1121 frame_dur = GST_BUFFER_DURATION (buffer);
1122 parse->priv->avg_bitrate = (8 * parse->priv->data_bytecount * GST_SECOND) /
1123 parse->priv->acc_duration;
1126 /* No way to figure out frame duration (is this even possible?) */
1130 /* override if subclass provided bitrate, e.g. metadata based */
1131 if (parse->priv->bitrate) {
1132 parse->priv->avg_bitrate = parse->priv->bitrate;
1135 frame_bitrate = (8 * data_len * GST_SECOND) / frame_dur;
1137 GST_LOG_OBJECT (parse, "frame bitrate %u, avg bitrate %u", frame_bitrate,
1138 parse->priv->avg_bitrate);
1140 if (frame_bitrate < parse->priv->min_bitrate) {
1141 parse->priv->min_bitrate = frame_bitrate;
1145 if (frame_bitrate > parse->priv->max_bitrate) {
1146 parse->priv->max_bitrate = frame_bitrate;
1150 old_avg_bitrate = parse->priv->posted_avg_bitrate;
1151 if ((gint) (old_avg_bitrate - parse->priv->avg_bitrate) > update_threshold ||
1152 (gint) (parse->priv->avg_bitrate - old_avg_bitrate) > update_threshold)
1155 /* always post all at threshold time */
1156 if (parse->priv->framecount == MIN_FRAMES_TO_POST_BITRATE)
1157 gst_base_parse_post_bitrates (parse, TRUE, TRUE, TRUE);
1159 if (parse->priv->framecount > MIN_FRAMES_TO_POST_BITRATE &&
1160 (update_min || update_avg || update_max))
1161 gst_base_parse_post_bitrates (parse, update_min, update_avg, update_max);
1163 /* If average bitrate changes that much and no valid (time) duration provided,
1164 * then post a new duration message so applications can update their cached
1166 if (update_avg && !(parse->priv->duration_fmt == GST_FORMAT_TIME &&
1167 GST_CLOCK_TIME_IS_VALID (parse->priv->duration)))
1168 gst_element_post_message (GST_ELEMENT (parse),
1169 gst_message_new_duration (GST_OBJECT (parse), GST_FORMAT_TIME, -1));
1173 * gst_base_parse_add_index_entry:
1174 * @parse: #GstBaseParse.
1175 * @offset: offset of entry
1176 * @ts: timestamp associated with offset
1177 * @key: whether entry refers to keyframe
1178 * @force: add entry disregarding sanity checks
1180 * Adds an entry to the index associating @offset to @ts. It is recommended
1181 * to only add keyframe entries. @force allows to bypass checks, such as
1182 * whether the stream is (upstream) seekable, another entry is already "close"
1183 * to the new entry, etc.
1185 * Returns: #gboolean indicating whether entry was added
1188 gst_base_parse_add_index_entry (GstBaseParse * parse, guint64 offset,
1189 GstClockTime ts, gboolean key, gboolean force)
1191 gboolean ret = FALSE;
1192 GstIndexAssociation associations[2];
1194 GST_LOG_OBJECT (parse, "Adding key=%d index entry %" GST_TIME_FORMAT
1195 " @ offset 0x%08" G_GINT64_MODIFIER "x", key, GST_TIME_ARGS (ts), offset);
1197 if (G_LIKELY (!force)) {
1199 if (!parse->priv->upstream_seekable) {
1200 GST_DEBUG_OBJECT (parse, "upstream not seekable; discarding");
1204 if (parse->priv->index_last_offset >= offset) {
1205 GST_DEBUG_OBJECT (parse, "already have entries up to offset "
1206 "0x%08" G_GINT64_MODIFIER "x", parse->priv->index_last_offset);
1210 if (GST_CLOCK_DIFF (parse->priv->index_last_ts, ts) <
1211 parse->priv->idx_interval) {
1212 GST_DEBUG_OBJECT (parse, "entry too close to last time %" GST_TIME_FORMAT,
1213 GST_TIME_ARGS (parse->priv->index_last_ts));
1218 associations[0].format = GST_FORMAT_TIME;
1219 associations[0].value = ts;
1220 associations[1].format = GST_FORMAT_BYTES;
1221 associations[1].value = offset;
1223 /* index might change on-the-fly, although that would be nutty app ... */
1224 GST_OBJECT_LOCK (parse);
1225 gst_index_add_associationv (parse->priv->index, parse->priv->index_id,
1226 (key) ? GST_ASSOCIATION_FLAG_KEY_UNIT : GST_ASSOCIATION_FLAG_NONE,
1227 2, (const GstIndexAssociation *) &associations);
1228 GST_OBJECT_UNLOCK (parse);
1230 parse->priv->index_last_offset = offset;
1231 parse->priv->index_last_ts = ts;
1239 /* check for seekable upstream, above and beyond a mere query */
1241 gst_base_parse_check_seekability (GstBaseParse * parse)
1244 gboolean seekable = FALSE;
1245 gint64 start = -1, stop = -1;
1246 guint idx_interval = 0;
1248 query = gst_query_new_seeking (GST_FORMAT_BYTES);
1249 if (!gst_pad_peer_query (parse->sinkpad, query)) {
1250 GST_DEBUG_OBJECT (parse, "seeking query failed");
1254 gst_query_parse_seeking (query, NULL, &seekable, &start, &stop);
1256 /* try harder to query upstream size if we didn't get it the first time */
1257 if (seekable && stop == -1) {
1258 GstFormat fmt = GST_FORMAT_BYTES;
1260 GST_DEBUG_OBJECT (parse, "doing duration query to fix up unset stop");
1261 gst_pad_query_peer_duration (parse->sinkpad, &fmt, &stop);
1264 /* if upstream doesn't know the size, it's likely that it's not seekable in
1265 * practice even if it technically may be seekable */
1266 if (seekable && (start != 0 || stop <= start)) {
1267 GST_DEBUG_OBJECT (parse, "seekable but unknown start/stop -> disable");
1271 /* let's not put every single frame into our index */
1273 if (stop < 10 * 1024 * 1024)
1275 else if (stop < 100 * 1024 * 1024)
1278 idx_interval = 1000;
1282 gst_query_unref (query);
1284 GST_DEBUG_OBJECT (parse, "seekable: %d (%" G_GUINT64_FORMAT " - %"
1285 G_GUINT64_FORMAT ")", seekable, start, stop);
1286 parse->priv->upstream_seekable = seekable;
1288 GST_DEBUG_OBJECT (parse, "idx_interval: %ums", idx_interval);
1289 parse->priv->idx_interval = idx_interval * GST_MSECOND;
1293 * gst_base_parse_handle_and_push_buffer:
1294 * @parse: #GstBaseParse.
1295 * @klass: #GstBaseParseClass.
1296 * @buffer: #GstBuffer.
1298 * Parses the frame from given buffer and pushes it forward. Also performs
1299 * timestamp handling and checks the segment limits.
1301 * This is called with srcpad STREAM_LOCK held.
1303 * Returns: #GstFlowReturn
1305 static GstFlowReturn
1306 gst_base_parse_handle_and_push_buffer (GstBaseParse * parse,
1307 GstBaseParseClass * klass, GstBuffer * buffer)
1311 if (parse->priv->discont) {
1312 GST_DEBUG_OBJECT (parse, "marking DISCONT");
1313 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1314 parse->priv->discont = FALSE;
1317 GST_LOG_OBJECT (parse,
1318 "parsing frame at offset %" G_GUINT64_FORMAT
1319 " (%#" G_GINT64_MODIFIER "x) of size %d",
1320 GST_BUFFER_OFFSET (buffer), GST_BUFFER_OFFSET (buffer),
1321 GST_BUFFER_SIZE (buffer));
1323 ret = klass->parse_frame (parse, buffer);
1325 /* re-use default handler to add missing metadata as-much-as-possible */
1326 gst_base_parse_parse_frame (parse, buffer);
1327 if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
1328 GST_BUFFER_DURATION_IS_VALID (buffer)) {
1329 parse->priv->next_ts =
1330 GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer);
1332 /* we lost track, do not produce bogus time next time around
1333 * (probably means parser subclass has given up on parsing as well) */
1334 GST_DEBUG_OBJECT (parse, "no next fallback timestamp");
1335 parse->priv->next_ts = GST_CLOCK_TIME_NONE;
1338 /* First buffers are dropped, this means that the subclass needs more
1339 * frames to decide on the format and queues them internally */
1340 /* convert internal flow to OK and mark discont for the next buffer. */
1341 if (ret == GST_BASE_PARSE_FLOW_DROPPED) {
1342 gst_buffer_unref (buffer);
1344 } else if (ret != GST_FLOW_OK) {
1348 return gst_base_parse_push_buffer (parse, buffer);
1352 * gst_base_parse_push_buffer:
1353 * @parse: #GstBaseParse.
1354 * @buffer: #GstBuffer.
1356 * Pushes the buffer downstream, sends any pending events and
1357 * does some timestamp and segment handling.
1359 * This must be called with srcpad STREAM_LOCK held.
1361 * Returns: #GstFlowReturn
1364 gst_base_parse_push_buffer (GstBaseParse * parse, GstBuffer * buffer)
1366 GstFlowReturn ret = GST_FLOW_OK;
1367 GstClockTime last_start = GST_CLOCK_TIME_NONE;
1368 GstClockTime last_stop = GST_CLOCK_TIME_NONE;
1369 GstBaseParseClass *klass = GST_BASE_PARSE_GET_CLASS (parse);
1371 GST_LOG_OBJECT (parse,
1372 "processing buffer of size %d with ts %" GST_TIME_FORMAT
1373 ", duration %" GST_TIME_FORMAT, GST_BUFFER_SIZE (buffer),
1374 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
1375 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
1377 /* some one-time start-up */
1378 if (G_UNLIKELY (!parse->priv->framecount)) {
1379 gst_base_parse_check_seekability (parse);
1383 parse->priv->bytecount += GST_BUFFER_SIZE (buffer);
1384 if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BASE_PARSE_BUFFER_FLAG_NO_FRAME)) {
1385 parse->priv->framecount++;
1386 if (GST_BUFFER_DURATION_IS_VALID (buffer)) {
1387 parse->priv->acc_duration += GST_BUFFER_DURATION (buffer);
1390 GST_BUFFER_FLAG_UNSET (buffer, GST_BASE_PARSE_BUFFER_FLAG_NO_FRAME);
1391 if (parse->priv->update_interval &&
1392 (parse->priv->framecount % parse->priv->update_interval) == 0)
1393 gst_base_parse_update_duration (parse);
1395 gst_base_parse_update_bitrates (parse, buffer);
1397 if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer))
1398 last_start = last_stop = GST_BUFFER_TIMESTAMP (buffer);
1399 if (last_start != GST_CLOCK_TIME_NONE
1400 && GST_BUFFER_DURATION_IS_VALID (buffer))
1401 last_stop = last_start + GST_BUFFER_DURATION (buffer);
1403 /* should have caps by now */
1404 g_return_val_if_fail (GST_PAD_CAPS (parse->srcpad), GST_FLOW_ERROR);
1406 gst_buffer_set_caps (buffer, GST_PAD_CAPS (parse->srcpad));
1408 /* segment adjustment magic; only if we are running the whole show */
1409 if (!parse->priv->passthrough && parse->segment.rate > 0.0 &&
1410 (parse->priv->pad_mode == GST_ACTIVATE_PULL ||
1411 parse->priv->upstream_seekable)) {
1412 /* segment times are typically estimates,
1413 * actual frame data might lead subclass to different timestamps,
1414 * so override segment start from what is supplied there */
1415 if (G_UNLIKELY (parse->pending_segment && !parse->priv->exact_position &&
1416 GST_CLOCK_TIME_IS_VALID (last_start))) {
1417 gst_event_unref (parse->pending_segment);
1418 parse->segment.start =
1419 MIN ((guint64) last_start, (guint64) parse->segment.stop);
1420 GST_DEBUG_OBJECT (parse,
1421 "adjusting pending segment start to %" GST_TIME_FORMAT,
1422 GST_TIME_ARGS (parse->segment.start));
1423 parse->pending_segment =
1424 gst_event_new_new_segment (FALSE, parse->segment.rate,
1425 parse->segment.format, parse->segment.start, parse->segment.stop,
1426 parse->segment.start);
1428 /* handle gaps, e.g. non-zero start-time, in as much not handled by above */
1429 if (GST_CLOCK_TIME_IS_VALID (parse->segment.last_stop) &&
1430 GST_CLOCK_TIME_IS_VALID (last_start)) {
1431 GstClockTimeDiff diff;
1433 /* only send newsegments with increasing start times,
1434 * otherwise if these go back and forth downstream (sinks) increase
1435 * accumulated time and running_time */
1436 diff = GST_CLOCK_DIFF (parse->segment.last_stop, last_start);
1437 if (G_UNLIKELY (diff > 2 * GST_SECOND && last_start > parse->segment.start
1438 && (!GST_CLOCK_TIME_IS_VALID (parse->segment.stop) ||
1439 last_start < parse->segment.stop))) {
1440 GST_DEBUG_OBJECT (parse,
1441 "Gap of %" G_GINT64_FORMAT " ns detected in stream "
1442 "(%" GST_TIME_FORMAT " -> %" GST_TIME_FORMAT "). "
1443 "Sending updated NEWSEGMENT events", diff,
1444 GST_TIME_ARGS (parse->segment.last_stop),
1445 GST_TIME_ARGS (last_start));
1446 if (G_UNLIKELY (parse->pending_segment)) {
1447 gst_event_unref (parse->pending_segment);
1448 parse->segment.start = last_start;
1449 parse->pending_segment =
1450 gst_event_new_new_segment (FALSE, parse->segment.rate,
1451 parse->segment.format, parse->segment.start, parse->segment.stop,
1452 parse->segment.start);
1454 /* send newsegment events such that the gap is not accounted in
1455 * accum time, hence running_time */
1456 /* close ahead of gap */
1457 gst_pad_push_event (parse->srcpad,
1458 gst_event_new_new_segment (TRUE, parse->segment.rate,
1459 parse->segment.format, parse->segment.last_stop,
1460 parse->segment.last_stop, parse->segment.last_stop));
1462 gst_pad_push_event (parse->srcpad,
1463 gst_event_new_new_segment (FALSE, parse->segment.rate,
1464 parse->segment.format, last_start, parse->segment.stop,
1467 /* align segment view with downstream,
1468 * prevents double-counting accum when closing segment */
1469 gst_segment_set_newsegment (&parse->segment, FALSE,
1470 parse->segment.rate, parse->segment.format, last_start,
1471 parse->segment.stop, last_start);
1472 parse->segment.last_stop = last_start;
1477 /* and should then also be linked downstream, so safe to send some events */
1478 if (parse->priv->pad_mode == GST_ACTIVATE_PULL) {
1479 if (G_UNLIKELY (parse->close_segment)) {
1480 GST_DEBUG_OBJECT (parse, "loop sending close segment");
1481 gst_pad_push_event (parse->srcpad, parse->close_segment);
1482 parse->close_segment = NULL;
1485 if (G_UNLIKELY (parse->pending_segment)) {
1486 GST_DEBUG_OBJECT (parse, "loop push pending segment");
1487 gst_pad_push_event (parse->srcpad, parse->pending_segment);
1488 parse->pending_segment = NULL;
1491 if (G_UNLIKELY (parse->pending_segment)) {
1492 GST_DEBUG_OBJECT (parse, "chain pushing a pending segment");
1493 gst_pad_push_event (parse->srcpad, parse->pending_segment);
1494 parse->pending_segment = NULL;
1498 if (G_UNLIKELY (parse->priv->pending_events)) {
1501 for (l = parse->priv->pending_events; l != NULL; l = l->next) {
1502 gst_pad_push_event (parse->srcpad, GST_EVENT (l->data));
1504 g_list_free (parse->priv->pending_events);
1505 parse->priv->pending_events = NULL;
1508 if (parse->priv->upstream_seekable && parse->priv->exact_position &&
1509 GST_BUFFER_TIMESTAMP_IS_VALID (buffer))
1510 gst_base_parse_add_index_entry (parse, GST_BUFFER_OFFSET (buffer),
1511 GST_BUFFER_TIMESTAMP (buffer),
1512 !GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT), FALSE);
1514 if (klass->pre_push_buffer)
1515 ret = klass->pre_push_buffer (parse, buffer);
1517 ret = GST_BASE_PARSE_FLOW_CLIP;
1519 if (ret == GST_BASE_PARSE_FLOW_CLIP) {
1520 if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
1521 GST_CLOCK_TIME_IS_VALID (parse->segment.stop) &&
1522 GST_BUFFER_TIMESTAMP (buffer) >
1523 parse->segment.stop + parse->priv->lead_out_ts) {
1524 GST_LOG_OBJECT (parse, "Dropped frame, after segment");
1525 ret = GST_FLOW_UNEXPECTED;
1526 } else if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
1527 GST_BUFFER_DURATION_IS_VALID (buffer) &&
1528 GST_CLOCK_TIME_IS_VALID (parse->segment.start) &&
1529 GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer) +
1530 parse->priv->lead_in_ts < parse->segment.start) {
1531 GST_LOG_OBJECT (parse, "Dropped frame, before segment");
1532 ret = GST_BASE_PARSE_FLOW_DROPPED;
1538 if (ret == GST_BASE_PARSE_FLOW_DROPPED) {
1539 GST_LOG_OBJECT (parse, "frame (%d bytes) dropped",
1540 GST_BUFFER_SIZE (buffer));
1541 gst_buffer_unref (buffer);
1543 } else if (ret == GST_FLOW_OK) {
1544 if (parse->segment.rate > 0.0) {
1545 GST_LOG_OBJECT (parse, "frame (%d bytes) pushed: %s",
1546 GST_BUFFER_SIZE (buffer), gst_flow_get_name (ret));
1547 ret = gst_pad_push (parse->srcpad, buffer);
1549 GST_LOG_OBJECT (parse, "frame (%d bytes) queued for now",
1550 GST_BUFFER_SIZE (buffer));
1551 parse->priv->buffers_queued =
1552 g_slist_prepend (parse->priv->buffers_queued, buffer);
1556 gst_buffer_unref (buffer);
1557 GST_LOG_OBJECT (parse, "frame (%d bytes) not pushed: %s",
1558 GST_BUFFER_SIZE (buffer), gst_flow_get_name (ret));
1559 /* if we are not sufficiently in control, let upstream decide on EOS */
1560 if (ret == GST_FLOW_UNEXPECTED &&
1561 (parse->priv->passthrough ||
1562 (parse->priv->pad_mode == GST_ACTIVATE_PUSH &&
1563 !parse->priv->upstream_seekable)))
1567 /* Update current running segment position */
1568 if (ret == GST_FLOW_OK && last_stop != GST_CLOCK_TIME_NONE &&
1569 parse->segment.last_stop < last_stop)
1570 gst_segment_set_last_stop (&parse->segment, GST_FORMAT_TIME, last_stop);
1577 * gst_base_parse_drain:
1578 * @parse: #GstBaseParse.
1580 * Drains the adapter until it is empty. It decreases the min_frame_size to
1581 * match the current adapter size and calls chain method until the adapter
1582 * is emptied or chain returns with error.
1585 gst_base_parse_drain (GstBaseParse * parse)
1589 GST_DEBUG_OBJECT (parse, "draining");
1590 parse->priv->drain = TRUE;
1593 avail = gst_adapter_available (parse->adapter);
1597 if (gst_base_parse_chain (parse->sinkpad, NULL) != GST_FLOW_OK) {
1601 /* nothing changed, maybe due to truncated frame; break infinite loop */
1602 if (avail == gst_adapter_available (parse->adapter)) {
1603 GST_DEBUG_OBJECT (parse, "no change during draining; flushing");
1604 gst_adapter_clear (parse->adapter);
1608 parse->priv->drain = FALSE;
1612 * gst_base_parse_process_fragment:
1613 * @parse: #GstBaseParse.
1615 * Processes a reverse playback (forward) fragment:
1616 * - append head of last fragment that was skipped to current fragment data
1617 * - drain the resulting current fragment data (i.e. repeated chain)
1618 * - add time/duration (if needed) to frames queued by chain
1619 * - push queued data
1621 static GstFlowReturn
1622 gst_base_parse_process_fragment (GstBaseParse * parse, gboolean push_only)
1625 GstFlowReturn ret = GST_FLOW_OK;
1626 GSList *send = NULL;
1632 parse->priv->buffers_pending = g_slist_reverse (parse->priv->buffers_pending);
1633 while (parse->priv->buffers_pending) {
1634 buf = GST_BUFFER_CAST (parse->priv->buffers_pending->data);
1635 GST_LOG_OBJECT (parse, "adding pending buffer (size %d)",
1636 GST_BUFFER_SIZE (buf));
1637 gst_adapter_push (parse->adapter, buf);
1638 parse->priv->buffers_pending =
1639 g_slist_delete_link (parse->priv->buffers_pending,
1640 parse->priv->buffers_pending);
1643 /* invalidate so no fall-back timestamping is performed;
1644 * ok if taken from subclass or upstream */
1645 parse->priv->next_ts = GST_CLOCK_TIME_NONE;
1646 /* prevent it hanging around stop all the time */
1647 parse->segment.last_stop = GST_CLOCK_TIME_NONE;
1649 parse->priv->discont = TRUE;
1651 /* chain looks for frames and queues resulting ones (in stead of pushing) */
1652 /* initial skipped data is added to buffers_pending */
1653 gst_base_parse_drain (parse);
1656 /* add metadata (if needed to queued buffers */
1657 GST_LOG_OBJECT (parse, "last timestamp: %" GST_TIME_FORMAT,
1658 GST_TIME_ARGS (parse->priv->last_ts));
1659 while (parse->priv->buffers_queued) {
1660 buf = GST_BUFFER_CAST (parse->priv->buffers_queued->data);
1662 /* no touching if upstream or parsing provided time */
1663 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
1664 GST_LOG_OBJECT (parse, "buffer has time %" GST_TIME_FORMAT,
1665 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
1666 } else if (GST_CLOCK_TIME_IS_VALID (parse->priv->last_ts) &&
1667 GST_BUFFER_DURATION_IS_VALID (buf)) {
1668 if (G_LIKELY (GST_BUFFER_DURATION (buf) <= parse->priv->last_ts))
1669 parse->priv->last_ts -= GST_BUFFER_DURATION (buf);
1671 parse->priv->last_ts = 0;
1672 GST_BUFFER_TIMESTAMP (buf) = parse->priv->last_ts;
1673 GST_LOG_OBJECT (parse, "applied time %" GST_TIME_FORMAT,
1674 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
1676 /* no idea, very bad */
1677 GST_WARNING_OBJECT (parse, "could not determine time for buffer");
1680 /* reverse order for ascending sending */
1681 send = g_slist_prepend (send, buf);
1682 parse->priv->buffers_queued =
1683 g_slist_delete_link (parse->priv->buffers_queued,
1684 parse->priv->buffers_queued);
1689 buf = GST_BUFFER_CAST (send->data);
1690 GST_LOG_OBJECT (parse, "pushing buffer %p, timestamp %"
1691 GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT
1692 ", offset %" G_GINT64_FORMAT, buf,
1693 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
1694 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)), GST_BUFFER_OFFSET (buf));
1696 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (parse->priv->last_ts)))
1697 parse->priv->last_ts = GST_BUFFER_TIMESTAMP (buf);
1699 /* iterate output queue an push downstream */
1700 ret = gst_pad_push (parse->srcpad, buf);
1701 send = g_slist_delete_link (send, send);
1703 /* clear any leftover if error */
1704 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
1706 buf = GST_BUFFER_CAST (send->data);
1707 gst_buffer_unref (buf);
1708 send = g_slist_delete_link (send, send);
1713 /* any trailing unused no longer usable (ideally none) */
1714 if (G_UNLIKELY (gst_adapter_available (parse->adapter))) {
1715 GST_DEBUG_OBJECT (parse, "discarding %d trailing bytes",
1716 gst_adapter_available (parse->adapter));
1717 gst_adapter_clear (parse->adapter);
1723 /* small helper that checks whether we have been trying to resync too long */
1724 static inline GstFlowReturn
1725 gst_base_parse_check_sync (GstBaseParse * parse)
1727 if (G_UNLIKELY (parse->priv->discont &&
1728 parse->priv->offset - parse->priv->sync_offset > 2 * 1024 * 1024)) {
1729 GST_ELEMENT_ERROR (parse, STREAM, DECODE,
1730 ("Failed to parse stream"), (NULL));
1731 return GST_FLOW_ERROR;
1739 * gst_base_parse_chain:
1741 * @buffer: #GstBuffer.
1743 * Returns: #GstFlowReturn.
1745 static GstFlowReturn
1746 gst_base_parse_chain (GstPad * pad, GstBuffer * buffer)
1748 GstBaseParseClass *bclass;
1749 GstBaseParse *parse;
1750 GstFlowReturn ret = GST_FLOW_OK;
1751 GstBuffer *outbuf = NULL;
1752 GstBuffer *tmpbuf = NULL;
1757 GstClockTime timestamp;
1759 parse = GST_BASE_PARSE (GST_OBJECT_PARENT (pad));
1760 bclass = GST_BASE_PARSE_GET_CLASS (parse);
1762 if (G_LIKELY (buffer)) {
1763 GST_LOG_OBJECT (parse, "buffer size: %d, offset = %" G_GINT64_FORMAT,
1764 GST_BUFFER_SIZE (buffer), GST_BUFFER_OFFSET (buffer));
1765 if (G_UNLIKELY (parse->priv->passthrough)) {
1766 buffer = gst_buffer_make_metadata_writable (buffer);
1767 return gst_base_parse_push_buffer (parse, buffer);
1769 /* upstream feeding us in reverse playback;
1770 * gather each fragment, then process it in single run */
1771 if (parse->segment.rate < 0.0) {
1772 if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT))) {
1773 GST_DEBUG_OBJECT (parse, "buffer starts new reverse playback fragment");
1774 ret = gst_base_parse_process_fragment (parse, FALSE);
1776 gst_adapter_push (parse->adapter, buffer);
1779 gst_adapter_push (parse->adapter, buffer);
1782 /* Parse and push as many frames as possible */
1783 /* Stop either when adapter is empty or we are flushing */
1784 while (!parse->priv->flushing) {
1785 tmpbuf = gst_buffer_new ();
1787 /* Synchronization loop */
1789 GST_BASE_PARSE_LOCK (parse);
1790 min_size = parse->priv->min_frame_size;
1791 GST_BASE_PARSE_UNLOCK (parse);
1793 if (G_UNLIKELY (parse->priv->drain)) {
1794 min_size = gst_adapter_available (parse->adapter);
1795 GST_DEBUG_OBJECT (parse, "draining, data left: %d", min_size);
1796 if (G_UNLIKELY (!min_size)) {
1797 gst_buffer_unref (tmpbuf);
1802 /* Collect at least min_frame_size bytes */
1803 if (gst_adapter_available (parse->adapter) < min_size) {
1804 GST_DEBUG_OBJECT (parse, "not enough data available (only %d bytes)",
1805 gst_adapter_available (parse->adapter));
1806 gst_buffer_unref (tmpbuf);
1810 data = gst_adapter_peek (parse->adapter, min_size);
1811 GST_BUFFER_DATA (tmpbuf) = (guint8 *) data;
1812 GST_BUFFER_SIZE (tmpbuf) = min_size;
1813 GST_BUFFER_OFFSET (tmpbuf) = parse->priv->offset;
1814 GST_BUFFER_FLAG_SET (tmpbuf, GST_MINI_OBJECT_FLAG_READONLY);
1816 if (parse->priv->discont) {
1817 GST_DEBUG_OBJECT (parse, "marking DISCONT");
1818 GST_BUFFER_FLAG_SET (tmpbuf, GST_BUFFER_FLAG_DISCONT);
1822 if (bclass->check_valid_frame (parse, tmpbuf, &fsize, &skip)) {
1823 if (gst_adapter_available (parse->adapter) < fsize) {
1824 GST_DEBUG_OBJECT (parse,
1825 "found valid frame but not enough data available (only %d bytes)",
1826 gst_adapter_available (parse->adapter));
1827 gst_buffer_unref (tmpbuf);
1833 /* subclass didn't touch this value. By default we skip 1 byte */
1837 GST_LOG_OBJECT (parse, "finding sync, skipping %d bytes", skip);
1838 if (parse->segment.rate < 0.0 && !parse->priv->buffers_queued) {
1839 /* reverse playback, and no frames found yet, so we are skipping
1840 * the leading part of a fragment, which may form the tail of
1841 * fragment coming later, hopefully subclass skips efficiently ... */
1842 timestamp = gst_adapter_prev_timestamp (parse->adapter, NULL);
1843 outbuf = gst_adapter_take_buffer (parse->adapter, skip);
1844 outbuf = gst_buffer_make_metadata_writable (outbuf);
1845 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
1846 parse->priv->buffers_pending =
1847 g_slist_prepend (parse->priv->buffers_pending, outbuf);
1850 gst_adapter_flush (parse->adapter, skip);
1852 parse->priv->offset += skip;
1853 if (!parse->priv->discont)
1854 parse->priv->sync_offset = parse->priv->offset;
1855 parse->priv->discont = TRUE;
1857 /* There is a possibility that subclass set the skip value to zero.
1858 This means that it has probably found a frame but wants to ask
1859 more data (by increasing the min_size) to be sure of this. */
1860 if ((ret = gst_base_parse_check_sync (parse)) != GST_FLOW_OK) {
1861 gst_buffer_unref (tmpbuf);
1865 gst_buffer_unref (tmpbuf);
1869 /* Subclass found the sync, but still wants to skip some data */
1870 GST_LOG_OBJECT (parse, "skipping %d bytes", skip);
1871 gst_adapter_flush (parse->adapter, skip);
1872 parse->priv->offset += skip;
1875 /* Grab lock to prevent a race with FLUSH_START handler */
1876 GST_PAD_STREAM_LOCK (parse->srcpad);
1878 /* FLUSH_START event causes the "flushing" flag to be set. In this
1879 * case we can leave the frame pushing loop */
1880 if (parse->priv->flushing) {
1881 GST_PAD_STREAM_UNLOCK (parse->srcpad);
1885 /* FIXME: Would it be more efficient to make a subbuffer instead? */
1886 outbuf = gst_adapter_take_buffer (parse->adapter, fsize);
1887 outbuf = gst_buffer_make_metadata_writable (outbuf);
1889 /* Subclass may want to know the data offset */
1890 GST_BUFFER_OFFSET (outbuf) = parse->priv->offset;
1891 parse->priv->offset += fsize;
1893 /* move along with upstream timestamp (if any),
1894 * but interpolate in between */
1895 timestamp = gst_adapter_prev_timestamp (parse->adapter, NULL);
1896 if (GST_CLOCK_TIME_IS_VALID (timestamp) &&
1897 (parse->priv->prev_ts != timestamp)) {
1898 parse->priv->prev_ts = parse->priv->next_ts = timestamp;
1901 ret = gst_base_parse_handle_and_push_buffer (parse, bclass, outbuf);
1902 GST_PAD_STREAM_UNLOCK (parse->srcpad);
1904 if (ret != GST_FLOW_OK) {
1905 GST_LOG_OBJECT (parse, "push returned %d", ret);
1911 GST_LOG_OBJECT (parse, "chain leaving");
1915 /* pull @size bytes at current offset,
1916 * i.e. at least try to and possibly return a shorter buffer if near the end */
1917 static GstFlowReturn
1918 gst_base_parse_pull_range (GstBaseParse * parse, guint size,
1919 GstBuffer ** buffer)
1921 GstFlowReturn ret = GST_FLOW_OK;
1923 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
1925 /* Caching here actually makes much less difference than one would expect.
1926 * We do it mainly to avoid pulling buffers of 1 byte all the time */
1927 if (parse->priv->cache) {
1928 gint64 cache_offset = GST_BUFFER_OFFSET (parse->priv->cache);
1929 gint cache_size = GST_BUFFER_SIZE (parse->priv->cache);
1931 if (cache_offset <= parse->priv->offset &&
1932 (parse->priv->offset + size) <= (cache_offset + cache_size)) {
1933 *buffer = gst_buffer_create_sub (parse->priv->cache,
1934 parse->priv->offset - cache_offset, size);
1935 GST_BUFFER_OFFSET (*buffer) = parse->priv->offset;
1938 /* not enough data in the cache, free cache and get a new one */
1939 gst_buffer_unref (parse->priv->cache);
1940 parse->priv->cache = NULL;
1943 /* refill the cache */
1945 gst_pad_pull_range (parse->sinkpad, parse->priv->offset, MAX (size,
1946 64 * 1024), &parse->priv->cache);
1947 if (ret != GST_FLOW_OK) {
1948 parse->priv->cache = NULL;
1952 if (GST_BUFFER_SIZE (parse->priv->cache) >= size) {
1953 *buffer = gst_buffer_create_sub (parse->priv->cache, 0, size);
1954 GST_BUFFER_OFFSET (*buffer) = parse->priv->offset;
1958 /* Not possible to get enough data, try a last time with
1959 * requesting exactly the size we need */
1960 gst_buffer_unref (parse->priv->cache);
1961 parse->priv->cache = NULL;
1963 ret = gst_pad_pull_range (parse->sinkpad, parse->priv->offset, size,
1964 &parse->priv->cache);
1966 if (ret != GST_FLOW_OK) {
1967 GST_DEBUG_OBJECT (parse, "pull_range returned %d", ret);
1972 if (GST_BUFFER_SIZE (parse->priv->cache) < size) {
1973 GST_DEBUG_OBJECT (parse, "Returning short buffer at offset %"
1974 G_GUINT64_FORMAT ": wanted %u bytes, got %u bytes", parse->priv->offset,
1975 size, GST_BUFFER_SIZE (parse->priv->cache));
1977 *buffer = parse->priv->cache;
1978 parse->priv->cache = NULL;
1983 *buffer = gst_buffer_create_sub (parse->priv->cache, 0, size);
1984 GST_BUFFER_OFFSET (*buffer) = parse->priv->offset;
1989 static GstFlowReturn
1990 gst_base_parse_handle_previous_fragment (GstBaseParse * parse)
1993 GstClockTime ts = 0;
1997 GST_DEBUG_OBJECT (parse, "fragment ended; last_ts = %" GST_TIME_FORMAT
1998 ", last_offset = %" G_GINT64_FORMAT, GST_TIME_ARGS (parse->priv->last_ts),
1999 parse->priv->last_offset);
2001 if (!parse->priv->last_offset || parse->priv->last_ts <= parse->segment.start) {
2002 GST_DEBUG_OBJECT (parse, "past start of segment %" GST_TIME_FORMAT,
2003 GST_TIME_ARGS (parse->segment.start));
2004 ret = GST_FLOW_UNEXPECTED;
2008 /* last fragment started at last_offset / last_ts;
2009 * seek back 10s capped at 1MB */
2010 if (parse->priv->last_ts >= 10 * GST_SECOND)
2011 ts = parse->priv->last_ts - 10 * GST_SECOND;
2012 /* if we are exact now, we will be more so going backwards */
2013 if (parse->priv->exact_position) {
2014 offset = gst_base_parse_find_offset (parse, ts, TRUE, NULL);
2016 GstFormat dstformat = GST_FORMAT_BYTES;
2018 if (!gst_pad_query_convert (parse->srcpad, GST_FORMAT_TIME, ts,
2019 &dstformat, &offset)) {
2020 GST_DEBUG_OBJECT (parse, "conversion failed, only BYTE based");
2023 offset = CLAMP (offset, parse->priv->last_offset - 1024 * 1024,
2024 parse->priv->last_offset - 1024);
2025 offset = MAX (0, offset);
2027 GST_DEBUG_OBJECT (parse, "next fragment from offset %" G_GINT64_FORMAT,
2029 parse->priv->offset = offset;
2031 ret = gst_base_parse_pull_range (parse, parse->priv->last_offset - offset,
2033 if (ret != GST_FLOW_OK)
2036 gst_adapter_push (parse->adapter, buffer);
2037 ret = gst_base_parse_process_fragment (parse, FALSE);
2038 if (ret != GST_FLOW_OK)
2046 * gst_base_parse_loop:
2049 * Loop that is used in pull mode to retrieve data from upstream.
2052 gst_base_parse_loop (GstPad * pad)
2054 GstBaseParse *parse;
2055 GstBaseParseClass *klass;
2056 GstBuffer *buffer, *outbuf;
2057 GstFlowReturn ret = FALSE;
2058 guint fsize = 0, min_size;
2061 parse = GST_BASE_PARSE (gst_pad_get_parent (pad));
2062 klass = GST_BASE_PARSE_GET_CLASS (parse);
2064 /* reverse playback:
2065 * first fragment (closest to stop time) is handled normally below,
2066 * then we pull in fragments going backwards */
2067 if (parse->segment.rate < 0.0) {
2068 if (GST_CLOCK_TIME_IS_VALID (parse->priv->last_ts)) {
2069 ret = gst_base_parse_handle_previous_fragment (parse);
2076 GST_BASE_PARSE_LOCK (parse);
2077 min_size = parse->priv->min_frame_size;
2078 GST_BASE_PARSE_UNLOCK (parse);
2080 ret = gst_base_parse_pull_range (parse, min_size, &buffer);
2081 if (ret != GST_FLOW_OK)
2084 if (parse->priv->discont) {
2085 GST_DEBUG_OBJECT (parse, "marking DISCONT");
2086 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
2089 /* if we got a short read, inform subclass we are draining leftover
2090 * and no more is to be expected */
2091 if (GST_BUFFER_SIZE (buffer) < min_size)
2092 parse->priv->drain = TRUE;
2095 if (klass->check_valid_frame (parse, buffer, &fsize, &skip)) {
2096 parse->priv->drain = FALSE;
2099 parse->priv->drain = FALSE;
2103 GST_LOG_OBJECT (parse, "finding sync, skipping %d bytes", skip);
2104 if (parse->segment.rate < 0.0 && !parse->priv->buffers_queued) {
2105 /* reverse playback, and no frames found yet, so we are skipping
2106 * the leading part of a fragment, which may form the tail of
2107 * fragment coming later, hopefully subclass skips efficiently ... */
2108 outbuf = gst_buffer_create_sub (buffer, 0, skip);
2109 parse->priv->buffers_pending =
2110 g_slist_prepend (parse->priv->buffers_pending, outbuf);
2113 parse->priv->offset += skip;
2114 if (!parse->priv->discont)
2115 parse->priv->sync_offset = parse->priv->offset;
2116 parse->priv->discont = TRUE;
2118 /* skip == 0 should imply subclass set min_size to need more data ... */
2119 GST_DEBUG_OBJECT (parse, "finding sync...");
2120 gst_buffer_unref (buffer);
2121 if ((ret = gst_base_parse_check_sync (parse)) != GST_FLOW_OK) {
2126 if (fsize <= GST_BUFFER_SIZE (buffer)) {
2127 outbuf = gst_buffer_create_sub (buffer, 0, fsize);
2128 GST_BUFFER_OFFSET (outbuf) = GST_BUFFER_OFFSET (buffer);
2129 gst_buffer_unref (buffer);
2131 gst_buffer_unref (buffer);
2132 ret = gst_base_parse_pull_range (parse, fsize, &outbuf);
2133 if (ret != GST_FLOW_OK)
2135 if (GST_BUFFER_SIZE (outbuf) < fsize)
2139 parse->priv->offset += fsize;
2141 /* Does the subclass want to skip too? */
2143 parse->priv->offset += skip;
2145 /* This always unrefs the outbuf, even if error occurs */
2146 ret = gst_base_parse_handle_and_push_buffer (parse, klass, outbuf);
2148 /* eat expected eos signalling past segment in reverse playback */
2149 if (parse->segment.rate < 0.0 && ret == GST_FLOW_UNEXPECTED &&
2150 parse->segment.last_stop >= parse->segment.stop) {
2151 GST_DEBUG_OBJECT (parse, "downstream has reached end of segment");
2152 /* push what was accumulated during loop run */
2153 gst_base_parse_process_fragment (parse, TRUE);
2158 if (ret == GST_FLOW_UNEXPECTED)
2160 else if (ret != GST_FLOW_OK)
2163 gst_object_unref (parse);
2169 ret = GST_FLOW_UNEXPECTED;
2170 GST_DEBUG_OBJECT (parse, "eos");
2175 gboolean push_eos = FALSE;
2177 GST_DEBUG_OBJECT (parse, "pausing task, reason %s",
2178 gst_flow_get_name (ret));
2179 gst_pad_pause_task (parse->sinkpad);
2181 if (ret == GST_FLOW_UNEXPECTED) {
2182 /* handle end-of-stream/segment */
2183 if (parse->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2186 if ((stop = parse->segment.stop) == -1)
2187 stop = parse->segment.duration;
2189 GST_DEBUG_OBJECT (parse, "sending segment_done");
2191 gst_element_post_message
2192 (GST_ELEMENT_CAST (parse),
2193 gst_message_new_segment_done (GST_OBJECT_CAST (parse),
2194 GST_FORMAT_TIME, stop));
2196 /* If we STILL have zero frames processed, fire an error */
2197 if (parse->priv->framecount == 0) {
2198 GST_ELEMENT_ERROR (parse, STREAM, WRONG_TYPE,
2199 ("No valid frames found before end of stream"), (NULL));
2203 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_UNEXPECTED) {
2204 /* for fatal errors we post an error message, wrong-state is
2205 * not fatal because it happens due to flushes and only means
2206 * that we should stop now. */
2207 GST_ELEMENT_ERROR (parse, STREAM, FAILED, (NULL),
2208 ("streaming stopped, reason %s", gst_flow_get_name (ret)));
2212 /* newsegment before eos */
2213 if (parse->pending_segment) {
2214 gst_pad_push_event (parse->srcpad, parse->pending_segment);
2215 parse->pending_segment = NULL;
2217 gst_pad_push_event (parse->srcpad, gst_event_new_eos ());
2219 gst_object_unref (parse);
2225 * gst_base_parse_sink_activate:
2226 * @sinkpad: #GstPad to be activated.
2228 * Returns: TRUE if activation succeeded.
2231 gst_base_parse_sink_activate (GstPad * sinkpad)
2233 GstBaseParse *parse;
2234 gboolean result = TRUE;
2236 parse = GST_BASE_PARSE (gst_pad_get_parent (sinkpad));
2238 GST_DEBUG_OBJECT (parse, "sink activate");
2240 if (gst_pad_check_pull_range (sinkpad)) {
2241 GST_DEBUG_OBJECT (parse, "trying to activate in pull mode");
2242 result = gst_pad_activate_pull (sinkpad, TRUE);
2244 GST_DEBUG_OBJECT (parse, "trying to activate in push mode");
2245 result = gst_pad_activate_push (sinkpad, TRUE);
2248 GST_DEBUG_OBJECT (parse, "sink activate return %d", result);
2249 gst_object_unref (parse);
2255 * gst_base_parse_activate:
2256 * @parse: #GstBaseParse.
2257 * @active: TRUE if element will be activated, FALSE if disactivated.
2259 * Returns: TRUE if the operation succeeded.
2262 gst_base_parse_activate (GstBaseParse * parse, gboolean active)
2264 GstBaseParseClass *klass;
2265 gboolean result = FALSE;
2267 GST_DEBUG_OBJECT (parse, "activate");
2269 klass = GST_BASE_PARSE_GET_CLASS (parse);
2272 if (parse->priv->pad_mode == GST_ACTIVATE_NONE && klass->start)
2273 result = klass->start (parse);
2275 /* We must make sure streaming has finished before resetting things
2276 * and calling the ::stop vfunc */
2277 GST_PAD_STREAM_LOCK (parse->sinkpad);
2278 GST_PAD_STREAM_UNLOCK (parse->sinkpad);
2280 if (parse->priv->pad_mode != GST_ACTIVATE_NONE && klass->stop)
2281 result = klass->stop (parse);
2283 parse->priv->pad_mode = GST_ACTIVATE_NONE;
2285 GST_DEBUG_OBJECT (parse, "activate: %d", result);
2291 * gst_base_parse_sink_activate_push:
2292 * @pad: #GstPad to be (de)activated.
2293 * @active: TRUE when activating, FALSE when deactivating.
2295 * Returns: TRUE if (de)activation succeeded.
2298 gst_base_parse_sink_activate_push (GstPad * pad, gboolean active)
2300 gboolean result = TRUE;
2301 GstBaseParse *parse;
2303 parse = GST_BASE_PARSE (gst_pad_get_parent (pad));
2305 GST_DEBUG_OBJECT (parse, "sink activate push");
2307 result = gst_base_parse_activate (parse, active);
2310 parse->priv->pad_mode = active ? GST_ACTIVATE_PUSH : GST_ACTIVATE_NONE;
2312 GST_DEBUG_OBJECT (parse, "sink activate push: %d", result);
2314 gst_object_unref (parse);
2320 * gst_base_parse_sink_activate_pull:
2321 * @sinkpad: #GstPad to be (de)activated.
2322 * @active: TRUE when activating, FALSE when deactivating.
2324 * Returns: TRUE if (de)activation succeeded.
2327 gst_base_parse_sink_activate_pull (GstPad * sinkpad, gboolean active)
2329 gboolean result = FALSE;
2330 GstBaseParse *parse;
2332 parse = GST_BASE_PARSE (gst_pad_get_parent (sinkpad));
2334 GST_DEBUG_OBJECT (parse, "activate pull");
2336 result = gst_base_parse_activate (parse, active);
2340 parse->pending_segment = gst_event_new_new_segment (FALSE,
2341 parse->segment.rate, parse->segment.format,
2342 parse->segment.start, parse->segment.stop, parse->segment.last_stop);
2343 result &= gst_pad_start_task (sinkpad,
2344 (GstTaskFunction) gst_base_parse_loop, sinkpad);
2346 result &= gst_pad_stop_task (sinkpad);
2351 parse->priv->pad_mode = active ? GST_ACTIVATE_PULL : GST_ACTIVATE_NONE;
2353 GST_DEBUG_OBJECT (parse, "sink activate pull: %d", result);
2355 gst_object_unref (parse);
2361 * gst_base_parse_set_duration:
2362 * @parse: #GstBaseParse.
2364 * @duration: duration value.
2366 * Sets the duration of the currently playing media. Subclass can use this
2367 * when it is able to determine duration and/or notices a change in the media
2368 * duration. Alternatively, if @interval is non-zero (default), then stream
2369 * duration is determined based on estimated bitrate, and updated every @interval
2372 gst_base_parse_set_duration (GstBaseParse * parse,
2373 GstFormat fmt, gint64 duration, gint interval)
2375 g_return_if_fail (parse != NULL);
2377 GST_BASE_PARSE_LOCK (parse);
2378 if (duration != parse->priv->duration) {
2381 m = gst_message_new_duration (GST_OBJECT (parse), fmt, duration);
2382 gst_element_post_message (GST_ELEMENT (parse), m);
2384 /* TODO: what about duration tag? */
2386 parse->priv->duration = duration;
2387 parse->priv->duration_fmt = fmt;
2388 GST_DEBUG_OBJECT (parse, "set duration: %" G_GINT64_FORMAT, duration);
2389 if (fmt == GST_FORMAT_TIME && GST_CLOCK_TIME_IS_VALID (duration)) {
2390 if (interval != 0) {
2391 GST_DEBUG_OBJECT (parse, "valid duration provided, disabling estimate");
2395 GST_DEBUG_OBJECT (parse, "set update interval: %d", interval);
2396 parse->priv->update_interval = interval;
2397 GST_BASE_PARSE_UNLOCK (parse);
2401 * gst_base_parse_set_seek:
2402 * @parse: #GstBaseParse.
2403 * @seek: #GstBaseParseSeekable.
2404 * @abitrate: average bitrate.
2406 * Sets whether and how the media is seekable (in time).
2407 * Also optionally provides average bitrate detected in media (if non-zero),
2408 * e.g. based on metadata, as it will be posted to the application.
2410 * By default, announced average bitrate is estimated, and seekability is assumed
2411 * possible based on estimated bitrate.
2414 gst_base_parse_set_seek (GstBaseParse * parse,
2415 GstBaseParseSeekable seek, guint bitrate)
2417 parse->priv->seekable = seek;
2418 parse->priv->bitrate = bitrate;
2423 * gst_base_parse_set_min_frame_size:
2424 * @parse: #GstBaseParse.
2425 * @min_size: Minimum size of the data that this base class should give to
2428 * Subclass can use this function to tell the base class that it needs to
2429 * give at least #min_size buffers.
2432 gst_base_parse_set_min_frame_size (GstBaseParse * parse, guint min_size)
2434 g_return_if_fail (parse != NULL);
2436 GST_BASE_PARSE_LOCK (parse);
2437 parse->priv->min_frame_size = min_size;
2438 GST_LOG_OBJECT (parse, "set frame_min_size: %d", min_size);
2439 GST_BASE_PARSE_UNLOCK (parse);
2443 * gst_base_transform_set_passthrough:
2444 * @trans: the #GstBaseParse to set
2445 * @passthrough: boolean indicating passthrough mode.
2447 * Set passthrough mode for this parser. If operating in passthrough,
2448 * incoming buffers are pushed through unmodified.
2451 gst_base_parse_set_passthrough (GstBaseParse * parse, gboolean passthrough)
2453 g_return_if_fail (parse != NULL);
2455 GST_BASE_PARSE_LOCK (parse);
2456 parse->priv->passthrough = passthrough;
2457 GST_LOG_OBJECT (parse, "set passthrough: %d", passthrough);
2458 GST_BASE_PARSE_UNLOCK (parse);
2462 * gst_base_transform_set_frame_props:
2463 * @parse: the #GstBaseParse to set
2464 * @fps_num: frames per second (numerator).
2465 * @fps_den: frames per second (denominator).
2466 * @lead_in: frames needed before a segment for subsequent decode
2467 * @lead_out: frames needed after a segment
2469 * If frames per second is configured, parser can take care of buffer duration
2470 * and timestamping. When performing segment clipping, or seeking to a specific
2471 * location, a corresponding decoder might need an initial @lead_in and a
2472 * following @lead_out number of frames to ensure the desired segment is
2473 * entirely filled upon decoding.
2476 gst_base_parse_set_frame_props (GstBaseParse * parse, guint fps_num,
2477 guint fps_den, guint lead_in, guint lead_out)
2479 g_return_if_fail (parse != NULL);
2481 GST_BASE_PARSE_LOCK (parse);
2482 parse->priv->fps_num = fps_num;
2483 parse->priv->fps_den = fps_den;
2484 if (!fps_num || !fps_den) {
2485 GST_DEBUG_OBJECT (parse, "invalid fps (%d/%d), ignoring parameters",
2487 fps_num = fps_den = 0;
2488 parse->priv->frame_duration = GST_CLOCK_TIME_NONE;
2489 parse->priv->lead_in = parse->priv->lead_out = 0;
2490 parse->priv->lead_in_ts = parse->priv->lead_out_ts = 0;
2492 parse->priv->frame_duration =
2493 gst_util_uint64_scale (GST_SECOND, fps_den, fps_num);
2494 parse->priv->lead_in = lead_in;
2495 parse->priv->lead_out = lead_out;
2496 parse->priv->lead_in_ts =
2497 gst_util_uint64_scale (GST_SECOND, fps_den * lead_in, fps_num);
2498 parse->priv->lead_out_ts =
2499 gst_util_uint64_scale (GST_SECOND, fps_den * lead_out, fps_num);
2501 GST_LOG_OBJECT (parse, "set fps: %d/%d => duration: %" G_GINT64_FORMAT " ms",
2502 fps_num, fps_den, parse->priv->frame_duration / GST_MSECOND);
2503 GST_LOG_OBJECT (parse, "set lead in: %d frames = %" G_GUINT64_FORMAT " ms, "
2504 "lead out: %d frames = %" G_GUINT64_FORMAT " ms",
2505 lead_in, parse->priv->lead_in_ts / GST_MSECOND,
2506 lead_out, parse->priv->lead_out_ts / GST_MSECOND);
2507 GST_BASE_PARSE_UNLOCK (parse);
2511 * gst_base_transform_get_sync:
2512 * @parse: the #GstBaseParse to query
2514 * Returns: TRUE if parser is considered 'in sync'. That is, frames have been
2515 * continuously successfully parsed and pushed.
2518 gst_base_parse_get_sync (GstBaseParse * parse)
2522 g_return_val_if_fail (parse != NULL, FALSE);
2524 GST_BASE_PARSE_LOCK (parse);
2525 /* losing sync is pretty much a discont (and vice versa), no ? */
2526 ret = !parse->priv->discont;
2527 GST_BASE_PARSE_UNLOCK (parse);
2529 GST_DEBUG_OBJECT (parse, "sync: %d", ret);
2534 * gst_base_transform_get_drain:
2535 * @parse: the #GstBaseParse to query
2537 * Returns: TRUE if parser is currently 'draining'. That is, leftover data
2538 * (e.g. in FLUSH or EOS situation) is being parsed.
2541 gst_base_parse_get_drain (GstBaseParse * parse)
2545 g_return_val_if_fail (parse != NULL, FALSE);
2547 GST_BASE_PARSE_LOCK (parse);
2548 /* losing sync is pretty much a discont (and vice versa), no ? */
2549 ret = parse->priv->drain;
2550 GST_BASE_PARSE_UNLOCK (parse);
2552 GST_DEBUG_OBJECT (parse, "drain: %d", ret);
2557 gst_base_parse_get_duration (GstBaseParse * parse, GstFormat format,
2558 GstClockTime * duration)
2560 gboolean res = FALSE;
2562 g_return_val_if_fail (duration != NULL, FALSE);
2564 *duration = GST_CLOCK_TIME_NONE;
2565 if (parse->priv->duration != -1 && format == parse->priv->duration_fmt) {
2566 GST_LOG_OBJECT (parse, "using provided duration");
2567 *duration = parse->priv->duration;
2569 } else if (parse->priv->duration != -1) {
2570 GST_LOG_OBJECT (parse, "converting provided duration");
2571 res = gst_base_parse_convert (parse, parse->priv->duration_fmt,
2572 parse->priv->duration, format, (gint64 *) duration);
2573 } else if (format == GST_FORMAT_TIME && parse->priv->estimated_duration != -1) {
2574 GST_LOG_OBJECT (parse, "using estimated duration");
2575 *duration = parse->priv->estimated_duration;
2579 GST_LOG_OBJECT (parse, "res: %d, duration %" GST_TIME_FORMAT, res,
2580 GST_TIME_ARGS (*duration));
2585 * gst_base_parse_get_querytypes:
2588 * Returns: A table of #GstQueryType items describing supported query types.
2590 static const GstQueryType *
2591 gst_base_parse_get_querytypes (GstPad * pad)
2593 static const GstQueryType list[] = {
2607 * gst_base_parse_query:
2609 * @query: #GstQuery.
2611 * Returns: TRUE on success.
2614 gst_base_parse_query (GstPad * pad, GstQuery * query)
2616 GstBaseParse *parse;
2617 GstBaseParseClass *klass;
2618 gboolean res = FALSE;
2620 parse = GST_BASE_PARSE (GST_PAD_PARENT (pad));
2621 klass = GST_BASE_PARSE_GET_CLASS (parse);
2623 GST_LOG_OBJECT (parse, "handling query: %" GST_PTR_FORMAT, query);
2625 switch (GST_QUERY_TYPE (query)) {
2626 case GST_QUERY_POSITION:
2631 GST_DEBUG_OBJECT (parse, "position query");
2632 gst_query_parse_position (query, &format, NULL);
2634 g_mutex_lock (parse->parse_lock);
2635 if (format == GST_FORMAT_BYTES) {
2636 dest_value = parse->priv->offset;
2638 } else if (format == parse->segment.format &&
2639 GST_CLOCK_TIME_IS_VALID (parse->segment.last_stop)) {
2640 dest_value = parse->segment.last_stop;
2643 g_mutex_unlock (parse->parse_lock);
2646 gst_query_set_position (query, format, dest_value);
2648 res = gst_pad_query_default (pad, query);
2650 /* no precise result, upstream no idea either, then best estimate */
2651 /* priv->offset is updated in both PUSH/PULL modes */
2652 g_mutex_lock (parse->parse_lock);
2653 res = gst_base_parse_convert (parse,
2654 GST_FORMAT_BYTES, parse->priv->offset, format, &dest_value);
2655 g_mutex_unlock (parse->parse_lock);
2660 case GST_QUERY_DURATION:
2663 GstClockTime duration;
2665 GST_DEBUG_OBJECT (parse, "duration query");
2666 gst_query_parse_duration (query, &format, NULL);
2668 /* consult upstream */
2669 res = gst_pad_query_default (pad, query);
2671 /* otherwise best estimate from us */
2673 g_mutex_lock (parse->parse_lock);
2674 res = gst_base_parse_get_duration (parse, format, &duration);
2675 g_mutex_unlock (parse->parse_lock);
2677 gst_query_set_duration (query, format, duration);
2681 case GST_QUERY_SEEKING:
2684 GstClockTime duration = GST_CLOCK_TIME_NONE;
2685 gboolean seekable = FALSE;
2687 GST_DEBUG_OBJECT (parse, "seeking query");
2688 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
2690 /* consult upstream */
2691 res = gst_pad_query_default (pad, query);
2693 /* we may be able to help if in TIME */
2694 if (fmt == GST_FORMAT_TIME &&
2695 parse->priv->seekable > GST_BASE_PARSE_SEEK_NONE) {
2696 gst_query_parse_seeking (query, &fmt, &seekable, NULL, NULL);
2697 /* already OK if upstream takes care */
2698 GST_LOG_OBJECT (parse, "upstream handled %d, seekable %d",
2700 if (!(res && seekable)) {
2701 if (!gst_base_parse_get_duration (parse, GST_FORMAT_TIME, &duration)
2702 || duration == -1) {
2705 seekable = parse->priv->upstream_seekable;
2706 GST_LOG_OBJECT (parse, "already determine upstream seekabled: %d",
2709 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable, 0, duration);
2715 case GST_QUERY_FORMATS:
2716 gst_query_set_formatsv (query, 3, fmtlist);
2719 case GST_QUERY_CONVERT:
2721 GstFormat src_format, dest_format;
2722 gint64 src_value, dest_value;
2724 gst_query_parse_convert (query, &src_format, &src_value,
2725 &dest_format, &dest_value);
2727 res = gst_base_parse_convert (parse, src_format, src_value,
2728 dest_format, &dest_value);
2730 gst_query_set_convert (query, src_format, src_value,
2731 dest_format, dest_value);
2736 res = gst_pad_query_default (pad, query);
2743 gst_base_parse_find_offset (GstBaseParse * parse, GstClockTime time,
2744 gboolean before, GstClockTime * _ts)
2746 gint64 bytes = 0, ts = 0;
2747 GstIndexEntry *entry = NULL;
2749 if (time == GST_CLOCK_TIME_NONE) {
2755 GST_OBJECT_LOCK (parse);
2756 if (parse->priv->index) {
2757 /* Let's check if we have an index entry for that time */
2758 entry = gst_index_get_assoc_entry (parse->priv->index,
2759 parse->priv->index_id,
2760 before ? GST_INDEX_LOOKUP_BEFORE : GST_INDEX_LOOKUP_AFTER,
2761 GST_ASSOCIATION_FLAG_KEY_UNIT, GST_FORMAT_TIME, time);
2765 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &bytes);
2766 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &ts);
2768 GST_DEBUG_OBJECT (parse, "found index entry for %" GST_TIME_FORMAT
2769 " at %" GST_TIME_FORMAT ", offset %" G_GINT64_FORMAT,
2770 GST_TIME_ARGS (time), GST_TIME_ARGS (ts), bytes);
2772 GST_DEBUG_OBJECT (parse, "no index entry found for %" GST_TIME_FORMAT,
2773 GST_TIME_ARGS (time));
2776 ts = GST_CLOCK_TIME_NONE;
2779 GST_OBJECT_UNLOCK (parse);
2790 * gst_base_parse_handle_seek:
2791 * @parse: #GstBaseParse.
2792 * @event: #GstEvent.
2794 * Returns: TRUE if seek succeeded.
2797 gst_base_parse_handle_seek (GstBaseParse * parse, GstEvent * event)
2799 GstBaseParseClass *klass;
2803 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
2804 gboolean flush, update, res = TRUE, accurate;
2805 gint64 cur, stop, seekpos, seekstop;
2806 GstSegment seeksegment = { 0, };
2807 GstFormat dstformat;
2808 GstClockTime start_ts;
2810 klass = GST_BASE_PARSE_GET_CLASS (parse);
2812 gst_event_parse_seek (event, &rate, &format, &flags,
2813 &cur_type, &cur, &stop_type, &stop);
2815 GST_DEBUG_OBJECT (parse, "seek to format %s, rate %f, "
2816 "start type %d at %" GST_TIME_FORMAT ", end type %d at %"
2817 GST_TIME_FORMAT, gst_format_get_name (format), rate,
2818 cur_type, GST_TIME_ARGS (cur), stop_type, GST_TIME_ARGS (stop));
2820 /* no negative rates yet */
2821 if (rate < 0.0 && parse->priv->pad_mode == GST_ACTIVATE_PUSH)
2824 if (cur_type != GST_SEEK_TYPE_SET ||
2825 (stop_type != GST_SEEK_TYPE_SET && stop_type != GST_SEEK_TYPE_NONE))
2828 /* For any format other than TIME, see if upstream handles
2829 * it directly or fail. For TIME, try upstream, but do it ourselves if
2830 * it fails upstream */
2831 if (format != GST_FORMAT_TIME) {
2832 /* default action delegates to upstream */
2835 gst_event_ref (event);
2836 if (gst_pad_push_event (parse->sinkpad, event)) {
2841 /* get flush flag */
2842 flush = flags & GST_SEEK_FLAG_FLUSH;
2844 /* copy segment, we need this because we still need the old
2845 * segment when we close the current segment. */
2846 memcpy (&seeksegment, &parse->segment, sizeof (GstSegment));
2848 GST_DEBUG_OBJECT (parse, "configuring seek");
2849 gst_segment_set_seek (&seeksegment, rate, format, flags,
2850 cur_type, cur, stop_type, stop, &update);
2852 /* accurate seeking implies seek tables are used to obtain position,
2853 * and the requested segment is maintained exactly, not adjusted any way */
2854 accurate = flags & GST_SEEK_FLAG_ACCURATE;
2856 /* maybe we can be accurate for (almost) free */
2857 if (seeksegment.last_stop <= parse->priv->index_last_ts + 20 * GST_SECOND) {
2858 GST_DEBUG_OBJECT (parse,
2859 "seek position %" GST_TIME_FORMAT " <= %" GST_TIME_FORMAT
2860 "accurate seek possible", GST_TIME_ARGS (seeksegment.last_stop),
2861 GST_TIME_ARGS (parse->priv->index_last_ts));
2865 GstClockTime startpos = seeksegment.last_stop;
2867 /* accurate requested, so ... seek a bit before target */
2868 if (startpos < parse->priv->lead_in_ts)
2871 startpos -= parse->priv->lead_in_ts;
2872 seekpos = gst_base_parse_find_offset (parse, startpos, TRUE, &start_ts);
2873 seekstop = gst_base_parse_find_offset (parse, seeksegment.stop, FALSE,
2876 start_ts = seeksegment.last_stop;
2877 dstformat = GST_FORMAT_BYTES;
2878 if (!gst_pad_query_convert (parse->srcpad, format, seeksegment.last_stop,
2879 &dstformat, &seekpos))
2880 goto convert_failed;
2881 if (!gst_pad_query_convert (parse->srcpad, format, seeksegment.stop,
2882 &dstformat, &seekstop))
2883 goto convert_failed;
2886 GST_DEBUG_OBJECT (parse,
2887 "seek position %" G_GINT64_FORMAT " in bytes: %" G_GINT64_FORMAT,
2889 GST_DEBUG_OBJECT (parse,
2890 "seek stop %" G_GINT64_FORMAT " in bytes: %" G_GINT64_FORMAT,
2891 seeksegment.stop, seekstop);
2893 if (parse->priv->pad_mode == GST_ACTIVATE_PULL) {
2896 GST_DEBUG_OBJECT (parse, "seek in PULL mode");
2899 if (parse->srcpad) {
2900 GST_DEBUG_OBJECT (parse, "sending flush start");
2901 gst_pad_push_event (parse->srcpad, gst_event_new_flush_start ());
2904 gst_pad_pause_task (parse->sinkpad);
2907 /* we should now be able to grab the streaming thread because we stopped it
2908 * with the above flush/pause code */
2909 GST_PAD_STREAM_LOCK (parse->sinkpad);
2911 /* save current position */
2912 last_stop = parse->segment.last_stop;
2913 GST_DEBUG_OBJECT (parse, "stopped streaming at %" G_GINT64_FORMAT,
2916 /* now commit to new position */
2918 /* prepare for streaming again */
2920 GST_DEBUG_OBJECT (parse, "sending flush stop");
2921 gst_pad_push_event (parse->srcpad, gst_event_new_flush_stop ());
2922 gst_base_parse_clear_queues (parse);
2924 if (parse->close_segment)
2925 gst_event_unref (parse->close_segment);
2927 parse->close_segment = gst_event_new_new_segment (TRUE,
2928 parse->segment.rate, parse->segment.format,
2929 parse->segment.accum, parse->segment.last_stop, parse->segment.accum);
2931 /* keep track of our last_stop */
2932 seeksegment.accum = parse->segment.last_stop;
2934 GST_DEBUG_OBJECT (parse, "Created close seg format %d, "
2935 "start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT
2936 ", pos = %" GST_TIME_FORMAT, format,
2937 GST_TIME_ARGS (parse->segment.accum),
2938 GST_TIME_ARGS (parse->segment.last_stop),
2939 GST_TIME_ARGS (parse->segment.accum));
2942 memcpy (&parse->segment, &seeksegment, sizeof (GstSegment));
2944 /* store the newsegment event so it can be sent from the streaming thread. */
2945 if (parse->pending_segment)
2946 gst_event_unref (parse->pending_segment);
2948 /* This will be sent later in _loop() */
2949 parse->pending_segment =
2950 gst_event_new_new_segment (FALSE, parse->segment.rate,
2951 parse->segment.format, parse->segment.start, parse->segment.stop,
2952 parse->segment.start);
2954 GST_DEBUG_OBJECT (parse, "Created newseg format %d, "
2955 "start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT
2956 ", pos = %" GST_TIME_FORMAT, format,
2957 GST_TIME_ARGS (parse->segment.start),
2958 GST_TIME_ARGS (parse->segment.stop),
2959 GST_TIME_ARGS (parse->segment.start));
2961 /* mark discont if we are going to stream from another position. */
2962 if (seekpos != parse->priv->offset) {
2963 GST_DEBUG_OBJECT (parse,
2964 "mark DISCONT, we did a seek to another position");
2965 parse->priv->offset = seekpos;
2966 parse->priv->last_offset = seekpos;
2967 parse->priv->discont = TRUE;
2968 parse->priv->next_ts = start_ts;
2969 parse->priv->last_ts = GST_CLOCK_TIME_NONE;
2970 parse->priv->sync_offset = seekpos;
2971 parse->priv->exact_position = accurate;
2974 /* Start streaming thread if paused */
2975 gst_pad_start_task (parse->sinkpad,
2976 (GstTaskFunction) gst_base_parse_loop, parse->sinkpad);
2978 GST_PAD_STREAM_UNLOCK (parse->sinkpad);
2980 GstEvent *new_event;
2981 GstBaseParseSeek *seek;
2983 /* The only thing we need to do in PUSH-mode is to send the
2984 seek event (in bytes) to upstream. Segment / flush handling happens
2985 in corresponding src event handlers */
2986 GST_DEBUG_OBJECT (parse, "seek in PUSH mode");
2987 new_event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flush,
2988 GST_SEEK_TYPE_SET, seekpos, stop_type, seekstop);
2990 /* store segment info so its precise details can be reconstructed when
2991 * receiving newsegment;
2992 * this matters for all details when accurate seeking,
2993 * is most useful to preserve NONE stop time otherwise */
2994 seek = g_new0 (GstBaseParseSeek, 1);
2995 seek->segment = seeksegment;
2996 seek->accurate = accurate;
2997 seek->offset = seekpos;
2998 seek->start_ts = start_ts;
2999 GST_OBJECT_LOCK (parse);
3000 /* less optimal, but preserves order */
3001 parse->priv->pending_seeks =
3002 g_slist_append (parse->priv->pending_seeks, seek);
3003 GST_OBJECT_UNLOCK (parse);
3005 res = gst_pad_push_event (parse->sinkpad, new_event);
3008 GST_OBJECT_LOCK (parse);
3009 parse->priv->pending_seeks =
3010 g_slist_remove (parse->priv->pending_seeks, seek);
3011 GST_OBJECT_UNLOCK (parse);
3022 GST_DEBUG_OBJECT (parse, "negative playback rates are not supported yet.");
3028 GST_DEBUG_OBJECT (parse, "unsupported seek type.");
3034 GST_DEBUG_OBJECT (parse, "conversion TIME to BYTES failed.");
3041 * gst_base_parse_handle_tag:
3042 * @parse: #GstBaseParse.
3043 * @event: #GstEvent.
3045 * Checks if bitrates are available from upstream tags so that we don't
3046 * override them later
3049 gst_base_parse_handle_tag (GstBaseParse * parse, GstEvent * event)
3051 GstTagList *taglist = NULL;
3054 gst_event_parse_tag (event, &taglist);
3056 if (gst_tag_list_get_uint (taglist, GST_TAG_MINIMUM_BITRATE, &tmp))
3057 parse->priv->post_min_bitrate = FALSE;
3058 if (gst_tag_list_get_uint (taglist, GST_TAG_BITRATE, &tmp))
3059 parse->priv->post_avg_bitrate = FALSE;
3060 if (gst_tag_list_get_uint (taglist, GST_TAG_MAXIMUM_BITRATE, &tmp))
3061 parse->priv->post_max_bitrate = FALSE;
3065 * gst_base_parse_sink_setcaps:
3069 * Returns: TRUE if caps were accepted.
3072 gst_base_parse_sink_setcaps (GstPad * pad, GstCaps * caps)
3074 GstBaseParse *parse;
3075 GstBaseParseClass *klass;
3076 gboolean res = TRUE;
3078 parse = GST_BASE_PARSE (GST_PAD_PARENT (pad));
3079 klass = GST_BASE_PARSE_GET_CLASS (parse);
3081 GST_DEBUG_OBJECT (parse, "caps: %" GST_PTR_FORMAT, caps);
3083 if (klass->set_sink_caps)
3084 res = klass->set_sink_caps (parse, caps);
3086 return res && gst_pad_set_caps (pad, caps);
3090 gst_base_parse_set_index (GstElement * element, GstIndex * index)
3092 GstBaseParse *parse = GST_BASE_PARSE (element);
3094 GST_OBJECT_LOCK (parse);
3095 if (parse->priv->index)
3096 gst_object_unref (parse->priv->index);
3098 parse->priv->index = gst_object_ref (index);
3099 gst_index_get_writer_id (index, GST_OBJECT (element),
3100 &parse->priv->index_id);
3101 parse->priv->own_index = FALSE;
3103 parse->priv->index = NULL;
3104 GST_OBJECT_UNLOCK (parse);
3108 gst_base_parse_get_index (GstElement * element)
3110 GstBaseParse *parse = GST_BASE_PARSE (element);
3111 GstIndex *result = NULL;
3113 GST_OBJECT_LOCK (parse);
3114 if (parse->priv->index)
3115 result = gst_object_ref (parse->priv->index);
3116 GST_OBJECT_UNLOCK (parse);
3121 static GstStateChangeReturn
3122 gst_base_parse_change_state (GstElement * element, GstStateChange transition)
3124 GstBaseParse *parse;
3125 GstStateChangeReturn result;
3127 parse = GST_BASE_PARSE (element);
3129 switch (transition) {
3130 case GST_STATE_CHANGE_READY_TO_PAUSED:
3131 /* If this is our own index destroy it as the
3132 * old entries might be wrong for the new stream */
3133 if (parse->priv->own_index) {
3134 gst_object_unref (parse->priv->index);
3135 parse->priv->index = NULL;
3136 parse->priv->own_index = FALSE;
3139 /* If no index was created, generate one */
3140 if (G_UNLIKELY (!parse->priv->index)) {
3141 GST_DEBUG_OBJECT (parse, "no index provided creating our own");
3143 parse->priv->index = gst_index_factory_make ("memindex");
3144 gst_index_get_writer_id (parse->priv->index, GST_OBJECT (parse),
3145 &parse->priv->index_id);
3146 parse->priv->own_index = TRUE;
3153 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3155 switch (transition) {
3156 case GST_STATE_CHANGE_PAUSED_TO_READY:
3157 gst_base_parse_reset (parse);