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 gboolean flush; /* if this was a flushing step */
169 gboolean intermediate; /* if this is an intermediate step */
170 gboolean need_preroll; /* if we need preroll after this step */
173 /* FIXME, some stuff in ABI.data and other in Private...
174 * Make up your mind please.
176 struct _GstBaseSinkPrivate
178 gint qos_enabled; /* ATOMIC */
179 gboolean async_enabled;
180 GstClockTimeDiff ts_offset;
181 GstClockTime render_delay;
183 /* start, stop of current buffer, stream time, used to report position */
184 GstClockTime current_sstart;
185 GstClockTime current_sstop;
187 /* start, stop and jitter of current buffer, running time */
188 GstClockTime current_rstart;
189 GstClockTime current_rstop;
190 GstClockTimeDiff current_jitter;
192 /* EOS sync time in running time */
193 GstClockTime eos_rtime;
195 /* last buffer that arrived in time, running time */
196 GstClockTime last_in_time;
197 /* when the last buffer left the sink, running time */
198 GstClockTime last_left;
200 /* running averages go here these are done on running time */
202 GstClockTime avg_duration;
205 /* these are done on system time. avg_jitter and avg_render are
206 * compared to eachother to see if the rendering time takes a
207 * huge amount of the processing, If so we are flooded with
209 GstClockTime last_left_systime;
210 GstClockTime avg_jitter;
211 GstClockTime start, stop;
212 GstClockTime avg_render;
214 /* number of rendered and dropped frames */
219 GstClockTime latency;
221 /* if we already commited the state */
224 /* when we received EOS */
225 gboolean received_eos;
227 /* when we are prerolled and able to report latency */
228 gboolean have_latency;
230 /* the last buffer we prerolled or rendered. Useful for making snapshots */
231 GstBuffer *last_buffer;
233 /* caps for pull based scheduling */
236 /* blocksize for pulling */
241 /* seqnum of the stream */
244 gboolean call_preroll;
245 gboolean step_unlock;
247 /* we have a pending and a current step operation */
248 GstStepInfo current_step;
249 GstStepInfo pending_step;
252 #define DO_RUNNING_AVG(avg,val,size) (((val) + ((size)-1) * (avg)) / (size))
254 /* generic running average, this has a neutral window size */
255 #define UPDATE_RUNNING_AVG(avg,val) DO_RUNNING_AVG(avg,val,8)
257 /* the windows for these running averages are experimentally obtained.
258 * possitive values get averaged more while negative values use a small
259 * window so we can react faster to badness. */
260 #define UPDATE_RUNNING_AVG_P(avg,val) DO_RUNNING_AVG(avg,val,16)
261 #define UPDATE_RUNNING_AVG_N(avg,val) DO_RUNNING_AVG(avg,val,4)
263 /* BaseSink properties */
265 #define DEFAULT_CAN_ACTIVATE_PULL FALSE /* fixme: enable me */
266 #define DEFAULT_CAN_ACTIVATE_PUSH TRUE
268 #define DEFAULT_PREROLL_QUEUE_LEN 0
269 #define DEFAULT_SYNC TRUE
270 #define DEFAULT_MAX_LATENESS -1
271 #define DEFAULT_QOS FALSE
272 #define DEFAULT_ASYNC TRUE
273 #define DEFAULT_TS_OFFSET 0
274 #define DEFAULT_BLOCKSIZE 4096
275 #define DEFAULT_RENDER_DELAY 0
280 PROP_PREROLL_QUEUE_LEN,
292 static GstElementClass *parent_class = NULL;
294 static void gst_base_sink_class_init (GstBaseSinkClass * klass);
295 static void gst_base_sink_init (GstBaseSink * trans, gpointer g_class);
296 static void gst_base_sink_finalize (GObject * object);
299 gst_base_sink_get_type (void)
301 static volatile gsize base_sink_type = 0;
303 if (g_once_init_enter (&base_sink_type)) {
305 static const GTypeInfo base_sink_info = {
306 sizeof (GstBaseSinkClass),
309 (GClassInitFunc) gst_base_sink_class_init,
312 sizeof (GstBaseSink),
314 (GInstanceInitFunc) gst_base_sink_init,
317 _type = g_type_register_static (GST_TYPE_ELEMENT,
318 "GstBaseSink", &base_sink_info, G_TYPE_FLAG_ABSTRACT);
319 g_once_init_leave (&base_sink_type, _type);
321 return base_sink_type;
324 static void gst_base_sink_set_property (GObject * object, guint prop_id,
325 const GValue * value, GParamSpec * pspec);
326 static void gst_base_sink_get_property (GObject * object, guint prop_id,
327 GValue * value, GParamSpec * pspec);
329 static gboolean gst_base_sink_send_event (GstElement * element,
331 static gboolean gst_base_sink_query (GstElement * element, GstQuery * query);
333 static GstCaps *gst_base_sink_get_caps (GstBaseSink * sink);
334 static gboolean gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps);
335 static GstFlowReturn gst_base_sink_buffer_alloc (GstBaseSink * sink,
336 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
337 static void gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
338 GstClockTime * start, GstClockTime * end);
339 static gboolean gst_base_sink_set_flushing (GstBaseSink * basesink,
340 GstPad * pad, gboolean flushing);
341 static gboolean gst_base_sink_default_activate_pull (GstBaseSink * basesink,
343 static gboolean gst_base_sink_default_do_seek (GstBaseSink * sink,
344 GstSegment * segment);
345 static gboolean gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
346 GstEvent * event, GstSegment * segment);
348 static GstStateChangeReturn gst_base_sink_change_state (GstElement * element,
349 GstStateChange transition);
351 static GstFlowReturn gst_base_sink_chain (GstPad * pad, GstBuffer * buffer);
352 static void gst_base_sink_loop (GstPad * pad);
353 static gboolean gst_base_sink_pad_activate (GstPad * pad);
354 static gboolean gst_base_sink_pad_activate_push (GstPad * pad, gboolean active);
355 static gboolean gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active);
356 static gboolean gst_base_sink_event (GstPad * pad, GstEvent * event);
357 static gboolean gst_base_sink_peer_query (GstBaseSink * sink, GstQuery * query);
359 static gboolean gst_base_sink_negotiate_pull (GstBaseSink * basesink);
361 /* check if an object was too late */
362 static gboolean gst_base_sink_is_too_late (GstBaseSink * basesink,
363 GstMiniObject * obj, GstClockTime start, GstClockTime stop,
364 GstClockReturn status, GstClockTimeDiff jitter);
365 static GstFlowReturn gst_base_sink_preroll_object (GstBaseSink * basesink,
366 GstMiniObject * obj);
369 gst_base_sink_class_init (GstBaseSinkClass * klass)
371 GObjectClass *gobject_class;
372 GstElementClass *gstelement_class;
374 gobject_class = G_OBJECT_CLASS (klass);
375 gstelement_class = GST_ELEMENT_CLASS (klass);
377 GST_DEBUG_CATEGORY_INIT (gst_base_sink_debug, "basesink", 0,
380 g_type_class_add_private (klass, sizeof (GstBaseSinkPrivate));
382 parent_class = g_type_class_peek_parent (klass);
384 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_sink_finalize);
385 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_base_sink_set_property);
386 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_base_sink_get_property);
388 /* FIXME, this next value should be configured using an event from the
389 * upstream element, ie, the BUFFER_SIZE event. */
390 g_object_class_install_property (gobject_class, PROP_PREROLL_QUEUE_LEN,
391 g_param_spec_uint ("preroll-queue-len", "Preroll queue length",
392 "Number of buffers to queue during preroll", 0, G_MAXUINT,
393 DEFAULT_PREROLL_QUEUE_LEN,
394 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
396 g_object_class_install_property (gobject_class, PROP_SYNC,
397 g_param_spec_boolean ("sync", "Sync", "Sync on the clock", DEFAULT_SYNC,
398 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
400 g_object_class_install_property (gobject_class, PROP_MAX_LATENESS,
401 g_param_spec_int64 ("max-lateness", "Max Lateness",
402 "Maximum number of nanoseconds that a buffer can be late before it "
403 "is dropped (-1 unlimited)", -1, G_MAXINT64, DEFAULT_MAX_LATENESS,
404 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
406 g_object_class_install_property (gobject_class, PROP_QOS,
407 g_param_spec_boolean ("qos", "Qos",
408 "Generate Quality-of-Service events upstream", DEFAULT_QOS,
409 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
413 * If set to #TRUE, the basesink will perform asynchronous state changes.
414 * When set to #FALSE, the sink will not signal the parent when it prerolls.
415 * Use this option when dealing with sparse streams or when synchronisation is
420 g_object_class_install_property (gobject_class, PROP_ASYNC,
421 g_param_spec_boolean ("async", "Async",
422 "Go asynchronously to PAUSED", DEFAULT_ASYNC,
423 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
425 * GstBaseSink:ts-offset
427 * Controls the final synchronisation, a negative value will render the buffer
428 * earlier while a positive value delays playback. This property can be
429 * used to fix synchronisation in bad files.
433 g_object_class_install_property (gobject_class, PROP_TS_OFFSET,
434 g_param_spec_int64 ("ts-offset", "TS Offset",
435 "Timestamp offset in nanoseconds", G_MININT64, G_MAXINT64,
436 DEFAULT_TS_OFFSET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
438 * GstBaseSink:last-buffer
440 * The last buffer that arrived in the sink and was used for preroll or for
441 * rendering. This property can be used to generate thumbnails. This property
442 * can be NULL when the sink has not yet received a bufer.
446 g_object_class_install_property (gobject_class, PROP_LAST_BUFFER,
447 gst_param_spec_mini_object ("last-buffer", "Last Buffer",
448 "The last buffer received in the sink", GST_TYPE_BUFFER,
449 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
451 * GstBaseSink:blocksize
453 * The amount of bytes to pull when operating in pull mode.
457 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
458 g_param_spec_uint ("blocksize", "Block size",
459 "Size in bytes to pull per buffer (0 = default)", 0, G_MAXUINT,
460 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
462 * GstBaseSink:render-delay
464 * The additional delay between synchronisation and actual rendering of the
465 * media. This property will add additional latency to the device in order to
466 * make other sinks compensate for the delay.
470 g_object_class_install_property (gobject_class, PROP_RENDER_DELAY,
471 g_param_spec_uint64 ("render-delay", "Render Delay",
472 "Additional render delay of the sink in nanoseconds", 0, G_MAXUINT64,
473 DEFAULT_RENDER_DELAY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
475 gstelement_class->change_state =
476 GST_DEBUG_FUNCPTR (gst_base_sink_change_state);
477 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_sink_send_event);
478 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_base_sink_query);
480 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_sink_get_caps);
481 klass->set_caps = GST_DEBUG_FUNCPTR (gst_base_sink_set_caps);
482 klass->buffer_alloc = GST_DEBUG_FUNCPTR (gst_base_sink_buffer_alloc);
483 klass->get_times = GST_DEBUG_FUNCPTR (gst_base_sink_get_times);
484 klass->activate_pull =
485 GST_DEBUG_FUNCPTR (gst_base_sink_default_activate_pull);
489 gst_base_sink_pad_getcaps (GstPad * pad)
491 GstBaseSinkClass *bclass;
493 GstCaps *caps = NULL;
495 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
496 bclass = GST_BASE_SINK_GET_CLASS (bsink);
498 if (bsink->pad_mode == GST_ACTIVATE_PULL) {
499 /* if we are operating in pull mode we only accept the negotiated caps */
500 GST_OBJECT_LOCK (pad);
501 if ((caps = GST_PAD_CAPS (pad)))
503 GST_OBJECT_UNLOCK (pad);
506 if (bclass->get_caps)
507 caps = bclass->get_caps (bsink);
510 GstPadTemplate *pad_template;
513 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass),
515 if (pad_template != NULL) {
516 caps = gst_caps_ref (gst_pad_template_get_caps (pad_template));
520 gst_object_unref (bsink);
526 gst_base_sink_pad_setcaps (GstPad * pad, GstCaps * caps)
528 GstBaseSinkClass *bclass;
532 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
533 bclass = GST_BASE_SINK_GET_CLASS (bsink);
535 if (res && bclass->set_caps)
536 res = bclass->set_caps (bsink, caps);
538 gst_object_unref (bsink);
544 gst_base_sink_pad_fixate (GstPad * pad, GstCaps * caps)
546 GstBaseSinkClass *bclass;
549 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
550 bclass = GST_BASE_SINK_GET_CLASS (bsink);
553 bclass->fixate (bsink, caps);
555 gst_object_unref (bsink);
559 gst_base_sink_pad_buffer_alloc (GstPad * pad, guint64 offset, guint size,
560 GstCaps * caps, GstBuffer ** buf)
562 GstBaseSinkClass *bclass;
564 GstFlowReturn result = GST_FLOW_OK;
566 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
567 bclass = GST_BASE_SINK_GET_CLASS (bsink);
569 if (bclass->buffer_alloc)
570 result = bclass->buffer_alloc (bsink, offset, size, caps, buf);
572 *buf = NULL; /* fallback in gstpad.c will allocate generic buffer */
574 gst_object_unref (bsink);
580 gst_base_sink_init (GstBaseSink * basesink, gpointer g_class)
582 GstPadTemplate *pad_template;
583 GstBaseSinkPrivate *priv;
585 basesink->priv = priv = GST_BASE_SINK_GET_PRIVATE (basesink);
588 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
589 g_return_if_fail (pad_template != NULL);
591 basesink->sinkpad = gst_pad_new_from_template (pad_template, "sink");
593 gst_pad_set_getcaps_function (basesink->sinkpad,
594 GST_DEBUG_FUNCPTR (gst_base_sink_pad_getcaps));
595 gst_pad_set_setcaps_function (basesink->sinkpad,
596 GST_DEBUG_FUNCPTR (gst_base_sink_pad_setcaps));
597 gst_pad_set_fixatecaps_function (basesink->sinkpad,
598 GST_DEBUG_FUNCPTR (gst_base_sink_pad_fixate));
599 gst_pad_set_bufferalloc_function (basesink->sinkpad,
600 GST_DEBUG_FUNCPTR (gst_base_sink_pad_buffer_alloc));
601 gst_pad_set_activate_function (basesink->sinkpad,
602 GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate));
603 gst_pad_set_activatepush_function (basesink->sinkpad,
604 GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate_push));
605 gst_pad_set_activatepull_function (basesink->sinkpad,
606 GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate_pull));
607 gst_pad_set_event_function (basesink->sinkpad,
608 GST_DEBUG_FUNCPTR (gst_base_sink_event));
609 gst_pad_set_chain_function (basesink->sinkpad,
610 GST_DEBUG_FUNCPTR (gst_base_sink_chain));
611 gst_element_add_pad (GST_ELEMENT_CAST (basesink), basesink->sinkpad);
613 basesink->pad_mode = GST_ACTIVATE_NONE;
614 basesink->preroll_queue = g_queue_new ();
615 basesink->abidata.ABI.clip_segment = gst_segment_new ();
616 priv->have_latency = FALSE;
618 basesink->can_activate_push = DEFAULT_CAN_ACTIVATE_PUSH;
619 basesink->can_activate_pull = DEFAULT_CAN_ACTIVATE_PULL;
621 basesink->sync = DEFAULT_SYNC;
622 basesink->abidata.ABI.max_lateness = DEFAULT_MAX_LATENESS;
623 g_atomic_int_set (&priv->qos_enabled, DEFAULT_QOS);
624 priv->async_enabled = DEFAULT_ASYNC;
625 priv->ts_offset = DEFAULT_TS_OFFSET;
626 priv->render_delay = DEFAULT_RENDER_DELAY;
627 priv->blocksize = DEFAULT_BLOCKSIZE;
629 GST_OBJECT_FLAG_SET (basesink, GST_ELEMENT_IS_SINK);
633 gst_base_sink_finalize (GObject * object)
635 GstBaseSink *basesink;
637 basesink = GST_BASE_SINK (object);
639 g_queue_free (basesink->preroll_queue);
640 gst_segment_free (basesink->abidata.ABI.clip_segment);
642 G_OBJECT_CLASS (parent_class)->finalize (object);
646 * gst_base_sink_set_sync:
648 * @sync: the new sync value.
650 * Configures @sink to synchronize on the clock or not. When
651 * @sync is FALSE, incomming samples will be played as fast as
652 * possible. If @sync is TRUE, the timestamps of the incomming
653 * buffers will be used to schedule the exact render time of its
659 gst_base_sink_set_sync (GstBaseSink * sink, gboolean sync)
661 g_return_if_fail (GST_IS_BASE_SINK (sink));
663 GST_OBJECT_LOCK (sink);
665 GST_OBJECT_UNLOCK (sink);
669 * gst_base_sink_get_sync:
672 * Checks if @sink is currently configured to synchronize against the
675 * Returns: TRUE if the sink is configured to synchronize against the clock.
680 gst_base_sink_get_sync (GstBaseSink * sink)
684 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
686 GST_OBJECT_LOCK (sink);
688 GST_OBJECT_UNLOCK (sink);
694 * gst_base_sink_set_max_lateness:
696 * @max_lateness: the new max lateness value.
698 * Sets the new max lateness value to @max_lateness. This value is
699 * used to decide if a buffer should be dropped or not based on the
700 * buffer timestamp and the current clock time. A value of -1 means
706 gst_base_sink_set_max_lateness (GstBaseSink * sink, gint64 max_lateness)
708 g_return_if_fail (GST_IS_BASE_SINK (sink));
710 GST_OBJECT_LOCK (sink);
711 sink->abidata.ABI.max_lateness = max_lateness;
712 GST_OBJECT_UNLOCK (sink);
716 * gst_base_sink_get_max_lateness:
719 * Gets the max lateness value. See gst_base_sink_set_max_lateness for
722 * Returns: The maximum time in nanoseconds that a buffer can be late
723 * before it is dropped and not rendered. A value of -1 means an
729 gst_base_sink_get_max_lateness (GstBaseSink * sink)
733 g_return_val_if_fail (GST_IS_BASE_SINK (sink), -1);
735 GST_OBJECT_LOCK (sink);
736 res = sink->abidata.ABI.max_lateness;
737 GST_OBJECT_UNLOCK (sink);
743 * gst_base_sink_set_qos_enabled:
745 * @enabled: the new qos value.
747 * Configures @sink to send Quality-of-Service events upstream.
752 gst_base_sink_set_qos_enabled (GstBaseSink * sink, gboolean enabled)
754 g_return_if_fail (GST_IS_BASE_SINK (sink));
756 g_atomic_int_set (&sink->priv->qos_enabled, enabled);
760 * gst_base_sink_is_qos_enabled:
763 * Checks if @sink is currently configured to send Quality-of-Service events
766 * Returns: TRUE if the sink is configured to perform Quality-of-Service.
771 gst_base_sink_is_qos_enabled (GstBaseSink * sink)
775 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
777 res = g_atomic_int_get (&sink->priv->qos_enabled);
783 * gst_base_sink_set_async_enabled:
785 * @enabled: the new async value.
787 * Configures @sink to perform all state changes asynchronusly. When async is
788 * disabled, the sink will immediatly go to PAUSED instead of waiting for a
789 * preroll buffer. This feature is usefull if the sink does not synchronize
790 * against the clock or when it is dealing with sparse streams.
795 gst_base_sink_set_async_enabled (GstBaseSink * sink, gboolean enabled)
797 g_return_if_fail (GST_IS_BASE_SINK (sink));
799 GST_PAD_PREROLL_LOCK (sink->sinkpad);
800 sink->priv->async_enabled = enabled;
801 GST_LOG_OBJECT (sink, "set async enabled to %d", enabled);
802 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
806 * gst_base_sink_is_async_enabled:
809 * Checks if @sink is currently configured to perform asynchronous state
812 * Returns: TRUE if the sink is configured to perform asynchronous state
818 gst_base_sink_is_async_enabled (GstBaseSink * sink)
822 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
824 GST_PAD_PREROLL_LOCK (sink->sinkpad);
825 res = sink->priv->async_enabled;
826 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
832 * gst_base_sink_set_ts_offset:
834 * @offset: the new offset
836 * Adjust the synchronisation of @sink with @offset. A negative value will
837 * render buffers earlier than their timestamp. A positive value will delay
838 * rendering. This function can be used to fix playback of badly timestamped
844 gst_base_sink_set_ts_offset (GstBaseSink * sink, GstClockTimeDiff offset)
846 g_return_if_fail (GST_IS_BASE_SINK (sink));
848 GST_OBJECT_LOCK (sink);
849 sink->priv->ts_offset = offset;
850 GST_LOG_OBJECT (sink, "set time offset to %" G_GINT64_FORMAT, offset);
851 GST_OBJECT_UNLOCK (sink);
855 * gst_base_sink_get_ts_offset:
858 * Get the synchronisation offset of @sink.
860 * Returns: The synchronisation offset.
865 gst_base_sink_get_ts_offset (GstBaseSink * sink)
867 GstClockTimeDiff res;
869 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
871 GST_OBJECT_LOCK (sink);
872 res = sink->priv->ts_offset;
873 GST_OBJECT_UNLOCK (sink);
879 * gst_base_sink_get_last_buffer:
882 * Get the last buffer that arrived in the sink and was used for preroll or for
883 * rendering. This property can be used to generate thumbnails.
885 * The #GstCaps on the buffer can be used to determine the type of the buffer.
887 * Returns: a #GstBuffer. gst_buffer_unref() after usage. This function returns
888 * NULL when no buffer has arrived in the sink yet or when the sink is not in
894 gst_base_sink_get_last_buffer (GstBaseSink * sink)
898 g_return_val_if_fail (GST_IS_BASE_SINK (sink), NULL);
900 GST_OBJECT_LOCK (sink);
901 if ((res = sink->priv->last_buffer))
902 gst_buffer_ref (res);
903 GST_OBJECT_UNLOCK (sink);
909 gst_base_sink_set_last_buffer (GstBaseSink * sink, GstBuffer * buffer)
913 GST_OBJECT_LOCK (sink);
914 old = sink->priv->last_buffer;
915 if (G_LIKELY (old != buffer)) {
916 GST_DEBUG_OBJECT (sink, "setting last buffer to %p", buffer);
917 if (G_LIKELY (buffer))
918 gst_buffer_ref (buffer);
919 sink->priv->last_buffer = buffer;
923 GST_OBJECT_UNLOCK (sink);
925 /* avoid unreffing with the lock because cleanup code might want to take the
928 gst_buffer_unref (old);
932 * gst_base_sink_get_latency:
935 * Get the currently configured latency.
937 * Returns: The configured latency.
942 gst_base_sink_get_latency (GstBaseSink * sink)
946 GST_OBJECT_LOCK (sink);
947 res = sink->priv->latency;
948 GST_OBJECT_UNLOCK (sink);
954 * gst_base_sink_query_latency:
956 * @live: if the sink is live
957 * @upstream_live: if an upstream element is live
958 * @min_latency: the min latency of the upstream elements
959 * @max_latency: the max latency of the upstream elements
961 * Query the sink for the latency parameters. The latency will be queried from
962 * the upstream elements. @live will be TRUE if @sink is configured to
963 * synchronize against the clock. @upstream_live will be TRUE if an upstream
966 * If both @live and @upstream_live are TRUE, the sink will want to compensate
967 * for the latency introduced by the upstream elements by setting the
968 * @min_latency to a strictly possitive value.
970 * This function is mostly used by subclasses.
972 * Returns: TRUE if the query succeeded.
977 gst_base_sink_query_latency (GstBaseSink * sink, gboolean * live,
978 gboolean * upstream_live, GstClockTime * min_latency,
979 GstClockTime * max_latency)
981 gboolean l, us_live, res, have_latency;
982 GstClockTime min, max, render_delay;
984 GstClockTime us_min, us_max;
986 /* we are live when we sync to the clock */
987 GST_OBJECT_LOCK (sink);
989 have_latency = sink->priv->have_latency;
990 render_delay = sink->priv->render_delay;
991 GST_OBJECT_UNLOCK (sink);
993 /* assume no latency */
999 GST_DEBUG_OBJECT (sink, "we are ready for LATENCY query");
1000 /* we are ready for a latency query this is when we preroll or when we are
1002 query = gst_query_new_latency ();
1004 /* ask the peer for the latency */
1005 if ((res = gst_base_sink_peer_query (sink, query))) {
1006 /* get upstream min and max latency */
1007 gst_query_parse_latency (query, &us_live, &us_min, &us_max);
1010 /* upstream live, use its latency, subclasses should use these
1011 * values to create the complete latency. */
1016 /* we need to add the render delay if we are live */
1018 min += render_delay;
1020 max += render_delay;
1023 gst_query_unref (query);
1025 GST_DEBUG_OBJECT (sink, "we are not yet ready for LATENCY query");
1029 /* not live, we tried to do the query, if it failed we return TRUE anyway */
1033 GST_DEBUG_OBJECT (sink, "latency query failed but we are not live");
1035 GST_DEBUG_OBJECT (sink, "latency query failed and we are live");
1040 GST_DEBUG_OBJECT (sink, "latency query: live: %d, have_latency %d,"
1041 " upstream: %d, min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT, l,
1042 have_latency, us_live, GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1047 *upstream_live = us_live;
1057 * gst_base_sink_set_render_delay:
1058 * @sink: a #GstBaseSink
1059 * @delay: the new delay
1061 * Set the render delay in @sink to @delay. The render delay is the time
1062 * between actual rendering of a buffer and its synchronisation time. Some
1063 * devices might delay media rendering which can be compensated for with this
1066 * After calling this function, this sink will report additional latency and
1067 * other sinks will adjust their latency to delay the rendering of their media.
1069 * This function is usually called by subclasses.
1074 gst_base_sink_set_render_delay (GstBaseSink * sink, GstClockTime delay)
1076 GstClockTime old_render_delay;
1078 g_return_if_fail (GST_IS_BASE_SINK (sink));
1080 GST_OBJECT_LOCK (sink);
1081 old_render_delay = sink->priv->render_delay;
1082 sink->priv->render_delay = delay;
1083 GST_LOG_OBJECT (sink, "set render delay to %" GST_TIME_FORMAT,
1084 GST_TIME_ARGS (delay));
1085 GST_OBJECT_UNLOCK (sink);
1087 if (delay != old_render_delay) {
1088 GST_DEBUG_OBJECT (sink, "posting latency changed");
1089 gst_element_post_message (GST_ELEMENT_CAST (sink),
1090 gst_message_new_latency (GST_OBJECT_CAST (sink)));
1095 * gst_base_sink_get_render_delay:
1096 * @sink: a #GstBaseSink
1098 * Get the render delay of @sink. see gst_base_sink_set_render_delay() for more
1099 * information about the render delay.
1101 * Returns: the render delay of @sink.
1106 gst_base_sink_get_render_delay (GstBaseSink * sink)
1108 GstClockTimeDiff res;
1110 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1112 GST_OBJECT_LOCK (sink);
1113 res = sink->priv->render_delay;
1114 GST_OBJECT_UNLOCK (sink);
1120 * gst_base_sink_set_blocksize:
1121 * @sink: a #GstBaseSink
1122 * @blocksize: the blocksize in bytes
1124 * Set the number of bytes that the sink will pull when it is operating in pull
1130 gst_base_sink_set_blocksize (GstBaseSink * sink, guint blocksize)
1132 g_return_if_fail (GST_IS_BASE_SINK (sink));
1134 GST_OBJECT_LOCK (sink);
1135 sink->priv->blocksize = blocksize;
1136 GST_LOG_OBJECT (sink, "set blocksize to %u", blocksize);
1137 GST_OBJECT_UNLOCK (sink);
1141 * gst_base_sink_get_blocksize:
1142 * @sink: a #GstBaseSink
1144 * Get the number of bytes that the sink will pull when it is operating in pull
1147 * Returns: the number of bytes @sink will pull in pull mode.
1152 gst_base_sink_get_blocksize (GstBaseSink * sink)
1156 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1158 GST_OBJECT_LOCK (sink);
1159 res = sink->priv->blocksize;
1160 GST_OBJECT_UNLOCK (sink);
1166 gst_base_sink_set_property (GObject * object, guint prop_id,
1167 const GValue * value, GParamSpec * pspec)
1169 GstBaseSink *sink = GST_BASE_SINK (object);
1172 case PROP_PREROLL_QUEUE_LEN:
1173 /* preroll lock necessary to serialize with finish_preroll */
1174 GST_PAD_PREROLL_LOCK (sink->sinkpad);
1175 sink->preroll_queue_max_len = g_value_get_uint (value);
1176 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
1179 gst_base_sink_set_sync (sink, g_value_get_boolean (value));
1181 case PROP_MAX_LATENESS:
1182 gst_base_sink_set_max_lateness (sink, g_value_get_int64 (value));
1185 gst_base_sink_set_qos_enabled (sink, g_value_get_boolean (value));
1188 gst_base_sink_set_async_enabled (sink, g_value_get_boolean (value));
1190 case PROP_TS_OFFSET:
1191 gst_base_sink_set_ts_offset (sink, g_value_get_int64 (value));
1193 case PROP_BLOCKSIZE:
1194 gst_base_sink_set_blocksize (sink, g_value_get_uint (value));
1196 case PROP_RENDER_DELAY:
1197 gst_base_sink_set_render_delay (sink, g_value_get_uint64 (value));
1200 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1206 gst_base_sink_get_property (GObject * object, guint prop_id, GValue * value,
1209 GstBaseSink *sink = GST_BASE_SINK (object);
1212 case PROP_PREROLL_QUEUE_LEN:
1213 GST_PAD_PREROLL_LOCK (sink->sinkpad);
1214 g_value_set_uint (value, sink->preroll_queue_max_len);
1215 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
1218 g_value_set_boolean (value, gst_base_sink_get_sync (sink));
1220 case PROP_MAX_LATENESS:
1221 g_value_set_int64 (value, gst_base_sink_get_max_lateness (sink));
1224 g_value_set_boolean (value, gst_base_sink_is_qos_enabled (sink));
1227 g_value_set_boolean (value, gst_base_sink_is_async_enabled (sink));
1229 case PROP_TS_OFFSET:
1230 g_value_set_int64 (value, gst_base_sink_get_ts_offset (sink));
1232 case PROP_LAST_BUFFER:
1233 gst_value_take_buffer (value, gst_base_sink_get_last_buffer (sink));
1235 case PROP_BLOCKSIZE:
1236 g_value_set_uint (value, gst_base_sink_get_blocksize (sink));
1238 case PROP_RENDER_DELAY:
1239 g_value_set_uint64 (value, gst_base_sink_get_render_delay (sink));
1242 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1249 gst_base_sink_get_caps (GstBaseSink * sink)
1255 gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps)
1260 static GstFlowReturn
1261 gst_base_sink_buffer_alloc (GstBaseSink * sink, guint64 offset, guint size,
1262 GstCaps * caps, GstBuffer ** buf)
1268 /* with PREROLL_LOCK, STREAM_LOCK */
1270 gst_base_sink_preroll_queue_flush (GstBaseSink * basesink, GstPad * pad)
1274 GST_DEBUG_OBJECT (basesink, "flushing queue %p", basesink);
1275 while ((obj = g_queue_pop_head (basesink->preroll_queue))) {
1276 GST_DEBUG_OBJECT (basesink, "popped %p", obj);
1277 gst_mini_object_unref (obj);
1279 /* we can't have EOS anymore now */
1280 basesink->eos = FALSE;
1281 basesink->priv->received_eos = FALSE;
1282 basesink->have_preroll = FALSE;
1283 basesink->priv->step_unlock = FALSE;
1284 basesink->eos_queued = FALSE;
1285 basesink->preroll_queued = 0;
1286 basesink->buffers_queued = 0;
1287 basesink->events_queued = 0;
1288 /* can't report latency anymore until we preroll again */
1289 if (basesink->priv->async_enabled) {
1290 GST_OBJECT_LOCK (basesink);
1291 basesink->priv->have_latency = FALSE;
1292 GST_OBJECT_UNLOCK (basesink);
1294 /* and signal any waiters now */
1295 GST_PAD_PREROLL_SIGNAL (pad);
1298 /* with STREAM_LOCK, configures given segment with the event information. */
1300 gst_base_sink_configure_segment (GstBaseSink * basesink, GstPad * pad,
1301 GstEvent * event, GstSegment * segment)
1304 gdouble rate, arate;
1310 /* the newsegment event is needed to bring the buffer timestamps to the
1311 * stream time and to drop samples outside of the playback segment. */
1312 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1313 &start, &stop, &time);
1315 /* The segment is protected with both the STREAM_LOCK and the OBJECT_LOCK.
1316 * We protect with the OBJECT_LOCK so that we can use the values to
1317 * safely answer a POSITION query. */
1318 GST_OBJECT_LOCK (basesink);
1319 gst_segment_set_newsegment_full (segment, update, rate, arate, format, start,
1322 if (format == GST_FORMAT_TIME) {
1323 GST_DEBUG_OBJECT (basesink,
1324 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1325 "format GST_FORMAT_TIME, "
1326 "%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
1327 ", time %" GST_TIME_FORMAT ", accum %" GST_TIME_FORMAT,
1328 update, rate, arate, GST_TIME_ARGS (segment->start),
1329 GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time),
1330 GST_TIME_ARGS (segment->accum));
1332 GST_DEBUG_OBJECT (basesink,
1333 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1335 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
1336 G_GINT64_FORMAT ", accum %" G_GINT64_FORMAT, update, rate, arate,
1337 segment->format, segment->start, segment->stop, segment->time,
1340 GST_OBJECT_UNLOCK (basesink);
1343 /* with PREROLL_LOCK, STREAM_LOCK */
1345 gst_base_sink_commit_state (GstBaseSink * basesink)
1347 /* commit state and proceed to next pending state */
1348 GstState current, next, pending, post_pending;
1349 gboolean post_paused = FALSE;
1350 gboolean post_async_done = FALSE;
1351 gboolean post_playing = FALSE;
1353 /* we are certainly not playing async anymore now */
1354 basesink->playing_async = FALSE;
1356 GST_OBJECT_LOCK (basesink);
1357 current = GST_STATE (basesink);
1358 next = GST_STATE_NEXT (basesink);
1359 pending = GST_STATE_PENDING (basesink);
1360 post_pending = pending;
1363 case GST_STATE_PLAYING:
1365 GstBaseSinkClass *bclass;
1366 GstStateChangeReturn ret;
1368 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1370 GST_DEBUG_OBJECT (basesink, "commiting state to PLAYING");
1372 basesink->need_preroll = FALSE;
1373 post_async_done = TRUE;
1374 basesink->priv->commited = TRUE;
1375 post_playing = TRUE;
1376 /* post PAUSED too when we were READY */
1377 if (current == GST_STATE_READY) {
1381 /* make sure we notify the subclass of async playing */
1382 if (bclass->async_play) {
1383 GST_WARNING_OBJECT (basesink, "deprecated async_play");
1384 ret = bclass->async_play (basesink);
1385 if (ret == GST_STATE_CHANGE_FAILURE)
1390 case GST_STATE_PAUSED:
1391 GST_DEBUG_OBJECT (basesink, "commiting state to PAUSED");
1393 post_async_done = TRUE;
1394 basesink->priv->commited = TRUE;
1395 post_pending = GST_STATE_VOID_PENDING;
1397 case GST_STATE_READY:
1398 case GST_STATE_NULL:
1400 case GST_STATE_VOID_PENDING:
1401 goto nothing_pending;
1406 /* we can report latency queries now */
1407 basesink->priv->have_latency = TRUE;
1409 GST_STATE (basesink) = pending;
1410 GST_STATE_NEXT (basesink) = GST_STATE_VOID_PENDING;
1411 GST_STATE_PENDING (basesink) = GST_STATE_VOID_PENDING;
1412 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_SUCCESS;
1413 GST_OBJECT_UNLOCK (basesink);
1416 GST_DEBUG_OBJECT (basesink, "posting PAUSED state change message");
1417 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1418 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1419 current, next, post_pending));
1421 if (post_async_done) {
1422 GST_DEBUG_OBJECT (basesink, "posting async-done message");
1423 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1424 gst_message_new_async_done (GST_OBJECT_CAST (basesink)));
1427 GST_DEBUG_OBJECT (basesink, "posting PLAYING state change message");
1428 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1429 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1430 next, pending, GST_STATE_VOID_PENDING));
1433 GST_STATE_BROADCAST (basesink);
1439 /* Depending on the state, set our vars. We get in this situation when the
1440 * state change function got a change to update the state vars before the
1441 * streaming thread did. This is fine but we need to make sure that we
1442 * update the need_preroll var since it was TRUE when we got here and might
1443 * become FALSE if we got to PLAYING. */
1444 GST_DEBUG_OBJECT (basesink, "nothing to commit, now in %s",
1445 gst_element_state_get_name (current));
1447 case GST_STATE_PLAYING:
1448 basesink->need_preroll = FALSE;
1450 case GST_STATE_PAUSED:
1451 basesink->need_preroll = TRUE;
1454 basesink->need_preroll = FALSE;
1455 basesink->flushing = TRUE;
1458 /* we can report latency queries now */
1459 basesink->priv->have_latency = TRUE;
1460 GST_OBJECT_UNLOCK (basesink);
1465 /* app is going to READY */
1466 GST_DEBUG_OBJECT (basesink, "stopping");
1467 basesink->need_preroll = FALSE;
1468 basesink->flushing = TRUE;
1469 GST_OBJECT_UNLOCK (basesink);
1474 GST_DEBUG_OBJECT (basesink, "async commit failed");
1475 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_FAILURE;
1476 GST_OBJECT_UNLOCK (basesink);
1482 start_stepping (GstBaseSink * sink, GstSegment * segment,
1483 GstStepInfo * pending, GstStepInfo * current)
1485 GST_DEBUG_OBJECT (sink, "update pending step");
1486 memcpy (current, pending, sizeof (GstStepInfo));
1487 pending->valid = FALSE;
1489 /* get the running time of where we paused and remember it */
1490 current->start = gst_element_get_start_time (GST_ELEMENT_CAST (sink));
1491 gst_segment_set_running_time (segment, GST_FORMAT_TIME, current->start);
1493 /* set the new rate for the remainder of the segment */
1494 current->start_rate = segment->rate;
1495 segment->rate *= current->rate;
1496 segment->abs_rate = ABS (segment->rate);
1498 GST_DEBUG_OBJECT (sink, "step started at running_time %" GST_TIME_FORMAT,
1499 GST_TIME_ARGS (current->start));
1501 if (current->amount == -1) {
1502 GST_DEBUG_OBJECT (sink, "step amount == -1, stop stepping");
1503 current->valid = FALSE;
1505 GST_DEBUG_OBJECT (sink, "step amount: %" G_GUINT64_FORMAT ", format: %s, "
1506 "rate: %f", current->amount, gst_format_get_name (current->format),
1512 stop_stepping (GstBaseSink * sink, GstSegment * segment,
1513 GstStepInfo * current, guint64 cstart, guint64 cstop, gint64 * rstart,
1516 gint64 stop, position;
1517 GstMessage *message;
1519 GST_DEBUG_OBJECT (sink, "step complete");
1521 if (segment->rate > 0.0)
1526 GST_DEBUG_OBJECT (sink,
1527 "step stop at running_time %" GST_TIME_FORMAT, GST_TIME_ARGS (stop));
1530 current->duration = current->position;
1532 current->duration = stop - current->start;
1534 GST_DEBUG_OBJECT (sink, "step elapsed running_time %" GST_TIME_FORMAT,
1535 GST_TIME_ARGS (current->duration));
1537 /* now move the segment to the new running time */
1538 position = current->start + current->duration;
1539 gst_segment_set_running_time (segment, GST_FORMAT_TIME, position);
1540 gst_element_set_start_time (GST_ELEMENT_CAST (sink), position);
1542 /* restore the previous rate */
1543 segment->rate = current->start_rate;
1544 segment->abs_rate = ABS (segment->rate);
1546 if (current->flush) {
1547 /* and remove the accumulated time we flushed */
1548 segment->accum = current->start;
1551 /* the clip segment is used for position report in paused... */
1552 memcpy (sink->abidata.ABI.clip_segment, segment, sizeof (GstSegment));
1554 /* post the step done when we know the stepped duration in TIME */
1556 gst_message_new_step_done (GST_OBJECT_CAST (sink), current->format,
1557 current->amount, current->rate, current->flush, current->intermediate,
1559 gst_message_set_seqnum (message, current->seqnum);
1560 gst_element_post_message (GST_ELEMENT_CAST (sink), message);
1562 if (!current->intermediate)
1563 sink->need_preroll = current->need_preroll;
1565 /* and the current step info finished and becomes invalid */
1566 current->valid = FALSE;
1570 handle_stepping (GstBaseSink * sink, GstSegment * segment,
1571 GstStepInfo * current, gint64 * cstart, gint64 * cstop, gint64 * rstart,
1574 GstBaseSinkPrivate *priv;
1575 gboolean step_end = FALSE;
1579 /* see if we need to skip this buffer because of stepping */
1580 switch (current->format) {
1581 case GST_FORMAT_TIME:
1586 if (segment->rate > 0.0) {
1594 end = current->start + current->amount;
1595 current->position = first - current->start;
1597 if (G_UNLIKELY (segment->abs_rate != 1.0))
1598 current->position /= segment->abs_rate;
1600 GST_DEBUG_OBJECT (sink,
1601 "buffer: %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT,
1602 GST_TIME_ARGS (first), GST_TIME_ARGS (last));
1603 GST_DEBUG_OBJECT (sink,
1604 "got time step %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT "/%"
1605 GST_TIME_FORMAT, GST_TIME_ARGS (current->position),
1606 GST_TIME_ARGS (last - current->start),
1607 GST_TIME_ARGS (current->amount));
1609 if (current->position >= current->amount || last >= end) {
1610 GST_DEBUG_OBJECT (sink, "step ended, we need clipping");
1612 if (segment->rate > 0.0) {
1614 *cstart = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1617 *cstop = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1620 GST_DEBUG_OBJECT (sink,
1621 "cstart %" GST_TIME_FORMAT ", rstart %" GST_TIME_FORMAT,
1622 GST_TIME_ARGS (*cstart), GST_TIME_ARGS (*rstart));
1623 GST_DEBUG_OBJECT (sink,
1624 "cstop %" GST_TIME_FORMAT ", rstop %" GST_TIME_FORMAT,
1625 GST_TIME_ARGS (*cstop), GST_TIME_ARGS (*rstop));
1628 case GST_FORMAT_BUFFERS:
1629 GST_DEBUG_OBJECT (sink,
1630 "got default step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1631 current->position, current->amount);
1633 if (current->position < current->amount) {
1634 current->position++;
1639 case GST_FORMAT_DEFAULT:
1641 GST_DEBUG_OBJECT (sink,
1642 "got unknown step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1643 current->position, current->amount);
1649 /* with STREAM_LOCK, PREROLL_LOCK
1651 * Returns TRUE if the object needs synchronisation and takes therefore
1652 * part in prerolling.
1654 * rsstart/rsstop contain the start/stop in stream time.
1655 * rrstart/rrstop contain the start/stop in running time.
1658 gst_base_sink_get_sync_times (GstBaseSink * basesink, GstMiniObject * obj,
1659 GstClockTime * rsstart, GstClockTime * rsstop,
1660 GstClockTime * rrstart, GstClockTime * rrstop, gboolean * do_sync,
1661 gboolean * stepped, GstSegment * segment, GstStepInfo * step)
1663 GstBaseSinkClass *bclass;
1665 GstClockTime start, stop; /* raw start/stop timestamps */
1666 gint64 cstart, cstop; /* clipped raw timestamps */
1667 gint64 rstart, rstop; /* clipped timestamps converted to running time */
1668 GstClockTime sstart, sstop; /* clipped timestamps converted to stream time */
1670 GstBaseSinkPrivate *priv;
1673 priv = basesink->priv;
1675 /* start with nothing */
1680 if (G_UNLIKELY (GST_IS_EVENT (obj))) {
1681 GstEvent *event = GST_EVENT_CAST (obj);
1683 switch (GST_EVENT_TYPE (event)) {
1684 /* EOS event needs syncing */
1687 if (basesink->segment.rate >= 0.0) {
1688 sstart = sstop = priv->current_sstop;
1690 /* we have not seen a buffer yet, use the segment values */
1691 sstart = sstop = gst_segment_to_stream_time (&basesink->segment,
1692 basesink->segment.format, basesink->segment.stop);
1695 sstart = sstop = priv->current_sstart;
1697 /* we have not seen a buffer yet, use the segment values */
1698 sstart = sstop = gst_segment_to_stream_time (&basesink->segment,
1699 basesink->segment.format, basesink->segment.start);
1703 rstart = rstop = priv->eos_rtime;
1704 *do_sync = rstart != -1;
1705 GST_DEBUG_OBJECT (basesink, "sync times for EOS %" GST_TIME_FORMAT,
1706 GST_TIME_ARGS (rstart));
1707 /* if we are stepping, we end now */
1708 step_end = step->valid;
1712 /* other events do not need syncing */
1713 /* FIXME, maybe NEWSEGMENT might need synchronisation
1714 * since the POSITION query depends on accumulated times and
1715 * we cannot accumulate the current segment before the previous
1722 /* else do buffer sync code */
1723 buffer = GST_BUFFER_CAST (obj);
1725 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1727 /* just get the times to see if we need syncing, if the start returns -1 we
1729 if (bclass->get_times)
1730 bclass->get_times (basesink, buffer, &start, &stop);
1733 /* we don't need to sync but we still want to get the timestamps for
1734 * tracking the position */
1735 gst_base_sink_get_times (basesink, buffer, &start, &stop);
1741 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
1742 ", stop: %" GST_TIME_FORMAT ", do_sync %d", GST_TIME_ARGS (start),
1743 GST_TIME_ARGS (stop), *do_sync);
1745 /* collect segment and format for code clarity */
1746 format = segment->format;
1748 /* no timestamp clipping if we did not get a TIME segment format */
1749 if (G_UNLIKELY (format != GST_FORMAT_TIME)) {
1752 /* do running and stream time in TIME format */
1753 format = GST_FORMAT_TIME;
1754 GST_LOG_OBJECT (basesink, "not time format, don't clip");
1758 /* clip, only when we know about time */
1759 if (G_UNLIKELY (!gst_segment_clip (segment, GST_FORMAT_TIME,
1760 (gint64) start, (gint64) stop, &cstart, &cstop)))
1761 goto out_of_segment;
1763 if (G_UNLIKELY (start != cstart || stop != cstop)) {
1764 GST_DEBUG_OBJECT (basesink, "clipped to: start %" GST_TIME_FORMAT
1765 ", stop: %" GST_TIME_FORMAT, GST_TIME_ARGS (cstart),
1766 GST_TIME_ARGS (cstop));
1769 /* set last stop position */
1770 if (G_LIKELY (cstop != GST_CLOCK_TIME_NONE))
1771 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, cstop);
1773 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, cstart);
1776 rstart = gst_segment_to_running_time (segment, format, cstart);
1777 rstop = gst_segment_to_running_time (segment, format, cstop);
1779 if (G_UNLIKELY (step->valid)) {
1780 if (!(step_end = handle_stepping (basesink, segment, step, &cstart, &cstop,
1781 &rstart, &rstop))) {
1782 /* step is still busy, we discard data when we are flushing */
1783 *stepped = step->flush;
1786 /* this can produce wrong values if we accumulated non-TIME segments. If this happens,
1787 * upstream is behaving very badly */
1788 sstart = gst_segment_to_stream_time (segment, format, cstart);
1789 sstop = gst_segment_to_stream_time (segment, format, cstop);
1792 /* done label only called when doing EOS, we also stop stepping then */
1794 stop_stepping (basesink, segment, step, cstart, cstop, &rstart, &rstop);
1802 /* buffers and EOS always need syncing and preroll */
1808 /* we usually clip in the chain function already but stepping could cause
1809 * the segment to be updated later. we return FALSE so that we don't try
1811 GST_LOG_OBJECT (basesink, "buffer skipped, not in segment");
1816 /* with STREAM_LOCK, PREROLL_LOCK, LOCK
1817 * adjust a timestamp with the latency and timestamp offset */
1819 gst_base_sink_adjust_time (GstBaseSink * basesink, GstClockTime time)
1821 GstClockTimeDiff ts_offset;
1823 /* don't do anything funny with invalid timestamps */
1824 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
1827 time += basesink->priv->latency;
1829 /* apply offset, be carefull for underflows */
1830 ts_offset = basesink->priv->ts_offset;
1831 if (ts_offset < 0) {
1832 ts_offset = -ts_offset;
1833 if (ts_offset < time)
1844 * gst_base_sink_wait_clock:
1846 * @time: the running_time to be reached
1847 * @jitter: the jitter to be filled with time diff (can be NULL)
1849 * This function will block until @time is reached. It is usually called by
1850 * subclasses that use their own internal synchronisation.
1852 * If @time is not valid, no sycnhronisation is done and #GST_CLOCK_BADTIME is
1853 * returned. Likewise, if synchronisation is disabled in the element or there
1854 * is no clock, no synchronisation is done and #GST_CLOCK_BADTIME is returned.
1856 * This function should only be called with the PREROLL_LOCK held, like when
1857 * receiving an EOS event in the ::event vmethod or when receiving a buffer in
1858 * the ::render vmethod.
1860 * The @time argument should be the running_time of when this method should
1861 * return and is not adjusted with any latency or offset configured in the
1866 * Returns: #GstClockReturn
1869 gst_base_sink_wait_clock (GstBaseSink * sink, GstClockTime time,
1870 GstClockTimeDiff * jitter)
1876 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
1879 GST_OBJECT_LOCK (sink);
1880 if (G_UNLIKELY (!sink->sync))
1883 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (sink)) == NULL))
1886 /* add base_time to running_time to get the time against the clock */
1887 time += GST_ELEMENT_CAST (sink)->base_time;
1889 id = gst_clock_new_single_shot_id (clock, time);
1890 GST_OBJECT_UNLOCK (sink);
1892 /* A blocking wait is performed on the clock. We save the ClockID
1893 * so we can unlock the entry at any time. While we are blocking, we
1894 * release the PREROLL_LOCK so that other threads can interrupt the
1896 sink->clock_id = id;
1897 /* release the preroll lock while waiting */
1898 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
1900 ret = gst_clock_id_wait (id, jitter);
1902 GST_PAD_PREROLL_LOCK (sink->sinkpad);
1903 gst_clock_id_unref (id);
1904 sink->clock_id = NULL;
1908 /* no syncing needed */
1911 GST_DEBUG_OBJECT (sink, "time not valid, no sync needed");
1912 return GST_CLOCK_BADTIME;
1916 GST_DEBUG_OBJECT (sink, "sync disabled");
1917 GST_OBJECT_UNLOCK (sink);
1918 return GST_CLOCK_BADTIME;
1922 GST_DEBUG_OBJECT (sink, "no clock, can't sync");
1923 GST_OBJECT_UNLOCK (sink);
1924 return GST_CLOCK_BADTIME;
1929 * gst_base_sink_wait_preroll:
1932 * If the #GstBaseSinkClass::render method performs its own synchronisation against
1933 * the clock it must unblock when going from PLAYING to the PAUSED state and call
1934 * this method before continuing to render the remaining data.
1936 * This function will block until a state change to PLAYING happens (in which
1937 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
1938 * to a state change to READY or a FLUSH event (in which case this function
1939 * returns #GST_FLOW_WRONG_STATE).
1941 * This function should only be called with the PREROLL_LOCK held, like in the
1946 * Returns: #GST_FLOW_OK if the preroll completed and processing can
1947 * continue. Any other return value should be returned from the render vmethod.
1950 gst_base_sink_wait_preroll (GstBaseSink * sink)
1952 sink->have_preroll = TRUE;
1953 GST_DEBUG_OBJECT (sink, "waiting in preroll for flush or PLAYING");
1954 /* block until the state changes, or we get a flush, or something */
1955 GST_PAD_PREROLL_WAIT (sink->sinkpad);
1956 sink->have_preroll = FALSE;
1957 if (G_UNLIKELY (sink->flushing))
1959 if (G_UNLIKELY (sink->priv->step_unlock))
1961 GST_DEBUG_OBJECT (sink, "continue after preroll");
1968 GST_DEBUG_OBJECT (sink, "preroll interrupted because of flush");
1969 return GST_FLOW_WRONG_STATE;
1973 sink->priv->step_unlock = FALSE;
1974 GST_DEBUG_OBJECT (sink, "preroll interrupted because of step");
1975 return GST_FLOW_STEP;
1980 * gst_base_sink_do_preroll:
1982 * @obj: the object that caused the preroll
1984 * If the @sink spawns its own thread for pulling buffers from upstream it
1985 * should call this method after it has pulled a buffer. If the element needed
1986 * to preroll, this function will perform the preroll and will then block
1987 * until the element state is changed.
1989 * This function should be called with the PREROLL_LOCK held.
1993 * Returns: #GST_FLOW_OK if the preroll completed and processing can
1994 * continue. Any other return value should be returned from the render vmethod.
1997 gst_base_sink_do_preroll (GstBaseSink * sink, GstMiniObject * obj)
2001 while (G_UNLIKELY (sink->need_preroll)) {
2002 GST_DEBUG_OBJECT (sink, "prerolling object %p", obj);
2004 ret = gst_base_sink_preroll_object (sink, obj);
2005 if (ret != GST_FLOW_OK)
2006 goto preroll_failed;
2008 /* need to recheck here because the commit state could have
2009 * made us not need the preroll anymore */
2010 if (G_LIKELY (sink->need_preroll)) {
2011 /* block until the state changes, or we get a flush, or something */
2012 ret = gst_base_sink_wait_preroll (sink);
2013 if (ret != GST_FLOW_OK) {
2014 if (ret == GST_FLOW_STEP)
2017 goto preroll_failed;
2026 GST_DEBUG_OBJECT (sink, "preroll failed %d", ret);
2032 * gst_base_sink_wait_eos:
2034 * @time: the running_time to be reached
2035 * @jitter: the jitter to be filled with time diff (can be NULL)
2037 * This function will block until @time is reached. It is usually called by
2038 * subclasses that use their own internal synchronisation but want to let the
2039 * EOS be handled by the base class.
2041 * This function should only be called with the PREROLL_LOCK held, like when
2042 * receiving an EOS event in the ::event vmethod.
2044 * The @time argument should be the running_time of when the EOS should happen
2045 * and will be adjusted with any latency and offset configured in the sink.
2049 * Returns: #GstFlowReturn
2052 gst_base_sink_wait_eos (GstBaseSink * sink, GstClockTime time,
2053 GstClockTimeDiff * jitter)
2055 GstClockReturn status;
2061 GST_DEBUG_OBJECT (sink, "checking preroll");
2063 /* first wait for the playing state before we can continue */
2064 if (G_UNLIKELY (sink->need_preroll)) {
2065 ret = gst_base_sink_wait_preroll (sink);
2066 if (ret != GST_FLOW_OK) {
2067 if (ret == GST_FLOW_STEP)
2074 /* preroll done, we can sync since we are in PLAYING now. */
2075 GST_DEBUG_OBJECT (sink, "possibly waiting for clock to reach %"
2076 GST_TIME_FORMAT, GST_TIME_ARGS (time));
2078 /* compensate for latency and ts_offset. We don't adjust for render delay
2079 * because we don't interact with the device on EOS normally. */
2080 stime = gst_base_sink_adjust_time (sink, time);
2082 /* wait for the clock, this can be interrupted because we got shut down or
2084 status = gst_base_sink_wait_clock (sink, stime, jitter);
2086 GST_DEBUG_OBJECT (sink, "clock returned %d", status);
2088 /* invalid time, no clock or sync disabled, just continue then */
2089 if (status == GST_CLOCK_BADTIME)
2092 /* waiting could have been interrupted and we can be flushing now */
2093 if (G_UNLIKELY (sink->flushing))
2096 /* retry if we got unscheduled, which means we did not reach the timeout
2097 * yet. if some other error occures, we continue. */
2098 } while (status == GST_CLOCK_UNSCHEDULED);
2100 GST_DEBUG_OBJECT (sink, "end of stream");
2107 GST_DEBUG_OBJECT (sink, "we are flushing");
2108 return GST_FLOW_WRONG_STATE;
2112 /* with STREAM_LOCK, PREROLL_LOCK
2114 * Make sure we are in PLAYING and synchronize an object to the clock.
2116 * If we need preroll, we are not in PLAYING. We try to commit the state
2117 * if needed and then block if we still are not PLAYING.
2119 * We start waiting on the clock in PLAYING. If we got interrupted, we
2120 * immediatly try to re-preroll.
2122 * Some objects do not need synchronisation (most events) and so this function
2123 * immediatly returns GST_FLOW_OK.
2125 * for objects that arrive later than max-lateness to be synchronized to the
2126 * clock have the @late boolean set to TRUE.
2128 * This function keeps a running average of the jitter (the diff between the
2129 * clock time and the requested sync time). The jitter is negative for
2130 * objects that arrive in time and positive for late buffers.
2132 * does not take ownership of obj.
2134 static GstFlowReturn
2135 gst_base_sink_do_sync (GstBaseSink * basesink, GstPad * pad,
2136 GstMiniObject * obj, gboolean * late)
2138 GstClockTimeDiff jitter;
2140 GstClockReturn status = GST_CLOCK_OK;
2141 GstClockTime rstart, rstop, sstart, sstop, stime;
2143 GstBaseSinkPrivate *priv;
2145 GstStepInfo *current, *pending;
2148 priv = basesink->priv;
2151 sstart = sstop = rstart = rstop = -1;
2155 priv->current_rstart = -1;
2157 /* get stepping info */
2158 current = &priv->current_step;
2159 pending = &priv->pending_step;
2161 /* get timing information for this object against the render segment */
2162 syncable = gst_base_sink_get_sync_times (basesink, obj,
2163 &sstart, &sstop, &rstart, &rstop, &do_sync, &stepped, &basesink->segment,
2166 if (G_UNLIKELY (stepped))
2169 /* a syncable object needs to participate in preroll and
2170 * clocking. All buffers and EOS are syncable. */
2171 if (G_UNLIKELY (!syncable))
2174 /* store timing info for current object */
2175 priv->current_rstart = rstart;
2176 priv->current_rstop = (rstop != -1 ? rstop : rstart);
2178 /* save sync time for eos when the previous object needed sync */
2179 priv->eos_rtime = (do_sync ? priv->current_rstop : -1);
2182 /* first do preroll, this makes sure we commit our state
2183 * to PAUSED and can continue to PLAYING. We cannot perform
2184 * any clock sync in PAUSED because there is no clock. */
2185 ret = gst_base_sink_do_preroll (basesink, obj);
2186 if (G_UNLIKELY (ret != GST_FLOW_OK))
2187 goto preroll_failed;
2189 /* After rendering we store the position of the last buffer so that we can use
2190 * it to report the position. We need to take the lock here. */
2191 GST_OBJECT_LOCK (basesink);
2192 priv->current_sstart = sstart;
2193 priv->current_sstop = (sstop != -1 ? sstop : sstart);
2194 GST_OBJECT_UNLOCK (basesink);
2196 /* update the segment with a pending step if the current one is invalid and we
2197 * have a new pending one. We only accept new step updates after a preroll */
2198 if (G_UNLIKELY (pending->valid && !current->valid)) {
2199 start_stepping (basesink, &basesink->segment, pending, current);
2206 /* adjust for latency */
2207 stime = gst_base_sink_adjust_time (basesink, rstart);
2209 /* adjust for render-delay, avoid underflows */
2211 if (stime > priv->render_delay)
2212 stime -= priv->render_delay;
2217 /* preroll done, we can sync since we are in PLAYING now. */
2218 GST_DEBUG_OBJECT (basesink, "possibly waiting for clock to reach %"
2219 GST_TIME_FORMAT ", adjusted %" GST_TIME_FORMAT,
2220 GST_TIME_ARGS (rstart), GST_TIME_ARGS (stime));
2222 /* This function will return immediatly if start == -1, no clock
2223 * or sync is disabled with GST_CLOCK_BADTIME. */
2224 status = gst_base_sink_wait_clock (basesink, stime, &jitter);
2226 GST_DEBUG_OBJECT (basesink, "clock returned %d", status);
2228 /* invalid time, no clock or sync disabled, just render */
2229 if (status == GST_CLOCK_BADTIME)
2232 /* waiting could have been interrupted and we can be flushing now */
2233 if (G_UNLIKELY (basesink->flushing))
2236 /* check for unlocked by a state change, we are not flushing so
2237 * we can try to preroll on the current buffer. */
2238 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
2239 GST_DEBUG_OBJECT (basesink, "unscheduled, waiting some more");
2240 priv->call_preroll = TRUE;
2244 /* successful syncing done, record observation */
2245 priv->current_jitter = jitter;
2247 /* check if the object should be dropped */
2248 *late = gst_base_sink_is_too_late (basesink, obj, rstart, rstop,
2257 GST_DEBUG_OBJECT (basesink, "skipped stepped object %p", obj);
2263 GST_DEBUG_OBJECT (basesink, "non syncable object %p", obj);
2268 GST_DEBUG_OBJECT (basesink, "we are flushing");
2269 return GST_FLOW_WRONG_STATE;
2273 GST_DEBUG_OBJECT (basesink, "preroll failed");
2279 gst_base_sink_send_qos (GstBaseSink * basesink,
2280 gdouble proportion, GstClockTime time, GstClockTimeDiff diff)
2285 /* generate Quality-of-Service event */
2286 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2287 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2288 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (time));
2290 event = gst_event_new_qos (proportion, diff, time);
2293 res = gst_pad_push_event (basesink->sinkpad, event);
2299 gst_base_sink_perform_qos (GstBaseSink * sink, gboolean dropped)
2301 GstBaseSinkPrivate *priv;
2302 GstClockTime start, stop;
2303 GstClockTimeDiff jitter;
2304 GstClockTime pt, entered, left;
2305 GstClockTime duration;
2310 start = priv->current_rstart;
2312 /* if Quality-of-Service disabled, do nothing */
2313 if (!g_atomic_int_get (&priv->qos_enabled) || start == -1)
2316 stop = priv->current_rstop;
2317 jitter = priv->current_jitter;
2320 /* this is the time the buffer entered the sink */
2321 if (start < -jitter)
2324 entered = start + jitter;
2327 /* this is the time the buffer entered the sink */
2328 entered = start + jitter;
2329 /* this is the time the buffer left the sink */
2330 left = start + jitter;
2333 /* calculate duration of the buffer */
2335 duration = stop - start;
2339 /* if we have the time when the last buffer left us, calculate
2340 * processing time */
2341 if (priv->last_left != -1) {
2342 if (entered > priv->last_left) {
2343 pt = entered - priv->last_left;
2351 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "start: %" GST_TIME_FORMAT
2352 ", entered %" GST_TIME_FORMAT ", left %" GST_TIME_FORMAT ", pt: %"
2353 GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT ",jitter %"
2354 G_GINT64_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (entered),
2355 GST_TIME_ARGS (left), GST_TIME_ARGS (pt), GST_TIME_ARGS (duration),
2358 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "avg_duration: %" GST_TIME_FORMAT
2359 ", avg_pt: %" GST_TIME_FORMAT ", avg_rate: %g",
2360 GST_TIME_ARGS (priv->avg_duration), GST_TIME_ARGS (priv->avg_pt),
2363 /* collect running averages. for first observations, we copy the
2365 if (priv->avg_duration == -1)
2366 priv->avg_duration = duration;
2368 priv->avg_duration = UPDATE_RUNNING_AVG (priv->avg_duration, duration);
2370 if (priv->avg_pt == -1)
2373 priv->avg_pt = UPDATE_RUNNING_AVG (priv->avg_pt, pt);
2375 if (priv->avg_duration != 0)
2377 gst_guint64_to_gdouble (priv->avg_pt) /
2378 gst_guint64_to_gdouble (priv->avg_duration);
2382 if (priv->last_left != -1) {
2383 if (dropped || priv->avg_rate < 0.0) {
2384 priv->avg_rate = rate;
2387 priv->avg_rate = UPDATE_RUNNING_AVG_N (priv->avg_rate, rate);
2389 priv->avg_rate = UPDATE_RUNNING_AVG_P (priv->avg_rate, rate);
2393 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink,
2394 "updated: avg_duration: %" GST_TIME_FORMAT ", avg_pt: %" GST_TIME_FORMAT
2395 ", avg_rate: %g", GST_TIME_ARGS (priv->avg_duration),
2396 GST_TIME_ARGS (priv->avg_pt), priv->avg_rate);
2399 if (priv->avg_rate >= 0.0) {
2400 /* if we have a valid rate, start sending QoS messages */
2401 if (priv->current_jitter < 0) {
2402 /* make sure we never go below 0 when adding the jitter to the
2404 if (priv->current_rstart < -priv->current_jitter)
2405 priv->current_jitter = -priv->current_rstart;
2407 gst_base_sink_send_qos (sink, priv->avg_rate, priv->current_rstart,
2408 priv->current_jitter);
2411 /* record when this buffer will leave us */
2412 priv->last_left = left;
2415 /* reset all qos measuring */
2417 gst_base_sink_reset_qos (GstBaseSink * sink)
2419 GstBaseSinkPrivate *priv;
2423 priv->last_in_time = -1;
2424 priv->last_left = -1;
2425 priv->avg_duration = -1;
2427 priv->avg_rate = -1.0;
2428 priv->avg_render = -1;
2434 /* Checks if the object was scheduled too late.
2436 * start/stop contain the raw timestamp start and stop values
2439 * status and jitter contain the return values from the clock wait.
2441 * returns TRUE if the buffer was too late.
2444 gst_base_sink_is_too_late (GstBaseSink * basesink, GstMiniObject * obj,
2445 GstClockTime start, GstClockTime stop,
2446 GstClockReturn status, GstClockTimeDiff jitter)
2449 gint64 max_lateness;
2450 GstBaseSinkPrivate *priv;
2452 priv = basesink->priv;
2456 /* only for objects that were too late */
2457 if (G_LIKELY (status != GST_CLOCK_EARLY))
2460 max_lateness = basesink->abidata.ABI.max_lateness;
2462 /* check if frame dropping is enabled */
2463 if (max_lateness == -1)
2466 /* only check for buffers */
2467 if (G_UNLIKELY (!GST_IS_BUFFER (obj)))
2470 /* can't do check if we don't have a timestamp */
2471 if (G_UNLIKELY (start == -1))
2474 /* we can add a valid stop time */
2476 max_lateness += stop;
2478 max_lateness += start;
2480 /* if the jitter bigger than duration and lateness we are too late */
2481 if ((late = start + jitter > max_lateness)) {
2482 GST_DEBUG_OBJECT (basesink, "buffer is too late %" GST_TIME_FORMAT
2483 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (start + jitter),
2484 GST_TIME_ARGS (max_lateness));
2485 /* !!emergency!!, if we did not receive anything valid for more than a
2486 * second, render it anyway so the user sees something */
2487 if (priv->last_in_time != -1 && start - priv->last_in_time > GST_SECOND) {
2489 GST_ELEMENT_WARNING (basesink, CORE, CLOCK,
2490 (_("A lot of buffers are dropped.")),
2491 ("Maybe there is a timestamp problem or this computer is too slow."));
2492 GST_DEBUG_OBJECT (basesink,
2493 "**emergency** last buffer at %" GST_TIME_FORMAT " > GST_SECOND",
2494 GST_TIME_ARGS (priv->last_in_time));
2500 priv->last_in_time = start;
2507 GST_DEBUG_OBJECT (basesink, "object was scheduled in time");
2512 GST_DEBUG_OBJECT (basesink, "frame dropping disabled");
2517 GST_DEBUG_OBJECT (basesink, "object is not a buffer");
2522 GST_DEBUG_OBJECT (basesink, "buffer has no timestamp");
2527 /* called before and after calling the render vmethod. It keeps track of how
2528 * much time was spent in the render method and is used to check if we are
2531 gst_base_sink_do_render_stats (GstBaseSink * basesink, gboolean start)
2533 GstBaseSinkPrivate *priv;
2535 priv = basesink->priv;
2538 priv->start = gst_util_get_timestamp ();
2540 GstClockTime elapsed;
2542 priv->stop = gst_util_get_timestamp ();
2544 elapsed = GST_CLOCK_DIFF (priv->start, priv->stop);
2546 if (priv->avg_render == -1)
2547 priv->avg_render = elapsed;
2549 priv->avg_render = UPDATE_RUNNING_AVG (priv->avg_render, elapsed);
2551 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2552 "avg_render: %" GST_TIME_FORMAT, GST_TIME_ARGS (priv->avg_render));
2556 /* with STREAM_LOCK, PREROLL_LOCK,
2558 * Synchronize the object on the clock and then render it.
2560 * takes ownership of obj.
2562 static GstFlowReturn
2563 gst_base_sink_render_object (GstBaseSink * basesink, GstPad * pad,
2564 GstMiniObject * obj)
2567 GstBaseSinkClass *bclass;
2570 GstBaseSinkPrivate *priv;
2572 priv = basesink->priv;
2578 /* synchronize this object, non syncable objects return OK
2580 ret = gst_base_sink_do_sync (basesink, pad, obj, &late);
2581 if (G_UNLIKELY (ret != GST_FLOW_OK))
2584 /* and now render, event or buffer. */
2585 if (G_LIKELY (GST_IS_BUFFER (obj))) {
2588 /* drop late buffers unconditionally, let's hope it's unlikely */
2589 if (G_UNLIKELY (late))
2592 buf = GST_BUFFER_CAST (obj);
2594 gst_base_sink_set_last_buffer (basesink, buf);
2596 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2598 if (G_LIKELY (bclass->render)) {
2601 /* read once, to get same value before and after */
2602 do_qos = g_atomic_int_get (&priv->qos_enabled);
2604 GST_DEBUG_OBJECT (basesink, "rendering buffer %p", obj);
2606 /* record rendering time for QoS and stats */
2608 gst_base_sink_do_render_stats (basesink, TRUE);
2610 ret = bclass->render (basesink, buf);
2613 gst_base_sink_do_render_stats (basesink, FALSE);
2615 if (ret == GST_FLOW_STEP)
2621 GstEvent *event = GST_EVENT_CAST (obj);
2622 gboolean event_res = TRUE;
2625 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2627 type = GST_EVENT_TYPE (event);
2629 GST_DEBUG_OBJECT (basesink, "rendering event %p, type %s", obj,
2630 gst_event_type_get_name (type));
2633 event_res = bclass->event (basesink, event);
2635 /* when we get here we could be flushing again when the event handler calls
2636 * _wait_eos(). We have to ignore this object in that case. */
2637 if (G_UNLIKELY (basesink->flushing))
2640 if (G_LIKELY (event_res)) {
2643 seqnum = basesink->priv->seqnum = gst_event_get_seqnum (event);
2644 GST_DEBUG_OBJECT (basesink, "Got seqnum #%" G_GUINT32_FORMAT, seqnum);
2649 GstMessage *message;
2651 /* the EOS event is completely handled so we mark
2652 * ourselves as being in the EOS state. eos is also
2653 * protected by the object lock so we can read it when
2654 * answering the POSITION query. */
2655 GST_OBJECT_LOCK (basesink);
2656 basesink->eos = TRUE;
2657 GST_OBJECT_UNLOCK (basesink);
2659 /* ok, now we can post the message */
2660 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
2662 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
2663 gst_message_set_seqnum (message, seqnum);
2664 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
2667 case GST_EVENT_NEWSEGMENT:
2668 /* configure the segment */
2669 gst_base_sink_configure_segment (basesink, pad, event,
2670 &basesink->segment);
2679 gst_base_sink_perform_qos (basesink, late);
2681 GST_DEBUG_OBJECT (basesink, "object unref after render %p", obj);
2682 gst_mini_object_unref (obj);
2689 GST_DEBUG_OBJECT (basesink, "do_sync returned %s", gst_flow_get_name (ret));
2695 GST_DEBUG_OBJECT (basesink, "buffer late, dropping");
2700 GST_DEBUG_OBJECT (basesink, "we are flushing, ignore object");
2701 gst_mini_object_unref (obj);
2702 return GST_FLOW_WRONG_STATE;
2706 /* with STREAM_LOCK, PREROLL_LOCK
2708 * Perform preroll on the given object. For buffers this means
2709 * calling the preroll subclass method.
2710 * If that succeeds, the state will be commited.
2712 * function does not take ownership of obj.
2714 static GstFlowReturn
2715 gst_base_sink_preroll_object (GstBaseSink * basesink, GstMiniObject * obj)
2719 GST_DEBUG_OBJECT (basesink, "prerolling object %p", obj);
2721 /* if it's a buffer, we need to call the preroll method */
2722 if (G_LIKELY (GST_IS_BUFFER (obj)) && basesink->priv->call_preroll) {
2723 GstBaseSinkClass *bclass;
2725 GstClockTime timestamp;
2727 buf = GST_BUFFER_CAST (obj);
2728 timestamp = GST_BUFFER_TIMESTAMP (buf);
2730 GST_DEBUG_OBJECT (basesink, "preroll buffer %" GST_TIME_FORMAT,
2731 GST_TIME_ARGS (timestamp));
2733 gst_base_sink_set_last_buffer (basesink, buf);
2735 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2736 if (bclass->preroll)
2737 if ((ret = bclass->preroll (basesink, buf)) != GST_FLOW_OK)
2738 goto preroll_failed;
2740 basesink->priv->call_preroll = FALSE;
2744 if (G_LIKELY (basesink->playing_async)) {
2745 if (G_UNLIKELY (!gst_base_sink_commit_state (basesink)))
2754 GST_DEBUG_OBJECT (basesink, "preroll failed, abort state");
2755 gst_element_abort_state (GST_ELEMENT_CAST (basesink));
2760 GST_DEBUG_OBJECT (basesink, "stopping while commiting state");
2761 return GST_FLOW_WRONG_STATE;
2765 /* with STREAM_LOCK, PREROLL_LOCK
2767 * Queue an object for rendering.
2768 * The first prerollable object queued will complete the preroll. If the
2769 * preroll queue if filled, we render all the objects in the queue.
2771 * This function takes ownership of the object.
2773 static GstFlowReturn
2774 gst_base_sink_queue_object_unlocked (GstBaseSink * basesink, GstPad * pad,
2775 GstMiniObject * obj, gboolean prerollable)
2777 GstFlowReturn ret = GST_FLOW_OK;
2781 if (G_UNLIKELY (basesink->need_preroll)) {
2782 if (G_LIKELY (prerollable))
2783 basesink->preroll_queued++;
2785 length = basesink->preroll_queued;
2787 GST_DEBUG_OBJECT (basesink, "now %d prerolled items", length);
2789 /* first prerollable item needs to finish the preroll */
2791 ret = gst_base_sink_preroll_object (basesink, obj);
2792 if (G_UNLIKELY (ret != GST_FLOW_OK))
2793 goto preroll_failed;
2795 /* need to recheck if we need preroll, commmit state during preroll
2796 * could have made us not need more preroll. */
2797 if (G_UNLIKELY (basesink->need_preroll)) {
2798 /* see if we can render now, if we can't add the object to the preroll
2800 if (G_UNLIKELY (length <= basesink->preroll_queue_max_len))
2805 /* we can start rendering (or blocking) the queued object
2807 q = basesink->preroll_queue;
2808 while (G_UNLIKELY (!g_queue_is_empty (q))) {
2811 o = g_queue_pop_head (q);
2812 GST_DEBUG_OBJECT (basesink, "rendering queued object %p", o);
2814 /* do something with the return value */
2815 ret = gst_base_sink_render_object (basesink, pad, o);
2816 if (ret != GST_FLOW_OK)
2817 goto dequeue_failed;
2820 /* now render the object */
2821 ret = gst_base_sink_render_object (basesink, pad, obj);
2822 basesink->preroll_queued = 0;
2829 GST_DEBUG_OBJECT (basesink, "preroll failed, reason %s",
2830 gst_flow_get_name (ret));
2831 gst_mini_object_unref (obj);
2836 /* add object to the queue and return */
2837 GST_DEBUG_OBJECT (basesink, "need more preroll data %d <= %d",
2838 length, basesink->preroll_queue_max_len);
2839 g_queue_push_tail (basesink->preroll_queue, obj);
2844 GST_DEBUG_OBJECT (basesink, "rendering queued objects failed, reason %s",
2845 gst_flow_get_name (ret));
2846 gst_mini_object_unref (obj);
2853 * This function grabs the PREROLL_LOCK and adds the object to
2856 * This function takes ownership of obj.
2858 static GstFlowReturn
2859 gst_base_sink_queue_object (GstBaseSink * basesink, GstPad * pad,
2860 GstMiniObject * obj, gboolean prerollable)
2864 GST_PAD_PREROLL_LOCK (pad);
2865 if (G_UNLIKELY (basesink->flushing))
2868 if (G_UNLIKELY (basesink->priv->received_eos))
2871 ret = gst_base_sink_queue_object_unlocked (basesink, pad, obj, prerollable);
2872 GST_PAD_PREROLL_UNLOCK (pad);
2879 GST_DEBUG_OBJECT (basesink, "sink is flushing");
2880 GST_PAD_PREROLL_UNLOCK (pad);
2881 gst_mini_object_unref (obj);
2882 return GST_FLOW_WRONG_STATE;
2886 GST_DEBUG_OBJECT (basesink,
2887 "we are EOS, dropping object, return UNEXPECTED");
2888 GST_PAD_PREROLL_UNLOCK (pad);
2889 gst_mini_object_unref (obj);
2890 return GST_FLOW_UNEXPECTED;
2895 gst_base_sink_flush_start (GstBaseSink * basesink, GstPad * pad)
2897 /* make sure we are not blocked on the clock also clear any pending
2899 gst_base_sink_set_flushing (basesink, pad, TRUE);
2901 /* we grab the stream lock but that is not needed since setting the
2902 * sink to flushing would make sure no state commit is being done
2904 GST_PAD_STREAM_LOCK (pad);
2905 gst_base_sink_reset_qos (basesink);
2906 if (basesink->priv->async_enabled) {
2907 /* and we need to commit our state again on the next
2908 * prerolled buffer */
2909 basesink->playing_async = TRUE;
2910 gst_element_lost_state (GST_ELEMENT_CAST (basesink));
2912 basesink->priv->have_latency = TRUE;
2913 basesink->need_preroll = FALSE;
2915 gst_base_sink_set_last_buffer (basesink, NULL);
2916 GST_PAD_STREAM_UNLOCK (pad);
2920 gst_base_sink_flush_stop (GstBaseSink * basesink, GstPad * pad)
2922 /* unset flushing so we can accept new data, this also flushes out any EOS
2924 gst_base_sink_set_flushing (basesink, pad, FALSE);
2926 /* for position reporting */
2927 GST_OBJECT_LOCK (basesink);
2928 basesink->priv->current_sstart = -1;
2929 basesink->priv->current_sstop = -1;
2930 basesink->priv->eos_rtime = -1;
2931 basesink->priv->call_preroll = TRUE;
2932 basesink->priv->current_step.valid = FALSE;
2933 basesink->priv->pending_step.valid = FALSE;
2934 if (basesink->pad_mode == GST_ACTIVATE_PUSH) {
2935 /* we need new segment info after the flush. */
2936 basesink->have_newsegment = FALSE;
2937 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
2938 gst_segment_init (basesink->abidata.ABI.clip_segment, GST_FORMAT_UNDEFINED);
2940 GST_OBJECT_UNLOCK (basesink);
2944 gst_base_sink_event (GstPad * pad, GstEvent * event)
2946 GstBaseSink *basesink;
2947 gboolean result = TRUE;
2948 GstBaseSinkClass *bclass;
2950 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
2952 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2954 GST_DEBUG_OBJECT (basesink, "event %p (%s)", event,
2955 GST_EVENT_TYPE_NAME (event));
2957 switch (GST_EVENT_TYPE (event)) {
2962 GST_PAD_PREROLL_LOCK (pad);
2963 if (G_UNLIKELY (basesink->flushing))
2966 if (G_UNLIKELY (basesink->priv->received_eos)) {
2967 /* we can't accept anything when we are EOS */
2969 gst_event_unref (event);
2971 /* we set the received EOS flag here so that we can use it when testing if
2972 * we are prerolled and to refuse more buffers. */
2973 basesink->priv->received_eos = TRUE;
2975 /* EOS is a prerollable object, we call the unlocked version because it
2976 * does not check the received_eos flag. */
2977 ret = gst_base_sink_queue_object_unlocked (basesink, pad,
2978 GST_MINI_OBJECT_CAST (event), TRUE);
2979 if (G_UNLIKELY (ret != GST_FLOW_OK))
2982 GST_PAD_PREROLL_UNLOCK (pad);
2985 case GST_EVENT_NEWSEGMENT:
2989 GST_DEBUG_OBJECT (basesink, "newsegment %p", event);
2991 GST_PAD_PREROLL_LOCK (pad);
2992 if (G_UNLIKELY (basesink->flushing))
2995 if (G_UNLIKELY (basesink->priv->received_eos)) {
2996 /* we can't accept anything when we are EOS */
2998 gst_event_unref (event);
3000 /* the new segment is a non prerollable item and does not block anything,
3001 * we need to configure the current clipping segment and insert the event
3002 * in the queue to serialize it with the buffers for rendering. */
3003 gst_base_sink_configure_segment (basesink, pad, event,
3004 basesink->abidata.ABI.clip_segment);
3007 gst_base_sink_queue_object_unlocked (basesink, pad,
3008 GST_MINI_OBJECT_CAST (event), FALSE);
3009 if (G_UNLIKELY (ret != GST_FLOW_OK))
3012 GST_OBJECT_LOCK (basesink);
3013 basesink->have_newsegment = TRUE;
3014 GST_OBJECT_UNLOCK (basesink);
3017 GST_PAD_PREROLL_UNLOCK (pad);
3020 case GST_EVENT_FLUSH_START:
3022 bclass->event (basesink, event);
3024 GST_DEBUG_OBJECT (basesink, "flush-start %p", event);
3026 gst_base_sink_flush_start (basesink, pad);
3028 gst_event_unref (event);
3030 case GST_EVENT_FLUSH_STOP:
3032 bclass->event (basesink, event);
3034 GST_DEBUG_OBJECT (basesink, "flush-stop %p", event);
3036 gst_base_sink_flush_stop (basesink, pad);
3038 gst_event_unref (event);
3041 /* other events are sent to queue or subclass depending on if they
3042 * are serialized. */
3043 if (GST_EVENT_IS_SERIALIZED (event)) {
3044 gst_base_sink_queue_object (basesink, pad,
3045 GST_MINI_OBJECT_CAST (event), FALSE);
3048 bclass->event (basesink, event);
3049 gst_event_unref (event);
3054 gst_object_unref (basesink);
3061 GST_DEBUG_OBJECT (basesink, "we are flushing");
3062 GST_PAD_PREROLL_UNLOCK (pad);
3064 gst_event_unref (event);
3069 /* default implementation to calculate the start and end
3070 * timestamps on a buffer, subclasses can override
3073 gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
3074 GstClockTime * start, GstClockTime * end)
3076 GstClockTime timestamp, duration;
3078 timestamp = GST_BUFFER_TIMESTAMP (buffer);
3079 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
3081 /* get duration to calculate end time */
3082 duration = GST_BUFFER_DURATION (buffer);
3083 if (GST_CLOCK_TIME_IS_VALID (duration)) {
3084 *end = timestamp + duration;
3090 /* must be called with PREROLL_LOCK */
3092 gst_base_sink_needs_preroll (GstBaseSink * basesink)
3094 gboolean is_prerolled, res;
3096 /* we have 2 cases where the PREROLL_LOCK is released:
3097 * 1) we are blocking in the PREROLL_LOCK and thus are prerolled.
3098 * 2) we are syncing on the clock
3100 is_prerolled = basesink->have_preroll || basesink->priv->received_eos;
3101 res = !is_prerolled;
3103 GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d => needs preroll: %d",
3104 basesink->have_preroll, basesink->priv->received_eos, res);
3109 /* with STREAM_LOCK, PREROLL_LOCK
3111 * Takes a buffer and compare the timestamps with the last segment.
3112 * If the buffer falls outside of the segment boundaries, drop it.
3113 * Else queue the buffer for preroll and rendering.
3115 * This function takes ownership of the buffer.
3117 static GstFlowReturn
3118 gst_base_sink_chain_unlocked (GstBaseSink * basesink, GstPad * pad,
3121 GstBaseSinkClass *bclass;
3122 GstFlowReturn result;
3123 GstClockTime start = GST_CLOCK_TIME_NONE, end = GST_CLOCK_TIME_NONE;
3124 GstSegment *clip_segment;
3126 if (G_UNLIKELY (basesink->flushing))
3129 if (G_UNLIKELY (basesink->priv->received_eos))
3132 /* for code clarity */
3133 clip_segment = basesink->abidata.ABI.clip_segment;
3135 if (G_UNLIKELY (!basesink->have_newsegment)) {
3138 sync = gst_base_sink_get_sync (basesink);
3140 GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
3141 (_("Internal data flow problem.")),
3142 ("Received buffer without a new-segment. Assuming timestamps start from 0."));
3145 /* this means this sink will assume timestamps start from 0 */
3146 GST_OBJECT_LOCK (basesink);
3147 clip_segment->start = 0;
3148 clip_segment->stop = -1;
3149 basesink->segment.start = 0;
3150 basesink->segment.stop = -1;
3151 basesink->have_newsegment = TRUE;
3152 GST_OBJECT_UNLOCK (basesink);
3155 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3157 /* check if the buffer needs to be dropped, we first ask the subclass for the
3159 if (bclass->get_times)
3160 bclass->get_times (basesink, buf, &start, &end);
3163 /* if the subclass does not want sync, we use our own values so that we at
3164 * least clip the buffer to the segment */
3165 gst_base_sink_get_times (basesink, buf, &start, &end);
3168 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
3169 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
3171 /* a dropped buffer does not participate in anything */
3172 if (GST_CLOCK_TIME_IS_VALID (start) &&
3173 (clip_segment->format == GST_FORMAT_TIME)) {
3174 if (G_UNLIKELY (!gst_segment_clip (clip_segment,
3175 GST_FORMAT_TIME, (gint64) start, (gint64) end, NULL, NULL)))
3176 goto out_of_segment;
3179 /* now we can process the buffer in the queue, this function takes ownership
3181 result = gst_base_sink_queue_object_unlocked (basesink, pad,
3182 GST_MINI_OBJECT_CAST (buf), TRUE);
3189 GST_DEBUG_OBJECT (basesink, "sink is flushing");
3190 gst_buffer_unref (buf);
3191 return GST_FLOW_WRONG_STATE;
3195 GST_DEBUG_OBJECT (basesink,
3196 "we are EOS, dropping object, return UNEXPECTED");
3197 gst_buffer_unref (buf);
3198 return GST_FLOW_UNEXPECTED;
3202 GST_DEBUG_OBJECT (basesink, "dropping buffer, out of clipping segment");
3203 gst_buffer_unref (buf);
3210 static GstFlowReturn
3211 gst_base_sink_chain (GstPad * pad, GstBuffer * buf)
3213 GstBaseSink *basesink;
3214 GstFlowReturn result;
3216 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
3218 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH))
3221 GST_PAD_PREROLL_LOCK (pad);
3222 result = gst_base_sink_chain_unlocked (basesink, pad, buf);
3223 GST_PAD_PREROLL_UNLOCK (pad);
3231 GST_OBJECT_LOCK (pad);
3232 GST_WARNING_OBJECT (basesink,
3233 "Push on pad %s:%s, but it was not activated in push mode",
3234 GST_DEBUG_PAD_NAME (pad));
3235 GST_OBJECT_UNLOCK (pad);
3236 gst_buffer_unref (buf);
3237 /* we don't post an error message this will signal to the peer
3238 * pushing that EOS is reached. */
3239 result = GST_FLOW_UNEXPECTED;
3245 gst_base_sink_default_do_seek (GstBaseSink * sink, GstSegment * segment)
3247 gboolean res = TRUE;
3249 /* update our offset if the start/stop position was updated */
3250 if (segment->format == GST_FORMAT_BYTES) {
3251 segment->time = segment->start;
3252 } else if (segment->start == 0) {
3253 /* seek to start, we can implement a default for this. */
3257 GST_INFO_OBJECT (sink, "Can't do a default seek");
3263 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
3266 gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
3267 GstEvent * event, GstSegment * segment)
3269 /* By default, we try one of 2 things:
3270 * - For absolute seek positions, convert the requested position to our
3271 * configured processing format and place it in the output segment \
3272 * - For relative seek positions, convert our current (input) values to the
3273 * seek format, adjust by the relative seek offset and then convert back to
3274 * the processing format
3276 GstSeekType cur_type, stop_type;
3279 GstFormat seek_format, dest_format;
3282 gboolean res = TRUE;
3284 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3285 &cur_type, &cur, &stop_type, &stop);
3286 dest_format = segment->format;
3288 if (seek_format == dest_format) {
3289 gst_segment_set_seek (segment, rate, seek_format, flags,
3290 cur_type, cur, stop_type, stop, &update);
3294 if (cur_type != GST_SEEK_TYPE_NONE) {
3295 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3297 gst_pad_query_convert (sink->sinkpad, seek_format, cur, &dest_format,
3299 cur_type = GST_SEEK_TYPE_SET;
3302 if (res && stop_type != GST_SEEK_TYPE_NONE) {
3303 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3305 gst_pad_query_convert (sink->sinkpad, seek_format, stop, &dest_format,
3307 stop_type = GST_SEEK_TYPE_SET;
3310 /* And finally, configure our output segment in the desired format */
3311 gst_segment_set_seek (segment, rate, dest_format, flags, cur_type, cur,
3312 stop_type, stop, &update);
3321 GST_DEBUG_OBJECT (sink, "undefined format given, seek aborted.");
3326 /* perform a seek, only executed in pull mode */
3328 gst_base_sink_perform_seek (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3332 GstFormat seek_format, dest_format;
3334 GstSeekType cur_type, stop_type;
3335 gboolean seekseg_configured = FALSE;
3337 gboolean update, res = TRUE;
3338 GstSegment seeksegment;
3340 dest_format = sink->segment.format;
3343 GST_DEBUG_OBJECT (sink, "performing seek with event %p", event);
3344 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3345 &cur_type, &cur, &stop_type, &stop);
3347 flush = flags & GST_SEEK_FLAG_FLUSH;
3349 GST_DEBUG_OBJECT (sink, "performing seek without event");
3354 GST_DEBUG_OBJECT (sink, "flushing upstream");
3355 gst_pad_push_event (pad, gst_event_new_flush_start ());
3356 gst_base_sink_flush_start (sink, pad);
3358 GST_DEBUG_OBJECT (sink, "pausing pulling thread");
3361 GST_PAD_STREAM_LOCK (pad);
3363 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
3364 * copy the current segment info into the temp segment that we can actually
3365 * attempt the seek with. We only update the real segment if the seek suceeds. */
3366 if (!seekseg_configured) {
3367 memcpy (&seeksegment, &sink->segment, sizeof (GstSegment));
3369 /* now configure the final seek segment */
3371 if (sink->segment.format != seek_format) {
3372 /* OK, here's where we give the subclass a chance to convert the relative
3373 * seek into an absolute one in the processing format. We set up any
3374 * absolute seek above, before taking the stream lock. */
3375 if (!gst_base_sink_default_prepare_seek_segment (sink, event,
3377 GST_DEBUG_OBJECT (sink,
3378 "Preparing the seek failed after flushing. " "Aborting seek");
3382 /* The seek format matches our processing format, no need to ask the
3383 * the subclass to configure the segment. */
3384 gst_segment_set_seek (&seeksegment, rate, seek_format, flags,
3385 cur_type, cur, stop_type, stop, &update);
3388 /* Else, no seek event passed, so we're just (re)starting the
3393 GST_DEBUG_OBJECT (sink, "segment configured from %" G_GINT64_FORMAT
3394 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
3395 seeksegment.start, seeksegment.stop, seeksegment.last_stop);
3397 /* do the seek, segment.last_stop contains the new position. */
3398 res = gst_base_sink_default_do_seek (sink, &seeksegment);
3403 GST_DEBUG_OBJECT (sink, "stop flushing upstream");
3404 gst_pad_push_event (pad, gst_event_new_flush_stop ());
3405 gst_base_sink_flush_stop (sink, pad);
3406 } else if (res && sink->abidata.ABI.running) {
3407 /* we are running the current segment and doing a non-flushing seek,
3408 * close the segment first based on the last_stop. */
3409 GST_DEBUG_OBJECT (sink, "closing running segment %" G_GINT64_FORMAT
3410 " to %" G_GINT64_FORMAT, sink->segment.start, sink->segment.last_stop);
3413 /* The subclass must have converted the segment to the processing format
3415 if (res && seeksegment.format != dest_format) {
3416 GST_DEBUG_OBJECT (sink, "Subclass failed to prepare a seek segment "
3417 "in the correct format. Aborting seek.");
3421 /* if successfull seek, we update our real segment and push
3422 * out the new segment. */
3424 memcpy (&sink->segment, &seeksegment, sizeof (GstSegment));
3426 if (sink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3427 gst_element_post_message (GST_ELEMENT (sink),
3428 gst_message_new_segment_start (GST_OBJECT (sink),
3429 sink->segment.format, sink->segment.last_stop));
3433 sink->priv->discont = TRUE;
3434 sink->abidata.ABI.running = TRUE;
3436 GST_PAD_STREAM_UNLOCK (pad);
3442 set_step_info (GstBaseSink * sink, GstStepInfo * info, guint seqnum,
3443 GstFormat format, guint64 amount, gdouble rate, gboolean flush,
3444 gboolean intermediate)
3446 GST_OBJECT_LOCK (sink);
3447 info->seqnum = seqnum;
3448 info->format = format;
3449 info->amount = amount;
3452 info->flush = flush;
3453 info->intermediate = intermediate;
3455 GST_OBJECT_UNLOCK (sink);
3459 gst_base_sink_perform_step (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3461 GstBaseSinkPrivate *priv;
3462 GstBaseSinkClass *bclass;
3463 gboolean flush, intermediate;
3468 GstStepInfo *pending;
3470 bclass = GST_BASE_SINK_GET_CLASS (sink);
3473 GST_DEBUG_OBJECT (sink, "performing step with event %p", event);
3475 gst_event_parse_step (event, &format, &amount, &rate, &flush, &intermediate);
3476 seqnum = gst_event_get_seqnum (event);
3478 pending = &priv->pending_step;
3481 /* we need to call ::unlock before locking PREROLL_LOCK
3482 * since we lock it before going into ::render */
3484 bclass->unlock (sink);
3486 GST_PAD_PREROLL_LOCK (sink->sinkpad);
3487 /* now that we have the PREROLL lock, clear our unlock request */
3488 if (bclass->unlock_stop)
3489 bclass->unlock_stop (sink);
3491 /* update the stepinfo and make it valid */
3492 set_step_info (sink, pending, seqnum, format, amount, rate, flush,
3495 if (sink->priv->async_enabled) {
3496 /* and we need to commit our state again on the next
3497 * prerolled buffer */
3498 sink->playing_async = TRUE;
3499 priv->pending_step.need_preroll = TRUE;
3500 sink->need_preroll = FALSE;
3501 gst_element_lost_state_full (GST_ELEMENT_CAST (sink), FALSE);
3503 sink->priv->have_latency = TRUE;
3504 sink->need_preroll = FALSE;
3506 priv->current_sstart = -1;
3507 priv->current_sstop = -1;
3508 priv->eos_rtime = -1;
3509 priv->call_preroll = TRUE;
3510 gst_base_sink_set_last_buffer (sink, NULL);
3511 gst_base_sink_reset_qos (sink);
3513 if (sink->clock_id) {
3514 gst_clock_id_unschedule (sink->clock_id);
3517 if (sink->have_preroll) {
3518 GST_DEBUG_OBJECT (sink, "signal waiter");
3519 priv->step_unlock = TRUE;
3520 GST_PAD_PREROLL_SIGNAL (sink->sinkpad);
3522 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
3524 /* update the stepinfo and make it valid */
3525 set_step_info (sink, pending, seqnum, format, amount, rate, flush,
3535 gst_base_sink_loop (GstPad * pad)
3537 GstBaseSink *basesink;
3538 GstBuffer *buf = NULL;
3539 GstFlowReturn result;
3543 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
3545 g_assert (basesink->pad_mode == GST_ACTIVATE_PULL);
3547 if ((blocksize = basesink->priv->blocksize) == 0)
3550 offset = basesink->segment.last_stop;
3552 GST_DEBUG_OBJECT (basesink, "pulling %" G_GUINT64_FORMAT ", %u",
3555 result = gst_pad_pull_range (pad, offset, blocksize, &buf);
3556 if (G_UNLIKELY (result != GST_FLOW_OK))
3559 if (G_UNLIKELY (buf == NULL))
3562 offset += GST_BUFFER_SIZE (buf);
3564 gst_segment_set_last_stop (&basesink->segment, GST_FORMAT_BYTES, offset);
3566 GST_PAD_PREROLL_LOCK (pad);
3567 result = gst_base_sink_chain_unlocked (basesink, pad, buf);
3568 GST_PAD_PREROLL_UNLOCK (pad);
3569 if (G_UNLIKELY (result != GST_FLOW_OK))
3577 GST_LOG_OBJECT (basesink, "pausing task, reason %s",
3578 gst_flow_get_name (result));
3579 gst_pad_pause_task (pad);
3580 /* fatal errors and NOT_LINKED cause EOS */
3581 if (GST_FLOW_IS_FATAL (result) || result == GST_FLOW_NOT_LINKED) {
3582 if (result == GST_FLOW_UNEXPECTED) {
3583 /* perform EOS logic */
3584 if (basesink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3585 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3586 gst_message_new_segment_done (GST_OBJECT_CAST (basesink),
3587 basesink->segment.format, basesink->segment.last_stop));
3589 gst_base_sink_event (pad, gst_event_new_eos ());
3592 /* for fatal errors we post an error message, post the error
3593 * first so the app knows about the error first. */
3594 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
3595 (_("Internal data stream error.")),
3596 ("stream stopped, reason %s", gst_flow_get_name (result)));
3597 gst_base_sink_event (pad, gst_event_new_eos ());
3604 GST_LOG_OBJECT (basesink, "no buffer, pausing");
3605 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
3606 (_("Internal data flow error.")), ("element returned NULL buffer"));
3607 result = GST_FLOW_ERROR;
3613 gst_base_sink_set_flushing (GstBaseSink * basesink, GstPad * pad,
3616 GstBaseSinkClass *bclass;
3618 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3621 /* unlock any subclasses, we need to do this before grabbing the
3622 * PREROLL_LOCK since we hold this lock before going into ::render. */
3624 bclass->unlock (basesink);
3627 GST_PAD_PREROLL_LOCK (pad);
3628 basesink->flushing = flushing;
3630 /* step 1, now that we have the PREROLL lock, clear our unlock request */
3631 if (bclass->unlock_stop)
3632 bclass->unlock_stop (basesink);
3634 /* set need_preroll before we unblock the clock. If the clock is unblocked
3635 * before timing out, we can reuse the buffer for preroll. */
3636 basesink->need_preroll = TRUE;
3638 /* step 2, unblock clock sync (if any) or any other blocking thing */
3639 if (basesink->clock_id) {
3640 gst_clock_id_unschedule (basesink->clock_id);
3643 /* flush out the data thread if it's locked in finish_preroll, this will
3644 * also flush out the EOS state */
3645 GST_DEBUG_OBJECT (basesink,
3646 "flushing out data thread, need preroll to TRUE");
3647 gst_base_sink_preroll_queue_flush (basesink, pad);
3649 GST_PAD_PREROLL_UNLOCK (pad);
3655 gst_base_sink_default_activate_pull (GstBaseSink * basesink, gboolean active)
3661 result = gst_pad_start_task (basesink->sinkpad,
3662 (GstTaskFunction) gst_base_sink_loop, basesink->sinkpad);
3664 /* step 2, make sure streaming finishes */
3665 result = gst_pad_stop_task (basesink->sinkpad);
3672 gst_base_sink_pad_activate (GstPad * pad)
3674 gboolean result = FALSE;
3675 GstBaseSink *basesink;
3677 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3679 GST_DEBUG_OBJECT (basesink, "Trying pull mode first");
3681 gst_base_sink_set_flushing (basesink, pad, FALSE);
3683 /* we need to have the pull mode enabled */
3684 if (!basesink->can_activate_pull) {
3685 GST_DEBUG_OBJECT (basesink, "pull mode disabled");
3689 /* check if downstreams supports pull mode at all */
3690 if (!gst_pad_check_pull_range (pad)) {
3691 GST_DEBUG_OBJECT (basesink, "pull mode not supported");
3695 /* set the pad mode before starting the task so that it's in the
3696 * correct state for the new thread. also the sink set_caps and get_caps
3697 * function checks this */
3698 basesink->pad_mode = GST_ACTIVATE_PULL;
3700 /* we first try to negotiate a format so that when we try to activate
3701 * downstream, it knows about our format */
3702 if (!gst_base_sink_negotiate_pull (basesink)) {
3703 GST_DEBUG_OBJECT (basesink, "failed to negotiate in pull mode");
3707 /* ok activate now */
3708 if (!gst_pad_activate_pull (pad, TRUE)) {
3709 /* clear any pending caps */
3710 GST_OBJECT_LOCK (basesink);
3711 gst_caps_replace (&basesink->priv->pull_caps, NULL);
3712 GST_OBJECT_UNLOCK (basesink);
3713 GST_DEBUG_OBJECT (basesink, "failed to activate in pull mode");
3717 GST_DEBUG_OBJECT (basesink, "Success activating pull mode");
3721 /* push mode fallback */
3723 GST_DEBUG_OBJECT (basesink, "Falling back to push mode");
3724 if ((result = gst_pad_activate_push (pad, TRUE))) {
3725 GST_DEBUG_OBJECT (basesink, "Success activating push mode");
3730 GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
3731 gst_base_sink_set_flushing (basesink, pad, TRUE);
3734 gst_object_unref (basesink);
3740 gst_base_sink_pad_activate_push (GstPad * pad, gboolean active)
3743 GstBaseSink *basesink;
3745 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3748 if (!basesink->can_activate_push) {
3750 basesink->pad_mode = GST_ACTIVATE_NONE;
3753 basesink->pad_mode = GST_ACTIVATE_PUSH;
3756 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH)) {
3757 g_warning ("Internal GStreamer activation error!!!");
3760 gst_base_sink_set_flushing (basesink, pad, TRUE);
3762 basesink->pad_mode = GST_ACTIVATE_NONE;
3766 gst_object_unref (basesink);
3772 gst_base_sink_negotiate_pull (GstBaseSink * basesink)
3779 /* this returns the intersection between our caps and the peer caps. If there
3780 * is no peer, it returns NULL and we can't operate in pull mode so we can
3781 * fail the negotiation. */
3782 caps = gst_pad_get_allowed_caps (GST_BASE_SINK_PAD (basesink));
3783 if (caps == NULL || gst_caps_is_empty (caps))
3784 goto no_caps_possible;
3786 GST_DEBUG_OBJECT (basesink, "allowed caps: %" GST_PTR_FORMAT, caps);
3788 caps = gst_caps_make_writable (caps);
3789 /* get the first (prefered) format */
3790 gst_caps_truncate (caps);
3792 gst_pad_fixate_caps (GST_BASE_SINK_PAD (basesink), caps);
3794 GST_DEBUG_OBJECT (basesink, "fixated to: %" GST_PTR_FORMAT, caps);
3796 if (gst_caps_is_any (caps)) {
3797 GST_DEBUG_OBJECT (basesink, "caps were ANY after fixating, "
3799 /* neither side has template caps in this case, so they are prepared for
3800 pull() without setcaps() */
3802 } else if (gst_caps_is_fixed (caps)) {
3803 if (!gst_pad_set_caps (GST_BASE_SINK_PAD (basesink), caps))
3804 goto could_not_set_caps;
3806 GST_OBJECT_LOCK (basesink);
3807 gst_caps_replace (&basesink->priv->pull_caps, caps);
3808 GST_OBJECT_UNLOCK (basesink);
3813 gst_caps_unref (caps);
3819 GST_INFO_OBJECT (basesink, "Pipeline could not agree on caps");
3820 GST_DEBUG_OBJECT (basesink, "get_allowed_caps() returned EMPTY");
3822 gst_caps_unref (caps);
3827 GST_INFO_OBJECT (basesink, "Could not set caps: %" GST_PTR_FORMAT, caps);
3828 gst_caps_unref (caps);
3833 /* this won't get called until we implement an activate function */
3835 gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active)
3837 gboolean result = FALSE;
3838 GstBaseSink *basesink;
3839 GstBaseSinkClass *bclass;
3841 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3842 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3848 /* we mark we have a newsegment here because pull based
3849 * mode works just fine without having a newsegment before the
3851 format = GST_FORMAT_BYTES;
3853 gst_segment_init (&basesink->segment, format);
3854 gst_segment_init (basesink->abidata.ABI.clip_segment, format);
3855 GST_OBJECT_LOCK (basesink);
3856 basesink->have_newsegment = TRUE;
3857 GST_OBJECT_UNLOCK (basesink);
3859 /* get the peer duration in bytes */
3860 result = gst_pad_query_peer_duration (pad, &format, &duration);
3862 GST_DEBUG_OBJECT (basesink,
3863 "setting duration in bytes to %" G_GINT64_FORMAT, duration);
3864 gst_segment_set_duration (basesink->abidata.ABI.clip_segment, format,
3866 gst_segment_set_duration (&basesink->segment, format, duration);
3868 GST_DEBUG_OBJECT (basesink, "unknown duration");
3871 if (bclass->activate_pull)
3872 result = bclass->activate_pull (basesink, TRUE);
3877 goto activate_failed;
3880 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PULL)) {
3881 g_warning ("Internal GStreamer activation error!!!");
3884 result = gst_base_sink_set_flushing (basesink, pad, TRUE);
3885 if (bclass->activate_pull)
3886 result &= bclass->activate_pull (basesink, FALSE);
3887 basesink->pad_mode = GST_ACTIVATE_NONE;
3888 /* clear any pending caps */
3889 GST_OBJECT_LOCK (basesink);
3890 gst_caps_replace (&basesink->priv->pull_caps, NULL);
3891 GST_OBJECT_UNLOCK (basesink);
3894 gst_object_unref (basesink);
3901 /* reset, as starting the thread failed */
3902 basesink->pad_mode = GST_ACTIVATE_NONE;
3904 GST_ERROR_OBJECT (basesink, "subclass failed to activate in pull mode");
3909 /* send an event to our sinkpad peer. */
3911 gst_base_sink_send_event (GstElement * element, GstEvent * event)
3914 GstBaseSink *basesink = GST_BASE_SINK (element);
3915 gboolean forward, result = TRUE;
3916 GstActivateMode mode;
3918 GST_OBJECT_LOCK (element);
3919 /* get the pad and the scheduling mode */
3920 pad = gst_object_ref (basesink->sinkpad);
3921 mode = basesink->pad_mode;
3922 GST_OBJECT_UNLOCK (element);
3924 /* only push UPSTREAM events upstream */
3925 forward = GST_EVENT_IS_UPSTREAM (event);
3927 switch (GST_EVENT_TYPE (event)) {
3928 case GST_EVENT_LATENCY:
3930 GstClockTime latency;
3932 gst_event_parse_latency (event, &latency);
3934 /* store the latency. We use this to adjust the running_time before syncing
3935 * it to the clock. */
3936 GST_OBJECT_LOCK (element);
3937 basesink->priv->latency = latency;
3938 if (!basesink->priv->have_latency)
3940 GST_OBJECT_UNLOCK (element);
3941 GST_DEBUG_OBJECT (basesink, "latency set to %" GST_TIME_FORMAT,
3942 GST_TIME_ARGS (latency));
3944 /* We forward this event so that all elements know about the global pipeline
3945 * latency. This is interesting for an element when it wants to figure out
3946 * when a particular piece of data will be rendered. */
3949 case GST_EVENT_SEEK:
3950 /* in pull mode we will execute the seek */
3951 if (mode == GST_ACTIVATE_PULL)
3952 result = gst_base_sink_perform_seek (basesink, pad, event);
3954 case GST_EVENT_STEP:
3955 result = gst_base_sink_perform_step (basesink, pad, event);
3963 result = gst_pad_push_event (pad, event);
3965 /* not forwarded, unref the event */
3966 gst_event_unref (event);
3969 gst_object_unref (pad);
3974 gst_base_sink_peer_query (GstBaseSink * sink, GstQuery * query)
3977 gboolean res = FALSE;
3979 if ((peer = gst_pad_get_peer (sink->sinkpad))) {
3980 res = gst_pad_query (peer, query);
3981 gst_object_unref (peer);
3986 /* get the end position of the last seen object, this is used
3987 * for EOS and for making sure that we don't report a position we
3988 * have not reached yet. With LOCK. */
3990 gst_base_sink_get_position_last (GstBaseSink * basesink, GstFormat format,
3994 GstSegment *segment;
3995 gboolean ret = TRUE;
3997 segment = &basesink->segment;
3998 oformat = segment->format;
4000 if (oformat == GST_FORMAT_TIME) {
4001 /* return last observed stream time, we keep the stream time around in the
4003 *cur = basesink->priv->current_sstop;
4005 /* convert last stop to stream time */
4006 *cur = gst_segment_to_stream_time (segment, oformat, segment->last_stop);
4009 if (*cur != -1 && oformat != format) {
4010 GST_OBJECT_UNLOCK (basesink);
4011 /* convert to the target format if we need to, release lock first */
4013 gst_pad_query_convert (basesink->sinkpad, oformat, *cur, &format, cur);
4016 GST_OBJECT_LOCK (basesink);
4019 GST_DEBUG_OBJECT (basesink, "POSITION: %" GST_TIME_FORMAT,
4020 GST_TIME_ARGS (*cur));
4025 /* get the position when we are PAUSED, this is the stream time of the buffer
4026 * that prerolled. If no buffer is prerolled (we are still flushing), this
4027 * value will be -1. With LOCK. */
4029 gst_base_sink_get_position_paused (GstBaseSink * basesink, GstFormat format,
4034 GstSegment *segment;
4037 /* we don't use the clip segment in pull mode, when seeking we update the
4038 * main segment directly with the new segment values without it having to be
4039 * activated by the rendering after preroll */
4040 if (basesink->pad_mode == GST_ACTIVATE_PUSH)
4041 segment = basesink->abidata.ABI.clip_segment;
4043 segment = &basesink->segment;
4044 oformat = segment->format;
4046 if (oformat == GST_FORMAT_TIME) {
4047 *cur = basesink->priv->current_sstart;
4048 if (segment->rate < 0.0 && basesink->priv->current_sstop != -1) {
4049 /* for reverse playback we prefer the stream time stop position if we have
4051 *cur = basesink->priv->current_sstop;
4054 *cur = gst_segment_to_stream_time (segment, oformat, segment->last_stop);
4057 time = segment->time;
4060 *cur = MAX (*cur, time);
4061 GST_DEBUG_OBJECT (basesink, "POSITION as max: %" GST_TIME_FORMAT
4062 ", time %" GST_TIME_FORMAT, GST_TIME_ARGS (*cur), GST_TIME_ARGS (time));
4064 /* we have no buffer, use the segment times. */
4065 if (segment->rate >= 0.0) {
4066 /* forward, next position is always the time of the segment */
4068 GST_DEBUG_OBJECT (basesink, "POSITION as time: %" GST_TIME_FORMAT,
4069 GST_TIME_ARGS (*cur));
4071 /* reverse, next expected timestamp is segment->stop. We use the function
4072 * to get things right for negative applied_rates. */
4073 *cur = gst_segment_to_stream_time (segment, oformat, segment->stop);
4074 GST_DEBUG_OBJECT (basesink, "reverse POSITION: %" GST_TIME_FORMAT,
4075 GST_TIME_ARGS (*cur));
4080 if (res && oformat != format) {
4081 GST_OBJECT_UNLOCK (basesink);
4083 gst_pad_query_convert (basesink->sinkpad, oformat, *cur, &format, cur);
4086 GST_OBJECT_LOCK (basesink);
4093 gst_base_sink_get_position (GstBaseSink * basesink, GstFormat format,
4094 gint64 * cur, gboolean * upstream)
4097 gboolean res = FALSE;
4098 GstFormat oformat, tformat;
4099 GstClockTime now, base, latency;
4100 gint64 time, accum, duration;
4104 GST_OBJECT_LOCK (basesink);
4105 /* our intermediate time format */
4106 tformat = GST_FORMAT_TIME;
4107 /* get the format in the segment */
4108 oformat = basesink->segment.format;
4110 /* can only give answer based on the clock if not EOS */
4111 if (G_UNLIKELY (basesink->eos))
4114 /* we can only get the segment when we are not NULL or READY */
4115 if (!basesink->have_newsegment)
4118 /* when not in PLAYING or when we're busy with a state change, we
4119 * cannot read from the clock so we report time based on the
4120 * last seen timestamp. */
4121 if (GST_STATE (basesink) != GST_STATE_PLAYING ||
4122 GST_STATE_PENDING (basesink) != GST_STATE_VOID_PENDING)
4125 /* we need to sync on the clock. */
4126 if (basesink->sync == FALSE)
4129 /* and we need a clock */
4130 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (basesink)) == NULL))
4133 /* collect all data we need holding the lock */
4134 if (GST_CLOCK_TIME_IS_VALID (basesink->segment.time))
4135 time = basesink->segment.time;
4139 if (GST_CLOCK_TIME_IS_VALID (basesink->segment.stop))
4140 duration = basesink->segment.stop - basesink->segment.start;
4144 base = GST_ELEMENT_CAST (basesink)->base_time;
4145 accum = basesink->segment.accum;
4146 rate = basesink->segment.rate * basesink->segment.applied_rate;
4147 latency = basesink->priv->latency;
4149 gst_object_ref (clock);
4151 /* this function might release the LOCK */
4152 gst_base_sink_get_position_last (basesink, format, &last);
4154 /* need to release the object lock before we can get the time,
4155 * a clock might take the LOCK of the provider, which could be
4156 * a basesink subclass. */
4157 GST_OBJECT_UNLOCK (basesink);
4159 now = gst_clock_get_time (clock);
4161 if (oformat != tformat) {
4162 /* convert accum, time and duration to time */
4163 if (!gst_pad_query_convert (basesink->sinkpad, oformat, accum, &tformat,
4165 goto convert_failed;
4166 if (!gst_pad_query_convert (basesink->sinkpad, oformat, duration, &tformat,
4168 goto convert_failed;
4169 if (!gst_pad_query_convert (basesink->sinkpad, oformat, time, &tformat,
4171 goto convert_failed;
4174 /* subtract base time and accumulated time from the clock time.
4175 * Make sure we don't go negative. This is the current time in
4176 * the segment which we need to scale with the combined
4177 * rate and applied rate. */
4180 base = MIN (now, base);
4182 /* for negative rates we need to count back from from the segment
4187 *cur = time + gst_guint64_to_gdouble (now - base) * rate;
4189 /* never report more than last seen position */
4191 *cur = MIN (last, *cur);
4193 gst_object_unref (clock);
4195 GST_DEBUG_OBJECT (basesink,
4196 "now %" GST_TIME_FORMAT " - base %" GST_TIME_FORMAT " - accum %"
4197 GST_TIME_FORMAT " + time %" GST_TIME_FORMAT,
4198 GST_TIME_ARGS (now), GST_TIME_ARGS (base),
4199 GST_TIME_ARGS (accum), GST_TIME_ARGS (time));
4201 if (oformat != format) {
4202 /* convert time to final format */
4203 if (!gst_pad_query_convert (basesink->sinkpad, tformat, *cur, &format, cur))
4204 goto convert_failed;
4210 GST_DEBUG_OBJECT (basesink, "res: %d, POSITION: %" GST_TIME_FORMAT,
4211 res, GST_TIME_ARGS (*cur));
4217 GST_DEBUG_OBJECT (basesink, "position in EOS");
4218 res = gst_base_sink_get_position_last (basesink, format, cur);
4219 GST_OBJECT_UNLOCK (basesink);
4224 GST_DEBUG_OBJECT (basesink, "position in PAUSED");
4225 res = gst_base_sink_get_position_paused (basesink, format, cur);
4226 GST_OBJECT_UNLOCK (basesink);
4231 /* in NULL or READY we always return FALSE and -1 */
4232 GST_DEBUG_OBJECT (basesink, "position in wrong state, return -1");
4235 GST_OBJECT_UNLOCK (basesink);
4240 /* report last seen timestamp if any, else ask upstream to answer */
4241 if ((*cur = basesink->priv->current_sstart) != -1)
4246 GST_DEBUG_OBJECT (basesink, "no sync, res %d, POSITION %" GST_TIME_FORMAT,
4247 res, GST_TIME_ARGS (*cur));
4248 GST_OBJECT_UNLOCK (basesink);
4253 GST_DEBUG_OBJECT (basesink, "convert failed, try upstream");
4260 gst_base_sink_query (GstElement * element, GstQuery * query)
4262 gboolean res = FALSE;
4264 GstBaseSink *basesink = GST_BASE_SINK (element);
4266 switch (GST_QUERY_TYPE (query)) {
4267 case GST_QUERY_POSITION:
4271 gboolean upstream = FALSE;
4273 gst_query_parse_position (query, &format, NULL);
4275 GST_DEBUG_OBJECT (basesink, "position format %d", format);
4277 /* first try to get the position based on the clock */
4279 gst_base_sink_get_position (basesink, format, &cur, &upstream))) {
4280 gst_query_set_position (query, format, cur);
4281 } else if (upstream) {
4282 /* fallback to peer query */
4283 res = gst_base_sink_peer_query (basesink, query);
4287 case GST_QUERY_DURATION:
4289 GstFormat format, uformat;
4290 gint64 duration, uduration;
4292 gst_query_parse_duration (query, &format, NULL);
4294 GST_DEBUG_OBJECT (basesink, "duration query in format %s",
4295 gst_format_get_name (format));
4297 if (basesink->pad_mode == GST_ACTIVATE_PULL) {
4298 uformat = GST_FORMAT_BYTES;
4300 /* get the duration in bytes, in pull mode that's all we are sure to
4301 * know. We have to explicitly get this value from upstream instead of
4302 * using our cached value because it might change. Duration caching
4303 * should be done at a higher level. */
4304 res = gst_pad_query_peer_duration (basesink->sinkpad, &uformat,
4307 gst_segment_set_duration (&basesink->segment, uformat, uduration);
4308 if (format != uformat) {
4309 /* convert to the requested format */
4310 res = gst_pad_query_convert (basesink->sinkpad, uformat, uduration,
4311 &format, &duration);
4313 duration = uduration;
4316 /* set the result */
4317 gst_query_set_duration (query, format, duration);
4321 /* in push mode we simply forward upstream */
4322 res = gst_base_sink_peer_query (basesink, query);
4326 case GST_QUERY_LATENCY:
4328 gboolean live, us_live;
4329 GstClockTime min, max;
4331 if ((res = gst_base_sink_query_latency (basesink, &live, &us_live, &min,
4333 gst_query_set_latency (query, live, min, max);
4337 case GST_QUERY_JITTER:
4339 case GST_QUERY_RATE:
4340 /* gst_query_set_rate (query, basesink->segment_rate); */
4343 case GST_QUERY_SEGMENT:
4345 /* FIXME, bring start/stop to stream time */
4346 gst_query_set_segment (query, basesink->segment.rate,
4347 GST_FORMAT_TIME, basesink->segment.start, basesink->segment.stop);
4350 case GST_QUERY_SEEKING:
4351 case GST_QUERY_CONVERT:
4352 case GST_QUERY_FORMATS:
4354 res = gst_base_sink_peer_query (basesink, query);
4360 static GstStateChangeReturn
4361 gst_base_sink_change_state (GstElement * element, GstStateChange transition)
4363 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
4364 GstBaseSink *basesink = GST_BASE_SINK (element);
4365 GstBaseSinkClass *bclass;
4366 GstBaseSinkPrivate *priv;
4368 priv = basesink->priv;
4370 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4372 switch (transition) {
4373 case GST_STATE_CHANGE_NULL_TO_READY:
4375 if (!bclass->start (basesink))
4378 case GST_STATE_CHANGE_READY_TO_PAUSED:
4379 /* need to complete preroll before this state change completes, there
4380 * is no data flow in READY so we can safely assume we need to preroll. */
4381 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4382 GST_DEBUG_OBJECT (basesink, "READY to PAUSED");
4383 basesink->have_newsegment = FALSE;
4384 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
4385 gst_segment_init (basesink->abidata.ABI.clip_segment,
4386 GST_FORMAT_UNDEFINED);
4387 basesink->offset = 0;
4388 basesink->have_preroll = FALSE;
4389 priv->step_unlock = FALSE;
4390 basesink->need_preroll = TRUE;
4391 basesink->playing_async = TRUE;
4392 priv->current_sstart = -1;
4393 priv->current_sstop = -1;
4394 priv->eos_rtime = -1;
4396 basesink->eos = FALSE;
4397 priv->received_eos = FALSE;
4398 gst_base_sink_reset_qos (basesink);
4399 priv->commited = FALSE;
4400 priv->call_preroll = TRUE;
4401 priv->current_step.valid = FALSE;
4402 priv->pending_step.valid = FALSE;
4403 if (priv->async_enabled) {
4404 GST_DEBUG_OBJECT (basesink, "doing async state change");
4405 /* when async enabled, post async-start message and return ASYNC from
4406 * the state change function */
4407 ret = GST_STATE_CHANGE_ASYNC;
4408 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4409 gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
4411 priv->have_latency = TRUE;
4413 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4415 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
4416 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4417 if (!gst_base_sink_needs_preroll (basesink)) {
4418 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, don't need preroll");
4419 /* no preroll needed anymore now. */
4420 basesink->playing_async = FALSE;
4421 basesink->need_preroll = FALSE;
4422 if (basesink->eos) {
4423 GstMessage *message;
4425 /* need to post EOS message here */
4426 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
4427 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
4428 gst_message_set_seqnum (message, basesink->priv->seqnum);
4429 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
4431 GST_DEBUG_OBJECT (basesink, "signal preroll");
4432 GST_PAD_PREROLL_SIGNAL (basesink->sinkpad);
4435 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, we are not prerolled");
4436 basesink->need_preroll = TRUE;
4437 basesink->playing_async = TRUE;
4438 priv->call_preroll = TRUE;
4439 priv->commited = FALSE;
4440 if (priv->async_enabled) {
4441 GST_DEBUG_OBJECT (basesink, "doing async state change");
4442 ret = GST_STATE_CHANGE_ASYNC;
4443 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4444 gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
4447 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4454 GstStateChangeReturn bret;
4456 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4457 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
4458 goto activate_failed;
4461 switch (transition) {
4462 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
4463 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED");
4464 /* FIXME, make sure we cannot enter _render first */
4466 /* we need to call ::unlock before locking PREROLL_LOCK
4467 * since we lock it before going into ::render */
4469 bclass->unlock (basesink);
4471 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4472 /* now that we have the PREROLL lock, clear our unlock request */
4473 if (bclass->unlock_stop)
4474 bclass->unlock_stop (basesink);
4476 /* we need preroll again and we set the flag before unlocking the clockid
4477 * because if the clockid is unlocked before a current buffer expired, we
4478 * can use that buffer to preroll with */
4479 basesink->need_preroll = TRUE;
4481 if (basesink->clock_id) {
4482 gst_clock_id_unschedule (basesink->clock_id);
4485 /* if we don't have a preroll buffer we need to wait for a preroll and
4487 if (!gst_base_sink_needs_preroll (basesink)) {
4488 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, we are prerolled");
4489 basesink->playing_async = FALSE;
4491 if (GST_STATE_TARGET (GST_ELEMENT (basesink)) <= GST_STATE_READY) {
4492 ret = GST_STATE_CHANGE_SUCCESS;
4494 GST_DEBUG_OBJECT (basesink,
4495 "PLAYING to PAUSED, we are not prerolled");
4496 basesink->playing_async = TRUE;
4497 priv->commited = FALSE;
4498 priv->call_preroll = TRUE;
4499 if (priv->async_enabled) {
4500 GST_DEBUG_OBJECT (basesink, "doing async state change");
4501 ret = GST_STATE_CHANGE_ASYNC;
4502 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4503 gst_message_new_async_start (GST_OBJECT_CAST (basesink),
4508 GST_DEBUG_OBJECT (basesink, "rendered: %" G_GUINT64_FORMAT
4509 ", dropped: %" G_GUINT64_FORMAT, priv->rendered, priv->dropped);
4511 gst_base_sink_reset_qos (basesink);
4512 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4514 case GST_STATE_CHANGE_PAUSED_TO_READY:
4515 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4516 /* start by reseting our position state with the object lock so that the
4517 * position query gets the right idea. We do this before we post the
4518 * messages so that the message handlers pick this up. */
4519 GST_OBJECT_LOCK (basesink);
4520 basesink->have_newsegment = FALSE;
4521 priv->current_sstart = -1;
4522 priv->current_sstop = -1;
4523 priv->have_latency = FALSE;
4524 GST_OBJECT_UNLOCK (basesink);
4526 gst_base_sink_set_last_buffer (basesink, NULL);
4527 priv->call_preroll = FALSE;
4529 if (!priv->commited) {
4530 if (priv->async_enabled) {
4531 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, posting async-done");
4533 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4534 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
4535 GST_STATE_PLAYING, GST_STATE_PAUSED, GST_STATE_READY));
4537 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4538 gst_message_new_async_done (GST_OBJECT_CAST (basesink)));
4540 priv->commited = TRUE;
4542 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, don't need_preroll");
4544 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4546 case GST_STATE_CHANGE_READY_TO_NULL:
4548 if (!bclass->stop (basesink)) {
4549 GST_WARNING_OBJECT (basesink, "failed to stop");
4552 gst_base_sink_set_last_buffer (basesink, NULL);
4553 priv->call_preroll = FALSE;
4564 GST_DEBUG_OBJECT (basesink, "failed to start");
4565 return GST_STATE_CHANGE_FAILURE;
4569 GST_DEBUG_OBJECT (basesink,
4570 "element failed to change states -- activation problem?");
4571 return GST_STATE_CHANGE_FAILURE;