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 #GstBaseParseClass.start() to inform subclass
51 * that data processing is about to start now.
53 * * #GstBaseParse class calls #GstBaseParseClass.set_sink_caps() to
54 * inform the subclass about incoming sinkpad caps. Subclass could
55 * already set the srcpad caps accordingly, but this might be delayed
56 * until calling 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 (minimum frame size ). It can
60 * do 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
72 * with #GstBaseParseClass.handle_frame(). Subclass checks the contents
73 * and can optionally return #GST_FLOW_OK along with an amount of data
74 * to be skipped to find a valid frame (which will result in a
75 * subsequent DISCONT). If, otherwise, the buffer does not hold a
76 * complete frame, #GstBaseParseClass.handle_frame() can merely return
77 * and will be called again when additional data is available. In push
78 * mode this amounts to an additional input buffer (thus minimal
79 * additional latency), in pull mode this amounts to some arbitrary
80 * reasonable buffer size increase.
82 * Of course, gst_base_parse_set_min_frame_size() could also be used if
83 * a very specific known amount of additional data is required. If,
84 * however, the buffer holds a complete valid frame, it can pass the
85 * size of this frame to gst_base_parse_finish_frame().
87 * If acting as a converter, it can also merely indicate consumed input
88 * data while simultaneously providing custom output data. Note that
89 * baseclass performs some processing (such as tracking overall consumed
90 * data rate versus duration) for each finished frame, but other state
91 * is only updated upon each call to #GstBaseParseClass.handle_frame()
92 * (such as tracking upstream input timestamp).
94 * Subclass is also responsible for setting the buffer metadata
95 * (e.g. buffer timestamp and duration, or keyframe if applicable).
96 * (although the latter can also be done by #GstBaseParse if it is
97 * appropriately configured, see below). Frame is provided with
98 * timestamp derived from upstream (as much as generally possible),
99 * duration obtained from configuration (see below), and offset
100 * if meaningful (in pull mode).
102 * Note that #GstBaseParseClass.handle_frame() might receive any small
103 * amount of input data when leftover data is being drained (e.g. at
106 * * As part of finish frame processing, just prior to actually pushing
107 * the buffer in question, it is passed to
108 * #GstBaseParseClass.pre_push_frame() which gives subclass yet one last
109 * chance to examine buffer metadata, or to send some custom (tag)
110 * events, or to perform custom (segment) filtering.
112 * * During the parsing process #GstBaseParseClass will handle both srcpad
113 * and sinkpad events. They will be passed to subclass if
114 * #GstBaseParseClass.event() or #GstBaseParseClass.src_event()
115 * implementations have been provided.
119 * * #GstBaseParse class calls #GstBaseParseClass.stop() to inform the
120 * subclass that data parsing will be stopped.
122 * Subclass is responsible for providing pad template caps for source and
123 * sink pads. The pads need to be named "sink" and "src". It also needs to
124 * set the fixed caps on srcpad, when the format is ensured (e.g. when
125 * base class calls subclass' #GstBaseParseClass.set_sink_caps() function).
127 * This base class uses %GST_FORMAT_DEFAULT as a meaning of frames. So,
128 * subclass conversion routine needs to know that conversion from
129 * %GST_FORMAT_TIME to %GST_FORMAT_DEFAULT must return the
130 * frame number that can be found from the given byte position.
132 * #GstBaseParse uses subclasses conversion methods also for seeking (or
133 * otherwise uses its own default one, see also below).
135 * Subclass @start and @stop functions will be called to inform the beginning
136 * and end of data processing.
138 * Things that subclass need to take care of:
140 * * Provide pad templates
141 * * Fixate the source pad caps when appropriate
142 * * Inform base class how big data chunks should be retrieved. This is
143 * done with gst_base_parse_set_min_frame_size() function.
144 * * Examine data chunks passed to subclass with
145 * #GstBaseParseClass.handle_frame() and pass proper frame(s) to
146 * gst_base_parse_finish_frame(), and setting src pad caps and timestamps
148 * * Provide conversion functions
149 * * Update the duration information with gst_base_parse_set_duration()
150 * * Optionally passthrough using gst_base_parse_set_passthrough()
151 * * Configure various baseparse parameters using
152 * gst_base_parse_set_average_bitrate(), gst_base_parse_set_syncable()
153 * and gst_base_parse_set_frame_rate().
155 * * In particular, if subclass is unable to determine a duration, but
156 * parsing (or specs) yields a frames per seconds rate, then this can be
157 * provided to #GstBaseParse to enable it to cater for buffer time
158 * metadata (which will be taken from upstream as much as
159 * possible). Internally keeping track of frame durations and respective
160 * sizes that have been pushed provides #GstBaseParse with an estimated
161 * bitrate. A default #GstBaseParseClass.convert() (used if not
162 * overridden) will then use these rates to perform obvious conversions.
163 * These rates are also used to update (estimated) duration at regular
169 * - In push mode provide a queue of adapter-"queued" buffers for upstream
171 * - Queue buffers/events until caps are set
181 #include <gst/base/gstadapter.h>
183 #include "gstbaseparse.h"
185 /* FIXME: get rid of old GstIndex code */
186 #include "gstindex.h"
187 #include "gstindex.c"
188 #include "gstmemindex.c"
190 #define GST_BASE_PARSE_FRAME_PRIVATE_FLAG_NOALLOC (1 << 0)
192 #define MIN_FRAMES_TO_POST_BITRATE 10
193 #define TARGET_DIFFERENCE (20 * GST_SECOND)
194 #define MAX_INDEX_ENTRIES 4096
195 #define UPDATE_THRESHOLD 2
197 #define ABSDIFF(a,b) (((a) > (b)) ? ((a) - (b)) : ((b) - (a)))
199 GST_DEBUG_CATEGORY_STATIC (gst_base_parse_debug);
200 #define GST_CAT_DEFAULT gst_base_parse_debug
202 /* Supported formats */
203 static const GstFormat fmtlist[] = {
210 struct _GstBaseParsePrivate
217 GstFormat duration_fmt;
218 gint64 estimated_duration;
219 gint64 estimated_drift;
221 guint min_frame_size;
222 gboolean disable_passthrough;
223 gboolean passthrough;
224 gboolean pts_interpolate;
227 gboolean has_timing_info;
228 guint fps_num, fps_den;
229 gint update_interval;
231 guint lead_in, lead_out;
232 GstClockTime lead_in_ts, lead_out_ts;
233 GstClockTime min_latency, max_latency;
242 GstClockTime next_pts;
243 GstClockTime next_dts;
244 GstClockTime prev_pts;
245 GstClockTime prev_dts;
246 gboolean prev_dts_from_pts;
247 GstClockTime frame_duration;
248 gboolean seen_keyframe;
254 guint64 data_bytecount;
255 guint64 acc_duration;
256 GstClockTime first_frame_pts;
257 GstClockTime first_frame_dts;
258 gint64 first_frame_offset;
260 gboolean post_min_bitrate;
261 gboolean post_avg_bitrate;
262 gboolean post_max_bitrate;
267 guint posted_avg_bitrate;
269 /* frames/buffers that are queued and ready to go on OK */
270 GQueue queued_frames;
274 /* index entry storage, either ours or provided */
280 /* seek table entries only maintained if upstream is BYTE seekable */
281 gboolean upstream_seekable;
282 gboolean upstream_has_duration;
283 gint64 upstream_size;
284 GstFormat upstream_format;
285 /* minimum distance between two index entries */
286 GstClockTimeDiff idx_interval;
287 guint64 idx_byte_interval;
288 /* ts and offset of last entry added */
289 GstClockTime index_last_ts;
290 gint64 index_last_offset;
291 gboolean index_last_valid;
293 /* timestamps currently produced are accurate, e.g. started from 0 onwards */
294 gboolean exact_position;
295 /* seek events are temporarily kept to match them with newsegments */
296 GSList *pending_seeks;
298 /* reverse playback */
299 GSList *buffers_pending;
300 GSList *buffers_head;
301 GSList *buffers_queued;
302 GSList *buffers_send;
303 GstClockTime last_pts;
304 GstClockTime last_dts;
307 /* Pending serialized events */
308 GList *pending_events;
310 /* If baseparse has checked the caps to identify if it is
311 * handling video or audio */
312 gboolean checked_media;
314 /* offset of last parsed frame/data */
316 /* force a new frame, regardless of offset */
318 /* whether we are merely scanning for a frame */
320 /* ... and resulting frame, if any */
321 GstBaseParseFrame *scanned_frame;
323 /* TRUE if we're still detecting the format, i.e.
324 * if ::detect() is still called for future buffers */
326 GList *detect_buffers;
327 guint detect_buffers_size;
329 /* True when no buffers have been received yet */
330 gboolean first_buffer;
332 /* if TRUE, a STREAM_START event needs to be pushed */
333 gboolean push_stream_start;
335 /* When we need to skip more data than we have currently */
338 /* Tag handling (stream tags only, global tags are passed through as-is) */
339 GstTagList *upstream_tags;
340 GstTagList *parser_tags;
341 GstTagMergeMode parser_tags_merge_mode;
342 gboolean tags_changed;
344 /* Current segment seqnum */
345 guint32 segment_seqnum;
348 typedef struct _GstBaseParseSeek
353 GstClockTime start_ts;
356 #define DEFAULT_DISABLE_PASSTHROUGH FALSE
361 PROP_DISABLE_PASSTHROUGH,
365 #define GST_BASE_PARSE_INDEX_LOCK(parse) \
366 g_mutex_lock (&parse->priv->index_lock);
367 #define GST_BASE_PARSE_INDEX_UNLOCK(parse) \
368 g_mutex_unlock (&parse->priv->index_lock);
370 static GstElementClass *parent_class = NULL;
371 static gint base_parse_private_offset = 0;
373 static void gst_base_parse_class_init (GstBaseParseClass * klass);
374 static void gst_base_parse_init (GstBaseParse * parse,
375 GstBaseParseClass * klass);
378 gst_base_parse_get_type (void)
380 static volatile gsize base_parse_type = 0;
382 if (g_once_init_enter (&base_parse_type)) {
383 static const GTypeInfo base_parse_info = {
384 sizeof (GstBaseParseClass),
385 (GBaseInitFunc) NULL,
386 (GBaseFinalizeFunc) NULL,
387 (GClassInitFunc) gst_base_parse_class_init,
390 sizeof (GstBaseParse),
392 (GInstanceInitFunc) gst_base_parse_init,
396 _type = g_type_register_static (GST_TYPE_ELEMENT,
397 "GstBaseParse", &base_parse_info, G_TYPE_FLAG_ABSTRACT);
399 base_parse_private_offset =
400 g_type_add_instance_private (_type, sizeof (GstBaseParsePrivate));
402 g_once_init_leave (&base_parse_type, _type);
404 return (GType) base_parse_type;
407 static inline GstBaseParsePrivate *
408 gst_base_parse_get_instance_private (GstBaseParse * self)
410 return (G_STRUCT_MEMBER_P (self, base_parse_private_offset));
413 static void gst_base_parse_finalize (GObject * object);
415 static GstStateChangeReturn gst_base_parse_change_state (GstElement * element,
416 GstStateChange transition);
417 static void gst_base_parse_reset (GstBaseParse * parse);
420 static void gst_base_parse_set_index (GstElement * element, GstIndex * index);
421 static GstIndex *gst_base_parse_get_index (GstElement * element);
424 static gboolean gst_base_parse_sink_activate (GstPad * sinkpad,
426 static gboolean gst_base_parse_sink_activate_mode (GstPad * pad,
427 GstObject * parent, GstPadMode mode, gboolean active);
428 static gboolean gst_base_parse_handle_seek (GstBaseParse * parse,
430 static void gst_base_parse_set_upstream_tags (GstBaseParse * parse,
431 GstTagList * taglist);
433 static void gst_base_parse_set_property (GObject * object, guint prop_id,
434 const GValue * value, GParamSpec * pspec);
435 static void gst_base_parse_get_property (GObject * object, guint prop_id,
436 GValue * value, GParamSpec * pspec);
438 static gboolean gst_base_parse_src_event (GstPad * pad, GstObject * parent,
440 static gboolean gst_base_parse_src_query (GstPad * pad, GstObject * parent,
443 static gboolean gst_base_parse_sink_event (GstPad * pad, GstObject * parent,
445 static gboolean gst_base_parse_sink_query (GstPad * pad, GstObject * parent,
448 static GstFlowReturn gst_base_parse_chain (GstPad * pad, GstObject * parent,
450 static void gst_base_parse_loop (GstPad * pad);
452 static GstFlowReturn gst_base_parse_parse_frame (GstBaseParse * parse,
453 GstBaseParseFrame * frame);
455 static gboolean gst_base_parse_sink_event_default (GstBaseParse * parse,
458 static gboolean gst_base_parse_src_event_default (GstBaseParse * parse,
461 static gboolean gst_base_parse_sink_query_default (GstBaseParse * parse,
463 static gboolean gst_base_parse_src_query_default (GstBaseParse * parse,
466 static gint64 gst_base_parse_find_offset (GstBaseParse * parse,
467 GstClockTime time, gboolean before, GstClockTime * _ts);
468 static GstFlowReturn gst_base_parse_locate_time (GstBaseParse * parse,
469 GstClockTime * _time, gint64 * _offset);
471 static GstFlowReturn gst_base_parse_start_fragment (GstBaseParse * parse);
472 static GstFlowReturn gst_base_parse_finish_fragment (GstBaseParse * parse,
474 static GstFlowReturn gst_base_parse_send_buffers (GstBaseParse * parse);
476 static inline GstFlowReturn gst_base_parse_check_sync (GstBaseParse * parse);
478 static gboolean gst_base_parse_is_seekable (GstBaseParse * parse);
480 static void gst_base_parse_push_pending_events (GstBaseParse * parse);
483 gst_base_parse_clear_queues (GstBaseParse * parse)
485 g_slist_foreach (parse->priv->buffers_queued, (GFunc) gst_buffer_unref, NULL);
486 g_slist_free (parse->priv->buffers_queued);
487 parse->priv->buffers_queued = NULL;
488 g_slist_foreach (parse->priv->buffers_pending, (GFunc) gst_buffer_unref,
490 g_slist_free (parse->priv->buffers_pending);
491 parse->priv->buffers_pending = NULL;
492 g_slist_foreach (parse->priv->buffers_head, (GFunc) gst_buffer_unref, NULL);
493 g_slist_free (parse->priv->buffers_head);
494 parse->priv->buffers_head = NULL;
495 g_slist_foreach (parse->priv->buffers_send, (GFunc) gst_buffer_unref, NULL);
496 g_slist_free (parse->priv->buffers_send);
497 parse->priv->buffers_send = NULL;
499 g_list_foreach (parse->priv->detect_buffers, (GFunc) gst_buffer_unref, NULL);
500 g_list_free (parse->priv->detect_buffers);
501 parse->priv->detect_buffers = NULL;
502 parse->priv->detect_buffers_size = 0;
504 g_queue_foreach (&parse->priv->queued_frames,
505 (GFunc) gst_base_parse_frame_free, NULL);
506 g_queue_clear (&parse->priv->queued_frames);
508 gst_buffer_replace (&parse->priv->cache, NULL);
510 g_list_foreach (parse->priv->pending_events, (GFunc) gst_event_unref, NULL);
511 g_list_free (parse->priv->pending_events);
512 parse->priv->pending_events = NULL;
514 parse->priv->checked_media = FALSE;
518 gst_base_parse_finalize (GObject * object)
520 GstBaseParse *parse = GST_BASE_PARSE (object);
522 g_object_unref (parse->priv->adapter);
524 if (parse->priv->index) {
525 gst_object_unref (parse->priv->index);
526 parse->priv->index = NULL;
528 g_mutex_clear (&parse->priv->index_lock);
530 gst_base_parse_clear_queues (parse);
532 G_OBJECT_CLASS (parent_class)->finalize (object);
536 gst_base_parse_class_init (GstBaseParseClass * klass)
538 GObjectClass *gobject_class;
539 GstElementClass *gstelement_class;
541 gobject_class = G_OBJECT_CLASS (klass);
543 if (base_parse_private_offset != 0)
544 g_type_class_adjust_private_offset (klass, &base_parse_private_offset);
546 parent_class = g_type_class_peek_parent (klass);
548 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_parse_finalize);
549 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_base_parse_set_property);
550 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_base_parse_get_property);
553 * GstBaseParse:disable-passthrough:
555 * If set to %TRUE, baseparse will unconditionally force parsing of the
556 * incoming data. This can be required in the rare cases where the incoming
557 * side-data (caps, pts, dts, ...) is not trusted by the user and wants to
558 * force validation and parsing of the incoming data.
559 * If set to %FALSE, decision of whether to parse the data or not is up to
560 * the implementation (standard behaviour).
562 g_object_class_install_property (gobject_class, PROP_DISABLE_PASSTHROUGH,
563 g_param_spec_boolean ("disable-passthrough", "Disable passthrough",
564 "Force processing (disables passthrough)",
565 DEFAULT_DISABLE_PASSTHROUGH,
566 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
568 gstelement_class = (GstElementClass *) klass;
569 gstelement_class->change_state =
570 GST_DEBUG_FUNCPTR (gst_base_parse_change_state);
573 gstelement_class->set_index = GST_DEBUG_FUNCPTR (gst_base_parse_set_index);
574 gstelement_class->get_index = GST_DEBUG_FUNCPTR (gst_base_parse_get_index);
577 /* Default handlers */
578 klass->sink_event = gst_base_parse_sink_event_default;
579 klass->src_event = gst_base_parse_src_event_default;
580 klass->sink_query = gst_base_parse_sink_query_default;
581 klass->src_query = gst_base_parse_src_query_default;
582 klass->convert = gst_base_parse_convert_default;
584 GST_DEBUG_CATEGORY_INIT (gst_base_parse_debug, "baseparse", 0,
585 "baseparse element");
589 gst_base_parse_init (GstBaseParse * parse, GstBaseParseClass * bclass)
591 GstPadTemplate *pad_template;
593 GST_DEBUG_OBJECT (parse, "gst_base_parse_init");
595 parse->priv = gst_base_parse_get_instance_private (parse);
598 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "sink");
599 g_return_if_fail (pad_template != NULL);
600 parse->sinkpad = gst_pad_new_from_template (pad_template, "sink");
601 gst_pad_set_event_function (parse->sinkpad,
602 GST_DEBUG_FUNCPTR (gst_base_parse_sink_event));
603 gst_pad_set_query_function (parse->sinkpad,
604 GST_DEBUG_FUNCPTR (gst_base_parse_sink_query));
605 gst_pad_set_chain_function (parse->sinkpad,
606 GST_DEBUG_FUNCPTR (gst_base_parse_chain));
607 gst_pad_set_activate_function (parse->sinkpad,
608 GST_DEBUG_FUNCPTR (gst_base_parse_sink_activate));
609 gst_pad_set_activatemode_function (parse->sinkpad,
610 GST_DEBUG_FUNCPTR (gst_base_parse_sink_activate_mode));
611 GST_PAD_SET_PROXY_ALLOCATION (parse->sinkpad);
612 gst_element_add_pad (GST_ELEMENT (parse), parse->sinkpad);
614 GST_DEBUG_OBJECT (parse, "sinkpad created");
617 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass), "src");
618 g_return_if_fail (pad_template != NULL);
619 parse->srcpad = gst_pad_new_from_template (pad_template, "src");
620 gst_pad_set_event_function (parse->srcpad,
621 GST_DEBUG_FUNCPTR (gst_base_parse_src_event));
622 gst_pad_set_query_function (parse->srcpad,
623 GST_DEBUG_FUNCPTR (gst_base_parse_src_query));
624 gst_pad_use_fixed_caps (parse->srcpad);
625 gst_element_add_pad (GST_ELEMENT (parse), parse->srcpad);
626 GST_DEBUG_OBJECT (parse, "src created");
628 g_queue_init (&parse->priv->queued_frames);
630 parse->priv->adapter = gst_adapter_new ();
632 parse->priv->pad_mode = GST_PAD_MODE_NONE;
634 g_mutex_init (&parse->priv->index_lock);
637 gst_base_parse_reset (parse);
638 GST_DEBUG_OBJECT (parse, "init ok");
640 GST_OBJECT_FLAG_SET (parse, GST_ELEMENT_FLAG_INDEXABLE);
642 parse->priv->upstream_tags = NULL;
643 parse->priv->parser_tags = NULL;
644 parse->priv->parser_tags_merge_mode = GST_TAG_MERGE_APPEND;
648 gst_base_parse_set_property (GObject * object, guint prop_id,
649 const GValue * value, GParamSpec * pspec)
651 GstBaseParse *parse = GST_BASE_PARSE (object);
654 case PROP_DISABLE_PASSTHROUGH:
655 parse->priv->disable_passthrough = g_value_get_boolean (value);
658 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
664 gst_base_parse_get_property (GObject * object, guint prop_id, GValue * value,
667 GstBaseParse *parse = GST_BASE_PARSE (object);
670 case PROP_DISABLE_PASSTHROUGH:
671 g_value_set_boolean (value, parse->priv->disable_passthrough);
674 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
680 * gst_base_parse_frame_copy:
681 * @frame: a #GstBaseParseFrame
683 * Copies a #GstBaseParseFrame.
685 * Returns: A copy of @frame
689 gst_base_parse_frame_copy (GstBaseParseFrame * frame)
691 GstBaseParseFrame *copy;
693 copy = g_slice_dup (GstBaseParseFrame, frame);
694 copy->buffer = gst_buffer_ref (frame->buffer);
695 copy->_private_flags &= ~GST_BASE_PARSE_FRAME_PRIVATE_FLAG_NOALLOC;
697 GST_TRACE ("copied frame %p -> %p", frame, copy);
703 * gst_base_parse_frame_free:
704 * @frame: A #GstBaseParseFrame
706 * Frees the provided @frame.
709 gst_base_parse_frame_free (GstBaseParseFrame * frame)
711 GST_TRACE ("freeing frame %p", frame);
714 gst_buffer_unref (frame->buffer);
715 frame->buffer = NULL;
718 if (!(frame->_private_flags & GST_BASE_PARSE_FRAME_PRIVATE_FLAG_NOALLOC)) {
719 g_slice_free (GstBaseParseFrame, frame);
721 memset (frame, 0, sizeof (*frame));
725 G_DEFINE_BOXED_TYPE (GstBaseParseFrame, gst_base_parse_frame,
726 (GBoxedCopyFunc) gst_base_parse_frame_copy,
727 (GBoxedFreeFunc) gst_base_parse_frame_free);
730 * gst_base_parse_frame_init:
731 * @frame: #GstBaseParseFrame.
733 * Sets a #GstBaseParseFrame to initial state. Currently this means
734 * all public fields are zero-ed and a private flag is set to make
735 * sure gst_base_parse_frame_free() only frees the contents but not
736 * the actual frame. Use this function to initialise a #GstBaseParseFrame
737 * allocated on the stack.
740 gst_base_parse_frame_init (GstBaseParseFrame * frame)
742 memset (frame, 0, sizeof (GstBaseParseFrame));
743 frame->_private_flags = GST_BASE_PARSE_FRAME_PRIVATE_FLAG_NOALLOC;
744 GST_TRACE ("inited frame %p", frame);
748 * gst_base_parse_frame_new:
749 * @buffer: (transfer none): a #GstBuffer
751 * @overhead: number of bytes in this frame which should be counted as
752 * metadata overhead, ie. not used to calculate the average bitrate.
753 * Set to -1 to mark the entire frame as metadata. If in doubt, set to 0.
755 * Allocates a new #GstBaseParseFrame. This function is mainly for bindings,
756 * elements written in C should usually allocate the frame on the stack and
757 * then use gst_base_parse_frame_init() to initialise it.
759 * Returns: a newly-allocated #GstBaseParseFrame. Free with
760 * gst_base_parse_frame_free() when no longer needed.
763 gst_base_parse_frame_new (GstBuffer * buffer, GstBaseParseFrameFlags flags,
766 GstBaseParseFrame *frame;
768 frame = g_slice_new0 (GstBaseParseFrame);
769 frame->buffer = gst_buffer_ref (buffer);
771 GST_TRACE ("created frame %p", frame);
776 gst_base_parse_update_flags (GstBaseParse * parse)
780 /* set flags one by one for clarity */
781 if (G_UNLIKELY (parse->priv->drain))
782 parse->flags |= GST_BASE_PARSE_FLAG_DRAINING;
784 /* losing sync is pretty much a discont (and vice versa), no ? */
785 if (G_UNLIKELY (parse->priv->discont))
786 parse->flags |= GST_BASE_PARSE_FLAG_LOST_SYNC;
790 gst_base_parse_update_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
792 if (G_UNLIKELY (parse->priv->discont)) {
793 GST_DEBUG_OBJECT (parse, "marking DISCONT");
794 GST_BUFFER_FLAG_SET (frame->buffer, GST_BUFFER_FLAG_DISCONT);
796 GST_BUFFER_FLAG_UNSET (frame->buffer, GST_BUFFER_FLAG_DISCONT);
799 if (parse->priv->prev_offset != parse->priv->offset || parse->priv->new_frame) {
800 GST_LOG_OBJECT (parse, "marking as new frame");
801 frame->flags |= GST_BASE_PARSE_FRAME_FLAG_NEW_FRAME;
804 frame->offset = parse->priv->prev_offset = parse->priv->offset;
808 gst_base_parse_reset (GstBaseParse * parse)
810 GST_OBJECT_LOCK (parse);
811 gst_segment_init (&parse->segment, GST_FORMAT_TIME);
812 parse->priv->duration = -1;
813 parse->priv->min_frame_size = 1;
814 parse->priv->discont = TRUE;
815 parse->priv->flushing = FALSE;
816 parse->priv->saw_gaps = FALSE;
817 parse->priv->offset = 0;
818 parse->priv->sync_offset = 0;
819 parse->priv->update_interval = -1;
820 parse->priv->fps_num = parse->priv->fps_den = 0;
821 parse->priv->frame_duration = GST_CLOCK_TIME_NONE;
822 parse->priv->lead_in = parse->priv->lead_out = 0;
823 parse->priv->lead_in_ts = parse->priv->lead_out_ts = 0;
824 parse->priv->bitrate = 0;
825 parse->priv->framecount = 0;
826 parse->priv->bytecount = 0;
827 parse->priv->acc_duration = 0;
828 parse->priv->first_frame_pts = GST_CLOCK_TIME_NONE;
829 parse->priv->first_frame_dts = GST_CLOCK_TIME_NONE;
830 parse->priv->first_frame_offset = -1;
831 parse->priv->estimated_duration = -1;
832 parse->priv->estimated_drift = 0;
833 parse->priv->next_pts = GST_CLOCK_TIME_NONE;
834 parse->priv->next_dts = 0;
835 parse->priv->syncable = TRUE;
836 parse->priv->disable_passthrough = DEFAULT_DISABLE_PASSTHROUGH;
837 parse->priv->passthrough = FALSE;
838 parse->priv->pts_interpolate = TRUE;
839 parse->priv->infer_ts = TRUE;
840 parse->priv->has_timing_info = FALSE;
841 parse->priv->min_bitrate = G_MAXUINT;
842 parse->priv->max_bitrate = 0;
843 parse->priv->avg_bitrate = 0;
844 parse->priv->posted_avg_bitrate = 0;
846 parse->priv->index_last_ts = GST_CLOCK_TIME_NONE;
847 parse->priv->index_last_offset = -1;
848 parse->priv->index_last_valid = TRUE;
849 parse->priv->upstream_seekable = FALSE;
850 parse->priv->upstream_size = 0;
851 parse->priv->upstream_has_duration = FALSE;
852 parse->priv->upstream_format = GST_FORMAT_UNDEFINED;
853 parse->priv->idx_interval = 0;
854 parse->priv->idx_byte_interval = 0;
855 parse->priv->exact_position = TRUE;
856 parse->priv->seen_keyframe = FALSE;
857 parse->priv->checked_media = FALSE;
859 parse->priv->last_dts = GST_CLOCK_TIME_NONE;
860 parse->priv->last_pts = GST_CLOCK_TIME_NONE;
861 parse->priv->last_offset = 0;
863 parse->priv->skip = 0;
865 g_list_foreach (parse->priv->pending_events, (GFunc) gst_mini_object_unref,
867 g_list_free (parse->priv->pending_events);
868 parse->priv->pending_events = NULL;
870 if (parse->priv->cache) {
871 gst_buffer_unref (parse->priv->cache);
872 parse->priv->cache = NULL;
875 g_slist_foreach (parse->priv->pending_seeks, (GFunc) g_free, NULL);
876 g_slist_free (parse->priv->pending_seeks);
877 parse->priv->pending_seeks = NULL;
879 if (parse->priv->adapter)
880 gst_adapter_clear (parse->priv->adapter);
882 gst_base_parse_set_upstream_tags (parse, NULL);
884 if (parse->priv->parser_tags) {
885 gst_tag_list_unref (parse->priv->parser_tags);
886 parse->priv->parser_tags = NULL;
888 parse->priv->parser_tags_merge_mode = GST_TAG_MERGE_APPEND;
890 parse->priv->new_frame = TRUE;
892 parse->priv->first_buffer = TRUE;
894 g_list_foreach (parse->priv->detect_buffers, (GFunc) gst_buffer_unref, NULL);
895 g_list_free (parse->priv->detect_buffers);
896 parse->priv->detect_buffers = NULL;
897 parse->priv->detect_buffers_size = 0;
899 parse->priv->segment_seqnum = GST_SEQNUM_INVALID;
900 GST_OBJECT_UNLOCK (parse);
904 gst_base_parse_check_bitrate_tag (GstBaseParse * parse, const gchar * tag)
906 gboolean got_tag = FALSE;
909 if (parse->priv->upstream_tags != NULL)
910 got_tag = gst_tag_list_get_uint (parse->priv->upstream_tags, tag, &n);
912 if (!got_tag && parse->priv->parser_tags != NULL)
913 got_tag = gst_tag_list_get_uint (parse->priv->parser_tags, tag, &n);
918 /* check if upstream or subclass tags contain bitrates already */
920 gst_base_parse_check_bitrate_tags (GstBaseParse * parse)
922 parse->priv->post_min_bitrate =
923 !gst_base_parse_check_bitrate_tag (parse, GST_TAG_MINIMUM_BITRATE);
924 parse->priv->post_avg_bitrate =
925 !gst_base_parse_check_bitrate_tag (parse, GST_TAG_BITRATE);
926 parse->priv->post_max_bitrate =
927 !gst_base_parse_check_bitrate_tag (parse, GST_TAG_MAXIMUM_BITRATE);
930 /* Queues new tag event with the current combined state of the stream tags
931 * (i.e. upstream tags merged with subclass tags and current baseparse tags) */
933 gst_base_parse_queue_tag_event_update (GstBaseParse * parse)
935 GstTagList *merged_tags;
937 GST_LOG_OBJECT (parse, "upstream : %" GST_PTR_FORMAT,
938 parse->priv->upstream_tags);
939 GST_LOG_OBJECT (parse, "parser : %" GST_PTR_FORMAT,
940 parse->priv->parser_tags);
941 GST_LOG_OBJECT (parse, "mode : %d", parse->priv->parser_tags_merge_mode);
944 gst_tag_list_merge (parse->priv->upstream_tags, parse->priv->parser_tags,
945 parse->priv->parser_tags_merge_mode);
947 GST_DEBUG_OBJECT (parse, "merged : %" GST_PTR_FORMAT, merged_tags);
949 if (merged_tags == NULL)
952 if (gst_tag_list_is_empty (merged_tags)) {
953 gst_tag_list_unref (merged_tags);
957 if (parse->priv->framecount >= MIN_FRAMES_TO_POST_BITRATE) {
958 /* only add bitrate tags to non-empty taglists for now, and only if neither
959 * upstream tags nor the subclass sets the bitrate tag in question already */
960 if (parse->priv->min_bitrate != G_MAXUINT && parse->priv->post_min_bitrate) {
961 GST_LOG_OBJECT (parse, "adding min bitrate %u", parse->priv->min_bitrate);
962 gst_tag_list_add (merged_tags, GST_TAG_MERGE_KEEP,
963 GST_TAG_MINIMUM_BITRATE, parse->priv->min_bitrate, NULL);
965 if (parse->priv->max_bitrate != 0 && parse->priv->post_max_bitrate) {
966 GST_LOG_OBJECT (parse, "adding max bitrate %u", parse->priv->max_bitrate);
967 gst_tag_list_add (merged_tags, GST_TAG_MERGE_KEEP,
968 GST_TAG_MAXIMUM_BITRATE, parse->priv->max_bitrate, NULL);
970 if (parse->priv->avg_bitrate != 0 && parse->priv->post_avg_bitrate) {
971 parse->priv->posted_avg_bitrate = parse->priv->avg_bitrate;
972 GST_LOG_OBJECT (parse, "adding avg bitrate %u", parse->priv->avg_bitrate);
973 gst_tag_list_add (merged_tags, GST_TAG_MERGE_KEEP,
974 GST_TAG_BITRATE, parse->priv->avg_bitrate, NULL);
978 parse->priv->pending_events =
979 g_list_prepend (parse->priv->pending_events,
980 gst_event_new_tag (merged_tags));
983 /* gst_base_parse_parse_frame:
984 * @parse: #GstBaseParse.
985 * @buffer: #GstBuffer.
987 * Default callback for parse_frame.
990 gst_base_parse_parse_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
992 GstBuffer *buffer = frame->buffer;
994 if (!GST_BUFFER_PTS_IS_VALID (buffer) &&
995 GST_CLOCK_TIME_IS_VALID (parse->priv->next_pts)) {
996 GST_BUFFER_PTS (buffer) = parse->priv->next_pts;
998 if (!GST_BUFFER_DTS_IS_VALID (buffer) &&
999 GST_CLOCK_TIME_IS_VALID (parse->priv->next_dts)) {
1000 GST_BUFFER_DTS (buffer) = parse->priv->next_dts;
1002 if (!GST_BUFFER_DURATION_IS_VALID (buffer) &&
1003 GST_CLOCK_TIME_IS_VALID (parse->priv->frame_duration)) {
1004 GST_BUFFER_DURATION (buffer) = parse->priv->frame_duration;
1009 /* gst_base_parse_convert:
1010 * @parse: #GstBaseParse.
1011 * @src_format: #GstFormat describing the source format.
1012 * @src_value: Source value to be converted.
1013 * @dest_format: #GstFormat defining the converted format.
1014 * @dest_value: Pointer where the conversion result will be put.
1016 * Converts using configured "convert" vmethod in #GstBaseParse class.
1018 * Returns: %TRUE if conversion was successful.
1021 gst_base_parse_convert (GstBaseParse * parse,
1022 GstFormat src_format,
1023 gint64 src_value, GstFormat dest_format, gint64 * dest_value)
1025 GstBaseParseClass *klass = GST_BASE_PARSE_GET_CLASS (parse);
1028 g_return_val_if_fail (dest_value != NULL, FALSE);
1030 if (!klass->convert)
1033 ret = klass->convert (parse, src_format, src_value, dest_format, dest_value);
1035 #ifndef GST_DISABLE_GST_DEBUG
1038 if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES) {
1039 GST_LOG_OBJECT (parse,
1040 "TIME -> BYTES: %" GST_TIME_FORMAT " -> %" G_GINT64_FORMAT,
1041 GST_TIME_ARGS (src_value), *dest_value);
1042 } else if (dest_format == GST_FORMAT_TIME &&
1043 src_format == GST_FORMAT_BYTES) {
1044 GST_LOG_OBJECT (parse,
1045 "BYTES -> TIME: %" G_GINT64_FORMAT " -> %" GST_TIME_FORMAT,
1046 src_value, GST_TIME_ARGS (*dest_value));
1048 GST_LOG_OBJECT (parse,
1049 "%s -> %s: %" G_GINT64_FORMAT " -> %" G_GINT64_FORMAT,
1050 GST_STR_NULL (gst_format_get_name (src_format)),
1051 GST_STR_NULL (gst_format_get_name (dest_format)),
1052 src_value, *dest_value);
1055 GST_DEBUG_OBJECT (parse, "conversion failed");
1064 update_upstream_provided (GQuark field_id, const GValue * value,
1067 GstCaps *default_caps = user_data;
1071 caps_size = gst_caps_get_size (default_caps);
1072 for (i = 0; i < caps_size; i++) {
1073 GstStructure *structure = gst_caps_get_structure (default_caps, i);
1074 if (gst_structure_id_has_field (structure, field_id))
1075 gst_structure_id_set_value (structure, field_id, value);
1082 gst_base_parse_negotiate_default_caps (GstBaseParse * parse)
1084 GstCaps *caps, *templcaps;
1085 GstCaps *sinkcaps = NULL;
1086 GstCaps *default_caps = NULL;
1087 GstStructure *structure;
1089 templcaps = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SRC_PAD (parse));
1090 caps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), templcaps);
1092 gst_caps_unref (templcaps);
1097 if (!caps || gst_caps_is_empty (caps) || gst_caps_is_any (caps)) {
1101 GST_LOG_OBJECT (parse, "peer caps %" GST_PTR_FORMAT, caps);
1103 /* before fixating, try to use whatever upstream provided */
1104 default_caps = gst_caps_copy (caps);
1105 sinkcaps = gst_pad_get_current_caps (GST_BASE_PARSE_SINK_PAD (parse));
1107 GST_LOG_OBJECT (parse, "current caps %" GST_PTR_FORMAT " for sinkpad",
1111 structure = gst_caps_get_structure (sinkcaps, 0);
1112 gst_structure_foreach (structure, update_upstream_provided, default_caps);
1115 default_caps = gst_caps_fixate (default_caps);
1117 if (!default_caps) {
1118 GST_WARNING_OBJECT (parse, "Failed to create default caps !");
1122 GST_INFO_OBJECT (parse,
1123 "Chose default caps %" GST_PTR_FORMAT " for initial gap", default_caps);
1126 gst_caps_unref (sinkcaps);
1127 gst_caps_unref (caps);
1129 return default_caps;
1134 gst_caps_unref (caps);
1136 gst_caps_unref (sinkcaps);
1141 /* gst_base_parse_sink_event:
1142 * @pad: #GstPad that received the event.
1143 * @event: #GstEvent to be handled.
1145 * Handler for sink pad events.
1147 * Returns: %TRUE if the event was handled.
1150 gst_base_parse_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
1152 GstBaseParse *parse = GST_BASE_PARSE (parent);
1153 GstBaseParseClass *bclass = GST_BASE_PARSE_GET_CLASS (parse);
1156 ret = bclass->sink_event (parse, event);
1161 /* gst_base_parse_sink_event_default:
1162 * @parse: #GstBaseParse.
1163 * @event: #GstEvent to be handled.
1165 * Element-level event handler function.
1167 * The event will be unreffed only if it has been handled and this
1168 * function returns %TRUE
1170 * Returns: %TRUE if the event was handled and not need forwarding.
1173 gst_base_parse_sink_event_default (GstBaseParse * parse, GstEvent * event)
1175 GstBaseParseClass *klass = GST_BASE_PARSE_GET_CLASS (parse);
1176 gboolean ret = FALSE;
1177 gboolean forward_immediate = FALSE;
1179 GST_DEBUG_OBJECT (parse, "handling event %d, %s", GST_EVENT_TYPE (event),
1180 GST_EVENT_TYPE_NAME (event));
1182 switch (GST_EVENT_TYPE (event)) {
1183 case GST_EVENT_CAPS:
1187 gst_event_parse_caps (event, &caps);
1188 GST_DEBUG_OBJECT (parse, "caps: %" GST_PTR_FORMAT, caps);
1190 if (klass->set_sink_caps)
1191 ret = klass->set_sink_caps (parse, caps);
1195 /* will send our own caps downstream */
1196 gst_event_unref (event);
1200 case GST_EVENT_SEGMENT:
1202 const GstSegment *in_segment;
1203 GstSegment out_segment;
1204 gint64 offset = 0, next_dts;
1206 parse->priv->segment_seqnum = gst_event_get_seqnum (event);
1207 gst_event_parse_segment (event, &in_segment);
1208 gst_segment_init (&out_segment, GST_FORMAT_TIME);
1209 out_segment.rate = in_segment->rate;
1210 out_segment.applied_rate = in_segment->applied_rate;
1212 GST_DEBUG_OBJECT (parse, "New segment %" GST_SEGMENT_FORMAT, in_segment);
1213 GST_DEBUG_OBJECT (parse, "Current segment %" GST_SEGMENT_FORMAT,
1216 parse->priv->upstream_format = in_segment->format;
1217 if (in_segment->format == GST_FORMAT_BYTES) {
1218 GstBaseParseSeek *seek = NULL;
1221 /* stop time is allowed to be open-ended, but not start & pos */
1222 offset = in_segment->time;
1224 GST_OBJECT_LOCK (parse);
1225 for (node = parse->priv->pending_seeks; node; node = node->next) {
1226 GstBaseParseSeek *tmp = node->data;
1228 if (tmp->offset == offset) {
1233 parse->priv->pending_seeks =
1234 g_slist_remove (parse->priv->pending_seeks, seek);
1235 GST_OBJECT_UNLOCK (parse);
1238 GST_DEBUG_OBJECT (parse,
1239 "Matched newsegment to%s seek: %" GST_SEGMENT_FORMAT,
1240 seek->accurate ? " accurate" : "", &seek->segment);
1242 out_segment.start = seek->segment.start;
1243 out_segment.stop = seek->segment.stop;
1244 out_segment.time = seek->segment.start;
1246 next_dts = seek->start_ts;
1247 parse->priv->exact_position = seek->accurate;
1250 /* best attempt convert */
1251 /* as these are only estimates, stop is kept open-ended to avoid
1252 * premature cutting */
1253 gst_base_parse_convert (parse, GST_FORMAT_BYTES, in_segment->start,
1254 GST_FORMAT_TIME, (gint64 *) & next_dts);
1256 out_segment.start = next_dts;
1257 out_segment.stop = GST_CLOCK_TIME_NONE;
1258 out_segment.time = next_dts;
1260 parse->priv->exact_position = (in_segment->start == 0);
1263 gst_event_unref (event);
1265 event = gst_event_new_segment (&out_segment);
1266 gst_event_set_seqnum (event, parse->priv->segment_seqnum);
1268 GST_DEBUG_OBJECT (parse, "Converted incoming segment to TIME. %"
1269 GST_SEGMENT_FORMAT, in_segment);
1271 } else if (in_segment->format != GST_FORMAT_TIME) {
1272 /* Unknown incoming segment format. Output a default open-ended
1274 gst_event_unref (event);
1276 out_segment.start = 0;
1277 out_segment.stop = GST_CLOCK_TIME_NONE;
1278 out_segment.time = 0;
1280 event = gst_event_new_segment (&out_segment);
1281 gst_event_set_seqnum (event, parse->priv->segment_seqnum);
1285 /* not considered BYTE seekable if it is talking to us in TIME,
1286 * whatever else it might claim */
1287 parse->priv->upstream_seekable = FALSE;
1288 next_dts = in_segment->start;
1289 gst_event_copy_segment (event, &out_segment);
1292 GST_DEBUG_OBJECT (parse, "OUT segment %" GST_SEGMENT_FORMAT,
1294 memcpy (&parse->segment, &out_segment, sizeof (GstSegment));
1297 gst_segment_set_newsegment (&parse->segment, update, rate,
1298 applied_rate, format, start, stop, start);
1303 /* save the segment for later, right before we push a new buffer so that
1304 * the caps are fixed and the next linked element can receive
1305 * the segment but finish the current segment */
1306 GST_DEBUG_OBJECT (parse, "draining current segment");
1307 if (in_segment->rate > 0.0)
1308 gst_base_parse_drain (parse);
1310 gst_base_parse_finish_fragment (parse, FALSE);
1311 gst_adapter_clear (parse->priv->adapter);
1313 parse->priv->offset = offset;
1314 parse->priv->sync_offset = offset;
1315 parse->priv->next_dts = next_dts;
1316 parse->priv->next_pts = GST_CLOCK_TIME_NONE;
1317 parse->priv->last_pts = GST_CLOCK_TIME_NONE;
1318 parse->priv->last_dts = GST_CLOCK_TIME_NONE;
1319 parse->priv->prev_pts = GST_CLOCK_TIME_NONE;
1320 parse->priv->prev_dts = GST_CLOCK_TIME_NONE;
1321 parse->priv->prev_dts_from_pts = FALSE;
1322 parse->priv->discont = TRUE;
1323 parse->priv->seen_keyframe = FALSE;
1324 parse->priv->skip = 0;
1328 case GST_EVENT_SEGMENT_DONE:
1329 /* need to drain now, rather than upon a new segment,
1330 * since that would have SEGMENT_DONE come before potential
1331 * delayed last part of the current segment */
1332 GST_DEBUG_OBJECT (parse, "draining current segment");
1333 if (parse->segment.rate > 0.0)
1334 gst_base_parse_drain (parse);
1336 gst_base_parse_finish_fragment (parse, FALSE);
1337 /* Also forward event immediately, there might be no new data
1338 * coming afterwards that would allow us to forward it later */
1339 forward_immediate = TRUE;
1342 case GST_EVENT_FLUSH_START:
1343 GST_OBJECT_LOCK (parse);
1344 parse->priv->flushing = TRUE;
1345 GST_OBJECT_UNLOCK (parse);
1348 case GST_EVENT_FLUSH_STOP:
1349 gst_adapter_clear (parse->priv->adapter);
1350 gst_base_parse_clear_queues (parse);
1351 parse->priv->flushing = FALSE;
1352 parse->priv->discont = TRUE;
1353 parse->priv->last_pts = GST_CLOCK_TIME_NONE;
1354 parse->priv->last_dts = GST_CLOCK_TIME_NONE;
1355 parse->priv->new_frame = TRUE;
1356 parse->priv->skip = 0;
1358 forward_immediate = TRUE;
1362 if (parse->segment.rate > 0.0)
1363 gst_base_parse_drain (parse);
1365 gst_base_parse_finish_fragment (parse, TRUE);
1367 /* If we STILL have zero frames processed, fire an error */
1368 if (parse->priv->framecount == 0 && !parse->priv->saw_gaps &&
1369 !parse->priv->first_buffer) {
1370 GST_ELEMENT_ERROR (parse, STREAM, WRONG_TYPE,
1371 ("No valid frames found before end of stream"), (NULL));
1374 if (!parse->priv->saw_gaps
1375 && parse->priv->framecount < MIN_FRAMES_TO_POST_BITRATE) {
1376 /* We've not posted bitrate tags yet - do so now */
1377 gst_base_parse_queue_tag_event_update (parse);
1380 /* newsegment and other serialized events before eos */
1381 gst_base_parse_push_pending_events (parse);
1383 forward_immediate = TRUE;
1385 case GST_EVENT_CUSTOM_DOWNSTREAM:{
1386 /* FIXME: Code duplicated from libgstvideo because core can't depend on -base */
1387 #ifndef GST_VIDEO_EVENT_STILL_STATE_NAME
1388 #define GST_VIDEO_EVENT_STILL_STATE_NAME "GstEventStillFrame"
1391 const GstStructure *s;
1392 gboolean ev_still_state;
1394 s = gst_event_get_structure (event);
1396 gst_structure_has_name (s, GST_VIDEO_EVENT_STILL_STATE_NAME) &&
1397 gst_structure_get_boolean (s, "still-state", &ev_still_state)) {
1398 if (ev_still_state) {
1399 GST_DEBUG_OBJECT (parse, "draining current data for still-frame");
1400 if (parse->segment.rate > 0.0)
1401 gst_base_parse_drain (parse);
1403 gst_base_parse_finish_fragment (parse, TRUE);
1405 forward_immediate = TRUE;
1411 GST_DEBUG_OBJECT (parse, "draining current data due to gap event");
1413 /* Ensure we have caps before forwarding the event */
1414 if (!gst_pad_has_current_caps (GST_BASE_PARSE_SRC_PAD (parse))) {
1415 GstCaps *default_caps = NULL;
1416 if ((default_caps = gst_base_parse_negotiate_default_caps (parse))) {
1418 GstEvent *caps_event = gst_event_new_caps (default_caps);
1420 GST_DEBUG_OBJECT (parse,
1421 "Store caps event to pending list for initial pre-rolling");
1423 /* Events are in decreasing order. Go down the list until we
1424 * find the first pre-CAPS event and insert our CAPS event there.
1426 * There should be a SEGMENT event already, which is > CAPS */
1427 for (l = parse->priv->pending_events; l; l = l->next) {
1428 GstEvent *e = l->data;
1430 if (GST_EVENT_TYPE (e) < GST_EVENT_CAPS) {
1431 parse->priv->pending_events =
1432 g_list_insert_before (parse->priv->pending_events, l,
1437 /* No pending event that is < CAPS, so we have to add it at the very
1438 * end of the list */
1440 parse->priv->pending_events =
1441 g_list_append (parse->priv->pending_events, caps_event);
1443 gst_caps_unref (default_caps);
1445 gst_event_unref (event);
1448 GST_ELEMENT_ERROR (parse, STREAM, FORMAT, (NULL),
1449 ("Parser output not negotiated before GAP event."));
1454 gst_base_parse_push_pending_events (parse);
1456 if (parse->segment.rate > 0.0)
1457 gst_base_parse_drain (parse);
1459 gst_base_parse_finish_fragment (parse, TRUE);
1460 forward_immediate = TRUE;
1461 parse->priv->saw_gaps = TRUE;
1466 GstTagList *tags = NULL;
1468 gst_event_parse_tag (event, &tags);
1470 /* We only care about stream tags here, global tags we just forward */
1471 if (gst_tag_list_get_scope (tags) != GST_TAG_SCOPE_STREAM)
1474 gst_base_parse_set_upstream_tags (parse, tags);
1475 gst_base_parse_queue_tag_event_update (parse);
1476 parse->priv->tags_changed = FALSE;
1477 gst_event_unref (event);
1482 case GST_EVENT_STREAM_START:
1484 if (parse->priv->pad_mode != GST_PAD_MODE_PULL)
1485 forward_immediate = TRUE;
1487 gst_base_parse_set_upstream_tags (parse, NULL);
1488 parse->priv->tags_changed = TRUE;
1495 /* Forward non-serialized events and EOS/FLUSH_STOP immediately.
1496 * For EOS this is required because no buffer or serialized event
1497 * will come after EOS and nothing could trigger another
1498 * _finish_frame() call. *
1499 * If the subclass handles sending of EOS manually it can return
1500 * _DROPPED from ::finish() and all other subclasses should have
1501 * decoded/flushed all remaining data before this
1503 * For FLUSH_STOP this is required because it is expected
1504 * to be forwarded immediately and no buffers are queued anyway.
1507 if (!GST_EVENT_IS_SERIALIZED (event) || forward_immediate) {
1508 ret = gst_pad_push_event (parse->srcpad, event);
1510 parse->priv->pending_events =
1511 g_list_prepend (parse->priv->pending_events, event);
1516 GST_DEBUG_OBJECT (parse, "event handled");
1522 gst_base_parse_sink_query_default (GstBaseParse * parse, GstQuery * query)
1527 pad = GST_BASE_PARSE_SINK_PAD (parse);
1529 switch (GST_QUERY_TYPE (query)) {
1530 case GST_QUERY_CAPS:
1532 GstBaseParseClass *bclass;
1534 bclass = GST_BASE_PARSE_GET_CLASS (parse);
1536 if (bclass->get_sink_caps) {
1537 GstCaps *caps, *filter;
1539 gst_query_parse_caps (query, &filter);
1540 caps = bclass->get_sink_caps (parse, filter);
1541 GST_LOG_OBJECT (parse, "sink getcaps returning caps %" GST_PTR_FORMAT,
1543 gst_query_set_caps_result (query, caps);
1544 gst_caps_unref (caps);
1548 GstCaps *caps, *template_caps, *filter;
1550 gst_query_parse_caps (query, &filter);
1551 template_caps = gst_pad_get_pad_template_caps (pad);
1552 if (filter != NULL) {
1554 gst_caps_intersect_full (filter, template_caps,
1555 GST_CAPS_INTERSECT_FIRST);
1556 gst_caps_unref (template_caps);
1558 caps = template_caps;
1560 gst_query_set_caps_result (query, caps);
1561 gst_caps_unref (caps);
1569 res = gst_pad_query_default (pad, GST_OBJECT_CAST (parse), query);
1578 gst_base_parse_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
1580 GstBaseParseClass *bclass;
1581 GstBaseParse *parse;
1584 parse = GST_BASE_PARSE (parent);
1585 bclass = GST_BASE_PARSE_GET_CLASS (parse);
1587 GST_DEBUG_OBJECT (parse, "%s query", GST_QUERY_TYPE_NAME (query));
1589 if (bclass->sink_query)
1590 ret = bclass->sink_query (parse, query);
1594 GST_LOG_OBJECT (parse, "%s query result: %d %" GST_PTR_FORMAT,
1595 GST_QUERY_TYPE_NAME (query), ret, query);
1601 gst_base_parse_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1603 GstBaseParseClass *bclass;
1604 GstBaseParse *parse;
1607 parse = GST_BASE_PARSE (parent);
1608 bclass = GST_BASE_PARSE_GET_CLASS (parse);
1610 GST_DEBUG_OBJECT (parse, "%s query: %" GST_PTR_FORMAT,
1611 GST_QUERY_TYPE_NAME (query), query);
1613 if (bclass->src_query)
1614 ret = bclass->src_query (parse, query);
1618 GST_LOG_OBJECT (parse, "%s query result: %d %" GST_PTR_FORMAT,
1619 GST_QUERY_TYPE_NAME (query), ret, query);
1624 /* gst_base_parse_src_event:
1625 * @pad: #GstPad that received the event.
1626 * @event: #GstEvent that was received.
1628 * Handler for source pad events.
1630 * Returns: %TRUE if the event was handled.
1633 gst_base_parse_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1635 GstBaseParse *parse;
1636 GstBaseParseClass *bclass;
1637 gboolean ret = TRUE;
1639 parse = GST_BASE_PARSE (parent);
1640 bclass = GST_BASE_PARSE_GET_CLASS (parse);
1642 GST_DEBUG_OBJECT (parse, "event %d, %s", GST_EVENT_TYPE (event),
1643 GST_EVENT_TYPE_NAME (event));
1645 if (bclass->src_event)
1646 ret = bclass->src_event (parse, event);
1648 gst_event_unref (event);
1654 gst_base_parse_is_seekable (GstBaseParse * parse)
1656 /* FIXME: could do more here, e.g. check index or just send data from 0
1657 * in pull mode and let decoder/sink clip */
1658 return parse->priv->syncable;
1661 /* gst_base_parse_src_event_default:
1662 * @parse: #GstBaseParse.
1663 * @event: #GstEvent that was received.
1665 * Default srcpad event handler.
1667 * Returns: %TRUE if the event was handled and can be dropped.
1670 gst_base_parse_src_event_default (GstBaseParse * parse, GstEvent * event)
1672 gboolean res = FALSE;
1674 switch (GST_EVENT_TYPE (event)) {
1675 case GST_EVENT_SEEK:
1676 if (gst_base_parse_is_seekable (parse))
1677 res = gst_base_parse_handle_seek (parse, event);
1680 res = gst_pad_event_default (parse->srcpad, GST_OBJECT_CAST (parse),
1689 * gst_base_parse_convert_default:
1690 * @parse: #GstBaseParse.
1691 * @src_format: #GstFormat describing the source format.
1692 * @src_value: Source value to be converted.
1693 * @dest_format: #GstFormat defining the converted format.
1694 * @dest_value: (out): Pointer where the conversion result will be put.
1696 * Default implementation of #GstBaseParseClass.convert().
1698 * Returns: %TRUE if conversion was successful.
1701 gst_base_parse_convert_default (GstBaseParse * parse,
1702 GstFormat src_format,
1703 gint64 src_value, GstFormat dest_format, gint64 * dest_value)
1705 gboolean ret = FALSE;
1706 guint64 bytes, duration;
1708 if (G_UNLIKELY (src_format == dest_format)) {
1709 *dest_value = src_value;
1713 if (G_UNLIKELY (src_value == -1)) {
1718 if (G_UNLIKELY (src_value == 0)) {
1723 if (parse->priv->upstream_format != GST_FORMAT_BYTES) {
1724 /* don't do byte format conversions if we're not really parsing
1725 * a raw elementary stream, since we don't really have BYTES
1726 * position / duration info */
1727 if (src_format == GST_FORMAT_BYTES || dest_format == GST_FORMAT_BYTES)
1728 goto no_slaved_conversions;
1731 /* need at least some frames */
1732 if (!parse->priv->framecount)
1735 duration = parse->priv->acc_duration / GST_MSECOND;
1736 bytes = parse->priv->bytecount;
1738 if (G_UNLIKELY (!duration || !bytes))
1739 goto no_duration_bytes;
1741 if (src_format == GST_FORMAT_BYTES) {
1742 if (dest_format == GST_FORMAT_TIME) {
1743 /* BYTES -> TIME conversion */
1744 GST_DEBUG_OBJECT (parse, "converting bytes -> time");
1745 *dest_value = gst_util_uint64_scale (src_value, duration, bytes);
1746 *dest_value *= GST_MSECOND;
1747 GST_DEBUG_OBJECT (parse, "conversion result: %" G_GINT64_FORMAT " ms",
1748 *dest_value / GST_MSECOND);
1751 GST_DEBUG_OBJECT (parse, "converting bytes -> other not implemented");
1753 } else if (src_format == GST_FORMAT_TIME) {
1754 if (dest_format == GST_FORMAT_BYTES) {
1755 GST_DEBUG_OBJECT (parse, "converting time -> bytes");
1756 *dest_value = gst_util_uint64_scale (src_value / GST_MSECOND, bytes,
1758 GST_DEBUG_OBJECT (parse,
1759 "time %" G_GINT64_FORMAT " ms in bytes = %" G_GINT64_FORMAT,
1760 src_value / GST_MSECOND, *dest_value);
1763 GST_DEBUG_OBJECT (parse, "converting time -> other not implemented");
1765 } else if (src_format == GST_FORMAT_DEFAULT) {
1766 /* DEFAULT == frame-based */
1767 if (dest_format == GST_FORMAT_TIME) {
1768 GST_DEBUG_OBJECT (parse, "converting default -> time");
1769 if (parse->priv->fps_den) {
1770 *dest_value = gst_util_uint64_scale (src_value,
1771 GST_SECOND * parse->priv->fps_den, parse->priv->fps_num);
1775 GST_DEBUG_OBJECT (parse, "converting default -> other not implemented");
1778 GST_DEBUG_OBJECT (parse, "conversion not implemented");
1785 GST_DEBUG_OBJECT (parse, "no framecount");
1790 GST_DEBUG_OBJECT (parse, "no duration %" G_GUINT64_FORMAT ", bytes %"
1791 G_GUINT64_FORMAT, duration, bytes);
1794 no_slaved_conversions:
1796 GST_DEBUG_OBJECT (parse,
1797 "Can't do format conversions when upstream format is not BYTES");
1803 gst_base_parse_update_duration (GstBaseParse * parse)
1805 gint64 ptot, dest_value;
1807 if (!gst_pad_peer_query_duration (parse->sinkpad, GST_FORMAT_BYTES, &ptot))
1810 if (!gst_base_parse_convert (parse, GST_FORMAT_BYTES, ptot,
1811 GST_FORMAT_TIME, &dest_value))
1814 /* inform if duration changed, but try to avoid spamming */
1815 parse->priv->estimated_drift += dest_value - parse->priv->estimated_duration;
1817 parse->priv->estimated_duration = dest_value;
1818 GST_LOG_OBJECT (parse,
1819 "updated estimated duration to %" GST_TIME_FORMAT,
1820 GST_TIME_ARGS (dest_value));
1822 if (parse->priv->estimated_drift > GST_SECOND ||
1823 parse->priv->estimated_drift < -GST_SECOND) {
1824 gst_element_post_message (GST_ELEMENT (parse),
1825 gst_message_new_duration_changed (GST_OBJECT (parse)));
1826 parse->priv->estimated_drift = 0;
1830 /* gst_base_parse_update_bitrates:
1831 * @parse: #GstBaseParse.
1832 * @buffer: Current frame as a #GstBuffer
1834 * Keeps track of the minimum and maximum bitrates, and also maintains a
1835 * running average bitrate of the stream so far.
1838 gst_base_parse_update_bitrates (GstBaseParse * parse, GstBaseParseFrame * frame)
1840 guint64 data_len, frame_dur;
1842 guint frame_bitrate;
1843 guint64 frame_bitrate64;
1844 GstBuffer *buffer = frame->buffer;
1846 overhead = frame->overhead;
1850 data_len = gst_buffer_get_size (buffer) - overhead;
1851 parse->priv->data_bytecount += data_len;
1853 /* duration should be valid by now,
1854 * either set by subclass or maybe based on fps settings */
1855 if (GST_BUFFER_DURATION_IS_VALID (buffer) && parse->priv->acc_duration != 0) {
1856 guint64 avg_bitrate;
1858 /* Calculate duration of a frame from buffer properties */
1859 frame_dur = GST_BUFFER_DURATION (buffer);
1860 avg_bitrate = gst_util_uint64_scale (GST_SECOND,
1861 8 * parse->priv->data_bytecount, parse->priv->acc_duration);
1863 if (avg_bitrate > G_MAXUINT)
1866 parse->priv->avg_bitrate = avg_bitrate;
1868 /* No way to figure out frame duration (is this even possible?) */
1872 /* override if subclass provided bitrate, e.g. metadata based */
1873 if (parse->priv->bitrate) {
1874 parse->priv->avg_bitrate = parse->priv->bitrate;
1875 /* spread this (confirmed) info ASAP */
1876 if (parse->priv->post_avg_bitrate &&
1877 parse->priv->posted_avg_bitrate != parse->priv->avg_bitrate)
1878 parse->priv->tags_changed = TRUE;
1884 frame_bitrate64 = gst_util_uint64_scale (GST_SECOND, 8 * data_len, frame_dur);
1886 if (frame_bitrate64 > G_MAXUINT)
1889 frame_bitrate = (guint) frame_bitrate64;
1891 GST_LOG_OBJECT (parse, "frame bitrate %u, avg bitrate %u", frame_bitrate,
1892 parse->priv->avg_bitrate);
1894 if (parse->priv->framecount < MIN_FRAMES_TO_POST_BITRATE)
1897 if (parse->priv->framecount == MIN_FRAMES_TO_POST_BITRATE &&
1898 (parse->priv->post_min_bitrate || parse->priv->post_avg_bitrate
1899 || parse->priv->post_max_bitrate))
1900 parse->priv->tags_changed = TRUE;
1902 if (G_LIKELY (parse->priv->framecount >= MIN_FRAMES_TO_POST_BITRATE)) {
1903 if (frame_bitrate < parse->priv->min_bitrate) {
1904 parse->priv->min_bitrate = frame_bitrate;
1905 if (parse->priv->post_min_bitrate)
1906 parse->priv->tags_changed = TRUE;
1909 if (frame_bitrate > parse->priv->max_bitrate) {
1910 parse->priv->max_bitrate = frame_bitrate;
1911 if (parse->priv->post_max_bitrate)
1912 parse->priv->tags_changed = TRUE;
1915 /* Only update the tag on a 2% change */
1916 if (parse->priv->post_avg_bitrate && parse->priv->avg_bitrate) {
1917 guint64 diffprev = gst_util_uint64_scale_int (100,
1918 ABSDIFF (parse->priv->avg_bitrate, parse->priv->posted_avg_bitrate),
1919 parse->priv->avg_bitrate);
1920 if (diffprev >= UPDATE_THRESHOLD)
1921 parse->priv->tags_changed = TRUE;
1927 * gst_base_parse_add_index_entry:
1928 * @parse: #GstBaseParse.
1929 * @offset: offset of entry
1930 * @ts: timestamp associated with offset
1931 * @key: whether entry refers to keyframe
1932 * @force: add entry disregarding sanity checks
1934 * Adds an entry to the index associating @offset to @ts. It is recommended
1935 * to only add keyframe entries. @force allows to bypass checks, such as
1936 * whether the stream is (upstream) seekable, another entry is already "close"
1937 * to the new entry, etc.
1939 * Returns: #gboolean indicating whether entry was added
1942 gst_base_parse_add_index_entry (GstBaseParse * parse, guint64 offset,
1943 GstClockTime ts, gboolean key, gboolean force)
1945 gboolean ret = FALSE;
1946 GstIndexAssociation associations[2];
1948 GST_LOG_OBJECT (parse, "Adding key=%d index entry %" GST_TIME_FORMAT
1949 " @ offset 0x%08" G_GINT64_MODIFIER "x", key, GST_TIME_ARGS (ts), offset);
1951 if (G_LIKELY (!force)) {
1953 if (!parse->priv->upstream_seekable) {
1954 GST_DEBUG_OBJECT (parse, "upstream not seekable; discarding");
1958 /* FIXME need better helper data structure that handles these issues
1959 * related to ongoing collecting of index entries */
1960 if (parse->priv->index_last_offset + parse->priv->idx_byte_interval >=
1962 GST_LOG_OBJECT (parse,
1963 "already have entries up to offset 0x%08" G_GINT64_MODIFIER "x",
1964 parse->priv->index_last_offset + parse->priv->idx_byte_interval);
1968 if (GST_CLOCK_TIME_IS_VALID (parse->priv->index_last_ts) &&
1969 GST_CLOCK_DIFF (parse->priv->index_last_ts, ts) <
1970 parse->priv->idx_interval) {
1971 GST_LOG_OBJECT (parse, "entry too close to last time %" GST_TIME_FORMAT,
1972 GST_TIME_ARGS (parse->priv->index_last_ts));
1976 /* if last is not really the last one */
1977 if (!parse->priv->index_last_valid) {
1978 GstClockTime prev_ts;
1980 gst_base_parse_find_offset (parse, ts, TRUE, &prev_ts);
1981 if (GST_CLOCK_DIFF (prev_ts, ts) < parse->priv->idx_interval) {
1982 GST_LOG_OBJECT (parse,
1983 "entry too close to existing entry %" GST_TIME_FORMAT,
1984 GST_TIME_ARGS (prev_ts));
1985 parse->priv->index_last_offset = offset;
1986 parse->priv->index_last_ts = ts;
1992 associations[0].format = GST_FORMAT_TIME;
1993 associations[0].value = ts;
1994 associations[1].format = GST_FORMAT_BYTES;
1995 associations[1].value = offset;
1997 /* index might change on-the-fly, although that would be nutty app ... */
1998 GST_BASE_PARSE_INDEX_LOCK (parse);
1999 gst_index_add_associationv (parse->priv->index, parse->priv->index_id,
2000 (key) ? GST_INDEX_ASSOCIATION_FLAG_KEY_UNIT :
2001 GST_INDEX_ASSOCIATION_FLAG_DELTA_UNIT, 2,
2002 (const GstIndexAssociation *) &associations);
2003 GST_BASE_PARSE_INDEX_UNLOCK (parse);
2006 parse->priv->index_last_offset = offset;
2007 parse->priv->index_last_ts = ts;
2016 /* check for seekable upstream, above and beyond a mere query */
2018 gst_base_parse_check_seekability (GstBaseParse * parse)
2021 gboolean seekable = FALSE;
2022 gint64 start = -1, stop = -1;
2023 guint idx_interval = 0;
2024 guint64 idx_byte_interval = 0;
2026 query = gst_query_new_seeking (GST_FORMAT_BYTES);
2027 if (!gst_pad_peer_query (parse->sinkpad, query)) {
2028 GST_DEBUG_OBJECT (parse, "seeking query failed");
2032 gst_query_parse_seeking (query, NULL, &seekable, &start, &stop);
2034 /* try harder to query upstream size if we didn't get it the first time */
2035 if (seekable && stop == -1) {
2036 GST_DEBUG_OBJECT (parse, "doing duration query to fix up unset stop");
2037 gst_pad_peer_query_duration (parse->sinkpad, GST_FORMAT_BYTES, &stop);
2040 /* if upstream doesn't know the size, it's likely that it's not seekable in
2041 * practice even if it technically may be seekable */
2042 if (seekable && (start != 0 || stop <= start)) {
2043 GST_DEBUG_OBJECT (parse, "seekable but unknown start/stop -> disable");
2047 /* let's not put every single frame into our index */
2049 if (stop < 10 * 1024 * 1024)
2051 else if (stop < 100 * 1024 * 1024)
2054 idx_interval = 1000;
2056 /* ensure that even for large files (e.g. very long audio files), the index
2057 * stays reasonably-size, with some arbitrary limit to the total number of
2059 idx_byte_interval = (stop - start) / MAX_INDEX_ENTRIES;
2060 GST_DEBUG_OBJECT (parse,
2061 "Limiting index entries to %d, indexing byte interval %"
2062 G_GUINT64_FORMAT " bytes", MAX_INDEX_ENTRIES, idx_byte_interval);
2066 gst_query_unref (query);
2068 GST_DEBUG_OBJECT (parse, "seekable: %d (%" G_GUINT64_FORMAT " - %"
2069 G_GUINT64_FORMAT ")", seekable, start, stop);
2070 parse->priv->upstream_seekable = seekable;
2071 parse->priv->upstream_size = seekable ? stop : 0;
2073 GST_DEBUG_OBJECT (parse, "idx_interval: %ums", idx_interval);
2074 parse->priv->idx_interval = idx_interval * GST_MSECOND;
2075 parse->priv->idx_byte_interval = idx_byte_interval;
2078 /* some misc checks on upstream */
2080 gst_base_parse_check_upstream (GstBaseParse * parse)
2084 if (gst_pad_peer_query_duration (parse->sinkpad, GST_FORMAT_TIME, &stop))
2085 if (GST_CLOCK_TIME_IS_VALID (stop) && stop) {
2086 /* upstream has one, accept it also, and no further updates */
2087 gst_base_parse_set_duration (parse, GST_FORMAT_TIME, stop, 0);
2088 parse->priv->upstream_has_duration = TRUE;
2091 GST_DEBUG_OBJECT (parse, "upstream_has_duration: %d",
2092 parse->priv->upstream_has_duration);
2095 /* checks src caps to determine if dealing with audio or video */
2096 /* TODO maybe forego automagic stuff and let subclass configure it ? */
2098 gst_base_parse_check_media (GstBaseParse * parse)
2103 caps = gst_pad_get_current_caps (parse->srcpad);
2104 if (G_LIKELY (caps) && (s = gst_caps_get_structure (caps, 0))) {
2105 parse->priv->is_video =
2106 g_str_has_prefix (gst_structure_get_name (s), "video");
2108 /* historical default */
2109 parse->priv->is_video = FALSE;
2112 gst_caps_unref (caps);
2114 parse->priv->checked_media = TRUE;
2115 GST_DEBUG_OBJECT (parse, "media is video: %d", parse->priv->is_video);
2118 /* takes ownership of frame */
2120 gst_base_parse_queue_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
2122 if (!(frame->_private_flags & GST_BASE_PARSE_FRAME_PRIVATE_FLAG_NOALLOC)) {
2123 /* frame allocated on the heap, we can just take ownership */
2124 g_queue_push_tail (&parse->priv->queued_frames, frame);
2125 GST_TRACE ("queued frame %p", frame);
2127 GstBaseParseFrame *copy;
2129 /* probably allocated on the stack, must make a proper copy */
2130 copy = gst_base_parse_frame_copy (frame);
2131 g_queue_push_tail (&parse->priv->queued_frames, copy);
2132 GST_TRACE ("queued frame %p (copy of %p)", copy, frame);
2133 gst_base_parse_frame_free (frame);
2137 /* makes sure that @buf is properly prepared and decorated for passing
2138 * to baseclass, and an equally setup frame is returned setup with @buf.
2139 * Takes ownership of @buf. */
2140 static GstBaseParseFrame *
2141 gst_base_parse_prepare_frame (GstBaseParse * parse, GstBuffer * buffer)
2143 GstBaseParseFrame *frame = NULL;
2145 buffer = gst_buffer_make_writable (buffer);
2147 GST_LOG_OBJECT (parse,
2148 "preparing frame at offset %" G_GUINT64_FORMAT
2149 " (%#" G_GINT64_MODIFIER "x) of size %" G_GSIZE_FORMAT,
2150 GST_BUFFER_OFFSET (buffer), GST_BUFFER_OFFSET (buffer),
2151 gst_buffer_get_size (buffer));
2153 GST_BUFFER_OFFSET (buffer) = parse->priv->offset;
2155 gst_base_parse_update_flags (parse);
2157 frame = gst_base_parse_frame_new (buffer, 0, 0);
2158 gst_buffer_unref (buffer);
2159 gst_base_parse_update_frame (parse, frame);
2161 /* clear flags for next frame */
2162 parse->priv->discont = FALSE;
2163 parse->priv->new_frame = FALSE;
2165 /* use default handler to provide initial (upstream) metadata */
2166 gst_base_parse_parse_frame (parse, frame);
2171 /* Wraps buffer in a frame and dispatches to subclass.
2172 * Also manages data skipping and offset handling (including adapter flushing).
2173 * Takes ownership of @buffer */
2174 static GstFlowReturn
2175 gst_base_parse_handle_buffer (GstBaseParse * parse, GstBuffer * buffer,
2176 gint * skip, gint * flushed)
2178 GstBaseParseClass *klass = GST_BASE_PARSE_GET_CLASS (parse);
2179 GstBaseParseFrame *frame;
2182 g_return_val_if_fail (skip != NULL || flushed != NULL, GST_FLOW_ERROR);
2184 GST_LOG_OBJECT (parse,
2185 "handling buffer of size %" G_GSIZE_FORMAT " with dts %" GST_TIME_FORMAT
2186 ", pts %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT,
2187 gst_buffer_get_size (buffer), GST_TIME_ARGS (GST_BUFFER_DTS (buffer)),
2188 GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
2189 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
2191 /* track what is being flushed during this single round of frame processing */
2192 parse->priv->flushed = 0;
2195 /* make it easy for _finish_frame to pick up input data */
2196 if (parse->priv->pad_mode == GST_PAD_MODE_PULL) {
2197 gst_buffer_ref (buffer);
2198 gst_adapter_push (parse->priv->adapter, buffer);
2201 frame = gst_base_parse_prepare_frame (parse, buffer);
2202 ret = klass->handle_frame (parse, frame, skip);
2204 *flushed = parse->priv->flushed;
2206 GST_LOG_OBJECT (parse, "handle_frame skipped %d, flushed %d",
2209 /* subclass can only do one of these, or semantics are too unclear */
2210 g_assert (*skip == 0 || *flushed == 0);
2212 /* track skipping */
2214 GstClockTime pts, dts;
2217 GST_LOG_OBJECT (parse, "finding sync, skipping %d bytes", *skip);
2218 if (parse->segment.rate < 0.0 && !parse->priv->buffers_queued) {
2219 /* reverse playback, and no frames found yet, so we are skipping
2220 * the leading part of a fragment, which may form the tail of
2221 * fragment coming later, hopefully subclass skips efficiently ... */
2222 pts = gst_adapter_prev_pts (parse->priv->adapter, NULL);
2223 dts = gst_adapter_prev_dts (parse->priv->adapter, NULL);
2224 outbuf = gst_adapter_take_buffer (parse->priv->adapter, *skip);
2225 outbuf = gst_buffer_make_writable (outbuf);
2226 GST_BUFFER_PTS (outbuf) = pts;
2227 GST_BUFFER_DTS (outbuf) = dts;
2228 parse->priv->buffers_head =
2229 g_slist_prepend (parse->priv->buffers_head, outbuf);
2232 /* If we're asked to skip more than is available in the adapter,
2233 we need to remember what we need to skip for next iteration */
2234 gsize av = gst_adapter_available (parse->priv->adapter);
2235 GST_DEBUG ("Asked to skip %u (%" G_GSIZE_FORMAT " available)", *skip, av);
2237 gst_adapter_flush (parse->priv->adapter, *skip);
2240 ("This is more than available, flushing %" G_GSIZE_FORMAT
2241 ", storing %u to skip", av, (guint) (*skip - av));
2242 parse->priv->skip = *skip - av;
2243 gst_adapter_flush (parse->priv->adapter, av);
2247 if (!parse->priv->discont)
2248 parse->priv->sync_offset = parse->priv->offset;
2249 parse->priv->offset += *skip;
2250 parse->priv->discont = TRUE;
2251 /* check for indefinite skipping */
2252 if (ret == GST_FLOW_OK)
2253 ret = gst_base_parse_check_sync (parse);
2256 parse->priv->offset += *flushed;
2258 if (parse->priv->pad_mode == GST_PAD_MODE_PULL) {
2259 gst_adapter_clear (parse->priv->adapter);
2262 if (*skip == 0 && *flushed == 0) {
2263 /* Carry over discont if we need more data */
2264 if (GST_BUFFER_IS_DISCONT (frame->buffer))
2265 parse->priv->discont = TRUE;
2268 gst_base_parse_frame_free (frame);
2273 /* gst_base_parse_push_pending_events:
2274 * @parse: #GstBaseParse
2276 * Pushes the pending events
2279 gst_base_parse_push_pending_events (GstBaseParse * parse)
2281 if (G_UNLIKELY (parse->priv->pending_events)) {
2282 GList *r = g_list_reverse (parse->priv->pending_events);
2285 parse->priv->pending_events = NULL;
2286 for (l = r; l != NULL; l = l->next) {
2287 gst_pad_push_event (parse->srcpad, GST_EVENT_CAST (l->data));
2293 /* gst_base_parse_handle_and_push_frame:
2294 * @parse: #GstBaseParse.
2295 * @klass: #GstBaseParseClass.
2296 * @frame: (transfer full): a #GstBaseParseFrame
2298 * Parses the frame from given buffer and pushes it forward. Also performs
2299 * timestamp handling and checks the segment limits.
2301 * This is called with srcpad STREAM_LOCK held.
2303 * Returns: #GstFlowReturn
2305 static GstFlowReturn
2306 gst_base_parse_handle_and_push_frame (GstBaseParse * parse,
2307 GstBaseParseFrame * frame)
2312 g_return_val_if_fail (frame != NULL, GST_FLOW_ERROR);
2314 buffer = frame->buffer;
2315 offset = frame->offset;
2317 /* check if subclass/format can provide ts.
2318 * If so, that allows and enables extra seek and duration determining options */
2319 if (G_UNLIKELY (parse->priv->first_frame_offset < 0)) {
2320 if (GST_BUFFER_PTS_IS_VALID (buffer) && parse->priv->has_timing_info
2321 && parse->priv->pad_mode == GST_PAD_MODE_PULL) {
2322 parse->priv->first_frame_offset = offset;
2323 parse->priv->first_frame_pts = GST_BUFFER_PTS (buffer);
2324 parse->priv->first_frame_dts = GST_BUFFER_DTS (buffer);
2325 GST_DEBUG_OBJECT (parse, "subclass provided dts %" GST_TIME_FORMAT
2326 ", pts %" GST_TIME_FORMAT " for first frame at offset %"
2327 G_GINT64_FORMAT, GST_TIME_ARGS (parse->priv->first_frame_dts),
2328 GST_TIME_ARGS (parse->priv->first_frame_pts),
2329 parse->priv->first_frame_offset);
2330 if (!GST_CLOCK_TIME_IS_VALID (parse->priv->duration)) {
2332 GstClockTime last_ts = G_MAXINT64;
2334 GST_DEBUG_OBJECT (parse, "no duration; trying scan to determine");
2335 gst_base_parse_locate_time (parse, &last_ts, &off);
2336 if (GST_CLOCK_TIME_IS_VALID (last_ts))
2337 gst_base_parse_set_duration (parse, GST_FORMAT_TIME, last_ts, 0);
2340 /* disable further checks */
2341 parse->priv->first_frame_offset = 0;
2345 /* track upstream time if provided, not subclass' internal notion of it */
2346 if (parse->priv->upstream_format == GST_FORMAT_TIME) {
2347 GST_BUFFER_PTS (frame->buffer) = GST_CLOCK_TIME_NONE;
2348 GST_BUFFER_DTS (frame->buffer) = GST_CLOCK_TIME_NONE;
2351 /* interpolating and no valid pts yet,
2352 * start with dts and carry on from there */
2353 if (parse->priv->infer_ts && parse->priv->pts_interpolate
2354 && !GST_CLOCK_TIME_IS_VALID (parse->priv->next_pts))
2355 parse->priv->next_pts = parse->priv->next_dts;
2357 /* again use default handler to add missing metadata;
2358 * we may have new information on frame properties */
2359 gst_base_parse_parse_frame (parse, frame);
2361 parse->priv->next_pts = GST_CLOCK_TIME_NONE;
2362 if (GST_BUFFER_DTS_IS_VALID (buffer) && GST_BUFFER_DURATION_IS_VALID (buffer)) {
2363 parse->priv->next_dts =
2364 GST_BUFFER_DTS (buffer) + GST_BUFFER_DURATION (buffer);
2365 if (parse->priv->pts_interpolate && GST_BUFFER_PTS_IS_VALID (buffer)) {
2366 GstClockTime next_pts =
2367 GST_BUFFER_PTS (buffer) + GST_BUFFER_DURATION (buffer);
2368 if (next_pts >= parse->priv->next_dts)
2369 parse->priv->next_pts = next_pts;
2372 /* we lost track, do not produce bogus time next time around
2373 * (probably means parser subclass has given up on parsing as well) */
2374 GST_DEBUG_OBJECT (parse, "no next fallback timestamp");
2375 parse->priv->next_dts = GST_CLOCK_TIME_NONE;
2378 if (parse->priv->upstream_seekable && parse->priv->exact_position &&
2379 GST_BUFFER_PTS_IS_VALID (buffer))
2380 gst_base_parse_add_index_entry (parse, offset,
2381 GST_BUFFER_PTS (buffer),
2382 !GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT), FALSE);
2384 /* All OK, push queued frames if there are any */
2385 if (G_UNLIKELY (!g_queue_is_empty (&parse->priv->queued_frames))) {
2386 GstBaseParseFrame *queued_frame;
2388 while ((queued_frame = g_queue_pop_head (&parse->priv->queued_frames))) {
2389 gst_base_parse_push_frame (parse, queued_frame);
2390 gst_base_parse_frame_free (queued_frame);
2394 return gst_base_parse_push_frame (parse, frame);
2398 * gst_base_parse_push_frame:
2399 * @parse: #GstBaseParse.
2400 * @frame: (transfer none): a #GstBaseParseFrame
2402 * Pushes the frame's buffer downstream, sends any pending events and
2403 * does some timestamp and segment handling. Takes ownership of
2404 * frame's buffer, though caller retains ownership of @frame.
2406 * This must be called with sinkpad STREAM_LOCK held.
2408 * Returns: #GstFlowReturn
2411 gst_base_parse_push_frame (GstBaseParse * parse, GstBaseParseFrame * frame)
2413 GstFlowReturn ret = GST_FLOW_OK;
2414 GstClockTime last_start = GST_CLOCK_TIME_NONE;
2415 GstClockTime last_stop = GST_CLOCK_TIME_NONE;
2416 GstBaseParseClass *klass = GST_BASE_PARSE_GET_CLASS (parse);
2420 g_return_val_if_fail (frame != NULL, GST_FLOW_ERROR);
2421 g_return_val_if_fail (frame->buffer != NULL, GST_FLOW_ERROR);
2423 GST_TRACE_OBJECT (parse, "pushing frame %p", frame);
2425 buffer = frame->buffer;
2427 GST_LOG_OBJECT (parse,
2428 "processing buffer of size %" G_GSIZE_FORMAT " with dts %" GST_TIME_FORMAT
2429 ", pts %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT,
2430 gst_buffer_get_size (buffer),
2431 GST_TIME_ARGS (GST_BUFFER_DTS (buffer)),
2432 GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
2433 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
2436 parse->priv->bytecount += frame->size;
2437 if (G_LIKELY (!(frame->flags & GST_BASE_PARSE_FRAME_FLAG_NO_FRAME))) {
2438 parse->priv->framecount++;
2439 if (GST_BUFFER_DURATION_IS_VALID (buffer)) {
2440 parse->priv->acc_duration += GST_BUFFER_DURATION (buffer);
2443 /* 0 means disabled */
2444 if (parse->priv->update_interval < 0)
2445 parse->priv->update_interval = 50;
2446 else if (parse->priv->update_interval > 0 &&
2447 (parse->priv->framecount % parse->priv->update_interval) == 0)
2448 gst_base_parse_update_duration (parse);
2450 if (GST_BUFFER_PTS_IS_VALID (buffer))
2451 last_start = last_stop = GST_BUFFER_PTS (buffer);
2452 if (last_start != GST_CLOCK_TIME_NONE
2453 && GST_BUFFER_DURATION_IS_VALID (buffer))
2454 last_stop = last_start + GST_BUFFER_DURATION (buffer);
2456 /* should have caps by now */
2457 if (!gst_pad_has_current_caps (parse->srcpad))
2460 if (G_UNLIKELY (!parse->priv->checked_media)) {
2461 /* have caps; check identity */
2462 gst_base_parse_check_media (parse);
2465 if (parse->priv->tags_changed) {
2466 gst_base_parse_queue_tag_event_update (parse);
2467 parse->priv->tags_changed = FALSE;
2470 /* Push pending events, including SEGMENT events */
2471 gst_base_parse_push_pending_events (parse);
2473 /* segment adjustment magic; only if we are running the whole show */
2474 if (!parse->priv->passthrough && parse->segment.rate > 0.0 &&
2475 (parse->priv->pad_mode == GST_PAD_MODE_PULL ||
2476 parse->priv->upstream_seekable)) {
2478 if (GST_CLOCK_TIME_IS_VALID (parse->segment.position) &&
2479 GST_CLOCK_TIME_IS_VALID (last_start)) {
2480 GstClockTimeDiff diff;
2482 /* only send newsegments with increasing start times,
2483 * otherwise if these go back and forth downstream (sinks) increase
2484 * accumulated time and running_time */
2485 diff = GST_CLOCK_DIFF (parse->segment.position, last_start);
2486 if (G_UNLIKELY (diff > 2 * GST_SECOND
2487 && last_start > parse->segment.start
2488 && (!GST_CLOCK_TIME_IS_VALID (parse->segment.stop)
2489 || last_start < parse->segment.stop))) {
2492 GST_DEBUG_OBJECT (parse,
2493 "Gap of %" G_GINT64_FORMAT " ns detected in stream " "(%"
2494 GST_TIME_FORMAT " -> %" GST_TIME_FORMAT "). "
2495 "Sending updated SEGMENT events", diff,
2496 GST_TIME_ARGS (parse->segment.position),
2497 GST_TIME_ARGS (last_start));
2499 /* skip gap FIXME */
2500 topush = gst_event_new_segment (&parse->segment);
2501 if (parse->priv->segment_seqnum != GST_SEQNUM_INVALID)
2502 gst_event_set_seqnum (topush, parse->priv->segment_seqnum);
2503 gst_pad_push_event (parse->srcpad, topush);
2505 parse->segment.position = last_start;
2510 /* update bitrates and optionally post corresponding tags
2511 * (following newsegment) */
2512 gst_base_parse_update_bitrates (parse, frame);
2514 if (klass->pre_push_frame) {
2515 ret = klass->pre_push_frame (parse, frame);
2517 frame->flags |= GST_BASE_PARSE_FRAME_FLAG_CLIP;
2520 /* Push pending events, if there are any new ones
2521 * like tags added by pre_push_frame */
2522 if (parse->priv->tags_changed) {
2523 gst_base_parse_queue_tag_event_update (parse);
2524 parse->priv->tags_changed = FALSE;
2526 gst_base_parse_push_pending_events (parse);
2528 /* take final ownership of frame buffer */
2529 if (frame->out_buffer) {
2530 buffer = frame->out_buffer;
2531 frame->out_buffer = NULL;
2532 gst_buffer_replace (&frame->buffer, NULL);
2534 buffer = frame->buffer;
2535 frame->buffer = NULL;
2538 /* subclass must play nice */
2539 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
2541 size = gst_buffer_get_size (buffer);
2543 parse->priv->seen_keyframe |= parse->priv->is_video &&
2544 !GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
2546 if (frame->flags & GST_BASE_PARSE_FRAME_FLAG_CLIP) {
2547 if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
2548 GST_CLOCK_TIME_IS_VALID (parse->segment.stop) &&
2549 GST_BUFFER_TIMESTAMP (buffer) >
2550 parse->segment.stop + parse->priv->lead_out_ts) {
2551 GST_LOG_OBJECT (parse, "Dropped frame, after segment");
2553 } else if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer) &&
2554 GST_BUFFER_DURATION_IS_VALID (buffer) &&
2555 GST_CLOCK_TIME_IS_VALID (parse->segment.start) &&
2556 GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer) +
2557 parse->priv->lead_in_ts < parse->segment.start) {
2558 if (parse->priv->seen_keyframe) {
2559 GST_LOG_OBJECT (parse, "Frame before segment, after keyframe");
2562 GST_LOG_OBJECT (parse, "Dropped frame, before segment");
2563 ret = GST_BASE_PARSE_FLOW_DROPPED;
2570 if (ret == GST_BASE_PARSE_FLOW_DROPPED) {
2571 GST_LOG_OBJECT (parse, "frame (%" G_GSIZE_FORMAT " bytes) dropped", size);
2572 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT))
2573 parse->priv->discont = TRUE;
2574 gst_buffer_unref (buffer);
2576 } else if (ret == GST_FLOW_OK) {
2577 if (parse->segment.rate > 0.0) {
2578 GST_LOG_OBJECT (parse, "pushing frame (%" G_GSIZE_FORMAT " bytes) now..",
2580 ret = gst_pad_push (parse->srcpad, buffer);
2581 GST_LOG_OBJECT (parse, "frame pushed, flow %s", gst_flow_get_name (ret));
2582 } else if (!parse->priv->disable_passthrough && parse->priv->passthrough) {
2584 /* in backwards playback mode, if on passthrough we need to push buffers
2585 * directly without accumulating them into the buffers_queued as baseparse
2586 * will never check for a DISCONT while on passthrough and those buffers
2587 * will never be pushed.
2589 * also, as we are on reverse playback, it might be possible that
2590 * passthrough might have just been enabled, so make sure to drain the
2591 * buffers_queued list */
2592 if (G_UNLIKELY (parse->priv->buffers_queued != NULL)) {
2593 gst_base_parse_finish_fragment (parse, TRUE);
2594 ret = gst_base_parse_send_buffers (parse);
2597 if (ret == GST_FLOW_OK) {
2598 GST_LOG_OBJECT (parse,
2599 "pushing frame (%" G_GSIZE_FORMAT " bytes) now..", size);
2600 ret = gst_pad_push (parse->srcpad, buffer);
2601 GST_LOG_OBJECT (parse, "frame pushed, flow %s",
2602 gst_flow_get_name (ret));
2604 GST_LOG_OBJECT (parse,
2605 "frame (%" G_GSIZE_FORMAT " bytes) not pushed: %s", size,
2606 gst_flow_get_name (ret));
2607 gst_buffer_unref (buffer);
2611 GST_LOG_OBJECT (parse, "frame (%" G_GSIZE_FORMAT " bytes) queued for now",
2613 parse->priv->buffers_queued =
2614 g_slist_prepend (parse->priv->buffers_queued, buffer);
2618 GST_LOG_OBJECT (parse, "frame (%" G_GSIZE_FORMAT " bytes) not pushed: %s",
2619 size, gst_flow_get_name (ret));
2620 gst_buffer_unref (buffer);
2621 /* if we are not sufficiently in control, let upstream decide on EOS */
2622 if (ret == GST_FLOW_EOS && !parse->priv->disable_passthrough &&
2623 (parse->priv->passthrough ||
2624 (parse->priv->pad_mode == GST_PAD_MODE_PUSH &&
2625 !parse->priv->upstream_seekable)))
2629 /* Update current running segment position */
2630 if ((ret == GST_FLOW_OK || ret == GST_FLOW_NOT_LINKED)
2631 && last_stop != GST_CLOCK_TIME_NONE
2632 && parse->segment.position < last_stop)
2633 parse->segment.position = last_stop;
2640 if (GST_PAD_IS_FLUSHING (parse->srcpad))
2641 return GST_FLOW_FLUSHING;
2643 GST_ELEMENT_ERROR (parse, STREAM, DECODE, ("No caps set"), (NULL));
2644 return GST_FLOW_ERROR;
2649 * gst_base_parse_finish_frame:
2650 * @parse: a #GstBaseParse
2651 * @frame: a #GstBaseParseFrame
2652 * @size: consumed input data represented by frame
2654 * Collects parsed data and pushes this downstream.
2655 * Source pad caps must be set when this is called.
2657 * If @frame's out_buffer is set, that will be used as subsequent frame data.
2658 * Otherwise, @size samples will be taken from the input and used for output,
2659 * and the output's metadata (timestamps etc) will be taken as (optionally)
2660 * set by the subclass on @frame's (input) buffer (which is otherwise
2661 * ignored for any but the above purpose/information).
2663 * Note that the latter buffer is invalidated by this call, whereas the
2664 * caller retains ownership of @frame.
2666 * Returns: a #GstFlowReturn that should be escalated to caller (of caller)
2669 gst_base_parse_finish_frame (GstBaseParse * parse, GstBaseParseFrame * frame,
2672 GstFlowReturn ret = GST_FLOW_OK;
2674 g_return_val_if_fail (frame != NULL, GST_FLOW_ERROR);
2675 g_return_val_if_fail (frame->buffer != NULL, GST_FLOW_ERROR);
2676 g_return_val_if_fail (size > 0 || frame->out_buffer, GST_FLOW_ERROR);
2677 g_return_val_if_fail (gst_adapter_available (parse->priv->adapter) >= size,
2680 GST_LOG_OBJECT (parse, "finished frame at offset %" G_GUINT64_FORMAT ", "
2681 "flushing size %d", frame->offset, size);
2683 /* some one-time start-up */
2684 if (G_UNLIKELY (parse->priv->framecount == 0)) {
2685 gst_base_parse_check_seekability (parse);
2686 gst_base_parse_check_upstream (parse);
2689 parse->priv->flushed += size;
2691 if (parse->priv->scanning && frame->buffer) {
2692 if (!parse->priv->scanned_frame) {
2693 parse->priv->scanned_frame = gst_base_parse_frame_copy (frame);
2698 /* either PUSH or PULL mode arranges for adapter data */
2699 /* ensure output buffer */
2700 if (!frame->out_buffer) {
2701 GstBuffer *src, *dest;
2703 frame->out_buffer = gst_adapter_take_buffer (parse->priv->adapter, size);
2704 dest = frame->out_buffer;
2705 src = frame->buffer;
2706 GST_BUFFER_PTS (dest) = GST_BUFFER_PTS (src);
2707 GST_BUFFER_DTS (dest) = GST_BUFFER_DTS (src);
2708 GST_BUFFER_OFFSET (dest) = GST_BUFFER_OFFSET (src);
2709 GST_BUFFER_DURATION (dest) = GST_BUFFER_DURATION (src);
2710 GST_BUFFER_OFFSET_END (dest) = GST_BUFFER_OFFSET_END (src);
2711 GST_MINI_OBJECT_FLAGS (dest) = GST_MINI_OBJECT_FLAGS (src);
2713 gst_adapter_flush (parse->priv->adapter, size);
2716 /* use as input for subsequent processing */
2717 gst_buffer_replace (&frame->buffer, frame->out_buffer);
2718 gst_buffer_unref (frame->out_buffer);
2719 frame->out_buffer = NULL;
2721 /* mark input size consumed */
2724 /* subclass might queue frames/data internally if it needs more
2725 * frames to decide on the format, or might request us to queue here. */
2726 if (frame->flags & GST_BASE_PARSE_FRAME_FLAG_DROP) {
2727 gst_buffer_replace (&frame->buffer, NULL);
2729 } else if (frame->flags & GST_BASE_PARSE_FRAME_FLAG_QUEUE) {
2730 GstBaseParseFrame *copy;
2732 copy = gst_base_parse_frame_copy (frame);
2733 copy->flags &= ~GST_BASE_PARSE_FRAME_FLAG_QUEUE;
2734 gst_base_parse_queue_frame (parse, copy);
2738 ret = gst_base_parse_handle_and_push_frame (parse, frame);
2745 * gst_base_parse_drain:
2746 * @parse: a #GstBaseParse
2748 * Drains the adapter until it is empty. It decreases the min_frame_size to
2749 * match the current adapter size and calls chain method until the adapter
2750 * is emptied or chain returns with error.
2755 gst_base_parse_drain (GstBaseParse * parse)
2759 GST_DEBUG_OBJECT (parse, "draining");
2760 parse->priv->drain = TRUE;
2763 avail = gst_adapter_available (parse->priv->adapter);
2767 if (gst_base_parse_chain (parse->sinkpad, GST_OBJECT_CAST (parse),
2768 NULL) != GST_FLOW_OK) {
2772 /* nothing changed, maybe due to truncated frame; break infinite loop */
2773 if (avail == gst_adapter_available (parse->priv->adapter)) {
2774 GST_DEBUG_OBJECT (parse, "no change during draining; flushing");
2775 gst_adapter_clear (parse->priv->adapter);
2779 parse->priv->drain = FALSE;
2782 /* gst_base_parse_send_buffers
2784 * Sends buffers collected in send_buffers downstream, and ensures that list
2785 * is empty at the end (errors or not).
2787 static GstFlowReturn
2788 gst_base_parse_send_buffers (GstBaseParse * parse)
2790 GSList *send = NULL;
2792 GstFlowReturn ret = GST_FLOW_OK;
2793 gboolean first = TRUE;
2795 send = parse->priv->buffers_send;
2799 buf = GST_BUFFER_CAST (send->data);
2800 GST_LOG_OBJECT (parse, "pushing buffer %p, dts %"
2801 GST_TIME_FORMAT ", pts %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT
2802 ", offset %" G_GINT64_FORMAT, buf,
2803 GST_TIME_ARGS (GST_BUFFER_DTS (buf)),
2804 GST_TIME_ARGS (GST_BUFFER_PTS (buf)),
2805 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)), GST_BUFFER_OFFSET (buf));
2807 /* Make sure the first buffer is always DISCONT. If we split
2808 * GOPs inside the parser this is otherwise not guaranteed */
2810 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2813 /* likewise, subsequent buffers should never have DISCONT
2814 * according to the "reverse fragment protocol", or such would
2815 * confuse a downstream decoder
2816 * (could be DISCONT due to aggregating upstream fragments by parsing) */
2817 GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DISCONT);
2820 /* iterate output queue an push downstream */
2821 ret = gst_pad_push (parse->srcpad, buf);
2822 send = g_slist_delete_link (send, send);
2824 /* clear any leftover if error */
2825 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2827 buf = GST_BUFFER_CAST (send->data);
2828 gst_buffer_unref (buf);
2829 send = g_slist_delete_link (send, send);
2834 parse->priv->buffers_send = send;
2839 /* gst_base_parse_start_fragment:
2841 * Prepares for processing a reverse playback (forward) fragment
2842 * by (re)setting proper state variables.
2844 static GstFlowReturn
2845 gst_base_parse_start_fragment (GstBaseParse * parse)
2847 GST_LOG_OBJECT (parse, "starting fragment");
2849 /* invalidate so no fall-back timestamping is performed;
2850 * ok if taken from subclass or upstream */
2851 parse->priv->next_pts = GST_CLOCK_TIME_NONE;
2852 parse->priv->prev_pts = GST_CLOCK_TIME_NONE;
2853 parse->priv->next_dts = GST_CLOCK_TIME_NONE;
2854 parse->priv->prev_dts = GST_CLOCK_TIME_NONE;
2855 parse->priv->prev_dts_from_pts = FALSE;
2856 /* prevent it hanging around stop all the time */
2857 parse->segment.position = GST_CLOCK_TIME_NONE;
2859 parse->priv->discont = TRUE;
2861 /* head of previous fragment is now pending tail of current fragment */
2862 parse->priv->buffers_pending = parse->priv->buffers_head;
2863 parse->priv->buffers_head = NULL;
2869 /* gst_base_parse_finish_fragment:
2871 * Processes a reverse playback (forward) fragment:
2872 * - append head of last fragment that was skipped to current fragment data
2873 * - drain the resulting current fragment data (i.e. repeated chain)
2874 * - add time/duration (if needed) to frames queued by chain
2875 * - push queued data
2877 static GstFlowReturn
2878 gst_base_parse_finish_fragment (GstBaseParse * parse, gboolean prev_head)
2881 GstFlowReturn ret = GST_FLOW_OK;
2882 gboolean seen_key = FALSE, seen_delta = FALSE;
2884 GST_LOG_OBJECT (parse, "finishing fragment");
2887 parse->priv->buffers_pending = g_slist_reverse (parse->priv->buffers_pending);
2888 while (parse->priv->buffers_pending) {
2889 buf = GST_BUFFER_CAST (parse->priv->buffers_pending->data);
2891 GST_LOG_OBJECT (parse, "adding pending buffer (size %" G_GSIZE_FORMAT ")",
2892 gst_buffer_get_size (buf));
2893 gst_adapter_push (parse->priv->adapter, buf);
2895 GST_LOG_OBJECT (parse, "discarding head buffer");
2896 gst_buffer_unref (buf);
2898 parse->priv->buffers_pending =
2899 g_slist_delete_link (parse->priv->buffers_pending,
2900 parse->priv->buffers_pending);
2903 /* chain looks for frames and queues resulting ones (in stead of pushing) */
2904 /* initial skipped data is added to buffers_pending */
2905 gst_base_parse_drain (parse);
2907 if (parse->priv->buffers_send) {
2908 buf = GST_BUFFER_CAST (parse->priv->buffers_send->data);
2909 seen_key |= !GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
2912 /* add metadata (if needed to queued buffers */
2913 GST_LOG_OBJECT (parse, "last timestamp: %" GST_TIME_FORMAT,
2914 GST_TIME_ARGS (parse->priv->last_pts));
2915 while (parse->priv->buffers_queued) {
2916 buf = GST_BUFFER_CAST (parse->priv->buffers_queued->data);
2918 /* no touching if upstream or parsing provided time */
2919 if (GST_BUFFER_PTS_IS_VALID (buf)) {
2920 GST_LOG_OBJECT (parse, "buffer has time %" GST_TIME_FORMAT,
2921 GST_TIME_ARGS (GST_BUFFER_PTS (buf)));
2922 } else if (GST_BUFFER_DURATION_IS_VALID (buf)) {
2923 if (GST_CLOCK_TIME_IS_VALID (parse->priv->last_pts)) {
2924 if (G_LIKELY (GST_BUFFER_DURATION (buf) <= parse->priv->last_pts))
2925 parse->priv->last_pts -= GST_BUFFER_DURATION (buf);
2927 parse->priv->last_pts = 0;
2928 GST_BUFFER_PTS (buf) = parse->priv->last_pts;
2929 GST_LOG_OBJECT (parse, "applied time %" GST_TIME_FORMAT,
2930 GST_TIME_ARGS (GST_BUFFER_PTS (buf)));
2932 if (GST_CLOCK_TIME_IS_VALID (parse->priv->last_dts)) {
2933 if (G_LIKELY (GST_BUFFER_DURATION (buf) <= parse->priv->last_dts))
2934 parse->priv->last_dts -= GST_BUFFER_DURATION (buf);
2936 parse->priv->last_dts = 0;
2937 GST_BUFFER_DTS (buf) = parse->priv->last_dts;
2938 GST_LOG_OBJECT (parse, "applied dts %" GST_TIME_FORMAT,
2939 GST_TIME_ARGS (GST_BUFFER_DTS (buf)));
2942 /* no idea, very bad */
2943 GST_WARNING_OBJECT (parse, "could not determine time for buffer");
2946 parse->priv->last_pts = GST_BUFFER_PTS (buf);
2947 parse->priv->last_dts = GST_BUFFER_DTS (buf);
2949 /* reverse order for ascending sending */
2950 /* send downstream at keyframe not preceded by a keyframe
2951 * (e.g. that should identify start of collection of IDR nals) */
2952 if (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT)) {
2954 ret = gst_base_parse_send_buffers (parse);
2955 /* if a problem, throw all to sending */
2956 if (ret != GST_FLOW_OK) {
2957 parse->priv->buffers_send =
2958 g_slist_reverse (parse->priv->buffers_queued);
2959 parse->priv->buffers_queued = NULL;
2969 parse->priv->buffers_send =
2970 g_slist_prepend (parse->priv->buffers_send, buf);
2971 parse->priv->buffers_queued =
2972 g_slist_delete_link (parse->priv->buffers_queued,
2973 parse->priv->buffers_queued);
2976 /* audio may have all marked as keyframe, so arrange to send here. Also
2977 * we might have ended the loop above on a keyframe, in which case we
2979 if (!seen_delta || seen_key)
2980 ret = gst_base_parse_send_buffers (parse);
2982 /* any trailing unused no longer usable (ideally none) */
2983 if (G_UNLIKELY (gst_adapter_available (parse->priv->adapter))) {
2984 GST_DEBUG_OBJECT (parse, "discarding %" G_GSIZE_FORMAT " trailing bytes",
2985 gst_adapter_available (parse->priv->adapter));
2986 gst_adapter_clear (parse->priv->adapter);
2992 /* small helper that checks whether we have been trying to resync too long */
2993 static inline GstFlowReturn
2994 gst_base_parse_check_sync (GstBaseParse * parse)
2996 if (G_UNLIKELY (parse->priv->discont &&
2997 parse->priv->offset - parse->priv->sync_offset > 2 * 1024 * 1024)) {
2998 GST_ELEMENT_ERROR (parse, STREAM, DECODE,
2999 ("Failed to parse stream"), (NULL));
3000 return GST_FLOW_ERROR;
3006 static GstFlowReturn
3007 gst_base_parse_process_streamheader (GstBaseParse * parse)
3011 const GValue *value;
3012 GstFlowReturn ret = GST_FLOW_OK;
3014 caps = gst_pad_get_current_caps (GST_BASE_PARSE_SINK_PAD (parse));
3018 str = gst_caps_get_structure (caps, 0);
3019 value = gst_structure_get_value (str, "streamheader");
3023 GST_DEBUG_OBJECT (parse, "Found streamheader field on input caps");
3025 if (GST_VALUE_HOLDS_ARRAY (value)) {
3027 gsize len = gst_value_array_get_size (value);
3029 for (i = 0; i < len; i++) {
3031 gst_value_get_buffer (gst_value_array_get_value (value, i));
3033 gst_base_parse_chain (GST_BASE_PARSE_SINK_PAD (parse),
3034 GST_OBJECT_CAST (parse), gst_buffer_ref (buffer));
3037 } else if (GST_VALUE_HOLDS_BUFFER (value)) {
3038 GstBuffer *buffer = gst_value_get_buffer (value);
3040 gst_base_parse_chain (GST_BASE_PARSE_SINK_PAD (parse),
3041 GST_OBJECT_CAST (parse), gst_buffer_ref (buffer));
3044 gst_caps_unref (caps);
3051 gst_caps_unref (caps);
3054 GST_DEBUG_OBJECT (parse, "No streamheader on caps");
3059 static GstFlowReturn
3060 gst_base_parse_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
3062 GstBaseParseClass *bclass;
3063 GstBaseParse *parse;
3064 GstFlowReturn ret = GST_FLOW_OK;
3065 GstFlowReturn old_ret = GST_FLOW_OK;
3066 GstBuffer *tmpbuf = NULL;
3070 GstClockTime pts, dts;
3072 parse = GST_BASE_PARSE (parent);
3073 bclass = GST_BASE_PARSE_GET_CLASS (parse);
3074 GST_DEBUG_OBJECT (parent, "chain");
3076 /* early out for speed, if we need to skip */
3077 if (buffer && GST_BUFFER_IS_DISCONT (buffer))
3078 parse->priv->skip = 0;
3079 if (parse->priv->skip > 0) {
3080 gsize bsize = gst_buffer_get_size (buffer);
3081 GST_DEBUG ("Got %" G_GSIZE_FORMAT " buffer, need to skip %u", bsize,
3083 if (parse->priv->skip >= bsize) {
3084 parse->priv->skip -= bsize;
3085 GST_DEBUG ("All the buffer is skipped");
3086 parse->priv->offset += bsize;
3087 parse->priv->sync_offset = parse->priv->offset;
3090 buffer = gst_buffer_make_writable (buffer);
3091 gst_buffer_resize (buffer, parse->priv->skip, bsize - parse->priv->skip);
3092 parse->priv->offset += parse->priv->skip;
3093 GST_DEBUG ("Done skipping, we have %u left on this buffer",
3094 (unsigned) (bsize - parse->priv->skip));
3095 parse->priv->skip = 0;
3096 parse->priv->discont = TRUE;
3099 if (G_UNLIKELY (parse->priv->first_buffer)) {
3100 parse->priv->first_buffer = FALSE;
3101 if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_HEADER)) {
3102 /* this stream has no header buffers, check if we just prepend the
3103 * streamheader from caps to the stream */
3104 GST_DEBUG_OBJECT (parse, "Looking for streamheader field on caps to "
3105 "prepend to the stream");
3106 gst_base_parse_process_streamheader (parse);
3108 GST_DEBUG_OBJECT (parse, "Stream has header buffers, not prepending "
3109 "streamheader from caps");
3113 if (parse->priv->detecting) {
3114 GstBuffer *detect_buf;
3116 if (parse->priv->detect_buffers_size == 0) {
3117 detect_buf = gst_buffer_ref (buffer);
3122 detect_buf = gst_buffer_new ();
3124 for (l = parse->priv->detect_buffers; l; l = l->next) {
3125 gsize tmpsize = gst_buffer_get_size (l->data);
3127 gst_buffer_copy_into (detect_buf, GST_BUFFER_CAST (l->data),
3128 GST_BUFFER_COPY_MEMORY, offset, tmpsize);
3132 gst_buffer_copy_into (detect_buf, buffer, GST_BUFFER_COPY_MEMORY,
3133 offset, gst_buffer_get_size (buffer));
3136 ret = bclass->detect (parse, detect_buf);
3137 gst_buffer_unref (detect_buf);
3139 if (ret == GST_FLOW_OK) {
3142 /* Detected something */
3143 parse->priv->detecting = FALSE;
3145 for (l = parse->priv->detect_buffers; l; l = l->next) {
3146 if (ret == GST_FLOW_OK && !parse->priv->flushing)
3148 gst_base_parse_chain (GST_BASE_PARSE_SINK_PAD (parse),
3149 parent, GST_BUFFER_CAST (l->data));
3151 gst_buffer_unref (GST_BUFFER_CAST (l->data));
3153 g_list_free (parse->priv->detect_buffers);
3154 parse->priv->detect_buffers = NULL;
3155 parse->priv->detect_buffers_size = 0;
3157 if (ret != GST_FLOW_OK) {
3161 /* Handle the current buffer */
3162 } else if (ret == GST_FLOW_NOT_NEGOTIATED) {
3163 /* Still detecting, append buffer or error out if draining */
3165 if (parse->priv->drain) {
3166 GST_DEBUG_OBJECT (parse, "Draining but did not detect format yet");
3167 return GST_FLOW_ERROR;
3168 } else if (parse->priv->flushing) {
3169 g_list_foreach (parse->priv->detect_buffers, (GFunc) gst_buffer_unref,
3171 g_list_free (parse->priv->detect_buffers);
3172 parse->priv->detect_buffers = NULL;
3173 parse->priv->detect_buffers_size = 0;
3175 parse->priv->detect_buffers =
3176 g_list_append (parse->priv->detect_buffers, buffer);
3177 parse->priv->detect_buffers_size += gst_buffer_get_size (buffer);
3181 /* Something went wrong, subclass responsible for error reporting */
3185 /* And now handle the current buffer if detection worked */
3188 if (G_LIKELY (buffer)) {
3189 GST_LOG_OBJECT (parse,
3190 "buffer size: %" G_GSIZE_FORMAT ", offset = %" G_GINT64_FORMAT
3191 ", dts %" GST_TIME_FORMAT ", pts %" GST_TIME_FORMAT,
3192 gst_buffer_get_size (buffer), GST_BUFFER_OFFSET (buffer),
3193 GST_TIME_ARGS (GST_BUFFER_DTS (buffer)),
3194 GST_TIME_ARGS (GST_BUFFER_PTS (buffer)));
3196 if (G_UNLIKELY (!parse->priv->disable_passthrough
3197 && parse->priv->passthrough)) {
3198 GstBaseParseFrame frame;
3200 gst_base_parse_frame_init (&frame);
3201 frame.buffer = gst_buffer_make_writable (buffer);
3202 ret = gst_base_parse_push_frame (parse, &frame);
3203 gst_base_parse_frame_free (&frame);
3206 if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DISCONT))) {
3207 /* upstream feeding us in reverse playback;
3208 * finish previous fragment and start new upon DISCONT */
3209 if (parse->segment.rate < 0.0) {
3210 GST_DEBUG_OBJECT (parse, "buffer starts new reverse playback fragment");
3211 ret = gst_base_parse_finish_fragment (parse, TRUE);
3212 gst_base_parse_start_fragment (parse);
3214 /* discont in the stream, drain and mark discont for next output */
3215 gst_base_parse_drain (parse);
3216 parse->priv->discont = TRUE;
3219 gst_adapter_push (parse->priv->adapter, buffer);
3222 /* Parse and push as many frames as possible */
3223 /* Stop either when adapter is empty or we are flushing */
3224 while (!parse->priv->flushing) {
3226 gboolean updated_prev_pts = FALSE;
3228 /* note: if subclass indicates MAX fsize,
3229 * this will not likely be available anyway ... */
3230 min_size = MAX (parse->priv->min_frame_size, fsize);
3231 av = gst_adapter_available (parse->priv->adapter);
3233 if (G_UNLIKELY (parse->priv->drain)) {
3235 GST_DEBUG_OBJECT (parse, "draining, data left: %d", min_size);
3236 if (G_UNLIKELY (!min_size)) {
3241 /* Collect at least min_frame_size bytes */
3242 if (av < min_size) {
3243 GST_DEBUG_OBJECT (parse, "not enough data available (only %d bytes)", av);
3247 /* move along with upstream timestamp (if any),
3248 * but interpolate in between */
3249 pts = gst_adapter_prev_pts (parse->priv->adapter, NULL);
3250 dts = gst_adapter_prev_dts (parse->priv->adapter, NULL);
3251 if (GST_CLOCK_TIME_IS_VALID (pts) && (parse->priv->prev_pts != pts)) {
3252 parse->priv->prev_pts = parse->priv->next_pts = pts;
3253 updated_prev_pts = TRUE;
3256 if (GST_CLOCK_TIME_IS_VALID (dts) && (parse->priv->prev_dts != dts)) {
3257 parse->priv->prev_dts = parse->priv->next_dts = dts;
3258 parse->priv->prev_dts_from_pts = FALSE;
3261 /* we can mess with, erm interpolate, timestamps,
3262 * and incoming stuff has PTS but no DTS seen so far,
3263 * then pick up DTS from PTS and hope for the best ... */
3264 if (parse->priv->infer_ts &&
3265 parse->priv->pts_interpolate &&
3266 !GST_CLOCK_TIME_IS_VALID (dts) &&
3267 (!GST_CLOCK_TIME_IS_VALID (parse->priv->prev_dts)
3268 || (parse->priv->prev_dts_from_pts && updated_prev_pts))
3269 && GST_CLOCK_TIME_IS_VALID (pts)) {
3270 parse->priv->prev_dts = parse->priv->next_dts = pts;
3271 parse->priv->prev_dts_from_pts = TRUE;
3274 /* always pass all available data */
3275 tmpbuf = gst_adapter_get_buffer (parse->priv->adapter, av);
3277 /* already inform subclass what timestamps we have planned,
3278 * at least if provided by time-based upstream */
3279 if (parse->priv->upstream_format == GST_FORMAT_TIME) {
3280 tmpbuf = gst_buffer_make_writable (tmpbuf);
3281 GST_BUFFER_PTS (tmpbuf) = parse->priv->next_pts;
3282 GST_BUFFER_DTS (tmpbuf) = parse->priv->next_dts;
3283 GST_BUFFER_DURATION (tmpbuf) = GST_CLOCK_TIME_NONE;
3286 /* keep the adapter mapped, so keep track of what has to be flushed */
3287 ret = gst_base_parse_handle_buffer (parse, tmpbuf, &skip, &flush);
3290 if (ret != GST_FLOW_OK && ret != GST_FLOW_NOT_LINKED) {
3293 if (skip == 0 && flush == 0) {
3294 GST_LOG_OBJECT (parse, "nothing skipped and no frames finished, "
3295 "breaking to get more data");
3296 /* ignore this return as it produced no data */
3300 if (old_ret == GST_FLOW_OK)
3305 GST_LOG_OBJECT (parse, "chain leaving");
3309 /* pull @size bytes at current offset,
3310 * i.e. at least try to and possibly return a shorter buffer if near the end */
3311 static GstFlowReturn
3312 gst_base_parse_pull_range (GstBaseParse * parse, guint size,
3313 GstBuffer ** buffer)
3315 GstFlowReturn ret = GST_FLOW_OK;
3317 g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);
3319 /* Caching here actually makes much less difference than one would expect.
3320 * We do it mainly to avoid pulling buffers of 1 byte all the time */
3321 if (parse->priv->cache) {
3322 gint64 cache_offset = GST_BUFFER_OFFSET (parse->priv->cache);
3323 gint cache_size = gst_buffer_get_size (parse->priv->cache);
3325 if (cache_offset <= parse->priv->offset &&
3326 (parse->priv->offset + size) <= (cache_offset + cache_size)) {
3327 *buffer = gst_buffer_copy_region (parse->priv->cache, GST_BUFFER_COPY_ALL,
3328 parse->priv->offset - cache_offset, size);
3329 GST_BUFFER_OFFSET (*buffer) = parse->priv->offset;
3332 /* not enough data in the cache, free cache and get a new one */
3333 gst_buffer_unref (parse->priv->cache);
3334 parse->priv->cache = NULL;
3337 /* refill the cache */
3339 gst_pad_pull_range (parse->sinkpad, parse->priv->offset, MAX (size,
3340 64 * 1024), &parse->priv->cache);
3341 if (ret != GST_FLOW_OK) {
3342 parse->priv->cache = NULL;
3346 if (gst_buffer_get_size (parse->priv->cache) >= size) {
3348 gst_buffer_copy_region (parse->priv->cache, GST_BUFFER_COPY_ALL, 0,
3350 GST_BUFFER_OFFSET (*buffer) = parse->priv->offset;
3354 /* Not possible to get enough data, try a last time with
3355 * requesting exactly the size we need */
3356 gst_buffer_unref (parse->priv->cache);
3357 parse->priv->cache = NULL;
3359 ret = gst_pad_pull_range (parse->sinkpad, parse->priv->offset, size,
3360 &parse->priv->cache);
3362 if (ret != GST_FLOW_OK) {
3363 GST_DEBUG_OBJECT (parse, "pull_range returned %d", ret);
3368 if (gst_buffer_get_size (parse->priv->cache) < size) {
3369 GST_DEBUG_OBJECT (parse, "Returning short buffer at offset %"
3370 G_GUINT64_FORMAT ": wanted %u bytes, got %" G_GSIZE_FORMAT " bytes",
3371 parse->priv->offset, size, gst_buffer_get_size (parse->priv->cache));
3373 *buffer = parse->priv->cache;
3374 parse->priv->cache = NULL;
3380 gst_buffer_copy_region (parse->priv->cache, GST_BUFFER_COPY_ALL, 0, size);
3381 GST_BUFFER_OFFSET (*buffer) = parse->priv->offset;
3386 static GstFlowReturn
3387 gst_base_parse_handle_previous_fragment (GstBaseParse * parse)
3390 GstClockTime ts = 0;
3394 GST_DEBUG_OBJECT (parse, "fragment ended; last_ts = %" GST_TIME_FORMAT
3395 ", last_offset = %" G_GINT64_FORMAT,
3396 GST_TIME_ARGS (parse->priv->last_pts), parse->priv->last_offset);
3398 if (!parse->priv->last_offset
3399 || parse->priv->last_pts <= parse->segment.start) {
3400 GST_DEBUG_OBJECT (parse, "past start of segment %" GST_TIME_FORMAT,
3401 GST_TIME_ARGS (parse->segment.start));
3406 /* last fragment started at last_offset / last_ts;
3407 * seek back 10s capped at 1MB */
3408 if (parse->priv->last_pts >= 10 * GST_SECOND)
3409 ts = parse->priv->last_pts - 10 * GST_SECOND;
3410 /* if we are exact now, we will be more so going backwards */
3411 if (parse->priv->exact_position) {
3412 offset = gst_base_parse_find_offset (parse, ts, TRUE, NULL);
3414 if (!gst_base_parse_convert (parse, GST_FORMAT_TIME, ts,
3415 GST_FORMAT_BYTES, &offset)) {
3416 GST_DEBUG_OBJECT (parse, "conversion failed, only BYTE based");
3419 offset = CLAMP (offset, parse->priv->last_offset - 1024 * 1024,
3420 parse->priv->last_offset - 1024);
3421 offset = MAX (0, offset);
3423 GST_DEBUG_OBJECT (parse, "next fragment from offset %" G_GINT64_FORMAT,
3425 parse->priv->offset = offset;
3427 ret = gst_base_parse_pull_range (parse, parse->priv->last_offset - offset,
3429 if (ret != GST_FLOW_OK)
3432 /* offset will increase again as fragment is processed/parsed */
3433 parse->priv->last_offset = offset;
3435 gst_base_parse_start_fragment (parse);
3436 gst_adapter_push (parse->priv->adapter, buffer);
3437 ret = gst_base_parse_finish_fragment (parse, TRUE);
3438 if (ret != GST_FLOW_OK)
3441 /* force previous fragment */
3442 parse->priv->offset = -1;
3449 * pull and scan for next frame starting from current offset
3450 * ajusts sync, drain and offset going along */
3451 static GstFlowReturn
3452 gst_base_parse_scan_frame (GstBaseParse * parse, GstBaseParseClass * klass)
3455 GstFlowReturn ret = GST_FLOW_OK;
3456 guint fsize, min_size;
3460 GST_LOG_OBJECT (parse, "scanning for frame at offset %" G_GUINT64_FORMAT
3461 " (%#" G_GINT64_MODIFIER "x)", parse->priv->offset, parse->priv->offset);
3463 /* let's make this efficient for all subclass once and for all;
3464 * maybe it does not need this much, but in the latter case, we know we are
3465 * in pull mode here and might as well try to read and supply more anyway
3466 * (so does the buffer caching mechanism) */
3470 min_size = MAX (parse->priv->min_frame_size, fsize);
3472 GST_LOG_OBJECT (parse, "reading buffer size %u", min_size);
3474 ret = gst_base_parse_pull_range (parse, min_size, &buffer);
3475 if (ret != GST_FLOW_OK)
3478 /* if we got a short read, inform subclass we are draining leftover
3479 * and no more is to be expected */
3480 if (gst_buffer_get_size (buffer) < min_size) {
3481 GST_LOG_OBJECT (parse, "... but did not get that; marked draining");
3482 parse->priv->drain = TRUE;
3485 if (parse->priv->detecting) {
3486 ret = klass->detect (parse, buffer);
3487 if (ret == GST_FLOW_NOT_NEGOTIATED) {
3488 /* If draining we error out, otherwise request a buffer
3490 if (parse->priv->drain) {
3491 gst_buffer_unref (buffer);
3492 GST_ERROR_OBJECT (parse, "Failed to detect format but draining");
3493 return GST_FLOW_ERROR;
3496 gst_buffer_unref (buffer);
3499 } else if (ret != GST_FLOW_OK) {
3500 gst_buffer_unref (buffer);
3501 GST_ERROR_OBJECT (parse, "detect() returned %s",
3502 gst_flow_get_name (ret));
3506 /* Else handle this buffer normally */
3509 ret = gst_base_parse_handle_buffer (parse, buffer, &skip, &flushed);
3510 if (ret != GST_FLOW_OK)
3513 /* If a large amount of data was requested to be skipped, _handle_buffer
3514 might have set the priv->skip flag to an extra amount on top of skip.
3515 In pull mode, we can just pull from the new offset directly. */
3516 parse->priv->offset += parse->priv->skip;
3517 parse->priv->skip = 0;
3519 /* something flushed means something happened,
3520 * and we should bail out of this loop so as not to occupy
3521 * the task thread indefinitely */
3523 GST_LOG_OBJECT (parse, "frame finished, breaking loop");
3526 /* nothing flushed, no skip and draining, so nothing left to do */
3527 if (!skip && parse->priv->drain) {
3528 GST_LOG_OBJECT (parse, "no activity or result when draining; "
3529 "breaking loop and marking EOS");
3533 /* otherwise, get some more data
3534 * note that is checked this does not happen indefinitely */
3536 GST_LOG_OBJECT (parse, "getting some more data");
3539 parse->priv->drain = FALSE;
3546 /* Loop that is used in pull mode to retrieve data from upstream */
3548 gst_base_parse_loop (GstPad * pad)
3550 GstBaseParse *parse;
3551 GstBaseParseClass *klass;
3552 GstFlowReturn ret = GST_FLOW_OK;
3554 parse = GST_BASE_PARSE (gst_pad_get_parent (pad));
3555 klass = GST_BASE_PARSE_GET_CLASS (parse);
3557 GST_LOG_OBJECT (parse, "Entering parse loop");
3559 if (G_UNLIKELY (parse->priv->push_stream_start)) {
3564 gst_pad_create_stream_id (parse->srcpad, GST_ELEMENT_CAST (parse),
3567 event = gst_event_new_stream_start (stream_id);
3568 gst_event_set_group_id (event, gst_util_group_id_next ());
3570 GST_DEBUG_OBJECT (parse, "Pushing STREAM_START");
3571 gst_pad_push_event (parse->srcpad, event);
3572 parse->priv->push_stream_start = FALSE;
3576 /* reverse playback:
3577 * first fragment (closest to stop time) is handled normally below,
3578 * then we pull in fragments going backwards */
3579 if (parse->segment.rate < 0.0) {
3580 /* check if we jumped back to a previous fragment,
3581 * which is a post-first fragment */
3582 if (parse->priv->offset < 0) {
3583 ret = gst_base_parse_handle_previous_fragment (parse);
3588 ret = gst_base_parse_scan_frame (parse, klass);
3590 /* eat expected eos signalling past segment in reverse playback */
3591 if (parse->segment.rate < 0.0 && ret == GST_FLOW_EOS &&
3592 parse->segment.position >= parse->segment.stop) {
3593 GST_DEBUG_OBJECT (parse, "downstream has reached end of segment");
3594 /* push what was accumulated during loop run */
3595 gst_base_parse_finish_fragment (parse, FALSE);
3596 /* force previous fragment */
3597 parse->priv->offset = -1;
3601 if (ret != GST_FLOW_OK)
3605 if (ret == GST_FLOW_EOS)
3607 else if (ret != GST_FLOW_OK)
3610 gst_object_unref (parse);
3617 GST_DEBUG_OBJECT (parse, "eos");
3622 gboolean push_eos = FALSE;
3624 GST_DEBUG_OBJECT (parse, "pausing task, reason %s",
3625 gst_flow_get_name (ret));
3626 gst_pad_pause_task (parse->sinkpad);
3628 if (ret == GST_FLOW_EOS) {
3629 /* handle end-of-stream/segment */
3630 if (parse->segment.flags & GST_SEGMENT_FLAG_SEGMENT) {
3633 if ((stop = parse->segment.stop) == -1)
3634 stop = parse->segment.duration;
3636 GST_DEBUG_OBJECT (parse, "sending segment_done");
3638 gst_element_post_message
3639 (GST_ELEMENT_CAST (parse),
3640 gst_message_new_segment_done (GST_OBJECT_CAST (parse),
3641 GST_FORMAT_TIME, stop));
3642 gst_pad_push_event (parse->srcpad,
3643 gst_event_new_segment_done (GST_FORMAT_TIME, stop));
3645 /* If we STILL have zero frames processed, fire an error */
3646 if (parse->priv->framecount == 0) {
3647 GST_ELEMENT_ERROR (parse, STREAM, WRONG_TYPE,
3648 ("No valid frames found before end of stream"), (NULL));
3652 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
3653 /* for fatal errors we post an error message, wrong-state is
3654 * not fatal because it happens due to flushes and only means
3655 * that we should stop now. */
3656 GST_ELEMENT_FLOW_ERROR (parse, ret);
3661 if (parse->priv->estimated_duration <= 0) {
3662 gst_base_parse_update_duration (parse);
3664 /* Push pending events, including SEGMENT events */
3665 gst_base_parse_push_pending_events (parse);
3667 topush = gst_event_new_eos ();
3668 GST_DEBUG_OBJECT (parse, "segment_seqnum:%" G_GUINT32_FORMAT,
3669 parse->priv->segment_seqnum);
3670 if (parse->priv->segment_seqnum != GST_SEQNUM_INVALID)
3671 gst_event_set_seqnum (topush, parse->priv->segment_seqnum);
3672 gst_pad_push_event (parse->srcpad, topush);
3674 gst_object_unref (parse);
3679 gst_base_parse_sink_activate (GstPad * sinkpad, GstObject * parent)
3681 GstSchedulingFlags sched_flags;
3682 GstBaseParse *parse;
3686 parse = GST_BASE_PARSE (parent);
3688 GST_DEBUG_OBJECT (parse, "sink activate");
3690 query = gst_query_new_scheduling ();
3691 if (!gst_pad_peer_query (sinkpad, query)) {
3692 gst_query_unref (query);
3693 goto baseparse_push;
3696 gst_query_parse_scheduling (query, &sched_flags, NULL, NULL, NULL);
3698 pull_mode = gst_query_has_scheduling_mode (query, GST_PAD_MODE_PULL)
3699 && ((sched_flags & GST_SCHEDULING_FLAG_SEEKABLE) != 0);
3701 gst_query_unref (query);
3704 goto baseparse_push;
3706 GST_DEBUG_OBJECT (parse, "trying to activate in pull mode");
3707 if (!gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE))
3708 goto baseparse_push;
3710 parse->priv->push_stream_start = TRUE;
3711 /* In pull mode, upstream is BYTES */
3712 parse->priv->upstream_format = GST_FORMAT_BYTES;
3714 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_base_parse_loop,
3719 GST_DEBUG_OBJECT (parse, "trying to activate in push mode");
3720 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
3725 gst_base_parse_activate (GstBaseParse * parse, gboolean active)
3727 GstBaseParseClass *klass;
3728 gboolean result = TRUE;
3730 GST_DEBUG_OBJECT (parse, "activate %d", active);
3732 klass = GST_BASE_PARSE_GET_CLASS (parse);
3735 if (parse->priv->pad_mode == GST_PAD_MODE_NONE && klass->start)
3736 result = klass->start (parse);
3738 /* If the subclass implements ::detect we want to
3739 * call it for the first buffers now */
3740 parse->priv->detecting = (klass->detect != NULL);
3742 /* We must make sure streaming has finished before resetting things
3743 * and calling the ::stop vfunc */
3744 GST_PAD_STREAM_LOCK (parse->sinkpad);
3745 GST_PAD_STREAM_UNLOCK (parse->sinkpad);
3747 if (parse->priv->pad_mode != GST_PAD_MODE_NONE && klass->stop)
3748 result = klass->stop (parse);
3750 parse->priv->pad_mode = GST_PAD_MODE_NONE;
3751 parse->priv->upstream_format = GST_FORMAT_UNDEFINED;
3753 GST_DEBUG_OBJECT (parse, "activate return: %d", result);
3758 gst_base_parse_sink_activate_mode (GstPad * pad, GstObject * parent,
3759 GstPadMode mode, gboolean active)
3762 GstBaseParse *parse;
3764 parse = GST_BASE_PARSE (parent);
3766 GST_DEBUG_OBJECT (parse, "sink %sactivate in %s mode",
3767 (active) ? "" : "de", gst_pad_mode_get_name (mode));
3769 if (!gst_base_parse_activate (parse, active))
3770 goto activate_failed;
3773 case GST_PAD_MODE_PULL:
3775 GstEvent *ev = gst_event_new_segment (&parse->segment);
3776 parse->priv->segment_seqnum = gst_event_get_seqnum (ev);
3777 parse->priv->pending_events =
3778 g_list_prepend (parse->priv->pending_events, ev);
3781 result = gst_pad_stop_task (pad);
3789 parse->priv->pad_mode = active ? mode : GST_PAD_MODE_NONE;
3791 GST_DEBUG_OBJECT (parse, "sink activate return: %d", result);
3798 GST_DEBUG_OBJECT (parse, "activate failed");
3804 * gst_base_parse_set_duration:
3805 * @parse: #GstBaseParse.
3807 * @duration: duration value.
3808 * @interval: how often to update the duration estimate based on bitrate, or 0.
3810 * Sets the duration of the currently playing media. Subclass can use this
3811 * when it is able to determine duration and/or notices a change in the media
3812 * duration. Alternatively, if @interval is non-zero (default), then stream
3813 * duration is determined based on estimated bitrate, and updated every @interval
3817 gst_base_parse_set_duration (GstBaseParse * parse,
3818 GstFormat fmt, gint64 duration, gint interval)
3820 g_return_if_fail (parse != NULL);
3822 if (parse->priv->upstream_has_duration) {
3823 GST_DEBUG_OBJECT (parse, "using upstream duration; discarding update");
3827 if (duration != parse->priv->duration) {
3830 m = gst_message_new_duration_changed (GST_OBJECT (parse));
3831 gst_element_post_message (GST_ELEMENT (parse), m);
3833 /* TODO: what about duration tag? */
3835 parse->priv->duration = duration;
3836 parse->priv->duration_fmt = fmt;
3837 GST_DEBUG_OBJECT (parse, "set duration: %" G_GINT64_FORMAT, duration);
3838 if (fmt == GST_FORMAT_TIME && GST_CLOCK_TIME_IS_VALID (duration)) {
3839 if (interval != 0) {
3840 GST_DEBUG_OBJECT (parse, "valid duration provided, disabling estimate");
3844 GST_DEBUG_OBJECT (parse, "set update interval: %d", interval);
3845 parse->priv->update_interval = interval;
3851 * gst_base_parse_set_average_bitrate:
3852 * @parse: #GstBaseParse.
3853 * @bitrate: average bitrate in bits/second
3855 * Optionally sets the average bitrate detected in media (if non-zero),
3856 * e.g. based on metadata, as it will be posted to the application.
3858 * By default, announced average bitrate is estimated. The average bitrate
3859 * is used to estimate the total duration of the stream and to estimate
3860 * a seek position, if there's no index and the format is syncable
3861 * (see gst_base_parse_set_syncable()).
3864 gst_base_parse_set_average_bitrate (GstBaseParse * parse, guint bitrate)
3866 parse->priv->bitrate = bitrate;
3867 GST_DEBUG_OBJECT (parse, "bitrate %u", bitrate);
3871 * gst_base_parse_set_min_frame_size:
3872 * @parse: #GstBaseParse.
3873 * @min_size: Minimum size in bytes of the data that this base class should
3876 * Subclass can use this function to tell the base class that it needs to
3877 * be given buffers of at least @min_size bytes.
3880 gst_base_parse_set_min_frame_size (GstBaseParse * parse, guint min_size)
3882 g_return_if_fail (parse != NULL);
3884 parse->priv->min_frame_size = min_size;
3885 GST_LOG_OBJECT (parse, "set frame_min_size: %d", min_size);
3889 * gst_base_parse_set_frame_rate:
3890 * @parse: the #GstBaseParse to set
3891 * @fps_num: frames per second (numerator).
3892 * @fps_den: frames per second (denominator).
3893 * @lead_in: frames needed before a segment for subsequent decode
3894 * @lead_out: frames needed after a segment
3896 * If frames per second is configured, parser can take care of buffer duration
3897 * and timestamping. When performing segment clipping, or seeking to a specific
3898 * location, a corresponding decoder might need an initial @lead_in and a
3899 * following @lead_out number of frames to ensure the desired segment is
3900 * entirely filled upon decoding.
3903 gst_base_parse_set_frame_rate (GstBaseParse * parse, guint fps_num,
3904 guint fps_den, guint lead_in, guint lead_out)
3906 g_return_if_fail (parse != NULL);
3908 parse->priv->fps_num = fps_num;
3909 parse->priv->fps_den = fps_den;
3910 if (!fps_num || !fps_den) {
3911 GST_DEBUG_OBJECT (parse, "invalid fps (%d/%d), ignoring parameters",
3913 fps_num = fps_den = 0;
3914 parse->priv->frame_duration = GST_CLOCK_TIME_NONE;
3915 parse->priv->lead_in = parse->priv->lead_out = 0;
3916 parse->priv->lead_in_ts = parse->priv->lead_out_ts = 0;
3918 parse->priv->frame_duration =
3919 gst_util_uint64_scale (GST_SECOND, fps_den, fps_num);
3920 parse->priv->lead_in = lead_in;
3921 parse->priv->lead_out = lead_out;
3922 parse->priv->lead_in_ts =
3923 gst_util_uint64_scale (GST_SECOND, fps_den * lead_in, fps_num);
3924 parse->priv->lead_out_ts =
3925 gst_util_uint64_scale (GST_SECOND, fps_den * lead_out, fps_num);
3926 /* aim for about 1.5s to estimate duration */
3927 if (parse->priv->update_interval < 0) {
3928 guint64 interval = gst_util_uint64_scale (fps_num, 3,
3929 G_GUINT64_CONSTANT (2) * fps_den);
3931 parse->priv->update_interval = MIN (interval, G_MAXINT);
3933 GST_LOG_OBJECT (parse, "estimated update interval to %d frames",
3934 parse->priv->update_interval);
3937 GST_LOG_OBJECT (parse, "set fps: %d/%d => duration: %" G_GINT64_FORMAT " ms",
3938 fps_num, fps_den, parse->priv->frame_duration / GST_MSECOND);
3939 GST_LOG_OBJECT (parse, "set lead in: %d frames = %" G_GUINT64_FORMAT " ms, "
3940 "lead out: %d frames = %" G_GUINT64_FORMAT " ms",
3941 lead_in, parse->priv->lead_in_ts / GST_MSECOND,
3942 lead_out, parse->priv->lead_out_ts / GST_MSECOND);
3946 * gst_base_parse_set_has_timing_info:
3947 * @parse: a #GstBaseParse
3948 * @has_timing: whether frames carry timing information
3950 * Set if frames carry timing information which the subclass can (generally)
3951 * parse and provide. In particular, intrinsic (rather than estimated) time
3952 * can be obtained following a seek.
3955 gst_base_parse_set_has_timing_info (GstBaseParse * parse, gboolean has_timing)
3957 parse->priv->has_timing_info = has_timing;
3958 GST_INFO_OBJECT (parse, "has_timing: %s", (has_timing) ? "yes" : "no");
3962 * gst_base_parse_set_syncable:
3963 * @parse: a #GstBaseParse
3964 * @syncable: set if frame starts can be identified
3966 * Set if frame starts can be identified. This is set by default and
3967 * determines whether seeking based on bitrate averages
3968 * is possible for a format/stream.
3971 gst_base_parse_set_syncable (GstBaseParse * parse, gboolean syncable)
3973 parse->priv->syncable = syncable;
3974 GST_INFO_OBJECT (parse, "syncable: %s", (syncable) ? "yes" : "no");
3978 * gst_base_parse_set_passthrough:
3979 * @parse: a #GstBaseParse
3980 * @passthrough: %TRUE if parser should run in passthrough mode
3982 * Set if the nature of the format or configuration does not allow (much)
3983 * parsing, and the parser should operate in passthrough mode (which only
3984 * applies when operating in push mode). That is, incoming buffers are
3985 * pushed through unmodified, i.e. no #GstBaseParseClass.handle_frame()
3986 * will be invoked, but #GstBaseParseClass.pre_push_frame() will still be
3987 * invoked, so subclass can perform as much or as little is appropriate for
3988 * passthrough semantics in #GstBaseParseClass.pre_push_frame().
3991 gst_base_parse_set_passthrough (GstBaseParse * parse, gboolean passthrough)
3993 parse->priv->passthrough = passthrough;
3994 GST_INFO_OBJECT (parse, "passthrough: %s", (passthrough) ? "yes" : "no");
3998 * gst_base_parse_set_pts_interpolation:
3999 * @parse: a #GstBaseParse
4000 * @pts_interpolate: %TRUE if parser should interpolate PTS timestamps
4002 * By default, the base class will guess PTS timestamps using a simple
4003 * interpolation (previous timestamp + duration), which is incorrect for
4004 * data streams with reordering, where PTS can go backward. Sub-classes
4005 * implementing such formats should disable PTS interpolation.
4008 gst_base_parse_set_pts_interpolation (GstBaseParse * parse,
4009 gboolean pts_interpolate)
4011 parse->priv->pts_interpolate = pts_interpolate;
4012 GST_INFO_OBJECT (parse, "PTS interpolation: %s",
4013 (pts_interpolate) ? "yes" : "no");
4017 * gst_base_parse_set_infer_ts:
4018 * @parse: a #GstBaseParse
4019 * @infer_ts: %TRUE if parser should infer DTS/PTS from each other
4021 * By default, the base class might try to infer PTS from DTS and vice
4022 * versa. While this is generally correct for audio data, it may not
4023 * be otherwise. Sub-classes implementing such formats should disable
4024 * timestamp inferring.
4027 gst_base_parse_set_infer_ts (GstBaseParse * parse, gboolean infer_ts)
4029 parse->priv->infer_ts = infer_ts;
4030 GST_INFO_OBJECT (parse, "TS inferring: %s", (infer_ts) ? "yes" : "no");
4034 * gst_base_parse_set_latency:
4035 * @parse: a #GstBaseParse
4036 * @min_latency: minimum parse latency
4037 * @max_latency: maximum parse latency
4039 * Sets the minimum and maximum (which may likely be equal) latency introduced
4040 * by the parsing process. If there is such a latency, which depends on the
4041 * particular parsing of the format, it typically corresponds to 1 frame duration.
4044 gst_base_parse_set_latency (GstBaseParse * parse, GstClockTime min_latency,
4045 GstClockTime max_latency)
4047 g_return_if_fail (min_latency != GST_CLOCK_TIME_NONE);
4048 g_return_if_fail (min_latency <= max_latency);
4050 GST_OBJECT_LOCK (parse);
4051 parse->priv->min_latency = min_latency;
4052 parse->priv->max_latency = max_latency;
4053 GST_OBJECT_UNLOCK (parse);
4054 GST_INFO_OBJECT (parse, "min/max latency %" GST_TIME_FORMAT ", %"
4055 GST_TIME_FORMAT, GST_TIME_ARGS (min_latency),
4056 GST_TIME_ARGS (max_latency));
4060 gst_base_parse_get_duration (GstBaseParse * parse, GstFormat format,
4061 GstClockTime * duration)
4063 gboolean res = FALSE;
4065 g_return_val_if_fail (duration != NULL, FALSE);
4067 *duration = GST_CLOCK_TIME_NONE;
4068 if (parse->priv->duration != -1 && format == parse->priv->duration_fmt) {
4069 GST_LOG_OBJECT (parse, "using provided duration");
4070 *duration = parse->priv->duration;
4072 } else if (parse->priv->duration != -1) {
4073 GST_LOG_OBJECT (parse, "converting provided duration");
4074 res = gst_base_parse_convert (parse, parse->priv->duration_fmt,
4075 parse->priv->duration, format, (gint64 *) duration);
4076 } else if (format == GST_FORMAT_TIME && parse->priv->estimated_duration != -1) {
4077 GST_LOG_OBJECT (parse, "using estimated duration");
4078 *duration = parse->priv->estimated_duration;
4081 GST_LOG_OBJECT (parse, "cannot estimate duration");
4084 GST_LOG_OBJECT (parse, "res: %d, duration %" GST_TIME_FORMAT, res,
4085 GST_TIME_ARGS (*duration));
4090 gst_base_parse_src_query_default (GstBaseParse * parse, GstQuery * query)
4092 gboolean res = FALSE;
4095 pad = GST_BASE_PARSE_SRC_PAD (parse);
4097 switch (GST_QUERY_TYPE (query)) {
4098 case GST_QUERY_POSITION:
4103 GST_DEBUG_OBJECT (parse, "position query");
4104 gst_query_parse_position (query, &format, NULL);
4106 /* try upstream first */
4107 res = gst_pad_query_default (pad, GST_OBJECT_CAST (parse), query);
4109 /* Fall back on interpreting segment */
4110 GST_OBJECT_LOCK (parse);
4111 /* Only reply BYTES if upstream is in BYTES already, otherwise
4112 * we're not in charge */
4113 if (format == GST_FORMAT_BYTES
4114 && parse->priv->upstream_format == GST_FORMAT_BYTES) {
4115 dest_value = parse->priv->offset;
4117 } else if (format == parse->segment.format &&
4118 GST_CLOCK_TIME_IS_VALID (parse->segment.position)) {
4119 dest_value = gst_segment_to_stream_time (&parse->segment,
4120 parse->segment.format, parse->segment.position);
4123 GST_OBJECT_UNLOCK (parse);
4124 if (!res && parse->priv->upstream_format == GST_FORMAT_BYTES) {
4125 /* no precise result, upstream no idea either, then best estimate */
4126 /* priv->offset is updated in both PUSH/PULL modes, *iff* we're
4127 * in charge of things */
4128 res = gst_base_parse_convert (parse,
4129 GST_FORMAT_BYTES, parse->priv->offset, format, &dest_value);
4132 gst_query_set_position (query, format, dest_value);
4136 case GST_QUERY_DURATION:
4139 GstClockTime duration;
4141 GST_DEBUG_OBJECT (parse, "duration query");
4142 gst_query_parse_duration (query, &format, NULL);
4144 /* consult upstream */
4145 res = gst_pad_query_default (pad, GST_OBJECT_CAST (parse), query);
4147 /* otherwise best estimate from us */
4149 res = gst_base_parse_get_duration (parse, format, &duration);
4151 gst_query_set_duration (query, format, duration);
4155 case GST_QUERY_SEEKING:
4158 GstClockTime duration = GST_CLOCK_TIME_NONE;
4159 gboolean seekable = FALSE;
4161 GST_DEBUG_OBJECT (parse, "seeking query");
4162 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
4164 /* consult upstream */
4165 res = gst_pad_query_default (pad, GST_OBJECT_CAST (parse), query);
4167 /* we may be able to help if in TIME */
4168 if (fmt == GST_FORMAT_TIME && gst_base_parse_is_seekable (parse)) {
4169 gst_query_parse_seeking (query, &fmt, &seekable, NULL, NULL);
4170 /* already OK if upstream takes care */
4171 GST_LOG_OBJECT (parse, "upstream handled %d, seekable %d",
4173 if (!(res && seekable)) {
4174 if (!gst_base_parse_get_duration (parse, GST_FORMAT_TIME, &duration)
4175 || duration == -1) {
4176 /* seekable if we still have a chance to get duration later on */
4177 seekable = parse->priv->upstream_seekable &&
4178 (parse->priv->update_interval > 0);
4180 seekable = parse->priv->upstream_seekable;
4181 GST_LOG_OBJECT (parse, "already determine upstream seekabled: %d",
4184 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable, 0, duration);
4190 case GST_QUERY_FORMATS:
4191 gst_query_set_formatsv (query, 3, fmtlist);
4194 case GST_QUERY_CONVERT:
4196 GstFormat src_format, dest_format;
4197 gint64 src_value, dest_value;
4199 gst_query_parse_convert (query, &src_format, &src_value,
4200 &dest_format, &dest_value);
4202 res = gst_base_parse_convert (parse, src_format, src_value,
4203 dest_format, &dest_value);
4205 gst_query_set_convert (query, src_format, src_value,
4206 dest_format, dest_value);
4210 case GST_QUERY_LATENCY:
4212 if ((res = gst_pad_peer_query (parse->sinkpad, query))) {
4214 GstClockTime min_latency, max_latency;
4216 gst_query_parse_latency (query, &live, &min_latency, &max_latency);
4217 GST_DEBUG_OBJECT (parse, "Peer latency: live %d, min %"
4218 GST_TIME_FORMAT " max %" GST_TIME_FORMAT, live,
4219 GST_TIME_ARGS (min_latency), GST_TIME_ARGS (max_latency));
4221 GST_OBJECT_LOCK (parse);
4222 /* add our latency */
4223 min_latency += parse->priv->min_latency;
4224 if (max_latency == -1 || parse->priv->max_latency == -1)
4227 max_latency += parse->priv->max_latency;
4228 GST_OBJECT_UNLOCK (parse);
4230 gst_query_set_latency (query, live, min_latency, max_latency);
4234 case GST_QUERY_SEGMENT:
4239 format = parse->segment.format;
4242 gst_segment_to_stream_time (&parse->segment, format,
4243 parse->segment.start);
4244 if ((stop = parse->segment.stop) == -1)
4245 stop = parse->segment.duration;
4247 stop = gst_segment_to_stream_time (&parse->segment, format, stop);
4249 gst_query_set_segment (query, parse->segment.rate, format, start, stop);
4254 res = gst_pad_query_default (pad, GST_OBJECT_CAST (parse), query);
4260 /* scans for a cluster start from @pos,
4261 * return GST_FLOW_OK and frame position/time in @pos/@time if found */
4262 static GstFlowReturn
4263 gst_base_parse_find_frame (GstBaseParse * parse, gint64 * pos,
4264 GstClockTime * time, GstClockTime * duration)
4266 GstBaseParseClass *klass;
4268 gboolean orig_drain, orig_discont;
4269 GstFlowReturn ret = GST_FLOW_OK;
4270 GstBuffer *buf = NULL;
4271 GstBaseParseFrame *sframe = NULL;
4273 g_return_val_if_fail (pos != NULL, GST_FLOW_ERROR);
4274 g_return_val_if_fail (time != NULL, GST_FLOW_ERROR);
4275 g_return_val_if_fail (duration != NULL, GST_FLOW_ERROR);
4277 klass = GST_BASE_PARSE_GET_CLASS (parse);
4279 *time = GST_CLOCK_TIME_NONE;
4280 *duration = GST_CLOCK_TIME_NONE;
4283 orig_offset = parse->priv->offset;
4284 orig_discont = parse->priv->discont;
4285 orig_drain = parse->priv->drain;
4287 GST_DEBUG_OBJECT (parse, "scanning for frame starting at %" G_GINT64_FORMAT
4288 " (%#" G_GINT64_MODIFIER "x)", *pos, *pos);
4290 /* jump elsewhere and locate next frame */
4291 parse->priv->offset = *pos;
4292 /* mark as scanning so frames don't get processed all the way */
4293 parse->priv->scanning = TRUE;
4294 ret = gst_base_parse_scan_frame (parse, klass);
4295 parse->priv->scanning = FALSE;
4296 /* retrieve frame found during scan */
4297 sframe = parse->priv->scanned_frame;
4298 parse->priv->scanned_frame = NULL;
4300 if (ret != GST_FLOW_OK || !sframe)
4303 /* get offset first, subclass parsing might dump other stuff in there */
4304 *pos = sframe->offset;
4305 buf = sframe->buffer;
4308 /* but it should provide proper time */
4309 *time = GST_BUFFER_TIMESTAMP (buf);
4310 *duration = GST_BUFFER_DURATION (buf);
4312 GST_LOG_OBJECT (parse,
4313 "frame with time %" GST_TIME_FORMAT " at offset %" G_GINT64_FORMAT,
4314 GST_TIME_ARGS (*time), *pos);
4318 gst_base_parse_frame_free (sframe);
4321 parse->priv->offset = orig_offset;
4322 parse->priv->discont = orig_discont;
4323 parse->priv->drain = orig_drain;
4328 /* bisect and scan through file for frame starting before @time,
4329 * returns OK and @time/@offset if found, NONE and/or error otherwise
4330 * If @time == G_MAXINT64, scan for duration ( == last frame) */
4331 static GstFlowReturn
4332 gst_base_parse_locate_time (GstBaseParse * parse, GstClockTime * _time,
4335 GstFlowReturn ret = GST_FLOW_OK;
4336 gint64 lpos, hpos, newpos;
4337 GstClockTime time, ltime, htime, newtime, dur;
4338 gboolean cont = TRUE;
4339 const GstClockTime tolerance = TARGET_DIFFERENCE;
4340 const guint chunk = 4 * 1024;
4342 g_return_val_if_fail (_time != NULL, GST_FLOW_ERROR);
4343 g_return_val_if_fail (_offset != NULL, GST_FLOW_ERROR);
4345 GST_DEBUG_OBJECT (parse, "Bisecting for time %" GST_TIME_FORMAT,
4346 GST_TIME_ARGS (*_time));
4348 /* TODO also make keyframe aware if useful some day */
4363 /* do not know at first */
4365 *_time = GST_CLOCK_TIME_NONE;
4367 /* need initial positions; start and end */
4368 lpos = parse->priv->first_frame_offset;
4369 ltime = parse->priv->first_frame_pts;
4370 /* try other one if no luck */
4371 if (!GST_CLOCK_TIME_IS_VALID (ltime))
4372 ltime = parse->priv->first_frame_dts;
4373 if (!gst_base_parse_get_duration (parse, GST_FORMAT_TIME, &htime)) {
4374 GST_DEBUG_OBJECT (parse, "Unknown time duration, cannot bisect");
4375 return GST_FLOW_ERROR;
4377 hpos = parse->priv->upstream_size;
4379 GST_DEBUG_OBJECT (parse,
4380 "Bisection initial bounds: bytes %" G_GINT64_FORMAT " %" G_GINT64_FORMAT
4381 ", times %" GST_TIME_FORMAT " %" GST_TIME_FORMAT, lpos, hpos,
4382 GST_TIME_ARGS (ltime), GST_TIME_ARGS (htime));
4384 /* check preconditions are satisfied;
4385 * start and end are needed, except for special case where we scan for
4386 * last frame to determine duration */
4387 if (parse->priv->pad_mode != GST_PAD_MODE_PULL || !hpos ||
4388 !GST_CLOCK_TIME_IS_VALID (ltime) ||
4389 (!GST_CLOCK_TIME_IS_VALID (htime) && time != G_MAXINT64)) {
4393 /* shortcut cases */
4396 } else if (time < ltime + tolerance) {
4400 } else if (time >= htime) {
4406 while (htime > ltime && cont) {
4407 GST_LOG_OBJECT (parse,
4408 "lpos: %" G_GUINT64_FORMAT ", ltime: %" GST_TIME_FORMAT, lpos,
4409 GST_TIME_ARGS (ltime));
4410 GST_LOG_OBJECT (parse,
4411 "hpos: %" G_GUINT64_FORMAT ", htime: %" GST_TIME_FORMAT, hpos,
4412 GST_TIME_ARGS (htime));
4413 if (G_UNLIKELY (time == G_MAXINT64)) {
4415 } else if (G_LIKELY (hpos > lpos)) {
4417 gst_util_uint64_scale (hpos - lpos, time - ltime, htime - ltime) +
4420 /* should mean lpos == hpos, since lpos <= hpos is invariant */
4422 /* we check this case once, but not forever, so break loop */
4427 newpos = CLAMP (newpos, lpos, hpos);
4428 GST_LOG_OBJECT (parse,
4429 "estimated _offset for %" GST_TIME_FORMAT ": %" G_GINT64_FORMAT,
4430 GST_TIME_ARGS (time), newpos);
4432 ret = gst_base_parse_find_frame (parse, &newpos, &newtime, &dur);
4433 if (ret == GST_FLOW_EOS) {
4434 /* heuristic HACK */
4435 hpos = MAX (lpos, hpos - chunk);
4437 } else if (ret != GST_FLOW_OK) {
4441 if (newtime == -1 || newpos == -1) {
4442 GST_DEBUG_OBJECT (parse, "subclass did not provide metadata; aborting");
4446 if (G_UNLIKELY (time == G_MAXINT64)) {
4449 if (GST_CLOCK_TIME_IS_VALID (dur))
4452 } else if (newtime > time) {
4454 hpos = (newpos >= hpos) ? MAX (lpos, hpos - chunk) : MAX (lpos, newpos);
4456 } else if (newtime + tolerance > time) {
4457 /* close enough undershoot */
4461 } else if (newtime < ltime) {
4462 /* so a position beyond lpos resulted in earlier time than ltime ... */
4463 GST_DEBUG_OBJECT (parse, "non-ascending time; aborting");
4466 /* undershoot too far */
4467 newpos += newpos == lpos ? chunk : 0;
4468 lpos = CLAMP (newpos, lpos, hpos);
4474 GST_LOG_OBJECT (parse, "return offset %" G_GINT64_FORMAT ", time %"
4475 GST_TIME_FORMAT, *_offset, GST_TIME_ARGS (*_time));
4480 gst_base_parse_find_offset (GstBaseParse * parse, GstClockTime time,
4481 gboolean before, GstClockTime * _ts)
4483 gint64 bytes = 0, ts = 0;
4484 GstIndexEntry *entry = NULL;
4486 if (time == GST_CLOCK_TIME_NONE) {
4492 GST_BASE_PARSE_INDEX_LOCK (parse);
4493 if (parse->priv->index) {
4494 /* Let's check if we have an index entry for that time */
4495 entry = gst_index_get_assoc_entry (parse->priv->index,
4496 parse->priv->index_id,
4497 before ? GST_INDEX_LOOKUP_BEFORE : GST_INDEX_LOOKUP_AFTER,
4498 GST_INDEX_ASSOCIATION_FLAG_KEY_UNIT, GST_FORMAT_TIME, time);
4502 gst_index_entry_assoc_map (entry, GST_FORMAT_BYTES, &bytes);
4503 gst_index_entry_assoc_map (entry, GST_FORMAT_TIME, &ts);
4505 GST_DEBUG_OBJECT (parse, "found index entry for %" GST_TIME_FORMAT
4506 " at %" GST_TIME_FORMAT ", offset %" G_GINT64_FORMAT,
4507 GST_TIME_ARGS (time), GST_TIME_ARGS (ts), bytes);
4509 GST_DEBUG_OBJECT (parse, "no index entry found for %" GST_TIME_FORMAT,
4510 GST_TIME_ARGS (time));
4513 ts = GST_CLOCK_TIME_NONE;
4516 GST_BASE_PARSE_INDEX_UNLOCK (parse);
4525 /* returns TRUE if seek succeeded */
4527 gst_base_parse_handle_seek (GstBaseParse * parse, GstEvent * event)
4532 GstSeekType start_type = GST_SEEK_TYPE_NONE, stop_type;
4533 gboolean flush, update, res = TRUE, accurate;
4534 gint64 start, stop, seekpos, seekstop;
4535 GstSegment seeksegment = { 0, };
4536 GstClockTime start_ts;
4538 GstEvent *segment_event;
4540 /* try upstream first, unless we're driving the streaming thread ourselves */
4541 if (parse->priv->pad_mode != GST_PAD_MODE_PULL) {
4542 res = gst_pad_push_event (parse->sinkpad, gst_event_ref (event));
4547 gst_event_parse_seek (event, &rate, &format, &flags,
4548 &start_type, &start, &stop_type, &stop);
4549 seqnum = gst_event_get_seqnum (event);
4551 GST_DEBUG_OBJECT (parse, "seek to format %s, rate %f, "
4552 "start type %d at %" GST_TIME_FORMAT ", end type %d at %"
4553 GST_TIME_FORMAT, gst_format_get_name (format), rate,
4554 start_type, GST_TIME_ARGS (start), stop_type, GST_TIME_ARGS (stop));
4556 /* we can only handle TIME, so check if subclass can convert
4557 * to TIME format if it's some other format (such as DEFAULT) */
4558 if (format != GST_FORMAT_TIME) {
4559 if (!gst_base_parse_convert (parse, format, start, GST_FORMAT_TIME, &start)
4560 || !gst_base_parse_convert (parse, format, stop, GST_FORMAT_TIME,
4562 goto no_convert_to_time;
4564 GST_INFO_OBJECT (parse, "converted %s format to start time "
4565 "%" GST_TIME_FORMAT " and stop time %" GST_TIME_FORMAT,
4566 gst_format_get_name (format), GST_TIME_ARGS (start),
4567 GST_TIME_ARGS (stop));
4569 format = GST_FORMAT_TIME;
4572 /* no negative rates in push mode (unless upstream takes care of that, but
4573 * we've already tried upstream and it didn't handle the seek request) */
4574 if (rate < 0.0 && parse->priv->pad_mode == GST_PAD_MODE_PUSH)
4577 if (start_type != GST_SEEK_TYPE_SET ||
4578 (stop_type != GST_SEEK_TYPE_SET && stop_type != GST_SEEK_TYPE_NONE))
4581 /* get flush flag */
4582 flush = flags & GST_SEEK_FLAG_FLUSH;
4584 /* copy segment, we need this because we still need the old
4585 * segment when we close the current segment. */
4586 gst_segment_copy_into (&parse->segment, &seeksegment);
4588 GST_DEBUG_OBJECT (parse, "configuring seek");
4589 gst_segment_do_seek (&seeksegment, rate, format, flags,
4590 start_type, start, stop_type, stop, &update);
4592 /* accurate seeking implies seek tables are used to obtain position,
4593 * and the requested segment is maintained exactly, not adjusted any way */
4594 accurate = flags & GST_SEEK_FLAG_ACCURATE;
4596 /* maybe we can be accurate for (almost) free */
4597 gst_base_parse_find_offset (parse, seeksegment.position, TRUE, &start_ts);
4598 if (seeksegment.position <= start_ts + TARGET_DIFFERENCE) {
4599 GST_DEBUG_OBJECT (parse, "accurate seek possible");
4604 GstClockTime startpos;
4606 startpos = seeksegment.position;
4610 /* accurate requested, so ... seek a bit before target */
4611 if (startpos < parse->priv->lead_in_ts)
4614 startpos -= parse->priv->lead_in_ts;
4616 if (seeksegment.stop == -1 && seeksegment.duration != -1)
4617 seeksegment.stop = seeksegment.start + seeksegment.duration;
4619 seekpos = gst_base_parse_find_offset (parse, startpos, TRUE, &start_ts);
4620 seekstop = gst_base_parse_find_offset (parse, seeksegment.stop, FALSE,
4624 start_ts = seeksegment.position;
4628 if (seeksegment.stop == -1 && seeksegment.duration != -1)
4629 seeksegment.stop = seeksegment.start + seeksegment.duration;
4631 if (!gst_base_parse_convert (parse, format, start_ts,
4632 GST_FORMAT_BYTES, &seekpos))
4633 goto convert_failed;
4634 if (!gst_base_parse_convert (parse, format, seeksegment.stop,
4635 GST_FORMAT_BYTES, &seekstop))
4636 goto convert_failed;
4639 GST_DEBUG_OBJECT (parse,
4640 "seek position %" G_GINT64_FORMAT " in bytes: %" G_GINT64_FORMAT,
4642 GST_DEBUG_OBJECT (parse,
4643 "seek stop %" G_GINT64_FORMAT " in bytes: %" G_GINT64_FORMAT,
4644 seeksegment.stop, seekstop);
4646 if (parse->priv->pad_mode == GST_PAD_MODE_PULL) {
4649 GST_DEBUG_OBJECT (parse, "seek in PULL mode");
4652 if (parse->srcpad) {
4653 GstEvent *fevent = gst_event_new_flush_start ();
4654 GST_DEBUG_OBJECT (parse, "sending flush start");
4656 gst_event_set_seqnum (fevent, seqnum);
4658 gst_pad_push_event (parse->srcpad, gst_event_ref (fevent));
4659 /* unlock upstream pull_range */
4660 gst_pad_push_event (parse->sinkpad, fevent);
4663 gst_pad_pause_task (parse->sinkpad);
4666 /* we should now be able to grab the streaming thread because we stopped it
4667 * with the above flush/pause code */
4668 GST_PAD_STREAM_LOCK (parse->sinkpad);
4670 /* save current position */
4671 last_stop = parse->segment.position;
4672 GST_DEBUG_OBJECT (parse, "stopped streaming at %" G_GINT64_FORMAT,
4675 /* now commit to new position */
4677 /* prepare for streaming again */
4679 GstEvent *fevent = gst_event_new_flush_stop (TRUE);
4680 GST_DEBUG_OBJECT (parse, "sending flush stop");
4681 gst_event_set_seqnum (fevent, seqnum);
4682 gst_pad_push_event (parse->srcpad, gst_event_ref (fevent));
4683 gst_pad_push_event (parse->sinkpad, fevent);
4684 gst_base_parse_clear_queues (parse);
4687 memcpy (&parse->segment, &seeksegment, sizeof (GstSegment));
4689 /* store the newsegment event so it can be sent from the streaming thread. */
4690 /* This will be sent later in _loop() */
4691 segment_event = gst_event_new_segment (&parse->segment);
4692 gst_event_set_seqnum (segment_event, seqnum);
4693 parse->priv->pending_events =
4694 g_list_prepend (parse->priv->pending_events, segment_event);
4696 GST_DEBUG_OBJECT (parse, "Created newseg format %d, "
4697 "start = %" GST_TIME_FORMAT ", stop = %" GST_TIME_FORMAT
4698 ", time = %" GST_TIME_FORMAT, format,
4699 GST_TIME_ARGS (parse->segment.start),
4700 GST_TIME_ARGS (parse->segment.stop),
4701 GST_TIME_ARGS (parse->segment.time));
4703 /* one last chance in pull mode to stay accurate;
4704 * maybe scan and subclass can find where to go */
4707 GstClockTime ts = seeksegment.position;
4709 gst_base_parse_locate_time (parse, &ts, &scanpos);
4713 /* running collected index now consists of several intervals,
4714 * so optimized check no longer possible */
4715 parse->priv->index_last_valid = FALSE;
4716 parse->priv->index_last_offset = 0;
4717 parse->priv->index_last_ts = 0;
4721 /* mark discont if we are going to stream from another position. */
4722 if (seekpos != parse->priv->offset) {
4723 GST_DEBUG_OBJECT (parse,
4724 "mark DISCONT, we did a seek to another position");
4725 parse->priv->offset = seekpos;
4726 parse->priv->last_offset = seekpos;
4727 parse->priv->seen_keyframe = FALSE;
4728 parse->priv->discont = TRUE;
4729 parse->priv->next_dts = start_ts;
4730 parse->priv->next_pts = GST_CLOCK_TIME_NONE;
4731 parse->priv->last_dts = GST_CLOCK_TIME_NONE;
4732 parse->priv->last_pts = GST_CLOCK_TIME_NONE;
4733 parse->priv->sync_offset = seekpos;
4734 parse->priv->exact_position = accurate;
4737 /* Start streaming thread if paused */
4738 gst_pad_start_task (parse->sinkpad,
4739 (GstTaskFunction) gst_base_parse_loop, parse->sinkpad, NULL);
4741 GST_PAD_STREAM_UNLOCK (parse->sinkpad);
4746 GstEvent *new_event;
4747 GstBaseParseSeek *seek;
4748 GstSeekFlags flags = (flush ? GST_SEEK_FLAG_FLUSH : GST_SEEK_FLAG_NONE);
4750 /* The only thing we need to do in PUSH-mode is to send the
4751 seek event (in bytes) to upstream. Segment / flush handling happens
4752 in corresponding src event handlers */
4753 GST_DEBUG_OBJECT (parse, "seek in PUSH mode");
4754 if (seekstop >= 0 && seekstop <= seekpos)
4756 new_event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags,
4757 GST_SEEK_TYPE_SET, seekpos, stop_type, seekstop);
4758 gst_event_set_seqnum (new_event, seqnum);
4760 /* store segment info so its precise details can be reconstructed when
4761 * receiving newsegment;
4762 * this matters for all details when accurate seeking,
4763 * is most useful to preserve NONE stop time otherwise */
4764 seek = g_new0 (GstBaseParseSeek, 1);
4765 seek->segment = seeksegment;
4766 seek->accurate = accurate;
4767 seek->offset = seekpos;
4768 seek->start_ts = start_ts;
4769 GST_OBJECT_LOCK (parse);
4770 /* less optimal, but preserves order */
4771 parse->priv->pending_seeks =
4772 g_slist_append (parse->priv->pending_seeks, seek);
4773 GST_OBJECT_UNLOCK (parse);
4775 res = gst_pad_push_event (parse->sinkpad, new_event);
4778 GST_OBJECT_LOCK (parse);
4779 parse->priv->pending_seeks =
4780 g_slist_remove (parse->priv->pending_seeks, seek);
4781 GST_OBJECT_UNLOCK (parse);
4787 gst_event_unref (event);
4793 GST_DEBUG_OBJECT (parse, "negative playback rates delegated upstream.");
4799 GST_DEBUG_OBJECT (parse, "unsupported seek type.");
4805 GST_DEBUG_OBJECT (parse, "seek in %s format was requested, but subclass "
4806 "couldn't convert that into TIME format", gst_format_get_name (format));
4812 GST_DEBUG_OBJECT (parse, "conversion TIME to BYTES failed.");
4819 gst_base_parse_set_upstream_tags (GstBaseParse * parse, GstTagList * taglist)
4821 if (taglist == parse->priv->upstream_tags)
4824 if (parse->priv->upstream_tags) {
4825 gst_tag_list_unref (parse->priv->upstream_tags);
4826 parse->priv->upstream_tags = NULL;
4829 GST_INFO_OBJECT (parse, "upstream tags: %" GST_PTR_FORMAT, taglist);
4831 if (taglist != NULL)
4832 parse->priv->upstream_tags = gst_tag_list_ref (taglist);
4834 gst_base_parse_check_bitrate_tags (parse);
4839 gst_base_parse_set_index (GstElement * element, GstIndex * index)
4841 GstBaseParse *parse = GST_BASE_PARSE (element);
4843 GST_BASE_PARSE_INDEX_LOCK (parse);
4844 if (parse->priv->index)
4845 gst_object_unref (parse->priv->index);
4847 parse->priv->index = gst_object_ref (index);
4848 gst_index_get_writer_id (index, GST_OBJECT_CAST (element),
4849 &parse->priv->index_id);
4850 parse->priv->own_index = FALSE;
4852 parse->priv->index = NULL;
4854 GST_BASE_PARSE_INDEX_UNLOCK (parse);
4858 gst_base_parse_get_index (GstElement * element)
4860 GstBaseParse *parse = GST_BASE_PARSE (element);
4861 GstIndex *result = NULL;
4863 GST_BASE_PARSE_INDEX_LOCK (parse);
4864 if (parse->priv->index)
4865 result = gst_object_ref (parse->priv->index);
4866 GST_BASE_PARSE_INDEX_UNLOCK (parse);
4872 static GstStateChangeReturn
4873 gst_base_parse_change_state (GstElement * element, GstStateChange transition)
4875 GstBaseParse *parse;
4876 GstStateChangeReturn result;
4878 parse = GST_BASE_PARSE (element);
4880 switch (transition) {
4881 case GST_STATE_CHANGE_READY_TO_PAUSED:
4882 /* If this is our own index destroy it as the
4883 * old entries might be wrong for the new stream */
4884 GST_BASE_PARSE_INDEX_LOCK (parse);
4885 if (parse->priv->own_index) {
4886 gst_object_unref (parse->priv->index);
4887 parse->priv->index = NULL;
4888 parse->priv->own_index = FALSE;
4891 /* If no index was created, generate one */
4892 if (G_UNLIKELY (!parse->priv->index)) {
4893 GST_DEBUG_OBJECT (parse, "no index provided creating our own");
4895 parse->priv->index = g_object_new (gst_mem_index_get_type (), NULL);
4896 gst_index_get_writer_id (parse->priv->index, GST_OBJECT (parse),
4897 &parse->priv->index_id);
4898 parse->priv->own_index = TRUE;
4900 GST_BASE_PARSE_INDEX_UNLOCK (parse);
4906 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4908 switch (transition) {
4909 case GST_STATE_CHANGE_PAUSED_TO_READY:
4910 gst_base_parse_reset (parse);
4920 * gst_base_parse_set_ts_at_offset:
4921 * @parse: a #GstBaseParse
4922 * @offset: offset into current buffer
4924 * This function should only be called from a @handle_frame implementation.
4926 * #GstBaseParse creates initial timestamps for frames by using the last
4927 * timestamp seen in the stream before the frame starts. In certain
4928 * cases, the correct timestamps will occur in the stream after the
4929 * start of the frame, but before the start of the actual picture data.
4930 * This function can be used to set the timestamps based on the offset
4931 * into the frame data that the picture starts.
4936 gst_base_parse_set_ts_at_offset (GstBaseParse * parse, gsize offset)
4938 GstClockTime pts, dts;
4940 g_return_if_fail (GST_IS_BASE_PARSE (parse));
4942 pts = gst_adapter_prev_pts_at_offset (parse->priv->adapter, offset, NULL);
4943 dts = gst_adapter_prev_dts_at_offset (parse->priv->adapter, offset, NULL);
4945 if (!GST_CLOCK_TIME_IS_VALID (pts) || !GST_CLOCK_TIME_IS_VALID (dts)) {
4946 GST_DEBUG_OBJECT (parse,
4947 "offset adapter timestamps dts=%" GST_TIME_FORMAT " pts=%"
4948 GST_TIME_FORMAT, GST_TIME_ARGS (dts), GST_TIME_ARGS (pts));
4950 if (GST_CLOCK_TIME_IS_VALID (pts) && (parse->priv->prev_pts != pts))
4951 parse->priv->prev_pts = parse->priv->next_pts = pts;
4953 if (GST_CLOCK_TIME_IS_VALID (dts) && (parse->priv->prev_dts != dts)) {
4954 parse->priv->prev_dts = parse->priv->next_dts = dts;
4955 parse->priv->prev_dts_from_pts = FALSE;
4960 * gst_base_parse_merge_tags:
4961 * @parse: a #GstBaseParse
4962 * @tags: (allow-none): a #GstTagList to merge, or NULL to unset
4963 * previously-set tags
4964 * @mode: the #GstTagMergeMode to use, usually #GST_TAG_MERGE_REPLACE
4966 * Sets the parser subclass's tags and how they should be merged with any
4967 * upstream stream tags. This will override any tags previously-set
4968 * with gst_base_parse_merge_tags().
4970 * Note that this is provided for convenience, and the subclass is
4971 * not required to use this and can still do tag handling on its own.
4976 gst_base_parse_merge_tags (GstBaseParse * parse, GstTagList * tags,
4977 GstTagMergeMode mode)
4979 g_return_if_fail (GST_IS_BASE_PARSE (parse));
4980 g_return_if_fail (tags == NULL || GST_IS_TAG_LIST (tags));
4981 g_return_if_fail (tags == NULL || mode != GST_TAG_MERGE_UNDEFINED);
4983 GST_OBJECT_LOCK (parse);
4985 if (tags != parse->priv->parser_tags) {
4986 if (parse->priv->parser_tags) {
4987 gst_tag_list_unref (parse->priv->parser_tags);
4988 parse->priv->parser_tags = NULL;
4989 parse->priv->parser_tags_merge_mode = GST_TAG_MERGE_APPEND;
4992 parse->priv->parser_tags = gst_tag_list_ref (tags);
4993 parse->priv->parser_tags_merge_mode = mode;
4996 GST_DEBUG_OBJECT (parse, "setting parser tags to %" GST_PTR_FORMAT
4997 " (mode %d)", tags, parse->priv->parser_tags_merge_mode);
4999 gst_base_parse_check_bitrate_tags (parse);
5000 parse->priv->tags_changed = TRUE;
5003 GST_OBJECT_UNLOCK (parse);