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 /* FIXME, some stuff in ABI.data and other in Private...
156 * Make up your mind please.
158 struct _GstBaseSinkPrivate
160 gint qos_enabled; /* ATOMIC */
161 gboolean async_enabled;
162 GstClockTimeDiff ts_offset;
163 GstClockTime render_delay;
165 /* start, stop of current buffer, stream time, used to report position */
166 GstClockTime current_sstart;
167 GstClockTime current_sstop;
169 /* start, stop and jitter of current buffer, running time */
170 GstClockTime current_rstart;
171 GstClockTime current_rstop;
172 GstClockTimeDiff current_jitter;
174 /* EOS sync time in running time */
175 GstClockTime eos_rtime;
177 /* last buffer that arrived in time, running time */
178 GstClockTime last_in_time;
179 /* when the last buffer left the sink, running time */
180 GstClockTime last_left;
182 /* running averages go here these are done on running time */
184 GstClockTime avg_duration;
187 /* these are done on system time. avg_jitter and avg_render are
188 * compared to eachother to see if the rendering time takes a
189 * huge amount of the processing, If so we are flooded with
191 GstClockTime last_left_systime;
192 GstClockTime avg_jitter;
193 GstClockTime start, stop;
194 GstClockTime avg_render;
196 /* number of rendered and dropped frames */
201 GstClockTime latency;
203 /* if we already commited the state */
206 /* when we received EOS */
207 gboolean received_eos;
209 /* when we are prerolled and able to report latency */
210 gboolean have_latency;
212 /* the last buffer we prerolled or rendered. Useful for making snapshots */
213 GstBuffer *last_buffer;
215 /* caps for pull based scheduling */
221 #define DO_RUNNING_AVG(avg,val,size) (((val) + ((size)-1) * (avg)) / (size))
223 /* generic running average, this has a neutral window size */
224 #define UPDATE_RUNNING_AVG(avg,val) DO_RUNNING_AVG(avg,val,8)
226 /* the windows for these running averages are experimentally obtained.
227 * possitive values get averaged more while negative values use a small
228 * window so we can react faster to badness. */
229 #define UPDATE_RUNNING_AVG_P(avg,val) DO_RUNNING_AVG(avg,val,16)
230 #define UPDATE_RUNNING_AVG_N(avg,val) DO_RUNNING_AVG(avg,val,4)
232 /* BaseSink properties */
234 #define DEFAULT_CAN_ACTIVATE_PULL FALSE /* fixme: enable me */
235 #define DEFAULT_CAN_ACTIVATE_PUSH TRUE
237 #define DEFAULT_PREROLL_QUEUE_LEN 0
238 #define DEFAULT_SYNC TRUE
239 #define DEFAULT_MAX_LATENESS -1
240 #define DEFAULT_QOS FALSE
241 #define DEFAULT_ASYNC TRUE
242 #define DEFAULT_TS_OFFSET 0
243 #define DEFAULT_BLOCKSIZE 4096
248 PROP_PREROLL_QUEUE_LEN,
259 static GstElementClass *parent_class = NULL;
261 static void gst_base_sink_class_init (GstBaseSinkClass * klass);
262 static void gst_base_sink_init (GstBaseSink * trans, gpointer g_class);
263 static void gst_base_sink_finalize (GObject * object);
266 gst_base_sink_get_type (void)
268 static GType base_sink_type = 0;
270 if (G_UNLIKELY (base_sink_type == 0)) {
271 static const GTypeInfo base_sink_info = {
272 sizeof (GstBaseSinkClass),
275 (GClassInitFunc) gst_base_sink_class_init,
278 sizeof (GstBaseSink),
280 (GInstanceInitFunc) gst_base_sink_init,
283 base_sink_type = g_type_register_static (GST_TYPE_ELEMENT,
284 "GstBaseSink", &base_sink_info, G_TYPE_FLAG_ABSTRACT);
286 return base_sink_type;
289 static void gst_base_sink_set_property (GObject * object, guint prop_id,
290 const GValue * value, GParamSpec * pspec);
291 static void gst_base_sink_get_property (GObject * object, guint prop_id,
292 GValue * value, GParamSpec * pspec);
294 static gboolean gst_base_sink_send_event (GstElement * element,
296 static gboolean gst_base_sink_query (GstElement * element, GstQuery * query);
298 static GstCaps *gst_base_sink_get_caps (GstBaseSink * sink);
299 static gboolean gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps);
300 static GstFlowReturn gst_base_sink_buffer_alloc (GstBaseSink * sink,
301 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
302 static void gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
303 GstClockTime * start, GstClockTime * end);
304 static gboolean gst_base_sink_set_flushing (GstBaseSink * basesink,
305 GstPad * pad, gboolean flushing);
306 static gboolean gst_base_sink_default_activate_pull (GstBaseSink * basesink,
309 static GstStateChangeReturn gst_base_sink_change_state (GstElement * element,
310 GstStateChange transition);
312 static GstFlowReturn gst_base_sink_chain (GstPad * pad, GstBuffer * buffer);
313 static void gst_base_sink_loop (GstPad * pad);
314 static gboolean gst_base_sink_pad_activate (GstPad * pad);
315 static gboolean gst_base_sink_pad_activate_push (GstPad * pad, gboolean active);
316 static gboolean gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active);
317 static gboolean gst_base_sink_event (GstPad * pad, GstEvent * event);
318 static gboolean gst_base_sink_peer_query (GstBaseSink * sink, GstQuery * query);
320 static gboolean gst_base_sink_negotiate_pull (GstBaseSink * basesink);
322 /* check if an object was too late */
323 static gboolean gst_base_sink_is_too_late (GstBaseSink * basesink,
324 GstMiniObject * obj, GstClockTime start, GstClockTime stop,
325 GstClockReturn status, GstClockTimeDiff jitter);
328 gst_base_sink_class_init (GstBaseSinkClass * klass)
330 GObjectClass *gobject_class;
331 GstElementClass *gstelement_class;
333 gobject_class = G_OBJECT_CLASS (klass);
334 gstelement_class = GST_ELEMENT_CLASS (klass);
336 GST_DEBUG_CATEGORY_INIT (gst_base_sink_debug, "basesink", 0,
339 g_type_class_add_private (klass, sizeof (GstBaseSinkPrivate));
341 parent_class = g_type_class_peek_parent (klass);
343 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_sink_finalize);
344 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_base_sink_set_property);
345 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_base_sink_get_property);
347 /* FIXME, this next value should be configured using an event from the
348 * upstream element, ie, the BUFFER_SIZE event. */
349 g_object_class_install_property (gobject_class, PROP_PREROLL_QUEUE_LEN,
350 g_param_spec_uint ("preroll-queue-len", "Preroll queue length",
351 "Number of buffers to queue during preroll", 0, G_MAXUINT,
352 DEFAULT_PREROLL_QUEUE_LEN,
353 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
355 g_object_class_install_property (gobject_class, PROP_SYNC,
356 g_param_spec_boolean ("sync", "Sync", "Sync on the clock", DEFAULT_SYNC,
357 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
359 g_object_class_install_property (gobject_class, PROP_MAX_LATENESS,
360 g_param_spec_int64 ("max-lateness", "Max Lateness",
361 "Maximum number of nanoseconds that a buffer can be late before it "
362 "is dropped (-1 unlimited)", -1, G_MAXINT64, DEFAULT_MAX_LATENESS,
363 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
365 g_object_class_install_property (gobject_class, PROP_QOS,
366 g_param_spec_boolean ("qos", "Qos",
367 "Generate Quality-of-Service events upstream", DEFAULT_QOS,
368 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
372 * If set to #TRUE, the basesink will perform asynchronous state changes.
373 * When set to #FALSE, the sink will not signal the parent when it prerolls.
374 * Use this option when dealing with sparse streams or when synchronisation is
379 g_object_class_install_property (gobject_class, PROP_ASYNC,
380 g_param_spec_boolean ("async", "Async",
381 "Go asynchronously to PAUSED", DEFAULT_ASYNC,
382 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
384 * GstBaseSink:ts-offset
386 * Controls the final synchronisation, a negative value will render the buffer
387 * earlier while a positive value delays playback. This property can be
388 * used to fix synchronisation in bad files.
392 g_object_class_install_property (gobject_class, PROP_TS_OFFSET,
393 g_param_spec_int64 ("ts-offset", "TS Offset",
394 "Timestamp offset in nanoseconds", G_MININT64, G_MAXINT64,
395 DEFAULT_TS_OFFSET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
398 * GstBaseSink:last-buffer
400 * The last buffer that arrived in the sink and was used for preroll or for
401 * rendering. This property can be used to generate thumbnails. This property
402 * can be NULL when the sink has not yet received a bufer.
406 g_object_class_install_property (gobject_class, PROP_LAST_BUFFER,
407 gst_param_spec_mini_object ("last-buffer", "Last Buffer",
408 "The last buffer received in the sink", GST_TYPE_BUFFER,
409 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
412 * GstBaseSink:blocksize
414 * The amount of bytes to pull when operating in pull mode.
418 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
419 g_param_spec_uint ("blocksize", "Block size",
420 "Size in bytes to pull per buffer (0 = default)", 0, G_MAXUINT,
421 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
423 gstelement_class->change_state =
424 GST_DEBUG_FUNCPTR (gst_base_sink_change_state);
425 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_sink_send_event);
426 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_base_sink_query);
428 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_sink_get_caps);
429 klass->set_caps = GST_DEBUG_FUNCPTR (gst_base_sink_set_caps);
430 klass->buffer_alloc = GST_DEBUG_FUNCPTR (gst_base_sink_buffer_alloc);
431 klass->get_times = GST_DEBUG_FUNCPTR (gst_base_sink_get_times);
432 klass->activate_pull =
433 GST_DEBUG_FUNCPTR (gst_base_sink_default_activate_pull);
437 gst_base_sink_pad_getcaps (GstPad * pad)
439 GstBaseSinkClass *bclass;
441 GstCaps *caps = NULL;
443 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
444 bclass = GST_BASE_SINK_GET_CLASS (bsink);
446 if (bsink->pad_mode == GST_ACTIVATE_PULL) {
447 /* if we are operating in pull mode we only accept the negotiated caps */
448 GST_OBJECT_LOCK (pad);
449 if ((caps = GST_PAD_CAPS (pad)))
451 GST_OBJECT_UNLOCK (pad);
454 if (bclass->get_caps)
455 caps = bclass->get_caps (bsink);
458 GstPadTemplate *pad_template;
461 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass),
463 if (pad_template != NULL) {
464 caps = gst_caps_ref (gst_pad_template_get_caps (pad_template));
468 gst_object_unref (bsink);
474 gst_base_sink_pad_setcaps (GstPad * pad, GstCaps * caps)
476 GstBaseSinkClass *bclass;
480 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
481 bclass = GST_BASE_SINK_GET_CLASS (bsink);
483 if (res && bclass->set_caps)
484 res = bclass->set_caps (bsink, caps);
486 gst_object_unref (bsink);
492 gst_base_sink_pad_fixate (GstPad * pad, GstCaps * caps)
494 GstBaseSinkClass *bclass;
497 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
498 bclass = GST_BASE_SINK_GET_CLASS (bsink);
501 bclass->fixate (bsink, caps);
503 gst_object_unref (bsink);
507 gst_base_sink_pad_buffer_alloc (GstPad * pad, guint64 offset, guint size,
508 GstCaps * caps, GstBuffer ** buf)
510 GstBaseSinkClass *bclass;
512 GstFlowReturn result = GST_FLOW_OK;
514 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
515 bclass = GST_BASE_SINK_GET_CLASS (bsink);
517 if (bclass->buffer_alloc)
518 result = bclass->buffer_alloc (bsink, offset, size, caps, buf);
520 *buf = NULL; /* fallback in gstpad.c will allocate generic buffer */
522 gst_object_unref (bsink);
528 gst_base_sink_init (GstBaseSink * basesink, gpointer g_class)
530 GstPadTemplate *pad_template;
531 GstBaseSinkPrivate *priv;
533 basesink->priv = priv = GST_BASE_SINK_GET_PRIVATE (basesink);
536 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
537 g_return_if_fail (pad_template != NULL);
539 basesink->sinkpad = gst_pad_new_from_template (pad_template, "sink");
541 gst_pad_set_getcaps_function (basesink->sinkpad,
542 GST_DEBUG_FUNCPTR (gst_base_sink_pad_getcaps));
543 gst_pad_set_setcaps_function (basesink->sinkpad,
544 GST_DEBUG_FUNCPTR (gst_base_sink_pad_setcaps));
545 gst_pad_set_fixatecaps_function (basesink->sinkpad,
546 GST_DEBUG_FUNCPTR (gst_base_sink_pad_fixate));
547 gst_pad_set_bufferalloc_function (basesink->sinkpad,
548 GST_DEBUG_FUNCPTR (gst_base_sink_pad_buffer_alloc));
549 gst_pad_set_activate_function (basesink->sinkpad,
550 GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate));
551 gst_pad_set_activatepush_function (basesink->sinkpad,
552 GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate_push));
553 gst_pad_set_activatepull_function (basesink->sinkpad,
554 GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate_pull));
555 gst_pad_set_event_function (basesink->sinkpad,
556 GST_DEBUG_FUNCPTR (gst_base_sink_event));
557 gst_pad_set_chain_function (basesink->sinkpad,
558 GST_DEBUG_FUNCPTR (gst_base_sink_chain));
559 gst_element_add_pad (GST_ELEMENT_CAST (basesink), basesink->sinkpad);
561 basesink->pad_mode = GST_ACTIVATE_NONE;
562 basesink->preroll_queue = g_queue_new ();
563 basesink->abidata.ABI.clip_segment = gst_segment_new ();
564 priv->have_latency = FALSE;
566 basesink->can_activate_push = DEFAULT_CAN_ACTIVATE_PUSH;
567 basesink->can_activate_pull = DEFAULT_CAN_ACTIVATE_PULL;
569 basesink->sync = DEFAULT_SYNC;
570 basesink->abidata.ABI.max_lateness = DEFAULT_MAX_LATENESS;
571 g_atomic_int_set (&priv->qos_enabled, DEFAULT_QOS);
572 priv->async_enabled = DEFAULT_ASYNC;
573 priv->ts_offset = DEFAULT_TS_OFFSET;
574 priv->render_delay = 0;
575 priv->blocksize = DEFAULT_BLOCKSIZE;
577 GST_OBJECT_FLAG_SET (basesink, GST_ELEMENT_IS_SINK);
581 gst_base_sink_finalize (GObject * object)
583 GstBaseSink *basesink;
585 basesink = GST_BASE_SINK (object);
587 g_queue_free (basesink->preroll_queue);
588 gst_segment_free (basesink->abidata.ABI.clip_segment);
590 G_OBJECT_CLASS (parent_class)->finalize (object);
594 * gst_base_sink_set_sync:
596 * @sync: the new sync value.
598 * Configures @sink to synchronize on the clock or not. When
599 * @sync is FALSE, incomming samples will be played as fast as
600 * possible. If @sync is TRUE, the timestamps of the incomming
601 * buffers will be used to schedule the exact render time of its
607 gst_base_sink_set_sync (GstBaseSink * sink, gboolean sync)
609 g_return_if_fail (GST_IS_BASE_SINK (sink));
611 GST_OBJECT_LOCK (sink);
613 GST_OBJECT_UNLOCK (sink);
617 * gst_base_sink_get_sync:
620 * Checks if @sink is currently configured to synchronize against the
623 * Returns: TRUE if the sink is configured to synchronize against the clock.
628 gst_base_sink_get_sync (GstBaseSink * sink)
632 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
634 GST_OBJECT_LOCK (sink);
636 GST_OBJECT_UNLOCK (sink);
642 * gst_base_sink_set_max_lateness:
644 * @max_lateness: the new max lateness value.
646 * Sets the new max lateness value to @max_lateness. This value is
647 * used to decide if a buffer should be dropped or not based on the
648 * buffer timestamp and the current clock time. A value of -1 means
654 gst_base_sink_set_max_lateness (GstBaseSink * sink, gint64 max_lateness)
656 g_return_if_fail (GST_IS_BASE_SINK (sink));
658 GST_OBJECT_LOCK (sink);
659 sink->abidata.ABI.max_lateness = max_lateness;
660 GST_OBJECT_UNLOCK (sink);
664 * gst_base_sink_get_max_lateness:
667 * Gets the max lateness value. See gst_base_sink_set_max_lateness for
670 * Returns: The maximum time in nanoseconds that a buffer can be late
671 * before it is dropped and not rendered. A value of -1 means an
677 gst_base_sink_get_max_lateness (GstBaseSink * sink)
681 g_return_val_if_fail (GST_IS_BASE_SINK (sink), -1);
683 GST_OBJECT_LOCK (sink);
684 res = sink->abidata.ABI.max_lateness;
685 GST_OBJECT_UNLOCK (sink);
691 * gst_base_sink_set_qos_enabled:
693 * @enabled: the new qos value.
695 * Configures @sink to send Quality-of-Service events upstream.
700 gst_base_sink_set_qos_enabled (GstBaseSink * sink, gboolean enabled)
702 g_return_if_fail (GST_IS_BASE_SINK (sink));
704 g_atomic_int_set (&sink->priv->qos_enabled, enabled);
708 * gst_base_sink_is_qos_enabled:
711 * Checks if @sink is currently configured to send Quality-of-Service events
714 * Returns: TRUE if the sink is configured to perform Quality-of-Service.
719 gst_base_sink_is_qos_enabled (GstBaseSink * sink)
723 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
725 res = g_atomic_int_get (&sink->priv->qos_enabled);
731 * gst_base_sink_set_async_enabled:
733 * @enabled: the new async value.
735 * Configures @sink to perform all state changes asynchronusly. When async is
736 * disabled, the sink will immediatly go to PAUSED instead of waiting for a
737 * preroll buffer. This feature is usefull if the sink does not synchronize
738 * against the clock or when it is dealing with sparse streams.
743 gst_base_sink_set_async_enabled (GstBaseSink * sink, gboolean enabled)
745 g_return_if_fail (GST_IS_BASE_SINK (sink));
747 GST_PAD_PREROLL_LOCK (sink->sinkpad);
748 sink->priv->async_enabled = enabled;
749 GST_LOG_OBJECT (sink, "set async enabled to %d", enabled);
750 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
754 * gst_base_sink_is_async_enabled:
757 * Checks if @sink is currently configured to perform asynchronous state
760 * Returns: TRUE if the sink is configured to perform asynchronous state
766 gst_base_sink_is_async_enabled (GstBaseSink * sink)
770 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
772 GST_PAD_PREROLL_LOCK (sink->sinkpad);
773 res = sink->priv->async_enabled;
774 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
780 * gst_base_sink_set_ts_offset:
782 * @offset: the new offset
784 * Adjust the synchronisation of @sink with @offset. A negative value will
785 * render buffers earlier than their timestamp. A positive value will delay
786 * rendering. This function can be used to fix playback of badly timestamped
792 gst_base_sink_set_ts_offset (GstBaseSink * sink, GstClockTimeDiff offset)
794 g_return_if_fail (GST_IS_BASE_SINK (sink));
796 GST_OBJECT_LOCK (sink);
797 sink->priv->ts_offset = offset;
798 GST_LOG_OBJECT (sink, "set time offset to %" G_GINT64_FORMAT, offset);
799 GST_OBJECT_UNLOCK (sink);
803 * gst_base_sink_get_ts_offset:
806 * Get the synchronisation offset of @sink.
808 * Returns: The synchronisation offset.
813 gst_base_sink_get_ts_offset (GstBaseSink * sink)
815 GstClockTimeDiff res;
817 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
819 GST_OBJECT_LOCK (sink);
820 res = sink->priv->ts_offset;
821 GST_OBJECT_UNLOCK (sink);
827 * gst_base_sink_get_last_buffer:
830 * Get the last buffer that arrived in the sink and was used for preroll or for
831 * rendering. This property can be used to generate thumbnails.
833 * The #GstCaps on the buffer can be used to determine the type of the buffer.
835 * Returns: a #GstBuffer. gst_buffer_unref() after usage. This function returns
836 * NULL when no buffer has arrived in the sink yet or when the sink is not in
842 gst_base_sink_get_last_buffer (GstBaseSink * sink)
846 g_return_val_if_fail (GST_IS_BASE_SINK (sink), NULL);
848 GST_OBJECT_LOCK (sink);
849 if ((res = sink->priv->last_buffer))
850 gst_buffer_ref (res);
851 GST_OBJECT_UNLOCK (sink);
857 gst_base_sink_set_last_buffer (GstBaseSink * sink, GstBuffer * buffer)
862 gst_buffer_ref (buffer);
864 GST_OBJECT_LOCK (sink);
865 old = sink->priv->last_buffer;
866 sink->priv->last_buffer = buffer;
867 GST_OBJECT_UNLOCK (sink);
870 gst_buffer_unref (old);
874 * gst_base_sink_get_latency:
877 * Get the currently configured latency.
879 * Returns: The configured latency.
884 gst_base_sink_get_latency (GstBaseSink * sink)
888 GST_OBJECT_LOCK (sink);
889 res = sink->priv->latency;
890 GST_OBJECT_UNLOCK (sink);
896 * gst_base_sink_query_latency:
898 * @live: if the sink is live
899 * @upstream_live: if an upstream element is live
900 * @min_latency: the min latency of the upstream elements
901 * @max_latency: the max latency of the upstream elements
903 * Query the sink for the latency parameters. The latency will be queried from
904 * the upstream elements. @live will be TRUE if @sink is configured to
905 * synchronize against the clock. @upstream_live will be TRUE if an upstream
908 * If both @live and @upstream_live are TRUE, the sink will want to compensate
909 * for the latency introduced by the upstream elements by setting the
910 * @min_latency to a strictly possitive value.
912 * This function is mostly used by subclasses.
914 * Returns: TRUE if the query succeeded.
919 gst_base_sink_query_latency (GstBaseSink * sink, gboolean * live,
920 gboolean * upstream_live, GstClockTime * min_latency,
921 GstClockTime * max_latency)
923 gboolean l, us_live, res, have_latency;
924 GstClockTime min, max, render_delay;
926 GstClockTime us_min, us_max;
928 /* we are live when we sync to the clock */
929 GST_OBJECT_LOCK (sink);
931 have_latency = sink->priv->have_latency;
932 render_delay = sink->priv->render_delay;
933 GST_OBJECT_UNLOCK (sink);
935 /* assume no latency */
941 GST_DEBUG_OBJECT (sink, "we are ready for LATENCY query");
942 /* we are ready for a latency query this is when we preroll or when we are
944 query = gst_query_new_latency ();
946 /* ask the peer for the latency */
947 if ((res = gst_base_sink_peer_query (sink, query))) {
948 /* get upstream min and max latency */
949 gst_query_parse_latency (query, &us_live, &us_min, &us_max);
952 /* upstream live, use its latency, subclasses should use these
953 * values to create the complete latency. */
958 /* we need to add the render delay if we are live */
965 gst_query_unref (query);
967 GST_DEBUG_OBJECT (sink, "we are not yet ready for LATENCY query");
971 /* not live, we tried to do the query, if it failed we return TRUE anyway */
975 GST_DEBUG_OBJECT (sink, "latency query failed but we are not live");
977 GST_DEBUG_OBJECT (sink, "latency query failed and we are live");
982 GST_DEBUG_OBJECT (sink, "latency query: live: %d, have_latency %d,"
983 " upstream: %d, min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT, l,
984 have_latency, us_live, GST_TIME_ARGS (min), GST_TIME_ARGS (max));
989 *upstream_live = us_live;
999 * gst_base_sink_set_render_delay:
1000 * @sink: a #GstBaseSink
1001 * @delay: the new delay
1003 * Set the render delay in @sink to @delay. The render delay is the time
1004 * between actual rendering of a buffer and its synchronisation time. Some
1005 * devices might delay media rendering which can be compensated for with this
1008 * After calling this function, this sink will report additional latency and
1009 * other sinks will adjust their latency to delay the rendering of their media.
1011 * This function is usually called by subclasses.
1016 gst_base_sink_set_render_delay (GstBaseSink * sink, GstClockTime delay)
1018 g_return_if_fail (GST_IS_BASE_SINK (sink));
1020 GST_OBJECT_LOCK (sink);
1021 sink->priv->render_delay = delay;
1022 GST_LOG_OBJECT (sink, "set render delay to %" GST_TIME_FORMAT,
1023 GST_TIME_ARGS (delay));
1024 GST_OBJECT_UNLOCK (sink);
1028 * gst_base_sink_get_render_delay:
1029 * @sink: a #GstBaseSink
1031 * Get the render delay of @sink. see gst_base_sink_set_render_delay() for more
1032 * information about the render delay.
1034 * Returns: the render delay of @sink.
1039 gst_base_sink_get_render_delay (GstBaseSink * sink)
1041 GstClockTimeDiff res;
1043 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1045 GST_OBJECT_LOCK (sink);
1046 res = sink->priv->render_delay;
1047 GST_OBJECT_UNLOCK (sink);
1053 * gst_base_sink_set_blocksize:
1054 * @sink: a #GstBaseSink
1055 * @blocksize: the blocksize in bytes
1057 * Set the number of bytes that the sink will pull when it is operating in pull
1063 gst_base_sink_set_blocksize (GstBaseSink * sink, guint blocksize)
1065 g_return_if_fail (GST_IS_BASE_SINK (sink));
1067 GST_OBJECT_LOCK (sink);
1068 sink->priv->blocksize = blocksize;
1069 GST_LOG_OBJECT (sink, "set blocksize to %u", blocksize);
1070 GST_OBJECT_UNLOCK (sink);
1074 * gst_base_sink_get_blocksize:
1075 * @sink: a #GstBaseSink
1077 * Get the number of bytes that the sink will pull when it is operating in pull
1080 * Returns: the number of bytes @sink will pull in pull mode.
1085 gst_base_sink_get_blocksize (GstBaseSink * sink)
1089 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1091 GST_OBJECT_LOCK (sink);
1092 res = sink->priv->blocksize;
1093 GST_OBJECT_UNLOCK (sink);
1099 gst_base_sink_set_property (GObject * object, guint prop_id,
1100 const GValue * value, GParamSpec * pspec)
1102 GstBaseSink *sink = GST_BASE_SINK (object);
1105 case PROP_PREROLL_QUEUE_LEN:
1106 /* preroll lock necessary to serialize with finish_preroll */
1107 GST_PAD_PREROLL_LOCK (sink->sinkpad);
1108 sink->preroll_queue_max_len = g_value_get_uint (value);
1109 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
1112 gst_base_sink_set_sync (sink, g_value_get_boolean (value));
1114 case PROP_MAX_LATENESS:
1115 gst_base_sink_set_max_lateness (sink, g_value_get_int64 (value));
1118 gst_base_sink_set_qos_enabled (sink, g_value_get_boolean (value));
1121 gst_base_sink_set_async_enabled (sink, g_value_get_boolean (value));
1123 case PROP_TS_OFFSET:
1124 gst_base_sink_set_ts_offset (sink, g_value_get_int64 (value));
1126 case PROP_BLOCKSIZE:
1127 gst_base_sink_set_blocksize (sink, g_value_get_uint (value));
1130 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1136 gst_base_sink_get_property (GObject * object, guint prop_id, GValue * value,
1139 GstBaseSink *sink = GST_BASE_SINK (object);
1142 case PROP_PREROLL_QUEUE_LEN:
1143 GST_PAD_PREROLL_LOCK (sink->sinkpad);
1144 g_value_set_uint (value, sink->preroll_queue_max_len);
1145 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
1148 g_value_set_boolean (value, gst_base_sink_get_sync (sink));
1150 case PROP_MAX_LATENESS:
1151 g_value_set_int64 (value, gst_base_sink_get_max_lateness (sink));
1154 g_value_set_boolean (value, gst_base_sink_is_qos_enabled (sink));
1157 g_value_set_boolean (value, gst_base_sink_is_async_enabled (sink));
1159 case PROP_TS_OFFSET:
1160 g_value_set_int64 (value, gst_base_sink_get_ts_offset (sink));
1162 case PROP_LAST_BUFFER:
1163 gst_value_take_buffer (value, gst_base_sink_get_last_buffer (sink));
1165 case PROP_BLOCKSIZE:
1166 g_value_set_uint (value, gst_base_sink_get_blocksize (sink));
1169 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1176 gst_base_sink_get_caps (GstBaseSink * sink)
1182 gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps)
1187 static GstFlowReturn
1188 gst_base_sink_buffer_alloc (GstBaseSink * sink, guint64 offset, guint size,
1189 GstCaps * caps, GstBuffer ** buf)
1195 /* with PREROLL_LOCK, STREAM_LOCK */
1197 gst_base_sink_preroll_queue_flush (GstBaseSink * basesink, GstPad * pad)
1201 GST_DEBUG_OBJECT (basesink, "flushing queue %p", basesink);
1202 while ((obj = g_queue_pop_head (basesink->preroll_queue))) {
1203 GST_DEBUG_OBJECT (basesink, "popped %p", obj);
1204 gst_mini_object_unref (obj);
1206 /* we can't have EOS anymore now */
1207 basesink->eos = FALSE;
1208 basesink->priv->received_eos = FALSE;
1209 basesink->have_preroll = FALSE;
1210 basesink->eos_queued = FALSE;
1211 basesink->preroll_queued = 0;
1212 basesink->buffers_queued = 0;
1213 basesink->events_queued = 0;
1214 /* can't report latency anymore until we preroll again */
1215 if (basesink->priv->async_enabled) {
1216 GST_OBJECT_LOCK (basesink);
1217 basesink->priv->have_latency = FALSE;
1218 GST_OBJECT_UNLOCK (basesink);
1220 /* and signal any waiters now */
1221 GST_PAD_PREROLL_SIGNAL (pad);
1224 /* with STREAM_LOCK, configures given segment with the event information. */
1226 gst_base_sink_configure_segment (GstBaseSink * basesink, GstPad * pad,
1227 GstEvent * event, GstSegment * segment)
1230 gdouble rate, arate;
1236 /* the newsegment event is needed to bring the buffer timestamps to the
1237 * stream time and to drop samples outside of the playback segment. */
1238 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1239 &start, &stop, &time);
1241 /* The segment is protected with both the STREAM_LOCK and the OBJECT_LOCK.
1242 * We protect with the OBJECT_LOCK so that we can use the values to
1243 * safely answer a POSITION query. */
1244 GST_OBJECT_LOCK (basesink);
1245 gst_segment_set_newsegment_full (segment, update, rate, arate, format, start,
1248 if (format == GST_FORMAT_TIME) {
1249 GST_DEBUG_OBJECT (basesink,
1250 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1251 "format GST_FORMAT_TIME, "
1252 "%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
1253 ", time %" GST_TIME_FORMAT ", accum %" GST_TIME_FORMAT,
1254 update, rate, arate, GST_TIME_ARGS (segment->start),
1255 GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time),
1256 GST_TIME_ARGS (segment->accum));
1258 GST_DEBUG_OBJECT (basesink,
1259 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1261 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
1262 G_GINT64_FORMAT ", accum %" G_GINT64_FORMAT, update, rate, arate,
1263 segment->format, segment->start, segment->stop, segment->time,
1266 GST_OBJECT_UNLOCK (basesink);
1269 /* with PREROLL_LOCK, STREAM_LOCK */
1271 gst_base_sink_commit_state (GstBaseSink * basesink)
1273 /* commit state and proceed to next pending state */
1274 GstState current, next, pending, post_pending;
1275 gboolean post_paused = FALSE;
1276 gboolean post_async_done = FALSE;
1277 gboolean post_playing = FALSE;
1280 /* we are certainly not playing async anymore now */
1281 basesink->playing_async = FALSE;
1283 GST_OBJECT_LOCK (basesink);
1284 current = GST_STATE (basesink);
1285 next = GST_STATE_NEXT (basesink);
1286 pending = GST_STATE_PENDING (basesink);
1287 post_pending = pending;
1288 sync = basesink->sync;
1291 case GST_STATE_PLAYING:
1293 GstBaseSinkClass *bclass;
1294 GstStateChangeReturn ret;
1296 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1298 GST_DEBUG_OBJECT (basesink, "commiting state to PLAYING");
1300 basesink->need_preroll = FALSE;
1301 post_async_done = TRUE;
1302 basesink->priv->commited = TRUE;
1303 post_playing = TRUE;
1304 /* post PAUSED too when we were READY */
1305 if (current == GST_STATE_READY) {
1309 /* make sure we notify the subclass of async playing */
1310 if (bclass->async_play) {
1311 ret = bclass->async_play (basesink);
1312 if (ret == GST_STATE_CHANGE_FAILURE)
1317 case GST_STATE_PAUSED:
1318 GST_DEBUG_OBJECT (basesink, "commiting state to PAUSED");
1320 post_async_done = TRUE;
1321 basesink->priv->commited = TRUE;
1322 post_pending = GST_STATE_VOID_PENDING;
1324 case GST_STATE_READY:
1325 case GST_STATE_NULL:
1327 case GST_STATE_VOID_PENDING:
1328 goto nothing_pending;
1333 /* we can report latency queries now */
1334 basesink->priv->have_latency = TRUE;
1336 GST_STATE (basesink) = pending;
1337 GST_STATE_NEXT (basesink) = GST_STATE_VOID_PENDING;
1338 GST_STATE_PENDING (basesink) = GST_STATE_VOID_PENDING;
1339 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_SUCCESS;
1340 GST_OBJECT_UNLOCK (basesink);
1343 GST_DEBUG_OBJECT (basesink, "posting PAUSED state change message");
1344 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1345 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1346 current, next, post_pending));
1348 if (post_async_done) {
1349 GST_DEBUG_OBJECT (basesink, "posting async-done message");
1350 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1351 gst_message_new_async_done (GST_OBJECT_CAST (basesink)));
1354 GST_DEBUG_OBJECT (basesink, "posting PLAYING state change message");
1355 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1356 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1357 next, pending, GST_STATE_VOID_PENDING));
1360 GST_STATE_BROADCAST (basesink);
1366 /* Depending on the state, set our vars. We get in this situation when the
1367 * state change function got a change to update the state vars before the
1368 * streaming thread did. This is fine but we need to make sure that we
1369 * update the need_preroll var since it was TRUE when we got here and might
1370 * become FALSE if we got to PLAYING. */
1371 GST_DEBUG_OBJECT (basesink, "nothing to commit, now in %s",
1372 gst_element_state_get_name (current));
1374 case GST_STATE_PLAYING:
1375 basesink->need_preroll = FALSE;
1377 case GST_STATE_PAUSED:
1378 basesink->need_preroll = TRUE;
1381 basesink->need_preroll = FALSE;
1382 basesink->flushing = TRUE;
1385 /* we can report latency queries now */
1386 basesink->priv->have_latency = TRUE;
1387 GST_OBJECT_UNLOCK (basesink);
1392 /* app is going to READY */
1393 GST_DEBUG_OBJECT (basesink, "stopping");
1394 basesink->need_preroll = FALSE;
1395 basesink->flushing = TRUE;
1396 GST_OBJECT_UNLOCK (basesink);
1401 GST_DEBUG_OBJECT (basesink, "async commit failed");
1402 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_FAILURE;
1403 GST_OBJECT_UNLOCK (basesink);
1409 /* with STREAM_LOCK, PREROLL_LOCK
1411 * Returns TRUE if the object needs synchronisation and takes therefore
1412 * part in prerolling.
1414 * rsstart/rsstop contain the start/stop in stream time.
1415 * rrstart/rrstop contain the start/stop in running time.
1418 gst_base_sink_get_sync_times (GstBaseSink * basesink, GstMiniObject * obj,
1419 GstClockTime * rsstart, GstClockTime * rsstop,
1420 GstClockTime * rrstart, GstClockTime * rrstop, gboolean * do_sync,
1421 GstSegment * segment)
1423 GstBaseSinkClass *bclass;
1425 GstClockTime start, stop; /* raw start/stop timestamps */
1426 gint64 cstart, cstop; /* clipped raw timestamps */
1427 gint64 rstart, rstop; /* clipped timestamps converted to running time */
1428 GstClockTime sstart, sstop; /* clipped timestamps converted to stream time */
1430 GstBaseSinkPrivate *priv;
1432 priv = basesink->priv;
1434 /* start with nothing */
1435 start = stop = sstart = sstop = rstart = rstop = -1;
1437 if (G_UNLIKELY (GST_IS_EVENT (obj))) {
1438 GstEvent *event = GST_EVENT_CAST (obj);
1440 switch (GST_EVENT_TYPE (event)) {
1441 /* EOS event needs syncing */
1444 if (basesink->segment.rate >= 0.0) {
1445 sstart = sstop = priv->current_sstop;
1447 /* we have not seen a buffer yet, use the segment values */
1448 sstart = sstop = gst_segment_to_stream_time (&basesink->segment,
1449 basesink->segment.format, basesink->segment.stop);
1452 sstart = sstop = priv->current_sstart;
1454 /* we have not seen a buffer yet, use the segment values */
1455 sstart = sstop = gst_segment_to_stream_time (&basesink->segment,
1456 basesink->segment.format, basesink->segment.start);
1460 rstart = rstop = priv->eos_rtime;
1461 *do_sync = rstart != -1;
1462 GST_DEBUG_OBJECT (basesink, "sync times for EOS %" GST_TIME_FORMAT,
1463 GST_TIME_ARGS (rstart));
1467 /* other events do not need syncing */
1468 /* FIXME, maybe NEWSEGMENT might need synchronisation
1469 * since the POSITION query depends on accumulated times and
1470 * we cannot accumulate the current segment before the previous
1477 /* else do buffer sync code */
1478 buffer = GST_BUFFER_CAST (obj);
1480 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1482 /* just get the times to see if we need syncing */
1483 if (bclass->get_times)
1484 bclass->get_times (basesink, buffer, &start, &stop);
1487 gst_base_sink_get_times (basesink, buffer, &start, &stop);
1493 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
1494 ", stop: %" GST_TIME_FORMAT ", do_sync %d", GST_TIME_ARGS (start),
1495 GST_TIME_ARGS (stop), *do_sync);
1497 /* collect segment and format for code clarity */
1498 format = segment->format;
1500 /* no timestamp clipping if we did not * get a TIME segment format */
1501 if (G_UNLIKELY (format != GST_FORMAT_TIME)) {
1504 /* do running and stream time in TIME format */
1505 format = GST_FORMAT_TIME;
1510 if (G_UNLIKELY (!gst_segment_clip (segment, GST_FORMAT_TIME,
1511 (gint64) start, (gint64) stop, &cstart, &cstop)))
1512 goto out_of_segment;
1514 if (G_UNLIKELY (start != cstart || stop != cstop)) {
1515 GST_DEBUG_OBJECT (basesink, "clipped to: start %" GST_TIME_FORMAT
1516 ", stop: %" GST_TIME_FORMAT, GST_TIME_ARGS (cstart),
1517 GST_TIME_ARGS (cstop));
1520 /* set last stop position */
1521 if (G_LIKELY (cstop != GST_CLOCK_TIME_NONE))
1522 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, cstop);
1524 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, cstart);
1527 /* this can produce wrong values if we accumulated non-TIME segments. If this happens,
1528 * upstream is behaving very badly */
1529 sstart = gst_segment_to_stream_time (segment, format, cstart);
1530 sstop = gst_segment_to_stream_time (segment, format, cstop);
1531 rstart = gst_segment_to_running_time (segment, format, cstart);
1532 rstop = gst_segment_to_running_time (segment, format, cstop);
1541 /* buffers and EOS always need syncing and preroll */
1547 /* should not happen since we clip them in the chain function already,
1548 * we return FALSE so that we don't try to sync on it. */
1549 GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
1550 (NULL), ("unexpected buffer out of segment found."));
1551 GST_LOG_OBJECT (basesink, "buffer skipped, not in segment");
1556 /* with STREAM_LOCK, PREROLL_LOCK, LOCK
1557 * adjust a timestamp with the latency and timestamp offset */
1559 gst_base_sink_adjust_time (GstBaseSink * basesink, GstClockTime time)
1561 GstClockTimeDiff ts_offset;
1563 /* don't do anything funny with invalid timestamps */
1564 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
1567 time += basesink->priv->latency;
1569 /* apply offset, be carefull for underflows */
1570 ts_offset = basesink->priv->ts_offset;
1571 if (ts_offset < 0) {
1572 ts_offset = -ts_offset;
1573 if (ts_offset < time)
1583 /* gst_base_sink_wait_clock:
1585 * @time: the running_time to be reached
1586 * @jitter: the jitter to be filled with time diff (can be NULL)
1588 * This function will block until @time is reached. It is usually called by
1589 * subclasses that use their own internal synchronisation.
1591 * If @time is not valid, no sycnhronisation is done and #GST_CLOCK_BADTIME is
1592 * returned. Likewise, if synchronisation is disabled in the element or there
1593 * is no clock, no synchronisation is done and #GST_CLOCK_BADTIME is returned.
1595 * This function should only be called with the PREROLL_LOCK held, like when
1596 * receiving an EOS event in the ::event vmethod or when receiving a buffer in
1597 * the ::render vmethod.
1599 * The @time argument should be the running_time of when this method should
1600 * return and is not adjusted with any latency or offset configured in the
1605 * Returns: #GstClockReturn
1608 gst_base_sink_wait_clock (GstBaseSink * basesink, GstClockTime time,
1609 GstClockTimeDiff * jitter)
1615 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
1618 GST_OBJECT_LOCK (basesink);
1619 if (G_UNLIKELY (!basesink->sync))
1622 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (basesink)) == NULL))
1625 /* add base_time to running_time to get the time against the clock */
1626 time += GST_ELEMENT_CAST (basesink)->base_time;
1628 id = gst_clock_new_single_shot_id (clock, time);
1629 GST_OBJECT_UNLOCK (basesink);
1631 /* A blocking wait is performed on the clock. We save the ClockID
1632 * so we can unlock the entry at any time. While we are blocking, we
1633 * release the PREROLL_LOCK so that other threads can interrupt the
1635 basesink->clock_id = id;
1636 /* release the preroll lock while waiting */
1637 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
1639 ret = gst_clock_id_wait (id, jitter);
1641 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
1642 gst_clock_id_unref (id);
1643 basesink->clock_id = NULL;
1647 /* no syncing needed */
1650 GST_DEBUG_OBJECT (basesink, "time not valid, no sync needed");
1651 return GST_CLOCK_BADTIME;
1655 GST_DEBUG_OBJECT (basesink, "sync disabled");
1656 GST_OBJECT_UNLOCK (basesink);
1657 return GST_CLOCK_BADTIME;
1661 GST_DEBUG_OBJECT (basesink, "no clock, can't sync");
1662 GST_OBJECT_UNLOCK (basesink);
1663 return GST_CLOCK_BADTIME;
1668 * gst_base_sink_wait_preroll:
1671 * If the #GstBaseSinkClass::render method performs its own synchronisation against
1672 * the clock it must unblock when going from PLAYING to the PAUSED state and call
1673 * this method before continuing to render the remaining data.
1675 * This function will block until a state change to PLAYING happens (in which
1676 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
1677 * to a state change to READY or a FLUSH event (in which case this function
1678 * returns #GST_FLOW_WRONG_STATE).
1680 * This function should only be called with the PREROLL_LOCK held, like in the
1685 * Returns: #GST_FLOW_OK if the preroll completed and processing can
1686 * continue. Any other return value should be returned from the render vmethod.
1689 gst_base_sink_wait_preroll (GstBaseSink * sink)
1691 sink->have_preroll = TRUE;
1692 GST_DEBUG_OBJECT (sink, "waiting in preroll for flush or PLAYING");
1693 /* block until the state changes, or we get a flush, or something */
1694 GST_PAD_PREROLL_WAIT (sink->sinkpad);
1695 sink->have_preroll = FALSE;
1696 if (G_UNLIKELY (sink->flushing))
1698 GST_DEBUG_OBJECT (sink, "continue after preroll");
1705 GST_DEBUG_OBJECT (sink, "preroll interrupted");
1706 return GST_FLOW_WRONG_STATE;
1711 * gst_base_sink_do_preroll:
1713 * @obj: the object that caused the preroll
1715 * If the @sink spawns its own thread for pulling buffers from upstream it
1716 * should call this method after it has pulled a buffer. If the element needed
1717 * to preroll, this function will perform the preroll and will then block
1718 * until the element state is changed.
1720 * This function should be called with the PREROLL_LOCK held.
1724 * Returns: #GST_FLOW_OK if the preroll completed and processing can
1725 * continue. Any other return value should be returned from the render vmethod.
1728 gst_base_sink_do_preroll (GstBaseSink * sink, GstMiniObject * obj)
1732 while (G_UNLIKELY (sink->need_preroll)) {
1733 GST_DEBUG_OBJECT (sink, "prerolling object %p", obj);
1735 if (G_LIKELY (sink->playing_async)) {
1737 if (G_UNLIKELY (!gst_base_sink_commit_state (sink)))
1741 /* need to recheck here because the commit state could have
1742 * made us not need the preroll anymore */
1743 if (G_LIKELY (sink->need_preroll)) {
1744 /* block until the state changes, or we get a flush, or something */
1745 ret = gst_base_sink_wait_preroll (sink);
1746 if (ret != GST_FLOW_OK)
1755 GST_DEBUG_OBJECT (sink, "we are flushing");
1760 GST_DEBUG_OBJECT (sink, "stopping while commiting state");
1761 return GST_FLOW_WRONG_STATE;
1766 * gst_base_sink_wait_eos:
1768 * @time: the running_time to be reached
1769 * @jitter: the jitter to be filled with time diff (can be NULL)
1771 * This function will block until @time is reached. It is usually called by
1772 * subclasses that use their own internal synchronisation but want to let the
1773 * EOS be handled by the base class.
1775 * This function should only be called with the PREROLL_LOCK held, like when
1776 * receiving an EOS event in the ::event vmethod.
1778 * The @time argument should be the running_time of when the EOS should happen
1779 * and will be adjusted with any latency and offset configured in the sink.
1783 * Returns: #GstFlowReturn
1786 gst_base_sink_wait_eos (GstBaseSink * sink, GstClockTime time,
1787 GstClockTimeDiff * jitter)
1789 GstClockReturn status;
1795 GST_DEBUG_OBJECT (sink, "checking preroll");
1797 /* first wait for the playing state before we can continue */
1798 if (G_UNLIKELY (sink->need_preroll)) {
1799 ret = gst_base_sink_wait_preroll (sink);
1800 if (ret != GST_FLOW_OK)
1804 /* preroll done, we can sync since we are in PLAYING now. */
1805 GST_DEBUG_OBJECT (sink, "possibly waiting for clock to reach %"
1806 GST_TIME_FORMAT, GST_TIME_ARGS (time));
1808 /* compensate for latency and ts_offset. We don't adjust for render delay
1809 * because we don't interact with the device on EOS normally. */
1810 stime = gst_base_sink_adjust_time (sink, time);
1812 /* wait for the clock, this can be interrupted because we got shut down or
1814 status = gst_base_sink_wait_clock (sink, stime, jitter);
1816 GST_DEBUG_OBJECT (sink, "clock returned %d", status);
1818 /* invalid time, no clock or sync disabled, just continue then */
1819 if (status == GST_CLOCK_BADTIME)
1822 /* waiting could have been interrupted and we can be flushing now */
1823 if (G_UNLIKELY (sink->flushing))
1826 /* retry if we got unscheduled, which means we did not reach the timeout
1827 * yet. if some other error occures, we continue. */
1828 } while (status == GST_CLOCK_UNSCHEDULED);
1830 GST_DEBUG_OBJECT (sink, "end of stream");
1837 GST_DEBUG_OBJECT (sink, "we are flushing");
1838 return GST_FLOW_WRONG_STATE;
1842 /* with STREAM_LOCK, PREROLL_LOCK
1844 * Make sure we are in PLAYING and synchronize an object to the clock.
1846 * If we need preroll, we are not in PLAYING. We try to commit the state
1847 * if needed and then block if we still are not PLAYING.
1849 * We start waiting on the clock in PLAYING. If we got interrupted, we
1850 * immediatly try to re-preroll.
1852 * Some objects do not need synchronisation (most events) and so this function
1853 * immediatly returns GST_FLOW_OK.
1855 * for objects that arrive later than max-lateness to be synchronized to the
1856 * clock have the @late boolean set to TRUE.
1858 * This function keeps a running average of the jitter (the diff between the
1859 * clock time and the requested sync time). The jitter is negative for
1860 * objects that arrive in time and positive for late buffers.
1862 * does not take ownership of obj.
1864 static GstFlowReturn
1865 gst_base_sink_do_sync (GstBaseSink * basesink, GstPad * pad,
1866 GstMiniObject * obj, gboolean * late)
1868 GstClockTimeDiff jitter;
1870 GstClockReturn status = GST_CLOCK_OK;
1871 GstClockTime rstart, rstop, sstart, sstop, stime;
1873 GstBaseSinkPrivate *priv;
1875 priv = basesink->priv;
1877 sstart = sstop = rstart = rstop = -1;
1880 priv->current_rstart = -1;
1882 /* get timing information for this object against the render segment */
1883 syncable = gst_base_sink_get_sync_times (basesink, obj,
1884 &sstart, &sstop, &rstart, &rstop, &do_sync, &basesink->segment);
1886 /* a syncable object needs to participate in preroll and
1887 * clocking. All buffers and EOS are syncable. */
1888 if (G_UNLIKELY (!syncable))
1891 /* store timing info for current object */
1892 priv->current_rstart = rstart;
1893 priv->current_rstop = (rstop != -1 ? rstop : rstart);
1894 /* save sync time for eos when the previous object needed sync */
1895 priv->eos_rtime = (do_sync ? priv->current_rstop : -1);
1898 /* first do preroll, this makes sure we commit our state
1899 * to PAUSED and can continue to PLAYING. We cannot perform
1900 * any clock sync in PAUSED because there is no clock.
1902 while (G_UNLIKELY (basesink->need_preroll)) {
1903 GST_DEBUG_OBJECT (basesink, "prerolling object %p", obj);
1905 if (G_LIKELY (basesink->playing_async)) {
1907 if (G_UNLIKELY (!gst_base_sink_commit_state (basesink)))
1911 /* need to recheck here because the commit state could have
1912 * made us not need the preroll anymore */
1913 if (G_LIKELY (basesink->need_preroll)) {
1914 /* block until the state changes, or we get a flush, or something */
1915 if (gst_base_sink_wait_preroll (basesink) != GST_FLOW_OK)
1920 /* After rendering we store the position of the last buffer so that we can use
1921 * it to report the position. We need to take the lock here. */
1922 GST_OBJECT_LOCK (basesink);
1923 priv->current_sstart = sstart;
1924 priv->current_sstop = (sstop != -1 ? sstop : sstart);
1925 GST_OBJECT_UNLOCK (basesink);
1930 /* adjust for latency */
1931 stime = gst_base_sink_adjust_time (basesink, rstart);
1933 /* adjust for render-delay, avoid underflows */
1935 if (stime > priv->render_delay)
1936 stime -= priv->render_delay;
1941 /* preroll done, we can sync since we are in PLAYING now. */
1942 GST_DEBUG_OBJECT (basesink, "possibly waiting for clock to reach %"
1943 GST_TIME_FORMAT ", adjusted %" GST_TIME_FORMAT,
1944 GST_TIME_ARGS (rstart), GST_TIME_ARGS (stime));
1946 /* This function will return immediatly if start == -1, no clock
1947 * or sync is disabled with GST_CLOCK_BADTIME. */
1948 status = gst_base_sink_wait_clock (basesink, stime, &jitter);
1950 GST_DEBUG_OBJECT (basesink, "clock returned %d", status);
1952 /* invalid time, no clock or sync disabled, just render */
1953 if (status == GST_CLOCK_BADTIME)
1956 /* waiting could have been interrupted and we can be flushing now */
1957 if (G_UNLIKELY (basesink->flushing))
1960 /* check for unlocked by a state change, we are not flushing so
1961 * we can try to preroll on the current buffer. */
1962 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
1963 GST_DEBUG_OBJECT (basesink, "unscheduled, waiting some more");
1967 /* successful syncing done, record observation */
1968 priv->current_jitter = jitter;
1970 /* check if the object should be dropped */
1971 *late = gst_base_sink_is_too_late (basesink, obj, rstart, rstop,
1980 GST_DEBUG_OBJECT (basesink, "non syncable object %p", obj);
1985 GST_DEBUG_OBJECT (basesink, "we are flushing");
1986 return GST_FLOW_WRONG_STATE;
1990 GST_DEBUG_OBJECT (basesink, "stopping while commiting state");
1991 return GST_FLOW_WRONG_STATE;
1996 gst_base_sink_send_qos (GstBaseSink * basesink,
1997 gdouble proportion, GstClockTime time, GstClockTimeDiff diff)
2002 /* generate Quality-of-Service event */
2003 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2004 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2005 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (time));
2007 event = gst_event_new_qos (proportion, diff, time);
2010 res = gst_pad_push_event (basesink->sinkpad, event);
2016 gst_base_sink_perform_qos (GstBaseSink * sink, gboolean dropped)
2018 GstBaseSinkPrivate *priv;
2019 GstClockTime start, stop;
2020 GstClockTimeDiff jitter;
2021 GstClockTime pt, entered, left;
2022 GstClockTime duration;
2027 start = priv->current_rstart;
2029 /* if Quality-of-Service disabled, do nothing */
2030 if (!g_atomic_int_get (&priv->qos_enabled) || start == -1)
2033 stop = priv->current_rstop;
2034 jitter = priv->current_jitter;
2037 /* this is the time the buffer entered the sink */
2038 if (start < -jitter)
2041 entered = start + jitter;
2044 /* this is the time the buffer entered the sink */
2045 entered = start + jitter;
2046 /* this is the time the buffer left the sink */
2047 left = start + jitter;
2050 /* calculate duration of the buffer */
2052 duration = stop - start;
2056 /* if we have the time when the last buffer left us, calculate
2057 * processing time */
2058 if (priv->last_left != -1) {
2059 if (entered > priv->last_left) {
2060 pt = entered - priv->last_left;
2068 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "start: %" GST_TIME_FORMAT
2069 ", entered %" GST_TIME_FORMAT ", left %" GST_TIME_FORMAT ", pt: %"
2070 GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT ",jitter %"
2071 G_GINT64_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (entered),
2072 GST_TIME_ARGS (left), GST_TIME_ARGS (pt), GST_TIME_ARGS (duration),
2075 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "avg_duration: %" GST_TIME_FORMAT
2076 ", avg_pt: %" GST_TIME_FORMAT ", avg_rate: %g",
2077 GST_TIME_ARGS (priv->avg_duration), GST_TIME_ARGS (priv->avg_pt),
2080 /* collect running averages. for first observations, we copy the
2082 if (priv->avg_duration == -1)
2083 priv->avg_duration = duration;
2085 priv->avg_duration = UPDATE_RUNNING_AVG (priv->avg_duration, duration);
2087 if (priv->avg_pt == -1)
2090 priv->avg_pt = UPDATE_RUNNING_AVG (priv->avg_pt, pt);
2092 if (priv->avg_duration != 0)
2094 gst_guint64_to_gdouble (priv->avg_pt) /
2095 gst_guint64_to_gdouble (priv->avg_duration);
2099 if (priv->last_left != -1) {
2100 if (dropped || priv->avg_rate < 0.0) {
2101 priv->avg_rate = rate;
2104 priv->avg_rate = UPDATE_RUNNING_AVG_N (priv->avg_rate, rate);
2106 priv->avg_rate = UPDATE_RUNNING_AVG_P (priv->avg_rate, rate);
2110 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink,
2111 "updated: avg_duration: %" GST_TIME_FORMAT ", avg_pt: %" GST_TIME_FORMAT
2112 ", avg_rate: %g", GST_TIME_ARGS (priv->avg_duration),
2113 GST_TIME_ARGS (priv->avg_pt), priv->avg_rate);
2116 if (priv->avg_rate >= 0.0) {
2117 /* if we have a valid rate, start sending QoS messages */
2118 if (priv->current_jitter < 0) {
2119 /* make sure we never go below 0 when adding the jitter to the
2121 if (priv->current_rstart < -priv->current_jitter)
2122 priv->current_jitter = -priv->current_rstart;
2124 gst_base_sink_send_qos (sink, priv->avg_rate, priv->current_rstart,
2125 priv->current_jitter);
2128 /* record when this buffer will leave us */
2129 priv->last_left = left;
2132 /* reset all qos measuring */
2134 gst_base_sink_reset_qos (GstBaseSink * sink)
2136 GstBaseSinkPrivate *priv;
2140 priv->last_in_time = -1;
2141 priv->last_left = -1;
2142 priv->avg_duration = -1;
2144 priv->avg_rate = -1.0;
2145 priv->avg_render = -1;
2151 /* Checks if the object was scheduled too late.
2153 * start/stop contain the raw timestamp start and stop values
2156 * status and jitter contain the return values from the clock wait.
2158 * returns TRUE if the buffer was too late.
2161 gst_base_sink_is_too_late (GstBaseSink * basesink, GstMiniObject * obj,
2162 GstClockTime start, GstClockTime stop,
2163 GstClockReturn status, GstClockTimeDiff jitter)
2166 gint64 max_lateness;
2167 GstBaseSinkPrivate *priv;
2169 priv = basesink->priv;
2173 /* only for objects that were too late */
2174 if (G_LIKELY (status != GST_CLOCK_EARLY))
2177 max_lateness = basesink->abidata.ABI.max_lateness;
2179 /* check if frame dropping is enabled */
2180 if (max_lateness == -1)
2183 /* only check for buffers */
2184 if (G_UNLIKELY (!GST_IS_BUFFER (obj)))
2187 /* can't do check if we don't have a timestamp */
2188 if (G_UNLIKELY (start == -1))
2191 /* we can add a valid stop time */
2193 max_lateness += stop;
2195 max_lateness += start;
2197 /* if the jitter bigger than duration and lateness we are too late */
2198 if ((late = start + jitter > max_lateness)) {
2199 GST_DEBUG_OBJECT (basesink, "buffer is too late %" GST_TIME_FORMAT
2200 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (start + jitter),
2201 GST_TIME_ARGS (max_lateness));
2202 /* !!emergency!!, if we did not receive anything valid for more than a
2203 * second, render it anyway so the user sees something */
2204 if (priv->last_in_time && start - priv->last_in_time > GST_SECOND) {
2206 GST_DEBUG_OBJECT (basesink,
2207 "**emergency** last buffer at %" GST_TIME_FORMAT " > GST_SECOND",
2208 GST_TIME_ARGS (priv->last_in_time));
2214 priv->last_in_time = start;
2221 GST_DEBUG_OBJECT (basesink, "object was scheduled in time");
2226 GST_DEBUG_OBJECT (basesink, "frame dropping disabled");
2231 GST_DEBUG_OBJECT (basesink, "object is not a buffer");
2236 GST_DEBUG_OBJECT (basesink, "buffer has no timestamp");
2241 /* called before and after calling the render vmethod. It keeps track of how
2242 * much time was spent in the render method and is used to check if we are
2245 gst_base_sink_do_render_stats (GstBaseSink * basesink, gboolean start)
2247 GstBaseSinkPrivate *priv;
2249 priv = basesink->priv;
2252 priv->start = gst_util_get_timestamp ();
2254 GstClockTime elapsed;
2256 priv->stop = gst_util_get_timestamp ();
2258 elapsed = GST_CLOCK_DIFF (priv->start, priv->stop);
2260 if (priv->avg_render == -1)
2261 priv->avg_render = elapsed;
2263 priv->avg_render = UPDATE_RUNNING_AVG (priv->avg_render, elapsed);
2265 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2266 "avg_render: %" GST_TIME_FORMAT, GST_TIME_ARGS (priv->avg_render));
2270 /* with STREAM_LOCK, PREROLL_LOCK,
2272 * Synchronize the object on the clock and then render it.
2274 * takes ownership of obj.
2276 static GstFlowReturn
2277 gst_base_sink_render_object (GstBaseSink * basesink, GstPad * pad,
2278 GstMiniObject * obj)
2280 GstFlowReturn ret = GST_FLOW_OK;
2281 GstBaseSinkClass *bclass;
2282 gboolean late = FALSE;
2283 GstBaseSinkPrivate *priv;
2285 priv = basesink->priv;
2287 /* synchronize this object, non syncable objects return OK
2289 ret = gst_base_sink_do_sync (basesink, pad, obj, &late);
2290 if (G_UNLIKELY (ret != GST_FLOW_OK))
2293 /* and now render, event or buffer. */
2294 if (G_LIKELY (GST_IS_BUFFER (obj))) {
2297 /* drop late buffers unconditionally, let's hope it's unlikely */
2298 if (G_UNLIKELY (late))
2301 buf = GST_BUFFER_CAST (obj);
2303 gst_base_sink_set_last_buffer (basesink, buf);
2305 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2307 if (G_LIKELY (bclass->render)) {
2310 /* read once, to get same value before and after */
2311 do_qos = g_atomic_int_get (&priv->qos_enabled);
2313 GST_DEBUG_OBJECT (basesink, "rendering buffer %p", obj);
2315 /* record rendering time for QoS and stats */
2317 gst_base_sink_do_render_stats (basesink, TRUE);
2319 ret = bclass->render (basesink, buf);
2324 gst_base_sink_do_render_stats (basesink, FALSE);
2327 GstEvent *event = GST_EVENT_CAST (obj);
2328 gboolean event_res = TRUE;
2331 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2333 type = GST_EVENT_TYPE (event);
2335 GST_DEBUG_OBJECT (basesink, "rendering event %p, type %s", obj,
2336 gst_event_type_get_name (type));
2339 event_res = bclass->event (basesink, event);
2341 /* when we get here we could be flushing again when the event handler calls
2342 * _wait_eos(). We have to ignore this object in that case. */
2343 if (G_UNLIKELY (basesink->flushing))
2346 if (G_LIKELY (event_res)) {
2349 /* the EOS event is completely handled so we mark
2350 * ourselves as being in the EOS state. eos is also
2351 * protected by the object lock so we can read it when
2352 * answering the POSITION query. */
2353 GST_OBJECT_LOCK (basesink);
2354 basesink->eos = TRUE;
2355 GST_OBJECT_UNLOCK (basesink);
2356 /* ok, now we can post the message */
2357 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
2358 gst_element_post_message (GST_ELEMENT_CAST (basesink),
2359 gst_message_new_eos (GST_OBJECT_CAST (basesink)));
2361 case GST_EVENT_NEWSEGMENT:
2362 /* configure the segment */
2363 gst_base_sink_configure_segment (basesink, pad, event,
2364 &basesink->segment);
2373 gst_base_sink_perform_qos (basesink, late);
2375 GST_DEBUG_OBJECT (basesink, "object unref after render %p", obj);
2376 gst_mini_object_unref (obj);
2383 GST_DEBUG_OBJECT (basesink, "do_sync returned %s", gst_flow_get_name (ret));
2389 GST_DEBUG_OBJECT (basesink, "buffer late, dropping");
2394 GST_DEBUG_OBJECT (basesink, "we are flushing, ignore object");
2395 gst_mini_object_unref (obj);
2396 return GST_FLOW_WRONG_STATE;
2400 /* with STREAM_LOCK, PREROLL_LOCK
2402 * Perform preroll on the given object. For buffers this means
2403 * calling the preroll subclass method.
2404 * If that succeeds, the state will be commited.
2406 * function does not take ownership of obj.
2408 static GstFlowReturn
2409 gst_base_sink_preroll_object (GstBaseSink * basesink, GstPad * pad,
2410 GstMiniObject * obj)
2414 GST_DEBUG_OBJECT (basesink, "do preroll %p", obj);
2416 /* if it's a buffer, we need to call the preroll method */
2417 if (G_LIKELY (GST_IS_BUFFER (obj))) {
2418 GstBaseSinkClass *bclass;
2420 GstClockTime timestamp;
2422 buf = GST_BUFFER_CAST (obj);
2423 timestamp = GST_BUFFER_TIMESTAMP (buf);
2425 GST_DEBUG_OBJECT (basesink, "preroll buffer %" GST_TIME_FORMAT,
2426 GST_TIME_ARGS (timestamp));
2428 gst_base_sink_set_last_buffer (basesink, buf);
2430 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2431 if (bclass->preroll)
2432 if ((ret = bclass->preroll (basesink, buf)) != GST_FLOW_OK)
2433 goto preroll_failed;
2437 if (G_LIKELY (basesink->playing_async)) {
2438 if (G_UNLIKELY (!gst_base_sink_commit_state (basesink)))
2447 GST_DEBUG_OBJECT (basesink, "preroll failed, abort state");
2448 gst_element_abort_state (GST_ELEMENT_CAST (basesink));
2453 GST_DEBUG_OBJECT (basesink, "stopping while commiting state");
2454 return GST_FLOW_WRONG_STATE;
2458 /* with STREAM_LOCK, PREROLL_LOCK
2460 * Queue an object for rendering.
2461 * The first prerollable object queued will complete the preroll. If the
2462 * preroll queue if filled, we render all the objects in the queue.
2464 * This function takes ownership of the object.
2466 static GstFlowReturn
2467 gst_base_sink_queue_object_unlocked (GstBaseSink * basesink, GstPad * pad,
2468 GstMiniObject * obj, gboolean prerollable)
2470 GstFlowReturn ret = GST_FLOW_OK;
2474 if (G_UNLIKELY (basesink->need_preroll)) {
2475 if (G_LIKELY (prerollable))
2476 basesink->preroll_queued++;
2478 length = basesink->preroll_queued;
2480 GST_DEBUG_OBJECT (basesink, "now %d prerolled items", length);
2482 /* first prerollable item needs to finish the preroll */
2484 ret = gst_base_sink_preroll_object (basesink, pad, obj);
2485 if (G_UNLIKELY (ret != GST_FLOW_OK))
2486 goto preroll_failed;
2488 /* need to recheck if we need preroll, commmit state during preroll
2489 * could have made us not need more preroll. */
2490 if (G_UNLIKELY (basesink->need_preroll)) {
2491 /* see if we can render now, if we can't add the object to the preroll
2493 if (G_UNLIKELY (length <= basesink->preroll_queue_max_len))
2498 /* we can start rendering (or blocking) the queued object
2500 q = basesink->preroll_queue;
2501 while (G_UNLIKELY (!g_queue_is_empty (q))) {
2504 o = g_queue_pop_head (q);
2505 GST_DEBUG_OBJECT (basesink, "rendering queued object %p", o);
2507 /* do something with the return value */
2508 ret = gst_base_sink_render_object (basesink, pad, o);
2509 if (ret != GST_FLOW_OK)
2510 goto dequeue_failed;
2513 /* now render the object */
2514 ret = gst_base_sink_render_object (basesink, pad, obj);
2515 basesink->preroll_queued = 0;
2522 GST_DEBUG_OBJECT (basesink, "preroll failed, reason %s",
2523 gst_flow_get_name (ret));
2524 gst_mini_object_unref (obj);
2529 /* add object to the queue and return */
2530 GST_DEBUG_OBJECT (basesink, "need more preroll data %d <= %d",
2531 length, basesink->preroll_queue_max_len);
2532 g_queue_push_tail (basesink->preroll_queue, obj);
2537 GST_DEBUG_OBJECT (basesink, "rendering queued objects failed, reason %s",
2538 gst_flow_get_name (ret));
2539 gst_mini_object_unref (obj);
2546 * This function grabs the PREROLL_LOCK and adds the object to
2549 * This function takes ownership of obj.
2551 static GstFlowReturn
2552 gst_base_sink_queue_object (GstBaseSink * basesink, GstPad * pad,
2553 GstMiniObject * obj, gboolean prerollable)
2557 GST_PAD_PREROLL_LOCK (pad);
2558 if (G_UNLIKELY (basesink->flushing))
2561 if (G_UNLIKELY (basesink->priv->received_eos))
2564 ret = gst_base_sink_queue_object_unlocked (basesink, pad, obj, prerollable);
2565 GST_PAD_PREROLL_UNLOCK (pad);
2572 GST_DEBUG_OBJECT (basesink, "sink is flushing");
2573 GST_PAD_PREROLL_UNLOCK (pad);
2574 gst_mini_object_unref (obj);
2575 return GST_FLOW_WRONG_STATE;
2579 GST_DEBUG_OBJECT (basesink,
2580 "we are EOS, dropping object, return UNEXPECTED");
2581 GST_PAD_PREROLL_UNLOCK (pad);
2582 gst_mini_object_unref (obj);
2583 return GST_FLOW_UNEXPECTED;
2588 gst_base_sink_flush_start (GstBaseSink * basesink, GstPad * pad)
2590 /* make sure we are not blocked on the clock also clear any pending
2592 gst_base_sink_set_flushing (basesink, pad, TRUE);
2594 /* we grab the stream lock but that is not needed since setting the
2595 * sink to flushing would make sure no state commit is being done
2597 GST_PAD_STREAM_LOCK (pad);
2598 gst_base_sink_reset_qos (basesink);
2599 if (basesink->priv->async_enabled) {
2600 /* and we need to commit our state again on the next
2601 * prerolled buffer */
2602 basesink->playing_async = TRUE;
2603 gst_element_lost_state (GST_ELEMENT_CAST (basesink));
2605 basesink->priv->have_latency = TRUE;
2606 basesink->need_preroll = FALSE;
2608 gst_base_sink_set_last_buffer (basesink, NULL);
2609 GST_PAD_STREAM_UNLOCK (pad);
2613 gst_base_sink_flush_stop (GstBaseSink * basesink, GstPad * pad)
2615 /* unset flushing so we can accept new data, this also flushes out any EOS
2617 gst_base_sink_set_flushing (basesink, pad, FALSE);
2619 /* for position reporting */
2620 GST_OBJECT_LOCK (basesink);
2621 basesink->priv->current_sstart = -1;
2622 basesink->priv->current_sstop = -1;
2623 basesink->priv->eos_rtime = -1;
2624 if (basesink->pad_mode == GST_ACTIVATE_PUSH) {
2625 /* we need new segment info after the flush. */
2626 basesink->have_newsegment = FALSE;
2627 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
2628 gst_segment_init (basesink->abidata.ABI.clip_segment, GST_FORMAT_UNDEFINED);
2630 GST_OBJECT_UNLOCK (basesink);
2634 gst_base_sink_event (GstPad * pad, GstEvent * event)
2636 GstBaseSink *basesink;
2637 gboolean result = TRUE;
2638 GstBaseSinkClass *bclass;
2640 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
2642 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2644 GST_DEBUG_OBJECT (basesink, "event %p (%s)", event,
2645 GST_EVENT_TYPE_NAME (event));
2647 switch (GST_EVENT_TYPE (event)) {
2652 GST_PAD_PREROLL_LOCK (pad);
2653 if (G_UNLIKELY (basesink->flushing))
2656 if (G_UNLIKELY (basesink->priv->received_eos)) {
2657 /* we can't accept anything when we are EOS */
2659 gst_event_unref (event);
2661 /* we set the received EOS flag here so that we can use it when testing if
2662 * we are prerolled and to refure more buffers. */
2663 basesink->priv->received_eos = TRUE;
2665 /* EOS is a prerollable object, we call the unlocked version because it
2666 * does not check the received_eos flag. */
2667 ret = gst_base_sink_queue_object_unlocked (basesink, pad,
2668 GST_MINI_OBJECT_CAST (event), TRUE);
2669 if (G_UNLIKELY (ret != GST_FLOW_OK))
2672 GST_PAD_PREROLL_UNLOCK (pad);
2675 case GST_EVENT_NEWSEGMENT:
2679 GST_DEBUG_OBJECT (basesink, "newsegment %p", event);
2681 GST_PAD_PREROLL_LOCK (pad);
2682 if (G_UNLIKELY (basesink->flushing))
2685 if (G_UNLIKELY (basesink->priv->received_eos)) {
2686 /* we can't accept anything when we are EOS */
2688 gst_event_unref (event);
2690 /* the new segment is a non prerollable item and does not block anything,
2691 * we need to configure the current clipping segment and insert the event
2692 * in the queue to serialize it with the buffers for rendering. */
2693 gst_base_sink_configure_segment (basesink, pad, event,
2694 basesink->abidata.ABI.clip_segment);
2697 gst_base_sink_queue_object_unlocked (basesink, pad,
2698 GST_MINI_OBJECT_CAST (event), FALSE);
2699 if (G_UNLIKELY (ret != GST_FLOW_OK))
2702 GST_OBJECT_LOCK (basesink);
2703 basesink->have_newsegment = TRUE;
2704 GST_OBJECT_UNLOCK (basesink);
2707 GST_PAD_PREROLL_UNLOCK (pad);
2710 case GST_EVENT_FLUSH_START:
2712 bclass->event (basesink, event);
2714 GST_DEBUG_OBJECT (basesink, "flush-start %p", event);
2716 gst_base_sink_flush_start (basesink, pad);
2718 gst_event_unref (event);
2720 case GST_EVENT_FLUSH_STOP:
2722 bclass->event (basesink, event);
2724 GST_DEBUG_OBJECT (basesink, "flush-stop %p", event);
2726 gst_base_sink_flush_stop (basesink, pad);
2728 gst_event_unref (event);
2731 /* other events are sent to queue or subclass depending on if they
2732 * are serialized. */
2733 if (GST_EVENT_IS_SERIALIZED (event)) {
2734 gst_base_sink_queue_object (basesink, pad,
2735 GST_MINI_OBJECT_CAST (event), FALSE);
2738 bclass->event (basesink, event);
2739 gst_event_unref (event);
2744 gst_object_unref (basesink);
2751 GST_DEBUG_OBJECT (basesink, "we are flushing");
2752 GST_PAD_PREROLL_UNLOCK (pad);
2754 gst_event_unref (event);
2759 /* default implementation to calculate the start and end
2760 * timestamps on a buffer, subclasses can override
2763 gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
2764 GstClockTime * start, GstClockTime * end)
2766 GstClockTime timestamp, duration;
2768 timestamp = GST_BUFFER_TIMESTAMP (buffer);
2769 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
2771 /* get duration to calculate end time */
2772 duration = GST_BUFFER_DURATION (buffer);
2773 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2774 *end = timestamp + duration;
2780 /* must be called with PREROLL_LOCK */
2782 gst_base_sink_needs_preroll (GstBaseSink * basesink)
2784 gboolean is_prerolled, res;
2786 /* we have 2 cases where the PREROLL_LOCK is released:
2787 * 1) we are blocking in the PREROLL_LOCK and thus are prerolled.
2788 * 2) we are syncing on the clock
2790 is_prerolled = basesink->have_preroll || basesink->priv->received_eos;
2791 res = !is_prerolled;
2793 GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d => needs preroll: %d",
2794 basesink->have_preroll, basesink->priv->received_eos, res);
2799 /* with STREAM_LOCK, PREROLL_LOCK
2801 * Takes a buffer and compare the timestamps with the last segment.
2802 * If the buffer falls outside of the segment boundaries, drop it.
2803 * Else queue the buffer for preroll and rendering.
2805 * This function takes ownership of the buffer.
2807 static GstFlowReturn
2808 gst_base_sink_chain_unlocked (GstBaseSink * basesink, GstPad * pad,
2811 GstBaseSinkClass *bclass;
2812 GstFlowReturn result;
2813 GstClockTime start = GST_CLOCK_TIME_NONE, end = GST_CLOCK_TIME_NONE;
2814 GstSegment *clip_segment;
2816 if (G_UNLIKELY (basesink->flushing))
2819 if (G_UNLIKELY (basesink->priv->received_eos))
2822 /* for code clarity */
2823 clip_segment = basesink->abidata.ABI.clip_segment;
2825 if (G_UNLIKELY (!basesink->have_newsegment)) {
2828 sync = gst_base_sink_get_sync (basesink);
2830 GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
2831 (_("Internal data flow problem.")),
2832 ("Received buffer without a new-segment. Assuming timestamps start from 0."));
2835 /* this means this sink will assume timestamps start from 0 */
2836 GST_OBJECT_LOCK (basesink);
2837 clip_segment->start = 0;
2838 clip_segment->stop = -1;
2839 basesink->segment.start = 0;
2840 basesink->segment.stop = -1;
2841 basesink->have_newsegment = TRUE;
2842 GST_OBJECT_UNLOCK (basesink);
2845 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2847 /* check if the buffer needs to be dropped, we first ask the subclass for the
2849 if (bclass->get_times)
2850 bclass->get_times (basesink, buf, &start, &end);
2853 /* if the subclass does not want sync, we use our own values so that we at
2854 * least clip the buffer to the segment */
2855 gst_base_sink_get_times (basesink, buf, &start, &end);
2858 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
2859 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
2861 /* a dropped buffer does not participate in anything */
2862 if (GST_CLOCK_TIME_IS_VALID (start) &&
2863 (clip_segment->format == GST_FORMAT_TIME)) {
2864 if (G_UNLIKELY (!gst_segment_clip (clip_segment,
2865 GST_FORMAT_TIME, (gint64) start, (gint64) end, NULL, NULL)))
2866 goto out_of_segment;
2869 /* now we can process the buffer in the queue, this function takes ownership
2871 result = gst_base_sink_queue_object_unlocked (basesink, pad,
2872 GST_MINI_OBJECT_CAST (buf), TRUE);
2879 GST_DEBUG_OBJECT (basesink, "sink is flushing");
2880 gst_buffer_unref (buf);
2881 return GST_FLOW_WRONG_STATE;
2885 GST_DEBUG_OBJECT (basesink,
2886 "we are EOS, dropping object, return UNEXPECTED");
2887 gst_buffer_unref (buf);
2888 return GST_FLOW_UNEXPECTED;
2892 GST_DEBUG_OBJECT (basesink, "dropping buffer, out of clipping segment");
2893 gst_buffer_unref (buf);
2900 static GstFlowReturn
2901 gst_base_sink_chain (GstPad * pad, GstBuffer * buf)
2903 GstBaseSink *basesink;
2904 GstFlowReturn result;
2906 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
2908 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH))
2911 GST_PAD_PREROLL_LOCK (pad);
2912 result = gst_base_sink_chain_unlocked (basesink, pad, buf);
2913 GST_PAD_PREROLL_UNLOCK (pad);
2921 GST_OBJECT_LOCK (pad);
2922 GST_WARNING_OBJECT (basesink,
2923 "Push on pad %s:%s, but it was not activated in push mode",
2924 GST_DEBUG_PAD_NAME (pad));
2925 GST_OBJECT_UNLOCK (pad);
2926 gst_buffer_unref (buf);
2927 /* we don't post an error message this will signal to the peer
2928 * pushing that EOS is reached. */
2929 result = GST_FLOW_UNEXPECTED;
2934 /* perform a seek, only executed in pull mode */
2936 gst_base_sink_perform_seek (GstBaseSink * basesink, GstPad * pad,
2941 GstFormat seek_format;
2943 GstSeekType cur_type, stop_type;
2947 GST_DEBUG_OBJECT (basesink, "performing seek with event %p", event);
2948 gst_event_parse_seek (event, &rate, &seek_format, &flags,
2949 &cur_type, &cur, &stop_type, &stop);
2951 flush = flags & GST_SEEK_FLAG_FLUSH;
2953 GST_DEBUG_OBJECT (basesink, "performing seek without event");
2958 GST_DEBUG_OBJECT (basesink, "flushing upstream");
2959 gst_pad_push_event (pad, gst_event_new_flush_start ());
2960 gst_base_sink_flush_start (basesink, pad);
2962 GST_DEBUG_OBJECT (basesink, "pausing pulling thread");
2965 GST_PAD_STREAM_LOCK (pad);
2968 GST_DEBUG_OBJECT (basesink, "stop flushing upstream");
2969 gst_pad_push_event (pad, gst_event_new_flush_stop ());
2970 gst_base_sink_flush_stop (basesink, pad);
2972 GST_DEBUG_OBJECT (basesink, "restarting pulling thread");
2975 GST_PAD_STREAM_UNLOCK (pad);
2983 gst_base_sink_loop (GstPad * pad)
2985 GstBaseSink *basesink;
2986 GstBuffer *buf = NULL;
2987 GstFlowReturn result;
2991 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
2993 g_assert (basesink->pad_mode == GST_ACTIVATE_PULL);
2995 if ((blocksize = basesink->priv->blocksize) == 0)
2998 offset = basesink->segment.last_stop;
3000 GST_DEBUG_OBJECT (basesink, "pulling %" G_GUINT64_FORMAT ", %u",
3003 result = gst_pad_pull_range (pad, offset, blocksize, &buf);
3004 if (G_UNLIKELY (result != GST_FLOW_OK))
3007 if (G_UNLIKELY (buf == NULL))
3010 offset += GST_BUFFER_SIZE (buf);
3012 gst_segment_set_last_stop (&basesink->segment, GST_FORMAT_BYTES, offset);
3014 GST_PAD_PREROLL_LOCK (pad);
3015 result = gst_base_sink_chain_unlocked (basesink, pad, buf);
3016 GST_PAD_PREROLL_UNLOCK (pad);
3017 if (G_UNLIKELY (result != GST_FLOW_OK))
3025 GST_LOG_OBJECT (basesink, "pausing task, reason %s",
3026 gst_flow_get_name (result));
3027 gst_pad_pause_task (pad);
3028 /* fatal errors and NOT_LINKED cause EOS */
3029 if (GST_FLOW_IS_FATAL (result) || result == GST_FLOW_NOT_LINKED) {
3030 if (result == GST_FLOW_UNEXPECTED) {
3031 /* perform EOS logic */
3032 if (basesink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3033 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3034 gst_message_new_segment_done (GST_OBJECT_CAST (basesink),
3035 basesink->segment.format, basesink->segment.last_stop));
3037 gst_base_sink_event (pad, gst_event_new_eos ());
3040 /* for fatal errors we post an error message, post the error
3041 * first so the app knows about the error first. */
3042 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
3043 (_("Internal data stream error.")),
3044 ("stream stopped, reason %s", gst_flow_get_name (result)));
3045 gst_base_sink_event (pad, gst_event_new_eos ());
3052 GST_LOG_OBJECT (basesink, "no buffer, pausing");
3053 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
3054 (_("Internal data flow error.")), ("element returned NULL buffer"));
3055 result = GST_FLOW_ERROR;
3061 gst_base_sink_set_flushing (GstBaseSink * basesink, GstPad * pad,
3064 GstBaseSinkClass *bclass;
3066 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3069 /* unlock any subclasses, we need to do this before grabbing the
3070 * PREROLL_LOCK since we hold this lock before going into ::render. */
3072 bclass->unlock (basesink);
3075 GST_PAD_PREROLL_LOCK (pad);
3076 basesink->flushing = flushing;
3078 /* step 1, now that we have the PREROLL lock, clear our unlock request */
3079 if (bclass->unlock_stop)
3080 bclass->unlock_stop (basesink);
3082 /* set need_preroll before we unblock the clock. If the clock is unblocked
3083 * before timing out, we can reuse the buffer for preroll. */
3084 basesink->need_preroll = TRUE;
3086 /* step 2, unblock clock sync (if any) or any other blocking thing */
3087 if (basesink->clock_id) {
3088 gst_clock_id_unschedule (basesink->clock_id);
3091 /* flush out the data thread if it's locked in finish_preroll, this will
3092 * also flush out the EOS state */
3093 GST_DEBUG_OBJECT (basesink,
3094 "flushing out data thread, need preroll to TRUE");
3095 gst_base_sink_preroll_queue_flush (basesink, pad);
3097 GST_PAD_PREROLL_UNLOCK (pad);
3103 gst_base_sink_default_activate_pull (GstBaseSink * basesink, gboolean active)
3109 result = gst_pad_start_task (basesink->sinkpad,
3110 (GstTaskFunction) gst_base_sink_loop, basesink->sinkpad);
3112 /* step 2, make sure streaming finishes */
3113 result = gst_pad_stop_task (basesink->sinkpad);
3120 gst_base_sink_pad_activate (GstPad * pad)
3122 gboolean result = FALSE;
3123 GstBaseSink *basesink;
3125 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3127 GST_DEBUG_OBJECT (basesink, "Trying pull mode first");
3129 gst_base_sink_set_flushing (basesink, pad, FALSE);
3131 /* we need to have the pull mode enabled */
3132 if (!basesink->can_activate_pull) {
3133 GST_DEBUG_OBJECT (basesink, "pull mode disabled");
3137 /* check if downstreams supports pull mode at all */
3138 if (!gst_pad_check_pull_range (pad)) {
3139 GST_DEBUG_OBJECT (basesink, "pull mode not supported");
3143 /* set the pad mode before starting the task so that it's in the
3144 * correct state for the new thread. also the sink set_caps and get_caps
3145 * function checks this */
3146 basesink->pad_mode = GST_ACTIVATE_PULL;
3148 /* we first try to negotiate a format so that when we try to activate
3149 * downstream, it knows about our format */
3150 if (!gst_base_sink_negotiate_pull (basesink)) {
3151 GST_DEBUG_OBJECT (basesink, "failed to negotiate in pull mode");
3155 /* ok activate now */
3156 if (!gst_pad_activate_pull (pad, TRUE)) {
3157 /* clear any pending caps */
3158 GST_OBJECT_LOCK (basesink);
3159 gst_caps_replace (&basesink->priv->pull_caps, NULL);
3160 GST_OBJECT_UNLOCK (basesink);
3161 GST_DEBUG_OBJECT (basesink, "failed to activate in pull mode");
3165 GST_DEBUG_OBJECT (basesink, "Success activating pull mode");
3169 /* push mode fallback */
3171 GST_DEBUG_OBJECT (basesink, "Falling back to push mode");
3172 if ((result = gst_pad_activate_push (pad, TRUE))) {
3173 GST_DEBUG_OBJECT (basesink, "Success activating push mode");
3178 GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
3179 gst_base_sink_set_flushing (basesink, pad, TRUE);
3182 gst_object_unref (basesink);
3188 gst_base_sink_pad_activate_push (GstPad * pad, gboolean active)
3191 GstBaseSink *basesink;
3193 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3196 if (!basesink->can_activate_push) {
3198 basesink->pad_mode = GST_ACTIVATE_NONE;
3201 basesink->pad_mode = GST_ACTIVATE_PUSH;
3204 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH)) {
3205 g_warning ("Internal GStreamer activation error!!!");
3208 gst_base_sink_set_flushing (basesink, pad, TRUE);
3210 basesink->pad_mode = GST_ACTIVATE_NONE;
3214 gst_object_unref (basesink);
3220 gst_base_sink_negotiate_pull (GstBaseSink * basesink)
3227 /* this returns the intersection between our caps and the peer caps. If there
3228 * is no peer, it returns NULL and we can't operate in pull mode so we can
3229 * fail the negotiation. */
3230 caps = gst_pad_get_allowed_caps (GST_BASE_SINK_PAD (basesink));
3231 if (caps == NULL || gst_caps_is_empty (caps))
3232 goto no_caps_possible;
3234 GST_DEBUG_OBJECT (basesink, "allowed caps: %" GST_PTR_FORMAT, caps);
3236 caps = gst_caps_make_writable (caps);
3237 /* get the first (prefered) format */
3238 gst_caps_truncate (caps);
3240 gst_pad_fixate_caps (GST_BASE_SINK_PAD (basesink), caps);
3242 GST_DEBUG_OBJECT (basesink, "fixated to: %" GST_PTR_FORMAT, caps);
3244 if (gst_caps_is_any (caps)) {
3245 GST_DEBUG_OBJECT (basesink, "caps were ANY after fixating, "
3247 /* neither side has template caps in this case, so they are prepared for
3248 pull() without setcaps() */
3250 } else if (gst_caps_is_fixed (caps)) {
3251 if (!gst_pad_set_caps (GST_BASE_SINK_PAD (basesink), caps))
3252 goto could_not_set_caps;
3254 GST_OBJECT_LOCK (basesink);
3255 gst_caps_replace (&basesink->priv->pull_caps, caps);
3256 GST_OBJECT_UNLOCK (basesink);
3261 gst_caps_unref (caps);
3267 GST_INFO_OBJECT (basesink, "Pipeline could not agree on caps");
3268 GST_DEBUG_OBJECT (basesink, "get_allowed_caps() returned EMPTY");
3270 gst_caps_unref (caps);
3275 GST_INFO_OBJECT (basesink, "Could not set caps: %" GST_PTR_FORMAT, caps);
3276 gst_caps_unref (caps);
3281 /* this won't get called until we implement an activate function */
3283 gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active)
3285 gboolean result = FALSE;
3286 GstBaseSink *basesink;
3287 GstBaseSinkClass *bclass;
3289 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3290 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3293 /* we mark we have a newsegment here because pull based
3294 * mode works just fine without having a newsegment before the
3296 gst_segment_init (&basesink->segment, GST_FORMAT_BYTES);
3297 gst_segment_init (basesink->abidata.ABI.clip_segment, GST_FORMAT_BYTES);
3298 GST_OBJECT_LOCK (basesink);
3299 basesink->have_newsegment = TRUE;
3300 GST_OBJECT_UNLOCK (basesink);
3302 if (bclass->activate_pull)
3303 result = bclass->activate_pull (basesink, TRUE);
3307 /* but if starting the thread fails, set it back */
3309 basesink->pad_mode = GST_ACTIVATE_NONE;
3311 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PULL)) {
3312 g_warning ("Internal GStreamer activation error!!!");
3315 result = gst_base_sink_set_flushing (basesink, pad, TRUE);
3316 if (bclass->activate_pull)
3317 result &= bclass->activate_pull (basesink, FALSE);
3318 basesink->pad_mode = GST_ACTIVATE_NONE;
3319 /* clear any pending caps */
3320 GST_OBJECT_LOCK (basesink);
3321 gst_caps_replace (&basesink->priv->pull_caps, NULL);
3322 GST_OBJECT_UNLOCK (basesink);
3325 gst_object_unref (basesink);
3330 /* send an event to our sinkpad peer. */
3332 gst_base_sink_send_event (GstElement * element, GstEvent * event)
3335 GstBaseSink *basesink = GST_BASE_SINK (element);
3336 gboolean forward, result = TRUE;
3337 GstActivateMode mode;
3339 GST_OBJECT_LOCK (element);
3340 /* get the pad and the scheduling mode */
3341 pad = gst_object_ref (basesink->sinkpad);
3342 mode = basesink->pad_mode;
3343 GST_OBJECT_UNLOCK (element);
3345 /* only push UPSTREAM events upstream and if we are in push mode */
3346 forward = GST_EVENT_IS_UPSTREAM (event) && (mode == GST_ACTIVATE_PUSH);
3348 switch (GST_EVENT_TYPE (event)) {
3349 case GST_EVENT_LATENCY:
3351 GstClockTime latency;
3353 gst_event_parse_latency (event, &latency);
3355 /* store the latency. We use this to adjust the running_time before syncing
3356 * it to the clock. */
3357 GST_OBJECT_LOCK (element);
3358 basesink->priv->latency = latency;
3359 GST_OBJECT_UNLOCK (element);
3360 GST_DEBUG_OBJECT (basesink, "latency set to %" GST_TIME_FORMAT,
3361 GST_TIME_ARGS (latency));
3363 /* We forward this event so that all elements know about the global pipeline
3364 * latency. This is interesting for an element when it wants to figure out
3365 * when a particular piece of data will be rendered. */
3368 case GST_EVENT_SEEK:
3369 /* in pull mode we will execute the seek */
3370 if (mode == GST_ACTIVATE_PULL)
3371 result = gst_base_sink_perform_seek (basesink, pad, event);
3378 result = gst_pad_push_event (pad, event);
3380 /* not forwarded, unref the event */
3381 gst_event_unref (event);
3384 gst_object_unref (pad);
3389 gst_base_sink_peer_query (GstBaseSink * sink, GstQuery * query)
3392 gboolean res = FALSE;
3394 if ((peer = gst_pad_get_peer (sink->sinkpad))) {
3395 res = gst_pad_query (peer, query);
3396 gst_object_unref (peer);
3401 /* get the end position of the last seen object, this is used
3402 * for EOS and for making sure that we don't report a position we
3403 * have not reached yet. */
3405 gst_base_sink_get_position_last (GstBaseSink * basesink, gint64 * cur)
3407 /* return last observed stream time */
3408 *cur = basesink->priv->current_sstop;
3410 GST_DEBUG_OBJECT (basesink, "POSITION: %" GST_TIME_FORMAT,
3411 GST_TIME_ARGS (*cur));
3415 /* get the position when we are PAUSED, this is the stream time of the buffer
3416 * that prerolled. If no buffer is prerolled (we are still flushing), this
3417 * value will be -1. */
3419 gst_base_sink_get_position_paused (GstBaseSink * basesink, gint64 * cur)
3423 GstSegment *segment;
3425 *cur = basesink->priv->current_sstart;
3426 segment = basesink->abidata.ABI.clip_segment;
3428 time = segment->time;
3431 *cur = MAX (*cur, time);
3432 GST_DEBUG_OBJECT (basesink, "POSITION as max: %" GST_TIME_FORMAT
3433 ", time %" GST_TIME_FORMAT, GST_TIME_ARGS (*cur), GST_TIME_ARGS (time));
3435 /* we have no buffer, use the segment times. */
3436 if (segment->rate >= 0.0) {
3437 /* forward, next position is always the time of the segment */
3439 GST_DEBUG_OBJECT (basesink, "POSITION as time: %" GST_TIME_FORMAT,
3440 GST_TIME_ARGS (*cur));
3442 /* reverse, next expected timestamp is segment->stop. We use the function
3443 * to get things right for negative applied_rates. */
3445 gst_segment_to_stream_time (segment, GST_FORMAT_TIME, segment->stop);
3446 GST_DEBUG_OBJECT (basesink, "reverse POSITION: %" GST_TIME_FORMAT,
3447 GST_TIME_ARGS (*cur));
3456 gst_base_sink_get_position (GstBaseSink * basesink, GstFormat format,
3457 gint64 * cur, gboolean * upstream)
3460 gboolean res = FALSE;
3463 /* we can answer time format */
3464 case GST_FORMAT_TIME:
3466 GstClockTime now, base, latency;
3467 gint64 time, accum, duration;
3471 GST_OBJECT_LOCK (basesink);
3473 /* can only give answer based on the clock if not EOS */
3474 if (G_UNLIKELY (basesink->eos))
3477 /* we can only get the segment when we are not NULL or READY */
3478 if (!basesink->have_newsegment)
3481 /* when not in PLAYING or when we're busy with a state change, we
3482 * cannot read from the clock so we report time based on the
3483 * last seen timestamp. */
3484 if (GST_STATE (basesink) != GST_STATE_PLAYING ||
3485 GST_STATE_PENDING (basesink) != GST_STATE_VOID_PENDING)
3488 /* we need to sync on the clock. */
3489 if (basesink->sync == FALSE)
3492 /* and we need a clock */
3493 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (basesink)) == NULL))
3496 /* collect all data we need holding the lock */
3497 if (GST_CLOCK_TIME_IS_VALID (basesink->segment.time))
3498 time = basesink->segment.time;
3502 if (GST_CLOCK_TIME_IS_VALID (basesink->segment.stop))
3503 duration = basesink->segment.stop - basesink->segment.start;
3507 base = GST_ELEMENT_CAST (basesink)->base_time;
3508 accum = basesink->segment.accum;
3509 rate = basesink->segment.rate * basesink->segment.applied_rate;
3510 gst_base_sink_get_position_last (basesink, &last);
3511 latency = basesink->priv->latency;
3513 gst_object_ref (clock);
3514 /* need to release the object lock before we can get the time,
3515 * a clock might take the LOCK of the provider, which could be
3516 * a basesink subclass. */
3517 GST_OBJECT_UNLOCK (basesink);
3519 now = gst_clock_get_time (clock);
3521 /* subtract base time and accumulated time from the clock time.
3522 * Make sure we don't go negative. This is the current time in
3523 * the segment which we need to scale with the combined
3524 * rate and applied rate. */
3527 base = MIN (now, base);
3529 /* for negative rates we need to count back from from the segment
3534 *cur = time + gst_guint64_to_gdouble (now - base) * rate;
3536 /* never report more than last seen position */
3538 *cur = MIN (last, *cur);
3540 gst_object_unref (clock);
3544 GST_DEBUG_OBJECT (basesink,
3545 "now %" GST_TIME_FORMAT " - base %" GST_TIME_FORMAT " - accum %"
3546 GST_TIME_FORMAT " + time %" GST_TIME_FORMAT,
3547 GST_TIME_ARGS (now), GST_TIME_ARGS (base),
3548 GST_TIME_ARGS (accum), GST_TIME_ARGS (time));
3552 /* cannot answer other than TIME, ask to send the query upstream. */
3558 GST_DEBUG_OBJECT (basesink, "res: %d, POSITION: %" GST_TIME_FORMAT,
3559 res, GST_TIME_ARGS (*cur));
3565 GST_DEBUG_OBJECT (basesink, "position in EOS");
3566 res = gst_base_sink_get_position_last (basesink, cur);
3567 GST_OBJECT_UNLOCK (basesink);
3572 GST_DEBUG_OBJECT (basesink, "position in PAUSED");
3573 res = gst_base_sink_get_position_paused (basesink, cur);
3574 GST_OBJECT_UNLOCK (basesink);
3579 /* in NULL or READY we always return FALSE and -1 */
3580 GST_DEBUG_OBJECT (basesink, "position in wrong state, return -1");
3583 GST_OBJECT_UNLOCK (basesink);
3588 /* report last seen timestamp if any, else ask upstream to answer */
3589 if ((*cur = basesink->priv->current_sstart) != -1)
3594 GST_DEBUG_OBJECT (basesink, "no sync, res %d, POSITION %" GST_TIME_FORMAT,
3595 res, GST_TIME_ARGS (*cur));
3596 GST_OBJECT_UNLOCK (basesink);
3602 gst_base_sink_query (GstElement * element, GstQuery * query)
3604 gboolean res = FALSE;
3606 GstBaseSink *basesink = GST_BASE_SINK (element);
3608 switch (GST_QUERY_TYPE (query)) {
3609 case GST_QUERY_POSITION:
3613 gboolean upstream = FALSE;
3615 gst_query_parse_position (query, &format, NULL);
3617 GST_DEBUG_OBJECT (basesink, "position format %d", format);
3619 /* first try to get the position based on the clock */
3621 gst_base_sink_get_position (basesink, format, &cur, &upstream))) {
3622 gst_query_set_position (query, format, cur);
3623 } else if (upstream) {
3624 /* fallback to peer query */
3625 res = gst_base_sink_peer_query (basesink, query);
3629 case GST_QUERY_DURATION:
3630 GST_DEBUG_OBJECT (basesink, "duration query");
3631 res = gst_base_sink_peer_query (basesink, query);
3633 case GST_QUERY_LATENCY:
3635 gboolean live, us_live;
3636 GstClockTime min, max;
3638 if ((res = gst_base_sink_query_latency (basesink, &live, &us_live, &min,
3640 gst_query_set_latency (query, live, min, max);
3644 case GST_QUERY_JITTER:
3646 case GST_QUERY_RATE:
3647 /* gst_query_set_rate (query, basesink->segment_rate); */
3650 case GST_QUERY_SEGMENT:
3652 /* FIXME, bring start/stop to stream time */
3653 gst_query_set_segment (query, basesink->segment.rate,
3654 GST_FORMAT_TIME, basesink->segment.start, basesink->segment.stop);
3657 case GST_QUERY_SEEKING:
3658 case GST_QUERY_CONVERT:
3659 case GST_QUERY_FORMATS:
3661 res = gst_base_sink_peer_query (basesink, query);
3667 static GstStateChangeReturn
3668 gst_base_sink_change_state (GstElement * element, GstStateChange transition)
3670 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
3671 GstBaseSink *basesink = GST_BASE_SINK (element);
3672 GstBaseSinkClass *bclass;
3673 GstBaseSinkPrivate *priv;
3675 priv = basesink->priv;
3677 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3679 switch (transition) {
3680 case GST_STATE_CHANGE_NULL_TO_READY:
3682 if (!bclass->start (basesink))
3685 case GST_STATE_CHANGE_READY_TO_PAUSED:
3686 /* need to complete preroll before this state change completes, there
3687 * is no data flow in READY so we can safely assume we need to preroll. */
3688 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
3689 GST_DEBUG_OBJECT (basesink, "READY to PAUSED");
3690 basesink->have_newsegment = FALSE;
3691 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
3692 gst_segment_init (basesink->abidata.ABI.clip_segment,
3693 GST_FORMAT_UNDEFINED);
3694 basesink->offset = 0;
3695 basesink->have_preroll = FALSE;
3696 basesink->need_preroll = TRUE;
3697 basesink->playing_async = TRUE;
3698 priv->current_sstart = -1;
3699 priv->current_sstop = -1;
3700 priv->eos_rtime = -1;
3702 basesink->eos = FALSE;
3703 priv->received_eos = FALSE;
3704 gst_base_sink_reset_qos (basesink);
3705 priv->commited = FALSE;
3706 if (priv->async_enabled) {
3707 GST_DEBUG_OBJECT (basesink, "doing async state change");
3708 /* when async enabled, post async-start message and return ASYNC from
3709 * the state change function */
3710 ret = GST_STATE_CHANGE_ASYNC;
3711 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3712 gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
3714 priv->have_latency = TRUE;
3716 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
3718 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3719 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
3720 if (!gst_base_sink_needs_preroll (basesink)) {
3721 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, don't need preroll");
3722 /* no preroll needed anymore now. */
3723 basesink->playing_async = FALSE;
3724 basesink->need_preroll = FALSE;
3725 if (basesink->eos) {
3726 /* need to post EOS message here */
3727 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
3728 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3729 gst_message_new_eos (GST_OBJECT_CAST (basesink)));
3731 GST_DEBUG_OBJECT (basesink, "signal preroll");
3732 GST_PAD_PREROLL_SIGNAL (basesink->sinkpad);
3735 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, we are not prerolled");
3736 basesink->need_preroll = TRUE;
3737 basesink->playing_async = TRUE;
3738 priv->commited = FALSE;
3739 if (priv->async_enabled) {
3740 GST_DEBUG_OBJECT (basesink, "doing async state change");
3741 ret = GST_STATE_CHANGE_ASYNC;
3742 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3743 gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
3746 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
3753 GstStateChangeReturn bret;
3755 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3756 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
3757 goto activate_failed;
3760 switch (transition) {
3761 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3762 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED");
3763 /* FIXME, make sure we cannot enter _render first */
3765 /* we need to call ::unlock before locking PREROLL_LOCK
3766 * since we lock it before going into ::render */
3768 bclass->unlock (basesink);
3770 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
3771 /* now that we have the PREROLL lock, clear our unlock request */
3772 if (bclass->unlock_stop)
3773 bclass->unlock_stop (basesink);
3775 /* we need preroll again and we set the flag before unlocking the clockid
3776 * because if the clockid is unlocked before a current buffer expired, we
3777 * can use that buffer to preroll with */
3778 basesink->need_preroll = TRUE;
3780 if (basesink->clock_id) {
3781 gst_clock_id_unschedule (basesink->clock_id);
3784 /* if we don't have a preroll buffer we need to wait for a preroll and
3786 if (!gst_base_sink_needs_preroll (basesink)) {
3787 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, we are prerolled");
3788 basesink->playing_async = FALSE;
3790 if (GST_STATE_TARGET (GST_ELEMENT (basesink)) <= GST_STATE_READY) {
3791 ret = GST_STATE_CHANGE_SUCCESS;
3793 GST_DEBUG_OBJECT (basesink,
3794 "PLAYING to PAUSED, we are not prerolled");
3795 basesink->playing_async = TRUE;
3796 priv->commited = FALSE;
3797 if (priv->async_enabled) {
3798 GST_DEBUG_OBJECT (basesink, "doing async state change");
3799 ret = GST_STATE_CHANGE_ASYNC;
3800 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3801 gst_message_new_async_start (GST_OBJECT_CAST (basesink),
3806 GST_DEBUG_OBJECT (basesink, "rendered: %" G_GUINT64_FORMAT
3807 ", dropped: %" G_GUINT64_FORMAT, priv->rendered, priv->dropped);
3809 gst_base_sink_reset_qos (basesink);
3810 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
3812 case GST_STATE_CHANGE_PAUSED_TO_READY:
3813 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
3814 /* start by reseting our position state with the object lock so that the
3815 * position query gets the right idea. We do this before we post the
3816 * messages so that the message handlers pick this up. */
3817 GST_OBJECT_LOCK (basesink);
3818 basesink->have_newsegment = FALSE;
3819 priv->current_sstart = -1;
3820 priv->current_sstop = -1;
3821 priv->have_latency = FALSE;
3822 GST_OBJECT_UNLOCK (basesink);
3824 gst_base_sink_set_last_buffer (basesink, NULL);
3826 if (!priv->commited) {
3827 if (priv->async_enabled) {
3828 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, posting async-done");
3830 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3831 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
3832 GST_STATE_PLAYING, GST_STATE_PAUSED, GST_STATE_READY));
3834 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3835 gst_message_new_async_done (GST_OBJECT_CAST (basesink)));
3837 priv->commited = TRUE;
3839 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, don't need_preroll");
3841 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
3843 case GST_STATE_CHANGE_READY_TO_NULL:
3845 if (!bclass->stop (basesink)) {
3846 GST_WARNING_OBJECT (basesink, "failed to stop");
3849 gst_base_sink_set_last_buffer (basesink, NULL);
3860 GST_DEBUG_OBJECT (basesink, "failed to start");
3861 return GST_STATE_CHANGE_FAILURE;
3865 GST_DEBUG_OBJECT (basesink,
3866 "element failed to change states -- activation problem?");
3867 return GST_STATE_CHANGE_FAILURE;