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 #define GST_FLOW_STEP GST_FLOW_CUSTOM_ERROR
159 gboolean valid; /* if this info is valid */
160 guint32 seqnum; /* the seqnum of the STEP event */
161 GstFormat format; /* the format of the amount */
162 guint64 amount; /* the total amount of data to skip */
163 guint64 position; /* the position in the stepped data */
164 guint64 duration; /* the duration in time of the skipped data */
165 guint64 start; /* running_time of the start */
166 gdouble rate; /* rate of skipping */
167 gdouble start_rate; /* rate before skipping */
168 guint64 start_start; /* start position skipping */
169 guint64 start_stop; /* stop position skipping */
170 gboolean flush; /* if this was a flushing step */
171 gboolean intermediate; /* if this is an intermediate step */
172 gboolean need_preroll; /* if we need preroll after this step */
175 /* FIXME, some stuff in ABI.data and other in Private...
176 * Make up your mind please.
178 struct _GstBaseSinkPrivate
180 gint qos_enabled; /* ATOMIC */
181 gboolean async_enabled;
182 GstClockTimeDiff ts_offset;
183 GstClockTime render_delay;
185 /* start, stop of current buffer, stream time, used to report position */
186 GstClockTime current_sstart;
187 GstClockTime current_sstop;
189 /* start, stop and jitter of current buffer, running time */
190 GstClockTime current_rstart;
191 GstClockTime current_rstop;
192 GstClockTimeDiff current_jitter;
194 /* EOS sync time in running time */
195 GstClockTime eos_rtime;
197 /* last buffer that arrived in time, running time */
198 GstClockTime last_in_time;
199 /* when the last buffer left the sink, running time */
200 GstClockTime last_left;
202 /* running averages go here these are done on running time */
204 GstClockTime avg_duration;
207 /* these are done on system time. avg_jitter and avg_render are
208 * compared to eachother to see if the rendering time takes a
209 * huge amount of the processing, If so we are flooded with
211 GstClockTime last_left_systime;
212 GstClockTime avg_jitter;
213 GstClockTime start, stop;
214 GstClockTime avg_render;
216 /* number of rendered and dropped frames */
221 GstClockTime latency;
223 /* if we already commited the state */
226 /* when we received EOS */
227 gboolean received_eos;
229 /* when we are prerolled and able to report latency */
230 gboolean have_latency;
232 /* the last buffer we prerolled or rendered. Useful for making snapshots */
233 GstBuffer *last_buffer;
235 /* caps for pull based scheduling */
238 /* blocksize for pulling */
243 /* seqnum of the stream */
246 gboolean call_preroll;
247 gboolean step_unlock;
249 /* we have a pending and a current step operation */
250 GstStepInfo current_step;
251 GstStepInfo pending_step;
254 #define DO_RUNNING_AVG(avg,val,size) (((val) + ((size)-1) * (avg)) / (size))
256 /* generic running average, this has a neutral window size */
257 #define UPDATE_RUNNING_AVG(avg,val) DO_RUNNING_AVG(avg,val,8)
259 /* the windows for these running averages are experimentally obtained.
260 * possitive values get averaged more while negative values use a small
261 * window so we can react faster to badness. */
262 #define UPDATE_RUNNING_AVG_P(avg,val) DO_RUNNING_AVG(avg,val,16)
263 #define UPDATE_RUNNING_AVG_N(avg,val) DO_RUNNING_AVG(avg,val,4)
265 /* BaseSink properties */
267 #define DEFAULT_CAN_ACTIVATE_PULL FALSE /* fixme: enable me */
268 #define DEFAULT_CAN_ACTIVATE_PUSH TRUE
270 #define DEFAULT_PREROLL_QUEUE_LEN 0
271 #define DEFAULT_SYNC TRUE
272 #define DEFAULT_MAX_LATENESS -1
273 #define DEFAULT_QOS FALSE
274 #define DEFAULT_ASYNC TRUE
275 #define DEFAULT_TS_OFFSET 0
276 #define DEFAULT_BLOCKSIZE 4096
277 #define DEFAULT_RENDER_DELAY 0
282 PROP_PREROLL_QUEUE_LEN,
294 static GstElementClass *parent_class = NULL;
296 static void gst_base_sink_class_init (GstBaseSinkClass * klass);
297 static void gst_base_sink_init (GstBaseSink * trans, gpointer g_class);
298 static void gst_base_sink_finalize (GObject * object);
301 gst_base_sink_get_type (void)
303 static volatile gsize base_sink_type = 0;
305 if (g_once_init_enter (&base_sink_type)) {
307 static const GTypeInfo base_sink_info = {
308 sizeof (GstBaseSinkClass),
311 (GClassInitFunc) gst_base_sink_class_init,
314 sizeof (GstBaseSink),
316 (GInstanceInitFunc) gst_base_sink_init,
319 _type = g_type_register_static (GST_TYPE_ELEMENT,
320 "GstBaseSink", &base_sink_info, G_TYPE_FLAG_ABSTRACT);
321 g_once_init_leave (&base_sink_type, _type);
323 return base_sink_type;
326 static void gst_base_sink_set_property (GObject * object, guint prop_id,
327 const GValue * value, GParamSpec * pspec);
328 static void gst_base_sink_get_property (GObject * object, guint prop_id,
329 GValue * value, GParamSpec * pspec);
331 static gboolean gst_base_sink_send_event (GstElement * element,
333 static gboolean gst_base_sink_query (GstElement * element, GstQuery * query);
335 static GstCaps *gst_base_sink_get_caps (GstBaseSink * sink);
336 static gboolean gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps);
337 static GstFlowReturn gst_base_sink_buffer_alloc (GstBaseSink * sink,
338 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
339 static void gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
340 GstClockTime * start, GstClockTime * end);
341 static gboolean gst_base_sink_set_flushing (GstBaseSink * basesink,
342 GstPad * pad, gboolean flushing);
343 static gboolean gst_base_sink_default_activate_pull (GstBaseSink * basesink,
345 static gboolean gst_base_sink_default_do_seek (GstBaseSink * sink,
346 GstSegment * segment);
347 static gboolean gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
348 GstEvent * event, GstSegment * segment);
350 static GstStateChangeReturn gst_base_sink_change_state (GstElement * element,
351 GstStateChange transition);
353 static GstFlowReturn gst_base_sink_chain (GstPad * pad, GstBuffer * buffer);
354 static void gst_base_sink_loop (GstPad * pad);
355 static gboolean gst_base_sink_pad_activate (GstPad * pad);
356 static gboolean gst_base_sink_pad_activate_push (GstPad * pad, gboolean active);
357 static gboolean gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active);
358 static gboolean gst_base_sink_event (GstPad * pad, GstEvent * event);
359 static gboolean gst_base_sink_peer_query (GstBaseSink * sink, GstQuery * query);
361 static gboolean gst_base_sink_negotiate_pull (GstBaseSink * basesink);
363 /* check if an object was too late */
364 static gboolean gst_base_sink_is_too_late (GstBaseSink * basesink,
365 GstMiniObject * obj, GstClockTime start, GstClockTime stop,
366 GstClockReturn status, GstClockTimeDiff jitter);
367 static GstFlowReturn gst_base_sink_preroll_object (GstBaseSink * basesink,
368 GstMiniObject * obj);
371 gst_base_sink_class_init (GstBaseSinkClass * klass)
373 GObjectClass *gobject_class;
374 GstElementClass *gstelement_class;
376 gobject_class = G_OBJECT_CLASS (klass);
377 gstelement_class = GST_ELEMENT_CLASS (klass);
379 GST_DEBUG_CATEGORY_INIT (gst_base_sink_debug, "basesink", 0,
382 g_type_class_add_private (klass, sizeof (GstBaseSinkPrivate));
384 parent_class = g_type_class_peek_parent (klass);
386 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_sink_finalize);
387 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_base_sink_set_property);
388 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_base_sink_get_property);
390 /* FIXME, this next value should be configured using an event from the
391 * upstream element, ie, the BUFFER_SIZE event. */
392 g_object_class_install_property (gobject_class, PROP_PREROLL_QUEUE_LEN,
393 g_param_spec_uint ("preroll-queue-len", "Preroll queue length",
394 "Number of buffers to queue during preroll", 0, G_MAXUINT,
395 DEFAULT_PREROLL_QUEUE_LEN,
396 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
398 g_object_class_install_property (gobject_class, PROP_SYNC,
399 g_param_spec_boolean ("sync", "Sync", "Sync on the clock", DEFAULT_SYNC,
400 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
402 g_object_class_install_property (gobject_class, PROP_MAX_LATENESS,
403 g_param_spec_int64 ("max-lateness", "Max Lateness",
404 "Maximum number of nanoseconds that a buffer can be late before it "
405 "is dropped (-1 unlimited)", -1, G_MAXINT64, DEFAULT_MAX_LATENESS,
406 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
408 g_object_class_install_property (gobject_class, PROP_QOS,
409 g_param_spec_boolean ("qos", "Qos",
410 "Generate Quality-of-Service events upstream", DEFAULT_QOS,
411 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
415 * If set to #TRUE, the basesink will perform asynchronous state changes.
416 * When set to #FALSE, the sink will not signal the parent when it prerolls.
417 * Use this option when dealing with sparse streams or when synchronisation is
422 g_object_class_install_property (gobject_class, PROP_ASYNC,
423 g_param_spec_boolean ("async", "Async",
424 "Go asynchronously to PAUSED", DEFAULT_ASYNC,
425 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
427 * GstBaseSink:ts-offset
429 * Controls the final synchronisation, a negative value will render the buffer
430 * earlier while a positive value delays playback. This property can be
431 * used to fix synchronisation in bad files.
435 g_object_class_install_property (gobject_class, PROP_TS_OFFSET,
436 g_param_spec_int64 ("ts-offset", "TS Offset",
437 "Timestamp offset in nanoseconds", G_MININT64, G_MAXINT64,
438 DEFAULT_TS_OFFSET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
440 * GstBaseSink:last-buffer
442 * The last buffer that arrived in the sink and was used for preroll or for
443 * rendering. This property can be used to generate thumbnails. This property
444 * can be NULL when the sink has not yet received a bufer.
448 g_object_class_install_property (gobject_class, PROP_LAST_BUFFER,
449 gst_param_spec_mini_object ("last-buffer", "Last Buffer",
450 "The last buffer received in the sink", GST_TYPE_BUFFER,
451 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
453 * GstBaseSink:blocksize
455 * The amount of bytes to pull when operating in pull mode.
459 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
460 g_param_spec_uint ("blocksize", "Block size",
461 "Size in bytes to pull per buffer (0 = default)", 0, G_MAXUINT,
462 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
464 * GstBaseSink:render-delay
466 * The additional delay between synchronisation and actual rendering of the
467 * media. This property will add additional latency to the device in order to
468 * make other sinks compensate for the delay.
472 g_object_class_install_property (gobject_class, PROP_RENDER_DELAY,
473 g_param_spec_uint64 ("render-delay", "Render Delay",
474 "Additional render delay of the sink in nanoseconds", 0, G_MAXUINT64,
475 DEFAULT_RENDER_DELAY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
477 gstelement_class->change_state =
478 GST_DEBUG_FUNCPTR (gst_base_sink_change_state);
479 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_sink_send_event);
480 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_base_sink_query);
482 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_sink_get_caps);
483 klass->set_caps = GST_DEBUG_FUNCPTR (gst_base_sink_set_caps);
484 klass->buffer_alloc = GST_DEBUG_FUNCPTR (gst_base_sink_buffer_alloc);
485 klass->get_times = GST_DEBUG_FUNCPTR (gst_base_sink_get_times);
486 klass->activate_pull =
487 GST_DEBUG_FUNCPTR (gst_base_sink_default_activate_pull);
491 gst_base_sink_pad_getcaps (GstPad * pad)
493 GstBaseSinkClass *bclass;
495 GstCaps *caps = NULL;
497 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
498 bclass = GST_BASE_SINK_GET_CLASS (bsink);
500 if (bsink->pad_mode == GST_ACTIVATE_PULL) {
501 /* if we are operating in pull mode we only accept the negotiated caps */
502 GST_OBJECT_LOCK (pad);
503 if ((caps = GST_PAD_CAPS (pad)))
505 GST_OBJECT_UNLOCK (pad);
508 if (bclass->get_caps)
509 caps = bclass->get_caps (bsink);
512 GstPadTemplate *pad_template;
515 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass),
517 if (pad_template != NULL) {
518 caps = gst_caps_ref (gst_pad_template_get_caps (pad_template));
522 gst_object_unref (bsink);
528 gst_base_sink_pad_setcaps (GstPad * pad, GstCaps * caps)
530 GstBaseSinkClass *bclass;
534 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
535 bclass = GST_BASE_SINK_GET_CLASS (bsink);
537 if (res && bclass->set_caps)
538 res = bclass->set_caps (bsink, caps);
540 gst_object_unref (bsink);
546 gst_base_sink_pad_fixate (GstPad * pad, GstCaps * caps)
548 GstBaseSinkClass *bclass;
551 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
552 bclass = GST_BASE_SINK_GET_CLASS (bsink);
555 bclass->fixate (bsink, caps);
557 gst_object_unref (bsink);
561 gst_base_sink_pad_buffer_alloc (GstPad * pad, guint64 offset, guint size,
562 GstCaps * caps, GstBuffer ** buf)
564 GstBaseSinkClass *bclass;
566 GstFlowReturn result = GST_FLOW_OK;
568 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
569 bclass = GST_BASE_SINK_GET_CLASS (bsink);
571 if (bclass->buffer_alloc)
572 result = bclass->buffer_alloc (bsink, offset, size, caps, buf);
574 *buf = NULL; /* fallback in gstpad.c will allocate generic buffer */
576 gst_object_unref (bsink);
582 gst_base_sink_init (GstBaseSink * basesink, gpointer g_class)
584 GstPadTemplate *pad_template;
585 GstBaseSinkPrivate *priv;
587 basesink->priv = priv = GST_BASE_SINK_GET_PRIVATE (basesink);
590 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
591 g_return_if_fail (pad_template != NULL);
593 basesink->sinkpad = gst_pad_new_from_template (pad_template, "sink");
595 gst_pad_set_getcaps_function (basesink->sinkpad,
596 GST_DEBUG_FUNCPTR (gst_base_sink_pad_getcaps));
597 gst_pad_set_setcaps_function (basesink->sinkpad,
598 GST_DEBUG_FUNCPTR (gst_base_sink_pad_setcaps));
599 gst_pad_set_fixatecaps_function (basesink->sinkpad,
600 GST_DEBUG_FUNCPTR (gst_base_sink_pad_fixate));
601 gst_pad_set_bufferalloc_function (basesink->sinkpad,
602 GST_DEBUG_FUNCPTR (gst_base_sink_pad_buffer_alloc));
603 gst_pad_set_activate_function (basesink->sinkpad,
604 GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate));
605 gst_pad_set_activatepush_function (basesink->sinkpad,
606 GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate_push));
607 gst_pad_set_activatepull_function (basesink->sinkpad,
608 GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate_pull));
609 gst_pad_set_event_function (basesink->sinkpad,
610 GST_DEBUG_FUNCPTR (gst_base_sink_event));
611 gst_pad_set_chain_function (basesink->sinkpad,
612 GST_DEBUG_FUNCPTR (gst_base_sink_chain));
613 gst_element_add_pad (GST_ELEMENT_CAST (basesink), basesink->sinkpad);
615 basesink->pad_mode = GST_ACTIVATE_NONE;
616 basesink->preroll_queue = g_queue_new ();
617 basesink->abidata.ABI.clip_segment = gst_segment_new ();
618 priv->have_latency = FALSE;
620 basesink->can_activate_push = DEFAULT_CAN_ACTIVATE_PUSH;
621 basesink->can_activate_pull = DEFAULT_CAN_ACTIVATE_PULL;
623 basesink->sync = DEFAULT_SYNC;
624 basesink->abidata.ABI.max_lateness = DEFAULT_MAX_LATENESS;
625 g_atomic_int_set (&priv->qos_enabled, DEFAULT_QOS);
626 priv->async_enabled = DEFAULT_ASYNC;
627 priv->ts_offset = DEFAULT_TS_OFFSET;
628 priv->render_delay = DEFAULT_RENDER_DELAY;
629 priv->blocksize = DEFAULT_BLOCKSIZE;
631 GST_OBJECT_FLAG_SET (basesink, GST_ELEMENT_IS_SINK);
635 gst_base_sink_finalize (GObject * object)
637 GstBaseSink *basesink;
639 basesink = GST_BASE_SINK (object);
641 g_queue_free (basesink->preroll_queue);
642 gst_segment_free (basesink->abidata.ABI.clip_segment);
644 G_OBJECT_CLASS (parent_class)->finalize (object);
648 * gst_base_sink_set_sync:
650 * @sync: the new sync value.
652 * Configures @sink to synchronize on the clock or not. When
653 * @sync is FALSE, incomming samples will be played as fast as
654 * possible. If @sync is TRUE, the timestamps of the incomming
655 * buffers will be used to schedule the exact render time of its
661 gst_base_sink_set_sync (GstBaseSink * sink, gboolean sync)
663 g_return_if_fail (GST_IS_BASE_SINK (sink));
665 GST_OBJECT_LOCK (sink);
667 GST_OBJECT_UNLOCK (sink);
671 * gst_base_sink_get_sync:
674 * Checks if @sink is currently configured to synchronize against the
677 * Returns: TRUE if the sink is configured to synchronize against the clock.
682 gst_base_sink_get_sync (GstBaseSink * sink)
686 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
688 GST_OBJECT_LOCK (sink);
690 GST_OBJECT_UNLOCK (sink);
696 * gst_base_sink_set_max_lateness:
698 * @max_lateness: the new max lateness value.
700 * Sets the new max lateness value to @max_lateness. This value is
701 * used to decide if a buffer should be dropped or not based on the
702 * buffer timestamp and the current clock time. A value of -1 means
708 gst_base_sink_set_max_lateness (GstBaseSink * sink, gint64 max_lateness)
710 g_return_if_fail (GST_IS_BASE_SINK (sink));
712 GST_OBJECT_LOCK (sink);
713 sink->abidata.ABI.max_lateness = max_lateness;
714 GST_OBJECT_UNLOCK (sink);
718 * gst_base_sink_get_max_lateness:
721 * Gets the max lateness value. See gst_base_sink_set_max_lateness for
724 * Returns: The maximum time in nanoseconds that a buffer can be late
725 * before it is dropped and not rendered. A value of -1 means an
731 gst_base_sink_get_max_lateness (GstBaseSink * sink)
735 g_return_val_if_fail (GST_IS_BASE_SINK (sink), -1);
737 GST_OBJECT_LOCK (sink);
738 res = sink->abidata.ABI.max_lateness;
739 GST_OBJECT_UNLOCK (sink);
745 * gst_base_sink_set_qos_enabled:
747 * @enabled: the new qos value.
749 * Configures @sink to send Quality-of-Service events upstream.
754 gst_base_sink_set_qos_enabled (GstBaseSink * sink, gboolean enabled)
756 g_return_if_fail (GST_IS_BASE_SINK (sink));
758 g_atomic_int_set (&sink->priv->qos_enabled, enabled);
762 * gst_base_sink_is_qos_enabled:
765 * Checks if @sink is currently configured to send Quality-of-Service events
768 * Returns: TRUE if the sink is configured to perform Quality-of-Service.
773 gst_base_sink_is_qos_enabled (GstBaseSink * sink)
777 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
779 res = g_atomic_int_get (&sink->priv->qos_enabled);
785 * gst_base_sink_set_async_enabled:
787 * @enabled: the new async value.
789 * Configures @sink to perform all state changes asynchronusly. When async is
790 * disabled, the sink will immediatly go to PAUSED instead of waiting for a
791 * preroll buffer. This feature is usefull if the sink does not synchronize
792 * against the clock or when it is dealing with sparse streams.
797 gst_base_sink_set_async_enabled (GstBaseSink * sink, gboolean enabled)
799 g_return_if_fail (GST_IS_BASE_SINK (sink));
801 GST_PAD_PREROLL_LOCK (sink->sinkpad);
802 sink->priv->async_enabled = enabled;
803 GST_LOG_OBJECT (sink, "set async enabled to %d", enabled);
804 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
808 * gst_base_sink_is_async_enabled:
811 * Checks if @sink is currently configured to perform asynchronous state
814 * Returns: TRUE if the sink is configured to perform asynchronous state
820 gst_base_sink_is_async_enabled (GstBaseSink * sink)
824 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
826 GST_PAD_PREROLL_LOCK (sink->sinkpad);
827 res = sink->priv->async_enabled;
828 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
834 * gst_base_sink_set_ts_offset:
836 * @offset: the new offset
838 * Adjust the synchronisation of @sink with @offset. A negative value will
839 * render buffers earlier than their timestamp. A positive value will delay
840 * rendering. This function can be used to fix playback of badly timestamped
846 gst_base_sink_set_ts_offset (GstBaseSink * sink, GstClockTimeDiff offset)
848 g_return_if_fail (GST_IS_BASE_SINK (sink));
850 GST_OBJECT_LOCK (sink);
851 sink->priv->ts_offset = offset;
852 GST_LOG_OBJECT (sink, "set time offset to %" G_GINT64_FORMAT, offset);
853 GST_OBJECT_UNLOCK (sink);
857 * gst_base_sink_get_ts_offset:
860 * Get the synchronisation offset of @sink.
862 * Returns: The synchronisation offset.
867 gst_base_sink_get_ts_offset (GstBaseSink * sink)
869 GstClockTimeDiff res;
871 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
873 GST_OBJECT_LOCK (sink);
874 res = sink->priv->ts_offset;
875 GST_OBJECT_UNLOCK (sink);
881 * gst_base_sink_get_last_buffer:
884 * Get the last buffer that arrived in the sink and was used for preroll or for
885 * rendering. This property can be used to generate thumbnails.
887 * The #GstCaps on the buffer can be used to determine the type of the buffer.
889 * Returns: a #GstBuffer. gst_buffer_unref() after usage. This function returns
890 * NULL when no buffer has arrived in the sink yet or when the sink is not in
896 gst_base_sink_get_last_buffer (GstBaseSink * sink)
900 g_return_val_if_fail (GST_IS_BASE_SINK (sink), NULL);
902 GST_OBJECT_LOCK (sink);
903 if ((res = sink->priv->last_buffer))
904 gst_buffer_ref (res);
905 GST_OBJECT_UNLOCK (sink);
911 gst_base_sink_set_last_buffer (GstBaseSink * sink, GstBuffer * buffer)
915 GST_OBJECT_LOCK (sink);
916 old = sink->priv->last_buffer;
917 if (G_LIKELY (old != buffer)) {
918 GST_DEBUG_OBJECT (sink, "setting last buffer to %p", buffer);
919 if (G_LIKELY (buffer))
920 gst_buffer_ref (buffer);
921 sink->priv->last_buffer = buffer;
925 GST_OBJECT_UNLOCK (sink);
927 /* avoid unreffing with the lock because cleanup code might want to take the
930 gst_buffer_unref (old);
934 * gst_base_sink_get_latency:
937 * Get the currently configured latency.
939 * Returns: The configured latency.
944 gst_base_sink_get_latency (GstBaseSink * sink)
948 GST_OBJECT_LOCK (sink);
949 res = sink->priv->latency;
950 GST_OBJECT_UNLOCK (sink);
956 * gst_base_sink_query_latency:
958 * @live: if the sink is live
959 * @upstream_live: if an upstream element is live
960 * @min_latency: the min latency of the upstream elements
961 * @max_latency: the max latency of the upstream elements
963 * Query the sink for the latency parameters. The latency will be queried from
964 * the upstream elements. @live will be TRUE if @sink is configured to
965 * synchronize against the clock. @upstream_live will be TRUE if an upstream
968 * If both @live and @upstream_live are TRUE, the sink will want to compensate
969 * for the latency introduced by the upstream elements by setting the
970 * @min_latency to a strictly possitive value.
972 * This function is mostly used by subclasses.
974 * Returns: TRUE if the query succeeded.
979 gst_base_sink_query_latency (GstBaseSink * sink, gboolean * live,
980 gboolean * upstream_live, GstClockTime * min_latency,
981 GstClockTime * max_latency)
983 gboolean l, us_live, res, have_latency;
984 GstClockTime min, max, render_delay;
986 GstClockTime us_min, us_max;
988 /* we are live when we sync to the clock */
989 GST_OBJECT_LOCK (sink);
991 have_latency = sink->priv->have_latency;
992 render_delay = sink->priv->render_delay;
993 GST_OBJECT_UNLOCK (sink);
995 /* assume no latency */
1001 GST_DEBUG_OBJECT (sink, "we are ready for LATENCY query");
1002 /* we are ready for a latency query this is when we preroll or when we are
1004 query = gst_query_new_latency ();
1006 /* ask the peer for the latency */
1007 if ((res = gst_base_sink_peer_query (sink, query))) {
1008 /* get upstream min and max latency */
1009 gst_query_parse_latency (query, &us_live, &us_min, &us_max);
1012 /* upstream live, use its latency, subclasses should use these
1013 * values to create the complete latency. */
1018 /* we need to add the render delay if we are live */
1020 min += render_delay;
1022 max += render_delay;
1025 gst_query_unref (query);
1027 GST_DEBUG_OBJECT (sink, "we are not yet ready for LATENCY query");
1031 /* not live, we tried to do the query, if it failed we return TRUE anyway */
1035 GST_DEBUG_OBJECT (sink, "latency query failed but we are not live");
1037 GST_DEBUG_OBJECT (sink, "latency query failed and we are live");
1042 GST_DEBUG_OBJECT (sink, "latency query: live: %d, have_latency %d,"
1043 " upstream: %d, min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT, l,
1044 have_latency, us_live, GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1049 *upstream_live = us_live;
1059 * gst_base_sink_set_render_delay:
1060 * @sink: a #GstBaseSink
1061 * @delay: the new delay
1063 * Set the render delay in @sink to @delay. The render delay is the time
1064 * between actual rendering of a buffer and its synchronisation time. Some
1065 * devices might delay media rendering which can be compensated for with this
1068 * After calling this function, this sink will report additional latency and
1069 * other sinks will adjust their latency to delay the rendering of their media.
1071 * This function is usually called by subclasses.
1076 gst_base_sink_set_render_delay (GstBaseSink * sink, GstClockTime delay)
1078 GstClockTime old_render_delay;
1080 g_return_if_fail (GST_IS_BASE_SINK (sink));
1082 GST_OBJECT_LOCK (sink);
1083 old_render_delay = sink->priv->render_delay;
1084 sink->priv->render_delay = delay;
1085 GST_LOG_OBJECT (sink, "set render delay to %" GST_TIME_FORMAT,
1086 GST_TIME_ARGS (delay));
1087 GST_OBJECT_UNLOCK (sink);
1089 if (delay != old_render_delay) {
1090 GST_DEBUG_OBJECT (sink, "posting latency changed");
1091 gst_element_post_message (GST_ELEMENT_CAST (sink),
1092 gst_message_new_latency (GST_OBJECT_CAST (sink)));
1097 * gst_base_sink_get_render_delay:
1098 * @sink: a #GstBaseSink
1100 * Get the render delay of @sink. see gst_base_sink_set_render_delay() for more
1101 * information about the render delay.
1103 * Returns: the render delay of @sink.
1108 gst_base_sink_get_render_delay (GstBaseSink * sink)
1110 GstClockTimeDiff res;
1112 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1114 GST_OBJECT_LOCK (sink);
1115 res = sink->priv->render_delay;
1116 GST_OBJECT_UNLOCK (sink);
1122 * gst_base_sink_set_blocksize:
1123 * @sink: a #GstBaseSink
1124 * @blocksize: the blocksize in bytes
1126 * Set the number of bytes that the sink will pull when it is operating in pull
1132 gst_base_sink_set_blocksize (GstBaseSink * sink, guint blocksize)
1134 g_return_if_fail (GST_IS_BASE_SINK (sink));
1136 GST_OBJECT_LOCK (sink);
1137 sink->priv->blocksize = blocksize;
1138 GST_LOG_OBJECT (sink, "set blocksize to %u", blocksize);
1139 GST_OBJECT_UNLOCK (sink);
1143 * gst_base_sink_get_blocksize:
1144 * @sink: a #GstBaseSink
1146 * Get the number of bytes that the sink will pull when it is operating in pull
1149 * Returns: the number of bytes @sink will pull in pull mode.
1154 gst_base_sink_get_blocksize (GstBaseSink * sink)
1158 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1160 GST_OBJECT_LOCK (sink);
1161 res = sink->priv->blocksize;
1162 GST_OBJECT_UNLOCK (sink);
1168 gst_base_sink_set_property (GObject * object, guint prop_id,
1169 const GValue * value, GParamSpec * pspec)
1171 GstBaseSink *sink = GST_BASE_SINK (object);
1174 case PROP_PREROLL_QUEUE_LEN:
1175 /* preroll lock necessary to serialize with finish_preroll */
1176 GST_PAD_PREROLL_LOCK (sink->sinkpad);
1177 sink->preroll_queue_max_len = g_value_get_uint (value);
1178 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
1181 gst_base_sink_set_sync (sink, g_value_get_boolean (value));
1183 case PROP_MAX_LATENESS:
1184 gst_base_sink_set_max_lateness (sink, g_value_get_int64 (value));
1187 gst_base_sink_set_qos_enabled (sink, g_value_get_boolean (value));
1190 gst_base_sink_set_async_enabled (sink, g_value_get_boolean (value));
1192 case PROP_TS_OFFSET:
1193 gst_base_sink_set_ts_offset (sink, g_value_get_int64 (value));
1195 case PROP_BLOCKSIZE:
1196 gst_base_sink_set_blocksize (sink, g_value_get_uint (value));
1198 case PROP_RENDER_DELAY:
1199 gst_base_sink_set_render_delay (sink, g_value_get_uint64 (value));
1202 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1208 gst_base_sink_get_property (GObject * object, guint prop_id, GValue * value,
1211 GstBaseSink *sink = GST_BASE_SINK (object);
1214 case PROP_PREROLL_QUEUE_LEN:
1215 GST_PAD_PREROLL_LOCK (sink->sinkpad);
1216 g_value_set_uint (value, sink->preroll_queue_max_len);
1217 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
1220 g_value_set_boolean (value, gst_base_sink_get_sync (sink));
1222 case PROP_MAX_LATENESS:
1223 g_value_set_int64 (value, gst_base_sink_get_max_lateness (sink));
1226 g_value_set_boolean (value, gst_base_sink_is_qos_enabled (sink));
1229 g_value_set_boolean (value, gst_base_sink_is_async_enabled (sink));
1231 case PROP_TS_OFFSET:
1232 g_value_set_int64 (value, gst_base_sink_get_ts_offset (sink));
1234 case PROP_LAST_BUFFER:
1235 gst_value_take_buffer (value, gst_base_sink_get_last_buffer (sink));
1237 case PROP_BLOCKSIZE:
1238 g_value_set_uint (value, gst_base_sink_get_blocksize (sink));
1240 case PROP_RENDER_DELAY:
1241 g_value_set_uint64 (value, gst_base_sink_get_render_delay (sink));
1244 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1251 gst_base_sink_get_caps (GstBaseSink * sink)
1257 gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps)
1262 static GstFlowReturn
1263 gst_base_sink_buffer_alloc (GstBaseSink * sink, guint64 offset, guint size,
1264 GstCaps * caps, GstBuffer ** buf)
1270 /* with PREROLL_LOCK, STREAM_LOCK */
1272 gst_base_sink_preroll_queue_flush (GstBaseSink * basesink, GstPad * pad)
1276 GST_DEBUG_OBJECT (basesink, "flushing queue %p", basesink);
1277 while ((obj = g_queue_pop_head (basesink->preroll_queue))) {
1278 GST_DEBUG_OBJECT (basesink, "popped %p", obj);
1279 gst_mini_object_unref (obj);
1281 /* we can't have EOS anymore now */
1282 basesink->eos = FALSE;
1283 basesink->priv->received_eos = FALSE;
1284 basesink->have_preroll = FALSE;
1285 basesink->priv->step_unlock = FALSE;
1286 basesink->eos_queued = FALSE;
1287 basesink->preroll_queued = 0;
1288 basesink->buffers_queued = 0;
1289 basesink->events_queued = 0;
1290 /* can't report latency anymore until we preroll again */
1291 if (basesink->priv->async_enabled) {
1292 GST_OBJECT_LOCK (basesink);
1293 basesink->priv->have_latency = FALSE;
1294 GST_OBJECT_UNLOCK (basesink);
1296 /* and signal any waiters now */
1297 GST_PAD_PREROLL_SIGNAL (pad);
1300 /* with STREAM_LOCK, configures given segment with the event information. */
1302 gst_base_sink_configure_segment (GstBaseSink * basesink, GstPad * pad,
1303 GstEvent * event, GstSegment * segment)
1306 gdouble rate, arate;
1312 /* the newsegment event is needed to bring the buffer timestamps to the
1313 * stream time and to drop samples outside of the playback segment. */
1314 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1315 &start, &stop, &time);
1317 /* The segment is protected with both the STREAM_LOCK and the OBJECT_LOCK.
1318 * We protect with the OBJECT_LOCK so that we can use the values to
1319 * safely answer a POSITION query. */
1320 GST_OBJECT_LOCK (basesink);
1321 gst_segment_set_newsegment_full (segment, update, rate, arate, format, start,
1324 if (format == GST_FORMAT_TIME) {
1325 GST_DEBUG_OBJECT (basesink,
1326 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1327 "format GST_FORMAT_TIME, "
1328 "%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
1329 ", time %" GST_TIME_FORMAT ", accum %" GST_TIME_FORMAT,
1330 update, rate, arate, GST_TIME_ARGS (segment->start),
1331 GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time),
1332 GST_TIME_ARGS (segment->accum));
1334 GST_DEBUG_OBJECT (basesink,
1335 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1337 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
1338 G_GINT64_FORMAT ", accum %" G_GINT64_FORMAT, update, rate, arate,
1339 segment->format, segment->start, segment->stop, segment->time,
1342 GST_OBJECT_UNLOCK (basesink);
1345 /* with PREROLL_LOCK, STREAM_LOCK */
1347 gst_base_sink_commit_state (GstBaseSink * basesink)
1349 /* commit state and proceed to next pending state */
1350 GstState current, next, pending, post_pending;
1351 gboolean post_paused = FALSE;
1352 gboolean post_async_done = FALSE;
1353 gboolean post_playing = FALSE;
1355 /* we are certainly not playing async anymore now */
1356 basesink->playing_async = FALSE;
1358 GST_OBJECT_LOCK (basesink);
1359 current = GST_STATE (basesink);
1360 next = GST_STATE_NEXT (basesink);
1361 pending = GST_STATE_PENDING (basesink);
1362 post_pending = pending;
1365 case GST_STATE_PLAYING:
1367 GstBaseSinkClass *bclass;
1368 GstStateChangeReturn ret;
1370 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1372 GST_DEBUG_OBJECT (basesink, "commiting state to PLAYING");
1374 basesink->need_preroll = FALSE;
1375 post_async_done = TRUE;
1376 basesink->priv->commited = TRUE;
1377 post_playing = TRUE;
1378 /* post PAUSED too when we were READY */
1379 if (current == GST_STATE_READY) {
1383 /* make sure we notify the subclass of async playing */
1384 if (bclass->async_play) {
1385 GST_WARNING_OBJECT (basesink, "deprecated async_play");
1386 ret = bclass->async_play (basesink);
1387 if (ret == GST_STATE_CHANGE_FAILURE)
1392 case GST_STATE_PAUSED:
1393 GST_DEBUG_OBJECT (basesink, "commiting state to PAUSED");
1395 post_async_done = TRUE;
1396 basesink->priv->commited = TRUE;
1397 post_pending = GST_STATE_VOID_PENDING;
1399 case GST_STATE_READY:
1400 case GST_STATE_NULL:
1402 case GST_STATE_VOID_PENDING:
1403 goto nothing_pending;
1408 /* we can report latency queries now */
1409 basesink->priv->have_latency = TRUE;
1411 GST_STATE (basesink) = pending;
1412 GST_STATE_NEXT (basesink) = GST_STATE_VOID_PENDING;
1413 GST_STATE_PENDING (basesink) = GST_STATE_VOID_PENDING;
1414 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_SUCCESS;
1415 GST_OBJECT_UNLOCK (basesink);
1418 GST_DEBUG_OBJECT (basesink, "posting PAUSED state change message");
1419 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1420 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1421 current, next, post_pending));
1423 if (post_async_done) {
1424 GST_DEBUG_OBJECT (basesink, "posting async-done message");
1425 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1426 gst_message_new_async_done (GST_OBJECT_CAST (basesink)));
1429 GST_DEBUG_OBJECT (basesink, "posting PLAYING state change message");
1430 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1431 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1432 next, pending, GST_STATE_VOID_PENDING));
1435 GST_STATE_BROADCAST (basesink);
1441 /* Depending on the state, set our vars. We get in this situation when the
1442 * state change function got a change to update the state vars before the
1443 * streaming thread did. This is fine but we need to make sure that we
1444 * update the need_preroll var since it was TRUE when we got here and might
1445 * become FALSE if we got to PLAYING. */
1446 GST_DEBUG_OBJECT (basesink, "nothing to commit, now in %s",
1447 gst_element_state_get_name (current));
1449 case GST_STATE_PLAYING:
1450 basesink->need_preroll = FALSE;
1452 case GST_STATE_PAUSED:
1453 basesink->need_preroll = TRUE;
1456 basesink->need_preroll = FALSE;
1457 basesink->flushing = TRUE;
1460 /* we can report latency queries now */
1461 basesink->priv->have_latency = TRUE;
1462 GST_OBJECT_UNLOCK (basesink);
1467 /* app is going to READY */
1468 GST_DEBUG_OBJECT (basesink, "stopping");
1469 basesink->need_preroll = FALSE;
1470 basesink->flushing = TRUE;
1471 GST_OBJECT_UNLOCK (basesink);
1476 GST_DEBUG_OBJECT (basesink, "async commit failed");
1477 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_FAILURE;
1478 GST_OBJECT_UNLOCK (basesink);
1484 start_stepping (GstBaseSink * sink, GstSegment * segment,
1485 GstStepInfo * pending, GstStepInfo * current)
1488 GstMessage *message;
1490 GST_DEBUG_OBJECT (sink, "update pending step");
1492 GST_OBJECT_LOCK (sink);
1493 memcpy (current, pending, sizeof (GstStepInfo));
1494 pending->valid = FALSE;
1495 GST_OBJECT_UNLOCK (sink);
1497 /* post message first */
1498 message = gst_message_new_step_start (GST_OBJECT (sink), TRUE);
1499 gst_message_set_seqnum (message, current->seqnum);
1500 gst_element_post_message (GST_ELEMENT (sink), message);
1502 /* get the running time of where we paused and remember it */
1503 current->start = gst_element_get_start_time (GST_ELEMENT_CAST (sink));
1504 gst_segment_set_running_time (segment, GST_FORMAT_TIME, current->start);
1506 /* set the new rate for the remainder of the segment */
1507 current->start_rate = segment->rate;
1508 segment->rate *= current->rate;
1509 segment->abs_rate = ABS (segment->rate);
1512 if (segment->rate > 0.0)
1513 current->start_stop = segment->stop;
1515 current->start_start = segment->start;
1517 if (current->format == GST_FORMAT_TIME) {
1518 end = current->start + current->amount;
1519 if (!current->flush) {
1520 /* update the segment clipping regions for non-flushing seeks */
1521 if (segment->rate > 0.0)
1522 segment->stop = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1525 gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1529 GST_DEBUG_OBJECT (sink, "segment now %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT,
1530 GST_TIME_ARGS (segment->start), GST_TIME_ARGS (segment->stop));
1532 GST_DEBUG_OBJECT (sink, "step started at running_time %" GST_TIME_FORMAT,
1533 GST_TIME_ARGS (current->start));
1535 if (current->amount == -1) {
1536 GST_DEBUG_OBJECT (sink, "step amount == -1, stop stepping");
1537 current->valid = FALSE;
1539 GST_DEBUG_OBJECT (sink, "step amount: %" G_GUINT64_FORMAT ", format: %s, "
1540 "rate: %f", current->amount, gst_format_get_name (current->format),
1546 stop_stepping (GstBaseSink * sink, GstSegment * segment,
1547 GstStepInfo * current, gint64 rstart, gint64 rstop)
1549 gint64 stop, position;
1550 GstMessage *message;
1552 GST_DEBUG_OBJECT (sink, "step complete");
1554 if (segment->rate > 0.0)
1559 GST_DEBUG_OBJECT (sink,
1560 "step stop at running_time %" GST_TIME_FORMAT, GST_TIME_ARGS (stop));
1563 current->duration = current->position;
1565 current->duration = stop - current->start;
1567 GST_DEBUG_OBJECT (sink, "step elapsed running_time %" GST_TIME_FORMAT,
1568 GST_TIME_ARGS (current->duration));
1570 position = current->start + current->duration;
1572 /* now move the segment to the new running time */
1573 gst_segment_set_running_time (segment, GST_FORMAT_TIME, position);
1575 if (current->flush) {
1576 /* and remove the accumulated time we flushed, start time did not change */
1577 segment->accum = current->start;
1579 /* start time is now the stepped position */
1580 gst_element_set_start_time (GST_ELEMENT_CAST (sink), position);
1583 /* restore the previous rate */
1584 segment->rate = current->start_rate;
1585 segment->abs_rate = ABS (segment->rate);
1587 if (segment->rate > 0.0)
1588 segment->stop = current->start_stop;
1590 segment->start = current->start_start;
1592 /* the clip segment is used for position report in paused... */
1593 memcpy (sink->abidata.ABI.clip_segment, segment, sizeof (GstSegment));
1595 /* post the step done when we know the stepped duration in TIME */
1597 gst_message_new_step_done (GST_OBJECT_CAST (sink), current->format,
1598 current->amount, current->rate, current->flush, current->intermediate,
1600 gst_message_set_seqnum (message, current->seqnum);
1601 gst_element_post_message (GST_ELEMENT_CAST (sink), message);
1603 if (!current->intermediate)
1604 sink->need_preroll = current->need_preroll;
1606 /* and the current step info finished and becomes invalid */
1607 current->valid = FALSE;
1611 handle_stepping (GstBaseSink * sink, GstSegment * segment,
1612 GstStepInfo * current, gint64 * cstart, gint64 * cstop, gint64 * rstart,
1615 GstBaseSinkPrivate *priv;
1616 gboolean step_end = FALSE;
1620 /* see if we need to skip this buffer because of stepping */
1621 switch (current->format) {
1622 case GST_FORMAT_TIME:
1627 if (segment->rate > 0.0) {
1635 end = current->start + current->amount;
1636 current->position = first - current->start;
1638 if (G_UNLIKELY (segment->abs_rate != 1.0))
1639 current->position /= segment->abs_rate;
1641 GST_DEBUG_OBJECT (sink,
1642 "buffer: %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT,
1643 GST_TIME_ARGS (first), GST_TIME_ARGS (last));
1644 GST_DEBUG_OBJECT (sink,
1645 "got time step %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT "/%"
1646 GST_TIME_FORMAT, GST_TIME_ARGS (current->position),
1647 GST_TIME_ARGS (last - current->start),
1648 GST_TIME_ARGS (current->amount));
1650 if ((current->flush && current->position >= current->amount)
1652 GST_DEBUG_OBJECT (sink, "step ended, we need clipping");
1654 if (segment->rate > 0.0) {
1656 *cstart = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1659 *cstop = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1662 GST_DEBUG_OBJECT (sink,
1663 "cstart %" GST_TIME_FORMAT ", rstart %" GST_TIME_FORMAT,
1664 GST_TIME_ARGS (*cstart), GST_TIME_ARGS (*rstart));
1665 GST_DEBUG_OBJECT (sink,
1666 "cstop %" GST_TIME_FORMAT ", rstop %" GST_TIME_FORMAT,
1667 GST_TIME_ARGS (*cstop), GST_TIME_ARGS (*rstop));
1670 case GST_FORMAT_BUFFERS:
1671 GST_DEBUG_OBJECT (sink,
1672 "got default step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1673 current->position, current->amount);
1675 if (current->position < current->amount) {
1676 current->position++;
1681 case GST_FORMAT_DEFAULT:
1683 GST_DEBUG_OBJECT (sink,
1684 "got unknown step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1685 current->position, current->amount);
1691 /* with STREAM_LOCK, PREROLL_LOCK
1693 * Returns TRUE if the object needs synchronisation and takes therefore
1694 * part in prerolling.
1696 * rsstart/rsstop contain the start/stop in stream time.
1697 * rrstart/rrstop contain the start/stop in running time.
1700 gst_base_sink_get_sync_times (GstBaseSink * basesink, GstMiniObject * obj,
1701 GstClockTime * rsstart, GstClockTime * rsstop,
1702 GstClockTime * rrstart, GstClockTime * rrstop, gboolean * do_sync,
1703 gboolean * stepped, GstSegment * segment, GstStepInfo * step,
1704 gboolean * step_end)
1706 GstBaseSinkClass *bclass;
1708 GstClockTime start, stop; /* raw start/stop timestamps */
1709 gint64 cstart, cstop; /* clipped raw timestamps */
1710 gint64 rstart, rstop; /* clipped timestamps converted to running time */
1711 GstClockTime sstart, sstop; /* clipped timestamps converted to stream time */
1713 GstBaseSinkPrivate *priv;
1715 priv = basesink->priv;
1717 /* start with nothing */
1720 if (G_UNLIKELY (GST_IS_EVENT (obj))) {
1721 GstEvent *event = GST_EVENT_CAST (obj);
1723 switch (GST_EVENT_TYPE (event)) {
1724 /* EOS event needs syncing */
1727 if (basesink->segment.rate >= 0.0) {
1728 sstart = sstop = priv->current_sstop;
1730 /* we have not seen a buffer yet, use the segment values */
1731 sstart = sstop = gst_segment_to_stream_time (&basesink->segment,
1732 basesink->segment.format, basesink->segment.stop);
1735 sstart = sstop = priv->current_sstart;
1737 /* we have not seen a buffer yet, use the segment values */
1738 sstart = sstop = gst_segment_to_stream_time (&basesink->segment,
1739 basesink->segment.format, basesink->segment.start);
1743 rstart = rstop = priv->eos_rtime;
1744 *do_sync = rstart != -1;
1745 GST_DEBUG_OBJECT (basesink, "sync times for EOS %" GST_TIME_FORMAT,
1746 GST_TIME_ARGS (rstart));
1747 /* if we are stepping, we end now */
1748 *step_end = step->valid;
1752 /* other events do not need syncing */
1753 /* FIXME, maybe NEWSEGMENT might need synchronisation
1754 * since the POSITION query depends on accumulated times and
1755 * we cannot accumulate the current segment before the previous
1762 /* else do buffer sync code */
1763 buffer = GST_BUFFER_CAST (obj);
1765 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1767 /* just get the times to see if we need syncing, if the start returns -1 we
1769 if (bclass->get_times)
1770 bclass->get_times (basesink, buffer, &start, &stop);
1773 /* we don't need to sync but we still want to get the timestamps for
1774 * tracking the position */
1775 gst_base_sink_get_times (basesink, buffer, &start, &stop);
1781 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
1782 ", stop: %" GST_TIME_FORMAT ", do_sync %d", GST_TIME_ARGS (start),
1783 GST_TIME_ARGS (stop), *do_sync);
1785 /* collect segment and format for code clarity */
1786 format = segment->format;
1788 /* no timestamp clipping if we did not get a TIME segment format */
1789 if (G_UNLIKELY (format != GST_FORMAT_TIME)) {
1792 /* do running and stream time in TIME format */
1793 format = GST_FORMAT_TIME;
1794 GST_LOG_OBJECT (basesink, "not time format, don't clip");
1798 /* clip, only when we know about time */
1799 if (G_UNLIKELY (!gst_segment_clip (segment, GST_FORMAT_TIME,
1800 (gint64) start, (gint64) stop, &cstart, &cstop))) {
1802 /* when we are stepping, pretend we're at the end of the segment */
1803 if (segment->rate > 0.0) {
1804 cstart = segment->stop;
1805 cstop = segment->stop;
1807 cstart = segment->start;
1808 cstop = segment->start;
1812 goto out_of_segment;
1815 if (G_UNLIKELY (start != cstart || stop != cstop)) {
1816 GST_DEBUG_OBJECT (basesink, "clipped to: start %" GST_TIME_FORMAT
1817 ", stop: %" GST_TIME_FORMAT, GST_TIME_ARGS (cstart),
1818 GST_TIME_ARGS (cstop));
1821 /* set last stop position */
1822 if (G_LIKELY (cstop != GST_CLOCK_TIME_NONE))
1823 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, cstop);
1825 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, cstart);
1828 rstart = gst_segment_to_running_time (segment, format, cstart);
1829 rstop = gst_segment_to_running_time (segment, format, cstop);
1831 if (G_UNLIKELY (step->valid)) {
1832 if (!(*step_end = handle_stepping (basesink, segment, step, &cstart, &cstop,
1833 &rstart, &rstop))) {
1834 /* step is still busy, we discard data when we are flushing */
1835 *stepped = step->flush;
1838 /* this can produce wrong values if we accumulated non-TIME segments. If this happens,
1839 * upstream is behaving very badly */
1840 sstart = gst_segment_to_stream_time (segment, format, cstart);
1841 sstop = gst_segment_to_stream_time (segment, format, cstop);
1844 /* done label only called when doing EOS, we also stop stepping then */
1845 if (*step_end && step->flush) {
1846 GST_DEBUG_OBJECT (basesink, "flushing step ended");
1847 stop_stepping (basesink, segment, step, rstart, rstop);
1857 /* buffers and EOS always need syncing and preroll */
1863 /* we usually clip in the chain function already but stepping could cause
1864 * the segment to be updated later. we return FALSE so that we don't try
1866 GST_LOG_OBJECT (basesink, "buffer skipped, not in segment");
1871 /* with STREAM_LOCK, PREROLL_LOCK, LOCK
1872 * adjust a timestamp with the latency and timestamp offset */
1874 gst_base_sink_adjust_time (GstBaseSink * basesink, GstClockTime time)
1876 GstClockTimeDiff ts_offset;
1878 /* don't do anything funny with invalid timestamps */
1879 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
1882 time += basesink->priv->latency;
1884 /* apply offset, be carefull for underflows */
1885 ts_offset = basesink->priv->ts_offset;
1886 if (ts_offset < 0) {
1887 ts_offset = -ts_offset;
1888 if (ts_offset < time)
1899 * gst_base_sink_wait_clock:
1901 * @time: the running_time to be reached
1902 * @jitter: the jitter to be filled with time diff (can be NULL)
1904 * This function will block until @time is reached. It is usually called by
1905 * subclasses that use their own internal synchronisation.
1907 * If @time is not valid, no sycnhronisation is done and #GST_CLOCK_BADTIME is
1908 * returned. Likewise, if synchronisation is disabled in the element or there
1909 * is no clock, no synchronisation is done and #GST_CLOCK_BADTIME is returned.
1911 * This function should only be called with the PREROLL_LOCK held, like when
1912 * receiving an EOS event in the ::event vmethod or when receiving a buffer in
1913 * the ::render vmethod.
1915 * The @time argument should be the running_time of when this method should
1916 * return and is not adjusted with any latency or offset configured in the
1921 * Returns: #GstClockReturn
1924 gst_base_sink_wait_clock (GstBaseSink * sink, GstClockTime time,
1925 GstClockTimeDiff * jitter)
1931 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
1934 GST_OBJECT_LOCK (sink);
1935 if (G_UNLIKELY (!sink->sync))
1938 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (sink)) == NULL))
1941 /* add base_time to running_time to get the time against the clock */
1942 time += GST_ELEMENT_CAST (sink)->base_time;
1944 id = gst_clock_new_single_shot_id (clock, time);
1945 GST_OBJECT_UNLOCK (sink);
1947 /* A blocking wait is performed on the clock. We save the ClockID
1948 * so we can unlock the entry at any time. While we are blocking, we
1949 * release the PREROLL_LOCK so that other threads can interrupt the
1951 sink->clock_id = id;
1952 /* release the preroll lock while waiting */
1953 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
1955 ret = gst_clock_id_wait (id, jitter);
1957 GST_PAD_PREROLL_LOCK (sink->sinkpad);
1958 gst_clock_id_unref (id);
1959 sink->clock_id = NULL;
1963 /* no syncing needed */
1966 GST_DEBUG_OBJECT (sink, "time not valid, no sync needed");
1967 return GST_CLOCK_BADTIME;
1971 GST_DEBUG_OBJECT (sink, "sync disabled");
1972 GST_OBJECT_UNLOCK (sink);
1973 return GST_CLOCK_BADTIME;
1977 GST_DEBUG_OBJECT (sink, "no clock, can't sync");
1978 GST_OBJECT_UNLOCK (sink);
1979 return GST_CLOCK_BADTIME;
1984 * gst_base_sink_wait_preroll:
1987 * If the #GstBaseSinkClass::render method performs its own synchronisation against
1988 * the clock it must unblock when going from PLAYING to the PAUSED state and call
1989 * this method before continuing to render the remaining data.
1991 * This function will block until a state change to PLAYING happens (in which
1992 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
1993 * to a state change to READY or a FLUSH event (in which case this function
1994 * returns #GST_FLOW_WRONG_STATE).
1996 * This function should only be called with the PREROLL_LOCK held, like in the
2001 * Returns: #GST_FLOW_OK if the preroll completed and processing can
2002 * continue. Any other return value should be returned from the render vmethod.
2005 gst_base_sink_wait_preroll (GstBaseSink * sink)
2007 sink->have_preroll = TRUE;
2008 GST_DEBUG_OBJECT (sink, "waiting in preroll for flush or PLAYING");
2009 /* block until the state changes, or we get a flush, or something */
2010 GST_PAD_PREROLL_WAIT (sink->sinkpad);
2011 sink->have_preroll = FALSE;
2012 if (G_UNLIKELY (sink->flushing))
2014 if (G_UNLIKELY (sink->priv->step_unlock))
2016 GST_DEBUG_OBJECT (sink, "continue after preroll");
2023 GST_DEBUG_OBJECT (sink, "preroll interrupted because of flush");
2024 return GST_FLOW_WRONG_STATE;
2028 sink->priv->step_unlock = FALSE;
2029 GST_DEBUG_OBJECT (sink, "preroll interrupted because of step");
2030 return GST_FLOW_STEP;
2035 * gst_base_sink_do_preroll:
2037 * @obj: the object that caused the preroll
2039 * If the @sink spawns its own thread for pulling buffers from upstream it
2040 * should call this method after it has pulled a buffer. If the element needed
2041 * to preroll, this function will perform the preroll and will then block
2042 * until the element state is changed.
2044 * This function should be called with the PREROLL_LOCK held.
2048 * Returns: #GST_FLOW_OK if the preroll completed and processing can
2049 * continue. Any other return value should be returned from the render vmethod.
2052 gst_base_sink_do_preroll (GstBaseSink * sink, GstMiniObject * obj)
2056 while (G_UNLIKELY (sink->need_preroll)) {
2057 GST_DEBUG_OBJECT (sink, "prerolling object %p", obj);
2059 ret = gst_base_sink_preroll_object (sink, obj);
2060 if (ret != GST_FLOW_OK)
2061 goto preroll_failed;
2063 /* need to recheck here because the commit state could have
2064 * made us not need the preroll anymore */
2065 if (G_LIKELY (sink->need_preroll)) {
2066 /* block until the state changes, or we get a flush, or something */
2067 ret = gst_base_sink_wait_preroll (sink);
2068 if (ret != GST_FLOW_OK) {
2069 if (ret == GST_FLOW_STEP)
2072 goto preroll_failed;
2081 GST_DEBUG_OBJECT (sink, "preroll failed %d", ret);
2087 * gst_base_sink_wait_eos:
2089 * @time: the running_time to be reached
2090 * @jitter: the jitter to be filled with time diff (can be NULL)
2092 * This function will block until @time is reached. It is usually called by
2093 * subclasses that use their own internal synchronisation but want to let the
2094 * EOS be handled by the base class.
2096 * This function should only be called with the PREROLL_LOCK held, like when
2097 * receiving an EOS event in the ::event vmethod.
2099 * The @time argument should be the running_time of when the EOS should happen
2100 * and will be adjusted with any latency and offset configured in the sink.
2104 * Returns: #GstFlowReturn
2107 gst_base_sink_wait_eos (GstBaseSink * sink, GstClockTime time,
2108 GstClockTimeDiff * jitter)
2110 GstClockReturn status;
2116 GST_DEBUG_OBJECT (sink, "checking preroll");
2118 /* first wait for the playing state before we can continue */
2119 if (G_UNLIKELY (sink->need_preroll)) {
2120 ret = gst_base_sink_wait_preroll (sink);
2121 if (ret != GST_FLOW_OK) {
2122 if (ret == GST_FLOW_STEP)
2129 /* preroll done, we can sync since we are in PLAYING now. */
2130 GST_DEBUG_OBJECT (sink, "possibly waiting for clock to reach %"
2131 GST_TIME_FORMAT, GST_TIME_ARGS (time));
2133 /* compensate for latency and ts_offset. We don't adjust for render delay
2134 * because we don't interact with the device on EOS normally. */
2135 stime = gst_base_sink_adjust_time (sink, time);
2137 /* wait for the clock, this can be interrupted because we got shut down or
2139 status = gst_base_sink_wait_clock (sink, stime, jitter);
2141 GST_DEBUG_OBJECT (sink, "clock returned %d", status);
2143 /* invalid time, no clock or sync disabled, just continue then */
2144 if (status == GST_CLOCK_BADTIME)
2147 /* waiting could have been interrupted and we can be flushing now */
2148 if (G_UNLIKELY (sink->flushing))
2151 /* retry if we got unscheduled, which means we did not reach the timeout
2152 * yet. if some other error occures, we continue. */
2153 } while (status == GST_CLOCK_UNSCHEDULED);
2155 GST_DEBUG_OBJECT (sink, "end of stream");
2162 GST_DEBUG_OBJECT (sink, "we are flushing");
2163 return GST_FLOW_WRONG_STATE;
2167 /* with STREAM_LOCK, PREROLL_LOCK
2169 * Make sure we are in PLAYING and synchronize an object to the clock.
2171 * If we need preroll, we are not in PLAYING. We try to commit the state
2172 * if needed and then block if we still are not PLAYING.
2174 * We start waiting on the clock in PLAYING. If we got interrupted, we
2175 * immediatly try to re-preroll.
2177 * Some objects do not need synchronisation (most events) and so this function
2178 * immediatly returns GST_FLOW_OK.
2180 * for objects that arrive later than max-lateness to be synchronized to the
2181 * clock have the @late boolean set to TRUE.
2183 * This function keeps a running average of the jitter (the diff between the
2184 * clock time and the requested sync time). The jitter is negative for
2185 * objects that arrive in time and positive for late buffers.
2187 * does not take ownership of obj.
2189 static GstFlowReturn
2190 gst_base_sink_do_sync (GstBaseSink * basesink, GstPad * pad,
2191 GstMiniObject * obj, gboolean * late, gboolean * step_end)
2193 GstClockTimeDiff jitter;
2195 GstClockReturn status = GST_CLOCK_OK;
2196 GstClockTime rstart, rstop, sstart, sstop, stime;
2198 GstBaseSinkPrivate *priv;
2200 GstStepInfo *current, *pending;
2203 priv = basesink->priv;
2206 sstart = sstop = rstart = rstop = -1;
2210 priv->current_rstart = -1;
2212 /* get stepping info */
2213 current = &priv->current_step;
2214 pending = &priv->pending_step;
2216 /* get timing information for this object against the render segment */
2217 syncable = gst_base_sink_get_sync_times (basesink, obj,
2218 &sstart, &sstop, &rstart, &rstop, &do_sync, &stepped, &basesink->segment,
2221 if (G_UNLIKELY (stepped))
2224 /* a syncable object needs to participate in preroll and
2225 * clocking. All buffers and EOS are syncable. */
2226 if (G_UNLIKELY (!syncable))
2229 /* store timing info for current object */
2230 priv->current_rstart = rstart;
2231 priv->current_rstop = (rstop != -1 ? rstop : rstart);
2233 /* save sync time for eos when the previous object needed sync */
2234 priv->eos_rtime = (do_sync ? priv->current_rstop : -1);
2237 /* first do preroll, this makes sure we commit our state
2238 * to PAUSED and can continue to PLAYING. We cannot perform
2239 * any clock sync in PAUSED because there is no clock. */
2240 ret = gst_base_sink_do_preroll (basesink, obj);
2241 if (G_UNLIKELY (ret != GST_FLOW_OK))
2242 goto preroll_failed;
2244 /* After rendering we store the position of the last buffer so that we can use
2245 * it to report the position. We need to take the lock here. */
2246 GST_OBJECT_LOCK (basesink);
2247 priv->current_sstart = sstart;
2248 priv->current_sstop = (sstop != -1 ? sstop : sstart);
2249 GST_OBJECT_UNLOCK (basesink);
2251 /* update the segment with a pending step if the current one is invalid and we
2252 * have a new pending one. We only accept new step updates after a preroll */
2253 if (G_UNLIKELY (pending->valid && !current->valid)) {
2254 start_stepping (basesink, &basesink->segment, pending, current);
2261 /* adjust for latency */
2262 stime = gst_base_sink_adjust_time (basesink, rstart);
2264 /* adjust for render-delay, avoid underflows */
2266 if (stime > priv->render_delay)
2267 stime -= priv->render_delay;
2272 /* preroll done, we can sync since we are in PLAYING now. */
2273 GST_DEBUG_OBJECT (basesink, "possibly waiting for clock to reach %"
2274 GST_TIME_FORMAT ", adjusted %" GST_TIME_FORMAT,
2275 GST_TIME_ARGS (rstart), GST_TIME_ARGS (stime));
2277 /* This function will return immediatly if start == -1, no clock
2278 * or sync is disabled with GST_CLOCK_BADTIME. */
2279 status = gst_base_sink_wait_clock (basesink, stime, &jitter);
2281 GST_DEBUG_OBJECT (basesink, "clock returned %d", status);
2283 /* invalid time, no clock or sync disabled, just render */
2284 if (status == GST_CLOCK_BADTIME)
2287 /* waiting could have been interrupted and we can be flushing now */
2288 if (G_UNLIKELY (basesink->flushing))
2291 /* check for unlocked by a state change, we are not flushing so
2292 * we can try to preroll on the current buffer. */
2293 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
2294 GST_DEBUG_OBJECT (basesink, "unscheduled, waiting some more");
2295 priv->call_preroll = TRUE;
2299 /* successful syncing done, record observation */
2300 priv->current_jitter = jitter;
2302 /* check if the object should be dropped */
2303 *late = gst_base_sink_is_too_late (basesink, obj, rstart, rstop,
2312 GST_DEBUG_OBJECT (basesink, "skipped stepped object %p", obj);
2318 GST_DEBUG_OBJECT (basesink, "non syncable object %p", obj);
2323 GST_DEBUG_OBJECT (basesink, "we are flushing");
2324 return GST_FLOW_WRONG_STATE;
2328 GST_DEBUG_OBJECT (basesink, "preroll failed");
2334 gst_base_sink_send_qos (GstBaseSink * basesink,
2335 gdouble proportion, GstClockTime time, GstClockTimeDiff diff)
2340 /* generate Quality-of-Service event */
2341 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2342 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2343 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (time));
2345 event = gst_event_new_qos (proportion, diff, time);
2348 res = gst_pad_push_event (basesink->sinkpad, event);
2354 gst_base_sink_perform_qos (GstBaseSink * sink, gboolean dropped)
2356 GstBaseSinkPrivate *priv;
2357 GstClockTime start, stop;
2358 GstClockTimeDiff jitter;
2359 GstClockTime pt, entered, left;
2360 GstClockTime duration;
2365 start = priv->current_rstart;
2367 if (priv->current_step.valid)
2370 /* if Quality-of-Service disabled, do nothing */
2371 if (!g_atomic_int_get (&priv->qos_enabled) || start == -1)
2374 stop = priv->current_rstop;
2375 jitter = priv->current_jitter;
2378 /* this is the time the buffer entered the sink */
2379 if (start < -jitter)
2382 entered = start + jitter;
2385 /* this is the time the buffer entered the sink */
2386 entered = start + jitter;
2387 /* this is the time the buffer left the sink */
2388 left = start + jitter;
2391 /* calculate duration of the buffer */
2393 duration = stop - start;
2397 /* if we have the time when the last buffer left us, calculate
2398 * processing time */
2399 if (priv->last_left != -1) {
2400 if (entered > priv->last_left) {
2401 pt = entered - priv->last_left;
2409 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "start: %" GST_TIME_FORMAT
2410 ", entered %" GST_TIME_FORMAT ", left %" GST_TIME_FORMAT ", pt: %"
2411 GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT ",jitter %"
2412 G_GINT64_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (entered),
2413 GST_TIME_ARGS (left), GST_TIME_ARGS (pt), GST_TIME_ARGS (duration),
2416 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "avg_duration: %" GST_TIME_FORMAT
2417 ", avg_pt: %" GST_TIME_FORMAT ", avg_rate: %g",
2418 GST_TIME_ARGS (priv->avg_duration), GST_TIME_ARGS (priv->avg_pt),
2421 /* collect running averages. for first observations, we copy the
2423 if (priv->avg_duration == -1)
2424 priv->avg_duration = duration;
2426 priv->avg_duration = UPDATE_RUNNING_AVG (priv->avg_duration, duration);
2428 if (priv->avg_pt == -1)
2431 priv->avg_pt = UPDATE_RUNNING_AVG (priv->avg_pt, pt);
2433 if (priv->avg_duration != 0)
2435 gst_guint64_to_gdouble (priv->avg_pt) /
2436 gst_guint64_to_gdouble (priv->avg_duration);
2440 if (priv->last_left != -1) {
2441 if (dropped || priv->avg_rate < 0.0) {
2442 priv->avg_rate = rate;
2445 priv->avg_rate = UPDATE_RUNNING_AVG_N (priv->avg_rate, rate);
2447 priv->avg_rate = UPDATE_RUNNING_AVG_P (priv->avg_rate, rate);
2451 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink,
2452 "updated: avg_duration: %" GST_TIME_FORMAT ", avg_pt: %" GST_TIME_FORMAT
2453 ", avg_rate: %g", GST_TIME_ARGS (priv->avg_duration),
2454 GST_TIME_ARGS (priv->avg_pt), priv->avg_rate);
2457 if (priv->avg_rate >= 0.0) {
2458 /* if we have a valid rate, start sending QoS messages */
2459 if (priv->current_jitter < 0) {
2460 /* make sure we never go below 0 when adding the jitter to the
2462 if (priv->current_rstart < -priv->current_jitter)
2463 priv->current_jitter = -priv->current_rstart;
2465 gst_base_sink_send_qos (sink, priv->avg_rate, priv->current_rstart,
2466 priv->current_jitter);
2469 /* record when this buffer will leave us */
2470 priv->last_left = left;
2473 /* reset all qos measuring */
2475 gst_base_sink_reset_qos (GstBaseSink * sink)
2477 GstBaseSinkPrivate *priv;
2481 priv->last_in_time = -1;
2482 priv->last_left = -1;
2483 priv->avg_duration = -1;
2485 priv->avg_rate = -1.0;
2486 priv->avg_render = -1;
2492 /* Checks if the object was scheduled too late.
2494 * start/stop contain the raw timestamp start and stop values
2497 * status and jitter contain the return values from the clock wait.
2499 * returns TRUE if the buffer was too late.
2502 gst_base_sink_is_too_late (GstBaseSink * basesink, GstMiniObject * obj,
2503 GstClockTime start, GstClockTime stop,
2504 GstClockReturn status, GstClockTimeDiff jitter)
2507 gint64 max_lateness;
2508 GstBaseSinkPrivate *priv;
2510 priv = basesink->priv;
2514 /* only for objects that were too late */
2515 if (G_LIKELY (status != GST_CLOCK_EARLY))
2518 max_lateness = basesink->abidata.ABI.max_lateness;
2520 /* check if frame dropping is enabled */
2521 if (max_lateness == -1)
2524 /* only check for buffers */
2525 if (G_UNLIKELY (!GST_IS_BUFFER (obj)))
2528 /* can't do check if we don't have a timestamp */
2529 if (G_UNLIKELY (start == -1))
2532 /* we can add a valid stop time */
2534 max_lateness += stop;
2536 max_lateness += start;
2538 /* if the jitter bigger than duration and lateness we are too late */
2539 if ((late = start + jitter > max_lateness)) {
2540 GST_DEBUG_OBJECT (basesink, "buffer is too late %" GST_TIME_FORMAT
2541 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (start + jitter),
2542 GST_TIME_ARGS (max_lateness));
2543 /* !!emergency!!, if we did not receive anything valid for more than a
2544 * second, render it anyway so the user sees something */
2545 if (priv->last_in_time != -1 && start - priv->last_in_time > GST_SECOND) {
2547 GST_ELEMENT_WARNING (basesink, CORE, CLOCK,
2548 (_("A lot of buffers are being dropped.")),
2549 ("There may be a timestamping problem, or this computer is too slow."));
2550 GST_DEBUG_OBJECT (basesink,
2551 "**emergency** last buffer at %" GST_TIME_FORMAT " > GST_SECOND",
2552 GST_TIME_ARGS (priv->last_in_time));
2558 priv->last_in_time = start;
2565 GST_DEBUG_OBJECT (basesink, "object was scheduled in time");
2570 GST_DEBUG_OBJECT (basesink, "frame dropping disabled");
2575 GST_DEBUG_OBJECT (basesink, "object is not a buffer");
2580 GST_DEBUG_OBJECT (basesink, "buffer has no timestamp");
2585 /* called before and after calling the render vmethod. It keeps track of how
2586 * much time was spent in the render method and is used to check if we are
2589 gst_base_sink_do_render_stats (GstBaseSink * basesink, gboolean start)
2591 GstBaseSinkPrivate *priv;
2593 priv = basesink->priv;
2596 priv->start = gst_util_get_timestamp ();
2598 GstClockTime elapsed;
2600 priv->stop = gst_util_get_timestamp ();
2602 elapsed = GST_CLOCK_DIFF (priv->start, priv->stop);
2604 if (priv->avg_render == -1)
2605 priv->avg_render = elapsed;
2607 priv->avg_render = UPDATE_RUNNING_AVG (priv->avg_render, elapsed);
2609 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2610 "avg_render: %" GST_TIME_FORMAT, GST_TIME_ARGS (priv->avg_render));
2614 /* with STREAM_LOCK, PREROLL_LOCK,
2616 * Synchronize the object on the clock and then render it.
2618 * takes ownership of obj.
2620 static GstFlowReturn
2621 gst_base_sink_render_object (GstBaseSink * basesink, GstPad * pad,
2622 GstMiniObject * obj)
2625 GstBaseSinkClass *bclass;
2626 gboolean late, step_end;
2628 GstBaseSinkPrivate *priv;
2630 priv = basesink->priv;
2637 /* synchronize this object, non syncable objects return OK
2639 ret = gst_base_sink_do_sync (basesink, pad, obj, &late, &step_end);
2640 if (G_UNLIKELY (ret != GST_FLOW_OK))
2643 /* and now render, event or buffer. */
2644 if (G_LIKELY (GST_IS_BUFFER (obj))) {
2647 /* drop late buffers unconditionally, let's hope it's unlikely */
2648 if (G_UNLIKELY (late))
2651 buf = GST_BUFFER_CAST (obj);
2653 gst_base_sink_set_last_buffer (basesink, buf);
2655 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2657 if (G_LIKELY (bclass->render)) {
2660 /* read once, to get same value before and after */
2661 do_qos = g_atomic_int_get (&priv->qos_enabled);
2663 GST_DEBUG_OBJECT (basesink, "rendering buffer %p", obj);
2665 /* record rendering time for QoS and stats */
2667 gst_base_sink_do_render_stats (basesink, TRUE);
2669 ret = bclass->render (basesink, buf);
2672 gst_base_sink_do_render_stats (basesink, FALSE);
2674 if (ret == GST_FLOW_STEP)
2680 GstEvent *event = GST_EVENT_CAST (obj);
2681 gboolean event_res = TRUE;
2684 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2686 type = GST_EVENT_TYPE (event);
2688 GST_DEBUG_OBJECT (basesink, "rendering event %p, type %s", obj,
2689 gst_event_type_get_name (type));
2692 event_res = bclass->event (basesink, event);
2694 /* when we get here we could be flushing again when the event handler calls
2695 * _wait_eos(). We have to ignore this object in that case. */
2696 if (G_UNLIKELY (basesink->flushing))
2699 if (G_LIKELY (event_res)) {
2702 seqnum = basesink->priv->seqnum = gst_event_get_seqnum (event);
2703 GST_DEBUG_OBJECT (basesink, "Got seqnum #%" G_GUINT32_FORMAT, seqnum);
2708 GstMessage *message;
2710 /* the EOS event is completely handled so we mark
2711 * ourselves as being in the EOS state. eos is also
2712 * protected by the object lock so we can read it when
2713 * answering the POSITION query. */
2714 GST_OBJECT_LOCK (basesink);
2715 basesink->eos = TRUE;
2716 GST_OBJECT_UNLOCK (basesink);
2718 /* ok, now we can post the message */
2719 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
2721 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
2722 gst_message_set_seqnum (message, seqnum);
2723 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
2726 case GST_EVENT_NEWSEGMENT:
2727 /* configure the segment */
2728 gst_base_sink_configure_segment (basesink, pad, event,
2729 &basesink->segment);
2739 /* the step ended, check if we need to activate a new step */
2740 GST_DEBUG_OBJECT (basesink, "step ended");
2741 stop_stepping (basesink, &basesink->segment, &priv->current_step,
2742 priv->current_rstart, priv->current_rstop);
2746 gst_base_sink_perform_qos (basesink, late);
2748 GST_DEBUG_OBJECT (basesink, "object unref after render %p", obj);
2749 gst_mini_object_unref (obj);
2756 GST_DEBUG_OBJECT (basesink, "do_sync returned %s", gst_flow_get_name (ret));
2762 GST_DEBUG_OBJECT (basesink, "buffer late, dropping");
2767 GST_DEBUG_OBJECT (basesink, "we are flushing, ignore object");
2768 gst_mini_object_unref (obj);
2769 return GST_FLOW_WRONG_STATE;
2773 /* with STREAM_LOCK, PREROLL_LOCK
2775 * Perform preroll on the given object. For buffers this means
2776 * calling the preroll subclass method.
2777 * If that succeeds, the state will be commited.
2779 * function does not take ownership of obj.
2781 static GstFlowReturn
2782 gst_base_sink_preroll_object (GstBaseSink * basesink, GstMiniObject * obj)
2786 GST_DEBUG_OBJECT (basesink, "prerolling object %p", obj);
2788 /* if it's a buffer, we need to call the preroll method */
2789 if (G_LIKELY (GST_IS_BUFFER (obj)) && basesink->priv->call_preroll) {
2790 GstBaseSinkClass *bclass;
2792 GstClockTime timestamp;
2794 buf = GST_BUFFER_CAST (obj);
2795 timestamp = GST_BUFFER_TIMESTAMP (buf);
2797 GST_DEBUG_OBJECT (basesink, "preroll buffer %" GST_TIME_FORMAT,
2798 GST_TIME_ARGS (timestamp));
2800 gst_base_sink_set_last_buffer (basesink, buf);
2802 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2803 if (bclass->preroll)
2804 if ((ret = bclass->preroll (basesink, buf)) != GST_FLOW_OK)
2805 goto preroll_failed;
2807 basesink->priv->call_preroll = FALSE;
2811 if (G_LIKELY (basesink->playing_async)) {
2812 if (G_UNLIKELY (!gst_base_sink_commit_state (basesink)))
2821 GST_DEBUG_OBJECT (basesink, "preroll failed, abort state");
2822 gst_element_abort_state (GST_ELEMENT_CAST (basesink));
2827 GST_DEBUG_OBJECT (basesink, "stopping while commiting state");
2828 return GST_FLOW_WRONG_STATE;
2832 /* with STREAM_LOCK, PREROLL_LOCK
2834 * Queue an object for rendering.
2835 * The first prerollable object queued will complete the preroll. If the
2836 * preroll queue if filled, we render all the objects in the queue.
2838 * This function takes ownership of the object.
2840 static GstFlowReturn
2841 gst_base_sink_queue_object_unlocked (GstBaseSink * basesink, GstPad * pad,
2842 GstMiniObject * obj, gboolean prerollable)
2844 GstFlowReturn ret = GST_FLOW_OK;
2848 if (G_UNLIKELY (basesink->need_preroll)) {
2849 if (G_LIKELY (prerollable))
2850 basesink->preroll_queued++;
2852 length = basesink->preroll_queued;
2854 GST_DEBUG_OBJECT (basesink, "now %d prerolled items", length);
2856 /* first prerollable item needs to finish the preroll */
2858 ret = gst_base_sink_preroll_object (basesink, obj);
2859 if (G_UNLIKELY (ret != GST_FLOW_OK))
2860 goto preroll_failed;
2862 /* need to recheck if we need preroll, commmit state during preroll
2863 * could have made us not need more preroll. */
2864 if (G_UNLIKELY (basesink->need_preroll)) {
2865 /* see if we can render now, if we can't add the object to the preroll
2867 if (G_UNLIKELY (length <= basesink->preroll_queue_max_len))
2872 /* we can start rendering (or blocking) the queued object
2874 q = basesink->preroll_queue;
2875 while (G_UNLIKELY (!g_queue_is_empty (q))) {
2878 o = g_queue_pop_head (q);
2879 GST_DEBUG_OBJECT (basesink, "rendering queued object %p", o);
2881 /* do something with the return value */
2882 ret = gst_base_sink_render_object (basesink, pad, o);
2883 if (ret != GST_FLOW_OK)
2884 goto dequeue_failed;
2887 /* now render the object */
2888 ret = gst_base_sink_render_object (basesink, pad, obj);
2889 basesink->preroll_queued = 0;
2896 GST_DEBUG_OBJECT (basesink, "preroll failed, reason %s",
2897 gst_flow_get_name (ret));
2898 gst_mini_object_unref (obj);
2903 /* add object to the queue and return */
2904 GST_DEBUG_OBJECT (basesink, "need more preroll data %d <= %d",
2905 length, basesink->preroll_queue_max_len);
2906 g_queue_push_tail (basesink->preroll_queue, obj);
2911 GST_DEBUG_OBJECT (basesink, "rendering queued objects failed, reason %s",
2912 gst_flow_get_name (ret));
2913 gst_mini_object_unref (obj);
2920 * This function grabs the PREROLL_LOCK and adds the object to
2923 * This function takes ownership of obj.
2925 static GstFlowReturn
2926 gst_base_sink_queue_object (GstBaseSink * basesink, GstPad * pad,
2927 GstMiniObject * obj, gboolean prerollable)
2931 GST_PAD_PREROLL_LOCK (pad);
2932 if (G_UNLIKELY (basesink->flushing))
2935 if (G_UNLIKELY (basesink->priv->received_eos))
2938 ret = gst_base_sink_queue_object_unlocked (basesink, pad, obj, prerollable);
2939 GST_PAD_PREROLL_UNLOCK (pad);
2946 GST_DEBUG_OBJECT (basesink, "sink is flushing");
2947 GST_PAD_PREROLL_UNLOCK (pad);
2948 gst_mini_object_unref (obj);
2949 return GST_FLOW_WRONG_STATE;
2953 GST_DEBUG_OBJECT (basesink,
2954 "we are EOS, dropping object, return UNEXPECTED");
2955 GST_PAD_PREROLL_UNLOCK (pad);
2956 gst_mini_object_unref (obj);
2957 return GST_FLOW_UNEXPECTED;
2962 gst_base_sink_flush_start (GstBaseSink * basesink, GstPad * pad)
2964 /* make sure we are not blocked on the clock also clear any pending
2966 gst_base_sink_set_flushing (basesink, pad, TRUE);
2968 /* we grab the stream lock but that is not needed since setting the
2969 * sink to flushing would make sure no state commit is being done
2971 GST_PAD_STREAM_LOCK (pad);
2972 gst_base_sink_reset_qos (basesink);
2973 if (basesink->priv->async_enabled) {
2974 /* and we need to commit our state again on the next
2975 * prerolled buffer */
2976 basesink->playing_async = TRUE;
2977 gst_element_lost_state (GST_ELEMENT_CAST (basesink));
2979 basesink->priv->have_latency = TRUE;
2980 basesink->need_preroll = FALSE;
2982 gst_base_sink_set_last_buffer (basesink, NULL);
2983 GST_PAD_STREAM_UNLOCK (pad);
2987 gst_base_sink_flush_stop (GstBaseSink * basesink, GstPad * pad)
2989 /* unset flushing so we can accept new data, this also flushes out any EOS
2991 gst_base_sink_set_flushing (basesink, pad, FALSE);
2993 /* for position reporting */
2994 GST_OBJECT_LOCK (basesink);
2995 basesink->priv->current_sstart = -1;
2996 basesink->priv->current_sstop = -1;
2997 basesink->priv->eos_rtime = -1;
2998 basesink->priv->call_preroll = TRUE;
2999 basesink->priv->current_step.valid = FALSE;
3000 basesink->priv->pending_step.valid = FALSE;
3001 if (basesink->pad_mode == GST_ACTIVATE_PUSH) {
3002 /* we need new segment info after the flush. */
3003 basesink->have_newsegment = FALSE;
3004 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
3005 gst_segment_init (basesink->abidata.ABI.clip_segment, GST_FORMAT_UNDEFINED);
3007 GST_OBJECT_UNLOCK (basesink);
3011 gst_base_sink_event (GstPad * pad, GstEvent * event)
3013 GstBaseSink *basesink;
3014 gboolean result = TRUE;
3015 GstBaseSinkClass *bclass;
3017 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3019 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3021 GST_DEBUG_OBJECT (basesink, "event %p (%s)", event,
3022 GST_EVENT_TYPE_NAME (event));
3024 switch (GST_EVENT_TYPE (event)) {
3029 GST_PAD_PREROLL_LOCK (pad);
3030 if (G_UNLIKELY (basesink->flushing))
3033 if (G_UNLIKELY (basesink->priv->received_eos)) {
3034 /* we can't accept anything when we are EOS */
3036 gst_event_unref (event);
3038 /* we set the received EOS flag here so that we can use it when testing if
3039 * we are prerolled and to refuse more buffers. */
3040 basesink->priv->received_eos = TRUE;
3042 /* EOS is a prerollable object, we call the unlocked version because it
3043 * does not check the received_eos flag. */
3044 ret = gst_base_sink_queue_object_unlocked (basesink, pad,
3045 GST_MINI_OBJECT_CAST (event), TRUE);
3046 if (G_UNLIKELY (ret != GST_FLOW_OK))
3049 GST_PAD_PREROLL_UNLOCK (pad);
3052 case GST_EVENT_NEWSEGMENT:
3056 GST_DEBUG_OBJECT (basesink, "newsegment %p", event);
3058 GST_PAD_PREROLL_LOCK (pad);
3059 if (G_UNLIKELY (basesink->flushing))
3062 if (G_UNLIKELY (basesink->priv->received_eos)) {
3063 /* we can't accept anything when we are EOS */
3065 gst_event_unref (event);
3067 /* the new segment is a non prerollable item and does not block anything,
3068 * we need to configure the current clipping segment and insert the event
3069 * in the queue to serialize it with the buffers for rendering. */
3070 gst_base_sink_configure_segment (basesink, pad, event,
3071 basesink->abidata.ABI.clip_segment);
3074 gst_base_sink_queue_object_unlocked (basesink, pad,
3075 GST_MINI_OBJECT_CAST (event), FALSE);
3076 if (G_UNLIKELY (ret != GST_FLOW_OK))
3079 GST_OBJECT_LOCK (basesink);
3080 basesink->have_newsegment = TRUE;
3081 GST_OBJECT_UNLOCK (basesink);
3084 GST_PAD_PREROLL_UNLOCK (pad);
3087 case GST_EVENT_FLUSH_START:
3089 bclass->event (basesink, event);
3091 GST_DEBUG_OBJECT (basesink, "flush-start %p", event);
3093 gst_base_sink_flush_start (basesink, pad);
3095 gst_event_unref (event);
3097 case GST_EVENT_FLUSH_STOP:
3099 bclass->event (basesink, event);
3101 GST_DEBUG_OBJECT (basesink, "flush-stop %p", event);
3103 gst_base_sink_flush_stop (basesink, pad);
3105 gst_event_unref (event);
3108 /* other events are sent to queue or subclass depending on if they
3109 * are serialized. */
3110 if (GST_EVENT_IS_SERIALIZED (event)) {
3111 gst_base_sink_queue_object (basesink, pad,
3112 GST_MINI_OBJECT_CAST (event), FALSE);
3115 bclass->event (basesink, event);
3116 gst_event_unref (event);
3121 gst_object_unref (basesink);
3128 GST_DEBUG_OBJECT (basesink, "we are flushing");
3129 GST_PAD_PREROLL_UNLOCK (pad);
3131 gst_event_unref (event);
3136 /* default implementation to calculate the start and end
3137 * timestamps on a buffer, subclasses can override
3140 gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
3141 GstClockTime * start, GstClockTime * end)
3143 GstClockTime timestamp, duration;
3145 timestamp = GST_BUFFER_TIMESTAMP (buffer);
3146 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
3148 /* get duration to calculate end time */
3149 duration = GST_BUFFER_DURATION (buffer);
3150 if (GST_CLOCK_TIME_IS_VALID (duration)) {
3151 *end = timestamp + duration;
3157 /* must be called with PREROLL_LOCK */
3159 gst_base_sink_needs_preroll (GstBaseSink * basesink)
3161 gboolean is_prerolled, res;
3163 /* we have 2 cases where the PREROLL_LOCK is released:
3164 * 1) we are blocking in the PREROLL_LOCK and thus are prerolled.
3165 * 2) we are syncing on the clock
3167 is_prerolled = basesink->have_preroll || basesink->priv->received_eos;
3168 res = !is_prerolled;
3170 GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d => needs preroll: %d",
3171 basesink->have_preroll, basesink->priv->received_eos, res);
3176 /* with STREAM_LOCK, PREROLL_LOCK
3178 * Takes a buffer and compare the timestamps with the last segment.
3179 * If the buffer falls outside of the segment boundaries, drop it.
3180 * Else queue the buffer for preroll and rendering.
3182 * This function takes ownership of the buffer.
3184 static GstFlowReturn
3185 gst_base_sink_chain_unlocked (GstBaseSink * basesink, GstPad * pad,
3188 GstBaseSinkClass *bclass;
3189 GstFlowReturn result;
3190 GstClockTime start = GST_CLOCK_TIME_NONE, end = GST_CLOCK_TIME_NONE;
3191 GstSegment *clip_segment;
3193 if (G_UNLIKELY (basesink->flushing))
3196 if (G_UNLIKELY (basesink->priv->received_eos))
3199 /* for code clarity */
3200 clip_segment = basesink->abidata.ABI.clip_segment;
3202 if (G_UNLIKELY (!basesink->have_newsegment)) {
3205 sync = gst_base_sink_get_sync (basesink);
3207 GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
3208 (_("Internal data flow problem.")),
3209 ("Received buffer without a new-segment. Assuming timestamps start from 0."));
3212 /* this means this sink will assume timestamps start from 0 */
3213 GST_OBJECT_LOCK (basesink);
3214 clip_segment->start = 0;
3215 clip_segment->stop = -1;
3216 basesink->segment.start = 0;
3217 basesink->segment.stop = -1;
3218 basesink->have_newsegment = TRUE;
3219 GST_OBJECT_UNLOCK (basesink);
3222 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3224 /* check if the buffer needs to be dropped, we first ask the subclass for the
3226 if (bclass->get_times)
3227 bclass->get_times (basesink, buf, &start, &end);
3230 /* if the subclass does not want sync, we use our own values so that we at
3231 * least clip the buffer to the segment */
3232 gst_base_sink_get_times (basesink, buf, &start, &end);
3235 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
3236 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
3238 /* a dropped buffer does not participate in anything */
3239 if (GST_CLOCK_TIME_IS_VALID (start) &&
3240 (clip_segment->format == GST_FORMAT_TIME)) {
3241 if (G_UNLIKELY (!gst_segment_clip (clip_segment,
3242 GST_FORMAT_TIME, (gint64) start, (gint64) end, NULL, NULL)))
3243 goto out_of_segment;
3246 /* now we can process the buffer in the queue, this function takes ownership
3248 result = gst_base_sink_queue_object_unlocked (basesink, pad,
3249 GST_MINI_OBJECT_CAST (buf), TRUE);
3256 GST_DEBUG_OBJECT (basesink, "sink is flushing");
3257 gst_buffer_unref (buf);
3258 return GST_FLOW_WRONG_STATE;
3262 GST_DEBUG_OBJECT (basesink,
3263 "we are EOS, dropping object, return UNEXPECTED");
3264 gst_buffer_unref (buf);
3265 return GST_FLOW_UNEXPECTED;
3269 GST_DEBUG_OBJECT (basesink, "dropping buffer, out of clipping segment");
3270 gst_buffer_unref (buf);
3277 static GstFlowReturn
3278 gst_base_sink_chain (GstPad * pad, GstBuffer * buf)
3280 GstBaseSink *basesink;
3281 GstFlowReturn result;
3283 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
3285 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH))
3288 GST_PAD_PREROLL_LOCK (pad);
3289 result = gst_base_sink_chain_unlocked (basesink, pad, buf);
3290 GST_PAD_PREROLL_UNLOCK (pad);
3298 GST_OBJECT_LOCK (pad);
3299 GST_WARNING_OBJECT (basesink,
3300 "Push on pad %s:%s, but it was not activated in push mode",
3301 GST_DEBUG_PAD_NAME (pad));
3302 GST_OBJECT_UNLOCK (pad);
3303 gst_buffer_unref (buf);
3304 /* we don't post an error message this will signal to the peer
3305 * pushing that EOS is reached. */
3306 result = GST_FLOW_UNEXPECTED;
3312 gst_base_sink_default_do_seek (GstBaseSink * sink, GstSegment * segment)
3314 gboolean res = TRUE;
3316 /* update our offset if the start/stop position was updated */
3317 if (segment->format == GST_FORMAT_BYTES) {
3318 segment->time = segment->start;
3319 } else if (segment->start == 0) {
3320 /* seek to start, we can implement a default for this. */
3324 GST_INFO_OBJECT (sink, "Can't do a default seek");
3330 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
3333 gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
3334 GstEvent * event, GstSegment * segment)
3336 /* By default, we try one of 2 things:
3337 * - For absolute seek positions, convert the requested position to our
3338 * configured processing format and place it in the output segment \
3339 * - For relative seek positions, convert our current (input) values to the
3340 * seek format, adjust by the relative seek offset and then convert back to
3341 * the processing format
3343 GstSeekType cur_type, stop_type;
3346 GstFormat seek_format, dest_format;
3349 gboolean res = TRUE;
3351 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3352 &cur_type, &cur, &stop_type, &stop);
3353 dest_format = segment->format;
3355 if (seek_format == dest_format) {
3356 gst_segment_set_seek (segment, rate, seek_format, flags,
3357 cur_type, cur, stop_type, stop, &update);
3361 if (cur_type != GST_SEEK_TYPE_NONE) {
3362 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3364 gst_pad_query_convert (sink->sinkpad, seek_format, cur, &dest_format,
3366 cur_type = GST_SEEK_TYPE_SET;
3369 if (res && stop_type != GST_SEEK_TYPE_NONE) {
3370 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3372 gst_pad_query_convert (sink->sinkpad, seek_format, stop, &dest_format,
3374 stop_type = GST_SEEK_TYPE_SET;
3377 /* And finally, configure our output segment in the desired format */
3378 gst_segment_set_seek (segment, rate, dest_format, flags, cur_type, cur,
3379 stop_type, stop, &update);
3388 GST_DEBUG_OBJECT (sink, "undefined format given, seek aborted.");
3393 /* perform a seek, only executed in pull mode */
3395 gst_base_sink_perform_seek (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3399 GstFormat seek_format, dest_format;
3401 GstSeekType cur_type, stop_type;
3402 gboolean seekseg_configured = FALSE;
3404 gboolean update, res = TRUE;
3405 GstSegment seeksegment;
3407 dest_format = sink->segment.format;
3410 GST_DEBUG_OBJECT (sink, "performing seek with event %p", event);
3411 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3412 &cur_type, &cur, &stop_type, &stop);
3414 flush = flags & GST_SEEK_FLAG_FLUSH;
3416 GST_DEBUG_OBJECT (sink, "performing seek without event");
3421 GST_DEBUG_OBJECT (sink, "flushing upstream");
3422 gst_pad_push_event (pad, gst_event_new_flush_start ());
3423 gst_base_sink_flush_start (sink, pad);
3425 GST_DEBUG_OBJECT (sink, "pausing pulling thread");
3428 GST_PAD_STREAM_LOCK (pad);
3430 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
3431 * copy the current segment info into the temp segment that we can actually
3432 * attempt the seek with. We only update the real segment if the seek suceeds. */
3433 if (!seekseg_configured) {
3434 memcpy (&seeksegment, &sink->segment, sizeof (GstSegment));
3436 /* now configure the final seek segment */
3438 if (sink->segment.format != seek_format) {
3439 /* OK, here's where we give the subclass a chance to convert the relative
3440 * seek into an absolute one in the processing format. We set up any
3441 * absolute seek above, before taking the stream lock. */
3442 if (!gst_base_sink_default_prepare_seek_segment (sink, event,
3444 GST_DEBUG_OBJECT (sink,
3445 "Preparing the seek failed after flushing. " "Aborting seek");
3449 /* The seek format matches our processing format, no need to ask the
3450 * the subclass to configure the segment. */
3451 gst_segment_set_seek (&seeksegment, rate, seek_format, flags,
3452 cur_type, cur, stop_type, stop, &update);
3455 /* Else, no seek event passed, so we're just (re)starting the
3460 GST_DEBUG_OBJECT (sink, "segment configured from %" G_GINT64_FORMAT
3461 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
3462 seeksegment.start, seeksegment.stop, seeksegment.last_stop);
3464 /* do the seek, segment.last_stop contains the new position. */
3465 res = gst_base_sink_default_do_seek (sink, &seeksegment);
3470 GST_DEBUG_OBJECT (sink, "stop flushing upstream");
3471 gst_pad_push_event (pad, gst_event_new_flush_stop ());
3472 gst_base_sink_flush_stop (sink, pad);
3473 } else if (res && sink->abidata.ABI.running) {
3474 /* we are running the current segment and doing a non-flushing seek,
3475 * close the segment first based on the last_stop. */
3476 GST_DEBUG_OBJECT (sink, "closing running segment %" G_GINT64_FORMAT
3477 " to %" G_GINT64_FORMAT, sink->segment.start, sink->segment.last_stop);
3480 /* The subclass must have converted the segment to the processing format
3482 if (res && seeksegment.format != dest_format) {
3483 GST_DEBUG_OBJECT (sink, "Subclass failed to prepare a seek segment "
3484 "in the correct format. Aborting seek.");
3488 /* if successfull seek, we update our real segment and push
3489 * out the new segment. */
3491 memcpy (&sink->segment, &seeksegment, sizeof (GstSegment));
3493 if (sink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3494 gst_element_post_message (GST_ELEMENT (sink),
3495 gst_message_new_segment_start (GST_OBJECT (sink),
3496 sink->segment.format, sink->segment.last_stop));
3500 sink->priv->discont = TRUE;
3501 sink->abidata.ABI.running = TRUE;
3503 GST_PAD_STREAM_UNLOCK (pad);
3509 set_step_info (GstBaseSink * sink, GstStepInfo * current, GstStepInfo * pending,
3510 guint seqnum, GstFormat format, guint64 amount, gdouble rate,
3511 gboolean flush, gboolean intermediate)
3513 GST_OBJECT_LOCK (sink);
3514 pending->seqnum = seqnum;
3515 pending->format = format;
3516 pending->amount = amount;
3517 pending->position = 0;
3518 pending->rate = rate;
3519 pending->flush = flush;
3520 pending->intermediate = intermediate;
3521 pending->valid = TRUE;
3522 /* flush invalidates the current stepping segment */
3524 current->valid = FALSE;
3525 GST_OBJECT_UNLOCK (sink);
3529 gst_base_sink_perform_step (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3531 GstBaseSinkPrivate *priv;
3532 GstBaseSinkClass *bclass;
3533 gboolean flush, intermediate;
3538 GstStepInfo *pending, *current;
3539 GstMessage *message;
3541 bclass = GST_BASE_SINK_GET_CLASS (sink);
3544 GST_DEBUG_OBJECT (sink, "performing step with event %p", event);
3546 gst_event_parse_step (event, &format, &amount, &rate, &flush, &intermediate);
3547 seqnum = gst_event_get_seqnum (event);
3549 pending = &priv->pending_step;
3550 current = &priv->current_step;
3552 /* post message first */
3553 message = gst_message_new_step_start (GST_OBJECT (sink), FALSE);
3554 gst_message_set_seqnum (message, current->seqnum);
3555 gst_element_post_message (GST_ELEMENT (sink), message);
3558 /* we need to call ::unlock before locking PREROLL_LOCK
3559 * since we lock it before going into ::render */
3561 bclass->unlock (sink);
3563 GST_PAD_PREROLL_LOCK (sink->sinkpad);
3564 /* now that we have the PREROLL lock, clear our unlock request */
3565 if (bclass->unlock_stop)
3566 bclass->unlock_stop (sink);
3568 /* update the stepinfo and make it valid */
3569 set_step_info (sink, current, pending, seqnum, format, amount, rate, flush,
3572 if (sink->priv->async_enabled) {
3573 /* and we need to commit our state again on the next
3574 * prerolled buffer */
3575 sink->playing_async = TRUE;
3576 priv->pending_step.need_preroll = TRUE;
3577 sink->need_preroll = FALSE;
3578 gst_element_lost_state_full (GST_ELEMENT_CAST (sink), FALSE);
3580 sink->priv->have_latency = TRUE;
3581 sink->need_preroll = FALSE;
3583 priv->current_sstart = -1;
3584 priv->current_sstop = -1;
3585 priv->eos_rtime = -1;
3586 priv->call_preroll = TRUE;
3587 gst_base_sink_set_last_buffer (sink, NULL);
3588 gst_base_sink_reset_qos (sink);
3590 if (sink->clock_id) {
3591 gst_clock_id_unschedule (sink->clock_id);
3594 if (sink->have_preroll) {
3595 GST_DEBUG_OBJECT (sink, "signal waiter");
3596 priv->step_unlock = TRUE;
3597 GST_PAD_PREROLL_SIGNAL (sink->sinkpad);
3599 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
3601 /* update the stepinfo and make it valid */
3602 set_step_info (sink, current, pending, seqnum, format, amount, rate, flush,
3612 gst_base_sink_loop (GstPad * pad)
3614 GstBaseSink *basesink;
3615 GstBuffer *buf = NULL;
3616 GstFlowReturn result;
3620 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
3622 g_assert (basesink->pad_mode == GST_ACTIVATE_PULL);
3624 if ((blocksize = basesink->priv->blocksize) == 0)
3627 offset = basesink->segment.last_stop;
3629 GST_DEBUG_OBJECT (basesink, "pulling %" G_GUINT64_FORMAT ", %u",
3632 result = gst_pad_pull_range (pad, offset, blocksize, &buf);
3633 if (G_UNLIKELY (result != GST_FLOW_OK))
3636 if (G_UNLIKELY (buf == NULL))
3639 offset += GST_BUFFER_SIZE (buf);
3641 gst_segment_set_last_stop (&basesink->segment, GST_FORMAT_BYTES, offset);
3643 GST_PAD_PREROLL_LOCK (pad);
3644 result = gst_base_sink_chain_unlocked (basesink, pad, buf);
3645 GST_PAD_PREROLL_UNLOCK (pad);
3646 if (G_UNLIKELY (result != GST_FLOW_OK))
3654 GST_LOG_OBJECT (basesink, "pausing task, reason %s",
3655 gst_flow_get_name (result));
3656 gst_pad_pause_task (pad);
3657 /* fatal errors and NOT_LINKED cause EOS */
3658 if (GST_FLOW_IS_FATAL (result) || result == GST_FLOW_NOT_LINKED) {
3659 if (result == GST_FLOW_UNEXPECTED) {
3660 /* perform EOS logic */
3661 if (basesink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3662 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3663 gst_message_new_segment_done (GST_OBJECT_CAST (basesink),
3664 basesink->segment.format, basesink->segment.last_stop));
3666 gst_base_sink_event (pad, gst_event_new_eos ());
3669 /* for fatal errors we post an error message, post the error
3670 * first so the app knows about the error first. */
3671 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
3672 (_("Internal data stream error.")),
3673 ("stream stopped, reason %s", gst_flow_get_name (result)));
3674 gst_base_sink_event (pad, gst_event_new_eos ());
3681 GST_LOG_OBJECT (basesink, "no buffer, pausing");
3682 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
3683 (_("Internal data flow error.")), ("element returned NULL buffer"));
3684 result = GST_FLOW_ERROR;
3690 gst_base_sink_set_flushing (GstBaseSink * basesink, GstPad * pad,
3693 GstBaseSinkClass *bclass;
3695 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3698 /* unlock any subclasses, we need to do this before grabbing the
3699 * PREROLL_LOCK since we hold this lock before going into ::render. */
3701 bclass->unlock (basesink);
3704 GST_PAD_PREROLL_LOCK (pad);
3705 basesink->flushing = flushing;
3707 /* step 1, now that we have the PREROLL lock, clear our unlock request */
3708 if (bclass->unlock_stop)
3709 bclass->unlock_stop (basesink);
3711 /* set need_preroll before we unblock the clock. If the clock is unblocked
3712 * before timing out, we can reuse the buffer for preroll. */
3713 basesink->need_preroll = TRUE;
3715 /* step 2, unblock clock sync (if any) or any other blocking thing */
3716 if (basesink->clock_id) {
3717 gst_clock_id_unschedule (basesink->clock_id);
3720 /* flush out the data thread if it's locked in finish_preroll, this will
3721 * also flush out the EOS state */
3722 GST_DEBUG_OBJECT (basesink,
3723 "flushing out data thread, need preroll to TRUE");
3724 gst_base_sink_preroll_queue_flush (basesink, pad);
3726 GST_PAD_PREROLL_UNLOCK (pad);
3732 gst_base_sink_default_activate_pull (GstBaseSink * basesink, gboolean active)
3738 result = gst_pad_start_task (basesink->sinkpad,
3739 (GstTaskFunction) gst_base_sink_loop, basesink->sinkpad);
3741 /* step 2, make sure streaming finishes */
3742 result = gst_pad_stop_task (basesink->sinkpad);
3749 gst_base_sink_pad_activate (GstPad * pad)
3751 gboolean result = FALSE;
3752 GstBaseSink *basesink;
3754 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3756 GST_DEBUG_OBJECT (basesink, "Trying pull mode first");
3758 gst_base_sink_set_flushing (basesink, pad, FALSE);
3760 /* we need to have the pull mode enabled */
3761 if (!basesink->can_activate_pull) {
3762 GST_DEBUG_OBJECT (basesink, "pull mode disabled");
3766 /* check if downstreams supports pull mode at all */
3767 if (!gst_pad_check_pull_range (pad)) {
3768 GST_DEBUG_OBJECT (basesink, "pull mode not supported");
3772 /* set the pad mode before starting the task so that it's in the
3773 * correct state for the new thread. also the sink set_caps and get_caps
3774 * function checks this */
3775 basesink->pad_mode = GST_ACTIVATE_PULL;
3777 /* we first try to negotiate a format so that when we try to activate
3778 * downstream, it knows about our format */
3779 if (!gst_base_sink_negotiate_pull (basesink)) {
3780 GST_DEBUG_OBJECT (basesink, "failed to negotiate in pull mode");
3784 /* ok activate now */
3785 if (!gst_pad_activate_pull (pad, TRUE)) {
3786 /* clear any pending caps */
3787 GST_OBJECT_LOCK (basesink);
3788 gst_caps_replace (&basesink->priv->pull_caps, NULL);
3789 GST_OBJECT_UNLOCK (basesink);
3790 GST_DEBUG_OBJECT (basesink, "failed to activate in pull mode");
3794 GST_DEBUG_OBJECT (basesink, "Success activating pull mode");
3798 /* push mode fallback */
3800 GST_DEBUG_OBJECT (basesink, "Falling back to push mode");
3801 if ((result = gst_pad_activate_push (pad, TRUE))) {
3802 GST_DEBUG_OBJECT (basesink, "Success activating push mode");
3807 GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
3808 gst_base_sink_set_flushing (basesink, pad, TRUE);
3811 gst_object_unref (basesink);
3817 gst_base_sink_pad_activate_push (GstPad * pad, gboolean active)
3820 GstBaseSink *basesink;
3822 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3825 if (!basesink->can_activate_push) {
3827 basesink->pad_mode = GST_ACTIVATE_NONE;
3830 basesink->pad_mode = GST_ACTIVATE_PUSH;
3833 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH)) {
3834 g_warning ("Internal GStreamer activation error!!!");
3837 gst_base_sink_set_flushing (basesink, pad, TRUE);
3839 basesink->pad_mode = GST_ACTIVATE_NONE;
3843 gst_object_unref (basesink);
3849 gst_base_sink_negotiate_pull (GstBaseSink * basesink)
3856 /* this returns the intersection between our caps and the peer caps. If there
3857 * is no peer, it returns NULL and we can't operate in pull mode so we can
3858 * fail the negotiation. */
3859 caps = gst_pad_get_allowed_caps (GST_BASE_SINK_PAD (basesink));
3860 if (caps == NULL || gst_caps_is_empty (caps))
3861 goto no_caps_possible;
3863 GST_DEBUG_OBJECT (basesink, "allowed caps: %" GST_PTR_FORMAT, caps);
3865 caps = gst_caps_make_writable (caps);
3866 /* get the first (prefered) format */
3867 gst_caps_truncate (caps);
3869 gst_pad_fixate_caps (GST_BASE_SINK_PAD (basesink), caps);
3871 GST_DEBUG_OBJECT (basesink, "fixated to: %" GST_PTR_FORMAT, caps);
3873 if (gst_caps_is_any (caps)) {
3874 GST_DEBUG_OBJECT (basesink, "caps were ANY after fixating, "
3876 /* neither side has template caps in this case, so they are prepared for
3877 pull() without setcaps() */
3879 } else if (gst_caps_is_fixed (caps)) {
3880 if (!gst_pad_set_caps (GST_BASE_SINK_PAD (basesink), caps))
3881 goto could_not_set_caps;
3883 GST_OBJECT_LOCK (basesink);
3884 gst_caps_replace (&basesink->priv->pull_caps, caps);
3885 GST_OBJECT_UNLOCK (basesink);
3890 gst_caps_unref (caps);
3896 GST_INFO_OBJECT (basesink, "Pipeline could not agree on caps");
3897 GST_DEBUG_OBJECT (basesink, "get_allowed_caps() returned EMPTY");
3899 gst_caps_unref (caps);
3904 GST_INFO_OBJECT (basesink, "Could not set caps: %" GST_PTR_FORMAT, caps);
3905 gst_caps_unref (caps);
3910 /* this won't get called until we implement an activate function */
3912 gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active)
3914 gboolean result = FALSE;
3915 GstBaseSink *basesink;
3916 GstBaseSinkClass *bclass;
3918 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3919 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3925 /* we mark we have a newsegment here because pull based
3926 * mode works just fine without having a newsegment before the
3928 format = GST_FORMAT_BYTES;
3930 gst_segment_init (&basesink->segment, format);
3931 gst_segment_init (basesink->abidata.ABI.clip_segment, format);
3932 GST_OBJECT_LOCK (basesink);
3933 basesink->have_newsegment = TRUE;
3934 GST_OBJECT_UNLOCK (basesink);
3936 /* get the peer duration in bytes */
3937 result = gst_pad_query_peer_duration (pad, &format, &duration);
3939 GST_DEBUG_OBJECT (basesink,
3940 "setting duration in bytes to %" G_GINT64_FORMAT, duration);
3941 gst_segment_set_duration (basesink->abidata.ABI.clip_segment, format,
3943 gst_segment_set_duration (&basesink->segment, format, duration);
3945 GST_DEBUG_OBJECT (basesink, "unknown duration");
3948 if (bclass->activate_pull)
3949 result = bclass->activate_pull (basesink, TRUE);
3954 goto activate_failed;
3957 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PULL)) {
3958 g_warning ("Internal GStreamer activation error!!!");
3961 result = gst_base_sink_set_flushing (basesink, pad, TRUE);
3962 if (bclass->activate_pull)
3963 result &= bclass->activate_pull (basesink, FALSE);
3964 basesink->pad_mode = GST_ACTIVATE_NONE;
3965 /* clear any pending caps */
3966 GST_OBJECT_LOCK (basesink);
3967 gst_caps_replace (&basesink->priv->pull_caps, NULL);
3968 GST_OBJECT_UNLOCK (basesink);
3971 gst_object_unref (basesink);
3978 /* reset, as starting the thread failed */
3979 basesink->pad_mode = GST_ACTIVATE_NONE;
3981 GST_ERROR_OBJECT (basesink, "subclass failed to activate in pull mode");
3986 /* send an event to our sinkpad peer. */
3988 gst_base_sink_send_event (GstElement * element, GstEvent * event)
3991 GstBaseSink *basesink = GST_BASE_SINK (element);
3992 gboolean forward, result = TRUE;
3993 GstActivateMode mode;
3995 GST_OBJECT_LOCK (element);
3996 /* get the pad and the scheduling mode */
3997 pad = gst_object_ref (basesink->sinkpad);
3998 mode = basesink->pad_mode;
3999 GST_OBJECT_UNLOCK (element);
4001 /* only push UPSTREAM events upstream */
4002 forward = GST_EVENT_IS_UPSTREAM (event);
4004 switch (GST_EVENT_TYPE (event)) {
4005 case GST_EVENT_LATENCY:
4007 GstClockTime latency;
4009 gst_event_parse_latency (event, &latency);
4011 /* store the latency. We use this to adjust the running_time before syncing
4012 * it to the clock. */
4013 GST_OBJECT_LOCK (element);
4014 basesink->priv->latency = latency;
4015 if (!basesink->priv->have_latency)
4017 GST_OBJECT_UNLOCK (element);
4018 GST_DEBUG_OBJECT (basesink, "latency set to %" GST_TIME_FORMAT,
4019 GST_TIME_ARGS (latency));
4021 /* We forward this event so that all elements know about the global pipeline
4022 * latency. This is interesting for an element when it wants to figure out
4023 * when a particular piece of data will be rendered. */
4026 case GST_EVENT_SEEK:
4027 /* in pull mode we will execute the seek */
4028 if (mode == GST_ACTIVATE_PULL)
4029 result = gst_base_sink_perform_seek (basesink, pad, event);
4031 case GST_EVENT_STEP:
4032 result = gst_base_sink_perform_step (basesink, pad, event);
4040 result = gst_pad_push_event (pad, event);
4042 /* not forwarded, unref the event */
4043 gst_event_unref (event);
4046 gst_object_unref (pad);
4051 gst_base_sink_peer_query (GstBaseSink * sink, GstQuery * query)
4054 gboolean res = FALSE;
4056 if ((peer = gst_pad_get_peer (sink->sinkpad))) {
4057 res = gst_pad_query (peer, query);
4058 gst_object_unref (peer);
4063 /* get the end position of the last seen object, this is used
4064 * for EOS and for making sure that we don't report a position we
4065 * have not reached yet. With LOCK. */
4067 gst_base_sink_get_position_last (GstBaseSink * basesink, GstFormat format,
4071 GstSegment *segment;
4072 gboolean ret = TRUE;
4074 segment = &basesink->segment;
4075 oformat = segment->format;
4077 if (oformat == GST_FORMAT_TIME) {
4078 /* return last observed stream time, we keep the stream time around in the
4080 *cur = basesink->priv->current_sstop;
4082 /* convert last stop to stream time */
4083 *cur = gst_segment_to_stream_time (segment, oformat, segment->last_stop);
4086 if (*cur != -1 && oformat != format) {
4087 GST_OBJECT_UNLOCK (basesink);
4088 /* convert to the target format if we need to, release lock first */
4090 gst_pad_query_convert (basesink->sinkpad, oformat, *cur, &format, cur);
4093 GST_OBJECT_LOCK (basesink);
4096 GST_DEBUG_OBJECT (basesink, "POSITION: %" GST_TIME_FORMAT,
4097 GST_TIME_ARGS (*cur));
4102 /* get the position when we are PAUSED, this is the stream time of the buffer
4103 * that prerolled. If no buffer is prerolled (we are still flushing), this
4104 * value will be -1. With LOCK. */
4106 gst_base_sink_get_position_paused (GstBaseSink * basesink, GstFormat format,
4111 GstSegment *segment;
4114 /* we don't use the clip segment in pull mode, when seeking we update the
4115 * main segment directly with the new segment values without it having to be
4116 * activated by the rendering after preroll */
4117 if (basesink->pad_mode == GST_ACTIVATE_PUSH)
4118 segment = basesink->abidata.ABI.clip_segment;
4120 segment = &basesink->segment;
4121 oformat = segment->format;
4123 if (oformat == GST_FORMAT_TIME) {
4124 *cur = basesink->priv->current_sstart;
4125 if (segment->rate < 0.0 && basesink->priv->current_sstop != -1) {
4126 /* for reverse playback we prefer the stream time stop position if we have
4128 *cur = basesink->priv->current_sstop;
4131 *cur = gst_segment_to_stream_time (segment, oformat, segment->last_stop);
4134 time = segment->time;
4137 *cur = MAX (*cur, time);
4138 GST_DEBUG_OBJECT (basesink, "POSITION as max: %" GST_TIME_FORMAT
4139 ", time %" GST_TIME_FORMAT, GST_TIME_ARGS (*cur), GST_TIME_ARGS (time));
4141 /* we have no buffer, use the segment times. */
4142 if (segment->rate >= 0.0) {
4143 /* forward, next position is always the time of the segment */
4145 GST_DEBUG_OBJECT (basesink, "POSITION as time: %" GST_TIME_FORMAT,
4146 GST_TIME_ARGS (*cur));
4148 /* reverse, next expected timestamp is segment->stop. We use the function
4149 * to get things right for negative applied_rates. */
4150 *cur = gst_segment_to_stream_time (segment, oformat, segment->stop);
4151 GST_DEBUG_OBJECT (basesink, "reverse POSITION: %" GST_TIME_FORMAT,
4152 GST_TIME_ARGS (*cur));
4157 if (res && oformat != format) {
4158 GST_OBJECT_UNLOCK (basesink);
4160 gst_pad_query_convert (basesink->sinkpad, oformat, *cur, &format, cur);
4163 GST_OBJECT_LOCK (basesink);
4170 gst_base_sink_get_position (GstBaseSink * basesink, GstFormat format,
4171 gint64 * cur, gboolean * upstream)
4174 gboolean res = FALSE;
4175 GstFormat oformat, tformat;
4176 GstClockTime now, base, latency;
4177 gint64 time, accum, duration;
4181 GST_OBJECT_LOCK (basesink);
4182 /* our intermediate time format */
4183 tformat = GST_FORMAT_TIME;
4184 /* get the format in the segment */
4185 oformat = basesink->segment.format;
4187 /* can only give answer based on the clock if not EOS */
4188 if (G_UNLIKELY (basesink->eos))
4191 /* we can only get the segment when we are not NULL or READY */
4192 if (!basesink->have_newsegment)
4195 /* when not in PLAYING or when we're busy with a state change, we
4196 * cannot read from the clock so we report time based on the
4197 * last seen timestamp. */
4198 if (GST_STATE (basesink) != GST_STATE_PLAYING ||
4199 GST_STATE_PENDING (basesink) != GST_STATE_VOID_PENDING)
4202 /* we need to sync on the clock. */
4203 if (basesink->sync == FALSE)
4206 /* and we need a clock */
4207 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (basesink)) == NULL))
4210 /* collect all data we need holding the lock */
4211 if (GST_CLOCK_TIME_IS_VALID (basesink->segment.time))
4212 time = basesink->segment.time;
4216 if (GST_CLOCK_TIME_IS_VALID (basesink->segment.stop))
4217 duration = basesink->segment.stop - basesink->segment.start;
4221 base = GST_ELEMENT_CAST (basesink)->base_time;
4222 accum = basesink->segment.accum;
4223 rate = basesink->segment.rate * basesink->segment.applied_rate;
4224 latency = basesink->priv->latency;
4226 gst_object_ref (clock);
4228 /* this function might release the LOCK */
4229 gst_base_sink_get_position_last (basesink, format, &last);
4231 /* need to release the object lock before we can get the time,
4232 * a clock might take the LOCK of the provider, which could be
4233 * a basesink subclass. */
4234 GST_OBJECT_UNLOCK (basesink);
4236 now = gst_clock_get_time (clock);
4238 if (oformat != tformat) {
4239 /* convert accum, time and duration to time */
4240 if (!gst_pad_query_convert (basesink->sinkpad, oformat, accum, &tformat,
4242 goto convert_failed;
4243 if (!gst_pad_query_convert (basesink->sinkpad, oformat, duration, &tformat,
4245 goto convert_failed;
4246 if (!gst_pad_query_convert (basesink->sinkpad, oformat, time, &tformat,
4248 goto convert_failed;
4251 /* subtract base time and accumulated time from the clock time.
4252 * Make sure we don't go negative. This is the current time in
4253 * the segment which we need to scale with the combined
4254 * rate and applied rate. */
4257 base = MIN (now, base);
4259 /* for negative rates we need to count back from from the segment
4264 *cur = time + gst_guint64_to_gdouble (now - base) * rate;
4266 /* never report more than last seen position */
4268 *cur = MIN (last, *cur);
4270 gst_object_unref (clock);
4272 GST_DEBUG_OBJECT (basesink,
4273 "now %" GST_TIME_FORMAT " - base %" GST_TIME_FORMAT " - accum %"
4274 GST_TIME_FORMAT " + time %" GST_TIME_FORMAT,
4275 GST_TIME_ARGS (now), GST_TIME_ARGS (base),
4276 GST_TIME_ARGS (accum), GST_TIME_ARGS (time));
4278 if (oformat != format) {
4279 /* convert time to final format */
4280 if (!gst_pad_query_convert (basesink->sinkpad, tformat, *cur, &format, cur))
4281 goto convert_failed;
4287 GST_DEBUG_OBJECT (basesink, "res: %d, POSITION: %" GST_TIME_FORMAT,
4288 res, GST_TIME_ARGS (*cur));
4294 GST_DEBUG_OBJECT (basesink, "position in EOS");
4295 res = gst_base_sink_get_position_last (basesink, format, cur);
4296 GST_OBJECT_UNLOCK (basesink);
4301 GST_DEBUG_OBJECT (basesink, "position in PAUSED");
4302 res = gst_base_sink_get_position_paused (basesink, format, cur);
4303 GST_OBJECT_UNLOCK (basesink);
4308 /* in NULL or READY we always return FALSE and -1 */
4309 GST_DEBUG_OBJECT (basesink, "position in wrong state, return -1");
4312 GST_OBJECT_UNLOCK (basesink);
4317 /* report last seen timestamp if any, else ask upstream to answer */
4318 if ((*cur = basesink->priv->current_sstart) != -1)
4323 GST_DEBUG_OBJECT (basesink, "no sync, res %d, POSITION %" GST_TIME_FORMAT,
4324 res, GST_TIME_ARGS (*cur));
4325 GST_OBJECT_UNLOCK (basesink);
4330 GST_DEBUG_OBJECT (basesink, "convert failed, try upstream");
4337 gst_base_sink_query (GstElement * element, GstQuery * query)
4339 gboolean res = FALSE;
4341 GstBaseSink *basesink = GST_BASE_SINK (element);
4343 switch (GST_QUERY_TYPE (query)) {
4344 case GST_QUERY_POSITION:
4348 gboolean upstream = FALSE;
4350 gst_query_parse_position (query, &format, NULL);
4352 GST_DEBUG_OBJECT (basesink, "position format %d", format);
4354 /* first try to get the position based on the clock */
4356 gst_base_sink_get_position (basesink, format, &cur, &upstream))) {
4357 gst_query_set_position (query, format, cur);
4358 } else if (upstream) {
4359 /* fallback to peer query */
4360 res = gst_base_sink_peer_query (basesink, query);
4364 case GST_QUERY_DURATION:
4366 GstFormat format, uformat;
4367 gint64 duration, uduration;
4369 gst_query_parse_duration (query, &format, NULL);
4371 GST_DEBUG_OBJECT (basesink, "duration query in format %s",
4372 gst_format_get_name (format));
4374 if (basesink->pad_mode == GST_ACTIVATE_PULL) {
4375 uformat = GST_FORMAT_BYTES;
4377 /* get the duration in bytes, in pull mode that's all we are sure to
4378 * know. We have to explicitly get this value from upstream instead of
4379 * using our cached value because it might change. Duration caching
4380 * should be done at a higher level. */
4381 res = gst_pad_query_peer_duration (basesink->sinkpad, &uformat,
4384 gst_segment_set_duration (&basesink->segment, uformat, uduration);
4385 if (format != uformat) {
4386 /* convert to the requested format */
4387 res = gst_pad_query_convert (basesink->sinkpad, uformat, uduration,
4388 &format, &duration);
4390 duration = uduration;
4393 /* set the result */
4394 gst_query_set_duration (query, format, duration);
4398 /* in push mode we simply forward upstream */
4399 res = gst_base_sink_peer_query (basesink, query);
4403 case GST_QUERY_LATENCY:
4405 gboolean live, us_live;
4406 GstClockTime min, max;
4408 if ((res = gst_base_sink_query_latency (basesink, &live, &us_live, &min,
4410 gst_query_set_latency (query, live, min, max);
4414 case GST_QUERY_JITTER:
4416 case GST_QUERY_RATE:
4417 /* gst_query_set_rate (query, basesink->segment_rate); */
4420 case GST_QUERY_SEGMENT:
4422 /* FIXME, bring start/stop to stream time */
4423 gst_query_set_segment (query, basesink->segment.rate,
4424 GST_FORMAT_TIME, basesink->segment.start, basesink->segment.stop);
4427 case GST_QUERY_SEEKING:
4428 case GST_QUERY_CONVERT:
4429 case GST_QUERY_FORMATS:
4431 res = gst_base_sink_peer_query (basesink, query);
4437 static GstStateChangeReturn
4438 gst_base_sink_change_state (GstElement * element, GstStateChange transition)
4440 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
4441 GstBaseSink *basesink = GST_BASE_SINK (element);
4442 GstBaseSinkClass *bclass;
4443 GstBaseSinkPrivate *priv;
4445 priv = basesink->priv;
4447 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4449 switch (transition) {
4450 case GST_STATE_CHANGE_NULL_TO_READY:
4452 if (!bclass->start (basesink))
4455 case GST_STATE_CHANGE_READY_TO_PAUSED:
4456 /* need to complete preroll before this state change completes, there
4457 * is no data flow in READY so we can safely assume we need to preroll. */
4458 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4459 GST_DEBUG_OBJECT (basesink, "READY to PAUSED");
4460 basesink->have_newsegment = FALSE;
4461 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
4462 gst_segment_init (basesink->abidata.ABI.clip_segment,
4463 GST_FORMAT_UNDEFINED);
4464 basesink->offset = 0;
4465 basesink->have_preroll = FALSE;
4466 priv->step_unlock = FALSE;
4467 basesink->need_preroll = TRUE;
4468 basesink->playing_async = TRUE;
4469 priv->current_sstart = -1;
4470 priv->current_sstop = -1;
4471 priv->eos_rtime = -1;
4473 basesink->eos = FALSE;
4474 priv->received_eos = FALSE;
4475 gst_base_sink_reset_qos (basesink);
4476 priv->commited = FALSE;
4477 priv->call_preroll = TRUE;
4478 priv->current_step.valid = FALSE;
4479 priv->pending_step.valid = FALSE;
4480 if (priv->async_enabled) {
4481 GST_DEBUG_OBJECT (basesink, "doing async state change");
4482 /* when async enabled, post async-start message and return ASYNC from
4483 * the state change function */
4484 ret = GST_STATE_CHANGE_ASYNC;
4485 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4486 gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
4488 priv->have_latency = TRUE;
4490 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4492 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
4493 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4494 if (!gst_base_sink_needs_preroll (basesink)) {
4495 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, don't need preroll");
4496 /* no preroll needed anymore now. */
4497 basesink->playing_async = FALSE;
4498 basesink->need_preroll = FALSE;
4499 if (basesink->eos) {
4500 GstMessage *message;
4502 /* need to post EOS message here */
4503 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
4504 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
4505 gst_message_set_seqnum (message, basesink->priv->seqnum);
4506 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
4508 GST_DEBUG_OBJECT (basesink, "signal preroll");
4509 GST_PAD_PREROLL_SIGNAL (basesink->sinkpad);
4512 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, we are not prerolled");
4513 basesink->need_preroll = TRUE;
4514 basesink->playing_async = TRUE;
4515 priv->call_preroll = TRUE;
4516 priv->commited = FALSE;
4517 if (priv->async_enabled) {
4518 GST_DEBUG_OBJECT (basesink, "doing async state change");
4519 ret = GST_STATE_CHANGE_ASYNC;
4520 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4521 gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
4524 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4531 GstStateChangeReturn bret;
4533 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4534 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
4535 goto activate_failed;
4538 switch (transition) {
4539 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
4540 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED");
4541 /* FIXME, make sure we cannot enter _render first */
4543 /* we need to call ::unlock before locking PREROLL_LOCK
4544 * since we lock it before going into ::render */
4546 bclass->unlock (basesink);
4548 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4549 /* now that we have the PREROLL lock, clear our unlock request */
4550 if (bclass->unlock_stop)
4551 bclass->unlock_stop (basesink);
4553 /* we need preroll again and we set the flag before unlocking the clockid
4554 * because if the clockid is unlocked before a current buffer expired, we
4555 * can use that buffer to preroll with */
4556 basesink->need_preroll = TRUE;
4558 if (basesink->clock_id) {
4559 gst_clock_id_unschedule (basesink->clock_id);
4562 /* if we don't have a preroll buffer we need to wait for a preroll and
4564 if (!gst_base_sink_needs_preroll (basesink)) {
4565 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, we are prerolled");
4566 basesink->playing_async = FALSE;
4568 if (GST_STATE_TARGET (GST_ELEMENT (basesink)) <= GST_STATE_READY) {
4569 ret = GST_STATE_CHANGE_SUCCESS;
4571 GST_DEBUG_OBJECT (basesink,
4572 "PLAYING to PAUSED, we are not prerolled");
4573 basesink->playing_async = TRUE;
4574 priv->commited = FALSE;
4575 priv->call_preroll = TRUE;
4576 if (priv->async_enabled) {
4577 GST_DEBUG_OBJECT (basesink, "doing async state change");
4578 ret = GST_STATE_CHANGE_ASYNC;
4579 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4580 gst_message_new_async_start (GST_OBJECT_CAST (basesink),
4585 GST_DEBUG_OBJECT (basesink, "rendered: %" G_GUINT64_FORMAT
4586 ", dropped: %" G_GUINT64_FORMAT, priv->rendered, priv->dropped);
4588 gst_base_sink_reset_qos (basesink);
4589 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4591 case GST_STATE_CHANGE_PAUSED_TO_READY:
4592 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4593 /* start by reseting our position state with the object lock so that the
4594 * position query gets the right idea. We do this before we post the
4595 * messages so that the message handlers pick this up. */
4596 GST_OBJECT_LOCK (basesink);
4597 basesink->have_newsegment = FALSE;
4598 priv->current_sstart = -1;
4599 priv->current_sstop = -1;
4600 priv->have_latency = FALSE;
4601 GST_OBJECT_UNLOCK (basesink);
4603 gst_base_sink_set_last_buffer (basesink, NULL);
4604 priv->call_preroll = FALSE;
4606 if (!priv->commited) {
4607 if (priv->async_enabled) {
4608 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, posting async-done");
4610 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4611 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
4612 GST_STATE_PLAYING, GST_STATE_PAUSED, GST_STATE_READY));
4614 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4615 gst_message_new_async_done (GST_OBJECT_CAST (basesink)));
4617 priv->commited = TRUE;
4619 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, don't need_preroll");
4621 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4623 case GST_STATE_CHANGE_READY_TO_NULL:
4625 if (!bclass->stop (basesink)) {
4626 GST_WARNING_OBJECT (basesink, "failed to stop");
4629 gst_base_sink_set_last_buffer (basesink, NULL);
4630 priv->call_preroll = FALSE;
4641 GST_DEBUG_OBJECT (basesink, "failed to start");
4642 return GST_STATE_CHANGE_FAILURE;
4646 GST_DEBUG_OBJECT (basesink,
4647 "element failed to change states -- activation problem?");
4648 return GST_STATE_CHANGE_FAILURE;