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 */
218 /* blocksize for pulling */
223 /* seqnum of the stream */
226 gboolean call_preroll;
229 #define DO_RUNNING_AVG(avg,val,size) (((val) + ((size)-1) * (avg)) / (size))
231 /* generic running average, this has a neutral window size */
232 #define UPDATE_RUNNING_AVG(avg,val) DO_RUNNING_AVG(avg,val,8)
234 /* the windows for these running averages are experimentally obtained.
235 * possitive values get averaged more while negative values use a small
236 * window so we can react faster to badness. */
237 #define UPDATE_RUNNING_AVG_P(avg,val) DO_RUNNING_AVG(avg,val,16)
238 #define UPDATE_RUNNING_AVG_N(avg,val) DO_RUNNING_AVG(avg,val,4)
240 /* BaseSink properties */
242 #define DEFAULT_CAN_ACTIVATE_PULL FALSE /* fixme: enable me */
243 #define DEFAULT_CAN_ACTIVATE_PUSH TRUE
245 #define DEFAULT_PREROLL_QUEUE_LEN 0
246 #define DEFAULT_SYNC TRUE
247 #define DEFAULT_MAX_LATENESS -1
248 #define DEFAULT_QOS FALSE
249 #define DEFAULT_ASYNC TRUE
250 #define DEFAULT_TS_OFFSET 0
251 #define DEFAULT_BLOCKSIZE 4096
252 #define DEFAULT_RENDER_DELAY 0
257 PROP_PREROLL_QUEUE_LEN,
269 static GstElementClass *parent_class = NULL;
271 static void gst_base_sink_class_init (GstBaseSinkClass * klass);
272 static void gst_base_sink_init (GstBaseSink * trans, gpointer g_class);
273 static void gst_base_sink_finalize (GObject * object);
276 gst_base_sink_get_type (void)
278 static volatile gsize base_sink_type = 0;
280 if (g_once_init_enter (&base_sink_type)) {
282 static const GTypeInfo base_sink_info = {
283 sizeof (GstBaseSinkClass),
286 (GClassInitFunc) gst_base_sink_class_init,
289 sizeof (GstBaseSink),
291 (GInstanceInitFunc) gst_base_sink_init,
294 _type = g_type_register_static (GST_TYPE_ELEMENT,
295 "GstBaseSink", &base_sink_info, G_TYPE_FLAG_ABSTRACT);
296 g_once_init_leave (&base_sink_type, _type);
298 return base_sink_type;
301 static void gst_base_sink_set_property (GObject * object, guint prop_id,
302 const GValue * value, GParamSpec * pspec);
303 static void gst_base_sink_get_property (GObject * object, guint prop_id,
304 GValue * value, GParamSpec * pspec);
306 static gboolean gst_base_sink_send_event (GstElement * element,
308 static gboolean gst_base_sink_query (GstElement * element, GstQuery * query);
310 static GstCaps *gst_base_sink_get_caps (GstBaseSink * sink);
311 static gboolean gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps);
312 static GstFlowReturn gst_base_sink_buffer_alloc (GstBaseSink * sink,
313 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
314 static void gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
315 GstClockTime * start, GstClockTime * end);
316 static gboolean gst_base_sink_set_flushing (GstBaseSink * basesink,
317 GstPad * pad, gboolean flushing);
318 static gboolean gst_base_sink_default_activate_pull (GstBaseSink * basesink,
320 static gboolean gst_base_sink_default_do_seek (GstBaseSink * sink,
321 GstSegment * segment);
322 static gboolean gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
323 GstEvent * event, GstSegment * segment);
325 static GstStateChangeReturn gst_base_sink_change_state (GstElement * element,
326 GstStateChange transition);
328 static GstFlowReturn gst_base_sink_chain (GstPad * pad, GstBuffer * buffer);
329 static void gst_base_sink_loop (GstPad * pad);
330 static gboolean gst_base_sink_pad_activate (GstPad * pad);
331 static gboolean gst_base_sink_pad_activate_push (GstPad * pad, gboolean active);
332 static gboolean gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active);
333 static gboolean gst_base_sink_event (GstPad * pad, GstEvent * event);
334 static gboolean gst_base_sink_peer_query (GstBaseSink * sink, GstQuery * query);
336 static gboolean gst_base_sink_negotiate_pull (GstBaseSink * basesink);
338 /* check if an object was too late */
339 static gboolean gst_base_sink_is_too_late (GstBaseSink * basesink,
340 GstMiniObject * obj, GstClockTime start, GstClockTime stop,
341 GstClockReturn status, GstClockTimeDiff jitter);
342 static GstFlowReturn gst_base_sink_preroll_object (GstBaseSink * basesink,
343 GstMiniObject * obj);
346 gst_base_sink_class_init (GstBaseSinkClass * klass)
348 GObjectClass *gobject_class;
349 GstElementClass *gstelement_class;
351 gobject_class = G_OBJECT_CLASS (klass);
352 gstelement_class = GST_ELEMENT_CLASS (klass);
354 GST_DEBUG_CATEGORY_INIT (gst_base_sink_debug, "basesink", 0,
357 g_type_class_add_private (klass, sizeof (GstBaseSinkPrivate));
359 parent_class = g_type_class_peek_parent (klass);
361 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_sink_finalize);
362 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_base_sink_set_property);
363 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_base_sink_get_property);
365 /* FIXME, this next value should be configured using an event from the
366 * upstream element, ie, the BUFFER_SIZE event. */
367 g_object_class_install_property (gobject_class, PROP_PREROLL_QUEUE_LEN,
368 g_param_spec_uint ("preroll-queue-len", "Preroll queue length",
369 "Number of buffers to queue during preroll", 0, G_MAXUINT,
370 DEFAULT_PREROLL_QUEUE_LEN,
371 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
373 g_object_class_install_property (gobject_class, PROP_SYNC,
374 g_param_spec_boolean ("sync", "Sync", "Sync on the clock", DEFAULT_SYNC,
375 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
377 g_object_class_install_property (gobject_class, PROP_MAX_LATENESS,
378 g_param_spec_int64 ("max-lateness", "Max Lateness",
379 "Maximum number of nanoseconds that a buffer can be late before it "
380 "is dropped (-1 unlimited)", -1, G_MAXINT64, DEFAULT_MAX_LATENESS,
381 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
383 g_object_class_install_property (gobject_class, PROP_QOS,
384 g_param_spec_boolean ("qos", "Qos",
385 "Generate Quality-of-Service events upstream", DEFAULT_QOS,
386 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
390 * If set to #TRUE, the basesink will perform asynchronous state changes.
391 * When set to #FALSE, the sink will not signal the parent when it prerolls.
392 * Use this option when dealing with sparse streams or when synchronisation is
397 g_object_class_install_property (gobject_class, PROP_ASYNC,
398 g_param_spec_boolean ("async", "Async",
399 "Go asynchronously to PAUSED", DEFAULT_ASYNC,
400 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
402 * GstBaseSink:ts-offset
404 * Controls the final synchronisation, a negative value will render the buffer
405 * earlier while a positive value delays playback. This property can be
406 * used to fix synchronisation in bad files.
410 g_object_class_install_property (gobject_class, PROP_TS_OFFSET,
411 g_param_spec_int64 ("ts-offset", "TS Offset",
412 "Timestamp offset in nanoseconds", G_MININT64, G_MAXINT64,
413 DEFAULT_TS_OFFSET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
415 * GstBaseSink:last-buffer
417 * The last buffer that arrived in the sink and was used for preroll or for
418 * rendering. This property can be used to generate thumbnails. This property
419 * can be NULL when the sink has not yet received a bufer.
423 g_object_class_install_property (gobject_class, PROP_LAST_BUFFER,
424 gst_param_spec_mini_object ("last-buffer", "Last Buffer",
425 "The last buffer received in the sink", GST_TYPE_BUFFER,
426 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
428 * GstBaseSink:blocksize
430 * The amount of bytes to pull when operating in pull mode.
434 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
435 g_param_spec_uint ("blocksize", "Block size",
436 "Size in bytes to pull per buffer (0 = default)", 0, G_MAXUINT,
437 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
439 * GstBaseSink:render-delay
441 * The additional delay between synchronisation and actual rendering of the
442 * media. This property will add additional latency to the device in order to
443 * make other sinks compensate for the delay.
447 g_object_class_install_property (gobject_class, PROP_RENDER_DELAY,
448 g_param_spec_uint64 ("render-delay", "Render Delay",
449 "Additional render delay of the sink in nanoseconds", 0, G_MAXUINT64,
450 DEFAULT_RENDER_DELAY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
452 gstelement_class->change_state =
453 GST_DEBUG_FUNCPTR (gst_base_sink_change_state);
454 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_sink_send_event);
455 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_base_sink_query);
457 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_sink_get_caps);
458 klass->set_caps = GST_DEBUG_FUNCPTR (gst_base_sink_set_caps);
459 klass->buffer_alloc = GST_DEBUG_FUNCPTR (gst_base_sink_buffer_alloc);
460 klass->get_times = GST_DEBUG_FUNCPTR (gst_base_sink_get_times);
461 klass->activate_pull =
462 GST_DEBUG_FUNCPTR (gst_base_sink_default_activate_pull);
466 gst_base_sink_pad_getcaps (GstPad * pad)
468 GstBaseSinkClass *bclass;
470 GstCaps *caps = NULL;
472 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
473 bclass = GST_BASE_SINK_GET_CLASS (bsink);
475 if (bsink->pad_mode == GST_ACTIVATE_PULL) {
476 /* if we are operating in pull mode we only accept the negotiated caps */
477 GST_OBJECT_LOCK (pad);
478 if ((caps = GST_PAD_CAPS (pad)))
480 GST_OBJECT_UNLOCK (pad);
483 if (bclass->get_caps)
484 caps = bclass->get_caps (bsink);
487 GstPadTemplate *pad_template;
490 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass),
492 if (pad_template != NULL) {
493 caps = gst_caps_ref (gst_pad_template_get_caps (pad_template));
497 gst_object_unref (bsink);
503 gst_base_sink_pad_setcaps (GstPad * pad, GstCaps * caps)
505 GstBaseSinkClass *bclass;
509 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
510 bclass = GST_BASE_SINK_GET_CLASS (bsink);
512 if (res && bclass->set_caps)
513 res = bclass->set_caps (bsink, caps);
515 gst_object_unref (bsink);
521 gst_base_sink_pad_fixate (GstPad * pad, GstCaps * caps)
523 GstBaseSinkClass *bclass;
526 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
527 bclass = GST_BASE_SINK_GET_CLASS (bsink);
530 bclass->fixate (bsink, caps);
532 gst_object_unref (bsink);
536 gst_base_sink_pad_buffer_alloc (GstPad * pad, guint64 offset, guint size,
537 GstCaps * caps, GstBuffer ** buf)
539 GstBaseSinkClass *bclass;
541 GstFlowReturn result = GST_FLOW_OK;
543 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
544 bclass = GST_BASE_SINK_GET_CLASS (bsink);
546 if (bclass->buffer_alloc)
547 result = bclass->buffer_alloc (bsink, offset, size, caps, buf);
549 *buf = NULL; /* fallback in gstpad.c will allocate generic buffer */
551 gst_object_unref (bsink);
557 gst_base_sink_init (GstBaseSink * basesink, gpointer g_class)
559 GstPadTemplate *pad_template;
560 GstBaseSinkPrivate *priv;
562 basesink->priv = priv = GST_BASE_SINK_GET_PRIVATE (basesink);
565 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
566 g_return_if_fail (pad_template != NULL);
568 basesink->sinkpad = gst_pad_new_from_template (pad_template, "sink");
570 gst_pad_set_getcaps_function (basesink->sinkpad,
571 GST_DEBUG_FUNCPTR (gst_base_sink_pad_getcaps));
572 gst_pad_set_setcaps_function (basesink->sinkpad,
573 GST_DEBUG_FUNCPTR (gst_base_sink_pad_setcaps));
574 gst_pad_set_fixatecaps_function (basesink->sinkpad,
575 GST_DEBUG_FUNCPTR (gst_base_sink_pad_fixate));
576 gst_pad_set_bufferalloc_function (basesink->sinkpad,
577 GST_DEBUG_FUNCPTR (gst_base_sink_pad_buffer_alloc));
578 gst_pad_set_activate_function (basesink->sinkpad,
579 GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate));
580 gst_pad_set_activatepush_function (basesink->sinkpad,
581 GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate_push));
582 gst_pad_set_activatepull_function (basesink->sinkpad,
583 GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate_pull));
584 gst_pad_set_event_function (basesink->sinkpad,
585 GST_DEBUG_FUNCPTR (gst_base_sink_event));
586 gst_pad_set_chain_function (basesink->sinkpad,
587 GST_DEBUG_FUNCPTR (gst_base_sink_chain));
588 gst_element_add_pad (GST_ELEMENT_CAST (basesink), basesink->sinkpad);
590 basesink->pad_mode = GST_ACTIVATE_NONE;
591 basesink->preroll_queue = g_queue_new ();
592 basesink->abidata.ABI.clip_segment = gst_segment_new ();
593 priv->have_latency = FALSE;
595 basesink->can_activate_push = DEFAULT_CAN_ACTIVATE_PUSH;
596 basesink->can_activate_pull = DEFAULT_CAN_ACTIVATE_PULL;
598 basesink->sync = DEFAULT_SYNC;
599 basesink->abidata.ABI.max_lateness = DEFAULT_MAX_LATENESS;
600 g_atomic_int_set (&priv->qos_enabled, DEFAULT_QOS);
601 priv->async_enabled = DEFAULT_ASYNC;
602 priv->ts_offset = DEFAULT_TS_OFFSET;
603 priv->render_delay = DEFAULT_RENDER_DELAY;
604 priv->blocksize = DEFAULT_BLOCKSIZE;
606 GST_OBJECT_FLAG_SET (basesink, GST_ELEMENT_IS_SINK);
610 gst_base_sink_finalize (GObject * object)
612 GstBaseSink *basesink;
614 basesink = GST_BASE_SINK (object);
616 g_queue_free (basesink->preroll_queue);
617 gst_segment_free (basesink->abidata.ABI.clip_segment);
619 G_OBJECT_CLASS (parent_class)->finalize (object);
623 * gst_base_sink_set_sync:
625 * @sync: the new sync value.
627 * Configures @sink to synchronize on the clock or not. When
628 * @sync is FALSE, incomming samples will be played as fast as
629 * possible. If @sync is TRUE, the timestamps of the incomming
630 * buffers will be used to schedule the exact render time of its
636 gst_base_sink_set_sync (GstBaseSink * sink, gboolean sync)
638 g_return_if_fail (GST_IS_BASE_SINK (sink));
640 GST_OBJECT_LOCK (sink);
642 GST_OBJECT_UNLOCK (sink);
646 * gst_base_sink_get_sync:
649 * Checks if @sink is currently configured to synchronize against the
652 * Returns: TRUE if the sink is configured to synchronize against the clock.
657 gst_base_sink_get_sync (GstBaseSink * sink)
661 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
663 GST_OBJECT_LOCK (sink);
665 GST_OBJECT_UNLOCK (sink);
671 * gst_base_sink_set_max_lateness:
673 * @max_lateness: the new max lateness value.
675 * Sets the new max lateness value to @max_lateness. This value is
676 * used to decide if a buffer should be dropped or not based on the
677 * buffer timestamp and the current clock time. A value of -1 means
683 gst_base_sink_set_max_lateness (GstBaseSink * sink, gint64 max_lateness)
685 g_return_if_fail (GST_IS_BASE_SINK (sink));
687 GST_OBJECT_LOCK (sink);
688 sink->abidata.ABI.max_lateness = max_lateness;
689 GST_OBJECT_UNLOCK (sink);
693 * gst_base_sink_get_max_lateness:
696 * Gets the max lateness value. See gst_base_sink_set_max_lateness for
699 * Returns: The maximum time in nanoseconds that a buffer can be late
700 * before it is dropped and not rendered. A value of -1 means an
706 gst_base_sink_get_max_lateness (GstBaseSink * sink)
710 g_return_val_if_fail (GST_IS_BASE_SINK (sink), -1);
712 GST_OBJECT_LOCK (sink);
713 res = sink->abidata.ABI.max_lateness;
714 GST_OBJECT_UNLOCK (sink);
720 * gst_base_sink_set_qos_enabled:
722 * @enabled: the new qos value.
724 * Configures @sink to send Quality-of-Service events upstream.
729 gst_base_sink_set_qos_enabled (GstBaseSink * sink, gboolean enabled)
731 g_return_if_fail (GST_IS_BASE_SINK (sink));
733 g_atomic_int_set (&sink->priv->qos_enabled, enabled);
737 * gst_base_sink_is_qos_enabled:
740 * Checks if @sink is currently configured to send Quality-of-Service events
743 * Returns: TRUE if the sink is configured to perform Quality-of-Service.
748 gst_base_sink_is_qos_enabled (GstBaseSink * sink)
752 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
754 res = g_atomic_int_get (&sink->priv->qos_enabled);
760 * gst_base_sink_set_async_enabled:
762 * @enabled: the new async value.
764 * Configures @sink to perform all state changes asynchronusly. When async is
765 * disabled, the sink will immediatly go to PAUSED instead of waiting for a
766 * preroll buffer. This feature is usefull if the sink does not synchronize
767 * against the clock or when it is dealing with sparse streams.
772 gst_base_sink_set_async_enabled (GstBaseSink * sink, gboolean enabled)
774 g_return_if_fail (GST_IS_BASE_SINK (sink));
776 GST_PAD_PREROLL_LOCK (sink->sinkpad);
777 sink->priv->async_enabled = enabled;
778 GST_LOG_OBJECT (sink, "set async enabled to %d", enabled);
779 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
783 * gst_base_sink_is_async_enabled:
786 * Checks if @sink is currently configured to perform asynchronous state
789 * Returns: TRUE if the sink is configured to perform asynchronous state
795 gst_base_sink_is_async_enabled (GstBaseSink * sink)
799 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
801 GST_PAD_PREROLL_LOCK (sink->sinkpad);
802 res = sink->priv->async_enabled;
803 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
809 * gst_base_sink_set_ts_offset:
811 * @offset: the new offset
813 * Adjust the synchronisation of @sink with @offset. A negative value will
814 * render buffers earlier than their timestamp. A positive value will delay
815 * rendering. This function can be used to fix playback of badly timestamped
821 gst_base_sink_set_ts_offset (GstBaseSink * sink, GstClockTimeDiff offset)
823 g_return_if_fail (GST_IS_BASE_SINK (sink));
825 GST_OBJECT_LOCK (sink);
826 sink->priv->ts_offset = offset;
827 GST_LOG_OBJECT (sink, "set time offset to %" G_GINT64_FORMAT, offset);
828 GST_OBJECT_UNLOCK (sink);
832 * gst_base_sink_get_ts_offset:
835 * Get the synchronisation offset of @sink.
837 * Returns: The synchronisation offset.
842 gst_base_sink_get_ts_offset (GstBaseSink * sink)
844 GstClockTimeDiff res;
846 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
848 GST_OBJECT_LOCK (sink);
849 res = sink->priv->ts_offset;
850 GST_OBJECT_UNLOCK (sink);
856 * gst_base_sink_get_last_buffer:
859 * Get the last buffer that arrived in the sink and was used for preroll or for
860 * rendering. This property can be used to generate thumbnails.
862 * The #GstCaps on the buffer can be used to determine the type of the buffer.
864 * Returns: a #GstBuffer. gst_buffer_unref() after usage. This function returns
865 * NULL when no buffer has arrived in the sink yet or when the sink is not in
871 gst_base_sink_get_last_buffer (GstBaseSink * sink)
875 g_return_val_if_fail (GST_IS_BASE_SINK (sink), NULL);
877 GST_OBJECT_LOCK (sink);
878 if ((res = sink->priv->last_buffer))
879 gst_buffer_ref (res);
880 GST_OBJECT_UNLOCK (sink);
886 gst_base_sink_set_last_buffer (GstBaseSink * sink, GstBuffer * buffer)
890 GST_OBJECT_LOCK (sink);
891 old = sink->priv->last_buffer;
892 if (G_LIKELY (old != buffer)) {
893 GST_DEBUG_OBJECT (sink, "setting last buffer to %p", buffer);
894 if (G_LIKELY (buffer))
895 gst_buffer_ref (buffer);
896 sink->priv->last_buffer = buffer;
900 GST_OBJECT_UNLOCK (sink);
902 /* avoid unreffing with the lock because cleanup code might want to take the
905 gst_buffer_unref (old);
909 * gst_base_sink_get_latency:
912 * Get the currently configured latency.
914 * Returns: The configured latency.
919 gst_base_sink_get_latency (GstBaseSink * sink)
923 GST_OBJECT_LOCK (sink);
924 res = sink->priv->latency;
925 GST_OBJECT_UNLOCK (sink);
931 * gst_base_sink_query_latency:
933 * @live: if the sink is live
934 * @upstream_live: if an upstream element is live
935 * @min_latency: the min latency of the upstream elements
936 * @max_latency: the max latency of the upstream elements
938 * Query the sink for the latency parameters. The latency will be queried from
939 * the upstream elements. @live will be TRUE if @sink is configured to
940 * synchronize against the clock. @upstream_live will be TRUE if an upstream
943 * If both @live and @upstream_live are TRUE, the sink will want to compensate
944 * for the latency introduced by the upstream elements by setting the
945 * @min_latency to a strictly possitive value.
947 * This function is mostly used by subclasses.
949 * Returns: TRUE if the query succeeded.
954 gst_base_sink_query_latency (GstBaseSink * sink, gboolean * live,
955 gboolean * upstream_live, GstClockTime * min_latency,
956 GstClockTime * max_latency)
958 gboolean l, us_live, res, have_latency;
959 GstClockTime min, max, render_delay;
961 GstClockTime us_min, us_max;
963 /* we are live when we sync to the clock */
964 GST_OBJECT_LOCK (sink);
966 have_latency = sink->priv->have_latency;
967 render_delay = sink->priv->render_delay;
968 GST_OBJECT_UNLOCK (sink);
970 /* assume no latency */
976 GST_DEBUG_OBJECT (sink, "we are ready for LATENCY query");
977 /* we are ready for a latency query this is when we preroll or when we are
979 query = gst_query_new_latency ();
981 /* ask the peer for the latency */
982 if ((res = gst_base_sink_peer_query (sink, query))) {
983 /* get upstream min and max latency */
984 gst_query_parse_latency (query, &us_live, &us_min, &us_max);
987 /* upstream live, use its latency, subclasses should use these
988 * values to create the complete latency. */
993 /* we need to add the render delay if we are live */
1000 gst_query_unref (query);
1002 GST_DEBUG_OBJECT (sink, "we are not yet ready for LATENCY query");
1006 /* not live, we tried to do the query, if it failed we return TRUE anyway */
1010 GST_DEBUG_OBJECT (sink, "latency query failed but we are not live");
1012 GST_DEBUG_OBJECT (sink, "latency query failed and we are live");
1017 GST_DEBUG_OBJECT (sink, "latency query: live: %d, have_latency %d,"
1018 " upstream: %d, min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT, l,
1019 have_latency, us_live, GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1024 *upstream_live = us_live;
1034 * gst_base_sink_set_render_delay:
1035 * @sink: a #GstBaseSink
1036 * @delay: the new delay
1038 * Set the render delay in @sink to @delay. The render delay is the time
1039 * between actual rendering of a buffer and its synchronisation time. Some
1040 * devices might delay media rendering which can be compensated for with this
1043 * After calling this function, this sink will report additional latency and
1044 * other sinks will adjust their latency to delay the rendering of their media.
1046 * This function is usually called by subclasses.
1051 gst_base_sink_set_render_delay (GstBaseSink * sink, GstClockTime delay)
1053 GstClockTime old_render_delay;
1055 g_return_if_fail (GST_IS_BASE_SINK (sink));
1057 GST_OBJECT_LOCK (sink);
1058 old_render_delay = sink->priv->render_delay;
1059 sink->priv->render_delay = delay;
1060 GST_LOG_OBJECT (sink, "set render delay to %" GST_TIME_FORMAT,
1061 GST_TIME_ARGS (delay));
1062 GST_OBJECT_UNLOCK (sink);
1064 if (delay != old_render_delay) {
1065 GST_DEBUG_OBJECT (sink, "posting latency changed");
1066 gst_element_post_message (GST_ELEMENT_CAST (sink),
1067 gst_message_new_latency (GST_OBJECT_CAST (sink)));
1072 * gst_base_sink_get_render_delay:
1073 * @sink: a #GstBaseSink
1075 * Get the render delay of @sink. see gst_base_sink_set_render_delay() for more
1076 * information about the render delay.
1078 * Returns: the render delay of @sink.
1083 gst_base_sink_get_render_delay (GstBaseSink * sink)
1085 GstClockTimeDiff res;
1087 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1089 GST_OBJECT_LOCK (sink);
1090 res = sink->priv->render_delay;
1091 GST_OBJECT_UNLOCK (sink);
1097 * gst_base_sink_set_blocksize:
1098 * @sink: a #GstBaseSink
1099 * @blocksize: the blocksize in bytes
1101 * Set the number of bytes that the sink will pull when it is operating in pull
1107 gst_base_sink_set_blocksize (GstBaseSink * sink, guint blocksize)
1109 g_return_if_fail (GST_IS_BASE_SINK (sink));
1111 GST_OBJECT_LOCK (sink);
1112 sink->priv->blocksize = blocksize;
1113 GST_LOG_OBJECT (sink, "set blocksize to %u", blocksize);
1114 GST_OBJECT_UNLOCK (sink);
1118 * gst_base_sink_get_blocksize:
1119 * @sink: a #GstBaseSink
1121 * Get the number of bytes that the sink will pull when it is operating in pull
1124 * Returns: the number of bytes @sink will pull in pull mode.
1129 gst_base_sink_get_blocksize (GstBaseSink * sink)
1133 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1135 GST_OBJECT_LOCK (sink);
1136 res = sink->priv->blocksize;
1137 GST_OBJECT_UNLOCK (sink);
1143 gst_base_sink_set_property (GObject * object, guint prop_id,
1144 const GValue * value, GParamSpec * pspec)
1146 GstBaseSink *sink = GST_BASE_SINK (object);
1149 case PROP_PREROLL_QUEUE_LEN:
1150 /* preroll lock necessary to serialize with finish_preroll */
1151 GST_PAD_PREROLL_LOCK (sink->sinkpad);
1152 sink->preroll_queue_max_len = g_value_get_uint (value);
1153 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
1156 gst_base_sink_set_sync (sink, g_value_get_boolean (value));
1158 case PROP_MAX_LATENESS:
1159 gst_base_sink_set_max_lateness (sink, g_value_get_int64 (value));
1162 gst_base_sink_set_qos_enabled (sink, g_value_get_boolean (value));
1165 gst_base_sink_set_async_enabled (sink, g_value_get_boolean (value));
1167 case PROP_TS_OFFSET:
1168 gst_base_sink_set_ts_offset (sink, g_value_get_int64 (value));
1170 case PROP_BLOCKSIZE:
1171 gst_base_sink_set_blocksize (sink, g_value_get_uint (value));
1173 case PROP_RENDER_DELAY:
1174 gst_base_sink_set_render_delay (sink, g_value_get_uint64 (value));
1177 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1183 gst_base_sink_get_property (GObject * object, guint prop_id, GValue * value,
1186 GstBaseSink *sink = GST_BASE_SINK (object);
1189 case PROP_PREROLL_QUEUE_LEN:
1190 GST_PAD_PREROLL_LOCK (sink->sinkpad);
1191 g_value_set_uint (value, sink->preroll_queue_max_len);
1192 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
1195 g_value_set_boolean (value, gst_base_sink_get_sync (sink));
1197 case PROP_MAX_LATENESS:
1198 g_value_set_int64 (value, gst_base_sink_get_max_lateness (sink));
1201 g_value_set_boolean (value, gst_base_sink_is_qos_enabled (sink));
1204 g_value_set_boolean (value, gst_base_sink_is_async_enabled (sink));
1206 case PROP_TS_OFFSET:
1207 g_value_set_int64 (value, gst_base_sink_get_ts_offset (sink));
1209 case PROP_LAST_BUFFER:
1210 gst_value_take_buffer (value, gst_base_sink_get_last_buffer (sink));
1212 case PROP_BLOCKSIZE:
1213 g_value_set_uint (value, gst_base_sink_get_blocksize (sink));
1215 case PROP_RENDER_DELAY:
1216 g_value_set_uint64 (value, gst_base_sink_get_render_delay (sink));
1219 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1226 gst_base_sink_get_caps (GstBaseSink * sink)
1232 gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps)
1237 static GstFlowReturn
1238 gst_base_sink_buffer_alloc (GstBaseSink * sink, guint64 offset, guint size,
1239 GstCaps * caps, GstBuffer ** buf)
1245 /* with PREROLL_LOCK, STREAM_LOCK */
1247 gst_base_sink_preroll_queue_flush (GstBaseSink * basesink, GstPad * pad)
1251 GST_DEBUG_OBJECT (basesink, "flushing queue %p", basesink);
1252 while ((obj = g_queue_pop_head (basesink->preroll_queue))) {
1253 GST_DEBUG_OBJECT (basesink, "popped %p", obj);
1254 gst_mini_object_unref (obj);
1256 /* we can't have EOS anymore now */
1257 basesink->eos = FALSE;
1258 basesink->priv->received_eos = FALSE;
1259 basesink->have_preroll = FALSE;
1260 basesink->eos_queued = FALSE;
1261 basesink->preroll_queued = 0;
1262 basesink->buffers_queued = 0;
1263 basesink->events_queued = 0;
1264 /* can't report latency anymore until we preroll again */
1265 if (basesink->priv->async_enabled) {
1266 GST_OBJECT_LOCK (basesink);
1267 basesink->priv->have_latency = FALSE;
1268 GST_OBJECT_UNLOCK (basesink);
1270 /* and signal any waiters now */
1271 GST_PAD_PREROLL_SIGNAL (pad);
1274 /* with STREAM_LOCK, configures given segment with the event information. */
1276 gst_base_sink_configure_segment (GstBaseSink * basesink, GstPad * pad,
1277 GstEvent * event, GstSegment * segment)
1280 gdouble rate, arate;
1286 /* the newsegment event is needed to bring the buffer timestamps to the
1287 * stream time and to drop samples outside of the playback segment. */
1288 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1289 &start, &stop, &time);
1291 /* The segment is protected with both the STREAM_LOCK and the OBJECT_LOCK.
1292 * We protect with the OBJECT_LOCK so that we can use the values to
1293 * safely answer a POSITION query. */
1294 GST_OBJECT_LOCK (basesink);
1295 gst_segment_set_newsegment_full (segment, update, rate, arate, format, start,
1298 if (format == GST_FORMAT_TIME) {
1299 GST_DEBUG_OBJECT (basesink,
1300 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1301 "format GST_FORMAT_TIME, "
1302 "%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
1303 ", time %" GST_TIME_FORMAT ", accum %" GST_TIME_FORMAT,
1304 update, rate, arate, GST_TIME_ARGS (segment->start),
1305 GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time),
1306 GST_TIME_ARGS (segment->accum));
1308 GST_DEBUG_OBJECT (basesink,
1309 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1311 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
1312 G_GINT64_FORMAT ", accum %" G_GINT64_FORMAT, update, rate, arate,
1313 segment->format, segment->start, segment->stop, segment->time,
1316 GST_OBJECT_UNLOCK (basesink);
1319 /* with PREROLL_LOCK, STREAM_LOCK */
1321 gst_base_sink_commit_state (GstBaseSink * basesink)
1323 /* commit state and proceed to next pending state */
1324 GstState current, next, pending, post_pending;
1325 gboolean post_paused = FALSE;
1326 gboolean post_async_done = FALSE;
1327 gboolean post_playing = FALSE;
1329 /* we are certainly not playing async anymore now */
1330 basesink->playing_async = FALSE;
1332 GST_OBJECT_LOCK (basesink);
1333 current = GST_STATE (basesink);
1334 next = GST_STATE_NEXT (basesink);
1335 pending = GST_STATE_PENDING (basesink);
1336 post_pending = pending;
1339 case GST_STATE_PLAYING:
1341 GstBaseSinkClass *bclass;
1342 GstStateChangeReturn ret;
1344 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1346 GST_DEBUG_OBJECT (basesink, "commiting state to PLAYING");
1348 basesink->need_preroll = FALSE;
1349 post_async_done = TRUE;
1350 basesink->priv->commited = TRUE;
1351 post_playing = TRUE;
1352 /* post PAUSED too when we were READY */
1353 if (current == GST_STATE_READY) {
1357 /* make sure we notify the subclass of async playing */
1358 if (bclass->async_play) {
1359 GST_WARNING_OBJECT (basesink, "deprecated async_play");
1360 ret = bclass->async_play (basesink);
1361 if (ret == GST_STATE_CHANGE_FAILURE)
1366 case GST_STATE_PAUSED:
1367 GST_DEBUG_OBJECT (basesink, "commiting state to PAUSED");
1369 post_async_done = TRUE;
1370 basesink->priv->commited = TRUE;
1371 post_pending = GST_STATE_VOID_PENDING;
1373 case GST_STATE_READY:
1374 case GST_STATE_NULL:
1376 case GST_STATE_VOID_PENDING:
1377 goto nothing_pending;
1382 /* we can report latency queries now */
1383 basesink->priv->have_latency = TRUE;
1385 GST_STATE (basesink) = pending;
1386 GST_STATE_NEXT (basesink) = GST_STATE_VOID_PENDING;
1387 GST_STATE_PENDING (basesink) = GST_STATE_VOID_PENDING;
1388 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_SUCCESS;
1389 GST_OBJECT_UNLOCK (basesink);
1392 GST_DEBUG_OBJECT (basesink, "posting PAUSED state change message");
1393 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1394 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1395 current, next, post_pending));
1397 if (post_async_done) {
1398 GST_DEBUG_OBJECT (basesink, "posting async-done message");
1399 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1400 gst_message_new_async_done (GST_OBJECT_CAST (basesink)));
1403 GST_DEBUG_OBJECT (basesink, "posting PLAYING state change message");
1404 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1405 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1406 next, pending, GST_STATE_VOID_PENDING));
1409 GST_STATE_BROADCAST (basesink);
1415 /* Depending on the state, set our vars. We get in this situation when the
1416 * state change function got a change to update the state vars before the
1417 * streaming thread did. This is fine but we need to make sure that we
1418 * update the need_preroll var since it was TRUE when we got here and might
1419 * become FALSE if we got to PLAYING. */
1420 GST_DEBUG_OBJECT (basesink, "nothing to commit, now in %s",
1421 gst_element_state_get_name (current));
1423 case GST_STATE_PLAYING:
1424 basesink->need_preroll = FALSE;
1426 case GST_STATE_PAUSED:
1427 basesink->need_preroll = TRUE;
1430 basesink->need_preroll = FALSE;
1431 basesink->flushing = TRUE;
1434 /* we can report latency queries now */
1435 basesink->priv->have_latency = TRUE;
1436 GST_OBJECT_UNLOCK (basesink);
1441 /* app is going to READY */
1442 GST_DEBUG_OBJECT (basesink, "stopping");
1443 basesink->need_preroll = FALSE;
1444 basesink->flushing = TRUE;
1445 GST_OBJECT_UNLOCK (basesink);
1450 GST_DEBUG_OBJECT (basesink, "async commit failed");
1451 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_FAILURE;
1452 GST_OBJECT_UNLOCK (basesink);
1458 /* with STREAM_LOCK, PREROLL_LOCK
1460 * Returns TRUE if the object needs synchronisation and takes therefore
1461 * part in prerolling.
1463 * rsstart/rsstop contain the start/stop in stream time.
1464 * rrstart/rrstop contain the start/stop in running time.
1467 gst_base_sink_get_sync_times (GstBaseSink * basesink, GstMiniObject * obj,
1468 GstClockTime * rsstart, GstClockTime * rsstop,
1469 GstClockTime * rrstart, GstClockTime * rrstop, gboolean * do_sync,
1470 GstSegment * segment)
1472 GstBaseSinkClass *bclass;
1474 GstClockTime start, stop; /* raw start/stop timestamps */
1475 gint64 cstart, cstop; /* clipped raw timestamps */
1476 gint64 rstart, rstop; /* clipped timestamps converted to running time */
1477 GstClockTime sstart, sstop; /* clipped timestamps converted to stream time */
1479 GstBaseSinkPrivate *priv;
1481 priv = basesink->priv;
1483 /* start with nothing */
1486 if (G_UNLIKELY (GST_IS_EVENT (obj))) {
1487 GstEvent *event = GST_EVENT_CAST (obj);
1489 switch (GST_EVENT_TYPE (event)) {
1490 /* EOS event needs syncing */
1493 if (basesink->segment.rate >= 0.0) {
1494 sstart = sstop = priv->current_sstop;
1496 /* we have not seen a buffer yet, use the segment values */
1497 sstart = sstop = gst_segment_to_stream_time (&basesink->segment,
1498 basesink->segment.format, basesink->segment.stop);
1501 sstart = sstop = priv->current_sstart;
1503 /* we have not seen a buffer yet, use the segment values */
1504 sstart = sstop = gst_segment_to_stream_time (&basesink->segment,
1505 basesink->segment.format, basesink->segment.start);
1509 rstart = rstop = priv->eos_rtime;
1510 *do_sync = rstart != -1;
1511 GST_DEBUG_OBJECT (basesink, "sync times for EOS %" GST_TIME_FORMAT,
1512 GST_TIME_ARGS (rstart));
1516 /* other events do not need syncing */
1517 /* FIXME, maybe NEWSEGMENT might need synchronisation
1518 * since the POSITION query depends on accumulated times and
1519 * we cannot accumulate the current segment before the previous
1526 /* else do buffer sync code */
1527 buffer = GST_BUFFER_CAST (obj);
1529 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1531 /* just get the times to see if we need syncing */
1532 if (bclass->get_times)
1533 bclass->get_times (basesink, buffer, &start, &stop);
1536 gst_base_sink_get_times (basesink, buffer, &start, &stop);
1542 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
1543 ", stop: %" GST_TIME_FORMAT ", do_sync %d", GST_TIME_ARGS (start),
1544 GST_TIME_ARGS (stop), *do_sync);
1546 /* collect segment and format for code clarity */
1547 format = segment->format;
1549 /* no timestamp clipping if we did not * get a TIME segment format */
1550 if (G_UNLIKELY (format != GST_FORMAT_TIME)) {
1553 /* do running and stream time in TIME format */
1554 format = GST_FORMAT_TIME;
1559 if (G_UNLIKELY (!gst_segment_clip (segment, GST_FORMAT_TIME,
1560 (gint64) start, (gint64) stop, &cstart, &cstop)))
1561 goto out_of_segment;
1563 if (G_UNLIKELY (start != cstart || stop != cstop)) {
1564 GST_DEBUG_OBJECT (basesink, "clipped to: start %" GST_TIME_FORMAT
1565 ", stop: %" GST_TIME_FORMAT, GST_TIME_ARGS (cstart),
1566 GST_TIME_ARGS (cstop));
1569 /* set last stop position */
1570 if (G_LIKELY (cstop != GST_CLOCK_TIME_NONE))
1571 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, cstop);
1573 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, cstart);
1576 /* this can produce wrong values if we accumulated non-TIME segments. If this happens,
1577 * upstream is behaving very badly */
1578 sstart = gst_segment_to_stream_time (segment, format, cstart);
1579 sstop = gst_segment_to_stream_time (segment, format, cstop);
1580 rstart = gst_segment_to_running_time (segment, format, cstart);
1581 rstop = gst_segment_to_running_time (segment, format, cstop);
1590 /* buffers and EOS always need syncing and preroll */
1596 /* should not happen since we clip them in the chain function already,
1597 * we return FALSE so that we don't try to sync on it. */
1598 GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
1599 (NULL), ("unexpected buffer out of segment found."));
1600 GST_LOG_OBJECT (basesink, "buffer skipped, not in segment");
1605 /* with STREAM_LOCK, PREROLL_LOCK, LOCK
1606 * adjust a timestamp with the latency and timestamp offset */
1608 gst_base_sink_adjust_time (GstBaseSink * basesink, GstClockTime time)
1610 GstClockTimeDiff ts_offset;
1612 /* don't do anything funny with invalid timestamps */
1613 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
1616 time += basesink->priv->latency;
1618 /* apply offset, be carefull for underflows */
1619 ts_offset = basesink->priv->ts_offset;
1620 if (ts_offset < 0) {
1621 ts_offset = -ts_offset;
1622 if (ts_offset < time)
1633 * gst_base_sink_wait_clock:
1635 * @time: the running_time to be reached
1636 * @jitter: the jitter to be filled with time diff (can be NULL)
1638 * This function will block until @time is reached. It is usually called by
1639 * subclasses that use their own internal synchronisation.
1641 * If @time is not valid, no sycnhronisation is done and #GST_CLOCK_BADTIME is
1642 * returned. Likewise, if synchronisation is disabled in the element or there
1643 * is no clock, no synchronisation is done and #GST_CLOCK_BADTIME is returned.
1645 * This function should only be called with the PREROLL_LOCK held, like when
1646 * receiving an EOS event in the ::event vmethod or when receiving a buffer in
1647 * the ::render vmethod.
1649 * The @time argument should be the running_time of when this method should
1650 * return and is not adjusted with any latency or offset configured in the
1655 * Returns: #GstClockReturn
1658 gst_base_sink_wait_clock (GstBaseSink * sink, GstClockTime time,
1659 GstClockTimeDiff * jitter)
1665 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
1668 GST_OBJECT_LOCK (sink);
1669 if (G_UNLIKELY (!sink->sync))
1672 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (sink)) == NULL))
1675 /* add base_time to running_time to get the time against the clock */
1676 time += GST_ELEMENT_CAST (sink)->base_time;
1678 id = gst_clock_new_single_shot_id (clock, time);
1679 GST_OBJECT_UNLOCK (sink);
1681 /* A blocking wait is performed on the clock. We save the ClockID
1682 * so we can unlock the entry at any time. While we are blocking, we
1683 * release the PREROLL_LOCK so that other threads can interrupt the
1685 sink->clock_id = id;
1686 /* release the preroll lock while waiting */
1687 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
1689 ret = gst_clock_id_wait (id, jitter);
1691 GST_PAD_PREROLL_LOCK (sink->sinkpad);
1692 gst_clock_id_unref (id);
1693 sink->clock_id = NULL;
1697 /* no syncing needed */
1700 GST_DEBUG_OBJECT (sink, "time not valid, no sync needed");
1701 return GST_CLOCK_BADTIME;
1705 GST_DEBUG_OBJECT (sink, "sync disabled");
1706 GST_OBJECT_UNLOCK (sink);
1707 return GST_CLOCK_BADTIME;
1711 GST_DEBUG_OBJECT (sink, "no clock, can't sync");
1712 GST_OBJECT_UNLOCK (sink);
1713 return GST_CLOCK_BADTIME;
1718 * gst_base_sink_wait_preroll:
1721 * If the #GstBaseSinkClass::render method performs its own synchronisation against
1722 * the clock it must unblock when going from PLAYING to the PAUSED state and call
1723 * this method before continuing to render the remaining data.
1725 * This function will block until a state change to PLAYING happens (in which
1726 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
1727 * to a state change to READY or a FLUSH event (in which case this function
1728 * returns #GST_FLOW_WRONG_STATE).
1730 * This function should only be called with the PREROLL_LOCK held, like in the
1735 * Returns: #GST_FLOW_OK if the preroll completed and processing can
1736 * continue. Any other return value should be returned from the render vmethod.
1739 gst_base_sink_wait_preroll (GstBaseSink * sink)
1741 sink->have_preroll = TRUE;
1742 GST_DEBUG_OBJECT (sink, "waiting in preroll for flush or PLAYING");
1743 /* block until the state changes, or we get a flush, or something */
1744 GST_PAD_PREROLL_WAIT (sink->sinkpad);
1745 sink->have_preroll = FALSE;
1746 if (G_UNLIKELY (sink->flushing))
1748 GST_DEBUG_OBJECT (sink, "continue after preroll");
1755 GST_DEBUG_OBJECT (sink, "preroll interrupted");
1756 return GST_FLOW_WRONG_STATE;
1761 * gst_base_sink_do_preroll:
1763 * @obj: the object that caused the preroll
1765 * If the @sink spawns its own thread for pulling buffers from upstream it
1766 * should call this method after it has pulled a buffer. If the element needed
1767 * to preroll, this function will perform the preroll and will then block
1768 * until the element state is changed.
1770 * This function should be called with the PREROLL_LOCK held.
1774 * Returns: #GST_FLOW_OK if the preroll completed and processing can
1775 * continue. Any other return value should be returned from the render vmethod.
1778 gst_base_sink_do_preroll (GstBaseSink * sink, GstMiniObject * obj)
1782 while (G_UNLIKELY (sink->need_preroll)) {
1783 GST_DEBUG_OBJECT (sink, "prerolling object %p", obj);
1785 ret = gst_base_sink_preroll_object (sink, obj);
1786 if (ret != GST_FLOW_OK)
1787 goto preroll_failed;
1789 /* need to recheck here because the commit state could have
1790 * made us not need the preroll anymore */
1791 if (G_LIKELY (sink->need_preroll)) {
1792 /* block until the state changes, or we get a flush, or something */
1793 ret = gst_base_sink_wait_preroll (sink);
1794 if (ret != GST_FLOW_OK)
1795 goto preroll_failed;
1803 GST_DEBUG_OBJECT (sink, "preroll failed %d", ret);
1809 * gst_base_sink_wait_eos:
1811 * @time: the running_time to be reached
1812 * @jitter: the jitter to be filled with time diff (can be NULL)
1814 * This function will block until @time is reached. It is usually called by
1815 * subclasses that use their own internal synchronisation but want to let the
1816 * EOS be handled by the base class.
1818 * This function should only be called with the PREROLL_LOCK held, like when
1819 * receiving an EOS event in the ::event vmethod.
1821 * The @time argument should be the running_time of when the EOS should happen
1822 * and will be adjusted with any latency and offset configured in the sink.
1826 * Returns: #GstFlowReturn
1829 gst_base_sink_wait_eos (GstBaseSink * sink, GstClockTime time,
1830 GstClockTimeDiff * jitter)
1832 GstClockReturn status;
1838 GST_DEBUG_OBJECT (sink, "checking preroll");
1840 /* first wait for the playing state before we can continue */
1841 if (G_UNLIKELY (sink->need_preroll)) {
1842 ret = gst_base_sink_wait_preroll (sink);
1843 if (ret != GST_FLOW_OK)
1847 /* preroll done, we can sync since we are in PLAYING now. */
1848 GST_DEBUG_OBJECT (sink, "possibly waiting for clock to reach %"
1849 GST_TIME_FORMAT, GST_TIME_ARGS (time));
1851 /* compensate for latency and ts_offset. We don't adjust for render delay
1852 * because we don't interact with the device on EOS normally. */
1853 stime = gst_base_sink_adjust_time (sink, time);
1855 /* wait for the clock, this can be interrupted because we got shut down or
1857 status = gst_base_sink_wait_clock (sink, stime, jitter);
1859 GST_DEBUG_OBJECT (sink, "clock returned %d", status);
1861 /* invalid time, no clock or sync disabled, just continue then */
1862 if (status == GST_CLOCK_BADTIME)
1865 /* waiting could have been interrupted and we can be flushing now */
1866 if (G_UNLIKELY (sink->flushing))
1869 /* retry if we got unscheduled, which means we did not reach the timeout
1870 * yet. if some other error occures, we continue. */
1871 } while (status == GST_CLOCK_UNSCHEDULED);
1873 GST_DEBUG_OBJECT (sink, "end of stream");
1880 GST_DEBUG_OBJECT (sink, "we are flushing");
1881 return GST_FLOW_WRONG_STATE;
1885 /* with STREAM_LOCK, PREROLL_LOCK
1887 * Make sure we are in PLAYING and synchronize an object to the clock.
1889 * If we need preroll, we are not in PLAYING. We try to commit the state
1890 * if needed and then block if we still are not PLAYING.
1892 * We start waiting on the clock in PLAYING. If we got interrupted, we
1893 * immediatly try to re-preroll.
1895 * Some objects do not need synchronisation (most events) and so this function
1896 * immediatly returns GST_FLOW_OK.
1898 * for objects that arrive later than max-lateness to be synchronized to the
1899 * clock have the @late boolean set to TRUE.
1901 * This function keeps a running average of the jitter (the diff between the
1902 * clock time and the requested sync time). The jitter is negative for
1903 * objects that arrive in time and positive for late buffers.
1905 * does not take ownership of obj.
1907 static GstFlowReturn
1908 gst_base_sink_do_sync (GstBaseSink * basesink, GstPad * pad,
1909 GstMiniObject * obj, gboolean * late)
1911 GstClockTimeDiff jitter;
1913 GstClockReturn status = GST_CLOCK_OK;
1914 GstClockTime rstart, rstop, sstart, sstop, stime;
1916 GstBaseSinkPrivate *priv;
1919 priv = basesink->priv;
1921 sstart = sstop = rstart = rstop = -1;
1924 priv->current_rstart = -1;
1926 /* get timing information for this object against the render segment */
1927 syncable = gst_base_sink_get_sync_times (basesink, obj,
1928 &sstart, &sstop, &rstart, &rstop, &do_sync, &basesink->segment);
1930 /* a syncable object needs to participate in preroll and
1931 * clocking. All buffers and EOS are syncable. */
1932 if (G_UNLIKELY (!syncable))
1935 /* store timing info for current object */
1936 priv->current_rstart = rstart;
1937 priv->current_rstop = (rstop != -1 ? rstop : rstart);
1938 /* save sync time for eos when the previous object needed sync */
1939 priv->eos_rtime = (do_sync ? priv->current_rstop : -1);
1942 /* first do preroll, this makes sure we commit our state
1943 * to PAUSED and can continue to PLAYING. We cannot perform
1944 * any clock sync in PAUSED because there is no clock.
1946 ret = gst_base_sink_do_preroll (basesink, obj);
1947 if (G_UNLIKELY (ret != GST_FLOW_OK))
1948 goto preroll_failed;
1950 /* After rendering we store the position of the last buffer so that we can use
1951 * it to report the position. We need to take the lock here. */
1952 GST_OBJECT_LOCK (basesink);
1953 priv->current_sstart = sstart;
1954 priv->current_sstop = (sstop != -1 ? sstop : sstart);
1955 GST_OBJECT_UNLOCK (basesink);
1960 /* adjust for latency */
1961 stime = gst_base_sink_adjust_time (basesink, rstart);
1963 /* adjust for render-delay, avoid underflows */
1965 if (stime > priv->render_delay)
1966 stime -= priv->render_delay;
1971 /* preroll done, we can sync since we are in PLAYING now. */
1972 GST_DEBUG_OBJECT (basesink, "possibly waiting for clock to reach %"
1973 GST_TIME_FORMAT ", adjusted %" GST_TIME_FORMAT,
1974 GST_TIME_ARGS (rstart), GST_TIME_ARGS (stime));
1976 /* This function will return immediatly if start == -1, no clock
1977 * or sync is disabled with GST_CLOCK_BADTIME. */
1978 status = gst_base_sink_wait_clock (basesink, stime, &jitter);
1980 GST_DEBUG_OBJECT (basesink, "clock returned %d", status);
1982 /* invalid time, no clock or sync disabled, just render */
1983 if (status == GST_CLOCK_BADTIME)
1986 /* waiting could have been interrupted and we can be flushing now */
1987 if (G_UNLIKELY (basesink->flushing))
1990 /* check for unlocked by a state change, we are not flushing so
1991 * we can try to preroll on the current buffer. */
1992 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
1993 GST_DEBUG_OBJECT (basesink, "unscheduled, waiting some more");
1994 priv->call_preroll = TRUE;
1998 /* successful syncing done, record observation */
1999 priv->current_jitter = jitter;
2001 /* check if the object should be dropped */
2002 *late = gst_base_sink_is_too_late (basesink, obj, rstart, rstop,
2011 GST_DEBUG_OBJECT (basesink, "non syncable object %p", obj);
2016 GST_DEBUG_OBJECT (basesink, "we are flushing");
2017 return GST_FLOW_WRONG_STATE;
2021 GST_DEBUG_OBJECT (basesink, "preroll failed");
2027 gst_base_sink_send_qos (GstBaseSink * basesink,
2028 gdouble proportion, GstClockTime time, GstClockTimeDiff diff)
2033 /* generate Quality-of-Service event */
2034 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2035 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2036 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (time));
2038 event = gst_event_new_qos (proportion, diff, time);
2041 res = gst_pad_push_event (basesink->sinkpad, event);
2047 gst_base_sink_perform_qos (GstBaseSink * sink, gboolean dropped)
2049 GstBaseSinkPrivate *priv;
2050 GstClockTime start, stop;
2051 GstClockTimeDiff jitter;
2052 GstClockTime pt, entered, left;
2053 GstClockTime duration;
2058 start = priv->current_rstart;
2060 /* if Quality-of-Service disabled, do nothing */
2061 if (!g_atomic_int_get (&priv->qos_enabled) || start == -1)
2064 stop = priv->current_rstop;
2065 jitter = priv->current_jitter;
2068 /* this is the time the buffer entered the sink */
2069 if (start < -jitter)
2072 entered = start + jitter;
2075 /* this is the time the buffer entered the sink */
2076 entered = start + jitter;
2077 /* this is the time the buffer left the sink */
2078 left = start + jitter;
2081 /* calculate duration of the buffer */
2083 duration = stop - start;
2087 /* if we have the time when the last buffer left us, calculate
2088 * processing time */
2089 if (priv->last_left != -1) {
2090 if (entered > priv->last_left) {
2091 pt = entered - priv->last_left;
2099 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "start: %" GST_TIME_FORMAT
2100 ", entered %" GST_TIME_FORMAT ", left %" GST_TIME_FORMAT ", pt: %"
2101 GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT ",jitter %"
2102 G_GINT64_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (entered),
2103 GST_TIME_ARGS (left), GST_TIME_ARGS (pt), GST_TIME_ARGS (duration),
2106 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "avg_duration: %" GST_TIME_FORMAT
2107 ", avg_pt: %" GST_TIME_FORMAT ", avg_rate: %g",
2108 GST_TIME_ARGS (priv->avg_duration), GST_TIME_ARGS (priv->avg_pt),
2111 /* collect running averages. for first observations, we copy the
2113 if (priv->avg_duration == -1)
2114 priv->avg_duration = duration;
2116 priv->avg_duration = UPDATE_RUNNING_AVG (priv->avg_duration, duration);
2118 if (priv->avg_pt == -1)
2121 priv->avg_pt = UPDATE_RUNNING_AVG (priv->avg_pt, pt);
2123 if (priv->avg_duration != 0)
2125 gst_guint64_to_gdouble (priv->avg_pt) /
2126 gst_guint64_to_gdouble (priv->avg_duration);
2130 if (priv->last_left != -1) {
2131 if (dropped || priv->avg_rate < 0.0) {
2132 priv->avg_rate = rate;
2135 priv->avg_rate = UPDATE_RUNNING_AVG_N (priv->avg_rate, rate);
2137 priv->avg_rate = UPDATE_RUNNING_AVG_P (priv->avg_rate, rate);
2141 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink,
2142 "updated: avg_duration: %" GST_TIME_FORMAT ", avg_pt: %" GST_TIME_FORMAT
2143 ", avg_rate: %g", GST_TIME_ARGS (priv->avg_duration),
2144 GST_TIME_ARGS (priv->avg_pt), priv->avg_rate);
2147 if (priv->avg_rate >= 0.0) {
2148 /* if we have a valid rate, start sending QoS messages */
2149 if (priv->current_jitter < 0) {
2150 /* make sure we never go below 0 when adding the jitter to the
2152 if (priv->current_rstart < -priv->current_jitter)
2153 priv->current_jitter = -priv->current_rstart;
2155 gst_base_sink_send_qos (sink, priv->avg_rate, priv->current_rstart,
2156 priv->current_jitter);
2159 /* record when this buffer will leave us */
2160 priv->last_left = left;
2163 /* reset all qos measuring */
2165 gst_base_sink_reset_qos (GstBaseSink * sink)
2167 GstBaseSinkPrivate *priv;
2171 priv->last_in_time = -1;
2172 priv->last_left = -1;
2173 priv->avg_duration = -1;
2175 priv->avg_rate = -1.0;
2176 priv->avg_render = -1;
2182 /* Checks if the object was scheduled too late.
2184 * start/stop contain the raw timestamp start and stop values
2187 * status and jitter contain the return values from the clock wait.
2189 * returns TRUE if the buffer was too late.
2192 gst_base_sink_is_too_late (GstBaseSink * basesink, GstMiniObject * obj,
2193 GstClockTime start, GstClockTime stop,
2194 GstClockReturn status, GstClockTimeDiff jitter)
2197 gint64 max_lateness;
2198 GstBaseSinkPrivate *priv;
2200 priv = basesink->priv;
2204 /* only for objects that were too late */
2205 if (G_LIKELY (status != GST_CLOCK_EARLY))
2208 max_lateness = basesink->abidata.ABI.max_lateness;
2210 /* check if frame dropping is enabled */
2211 if (max_lateness == -1)
2214 /* only check for buffers */
2215 if (G_UNLIKELY (!GST_IS_BUFFER (obj)))
2218 /* can't do check if we don't have a timestamp */
2219 if (G_UNLIKELY (start == -1))
2222 /* we can add a valid stop time */
2224 max_lateness += stop;
2226 max_lateness += start;
2228 /* if the jitter bigger than duration and lateness we are too late */
2229 if ((late = start + jitter > max_lateness)) {
2230 GST_DEBUG_OBJECT (basesink, "buffer is too late %" GST_TIME_FORMAT
2231 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (start + jitter),
2232 GST_TIME_ARGS (max_lateness));
2233 /* !!emergency!!, if we did not receive anything valid for more than a
2234 * second, render it anyway so the user sees something */
2235 if (priv->last_in_time != -1 && start - priv->last_in_time > GST_SECOND) {
2237 GST_DEBUG_OBJECT (basesink,
2238 "**emergency** last buffer at %" GST_TIME_FORMAT " > GST_SECOND",
2239 GST_TIME_ARGS (priv->last_in_time));
2245 priv->last_in_time = start;
2252 GST_DEBUG_OBJECT (basesink, "object was scheduled in time");
2257 GST_DEBUG_OBJECT (basesink, "frame dropping disabled");
2262 GST_DEBUG_OBJECT (basesink, "object is not a buffer");
2267 GST_DEBUG_OBJECT (basesink, "buffer has no timestamp");
2272 /* called before and after calling the render vmethod. It keeps track of how
2273 * much time was spent in the render method and is used to check if we are
2276 gst_base_sink_do_render_stats (GstBaseSink * basesink, gboolean start)
2278 GstBaseSinkPrivate *priv;
2280 priv = basesink->priv;
2283 priv->start = gst_util_get_timestamp ();
2285 GstClockTime elapsed;
2287 priv->stop = gst_util_get_timestamp ();
2289 elapsed = GST_CLOCK_DIFF (priv->start, priv->stop);
2291 if (priv->avg_render == -1)
2292 priv->avg_render = elapsed;
2294 priv->avg_render = UPDATE_RUNNING_AVG (priv->avg_render, elapsed);
2296 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2297 "avg_render: %" GST_TIME_FORMAT, GST_TIME_ARGS (priv->avg_render));
2301 /* with STREAM_LOCK, PREROLL_LOCK,
2303 * Synchronize the object on the clock and then render it.
2305 * takes ownership of obj.
2307 static GstFlowReturn
2308 gst_base_sink_render_object (GstBaseSink * basesink, GstPad * pad,
2309 GstMiniObject * obj)
2311 GstFlowReturn ret = GST_FLOW_OK;
2312 GstBaseSinkClass *bclass;
2313 gboolean late = FALSE;
2314 GstBaseSinkPrivate *priv;
2316 priv = basesink->priv;
2318 /* synchronize this object, non syncable objects return OK
2320 ret = gst_base_sink_do_sync (basesink, pad, obj, &late);
2321 if (G_UNLIKELY (ret != GST_FLOW_OK))
2324 /* and now render, event or buffer. */
2325 if (G_LIKELY (GST_IS_BUFFER (obj))) {
2328 /* drop late buffers unconditionally, let's hope it's unlikely */
2329 if (G_UNLIKELY (late))
2332 buf = GST_BUFFER_CAST (obj);
2334 gst_base_sink_set_last_buffer (basesink, buf);
2336 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2338 if (G_LIKELY (bclass->render)) {
2341 /* read once, to get same value before and after */
2342 do_qos = g_atomic_int_get (&priv->qos_enabled);
2344 GST_DEBUG_OBJECT (basesink, "rendering buffer %p", obj);
2346 /* record rendering time for QoS and stats */
2348 gst_base_sink_do_render_stats (basesink, TRUE);
2350 ret = bclass->render (basesink, buf);
2355 gst_base_sink_do_render_stats (basesink, FALSE);
2358 GstEvent *event = GST_EVENT_CAST (obj);
2359 gboolean event_res = TRUE;
2362 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2364 type = GST_EVENT_TYPE (event);
2366 GST_DEBUG_OBJECT (basesink, "rendering event %p, type %s", obj,
2367 gst_event_type_get_name (type));
2370 event_res = bclass->event (basesink, event);
2372 /* when we get here we could be flushing again when the event handler calls
2373 * _wait_eos(). We have to ignore this object in that case. */
2374 if (G_UNLIKELY (basesink->flushing))
2377 if (G_LIKELY (event_res)) {
2380 seqnum = basesink->priv->seqnum = gst_event_get_seqnum (event);
2381 GST_DEBUG_OBJECT (basesink, "Got seqnum #%" G_GUINT32_FORMAT, seqnum);
2386 GstMessage *message;
2388 /* the EOS event is completely handled so we mark
2389 * ourselves as being in the EOS state. eos is also
2390 * protected by the object lock so we can read it when
2391 * answering the POSITION query. */
2392 GST_OBJECT_LOCK (basesink);
2393 basesink->eos = TRUE;
2394 GST_OBJECT_UNLOCK (basesink);
2396 /* ok, now we can post the message */
2397 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
2399 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
2400 gst_message_set_seqnum (message, seqnum);
2401 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
2404 case GST_EVENT_NEWSEGMENT:
2405 /* configure the segment */
2406 gst_base_sink_configure_segment (basesink, pad, event,
2407 &basesink->segment);
2416 gst_base_sink_perform_qos (basesink, late);
2418 GST_DEBUG_OBJECT (basesink, "object unref after render %p", obj);
2419 gst_mini_object_unref (obj);
2426 GST_DEBUG_OBJECT (basesink, "do_sync returned %s", gst_flow_get_name (ret));
2432 GST_DEBUG_OBJECT (basesink, "buffer late, dropping");
2437 GST_DEBUG_OBJECT (basesink, "we are flushing, ignore object");
2438 gst_mini_object_unref (obj);
2439 return GST_FLOW_WRONG_STATE;
2443 /* with STREAM_LOCK, PREROLL_LOCK
2445 * Perform preroll on the given object. For buffers this means
2446 * calling the preroll subclass method.
2447 * If that succeeds, the state will be commited.
2449 * function does not take ownership of obj.
2451 static GstFlowReturn
2452 gst_base_sink_preroll_object (GstBaseSink * basesink, GstMiniObject * obj)
2456 GST_DEBUG_OBJECT (basesink, "prerolling object %p", obj);
2458 /* if it's a buffer, we need to call the preroll method */
2459 if (G_LIKELY (GST_IS_BUFFER (obj)) && basesink->priv->call_preroll) {
2460 GstBaseSinkClass *bclass;
2462 GstClockTime timestamp;
2464 buf = GST_BUFFER_CAST (obj);
2465 timestamp = GST_BUFFER_TIMESTAMP (buf);
2467 GST_DEBUG_OBJECT (basesink, "preroll buffer %" GST_TIME_FORMAT,
2468 GST_TIME_ARGS (timestamp));
2470 gst_base_sink_set_last_buffer (basesink, buf);
2472 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2473 if (bclass->preroll)
2474 if ((ret = bclass->preroll (basesink, buf)) != GST_FLOW_OK)
2475 goto preroll_failed;
2477 basesink->priv->call_preroll = FALSE;
2481 if (G_LIKELY (basesink->playing_async)) {
2482 if (G_UNLIKELY (!gst_base_sink_commit_state (basesink)))
2491 GST_DEBUG_OBJECT (basesink, "preroll failed, abort state");
2492 gst_element_abort_state (GST_ELEMENT_CAST (basesink));
2497 GST_DEBUG_OBJECT (basesink, "stopping while commiting state");
2498 return GST_FLOW_WRONG_STATE;
2502 /* with STREAM_LOCK, PREROLL_LOCK
2504 * Queue an object for rendering.
2505 * The first prerollable object queued will complete the preroll. If the
2506 * preroll queue if filled, we render all the objects in the queue.
2508 * This function takes ownership of the object.
2510 static GstFlowReturn
2511 gst_base_sink_queue_object_unlocked (GstBaseSink * basesink, GstPad * pad,
2512 GstMiniObject * obj, gboolean prerollable)
2514 GstFlowReturn ret = GST_FLOW_OK;
2518 if (G_UNLIKELY (basesink->need_preroll)) {
2519 if (G_LIKELY (prerollable))
2520 basesink->preroll_queued++;
2522 length = basesink->preroll_queued;
2524 GST_DEBUG_OBJECT (basesink, "now %d prerolled items", length);
2526 /* first prerollable item needs to finish the preroll */
2528 ret = gst_base_sink_preroll_object (basesink, obj);
2529 if (G_UNLIKELY (ret != GST_FLOW_OK))
2530 goto preroll_failed;
2532 /* need to recheck if we need preroll, commmit state during preroll
2533 * could have made us not need more preroll. */
2534 if (G_UNLIKELY (basesink->need_preroll)) {
2535 /* see if we can render now, if we can't add the object to the preroll
2537 if (G_UNLIKELY (length <= basesink->preroll_queue_max_len))
2542 /* we can start rendering (or blocking) the queued object
2544 q = basesink->preroll_queue;
2545 while (G_UNLIKELY (!g_queue_is_empty (q))) {
2548 o = g_queue_pop_head (q);
2549 GST_DEBUG_OBJECT (basesink, "rendering queued object %p", o);
2551 /* do something with the return value */
2552 ret = gst_base_sink_render_object (basesink, pad, o);
2553 if (ret != GST_FLOW_OK)
2554 goto dequeue_failed;
2557 /* now render the object */
2558 ret = gst_base_sink_render_object (basesink, pad, obj);
2559 basesink->preroll_queued = 0;
2566 GST_DEBUG_OBJECT (basesink, "preroll failed, reason %s",
2567 gst_flow_get_name (ret));
2568 gst_mini_object_unref (obj);
2573 /* add object to the queue and return */
2574 GST_DEBUG_OBJECT (basesink, "need more preroll data %d <= %d",
2575 length, basesink->preroll_queue_max_len);
2576 g_queue_push_tail (basesink->preroll_queue, obj);
2581 GST_DEBUG_OBJECT (basesink, "rendering queued objects failed, reason %s",
2582 gst_flow_get_name (ret));
2583 gst_mini_object_unref (obj);
2590 * This function grabs the PREROLL_LOCK and adds the object to
2593 * This function takes ownership of obj.
2595 static GstFlowReturn
2596 gst_base_sink_queue_object (GstBaseSink * basesink, GstPad * pad,
2597 GstMiniObject * obj, gboolean prerollable)
2601 GST_PAD_PREROLL_LOCK (pad);
2602 if (G_UNLIKELY (basesink->flushing))
2605 if (G_UNLIKELY (basesink->priv->received_eos))
2608 ret = gst_base_sink_queue_object_unlocked (basesink, pad, obj, prerollable);
2609 GST_PAD_PREROLL_UNLOCK (pad);
2616 GST_DEBUG_OBJECT (basesink, "sink is flushing");
2617 GST_PAD_PREROLL_UNLOCK (pad);
2618 gst_mini_object_unref (obj);
2619 return GST_FLOW_WRONG_STATE;
2623 GST_DEBUG_OBJECT (basesink,
2624 "we are EOS, dropping object, return UNEXPECTED");
2625 GST_PAD_PREROLL_UNLOCK (pad);
2626 gst_mini_object_unref (obj);
2627 return GST_FLOW_UNEXPECTED;
2632 gst_base_sink_flush_start (GstBaseSink * basesink, GstPad * pad)
2634 /* make sure we are not blocked on the clock also clear any pending
2636 gst_base_sink_set_flushing (basesink, pad, TRUE);
2638 /* we grab the stream lock but that is not needed since setting the
2639 * sink to flushing would make sure no state commit is being done
2641 GST_PAD_STREAM_LOCK (pad);
2642 gst_base_sink_reset_qos (basesink);
2643 if (basesink->priv->async_enabled) {
2644 /* and we need to commit our state again on the next
2645 * prerolled buffer */
2646 basesink->playing_async = TRUE;
2647 gst_element_lost_state (GST_ELEMENT_CAST (basesink));
2649 basesink->priv->have_latency = TRUE;
2650 basesink->need_preroll = FALSE;
2652 gst_base_sink_set_last_buffer (basesink, NULL);
2653 GST_PAD_STREAM_UNLOCK (pad);
2657 gst_base_sink_flush_stop (GstBaseSink * basesink, GstPad * pad)
2659 /* unset flushing so we can accept new data, this also flushes out any EOS
2661 gst_base_sink_set_flushing (basesink, pad, FALSE);
2663 /* for position reporting */
2664 GST_OBJECT_LOCK (basesink);
2665 basesink->priv->current_sstart = -1;
2666 basesink->priv->current_sstop = -1;
2667 basesink->priv->eos_rtime = -1;
2668 basesink->priv->call_preroll = TRUE;
2669 if (basesink->pad_mode == GST_ACTIVATE_PUSH) {
2670 /* we need new segment info after the flush. */
2671 basesink->have_newsegment = FALSE;
2672 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
2673 gst_segment_init (basesink->abidata.ABI.clip_segment, GST_FORMAT_UNDEFINED);
2675 GST_OBJECT_UNLOCK (basesink);
2679 gst_base_sink_event (GstPad * pad, GstEvent * event)
2681 GstBaseSink *basesink;
2682 gboolean result = TRUE;
2683 GstBaseSinkClass *bclass;
2685 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
2687 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2689 GST_DEBUG_OBJECT (basesink, "event %p (%s)", event,
2690 GST_EVENT_TYPE_NAME (event));
2692 switch (GST_EVENT_TYPE (event)) {
2697 GST_PAD_PREROLL_LOCK (pad);
2698 if (G_UNLIKELY (basesink->flushing))
2701 if (G_UNLIKELY (basesink->priv->received_eos)) {
2702 /* we can't accept anything when we are EOS */
2704 gst_event_unref (event);
2706 /* we set the received EOS flag here so that we can use it when testing if
2707 * we are prerolled and to refuse more buffers. */
2708 basesink->priv->received_eos = TRUE;
2710 /* EOS is a prerollable object, we call the unlocked version because it
2711 * does not check the received_eos flag. */
2712 ret = gst_base_sink_queue_object_unlocked (basesink, pad,
2713 GST_MINI_OBJECT_CAST (event), TRUE);
2714 if (G_UNLIKELY (ret != GST_FLOW_OK))
2717 GST_PAD_PREROLL_UNLOCK (pad);
2720 case GST_EVENT_NEWSEGMENT:
2724 GST_DEBUG_OBJECT (basesink, "newsegment %p", event);
2726 GST_PAD_PREROLL_LOCK (pad);
2727 if (G_UNLIKELY (basesink->flushing))
2730 if (G_UNLIKELY (basesink->priv->received_eos)) {
2731 /* we can't accept anything when we are EOS */
2733 gst_event_unref (event);
2735 /* the new segment is a non prerollable item and does not block anything,
2736 * we need to configure the current clipping segment and insert the event
2737 * in the queue to serialize it with the buffers for rendering. */
2738 gst_base_sink_configure_segment (basesink, pad, event,
2739 basesink->abidata.ABI.clip_segment);
2742 gst_base_sink_queue_object_unlocked (basesink, pad,
2743 GST_MINI_OBJECT_CAST (event), FALSE);
2744 if (G_UNLIKELY (ret != GST_FLOW_OK))
2747 GST_OBJECT_LOCK (basesink);
2748 basesink->have_newsegment = TRUE;
2749 GST_OBJECT_UNLOCK (basesink);
2752 GST_PAD_PREROLL_UNLOCK (pad);
2755 case GST_EVENT_FLUSH_START:
2757 bclass->event (basesink, event);
2759 GST_DEBUG_OBJECT (basesink, "flush-start %p", event);
2761 gst_base_sink_flush_start (basesink, pad);
2763 gst_event_unref (event);
2765 case GST_EVENT_FLUSH_STOP:
2767 bclass->event (basesink, event);
2769 GST_DEBUG_OBJECT (basesink, "flush-stop %p", event);
2771 gst_base_sink_flush_stop (basesink, pad);
2773 gst_event_unref (event);
2776 /* other events are sent to queue or subclass depending on if they
2777 * are serialized. */
2778 if (GST_EVENT_IS_SERIALIZED (event)) {
2779 gst_base_sink_queue_object (basesink, pad,
2780 GST_MINI_OBJECT_CAST (event), FALSE);
2783 bclass->event (basesink, event);
2784 gst_event_unref (event);
2789 gst_object_unref (basesink);
2796 GST_DEBUG_OBJECT (basesink, "we are flushing");
2797 GST_PAD_PREROLL_UNLOCK (pad);
2799 gst_event_unref (event);
2804 /* default implementation to calculate the start and end
2805 * timestamps on a buffer, subclasses can override
2808 gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
2809 GstClockTime * start, GstClockTime * end)
2811 GstClockTime timestamp, duration;
2813 timestamp = GST_BUFFER_TIMESTAMP (buffer);
2814 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
2816 /* get duration to calculate end time */
2817 duration = GST_BUFFER_DURATION (buffer);
2818 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2819 *end = timestamp + duration;
2825 /* must be called with PREROLL_LOCK */
2827 gst_base_sink_needs_preroll (GstBaseSink * basesink)
2829 gboolean is_prerolled, res;
2831 /* we have 2 cases where the PREROLL_LOCK is released:
2832 * 1) we are blocking in the PREROLL_LOCK and thus are prerolled.
2833 * 2) we are syncing on the clock
2835 is_prerolled = basesink->have_preroll || basesink->priv->received_eos;
2836 res = !is_prerolled;
2838 GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d => needs preroll: %d",
2839 basesink->have_preroll, basesink->priv->received_eos, res);
2844 /* with STREAM_LOCK, PREROLL_LOCK
2846 * Takes a buffer and compare the timestamps with the last segment.
2847 * If the buffer falls outside of the segment boundaries, drop it.
2848 * Else queue the buffer for preroll and rendering.
2850 * This function takes ownership of the buffer.
2852 static GstFlowReturn
2853 gst_base_sink_chain_unlocked (GstBaseSink * basesink, GstPad * pad,
2856 GstBaseSinkClass *bclass;
2857 GstFlowReturn result;
2858 GstClockTime start = GST_CLOCK_TIME_NONE, end = GST_CLOCK_TIME_NONE;
2859 GstSegment *clip_segment;
2861 if (G_UNLIKELY (basesink->flushing))
2864 if (G_UNLIKELY (basesink->priv->received_eos))
2867 /* for code clarity */
2868 clip_segment = basesink->abidata.ABI.clip_segment;
2870 if (G_UNLIKELY (!basesink->have_newsegment)) {
2873 sync = gst_base_sink_get_sync (basesink);
2875 GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
2876 (_("Internal data flow problem.")),
2877 ("Received buffer without a new-segment. Assuming timestamps start from 0."));
2880 /* this means this sink will assume timestamps start from 0 */
2881 GST_OBJECT_LOCK (basesink);
2882 clip_segment->start = 0;
2883 clip_segment->stop = -1;
2884 basesink->segment.start = 0;
2885 basesink->segment.stop = -1;
2886 basesink->have_newsegment = TRUE;
2887 GST_OBJECT_UNLOCK (basesink);
2890 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2892 /* check if the buffer needs to be dropped, we first ask the subclass for the
2894 if (bclass->get_times)
2895 bclass->get_times (basesink, buf, &start, &end);
2898 /* if the subclass does not want sync, we use our own values so that we at
2899 * least clip the buffer to the segment */
2900 gst_base_sink_get_times (basesink, buf, &start, &end);
2903 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
2904 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
2906 /* a dropped buffer does not participate in anything */
2907 if (GST_CLOCK_TIME_IS_VALID (start) &&
2908 (clip_segment->format == GST_FORMAT_TIME)) {
2909 if (G_UNLIKELY (!gst_segment_clip (clip_segment,
2910 GST_FORMAT_TIME, (gint64) start, (gint64) end, NULL, NULL)))
2911 goto out_of_segment;
2914 /* now we can process the buffer in the queue, this function takes ownership
2916 result = gst_base_sink_queue_object_unlocked (basesink, pad,
2917 GST_MINI_OBJECT_CAST (buf), TRUE);
2924 GST_DEBUG_OBJECT (basesink, "sink is flushing");
2925 gst_buffer_unref (buf);
2926 return GST_FLOW_WRONG_STATE;
2930 GST_DEBUG_OBJECT (basesink,
2931 "we are EOS, dropping object, return UNEXPECTED");
2932 gst_buffer_unref (buf);
2933 return GST_FLOW_UNEXPECTED;
2937 GST_DEBUG_OBJECT (basesink, "dropping buffer, out of clipping segment");
2938 gst_buffer_unref (buf);
2945 static GstFlowReturn
2946 gst_base_sink_chain (GstPad * pad, GstBuffer * buf)
2948 GstBaseSink *basesink;
2949 GstFlowReturn result;
2951 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
2953 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH))
2956 GST_PAD_PREROLL_LOCK (pad);
2957 result = gst_base_sink_chain_unlocked (basesink, pad, buf);
2958 GST_PAD_PREROLL_UNLOCK (pad);
2966 GST_OBJECT_LOCK (pad);
2967 GST_WARNING_OBJECT (basesink,
2968 "Push on pad %s:%s, but it was not activated in push mode",
2969 GST_DEBUG_PAD_NAME (pad));
2970 GST_OBJECT_UNLOCK (pad);
2971 gst_buffer_unref (buf);
2972 /* we don't post an error message this will signal to the peer
2973 * pushing that EOS is reached. */
2974 result = GST_FLOW_UNEXPECTED;
2980 gst_base_sink_default_do_seek (GstBaseSink * sink, GstSegment * segment)
2982 gboolean res = TRUE;
2984 /* update our offset if the start/stop position was updated */
2985 if (segment->format == GST_FORMAT_BYTES) {
2986 segment->time = segment->start;
2987 } else if (segment->start == 0) {
2988 /* seek to start, we can implement a default for this. */
2992 GST_INFO_OBJECT (sink, "Can't do a default seek");
2998 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
3001 gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
3002 GstEvent * event, GstSegment * segment)
3004 /* By default, we try one of 2 things:
3005 * - For absolute seek positions, convert the requested position to our
3006 * configured processing format and place it in the output segment \
3007 * - For relative seek positions, convert our current (input) values to the
3008 * seek format, adjust by the relative seek offset and then convert back to
3009 * the processing format
3011 GstSeekType cur_type, stop_type;
3014 GstFormat seek_format, dest_format;
3017 gboolean res = TRUE;
3019 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3020 &cur_type, &cur, &stop_type, &stop);
3021 dest_format = segment->format;
3023 if (seek_format == dest_format) {
3024 gst_segment_set_seek (segment, rate, seek_format, flags,
3025 cur_type, cur, stop_type, stop, &update);
3029 if (cur_type != GST_SEEK_TYPE_NONE) {
3030 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3032 gst_pad_query_convert (sink->sinkpad, seek_format, cur, &dest_format,
3034 cur_type = GST_SEEK_TYPE_SET;
3037 if (res && stop_type != GST_SEEK_TYPE_NONE) {
3038 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3040 gst_pad_query_convert (sink->sinkpad, seek_format, stop, &dest_format,
3042 stop_type = GST_SEEK_TYPE_SET;
3045 /* And finally, configure our output segment in the desired format */
3046 gst_segment_set_seek (segment, rate, dest_format, flags, cur_type, cur,
3047 stop_type, stop, &update);
3056 GST_DEBUG_OBJECT (sink, "undefined format given, seek aborted.");
3061 /* perform a seek, only executed in pull mode */
3063 gst_base_sink_perform_seek (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3067 GstFormat seek_format, dest_format;
3069 GstSeekType cur_type, stop_type;
3070 gboolean seekseg_configured = FALSE;
3072 gboolean update, res = TRUE;
3073 GstSegment seeksegment;
3075 dest_format = sink->segment.format;
3078 GST_DEBUG_OBJECT (sink, "performing seek with event %p", event);
3079 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3080 &cur_type, &cur, &stop_type, &stop);
3082 flush = flags & GST_SEEK_FLAG_FLUSH;
3084 GST_DEBUG_OBJECT (sink, "performing seek without event");
3089 GST_DEBUG_OBJECT (sink, "flushing upstream");
3090 gst_pad_push_event (pad, gst_event_new_flush_start ());
3091 gst_base_sink_flush_start (sink, pad);
3093 GST_DEBUG_OBJECT (sink, "pausing pulling thread");
3096 GST_PAD_STREAM_LOCK (pad);
3098 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
3099 * copy the current segment info into the temp segment that we can actually
3100 * attempt the seek with. We only update the real segment if the seek suceeds. */
3101 if (!seekseg_configured) {
3102 memcpy (&seeksegment, &sink->segment, sizeof (GstSegment));
3104 /* now configure the final seek segment */
3106 if (sink->segment.format != seek_format) {
3107 /* OK, here's where we give the subclass a chance to convert the relative
3108 * seek into an absolute one in the processing format. We set up any
3109 * absolute seek above, before taking the stream lock. */
3110 if (!gst_base_sink_default_prepare_seek_segment (sink, event,
3112 GST_DEBUG_OBJECT (sink,
3113 "Preparing the seek failed after flushing. " "Aborting seek");
3117 /* The seek format matches our processing format, no need to ask the
3118 * the subclass to configure the segment. */
3119 gst_segment_set_seek (&seeksegment, rate, seek_format, flags,
3120 cur_type, cur, stop_type, stop, &update);
3123 /* Else, no seek event passed, so we're just (re)starting the
3128 GST_DEBUG_OBJECT (sink, "segment configured from %" G_GINT64_FORMAT
3129 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
3130 seeksegment.start, seeksegment.stop, seeksegment.last_stop);
3132 /* do the seek, segment.last_stop contains the new position. */
3133 res = gst_base_sink_default_do_seek (sink, &seeksegment);
3138 GST_DEBUG_OBJECT (sink, "stop flushing upstream");
3139 gst_pad_push_event (pad, gst_event_new_flush_stop ());
3140 gst_base_sink_flush_stop (sink, pad);
3141 } else if (res && sink->abidata.ABI.running) {
3142 /* we are running the current segment and doing a non-flushing seek,
3143 * close the segment first based on the last_stop. */
3144 GST_DEBUG_OBJECT (sink, "closing running segment %" G_GINT64_FORMAT
3145 " to %" G_GINT64_FORMAT, sink->segment.start, sink->segment.last_stop);
3148 /* The subclass must have converted the segment to the processing format
3150 if (res && seeksegment.format != dest_format) {
3151 GST_DEBUG_OBJECT (sink, "Subclass failed to prepare a seek segment "
3152 "in the correct format. Aborting seek.");
3156 /* if successfull seek, we update our real segment and push
3157 * out the new segment. */
3159 memcpy (&sink->segment, &seeksegment, sizeof (GstSegment));
3161 if (sink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3162 gst_element_post_message (GST_ELEMENT (sink),
3163 gst_message_new_segment_start (GST_OBJECT (sink),
3164 sink->segment.format, sink->segment.last_stop));
3168 sink->priv->discont = TRUE;
3169 sink->abidata.ABI.running = TRUE;
3171 GST_PAD_STREAM_UNLOCK (pad);
3179 gst_base_sink_loop (GstPad * pad)
3181 GstBaseSink *basesink;
3182 GstBuffer *buf = NULL;
3183 GstFlowReturn result;
3187 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
3189 g_assert (basesink->pad_mode == GST_ACTIVATE_PULL);
3191 if ((blocksize = basesink->priv->blocksize) == 0)
3194 offset = basesink->segment.last_stop;
3196 GST_DEBUG_OBJECT (basesink, "pulling %" G_GUINT64_FORMAT ", %u",
3199 result = gst_pad_pull_range (pad, offset, blocksize, &buf);
3200 if (G_UNLIKELY (result != GST_FLOW_OK))
3203 if (G_UNLIKELY (buf == NULL))
3206 offset += GST_BUFFER_SIZE (buf);
3208 gst_segment_set_last_stop (&basesink->segment, GST_FORMAT_BYTES, offset);
3210 GST_PAD_PREROLL_LOCK (pad);
3211 result = gst_base_sink_chain_unlocked (basesink, pad, buf);
3212 GST_PAD_PREROLL_UNLOCK (pad);
3213 if (G_UNLIKELY (result != GST_FLOW_OK))
3221 GST_LOG_OBJECT (basesink, "pausing task, reason %s",
3222 gst_flow_get_name (result));
3223 gst_pad_pause_task (pad);
3224 /* fatal errors and NOT_LINKED cause EOS */
3225 if (GST_FLOW_IS_FATAL (result) || result == GST_FLOW_NOT_LINKED) {
3226 if (result == GST_FLOW_UNEXPECTED) {
3227 /* perform EOS logic */
3228 if (basesink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3229 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3230 gst_message_new_segment_done (GST_OBJECT_CAST (basesink),
3231 basesink->segment.format, basesink->segment.last_stop));
3233 gst_base_sink_event (pad, gst_event_new_eos ());
3236 /* for fatal errors we post an error message, post the error
3237 * first so the app knows about the error first. */
3238 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
3239 (_("Internal data stream error.")),
3240 ("stream stopped, reason %s", gst_flow_get_name (result)));
3241 gst_base_sink_event (pad, gst_event_new_eos ());
3248 GST_LOG_OBJECT (basesink, "no buffer, pausing");
3249 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
3250 (_("Internal data flow error.")), ("element returned NULL buffer"));
3251 result = GST_FLOW_ERROR;
3257 gst_base_sink_set_flushing (GstBaseSink * basesink, GstPad * pad,
3260 GstBaseSinkClass *bclass;
3262 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3265 /* unlock any subclasses, we need to do this before grabbing the
3266 * PREROLL_LOCK since we hold this lock before going into ::render. */
3268 bclass->unlock (basesink);
3271 GST_PAD_PREROLL_LOCK (pad);
3272 basesink->flushing = flushing;
3274 /* step 1, now that we have the PREROLL lock, clear our unlock request */
3275 if (bclass->unlock_stop)
3276 bclass->unlock_stop (basesink);
3278 /* set need_preroll before we unblock the clock. If the clock is unblocked
3279 * before timing out, we can reuse the buffer for preroll. */
3280 basesink->need_preroll = TRUE;
3282 /* step 2, unblock clock sync (if any) or any other blocking thing */
3283 if (basesink->clock_id) {
3284 gst_clock_id_unschedule (basesink->clock_id);
3287 /* flush out the data thread if it's locked in finish_preroll, this will
3288 * also flush out the EOS state */
3289 GST_DEBUG_OBJECT (basesink,
3290 "flushing out data thread, need preroll to TRUE");
3291 gst_base_sink_preroll_queue_flush (basesink, pad);
3293 GST_PAD_PREROLL_UNLOCK (pad);
3299 gst_base_sink_default_activate_pull (GstBaseSink * basesink, gboolean active)
3305 result = gst_pad_start_task (basesink->sinkpad,
3306 (GstTaskFunction) gst_base_sink_loop, basesink->sinkpad);
3308 /* step 2, make sure streaming finishes */
3309 result = gst_pad_stop_task (basesink->sinkpad);
3316 gst_base_sink_pad_activate (GstPad * pad)
3318 gboolean result = FALSE;
3319 GstBaseSink *basesink;
3321 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3323 GST_DEBUG_OBJECT (basesink, "Trying pull mode first");
3325 gst_base_sink_set_flushing (basesink, pad, FALSE);
3327 /* we need to have the pull mode enabled */
3328 if (!basesink->can_activate_pull) {
3329 GST_DEBUG_OBJECT (basesink, "pull mode disabled");
3333 /* check if downstreams supports pull mode at all */
3334 if (!gst_pad_check_pull_range (pad)) {
3335 GST_DEBUG_OBJECT (basesink, "pull mode not supported");
3339 /* set the pad mode before starting the task so that it's in the
3340 * correct state for the new thread. also the sink set_caps and get_caps
3341 * function checks this */
3342 basesink->pad_mode = GST_ACTIVATE_PULL;
3344 /* we first try to negotiate a format so that when we try to activate
3345 * downstream, it knows about our format */
3346 if (!gst_base_sink_negotiate_pull (basesink)) {
3347 GST_DEBUG_OBJECT (basesink, "failed to negotiate in pull mode");
3351 /* ok activate now */
3352 if (!gst_pad_activate_pull (pad, TRUE)) {
3353 /* clear any pending caps */
3354 GST_OBJECT_LOCK (basesink);
3355 gst_caps_replace (&basesink->priv->pull_caps, NULL);
3356 GST_OBJECT_UNLOCK (basesink);
3357 GST_DEBUG_OBJECT (basesink, "failed to activate in pull mode");
3361 GST_DEBUG_OBJECT (basesink, "Success activating pull mode");
3365 /* push mode fallback */
3367 GST_DEBUG_OBJECT (basesink, "Falling back to push mode");
3368 if ((result = gst_pad_activate_push (pad, TRUE))) {
3369 GST_DEBUG_OBJECT (basesink, "Success activating push mode");
3374 GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
3375 gst_base_sink_set_flushing (basesink, pad, TRUE);
3378 gst_object_unref (basesink);
3384 gst_base_sink_pad_activate_push (GstPad * pad, gboolean active)
3387 GstBaseSink *basesink;
3389 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3392 if (!basesink->can_activate_push) {
3394 basesink->pad_mode = GST_ACTIVATE_NONE;
3397 basesink->pad_mode = GST_ACTIVATE_PUSH;
3400 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH)) {
3401 g_warning ("Internal GStreamer activation error!!!");
3404 gst_base_sink_set_flushing (basesink, pad, TRUE);
3406 basesink->pad_mode = GST_ACTIVATE_NONE;
3410 gst_object_unref (basesink);
3416 gst_base_sink_negotiate_pull (GstBaseSink * basesink)
3423 /* this returns the intersection between our caps and the peer caps. If there
3424 * is no peer, it returns NULL and we can't operate in pull mode so we can
3425 * fail the negotiation. */
3426 caps = gst_pad_get_allowed_caps (GST_BASE_SINK_PAD (basesink));
3427 if (caps == NULL || gst_caps_is_empty (caps))
3428 goto no_caps_possible;
3430 GST_DEBUG_OBJECT (basesink, "allowed caps: %" GST_PTR_FORMAT, caps);
3432 caps = gst_caps_make_writable (caps);
3433 /* get the first (prefered) format */
3434 gst_caps_truncate (caps);
3436 gst_pad_fixate_caps (GST_BASE_SINK_PAD (basesink), caps);
3438 GST_DEBUG_OBJECT (basesink, "fixated to: %" GST_PTR_FORMAT, caps);
3440 if (gst_caps_is_any (caps)) {
3441 GST_DEBUG_OBJECT (basesink, "caps were ANY after fixating, "
3443 /* neither side has template caps in this case, so they are prepared for
3444 pull() without setcaps() */
3446 } else if (gst_caps_is_fixed (caps)) {
3447 if (!gst_pad_set_caps (GST_BASE_SINK_PAD (basesink), caps))
3448 goto could_not_set_caps;
3450 GST_OBJECT_LOCK (basesink);
3451 gst_caps_replace (&basesink->priv->pull_caps, caps);
3452 GST_OBJECT_UNLOCK (basesink);
3457 gst_caps_unref (caps);
3463 GST_INFO_OBJECT (basesink, "Pipeline could not agree on caps");
3464 GST_DEBUG_OBJECT (basesink, "get_allowed_caps() returned EMPTY");
3466 gst_caps_unref (caps);
3471 GST_INFO_OBJECT (basesink, "Could not set caps: %" GST_PTR_FORMAT, caps);
3472 gst_caps_unref (caps);
3477 /* this won't get called until we implement an activate function */
3479 gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active)
3481 gboolean result = FALSE;
3482 GstBaseSink *basesink;
3483 GstBaseSinkClass *bclass;
3485 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3486 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3492 /* we mark we have a newsegment here because pull based
3493 * mode works just fine without having a newsegment before the
3495 format = GST_FORMAT_BYTES;
3497 gst_segment_init (&basesink->segment, format);
3498 gst_segment_init (basesink->abidata.ABI.clip_segment, format);
3499 GST_OBJECT_LOCK (basesink);
3500 basesink->have_newsegment = TRUE;
3501 GST_OBJECT_UNLOCK (basesink);
3503 /* get the peer duration in bytes */
3504 result = gst_pad_query_peer_duration (pad, &format, &duration);
3506 GST_DEBUG_OBJECT (basesink,
3507 "setting duration in bytes to %" G_GINT64_FORMAT, duration);
3508 gst_segment_set_duration (basesink->abidata.ABI.clip_segment, format,
3510 gst_segment_set_duration (&basesink->segment, format, duration);
3512 GST_DEBUG_OBJECT (basesink, "unknown duration");
3515 if (bclass->activate_pull)
3516 result = bclass->activate_pull (basesink, TRUE);
3521 goto activate_failed;
3524 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PULL)) {
3525 g_warning ("Internal GStreamer activation error!!!");
3528 result = gst_base_sink_set_flushing (basesink, pad, TRUE);
3529 if (bclass->activate_pull)
3530 result &= bclass->activate_pull (basesink, FALSE);
3531 basesink->pad_mode = GST_ACTIVATE_NONE;
3532 /* clear any pending caps */
3533 GST_OBJECT_LOCK (basesink);
3534 gst_caps_replace (&basesink->priv->pull_caps, NULL);
3535 GST_OBJECT_UNLOCK (basesink);
3538 gst_object_unref (basesink);
3545 /* reset, as starting the thread failed */
3546 basesink->pad_mode = GST_ACTIVATE_NONE;
3548 GST_ERROR_OBJECT (basesink, "subclass failed to activate in pull mode");
3553 /* send an event to our sinkpad peer. */
3555 gst_base_sink_send_event (GstElement * element, GstEvent * event)
3558 GstBaseSink *basesink = GST_BASE_SINK (element);
3559 gboolean forward, result = TRUE;
3560 GstActivateMode mode;
3562 GST_OBJECT_LOCK (element);
3563 /* get the pad and the scheduling mode */
3564 pad = gst_object_ref (basesink->sinkpad);
3565 mode = basesink->pad_mode;
3566 GST_OBJECT_UNLOCK (element);
3568 /* only push UPSTREAM events upstream */
3569 forward = GST_EVENT_IS_UPSTREAM (event);
3571 switch (GST_EVENT_TYPE (event)) {
3572 case GST_EVENT_LATENCY:
3574 GstClockTime latency;
3576 gst_event_parse_latency (event, &latency);
3578 /* store the latency. We use this to adjust the running_time before syncing
3579 * it to the clock. */
3580 GST_OBJECT_LOCK (element);
3581 basesink->priv->latency = latency;
3582 if (!basesink->priv->have_latency)
3584 GST_OBJECT_UNLOCK (element);
3585 GST_DEBUG_OBJECT (basesink, "latency set to %" GST_TIME_FORMAT,
3586 GST_TIME_ARGS (latency));
3588 /* We forward this event so that all elements know about the global pipeline
3589 * latency. This is interesting for an element when it wants to figure out
3590 * when a particular piece of data will be rendered. */
3593 case GST_EVENT_SEEK:
3594 /* in pull mode we will execute the seek */
3595 if (mode == GST_ACTIVATE_PULL)
3596 result = gst_base_sink_perform_seek (basesink, pad, event);
3603 result = gst_pad_push_event (pad, event);
3605 /* not forwarded, unref the event */
3606 gst_event_unref (event);
3609 gst_object_unref (pad);
3614 gst_base_sink_peer_query (GstBaseSink * sink, GstQuery * query)
3617 gboolean res = FALSE;
3619 if ((peer = gst_pad_get_peer (sink->sinkpad))) {
3620 res = gst_pad_query (peer, query);
3621 gst_object_unref (peer);
3626 /* get the end position of the last seen object, this is used
3627 * for EOS and for making sure that we don't report a position we
3628 * have not reached yet. With LOCK. */
3630 gst_base_sink_get_position_last (GstBaseSink * basesink, GstFormat format,
3634 GstSegment *segment;
3635 gboolean ret = TRUE;
3637 segment = &basesink->segment;
3638 oformat = segment->format;
3640 if (oformat == GST_FORMAT_TIME) {
3641 /* return last observed stream time, we keep the stream time around in the
3643 *cur = basesink->priv->current_sstop;
3645 /* convert last stop to stream time */
3646 *cur = gst_segment_to_stream_time (segment, oformat, segment->last_stop);
3649 if (*cur != -1 && oformat != format) {
3650 GST_OBJECT_UNLOCK (basesink);
3651 /* convert to the target format if we need to, release lock first */
3653 gst_pad_query_convert (basesink->sinkpad, oformat, *cur, &format, cur);
3656 GST_OBJECT_LOCK (basesink);
3659 GST_DEBUG_OBJECT (basesink, "POSITION: %" GST_TIME_FORMAT,
3660 GST_TIME_ARGS (*cur));
3665 /* get the position when we are PAUSED, this is the stream time of the buffer
3666 * that prerolled. If no buffer is prerolled (we are still flushing), this
3667 * value will be -1. With LOCK. */
3669 gst_base_sink_get_position_paused (GstBaseSink * basesink, GstFormat format,
3674 GstSegment *segment;
3677 /* we don't use the clip segment in pull mode, when seeking we update the
3678 * main segment directly with the new segment values without it having to be
3679 * activated by the rendering after preroll */
3680 if (basesink->pad_mode == GST_ACTIVATE_PUSH)
3681 segment = basesink->abidata.ABI.clip_segment;
3683 segment = &basesink->segment;
3684 oformat = segment->format;
3686 if (oformat == GST_FORMAT_TIME) {
3687 *cur = basesink->priv->current_sstart;
3689 *cur = gst_segment_to_stream_time (segment, oformat, segment->last_stop);
3692 time = segment->time;
3695 *cur = MAX (*cur, time);
3696 GST_DEBUG_OBJECT (basesink, "POSITION as max: %" GST_TIME_FORMAT
3697 ", time %" GST_TIME_FORMAT, GST_TIME_ARGS (*cur), GST_TIME_ARGS (time));
3699 /* we have no buffer, use the segment times. */
3700 if (segment->rate >= 0.0) {
3701 /* forward, next position is always the time of the segment */
3703 GST_DEBUG_OBJECT (basesink, "POSITION as time: %" GST_TIME_FORMAT,
3704 GST_TIME_ARGS (*cur));
3706 /* reverse, next expected timestamp is segment->stop. We use the function
3707 * to get things right for negative applied_rates. */
3708 *cur = gst_segment_to_stream_time (segment, oformat, segment->stop);
3709 GST_DEBUG_OBJECT (basesink, "reverse POSITION: %" GST_TIME_FORMAT,
3710 GST_TIME_ARGS (*cur));
3715 if (res && oformat != format) {
3716 GST_OBJECT_UNLOCK (basesink);
3718 gst_pad_query_convert (basesink->sinkpad, oformat, *cur, &format, cur);
3721 GST_OBJECT_LOCK (basesink);
3728 gst_base_sink_get_position (GstBaseSink * basesink, GstFormat format,
3729 gint64 * cur, gboolean * upstream)
3732 gboolean res = FALSE;
3733 GstFormat oformat, tformat;
3734 GstClockTime now, base, latency;
3735 gint64 time, accum, duration;
3739 GST_OBJECT_LOCK (basesink);
3740 /* our intermediate time format */
3741 tformat = GST_FORMAT_TIME;
3742 /* get the format in the segment */
3743 oformat = basesink->segment.format;
3745 /* can only give answer based on the clock if not EOS */
3746 if (G_UNLIKELY (basesink->eos))
3749 /* we can only get the segment when we are not NULL or READY */
3750 if (!basesink->have_newsegment)
3753 /* when not in PLAYING or when we're busy with a state change, we
3754 * cannot read from the clock so we report time based on the
3755 * last seen timestamp. */
3756 if (GST_STATE (basesink) != GST_STATE_PLAYING ||
3757 GST_STATE_PENDING (basesink) != GST_STATE_VOID_PENDING)
3760 /* we need to sync on the clock. */
3761 if (basesink->sync == FALSE)
3764 /* and we need a clock */
3765 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (basesink)) == NULL))
3768 /* collect all data we need holding the lock */
3769 if (GST_CLOCK_TIME_IS_VALID (basesink->segment.time))
3770 time = basesink->segment.time;
3774 if (GST_CLOCK_TIME_IS_VALID (basesink->segment.stop))
3775 duration = basesink->segment.stop - basesink->segment.start;
3779 base = GST_ELEMENT_CAST (basesink)->base_time;
3780 accum = basesink->segment.accum;
3781 rate = basesink->segment.rate * basesink->segment.applied_rate;
3782 latency = basesink->priv->latency;
3784 gst_object_ref (clock);
3786 /* this function might release the LOCK */
3787 gst_base_sink_get_position_last (basesink, format, &last);
3789 /* need to release the object lock before we can get the time,
3790 * a clock might take the LOCK of the provider, which could be
3791 * a basesink subclass. */
3792 GST_OBJECT_UNLOCK (basesink);
3794 now = gst_clock_get_time (clock);
3796 if (oformat != tformat) {
3797 /* convert accum, time and duration to time */
3798 if (!gst_pad_query_convert (basesink->sinkpad, oformat, accum, &tformat,
3800 goto convert_failed;
3801 if (!gst_pad_query_convert (basesink->sinkpad, oformat, duration, &tformat,
3803 goto convert_failed;
3804 if (!gst_pad_query_convert (basesink->sinkpad, oformat, time, &tformat,
3806 goto convert_failed;
3809 /* subtract base time and accumulated time from the clock time.
3810 * Make sure we don't go negative. This is the current time in
3811 * the segment which we need to scale with the combined
3812 * rate and applied rate. */
3815 base = MIN (now, base);
3817 /* for negative rates we need to count back from from the segment
3822 *cur = time + gst_guint64_to_gdouble (now - base) * rate;
3824 /* never report more than last seen position */
3826 *cur = MIN (last, *cur);
3828 gst_object_unref (clock);
3830 GST_DEBUG_OBJECT (basesink,
3831 "now %" GST_TIME_FORMAT " - base %" GST_TIME_FORMAT " - accum %"
3832 GST_TIME_FORMAT " + time %" GST_TIME_FORMAT,
3833 GST_TIME_ARGS (now), GST_TIME_ARGS (base),
3834 GST_TIME_ARGS (accum), GST_TIME_ARGS (time));
3836 if (oformat != format) {
3837 /* convert time to final format */
3838 if (!gst_pad_query_convert (basesink->sinkpad, tformat, *cur, &format, cur))
3839 goto convert_failed;
3845 GST_DEBUG_OBJECT (basesink, "res: %d, POSITION: %" GST_TIME_FORMAT,
3846 res, GST_TIME_ARGS (*cur));
3852 GST_DEBUG_OBJECT (basesink, "position in EOS");
3853 res = gst_base_sink_get_position_last (basesink, format, cur);
3854 GST_OBJECT_UNLOCK (basesink);
3859 GST_DEBUG_OBJECT (basesink, "position in PAUSED");
3860 res = gst_base_sink_get_position_paused (basesink, format, cur);
3861 GST_OBJECT_UNLOCK (basesink);
3866 /* in NULL or READY we always return FALSE and -1 */
3867 GST_DEBUG_OBJECT (basesink, "position in wrong state, return -1");
3870 GST_OBJECT_UNLOCK (basesink);
3875 /* report last seen timestamp if any, else ask upstream to answer */
3876 if ((*cur = basesink->priv->current_sstart) != -1)
3881 GST_DEBUG_OBJECT (basesink, "no sync, res %d, POSITION %" GST_TIME_FORMAT,
3882 res, GST_TIME_ARGS (*cur));
3883 GST_OBJECT_UNLOCK (basesink);
3888 GST_DEBUG_OBJECT (basesink, "convert failed, try upstream");
3895 gst_base_sink_query (GstElement * element, GstQuery * query)
3897 gboolean res = FALSE;
3899 GstBaseSink *basesink = GST_BASE_SINK (element);
3901 switch (GST_QUERY_TYPE (query)) {
3902 case GST_QUERY_POSITION:
3906 gboolean upstream = FALSE;
3908 gst_query_parse_position (query, &format, NULL);
3910 GST_DEBUG_OBJECT (basesink, "position format %d", format);
3912 /* first try to get the position based on the clock */
3914 gst_base_sink_get_position (basesink, format, &cur, &upstream))) {
3915 gst_query_set_position (query, format, cur);
3916 } else if (upstream) {
3917 /* fallback to peer query */
3918 res = gst_base_sink_peer_query (basesink, query);
3922 case GST_QUERY_DURATION:
3924 GstFormat format, uformat;
3925 gint64 duration, uduration;
3927 gst_query_parse_duration (query, &format, NULL);
3929 GST_DEBUG_OBJECT (basesink, "duration query in format %s",
3930 gst_format_get_name (format));
3932 if (basesink->pad_mode == GST_ACTIVATE_PULL) {
3933 uformat = GST_FORMAT_BYTES;
3935 /* get the duration in bytes, in pull mode that's all we are sure to
3936 * know. We have to explicitly get this value from upstream instead of
3937 * using our cached value because it might change. Duration caching
3938 * should be done at a higher level. */
3939 res = gst_pad_query_peer_duration (basesink->sinkpad, &uformat,
3942 gst_segment_set_duration (&basesink->segment, uformat, uduration);
3943 if (format != uformat) {
3944 /* convert to the requested format */
3945 res = gst_pad_query_convert (basesink->sinkpad, uformat, uduration,
3946 &format, &duration);
3948 duration = uduration;
3951 /* set the result */
3952 gst_query_set_duration (query, format, duration);
3956 /* in push mode we simply forward upstream */
3957 res = gst_base_sink_peer_query (basesink, query);
3961 case GST_QUERY_LATENCY:
3963 gboolean live, us_live;
3964 GstClockTime min, max;
3966 if ((res = gst_base_sink_query_latency (basesink, &live, &us_live, &min,
3968 gst_query_set_latency (query, live, min, max);
3972 case GST_QUERY_JITTER:
3974 case GST_QUERY_RATE:
3975 /* gst_query_set_rate (query, basesink->segment_rate); */
3978 case GST_QUERY_SEGMENT:
3980 /* FIXME, bring start/stop to stream time */
3981 gst_query_set_segment (query, basesink->segment.rate,
3982 GST_FORMAT_TIME, basesink->segment.start, basesink->segment.stop);
3985 case GST_QUERY_SEEKING:
3986 case GST_QUERY_CONVERT:
3987 case GST_QUERY_FORMATS:
3989 res = gst_base_sink_peer_query (basesink, query);
3995 static GstStateChangeReturn
3996 gst_base_sink_change_state (GstElement * element, GstStateChange transition)
3998 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
3999 GstBaseSink *basesink = GST_BASE_SINK (element);
4000 GstBaseSinkClass *bclass;
4001 GstBaseSinkPrivate *priv;
4003 priv = basesink->priv;
4005 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4007 switch (transition) {
4008 case GST_STATE_CHANGE_NULL_TO_READY:
4010 if (!bclass->start (basesink))
4013 case GST_STATE_CHANGE_READY_TO_PAUSED:
4014 /* need to complete preroll before this state change completes, there
4015 * is no data flow in READY so we can safely assume we need to preroll. */
4016 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4017 GST_DEBUG_OBJECT (basesink, "READY to PAUSED");
4018 basesink->have_newsegment = FALSE;
4019 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
4020 gst_segment_init (basesink->abidata.ABI.clip_segment,
4021 GST_FORMAT_UNDEFINED);
4022 basesink->offset = 0;
4023 basesink->have_preroll = FALSE;
4024 basesink->need_preroll = TRUE;
4025 basesink->playing_async = TRUE;
4026 priv->current_sstart = -1;
4027 priv->current_sstop = -1;
4028 priv->eos_rtime = -1;
4030 basesink->eos = FALSE;
4031 priv->received_eos = FALSE;
4032 gst_base_sink_reset_qos (basesink);
4033 priv->commited = FALSE;
4034 priv->call_preroll = TRUE;
4035 if (priv->async_enabled) {
4036 GST_DEBUG_OBJECT (basesink, "doing async state change");
4037 /* when async enabled, post async-start message and return ASYNC from
4038 * the state change function */
4039 ret = GST_STATE_CHANGE_ASYNC;
4040 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4041 gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
4043 priv->have_latency = TRUE;
4045 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4047 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
4048 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4049 if (!gst_base_sink_needs_preroll (basesink)) {
4050 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, don't need preroll");
4051 /* no preroll needed anymore now. */
4052 basesink->playing_async = FALSE;
4053 basesink->need_preroll = FALSE;
4054 if (basesink->eos) {
4055 GstMessage *message;
4057 /* need to post EOS message here */
4058 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
4059 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
4060 gst_message_set_seqnum (message, basesink->priv->seqnum);
4061 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
4063 GST_DEBUG_OBJECT (basesink, "signal preroll");
4064 GST_PAD_PREROLL_SIGNAL (basesink->sinkpad);
4067 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, we are not prerolled");
4068 basesink->need_preroll = TRUE;
4069 basesink->playing_async = TRUE;
4070 priv->call_preroll = 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), FALSE));
4079 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4086 GstStateChangeReturn bret;
4088 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4089 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
4090 goto activate_failed;
4093 switch (transition) {
4094 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
4095 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED");
4096 /* FIXME, make sure we cannot enter _render first */
4098 /* we need to call ::unlock before locking PREROLL_LOCK
4099 * since we lock it before going into ::render */
4101 bclass->unlock (basesink);
4103 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4104 /* now that we have the PREROLL lock, clear our unlock request */
4105 if (bclass->unlock_stop)
4106 bclass->unlock_stop (basesink);
4108 /* we need preroll again and we set the flag before unlocking the clockid
4109 * because if the clockid is unlocked before a current buffer expired, we
4110 * can use that buffer to preroll with */
4111 basesink->need_preroll = TRUE;
4113 if (basesink->clock_id) {
4114 gst_clock_id_unschedule (basesink->clock_id);
4117 /* if we don't have a preroll buffer we need to wait for a preroll and
4119 if (!gst_base_sink_needs_preroll (basesink)) {
4120 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, we are prerolled");
4121 basesink->playing_async = FALSE;
4123 if (GST_STATE_TARGET (GST_ELEMENT (basesink)) <= GST_STATE_READY) {
4124 ret = GST_STATE_CHANGE_SUCCESS;
4126 GST_DEBUG_OBJECT (basesink,
4127 "PLAYING to PAUSED, we are not prerolled");
4128 basesink->playing_async = TRUE;
4129 priv->commited = FALSE;
4130 priv->call_preroll = TRUE;
4131 if (priv->async_enabled) {
4132 GST_DEBUG_OBJECT (basesink, "doing async state change");
4133 ret = GST_STATE_CHANGE_ASYNC;
4134 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4135 gst_message_new_async_start (GST_OBJECT_CAST (basesink),
4140 GST_DEBUG_OBJECT (basesink, "rendered: %" G_GUINT64_FORMAT
4141 ", dropped: %" G_GUINT64_FORMAT, priv->rendered, priv->dropped);
4143 gst_base_sink_reset_qos (basesink);
4144 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4146 case GST_STATE_CHANGE_PAUSED_TO_READY:
4147 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4148 /* start by reseting our position state with the object lock so that the
4149 * position query gets the right idea. We do this before we post the
4150 * messages so that the message handlers pick this up. */
4151 GST_OBJECT_LOCK (basesink);
4152 basesink->have_newsegment = FALSE;
4153 priv->current_sstart = -1;
4154 priv->current_sstop = -1;
4155 priv->have_latency = FALSE;
4156 GST_OBJECT_UNLOCK (basesink);
4158 gst_base_sink_set_last_buffer (basesink, NULL);
4159 priv->call_preroll = FALSE;
4161 if (!priv->commited) {
4162 if (priv->async_enabled) {
4163 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, posting async-done");
4165 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4166 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
4167 GST_STATE_PLAYING, GST_STATE_PAUSED, GST_STATE_READY));
4169 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4170 gst_message_new_async_done (GST_OBJECT_CAST (basesink)));
4172 priv->commited = TRUE;
4174 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, don't need_preroll");
4176 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4178 case GST_STATE_CHANGE_READY_TO_NULL:
4180 if (!bclass->stop (basesink)) {
4181 GST_WARNING_OBJECT (basesink, "failed to stop");
4184 gst_base_sink_set_last_buffer (basesink, NULL);
4185 priv->call_preroll = FALSE;
4196 GST_DEBUG_OBJECT (basesink, "failed to start");
4197 return GST_STATE_CHANGE_FAILURE;
4201 GST_DEBUG_OBJECT (basesink,
4202 "element failed to change states -- activation problem?");
4203 return GST_STATE_CHANGE_FAILURE;