2 * Copyright (C) 2008 Nokia Corporation. All rights reserved.
3 * Contact: Stefan Kost <stefan.kost@nokia.com>
4 * Copyright (C) 2008 Sebastian Dröge <sebastian.droege@collabora.co.uk>.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * SECTION:gstbaseparse
24 * @short_description: Base class for stream parsers
25 * @see_also: #GstBaseTransform
27 * This base class is for parser elements that process data and splits it
28 * into separate audio/video/whatever frames.
32 * <listitem><para>One sinkpad and one srcpad</para></listitem>
33 * <listitem><para>Handles state changes</para></listitem>
34 * <listitem><para>Does flushing</para></listitem>
35 * <listitem><para>Push mode</para></listitem>
36 * <listitem><para>Pull mode</para></listitem>
37 * <listitem><para>Handles events (NEWSEGMENT/EOS/FLUSH)</para></listitem>
38 * <listitem><para>Handles seeking in both modes</para></listitem>
40 * Handles POSITION/DURATION/SEEKING/FORMAT/CONVERT queries
44 * The purpose of this base class is to provide a basic functionality of
45 * a parser and share a lot of rather complex code.
47 * Description of the parsing mechanism:
50 * <itemizedlist><title>Set-up phase</title>
52 * GstBaseParse class calls @set_sink_caps to inform the subclass about
53 * incoming sinkpad caps. Subclass should set the srcpad caps accordingly.
56 * GstBaseParse calls @start to inform subclass that data processing is
60 * At least in this point subclass needs to tell the GstBaseParse class
61 * how big data chunks it wants to receive (min_frame_size). It can do
62 * this with @gst_base_parse_set_min_frame_size.
65 * GstBaseParse class sets up appropriate data passing mode (pull/push)
66 * and starts to process the data.
72 * <title>Parsing phase</title>
74 * GstBaseParse gathers at least min_frame_size bytes of data either
75 * by pulling it from upstream or collecting buffers into internal
79 * A buffer of (at least) min_frame_size bytes is passed to subclass with
80 * @check_valid_frame. Subclass checks the contents and returns TRUE
81 * if the buffer contains a valid frame. It also needs to set the
82 * @framesize according to the detected frame size. If buffer didn't
83 * contain a valid frame, this call must return FALSE and optionally
84 * set the @skipsize value to inform base class that how many bytes
85 * it needs to skip in order to find a valid frame. @framesize can always
86 * indicate a new minimum for current frame parsing. The passed buffer
87 * is read-only. Note that @check_valid_frame might receive any small
88 * amount of input data when leftover data is being drained (e.g. at EOS).
91 * After valid frame is found, it will be passed again to subclass with
92 * @parse_frame call. Now subclass is responsible for parsing the
93 * frame contents and setting the caps, and buffer metadata (e.g.
94 * buffer timestamp and duration, or keyframe if applicable).
95 * (although the latter can also be done by GstBaseParse if it is
96 * appropriately configured, see below). Frame is provided with
97 * timestamp derived from upstream (as much as generally possible),
98 * duration obtained form configuration (see below), and offset
99 * if meaningful (in pull mode).
102 * Finally the buffer can be pushed downstream and parsing loop starts
103 * over again. Just prior to actually pushing the buffer in question,
104 * it is passed to @pre_push_buffer which gives subclass yet one
105 * last chance to examine buffer metadata, or to send some custom (tag)
106 * events, or to perform custom (segment) filtering.
109 * During the parsing process GstBaseParseClass will handle both srcpad and
110 * sinkpad events. They will be passed to subclass if @event or
111 * @src_event callbacks have been provided.
116 * <itemizedlist><title>Shutdown phase</title>
118 * GstBaseParse class calls @stop to inform the subclass that data
119 * parsing will be stopped.
125 * Subclass is responsible for providing pad template caps for
126 * source and sink pads. The pads need to be named "sink" and "src". It also
127 * needs to set the fixed caps on srcpad, when the format is ensured (e.g.
128 * when base class calls subclass' @set_sink_caps function).
130 * This base class uses GST_FORMAT_DEFAULT as a meaning of frames. So,
131 * subclass conversion routine needs to know that conversion from
132 * GST_FORMAT_TIME to GST_FORMAT_DEFAULT must return the
133 * frame number that can be found from the given byte position.
135 * GstBaseParse uses subclasses conversion methods also for seeking (or otherwise
136 * uses its own default one, see also below).
138 * Subclass @start and @stop functions will be called to inform the beginning
139 * and end of data processing.
141 * Things that subclass need to take care of:
143 * <listitem><para>Provide pad templates</para></listitem>
145 * Fixate the source pad caps when appropriate
148 * Inform base class how big data chunks should be retrieved. This is
149 * done with @gst_base_parse_set_min_frame_size function.
152 * Examine data chunks passed to subclass with @check_valid_frame
153 * and tell if they contain a valid frame
156 * Set the caps and timestamp to frame that is passed to subclass with
157 * @parse_frame function.
159 * <listitem><para>Provide conversion functions</para></listitem>
161 * Update the duration information with @gst_base_parse_set_duration
164 * Optionally passthrough using @gst_base_parse_set_format
167 * Configure various baseparse parameters using @gst_base_parse_set_seek and
168 * @gst_base_parse_set_frame_props.
171 * In particular, if subclass is unable to determine a duration, but
172 * parsing (or specs) yields a frames per seconds rate, then this can be
173 * provided to GstBaseParse to enable it to cater for
174 * buffer time metadata (which will be taken from upstream as much as possible).
175 * Internally keeping track of frame durations and respective
176 * sizes that have been pushed provides GstBaseParse with an estimated bitrate.
177 * A default @convert (used if not overriden) will then use these
178 * rates to perform obvious conversions. These rates are also used to update
179 * (estimated) duration at regular frame intervals.
186 * - In push mode provide a queue of adapter-"queued" buffers for upstream
188 * - Queue buffers/events until caps are set
198 #include <gst/base/gstadapter.h>
200 #include "gstbaseparse.h"
202 #define MIN_FRAMES_TO_POST_BITRATE 10
203 #define TARGET_DIFFERENCE (20 * GST_SECOND)
205 GST_DEBUG_CATEGORY_STATIC (gst_base_parse_debug);
206 #define GST_CAT_DEFAULT gst_base_parse_debug
208 /* Supported formats */
209 static GstFormat fmtlist[] = {
216 #define GST_BASE_PARSE_GET_PRIVATE(obj) \
217 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_PARSE, GstBaseParsePrivate))
219 struct _GstBaseParsePrivate
221 GstActivateMode pad_mode;
226 GstFormat duration_fmt;
227 gint64 estimated_duration;
229 guint min_frame_size;
230 GstBaseParseFormatFlags format_flags;
231 guint fps_num, fps_den;
232 gint update_interval;
234 guint lead_in, lead_out;
235 GstClockTime lead_in_ts, lead_out_ts;
243 GstClockTime next_ts;
244 GstClockTime prev_ts;
245 GstClockTime frame_duration;
246 gboolean seen_keyframe;
251 guint64 data_bytecount;
252 guint64 acc_duration;
253 GstClockTime first_frame_ts;
254 gint64 first_frame_offset;
256 gboolean post_min_bitrate;
257 gboolean post_avg_bitrate;
258 gboolean post_max_bitrate;
262 guint posted_avg_bitrate;
264 GList *pending_events;
266 /* frames/buffers that are queued and ready to go on OK */
267 GList *queued_frames;
271 /* index entry storage, either ours or provided */
275 /* seek table entries only maintained if upstream is BYTE seekable */
276 gboolean upstream_seekable;
277 gboolean upstream_has_duration;
278 gint64 upstream_size;
279 /* minimum distance between two index entries */
280 GstClockTimeDiff idx_interval;
281 /* ts and offset of last entry added */
282 GstClockTime index_last_ts;
283 gint64 index_last_offset;
284 gboolean index_last_valid;
286 /* timestamps currently produced are accurate, e.g. started from 0 onwards */
287 gboolean exact_position;
288 /* seek events are temporarily kept to match them with newsegments */
289 GSList *pending_seeks;
291 /* reverse playback */
292 GSList *buffers_pending;
293 GSList *buffers_queued;
294 GSList *buffers_send;
295 GstClockTime last_ts;
298 /* Newsegment event to be sent after SEEK */
299 GstEvent *pending_segment;
301 /* Segment event that closes the running segment prior to SEEK */
302 GstEvent *close_segment;
305 typedef struct _GstBaseParseSeek
310 GstClockTime start_ts;
313 #define GST_BASE_PARSE_PASSTHROUGH(parse) \
314 (parse->priv->format_flags & GST_BASE_PARSE_FORMAT_FLAG_PASSTHROUGH)
315 #define GST_BASE_PARSE_HAS_TIME(parse) \
316 (parse->priv->format_flags & GST_BASE_PARSE_FORMAT_FLAG_HAS_TIME)
319 static GstElementClass *parent_class = NULL;
321 static void gst_base_parse_class_init (GstBaseParseClass * klass);
322 static void gst_base_parse_init (GstBaseParse * parse,
323 GstBaseParseClass * klass);
326 gst_base_parse_get_type (void)
328 static volatile gsize base_parse_type = 0;
330 if (g_once_init_enter (&base_parse_type)) {
331 static const GTypeInfo base_parse_info = {
332 sizeof (GstBaseParseClass),
333 (GBaseInitFunc) NULL,
334 (GBaseFinalizeFunc) NULL,
335 (GClassInitFunc) gst_base_parse_class_init,
338 sizeof (GstBaseParse),
340 (GInstanceInitFunc) gst_base_parse_init,
344 _type = g_type_register_static (GST_TYPE_ELEMENT,
345 "GstBaseParse", &base_parse_info, G_TYPE_FLAG_ABSTRACT);
346 g_once_init_leave (&base_parse_type, _type);
348 return (GType) base_parse_type;
351 static void gst_base_parse_finalize (GObject * object);
353 static GstStateChangeReturn gst_base_parse_change_state (GstElement * element,
354 GstStateChange transition);
355 static void gst_base_parse_reset (GstBaseParse * parse);
357 static void gst_base_parse_set_index (GstElement * element, GstIndex * index);
358 static GstIndex *gst_base_parse_get_index (GstElement * element);
360 static gboolean gst_base_parse_sink_activate (GstPad * sinkpad);
361 static gboolean gst_base_parse_sink_activate_push (GstPad * pad,
363 static gboolean gst_base_parse_sink_activate_pull (GstPad * pad,
365 static gboolean gst_base_parse_handle_seek (GstBaseParse * parse,
367 static void gst_base_parse_handle_tag (GstBaseParse * parse, GstEvent * event);
369 static gboolean gst_base_parse_src_event (GstPad * pad, GstEvent * event);
370 static gboolean gst_base_parse_sink_event (GstPad * pad, GstEvent * event);
371 static gboolean gst_base_parse_query (GstPad * pad, GstQuery * query);
372 static gboolean gst_base_parse_sink_setcaps (GstPad * pad, GstCaps * caps);
373 static const GstQueryType *gst_base_parse_get_querytypes (GstPad * pad);
375 static GstFlowReturn gst_base_parse_chain (GstPad * pad, GstBuffer * buffer);
376 static void gst_base_parse_loop (GstPad * pad);
378 static gboolean gst_base_parse_check_frame (GstBaseParse * parse,
379 GstBaseParseFrame * frame, guint * framesize, gint * skipsize);
380 static GstFlowReturn gst_base_parse_parse_frame (GstBaseParse * parse,
381 GstBaseParseFrame * frame);
383 static gboolean gst_base_parse_sink_eventfunc (GstBaseParse * parse,
386 static gboolean gst_base_parse_src_eventfunc (GstBaseParse * parse,
389 static void gst_base_parse_drain (GstBaseParse * parse);
391 static void gst_base_parse_post_bitrates (GstBaseParse * parse,
392 gboolean post_min, gboolean post_avg, gboolean post_max);
394 static gint64 gst_base_parse_find_offset (GstBaseParse * parse,
395 GstClockTime time, gboolean before, GstClockTime * _ts);
396 static GstFlowReturn gst_base_parse_locate_time (GstBaseParse * parse,
397 GstClockTime * _time, gint64 * _offset);
399 static GstFlowReturn gst_base_parse_process_fragment (GstBaseParse * parse,
402 static gboolean gst_base_parse_is_seekable (GstBaseParse * parse);
404 static void gst_base_parse_push_pending_frame (GstBaseParseFrame * frame,
405 GstBaseParse * parse);
406 static void gst_base_parse_frame_free (GstBaseParseFrame * frame);
407 static GstBaseParseFrame *gst_base_parse_frame_copy_and_clear (GstBaseParseFrame
411 gst_base_parse_clear_queues (GstBaseParse * parse)
413 g_slist_foreach (parse->priv->buffers_queued, (GFunc) gst_buffer_unref, NULL);
414 g_slist_free (parse->priv->buffers_queued);
415 parse->priv->buffers_queued = NULL;
416 g_slist_foreach (parse->priv->buffers_pending, (GFunc) gst_buffer_unref,
418 g_slist_free (parse->priv->buffers_pending);
419 parse->priv->buffers_pending = NULL;
420 g_slist_foreach (parse->priv->buffers_send, (GFunc) gst_buffer_unref, NULL);
421 g_slist_free (parse->priv->buffers_send);
422 parse->priv->buffers_send = NULL;
426 gst_base_parse_finalize (GObject * object)
428 GstBaseParse *parse = GST_BASE_PARSE (object);
431 g_object_unref (parse->priv->adapter);
433 if (parse->priv->pending_segment) {
434 p_ev = &parse->priv->pending_segment;
435 gst_event_replace (p_ev, NULL);
437 if (parse->priv->close_segment) {
438 p_ev = &parse->priv->close_segment;
439 gst_event_replace (p_ev, NULL);
442 if (parse->priv->cache) {
443 gst_buffer_unref (parse->priv->cache);
444 parse->priv->cache = NULL;
447 g_list_foreach (parse->priv->pending_events, (GFunc) gst_mini_object_unref,
449 g_list_free (parse->priv->pending_events);
450 parse->priv->pending_events = NULL;
452 g_list_foreach (parse->priv->queued_frames,
453 (GFunc) gst_base_parse_frame_free, NULL);
454 g_list_free (parse->priv->queued_frames);
455 parse->priv->queued_frames = NULL;
457 if (parse->priv->index) {
458 gst_object_unref (parse->priv->index);
459 parse->priv->index = NULL;
462 gst_base_parse_clear_queues (parse);
464 G_OBJECT_CLASS (parent_class)->finalize (object);
468 gst_base_parse_class_init (GstBaseParseClass * klass)
470 GObjectClass *gobject_class;
471 GstElementClass *gstelement_class;
473 gobject_class = G_OBJECT_CLASS (klass);
474 g_type_class_add_private (klass, sizeof (GstBaseParsePrivate));
475 parent_class = g_type_class_peek_parent (klass);
476 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_parse_finalize);
478 gstelement_class = (GstElementClass *) klass;
479 gstelement_class->change_state =
480 GST_DEBUG_FUNCPTR (gst_base_parse_change_state);
481 gstelement_class->set_index = GST_DEBUG_FUNCPTR (gst_base_parse_set_index);
482 gstelement_class->get_index = GST_DEBUG_FUNCPTR (gst_base_parse_get_index);
484 /* Default handlers */
485 klass->check_valid_frame = gst_base_parse_check_frame;
486 klass->parse_frame = gst_base_parse_parse_frame;
487 klass->src_event = gst_base_parse_src_eventfunc;
488 klass->convert = gst_base_parse_convert_default;
490 GST_DEBUG_CATEGORY_INIT (gst_base_parse_debug, "baseparse", 0,
491 "baseparse element");
495 gst_base_parse_init (GstBaseParse * parse, GstBaseParseClass * bclass)
497 GstPadTemplate *pad_template;
499 GST_DEBUG_OBJECT (parse, "gst_base_parse_init");
501 parse->priv = GST_BASE_PARSE_GET_PRIVATE (parse);
504 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "sink");
505 g_return_if_fail (pad_template != NULL);
506 parse->sinkpad = gst_pad_new_from_template (pad_template, "sink");
507 gst_pad_set_event_function (parse->sinkpad,
508 GST_DEBUG_FUNCPTR (gst_base_parse_sink_event));
509 gst_pad_set_setcaps_function (parse->sinkpad,
510 GST_DEBUG_FUNCPTR (gst_base_parse_sink_setcaps));
511 gst_pad_set_chain_function (parse->sinkpad,
512 GST_DEBUG_FUNCPTR (gst_base_parse_chain));
513 gst_pad_set_activate_function (parse->sinkpad,
514 GST_DEBUG_FUNCPTR (gst_base_parse_sink_activate));
515 gst_pad_set_activatepush_function (parse->sinkpad,
516 GST_DEBUG_FUNCPTR (gst_base_parse_sink_activate_push));
517 gst_pad_set_activatepull_function (parse->sinkpad,
518 GST_DEBUG_FUNCPTR (gst_base_parse_sink_activate_pull));
519 gst_element_add_pad (GST_ELEMENT (parse), parse->sinkpad);
521 GST_DEBUG_OBJECT (parse, "sinkpad created");
524 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
525 g_return_if_fail (pad_template != NULL);
526 parse->srcpad = gst_pad_new_from_template (pad_template, "src");
527 gst_pad_set_event_function (parse->srcpad,
528 GST_DEBUG_FUNCPTR (gst_base_parse_src_event));
529 gst_pad_set_query_type_function (parse->srcpad,
530 GST_DEBUG_FUNCPTR (gst_base_parse_get_querytypes));
531 gst_pad_set_query_function (parse->srcpad,
532 GST_DEBUG_FUNCPTR (gst_base_parse_query));
533 gst_pad_use_fixed_caps (parse->srcpad);
534 gst_element_add_pad (GST_ELEMENT (parse), parse->srcpad);
535 GST_DEBUG_OBJECT (parse, "src created");
537 parse->priv->adapter = gst_adapter_new ();
539 parse->priv->pad_mode = GST_ACTIVATE_NONE;
542 gst_base_parse_reset (parse);
543 GST_DEBUG_OBJECT (parse, "init ok");
547 * gst_base_parse_frame_init:
548 * @parse: #GstBaseParse.
549 * @frame: #GstBaseParseFrame.
551 * Sets a #GstBaseParseFrame to initial state. Currently this means
552 * all fields are zero-ed.
557 gst_base_parse_frame_init (GstBaseParse * parse, GstBaseParseFrame * frame)
559 memset (frame, 0, sizeof (*frame));
562 /* clear == frame no longer to be used following this */
564 gst_base_parse_frame_clear (GstBaseParse * parse, GstBaseParseFrame * frame)
566 /* limited for now */
568 gst_buffer_unref (frame->buffer);
569 frame->buffer = NULL;
573 /* free frame allocated with copy_and_clear (and not on the stack) */
575 gst_base_parse_frame_free (GstBaseParseFrame * frame)
577 gst_base_parse_frame_clear (NULL, frame);
578 g_slice_free (GstBaseParseFrame, frame);
581 /* copy frame (taking ownership of contents of passed frame) */
582 static GstBaseParseFrame *
583 gst_base_parse_frame_copy_and_clear (GstBaseParseFrame * frame)
585 GstBaseParseFrame *copy;
587 copy = g_slice_dup (GstBaseParseFrame, frame);
588 memset (frame, 0, sizeof (GstBaseParseFrame));
593 gst_base_parse_frame_update (GstBaseParse * parse, GstBaseParseFrame * frame,
596 gst_buffer_replace (&frame->buffer, buf);
597 if (parse->priv->drain) {
598 frame->flags |= GST_BASE_PARSE_FRAME_FLAG_DRAIN;
600 frame->flags &= ~(GST_BASE_PARSE_FRAME_FLAG_DRAIN);
602 /* losing sync is pretty much a discont (and vice versa), no ? */
603 if (!parse->priv->discont) {
604 frame->flags |= GST_BASE_PARSE_FRAME_FLAG_SYNC;
606 frame->flags &= ~(GST_BASE_PARSE_FRAME_FLAG_SYNC);
611 gst_base_parse_reset (GstBaseParse * parse)
613 GST_OBJECT_LOCK (parse);
614 gst_segment_init (&parse->segment, GST_FORMAT_TIME);
615 parse->priv->duration = -1;
616 parse->priv->min_frame_size = 1;
617 parse->priv->discont = TRUE;
618 parse->priv->flushing = FALSE;
619 parse->priv->offset = 0;
620 parse->priv->sync_offset = 0;
621 parse->priv->update_interval = -1;
622 parse->priv->fps_num = parse->priv->fps_den = 0;
623 parse->priv->frame_duration = GST_CLOCK_TIME_NONE;
624 parse->priv->lead_in = parse->priv->lead_out = 0;
625 parse->priv->lead_in_ts = parse->priv->lead_out_ts = 0;
626 parse->priv->bitrate = 0;
627 parse->priv->framecount = 0;
628 parse->priv->bytecount = 0;
629 parse->priv->acc_duration = 0;
630 parse->priv->first_frame_ts = GST_CLOCK_TIME_NONE;
631 parse->priv->first_frame_offset = -1;
632 parse->priv->estimated_duration = -1;
633 parse->priv->next_ts = 0;
634 parse->priv->format_flags = GST_BASE_PARSE_FORMAT_FLAG_SYNCABLE;
635 parse->priv->post_min_bitrate = TRUE;
636 parse->priv->post_avg_bitrate = TRUE;
637 parse->priv->post_max_bitrate = TRUE;
638 parse->priv->min_bitrate = G_MAXUINT;
639 parse->priv->max_bitrate = 0;
640 parse->priv->avg_bitrate = 0;
641 parse->priv->posted_avg_bitrate = 0;
643 parse->priv->index_last_ts = GST_CLOCK_TIME_NONE;
644 parse->priv->index_last_offset = -1;
645 parse->priv->index_last_valid = TRUE;
646 parse->priv->upstream_seekable = FALSE;
647 parse->priv->upstream_size = 0;
648 parse->priv->upstream_has_duration = FALSE;
649 parse->priv->idx_interval = 0;
650 parse->priv->exact_position = TRUE;
651 parse->priv->seen_keyframe = FALSE;
653 parse->priv->last_ts = GST_CLOCK_TIME_NONE;
654 parse->priv->last_offset = 0;
656 if (parse->priv->pending_segment) {
657 gst_event_unref (parse->priv->pending_segment);
658 parse->priv->pending_segment = NULL;
661 g_list_foreach (parse->priv->pending_events, (GFunc) gst_mini_object_unref,
663 g_list_free (parse->priv->pending_events);
664 parse->priv->pending_events = NULL;
666 if (parse->priv->cache) {
667 gst_buffer_unref (parse->priv->cache);
668 parse->priv->cache = NULL;
671 g_slist_foreach (parse->priv->pending_seeks, (GFunc) g_free, NULL);
672 g_slist_free (parse->priv->pending_seeks);
673 parse->priv->pending_seeks = NULL;
675 GST_OBJECT_UNLOCK (parse);
678 /* gst_base_parse_check_frame:
679 * @parse: #GstBaseParse.
680 * @buffer: GstBuffer.
681 * @framesize: This will be set to tell the found frame size in bytes.
682 * @skipsize: Output parameter that tells how much data needs to be skipped
683 * in order to find the following frame header.
685 * Default callback for check_valid_frame.
687 * Returns: Always TRUE.
690 gst_base_parse_check_frame (GstBaseParse * parse,
691 GstBaseParseFrame * frame, guint * framesize, gint * skipsize)
693 *framesize = GST_BUFFER_SIZE (frame->buffer);
699 /* gst_base_parse_parse_frame:
700 * @parse: #GstBaseParse.
701 * @buffer: #GstBuffer.
703 * Default callback for parse_frame.
706 gst_base_parse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
708 GstBuffer *buffer = frame->buffer;
710 if (!GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
711 GST_CLOCK_TIME_IS_VALID (parse->priv->next_ts)) {
712 GST_BUFFER_TIMESTAMP (buffer) = parse->priv->next_ts;
714 if (!GST_BUFFER_DURATION_IS_VALID (buffer) &&
715 GST_CLOCK_TIME_IS_VALID (parse->priv->frame_duration)) {
716 GST_BUFFER_DURATION (buffer) = parse->priv->frame_duration;
721 /* gst_base_parse_convert:
722 * @parse: #GstBaseParse.
723 * @src_format: #GstFormat describing the source format.
724 * @src_value: Source value to be converted.
725 * @dest_format: #GstFormat defining the converted format.
726 * @dest_value: Pointer where the conversion result will be put.
728 * Converts using configured "convert" vmethod in #GstBaseParse class.
730 * Returns: TRUE if conversion was successful.
733 gst_base_parse_convert (GstBaseParse * parse,
734 GstFormat src_format,
735 gint64 src_value, GstFormat dest_format, gint64 * dest_value)
737 GstBaseParseClass *klass = GST_BASE_PARSE_GET_CLASS (parse);
740 g_return_val_if_fail (dest_value != NULL, FALSE);
745 ret = klass->convert (parse, src_format, src_value, dest_format, dest_value);
747 #ifndef GST_DISABLE_GST_DEBUG
750 if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES) {
751 GST_LOG_OBJECT (parse,
752 "TIME -> BYTES: %" GST_TIME_FORMAT " -> %" G_GINT64_FORMAT,
753 GST_TIME_ARGS (src_value), *dest_value);
754 } else if (dest_format == GST_FORMAT_TIME &&
755 src_format == GST_FORMAT_BYTES) {
756 GST_LOG_OBJECT (parse,
757 "BYTES -> TIME: %" G_GINT64_FORMAT " -> %" GST_TIME_FORMAT,
758 src_value, GST_TIME_ARGS (*dest_value));
760 GST_LOG_OBJECT (parse,
761 "%s -> %s: %" G_GINT64_FORMAT " -> %" G_GINT64_FORMAT,
762 GST_STR_NULL (gst_format_get_name (src_format)),
763 GST_STR_NULL (gst_format_get_name (dest_format)),
764 src_value, *dest_value);
767 GST_DEBUG_OBJECT (parse, "conversion failed");
775 /* gst_base_parse_sink_event:
776 * @pad: #GstPad that received the event.
777 * @event: #GstEvent to be handled.
779 * Handler for sink pad events.
781 * Returns: TRUE if the event was handled.
784 gst_base_parse_sink_event (GstPad * pad, GstEvent * event)
787 GstBaseParseClass *bclass;
788 gboolean handled = FALSE;
791 parse = GST_BASE_PARSE (gst_pad_get_parent (pad));
792 bclass = GST_BASE_PARSE_GET_CLASS (parse);
794 GST_DEBUG_OBJECT (parse, "handling event %d, %s", GST_EVENT_TYPE (event),
795 GST_EVENT_TYPE_NAME (event));
797 /* Cache all events except EOS, NEWSEGMENT and FLUSH_STOP if we have a
799 if (parse->priv->pending_segment && GST_EVENT_TYPE (event) != GST_EVENT_EOS
800 && GST_EVENT_TYPE (event) != GST_EVENT_NEWSEGMENT
801 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_START
802 && GST_EVENT_TYPE (event) != GST_EVENT_FLUSH_STOP) {
804 if (GST_EVENT_TYPE (event) == GST_EVENT_TAG)
805 /* See if any bitrate tags were posted */
806 gst_base_parse_handle_tag (parse, event);
808 parse->priv->pending_events =
809 g_list_append (parse->priv->pending_events, event);
813 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS &&
814 parse->priv->framecount < MIN_FRAMES_TO_POST_BITRATE)
815 /* We've not posted bitrate tags yet - do so now */
816 gst_base_parse_post_bitrates (parse, TRUE, TRUE, TRUE);
819 handled = bclass->event (parse, event);
822 handled = gst_base_parse_sink_eventfunc (parse, event);
825 ret = gst_pad_event_default (pad, event);
828 gst_object_unref (parse);
829 GST_DEBUG_OBJECT (parse, "event handled");
834 /* gst_base_parse_sink_eventfunc:
835 * @parse: #GstBaseParse.
836 * @event: #GstEvent to be handled.
838 * Element-level event handler function.
840 * The event will be unreffed only if it has been handled and this
841 * function returns %TRUE
843 * Returns: %TRUE if the event was handled and not need forwarding.
846 gst_base_parse_sink_eventfunc (GstBaseParse * parse, GstEvent * event)
848 gboolean handled = FALSE;
851 switch (GST_EVENT_TYPE (event)) {
852 case GST_EVENT_NEWSEGMENT:
854 gdouble rate, applied_rate;
856 gint64 start, stop, pos, next_ts, offset = 0;
859 gst_event_parse_new_segment_full (event, &update, &rate, &applied_rate,
860 &format, &start, &stop, &pos);
862 GST_DEBUG_OBJECT (parse, "newseg rate %g, applied rate %g, "
863 "format %d, start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT
864 ", pos = %" GST_TIME_FORMAT, rate, applied_rate, format,
865 GST_TIME_ARGS (start), GST_TIME_ARGS (stop), GST_TIME_ARGS (pos));
867 if (format == GST_FORMAT_BYTES) {
868 GstClockTime seg_start, seg_stop;
869 GstBaseParseSeek *seek = NULL;
872 /* stop time is allowed to be open-ended, but not start & pos */
873 seg_stop = GST_CLOCK_TIME_NONE;
877 GST_OBJECT_LOCK (parse);
878 for (node = parse->priv->pending_seeks; node; node = node->next) {
879 GstBaseParseSeek *tmp = node->data;
881 if (tmp->offset == pos) {
886 parse->priv->pending_seeks =
887 g_slist_remove (parse->priv->pending_seeks, seek);
888 GST_OBJECT_UNLOCK (parse);
891 GST_DEBUG_OBJECT (parse,
892 "Matched newsegment to%s seek: %" GST_SEGMENT_FORMAT,
893 seek->accurate ? " accurate" : "", &seek->segment);
894 seg_start = seek->segment.start;
895 seg_stop = seek->segment.stop;
896 next_ts = seek->start_ts;
897 parse->priv->exact_position = seek->accurate;
900 /* best attempt convert */
901 /* as these are only estimates, stop is kept open-ended to avoid
902 * premature cutting */
903 gst_base_parse_convert (parse, GST_FORMAT_BYTES, start,
904 GST_FORMAT_TIME, (gint64 *) & seg_start);
905 parse->priv->exact_position = (start == 0);
909 gst_event_unref (event);
910 event = gst_event_new_new_segment_full (update, rate, applied_rate,
911 GST_FORMAT_TIME, seg_start, seg_stop, seg_start);
912 format = GST_FORMAT_TIME;
915 GST_DEBUG_OBJECT (parse, "Converted incoming segment to TIME. "
916 "start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT,
917 GST_TIME_ARGS (seg_start), GST_TIME_ARGS (seg_stop));
918 } else if (format != GST_FORMAT_TIME) {
919 /* Unknown incoming segment format. Output a default open-ended
921 gst_event_unref (event);
922 event = gst_event_new_new_segment_full (update, rate, applied_rate,
923 GST_FORMAT_TIME, 0, GST_CLOCK_TIME_NONE, 0);
924 format = GST_FORMAT_TIME;
926 stop = GST_CLOCK_TIME_NONE;
928 /* not considered BYTE seekable if it is talking to us in TIME,
929 * whatever else it might claim */
930 parse->priv->upstream_seekable = FALSE;
934 gst_segment_set_newsegment_full (&parse->segment, update, rate,
935 applied_rate, format, start, stop, start);
937 /* save the segment for later, right before we push a new buffer so that
938 * the caps are fixed and the next linked element can receive
940 eventp = &parse->priv->pending_segment;
941 gst_event_replace (eventp, event);
942 gst_event_unref (event);
945 /* but finish the current segment */
946 GST_DEBUG_OBJECT (parse, "draining current segment");
947 if (parse->segment.rate > 0.0)
948 gst_base_parse_drain (parse);
950 gst_base_parse_process_fragment (parse, FALSE);
951 gst_adapter_clear (parse->priv->adapter);
952 parse->priv->offset = offset;
953 parse->priv->sync_offset = offset;
954 parse->priv->next_ts = next_ts;
955 parse->priv->last_ts = GST_CLOCK_TIME_NONE;
956 parse->priv->discont = TRUE;
957 parse->priv->seen_keyframe = FALSE;
961 case GST_EVENT_FLUSH_START:
962 parse->priv->flushing = TRUE;
963 handled = gst_pad_push_event (parse->srcpad, gst_event_ref (event));
965 gst_event_unref (event);
966 /* Wait for _chain() to exit by taking the srcpad STREAM_LOCK */
967 GST_PAD_STREAM_LOCK (parse->srcpad);
968 GST_PAD_STREAM_UNLOCK (parse->srcpad);
972 case GST_EVENT_FLUSH_STOP:
973 gst_adapter_clear (parse->priv->adapter);
974 gst_base_parse_clear_queues (parse);
975 parse->priv->flushing = FALSE;
976 parse->priv->discont = TRUE;
977 parse->priv->last_ts = GST_CLOCK_TIME_NONE;
981 if (parse->segment.rate > 0.0)
982 gst_base_parse_drain (parse);
984 gst_base_parse_process_fragment (parse, FALSE);
986 /* If we STILL have zero frames processed, fire an error */
987 if (parse->priv->framecount == 0) {
988 GST_ELEMENT_ERROR (parse, STREAM, WRONG_TYPE,
989 ("No valid frames found before end of stream"), (NULL));
991 /* newsegment before eos */
992 if (parse->priv->pending_segment) {
993 gst_pad_push_event (parse->srcpad, parse->priv->pending_segment);
994 parse->priv->pending_segment = NULL;
1006 /* gst_base_parse_src_event:
1007 * @pad: #GstPad that received the event.
1008 * @event: #GstEvent that was received.
1010 * Handler for source pad events.
1012 * Returns: TRUE if the event was handled.
1015 gst_base_parse_src_event (GstPad * pad, GstEvent * event)
1017 GstBaseParse *parse;
1018 GstBaseParseClass *bclass;
1019 gboolean handled = FALSE;
1020 gboolean ret = TRUE;
1022 parse = GST_BASE_PARSE (gst_pad_get_parent (pad));
1023 bclass = GST_BASE_PARSE_GET_CLASS (parse);
1025 GST_DEBUG_OBJECT (parse, "event %d, %s", GST_EVENT_TYPE (event),
1026 GST_EVENT_TYPE_NAME (event));
1028 if (bclass->src_event)
1029 handled = bclass->src_event (parse, event);
1032 ret = gst_pad_event_default (pad, event);
1034 gst_object_unref (parse);
1039 gst_base_parse_is_seekable (GstBaseParse * parse)
1041 /* FIXME: could do more here, e.g. check index or just send data from 0
1042 * in pull mode and let decoder/sink clip */
1043 return (parse->priv->format_flags & GST_BASE_PARSE_FORMAT_FLAG_SYNCABLE);
1046 /* gst_base_parse_src_eventfunc:
1047 * @parse: #GstBaseParse.
1048 * @event: #GstEvent that was received.
1050 * Default srcpad event handler.
1052 * Returns: TRUE if the event was handled and can be dropped.
1055 gst_base_parse_src_eventfunc (GstBaseParse * parse, GstEvent * event)
1057 gboolean handled = FALSE;
1059 switch (GST_EVENT_TYPE (event)) {
1060 case GST_EVENT_SEEK:
1062 if (gst_base_parse_is_seekable (parse)) {
1063 handled = gst_base_parse_handle_seek (parse, event);
1075 * gst_base_parse_convert_default:
1076 * @parse: #GstBaseParse.
1077 * @src_format: #GstFormat describing the source format.
1078 * @src_value: Source value to be converted.
1079 * @dest_format: #GstFormat defining the converted format.
1080 * @dest_value: Pointer where the conversion result will be put.
1082 * Default implementation of "convert" vmethod in #GstBaseParse class.
1084 * Returns: TRUE if conversion was successful.
1089 gst_base_parse_convert_default (GstBaseParse * parse,
1090 GstFormat src_format,
1091 gint64 src_value, GstFormat dest_format, gint64 * dest_value)
1093 gboolean ret = FALSE;
1094 guint64 bytes, duration;
1096 if (G_UNLIKELY (src_format == dest_format)) {
1097 *dest_value = src_value;
1101 if (G_UNLIKELY (src_value == -1)) {
1106 if (G_UNLIKELY (src_value == 0)) {
1111 /* need at least some frames */
1112 if (!parse->priv->framecount)
1115 duration = parse->priv->acc_duration / GST_MSECOND;
1116 bytes = parse->priv->bytecount;
1118 if (G_UNLIKELY (!duration || !bytes))
1121 if (src_format == GST_FORMAT_BYTES) {
1122 if (dest_format == GST_FORMAT_TIME) {
1123 /* BYTES -> TIME conversion */
1124 GST_DEBUG_OBJECT (parse, "converting bytes -> time");
1125 *dest_value = gst_util_uint64_scale (src_value, duration, bytes);
1126 *dest_value *= GST_MSECOND;
1127 GST_DEBUG_OBJECT (parse, "conversion result: %" G_GINT64_FORMAT " ms",
1128 *dest_value / GST_MSECOND);
1131 } else if (src_format == GST_FORMAT_TIME) {
1132 if (dest_format == GST_FORMAT_BYTES) {
1133 GST_DEBUG_OBJECT (parse, "converting time -> bytes");
1134 *dest_value = gst_util_uint64_scale (src_value / GST_MSECOND, bytes,
1136 GST_DEBUG_OBJECT (parse,
1137 "time %" G_GINT64_FORMAT " ms in bytes = %" G_GINT64_FORMAT,
1138 src_value / GST_MSECOND, *dest_value);
1141 } else if (src_format == GST_FORMAT_DEFAULT) {
1142 /* DEFAULT == frame-based */
1143 if (dest_format == GST_FORMAT_TIME) {
1144 if (parse->priv->fps_den) {
1145 *dest_value = gst_util_uint64_scale (src_value,
1146 GST_SECOND * parse->priv->fps_den, parse->priv->fps_num);
1149 } else if (dest_format == GST_FORMAT_BYTES) {
1157 gst_base_parse_update_duration (GstBaseParse * aacparse)
1160 GstBaseParse *parse;
1162 parse = GST_BASE_PARSE (aacparse);
1164 peer = gst_pad_get_peer (parse->sinkpad);
1166 GstFormat pformat = GST_FORMAT_BYTES;
1167 gboolean qres = FALSE;
1168 gint64 ptot, dest_value;
1170 qres = gst_pad_query_duration (peer, &pformat, &ptot);
1171 gst_object_unref (GST_OBJECT (peer));
1173 if (gst_base_parse_convert (parse, pformat, ptot,
1174 GST_FORMAT_TIME, &dest_value)) {
1175 parse->priv->estimated_duration = dest_value;
1176 GST_LOG_OBJECT (parse,
1177 "updated estimated duration to %" GST_TIME_FORMAT,
1178 GST_TIME_ARGS (dest_value));
1185 gst_base_parse_post_bitrates (GstBaseParse * parse, gboolean post_min,
1186 gboolean post_avg, gboolean post_max)
1188 GstTagList *taglist = gst_tag_list_new ();
1190 if (post_min && parse->priv->post_min_bitrate)
1191 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE,
1192 GST_TAG_MINIMUM_BITRATE, parse->priv->min_bitrate, NULL);
1194 if (post_avg && parse->priv->post_avg_bitrate) {
1195 parse->priv->posted_avg_bitrate = parse->priv->avg_bitrate;
1196 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_BITRATE,
1197 parse->priv->avg_bitrate, NULL);
1200 if (post_max && parse->priv->post_max_bitrate)
1201 gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE,
1202 GST_TAG_MAXIMUM_BITRATE, parse->priv->max_bitrate, NULL);
1204 GST_DEBUG_OBJECT (parse, "Updated bitrates. Min: %u, Avg: %u, Max: %u",
1205 parse->priv->min_bitrate, parse->priv->avg_bitrate,
1206 parse->priv->max_bitrate);
1208 gst_element_found_tags_for_pad (GST_ELEMENT (parse), parse->srcpad, taglist);
1211 /* gst_base_parse_update_bitrates:
1212 * @parse: #GstBaseParse.
1213 * @buffer: Current frame as a #GstBuffer
1215 * Keeps track of the minimum and maximum bitrates, and also maintains a
1216 * running average bitrate of the stream so far.
1219 gst_base_parse_update_bitrates (GstBaseParse * parse, GstBaseParseFrame * frame)
1221 /* Only update the tag on a 10 kbps delta */
1222 static const gint update_threshold = 10000;
1224 GstBaseParseClass *klass;
1225 guint64 data_len, frame_dur;
1226 gint overhead, frame_bitrate, old_avg_bitrate;
1227 gboolean update_min = FALSE, update_avg = FALSE, update_max = FALSE;
1228 GstBuffer *buffer = frame->buffer;
1230 klass = GST_BASE_PARSE_GET_CLASS (parse);
1232 overhead = frame->overhead;
1236 data_len = GST_BUFFER_SIZE (buffer) - overhead;
1237 parse->priv->data_bytecount += data_len;
1239 /* duration should be valid by now,
1240 * either set by subclass or maybe based on fps settings */
1241 if (GST_BUFFER_DURATION_IS_VALID (buffer) && parse->priv->acc_duration != 0) {
1242 /* Calculate duration of a frame from buffer properties */
1243 frame_dur = GST_BUFFER_DURATION (buffer);
1244 parse->priv->avg_bitrate = (8 * parse->priv->data_bytecount * GST_SECOND) /
1245 parse->priv->acc_duration;
1248 /* No way to figure out frame duration (is this even possible?) */
1252 /* override if subclass provided bitrate, e.g. metadata based */
1253 if (parse->priv->bitrate) {
1254 parse->priv->avg_bitrate = parse->priv->bitrate;
1255 /* spread this (confirmed) info ASAP */
1256 if (parse->priv->posted_avg_bitrate != parse->priv->avg_bitrate)
1257 gst_base_parse_post_bitrates (parse, FALSE, TRUE, FALSE);
1261 frame_bitrate = (8 * data_len * GST_SECOND) / frame_dur;
1265 GST_LOG_OBJECT (parse, "frame bitrate %u, avg bitrate %u", frame_bitrate,
1266 parse->priv->avg_bitrate);
1268 if (parse->priv->framecount < MIN_FRAMES_TO_POST_BITRATE) {
1270 } else if (parse->priv->framecount == MIN_FRAMES_TO_POST_BITRATE) {
1271 /* always post all at threshold time */
1272 update_min = update_max = update_avg = TRUE;
1275 if (G_LIKELY (parse->priv->framecount >= MIN_FRAMES_TO_POST_BITRATE)) {
1276 if (frame_bitrate < parse->priv->min_bitrate) {
1277 parse->priv->min_bitrate = frame_bitrate;
1281 if (frame_bitrate > parse->priv->max_bitrate) {
1282 parse->priv->max_bitrate = frame_bitrate;
1286 old_avg_bitrate = parse->priv->posted_avg_bitrate;
1287 if ((gint) (old_avg_bitrate - parse->priv->avg_bitrate) > update_threshold
1288 || (gint) (parse->priv->avg_bitrate - old_avg_bitrate) >
1293 if ((update_min || update_avg || update_max))
1294 gst_base_parse_post_bitrates (parse, update_min, update_avg, update_max);
1296 /* If average bitrate changes that much and no valid (time) duration provided,
1297 * then post a new duration message so applications can update their cached
1299 if (update_avg && !(parse->priv->duration_fmt == GST_FORMAT_TIME &&
1300 GST_CLOCK_TIME_IS_VALID (parse->priv->duration)))
1301 gst_element_post_message (GST_ELEMENT (parse),
1302 gst_message_new_duration (GST_OBJECT (parse), GST_FORMAT_TIME, -1));
1309 * gst_base_parse_add_index_entry:
1310 * @parse: #GstBaseParse.
1311 * @offset: offset of entry
1312 * @ts: timestamp associated with offset
1313 * @key: whether entry refers to keyframe
1314 * @force: add entry disregarding sanity checks
1316 * Adds an entry to the index associating @offset to @ts. It is recommended
1317 * to only add keyframe entries. @force allows to bypass checks, such as
1318 * whether the stream is (upstream) seekable, another entry is already "close"
1319 * to the new entry, etc.
1321 * Returns: #gboolean indicating whether entry was added
1326 gst_base_parse_add_index_entry (GstBaseParse * parse, guint64 offset,
1327 GstClockTime ts, gboolean key, gboolean force)
1329 gboolean ret = FALSE;
1330 GstIndexAssociation associations[2];
1332 GST_LOG_OBJECT (parse, "Adding key=%d index entry %" GST_TIME_FORMAT
1333 " @ offset 0x%08" G_GINT64_MODIFIER "x", key, GST_TIME_ARGS (ts), offset);
1335 if (G_LIKELY (!force)) {
1337 if (!parse->priv->upstream_seekable) {
1338 GST_DEBUG_OBJECT (parse, "upstream not seekable; discarding");
1342 /* FIXME need better helper data structure that handles these issues
1343 * related to ongoing collecting of index entries */
1344 if (parse->priv->index_last_offset >= (gint64) offset) {
1345 GST_DEBUG_OBJECT (parse, "already have entries up to offset "
1346 "0x%08" G_GINT64_MODIFIER "x", parse->priv->index_last_offset);
1350 if (GST_CLOCK_TIME_IS_VALID (parse->priv->index_last_ts) &&
1351 GST_CLOCK_DIFF (parse->priv->index_last_ts, ts) <
1352 parse->priv->idx_interval) {
1353 GST_DEBUG_OBJECT (parse, "entry too close to last time %" GST_TIME_FORMAT,
1354 GST_TIME_ARGS (parse->priv->index_last_ts));
1358 /* if last is not really the last one */
1359 if (!parse->priv->index_last_valid) {
1360 GstClockTime prev_ts;
1362 gst_base_parse_find_offset (parse, ts, TRUE, &prev_ts);
1363 if (GST_CLOCK_DIFF (prev_ts, ts) < parse->priv->idx_interval) {
1364 GST_DEBUG_OBJECT (parse,
1365 "entry too close to existing entry %" GST_TIME_FORMAT,
1366 GST_TIME_ARGS (prev_ts));
1367 parse->priv->index_last_offset = offset;
1368 parse->priv->index_last_ts = ts;
1374 associations[0].format = GST_FORMAT_TIME;
1375 associations[0].value = ts;
1376 associations[1].format = GST_FORMAT_BYTES;
1377 associations[1].value = offset;
1379 /* index might change on-the-fly, although that would be nutty app ... */
1380 GST_OBJECT_LOCK (parse);
1381 gst_index_add_associationv (parse->priv->index, parse->priv->index_id,
1382 (key) ? GST_ASSOCIATION_FLAG_KEY_UNIT : GST_ASSOCIATION_FLAG_DELTA_UNIT,
1383 2, (const GstIndexAssociation *) &associations);
1384 GST_OBJECT_UNLOCK (parse);
1387 parse->priv->index_last_offset = offset;
1388 parse->priv->index_last_ts = ts;
1397 /* check for seekable upstream, above and beyond a mere query */
1399 gst_base_parse_check_seekability (GstBaseParse * parse)
1402 gboolean seekable = FALSE;
1403 gint64 start = -1, stop = -1;
1404 guint idx_interval = 0;
1406 query = gst_query_new_seeking (GST_FORMAT_BYTES);
1407 if (!gst_pad_peer_query (parse->sinkpad, query)) {
1408 GST_DEBUG_OBJECT (parse, "seeking query failed");
1412 gst_query_parse_seeking (query, NULL, &seekable, &start, &stop);
1414 /* try harder to query upstream size if we didn't get it the first time */
1415 if (seekable && stop == -1) {
1416 GstFormat fmt = GST_FORMAT_BYTES;
1418 GST_DEBUG_OBJECT (parse, "doing duration query to fix up unset stop");
1419 gst_pad_query_peer_duration (parse->sinkpad, &fmt, &stop);
1422 /* if upstream doesn't know the size, it's likely that it's not seekable in
1423 * practice even if it technically may be seekable */
1424 if (seekable && (start != 0 || stop <= start)) {
1425 GST_DEBUG_OBJECT (parse, "seekable but unknown start/stop -> disable");
1429 /* let's not put every single frame into our index */
1431 if (stop < 10 * 1024 * 1024)
1433 else if (stop < 100 * 1024 * 1024)
1436 idx_interval = 1000;
1440 gst_query_unref (query);
1442 GST_DEBUG_OBJECT (parse, "seekable: %d (%" G_GUINT64_FORMAT " - %"
1443 G_GUINT64_FORMAT ")", seekable, start, stop);
1444 parse->priv->upstream_seekable = seekable;
1445 parse->priv->upstream_size = seekable ? stop : 0;
1447 GST_DEBUG_OBJECT (parse, "idx_interval: %ums", idx_interval);
1448 parse->priv->idx_interval = idx_interval * GST_MSECOND;
1451 /* some misc checks on upstream */
1453 gst_base_parse_check_upstream (GstBaseParse * parse)
1455 GstFormat fmt = GST_FORMAT_TIME;
1458 if (gst_pad_query_peer_duration (parse->sinkpad, &fmt, &stop))
1459 if (GST_CLOCK_TIME_IS_VALID (stop) && stop) {
1460 /* upstream has one, accept it also, and no further updates */
1461 gst_base_parse_set_duration (parse, GST_FORMAT_TIME, stop, 0);
1462 parse->priv->upstream_has_duration = TRUE;
1465 GST_DEBUG_OBJECT (parse, "upstream_has_duration: %d",
1466 parse->priv->upstream_has_duration);
1469 /* checks src caps to determine if dealing with audio or video */
1470 /* TODO maybe forego automagic stuff and let subclass configure it ? */
1472 gst_base_parse_check_media (GstBaseParse * parse)
1477 caps = GST_PAD_CAPS (parse->srcpad);
1478 if (G_LIKELY (caps) && (s = gst_caps_get_structure (caps, 0))) {
1479 parse->priv->is_video =
1480 g_str_has_prefix (gst_structure_get_name (s), "video");
1482 /* historical default */
1483 parse->priv->is_video = FALSE;
1486 GST_DEBUG_OBJECT (parse, "media is video == %d", parse->priv->is_video);
1489 /* gst_base_parse_handle_and_push_buffer:
1490 * @parse: #GstBaseParse.
1491 * @klass: #GstBaseParseClass.
1492 * @buffer: #GstBuffer.
1494 * Parses the frame from given buffer and pushes it forward. Also performs
1495 * timestamp handling and checks the segment limits.
1497 * This is called with srcpad STREAM_LOCK held.
1499 * Returns: #GstFlowReturn
1501 static GstFlowReturn
1502 gst_base_parse_handle_and_push_frame (GstBaseParse * parse,
1503 GstBaseParseClass * klass, GstBaseParseFrame * frame)
1509 g_return_val_if_fail (frame != NULL, GST_FLOW_ERROR);
1511 buffer = frame->buffer;
1513 if (parse->priv->discont) {
1514 GST_DEBUG_OBJECT (parse, "marking DISCONT");
1515 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
1516 parse->priv->discont = FALSE;
1519 /* some one-time start-up */
1520 if (G_UNLIKELY (!parse->priv->framecount)) {
1521 gst_base_parse_check_seekability (parse);
1522 gst_base_parse_check_upstream (parse);
1525 GST_LOG_OBJECT (parse,
1526 "parsing frame at offset %" G_GUINT64_FORMAT
1527 " (%#" G_GINT64_MODIFIER "x) of size %d",
1528 GST_BUFFER_OFFSET (buffer), GST_BUFFER_OFFSET (buffer),
1529 GST_BUFFER_SIZE (buffer));
1531 /* use default handler to provide initial (upstream) metadata */
1532 gst_base_parse_parse_frame (parse, frame);
1534 /* store offset as it might get overwritten */
1535 offset = GST_BUFFER_OFFSET (buffer);
1536 ret = klass->parse_frame (parse, frame);
1538 buffer = frame->buffer;
1539 /* subclass must play nice */
1540 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
1542 /* check if subclass/format can provide ts.
1543 * If so, that allows and enables extra seek and duration determining options */
1544 if (G_UNLIKELY (parse->priv->first_frame_offset < 0 && ret == GST_FLOW_OK)) {
1545 if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
1546 GST_BASE_PARSE_HAS_TIME (parse) &&
1547 parse->priv->pad_mode == GST_ACTIVATE_PULL) {
1548 parse->priv->first_frame_offset = offset;
1549 parse->priv->first_frame_ts = GST_BUFFER_TIMESTAMP (buffer);
1550 GST_DEBUG_OBJECT (parse, "subclass provided ts %" GST_TIME_FORMAT
1551 " for first frame at offset %" G_GINT64_FORMAT,
1552 GST_TIME_ARGS (parse->priv->first_frame_ts),
1553 parse->priv->first_frame_offset);
1554 if (!GST_CLOCK_TIME_IS_VALID (parse->priv->duration)) {
1556 GstClockTime last_ts = G_MAXINT64;
1558 GST_DEBUG_OBJECT (parse, "no duration; trying scan to determine");
1559 gst_base_parse_locate_time (parse, &last_ts, &off);
1560 if (GST_CLOCK_TIME_IS_VALID (last_ts))
1561 gst_base_parse_set_duration (parse, GST_FORMAT_TIME, last_ts, 0);
1564 /* disable further checks */
1565 parse->priv->first_frame_offset = 0;
1569 /* again use default handler to add missing metadata;
1570 * we may have new information on frame properties */
1571 gst_base_parse_parse_frame (parse, frame);
1572 if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
1573 GST_BUFFER_DURATION_IS_VALID (buffer)) {
1574 parse->priv->next_ts =
1575 GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer);
1577 /* we lost track, do not produce bogus time next time around
1578 * (probably means parser subclass has given up on parsing as well) */
1579 GST_DEBUG_OBJECT (parse, "no next fallback timestamp");
1580 parse->priv->next_ts = GST_CLOCK_TIME_NONE;
1583 if (parse->priv->upstream_seekable && parse->priv->exact_position &&
1584 GST_BUFFER_TIMESTAMP_IS_VALID (buffer))
1585 gst_base_parse_add_index_entry (parse, offset,
1586 GST_BUFFER_TIMESTAMP (buffer),
1587 !GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT), FALSE);
1589 /* First buffers are dropped, this means that the subclass needs more
1590 * frames to decide on the format and queues them internally */
1591 /* convert internal flow to OK and mark discont for the next buffer. */
1592 if (ret == GST_BASE_PARSE_FLOW_DROPPED) {
1593 gst_base_parse_frame_clear (parse, frame);
1595 } else if (ret == GST_BASE_PARSE_FLOW_QUEUED) {
1596 parse->priv->queued_frames = g_list_append (parse->priv->queued_frames,
1597 gst_base_parse_frame_copy_and_clear (frame));
1599 } else if (ret != GST_FLOW_OK) {
1603 /* All OK, push queued frames if there are any */
1604 if (G_UNLIKELY (parse->priv->queued_frames != NULL)) {
1605 g_list_foreach (parse->priv->queued_frames,
1606 (GFunc) gst_base_parse_push_pending_frame, parse);
1607 g_list_free (parse->priv->queued_frames);
1608 parse->priv->queued_frames = NULL;
1611 return gst_base_parse_push_frame (parse, frame);
1615 gst_base_parse_push_pending_frame (GstBaseParseFrame * frame,
1616 GstBaseParse * parse)
1618 gst_buffer_set_caps (frame->buffer,
1619 GST_PAD_CAPS (GST_BASE_PARSE_SRC_PAD (parse)));
1620 gst_base_parse_push_frame (parse, frame);
1621 gst_base_parse_frame_free (frame);
1625 * gst_base_parse_push_frame:
1626 * @parse: #GstBaseParse.
1627 * @frame: #GstBaseParseFrame.
1629 * Pushes the frame downstream, sends any pending events and
1630 * does some timestamp and segment handling.
1632 * This must be called with sinkpad STREAM_LOCK held.
1634 * Returns: #GstFlowReturn
1639 gst_base_parse_push_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
1641 GstFlowReturn ret = GST_FLOW_OK;
1642 GstClockTime last_start = GST_CLOCK_TIME_NONE;
1643 GstClockTime last_stop = GST_CLOCK_TIME_NONE;
1644 GstBaseParseClass *klass = GST_BASE_PARSE_GET_CLASS (parse);
1647 g_return_val_if_fail (frame != NULL, GST_FLOW_ERROR);
1648 g_return_val_if_fail (frame->buffer != NULL, GST_FLOW_ERROR);
1650 buffer = frame->buffer;
1652 GST_LOG_OBJECT (parse,
1653 "processing buffer of size %d with ts %" GST_TIME_FORMAT
1654 ", duration %" GST_TIME_FORMAT, GST_BUFFER_SIZE (buffer),
1655 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
1656 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
1659 parse->priv->bytecount += GST_BUFFER_SIZE (buffer);
1660 if (G_LIKELY (!(frame->flags & GST_BASE_PARSE_FRAME_FLAG_NO_FRAME))) {
1661 parse->priv->framecount++;
1662 if (GST_BUFFER_DURATION_IS_VALID (buffer)) {
1663 parse->priv->acc_duration += GST_BUFFER_DURATION (buffer);
1666 /* 0 means disabled */
1667 if (parse->priv->update_interval < 0)
1668 parse->priv->update_interval = 50;
1669 else if (parse->priv->update_interval > 0 &&
1670 (parse->priv->framecount % parse->priv->update_interval) == 0)
1671 gst_base_parse_update_duration (parse);
1673 if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer))
1674 last_start = last_stop = GST_BUFFER_TIMESTAMP (buffer);
1675 if (last_start != GST_CLOCK_TIME_NONE
1676 && GST_BUFFER_DURATION_IS_VALID (buffer))
1677 last_stop = last_start + GST_BUFFER_DURATION (buffer);
1679 /* should have caps by now */
1680 g_return_val_if_fail (GST_PAD_CAPS (parse->srcpad), GST_FLOW_ERROR);
1682 /* segment adjustment magic; only if we are running the whole show */
1683 if (!GST_BASE_PARSE_PASSTHROUGH (parse) && parse->segment.rate > 0.0 &&
1684 (parse->priv->pad_mode == GST_ACTIVATE_PULL ||
1685 parse->priv->upstream_seekable)) {
1686 /* segment times are typically estimates,
1687 * actual frame data might lead subclass to different timestamps,
1688 * so override segment start from what is supplied there */
1689 if (G_UNLIKELY (parse->priv->pending_segment && !parse->priv->exact_position
1690 && GST_CLOCK_TIME_IS_VALID (last_start))) {
1691 gst_event_unref (parse->priv->pending_segment);
1692 parse->segment.start =
1693 MIN ((guint64) last_start, (guint64) parse->segment.stop);
1694 GST_DEBUG_OBJECT (parse,
1695 "adjusting pending segment start to %" GST_TIME_FORMAT,
1696 GST_TIME_ARGS (parse->segment.start));
1697 parse->priv->pending_segment =
1698 gst_event_new_new_segment (FALSE, parse->segment.rate,
1699 parse->segment.format, parse->segment.start,
1700 parse->segment.stop, parse->segment.start);
1702 /* handle gaps, e.g. non-zero start-time, in as much not handled by above */
1703 if (GST_CLOCK_TIME_IS_VALID (parse->segment.last_stop) &&
1704 GST_CLOCK_TIME_IS_VALID (last_start)) {
1705 GstClockTimeDiff diff;
1707 /* only send newsegments with increasing start times,
1708 * otherwise if these go back and forth downstream (sinks) increase
1709 * accumulated time and running_time */
1710 diff = GST_CLOCK_DIFF (parse->segment.last_stop, last_start);
1711 if (G_UNLIKELY (diff > 2 * GST_SECOND
1712 && last_start > parse->segment.start
1713 && (!GST_CLOCK_TIME_IS_VALID (parse->segment.stop)
1714 || last_start < parse->segment.stop))) {
1715 GST_DEBUG_OBJECT (parse,
1716 "Gap of %" G_GINT64_FORMAT " ns detected in stream " "(%"
1717 GST_TIME_FORMAT " -> %" GST_TIME_FORMAT "). "
1718 "Sending updated NEWSEGMENT events", diff,
1719 GST_TIME_ARGS (parse->segment.last_stop),
1720 GST_TIME_ARGS (last_start));
1721 if (G_UNLIKELY (parse->priv->pending_segment)) {
1722 gst_event_unref (parse->priv->pending_segment);
1723 parse->segment.start = last_start;
1724 parse->priv->pending_segment =
1725 gst_event_new_new_segment (FALSE, parse->segment.rate,
1726 parse->segment.format, parse->segment.start,
1727 parse->segment.stop, parse->segment.start);
1729 /* send newsegment events such that the gap is not accounted in
1730 * accum time, hence running_time */
1731 /* close ahead of gap */
1732 gst_pad_push_event (parse->srcpad,
1733 gst_event_new_new_segment (TRUE, parse->segment.rate,
1734 parse->segment.format, parse->segment.last_stop,
1735 parse->segment.last_stop, parse->segment.last_stop));
1737 gst_pad_push_event (parse->srcpad,
1738 gst_event_new_new_segment (FALSE, parse->segment.rate,
1739 parse->segment.format, last_start,
1740 parse->segment.stop, last_start));
1742 /* align segment view with downstream,
1743 * prevents double-counting accum when closing segment */
1744 gst_segment_set_newsegment (&parse->segment, FALSE,
1745 parse->segment.rate, parse->segment.format, last_start,
1746 parse->segment.stop, last_start);
1747 parse->segment.last_stop = last_start;
1752 /* and should then also be linked downstream, so safe to send some events */
1753 if (G_UNLIKELY (parse->priv->close_segment)) {
1754 /* only set up by loop */
1755 GST_DEBUG_OBJECT (parse, "loop sending close segment");
1756 gst_pad_push_event (parse->srcpad, parse->priv->close_segment);
1757 parse->priv->close_segment = NULL;
1759 if (G_UNLIKELY (parse->priv->pending_segment)) {
1760 GST_DEBUG_OBJECT (parse, "%s push pending segment",
1761 parse->priv->pad_mode == GST_ACTIVATE_PULL ? "loop" : "chain");
1762 gst_pad_push_event (parse->srcpad, parse->priv->pending_segment);
1763 parse->priv->pending_segment = NULL;
1765 /* have caps; check identity */
1766 gst_base_parse_check_media (parse);
1769 /* update bitrates and optionally post corresponding tags
1770 * (following newsegment) */
1771 gst_base_parse_update_bitrates (parse, frame);
1773 if (G_UNLIKELY (parse->priv->pending_events)) {
1776 for (l = parse->priv->pending_events; l != NULL; l = l->next) {
1777 gst_pad_push_event (parse->srcpad, GST_EVENT (l->data));
1779 g_list_free (parse->priv->pending_events);
1780 parse->priv->pending_events = NULL;
1783 if (klass->pre_push_frame) {
1784 ret = klass->pre_push_frame (parse, frame);
1786 frame->flags |= GST_BASE_PARSE_FRAME_FLAG_CLIP;
1789 /* take final ownership of frame buffer */
1790 buffer = frame->buffer;
1791 frame->buffer = NULL;
1793 /* subclass must play nice */
1794 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
1797 buffer = gst_buffer_make_metadata_writable (buffer);
1798 gst_buffer_set_caps (buffer, GST_PAD_CAPS (parse->srcpad));
1800 parse->priv->seen_keyframe |= parse->priv->is_video &&
1801 !GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
1803 if (frame->flags & GST_BASE_PARSE_FRAME_FLAG_CLIP) {
1804 if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
1805 GST_CLOCK_TIME_IS_VALID (parse->segment.stop) &&
1806 GST_BUFFER_TIMESTAMP (buffer) >
1807 parse->segment.stop + parse->priv->lead_out_ts) {
1808 GST_LOG_OBJECT (parse, "Dropped frame, after segment");
1809 ret = GST_FLOW_UNEXPECTED;
1810 } else if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
1811 GST_BUFFER_DURATION_IS_VALID (buffer) &&
1812 GST_CLOCK_TIME_IS_VALID (parse->segment.start) &&
1813 GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer) +
1814 parse->priv->lead_in_ts < parse->segment.start) {
1815 if (parse->priv->seen_keyframe) {
1816 GST_LOG_OBJECT (parse, "Frame before segment, after keyframe");
1819 GST_LOG_OBJECT (parse, "Dropped frame, before segment");
1820 ret = GST_BASE_PARSE_FLOW_DROPPED;
1827 if (ret == GST_BASE_PARSE_FLOW_DROPPED) {
1828 GST_LOG_OBJECT (parse, "frame (%d bytes) dropped",
1829 GST_BUFFER_SIZE (buffer));
1830 gst_buffer_unref (buffer);
1832 } else if (ret == GST_FLOW_OK) {
1833 if (parse->segment.rate > 0.0) {
1834 ret = gst_pad_push (parse->srcpad, buffer);
1835 GST_LOG_OBJECT (parse, "frame (%d bytes) pushed: %s",
1836 GST_BUFFER_SIZE (buffer), gst_flow_get_name (ret));
1838 GST_LOG_OBJECT (parse, "frame (%d bytes) queued for now",
1839 GST_BUFFER_SIZE (buffer));
1840 parse->priv->buffers_queued =
1841 g_slist_prepend (parse->priv->buffers_queued, buffer);
1845 gst_buffer_unref (buffer);
1846 GST_LOG_OBJECT (parse, "frame (%d bytes) not pushed: %s",
1847 GST_BUFFER_SIZE (buffer), gst_flow_get_name (ret));
1848 /* if we are not sufficiently in control, let upstream decide on EOS */
1849 if (ret == GST_FLOW_UNEXPECTED &&
1850 (GST_BASE_PARSE_PASSTHROUGH (parse) ||
1851 (parse->priv->pad_mode == GST_ACTIVATE_PUSH &&
1852 !parse->priv->upstream_seekable)))
1856 /* Update current running segment position */
1857 if (ret == GST_FLOW_OK && last_stop != GST_CLOCK_TIME_NONE &&
1858 parse->segment.last_stop < last_stop)
1859 gst_segment_set_last_stop (&parse->segment, GST_FORMAT_TIME, last_stop);
1861 gst_base_parse_frame_clear (parse, frame);
1867 /* gst_base_parse_drain:
1869 * Drains the adapter until it is empty. It decreases the min_frame_size to
1870 * match the current adapter size and calls chain method until the adapter
1871 * is emptied or chain returns with error.
1874 gst_base_parse_drain (GstBaseParse * parse)
1878 GST_DEBUG_OBJECT (parse, "draining");
1879 parse->priv->drain = TRUE;
1882 avail = gst_adapter_available (parse->priv->adapter);
1886 if (gst_base_parse_chain (parse->sinkpad, NULL) != GST_FLOW_OK) {
1890 /* nothing changed, maybe due to truncated frame; break infinite loop */
1891 if (avail == gst_adapter_available (parse->priv->adapter)) {
1892 GST_DEBUG_OBJECT (parse, "no change during draining; flushing");
1893 gst_adapter_clear (parse->priv->adapter);
1897 parse->priv->drain = FALSE;
1900 /* gst_base_parse_send_buffers
1902 * Sends buffers collected in send_buffers downstream, and ensures that list
1903 * is empty at the end (errors or not).
1905 static GstFlowReturn
1906 gst_base_parse_send_buffers (GstBaseParse * parse)
1908 GSList *send = NULL;
1910 GstFlowReturn ret = GST_FLOW_OK;
1912 send = parse->priv->buffers_send;
1916 buf = GST_BUFFER_CAST (send->data);
1917 GST_LOG_OBJECT (parse, "pushing buffer %p, timestamp %"
1918 GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT
1919 ", offset %" G_GINT64_FORMAT, buf,
1920 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
1921 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)), GST_BUFFER_OFFSET (buf));
1923 /* iterate output queue an push downstream */
1924 ret = gst_pad_push (parse->srcpad, buf);
1925 send = g_slist_delete_link (send, send);
1927 /* clear any leftover if error */
1928 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
1930 buf = GST_BUFFER_CAST (send->data);
1931 gst_buffer_unref (buf);
1932 send = g_slist_delete_link (send, send);
1937 parse->priv->buffers_send = send;
1942 /* gst_base_parse_process_fragment:
1944 * Processes a reverse playback (forward) fragment:
1945 * - append head of last fragment that was skipped to current fragment data
1946 * - drain the resulting current fragment data (i.e. repeated chain)
1947 * - add time/duration (if needed) to frames queued by chain
1948 * - push queued data
1950 static GstFlowReturn
1951 gst_base_parse_process_fragment (GstBaseParse * parse, gboolean push_only)
1954 GstFlowReturn ret = GST_FLOW_OK;
1955 gboolean seen_key = FALSE, seen_delta = FALSE;
1961 parse->priv->buffers_pending = g_slist_reverse (parse->priv->buffers_pending);
1962 while (parse->priv->buffers_pending) {
1963 buf = GST_BUFFER_CAST (parse->priv->buffers_pending->data);
1964 GST_LOG_OBJECT (parse, "adding pending buffer (size %d)",
1965 GST_BUFFER_SIZE (buf));
1966 gst_adapter_push (parse->priv->adapter, buf);
1967 parse->priv->buffers_pending =
1968 g_slist_delete_link (parse->priv->buffers_pending,
1969 parse->priv->buffers_pending);
1972 /* invalidate so no fall-back timestamping is performed;
1973 * ok if taken from subclass or upstream */
1974 parse->priv->next_ts = GST_CLOCK_TIME_NONE;
1975 /* prevent it hanging around stop all the time */
1976 parse->segment.last_stop = GST_CLOCK_TIME_NONE;
1978 parse->priv->discont = TRUE;
1980 /* chain looks for frames and queues resulting ones (in stead of pushing) */
1981 /* initial skipped data is added to buffers_pending */
1982 gst_base_parse_drain (parse);
1985 if (parse->priv->buffers_send) {
1986 buf = GST_BUFFER_CAST (parse->priv->buffers_send->data);
1987 seen_key |= !GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
1990 /* add metadata (if needed to queued buffers */
1991 GST_LOG_OBJECT (parse, "last timestamp: %" GST_TIME_FORMAT,
1992 GST_TIME_ARGS (parse->priv->last_ts));
1993 while (parse->priv->buffers_queued) {
1994 buf = GST_BUFFER_CAST (parse->priv->buffers_queued->data);
1996 /* no touching if upstream or parsing provided time */
1997 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
1998 GST_LOG_OBJECT (parse, "buffer has time %" GST_TIME_FORMAT,
1999 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
2000 } else if (GST_CLOCK_TIME_IS_VALID (parse->priv->last_ts) &&
2001 GST_BUFFER_DURATION_IS_VALID (buf)) {
2002 if (G_LIKELY (GST_BUFFER_DURATION (buf) <= parse->priv->last_ts))
2003 parse->priv->last_ts -= GST_BUFFER_DURATION (buf);
2005 parse->priv->last_ts = 0;
2006 GST_BUFFER_TIMESTAMP (buf) = parse->priv->last_ts;
2007 GST_LOG_OBJECT (parse, "applied time %" GST_TIME_FORMAT,
2008 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
2010 /* no idea, very bad */
2011 GST_WARNING_OBJECT (parse, "could not determine time for buffer");
2014 parse->priv->last_ts = GST_BUFFER_TIMESTAMP (buf);
2016 /* reverse order for ascending sending */
2017 /* send downstream at keyframe not preceded by a keyframe
2018 * (e.g. that should identify start of collection of IDR nals) */
2019 if (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT)) {
2021 ret = gst_base_parse_send_buffers (parse);
2022 /* if a problem, throw all to sending */
2023 if (ret != GST_FLOW_OK) {
2024 parse->priv->buffers_send =
2025 g_slist_reverse (parse->priv->buffers_queued);
2026 parse->priv->buffers_queued = NULL;
2035 seen_key |= !GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
2037 parse->priv->buffers_send =
2038 g_slist_prepend (parse->priv->buffers_send, buf);
2039 parse->priv->buffers_queued =
2040 g_slist_delete_link (parse->priv->buffers_queued,
2041 parse->priv->buffers_queued);
2044 /* audio may have all marked as keyframe, so arrange to send here */
2046 ret = gst_base_parse_send_buffers (parse);
2048 /* any trailing unused no longer usable (ideally none) */
2049 if (G_UNLIKELY (gst_adapter_available (parse->priv->adapter))) {
2050 GST_DEBUG_OBJECT (parse, "discarding %d trailing bytes",
2051 gst_adapter_available (parse->priv->adapter));
2052 gst_adapter_clear (parse->priv->adapter);
2058 /* small helper that checks whether we have been trying to resync too long */
2059 static inline GstFlowReturn
2060 gst_base_parse_check_sync (GstBaseParse * parse)
2062 if (G_UNLIKELY (parse->priv->discont &&
2063 parse->priv->offset - parse->priv->sync_offset > 2 * 1024 * 1024)) {
2064 GST_ELEMENT_ERROR (parse, STREAM, DECODE,
2065 ("Failed to parse stream"), (NULL));
2066 return GST_FLOW_ERROR;
2072 static GstFlowReturn
2073 gst_base_parse_chain (GstPad * pad, GstBuffer * buffer)
2075 GstBaseParseClass *bclass;
2076 GstBaseParse *parse;
2077 GstFlowReturn ret = GST_FLOW_OK;
2078 GstBuffer *outbuf = NULL;
2079 GstBuffer *tmpbuf = NULL;
2083 guint old_min_size = 0, min_size, av;
2084 GstClockTime timestamp;
2085 GstBaseParseFrame _frame = { 0, };
2086 GstBaseParseFrame *frame;
2088 parse = GST_BASE_PARSE (GST_OBJECT_PARENT (pad));
2089 bclass = GST_BASE_PARSE_GET_CLASS (parse);
2092 if (G_LIKELY (buffer)) {
2093 GST_LOG_OBJECT (parse, "buffer size: %d, offset = %" G_GINT64_FORMAT,
2094 GST_BUFFER_SIZE (buffer), GST_BUFFER_OFFSET (buffer));
2095 if (G_UNLIKELY (GST_BASE_PARSE_PASSTHROUGH (parse))) {
2096 frame->buffer = gst_buffer_make_metadata_writable (buffer);
2097 return gst_base_parse_push_frame (parse, frame);
2099 /* upstream feeding us in reverse playback;
2100 * gather each fragment, then process it in single run */
2101 if (parse->segment.rate < 0.0) {
2102 if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT))) {
2103 GST_DEBUG_OBJECT (parse, "buffer starts new reverse playback fragment");
2104 ret = gst_base_parse_process_fragment (parse, FALSE);
2106 gst_adapter_push (parse->priv->adapter, buffer);
2109 gst_adapter_push (parse->priv->adapter, buffer);
2112 /* Parse and push as many frames as possible */
2113 /* Stop either when adapter is empty or we are flushing */
2114 while (!parse->priv->flushing) {
2117 tmpbuf = gst_buffer_new ();
2120 /* Synchronization loop */
2122 min_size = MAX (parse->priv->min_frame_size, fsize);
2123 av = gst_adapter_available (parse->priv->adapter);
2125 /* loop safety check */
2126 if (G_UNLIKELY (old_min_size >= min_size))
2128 old_min_size = min_size;
2130 if (G_UNLIKELY (parse->priv->drain)) {
2132 GST_DEBUG_OBJECT (parse, "draining, data left: %d", min_size);
2133 if (G_UNLIKELY (!min_size)) {
2134 gst_buffer_unref (tmpbuf);
2139 /* Collect at least min_frame_size bytes */
2140 if (av < min_size) {
2141 GST_DEBUG_OBJECT (parse, "not enough data available (only %d bytes)",
2143 gst_buffer_unref (tmpbuf);
2147 /* always pass all available data */
2148 data = gst_adapter_peek (parse->priv->adapter, av);
2149 GST_BUFFER_DATA (tmpbuf) = (guint8 *) data;
2150 GST_BUFFER_SIZE (tmpbuf) = min_size;
2151 GST_BUFFER_OFFSET (tmpbuf) = parse->priv->offset;
2152 GST_BUFFER_FLAG_SET (tmpbuf, GST_MINI_OBJECT_FLAG_READONLY);
2154 if (parse->priv->discont) {
2155 GST_DEBUG_OBJECT (parse, "marking DISCONT");
2156 GST_BUFFER_FLAG_SET (tmpbuf, GST_BUFFER_FLAG_DISCONT);
2160 gst_base_parse_frame_update (parse, frame, tmpbuf);
2161 res = bclass->check_valid_frame (parse, frame, &fsize, &skip);
2162 gst_buffer_replace (&frame->buffer, NULL);
2164 if (gst_adapter_available (parse->priv->adapter) < fsize) {
2165 GST_DEBUG_OBJECT (parse,
2166 "found valid frame but not enough data available (only %d bytes)",
2167 gst_adapter_available (parse->priv->adapter));
2168 gst_buffer_unref (tmpbuf);
2171 GST_LOG_OBJECT (parse, "valid frame of size %d at pos %d", fsize, skip);
2175 /* subclass didn't touch this value. By default we skip 1 byte */
2179 GST_LOG_OBJECT (parse, "finding sync, skipping %d bytes", skip);
2180 if (parse->segment.rate < 0.0 && !parse->priv->buffers_queued) {
2181 /* reverse playback, and no frames found yet, so we are skipping
2182 * the leading part of a fragment, which may form the tail of
2183 * fragment coming later, hopefully subclass skips efficiently ... */
2184 timestamp = gst_adapter_prev_timestamp (parse->priv->adapter, NULL);
2185 outbuf = gst_adapter_take_buffer (parse->priv->adapter, skip);
2186 outbuf = gst_buffer_make_metadata_writable (outbuf);
2187 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
2188 parse->priv->buffers_pending =
2189 g_slist_prepend (parse->priv->buffers_pending, outbuf);
2192 gst_adapter_flush (parse->priv->adapter, skip);
2194 parse->priv->offset += skip;
2195 if (!parse->priv->discont)
2196 parse->priv->sync_offset = parse->priv->offset;
2197 parse->priv->discont = TRUE;
2198 /* something changed least; nullify loop check */
2201 /* skip == 0 should imply subclass set min_size to need more data;
2202 * we check this shortly */
2203 if ((ret = gst_base_parse_check_sync (parse)) != GST_FLOW_OK) {
2204 gst_buffer_unref (tmpbuf);
2208 gst_buffer_unref (tmpbuf);
2212 /* Subclass found the sync, but still wants to skip some data */
2213 GST_LOG_OBJECT (parse, "skipping %d bytes", skip);
2214 gst_adapter_flush (parse->priv->adapter, skip);
2215 parse->priv->offset += skip;
2218 /* Grab lock to prevent a race with FLUSH_START handler */
2219 GST_PAD_STREAM_LOCK (parse->srcpad);
2221 /* FLUSH_START event causes the "flushing" flag to be set. In this
2222 * case we can leave the frame pushing loop */
2223 if (parse->priv->flushing) {
2224 GST_PAD_STREAM_UNLOCK (parse->srcpad);
2228 /* move along with upstream timestamp (if any),
2229 * but interpolate in between */
2230 timestamp = gst_adapter_prev_timestamp (parse->priv->adapter, NULL);
2231 if (GST_CLOCK_TIME_IS_VALID (timestamp) &&
2232 (parse->priv->prev_ts != timestamp)) {
2233 parse->priv->prev_ts = parse->priv->next_ts = timestamp;
2236 /* FIXME: Would it be more efficient to make a subbuffer instead? */
2237 outbuf = gst_adapter_take_buffer (parse->priv->adapter, fsize);
2238 outbuf = gst_buffer_make_metadata_writable (outbuf);
2240 /* Subclass may want to know the data offset */
2241 GST_BUFFER_OFFSET (outbuf) = parse->priv->offset;
2242 parse->priv->offset += fsize;
2243 GST_BUFFER_TIMESTAMP (outbuf) = GST_CLOCK_TIME_NONE;
2244 GST_BUFFER_DURATION (outbuf) = GST_CLOCK_TIME_NONE;
2246 frame->buffer = outbuf;
2247 ret = gst_base_parse_handle_and_push_frame (parse, bclass, frame);
2248 GST_PAD_STREAM_UNLOCK (parse->srcpad);
2250 if (ret != GST_FLOW_OK) {
2251 GST_LOG_OBJECT (parse, "push returned %d", ret);
2257 GST_LOG_OBJECT (parse, "chain leaving");
2263 GST_ELEMENT_ERROR (parse, STREAM, FAILED, (NULL),
2264 ("min_size evolution %d -> %d; breaking to avoid looping",
2265 old_min_size, min_size));
2266 return GST_FLOW_ERROR;
2270 /* pull @size bytes at current offset,
2271 * i.e. at least try to and possibly return a shorter buffer if near the end */
2272 static GstFlowReturn
2273 gst_base_parse_pull_range (GstBaseParse * parse, guint size,
2274 GstBuffer ** buffer)
2276 GstFlowReturn ret = GST_FLOW_OK;
2278 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
2280 /* Caching here actually makes much less difference than one would expect.
2281 * We do it mainly to avoid pulling buffers of 1 byte all the time */
2282 if (parse->priv->cache) {
2283 gint64 cache_offset = GST_BUFFER_OFFSET (parse->priv->cache);
2284 gint cache_size = GST_BUFFER_SIZE (parse->priv->cache);
2286 if (cache_offset <= parse->priv->offset &&
2287 (parse->priv->offset + size) <= (cache_offset + cache_size)) {
2288 *buffer = gst_buffer_create_sub (parse->priv->cache,
2289 parse->priv->offset - cache_offset, size);
2290 GST_BUFFER_OFFSET (*buffer) = parse->priv->offset;
2293 /* not enough data in the cache, free cache and get a new one */
2294 gst_buffer_unref (parse->priv->cache);
2295 parse->priv->cache = NULL;
2298 /* refill the cache */
2300 gst_pad_pull_range (parse->sinkpad, parse->priv->offset, MAX (size,
2301 64 * 1024), &parse->priv->cache);
2302 if (ret != GST_FLOW_OK) {
2303 parse->priv->cache = NULL;
2307 if (GST_BUFFER_SIZE (parse->priv->cache) >= size) {
2308 *buffer = gst_buffer_create_sub (parse->priv->cache, 0, size);
2309 GST_BUFFER_OFFSET (*buffer) = parse->priv->offset;
2313 /* Not possible to get enough data, try a last time with
2314 * requesting exactly the size we need */
2315 gst_buffer_unref (parse->priv->cache);
2316 parse->priv->cache = NULL;
2318 ret = gst_pad_pull_range (parse->sinkpad, parse->priv->offset, size,
2319 &parse->priv->cache);
2321 if (ret != GST_FLOW_OK) {
2322 GST_DEBUG_OBJECT (parse, "pull_range returned %d", ret);
2327 if (GST_BUFFER_SIZE (parse->priv->cache) < size) {
2328 GST_DEBUG_OBJECT (parse, "Returning short buffer at offset %"
2329 G_GUINT64_FORMAT ": wanted %u bytes, got %u bytes", parse->priv->offset,
2330 size, GST_BUFFER_SIZE (parse->priv->cache));
2332 *buffer = parse->priv->cache;
2333 parse->priv->cache = NULL;
2338 *buffer = gst_buffer_create_sub (parse->priv->cache, 0, size);
2339 GST_BUFFER_OFFSET (*buffer) = parse->priv->offset;
2344 static GstFlowReturn
2345 gst_base_parse_handle_previous_fragment (GstBaseParse * parse)
2348 GstClockTime ts = 0;
2352 GST_DEBUG_OBJECT (parse, "fragment ended; last_ts = %" GST_TIME_FORMAT
2353 ", last_offset = %" G_GINT64_FORMAT, GST_TIME_ARGS (parse->priv->last_ts),
2354 parse->priv->last_offset);
2356 if (!parse->priv->last_offset || parse->priv->last_ts <= parse->segment.start) {
2357 GST_DEBUG_OBJECT (parse, "past start of segment %" GST_TIME_FORMAT,
2358 GST_TIME_ARGS (parse->segment.start));
2359 ret = GST_FLOW_UNEXPECTED;
2363 /* last fragment started at last_offset / last_ts;
2364 * seek back 10s capped at 1MB */
2365 if (parse->priv->last_ts >= 10 * GST_SECOND)
2366 ts = parse->priv->last_ts - 10 * GST_SECOND;
2367 /* if we are exact now, we will be more so going backwards */
2368 if (parse->priv->exact_position) {
2369 offset = gst_base_parse_find_offset (parse, ts, TRUE, NULL);
2371 GstFormat dstformat = GST_FORMAT_BYTES;
2373 if (!gst_pad_query_convert (parse->srcpad, GST_FORMAT_TIME, ts,
2374 &dstformat, &offset)) {
2375 GST_DEBUG_OBJECT (parse, "conversion failed, only BYTE based");
2378 offset = CLAMP (offset, parse->priv->last_offset - 1024 * 1024,
2379 parse->priv->last_offset - 1024);
2380 offset = MAX (0, offset);
2382 GST_DEBUG_OBJECT (parse, "next fragment from offset %" G_GINT64_FORMAT,
2384 parse->priv->offset = offset;
2386 ret = gst_base_parse_pull_range (parse, parse->priv->last_offset - offset,
2388 if (ret != GST_FLOW_OK)
2391 /* offset will increase again as fragment is processed/parsed */
2392 parse->priv->last_offset = offset;
2394 gst_adapter_push (parse->priv->adapter, buffer);
2395 ret = gst_base_parse_process_fragment (parse, FALSE);
2396 if (ret != GST_FLOW_OK)
2399 /* force previous fragment */
2400 parse->priv->offset = -1;
2407 * pull and scan for next frame starting from current offset
2408 * ajusts sync, drain and offset going along */
2409 static GstFlowReturn
2410 gst_base_parse_scan_frame (GstBaseParse * parse, GstBaseParseClass * klass,
2411 GstBaseParseFrame * frame, gboolean full)
2413 GstBuffer *buffer, *outbuf;
2414 GstFlowReturn ret = GST_FLOW_OK;
2415 guint fsize = 1, min_size, old_min_size = 0;
2418 g_return_val_if_fail (frame != NULL, GST_FLOW_ERROR);
2420 GST_LOG_OBJECT (parse, "scanning for frame at offset %" G_GUINT64_FORMAT
2421 " (%#" G_GINT64_MODIFIER "x)", parse->priv->offset, parse->priv->offset);
2426 min_size = MAX (parse->priv->min_frame_size, fsize);
2427 /* loop safety check */
2428 if (G_UNLIKELY (old_min_size >= min_size))
2430 old_min_size = min_size;
2432 ret = gst_base_parse_pull_range (parse, min_size, &buffer);
2433 if (ret != GST_FLOW_OK)
2436 if (parse->priv->discont) {
2437 GST_DEBUG_OBJECT (parse, "marking DISCONT");
2438 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
2441 /* if we got a short read, inform subclass we are draining leftover
2442 * and no more is to be expected */
2443 if (GST_BUFFER_SIZE (buffer) < min_size)
2444 parse->priv->drain = TRUE;
2447 gst_base_parse_frame_update (parse, frame, buffer);
2448 res = klass->check_valid_frame (parse, frame, &fsize, &skip);
2449 gst_buffer_replace (&frame->buffer, NULL);
2451 parse->priv->drain = FALSE;
2452 GST_LOG_OBJECT (parse, "valid frame of size %d at pos %d", fsize, skip);
2455 parse->priv->drain = FALSE;
2459 GST_LOG_OBJECT (parse, "finding sync, skipping %d bytes", skip);
2460 if (full && parse->segment.rate < 0.0 && !parse->priv->buffers_queued) {
2461 /* reverse playback, and no frames found yet, so we are skipping
2462 * the leading part of a fragment, which may form the tail of
2463 * fragment coming later, hopefully subclass skips efficiently ... */
2464 outbuf = gst_buffer_create_sub (buffer, 0, skip);
2465 parse->priv->buffers_pending =
2466 g_slist_prepend (parse->priv->buffers_pending, outbuf);
2469 parse->priv->offset += skip;
2470 if (!parse->priv->discont)
2471 parse->priv->sync_offset = parse->priv->offset;
2472 parse->priv->discont = TRUE;
2473 /* something changed least; nullify loop check */
2476 /* skip == 0 should imply subclass set min_size to need more data;
2477 * we check this shortly */
2478 GST_DEBUG_OBJECT (parse, "finding sync...");
2479 gst_buffer_unref (buffer);
2480 if ((ret = gst_base_parse_check_sync (parse)) != GST_FLOW_OK) {
2485 /* Does the subclass want to skip too? */
2487 parse->priv->offset += skip;
2491 if (fsize + skip <= GST_BUFFER_SIZE (buffer)) {
2492 outbuf = gst_buffer_create_sub (buffer, skip, fsize);
2493 GST_BUFFER_OFFSET (outbuf) = GST_BUFFER_OFFSET (buffer) + skip;
2494 GST_BUFFER_TIMESTAMP (outbuf) = GST_CLOCK_TIME_NONE;
2495 gst_buffer_unref (buffer);
2497 gst_buffer_unref (buffer);
2498 ret = gst_base_parse_pull_range (parse, fsize, &outbuf);
2499 if (ret != GST_FLOW_OK)
2501 if (GST_BUFFER_SIZE (outbuf) < fsize) {
2502 gst_buffer_unref (outbuf);
2503 ret = GST_FLOW_UNEXPECTED;
2507 parse->priv->offset += fsize;
2509 frame->buffer = outbuf;
2517 GST_ELEMENT_ERROR (parse, STREAM, FAILED, (NULL),
2518 ("min_size evolution %d -> %d; breaking to avoid looping",
2519 old_min_size, min_size));
2520 return GST_FLOW_ERROR;
2524 /* Loop that is used in pull mode to retrieve data from upstream */
2526 gst_base_parse_loop (GstPad * pad)
2528 GstBaseParse *parse;
2529 GstBaseParseClass *klass;
2530 GstFlowReturn ret = GST_FLOW_OK;
2531 GstBaseParseFrame frame = { 0, };
2533 parse = GST_BASE_PARSE (gst_pad_get_parent (pad));
2534 klass = GST_BASE_PARSE_GET_CLASS (parse);
2536 /* reverse playback:
2537 * first fragment (closest to stop time) is handled normally below,
2538 * then we pull in fragments going backwards */
2539 if (parse->segment.rate < 0.0) {
2540 /* check if we jumped back to a previous fragment,
2541 * which is a post-first fragment */
2542 if (parse->priv->offset < 0) {
2543 ret = gst_base_parse_handle_previous_fragment (parse);
2548 ret = gst_base_parse_scan_frame (parse, klass, &frame, TRUE);
2549 if (ret != GST_FLOW_OK)
2552 /* This always cleans up frame, even if error occurs */
2553 ret = gst_base_parse_handle_and_push_frame (parse, klass, &frame);
2555 /* eat expected eos signalling past segment in reverse playback */
2556 if (parse->segment.rate < 0.0 && ret == GST_FLOW_UNEXPECTED &&
2557 parse->segment.last_stop >= parse->segment.stop) {
2558 GST_DEBUG_OBJECT (parse, "downstream has reached end of segment");
2559 /* push what was accumulated during loop run */
2560 gst_base_parse_process_fragment (parse, TRUE);
2561 /* force previous fragment */
2562 parse->priv->offset = -1;
2567 if (ret == GST_FLOW_UNEXPECTED)
2569 else if (ret != GST_FLOW_OK)
2572 gst_object_unref (parse);
2578 ret = GST_FLOW_UNEXPECTED;
2579 GST_DEBUG_OBJECT (parse, "eos");
2584 gboolean push_eos = FALSE;
2586 GST_DEBUG_OBJECT (parse, "pausing task, reason %s",
2587 gst_flow_get_name (ret));
2588 gst_pad_pause_task (parse->sinkpad);
2590 if (ret == GST_FLOW_UNEXPECTED) {
2591 /* handle end-of-stream/segment */
2592 if (parse->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2595 if ((stop = parse->segment.stop) == -1)
2596 stop = parse->segment.duration;
2598 GST_DEBUG_OBJECT (parse, "sending segment_done");
2600 gst_element_post_message
2601 (GST_ELEMENT_CAST (parse),
2602 gst_message_new_segment_done (GST_OBJECT_CAST (parse),
2603 GST_FORMAT_TIME, stop));
2605 /* If we STILL have zero frames processed, fire an error */
2606 if (parse->priv->framecount == 0) {
2607 GST_ELEMENT_ERROR (parse, STREAM, WRONG_TYPE,
2608 ("No valid frames found before end of stream"), (NULL));
2612 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_UNEXPECTED) {
2613 /* for fatal errors we post an error message, wrong-state is
2614 * not fatal because it happens due to flushes and only means
2615 * that we should stop now. */
2616 GST_ELEMENT_ERROR (parse, STREAM, FAILED, (NULL),
2617 ("streaming stopped, reason %s", gst_flow_get_name (ret)));
2621 /* newsegment before eos */
2622 if (parse->priv->pending_segment) {
2623 gst_pad_push_event (parse->srcpad, parse->priv->pending_segment);
2624 parse->priv->pending_segment = NULL;
2626 gst_pad_push_event (parse->srcpad, gst_event_new_eos ());
2628 gst_object_unref (parse);
2633 gst_base_parse_sink_activate (GstPad * sinkpad)
2635 GstBaseParse *parse;
2636 gboolean result = TRUE;
2638 parse = GST_BASE_PARSE (gst_pad_get_parent (sinkpad));
2640 GST_DEBUG_OBJECT (parse, "sink activate");
2642 if (gst_pad_check_pull_range (sinkpad)) {
2643 GST_DEBUG_OBJECT (parse, "trying to activate in pull mode");
2644 result = gst_pad_activate_pull (sinkpad, TRUE);
2646 GST_DEBUG_OBJECT (parse, "trying to activate in push mode");
2647 result = gst_pad_activate_push (sinkpad, TRUE);
2650 GST_DEBUG_OBJECT (parse, "sink activate return %d", result);
2651 gst_object_unref (parse);
2656 gst_base_parse_activate (GstBaseParse * parse, gboolean active)
2658 GstBaseParseClass *klass;
2659 gboolean result = FALSE;
2661 GST_DEBUG_OBJECT (parse, "activate %d", active);
2663 klass = GST_BASE_PARSE_GET_CLASS (parse);
2666 if (parse->priv->pad_mode == GST_ACTIVATE_NONE && klass->start)
2667 result = klass->start (parse);
2669 /* We must make sure streaming has finished before resetting things
2670 * and calling the ::stop vfunc */
2671 GST_PAD_STREAM_LOCK (parse->sinkpad);
2672 GST_PAD_STREAM_UNLOCK (parse->sinkpad);
2674 if (parse->priv->pad_mode != GST_ACTIVATE_NONE && klass->stop)
2675 result = klass->stop (parse);
2677 parse->priv->pad_mode = GST_ACTIVATE_NONE;
2679 GST_DEBUG_OBJECT (parse, "activate return: %d", result);
2684 gst_base_parse_sink_activate_push (GstPad * pad, gboolean active)
2686 gboolean result = TRUE;
2687 GstBaseParse *parse;
2689 parse = GST_BASE_PARSE (gst_pad_get_parent (pad));
2691 GST_DEBUG_OBJECT (parse, "sink activate push %d", active);
2693 result = gst_base_parse_activate (parse, active);
2696 parse->priv->pad_mode = active ? GST_ACTIVATE_PUSH : GST_ACTIVATE_NONE;
2698 GST_DEBUG_OBJECT (parse, "sink activate push return: %d", result);
2700 gst_object_unref (parse);
2705 gst_base_parse_sink_activate_pull (GstPad * sinkpad, gboolean active)
2707 gboolean result = FALSE;
2708 GstBaseParse *parse;
2710 parse = GST_BASE_PARSE (gst_pad_get_parent (sinkpad));
2712 GST_DEBUG_OBJECT (parse, "activate pull %d", active);
2714 result = gst_base_parse_activate (parse, active);
2718 parse->priv->pending_segment = gst_event_new_new_segment (FALSE,
2719 parse->segment.rate, parse->segment.format,
2720 parse->segment.start, parse->segment.stop, parse->segment.last_stop);
2722 gst_pad_start_task (sinkpad, (GstTaskFunction) gst_base_parse_loop,
2725 result &= gst_pad_stop_task (sinkpad);
2730 parse->priv->pad_mode = active ? GST_ACTIVATE_PULL : GST_ACTIVATE_NONE;
2732 GST_DEBUG_OBJECT (parse, "sink activate pull return: %d", result);
2734 gst_object_unref (parse);
2740 * gst_base_parse_set_duration:
2741 * @parse: #GstBaseParse.
2743 * @duration: duration value.
2744 * @interval: how often to update the duration estimate based on bitrate, or 0.
2746 * Sets the duration of the currently playing media. Subclass can use this
2747 * when it is able to determine duration and/or notices a change in the media
2748 * duration. Alternatively, if @interval is non-zero (default), then stream
2749 * duration is determined based on estimated bitrate, and updated every @interval
2755 gst_base_parse_set_duration (GstBaseParse * parse,
2756 GstFormat fmt, gint64 duration, gint interval)
2758 g_return_if_fail (parse != NULL);
2760 if (parse->priv->upstream_has_duration) {
2761 GST_DEBUG_OBJECT (parse, "using upstream duration; discarding update");
2765 if (duration != parse->priv->duration) {
2768 m = gst_message_new_duration (GST_OBJECT (parse), fmt, duration);
2769 gst_element_post_message (GST_ELEMENT (parse), m);
2771 /* TODO: what about duration tag? */
2773 parse->priv->duration = duration;
2774 parse->priv->duration_fmt = fmt;
2775 GST_DEBUG_OBJECT (parse, "set duration: %" G_GINT64_FORMAT, duration);
2776 if (fmt == GST_FORMAT_TIME && GST_CLOCK_TIME_IS_VALID (duration)) {
2777 if (interval != 0) {
2778 GST_DEBUG_OBJECT (parse, "valid duration provided, disabling estimate");
2782 GST_DEBUG_OBJECT (parse, "set update interval: %d", interval);
2783 parse->priv->update_interval = interval;
2789 * gst_base_parse_set_average_bitrate:
2790 * @parse: #GstBaseParse.
2791 * @bitrate: average bitrate in bits/second
2793 * Optionally sets the average bitrate detected in media (if non-zero),
2794 * e.g. based on metadata, as it will be posted to the application.
2796 * By default, announced average bitrate is estimated. The average bitrate
2797 * is used to estimate the total duration of the stream and to estimate
2798 * a seek position, if there's no index and #GST_BASE_PARSE_FORMAT_FLAG_SYNCABLE
2804 gst_base_parse_set_average_bitrate (GstBaseParse * parse, guint bitrate)
2806 parse->priv->bitrate = bitrate;
2807 GST_DEBUG_OBJECT (parse, "bitrate %u", bitrate);
2811 * gst_base_parse_set_min_frame_size:
2812 * @parse: #GstBaseParse.
2813 * @min_size: Minimum size of the data that this base class should give to
2816 * Subclass can use this function to tell the base class that it needs to
2817 * give at least #min_size buffers.
2822 gst_base_parse_set_min_frame_size (GstBaseParse * parse, guint min_size)
2824 g_return_if_fail (parse != NULL);
2826 parse->priv->min_frame_size = min_size;
2827 GST_LOG_OBJECT (parse, "set frame_min_size: %d", min_size);
2831 * gst_base_parse_set_format_flags:
2832 * @parse: #GstBaseParse
2833 * @flags: the #GstBaseParseFormatFlags to set
2835 * Set flags describing characteristics of parsed format. This overrides
2836 * any previous flags set (ie. it's not a bitwise OR operation).
2841 gst_base_parse_set_format_flags (GstBaseParse * parse,
2842 GstBaseParseFormatFlags flags)
2844 g_return_if_fail (parse != NULL);
2846 GST_LOG_OBJECT (parse, "setting flags 0x%02x", flags);
2847 parse->priv->format_flags = flags;
2851 * gst_base_parse_set_frame_props:
2852 * @parse: the #GstBaseParse to set
2853 * @fps_num: frames per second (numerator).
2854 * @fps_den: frames per second (denominator).
2855 * @lead_in: frames needed before a segment for subsequent decode
2856 * @lead_out: frames needed after a segment
2858 * If frames per second is configured, parser can take care of buffer duration
2859 * and timestamping. When performing segment clipping, or seeking to a specific
2860 * location, a corresponding decoder might need an initial @lead_in and a
2861 * following @lead_out number of frames to ensure the desired segment is
2862 * entirely filled upon decoding.
2867 gst_base_parse_set_frame_props (GstBaseParse * parse, guint fps_num,
2868 guint fps_den, guint lead_in, guint lead_out)
2870 g_return_if_fail (parse != NULL);
2872 parse->priv->fps_num = fps_num;
2873 parse->priv->fps_den = fps_den;
2874 if (!fps_num || !fps_den) {
2875 GST_DEBUG_OBJECT (parse, "invalid fps (%d/%d), ignoring parameters",
2877 fps_num = fps_den = 0;
2878 parse->priv->frame_duration = GST_CLOCK_TIME_NONE;
2879 parse->priv->lead_in = parse->priv->lead_out = 0;
2880 parse->priv->lead_in_ts = parse->priv->lead_out_ts = 0;
2882 parse->priv->frame_duration =
2883 gst_util_uint64_scale (GST_SECOND, fps_den, fps_num);
2884 parse->priv->lead_in = lead_in;
2885 parse->priv->lead_out = lead_out;
2886 parse->priv->lead_in_ts =
2887 gst_util_uint64_scale (GST_SECOND, fps_den * lead_in, fps_num);
2888 parse->priv->lead_out_ts =
2889 gst_util_uint64_scale (GST_SECOND, fps_den * lead_out, fps_num);
2890 /* aim for about 1.5s to estimate duration */
2891 if (parse->priv->update_interval < 0) {
2892 parse->priv->update_interval = fps_num * 3 / (fps_den * 2);
2893 GST_LOG_OBJECT (parse, "estimated update interval to %d frames",
2894 parse->priv->update_interval);
2897 GST_LOG_OBJECT (parse, "set fps: %d/%d => duration: %" G_GINT64_FORMAT " ms",
2898 fps_num, fps_den, parse->priv->frame_duration / GST_MSECOND);
2899 GST_LOG_OBJECT (parse, "set lead in: %d frames = %" G_GUINT64_FORMAT " ms, "
2900 "lead out: %d frames = %" G_GUINT64_FORMAT " ms",
2901 lead_in, parse->priv->lead_in_ts / GST_MSECOND,
2902 lead_out, parse->priv->lead_out_ts / GST_MSECOND);
2906 gst_base_parse_get_duration (GstBaseParse * parse, GstFormat format,
2907 GstClockTime * duration)
2909 gboolean res = FALSE;
2911 g_return_val_if_fail (duration != NULL, FALSE);
2913 *duration = GST_CLOCK_TIME_NONE;
2914 if (parse->priv->duration != -1 && format == parse->priv->duration_fmt) {
2915 GST_LOG_OBJECT (parse, "using provided duration");
2916 *duration = parse->priv->duration;
2918 } else if (parse->priv->duration != -1) {
2919 GST_LOG_OBJECT (parse, "converting provided duration");
2920 res = gst_base_parse_convert (parse, parse->priv->duration_fmt,
2921 parse->priv->duration, format, (gint64 *) duration);
2922 } else if (format == GST_FORMAT_TIME && parse->priv->estimated_duration != -1) {
2923 GST_LOG_OBJECT (parse, "using estimated duration");
2924 *duration = parse->priv->estimated_duration;
2928 GST_LOG_OBJECT (parse, "res: %d, duration %" GST_TIME_FORMAT, res,
2929 GST_TIME_ARGS (*duration));
2933 static const GstQueryType *
2934 gst_base_parse_get_querytypes (GstPad * pad)
2936 static const GstQueryType list[] = {
2949 gst_base_parse_query (GstPad * pad, GstQuery * query)
2951 GstBaseParse *parse;
2952 gboolean res = FALSE;
2954 parse = GST_BASE_PARSE (GST_PAD_PARENT (pad));
2956 GST_LOG_OBJECT (parse, "handling query: %" GST_PTR_FORMAT, query);
2958 switch (GST_QUERY_TYPE (query)) {
2959 case GST_QUERY_POSITION:
2964 GST_DEBUG_OBJECT (parse, "position query");
2965 gst_query_parse_position (query, &format, NULL);
2967 GST_OBJECT_LOCK (parse);
2968 if (format == GST_FORMAT_BYTES) {
2969 dest_value = parse->priv->offset;
2971 } else if (format == parse->segment.format &&
2972 GST_CLOCK_TIME_IS_VALID (parse->segment.last_stop)) {
2973 dest_value = parse->segment.last_stop;
2976 GST_OBJECT_UNLOCK (parse);
2979 gst_query_set_position (query, format, dest_value);
2981 res = gst_pad_query_default (pad, query);
2983 /* no precise result, upstream no idea either, then best estimate */
2984 /* priv->offset is updated in both PUSH/PULL modes */
2985 res = gst_base_parse_convert (parse,
2986 GST_FORMAT_BYTES, parse->priv->offset, format, &dest_value);
2991 case GST_QUERY_DURATION:
2994 GstClockTime duration;
2996 GST_DEBUG_OBJECT (parse, "duration query");
2997 gst_query_parse_duration (query, &format, NULL);
2999 /* consult upstream */
3000 res = gst_pad_query_default (pad, query);
3002 /* otherwise best estimate from us */
3004 res = gst_base_parse_get_duration (parse, format, &duration);
3006 gst_query_set_duration (query, format, duration);
3010 case GST_QUERY_SEEKING:
3013 GstClockTime duration = GST_CLOCK_TIME_NONE;
3014 gboolean seekable = FALSE;
3016 GST_DEBUG_OBJECT (parse, "seeking query");
3017 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
3019 /* consult upstream */
3020 res = gst_pad_query_default (pad, query);
3022 /* we may be able to help if in TIME */
3023 if (fmt == GST_FORMAT_TIME && gst_base_parse_is_seekable (parse)) {
3024 gst_query_parse_seeking (query, &fmt, &seekable, NULL, NULL);
3025 /* already OK if upstream takes care */
3026 GST_LOG_OBJECT (parse, "upstream handled %d, seekable %d",
3028 if (!(res && seekable)) {
3029 if (!gst_base_parse_get_duration (parse, GST_FORMAT_TIME, &duration)
3030 || duration == -1) {
3031 /* seekable if we still have a chance to get duration later on */
3033 parse->priv->upstream_seekable && parse->priv->update_interval;
3035 seekable = parse->priv->upstream_seekable;
3036 GST_LOG_OBJECT (parse, "already determine upstream seekabled: %d",
3039 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable, 0, duration);
3045 case GST_QUERY_FORMATS:
3046 gst_query_set_formatsv (query, 3, fmtlist);
3049 case GST_QUERY_CONVERT:
3051 GstFormat src_format, dest_format;
3052 gint64 src_value, dest_value;
3054 gst_query_parse_convert (query, &src_format, &src_value,
3055 &dest_format, &dest_value);
3057 res = gst_base_parse_convert (parse, src_format, src_value,
3058 dest_format, &dest_value);
3060 gst_query_set_convert (query, src_format, src_value,
3061 dest_format, dest_value);
3066 res = gst_pad_query_default (pad, query);
3072 /* scans for a cluster start from @pos,
3073 * return GST_FLOW_OK and frame position/time in @pos/@time if found */
3074 static GstFlowReturn
3075 gst_base_parse_find_frame (GstBaseParse * parse, gint64 * pos,
3076 GstClockTime * time, GstClockTime * duration)
3078 GstBaseParseClass *klass;
3080 gboolean orig_drain, orig_discont;
3081 GstFlowReturn ret = GST_FLOW_OK;
3082 GstBuffer *buf = NULL;
3083 GstBaseParseFrame frame = { 0, };
3085 g_return_val_if_fail (GST_FLOW_ERROR, pos != NULL);
3086 g_return_val_if_fail (GST_FLOW_ERROR, time != NULL);
3087 g_return_val_if_fail (GST_FLOW_ERROR, duration != NULL);
3089 klass = GST_BASE_PARSE_GET_CLASS (parse);
3091 *time = GST_CLOCK_TIME_NONE;
3092 *duration = GST_CLOCK_TIME_NONE;
3095 orig_offset = parse->priv->offset;
3096 orig_discont = parse->priv->discont;
3097 orig_drain = parse->priv->drain;
3099 GST_DEBUG_OBJECT (parse, "scanning for frame starting at %" G_GINT64_FORMAT
3100 " (%#" G_GINT64_MODIFIER "x)", *pos, *pos);
3102 /* jump elsewhere and locate next frame */
3103 parse->priv->offset = *pos;
3104 ret = gst_base_parse_scan_frame (parse, klass, &frame, FALSE);
3105 if (ret != GST_FLOW_OK)
3109 GST_LOG_OBJECT (parse,
3110 "peek parsing frame at offset %" G_GUINT64_FORMAT
3111 " (%#" G_GINT64_MODIFIER "x) of size %d",
3112 GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET (buf), GST_BUFFER_SIZE (buf));
3114 /* get offset first, subclass parsing might dump other stuff in there */
3115 *pos = GST_BUFFER_OFFSET (buf);
3116 ret = klass->parse_frame (parse, &frame);
3119 /* but it should provide proper time */
3120 *time = GST_BUFFER_TIMESTAMP (buf);
3121 *duration = GST_BUFFER_DURATION (buf);
3122 gst_base_parse_frame_clear (parse, &frame);
3124 GST_LOG_OBJECT (parse,
3125 "frame with time %" GST_TIME_FORMAT " at offset %" G_GINT64_FORMAT,
3126 GST_TIME_ARGS (*time), *pos);
3130 parse->priv->offset = orig_offset;
3131 parse->priv->discont = orig_discont;
3132 parse->priv->drain = orig_drain;
3137 /* bisect and scan through file for frame starting before @time,
3138 * returns OK and @time/@offset if found, NONE and/or error otherwise
3139 * If @time == G_MAXINT64, scan for duration ( == last frame) */
3140 static GstFlowReturn
3141 gst_base_parse_locate_time (GstBaseParse * parse, GstClockTime * _time,
3144 GstFlowReturn ret = GST_FLOW_OK;
3145 gint64 lpos, hpos, newpos;
3146 GstClockTime time, ltime, htime, newtime, dur;
3147 gboolean cont = TRUE;
3148 const GstClockTime tolerance = TARGET_DIFFERENCE;
3149 const guint chunk = 4 * 1024;
3151 g_return_val_if_fail (_time != NULL, GST_FLOW_ERROR);
3152 g_return_val_if_fail (_offset != NULL, GST_FLOW_ERROR);
3154 /* TODO also make keyframe aware if useful some day */
3169 /* do not know at first */
3171 *_time = GST_CLOCK_TIME_NONE;
3173 /* need initial positions; start and end */
3174 lpos = parse->priv->first_frame_offset;
3175 ltime = parse->priv->first_frame_ts;
3176 htime = parse->priv->duration;
3177 hpos = parse->priv->upstream_size;
3179 /* check preconditions are satisfied;
3180 * start and end are needed, except for special case where we scan for
3181 * last frame to determine duration */
3182 if (parse->priv->pad_mode != GST_ACTIVATE_PULL || !hpos ||
3183 !GST_CLOCK_TIME_IS_VALID (ltime) ||
3184 (!GST_CLOCK_TIME_IS_VALID (htime) && time != G_MAXINT64)) {
3188 /* shortcut cases */
3191 } else if (time < ltime + tolerance) {
3195 } else if (time >= htime) {
3201 while (htime > ltime && cont) {
3202 GST_LOG_OBJECT (parse,
3203 "lpos: %" G_GUINT64_FORMAT ", ltime: %" GST_TIME_FORMAT, lpos,
3204 GST_TIME_ARGS (ltime));
3205 GST_LOG_OBJECT (parse,
3206 "hpos: %" G_GUINT64_FORMAT ", htime: %" GST_TIME_FORMAT, hpos,
3207 GST_TIME_ARGS (htime));
3208 if (G_UNLIKELY (time == G_MAXINT64)) {
3210 } else if (G_LIKELY (hpos > lpos)) {
3212 gst_util_uint64_scale (hpos - lpos, time - ltime, htime - ltime) +
3215 /* should mean lpos == hpos, since lpos <= hpos is invariant */
3217 /* we check this case once, but not forever, so break loop */
3222 newpos = CLAMP (newpos, lpos, hpos);
3223 GST_LOG_OBJECT (parse,
3224 "estimated _offset for %" GST_TIME_FORMAT ": %" G_GINT64_FORMAT,
3225 GST_TIME_ARGS (time), newpos);
3227 ret = gst_base_parse_find_frame (parse, &newpos, &newtime, &dur);
3228 if (ret == GST_FLOW_UNEXPECTED) {
3229 /* heuristic HACK */
3230 hpos = MAX (lpos, hpos - chunk);
3232 } else if (ret != GST_FLOW_OK) {
3236 if (newtime == -1 || newpos == -1) {
3237 GST_DEBUG_OBJECT (parse, "subclass did not provide metadata; aborting");
3241 if (G_UNLIKELY (time == G_MAXINT64)) {
3244 if (GST_CLOCK_TIME_IS_VALID (dur))
3247 } else if (newtime > time) {
3249 hpos = (newpos >= hpos) ? MAX (lpos, hpos - chunk) : MAX (lpos, newpos);
3251 } else if (newtime + tolerance > time) {
3252 /* close enough undershoot */
3256 } else if (newtime < ltime) {
3257 /* so a position beyond lpos resulted in earlier time than ltime ... */
3258 GST_DEBUG_OBJECT (parse, "non-ascending time; aborting");
3261 /* undershoot too far */
3262 newpos += newpos == lpos ? chunk : 0;
3263 lpos = CLAMP (newpos, lpos, hpos);
3269 GST_LOG_OBJECT (parse, "return offset %" G_GINT64_FORMAT ", time %"
3270 GST_TIME_FORMAT, *_offset, GST_TIME_ARGS (*_time));
3275 gst_base_parse_find_offset (GstBaseParse * parse, GstClockTime time,
3276 gboolean before, GstClockTime * _ts)
3278 gint64 bytes = 0, ts = 0;
3279 GstIndexEntry *entry = NULL;
3281 if (time == GST_CLOCK_TIME_NONE) {
3287 GST_OBJECT_LOCK (parse);
3288 if (parse->priv->index) {
3289 /* Let's check if we have an index entry for that time */
3290 entry = gst_index_get_assoc_entry (parse->priv->index,
3291 parse->priv->index_id,
3292 before ? GST_INDEX_LOOKUP_BEFORE : GST_INDEX_LOOKUP_AFTER,
3293 GST_ASSOCIATION_FLAG_KEY_UNIT, GST_FORMAT_TIME, time);
3297 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &bytes);
3298 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &ts);
3300 GST_DEBUG_OBJECT (parse, "found index entry for %" GST_TIME_FORMAT
3301 " at %" GST_TIME_FORMAT ", offset %" G_GINT64_FORMAT,
3302 GST_TIME_ARGS (time), GST_TIME_ARGS (ts), bytes);
3304 GST_DEBUG_OBJECT (parse, "no index entry found for %" GST_TIME_FORMAT,
3305 GST_TIME_ARGS (time));
3308 ts = GST_CLOCK_TIME_NONE;
3311 GST_OBJECT_UNLOCK (parse);
3320 /* returns TRUE if seek succeeded */
3322 gst_base_parse_handle_seek (GstBaseParse * parse, GstEvent * event)
3327 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
3328 gboolean flush, update, res = TRUE, accurate;
3329 gint64 cur, stop, seekpos, seekstop;
3330 GstSegment seeksegment = { 0, };
3331 GstFormat dstformat;
3332 GstClockTime start_ts;
3334 gst_event_parse_seek (event, &rate, &format, &flags,
3335 &cur_type, &cur, &stop_type, &stop);
3337 GST_DEBUG_OBJECT (parse, "seek to format %s, rate %f, "
3338 "start type %d at %" GST_TIME_FORMAT ", end type %d at %"
3339 GST_TIME_FORMAT, gst_format_get_name (format), rate,
3340 cur_type, GST_TIME_ARGS (cur), stop_type, GST_TIME_ARGS (stop));
3342 /* no negative rates in push mode */
3343 if (rate < 0.0 && parse->priv->pad_mode == GST_ACTIVATE_PUSH)
3346 if (cur_type != GST_SEEK_TYPE_SET ||
3347 (stop_type != GST_SEEK_TYPE_SET && stop_type != GST_SEEK_TYPE_NONE))
3350 /* For any format other than TIME, see if upstream handles
3351 * it directly or fail. For TIME, try upstream, but do it ourselves if
3352 * it fails upstream */
3353 if (format != GST_FORMAT_TIME) {
3354 /* default action delegates to upstream */
3358 gst_event_ref (event);
3359 if ((res = gst_pad_push_event (parse->sinkpad, event))) {
3364 /* get flush flag */
3365 flush = flags & GST_SEEK_FLAG_FLUSH;
3367 /* copy segment, we need this because we still need the old
3368 * segment when we close the current segment. */
3369 memcpy (&seeksegment, &parse->segment, sizeof (GstSegment));
3371 GST_DEBUG_OBJECT (parse, "configuring seek");
3372 gst_segment_set_seek (&seeksegment, rate, format, flags,
3373 cur_type, cur, stop_type, stop, &update);
3375 /* accurate seeking implies seek tables are used to obtain position,
3376 * and the requested segment is maintained exactly, not adjusted any way */
3377 accurate = flags & GST_SEEK_FLAG_ACCURATE;
3379 /* maybe we can be accurate for (almost) free */
3380 gst_base_parse_find_offset (parse, seeksegment.last_stop, TRUE, &start_ts);
3381 if (seeksegment.last_stop <= start_ts + TARGET_DIFFERENCE) {
3382 GST_DEBUG_OBJECT (parse, "accurate seek possible");
3386 GstClockTime startpos = seeksegment.last_stop;
3388 /* accurate requested, so ... seek a bit before target */
3389 if (startpos < parse->priv->lead_in_ts)
3392 startpos -= parse->priv->lead_in_ts;
3393 seekpos = gst_base_parse_find_offset (parse, startpos, TRUE, &start_ts);
3394 seekstop = gst_base_parse_find_offset (parse, seeksegment.stop, FALSE,
3397 start_ts = seeksegment.last_stop;
3398 dstformat = GST_FORMAT_BYTES;
3399 if (!gst_pad_query_convert (parse->srcpad, format, seeksegment.last_stop,
3400 &dstformat, &seekpos))
3401 goto convert_failed;
3402 if (!gst_pad_query_convert (parse->srcpad, format, seeksegment.stop,
3403 &dstformat, &seekstop))
3404 goto convert_failed;
3407 GST_DEBUG_OBJECT (parse,
3408 "seek position %" G_GINT64_FORMAT " in bytes: %" G_GINT64_FORMAT,
3410 GST_DEBUG_OBJECT (parse,
3411 "seek stop %" G_GINT64_FORMAT " in bytes: %" G_GINT64_FORMAT,
3412 seeksegment.stop, seekstop);
3414 if (parse->priv->pad_mode == GST_ACTIVATE_PULL) {
3417 GST_DEBUG_OBJECT (parse, "seek in PULL mode");
3420 if (parse->srcpad) {
3421 GST_DEBUG_OBJECT (parse, "sending flush start");
3422 gst_pad_push_event (parse->srcpad, gst_event_new_flush_start ());
3423 /* unlock upstream pull_range */
3424 gst_pad_push_event (parse->sinkpad, gst_event_new_flush_start ());
3427 gst_pad_pause_task (parse->sinkpad);
3430 /* we should now be able to grab the streaming thread because we stopped it
3431 * with the above flush/pause code */
3432 GST_PAD_STREAM_LOCK (parse->sinkpad);
3434 /* save current position */
3435 last_stop = parse->segment.last_stop;
3436 GST_DEBUG_OBJECT (parse, "stopped streaming at %" G_GINT64_FORMAT,
3439 /* now commit to new position */
3441 /* prepare for streaming again */
3443 GST_DEBUG_OBJECT (parse, "sending flush stop");
3444 gst_pad_push_event (parse->srcpad, gst_event_new_flush_stop ());
3445 gst_pad_push_event (parse->sinkpad, gst_event_new_flush_stop ());
3446 gst_base_parse_clear_queues (parse);
3448 if (parse->priv->close_segment)
3449 gst_event_unref (parse->priv->close_segment);
3451 parse->priv->close_segment = gst_event_new_new_segment (TRUE,
3452 parse->segment.rate, parse->segment.format,
3453 parse->segment.accum, parse->segment.last_stop, parse->segment.accum);
3455 /* keep track of our last_stop */
3456 seeksegment.accum = parse->segment.last_stop;
3458 GST_DEBUG_OBJECT (parse, "Created close seg format %d, "
3459 "start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT
3460 ", pos = %" GST_TIME_FORMAT, format,
3461 GST_TIME_ARGS (parse->segment.accum),
3462 GST_TIME_ARGS (parse->segment.last_stop),
3463 GST_TIME_ARGS (parse->segment.accum));
3466 memcpy (&parse->segment, &seeksegment, sizeof (GstSegment));
3468 /* store the newsegment event so it can be sent from the streaming thread. */
3469 if (parse->priv->pending_segment)
3470 gst_event_unref (parse->priv->pending_segment);
3472 /* This will be sent later in _loop() */
3473 parse->priv->pending_segment =
3474 gst_event_new_new_segment (FALSE, parse->segment.rate,
3475 parse->segment.format, parse->segment.start,
3476 parse->segment.stop, parse->segment.start);
3478 GST_DEBUG_OBJECT (parse, "Created newseg format %d, "
3479 "start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT
3480 ", pos = %" GST_TIME_FORMAT, format,
3481 GST_TIME_ARGS (parse->segment.start),
3482 GST_TIME_ARGS (parse->segment.stop),
3483 GST_TIME_ARGS (parse->segment.start));
3485 /* one last chance in pull mode to stay accurate;
3486 * maybe scan and subclass can find where to go */
3489 GstClockTime ts = seeksegment.last_stop;
3491 gst_base_parse_locate_time (parse, &ts, &scanpos);
3495 /* running collected index now consists of several intervals,
3496 * so optimized check no longer possible */
3497 parse->priv->index_last_valid = FALSE;
3498 parse->priv->index_last_offset = 0;
3499 parse->priv->index_last_ts = 0;
3503 /* mark discont if we are going to stream from another position. */
3504 if (seekpos != parse->priv->offset) {
3505 GST_DEBUG_OBJECT (parse,
3506 "mark DISCONT, we did a seek to another position");
3507 parse->priv->offset = seekpos;
3508 parse->priv->last_offset = seekpos;
3509 parse->priv->seen_keyframe = FALSE;
3510 parse->priv->discont = TRUE;
3511 parse->priv->next_ts = start_ts;
3512 parse->priv->last_ts = GST_CLOCK_TIME_NONE;
3513 parse->priv->sync_offset = seekpos;
3514 parse->priv->exact_position = accurate;
3517 /* Start streaming thread if paused */
3518 gst_pad_start_task (parse->sinkpad,
3519 (GstTaskFunction) gst_base_parse_loop, parse->sinkpad);
3521 GST_PAD_STREAM_UNLOCK (parse->sinkpad);
3526 GstEvent *new_event;
3527 GstBaseParseSeek *seek;
3529 /* The only thing we need to do in PUSH-mode is to send the
3530 seek event (in bytes) to upstream. Segment / flush handling happens
3531 in corresponding src event handlers */
3532 GST_DEBUG_OBJECT (parse, "seek in PUSH mode");
3533 if (seekstop >= 0 && seekpos <= seekpos)
3535 new_event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flush,
3536 GST_SEEK_TYPE_SET, seekpos, stop_type, seekstop);
3538 /* store segment info so its precise details can be reconstructed when
3539 * receiving newsegment;
3540 * this matters for all details when accurate seeking,
3541 * is most useful to preserve NONE stop time otherwise */
3542 seek = g_new0 (GstBaseParseSeek, 1);
3543 seek->segment = seeksegment;
3544 seek->accurate = accurate;
3545 seek->offset = seekpos;
3546 seek->start_ts = start_ts;
3547 GST_OBJECT_LOCK (parse);
3548 /* less optimal, but preserves order */
3549 parse->priv->pending_seeks =
3550 g_slist_append (parse->priv->pending_seeks, seek);
3551 GST_OBJECT_UNLOCK (parse);
3553 res = gst_pad_push_event (parse->sinkpad, new_event);
3556 GST_OBJECT_LOCK (parse);
3557 parse->priv->pending_seeks =
3558 g_slist_remove (parse->priv->pending_seeks, seek);
3559 GST_OBJECT_UNLOCK (parse);
3565 /* handled event is ours to free */
3567 gst_event_unref (event);
3573 GST_DEBUG_OBJECT (parse, "negative playback rates delegated upstream.");
3579 GST_DEBUG_OBJECT (parse, "unsupported seek type.");
3585 GST_DEBUG_OBJECT (parse, "conversion TIME to BYTES failed.");
3591 /* Checks if bitrates are available from upstream tags so that we don't
3592 * override them later
3595 gst_base_parse_handle_tag (GstBaseParse * parse, GstEvent * event)
3597 GstTagList *taglist = NULL;
3600 gst_event_parse_tag (event, &taglist);
3602 if (gst_tag_list_get_uint (taglist, GST_TAG_MINIMUM_BITRATE, &tmp)) {
3603 GST_DEBUG_OBJECT (parse, "upstream min bitrate %d", tmp);
3604 parse->priv->post_min_bitrate = FALSE;
3606 if (gst_tag_list_get_uint (taglist, GST_TAG_BITRATE, &tmp)) {
3607 GST_DEBUG_OBJECT (parse, "upstream avg bitrate %d", tmp);
3608 parse->priv->post_avg_bitrate = FALSE;
3610 if (gst_tag_list_get_uint (taglist, GST_TAG_MAXIMUM_BITRATE, &tmp)) {
3611 GST_DEBUG_OBJECT (parse, "upstream max bitrate %d", tmp);
3612 parse->priv->post_max_bitrate = FALSE;
3617 gst_base_parse_sink_setcaps (GstPad * pad, GstCaps * caps)
3619 GstBaseParse *parse;
3620 GstBaseParseClass *klass;
3621 gboolean res = TRUE;
3623 parse = GST_BASE_PARSE (GST_PAD_PARENT (pad));
3624 klass = GST_BASE_PARSE_GET_CLASS (parse);
3626 GST_DEBUG_OBJECT (parse, "caps: %" GST_PTR_FORMAT, caps);
3628 if (klass->set_sink_caps)
3629 res = klass->set_sink_caps (parse, caps);
3635 gst_base_parse_set_index (GstElement * element, GstIndex * index)
3637 GstBaseParse *parse = GST_BASE_PARSE (element);
3639 GST_OBJECT_LOCK (parse);
3640 if (parse->priv->index)
3641 gst_object_unref (parse->priv->index);
3643 parse->priv->index = gst_object_ref (index);
3644 gst_index_get_writer_id (index, GST_OBJECT (element),
3645 &parse->priv->index_id);
3646 parse->priv->own_index = FALSE;
3648 parse->priv->index = NULL;
3649 GST_OBJECT_UNLOCK (parse);
3653 gst_base_parse_get_index (GstElement * element)
3655 GstBaseParse *parse = GST_BASE_PARSE (element);
3656 GstIndex *result = NULL;
3658 GST_OBJECT_LOCK (parse);
3659 if (parse->priv->index)
3660 result = gst_object_ref (parse->priv->index);
3661 GST_OBJECT_UNLOCK (parse);
3666 static GstStateChangeReturn
3667 gst_base_parse_change_state (GstElement * element, GstStateChange transition)
3669 GstBaseParse *parse;
3670 GstStateChangeReturn result;
3672 parse = GST_BASE_PARSE (element);
3674 switch (transition) {
3675 case GST_STATE_CHANGE_READY_TO_PAUSED:
3676 /* If this is our own index destroy it as the
3677 * old entries might be wrong for the new stream */
3678 if (parse->priv->own_index) {
3679 gst_object_unref (parse->priv->index);
3680 parse->priv->index = NULL;
3681 parse->priv->own_index = FALSE;
3684 /* If no index was created, generate one */
3685 if (G_UNLIKELY (!parse->priv->index)) {
3686 GST_DEBUG_OBJECT (parse, "no index provided creating our own");
3688 parse->priv->index = gst_index_factory_make ("memindex");
3689 gst_index_get_writer_id (parse->priv->index, GST_OBJECT (parse),
3690 &parse->priv->index_id);
3691 parse->priv->own_index = TRUE;
3698 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3700 switch (transition) {
3701 case GST_STATE_CHANGE_PAUSED_TO_READY:
3702 gst_base_parse_reset (parse);