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 GType base_sink_type = 0;
280 if (G_UNLIKELY (base_sink_type == 0)) {
281 static const GTypeInfo base_sink_info = {
282 sizeof (GstBaseSinkClass),
285 (GClassInitFunc) gst_base_sink_class_init,
288 sizeof (GstBaseSink),
290 (GInstanceInitFunc) gst_base_sink_init,
293 base_sink_type = g_type_register_static (GST_TYPE_ELEMENT,
294 "GstBaseSink", &base_sink_info, G_TYPE_FLAG_ABSTRACT);
296 return base_sink_type;
299 static void gst_base_sink_set_property (GObject * object, guint prop_id,
300 const GValue * value, GParamSpec * pspec);
301 static void gst_base_sink_get_property (GObject * object, guint prop_id,
302 GValue * value, GParamSpec * pspec);
304 static gboolean gst_base_sink_send_event (GstElement * element,
306 static gboolean gst_base_sink_query (GstElement * element, GstQuery * query);
308 static GstCaps *gst_base_sink_get_caps (GstBaseSink * sink);
309 static gboolean gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps);
310 static GstFlowReturn gst_base_sink_buffer_alloc (GstBaseSink * sink,
311 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
312 static void gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
313 GstClockTime * start, GstClockTime * end);
314 static gboolean gst_base_sink_set_flushing (GstBaseSink * basesink,
315 GstPad * pad, gboolean flushing);
316 static gboolean gst_base_sink_default_activate_pull (GstBaseSink * basesink,
318 static gboolean gst_base_sink_default_do_seek (GstBaseSink * sink,
319 GstSegment * segment);
320 static gboolean gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
321 GstEvent * event, GstSegment * segment);
323 static GstStateChangeReturn gst_base_sink_change_state (GstElement * element,
324 GstStateChange transition);
326 static GstFlowReturn gst_base_sink_chain (GstPad * pad, GstBuffer * buffer);
327 static void gst_base_sink_loop (GstPad * pad);
328 static gboolean gst_base_sink_pad_activate (GstPad * pad);
329 static gboolean gst_base_sink_pad_activate_push (GstPad * pad, gboolean active);
330 static gboolean gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active);
331 static gboolean gst_base_sink_event (GstPad * pad, GstEvent * event);
332 static gboolean gst_base_sink_peer_query (GstBaseSink * sink, GstQuery * query);
334 static gboolean gst_base_sink_negotiate_pull (GstBaseSink * basesink);
336 /* check if an object was too late */
337 static gboolean gst_base_sink_is_too_late (GstBaseSink * basesink,
338 GstMiniObject * obj, GstClockTime start, GstClockTime stop,
339 GstClockReturn status, GstClockTimeDiff jitter);
340 static GstFlowReturn gst_base_sink_preroll_object (GstBaseSink * basesink,
341 GstMiniObject * obj);
344 gst_base_sink_class_init (GstBaseSinkClass * klass)
346 GObjectClass *gobject_class;
347 GstElementClass *gstelement_class;
349 gobject_class = G_OBJECT_CLASS (klass);
350 gstelement_class = GST_ELEMENT_CLASS (klass);
352 GST_DEBUG_CATEGORY_INIT (gst_base_sink_debug, "basesink", 0,
355 g_type_class_add_private (klass, sizeof (GstBaseSinkPrivate));
357 parent_class = g_type_class_peek_parent (klass);
359 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_sink_finalize);
360 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_base_sink_set_property);
361 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_base_sink_get_property);
363 /* FIXME, this next value should be configured using an event from the
364 * upstream element, ie, the BUFFER_SIZE event. */
365 g_object_class_install_property (gobject_class, PROP_PREROLL_QUEUE_LEN,
366 g_param_spec_uint ("preroll-queue-len", "Preroll queue length",
367 "Number of buffers to queue during preroll", 0, G_MAXUINT,
368 DEFAULT_PREROLL_QUEUE_LEN,
369 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
371 g_object_class_install_property (gobject_class, PROP_SYNC,
372 g_param_spec_boolean ("sync", "Sync", "Sync on the clock", DEFAULT_SYNC,
373 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
375 g_object_class_install_property (gobject_class, PROP_MAX_LATENESS,
376 g_param_spec_int64 ("max-lateness", "Max Lateness",
377 "Maximum number of nanoseconds that a buffer can be late before it "
378 "is dropped (-1 unlimited)", -1, G_MAXINT64, DEFAULT_MAX_LATENESS,
379 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
381 g_object_class_install_property (gobject_class, PROP_QOS,
382 g_param_spec_boolean ("qos", "Qos",
383 "Generate Quality-of-Service events upstream", DEFAULT_QOS,
384 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
388 * If set to #TRUE, the basesink will perform asynchronous state changes.
389 * When set to #FALSE, the sink will not signal the parent when it prerolls.
390 * Use this option when dealing with sparse streams or when synchronisation is
395 g_object_class_install_property (gobject_class, PROP_ASYNC,
396 g_param_spec_boolean ("async", "Async",
397 "Go asynchronously to PAUSED", DEFAULT_ASYNC,
398 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
400 * GstBaseSink:ts-offset
402 * Controls the final synchronisation, a negative value will render the buffer
403 * earlier while a positive value delays playback. This property can be
404 * used to fix synchronisation in bad files.
408 g_object_class_install_property (gobject_class, PROP_TS_OFFSET,
409 g_param_spec_int64 ("ts-offset", "TS Offset",
410 "Timestamp offset in nanoseconds", G_MININT64, G_MAXINT64,
411 DEFAULT_TS_OFFSET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
413 * GstBaseSink:last-buffer
415 * The last buffer that arrived in the sink and was used for preroll or for
416 * rendering. This property can be used to generate thumbnails. This property
417 * can be NULL when the sink has not yet received a bufer.
421 g_object_class_install_property (gobject_class, PROP_LAST_BUFFER,
422 gst_param_spec_mini_object ("last-buffer", "Last Buffer",
423 "The last buffer received in the sink", GST_TYPE_BUFFER,
424 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
426 * GstBaseSink:blocksize
428 * The amount of bytes to pull when operating in pull mode.
432 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
433 g_param_spec_uint ("blocksize", "Block size",
434 "Size in bytes to pull per buffer (0 = default)", 0, G_MAXUINT,
435 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
437 * GstBaseSink:render-delay
439 * The additional delay between synchronisation and actual rendering of the
440 * media. This property will add additional latency to the device in order to
441 * make other sinks compensate for the delay.
445 g_object_class_install_property (gobject_class, PROP_RENDER_DELAY,
446 g_param_spec_uint64 ("render-delay", "Render Delay",
447 "Additional render delay of the sink in nanoseconds", 0, G_MAXUINT64,
448 DEFAULT_RENDER_DELAY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
450 gstelement_class->change_state =
451 GST_DEBUG_FUNCPTR (gst_base_sink_change_state);
452 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_sink_send_event);
453 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_base_sink_query);
455 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_sink_get_caps);
456 klass->set_caps = GST_DEBUG_FUNCPTR (gst_base_sink_set_caps);
457 klass->buffer_alloc = GST_DEBUG_FUNCPTR (gst_base_sink_buffer_alloc);
458 klass->get_times = GST_DEBUG_FUNCPTR (gst_base_sink_get_times);
459 klass->activate_pull =
460 GST_DEBUG_FUNCPTR (gst_base_sink_default_activate_pull);
464 gst_base_sink_pad_getcaps (GstPad * pad)
466 GstBaseSinkClass *bclass;
468 GstCaps *caps = NULL;
470 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
471 bclass = GST_BASE_SINK_GET_CLASS (bsink);
473 if (bsink->pad_mode == GST_ACTIVATE_PULL) {
474 /* if we are operating in pull mode we only accept the negotiated caps */
475 GST_OBJECT_LOCK (pad);
476 if ((caps = GST_PAD_CAPS (pad)))
478 GST_OBJECT_UNLOCK (pad);
481 if (bclass->get_caps)
482 caps = bclass->get_caps (bsink);
485 GstPadTemplate *pad_template;
488 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass),
490 if (pad_template != NULL) {
491 caps = gst_caps_ref (gst_pad_template_get_caps (pad_template));
495 gst_object_unref (bsink);
501 gst_base_sink_pad_setcaps (GstPad * pad, GstCaps * caps)
503 GstBaseSinkClass *bclass;
507 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
508 bclass = GST_BASE_SINK_GET_CLASS (bsink);
510 if (res && bclass->set_caps)
511 res = bclass->set_caps (bsink, caps);
513 gst_object_unref (bsink);
519 gst_base_sink_pad_fixate (GstPad * pad, GstCaps * caps)
521 GstBaseSinkClass *bclass;
524 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
525 bclass = GST_BASE_SINK_GET_CLASS (bsink);
528 bclass->fixate (bsink, caps);
530 gst_object_unref (bsink);
534 gst_base_sink_pad_buffer_alloc (GstPad * pad, guint64 offset, guint size,
535 GstCaps * caps, GstBuffer ** buf)
537 GstBaseSinkClass *bclass;
539 GstFlowReturn result = GST_FLOW_OK;
541 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
542 bclass = GST_BASE_SINK_GET_CLASS (bsink);
544 if (bclass->buffer_alloc)
545 result = bclass->buffer_alloc (bsink, offset, size, caps, buf);
547 *buf = NULL; /* fallback in gstpad.c will allocate generic buffer */
549 gst_object_unref (bsink);
555 gst_base_sink_init (GstBaseSink * basesink, gpointer g_class)
557 GstPadTemplate *pad_template;
558 GstBaseSinkPrivate *priv;
560 basesink->priv = priv = GST_BASE_SINK_GET_PRIVATE (basesink);
563 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
564 g_return_if_fail (pad_template != NULL);
566 basesink->sinkpad = gst_pad_new_from_template (pad_template, "sink");
568 gst_pad_set_getcaps_function (basesink->sinkpad,
569 GST_DEBUG_FUNCPTR (gst_base_sink_pad_getcaps));
570 gst_pad_set_setcaps_function (basesink->sinkpad,
571 GST_DEBUG_FUNCPTR (gst_base_sink_pad_setcaps));
572 gst_pad_set_fixatecaps_function (basesink->sinkpad,
573 GST_DEBUG_FUNCPTR (gst_base_sink_pad_fixate));
574 gst_pad_set_bufferalloc_function (basesink->sinkpad,
575 GST_DEBUG_FUNCPTR (gst_base_sink_pad_buffer_alloc));
576 gst_pad_set_activate_function (basesink->sinkpad,
577 GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate));
578 gst_pad_set_activatepush_function (basesink->sinkpad,
579 GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate_push));
580 gst_pad_set_activatepull_function (basesink->sinkpad,
581 GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate_pull));
582 gst_pad_set_event_function (basesink->sinkpad,
583 GST_DEBUG_FUNCPTR (gst_base_sink_event));
584 gst_pad_set_chain_function (basesink->sinkpad,
585 GST_DEBUG_FUNCPTR (gst_base_sink_chain));
586 gst_element_add_pad (GST_ELEMENT_CAST (basesink), basesink->sinkpad);
588 basesink->pad_mode = GST_ACTIVATE_NONE;
589 basesink->preroll_queue = g_queue_new ();
590 basesink->abidata.ABI.clip_segment = gst_segment_new ();
591 priv->have_latency = FALSE;
593 basesink->can_activate_push = DEFAULT_CAN_ACTIVATE_PUSH;
594 basesink->can_activate_pull = DEFAULT_CAN_ACTIVATE_PULL;
596 basesink->sync = DEFAULT_SYNC;
597 basesink->abidata.ABI.max_lateness = DEFAULT_MAX_LATENESS;
598 g_atomic_int_set (&priv->qos_enabled, DEFAULT_QOS);
599 priv->async_enabled = DEFAULT_ASYNC;
600 priv->ts_offset = DEFAULT_TS_OFFSET;
601 priv->render_delay = DEFAULT_RENDER_DELAY;
602 priv->blocksize = DEFAULT_BLOCKSIZE;
604 GST_OBJECT_FLAG_SET (basesink, GST_ELEMENT_IS_SINK);
608 gst_base_sink_finalize (GObject * object)
610 GstBaseSink *basesink;
612 basesink = GST_BASE_SINK (object);
614 g_queue_free (basesink->preroll_queue);
615 gst_segment_free (basesink->abidata.ABI.clip_segment);
617 G_OBJECT_CLASS (parent_class)->finalize (object);
621 * gst_base_sink_set_sync:
623 * @sync: the new sync value.
625 * Configures @sink to synchronize on the clock or not. When
626 * @sync is FALSE, incomming samples will be played as fast as
627 * possible. If @sync is TRUE, the timestamps of the incomming
628 * buffers will be used to schedule the exact render time of its
634 gst_base_sink_set_sync (GstBaseSink * sink, gboolean sync)
636 g_return_if_fail (GST_IS_BASE_SINK (sink));
638 GST_OBJECT_LOCK (sink);
640 GST_OBJECT_UNLOCK (sink);
644 * gst_base_sink_get_sync:
647 * Checks if @sink is currently configured to synchronize against the
650 * Returns: TRUE if the sink is configured to synchronize against the clock.
655 gst_base_sink_get_sync (GstBaseSink * sink)
659 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
661 GST_OBJECT_LOCK (sink);
663 GST_OBJECT_UNLOCK (sink);
669 * gst_base_sink_set_max_lateness:
671 * @max_lateness: the new max lateness value.
673 * Sets the new max lateness value to @max_lateness. This value is
674 * used to decide if a buffer should be dropped or not based on the
675 * buffer timestamp and the current clock time. A value of -1 means
681 gst_base_sink_set_max_lateness (GstBaseSink * sink, gint64 max_lateness)
683 g_return_if_fail (GST_IS_BASE_SINK (sink));
685 GST_OBJECT_LOCK (sink);
686 sink->abidata.ABI.max_lateness = max_lateness;
687 GST_OBJECT_UNLOCK (sink);
691 * gst_base_sink_get_max_lateness:
694 * Gets the max lateness value. See gst_base_sink_set_max_lateness for
697 * Returns: The maximum time in nanoseconds that a buffer can be late
698 * before it is dropped and not rendered. A value of -1 means an
704 gst_base_sink_get_max_lateness (GstBaseSink * sink)
708 g_return_val_if_fail (GST_IS_BASE_SINK (sink), -1);
710 GST_OBJECT_LOCK (sink);
711 res = sink->abidata.ABI.max_lateness;
712 GST_OBJECT_UNLOCK (sink);
718 * gst_base_sink_set_qos_enabled:
720 * @enabled: the new qos value.
722 * Configures @sink to send Quality-of-Service events upstream.
727 gst_base_sink_set_qos_enabled (GstBaseSink * sink, gboolean enabled)
729 g_return_if_fail (GST_IS_BASE_SINK (sink));
731 g_atomic_int_set (&sink->priv->qos_enabled, enabled);
735 * gst_base_sink_is_qos_enabled:
738 * Checks if @sink is currently configured to send Quality-of-Service events
741 * Returns: TRUE if the sink is configured to perform Quality-of-Service.
746 gst_base_sink_is_qos_enabled (GstBaseSink * sink)
750 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
752 res = g_atomic_int_get (&sink->priv->qos_enabled);
758 * gst_base_sink_set_async_enabled:
760 * @enabled: the new async value.
762 * Configures @sink to perform all state changes asynchronusly. When async is
763 * disabled, the sink will immediatly go to PAUSED instead of waiting for a
764 * preroll buffer. This feature is usefull if the sink does not synchronize
765 * against the clock or when it is dealing with sparse streams.
770 gst_base_sink_set_async_enabled (GstBaseSink * sink, gboolean enabled)
772 g_return_if_fail (GST_IS_BASE_SINK (sink));
774 GST_PAD_PREROLL_LOCK (sink->sinkpad);
775 sink->priv->async_enabled = enabled;
776 GST_LOG_OBJECT (sink, "set async enabled to %d", enabled);
777 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
781 * gst_base_sink_is_async_enabled:
784 * Checks if @sink is currently configured to perform asynchronous state
787 * Returns: TRUE if the sink is configured to perform asynchronous state
793 gst_base_sink_is_async_enabled (GstBaseSink * sink)
797 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
799 GST_PAD_PREROLL_LOCK (sink->sinkpad);
800 res = sink->priv->async_enabled;
801 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
807 * gst_base_sink_set_ts_offset:
809 * @offset: the new offset
811 * Adjust the synchronisation of @sink with @offset. A negative value will
812 * render buffers earlier than their timestamp. A positive value will delay
813 * rendering. This function can be used to fix playback of badly timestamped
819 gst_base_sink_set_ts_offset (GstBaseSink * sink, GstClockTimeDiff offset)
821 g_return_if_fail (GST_IS_BASE_SINK (sink));
823 GST_OBJECT_LOCK (sink);
824 sink->priv->ts_offset = offset;
825 GST_LOG_OBJECT (sink, "set time offset to %" G_GINT64_FORMAT, offset);
826 GST_OBJECT_UNLOCK (sink);
830 * gst_base_sink_get_ts_offset:
833 * Get the synchronisation offset of @sink.
835 * Returns: The synchronisation offset.
840 gst_base_sink_get_ts_offset (GstBaseSink * sink)
842 GstClockTimeDiff res;
844 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
846 GST_OBJECT_LOCK (sink);
847 res = sink->priv->ts_offset;
848 GST_OBJECT_UNLOCK (sink);
854 * gst_base_sink_get_last_buffer:
857 * Get the last buffer that arrived in the sink and was used for preroll or for
858 * rendering. This property can be used to generate thumbnails.
860 * The #GstCaps on the buffer can be used to determine the type of the buffer.
862 * Returns: a #GstBuffer. gst_buffer_unref() after usage. This function returns
863 * NULL when no buffer has arrived in the sink yet or when the sink is not in
869 gst_base_sink_get_last_buffer (GstBaseSink * sink)
873 g_return_val_if_fail (GST_IS_BASE_SINK (sink), NULL);
875 GST_OBJECT_LOCK (sink);
876 if ((res = sink->priv->last_buffer))
877 gst_buffer_ref (res);
878 GST_OBJECT_UNLOCK (sink);
884 gst_base_sink_set_last_buffer (GstBaseSink * sink, GstBuffer * buffer)
888 GST_OBJECT_LOCK (sink);
889 old = sink->priv->last_buffer;
890 if (G_LIKELY (old != buffer)) {
891 GST_DEBUG_OBJECT (sink, "setting last buffer to %p", buffer);
892 if (G_LIKELY (buffer))
893 gst_buffer_ref (buffer);
894 sink->priv->last_buffer = buffer;
898 GST_OBJECT_UNLOCK (sink);
900 /* avoid unreffing with the lock because cleanup code might want to take the
903 gst_buffer_unref (old);
907 * gst_base_sink_get_latency:
910 * Get the currently configured latency.
912 * Returns: The configured latency.
917 gst_base_sink_get_latency (GstBaseSink * sink)
921 GST_OBJECT_LOCK (sink);
922 res = sink->priv->latency;
923 GST_OBJECT_UNLOCK (sink);
929 * gst_base_sink_query_latency:
931 * @live: if the sink is live
932 * @upstream_live: if an upstream element is live
933 * @min_latency: the min latency of the upstream elements
934 * @max_latency: the max latency of the upstream elements
936 * Query the sink for the latency parameters. The latency will be queried from
937 * the upstream elements. @live will be TRUE if @sink is configured to
938 * synchronize against the clock. @upstream_live will be TRUE if an upstream
941 * If both @live and @upstream_live are TRUE, the sink will want to compensate
942 * for the latency introduced by the upstream elements by setting the
943 * @min_latency to a strictly possitive value.
945 * This function is mostly used by subclasses.
947 * Returns: TRUE if the query succeeded.
952 gst_base_sink_query_latency (GstBaseSink * sink, gboolean * live,
953 gboolean * upstream_live, GstClockTime * min_latency,
954 GstClockTime * max_latency)
956 gboolean l, us_live, res, have_latency;
957 GstClockTime min, max, render_delay;
959 GstClockTime us_min, us_max;
961 /* we are live when we sync to the clock */
962 GST_OBJECT_LOCK (sink);
964 have_latency = sink->priv->have_latency;
965 render_delay = sink->priv->render_delay;
966 GST_OBJECT_UNLOCK (sink);
968 /* assume no latency */
974 GST_DEBUG_OBJECT (sink, "we are ready for LATENCY query");
975 /* we are ready for a latency query this is when we preroll or when we are
977 query = gst_query_new_latency ();
979 /* ask the peer for the latency */
980 if ((res = gst_base_sink_peer_query (sink, query))) {
981 /* get upstream min and max latency */
982 gst_query_parse_latency (query, &us_live, &us_min, &us_max);
985 /* upstream live, use its latency, subclasses should use these
986 * values to create the complete latency. */
991 /* we need to add the render delay if we are live */
998 gst_query_unref (query);
1000 GST_DEBUG_OBJECT (sink, "we are not yet ready for LATENCY query");
1004 /* not live, we tried to do the query, if it failed we return TRUE anyway */
1008 GST_DEBUG_OBJECT (sink, "latency query failed but we are not live");
1010 GST_DEBUG_OBJECT (sink, "latency query failed and we are live");
1015 GST_DEBUG_OBJECT (sink, "latency query: live: %d, have_latency %d,"
1016 " upstream: %d, min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT, l,
1017 have_latency, us_live, GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1022 *upstream_live = us_live;
1032 * gst_base_sink_set_render_delay:
1033 * @sink: a #GstBaseSink
1034 * @delay: the new delay
1036 * Set the render delay in @sink to @delay. The render delay is the time
1037 * between actual rendering of a buffer and its synchronisation time. Some
1038 * devices might delay media rendering which can be compensated for with this
1041 * After calling this function, this sink will report additional latency and
1042 * other sinks will adjust their latency to delay the rendering of their media.
1044 * This function is usually called by subclasses.
1049 gst_base_sink_set_render_delay (GstBaseSink * sink, GstClockTime delay)
1051 GstClockTime old_render_delay;
1053 g_return_if_fail (GST_IS_BASE_SINK (sink));
1055 GST_OBJECT_LOCK (sink);
1056 old_render_delay = sink->priv->render_delay;
1057 sink->priv->render_delay = delay;
1058 GST_LOG_OBJECT (sink, "set render delay to %" GST_TIME_FORMAT,
1059 GST_TIME_ARGS (delay));
1060 GST_OBJECT_UNLOCK (sink);
1062 if (delay != old_render_delay) {
1063 GST_DEBUG_OBJECT (sink, "posting latency changed");
1064 gst_element_post_message (GST_ELEMENT_CAST (sink),
1065 gst_message_new_latency (GST_OBJECT_CAST (sink)));
1070 * gst_base_sink_get_render_delay:
1071 * @sink: a #GstBaseSink
1073 * Get the render delay of @sink. see gst_base_sink_set_render_delay() for more
1074 * information about the render delay.
1076 * Returns: the render delay of @sink.
1081 gst_base_sink_get_render_delay (GstBaseSink * sink)
1083 GstClockTimeDiff res;
1085 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1087 GST_OBJECT_LOCK (sink);
1088 res = sink->priv->render_delay;
1089 GST_OBJECT_UNLOCK (sink);
1095 * gst_base_sink_set_blocksize:
1096 * @sink: a #GstBaseSink
1097 * @blocksize: the blocksize in bytes
1099 * Set the number of bytes that the sink will pull when it is operating in pull
1105 gst_base_sink_set_blocksize (GstBaseSink * sink, guint blocksize)
1107 g_return_if_fail (GST_IS_BASE_SINK (sink));
1109 GST_OBJECT_LOCK (sink);
1110 sink->priv->blocksize = blocksize;
1111 GST_LOG_OBJECT (sink, "set blocksize to %u", blocksize);
1112 GST_OBJECT_UNLOCK (sink);
1116 * gst_base_sink_get_blocksize:
1117 * @sink: a #GstBaseSink
1119 * Get the number of bytes that the sink will pull when it is operating in pull
1122 * Returns: the number of bytes @sink will pull in pull mode.
1127 gst_base_sink_get_blocksize (GstBaseSink * sink)
1131 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1133 GST_OBJECT_LOCK (sink);
1134 res = sink->priv->blocksize;
1135 GST_OBJECT_UNLOCK (sink);
1141 gst_base_sink_set_property (GObject * object, guint prop_id,
1142 const GValue * value, GParamSpec * pspec)
1144 GstBaseSink *sink = GST_BASE_SINK (object);
1147 case PROP_PREROLL_QUEUE_LEN:
1148 /* preroll lock necessary to serialize with finish_preroll */
1149 GST_PAD_PREROLL_LOCK (sink->sinkpad);
1150 sink->preroll_queue_max_len = g_value_get_uint (value);
1151 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
1154 gst_base_sink_set_sync (sink, g_value_get_boolean (value));
1156 case PROP_MAX_LATENESS:
1157 gst_base_sink_set_max_lateness (sink, g_value_get_int64 (value));
1160 gst_base_sink_set_qos_enabled (sink, g_value_get_boolean (value));
1163 gst_base_sink_set_async_enabled (sink, g_value_get_boolean (value));
1165 case PROP_TS_OFFSET:
1166 gst_base_sink_set_ts_offset (sink, g_value_get_int64 (value));
1168 case PROP_BLOCKSIZE:
1169 gst_base_sink_set_blocksize (sink, g_value_get_uint (value));
1171 case PROP_RENDER_DELAY:
1172 gst_base_sink_set_render_delay (sink, g_value_get_uint64 (value));
1175 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1181 gst_base_sink_get_property (GObject * object, guint prop_id, GValue * value,
1184 GstBaseSink *sink = GST_BASE_SINK (object);
1187 case PROP_PREROLL_QUEUE_LEN:
1188 GST_PAD_PREROLL_LOCK (sink->sinkpad);
1189 g_value_set_uint (value, sink->preroll_queue_max_len);
1190 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
1193 g_value_set_boolean (value, gst_base_sink_get_sync (sink));
1195 case PROP_MAX_LATENESS:
1196 g_value_set_int64 (value, gst_base_sink_get_max_lateness (sink));
1199 g_value_set_boolean (value, gst_base_sink_is_qos_enabled (sink));
1202 g_value_set_boolean (value, gst_base_sink_is_async_enabled (sink));
1204 case PROP_TS_OFFSET:
1205 g_value_set_int64 (value, gst_base_sink_get_ts_offset (sink));
1207 case PROP_LAST_BUFFER:
1208 gst_value_take_buffer (value, gst_base_sink_get_last_buffer (sink));
1210 case PROP_BLOCKSIZE:
1211 g_value_set_uint (value, gst_base_sink_get_blocksize (sink));
1213 case PROP_RENDER_DELAY:
1214 g_value_set_uint64 (value, gst_base_sink_get_render_delay (sink));
1217 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1224 gst_base_sink_get_caps (GstBaseSink * sink)
1230 gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps)
1235 static GstFlowReturn
1236 gst_base_sink_buffer_alloc (GstBaseSink * sink, guint64 offset, guint size,
1237 GstCaps * caps, GstBuffer ** buf)
1243 /* with PREROLL_LOCK, STREAM_LOCK */
1245 gst_base_sink_preroll_queue_flush (GstBaseSink * basesink, GstPad * pad)
1249 GST_DEBUG_OBJECT (basesink, "flushing queue %p", basesink);
1250 while ((obj = g_queue_pop_head (basesink->preroll_queue))) {
1251 GST_DEBUG_OBJECT (basesink, "popped %p", obj);
1252 gst_mini_object_unref (obj);
1254 /* we can't have EOS anymore now */
1255 basesink->eos = FALSE;
1256 basesink->priv->received_eos = FALSE;
1257 basesink->have_preroll = FALSE;
1258 basesink->eos_queued = FALSE;
1259 basesink->preroll_queued = 0;
1260 basesink->buffers_queued = 0;
1261 basesink->events_queued = 0;
1262 /* can't report latency anymore until we preroll again */
1263 if (basesink->priv->async_enabled) {
1264 GST_OBJECT_LOCK (basesink);
1265 basesink->priv->have_latency = FALSE;
1266 GST_OBJECT_UNLOCK (basesink);
1268 /* and signal any waiters now */
1269 GST_PAD_PREROLL_SIGNAL (pad);
1272 /* with STREAM_LOCK, configures given segment with the event information. */
1274 gst_base_sink_configure_segment (GstBaseSink * basesink, GstPad * pad,
1275 GstEvent * event, GstSegment * segment)
1278 gdouble rate, arate;
1284 /* the newsegment event is needed to bring the buffer timestamps to the
1285 * stream time and to drop samples outside of the playback segment. */
1286 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1287 &start, &stop, &time);
1289 /* The segment is protected with both the STREAM_LOCK and the OBJECT_LOCK.
1290 * We protect with the OBJECT_LOCK so that we can use the values to
1291 * safely answer a POSITION query. */
1292 GST_OBJECT_LOCK (basesink);
1293 gst_segment_set_newsegment_full (segment, update, rate, arate, format, start,
1296 if (format == GST_FORMAT_TIME) {
1297 GST_DEBUG_OBJECT (basesink,
1298 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1299 "format GST_FORMAT_TIME, "
1300 "%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
1301 ", time %" GST_TIME_FORMAT ", accum %" GST_TIME_FORMAT,
1302 update, rate, arate, GST_TIME_ARGS (segment->start),
1303 GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time),
1304 GST_TIME_ARGS (segment->accum));
1306 GST_DEBUG_OBJECT (basesink,
1307 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1309 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
1310 G_GINT64_FORMAT ", accum %" G_GINT64_FORMAT, update, rate, arate,
1311 segment->format, segment->start, segment->stop, segment->time,
1314 GST_OBJECT_UNLOCK (basesink);
1317 /* with PREROLL_LOCK, STREAM_LOCK */
1319 gst_base_sink_commit_state (GstBaseSink * basesink)
1321 /* commit state and proceed to next pending state */
1322 GstState current, next, pending, post_pending;
1323 gboolean post_paused = FALSE;
1324 gboolean post_async_done = FALSE;
1325 gboolean post_playing = FALSE;
1327 /* we are certainly not playing async anymore now */
1328 basesink->playing_async = FALSE;
1330 GST_OBJECT_LOCK (basesink);
1331 current = GST_STATE (basesink);
1332 next = GST_STATE_NEXT (basesink);
1333 pending = GST_STATE_PENDING (basesink);
1334 post_pending = pending;
1337 case GST_STATE_PLAYING:
1339 GstBaseSinkClass *bclass;
1340 GstStateChangeReturn ret;
1342 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1344 GST_DEBUG_OBJECT (basesink, "commiting state to PLAYING");
1346 basesink->need_preroll = FALSE;
1347 post_async_done = TRUE;
1348 basesink->priv->commited = TRUE;
1349 post_playing = TRUE;
1350 /* post PAUSED too when we were READY */
1351 if (current == GST_STATE_READY) {
1355 /* make sure we notify the subclass of async playing */
1356 if (bclass->async_play) {
1357 GST_WARNING_OBJECT (basesink, "deprecated async_play");
1358 ret = bclass->async_play (basesink);
1359 if (ret == GST_STATE_CHANGE_FAILURE)
1364 case GST_STATE_PAUSED:
1365 GST_DEBUG_OBJECT (basesink, "commiting state to PAUSED");
1367 post_async_done = TRUE;
1368 basesink->priv->commited = TRUE;
1369 post_pending = GST_STATE_VOID_PENDING;
1371 case GST_STATE_READY:
1372 case GST_STATE_NULL:
1374 case GST_STATE_VOID_PENDING:
1375 goto nothing_pending;
1380 /* we can report latency queries now */
1381 basesink->priv->have_latency = TRUE;
1383 GST_STATE (basesink) = pending;
1384 GST_STATE_NEXT (basesink) = GST_STATE_VOID_PENDING;
1385 GST_STATE_PENDING (basesink) = GST_STATE_VOID_PENDING;
1386 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_SUCCESS;
1387 GST_OBJECT_UNLOCK (basesink);
1390 GST_DEBUG_OBJECT (basesink, "posting PAUSED state change message");
1391 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1392 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1393 current, next, post_pending));
1395 if (post_async_done) {
1396 GST_DEBUG_OBJECT (basesink, "posting async-done message");
1397 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1398 gst_message_new_async_done (GST_OBJECT_CAST (basesink)));
1401 GST_DEBUG_OBJECT (basesink, "posting PLAYING state change message");
1402 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1403 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1404 next, pending, GST_STATE_VOID_PENDING));
1407 GST_STATE_BROADCAST (basesink);
1413 /* Depending on the state, set our vars. We get in this situation when the
1414 * state change function got a change to update the state vars before the
1415 * streaming thread did. This is fine but we need to make sure that we
1416 * update the need_preroll var since it was TRUE when we got here and might
1417 * become FALSE if we got to PLAYING. */
1418 GST_DEBUG_OBJECT (basesink, "nothing to commit, now in %s",
1419 gst_element_state_get_name (current));
1421 case GST_STATE_PLAYING:
1422 basesink->need_preroll = FALSE;
1424 case GST_STATE_PAUSED:
1425 basesink->need_preroll = TRUE;
1428 basesink->need_preroll = FALSE;
1429 basesink->flushing = TRUE;
1432 /* we can report latency queries now */
1433 basesink->priv->have_latency = TRUE;
1434 GST_OBJECT_UNLOCK (basesink);
1439 /* app is going to READY */
1440 GST_DEBUG_OBJECT (basesink, "stopping");
1441 basesink->need_preroll = FALSE;
1442 basesink->flushing = TRUE;
1443 GST_OBJECT_UNLOCK (basesink);
1448 GST_DEBUG_OBJECT (basesink, "async commit failed");
1449 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_FAILURE;
1450 GST_OBJECT_UNLOCK (basesink);
1456 /* with STREAM_LOCK, PREROLL_LOCK
1458 * Returns TRUE if the object needs synchronisation and takes therefore
1459 * part in prerolling.
1461 * rsstart/rsstop contain the start/stop in stream time.
1462 * rrstart/rrstop contain the start/stop in running time.
1465 gst_base_sink_get_sync_times (GstBaseSink * basesink, GstMiniObject * obj,
1466 GstClockTime * rsstart, GstClockTime * rsstop,
1467 GstClockTime * rrstart, GstClockTime * rrstop, gboolean * do_sync,
1468 GstSegment * segment)
1470 GstBaseSinkClass *bclass;
1472 GstClockTime start, stop; /* raw start/stop timestamps */
1473 gint64 cstart, cstop; /* clipped raw timestamps */
1474 gint64 rstart, rstop; /* clipped timestamps converted to running time */
1475 GstClockTime sstart, sstop; /* clipped timestamps converted to stream time */
1477 GstBaseSinkPrivate *priv;
1479 priv = basesink->priv;
1481 /* start with nothing */
1482 start = stop = sstart = sstop = rstart = rstop = -1;
1484 if (G_UNLIKELY (GST_IS_EVENT (obj))) {
1485 GstEvent *event = GST_EVENT_CAST (obj);
1487 switch (GST_EVENT_TYPE (event)) {
1488 /* EOS event needs syncing */
1491 if (basesink->segment.rate >= 0.0) {
1492 sstart = sstop = priv->current_sstop;
1494 /* we have not seen a buffer yet, use the segment values */
1495 sstart = sstop = gst_segment_to_stream_time (&basesink->segment,
1496 basesink->segment.format, basesink->segment.stop);
1499 sstart = sstop = priv->current_sstart;
1501 /* we have not seen a buffer yet, use the segment values */
1502 sstart = sstop = gst_segment_to_stream_time (&basesink->segment,
1503 basesink->segment.format, basesink->segment.start);
1507 rstart = rstop = priv->eos_rtime;
1508 *do_sync = rstart != -1;
1509 GST_DEBUG_OBJECT (basesink, "sync times for EOS %" GST_TIME_FORMAT,
1510 GST_TIME_ARGS (rstart));
1514 /* other events do not need syncing */
1515 /* FIXME, maybe NEWSEGMENT might need synchronisation
1516 * since the POSITION query depends on accumulated times and
1517 * we cannot accumulate the current segment before the previous
1524 /* else do buffer sync code */
1525 buffer = GST_BUFFER_CAST (obj);
1527 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1529 /* just get the times to see if we need syncing */
1530 if (bclass->get_times)
1531 bclass->get_times (basesink, buffer, &start, &stop);
1534 gst_base_sink_get_times (basesink, buffer, &start, &stop);
1540 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
1541 ", stop: %" GST_TIME_FORMAT ", do_sync %d", GST_TIME_ARGS (start),
1542 GST_TIME_ARGS (stop), *do_sync);
1544 /* collect segment and format for code clarity */
1545 format = segment->format;
1547 /* no timestamp clipping if we did not * get a TIME segment format */
1548 if (G_UNLIKELY (format != GST_FORMAT_TIME)) {
1551 /* do running and stream time in TIME format */
1552 format = GST_FORMAT_TIME;
1557 if (G_UNLIKELY (!gst_segment_clip (segment, GST_FORMAT_TIME,
1558 (gint64) start, (gint64) stop, &cstart, &cstop)))
1559 goto out_of_segment;
1561 if (G_UNLIKELY (start != cstart || stop != cstop)) {
1562 GST_DEBUG_OBJECT (basesink, "clipped to: start %" GST_TIME_FORMAT
1563 ", stop: %" GST_TIME_FORMAT, GST_TIME_ARGS (cstart),
1564 GST_TIME_ARGS (cstop));
1567 /* set last stop position */
1568 if (G_LIKELY (cstop != GST_CLOCK_TIME_NONE))
1569 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, cstop);
1571 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, cstart);
1574 /* this can produce wrong values if we accumulated non-TIME segments. If this happens,
1575 * upstream is behaving very badly */
1576 sstart = gst_segment_to_stream_time (segment, format, cstart);
1577 sstop = gst_segment_to_stream_time (segment, format, cstop);
1578 rstart = gst_segment_to_running_time (segment, format, cstart);
1579 rstop = gst_segment_to_running_time (segment, format, cstop);
1588 /* buffers and EOS always need syncing and preroll */
1594 /* should not happen since we clip them in the chain function already,
1595 * we return FALSE so that we don't try to sync on it. */
1596 GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
1597 (NULL), ("unexpected buffer out of segment found."));
1598 GST_LOG_OBJECT (basesink, "buffer skipped, not in segment");
1603 /* with STREAM_LOCK, PREROLL_LOCK, LOCK
1604 * adjust a timestamp with the latency and timestamp offset */
1606 gst_base_sink_adjust_time (GstBaseSink * basesink, GstClockTime time)
1608 GstClockTimeDiff ts_offset;
1610 /* don't do anything funny with invalid timestamps */
1611 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
1614 time += basesink->priv->latency;
1616 /* apply offset, be carefull for underflows */
1617 ts_offset = basesink->priv->ts_offset;
1618 if (ts_offset < 0) {
1619 ts_offset = -ts_offset;
1620 if (ts_offset < time)
1631 * gst_base_sink_wait_clock:
1633 * @time: the running_time to be reached
1634 * @jitter: the jitter to be filled with time diff (can be NULL)
1636 * This function will block until @time is reached. It is usually called by
1637 * subclasses that use their own internal synchronisation.
1639 * If @time is not valid, no sycnhronisation is done and #GST_CLOCK_BADTIME is
1640 * returned. Likewise, if synchronisation is disabled in the element or there
1641 * is no clock, no synchronisation is done and #GST_CLOCK_BADTIME is returned.
1643 * This function should only be called with the PREROLL_LOCK held, like when
1644 * receiving an EOS event in the ::event vmethod or when receiving a buffer in
1645 * the ::render vmethod.
1647 * The @time argument should be the running_time of when this method should
1648 * return and is not adjusted with any latency or offset configured in the
1653 * Returns: #GstClockReturn
1656 gst_base_sink_wait_clock (GstBaseSink * sink, GstClockTime time,
1657 GstClockTimeDiff * jitter)
1663 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
1666 GST_OBJECT_LOCK (sink);
1667 if (G_UNLIKELY (!sink->sync))
1670 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (sink)) == NULL))
1673 /* add base_time to running_time to get the time against the clock */
1674 time += GST_ELEMENT_CAST (sink)->base_time;
1676 id = gst_clock_new_single_shot_id (clock, time);
1677 GST_OBJECT_UNLOCK (sink);
1679 /* A blocking wait is performed on the clock. We save the ClockID
1680 * so we can unlock the entry at any time. While we are blocking, we
1681 * release the PREROLL_LOCK so that other threads can interrupt the
1683 sink->clock_id = id;
1684 /* release the preroll lock while waiting */
1685 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
1687 ret = gst_clock_id_wait (id, jitter);
1689 GST_PAD_PREROLL_LOCK (sink->sinkpad);
1690 gst_clock_id_unref (id);
1691 sink->clock_id = NULL;
1695 /* no syncing needed */
1698 GST_DEBUG_OBJECT (sink, "time not valid, no sync needed");
1699 return GST_CLOCK_BADTIME;
1703 GST_DEBUG_OBJECT (sink, "sync disabled");
1704 GST_OBJECT_UNLOCK (sink);
1705 return GST_CLOCK_BADTIME;
1709 GST_DEBUG_OBJECT (sink, "no clock, can't sync");
1710 GST_OBJECT_UNLOCK (sink);
1711 return GST_CLOCK_BADTIME;
1716 * gst_base_sink_wait_preroll:
1719 * If the #GstBaseSinkClass::render method performs its own synchronisation against
1720 * the clock it must unblock when going from PLAYING to the PAUSED state and call
1721 * this method before continuing to render the remaining data.
1723 * This function will block until a state change to PLAYING happens (in which
1724 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
1725 * to a state change to READY or a FLUSH event (in which case this function
1726 * returns #GST_FLOW_WRONG_STATE).
1728 * This function should only be called with the PREROLL_LOCK held, like in the
1733 * Returns: #GST_FLOW_OK if the preroll completed and processing can
1734 * continue. Any other return value should be returned from the render vmethod.
1737 gst_base_sink_wait_preroll (GstBaseSink * sink)
1739 sink->have_preroll = TRUE;
1740 GST_DEBUG_OBJECT (sink, "waiting in preroll for flush or PLAYING");
1741 /* block until the state changes, or we get a flush, or something */
1742 GST_PAD_PREROLL_WAIT (sink->sinkpad);
1743 sink->have_preroll = FALSE;
1744 if (G_UNLIKELY (sink->flushing))
1746 GST_DEBUG_OBJECT (sink, "continue after preroll");
1753 GST_DEBUG_OBJECT (sink, "preroll interrupted");
1754 return GST_FLOW_WRONG_STATE;
1759 * gst_base_sink_do_preroll:
1761 * @obj: the object that caused the preroll
1763 * If the @sink spawns its own thread for pulling buffers from upstream it
1764 * should call this method after it has pulled a buffer. If the element needed
1765 * to preroll, this function will perform the preroll and will then block
1766 * until the element state is changed.
1768 * This function should be called with the PREROLL_LOCK held.
1772 * Returns: #GST_FLOW_OK if the preroll completed and processing can
1773 * continue. Any other return value should be returned from the render vmethod.
1776 gst_base_sink_do_preroll (GstBaseSink * sink, GstMiniObject * obj)
1780 while (G_UNLIKELY (sink->need_preroll)) {
1781 GST_DEBUG_OBJECT (sink, "prerolling object %p", obj);
1783 ret = gst_base_sink_preroll_object (sink, obj);
1784 if (ret != GST_FLOW_OK)
1785 goto preroll_failed;
1787 /* need to recheck here because the commit state could have
1788 * made us not need the preroll anymore */
1789 if (G_LIKELY (sink->need_preroll)) {
1790 /* block until the state changes, or we get a flush, or something */
1791 ret = gst_base_sink_wait_preroll (sink);
1792 if (ret != GST_FLOW_OK)
1793 goto preroll_failed;
1801 GST_DEBUG_OBJECT (sink, "preroll failed %d", ret);
1807 * gst_base_sink_wait_eos:
1809 * @time: the running_time to be reached
1810 * @jitter: the jitter to be filled with time diff (can be NULL)
1812 * This function will block until @time is reached. It is usually called by
1813 * subclasses that use their own internal synchronisation but want to let the
1814 * EOS be handled by the base class.
1816 * This function should only be called with the PREROLL_LOCK held, like when
1817 * receiving an EOS event in the ::event vmethod.
1819 * The @time argument should be the running_time of when the EOS should happen
1820 * and will be adjusted with any latency and offset configured in the sink.
1824 * Returns: #GstFlowReturn
1827 gst_base_sink_wait_eos (GstBaseSink * sink, GstClockTime time,
1828 GstClockTimeDiff * jitter)
1830 GstClockReturn status;
1836 GST_DEBUG_OBJECT (sink, "checking preroll");
1838 /* first wait for the playing state before we can continue */
1839 if (G_UNLIKELY (sink->need_preroll)) {
1840 ret = gst_base_sink_wait_preroll (sink);
1841 if (ret != GST_FLOW_OK)
1845 /* preroll done, we can sync since we are in PLAYING now. */
1846 GST_DEBUG_OBJECT (sink, "possibly waiting for clock to reach %"
1847 GST_TIME_FORMAT, GST_TIME_ARGS (time));
1849 /* compensate for latency and ts_offset. We don't adjust for render delay
1850 * because we don't interact with the device on EOS normally. */
1851 stime = gst_base_sink_adjust_time (sink, time);
1853 /* wait for the clock, this can be interrupted because we got shut down or
1855 status = gst_base_sink_wait_clock (sink, stime, jitter);
1857 GST_DEBUG_OBJECT (sink, "clock returned %d", status);
1859 /* invalid time, no clock or sync disabled, just continue then */
1860 if (status == GST_CLOCK_BADTIME)
1863 /* waiting could have been interrupted and we can be flushing now */
1864 if (G_UNLIKELY (sink->flushing))
1867 /* retry if we got unscheduled, which means we did not reach the timeout
1868 * yet. if some other error occures, we continue. */
1869 } while (status == GST_CLOCK_UNSCHEDULED);
1871 GST_DEBUG_OBJECT (sink, "end of stream");
1878 GST_DEBUG_OBJECT (sink, "we are flushing");
1879 return GST_FLOW_WRONG_STATE;
1883 /* with STREAM_LOCK, PREROLL_LOCK
1885 * Make sure we are in PLAYING and synchronize an object to the clock.
1887 * If we need preroll, we are not in PLAYING. We try to commit the state
1888 * if needed and then block if we still are not PLAYING.
1890 * We start waiting on the clock in PLAYING. If we got interrupted, we
1891 * immediatly try to re-preroll.
1893 * Some objects do not need synchronisation (most events) and so this function
1894 * immediatly returns GST_FLOW_OK.
1896 * for objects that arrive later than max-lateness to be synchronized to the
1897 * clock have the @late boolean set to TRUE.
1899 * This function keeps a running average of the jitter (the diff between the
1900 * clock time and the requested sync time). The jitter is negative for
1901 * objects that arrive in time and positive for late buffers.
1903 * does not take ownership of obj.
1905 static GstFlowReturn
1906 gst_base_sink_do_sync (GstBaseSink * basesink, GstPad * pad,
1907 GstMiniObject * obj, gboolean * late)
1909 GstClockTimeDiff jitter;
1911 GstClockReturn status = GST_CLOCK_OK;
1912 GstClockTime rstart, rstop, sstart, sstop, stime;
1914 GstBaseSinkPrivate *priv;
1917 priv = basesink->priv;
1919 sstart = sstop = rstart = rstop = -1;
1922 priv->current_rstart = -1;
1924 /* get timing information for this object against the render segment */
1925 syncable = gst_base_sink_get_sync_times (basesink, obj,
1926 &sstart, &sstop, &rstart, &rstop, &do_sync, &basesink->segment);
1928 /* a syncable object needs to participate in preroll and
1929 * clocking. All buffers and EOS are syncable. */
1930 if (G_UNLIKELY (!syncable))
1933 /* store timing info for current object */
1934 priv->current_rstart = rstart;
1935 priv->current_rstop = (rstop != -1 ? rstop : rstart);
1936 /* save sync time for eos when the previous object needed sync */
1937 priv->eos_rtime = (do_sync ? priv->current_rstop : -1);
1940 /* first do preroll, this makes sure we commit our state
1941 * to PAUSED and can continue to PLAYING. We cannot perform
1942 * any clock sync in PAUSED because there is no clock.
1944 ret = gst_base_sink_do_preroll (basesink, obj);
1945 if (G_UNLIKELY (ret != GST_FLOW_OK))
1946 goto preroll_failed;
1948 /* After rendering we store the position of the last buffer so that we can use
1949 * it to report the position. We need to take the lock here. */
1950 GST_OBJECT_LOCK (basesink);
1951 priv->current_sstart = sstart;
1952 priv->current_sstop = (sstop != -1 ? sstop : sstart);
1953 GST_OBJECT_UNLOCK (basesink);
1958 /* adjust for latency */
1959 stime = gst_base_sink_adjust_time (basesink, rstart);
1961 /* adjust for render-delay, avoid underflows */
1963 if (stime > priv->render_delay)
1964 stime -= priv->render_delay;
1969 /* preroll done, we can sync since we are in PLAYING now. */
1970 GST_DEBUG_OBJECT (basesink, "possibly waiting for clock to reach %"
1971 GST_TIME_FORMAT ", adjusted %" GST_TIME_FORMAT,
1972 GST_TIME_ARGS (rstart), GST_TIME_ARGS (stime));
1974 /* This function will return immediatly if start == -1, no clock
1975 * or sync is disabled with GST_CLOCK_BADTIME. */
1976 status = gst_base_sink_wait_clock (basesink, stime, &jitter);
1978 GST_DEBUG_OBJECT (basesink, "clock returned %d", status);
1980 /* invalid time, no clock or sync disabled, just render */
1981 if (status == GST_CLOCK_BADTIME)
1984 /* waiting could have been interrupted and we can be flushing now */
1985 if (G_UNLIKELY (basesink->flushing))
1988 /* check for unlocked by a state change, we are not flushing so
1989 * we can try to preroll on the current buffer. */
1990 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
1991 GST_DEBUG_OBJECT (basesink, "unscheduled, waiting some more");
1992 priv->call_preroll = TRUE;
1996 /* successful syncing done, record observation */
1997 priv->current_jitter = jitter;
1999 /* check if the object should be dropped */
2000 *late = gst_base_sink_is_too_late (basesink, obj, rstart, rstop,
2009 GST_DEBUG_OBJECT (basesink, "non syncable object %p", obj);
2014 GST_DEBUG_OBJECT (basesink, "we are flushing");
2015 return GST_FLOW_WRONG_STATE;
2019 GST_DEBUG_OBJECT (basesink, "preroll failed");
2025 gst_base_sink_send_qos (GstBaseSink * basesink,
2026 gdouble proportion, GstClockTime time, GstClockTimeDiff diff)
2031 /* generate Quality-of-Service event */
2032 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2033 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2034 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (time));
2036 event = gst_event_new_qos (proportion, diff, time);
2039 res = gst_pad_push_event (basesink->sinkpad, event);
2045 gst_base_sink_perform_qos (GstBaseSink * sink, gboolean dropped)
2047 GstBaseSinkPrivate *priv;
2048 GstClockTime start, stop;
2049 GstClockTimeDiff jitter;
2050 GstClockTime pt, entered, left;
2051 GstClockTime duration;
2056 start = priv->current_rstart;
2058 /* if Quality-of-Service disabled, do nothing */
2059 if (!g_atomic_int_get (&priv->qos_enabled) || start == -1)
2062 stop = priv->current_rstop;
2063 jitter = priv->current_jitter;
2066 /* this is the time the buffer entered the sink */
2067 if (start < -jitter)
2070 entered = start + jitter;
2073 /* this is the time the buffer entered the sink */
2074 entered = start + jitter;
2075 /* this is the time the buffer left the sink */
2076 left = start + jitter;
2079 /* calculate duration of the buffer */
2081 duration = stop - start;
2085 /* if we have the time when the last buffer left us, calculate
2086 * processing time */
2087 if (priv->last_left != -1) {
2088 if (entered > priv->last_left) {
2089 pt = entered - priv->last_left;
2097 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "start: %" GST_TIME_FORMAT
2098 ", entered %" GST_TIME_FORMAT ", left %" GST_TIME_FORMAT ", pt: %"
2099 GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT ",jitter %"
2100 G_GINT64_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (entered),
2101 GST_TIME_ARGS (left), GST_TIME_ARGS (pt), GST_TIME_ARGS (duration),
2104 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "avg_duration: %" GST_TIME_FORMAT
2105 ", avg_pt: %" GST_TIME_FORMAT ", avg_rate: %g",
2106 GST_TIME_ARGS (priv->avg_duration), GST_TIME_ARGS (priv->avg_pt),
2109 /* collect running averages. for first observations, we copy the
2111 if (priv->avg_duration == -1)
2112 priv->avg_duration = duration;
2114 priv->avg_duration = UPDATE_RUNNING_AVG (priv->avg_duration, duration);
2116 if (priv->avg_pt == -1)
2119 priv->avg_pt = UPDATE_RUNNING_AVG (priv->avg_pt, pt);
2121 if (priv->avg_duration != 0)
2123 gst_guint64_to_gdouble (priv->avg_pt) /
2124 gst_guint64_to_gdouble (priv->avg_duration);
2128 if (priv->last_left != -1) {
2129 if (dropped || priv->avg_rate < 0.0) {
2130 priv->avg_rate = rate;
2133 priv->avg_rate = UPDATE_RUNNING_AVG_N (priv->avg_rate, rate);
2135 priv->avg_rate = UPDATE_RUNNING_AVG_P (priv->avg_rate, rate);
2139 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink,
2140 "updated: avg_duration: %" GST_TIME_FORMAT ", avg_pt: %" GST_TIME_FORMAT
2141 ", avg_rate: %g", GST_TIME_ARGS (priv->avg_duration),
2142 GST_TIME_ARGS (priv->avg_pt), priv->avg_rate);
2145 if (priv->avg_rate >= 0.0) {
2146 /* if we have a valid rate, start sending QoS messages */
2147 if (priv->current_jitter < 0) {
2148 /* make sure we never go below 0 when adding the jitter to the
2150 if (priv->current_rstart < -priv->current_jitter)
2151 priv->current_jitter = -priv->current_rstart;
2153 gst_base_sink_send_qos (sink, priv->avg_rate, priv->current_rstart,
2154 priv->current_jitter);
2157 /* record when this buffer will leave us */
2158 priv->last_left = left;
2161 /* reset all qos measuring */
2163 gst_base_sink_reset_qos (GstBaseSink * sink)
2165 GstBaseSinkPrivate *priv;
2169 priv->last_in_time = -1;
2170 priv->last_left = -1;
2171 priv->avg_duration = -1;
2173 priv->avg_rate = -1.0;
2174 priv->avg_render = -1;
2180 /* Checks if the object was scheduled too late.
2182 * start/stop contain the raw timestamp start and stop values
2185 * status and jitter contain the return values from the clock wait.
2187 * returns TRUE if the buffer was too late.
2190 gst_base_sink_is_too_late (GstBaseSink * basesink, GstMiniObject * obj,
2191 GstClockTime start, GstClockTime stop,
2192 GstClockReturn status, GstClockTimeDiff jitter)
2195 gint64 max_lateness;
2196 GstBaseSinkPrivate *priv;
2198 priv = basesink->priv;
2202 /* only for objects that were too late */
2203 if (G_LIKELY (status != GST_CLOCK_EARLY))
2206 max_lateness = basesink->abidata.ABI.max_lateness;
2208 /* check if frame dropping is enabled */
2209 if (max_lateness == -1)
2212 /* only check for buffers */
2213 if (G_UNLIKELY (!GST_IS_BUFFER (obj)))
2216 /* can't do check if we don't have a timestamp */
2217 if (G_UNLIKELY (start == -1))
2220 /* we can add a valid stop time */
2222 max_lateness += stop;
2224 max_lateness += start;
2226 /* if the jitter bigger than duration and lateness we are too late */
2227 if ((late = start + jitter > max_lateness)) {
2228 GST_DEBUG_OBJECT (basesink, "buffer is too late %" GST_TIME_FORMAT
2229 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (start + jitter),
2230 GST_TIME_ARGS (max_lateness));
2231 /* !!emergency!!, if we did not receive anything valid for more than a
2232 * second, render it anyway so the user sees something */
2233 if (priv->last_in_time != -1 && start - priv->last_in_time > GST_SECOND) {
2235 GST_DEBUG_OBJECT (basesink,
2236 "**emergency** last buffer at %" GST_TIME_FORMAT " > GST_SECOND",
2237 GST_TIME_ARGS (priv->last_in_time));
2243 priv->last_in_time = start;
2250 GST_DEBUG_OBJECT (basesink, "object was scheduled in time");
2255 GST_DEBUG_OBJECT (basesink, "frame dropping disabled");
2260 GST_DEBUG_OBJECT (basesink, "object is not a buffer");
2265 GST_DEBUG_OBJECT (basesink, "buffer has no timestamp");
2270 /* called before and after calling the render vmethod. It keeps track of how
2271 * much time was spent in the render method and is used to check if we are
2274 gst_base_sink_do_render_stats (GstBaseSink * basesink, gboolean start)
2276 GstBaseSinkPrivate *priv;
2278 priv = basesink->priv;
2281 priv->start = gst_util_get_timestamp ();
2283 GstClockTime elapsed;
2285 priv->stop = gst_util_get_timestamp ();
2287 elapsed = GST_CLOCK_DIFF (priv->start, priv->stop);
2289 if (priv->avg_render == -1)
2290 priv->avg_render = elapsed;
2292 priv->avg_render = UPDATE_RUNNING_AVG (priv->avg_render, elapsed);
2294 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2295 "avg_render: %" GST_TIME_FORMAT, GST_TIME_ARGS (priv->avg_render));
2299 /* with STREAM_LOCK, PREROLL_LOCK,
2301 * Synchronize the object on the clock and then render it.
2303 * takes ownership of obj.
2305 static GstFlowReturn
2306 gst_base_sink_render_object (GstBaseSink * basesink, GstPad * pad,
2307 GstMiniObject * obj)
2309 GstFlowReturn ret = GST_FLOW_OK;
2310 GstBaseSinkClass *bclass;
2311 gboolean late = FALSE;
2312 GstBaseSinkPrivate *priv;
2314 priv = basesink->priv;
2316 /* synchronize this object, non syncable objects return OK
2318 ret = gst_base_sink_do_sync (basesink, pad, obj, &late);
2319 if (G_UNLIKELY (ret != GST_FLOW_OK))
2322 /* and now render, event or buffer. */
2323 if (G_LIKELY (GST_IS_BUFFER (obj))) {
2326 /* drop late buffers unconditionally, let's hope it's unlikely */
2327 if (G_UNLIKELY (late))
2330 buf = GST_BUFFER_CAST (obj);
2332 gst_base_sink_set_last_buffer (basesink, buf);
2334 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2336 if (G_LIKELY (bclass->render)) {
2339 /* read once, to get same value before and after */
2340 do_qos = g_atomic_int_get (&priv->qos_enabled);
2342 GST_DEBUG_OBJECT (basesink, "rendering buffer %p", obj);
2344 /* record rendering time for QoS and stats */
2346 gst_base_sink_do_render_stats (basesink, TRUE);
2348 ret = bclass->render (basesink, buf);
2353 gst_base_sink_do_render_stats (basesink, FALSE);
2356 GstEvent *event = GST_EVENT_CAST (obj);
2357 gboolean event_res = TRUE;
2360 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2362 type = GST_EVENT_TYPE (event);
2364 GST_DEBUG_OBJECT (basesink, "rendering event %p, type %s", obj,
2365 gst_event_type_get_name (type));
2368 event_res = bclass->event (basesink, event);
2370 /* when we get here we could be flushing again when the event handler calls
2371 * _wait_eos(). We have to ignore this object in that case. */
2372 if (G_UNLIKELY (basesink->flushing))
2375 if (G_LIKELY (event_res)) {
2378 seqnum = basesink->priv->seqnum = gst_event_get_seqnum (event);
2379 GST_DEBUG_OBJECT (basesink, "Got seqnum #%" G_GUINT32_FORMAT, seqnum);
2384 GstMessage *message;
2386 /* the EOS event is completely handled so we mark
2387 * ourselves as being in the EOS state. eos is also
2388 * protected by the object lock so we can read it when
2389 * answering the POSITION query. */
2390 GST_OBJECT_LOCK (basesink);
2391 basesink->eos = TRUE;
2392 GST_OBJECT_UNLOCK (basesink);
2394 /* ok, now we can post the message */
2395 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
2397 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
2398 gst_message_set_seqnum (message, seqnum);
2399 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
2402 case GST_EVENT_NEWSEGMENT:
2403 /* configure the segment */
2404 gst_base_sink_configure_segment (basesink, pad, event,
2405 &basesink->segment);
2414 gst_base_sink_perform_qos (basesink, late);
2416 GST_DEBUG_OBJECT (basesink, "object unref after render %p", obj);
2417 gst_mini_object_unref (obj);
2424 GST_DEBUG_OBJECT (basesink, "do_sync returned %s", gst_flow_get_name (ret));
2430 GST_DEBUG_OBJECT (basesink, "buffer late, dropping");
2435 GST_DEBUG_OBJECT (basesink, "we are flushing, ignore object");
2436 gst_mini_object_unref (obj);
2437 return GST_FLOW_WRONG_STATE;
2441 /* with STREAM_LOCK, PREROLL_LOCK
2443 * Perform preroll on the given object. For buffers this means
2444 * calling the preroll subclass method.
2445 * If that succeeds, the state will be commited.
2447 * function does not take ownership of obj.
2449 static GstFlowReturn
2450 gst_base_sink_preroll_object (GstBaseSink * basesink, GstMiniObject * obj)
2454 GST_DEBUG_OBJECT (basesink, "prerolling object %p", obj);
2456 /* if it's a buffer, we need to call the preroll method */
2457 if (G_LIKELY (GST_IS_BUFFER (obj)) && basesink->priv->call_preroll) {
2458 GstBaseSinkClass *bclass;
2460 GstClockTime timestamp;
2462 buf = GST_BUFFER_CAST (obj);
2463 timestamp = GST_BUFFER_TIMESTAMP (buf);
2465 GST_DEBUG_OBJECT (basesink, "preroll buffer %" GST_TIME_FORMAT,
2466 GST_TIME_ARGS (timestamp));
2468 gst_base_sink_set_last_buffer (basesink, buf);
2470 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2471 if (bclass->preroll)
2472 if ((ret = bclass->preroll (basesink, buf)) != GST_FLOW_OK)
2473 goto preroll_failed;
2475 basesink->priv->call_preroll = FALSE;
2479 if (G_LIKELY (basesink->playing_async)) {
2480 if (G_UNLIKELY (!gst_base_sink_commit_state (basesink)))
2489 GST_DEBUG_OBJECT (basesink, "preroll failed, abort state");
2490 gst_element_abort_state (GST_ELEMENT_CAST (basesink));
2495 GST_DEBUG_OBJECT (basesink, "stopping while commiting state");
2496 return GST_FLOW_WRONG_STATE;
2500 /* with STREAM_LOCK, PREROLL_LOCK
2502 * Queue an object for rendering.
2503 * The first prerollable object queued will complete the preroll. If the
2504 * preroll queue if filled, we render all the objects in the queue.
2506 * This function takes ownership of the object.
2508 static GstFlowReturn
2509 gst_base_sink_queue_object_unlocked (GstBaseSink * basesink, GstPad * pad,
2510 GstMiniObject * obj, gboolean prerollable)
2512 GstFlowReturn ret = GST_FLOW_OK;
2516 if (G_UNLIKELY (basesink->need_preroll)) {
2517 if (G_LIKELY (prerollable))
2518 basesink->preroll_queued++;
2520 length = basesink->preroll_queued;
2522 GST_DEBUG_OBJECT (basesink, "now %d prerolled items", length);
2524 /* first prerollable item needs to finish the preroll */
2526 ret = gst_base_sink_preroll_object (basesink, obj);
2527 if (G_UNLIKELY (ret != GST_FLOW_OK))
2528 goto preroll_failed;
2530 /* need to recheck if we need preroll, commmit state during preroll
2531 * could have made us not need more preroll. */
2532 if (G_UNLIKELY (basesink->need_preroll)) {
2533 /* see if we can render now, if we can't add the object to the preroll
2535 if (G_UNLIKELY (length <= basesink->preroll_queue_max_len))
2540 /* we can start rendering (or blocking) the queued object
2542 q = basesink->preroll_queue;
2543 while (G_UNLIKELY (!g_queue_is_empty (q))) {
2546 o = g_queue_pop_head (q);
2547 GST_DEBUG_OBJECT (basesink, "rendering queued object %p", o);
2549 /* do something with the return value */
2550 ret = gst_base_sink_render_object (basesink, pad, o);
2551 if (ret != GST_FLOW_OK)
2552 goto dequeue_failed;
2555 /* now render the object */
2556 ret = gst_base_sink_render_object (basesink, pad, obj);
2557 basesink->preroll_queued = 0;
2564 GST_DEBUG_OBJECT (basesink, "preroll failed, reason %s",
2565 gst_flow_get_name (ret));
2566 gst_mini_object_unref (obj);
2571 /* add object to the queue and return */
2572 GST_DEBUG_OBJECT (basesink, "need more preroll data %d <= %d",
2573 length, basesink->preroll_queue_max_len);
2574 g_queue_push_tail (basesink->preroll_queue, obj);
2579 GST_DEBUG_OBJECT (basesink, "rendering queued objects failed, reason %s",
2580 gst_flow_get_name (ret));
2581 gst_mini_object_unref (obj);
2588 * This function grabs the PREROLL_LOCK and adds the object to
2591 * This function takes ownership of obj.
2593 static GstFlowReturn
2594 gst_base_sink_queue_object (GstBaseSink * basesink, GstPad * pad,
2595 GstMiniObject * obj, gboolean prerollable)
2599 GST_PAD_PREROLL_LOCK (pad);
2600 if (G_UNLIKELY (basesink->flushing))
2603 if (G_UNLIKELY (basesink->priv->received_eos))
2606 ret = gst_base_sink_queue_object_unlocked (basesink, pad, obj, prerollable);
2607 GST_PAD_PREROLL_UNLOCK (pad);
2614 GST_DEBUG_OBJECT (basesink, "sink is flushing");
2615 GST_PAD_PREROLL_UNLOCK (pad);
2616 gst_mini_object_unref (obj);
2617 return GST_FLOW_WRONG_STATE;
2621 GST_DEBUG_OBJECT (basesink,
2622 "we are EOS, dropping object, return UNEXPECTED");
2623 GST_PAD_PREROLL_UNLOCK (pad);
2624 gst_mini_object_unref (obj);
2625 return GST_FLOW_UNEXPECTED;
2630 gst_base_sink_flush_start (GstBaseSink * basesink, GstPad * pad)
2632 /* make sure we are not blocked on the clock also clear any pending
2634 gst_base_sink_set_flushing (basesink, pad, TRUE);
2636 /* we grab the stream lock but that is not needed since setting the
2637 * sink to flushing would make sure no state commit is being done
2639 GST_PAD_STREAM_LOCK (pad);
2640 gst_base_sink_reset_qos (basesink);
2641 if (basesink->priv->async_enabled) {
2642 /* and we need to commit our state again on the next
2643 * prerolled buffer */
2644 basesink->playing_async = TRUE;
2645 gst_element_lost_state (GST_ELEMENT_CAST (basesink));
2647 basesink->priv->have_latency = TRUE;
2648 basesink->need_preroll = FALSE;
2650 gst_base_sink_set_last_buffer (basesink, NULL);
2651 GST_PAD_STREAM_UNLOCK (pad);
2655 gst_base_sink_flush_stop (GstBaseSink * basesink, GstPad * pad)
2657 /* unset flushing so we can accept new data, this also flushes out any EOS
2659 gst_base_sink_set_flushing (basesink, pad, FALSE);
2661 /* for position reporting */
2662 GST_OBJECT_LOCK (basesink);
2663 basesink->priv->current_sstart = -1;
2664 basesink->priv->current_sstop = -1;
2665 basesink->priv->eos_rtime = -1;
2666 basesink->priv->call_preroll = TRUE;
2667 if (basesink->pad_mode == GST_ACTIVATE_PUSH) {
2668 /* we need new segment info after the flush. */
2669 basesink->have_newsegment = FALSE;
2670 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
2671 gst_segment_init (basesink->abidata.ABI.clip_segment, GST_FORMAT_UNDEFINED);
2673 GST_OBJECT_UNLOCK (basesink);
2677 gst_base_sink_event (GstPad * pad, GstEvent * event)
2679 GstBaseSink *basesink;
2680 gboolean result = TRUE;
2681 GstBaseSinkClass *bclass;
2683 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
2685 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2687 GST_DEBUG_OBJECT (basesink, "event %p (%s)", event,
2688 GST_EVENT_TYPE_NAME (event));
2690 switch (GST_EVENT_TYPE (event)) {
2695 GST_PAD_PREROLL_LOCK (pad);
2696 if (G_UNLIKELY (basesink->flushing))
2699 if (G_UNLIKELY (basesink->priv->received_eos)) {
2700 /* we can't accept anything when we are EOS */
2702 gst_event_unref (event);
2704 /* we set the received EOS flag here so that we can use it when testing if
2705 * we are prerolled and to refuse more buffers. */
2706 basesink->priv->received_eos = TRUE;
2708 /* EOS is a prerollable object, we call the unlocked version because it
2709 * does not check the received_eos flag. */
2710 ret = gst_base_sink_queue_object_unlocked (basesink, pad,
2711 GST_MINI_OBJECT_CAST (event), TRUE);
2712 if (G_UNLIKELY (ret != GST_FLOW_OK))
2715 GST_PAD_PREROLL_UNLOCK (pad);
2718 case GST_EVENT_NEWSEGMENT:
2722 GST_DEBUG_OBJECT (basesink, "newsegment %p", event);
2724 GST_PAD_PREROLL_LOCK (pad);
2725 if (G_UNLIKELY (basesink->flushing))
2728 if (G_UNLIKELY (basesink->priv->received_eos)) {
2729 /* we can't accept anything when we are EOS */
2731 gst_event_unref (event);
2733 /* the new segment is a non prerollable item and does not block anything,
2734 * we need to configure the current clipping segment and insert the event
2735 * in the queue to serialize it with the buffers for rendering. */
2736 gst_base_sink_configure_segment (basesink, pad, event,
2737 basesink->abidata.ABI.clip_segment);
2740 gst_base_sink_queue_object_unlocked (basesink, pad,
2741 GST_MINI_OBJECT_CAST (event), FALSE);
2742 if (G_UNLIKELY (ret != GST_FLOW_OK))
2745 GST_OBJECT_LOCK (basesink);
2746 basesink->have_newsegment = TRUE;
2747 GST_OBJECT_UNLOCK (basesink);
2750 GST_PAD_PREROLL_UNLOCK (pad);
2753 case GST_EVENT_FLUSH_START:
2755 bclass->event (basesink, event);
2757 GST_DEBUG_OBJECT (basesink, "flush-start %p", event);
2759 gst_base_sink_flush_start (basesink, pad);
2761 gst_event_unref (event);
2763 case GST_EVENT_FLUSH_STOP:
2765 bclass->event (basesink, event);
2767 GST_DEBUG_OBJECT (basesink, "flush-stop %p", event);
2769 gst_base_sink_flush_stop (basesink, pad);
2771 gst_event_unref (event);
2774 /* other events are sent to queue or subclass depending on if they
2775 * are serialized. */
2776 if (GST_EVENT_IS_SERIALIZED (event)) {
2777 gst_base_sink_queue_object (basesink, pad,
2778 GST_MINI_OBJECT_CAST (event), FALSE);
2781 bclass->event (basesink, event);
2782 gst_event_unref (event);
2787 gst_object_unref (basesink);
2794 GST_DEBUG_OBJECT (basesink, "we are flushing");
2795 GST_PAD_PREROLL_UNLOCK (pad);
2797 gst_event_unref (event);
2802 /* default implementation to calculate the start and end
2803 * timestamps on a buffer, subclasses can override
2806 gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
2807 GstClockTime * start, GstClockTime * end)
2809 GstClockTime timestamp, duration;
2811 timestamp = GST_BUFFER_TIMESTAMP (buffer);
2812 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
2814 /* get duration to calculate end time */
2815 duration = GST_BUFFER_DURATION (buffer);
2816 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2817 *end = timestamp + duration;
2823 /* must be called with PREROLL_LOCK */
2825 gst_base_sink_needs_preroll (GstBaseSink * basesink)
2827 gboolean is_prerolled, res;
2829 /* we have 2 cases where the PREROLL_LOCK is released:
2830 * 1) we are blocking in the PREROLL_LOCK and thus are prerolled.
2831 * 2) we are syncing on the clock
2833 is_prerolled = basesink->have_preroll || basesink->priv->received_eos;
2834 res = !is_prerolled;
2836 GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d => needs preroll: %d",
2837 basesink->have_preroll, basesink->priv->received_eos, res);
2842 /* with STREAM_LOCK, PREROLL_LOCK
2844 * Takes a buffer and compare the timestamps with the last segment.
2845 * If the buffer falls outside of the segment boundaries, drop it.
2846 * Else queue the buffer for preroll and rendering.
2848 * This function takes ownership of the buffer.
2850 static GstFlowReturn
2851 gst_base_sink_chain_unlocked (GstBaseSink * basesink, GstPad * pad,
2854 GstBaseSinkClass *bclass;
2855 GstFlowReturn result;
2856 GstClockTime start = GST_CLOCK_TIME_NONE, end = GST_CLOCK_TIME_NONE;
2857 GstSegment *clip_segment;
2859 if (G_UNLIKELY (basesink->flushing))
2862 if (G_UNLIKELY (basesink->priv->received_eos))
2865 /* for code clarity */
2866 clip_segment = basesink->abidata.ABI.clip_segment;
2868 if (G_UNLIKELY (!basesink->have_newsegment)) {
2871 sync = gst_base_sink_get_sync (basesink);
2873 GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
2874 (_("Internal data flow problem.")),
2875 ("Received buffer without a new-segment. Assuming timestamps start from 0."));
2878 /* this means this sink will assume timestamps start from 0 */
2879 GST_OBJECT_LOCK (basesink);
2880 clip_segment->start = 0;
2881 clip_segment->stop = -1;
2882 basesink->segment.start = 0;
2883 basesink->segment.stop = -1;
2884 basesink->have_newsegment = TRUE;
2885 GST_OBJECT_UNLOCK (basesink);
2888 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2890 /* check if the buffer needs to be dropped, we first ask the subclass for the
2892 if (bclass->get_times)
2893 bclass->get_times (basesink, buf, &start, &end);
2896 /* if the subclass does not want sync, we use our own values so that we at
2897 * least clip the buffer to the segment */
2898 gst_base_sink_get_times (basesink, buf, &start, &end);
2901 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
2902 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
2904 /* a dropped buffer does not participate in anything */
2905 if (GST_CLOCK_TIME_IS_VALID (start) &&
2906 (clip_segment->format == GST_FORMAT_TIME)) {
2907 if (G_UNLIKELY (!gst_segment_clip (clip_segment,
2908 GST_FORMAT_TIME, (gint64) start, (gint64) end, NULL, NULL)))
2909 goto out_of_segment;
2912 /* now we can process the buffer in the queue, this function takes ownership
2914 result = gst_base_sink_queue_object_unlocked (basesink, pad,
2915 GST_MINI_OBJECT_CAST (buf), TRUE);
2922 GST_DEBUG_OBJECT (basesink, "sink is flushing");
2923 gst_buffer_unref (buf);
2924 return GST_FLOW_WRONG_STATE;
2928 GST_DEBUG_OBJECT (basesink,
2929 "we are EOS, dropping object, return UNEXPECTED");
2930 gst_buffer_unref (buf);
2931 return GST_FLOW_UNEXPECTED;
2935 GST_DEBUG_OBJECT (basesink, "dropping buffer, out of clipping segment");
2936 gst_buffer_unref (buf);
2943 static GstFlowReturn
2944 gst_base_sink_chain (GstPad * pad, GstBuffer * buf)
2946 GstBaseSink *basesink;
2947 GstFlowReturn result;
2949 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
2951 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH))
2954 GST_PAD_PREROLL_LOCK (pad);
2955 result = gst_base_sink_chain_unlocked (basesink, pad, buf);
2956 GST_PAD_PREROLL_UNLOCK (pad);
2964 GST_OBJECT_LOCK (pad);
2965 GST_WARNING_OBJECT (basesink,
2966 "Push on pad %s:%s, but it was not activated in push mode",
2967 GST_DEBUG_PAD_NAME (pad));
2968 GST_OBJECT_UNLOCK (pad);
2969 gst_buffer_unref (buf);
2970 /* we don't post an error message this will signal to the peer
2971 * pushing that EOS is reached. */
2972 result = GST_FLOW_UNEXPECTED;
2978 gst_base_sink_default_do_seek (GstBaseSink * sink, GstSegment * segment)
2980 gboolean res = TRUE;
2982 /* update our offset if the start/stop position was updated */
2983 if (segment->format == GST_FORMAT_BYTES) {
2984 segment->time = segment->start;
2985 } else if (segment->start == 0) {
2986 /* seek to start, we can implement a default for this. */
2990 GST_INFO_OBJECT (sink, "Can't do a default seek");
2996 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
2999 gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
3000 GstEvent * event, GstSegment * segment)
3002 /* By default, we try one of 2 things:
3003 * - For absolute seek positions, convert the requested position to our
3004 * configured processing format and place it in the output segment \
3005 * - For relative seek positions, convert our current (input) values to the
3006 * seek format, adjust by the relative seek offset and then convert back to
3007 * the processing format
3009 GstSeekType cur_type, stop_type;
3012 GstFormat seek_format, dest_format;
3015 gboolean res = TRUE;
3017 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3018 &cur_type, &cur, &stop_type, &stop);
3019 dest_format = segment->format;
3021 if (seek_format == dest_format) {
3022 gst_segment_set_seek (segment, rate, seek_format, flags,
3023 cur_type, cur, stop_type, stop, &update);
3027 if (cur_type != GST_SEEK_TYPE_NONE) {
3028 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3030 gst_pad_query_convert (sink->sinkpad, seek_format, cur, &dest_format,
3032 cur_type = GST_SEEK_TYPE_SET;
3035 if (res && stop_type != GST_SEEK_TYPE_NONE) {
3036 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3038 gst_pad_query_convert (sink->sinkpad, seek_format, stop, &dest_format,
3040 stop_type = GST_SEEK_TYPE_SET;
3043 /* And finally, configure our output segment in the desired format */
3044 gst_segment_set_seek (segment, rate, dest_format, flags, cur_type, cur,
3045 stop_type, stop, &update);
3054 GST_DEBUG_OBJECT (sink, "undefined format given, seek aborted.");
3059 /* perform a seek, only executed in pull mode */
3061 gst_base_sink_perform_seek (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3065 GstFormat seek_format, dest_format;
3067 GstSeekType cur_type, stop_type;
3068 gboolean seekseg_configured = FALSE;
3070 gboolean update, res = TRUE;
3071 GstSegment seeksegment;
3073 dest_format = sink->segment.format;
3076 GST_DEBUG_OBJECT (sink, "performing seek with event %p", event);
3077 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3078 &cur_type, &cur, &stop_type, &stop);
3080 flush = flags & GST_SEEK_FLAG_FLUSH;
3082 GST_DEBUG_OBJECT (sink, "performing seek without event");
3087 GST_DEBUG_OBJECT (sink, "flushing upstream");
3088 gst_pad_push_event (pad, gst_event_new_flush_start ());
3089 gst_base_sink_flush_start (sink, pad);
3091 GST_DEBUG_OBJECT (sink, "pausing pulling thread");
3094 GST_PAD_STREAM_LOCK (pad);
3096 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
3097 * copy the current segment info into the temp segment that we can actually
3098 * attempt the seek with. We only update the real segment if the seek suceeds. */
3099 if (!seekseg_configured) {
3100 memcpy (&seeksegment, &sink->segment, sizeof (GstSegment));
3102 /* now configure the final seek segment */
3104 if (sink->segment.format != seek_format) {
3105 /* OK, here's where we give the subclass a chance to convert the relative
3106 * seek into an absolute one in the processing format. We set up any
3107 * absolute seek above, before taking the stream lock. */
3108 if (!gst_base_sink_default_prepare_seek_segment (sink, event,
3110 GST_DEBUG_OBJECT (sink,
3111 "Preparing the seek failed after flushing. " "Aborting seek");
3115 /* The seek format matches our processing format, no need to ask the
3116 * the subclass to configure the segment. */
3117 gst_segment_set_seek (&seeksegment, rate, seek_format, flags,
3118 cur_type, cur, stop_type, stop, &update);
3121 /* Else, no seek event passed, so we're just (re)starting the
3126 GST_DEBUG_OBJECT (sink, "segment configured from %" G_GINT64_FORMAT
3127 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
3128 seeksegment.start, seeksegment.stop, seeksegment.last_stop);
3130 /* do the seek, segment.last_stop contains the new position. */
3131 res = gst_base_sink_default_do_seek (sink, &seeksegment);
3136 GST_DEBUG_OBJECT (sink, "stop flushing upstream");
3137 gst_pad_push_event (pad, gst_event_new_flush_stop ());
3138 gst_base_sink_flush_stop (sink, pad);
3139 } else if (res && sink->abidata.ABI.running) {
3140 /* we are running the current segment and doing a non-flushing seek,
3141 * close the segment first based on the last_stop. */
3142 GST_DEBUG_OBJECT (sink, "closing running segment %" G_GINT64_FORMAT
3143 " to %" G_GINT64_FORMAT, sink->segment.start, sink->segment.last_stop);
3146 /* The subclass must have converted the segment to the processing format
3148 if (res && seeksegment.format != dest_format) {
3149 GST_DEBUG_OBJECT (sink, "Subclass failed to prepare a seek segment "
3150 "in the correct format. Aborting seek.");
3154 /* if successfull seek, we update our real segment and push
3155 * out the new segment. */
3157 memcpy (&sink->segment, &seeksegment, sizeof (GstSegment));
3159 if (sink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3160 gst_element_post_message (GST_ELEMENT (sink),
3161 gst_message_new_segment_start (GST_OBJECT (sink),
3162 sink->segment.format, sink->segment.last_stop));
3166 sink->priv->discont = TRUE;
3167 sink->abidata.ABI.running = TRUE;
3169 GST_PAD_STREAM_UNLOCK (pad);
3177 gst_base_sink_loop (GstPad * pad)
3179 GstBaseSink *basesink;
3180 GstBuffer *buf = NULL;
3181 GstFlowReturn result;
3185 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
3187 g_assert (basesink->pad_mode == GST_ACTIVATE_PULL);
3189 if ((blocksize = basesink->priv->blocksize) == 0)
3192 offset = basesink->segment.last_stop;
3194 GST_DEBUG_OBJECT (basesink, "pulling %" G_GUINT64_FORMAT ", %u",
3197 result = gst_pad_pull_range (pad, offset, blocksize, &buf);
3198 if (G_UNLIKELY (result != GST_FLOW_OK))
3201 if (G_UNLIKELY (buf == NULL))
3204 offset += GST_BUFFER_SIZE (buf);
3206 gst_segment_set_last_stop (&basesink->segment, GST_FORMAT_BYTES, offset);
3208 GST_PAD_PREROLL_LOCK (pad);
3209 result = gst_base_sink_chain_unlocked (basesink, pad, buf);
3210 GST_PAD_PREROLL_UNLOCK (pad);
3211 if (G_UNLIKELY (result != GST_FLOW_OK))
3219 GST_LOG_OBJECT (basesink, "pausing task, reason %s",
3220 gst_flow_get_name (result));
3221 gst_pad_pause_task (pad);
3222 /* fatal errors and NOT_LINKED cause EOS */
3223 if (GST_FLOW_IS_FATAL (result) || result == GST_FLOW_NOT_LINKED) {
3224 if (result == GST_FLOW_UNEXPECTED) {
3225 /* perform EOS logic */
3226 if (basesink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3227 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3228 gst_message_new_segment_done (GST_OBJECT_CAST (basesink),
3229 basesink->segment.format, basesink->segment.last_stop));
3231 gst_base_sink_event (pad, gst_event_new_eos ());
3234 /* for fatal errors we post an error message, post the error
3235 * first so the app knows about the error first. */
3236 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
3237 (_("Internal data stream error.")),
3238 ("stream stopped, reason %s", gst_flow_get_name (result)));
3239 gst_base_sink_event (pad, gst_event_new_eos ());
3246 GST_LOG_OBJECT (basesink, "no buffer, pausing");
3247 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
3248 (_("Internal data flow error.")), ("element returned NULL buffer"));
3249 result = GST_FLOW_ERROR;
3255 gst_base_sink_set_flushing (GstBaseSink * basesink, GstPad * pad,
3258 GstBaseSinkClass *bclass;
3260 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3263 /* unlock any subclasses, we need to do this before grabbing the
3264 * PREROLL_LOCK since we hold this lock before going into ::render. */
3266 bclass->unlock (basesink);
3269 GST_PAD_PREROLL_LOCK (pad);
3270 basesink->flushing = flushing;
3272 /* step 1, now that we have the PREROLL lock, clear our unlock request */
3273 if (bclass->unlock_stop)
3274 bclass->unlock_stop (basesink);
3276 /* set need_preroll before we unblock the clock. If the clock is unblocked
3277 * before timing out, we can reuse the buffer for preroll. */
3278 basesink->need_preroll = TRUE;
3280 /* step 2, unblock clock sync (if any) or any other blocking thing */
3281 if (basesink->clock_id) {
3282 gst_clock_id_unschedule (basesink->clock_id);
3285 /* flush out the data thread if it's locked in finish_preroll, this will
3286 * also flush out the EOS state */
3287 GST_DEBUG_OBJECT (basesink,
3288 "flushing out data thread, need preroll to TRUE");
3289 gst_base_sink_preroll_queue_flush (basesink, pad);
3291 GST_PAD_PREROLL_UNLOCK (pad);
3297 gst_base_sink_default_activate_pull (GstBaseSink * basesink, gboolean active)
3303 result = gst_pad_start_task (basesink->sinkpad,
3304 (GstTaskFunction) gst_base_sink_loop, basesink->sinkpad);
3306 /* step 2, make sure streaming finishes */
3307 result = gst_pad_stop_task (basesink->sinkpad);
3314 gst_base_sink_pad_activate (GstPad * pad)
3316 gboolean result = FALSE;
3317 GstBaseSink *basesink;
3319 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3321 GST_DEBUG_OBJECT (basesink, "Trying pull mode first");
3323 gst_base_sink_set_flushing (basesink, pad, FALSE);
3325 /* we need to have the pull mode enabled */
3326 if (!basesink->can_activate_pull) {
3327 GST_DEBUG_OBJECT (basesink, "pull mode disabled");
3331 /* check if downstreams supports pull mode at all */
3332 if (!gst_pad_check_pull_range (pad)) {
3333 GST_DEBUG_OBJECT (basesink, "pull mode not supported");
3337 /* set the pad mode before starting the task so that it's in the
3338 * correct state for the new thread. also the sink set_caps and get_caps
3339 * function checks this */
3340 basesink->pad_mode = GST_ACTIVATE_PULL;
3342 /* we first try to negotiate a format so that when we try to activate
3343 * downstream, it knows about our format */
3344 if (!gst_base_sink_negotiate_pull (basesink)) {
3345 GST_DEBUG_OBJECT (basesink, "failed to negotiate in pull mode");
3349 /* ok activate now */
3350 if (!gst_pad_activate_pull (pad, TRUE)) {
3351 /* clear any pending caps */
3352 GST_OBJECT_LOCK (basesink);
3353 gst_caps_replace (&basesink->priv->pull_caps, NULL);
3354 GST_OBJECT_UNLOCK (basesink);
3355 GST_DEBUG_OBJECT (basesink, "failed to activate in pull mode");
3359 GST_DEBUG_OBJECT (basesink, "Success activating pull mode");
3363 /* push mode fallback */
3365 GST_DEBUG_OBJECT (basesink, "Falling back to push mode");
3366 if ((result = gst_pad_activate_push (pad, TRUE))) {
3367 GST_DEBUG_OBJECT (basesink, "Success activating push mode");
3372 GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
3373 gst_base_sink_set_flushing (basesink, pad, TRUE);
3376 gst_object_unref (basesink);
3382 gst_base_sink_pad_activate_push (GstPad * pad, gboolean active)
3385 GstBaseSink *basesink;
3387 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3390 if (!basesink->can_activate_push) {
3392 basesink->pad_mode = GST_ACTIVATE_NONE;
3395 basesink->pad_mode = GST_ACTIVATE_PUSH;
3398 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH)) {
3399 g_warning ("Internal GStreamer activation error!!!");
3402 gst_base_sink_set_flushing (basesink, pad, TRUE);
3404 basesink->pad_mode = GST_ACTIVATE_NONE;
3408 gst_object_unref (basesink);
3414 gst_base_sink_negotiate_pull (GstBaseSink * basesink)
3421 /* this returns the intersection between our caps and the peer caps. If there
3422 * is no peer, it returns NULL and we can't operate in pull mode so we can
3423 * fail the negotiation. */
3424 caps = gst_pad_get_allowed_caps (GST_BASE_SINK_PAD (basesink));
3425 if (caps == NULL || gst_caps_is_empty (caps))
3426 goto no_caps_possible;
3428 GST_DEBUG_OBJECT (basesink, "allowed caps: %" GST_PTR_FORMAT, caps);
3430 caps = gst_caps_make_writable (caps);
3431 /* get the first (prefered) format */
3432 gst_caps_truncate (caps);
3434 gst_pad_fixate_caps (GST_BASE_SINK_PAD (basesink), caps);
3436 GST_DEBUG_OBJECT (basesink, "fixated to: %" GST_PTR_FORMAT, caps);
3438 if (gst_caps_is_any (caps)) {
3439 GST_DEBUG_OBJECT (basesink, "caps were ANY after fixating, "
3441 /* neither side has template caps in this case, so they are prepared for
3442 pull() without setcaps() */
3444 } else if (gst_caps_is_fixed (caps)) {
3445 if (!gst_pad_set_caps (GST_BASE_SINK_PAD (basesink), caps))
3446 goto could_not_set_caps;
3448 GST_OBJECT_LOCK (basesink);
3449 gst_caps_replace (&basesink->priv->pull_caps, caps);
3450 GST_OBJECT_UNLOCK (basesink);
3455 gst_caps_unref (caps);
3461 GST_INFO_OBJECT (basesink, "Pipeline could not agree on caps");
3462 GST_DEBUG_OBJECT (basesink, "get_allowed_caps() returned EMPTY");
3464 gst_caps_unref (caps);
3469 GST_INFO_OBJECT (basesink, "Could not set caps: %" GST_PTR_FORMAT, caps);
3470 gst_caps_unref (caps);
3475 /* this won't get called until we implement an activate function */
3477 gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active)
3479 gboolean result = FALSE;
3480 GstBaseSink *basesink;
3481 GstBaseSinkClass *bclass;
3483 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3484 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3490 /* we mark we have a newsegment here because pull based
3491 * mode works just fine without having a newsegment before the
3493 format = GST_FORMAT_BYTES;
3495 gst_segment_init (&basesink->segment, format);
3496 gst_segment_init (basesink->abidata.ABI.clip_segment, format);
3497 GST_OBJECT_LOCK (basesink);
3498 basesink->have_newsegment = TRUE;
3499 GST_OBJECT_UNLOCK (basesink);
3501 /* get the peer duration in bytes */
3502 result = gst_pad_query_peer_duration (pad, &format, &duration);
3504 GST_DEBUG_OBJECT (basesink,
3505 "setting duration in bytes to %" G_GINT64_FORMAT, duration);
3506 gst_segment_set_duration (basesink->abidata.ABI.clip_segment, format,
3508 gst_segment_set_duration (&basesink->segment, format, duration);
3510 GST_DEBUG_OBJECT (basesink, "unknown duration");
3513 if (bclass->activate_pull)
3514 result = bclass->activate_pull (basesink, TRUE);
3519 goto activate_failed;
3522 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PULL)) {
3523 g_warning ("Internal GStreamer activation error!!!");
3526 result = gst_base_sink_set_flushing (basesink, pad, TRUE);
3527 if (bclass->activate_pull)
3528 result &= bclass->activate_pull (basesink, FALSE);
3529 basesink->pad_mode = GST_ACTIVATE_NONE;
3530 /* clear any pending caps */
3531 GST_OBJECT_LOCK (basesink);
3532 gst_caps_replace (&basesink->priv->pull_caps, NULL);
3533 GST_OBJECT_UNLOCK (basesink);
3536 gst_object_unref (basesink);
3543 /* reset, as starting the thread failed */
3544 basesink->pad_mode = GST_ACTIVATE_NONE;
3546 GST_ERROR_OBJECT (basesink, "subclass failed to activate in pull mode");
3551 /* send an event to our sinkpad peer. */
3553 gst_base_sink_send_event (GstElement * element, GstEvent * event)
3556 GstBaseSink *basesink = GST_BASE_SINK (element);
3557 gboolean forward, result = TRUE;
3558 GstActivateMode mode;
3560 GST_OBJECT_LOCK (element);
3561 /* get the pad and the scheduling mode */
3562 pad = gst_object_ref (basesink->sinkpad);
3563 mode = basesink->pad_mode;
3564 GST_OBJECT_UNLOCK (element);
3566 /* only push UPSTREAM events upstream */
3567 forward = GST_EVENT_IS_UPSTREAM (event);
3569 switch (GST_EVENT_TYPE (event)) {
3570 case GST_EVENT_LATENCY:
3572 GstClockTime latency;
3574 gst_event_parse_latency (event, &latency);
3576 /* store the latency. We use this to adjust the running_time before syncing
3577 * it to the clock. */
3578 GST_OBJECT_LOCK (element);
3579 basesink->priv->latency = latency;
3580 if (!basesink->priv->have_latency)
3582 GST_OBJECT_UNLOCK (element);
3583 GST_DEBUG_OBJECT (basesink, "latency set to %" GST_TIME_FORMAT,
3584 GST_TIME_ARGS (latency));
3586 /* We forward this event so that all elements know about the global pipeline
3587 * latency. This is interesting for an element when it wants to figure out
3588 * when a particular piece of data will be rendered. */
3591 case GST_EVENT_SEEK:
3592 /* in pull mode we will execute the seek */
3593 if (mode == GST_ACTIVATE_PULL)
3594 result = gst_base_sink_perform_seek (basesink, pad, event);
3601 result = gst_pad_push_event (pad, event);
3603 /* not forwarded, unref the event */
3604 gst_event_unref (event);
3607 gst_object_unref (pad);
3612 gst_base_sink_peer_query (GstBaseSink * sink, GstQuery * query)
3615 gboolean res = FALSE;
3617 if ((peer = gst_pad_get_peer (sink->sinkpad))) {
3618 res = gst_pad_query (peer, query);
3619 gst_object_unref (peer);
3624 /* get the end position of the last seen object, this is used
3625 * for EOS and for making sure that we don't report a position we
3626 * have not reached yet. With LOCK. */
3628 gst_base_sink_get_position_last (GstBaseSink * basesink, GstFormat format,
3632 GstSegment *segment;
3633 gboolean ret = TRUE;
3635 segment = &basesink->segment;
3636 oformat = segment->format;
3638 if (oformat == GST_FORMAT_TIME) {
3639 /* return last observed stream time, we keep the stream time around in the
3641 *cur = basesink->priv->current_sstop;
3643 /* convert last stop to stream time */
3644 *cur = gst_segment_to_stream_time (segment, oformat, segment->last_stop);
3647 if (*cur != -1 && oformat != format) {
3648 GST_OBJECT_UNLOCK (basesink);
3649 /* convert to the target format if we need to, release lock first */
3651 gst_pad_query_convert (basesink->sinkpad, oformat, *cur, &format, cur);
3654 GST_OBJECT_LOCK (basesink);
3657 GST_DEBUG_OBJECT (basesink, "POSITION: %" GST_TIME_FORMAT,
3658 GST_TIME_ARGS (*cur));
3663 /* get the position when we are PAUSED, this is the stream time of the buffer
3664 * that prerolled. If no buffer is prerolled (we are still flushing), this
3665 * value will be -1. With LOCK. */
3667 gst_base_sink_get_position_paused (GstBaseSink * basesink, GstFormat format,
3672 GstSegment *segment;
3675 /* we don't use the clip segment in pull mode, when seeking we update the
3676 * main segment directly with the new segment values without it having to be
3677 * activated by the rendering after preroll */
3678 if (basesink->pad_mode == GST_ACTIVATE_PUSH)
3679 segment = basesink->abidata.ABI.clip_segment;
3681 segment = &basesink->segment;
3682 oformat = segment->format;
3684 if (oformat == GST_FORMAT_TIME) {
3685 *cur = basesink->priv->current_sstart;
3687 *cur = gst_segment_to_stream_time (segment, oformat, segment->last_stop);
3690 time = segment->time;
3693 *cur = MAX (*cur, time);
3694 GST_DEBUG_OBJECT (basesink, "POSITION as max: %" GST_TIME_FORMAT
3695 ", time %" GST_TIME_FORMAT, GST_TIME_ARGS (*cur), GST_TIME_ARGS (time));
3697 /* we have no buffer, use the segment times. */
3698 if (segment->rate >= 0.0) {
3699 /* forward, next position is always the time of the segment */
3701 GST_DEBUG_OBJECT (basesink, "POSITION as time: %" GST_TIME_FORMAT,
3702 GST_TIME_ARGS (*cur));
3704 /* reverse, next expected timestamp is segment->stop. We use the function
3705 * to get things right for negative applied_rates. */
3706 *cur = gst_segment_to_stream_time (segment, oformat, segment->stop);
3707 GST_DEBUG_OBJECT (basesink, "reverse POSITION: %" GST_TIME_FORMAT,
3708 GST_TIME_ARGS (*cur));
3713 if (res && oformat != format) {
3714 GST_OBJECT_UNLOCK (basesink);
3716 gst_pad_query_convert (basesink->sinkpad, oformat, *cur, &format, cur);
3719 GST_OBJECT_LOCK (basesink);
3726 gst_base_sink_get_position (GstBaseSink * basesink, GstFormat format,
3727 gint64 * cur, gboolean * upstream)
3730 gboolean res = FALSE;
3731 GstFormat oformat, tformat;
3732 GstClockTime now, base, latency;
3733 gint64 time, accum, duration;
3737 GST_OBJECT_LOCK (basesink);
3738 /* our intermediate time format */
3739 tformat = GST_FORMAT_TIME;
3740 /* get the format in the segment */
3741 oformat = basesink->segment.format;
3743 /* can only give answer based on the clock if not EOS */
3744 if (G_UNLIKELY (basesink->eos))
3747 /* we can only get the segment when we are not NULL or READY */
3748 if (!basesink->have_newsegment)
3751 /* when not in PLAYING or when we're busy with a state change, we
3752 * cannot read from the clock so we report time based on the
3753 * last seen timestamp. */
3754 if (GST_STATE (basesink) != GST_STATE_PLAYING ||
3755 GST_STATE_PENDING (basesink) != GST_STATE_VOID_PENDING)
3758 /* we need to sync on the clock. */
3759 if (basesink->sync == FALSE)
3762 /* and we need a clock */
3763 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (basesink)) == NULL))
3766 /* collect all data we need holding the lock */
3767 if (GST_CLOCK_TIME_IS_VALID (basesink->segment.time))
3768 time = basesink->segment.time;
3772 if (GST_CLOCK_TIME_IS_VALID (basesink->segment.stop))
3773 duration = basesink->segment.stop - basesink->segment.start;
3777 base = GST_ELEMENT_CAST (basesink)->base_time;
3778 accum = basesink->segment.accum;
3779 rate = basesink->segment.rate * basesink->segment.applied_rate;
3780 latency = basesink->priv->latency;
3782 gst_object_ref (clock);
3784 /* this function might release the LOCK */
3785 gst_base_sink_get_position_last (basesink, format, &last);
3787 /* need to release the object lock before we can get the time,
3788 * a clock might take the LOCK of the provider, which could be
3789 * a basesink subclass. */
3790 GST_OBJECT_UNLOCK (basesink);
3792 now = gst_clock_get_time (clock);
3794 if (oformat != tformat) {
3795 /* convert accum, time and duration to time */
3796 if (!gst_pad_query_convert (basesink->sinkpad, oformat, accum, &tformat,
3798 goto convert_failed;
3799 if (!gst_pad_query_convert (basesink->sinkpad, oformat, duration, &tformat,
3801 goto convert_failed;
3802 if (!gst_pad_query_convert (basesink->sinkpad, oformat, time, &tformat,
3804 goto convert_failed;
3807 /* subtract base time and accumulated time from the clock time.
3808 * Make sure we don't go negative. This is the current time in
3809 * the segment which we need to scale with the combined
3810 * rate and applied rate. */
3813 base = MIN (now, base);
3815 /* for negative rates we need to count back from from the segment
3820 *cur = time + gst_guint64_to_gdouble (now - base) * rate;
3822 /* never report more than last seen position */
3824 *cur = MIN (last, *cur);
3826 gst_object_unref (clock);
3828 GST_DEBUG_OBJECT (basesink,
3829 "now %" GST_TIME_FORMAT " - base %" GST_TIME_FORMAT " - accum %"
3830 GST_TIME_FORMAT " + time %" GST_TIME_FORMAT,
3831 GST_TIME_ARGS (now), GST_TIME_ARGS (base),
3832 GST_TIME_ARGS (accum), GST_TIME_ARGS (time));
3834 if (oformat != format) {
3835 /* convert time to final format */
3836 if (!gst_pad_query_convert (basesink->sinkpad, tformat, *cur, &format, cur))
3837 goto convert_failed;
3843 GST_DEBUG_OBJECT (basesink, "res: %d, POSITION: %" GST_TIME_FORMAT,
3844 res, GST_TIME_ARGS (*cur));
3850 GST_DEBUG_OBJECT (basesink, "position in EOS");
3851 res = gst_base_sink_get_position_last (basesink, format, cur);
3852 GST_OBJECT_UNLOCK (basesink);
3857 GST_DEBUG_OBJECT (basesink, "position in PAUSED");
3858 res = gst_base_sink_get_position_paused (basesink, format, cur);
3859 GST_OBJECT_UNLOCK (basesink);
3864 /* in NULL or READY we always return FALSE and -1 */
3865 GST_DEBUG_OBJECT (basesink, "position in wrong state, return -1");
3868 GST_OBJECT_UNLOCK (basesink);
3873 /* report last seen timestamp if any, else ask upstream to answer */
3874 if ((*cur = basesink->priv->current_sstart) != -1)
3879 GST_DEBUG_OBJECT (basesink, "no sync, res %d, POSITION %" GST_TIME_FORMAT,
3880 res, GST_TIME_ARGS (*cur));
3881 GST_OBJECT_UNLOCK (basesink);
3886 GST_DEBUG_OBJECT (basesink, "convert failed, try upstream");
3893 gst_base_sink_query (GstElement * element, GstQuery * query)
3895 gboolean res = FALSE;
3897 GstBaseSink *basesink = GST_BASE_SINK (element);
3899 switch (GST_QUERY_TYPE (query)) {
3900 case GST_QUERY_POSITION:
3904 gboolean upstream = FALSE;
3906 gst_query_parse_position (query, &format, NULL);
3908 GST_DEBUG_OBJECT (basesink, "position format %d", format);
3910 /* first try to get the position based on the clock */
3912 gst_base_sink_get_position (basesink, format, &cur, &upstream))) {
3913 gst_query_set_position (query, format, cur);
3914 } else if (upstream) {
3915 /* fallback to peer query */
3916 res = gst_base_sink_peer_query (basesink, query);
3920 case GST_QUERY_DURATION:
3922 GstFormat format, uformat;
3923 gint64 duration, uduration;
3925 gst_query_parse_duration (query, &format, NULL);
3927 GST_DEBUG_OBJECT (basesink, "duration query in format %s",
3928 gst_format_get_name (format));
3930 if (basesink->pad_mode == GST_ACTIVATE_PULL) {
3931 uformat = GST_FORMAT_BYTES;
3933 /* get the duration in bytes, in pull mode that's all we are sure to
3934 * know. We have to explicitly get this value from upstream instead of
3935 * using our cached value because it might change. Duration caching
3936 * should be done at a higher level. */
3937 res = gst_pad_query_peer_duration (basesink->sinkpad, &uformat,
3940 gst_segment_set_duration (&basesink->segment, uformat, uduration);
3941 if (format != uformat) {
3942 /* convert to the requested format */
3943 res = gst_pad_query_convert (basesink->sinkpad, uformat, uduration,
3944 &format, &duration);
3946 duration = uduration;
3949 /* set the result */
3950 gst_query_set_duration (query, format, duration);
3954 /* in push mode we simply forward upstream */
3955 res = gst_base_sink_peer_query (basesink, query);
3959 case GST_QUERY_LATENCY:
3961 gboolean live, us_live;
3962 GstClockTime min, max;
3964 if ((res = gst_base_sink_query_latency (basesink, &live, &us_live, &min,
3966 gst_query_set_latency (query, live, min, max);
3970 case GST_QUERY_JITTER:
3972 case GST_QUERY_RATE:
3973 /* gst_query_set_rate (query, basesink->segment_rate); */
3976 case GST_QUERY_SEGMENT:
3978 /* FIXME, bring start/stop to stream time */
3979 gst_query_set_segment (query, basesink->segment.rate,
3980 GST_FORMAT_TIME, basesink->segment.start, basesink->segment.stop);
3983 case GST_QUERY_SEEKING:
3984 case GST_QUERY_CONVERT:
3985 case GST_QUERY_FORMATS:
3987 res = gst_base_sink_peer_query (basesink, query);
3993 static GstStateChangeReturn
3994 gst_base_sink_change_state (GstElement * element, GstStateChange transition)
3996 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
3997 GstBaseSink *basesink = GST_BASE_SINK (element);
3998 GstBaseSinkClass *bclass;
3999 GstBaseSinkPrivate *priv;
4001 priv = basesink->priv;
4003 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4005 switch (transition) {
4006 case GST_STATE_CHANGE_NULL_TO_READY:
4008 if (!bclass->start (basesink))
4011 case GST_STATE_CHANGE_READY_TO_PAUSED:
4012 /* need to complete preroll before this state change completes, there
4013 * is no data flow in READY so we can safely assume we need to preroll. */
4014 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4015 GST_DEBUG_OBJECT (basesink, "READY to PAUSED");
4016 basesink->have_newsegment = FALSE;
4017 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
4018 gst_segment_init (basesink->abidata.ABI.clip_segment,
4019 GST_FORMAT_UNDEFINED);
4020 basesink->offset = 0;
4021 basesink->have_preroll = FALSE;
4022 basesink->need_preroll = TRUE;
4023 basesink->playing_async = TRUE;
4024 priv->current_sstart = -1;
4025 priv->current_sstop = -1;
4026 priv->eos_rtime = -1;
4028 basesink->eos = FALSE;
4029 priv->received_eos = FALSE;
4030 gst_base_sink_reset_qos (basesink);
4031 priv->commited = FALSE;
4032 priv->call_preroll = TRUE;
4033 if (priv->async_enabled) {
4034 GST_DEBUG_OBJECT (basesink, "doing async state change");
4035 /* when async enabled, post async-start message and return ASYNC from
4036 * the state change function */
4037 ret = GST_STATE_CHANGE_ASYNC;
4038 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4039 gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
4041 priv->have_latency = TRUE;
4043 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4045 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
4046 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4047 if (!gst_base_sink_needs_preroll (basesink)) {
4048 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, don't need preroll");
4049 /* no preroll needed anymore now. */
4050 basesink->playing_async = FALSE;
4051 basesink->need_preroll = FALSE;
4052 if (basesink->eos) {
4053 GstMessage *message;
4055 /* need to post EOS message here */
4056 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
4057 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
4058 gst_message_set_seqnum (message, basesink->priv->seqnum);
4059 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
4061 GST_DEBUG_OBJECT (basesink, "signal preroll");
4062 GST_PAD_PREROLL_SIGNAL (basesink->sinkpad);
4065 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, we are not prerolled");
4066 basesink->need_preroll = TRUE;
4067 basesink->playing_async = TRUE;
4068 priv->call_preroll = TRUE;
4069 priv->commited = FALSE;
4070 if (priv->async_enabled) {
4071 GST_DEBUG_OBJECT (basesink, "doing async state change");
4072 ret = GST_STATE_CHANGE_ASYNC;
4073 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4074 gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
4077 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4084 GstStateChangeReturn bret;
4086 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4087 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
4088 goto activate_failed;
4091 switch (transition) {
4092 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
4093 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED");
4094 /* FIXME, make sure we cannot enter _render first */
4096 /* we need to call ::unlock before locking PREROLL_LOCK
4097 * since we lock it before going into ::render */
4099 bclass->unlock (basesink);
4101 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4102 /* now that we have the PREROLL lock, clear our unlock request */
4103 if (bclass->unlock_stop)
4104 bclass->unlock_stop (basesink);
4106 /* we need preroll again and we set the flag before unlocking the clockid
4107 * because if the clockid is unlocked before a current buffer expired, we
4108 * can use that buffer to preroll with */
4109 basesink->need_preroll = TRUE;
4111 if (basesink->clock_id) {
4112 gst_clock_id_unschedule (basesink->clock_id);
4115 /* if we don't have a preroll buffer we need to wait for a preroll and
4117 if (!gst_base_sink_needs_preroll (basesink)) {
4118 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, we are prerolled");
4119 basesink->playing_async = FALSE;
4121 if (GST_STATE_TARGET (GST_ELEMENT (basesink)) <= GST_STATE_READY) {
4122 ret = GST_STATE_CHANGE_SUCCESS;
4124 GST_DEBUG_OBJECT (basesink,
4125 "PLAYING to PAUSED, we are not prerolled");
4126 basesink->playing_async = TRUE;
4127 priv->commited = FALSE;
4128 priv->call_preroll = TRUE;
4129 if (priv->async_enabled) {
4130 GST_DEBUG_OBJECT (basesink, "doing async state change");
4131 ret = GST_STATE_CHANGE_ASYNC;
4132 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4133 gst_message_new_async_start (GST_OBJECT_CAST (basesink),
4138 GST_DEBUG_OBJECT (basesink, "rendered: %" G_GUINT64_FORMAT
4139 ", dropped: %" G_GUINT64_FORMAT, priv->rendered, priv->dropped);
4141 gst_base_sink_reset_qos (basesink);
4142 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4144 case GST_STATE_CHANGE_PAUSED_TO_READY:
4145 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4146 /* start by reseting our position state with the object lock so that the
4147 * position query gets the right idea. We do this before we post the
4148 * messages so that the message handlers pick this up. */
4149 GST_OBJECT_LOCK (basesink);
4150 basesink->have_newsegment = FALSE;
4151 priv->current_sstart = -1;
4152 priv->current_sstop = -1;
4153 priv->have_latency = FALSE;
4154 GST_OBJECT_UNLOCK (basesink);
4156 gst_base_sink_set_last_buffer (basesink, NULL);
4157 priv->call_preroll = FALSE;
4159 if (!priv->commited) {
4160 if (priv->async_enabled) {
4161 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, posting async-done");
4163 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4164 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
4165 GST_STATE_PLAYING, GST_STATE_PAUSED, GST_STATE_READY));
4167 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4168 gst_message_new_async_done (GST_OBJECT_CAST (basesink)));
4170 priv->commited = TRUE;
4172 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, don't need_preroll");
4174 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4176 case GST_STATE_CHANGE_READY_TO_NULL:
4178 if (!bclass->stop (basesink)) {
4179 GST_WARNING_OBJECT (basesink, "failed to stop");
4182 gst_base_sink_set_last_buffer (basesink, NULL);
4183 priv->call_preroll = FALSE;
4194 GST_DEBUG_OBJECT (basesink, "failed to start");
4195 return GST_STATE_CHANGE_FAILURE;
4199 GST_DEBUG_OBJECT (basesink,
4200 "element failed to change states -- activation problem?");
4201 return GST_STATE_CHANGE_FAILURE;