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))
155 #define GST_FLOW_STEP GST_FLOW_CUSTOM_ERROR
159 gboolean valid; /* if this info is valid */
160 guint32 seqnum; /* the seqnum of the STEP event */
161 GstFormat format; /* the format of the amount */
162 guint64 amount; /* the total amount of data to skip */
163 guint64 position; /* the position in the stepped data */
164 guint64 duration; /* the duration in time of the skipped data */
165 guint64 start; /* running_time of the start */
166 gdouble rate; /* rate of skipping */
167 gdouble start_rate; /* rate before skipping */
168 guint64 start_stop; /* stop position skipping */
169 gboolean flush; /* if this was a flushing step */
170 gboolean intermediate; /* if this is an intermediate step */
171 gboolean need_preroll; /* if we need preroll after this step */
174 /* FIXME, some stuff in ABI.data and other in Private...
175 * Make up your mind please.
177 struct _GstBaseSinkPrivate
179 gint qos_enabled; /* ATOMIC */
180 gboolean async_enabled;
181 GstClockTimeDiff ts_offset;
182 GstClockTime render_delay;
184 /* start, stop of current buffer, stream time, used to report position */
185 GstClockTime current_sstart;
186 GstClockTime current_sstop;
188 /* start, stop and jitter of current buffer, running time */
189 GstClockTime current_rstart;
190 GstClockTime current_rstop;
191 GstClockTimeDiff current_jitter;
193 /* EOS sync time in running time */
194 GstClockTime eos_rtime;
196 /* last buffer that arrived in time, running time */
197 GstClockTime last_in_time;
198 /* when the last buffer left the sink, running time */
199 GstClockTime last_left;
201 /* running averages go here these are done on running time */
203 GstClockTime avg_duration;
206 /* these are done on system time. avg_jitter and avg_render are
207 * compared to eachother to see if the rendering time takes a
208 * huge amount of the processing, If so we are flooded with
210 GstClockTime last_left_systime;
211 GstClockTime avg_jitter;
212 GstClockTime start, stop;
213 GstClockTime avg_render;
215 /* number of rendered and dropped frames */
220 GstClockTime latency;
222 /* if we already commited the state */
225 /* when we received EOS */
226 gboolean received_eos;
228 /* when we are prerolled and able to report latency */
229 gboolean have_latency;
231 /* the last buffer we prerolled or rendered. Useful for making snapshots */
232 GstBuffer *last_buffer;
234 /* caps for pull based scheduling */
237 /* blocksize for pulling */
242 /* seqnum of the stream */
245 gboolean call_preroll;
246 gboolean step_unlock;
248 /* we have a pending and a current step operation */
249 GstStepInfo current_step;
250 GstStepInfo pending_step;
253 #define DO_RUNNING_AVG(avg,val,size) (((val) + ((size)-1) * (avg)) / (size))
255 /* generic running average, this has a neutral window size */
256 #define UPDATE_RUNNING_AVG(avg,val) DO_RUNNING_AVG(avg,val,8)
258 /* the windows for these running averages are experimentally obtained.
259 * possitive values get averaged more while negative values use a small
260 * window so we can react faster to badness. */
261 #define UPDATE_RUNNING_AVG_P(avg,val) DO_RUNNING_AVG(avg,val,16)
262 #define UPDATE_RUNNING_AVG_N(avg,val) DO_RUNNING_AVG(avg,val,4)
264 /* BaseSink properties */
266 #define DEFAULT_CAN_ACTIVATE_PULL FALSE /* fixme: enable me */
267 #define DEFAULT_CAN_ACTIVATE_PUSH TRUE
269 #define DEFAULT_PREROLL_QUEUE_LEN 0
270 #define DEFAULT_SYNC TRUE
271 #define DEFAULT_MAX_LATENESS -1
272 #define DEFAULT_QOS FALSE
273 #define DEFAULT_ASYNC TRUE
274 #define DEFAULT_TS_OFFSET 0
275 #define DEFAULT_BLOCKSIZE 4096
276 #define DEFAULT_RENDER_DELAY 0
281 PROP_PREROLL_QUEUE_LEN,
293 static GstElementClass *parent_class = NULL;
295 static void gst_base_sink_class_init (GstBaseSinkClass * klass);
296 static void gst_base_sink_init (GstBaseSink * trans, gpointer g_class);
297 static void gst_base_sink_finalize (GObject * object);
300 gst_base_sink_get_type (void)
302 static volatile gsize base_sink_type = 0;
304 if (g_once_init_enter (&base_sink_type)) {
306 static const GTypeInfo base_sink_info = {
307 sizeof (GstBaseSinkClass),
310 (GClassInitFunc) gst_base_sink_class_init,
313 sizeof (GstBaseSink),
315 (GInstanceInitFunc) gst_base_sink_init,
318 _type = g_type_register_static (GST_TYPE_ELEMENT,
319 "GstBaseSink", &base_sink_info, G_TYPE_FLAG_ABSTRACT);
320 g_once_init_leave (&base_sink_type, _type);
322 return base_sink_type;
325 static void gst_base_sink_set_property (GObject * object, guint prop_id,
326 const GValue * value, GParamSpec * pspec);
327 static void gst_base_sink_get_property (GObject * object, guint prop_id,
328 GValue * value, GParamSpec * pspec);
330 static gboolean gst_base_sink_send_event (GstElement * element,
332 static gboolean gst_base_sink_query (GstElement * element, GstQuery * query);
334 static GstCaps *gst_base_sink_get_caps (GstBaseSink * sink);
335 static gboolean gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps);
336 static GstFlowReturn gst_base_sink_buffer_alloc (GstBaseSink * sink,
337 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
338 static void gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
339 GstClockTime * start, GstClockTime * end);
340 static gboolean gst_base_sink_set_flushing (GstBaseSink * basesink,
341 GstPad * pad, gboolean flushing);
342 static gboolean gst_base_sink_default_activate_pull (GstBaseSink * basesink,
344 static gboolean gst_base_sink_default_do_seek (GstBaseSink * sink,
345 GstSegment * segment);
346 static gboolean gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
347 GstEvent * event, GstSegment * segment);
349 static GstStateChangeReturn gst_base_sink_change_state (GstElement * element,
350 GstStateChange transition);
352 static GstFlowReturn gst_base_sink_chain (GstPad * pad, GstBuffer * buffer);
353 static void gst_base_sink_loop (GstPad * pad);
354 static gboolean gst_base_sink_pad_activate (GstPad * pad);
355 static gboolean gst_base_sink_pad_activate_push (GstPad * pad, gboolean active);
356 static gboolean gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active);
357 static gboolean gst_base_sink_event (GstPad * pad, GstEvent * event);
358 static gboolean gst_base_sink_peer_query (GstBaseSink * sink, GstQuery * query);
360 static gboolean gst_base_sink_negotiate_pull (GstBaseSink * basesink);
362 /* check if an object was too late */
363 static gboolean gst_base_sink_is_too_late (GstBaseSink * basesink,
364 GstMiniObject * obj, GstClockTime start, GstClockTime stop,
365 GstClockReturn status, GstClockTimeDiff jitter);
366 static GstFlowReturn gst_base_sink_preroll_object (GstBaseSink * basesink,
367 GstMiniObject * obj);
370 gst_base_sink_class_init (GstBaseSinkClass * klass)
372 GObjectClass *gobject_class;
373 GstElementClass *gstelement_class;
375 gobject_class = G_OBJECT_CLASS (klass);
376 gstelement_class = GST_ELEMENT_CLASS (klass);
378 GST_DEBUG_CATEGORY_INIT (gst_base_sink_debug, "basesink", 0,
381 g_type_class_add_private (klass, sizeof (GstBaseSinkPrivate));
383 parent_class = g_type_class_peek_parent (klass);
385 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_sink_finalize);
386 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_base_sink_set_property);
387 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_base_sink_get_property);
389 /* FIXME, this next value should be configured using an event from the
390 * upstream element, ie, the BUFFER_SIZE event. */
391 g_object_class_install_property (gobject_class, PROP_PREROLL_QUEUE_LEN,
392 g_param_spec_uint ("preroll-queue-len", "Preroll queue length",
393 "Number of buffers to queue during preroll", 0, G_MAXUINT,
394 DEFAULT_PREROLL_QUEUE_LEN,
395 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
397 g_object_class_install_property (gobject_class, PROP_SYNC,
398 g_param_spec_boolean ("sync", "Sync", "Sync on the clock", DEFAULT_SYNC,
399 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
401 g_object_class_install_property (gobject_class, PROP_MAX_LATENESS,
402 g_param_spec_int64 ("max-lateness", "Max Lateness",
403 "Maximum number of nanoseconds that a buffer can be late before it "
404 "is dropped (-1 unlimited)", -1, G_MAXINT64, DEFAULT_MAX_LATENESS,
405 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
407 g_object_class_install_property (gobject_class, PROP_QOS,
408 g_param_spec_boolean ("qos", "Qos",
409 "Generate Quality-of-Service events upstream", DEFAULT_QOS,
410 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
414 * If set to #TRUE, the basesink will perform asynchronous state changes.
415 * When set to #FALSE, the sink will not signal the parent when it prerolls.
416 * Use this option when dealing with sparse streams or when synchronisation is
421 g_object_class_install_property (gobject_class, PROP_ASYNC,
422 g_param_spec_boolean ("async", "Async",
423 "Go asynchronously to PAUSED", DEFAULT_ASYNC,
424 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
426 * GstBaseSink:ts-offset
428 * Controls the final synchronisation, a negative value will render the buffer
429 * earlier while a positive value delays playback. This property can be
430 * used to fix synchronisation in bad files.
434 g_object_class_install_property (gobject_class, PROP_TS_OFFSET,
435 g_param_spec_int64 ("ts-offset", "TS Offset",
436 "Timestamp offset in nanoseconds", G_MININT64, G_MAXINT64,
437 DEFAULT_TS_OFFSET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
439 * GstBaseSink:last-buffer
441 * The last buffer that arrived in the sink and was used for preroll or for
442 * rendering. This property can be used to generate thumbnails. This property
443 * can be NULL when the sink has not yet received a bufer.
447 g_object_class_install_property (gobject_class, PROP_LAST_BUFFER,
448 gst_param_spec_mini_object ("last-buffer", "Last Buffer",
449 "The last buffer received in the sink", GST_TYPE_BUFFER,
450 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
452 * GstBaseSink:blocksize
454 * The amount of bytes to pull when operating in pull mode.
458 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
459 g_param_spec_uint ("blocksize", "Block size",
460 "Size in bytes to pull per buffer (0 = default)", 0, G_MAXUINT,
461 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
463 * GstBaseSink:render-delay
465 * The additional delay between synchronisation and actual rendering of the
466 * media. This property will add additional latency to the device in order to
467 * make other sinks compensate for the delay.
471 g_object_class_install_property (gobject_class, PROP_RENDER_DELAY,
472 g_param_spec_uint64 ("render-delay", "Render Delay",
473 "Additional render delay of the sink in nanoseconds", 0, G_MAXUINT64,
474 DEFAULT_RENDER_DELAY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
476 gstelement_class->change_state =
477 GST_DEBUG_FUNCPTR (gst_base_sink_change_state);
478 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_sink_send_event);
479 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_base_sink_query);
481 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_sink_get_caps);
482 klass->set_caps = GST_DEBUG_FUNCPTR (gst_base_sink_set_caps);
483 klass->buffer_alloc = GST_DEBUG_FUNCPTR (gst_base_sink_buffer_alloc);
484 klass->get_times = GST_DEBUG_FUNCPTR (gst_base_sink_get_times);
485 klass->activate_pull =
486 GST_DEBUG_FUNCPTR (gst_base_sink_default_activate_pull);
490 gst_base_sink_pad_getcaps (GstPad * pad)
492 GstBaseSinkClass *bclass;
494 GstCaps *caps = NULL;
496 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
497 bclass = GST_BASE_SINK_GET_CLASS (bsink);
499 if (bsink->pad_mode == GST_ACTIVATE_PULL) {
500 /* if we are operating in pull mode we only accept the negotiated caps */
501 GST_OBJECT_LOCK (pad);
502 if ((caps = GST_PAD_CAPS (pad)))
504 GST_OBJECT_UNLOCK (pad);
507 if (bclass->get_caps)
508 caps = bclass->get_caps (bsink);
511 GstPadTemplate *pad_template;
514 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass),
516 if (pad_template != NULL) {
517 caps = gst_caps_ref (gst_pad_template_get_caps (pad_template));
521 gst_object_unref (bsink);
527 gst_base_sink_pad_setcaps (GstPad * pad, GstCaps * caps)
529 GstBaseSinkClass *bclass;
533 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
534 bclass = GST_BASE_SINK_GET_CLASS (bsink);
536 if (res && bclass->set_caps)
537 res = bclass->set_caps (bsink, caps);
539 gst_object_unref (bsink);
545 gst_base_sink_pad_fixate (GstPad * pad, GstCaps * caps)
547 GstBaseSinkClass *bclass;
550 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
551 bclass = GST_BASE_SINK_GET_CLASS (bsink);
554 bclass->fixate (bsink, caps);
556 gst_object_unref (bsink);
560 gst_base_sink_pad_buffer_alloc (GstPad * pad, guint64 offset, guint size,
561 GstCaps * caps, GstBuffer ** buf)
563 GstBaseSinkClass *bclass;
565 GstFlowReturn result = GST_FLOW_OK;
567 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
568 bclass = GST_BASE_SINK_GET_CLASS (bsink);
570 if (bclass->buffer_alloc)
571 result = bclass->buffer_alloc (bsink, offset, size, caps, buf);
573 *buf = NULL; /* fallback in gstpad.c will allocate generic buffer */
575 gst_object_unref (bsink);
581 gst_base_sink_init (GstBaseSink * basesink, gpointer g_class)
583 GstPadTemplate *pad_template;
584 GstBaseSinkPrivate *priv;
586 basesink->priv = priv = GST_BASE_SINK_GET_PRIVATE (basesink);
589 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
590 g_return_if_fail (pad_template != NULL);
592 basesink->sinkpad = gst_pad_new_from_template (pad_template, "sink");
594 gst_pad_set_getcaps_function (basesink->sinkpad,
595 GST_DEBUG_FUNCPTR (gst_base_sink_pad_getcaps));
596 gst_pad_set_setcaps_function (basesink->sinkpad,
597 GST_DEBUG_FUNCPTR (gst_base_sink_pad_setcaps));
598 gst_pad_set_fixatecaps_function (basesink->sinkpad,
599 GST_DEBUG_FUNCPTR (gst_base_sink_pad_fixate));
600 gst_pad_set_bufferalloc_function (basesink->sinkpad,
601 GST_DEBUG_FUNCPTR (gst_base_sink_pad_buffer_alloc));
602 gst_pad_set_activate_function (basesink->sinkpad,
603 GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate));
604 gst_pad_set_activatepush_function (basesink->sinkpad,
605 GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate_push));
606 gst_pad_set_activatepull_function (basesink->sinkpad,
607 GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate_pull));
608 gst_pad_set_event_function (basesink->sinkpad,
609 GST_DEBUG_FUNCPTR (gst_base_sink_event));
610 gst_pad_set_chain_function (basesink->sinkpad,
611 GST_DEBUG_FUNCPTR (gst_base_sink_chain));
612 gst_element_add_pad (GST_ELEMENT_CAST (basesink), basesink->sinkpad);
614 basesink->pad_mode = GST_ACTIVATE_NONE;
615 basesink->preroll_queue = g_queue_new ();
616 basesink->abidata.ABI.clip_segment = gst_segment_new ();
617 priv->have_latency = FALSE;
619 basesink->can_activate_push = DEFAULT_CAN_ACTIVATE_PUSH;
620 basesink->can_activate_pull = DEFAULT_CAN_ACTIVATE_PULL;
622 basesink->sync = DEFAULT_SYNC;
623 basesink->abidata.ABI.max_lateness = DEFAULT_MAX_LATENESS;
624 g_atomic_int_set (&priv->qos_enabled, DEFAULT_QOS);
625 priv->async_enabled = DEFAULT_ASYNC;
626 priv->ts_offset = DEFAULT_TS_OFFSET;
627 priv->render_delay = DEFAULT_RENDER_DELAY;
628 priv->blocksize = DEFAULT_BLOCKSIZE;
630 GST_OBJECT_FLAG_SET (basesink, GST_ELEMENT_IS_SINK);
634 gst_base_sink_finalize (GObject * object)
636 GstBaseSink *basesink;
638 basesink = GST_BASE_SINK (object);
640 g_queue_free (basesink->preroll_queue);
641 gst_segment_free (basesink->abidata.ABI.clip_segment);
643 G_OBJECT_CLASS (parent_class)->finalize (object);
647 * gst_base_sink_set_sync:
649 * @sync: the new sync value.
651 * Configures @sink to synchronize on the clock or not. When
652 * @sync is FALSE, incomming samples will be played as fast as
653 * possible. If @sync is TRUE, the timestamps of the incomming
654 * buffers will be used to schedule the exact render time of its
660 gst_base_sink_set_sync (GstBaseSink * sink, gboolean sync)
662 g_return_if_fail (GST_IS_BASE_SINK (sink));
664 GST_OBJECT_LOCK (sink);
666 GST_OBJECT_UNLOCK (sink);
670 * gst_base_sink_get_sync:
673 * Checks if @sink is currently configured to synchronize against the
676 * Returns: TRUE if the sink is configured to synchronize against the clock.
681 gst_base_sink_get_sync (GstBaseSink * sink)
685 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
687 GST_OBJECT_LOCK (sink);
689 GST_OBJECT_UNLOCK (sink);
695 * gst_base_sink_set_max_lateness:
697 * @max_lateness: the new max lateness value.
699 * Sets the new max lateness value to @max_lateness. This value is
700 * used to decide if a buffer should be dropped or not based on the
701 * buffer timestamp and the current clock time. A value of -1 means
707 gst_base_sink_set_max_lateness (GstBaseSink * sink, gint64 max_lateness)
709 g_return_if_fail (GST_IS_BASE_SINK (sink));
711 GST_OBJECT_LOCK (sink);
712 sink->abidata.ABI.max_lateness = max_lateness;
713 GST_OBJECT_UNLOCK (sink);
717 * gst_base_sink_get_max_lateness:
720 * Gets the max lateness value. See gst_base_sink_set_max_lateness for
723 * Returns: The maximum time in nanoseconds that a buffer can be late
724 * before it is dropped and not rendered. A value of -1 means an
730 gst_base_sink_get_max_lateness (GstBaseSink * sink)
734 g_return_val_if_fail (GST_IS_BASE_SINK (sink), -1);
736 GST_OBJECT_LOCK (sink);
737 res = sink->abidata.ABI.max_lateness;
738 GST_OBJECT_UNLOCK (sink);
744 * gst_base_sink_set_qos_enabled:
746 * @enabled: the new qos value.
748 * Configures @sink to send Quality-of-Service events upstream.
753 gst_base_sink_set_qos_enabled (GstBaseSink * sink, gboolean enabled)
755 g_return_if_fail (GST_IS_BASE_SINK (sink));
757 g_atomic_int_set (&sink->priv->qos_enabled, enabled);
761 * gst_base_sink_is_qos_enabled:
764 * Checks if @sink is currently configured to send Quality-of-Service events
767 * Returns: TRUE if the sink is configured to perform Quality-of-Service.
772 gst_base_sink_is_qos_enabled (GstBaseSink * sink)
776 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
778 res = g_atomic_int_get (&sink->priv->qos_enabled);
784 * gst_base_sink_set_async_enabled:
786 * @enabled: the new async value.
788 * Configures @sink to perform all state changes asynchronusly. When async is
789 * disabled, the sink will immediatly go to PAUSED instead of waiting for a
790 * preroll buffer. This feature is usefull if the sink does not synchronize
791 * against the clock or when it is dealing with sparse streams.
796 gst_base_sink_set_async_enabled (GstBaseSink * sink, gboolean enabled)
798 g_return_if_fail (GST_IS_BASE_SINK (sink));
800 GST_PAD_PREROLL_LOCK (sink->sinkpad);
801 sink->priv->async_enabled = enabled;
802 GST_LOG_OBJECT (sink, "set async enabled to %d", enabled);
803 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
807 * gst_base_sink_is_async_enabled:
810 * Checks if @sink is currently configured to perform asynchronous state
813 * Returns: TRUE if the sink is configured to perform asynchronous state
819 gst_base_sink_is_async_enabled (GstBaseSink * sink)
823 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
825 GST_PAD_PREROLL_LOCK (sink->sinkpad);
826 res = sink->priv->async_enabled;
827 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
833 * gst_base_sink_set_ts_offset:
835 * @offset: the new offset
837 * Adjust the synchronisation of @sink with @offset. A negative value will
838 * render buffers earlier than their timestamp. A positive value will delay
839 * rendering. This function can be used to fix playback of badly timestamped
845 gst_base_sink_set_ts_offset (GstBaseSink * sink, GstClockTimeDiff offset)
847 g_return_if_fail (GST_IS_BASE_SINK (sink));
849 GST_OBJECT_LOCK (sink);
850 sink->priv->ts_offset = offset;
851 GST_LOG_OBJECT (sink, "set time offset to %" G_GINT64_FORMAT, offset);
852 GST_OBJECT_UNLOCK (sink);
856 * gst_base_sink_get_ts_offset:
859 * Get the synchronisation offset of @sink.
861 * Returns: The synchronisation offset.
866 gst_base_sink_get_ts_offset (GstBaseSink * sink)
868 GstClockTimeDiff res;
870 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
872 GST_OBJECT_LOCK (sink);
873 res = sink->priv->ts_offset;
874 GST_OBJECT_UNLOCK (sink);
880 * gst_base_sink_get_last_buffer:
883 * Get the last buffer that arrived in the sink and was used for preroll or for
884 * rendering. This property can be used to generate thumbnails.
886 * The #GstCaps on the buffer can be used to determine the type of the buffer.
888 * Returns: a #GstBuffer. gst_buffer_unref() after usage. This function returns
889 * NULL when no buffer has arrived in the sink yet or when the sink is not in
895 gst_base_sink_get_last_buffer (GstBaseSink * sink)
899 g_return_val_if_fail (GST_IS_BASE_SINK (sink), NULL);
901 GST_OBJECT_LOCK (sink);
902 if ((res = sink->priv->last_buffer))
903 gst_buffer_ref (res);
904 GST_OBJECT_UNLOCK (sink);
910 gst_base_sink_set_last_buffer (GstBaseSink * sink, GstBuffer * buffer)
914 GST_OBJECT_LOCK (sink);
915 old = sink->priv->last_buffer;
916 if (G_LIKELY (old != buffer)) {
917 GST_DEBUG_OBJECT (sink, "setting last buffer to %p", buffer);
918 if (G_LIKELY (buffer))
919 gst_buffer_ref (buffer);
920 sink->priv->last_buffer = buffer;
924 GST_OBJECT_UNLOCK (sink);
926 /* avoid unreffing with the lock because cleanup code might want to take the
929 gst_buffer_unref (old);
933 * gst_base_sink_get_latency:
936 * Get the currently configured latency.
938 * Returns: The configured latency.
943 gst_base_sink_get_latency (GstBaseSink * sink)
947 GST_OBJECT_LOCK (sink);
948 res = sink->priv->latency;
949 GST_OBJECT_UNLOCK (sink);
955 * gst_base_sink_query_latency:
957 * @live: if the sink is live
958 * @upstream_live: if an upstream element is live
959 * @min_latency: the min latency of the upstream elements
960 * @max_latency: the max latency of the upstream elements
962 * Query the sink for the latency parameters. The latency will be queried from
963 * the upstream elements. @live will be TRUE if @sink is configured to
964 * synchronize against the clock. @upstream_live will be TRUE if an upstream
967 * If both @live and @upstream_live are TRUE, the sink will want to compensate
968 * for the latency introduced by the upstream elements by setting the
969 * @min_latency to a strictly possitive value.
971 * This function is mostly used by subclasses.
973 * Returns: TRUE if the query succeeded.
978 gst_base_sink_query_latency (GstBaseSink * sink, gboolean * live,
979 gboolean * upstream_live, GstClockTime * min_latency,
980 GstClockTime * max_latency)
982 gboolean l, us_live, res, have_latency;
983 GstClockTime min, max, render_delay;
985 GstClockTime us_min, us_max;
987 /* we are live when we sync to the clock */
988 GST_OBJECT_LOCK (sink);
990 have_latency = sink->priv->have_latency;
991 render_delay = sink->priv->render_delay;
992 GST_OBJECT_UNLOCK (sink);
994 /* assume no latency */
1000 GST_DEBUG_OBJECT (sink, "we are ready for LATENCY query");
1001 /* we are ready for a latency query this is when we preroll or when we are
1003 query = gst_query_new_latency ();
1005 /* ask the peer for the latency */
1006 if ((res = gst_base_sink_peer_query (sink, query))) {
1007 /* get upstream min and max latency */
1008 gst_query_parse_latency (query, &us_live, &us_min, &us_max);
1011 /* upstream live, use its latency, subclasses should use these
1012 * values to create the complete latency. */
1017 /* we need to add the render delay if we are live */
1019 min += render_delay;
1021 max += render_delay;
1024 gst_query_unref (query);
1026 GST_DEBUG_OBJECT (sink, "we are not yet ready for LATENCY query");
1030 /* not live, we tried to do the query, if it failed we return TRUE anyway */
1034 GST_DEBUG_OBJECT (sink, "latency query failed but we are not live");
1036 GST_DEBUG_OBJECT (sink, "latency query failed and we are live");
1041 GST_DEBUG_OBJECT (sink, "latency query: live: %d, have_latency %d,"
1042 " upstream: %d, min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT, l,
1043 have_latency, us_live, GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1048 *upstream_live = us_live;
1058 * gst_base_sink_set_render_delay:
1059 * @sink: a #GstBaseSink
1060 * @delay: the new delay
1062 * Set the render delay in @sink to @delay. The render delay is the time
1063 * between actual rendering of a buffer and its synchronisation time. Some
1064 * devices might delay media rendering which can be compensated for with this
1067 * After calling this function, this sink will report additional latency and
1068 * other sinks will adjust their latency to delay the rendering of their media.
1070 * This function is usually called by subclasses.
1075 gst_base_sink_set_render_delay (GstBaseSink * sink, GstClockTime delay)
1077 GstClockTime old_render_delay;
1079 g_return_if_fail (GST_IS_BASE_SINK (sink));
1081 GST_OBJECT_LOCK (sink);
1082 old_render_delay = sink->priv->render_delay;
1083 sink->priv->render_delay = delay;
1084 GST_LOG_OBJECT (sink, "set render delay to %" GST_TIME_FORMAT,
1085 GST_TIME_ARGS (delay));
1086 GST_OBJECT_UNLOCK (sink);
1088 if (delay != old_render_delay) {
1089 GST_DEBUG_OBJECT (sink, "posting latency changed");
1090 gst_element_post_message (GST_ELEMENT_CAST (sink),
1091 gst_message_new_latency (GST_OBJECT_CAST (sink)));
1096 * gst_base_sink_get_render_delay:
1097 * @sink: a #GstBaseSink
1099 * Get the render delay of @sink. see gst_base_sink_set_render_delay() for more
1100 * information about the render delay.
1102 * Returns: the render delay of @sink.
1107 gst_base_sink_get_render_delay (GstBaseSink * sink)
1109 GstClockTimeDiff res;
1111 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1113 GST_OBJECT_LOCK (sink);
1114 res = sink->priv->render_delay;
1115 GST_OBJECT_UNLOCK (sink);
1121 * gst_base_sink_set_blocksize:
1122 * @sink: a #GstBaseSink
1123 * @blocksize: the blocksize in bytes
1125 * Set the number of bytes that the sink will pull when it is operating in pull
1131 gst_base_sink_set_blocksize (GstBaseSink * sink, guint blocksize)
1133 g_return_if_fail (GST_IS_BASE_SINK (sink));
1135 GST_OBJECT_LOCK (sink);
1136 sink->priv->blocksize = blocksize;
1137 GST_LOG_OBJECT (sink, "set blocksize to %u", blocksize);
1138 GST_OBJECT_UNLOCK (sink);
1142 * gst_base_sink_get_blocksize:
1143 * @sink: a #GstBaseSink
1145 * Get the number of bytes that the sink will pull when it is operating in pull
1148 * Returns: the number of bytes @sink will pull in pull mode.
1153 gst_base_sink_get_blocksize (GstBaseSink * sink)
1157 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1159 GST_OBJECT_LOCK (sink);
1160 res = sink->priv->blocksize;
1161 GST_OBJECT_UNLOCK (sink);
1167 gst_base_sink_set_property (GObject * object, guint prop_id,
1168 const GValue * value, GParamSpec * pspec)
1170 GstBaseSink *sink = GST_BASE_SINK (object);
1173 case PROP_PREROLL_QUEUE_LEN:
1174 /* preroll lock necessary to serialize with finish_preroll */
1175 GST_PAD_PREROLL_LOCK (sink->sinkpad);
1176 sink->preroll_queue_max_len = g_value_get_uint (value);
1177 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
1180 gst_base_sink_set_sync (sink, g_value_get_boolean (value));
1182 case PROP_MAX_LATENESS:
1183 gst_base_sink_set_max_lateness (sink, g_value_get_int64 (value));
1186 gst_base_sink_set_qos_enabled (sink, g_value_get_boolean (value));
1189 gst_base_sink_set_async_enabled (sink, g_value_get_boolean (value));
1191 case PROP_TS_OFFSET:
1192 gst_base_sink_set_ts_offset (sink, g_value_get_int64 (value));
1194 case PROP_BLOCKSIZE:
1195 gst_base_sink_set_blocksize (sink, g_value_get_uint (value));
1197 case PROP_RENDER_DELAY:
1198 gst_base_sink_set_render_delay (sink, g_value_get_uint64 (value));
1201 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1207 gst_base_sink_get_property (GObject * object, guint prop_id, GValue * value,
1210 GstBaseSink *sink = GST_BASE_SINK (object);
1213 case PROP_PREROLL_QUEUE_LEN:
1214 GST_PAD_PREROLL_LOCK (sink->sinkpad);
1215 g_value_set_uint (value, sink->preroll_queue_max_len);
1216 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
1219 g_value_set_boolean (value, gst_base_sink_get_sync (sink));
1221 case PROP_MAX_LATENESS:
1222 g_value_set_int64 (value, gst_base_sink_get_max_lateness (sink));
1225 g_value_set_boolean (value, gst_base_sink_is_qos_enabled (sink));
1228 g_value_set_boolean (value, gst_base_sink_is_async_enabled (sink));
1230 case PROP_TS_OFFSET:
1231 g_value_set_int64 (value, gst_base_sink_get_ts_offset (sink));
1233 case PROP_LAST_BUFFER:
1234 gst_value_take_buffer (value, gst_base_sink_get_last_buffer (sink));
1236 case PROP_BLOCKSIZE:
1237 g_value_set_uint (value, gst_base_sink_get_blocksize (sink));
1239 case PROP_RENDER_DELAY:
1240 g_value_set_uint64 (value, gst_base_sink_get_render_delay (sink));
1243 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1250 gst_base_sink_get_caps (GstBaseSink * sink)
1256 gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps)
1261 static GstFlowReturn
1262 gst_base_sink_buffer_alloc (GstBaseSink * sink, guint64 offset, guint size,
1263 GstCaps * caps, GstBuffer ** buf)
1269 /* with PREROLL_LOCK, STREAM_LOCK */
1271 gst_base_sink_preroll_queue_flush (GstBaseSink * basesink, GstPad * pad)
1275 GST_DEBUG_OBJECT (basesink, "flushing queue %p", basesink);
1276 while ((obj = g_queue_pop_head (basesink->preroll_queue))) {
1277 GST_DEBUG_OBJECT (basesink, "popped %p", obj);
1278 gst_mini_object_unref (obj);
1280 /* we can't have EOS anymore now */
1281 basesink->eos = FALSE;
1282 basesink->priv->received_eos = FALSE;
1283 basesink->have_preroll = FALSE;
1284 basesink->priv->step_unlock = FALSE;
1285 basesink->eos_queued = FALSE;
1286 basesink->preroll_queued = 0;
1287 basesink->buffers_queued = 0;
1288 basesink->events_queued = 0;
1289 /* can't report latency anymore until we preroll again */
1290 if (basesink->priv->async_enabled) {
1291 GST_OBJECT_LOCK (basesink);
1292 basesink->priv->have_latency = FALSE;
1293 GST_OBJECT_UNLOCK (basesink);
1295 /* and signal any waiters now */
1296 GST_PAD_PREROLL_SIGNAL (pad);
1299 /* with STREAM_LOCK, configures given segment with the event information. */
1301 gst_base_sink_configure_segment (GstBaseSink * basesink, GstPad * pad,
1302 GstEvent * event, GstSegment * segment)
1305 gdouble rate, arate;
1311 /* the newsegment event is needed to bring the buffer timestamps to the
1312 * stream time and to drop samples outside of the playback segment. */
1313 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1314 &start, &stop, &time);
1316 /* The segment is protected with both the STREAM_LOCK and the OBJECT_LOCK.
1317 * We protect with the OBJECT_LOCK so that we can use the values to
1318 * safely answer a POSITION query. */
1319 GST_OBJECT_LOCK (basesink);
1320 gst_segment_set_newsegment_full (segment, update, rate, arate, format, start,
1323 if (format == GST_FORMAT_TIME) {
1324 GST_DEBUG_OBJECT (basesink,
1325 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1326 "format GST_FORMAT_TIME, "
1327 "%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
1328 ", time %" GST_TIME_FORMAT ", accum %" GST_TIME_FORMAT,
1329 update, rate, arate, GST_TIME_ARGS (segment->start),
1330 GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time),
1331 GST_TIME_ARGS (segment->accum));
1333 GST_DEBUG_OBJECT (basesink,
1334 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1336 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
1337 G_GINT64_FORMAT ", accum %" G_GINT64_FORMAT, update, rate, arate,
1338 segment->format, segment->start, segment->stop, segment->time,
1341 GST_OBJECT_UNLOCK (basesink);
1344 /* with PREROLL_LOCK, STREAM_LOCK */
1346 gst_base_sink_commit_state (GstBaseSink * basesink)
1348 /* commit state and proceed to next pending state */
1349 GstState current, next, pending, post_pending;
1350 gboolean post_paused = FALSE;
1351 gboolean post_async_done = FALSE;
1352 gboolean post_playing = FALSE;
1354 /* we are certainly not playing async anymore now */
1355 basesink->playing_async = FALSE;
1357 GST_OBJECT_LOCK (basesink);
1358 current = GST_STATE (basesink);
1359 next = GST_STATE_NEXT (basesink);
1360 pending = GST_STATE_PENDING (basesink);
1361 post_pending = pending;
1364 case GST_STATE_PLAYING:
1366 GstBaseSinkClass *bclass;
1367 GstStateChangeReturn ret;
1369 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1371 GST_DEBUG_OBJECT (basesink, "commiting state to PLAYING");
1373 basesink->need_preroll = FALSE;
1374 post_async_done = TRUE;
1375 basesink->priv->commited = TRUE;
1376 post_playing = TRUE;
1377 /* post PAUSED too when we were READY */
1378 if (current == GST_STATE_READY) {
1382 /* make sure we notify the subclass of async playing */
1383 if (bclass->async_play) {
1384 GST_WARNING_OBJECT (basesink, "deprecated async_play");
1385 ret = bclass->async_play (basesink);
1386 if (ret == GST_STATE_CHANGE_FAILURE)
1391 case GST_STATE_PAUSED:
1392 GST_DEBUG_OBJECT (basesink, "commiting state to PAUSED");
1394 post_async_done = TRUE;
1395 basesink->priv->commited = TRUE;
1396 post_pending = GST_STATE_VOID_PENDING;
1398 case GST_STATE_READY:
1399 case GST_STATE_NULL:
1401 case GST_STATE_VOID_PENDING:
1402 goto nothing_pending;
1407 /* we can report latency queries now */
1408 basesink->priv->have_latency = TRUE;
1410 GST_STATE (basesink) = pending;
1411 GST_STATE_NEXT (basesink) = GST_STATE_VOID_PENDING;
1412 GST_STATE_PENDING (basesink) = GST_STATE_VOID_PENDING;
1413 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_SUCCESS;
1414 GST_OBJECT_UNLOCK (basesink);
1417 GST_DEBUG_OBJECT (basesink, "posting PAUSED state change message");
1418 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1419 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1420 current, next, post_pending));
1422 if (post_async_done) {
1423 GST_DEBUG_OBJECT (basesink, "posting async-done message");
1424 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1425 gst_message_new_async_done (GST_OBJECT_CAST (basesink)));
1428 GST_DEBUG_OBJECT (basesink, "posting PLAYING state change message");
1429 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1430 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1431 next, pending, GST_STATE_VOID_PENDING));
1434 GST_STATE_BROADCAST (basesink);
1440 /* Depending on the state, set our vars. We get in this situation when the
1441 * state change function got a change to update the state vars before the
1442 * streaming thread did. This is fine but we need to make sure that we
1443 * update the need_preroll var since it was TRUE when we got here and might
1444 * become FALSE if we got to PLAYING. */
1445 GST_DEBUG_OBJECT (basesink, "nothing to commit, now in %s",
1446 gst_element_state_get_name (current));
1448 case GST_STATE_PLAYING:
1449 basesink->need_preroll = FALSE;
1451 case GST_STATE_PAUSED:
1452 basesink->need_preroll = TRUE;
1455 basesink->need_preroll = FALSE;
1456 basesink->flushing = TRUE;
1459 /* we can report latency queries now */
1460 basesink->priv->have_latency = TRUE;
1461 GST_OBJECT_UNLOCK (basesink);
1466 /* app is going to READY */
1467 GST_DEBUG_OBJECT (basesink, "stopping");
1468 basesink->need_preroll = FALSE;
1469 basesink->flushing = TRUE;
1470 GST_OBJECT_UNLOCK (basesink);
1475 GST_DEBUG_OBJECT (basesink, "async commit failed");
1476 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_FAILURE;
1477 GST_OBJECT_UNLOCK (basesink);
1483 start_stepping (GstBaseSink * sink, GstSegment * segment,
1484 GstStepInfo * pending, GstStepInfo * current)
1488 GST_DEBUG_OBJECT (sink, "update pending step");
1490 GST_OBJECT_LOCK (sink);
1491 memcpy (current, pending, sizeof (GstStepInfo));
1492 pending->valid = FALSE;
1493 GST_OBJECT_UNLOCK (sink);
1495 /* get the running time of where we paused and remember it */
1496 current->start = gst_element_get_start_time (GST_ELEMENT_CAST (sink));
1497 gst_segment_set_running_time (segment, GST_FORMAT_TIME, current->start);
1499 /* set the new rate for the remainder of the segment */
1500 current->start_rate = segment->rate;
1501 segment->rate *= current->rate;
1502 segment->abs_rate = ABS (segment->rate);
1504 if (segment->format == GST_FORMAT_TIME) {
1505 current->start_stop = segment->stop;
1506 end = current->start + current->amount;
1507 segment->stop = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1510 GST_DEBUG_OBJECT (sink, "segment now %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT,
1511 GST_TIME_ARGS (segment->start), GST_TIME_ARGS (segment->stop));
1513 GST_DEBUG_OBJECT (sink, "step started at running_time %" GST_TIME_FORMAT,
1514 GST_TIME_ARGS (current->start));
1516 if (current->amount == -1) {
1517 GST_DEBUG_OBJECT (sink, "step amount == -1, stop stepping");
1518 current->valid = FALSE;
1520 GST_DEBUG_OBJECT (sink, "step amount: %" G_GUINT64_FORMAT ", format: %s, "
1521 "rate: %f", current->amount, gst_format_get_name (current->format),
1527 stop_stepping (GstBaseSink * sink, GstSegment * segment,
1528 GstStepInfo * current, gint64 rstart, gint64 rstop)
1530 gint64 stop, position;
1531 GstMessage *message;
1533 GST_DEBUG_OBJECT (sink, "step complete");
1535 if (segment->rate > 0.0)
1540 GST_DEBUG_OBJECT (sink,
1541 "step stop at running_time %" GST_TIME_FORMAT, GST_TIME_ARGS (stop));
1544 current->duration = current->position;
1546 current->duration = stop - current->start;
1548 GST_DEBUG_OBJECT (sink, "step elapsed running_time %" GST_TIME_FORMAT,
1549 GST_TIME_ARGS (current->duration));
1551 position = current->start + current->duration;
1552 gst_element_set_start_time (GST_ELEMENT_CAST (sink), position);
1554 /* now move the segment to the new running time */
1555 gst_segment_set_running_time (segment, GST_FORMAT_TIME, position);
1557 if (current->flush) {
1558 /* and remove the accumulated time we flushed */
1559 segment->accum = current->start;
1562 /* restore the previous rate */
1563 segment->rate = current->start_rate;
1564 segment->abs_rate = ABS (segment->rate);
1565 segment->stop = current->start_stop;
1567 /* the clip segment is used for position report in paused... */
1568 memcpy (sink->abidata.ABI.clip_segment, segment, sizeof (GstSegment));
1570 /* post the step done when we know the stepped duration in TIME */
1572 gst_message_new_step_done (GST_OBJECT_CAST (sink), current->format,
1573 current->amount, current->rate, current->flush, current->intermediate,
1575 gst_message_set_seqnum (message, current->seqnum);
1576 gst_element_post_message (GST_ELEMENT_CAST (sink), message);
1578 if (!current->intermediate)
1579 sink->need_preroll = current->need_preroll;
1581 /* and the current step info finished and becomes invalid */
1582 current->valid = FALSE;
1586 handle_stepping (GstBaseSink * sink, GstSegment * segment,
1587 GstStepInfo * current, gint64 * cstart, gint64 * cstop, gint64 * rstart,
1590 GstBaseSinkPrivate *priv;
1591 gboolean step_end = FALSE;
1595 /* see if we need to skip this buffer because of stepping */
1596 switch (current->format) {
1597 case GST_FORMAT_TIME:
1602 if (segment->rate > 0.0) {
1610 end = current->start + current->amount;
1611 current->position = first - current->start;
1613 if (G_UNLIKELY (segment->abs_rate != 1.0))
1614 current->position /= segment->abs_rate;
1616 GST_DEBUG_OBJECT (sink,
1617 "buffer: %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT,
1618 GST_TIME_ARGS (first), GST_TIME_ARGS (last));
1619 GST_DEBUG_OBJECT (sink,
1620 "got time step %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT "/%"
1621 GST_TIME_FORMAT, GST_TIME_ARGS (current->position),
1622 GST_TIME_ARGS (last - current->start),
1623 GST_TIME_ARGS (current->amount));
1625 if ((current->flush && current->position >= current->amount)
1627 GST_DEBUG_OBJECT (sink, "step ended, we need clipping");
1629 if (segment->rate > 0.0) {
1631 *cstart = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1634 *cstop = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1637 GST_DEBUG_OBJECT (sink,
1638 "cstart %" GST_TIME_FORMAT ", rstart %" GST_TIME_FORMAT,
1639 GST_TIME_ARGS (*cstart), GST_TIME_ARGS (*rstart));
1640 GST_DEBUG_OBJECT (sink,
1641 "cstop %" GST_TIME_FORMAT ", rstop %" GST_TIME_FORMAT,
1642 GST_TIME_ARGS (*cstop), GST_TIME_ARGS (*rstop));
1645 case GST_FORMAT_BUFFERS:
1646 GST_DEBUG_OBJECT (sink,
1647 "got default step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1648 current->position, current->amount);
1650 if (current->position < current->amount) {
1651 current->position++;
1656 case GST_FORMAT_DEFAULT:
1658 GST_DEBUG_OBJECT (sink,
1659 "got unknown step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1660 current->position, current->amount);
1666 /* with STREAM_LOCK, PREROLL_LOCK
1668 * Returns TRUE if the object needs synchronisation and takes therefore
1669 * part in prerolling.
1671 * rsstart/rsstop contain the start/stop in stream time.
1672 * rrstart/rrstop contain the start/stop in running time.
1675 gst_base_sink_get_sync_times (GstBaseSink * basesink, GstMiniObject * obj,
1676 GstClockTime * rsstart, GstClockTime * rsstop,
1677 GstClockTime * rrstart, GstClockTime * rrstop, gboolean * do_sync,
1678 gboolean * stepped, GstSegment * segment, GstStepInfo * step,
1679 gboolean * step_end)
1681 GstBaseSinkClass *bclass;
1683 GstClockTime start, stop; /* raw start/stop timestamps */
1684 gint64 cstart, cstop; /* clipped raw timestamps */
1685 gint64 rstart, rstop; /* clipped timestamps converted to running time */
1686 GstClockTime sstart, sstop; /* clipped timestamps converted to stream time */
1688 GstBaseSinkPrivate *priv;
1690 priv = basesink->priv;
1692 /* start with nothing */
1695 if (G_UNLIKELY (GST_IS_EVENT (obj))) {
1696 GstEvent *event = GST_EVENT_CAST (obj);
1698 switch (GST_EVENT_TYPE (event)) {
1699 /* EOS event needs syncing */
1702 if (basesink->segment.rate >= 0.0) {
1703 sstart = sstop = priv->current_sstop;
1705 /* we have not seen a buffer yet, use the segment values */
1706 sstart = sstop = gst_segment_to_stream_time (&basesink->segment,
1707 basesink->segment.format, basesink->segment.stop);
1710 sstart = sstop = priv->current_sstart;
1712 /* we have not seen a buffer yet, use the segment values */
1713 sstart = sstop = gst_segment_to_stream_time (&basesink->segment,
1714 basesink->segment.format, basesink->segment.start);
1718 rstart = rstop = priv->eos_rtime;
1719 *do_sync = rstart != -1;
1720 GST_DEBUG_OBJECT (basesink, "sync times for EOS %" GST_TIME_FORMAT,
1721 GST_TIME_ARGS (rstart));
1722 /* if we are stepping, we end now */
1723 *step_end = step->valid;
1727 /* other events do not need syncing */
1728 /* FIXME, maybe NEWSEGMENT might need synchronisation
1729 * since the POSITION query depends on accumulated times and
1730 * we cannot accumulate the current segment before the previous
1737 /* else do buffer sync code */
1738 buffer = GST_BUFFER_CAST (obj);
1740 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1742 /* just get the times to see if we need syncing, if the start returns -1 we
1744 if (bclass->get_times)
1745 bclass->get_times (basesink, buffer, &start, &stop);
1748 /* we don't need to sync but we still want to get the timestamps for
1749 * tracking the position */
1750 gst_base_sink_get_times (basesink, buffer, &start, &stop);
1756 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
1757 ", stop: %" GST_TIME_FORMAT ", do_sync %d", GST_TIME_ARGS (start),
1758 GST_TIME_ARGS (stop), *do_sync);
1760 /* collect segment and format for code clarity */
1761 format = segment->format;
1763 /* no timestamp clipping if we did not get a TIME segment format */
1764 if (G_UNLIKELY (format != GST_FORMAT_TIME)) {
1767 /* do running and stream time in TIME format */
1768 format = GST_FORMAT_TIME;
1769 GST_LOG_OBJECT (basesink, "not time format, don't clip");
1773 /* clip, only when we know about time */
1774 if (G_UNLIKELY (!gst_segment_clip (segment, GST_FORMAT_TIME,
1775 (gint64) start, (gint64) stop, &cstart, &cstop)))
1776 goto out_of_segment;
1778 if (G_UNLIKELY (start != cstart || stop != cstop)) {
1779 GST_DEBUG_OBJECT (basesink, "clipped to: start %" GST_TIME_FORMAT
1780 ", stop: %" GST_TIME_FORMAT, GST_TIME_ARGS (cstart),
1781 GST_TIME_ARGS (cstop));
1784 /* set last stop position */
1785 if (G_LIKELY (cstop != GST_CLOCK_TIME_NONE))
1786 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, cstop);
1788 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, cstart);
1791 rstart = gst_segment_to_running_time (segment, format, cstart);
1792 rstop = gst_segment_to_running_time (segment, format, cstop);
1794 if (G_UNLIKELY (step->valid)) {
1795 if (!(*step_end = handle_stepping (basesink, segment, step, &cstart, &cstop,
1796 &rstart, &rstop))) {
1797 /* step is still busy, we discard data when we are flushing */
1798 *stepped = step->flush;
1801 /* this can produce wrong values if we accumulated non-TIME segments. If this happens,
1802 * upstream is behaving very badly */
1803 sstart = gst_segment_to_stream_time (segment, format, cstart);
1804 sstop = gst_segment_to_stream_time (segment, format, cstop);
1807 /* done label only called when doing EOS, we also stop stepping then */
1808 if (*step_end && step->flush) {
1809 stop_stepping (basesink, segment, step, rstart, rstop);
1819 /* buffers and EOS always need syncing and preroll */
1825 /* we usually clip in the chain function already but stepping could cause
1826 * the segment to be updated later. we return FALSE so that we don't try
1828 GST_LOG_OBJECT (basesink, "buffer skipped, not in segment");
1833 /* with STREAM_LOCK, PREROLL_LOCK, LOCK
1834 * adjust a timestamp with the latency and timestamp offset */
1836 gst_base_sink_adjust_time (GstBaseSink * basesink, GstClockTime time)
1838 GstClockTimeDiff ts_offset;
1840 /* don't do anything funny with invalid timestamps */
1841 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
1844 time += basesink->priv->latency;
1846 /* apply offset, be carefull for underflows */
1847 ts_offset = basesink->priv->ts_offset;
1848 if (ts_offset < 0) {
1849 ts_offset = -ts_offset;
1850 if (ts_offset < time)
1861 * gst_base_sink_wait_clock:
1863 * @time: the running_time to be reached
1864 * @jitter: the jitter to be filled with time diff (can be NULL)
1866 * This function will block until @time is reached. It is usually called by
1867 * subclasses that use their own internal synchronisation.
1869 * If @time is not valid, no sycnhronisation is done and #GST_CLOCK_BADTIME is
1870 * returned. Likewise, if synchronisation is disabled in the element or there
1871 * is no clock, no synchronisation is done and #GST_CLOCK_BADTIME is returned.
1873 * This function should only be called with the PREROLL_LOCK held, like when
1874 * receiving an EOS event in the ::event vmethod or when receiving a buffer in
1875 * the ::render vmethod.
1877 * The @time argument should be the running_time of when this method should
1878 * return and is not adjusted with any latency or offset configured in the
1883 * Returns: #GstClockReturn
1886 gst_base_sink_wait_clock (GstBaseSink * sink, GstClockTime time,
1887 GstClockTimeDiff * jitter)
1893 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
1896 GST_OBJECT_LOCK (sink);
1897 if (G_UNLIKELY (!sink->sync))
1900 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (sink)) == NULL))
1903 /* add base_time to running_time to get the time against the clock */
1904 time += GST_ELEMENT_CAST (sink)->base_time;
1906 id = gst_clock_new_single_shot_id (clock, time);
1907 GST_OBJECT_UNLOCK (sink);
1909 /* A blocking wait is performed on the clock. We save the ClockID
1910 * so we can unlock the entry at any time. While we are blocking, we
1911 * release the PREROLL_LOCK so that other threads can interrupt the
1913 sink->clock_id = id;
1914 /* release the preroll lock while waiting */
1915 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
1917 ret = gst_clock_id_wait (id, jitter);
1919 GST_PAD_PREROLL_LOCK (sink->sinkpad);
1920 gst_clock_id_unref (id);
1921 sink->clock_id = NULL;
1925 /* no syncing needed */
1928 GST_DEBUG_OBJECT (sink, "time not valid, no sync needed");
1929 return GST_CLOCK_BADTIME;
1933 GST_DEBUG_OBJECT (sink, "sync disabled");
1934 GST_OBJECT_UNLOCK (sink);
1935 return GST_CLOCK_BADTIME;
1939 GST_DEBUG_OBJECT (sink, "no clock, can't sync");
1940 GST_OBJECT_UNLOCK (sink);
1941 return GST_CLOCK_BADTIME;
1946 * gst_base_sink_wait_preroll:
1949 * If the #GstBaseSinkClass::render method performs its own synchronisation against
1950 * the clock it must unblock when going from PLAYING to the PAUSED state and call
1951 * this method before continuing to render the remaining data.
1953 * This function will block until a state change to PLAYING happens (in which
1954 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
1955 * to a state change to READY or a FLUSH event (in which case this function
1956 * returns #GST_FLOW_WRONG_STATE).
1958 * This function should only be called with the PREROLL_LOCK held, like in the
1963 * Returns: #GST_FLOW_OK if the preroll completed and processing can
1964 * continue. Any other return value should be returned from the render vmethod.
1967 gst_base_sink_wait_preroll (GstBaseSink * sink)
1969 sink->have_preroll = TRUE;
1970 GST_DEBUG_OBJECT (sink, "waiting in preroll for flush or PLAYING");
1971 /* block until the state changes, or we get a flush, or something */
1972 GST_PAD_PREROLL_WAIT (sink->sinkpad);
1973 sink->have_preroll = FALSE;
1974 if (G_UNLIKELY (sink->flushing))
1976 if (G_UNLIKELY (sink->priv->step_unlock))
1978 GST_DEBUG_OBJECT (sink, "continue after preroll");
1985 GST_DEBUG_OBJECT (sink, "preroll interrupted because of flush");
1986 return GST_FLOW_WRONG_STATE;
1990 sink->priv->step_unlock = FALSE;
1991 GST_DEBUG_OBJECT (sink, "preroll interrupted because of step");
1992 return GST_FLOW_STEP;
1997 * gst_base_sink_do_preroll:
1999 * @obj: the object that caused the preroll
2001 * If the @sink spawns its own thread for pulling buffers from upstream it
2002 * should call this method after it has pulled a buffer. If the element needed
2003 * to preroll, this function will perform the preroll and will then block
2004 * until the element state is changed.
2006 * This function should be called with the PREROLL_LOCK held.
2010 * Returns: #GST_FLOW_OK if the preroll completed and processing can
2011 * continue. Any other return value should be returned from the render vmethod.
2014 gst_base_sink_do_preroll (GstBaseSink * sink, GstMiniObject * obj)
2018 while (G_UNLIKELY (sink->need_preroll)) {
2019 GST_DEBUG_OBJECT (sink, "prerolling object %p", obj);
2021 ret = gst_base_sink_preroll_object (sink, obj);
2022 if (ret != GST_FLOW_OK)
2023 goto preroll_failed;
2025 /* need to recheck here because the commit state could have
2026 * made us not need the preroll anymore */
2027 if (G_LIKELY (sink->need_preroll)) {
2028 /* block until the state changes, or we get a flush, or something */
2029 ret = gst_base_sink_wait_preroll (sink);
2030 if (ret != GST_FLOW_OK) {
2031 if (ret == GST_FLOW_STEP)
2034 goto preroll_failed;
2043 GST_DEBUG_OBJECT (sink, "preroll failed %d", ret);
2049 * gst_base_sink_wait_eos:
2051 * @time: the running_time to be reached
2052 * @jitter: the jitter to be filled with time diff (can be NULL)
2054 * This function will block until @time is reached. It is usually called by
2055 * subclasses that use their own internal synchronisation but want to let the
2056 * EOS be handled by the base class.
2058 * This function should only be called with the PREROLL_LOCK held, like when
2059 * receiving an EOS event in the ::event vmethod.
2061 * The @time argument should be the running_time of when the EOS should happen
2062 * and will be adjusted with any latency and offset configured in the sink.
2066 * Returns: #GstFlowReturn
2069 gst_base_sink_wait_eos (GstBaseSink * sink, GstClockTime time,
2070 GstClockTimeDiff * jitter)
2072 GstClockReturn status;
2078 GST_DEBUG_OBJECT (sink, "checking preroll");
2080 /* first wait for the playing state before we can continue */
2081 if (G_UNLIKELY (sink->need_preroll)) {
2082 ret = gst_base_sink_wait_preroll (sink);
2083 if (ret != GST_FLOW_OK) {
2084 if (ret == GST_FLOW_STEP)
2091 /* preroll done, we can sync since we are in PLAYING now. */
2092 GST_DEBUG_OBJECT (sink, "possibly waiting for clock to reach %"
2093 GST_TIME_FORMAT, GST_TIME_ARGS (time));
2095 /* compensate for latency and ts_offset. We don't adjust for render delay
2096 * because we don't interact with the device on EOS normally. */
2097 stime = gst_base_sink_adjust_time (sink, time);
2099 /* wait for the clock, this can be interrupted because we got shut down or
2101 status = gst_base_sink_wait_clock (sink, stime, jitter);
2103 GST_DEBUG_OBJECT (sink, "clock returned %d", status);
2105 /* invalid time, no clock or sync disabled, just continue then */
2106 if (status == GST_CLOCK_BADTIME)
2109 /* waiting could have been interrupted and we can be flushing now */
2110 if (G_UNLIKELY (sink->flushing))
2113 /* retry if we got unscheduled, which means we did not reach the timeout
2114 * yet. if some other error occures, we continue. */
2115 } while (status == GST_CLOCK_UNSCHEDULED);
2117 GST_DEBUG_OBJECT (sink, "end of stream");
2124 GST_DEBUG_OBJECT (sink, "we are flushing");
2125 return GST_FLOW_WRONG_STATE;
2129 /* with STREAM_LOCK, PREROLL_LOCK
2131 * Make sure we are in PLAYING and synchronize an object to the clock.
2133 * If we need preroll, we are not in PLAYING. We try to commit the state
2134 * if needed and then block if we still are not PLAYING.
2136 * We start waiting on the clock in PLAYING. If we got interrupted, we
2137 * immediatly try to re-preroll.
2139 * Some objects do not need synchronisation (most events) and so this function
2140 * immediatly returns GST_FLOW_OK.
2142 * for objects that arrive later than max-lateness to be synchronized to the
2143 * clock have the @late boolean set to TRUE.
2145 * This function keeps a running average of the jitter (the diff between the
2146 * clock time and the requested sync time). The jitter is negative for
2147 * objects that arrive in time and positive for late buffers.
2149 * does not take ownership of obj.
2151 static GstFlowReturn
2152 gst_base_sink_do_sync (GstBaseSink * basesink, GstPad * pad,
2153 GstMiniObject * obj, gboolean * late, gboolean * step_end)
2155 GstClockTimeDiff jitter;
2157 GstClockReturn status = GST_CLOCK_OK;
2158 GstClockTime rstart, rstop, sstart, sstop, stime;
2160 GstBaseSinkPrivate *priv;
2162 GstStepInfo *current, *pending;
2165 priv = basesink->priv;
2168 sstart = sstop = rstart = rstop = -1;
2172 priv->current_rstart = -1;
2174 /* get stepping info */
2175 current = &priv->current_step;
2176 pending = &priv->pending_step;
2178 /* get timing information for this object against the render segment */
2179 syncable = gst_base_sink_get_sync_times (basesink, obj,
2180 &sstart, &sstop, &rstart, &rstop, &do_sync, &stepped, &basesink->segment,
2183 if (G_UNLIKELY (stepped))
2186 /* a syncable object needs to participate in preroll and
2187 * clocking. All buffers and EOS are syncable. */
2188 if (G_UNLIKELY (!syncable))
2191 /* store timing info for current object */
2192 priv->current_rstart = rstart;
2193 priv->current_rstop = (rstop != -1 ? rstop : rstart);
2195 /* save sync time for eos when the previous object needed sync */
2196 priv->eos_rtime = (do_sync ? priv->current_rstop : -1);
2199 /* first do preroll, this makes sure we commit our state
2200 * to PAUSED and can continue to PLAYING. We cannot perform
2201 * any clock sync in PAUSED because there is no clock. */
2202 ret = gst_base_sink_do_preroll (basesink, obj);
2203 if (G_UNLIKELY (ret != GST_FLOW_OK))
2204 goto preroll_failed;
2206 /* After rendering we store the position of the last buffer so that we can use
2207 * it to report the position. We need to take the lock here. */
2208 GST_OBJECT_LOCK (basesink);
2209 priv->current_sstart = sstart;
2210 priv->current_sstop = (sstop != -1 ? sstop : sstart);
2211 GST_OBJECT_UNLOCK (basesink);
2213 /* update the segment with a pending step if the current one is invalid and we
2214 * have a new pending one. We only accept new step updates after a preroll */
2215 if (G_UNLIKELY (pending->valid && !current->valid)) {
2216 start_stepping (basesink, &basesink->segment, pending, current);
2223 /* adjust for latency */
2224 stime = gst_base_sink_adjust_time (basesink, rstart);
2226 /* adjust for render-delay, avoid underflows */
2228 if (stime > priv->render_delay)
2229 stime -= priv->render_delay;
2234 /* preroll done, we can sync since we are in PLAYING now. */
2235 GST_DEBUG_OBJECT (basesink, "possibly waiting for clock to reach %"
2236 GST_TIME_FORMAT ", adjusted %" GST_TIME_FORMAT,
2237 GST_TIME_ARGS (rstart), GST_TIME_ARGS (stime));
2239 /* This function will return immediatly if start == -1, no clock
2240 * or sync is disabled with GST_CLOCK_BADTIME. */
2241 status = gst_base_sink_wait_clock (basesink, stime, &jitter);
2243 GST_DEBUG_OBJECT (basesink, "clock returned %d", status);
2245 /* invalid time, no clock or sync disabled, just render */
2246 if (status == GST_CLOCK_BADTIME)
2249 /* waiting could have been interrupted and we can be flushing now */
2250 if (G_UNLIKELY (basesink->flushing))
2253 /* check for unlocked by a state change, we are not flushing so
2254 * we can try to preroll on the current buffer. */
2255 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
2256 GST_DEBUG_OBJECT (basesink, "unscheduled, waiting some more");
2257 priv->call_preroll = TRUE;
2261 /* successful syncing done, record observation */
2262 priv->current_jitter = jitter;
2264 /* check if the object should be dropped */
2265 *late = gst_base_sink_is_too_late (basesink, obj, rstart, rstop,
2274 GST_DEBUG_OBJECT (basesink, "skipped stepped object %p", obj);
2280 GST_DEBUG_OBJECT (basesink, "non syncable object %p", obj);
2285 GST_DEBUG_OBJECT (basesink, "we are flushing");
2286 return GST_FLOW_WRONG_STATE;
2290 GST_DEBUG_OBJECT (basesink, "preroll failed");
2296 gst_base_sink_send_qos (GstBaseSink * basesink,
2297 gdouble proportion, GstClockTime time, GstClockTimeDiff diff)
2302 /* generate Quality-of-Service event */
2303 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2304 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2305 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (time));
2307 event = gst_event_new_qos (proportion, diff, time);
2310 res = gst_pad_push_event (basesink->sinkpad, event);
2316 gst_base_sink_perform_qos (GstBaseSink * sink, gboolean dropped)
2318 GstBaseSinkPrivate *priv;
2319 GstClockTime start, stop;
2320 GstClockTimeDiff jitter;
2321 GstClockTime pt, entered, left;
2322 GstClockTime duration;
2327 start = priv->current_rstart;
2329 /* if Quality-of-Service disabled, do nothing */
2330 if (!g_atomic_int_get (&priv->qos_enabled) || start == -1)
2333 stop = priv->current_rstop;
2334 jitter = priv->current_jitter;
2337 /* this is the time the buffer entered the sink */
2338 if (start < -jitter)
2341 entered = start + jitter;
2344 /* this is the time the buffer entered the sink */
2345 entered = start + jitter;
2346 /* this is the time the buffer left the sink */
2347 left = start + jitter;
2350 /* calculate duration of the buffer */
2352 duration = stop - start;
2356 /* if we have the time when the last buffer left us, calculate
2357 * processing time */
2358 if (priv->last_left != -1) {
2359 if (entered > priv->last_left) {
2360 pt = entered - priv->last_left;
2368 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "start: %" GST_TIME_FORMAT
2369 ", entered %" GST_TIME_FORMAT ", left %" GST_TIME_FORMAT ", pt: %"
2370 GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT ",jitter %"
2371 G_GINT64_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (entered),
2372 GST_TIME_ARGS (left), GST_TIME_ARGS (pt), GST_TIME_ARGS (duration),
2375 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "avg_duration: %" GST_TIME_FORMAT
2376 ", avg_pt: %" GST_TIME_FORMAT ", avg_rate: %g",
2377 GST_TIME_ARGS (priv->avg_duration), GST_TIME_ARGS (priv->avg_pt),
2380 /* collect running averages. for first observations, we copy the
2382 if (priv->avg_duration == -1)
2383 priv->avg_duration = duration;
2385 priv->avg_duration = UPDATE_RUNNING_AVG (priv->avg_duration, duration);
2387 if (priv->avg_pt == -1)
2390 priv->avg_pt = UPDATE_RUNNING_AVG (priv->avg_pt, pt);
2392 if (priv->avg_duration != 0)
2394 gst_guint64_to_gdouble (priv->avg_pt) /
2395 gst_guint64_to_gdouble (priv->avg_duration);
2399 if (priv->last_left != -1) {
2400 if (dropped || priv->avg_rate < 0.0) {
2401 priv->avg_rate = rate;
2404 priv->avg_rate = UPDATE_RUNNING_AVG_N (priv->avg_rate, rate);
2406 priv->avg_rate = UPDATE_RUNNING_AVG_P (priv->avg_rate, rate);
2410 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink,
2411 "updated: avg_duration: %" GST_TIME_FORMAT ", avg_pt: %" GST_TIME_FORMAT
2412 ", avg_rate: %g", GST_TIME_ARGS (priv->avg_duration),
2413 GST_TIME_ARGS (priv->avg_pt), priv->avg_rate);
2416 if (priv->avg_rate >= 0.0) {
2417 /* if we have a valid rate, start sending QoS messages */
2418 if (priv->current_jitter < 0) {
2419 /* make sure we never go below 0 when adding the jitter to the
2421 if (priv->current_rstart < -priv->current_jitter)
2422 priv->current_jitter = -priv->current_rstart;
2424 gst_base_sink_send_qos (sink, priv->avg_rate, priv->current_rstart,
2425 priv->current_jitter);
2428 /* record when this buffer will leave us */
2429 priv->last_left = left;
2432 /* reset all qos measuring */
2434 gst_base_sink_reset_qos (GstBaseSink * sink)
2436 GstBaseSinkPrivate *priv;
2440 priv->last_in_time = -1;
2441 priv->last_left = -1;
2442 priv->avg_duration = -1;
2444 priv->avg_rate = -1.0;
2445 priv->avg_render = -1;
2451 /* Checks if the object was scheduled too late.
2453 * start/stop contain the raw timestamp start and stop values
2456 * status and jitter contain the return values from the clock wait.
2458 * returns TRUE if the buffer was too late.
2461 gst_base_sink_is_too_late (GstBaseSink * basesink, GstMiniObject * obj,
2462 GstClockTime start, GstClockTime stop,
2463 GstClockReturn status, GstClockTimeDiff jitter)
2466 gint64 max_lateness;
2467 GstBaseSinkPrivate *priv;
2469 priv = basesink->priv;
2473 /* only for objects that were too late */
2474 if (G_LIKELY (status != GST_CLOCK_EARLY))
2477 max_lateness = basesink->abidata.ABI.max_lateness;
2479 /* check if frame dropping is enabled */
2480 if (max_lateness == -1)
2483 /* only check for buffers */
2484 if (G_UNLIKELY (!GST_IS_BUFFER (obj)))
2487 /* can't do check if we don't have a timestamp */
2488 if (G_UNLIKELY (start == -1))
2491 /* we can add a valid stop time */
2493 max_lateness += stop;
2495 max_lateness += start;
2497 /* if the jitter bigger than duration and lateness we are too late */
2498 if ((late = start + jitter > max_lateness)) {
2499 GST_DEBUG_OBJECT (basesink, "buffer is too late %" GST_TIME_FORMAT
2500 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (start + jitter),
2501 GST_TIME_ARGS (max_lateness));
2502 /* !!emergency!!, if we did not receive anything valid for more than a
2503 * second, render it anyway so the user sees something */
2504 if (priv->last_in_time != -1 && start - priv->last_in_time > GST_SECOND) {
2506 GST_ELEMENT_WARNING (basesink, CORE, CLOCK,
2507 (_("A lot of buffers are being dropped.")),
2508 ("There may be a timestamping problem, or this computer is too slow."));
2509 GST_DEBUG_OBJECT (basesink,
2510 "**emergency** last buffer at %" GST_TIME_FORMAT " > GST_SECOND",
2511 GST_TIME_ARGS (priv->last_in_time));
2517 priv->last_in_time = start;
2524 GST_DEBUG_OBJECT (basesink, "object was scheduled in time");
2529 GST_DEBUG_OBJECT (basesink, "frame dropping disabled");
2534 GST_DEBUG_OBJECT (basesink, "object is not a buffer");
2539 GST_DEBUG_OBJECT (basesink, "buffer has no timestamp");
2544 /* called before and after calling the render vmethod. It keeps track of how
2545 * much time was spent in the render method and is used to check if we are
2548 gst_base_sink_do_render_stats (GstBaseSink * basesink, gboolean start)
2550 GstBaseSinkPrivate *priv;
2552 priv = basesink->priv;
2555 priv->start = gst_util_get_timestamp ();
2557 GstClockTime elapsed;
2559 priv->stop = gst_util_get_timestamp ();
2561 elapsed = GST_CLOCK_DIFF (priv->start, priv->stop);
2563 if (priv->avg_render == -1)
2564 priv->avg_render = elapsed;
2566 priv->avg_render = UPDATE_RUNNING_AVG (priv->avg_render, elapsed);
2568 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2569 "avg_render: %" GST_TIME_FORMAT, GST_TIME_ARGS (priv->avg_render));
2573 /* with STREAM_LOCK, PREROLL_LOCK,
2575 * Synchronize the object on the clock and then render it.
2577 * takes ownership of obj.
2579 static GstFlowReturn
2580 gst_base_sink_render_object (GstBaseSink * basesink, GstPad * pad,
2581 GstMiniObject * obj)
2584 GstBaseSinkClass *bclass;
2585 gboolean late, step_end;
2587 GstBaseSinkPrivate *priv;
2589 priv = basesink->priv;
2596 /* synchronize this object, non syncable objects return OK
2598 ret = gst_base_sink_do_sync (basesink, pad, obj, &late, &step_end);
2599 if (G_UNLIKELY (ret != GST_FLOW_OK))
2602 /* and now render, event or buffer. */
2603 if (G_LIKELY (GST_IS_BUFFER (obj))) {
2606 /* drop late buffers unconditionally, let's hope it's unlikely */
2607 if (G_UNLIKELY (late && !step_end))
2610 buf = GST_BUFFER_CAST (obj);
2612 gst_base_sink_set_last_buffer (basesink, buf);
2614 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2616 if (G_LIKELY (bclass->render)) {
2619 /* read once, to get same value before and after */
2620 do_qos = g_atomic_int_get (&priv->qos_enabled);
2622 GST_DEBUG_OBJECT (basesink, "rendering buffer %p", obj);
2624 /* record rendering time for QoS and stats */
2626 gst_base_sink_do_render_stats (basesink, TRUE);
2628 ret = bclass->render (basesink, buf);
2631 gst_base_sink_do_render_stats (basesink, FALSE);
2633 if (ret == GST_FLOW_STEP)
2637 stop_stepping (basesink, &basesink->segment, &priv->current_step,
2638 priv->current_rstart, priv->current_rstop);
2645 GstEvent *event = GST_EVENT_CAST (obj);
2646 gboolean event_res = TRUE;
2649 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2651 type = GST_EVENT_TYPE (event);
2653 GST_DEBUG_OBJECT (basesink, "rendering event %p, type %s", obj,
2654 gst_event_type_get_name (type));
2657 event_res = bclass->event (basesink, event);
2659 /* when we get here we could be flushing again when the event handler calls
2660 * _wait_eos(). We have to ignore this object in that case. */
2661 if (G_UNLIKELY (basesink->flushing))
2664 if (G_LIKELY (event_res)) {
2667 seqnum = basesink->priv->seqnum = gst_event_get_seqnum (event);
2668 GST_DEBUG_OBJECT (basesink, "Got seqnum #%" G_GUINT32_FORMAT, seqnum);
2673 GstMessage *message;
2675 /* the EOS event is completely handled so we mark
2676 * ourselves as being in the EOS state. eos is also
2677 * protected by the object lock so we can read it when
2678 * answering the POSITION query. */
2679 GST_OBJECT_LOCK (basesink);
2680 basesink->eos = TRUE;
2681 GST_OBJECT_UNLOCK (basesink);
2683 /* ok, now we can post the message */
2684 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
2686 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
2687 gst_message_set_seqnum (message, seqnum);
2688 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
2691 case GST_EVENT_NEWSEGMENT:
2692 /* configure the segment */
2693 gst_base_sink_configure_segment (basesink, pad, event,
2694 &basesink->segment);
2703 gst_base_sink_perform_qos (basesink, late);
2705 GST_DEBUG_OBJECT (basesink, "object unref after render %p", obj);
2706 gst_mini_object_unref (obj);
2713 GST_DEBUG_OBJECT (basesink, "do_sync returned %s", gst_flow_get_name (ret));
2719 GST_DEBUG_OBJECT (basesink, "buffer late, dropping");
2724 GST_DEBUG_OBJECT (basesink, "we are flushing, ignore object");
2725 gst_mini_object_unref (obj);
2726 return GST_FLOW_WRONG_STATE;
2730 /* with STREAM_LOCK, PREROLL_LOCK
2732 * Perform preroll on the given object. For buffers this means
2733 * calling the preroll subclass method.
2734 * If that succeeds, the state will be commited.
2736 * function does not take ownership of obj.
2738 static GstFlowReturn
2739 gst_base_sink_preroll_object (GstBaseSink * basesink, GstMiniObject * obj)
2743 GST_DEBUG_OBJECT (basesink, "prerolling object %p", obj);
2745 /* if it's a buffer, we need to call the preroll method */
2746 if (G_LIKELY (GST_IS_BUFFER (obj)) && basesink->priv->call_preroll) {
2747 GstBaseSinkClass *bclass;
2749 GstClockTime timestamp;
2751 buf = GST_BUFFER_CAST (obj);
2752 timestamp = GST_BUFFER_TIMESTAMP (buf);
2754 GST_DEBUG_OBJECT (basesink, "preroll buffer %" GST_TIME_FORMAT,
2755 GST_TIME_ARGS (timestamp));
2757 gst_base_sink_set_last_buffer (basesink, buf);
2759 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2760 if (bclass->preroll)
2761 if ((ret = bclass->preroll (basesink, buf)) != GST_FLOW_OK)
2762 goto preroll_failed;
2764 basesink->priv->call_preroll = FALSE;
2768 if (G_LIKELY (basesink->playing_async)) {
2769 if (G_UNLIKELY (!gst_base_sink_commit_state (basesink)))
2778 GST_DEBUG_OBJECT (basesink, "preroll failed, abort state");
2779 gst_element_abort_state (GST_ELEMENT_CAST (basesink));
2784 GST_DEBUG_OBJECT (basesink, "stopping while commiting state");
2785 return GST_FLOW_WRONG_STATE;
2789 /* with STREAM_LOCK, PREROLL_LOCK
2791 * Queue an object for rendering.
2792 * The first prerollable object queued will complete the preroll. If the
2793 * preroll queue if filled, we render all the objects in the queue.
2795 * This function takes ownership of the object.
2797 static GstFlowReturn
2798 gst_base_sink_queue_object_unlocked (GstBaseSink * basesink, GstPad * pad,
2799 GstMiniObject * obj, gboolean prerollable)
2801 GstFlowReturn ret = GST_FLOW_OK;
2805 if (G_UNLIKELY (basesink->need_preroll)) {
2806 if (G_LIKELY (prerollable))
2807 basesink->preroll_queued++;
2809 length = basesink->preroll_queued;
2811 GST_DEBUG_OBJECT (basesink, "now %d prerolled items", length);
2813 /* first prerollable item needs to finish the preroll */
2815 ret = gst_base_sink_preroll_object (basesink, obj);
2816 if (G_UNLIKELY (ret != GST_FLOW_OK))
2817 goto preroll_failed;
2819 /* need to recheck if we need preroll, commmit state during preroll
2820 * could have made us not need more preroll. */
2821 if (G_UNLIKELY (basesink->need_preroll)) {
2822 /* see if we can render now, if we can't add the object to the preroll
2824 if (G_UNLIKELY (length <= basesink->preroll_queue_max_len))
2829 /* we can start rendering (or blocking) the queued object
2831 q = basesink->preroll_queue;
2832 while (G_UNLIKELY (!g_queue_is_empty (q))) {
2835 o = g_queue_pop_head (q);
2836 GST_DEBUG_OBJECT (basesink, "rendering queued object %p", o);
2838 /* do something with the return value */
2839 ret = gst_base_sink_render_object (basesink, pad, o);
2840 if (ret != GST_FLOW_OK)
2841 goto dequeue_failed;
2844 /* now render the object */
2845 ret = gst_base_sink_render_object (basesink, pad, obj);
2846 basesink->preroll_queued = 0;
2853 GST_DEBUG_OBJECT (basesink, "preroll failed, reason %s",
2854 gst_flow_get_name (ret));
2855 gst_mini_object_unref (obj);
2860 /* add object to the queue and return */
2861 GST_DEBUG_OBJECT (basesink, "need more preroll data %d <= %d",
2862 length, basesink->preroll_queue_max_len);
2863 g_queue_push_tail (basesink->preroll_queue, obj);
2868 GST_DEBUG_OBJECT (basesink, "rendering queued objects failed, reason %s",
2869 gst_flow_get_name (ret));
2870 gst_mini_object_unref (obj);
2877 * This function grabs the PREROLL_LOCK and adds the object to
2880 * This function takes ownership of obj.
2882 static GstFlowReturn
2883 gst_base_sink_queue_object (GstBaseSink * basesink, GstPad * pad,
2884 GstMiniObject * obj, gboolean prerollable)
2888 GST_PAD_PREROLL_LOCK (pad);
2889 if (G_UNLIKELY (basesink->flushing))
2892 if (G_UNLIKELY (basesink->priv->received_eos))
2895 ret = gst_base_sink_queue_object_unlocked (basesink, pad, obj, prerollable);
2896 GST_PAD_PREROLL_UNLOCK (pad);
2903 GST_DEBUG_OBJECT (basesink, "sink is flushing");
2904 GST_PAD_PREROLL_UNLOCK (pad);
2905 gst_mini_object_unref (obj);
2906 return GST_FLOW_WRONG_STATE;
2910 GST_DEBUG_OBJECT (basesink,
2911 "we are EOS, dropping object, return UNEXPECTED");
2912 GST_PAD_PREROLL_UNLOCK (pad);
2913 gst_mini_object_unref (obj);
2914 return GST_FLOW_UNEXPECTED;
2919 gst_base_sink_flush_start (GstBaseSink * basesink, GstPad * pad)
2921 /* make sure we are not blocked on the clock also clear any pending
2923 gst_base_sink_set_flushing (basesink, pad, TRUE);
2925 /* we grab the stream lock but that is not needed since setting the
2926 * sink to flushing would make sure no state commit is being done
2928 GST_PAD_STREAM_LOCK (pad);
2929 gst_base_sink_reset_qos (basesink);
2930 if (basesink->priv->async_enabled) {
2931 /* and we need to commit our state again on the next
2932 * prerolled buffer */
2933 basesink->playing_async = TRUE;
2934 gst_element_lost_state (GST_ELEMENT_CAST (basesink));
2936 basesink->priv->have_latency = TRUE;
2937 basesink->need_preroll = FALSE;
2939 gst_base_sink_set_last_buffer (basesink, NULL);
2940 GST_PAD_STREAM_UNLOCK (pad);
2944 gst_base_sink_flush_stop (GstBaseSink * basesink, GstPad * pad)
2946 /* unset flushing so we can accept new data, this also flushes out any EOS
2948 gst_base_sink_set_flushing (basesink, pad, FALSE);
2950 /* for position reporting */
2951 GST_OBJECT_LOCK (basesink);
2952 basesink->priv->current_sstart = -1;
2953 basesink->priv->current_sstop = -1;
2954 basesink->priv->eos_rtime = -1;
2955 basesink->priv->call_preroll = TRUE;
2956 basesink->priv->current_step.valid = FALSE;
2957 basesink->priv->pending_step.valid = FALSE;
2958 if (basesink->pad_mode == GST_ACTIVATE_PUSH) {
2959 /* we need new segment info after the flush. */
2960 basesink->have_newsegment = FALSE;
2961 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
2962 gst_segment_init (basesink->abidata.ABI.clip_segment, GST_FORMAT_UNDEFINED);
2964 GST_OBJECT_UNLOCK (basesink);
2968 gst_base_sink_event (GstPad * pad, GstEvent * event)
2970 GstBaseSink *basesink;
2971 gboolean result = TRUE;
2972 GstBaseSinkClass *bclass;
2974 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
2976 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2978 GST_DEBUG_OBJECT (basesink, "event %p (%s)", event,
2979 GST_EVENT_TYPE_NAME (event));
2981 switch (GST_EVENT_TYPE (event)) {
2986 GST_PAD_PREROLL_LOCK (pad);
2987 if (G_UNLIKELY (basesink->flushing))
2990 if (G_UNLIKELY (basesink->priv->received_eos)) {
2991 /* we can't accept anything when we are EOS */
2993 gst_event_unref (event);
2995 /* we set the received EOS flag here so that we can use it when testing if
2996 * we are prerolled and to refuse more buffers. */
2997 basesink->priv->received_eos = TRUE;
2999 /* EOS is a prerollable object, we call the unlocked version because it
3000 * does not check the received_eos flag. */
3001 ret = gst_base_sink_queue_object_unlocked (basesink, pad,
3002 GST_MINI_OBJECT_CAST (event), TRUE);
3003 if (G_UNLIKELY (ret != GST_FLOW_OK))
3006 GST_PAD_PREROLL_UNLOCK (pad);
3009 case GST_EVENT_NEWSEGMENT:
3013 GST_DEBUG_OBJECT (basesink, "newsegment %p", event);
3015 GST_PAD_PREROLL_LOCK (pad);
3016 if (G_UNLIKELY (basesink->flushing))
3019 if (G_UNLIKELY (basesink->priv->received_eos)) {
3020 /* we can't accept anything when we are EOS */
3022 gst_event_unref (event);
3024 /* the new segment is a non prerollable item and does not block anything,
3025 * we need to configure the current clipping segment and insert the event
3026 * in the queue to serialize it with the buffers for rendering. */
3027 gst_base_sink_configure_segment (basesink, pad, event,
3028 basesink->abidata.ABI.clip_segment);
3031 gst_base_sink_queue_object_unlocked (basesink, pad,
3032 GST_MINI_OBJECT_CAST (event), FALSE);
3033 if (G_UNLIKELY (ret != GST_FLOW_OK))
3036 GST_OBJECT_LOCK (basesink);
3037 basesink->have_newsegment = TRUE;
3038 GST_OBJECT_UNLOCK (basesink);
3041 GST_PAD_PREROLL_UNLOCK (pad);
3044 case GST_EVENT_FLUSH_START:
3046 bclass->event (basesink, event);
3048 GST_DEBUG_OBJECT (basesink, "flush-start %p", event);
3050 gst_base_sink_flush_start (basesink, pad);
3052 gst_event_unref (event);
3054 case GST_EVENT_FLUSH_STOP:
3056 bclass->event (basesink, event);
3058 GST_DEBUG_OBJECT (basesink, "flush-stop %p", event);
3060 gst_base_sink_flush_stop (basesink, pad);
3062 gst_event_unref (event);
3065 /* other events are sent to queue or subclass depending on if they
3066 * are serialized. */
3067 if (GST_EVENT_IS_SERIALIZED (event)) {
3068 gst_base_sink_queue_object (basesink, pad,
3069 GST_MINI_OBJECT_CAST (event), FALSE);
3072 bclass->event (basesink, event);
3073 gst_event_unref (event);
3078 gst_object_unref (basesink);
3085 GST_DEBUG_OBJECT (basesink, "we are flushing");
3086 GST_PAD_PREROLL_UNLOCK (pad);
3088 gst_event_unref (event);
3093 /* default implementation to calculate the start and end
3094 * timestamps on a buffer, subclasses can override
3097 gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
3098 GstClockTime * start, GstClockTime * end)
3100 GstClockTime timestamp, duration;
3102 timestamp = GST_BUFFER_TIMESTAMP (buffer);
3103 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
3105 /* get duration to calculate end time */
3106 duration = GST_BUFFER_DURATION (buffer);
3107 if (GST_CLOCK_TIME_IS_VALID (duration)) {
3108 *end = timestamp + duration;
3114 /* must be called with PREROLL_LOCK */
3116 gst_base_sink_needs_preroll (GstBaseSink * basesink)
3118 gboolean is_prerolled, res;
3120 /* we have 2 cases where the PREROLL_LOCK is released:
3121 * 1) we are blocking in the PREROLL_LOCK and thus are prerolled.
3122 * 2) we are syncing on the clock
3124 is_prerolled = basesink->have_preroll || basesink->priv->received_eos;
3125 res = !is_prerolled;
3127 GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d => needs preroll: %d",
3128 basesink->have_preroll, basesink->priv->received_eos, res);
3133 /* with STREAM_LOCK, PREROLL_LOCK
3135 * Takes a buffer and compare the timestamps with the last segment.
3136 * If the buffer falls outside of the segment boundaries, drop it.
3137 * Else queue the buffer for preroll and rendering.
3139 * This function takes ownership of the buffer.
3141 static GstFlowReturn
3142 gst_base_sink_chain_unlocked (GstBaseSink * basesink, GstPad * pad,
3145 GstBaseSinkClass *bclass;
3146 GstFlowReturn result;
3147 GstClockTime start = GST_CLOCK_TIME_NONE, end = GST_CLOCK_TIME_NONE;
3148 GstSegment *clip_segment;
3150 if (G_UNLIKELY (basesink->flushing))
3153 if (G_UNLIKELY (basesink->priv->received_eos))
3156 /* for code clarity */
3157 clip_segment = basesink->abidata.ABI.clip_segment;
3159 if (G_UNLIKELY (!basesink->have_newsegment)) {
3162 sync = gst_base_sink_get_sync (basesink);
3164 GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
3165 (_("Internal data flow problem.")),
3166 ("Received buffer without a new-segment. Assuming timestamps start from 0."));
3169 /* this means this sink will assume timestamps start from 0 */
3170 GST_OBJECT_LOCK (basesink);
3171 clip_segment->start = 0;
3172 clip_segment->stop = -1;
3173 basesink->segment.start = 0;
3174 basesink->segment.stop = -1;
3175 basesink->have_newsegment = TRUE;
3176 GST_OBJECT_UNLOCK (basesink);
3179 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3181 /* check if the buffer needs to be dropped, we first ask the subclass for the
3183 if (bclass->get_times)
3184 bclass->get_times (basesink, buf, &start, &end);
3187 /* if the subclass does not want sync, we use our own values so that we at
3188 * least clip the buffer to the segment */
3189 gst_base_sink_get_times (basesink, buf, &start, &end);
3192 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
3193 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
3195 /* a dropped buffer does not participate in anything */
3196 if (GST_CLOCK_TIME_IS_VALID (start) &&
3197 (clip_segment->format == GST_FORMAT_TIME)) {
3198 if (G_UNLIKELY (!gst_segment_clip (clip_segment,
3199 GST_FORMAT_TIME, (gint64) start, (gint64) end, NULL, NULL)))
3200 goto out_of_segment;
3203 /* now we can process the buffer in the queue, this function takes ownership
3205 result = gst_base_sink_queue_object_unlocked (basesink, pad,
3206 GST_MINI_OBJECT_CAST (buf), TRUE);
3213 GST_DEBUG_OBJECT (basesink, "sink is flushing");
3214 gst_buffer_unref (buf);
3215 return GST_FLOW_WRONG_STATE;
3219 GST_DEBUG_OBJECT (basesink,
3220 "we are EOS, dropping object, return UNEXPECTED");
3221 gst_buffer_unref (buf);
3222 return GST_FLOW_UNEXPECTED;
3226 GST_DEBUG_OBJECT (basesink, "dropping buffer, out of clipping segment");
3227 gst_buffer_unref (buf);
3234 static GstFlowReturn
3235 gst_base_sink_chain (GstPad * pad, GstBuffer * buf)
3237 GstBaseSink *basesink;
3238 GstFlowReturn result;
3240 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
3242 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH))
3245 GST_PAD_PREROLL_LOCK (pad);
3246 result = gst_base_sink_chain_unlocked (basesink, pad, buf);
3247 GST_PAD_PREROLL_UNLOCK (pad);
3255 GST_OBJECT_LOCK (pad);
3256 GST_WARNING_OBJECT (basesink,
3257 "Push on pad %s:%s, but it was not activated in push mode",
3258 GST_DEBUG_PAD_NAME (pad));
3259 GST_OBJECT_UNLOCK (pad);
3260 gst_buffer_unref (buf);
3261 /* we don't post an error message this will signal to the peer
3262 * pushing that EOS is reached. */
3263 result = GST_FLOW_UNEXPECTED;
3269 gst_base_sink_default_do_seek (GstBaseSink * sink, GstSegment * segment)
3271 gboolean res = TRUE;
3273 /* update our offset if the start/stop position was updated */
3274 if (segment->format == GST_FORMAT_BYTES) {
3275 segment->time = segment->start;
3276 } else if (segment->start == 0) {
3277 /* seek to start, we can implement a default for this. */
3281 GST_INFO_OBJECT (sink, "Can't do a default seek");
3287 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
3290 gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
3291 GstEvent * event, GstSegment * segment)
3293 /* By default, we try one of 2 things:
3294 * - For absolute seek positions, convert the requested position to our
3295 * configured processing format and place it in the output segment \
3296 * - For relative seek positions, convert our current (input) values to the
3297 * seek format, adjust by the relative seek offset and then convert back to
3298 * the processing format
3300 GstSeekType cur_type, stop_type;
3303 GstFormat seek_format, dest_format;
3306 gboolean res = TRUE;
3308 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3309 &cur_type, &cur, &stop_type, &stop);
3310 dest_format = segment->format;
3312 if (seek_format == dest_format) {
3313 gst_segment_set_seek (segment, rate, seek_format, flags,
3314 cur_type, cur, stop_type, stop, &update);
3318 if (cur_type != GST_SEEK_TYPE_NONE) {
3319 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3321 gst_pad_query_convert (sink->sinkpad, seek_format, cur, &dest_format,
3323 cur_type = GST_SEEK_TYPE_SET;
3326 if (res && stop_type != GST_SEEK_TYPE_NONE) {
3327 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3329 gst_pad_query_convert (sink->sinkpad, seek_format, stop, &dest_format,
3331 stop_type = GST_SEEK_TYPE_SET;
3334 /* And finally, configure our output segment in the desired format */
3335 gst_segment_set_seek (segment, rate, dest_format, flags, cur_type, cur,
3336 stop_type, stop, &update);
3345 GST_DEBUG_OBJECT (sink, "undefined format given, seek aborted.");
3350 /* perform a seek, only executed in pull mode */
3352 gst_base_sink_perform_seek (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3356 GstFormat seek_format, dest_format;
3358 GstSeekType cur_type, stop_type;
3359 gboolean seekseg_configured = FALSE;
3361 gboolean update, res = TRUE;
3362 GstSegment seeksegment;
3364 dest_format = sink->segment.format;
3367 GST_DEBUG_OBJECT (sink, "performing seek with event %p", event);
3368 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3369 &cur_type, &cur, &stop_type, &stop);
3371 flush = flags & GST_SEEK_FLAG_FLUSH;
3373 GST_DEBUG_OBJECT (sink, "performing seek without event");
3378 GST_DEBUG_OBJECT (sink, "flushing upstream");
3379 gst_pad_push_event (pad, gst_event_new_flush_start ());
3380 gst_base_sink_flush_start (sink, pad);
3382 GST_DEBUG_OBJECT (sink, "pausing pulling thread");
3385 GST_PAD_STREAM_LOCK (pad);
3387 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
3388 * copy the current segment info into the temp segment that we can actually
3389 * attempt the seek with. We only update the real segment if the seek suceeds. */
3390 if (!seekseg_configured) {
3391 memcpy (&seeksegment, &sink->segment, sizeof (GstSegment));
3393 /* now configure the final seek segment */
3395 if (sink->segment.format != seek_format) {
3396 /* OK, here's where we give the subclass a chance to convert the relative
3397 * seek into an absolute one in the processing format. We set up any
3398 * absolute seek above, before taking the stream lock. */
3399 if (!gst_base_sink_default_prepare_seek_segment (sink, event,
3401 GST_DEBUG_OBJECT (sink,
3402 "Preparing the seek failed after flushing. " "Aborting seek");
3406 /* The seek format matches our processing format, no need to ask the
3407 * the subclass to configure the segment. */
3408 gst_segment_set_seek (&seeksegment, rate, seek_format, flags,
3409 cur_type, cur, stop_type, stop, &update);
3412 /* Else, no seek event passed, so we're just (re)starting the
3417 GST_DEBUG_OBJECT (sink, "segment configured from %" G_GINT64_FORMAT
3418 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
3419 seeksegment.start, seeksegment.stop, seeksegment.last_stop);
3421 /* do the seek, segment.last_stop contains the new position. */
3422 res = gst_base_sink_default_do_seek (sink, &seeksegment);
3427 GST_DEBUG_OBJECT (sink, "stop flushing upstream");
3428 gst_pad_push_event (pad, gst_event_new_flush_stop ());
3429 gst_base_sink_flush_stop (sink, pad);
3430 } else if (res && sink->abidata.ABI.running) {
3431 /* we are running the current segment and doing a non-flushing seek,
3432 * close the segment first based on the last_stop. */
3433 GST_DEBUG_OBJECT (sink, "closing running segment %" G_GINT64_FORMAT
3434 " to %" G_GINT64_FORMAT, sink->segment.start, sink->segment.last_stop);
3437 /* The subclass must have converted the segment to the processing format
3439 if (res && seeksegment.format != dest_format) {
3440 GST_DEBUG_OBJECT (sink, "Subclass failed to prepare a seek segment "
3441 "in the correct format. Aborting seek.");
3445 /* if successfull seek, we update our real segment and push
3446 * out the new segment. */
3448 memcpy (&sink->segment, &seeksegment, sizeof (GstSegment));
3450 if (sink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3451 gst_element_post_message (GST_ELEMENT (sink),
3452 gst_message_new_segment_start (GST_OBJECT (sink),
3453 sink->segment.format, sink->segment.last_stop));
3457 sink->priv->discont = TRUE;
3458 sink->abidata.ABI.running = TRUE;
3460 GST_PAD_STREAM_UNLOCK (pad);
3466 set_step_info (GstBaseSink * sink, GstStepInfo * current, GstStepInfo * pending,
3467 guint seqnum, GstFormat format, guint64 amount, gdouble rate,
3468 gboolean flush, gboolean intermediate)
3470 GST_OBJECT_LOCK (sink);
3471 pending->seqnum = seqnum;
3472 pending->format = format;
3473 pending->amount = amount;
3474 pending->position = 0;
3475 pending->rate = rate;
3476 pending->flush = flush;
3477 pending->intermediate = intermediate;
3478 pending->valid = TRUE;
3479 /* flush invalidates the current stepping segment */
3481 current->valid = FALSE;
3482 GST_OBJECT_UNLOCK (sink);
3486 gst_base_sink_perform_step (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3488 GstBaseSinkPrivate *priv;
3489 GstBaseSinkClass *bclass;
3490 gboolean flush, intermediate;
3495 GstStepInfo *pending, *current;
3497 bclass = GST_BASE_SINK_GET_CLASS (sink);
3500 GST_DEBUG_OBJECT (sink, "performing step with event %p", event);
3502 gst_event_parse_step (event, &format, &amount, &rate, &flush, &intermediate);
3503 seqnum = gst_event_get_seqnum (event);
3505 pending = &priv->pending_step;
3506 current = &priv->current_step;
3509 /* we need to call ::unlock before locking PREROLL_LOCK
3510 * since we lock it before going into ::render */
3512 bclass->unlock (sink);
3514 GST_PAD_PREROLL_LOCK (sink->sinkpad);
3515 /* now that we have the PREROLL lock, clear our unlock request */
3516 if (bclass->unlock_stop)
3517 bclass->unlock_stop (sink);
3519 /* update the stepinfo and make it valid */
3520 set_step_info (sink, current, pending, seqnum, format, amount, rate, flush,
3523 if (sink->priv->async_enabled) {
3524 /* and we need to commit our state again on the next
3525 * prerolled buffer */
3526 sink->playing_async = TRUE;
3527 priv->pending_step.need_preroll = TRUE;
3528 sink->need_preroll = FALSE;
3529 gst_element_lost_state_full (GST_ELEMENT_CAST (sink), FALSE);
3531 sink->priv->have_latency = TRUE;
3532 sink->need_preroll = FALSE;
3534 priv->current_sstart = -1;
3535 priv->current_sstop = -1;
3536 priv->eos_rtime = -1;
3537 priv->call_preroll = TRUE;
3538 gst_base_sink_set_last_buffer (sink, NULL);
3539 gst_base_sink_reset_qos (sink);
3541 if (sink->clock_id) {
3542 gst_clock_id_unschedule (sink->clock_id);
3545 if (sink->have_preroll) {
3546 GST_DEBUG_OBJECT (sink, "signal waiter");
3547 priv->step_unlock = TRUE;
3548 GST_PAD_PREROLL_SIGNAL (sink->sinkpad);
3550 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
3552 /* update the stepinfo and make it valid */
3553 set_step_info (sink, current, pending, seqnum, format, amount, rate, flush,
3563 gst_base_sink_loop (GstPad * pad)
3565 GstBaseSink *basesink;
3566 GstBuffer *buf = NULL;
3567 GstFlowReturn result;
3571 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
3573 g_assert (basesink->pad_mode == GST_ACTIVATE_PULL);
3575 if ((blocksize = basesink->priv->blocksize) == 0)
3578 offset = basesink->segment.last_stop;
3580 GST_DEBUG_OBJECT (basesink, "pulling %" G_GUINT64_FORMAT ", %u",
3583 result = gst_pad_pull_range (pad, offset, blocksize, &buf);
3584 if (G_UNLIKELY (result != GST_FLOW_OK))
3587 if (G_UNLIKELY (buf == NULL))
3590 offset += GST_BUFFER_SIZE (buf);
3592 gst_segment_set_last_stop (&basesink->segment, GST_FORMAT_BYTES, offset);
3594 GST_PAD_PREROLL_LOCK (pad);
3595 result = gst_base_sink_chain_unlocked (basesink, pad, buf);
3596 GST_PAD_PREROLL_UNLOCK (pad);
3597 if (G_UNLIKELY (result != GST_FLOW_OK))
3605 GST_LOG_OBJECT (basesink, "pausing task, reason %s",
3606 gst_flow_get_name (result));
3607 gst_pad_pause_task (pad);
3608 /* fatal errors and NOT_LINKED cause EOS */
3609 if (GST_FLOW_IS_FATAL (result) || result == GST_FLOW_NOT_LINKED) {
3610 if (result == GST_FLOW_UNEXPECTED) {
3611 /* perform EOS logic */
3612 if (basesink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3613 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3614 gst_message_new_segment_done (GST_OBJECT_CAST (basesink),
3615 basesink->segment.format, basesink->segment.last_stop));
3617 gst_base_sink_event (pad, gst_event_new_eos ());
3620 /* for fatal errors we post an error message, post the error
3621 * first so the app knows about the error first. */
3622 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
3623 (_("Internal data stream error.")),
3624 ("stream stopped, reason %s", gst_flow_get_name (result)));
3625 gst_base_sink_event (pad, gst_event_new_eos ());
3632 GST_LOG_OBJECT (basesink, "no buffer, pausing");
3633 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
3634 (_("Internal data flow error.")), ("element returned NULL buffer"));
3635 result = GST_FLOW_ERROR;
3641 gst_base_sink_set_flushing (GstBaseSink * basesink, GstPad * pad,
3644 GstBaseSinkClass *bclass;
3646 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3649 /* unlock any subclasses, we need to do this before grabbing the
3650 * PREROLL_LOCK since we hold this lock before going into ::render. */
3652 bclass->unlock (basesink);
3655 GST_PAD_PREROLL_LOCK (pad);
3656 basesink->flushing = flushing;
3658 /* step 1, now that we have the PREROLL lock, clear our unlock request */
3659 if (bclass->unlock_stop)
3660 bclass->unlock_stop (basesink);
3662 /* set need_preroll before we unblock the clock. If the clock is unblocked
3663 * before timing out, we can reuse the buffer for preroll. */
3664 basesink->need_preroll = TRUE;
3666 /* step 2, unblock clock sync (if any) or any other blocking thing */
3667 if (basesink->clock_id) {
3668 gst_clock_id_unschedule (basesink->clock_id);
3671 /* flush out the data thread if it's locked in finish_preroll, this will
3672 * also flush out the EOS state */
3673 GST_DEBUG_OBJECT (basesink,
3674 "flushing out data thread, need preroll to TRUE");
3675 gst_base_sink_preroll_queue_flush (basesink, pad);
3677 GST_PAD_PREROLL_UNLOCK (pad);
3683 gst_base_sink_default_activate_pull (GstBaseSink * basesink, gboolean active)
3689 result = gst_pad_start_task (basesink->sinkpad,
3690 (GstTaskFunction) gst_base_sink_loop, basesink->sinkpad);
3692 /* step 2, make sure streaming finishes */
3693 result = gst_pad_stop_task (basesink->sinkpad);
3700 gst_base_sink_pad_activate (GstPad * pad)
3702 gboolean result = FALSE;
3703 GstBaseSink *basesink;
3705 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3707 GST_DEBUG_OBJECT (basesink, "Trying pull mode first");
3709 gst_base_sink_set_flushing (basesink, pad, FALSE);
3711 /* we need to have the pull mode enabled */
3712 if (!basesink->can_activate_pull) {
3713 GST_DEBUG_OBJECT (basesink, "pull mode disabled");
3717 /* check if downstreams supports pull mode at all */
3718 if (!gst_pad_check_pull_range (pad)) {
3719 GST_DEBUG_OBJECT (basesink, "pull mode not supported");
3723 /* set the pad mode before starting the task so that it's in the
3724 * correct state for the new thread. also the sink set_caps and get_caps
3725 * function checks this */
3726 basesink->pad_mode = GST_ACTIVATE_PULL;
3728 /* we first try to negotiate a format so that when we try to activate
3729 * downstream, it knows about our format */
3730 if (!gst_base_sink_negotiate_pull (basesink)) {
3731 GST_DEBUG_OBJECT (basesink, "failed to negotiate in pull mode");
3735 /* ok activate now */
3736 if (!gst_pad_activate_pull (pad, TRUE)) {
3737 /* clear any pending caps */
3738 GST_OBJECT_LOCK (basesink);
3739 gst_caps_replace (&basesink->priv->pull_caps, NULL);
3740 GST_OBJECT_UNLOCK (basesink);
3741 GST_DEBUG_OBJECT (basesink, "failed to activate in pull mode");
3745 GST_DEBUG_OBJECT (basesink, "Success activating pull mode");
3749 /* push mode fallback */
3751 GST_DEBUG_OBJECT (basesink, "Falling back to push mode");
3752 if ((result = gst_pad_activate_push (pad, TRUE))) {
3753 GST_DEBUG_OBJECT (basesink, "Success activating push mode");
3758 GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
3759 gst_base_sink_set_flushing (basesink, pad, TRUE);
3762 gst_object_unref (basesink);
3768 gst_base_sink_pad_activate_push (GstPad * pad, gboolean active)
3771 GstBaseSink *basesink;
3773 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3776 if (!basesink->can_activate_push) {
3778 basesink->pad_mode = GST_ACTIVATE_NONE;
3781 basesink->pad_mode = GST_ACTIVATE_PUSH;
3784 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH)) {
3785 g_warning ("Internal GStreamer activation error!!!");
3788 gst_base_sink_set_flushing (basesink, pad, TRUE);
3790 basesink->pad_mode = GST_ACTIVATE_NONE;
3794 gst_object_unref (basesink);
3800 gst_base_sink_negotiate_pull (GstBaseSink * basesink)
3807 /* this returns the intersection between our caps and the peer caps. If there
3808 * is no peer, it returns NULL and we can't operate in pull mode so we can
3809 * fail the negotiation. */
3810 caps = gst_pad_get_allowed_caps (GST_BASE_SINK_PAD (basesink));
3811 if (caps == NULL || gst_caps_is_empty (caps))
3812 goto no_caps_possible;
3814 GST_DEBUG_OBJECT (basesink, "allowed caps: %" GST_PTR_FORMAT, caps);
3816 caps = gst_caps_make_writable (caps);
3817 /* get the first (prefered) format */
3818 gst_caps_truncate (caps);
3820 gst_pad_fixate_caps (GST_BASE_SINK_PAD (basesink), caps);
3822 GST_DEBUG_OBJECT (basesink, "fixated to: %" GST_PTR_FORMAT, caps);
3824 if (gst_caps_is_any (caps)) {
3825 GST_DEBUG_OBJECT (basesink, "caps were ANY after fixating, "
3827 /* neither side has template caps in this case, so they are prepared for
3828 pull() without setcaps() */
3830 } else if (gst_caps_is_fixed (caps)) {
3831 if (!gst_pad_set_caps (GST_BASE_SINK_PAD (basesink), caps))
3832 goto could_not_set_caps;
3834 GST_OBJECT_LOCK (basesink);
3835 gst_caps_replace (&basesink->priv->pull_caps, caps);
3836 GST_OBJECT_UNLOCK (basesink);
3841 gst_caps_unref (caps);
3847 GST_INFO_OBJECT (basesink, "Pipeline could not agree on caps");
3848 GST_DEBUG_OBJECT (basesink, "get_allowed_caps() returned EMPTY");
3850 gst_caps_unref (caps);
3855 GST_INFO_OBJECT (basesink, "Could not set caps: %" GST_PTR_FORMAT, caps);
3856 gst_caps_unref (caps);
3861 /* this won't get called until we implement an activate function */
3863 gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active)
3865 gboolean result = FALSE;
3866 GstBaseSink *basesink;
3867 GstBaseSinkClass *bclass;
3869 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3870 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3876 /* we mark we have a newsegment here because pull based
3877 * mode works just fine without having a newsegment before the
3879 format = GST_FORMAT_BYTES;
3881 gst_segment_init (&basesink->segment, format);
3882 gst_segment_init (basesink->abidata.ABI.clip_segment, format);
3883 GST_OBJECT_LOCK (basesink);
3884 basesink->have_newsegment = TRUE;
3885 GST_OBJECT_UNLOCK (basesink);
3887 /* get the peer duration in bytes */
3888 result = gst_pad_query_peer_duration (pad, &format, &duration);
3890 GST_DEBUG_OBJECT (basesink,
3891 "setting duration in bytes to %" G_GINT64_FORMAT, duration);
3892 gst_segment_set_duration (basesink->abidata.ABI.clip_segment, format,
3894 gst_segment_set_duration (&basesink->segment, format, duration);
3896 GST_DEBUG_OBJECT (basesink, "unknown duration");
3899 if (bclass->activate_pull)
3900 result = bclass->activate_pull (basesink, TRUE);
3905 goto activate_failed;
3908 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PULL)) {
3909 g_warning ("Internal GStreamer activation error!!!");
3912 result = gst_base_sink_set_flushing (basesink, pad, TRUE);
3913 if (bclass->activate_pull)
3914 result &= bclass->activate_pull (basesink, FALSE);
3915 basesink->pad_mode = GST_ACTIVATE_NONE;
3916 /* clear any pending caps */
3917 GST_OBJECT_LOCK (basesink);
3918 gst_caps_replace (&basesink->priv->pull_caps, NULL);
3919 GST_OBJECT_UNLOCK (basesink);
3922 gst_object_unref (basesink);
3929 /* reset, as starting the thread failed */
3930 basesink->pad_mode = GST_ACTIVATE_NONE;
3932 GST_ERROR_OBJECT (basesink, "subclass failed to activate in pull mode");
3937 /* send an event to our sinkpad peer. */
3939 gst_base_sink_send_event (GstElement * element, GstEvent * event)
3942 GstBaseSink *basesink = GST_BASE_SINK (element);
3943 gboolean forward, result = TRUE;
3944 GstActivateMode mode;
3946 GST_OBJECT_LOCK (element);
3947 /* get the pad and the scheduling mode */
3948 pad = gst_object_ref (basesink->sinkpad);
3949 mode = basesink->pad_mode;
3950 GST_OBJECT_UNLOCK (element);
3952 /* only push UPSTREAM events upstream */
3953 forward = GST_EVENT_IS_UPSTREAM (event);
3955 switch (GST_EVENT_TYPE (event)) {
3956 case GST_EVENT_LATENCY:
3958 GstClockTime latency;
3960 gst_event_parse_latency (event, &latency);
3962 /* store the latency. We use this to adjust the running_time before syncing
3963 * it to the clock. */
3964 GST_OBJECT_LOCK (element);
3965 basesink->priv->latency = latency;
3966 if (!basesink->priv->have_latency)
3968 GST_OBJECT_UNLOCK (element);
3969 GST_DEBUG_OBJECT (basesink, "latency set to %" GST_TIME_FORMAT,
3970 GST_TIME_ARGS (latency));
3972 /* We forward this event so that all elements know about the global pipeline
3973 * latency. This is interesting for an element when it wants to figure out
3974 * when a particular piece of data will be rendered. */
3977 case GST_EVENT_SEEK:
3978 /* in pull mode we will execute the seek */
3979 if (mode == GST_ACTIVATE_PULL)
3980 result = gst_base_sink_perform_seek (basesink, pad, event);
3982 case GST_EVENT_STEP:
3983 result = gst_base_sink_perform_step (basesink, pad, event);
3991 result = gst_pad_push_event (pad, event);
3993 /* not forwarded, unref the event */
3994 gst_event_unref (event);
3997 gst_object_unref (pad);
4002 gst_base_sink_peer_query (GstBaseSink * sink, GstQuery * query)
4005 gboolean res = FALSE;
4007 if ((peer = gst_pad_get_peer (sink->sinkpad))) {
4008 res = gst_pad_query (peer, query);
4009 gst_object_unref (peer);
4014 /* get the end position of the last seen object, this is used
4015 * for EOS and for making sure that we don't report a position we
4016 * have not reached yet. With LOCK. */
4018 gst_base_sink_get_position_last (GstBaseSink * basesink, GstFormat format,
4022 GstSegment *segment;
4023 gboolean ret = TRUE;
4025 segment = &basesink->segment;
4026 oformat = segment->format;
4028 if (oformat == GST_FORMAT_TIME) {
4029 /* return last observed stream time, we keep the stream time around in the
4031 *cur = basesink->priv->current_sstop;
4033 /* convert last stop to stream time */
4034 *cur = gst_segment_to_stream_time (segment, oformat, segment->last_stop);
4037 if (*cur != -1 && oformat != format) {
4038 GST_OBJECT_UNLOCK (basesink);
4039 /* convert to the target format if we need to, release lock first */
4041 gst_pad_query_convert (basesink->sinkpad, oformat, *cur, &format, cur);
4044 GST_OBJECT_LOCK (basesink);
4047 GST_DEBUG_OBJECT (basesink, "POSITION: %" GST_TIME_FORMAT,
4048 GST_TIME_ARGS (*cur));
4053 /* get the position when we are PAUSED, this is the stream time of the buffer
4054 * that prerolled. If no buffer is prerolled (we are still flushing), this
4055 * value will be -1. With LOCK. */
4057 gst_base_sink_get_position_paused (GstBaseSink * basesink, GstFormat format,
4062 GstSegment *segment;
4065 /* we don't use the clip segment in pull mode, when seeking we update the
4066 * main segment directly with the new segment values without it having to be
4067 * activated by the rendering after preroll */
4068 if (basesink->pad_mode == GST_ACTIVATE_PUSH)
4069 segment = basesink->abidata.ABI.clip_segment;
4071 segment = &basesink->segment;
4072 oformat = segment->format;
4074 if (oformat == GST_FORMAT_TIME) {
4075 *cur = basesink->priv->current_sstart;
4076 if (segment->rate < 0.0 && basesink->priv->current_sstop != -1) {
4077 /* for reverse playback we prefer the stream time stop position if we have
4079 *cur = basesink->priv->current_sstop;
4082 *cur = gst_segment_to_stream_time (segment, oformat, segment->last_stop);
4085 time = segment->time;
4088 *cur = MAX (*cur, time);
4089 GST_DEBUG_OBJECT (basesink, "POSITION as max: %" GST_TIME_FORMAT
4090 ", time %" GST_TIME_FORMAT, GST_TIME_ARGS (*cur), GST_TIME_ARGS (time));
4092 /* we have no buffer, use the segment times. */
4093 if (segment->rate >= 0.0) {
4094 /* forward, next position is always the time of the segment */
4096 GST_DEBUG_OBJECT (basesink, "POSITION as time: %" GST_TIME_FORMAT,
4097 GST_TIME_ARGS (*cur));
4099 /* reverse, next expected timestamp is segment->stop. We use the function
4100 * to get things right for negative applied_rates. */
4101 *cur = gst_segment_to_stream_time (segment, oformat, segment->stop);
4102 GST_DEBUG_OBJECT (basesink, "reverse POSITION: %" GST_TIME_FORMAT,
4103 GST_TIME_ARGS (*cur));
4108 if (res && oformat != format) {
4109 GST_OBJECT_UNLOCK (basesink);
4111 gst_pad_query_convert (basesink->sinkpad, oformat, *cur, &format, cur);
4114 GST_OBJECT_LOCK (basesink);
4121 gst_base_sink_get_position (GstBaseSink * basesink, GstFormat format,
4122 gint64 * cur, gboolean * upstream)
4125 gboolean res = FALSE;
4126 GstFormat oformat, tformat;
4127 GstClockTime now, base, latency;
4128 gint64 time, accum, duration;
4132 GST_OBJECT_LOCK (basesink);
4133 /* our intermediate time format */
4134 tformat = GST_FORMAT_TIME;
4135 /* get the format in the segment */
4136 oformat = basesink->segment.format;
4138 /* can only give answer based on the clock if not EOS */
4139 if (G_UNLIKELY (basesink->eos))
4142 /* we can only get the segment when we are not NULL or READY */
4143 if (!basesink->have_newsegment)
4146 /* when not in PLAYING or when we're busy with a state change, we
4147 * cannot read from the clock so we report time based on the
4148 * last seen timestamp. */
4149 if (GST_STATE (basesink) != GST_STATE_PLAYING ||
4150 GST_STATE_PENDING (basesink) != GST_STATE_VOID_PENDING)
4153 /* we need to sync on the clock. */
4154 if (basesink->sync == FALSE)
4157 /* and we need a clock */
4158 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (basesink)) == NULL))
4161 /* collect all data we need holding the lock */
4162 if (GST_CLOCK_TIME_IS_VALID (basesink->segment.time))
4163 time = basesink->segment.time;
4167 if (GST_CLOCK_TIME_IS_VALID (basesink->segment.stop))
4168 duration = basesink->segment.stop - basesink->segment.start;
4172 base = GST_ELEMENT_CAST (basesink)->base_time;
4173 accum = basesink->segment.accum;
4174 rate = basesink->segment.rate * basesink->segment.applied_rate;
4175 latency = basesink->priv->latency;
4177 gst_object_ref (clock);
4179 /* this function might release the LOCK */
4180 gst_base_sink_get_position_last (basesink, format, &last);
4182 /* need to release the object lock before we can get the time,
4183 * a clock might take the LOCK of the provider, which could be
4184 * a basesink subclass. */
4185 GST_OBJECT_UNLOCK (basesink);
4187 now = gst_clock_get_time (clock);
4189 if (oformat != tformat) {
4190 /* convert accum, time and duration to time */
4191 if (!gst_pad_query_convert (basesink->sinkpad, oformat, accum, &tformat,
4193 goto convert_failed;
4194 if (!gst_pad_query_convert (basesink->sinkpad, oformat, duration, &tformat,
4196 goto convert_failed;
4197 if (!gst_pad_query_convert (basesink->sinkpad, oformat, time, &tformat,
4199 goto convert_failed;
4202 /* subtract base time and accumulated time from the clock time.
4203 * Make sure we don't go negative. This is the current time in
4204 * the segment which we need to scale with the combined
4205 * rate and applied rate. */
4208 base = MIN (now, base);
4210 /* for negative rates we need to count back from from the segment
4215 *cur = time + gst_guint64_to_gdouble (now - base) * rate;
4217 /* never report more than last seen position */
4219 *cur = MIN (last, *cur);
4221 gst_object_unref (clock);
4223 GST_DEBUG_OBJECT (basesink,
4224 "now %" GST_TIME_FORMAT " - base %" GST_TIME_FORMAT " - accum %"
4225 GST_TIME_FORMAT " + time %" GST_TIME_FORMAT,
4226 GST_TIME_ARGS (now), GST_TIME_ARGS (base),
4227 GST_TIME_ARGS (accum), GST_TIME_ARGS (time));
4229 if (oformat != format) {
4230 /* convert time to final format */
4231 if (!gst_pad_query_convert (basesink->sinkpad, tformat, *cur, &format, cur))
4232 goto convert_failed;
4238 GST_DEBUG_OBJECT (basesink, "res: %d, POSITION: %" GST_TIME_FORMAT,
4239 res, GST_TIME_ARGS (*cur));
4245 GST_DEBUG_OBJECT (basesink, "position in EOS");
4246 res = gst_base_sink_get_position_last (basesink, format, cur);
4247 GST_OBJECT_UNLOCK (basesink);
4252 GST_DEBUG_OBJECT (basesink, "position in PAUSED");
4253 res = gst_base_sink_get_position_paused (basesink, format, cur);
4254 GST_OBJECT_UNLOCK (basesink);
4259 /* in NULL or READY we always return FALSE and -1 */
4260 GST_DEBUG_OBJECT (basesink, "position in wrong state, return -1");
4263 GST_OBJECT_UNLOCK (basesink);
4268 /* report last seen timestamp if any, else ask upstream to answer */
4269 if ((*cur = basesink->priv->current_sstart) != -1)
4274 GST_DEBUG_OBJECT (basesink, "no sync, res %d, POSITION %" GST_TIME_FORMAT,
4275 res, GST_TIME_ARGS (*cur));
4276 GST_OBJECT_UNLOCK (basesink);
4281 GST_DEBUG_OBJECT (basesink, "convert failed, try upstream");
4288 gst_base_sink_query (GstElement * element, GstQuery * query)
4290 gboolean res = FALSE;
4292 GstBaseSink *basesink = GST_BASE_SINK (element);
4294 switch (GST_QUERY_TYPE (query)) {
4295 case GST_QUERY_POSITION:
4299 gboolean upstream = FALSE;
4301 gst_query_parse_position (query, &format, NULL);
4303 GST_DEBUG_OBJECT (basesink, "position format %d", format);
4305 /* first try to get the position based on the clock */
4307 gst_base_sink_get_position (basesink, format, &cur, &upstream))) {
4308 gst_query_set_position (query, format, cur);
4309 } else if (upstream) {
4310 /* fallback to peer query */
4311 res = gst_base_sink_peer_query (basesink, query);
4315 case GST_QUERY_DURATION:
4317 GstFormat format, uformat;
4318 gint64 duration, uduration;
4320 gst_query_parse_duration (query, &format, NULL);
4322 GST_DEBUG_OBJECT (basesink, "duration query in format %s",
4323 gst_format_get_name (format));
4325 if (basesink->pad_mode == GST_ACTIVATE_PULL) {
4326 uformat = GST_FORMAT_BYTES;
4328 /* get the duration in bytes, in pull mode that's all we are sure to
4329 * know. We have to explicitly get this value from upstream instead of
4330 * using our cached value because it might change. Duration caching
4331 * should be done at a higher level. */
4332 res = gst_pad_query_peer_duration (basesink->sinkpad, &uformat,
4335 gst_segment_set_duration (&basesink->segment, uformat, uduration);
4336 if (format != uformat) {
4337 /* convert to the requested format */
4338 res = gst_pad_query_convert (basesink->sinkpad, uformat, uduration,
4339 &format, &duration);
4341 duration = uduration;
4344 /* set the result */
4345 gst_query_set_duration (query, format, duration);
4349 /* in push mode we simply forward upstream */
4350 res = gst_base_sink_peer_query (basesink, query);
4354 case GST_QUERY_LATENCY:
4356 gboolean live, us_live;
4357 GstClockTime min, max;
4359 if ((res = gst_base_sink_query_latency (basesink, &live, &us_live, &min,
4361 gst_query_set_latency (query, live, min, max);
4365 case GST_QUERY_JITTER:
4367 case GST_QUERY_RATE:
4368 /* gst_query_set_rate (query, basesink->segment_rate); */
4371 case GST_QUERY_SEGMENT:
4373 /* FIXME, bring start/stop to stream time */
4374 gst_query_set_segment (query, basesink->segment.rate,
4375 GST_FORMAT_TIME, basesink->segment.start, basesink->segment.stop);
4378 case GST_QUERY_SEEKING:
4379 case GST_QUERY_CONVERT:
4380 case GST_QUERY_FORMATS:
4382 res = gst_base_sink_peer_query (basesink, query);
4388 static GstStateChangeReturn
4389 gst_base_sink_change_state (GstElement * element, GstStateChange transition)
4391 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
4392 GstBaseSink *basesink = GST_BASE_SINK (element);
4393 GstBaseSinkClass *bclass;
4394 GstBaseSinkPrivate *priv;
4396 priv = basesink->priv;
4398 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4400 switch (transition) {
4401 case GST_STATE_CHANGE_NULL_TO_READY:
4403 if (!bclass->start (basesink))
4406 case GST_STATE_CHANGE_READY_TO_PAUSED:
4407 /* need to complete preroll before this state change completes, there
4408 * is no data flow in READY so we can safely assume we need to preroll. */
4409 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4410 GST_DEBUG_OBJECT (basesink, "READY to PAUSED");
4411 basesink->have_newsegment = FALSE;
4412 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
4413 gst_segment_init (basesink->abidata.ABI.clip_segment,
4414 GST_FORMAT_UNDEFINED);
4415 basesink->offset = 0;
4416 basesink->have_preroll = FALSE;
4417 priv->step_unlock = FALSE;
4418 basesink->need_preroll = TRUE;
4419 basesink->playing_async = TRUE;
4420 priv->current_sstart = -1;
4421 priv->current_sstop = -1;
4422 priv->eos_rtime = -1;
4424 basesink->eos = FALSE;
4425 priv->received_eos = FALSE;
4426 gst_base_sink_reset_qos (basesink);
4427 priv->commited = FALSE;
4428 priv->call_preroll = TRUE;
4429 priv->current_step.valid = FALSE;
4430 priv->pending_step.valid = FALSE;
4431 if (priv->async_enabled) {
4432 GST_DEBUG_OBJECT (basesink, "doing async state change");
4433 /* when async enabled, post async-start message and return ASYNC from
4434 * the state change function */
4435 ret = GST_STATE_CHANGE_ASYNC;
4436 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4437 gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
4439 priv->have_latency = TRUE;
4441 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4443 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
4444 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4445 if (!gst_base_sink_needs_preroll (basesink)) {
4446 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, don't need preroll");
4447 /* no preroll needed anymore now. */
4448 basesink->playing_async = FALSE;
4449 basesink->need_preroll = FALSE;
4450 if (basesink->eos) {
4451 GstMessage *message;
4453 /* need to post EOS message here */
4454 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
4455 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
4456 gst_message_set_seqnum (message, basesink->priv->seqnum);
4457 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
4459 GST_DEBUG_OBJECT (basesink, "signal preroll");
4460 GST_PAD_PREROLL_SIGNAL (basesink->sinkpad);
4463 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, we are not prerolled");
4464 basesink->need_preroll = TRUE;
4465 basesink->playing_async = TRUE;
4466 priv->call_preroll = TRUE;
4467 priv->commited = FALSE;
4468 if (priv->async_enabled) {
4469 GST_DEBUG_OBJECT (basesink, "doing async state change");
4470 ret = GST_STATE_CHANGE_ASYNC;
4471 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4472 gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
4475 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4482 GstStateChangeReturn bret;
4484 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4485 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
4486 goto activate_failed;
4489 switch (transition) {
4490 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
4491 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED");
4492 /* FIXME, make sure we cannot enter _render first */
4494 /* we need to call ::unlock before locking PREROLL_LOCK
4495 * since we lock it before going into ::render */
4497 bclass->unlock (basesink);
4499 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4500 /* now that we have the PREROLL lock, clear our unlock request */
4501 if (bclass->unlock_stop)
4502 bclass->unlock_stop (basesink);
4504 /* we need preroll again and we set the flag before unlocking the clockid
4505 * because if the clockid is unlocked before a current buffer expired, we
4506 * can use that buffer to preroll with */
4507 basesink->need_preroll = TRUE;
4509 if (basesink->clock_id) {
4510 gst_clock_id_unschedule (basesink->clock_id);
4513 /* if we don't have a preroll buffer we need to wait for a preroll and
4515 if (!gst_base_sink_needs_preroll (basesink)) {
4516 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, we are prerolled");
4517 basesink->playing_async = FALSE;
4519 if (GST_STATE_TARGET (GST_ELEMENT (basesink)) <= GST_STATE_READY) {
4520 ret = GST_STATE_CHANGE_SUCCESS;
4522 GST_DEBUG_OBJECT (basesink,
4523 "PLAYING to PAUSED, we are not prerolled");
4524 basesink->playing_async = TRUE;
4525 priv->commited = FALSE;
4526 priv->call_preroll = TRUE;
4527 if (priv->async_enabled) {
4528 GST_DEBUG_OBJECT (basesink, "doing async state change");
4529 ret = GST_STATE_CHANGE_ASYNC;
4530 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4531 gst_message_new_async_start (GST_OBJECT_CAST (basesink),
4536 GST_DEBUG_OBJECT (basesink, "rendered: %" G_GUINT64_FORMAT
4537 ", dropped: %" G_GUINT64_FORMAT, priv->rendered, priv->dropped);
4539 gst_base_sink_reset_qos (basesink);
4540 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4542 case GST_STATE_CHANGE_PAUSED_TO_READY:
4543 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4544 /* start by reseting our position state with the object lock so that the
4545 * position query gets the right idea. We do this before we post the
4546 * messages so that the message handlers pick this up. */
4547 GST_OBJECT_LOCK (basesink);
4548 basesink->have_newsegment = FALSE;
4549 priv->current_sstart = -1;
4550 priv->current_sstop = -1;
4551 priv->have_latency = FALSE;
4552 GST_OBJECT_UNLOCK (basesink);
4554 gst_base_sink_set_last_buffer (basesink, NULL);
4555 priv->call_preroll = FALSE;
4557 if (!priv->commited) {
4558 if (priv->async_enabled) {
4559 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, posting async-done");
4561 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4562 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
4563 GST_STATE_PLAYING, GST_STATE_PAUSED, GST_STATE_READY));
4565 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4566 gst_message_new_async_done (GST_OBJECT_CAST (basesink)));
4568 priv->commited = TRUE;
4570 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, don't need_preroll");
4572 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4574 case GST_STATE_CHANGE_READY_TO_NULL:
4576 if (!bclass->stop (basesink)) {
4577 GST_WARNING_OBJECT (basesink, "failed to stop");
4580 gst_base_sink_set_last_buffer (basesink, NULL);
4581 priv->call_preroll = FALSE;
4592 GST_DEBUG_OBJECT (basesink, "failed to start");
4593 return GST_STATE_CHANGE_FAILURE;
4597 GST_DEBUG_OBJECT (basesink,
4598 "element failed to change states -- activation problem?");
4599 return GST_STATE_CHANGE_FAILURE;