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_wait_eos:
1713 * @time: the running_time to be reached
1714 * @jitter: the jitter to be filled with time diff (can be NULL)
1716 * This function will block until @time is reached. It is usually called by
1717 * subclasses that use their own internal synchronisation but want to let the
1718 * EOS be handled by the base class.
1720 * This function should only be called with the PREROLL_LOCK held, like when
1721 * receiving an EOS event in the ::event vmethod.
1723 * The @time argument should be the running_time of when the EOS should happen
1724 * and will be adjusted with any latency and offset configured in the sink.
1728 * Returns: #GstFlowReturn
1731 gst_base_sink_wait_eos (GstBaseSink * sink, GstClockTime time,
1732 GstClockTimeDiff * jitter)
1734 GstClockReturn status;
1740 GST_DEBUG_OBJECT (sink, "checking preroll");
1742 /* first wait for the playing state before we can continue */
1743 if (G_UNLIKELY (sink->need_preroll)) {
1744 ret = gst_base_sink_wait_preroll (sink);
1745 if (ret != GST_FLOW_OK)
1749 /* preroll done, we can sync since we are in PLAYING now. */
1750 GST_DEBUG_OBJECT (sink, "possibly waiting for clock to reach %"
1751 GST_TIME_FORMAT, GST_TIME_ARGS (time));
1753 /* compensate for latency and ts_offset. We don't adjust for render delay
1754 * because we don't interact with the device on EOS normally. */
1755 stime = gst_base_sink_adjust_time (sink, time);
1757 /* wait for the clock, this can be interrupted because we got shut down or
1759 status = gst_base_sink_wait_clock (sink, stime, jitter);
1761 GST_DEBUG_OBJECT (sink, "clock returned %d", status);
1763 /* invalid time, no clock or sync disabled, just continue then */
1764 if (status == GST_CLOCK_BADTIME)
1767 /* waiting could have been interrupted and we can be flushing now */
1768 if (G_UNLIKELY (sink->flushing))
1771 /* retry if we got unscheduled, which means we did not reach the timeout
1772 * yet. if some other error occures, we continue. */
1773 } while (status == GST_CLOCK_UNSCHEDULED);
1775 GST_DEBUG_OBJECT (sink, "end of stream");
1782 GST_DEBUG_OBJECT (sink, "we are flushing");
1783 return GST_FLOW_WRONG_STATE;
1787 /* with STREAM_LOCK, PREROLL_LOCK
1789 * Make sure we are in PLAYING and synchronize an object to the clock.
1791 * If we need preroll, we are not in PLAYING. We try to commit the state
1792 * if needed and then block if we still are not PLAYING.
1794 * We start waiting on the clock in PLAYING. If we got interrupted, we
1795 * immediatly try to re-preroll.
1797 * Some objects do not need synchronisation (most events) and so this function
1798 * immediatly returns GST_FLOW_OK.
1800 * for objects that arrive later than max-lateness to be synchronized to the
1801 * clock have the @late boolean set to TRUE.
1803 * This function keeps a running average of the jitter (the diff between the
1804 * clock time and the requested sync time). The jitter is negative for
1805 * objects that arrive in time and positive for late buffers.
1807 * does not take ownership of obj.
1809 static GstFlowReturn
1810 gst_base_sink_do_sync (GstBaseSink * basesink, GstPad * pad,
1811 GstMiniObject * obj, gboolean * late)
1813 GstClockTimeDiff jitter;
1815 GstClockReturn status = GST_CLOCK_OK;
1816 GstClockTime rstart, rstop, sstart, sstop, stime;
1818 GstBaseSinkPrivate *priv;
1820 priv = basesink->priv;
1822 sstart = sstop = rstart = rstop = -1;
1825 priv->current_rstart = -1;
1827 /* get timing information for this object against the render segment */
1828 syncable = gst_base_sink_get_sync_times (basesink, obj,
1829 &sstart, &sstop, &rstart, &rstop, &do_sync, &basesink->segment);
1831 /* a syncable object needs to participate in preroll and
1832 * clocking. All buffers and EOS are syncable. */
1833 if (G_UNLIKELY (!syncable))
1836 /* store timing info for current object */
1837 priv->current_rstart = rstart;
1838 priv->current_rstop = (rstop != -1 ? rstop : rstart);
1839 /* save sync time for eos when the previous object needed sync */
1840 priv->eos_rtime = (do_sync ? priv->current_rstop : -1);
1843 /* first do preroll, this makes sure we commit our state
1844 * to PAUSED and can continue to PLAYING. We cannot perform
1845 * any clock sync in PAUSED because there is no clock.
1847 while (G_UNLIKELY (basesink->need_preroll)) {
1848 GST_DEBUG_OBJECT (basesink, "prerolling object %p", obj);
1850 if (G_LIKELY (basesink->playing_async)) {
1852 if (G_UNLIKELY (!gst_base_sink_commit_state (basesink)))
1856 /* need to recheck here because the commit state could have
1857 * made us not need the preroll anymore */
1858 if (G_LIKELY (basesink->need_preroll)) {
1859 /* block until the state changes, or we get a flush, or something */
1860 if (gst_base_sink_wait_preroll (basesink) != GST_FLOW_OK)
1865 /* After rendering we store the position of the last buffer so that we can use
1866 * it to report the position. We need to take the lock here. */
1867 GST_OBJECT_LOCK (basesink);
1868 priv->current_sstart = sstart;
1869 priv->current_sstop = (sstop != -1 ? sstop : sstart);
1870 GST_OBJECT_UNLOCK (basesink);
1875 /* adjust for latency */
1876 stime = gst_base_sink_adjust_time (basesink, rstart);
1878 /* adjust for render-delay, avoid underflows */
1880 if (stime > priv->render_delay)
1881 stime -= priv->render_delay;
1886 /* preroll done, we can sync since we are in PLAYING now. */
1887 GST_DEBUG_OBJECT (basesink, "possibly waiting for clock to reach %"
1888 GST_TIME_FORMAT ", adjusted %" GST_TIME_FORMAT,
1889 GST_TIME_ARGS (rstart), GST_TIME_ARGS (stime));
1891 /* This function will return immediatly if start == -1, no clock
1892 * or sync is disabled with GST_CLOCK_BADTIME. */
1893 status = gst_base_sink_wait_clock (basesink, stime, &jitter);
1895 GST_DEBUG_OBJECT (basesink, "clock returned %d", status);
1897 /* invalid time, no clock or sync disabled, just render */
1898 if (status == GST_CLOCK_BADTIME)
1901 /* waiting could have been interrupted and we can be flushing now */
1902 if (G_UNLIKELY (basesink->flushing))
1905 /* check for unlocked by a state change, we are not flushing so
1906 * we can try to preroll on the current buffer. */
1907 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
1908 GST_DEBUG_OBJECT (basesink, "unscheduled, waiting some more");
1912 /* successful syncing done, record observation */
1913 priv->current_jitter = jitter;
1915 /* check if the object should be dropped */
1916 *late = gst_base_sink_is_too_late (basesink, obj, rstart, rstop,
1925 GST_DEBUG_OBJECT (basesink, "non syncable object %p", obj);
1930 GST_DEBUG_OBJECT (basesink, "we are flushing");
1931 return GST_FLOW_WRONG_STATE;
1935 GST_DEBUG_OBJECT (basesink, "stopping while commiting state");
1936 return GST_FLOW_WRONG_STATE;
1941 gst_base_sink_send_qos (GstBaseSink * basesink,
1942 gdouble proportion, GstClockTime time, GstClockTimeDiff diff)
1947 /* generate Quality-of-Service event */
1948 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
1949 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
1950 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (time));
1952 event = gst_event_new_qos (proportion, diff, time);
1955 res = gst_pad_push_event (basesink->sinkpad, event);
1961 gst_base_sink_perform_qos (GstBaseSink * sink, gboolean dropped)
1963 GstBaseSinkPrivate *priv;
1964 GstClockTime start, stop;
1965 GstClockTimeDiff jitter;
1966 GstClockTime pt, entered, left;
1967 GstClockTime duration;
1972 start = priv->current_rstart;
1974 /* if Quality-of-Service disabled, do nothing */
1975 if (!g_atomic_int_get (&priv->qos_enabled) || start == -1)
1978 stop = priv->current_rstop;
1979 jitter = priv->current_jitter;
1982 /* this is the time the buffer entered the sink */
1983 if (start < -jitter)
1986 entered = start + jitter;
1989 /* this is the time the buffer entered the sink */
1990 entered = start + jitter;
1991 /* this is the time the buffer left the sink */
1992 left = start + jitter;
1995 /* calculate duration of the buffer */
1997 duration = stop - start;
2001 /* if we have the time when the last buffer left us, calculate
2002 * processing time */
2003 if (priv->last_left != -1) {
2004 if (entered > priv->last_left) {
2005 pt = entered - priv->last_left;
2013 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "start: %" GST_TIME_FORMAT
2014 ", entered %" GST_TIME_FORMAT ", left %" GST_TIME_FORMAT ", pt: %"
2015 GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT ",jitter %"
2016 G_GINT64_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (entered),
2017 GST_TIME_ARGS (left), GST_TIME_ARGS (pt), GST_TIME_ARGS (duration),
2020 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "avg_duration: %" GST_TIME_FORMAT
2021 ", avg_pt: %" GST_TIME_FORMAT ", avg_rate: %g",
2022 GST_TIME_ARGS (priv->avg_duration), GST_TIME_ARGS (priv->avg_pt),
2025 /* collect running averages. for first observations, we copy the
2027 if (priv->avg_duration == -1)
2028 priv->avg_duration = duration;
2030 priv->avg_duration = UPDATE_RUNNING_AVG (priv->avg_duration, duration);
2032 if (priv->avg_pt == -1)
2035 priv->avg_pt = UPDATE_RUNNING_AVG (priv->avg_pt, pt);
2037 if (priv->avg_duration != 0)
2039 gst_guint64_to_gdouble (priv->avg_pt) /
2040 gst_guint64_to_gdouble (priv->avg_duration);
2044 if (priv->last_left != -1) {
2045 if (dropped || priv->avg_rate < 0.0) {
2046 priv->avg_rate = rate;
2049 priv->avg_rate = UPDATE_RUNNING_AVG_N (priv->avg_rate, rate);
2051 priv->avg_rate = UPDATE_RUNNING_AVG_P (priv->avg_rate, rate);
2055 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink,
2056 "updated: avg_duration: %" GST_TIME_FORMAT ", avg_pt: %" GST_TIME_FORMAT
2057 ", avg_rate: %g", GST_TIME_ARGS (priv->avg_duration),
2058 GST_TIME_ARGS (priv->avg_pt), priv->avg_rate);
2061 if (priv->avg_rate >= 0.0) {
2062 /* if we have a valid rate, start sending QoS messages */
2063 if (priv->current_jitter < 0) {
2064 /* make sure we never go below 0 when adding the jitter to the
2066 if (priv->current_rstart < -priv->current_jitter)
2067 priv->current_jitter = -priv->current_rstart;
2069 gst_base_sink_send_qos (sink, priv->avg_rate, priv->current_rstart,
2070 priv->current_jitter);
2073 /* record when this buffer will leave us */
2074 priv->last_left = left;
2077 /* reset all qos measuring */
2079 gst_base_sink_reset_qos (GstBaseSink * sink)
2081 GstBaseSinkPrivate *priv;
2085 priv->last_in_time = -1;
2086 priv->last_left = -1;
2087 priv->avg_duration = -1;
2089 priv->avg_rate = -1.0;
2090 priv->avg_render = -1;
2096 /* Checks if the object was scheduled too late.
2098 * start/stop contain the raw timestamp start and stop values
2101 * status and jitter contain the return values from the clock wait.
2103 * returns TRUE if the buffer was too late.
2106 gst_base_sink_is_too_late (GstBaseSink * basesink, GstMiniObject * obj,
2107 GstClockTime start, GstClockTime stop,
2108 GstClockReturn status, GstClockTimeDiff jitter)
2111 gint64 max_lateness;
2112 GstBaseSinkPrivate *priv;
2114 priv = basesink->priv;
2118 /* only for objects that were too late */
2119 if (G_LIKELY (status != GST_CLOCK_EARLY))
2122 max_lateness = basesink->abidata.ABI.max_lateness;
2124 /* check if frame dropping is enabled */
2125 if (max_lateness == -1)
2128 /* only check for buffers */
2129 if (G_UNLIKELY (!GST_IS_BUFFER (obj)))
2132 /* can't do check if we don't have a timestamp */
2133 if (G_UNLIKELY (start == -1))
2136 /* we can add a valid stop time */
2138 max_lateness += stop;
2140 max_lateness += start;
2142 /* if the jitter bigger than duration and lateness we are too late */
2143 if ((late = start + jitter > max_lateness)) {
2144 GST_DEBUG_OBJECT (basesink, "buffer is too late %" GST_TIME_FORMAT
2145 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (start + jitter),
2146 GST_TIME_ARGS (max_lateness));
2147 /* !!emergency!!, if we did not receive anything valid for more than a
2148 * second, render it anyway so the user sees something */
2149 if (priv->last_in_time && start - priv->last_in_time > GST_SECOND) {
2151 GST_DEBUG_OBJECT (basesink,
2152 "**emergency** last buffer at %" GST_TIME_FORMAT " > GST_SECOND",
2153 GST_TIME_ARGS (priv->last_in_time));
2159 priv->last_in_time = start;
2166 GST_DEBUG_OBJECT (basesink, "object was scheduled in time");
2171 GST_DEBUG_OBJECT (basesink, "frame dropping disabled");
2176 GST_DEBUG_OBJECT (basesink, "object is not a buffer");
2181 GST_DEBUG_OBJECT (basesink, "buffer has no timestamp");
2186 /* called before and after calling the render vmethod. It keeps track of how
2187 * much time was spent in the render method and is used to check if we are
2190 gst_base_sink_do_render_stats (GstBaseSink * basesink, gboolean start)
2192 GstBaseSinkPrivate *priv;
2194 priv = basesink->priv;
2197 priv->start = gst_util_get_timestamp ();
2199 GstClockTime elapsed;
2201 priv->stop = gst_util_get_timestamp ();
2203 elapsed = GST_CLOCK_DIFF (priv->start, priv->stop);
2205 if (priv->avg_render == -1)
2206 priv->avg_render = elapsed;
2208 priv->avg_render = UPDATE_RUNNING_AVG (priv->avg_render, elapsed);
2210 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2211 "avg_render: %" GST_TIME_FORMAT, GST_TIME_ARGS (priv->avg_render));
2215 /* with STREAM_LOCK, PREROLL_LOCK,
2217 * Synchronize the object on the clock and then render it.
2219 * takes ownership of obj.
2221 static GstFlowReturn
2222 gst_base_sink_render_object (GstBaseSink * basesink, GstPad * pad,
2223 GstMiniObject * obj)
2225 GstFlowReturn ret = GST_FLOW_OK;
2226 GstBaseSinkClass *bclass;
2227 gboolean late = FALSE;
2228 GstBaseSinkPrivate *priv;
2230 priv = basesink->priv;
2232 /* synchronize this object, non syncable objects return OK
2234 ret = gst_base_sink_do_sync (basesink, pad, obj, &late);
2235 if (G_UNLIKELY (ret != GST_FLOW_OK))
2238 /* and now render, event or buffer. */
2239 if (G_LIKELY (GST_IS_BUFFER (obj))) {
2242 /* drop late buffers unconditionally, let's hope it's unlikely */
2243 if (G_UNLIKELY (late))
2246 buf = GST_BUFFER_CAST (obj);
2248 gst_base_sink_set_last_buffer (basesink, buf);
2250 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2252 if (G_LIKELY (bclass->render)) {
2255 /* read once, to get same value before and after */
2256 do_qos = g_atomic_int_get (&priv->qos_enabled);
2258 GST_DEBUG_OBJECT (basesink, "rendering buffer %p", obj);
2260 /* record rendering time for QoS and stats */
2262 gst_base_sink_do_render_stats (basesink, TRUE);
2264 ret = bclass->render (basesink, buf);
2269 gst_base_sink_do_render_stats (basesink, FALSE);
2272 GstEvent *event = GST_EVENT_CAST (obj);
2273 gboolean event_res = TRUE;
2276 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2278 type = GST_EVENT_TYPE (event);
2280 GST_DEBUG_OBJECT (basesink, "rendering event %p, type %s", obj,
2281 gst_event_type_get_name (type));
2284 event_res = bclass->event (basesink, event);
2286 /* when we get here we could be flushing again when the event handler calls
2287 * _wait_eos(). We have to ignore this object in that case. */
2288 if (G_UNLIKELY (basesink->flushing))
2291 if (G_LIKELY (event_res)) {
2294 /* the EOS event is completely handled so we mark
2295 * ourselves as being in the EOS state. eos is also
2296 * protected by the object lock so we can read it when
2297 * answering the POSITION query. */
2298 GST_OBJECT_LOCK (basesink);
2299 basesink->eos = TRUE;
2300 GST_OBJECT_UNLOCK (basesink);
2301 /* ok, now we can post the message */
2302 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
2303 gst_element_post_message (GST_ELEMENT_CAST (basesink),
2304 gst_message_new_eos (GST_OBJECT_CAST (basesink)));
2306 case GST_EVENT_NEWSEGMENT:
2307 /* configure the segment */
2308 gst_base_sink_configure_segment (basesink, pad, event,
2309 &basesink->segment);
2318 gst_base_sink_perform_qos (basesink, late);
2320 GST_DEBUG_OBJECT (basesink, "object unref after render %p", obj);
2321 gst_mini_object_unref (obj);
2328 GST_DEBUG_OBJECT (basesink, "do_sync returned %s", gst_flow_get_name (ret));
2334 GST_DEBUG_OBJECT (basesink, "buffer late, dropping");
2339 GST_DEBUG_OBJECT (basesink, "we are flushing, ignore object");
2340 gst_mini_object_unref (obj);
2341 return GST_FLOW_WRONG_STATE;
2345 /* with STREAM_LOCK, PREROLL_LOCK
2347 * Perform preroll on the given object. For buffers this means
2348 * calling the preroll subclass method.
2349 * If that succeeds, the state will be commited.
2351 * function does not take ownership of obj.
2353 static GstFlowReturn
2354 gst_base_sink_preroll_object (GstBaseSink * basesink, GstPad * pad,
2355 GstMiniObject * obj)
2359 GST_DEBUG_OBJECT (basesink, "do preroll %p", obj);
2361 /* if it's a buffer, we need to call the preroll method */
2362 if (G_LIKELY (GST_IS_BUFFER (obj))) {
2363 GstBaseSinkClass *bclass;
2365 GstClockTime timestamp;
2367 buf = GST_BUFFER_CAST (obj);
2368 timestamp = GST_BUFFER_TIMESTAMP (buf);
2370 GST_DEBUG_OBJECT (basesink, "preroll buffer %" GST_TIME_FORMAT,
2371 GST_TIME_ARGS (timestamp));
2373 gst_base_sink_set_last_buffer (basesink, buf);
2375 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2376 if (bclass->preroll)
2377 if ((ret = bclass->preroll (basesink, buf)) != GST_FLOW_OK)
2378 goto preroll_failed;
2382 if (G_LIKELY (basesink->playing_async)) {
2383 if (G_UNLIKELY (!gst_base_sink_commit_state (basesink)))
2392 GST_DEBUG_OBJECT (basesink, "preroll failed, abort state");
2393 gst_element_abort_state (GST_ELEMENT_CAST (basesink));
2398 GST_DEBUG_OBJECT (basesink, "stopping while commiting state");
2399 return GST_FLOW_WRONG_STATE;
2403 /* with STREAM_LOCK, PREROLL_LOCK
2405 * Queue an object for rendering.
2406 * The first prerollable object queued will complete the preroll. If the
2407 * preroll queue if filled, we render all the objects in the queue.
2409 * This function takes ownership of the object.
2411 static GstFlowReturn
2412 gst_base_sink_queue_object_unlocked (GstBaseSink * basesink, GstPad * pad,
2413 GstMiniObject * obj, gboolean prerollable)
2415 GstFlowReturn ret = GST_FLOW_OK;
2419 if (G_UNLIKELY (basesink->need_preroll)) {
2420 if (G_LIKELY (prerollable))
2421 basesink->preroll_queued++;
2423 length = basesink->preroll_queued;
2425 GST_DEBUG_OBJECT (basesink, "now %d prerolled items", length);
2427 /* first prerollable item needs to finish the preroll */
2429 ret = gst_base_sink_preroll_object (basesink, pad, obj);
2430 if (G_UNLIKELY (ret != GST_FLOW_OK))
2431 goto preroll_failed;
2433 /* need to recheck if we need preroll, commmit state during preroll
2434 * could have made us not need more preroll. */
2435 if (G_UNLIKELY (basesink->need_preroll)) {
2436 /* see if we can render now, if we can't add the object to the preroll
2438 if (G_UNLIKELY (length <= basesink->preroll_queue_max_len))
2443 /* we can start rendering (or blocking) the queued object
2445 q = basesink->preroll_queue;
2446 while (G_UNLIKELY (!g_queue_is_empty (q))) {
2449 o = g_queue_pop_head (q);
2450 GST_DEBUG_OBJECT (basesink, "rendering queued object %p", o);
2452 /* do something with the return value */
2453 ret = gst_base_sink_render_object (basesink, pad, o);
2454 if (ret != GST_FLOW_OK)
2455 goto dequeue_failed;
2458 /* now render the object */
2459 ret = gst_base_sink_render_object (basesink, pad, obj);
2460 basesink->preroll_queued = 0;
2467 GST_DEBUG_OBJECT (basesink, "preroll failed, reason %s",
2468 gst_flow_get_name (ret));
2469 gst_mini_object_unref (obj);
2474 /* add object to the queue and return */
2475 GST_DEBUG_OBJECT (basesink, "need more preroll data %d <= %d",
2476 length, basesink->preroll_queue_max_len);
2477 g_queue_push_tail (basesink->preroll_queue, obj);
2482 GST_DEBUG_OBJECT (basesink, "rendering queued objects failed, reason %s",
2483 gst_flow_get_name (ret));
2484 gst_mini_object_unref (obj);
2491 * This function grabs the PREROLL_LOCK and adds the object to
2494 * This function takes ownership of obj.
2496 static GstFlowReturn
2497 gst_base_sink_queue_object (GstBaseSink * basesink, GstPad * pad,
2498 GstMiniObject * obj, gboolean prerollable)
2502 GST_PAD_PREROLL_LOCK (pad);
2503 if (G_UNLIKELY (basesink->flushing))
2506 if (G_UNLIKELY (basesink->priv->received_eos))
2509 ret = gst_base_sink_queue_object_unlocked (basesink, pad, obj, prerollable);
2510 GST_PAD_PREROLL_UNLOCK (pad);
2517 GST_DEBUG_OBJECT (basesink, "sink is flushing");
2518 GST_PAD_PREROLL_UNLOCK (pad);
2519 gst_mini_object_unref (obj);
2520 return GST_FLOW_WRONG_STATE;
2524 GST_DEBUG_OBJECT (basesink,
2525 "we are EOS, dropping object, return UNEXPECTED");
2526 GST_PAD_PREROLL_UNLOCK (pad);
2527 gst_mini_object_unref (obj);
2528 return GST_FLOW_UNEXPECTED;
2533 gst_base_sink_event (GstPad * pad, GstEvent * event)
2535 GstBaseSink *basesink;
2536 gboolean result = TRUE;
2537 GstBaseSinkClass *bclass;
2539 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
2541 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2543 GST_DEBUG_OBJECT (basesink, "event %p (%s)", event,
2544 GST_EVENT_TYPE_NAME (event));
2546 switch (GST_EVENT_TYPE (event)) {
2551 GST_PAD_PREROLL_LOCK (pad);
2552 if (G_UNLIKELY (basesink->flushing))
2555 if (G_UNLIKELY (basesink->priv->received_eos)) {
2556 /* we can't accept anything when we are EOS */
2558 gst_event_unref (event);
2560 /* we set the received EOS flag here so that we can use it when testing if
2561 * we are prerolled and to refure more buffers. */
2562 basesink->priv->received_eos = TRUE;
2564 /* EOS is a prerollable object, we call the unlocked version because it
2565 * does not check the received_eos flag. */
2566 ret = gst_base_sink_queue_object_unlocked (basesink, pad,
2567 GST_MINI_OBJECT_CAST (event), TRUE);
2568 if (G_UNLIKELY (ret != GST_FLOW_OK))
2571 GST_PAD_PREROLL_UNLOCK (pad);
2574 case GST_EVENT_NEWSEGMENT:
2578 GST_DEBUG_OBJECT (basesink, "newsegment %p", event);
2580 GST_PAD_PREROLL_LOCK (pad);
2581 if (G_UNLIKELY (basesink->flushing))
2584 if (G_UNLIKELY (basesink->priv->received_eos)) {
2585 /* we can't accept anything when we are EOS */
2587 gst_event_unref (event);
2589 /* the new segment is a non prerollable item and does not block anything,
2590 * we need to configure the current clipping segment and insert the event
2591 * in the queue to serialize it with the buffers for rendering. */
2592 gst_base_sink_configure_segment (basesink, pad, event,
2593 basesink->abidata.ABI.clip_segment);
2596 gst_base_sink_queue_object_unlocked (basesink, pad,
2597 GST_MINI_OBJECT_CAST (event), FALSE);
2598 if (G_UNLIKELY (ret != GST_FLOW_OK))
2601 GST_OBJECT_LOCK (basesink);
2602 basesink->have_newsegment = TRUE;
2603 GST_OBJECT_UNLOCK (basesink);
2606 GST_PAD_PREROLL_UNLOCK (pad);
2609 case GST_EVENT_FLUSH_START:
2611 bclass->event (basesink, event);
2613 GST_DEBUG_OBJECT (basesink, "flush-start %p", event);
2615 /* make sure we are not blocked on the clock also clear any pending
2617 gst_base_sink_set_flushing (basesink, pad, TRUE);
2619 /* we grab the stream lock but that is not needed since setting the
2620 * sink to flushing would make sure no state commit is being done
2622 GST_PAD_STREAM_LOCK (pad);
2623 gst_base_sink_reset_qos (basesink);
2624 if (basesink->priv->async_enabled) {
2625 /* and we need to commit our state again on the next
2626 * prerolled buffer */
2627 basesink->playing_async = TRUE;
2628 gst_element_lost_state (GST_ELEMENT_CAST (basesink));
2630 basesink->priv->have_latency = TRUE;
2631 basesink->need_preroll = FALSE;
2633 gst_base_sink_set_last_buffer (basesink, NULL);
2634 GST_PAD_STREAM_UNLOCK (pad);
2636 gst_event_unref (event);
2638 case GST_EVENT_FLUSH_STOP:
2640 bclass->event (basesink, event);
2642 GST_DEBUG_OBJECT (basesink, "flush-stop %p", event);
2644 /* unset flushing so we can accept new data, this also flushes out any EOS
2646 gst_base_sink_set_flushing (basesink, pad, FALSE);
2648 /* for position reporting */
2649 GST_OBJECT_LOCK (basesink);
2650 basesink->priv->current_sstart = -1;
2651 basesink->priv->current_sstop = -1;
2652 basesink->priv->eos_rtime = -1;
2653 basesink->have_newsegment = FALSE;
2654 GST_OBJECT_UNLOCK (basesink);
2656 /* we need new segment info after the flush. */
2657 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
2658 gst_segment_init (basesink->abidata.ABI.clip_segment,
2659 GST_FORMAT_UNDEFINED);
2661 gst_event_unref (event);
2664 /* other events are sent to queue or subclass depending on if they
2665 * are serialized. */
2666 if (GST_EVENT_IS_SERIALIZED (event)) {
2667 gst_base_sink_queue_object (basesink, pad,
2668 GST_MINI_OBJECT_CAST (event), FALSE);
2671 bclass->event (basesink, event);
2672 gst_event_unref (event);
2677 gst_object_unref (basesink);
2684 GST_DEBUG_OBJECT (basesink, "we are flushing");
2685 GST_PAD_PREROLL_UNLOCK (pad);
2687 gst_event_unref (event);
2692 /* default implementation to calculate the start and end
2693 * timestamps on a buffer, subclasses can override
2696 gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
2697 GstClockTime * start, GstClockTime * end)
2699 GstClockTime timestamp, duration;
2701 timestamp = GST_BUFFER_TIMESTAMP (buffer);
2702 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
2704 /* get duration to calculate end time */
2705 duration = GST_BUFFER_DURATION (buffer);
2706 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2707 *end = timestamp + duration;
2713 /* must be called with PREROLL_LOCK */
2715 gst_base_sink_needs_preroll (GstBaseSink * basesink)
2717 gboolean is_prerolled, res;
2719 /* we have 2 cases where the PREROLL_LOCK is released:
2720 * 1) we are blocking in the PREROLL_LOCK and thus are prerolled.
2721 * 2) we are syncing on the clock
2723 is_prerolled = basesink->have_preroll || basesink->priv->received_eos;
2724 res = !is_prerolled;
2726 GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d => needs preroll: %d",
2727 basesink->have_preroll, basesink->priv->received_eos, res);
2732 /* with STREAM_LOCK, PREROLL_LOCK
2734 * Takes a buffer and compare the timestamps with the last segment.
2735 * If the buffer falls outside of the segment boundaries, drop it.
2736 * Else queue the buffer for preroll and rendering.
2738 * This function takes ownership of the buffer.
2740 static GstFlowReturn
2741 gst_base_sink_chain_unlocked (GstBaseSink * basesink, GstPad * pad,
2744 GstBaseSinkClass *bclass;
2745 GstFlowReturn result;
2746 GstClockTime start = GST_CLOCK_TIME_NONE, end = GST_CLOCK_TIME_NONE;
2747 GstSegment *clip_segment;
2749 if (G_UNLIKELY (basesink->flushing))
2752 if (G_UNLIKELY (basesink->priv->received_eos))
2755 /* for code clarity */
2756 clip_segment = basesink->abidata.ABI.clip_segment;
2758 if (G_UNLIKELY (!basesink->have_newsegment)) {
2761 sync = gst_base_sink_get_sync (basesink);
2763 GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
2764 (_("Internal data flow problem.")),
2765 ("Received buffer without a new-segment. Assuming timestamps start from 0."));
2768 /* this means this sink will assume timestamps start from 0 */
2769 GST_OBJECT_LOCK (basesink);
2770 clip_segment->start = 0;
2771 clip_segment->stop = -1;
2772 basesink->segment.start = 0;
2773 basesink->segment.stop = -1;
2774 basesink->have_newsegment = TRUE;
2775 GST_OBJECT_UNLOCK (basesink);
2778 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2780 /* check if the buffer needs to be dropped, we first ask the subclass for the
2782 if (bclass->get_times)
2783 bclass->get_times (basesink, buf, &start, &end);
2786 /* if the subclass does not want sync, we use our own values so that we at
2787 * least clip the buffer to the segment */
2788 gst_base_sink_get_times (basesink, buf, &start, &end);
2791 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
2792 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
2794 /* a dropped buffer does not participate in anything */
2795 if (GST_CLOCK_TIME_IS_VALID (start) &&
2796 (clip_segment->format == GST_FORMAT_TIME)) {
2797 if (G_UNLIKELY (!gst_segment_clip (clip_segment,
2798 GST_FORMAT_TIME, (gint64) start, (gint64) end, NULL, NULL)))
2799 goto out_of_segment;
2802 /* now we can process the buffer in the queue, this function takes ownership
2804 result = gst_base_sink_queue_object_unlocked (basesink, pad,
2805 GST_MINI_OBJECT_CAST (buf), TRUE);
2812 GST_DEBUG_OBJECT (basesink, "sink is flushing");
2813 gst_buffer_unref (buf);
2814 return GST_FLOW_WRONG_STATE;
2818 GST_DEBUG_OBJECT (basesink,
2819 "we are EOS, dropping object, return UNEXPECTED");
2820 gst_buffer_unref (buf);
2821 return GST_FLOW_UNEXPECTED;
2825 GST_DEBUG_OBJECT (basesink, "dropping buffer, out of clipping segment");
2826 gst_buffer_unref (buf);
2833 static GstFlowReturn
2834 gst_base_sink_chain (GstPad * pad, GstBuffer * buf)
2836 GstBaseSink *basesink;
2837 GstFlowReturn result;
2839 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
2841 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH))
2844 GST_PAD_PREROLL_LOCK (pad);
2845 result = gst_base_sink_chain_unlocked (basesink, pad, buf);
2846 GST_PAD_PREROLL_UNLOCK (pad);
2854 GST_OBJECT_LOCK (pad);
2855 GST_WARNING_OBJECT (basesink,
2856 "Push on pad %s:%s, but it was not activated in push mode",
2857 GST_DEBUG_PAD_NAME (pad));
2858 GST_OBJECT_UNLOCK (pad);
2859 gst_buffer_unref (buf);
2860 /* we don't post an error message this will signal to the peer
2861 * pushing that EOS is reached. */
2862 result = GST_FLOW_UNEXPECTED;
2870 gst_base_sink_loop (GstPad * pad)
2872 GstBaseSink *basesink;
2873 GstBuffer *buf = NULL;
2874 GstFlowReturn result;
2877 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
2879 g_assert (basesink->pad_mode == GST_ACTIVATE_PULL);
2881 if ((blocksize = basesink->priv->blocksize) == 0)
2884 GST_DEBUG_OBJECT (basesink, "pulling %" G_GUINT64_FORMAT ", %u",
2885 basesink->offset, blocksize);
2887 result = gst_pad_pull_range (pad, basesink->offset, blocksize, &buf);
2888 if (G_UNLIKELY (result != GST_FLOW_OK))
2891 if (G_UNLIKELY (buf == NULL))
2894 basesink->offset += GST_BUFFER_SIZE (buf);
2896 GST_PAD_PREROLL_LOCK (pad);
2897 result = gst_base_sink_chain_unlocked (basesink, pad, buf);
2898 GST_PAD_PREROLL_UNLOCK (pad);
2899 if (G_UNLIKELY (result != GST_FLOW_OK))
2907 GST_LOG_OBJECT (basesink, "pausing task, reason %s",
2908 gst_flow_get_name (result));
2909 gst_pad_pause_task (pad);
2910 /* fatal errors and NOT_LINKED cause EOS */
2911 if (GST_FLOW_IS_FATAL (result) || result == GST_FLOW_NOT_LINKED) {
2912 /* FIXME, we shouldn't post EOS when we are operating in segment mode */
2913 gst_base_sink_event (pad, gst_event_new_eos ());
2914 /* EOS does not cause an ERROR message */
2915 if (result != GST_FLOW_UNEXPECTED) {
2916 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
2917 (_("Internal data stream error.")),
2918 ("stream stopped, reason %s", gst_flow_get_name (result)));
2925 GST_LOG_OBJECT (basesink, "no buffer, pausing");
2926 result = GST_FLOW_ERROR;
2932 gst_base_sink_set_flushing (GstBaseSink * basesink, GstPad * pad,
2935 GstBaseSinkClass *bclass;
2937 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2940 /* unlock any subclasses, we need to do this before grabbing the
2941 * PREROLL_LOCK since we hold this lock before going into ::render. */
2943 bclass->unlock (basesink);
2946 GST_PAD_PREROLL_LOCK (pad);
2947 basesink->flushing = flushing;
2949 /* step 1, now that we have the PREROLL lock, clear our unlock request */
2950 if (bclass->unlock_stop)
2951 bclass->unlock_stop (basesink);
2953 /* set need_preroll before we unblock the clock. If the clock is unblocked
2954 * before timing out, we can reuse the buffer for preroll. */
2955 basesink->need_preroll = TRUE;
2957 /* step 2, unblock clock sync (if any) or any other blocking thing */
2958 if (basesink->clock_id) {
2959 gst_clock_id_unschedule (basesink->clock_id);
2962 /* flush out the data thread if it's locked in finish_preroll, this will
2963 * also flush out the EOS state */
2964 GST_DEBUG_OBJECT (basesink,
2965 "flushing out data thread, need preroll to TRUE");
2966 gst_base_sink_preroll_queue_flush (basesink, pad);
2968 GST_PAD_PREROLL_UNLOCK (pad);
2974 gst_base_sink_default_activate_pull (GstBaseSink * basesink, gboolean active)
2980 result = gst_pad_start_task (basesink->sinkpad,
2981 (GstTaskFunction) gst_base_sink_loop, basesink->sinkpad);
2983 /* step 2, make sure streaming finishes */
2984 result = gst_pad_stop_task (basesink->sinkpad);
2991 gst_base_sink_pad_activate (GstPad * pad)
2993 gboolean result = FALSE;
2994 GstBaseSink *basesink;
2996 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
2998 GST_DEBUG_OBJECT (basesink, "Trying pull mode first");
3000 gst_base_sink_set_flushing (basesink, pad, FALSE);
3002 /* we need to have the pull mode enabled */
3003 if (!basesink->can_activate_pull) {
3004 GST_DEBUG_OBJECT (basesink, "pull mode disabled");
3008 /* check if downstreams supports pull mode at all */
3009 if (!gst_pad_check_pull_range (pad)) {
3010 GST_DEBUG_OBJECT (basesink, "pull mode not supported");
3014 /* set the pad mode before starting the task so that it's in the
3015 * correct state for the new thread. also the sink set_caps and get_caps
3016 * function checks this */
3017 basesink->pad_mode = GST_ACTIVATE_PULL;
3019 /* we first try to negotiate a format so that when we try to activate
3020 * downstream, it knows about our format */
3021 if (!gst_base_sink_negotiate_pull (basesink)) {
3022 GST_DEBUG_OBJECT (basesink, "failed to negotiate in pull mode");
3026 /* ok activate now */
3027 if (!gst_pad_activate_pull (pad, TRUE)) {
3028 /* clear any pending caps */
3029 GST_OBJECT_LOCK (basesink);
3030 gst_caps_replace (&basesink->priv->pull_caps, NULL);
3031 GST_OBJECT_UNLOCK (basesink);
3032 GST_DEBUG_OBJECT (basesink, "failed to activate in pull mode");
3036 GST_DEBUG_OBJECT (basesink, "Success activating pull mode");
3040 /* push mode fallback */
3042 GST_DEBUG_OBJECT (basesink, "Falling back to push mode");
3043 if ((result = gst_pad_activate_push (pad, TRUE))) {
3044 GST_DEBUG_OBJECT (basesink, "Success activating push mode");
3049 GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
3050 gst_base_sink_set_flushing (basesink, pad, TRUE);
3053 gst_object_unref (basesink);
3059 gst_base_sink_pad_activate_push (GstPad * pad, gboolean active)
3062 GstBaseSink *basesink;
3064 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3067 if (!basesink->can_activate_push) {
3069 basesink->pad_mode = GST_ACTIVATE_NONE;
3072 basesink->pad_mode = GST_ACTIVATE_PUSH;
3075 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH)) {
3076 g_warning ("Internal GStreamer activation error!!!");
3079 gst_base_sink_set_flushing (basesink, pad, TRUE);
3081 basesink->pad_mode = GST_ACTIVATE_NONE;
3085 gst_object_unref (basesink);
3091 gst_base_sink_negotiate_pull (GstBaseSink * basesink)
3098 /* this returns the intersection between our caps and the peer caps. If there
3099 * is no peer, it returns NULL and we can't operate in pull mode so we can
3100 * fail the negotiation. */
3101 caps = gst_pad_get_allowed_caps (GST_BASE_SINK_PAD (basesink));
3102 if (caps == NULL || gst_caps_is_empty (caps))
3103 goto no_caps_possible;
3105 GST_DEBUG_OBJECT (basesink, "allowed caps: %" GST_PTR_FORMAT, caps);
3107 caps = gst_caps_make_writable (caps);
3108 /* get the first (prefered) format */
3109 gst_caps_truncate (caps);
3111 gst_pad_fixate_caps (GST_BASE_SINK_PAD (basesink), caps);
3113 GST_DEBUG_OBJECT (basesink, "fixated to: %" GST_PTR_FORMAT, caps);
3115 if (gst_caps_is_any (caps)) {
3116 GST_DEBUG_OBJECT (basesink, "caps were ANY after fixating, "
3118 /* neither side has template caps in this case, so they are prepared for
3119 pull() without setcaps() */
3121 } else if (gst_caps_is_fixed (caps)) {
3122 if (!gst_pad_set_caps (GST_BASE_SINK_PAD (basesink), caps))
3123 goto could_not_set_caps;
3125 GST_OBJECT_LOCK (basesink);
3126 gst_caps_replace (&basesink->priv->pull_caps, caps);
3127 GST_OBJECT_UNLOCK (basesink);
3132 gst_caps_unref (caps);
3138 GST_INFO_OBJECT (basesink, "Pipeline could not agree on caps");
3139 GST_DEBUG_OBJECT (basesink, "get_allowed_caps() returned EMPTY");
3141 gst_caps_unref (caps);
3146 GST_INFO_OBJECT (basesink, "Could not set caps: %" GST_PTR_FORMAT, caps);
3147 gst_caps_unref (caps);
3152 /* this won't get called until we implement an activate function */
3154 gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active)
3156 gboolean result = FALSE;
3157 GstBaseSink *basesink;
3158 GstBaseSinkClass *bclass;
3160 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3161 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3164 /* we mark we have a newsegment here because pull based
3165 * mode works just fine without having a newsegment before the
3167 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
3168 gst_segment_init (basesink->abidata.ABI.clip_segment, GST_FORMAT_UNDEFINED);
3169 GST_OBJECT_LOCK (basesink);
3170 basesink->have_newsegment = TRUE;
3171 GST_OBJECT_UNLOCK (basesink);
3173 if (bclass->activate_pull)
3174 result = bclass->activate_pull (basesink, TRUE);
3178 /* but if starting the thread fails, set it back */
3180 basesink->pad_mode = GST_ACTIVATE_NONE;
3182 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PULL)) {
3183 g_warning ("Internal GStreamer activation error!!!");
3186 result = gst_base_sink_set_flushing (basesink, pad, TRUE);
3187 if (bclass->activate_pull)
3188 result &= bclass->activate_pull (basesink, FALSE);
3189 basesink->pad_mode = GST_ACTIVATE_NONE;
3190 /* clear any pending caps */
3191 GST_OBJECT_LOCK (basesink);
3192 gst_caps_replace (&basesink->priv->pull_caps, NULL);
3193 GST_OBJECT_UNLOCK (basesink);
3196 gst_object_unref (basesink);
3201 /* send an event to our sinkpad peer. */
3203 gst_base_sink_send_event (GstElement * element, GstEvent * event)
3206 GstBaseSink *basesink = GST_BASE_SINK (element);
3207 gboolean forward, result = TRUE;
3209 /* only push UPSTREAM events upstream */
3210 forward = GST_EVENT_IS_UPSTREAM (event);
3212 switch (GST_EVENT_TYPE (event)) {
3213 case GST_EVENT_LATENCY:
3215 GstClockTime latency;
3217 gst_event_parse_latency (event, &latency);
3219 /* store the latency. We use this to adjust the running_time before syncing
3220 * it to the clock. */
3221 GST_OBJECT_LOCK (element);
3222 basesink->priv->latency = latency;
3223 GST_OBJECT_UNLOCK (element);
3224 GST_DEBUG_OBJECT (basesink, "latency set to %" GST_TIME_FORMAT,
3225 GST_TIME_ARGS (latency));
3227 /* We forward this event so that all elements know about the global pipeline
3228 * latency. This is interesting for an element when it wants to figure out
3229 * when a particular piece of data will be rendered. */
3237 GST_OBJECT_LOCK (element);
3238 pad = gst_object_ref (basesink->sinkpad);
3239 GST_OBJECT_UNLOCK (element);
3241 result = gst_pad_push_event (pad, event);
3243 gst_object_unref (pad);
3245 /* not forwarded, unref the event */
3246 gst_event_unref (event);
3252 gst_base_sink_peer_query (GstBaseSink * sink, GstQuery * query)
3255 gboolean res = FALSE;
3257 if ((peer = gst_pad_get_peer (sink->sinkpad))) {
3258 res = gst_pad_query (peer, query);
3259 gst_object_unref (peer);
3264 /* get the end position of the last seen object, this is used
3265 * for EOS and for making sure that we don't report a position we
3266 * have not reached yet. */
3268 gst_base_sink_get_position_last (GstBaseSink * basesink, gint64 * cur)
3270 /* return last observed stream time */
3271 *cur = basesink->priv->current_sstop;
3273 GST_DEBUG_OBJECT (basesink, "POSITION: %" GST_TIME_FORMAT,
3274 GST_TIME_ARGS (*cur));
3278 /* get the position when we are PAUSED, this is the stream time of the buffer
3279 * that prerolled. If no buffer is prerolled (we are still flushing), this
3280 * value will be -1. */
3282 gst_base_sink_get_position_paused (GstBaseSink * basesink, gint64 * cur)
3286 GstSegment *segment;
3288 *cur = basesink->priv->current_sstart;
3289 segment = basesink->abidata.ABI.clip_segment;
3291 time = segment->time;
3294 *cur = MAX (*cur, time);
3295 GST_DEBUG_OBJECT (basesink, "POSITION as max: %" GST_TIME_FORMAT
3296 ", time %" GST_TIME_FORMAT, GST_TIME_ARGS (*cur), GST_TIME_ARGS (time));
3298 /* we have no buffer, use the segment times. */
3299 if (segment->rate >= 0.0) {
3300 /* forward, next position is always the time of the segment */
3302 GST_DEBUG_OBJECT (basesink, "POSITION as time: %" GST_TIME_FORMAT,
3303 GST_TIME_ARGS (*cur));
3305 /* reverse, next expected timestamp is segment->stop. We use the function
3306 * to get things right for negative applied_rates. */
3308 gst_segment_to_stream_time (segment, GST_FORMAT_TIME, segment->stop);
3309 GST_DEBUG_OBJECT (basesink, "reverse POSITION: %" GST_TIME_FORMAT,
3310 GST_TIME_ARGS (*cur));
3319 gst_base_sink_get_position (GstBaseSink * basesink, GstFormat format,
3320 gint64 * cur, gboolean * upstream)
3323 gboolean res = FALSE;
3326 /* we can answer time format */
3327 case GST_FORMAT_TIME:
3329 GstClockTime now, base, latency;
3330 gint64 time, accum, duration;
3334 GST_OBJECT_LOCK (basesink);
3336 /* can only give answer based on the clock if not EOS */
3337 if (G_UNLIKELY (basesink->eos))
3340 /* we can only get the segment when we are not NULL or READY */
3341 if (!basesink->have_newsegment)
3344 /* when not in PLAYING or when we're busy with a state change, we
3345 * cannot read from the clock so we report time based on the
3346 * last seen timestamp. */
3347 if (GST_STATE (basesink) != GST_STATE_PLAYING ||
3348 GST_STATE_PENDING (basesink) != GST_STATE_VOID_PENDING)
3351 /* we need to sync on the clock. */
3352 if (basesink->sync == FALSE)
3355 /* and we need a clock */
3356 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (basesink)) == NULL))
3359 /* collect all data we need holding the lock */
3360 if (GST_CLOCK_TIME_IS_VALID (basesink->segment.time))
3361 time = basesink->segment.time;
3365 if (GST_CLOCK_TIME_IS_VALID (basesink->segment.stop))
3366 duration = basesink->segment.stop - basesink->segment.start;
3370 base = GST_ELEMENT_CAST (basesink)->base_time;
3371 accum = basesink->segment.accum;
3372 rate = basesink->segment.rate * basesink->segment.applied_rate;
3373 gst_base_sink_get_position_last (basesink, &last);
3374 latency = basesink->priv->latency;
3376 gst_object_ref (clock);
3377 /* need to release the object lock before we can get the time,
3378 * a clock might take the LOCK of the provider, which could be
3379 * a basesink subclass. */
3380 GST_OBJECT_UNLOCK (basesink);
3382 now = gst_clock_get_time (clock);
3384 /* subtract base time and accumulated time from the clock time.
3385 * Make sure we don't go negative. This is the current time in
3386 * the segment which we need to scale with the combined
3387 * rate and applied rate. */
3390 base = MIN (now, base);
3392 /* for negative rates we need to count back from from the segment
3397 *cur = time + gst_guint64_to_gdouble (now - base) * rate;
3399 /* never report more than last seen position */
3401 *cur = MIN (last, *cur);
3403 gst_object_unref (clock);
3407 GST_DEBUG_OBJECT (basesink,
3408 "now %" GST_TIME_FORMAT " - base %" GST_TIME_FORMAT " - accum %"
3409 GST_TIME_FORMAT " + time %" GST_TIME_FORMAT,
3410 GST_TIME_ARGS (now), GST_TIME_ARGS (base),
3411 GST_TIME_ARGS (accum), GST_TIME_ARGS (time));
3415 /* cannot answer other than TIME, ask to send the query upstream. */
3421 GST_DEBUG_OBJECT (basesink, "res: %d, POSITION: %" GST_TIME_FORMAT,
3422 res, GST_TIME_ARGS (*cur));
3428 GST_DEBUG_OBJECT (basesink, "position in EOS");
3429 res = gst_base_sink_get_position_last (basesink, cur);
3430 GST_OBJECT_UNLOCK (basesink);
3435 GST_DEBUG_OBJECT (basesink, "position in PAUSED");
3436 res = gst_base_sink_get_position_paused (basesink, cur);
3437 GST_OBJECT_UNLOCK (basesink);
3442 /* in NULL or READY we always return FALSE and -1 */
3443 GST_DEBUG_OBJECT (basesink, "position in wrong state, return -1");
3446 GST_OBJECT_UNLOCK (basesink);
3451 /* report last seen timestamp if any, else ask upstream to answer */
3452 if ((*cur = basesink->priv->current_sstart) != -1)
3457 GST_DEBUG_OBJECT (basesink, "no sync, res %d, POSITION %" GST_TIME_FORMAT,
3458 res, GST_TIME_ARGS (*cur));
3459 GST_OBJECT_UNLOCK (basesink);
3465 gst_base_sink_query (GstElement * element, GstQuery * query)
3467 gboolean res = FALSE;
3469 GstBaseSink *basesink = GST_BASE_SINK (element);
3471 switch (GST_QUERY_TYPE (query)) {
3472 case GST_QUERY_POSITION:
3476 gboolean upstream = FALSE;
3478 gst_query_parse_position (query, &format, NULL);
3480 GST_DEBUG_OBJECT (basesink, "position format %d", format);
3482 /* first try to get the position based on the clock */
3484 gst_base_sink_get_position (basesink, format, &cur, &upstream))) {
3485 gst_query_set_position (query, format, cur);
3486 } else if (upstream) {
3487 /* fallback to peer query */
3488 res = gst_base_sink_peer_query (basesink, query);
3492 case GST_QUERY_DURATION:
3493 GST_DEBUG_OBJECT (basesink, "duration query");
3494 res = gst_base_sink_peer_query (basesink, query);
3496 case GST_QUERY_LATENCY:
3498 gboolean live, us_live;
3499 GstClockTime min, max;
3501 if ((res = gst_base_sink_query_latency (basesink, &live, &us_live, &min,
3503 gst_query_set_latency (query, live, min, max);
3507 case GST_QUERY_JITTER:
3509 case GST_QUERY_RATE:
3510 /* gst_query_set_rate (query, basesink->segment_rate); */
3513 case GST_QUERY_SEGMENT:
3515 /* FIXME, bring start/stop to stream time */
3516 gst_query_set_segment (query, basesink->segment.rate,
3517 GST_FORMAT_TIME, basesink->segment.start, basesink->segment.stop);
3520 case GST_QUERY_SEEKING:
3521 case GST_QUERY_CONVERT:
3522 case GST_QUERY_FORMATS:
3524 res = gst_base_sink_peer_query (basesink, query);
3530 static GstStateChangeReturn
3531 gst_base_sink_change_state (GstElement * element, GstStateChange transition)
3533 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
3534 GstBaseSink *basesink = GST_BASE_SINK (element);
3535 GstBaseSinkClass *bclass;
3536 GstBaseSinkPrivate *priv;
3538 priv = basesink->priv;
3540 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3542 switch (transition) {
3543 case GST_STATE_CHANGE_NULL_TO_READY:
3545 if (!bclass->start (basesink))
3548 case GST_STATE_CHANGE_READY_TO_PAUSED:
3549 /* need to complete preroll before this state change completes, there
3550 * is no data flow in READY so we can safely assume we need to preroll. */
3551 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
3552 GST_DEBUG_OBJECT (basesink, "READY to PAUSED");
3553 basesink->have_newsegment = FALSE;
3554 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
3555 gst_segment_init (basesink->abidata.ABI.clip_segment,
3556 GST_FORMAT_UNDEFINED);
3557 basesink->offset = 0;
3558 basesink->have_preroll = FALSE;
3559 basesink->need_preroll = TRUE;
3560 basesink->playing_async = TRUE;
3561 priv->current_sstart = -1;
3562 priv->current_sstop = -1;
3563 priv->eos_rtime = -1;
3565 basesink->eos = FALSE;
3566 priv->received_eos = FALSE;
3567 gst_base_sink_reset_qos (basesink);
3568 priv->commited = FALSE;
3569 if (priv->async_enabled) {
3570 GST_DEBUG_OBJECT (basesink, "doing async state change");
3571 /* when async enabled, post async-start message and return ASYNC from
3572 * the state change function */
3573 ret = GST_STATE_CHANGE_ASYNC;
3574 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3575 gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
3577 priv->have_latency = TRUE;
3579 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
3581 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3582 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
3583 if (!gst_base_sink_needs_preroll (basesink)) {
3584 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, don't need preroll");
3585 /* no preroll needed anymore now. */
3586 basesink->playing_async = FALSE;
3587 basesink->need_preroll = FALSE;
3588 if (basesink->eos) {
3589 /* need to post EOS message here */
3590 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
3591 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3592 gst_message_new_eos (GST_OBJECT_CAST (basesink)));
3594 GST_DEBUG_OBJECT (basesink, "signal preroll");
3595 GST_PAD_PREROLL_SIGNAL (basesink->sinkpad);
3598 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, we are not prerolled");
3599 basesink->need_preroll = TRUE;
3600 basesink->playing_async = TRUE;
3601 priv->commited = FALSE;
3602 if (priv->async_enabled) {
3603 GST_DEBUG_OBJECT (basesink, "doing async state change");
3604 ret = GST_STATE_CHANGE_ASYNC;
3605 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3606 gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
3609 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
3616 GstStateChangeReturn bret;
3618 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3619 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
3620 goto activate_failed;
3623 switch (transition) {
3624 case GST_STATE_CHANGE_READY_TO_PAUSED:
3626 /* note that this is the upward case, which doesn't follow most
3628 if (basesink->pad_mode == GST_ACTIVATE_PULL) {
3629 GST_DEBUG_OBJECT (basesink, "basesink activated in pull mode, "
3630 "returning SUCCESS directly");
3631 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
3632 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3633 gst_message_new_async_done (GST_OBJECT_CAST (basesink)));
3634 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
3635 ret = GST_STATE_CHANGE_SUCCESS;
3639 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3640 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED");
3641 /* FIXME, make sure we cannot enter _render first */
3643 /* we need to call ::unlock before locking PREROLL_LOCK
3644 * since we lock it before going into ::render */
3646 bclass->unlock (basesink);
3648 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
3649 /* now that we have the PREROLL lock, clear our unlock request */
3650 if (bclass->unlock_stop)
3651 bclass->unlock_stop (basesink);
3653 /* we need preroll again and we set the flag before unlocking the clockid
3654 * because if the clockid is unlocked before a current buffer expired, we
3655 * can use that buffer to preroll with */
3656 basesink->need_preroll = TRUE;
3658 if (basesink->clock_id) {
3659 gst_clock_id_unschedule (basesink->clock_id);
3662 /* if we don't have a preroll buffer we need to wait for a preroll and
3664 if (!gst_base_sink_needs_preroll (basesink)) {
3665 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, we are prerolled");
3666 basesink->playing_async = FALSE;
3668 if (GST_STATE_TARGET (GST_ELEMENT (basesink)) <= GST_STATE_READY) {
3669 ret = GST_STATE_CHANGE_SUCCESS;
3671 GST_DEBUG_OBJECT (basesink,
3672 "PLAYING to PAUSED, we are not prerolled");
3673 basesink->playing_async = TRUE;
3674 priv->commited = FALSE;
3675 if (priv->async_enabled) {
3676 GST_DEBUG_OBJECT (basesink, "doing async state change");
3677 ret = GST_STATE_CHANGE_ASYNC;
3678 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3679 gst_message_new_async_start (GST_OBJECT_CAST (basesink),
3684 GST_DEBUG_OBJECT (basesink, "rendered: %" G_GUINT64_FORMAT
3685 ", dropped: %" G_GUINT64_FORMAT, priv->rendered, priv->dropped);
3687 gst_base_sink_reset_qos (basesink);
3688 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
3690 case GST_STATE_CHANGE_PAUSED_TO_READY:
3691 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
3692 /* start by reseting our position state with the object lock so that the
3693 * position query gets the right idea. We do this before we post the
3694 * messages so that the message handlers pick this up. */
3695 GST_OBJECT_LOCK (basesink);
3696 basesink->have_newsegment = FALSE;
3697 priv->current_sstart = -1;
3698 priv->current_sstop = -1;
3699 priv->have_latency = FALSE;
3700 GST_OBJECT_UNLOCK (basesink);
3702 gst_base_sink_set_last_buffer (basesink, NULL);
3704 if (!priv->commited) {
3705 if (priv->async_enabled) {
3706 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, posting async-done");
3708 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3709 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
3710 GST_STATE_PLAYING, GST_STATE_PAUSED, GST_STATE_READY));
3712 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3713 gst_message_new_async_done (GST_OBJECT_CAST (basesink)));
3715 priv->commited = TRUE;
3717 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, don't need_preroll");
3719 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
3721 case GST_STATE_CHANGE_READY_TO_NULL:
3723 if (!bclass->stop (basesink)) {
3724 GST_WARNING_OBJECT (basesink, "failed to stop");
3727 gst_base_sink_set_last_buffer (basesink, NULL);
3738 GST_DEBUG_OBJECT (basesink, "failed to start");
3739 return GST_STATE_CHANGE_FAILURE;
3743 GST_DEBUG_OBJECT (basesink,
3744 "element failed to change states -- activation problem?");
3745 return GST_STATE_CHANGE_FAILURE;