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 */
223 #define DO_RUNNING_AVG(avg,val,size) (((val) + ((size)-1) * (avg)) / (size))
225 /* generic running average, this has a neutral window size */
226 #define UPDATE_RUNNING_AVG(avg,val) DO_RUNNING_AVG(avg,val,8)
228 /* the windows for these running averages are experimentally obtained.
229 * possitive values get averaged more while negative values use a small
230 * window so we can react faster to badness. */
231 #define UPDATE_RUNNING_AVG_P(avg,val) DO_RUNNING_AVG(avg,val,16)
232 #define UPDATE_RUNNING_AVG_N(avg,val) DO_RUNNING_AVG(avg,val,4)
234 /* BaseSink properties */
236 #define DEFAULT_CAN_ACTIVATE_PULL FALSE /* fixme: enable me */
237 #define DEFAULT_CAN_ACTIVATE_PUSH TRUE
239 #define DEFAULT_PREROLL_QUEUE_LEN 0
240 #define DEFAULT_SYNC TRUE
241 #define DEFAULT_MAX_LATENESS -1
242 #define DEFAULT_QOS FALSE
243 #define DEFAULT_ASYNC TRUE
244 #define DEFAULT_TS_OFFSET 0
245 #define DEFAULT_BLOCKSIZE 4096
250 PROP_PREROLL_QUEUE_LEN,
261 static GstElementClass *parent_class = NULL;
263 static void gst_base_sink_class_init (GstBaseSinkClass * klass);
264 static void gst_base_sink_init (GstBaseSink * trans, gpointer g_class);
265 static void gst_base_sink_finalize (GObject * object);
268 gst_base_sink_get_type (void)
270 static GType base_sink_type = 0;
272 if (G_UNLIKELY (base_sink_type == 0)) {
273 static const GTypeInfo base_sink_info = {
274 sizeof (GstBaseSinkClass),
277 (GClassInitFunc) gst_base_sink_class_init,
280 sizeof (GstBaseSink),
282 (GInstanceInitFunc) gst_base_sink_init,
285 base_sink_type = g_type_register_static (GST_TYPE_ELEMENT,
286 "GstBaseSink", &base_sink_info, G_TYPE_FLAG_ABSTRACT);
288 return base_sink_type;
291 static void gst_base_sink_set_property (GObject * object, guint prop_id,
292 const GValue * value, GParamSpec * pspec);
293 static void gst_base_sink_get_property (GObject * object, guint prop_id,
294 GValue * value, GParamSpec * pspec);
296 static gboolean gst_base_sink_send_event (GstElement * element,
298 static gboolean gst_base_sink_query (GstElement * element, GstQuery * query);
300 static GstCaps *gst_base_sink_get_caps (GstBaseSink * sink);
301 static gboolean gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps);
302 static GstFlowReturn gst_base_sink_buffer_alloc (GstBaseSink * sink,
303 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
304 static void gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
305 GstClockTime * start, GstClockTime * end);
306 static gboolean gst_base_sink_set_flushing (GstBaseSink * basesink,
307 GstPad * pad, gboolean flushing);
308 static gboolean gst_base_sink_default_activate_pull (GstBaseSink * basesink,
310 static gboolean gst_base_sink_default_do_seek (GstBaseSink * sink,
311 GstSegment * segment);
312 static gboolean gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
313 GstEvent * event, GstSegment * segment);
315 static GstStateChangeReturn gst_base_sink_change_state (GstElement * element,
316 GstStateChange transition);
318 static GstFlowReturn gst_base_sink_chain (GstPad * pad, GstBuffer * buffer);
319 static void gst_base_sink_loop (GstPad * pad);
320 static gboolean gst_base_sink_pad_activate (GstPad * pad);
321 static gboolean gst_base_sink_pad_activate_push (GstPad * pad, gboolean active);
322 static gboolean gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active);
323 static gboolean gst_base_sink_event (GstPad * pad, GstEvent * event);
324 static gboolean gst_base_sink_peer_query (GstBaseSink * sink, GstQuery * query);
326 static gboolean gst_base_sink_negotiate_pull (GstBaseSink * basesink);
328 /* check if an object was too late */
329 static gboolean gst_base_sink_is_too_late (GstBaseSink * basesink,
330 GstMiniObject * obj, GstClockTime start, GstClockTime stop,
331 GstClockReturn status, GstClockTimeDiff jitter);
334 gst_base_sink_class_init (GstBaseSinkClass * klass)
336 GObjectClass *gobject_class;
337 GstElementClass *gstelement_class;
339 gobject_class = G_OBJECT_CLASS (klass);
340 gstelement_class = GST_ELEMENT_CLASS (klass);
342 GST_DEBUG_CATEGORY_INIT (gst_base_sink_debug, "basesink", 0,
345 g_type_class_add_private (klass, sizeof (GstBaseSinkPrivate));
347 parent_class = g_type_class_peek_parent (klass);
349 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_sink_finalize);
350 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_base_sink_set_property);
351 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_base_sink_get_property);
353 /* FIXME, this next value should be configured using an event from the
354 * upstream element, ie, the BUFFER_SIZE event. */
355 g_object_class_install_property (gobject_class, PROP_PREROLL_QUEUE_LEN,
356 g_param_spec_uint ("preroll-queue-len", "Preroll queue length",
357 "Number of buffers to queue during preroll", 0, G_MAXUINT,
358 DEFAULT_PREROLL_QUEUE_LEN,
359 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
361 g_object_class_install_property (gobject_class, PROP_SYNC,
362 g_param_spec_boolean ("sync", "Sync", "Sync on the clock", DEFAULT_SYNC,
363 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
365 g_object_class_install_property (gobject_class, PROP_MAX_LATENESS,
366 g_param_spec_int64 ("max-lateness", "Max Lateness",
367 "Maximum number of nanoseconds that a buffer can be late before it "
368 "is dropped (-1 unlimited)", -1, G_MAXINT64, DEFAULT_MAX_LATENESS,
369 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
371 g_object_class_install_property (gobject_class, PROP_QOS,
372 g_param_spec_boolean ("qos", "Qos",
373 "Generate Quality-of-Service events upstream", DEFAULT_QOS,
374 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
378 * If set to #TRUE, the basesink will perform asynchronous state changes.
379 * When set to #FALSE, the sink will not signal the parent when it prerolls.
380 * Use this option when dealing with sparse streams or when synchronisation is
385 g_object_class_install_property (gobject_class, PROP_ASYNC,
386 g_param_spec_boolean ("async", "Async",
387 "Go asynchronously to PAUSED", DEFAULT_ASYNC,
388 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
390 * GstBaseSink:ts-offset
392 * Controls the final synchronisation, a negative value will render the buffer
393 * earlier while a positive value delays playback. This property can be
394 * used to fix synchronisation in bad files.
398 g_object_class_install_property (gobject_class, PROP_TS_OFFSET,
399 g_param_spec_int64 ("ts-offset", "TS Offset",
400 "Timestamp offset in nanoseconds", G_MININT64, G_MAXINT64,
401 DEFAULT_TS_OFFSET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
404 * GstBaseSink:last-buffer
406 * The last buffer that arrived in the sink and was used for preroll or for
407 * rendering. This property can be used to generate thumbnails. This property
408 * can be NULL when the sink has not yet received a bufer.
412 g_object_class_install_property (gobject_class, PROP_LAST_BUFFER,
413 gst_param_spec_mini_object ("last-buffer", "Last Buffer",
414 "The last buffer received in the sink", GST_TYPE_BUFFER,
415 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
418 * GstBaseSink:blocksize
420 * The amount of bytes to pull when operating in pull mode.
424 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
425 g_param_spec_uint ("blocksize", "Block size",
426 "Size in bytes to pull per buffer (0 = default)", 0, G_MAXUINT,
427 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
429 gstelement_class->change_state =
430 GST_DEBUG_FUNCPTR (gst_base_sink_change_state);
431 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_sink_send_event);
432 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_base_sink_query);
434 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_sink_get_caps);
435 klass->set_caps = GST_DEBUG_FUNCPTR (gst_base_sink_set_caps);
436 klass->buffer_alloc = GST_DEBUG_FUNCPTR (gst_base_sink_buffer_alloc);
437 klass->get_times = GST_DEBUG_FUNCPTR (gst_base_sink_get_times);
438 klass->activate_pull =
439 GST_DEBUG_FUNCPTR (gst_base_sink_default_activate_pull);
443 gst_base_sink_pad_getcaps (GstPad * pad)
445 GstBaseSinkClass *bclass;
447 GstCaps *caps = NULL;
449 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
450 bclass = GST_BASE_SINK_GET_CLASS (bsink);
452 if (bsink->pad_mode == GST_ACTIVATE_PULL) {
453 /* if we are operating in pull mode we only accept the negotiated caps */
454 GST_OBJECT_LOCK (pad);
455 if ((caps = GST_PAD_CAPS (pad)))
457 GST_OBJECT_UNLOCK (pad);
460 if (bclass->get_caps)
461 caps = bclass->get_caps (bsink);
464 GstPadTemplate *pad_template;
467 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass),
469 if (pad_template != NULL) {
470 caps = gst_caps_ref (gst_pad_template_get_caps (pad_template));
474 gst_object_unref (bsink);
480 gst_base_sink_pad_setcaps (GstPad * pad, GstCaps * caps)
482 GstBaseSinkClass *bclass;
486 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
487 bclass = GST_BASE_SINK_GET_CLASS (bsink);
489 if (res && bclass->set_caps)
490 res = bclass->set_caps (bsink, caps);
492 gst_object_unref (bsink);
498 gst_base_sink_pad_fixate (GstPad * pad, GstCaps * caps)
500 GstBaseSinkClass *bclass;
503 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
504 bclass = GST_BASE_SINK_GET_CLASS (bsink);
507 bclass->fixate (bsink, caps);
509 gst_object_unref (bsink);
513 gst_base_sink_pad_buffer_alloc (GstPad * pad, guint64 offset, guint size,
514 GstCaps * caps, GstBuffer ** buf)
516 GstBaseSinkClass *bclass;
518 GstFlowReturn result = GST_FLOW_OK;
520 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
521 bclass = GST_BASE_SINK_GET_CLASS (bsink);
523 if (bclass->buffer_alloc)
524 result = bclass->buffer_alloc (bsink, offset, size, caps, buf);
526 *buf = NULL; /* fallback in gstpad.c will allocate generic buffer */
528 gst_object_unref (bsink);
534 gst_base_sink_init (GstBaseSink * basesink, gpointer g_class)
536 GstPadTemplate *pad_template;
537 GstBaseSinkPrivate *priv;
539 basesink->priv = priv = GST_BASE_SINK_GET_PRIVATE (basesink);
542 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
543 g_return_if_fail (pad_template != NULL);
545 basesink->sinkpad = gst_pad_new_from_template (pad_template, "sink");
547 gst_pad_set_getcaps_function (basesink->sinkpad,
548 GST_DEBUG_FUNCPTR (gst_base_sink_pad_getcaps));
549 gst_pad_set_setcaps_function (basesink->sinkpad,
550 GST_DEBUG_FUNCPTR (gst_base_sink_pad_setcaps));
551 gst_pad_set_fixatecaps_function (basesink->sinkpad,
552 GST_DEBUG_FUNCPTR (gst_base_sink_pad_fixate));
553 gst_pad_set_bufferalloc_function (basesink->sinkpad,
554 GST_DEBUG_FUNCPTR (gst_base_sink_pad_buffer_alloc));
555 gst_pad_set_activate_function (basesink->sinkpad,
556 GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate));
557 gst_pad_set_activatepush_function (basesink->sinkpad,
558 GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate_push));
559 gst_pad_set_activatepull_function (basesink->sinkpad,
560 GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate_pull));
561 gst_pad_set_event_function (basesink->sinkpad,
562 GST_DEBUG_FUNCPTR (gst_base_sink_event));
563 gst_pad_set_chain_function (basesink->sinkpad,
564 GST_DEBUG_FUNCPTR (gst_base_sink_chain));
565 gst_element_add_pad (GST_ELEMENT_CAST (basesink), basesink->sinkpad);
567 basesink->pad_mode = GST_ACTIVATE_NONE;
568 basesink->preroll_queue = g_queue_new ();
569 basesink->abidata.ABI.clip_segment = gst_segment_new ();
570 priv->have_latency = FALSE;
572 basesink->can_activate_push = DEFAULT_CAN_ACTIVATE_PUSH;
573 basesink->can_activate_pull = DEFAULT_CAN_ACTIVATE_PULL;
575 basesink->sync = DEFAULT_SYNC;
576 basesink->abidata.ABI.max_lateness = DEFAULT_MAX_LATENESS;
577 g_atomic_int_set (&priv->qos_enabled, DEFAULT_QOS);
578 priv->async_enabled = DEFAULT_ASYNC;
579 priv->ts_offset = DEFAULT_TS_OFFSET;
580 priv->render_delay = 0;
581 priv->blocksize = DEFAULT_BLOCKSIZE;
583 GST_OBJECT_FLAG_SET (basesink, GST_ELEMENT_IS_SINK);
587 gst_base_sink_finalize (GObject * object)
589 GstBaseSink *basesink;
591 basesink = GST_BASE_SINK (object);
593 g_queue_free (basesink->preroll_queue);
594 gst_segment_free (basesink->abidata.ABI.clip_segment);
596 G_OBJECT_CLASS (parent_class)->finalize (object);
600 * gst_base_sink_set_sync:
602 * @sync: the new sync value.
604 * Configures @sink to synchronize on the clock or not. When
605 * @sync is FALSE, incomming samples will be played as fast as
606 * possible. If @sync is TRUE, the timestamps of the incomming
607 * buffers will be used to schedule the exact render time of its
613 gst_base_sink_set_sync (GstBaseSink * sink, gboolean sync)
615 g_return_if_fail (GST_IS_BASE_SINK (sink));
617 GST_OBJECT_LOCK (sink);
619 GST_OBJECT_UNLOCK (sink);
623 * gst_base_sink_get_sync:
626 * Checks if @sink is currently configured to synchronize against the
629 * Returns: TRUE if the sink is configured to synchronize against the clock.
634 gst_base_sink_get_sync (GstBaseSink * sink)
638 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
640 GST_OBJECT_LOCK (sink);
642 GST_OBJECT_UNLOCK (sink);
648 * gst_base_sink_set_max_lateness:
650 * @max_lateness: the new max lateness value.
652 * Sets the new max lateness value to @max_lateness. This value is
653 * used to decide if a buffer should be dropped or not based on the
654 * buffer timestamp and the current clock time. A value of -1 means
660 gst_base_sink_set_max_lateness (GstBaseSink * sink, gint64 max_lateness)
662 g_return_if_fail (GST_IS_BASE_SINK (sink));
664 GST_OBJECT_LOCK (sink);
665 sink->abidata.ABI.max_lateness = max_lateness;
666 GST_OBJECT_UNLOCK (sink);
670 * gst_base_sink_get_max_lateness:
673 * Gets the max lateness value. See gst_base_sink_set_max_lateness for
676 * Returns: The maximum time in nanoseconds that a buffer can be late
677 * before it is dropped and not rendered. A value of -1 means an
683 gst_base_sink_get_max_lateness (GstBaseSink * sink)
687 g_return_val_if_fail (GST_IS_BASE_SINK (sink), -1);
689 GST_OBJECT_LOCK (sink);
690 res = sink->abidata.ABI.max_lateness;
691 GST_OBJECT_UNLOCK (sink);
697 * gst_base_sink_set_qos_enabled:
699 * @enabled: the new qos value.
701 * Configures @sink to send Quality-of-Service events upstream.
706 gst_base_sink_set_qos_enabled (GstBaseSink * sink, gboolean enabled)
708 g_return_if_fail (GST_IS_BASE_SINK (sink));
710 g_atomic_int_set (&sink->priv->qos_enabled, enabled);
714 * gst_base_sink_is_qos_enabled:
717 * Checks if @sink is currently configured to send Quality-of-Service events
720 * Returns: TRUE if the sink is configured to perform Quality-of-Service.
725 gst_base_sink_is_qos_enabled (GstBaseSink * sink)
729 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
731 res = g_atomic_int_get (&sink->priv->qos_enabled);
737 * gst_base_sink_set_async_enabled:
739 * @enabled: the new async value.
741 * Configures @sink to perform all state changes asynchronusly. When async is
742 * disabled, the sink will immediatly go to PAUSED instead of waiting for a
743 * preroll buffer. This feature is usefull if the sink does not synchronize
744 * against the clock or when it is dealing with sparse streams.
749 gst_base_sink_set_async_enabled (GstBaseSink * sink, gboolean enabled)
751 g_return_if_fail (GST_IS_BASE_SINK (sink));
753 GST_PAD_PREROLL_LOCK (sink->sinkpad);
754 sink->priv->async_enabled = enabled;
755 GST_LOG_OBJECT (sink, "set async enabled to %d", enabled);
756 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
760 * gst_base_sink_is_async_enabled:
763 * Checks if @sink is currently configured to perform asynchronous state
766 * Returns: TRUE if the sink is configured to perform asynchronous state
772 gst_base_sink_is_async_enabled (GstBaseSink * sink)
776 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
778 GST_PAD_PREROLL_LOCK (sink->sinkpad);
779 res = sink->priv->async_enabled;
780 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
786 * gst_base_sink_set_ts_offset:
788 * @offset: the new offset
790 * Adjust the synchronisation of @sink with @offset. A negative value will
791 * render buffers earlier than their timestamp. A positive value will delay
792 * rendering. This function can be used to fix playback of badly timestamped
798 gst_base_sink_set_ts_offset (GstBaseSink * sink, GstClockTimeDiff offset)
800 g_return_if_fail (GST_IS_BASE_SINK (sink));
802 GST_OBJECT_LOCK (sink);
803 sink->priv->ts_offset = offset;
804 GST_LOG_OBJECT (sink, "set time offset to %" G_GINT64_FORMAT, offset);
805 GST_OBJECT_UNLOCK (sink);
809 * gst_base_sink_get_ts_offset:
812 * Get the synchronisation offset of @sink.
814 * Returns: The synchronisation offset.
819 gst_base_sink_get_ts_offset (GstBaseSink * sink)
821 GstClockTimeDiff res;
823 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
825 GST_OBJECT_LOCK (sink);
826 res = sink->priv->ts_offset;
827 GST_OBJECT_UNLOCK (sink);
833 * gst_base_sink_get_last_buffer:
836 * Get the last buffer that arrived in the sink and was used for preroll or for
837 * rendering. This property can be used to generate thumbnails.
839 * The #GstCaps on the buffer can be used to determine the type of the buffer.
841 * Returns: a #GstBuffer. gst_buffer_unref() after usage. This function returns
842 * NULL when no buffer has arrived in the sink yet or when the sink is not in
848 gst_base_sink_get_last_buffer (GstBaseSink * sink)
852 g_return_val_if_fail (GST_IS_BASE_SINK (sink), NULL);
854 GST_OBJECT_LOCK (sink);
855 if ((res = sink->priv->last_buffer))
856 gst_buffer_ref (res);
857 GST_OBJECT_UNLOCK (sink);
863 gst_base_sink_set_last_buffer (GstBaseSink * sink, GstBuffer * buffer)
868 gst_buffer_ref (buffer);
870 GST_OBJECT_LOCK (sink);
871 old = sink->priv->last_buffer;
872 sink->priv->last_buffer = buffer;
873 GST_OBJECT_UNLOCK (sink);
876 gst_buffer_unref (old);
880 * gst_base_sink_get_latency:
883 * Get the currently configured latency.
885 * Returns: The configured latency.
890 gst_base_sink_get_latency (GstBaseSink * sink)
894 GST_OBJECT_LOCK (sink);
895 res = sink->priv->latency;
896 GST_OBJECT_UNLOCK (sink);
902 * gst_base_sink_query_latency:
904 * @live: if the sink is live
905 * @upstream_live: if an upstream element is live
906 * @min_latency: the min latency of the upstream elements
907 * @max_latency: the max latency of the upstream elements
909 * Query the sink for the latency parameters. The latency will be queried from
910 * the upstream elements. @live will be TRUE if @sink is configured to
911 * synchronize against the clock. @upstream_live will be TRUE if an upstream
914 * If both @live and @upstream_live are TRUE, the sink will want to compensate
915 * for the latency introduced by the upstream elements by setting the
916 * @min_latency to a strictly possitive value.
918 * This function is mostly used by subclasses.
920 * Returns: TRUE if the query succeeded.
925 gst_base_sink_query_latency (GstBaseSink * sink, gboolean * live,
926 gboolean * upstream_live, GstClockTime * min_latency,
927 GstClockTime * max_latency)
929 gboolean l, us_live, res, have_latency;
930 GstClockTime min, max, render_delay;
932 GstClockTime us_min, us_max;
934 /* we are live when we sync to the clock */
935 GST_OBJECT_LOCK (sink);
937 have_latency = sink->priv->have_latency;
938 render_delay = sink->priv->render_delay;
939 GST_OBJECT_UNLOCK (sink);
941 /* assume no latency */
947 GST_DEBUG_OBJECT (sink, "we are ready for LATENCY query");
948 /* we are ready for a latency query this is when we preroll or when we are
950 query = gst_query_new_latency ();
952 /* ask the peer for the latency */
953 if ((res = gst_base_sink_peer_query (sink, query))) {
954 /* get upstream min and max latency */
955 gst_query_parse_latency (query, &us_live, &us_min, &us_max);
958 /* upstream live, use its latency, subclasses should use these
959 * values to create the complete latency. */
964 /* we need to add the render delay if we are live */
971 gst_query_unref (query);
973 GST_DEBUG_OBJECT (sink, "we are not yet ready for LATENCY query");
977 /* not live, we tried to do the query, if it failed we return TRUE anyway */
981 GST_DEBUG_OBJECT (sink, "latency query failed but we are not live");
983 GST_DEBUG_OBJECT (sink, "latency query failed and we are live");
988 GST_DEBUG_OBJECT (sink, "latency query: live: %d, have_latency %d,"
989 " upstream: %d, min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT, l,
990 have_latency, us_live, GST_TIME_ARGS (min), GST_TIME_ARGS (max));
995 *upstream_live = us_live;
1005 * gst_base_sink_set_render_delay:
1006 * @sink: a #GstBaseSink
1007 * @delay: the new delay
1009 * Set the render delay in @sink to @delay. The render delay is the time
1010 * between actual rendering of a buffer and its synchronisation time. Some
1011 * devices might delay media rendering which can be compensated for with this
1014 * After calling this function, this sink will report additional latency and
1015 * other sinks will adjust their latency to delay the rendering of their media.
1017 * This function is usually called by subclasses.
1022 gst_base_sink_set_render_delay (GstBaseSink * sink, GstClockTime delay)
1024 g_return_if_fail (GST_IS_BASE_SINK (sink));
1026 GST_OBJECT_LOCK (sink);
1027 sink->priv->render_delay = delay;
1028 GST_LOG_OBJECT (sink, "set render delay to %" GST_TIME_FORMAT,
1029 GST_TIME_ARGS (delay));
1030 GST_OBJECT_UNLOCK (sink);
1034 * gst_base_sink_get_render_delay:
1035 * @sink: a #GstBaseSink
1037 * Get the render delay of @sink. see gst_base_sink_set_render_delay() for more
1038 * information about the render delay.
1040 * Returns: the render delay of @sink.
1045 gst_base_sink_get_render_delay (GstBaseSink * sink)
1047 GstClockTimeDiff res;
1049 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1051 GST_OBJECT_LOCK (sink);
1052 res = sink->priv->render_delay;
1053 GST_OBJECT_UNLOCK (sink);
1059 * gst_base_sink_set_blocksize:
1060 * @sink: a #GstBaseSink
1061 * @blocksize: the blocksize in bytes
1063 * Set the number of bytes that the sink will pull when it is operating in pull
1069 gst_base_sink_set_blocksize (GstBaseSink * sink, guint blocksize)
1071 g_return_if_fail (GST_IS_BASE_SINK (sink));
1073 GST_OBJECT_LOCK (sink);
1074 sink->priv->blocksize = blocksize;
1075 GST_LOG_OBJECT (sink, "set blocksize to %u", blocksize);
1076 GST_OBJECT_UNLOCK (sink);
1080 * gst_base_sink_get_blocksize:
1081 * @sink: a #GstBaseSink
1083 * Get the number of bytes that the sink will pull when it is operating in pull
1086 * Returns: the number of bytes @sink will pull in pull mode.
1091 gst_base_sink_get_blocksize (GstBaseSink * sink)
1095 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1097 GST_OBJECT_LOCK (sink);
1098 res = sink->priv->blocksize;
1099 GST_OBJECT_UNLOCK (sink);
1105 gst_base_sink_set_property (GObject * object, guint prop_id,
1106 const GValue * value, GParamSpec * pspec)
1108 GstBaseSink *sink = GST_BASE_SINK (object);
1111 case PROP_PREROLL_QUEUE_LEN:
1112 /* preroll lock necessary to serialize with finish_preroll */
1113 GST_PAD_PREROLL_LOCK (sink->sinkpad);
1114 sink->preroll_queue_max_len = g_value_get_uint (value);
1115 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
1118 gst_base_sink_set_sync (sink, g_value_get_boolean (value));
1120 case PROP_MAX_LATENESS:
1121 gst_base_sink_set_max_lateness (sink, g_value_get_int64 (value));
1124 gst_base_sink_set_qos_enabled (sink, g_value_get_boolean (value));
1127 gst_base_sink_set_async_enabled (sink, g_value_get_boolean (value));
1129 case PROP_TS_OFFSET:
1130 gst_base_sink_set_ts_offset (sink, g_value_get_int64 (value));
1132 case PROP_BLOCKSIZE:
1133 gst_base_sink_set_blocksize (sink, g_value_get_uint (value));
1136 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1142 gst_base_sink_get_property (GObject * object, guint prop_id, GValue * value,
1145 GstBaseSink *sink = GST_BASE_SINK (object);
1148 case PROP_PREROLL_QUEUE_LEN:
1149 GST_PAD_PREROLL_LOCK (sink->sinkpad);
1150 g_value_set_uint (value, sink->preroll_queue_max_len);
1151 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
1154 g_value_set_boolean (value, gst_base_sink_get_sync (sink));
1156 case PROP_MAX_LATENESS:
1157 g_value_set_int64 (value, gst_base_sink_get_max_lateness (sink));
1160 g_value_set_boolean (value, gst_base_sink_is_qos_enabled (sink));
1163 g_value_set_boolean (value, gst_base_sink_is_async_enabled (sink));
1165 case PROP_TS_OFFSET:
1166 g_value_set_int64 (value, gst_base_sink_get_ts_offset (sink));
1168 case PROP_LAST_BUFFER:
1169 gst_value_take_buffer (value, gst_base_sink_get_last_buffer (sink));
1171 case PROP_BLOCKSIZE:
1172 g_value_set_uint (value, gst_base_sink_get_blocksize (sink));
1175 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1182 gst_base_sink_get_caps (GstBaseSink * sink)
1188 gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps)
1193 static GstFlowReturn
1194 gst_base_sink_buffer_alloc (GstBaseSink * sink, guint64 offset, guint size,
1195 GstCaps * caps, GstBuffer ** buf)
1201 /* with PREROLL_LOCK, STREAM_LOCK */
1203 gst_base_sink_preroll_queue_flush (GstBaseSink * basesink, GstPad * pad)
1207 GST_DEBUG_OBJECT (basesink, "flushing queue %p", basesink);
1208 while ((obj = g_queue_pop_head (basesink->preroll_queue))) {
1209 GST_DEBUG_OBJECT (basesink, "popped %p", obj);
1210 gst_mini_object_unref (obj);
1212 /* we can't have EOS anymore now */
1213 basesink->eos = FALSE;
1214 basesink->priv->received_eos = FALSE;
1215 basesink->have_preroll = FALSE;
1216 basesink->eos_queued = FALSE;
1217 basesink->preroll_queued = 0;
1218 basesink->buffers_queued = 0;
1219 basesink->events_queued = 0;
1220 /* can't report latency anymore until we preroll again */
1221 if (basesink->priv->async_enabled) {
1222 GST_OBJECT_LOCK (basesink);
1223 basesink->priv->have_latency = FALSE;
1224 GST_OBJECT_UNLOCK (basesink);
1226 /* and signal any waiters now */
1227 GST_PAD_PREROLL_SIGNAL (pad);
1230 /* with STREAM_LOCK, configures given segment with the event information. */
1232 gst_base_sink_configure_segment (GstBaseSink * basesink, GstPad * pad,
1233 GstEvent * event, GstSegment * segment)
1236 gdouble rate, arate;
1242 /* the newsegment event is needed to bring the buffer timestamps to the
1243 * stream time and to drop samples outside of the playback segment. */
1244 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1245 &start, &stop, &time);
1247 /* The segment is protected with both the STREAM_LOCK and the OBJECT_LOCK.
1248 * We protect with the OBJECT_LOCK so that we can use the values to
1249 * safely answer a POSITION query. */
1250 GST_OBJECT_LOCK (basesink);
1251 gst_segment_set_newsegment_full (segment, update, rate, arate, format, start,
1254 if (format == GST_FORMAT_TIME) {
1255 GST_DEBUG_OBJECT (basesink,
1256 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1257 "format GST_FORMAT_TIME, "
1258 "%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
1259 ", time %" GST_TIME_FORMAT ", accum %" GST_TIME_FORMAT,
1260 update, rate, arate, GST_TIME_ARGS (segment->start),
1261 GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time),
1262 GST_TIME_ARGS (segment->accum));
1264 GST_DEBUG_OBJECT (basesink,
1265 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1267 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
1268 G_GINT64_FORMAT ", accum %" G_GINT64_FORMAT, update, rate, arate,
1269 segment->format, segment->start, segment->stop, segment->time,
1272 GST_OBJECT_UNLOCK (basesink);
1275 /* with PREROLL_LOCK, STREAM_LOCK */
1277 gst_base_sink_commit_state (GstBaseSink * basesink)
1279 /* commit state and proceed to next pending state */
1280 GstState current, next, pending, post_pending;
1281 gboolean post_paused = FALSE;
1282 gboolean post_async_done = FALSE;
1283 gboolean post_playing = FALSE;
1286 /* we are certainly not playing async anymore now */
1287 basesink->playing_async = FALSE;
1289 GST_OBJECT_LOCK (basesink);
1290 current = GST_STATE (basesink);
1291 next = GST_STATE_NEXT (basesink);
1292 pending = GST_STATE_PENDING (basesink);
1293 post_pending = pending;
1294 sync = basesink->sync;
1297 case GST_STATE_PLAYING:
1299 GstBaseSinkClass *bclass;
1300 GstStateChangeReturn ret;
1302 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1304 GST_DEBUG_OBJECT (basesink, "commiting state to PLAYING");
1306 basesink->need_preroll = FALSE;
1307 post_async_done = TRUE;
1308 basesink->priv->commited = TRUE;
1309 post_playing = TRUE;
1310 /* post PAUSED too when we were READY */
1311 if (current == GST_STATE_READY) {
1315 /* make sure we notify the subclass of async playing */
1316 if (bclass->async_play) {
1317 ret = bclass->async_play (basesink);
1318 if (ret == GST_STATE_CHANGE_FAILURE)
1323 case GST_STATE_PAUSED:
1324 GST_DEBUG_OBJECT (basesink, "commiting state to PAUSED");
1326 post_async_done = TRUE;
1327 basesink->priv->commited = TRUE;
1328 post_pending = GST_STATE_VOID_PENDING;
1330 case GST_STATE_READY:
1331 case GST_STATE_NULL:
1333 case GST_STATE_VOID_PENDING:
1334 goto nothing_pending;
1339 /* we can report latency queries now */
1340 basesink->priv->have_latency = TRUE;
1342 GST_STATE (basesink) = pending;
1343 GST_STATE_NEXT (basesink) = GST_STATE_VOID_PENDING;
1344 GST_STATE_PENDING (basesink) = GST_STATE_VOID_PENDING;
1345 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_SUCCESS;
1346 GST_OBJECT_UNLOCK (basesink);
1349 GST_DEBUG_OBJECT (basesink, "posting PAUSED state change message");
1350 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1351 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1352 current, next, post_pending));
1354 if (post_async_done) {
1355 GST_DEBUG_OBJECT (basesink, "posting async-done message");
1356 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1357 gst_message_new_async_done (GST_OBJECT_CAST (basesink)));
1360 GST_DEBUG_OBJECT (basesink, "posting PLAYING state change message");
1361 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1362 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1363 next, pending, GST_STATE_VOID_PENDING));
1366 GST_STATE_BROADCAST (basesink);
1372 /* Depending on the state, set our vars. We get in this situation when the
1373 * state change function got a change to update the state vars before the
1374 * streaming thread did. This is fine but we need to make sure that we
1375 * update the need_preroll var since it was TRUE when we got here and might
1376 * become FALSE if we got to PLAYING. */
1377 GST_DEBUG_OBJECT (basesink, "nothing to commit, now in %s",
1378 gst_element_state_get_name (current));
1380 case GST_STATE_PLAYING:
1381 basesink->need_preroll = FALSE;
1383 case GST_STATE_PAUSED:
1384 basesink->need_preroll = TRUE;
1387 basesink->need_preroll = FALSE;
1388 basesink->flushing = TRUE;
1391 /* we can report latency queries now */
1392 basesink->priv->have_latency = TRUE;
1393 GST_OBJECT_UNLOCK (basesink);
1398 /* app is going to READY */
1399 GST_DEBUG_OBJECT (basesink, "stopping");
1400 basesink->need_preroll = FALSE;
1401 basesink->flushing = TRUE;
1402 GST_OBJECT_UNLOCK (basesink);
1407 GST_DEBUG_OBJECT (basesink, "async commit failed");
1408 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_FAILURE;
1409 GST_OBJECT_UNLOCK (basesink);
1415 /* with STREAM_LOCK, PREROLL_LOCK
1417 * Returns TRUE if the object needs synchronisation and takes therefore
1418 * part in prerolling.
1420 * rsstart/rsstop contain the start/stop in stream time.
1421 * rrstart/rrstop contain the start/stop in running time.
1424 gst_base_sink_get_sync_times (GstBaseSink * basesink, GstMiniObject * obj,
1425 GstClockTime * rsstart, GstClockTime * rsstop,
1426 GstClockTime * rrstart, GstClockTime * rrstop, gboolean * do_sync,
1427 GstSegment * segment)
1429 GstBaseSinkClass *bclass;
1431 GstClockTime start, stop; /* raw start/stop timestamps */
1432 gint64 cstart, cstop; /* clipped raw timestamps */
1433 gint64 rstart, rstop; /* clipped timestamps converted to running time */
1434 GstClockTime sstart, sstop; /* clipped timestamps converted to stream time */
1436 GstBaseSinkPrivate *priv;
1438 priv = basesink->priv;
1440 /* start with nothing */
1441 start = stop = sstart = sstop = rstart = rstop = -1;
1443 if (G_UNLIKELY (GST_IS_EVENT (obj))) {
1444 GstEvent *event = GST_EVENT_CAST (obj);
1446 switch (GST_EVENT_TYPE (event)) {
1447 /* EOS event needs syncing */
1450 if (basesink->segment.rate >= 0.0) {
1451 sstart = sstop = priv->current_sstop;
1453 /* we have not seen a buffer yet, use the segment values */
1454 sstart = sstop = gst_segment_to_stream_time (&basesink->segment,
1455 basesink->segment.format, basesink->segment.stop);
1458 sstart = sstop = priv->current_sstart;
1460 /* we have not seen a buffer yet, use the segment values */
1461 sstart = sstop = gst_segment_to_stream_time (&basesink->segment,
1462 basesink->segment.format, basesink->segment.start);
1466 rstart = rstop = priv->eos_rtime;
1467 *do_sync = rstart != -1;
1468 GST_DEBUG_OBJECT (basesink, "sync times for EOS %" GST_TIME_FORMAT,
1469 GST_TIME_ARGS (rstart));
1473 /* other events do not need syncing */
1474 /* FIXME, maybe NEWSEGMENT might need synchronisation
1475 * since the POSITION query depends on accumulated times and
1476 * we cannot accumulate the current segment before the previous
1483 /* else do buffer sync code */
1484 buffer = GST_BUFFER_CAST (obj);
1486 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1488 /* just get the times to see if we need syncing */
1489 if (bclass->get_times)
1490 bclass->get_times (basesink, buffer, &start, &stop);
1493 gst_base_sink_get_times (basesink, buffer, &start, &stop);
1499 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
1500 ", stop: %" GST_TIME_FORMAT ", do_sync %d", GST_TIME_ARGS (start),
1501 GST_TIME_ARGS (stop), *do_sync);
1503 /* collect segment and format for code clarity */
1504 format = segment->format;
1506 /* no timestamp clipping if we did not * get a TIME segment format */
1507 if (G_UNLIKELY (format != GST_FORMAT_TIME)) {
1510 /* do running and stream time in TIME format */
1511 format = GST_FORMAT_TIME;
1516 if (G_UNLIKELY (!gst_segment_clip (segment, GST_FORMAT_TIME,
1517 (gint64) start, (gint64) stop, &cstart, &cstop)))
1518 goto out_of_segment;
1520 if (G_UNLIKELY (start != cstart || stop != cstop)) {
1521 GST_DEBUG_OBJECT (basesink, "clipped to: start %" GST_TIME_FORMAT
1522 ", stop: %" GST_TIME_FORMAT, GST_TIME_ARGS (cstart),
1523 GST_TIME_ARGS (cstop));
1526 /* set last stop position */
1527 if (G_LIKELY (cstop != GST_CLOCK_TIME_NONE))
1528 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, cstop);
1530 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, cstart);
1533 /* this can produce wrong values if we accumulated non-TIME segments. If this happens,
1534 * upstream is behaving very badly */
1535 sstart = gst_segment_to_stream_time (segment, format, cstart);
1536 sstop = gst_segment_to_stream_time (segment, format, cstop);
1537 rstart = gst_segment_to_running_time (segment, format, cstart);
1538 rstop = gst_segment_to_running_time (segment, format, cstop);
1547 /* buffers and EOS always need syncing and preroll */
1553 /* should not happen since we clip them in the chain function already,
1554 * we return FALSE so that we don't try to sync on it. */
1555 GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
1556 (NULL), ("unexpected buffer out of segment found."));
1557 GST_LOG_OBJECT (basesink, "buffer skipped, not in segment");
1562 /* with STREAM_LOCK, PREROLL_LOCK, LOCK
1563 * adjust a timestamp with the latency and timestamp offset */
1565 gst_base_sink_adjust_time (GstBaseSink * basesink, GstClockTime time)
1567 GstClockTimeDiff ts_offset;
1569 /* don't do anything funny with invalid timestamps */
1570 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
1573 time += basesink->priv->latency;
1575 /* apply offset, be carefull for underflows */
1576 ts_offset = basesink->priv->ts_offset;
1577 if (ts_offset < 0) {
1578 ts_offset = -ts_offset;
1579 if (ts_offset < time)
1589 /* gst_base_sink_wait_clock:
1591 * @time: the running_time to be reached
1592 * @jitter: the jitter to be filled with time diff (can be NULL)
1594 * This function will block until @time is reached. It is usually called by
1595 * subclasses that use their own internal synchronisation.
1597 * If @time is not valid, no sycnhronisation is done and #GST_CLOCK_BADTIME is
1598 * returned. Likewise, if synchronisation is disabled in the element or there
1599 * is no clock, no synchronisation is done and #GST_CLOCK_BADTIME is returned.
1601 * This function should only be called with the PREROLL_LOCK held, like when
1602 * receiving an EOS event in the ::event vmethod or when receiving a buffer in
1603 * the ::render vmethod.
1605 * The @time argument should be the running_time of when this method should
1606 * return and is not adjusted with any latency or offset configured in the
1611 * Returns: #GstClockReturn
1614 gst_base_sink_wait_clock (GstBaseSink * basesink, GstClockTime time,
1615 GstClockTimeDiff * jitter)
1621 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
1624 GST_OBJECT_LOCK (basesink);
1625 if (G_UNLIKELY (!basesink->sync))
1628 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (basesink)) == NULL))
1631 /* add base_time to running_time to get the time against the clock */
1632 time += GST_ELEMENT_CAST (basesink)->base_time;
1634 id = gst_clock_new_single_shot_id (clock, time);
1635 GST_OBJECT_UNLOCK (basesink);
1637 /* A blocking wait is performed on the clock. We save the ClockID
1638 * so we can unlock the entry at any time. While we are blocking, we
1639 * release the PREROLL_LOCK so that other threads can interrupt the
1641 basesink->clock_id = id;
1642 /* release the preroll lock while waiting */
1643 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
1645 ret = gst_clock_id_wait (id, jitter);
1647 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
1648 gst_clock_id_unref (id);
1649 basesink->clock_id = NULL;
1653 /* no syncing needed */
1656 GST_DEBUG_OBJECT (basesink, "time not valid, no sync needed");
1657 return GST_CLOCK_BADTIME;
1661 GST_DEBUG_OBJECT (basesink, "sync disabled");
1662 GST_OBJECT_UNLOCK (basesink);
1663 return GST_CLOCK_BADTIME;
1667 GST_DEBUG_OBJECT (basesink, "no clock, can't sync");
1668 GST_OBJECT_UNLOCK (basesink);
1669 return GST_CLOCK_BADTIME;
1674 * gst_base_sink_wait_preroll:
1677 * If the #GstBaseSinkClass::render method performs its own synchronisation against
1678 * the clock it must unblock when going from PLAYING to the PAUSED state and call
1679 * this method before continuing to render the remaining data.
1681 * This function will block until a state change to PLAYING happens (in which
1682 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
1683 * to a state change to READY or a FLUSH event (in which case this function
1684 * returns #GST_FLOW_WRONG_STATE).
1686 * This function should only be called with the PREROLL_LOCK held, like in the
1691 * Returns: #GST_FLOW_OK if the preroll completed and processing can
1692 * continue. Any other return value should be returned from the render vmethod.
1695 gst_base_sink_wait_preroll (GstBaseSink * sink)
1697 sink->have_preroll = TRUE;
1698 GST_DEBUG_OBJECT (sink, "waiting in preroll for flush or PLAYING");
1699 /* block until the state changes, or we get a flush, or something */
1700 GST_PAD_PREROLL_WAIT (sink->sinkpad);
1701 sink->have_preroll = FALSE;
1702 if (G_UNLIKELY (sink->flushing))
1704 GST_DEBUG_OBJECT (sink, "continue after preroll");
1711 GST_DEBUG_OBJECT (sink, "preroll interrupted");
1712 return GST_FLOW_WRONG_STATE;
1717 * gst_base_sink_do_preroll:
1719 * @obj: the object that caused the preroll
1721 * If the @sink spawns its own thread for pulling buffers from upstream it
1722 * should call this method after it has pulled a buffer. If the element needed
1723 * to preroll, this function will perform the preroll and will then block
1724 * until the element state is changed.
1726 * This function should be called with the PREROLL_LOCK held.
1730 * Returns: #GST_FLOW_OK if the preroll completed and processing can
1731 * continue. Any other return value should be returned from the render vmethod.
1734 gst_base_sink_do_preroll (GstBaseSink * sink, GstMiniObject * obj)
1738 while (G_UNLIKELY (sink->need_preroll)) {
1739 GST_DEBUG_OBJECT (sink, "prerolling object %p", obj);
1741 if (G_LIKELY (sink->playing_async)) {
1743 if (G_UNLIKELY (!gst_base_sink_commit_state (sink)))
1747 /* need to recheck here because the commit state could have
1748 * made us not need the preroll anymore */
1749 if (G_LIKELY (sink->need_preroll)) {
1750 /* block until the state changes, or we get a flush, or something */
1751 ret = gst_base_sink_wait_preroll (sink);
1752 if (ret != GST_FLOW_OK)
1761 GST_DEBUG_OBJECT (sink, "we are flushing");
1766 GST_DEBUG_OBJECT (sink, "stopping while commiting state");
1767 return GST_FLOW_WRONG_STATE;
1772 * gst_base_sink_wait_eos:
1774 * @time: the running_time to be reached
1775 * @jitter: the jitter to be filled with time diff (can be NULL)
1777 * This function will block until @time is reached. It is usually called by
1778 * subclasses that use their own internal synchronisation but want to let the
1779 * EOS be handled by the base class.
1781 * This function should only be called with the PREROLL_LOCK held, like when
1782 * receiving an EOS event in the ::event vmethod.
1784 * The @time argument should be the running_time of when the EOS should happen
1785 * and will be adjusted with any latency and offset configured in the sink.
1789 * Returns: #GstFlowReturn
1792 gst_base_sink_wait_eos (GstBaseSink * sink, GstClockTime time,
1793 GstClockTimeDiff * jitter)
1795 GstClockReturn status;
1801 GST_DEBUG_OBJECT (sink, "checking preroll");
1803 /* first wait for the playing state before we can continue */
1804 if (G_UNLIKELY (sink->need_preroll)) {
1805 ret = gst_base_sink_wait_preroll (sink);
1806 if (ret != GST_FLOW_OK)
1810 /* preroll done, we can sync since we are in PLAYING now. */
1811 GST_DEBUG_OBJECT (sink, "possibly waiting for clock to reach %"
1812 GST_TIME_FORMAT, GST_TIME_ARGS (time));
1814 /* compensate for latency and ts_offset. We don't adjust for render delay
1815 * because we don't interact with the device on EOS normally. */
1816 stime = gst_base_sink_adjust_time (sink, time);
1818 /* wait for the clock, this can be interrupted because we got shut down or
1820 status = gst_base_sink_wait_clock (sink, stime, jitter);
1822 GST_DEBUG_OBJECT (sink, "clock returned %d", status);
1824 /* invalid time, no clock or sync disabled, just continue then */
1825 if (status == GST_CLOCK_BADTIME)
1828 /* waiting could have been interrupted and we can be flushing now */
1829 if (G_UNLIKELY (sink->flushing))
1832 /* retry if we got unscheduled, which means we did not reach the timeout
1833 * yet. if some other error occures, we continue. */
1834 } while (status == GST_CLOCK_UNSCHEDULED);
1836 GST_DEBUG_OBJECT (sink, "end of stream");
1843 GST_DEBUG_OBJECT (sink, "we are flushing");
1844 return GST_FLOW_WRONG_STATE;
1848 /* with STREAM_LOCK, PREROLL_LOCK
1850 * Make sure we are in PLAYING and synchronize an object to the clock.
1852 * If we need preroll, we are not in PLAYING. We try to commit the state
1853 * if needed and then block if we still are not PLAYING.
1855 * We start waiting on the clock in PLAYING. If we got interrupted, we
1856 * immediatly try to re-preroll.
1858 * Some objects do not need synchronisation (most events) and so this function
1859 * immediatly returns GST_FLOW_OK.
1861 * for objects that arrive later than max-lateness to be synchronized to the
1862 * clock have the @late boolean set to TRUE.
1864 * This function keeps a running average of the jitter (the diff between the
1865 * clock time and the requested sync time). The jitter is negative for
1866 * objects that arrive in time and positive for late buffers.
1868 * does not take ownership of obj.
1870 static GstFlowReturn
1871 gst_base_sink_do_sync (GstBaseSink * basesink, GstPad * pad,
1872 GstMiniObject * obj, gboolean * late)
1874 GstClockTimeDiff jitter;
1876 GstClockReturn status = GST_CLOCK_OK;
1877 GstClockTime rstart, rstop, sstart, sstop, stime;
1879 GstBaseSinkPrivate *priv;
1881 priv = basesink->priv;
1883 sstart = sstop = rstart = rstop = -1;
1886 priv->current_rstart = -1;
1888 /* get timing information for this object against the render segment */
1889 syncable = gst_base_sink_get_sync_times (basesink, obj,
1890 &sstart, &sstop, &rstart, &rstop, &do_sync, &basesink->segment);
1892 /* a syncable object needs to participate in preroll and
1893 * clocking. All buffers and EOS are syncable. */
1894 if (G_UNLIKELY (!syncable))
1897 /* store timing info for current object */
1898 priv->current_rstart = rstart;
1899 priv->current_rstop = (rstop != -1 ? rstop : rstart);
1900 /* save sync time for eos when the previous object needed sync */
1901 priv->eos_rtime = (do_sync ? priv->current_rstop : -1);
1904 /* first do preroll, this makes sure we commit our state
1905 * to PAUSED and can continue to PLAYING. We cannot perform
1906 * any clock sync in PAUSED because there is no clock.
1908 while (G_UNLIKELY (basesink->need_preroll)) {
1909 GST_DEBUG_OBJECT (basesink, "prerolling object %p", obj);
1911 if (G_LIKELY (basesink->playing_async)) {
1913 if (G_UNLIKELY (!gst_base_sink_commit_state (basesink)))
1917 /* need to recheck here because the commit state could have
1918 * made us not need the preroll anymore */
1919 if (G_LIKELY (basesink->need_preroll)) {
1920 /* block until the state changes, or we get a flush, or something */
1921 if (gst_base_sink_wait_preroll (basesink) != GST_FLOW_OK)
1926 /* After rendering we store the position of the last buffer so that we can use
1927 * it to report the position. We need to take the lock here. */
1928 GST_OBJECT_LOCK (basesink);
1929 priv->current_sstart = sstart;
1930 priv->current_sstop = (sstop != -1 ? sstop : sstart);
1931 GST_OBJECT_UNLOCK (basesink);
1936 /* adjust for latency */
1937 stime = gst_base_sink_adjust_time (basesink, rstart);
1939 /* adjust for render-delay, avoid underflows */
1941 if (stime > priv->render_delay)
1942 stime -= priv->render_delay;
1947 /* preroll done, we can sync since we are in PLAYING now. */
1948 GST_DEBUG_OBJECT (basesink, "possibly waiting for clock to reach %"
1949 GST_TIME_FORMAT ", adjusted %" GST_TIME_FORMAT,
1950 GST_TIME_ARGS (rstart), GST_TIME_ARGS (stime));
1952 /* This function will return immediatly if start == -1, no clock
1953 * or sync is disabled with GST_CLOCK_BADTIME. */
1954 status = gst_base_sink_wait_clock (basesink, stime, &jitter);
1956 GST_DEBUG_OBJECT (basesink, "clock returned %d", status);
1958 /* invalid time, no clock or sync disabled, just render */
1959 if (status == GST_CLOCK_BADTIME)
1962 /* waiting could have been interrupted and we can be flushing now */
1963 if (G_UNLIKELY (basesink->flushing))
1966 /* check for unlocked by a state change, we are not flushing so
1967 * we can try to preroll on the current buffer. */
1968 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
1969 GST_DEBUG_OBJECT (basesink, "unscheduled, waiting some more");
1973 /* successful syncing done, record observation */
1974 priv->current_jitter = jitter;
1976 /* check if the object should be dropped */
1977 *late = gst_base_sink_is_too_late (basesink, obj, rstart, rstop,
1986 GST_DEBUG_OBJECT (basesink, "non syncable object %p", obj);
1991 GST_DEBUG_OBJECT (basesink, "we are flushing");
1992 return GST_FLOW_WRONG_STATE;
1996 GST_DEBUG_OBJECT (basesink, "stopping while commiting state");
1997 return GST_FLOW_WRONG_STATE;
2002 gst_base_sink_send_qos (GstBaseSink * basesink,
2003 gdouble proportion, GstClockTime time, GstClockTimeDiff diff)
2008 /* generate Quality-of-Service event */
2009 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2010 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2011 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (time));
2013 event = gst_event_new_qos (proportion, diff, time);
2016 res = gst_pad_push_event (basesink->sinkpad, event);
2022 gst_base_sink_perform_qos (GstBaseSink * sink, gboolean dropped)
2024 GstBaseSinkPrivate *priv;
2025 GstClockTime start, stop;
2026 GstClockTimeDiff jitter;
2027 GstClockTime pt, entered, left;
2028 GstClockTime duration;
2033 start = priv->current_rstart;
2035 /* if Quality-of-Service disabled, do nothing */
2036 if (!g_atomic_int_get (&priv->qos_enabled) || start == -1)
2039 stop = priv->current_rstop;
2040 jitter = priv->current_jitter;
2043 /* this is the time the buffer entered the sink */
2044 if (start < -jitter)
2047 entered = start + jitter;
2050 /* this is the time the buffer entered the sink */
2051 entered = start + jitter;
2052 /* this is the time the buffer left the sink */
2053 left = start + jitter;
2056 /* calculate duration of the buffer */
2058 duration = stop - start;
2062 /* if we have the time when the last buffer left us, calculate
2063 * processing time */
2064 if (priv->last_left != -1) {
2065 if (entered > priv->last_left) {
2066 pt = entered - priv->last_left;
2074 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "start: %" GST_TIME_FORMAT
2075 ", entered %" GST_TIME_FORMAT ", left %" GST_TIME_FORMAT ", pt: %"
2076 GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT ",jitter %"
2077 G_GINT64_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (entered),
2078 GST_TIME_ARGS (left), GST_TIME_ARGS (pt), GST_TIME_ARGS (duration),
2081 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "avg_duration: %" GST_TIME_FORMAT
2082 ", avg_pt: %" GST_TIME_FORMAT ", avg_rate: %g",
2083 GST_TIME_ARGS (priv->avg_duration), GST_TIME_ARGS (priv->avg_pt),
2086 /* collect running averages. for first observations, we copy the
2088 if (priv->avg_duration == -1)
2089 priv->avg_duration = duration;
2091 priv->avg_duration = UPDATE_RUNNING_AVG (priv->avg_duration, duration);
2093 if (priv->avg_pt == -1)
2096 priv->avg_pt = UPDATE_RUNNING_AVG (priv->avg_pt, pt);
2098 if (priv->avg_duration != 0)
2100 gst_guint64_to_gdouble (priv->avg_pt) /
2101 gst_guint64_to_gdouble (priv->avg_duration);
2105 if (priv->last_left != -1) {
2106 if (dropped || priv->avg_rate < 0.0) {
2107 priv->avg_rate = rate;
2110 priv->avg_rate = UPDATE_RUNNING_AVG_N (priv->avg_rate, rate);
2112 priv->avg_rate = UPDATE_RUNNING_AVG_P (priv->avg_rate, rate);
2116 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink,
2117 "updated: avg_duration: %" GST_TIME_FORMAT ", avg_pt: %" GST_TIME_FORMAT
2118 ", avg_rate: %g", GST_TIME_ARGS (priv->avg_duration),
2119 GST_TIME_ARGS (priv->avg_pt), priv->avg_rate);
2122 if (priv->avg_rate >= 0.0) {
2123 /* if we have a valid rate, start sending QoS messages */
2124 if (priv->current_jitter < 0) {
2125 /* make sure we never go below 0 when adding the jitter to the
2127 if (priv->current_rstart < -priv->current_jitter)
2128 priv->current_jitter = -priv->current_rstart;
2130 gst_base_sink_send_qos (sink, priv->avg_rate, priv->current_rstart,
2131 priv->current_jitter);
2134 /* record when this buffer will leave us */
2135 priv->last_left = left;
2138 /* reset all qos measuring */
2140 gst_base_sink_reset_qos (GstBaseSink * sink)
2142 GstBaseSinkPrivate *priv;
2146 priv->last_in_time = -1;
2147 priv->last_left = -1;
2148 priv->avg_duration = -1;
2150 priv->avg_rate = -1.0;
2151 priv->avg_render = -1;
2157 /* Checks if the object was scheduled too late.
2159 * start/stop contain the raw timestamp start and stop values
2162 * status and jitter contain the return values from the clock wait.
2164 * returns TRUE if the buffer was too late.
2167 gst_base_sink_is_too_late (GstBaseSink * basesink, GstMiniObject * obj,
2168 GstClockTime start, GstClockTime stop,
2169 GstClockReturn status, GstClockTimeDiff jitter)
2172 gint64 max_lateness;
2173 GstBaseSinkPrivate *priv;
2175 priv = basesink->priv;
2179 /* only for objects that were too late */
2180 if (G_LIKELY (status != GST_CLOCK_EARLY))
2183 max_lateness = basesink->abidata.ABI.max_lateness;
2185 /* check if frame dropping is enabled */
2186 if (max_lateness == -1)
2189 /* only check for buffers */
2190 if (G_UNLIKELY (!GST_IS_BUFFER (obj)))
2193 /* can't do check if we don't have a timestamp */
2194 if (G_UNLIKELY (start == -1))
2197 /* we can add a valid stop time */
2199 max_lateness += stop;
2201 max_lateness += start;
2203 /* if the jitter bigger than duration and lateness we are too late */
2204 if ((late = start + jitter > max_lateness)) {
2205 GST_DEBUG_OBJECT (basesink, "buffer is too late %" GST_TIME_FORMAT
2206 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (start + jitter),
2207 GST_TIME_ARGS (max_lateness));
2208 /* !!emergency!!, if we did not receive anything valid for more than a
2209 * second, render it anyway so the user sees something */
2210 if (priv->last_in_time && start - priv->last_in_time > GST_SECOND) {
2212 GST_DEBUG_OBJECT (basesink,
2213 "**emergency** last buffer at %" GST_TIME_FORMAT " > GST_SECOND",
2214 GST_TIME_ARGS (priv->last_in_time));
2220 priv->last_in_time = start;
2227 GST_DEBUG_OBJECT (basesink, "object was scheduled in time");
2232 GST_DEBUG_OBJECT (basesink, "frame dropping disabled");
2237 GST_DEBUG_OBJECT (basesink, "object is not a buffer");
2242 GST_DEBUG_OBJECT (basesink, "buffer has no timestamp");
2247 /* called before and after calling the render vmethod. It keeps track of how
2248 * much time was spent in the render method and is used to check if we are
2251 gst_base_sink_do_render_stats (GstBaseSink * basesink, gboolean start)
2253 GstBaseSinkPrivate *priv;
2255 priv = basesink->priv;
2258 priv->start = gst_util_get_timestamp ();
2260 GstClockTime elapsed;
2262 priv->stop = gst_util_get_timestamp ();
2264 elapsed = GST_CLOCK_DIFF (priv->start, priv->stop);
2266 if (priv->avg_render == -1)
2267 priv->avg_render = elapsed;
2269 priv->avg_render = UPDATE_RUNNING_AVG (priv->avg_render, elapsed);
2271 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2272 "avg_render: %" GST_TIME_FORMAT, GST_TIME_ARGS (priv->avg_render));
2276 /* with STREAM_LOCK, PREROLL_LOCK,
2278 * Synchronize the object on the clock and then render it.
2280 * takes ownership of obj.
2282 static GstFlowReturn
2283 gst_base_sink_render_object (GstBaseSink * basesink, GstPad * pad,
2284 GstMiniObject * obj)
2286 GstFlowReturn ret = GST_FLOW_OK;
2287 GstBaseSinkClass *bclass;
2288 gboolean late = FALSE;
2289 GstBaseSinkPrivate *priv;
2291 priv = basesink->priv;
2293 /* synchronize this object, non syncable objects return OK
2295 ret = gst_base_sink_do_sync (basesink, pad, obj, &late);
2296 if (G_UNLIKELY (ret != GST_FLOW_OK))
2299 /* and now render, event or buffer. */
2300 if (G_LIKELY (GST_IS_BUFFER (obj))) {
2303 /* drop late buffers unconditionally, let's hope it's unlikely */
2304 if (G_UNLIKELY (late))
2307 buf = GST_BUFFER_CAST (obj);
2309 gst_base_sink_set_last_buffer (basesink, buf);
2311 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2313 if (G_LIKELY (bclass->render)) {
2316 /* read once, to get same value before and after */
2317 do_qos = g_atomic_int_get (&priv->qos_enabled);
2319 GST_DEBUG_OBJECT (basesink, "rendering buffer %p", obj);
2321 /* record rendering time for QoS and stats */
2323 gst_base_sink_do_render_stats (basesink, TRUE);
2325 ret = bclass->render (basesink, buf);
2330 gst_base_sink_do_render_stats (basesink, FALSE);
2333 GstEvent *event = GST_EVENT_CAST (obj);
2334 gboolean event_res = TRUE;
2337 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2339 type = GST_EVENT_TYPE (event);
2341 GST_DEBUG_OBJECT (basesink, "rendering event %p, type %s", obj,
2342 gst_event_type_get_name (type));
2345 event_res = bclass->event (basesink, event);
2347 /* when we get here we could be flushing again when the event handler calls
2348 * _wait_eos(). We have to ignore this object in that case. */
2349 if (G_UNLIKELY (basesink->flushing))
2352 if (G_LIKELY (event_res)) {
2355 /* the EOS event is completely handled so we mark
2356 * ourselves as being in the EOS state. eos is also
2357 * protected by the object lock so we can read it when
2358 * answering the POSITION query. */
2359 GST_OBJECT_LOCK (basesink);
2360 basesink->eos = TRUE;
2361 GST_OBJECT_UNLOCK (basesink);
2362 /* ok, now we can post the message */
2363 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
2364 gst_element_post_message (GST_ELEMENT_CAST (basesink),
2365 gst_message_new_eos (GST_OBJECT_CAST (basesink)));
2367 case GST_EVENT_NEWSEGMENT:
2368 /* configure the segment */
2369 gst_base_sink_configure_segment (basesink, pad, event,
2370 &basesink->segment);
2379 gst_base_sink_perform_qos (basesink, late);
2381 GST_DEBUG_OBJECT (basesink, "object unref after render %p", obj);
2382 gst_mini_object_unref (obj);
2389 GST_DEBUG_OBJECT (basesink, "do_sync returned %s", gst_flow_get_name (ret));
2395 GST_DEBUG_OBJECT (basesink, "buffer late, dropping");
2400 GST_DEBUG_OBJECT (basesink, "we are flushing, ignore object");
2401 gst_mini_object_unref (obj);
2402 return GST_FLOW_WRONG_STATE;
2406 /* with STREAM_LOCK, PREROLL_LOCK
2408 * Perform preroll on the given object. For buffers this means
2409 * calling the preroll subclass method.
2410 * If that succeeds, the state will be commited.
2412 * function does not take ownership of obj.
2414 static GstFlowReturn
2415 gst_base_sink_preroll_object (GstBaseSink * basesink, GstPad * pad,
2416 GstMiniObject * obj)
2420 GST_DEBUG_OBJECT (basesink, "do preroll %p", obj);
2422 /* if it's a buffer, we need to call the preroll method */
2423 if (G_LIKELY (GST_IS_BUFFER (obj))) {
2424 GstBaseSinkClass *bclass;
2426 GstClockTime timestamp;
2428 buf = GST_BUFFER_CAST (obj);
2429 timestamp = GST_BUFFER_TIMESTAMP (buf);
2431 GST_DEBUG_OBJECT (basesink, "preroll buffer %" GST_TIME_FORMAT,
2432 GST_TIME_ARGS (timestamp));
2434 gst_base_sink_set_last_buffer (basesink, buf);
2436 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2437 if (bclass->preroll)
2438 if ((ret = bclass->preroll (basesink, buf)) != GST_FLOW_OK)
2439 goto preroll_failed;
2443 if (G_LIKELY (basesink->playing_async)) {
2444 if (G_UNLIKELY (!gst_base_sink_commit_state (basesink)))
2453 GST_DEBUG_OBJECT (basesink, "preroll failed, abort state");
2454 gst_element_abort_state (GST_ELEMENT_CAST (basesink));
2459 GST_DEBUG_OBJECT (basesink, "stopping while commiting state");
2460 return GST_FLOW_WRONG_STATE;
2464 /* with STREAM_LOCK, PREROLL_LOCK
2466 * Queue an object for rendering.
2467 * The first prerollable object queued will complete the preroll. If the
2468 * preroll queue if filled, we render all the objects in the queue.
2470 * This function takes ownership of the object.
2472 static GstFlowReturn
2473 gst_base_sink_queue_object_unlocked (GstBaseSink * basesink, GstPad * pad,
2474 GstMiniObject * obj, gboolean prerollable)
2476 GstFlowReturn ret = GST_FLOW_OK;
2480 if (G_UNLIKELY (basesink->need_preroll)) {
2481 if (G_LIKELY (prerollable))
2482 basesink->preroll_queued++;
2484 length = basesink->preroll_queued;
2486 GST_DEBUG_OBJECT (basesink, "now %d prerolled items", length);
2488 /* first prerollable item needs to finish the preroll */
2490 ret = gst_base_sink_preroll_object (basesink, pad, obj);
2491 if (G_UNLIKELY (ret != GST_FLOW_OK))
2492 goto preroll_failed;
2494 /* need to recheck if we need preroll, commmit state during preroll
2495 * could have made us not need more preroll. */
2496 if (G_UNLIKELY (basesink->need_preroll)) {
2497 /* see if we can render now, if we can't add the object to the preroll
2499 if (G_UNLIKELY (length <= basesink->preroll_queue_max_len))
2504 /* we can start rendering (or blocking) the queued object
2506 q = basesink->preroll_queue;
2507 while (G_UNLIKELY (!g_queue_is_empty (q))) {
2510 o = g_queue_pop_head (q);
2511 GST_DEBUG_OBJECT (basesink, "rendering queued object %p", o);
2513 /* do something with the return value */
2514 ret = gst_base_sink_render_object (basesink, pad, o);
2515 if (ret != GST_FLOW_OK)
2516 goto dequeue_failed;
2519 /* now render the object */
2520 ret = gst_base_sink_render_object (basesink, pad, obj);
2521 basesink->preroll_queued = 0;
2528 GST_DEBUG_OBJECT (basesink, "preroll failed, reason %s",
2529 gst_flow_get_name (ret));
2530 gst_mini_object_unref (obj);
2535 /* add object to the queue and return */
2536 GST_DEBUG_OBJECT (basesink, "need more preroll data %d <= %d",
2537 length, basesink->preroll_queue_max_len);
2538 g_queue_push_tail (basesink->preroll_queue, obj);
2543 GST_DEBUG_OBJECT (basesink, "rendering queued objects failed, reason %s",
2544 gst_flow_get_name (ret));
2545 gst_mini_object_unref (obj);
2552 * This function grabs the PREROLL_LOCK and adds the object to
2555 * This function takes ownership of obj.
2557 static GstFlowReturn
2558 gst_base_sink_queue_object (GstBaseSink * basesink, GstPad * pad,
2559 GstMiniObject * obj, gboolean prerollable)
2563 GST_PAD_PREROLL_LOCK (pad);
2564 if (G_UNLIKELY (basesink->flushing))
2567 if (G_UNLIKELY (basesink->priv->received_eos))
2570 ret = gst_base_sink_queue_object_unlocked (basesink, pad, obj, prerollable);
2571 GST_PAD_PREROLL_UNLOCK (pad);
2578 GST_DEBUG_OBJECT (basesink, "sink is flushing");
2579 GST_PAD_PREROLL_UNLOCK (pad);
2580 gst_mini_object_unref (obj);
2581 return GST_FLOW_WRONG_STATE;
2585 GST_DEBUG_OBJECT (basesink,
2586 "we are EOS, dropping object, return UNEXPECTED");
2587 GST_PAD_PREROLL_UNLOCK (pad);
2588 gst_mini_object_unref (obj);
2589 return GST_FLOW_UNEXPECTED;
2594 gst_base_sink_flush_start (GstBaseSink * basesink, GstPad * pad)
2596 /* make sure we are not blocked on the clock also clear any pending
2598 gst_base_sink_set_flushing (basesink, pad, TRUE);
2600 /* we grab the stream lock but that is not needed since setting the
2601 * sink to flushing would make sure no state commit is being done
2603 GST_PAD_STREAM_LOCK (pad);
2604 gst_base_sink_reset_qos (basesink);
2605 if (basesink->priv->async_enabled) {
2606 /* and we need to commit our state again on the next
2607 * prerolled buffer */
2608 basesink->playing_async = TRUE;
2609 gst_element_lost_state (GST_ELEMENT_CAST (basesink));
2611 basesink->priv->have_latency = TRUE;
2612 basesink->need_preroll = FALSE;
2614 gst_base_sink_set_last_buffer (basesink, NULL);
2615 GST_PAD_STREAM_UNLOCK (pad);
2619 gst_base_sink_flush_stop (GstBaseSink * basesink, GstPad * pad)
2621 /* unset flushing so we can accept new data, this also flushes out any EOS
2623 gst_base_sink_set_flushing (basesink, pad, FALSE);
2625 /* for position reporting */
2626 GST_OBJECT_LOCK (basesink);
2627 basesink->priv->current_sstart = -1;
2628 basesink->priv->current_sstop = -1;
2629 basesink->priv->eos_rtime = -1;
2630 if (basesink->pad_mode == GST_ACTIVATE_PUSH) {
2631 /* we need new segment info after the flush. */
2632 basesink->have_newsegment = FALSE;
2633 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
2634 gst_segment_init (basesink->abidata.ABI.clip_segment, GST_FORMAT_UNDEFINED);
2636 GST_OBJECT_UNLOCK (basesink);
2640 gst_base_sink_event (GstPad * pad, GstEvent * event)
2642 GstBaseSink *basesink;
2643 gboolean result = TRUE;
2644 GstBaseSinkClass *bclass;
2646 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
2648 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2650 GST_DEBUG_OBJECT (basesink, "event %p (%s)", event,
2651 GST_EVENT_TYPE_NAME (event));
2653 switch (GST_EVENT_TYPE (event)) {
2658 GST_PAD_PREROLL_LOCK (pad);
2659 if (G_UNLIKELY (basesink->flushing))
2662 if (G_UNLIKELY (basesink->priv->received_eos)) {
2663 /* we can't accept anything when we are EOS */
2665 gst_event_unref (event);
2667 /* we set the received EOS flag here so that we can use it when testing if
2668 * we are prerolled and to refure more buffers. */
2669 basesink->priv->received_eos = TRUE;
2671 /* EOS is a prerollable object, we call the unlocked version because it
2672 * does not check the received_eos flag. */
2673 ret = gst_base_sink_queue_object_unlocked (basesink, pad,
2674 GST_MINI_OBJECT_CAST (event), TRUE);
2675 if (G_UNLIKELY (ret != GST_FLOW_OK))
2678 GST_PAD_PREROLL_UNLOCK (pad);
2681 case GST_EVENT_NEWSEGMENT:
2685 GST_DEBUG_OBJECT (basesink, "newsegment %p", event);
2687 GST_PAD_PREROLL_LOCK (pad);
2688 if (G_UNLIKELY (basesink->flushing))
2691 if (G_UNLIKELY (basesink->priv->received_eos)) {
2692 /* we can't accept anything when we are EOS */
2694 gst_event_unref (event);
2696 /* the new segment is a non prerollable item and does not block anything,
2697 * we need to configure the current clipping segment and insert the event
2698 * in the queue to serialize it with the buffers for rendering. */
2699 gst_base_sink_configure_segment (basesink, pad, event,
2700 basesink->abidata.ABI.clip_segment);
2703 gst_base_sink_queue_object_unlocked (basesink, pad,
2704 GST_MINI_OBJECT_CAST (event), FALSE);
2705 if (G_UNLIKELY (ret != GST_FLOW_OK))
2708 GST_OBJECT_LOCK (basesink);
2709 basesink->have_newsegment = TRUE;
2710 GST_OBJECT_UNLOCK (basesink);
2713 GST_PAD_PREROLL_UNLOCK (pad);
2716 case GST_EVENT_FLUSH_START:
2718 bclass->event (basesink, event);
2720 GST_DEBUG_OBJECT (basesink, "flush-start %p", event);
2722 gst_base_sink_flush_start (basesink, pad);
2724 gst_event_unref (event);
2726 case GST_EVENT_FLUSH_STOP:
2728 bclass->event (basesink, event);
2730 GST_DEBUG_OBJECT (basesink, "flush-stop %p", event);
2732 gst_base_sink_flush_stop (basesink, pad);
2734 gst_event_unref (event);
2737 /* other events are sent to queue or subclass depending on if they
2738 * are serialized. */
2739 if (GST_EVENT_IS_SERIALIZED (event)) {
2740 gst_base_sink_queue_object (basesink, pad,
2741 GST_MINI_OBJECT_CAST (event), FALSE);
2744 bclass->event (basesink, event);
2745 gst_event_unref (event);
2750 gst_object_unref (basesink);
2757 GST_DEBUG_OBJECT (basesink, "we are flushing");
2758 GST_PAD_PREROLL_UNLOCK (pad);
2760 gst_event_unref (event);
2765 /* default implementation to calculate the start and end
2766 * timestamps on a buffer, subclasses can override
2769 gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
2770 GstClockTime * start, GstClockTime * end)
2772 GstClockTime timestamp, duration;
2774 timestamp = GST_BUFFER_TIMESTAMP (buffer);
2775 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
2777 /* get duration to calculate end time */
2778 duration = GST_BUFFER_DURATION (buffer);
2779 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2780 *end = timestamp + duration;
2786 /* must be called with PREROLL_LOCK */
2788 gst_base_sink_needs_preroll (GstBaseSink * basesink)
2790 gboolean is_prerolled, res;
2792 /* we have 2 cases where the PREROLL_LOCK is released:
2793 * 1) we are blocking in the PREROLL_LOCK and thus are prerolled.
2794 * 2) we are syncing on the clock
2796 is_prerolled = basesink->have_preroll || basesink->priv->received_eos;
2797 res = !is_prerolled;
2799 GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d => needs preroll: %d",
2800 basesink->have_preroll, basesink->priv->received_eos, res);
2805 /* with STREAM_LOCK, PREROLL_LOCK
2807 * Takes a buffer and compare the timestamps with the last segment.
2808 * If the buffer falls outside of the segment boundaries, drop it.
2809 * Else queue the buffer for preroll and rendering.
2811 * This function takes ownership of the buffer.
2813 static GstFlowReturn
2814 gst_base_sink_chain_unlocked (GstBaseSink * basesink, GstPad * pad,
2817 GstBaseSinkClass *bclass;
2818 GstFlowReturn result;
2819 GstClockTime start = GST_CLOCK_TIME_NONE, end = GST_CLOCK_TIME_NONE;
2820 GstSegment *clip_segment;
2822 if (G_UNLIKELY (basesink->flushing))
2825 if (G_UNLIKELY (basesink->priv->received_eos))
2828 /* for code clarity */
2829 clip_segment = basesink->abidata.ABI.clip_segment;
2831 if (G_UNLIKELY (!basesink->have_newsegment)) {
2834 sync = gst_base_sink_get_sync (basesink);
2836 GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
2837 (_("Internal data flow problem.")),
2838 ("Received buffer without a new-segment. Assuming timestamps start from 0."));
2841 /* this means this sink will assume timestamps start from 0 */
2842 GST_OBJECT_LOCK (basesink);
2843 clip_segment->start = 0;
2844 clip_segment->stop = -1;
2845 basesink->segment.start = 0;
2846 basesink->segment.stop = -1;
2847 basesink->have_newsegment = TRUE;
2848 GST_OBJECT_UNLOCK (basesink);
2851 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2853 /* check if the buffer needs to be dropped, we first ask the subclass for the
2855 if (bclass->get_times)
2856 bclass->get_times (basesink, buf, &start, &end);
2859 /* if the subclass does not want sync, we use our own values so that we at
2860 * least clip the buffer to the segment */
2861 gst_base_sink_get_times (basesink, buf, &start, &end);
2864 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
2865 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
2867 /* a dropped buffer does not participate in anything */
2868 if (GST_CLOCK_TIME_IS_VALID (start) &&
2869 (clip_segment->format == GST_FORMAT_TIME)) {
2870 if (G_UNLIKELY (!gst_segment_clip (clip_segment,
2871 GST_FORMAT_TIME, (gint64) start, (gint64) end, NULL, NULL)))
2872 goto out_of_segment;
2875 /* now we can process the buffer in the queue, this function takes ownership
2877 result = gst_base_sink_queue_object_unlocked (basesink, pad,
2878 GST_MINI_OBJECT_CAST (buf), TRUE);
2885 GST_DEBUG_OBJECT (basesink, "sink is flushing");
2886 gst_buffer_unref (buf);
2887 return GST_FLOW_WRONG_STATE;
2891 GST_DEBUG_OBJECT (basesink,
2892 "we are EOS, dropping object, return UNEXPECTED");
2893 gst_buffer_unref (buf);
2894 return GST_FLOW_UNEXPECTED;
2898 GST_DEBUG_OBJECT (basesink, "dropping buffer, out of clipping segment");
2899 gst_buffer_unref (buf);
2906 static GstFlowReturn
2907 gst_base_sink_chain (GstPad * pad, GstBuffer * buf)
2909 GstBaseSink *basesink;
2910 GstFlowReturn result;
2912 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
2914 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH))
2917 GST_PAD_PREROLL_LOCK (pad);
2918 result = gst_base_sink_chain_unlocked (basesink, pad, buf);
2919 GST_PAD_PREROLL_UNLOCK (pad);
2927 GST_OBJECT_LOCK (pad);
2928 GST_WARNING_OBJECT (basesink,
2929 "Push on pad %s:%s, but it was not activated in push mode",
2930 GST_DEBUG_PAD_NAME (pad));
2931 GST_OBJECT_UNLOCK (pad);
2932 gst_buffer_unref (buf);
2933 /* we don't post an error message this will signal to the peer
2934 * pushing that EOS is reached. */
2935 result = GST_FLOW_UNEXPECTED;
2941 gst_base_sink_default_do_seek (GstBaseSink * sink, GstSegment * segment)
2943 gboolean res = TRUE;
2945 /* update our offset if the start/stop position was updated */
2946 if (segment->format == GST_FORMAT_BYTES) {
2947 segment->time = segment->start;
2948 } else if (segment->start == 0) {
2949 /* seek to start, we can implement a default for this. */
2953 GST_INFO_OBJECT (sink, "Can't do a default seek");
2959 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
2962 gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
2963 GstEvent * event, GstSegment * segment)
2965 /* By default, we try one of 2 things:
2966 * - For absolute seek positions, convert the requested position to our
2967 * configured processing format and place it in the output segment \
2968 * - For relative seek positions, convert our current (input) values to the
2969 * seek format, adjust by the relative seek offset and then convert back to
2970 * the processing format
2972 GstSeekType cur_type, stop_type;
2975 GstFormat seek_format, dest_format;
2978 gboolean res = TRUE;
2980 gst_event_parse_seek (event, &rate, &seek_format, &flags,
2981 &cur_type, &cur, &stop_type, &stop);
2982 dest_format = segment->format;
2984 if (seek_format == dest_format) {
2985 gst_segment_set_seek (segment, rate, seek_format, flags,
2986 cur_type, cur, stop_type, stop, &update);
2990 if (cur_type != GST_SEEK_TYPE_NONE) {
2991 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
2993 gst_pad_query_convert (sink->sinkpad, seek_format, cur, &dest_format,
2995 cur_type = GST_SEEK_TYPE_SET;
2998 if (res && stop_type != GST_SEEK_TYPE_NONE) {
2999 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3001 gst_pad_query_convert (sink->sinkpad, seek_format, stop, &dest_format,
3003 stop_type = GST_SEEK_TYPE_SET;
3006 /* And finally, configure our output segment in the desired format */
3007 gst_segment_set_seek (segment, rate, dest_format, flags, cur_type, cur,
3008 stop_type, stop, &update);
3017 GST_DEBUG_OBJECT (sink, "undefined format given, seek aborted.");
3022 /* perform a seek, only executed in pull mode */
3024 gst_base_sink_perform_seek (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3028 GstFormat seek_format, dest_format;
3030 GstSeekType cur_type, stop_type;
3031 gboolean seekseg_configured = FALSE;
3033 gboolean update, res = TRUE;
3034 GstSegment seeksegment;
3036 dest_format = sink->segment.format;
3039 GST_DEBUG_OBJECT (sink, "performing seek with event %p", event);
3040 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3041 &cur_type, &cur, &stop_type, &stop);
3043 flush = flags & GST_SEEK_FLAG_FLUSH;
3045 GST_DEBUG_OBJECT (sink, "performing seek without event");
3050 GST_DEBUG_OBJECT (sink, "flushing upstream");
3051 gst_pad_push_event (pad, gst_event_new_flush_start ());
3052 gst_base_sink_flush_start (sink, pad);
3054 GST_DEBUG_OBJECT (sink, "pausing pulling thread");
3057 GST_PAD_STREAM_LOCK (pad);
3059 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
3060 * copy the current segment info into the temp segment that we can actually
3061 * attempt the seek with. We only update the real segment if the seek suceeds. */
3062 if (!seekseg_configured) {
3063 memcpy (&seeksegment, &sink->segment, sizeof (GstSegment));
3065 /* now configure the final seek segment */
3067 if (sink->segment.format != seek_format) {
3068 /* OK, here's where we give the subclass a chance to convert the relative
3069 * seek into an absolute one in the processing format. We set up any
3070 * absolute seek above, before taking the stream lock. */
3071 if (!gst_base_sink_default_prepare_seek_segment (sink, event,
3073 GST_DEBUG_OBJECT (sink,
3074 "Preparing the seek failed after flushing. " "Aborting seek");
3078 /* The seek format matches our processing format, no need to ask the
3079 * the subclass to configure the segment. */
3080 gst_segment_set_seek (&seeksegment, rate, seek_format, flags,
3081 cur_type, cur, stop_type, stop, &update);
3084 /* Else, no seek event passed, so we're just (re)starting the
3089 GST_DEBUG_OBJECT (sink, "segment configured from %" G_GINT64_FORMAT
3090 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
3091 seeksegment.start, seeksegment.stop, seeksegment.last_stop);
3093 /* do the seek, segment.last_stop contains the new position. */
3094 res = gst_base_sink_default_do_seek (sink, &seeksegment);
3099 GST_DEBUG_OBJECT (sink, "stop flushing upstream");
3100 gst_pad_push_event (pad, gst_event_new_flush_stop ());
3101 gst_base_sink_flush_stop (sink, pad);
3102 } else if (res && sink->abidata.ABI.running) {
3103 /* we are running the current segment and doing a non-flushing seek,
3104 * close the segment first based on the last_stop. */
3105 GST_DEBUG_OBJECT (sink, "closing running segment %" G_GINT64_FORMAT
3106 " to %" G_GINT64_FORMAT, sink->segment.start, sink->segment.last_stop);
3109 /* The subclass must have converted the segment to the processing format
3111 if (res && seeksegment.format != dest_format) {
3112 GST_DEBUG_OBJECT (sink, "Subclass failed to prepare a seek segment "
3113 "in the correct format. Aborting seek.");
3117 /* if successfull seek, we update our real segment and push
3118 * out the new segment. */
3120 memcpy (&sink->segment, &seeksegment, sizeof (GstSegment));
3122 if (sink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3123 gst_element_post_message (GST_ELEMENT (sink),
3124 gst_message_new_segment_start (GST_OBJECT (sink),
3125 sink->segment.format, sink->segment.last_stop));
3129 sink->priv->discont = TRUE;
3130 sink->abidata.ABI.running = TRUE;
3132 GST_PAD_STREAM_UNLOCK (pad);
3140 gst_base_sink_loop (GstPad * pad)
3142 GstBaseSink *basesink;
3143 GstBuffer *buf = NULL;
3144 GstFlowReturn result;
3148 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
3150 g_assert (basesink->pad_mode == GST_ACTIVATE_PULL);
3152 if ((blocksize = basesink->priv->blocksize) == 0)
3155 offset = basesink->segment.last_stop;
3157 GST_DEBUG_OBJECT (basesink, "pulling %" G_GUINT64_FORMAT ", %u",
3160 result = gst_pad_pull_range (pad, offset, blocksize, &buf);
3161 if (G_UNLIKELY (result != GST_FLOW_OK))
3164 if (G_UNLIKELY (buf == NULL))
3167 offset += GST_BUFFER_SIZE (buf);
3169 gst_segment_set_last_stop (&basesink->segment, GST_FORMAT_BYTES, offset);
3171 GST_PAD_PREROLL_LOCK (pad);
3172 result = gst_base_sink_chain_unlocked (basesink, pad, buf);
3173 GST_PAD_PREROLL_UNLOCK (pad);
3174 if (G_UNLIKELY (result != GST_FLOW_OK))
3182 GST_LOG_OBJECT (basesink, "pausing task, reason %s",
3183 gst_flow_get_name (result));
3184 gst_pad_pause_task (pad);
3185 /* fatal errors and NOT_LINKED cause EOS */
3186 if (GST_FLOW_IS_FATAL (result) || result == GST_FLOW_NOT_LINKED) {
3187 if (result == GST_FLOW_UNEXPECTED) {
3188 /* perform EOS logic */
3189 if (basesink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3190 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3191 gst_message_new_segment_done (GST_OBJECT_CAST (basesink),
3192 basesink->segment.format, basesink->segment.last_stop));
3194 gst_base_sink_event (pad, gst_event_new_eos ());
3197 /* for fatal errors we post an error message, post the error
3198 * first so the app knows about the error first. */
3199 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
3200 (_("Internal data stream error.")),
3201 ("stream stopped, reason %s", gst_flow_get_name (result)));
3202 gst_base_sink_event (pad, gst_event_new_eos ());
3209 GST_LOG_OBJECT (basesink, "no buffer, pausing");
3210 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
3211 (_("Internal data flow error.")), ("element returned NULL buffer"));
3212 result = GST_FLOW_ERROR;
3218 gst_base_sink_set_flushing (GstBaseSink * basesink, GstPad * pad,
3221 GstBaseSinkClass *bclass;
3223 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3226 /* unlock any subclasses, we need to do this before grabbing the
3227 * PREROLL_LOCK since we hold this lock before going into ::render. */
3229 bclass->unlock (basesink);
3232 GST_PAD_PREROLL_LOCK (pad);
3233 basesink->flushing = flushing;
3235 /* step 1, now that we have the PREROLL lock, clear our unlock request */
3236 if (bclass->unlock_stop)
3237 bclass->unlock_stop (basesink);
3239 /* set need_preroll before we unblock the clock. If the clock is unblocked
3240 * before timing out, we can reuse the buffer for preroll. */
3241 basesink->need_preroll = TRUE;
3243 /* step 2, unblock clock sync (if any) or any other blocking thing */
3244 if (basesink->clock_id) {
3245 gst_clock_id_unschedule (basesink->clock_id);
3248 /* flush out the data thread if it's locked in finish_preroll, this will
3249 * also flush out the EOS state */
3250 GST_DEBUG_OBJECT (basesink,
3251 "flushing out data thread, need preroll to TRUE");
3252 gst_base_sink_preroll_queue_flush (basesink, pad);
3254 GST_PAD_PREROLL_UNLOCK (pad);
3260 gst_base_sink_default_activate_pull (GstBaseSink * basesink, gboolean active)
3266 result = gst_pad_start_task (basesink->sinkpad,
3267 (GstTaskFunction) gst_base_sink_loop, basesink->sinkpad);
3269 /* step 2, make sure streaming finishes */
3270 result = gst_pad_stop_task (basesink->sinkpad);
3277 gst_base_sink_pad_activate (GstPad * pad)
3279 gboolean result = FALSE;
3280 GstBaseSink *basesink;
3282 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3284 GST_DEBUG_OBJECT (basesink, "Trying pull mode first");
3286 gst_base_sink_set_flushing (basesink, pad, FALSE);
3288 /* we need to have the pull mode enabled */
3289 if (!basesink->can_activate_pull) {
3290 GST_DEBUG_OBJECT (basesink, "pull mode disabled");
3294 /* check if downstreams supports pull mode at all */
3295 if (!gst_pad_check_pull_range (pad)) {
3296 GST_DEBUG_OBJECT (basesink, "pull mode not supported");
3300 /* set the pad mode before starting the task so that it's in the
3301 * correct state for the new thread. also the sink set_caps and get_caps
3302 * function checks this */
3303 basesink->pad_mode = GST_ACTIVATE_PULL;
3305 /* we first try to negotiate a format so that when we try to activate
3306 * downstream, it knows about our format */
3307 if (!gst_base_sink_negotiate_pull (basesink)) {
3308 GST_DEBUG_OBJECT (basesink, "failed to negotiate in pull mode");
3312 /* ok activate now */
3313 if (!gst_pad_activate_pull (pad, TRUE)) {
3314 /* clear any pending caps */
3315 GST_OBJECT_LOCK (basesink);
3316 gst_caps_replace (&basesink->priv->pull_caps, NULL);
3317 GST_OBJECT_UNLOCK (basesink);
3318 GST_DEBUG_OBJECT (basesink, "failed to activate in pull mode");
3322 GST_DEBUG_OBJECT (basesink, "Success activating pull mode");
3326 /* push mode fallback */
3328 GST_DEBUG_OBJECT (basesink, "Falling back to push mode");
3329 if ((result = gst_pad_activate_push (pad, TRUE))) {
3330 GST_DEBUG_OBJECT (basesink, "Success activating push mode");
3335 GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
3336 gst_base_sink_set_flushing (basesink, pad, TRUE);
3339 gst_object_unref (basesink);
3345 gst_base_sink_pad_activate_push (GstPad * pad, gboolean active)
3348 GstBaseSink *basesink;
3350 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3353 if (!basesink->can_activate_push) {
3355 basesink->pad_mode = GST_ACTIVATE_NONE;
3358 basesink->pad_mode = GST_ACTIVATE_PUSH;
3361 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH)) {
3362 g_warning ("Internal GStreamer activation error!!!");
3365 gst_base_sink_set_flushing (basesink, pad, TRUE);
3367 basesink->pad_mode = GST_ACTIVATE_NONE;
3371 gst_object_unref (basesink);
3377 gst_base_sink_negotiate_pull (GstBaseSink * basesink)
3384 /* this returns the intersection between our caps and the peer caps. If there
3385 * is no peer, it returns NULL and we can't operate in pull mode so we can
3386 * fail the negotiation. */
3387 caps = gst_pad_get_allowed_caps (GST_BASE_SINK_PAD (basesink));
3388 if (caps == NULL || gst_caps_is_empty (caps))
3389 goto no_caps_possible;
3391 GST_DEBUG_OBJECT (basesink, "allowed caps: %" GST_PTR_FORMAT, caps);
3393 caps = gst_caps_make_writable (caps);
3394 /* get the first (prefered) format */
3395 gst_caps_truncate (caps);
3397 gst_pad_fixate_caps (GST_BASE_SINK_PAD (basesink), caps);
3399 GST_DEBUG_OBJECT (basesink, "fixated to: %" GST_PTR_FORMAT, caps);
3401 if (gst_caps_is_any (caps)) {
3402 GST_DEBUG_OBJECT (basesink, "caps were ANY after fixating, "
3404 /* neither side has template caps in this case, so they are prepared for
3405 pull() without setcaps() */
3407 } else if (gst_caps_is_fixed (caps)) {
3408 if (!gst_pad_set_caps (GST_BASE_SINK_PAD (basesink), caps))
3409 goto could_not_set_caps;
3411 GST_OBJECT_LOCK (basesink);
3412 gst_caps_replace (&basesink->priv->pull_caps, caps);
3413 GST_OBJECT_UNLOCK (basesink);
3418 gst_caps_unref (caps);
3424 GST_INFO_OBJECT (basesink, "Pipeline could not agree on caps");
3425 GST_DEBUG_OBJECT (basesink, "get_allowed_caps() returned EMPTY");
3427 gst_caps_unref (caps);
3432 GST_INFO_OBJECT (basesink, "Could not set caps: %" GST_PTR_FORMAT, caps);
3433 gst_caps_unref (caps);
3438 /* this won't get called until we implement an activate function */
3440 gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active)
3442 gboolean result = FALSE;
3443 GstBaseSink *basesink;
3444 GstBaseSinkClass *bclass;
3446 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3447 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3453 /* we mark we have a newsegment here because pull based
3454 * mode works just fine without having a newsegment before the
3456 format = GST_FORMAT_BYTES;
3458 gst_segment_init (&basesink->segment, format);
3459 gst_segment_init (basesink->abidata.ABI.clip_segment, format);
3460 GST_OBJECT_LOCK (basesink);
3461 basesink->have_newsegment = TRUE;
3462 GST_OBJECT_UNLOCK (basesink);
3464 /* get the peer duration in bytes */
3465 result = gst_pad_query_peer_duration (pad, &format, &duration);
3467 GST_DEBUG_OBJECT (basesink,
3468 "setting duration in bytes to %" G_GINT64_FORMAT, duration);
3469 gst_segment_set_duration (basesink->abidata.ABI.clip_segment, format,
3471 gst_segment_set_duration (&basesink->segment, format, duration);
3473 GST_DEBUG_OBJECT (basesink, "unknown duration");
3476 if (bclass->activate_pull)
3477 result = bclass->activate_pull (basesink, TRUE);
3482 goto activate_failed;
3484 /* but if starting the thread fails, set it back */
3486 basesink->pad_mode = GST_ACTIVATE_NONE;
3488 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PULL)) {
3489 g_warning ("Internal GStreamer activation error!!!");
3492 result = gst_base_sink_set_flushing (basesink, pad, TRUE);
3493 if (bclass->activate_pull)
3494 result &= bclass->activate_pull (basesink, FALSE);
3495 basesink->pad_mode = GST_ACTIVATE_NONE;
3496 /* clear any pending caps */
3497 GST_OBJECT_LOCK (basesink);
3498 gst_caps_replace (&basesink->priv->pull_caps, NULL);
3499 GST_OBJECT_UNLOCK (basesink);
3502 gst_object_unref (basesink);
3509 GST_ERROR_OBJECT (basesink, "subclass failed to activate in pull mode");
3514 /* send an event to our sinkpad peer. */
3516 gst_base_sink_send_event (GstElement * element, GstEvent * event)
3519 GstBaseSink *basesink = GST_BASE_SINK (element);
3520 gboolean forward, result = TRUE;
3521 GstActivateMode mode;
3523 GST_OBJECT_LOCK (element);
3524 /* get the pad and the scheduling mode */
3525 pad = gst_object_ref (basesink->sinkpad);
3526 mode = basesink->pad_mode;
3527 GST_OBJECT_UNLOCK (element);
3529 /* only push UPSTREAM events upstream and if we are in push mode */
3530 forward = GST_EVENT_IS_UPSTREAM (event) && (mode == GST_ACTIVATE_PUSH);
3532 switch (GST_EVENT_TYPE (event)) {
3533 case GST_EVENT_LATENCY:
3535 GstClockTime latency;
3537 gst_event_parse_latency (event, &latency);
3539 /* store the latency. We use this to adjust the running_time before syncing
3540 * it to the clock. */
3541 GST_OBJECT_LOCK (element);
3542 basesink->priv->latency = latency;
3543 GST_OBJECT_UNLOCK (element);
3544 GST_DEBUG_OBJECT (basesink, "latency set to %" GST_TIME_FORMAT,
3545 GST_TIME_ARGS (latency));
3547 /* We forward this event so that all elements know about the global pipeline
3548 * latency. This is interesting for an element when it wants to figure out
3549 * when a particular piece of data will be rendered. */
3552 case GST_EVENT_SEEK:
3553 /* in pull mode we will execute the seek */
3554 if (mode == GST_ACTIVATE_PULL)
3555 result = gst_base_sink_perform_seek (basesink, pad, event);
3562 result = gst_pad_push_event (pad, event);
3564 /* not forwarded, unref the event */
3565 gst_event_unref (event);
3568 gst_object_unref (pad);
3573 gst_base_sink_peer_query (GstBaseSink * sink, GstQuery * query)
3576 gboolean res = FALSE;
3578 if ((peer = gst_pad_get_peer (sink->sinkpad))) {
3579 res = gst_pad_query (peer, query);
3580 gst_object_unref (peer);
3585 /* get the end position of the last seen object, this is used
3586 * for EOS and for making sure that we don't report a position we
3587 * have not reached yet. */
3589 gst_base_sink_get_position_last (GstBaseSink * basesink, GstFormat format,
3593 GstSegment *segment;
3594 gboolean ret = TRUE;
3596 segment = &basesink->segment;
3597 oformat = segment->format;
3599 if (oformat == GST_FORMAT_TIME) {
3600 /* return last observed stream time, we keep the stream time around in the
3602 *cur = basesink->priv->current_sstop;
3604 /* convert last stop to stream time */
3605 *cur = gst_segment_to_stream_time (segment, oformat, segment->last_stop);
3608 if (*cur != -1 && oformat != format) {
3609 /* convert to the target format if we need to */
3611 gst_pad_query_convert (basesink->sinkpad, oformat, *cur, &format, cur);
3614 GST_DEBUG_OBJECT (basesink, "POSITION: %" GST_TIME_FORMAT,
3615 GST_TIME_ARGS (*cur));
3620 /* get the position when we are PAUSED, this is the stream time of the buffer
3621 * that prerolled. If no buffer is prerolled (we are still flushing), this
3622 * value will be -1. */
3624 gst_base_sink_get_position_paused (GstBaseSink * basesink, GstFormat format,
3629 GstSegment *segment;
3632 /* we don't use the clip segment in pull mode, when seeking we update the
3633 * main segment directly with the new segment values without it having to be
3634 * activated by the rendering after preroll */
3635 if (basesink->pad_mode == GST_ACTIVATE_PUSH)
3636 segment = basesink->abidata.ABI.clip_segment;
3638 segment = &basesink->segment;
3639 oformat = segment->format;
3641 if (oformat == GST_FORMAT_TIME) {
3642 *cur = basesink->priv->current_sstart;
3644 *cur = gst_segment_to_stream_time (segment, oformat, segment->last_stop);
3647 time = segment->time;
3650 *cur = MAX (*cur, time);
3651 GST_DEBUG_OBJECT (basesink, "POSITION as max: %" GST_TIME_FORMAT
3652 ", time %" GST_TIME_FORMAT, GST_TIME_ARGS (*cur), GST_TIME_ARGS (time));
3654 /* we have no buffer, use the segment times. */
3655 if (segment->rate >= 0.0) {
3656 /* forward, next position is always the time of the segment */
3658 GST_DEBUG_OBJECT (basesink, "POSITION as time: %" GST_TIME_FORMAT,
3659 GST_TIME_ARGS (*cur));
3661 /* reverse, next expected timestamp is segment->stop. We use the function
3662 * to get things right for negative applied_rates. */
3663 *cur = gst_segment_to_stream_time (segment, oformat, segment->stop);
3664 GST_DEBUG_OBJECT (basesink, "reverse POSITION: %" GST_TIME_FORMAT,
3665 GST_TIME_ARGS (*cur));
3669 if (res && oformat != format) {
3671 gst_pad_query_convert (basesink->sinkpad, oformat, *cur, &format, cur);
3678 gst_base_sink_get_position (GstBaseSink * basesink, GstFormat format,
3679 gint64 * cur, gboolean * upstream)
3682 gboolean res = FALSE;
3683 GstFormat oformat, tformat;
3684 GstClockTime now, base, latency;
3685 gint64 time, accum, duration;
3689 GST_OBJECT_LOCK (basesink);
3690 /* our intermediate time format */
3691 tformat = GST_FORMAT_TIME;
3692 /* get the format in the segment */
3693 oformat = basesink->segment.format;
3695 /* can only give answer based on the clock if not EOS */
3696 if (G_UNLIKELY (basesink->eos))
3699 /* we can only get the segment when we are not NULL or READY */
3700 if (!basesink->have_newsegment)
3703 /* when not in PLAYING or when we're busy with a state change, we
3704 * cannot read from the clock so we report time based on the
3705 * last seen timestamp. */
3706 if (GST_STATE (basesink) != GST_STATE_PLAYING ||
3707 GST_STATE_PENDING (basesink) != GST_STATE_VOID_PENDING)
3710 /* we need to sync on the clock. */
3711 if (basesink->sync == FALSE)
3714 /* and we need a clock */
3715 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (basesink)) == NULL))
3718 /* collect all data we need holding the lock */
3719 if (GST_CLOCK_TIME_IS_VALID (basesink->segment.time))
3720 time = basesink->segment.time;
3724 if (GST_CLOCK_TIME_IS_VALID (basesink->segment.stop))
3725 duration = basesink->segment.stop - basesink->segment.start;
3729 base = GST_ELEMENT_CAST (basesink)->base_time;
3730 accum = basesink->segment.accum;
3731 rate = basesink->segment.rate * basesink->segment.applied_rate;
3732 gst_base_sink_get_position_last (basesink, format, &last);
3733 latency = basesink->priv->latency;
3735 gst_object_ref (clock);
3736 /* need to release the object lock before we can get the time,
3737 * a clock might take the LOCK of the provider, which could be
3738 * a basesink subclass. */
3739 GST_OBJECT_UNLOCK (basesink);
3741 now = gst_clock_get_time (clock);
3743 if (oformat != tformat) {
3744 /* convert accum, time and duration to time */
3745 if (!gst_pad_query_convert (basesink->sinkpad, oformat, accum, &tformat,
3747 goto convert_failed;
3748 if (!gst_pad_query_convert (basesink->sinkpad, oformat, duration, &tformat,
3750 goto convert_failed;
3751 if (!gst_pad_query_convert (basesink->sinkpad, oformat, time, &tformat,
3753 goto convert_failed;
3756 /* subtract base time and accumulated time from the clock time.
3757 * Make sure we don't go negative. This is the current time in
3758 * the segment which we need to scale with the combined
3759 * rate and applied rate. */
3762 base = MIN (now, base);
3764 /* for negative rates we need to count back from from the segment
3769 *cur = time + gst_guint64_to_gdouble (now - base) * rate;
3771 /* never report more than last seen position */
3773 *cur = MIN (last, *cur);
3775 gst_object_unref (clock);
3777 GST_DEBUG_OBJECT (basesink,
3778 "now %" GST_TIME_FORMAT " - base %" GST_TIME_FORMAT " - accum %"
3779 GST_TIME_FORMAT " + time %" GST_TIME_FORMAT,
3780 GST_TIME_ARGS (now), GST_TIME_ARGS (base),
3781 GST_TIME_ARGS (accum), GST_TIME_ARGS (time));
3783 if (oformat != format) {
3784 /* convert time to final format */
3785 if (!gst_pad_query_convert (basesink->sinkpad, tformat, *cur, &format, cur))
3786 goto convert_failed;
3792 GST_DEBUG_OBJECT (basesink, "res: %d, POSITION: %" GST_TIME_FORMAT,
3793 res, GST_TIME_ARGS (*cur));
3799 GST_DEBUG_OBJECT (basesink, "position in EOS");
3800 res = gst_base_sink_get_position_last (basesink, format, cur);
3801 GST_OBJECT_UNLOCK (basesink);
3806 GST_DEBUG_OBJECT (basesink, "position in PAUSED");
3807 res = gst_base_sink_get_position_paused (basesink, format, cur);
3808 GST_OBJECT_UNLOCK (basesink);
3813 /* in NULL or READY we always return FALSE and -1 */
3814 GST_DEBUG_OBJECT (basesink, "position in wrong state, return -1");
3817 GST_OBJECT_UNLOCK (basesink);
3822 /* report last seen timestamp if any, else ask upstream to answer */
3823 if ((*cur = basesink->priv->current_sstart) != -1)
3828 GST_DEBUG_OBJECT (basesink, "no sync, res %d, POSITION %" GST_TIME_FORMAT,
3829 res, GST_TIME_ARGS (*cur));
3830 GST_OBJECT_UNLOCK (basesink);
3835 GST_DEBUG_OBJECT (basesink, "convert failed, try upstream");
3842 gst_base_sink_query (GstElement * element, GstQuery * query)
3844 gboolean res = FALSE;
3846 GstBaseSink *basesink = GST_BASE_SINK (element);
3848 switch (GST_QUERY_TYPE (query)) {
3849 case GST_QUERY_POSITION:
3853 gboolean upstream = FALSE;
3855 gst_query_parse_position (query, &format, NULL);
3857 GST_DEBUG_OBJECT (basesink, "position format %d", format);
3859 /* first try to get the position based on the clock */
3861 gst_base_sink_get_position (basesink, format, &cur, &upstream))) {
3862 gst_query_set_position (query, format, cur);
3863 } else if (upstream) {
3864 /* fallback to peer query */
3865 res = gst_base_sink_peer_query (basesink, query);
3869 case GST_QUERY_DURATION:
3871 GstFormat format, uformat;
3872 gint64 duration, uduration;
3874 gst_query_parse_duration (query, &format, NULL);
3876 GST_DEBUG_OBJECT (basesink, "duration query in format %s",
3877 gst_format_get_name (format));
3879 if (basesink->pad_mode == GST_ACTIVATE_PULL) {
3880 uformat = GST_FORMAT_BYTES;
3882 /* get the duration in bytes, in pull mode that's all we are sure to
3883 * know. We have to explicitly get this value from upstream instead of
3884 * using our cached value because it might change. Duration caching
3885 * should be done at a higher level. */
3886 res = gst_pad_query_peer_duration (basesink->sinkpad, &uformat,
3889 gst_segment_set_duration (&basesink->segment, uformat, uduration);
3890 if (format != uformat) {
3891 /* convert to the requested format */
3892 res = gst_pad_query_convert (basesink->sinkpad, uformat, uduration,
3893 &format, &duration);
3895 duration = uduration;
3898 /* set the result */
3899 gst_query_set_duration (query, format, duration);
3903 /* in push mode we simply forward upstream */
3904 res = gst_base_sink_peer_query (basesink, query);
3908 case GST_QUERY_LATENCY:
3910 gboolean live, us_live;
3911 GstClockTime min, max;
3913 if ((res = gst_base_sink_query_latency (basesink, &live, &us_live, &min,
3915 gst_query_set_latency (query, live, min, max);
3919 case GST_QUERY_JITTER:
3921 case GST_QUERY_RATE:
3922 /* gst_query_set_rate (query, basesink->segment_rate); */
3925 case GST_QUERY_SEGMENT:
3927 /* FIXME, bring start/stop to stream time */
3928 gst_query_set_segment (query, basesink->segment.rate,
3929 GST_FORMAT_TIME, basesink->segment.start, basesink->segment.stop);
3932 case GST_QUERY_SEEKING:
3933 case GST_QUERY_CONVERT:
3934 case GST_QUERY_FORMATS:
3936 res = gst_base_sink_peer_query (basesink, query);
3942 static GstStateChangeReturn
3943 gst_base_sink_change_state (GstElement * element, GstStateChange transition)
3945 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
3946 GstBaseSink *basesink = GST_BASE_SINK (element);
3947 GstBaseSinkClass *bclass;
3948 GstBaseSinkPrivate *priv;
3950 priv = basesink->priv;
3952 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3954 switch (transition) {
3955 case GST_STATE_CHANGE_NULL_TO_READY:
3957 if (!bclass->start (basesink))
3960 case GST_STATE_CHANGE_READY_TO_PAUSED:
3961 /* need to complete preroll before this state change completes, there
3962 * is no data flow in READY so we can safely assume we need to preroll. */
3963 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
3964 GST_DEBUG_OBJECT (basesink, "READY to PAUSED");
3965 basesink->have_newsegment = FALSE;
3966 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
3967 gst_segment_init (basesink->abidata.ABI.clip_segment,
3968 GST_FORMAT_UNDEFINED);
3969 basesink->offset = 0;
3970 basesink->have_preroll = FALSE;
3971 basesink->need_preroll = TRUE;
3972 basesink->playing_async = TRUE;
3973 priv->current_sstart = -1;
3974 priv->current_sstop = -1;
3975 priv->eos_rtime = -1;
3977 basesink->eos = FALSE;
3978 priv->received_eos = FALSE;
3979 gst_base_sink_reset_qos (basesink);
3980 priv->commited = FALSE;
3981 if (priv->async_enabled) {
3982 GST_DEBUG_OBJECT (basesink, "doing async state change");
3983 /* when async enabled, post async-start message and return ASYNC from
3984 * the state change function */
3985 ret = GST_STATE_CHANGE_ASYNC;
3986 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3987 gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
3989 priv->have_latency = TRUE;
3991 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
3993 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3994 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
3995 if (!gst_base_sink_needs_preroll (basesink)) {
3996 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, don't need preroll");
3997 /* no preroll needed anymore now. */
3998 basesink->playing_async = FALSE;
3999 basesink->need_preroll = FALSE;
4000 if (basesink->eos) {
4001 /* need to post EOS message here */
4002 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
4003 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4004 gst_message_new_eos (GST_OBJECT_CAST (basesink)));
4006 GST_DEBUG_OBJECT (basesink, "signal preroll");
4007 GST_PAD_PREROLL_SIGNAL (basesink->sinkpad);
4010 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, we are not prerolled");
4011 basesink->need_preroll = TRUE;
4012 basesink->playing_async = TRUE;
4013 priv->commited = FALSE;
4014 if (priv->async_enabled) {
4015 GST_DEBUG_OBJECT (basesink, "doing async state change");
4016 ret = GST_STATE_CHANGE_ASYNC;
4017 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4018 gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
4021 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4028 GstStateChangeReturn bret;
4030 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4031 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
4032 goto activate_failed;
4035 switch (transition) {
4036 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
4037 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED");
4038 /* FIXME, make sure we cannot enter _render first */
4040 /* we need to call ::unlock before locking PREROLL_LOCK
4041 * since we lock it before going into ::render */
4043 bclass->unlock (basesink);
4045 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4046 /* now that we have the PREROLL lock, clear our unlock request */
4047 if (bclass->unlock_stop)
4048 bclass->unlock_stop (basesink);
4050 /* we need preroll again and we set the flag before unlocking the clockid
4051 * because if the clockid is unlocked before a current buffer expired, we
4052 * can use that buffer to preroll with */
4053 basesink->need_preroll = TRUE;
4055 if (basesink->clock_id) {
4056 gst_clock_id_unschedule (basesink->clock_id);
4059 /* if we don't have a preroll buffer we need to wait for a preroll and
4061 if (!gst_base_sink_needs_preroll (basesink)) {
4062 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, we are prerolled");
4063 basesink->playing_async = FALSE;
4065 if (GST_STATE_TARGET (GST_ELEMENT (basesink)) <= GST_STATE_READY) {
4066 ret = GST_STATE_CHANGE_SUCCESS;
4068 GST_DEBUG_OBJECT (basesink,
4069 "PLAYING to PAUSED, we are not prerolled");
4070 basesink->playing_async = TRUE;
4071 priv->commited = FALSE;
4072 if (priv->async_enabled) {
4073 GST_DEBUG_OBJECT (basesink, "doing async state change");
4074 ret = GST_STATE_CHANGE_ASYNC;
4075 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4076 gst_message_new_async_start (GST_OBJECT_CAST (basesink),
4081 GST_DEBUG_OBJECT (basesink, "rendered: %" G_GUINT64_FORMAT
4082 ", dropped: %" G_GUINT64_FORMAT, priv->rendered, priv->dropped);
4084 gst_base_sink_reset_qos (basesink);
4085 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4087 case GST_STATE_CHANGE_PAUSED_TO_READY:
4088 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4089 /* start by reseting our position state with the object lock so that the
4090 * position query gets the right idea. We do this before we post the
4091 * messages so that the message handlers pick this up. */
4092 GST_OBJECT_LOCK (basesink);
4093 basesink->have_newsegment = FALSE;
4094 priv->current_sstart = -1;
4095 priv->current_sstop = -1;
4096 priv->have_latency = FALSE;
4097 GST_OBJECT_UNLOCK (basesink);
4099 gst_base_sink_set_last_buffer (basesink, NULL);
4101 if (!priv->commited) {
4102 if (priv->async_enabled) {
4103 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, posting async-done");
4105 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4106 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
4107 GST_STATE_PLAYING, GST_STATE_PAUSED, GST_STATE_READY));
4109 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4110 gst_message_new_async_done (GST_OBJECT_CAST (basesink)));
4112 priv->commited = TRUE;
4114 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, don't need_preroll");
4116 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4118 case GST_STATE_CHANGE_READY_TO_NULL:
4120 if (!bclass->stop (basesink)) {
4121 GST_WARNING_OBJECT (basesink, "failed to stop");
4124 gst_base_sink_set_last_buffer (basesink, NULL);
4135 GST_DEBUG_OBJECT (basesink, "failed to start");
4136 return GST_STATE_CHANGE_FAILURE;
4140 GST_DEBUG_OBJECT (basesink,
4141 "element failed to change states -- activation problem?");
4142 return GST_STATE_CHANGE_FAILURE;