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>.
5 * Copyright (C) 2011, Hewlett-Packard Development Company, L.P.
6 * Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>, Collabora Ltd.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
25 * SECTION:gstbaseparse
26 * @title: GstBaseParse
27 * @short_description: Base class for stream parsers
28 * @see_also: #GstBaseTransform
30 * This base class is for parser elements that process data and splits it
31 * into separate audio/video/whatever frames.
35 * * provides one sink pad and one source pad
36 * * handles state changes
37 * * can operate in pull mode or push mode
38 * * handles seeking in both modes
39 * * handles events (SEGMENT/EOS/FLUSH)
40 * * handles queries (POSITION/DURATION/SEEKING/FORMAT/CONVERT)
43 * The purpose of this base class is to provide the basic functionality of
44 * a parser and share a lot of rather complex code.
46 * # Description of the parsing mechanism:
50 * * #GstBaseParse calls @start to inform subclass that data processing is
53 * * #GstBaseParse class calls @set_sink_caps to inform the subclass about
54 * incoming sinkpad caps. Subclass could already set the srcpad caps
55 * accordingly, but this might be delayed until calling
56 * gst_base_parse_finish_frame() with a non-queued frame.
58 * * At least at this point subclass needs to tell the #GstBaseParse class
59 * how big data chunks it wants to receive (min_frame_size). It can do
60 * this with gst_base_parse_set_min_frame_size().
62 * * #GstBaseParse class sets up appropriate data passing mode (pull/push)
63 * and starts to process the data.
67 * * #GstBaseParse gathers at least min_frame_size bytes of data either
68 * by pulling it from upstream or collecting buffers in an internal
71 * * A buffer of (at least) min_frame_size bytes is passed to subclass with
72 * @handle_frame. Subclass checks the contents and can optionally
73 * return GST_FLOW_OK along with an amount of data to be skipped to find
74 * a valid frame (which will result in a subsequent DISCONT).
75 * If, otherwise, the buffer does not hold a complete frame,
76 * @handle_frame can merely return and will be called again when additional
77 * data is available. In push mode this amounts to an
78 * additional input buffer (thus minimal additional latency), in pull mode
79 * this amounts to some arbitrary reasonable buffer size increase.
80 * Of course, gst_base_parse_set_min_frame_size() could also be used if a
81 * very specific known amount of additional data is required.
82 * If, however, the buffer holds a complete valid frame, it can pass
83 * the size of this frame to gst_base_parse_finish_frame().
84 * If acting as a converter, it can also merely indicate consumed input data
85 * while simultaneously providing custom output data.
86 * Note that baseclass performs some processing (such as tracking
87 * overall consumed data rate versus duration) for each finished frame,
88 * but other state is only updated upon each call to @handle_frame
89 * (such as tracking upstream input timestamp).
91 * Subclass is also responsible for setting the buffer metadata
92 * (e.g. buffer timestamp and duration, or keyframe if applicable).
93 * (although the latter can also be done by #GstBaseParse if it is
94 * appropriately configured, see below). Frame is provided with
95 * timestamp derived from upstream (as much as generally possible),
96 * duration obtained from configuration (see below), and offset
97 * if meaningful (in pull mode).
99 * Note that @check_valid_frame might receive any small
100 * amount of input data when leftover data is being drained (e.g. at EOS).
102 * * As part of finish frame processing,
103 * just prior to actually pushing the buffer in question,
104 * it is passed to @pre_push_frame 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.
108 * * During the parsing process #GstBaseParseClass will handle both srcpad
109 * and sinkpad events. They will be passed to subclass if @event or
110 * @src_event callbacks have been provided.
114 * * #GstBaseParse class calls @stop to inform the subclass that data
115 * parsing will be stopped.
117 * Subclass is responsible for providing pad template caps for
118 * source and sink pads. The pads need to be named "sink" and "src". It also
119 * needs to set the fixed caps on srcpad, when the format is ensured (e.g.
120 * when base class calls subclass' @set_sink_caps function).
122 * This base class uses %GST_FORMAT_DEFAULT as a meaning of frames. So,
123 * subclass conversion routine needs to know that conversion from
124 * %GST_FORMAT_TIME to %GST_FORMAT_DEFAULT must return the
125 * frame number that can be found from the given byte position.
127 * #GstBaseParse uses subclasses conversion methods also for seeking (or
128 * otherwise uses its own default one, see also below).
130 * Subclass @start and @stop functions will be called to inform the beginning
131 * and end of data processing.
133 * Things that subclass need to take care of:
135 * * Provide pad templates
136 * * Fixate the source pad caps when appropriate
137 * * Inform base class how big data chunks should be retrieved. This is
138 * done with gst_base_parse_set_min_frame_size() function.
139 * * Examine data chunks passed to subclass with @handle_frame and pass
140 * proper frame(s) to gst_base_parse_finish_frame(), and setting src pad
141 * caps and timestamps on frame.
142 * * Provide conversion functions
143 * * Update the duration information with gst_base_parse_set_duration()
144 * * Optionally passthrough using gst_base_parse_set_passthrough()
145 * * Configure various baseparse parameters using
146 * gst_base_parse_set_average_bitrate(), gst_base_parse_set_syncable()
147 * and gst_base_parse_set_frame_rate().
149 * * In particular, if subclass is unable to determine a duration, but
150 * parsing (or specs) yields a frames per seconds rate, then this can be
151 * provided to #GstBaseParse to enable it to cater for
152 * buffer time metadata (which will be taken from upstream as much as
153 * possible). Internally keeping track of frame durations and respective
154 * sizes that have been pushed provides #GstBaseParse with an estimated
155 * bitrate. A default @convert (used if not overridden) will then use these
156 * rates to perform obvious conversions. These rates are also used to
157 * update (estimated) duration at regular frame intervals.
162 * - In push mode provide a queue of adapter-"queued" buffers for upstream
164 * - Queue buffers/events until caps are set
174 #include <gst/base/gstadapter.h>
176 #include "gstbaseparse.h"
178 /* FIXME: get rid of old GstIndex code */
179 #include "gstindex.h"
180 #include "gstindex.c"
181 #include "gstmemindex.c"
183 #define GST_BASE_PARSE_FRAME_PRIVATE_FLAG_NOALLOC (1 << 0)
185 #define MIN_FRAMES_TO_POST_BITRATE 10
186 #define TARGET_DIFFERENCE (20 * GST_SECOND)
187 #define MAX_INDEX_ENTRIES 4096
188 #define UPDATE_THRESHOLD 2
190 #define ABSDIFF(a,b) (((a) > (b)) ? ((a) - (b)) : ((b) - (a)))
192 GST_DEBUG_CATEGORY_STATIC (gst_base_parse_debug);
193 #define GST_CAT_DEFAULT gst_base_parse_debug
195 /* Supported formats */
196 static const GstFormat fmtlist[] = {
203 #define GST_BASE_PARSE_GET_PRIVATE(obj) \
204 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_PARSE, GstBaseParsePrivate))
206 struct _GstBaseParsePrivate
213 GstFormat duration_fmt;
214 gint64 estimated_duration;
215 gint64 estimated_drift;
217 guint min_frame_size;
218 gboolean disable_passthrough;
219 gboolean passthrough;
220 gboolean pts_interpolate;
223 gboolean has_timing_info;
224 guint fps_num, fps_den;
225 gint update_interval;
227 guint lead_in, lead_out;
228 GstClockTime lead_in_ts, lead_out_ts;
229 GstClockTime min_latency, max_latency;
238 GstClockTime next_pts;
239 GstClockTime next_dts;
240 GstClockTime prev_pts;
241 GstClockTime prev_dts;
242 gboolean prev_dts_from_pts;
243 GstClockTime frame_duration;
244 gboolean seen_keyframe;
250 guint64 data_bytecount;
251 guint64 acc_duration;
252 GstClockTime first_frame_pts;
253 GstClockTime first_frame_dts;
254 gint64 first_frame_offset;
256 gboolean post_min_bitrate;
257 gboolean post_avg_bitrate;
258 gboolean post_max_bitrate;
263 guint posted_avg_bitrate;
265 /* frames/buffers that are queued and ready to go on OK */
266 GQueue queued_frames;
270 /* index entry storage, either ours or provided */
276 /* seek table entries only maintained if upstream is BYTE seekable */
277 gboolean upstream_seekable;
278 gboolean upstream_has_duration;
279 gint64 upstream_size;
280 GstFormat upstream_format;
281 /* minimum distance between two index entries */
282 GstClockTimeDiff idx_interval;
283 guint64 idx_byte_interval;
284 /* ts and offset of last entry added */
285 GstClockTime index_last_ts;
286 gint64 index_last_offset;
287 gboolean index_last_valid;
289 /* timestamps currently produced are accurate, e.g. started from 0 onwards */
290 gboolean exact_position;
291 /* seek events are temporarily kept to match them with newsegments */
292 GSList *pending_seeks;
294 /* reverse playback */
295 GSList *buffers_pending;
296 GSList *buffers_head;
297 GSList *buffers_queued;
298 GSList *buffers_send;
299 GstClockTime last_pts;
300 GstClockTime last_dts;
303 /* Pending serialized events */
304 GList *pending_events;
306 /* If baseparse has checked the caps to identify if it is
307 * handling video or audio */
308 gboolean checked_media;
310 /* offset of last parsed frame/data */
312 /* force a new frame, regardless of offset */
314 /* whether we are merely scanning for a frame */
316 /* ... and resulting frame, if any */
317 GstBaseParseFrame *scanned_frame;
319 /* TRUE if we're still detecting the format, i.e.
320 * if ::detect() is still called for future buffers */
322 GList *detect_buffers;
323 guint detect_buffers_size;
325 /* True when no buffers have been received yet */
326 gboolean first_buffer;
328 /* if TRUE, a STREAM_START event needs to be pushed */
329 gboolean push_stream_start;
331 /* When we need to skip more data than we have currently */
334 /* Tag handling (stream tags only, global tags are passed through as-is) */
335 GstTagList *upstream_tags;
336 GstTagList *parser_tags;
337 GstTagMergeMode parser_tags_merge_mode;
338 gboolean tags_changed;
341 typedef struct _GstBaseParseSeek
346 GstClockTime start_ts;
349 #define DEFAULT_DISABLE_PASSTHROUGH FALSE
354 PROP_DISABLE_PASSTHROUGH,
358 #define GST_BASE_PARSE_INDEX_LOCK(parse) \
359 g_mutex_lock (&parse->priv->index_lock);
360 #define GST_BASE_PARSE_INDEX_UNLOCK(parse) \
361 g_mutex_unlock (&parse->priv->index_lock);
363 static GstElementClass *parent_class = NULL;
365 static void gst_base_parse_class_init (GstBaseParseClass * klass);
366 static void gst_base_parse_init (GstBaseParse * parse,
367 GstBaseParseClass * klass);
370 gst_base_parse_get_type (void)
372 static volatile gsize base_parse_type = 0;
374 if (g_once_init_enter (&base_parse_type)) {
375 static const GTypeInfo base_parse_info = {
376 sizeof (GstBaseParseClass),
377 (GBaseInitFunc) NULL,
378 (GBaseFinalizeFunc) NULL,
379 (GClassInitFunc) gst_base_parse_class_init,
382 sizeof (GstBaseParse),
384 (GInstanceInitFunc) gst_base_parse_init,
388 _type = g_type_register_static (GST_TYPE_ELEMENT,
389 "GstBaseParse", &base_parse_info, G_TYPE_FLAG_ABSTRACT);
390 g_once_init_leave (&base_parse_type, _type);
392 return (GType) base_parse_type;
395 static void gst_base_parse_finalize (GObject * object);
397 static GstStateChangeReturn gst_base_parse_change_state (GstElement * element,
398 GstStateChange transition);
399 static void gst_base_parse_reset (GstBaseParse * parse);
402 static void gst_base_parse_set_index (GstElement * element, GstIndex * index);
403 static GstIndex *gst_base_parse_get_index (GstElement * element);
406 static gboolean gst_base_parse_sink_activate (GstPad * sinkpad,
408 static gboolean gst_base_parse_sink_activate_mode (GstPad * pad,
409 GstObject * parent, GstPadMode mode, gboolean active);
410 static gboolean gst_base_parse_handle_seek (GstBaseParse * parse,
412 static void gst_base_parse_set_upstream_tags (GstBaseParse * parse,
413 GstTagList * taglist);
415 static void gst_base_parse_set_property (GObject * object, guint prop_id,
416 const GValue * value, GParamSpec * pspec);
417 static void gst_base_parse_get_property (GObject * object, guint prop_id,
418 GValue * value, GParamSpec * pspec);
420 static gboolean gst_base_parse_src_event (GstPad * pad, GstObject * parent,
422 static gboolean gst_base_parse_src_query (GstPad * pad, GstObject * parent,
425 static gboolean gst_base_parse_sink_event (GstPad * pad, GstObject * parent,
427 static gboolean gst_base_parse_sink_query (GstPad * pad, GstObject * parent,
430 static GstFlowReturn gst_base_parse_chain (GstPad * pad, GstObject * parent,
432 static void gst_base_parse_loop (GstPad * pad);
434 static GstFlowReturn gst_base_parse_parse_frame (GstBaseParse * parse,
435 GstBaseParseFrame * frame);
437 static gboolean gst_base_parse_sink_event_default (GstBaseParse * parse,
440 static gboolean gst_base_parse_src_event_default (GstBaseParse * parse,
443 static gboolean gst_base_parse_sink_query_default (GstBaseParse * parse,
445 static gboolean gst_base_parse_src_query_default (GstBaseParse * parse,
448 static gint64 gst_base_parse_find_offset (GstBaseParse * parse,
449 GstClockTime time, gboolean before, GstClockTime * _ts);
450 static GstFlowReturn gst_base_parse_locate_time (GstBaseParse * parse,
451 GstClockTime * _time, gint64 * _offset);
453 static GstFlowReturn gst_base_parse_start_fragment (GstBaseParse * parse);
454 static GstFlowReturn gst_base_parse_finish_fragment (GstBaseParse * parse,
456 static GstFlowReturn gst_base_parse_send_buffers (GstBaseParse * parse);
458 static inline GstFlowReturn gst_base_parse_check_sync (GstBaseParse * parse);
460 static gboolean gst_base_parse_is_seekable (GstBaseParse * parse);
462 static void gst_base_parse_push_pending_events (GstBaseParse * parse);
465 gst_base_parse_clear_queues (GstBaseParse * parse)
467 g_slist_foreach (parse->priv->buffers_queued, (GFunc) gst_buffer_unref, NULL);
468 g_slist_free (parse->priv->buffers_queued);
469 parse->priv->buffers_queued = NULL;
470 g_slist_foreach (parse->priv->buffers_pending, (GFunc) gst_buffer_unref,
472 g_slist_free (parse->priv->buffers_pending);
473 parse->priv->buffers_pending = NULL;
474 g_slist_foreach (parse->priv->buffers_head, (GFunc) gst_buffer_unref, NULL);
475 g_slist_free (parse->priv->buffers_head);
476 parse->priv->buffers_head = NULL;
477 g_slist_foreach (parse->priv->buffers_send, (GFunc) gst_buffer_unref, NULL);
478 g_slist_free (parse->priv->buffers_send);
479 parse->priv->buffers_send = NULL;
481 g_list_foreach (parse->priv->detect_buffers, (GFunc) gst_buffer_unref, NULL);
482 g_list_free (parse->priv->detect_buffers);
483 parse->priv->detect_buffers = NULL;
484 parse->priv->detect_buffers_size = 0;
486 g_queue_foreach (&parse->priv->queued_frames,
487 (GFunc) gst_base_parse_frame_free, NULL);
488 g_queue_clear (&parse->priv->queued_frames);
490 gst_buffer_replace (&parse->priv->cache, NULL);
492 g_list_foreach (parse->priv->pending_events, (GFunc) gst_event_unref, NULL);
493 g_list_free (parse->priv->pending_events);
494 parse->priv->pending_events = NULL;
496 parse->priv->checked_media = FALSE;
500 gst_base_parse_finalize (GObject * object)
502 GstBaseParse *parse = GST_BASE_PARSE (object);
504 g_object_unref (parse->priv->adapter);
506 if (parse->priv->index) {
507 gst_object_unref (parse->priv->index);
508 parse->priv->index = NULL;
510 g_mutex_clear (&parse->priv->index_lock);
512 gst_base_parse_clear_queues (parse);
514 G_OBJECT_CLASS (parent_class)->finalize (object);
518 gst_base_parse_class_init (GstBaseParseClass * klass)
520 GObjectClass *gobject_class;
521 GstElementClass *gstelement_class;
523 gobject_class = G_OBJECT_CLASS (klass);
524 g_type_class_add_private (klass, sizeof (GstBaseParsePrivate));
525 parent_class = g_type_class_peek_parent (klass);
527 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_parse_finalize);
528 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_base_parse_set_property);
529 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_base_parse_get_property);
532 * GstBaseParse:disable-passthrough:
534 * If set to %TRUE, baseparse will unconditionally force parsing of the
535 * incoming data. This can be required in the rare cases where the incoming
536 * side-data (caps, pts, dts, ...) is not trusted by the user and wants to
537 * force validation and parsing of the incoming data.
538 * If set to %FALSE, decision of whether to parse the data or not is up to
539 * the implementation (standard behaviour).
541 g_object_class_install_property (gobject_class, PROP_DISABLE_PASSTHROUGH,
542 g_param_spec_boolean ("disable-passthrough", "Disable passthrough",
543 "Force processing (disables passthrough)",
544 DEFAULT_DISABLE_PASSTHROUGH,
545 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
547 gstelement_class = (GstElementClass *) klass;
548 gstelement_class->change_state =
549 GST_DEBUG_FUNCPTR (gst_base_parse_change_state);
552 gstelement_class->set_index = GST_DEBUG_FUNCPTR (gst_base_parse_set_index);
553 gstelement_class->get_index = GST_DEBUG_FUNCPTR (gst_base_parse_get_index);
556 /* Default handlers */
557 klass->sink_event = gst_base_parse_sink_event_default;
558 klass->src_event = gst_base_parse_src_event_default;
559 klass->sink_query = gst_base_parse_sink_query_default;
560 klass->src_query = gst_base_parse_src_query_default;
561 klass->convert = gst_base_parse_convert_default;
563 GST_DEBUG_CATEGORY_INIT (gst_base_parse_debug, "baseparse", 0,
564 "baseparse element");
568 gst_base_parse_init (GstBaseParse * parse, GstBaseParseClass * bclass)
570 GstPadTemplate *pad_template;
572 GST_DEBUG_OBJECT (parse, "gst_base_parse_init");
574 parse->priv = GST_BASE_PARSE_GET_PRIVATE (parse);
577 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "sink");
578 g_return_if_fail (pad_template != NULL);
579 parse->sinkpad = gst_pad_new_from_template (pad_template, "sink");
580 gst_pad_set_event_function (parse->sinkpad,
581 GST_DEBUG_FUNCPTR (gst_base_parse_sink_event));
582 gst_pad_set_query_function (parse->sinkpad,
583 GST_DEBUG_FUNCPTR (gst_base_parse_sink_query));
584 gst_pad_set_chain_function (parse->sinkpad,
585 GST_DEBUG_FUNCPTR (gst_base_parse_chain));
586 gst_pad_set_activate_function (parse->sinkpad,
587 GST_DEBUG_FUNCPTR (gst_base_parse_sink_activate));
588 gst_pad_set_activatemode_function (parse->sinkpad,
589 GST_DEBUG_FUNCPTR (gst_base_parse_sink_activate_mode));
590 GST_PAD_SET_PROXY_ALLOCATION (parse->sinkpad);
591 gst_element_add_pad (GST_ELEMENT (parse), parse->sinkpad);
593 GST_DEBUG_OBJECT (parse, "sinkpad created");
596 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
597 g_return_if_fail (pad_template != NULL);
598 parse->srcpad = gst_pad_new_from_template (pad_template, "src");
599 gst_pad_set_event_function (parse->srcpad,
600 GST_DEBUG_FUNCPTR (gst_base_parse_src_event));
601 gst_pad_set_query_function (parse->srcpad,
602 GST_DEBUG_FUNCPTR (gst_base_parse_src_query));
603 gst_pad_use_fixed_caps (parse->srcpad);
604 gst_element_add_pad (GST_ELEMENT (parse), parse->srcpad);
605 GST_DEBUG_OBJECT (parse, "src created");
607 g_queue_init (&parse->priv->queued_frames);
609 parse->priv->adapter = gst_adapter_new ();
611 parse->priv->pad_mode = GST_PAD_MODE_NONE;
613 g_mutex_init (&parse->priv->index_lock);
616 gst_base_parse_reset (parse);
617 GST_DEBUG_OBJECT (parse, "init ok");
619 GST_OBJECT_FLAG_SET (parse, GST_ELEMENT_FLAG_INDEXABLE);
621 parse->priv->upstream_tags = NULL;
622 parse->priv->parser_tags = NULL;
623 parse->priv->parser_tags_merge_mode = GST_TAG_MERGE_APPEND;
627 gst_base_parse_set_property (GObject * object, guint prop_id,
628 const GValue * value, GParamSpec * pspec)
630 GstBaseParse *parse = GST_BASE_PARSE (object);
633 case PROP_DISABLE_PASSTHROUGH:
634 parse->priv->disable_passthrough = g_value_get_boolean (value);
637 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
643 gst_base_parse_get_property (GObject * object, guint prop_id, GValue * value,
646 GstBaseParse *parse = GST_BASE_PARSE (object);
649 case PROP_DISABLE_PASSTHROUGH:
650 g_value_set_boolean (value, parse->priv->disable_passthrough);
653 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
659 gst_base_parse_frame_copy (GstBaseParseFrame * frame)
661 GstBaseParseFrame *copy;
663 copy = g_slice_dup (GstBaseParseFrame, frame);
664 copy->buffer = gst_buffer_ref (frame->buffer);
665 copy->_private_flags &= ~GST_BASE_PARSE_FRAME_PRIVATE_FLAG_NOALLOC;
667 GST_TRACE ("copied frame %p -> %p", frame, copy);
673 gst_base_parse_frame_free (GstBaseParseFrame * frame)
675 GST_TRACE ("freeing frame %p", frame);
678 gst_buffer_unref (frame->buffer);
679 frame->buffer = NULL;
682 if (!(frame->_private_flags & GST_BASE_PARSE_FRAME_PRIVATE_FLAG_NOALLOC)) {
683 g_slice_free (GstBaseParseFrame, frame);
685 memset (frame, 0, sizeof (*frame));
689 G_DEFINE_BOXED_TYPE (GstBaseParseFrame, gst_base_parse_frame,
690 (GBoxedCopyFunc) gst_base_parse_frame_copy,
691 (GBoxedFreeFunc) gst_base_parse_frame_free);
694 * gst_base_parse_frame_init:
695 * @frame: #GstBaseParseFrame.
697 * Sets a #GstBaseParseFrame to initial state. Currently this means
698 * all public fields are zero-ed and a private flag is set to make
699 * sure gst_base_parse_frame_free() only frees the contents but not
700 * the actual frame. Use this function to initialise a #GstBaseParseFrame
701 * allocated on the stack.
704 gst_base_parse_frame_init (GstBaseParseFrame * frame)
706 memset (frame, 0, sizeof (GstBaseParseFrame));
707 frame->_private_flags = GST_BASE_PARSE_FRAME_PRIVATE_FLAG_NOALLOC;
708 GST_TRACE ("inited frame %p", frame);
712 * gst_base_parse_frame_new:
713 * @buffer: (transfer none): a #GstBuffer
715 * @overhead: number of bytes in this frame which should be counted as
716 * metadata overhead, ie. not used to calculate the average bitrate.
717 * Set to -1 to mark the entire frame as metadata. If in doubt, set to 0.
719 * Allocates a new #GstBaseParseFrame. This function is mainly for bindings,
720 * elements written in C should usually allocate the frame on the stack and
721 * then use gst_base_parse_frame_init() to initialise it.
723 * Returns: a newly-allocated #GstBaseParseFrame. Free with
724 * gst_base_parse_frame_free() when no longer needed.
727 gst_base_parse_frame_new (GstBuffer * buffer, GstBaseParseFrameFlags flags,
730 GstBaseParseFrame *frame;
732 frame = g_slice_new0 (GstBaseParseFrame);
733 frame->buffer = gst_buffer_ref (buffer);
735 GST_TRACE ("created frame %p", frame);
740 gst_base_parse_update_flags (GstBaseParse * parse)
744 /* set flags one by one for clarity */
745 if (G_UNLIKELY (parse->priv->drain))
746 parse->flags |= GST_BASE_PARSE_FLAG_DRAINING;
748 /* losing sync is pretty much a discont (and vice versa), no ? */
749 if (G_UNLIKELY (parse->priv->discont))
750 parse->flags |= GST_BASE_PARSE_FLAG_LOST_SYNC;
754 gst_base_parse_update_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
756 if (G_UNLIKELY (parse->priv->discont)) {
757 GST_DEBUG_OBJECT (parse, "marking DISCONT");
758 GST_BUFFER_FLAG_SET (frame->buffer, GST_BUFFER_FLAG_DISCONT);
760 GST_BUFFER_FLAG_UNSET (frame->buffer, GST_BUFFER_FLAG_DISCONT);
763 if (parse->priv->prev_offset != parse->priv->offset || parse->priv->new_frame) {
764 GST_LOG_OBJECT (parse, "marking as new frame");
765 frame->flags |= GST_BASE_PARSE_FRAME_FLAG_NEW_FRAME;
768 frame->offset = parse->priv->prev_offset = parse->priv->offset;
772 gst_base_parse_reset (GstBaseParse * parse)
774 GST_OBJECT_LOCK (parse);
775 gst_segment_init (&parse->segment, GST_FORMAT_TIME);
776 parse->priv->duration = -1;
777 parse->priv->min_frame_size = 1;
778 parse->priv->discont = TRUE;
779 parse->priv->flushing = FALSE;
780 parse->priv->saw_gaps = FALSE;
781 parse->priv->offset = 0;
782 parse->priv->sync_offset = 0;
783 parse->priv->update_interval = -1;
784 parse->priv->fps_num = parse->priv->fps_den = 0;
785 parse->priv->frame_duration = GST_CLOCK_TIME_NONE;
786 parse->priv->lead_in = parse->priv->lead_out = 0;
787 parse->priv->lead_in_ts = parse->priv->lead_out_ts = 0;
788 parse->priv->bitrate = 0;
789 parse->priv->framecount = 0;
790 parse->priv->bytecount = 0;
791 parse->priv->acc_duration = 0;
792 parse->priv->first_frame_pts = GST_CLOCK_TIME_NONE;
793 parse->priv->first_frame_dts = GST_CLOCK_TIME_NONE;
794 parse->priv->first_frame_offset = -1;
795 parse->priv->estimated_duration = -1;
796 parse->priv->estimated_drift = 0;
797 parse->priv->next_pts = GST_CLOCK_TIME_NONE;
798 parse->priv->next_dts = 0;
799 parse->priv->syncable = TRUE;
800 parse->priv->disable_passthrough = DEFAULT_DISABLE_PASSTHROUGH;
801 parse->priv->passthrough = FALSE;
802 parse->priv->pts_interpolate = TRUE;
803 parse->priv->infer_ts = TRUE;
804 parse->priv->has_timing_info = FALSE;
805 parse->priv->min_bitrate = G_MAXUINT;
806 parse->priv->max_bitrate = 0;
807 parse->priv->avg_bitrate = 0;
808 parse->priv->posted_avg_bitrate = 0;
810 parse->priv->index_last_ts = GST_CLOCK_TIME_NONE;
811 parse->priv->index_last_offset = -1;
812 parse->priv->index_last_valid = TRUE;
813 parse->priv->upstream_seekable = FALSE;
814 parse->priv->upstream_size = 0;
815 parse->priv->upstream_has_duration = FALSE;
816 parse->priv->upstream_format = GST_FORMAT_UNDEFINED;
817 parse->priv->idx_interval = 0;
818 parse->priv->idx_byte_interval = 0;
819 parse->priv->exact_position = TRUE;
820 parse->priv->seen_keyframe = FALSE;
821 parse->priv->checked_media = FALSE;
823 parse->priv->last_dts = GST_CLOCK_TIME_NONE;
824 parse->priv->last_pts = GST_CLOCK_TIME_NONE;
825 parse->priv->last_offset = 0;
827 parse->priv->skip = 0;
829 g_list_foreach (parse->priv->pending_events, (GFunc) gst_mini_object_unref,
831 g_list_free (parse->priv->pending_events);
832 parse->priv->pending_events = NULL;
834 if (parse->priv->cache) {
835 gst_buffer_unref (parse->priv->cache);
836 parse->priv->cache = NULL;
839 g_slist_foreach (parse->priv->pending_seeks, (GFunc) g_free, NULL);
840 g_slist_free (parse->priv->pending_seeks);
841 parse->priv->pending_seeks = NULL;
843 if (parse->priv->adapter)
844 gst_adapter_clear (parse->priv->adapter);
846 gst_base_parse_set_upstream_tags (parse, NULL);
848 if (parse->priv->parser_tags) {
849 gst_tag_list_unref (parse->priv->parser_tags);
850 parse->priv->parser_tags = NULL;
852 parse->priv->parser_tags_merge_mode = GST_TAG_MERGE_APPEND;
854 parse->priv->new_frame = TRUE;
856 parse->priv->first_buffer = TRUE;
858 g_list_foreach (parse->priv->detect_buffers, (GFunc) gst_buffer_unref, NULL);
859 g_list_free (parse->priv->detect_buffers);
860 parse->priv->detect_buffers = NULL;
861 parse->priv->detect_buffers_size = 0;
862 GST_OBJECT_UNLOCK (parse);
866 gst_base_parse_check_bitrate_tag (GstBaseParse * parse, const gchar * tag)
868 gboolean got_tag = FALSE;
871 if (parse->priv->upstream_tags != NULL)
872 got_tag = gst_tag_list_get_uint (parse->priv->upstream_tags, tag, &n);
874 if (!got_tag && parse->priv->parser_tags != NULL)
875 got_tag = gst_tag_list_get_uint (parse->priv->parser_tags, tag, &n);
880 /* check if upstream or subclass tags contain bitrates already */
882 gst_base_parse_check_bitrate_tags (GstBaseParse * parse)
884 parse->priv->post_min_bitrate =
885 !gst_base_parse_check_bitrate_tag (parse, GST_TAG_MINIMUM_BITRATE);
886 parse->priv->post_avg_bitrate =
887 !gst_base_parse_check_bitrate_tag (parse, GST_TAG_BITRATE);
888 parse->priv->post_max_bitrate =
889 !gst_base_parse_check_bitrate_tag (parse, GST_TAG_MAXIMUM_BITRATE);
892 /* Queues new tag event with the current combined state of the stream tags
893 * (i.e. upstream tags merged with subclass tags and current baseparse tags) */
895 gst_base_parse_queue_tag_event_update (GstBaseParse * parse)
897 GstTagList *merged_tags;
899 GST_LOG_OBJECT (parse, "upstream : %" GST_PTR_FORMAT,
900 parse->priv->upstream_tags);
901 GST_LOG_OBJECT (parse, "parser : %" GST_PTR_FORMAT,
902 parse->priv->parser_tags);
903 GST_LOG_OBJECT (parse, "mode : %d", parse->priv->parser_tags_merge_mode);
906 gst_tag_list_merge (parse->priv->upstream_tags, parse->priv->parser_tags,
907 parse->priv->parser_tags_merge_mode);
909 GST_DEBUG_OBJECT (parse, "merged : %" GST_PTR_FORMAT, merged_tags);
911 if (merged_tags == NULL)
914 if (gst_tag_list_is_empty (merged_tags)) {
915 gst_tag_list_unref (merged_tags);
919 if (parse->priv->framecount >= MIN_FRAMES_TO_POST_BITRATE) {
920 /* only add bitrate tags to non-empty taglists for now, and only if neither
921 * upstream tags nor the subclass sets the bitrate tag in question already */
922 if (parse->priv->min_bitrate != G_MAXUINT && parse->priv->post_min_bitrate) {
923 GST_LOG_OBJECT (parse, "adding min bitrate %u", parse->priv->min_bitrate);
924 gst_tag_list_add (merged_tags, GST_TAG_MERGE_KEEP,
925 GST_TAG_MINIMUM_BITRATE, parse->priv->min_bitrate, NULL);
927 if (parse->priv->max_bitrate != 0 && parse->priv->post_max_bitrate) {
928 GST_LOG_OBJECT (parse, "adding max bitrate %u", parse->priv->max_bitrate);
929 gst_tag_list_add (merged_tags, GST_TAG_MERGE_KEEP,
930 GST_TAG_MAXIMUM_BITRATE, parse->priv->max_bitrate, NULL);
932 if (parse->priv->avg_bitrate != 0 && parse->priv->post_avg_bitrate) {
933 parse->priv->posted_avg_bitrate = parse->priv->avg_bitrate;
934 GST_LOG_OBJECT (parse, "adding avg bitrate %u", parse->priv->avg_bitrate);
935 gst_tag_list_add (merged_tags, GST_TAG_MERGE_KEEP,
936 GST_TAG_BITRATE, parse->priv->avg_bitrate, NULL);
940 parse->priv->pending_events =
941 g_list_prepend (parse->priv->pending_events,
942 gst_event_new_tag (merged_tags));
945 /* gst_base_parse_parse_frame:
946 * @parse: #GstBaseParse.
947 * @buffer: #GstBuffer.
949 * Default callback for parse_frame.
952 gst_base_parse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
954 GstBuffer *buffer = frame->buffer;
956 if (!GST_BUFFER_PTS_IS_VALID (buffer) &&
957 GST_CLOCK_TIME_IS_VALID (parse->priv->next_pts)) {
958 GST_BUFFER_PTS (buffer) = parse->priv->next_pts;
960 if (!GST_BUFFER_DTS_IS_VALID (buffer) &&
961 GST_CLOCK_TIME_IS_VALID (parse->priv->next_dts)) {
962 GST_BUFFER_DTS (buffer) = parse->priv->next_dts;
964 if (!GST_BUFFER_DURATION_IS_VALID (buffer) &&
965 GST_CLOCK_TIME_IS_VALID (parse->priv->frame_duration)) {
966 GST_BUFFER_DURATION (buffer) = parse->priv->frame_duration;
971 /* gst_base_parse_convert:
972 * @parse: #GstBaseParse.
973 * @src_format: #GstFormat describing the source format.
974 * @src_value: Source value to be converted.
975 * @dest_format: #GstFormat defining the converted format.
976 * @dest_value: Pointer where the conversion result will be put.
978 * Converts using configured "convert" vmethod in #GstBaseParse class.
980 * Returns: %TRUE if conversion was successful.
983 gst_base_parse_convert (GstBaseParse * parse,
984 GstFormat src_format,
985 gint64 src_value, GstFormat dest_format, gint64 * dest_value)
987 GstBaseParseClass *klass = GST_BASE_PARSE_GET_CLASS (parse);
990 g_return_val_if_fail (dest_value != NULL, FALSE);
995 ret = klass->convert (parse, src_format, src_value, dest_format, dest_value);
997 #ifndef GST_DISABLE_GST_DEBUG
1000 if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES) {
1001 GST_LOG_OBJECT (parse,
1002 "TIME -> BYTES: %" GST_TIME_FORMAT " -> %" G_GINT64_FORMAT,
1003 GST_TIME_ARGS (src_value), *dest_value);
1004 } else if (dest_format == GST_FORMAT_TIME &&
1005 src_format == GST_FORMAT_BYTES) {
1006 GST_LOG_OBJECT (parse,
1007 "BYTES -> TIME: %" G_GINT64_FORMAT " -> %" GST_TIME_FORMAT,
1008 src_value, GST_TIME_ARGS (*dest_value));
1010 GST_LOG_OBJECT (parse,
1011 "%s -> %s: %" G_GINT64_FORMAT " -> %" G_GINT64_FORMAT,
1012 GST_STR_NULL (gst_format_get_name (src_format)),
1013 GST_STR_NULL (gst_format_get_name (dest_format)),
1014 src_value, *dest_value);
1017 GST_DEBUG_OBJECT (parse, "conversion failed");
1026 update_upstream_provided (GQuark field_id, const GValue * value,
1029 GstCaps *default_caps = user_data;
1033 caps_size = gst_caps_get_size (default_caps);
1034 for (i = 0; i < caps_size; i++) {
1035 GstStructure *structure = gst_caps_get_structure (default_caps, i);
1036 if (gst_structure_id_has_field (structure, field_id))
1037 gst_structure_id_set_value (structure, field_id, value);
1044 gst_base_parse_negotiate_default_caps (GstBaseParse * parse)
1046 GstCaps *caps, *templcaps;
1047 GstCaps *sinkcaps = NULL;
1048 GstCaps *default_caps = NULL;
1049 GstStructure *structure;
1051 templcaps = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SRC_PAD (parse));
1052 caps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), templcaps);
1054 gst_caps_unref (templcaps);
1059 if (!caps || gst_caps_is_empty (caps) || gst_caps_is_any (caps)) {
1063 GST_LOG_OBJECT (parse, "peer caps %" GST_PTR_FORMAT, caps);
1065 /* before fixating, try to use whatever upstream provided */
1066 default_caps = gst_caps_copy (caps);
1067 sinkcaps = gst_pad_get_current_caps (GST_BASE_PARSE_SINK_PAD (parse));
1069 GST_LOG_OBJECT (parse, "current caps %" GST_PTR_FORMAT " for sinkpad",
1073 structure = gst_caps_get_structure (sinkcaps, 0);
1074 gst_structure_foreach (structure, update_upstream_provided, default_caps);
1077 default_caps = gst_caps_fixate (default_caps);
1079 if (!default_caps) {
1080 GST_WARNING_OBJECT (parse, "Failed to create default caps !");
1084 GST_INFO_OBJECT (parse,
1085 "Chose default caps %" GST_PTR_FORMAT " for initial gap", default_caps);
1088 gst_caps_unref (sinkcaps);
1089 gst_caps_unref (caps);
1091 return default_caps;
1096 gst_caps_unref (caps);
1098 gst_caps_unref (sinkcaps);
1103 /* gst_base_parse_sink_event:
1104 * @pad: #GstPad that received the event.
1105 * @event: #GstEvent to be handled.
1107 * Handler for sink pad events.
1109 * Returns: %TRUE if the event was handled.
1112 gst_base_parse_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
1114 GstBaseParse *parse = GST_BASE_PARSE (parent);
1115 GstBaseParseClass *bclass = GST_BASE_PARSE_GET_CLASS (parse);
1118 ret = bclass->sink_event (parse, event);
1123 /* gst_base_parse_sink_event_default:
1124 * @parse: #GstBaseParse.
1125 * @event: #GstEvent to be handled.
1127 * Element-level event handler function.
1129 * The event will be unreffed only if it has been handled and this
1130 * function returns %TRUE
1132 * Returns: %TRUE if the event was handled and not need forwarding.
1135 gst_base_parse_sink_event_default (GstBaseParse * parse, GstEvent * event)
1137 GstBaseParseClass *klass = GST_BASE_PARSE_GET_CLASS (parse);
1138 gboolean ret = FALSE;
1139 gboolean forward_immediate = FALSE;
1141 GST_DEBUG_OBJECT (parse, "handling event %d, %s", GST_EVENT_TYPE (event),
1142 GST_EVENT_TYPE_NAME (event));
1144 switch (GST_EVENT_TYPE (event)) {
1145 case GST_EVENT_CAPS:
1149 gst_event_parse_caps (event, &caps);
1150 GST_DEBUG_OBJECT (parse, "caps: %" GST_PTR_FORMAT, caps);
1152 if (klass->set_sink_caps)
1153 ret = klass->set_sink_caps (parse, caps);
1157 /* will send our own caps downstream */
1158 gst_event_unref (event);
1162 case GST_EVENT_SEGMENT:
1164 const GstSegment *in_segment;
1165 GstSegment out_segment;
1166 gint64 offset = 0, next_dts;
1167 guint32 seqnum = gst_event_get_seqnum (event);
1169 gst_event_parse_segment (event, &in_segment);
1170 gst_segment_init (&out_segment, GST_FORMAT_TIME);
1171 out_segment.rate = in_segment->rate;
1172 out_segment.applied_rate = in_segment->applied_rate;
1174 GST_DEBUG_OBJECT (parse, "segment %" GST_SEGMENT_FORMAT, in_segment);
1176 parse->priv->upstream_format = in_segment->format;
1177 if (in_segment->format == GST_FORMAT_BYTES) {
1178 GstBaseParseSeek *seek = NULL;
1181 /* stop time is allowed to be open-ended, but not start & pos */
1182 offset = in_segment->time;
1184 GST_OBJECT_LOCK (parse);
1185 for (node = parse->priv->pending_seeks; node; node = node->next) {
1186 GstBaseParseSeek *tmp = node->data;
1188 if (tmp->offset == offset) {
1193 parse->priv->pending_seeks =
1194 g_slist_remove (parse->priv->pending_seeks, seek);
1195 GST_OBJECT_UNLOCK (parse);
1198 GST_DEBUG_OBJECT (parse,
1199 "Matched newsegment to%s seek: %" GST_SEGMENT_FORMAT,
1200 seek->accurate ? " accurate" : "", &seek->segment);
1202 out_segment.start = seek->segment.start;
1203 out_segment.stop = seek->segment.stop;
1204 out_segment.time = seek->segment.start;
1206 next_dts = seek->start_ts;
1207 parse->priv->exact_position = seek->accurate;
1210 /* best attempt convert */
1211 /* as these are only estimates, stop is kept open-ended to avoid
1212 * premature cutting */
1213 gst_base_parse_convert (parse, GST_FORMAT_BYTES, in_segment->start,
1214 GST_FORMAT_TIME, (gint64 *) & next_dts);
1216 out_segment.start = next_dts;
1217 out_segment.stop = GST_CLOCK_TIME_NONE;
1218 out_segment.time = next_dts;
1220 parse->priv->exact_position = (in_segment->start == 0);
1223 gst_event_unref (event);
1225 event = gst_event_new_segment (&out_segment);
1226 gst_event_set_seqnum (event, seqnum);
1228 GST_DEBUG_OBJECT (parse, "Converted incoming segment to TIME. %"
1229 GST_SEGMENT_FORMAT, in_segment);
1231 } else if (in_segment->format != GST_FORMAT_TIME) {
1232 /* Unknown incoming segment format. Output a default open-ended
1234 gst_event_unref (event);
1236 out_segment.start = 0;
1237 out_segment.stop = GST_CLOCK_TIME_NONE;
1238 out_segment.time = 0;
1240 event = gst_event_new_segment (&out_segment);
1241 gst_event_set_seqnum (event, seqnum);
1245 /* not considered BYTE seekable if it is talking to us in TIME,
1246 * whatever else it might claim */
1247 parse->priv->upstream_seekable = FALSE;
1248 next_dts = in_segment->start;
1249 gst_event_copy_segment (event, &out_segment);
1252 memcpy (&parse->segment, &out_segment, sizeof (GstSegment));
1255 gst_segment_set_newsegment (&parse->segment, update, rate,
1256 applied_rate, format, start, stop, start);
1261 /* save the segment for later, right before we push a new buffer so that
1262 * the caps are fixed and the next linked element can receive
1263 * the segment but finish the current segment */
1264 GST_DEBUG_OBJECT (parse, "draining current segment");
1265 if (in_segment->rate > 0.0)
1266 gst_base_parse_drain (parse);
1268 gst_base_parse_finish_fragment (parse, FALSE);
1269 gst_adapter_clear (parse->priv->adapter);
1271 parse->priv->offset = offset;
1272 parse->priv->sync_offset = offset;
1273 parse->priv->next_dts = next_dts;
1274 parse->priv->next_pts = GST_CLOCK_TIME_NONE;
1275 parse->priv->last_pts = GST_CLOCK_TIME_NONE;
1276 parse->priv->last_dts = GST_CLOCK_TIME_NONE;
1277 parse->priv->prev_pts = GST_CLOCK_TIME_NONE;
1278 parse->priv->prev_dts = GST_CLOCK_TIME_NONE;
1279 parse->priv->prev_dts_from_pts = FALSE;
1280 parse->priv->discont = TRUE;
1281 parse->priv->seen_keyframe = FALSE;
1282 parse->priv->skip = 0;
1286 case GST_EVENT_SEGMENT_DONE:
1287 /* need to drain now, rather than upon a new segment,
1288 * since that would have SEGMENT_DONE come before potential
1289 * delayed last part of the current segment */
1290 GST_DEBUG_OBJECT (parse, "draining current segment");
1291 if (parse->segment.rate > 0.0)
1292 gst_base_parse_drain (parse);
1294 gst_base_parse_finish_fragment (parse, FALSE);
1295 /* Also forward event immediately, there might be no new data
1296 * coming afterwards that would allow us to forward it later */
1297 forward_immediate = TRUE;
1300 case GST_EVENT_FLUSH_START:
1301 GST_OBJECT_LOCK (parse);
1302 parse->priv->flushing = TRUE;
1303 GST_OBJECT_UNLOCK (parse);
1306 case GST_EVENT_FLUSH_STOP:
1307 gst_adapter_clear (parse->priv->adapter);
1308 gst_base_parse_clear_queues (parse);
1309 parse->priv->flushing = FALSE;
1310 parse->priv->discont = TRUE;
1311 parse->priv->last_pts = GST_CLOCK_TIME_NONE;
1312 parse->priv->last_dts = GST_CLOCK_TIME_NONE;
1313 parse->priv->new_frame = TRUE;
1314 parse->priv->skip = 0;
1316 forward_immediate = TRUE;
1320 if (parse->segment.rate > 0.0)
1321 gst_base_parse_drain (parse);
1323 gst_base_parse_finish_fragment (parse, TRUE);
1325 /* If we STILL have zero frames processed, fire an error */
1326 if (parse->priv->framecount == 0 && !parse->priv->saw_gaps &&
1327 !parse->priv->first_buffer) {
1328 GST_ELEMENT_ERROR (parse, STREAM, WRONG_TYPE,
1329 ("No valid frames found before end of stream"), (NULL));
1332 if (!parse->priv->saw_gaps
1333 && parse->priv->framecount < MIN_FRAMES_TO_POST_BITRATE) {
1334 /* We've not posted bitrate tags yet - do so now */
1335 gst_base_parse_queue_tag_event_update (parse);
1338 /* newsegment and other serialized events before eos */
1339 gst_base_parse_push_pending_events (parse);
1341 forward_immediate = TRUE;
1343 case GST_EVENT_CUSTOM_DOWNSTREAM:{
1344 /* FIXME: Code duplicated from libgstvideo because core can't depend on -base */
1345 #ifndef GST_VIDEO_EVENT_STILL_STATE_NAME
1346 #define GST_VIDEO_EVENT_STILL_STATE_NAME "GstEventStillFrame"
1349 const GstStructure *s;
1350 gboolean ev_still_state;
1352 s = gst_event_get_structure (event);
1354 gst_structure_has_name (s, GST_VIDEO_EVENT_STILL_STATE_NAME) &&
1355 gst_structure_get_boolean (s, "still-state", &ev_still_state)) {
1356 if (ev_still_state) {
1357 GST_DEBUG_OBJECT (parse, "draining current data for still-frame");
1358 if (parse->segment.rate > 0.0)
1359 gst_base_parse_drain (parse);
1361 gst_base_parse_finish_fragment (parse, TRUE);
1363 forward_immediate = TRUE;
1369 GST_DEBUG_OBJECT (parse, "draining current data due to gap event");
1371 /* Ensure we have caps before forwarding the event */
1372 if (!gst_pad_has_current_caps (GST_BASE_PARSE_SRC_PAD (parse))) {
1373 GstCaps *default_caps = NULL;
1374 if ((default_caps = gst_base_parse_negotiate_default_caps (parse))) {
1376 GstEvent *caps_event = gst_event_new_caps (default_caps);
1378 GST_DEBUG_OBJECT (parse,
1379 "Store caps event to pending list for initial pre-rolling");
1381 /* Events are in decreasing order. Go down the list until we
1382 * find the first pre-CAPS event and insert our CAPS event there.
1384 * There should be a SEGMENT event already, which is > CAPS */
1385 for (l = parse->priv->pending_events; l; l = l->next) {
1386 GstEvent *e = l->data;
1388 if (GST_EVENT_TYPE (e) < GST_EVENT_CAPS) {
1389 parse->priv->pending_events =
1390 g_list_insert_before (parse->priv->pending_events, l,
1395 /* No pending event that is < CAPS, so we have to add it at the very
1396 * end of the list */
1398 parse->priv->pending_events =
1399 g_list_append (parse->priv->pending_events, caps_event);
1401 gst_caps_unref (default_caps);
1403 gst_event_unref (event);
1406 GST_ELEMENT_ERROR (parse, STREAM, FORMAT, (NULL),
1407 ("Parser output not negotiated before GAP event."));
1412 gst_base_parse_push_pending_events (parse);
1414 if (parse->segment.rate > 0.0)
1415 gst_base_parse_drain (parse);
1417 gst_base_parse_finish_fragment (parse, TRUE);
1418 forward_immediate = TRUE;
1419 parse->priv->saw_gaps = TRUE;
1424 GstTagList *tags = NULL;
1426 gst_event_parse_tag (event, &tags);
1428 /* We only care about stream tags here, global tags we just forward */
1429 if (gst_tag_list_get_scope (tags) != GST_TAG_SCOPE_STREAM)
1432 gst_base_parse_set_upstream_tags (parse, tags);
1433 gst_base_parse_queue_tag_event_update (parse);
1434 parse->priv->tags_changed = FALSE;
1435 gst_event_unref (event);
1440 case GST_EVENT_STREAM_START:
1442 if (parse->priv->pad_mode != GST_PAD_MODE_PULL)
1443 forward_immediate = TRUE;
1445 gst_base_parse_set_upstream_tags (parse, NULL);
1446 parse->priv->tags_changed = TRUE;
1453 /* Forward non-serialized events and EOS/FLUSH_STOP immediately.
1454 * For EOS this is required because no buffer or serialized event
1455 * will come after EOS and nothing could trigger another
1456 * _finish_frame() call. *
1457 * If the subclass handles sending of EOS manually it can return
1458 * _DROPPED from ::finish() and all other subclasses should have
1459 * decoded/flushed all remaining data before this
1461 * For FLUSH_STOP this is required because it is expected
1462 * to be forwarded immediately and no buffers are queued anyway.
1465 if (!GST_EVENT_IS_SERIALIZED (event) || forward_immediate) {
1466 ret = gst_pad_push_event (parse->srcpad, event);
1468 parse->priv->pending_events =
1469 g_list_prepend (parse->priv->pending_events, event);
1474 GST_DEBUG_OBJECT (parse, "event handled");
1480 gst_base_parse_sink_query_default (GstBaseParse * parse, GstQuery * query)
1485 pad = GST_BASE_PARSE_SINK_PAD (parse);
1487 switch (GST_QUERY_TYPE (query)) {
1488 case GST_QUERY_CAPS:
1490 GstBaseParseClass *bclass;
1492 bclass = GST_BASE_PARSE_GET_CLASS (parse);
1494 if (bclass->get_sink_caps) {
1495 GstCaps *caps, *filter;
1497 gst_query_parse_caps (query, &filter);
1498 caps = bclass->get_sink_caps (parse, filter);
1499 GST_LOG_OBJECT (parse, "sink getcaps returning caps %" GST_PTR_FORMAT,
1501 gst_query_set_caps_result (query, caps);
1502 gst_caps_unref (caps);
1506 GstCaps *caps, *template_caps, *filter;
1508 gst_query_parse_caps (query, &filter);
1509 template_caps = gst_pad_get_pad_template_caps (pad);
1510 if (filter != NULL) {
1512 gst_caps_intersect_full (filter, template_caps,
1513 GST_CAPS_INTERSECT_FIRST);
1514 gst_caps_unref (template_caps);
1516 caps = template_caps;
1518 gst_query_set_caps_result (query, caps);
1519 gst_caps_unref (caps);
1527 res = gst_pad_query_default (pad, GST_OBJECT_CAST (parse), query);
1536 gst_base_parse_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
1538 GstBaseParseClass *bclass;
1539 GstBaseParse *parse;
1542 parse = GST_BASE_PARSE (parent);
1543 bclass = GST_BASE_PARSE_GET_CLASS (parse);
1545 GST_DEBUG_OBJECT (parse, "%s query", GST_QUERY_TYPE_NAME (query));
1547 if (bclass->sink_query)
1548 ret = bclass->sink_query (parse, query);
1552 GST_LOG_OBJECT (parse, "%s query result: %d %" GST_PTR_FORMAT,
1553 GST_QUERY_TYPE_NAME (query), ret, query);
1559 gst_base_parse_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1561 GstBaseParseClass *bclass;
1562 GstBaseParse *parse;
1565 parse = GST_BASE_PARSE (parent);
1566 bclass = GST_BASE_PARSE_GET_CLASS (parse);
1568 GST_DEBUG_OBJECT (parse, "%s query: %" GST_PTR_FORMAT,
1569 GST_QUERY_TYPE_NAME (query), query);
1571 if (bclass->src_query)
1572 ret = bclass->src_query (parse, query);
1576 GST_LOG_OBJECT (parse, "%s query result: %d %" GST_PTR_FORMAT,
1577 GST_QUERY_TYPE_NAME (query), ret, query);
1582 /* gst_base_parse_src_event:
1583 * @pad: #GstPad that received the event.
1584 * @event: #GstEvent that was received.
1586 * Handler for source pad events.
1588 * Returns: %TRUE if the event was handled.
1591 gst_base_parse_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1593 GstBaseParse *parse;
1594 GstBaseParseClass *bclass;
1595 gboolean ret = TRUE;
1597 parse = GST_BASE_PARSE (parent);
1598 bclass = GST_BASE_PARSE_GET_CLASS (parse);
1600 GST_DEBUG_OBJECT (parse, "event %d, %s", GST_EVENT_TYPE (event),
1601 GST_EVENT_TYPE_NAME (event));
1603 if (bclass->src_event)
1604 ret = bclass->src_event (parse, event);
1606 gst_event_unref (event);
1612 gst_base_parse_is_seekable (GstBaseParse * parse)
1614 /* FIXME: could do more here, e.g. check index or just send data from 0
1615 * in pull mode and let decoder/sink clip */
1616 return parse->priv->syncable;
1619 /* gst_base_parse_src_event_default:
1620 * @parse: #GstBaseParse.
1621 * @event: #GstEvent that was received.
1623 * Default srcpad event handler.
1625 * Returns: %TRUE if the event was handled and can be dropped.
1628 gst_base_parse_src_event_default (GstBaseParse * parse, GstEvent * event)
1630 gboolean res = FALSE;
1632 switch (GST_EVENT_TYPE (event)) {
1633 case GST_EVENT_SEEK:
1634 if (gst_base_parse_is_seekable (parse))
1635 res = gst_base_parse_handle_seek (parse, event);
1638 res = gst_pad_event_default (parse->srcpad, GST_OBJECT_CAST (parse),
1647 * gst_base_parse_convert_default:
1648 * @parse: #GstBaseParse.
1649 * @src_format: #GstFormat describing the source format.
1650 * @src_value: Source value to be converted.
1651 * @dest_format: #GstFormat defining the converted format.
1652 * @dest_value: (out): Pointer where the conversion result will be put.
1654 * Default implementation of "convert" vmethod in #GstBaseParse class.
1656 * Returns: %TRUE if conversion was successful.
1659 gst_base_parse_convert_default (GstBaseParse * parse,
1660 GstFormat src_format,
1661 gint64 src_value, GstFormat dest_format, gint64 * dest_value)
1663 gboolean ret = FALSE;
1664 guint64 bytes, duration;
1666 if (G_UNLIKELY (src_format == dest_format)) {
1667 *dest_value = src_value;
1671 if (G_UNLIKELY (src_value == -1)) {
1676 if (G_UNLIKELY (src_value == 0)) {
1681 if (parse->priv->upstream_format != GST_FORMAT_BYTES) {
1682 /* don't do byte format conversions if we're not really parsing
1683 * a raw elementary stream, since we don't really have BYTES
1684 * position / duration info */
1685 if (src_format == GST_FORMAT_BYTES || dest_format == GST_FORMAT_BYTES)
1686 goto no_slaved_conversions;
1689 /* need at least some frames */
1690 if (!parse->priv->framecount)
1693 duration = parse->priv->acc_duration / GST_MSECOND;
1694 bytes = parse->priv->bytecount;
1696 if (G_UNLIKELY (!duration || !bytes))
1697 goto no_duration_bytes;
1699 if (src_format == GST_FORMAT_BYTES) {
1700 if (dest_format == GST_FORMAT_TIME) {
1701 /* BYTES -> TIME conversion */
1702 GST_DEBUG_OBJECT (parse, "converting bytes -> time");
1703 *dest_value = gst_util_uint64_scale (src_value, duration, bytes);
1704 *dest_value *= GST_MSECOND;
1705 GST_DEBUG_OBJECT (parse, "conversion result: %" G_GINT64_FORMAT " ms",
1706 *dest_value / GST_MSECOND);
1709 GST_DEBUG_OBJECT (parse, "converting bytes -> other not implemented");
1711 } else if (src_format == GST_FORMAT_TIME) {
1712 if (dest_format == GST_FORMAT_BYTES) {
1713 GST_DEBUG_OBJECT (parse, "converting time -> bytes");
1714 *dest_value = gst_util_uint64_scale (src_value / GST_MSECOND, bytes,
1716 GST_DEBUG_OBJECT (parse,
1717 "time %" G_GINT64_FORMAT " ms in bytes = %" G_GINT64_FORMAT,
1718 src_value / GST_MSECOND, *dest_value);
1721 GST_DEBUG_OBJECT (parse, "converting time -> other not implemented");
1723 } else if (src_format == GST_FORMAT_DEFAULT) {
1724 /* DEFAULT == frame-based */
1725 if (dest_format == GST_FORMAT_TIME) {
1726 GST_DEBUG_OBJECT (parse, "converting default -> time");
1727 if (parse->priv->fps_den) {
1728 *dest_value = gst_util_uint64_scale (src_value,
1729 GST_SECOND * parse->priv->fps_den, parse->priv->fps_num);
1733 GST_DEBUG_OBJECT (parse, "converting default -> other not implemented");
1736 GST_DEBUG_OBJECT (parse, "conversion not implemented");
1743 GST_DEBUG_OBJECT (parse, "no framecount");
1748 GST_DEBUG_OBJECT (parse, "no duration %" G_GUINT64_FORMAT ", bytes %"
1749 G_GUINT64_FORMAT, duration, bytes);
1752 no_slaved_conversions:
1754 GST_DEBUG_OBJECT (parse,
1755 "Can't do format conversions when upstream format is not BYTES");
1761 gst_base_parse_update_duration (GstBaseParse * parse)
1763 gint64 ptot, dest_value;
1765 if (!gst_pad_peer_query_duration (parse->sinkpad, GST_FORMAT_BYTES, &ptot))
1768 if (!gst_base_parse_convert (parse, GST_FORMAT_BYTES, ptot,
1769 GST_FORMAT_TIME, &dest_value))
1772 /* inform if duration changed, but try to avoid spamming */
1773 parse->priv->estimated_drift += dest_value - parse->priv->estimated_duration;
1775 parse->priv->estimated_duration = dest_value;
1776 GST_LOG_OBJECT (parse,
1777 "updated estimated duration to %" GST_TIME_FORMAT,
1778 GST_TIME_ARGS (dest_value));
1780 if (parse->priv->estimated_drift > GST_SECOND ||
1781 parse->priv->estimated_drift < -GST_SECOND) {
1782 gst_element_post_message (GST_ELEMENT (parse),
1783 gst_message_new_duration_changed (GST_OBJECT (parse)));
1784 parse->priv->estimated_drift = 0;
1788 /* gst_base_parse_update_bitrates:
1789 * @parse: #GstBaseParse.
1790 * @buffer: Current frame as a #GstBuffer
1792 * Keeps track of the minimum and maximum bitrates, and also maintains a
1793 * running average bitrate of the stream so far.
1796 gst_base_parse_update_bitrates (GstBaseParse * parse, GstBaseParseFrame * frame)
1798 guint64 data_len, frame_dur;
1800 guint frame_bitrate;
1801 guint64 frame_bitrate64;
1802 GstBuffer *buffer = frame->buffer;
1804 overhead = frame->overhead;
1808 data_len = gst_buffer_get_size (buffer) - overhead;
1809 parse->priv->data_bytecount += data_len;
1811 /* duration should be valid by now,
1812 * either set by subclass or maybe based on fps settings */
1813 if (GST_BUFFER_DURATION_IS_VALID (buffer) && parse->priv->acc_duration != 0) {
1814 guint64 avg_bitrate;
1816 /* Calculate duration of a frame from buffer properties */
1817 frame_dur = GST_BUFFER_DURATION (buffer);
1818 avg_bitrate = gst_util_uint64_scale (GST_SECOND,
1819 8 * parse->priv->data_bytecount, parse->priv->acc_duration);
1821 if (avg_bitrate > G_MAXUINT)
1824 parse->priv->avg_bitrate = avg_bitrate;
1826 /* No way to figure out frame duration (is this even possible?) */
1830 /* override if subclass provided bitrate, e.g. metadata based */
1831 if (parse->priv->bitrate) {
1832 parse->priv->avg_bitrate = parse->priv->bitrate;
1833 /* spread this (confirmed) info ASAP */
1834 if (parse->priv->post_avg_bitrate &&
1835 parse->priv->posted_avg_bitrate != parse->priv->avg_bitrate)
1836 parse->priv->tags_changed = TRUE;
1842 frame_bitrate64 = gst_util_uint64_scale (GST_SECOND, 8 * data_len, frame_dur);
1844 if (frame_bitrate64 > G_MAXUINT)
1847 frame_bitrate = (guint) frame_bitrate64;
1849 GST_LOG_OBJECT (parse, "frame bitrate %u, avg bitrate %u", frame_bitrate,
1850 parse->priv->avg_bitrate);
1852 if (parse->priv->framecount < MIN_FRAMES_TO_POST_BITRATE)
1855 if (parse->priv->framecount == MIN_FRAMES_TO_POST_BITRATE &&
1856 (parse->priv->post_min_bitrate || parse->priv->post_avg_bitrate
1857 || parse->priv->post_max_bitrate))
1858 parse->priv->tags_changed = TRUE;
1860 if (G_LIKELY (parse->priv->framecount >= MIN_FRAMES_TO_POST_BITRATE)) {
1861 if (frame_bitrate < parse->priv->min_bitrate) {
1862 parse->priv->min_bitrate = frame_bitrate;
1863 if (parse->priv->post_min_bitrate)
1864 parse->priv->tags_changed = TRUE;
1867 if (frame_bitrate > parse->priv->max_bitrate) {
1868 parse->priv->max_bitrate = frame_bitrate;
1869 if (parse->priv->post_max_bitrate)
1870 parse->priv->tags_changed = TRUE;
1873 /* Only update the tag on a 2% change */
1874 if (parse->priv->post_avg_bitrate && parse->priv->avg_bitrate) {
1875 guint64 diffprev = gst_util_uint64_scale_int (100,
1876 ABSDIFF (parse->priv->avg_bitrate, parse->priv->posted_avg_bitrate),
1877 parse->priv->avg_bitrate);
1878 if (diffprev >= UPDATE_THRESHOLD)
1879 parse->priv->tags_changed = TRUE;
1885 * gst_base_parse_add_index_entry:
1886 * @parse: #GstBaseParse.
1887 * @offset: offset of entry
1888 * @ts: timestamp associated with offset
1889 * @key: whether entry refers to keyframe
1890 * @force: add entry disregarding sanity checks
1892 * Adds an entry to the index associating @offset to @ts. It is recommended
1893 * to only add keyframe entries. @force allows to bypass checks, such as
1894 * whether the stream is (upstream) seekable, another entry is already "close"
1895 * to the new entry, etc.
1897 * Returns: #gboolean indicating whether entry was added
1900 gst_base_parse_add_index_entry (GstBaseParse * parse, guint64 offset,
1901 GstClockTime ts, gboolean key, gboolean force)
1903 gboolean ret = FALSE;
1904 GstIndexAssociation associations[2];
1906 GST_LOG_OBJECT (parse, "Adding key=%d index entry %" GST_TIME_FORMAT
1907 " @ offset 0x%08" G_GINT64_MODIFIER "x", key, GST_TIME_ARGS (ts), offset);
1909 if (G_LIKELY (!force)) {
1911 if (!parse->priv->upstream_seekable) {
1912 GST_DEBUG_OBJECT (parse, "upstream not seekable; discarding");
1916 /* FIXME need better helper data structure that handles these issues
1917 * related to ongoing collecting of index entries */
1918 if (parse->priv->index_last_offset + parse->priv->idx_byte_interval >=
1920 GST_LOG_OBJECT (parse,
1921 "already have entries up to offset 0x%08" G_GINT64_MODIFIER "x",
1922 parse->priv->index_last_offset + parse->priv->idx_byte_interval);
1926 if (GST_CLOCK_TIME_IS_VALID (parse->priv->index_last_ts) &&
1927 GST_CLOCK_DIFF (parse->priv->index_last_ts, ts) <
1928 parse->priv->idx_interval) {
1929 GST_LOG_OBJECT (parse, "entry too close to last time %" GST_TIME_FORMAT,
1930 GST_TIME_ARGS (parse->priv->index_last_ts));
1934 /* if last is not really the last one */
1935 if (!parse->priv->index_last_valid) {
1936 GstClockTime prev_ts;
1938 gst_base_parse_find_offset (parse, ts, TRUE, &prev_ts);
1939 if (GST_CLOCK_DIFF (prev_ts, ts) < parse->priv->idx_interval) {
1940 GST_LOG_OBJECT (parse,
1941 "entry too close to existing entry %" GST_TIME_FORMAT,
1942 GST_TIME_ARGS (prev_ts));
1943 parse->priv->index_last_offset = offset;
1944 parse->priv->index_last_ts = ts;
1950 associations[0].format = GST_FORMAT_TIME;
1951 associations[0].value = ts;
1952 associations[1].format = GST_FORMAT_BYTES;
1953 associations[1].value = offset;
1955 /* index might change on-the-fly, although that would be nutty app ... */
1956 GST_BASE_PARSE_INDEX_LOCK (parse);
1957 gst_index_add_associationv (parse->priv->index, parse->priv->index_id,
1958 (key) ? GST_INDEX_ASSOCIATION_FLAG_KEY_UNIT :
1959 GST_INDEX_ASSOCIATION_FLAG_DELTA_UNIT, 2,
1960 (const GstIndexAssociation *) &associations);
1961 GST_BASE_PARSE_INDEX_UNLOCK (parse);
1964 parse->priv->index_last_offset = offset;
1965 parse->priv->index_last_ts = ts;
1974 /* check for seekable upstream, above and beyond a mere query */
1976 gst_base_parse_check_seekability (GstBaseParse * parse)
1979 gboolean seekable = FALSE;
1980 gint64 start = -1, stop = -1;
1981 guint idx_interval = 0;
1982 guint64 idx_byte_interval = 0;
1984 query = gst_query_new_seeking (GST_FORMAT_BYTES);
1985 if (!gst_pad_peer_query (parse->sinkpad, query)) {
1986 GST_DEBUG_OBJECT (parse, "seeking query failed");
1990 gst_query_parse_seeking (query, NULL, &seekable, &start, &stop);
1992 /* try harder to query upstream size if we didn't get it the first time */
1993 if (seekable && stop == -1) {
1994 GST_DEBUG_OBJECT (parse, "doing duration query to fix up unset stop");
1995 gst_pad_peer_query_duration (parse->sinkpad, GST_FORMAT_BYTES, &stop);
1998 /* if upstream doesn't know the size, it's likely that it's not seekable in
1999 * practice even if it technically may be seekable */
2000 if (seekable && (start != 0 || stop <= start)) {
2001 GST_DEBUG_OBJECT (parse, "seekable but unknown start/stop -> disable");
2005 /* let's not put every single frame into our index */
2007 if (stop < 10 * 1024 * 1024)
2009 else if (stop < 100 * 1024 * 1024)
2012 idx_interval = 1000;
2014 /* ensure that even for large files (e.g. very long audio files), the index
2015 * stays reasonably-size, with some arbitrary limit to the total number of
2017 idx_byte_interval = (stop - start) / MAX_INDEX_ENTRIES;
2018 GST_DEBUG_OBJECT (parse,
2019 "Limiting index entries to %d, indexing byte interval %"
2020 G_GUINT64_FORMAT " bytes", MAX_INDEX_ENTRIES, idx_byte_interval);
2024 gst_query_unref (query);
2026 GST_DEBUG_OBJECT (parse, "seekable: %d (%" G_GUINT64_FORMAT " - %"
2027 G_GUINT64_FORMAT ")", seekable, start, stop);
2028 parse->priv->upstream_seekable = seekable;
2029 parse->priv->upstream_size = seekable ? stop : 0;
2031 GST_DEBUG_OBJECT (parse, "idx_interval: %ums", idx_interval);
2032 parse->priv->idx_interval = idx_interval * GST_MSECOND;
2033 parse->priv->idx_byte_interval = idx_byte_interval;
2036 /* some misc checks on upstream */
2038 gst_base_parse_check_upstream (GstBaseParse * parse)
2042 if (gst_pad_peer_query_duration (parse->sinkpad, GST_FORMAT_TIME, &stop))
2043 if (GST_CLOCK_TIME_IS_VALID (stop) && stop) {
2044 /* upstream has one, accept it also, and no further updates */
2045 gst_base_parse_set_duration (parse, GST_FORMAT_TIME, stop, 0);
2046 parse->priv->upstream_has_duration = TRUE;
2049 GST_DEBUG_OBJECT (parse, "upstream_has_duration: %d",
2050 parse->priv->upstream_has_duration);
2053 /* checks src caps to determine if dealing with audio or video */
2054 /* TODO maybe forego automagic stuff and let subclass configure it ? */
2056 gst_base_parse_check_media (GstBaseParse * parse)
2061 caps = gst_pad_get_current_caps (parse->srcpad);
2062 if (G_LIKELY (caps) && (s = gst_caps_get_structure (caps, 0))) {
2063 parse->priv->is_video =
2064 g_str_has_prefix (gst_structure_get_name (s), "video");
2066 /* historical default */
2067 parse->priv->is_video = FALSE;
2070 gst_caps_unref (caps);
2072 parse->priv->checked_media = TRUE;
2073 GST_DEBUG_OBJECT (parse, "media is video: %d", parse->priv->is_video);
2076 /* takes ownership of frame */
2078 gst_base_parse_queue_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
2080 if (!(frame->_private_flags & GST_BASE_PARSE_FRAME_PRIVATE_FLAG_NOALLOC)) {
2081 /* frame allocated on the heap, we can just take ownership */
2082 g_queue_push_tail (&parse->priv->queued_frames, frame);
2083 GST_TRACE ("queued frame %p", frame);
2085 GstBaseParseFrame *copy;
2087 /* probably allocated on the stack, must make a proper copy */
2088 copy = gst_base_parse_frame_copy (frame);
2089 g_queue_push_tail (&parse->priv->queued_frames, copy);
2090 GST_TRACE ("queued frame %p (copy of %p)", copy, frame);
2091 gst_base_parse_frame_free (frame);
2095 /* makes sure that @buf is properly prepared and decorated for passing
2096 * to baseclass, and an equally setup frame is returned setup with @buf.
2097 * Takes ownership of @buf. */
2098 static GstBaseParseFrame *
2099 gst_base_parse_prepare_frame (GstBaseParse * parse, GstBuffer * buffer)
2101 GstBaseParseFrame *frame = NULL;
2103 buffer = gst_buffer_make_writable (buffer);
2105 GST_LOG_OBJECT (parse,
2106 "preparing frame at offset %" G_GUINT64_FORMAT
2107 " (%#" G_GINT64_MODIFIER "x) of size %" G_GSIZE_FORMAT,
2108 GST_BUFFER_OFFSET (buffer), GST_BUFFER_OFFSET (buffer),
2109 gst_buffer_get_size (buffer));
2111 GST_BUFFER_OFFSET (buffer) = parse->priv->offset;
2113 gst_base_parse_update_flags (parse);
2115 frame = gst_base_parse_frame_new (buffer, 0, 0);
2116 gst_buffer_unref (buffer);
2117 gst_base_parse_update_frame (parse, frame);
2119 /* clear flags for next frame */
2120 parse->priv->discont = FALSE;
2121 parse->priv->new_frame = FALSE;
2123 /* use default handler to provide initial (upstream) metadata */
2124 gst_base_parse_parse_frame (parse, frame);
2129 /* Wraps buffer in a frame and dispatches to subclass.
2130 * Also manages data skipping and offset handling (including adapter flushing).
2131 * Takes ownership of @buffer */
2132 static GstFlowReturn
2133 gst_base_parse_handle_buffer (GstBaseParse * parse, GstBuffer * buffer,
2134 gint * skip, gint * flushed)
2136 GstBaseParseClass *klass = GST_BASE_PARSE_GET_CLASS (parse);
2137 GstBaseParseFrame *frame;
2140 g_return_val_if_fail (skip != NULL || flushed != NULL, GST_FLOW_ERROR);
2142 GST_LOG_OBJECT (parse,
2143 "handling buffer of size %" G_GSIZE_FORMAT " with dts %" GST_TIME_FORMAT
2144 ", pts %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT,
2145 gst_buffer_get_size (buffer), GST_TIME_ARGS (GST_BUFFER_DTS (buffer)),
2146 GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
2147 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
2149 /* track what is being flushed during this single round of frame processing */
2150 parse->priv->flushed = 0;
2153 /* make it easy for _finish_frame to pick up input data */
2154 if (parse->priv->pad_mode == GST_PAD_MODE_PULL) {
2155 gst_buffer_ref (buffer);
2156 gst_adapter_push (parse->priv->adapter, buffer);
2159 frame = gst_base_parse_prepare_frame (parse, buffer);
2160 ret = klass->handle_frame (parse, frame, skip);
2162 *flushed = parse->priv->flushed;
2164 GST_LOG_OBJECT (parse, "handle_frame skipped %d, flushed %d",
2167 /* subclass can only do one of these, or semantics are too unclear */
2168 g_assert (*skip == 0 || *flushed == 0);
2170 /* track skipping */
2172 GstClockTime pts, dts;
2175 GST_LOG_OBJECT (parse, "finding sync, skipping %d bytes", *skip);
2176 if (parse->segment.rate < 0.0 && !parse->priv->buffers_queued) {
2177 /* reverse playback, and no frames found yet, so we are skipping
2178 * the leading part of a fragment, which may form the tail of
2179 * fragment coming later, hopefully subclass skips efficiently ... */
2180 pts = gst_adapter_prev_pts (parse->priv->adapter, NULL);
2181 dts = gst_adapter_prev_dts (parse->priv->adapter, NULL);
2182 outbuf = gst_adapter_take_buffer (parse->priv->adapter, *skip);
2183 outbuf = gst_buffer_make_writable (outbuf);
2184 GST_BUFFER_PTS (outbuf) = pts;
2185 GST_BUFFER_DTS (outbuf) = dts;
2186 parse->priv->buffers_head =
2187 g_slist_prepend (parse->priv->buffers_head, outbuf);
2190 /* If we're asked to skip more than is available in the adapter,
2191 we need to remember what we need to skip for next iteration */
2192 gsize av = gst_adapter_available (parse->priv->adapter);
2193 GST_DEBUG ("Asked to skip %u (%" G_GSIZE_FORMAT " available)", *skip, av);
2195 gst_adapter_flush (parse->priv->adapter, *skip);
2198 ("This is more than available, flushing %" G_GSIZE_FORMAT
2199 ", storing %u to skip", av, (guint) (*skip - av));
2200 parse->priv->skip = *skip - av;
2201 gst_adapter_flush (parse->priv->adapter, av);
2205 if (!parse->priv->discont)
2206 parse->priv->sync_offset = parse->priv->offset;
2207 parse->priv->offset += *skip;
2208 parse->priv->discont = TRUE;
2209 /* check for indefinite skipping */
2210 if (ret == GST_FLOW_OK)
2211 ret = gst_base_parse_check_sync (parse);
2214 parse->priv->offset += *flushed;
2216 if (parse->priv->pad_mode == GST_PAD_MODE_PULL) {
2217 gst_adapter_clear (parse->priv->adapter);
2220 if (*skip == 0 && *flushed == 0) {
2221 /* Carry over discont if we need more data */
2222 if (GST_BUFFER_IS_DISCONT (frame->buffer))
2223 parse->priv->discont = TRUE;
2226 gst_base_parse_frame_free (frame);
2231 /* gst_base_parse_push_pending_events:
2232 * @parse: #GstBaseParse
2234 * Pushes the pending events
2237 gst_base_parse_push_pending_events (GstBaseParse * parse)
2239 if (G_UNLIKELY (parse->priv->pending_events)) {
2240 GList *r = g_list_reverse (parse->priv->pending_events);
2243 parse->priv->pending_events = NULL;
2244 for (l = r; l != NULL; l = l->next) {
2245 gst_pad_push_event (parse->srcpad, GST_EVENT_CAST (l->data));
2251 /* gst_base_parse_handle_and_push_frame:
2252 * @parse: #GstBaseParse.
2253 * @klass: #GstBaseParseClass.
2254 * @frame: (transfer full): a #GstBaseParseFrame
2256 * Parses the frame from given buffer and pushes it forward. Also performs
2257 * timestamp handling and checks the segment limits.
2259 * This is called with srcpad STREAM_LOCK held.
2261 * Returns: #GstFlowReturn
2263 static GstFlowReturn
2264 gst_base_parse_handle_and_push_frame (GstBaseParse * parse,
2265 GstBaseParseFrame * frame)
2270 g_return_val_if_fail (frame != NULL, GST_FLOW_ERROR);
2272 buffer = frame->buffer;
2273 offset = frame->offset;
2275 /* check if subclass/format can provide ts.
2276 * If so, that allows and enables extra seek and duration determining options */
2277 if (G_UNLIKELY (parse->priv->first_frame_offset < 0)) {
2278 if (GST_BUFFER_PTS_IS_VALID (buffer) && parse->priv->has_timing_info
2279 && parse->priv->pad_mode == GST_PAD_MODE_PULL) {
2280 parse->priv->first_frame_offset = offset;
2281 parse->priv->first_frame_pts = GST_BUFFER_PTS (buffer);
2282 parse->priv->first_frame_dts = GST_BUFFER_DTS (buffer);
2283 GST_DEBUG_OBJECT (parse, "subclass provided dts %" GST_TIME_FORMAT
2284 ", pts %" GST_TIME_FORMAT " for first frame at offset %"
2285 G_GINT64_FORMAT, GST_TIME_ARGS (parse->priv->first_frame_dts),
2286 GST_TIME_ARGS (parse->priv->first_frame_pts),
2287 parse->priv->first_frame_offset);
2288 if (!GST_CLOCK_TIME_IS_VALID (parse->priv->duration)) {
2290 GstClockTime last_ts = G_MAXINT64;
2292 GST_DEBUG_OBJECT (parse, "no duration; trying scan to determine");
2293 gst_base_parse_locate_time (parse, &last_ts, &off);
2294 if (GST_CLOCK_TIME_IS_VALID (last_ts))
2295 gst_base_parse_set_duration (parse, GST_FORMAT_TIME, last_ts, 0);
2298 /* disable further checks */
2299 parse->priv->first_frame_offset = 0;
2303 /* track upstream time if provided, not subclass' internal notion of it */
2304 if (parse->priv->upstream_format == GST_FORMAT_TIME) {
2305 GST_BUFFER_PTS (frame->buffer) = GST_CLOCK_TIME_NONE;
2306 GST_BUFFER_DTS (frame->buffer) = GST_CLOCK_TIME_NONE;
2309 /* interpolating and no valid pts yet,
2310 * start with dts and carry on from there */
2311 if (parse->priv->infer_ts && parse->priv->pts_interpolate
2312 && !GST_CLOCK_TIME_IS_VALID (parse->priv->next_pts))
2313 parse->priv->next_pts = parse->priv->next_dts;
2315 /* again use default handler to add missing metadata;
2316 * we may have new information on frame properties */
2317 gst_base_parse_parse_frame (parse, frame);
2319 parse->priv->next_pts = GST_CLOCK_TIME_NONE;
2320 if (GST_BUFFER_DTS_IS_VALID (buffer) && GST_BUFFER_DURATION_IS_VALID (buffer)) {
2321 parse->priv->next_dts =
2322 GST_BUFFER_DTS (buffer) + GST_BUFFER_DURATION (buffer);
2323 if (parse->priv->pts_interpolate && GST_BUFFER_PTS_IS_VALID (buffer)) {
2324 GstClockTime next_pts =
2325 GST_BUFFER_PTS (buffer) + GST_BUFFER_DURATION (buffer);
2326 if (next_pts >= parse->priv->next_dts)
2327 parse->priv->next_pts = next_pts;
2330 /* we lost track, do not produce bogus time next time around
2331 * (probably means parser subclass has given up on parsing as well) */
2332 GST_DEBUG_OBJECT (parse, "no next fallback timestamp");
2333 parse->priv->next_dts = GST_CLOCK_TIME_NONE;
2336 if (parse->priv->upstream_seekable && parse->priv->exact_position &&
2337 GST_BUFFER_PTS_IS_VALID (buffer))
2338 gst_base_parse_add_index_entry (parse, offset,
2339 GST_BUFFER_PTS (buffer),
2340 !GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT), FALSE);
2342 /* All OK, push queued frames if there are any */
2343 if (G_UNLIKELY (!g_queue_is_empty (&parse->priv->queued_frames))) {
2344 GstBaseParseFrame *queued_frame;
2346 while ((queued_frame = g_queue_pop_head (&parse->priv->queued_frames))) {
2347 gst_base_parse_push_frame (parse, queued_frame);
2348 gst_base_parse_frame_free (queued_frame);
2352 return gst_base_parse_push_frame (parse, frame);
2356 * gst_base_parse_push_frame:
2357 * @parse: #GstBaseParse.
2358 * @frame: (transfer none): a #GstBaseParseFrame
2360 * Pushes the frame's buffer downstream, sends any pending events and
2361 * does some timestamp and segment handling. Takes ownership of
2362 * frame's buffer, though caller retains ownership of @frame.
2364 * This must be called with sinkpad STREAM_LOCK held.
2366 * Returns: #GstFlowReturn
2369 gst_base_parse_push_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
2371 GstFlowReturn ret = GST_FLOW_OK;
2372 GstClockTime last_start = GST_CLOCK_TIME_NONE;
2373 GstClockTime last_stop = GST_CLOCK_TIME_NONE;
2374 GstBaseParseClass *klass = GST_BASE_PARSE_GET_CLASS (parse);
2378 g_return_val_if_fail (frame != NULL, GST_FLOW_ERROR);
2379 g_return_val_if_fail (frame->buffer != NULL, GST_FLOW_ERROR);
2381 GST_TRACE_OBJECT (parse, "pushing frame %p", frame);
2383 buffer = frame->buffer;
2385 GST_LOG_OBJECT (parse,
2386 "processing buffer of size %" G_GSIZE_FORMAT " with dts %" GST_TIME_FORMAT
2387 ", pts %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT,
2388 gst_buffer_get_size (buffer),
2389 GST_TIME_ARGS (GST_BUFFER_DTS (buffer)),
2390 GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
2391 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
2394 parse->priv->bytecount += frame->size;
2395 if (G_LIKELY (!(frame->flags & GST_BASE_PARSE_FRAME_FLAG_NO_FRAME))) {
2396 parse->priv->framecount++;
2397 if (GST_BUFFER_DURATION_IS_VALID (buffer)) {
2398 parse->priv->acc_duration += GST_BUFFER_DURATION (buffer);
2401 /* 0 means disabled */
2402 if (parse->priv->update_interval < 0)
2403 parse->priv->update_interval = 50;
2404 else if (parse->priv->update_interval > 0 &&
2405 (parse->priv->framecount % parse->priv->update_interval) == 0)
2406 gst_base_parse_update_duration (parse);
2408 if (GST_BUFFER_PTS_IS_VALID (buffer))
2409 last_start = last_stop = GST_BUFFER_PTS (buffer);
2410 if (last_start != GST_CLOCK_TIME_NONE
2411 && GST_BUFFER_DURATION_IS_VALID (buffer))
2412 last_stop = last_start + GST_BUFFER_DURATION (buffer);
2414 /* should have caps by now */
2415 if (!gst_pad_has_current_caps (parse->srcpad))
2418 if (G_UNLIKELY (!parse->priv->checked_media)) {
2419 /* have caps; check identity */
2420 gst_base_parse_check_media (parse);
2423 if (parse->priv->tags_changed) {
2424 gst_base_parse_queue_tag_event_update (parse);
2425 parse->priv->tags_changed = FALSE;
2428 /* Push pending events, including SEGMENT events */
2429 gst_base_parse_push_pending_events (parse);
2431 /* segment adjustment magic; only if we are running the whole show */
2432 if (!parse->priv->passthrough && parse->segment.rate > 0.0 &&
2433 (parse->priv->pad_mode == GST_PAD_MODE_PULL ||
2434 parse->priv->upstream_seekable)) {
2436 if (GST_CLOCK_TIME_IS_VALID (parse->segment.position) &&
2437 GST_CLOCK_TIME_IS_VALID (last_start)) {
2438 GstClockTimeDiff diff;
2440 /* only send newsegments with increasing start times,
2441 * otherwise if these go back and forth downstream (sinks) increase
2442 * accumulated time and running_time */
2443 diff = GST_CLOCK_DIFF (parse->segment.position, last_start);
2444 if (G_UNLIKELY (diff > 2 * GST_SECOND
2445 && last_start > parse->segment.start
2446 && (!GST_CLOCK_TIME_IS_VALID (parse->segment.stop)
2447 || last_start < parse->segment.stop))) {
2449 GST_DEBUG_OBJECT (parse,
2450 "Gap of %" G_GINT64_FORMAT " ns detected in stream " "(%"
2451 GST_TIME_FORMAT " -> %" GST_TIME_FORMAT "). "
2452 "Sending updated SEGMENT events", diff,
2453 GST_TIME_ARGS (parse->segment.position),
2454 GST_TIME_ARGS (last_start));
2456 /* skip gap FIXME */
2457 gst_pad_push_event (parse->srcpad,
2458 gst_event_new_segment (&parse->segment));
2460 parse->segment.position = last_start;
2465 /* update bitrates and optionally post corresponding tags
2466 * (following newsegment) */
2467 gst_base_parse_update_bitrates (parse, frame);
2469 if (klass->pre_push_frame) {
2470 ret = klass->pre_push_frame (parse, frame);
2472 frame->flags |= GST_BASE_PARSE_FRAME_FLAG_CLIP;
2475 /* Push pending events, if there are any new ones
2476 * like tags added by pre_push_frame */
2477 if (parse->priv->tags_changed) {
2478 gst_base_parse_queue_tag_event_update (parse);
2479 parse->priv->tags_changed = FALSE;
2481 gst_base_parse_push_pending_events (parse);
2483 /* take final ownership of frame buffer */
2484 if (frame->out_buffer) {
2485 buffer = frame->out_buffer;
2486 frame->out_buffer = NULL;
2487 gst_buffer_replace (&frame->buffer, NULL);
2489 buffer = frame->buffer;
2490 frame->buffer = NULL;
2493 /* subclass must play nice */
2494 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
2496 size = gst_buffer_get_size (buffer);
2498 parse->priv->seen_keyframe |= parse->priv->is_video &&
2499 !GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
2501 if (frame->flags & GST_BASE_PARSE_FRAME_FLAG_CLIP) {
2502 if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
2503 GST_CLOCK_TIME_IS_VALID (parse->segment.stop) &&
2504 GST_BUFFER_TIMESTAMP (buffer) >
2505 parse->segment.stop + parse->priv->lead_out_ts) {
2506 GST_LOG_OBJECT (parse, "Dropped frame, after segment");
2508 } else if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
2509 GST_BUFFER_DURATION_IS_VALID (buffer) &&
2510 GST_CLOCK_TIME_IS_VALID (parse->segment.start) &&
2511 GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer) +
2512 parse->priv->lead_in_ts < parse->segment.start) {
2513 if (parse->priv->seen_keyframe) {
2514 GST_LOG_OBJECT (parse, "Frame before segment, after keyframe");
2517 GST_LOG_OBJECT (parse, "Dropped frame, before segment");
2518 ret = GST_BASE_PARSE_FLOW_DROPPED;
2525 if (ret == GST_BASE_PARSE_FLOW_DROPPED) {
2526 GST_LOG_OBJECT (parse, "frame (%" G_GSIZE_FORMAT " bytes) dropped", size);
2527 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT))
2528 parse->priv->discont = TRUE;
2529 gst_buffer_unref (buffer);
2531 } else if (ret == GST_FLOW_OK) {
2532 if (parse->segment.rate > 0.0) {
2533 GST_LOG_OBJECT (parse, "pushing frame (%" G_GSIZE_FORMAT " bytes) now..",
2535 ret = gst_pad_push (parse->srcpad, buffer);
2536 GST_LOG_OBJECT (parse, "frame pushed, flow %s", gst_flow_get_name (ret));
2537 } else if (!parse->priv->disable_passthrough && parse->priv->passthrough) {
2539 /* in backwards playback mode, if on passthrough we need to push buffers
2540 * directly without accumulating them into the buffers_queued as baseparse
2541 * will never check for a DISCONT while on passthrough and those buffers
2542 * will never be pushed.
2544 * also, as we are on reverse playback, it might be possible that
2545 * passthrough might have just been enabled, so make sure to drain the
2546 * buffers_queued list */
2547 if (G_UNLIKELY (parse->priv->buffers_queued != NULL)) {
2548 gst_base_parse_finish_fragment (parse, TRUE);
2549 ret = gst_base_parse_send_buffers (parse);
2552 if (ret == GST_FLOW_OK) {
2553 GST_LOG_OBJECT (parse,
2554 "pushing frame (%" G_GSIZE_FORMAT " bytes) now..", size);
2555 ret = gst_pad_push (parse->srcpad, buffer);
2556 GST_LOG_OBJECT (parse, "frame pushed, flow %s",
2557 gst_flow_get_name (ret));
2559 GST_LOG_OBJECT (parse,
2560 "frame (%" G_GSIZE_FORMAT " bytes) not pushed: %s", size,
2561 gst_flow_get_name (ret));
2562 gst_buffer_unref (buffer);
2566 GST_LOG_OBJECT (parse, "frame (%" G_GSIZE_FORMAT " bytes) queued for now",
2568 parse->priv->buffers_queued =
2569 g_slist_prepend (parse->priv->buffers_queued, buffer);
2573 GST_LOG_OBJECT (parse, "frame (%" G_GSIZE_FORMAT " bytes) not pushed: %s",
2574 size, gst_flow_get_name (ret));
2575 gst_buffer_unref (buffer);
2576 /* if we are not sufficiently in control, let upstream decide on EOS */
2577 if (ret == GST_FLOW_EOS && !parse->priv->disable_passthrough &&
2578 (parse->priv->passthrough ||
2579 (parse->priv->pad_mode == GST_PAD_MODE_PUSH &&
2580 !parse->priv->upstream_seekable)))
2584 /* Update current running segment position */
2585 if ((ret == GST_FLOW_OK || ret == GST_FLOW_NOT_LINKED)
2586 && last_stop != GST_CLOCK_TIME_NONE
2587 && parse->segment.position < last_stop)
2588 parse->segment.position = last_stop;
2595 if (GST_PAD_IS_FLUSHING (parse->srcpad))
2596 return GST_FLOW_FLUSHING;
2598 GST_ELEMENT_ERROR (parse, STREAM, DECODE, ("No caps set"), (NULL));
2599 return GST_FLOW_ERROR;
2604 * gst_base_parse_finish_frame:
2605 * @parse: a #GstBaseParse
2606 * @frame: a #GstBaseParseFrame
2607 * @size: consumed input data represented by frame
2609 * Collects parsed data and pushes this downstream.
2610 * Source pad caps must be set when this is called.
2612 * If @frame's out_buffer is set, that will be used as subsequent frame data.
2613 * Otherwise, @size samples will be taken from the input and used for output,
2614 * and the output's metadata (timestamps etc) will be taken as (optionally)
2615 * set by the subclass on @frame's (input) buffer (which is otherwise
2616 * ignored for any but the above purpose/information).
2618 * Note that the latter buffer is invalidated by this call, whereas the
2619 * caller retains ownership of @frame.
2621 * Returns: a #GstFlowReturn that should be escalated to caller (of caller)
2624 gst_base_parse_finish_frame (GstBaseParse * parse, GstBaseParseFrame * frame,
2627 GstFlowReturn ret = GST_FLOW_OK;
2629 g_return_val_if_fail (frame != NULL, GST_FLOW_ERROR);
2630 g_return_val_if_fail (frame->buffer != NULL, GST_FLOW_ERROR);
2631 g_return_val_if_fail (size > 0 || frame->out_buffer, GST_FLOW_ERROR);
2632 g_return_val_if_fail (gst_adapter_available (parse->priv->adapter) >= size,
2635 GST_LOG_OBJECT (parse, "finished frame at offset %" G_GUINT64_FORMAT ", "
2636 "flushing size %d", frame->offset, size);
2638 /* some one-time start-up */
2639 if (G_UNLIKELY (parse->priv->framecount == 0)) {
2640 gst_base_parse_check_seekability (parse);
2641 gst_base_parse_check_upstream (parse);
2644 parse->priv->flushed += size;
2646 if (parse->priv->scanning && frame->buffer) {
2647 if (!parse->priv->scanned_frame) {
2648 parse->priv->scanned_frame = gst_base_parse_frame_copy (frame);
2653 /* either PUSH or PULL mode arranges for adapter data */
2654 /* ensure output buffer */
2655 if (!frame->out_buffer) {
2656 GstBuffer *src, *dest;
2658 frame->out_buffer = gst_adapter_take_buffer (parse->priv->adapter, size);
2659 dest = frame->out_buffer;
2660 src = frame->buffer;
2661 GST_BUFFER_PTS (dest) = GST_BUFFER_PTS (src);
2662 GST_BUFFER_DTS (dest) = GST_BUFFER_DTS (src);
2663 GST_BUFFER_OFFSET (dest) = GST_BUFFER_OFFSET (src);
2664 GST_BUFFER_DURATION (dest) = GST_BUFFER_DURATION (src);
2665 GST_BUFFER_OFFSET_END (dest) = GST_BUFFER_OFFSET_END (src);
2666 GST_MINI_OBJECT_FLAGS (dest) = GST_MINI_OBJECT_FLAGS (src);
2668 gst_adapter_flush (parse->priv->adapter, size);
2671 /* use as input for subsequent processing */
2672 gst_buffer_replace (&frame->buffer, frame->out_buffer);
2673 gst_buffer_unref (frame->out_buffer);
2674 frame->out_buffer = NULL;
2676 /* mark input size consumed */
2679 /* subclass might queue frames/data internally if it needs more
2680 * frames to decide on the format, or might request us to queue here. */
2681 if (frame->flags & GST_BASE_PARSE_FRAME_FLAG_DROP) {
2682 gst_buffer_replace (&frame->buffer, NULL);
2684 } else if (frame->flags & GST_BASE_PARSE_FRAME_FLAG_QUEUE) {
2685 GstBaseParseFrame *copy;
2687 copy = gst_base_parse_frame_copy (frame);
2688 copy->flags &= ~GST_BASE_PARSE_FRAME_FLAG_QUEUE;
2689 gst_base_parse_queue_frame (parse, copy);
2693 ret = gst_base_parse_handle_and_push_frame (parse, frame);
2700 * gst_base_parse_drain:
2701 * @parse: a #GstBaseParse
2703 * Drains the adapter until it is empty. It decreases the min_frame_size to
2704 * match the current adapter size and calls chain method until the adapter
2705 * is emptied or chain returns with error.
2710 gst_base_parse_drain (GstBaseParse * parse)
2714 GST_DEBUG_OBJECT (parse, "draining");
2715 parse->priv->drain = TRUE;
2718 avail = gst_adapter_available (parse->priv->adapter);
2722 if (gst_base_parse_chain (parse->sinkpad, GST_OBJECT_CAST (parse),
2723 NULL) != GST_FLOW_OK) {
2727 /* nothing changed, maybe due to truncated frame; break infinite loop */
2728 if (avail == gst_adapter_available (parse->priv->adapter)) {
2729 GST_DEBUG_OBJECT (parse, "no change during draining; flushing");
2730 gst_adapter_clear (parse->priv->adapter);
2734 parse->priv->drain = FALSE;
2737 /* gst_base_parse_send_buffers
2739 * Sends buffers collected in send_buffers downstream, and ensures that list
2740 * is empty at the end (errors or not).
2742 static GstFlowReturn
2743 gst_base_parse_send_buffers (GstBaseParse * parse)
2745 GSList *send = NULL;
2747 GstFlowReturn ret = GST_FLOW_OK;
2748 gboolean first = TRUE;
2750 send = parse->priv->buffers_send;
2754 buf = GST_BUFFER_CAST (send->data);
2755 GST_LOG_OBJECT (parse, "pushing buffer %p, dts %"
2756 GST_TIME_FORMAT ", pts %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT
2757 ", offset %" G_GINT64_FORMAT, buf,
2758 GST_TIME_ARGS (GST_BUFFER_DTS (buf)),
2759 GST_TIME_ARGS (GST_BUFFER_PTS (buf)),
2760 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)), GST_BUFFER_OFFSET (buf));
2762 /* Make sure the first buffer is always DISCONT. If we split
2763 * GOPs inside the parser this is otherwise not guaranteed */
2765 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2768 /* likewise, subsequent buffers should never have DISCONT
2769 * according to the "reverse fragment protocol", or such would
2770 * confuse a downstream decoder
2771 * (could be DISCONT due to aggregating upstream fragments by parsing) */
2772 GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DISCONT);
2775 /* iterate output queue an push downstream */
2776 ret = gst_pad_push (parse->srcpad, buf);
2777 send = g_slist_delete_link (send, send);
2779 /* clear any leftover if error */
2780 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2782 buf = GST_BUFFER_CAST (send->data);
2783 gst_buffer_unref (buf);
2784 send = g_slist_delete_link (send, send);
2789 parse->priv->buffers_send = send;
2794 /* gst_base_parse_start_fragment:
2796 * Prepares for processing a reverse playback (forward) fragment
2797 * by (re)setting proper state variables.
2799 static GstFlowReturn
2800 gst_base_parse_start_fragment (GstBaseParse * parse)
2802 GST_LOG_OBJECT (parse, "starting fragment");
2804 /* invalidate so no fall-back timestamping is performed;
2805 * ok if taken from subclass or upstream */
2806 parse->priv->next_pts = GST_CLOCK_TIME_NONE;
2807 parse->priv->prev_pts = GST_CLOCK_TIME_NONE;
2808 parse->priv->next_dts = GST_CLOCK_TIME_NONE;
2809 parse->priv->prev_dts = GST_CLOCK_TIME_NONE;
2810 parse->priv->prev_dts_from_pts = FALSE;
2811 /* prevent it hanging around stop all the time */
2812 parse->segment.position = GST_CLOCK_TIME_NONE;
2814 parse->priv->discont = TRUE;
2816 /* head of previous fragment is now pending tail of current fragment */
2817 parse->priv->buffers_pending = parse->priv->buffers_head;
2818 parse->priv->buffers_head = NULL;
2824 /* gst_base_parse_finish_fragment:
2826 * Processes a reverse playback (forward) fragment:
2827 * - append head of last fragment that was skipped to current fragment data
2828 * - drain the resulting current fragment data (i.e. repeated chain)
2829 * - add time/duration (if needed) to frames queued by chain
2830 * - push queued data
2832 static GstFlowReturn
2833 gst_base_parse_finish_fragment (GstBaseParse * parse, gboolean prev_head)
2836 GstFlowReturn ret = GST_FLOW_OK;
2837 gboolean seen_key = FALSE, seen_delta = FALSE;
2839 GST_LOG_OBJECT (parse, "finishing fragment");
2842 parse->priv->buffers_pending = g_slist_reverse (parse->priv->buffers_pending);
2843 while (parse->priv->buffers_pending) {
2844 buf = GST_BUFFER_CAST (parse->priv->buffers_pending->data);
2846 GST_LOG_OBJECT (parse, "adding pending buffer (size %" G_GSIZE_FORMAT ")",
2847 gst_buffer_get_size (buf));
2848 gst_adapter_push (parse->priv->adapter, buf);
2850 GST_LOG_OBJECT (parse, "discarding head buffer");
2851 gst_buffer_unref (buf);
2853 parse->priv->buffers_pending =
2854 g_slist_delete_link (parse->priv->buffers_pending,
2855 parse->priv->buffers_pending);
2858 /* chain looks for frames and queues resulting ones (in stead of pushing) */
2859 /* initial skipped data is added to buffers_pending */
2860 gst_base_parse_drain (parse);
2862 if (parse->priv->buffers_send) {
2863 buf = GST_BUFFER_CAST (parse->priv->buffers_send->data);
2864 seen_key |= !GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
2867 /* add metadata (if needed to queued buffers */
2868 GST_LOG_OBJECT (parse, "last timestamp: %" GST_TIME_FORMAT,
2869 GST_TIME_ARGS (parse->priv->last_pts));
2870 while (parse->priv->buffers_queued) {
2871 buf = GST_BUFFER_CAST (parse->priv->buffers_queued->data);
2873 /* no touching if upstream or parsing provided time */
2874 if (GST_BUFFER_PTS_IS_VALID (buf)) {
2875 GST_LOG_OBJECT (parse, "buffer has time %" GST_TIME_FORMAT,
2876 GST_TIME_ARGS (GST_BUFFER_PTS (buf)));
2877 } else if (GST_BUFFER_DURATION_IS_VALID (buf)) {
2878 if (GST_CLOCK_TIME_IS_VALID (parse->priv->last_pts)) {
2879 if (G_LIKELY (GST_BUFFER_DURATION (buf) <= parse->priv->last_pts))
2880 parse->priv->last_pts -= GST_BUFFER_DURATION (buf);
2882 parse->priv->last_pts = 0;
2883 GST_BUFFER_PTS (buf) = parse->priv->last_pts;
2884 GST_LOG_OBJECT (parse, "applied time %" GST_TIME_FORMAT,
2885 GST_TIME_ARGS (GST_BUFFER_PTS (buf)));
2887 if (GST_CLOCK_TIME_IS_VALID (parse->priv->last_dts)) {
2888 if (G_LIKELY (GST_BUFFER_DURATION (buf) <= parse->priv->last_dts))
2889 parse->priv->last_dts -= GST_BUFFER_DURATION (buf);
2891 parse->priv->last_dts = 0;
2892 GST_BUFFER_DTS (buf) = parse->priv->last_dts;
2893 GST_LOG_OBJECT (parse, "applied dts %" GST_TIME_FORMAT,
2894 GST_TIME_ARGS (GST_BUFFER_DTS (buf)));
2897 /* no idea, very bad */
2898 GST_WARNING_OBJECT (parse, "could not determine time for buffer");
2901 parse->priv->last_pts = GST_BUFFER_PTS (buf);
2902 parse->priv->last_dts = GST_BUFFER_DTS (buf);
2904 /* reverse order for ascending sending */
2905 /* send downstream at keyframe not preceded by a keyframe
2906 * (e.g. that should identify start of collection of IDR nals) */
2907 if (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT)) {
2909 ret = gst_base_parse_send_buffers (parse);
2910 /* if a problem, throw all to sending */
2911 if (ret != GST_FLOW_OK) {
2912 parse->priv->buffers_send =
2913 g_slist_reverse (parse->priv->buffers_queued);
2914 parse->priv->buffers_queued = NULL;
2924 parse->priv->buffers_send =
2925 g_slist_prepend (parse->priv->buffers_send, buf);
2926 parse->priv->buffers_queued =
2927 g_slist_delete_link (parse->priv->buffers_queued,
2928 parse->priv->buffers_queued);
2931 /* audio may have all marked as keyframe, so arrange to send here. Also
2932 * we might have ended the loop above on a keyframe, in which case we
2934 if (!seen_delta || seen_key)
2935 ret = gst_base_parse_send_buffers (parse);
2937 /* any trailing unused no longer usable (ideally none) */
2938 if (G_UNLIKELY (gst_adapter_available (parse->priv->adapter))) {
2939 GST_DEBUG_OBJECT (parse, "discarding %" G_GSIZE_FORMAT " trailing bytes",
2940 gst_adapter_available (parse->priv->adapter));
2941 gst_adapter_clear (parse->priv->adapter);
2947 /* small helper that checks whether we have been trying to resync too long */
2948 static inline GstFlowReturn
2949 gst_base_parse_check_sync (GstBaseParse * parse)
2951 if (G_UNLIKELY (parse->priv->discont &&
2952 parse->priv->offset - parse->priv->sync_offset > 2 * 1024 * 1024)) {
2953 GST_ELEMENT_ERROR (parse, STREAM, DECODE,
2954 ("Failed to parse stream"), (NULL));
2955 return GST_FLOW_ERROR;
2961 static GstFlowReturn
2962 gst_base_parse_process_streamheader (GstBaseParse * parse)
2966 const GValue *value;
2967 GstFlowReturn ret = GST_FLOW_OK;
2969 caps = gst_pad_get_current_caps (GST_BASE_PARSE_SINK_PAD (parse));
2973 str = gst_caps_get_structure (caps, 0);
2974 value = gst_structure_get_value (str, "streamheader");
2978 GST_DEBUG_OBJECT (parse, "Found streamheader field on input caps");
2980 if (GST_VALUE_HOLDS_ARRAY (value)) {
2982 gsize len = gst_value_array_get_size (value);
2984 for (i = 0; i < len; i++) {
2986 gst_value_get_buffer (gst_value_array_get_value (value, i));
2988 gst_base_parse_chain (GST_BASE_PARSE_SINK_PAD (parse),
2989 GST_OBJECT_CAST (parse), gst_buffer_ref (buffer));
2992 } else if (GST_VALUE_HOLDS_BUFFER (value)) {
2993 GstBuffer *buffer = gst_value_get_buffer (value);
2995 gst_base_parse_chain (GST_BASE_PARSE_SINK_PAD (parse),
2996 GST_OBJECT_CAST (parse), gst_buffer_ref (buffer));
2999 gst_caps_unref (caps);
3006 gst_caps_unref (caps);
3009 GST_DEBUG_OBJECT (parse, "No streamheader on caps");
3014 static GstFlowReturn
3015 gst_base_parse_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
3017 GstBaseParseClass *bclass;
3018 GstBaseParse *parse;
3019 GstFlowReturn ret = GST_FLOW_OK;
3020 GstFlowReturn old_ret = GST_FLOW_OK;
3021 GstBuffer *tmpbuf = NULL;
3025 GstClockTime pts, dts;
3027 parse = GST_BASE_PARSE (parent);
3028 bclass = GST_BASE_PARSE_GET_CLASS (parse);
3029 GST_DEBUG_OBJECT (parent, "chain");
3031 /* early out for speed, if we need to skip */
3032 if (buffer && GST_BUFFER_IS_DISCONT (buffer))
3033 parse->priv->skip = 0;
3034 if (parse->priv->skip > 0) {
3035 gsize bsize = gst_buffer_get_size (buffer);
3036 GST_DEBUG ("Got %" G_GSIZE_FORMAT " buffer, need to skip %u", bsize,
3038 if (parse->priv->skip >= bsize) {
3039 parse->priv->skip -= bsize;
3040 GST_DEBUG ("All the buffer is skipped");
3041 parse->priv->offset += bsize;
3042 parse->priv->sync_offset = parse->priv->offset;
3045 buffer = gst_buffer_make_writable (buffer);
3046 gst_buffer_resize (buffer, parse->priv->skip, bsize - parse->priv->skip);
3047 parse->priv->offset += parse->priv->skip;
3048 GST_DEBUG ("Done skipping, we have %u left on this buffer",
3049 (unsigned) (bsize - parse->priv->skip));
3050 parse->priv->skip = 0;
3051 parse->priv->discont = TRUE;
3054 if (G_UNLIKELY (parse->priv->first_buffer)) {
3055 parse->priv->first_buffer = FALSE;
3056 if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_HEADER)) {
3057 /* this stream has no header buffers, check if we just prepend the
3058 * streamheader from caps to the stream */
3059 GST_DEBUG_OBJECT (parse, "Looking for streamheader field on caps to "
3060 "prepend to the stream");
3061 gst_base_parse_process_streamheader (parse);
3063 GST_DEBUG_OBJECT (parse, "Stream has header buffers, not prepending "
3064 "streamheader from caps");
3068 if (parse->priv->detecting) {
3069 GstBuffer *detect_buf;
3071 if (parse->priv->detect_buffers_size == 0) {
3072 detect_buf = gst_buffer_ref (buffer);
3077 detect_buf = gst_buffer_new ();
3079 for (l = parse->priv->detect_buffers; l; l = l->next) {
3080 gsize tmpsize = gst_buffer_get_size (l->data);
3082 gst_buffer_copy_into (detect_buf, GST_BUFFER_CAST (l->data),
3083 GST_BUFFER_COPY_MEMORY, offset, tmpsize);
3087 gst_buffer_copy_into (detect_buf, buffer, GST_BUFFER_COPY_MEMORY,
3088 offset, gst_buffer_get_size (buffer));
3091 ret = bclass->detect (parse, detect_buf);
3092 gst_buffer_unref (detect_buf);
3094 if (ret == GST_FLOW_OK) {
3097 /* Detected something */
3098 parse->priv->detecting = FALSE;
3100 for (l = parse->priv->detect_buffers; l; l = l->next) {
3101 if (ret == GST_FLOW_OK && !parse->priv->flushing)
3103 gst_base_parse_chain (GST_BASE_PARSE_SINK_PAD (parse),
3104 parent, GST_BUFFER_CAST (l->data));
3106 gst_buffer_unref (GST_BUFFER_CAST (l->data));
3108 g_list_free (parse->priv->detect_buffers);
3109 parse->priv->detect_buffers = NULL;
3110 parse->priv->detect_buffers_size = 0;
3112 if (ret != GST_FLOW_OK) {
3116 /* Handle the current buffer */
3117 } else if (ret == GST_FLOW_NOT_NEGOTIATED) {
3118 /* Still detecting, append buffer or error out if draining */
3120 if (parse->priv->drain) {
3121 GST_DEBUG_OBJECT (parse, "Draining but did not detect format yet");
3122 return GST_FLOW_ERROR;
3123 } else if (parse->priv->flushing) {
3124 g_list_foreach (parse->priv->detect_buffers, (GFunc) gst_buffer_unref,
3126 g_list_free (parse->priv->detect_buffers);
3127 parse->priv->detect_buffers = NULL;
3128 parse->priv->detect_buffers_size = 0;
3130 parse->priv->detect_buffers =
3131 g_list_append (parse->priv->detect_buffers, buffer);
3132 parse->priv->detect_buffers_size += gst_buffer_get_size (buffer);
3136 /* Something went wrong, subclass responsible for error reporting */
3140 /* And now handle the current buffer if detection worked */
3143 if (G_LIKELY (buffer)) {
3144 GST_LOG_OBJECT (parse,
3145 "buffer size: %" G_GSIZE_FORMAT ", offset = %" G_GINT64_FORMAT
3146 ", dts %" GST_TIME_FORMAT ", pts %" GST_TIME_FORMAT,
3147 gst_buffer_get_size (buffer), GST_BUFFER_OFFSET (buffer),
3148 GST_TIME_ARGS (GST_BUFFER_DTS (buffer)),
3149 GST_TIME_ARGS (GST_BUFFER_PTS (buffer)));
3151 if (G_UNLIKELY (!parse->priv->disable_passthrough
3152 && parse->priv->passthrough)) {
3153 GstBaseParseFrame frame;
3155 gst_base_parse_frame_init (&frame);
3156 frame.buffer = gst_buffer_make_writable (buffer);
3157 ret = gst_base_parse_push_frame (parse, &frame);
3158 gst_base_parse_frame_free (&frame);
3161 if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT))) {
3162 /* upstream feeding us in reverse playback;
3163 * finish previous fragment and start new upon DISCONT */
3164 if (parse->segment.rate < 0.0) {
3165 GST_DEBUG_OBJECT (parse, "buffer starts new reverse playback fragment");
3166 ret = gst_base_parse_finish_fragment (parse, TRUE);
3167 gst_base_parse_start_fragment (parse);
3169 /* discont in the stream, drain and mark discont for next output */
3170 gst_base_parse_drain (parse);
3171 parse->priv->discont = TRUE;
3174 gst_adapter_push (parse->priv->adapter, buffer);
3177 /* Parse and push as many frames as possible */
3178 /* Stop either when adapter is empty or we are flushing */
3179 while (!parse->priv->flushing) {
3181 gboolean updated_prev_pts = FALSE;
3183 /* note: if subclass indicates MAX fsize,
3184 * this will not likely be available anyway ... */
3185 min_size = MAX (parse->priv->min_frame_size, fsize);
3186 av = gst_adapter_available (parse->priv->adapter);
3188 if (G_UNLIKELY (parse->priv->drain)) {
3190 GST_DEBUG_OBJECT (parse, "draining, data left: %d", min_size);
3191 if (G_UNLIKELY (!min_size)) {
3196 /* Collect at least min_frame_size bytes */
3197 if (av < min_size) {
3198 GST_DEBUG_OBJECT (parse, "not enough data available (only %d bytes)", av);
3202 /* move along with upstream timestamp (if any),
3203 * but interpolate in between */
3204 pts = gst_adapter_prev_pts (parse->priv->adapter, NULL);
3205 dts = gst_adapter_prev_dts (parse->priv->adapter, NULL);
3206 if (GST_CLOCK_TIME_IS_VALID (pts) && (parse->priv->prev_pts != pts)) {
3207 parse->priv->prev_pts = parse->priv->next_pts = pts;
3208 updated_prev_pts = TRUE;
3211 if (GST_CLOCK_TIME_IS_VALID (dts) && (parse->priv->prev_dts != dts)) {
3212 parse->priv->prev_dts = parse->priv->next_dts = dts;
3213 parse->priv->prev_dts_from_pts = FALSE;
3216 /* we can mess with, erm interpolate, timestamps,
3217 * and incoming stuff has PTS but no DTS seen so far,
3218 * then pick up DTS from PTS and hope for the best ... */
3219 if (parse->priv->infer_ts &&
3220 parse->priv->pts_interpolate &&
3221 !GST_CLOCK_TIME_IS_VALID (dts) &&
3222 (!GST_CLOCK_TIME_IS_VALID (parse->priv->prev_dts)
3223 || (parse->priv->prev_dts_from_pts && updated_prev_pts))
3224 && GST_CLOCK_TIME_IS_VALID (pts)) {
3225 parse->priv->prev_dts = parse->priv->next_dts = pts;
3226 parse->priv->prev_dts_from_pts = TRUE;
3229 /* always pass all available data */
3230 tmpbuf = gst_adapter_get_buffer (parse->priv->adapter, av);
3232 /* already inform subclass what timestamps we have planned,
3233 * at least if provided by time-based upstream */
3234 if (parse->priv->upstream_format == GST_FORMAT_TIME) {
3235 tmpbuf = gst_buffer_make_writable (tmpbuf);
3236 GST_BUFFER_PTS (tmpbuf) = parse->priv->next_pts;
3237 GST_BUFFER_DTS (tmpbuf) = parse->priv->next_dts;
3238 GST_BUFFER_DURATION (tmpbuf) = GST_CLOCK_TIME_NONE;
3241 /* keep the adapter mapped, so keep track of what has to be flushed */
3242 ret = gst_base_parse_handle_buffer (parse, tmpbuf, &skip, &flush);
3245 if (ret != GST_FLOW_OK && ret != GST_FLOW_NOT_LINKED) {
3248 if (skip == 0 && flush == 0) {
3249 GST_LOG_OBJECT (parse, "nothing skipped and no frames finished, "
3250 "breaking to get more data");
3251 /* ignore this return as it produced no data */
3255 if (old_ret == GST_FLOW_OK)
3260 GST_LOG_OBJECT (parse, "chain leaving");
3264 /* pull @size bytes at current offset,
3265 * i.e. at least try to and possibly return a shorter buffer if near the end */
3266 static GstFlowReturn
3267 gst_base_parse_pull_range (GstBaseParse * parse, guint size,
3268 GstBuffer ** buffer)
3270 GstFlowReturn ret = GST_FLOW_OK;
3272 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
3274 /* Caching here actually makes much less difference than one would expect.
3275 * We do it mainly to avoid pulling buffers of 1 byte all the time */
3276 if (parse->priv->cache) {
3277 gint64 cache_offset = GST_BUFFER_OFFSET (parse->priv->cache);
3278 gint cache_size = gst_buffer_get_size (parse->priv->cache);
3280 if (cache_offset <= parse->priv->offset &&
3281 (parse->priv->offset + size) <= (cache_offset + cache_size)) {
3282 *buffer = gst_buffer_copy_region (parse->priv->cache, GST_BUFFER_COPY_ALL,
3283 parse->priv->offset - cache_offset, size);
3284 GST_BUFFER_OFFSET (*buffer) = parse->priv->offset;
3287 /* not enough data in the cache, free cache and get a new one */
3288 gst_buffer_unref (parse->priv->cache);
3289 parse->priv->cache = NULL;
3292 /* refill the cache */
3294 gst_pad_pull_range (parse->sinkpad, parse->priv->offset, MAX (size,
3295 64 * 1024), &parse->priv->cache);
3296 if (ret != GST_FLOW_OK) {
3297 parse->priv->cache = NULL;
3301 if (gst_buffer_get_size (parse->priv->cache) >= size) {
3303 gst_buffer_copy_region (parse->priv->cache, GST_BUFFER_COPY_ALL, 0,
3305 GST_BUFFER_OFFSET (*buffer) = parse->priv->offset;
3309 /* Not possible to get enough data, try a last time with
3310 * requesting exactly the size we need */
3311 gst_buffer_unref (parse->priv->cache);
3312 parse->priv->cache = NULL;
3314 ret = gst_pad_pull_range (parse->sinkpad, parse->priv->offset, size,
3315 &parse->priv->cache);
3317 if (ret != GST_FLOW_OK) {
3318 GST_DEBUG_OBJECT (parse, "pull_range returned %d", ret);
3323 if (gst_buffer_get_size (parse->priv->cache) < size) {
3324 GST_DEBUG_OBJECT (parse, "Returning short buffer at offset %"
3325 G_GUINT64_FORMAT ": wanted %u bytes, got %" G_GSIZE_FORMAT " bytes",
3326 parse->priv->offset, size, gst_buffer_get_size (parse->priv->cache));
3328 *buffer = parse->priv->cache;
3329 parse->priv->cache = NULL;
3335 gst_buffer_copy_region (parse->priv->cache, GST_BUFFER_COPY_ALL, 0, size);
3336 GST_BUFFER_OFFSET (*buffer) = parse->priv->offset;
3341 static GstFlowReturn
3342 gst_base_parse_handle_previous_fragment (GstBaseParse * parse)
3345 GstClockTime ts = 0;
3349 GST_DEBUG_OBJECT (parse, "fragment ended; last_ts = %" GST_TIME_FORMAT
3350 ", last_offset = %" G_GINT64_FORMAT,
3351 GST_TIME_ARGS (parse->priv->last_pts), parse->priv->last_offset);
3353 if (!parse->priv->last_offset
3354 || parse->priv->last_pts <= parse->segment.start) {
3355 GST_DEBUG_OBJECT (parse, "past start of segment %" GST_TIME_FORMAT,
3356 GST_TIME_ARGS (parse->segment.start));
3361 /* last fragment started at last_offset / last_ts;
3362 * seek back 10s capped at 1MB */
3363 if (parse->priv->last_pts >= 10 * GST_SECOND)
3364 ts = parse->priv->last_pts - 10 * GST_SECOND;
3365 /* if we are exact now, we will be more so going backwards */
3366 if (parse->priv->exact_position) {
3367 offset = gst_base_parse_find_offset (parse, ts, TRUE, NULL);
3369 if (!gst_base_parse_convert (parse, GST_FORMAT_TIME, ts,
3370 GST_FORMAT_BYTES, &offset)) {
3371 GST_DEBUG_OBJECT (parse, "conversion failed, only BYTE based");
3374 offset = CLAMP (offset, parse->priv->last_offset - 1024 * 1024,
3375 parse->priv->last_offset - 1024);
3376 offset = MAX (0, offset);
3378 GST_DEBUG_OBJECT (parse, "next fragment from offset %" G_GINT64_FORMAT,
3380 parse->priv->offset = offset;
3382 ret = gst_base_parse_pull_range (parse, parse->priv->last_offset - offset,
3384 if (ret != GST_FLOW_OK)
3387 /* offset will increase again as fragment is processed/parsed */
3388 parse->priv->last_offset = offset;
3390 gst_base_parse_start_fragment (parse);
3391 gst_adapter_push (parse->priv->adapter, buffer);
3392 ret = gst_base_parse_finish_fragment (parse, TRUE);
3393 if (ret != GST_FLOW_OK)
3396 /* force previous fragment */
3397 parse->priv->offset = -1;
3404 * pull and scan for next frame starting from current offset
3405 * ajusts sync, drain and offset going along */
3406 static GstFlowReturn
3407 gst_base_parse_scan_frame (GstBaseParse * parse, GstBaseParseClass * klass)
3410 GstFlowReturn ret = GST_FLOW_OK;
3411 guint fsize, min_size;
3415 GST_LOG_OBJECT (parse, "scanning for frame at offset %" G_GUINT64_FORMAT
3416 " (%#" G_GINT64_MODIFIER "x)", parse->priv->offset, parse->priv->offset);
3418 /* let's make this efficient for all subclass once and for all;
3419 * maybe it does not need this much, but in the latter case, we know we are
3420 * in pull mode here and might as well try to read and supply more anyway
3421 * (so does the buffer caching mechanism) */
3425 min_size = MAX (parse->priv->min_frame_size, fsize);
3427 GST_LOG_OBJECT (parse, "reading buffer size %u", min_size);
3429 ret = gst_base_parse_pull_range (parse, min_size, &buffer);
3430 if (ret != GST_FLOW_OK)
3433 /* if we got a short read, inform subclass we are draining leftover
3434 * and no more is to be expected */
3435 if (gst_buffer_get_size (buffer) < min_size) {
3436 GST_LOG_OBJECT (parse, "... but did not get that; marked draining");
3437 parse->priv->drain = TRUE;
3440 if (parse->priv->detecting) {
3441 ret = klass->detect (parse, buffer);
3442 if (ret == GST_FLOW_NOT_NEGOTIATED) {
3443 /* If draining we error out, otherwise request a buffer
3445 if (parse->priv->drain) {
3446 gst_buffer_unref (buffer);
3447 GST_ERROR_OBJECT (parse, "Failed to detect format but draining");
3448 return GST_FLOW_ERROR;
3451 gst_buffer_unref (buffer);
3454 } else if (ret != GST_FLOW_OK) {
3455 gst_buffer_unref (buffer);
3456 GST_ERROR_OBJECT (parse, "detect() returned %s",
3457 gst_flow_get_name (ret));
3461 /* Else handle this buffer normally */
3464 ret = gst_base_parse_handle_buffer (parse, buffer, &skip, &flushed);
3465 if (ret != GST_FLOW_OK)
3468 /* If a large amount of data was requested to be skipped, _handle_buffer
3469 might have set the priv->skip flag to an extra amount on top of skip.
3470 In pull mode, we can just pull from the new offset directly. */
3471 parse->priv->offset += parse->priv->skip;
3472 parse->priv->skip = 0;
3474 /* something flushed means something happened,
3475 * and we should bail out of this loop so as not to occupy
3476 * the task thread indefinitely */
3478 GST_LOG_OBJECT (parse, "frame finished, breaking loop");
3481 /* nothing flushed, no skip and draining, so nothing left to do */
3482 if (!skip && parse->priv->drain) {
3483 GST_LOG_OBJECT (parse, "no activity or result when draining; "
3484 "breaking loop and marking EOS");
3488 /* otherwise, get some more data
3489 * note that is checked this does not happen indefinitely */
3491 GST_LOG_OBJECT (parse, "getting some more data");
3494 parse->priv->drain = FALSE;
3501 /* Loop that is used in pull mode to retrieve data from upstream */
3503 gst_base_parse_loop (GstPad * pad)
3505 GstBaseParse *parse;
3506 GstBaseParseClass *klass;
3507 GstFlowReturn ret = GST_FLOW_OK;
3509 parse = GST_BASE_PARSE (gst_pad_get_parent (pad));
3510 klass = GST_BASE_PARSE_GET_CLASS (parse);
3512 GST_LOG_OBJECT (parse, "Entering parse loop");
3514 if (G_UNLIKELY (parse->priv->push_stream_start)) {
3519 gst_pad_create_stream_id (parse->srcpad, GST_ELEMENT_CAST (parse),
3522 event = gst_event_new_stream_start (stream_id);
3523 gst_event_set_group_id (event, gst_util_group_id_next ());
3525 GST_DEBUG_OBJECT (parse, "Pushing STREAM_START");
3526 gst_pad_push_event (parse->srcpad, event);
3527 parse->priv->push_stream_start = FALSE;
3531 /* reverse playback:
3532 * first fragment (closest to stop time) is handled normally below,
3533 * then we pull in fragments going backwards */
3534 if (parse->segment.rate < 0.0) {
3535 /* check if we jumped back to a previous fragment,
3536 * which is a post-first fragment */
3537 if (parse->priv->offset < 0) {
3538 ret = gst_base_parse_handle_previous_fragment (parse);
3543 ret = gst_base_parse_scan_frame (parse, klass);
3545 /* eat expected eos signalling past segment in reverse playback */
3546 if (parse->segment.rate < 0.0 && ret == GST_FLOW_EOS &&
3547 parse->segment.position >= parse->segment.stop) {
3548 GST_DEBUG_OBJECT (parse, "downstream has reached end of segment");
3549 /* push what was accumulated during loop run */
3550 gst_base_parse_finish_fragment (parse, FALSE);
3551 /* force previous fragment */
3552 parse->priv->offset = -1;
3556 if (ret != GST_FLOW_OK)
3560 if (ret == GST_FLOW_EOS)
3562 else if (ret != GST_FLOW_OK)
3565 gst_object_unref (parse);
3572 GST_DEBUG_OBJECT (parse, "eos");
3577 gboolean push_eos = FALSE;
3579 GST_DEBUG_OBJECT (parse, "pausing task, reason %s",
3580 gst_flow_get_name (ret));
3581 gst_pad_pause_task (parse->sinkpad);
3583 if (ret == GST_FLOW_EOS) {
3584 /* handle end-of-stream/segment */
3585 if (parse->segment.flags & GST_SEGMENT_FLAG_SEGMENT) {
3588 if ((stop = parse->segment.stop) == -1)
3589 stop = parse->segment.duration;
3591 GST_DEBUG_OBJECT (parse, "sending segment_done");
3593 gst_element_post_message
3594 (GST_ELEMENT_CAST (parse),
3595 gst_message_new_segment_done (GST_OBJECT_CAST (parse),
3596 GST_FORMAT_TIME, stop));
3597 gst_pad_push_event (parse->srcpad,
3598 gst_event_new_segment_done (GST_FORMAT_TIME, stop));
3600 /* If we STILL have zero frames processed, fire an error */
3601 if (parse->priv->framecount == 0) {
3602 GST_ELEMENT_ERROR (parse, STREAM, WRONG_TYPE,
3603 ("No valid frames found before end of stream"), (NULL));
3607 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
3608 /* for fatal errors we post an error message, wrong-state is
3609 * not fatal because it happens due to flushes and only means
3610 * that we should stop now. */
3611 GST_ELEMENT_FLOW_ERROR (parse, ret);
3615 if (parse->priv->estimated_duration <= 0) {
3616 gst_base_parse_update_duration (parse);
3618 /* Push pending events, including SEGMENT events */
3619 gst_base_parse_push_pending_events (parse);
3621 gst_pad_push_event (parse->srcpad, gst_event_new_eos ());
3623 gst_object_unref (parse);
3628 gst_base_parse_sink_activate (GstPad * sinkpad, GstObject * parent)
3630 GstSchedulingFlags sched_flags;
3631 GstBaseParse *parse;
3635 parse = GST_BASE_PARSE (parent);
3637 GST_DEBUG_OBJECT (parse, "sink activate");
3639 query = gst_query_new_scheduling ();
3640 if (!gst_pad_peer_query (sinkpad, query)) {
3641 gst_query_unref (query);
3642 goto baseparse_push;
3645 gst_query_parse_scheduling (query, &sched_flags, NULL, NULL, NULL);
3647 pull_mode = gst_query_has_scheduling_mode (query, GST_PAD_MODE_PULL)
3648 && ((sched_flags & GST_SCHEDULING_FLAG_SEEKABLE) != 0);
3650 gst_query_unref (query);
3653 goto baseparse_push;
3655 GST_DEBUG_OBJECT (parse, "trying to activate in pull mode");
3656 if (!gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE))
3657 goto baseparse_push;
3659 parse->priv->push_stream_start = TRUE;
3660 /* In pull mode, upstream is BYTES */
3661 parse->priv->upstream_format = GST_FORMAT_BYTES;
3663 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_base_parse_loop,
3668 GST_DEBUG_OBJECT (parse, "trying to activate in push mode");
3669 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
3674 gst_base_parse_activate (GstBaseParse * parse, gboolean active)
3676 GstBaseParseClass *klass;
3677 gboolean result = TRUE;
3679 GST_DEBUG_OBJECT (parse, "activate %d", active);
3681 klass = GST_BASE_PARSE_GET_CLASS (parse);
3684 if (parse->priv->pad_mode == GST_PAD_MODE_NONE && klass->start)
3685 result = klass->start (parse);
3687 /* If the subclass implements ::detect we want to
3688 * call it for the first buffers now */
3689 parse->priv->detecting = (klass->detect != NULL);
3691 /* We must make sure streaming has finished before resetting things
3692 * and calling the ::stop vfunc */
3693 GST_PAD_STREAM_LOCK (parse->sinkpad);
3694 GST_PAD_STREAM_UNLOCK (parse->sinkpad);
3696 if (parse->priv->pad_mode != GST_PAD_MODE_NONE && klass->stop)
3697 result = klass->stop (parse);
3699 parse->priv->pad_mode = GST_PAD_MODE_NONE;
3700 parse->priv->upstream_format = GST_FORMAT_UNDEFINED;
3702 GST_DEBUG_OBJECT (parse, "activate return: %d", result);
3707 gst_base_parse_sink_activate_mode (GstPad * pad, GstObject * parent,
3708 GstPadMode mode, gboolean active)
3711 GstBaseParse *parse;
3713 parse = GST_BASE_PARSE (parent);
3715 GST_DEBUG_OBJECT (parse, "sink %sactivate in %s mode",
3716 (active) ? "" : "de", gst_pad_mode_get_name (mode));
3718 if (!gst_base_parse_activate (parse, active))
3719 goto activate_failed;
3722 case GST_PAD_MODE_PULL:
3724 parse->priv->pending_events =
3725 g_list_prepend (parse->priv->pending_events,
3726 gst_event_new_segment (&parse->segment));
3729 result = gst_pad_stop_task (pad);
3737 parse->priv->pad_mode = active ? mode : GST_PAD_MODE_NONE;
3739 GST_DEBUG_OBJECT (parse, "sink activate return: %d", result);
3746 GST_DEBUG_OBJECT (parse, "activate failed");
3752 * gst_base_parse_set_duration:
3753 * @parse: #GstBaseParse.
3755 * @duration: duration value.
3756 * @interval: how often to update the duration estimate based on bitrate, or 0.
3758 * Sets the duration of the currently playing media. Subclass can use this
3759 * when it is able to determine duration and/or notices a change in the media
3760 * duration. Alternatively, if @interval is non-zero (default), then stream
3761 * duration is determined based on estimated bitrate, and updated every @interval
3765 gst_base_parse_set_duration (GstBaseParse * parse,
3766 GstFormat fmt, gint64 duration, gint interval)
3768 g_return_if_fail (parse != NULL);
3770 if (parse->priv->upstream_has_duration) {
3771 GST_DEBUG_OBJECT (parse, "using upstream duration; discarding update");
3775 if (duration != parse->priv->duration) {
3778 m = gst_message_new_duration_changed (GST_OBJECT (parse));
3779 gst_element_post_message (GST_ELEMENT (parse), m);
3781 /* TODO: what about duration tag? */
3783 parse->priv->duration = duration;
3784 parse->priv->duration_fmt = fmt;
3785 GST_DEBUG_OBJECT (parse, "set duration: %" G_GINT64_FORMAT, duration);
3786 if (fmt == GST_FORMAT_TIME && GST_CLOCK_TIME_IS_VALID (duration)) {
3787 if (interval != 0) {
3788 GST_DEBUG_OBJECT (parse, "valid duration provided, disabling estimate");
3792 GST_DEBUG_OBJECT (parse, "set update interval: %d", interval);
3793 parse->priv->update_interval = interval;
3799 * gst_base_parse_set_average_bitrate:
3800 * @parse: #GstBaseParse.
3801 * @bitrate: average bitrate in bits/second
3803 * Optionally sets the average bitrate detected in media (if non-zero),
3804 * e.g. based on metadata, as it will be posted to the application.
3806 * By default, announced average bitrate is estimated. The average bitrate
3807 * is used to estimate the total duration of the stream and to estimate
3808 * a seek position, if there's no index and the format is syncable
3809 * (see gst_base_parse_set_syncable()).
3812 gst_base_parse_set_average_bitrate (GstBaseParse * parse, guint bitrate)
3814 parse->priv->bitrate = bitrate;
3815 GST_DEBUG_OBJECT (parse, "bitrate %u", bitrate);
3819 * gst_base_parse_set_min_frame_size:
3820 * @parse: #GstBaseParse.
3821 * @min_size: Minimum size of the data that this base class should give to
3824 * Subclass can use this function to tell the base class that it needs to
3825 * give at least #min_size buffers.
3828 gst_base_parse_set_min_frame_size (GstBaseParse * parse, guint min_size)
3830 g_return_if_fail (parse != NULL);
3832 parse->priv->min_frame_size = min_size;
3833 GST_LOG_OBJECT (parse, "set frame_min_size: %d", min_size);
3837 * gst_base_parse_set_frame_rate:
3838 * @parse: the #GstBaseParse to set
3839 * @fps_num: frames per second (numerator).
3840 * @fps_den: frames per second (denominator).
3841 * @lead_in: frames needed before a segment for subsequent decode
3842 * @lead_out: frames needed after a segment
3844 * If frames per second is configured, parser can take care of buffer duration
3845 * and timestamping. When performing segment clipping, or seeking to a specific
3846 * location, a corresponding decoder might need an initial @lead_in and a
3847 * following @lead_out number of frames to ensure the desired segment is
3848 * entirely filled upon decoding.
3851 gst_base_parse_set_frame_rate (GstBaseParse * parse, guint fps_num,
3852 guint fps_den, guint lead_in, guint lead_out)
3854 g_return_if_fail (parse != NULL);
3856 parse->priv->fps_num = fps_num;
3857 parse->priv->fps_den = fps_den;
3858 if (!fps_num || !fps_den) {
3859 GST_DEBUG_OBJECT (parse, "invalid fps (%d/%d), ignoring parameters",
3861 fps_num = fps_den = 0;
3862 parse->priv->frame_duration = GST_CLOCK_TIME_NONE;
3863 parse->priv->lead_in = parse->priv->lead_out = 0;
3864 parse->priv->lead_in_ts = parse->priv->lead_out_ts = 0;
3866 parse->priv->frame_duration =
3867 gst_util_uint64_scale (GST_SECOND, fps_den, fps_num);
3868 parse->priv->lead_in = lead_in;
3869 parse->priv->lead_out = lead_out;
3870 parse->priv->lead_in_ts =
3871 gst_util_uint64_scale (GST_SECOND, fps_den * lead_in, fps_num);
3872 parse->priv->lead_out_ts =
3873 gst_util_uint64_scale (GST_SECOND, fps_den * lead_out, fps_num);
3874 /* aim for about 1.5s to estimate duration */
3875 if (parse->priv->update_interval < 0) {
3876 guint64 interval = gst_util_uint64_scale (fps_num, 3,
3877 G_GUINT64_CONSTANT (2) * fps_den);
3879 parse->priv->update_interval = MIN (interval, G_MAXINT);
3881 GST_LOG_OBJECT (parse, "estimated update interval to %d frames",
3882 parse->priv->update_interval);
3885 GST_LOG_OBJECT (parse, "set fps: %d/%d => duration: %" G_GINT64_FORMAT " ms",
3886 fps_num, fps_den, parse->priv->frame_duration / GST_MSECOND);
3887 GST_LOG_OBJECT (parse, "set lead in: %d frames = %" G_GUINT64_FORMAT " ms, "
3888 "lead out: %d frames = %" G_GUINT64_FORMAT " ms",
3889 lead_in, parse->priv->lead_in_ts / GST_MSECOND,
3890 lead_out, parse->priv->lead_out_ts / GST_MSECOND);
3894 * gst_base_parse_set_has_timing_info:
3895 * @parse: a #GstBaseParse
3896 * @has_timing: whether frames carry timing information
3898 * Set if frames carry timing information which the subclass can (generally)
3899 * parse and provide. In particular, intrinsic (rather than estimated) time
3900 * can be obtained following a seek.
3903 gst_base_parse_set_has_timing_info (GstBaseParse * parse, gboolean has_timing)
3905 parse->priv->has_timing_info = has_timing;
3906 GST_INFO_OBJECT (parse, "has_timing: %s", (has_timing) ? "yes" : "no");
3910 * gst_base_parse_set_syncable:
3911 * @parse: a #GstBaseParse
3912 * @syncable: set if frame starts can be identified
3914 * Set if frame starts can be identified. This is set by default and
3915 * determines whether seeking based on bitrate averages
3916 * is possible for a format/stream.
3919 gst_base_parse_set_syncable (GstBaseParse * parse, gboolean syncable)
3921 parse->priv->syncable = syncable;
3922 GST_INFO_OBJECT (parse, "syncable: %s", (syncable) ? "yes" : "no");
3926 * gst_base_parse_set_passthrough:
3927 * @parse: a #GstBaseParse
3928 * @passthrough: %TRUE if parser should run in passthrough mode
3930 * Set if the nature of the format or configuration does not allow (much)
3931 * parsing, and the parser should operate in passthrough mode (which only
3932 * applies when operating in push mode). That is, incoming buffers are
3933 * pushed through unmodified, i.e. no @check_valid_frame or @parse_frame
3934 * callbacks will be invoked, but @pre_push_frame will still be invoked,
3935 * so subclass can perform as much or as little is appropriate for
3936 * passthrough semantics in @pre_push_frame.
3939 gst_base_parse_set_passthrough (GstBaseParse * parse, gboolean passthrough)
3941 parse->priv->passthrough = passthrough;
3942 GST_INFO_OBJECT (parse, "passthrough: %s", (passthrough) ? "yes" : "no");
3946 * gst_base_parse_set_pts_interpolation:
3947 * @parse: a #GstBaseParse
3948 * @pts_interpolate: %TRUE if parser should interpolate PTS timestamps
3950 * By default, the base class will guess PTS timestamps using a simple
3951 * interpolation (previous timestamp + duration), which is incorrect for
3952 * data streams with reordering, where PTS can go backward. Sub-classes
3953 * implementing such formats should disable PTS interpolation.
3956 gst_base_parse_set_pts_interpolation (GstBaseParse * parse,
3957 gboolean pts_interpolate)
3959 parse->priv->pts_interpolate = pts_interpolate;
3960 GST_INFO_OBJECT (parse, "PTS interpolation: %s",
3961 (pts_interpolate) ? "yes" : "no");
3965 * gst_base_parse_set_infer_ts:
3966 * @parse: a #GstBaseParse
3967 * @infer_ts: %TRUE if parser should infer DTS/PTS from each other
3969 * By default, the base class might try to infer PTS from DTS and vice
3970 * versa. While this is generally correct for audio data, it may not
3971 * be otherwise. Sub-classes implementing such formats should disable
3972 * timestamp inferring.
3975 gst_base_parse_set_infer_ts (GstBaseParse * parse, gboolean infer_ts)
3977 parse->priv->infer_ts = infer_ts;
3978 GST_INFO_OBJECT (parse, "TS inferring: %s", (infer_ts) ? "yes" : "no");
3982 * gst_base_parse_set_latency:
3983 * @parse: a #GstBaseParse
3984 * @min_latency: minimum parse latency
3985 * @max_latency: maximum parse latency
3987 * Sets the minimum and maximum (which may likely be equal) latency introduced
3988 * by the parsing process. If there is such a latency, which depends on the
3989 * particular parsing of the format, it typically corresponds to 1 frame duration.
3992 gst_base_parse_set_latency (GstBaseParse * parse, GstClockTime min_latency,
3993 GstClockTime max_latency)
3995 g_return_if_fail (min_latency != GST_CLOCK_TIME_NONE);
3996 g_return_if_fail (min_latency <= max_latency);
3998 GST_OBJECT_LOCK (parse);
3999 parse->priv->min_latency = min_latency;
4000 parse->priv->max_latency = max_latency;
4001 GST_OBJECT_UNLOCK (parse);
4002 GST_INFO_OBJECT (parse, "min/max latency %" GST_TIME_FORMAT ", %"
4003 GST_TIME_FORMAT, GST_TIME_ARGS (min_latency),
4004 GST_TIME_ARGS (max_latency));
4008 gst_base_parse_get_duration (GstBaseParse * parse, GstFormat format,
4009 GstClockTime * duration)
4011 gboolean res = FALSE;
4013 g_return_val_if_fail (duration != NULL, FALSE);
4015 *duration = GST_CLOCK_TIME_NONE;
4016 if (parse->priv->duration != -1 && format == parse->priv->duration_fmt) {
4017 GST_LOG_OBJECT (parse, "using provided duration");
4018 *duration = parse->priv->duration;
4020 } else if (parse->priv->duration != -1) {
4021 GST_LOG_OBJECT (parse, "converting provided duration");
4022 res = gst_base_parse_convert (parse, parse->priv->duration_fmt,
4023 parse->priv->duration, format, (gint64 *) duration);
4024 } else if (format == GST_FORMAT_TIME && parse->priv->estimated_duration != -1) {
4025 GST_LOG_OBJECT (parse, "using estimated duration");
4026 *duration = parse->priv->estimated_duration;
4029 GST_LOG_OBJECT (parse, "cannot estimate duration");
4032 GST_LOG_OBJECT (parse, "res: %d, duration %" GST_TIME_FORMAT, res,
4033 GST_TIME_ARGS (*duration));
4038 gst_base_parse_src_query_default (GstBaseParse * parse, GstQuery * query)
4040 gboolean res = FALSE;
4043 pad = GST_BASE_PARSE_SRC_PAD (parse);
4045 switch (GST_QUERY_TYPE (query)) {
4046 case GST_QUERY_POSITION:
4051 GST_DEBUG_OBJECT (parse, "position query");
4052 gst_query_parse_position (query, &format, NULL);
4054 /* try upstream first */
4055 res = gst_pad_query_default (pad, GST_OBJECT_CAST (parse), query);
4057 /* Fall back on interpreting segment */
4058 GST_OBJECT_LOCK (parse);
4059 /* Only reply BYTES if upstream is in BYTES already, otherwise
4060 * we're not in charge */
4061 if (format == GST_FORMAT_BYTES
4062 && parse->priv->upstream_format == GST_FORMAT_BYTES) {
4063 dest_value = parse->priv->offset;
4065 } else if (format == parse->segment.format &&
4066 GST_CLOCK_TIME_IS_VALID (parse->segment.position)) {
4067 dest_value = gst_segment_to_stream_time (&parse->segment,
4068 parse->segment.format, parse->segment.position);
4071 GST_OBJECT_UNLOCK (parse);
4072 if (!res && parse->priv->upstream_format == GST_FORMAT_BYTES) {
4073 /* no precise result, upstream no idea either, then best estimate */
4074 /* priv->offset is updated in both PUSH/PULL modes, *iff* we're
4075 * in charge of things */
4076 res = gst_base_parse_convert (parse,
4077 GST_FORMAT_BYTES, parse->priv->offset, format, &dest_value);
4080 gst_query_set_position (query, format, dest_value);
4084 case GST_QUERY_DURATION:
4087 GstClockTime duration;
4089 GST_DEBUG_OBJECT (parse, "duration query");
4090 gst_query_parse_duration (query, &format, NULL);
4092 /* consult upstream */
4093 res = gst_pad_query_default (pad, GST_OBJECT_CAST (parse), query);
4095 /* otherwise best estimate from us */
4097 res = gst_base_parse_get_duration (parse, format, &duration);
4099 gst_query_set_duration (query, format, duration);
4103 case GST_QUERY_SEEKING:
4106 GstClockTime duration = GST_CLOCK_TIME_NONE;
4107 gboolean seekable = FALSE;
4109 GST_DEBUG_OBJECT (parse, "seeking query");
4110 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
4112 /* consult upstream */
4113 res = gst_pad_query_default (pad, GST_OBJECT_CAST (parse), query);
4115 /* we may be able to help if in TIME */
4116 if (fmt == GST_FORMAT_TIME && gst_base_parse_is_seekable (parse)) {
4117 gst_query_parse_seeking (query, &fmt, &seekable, NULL, NULL);
4118 /* already OK if upstream takes care */
4119 GST_LOG_OBJECT (parse, "upstream handled %d, seekable %d",
4121 if (!(res && seekable)) {
4122 if (!gst_base_parse_get_duration (parse, GST_FORMAT_TIME, &duration)
4123 || duration == -1) {
4124 /* seekable if we still have a chance to get duration later on */
4125 seekable = parse->priv->upstream_seekable &&
4126 (parse->priv->update_interval > 0);
4128 seekable = parse->priv->upstream_seekable;
4129 GST_LOG_OBJECT (parse, "already determine upstream seekabled: %d",
4132 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable, 0, duration);
4138 case GST_QUERY_FORMATS:
4139 gst_query_set_formatsv (query, 3, fmtlist);
4142 case GST_QUERY_CONVERT:
4144 GstFormat src_format, dest_format;
4145 gint64 src_value, dest_value;
4147 gst_query_parse_convert (query, &src_format, &src_value,
4148 &dest_format, &dest_value);
4150 res = gst_base_parse_convert (parse, src_format, src_value,
4151 dest_format, &dest_value);
4153 gst_query_set_convert (query, src_format, src_value,
4154 dest_format, dest_value);
4158 case GST_QUERY_LATENCY:
4160 if ((res = gst_pad_peer_query (parse->sinkpad, query))) {
4162 GstClockTime min_latency, max_latency;
4164 gst_query_parse_latency (query, &live, &min_latency, &max_latency);
4165 GST_DEBUG_OBJECT (parse, "Peer latency: live %d, min %"
4166 GST_TIME_FORMAT " max %" GST_TIME_FORMAT, live,
4167 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
4169 GST_OBJECT_LOCK (parse);
4170 /* add our latency */
4171 min_latency += parse->priv->min_latency;
4172 if (max_latency == -1 || parse->priv->max_latency == -1)
4175 max_latency += parse->priv->max_latency;
4176 GST_OBJECT_UNLOCK (parse);
4178 gst_query_set_latency (query, live, min_latency, max_latency);
4182 case GST_QUERY_SEGMENT:
4187 format = parse->segment.format;
4190 gst_segment_to_stream_time (&parse->segment, format,
4191 parse->segment.start);
4192 if ((stop = parse->segment.stop) == -1)
4193 stop = parse->segment.duration;
4195 stop = gst_segment_to_stream_time (&parse->segment, format, stop);
4197 gst_query_set_segment (query, parse->segment.rate, format, start, stop);
4202 res = gst_pad_query_default (pad, GST_OBJECT_CAST (parse), query);
4208 /* scans for a cluster start from @pos,
4209 * return GST_FLOW_OK and frame position/time in @pos/@time if found */
4210 static GstFlowReturn
4211 gst_base_parse_find_frame (GstBaseParse * parse, gint64 * pos,
4212 GstClockTime * time, GstClockTime * duration)
4214 GstBaseParseClass *klass;
4216 gboolean orig_drain, orig_discont;
4217 GstFlowReturn ret = GST_FLOW_OK;
4218 GstBuffer *buf = NULL;
4219 GstBaseParseFrame *sframe = NULL;
4221 g_return_val_if_fail (pos != NULL, GST_FLOW_ERROR);
4222 g_return_val_if_fail (time != NULL, GST_FLOW_ERROR);
4223 g_return_val_if_fail (duration != NULL, GST_FLOW_ERROR);
4225 klass = GST_BASE_PARSE_GET_CLASS (parse);
4227 *time = GST_CLOCK_TIME_NONE;
4228 *duration = GST_CLOCK_TIME_NONE;
4231 orig_offset = parse->priv->offset;
4232 orig_discont = parse->priv->discont;
4233 orig_drain = parse->priv->drain;
4235 GST_DEBUG_OBJECT (parse, "scanning for frame starting at %" G_GINT64_FORMAT
4236 " (%#" G_GINT64_MODIFIER "x)", *pos, *pos);
4238 /* jump elsewhere and locate next frame */
4239 parse->priv->offset = *pos;
4240 /* mark as scanning so frames don't get processed all the way */
4241 parse->priv->scanning = TRUE;
4242 ret = gst_base_parse_scan_frame (parse, klass);
4243 parse->priv->scanning = FALSE;
4244 /* retrieve frame found during scan */
4245 sframe = parse->priv->scanned_frame;
4246 parse->priv->scanned_frame = NULL;
4248 if (ret != GST_FLOW_OK || !sframe)
4251 /* get offset first, subclass parsing might dump other stuff in there */
4252 *pos = sframe->offset;
4253 buf = sframe->buffer;
4256 /* but it should provide proper time */
4257 *time = GST_BUFFER_TIMESTAMP (buf);
4258 *duration = GST_BUFFER_DURATION (buf);
4260 GST_LOG_OBJECT (parse,
4261 "frame with time %" GST_TIME_FORMAT " at offset %" G_GINT64_FORMAT,
4262 GST_TIME_ARGS (*time), *pos);
4266 gst_base_parse_frame_free (sframe);
4269 parse->priv->offset = orig_offset;
4270 parse->priv->discont = orig_discont;
4271 parse->priv->drain = orig_drain;
4276 /* bisect and scan through file for frame starting before @time,
4277 * returns OK and @time/@offset if found, NONE and/or error otherwise
4278 * If @time == G_MAXINT64, scan for duration ( == last frame) */
4279 static GstFlowReturn
4280 gst_base_parse_locate_time (GstBaseParse * parse, GstClockTime * _time,
4283 GstFlowReturn ret = GST_FLOW_OK;
4284 gint64 lpos, hpos, newpos;
4285 GstClockTime time, ltime, htime, newtime, dur;
4286 gboolean cont = TRUE;
4287 const GstClockTime tolerance = TARGET_DIFFERENCE;
4288 const guint chunk = 4 * 1024;
4290 g_return_val_if_fail (_time != NULL, GST_FLOW_ERROR);
4291 g_return_val_if_fail (_offset != NULL, GST_FLOW_ERROR);
4293 GST_DEBUG_OBJECT (parse, "Bisecting for time %" GST_TIME_FORMAT,
4294 GST_TIME_ARGS (*_time));
4296 /* TODO also make keyframe aware if useful some day */
4311 /* do not know at first */
4313 *_time = GST_CLOCK_TIME_NONE;
4315 /* need initial positions; start and end */
4316 lpos = parse->priv->first_frame_offset;
4317 ltime = parse->priv->first_frame_pts;
4318 /* try other one if no luck */
4319 if (!GST_CLOCK_TIME_IS_VALID (ltime))
4320 ltime = parse->priv->first_frame_dts;
4321 if (!gst_base_parse_get_duration (parse, GST_FORMAT_TIME, &htime)) {
4322 GST_DEBUG_OBJECT (parse, "Unknown time duration, cannot bisect");
4323 return GST_FLOW_ERROR;
4325 hpos = parse->priv->upstream_size;
4327 GST_DEBUG_OBJECT (parse,
4328 "Bisection initial bounds: bytes %" G_GINT64_FORMAT " %" G_GINT64_FORMAT
4329 ", times %" GST_TIME_FORMAT " %" GST_TIME_FORMAT, lpos, hpos,
4330 GST_TIME_ARGS (ltime), GST_TIME_ARGS (htime));
4332 /* check preconditions are satisfied;
4333 * start and end are needed, except for special case where we scan for
4334 * last frame to determine duration */
4335 if (parse->priv->pad_mode != GST_PAD_MODE_PULL || !hpos ||
4336 !GST_CLOCK_TIME_IS_VALID (ltime) ||
4337 (!GST_CLOCK_TIME_IS_VALID (htime) && time != G_MAXINT64)) {
4341 /* shortcut cases */
4344 } else if (time < ltime + tolerance) {
4348 } else if (time >= htime) {
4354 while (htime > ltime && cont) {
4355 GST_LOG_OBJECT (parse,
4356 "lpos: %" G_GUINT64_FORMAT ", ltime: %" GST_TIME_FORMAT, lpos,
4357 GST_TIME_ARGS (ltime));
4358 GST_LOG_OBJECT (parse,
4359 "hpos: %" G_GUINT64_FORMAT ", htime: %" GST_TIME_FORMAT, hpos,
4360 GST_TIME_ARGS (htime));
4361 if (G_UNLIKELY (time == G_MAXINT64)) {
4363 } else if (G_LIKELY (hpos > lpos)) {
4365 gst_util_uint64_scale (hpos - lpos, time - ltime, htime - ltime) +
4368 /* should mean lpos == hpos, since lpos <= hpos is invariant */
4370 /* we check this case once, but not forever, so break loop */
4375 newpos = CLAMP (newpos, lpos, hpos);
4376 GST_LOG_OBJECT (parse,
4377 "estimated _offset for %" GST_TIME_FORMAT ": %" G_GINT64_FORMAT,
4378 GST_TIME_ARGS (time), newpos);
4380 ret = gst_base_parse_find_frame (parse, &newpos, &newtime, &dur);
4381 if (ret == GST_FLOW_EOS) {
4382 /* heuristic HACK */
4383 hpos = MAX (lpos, hpos - chunk);
4385 } else if (ret != GST_FLOW_OK) {
4389 if (newtime == -1 || newpos == -1) {
4390 GST_DEBUG_OBJECT (parse, "subclass did not provide metadata; aborting");
4394 if (G_UNLIKELY (time == G_MAXINT64)) {
4397 if (GST_CLOCK_TIME_IS_VALID (dur))
4400 } else if (newtime > time) {
4402 hpos = (newpos >= hpos) ? MAX (lpos, hpos - chunk) : MAX (lpos, newpos);
4404 } else if (newtime + tolerance > time) {
4405 /* close enough undershoot */
4409 } else if (newtime < ltime) {
4410 /* so a position beyond lpos resulted in earlier time than ltime ... */
4411 GST_DEBUG_OBJECT (parse, "non-ascending time; aborting");
4414 /* undershoot too far */
4415 newpos += newpos == lpos ? chunk : 0;
4416 lpos = CLAMP (newpos, lpos, hpos);
4422 GST_LOG_OBJECT (parse, "return offset %" G_GINT64_FORMAT ", time %"
4423 GST_TIME_FORMAT, *_offset, GST_TIME_ARGS (*_time));
4428 gst_base_parse_find_offset (GstBaseParse * parse, GstClockTime time,
4429 gboolean before, GstClockTime * _ts)
4431 gint64 bytes = 0, ts = 0;
4432 GstIndexEntry *entry = NULL;
4434 if (time == GST_CLOCK_TIME_NONE) {
4440 GST_BASE_PARSE_INDEX_LOCK (parse);
4441 if (parse->priv->index) {
4442 /* Let's check if we have an index entry for that time */
4443 entry = gst_index_get_assoc_entry (parse->priv->index,
4444 parse->priv->index_id,
4445 before ? GST_INDEX_LOOKUP_BEFORE : GST_INDEX_LOOKUP_AFTER,
4446 GST_INDEX_ASSOCIATION_FLAG_KEY_UNIT, GST_FORMAT_TIME, time);
4450 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &bytes);
4451 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &ts);
4453 GST_DEBUG_OBJECT (parse, "found index entry for %" GST_TIME_FORMAT
4454 " at %" GST_TIME_FORMAT ", offset %" G_GINT64_FORMAT,
4455 GST_TIME_ARGS (time), GST_TIME_ARGS (ts), bytes);
4457 GST_DEBUG_OBJECT (parse, "no index entry found for %" GST_TIME_FORMAT,
4458 GST_TIME_ARGS (time));
4461 ts = GST_CLOCK_TIME_NONE;
4464 GST_BASE_PARSE_INDEX_UNLOCK (parse);
4473 /* returns TRUE if seek succeeded */
4475 gst_base_parse_handle_seek (GstBaseParse * parse, GstEvent * event)
4480 GstSeekType start_type = GST_SEEK_TYPE_NONE, stop_type;
4481 gboolean flush, update, res = TRUE, accurate;
4482 gint64 start, stop, seekpos, seekstop;
4483 GstSegment seeksegment = { 0, };
4484 GstClockTime start_ts;
4486 GstEvent *segment_event;
4488 /* try upstream first, unless we're driving the streaming thread ourselves */
4489 if (parse->priv->pad_mode != GST_PAD_MODE_PULL) {
4490 res = gst_pad_push_event (parse->sinkpad, gst_event_ref (event));
4495 gst_event_parse_seek (event, &rate, &format, &flags,
4496 &start_type, &start, &stop_type, &stop);
4497 seqnum = gst_event_get_seqnum (event);
4499 GST_DEBUG_OBJECT (parse, "seek to format %s, rate %f, "
4500 "start type %d at %" GST_TIME_FORMAT ", end type %d at %"
4501 GST_TIME_FORMAT, gst_format_get_name (format), rate,
4502 start_type, GST_TIME_ARGS (start), stop_type, GST_TIME_ARGS (stop));
4504 /* we can only handle TIME, so check if subclass can convert
4505 * to TIME format if it's some other format (such as DEFAULT) */
4506 if (format != GST_FORMAT_TIME) {
4507 if (!gst_base_parse_convert (parse, format, start, GST_FORMAT_TIME, &start)
4508 || !gst_base_parse_convert (parse, format, stop, GST_FORMAT_TIME,
4510 goto no_convert_to_time;
4512 GST_INFO_OBJECT (parse, "converted %s format to start time "
4513 "%" GST_TIME_FORMAT " and stop time %" GST_TIME_FORMAT,
4514 gst_format_get_name (format), GST_TIME_ARGS (start),
4515 GST_TIME_ARGS (stop));
4517 format = GST_FORMAT_TIME;
4520 /* no negative rates in push mode (unless upstream takes care of that, but
4521 * we've already tried upstream and it didn't handle the seek request) */
4522 if (rate < 0.0 && parse->priv->pad_mode == GST_PAD_MODE_PUSH)
4525 if (start_type != GST_SEEK_TYPE_SET ||
4526 (stop_type != GST_SEEK_TYPE_SET && stop_type != GST_SEEK_TYPE_NONE))
4529 /* get flush flag */
4530 flush = flags & GST_SEEK_FLAG_FLUSH;
4532 /* copy segment, we need this because we still need the old
4533 * segment when we close the current segment. */
4534 gst_segment_copy_into (&parse->segment, &seeksegment);
4536 GST_DEBUG_OBJECT (parse, "configuring seek");
4537 gst_segment_do_seek (&seeksegment, rate, format, flags,
4538 start_type, start, stop_type, stop, &update);
4540 /* accurate seeking implies seek tables are used to obtain position,
4541 * and the requested segment is maintained exactly, not adjusted any way */
4542 accurate = flags & GST_SEEK_FLAG_ACCURATE;
4544 /* maybe we can be accurate for (almost) free */
4545 gst_base_parse_find_offset (parse, seeksegment.position, TRUE, &start_ts);
4546 if (seeksegment.position <= start_ts + TARGET_DIFFERENCE) {
4547 GST_DEBUG_OBJECT (parse, "accurate seek possible");
4552 GstClockTime startpos;
4554 startpos = seeksegment.position;
4558 /* accurate requested, so ... seek a bit before target */
4559 if (startpos < parse->priv->lead_in_ts)
4562 startpos -= parse->priv->lead_in_ts;
4564 if (seeksegment.stop == -1 && seeksegment.duration != -1)
4565 seeksegment.stop = seeksegment.start + seeksegment.duration;
4567 seekpos = gst_base_parse_find_offset (parse, startpos, TRUE, &start_ts);
4568 seekstop = gst_base_parse_find_offset (parse, seeksegment.stop, FALSE,
4572 start_ts = seeksegment.position;
4576 if (seeksegment.stop == -1 && seeksegment.duration != -1)
4577 seeksegment.stop = seeksegment.start + seeksegment.duration;
4579 if (!gst_base_parse_convert (parse, format, start_ts,
4580 GST_FORMAT_BYTES, &seekpos))
4581 goto convert_failed;
4582 if (!gst_base_parse_convert (parse, format, seeksegment.stop,
4583 GST_FORMAT_BYTES, &seekstop))
4584 goto convert_failed;
4587 GST_DEBUG_OBJECT (parse,
4588 "seek position %" G_GINT64_FORMAT " in bytes: %" G_GINT64_FORMAT,
4590 GST_DEBUG_OBJECT (parse,
4591 "seek stop %" G_GINT64_FORMAT " in bytes: %" G_GINT64_FORMAT,
4592 seeksegment.stop, seekstop);
4594 if (parse->priv->pad_mode == GST_PAD_MODE_PULL) {
4597 GST_DEBUG_OBJECT (parse, "seek in PULL mode");
4600 if (parse->srcpad) {
4601 GstEvent *fevent = gst_event_new_flush_start ();
4602 GST_DEBUG_OBJECT (parse, "sending flush start");
4604 gst_event_set_seqnum (fevent, seqnum);
4606 gst_pad_push_event (parse->srcpad, gst_event_ref (fevent));
4607 /* unlock upstream pull_range */
4608 gst_pad_push_event (parse->sinkpad, fevent);
4611 gst_pad_pause_task (parse->sinkpad);
4614 /* we should now be able to grab the streaming thread because we stopped it
4615 * with the above flush/pause code */
4616 GST_PAD_STREAM_LOCK (parse->sinkpad);
4618 /* save current position */
4619 last_stop = parse->segment.position;
4620 GST_DEBUG_OBJECT (parse, "stopped streaming at %" G_GINT64_FORMAT,
4623 /* now commit to new position */
4625 /* prepare for streaming again */
4627 GstEvent *fevent = gst_event_new_flush_stop (TRUE);
4628 GST_DEBUG_OBJECT (parse, "sending flush stop");
4629 gst_event_set_seqnum (fevent, seqnum);
4630 gst_pad_push_event (parse->srcpad, gst_event_ref (fevent));
4631 gst_pad_push_event (parse->sinkpad, fevent);
4632 gst_base_parse_clear_queues (parse);
4635 memcpy (&parse->segment, &seeksegment, sizeof (GstSegment));
4637 /* store the newsegment event so it can be sent from the streaming thread. */
4638 /* This will be sent later in _loop() */
4639 segment_event = gst_event_new_segment (&parse->segment);
4640 gst_event_set_seqnum (segment_event, seqnum);
4641 parse->priv->pending_events =
4642 g_list_prepend (parse->priv->pending_events, segment_event);
4644 GST_DEBUG_OBJECT (parse, "Created newseg format %d, "
4645 "start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT
4646 ", time = %" GST_TIME_FORMAT, format,
4647 GST_TIME_ARGS (parse->segment.start),
4648 GST_TIME_ARGS (parse->segment.stop),
4649 GST_TIME_ARGS (parse->segment.time));
4651 /* one last chance in pull mode to stay accurate;
4652 * maybe scan and subclass can find where to go */
4655 GstClockTime ts = seeksegment.position;
4657 gst_base_parse_locate_time (parse, &ts, &scanpos);
4661 /* running collected index now consists of several intervals,
4662 * so optimized check no longer possible */
4663 parse->priv->index_last_valid = FALSE;
4664 parse->priv->index_last_offset = 0;
4665 parse->priv->index_last_ts = 0;
4669 /* mark discont if we are going to stream from another position. */
4670 if (seekpos != parse->priv->offset) {
4671 GST_DEBUG_OBJECT (parse,
4672 "mark DISCONT, we did a seek to another position");
4673 parse->priv->offset = seekpos;
4674 parse->priv->last_offset = seekpos;
4675 parse->priv->seen_keyframe = FALSE;
4676 parse->priv->discont = TRUE;
4677 parse->priv->next_dts = start_ts;
4678 parse->priv->next_pts = GST_CLOCK_TIME_NONE;
4679 parse->priv->last_dts = GST_CLOCK_TIME_NONE;
4680 parse->priv->last_pts = GST_CLOCK_TIME_NONE;
4681 parse->priv->sync_offset = seekpos;
4682 parse->priv->exact_position = accurate;
4685 /* Start streaming thread if paused */
4686 gst_pad_start_task (parse->sinkpad,
4687 (GstTaskFunction) gst_base_parse_loop, parse->sinkpad, NULL);
4689 GST_PAD_STREAM_UNLOCK (parse->sinkpad);
4694 GstEvent *new_event;
4695 GstBaseParseSeek *seek;
4696 GstSeekFlags flags = (flush ? GST_SEEK_FLAG_FLUSH : GST_SEEK_FLAG_NONE);
4698 /* The only thing we need to do in PUSH-mode is to send the
4699 seek event (in bytes) to upstream. Segment / flush handling happens
4700 in corresponding src event handlers */
4701 GST_DEBUG_OBJECT (parse, "seek in PUSH mode");
4702 if (seekstop >= 0 && seekstop <= seekpos)
4704 new_event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags,
4705 GST_SEEK_TYPE_SET, seekpos, stop_type, seekstop);
4706 gst_event_set_seqnum (new_event, seqnum);
4708 /* store segment info so its precise details can be reconstructed when
4709 * receiving newsegment;
4710 * this matters for all details when accurate seeking,
4711 * is most useful to preserve NONE stop time otherwise */
4712 seek = g_new0 (GstBaseParseSeek, 1);
4713 seek->segment = seeksegment;
4714 seek->accurate = accurate;
4715 seek->offset = seekpos;
4716 seek->start_ts = start_ts;
4717 GST_OBJECT_LOCK (parse);
4718 /* less optimal, but preserves order */
4719 parse->priv->pending_seeks =
4720 g_slist_append (parse->priv->pending_seeks, seek);
4721 GST_OBJECT_UNLOCK (parse);
4723 res = gst_pad_push_event (parse->sinkpad, new_event);
4726 GST_OBJECT_LOCK (parse);
4727 parse->priv->pending_seeks =
4728 g_slist_remove (parse->priv->pending_seeks, seek);
4729 GST_OBJECT_UNLOCK (parse);
4735 gst_event_unref (event);
4741 GST_DEBUG_OBJECT (parse, "negative playback rates delegated upstream.");
4747 GST_DEBUG_OBJECT (parse, "unsupported seek type.");
4753 GST_DEBUG_OBJECT (parse, "seek in %s format was requested, but subclass "
4754 "couldn't convert that into TIME format", gst_format_get_name (format));
4760 GST_DEBUG_OBJECT (parse, "conversion TIME to BYTES failed.");
4767 gst_base_parse_set_upstream_tags (GstBaseParse * parse, GstTagList * taglist)
4769 if (taglist == parse->priv->upstream_tags)
4772 if (parse->priv->upstream_tags) {
4773 gst_tag_list_unref (parse->priv->upstream_tags);
4774 parse->priv->upstream_tags = NULL;
4777 GST_INFO_OBJECT (parse, "upstream tags: %" GST_PTR_FORMAT, taglist);
4779 if (taglist != NULL)
4780 parse->priv->upstream_tags = gst_tag_list_ref (taglist);
4782 gst_base_parse_check_bitrate_tags (parse);
4787 gst_base_parse_set_index (GstElement * element, GstIndex * index)
4789 GstBaseParse *parse = GST_BASE_PARSE (element);
4791 GST_BASE_PARSE_INDEX_LOCK (parse);
4792 if (parse->priv->index)
4793 gst_object_unref (parse->priv->index);
4795 parse->priv->index = gst_object_ref (index);
4796 gst_index_get_writer_id (index, GST_OBJECT_CAST (element),
4797 &parse->priv->index_id);
4798 parse->priv->own_index = FALSE;
4800 parse->priv->index = NULL;
4802 GST_BASE_PARSE_INDEX_UNLOCK (parse);
4806 gst_base_parse_get_index (GstElement * element)
4808 GstBaseParse *parse = GST_BASE_PARSE (element);
4809 GstIndex *result = NULL;
4811 GST_BASE_PARSE_INDEX_LOCK (parse);
4812 if (parse->priv->index)
4813 result = gst_object_ref (parse->priv->index);
4814 GST_BASE_PARSE_INDEX_UNLOCK (parse);
4820 static GstStateChangeReturn
4821 gst_base_parse_change_state (GstElement * element, GstStateChange transition)
4823 GstBaseParse *parse;
4824 GstStateChangeReturn result;
4826 parse = GST_BASE_PARSE (element);
4828 switch (transition) {
4829 case GST_STATE_CHANGE_READY_TO_PAUSED:
4830 /* If this is our own index destroy it as the
4831 * old entries might be wrong for the new stream */
4832 GST_BASE_PARSE_INDEX_LOCK (parse);
4833 if (parse->priv->own_index) {
4834 gst_object_unref (parse->priv->index);
4835 parse->priv->index = NULL;
4836 parse->priv->own_index = FALSE;
4839 /* If no index was created, generate one */
4840 if (G_UNLIKELY (!parse->priv->index)) {
4841 GST_DEBUG_OBJECT (parse, "no index provided creating our own");
4843 parse->priv->index = g_object_new (gst_mem_index_get_type (), NULL);
4844 gst_index_get_writer_id (parse->priv->index, GST_OBJECT (parse),
4845 &parse->priv->index_id);
4846 parse->priv->own_index = TRUE;
4848 GST_BASE_PARSE_INDEX_UNLOCK (parse);
4854 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4856 switch (transition) {
4857 case GST_STATE_CHANGE_PAUSED_TO_READY:
4858 gst_base_parse_reset (parse);
4868 * gst_base_parse_set_ts_at_offset:
4869 * @parse: a #GstBaseParse
4870 * @offset: offset into current buffer
4872 * This function should only be called from a @handle_frame implementation.
4874 * #GstBaseParse creates initial timestamps for frames by using the last
4875 * timestamp seen in the stream before the frame starts. In certain
4876 * cases, the correct timestamps will occur in the stream after the
4877 * start of the frame, but before the start of the actual picture data.
4878 * This function can be used to set the timestamps based on the offset
4879 * into the frame data that the picture starts.
4884 gst_base_parse_set_ts_at_offset (GstBaseParse * parse, gsize offset)
4886 GstClockTime pts, dts;
4888 g_return_if_fail (GST_IS_BASE_PARSE (parse));
4890 pts = gst_adapter_prev_pts_at_offset (parse->priv->adapter, offset, NULL);
4891 dts = gst_adapter_prev_dts_at_offset (parse->priv->adapter, offset, NULL);
4893 if (!GST_CLOCK_TIME_IS_VALID (pts) || !GST_CLOCK_TIME_IS_VALID (dts)) {
4894 GST_DEBUG_OBJECT (parse,
4895 "offset adapter timestamps dts=%" GST_TIME_FORMAT " pts=%"
4896 GST_TIME_FORMAT, GST_TIME_ARGS (dts), GST_TIME_ARGS (pts));
4898 if (GST_CLOCK_TIME_IS_VALID (pts) && (parse->priv->prev_pts != pts))
4899 parse->priv->prev_pts = parse->priv->next_pts = pts;
4901 if (GST_CLOCK_TIME_IS_VALID (dts) && (parse->priv->prev_dts != dts)) {
4902 parse->priv->prev_dts = parse->priv->next_dts = dts;
4903 parse->priv->prev_dts_from_pts = FALSE;
4908 * gst_base_parse_merge_tags:
4909 * @parse: a #GstBaseParse
4910 * @tags: (allow-none): a #GstTagList to merge, or NULL to unset
4911 * previously-set tags
4912 * @mode: the #GstTagMergeMode to use, usually #GST_TAG_MERGE_REPLACE
4914 * Sets the parser subclass's tags and how they should be merged with any
4915 * upstream stream tags. This will override any tags previously-set
4916 * with gst_base_parse_merge_tags().
4918 * Note that this is provided for convenience, and the subclass is
4919 * not required to use this and can still do tag handling on its own.
4924 gst_base_parse_merge_tags (GstBaseParse * parse, GstTagList * tags,
4925 GstTagMergeMode mode)
4927 g_return_if_fail (GST_IS_BASE_PARSE (parse));
4928 g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
4929 g_return_if_fail (tags == NULL || mode != GST_TAG_MERGE_UNDEFINED);
4931 GST_OBJECT_LOCK (parse);
4933 if (tags != parse->priv->parser_tags) {
4934 if (parse->priv->parser_tags) {
4935 gst_tag_list_unref (parse->priv->parser_tags);
4936 parse->priv->parser_tags = NULL;
4937 parse->priv->parser_tags_merge_mode = GST_TAG_MERGE_APPEND;
4940 parse->priv->parser_tags = gst_tag_list_ref (tags);
4941 parse->priv->parser_tags_merge_mode = mode;
4944 GST_DEBUG_OBJECT (parse, "setting parser tags to %" GST_PTR_FORMAT
4945 " (mode %d)", tags, parse->priv->parser_tags_merge_mode);
4947 gst_base_parse_check_bitrate_tags (parse);
4948 parse->priv->tags_changed = TRUE;
4951 GST_OBJECT_UNLOCK (parse);