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 gboolean upstream_has_duration;
262 /* minimum distance between two index entries */
263 GstClockTimeDiff idx_interval;
264 /* ts and offset of last entry added */
265 GstClockTime index_last_ts;
266 guint64 index_last_offset;
268 /* timestamps currently produced are accurate, e.g. started from 0 onwards */
269 gboolean exact_position;
270 /* seek events are temporarily kept to match them with newsegments */
271 GSList *pending_seeks;
273 /* reverse playback */
274 GSList *buffers_pending;
275 GSList *buffers_queued;
276 GstClockTime last_ts;
280 typedef struct _GstBaseParseSeek
285 GstClockTime start_ts;
288 static GstElementClass *parent_class = NULL;
290 static void gst_base_parse_class_init (GstBaseParseClass * klass);
291 static void gst_base_parse_init (GstBaseParse * parse,
292 GstBaseParseClass * klass);
295 gst_base_parse_get_type (void)
297 static GType base_parse_type = 0;
299 if (!base_parse_type) {
300 static const GTypeInfo base_parse_info = {
301 sizeof (GstBaseParseClass),
302 (GBaseInitFunc) NULL,
303 (GBaseFinalizeFunc) NULL,
304 (GClassInitFunc) gst_base_parse_class_init,
307 sizeof (GstBaseParse),
309 (GInstanceInitFunc) gst_base_parse_init,
312 base_parse_type = g_type_register_static (GST_TYPE_ELEMENT,
313 "GstAudioBaseParseBad", &base_parse_info, G_TYPE_FLAG_ABSTRACT);
315 return base_parse_type;
318 static void gst_base_parse_finalize (GObject * object);
320 static GstStateChangeReturn gst_base_parse_change_state (GstElement * element,
321 GstStateChange transition);
322 static void gst_base_parse_reset (GstBaseParse * parse);
324 static void gst_base_parse_set_index (GstElement * element, GstIndex * index);
325 static GstIndex *gst_base_parse_get_index (GstElement * element);
327 static gboolean gst_base_parse_sink_activate (GstPad * sinkpad);
328 static gboolean gst_base_parse_sink_activate_push (GstPad * pad,
330 static gboolean gst_base_parse_sink_activate_pull (GstPad * pad,
332 static gboolean gst_base_parse_handle_seek (GstBaseParse * parse,
334 static void gst_base_parse_handle_tag (GstBaseParse * parse, GstEvent * event);
336 static gboolean gst_base_parse_src_event (GstPad * pad, GstEvent * event);
337 static gboolean gst_base_parse_sink_event (GstPad * pad, GstEvent * event);
338 static gboolean gst_base_parse_query (GstPad * pad, GstQuery * query);
339 static gboolean gst_base_parse_sink_setcaps (GstPad * pad, GstCaps * caps);
340 static const GstQueryType *gst_base_parse_get_querytypes (GstPad * pad);
342 static GstFlowReturn gst_base_parse_chain (GstPad * pad, GstBuffer * buffer);
343 static void gst_base_parse_loop (GstPad * pad);
345 static gboolean gst_base_parse_check_frame (GstBaseParse * parse,
346 GstBuffer * buffer, guint * framesize, gint * skipsize);
348 static GstFlowReturn gst_base_parse_parse_frame (GstBaseParse * parse,
351 static gboolean gst_base_parse_sink_eventfunc (GstBaseParse * parse,
354 static gboolean gst_base_parse_src_eventfunc (GstBaseParse * parse,
357 static void gst_base_parse_drain (GstBaseParse * parse);
359 static void gst_base_parse_post_bitrates (GstBaseParse * parse,
360 gboolean post_min, gboolean post_avg, gboolean post_max);
362 static gint64 gst_base_parse_find_offset (GstBaseParse * parse,
363 GstClockTime time, gboolean before, GstClockTime * _ts);
365 static GstFlowReturn gst_base_parse_process_fragment (GstBaseParse * parse,
369 gst_base_parse_clear_queues (GstBaseParse * parse)
371 g_slist_foreach (parse->priv->buffers_queued, (GFunc) gst_buffer_unref, NULL);
372 g_slist_free (parse->priv->buffers_queued);
373 parse->priv->buffers_queued = NULL;
374 g_slist_foreach (parse->priv->buffers_pending, (GFunc) gst_buffer_unref,
376 g_slist_free (parse->priv->buffers_pending);
377 parse->priv->buffers_pending = NULL;
381 gst_base_parse_finalize (GObject * object)
383 GstBaseParse *parse = GST_BASE_PARSE (object);
386 g_mutex_free (parse->parse_lock);
387 g_object_unref (parse->adapter);
389 if (parse->pending_segment) {
390 p_ev = &parse->pending_segment;
391 gst_event_replace (p_ev, NULL);
393 if (parse->close_segment) {
394 p_ev = &parse->close_segment;
395 gst_event_replace (p_ev, NULL);
398 if (parse->priv->cache) {
399 gst_buffer_unref (parse->priv->cache);
400 parse->priv->cache = NULL;
403 g_list_foreach (parse->priv->pending_events, (GFunc) gst_mini_object_unref,
405 g_list_free (parse->priv->pending_events);
406 parse->priv->pending_events = NULL;
408 if (parse->priv->index) {
409 gst_object_unref (parse->priv->index);
410 parse->priv->index = NULL;
413 gst_base_parse_clear_queues (parse);
415 G_OBJECT_CLASS (parent_class)->finalize (object);
419 gst_base_parse_class_init (GstBaseParseClass * klass)
421 GObjectClass *gobject_class;
422 GstElementClass *gstelement_class;
424 gobject_class = G_OBJECT_CLASS (klass);
425 g_type_class_add_private (klass, sizeof (GstBaseParsePrivate));
426 parent_class = g_type_class_peek_parent (klass);
427 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_parse_finalize);
429 gstelement_class = (GstElementClass *) klass;
430 gstelement_class->change_state =
431 GST_DEBUG_FUNCPTR (gst_base_parse_change_state);
432 gstelement_class->set_index = GST_DEBUG_FUNCPTR (gst_base_parse_set_index);
433 gstelement_class->get_index = GST_DEBUG_FUNCPTR (gst_base_parse_get_index);
435 /* Default handlers */
436 klass->check_valid_frame = gst_base_parse_check_frame;
437 klass->parse_frame = gst_base_parse_parse_frame;
438 klass->src_event = gst_base_parse_src_eventfunc;
439 klass->convert = gst_base_parse_convert_default;
441 GST_DEBUG_CATEGORY_INIT (gst_base_parse_debug, "baseparse", 0,
442 "baseparse element");
446 gst_base_parse_init (GstBaseParse * parse, GstBaseParseClass * bclass)
448 GstPadTemplate *pad_template;
450 GST_DEBUG_OBJECT (parse, "gst_base_parse_init");
452 parse->priv = GST_BASE_PARSE_GET_PRIVATE (parse);
455 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "sink");
456 g_return_if_fail (pad_template != NULL);
457 parse->sinkpad = gst_pad_new_from_template (pad_template, "sink");
458 gst_pad_set_event_function (parse->sinkpad,
459 GST_DEBUG_FUNCPTR (gst_base_parse_sink_event));
460 gst_pad_set_setcaps_function (parse->sinkpad,
461 GST_DEBUG_FUNCPTR (gst_base_parse_sink_setcaps));
462 gst_pad_set_chain_function (parse->sinkpad,
463 GST_DEBUG_FUNCPTR (gst_base_parse_chain));
464 gst_pad_set_activate_function (parse->sinkpad,
465 GST_DEBUG_FUNCPTR (gst_base_parse_sink_activate));
466 gst_pad_set_activatepush_function (parse->sinkpad,
467 GST_DEBUG_FUNCPTR (gst_base_parse_sink_activate_push));
468 gst_pad_set_activatepull_function (parse->sinkpad,
469 GST_DEBUG_FUNCPTR (gst_base_parse_sink_activate_pull));
470 gst_element_add_pad (GST_ELEMENT (parse), parse->sinkpad);
472 GST_DEBUG_OBJECT (parse, "sinkpad created");
475 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
476 g_return_if_fail (pad_template != NULL);
477 parse->srcpad = gst_pad_new_from_template (pad_template, "src");
478 gst_pad_set_event_function (parse->srcpad,
479 GST_DEBUG_FUNCPTR (gst_base_parse_src_event));
480 gst_pad_set_query_type_function (parse->srcpad,
481 GST_DEBUG_FUNCPTR (gst_base_parse_get_querytypes));
482 gst_pad_set_query_function (parse->srcpad,
483 GST_DEBUG_FUNCPTR (gst_base_parse_query));
484 gst_pad_use_fixed_caps (parse->srcpad);
485 gst_element_add_pad (GST_ELEMENT (parse), parse->srcpad);
486 GST_DEBUG_OBJECT (parse, "src created");
488 parse->parse_lock = g_mutex_new ();
489 parse->adapter = gst_adapter_new ();
491 parse->priv->pad_mode = GST_ACTIVATE_NONE;
494 gst_base_parse_reset (parse);
495 GST_DEBUG_OBJECT (parse, "init ok");
499 gst_base_parse_reset (GstBaseParse * parse)
501 GST_OBJECT_LOCK (parse);
502 gst_segment_init (&parse->segment, GST_FORMAT_TIME);
503 parse->priv->duration = -1;
504 parse->priv->min_frame_size = 1;
505 parse->priv->discont = TRUE;
506 parse->priv->flushing = FALSE;
507 parse->priv->offset = 0;
508 parse->priv->sync_offset = 0;
509 parse->priv->update_interval = 50;
510 parse->priv->fps_num = parse->priv->fps_den = 0;
511 parse->priv->frame_duration = GST_CLOCK_TIME_NONE;
512 parse->priv->lead_in = parse->priv->lead_out = 0;
513 parse->priv->lead_in_ts = parse->priv->lead_out_ts = 0;
514 parse->priv->seekable = GST_BASE_PARSE_SEEK_DEFAULT;
515 parse->priv->bitrate = 0;
516 parse->priv->framecount = 0;
517 parse->priv->bytecount = 0;
518 parse->priv->acc_duration = 0;
519 parse->priv->estimated_duration = -1;
520 parse->priv->next_ts = 0;
521 parse->priv->passthrough = FALSE;
522 parse->priv->post_min_bitrate = TRUE;
523 parse->priv->post_avg_bitrate = TRUE;
524 parse->priv->post_max_bitrate = TRUE;
525 parse->priv->min_bitrate = G_MAXUINT;
526 parse->priv->max_bitrate = 0;
527 parse->priv->avg_bitrate = 0;
528 parse->priv->posted_avg_bitrate = 0;
530 parse->priv->index_last_ts = 0;
531 parse->priv->index_last_offset = 0;
532 parse->priv->upstream_seekable = FALSE;
533 parse->priv->upstream_has_duration = FALSE;
534 parse->priv->idx_interval = 0;
535 parse->priv->exact_position = TRUE;
537 parse->priv->last_ts = GST_CLOCK_TIME_NONE;
538 parse->priv->last_offset = 0;
540 if (parse->pending_segment)
541 gst_event_unref (parse->pending_segment);
543 g_list_foreach (parse->priv->pending_events, (GFunc) gst_mini_object_unref,
545 g_list_free (parse->priv->pending_events);
546 parse->priv->pending_events = NULL;
548 if (parse->priv->cache) {
549 gst_buffer_unref (parse->priv->cache);
550 parse->priv->cache = NULL;
553 g_slist_foreach (parse->priv->pending_seeks, (GFunc) g_free, NULL);
554 g_slist_free (parse->priv->pending_seeks);
555 parse->priv->pending_seeks = NULL;
557 GST_OBJECT_UNLOCK (parse);
561 * gst_base_parse_check_frame:
562 * @parse: #GstBaseParse.
563 * @buffer: GstBuffer.
564 * @framesize: This will be set to tell the found frame size in bytes.
565 * @skipsize: Output parameter that tells how much data needs to be skipped
566 * in order to find the following frame header.
568 * Default callback for check_valid_frame.
570 * Returns: Always TRUE.
573 gst_base_parse_check_frame (GstBaseParse * parse,
574 GstBuffer * buffer, guint * framesize, gint * skipsize)
576 *framesize = GST_BUFFER_SIZE (buffer);
583 * gst_base_parse_parse_frame:
584 * @parse: #GstBaseParse.
585 * @buffer: #GstBuffer.
587 * Default callback for parse_frame.
590 gst_base_parse_parse_frame (GstBaseParse * parse, GstBuffer * buffer)
592 if (!GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
593 GST_CLOCK_TIME_IS_VALID (parse->priv->next_ts)) {
594 GST_BUFFER_TIMESTAMP (buffer) = parse->priv->next_ts;
596 if (!GST_BUFFER_DURATION_IS_VALID (buffer) &&
597 GST_CLOCK_TIME_IS_VALID (parse->priv->frame_duration)) {
598 GST_BUFFER_DURATION (buffer) = parse->priv->frame_duration;
604 * gst_base_parse_convert:
605 * @parse: #GstBaseParse.
606 * @src_format: #GstFormat describing the source format.
607 * @src_value: Source value to be converted.
608 * @dest_format: #GstFormat defining the converted format.
609 * @dest_value: Pointer where the conversion result will be put.
611 * Converts using configured "convert" vmethod in #GstBaseParse class.
613 * Returns: TRUE if conversion was successful.
616 gst_base_parse_convert (GstBaseParse * parse,
617 GstFormat src_format,
618 gint64 src_value, GstFormat dest_format, gint64 * dest_value)
620 GstBaseParseClass *klass = GST_BASE_PARSE_GET_CLASS (parse);
623 g_return_val_if_fail (dest_value != NULL, FALSE);
628 ret = klass->convert (parse, src_format, src_value, dest_format, dest_value);
630 #ifndef GST_DISABLE_GST_DEBUG
633 if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES) {
634 GST_LOG_OBJECT (parse,
635 "TIME -> BYTES: %" GST_TIME_FORMAT " -> %" G_GINT64_FORMAT,
636 GST_TIME_ARGS (src_value), *dest_value);
637 } else if (dest_format == GST_FORMAT_TIME &&
638 src_format == GST_FORMAT_BYTES) {
639 GST_LOG_OBJECT (parse,
640 "BYTES -> TIME: %" G_GINT64_FORMAT " -> %" GST_TIME_FORMAT,
641 src_value, GST_TIME_ARGS (*dest_value));
643 GST_LOG_OBJECT (parse,
644 "%s -> %s: %" G_GINT64_FORMAT " -> %" G_GINT64_FORMAT,
645 GST_STR_NULL (gst_format_get_name (src_format)),
646 GST_STR_NULL (gst_format_get_name (dest_format)),
647 src_value, *dest_value);
650 GST_DEBUG_OBJECT (parse, "conversion failed");
659 * gst_base_parse_sink_event:
660 * @pad: #GstPad that received the event.
661 * @event: #GstEvent to be handled.
663 * Handler for sink pad events.
665 * Returns: TRUE if the event was handled.
668 gst_base_parse_sink_event (GstPad * pad, GstEvent * event)
671 GstBaseParseClass *bclass;
672 gboolean handled = FALSE;
676 parse = GST_BASE_PARSE (gst_pad_get_parent (pad));
677 bclass = GST_BASE_PARSE_GET_CLASS (parse);
679 GST_DEBUG_OBJECT (parse, "handling event %d", GST_EVENT_TYPE (event));
681 /* Cache all events except EOS, NEWSEGMENT and FLUSH_STOP if we have a
683 if (parse->pending_segment && GST_EVENT_TYPE (event) != GST_EVENT_EOS
684 && GST_EVENT_TYPE (event) != GST_EVENT_NEWSEGMENT
685 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_START
686 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
688 if (GST_EVENT_TYPE (event) == GST_EVENT_TAG)
689 /* See if any bitrate tags were posted */
690 gst_base_parse_handle_tag (parse, event);
692 parse->priv->pending_events =
693 g_list_append (parse->priv->pending_events, event);
697 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS &&
698 parse->priv->framecount < MIN_FRAMES_TO_POST_BITRATE)
699 /* We've not posted bitrate tags yet - do so now */
700 gst_base_parse_post_bitrates (parse, TRUE, TRUE, TRUE);
703 handled = bclass->event (parse, event);
706 handled = gst_base_parse_sink_eventfunc (parse, event);
709 ret = gst_pad_event_default (pad, event);
712 gst_object_unref (parse);
713 GST_DEBUG_OBJECT (parse, "event handled");
719 * gst_base_parse_sink_eventfunc:
720 * @parse: #GstBaseParse.
721 * @event: #GstEvent to be handled.
723 * Element-level event handler function.
725 * Returns: TRUE if the event was handled and not need forwarding.
728 gst_base_parse_sink_eventfunc (GstBaseParse * parse, GstEvent * event)
730 gboolean handled = FALSE;
733 switch (GST_EVENT_TYPE (event)) {
734 case GST_EVENT_NEWSEGMENT:
736 gdouble rate, applied_rate;
738 gint64 start, stop, pos, next_ts, offset = 0;
741 gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
742 &format, &start, &stop, &pos);
744 GST_DEBUG_OBJECT (parse, "newseg rate %g, applied rate %g, "
745 "format %d, start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT
746 ", pos = %" GST_TIME_FORMAT, rate, applied_rate, format,
747 GST_TIME_ARGS (start), GST_TIME_ARGS (stop), GST_TIME_ARGS (pos));
749 if (format == GST_FORMAT_BYTES) {
750 GstClockTime seg_start, seg_stop;
751 GstBaseParseSeek *seek = NULL;
754 /* stop time is allowed to be open-ended, but not start & pos */
755 seg_stop = GST_CLOCK_TIME_NONE;
759 GST_OBJECT_LOCK (parse);
760 for (node = parse->priv->pending_seeks; node; node = node->next) {
761 GstBaseParseSeek *tmp = node->data;
763 if (tmp->offset == pos) {
768 parse->priv->pending_seeks =
769 g_slist_remove (parse->priv->pending_seeks, seek);
770 GST_OBJECT_UNLOCK (parse);
773 GST_DEBUG_OBJECT (parse,
774 "Matched newsegment to%s seek: %" GST_SEGMENT_FORMAT,
775 seek->accurate ? " accurate" : "", &seek->segment);
776 seg_start = seek->segment.start;
777 seg_stop = seek->segment.stop;
778 next_ts = seek->start_ts;
779 parse->priv->exact_position = seek->accurate;
782 /* best attempt convert */
783 /* as these are only estimates, stop is kept open-ended to avoid
784 * premature cutting */
785 gst_base_parse_convert (parse, GST_FORMAT_BYTES, start,
786 GST_FORMAT_TIME, (gint64 *) & seg_start);
787 parse->priv->exact_position = (start == 0);
791 gst_event_unref (event);
792 event = gst_event_new_new_segment_full (update, rate, applied_rate,
793 GST_FORMAT_TIME, seg_start, seg_stop, seg_start);
794 format = GST_FORMAT_TIME;
797 GST_DEBUG_OBJECT (parse, "Converted incoming segment to TIME. "
798 "start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT,
799 GST_TIME_ARGS (seg_start), GST_TIME_ARGS (seg_stop));
800 } else if (format != GST_FORMAT_TIME) {
801 /* Unknown incoming segment format. Output a default open-ended
803 gst_event_unref (event);
804 event = gst_event_new_new_segment_full (update, rate, applied_rate,
805 GST_FORMAT_TIME, 0, GST_CLOCK_TIME_NONE, 0);
806 format = GST_FORMAT_TIME;
808 stop = GST_CLOCK_TIME_NONE;
810 /* not considered BYTE seekable if it is talking to us in TIME,
811 * whatever else it might claim */
812 parse->priv->upstream_seekable = FALSE;
816 gst_segment_set_newsegment_full (&parse->segment, update, rate,
817 applied_rate, format, start, stop, start);
819 /* save the segment for later, right before we push a new buffer so that
820 * the caps are fixed and the next linked element can receive
822 eventp = &parse->pending_segment;
823 gst_event_replace (eventp, event);
824 gst_event_unref (event);
827 /* but finish the current segment */
828 GST_DEBUG_OBJECT (parse, "draining current segment");
829 if (parse->segment.rate > 0.0)
830 gst_base_parse_drain (parse);
832 gst_base_parse_process_fragment (parse, FALSE);
833 gst_adapter_clear (parse->adapter);
834 parse->priv->offset = offset;
835 parse->priv->sync_offset = offset;
836 parse->priv->next_ts = next_ts;
837 parse->priv->last_ts = GST_CLOCK_TIME_NONE;
838 parse->priv->discont = TRUE;
842 case GST_EVENT_FLUSH_START:
843 parse->priv->flushing = TRUE;
844 handled = gst_pad_push_event (parse->srcpad, event);
845 /* Wait for _chain() to exit by taking the srcpad STREAM_LOCK */
846 GST_PAD_STREAM_LOCK (parse->srcpad);
847 GST_PAD_STREAM_UNLOCK (parse->srcpad);
851 case GST_EVENT_FLUSH_STOP:
852 gst_adapter_clear (parse->adapter);
853 gst_base_parse_clear_queues (parse);
854 parse->priv->flushing = FALSE;
855 parse->priv->discont = TRUE;
856 parse->priv->last_ts = GST_CLOCK_TIME_NONE;
860 if (parse->segment.rate > 0.0)
861 gst_base_parse_drain (parse);
863 gst_base_parse_process_fragment (parse, FALSE);
865 /* If we STILL have zero frames processed, fire an error */
866 if (parse->priv->framecount == 0) {
867 GST_ELEMENT_ERROR (parse, STREAM, WRONG_TYPE,
868 ("No valid frames found before end of stream"), (NULL));
870 /* newsegment before eos */
871 if (parse->pending_segment) {
872 gst_pad_push_event (parse->srcpad, parse->pending_segment);
873 parse->pending_segment = NULL;
886 * gst_base_parse_src_event:
887 * @pad: #GstPad that received the event.
888 * @event: #GstEvent that was received.
890 * Handler for source pad events.
892 * Returns: TRUE if the event was handled.
895 gst_base_parse_src_event (GstPad * pad, GstEvent * event)
898 GstBaseParseClass *bclass;
899 gboolean handled = FALSE;
902 parse = GST_BASE_PARSE (gst_pad_get_parent (pad));
903 bclass = GST_BASE_PARSE_GET_CLASS (parse);
905 GST_DEBUG_OBJECT (parse, "event %d, %s", GST_EVENT_TYPE (event),
906 GST_EVENT_TYPE_NAME (event));
908 if (bclass->src_event)
909 handled = bclass->src_event (parse, event);
912 ret = gst_pad_event_default (pad, event);
914 gst_event_unref (event);
916 gst_object_unref (parse);
922 * gst_base_parse_src_eventfunc:
923 * @parse: #GstBaseParse.
924 * @event: #GstEvent that was received.
926 * Default srcpad event handler.
928 * Returns: TRUE if the event was handled and can be dropped.
931 gst_base_parse_src_eventfunc (GstBaseParse * parse, GstEvent * event)
933 gboolean handled = FALSE;
934 GstBaseParseClass *bclass;
936 bclass = GST_BASE_PARSE_GET_CLASS (parse);
938 switch (GST_EVENT_TYPE (event)) {
941 if (parse->priv->seekable > GST_BASE_PARSE_SEEK_NONE) {
942 handled = gst_base_parse_handle_seek (parse, event);
954 * gst_base_parse_convert_default:
955 * @parse: #GstBaseParse.
956 * @src_format: #GstFormat describing the source format.
957 * @src_value: Source value to be converted.
958 * @dest_format: #GstFormat defining the converted format.
959 * @dest_value: Pointer where the conversion result will be put.
961 * Default implementation of "convert" vmethod in #GstBaseParse class.
963 * Returns: TRUE if conversion was successful.
966 gst_base_parse_convert_default (GstBaseParse * parse,
967 GstFormat src_format,
968 gint64 src_value, GstFormat dest_format, gint64 * dest_value)
970 gboolean ret = FALSE;
971 guint64 bytes, duration;
973 if (G_UNLIKELY (src_format == dest_format)) {
974 *dest_value = src_value;
978 if (G_UNLIKELY (src_value == -1)) {
983 if (G_UNLIKELY (src_value == 0)) {
988 /* need at least some frames */
989 if (!parse->priv->framecount)
992 duration = parse->priv->acc_duration / GST_MSECOND;
993 bytes = parse->priv->bytecount;
995 if (G_UNLIKELY (!duration || !bytes))
998 if (src_format == GST_FORMAT_BYTES) {
999 if (dest_format == GST_FORMAT_TIME) {
1000 /* BYTES -> TIME conversion */
1001 GST_DEBUG_OBJECT (parse, "converting bytes -> time");
1002 *dest_value = gst_util_uint64_scale (src_value, duration, bytes);
1003 *dest_value *= GST_MSECOND;
1004 GST_DEBUG_OBJECT (parse, "conversion result: %" G_GINT64_FORMAT " ms",
1005 *dest_value / GST_MSECOND);
1008 } else if (src_format == GST_FORMAT_TIME) {
1009 if (dest_format == GST_FORMAT_BYTES) {
1010 GST_DEBUG_OBJECT (parse, "converting time -> bytes");
1011 *dest_value = gst_util_uint64_scale (src_value / GST_MSECOND, bytes,
1013 GST_DEBUG_OBJECT (parse,
1014 "time %" G_GINT64_FORMAT " ms in bytes = %" G_GINT64_FORMAT,
1015 src_value / GST_MSECOND, *dest_value);
1018 } else if (src_format == GST_FORMAT_DEFAULT) {
1019 /* DEFAULT == frame-based */
1020 if (dest_format == GST_FORMAT_TIME) {
1021 if (parse->priv->fps_den) {
1022 *dest_value = gst_util_uint64_scale (src_value,
1023 GST_SECOND * parse->priv->fps_den, parse->priv->fps_num);
1026 } else if (dest_format == GST_FORMAT_BYTES) {
1034 * gst_base_parse_update_duration:
1035 * @parse: #GstBaseParse.
1039 gst_base_parse_update_duration (GstBaseParse * aacparse)
1042 GstBaseParse *parse;
1044 parse = GST_BASE_PARSE (aacparse);
1046 peer = gst_pad_get_peer (parse->sinkpad);
1048 GstFormat pformat = GST_FORMAT_BYTES;
1049 gboolean qres = FALSE;
1050 gint64 ptot, dest_value;
1052 qres = gst_pad_query_duration (peer, &pformat, &ptot);
1053 gst_object_unref (GST_OBJECT (peer));
1055 if (gst_base_parse_convert (parse, pformat, ptot,
1056 GST_FORMAT_TIME, &dest_value))
1057 parse->priv->estimated_duration = dest_value;
1063 gst_base_parse_post_bitrates (GstBaseParse * parse, gboolean post_min,
1064 gboolean post_avg, gboolean post_max)
1066 GstTagList *taglist = gst_tag_list_new ();
1068 if (post_min && parse->priv->post_min_bitrate)
1069 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE,
1070 GST_TAG_MINIMUM_BITRATE, parse->priv->min_bitrate, NULL);
1072 if (post_avg && parse->priv->post_avg_bitrate) {
1073 parse->priv->posted_avg_bitrate = parse->priv->avg_bitrate;
1074 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_BITRATE,
1075 parse->priv->avg_bitrate, NULL);
1078 if (post_max && parse->priv->post_max_bitrate)
1079 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE,
1080 GST_TAG_MAXIMUM_BITRATE, parse->priv->max_bitrate, NULL);
1082 GST_DEBUG_OBJECT (parse, "Updated bitrates. Min: %u, Avg: %u, Max: %u",
1083 parse->priv->min_bitrate, parse->priv->avg_bitrate,
1084 parse->priv->max_bitrate);
1086 gst_element_found_tags_for_pad (GST_ELEMENT (parse), parse->srcpad, taglist);
1090 * gst_base_parse_update_bitrates:
1091 * @parse: #GstBaseParse.
1092 * @buffer: Current frame as a #GstBuffer
1094 * Keeps track of the minimum and maximum bitrates, and also maintains a
1095 * running average bitrate of the stream so far.
1098 gst_base_parse_update_bitrates (GstBaseParse * parse, GstBuffer * buffer)
1100 /* Only update the tag on a 10 kbps delta */
1101 static const gint update_threshold = 10000;
1103 GstBaseParseClass *klass;
1104 guint64 data_len, frame_dur;
1105 gint overhead = 0, frame_bitrate, old_avg_bitrate;
1106 gboolean update_min = FALSE, update_avg = FALSE, update_max = FALSE;
1108 klass = GST_BASE_PARSE_GET_CLASS (parse);
1110 if (klass->get_frame_overhead) {
1111 overhead = klass->get_frame_overhead (parse, buffer);
1116 data_len = GST_BUFFER_SIZE (buffer) - overhead;
1117 parse->priv->data_bytecount += data_len;
1119 /* duration should be valid by now,
1120 * either set by subclass or maybe based on fps settings */
1121 if (GST_BUFFER_DURATION_IS_VALID (buffer)) {
1122 /* Calculate duration of a frame from buffer properties */
1123 frame_dur = GST_BUFFER_DURATION (buffer);
1124 parse->priv->avg_bitrate = (8 * parse->priv->data_bytecount * GST_SECOND) /
1125 parse->priv->acc_duration;
1128 /* No way to figure out frame duration (is this even possible?) */
1132 /* override if subclass provided bitrate, e.g. metadata based */
1133 if (parse->priv->bitrate) {
1134 parse->priv->avg_bitrate = parse->priv->bitrate;
1135 /* spread this (confirmed) info ASAP */
1136 if (parse->priv->posted_avg_bitrate != parse->priv->avg_bitrate)
1137 gst_base_parse_post_bitrates (parse, FALSE, TRUE, FALSE);
1140 frame_bitrate = (8 * data_len * GST_SECOND) / frame_dur;
1142 GST_LOG_OBJECT (parse, "frame bitrate %u, avg bitrate %u", frame_bitrate,
1143 parse->priv->avg_bitrate);
1145 if (parse->priv->framecount < MIN_FRAMES_TO_POST_BITRATE) {
1147 } else if (parse->priv->framecount == MIN_FRAMES_TO_POST_BITRATE) {
1148 /* always post all at threshold time */
1149 update_min = update_max = update_avg = TRUE;
1151 if (frame_bitrate < parse->priv->min_bitrate) {
1152 parse->priv->min_bitrate = frame_bitrate;
1156 if (frame_bitrate > parse->priv->max_bitrate) {
1157 parse->priv->max_bitrate = frame_bitrate;
1161 old_avg_bitrate = parse->priv->posted_avg_bitrate;
1162 if ((gint) (old_avg_bitrate - parse->priv->avg_bitrate) > update_threshold
1163 || (gint) (parse->priv->avg_bitrate - old_avg_bitrate) >
1168 if ((update_min || update_avg || update_max))
1169 gst_base_parse_post_bitrates (parse, update_min, update_avg, update_max);
1171 /* If average bitrate changes that much and no valid (time) duration provided,
1172 * then post a new duration message so applications can update their cached
1174 if (update_avg && !(parse->priv->duration_fmt == GST_FORMAT_TIME &&
1175 GST_CLOCK_TIME_IS_VALID (parse->priv->duration)))
1176 gst_element_post_message (GST_ELEMENT (parse),
1177 gst_message_new_duration (GST_OBJECT (parse), GST_FORMAT_TIME, -1));
1184 * gst_base_parse_add_index_entry:
1185 * @parse: #GstBaseParse.
1186 * @offset: offset of entry
1187 * @ts: timestamp associated with offset
1188 * @key: whether entry refers to keyframe
1189 * @force: add entry disregarding sanity checks
1191 * Adds an entry to the index associating @offset to @ts. It is recommended
1192 * to only add keyframe entries. @force allows to bypass checks, such as
1193 * whether the stream is (upstream) seekable, another entry is already "close"
1194 * to the new entry, etc.
1196 * Returns: #gboolean indicating whether entry was added
1199 gst_base_parse_add_index_entry (GstBaseParse * parse, guint64 offset,
1200 GstClockTime ts, gboolean key, gboolean force)
1202 gboolean ret = FALSE;
1203 GstIndexAssociation associations[2];
1205 GST_LOG_OBJECT (parse, "Adding key=%d index entry %" GST_TIME_FORMAT
1206 " @ offset 0x%08" G_GINT64_MODIFIER "x", key, GST_TIME_ARGS (ts), offset);
1208 if (G_LIKELY (!force)) {
1210 if (!parse->priv->upstream_seekable) {
1211 GST_DEBUG_OBJECT (parse, "upstream not seekable; discarding");
1215 if (parse->priv->index_last_offset >= offset) {
1216 GST_DEBUG_OBJECT (parse, "already have entries up to offset "
1217 "0x%08" G_GINT64_MODIFIER "x", parse->priv->index_last_offset);
1221 if (GST_CLOCK_DIFF (parse->priv->index_last_ts, ts) <
1222 parse->priv->idx_interval) {
1223 GST_DEBUG_OBJECT (parse, "entry too close to last time %" GST_TIME_FORMAT,
1224 GST_TIME_ARGS (parse->priv->index_last_ts));
1229 associations[0].format = GST_FORMAT_TIME;
1230 associations[0].value = ts;
1231 associations[1].format = GST_FORMAT_BYTES;
1232 associations[1].value = offset;
1234 /* index might change on-the-fly, although that would be nutty app ... */
1235 GST_OBJECT_LOCK (parse);
1236 gst_index_add_associationv (parse->priv->index, parse->priv->index_id,
1237 (key) ? GST_ASSOCIATION_FLAG_KEY_UNIT : GST_ASSOCIATION_FLAG_NONE,
1238 2, (const GstIndexAssociation *) &associations);
1239 GST_OBJECT_UNLOCK (parse);
1241 parse->priv->index_last_offset = offset;
1242 parse->priv->index_last_ts = ts;
1250 /* check for seekable upstream, above and beyond a mere query */
1252 gst_base_parse_check_seekability (GstBaseParse * parse)
1255 gboolean seekable = FALSE;
1256 gint64 start = -1, stop = -1;
1257 guint idx_interval = 0;
1259 query = gst_query_new_seeking (GST_FORMAT_BYTES);
1260 if (!gst_pad_peer_query (parse->sinkpad, query)) {
1261 GST_DEBUG_OBJECT (parse, "seeking query failed");
1265 gst_query_parse_seeking (query, NULL, &seekable, &start, &stop);
1267 /* try harder to query upstream size if we didn't get it the first time */
1268 if (seekable && stop == -1) {
1269 GstFormat fmt = GST_FORMAT_BYTES;
1271 GST_DEBUG_OBJECT (parse, "doing duration query to fix up unset stop");
1272 gst_pad_query_peer_duration (parse->sinkpad, &fmt, &stop);
1275 /* if upstream doesn't know the size, it's likely that it's not seekable in
1276 * practice even if it technically may be seekable */
1277 if (seekable && (start != 0 || stop <= start)) {
1278 GST_DEBUG_OBJECT (parse, "seekable but unknown start/stop -> disable");
1282 /* let's not put every single frame into our index */
1284 if (stop < 10 * 1024 * 1024)
1286 else if (stop < 100 * 1024 * 1024)
1289 idx_interval = 1000;
1293 gst_query_unref (query);
1295 GST_DEBUG_OBJECT (parse, "seekable: %d (%" G_GUINT64_FORMAT " - %"
1296 G_GUINT64_FORMAT ")", seekable, start, stop);
1297 parse->priv->upstream_seekable = seekable;
1299 GST_DEBUG_OBJECT (parse, "idx_interval: %ums", idx_interval);
1300 parse->priv->idx_interval = idx_interval * GST_MSECOND;
1303 /* some misc checks on upstream */
1305 gst_base_parse_check_upstream (GstBaseParse * parse)
1307 GstFormat fmt = GST_FORMAT_TIME;
1310 if (gst_pad_query_peer_duration (parse->sinkpad, &fmt, &stop))
1311 if (GST_CLOCK_TIME_IS_VALID (stop) && stop) {
1312 /* upstream has one, accept it also, and no further updates */
1313 gst_base_parse_set_duration (parse, GST_FORMAT_TIME, stop, 0);
1314 parse->priv->upstream_has_duration = TRUE;
1317 GST_DEBUG_OBJECT (parse, "upstream_has_duration: %d",
1318 parse->priv->upstream_has_duration);
1322 * gst_base_parse_handle_and_push_buffer:
1323 * @parse: #GstBaseParse.
1324 * @klass: #GstBaseParseClass.
1325 * @buffer: #GstBuffer.
1327 * Parses the frame from given buffer and pushes it forward. Also performs
1328 * timestamp handling and checks the segment limits.
1330 * This is called with srcpad STREAM_LOCK held.
1332 * Returns: #GstFlowReturn
1334 static GstFlowReturn
1335 gst_base_parse_handle_and_push_buffer (GstBaseParse * parse,
1336 GstBaseParseClass * klass, GstBuffer * buffer)
1340 if (parse->priv->discont) {
1341 GST_DEBUG_OBJECT (parse, "marking DISCONT");
1342 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1343 parse->priv->discont = FALSE;
1346 /* some one-time start-up */
1347 if (G_UNLIKELY (!parse->priv->framecount)) {
1348 gst_base_parse_check_seekability (parse);
1349 gst_base_parse_check_upstream (parse);
1352 GST_LOG_OBJECT (parse,
1353 "parsing frame at offset %" G_GUINT64_FORMAT
1354 " (%#" G_GINT64_MODIFIER "x) of size %d",
1355 GST_BUFFER_OFFSET (buffer), GST_BUFFER_OFFSET (buffer),
1356 GST_BUFFER_SIZE (buffer));
1358 ret = klass->parse_frame (parse, buffer);
1360 /* re-use default handler to add missing metadata as-much-as-possible */
1361 gst_base_parse_parse_frame (parse, buffer);
1362 if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
1363 GST_BUFFER_DURATION_IS_VALID (buffer)) {
1364 parse->priv->next_ts =
1365 GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer);
1367 /* we lost track, do not produce bogus time next time around
1368 * (probably means parser subclass has given up on parsing as well) */
1369 GST_DEBUG_OBJECT (parse, "no next fallback timestamp");
1370 parse->priv->next_ts = GST_CLOCK_TIME_NONE;
1373 /* First buffers are dropped, this means that the subclass needs more
1374 * frames to decide on the format and queues them internally */
1375 /* convert internal flow to OK and mark discont for the next buffer. */
1376 if (ret == GST_BASE_PARSE_FLOW_DROPPED) {
1377 gst_buffer_unref (buffer);
1379 } else if (ret != GST_FLOW_OK) {
1383 return gst_base_parse_push_buffer (parse, buffer);
1387 * gst_base_parse_push_buffer:
1388 * @parse: #GstBaseParse.
1389 * @buffer: #GstBuffer.
1391 * Pushes the buffer downstream, sends any pending events and
1392 * does some timestamp and segment handling.
1394 * This must be called with srcpad STREAM_LOCK held.
1396 * Returns: #GstFlowReturn
1399 gst_base_parse_push_buffer (GstBaseParse * parse, GstBuffer * buffer)
1401 GstFlowReturn ret = GST_FLOW_OK;
1402 GstClockTime last_start = GST_CLOCK_TIME_NONE;
1403 GstClockTime last_stop = GST_CLOCK_TIME_NONE;
1404 GstBaseParseClass *klass = GST_BASE_PARSE_GET_CLASS (parse);
1406 GST_LOG_OBJECT (parse,
1407 "processing buffer of size %d with ts %" GST_TIME_FORMAT
1408 ", duration %" GST_TIME_FORMAT, GST_BUFFER_SIZE (buffer),
1409 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
1410 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
1413 parse->priv->bytecount += GST_BUFFER_SIZE (buffer);
1414 if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BASE_PARSE_BUFFER_FLAG_NO_FRAME)) {
1415 parse->priv->framecount++;
1416 if (GST_BUFFER_DURATION_IS_VALID (buffer)) {
1417 parse->priv->acc_duration += GST_BUFFER_DURATION (buffer);
1420 GST_BUFFER_FLAG_UNSET (buffer, GST_BASE_PARSE_BUFFER_FLAG_NO_FRAME);
1421 if (parse->priv->update_interval &&
1422 (parse->priv->framecount % parse->priv->update_interval) == 0)
1423 gst_base_parse_update_duration (parse);
1425 if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer))
1426 last_start = last_stop = GST_BUFFER_TIMESTAMP (buffer);
1427 if (last_start != GST_CLOCK_TIME_NONE
1428 && GST_BUFFER_DURATION_IS_VALID (buffer))
1429 last_stop = last_start + GST_BUFFER_DURATION (buffer);
1431 /* should have caps by now */
1432 g_return_val_if_fail (GST_PAD_CAPS (parse->srcpad), GST_FLOW_ERROR);
1434 gst_buffer_set_caps (buffer, GST_PAD_CAPS (parse->srcpad));
1436 /* segment adjustment magic; only if we are running the whole show */
1437 if (!parse->priv->passthrough && parse->segment.rate > 0.0 &&
1438 (parse->priv->pad_mode == GST_ACTIVATE_PULL ||
1439 parse->priv->upstream_seekable)) {
1440 /* segment times are typically estimates,
1441 * actual frame data might lead subclass to different timestamps,
1442 * so override segment start from what is supplied there */
1443 if (G_UNLIKELY (parse->pending_segment && !parse->priv->exact_position &&
1444 GST_CLOCK_TIME_IS_VALID (last_start))) {
1445 gst_event_unref (parse->pending_segment);
1446 parse->segment.start =
1447 MIN ((guint64) last_start, (guint64) parse->segment.stop);
1448 GST_DEBUG_OBJECT (parse,
1449 "adjusting pending segment start to %" GST_TIME_FORMAT,
1450 GST_TIME_ARGS (parse->segment.start));
1451 parse->pending_segment =
1452 gst_event_new_new_segment (FALSE, parse->segment.rate,
1453 parse->segment.format, parse->segment.start, parse->segment.stop,
1454 parse->segment.start);
1456 /* handle gaps, e.g. non-zero start-time, in as much not handled by above */
1457 if (GST_CLOCK_TIME_IS_VALID (parse->segment.last_stop) &&
1458 GST_CLOCK_TIME_IS_VALID (last_start)) {
1459 GstClockTimeDiff diff;
1461 /* only send newsegments with increasing start times,
1462 * otherwise if these go back and forth downstream (sinks) increase
1463 * accumulated time and running_time */
1464 diff = GST_CLOCK_DIFF (parse->segment.last_stop, last_start);
1465 if (G_UNLIKELY (diff > 2 * GST_SECOND && last_start > parse->segment.start
1466 && (!GST_CLOCK_TIME_IS_VALID (parse->segment.stop) ||
1467 last_start < parse->segment.stop))) {
1468 GST_DEBUG_OBJECT (parse,
1469 "Gap of %" G_GINT64_FORMAT " ns detected in stream "
1470 "(%" GST_TIME_FORMAT " -> %" GST_TIME_FORMAT "). "
1471 "Sending updated NEWSEGMENT events", diff,
1472 GST_TIME_ARGS (parse->segment.last_stop),
1473 GST_TIME_ARGS (last_start));
1474 if (G_UNLIKELY (parse->pending_segment)) {
1475 gst_event_unref (parse->pending_segment);
1476 parse->segment.start = last_start;
1477 parse->pending_segment =
1478 gst_event_new_new_segment (FALSE, parse->segment.rate,
1479 parse->segment.format, parse->segment.start, parse->segment.stop,
1480 parse->segment.start);
1482 /* send newsegment events such that the gap is not accounted in
1483 * accum time, hence running_time */
1484 /* close ahead of gap */
1485 gst_pad_push_event (parse->srcpad,
1486 gst_event_new_new_segment (TRUE, parse->segment.rate,
1487 parse->segment.format, parse->segment.last_stop,
1488 parse->segment.last_stop, parse->segment.last_stop));
1490 gst_pad_push_event (parse->srcpad,
1491 gst_event_new_new_segment (FALSE, parse->segment.rate,
1492 parse->segment.format, last_start, parse->segment.stop,
1495 /* align segment view with downstream,
1496 * prevents double-counting accum when closing segment */
1497 gst_segment_set_newsegment (&parse->segment, FALSE,
1498 parse->segment.rate, parse->segment.format, last_start,
1499 parse->segment.stop, last_start);
1500 parse->segment.last_stop = last_start;
1505 /* and should then also be linked downstream, so safe to send some events */
1506 if (parse->priv->pad_mode == GST_ACTIVATE_PULL) {
1507 if (G_UNLIKELY (parse->close_segment)) {
1508 GST_DEBUG_OBJECT (parse, "loop sending close segment");
1509 gst_pad_push_event (parse->srcpad, parse->close_segment);
1510 parse->close_segment = NULL;
1513 if (G_UNLIKELY (parse->pending_segment)) {
1514 GST_DEBUG_OBJECT (parse, "loop push pending segment");
1515 gst_pad_push_event (parse->srcpad, parse->pending_segment);
1516 parse->pending_segment = NULL;
1519 if (G_UNLIKELY (parse->pending_segment)) {
1520 GST_DEBUG_OBJECT (parse, "chain pushing a pending segment");
1521 gst_pad_push_event (parse->srcpad, parse->pending_segment);
1522 parse->pending_segment = NULL;
1526 /* update bitrates and optionally post corresponding tags
1527 * (following newsegment) */
1528 gst_base_parse_update_bitrates (parse, buffer);
1530 if (G_UNLIKELY (parse->priv->pending_events)) {
1533 for (l = parse->priv->pending_events; l != NULL; l = l->next) {
1534 gst_pad_push_event (parse->srcpad, GST_EVENT (l->data));
1536 g_list_free (parse->priv->pending_events);
1537 parse->priv->pending_events = NULL;
1540 if (parse->priv->upstream_seekable && parse->priv->exact_position &&
1541 GST_BUFFER_TIMESTAMP_IS_VALID (buffer))
1542 gst_base_parse_add_index_entry (parse, GST_BUFFER_OFFSET (buffer),
1543 GST_BUFFER_TIMESTAMP (buffer),
1544 !GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT), FALSE);
1546 if (klass->pre_push_buffer)
1547 ret = klass->pre_push_buffer (parse, buffer);
1549 ret = GST_BASE_PARSE_FLOW_CLIP;
1551 if (ret == GST_BASE_PARSE_FLOW_CLIP) {
1552 if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
1553 GST_CLOCK_TIME_IS_VALID (parse->segment.stop) &&
1554 GST_BUFFER_TIMESTAMP (buffer) >
1555 parse->segment.stop + parse->priv->lead_out_ts) {
1556 GST_LOG_OBJECT (parse, "Dropped frame, after segment");
1557 ret = GST_FLOW_UNEXPECTED;
1558 } else if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
1559 GST_BUFFER_DURATION_IS_VALID (buffer) &&
1560 GST_CLOCK_TIME_IS_VALID (parse->segment.start) &&
1561 GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer) +
1562 parse->priv->lead_in_ts < parse->segment.start) {
1563 GST_LOG_OBJECT (parse, "Dropped frame, before segment");
1564 ret = GST_BASE_PARSE_FLOW_DROPPED;
1570 if (ret == GST_BASE_PARSE_FLOW_DROPPED) {
1571 GST_LOG_OBJECT (parse, "frame (%d bytes) dropped",
1572 GST_BUFFER_SIZE (buffer));
1573 gst_buffer_unref (buffer);
1575 } else if (ret == GST_FLOW_OK) {
1576 if (parse->segment.rate > 0.0) {
1577 GST_LOG_OBJECT (parse, "frame (%d bytes) pushed: %s",
1578 GST_BUFFER_SIZE (buffer), gst_flow_get_name (ret));
1579 ret = gst_pad_push (parse->srcpad, buffer);
1581 GST_LOG_OBJECT (parse, "frame (%d bytes) queued for now",
1582 GST_BUFFER_SIZE (buffer));
1583 parse->priv->buffers_queued =
1584 g_slist_prepend (parse->priv->buffers_queued, buffer);
1588 gst_buffer_unref (buffer);
1589 GST_LOG_OBJECT (parse, "frame (%d bytes) not pushed: %s",
1590 GST_BUFFER_SIZE (buffer), gst_flow_get_name (ret));
1591 /* if we are not sufficiently in control, let upstream decide on EOS */
1592 if (ret == GST_FLOW_UNEXPECTED &&
1593 (parse->priv->passthrough ||
1594 (parse->priv->pad_mode == GST_ACTIVATE_PUSH &&
1595 !parse->priv->upstream_seekable)))
1599 /* Update current running segment position */
1600 if (ret == GST_FLOW_OK && last_stop != GST_CLOCK_TIME_NONE &&
1601 parse->segment.last_stop < last_stop)
1602 gst_segment_set_last_stop (&parse->segment, GST_FORMAT_TIME, last_stop);
1609 * gst_base_parse_drain:
1610 * @parse: #GstBaseParse.
1612 * Drains the adapter until it is empty. It decreases the min_frame_size to
1613 * match the current adapter size and calls chain method until the adapter
1614 * is emptied or chain returns with error.
1617 gst_base_parse_drain (GstBaseParse * parse)
1621 GST_DEBUG_OBJECT (parse, "draining");
1622 parse->priv->drain = TRUE;
1625 avail = gst_adapter_available (parse->adapter);
1629 if (gst_base_parse_chain (parse->sinkpad, NULL) != GST_FLOW_OK) {
1633 /* nothing changed, maybe due to truncated frame; break infinite loop */
1634 if (avail == gst_adapter_available (parse->adapter)) {
1635 GST_DEBUG_OBJECT (parse, "no change during draining; flushing");
1636 gst_adapter_clear (parse->adapter);
1640 parse->priv->drain = FALSE;
1644 * gst_base_parse_process_fragment:
1645 * @parse: #GstBaseParse.
1647 * Processes a reverse playback (forward) fragment:
1648 * - append head of last fragment that was skipped to current fragment data
1649 * - drain the resulting current fragment data (i.e. repeated chain)
1650 * - add time/duration (if needed) to frames queued by chain
1651 * - push queued data
1653 static GstFlowReturn
1654 gst_base_parse_process_fragment (GstBaseParse * parse, gboolean push_only)
1657 GstFlowReturn ret = GST_FLOW_OK;
1658 GSList *send = NULL;
1664 parse->priv->buffers_pending = g_slist_reverse (parse->priv->buffers_pending);
1665 while (parse->priv->buffers_pending) {
1666 buf = GST_BUFFER_CAST (parse->priv->buffers_pending->data);
1667 GST_LOG_OBJECT (parse, "adding pending buffer (size %d)",
1668 GST_BUFFER_SIZE (buf));
1669 gst_adapter_push (parse->adapter, buf);
1670 parse->priv->buffers_pending =
1671 g_slist_delete_link (parse->priv->buffers_pending,
1672 parse->priv->buffers_pending);
1675 /* invalidate so no fall-back timestamping is performed;
1676 * ok if taken from subclass or upstream */
1677 parse->priv->next_ts = GST_CLOCK_TIME_NONE;
1678 /* prevent it hanging around stop all the time */
1679 parse->segment.last_stop = GST_CLOCK_TIME_NONE;
1681 parse->priv->discont = TRUE;
1683 /* chain looks for frames and queues resulting ones (in stead of pushing) */
1684 /* initial skipped data is added to buffers_pending */
1685 gst_base_parse_drain (parse);
1688 /* add metadata (if needed to queued buffers */
1689 GST_LOG_OBJECT (parse, "last timestamp: %" GST_TIME_FORMAT,
1690 GST_TIME_ARGS (parse->priv->last_ts));
1691 while (parse->priv->buffers_queued) {
1692 buf = GST_BUFFER_CAST (parse->priv->buffers_queued->data);
1694 /* no touching if upstream or parsing provided time */
1695 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
1696 GST_LOG_OBJECT (parse, "buffer has time %" GST_TIME_FORMAT,
1697 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
1698 } else if (GST_CLOCK_TIME_IS_VALID (parse->priv->last_ts) &&
1699 GST_BUFFER_DURATION_IS_VALID (buf)) {
1700 if (G_LIKELY (GST_BUFFER_DURATION (buf) <= parse->priv->last_ts))
1701 parse->priv->last_ts -= GST_BUFFER_DURATION (buf);
1703 parse->priv->last_ts = 0;
1704 GST_BUFFER_TIMESTAMP (buf) = parse->priv->last_ts;
1705 GST_LOG_OBJECT (parse, "applied time %" GST_TIME_FORMAT,
1706 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
1708 /* no idea, very bad */
1709 GST_WARNING_OBJECT (parse, "could not determine time for buffer");
1712 /* reverse order for ascending sending */
1713 send = g_slist_prepend (send, buf);
1714 parse->priv->buffers_queued =
1715 g_slist_delete_link (parse->priv->buffers_queued,
1716 parse->priv->buffers_queued);
1721 buf = GST_BUFFER_CAST (send->data);
1722 GST_LOG_OBJECT (parse, "pushing buffer %p, timestamp %"
1723 GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT
1724 ", offset %" G_GINT64_FORMAT, buf,
1725 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
1726 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)), GST_BUFFER_OFFSET (buf));
1728 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (parse->priv->last_ts)))
1729 parse->priv->last_ts = GST_BUFFER_TIMESTAMP (buf);
1731 /* iterate output queue an push downstream */
1732 ret = gst_pad_push (parse->srcpad, buf);
1733 send = g_slist_delete_link (send, send);
1735 /* clear any leftover if error */
1736 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
1738 buf = GST_BUFFER_CAST (send->data);
1739 gst_buffer_unref (buf);
1740 send = g_slist_delete_link (send, send);
1745 /* any trailing unused no longer usable (ideally none) */
1746 if (G_UNLIKELY (gst_adapter_available (parse->adapter))) {
1747 GST_DEBUG_OBJECT (parse, "discarding %d trailing bytes",
1748 gst_adapter_available (parse->adapter));
1749 gst_adapter_clear (parse->adapter);
1755 /* small helper that checks whether we have been trying to resync too long */
1756 static inline GstFlowReturn
1757 gst_base_parse_check_sync (GstBaseParse * parse)
1759 if (G_UNLIKELY (parse->priv->discont &&
1760 parse->priv->offset - parse->priv->sync_offset > 2 * 1024 * 1024)) {
1761 GST_ELEMENT_ERROR (parse, STREAM, DECODE,
1762 ("Failed to parse stream"), (NULL));
1763 return GST_FLOW_ERROR;
1771 * gst_base_parse_chain:
1773 * @buffer: #GstBuffer.
1775 * Returns: #GstFlowReturn.
1777 static GstFlowReturn
1778 gst_base_parse_chain (GstPad * pad, GstBuffer * buffer)
1780 GstBaseParseClass *bclass;
1781 GstBaseParse *parse;
1782 GstFlowReturn ret = GST_FLOW_OK;
1783 GstBuffer *outbuf = NULL;
1784 GstBuffer *tmpbuf = NULL;
1789 GstClockTime timestamp;
1791 parse = GST_BASE_PARSE (GST_OBJECT_PARENT (pad));
1792 bclass = GST_BASE_PARSE_GET_CLASS (parse);
1794 if (G_LIKELY (buffer)) {
1795 GST_LOG_OBJECT (parse, "buffer size: %d, offset = %" G_GINT64_FORMAT,
1796 GST_BUFFER_SIZE (buffer), GST_BUFFER_OFFSET (buffer));
1797 if (G_UNLIKELY (parse->priv->passthrough)) {
1798 buffer = gst_buffer_make_metadata_writable (buffer);
1799 return gst_base_parse_push_buffer (parse, buffer);
1801 /* upstream feeding us in reverse playback;
1802 * gather each fragment, then process it in single run */
1803 if (parse->segment.rate < 0.0) {
1804 if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT))) {
1805 GST_DEBUG_OBJECT (parse, "buffer starts new reverse playback fragment");
1806 ret = gst_base_parse_process_fragment (parse, FALSE);
1808 gst_adapter_push (parse->adapter, buffer);
1811 gst_adapter_push (parse->adapter, buffer);
1814 /* Parse and push as many frames as possible */
1815 /* Stop either when adapter is empty or we are flushing */
1816 while (!parse->priv->flushing) {
1817 tmpbuf = gst_buffer_new ();
1819 /* Synchronization loop */
1821 GST_BASE_PARSE_LOCK (parse);
1822 min_size = parse->priv->min_frame_size;
1823 GST_BASE_PARSE_UNLOCK (parse);
1825 if (G_UNLIKELY (parse->priv->drain)) {
1826 min_size = gst_adapter_available (parse->adapter);
1827 GST_DEBUG_OBJECT (parse, "draining, data left: %d", min_size);
1828 if (G_UNLIKELY (!min_size)) {
1829 gst_buffer_unref (tmpbuf);
1834 /* Collect at least min_frame_size bytes */
1835 if (gst_adapter_available (parse->adapter) < min_size) {
1836 GST_DEBUG_OBJECT (parse, "not enough data available (only %d bytes)",
1837 gst_adapter_available (parse->adapter));
1838 gst_buffer_unref (tmpbuf);
1842 data = gst_adapter_peek (parse->adapter, min_size);
1843 GST_BUFFER_DATA (tmpbuf) = (guint8 *) data;
1844 GST_BUFFER_SIZE (tmpbuf) = min_size;
1845 GST_BUFFER_OFFSET (tmpbuf) = parse->priv->offset;
1846 GST_BUFFER_FLAG_SET (tmpbuf, GST_MINI_OBJECT_FLAG_READONLY);
1848 if (parse->priv->discont) {
1849 GST_DEBUG_OBJECT (parse, "marking DISCONT");
1850 GST_BUFFER_FLAG_SET (tmpbuf, GST_BUFFER_FLAG_DISCONT);
1854 if (bclass->check_valid_frame (parse, tmpbuf, &fsize, &skip)) {
1855 if (gst_adapter_available (parse->adapter) < fsize) {
1856 GST_DEBUG_OBJECT (parse,
1857 "found valid frame but not enough data available (only %d bytes)",
1858 gst_adapter_available (parse->adapter));
1859 gst_buffer_unref (tmpbuf);
1865 /* subclass didn't touch this value. By default we skip 1 byte */
1869 GST_LOG_OBJECT (parse, "finding sync, skipping %d bytes", skip);
1870 if (parse->segment.rate < 0.0 && !parse->priv->buffers_queued) {
1871 /* reverse playback, and no frames found yet, so we are skipping
1872 * the leading part of a fragment, which may form the tail of
1873 * fragment coming later, hopefully subclass skips efficiently ... */
1874 timestamp = gst_adapter_prev_timestamp (parse->adapter, NULL);
1875 outbuf = gst_adapter_take_buffer (parse->adapter, skip);
1876 outbuf = gst_buffer_make_metadata_writable (outbuf);
1877 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
1878 parse->priv->buffers_pending =
1879 g_slist_prepend (parse->priv->buffers_pending, outbuf);
1882 gst_adapter_flush (parse->adapter, skip);
1884 parse->priv->offset += skip;
1885 if (!parse->priv->discont)
1886 parse->priv->sync_offset = parse->priv->offset;
1887 parse->priv->discont = TRUE;
1889 /* There is a possibility that subclass set the skip value to zero.
1890 This means that it has probably found a frame but wants to ask
1891 more data (by increasing the min_size) to be sure of this. */
1892 if ((ret = gst_base_parse_check_sync (parse)) != GST_FLOW_OK) {
1893 gst_buffer_unref (tmpbuf);
1897 gst_buffer_unref (tmpbuf);
1901 /* Subclass found the sync, but still wants to skip some data */
1902 GST_LOG_OBJECT (parse, "skipping %d bytes", skip);
1903 gst_adapter_flush (parse->adapter, skip);
1904 parse->priv->offset += skip;
1907 /* Grab lock to prevent a race with FLUSH_START handler */
1908 GST_PAD_STREAM_LOCK (parse->srcpad);
1910 /* FLUSH_START event causes the "flushing" flag to be set. In this
1911 * case we can leave the frame pushing loop */
1912 if (parse->priv->flushing) {
1913 GST_PAD_STREAM_UNLOCK (parse->srcpad);
1917 /* FIXME: Would it be more efficient to make a subbuffer instead? */
1918 outbuf = gst_adapter_take_buffer (parse->adapter, fsize);
1919 outbuf = gst_buffer_make_metadata_writable (outbuf);
1921 /* Subclass may want to know the data offset */
1922 GST_BUFFER_OFFSET (outbuf) = parse->priv->offset;
1923 parse->priv->offset += fsize;
1925 /* move along with upstream timestamp (if any),
1926 * but interpolate in between */
1927 timestamp = gst_adapter_prev_timestamp (parse->adapter, NULL);
1928 if (GST_CLOCK_TIME_IS_VALID (timestamp) &&
1929 (parse->priv->prev_ts != timestamp)) {
1930 parse->priv->prev_ts = parse->priv->next_ts = timestamp;
1933 ret = gst_base_parse_handle_and_push_buffer (parse, bclass, outbuf);
1934 GST_PAD_STREAM_UNLOCK (parse->srcpad);
1936 if (ret != GST_FLOW_OK) {
1937 GST_LOG_OBJECT (parse, "push returned %d", ret);
1943 GST_LOG_OBJECT (parse, "chain leaving");
1947 /* pull @size bytes at current offset,
1948 * i.e. at least try to and possibly return a shorter buffer if near the end */
1949 static GstFlowReturn
1950 gst_base_parse_pull_range (GstBaseParse * parse, guint size,
1951 GstBuffer ** buffer)
1953 GstFlowReturn ret = GST_FLOW_OK;
1955 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
1957 /* Caching here actually makes much less difference than one would expect.
1958 * We do it mainly to avoid pulling buffers of 1 byte all the time */
1959 if (parse->priv->cache) {
1960 gint64 cache_offset = GST_BUFFER_OFFSET (parse->priv->cache);
1961 gint cache_size = GST_BUFFER_SIZE (parse->priv->cache);
1963 if (cache_offset <= parse->priv->offset &&
1964 (parse->priv->offset + size) <= (cache_offset + cache_size)) {
1965 *buffer = gst_buffer_create_sub (parse->priv->cache,
1966 parse->priv->offset - cache_offset, size);
1967 GST_BUFFER_OFFSET (*buffer) = parse->priv->offset;
1970 /* not enough data in the cache, free cache and get a new one */
1971 gst_buffer_unref (parse->priv->cache);
1972 parse->priv->cache = NULL;
1975 /* refill the cache */
1977 gst_pad_pull_range (parse->sinkpad, parse->priv->offset, MAX (size,
1978 64 * 1024), &parse->priv->cache);
1979 if (ret != GST_FLOW_OK) {
1980 parse->priv->cache = NULL;
1984 if (GST_BUFFER_SIZE (parse->priv->cache) >= size) {
1985 *buffer = gst_buffer_create_sub (parse->priv->cache, 0, size);
1986 GST_BUFFER_OFFSET (*buffer) = parse->priv->offset;
1990 /* Not possible to get enough data, try a last time with
1991 * requesting exactly the size we need */
1992 gst_buffer_unref (parse->priv->cache);
1993 parse->priv->cache = NULL;
1995 ret = gst_pad_pull_range (parse->sinkpad, parse->priv->offset, size,
1996 &parse->priv->cache);
1998 if (ret != GST_FLOW_OK) {
1999 GST_DEBUG_OBJECT (parse, "pull_range returned %d", ret);
2004 if (GST_BUFFER_SIZE (parse->priv->cache) < size) {
2005 GST_DEBUG_OBJECT (parse, "Returning short buffer at offset %"
2006 G_GUINT64_FORMAT ": wanted %u bytes, got %u bytes", parse->priv->offset,
2007 size, GST_BUFFER_SIZE (parse->priv->cache));
2009 *buffer = parse->priv->cache;
2010 parse->priv->cache = NULL;
2015 *buffer = gst_buffer_create_sub (parse->priv->cache, 0, size);
2016 GST_BUFFER_OFFSET (*buffer) = parse->priv->offset;
2021 static GstFlowReturn
2022 gst_base_parse_handle_previous_fragment (GstBaseParse * parse)
2025 GstClockTime ts = 0;
2029 GST_DEBUG_OBJECT (parse, "fragment ended; last_ts = %" GST_TIME_FORMAT
2030 ", last_offset = %" G_GINT64_FORMAT, GST_TIME_ARGS (parse->priv->last_ts),
2031 parse->priv->last_offset);
2033 if (!parse->priv->last_offset || parse->priv->last_ts <= parse->segment.start) {
2034 GST_DEBUG_OBJECT (parse, "past start of segment %" GST_TIME_FORMAT,
2035 GST_TIME_ARGS (parse->segment.start));
2036 ret = GST_FLOW_UNEXPECTED;
2040 /* last fragment started at last_offset / last_ts;
2041 * seek back 10s capped at 1MB */
2042 if (parse->priv->last_ts >= 10 * GST_SECOND)
2043 ts = parse->priv->last_ts - 10 * GST_SECOND;
2044 /* if we are exact now, we will be more so going backwards */
2045 if (parse->priv->exact_position) {
2046 offset = gst_base_parse_find_offset (parse, ts, TRUE, NULL);
2048 GstFormat dstformat = GST_FORMAT_BYTES;
2050 if (!gst_pad_query_convert (parse->srcpad, GST_FORMAT_TIME, ts,
2051 &dstformat, &offset)) {
2052 GST_DEBUG_OBJECT (parse, "conversion failed, only BYTE based");
2055 offset = CLAMP (offset, parse->priv->last_offset - 1024 * 1024,
2056 parse->priv->last_offset - 1024);
2057 offset = MAX (0, offset);
2059 GST_DEBUG_OBJECT (parse, "next fragment from offset %" G_GINT64_FORMAT,
2061 parse->priv->offset = offset;
2063 ret = gst_base_parse_pull_range (parse, parse->priv->last_offset - offset,
2065 if (ret != GST_FLOW_OK)
2068 gst_adapter_push (parse->adapter, buffer);
2069 ret = gst_base_parse_process_fragment (parse, FALSE);
2070 if (ret != GST_FLOW_OK)
2078 * gst_base_parse_loop:
2081 * Loop that is used in pull mode to retrieve data from upstream.
2084 gst_base_parse_loop (GstPad * pad)
2086 GstBaseParse *parse;
2087 GstBaseParseClass *klass;
2088 GstBuffer *buffer, *outbuf;
2089 GstFlowReturn ret = FALSE;
2090 guint fsize = 0, min_size;
2093 parse = GST_BASE_PARSE (gst_pad_get_parent (pad));
2094 klass = GST_BASE_PARSE_GET_CLASS (parse);
2096 /* reverse playback:
2097 * first fragment (closest to stop time) is handled normally below,
2098 * then we pull in fragments going backwards */
2099 if (parse->segment.rate < 0.0) {
2100 if (GST_CLOCK_TIME_IS_VALID (parse->priv->last_ts)) {
2101 ret = gst_base_parse_handle_previous_fragment (parse);
2108 GST_BASE_PARSE_LOCK (parse);
2109 min_size = parse->priv->min_frame_size;
2110 GST_BASE_PARSE_UNLOCK (parse);
2112 ret = gst_base_parse_pull_range (parse, min_size, &buffer);
2113 if (ret != GST_FLOW_OK)
2116 if (parse->priv->discont) {
2117 GST_DEBUG_OBJECT (parse, "marking DISCONT");
2118 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
2121 /* if we got a short read, inform subclass we are draining leftover
2122 * and no more is to be expected */
2123 if (GST_BUFFER_SIZE (buffer) < min_size)
2124 parse->priv->drain = TRUE;
2127 if (klass->check_valid_frame (parse, buffer, &fsize, &skip)) {
2128 parse->priv->drain = FALSE;
2131 parse->priv->drain = FALSE;
2135 GST_LOG_OBJECT (parse, "finding sync, skipping %d bytes", skip);
2136 if (parse->segment.rate < 0.0 && !parse->priv->buffers_queued) {
2137 /* reverse playback, and no frames found yet, so we are skipping
2138 * the leading part of a fragment, which may form the tail of
2139 * fragment coming later, hopefully subclass skips efficiently ... */
2140 outbuf = gst_buffer_create_sub (buffer, 0, skip);
2141 parse->priv->buffers_pending =
2142 g_slist_prepend (parse->priv->buffers_pending, outbuf);
2145 parse->priv->offset += skip;
2146 if (!parse->priv->discont)
2147 parse->priv->sync_offset = parse->priv->offset;
2148 parse->priv->discont = TRUE;
2150 /* skip == 0 should imply subclass set min_size to need more data ... */
2151 GST_DEBUG_OBJECT (parse, "finding sync...");
2152 gst_buffer_unref (buffer);
2153 if ((ret = gst_base_parse_check_sync (parse)) != GST_FLOW_OK) {
2158 if (fsize <= GST_BUFFER_SIZE (buffer)) {
2159 outbuf = gst_buffer_create_sub (buffer, 0, fsize);
2160 GST_BUFFER_OFFSET (outbuf) = GST_BUFFER_OFFSET (buffer);
2161 gst_buffer_unref (buffer);
2163 gst_buffer_unref (buffer);
2164 ret = gst_base_parse_pull_range (parse, fsize, &outbuf);
2165 if (ret != GST_FLOW_OK)
2167 if (GST_BUFFER_SIZE (outbuf) < fsize)
2171 parse->priv->offset += fsize;
2173 /* Does the subclass want to skip too? */
2175 parse->priv->offset += skip;
2177 /* This always unrefs the outbuf, even if error occurs */
2178 ret = gst_base_parse_handle_and_push_buffer (parse, klass, outbuf);
2180 /* eat expected eos signalling past segment in reverse playback */
2181 if (parse->segment.rate < 0.0 && ret == GST_FLOW_UNEXPECTED &&
2182 parse->segment.last_stop >= parse->segment.stop) {
2183 GST_DEBUG_OBJECT (parse, "downstream has reached end of segment");
2184 /* push what was accumulated during loop run */
2185 gst_base_parse_process_fragment (parse, TRUE);
2190 if (ret == GST_FLOW_UNEXPECTED)
2192 else if (ret != GST_FLOW_OK)
2195 gst_object_unref (parse);
2201 ret = GST_FLOW_UNEXPECTED;
2202 GST_DEBUG_OBJECT (parse, "eos");
2207 gboolean push_eos = FALSE;
2209 GST_DEBUG_OBJECT (parse, "pausing task, reason %s",
2210 gst_flow_get_name (ret));
2211 gst_pad_pause_task (parse->sinkpad);
2213 if (ret == GST_FLOW_UNEXPECTED) {
2214 /* handle end-of-stream/segment */
2215 if (parse->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2218 if ((stop = parse->segment.stop) == -1)
2219 stop = parse->segment.duration;
2221 GST_DEBUG_OBJECT (parse, "sending segment_done");
2223 gst_element_post_message
2224 (GST_ELEMENT_CAST (parse),
2225 gst_message_new_segment_done (GST_OBJECT_CAST (parse),
2226 GST_FORMAT_TIME, stop));
2228 /* If we STILL have zero frames processed, fire an error */
2229 if (parse->priv->framecount == 0) {
2230 GST_ELEMENT_ERROR (parse, STREAM, WRONG_TYPE,
2231 ("No valid frames found before end of stream"), (NULL));
2235 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_UNEXPECTED) {
2236 /* for fatal errors we post an error message, wrong-state is
2237 * not fatal because it happens due to flushes and only means
2238 * that we should stop now. */
2239 GST_ELEMENT_ERROR (parse, STREAM, FAILED, (NULL),
2240 ("streaming stopped, reason %s", gst_flow_get_name (ret)));
2244 /* newsegment before eos */
2245 if (parse->pending_segment) {
2246 gst_pad_push_event (parse->srcpad, parse->pending_segment);
2247 parse->pending_segment = NULL;
2249 gst_pad_push_event (parse->srcpad, gst_event_new_eos ());
2251 gst_object_unref (parse);
2257 * gst_base_parse_sink_activate:
2258 * @sinkpad: #GstPad to be activated.
2260 * Returns: TRUE if activation succeeded.
2263 gst_base_parse_sink_activate (GstPad * sinkpad)
2265 GstBaseParse *parse;
2266 gboolean result = TRUE;
2268 parse = GST_BASE_PARSE (gst_pad_get_parent (sinkpad));
2270 GST_DEBUG_OBJECT (parse, "sink activate");
2272 if (gst_pad_check_pull_range (sinkpad)) {
2273 GST_DEBUG_OBJECT (parse, "trying to activate in pull mode");
2274 result = gst_pad_activate_pull (sinkpad, TRUE);
2276 GST_DEBUG_OBJECT (parse, "trying to activate in push mode");
2277 result = gst_pad_activate_push (sinkpad, TRUE);
2280 GST_DEBUG_OBJECT (parse, "sink activate return %d", result);
2281 gst_object_unref (parse);
2287 * gst_base_parse_activate:
2288 * @parse: #GstBaseParse.
2289 * @active: TRUE if element will be activated, FALSE if disactivated.
2291 * Returns: TRUE if the operation succeeded.
2294 gst_base_parse_activate (GstBaseParse * parse, gboolean active)
2296 GstBaseParseClass *klass;
2297 gboolean result = FALSE;
2299 GST_DEBUG_OBJECT (parse, "activate");
2301 klass = GST_BASE_PARSE_GET_CLASS (parse);
2304 if (parse->priv->pad_mode == GST_ACTIVATE_NONE && klass->start)
2305 result = klass->start (parse);
2307 /* We must make sure streaming has finished before resetting things
2308 * and calling the ::stop vfunc */
2309 GST_PAD_STREAM_LOCK (parse->sinkpad);
2310 GST_PAD_STREAM_UNLOCK (parse->sinkpad);
2312 if (parse->priv->pad_mode != GST_ACTIVATE_NONE && klass->stop)
2313 result = klass->stop (parse);
2315 parse->priv->pad_mode = GST_ACTIVATE_NONE;
2317 GST_DEBUG_OBJECT (parse, "activate: %d", result);
2323 * gst_base_parse_sink_activate_push:
2324 * @pad: #GstPad to be (de)activated.
2325 * @active: TRUE when activating, FALSE when deactivating.
2327 * Returns: TRUE if (de)activation succeeded.
2330 gst_base_parse_sink_activate_push (GstPad * pad, gboolean active)
2332 gboolean result = TRUE;
2333 GstBaseParse *parse;
2335 parse = GST_BASE_PARSE (gst_pad_get_parent (pad));
2337 GST_DEBUG_OBJECT (parse, "sink activate push");
2339 result = gst_base_parse_activate (parse, active);
2342 parse->priv->pad_mode = active ? GST_ACTIVATE_PUSH : GST_ACTIVATE_NONE;
2344 GST_DEBUG_OBJECT (parse, "sink activate push: %d", result);
2346 gst_object_unref (parse);
2352 * gst_base_parse_sink_activate_pull:
2353 * @sinkpad: #GstPad to be (de)activated.
2354 * @active: TRUE when activating, FALSE when deactivating.
2356 * Returns: TRUE if (de)activation succeeded.
2359 gst_base_parse_sink_activate_pull (GstPad * sinkpad, gboolean active)
2361 gboolean result = FALSE;
2362 GstBaseParse *parse;
2364 parse = GST_BASE_PARSE (gst_pad_get_parent (sinkpad));
2366 GST_DEBUG_OBJECT (parse, "activate pull");
2368 result = gst_base_parse_activate (parse, active);
2372 parse->pending_segment = gst_event_new_new_segment (FALSE,
2373 parse->segment.rate, parse->segment.format,
2374 parse->segment.start, parse->segment.stop, parse->segment.last_stop);
2375 result &= gst_pad_start_task (sinkpad,
2376 (GstTaskFunction) gst_base_parse_loop, sinkpad);
2378 result &= gst_pad_stop_task (sinkpad);
2383 parse->priv->pad_mode = active ? GST_ACTIVATE_PULL : GST_ACTIVATE_NONE;
2385 GST_DEBUG_OBJECT (parse, "sink activate pull: %d", result);
2387 gst_object_unref (parse);
2393 * gst_base_parse_set_duration:
2394 * @parse: #GstBaseParse.
2396 * @duration: duration value.
2398 * Sets the duration of the currently playing media. Subclass can use this
2399 * when it is able to determine duration and/or notices a change in the media
2400 * duration. Alternatively, if @interval is non-zero (default), then stream
2401 * duration is determined based on estimated bitrate, and updated every @interval
2404 gst_base_parse_set_duration (GstBaseParse * parse,
2405 GstFormat fmt, gint64 duration, gint interval)
2407 g_return_if_fail (parse != NULL);
2409 GST_BASE_PARSE_LOCK (parse);
2410 if (parse->priv->upstream_has_duration) {
2411 GST_DEBUG_OBJECT (parse, "using upstream duration; discarding update");
2415 if (duration != parse->priv->duration) {
2418 m = gst_message_new_duration (GST_OBJECT (parse), fmt, duration);
2419 gst_element_post_message (GST_ELEMENT (parse), m);
2421 /* TODO: what about duration tag? */
2423 parse->priv->duration = duration;
2424 parse->priv->duration_fmt = fmt;
2425 GST_DEBUG_OBJECT (parse, "set duration: %" G_GINT64_FORMAT, duration);
2426 if (fmt == GST_FORMAT_TIME && GST_CLOCK_TIME_IS_VALID (duration)) {
2427 if (interval != 0) {
2428 GST_DEBUG_OBJECT (parse, "valid duration provided, disabling estimate");
2432 GST_DEBUG_OBJECT (parse, "set update interval: %d", interval);
2433 parse->priv->update_interval = interval;
2435 GST_BASE_PARSE_UNLOCK (parse);
2439 * gst_base_parse_set_seek:
2440 * @parse: #GstBaseParse.
2441 * @seek: #GstBaseParseSeekable.
2442 * @abitrate: average bitrate.
2444 * Sets whether and how the media is seekable (in time).
2445 * Also optionally provides average bitrate detected in media (if non-zero),
2446 * e.g. based on metadata, as it will be posted to the application.
2448 * By default, announced average bitrate is estimated, and seekability is assumed
2449 * possible based on estimated bitrate.
2452 gst_base_parse_set_seek (GstBaseParse * parse,
2453 GstBaseParseSeekable seek, guint bitrate)
2455 parse->priv->seekable = seek;
2456 parse->priv->bitrate = bitrate;
2461 * gst_base_parse_set_min_frame_size:
2462 * @parse: #GstBaseParse.
2463 * @min_size: Minimum size of the data that this base class should give to
2466 * Subclass can use this function to tell the base class that it needs to
2467 * give at least #min_size buffers.
2470 gst_base_parse_set_min_frame_size (GstBaseParse * parse, guint min_size)
2472 g_return_if_fail (parse != NULL);
2474 GST_BASE_PARSE_LOCK (parse);
2475 parse->priv->min_frame_size = min_size;
2476 GST_LOG_OBJECT (parse, "set frame_min_size: %d", min_size);
2477 GST_BASE_PARSE_UNLOCK (parse);
2481 * gst_base_transform_set_passthrough:
2482 * @trans: the #GstBaseParse to set
2483 * @passthrough: boolean indicating passthrough mode.
2485 * Set passthrough mode for this parser. If operating in passthrough,
2486 * incoming buffers are pushed through unmodified.
2489 gst_base_parse_set_passthrough (GstBaseParse * parse, gboolean passthrough)
2491 g_return_if_fail (parse != NULL);
2493 GST_BASE_PARSE_LOCK (parse);
2494 parse->priv->passthrough = passthrough;
2495 GST_LOG_OBJECT (parse, "set passthrough: %d", passthrough);
2496 GST_BASE_PARSE_UNLOCK (parse);
2500 * gst_base_transform_set_frame_props:
2501 * @parse: the #GstBaseParse to set
2502 * @fps_num: frames per second (numerator).
2503 * @fps_den: frames per second (denominator).
2504 * @lead_in: frames needed before a segment for subsequent decode
2505 * @lead_out: frames needed after a segment
2507 * If frames per second is configured, parser can take care of buffer duration
2508 * and timestamping. When performing segment clipping, or seeking to a specific
2509 * location, a corresponding decoder might need an initial @lead_in and a
2510 * following @lead_out number of frames to ensure the desired segment is
2511 * entirely filled upon decoding.
2514 gst_base_parse_set_frame_props (GstBaseParse * parse, guint fps_num,
2515 guint fps_den, guint lead_in, guint lead_out)
2517 g_return_if_fail (parse != NULL);
2519 GST_BASE_PARSE_LOCK (parse);
2520 parse->priv->fps_num = fps_num;
2521 parse->priv->fps_den = fps_den;
2522 if (!fps_num || !fps_den) {
2523 GST_DEBUG_OBJECT (parse, "invalid fps (%d/%d), ignoring parameters",
2525 fps_num = fps_den = 0;
2526 parse->priv->frame_duration = GST_CLOCK_TIME_NONE;
2527 parse->priv->lead_in = parse->priv->lead_out = 0;
2528 parse->priv->lead_in_ts = parse->priv->lead_out_ts = 0;
2530 parse->priv->frame_duration =
2531 gst_util_uint64_scale (GST_SECOND, fps_den, fps_num);
2532 parse->priv->lead_in = lead_in;
2533 parse->priv->lead_out = lead_out;
2534 parse->priv->lead_in_ts =
2535 gst_util_uint64_scale (GST_SECOND, fps_den * lead_in, fps_num);
2536 parse->priv->lead_out_ts =
2537 gst_util_uint64_scale (GST_SECOND, fps_den * lead_out, fps_num);
2539 GST_LOG_OBJECT (parse, "set fps: %d/%d => duration: %" G_GINT64_FORMAT " ms",
2540 fps_num, fps_den, parse->priv->frame_duration / GST_MSECOND);
2541 GST_LOG_OBJECT (parse, "set lead in: %d frames = %" G_GUINT64_FORMAT " ms, "
2542 "lead out: %d frames = %" G_GUINT64_FORMAT " ms",
2543 lead_in, parse->priv->lead_in_ts / GST_MSECOND,
2544 lead_out, parse->priv->lead_out_ts / GST_MSECOND);
2545 GST_BASE_PARSE_UNLOCK (parse);
2549 * gst_base_transform_get_sync:
2550 * @parse: the #GstBaseParse to query
2552 * Returns: TRUE if parser is considered 'in sync'. That is, frames have been
2553 * continuously successfully parsed and pushed.
2556 gst_base_parse_get_sync (GstBaseParse * parse)
2560 g_return_val_if_fail (parse != NULL, FALSE);
2562 GST_BASE_PARSE_LOCK (parse);
2563 /* losing sync is pretty much a discont (and vice versa), no ? */
2564 ret = !parse->priv->discont;
2565 GST_BASE_PARSE_UNLOCK (parse);
2567 GST_DEBUG_OBJECT (parse, "sync: %d", ret);
2572 * gst_base_transform_get_drain:
2573 * @parse: the #GstBaseParse to query
2575 * Returns: TRUE if parser is currently 'draining'. That is, leftover data
2576 * (e.g. in FLUSH or EOS situation) is being parsed.
2579 gst_base_parse_get_drain (GstBaseParse * parse)
2583 g_return_val_if_fail (parse != NULL, FALSE);
2585 GST_BASE_PARSE_LOCK (parse);
2586 /* losing sync is pretty much a discont (and vice versa), no ? */
2587 ret = parse->priv->drain;
2588 GST_BASE_PARSE_UNLOCK (parse);
2590 GST_DEBUG_OBJECT (parse, "drain: %d", ret);
2595 gst_base_parse_get_duration (GstBaseParse * parse, GstFormat format,
2596 GstClockTime * duration)
2598 gboolean res = FALSE;
2600 g_return_val_if_fail (duration != NULL, FALSE);
2602 *duration = GST_CLOCK_TIME_NONE;
2603 if (parse->priv->duration != -1 && format == parse->priv->duration_fmt) {
2604 GST_LOG_OBJECT (parse, "using provided duration");
2605 *duration = parse->priv->duration;
2607 } else if (parse->priv->duration != -1) {
2608 GST_LOG_OBJECT (parse, "converting provided duration");
2609 res = gst_base_parse_convert (parse, parse->priv->duration_fmt,
2610 parse->priv->duration, format, (gint64 *) duration);
2611 } else if (format == GST_FORMAT_TIME && parse->priv->estimated_duration != -1) {
2612 GST_LOG_OBJECT (parse, "using estimated duration");
2613 *duration = parse->priv->estimated_duration;
2617 GST_LOG_OBJECT (parse, "res: %d, duration %" GST_TIME_FORMAT, res,
2618 GST_TIME_ARGS (*duration));
2623 * gst_base_parse_get_querytypes:
2626 * Returns: A table of #GstQueryType items describing supported query types.
2628 static const GstQueryType *
2629 gst_base_parse_get_querytypes (GstPad * pad)
2631 static const GstQueryType list[] = {
2645 * gst_base_parse_query:
2647 * @query: #GstQuery.
2649 * Returns: TRUE on success.
2652 gst_base_parse_query (GstPad * pad, GstQuery * query)
2654 GstBaseParse *parse;
2655 GstBaseParseClass *klass;
2656 gboolean res = FALSE;
2658 parse = GST_BASE_PARSE (GST_PAD_PARENT (pad));
2659 klass = GST_BASE_PARSE_GET_CLASS (parse);
2661 GST_LOG_OBJECT (parse, "handling query: %" GST_PTR_FORMAT, query);
2663 switch (GST_QUERY_TYPE (query)) {
2664 case GST_QUERY_POSITION:
2669 GST_DEBUG_OBJECT (parse, "position query");
2670 gst_query_parse_position (query, &format, NULL);
2672 g_mutex_lock (parse->parse_lock);
2673 if (format == GST_FORMAT_BYTES) {
2674 dest_value = parse->priv->offset;
2676 } else if (format == parse->segment.format &&
2677 GST_CLOCK_TIME_IS_VALID (parse->segment.last_stop)) {
2678 dest_value = parse->segment.last_stop;
2681 g_mutex_unlock (parse->parse_lock);
2684 gst_query_set_position (query, format, dest_value);
2686 res = gst_pad_query_default (pad, query);
2688 /* no precise result, upstream no idea either, then best estimate */
2689 /* priv->offset is updated in both PUSH/PULL modes */
2690 g_mutex_lock (parse->parse_lock);
2691 res = gst_base_parse_convert (parse,
2692 GST_FORMAT_BYTES, parse->priv->offset, format, &dest_value);
2693 g_mutex_unlock (parse->parse_lock);
2698 case GST_QUERY_DURATION:
2701 GstClockTime duration;
2703 GST_DEBUG_OBJECT (parse, "duration query");
2704 gst_query_parse_duration (query, &format, NULL);
2706 /* consult upstream */
2707 res = gst_pad_query_default (pad, query);
2709 /* otherwise best estimate from us */
2711 g_mutex_lock (parse->parse_lock);
2712 res = gst_base_parse_get_duration (parse, format, &duration);
2713 g_mutex_unlock (parse->parse_lock);
2715 gst_query_set_duration (query, format, duration);
2719 case GST_QUERY_SEEKING:
2722 GstClockTime duration = GST_CLOCK_TIME_NONE;
2723 gboolean seekable = FALSE;
2725 GST_DEBUG_OBJECT (parse, "seeking query");
2726 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
2728 /* consult upstream */
2729 res = gst_pad_query_default (pad, query);
2731 /* we may be able to help if in TIME */
2732 if (fmt == GST_FORMAT_TIME &&
2733 parse->priv->seekable > GST_BASE_PARSE_SEEK_NONE) {
2734 gst_query_parse_seeking (query, &fmt, &seekable, NULL, NULL);
2735 /* already OK if upstream takes care */
2736 GST_LOG_OBJECT (parse, "upstream handled %d, seekable %d",
2738 if (!(res && seekable)) {
2739 if (!gst_base_parse_get_duration (parse, GST_FORMAT_TIME, &duration)
2740 || duration == -1) {
2743 seekable = parse->priv->upstream_seekable;
2744 GST_LOG_OBJECT (parse, "already determine upstream seekabled: %d",
2747 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable, 0, duration);
2753 case GST_QUERY_FORMATS:
2754 gst_query_set_formatsv (query, 3, fmtlist);
2757 case GST_QUERY_CONVERT:
2759 GstFormat src_format, dest_format;
2760 gint64 src_value, dest_value;
2762 gst_query_parse_convert (query, &src_format, &src_value,
2763 &dest_format, &dest_value);
2765 res = gst_base_parse_convert (parse, src_format, src_value,
2766 dest_format, &dest_value);
2768 gst_query_set_convert (query, src_format, src_value,
2769 dest_format, dest_value);
2774 res = gst_pad_query_default (pad, query);
2781 gst_base_parse_find_offset (GstBaseParse * parse, GstClockTime time,
2782 gboolean before, GstClockTime * _ts)
2784 gint64 bytes = 0, ts = 0;
2785 GstIndexEntry *entry = NULL;
2787 if (time == GST_CLOCK_TIME_NONE) {
2793 GST_OBJECT_LOCK (parse);
2794 if (parse->priv->index) {
2795 /* Let's check if we have an index entry for that time */
2796 entry = gst_index_get_assoc_entry (parse->priv->index,
2797 parse->priv->index_id,
2798 before ? GST_INDEX_LOOKUP_BEFORE : GST_INDEX_LOOKUP_AFTER,
2799 GST_ASSOCIATION_FLAG_KEY_UNIT, GST_FORMAT_TIME, time);
2803 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &bytes);
2804 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &ts);
2806 GST_DEBUG_OBJECT (parse, "found index entry for %" GST_TIME_FORMAT
2807 " at %" GST_TIME_FORMAT ", offset %" G_GINT64_FORMAT,
2808 GST_TIME_ARGS (time), GST_TIME_ARGS (ts), bytes);
2810 GST_DEBUG_OBJECT (parse, "no index entry found for %" GST_TIME_FORMAT,
2811 GST_TIME_ARGS (time));
2814 ts = GST_CLOCK_TIME_NONE;
2817 GST_OBJECT_UNLOCK (parse);
2828 * gst_base_parse_handle_seek:
2829 * @parse: #GstBaseParse.
2830 * @event: #GstEvent.
2832 * Returns: TRUE if seek succeeded.
2835 gst_base_parse_handle_seek (GstBaseParse * parse, GstEvent * event)
2837 GstBaseParseClass *klass;
2841 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
2842 gboolean flush, update, res = TRUE, accurate;
2843 gint64 cur, stop, seekpos, seekstop;
2844 GstSegment seeksegment = { 0, };
2845 GstFormat dstformat;
2846 GstClockTime start_ts;
2848 klass = GST_BASE_PARSE_GET_CLASS (parse);
2850 gst_event_parse_seek (event, &rate, &format, &flags,
2851 &cur_type, &cur, &stop_type, &stop);
2853 GST_DEBUG_OBJECT (parse, "seek to format %s, rate %f, "
2854 "start type %d at %" GST_TIME_FORMAT ", end type %d at %"
2855 GST_TIME_FORMAT, gst_format_get_name (format), rate,
2856 cur_type, GST_TIME_ARGS (cur), stop_type, GST_TIME_ARGS (stop));
2858 /* no negative rates yet */
2859 if (rate < 0.0 && parse->priv->pad_mode == GST_ACTIVATE_PUSH)
2862 if (cur_type != GST_SEEK_TYPE_SET ||
2863 (stop_type != GST_SEEK_TYPE_SET && stop_type != GST_SEEK_TYPE_NONE))
2866 /* For any format other than TIME, see if upstream handles
2867 * it directly or fail. For TIME, try upstream, but do it ourselves if
2868 * it fails upstream */
2869 if (format != GST_FORMAT_TIME) {
2870 /* default action delegates to upstream */
2873 gst_event_ref (event);
2874 if (gst_pad_push_event (parse->sinkpad, event)) {
2879 /* get flush flag */
2880 flush = flags & GST_SEEK_FLAG_FLUSH;
2882 /* copy segment, we need this because we still need the old
2883 * segment when we close the current segment. */
2884 memcpy (&seeksegment, &parse->segment, sizeof (GstSegment));
2886 GST_DEBUG_OBJECT (parse, "configuring seek");
2887 gst_segment_set_seek (&seeksegment, rate, format, flags,
2888 cur_type, cur, stop_type, stop, &update);
2890 /* accurate seeking implies seek tables are used to obtain position,
2891 * and the requested segment is maintained exactly, not adjusted any way */
2892 accurate = flags & GST_SEEK_FLAG_ACCURATE;
2894 /* maybe we can be accurate for (almost) free */
2895 if (seeksegment.last_stop <= parse->priv->index_last_ts + 20 * GST_SECOND) {
2896 GST_DEBUG_OBJECT (parse,
2897 "seek position %" GST_TIME_FORMAT " <= %" GST_TIME_FORMAT
2898 "accurate seek possible", GST_TIME_ARGS (seeksegment.last_stop),
2899 GST_TIME_ARGS (parse->priv->index_last_ts));
2903 GstClockTime startpos = seeksegment.last_stop;
2905 /* accurate requested, so ... seek a bit before target */
2906 if (startpos < parse->priv->lead_in_ts)
2909 startpos -= parse->priv->lead_in_ts;
2910 seekpos = gst_base_parse_find_offset (parse, startpos, TRUE, &start_ts);
2911 seekstop = gst_base_parse_find_offset (parse, seeksegment.stop, FALSE,
2914 start_ts = seeksegment.last_stop;
2915 dstformat = GST_FORMAT_BYTES;
2916 if (!gst_pad_query_convert (parse->srcpad, format, seeksegment.last_stop,
2917 &dstformat, &seekpos))
2918 goto convert_failed;
2919 if (!gst_pad_query_convert (parse->srcpad, format, seeksegment.stop,
2920 &dstformat, &seekstop))
2921 goto convert_failed;
2924 GST_DEBUG_OBJECT (parse,
2925 "seek position %" G_GINT64_FORMAT " in bytes: %" G_GINT64_FORMAT,
2927 GST_DEBUG_OBJECT (parse,
2928 "seek stop %" G_GINT64_FORMAT " in bytes: %" G_GINT64_FORMAT,
2929 seeksegment.stop, seekstop);
2931 if (parse->priv->pad_mode == GST_ACTIVATE_PULL) {
2934 GST_DEBUG_OBJECT (parse, "seek in PULL mode");
2937 if (parse->srcpad) {
2938 GST_DEBUG_OBJECT (parse, "sending flush start");
2939 gst_pad_push_event (parse->srcpad, gst_event_new_flush_start ());
2942 gst_pad_pause_task (parse->sinkpad);
2945 /* we should now be able to grab the streaming thread because we stopped it
2946 * with the above flush/pause code */
2947 GST_PAD_STREAM_LOCK (parse->sinkpad);
2949 /* save current position */
2950 last_stop = parse->segment.last_stop;
2951 GST_DEBUG_OBJECT (parse, "stopped streaming at %" G_GINT64_FORMAT,
2954 /* now commit to new position */
2956 /* prepare for streaming again */
2958 GST_DEBUG_OBJECT (parse, "sending flush stop");
2959 gst_pad_push_event (parse->srcpad, gst_event_new_flush_stop ());
2960 gst_base_parse_clear_queues (parse);
2962 if (parse->close_segment)
2963 gst_event_unref (parse->close_segment);
2965 parse->close_segment = gst_event_new_new_segment (TRUE,
2966 parse->segment.rate, parse->segment.format,
2967 parse->segment.accum, parse->segment.last_stop, parse->segment.accum);
2969 /* keep track of our last_stop */
2970 seeksegment.accum = parse->segment.last_stop;
2972 GST_DEBUG_OBJECT (parse, "Created close seg format %d, "
2973 "start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT
2974 ", pos = %" GST_TIME_FORMAT, format,
2975 GST_TIME_ARGS (parse->segment.accum),
2976 GST_TIME_ARGS (parse->segment.last_stop),
2977 GST_TIME_ARGS (parse->segment.accum));
2980 memcpy (&parse->segment, &seeksegment, sizeof (GstSegment));
2982 /* store the newsegment event so it can be sent from the streaming thread. */
2983 if (parse->pending_segment)
2984 gst_event_unref (parse->pending_segment);
2986 /* This will be sent later in _loop() */
2987 parse->pending_segment =
2988 gst_event_new_new_segment (FALSE, parse->segment.rate,
2989 parse->segment.format, parse->segment.start, parse->segment.stop,
2990 parse->segment.start);
2992 GST_DEBUG_OBJECT (parse, "Created newseg format %d, "
2993 "start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT
2994 ", pos = %" GST_TIME_FORMAT, format,
2995 GST_TIME_ARGS (parse->segment.start),
2996 GST_TIME_ARGS (parse->segment.stop),
2997 GST_TIME_ARGS (parse->segment.start));
2999 /* mark discont if we are going to stream from another position. */
3000 if (seekpos != parse->priv->offset) {
3001 GST_DEBUG_OBJECT (parse,
3002 "mark DISCONT, we did a seek to another position");
3003 parse->priv->offset = seekpos;
3004 parse->priv->last_offset = seekpos;
3005 parse->priv->discont = TRUE;
3006 parse->priv->next_ts = start_ts;
3007 parse->priv->last_ts = GST_CLOCK_TIME_NONE;
3008 parse->priv->sync_offset = seekpos;
3009 parse->priv->exact_position = accurate;
3012 /* Start streaming thread if paused */
3013 gst_pad_start_task (parse->sinkpad,
3014 (GstTaskFunction) gst_base_parse_loop, parse->sinkpad);
3016 GST_PAD_STREAM_UNLOCK (parse->sinkpad);
3018 GstEvent *new_event;
3019 GstBaseParseSeek *seek;
3021 /* The only thing we need to do in PUSH-mode is to send the
3022 seek event (in bytes) to upstream. Segment / flush handling happens
3023 in corresponding src event handlers */
3024 GST_DEBUG_OBJECT (parse, "seek in PUSH mode");
3025 new_event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flush,
3026 GST_SEEK_TYPE_SET, seekpos, stop_type, seekstop);
3028 /* store segment info so its precise details can be reconstructed when
3029 * receiving newsegment;
3030 * this matters for all details when accurate seeking,
3031 * is most useful to preserve NONE stop time otherwise */
3032 seek = g_new0 (GstBaseParseSeek, 1);
3033 seek->segment = seeksegment;
3034 seek->accurate = accurate;
3035 seek->offset = seekpos;
3036 seek->start_ts = start_ts;
3037 GST_OBJECT_LOCK (parse);
3038 /* less optimal, but preserves order */
3039 parse->priv->pending_seeks =
3040 g_slist_append (parse->priv->pending_seeks, seek);
3041 GST_OBJECT_UNLOCK (parse);
3043 res = gst_pad_push_event (parse->sinkpad, new_event);
3046 GST_OBJECT_LOCK (parse);
3047 parse->priv->pending_seeks =
3048 g_slist_remove (parse->priv->pending_seeks, seek);
3049 GST_OBJECT_UNLOCK (parse);
3060 GST_DEBUG_OBJECT (parse, "negative playback rates are not supported yet.");
3066 GST_DEBUG_OBJECT (parse, "unsupported seek type.");
3072 GST_DEBUG_OBJECT (parse, "conversion TIME to BYTES failed.");
3079 * gst_base_parse_handle_tag:
3080 * @parse: #GstBaseParse.
3081 * @event: #GstEvent.
3083 * Checks if bitrates are available from upstream tags so that we don't
3084 * override them later
3087 gst_base_parse_handle_tag (GstBaseParse * parse, GstEvent * event)
3089 GstTagList *taglist = NULL;
3092 gst_event_parse_tag (event, &taglist);
3094 if (gst_tag_list_get_uint (taglist, GST_TAG_MINIMUM_BITRATE, &tmp)) {
3095 GST_DEBUG_OBJECT (parse, "upstream min bitrate %d", tmp);
3096 parse->priv->post_min_bitrate = FALSE;
3098 if (gst_tag_list_get_uint (taglist, GST_TAG_BITRATE, &tmp)) {
3099 GST_DEBUG_OBJECT (parse, "upstream avg bitrate %d", tmp);
3100 parse->priv->post_avg_bitrate = FALSE;
3102 if (gst_tag_list_get_uint (taglist, GST_TAG_MAXIMUM_BITRATE, &tmp)) {
3103 GST_DEBUG_OBJECT (parse, "upstream max bitrate %d", tmp);
3104 parse->priv->post_max_bitrate = FALSE;
3109 * gst_base_parse_sink_setcaps:
3113 * Returns: TRUE if caps were accepted.
3116 gst_base_parse_sink_setcaps (GstPad * pad, GstCaps * caps)
3118 GstBaseParse *parse;
3119 GstBaseParseClass *klass;
3120 gboolean res = TRUE;
3122 parse = GST_BASE_PARSE (GST_PAD_PARENT (pad));
3123 klass = GST_BASE_PARSE_GET_CLASS (parse);
3125 GST_DEBUG_OBJECT (parse, "caps: %" GST_PTR_FORMAT, caps);
3127 if (klass->set_sink_caps)
3128 res = klass->set_sink_caps (parse, caps);
3130 return res && gst_pad_set_caps (pad, caps);
3134 gst_base_parse_set_index (GstElement * element, GstIndex * index)
3136 GstBaseParse *parse = GST_BASE_PARSE (element);
3138 GST_OBJECT_LOCK (parse);
3139 if (parse->priv->index)
3140 gst_object_unref (parse->priv->index);
3142 parse->priv->index = gst_object_ref (index);
3143 gst_index_get_writer_id (index, GST_OBJECT (element),
3144 &parse->priv->index_id);
3145 parse->priv->own_index = FALSE;
3147 parse->priv->index = NULL;
3148 GST_OBJECT_UNLOCK (parse);
3152 gst_base_parse_get_index (GstElement * element)
3154 GstBaseParse *parse = GST_BASE_PARSE (element);
3155 GstIndex *result = NULL;
3157 GST_OBJECT_LOCK (parse);
3158 if (parse->priv->index)
3159 result = gst_object_ref (parse->priv->index);
3160 GST_OBJECT_UNLOCK (parse);
3165 static GstStateChangeReturn
3166 gst_base_parse_change_state (GstElement * element, GstStateChange transition)
3168 GstBaseParse *parse;
3169 GstStateChangeReturn result;
3171 parse = GST_BASE_PARSE (element);
3173 switch (transition) {
3174 case GST_STATE_CHANGE_READY_TO_PAUSED:
3175 /* If this is our own index destroy it as the
3176 * old entries might be wrong for the new stream */
3177 if (parse->priv->own_index) {
3178 gst_object_unref (parse->priv->index);
3179 parse->priv->index = NULL;
3180 parse->priv->own_index = FALSE;
3183 /* If no index was created, generate one */
3184 if (G_UNLIKELY (!parse->priv->index)) {
3185 GST_DEBUG_OBJECT (parse, "no index provided creating our own");
3187 parse->priv->index = gst_index_factory_make ("memindex");
3188 gst_index_get_writer_id (parse->priv->index, GST_OBJECT (parse),
3189 &parse->priv->index_id);
3190 parse->priv->own_index = TRUE;
3197 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3199 switch (transition) {
3200 case GST_STATE_CHANGE_PAUSED_TO_READY:
3201 gst_base_parse_reset (parse);