2 * Copyright (C) 2005-2007 Wim Taymans <wim.taymans@gmail.com>
4 * gstbasesink.c: Base class for sink elements
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
24 * @short_description: Base class for sink elements
25 * @see_also: #GstBaseTransform, #GstBaseSource
27 * #GstBaseSink is the base class for sink elements in GStreamer, such as
28 * xvimagesink or filesink. It is a layer on top of #GstElement that provides a
29 * simplified interface to plugin writers. #GstBaseSink handles many details
30 * for you, for example: preroll, clock synchronization, state changes,
31 * activation in push or pull mode, and queries.
33 * In most cases, when writing sink elements, there is no need to implement
34 * class methods from #GstElement or to set functions on pads, because the
35 * #GstBaseSink infrastructure should be sufficient.
37 * #GstBaseSink provides support for exactly one sink pad, which should be
38 * named "sink". A sink implementation (subclass of #GstBaseSink) should
39 * install a pad template in its base_init function, like so:
42 * my_element_base_init (gpointer g_class)
44 * GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
46 * // sinktemplate should be a #GstStaticPadTemplate with direction
47 * // #GST_PAD_SINK and name "sink"
48 * gst_element_class_add_pad_template (gstelement_class,
49 * gst_static_pad_template_get (&sinktemplate));
50 * // see #GstElementDetails
51 * gst_element_class_set_details (gstelement_class, &details);
55 * #GstBaseSink will handle the prerolling correctly. This means that it will
56 * return #GST_STATE_CHANGE_ASYNC from a state change to PAUSED until the first
57 * buffer arrives in this element. The base class will call the
58 * #GstBaseSink::preroll vmethod with this preroll buffer and will then commit
59 * the state change to the next asynchronously pending state.
61 * When the element is set to PLAYING, #GstBaseSink will synchronise on the
62 * clock using the times returned from ::get_times. If this function returns
63 * #GST_CLOCK_TIME_NONE for the start time, no synchronisation will be done.
64 * Synchronisation can be disabled entirely by setting the object "sync"
67 * After synchronisation the virtual method #GstBaseSink::render will be called.
68 * Subclasses should minimally implement this method.
70 * Since 0.10.3 subclasses that synchronise on the clock in the ::render method
71 * are supported as well. These classes typically receive a buffer in the render
72 * method and can then potentially block on the clock while rendering. A typical
73 * example is an audiosink. Since 0.10.11 these subclasses can use
74 * gst_base_sink_wait_preroll() to perform the blocking wait.
76 * Upon receiving the EOS event in the PLAYING state, #GstBaseSink will wait
77 * for the clock to reach the time indicated by the stop time of the last
78 * ::get_times call before posting an EOS message. When the element receives
79 * EOS in PAUSED, preroll completes, the event is queued and an EOS message is
80 * posted when going to PLAYING.
82 * #GstBaseSink will internally use the #GST_EVENT_NEWSEGMENT events to schedule
83 * synchronisation and clipping of buffers. Buffers that fall completely outside
84 * of the current segment are dropped. Buffers that fall partially in the
85 * segment are rendered (and prerolled). Subclasses should do any subbuffer
86 * clipping themselves when needed.
88 * #GstBaseSink will by default report the current playback position in
89 * #GST_FORMAT_TIME based on the current clock time and segment information.
90 * If no clock has been set on the element, the query will be forwarded
93 * The ::set_caps function will be called when the subclass should configure
94 * itself to process a specific media type.
96 * The ::start and ::stop virtual methods will be called when resources should
97 * be allocated. Any ::preroll, ::render and ::set_caps function will be
98 * called between the ::start and ::stop calls.
100 * The ::event virtual method will be called when an event is received by
101 * #GstBaseSink. Normally this method should only be overriden by very specific
102 * elements (such as file sinks) which need to handle the newsegment event
105 * #GstBaseSink provides an overridable ::buffer_alloc function that can be
106 * used by sinks that want to do reverse negotiation or to provide
107 * custom buffers (hardware buffers for example) to upstream elements.
109 * The ::unlock method is called when the elements should unblock any blocking
110 * operations they perform in the ::render method. This is mostly useful when
111 * the ::render method performs a blocking write on a file descriptor, for
114 * The max-lateness property affects how the sink deals with buffers that
115 * arrive too late in the sink. A buffer arrives too late in the sink when
116 * the presentation time (as a combination of the last segment, buffer
117 * timestamp and element base_time) plus the duration is before the current
119 * If the frame is later than max-lateness, the sink will drop the buffer
120 * without calling the render method.
121 * This feature is disabled if sync is disabled, the ::get-times method does
122 * not return a valid start time or max-lateness is set to -1 (the default).
123 * Subclasses can use gst_base_sink_set_max_lateness() to configure the
124 * max-lateness value.
126 * The qos property will enable the quality-of-service features of the basesink
127 * which gather statistics about the real-time performance of the clock
128 * synchronisation. For each buffer received in the sink, statistics are
129 * gathered and a QOS event is sent upstream with these numbers. This
130 * information can then be used by upstream elements to reduce their processing
133 * Since 0.10.15 the async property can be used to instruct the sink to never
134 * perform an ASYNC state change. This feature is mostly usable when dealing
135 * with non-synchronized streams or sparse streams.
137 * Last reviewed on 2007-08-29 (0.10.15)
144 #include "gstbasesink.h"
145 #include <gst/gstmarshal.h>
146 #include <gst/gst_private.h>
147 #include <gst/gst-i18n-lib.h>
149 GST_DEBUG_CATEGORY_STATIC (gst_base_sink_debug);
150 #define GST_CAT_DEFAULT gst_base_sink_debug
152 #define GST_BASE_SINK_GET_PRIVATE(obj) \
153 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_SINK, GstBaseSinkPrivate))
157 gboolean valid; /* if this info is valid */
158 guint32 seqnum; /* the seqnum of the STEP event */
159 GstFormat format; /* the format of the amount */
160 guint64 amount; /* the total amount of data to skip */
161 guint64 position; /* the position in the stepped data */
162 guint64 duration; /* the duration in time of the skipped data */
163 guint64 start; /* running_time of the start */
164 gdouble rate; /* rate of skipping */
165 gboolean flush; /* if this was a flushing step */
166 gboolean intermediate; /* if this is an intermediate step */
167 gboolean need_preroll; /* if we need preroll after this step */
170 /* FIXME, some stuff in ABI.data and other in Private...
171 * Make up your mind please.
173 struct _GstBaseSinkPrivate
175 gint qos_enabled; /* ATOMIC */
176 gboolean async_enabled;
177 GstClockTimeDiff ts_offset;
178 GstClockTime render_delay;
180 /* start, stop of current buffer, stream time, used to report position */
181 GstClockTime current_sstart;
182 GstClockTime current_sstop;
184 /* start, stop and jitter of current buffer, running time */
185 GstClockTime current_rstart;
186 GstClockTime current_rstop;
187 GstClockTimeDiff current_jitter;
189 /* EOS sync time in running time */
190 GstClockTime eos_rtime;
192 /* last buffer that arrived in time, running time */
193 GstClockTime last_in_time;
194 /* when the last buffer left the sink, running time */
195 GstClockTime last_left;
197 /* running averages go here these are done on running time */
199 GstClockTime avg_duration;
202 /* these are done on system time. avg_jitter and avg_render are
203 * compared to eachother to see if the rendering time takes a
204 * huge amount of the processing, If so we are flooded with
206 GstClockTime last_left_systime;
207 GstClockTime avg_jitter;
208 GstClockTime start, stop;
209 GstClockTime avg_render;
211 /* number of rendered and dropped frames */
216 GstClockTime latency;
218 /* if we already commited the state */
221 /* when we received EOS */
222 gboolean received_eos;
224 /* when we are prerolled and able to report latency */
225 gboolean have_latency;
227 /* the last buffer we prerolled or rendered. Useful for making snapshots */
228 GstBuffer *last_buffer;
230 /* caps for pull based scheduling */
233 /* blocksize for pulling */
238 /* seqnum of the stream */
241 gboolean call_preroll;
243 /* we have a pending and a current step operation */
244 GstStepInfo current_step;
245 GstStepInfo pending_step;
248 #define DO_RUNNING_AVG(avg,val,size) (((val) + ((size)-1) * (avg)) / (size))
250 /* generic running average, this has a neutral window size */
251 #define UPDATE_RUNNING_AVG(avg,val) DO_RUNNING_AVG(avg,val,8)
253 /* the windows for these running averages are experimentally obtained.
254 * possitive values get averaged more while negative values use a small
255 * window so we can react faster to badness. */
256 #define UPDATE_RUNNING_AVG_P(avg,val) DO_RUNNING_AVG(avg,val,16)
257 #define UPDATE_RUNNING_AVG_N(avg,val) DO_RUNNING_AVG(avg,val,4)
259 /* BaseSink properties */
261 #define DEFAULT_CAN_ACTIVATE_PULL FALSE /* fixme: enable me */
262 #define DEFAULT_CAN_ACTIVATE_PUSH TRUE
264 #define DEFAULT_PREROLL_QUEUE_LEN 0
265 #define DEFAULT_SYNC TRUE
266 #define DEFAULT_MAX_LATENESS -1
267 #define DEFAULT_QOS FALSE
268 #define DEFAULT_ASYNC TRUE
269 #define DEFAULT_TS_OFFSET 0
270 #define DEFAULT_BLOCKSIZE 4096
271 #define DEFAULT_RENDER_DELAY 0
276 PROP_PREROLL_QUEUE_LEN,
288 static GstElementClass *parent_class = NULL;
290 static void gst_base_sink_class_init (GstBaseSinkClass * klass);
291 static void gst_base_sink_init (GstBaseSink * trans, gpointer g_class);
292 static void gst_base_sink_finalize (GObject * object);
295 gst_base_sink_get_type (void)
297 static volatile gsize base_sink_type = 0;
299 if (g_once_init_enter (&base_sink_type)) {
301 static const GTypeInfo base_sink_info = {
302 sizeof (GstBaseSinkClass),
305 (GClassInitFunc) gst_base_sink_class_init,
308 sizeof (GstBaseSink),
310 (GInstanceInitFunc) gst_base_sink_init,
313 _type = g_type_register_static (GST_TYPE_ELEMENT,
314 "GstBaseSink", &base_sink_info, G_TYPE_FLAG_ABSTRACT);
315 g_once_init_leave (&base_sink_type, _type);
317 return base_sink_type;
320 static void gst_base_sink_set_property (GObject * object, guint prop_id,
321 const GValue * value, GParamSpec * pspec);
322 static void gst_base_sink_get_property (GObject * object, guint prop_id,
323 GValue * value, GParamSpec * pspec);
325 static gboolean gst_base_sink_send_event (GstElement * element,
327 static gboolean gst_base_sink_query (GstElement * element, GstQuery * query);
329 static GstCaps *gst_base_sink_get_caps (GstBaseSink * sink);
330 static gboolean gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps);
331 static GstFlowReturn gst_base_sink_buffer_alloc (GstBaseSink * sink,
332 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
333 static void gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
334 GstClockTime * start, GstClockTime * end);
335 static gboolean gst_base_sink_set_flushing (GstBaseSink * basesink,
336 GstPad * pad, gboolean flushing);
337 static gboolean gst_base_sink_default_activate_pull (GstBaseSink * basesink,
339 static gboolean gst_base_sink_default_do_seek (GstBaseSink * sink,
340 GstSegment * segment);
341 static gboolean gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
342 GstEvent * event, GstSegment * segment);
344 static GstStateChangeReturn gst_base_sink_change_state (GstElement * element,
345 GstStateChange transition);
347 static GstFlowReturn gst_base_sink_chain (GstPad * pad, GstBuffer * buffer);
348 static void gst_base_sink_loop (GstPad * pad);
349 static gboolean gst_base_sink_pad_activate (GstPad * pad);
350 static gboolean gst_base_sink_pad_activate_push (GstPad * pad, gboolean active);
351 static gboolean gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active);
352 static gboolean gst_base_sink_event (GstPad * pad, GstEvent * event);
353 static gboolean gst_base_sink_peer_query (GstBaseSink * sink, GstQuery * query);
355 static gboolean gst_base_sink_negotiate_pull (GstBaseSink * basesink);
357 /* check if an object was too late */
358 static gboolean gst_base_sink_is_too_late (GstBaseSink * basesink,
359 GstMiniObject * obj, GstClockTime start, GstClockTime stop,
360 GstClockReturn status, GstClockTimeDiff jitter);
361 static GstFlowReturn gst_base_sink_preroll_object (GstBaseSink * basesink,
362 GstMiniObject * obj);
365 gst_base_sink_class_init (GstBaseSinkClass * klass)
367 GObjectClass *gobject_class;
368 GstElementClass *gstelement_class;
370 gobject_class = G_OBJECT_CLASS (klass);
371 gstelement_class = GST_ELEMENT_CLASS (klass);
373 GST_DEBUG_CATEGORY_INIT (gst_base_sink_debug, "basesink", 0,
376 g_type_class_add_private (klass, sizeof (GstBaseSinkPrivate));
378 parent_class = g_type_class_peek_parent (klass);
380 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_sink_finalize);
381 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_base_sink_set_property);
382 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_base_sink_get_property);
384 /* FIXME, this next value should be configured using an event from the
385 * upstream element, ie, the BUFFER_SIZE event. */
386 g_object_class_install_property (gobject_class, PROP_PREROLL_QUEUE_LEN,
387 g_param_spec_uint ("preroll-queue-len", "Preroll queue length",
388 "Number of buffers to queue during preroll", 0, G_MAXUINT,
389 DEFAULT_PREROLL_QUEUE_LEN,
390 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
392 g_object_class_install_property (gobject_class, PROP_SYNC,
393 g_param_spec_boolean ("sync", "Sync", "Sync on the clock", DEFAULT_SYNC,
394 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
396 g_object_class_install_property (gobject_class, PROP_MAX_LATENESS,
397 g_param_spec_int64 ("max-lateness", "Max Lateness",
398 "Maximum number of nanoseconds that a buffer can be late before it "
399 "is dropped (-1 unlimited)", -1, G_MAXINT64, DEFAULT_MAX_LATENESS,
400 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
402 g_object_class_install_property (gobject_class, PROP_QOS,
403 g_param_spec_boolean ("qos", "Qos",
404 "Generate Quality-of-Service events upstream", DEFAULT_QOS,
405 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
409 * If set to #TRUE, the basesink will perform asynchronous state changes.
410 * When set to #FALSE, the sink will not signal the parent when it prerolls.
411 * Use this option when dealing with sparse streams or when synchronisation is
416 g_object_class_install_property (gobject_class, PROP_ASYNC,
417 g_param_spec_boolean ("async", "Async",
418 "Go asynchronously to PAUSED", DEFAULT_ASYNC,
419 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
421 * GstBaseSink:ts-offset
423 * Controls the final synchronisation, a negative value will render the buffer
424 * earlier while a positive value delays playback. This property can be
425 * used to fix synchronisation in bad files.
429 g_object_class_install_property (gobject_class, PROP_TS_OFFSET,
430 g_param_spec_int64 ("ts-offset", "TS Offset",
431 "Timestamp offset in nanoseconds", G_MININT64, G_MAXINT64,
432 DEFAULT_TS_OFFSET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
434 * GstBaseSink:last-buffer
436 * The last buffer that arrived in the sink and was used for preroll or for
437 * rendering. This property can be used to generate thumbnails. This property
438 * can be NULL when the sink has not yet received a bufer.
442 g_object_class_install_property (gobject_class, PROP_LAST_BUFFER,
443 gst_param_spec_mini_object ("last-buffer", "Last Buffer",
444 "The last buffer received in the sink", GST_TYPE_BUFFER,
445 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
447 * GstBaseSink:blocksize
449 * The amount of bytes to pull when operating in pull mode.
453 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
454 g_param_spec_uint ("blocksize", "Block size",
455 "Size in bytes to pull per buffer (0 = default)", 0, G_MAXUINT,
456 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
458 * GstBaseSink:render-delay
460 * The additional delay between synchronisation and actual rendering of the
461 * media. This property will add additional latency to the device in order to
462 * make other sinks compensate for the delay.
466 g_object_class_install_property (gobject_class, PROP_RENDER_DELAY,
467 g_param_spec_uint64 ("render-delay", "Render Delay",
468 "Additional render delay of the sink in nanoseconds", 0, G_MAXUINT64,
469 DEFAULT_RENDER_DELAY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
471 gstelement_class->change_state =
472 GST_DEBUG_FUNCPTR (gst_base_sink_change_state);
473 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_sink_send_event);
474 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_base_sink_query);
476 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_sink_get_caps);
477 klass->set_caps = GST_DEBUG_FUNCPTR (gst_base_sink_set_caps);
478 klass->buffer_alloc = GST_DEBUG_FUNCPTR (gst_base_sink_buffer_alloc);
479 klass->get_times = GST_DEBUG_FUNCPTR (gst_base_sink_get_times);
480 klass->activate_pull =
481 GST_DEBUG_FUNCPTR (gst_base_sink_default_activate_pull);
485 gst_base_sink_pad_getcaps (GstPad * pad)
487 GstBaseSinkClass *bclass;
489 GstCaps *caps = NULL;
491 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
492 bclass = GST_BASE_SINK_GET_CLASS (bsink);
494 if (bsink->pad_mode == GST_ACTIVATE_PULL) {
495 /* if we are operating in pull mode we only accept the negotiated caps */
496 GST_OBJECT_LOCK (pad);
497 if ((caps = GST_PAD_CAPS (pad)))
499 GST_OBJECT_UNLOCK (pad);
502 if (bclass->get_caps)
503 caps = bclass->get_caps (bsink);
506 GstPadTemplate *pad_template;
509 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass),
511 if (pad_template != NULL) {
512 caps = gst_caps_ref (gst_pad_template_get_caps (pad_template));
516 gst_object_unref (bsink);
522 gst_base_sink_pad_setcaps (GstPad * pad, GstCaps * caps)
524 GstBaseSinkClass *bclass;
528 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
529 bclass = GST_BASE_SINK_GET_CLASS (bsink);
531 if (res && bclass->set_caps)
532 res = bclass->set_caps (bsink, caps);
534 gst_object_unref (bsink);
540 gst_base_sink_pad_fixate (GstPad * pad, GstCaps * caps)
542 GstBaseSinkClass *bclass;
545 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
546 bclass = GST_BASE_SINK_GET_CLASS (bsink);
549 bclass->fixate (bsink, caps);
551 gst_object_unref (bsink);
555 gst_base_sink_pad_buffer_alloc (GstPad * pad, guint64 offset, guint size,
556 GstCaps * caps, GstBuffer ** buf)
558 GstBaseSinkClass *bclass;
560 GstFlowReturn result = GST_FLOW_OK;
562 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
563 bclass = GST_BASE_SINK_GET_CLASS (bsink);
565 if (bclass->buffer_alloc)
566 result = bclass->buffer_alloc (bsink, offset, size, caps, buf);
568 *buf = NULL; /* fallback in gstpad.c will allocate generic buffer */
570 gst_object_unref (bsink);
576 gst_base_sink_init (GstBaseSink * basesink, gpointer g_class)
578 GstPadTemplate *pad_template;
579 GstBaseSinkPrivate *priv;
581 basesink->priv = priv = GST_BASE_SINK_GET_PRIVATE (basesink);
584 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
585 g_return_if_fail (pad_template != NULL);
587 basesink->sinkpad = gst_pad_new_from_template (pad_template, "sink");
589 gst_pad_set_getcaps_function (basesink->sinkpad,
590 GST_DEBUG_FUNCPTR (gst_base_sink_pad_getcaps));
591 gst_pad_set_setcaps_function (basesink->sinkpad,
592 GST_DEBUG_FUNCPTR (gst_base_sink_pad_setcaps));
593 gst_pad_set_fixatecaps_function (basesink->sinkpad,
594 GST_DEBUG_FUNCPTR (gst_base_sink_pad_fixate));
595 gst_pad_set_bufferalloc_function (basesink->sinkpad,
596 GST_DEBUG_FUNCPTR (gst_base_sink_pad_buffer_alloc));
597 gst_pad_set_activate_function (basesink->sinkpad,
598 GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate));
599 gst_pad_set_activatepush_function (basesink->sinkpad,
600 GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate_push));
601 gst_pad_set_activatepull_function (basesink->sinkpad,
602 GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate_pull));
603 gst_pad_set_event_function (basesink->sinkpad,
604 GST_DEBUG_FUNCPTR (gst_base_sink_event));
605 gst_pad_set_chain_function (basesink->sinkpad,
606 GST_DEBUG_FUNCPTR (gst_base_sink_chain));
607 gst_element_add_pad (GST_ELEMENT_CAST (basesink), basesink->sinkpad);
609 basesink->pad_mode = GST_ACTIVATE_NONE;
610 basesink->preroll_queue = g_queue_new ();
611 basesink->abidata.ABI.clip_segment = gst_segment_new ();
612 priv->have_latency = FALSE;
614 basesink->can_activate_push = DEFAULT_CAN_ACTIVATE_PUSH;
615 basesink->can_activate_pull = DEFAULT_CAN_ACTIVATE_PULL;
617 basesink->sync = DEFAULT_SYNC;
618 basesink->abidata.ABI.max_lateness = DEFAULT_MAX_LATENESS;
619 g_atomic_int_set (&priv->qos_enabled, DEFAULT_QOS);
620 priv->async_enabled = DEFAULT_ASYNC;
621 priv->ts_offset = DEFAULT_TS_OFFSET;
622 priv->render_delay = DEFAULT_RENDER_DELAY;
623 priv->blocksize = DEFAULT_BLOCKSIZE;
625 GST_OBJECT_FLAG_SET (basesink, GST_ELEMENT_IS_SINK);
629 gst_base_sink_finalize (GObject * object)
631 GstBaseSink *basesink;
633 basesink = GST_BASE_SINK (object);
635 g_queue_free (basesink->preroll_queue);
636 gst_segment_free (basesink->abidata.ABI.clip_segment);
638 G_OBJECT_CLASS (parent_class)->finalize (object);
642 * gst_base_sink_set_sync:
644 * @sync: the new sync value.
646 * Configures @sink to synchronize on the clock or not. When
647 * @sync is FALSE, incomming samples will be played as fast as
648 * possible. If @sync is TRUE, the timestamps of the incomming
649 * buffers will be used to schedule the exact render time of its
655 gst_base_sink_set_sync (GstBaseSink * sink, gboolean sync)
657 g_return_if_fail (GST_IS_BASE_SINK (sink));
659 GST_OBJECT_LOCK (sink);
661 GST_OBJECT_UNLOCK (sink);
665 * gst_base_sink_get_sync:
668 * Checks if @sink is currently configured to synchronize against the
671 * Returns: TRUE if the sink is configured to synchronize against the clock.
676 gst_base_sink_get_sync (GstBaseSink * sink)
680 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
682 GST_OBJECT_LOCK (sink);
684 GST_OBJECT_UNLOCK (sink);
690 * gst_base_sink_set_max_lateness:
692 * @max_lateness: the new max lateness value.
694 * Sets the new max lateness value to @max_lateness. This value is
695 * used to decide if a buffer should be dropped or not based on the
696 * buffer timestamp and the current clock time. A value of -1 means
702 gst_base_sink_set_max_lateness (GstBaseSink * sink, gint64 max_lateness)
704 g_return_if_fail (GST_IS_BASE_SINK (sink));
706 GST_OBJECT_LOCK (sink);
707 sink->abidata.ABI.max_lateness = max_lateness;
708 GST_OBJECT_UNLOCK (sink);
712 * gst_base_sink_get_max_lateness:
715 * Gets the max lateness value. See gst_base_sink_set_max_lateness for
718 * Returns: The maximum time in nanoseconds that a buffer can be late
719 * before it is dropped and not rendered. A value of -1 means an
725 gst_base_sink_get_max_lateness (GstBaseSink * sink)
729 g_return_val_if_fail (GST_IS_BASE_SINK (sink), -1);
731 GST_OBJECT_LOCK (sink);
732 res = sink->abidata.ABI.max_lateness;
733 GST_OBJECT_UNLOCK (sink);
739 * gst_base_sink_set_qos_enabled:
741 * @enabled: the new qos value.
743 * Configures @sink to send Quality-of-Service events upstream.
748 gst_base_sink_set_qos_enabled (GstBaseSink * sink, gboolean enabled)
750 g_return_if_fail (GST_IS_BASE_SINK (sink));
752 g_atomic_int_set (&sink->priv->qos_enabled, enabled);
756 * gst_base_sink_is_qos_enabled:
759 * Checks if @sink is currently configured to send Quality-of-Service events
762 * Returns: TRUE if the sink is configured to perform Quality-of-Service.
767 gst_base_sink_is_qos_enabled (GstBaseSink * sink)
771 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
773 res = g_atomic_int_get (&sink->priv->qos_enabled);
779 * gst_base_sink_set_async_enabled:
781 * @enabled: the new async value.
783 * Configures @sink to perform all state changes asynchronusly. When async is
784 * disabled, the sink will immediatly go to PAUSED instead of waiting for a
785 * preroll buffer. This feature is usefull if the sink does not synchronize
786 * against the clock or when it is dealing with sparse streams.
791 gst_base_sink_set_async_enabled (GstBaseSink * sink, gboolean enabled)
793 g_return_if_fail (GST_IS_BASE_SINK (sink));
795 GST_PAD_PREROLL_LOCK (sink->sinkpad);
796 sink->priv->async_enabled = enabled;
797 GST_LOG_OBJECT (sink, "set async enabled to %d", enabled);
798 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
802 * gst_base_sink_is_async_enabled:
805 * Checks if @sink is currently configured to perform asynchronous state
808 * Returns: TRUE if the sink is configured to perform asynchronous state
814 gst_base_sink_is_async_enabled (GstBaseSink * sink)
818 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
820 GST_PAD_PREROLL_LOCK (sink->sinkpad);
821 res = sink->priv->async_enabled;
822 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
828 * gst_base_sink_set_ts_offset:
830 * @offset: the new offset
832 * Adjust the synchronisation of @sink with @offset. A negative value will
833 * render buffers earlier than their timestamp. A positive value will delay
834 * rendering. This function can be used to fix playback of badly timestamped
840 gst_base_sink_set_ts_offset (GstBaseSink * sink, GstClockTimeDiff offset)
842 g_return_if_fail (GST_IS_BASE_SINK (sink));
844 GST_OBJECT_LOCK (sink);
845 sink->priv->ts_offset = offset;
846 GST_LOG_OBJECT (sink, "set time offset to %" G_GINT64_FORMAT, offset);
847 GST_OBJECT_UNLOCK (sink);
851 * gst_base_sink_get_ts_offset:
854 * Get the synchronisation offset of @sink.
856 * Returns: The synchronisation offset.
861 gst_base_sink_get_ts_offset (GstBaseSink * sink)
863 GstClockTimeDiff res;
865 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
867 GST_OBJECT_LOCK (sink);
868 res = sink->priv->ts_offset;
869 GST_OBJECT_UNLOCK (sink);
875 * gst_base_sink_get_last_buffer:
878 * Get the last buffer that arrived in the sink and was used for preroll or for
879 * rendering. This property can be used to generate thumbnails.
881 * The #GstCaps on the buffer can be used to determine the type of the buffer.
883 * Returns: a #GstBuffer. gst_buffer_unref() after usage. This function returns
884 * NULL when no buffer has arrived in the sink yet or when the sink is not in
890 gst_base_sink_get_last_buffer (GstBaseSink * sink)
894 g_return_val_if_fail (GST_IS_BASE_SINK (sink), NULL);
896 GST_OBJECT_LOCK (sink);
897 if ((res = sink->priv->last_buffer))
898 gst_buffer_ref (res);
899 GST_OBJECT_UNLOCK (sink);
905 gst_base_sink_set_last_buffer (GstBaseSink * sink, GstBuffer * buffer)
909 GST_OBJECT_LOCK (sink);
910 old = sink->priv->last_buffer;
911 if (G_LIKELY (old != buffer)) {
912 GST_DEBUG_OBJECT (sink, "setting last buffer to %p", buffer);
913 if (G_LIKELY (buffer))
914 gst_buffer_ref (buffer);
915 sink->priv->last_buffer = buffer;
919 GST_OBJECT_UNLOCK (sink);
921 /* avoid unreffing with the lock because cleanup code might want to take the
924 gst_buffer_unref (old);
928 * gst_base_sink_get_latency:
931 * Get the currently configured latency.
933 * Returns: The configured latency.
938 gst_base_sink_get_latency (GstBaseSink * sink)
942 GST_OBJECT_LOCK (sink);
943 res = sink->priv->latency;
944 GST_OBJECT_UNLOCK (sink);
950 * gst_base_sink_query_latency:
952 * @live: if the sink is live
953 * @upstream_live: if an upstream element is live
954 * @min_latency: the min latency of the upstream elements
955 * @max_latency: the max latency of the upstream elements
957 * Query the sink for the latency parameters. The latency will be queried from
958 * the upstream elements. @live will be TRUE if @sink is configured to
959 * synchronize against the clock. @upstream_live will be TRUE if an upstream
962 * If both @live and @upstream_live are TRUE, the sink will want to compensate
963 * for the latency introduced by the upstream elements by setting the
964 * @min_latency to a strictly possitive value.
966 * This function is mostly used by subclasses.
968 * Returns: TRUE if the query succeeded.
973 gst_base_sink_query_latency (GstBaseSink * sink, gboolean * live,
974 gboolean * upstream_live, GstClockTime * min_latency,
975 GstClockTime * max_latency)
977 gboolean l, us_live, res, have_latency;
978 GstClockTime min, max, render_delay;
980 GstClockTime us_min, us_max;
982 /* we are live when we sync to the clock */
983 GST_OBJECT_LOCK (sink);
985 have_latency = sink->priv->have_latency;
986 render_delay = sink->priv->render_delay;
987 GST_OBJECT_UNLOCK (sink);
989 /* assume no latency */
995 GST_DEBUG_OBJECT (sink, "we are ready for LATENCY query");
996 /* we are ready for a latency query this is when we preroll or when we are
998 query = gst_query_new_latency ();
1000 /* ask the peer for the latency */
1001 if ((res = gst_base_sink_peer_query (sink, query))) {
1002 /* get upstream min and max latency */
1003 gst_query_parse_latency (query, &us_live, &us_min, &us_max);
1006 /* upstream live, use its latency, subclasses should use these
1007 * values to create the complete latency. */
1012 /* we need to add the render delay if we are live */
1014 min += render_delay;
1016 max += render_delay;
1019 gst_query_unref (query);
1021 GST_DEBUG_OBJECT (sink, "we are not yet ready for LATENCY query");
1025 /* not live, we tried to do the query, if it failed we return TRUE anyway */
1029 GST_DEBUG_OBJECT (sink, "latency query failed but we are not live");
1031 GST_DEBUG_OBJECT (sink, "latency query failed and we are live");
1036 GST_DEBUG_OBJECT (sink, "latency query: live: %d, have_latency %d,"
1037 " upstream: %d, min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT, l,
1038 have_latency, us_live, GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1043 *upstream_live = us_live;
1053 * gst_base_sink_set_render_delay:
1054 * @sink: a #GstBaseSink
1055 * @delay: the new delay
1057 * Set the render delay in @sink to @delay. The render delay is the time
1058 * between actual rendering of a buffer and its synchronisation time. Some
1059 * devices might delay media rendering which can be compensated for with this
1062 * After calling this function, this sink will report additional latency and
1063 * other sinks will adjust their latency to delay the rendering of their media.
1065 * This function is usually called by subclasses.
1070 gst_base_sink_set_render_delay (GstBaseSink * sink, GstClockTime delay)
1072 GstClockTime old_render_delay;
1074 g_return_if_fail (GST_IS_BASE_SINK (sink));
1076 GST_OBJECT_LOCK (sink);
1077 old_render_delay = sink->priv->render_delay;
1078 sink->priv->render_delay = delay;
1079 GST_LOG_OBJECT (sink, "set render delay to %" GST_TIME_FORMAT,
1080 GST_TIME_ARGS (delay));
1081 GST_OBJECT_UNLOCK (sink);
1083 if (delay != old_render_delay) {
1084 GST_DEBUG_OBJECT (sink, "posting latency changed");
1085 gst_element_post_message (GST_ELEMENT_CAST (sink),
1086 gst_message_new_latency (GST_OBJECT_CAST (sink)));
1091 * gst_base_sink_get_render_delay:
1092 * @sink: a #GstBaseSink
1094 * Get the render delay of @sink. see gst_base_sink_set_render_delay() for more
1095 * information about the render delay.
1097 * Returns: the render delay of @sink.
1102 gst_base_sink_get_render_delay (GstBaseSink * sink)
1104 GstClockTimeDiff res;
1106 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1108 GST_OBJECT_LOCK (sink);
1109 res = sink->priv->render_delay;
1110 GST_OBJECT_UNLOCK (sink);
1116 * gst_base_sink_set_blocksize:
1117 * @sink: a #GstBaseSink
1118 * @blocksize: the blocksize in bytes
1120 * Set the number of bytes that the sink will pull when it is operating in pull
1126 gst_base_sink_set_blocksize (GstBaseSink * sink, guint blocksize)
1128 g_return_if_fail (GST_IS_BASE_SINK (sink));
1130 GST_OBJECT_LOCK (sink);
1131 sink->priv->blocksize = blocksize;
1132 GST_LOG_OBJECT (sink, "set blocksize to %u", blocksize);
1133 GST_OBJECT_UNLOCK (sink);
1137 * gst_base_sink_get_blocksize:
1138 * @sink: a #GstBaseSink
1140 * Get the number of bytes that the sink will pull when it is operating in pull
1143 * Returns: the number of bytes @sink will pull in pull mode.
1148 gst_base_sink_get_blocksize (GstBaseSink * sink)
1152 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1154 GST_OBJECT_LOCK (sink);
1155 res = sink->priv->blocksize;
1156 GST_OBJECT_UNLOCK (sink);
1162 gst_base_sink_set_property (GObject * object, guint prop_id,
1163 const GValue * value, GParamSpec * pspec)
1165 GstBaseSink *sink = GST_BASE_SINK (object);
1168 case PROP_PREROLL_QUEUE_LEN:
1169 /* preroll lock necessary to serialize with finish_preroll */
1170 GST_PAD_PREROLL_LOCK (sink->sinkpad);
1171 sink->preroll_queue_max_len = g_value_get_uint (value);
1172 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
1175 gst_base_sink_set_sync (sink, g_value_get_boolean (value));
1177 case PROP_MAX_LATENESS:
1178 gst_base_sink_set_max_lateness (sink, g_value_get_int64 (value));
1181 gst_base_sink_set_qos_enabled (sink, g_value_get_boolean (value));
1184 gst_base_sink_set_async_enabled (sink, g_value_get_boolean (value));
1186 case PROP_TS_OFFSET:
1187 gst_base_sink_set_ts_offset (sink, g_value_get_int64 (value));
1189 case PROP_BLOCKSIZE:
1190 gst_base_sink_set_blocksize (sink, g_value_get_uint (value));
1192 case PROP_RENDER_DELAY:
1193 gst_base_sink_set_render_delay (sink, g_value_get_uint64 (value));
1196 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1202 gst_base_sink_get_property (GObject * object, guint prop_id, GValue * value,
1205 GstBaseSink *sink = GST_BASE_SINK (object);
1208 case PROP_PREROLL_QUEUE_LEN:
1209 GST_PAD_PREROLL_LOCK (sink->sinkpad);
1210 g_value_set_uint (value, sink->preroll_queue_max_len);
1211 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
1214 g_value_set_boolean (value, gst_base_sink_get_sync (sink));
1216 case PROP_MAX_LATENESS:
1217 g_value_set_int64 (value, gst_base_sink_get_max_lateness (sink));
1220 g_value_set_boolean (value, gst_base_sink_is_qos_enabled (sink));
1223 g_value_set_boolean (value, gst_base_sink_is_async_enabled (sink));
1225 case PROP_TS_OFFSET:
1226 g_value_set_int64 (value, gst_base_sink_get_ts_offset (sink));
1228 case PROP_LAST_BUFFER:
1229 gst_value_take_buffer (value, gst_base_sink_get_last_buffer (sink));
1231 case PROP_BLOCKSIZE:
1232 g_value_set_uint (value, gst_base_sink_get_blocksize (sink));
1234 case PROP_RENDER_DELAY:
1235 g_value_set_uint64 (value, gst_base_sink_get_render_delay (sink));
1238 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1245 gst_base_sink_get_caps (GstBaseSink * sink)
1251 gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps)
1256 static GstFlowReturn
1257 gst_base_sink_buffer_alloc (GstBaseSink * sink, guint64 offset, guint size,
1258 GstCaps * caps, GstBuffer ** buf)
1264 /* with PREROLL_LOCK, STREAM_LOCK */
1266 gst_base_sink_preroll_queue_flush (GstBaseSink * basesink, GstPad * pad)
1270 GST_DEBUG_OBJECT (basesink, "flushing queue %p", basesink);
1271 while ((obj = g_queue_pop_head (basesink->preroll_queue))) {
1272 GST_DEBUG_OBJECT (basesink, "popped %p", obj);
1273 gst_mini_object_unref (obj);
1275 /* we can't have EOS anymore now */
1276 basesink->eos = FALSE;
1277 basesink->priv->received_eos = FALSE;
1278 basesink->have_preroll = FALSE;
1279 basesink->eos_queued = FALSE;
1280 basesink->preroll_queued = 0;
1281 basesink->buffers_queued = 0;
1282 basesink->events_queued = 0;
1283 /* can't report latency anymore until we preroll again */
1284 if (basesink->priv->async_enabled) {
1285 GST_OBJECT_LOCK (basesink);
1286 basesink->priv->have_latency = FALSE;
1287 GST_OBJECT_UNLOCK (basesink);
1289 /* and signal any waiters now */
1290 GST_PAD_PREROLL_SIGNAL (pad);
1293 /* with STREAM_LOCK, configures given segment with the event information. */
1295 gst_base_sink_configure_segment (GstBaseSink * basesink, GstPad * pad,
1296 GstEvent * event, GstSegment * segment)
1299 gdouble rate, arate;
1305 /* the newsegment event is needed to bring the buffer timestamps to the
1306 * stream time and to drop samples outside of the playback segment. */
1307 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1308 &start, &stop, &time);
1310 /* The segment is protected with both the STREAM_LOCK and the OBJECT_LOCK.
1311 * We protect with the OBJECT_LOCK so that we can use the values to
1312 * safely answer a POSITION query. */
1313 GST_OBJECT_LOCK (basesink);
1314 gst_segment_set_newsegment_full (segment, update, rate, arate, format, start,
1317 if (format == GST_FORMAT_TIME) {
1318 GST_DEBUG_OBJECT (basesink,
1319 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1320 "format GST_FORMAT_TIME, "
1321 "%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
1322 ", time %" GST_TIME_FORMAT ", accum %" GST_TIME_FORMAT,
1323 update, rate, arate, GST_TIME_ARGS (segment->start),
1324 GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time),
1325 GST_TIME_ARGS (segment->accum));
1327 GST_DEBUG_OBJECT (basesink,
1328 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1330 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
1331 G_GINT64_FORMAT ", accum %" G_GINT64_FORMAT, update, rate, arate,
1332 segment->format, segment->start, segment->stop, segment->time,
1335 GST_OBJECT_UNLOCK (basesink);
1338 /* with PREROLL_LOCK, STREAM_LOCK */
1340 gst_base_sink_commit_state (GstBaseSink * basesink)
1342 /* commit state and proceed to next pending state */
1343 GstState current, next, pending, post_pending;
1344 gboolean post_paused = FALSE;
1345 gboolean post_async_done = FALSE;
1346 gboolean post_playing = FALSE;
1348 /* we are certainly not playing async anymore now */
1349 basesink->playing_async = FALSE;
1351 GST_OBJECT_LOCK (basesink);
1352 current = GST_STATE (basesink);
1353 next = GST_STATE_NEXT (basesink);
1354 pending = GST_STATE_PENDING (basesink);
1355 post_pending = pending;
1358 case GST_STATE_PLAYING:
1360 GstBaseSinkClass *bclass;
1361 GstStateChangeReturn ret;
1363 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1365 GST_DEBUG_OBJECT (basesink, "commiting state to PLAYING");
1367 basesink->need_preroll = FALSE;
1368 post_async_done = TRUE;
1369 basesink->priv->commited = TRUE;
1370 post_playing = TRUE;
1371 /* post PAUSED too when we were READY */
1372 if (current == GST_STATE_READY) {
1376 /* make sure we notify the subclass of async playing */
1377 if (bclass->async_play) {
1378 GST_WARNING_OBJECT (basesink, "deprecated async_play");
1379 ret = bclass->async_play (basesink);
1380 if (ret == GST_STATE_CHANGE_FAILURE)
1385 case GST_STATE_PAUSED:
1386 GST_DEBUG_OBJECT (basesink, "commiting state to PAUSED");
1388 post_async_done = TRUE;
1389 basesink->priv->commited = TRUE;
1390 post_pending = GST_STATE_VOID_PENDING;
1392 case GST_STATE_READY:
1393 case GST_STATE_NULL:
1395 case GST_STATE_VOID_PENDING:
1396 goto nothing_pending;
1401 /* we can report latency queries now */
1402 basesink->priv->have_latency = TRUE;
1404 GST_STATE (basesink) = pending;
1405 GST_STATE_NEXT (basesink) = GST_STATE_VOID_PENDING;
1406 GST_STATE_PENDING (basesink) = GST_STATE_VOID_PENDING;
1407 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_SUCCESS;
1408 GST_OBJECT_UNLOCK (basesink);
1411 GST_DEBUG_OBJECT (basesink, "posting PAUSED state change message");
1412 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1413 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1414 current, next, post_pending));
1416 if (post_async_done) {
1417 GST_DEBUG_OBJECT (basesink, "posting async-done message");
1418 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1419 gst_message_new_async_done (GST_OBJECT_CAST (basesink)));
1422 GST_DEBUG_OBJECT (basesink, "posting PLAYING state change message");
1423 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1424 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1425 next, pending, GST_STATE_VOID_PENDING));
1428 GST_STATE_BROADCAST (basesink);
1434 /* Depending on the state, set our vars. We get in this situation when the
1435 * state change function got a change to update the state vars before the
1436 * streaming thread did. This is fine but we need to make sure that we
1437 * update the need_preroll var since it was TRUE when we got here and might
1438 * become FALSE if we got to PLAYING. */
1439 GST_DEBUG_OBJECT (basesink, "nothing to commit, now in %s",
1440 gst_element_state_get_name (current));
1442 case GST_STATE_PLAYING:
1443 basesink->need_preroll = FALSE;
1445 case GST_STATE_PAUSED:
1446 basesink->need_preroll = TRUE;
1449 basesink->need_preroll = FALSE;
1450 basesink->flushing = TRUE;
1453 /* we can report latency queries now */
1454 basesink->priv->have_latency = TRUE;
1455 GST_OBJECT_UNLOCK (basesink);
1460 /* app is going to READY */
1461 GST_DEBUG_OBJECT (basesink, "stopping");
1462 basesink->need_preroll = FALSE;
1463 basesink->flushing = TRUE;
1464 GST_OBJECT_UNLOCK (basesink);
1469 GST_DEBUG_OBJECT (basesink, "async commit failed");
1470 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_FAILURE;
1471 GST_OBJECT_UNLOCK (basesink);
1477 start_stepping (GstBaseSink * sink, GstSegment * segment,
1478 GstStepInfo * pending, GstStepInfo * current)
1480 GST_DEBUG_OBJECT (sink, "update pending step");
1481 memcpy (current, pending, sizeof (GstStepInfo));
1482 pending->valid = FALSE;
1484 /* get the running time of the current segment start and remember it */
1485 if (segment->rate > 0.0)
1487 gst_segment_to_running_time (segment, segment->format, segment->start);
1490 gst_segment_to_running_time (segment, segment->format, segment->stop);
1492 /* set the new rate */
1493 segment->rate = segment->rate * current->rate;
1495 GST_DEBUG_OBJECT (sink, "step started at running_time %" GST_TIME_FORMAT,
1496 GST_TIME_ARGS (current->start));
1498 if (current->amount == -1) {
1499 GST_DEBUG_OBJECT (sink, "step amount == -1, stop stepping");
1500 current->valid = FALSE;
1502 GST_DEBUG_OBJECT (sink, "step amount: %" G_GUINT64_FORMAT ", format: %s, "
1503 "rate: %f", current->amount, gst_format_get_name (current->format),
1509 stop_stepping (GstBaseSink * sink, GstSegment * segment,
1510 GstStepInfo * current, guint64 cstart, guint64 cstop, gint64 * rstart,
1513 GstMessage *message;
1515 GST_DEBUG_OBJECT (sink, "step complete");
1517 GST_DEBUG_OBJECT (sink,
1518 "step stop at running_time %" GST_TIME_FORMAT ", timestamp %"
1519 GST_TIME_FORMAT, GST_TIME_ARGS (*rstart), GST_TIME_ARGS (cstart));
1521 /* configure the duration of the elapsed segment */
1522 if (segment->rate > 0.0)
1523 current->duration = *rstart - current->start;
1525 current->duration = *rstop - current->start;
1527 GST_DEBUG_OBJECT (sink, "step elapsed running_time %" GST_TIME_FORMAT,
1528 GST_TIME_ARGS (current->duration));
1530 /* update the segment, discarding what was consumed, running time goes
1531 * backwards with the duration of the data we skipped. FIXME, this only works
1533 if (segment->rate > 0.0) {
1535 gst_segment_to_stream_time (segment, segment->format, cstart);
1536 segment->start = cstart;
1538 *rstart = current->start;
1539 *rstop -= current->duration;
1541 segment->stop = cstop;
1542 *rstop = current->start;
1543 *rstart -= current->duration;
1545 segment->accum = current->start;
1547 /* the clip segment is used for position report in paused... */
1548 memcpy (sink->abidata.ABI.clip_segment, segment, sizeof (GstSegment));
1551 gst_message_new_step_done (GST_OBJECT_CAST (sink), current->format,
1552 current->amount, current->rate, current->flush, current->intermediate,
1554 gst_message_set_seqnum (message, current->seqnum);
1555 gst_element_post_message (GST_ELEMENT_CAST (sink), message);
1557 if (!current->intermediate)
1558 sink->need_preroll = current->need_preroll;
1560 /* and the current step info finished and becomes invalid */
1561 current->valid = FALSE;
1565 handle_stepping (GstBaseSink * sink, GstSegment * segment,
1566 GstStepInfo * current, gint64 * cstart, gint64 * cstop, gint64 * rstart,
1569 GstBaseSinkPrivate *priv;
1570 gboolean step_end = FALSE;
1574 /* see if we need to skip this buffer because of stepping */
1575 switch (current->format) {
1576 case GST_FORMAT_TIME:
1580 end = current->start + current->amount;
1582 current->position = *rstart - current->start;
1584 GST_DEBUG_OBJECT (sink,
1585 "got time step %" GST_TIME_FORMAT "/%" GST_TIME_FORMAT,
1586 GST_TIME_ARGS (current->position), GST_TIME_ARGS (current->amount));
1588 if (current->position >= current->amount || *rstop >= end) {
1590 *cstart += end - *rstart;
1595 case GST_FORMAT_BUFFERS:
1596 GST_DEBUG_OBJECT (sink,
1597 "got default step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1598 current->position, current->amount);
1600 if (current->position < current->amount) {
1601 current->position++;
1606 case GST_FORMAT_DEFAULT:
1608 GST_DEBUG_OBJECT (sink,
1609 "got unknown step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1610 current->position, current->amount);
1616 /* with STREAM_LOCK, PREROLL_LOCK
1618 * Returns TRUE if the object needs synchronisation and takes therefore
1619 * part in prerolling.
1621 * rsstart/rsstop contain the start/stop in stream time.
1622 * rrstart/rrstop contain the start/stop in running time.
1625 gst_base_sink_get_sync_times (GstBaseSink * basesink, GstMiniObject * obj,
1626 GstClockTime * rsstart, GstClockTime * rsstop,
1627 GstClockTime * rrstart, GstClockTime * rrstop, gboolean * do_sync,
1628 gboolean * stepped, GstSegment * segment, GstStepInfo * step)
1630 GstBaseSinkClass *bclass;
1632 GstClockTime start, stop; /* raw start/stop timestamps */
1633 gint64 cstart, cstop; /* clipped raw timestamps */
1634 gint64 rstart, rstop; /* clipped timestamps converted to running time */
1635 GstClockTime sstart, sstop; /* clipped timestamps converted to stream time */
1637 GstBaseSinkPrivate *priv;
1640 priv = basesink->priv;
1642 /* start with nothing */
1647 if (G_UNLIKELY (GST_IS_EVENT (obj))) {
1648 GstEvent *event = GST_EVENT_CAST (obj);
1650 switch (GST_EVENT_TYPE (event)) {
1651 /* EOS event needs syncing */
1654 if (basesink->segment.rate >= 0.0) {
1655 sstart = sstop = priv->current_sstop;
1657 /* we have not seen a buffer yet, use the segment values */
1658 sstart = sstop = gst_segment_to_stream_time (&basesink->segment,
1659 basesink->segment.format, basesink->segment.stop);
1662 sstart = sstop = priv->current_sstart;
1664 /* we have not seen a buffer yet, use the segment values */
1665 sstart = sstop = gst_segment_to_stream_time (&basesink->segment,
1666 basesink->segment.format, basesink->segment.start);
1670 rstart = rstop = priv->eos_rtime;
1671 *do_sync = rstart != -1;
1672 GST_DEBUG_OBJECT (basesink, "sync times for EOS %" GST_TIME_FORMAT,
1673 GST_TIME_ARGS (rstart));
1674 /* if we are stepping, we end now */
1675 step_end = step->valid;
1679 /* other events do not need syncing */
1680 /* FIXME, maybe NEWSEGMENT might need synchronisation
1681 * since the POSITION query depends on accumulated times and
1682 * we cannot accumulate the current segment before the previous
1689 /* else do buffer sync code */
1690 buffer = GST_BUFFER_CAST (obj);
1692 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1694 /* just get the times to see if we need syncing, if the start returns -1 we
1696 if (bclass->get_times)
1697 bclass->get_times (basesink, buffer, &start, &stop);
1700 /* we don't need to sync but we still want to get the timestamps for
1701 * tracking the position */
1702 gst_base_sink_get_times (basesink, buffer, &start, &stop);
1708 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
1709 ", stop: %" GST_TIME_FORMAT ", do_sync %d", GST_TIME_ARGS (start),
1710 GST_TIME_ARGS (stop), *do_sync);
1712 /* collect segment and format for code clarity */
1713 format = segment->format;
1715 /* no timestamp clipping if we did not get a TIME segment format */
1716 if (G_UNLIKELY (format != GST_FORMAT_TIME)) {
1719 /* do running and stream time in TIME format */
1720 format = GST_FORMAT_TIME;
1724 /* clip, only when we know about time */
1725 if (G_UNLIKELY (!gst_segment_clip (segment, GST_FORMAT_TIME,
1726 (gint64) start, (gint64) stop, &cstart, &cstop)))
1727 goto out_of_segment;
1729 if (G_UNLIKELY (start != cstart || stop != cstop)) {
1730 GST_DEBUG_OBJECT (basesink, "clipped to: start %" GST_TIME_FORMAT
1731 ", stop: %" GST_TIME_FORMAT, GST_TIME_ARGS (cstart),
1732 GST_TIME_ARGS (cstop));
1735 /* set last stop position */
1736 if (G_LIKELY (cstop != GST_CLOCK_TIME_NONE))
1737 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, cstop);
1739 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, cstart);
1742 /* this can produce wrong values if we accumulated non-TIME segments. If this happens,
1743 * upstream is behaving very badly */
1744 sstart = gst_segment_to_stream_time (segment, format, cstart);
1745 sstop = gst_segment_to_stream_time (segment, format, cstop);
1746 rstart = gst_segment_to_running_time (segment, format, cstart);
1747 rstop = gst_segment_to_running_time (segment, format, cstop);
1749 if (G_UNLIKELY (step->valid)) {
1750 if (!(step_end = handle_stepping (basesink, segment, step, &cstart, &cstop,
1756 /* done label only called when doing EOS, we also stop stepping then */
1758 stop_stepping (basesink, segment, step, cstart, cstop, &rstart, &rstop);
1766 /* buffers and EOS always need syncing and preroll */
1772 /* should not happen since we clip them in the chain function already,
1773 * we return FALSE so that we don't try to sync on it. */
1774 GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
1775 (NULL), ("unexpected buffer out of segment found."));
1776 GST_LOG_OBJECT (basesink, "buffer skipped, not in segment");
1781 /* with STREAM_LOCK, PREROLL_LOCK, LOCK
1782 * adjust a timestamp with the latency and timestamp offset */
1784 gst_base_sink_adjust_time (GstBaseSink * basesink, GstClockTime time)
1786 GstClockTimeDiff ts_offset;
1788 /* don't do anything funny with invalid timestamps */
1789 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
1792 time += basesink->priv->latency;
1794 /* apply offset, be carefull for underflows */
1795 ts_offset = basesink->priv->ts_offset;
1796 if (ts_offset < 0) {
1797 ts_offset = -ts_offset;
1798 if (ts_offset < time)
1809 * gst_base_sink_wait_clock:
1811 * @time: the running_time to be reached
1812 * @jitter: the jitter to be filled with time diff (can be NULL)
1814 * This function will block until @time is reached. It is usually called by
1815 * subclasses that use their own internal synchronisation.
1817 * If @time is not valid, no sycnhronisation is done and #GST_CLOCK_BADTIME is
1818 * returned. Likewise, if synchronisation is disabled in the element or there
1819 * is no clock, no synchronisation is done and #GST_CLOCK_BADTIME is returned.
1821 * This function should only be called with the PREROLL_LOCK held, like when
1822 * receiving an EOS event in the ::event vmethod or when receiving a buffer in
1823 * the ::render vmethod.
1825 * The @time argument should be the running_time of when this method should
1826 * return and is not adjusted with any latency or offset configured in the
1831 * Returns: #GstClockReturn
1834 gst_base_sink_wait_clock (GstBaseSink * sink, GstClockTime time,
1835 GstClockTimeDiff * jitter)
1841 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
1844 GST_OBJECT_LOCK (sink);
1845 if (G_UNLIKELY (!sink->sync))
1848 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (sink)) == NULL))
1851 /* add base_time to running_time to get the time against the clock */
1852 time += GST_ELEMENT_CAST (sink)->base_time;
1854 id = gst_clock_new_single_shot_id (clock, time);
1855 GST_OBJECT_UNLOCK (sink);
1857 /* A blocking wait is performed on the clock. We save the ClockID
1858 * so we can unlock the entry at any time. While we are blocking, we
1859 * release the PREROLL_LOCK so that other threads can interrupt the
1861 sink->clock_id = id;
1862 /* release the preroll lock while waiting */
1863 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
1865 ret = gst_clock_id_wait (id, jitter);
1867 GST_PAD_PREROLL_LOCK (sink->sinkpad);
1868 gst_clock_id_unref (id);
1869 sink->clock_id = NULL;
1873 /* no syncing needed */
1876 GST_DEBUG_OBJECT (sink, "time not valid, no sync needed");
1877 return GST_CLOCK_BADTIME;
1881 GST_DEBUG_OBJECT (sink, "sync disabled");
1882 GST_OBJECT_UNLOCK (sink);
1883 return GST_CLOCK_BADTIME;
1887 GST_DEBUG_OBJECT (sink, "no clock, can't sync");
1888 GST_OBJECT_UNLOCK (sink);
1889 return GST_CLOCK_BADTIME;
1894 * gst_base_sink_wait_preroll:
1897 * If the #GstBaseSinkClass::render method performs its own synchronisation against
1898 * the clock it must unblock when going from PLAYING to the PAUSED state and call
1899 * this method before continuing to render the remaining data.
1901 * This function will block until a state change to PLAYING happens (in which
1902 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
1903 * to a state change to READY or a FLUSH event (in which case this function
1904 * returns #GST_FLOW_WRONG_STATE).
1906 * This function should only be called with the PREROLL_LOCK held, like in the
1911 * Returns: #GST_FLOW_OK if the preroll completed and processing can
1912 * continue. Any other return value should be returned from the render vmethod.
1915 gst_base_sink_wait_preroll (GstBaseSink * sink)
1917 sink->have_preroll = TRUE;
1918 GST_DEBUG_OBJECT (sink, "waiting in preroll for flush or PLAYING");
1919 /* block until the state changes, or we get a flush, or something */
1920 GST_PAD_PREROLL_WAIT (sink->sinkpad);
1921 sink->have_preroll = FALSE;
1922 if (G_UNLIKELY (sink->flushing))
1924 GST_DEBUG_OBJECT (sink, "continue after preroll");
1931 GST_DEBUG_OBJECT (sink, "preroll interrupted");
1932 return GST_FLOW_WRONG_STATE;
1937 * gst_base_sink_do_preroll:
1939 * @obj: the object that caused the preroll
1941 * If the @sink spawns its own thread for pulling buffers from upstream it
1942 * should call this method after it has pulled a buffer. If the element needed
1943 * to preroll, this function will perform the preroll and will then block
1944 * until the element state is changed.
1946 * This function should be called with the PREROLL_LOCK held.
1950 * Returns: #GST_FLOW_OK if the preroll completed and processing can
1951 * continue. Any other return value should be returned from the render vmethod.
1954 gst_base_sink_do_preroll (GstBaseSink * sink, GstMiniObject * obj)
1958 while (G_UNLIKELY (sink->need_preroll)) {
1959 GST_DEBUG_OBJECT (sink, "prerolling object %p", obj);
1961 ret = gst_base_sink_preroll_object (sink, obj);
1962 if (ret != GST_FLOW_OK)
1963 goto preroll_failed;
1965 /* need to recheck here because the commit state could have
1966 * made us not need the preroll anymore */
1967 if (G_LIKELY (sink->need_preroll)) {
1968 /* block until the state changes, or we get a flush, or something */
1969 ret = gst_base_sink_wait_preroll (sink);
1970 if (ret != GST_FLOW_OK)
1971 goto preroll_failed;
1979 GST_DEBUG_OBJECT (sink, "preroll failed %d", ret);
1985 * gst_base_sink_wait_eos:
1987 * @time: the running_time to be reached
1988 * @jitter: the jitter to be filled with time diff (can be NULL)
1990 * This function will block until @time is reached. It is usually called by
1991 * subclasses that use their own internal synchronisation but want to let the
1992 * EOS be handled by the base class.
1994 * This function should only be called with the PREROLL_LOCK held, like when
1995 * receiving an EOS event in the ::event vmethod.
1997 * The @time argument should be the running_time of when the EOS should happen
1998 * and will be adjusted with any latency and offset configured in the sink.
2002 * Returns: #GstFlowReturn
2005 gst_base_sink_wait_eos (GstBaseSink * sink, GstClockTime time,
2006 GstClockTimeDiff * jitter)
2008 GstClockReturn status;
2014 GST_DEBUG_OBJECT (sink, "checking preroll");
2016 /* first wait for the playing state before we can continue */
2017 if (G_UNLIKELY (sink->need_preroll)) {
2018 ret = gst_base_sink_wait_preroll (sink);
2019 if (ret != GST_FLOW_OK)
2023 /* preroll done, we can sync since we are in PLAYING now. */
2024 GST_DEBUG_OBJECT (sink, "possibly waiting for clock to reach %"
2025 GST_TIME_FORMAT, GST_TIME_ARGS (time));
2027 /* compensate for latency and ts_offset. We don't adjust for render delay
2028 * because we don't interact with the device on EOS normally. */
2029 stime = gst_base_sink_adjust_time (sink, time);
2031 /* wait for the clock, this can be interrupted because we got shut down or
2033 status = gst_base_sink_wait_clock (sink, stime, jitter);
2035 GST_DEBUG_OBJECT (sink, "clock returned %d", status);
2037 /* invalid time, no clock or sync disabled, just continue then */
2038 if (status == GST_CLOCK_BADTIME)
2041 /* waiting could have been interrupted and we can be flushing now */
2042 if (G_UNLIKELY (sink->flushing))
2045 /* retry if we got unscheduled, which means we did not reach the timeout
2046 * yet. if some other error occures, we continue. */
2047 } while (status == GST_CLOCK_UNSCHEDULED);
2049 GST_DEBUG_OBJECT (sink, "end of stream");
2056 GST_DEBUG_OBJECT (sink, "we are flushing");
2057 return GST_FLOW_WRONG_STATE;
2061 /* with STREAM_LOCK, PREROLL_LOCK
2063 * Make sure we are in PLAYING and synchronize an object to the clock.
2065 * If we need preroll, we are not in PLAYING. We try to commit the state
2066 * if needed and then block if we still are not PLAYING.
2068 * We start waiting on the clock in PLAYING. If we got interrupted, we
2069 * immediatly try to re-preroll.
2071 * Some objects do not need synchronisation (most events) and so this function
2072 * immediatly returns GST_FLOW_OK.
2074 * for objects that arrive later than max-lateness to be synchronized to the
2075 * clock have the @late boolean set to TRUE.
2077 * This function keeps a running average of the jitter (the diff between the
2078 * clock time and the requested sync time). The jitter is negative for
2079 * objects that arrive in time and positive for late buffers.
2081 * does not take ownership of obj.
2083 static GstFlowReturn
2084 gst_base_sink_do_sync (GstBaseSink * basesink, GstPad * pad,
2085 GstMiniObject * obj, gboolean * late)
2087 GstClockTimeDiff jitter;
2089 GstClockReturn status = GST_CLOCK_OK;
2090 GstClockTime rstart, rstop, sstart, sstop, stime;
2092 GstBaseSinkPrivate *priv;
2094 GstStepInfo *current, *pending;
2097 priv = basesink->priv;
2100 sstart = sstop = rstart = rstop = -1;
2104 priv->current_rstart = -1;
2106 /* get stepping info */
2107 current = &priv->current_step;
2108 pending = &priv->pending_step;
2110 /* get timing information for this object against the render segment */
2111 syncable = gst_base_sink_get_sync_times (basesink, obj,
2112 &sstart, &sstop, &rstart, &rstop, &do_sync, &stepped, &basesink->segment,
2115 if (G_UNLIKELY (stepped))
2118 /* a syncable object needs to participate in preroll and
2119 * clocking. All buffers and EOS are syncable. */
2120 if (G_UNLIKELY (!syncable))
2123 /* store timing info for current object */
2124 priv->current_rstart = rstart;
2125 priv->current_rstop = (rstop != -1 ? rstop : rstart);
2127 /* save sync time for eos when the previous object needed sync */
2128 priv->eos_rtime = (do_sync ? priv->current_rstop : -1);
2131 /* first do preroll, this makes sure we commit our state
2132 * to PAUSED and can continue to PLAYING. We cannot perform
2133 * any clock sync in PAUSED because there is no clock. */
2134 ret = gst_base_sink_do_preroll (basesink, obj);
2135 if (G_UNLIKELY (ret != GST_FLOW_OK))
2136 goto preroll_failed;
2138 /* update the segment with a pending step if the current one is invalid and we
2139 * have a new pending one. We only accept new step updates after a preroll */
2140 if (G_UNLIKELY (pending->valid && !current->valid)) {
2141 start_stepping (basesink, &basesink->segment, pending, current);
2145 /* After rendering we store the position of the last buffer so that we can use
2146 * it to report the position. We need to take the lock here. */
2147 GST_OBJECT_LOCK (basesink);
2148 priv->current_sstart = sstart;
2149 priv->current_sstop = (sstop != -1 ? sstop : sstart);
2150 GST_OBJECT_UNLOCK (basesink);
2155 /* adjust for latency */
2156 stime = gst_base_sink_adjust_time (basesink, rstart);
2158 /* adjust for render-delay, avoid underflows */
2160 if (stime > priv->render_delay)
2161 stime -= priv->render_delay;
2166 /* preroll done, we can sync since we are in PLAYING now. */
2167 GST_DEBUG_OBJECT (basesink, "possibly waiting for clock to reach %"
2168 GST_TIME_FORMAT ", adjusted %" GST_TIME_FORMAT,
2169 GST_TIME_ARGS (rstart), GST_TIME_ARGS (stime));
2171 /* This function will return immediatly if start == -1, no clock
2172 * or sync is disabled with GST_CLOCK_BADTIME. */
2173 status = gst_base_sink_wait_clock (basesink, stime, &jitter);
2175 GST_DEBUG_OBJECT (basesink, "clock returned %d", status);
2177 /* invalid time, no clock or sync disabled, just render */
2178 if (status == GST_CLOCK_BADTIME)
2181 /* waiting could have been interrupted and we can be flushing now */
2182 if (G_UNLIKELY (basesink->flushing))
2185 /* check for unlocked by a state change, we are not flushing so
2186 * we can try to preroll on the current buffer. */
2187 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
2188 GST_DEBUG_OBJECT (basesink, "unscheduled, waiting some more");
2189 priv->call_preroll = TRUE;
2193 /* successful syncing done, record observation */
2194 priv->current_jitter = jitter;
2196 /* check if the object should be dropped */
2197 *late = gst_base_sink_is_too_late (basesink, obj, rstart, rstop,
2206 GST_DEBUG_OBJECT (basesink, "skipped stepped object %p", obj);
2212 GST_DEBUG_OBJECT (basesink, "non syncable object %p", obj);
2217 GST_DEBUG_OBJECT (basesink, "we are flushing");
2218 return GST_FLOW_WRONG_STATE;
2222 GST_DEBUG_OBJECT (basesink, "preroll failed");
2228 gst_base_sink_send_qos (GstBaseSink * basesink,
2229 gdouble proportion, GstClockTime time, GstClockTimeDiff diff)
2234 /* generate Quality-of-Service event */
2235 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2236 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2237 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (time));
2239 event = gst_event_new_qos (proportion, diff, time);
2242 res = gst_pad_push_event (basesink->sinkpad, event);
2248 gst_base_sink_perform_qos (GstBaseSink * sink, gboolean dropped)
2250 GstBaseSinkPrivate *priv;
2251 GstClockTime start, stop;
2252 GstClockTimeDiff jitter;
2253 GstClockTime pt, entered, left;
2254 GstClockTime duration;
2259 start = priv->current_rstart;
2261 /* if Quality-of-Service disabled, do nothing */
2262 if (!g_atomic_int_get (&priv->qos_enabled) || start == -1)
2265 stop = priv->current_rstop;
2266 jitter = priv->current_jitter;
2269 /* this is the time the buffer entered the sink */
2270 if (start < -jitter)
2273 entered = start + jitter;
2276 /* this is the time the buffer entered the sink */
2277 entered = start + jitter;
2278 /* this is the time the buffer left the sink */
2279 left = start + jitter;
2282 /* calculate duration of the buffer */
2284 duration = stop - start;
2288 /* if we have the time when the last buffer left us, calculate
2289 * processing time */
2290 if (priv->last_left != -1) {
2291 if (entered > priv->last_left) {
2292 pt = entered - priv->last_left;
2300 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "start: %" GST_TIME_FORMAT
2301 ", entered %" GST_TIME_FORMAT ", left %" GST_TIME_FORMAT ", pt: %"
2302 GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT ",jitter %"
2303 G_GINT64_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (entered),
2304 GST_TIME_ARGS (left), GST_TIME_ARGS (pt), GST_TIME_ARGS (duration),
2307 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "avg_duration: %" GST_TIME_FORMAT
2308 ", avg_pt: %" GST_TIME_FORMAT ", avg_rate: %g",
2309 GST_TIME_ARGS (priv->avg_duration), GST_TIME_ARGS (priv->avg_pt),
2312 /* collect running averages. for first observations, we copy the
2314 if (priv->avg_duration == -1)
2315 priv->avg_duration = duration;
2317 priv->avg_duration = UPDATE_RUNNING_AVG (priv->avg_duration, duration);
2319 if (priv->avg_pt == -1)
2322 priv->avg_pt = UPDATE_RUNNING_AVG (priv->avg_pt, pt);
2324 if (priv->avg_duration != 0)
2326 gst_guint64_to_gdouble (priv->avg_pt) /
2327 gst_guint64_to_gdouble (priv->avg_duration);
2331 if (priv->last_left != -1) {
2332 if (dropped || priv->avg_rate < 0.0) {
2333 priv->avg_rate = rate;
2336 priv->avg_rate = UPDATE_RUNNING_AVG_N (priv->avg_rate, rate);
2338 priv->avg_rate = UPDATE_RUNNING_AVG_P (priv->avg_rate, rate);
2342 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink,
2343 "updated: avg_duration: %" GST_TIME_FORMAT ", avg_pt: %" GST_TIME_FORMAT
2344 ", avg_rate: %g", GST_TIME_ARGS (priv->avg_duration),
2345 GST_TIME_ARGS (priv->avg_pt), priv->avg_rate);
2348 if (priv->avg_rate >= 0.0) {
2349 /* if we have a valid rate, start sending QoS messages */
2350 if (priv->current_jitter < 0) {
2351 /* make sure we never go below 0 when adding the jitter to the
2353 if (priv->current_rstart < -priv->current_jitter)
2354 priv->current_jitter = -priv->current_rstart;
2356 gst_base_sink_send_qos (sink, priv->avg_rate, priv->current_rstart,
2357 priv->current_jitter);
2360 /* record when this buffer will leave us */
2361 priv->last_left = left;
2364 /* reset all qos measuring */
2366 gst_base_sink_reset_qos (GstBaseSink * sink)
2368 GstBaseSinkPrivate *priv;
2372 priv->last_in_time = -1;
2373 priv->last_left = -1;
2374 priv->avg_duration = -1;
2376 priv->avg_rate = -1.0;
2377 priv->avg_render = -1;
2383 /* Checks if the object was scheduled too late.
2385 * start/stop contain the raw timestamp start and stop values
2388 * status and jitter contain the return values from the clock wait.
2390 * returns TRUE if the buffer was too late.
2393 gst_base_sink_is_too_late (GstBaseSink * basesink, GstMiniObject * obj,
2394 GstClockTime start, GstClockTime stop,
2395 GstClockReturn status, GstClockTimeDiff jitter)
2398 gint64 max_lateness;
2399 GstBaseSinkPrivate *priv;
2401 priv = basesink->priv;
2405 /* only for objects that were too late */
2406 if (G_LIKELY (status != GST_CLOCK_EARLY))
2409 max_lateness = basesink->abidata.ABI.max_lateness;
2411 /* check if frame dropping is enabled */
2412 if (max_lateness == -1)
2415 /* only check for buffers */
2416 if (G_UNLIKELY (!GST_IS_BUFFER (obj)))
2419 /* can't do check if we don't have a timestamp */
2420 if (G_UNLIKELY (start == -1))
2423 /* we can add a valid stop time */
2425 max_lateness += stop;
2427 max_lateness += start;
2429 /* if the jitter bigger than duration and lateness we are too late */
2430 if ((late = start + jitter > max_lateness)) {
2431 GST_DEBUG_OBJECT (basesink, "buffer is too late %" GST_TIME_FORMAT
2432 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (start + jitter),
2433 GST_TIME_ARGS (max_lateness));
2434 /* !!emergency!!, if we did not receive anything valid for more than a
2435 * second, render it anyway so the user sees something */
2436 if (priv->last_in_time != -1 && start - priv->last_in_time > GST_SECOND) {
2438 GST_DEBUG_OBJECT (basesink,
2439 "**emergency** last buffer at %" GST_TIME_FORMAT " > GST_SECOND",
2440 GST_TIME_ARGS (priv->last_in_time));
2446 priv->last_in_time = start;
2453 GST_DEBUG_OBJECT (basesink, "object was scheduled in time");
2458 GST_DEBUG_OBJECT (basesink, "frame dropping disabled");
2463 GST_DEBUG_OBJECT (basesink, "object is not a buffer");
2468 GST_DEBUG_OBJECT (basesink, "buffer has no timestamp");
2473 /* called before and after calling the render vmethod. It keeps track of how
2474 * much time was spent in the render method and is used to check if we are
2477 gst_base_sink_do_render_stats (GstBaseSink * basesink, gboolean start)
2479 GstBaseSinkPrivate *priv;
2481 priv = basesink->priv;
2484 priv->start = gst_util_get_timestamp ();
2486 GstClockTime elapsed;
2488 priv->stop = gst_util_get_timestamp ();
2490 elapsed = GST_CLOCK_DIFF (priv->start, priv->stop);
2492 if (priv->avg_render == -1)
2493 priv->avg_render = elapsed;
2495 priv->avg_render = UPDATE_RUNNING_AVG (priv->avg_render, elapsed);
2497 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2498 "avg_render: %" GST_TIME_FORMAT, GST_TIME_ARGS (priv->avg_render));
2502 /* with STREAM_LOCK, PREROLL_LOCK,
2504 * Synchronize the object on the clock and then render it.
2506 * takes ownership of obj.
2508 static GstFlowReturn
2509 gst_base_sink_render_object (GstBaseSink * basesink, GstPad * pad,
2510 GstMiniObject * obj)
2513 GstBaseSinkClass *bclass;
2516 GstBaseSinkPrivate *priv;
2518 priv = basesink->priv;
2523 /* synchronize this object, non syncable objects return OK
2525 ret = gst_base_sink_do_sync (basesink, pad, obj, &late);
2526 if (G_UNLIKELY (ret != GST_FLOW_OK))
2529 /* and now render, event or buffer. */
2530 if (G_LIKELY (GST_IS_BUFFER (obj))) {
2533 /* drop late buffers unconditionally, let's hope it's unlikely */
2534 if (G_UNLIKELY (late))
2537 buf = GST_BUFFER_CAST (obj);
2539 gst_base_sink_set_last_buffer (basesink, buf);
2541 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2543 if (G_LIKELY (bclass->render)) {
2546 /* read once, to get same value before and after */
2547 do_qos = g_atomic_int_get (&priv->qos_enabled);
2549 GST_DEBUG_OBJECT (basesink, "rendering buffer %p", obj);
2551 /* record rendering time for QoS and stats */
2553 gst_base_sink_do_render_stats (basesink, TRUE);
2555 ret = bclass->render (basesink, buf);
2560 gst_base_sink_do_render_stats (basesink, FALSE);
2563 GstEvent *event = GST_EVENT_CAST (obj);
2564 gboolean event_res = TRUE;
2567 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2569 type = GST_EVENT_TYPE (event);
2571 GST_DEBUG_OBJECT (basesink, "rendering event %p, type %s", obj,
2572 gst_event_type_get_name (type));
2575 event_res = bclass->event (basesink, event);
2577 /* when we get here we could be flushing again when the event handler calls
2578 * _wait_eos(). We have to ignore this object in that case. */
2579 if (G_UNLIKELY (basesink->flushing))
2582 if (G_LIKELY (event_res)) {
2585 seqnum = basesink->priv->seqnum = gst_event_get_seqnum (event);
2586 GST_DEBUG_OBJECT (basesink, "Got seqnum #%" G_GUINT32_FORMAT, seqnum);
2591 GstMessage *message;
2593 /* the EOS event is completely handled so we mark
2594 * ourselves as being in the EOS state. eos is also
2595 * protected by the object lock so we can read it when
2596 * answering the POSITION query. */
2597 GST_OBJECT_LOCK (basesink);
2598 basesink->eos = TRUE;
2599 GST_OBJECT_UNLOCK (basesink);
2601 /* ok, now we can post the message */
2602 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
2604 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
2605 gst_message_set_seqnum (message, seqnum);
2606 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
2609 case GST_EVENT_NEWSEGMENT:
2610 /* configure the segment */
2611 gst_base_sink_configure_segment (basesink, pad, event,
2612 &basesink->segment);
2621 gst_base_sink_perform_qos (basesink, late);
2623 GST_DEBUG_OBJECT (basesink, "object unref after render %p", obj);
2624 gst_mini_object_unref (obj);
2631 GST_DEBUG_OBJECT (basesink, "do_sync returned %s", gst_flow_get_name (ret));
2637 GST_DEBUG_OBJECT (basesink, "buffer late, dropping");
2642 GST_DEBUG_OBJECT (basesink, "we are flushing, ignore object");
2643 gst_mini_object_unref (obj);
2644 return GST_FLOW_WRONG_STATE;
2648 /* with STREAM_LOCK, PREROLL_LOCK
2650 * Perform preroll on the given object. For buffers this means
2651 * calling the preroll subclass method.
2652 * If that succeeds, the state will be commited.
2654 * function does not take ownership of obj.
2656 static GstFlowReturn
2657 gst_base_sink_preroll_object (GstBaseSink * basesink, GstMiniObject * obj)
2661 GST_DEBUG_OBJECT (basesink, "prerolling object %p", obj);
2663 /* if it's a buffer, we need to call the preroll method */
2664 if (G_LIKELY (GST_IS_BUFFER (obj)) && basesink->priv->call_preroll) {
2665 GstBaseSinkClass *bclass;
2667 GstClockTime timestamp;
2669 buf = GST_BUFFER_CAST (obj);
2670 timestamp = GST_BUFFER_TIMESTAMP (buf);
2672 GST_DEBUG_OBJECT (basesink, "preroll buffer %" GST_TIME_FORMAT,
2673 GST_TIME_ARGS (timestamp));
2675 gst_base_sink_set_last_buffer (basesink, buf);
2677 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2678 if (bclass->preroll)
2679 if ((ret = bclass->preroll (basesink, buf)) != GST_FLOW_OK)
2680 goto preroll_failed;
2682 basesink->priv->call_preroll = FALSE;
2686 if (G_LIKELY (basesink->playing_async)) {
2687 if (G_UNLIKELY (!gst_base_sink_commit_state (basesink)))
2696 GST_DEBUG_OBJECT (basesink, "preroll failed, abort state");
2697 gst_element_abort_state (GST_ELEMENT_CAST (basesink));
2702 GST_DEBUG_OBJECT (basesink, "stopping while commiting state");
2703 return GST_FLOW_WRONG_STATE;
2707 /* with STREAM_LOCK, PREROLL_LOCK
2709 * Queue an object for rendering.
2710 * The first prerollable object queued will complete the preroll. If the
2711 * preroll queue if filled, we render all the objects in the queue.
2713 * This function takes ownership of the object.
2715 static GstFlowReturn
2716 gst_base_sink_queue_object_unlocked (GstBaseSink * basesink, GstPad * pad,
2717 GstMiniObject * obj, gboolean prerollable)
2719 GstFlowReturn ret = GST_FLOW_OK;
2723 if (G_UNLIKELY (basesink->need_preroll)) {
2724 if (G_LIKELY (prerollable))
2725 basesink->preroll_queued++;
2727 length = basesink->preroll_queued;
2729 GST_DEBUG_OBJECT (basesink, "now %d prerolled items", length);
2731 /* first prerollable item needs to finish the preroll */
2733 ret = gst_base_sink_preroll_object (basesink, obj);
2734 if (G_UNLIKELY (ret != GST_FLOW_OK))
2735 goto preroll_failed;
2737 /* need to recheck if we need preroll, commmit state during preroll
2738 * could have made us not need more preroll. */
2739 if (G_UNLIKELY (basesink->need_preroll)) {
2740 /* see if we can render now, if we can't add the object to the preroll
2742 if (G_UNLIKELY (length <= basesink->preroll_queue_max_len))
2747 /* we can start rendering (or blocking) the queued object
2749 q = basesink->preroll_queue;
2750 while (G_UNLIKELY (!g_queue_is_empty (q))) {
2753 o = g_queue_pop_head (q);
2754 GST_DEBUG_OBJECT (basesink, "rendering queued object %p", o);
2756 /* do something with the return value */
2757 ret = gst_base_sink_render_object (basesink, pad, o);
2758 if (ret != GST_FLOW_OK)
2759 goto dequeue_failed;
2762 /* now render the object */
2763 ret = gst_base_sink_render_object (basesink, pad, obj);
2764 basesink->preroll_queued = 0;
2771 GST_DEBUG_OBJECT (basesink, "preroll failed, reason %s",
2772 gst_flow_get_name (ret));
2773 gst_mini_object_unref (obj);
2778 /* add object to the queue and return */
2779 GST_DEBUG_OBJECT (basesink, "need more preroll data %d <= %d",
2780 length, basesink->preroll_queue_max_len);
2781 g_queue_push_tail (basesink->preroll_queue, obj);
2786 GST_DEBUG_OBJECT (basesink, "rendering queued objects failed, reason %s",
2787 gst_flow_get_name (ret));
2788 gst_mini_object_unref (obj);
2795 * This function grabs the PREROLL_LOCK and adds the object to
2798 * This function takes ownership of obj.
2800 static GstFlowReturn
2801 gst_base_sink_queue_object (GstBaseSink * basesink, GstPad * pad,
2802 GstMiniObject * obj, gboolean prerollable)
2806 GST_PAD_PREROLL_LOCK (pad);
2807 if (G_UNLIKELY (basesink->flushing))
2810 if (G_UNLIKELY (basesink->priv->received_eos))
2813 ret = gst_base_sink_queue_object_unlocked (basesink, pad, obj, prerollable);
2814 GST_PAD_PREROLL_UNLOCK (pad);
2821 GST_DEBUG_OBJECT (basesink, "sink is flushing");
2822 GST_PAD_PREROLL_UNLOCK (pad);
2823 gst_mini_object_unref (obj);
2824 return GST_FLOW_WRONG_STATE;
2828 GST_DEBUG_OBJECT (basesink,
2829 "we are EOS, dropping object, return UNEXPECTED");
2830 GST_PAD_PREROLL_UNLOCK (pad);
2831 gst_mini_object_unref (obj);
2832 return GST_FLOW_UNEXPECTED;
2837 gst_base_sink_flush_start (GstBaseSink * basesink, GstPad * pad)
2839 /* make sure we are not blocked on the clock also clear any pending
2841 gst_base_sink_set_flushing (basesink, pad, TRUE);
2843 /* we grab the stream lock but that is not needed since setting the
2844 * sink to flushing would make sure no state commit is being done
2846 GST_PAD_STREAM_LOCK (pad);
2847 gst_base_sink_reset_qos (basesink);
2848 if (basesink->priv->async_enabled) {
2849 /* and we need to commit our state again on the next
2850 * prerolled buffer */
2851 basesink->playing_async = TRUE;
2852 gst_element_lost_state (GST_ELEMENT_CAST (basesink));
2854 basesink->priv->have_latency = TRUE;
2855 basesink->need_preroll = FALSE;
2857 gst_base_sink_set_last_buffer (basesink, NULL);
2858 GST_PAD_STREAM_UNLOCK (pad);
2862 gst_base_sink_flush_stop (GstBaseSink * basesink, GstPad * pad)
2864 /* unset flushing so we can accept new data, this also flushes out any EOS
2866 gst_base_sink_set_flushing (basesink, pad, FALSE);
2868 /* for position reporting */
2869 GST_OBJECT_LOCK (basesink);
2870 basesink->priv->current_sstart = -1;
2871 basesink->priv->current_sstop = -1;
2872 basesink->priv->eos_rtime = -1;
2873 basesink->priv->call_preroll = TRUE;
2874 if (basesink->pad_mode == GST_ACTIVATE_PUSH) {
2875 /* we need new segment info after the flush. */
2876 basesink->have_newsegment = FALSE;
2877 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
2878 gst_segment_init (basesink->abidata.ABI.clip_segment, GST_FORMAT_UNDEFINED);
2880 GST_OBJECT_UNLOCK (basesink);
2884 gst_base_sink_event (GstPad * pad, GstEvent * event)
2886 GstBaseSink *basesink;
2887 gboolean result = TRUE;
2888 GstBaseSinkClass *bclass;
2890 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
2892 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2894 GST_DEBUG_OBJECT (basesink, "event %p (%s)", event,
2895 GST_EVENT_TYPE_NAME (event));
2897 switch (GST_EVENT_TYPE (event)) {
2902 GST_PAD_PREROLL_LOCK (pad);
2903 if (G_UNLIKELY (basesink->flushing))
2906 if (G_UNLIKELY (basesink->priv->received_eos)) {
2907 /* we can't accept anything when we are EOS */
2909 gst_event_unref (event);
2911 /* we set the received EOS flag here so that we can use it when testing if
2912 * we are prerolled and to refuse more buffers. */
2913 basesink->priv->received_eos = TRUE;
2915 /* EOS is a prerollable object, we call the unlocked version because it
2916 * does not check the received_eos flag. */
2917 ret = gst_base_sink_queue_object_unlocked (basesink, pad,
2918 GST_MINI_OBJECT_CAST (event), TRUE);
2919 if (G_UNLIKELY (ret != GST_FLOW_OK))
2922 GST_PAD_PREROLL_UNLOCK (pad);
2925 case GST_EVENT_NEWSEGMENT:
2929 GST_DEBUG_OBJECT (basesink, "newsegment %p", event);
2931 GST_PAD_PREROLL_LOCK (pad);
2932 if (G_UNLIKELY (basesink->flushing))
2935 if (G_UNLIKELY (basesink->priv->received_eos)) {
2936 /* we can't accept anything when we are EOS */
2938 gst_event_unref (event);
2940 /* the new segment is a non prerollable item and does not block anything,
2941 * we need to configure the current clipping segment and insert the event
2942 * in the queue to serialize it with the buffers for rendering. */
2943 gst_base_sink_configure_segment (basesink, pad, event,
2944 basesink->abidata.ABI.clip_segment);
2947 gst_base_sink_queue_object_unlocked (basesink, pad,
2948 GST_MINI_OBJECT_CAST (event), FALSE);
2949 if (G_UNLIKELY (ret != GST_FLOW_OK))
2952 GST_OBJECT_LOCK (basesink);
2953 basesink->have_newsegment = TRUE;
2954 GST_OBJECT_UNLOCK (basesink);
2957 GST_PAD_PREROLL_UNLOCK (pad);
2960 case GST_EVENT_FLUSH_START:
2962 bclass->event (basesink, event);
2964 GST_DEBUG_OBJECT (basesink, "flush-start %p", event);
2966 gst_base_sink_flush_start (basesink, pad);
2968 gst_event_unref (event);
2970 case GST_EVENT_FLUSH_STOP:
2972 bclass->event (basesink, event);
2974 GST_DEBUG_OBJECT (basesink, "flush-stop %p", event);
2976 gst_base_sink_flush_stop (basesink, pad);
2978 gst_event_unref (event);
2981 /* other events are sent to queue or subclass depending on if they
2982 * are serialized. */
2983 if (GST_EVENT_IS_SERIALIZED (event)) {
2984 gst_base_sink_queue_object (basesink, pad,
2985 GST_MINI_OBJECT_CAST (event), FALSE);
2988 bclass->event (basesink, event);
2989 gst_event_unref (event);
2994 gst_object_unref (basesink);
3001 GST_DEBUG_OBJECT (basesink, "we are flushing");
3002 GST_PAD_PREROLL_UNLOCK (pad);
3004 gst_event_unref (event);
3009 /* default implementation to calculate the start and end
3010 * timestamps on a buffer, subclasses can override
3013 gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
3014 GstClockTime * start, GstClockTime * end)
3016 GstClockTime timestamp, duration;
3018 timestamp = GST_BUFFER_TIMESTAMP (buffer);
3019 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
3021 /* get duration to calculate end time */
3022 duration = GST_BUFFER_DURATION (buffer);
3023 if (GST_CLOCK_TIME_IS_VALID (duration)) {
3024 *end = timestamp + duration;
3030 /* must be called with PREROLL_LOCK */
3032 gst_base_sink_needs_preroll (GstBaseSink * basesink)
3034 gboolean is_prerolled, res;
3036 /* we have 2 cases where the PREROLL_LOCK is released:
3037 * 1) we are blocking in the PREROLL_LOCK and thus are prerolled.
3038 * 2) we are syncing on the clock
3040 is_prerolled = basesink->have_preroll || basesink->priv->received_eos;
3041 res = !is_prerolled;
3043 GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d => needs preroll: %d",
3044 basesink->have_preroll, basesink->priv->received_eos, res);
3049 /* with STREAM_LOCK, PREROLL_LOCK
3051 * Takes a buffer and compare the timestamps with the last segment.
3052 * If the buffer falls outside of the segment boundaries, drop it.
3053 * Else queue the buffer for preroll and rendering.
3055 * This function takes ownership of the buffer.
3057 static GstFlowReturn
3058 gst_base_sink_chain_unlocked (GstBaseSink * basesink, GstPad * pad,
3061 GstBaseSinkClass *bclass;
3062 GstFlowReturn result;
3063 GstClockTime start = GST_CLOCK_TIME_NONE, end = GST_CLOCK_TIME_NONE;
3064 GstSegment *clip_segment;
3066 if (G_UNLIKELY (basesink->flushing))
3069 if (G_UNLIKELY (basesink->priv->received_eos))
3072 /* for code clarity */
3073 clip_segment = basesink->abidata.ABI.clip_segment;
3075 if (G_UNLIKELY (!basesink->have_newsegment)) {
3078 sync = gst_base_sink_get_sync (basesink);
3080 GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
3081 (_("Internal data flow problem.")),
3082 ("Received buffer without a new-segment. Assuming timestamps start from 0."));
3085 /* this means this sink will assume timestamps start from 0 */
3086 GST_OBJECT_LOCK (basesink);
3087 clip_segment->start = 0;
3088 clip_segment->stop = -1;
3089 basesink->segment.start = 0;
3090 basesink->segment.stop = -1;
3091 basesink->have_newsegment = TRUE;
3092 GST_OBJECT_UNLOCK (basesink);
3095 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3097 /* check if the buffer needs to be dropped, we first ask the subclass for the
3099 if (bclass->get_times)
3100 bclass->get_times (basesink, buf, &start, &end);
3103 /* if the subclass does not want sync, we use our own values so that we at
3104 * least clip the buffer to the segment */
3105 gst_base_sink_get_times (basesink, buf, &start, &end);
3108 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
3109 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
3111 /* a dropped buffer does not participate in anything */
3112 if (GST_CLOCK_TIME_IS_VALID (start) &&
3113 (clip_segment->format == GST_FORMAT_TIME)) {
3114 if (G_UNLIKELY (!gst_segment_clip (clip_segment,
3115 GST_FORMAT_TIME, (gint64) start, (gint64) end, NULL, NULL)))
3116 goto out_of_segment;
3119 /* now we can process the buffer in the queue, this function takes ownership
3121 result = gst_base_sink_queue_object_unlocked (basesink, pad,
3122 GST_MINI_OBJECT_CAST (buf), TRUE);
3129 GST_DEBUG_OBJECT (basesink, "sink is flushing");
3130 gst_buffer_unref (buf);
3131 return GST_FLOW_WRONG_STATE;
3135 GST_DEBUG_OBJECT (basesink,
3136 "we are EOS, dropping object, return UNEXPECTED");
3137 gst_buffer_unref (buf);
3138 return GST_FLOW_UNEXPECTED;
3142 GST_DEBUG_OBJECT (basesink, "dropping buffer, out of clipping segment");
3143 gst_buffer_unref (buf);
3150 static GstFlowReturn
3151 gst_base_sink_chain (GstPad * pad, GstBuffer * buf)
3153 GstBaseSink *basesink;
3154 GstFlowReturn result;
3156 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
3158 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH))
3161 GST_PAD_PREROLL_LOCK (pad);
3162 result = gst_base_sink_chain_unlocked (basesink, pad, buf);
3163 GST_PAD_PREROLL_UNLOCK (pad);
3171 GST_OBJECT_LOCK (pad);
3172 GST_WARNING_OBJECT (basesink,
3173 "Push on pad %s:%s, but it was not activated in push mode",
3174 GST_DEBUG_PAD_NAME (pad));
3175 GST_OBJECT_UNLOCK (pad);
3176 gst_buffer_unref (buf);
3177 /* we don't post an error message this will signal to the peer
3178 * pushing that EOS is reached. */
3179 result = GST_FLOW_UNEXPECTED;
3185 gst_base_sink_default_do_seek (GstBaseSink * sink, GstSegment * segment)
3187 gboolean res = TRUE;
3189 /* update our offset if the start/stop position was updated */
3190 if (segment->format == GST_FORMAT_BYTES) {
3191 segment->time = segment->start;
3192 } else if (segment->start == 0) {
3193 /* seek to start, we can implement a default for this. */
3197 GST_INFO_OBJECT (sink, "Can't do a default seek");
3203 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
3206 gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
3207 GstEvent * event, GstSegment * segment)
3209 /* By default, we try one of 2 things:
3210 * - For absolute seek positions, convert the requested position to our
3211 * configured processing format and place it in the output segment \
3212 * - For relative seek positions, convert our current (input) values to the
3213 * seek format, adjust by the relative seek offset and then convert back to
3214 * the processing format
3216 GstSeekType cur_type, stop_type;
3219 GstFormat seek_format, dest_format;
3222 gboolean res = TRUE;
3224 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3225 &cur_type, &cur, &stop_type, &stop);
3226 dest_format = segment->format;
3228 if (seek_format == dest_format) {
3229 gst_segment_set_seek (segment, rate, seek_format, flags,
3230 cur_type, cur, stop_type, stop, &update);
3234 if (cur_type != GST_SEEK_TYPE_NONE) {
3235 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3237 gst_pad_query_convert (sink->sinkpad, seek_format, cur, &dest_format,
3239 cur_type = GST_SEEK_TYPE_SET;
3242 if (res && stop_type != GST_SEEK_TYPE_NONE) {
3243 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3245 gst_pad_query_convert (sink->sinkpad, seek_format, stop, &dest_format,
3247 stop_type = GST_SEEK_TYPE_SET;
3250 /* And finally, configure our output segment in the desired format */
3251 gst_segment_set_seek (segment, rate, dest_format, flags, cur_type, cur,
3252 stop_type, stop, &update);
3261 GST_DEBUG_OBJECT (sink, "undefined format given, seek aborted.");
3266 /* perform a seek, only executed in pull mode */
3268 gst_base_sink_perform_seek (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3272 GstFormat seek_format, dest_format;
3274 GstSeekType cur_type, stop_type;
3275 gboolean seekseg_configured = FALSE;
3277 gboolean update, res = TRUE;
3278 GstSegment seeksegment;
3280 dest_format = sink->segment.format;
3283 GST_DEBUG_OBJECT (sink, "performing seek with event %p", event);
3284 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3285 &cur_type, &cur, &stop_type, &stop);
3287 flush = flags & GST_SEEK_FLAG_FLUSH;
3289 GST_DEBUG_OBJECT (sink, "performing seek without event");
3294 GST_DEBUG_OBJECT (sink, "flushing upstream");
3295 gst_pad_push_event (pad, gst_event_new_flush_start ());
3296 gst_base_sink_flush_start (sink, pad);
3298 GST_DEBUG_OBJECT (sink, "pausing pulling thread");
3301 GST_PAD_STREAM_LOCK (pad);
3303 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
3304 * copy the current segment info into the temp segment that we can actually
3305 * attempt the seek with. We only update the real segment if the seek suceeds. */
3306 if (!seekseg_configured) {
3307 memcpy (&seeksegment, &sink->segment, sizeof (GstSegment));
3309 /* now configure the final seek segment */
3311 if (sink->segment.format != seek_format) {
3312 /* OK, here's where we give the subclass a chance to convert the relative
3313 * seek into an absolute one in the processing format. We set up any
3314 * absolute seek above, before taking the stream lock. */
3315 if (!gst_base_sink_default_prepare_seek_segment (sink, event,
3317 GST_DEBUG_OBJECT (sink,
3318 "Preparing the seek failed after flushing. " "Aborting seek");
3322 /* The seek format matches our processing format, no need to ask the
3323 * the subclass to configure the segment. */
3324 gst_segment_set_seek (&seeksegment, rate, seek_format, flags,
3325 cur_type, cur, stop_type, stop, &update);
3328 /* Else, no seek event passed, so we're just (re)starting the
3333 GST_DEBUG_OBJECT (sink, "segment configured from %" G_GINT64_FORMAT
3334 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
3335 seeksegment.start, seeksegment.stop, seeksegment.last_stop);
3337 /* do the seek, segment.last_stop contains the new position. */
3338 res = gst_base_sink_default_do_seek (sink, &seeksegment);
3343 GST_DEBUG_OBJECT (sink, "stop flushing upstream");
3344 gst_pad_push_event (pad, gst_event_new_flush_stop ());
3345 gst_base_sink_flush_stop (sink, pad);
3346 } else if (res && sink->abidata.ABI.running) {
3347 /* we are running the current segment and doing a non-flushing seek,
3348 * close the segment first based on the last_stop. */
3349 GST_DEBUG_OBJECT (sink, "closing running segment %" G_GINT64_FORMAT
3350 " to %" G_GINT64_FORMAT, sink->segment.start, sink->segment.last_stop);
3353 /* The subclass must have converted the segment to the processing format
3355 if (res && seeksegment.format != dest_format) {
3356 GST_DEBUG_OBJECT (sink, "Subclass failed to prepare a seek segment "
3357 "in the correct format. Aborting seek.");
3361 /* if successfull seek, we update our real segment and push
3362 * out the new segment. */
3364 memcpy (&sink->segment, &seeksegment, sizeof (GstSegment));
3366 if (sink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3367 gst_element_post_message (GST_ELEMENT (sink),
3368 gst_message_new_segment_start (GST_OBJECT (sink),
3369 sink->segment.format, sink->segment.last_stop));
3373 sink->priv->discont = TRUE;
3374 sink->abidata.ABI.running = TRUE;
3376 GST_PAD_STREAM_UNLOCK (pad);
3382 gst_base_sink_perform_step (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3384 GstBaseSinkPrivate *priv;
3385 GstBaseSinkClass *bclass;
3386 gboolean flush, intermediate;
3391 GstStepInfo *pending;
3393 bclass = GST_BASE_SINK_GET_CLASS (sink);
3396 GST_DEBUG_OBJECT (sink, "performing step with event %p", event);
3398 gst_event_parse_step (event, &format, &amount, &rate, &flush, &intermediate);
3399 seqnum = gst_event_get_seqnum (event);
3401 pending = &priv->pending_step;
3404 /* we need to call ::unlock before locking PREROLL_LOCK
3405 * since we lock it before going into ::render */
3407 bclass->unlock (sink);
3409 GST_PAD_PREROLL_LOCK (sink->sinkpad);
3410 /* update the segment */
3411 pending->seqnum = seqnum;
3412 pending->format = format;
3413 pending->amount = amount;
3414 pending->position = 0;
3415 pending->rate = rate;
3416 pending->flush = flush;
3417 pending->intermediate = intermediate;
3418 pending->valid = TRUE;
3420 /* now that we have the PREROLL lock, clear our unlock request */
3421 if (bclass->unlock_stop)
3422 bclass->unlock_stop (sink);
3424 if (sink->priv->async_enabled) {
3425 /* and we need to commit our state again on the next
3426 * prerolled buffer */
3427 sink->playing_async = TRUE;
3428 priv->pending_step.need_preroll = TRUE;
3429 sink->need_preroll = FALSE;
3430 gst_element_lost_state_full (GST_ELEMENT_CAST (sink), FALSE);
3432 sink->priv->have_latency = TRUE;
3433 sink->need_preroll = FALSE;
3435 priv->call_preroll = TRUE;
3436 gst_base_sink_set_last_buffer (sink, NULL);
3437 gst_base_sink_reset_qos (sink);
3439 if (sink->clock_id) {
3440 gst_clock_id_unschedule (sink->clock_id);
3443 GST_DEBUG_OBJECT (sink, "signal waiter");
3444 GST_PAD_PREROLL_SIGNAL (sink->sinkpad);
3446 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
3455 gst_base_sink_loop (GstPad * pad)
3457 GstBaseSink *basesink;
3458 GstBuffer *buf = NULL;
3459 GstFlowReturn result;
3463 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
3465 g_assert (basesink->pad_mode == GST_ACTIVATE_PULL);
3467 if ((blocksize = basesink->priv->blocksize) == 0)
3470 offset = basesink->segment.last_stop;
3472 GST_DEBUG_OBJECT (basesink, "pulling %" G_GUINT64_FORMAT ", %u",
3475 result = gst_pad_pull_range (pad, offset, blocksize, &buf);
3476 if (G_UNLIKELY (result != GST_FLOW_OK))
3479 if (G_UNLIKELY (buf == NULL))
3482 offset += GST_BUFFER_SIZE (buf);
3484 gst_segment_set_last_stop (&basesink->segment, GST_FORMAT_BYTES, offset);
3486 GST_PAD_PREROLL_LOCK (pad);
3487 result = gst_base_sink_chain_unlocked (basesink, pad, buf);
3488 GST_PAD_PREROLL_UNLOCK (pad);
3489 if (G_UNLIKELY (result != GST_FLOW_OK))
3497 GST_LOG_OBJECT (basesink, "pausing task, reason %s",
3498 gst_flow_get_name (result));
3499 gst_pad_pause_task (pad);
3500 /* fatal errors and NOT_LINKED cause EOS */
3501 if (GST_FLOW_IS_FATAL (result) || result == GST_FLOW_NOT_LINKED) {
3502 if (result == GST_FLOW_UNEXPECTED) {
3503 /* perform EOS logic */
3504 if (basesink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3505 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3506 gst_message_new_segment_done (GST_OBJECT_CAST (basesink),
3507 basesink->segment.format, basesink->segment.last_stop));
3509 gst_base_sink_event (pad, gst_event_new_eos ());
3512 /* for fatal errors we post an error message, post the error
3513 * first so the app knows about the error first. */
3514 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
3515 (_("Internal data stream error.")),
3516 ("stream stopped, reason %s", gst_flow_get_name (result)));
3517 gst_base_sink_event (pad, gst_event_new_eos ());
3524 GST_LOG_OBJECT (basesink, "no buffer, pausing");
3525 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
3526 (_("Internal data flow error.")), ("element returned NULL buffer"));
3527 result = GST_FLOW_ERROR;
3533 gst_base_sink_set_flushing (GstBaseSink * basesink, GstPad * pad,
3536 GstBaseSinkClass *bclass;
3538 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3541 /* unlock any subclasses, we need to do this before grabbing the
3542 * PREROLL_LOCK since we hold this lock before going into ::render. */
3544 bclass->unlock (basesink);
3547 GST_PAD_PREROLL_LOCK (pad);
3548 basesink->flushing = flushing;
3550 /* step 1, now that we have the PREROLL lock, clear our unlock request */
3551 if (bclass->unlock_stop)
3552 bclass->unlock_stop (basesink);
3554 /* set need_preroll before we unblock the clock. If the clock is unblocked
3555 * before timing out, we can reuse the buffer for preroll. */
3556 basesink->need_preroll = TRUE;
3558 /* step 2, unblock clock sync (if any) or any other blocking thing */
3559 if (basesink->clock_id) {
3560 gst_clock_id_unschedule (basesink->clock_id);
3563 /* flush out the data thread if it's locked in finish_preroll, this will
3564 * also flush out the EOS state */
3565 GST_DEBUG_OBJECT (basesink,
3566 "flushing out data thread, need preroll to TRUE");
3567 gst_base_sink_preroll_queue_flush (basesink, pad);
3569 GST_PAD_PREROLL_UNLOCK (pad);
3575 gst_base_sink_default_activate_pull (GstBaseSink * basesink, gboolean active)
3581 result = gst_pad_start_task (basesink->sinkpad,
3582 (GstTaskFunction) gst_base_sink_loop, basesink->sinkpad);
3584 /* step 2, make sure streaming finishes */
3585 result = gst_pad_stop_task (basesink->sinkpad);
3592 gst_base_sink_pad_activate (GstPad * pad)
3594 gboolean result = FALSE;
3595 GstBaseSink *basesink;
3597 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3599 GST_DEBUG_OBJECT (basesink, "Trying pull mode first");
3601 gst_base_sink_set_flushing (basesink, pad, FALSE);
3603 /* we need to have the pull mode enabled */
3604 if (!basesink->can_activate_pull) {
3605 GST_DEBUG_OBJECT (basesink, "pull mode disabled");
3609 /* check if downstreams supports pull mode at all */
3610 if (!gst_pad_check_pull_range (pad)) {
3611 GST_DEBUG_OBJECT (basesink, "pull mode not supported");
3615 /* set the pad mode before starting the task so that it's in the
3616 * correct state for the new thread. also the sink set_caps and get_caps
3617 * function checks this */
3618 basesink->pad_mode = GST_ACTIVATE_PULL;
3620 /* we first try to negotiate a format so that when we try to activate
3621 * downstream, it knows about our format */
3622 if (!gst_base_sink_negotiate_pull (basesink)) {
3623 GST_DEBUG_OBJECT (basesink, "failed to negotiate in pull mode");
3627 /* ok activate now */
3628 if (!gst_pad_activate_pull (pad, TRUE)) {
3629 /* clear any pending caps */
3630 GST_OBJECT_LOCK (basesink);
3631 gst_caps_replace (&basesink->priv->pull_caps, NULL);
3632 GST_OBJECT_UNLOCK (basesink);
3633 GST_DEBUG_OBJECT (basesink, "failed to activate in pull mode");
3637 GST_DEBUG_OBJECT (basesink, "Success activating pull mode");
3641 /* push mode fallback */
3643 GST_DEBUG_OBJECT (basesink, "Falling back to push mode");
3644 if ((result = gst_pad_activate_push (pad, TRUE))) {
3645 GST_DEBUG_OBJECT (basesink, "Success activating push mode");
3650 GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
3651 gst_base_sink_set_flushing (basesink, pad, TRUE);
3654 gst_object_unref (basesink);
3660 gst_base_sink_pad_activate_push (GstPad * pad, gboolean active)
3663 GstBaseSink *basesink;
3665 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3668 if (!basesink->can_activate_push) {
3670 basesink->pad_mode = GST_ACTIVATE_NONE;
3673 basesink->pad_mode = GST_ACTIVATE_PUSH;
3676 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH)) {
3677 g_warning ("Internal GStreamer activation error!!!");
3680 gst_base_sink_set_flushing (basesink, pad, TRUE);
3682 basesink->pad_mode = GST_ACTIVATE_NONE;
3686 gst_object_unref (basesink);
3692 gst_base_sink_negotiate_pull (GstBaseSink * basesink)
3699 /* this returns the intersection between our caps and the peer caps. If there
3700 * is no peer, it returns NULL and we can't operate in pull mode so we can
3701 * fail the negotiation. */
3702 caps = gst_pad_get_allowed_caps (GST_BASE_SINK_PAD (basesink));
3703 if (caps == NULL || gst_caps_is_empty (caps))
3704 goto no_caps_possible;
3706 GST_DEBUG_OBJECT (basesink, "allowed caps: %" GST_PTR_FORMAT, caps);
3708 caps = gst_caps_make_writable (caps);
3709 /* get the first (prefered) format */
3710 gst_caps_truncate (caps);
3712 gst_pad_fixate_caps (GST_BASE_SINK_PAD (basesink), caps);
3714 GST_DEBUG_OBJECT (basesink, "fixated to: %" GST_PTR_FORMAT, caps);
3716 if (gst_caps_is_any (caps)) {
3717 GST_DEBUG_OBJECT (basesink, "caps were ANY after fixating, "
3719 /* neither side has template caps in this case, so they are prepared for
3720 pull() without setcaps() */
3722 } else if (gst_caps_is_fixed (caps)) {
3723 if (!gst_pad_set_caps (GST_BASE_SINK_PAD (basesink), caps))
3724 goto could_not_set_caps;
3726 GST_OBJECT_LOCK (basesink);
3727 gst_caps_replace (&basesink->priv->pull_caps, caps);
3728 GST_OBJECT_UNLOCK (basesink);
3733 gst_caps_unref (caps);
3739 GST_INFO_OBJECT (basesink, "Pipeline could not agree on caps");
3740 GST_DEBUG_OBJECT (basesink, "get_allowed_caps() returned EMPTY");
3742 gst_caps_unref (caps);
3747 GST_INFO_OBJECT (basesink, "Could not set caps: %" GST_PTR_FORMAT, caps);
3748 gst_caps_unref (caps);
3753 /* this won't get called until we implement an activate function */
3755 gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active)
3757 gboolean result = FALSE;
3758 GstBaseSink *basesink;
3759 GstBaseSinkClass *bclass;
3761 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3762 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3768 /* we mark we have a newsegment here because pull based
3769 * mode works just fine without having a newsegment before the
3771 format = GST_FORMAT_BYTES;
3773 gst_segment_init (&basesink->segment, format);
3774 gst_segment_init (basesink->abidata.ABI.clip_segment, format);
3775 GST_OBJECT_LOCK (basesink);
3776 basesink->have_newsegment = TRUE;
3777 GST_OBJECT_UNLOCK (basesink);
3779 /* get the peer duration in bytes */
3780 result = gst_pad_query_peer_duration (pad, &format, &duration);
3782 GST_DEBUG_OBJECT (basesink,
3783 "setting duration in bytes to %" G_GINT64_FORMAT, duration);
3784 gst_segment_set_duration (basesink->abidata.ABI.clip_segment, format,
3786 gst_segment_set_duration (&basesink->segment, format, duration);
3788 GST_DEBUG_OBJECT (basesink, "unknown duration");
3791 if (bclass->activate_pull)
3792 result = bclass->activate_pull (basesink, TRUE);
3797 goto activate_failed;
3800 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PULL)) {
3801 g_warning ("Internal GStreamer activation error!!!");
3804 result = gst_base_sink_set_flushing (basesink, pad, TRUE);
3805 if (bclass->activate_pull)
3806 result &= bclass->activate_pull (basesink, FALSE);
3807 basesink->pad_mode = GST_ACTIVATE_NONE;
3808 /* clear any pending caps */
3809 GST_OBJECT_LOCK (basesink);
3810 gst_caps_replace (&basesink->priv->pull_caps, NULL);
3811 GST_OBJECT_UNLOCK (basesink);
3814 gst_object_unref (basesink);
3821 /* reset, as starting the thread failed */
3822 basesink->pad_mode = GST_ACTIVATE_NONE;
3824 GST_ERROR_OBJECT (basesink, "subclass failed to activate in pull mode");
3829 /* send an event to our sinkpad peer. */
3831 gst_base_sink_send_event (GstElement * element, GstEvent * event)
3834 GstBaseSink *basesink = GST_BASE_SINK (element);
3835 gboolean forward, result = TRUE;
3836 GstActivateMode mode;
3838 GST_OBJECT_LOCK (element);
3839 /* get the pad and the scheduling mode */
3840 pad = gst_object_ref (basesink->sinkpad);
3841 mode = basesink->pad_mode;
3842 GST_OBJECT_UNLOCK (element);
3844 /* only push UPSTREAM events upstream */
3845 forward = GST_EVENT_IS_UPSTREAM (event);
3847 switch (GST_EVENT_TYPE (event)) {
3848 case GST_EVENT_LATENCY:
3850 GstClockTime latency;
3852 gst_event_parse_latency (event, &latency);
3854 /* store the latency. We use this to adjust the running_time before syncing
3855 * it to the clock. */
3856 GST_OBJECT_LOCK (element);
3857 basesink->priv->latency = latency;
3858 if (!basesink->priv->have_latency)
3860 GST_OBJECT_UNLOCK (element);
3861 GST_DEBUG_OBJECT (basesink, "latency set to %" GST_TIME_FORMAT,
3862 GST_TIME_ARGS (latency));
3864 /* We forward this event so that all elements know about the global pipeline
3865 * latency. This is interesting for an element when it wants to figure out
3866 * when a particular piece of data will be rendered. */
3869 case GST_EVENT_SEEK:
3870 /* in pull mode we will execute the seek */
3871 if (mode == GST_ACTIVATE_PULL)
3872 result = gst_base_sink_perform_seek (basesink, pad, event);
3874 case GST_EVENT_STEP:
3875 result = gst_base_sink_perform_step (basesink, pad, event);
3883 result = gst_pad_push_event (pad, event);
3885 /* not forwarded, unref the event */
3886 gst_event_unref (event);
3889 gst_object_unref (pad);
3894 gst_base_sink_peer_query (GstBaseSink * sink, GstQuery * query)
3897 gboolean res = FALSE;
3899 if ((peer = gst_pad_get_peer (sink->sinkpad))) {
3900 res = gst_pad_query (peer, query);
3901 gst_object_unref (peer);
3906 /* get the end position of the last seen object, this is used
3907 * for EOS and for making sure that we don't report a position we
3908 * have not reached yet. With LOCK. */
3910 gst_base_sink_get_position_last (GstBaseSink * basesink, GstFormat format,
3914 GstSegment *segment;
3915 gboolean ret = TRUE;
3917 segment = &basesink->segment;
3918 oformat = segment->format;
3920 if (oformat == GST_FORMAT_TIME) {
3921 /* return last observed stream time, we keep the stream time around in the
3923 *cur = basesink->priv->current_sstop;
3925 /* convert last stop to stream time */
3926 *cur = gst_segment_to_stream_time (segment, oformat, segment->last_stop);
3929 if (*cur != -1 && oformat != format) {
3930 GST_OBJECT_UNLOCK (basesink);
3931 /* convert to the target format if we need to, release lock first */
3933 gst_pad_query_convert (basesink->sinkpad, oformat, *cur, &format, cur);
3936 GST_OBJECT_LOCK (basesink);
3939 GST_DEBUG_OBJECT (basesink, "POSITION: %" GST_TIME_FORMAT,
3940 GST_TIME_ARGS (*cur));
3945 /* get the position when we are PAUSED, this is the stream time of the buffer
3946 * that prerolled. If no buffer is prerolled (we are still flushing), this
3947 * value will be -1. With LOCK. */
3949 gst_base_sink_get_position_paused (GstBaseSink * basesink, GstFormat format,
3954 GstSegment *segment;
3957 /* we don't use the clip segment in pull mode, when seeking we update the
3958 * main segment directly with the new segment values without it having to be
3959 * activated by the rendering after preroll */
3960 if (basesink->pad_mode == GST_ACTIVATE_PUSH)
3961 segment = basesink->abidata.ABI.clip_segment;
3963 segment = &basesink->segment;
3964 oformat = segment->format;
3966 if (oformat == GST_FORMAT_TIME) {
3967 *cur = basesink->priv->current_sstart;
3969 *cur = gst_segment_to_stream_time (segment, oformat, segment->last_stop);
3972 time = segment->time;
3975 *cur = MAX (*cur, time);
3976 GST_DEBUG_OBJECT (basesink, "POSITION as max: %" GST_TIME_FORMAT
3977 ", time %" GST_TIME_FORMAT, GST_TIME_ARGS (*cur), GST_TIME_ARGS (time));
3979 /* we have no buffer, use the segment times. */
3980 if (segment->rate >= 0.0) {
3981 /* forward, next position is always the time of the segment */
3983 GST_DEBUG_OBJECT (basesink, "POSITION as time: %" GST_TIME_FORMAT,
3984 GST_TIME_ARGS (*cur));
3986 /* reverse, next expected timestamp is segment->stop. We use the function
3987 * to get things right for negative applied_rates. */
3988 *cur = gst_segment_to_stream_time (segment, oformat, segment->stop);
3989 GST_DEBUG_OBJECT (basesink, "reverse POSITION: %" GST_TIME_FORMAT,
3990 GST_TIME_ARGS (*cur));
3995 if (res && oformat != format) {
3996 GST_OBJECT_UNLOCK (basesink);
3998 gst_pad_query_convert (basesink->sinkpad, oformat, *cur, &format, cur);
4001 GST_OBJECT_LOCK (basesink);
4008 gst_base_sink_get_position (GstBaseSink * basesink, GstFormat format,
4009 gint64 * cur, gboolean * upstream)
4012 gboolean res = FALSE;
4013 GstFormat oformat, tformat;
4014 GstClockTime now, base, latency;
4015 gint64 time, accum, duration;
4019 GST_OBJECT_LOCK (basesink);
4020 /* our intermediate time format */
4021 tformat = GST_FORMAT_TIME;
4022 /* get the format in the segment */
4023 oformat = basesink->segment.format;
4025 /* can only give answer based on the clock if not EOS */
4026 if (G_UNLIKELY (basesink->eos))
4029 /* we can only get the segment when we are not NULL or READY */
4030 if (!basesink->have_newsegment)
4033 /* when not in PLAYING or when we're busy with a state change, we
4034 * cannot read from the clock so we report time based on the
4035 * last seen timestamp. */
4036 if (GST_STATE (basesink) != GST_STATE_PLAYING ||
4037 GST_STATE_PENDING (basesink) != GST_STATE_VOID_PENDING)
4040 /* we need to sync on the clock. */
4041 if (basesink->sync == FALSE)
4044 /* and we need a clock */
4045 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (basesink)) == NULL))
4048 /* collect all data we need holding the lock */
4049 if (GST_CLOCK_TIME_IS_VALID (basesink->segment.time))
4050 time = basesink->segment.time;
4054 if (GST_CLOCK_TIME_IS_VALID (basesink->segment.stop))
4055 duration = basesink->segment.stop - basesink->segment.start;
4059 base = GST_ELEMENT_CAST (basesink)->base_time;
4060 accum = basesink->segment.accum;
4061 rate = basesink->segment.rate * basesink->segment.applied_rate;
4062 latency = basesink->priv->latency;
4064 gst_object_ref (clock);
4066 /* this function might release the LOCK */
4067 gst_base_sink_get_position_last (basesink, format, &last);
4069 /* need to release the object lock before we can get the time,
4070 * a clock might take the LOCK of the provider, which could be
4071 * a basesink subclass. */
4072 GST_OBJECT_UNLOCK (basesink);
4074 now = gst_clock_get_time (clock);
4076 if (oformat != tformat) {
4077 /* convert accum, time and duration to time */
4078 if (!gst_pad_query_convert (basesink->sinkpad, oformat, accum, &tformat,
4080 goto convert_failed;
4081 if (!gst_pad_query_convert (basesink->sinkpad, oformat, duration, &tformat,
4083 goto convert_failed;
4084 if (!gst_pad_query_convert (basesink->sinkpad, oformat, time, &tformat,
4086 goto convert_failed;
4089 /* subtract base time and accumulated time from the clock time.
4090 * Make sure we don't go negative. This is the current time in
4091 * the segment which we need to scale with the combined
4092 * rate and applied rate. */
4095 base = MIN (now, base);
4097 /* for negative rates we need to count back from from the segment
4102 *cur = time + gst_guint64_to_gdouble (now - base) * rate;
4104 /* never report more than last seen position */
4106 *cur = MIN (last, *cur);
4108 gst_object_unref (clock);
4110 GST_DEBUG_OBJECT (basesink,
4111 "now %" GST_TIME_FORMAT " - base %" GST_TIME_FORMAT " - accum %"
4112 GST_TIME_FORMAT " + time %" GST_TIME_FORMAT,
4113 GST_TIME_ARGS (now), GST_TIME_ARGS (base),
4114 GST_TIME_ARGS (accum), GST_TIME_ARGS (time));
4116 if (oformat != format) {
4117 /* convert time to final format */
4118 if (!gst_pad_query_convert (basesink->sinkpad, tformat, *cur, &format, cur))
4119 goto convert_failed;
4125 GST_DEBUG_OBJECT (basesink, "res: %d, POSITION: %" GST_TIME_FORMAT,
4126 res, GST_TIME_ARGS (*cur));
4132 GST_DEBUG_OBJECT (basesink, "position in EOS");
4133 res = gst_base_sink_get_position_last (basesink, format, cur);
4134 GST_OBJECT_UNLOCK (basesink);
4139 GST_DEBUG_OBJECT (basesink, "position in PAUSED");
4140 res = gst_base_sink_get_position_paused (basesink, format, cur);
4141 GST_OBJECT_UNLOCK (basesink);
4146 /* in NULL or READY we always return FALSE and -1 */
4147 GST_DEBUG_OBJECT (basesink, "position in wrong state, return -1");
4150 GST_OBJECT_UNLOCK (basesink);
4155 /* report last seen timestamp if any, else ask upstream to answer */
4156 if ((*cur = basesink->priv->current_sstart) != -1)
4161 GST_DEBUG_OBJECT (basesink, "no sync, res %d, POSITION %" GST_TIME_FORMAT,
4162 res, GST_TIME_ARGS (*cur));
4163 GST_OBJECT_UNLOCK (basesink);
4168 GST_DEBUG_OBJECT (basesink, "convert failed, try upstream");
4175 gst_base_sink_query (GstElement * element, GstQuery * query)
4177 gboolean res = FALSE;
4179 GstBaseSink *basesink = GST_BASE_SINK (element);
4181 switch (GST_QUERY_TYPE (query)) {
4182 case GST_QUERY_POSITION:
4186 gboolean upstream = FALSE;
4188 gst_query_parse_position (query, &format, NULL);
4190 GST_DEBUG_OBJECT (basesink, "position format %d", format);
4192 /* first try to get the position based on the clock */
4194 gst_base_sink_get_position (basesink, format, &cur, &upstream))) {
4195 gst_query_set_position (query, format, cur);
4196 } else if (upstream) {
4197 /* fallback to peer query */
4198 res = gst_base_sink_peer_query (basesink, query);
4202 case GST_QUERY_DURATION:
4204 GstFormat format, uformat;
4205 gint64 duration, uduration;
4207 gst_query_parse_duration (query, &format, NULL);
4209 GST_DEBUG_OBJECT (basesink, "duration query in format %s",
4210 gst_format_get_name (format));
4212 if (basesink->pad_mode == GST_ACTIVATE_PULL) {
4213 uformat = GST_FORMAT_BYTES;
4215 /* get the duration in bytes, in pull mode that's all we are sure to
4216 * know. We have to explicitly get this value from upstream instead of
4217 * using our cached value because it might change. Duration caching
4218 * should be done at a higher level. */
4219 res = gst_pad_query_peer_duration (basesink->sinkpad, &uformat,
4222 gst_segment_set_duration (&basesink->segment, uformat, uduration);
4223 if (format != uformat) {
4224 /* convert to the requested format */
4225 res = gst_pad_query_convert (basesink->sinkpad, uformat, uduration,
4226 &format, &duration);
4228 duration = uduration;
4231 /* set the result */
4232 gst_query_set_duration (query, format, duration);
4236 /* in push mode we simply forward upstream */
4237 res = gst_base_sink_peer_query (basesink, query);
4241 case GST_QUERY_LATENCY:
4243 gboolean live, us_live;
4244 GstClockTime min, max;
4246 if ((res = gst_base_sink_query_latency (basesink, &live, &us_live, &min,
4248 gst_query_set_latency (query, live, min, max);
4252 case GST_QUERY_JITTER:
4254 case GST_QUERY_RATE:
4255 /* gst_query_set_rate (query, basesink->segment_rate); */
4258 case GST_QUERY_SEGMENT:
4260 /* FIXME, bring start/stop to stream time */
4261 gst_query_set_segment (query, basesink->segment.rate,
4262 GST_FORMAT_TIME, basesink->segment.start, basesink->segment.stop);
4265 case GST_QUERY_SEEKING:
4266 case GST_QUERY_CONVERT:
4267 case GST_QUERY_FORMATS:
4269 res = gst_base_sink_peer_query (basesink, query);
4275 static GstStateChangeReturn
4276 gst_base_sink_change_state (GstElement * element, GstStateChange transition)
4278 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
4279 GstBaseSink *basesink = GST_BASE_SINK (element);
4280 GstBaseSinkClass *bclass;
4281 GstBaseSinkPrivate *priv;
4283 priv = basesink->priv;
4285 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4287 switch (transition) {
4288 case GST_STATE_CHANGE_NULL_TO_READY:
4290 if (!bclass->start (basesink))
4293 case GST_STATE_CHANGE_READY_TO_PAUSED:
4294 /* need to complete preroll before this state change completes, there
4295 * is no data flow in READY so we can safely assume we need to preroll. */
4296 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4297 GST_DEBUG_OBJECT (basesink, "READY to PAUSED");
4298 basesink->have_newsegment = FALSE;
4299 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
4300 gst_segment_init (basesink->abidata.ABI.clip_segment,
4301 GST_FORMAT_UNDEFINED);
4302 basesink->offset = 0;
4303 basesink->have_preroll = FALSE;
4304 basesink->need_preroll = TRUE;
4305 basesink->playing_async = TRUE;
4306 priv->current_sstart = -1;
4307 priv->current_sstop = -1;
4308 priv->eos_rtime = -1;
4310 basesink->eos = FALSE;
4311 priv->received_eos = FALSE;
4312 gst_base_sink_reset_qos (basesink);
4313 priv->commited = FALSE;
4314 priv->call_preroll = TRUE;
4315 priv->current_step.valid = FALSE;
4316 priv->pending_step.valid = FALSE;
4317 if (priv->async_enabled) {
4318 GST_DEBUG_OBJECT (basesink, "doing async state change");
4319 /* when async enabled, post async-start message and return ASYNC from
4320 * the state change function */
4321 ret = GST_STATE_CHANGE_ASYNC;
4322 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4323 gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
4325 priv->have_latency = TRUE;
4327 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4329 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
4330 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4331 if (!gst_base_sink_needs_preroll (basesink)) {
4332 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, don't need preroll");
4333 /* no preroll needed anymore now. */
4334 basesink->playing_async = FALSE;
4335 basesink->need_preroll = FALSE;
4336 if (basesink->eos) {
4337 GstMessage *message;
4339 /* need to post EOS message here */
4340 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
4341 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
4342 gst_message_set_seqnum (message, basesink->priv->seqnum);
4343 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
4345 GST_DEBUG_OBJECT (basesink, "signal preroll");
4346 GST_PAD_PREROLL_SIGNAL (basesink->sinkpad);
4349 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, we are not prerolled");
4350 basesink->need_preroll = TRUE;
4351 basesink->playing_async = TRUE;
4352 priv->call_preroll = TRUE;
4353 priv->commited = FALSE;
4354 if (priv->async_enabled) {
4355 GST_DEBUG_OBJECT (basesink, "doing async state change");
4356 ret = GST_STATE_CHANGE_ASYNC;
4357 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4358 gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
4361 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4368 GstStateChangeReturn bret;
4370 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4371 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
4372 goto activate_failed;
4375 switch (transition) {
4376 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
4377 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED");
4378 /* FIXME, make sure we cannot enter _render first */
4380 /* we need to call ::unlock before locking PREROLL_LOCK
4381 * since we lock it before going into ::render */
4383 bclass->unlock (basesink);
4385 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4386 /* now that we have the PREROLL lock, clear our unlock request */
4387 if (bclass->unlock_stop)
4388 bclass->unlock_stop (basesink);
4390 /* we need preroll again and we set the flag before unlocking the clockid
4391 * because if the clockid is unlocked before a current buffer expired, we
4392 * can use that buffer to preroll with */
4393 basesink->need_preroll = TRUE;
4395 if (basesink->clock_id) {
4396 gst_clock_id_unschedule (basesink->clock_id);
4399 /* if we don't have a preroll buffer we need to wait for a preroll and
4401 if (!gst_base_sink_needs_preroll (basesink)) {
4402 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, we are prerolled");
4403 basesink->playing_async = FALSE;
4405 if (GST_STATE_TARGET (GST_ELEMENT (basesink)) <= GST_STATE_READY) {
4406 ret = GST_STATE_CHANGE_SUCCESS;
4408 GST_DEBUG_OBJECT (basesink,
4409 "PLAYING to PAUSED, we are not prerolled");
4410 basesink->playing_async = TRUE;
4411 priv->commited = FALSE;
4412 priv->call_preroll = TRUE;
4413 if (priv->async_enabled) {
4414 GST_DEBUG_OBJECT (basesink, "doing async state change");
4415 ret = GST_STATE_CHANGE_ASYNC;
4416 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4417 gst_message_new_async_start (GST_OBJECT_CAST (basesink),
4422 GST_DEBUG_OBJECT (basesink, "rendered: %" G_GUINT64_FORMAT
4423 ", dropped: %" G_GUINT64_FORMAT, priv->rendered, priv->dropped);
4425 gst_base_sink_reset_qos (basesink);
4426 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4428 case GST_STATE_CHANGE_PAUSED_TO_READY:
4429 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4430 /* start by reseting our position state with the object lock so that the
4431 * position query gets the right idea. We do this before we post the
4432 * messages so that the message handlers pick this up. */
4433 GST_OBJECT_LOCK (basesink);
4434 basesink->have_newsegment = FALSE;
4435 priv->current_sstart = -1;
4436 priv->current_sstop = -1;
4437 priv->have_latency = FALSE;
4438 GST_OBJECT_UNLOCK (basesink);
4440 gst_base_sink_set_last_buffer (basesink, NULL);
4441 priv->call_preroll = FALSE;
4443 if (!priv->commited) {
4444 if (priv->async_enabled) {
4445 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, posting async-done");
4447 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4448 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
4449 GST_STATE_PLAYING, GST_STATE_PAUSED, GST_STATE_READY));
4451 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4452 gst_message_new_async_done (GST_OBJECT_CAST (basesink)));
4454 priv->commited = TRUE;
4456 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, don't need_preroll");
4458 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4460 case GST_STATE_CHANGE_READY_TO_NULL:
4462 if (!bclass->stop (basesink)) {
4463 GST_WARNING_OBJECT (basesink, "failed to stop");
4466 gst_base_sink_set_last_buffer (basesink, NULL);
4467 priv->call_preroll = FALSE;
4478 GST_DEBUG_OBJECT (basesink, "failed to start");
4479 return GST_STATE_CHANGE_FAILURE;
4483 GST_DEBUG_OBJECT (basesink,
4484 "element failed to change states -- activation problem?");
4485 return GST_STATE_CHANGE_FAILURE;