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 gboolean flush; /* if this was a flushing step */
168 gboolean intermediate; /* if this is an intermediate step */
169 gboolean need_preroll; /* if we need preroll after this step */
172 /* FIXME, some stuff in ABI.data and other in Private...
173 * Make up your mind please.
175 struct _GstBaseSinkPrivate
177 gint qos_enabled; /* ATOMIC */
178 gboolean async_enabled;
179 GstClockTimeDiff ts_offset;
180 GstClockTime render_delay;
182 /* start, stop of current buffer, stream time, used to report position */
183 GstClockTime current_sstart;
184 GstClockTime current_sstop;
186 /* start, stop and jitter of current buffer, running time */
187 GstClockTime current_rstart;
188 GstClockTime current_rstop;
189 GstClockTimeDiff current_jitter;
191 /* EOS sync time in running time */
192 GstClockTime eos_rtime;
194 /* last buffer that arrived in time, running time */
195 GstClockTime last_in_time;
196 /* when the last buffer left the sink, running time */
197 GstClockTime last_left;
199 /* running averages go here these are done on running time */
201 GstClockTime avg_duration;
204 /* these are done on system time. avg_jitter and avg_render are
205 * compared to eachother to see if the rendering time takes a
206 * huge amount of the processing, If so we are flooded with
208 GstClockTime last_left_systime;
209 GstClockTime avg_jitter;
210 GstClockTime start, stop;
211 GstClockTime avg_render;
213 /* number of rendered and dropped frames */
218 GstClockTime latency;
220 /* if we already commited the state */
223 /* when we received EOS */
224 gboolean received_eos;
226 /* when we are prerolled and able to report latency */
227 gboolean have_latency;
229 /* the last buffer we prerolled or rendered. Useful for making snapshots */
230 GstBuffer *last_buffer;
232 /* caps for pull based scheduling */
235 /* blocksize for pulling */
240 /* seqnum of the stream */
243 gboolean call_preroll;
244 gboolean step_unlock;
246 /* we have a pending and a current step operation */
247 GstStepInfo current_step;
248 GstStepInfo pending_step;
251 #define DO_RUNNING_AVG(avg,val,size) (((val) + ((size)-1) * (avg)) / (size))
253 /* generic running average, this has a neutral window size */
254 #define UPDATE_RUNNING_AVG(avg,val) DO_RUNNING_AVG(avg,val,8)
256 /* the windows for these running averages are experimentally obtained.
257 * possitive values get averaged more while negative values use a small
258 * window so we can react faster to badness. */
259 #define UPDATE_RUNNING_AVG_P(avg,val) DO_RUNNING_AVG(avg,val,16)
260 #define UPDATE_RUNNING_AVG_N(avg,val) DO_RUNNING_AVG(avg,val,4)
262 /* BaseSink properties */
264 #define DEFAULT_CAN_ACTIVATE_PULL FALSE /* fixme: enable me */
265 #define DEFAULT_CAN_ACTIVATE_PUSH TRUE
267 #define DEFAULT_PREROLL_QUEUE_LEN 0
268 #define DEFAULT_SYNC TRUE
269 #define DEFAULT_MAX_LATENESS -1
270 #define DEFAULT_QOS FALSE
271 #define DEFAULT_ASYNC TRUE
272 #define DEFAULT_TS_OFFSET 0
273 #define DEFAULT_BLOCKSIZE 4096
274 #define DEFAULT_RENDER_DELAY 0
279 PROP_PREROLL_QUEUE_LEN,
291 static GstElementClass *parent_class = NULL;
293 static void gst_base_sink_class_init (GstBaseSinkClass * klass);
294 static void gst_base_sink_init (GstBaseSink * trans, gpointer g_class);
295 static void gst_base_sink_finalize (GObject * object);
298 gst_base_sink_get_type (void)
300 static volatile gsize base_sink_type = 0;
302 if (g_once_init_enter (&base_sink_type)) {
304 static const GTypeInfo base_sink_info = {
305 sizeof (GstBaseSinkClass),
308 (GClassInitFunc) gst_base_sink_class_init,
311 sizeof (GstBaseSink),
313 (GInstanceInitFunc) gst_base_sink_init,
316 _type = g_type_register_static (GST_TYPE_ELEMENT,
317 "GstBaseSink", &base_sink_info, G_TYPE_FLAG_ABSTRACT);
318 g_once_init_leave (&base_sink_type, _type);
320 return base_sink_type;
323 static void gst_base_sink_set_property (GObject * object, guint prop_id,
324 const GValue * value, GParamSpec * pspec);
325 static void gst_base_sink_get_property (GObject * object, guint prop_id,
326 GValue * value, GParamSpec * pspec);
328 static gboolean gst_base_sink_send_event (GstElement * element,
330 static gboolean gst_base_sink_query (GstElement * element, GstQuery * query);
332 static GstCaps *gst_base_sink_get_caps (GstBaseSink * sink);
333 static gboolean gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps);
334 static GstFlowReturn gst_base_sink_buffer_alloc (GstBaseSink * sink,
335 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
336 static void gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
337 GstClockTime * start, GstClockTime * end);
338 static gboolean gst_base_sink_set_flushing (GstBaseSink * basesink,
339 GstPad * pad, gboolean flushing);
340 static gboolean gst_base_sink_default_activate_pull (GstBaseSink * basesink,
342 static gboolean gst_base_sink_default_do_seek (GstBaseSink * sink,
343 GstSegment * segment);
344 static gboolean gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
345 GstEvent * event, GstSegment * segment);
347 static GstStateChangeReturn gst_base_sink_change_state (GstElement * element,
348 GstStateChange transition);
350 static GstFlowReturn gst_base_sink_chain (GstPad * pad, GstBuffer * buffer);
351 static void gst_base_sink_loop (GstPad * pad);
352 static gboolean gst_base_sink_pad_activate (GstPad * pad);
353 static gboolean gst_base_sink_pad_activate_push (GstPad * pad, gboolean active);
354 static gboolean gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active);
355 static gboolean gst_base_sink_event (GstPad * pad, GstEvent * event);
356 static gboolean gst_base_sink_peer_query (GstBaseSink * sink, GstQuery * query);
358 static gboolean gst_base_sink_negotiate_pull (GstBaseSink * basesink);
360 /* check if an object was too late */
361 static gboolean gst_base_sink_is_too_late (GstBaseSink * basesink,
362 GstMiniObject * obj, GstClockTime start, GstClockTime stop,
363 GstClockReturn status, GstClockTimeDiff jitter);
364 static GstFlowReturn gst_base_sink_preroll_object (GstBaseSink * basesink,
365 GstMiniObject * obj);
368 gst_base_sink_class_init (GstBaseSinkClass * klass)
370 GObjectClass *gobject_class;
371 GstElementClass *gstelement_class;
373 gobject_class = G_OBJECT_CLASS (klass);
374 gstelement_class = GST_ELEMENT_CLASS (klass);
376 GST_DEBUG_CATEGORY_INIT (gst_base_sink_debug, "basesink", 0,
379 g_type_class_add_private (klass, sizeof (GstBaseSinkPrivate));
381 parent_class = g_type_class_peek_parent (klass);
383 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_sink_finalize);
384 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_base_sink_set_property);
385 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_base_sink_get_property);
387 /* FIXME, this next value should be configured using an event from the
388 * upstream element, ie, the BUFFER_SIZE event. */
389 g_object_class_install_property (gobject_class, PROP_PREROLL_QUEUE_LEN,
390 g_param_spec_uint ("preroll-queue-len", "Preroll queue length",
391 "Number of buffers to queue during preroll", 0, G_MAXUINT,
392 DEFAULT_PREROLL_QUEUE_LEN,
393 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
395 g_object_class_install_property (gobject_class, PROP_SYNC,
396 g_param_spec_boolean ("sync", "Sync", "Sync on the clock", DEFAULT_SYNC,
397 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
399 g_object_class_install_property (gobject_class, PROP_MAX_LATENESS,
400 g_param_spec_int64 ("max-lateness", "Max Lateness",
401 "Maximum number of nanoseconds that a buffer can be late before it "
402 "is dropped (-1 unlimited)", -1, G_MAXINT64, DEFAULT_MAX_LATENESS,
403 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
405 g_object_class_install_property (gobject_class, PROP_QOS,
406 g_param_spec_boolean ("qos", "Qos",
407 "Generate Quality-of-Service events upstream", DEFAULT_QOS,
408 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
412 * If set to #TRUE, the basesink will perform asynchronous state changes.
413 * When set to #FALSE, the sink will not signal the parent when it prerolls.
414 * Use this option when dealing with sparse streams or when synchronisation is
419 g_object_class_install_property (gobject_class, PROP_ASYNC,
420 g_param_spec_boolean ("async", "Async",
421 "Go asynchronously to PAUSED", DEFAULT_ASYNC,
422 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
424 * GstBaseSink:ts-offset
426 * Controls the final synchronisation, a negative value will render the buffer
427 * earlier while a positive value delays playback. This property can be
428 * used to fix synchronisation in bad files.
432 g_object_class_install_property (gobject_class, PROP_TS_OFFSET,
433 g_param_spec_int64 ("ts-offset", "TS Offset",
434 "Timestamp offset in nanoseconds", G_MININT64, G_MAXINT64,
435 DEFAULT_TS_OFFSET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
437 * GstBaseSink:last-buffer
439 * The last buffer that arrived in the sink and was used for preroll or for
440 * rendering. This property can be used to generate thumbnails. This property
441 * can be NULL when the sink has not yet received a bufer.
445 g_object_class_install_property (gobject_class, PROP_LAST_BUFFER,
446 gst_param_spec_mini_object ("last-buffer", "Last Buffer",
447 "The last buffer received in the sink", GST_TYPE_BUFFER,
448 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
450 * GstBaseSink:blocksize
452 * The amount of bytes to pull when operating in pull mode.
456 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
457 g_param_spec_uint ("blocksize", "Block size",
458 "Size in bytes to pull per buffer (0 = default)", 0, G_MAXUINT,
459 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
461 * GstBaseSink:render-delay
463 * The additional delay between synchronisation and actual rendering of the
464 * media. This property will add additional latency to the device in order to
465 * make other sinks compensate for the delay.
469 g_object_class_install_property (gobject_class, PROP_RENDER_DELAY,
470 g_param_spec_uint64 ("render-delay", "Render Delay",
471 "Additional render delay of the sink in nanoseconds", 0, G_MAXUINT64,
472 DEFAULT_RENDER_DELAY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
474 gstelement_class->change_state =
475 GST_DEBUG_FUNCPTR (gst_base_sink_change_state);
476 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_sink_send_event);
477 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_base_sink_query);
479 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_sink_get_caps);
480 klass->set_caps = GST_DEBUG_FUNCPTR (gst_base_sink_set_caps);
481 klass->buffer_alloc = GST_DEBUG_FUNCPTR (gst_base_sink_buffer_alloc);
482 klass->get_times = GST_DEBUG_FUNCPTR (gst_base_sink_get_times);
483 klass->activate_pull =
484 GST_DEBUG_FUNCPTR (gst_base_sink_default_activate_pull);
488 gst_base_sink_pad_getcaps (GstPad * pad)
490 GstBaseSinkClass *bclass;
492 GstCaps *caps = NULL;
494 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
495 bclass = GST_BASE_SINK_GET_CLASS (bsink);
497 if (bsink->pad_mode == GST_ACTIVATE_PULL) {
498 /* if we are operating in pull mode we only accept the negotiated caps */
499 GST_OBJECT_LOCK (pad);
500 if ((caps = GST_PAD_CAPS (pad)))
502 GST_OBJECT_UNLOCK (pad);
505 if (bclass->get_caps)
506 caps = bclass->get_caps (bsink);
509 GstPadTemplate *pad_template;
512 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass),
514 if (pad_template != NULL) {
515 caps = gst_caps_ref (gst_pad_template_get_caps (pad_template));
519 gst_object_unref (bsink);
525 gst_base_sink_pad_setcaps (GstPad * pad, GstCaps * caps)
527 GstBaseSinkClass *bclass;
531 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
532 bclass = GST_BASE_SINK_GET_CLASS (bsink);
534 if (res && bclass->set_caps)
535 res = bclass->set_caps (bsink, caps);
537 gst_object_unref (bsink);
543 gst_base_sink_pad_fixate (GstPad * pad, GstCaps * caps)
545 GstBaseSinkClass *bclass;
548 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
549 bclass = GST_BASE_SINK_GET_CLASS (bsink);
552 bclass->fixate (bsink, caps);
554 gst_object_unref (bsink);
558 gst_base_sink_pad_buffer_alloc (GstPad * pad, guint64 offset, guint size,
559 GstCaps * caps, GstBuffer ** buf)
561 GstBaseSinkClass *bclass;
563 GstFlowReturn result = GST_FLOW_OK;
565 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
566 bclass = GST_BASE_SINK_GET_CLASS (bsink);
568 if (bclass->buffer_alloc)
569 result = bclass->buffer_alloc (bsink, offset, size, caps, buf);
571 *buf = NULL; /* fallback in gstpad.c will allocate generic buffer */
573 gst_object_unref (bsink);
579 gst_base_sink_init (GstBaseSink * basesink, gpointer g_class)
581 GstPadTemplate *pad_template;
582 GstBaseSinkPrivate *priv;
584 basesink->priv = priv = GST_BASE_SINK_GET_PRIVATE (basesink);
587 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
588 g_return_if_fail (pad_template != NULL);
590 basesink->sinkpad = gst_pad_new_from_template (pad_template, "sink");
592 gst_pad_set_getcaps_function (basesink->sinkpad,
593 GST_DEBUG_FUNCPTR (gst_base_sink_pad_getcaps));
594 gst_pad_set_setcaps_function (basesink->sinkpad,
595 GST_DEBUG_FUNCPTR (gst_base_sink_pad_setcaps));
596 gst_pad_set_fixatecaps_function (basesink->sinkpad,
597 GST_DEBUG_FUNCPTR (gst_base_sink_pad_fixate));
598 gst_pad_set_bufferalloc_function (basesink->sinkpad,
599 GST_DEBUG_FUNCPTR (gst_base_sink_pad_buffer_alloc));
600 gst_pad_set_activate_function (basesink->sinkpad,
601 GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate));
602 gst_pad_set_activatepush_function (basesink->sinkpad,
603 GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate_push));
604 gst_pad_set_activatepull_function (basesink->sinkpad,
605 GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate_pull));
606 gst_pad_set_event_function (basesink->sinkpad,
607 GST_DEBUG_FUNCPTR (gst_base_sink_event));
608 gst_pad_set_chain_function (basesink->sinkpad,
609 GST_DEBUG_FUNCPTR (gst_base_sink_chain));
610 gst_element_add_pad (GST_ELEMENT_CAST (basesink), basesink->sinkpad);
612 basesink->pad_mode = GST_ACTIVATE_NONE;
613 basesink->preroll_queue = g_queue_new ();
614 basesink->abidata.ABI.clip_segment = gst_segment_new ();
615 priv->have_latency = FALSE;
617 basesink->can_activate_push = DEFAULT_CAN_ACTIVATE_PUSH;
618 basesink->can_activate_pull = DEFAULT_CAN_ACTIVATE_PULL;
620 basesink->sync = DEFAULT_SYNC;
621 basesink->abidata.ABI.max_lateness = DEFAULT_MAX_LATENESS;
622 g_atomic_int_set (&priv->qos_enabled, DEFAULT_QOS);
623 priv->async_enabled = DEFAULT_ASYNC;
624 priv->ts_offset = DEFAULT_TS_OFFSET;
625 priv->render_delay = DEFAULT_RENDER_DELAY;
626 priv->blocksize = DEFAULT_BLOCKSIZE;
628 GST_OBJECT_FLAG_SET (basesink, GST_ELEMENT_IS_SINK);
632 gst_base_sink_finalize (GObject * object)
634 GstBaseSink *basesink;
636 basesink = GST_BASE_SINK (object);
638 g_queue_free (basesink->preroll_queue);
639 gst_segment_free (basesink->abidata.ABI.clip_segment);
641 G_OBJECT_CLASS (parent_class)->finalize (object);
645 * gst_base_sink_set_sync:
647 * @sync: the new sync value.
649 * Configures @sink to synchronize on the clock or not. When
650 * @sync is FALSE, incomming samples will be played as fast as
651 * possible. If @sync is TRUE, the timestamps of the incomming
652 * buffers will be used to schedule the exact render time of its
658 gst_base_sink_set_sync (GstBaseSink * sink, gboolean sync)
660 g_return_if_fail (GST_IS_BASE_SINK (sink));
662 GST_OBJECT_LOCK (sink);
664 GST_OBJECT_UNLOCK (sink);
668 * gst_base_sink_get_sync:
671 * Checks if @sink is currently configured to synchronize against the
674 * Returns: TRUE if the sink is configured to synchronize against the clock.
679 gst_base_sink_get_sync (GstBaseSink * sink)
683 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
685 GST_OBJECT_LOCK (sink);
687 GST_OBJECT_UNLOCK (sink);
693 * gst_base_sink_set_max_lateness:
695 * @max_lateness: the new max lateness value.
697 * Sets the new max lateness value to @max_lateness. This value is
698 * used to decide if a buffer should be dropped or not based on the
699 * buffer timestamp and the current clock time. A value of -1 means
705 gst_base_sink_set_max_lateness (GstBaseSink * sink, gint64 max_lateness)
707 g_return_if_fail (GST_IS_BASE_SINK (sink));
709 GST_OBJECT_LOCK (sink);
710 sink->abidata.ABI.max_lateness = max_lateness;
711 GST_OBJECT_UNLOCK (sink);
715 * gst_base_sink_get_max_lateness:
718 * Gets the max lateness value. See gst_base_sink_set_max_lateness for
721 * Returns: The maximum time in nanoseconds that a buffer can be late
722 * before it is dropped and not rendered. A value of -1 means an
728 gst_base_sink_get_max_lateness (GstBaseSink * sink)
732 g_return_val_if_fail (GST_IS_BASE_SINK (sink), -1);
734 GST_OBJECT_LOCK (sink);
735 res = sink->abidata.ABI.max_lateness;
736 GST_OBJECT_UNLOCK (sink);
742 * gst_base_sink_set_qos_enabled:
744 * @enabled: the new qos value.
746 * Configures @sink to send Quality-of-Service events upstream.
751 gst_base_sink_set_qos_enabled (GstBaseSink * sink, gboolean enabled)
753 g_return_if_fail (GST_IS_BASE_SINK (sink));
755 g_atomic_int_set (&sink->priv->qos_enabled, enabled);
759 * gst_base_sink_is_qos_enabled:
762 * Checks if @sink is currently configured to send Quality-of-Service events
765 * Returns: TRUE if the sink is configured to perform Quality-of-Service.
770 gst_base_sink_is_qos_enabled (GstBaseSink * sink)
774 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
776 res = g_atomic_int_get (&sink->priv->qos_enabled);
782 * gst_base_sink_set_async_enabled:
784 * @enabled: the new async value.
786 * Configures @sink to perform all state changes asynchronusly. When async is
787 * disabled, the sink will immediatly go to PAUSED instead of waiting for a
788 * preroll buffer. This feature is usefull if the sink does not synchronize
789 * against the clock or when it is dealing with sparse streams.
794 gst_base_sink_set_async_enabled (GstBaseSink * sink, gboolean enabled)
796 g_return_if_fail (GST_IS_BASE_SINK (sink));
798 GST_PAD_PREROLL_LOCK (sink->sinkpad);
799 sink->priv->async_enabled = enabled;
800 GST_LOG_OBJECT (sink, "set async enabled to %d", enabled);
801 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
805 * gst_base_sink_is_async_enabled:
808 * Checks if @sink is currently configured to perform asynchronous state
811 * Returns: TRUE if the sink is configured to perform asynchronous state
817 gst_base_sink_is_async_enabled (GstBaseSink * sink)
821 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
823 GST_PAD_PREROLL_LOCK (sink->sinkpad);
824 res = sink->priv->async_enabled;
825 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
831 * gst_base_sink_set_ts_offset:
833 * @offset: the new offset
835 * Adjust the synchronisation of @sink with @offset. A negative value will
836 * render buffers earlier than their timestamp. A positive value will delay
837 * rendering. This function can be used to fix playback of badly timestamped
843 gst_base_sink_set_ts_offset (GstBaseSink * sink, GstClockTimeDiff offset)
845 g_return_if_fail (GST_IS_BASE_SINK (sink));
847 GST_OBJECT_LOCK (sink);
848 sink->priv->ts_offset = offset;
849 GST_LOG_OBJECT (sink, "set time offset to %" G_GINT64_FORMAT, offset);
850 GST_OBJECT_UNLOCK (sink);
854 * gst_base_sink_get_ts_offset:
857 * Get the synchronisation offset of @sink.
859 * Returns: The synchronisation offset.
864 gst_base_sink_get_ts_offset (GstBaseSink * sink)
866 GstClockTimeDiff res;
868 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
870 GST_OBJECT_LOCK (sink);
871 res = sink->priv->ts_offset;
872 GST_OBJECT_UNLOCK (sink);
878 * gst_base_sink_get_last_buffer:
881 * Get the last buffer that arrived in the sink and was used for preroll or for
882 * rendering. This property can be used to generate thumbnails.
884 * The #GstCaps on the buffer can be used to determine the type of the buffer.
886 * Returns: a #GstBuffer. gst_buffer_unref() after usage. This function returns
887 * NULL when no buffer has arrived in the sink yet or when the sink is not in
893 gst_base_sink_get_last_buffer (GstBaseSink * sink)
897 g_return_val_if_fail (GST_IS_BASE_SINK (sink), NULL);
899 GST_OBJECT_LOCK (sink);
900 if ((res = sink->priv->last_buffer))
901 gst_buffer_ref (res);
902 GST_OBJECT_UNLOCK (sink);
908 gst_base_sink_set_last_buffer (GstBaseSink * sink, GstBuffer * buffer)
912 GST_OBJECT_LOCK (sink);
913 old = sink->priv->last_buffer;
914 if (G_LIKELY (old != buffer)) {
915 GST_DEBUG_OBJECT (sink, "setting last buffer to %p", buffer);
916 if (G_LIKELY (buffer))
917 gst_buffer_ref (buffer);
918 sink->priv->last_buffer = buffer;
922 GST_OBJECT_UNLOCK (sink);
924 /* avoid unreffing with the lock because cleanup code might want to take the
927 gst_buffer_unref (old);
931 * gst_base_sink_get_latency:
934 * Get the currently configured latency.
936 * Returns: The configured latency.
941 gst_base_sink_get_latency (GstBaseSink * sink)
945 GST_OBJECT_LOCK (sink);
946 res = sink->priv->latency;
947 GST_OBJECT_UNLOCK (sink);
953 * gst_base_sink_query_latency:
955 * @live: if the sink is live
956 * @upstream_live: if an upstream element is live
957 * @min_latency: the min latency of the upstream elements
958 * @max_latency: the max latency of the upstream elements
960 * Query the sink for the latency parameters. The latency will be queried from
961 * the upstream elements. @live will be TRUE if @sink is configured to
962 * synchronize against the clock. @upstream_live will be TRUE if an upstream
965 * If both @live and @upstream_live are TRUE, the sink will want to compensate
966 * for the latency introduced by the upstream elements by setting the
967 * @min_latency to a strictly possitive value.
969 * This function is mostly used by subclasses.
971 * Returns: TRUE if the query succeeded.
976 gst_base_sink_query_latency (GstBaseSink * sink, gboolean * live,
977 gboolean * upstream_live, GstClockTime * min_latency,
978 GstClockTime * max_latency)
980 gboolean l, us_live, res, have_latency;
981 GstClockTime min, max, render_delay;
983 GstClockTime us_min, us_max;
985 /* we are live when we sync to the clock */
986 GST_OBJECT_LOCK (sink);
988 have_latency = sink->priv->have_latency;
989 render_delay = sink->priv->render_delay;
990 GST_OBJECT_UNLOCK (sink);
992 /* assume no latency */
998 GST_DEBUG_OBJECT (sink, "we are ready for LATENCY query");
999 /* we are ready for a latency query this is when we preroll or when we are
1001 query = gst_query_new_latency ();
1003 /* ask the peer for the latency */
1004 if ((res = gst_base_sink_peer_query (sink, query))) {
1005 /* get upstream min and max latency */
1006 gst_query_parse_latency (query, &us_live, &us_min, &us_max);
1009 /* upstream live, use its latency, subclasses should use these
1010 * values to create the complete latency. */
1015 /* we need to add the render delay if we are live */
1017 min += render_delay;
1019 max += render_delay;
1022 gst_query_unref (query);
1024 GST_DEBUG_OBJECT (sink, "we are not yet ready for LATENCY query");
1028 /* not live, we tried to do the query, if it failed we return TRUE anyway */
1032 GST_DEBUG_OBJECT (sink, "latency query failed but we are not live");
1034 GST_DEBUG_OBJECT (sink, "latency query failed and we are live");
1039 GST_DEBUG_OBJECT (sink, "latency query: live: %d, have_latency %d,"
1040 " upstream: %d, min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT, l,
1041 have_latency, us_live, GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1046 *upstream_live = us_live;
1056 * gst_base_sink_set_render_delay:
1057 * @sink: a #GstBaseSink
1058 * @delay: the new delay
1060 * Set the render delay in @sink to @delay. The render delay is the time
1061 * between actual rendering of a buffer and its synchronisation time. Some
1062 * devices might delay media rendering which can be compensated for with this
1065 * After calling this function, this sink will report additional latency and
1066 * other sinks will adjust their latency to delay the rendering of their media.
1068 * This function is usually called by subclasses.
1073 gst_base_sink_set_render_delay (GstBaseSink * sink, GstClockTime delay)
1075 GstClockTime old_render_delay;
1077 g_return_if_fail (GST_IS_BASE_SINK (sink));
1079 GST_OBJECT_LOCK (sink);
1080 old_render_delay = sink->priv->render_delay;
1081 sink->priv->render_delay = delay;
1082 GST_LOG_OBJECT (sink, "set render delay to %" GST_TIME_FORMAT,
1083 GST_TIME_ARGS (delay));
1084 GST_OBJECT_UNLOCK (sink);
1086 if (delay != old_render_delay) {
1087 GST_DEBUG_OBJECT (sink, "posting latency changed");
1088 gst_element_post_message (GST_ELEMENT_CAST (sink),
1089 gst_message_new_latency (GST_OBJECT_CAST (sink)));
1094 * gst_base_sink_get_render_delay:
1095 * @sink: a #GstBaseSink
1097 * Get the render delay of @sink. see gst_base_sink_set_render_delay() for more
1098 * information about the render delay.
1100 * Returns: the render delay of @sink.
1105 gst_base_sink_get_render_delay (GstBaseSink * sink)
1107 GstClockTimeDiff res;
1109 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1111 GST_OBJECT_LOCK (sink);
1112 res = sink->priv->render_delay;
1113 GST_OBJECT_UNLOCK (sink);
1119 * gst_base_sink_set_blocksize:
1120 * @sink: a #GstBaseSink
1121 * @blocksize: the blocksize in bytes
1123 * Set the number of bytes that the sink will pull when it is operating in pull
1129 gst_base_sink_set_blocksize (GstBaseSink * sink, guint blocksize)
1131 g_return_if_fail (GST_IS_BASE_SINK (sink));
1133 GST_OBJECT_LOCK (sink);
1134 sink->priv->blocksize = blocksize;
1135 GST_LOG_OBJECT (sink, "set blocksize to %u", blocksize);
1136 GST_OBJECT_UNLOCK (sink);
1140 * gst_base_sink_get_blocksize:
1141 * @sink: a #GstBaseSink
1143 * Get the number of bytes that the sink will pull when it is operating in pull
1146 * Returns: the number of bytes @sink will pull in pull mode.
1151 gst_base_sink_get_blocksize (GstBaseSink * sink)
1155 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1157 GST_OBJECT_LOCK (sink);
1158 res = sink->priv->blocksize;
1159 GST_OBJECT_UNLOCK (sink);
1165 gst_base_sink_set_property (GObject * object, guint prop_id,
1166 const GValue * value, GParamSpec * pspec)
1168 GstBaseSink *sink = GST_BASE_SINK (object);
1171 case PROP_PREROLL_QUEUE_LEN:
1172 /* preroll lock necessary to serialize with finish_preroll */
1173 GST_PAD_PREROLL_LOCK (sink->sinkpad);
1174 sink->preroll_queue_max_len = g_value_get_uint (value);
1175 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
1178 gst_base_sink_set_sync (sink, g_value_get_boolean (value));
1180 case PROP_MAX_LATENESS:
1181 gst_base_sink_set_max_lateness (sink, g_value_get_int64 (value));
1184 gst_base_sink_set_qos_enabled (sink, g_value_get_boolean (value));
1187 gst_base_sink_set_async_enabled (sink, g_value_get_boolean (value));
1189 case PROP_TS_OFFSET:
1190 gst_base_sink_set_ts_offset (sink, g_value_get_int64 (value));
1192 case PROP_BLOCKSIZE:
1193 gst_base_sink_set_blocksize (sink, g_value_get_uint (value));
1195 case PROP_RENDER_DELAY:
1196 gst_base_sink_set_render_delay (sink, g_value_get_uint64 (value));
1199 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1205 gst_base_sink_get_property (GObject * object, guint prop_id, GValue * value,
1208 GstBaseSink *sink = GST_BASE_SINK (object);
1211 case PROP_PREROLL_QUEUE_LEN:
1212 GST_PAD_PREROLL_LOCK (sink->sinkpad);
1213 g_value_set_uint (value, sink->preroll_queue_max_len);
1214 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
1217 g_value_set_boolean (value, gst_base_sink_get_sync (sink));
1219 case PROP_MAX_LATENESS:
1220 g_value_set_int64 (value, gst_base_sink_get_max_lateness (sink));
1223 g_value_set_boolean (value, gst_base_sink_is_qos_enabled (sink));
1226 g_value_set_boolean (value, gst_base_sink_is_async_enabled (sink));
1228 case PROP_TS_OFFSET:
1229 g_value_set_int64 (value, gst_base_sink_get_ts_offset (sink));
1231 case PROP_LAST_BUFFER:
1232 gst_value_take_buffer (value, gst_base_sink_get_last_buffer (sink));
1234 case PROP_BLOCKSIZE:
1235 g_value_set_uint (value, gst_base_sink_get_blocksize (sink));
1237 case PROP_RENDER_DELAY:
1238 g_value_set_uint64 (value, gst_base_sink_get_render_delay (sink));
1241 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1248 gst_base_sink_get_caps (GstBaseSink * sink)
1254 gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps)
1259 static GstFlowReturn
1260 gst_base_sink_buffer_alloc (GstBaseSink * sink, guint64 offset, guint size,
1261 GstCaps * caps, GstBuffer ** buf)
1267 /* with PREROLL_LOCK, STREAM_LOCK */
1269 gst_base_sink_preroll_queue_flush (GstBaseSink * basesink, GstPad * pad)
1273 GST_DEBUG_OBJECT (basesink, "flushing queue %p", basesink);
1274 while ((obj = g_queue_pop_head (basesink->preroll_queue))) {
1275 GST_DEBUG_OBJECT (basesink, "popped %p", obj);
1276 gst_mini_object_unref (obj);
1278 /* we can't have EOS anymore now */
1279 basesink->eos = FALSE;
1280 basesink->priv->received_eos = FALSE;
1281 basesink->have_preroll = FALSE;
1282 basesink->priv->step_unlock = FALSE;
1283 basesink->eos_queued = FALSE;
1284 basesink->preroll_queued = 0;
1285 basesink->buffers_queued = 0;
1286 basesink->events_queued = 0;
1287 /* can't report latency anymore until we preroll again */
1288 if (basesink->priv->async_enabled) {
1289 GST_OBJECT_LOCK (basesink);
1290 basesink->priv->have_latency = FALSE;
1291 GST_OBJECT_UNLOCK (basesink);
1293 /* and signal any waiters now */
1294 GST_PAD_PREROLL_SIGNAL (pad);
1297 /* with STREAM_LOCK, configures given segment with the event information. */
1299 gst_base_sink_configure_segment (GstBaseSink * basesink, GstPad * pad,
1300 GstEvent * event, GstSegment * segment)
1303 gdouble rate, arate;
1309 /* the newsegment event is needed to bring the buffer timestamps to the
1310 * stream time and to drop samples outside of the playback segment. */
1311 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1312 &start, &stop, &time);
1314 /* The segment is protected with both the STREAM_LOCK and the OBJECT_LOCK.
1315 * We protect with the OBJECT_LOCK so that we can use the values to
1316 * safely answer a POSITION query. */
1317 GST_OBJECT_LOCK (basesink);
1318 gst_segment_set_newsegment_full (segment, update, rate, arate, format, start,
1321 if (format == GST_FORMAT_TIME) {
1322 GST_DEBUG_OBJECT (basesink,
1323 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1324 "format GST_FORMAT_TIME, "
1325 "%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
1326 ", time %" GST_TIME_FORMAT ", accum %" GST_TIME_FORMAT,
1327 update, rate, arate, GST_TIME_ARGS (segment->start),
1328 GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time),
1329 GST_TIME_ARGS (segment->accum));
1331 GST_DEBUG_OBJECT (basesink,
1332 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1334 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
1335 G_GINT64_FORMAT ", accum %" G_GINT64_FORMAT, update, rate, arate,
1336 segment->format, segment->start, segment->stop, segment->time,
1339 GST_OBJECT_UNLOCK (basesink);
1342 /* with PREROLL_LOCK, STREAM_LOCK */
1344 gst_base_sink_commit_state (GstBaseSink * basesink)
1346 /* commit state and proceed to next pending state */
1347 GstState current, next, pending, post_pending;
1348 gboolean post_paused = FALSE;
1349 gboolean post_async_done = FALSE;
1350 gboolean post_playing = FALSE;
1352 /* we are certainly not playing async anymore now */
1353 basesink->playing_async = FALSE;
1355 GST_OBJECT_LOCK (basesink);
1356 current = GST_STATE (basesink);
1357 next = GST_STATE_NEXT (basesink);
1358 pending = GST_STATE_PENDING (basesink);
1359 post_pending = pending;
1362 case GST_STATE_PLAYING:
1364 GstBaseSinkClass *bclass;
1365 GstStateChangeReturn ret;
1367 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1369 GST_DEBUG_OBJECT (basesink, "commiting state to PLAYING");
1371 basesink->need_preroll = FALSE;
1372 post_async_done = TRUE;
1373 basesink->priv->commited = TRUE;
1374 post_playing = TRUE;
1375 /* post PAUSED too when we were READY */
1376 if (current == GST_STATE_READY) {
1380 /* make sure we notify the subclass of async playing */
1381 if (bclass->async_play) {
1382 GST_WARNING_OBJECT (basesink, "deprecated async_play");
1383 ret = bclass->async_play (basesink);
1384 if (ret == GST_STATE_CHANGE_FAILURE)
1389 case GST_STATE_PAUSED:
1390 GST_DEBUG_OBJECT (basesink, "commiting state to PAUSED");
1392 post_async_done = TRUE;
1393 basesink->priv->commited = TRUE;
1394 post_pending = GST_STATE_VOID_PENDING;
1396 case GST_STATE_READY:
1397 case GST_STATE_NULL:
1399 case GST_STATE_VOID_PENDING:
1400 goto nothing_pending;
1405 /* we can report latency queries now */
1406 basesink->priv->have_latency = TRUE;
1408 GST_STATE (basesink) = pending;
1409 GST_STATE_NEXT (basesink) = GST_STATE_VOID_PENDING;
1410 GST_STATE_PENDING (basesink) = GST_STATE_VOID_PENDING;
1411 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_SUCCESS;
1412 GST_OBJECT_UNLOCK (basesink);
1415 GST_DEBUG_OBJECT (basesink, "posting PAUSED state change message");
1416 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1417 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1418 current, next, post_pending));
1420 if (post_async_done) {
1421 GST_DEBUG_OBJECT (basesink, "posting async-done message");
1422 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1423 gst_message_new_async_done (GST_OBJECT_CAST (basesink)));
1426 GST_DEBUG_OBJECT (basesink, "posting PLAYING state change message");
1427 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1428 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1429 next, pending, GST_STATE_VOID_PENDING));
1432 GST_STATE_BROADCAST (basesink);
1438 /* Depending on the state, set our vars. We get in this situation when the
1439 * state change function got a change to update the state vars before the
1440 * streaming thread did. This is fine but we need to make sure that we
1441 * update the need_preroll var since it was TRUE when we got here and might
1442 * become FALSE if we got to PLAYING. */
1443 GST_DEBUG_OBJECT (basesink, "nothing to commit, now in %s",
1444 gst_element_state_get_name (current));
1446 case GST_STATE_PLAYING:
1447 basesink->need_preroll = FALSE;
1449 case GST_STATE_PAUSED:
1450 basesink->need_preroll = TRUE;
1453 basesink->need_preroll = FALSE;
1454 basesink->flushing = TRUE;
1457 /* we can report latency queries now */
1458 basesink->priv->have_latency = TRUE;
1459 GST_OBJECT_UNLOCK (basesink);
1464 /* app is going to READY */
1465 GST_DEBUG_OBJECT (basesink, "stopping");
1466 basesink->need_preroll = FALSE;
1467 basesink->flushing = TRUE;
1468 GST_OBJECT_UNLOCK (basesink);
1473 GST_DEBUG_OBJECT (basesink, "async commit failed");
1474 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_FAILURE;
1475 GST_OBJECT_UNLOCK (basesink);
1481 start_stepping (GstBaseSink * sink, GstSegment * segment,
1482 GstStepInfo * pending, GstStepInfo * current)
1484 GST_DEBUG_OBJECT (sink, "update pending step");
1485 memcpy (current, pending, sizeof (GstStepInfo));
1486 pending->valid = FALSE;
1488 /* get the running time of where we paused and remember it */
1489 current->start = gst_element_get_start_time (GST_ELEMENT_CAST (sink));
1491 /* set the new rate */
1492 segment->rate = segment->rate * current->rate;
1494 GST_DEBUG_OBJECT (sink, "step started at running_time %" GST_TIME_FORMAT,
1495 GST_TIME_ARGS (current->start));
1497 if (current->amount == -1) {
1498 GST_DEBUG_OBJECT (sink, "step amount == -1, stop stepping");
1499 current->valid = FALSE;
1501 GST_DEBUG_OBJECT (sink, "step amount: %" G_GUINT64_FORMAT ", format: %s, "
1502 "rate: %f", current->amount, gst_format_get_name (current->format),
1508 stop_stepping (GstBaseSink * sink, GstSegment * segment,
1509 GstStepInfo * current, guint64 cstart, guint64 cstop, gint64 * rstart,
1512 GstMessage *message;
1514 GST_DEBUG_OBJECT (sink, "step complete");
1516 /* update the segment, discarding what was consumed, running time goes
1517 * backwards with the duration of the data we skipped. FIXME, this only works
1519 if (segment->rate > 0.0) {
1520 GST_DEBUG_OBJECT (sink,
1521 "step stop at running_time %" GST_TIME_FORMAT ", timestamp %"
1522 GST_TIME_FORMAT, GST_TIME_ARGS (*rstart), GST_TIME_ARGS (cstart));
1523 current->duration = *rstart - current->start;
1526 gst_segment_to_stream_time (segment, segment->format, cstart);
1527 segment->start = cstart;
1529 *rstart = current->start;
1530 *rstop -= current->duration;
1532 GST_DEBUG_OBJECT (sink,
1533 "step stop at running_time %" GST_TIME_FORMAT ", timestamp %"
1534 GST_TIME_FORMAT, GST_TIME_ARGS (*rstop), GST_TIME_ARGS (cstop));
1535 current->duration = *rstop - current->start;
1537 segment->stop = cstop;
1538 *rstop = current->start;
1539 *rstart -= current->duration;
1541 segment->accum = current->start;
1543 GST_DEBUG_OBJECT (sink, "step elapsed running_time %" GST_TIME_FORMAT,
1544 GST_TIME_ARGS (current->duration));
1546 /* the clip segment is used for position report in paused... */
1547 memcpy (sink->abidata.ABI.clip_segment, segment, sizeof (GstSegment));
1550 gst_message_new_step_done (GST_OBJECT_CAST (sink), current->format,
1551 current->amount, current->rate, current->flush, current->intermediate,
1553 gst_message_set_seqnum (message, current->seqnum);
1554 gst_element_post_message (GST_ELEMENT_CAST (sink), message);
1556 if (!current->intermediate)
1557 sink->need_preroll = current->need_preroll;
1559 /* and the current step info finished and becomes invalid */
1560 current->valid = FALSE;
1564 handle_stepping (GstBaseSink * sink, GstSegment * segment,
1565 GstStepInfo * current, gint64 * cstart, gint64 * cstop, gint64 * rstart,
1568 GstBaseSinkPrivate *priv;
1569 gboolean step_end = FALSE;
1573 /* see if we need to skip this buffer because of stepping */
1574 switch (current->format) {
1575 case GST_FORMAT_TIME:
1580 end = current->start + current->amount;
1582 if (segment->rate > 0.0) {
1583 current->position = *rstart - current->start;
1586 current->position = *rstop - current->start;
1590 GST_DEBUG_OBJECT (sink,
1591 "got time step %" GST_TIME_FORMAT "/%" GST_TIME_FORMAT,
1592 GST_TIME_ARGS (current->position), GST_TIME_ARGS (current->amount));
1594 if (current->position >= current->amount || last >= end) {
1596 if (segment->rate > 0.0) {
1597 *cstart += end - *rstart;
1600 *cstop += end - *rstop;
1606 case GST_FORMAT_BUFFERS:
1607 GST_DEBUG_OBJECT (sink,
1608 "got default step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1609 current->position, current->amount);
1611 if (current->position < current->amount) {
1612 current->position++;
1617 case GST_FORMAT_DEFAULT:
1619 GST_DEBUG_OBJECT (sink,
1620 "got unknown step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1621 current->position, current->amount);
1627 /* with STREAM_LOCK, PREROLL_LOCK
1629 * Returns TRUE if the object needs synchronisation and takes therefore
1630 * part in prerolling.
1632 * rsstart/rsstop contain the start/stop in stream time.
1633 * rrstart/rrstop contain the start/stop in running time.
1636 gst_base_sink_get_sync_times (GstBaseSink * basesink, GstMiniObject * obj,
1637 GstClockTime * rsstart, GstClockTime * rsstop,
1638 GstClockTime * rrstart, GstClockTime * rrstop, gboolean * do_sync,
1639 gboolean * stepped, GstSegment * segment, GstStepInfo * step)
1641 GstBaseSinkClass *bclass;
1643 GstClockTime start, stop; /* raw start/stop timestamps */
1644 gint64 cstart, cstop; /* clipped raw timestamps */
1645 gint64 rstart, rstop; /* clipped timestamps converted to running time */
1646 GstClockTime sstart, sstop; /* clipped timestamps converted to stream time */
1648 GstBaseSinkPrivate *priv;
1651 priv = basesink->priv;
1653 /* start with nothing */
1658 if (G_UNLIKELY (GST_IS_EVENT (obj))) {
1659 GstEvent *event = GST_EVENT_CAST (obj);
1661 switch (GST_EVENT_TYPE (event)) {
1662 /* EOS event needs syncing */
1665 if (basesink->segment.rate >= 0.0) {
1666 sstart = sstop = priv->current_sstop;
1668 /* we have not seen a buffer yet, use the segment values */
1669 sstart = sstop = gst_segment_to_stream_time (&basesink->segment,
1670 basesink->segment.format, basesink->segment.stop);
1673 sstart = sstop = priv->current_sstart;
1675 /* we have not seen a buffer yet, use the segment values */
1676 sstart = sstop = gst_segment_to_stream_time (&basesink->segment,
1677 basesink->segment.format, basesink->segment.start);
1681 rstart = rstop = priv->eos_rtime;
1682 *do_sync = rstart != -1;
1683 GST_DEBUG_OBJECT (basesink, "sync times for EOS %" GST_TIME_FORMAT,
1684 GST_TIME_ARGS (rstart));
1685 /* if we are stepping, we end now */
1686 step_end = step->valid;
1690 /* other events do not need syncing */
1691 /* FIXME, maybe NEWSEGMENT might need synchronisation
1692 * since the POSITION query depends on accumulated times and
1693 * we cannot accumulate the current segment before the previous
1700 /* else do buffer sync code */
1701 buffer = GST_BUFFER_CAST (obj);
1703 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1705 /* just get the times to see if we need syncing, if the start returns -1 we
1707 if (bclass->get_times)
1708 bclass->get_times (basesink, buffer, &start, &stop);
1711 /* we don't need to sync but we still want to get the timestamps for
1712 * tracking the position */
1713 gst_base_sink_get_times (basesink, buffer, &start, &stop);
1719 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
1720 ", stop: %" GST_TIME_FORMAT ", do_sync %d", GST_TIME_ARGS (start),
1721 GST_TIME_ARGS (stop), *do_sync);
1723 /* collect segment and format for code clarity */
1724 format = segment->format;
1726 /* no timestamp clipping if we did not get a TIME segment format */
1727 if (G_UNLIKELY (format != GST_FORMAT_TIME)) {
1730 /* do running and stream time in TIME format */
1731 format = GST_FORMAT_TIME;
1732 GST_LOG_OBJECT (basesink, "not time format, don't clip");
1736 /* clip, only when we know about time */
1737 if (G_UNLIKELY (!gst_segment_clip (segment, GST_FORMAT_TIME,
1738 (gint64) start, (gint64) stop, &cstart, &cstop)))
1739 goto out_of_segment;
1741 if (G_UNLIKELY (start != cstart || stop != cstop)) {
1742 GST_DEBUG_OBJECT (basesink, "clipped to: start %" GST_TIME_FORMAT
1743 ", stop: %" GST_TIME_FORMAT, GST_TIME_ARGS (cstart),
1744 GST_TIME_ARGS (cstop));
1747 /* set last stop position */
1748 if (G_LIKELY (cstop != GST_CLOCK_TIME_NONE))
1749 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, cstop);
1751 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, cstart);
1754 /* this can produce wrong values if we accumulated non-TIME segments. If this happens,
1755 * upstream is behaving very badly */
1756 sstart = gst_segment_to_stream_time (segment, format, cstart);
1757 sstop = gst_segment_to_stream_time (segment, format, cstop);
1758 rstart = gst_segment_to_running_time (segment, format, cstart);
1759 rstop = gst_segment_to_running_time (segment, format, cstop);
1761 if (G_UNLIKELY (step->valid)) {
1762 if (!(step_end = handle_stepping (basesink, segment, step, &cstart, &cstop,
1768 /* done label only called when doing EOS, we also stop stepping then */
1770 stop_stepping (basesink, segment, step, cstart, cstop, &rstart, &rstop);
1778 /* buffers and EOS always need syncing and preroll */
1784 /* should not happen since we clip them in the chain function already,
1785 * we return FALSE so that we don't try to sync on it. */
1786 GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
1787 (NULL), ("unexpected buffer out of segment found."));
1788 GST_LOG_OBJECT (basesink, "buffer skipped, not in segment");
1793 /* with STREAM_LOCK, PREROLL_LOCK, LOCK
1794 * adjust a timestamp with the latency and timestamp offset */
1796 gst_base_sink_adjust_time (GstBaseSink * basesink, GstClockTime time)
1798 GstClockTimeDiff ts_offset;
1800 /* don't do anything funny with invalid timestamps */
1801 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
1804 time += basesink->priv->latency;
1806 /* apply offset, be carefull for underflows */
1807 ts_offset = basesink->priv->ts_offset;
1808 if (ts_offset < 0) {
1809 ts_offset = -ts_offset;
1810 if (ts_offset < time)
1821 * gst_base_sink_wait_clock:
1823 * @time: the running_time to be reached
1824 * @jitter: the jitter to be filled with time diff (can be NULL)
1826 * This function will block until @time is reached. It is usually called by
1827 * subclasses that use their own internal synchronisation.
1829 * If @time is not valid, no sycnhronisation is done and #GST_CLOCK_BADTIME is
1830 * returned. Likewise, if synchronisation is disabled in the element or there
1831 * is no clock, no synchronisation is done and #GST_CLOCK_BADTIME is returned.
1833 * This function should only be called with the PREROLL_LOCK held, like when
1834 * receiving an EOS event in the ::event vmethod or when receiving a buffer in
1835 * the ::render vmethod.
1837 * The @time argument should be the running_time of when this method should
1838 * return and is not adjusted with any latency or offset configured in the
1843 * Returns: #GstClockReturn
1846 gst_base_sink_wait_clock (GstBaseSink * sink, GstClockTime time,
1847 GstClockTimeDiff * jitter)
1853 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
1856 GST_OBJECT_LOCK (sink);
1857 if (G_UNLIKELY (!sink->sync))
1860 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (sink)) == NULL))
1863 /* add base_time to running_time to get the time against the clock */
1864 time += GST_ELEMENT_CAST (sink)->base_time;
1866 id = gst_clock_new_single_shot_id (clock, time);
1867 GST_OBJECT_UNLOCK (sink);
1869 /* A blocking wait is performed on the clock. We save the ClockID
1870 * so we can unlock the entry at any time. While we are blocking, we
1871 * release the PREROLL_LOCK so that other threads can interrupt the
1873 sink->clock_id = id;
1874 /* release the preroll lock while waiting */
1875 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
1877 ret = gst_clock_id_wait (id, jitter);
1879 GST_PAD_PREROLL_LOCK (sink->sinkpad);
1880 gst_clock_id_unref (id);
1881 sink->clock_id = NULL;
1885 /* no syncing needed */
1888 GST_DEBUG_OBJECT (sink, "time not valid, no sync needed");
1889 return GST_CLOCK_BADTIME;
1893 GST_DEBUG_OBJECT (sink, "sync disabled");
1894 GST_OBJECT_UNLOCK (sink);
1895 return GST_CLOCK_BADTIME;
1899 GST_DEBUG_OBJECT (sink, "no clock, can't sync");
1900 GST_OBJECT_UNLOCK (sink);
1901 return GST_CLOCK_BADTIME;
1906 * gst_base_sink_wait_preroll:
1909 * If the #GstBaseSinkClass::render method performs its own synchronisation against
1910 * the clock it must unblock when going from PLAYING to the PAUSED state and call
1911 * this method before continuing to render the remaining data.
1913 * This function will block until a state change to PLAYING happens (in which
1914 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
1915 * to a state change to READY or a FLUSH event (in which case this function
1916 * returns #GST_FLOW_WRONG_STATE).
1918 * This function should only be called with the PREROLL_LOCK held, like in the
1923 * Returns: #GST_FLOW_OK if the preroll completed and processing can
1924 * continue. Any other return value should be returned from the render vmethod.
1927 gst_base_sink_wait_preroll (GstBaseSink * sink)
1929 sink->have_preroll = TRUE;
1930 GST_DEBUG_OBJECT (sink, "waiting in preroll for flush or PLAYING");
1931 /* block until the state changes, or we get a flush, or something */
1932 GST_PAD_PREROLL_WAIT (sink->sinkpad);
1933 sink->have_preroll = FALSE;
1934 if (G_UNLIKELY (sink->flushing))
1936 if (G_UNLIKELY (sink->priv->step_unlock))
1938 GST_DEBUG_OBJECT (sink, "continue after preroll");
1945 GST_DEBUG_OBJECT (sink, "preroll interrupted because of flush");
1946 return GST_FLOW_WRONG_STATE;
1950 sink->priv->step_unlock = FALSE;
1951 GST_DEBUG_OBJECT (sink, "preroll interrupted because of step");
1952 return GST_FLOW_STEP;
1957 * gst_base_sink_do_preroll:
1959 * @obj: the object that caused the preroll
1961 * If the @sink spawns its own thread for pulling buffers from upstream it
1962 * should call this method after it has pulled a buffer. If the element needed
1963 * to preroll, this function will perform the preroll and will then block
1964 * until the element state is changed.
1966 * This function should be called with the PREROLL_LOCK held.
1970 * Returns: #GST_FLOW_OK if the preroll completed and processing can
1971 * continue. Any other return value should be returned from the render vmethod.
1974 gst_base_sink_do_preroll (GstBaseSink * sink, GstMiniObject * obj)
1978 while (G_UNLIKELY (sink->need_preroll)) {
1979 GST_DEBUG_OBJECT (sink, "prerolling object %p", obj);
1981 ret = gst_base_sink_preroll_object (sink, obj);
1982 if (ret != GST_FLOW_OK)
1983 goto preroll_failed;
1985 /* need to recheck here because the commit state could have
1986 * made us not need the preroll anymore */
1987 if (G_LIKELY (sink->need_preroll)) {
1988 /* block until the state changes, or we get a flush, or something */
1989 ret = gst_base_sink_wait_preroll (sink);
1990 if (ret != GST_FLOW_OK) {
1991 if (ret == GST_FLOW_STEP)
1994 goto preroll_failed;
2003 GST_DEBUG_OBJECT (sink, "preroll failed %d", ret);
2009 * gst_base_sink_wait_eos:
2011 * @time: the running_time to be reached
2012 * @jitter: the jitter to be filled with time diff (can be NULL)
2014 * This function will block until @time is reached. It is usually called by
2015 * subclasses that use their own internal synchronisation but want to let the
2016 * EOS be handled by the base class.
2018 * This function should only be called with the PREROLL_LOCK held, like when
2019 * receiving an EOS event in the ::event vmethod.
2021 * The @time argument should be the running_time of when the EOS should happen
2022 * and will be adjusted with any latency and offset configured in the sink.
2026 * Returns: #GstFlowReturn
2029 gst_base_sink_wait_eos (GstBaseSink * sink, GstClockTime time,
2030 GstClockTimeDiff * jitter)
2032 GstClockReturn status;
2038 GST_DEBUG_OBJECT (sink, "checking preroll");
2040 /* first wait for the playing state before we can continue */
2041 if (G_UNLIKELY (sink->need_preroll)) {
2042 ret = gst_base_sink_wait_preroll (sink);
2043 if (ret != GST_FLOW_OK) {
2044 if (ret == GST_FLOW_STEP)
2051 /* preroll done, we can sync since we are in PLAYING now. */
2052 GST_DEBUG_OBJECT (sink, "possibly waiting for clock to reach %"
2053 GST_TIME_FORMAT, GST_TIME_ARGS (time));
2055 /* compensate for latency and ts_offset. We don't adjust for render delay
2056 * because we don't interact with the device on EOS normally. */
2057 stime = gst_base_sink_adjust_time (sink, time);
2059 /* wait for the clock, this can be interrupted because we got shut down or
2061 status = gst_base_sink_wait_clock (sink, stime, jitter);
2063 GST_DEBUG_OBJECT (sink, "clock returned %d", status);
2065 /* invalid time, no clock or sync disabled, just continue then */
2066 if (status == GST_CLOCK_BADTIME)
2069 /* waiting could have been interrupted and we can be flushing now */
2070 if (G_UNLIKELY (sink->flushing))
2073 /* retry if we got unscheduled, which means we did not reach the timeout
2074 * yet. if some other error occures, we continue. */
2075 } while (status == GST_CLOCK_UNSCHEDULED);
2077 GST_DEBUG_OBJECT (sink, "end of stream");
2084 GST_DEBUG_OBJECT (sink, "we are flushing");
2085 return GST_FLOW_WRONG_STATE;
2089 /* with STREAM_LOCK, PREROLL_LOCK
2091 * Make sure we are in PLAYING and synchronize an object to the clock.
2093 * If we need preroll, we are not in PLAYING. We try to commit the state
2094 * if needed and then block if we still are not PLAYING.
2096 * We start waiting on the clock in PLAYING. If we got interrupted, we
2097 * immediatly try to re-preroll.
2099 * Some objects do not need synchronisation (most events) and so this function
2100 * immediatly returns GST_FLOW_OK.
2102 * for objects that arrive later than max-lateness to be synchronized to the
2103 * clock have the @late boolean set to TRUE.
2105 * This function keeps a running average of the jitter (the diff between the
2106 * clock time and the requested sync time). The jitter is negative for
2107 * objects that arrive in time and positive for late buffers.
2109 * does not take ownership of obj.
2111 static GstFlowReturn
2112 gst_base_sink_do_sync (GstBaseSink * basesink, GstPad * pad,
2113 GstMiniObject * obj, gboolean * late)
2115 GstClockTimeDiff jitter;
2117 GstClockReturn status = GST_CLOCK_OK;
2118 GstClockTime rstart, rstop, sstart, sstop, stime;
2120 GstBaseSinkPrivate *priv;
2122 GstStepInfo *current, *pending;
2125 priv = basesink->priv;
2128 sstart = sstop = rstart = rstop = -1;
2132 priv->current_rstart = -1;
2134 /* get stepping info */
2135 current = &priv->current_step;
2136 pending = &priv->pending_step;
2138 /* get timing information for this object against the render segment */
2139 syncable = gst_base_sink_get_sync_times (basesink, obj,
2140 &sstart, &sstop, &rstart, &rstop, &do_sync, &stepped, &basesink->segment,
2143 if (G_UNLIKELY (stepped))
2146 /* a syncable object needs to participate in preroll and
2147 * clocking. All buffers and EOS are syncable. */
2148 if (G_UNLIKELY (!syncable))
2151 /* store timing info for current object */
2152 priv->current_rstart = rstart;
2153 priv->current_rstop = (rstop != -1 ? rstop : rstart);
2155 /* save sync time for eos when the previous object needed sync */
2156 priv->eos_rtime = (do_sync ? priv->current_rstop : -1);
2159 /* first do preroll, this makes sure we commit our state
2160 * to PAUSED and can continue to PLAYING. We cannot perform
2161 * any clock sync in PAUSED because there is no clock. */
2162 ret = gst_base_sink_do_preroll (basesink, obj);
2163 if (G_UNLIKELY (ret != GST_FLOW_OK))
2164 goto preroll_failed;
2166 /* update the segment with a pending step if the current one is invalid and we
2167 * have a new pending one. We only accept new step updates after a preroll */
2168 if (G_UNLIKELY (pending->valid && !current->valid)) {
2169 start_stepping (basesink, &basesink->segment, pending, current);
2173 /* After rendering we store the position of the last buffer so that we can use
2174 * it to report the position. We need to take the lock here. */
2175 GST_OBJECT_LOCK (basesink);
2176 priv->current_sstart = sstart;
2177 priv->current_sstop = (sstop != -1 ? sstop : sstart);
2178 GST_OBJECT_UNLOCK (basesink);
2183 /* adjust for latency */
2184 stime = gst_base_sink_adjust_time (basesink, rstart);
2186 /* adjust for render-delay, avoid underflows */
2188 if (stime > priv->render_delay)
2189 stime -= priv->render_delay;
2194 /* preroll done, we can sync since we are in PLAYING now. */
2195 GST_DEBUG_OBJECT (basesink, "possibly waiting for clock to reach %"
2196 GST_TIME_FORMAT ", adjusted %" GST_TIME_FORMAT,
2197 GST_TIME_ARGS (rstart), GST_TIME_ARGS (stime));
2199 /* This function will return immediatly if start == -1, no clock
2200 * or sync is disabled with GST_CLOCK_BADTIME. */
2201 status = gst_base_sink_wait_clock (basesink, stime, &jitter);
2203 GST_DEBUG_OBJECT (basesink, "clock returned %d", status);
2205 /* invalid time, no clock or sync disabled, just render */
2206 if (status == GST_CLOCK_BADTIME)
2209 /* waiting could have been interrupted and we can be flushing now */
2210 if (G_UNLIKELY (basesink->flushing))
2213 /* check for unlocked by a state change, we are not flushing so
2214 * we can try to preroll on the current buffer. */
2215 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
2216 GST_DEBUG_OBJECT (basesink, "unscheduled, waiting some more");
2217 priv->call_preroll = TRUE;
2221 /* successful syncing done, record observation */
2222 priv->current_jitter = jitter;
2224 /* check if the object should be dropped */
2225 *late = gst_base_sink_is_too_late (basesink, obj, rstart, rstop,
2234 GST_DEBUG_OBJECT (basesink, "skipped stepped object %p", obj);
2240 GST_DEBUG_OBJECT (basesink, "non syncable object %p", obj);
2245 GST_DEBUG_OBJECT (basesink, "we are flushing");
2246 return GST_FLOW_WRONG_STATE;
2250 GST_DEBUG_OBJECT (basesink, "preroll failed");
2256 gst_base_sink_send_qos (GstBaseSink * basesink,
2257 gdouble proportion, GstClockTime time, GstClockTimeDiff diff)
2262 /* generate Quality-of-Service event */
2263 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2264 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2265 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (time));
2267 event = gst_event_new_qos (proportion, diff, time);
2270 res = gst_pad_push_event (basesink->sinkpad, event);
2276 gst_base_sink_perform_qos (GstBaseSink * sink, gboolean dropped)
2278 GstBaseSinkPrivate *priv;
2279 GstClockTime start, stop;
2280 GstClockTimeDiff jitter;
2281 GstClockTime pt, entered, left;
2282 GstClockTime duration;
2287 start = priv->current_rstart;
2289 /* if Quality-of-Service disabled, do nothing */
2290 if (!g_atomic_int_get (&priv->qos_enabled) || start == -1)
2293 stop = priv->current_rstop;
2294 jitter = priv->current_jitter;
2297 /* this is the time the buffer entered the sink */
2298 if (start < -jitter)
2301 entered = start + jitter;
2304 /* this is the time the buffer entered the sink */
2305 entered = start + jitter;
2306 /* this is the time the buffer left the sink */
2307 left = start + jitter;
2310 /* calculate duration of the buffer */
2312 duration = stop - start;
2316 /* if we have the time when the last buffer left us, calculate
2317 * processing time */
2318 if (priv->last_left != -1) {
2319 if (entered > priv->last_left) {
2320 pt = entered - priv->last_left;
2328 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "start: %" GST_TIME_FORMAT
2329 ", entered %" GST_TIME_FORMAT ", left %" GST_TIME_FORMAT ", pt: %"
2330 GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT ",jitter %"
2331 G_GINT64_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (entered),
2332 GST_TIME_ARGS (left), GST_TIME_ARGS (pt), GST_TIME_ARGS (duration),
2335 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "avg_duration: %" GST_TIME_FORMAT
2336 ", avg_pt: %" GST_TIME_FORMAT ", avg_rate: %g",
2337 GST_TIME_ARGS (priv->avg_duration), GST_TIME_ARGS (priv->avg_pt),
2340 /* collect running averages. for first observations, we copy the
2342 if (priv->avg_duration == -1)
2343 priv->avg_duration = duration;
2345 priv->avg_duration = UPDATE_RUNNING_AVG (priv->avg_duration, duration);
2347 if (priv->avg_pt == -1)
2350 priv->avg_pt = UPDATE_RUNNING_AVG (priv->avg_pt, pt);
2352 if (priv->avg_duration != 0)
2354 gst_guint64_to_gdouble (priv->avg_pt) /
2355 gst_guint64_to_gdouble (priv->avg_duration);
2359 if (priv->last_left != -1) {
2360 if (dropped || priv->avg_rate < 0.0) {
2361 priv->avg_rate = rate;
2364 priv->avg_rate = UPDATE_RUNNING_AVG_N (priv->avg_rate, rate);
2366 priv->avg_rate = UPDATE_RUNNING_AVG_P (priv->avg_rate, rate);
2370 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink,
2371 "updated: avg_duration: %" GST_TIME_FORMAT ", avg_pt: %" GST_TIME_FORMAT
2372 ", avg_rate: %g", GST_TIME_ARGS (priv->avg_duration),
2373 GST_TIME_ARGS (priv->avg_pt), priv->avg_rate);
2376 if (priv->avg_rate >= 0.0) {
2377 /* if we have a valid rate, start sending QoS messages */
2378 if (priv->current_jitter < 0) {
2379 /* make sure we never go below 0 when adding the jitter to the
2381 if (priv->current_rstart < -priv->current_jitter)
2382 priv->current_jitter = -priv->current_rstart;
2384 gst_base_sink_send_qos (sink, priv->avg_rate, priv->current_rstart,
2385 priv->current_jitter);
2388 /* record when this buffer will leave us */
2389 priv->last_left = left;
2392 /* reset all qos measuring */
2394 gst_base_sink_reset_qos (GstBaseSink * sink)
2396 GstBaseSinkPrivate *priv;
2400 priv->last_in_time = -1;
2401 priv->last_left = -1;
2402 priv->avg_duration = -1;
2404 priv->avg_rate = -1.0;
2405 priv->avg_render = -1;
2411 /* Checks if the object was scheduled too late.
2413 * start/stop contain the raw timestamp start and stop values
2416 * status and jitter contain the return values from the clock wait.
2418 * returns TRUE if the buffer was too late.
2421 gst_base_sink_is_too_late (GstBaseSink * basesink, GstMiniObject * obj,
2422 GstClockTime start, GstClockTime stop,
2423 GstClockReturn status, GstClockTimeDiff jitter)
2426 gint64 max_lateness;
2427 GstBaseSinkPrivate *priv;
2429 priv = basesink->priv;
2433 /* only for objects that were too late */
2434 if (G_LIKELY (status != GST_CLOCK_EARLY))
2437 max_lateness = basesink->abidata.ABI.max_lateness;
2439 /* check if frame dropping is enabled */
2440 if (max_lateness == -1)
2443 /* only check for buffers */
2444 if (G_UNLIKELY (!GST_IS_BUFFER (obj)))
2447 /* can't do check if we don't have a timestamp */
2448 if (G_UNLIKELY (start == -1))
2451 /* we can add a valid stop time */
2453 max_lateness += stop;
2455 max_lateness += start;
2457 /* if the jitter bigger than duration and lateness we are too late */
2458 if ((late = start + jitter > max_lateness)) {
2459 GST_DEBUG_OBJECT (basesink, "buffer is too late %" GST_TIME_FORMAT
2460 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (start + jitter),
2461 GST_TIME_ARGS (max_lateness));
2462 /* !!emergency!!, if we did not receive anything valid for more than a
2463 * second, render it anyway so the user sees something */
2464 if (priv->last_in_time != -1 && start - priv->last_in_time > GST_SECOND) {
2466 GST_DEBUG_OBJECT (basesink,
2467 "**emergency** last buffer at %" GST_TIME_FORMAT " > GST_SECOND",
2468 GST_TIME_ARGS (priv->last_in_time));
2474 priv->last_in_time = start;
2481 GST_DEBUG_OBJECT (basesink, "object was scheduled in time");
2486 GST_DEBUG_OBJECT (basesink, "frame dropping disabled");
2491 GST_DEBUG_OBJECT (basesink, "object is not a buffer");
2496 GST_DEBUG_OBJECT (basesink, "buffer has no timestamp");
2501 /* called before and after calling the render vmethod. It keeps track of how
2502 * much time was spent in the render method and is used to check if we are
2505 gst_base_sink_do_render_stats (GstBaseSink * basesink, gboolean start)
2507 GstBaseSinkPrivate *priv;
2509 priv = basesink->priv;
2512 priv->start = gst_util_get_timestamp ();
2514 GstClockTime elapsed;
2516 priv->stop = gst_util_get_timestamp ();
2518 elapsed = GST_CLOCK_DIFF (priv->start, priv->stop);
2520 if (priv->avg_render == -1)
2521 priv->avg_render = elapsed;
2523 priv->avg_render = UPDATE_RUNNING_AVG (priv->avg_render, elapsed);
2525 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2526 "avg_render: %" GST_TIME_FORMAT, GST_TIME_ARGS (priv->avg_render));
2530 /* with STREAM_LOCK, PREROLL_LOCK,
2532 * Synchronize the object on the clock and then render it.
2534 * takes ownership of obj.
2536 static GstFlowReturn
2537 gst_base_sink_render_object (GstBaseSink * basesink, GstPad * pad,
2538 GstMiniObject * obj)
2541 GstBaseSinkClass *bclass;
2544 GstBaseSinkPrivate *priv;
2546 priv = basesink->priv;
2552 /* synchronize this object, non syncable objects return OK
2554 ret = gst_base_sink_do_sync (basesink, pad, obj, &late);
2555 if (G_UNLIKELY (ret != GST_FLOW_OK))
2558 /* and now render, event or buffer. */
2559 if (G_LIKELY (GST_IS_BUFFER (obj))) {
2562 /* drop late buffers unconditionally, let's hope it's unlikely */
2563 if (G_UNLIKELY (late))
2566 buf = GST_BUFFER_CAST (obj);
2568 gst_base_sink_set_last_buffer (basesink, buf);
2570 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2572 if (G_LIKELY (bclass->render)) {
2575 /* read once, to get same value before and after */
2576 do_qos = g_atomic_int_get (&priv->qos_enabled);
2578 GST_DEBUG_OBJECT (basesink, "rendering buffer %p", obj);
2580 /* record rendering time for QoS and stats */
2582 gst_base_sink_do_render_stats (basesink, TRUE);
2584 ret = bclass->render (basesink, buf);
2587 gst_base_sink_do_render_stats (basesink, FALSE);
2589 if (ret == GST_FLOW_STEP)
2595 GstEvent *event = GST_EVENT_CAST (obj);
2596 gboolean event_res = TRUE;
2599 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2601 type = GST_EVENT_TYPE (event);
2603 GST_DEBUG_OBJECT (basesink, "rendering event %p, type %s", obj,
2604 gst_event_type_get_name (type));
2607 event_res = bclass->event (basesink, event);
2609 /* when we get here we could be flushing again when the event handler calls
2610 * _wait_eos(). We have to ignore this object in that case. */
2611 if (G_UNLIKELY (basesink->flushing))
2614 if (G_LIKELY (event_res)) {
2617 seqnum = basesink->priv->seqnum = gst_event_get_seqnum (event);
2618 GST_DEBUG_OBJECT (basesink, "Got seqnum #%" G_GUINT32_FORMAT, seqnum);
2623 GstMessage *message;
2625 /* the EOS event is completely handled so we mark
2626 * ourselves as being in the EOS state. eos is also
2627 * protected by the object lock so we can read it when
2628 * answering the POSITION query. */
2629 GST_OBJECT_LOCK (basesink);
2630 basesink->eos = TRUE;
2631 GST_OBJECT_UNLOCK (basesink);
2633 /* ok, now we can post the message */
2634 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
2636 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
2637 gst_message_set_seqnum (message, seqnum);
2638 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
2641 case GST_EVENT_NEWSEGMENT:
2642 /* configure the segment */
2643 gst_base_sink_configure_segment (basesink, pad, event,
2644 &basesink->segment);
2653 gst_base_sink_perform_qos (basesink, late);
2655 GST_DEBUG_OBJECT (basesink, "object unref after render %p", obj);
2656 gst_mini_object_unref (obj);
2663 GST_DEBUG_OBJECT (basesink, "do_sync returned %s", gst_flow_get_name (ret));
2669 GST_DEBUG_OBJECT (basesink, "buffer late, dropping");
2674 GST_DEBUG_OBJECT (basesink, "we are flushing, ignore object");
2675 gst_mini_object_unref (obj);
2676 return GST_FLOW_WRONG_STATE;
2680 /* with STREAM_LOCK, PREROLL_LOCK
2682 * Perform preroll on the given object. For buffers this means
2683 * calling the preroll subclass method.
2684 * If that succeeds, the state will be commited.
2686 * function does not take ownership of obj.
2688 static GstFlowReturn
2689 gst_base_sink_preroll_object (GstBaseSink * basesink, GstMiniObject * obj)
2693 GST_DEBUG_OBJECT (basesink, "prerolling object %p", obj);
2695 /* if it's a buffer, we need to call the preroll method */
2696 if (G_LIKELY (GST_IS_BUFFER (obj)) && basesink->priv->call_preroll) {
2697 GstBaseSinkClass *bclass;
2699 GstClockTime timestamp;
2701 buf = GST_BUFFER_CAST (obj);
2702 timestamp = GST_BUFFER_TIMESTAMP (buf);
2704 GST_DEBUG_OBJECT (basesink, "preroll buffer %" GST_TIME_FORMAT,
2705 GST_TIME_ARGS (timestamp));
2707 gst_base_sink_set_last_buffer (basesink, buf);
2709 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2710 if (bclass->preroll)
2711 if ((ret = bclass->preroll (basesink, buf)) != GST_FLOW_OK)
2712 goto preroll_failed;
2714 basesink->priv->call_preroll = FALSE;
2718 if (G_LIKELY (basesink->playing_async)) {
2719 if (G_UNLIKELY (!gst_base_sink_commit_state (basesink)))
2728 GST_DEBUG_OBJECT (basesink, "preroll failed, abort state");
2729 gst_element_abort_state (GST_ELEMENT_CAST (basesink));
2734 GST_DEBUG_OBJECT (basesink, "stopping while commiting state");
2735 return GST_FLOW_WRONG_STATE;
2739 /* with STREAM_LOCK, PREROLL_LOCK
2741 * Queue an object for rendering.
2742 * The first prerollable object queued will complete the preroll. If the
2743 * preroll queue if filled, we render all the objects in the queue.
2745 * This function takes ownership of the object.
2747 static GstFlowReturn
2748 gst_base_sink_queue_object_unlocked (GstBaseSink * basesink, GstPad * pad,
2749 GstMiniObject * obj, gboolean prerollable)
2751 GstFlowReturn ret = GST_FLOW_OK;
2755 if (G_UNLIKELY (basesink->need_preroll)) {
2756 if (G_LIKELY (prerollable))
2757 basesink->preroll_queued++;
2759 length = basesink->preroll_queued;
2761 GST_DEBUG_OBJECT (basesink, "now %d prerolled items", length);
2763 /* first prerollable item needs to finish the preroll */
2765 ret = gst_base_sink_preroll_object (basesink, obj);
2766 if (G_UNLIKELY (ret != GST_FLOW_OK))
2767 goto preroll_failed;
2769 /* need to recheck if we need preroll, commmit state during preroll
2770 * could have made us not need more preroll. */
2771 if (G_UNLIKELY (basesink->need_preroll)) {
2772 /* see if we can render now, if we can't add the object to the preroll
2774 if (G_UNLIKELY (length <= basesink->preroll_queue_max_len))
2779 /* we can start rendering (or blocking) the queued object
2781 q = basesink->preroll_queue;
2782 while (G_UNLIKELY (!g_queue_is_empty (q))) {
2785 o = g_queue_pop_head (q);
2786 GST_DEBUG_OBJECT (basesink, "rendering queued object %p", o);
2788 /* do something with the return value */
2789 ret = gst_base_sink_render_object (basesink, pad, o);
2790 if (ret != GST_FLOW_OK)
2791 goto dequeue_failed;
2794 /* now render the object */
2795 ret = gst_base_sink_render_object (basesink, pad, obj);
2796 basesink->preroll_queued = 0;
2803 GST_DEBUG_OBJECT (basesink, "preroll failed, reason %s",
2804 gst_flow_get_name (ret));
2805 gst_mini_object_unref (obj);
2810 /* add object to the queue and return */
2811 GST_DEBUG_OBJECT (basesink, "need more preroll data %d <= %d",
2812 length, basesink->preroll_queue_max_len);
2813 g_queue_push_tail (basesink->preroll_queue, obj);
2818 GST_DEBUG_OBJECT (basesink, "rendering queued objects failed, reason %s",
2819 gst_flow_get_name (ret));
2820 gst_mini_object_unref (obj);
2827 * This function grabs the PREROLL_LOCK and adds the object to
2830 * This function takes ownership of obj.
2832 static GstFlowReturn
2833 gst_base_sink_queue_object (GstBaseSink * basesink, GstPad * pad,
2834 GstMiniObject * obj, gboolean prerollable)
2838 GST_PAD_PREROLL_LOCK (pad);
2839 if (G_UNLIKELY (basesink->flushing))
2842 if (G_UNLIKELY (basesink->priv->received_eos))
2845 ret = gst_base_sink_queue_object_unlocked (basesink, pad, obj, prerollable);
2846 GST_PAD_PREROLL_UNLOCK (pad);
2853 GST_DEBUG_OBJECT (basesink, "sink is flushing");
2854 GST_PAD_PREROLL_UNLOCK (pad);
2855 gst_mini_object_unref (obj);
2856 return GST_FLOW_WRONG_STATE;
2860 GST_DEBUG_OBJECT (basesink,
2861 "we are EOS, dropping object, return UNEXPECTED");
2862 GST_PAD_PREROLL_UNLOCK (pad);
2863 gst_mini_object_unref (obj);
2864 return GST_FLOW_UNEXPECTED;
2869 gst_base_sink_flush_start (GstBaseSink * basesink, GstPad * pad)
2871 /* make sure we are not blocked on the clock also clear any pending
2873 gst_base_sink_set_flushing (basesink, pad, TRUE);
2875 /* we grab the stream lock but that is not needed since setting the
2876 * sink to flushing would make sure no state commit is being done
2878 GST_PAD_STREAM_LOCK (pad);
2879 gst_base_sink_reset_qos (basesink);
2880 if (basesink->priv->async_enabled) {
2881 /* and we need to commit our state again on the next
2882 * prerolled buffer */
2883 basesink->playing_async = TRUE;
2884 gst_element_lost_state (GST_ELEMENT_CAST (basesink));
2886 basesink->priv->have_latency = TRUE;
2887 basesink->need_preroll = FALSE;
2889 gst_base_sink_set_last_buffer (basesink, NULL);
2890 GST_PAD_STREAM_UNLOCK (pad);
2894 gst_base_sink_flush_stop (GstBaseSink * basesink, GstPad * pad)
2896 /* unset flushing so we can accept new data, this also flushes out any EOS
2898 gst_base_sink_set_flushing (basesink, pad, FALSE);
2900 /* for position reporting */
2901 GST_OBJECT_LOCK (basesink);
2902 basesink->priv->current_sstart = -1;
2903 basesink->priv->current_sstop = -1;
2904 basesink->priv->eos_rtime = -1;
2905 basesink->priv->call_preroll = TRUE;
2906 if (basesink->pad_mode == GST_ACTIVATE_PUSH) {
2907 /* we need new segment info after the flush. */
2908 basesink->have_newsegment = FALSE;
2909 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
2910 gst_segment_init (basesink->abidata.ABI.clip_segment, GST_FORMAT_UNDEFINED);
2912 GST_OBJECT_UNLOCK (basesink);
2916 gst_base_sink_event (GstPad * pad, GstEvent * event)
2918 GstBaseSink *basesink;
2919 gboolean result = TRUE;
2920 GstBaseSinkClass *bclass;
2922 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
2924 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2926 GST_DEBUG_OBJECT (basesink, "event %p (%s)", event,
2927 GST_EVENT_TYPE_NAME (event));
2929 switch (GST_EVENT_TYPE (event)) {
2934 GST_PAD_PREROLL_LOCK (pad);
2935 if (G_UNLIKELY (basesink->flushing))
2938 if (G_UNLIKELY (basesink->priv->received_eos)) {
2939 /* we can't accept anything when we are EOS */
2941 gst_event_unref (event);
2943 /* we set the received EOS flag here so that we can use it when testing if
2944 * we are prerolled and to refuse more buffers. */
2945 basesink->priv->received_eos = TRUE;
2947 /* EOS is a prerollable object, we call the unlocked version because it
2948 * does not check the received_eos flag. */
2949 ret = gst_base_sink_queue_object_unlocked (basesink, pad,
2950 GST_MINI_OBJECT_CAST (event), TRUE);
2951 if (G_UNLIKELY (ret != GST_FLOW_OK))
2954 GST_PAD_PREROLL_UNLOCK (pad);
2957 case GST_EVENT_NEWSEGMENT:
2961 GST_DEBUG_OBJECT (basesink, "newsegment %p", event);
2963 GST_PAD_PREROLL_LOCK (pad);
2964 if (G_UNLIKELY (basesink->flushing))
2967 if (G_UNLIKELY (basesink->priv->received_eos)) {
2968 /* we can't accept anything when we are EOS */
2970 gst_event_unref (event);
2972 /* the new segment is a non prerollable item and does not block anything,
2973 * we need to configure the current clipping segment and insert the event
2974 * in the queue to serialize it with the buffers for rendering. */
2975 gst_base_sink_configure_segment (basesink, pad, event,
2976 basesink->abidata.ABI.clip_segment);
2979 gst_base_sink_queue_object_unlocked (basesink, pad,
2980 GST_MINI_OBJECT_CAST (event), FALSE);
2981 if (G_UNLIKELY (ret != GST_FLOW_OK))
2984 GST_OBJECT_LOCK (basesink);
2985 basesink->have_newsegment = TRUE;
2986 GST_OBJECT_UNLOCK (basesink);
2989 GST_PAD_PREROLL_UNLOCK (pad);
2992 case GST_EVENT_FLUSH_START:
2994 bclass->event (basesink, event);
2996 GST_DEBUG_OBJECT (basesink, "flush-start %p", event);
2998 gst_base_sink_flush_start (basesink, pad);
3000 gst_event_unref (event);
3002 case GST_EVENT_FLUSH_STOP:
3004 bclass->event (basesink, event);
3006 GST_DEBUG_OBJECT (basesink, "flush-stop %p", event);
3008 gst_base_sink_flush_stop (basesink, pad);
3010 gst_event_unref (event);
3013 /* other events are sent to queue or subclass depending on if they
3014 * are serialized. */
3015 if (GST_EVENT_IS_SERIALIZED (event)) {
3016 gst_base_sink_queue_object (basesink, pad,
3017 GST_MINI_OBJECT_CAST (event), FALSE);
3020 bclass->event (basesink, event);
3021 gst_event_unref (event);
3026 gst_object_unref (basesink);
3033 GST_DEBUG_OBJECT (basesink, "we are flushing");
3034 GST_PAD_PREROLL_UNLOCK (pad);
3036 gst_event_unref (event);
3041 /* default implementation to calculate the start and end
3042 * timestamps on a buffer, subclasses can override
3045 gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
3046 GstClockTime * start, GstClockTime * end)
3048 GstClockTime timestamp, duration;
3050 timestamp = GST_BUFFER_TIMESTAMP (buffer);
3051 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
3053 /* get duration to calculate end time */
3054 duration = GST_BUFFER_DURATION (buffer);
3055 if (GST_CLOCK_TIME_IS_VALID (duration)) {
3056 *end = timestamp + duration;
3062 /* must be called with PREROLL_LOCK */
3064 gst_base_sink_needs_preroll (GstBaseSink * basesink)
3066 gboolean is_prerolled, res;
3068 /* we have 2 cases where the PREROLL_LOCK is released:
3069 * 1) we are blocking in the PREROLL_LOCK and thus are prerolled.
3070 * 2) we are syncing on the clock
3072 is_prerolled = basesink->have_preroll || basesink->priv->received_eos;
3073 res = !is_prerolled;
3075 GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d => needs preroll: %d",
3076 basesink->have_preroll, basesink->priv->received_eos, res);
3081 /* with STREAM_LOCK, PREROLL_LOCK
3083 * Takes a buffer and compare the timestamps with the last segment.
3084 * If the buffer falls outside of the segment boundaries, drop it.
3085 * Else queue the buffer for preroll and rendering.
3087 * This function takes ownership of the buffer.
3089 static GstFlowReturn
3090 gst_base_sink_chain_unlocked (GstBaseSink * basesink, GstPad * pad,
3093 GstBaseSinkClass *bclass;
3094 GstFlowReturn result;
3095 GstClockTime start = GST_CLOCK_TIME_NONE, end = GST_CLOCK_TIME_NONE;
3096 GstSegment *clip_segment;
3098 if (G_UNLIKELY (basesink->flushing))
3101 if (G_UNLIKELY (basesink->priv->received_eos))
3104 /* for code clarity */
3105 clip_segment = basesink->abidata.ABI.clip_segment;
3107 if (G_UNLIKELY (!basesink->have_newsegment)) {
3110 sync = gst_base_sink_get_sync (basesink);
3112 GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
3113 (_("Internal data flow problem.")),
3114 ("Received buffer without a new-segment. Assuming timestamps start from 0."));
3117 /* this means this sink will assume timestamps start from 0 */
3118 GST_OBJECT_LOCK (basesink);
3119 clip_segment->start = 0;
3120 clip_segment->stop = -1;
3121 basesink->segment.start = 0;
3122 basesink->segment.stop = -1;
3123 basesink->have_newsegment = TRUE;
3124 GST_OBJECT_UNLOCK (basesink);
3127 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3129 /* check if the buffer needs to be dropped, we first ask the subclass for the
3131 if (bclass->get_times)
3132 bclass->get_times (basesink, buf, &start, &end);
3135 /* if the subclass does not want sync, we use our own values so that we at
3136 * least clip the buffer to the segment */
3137 gst_base_sink_get_times (basesink, buf, &start, &end);
3140 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
3141 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
3143 /* a dropped buffer does not participate in anything */
3144 if (GST_CLOCK_TIME_IS_VALID (start) &&
3145 (clip_segment->format == GST_FORMAT_TIME)) {
3146 if (G_UNLIKELY (!gst_segment_clip (clip_segment,
3147 GST_FORMAT_TIME, (gint64) start, (gint64) end, NULL, NULL)))
3148 goto out_of_segment;
3151 /* now we can process the buffer in the queue, this function takes ownership
3153 result = gst_base_sink_queue_object_unlocked (basesink, pad,
3154 GST_MINI_OBJECT_CAST (buf), TRUE);
3161 GST_DEBUG_OBJECT (basesink, "sink is flushing");
3162 gst_buffer_unref (buf);
3163 return GST_FLOW_WRONG_STATE;
3167 GST_DEBUG_OBJECT (basesink,
3168 "we are EOS, dropping object, return UNEXPECTED");
3169 gst_buffer_unref (buf);
3170 return GST_FLOW_UNEXPECTED;
3174 GST_DEBUG_OBJECT (basesink, "dropping buffer, out of clipping segment");
3175 gst_buffer_unref (buf);
3182 static GstFlowReturn
3183 gst_base_sink_chain (GstPad * pad, GstBuffer * buf)
3185 GstBaseSink *basesink;
3186 GstFlowReturn result;
3188 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
3190 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH))
3193 GST_PAD_PREROLL_LOCK (pad);
3194 result = gst_base_sink_chain_unlocked (basesink, pad, buf);
3195 GST_PAD_PREROLL_UNLOCK (pad);
3203 GST_OBJECT_LOCK (pad);
3204 GST_WARNING_OBJECT (basesink,
3205 "Push on pad %s:%s, but it was not activated in push mode",
3206 GST_DEBUG_PAD_NAME (pad));
3207 GST_OBJECT_UNLOCK (pad);
3208 gst_buffer_unref (buf);
3209 /* we don't post an error message this will signal to the peer
3210 * pushing that EOS is reached. */
3211 result = GST_FLOW_UNEXPECTED;
3217 gst_base_sink_default_do_seek (GstBaseSink * sink, GstSegment * segment)
3219 gboolean res = TRUE;
3221 /* update our offset if the start/stop position was updated */
3222 if (segment->format == GST_FORMAT_BYTES) {
3223 segment->time = segment->start;
3224 } else if (segment->start == 0) {
3225 /* seek to start, we can implement a default for this. */
3229 GST_INFO_OBJECT (sink, "Can't do a default seek");
3235 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
3238 gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
3239 GstEvent * event, GstSegment * segment)
3241 /* By default, we try one of 2 things:
3242 * - For absolute seek positions, convert the requested position to our
3243 * configured processing format and place it in the output segment \
3244 * - For relative seek positions, convert our current (input) values to the
3245 * seek format, adjust by the relative seek offset and then convert back to
3246 * the processing format
3248 GstSeekType cur_type, stop_type;
3251 GstFormat seek_format, dest_format;
3254 gboolean res = TRUE;
3256 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3257 &cur_type, &cur, &stop_type, &stop);
3258 dest_format = segment->format;
3260 if (seek_format == dest_format) {
3261 gst_segment_set_seek (segment, rate, seek_format, flags,
3262 cur_type, cur, stop_type, stop, &update);
3266 if (cur_type != GST_SEEK_TYPE_NONE) {
3267 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3269 gst_pad_query_convert (sink->sinkpad, seek_format, cur, &dest_format,
3271 cur_type = GST_SEEK_TYPE_SET;
3274 if (res && stop_type != GST_SEEK_TYPE_NONE) {
3275 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3277 gst_pad_query_convert (sink->sinkpad, seek_format, stop, &dest_format,
3279 stop_type = GST_SEEK_TYPE_SET;
3282 /* And finally, configure our output segment in the desired format */
3283 gst_segment_set_seek (segment, rate, dest_format, flags, cur_type, cur,
3284 stop_type, stop, &update);
3293 GST_DEBUG_OBJECT (sink, "undefined format given, seek aborted.");
3298 /* perform a seek, only executed in pull mode */
3300 gst_base_sink_perform_seek (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3304 GstFormat seek_format, dest_format;
3306 GstSeekType cur_type, stop_type;
3307 gboolean seekseg_configured = FALSE;
3309 gboolean update, res = TRUE;
3310 GstSegment seeksegment;
3312 dest_format = sink->segment.format;
3315 GST_DEBUG_OBJECT (sink, "performing seek with event %p", event);
3316 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3317 &cur_type, &cur, &stop_type, &stop);
3319 flush = flags & GST_SEEK_FLAG_FLUSH;
3321 GST_DEBUG_OBJECT (sink, "performing seek without event");
3326 GST_DEBUG_OBJECT (sink, "flushing upstream");
3327 gst_pad_push_event (pad, gst_event_new_flush_start ());
3328 gst_base_sink_flush_start (sink, pad);
3330 GST_DEBUG_OBJECT (sink, "pausing pulling thread");
3333 GST_PAD_STREAM_LOCK (pad);
3335 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
3336 * copy the current segment info into the temp segment that we can actually
3337 * attempt the seek with. We only update the real segment if the seek suceeds. */
3338 if (!seekseg_configured) {
3339 memcpy (&seeksegment, &sink->segment, sizeof (GstSegment));
3341 /* now configure the final seek segment */
3343 if (sink->segment.format != seek_format) {
3344 /* OK, here's where we give the subclass a chance to convert the relative
3345 * seek into an absolute one in the processing format. We set up any
3346 * absolute seek above, before taking the stream lock. */
3347 if (!gst_base_sink_default_prepare_seek_segment (sink, event,
3349 GST_DEBUG_OBJECT (sink,
3350 "Preparing the seek failed after flushing. " "Aborting seek");
3354 /* The seek format matches our processing format, no need to ask the
3355 * the subclass to configure the segment. */
3356 gst_segment_set_seek (&seeksegment, rate, seek_format, flags,
3357 cur_type, cur, stop_type, stop, &update);
3360 /* Else, no seek event passed, so we're just (re)starting the
3365 GST_DEBUG_OBJECT (sink, "segment configured from %" G_GINT64_FORMAT
3366 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
3367 seeksegment.start, seeksegment.stop, seeksegment.last_stop);
3369 /* do the seek, segment.last_stop contains the new position. */
3370 res = gst_base_sink_default_do_seek (sink, &seeksegment);
3375 GST_DEBUG_OBJECT (sink, "stop flushing upstream");
3376 gst_pad_push_event (pad, gst_event_new_flush_stop ());
3377 gst_base_sink_flush_stop (sink, pad);
3378 } else if (res && sink->abidata.ABI.running) {
3379 /* we are running the current segment and doing a non-flushing seek,
3380 * close the segment first based on the last_stop. */
3381 GST_DEBUG_OBJECT (sink, "closing running segment %" G_GINT64_FORMAT
3382 " to %" G_GINT64_FORMAT, sink->segment.start, sink->segment.last_stop);
3385 /* The subclass must have converted the segment to the processing format
3387 if (res && seeksegment.format != dest_format) {
3388 GST_DEBUG_OBJECT (sink, "Subclass failed to prepare a seek segment "
3389 "in the correct format. Aborting seek.");
3393 /* if successfull seek, we update our real segment and push
3394 * out the new segment. */
3396 memcpy (&sink->segment, &seeksegment, sizeof (GstSegment));
3398 if (sink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3399 gst_element_post_message (GST_ELEMENT (sink),
3400 gst_message_new_segment_start (GST_OBJECT (sink),
3401 sink->segment.format, sink->segment.last_stop));
3405 sink->priv->discont = TRUE;
3406 sink->abidata.ABI.running = TRUE;
3408 GST_PAD_STREAM_UNLOCK (pad);
3414 gst_base_sink_perform_step (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3416 GstBaseSinkPrivate *priv;
3417 GstBaseSinkClass *bclass;
3418 gboolean flush, intermediate;
3423 GstStepInfo *pending;
3425 bclass = GST_BASE_SINK_GET_CLASS (sink);
3428 GST_DEBUG_OBJECT (sink, "performing step with event %p", event);
3430 gst_event_parse_step (event, &format, &amount, &rate, &flush, &intermediate);
3431 seqnum = gst_event_get_seqnum (event);
3433 pending = &priv->pending_step;
3436 /* we need to call ::unlock before locking PREROLL_LOCK
3437 * since we lock it before going into ::render */
3439 bclass->unlock (sink);
3441 GST_PAD_PREROLL_LOCK (sink->sinkpad);
3442 /* now that we have the PREROLL lock, clear our unlock request */
3443 if (bclass->unlock_stop)
3444 bclass->unlock_stop (sink);
3446 /* update the stepinfo and make it valid */
3447 pending->seqnum = seqnum;
3448 pending->format = format;
3449 pending->amount = amount;
3450 pending->position = 0;
3451 pending->rate = rate;
3452 pending->flush = flush;
3453 pending->intermediate = intermediate;
3454 pending->valid = TRUE;
3456 if (sink->priv->async_enabled) {
3457 /* and we need to commit our state again on the next
3458 * prerolled buffer */
3459 sink->playing_async = TRUE;
3460 priv->pending_step.need_preroll = TRUE;
3461 sink->need_preroll = FALSE;
3462 gst_element_lost_state_full (GST_ELEMENT_CAST (sink), FALSE);
3464 sink->priv->have_latency = TRUE;
3465 sink->need_preroll = FALSE;
3467 priv->call_preroll = TRUE;
3468 gst_base_sink_set_last_buffer (sink, NULL);
3469 gst_base_sink_reset_qos (sink);
3471 if (sink->clock_id) {
3472 gst_clock_id_unschedule (sink->clock_id);
3475 if (sink->have_preroll) {
3476 GST_DEBUG_OBJECT (sink, "signal waiter");
3477 priv->step_unlock = TRUE;
3478 GST_PAD_PREROLL_SIGNAL (sink->sinkpad);
3480 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
3489 gst_base_sink_loop (GstPad * pad)
3491 GstBaseSink *basesink;
3492 GstBuffer *buf = NULL;
3493 GstFlowReturn result;
3497 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
3499 g_assert (basesink->pad_mode == GST_ACTIVATE_PULL);
3501 if ((blocksize = basesink->priv->blocksize) == 0)
3504 offset = basesink->segment.last_stop;
3506 GST_DEBUG_OBJECT (basesink, "pulling %" G_GUINT64_FORMAT ", %u",
3509 result = gst_pad_pull_range (pad, offset, blocksize, &buf);
3510 if (G_UNLIKELY (result != GST_FLOW_OK))
3513 if (G_UNLIKELY (buf == NULL))
3516 offset += GST_BUFFER_SIZE (buf);
3518 gst_segment_set_last_stop (&basesink->segment, GST_FORMAT_BYTES, offset);
3520 GST_PAD_PREROLL_LOCK (pad);
3521 result = gst_base_sink_chain_unlocked (basesink, pad, buf);
3522 GST_PAD_PREROLL_UNLOCK (pad);
3523 if (G_UNLIKELY (result != GST_FLOW_OK))
3531 GST_LOG_OBJECT (basesink, "pausing task, reason %s",
3532 gst_flow_get_name (result));
3533 gst_pad_pause_task (pad);
3534 /* fatal errors and NOT_LINKED cause EOS */
3535 if (GST_FLOW_IS_FATAL (result) || result == GST_FLOW_NOT_LINKED) {
3536 if (result == GST_FLOW_UNEXPECTED) {
3537 /* perform EOS logic */
3538 if (basesink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3539 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3540 gst_message_new_segment_done (GST_OBJECT_CAST (basesink),
3541 basesink->segment.format, basesink->segment.last_stop));
3543 gst_base_sink_event (pad, gst_event_new_eos ());
3546 /* for fatal errors we post an error message, post the error
3547 * first so the app knows about the error first. */
3548 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
3549 (_("Internal data stream error.")),
3550 ("stream stopped, reason %s", gst_flow_get_name (result)));
3551 gst_base_sink_event (pad, gst_event_new_eos ());
3558 GST_LOG_OBJECT (basesink, "no buffer, pausing");
3559 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
3560 (_("Internal data flow error.")), ("element returned NULL buffer"));
3561 result = GST_FLOW_ERROR;
3567 gst_base_sink_set_flushing (GstBaseSink * basesink, GstPad * pad,
3570 GstBaseSinkClass *bclass;
3572 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3575 /* unlock any subclasses, we need to do this before grabbing the
3576 * PREROLL_LOCK since we hold this lock before going into ::render. */
3578 bclass->unlock (basesink);
3581 GST_PAD_PREROLL_LOCK (pad);
3582 basesink->flushing = flushing;
3584 /* step 1, now that we have the PREROLL lock, clear our unlock request */
3585 if (bclass->unlock_stop)
3586 bclass->unlock_stop (basesink);
3588 /* set need_preroll before we unblock the clock. If the clock is unblocked
3589 * before timing out, we can reuse the buffer for preroll. */
3590 basesink->need_preroll = TRUE;
3592 /* step 2, unblock clock sync (if any) or any other blocking thing */
3593 if (basesink->clock_id) {
3594 gst_clock_id_unschedule (basesink->clock_id);
3597 /* flush out the data thread if it's locked in finish_preroll, this will
3598 * also flush out the EOS state */
3599 GST_DEBUG_OBJECT (basesink,
3600 "flushing out data thread, need preroll to TRUE");
3601 gst_base_sink_preroll_queue_flush (basesink, pad);
3603 GST_PAD_PREROLL_UNLOCK (pad);
3609 gst_base_sink_default_activate_pull (GstBaseSink * basesink, gboolean active)
3615 result = gst_pad_start_task (basesink->sinkpad,
3616 (GstTaskFunction) gst_base_sink_loop, basesink->sinkpad);
3618 /* step 2, make sure streaming finishes */
3619 result = gst_pad_stop_task (basesink->sinkpad);
3626 gst_base_sink_pad_activate (GstPad * pad)
3628 gboolean result = FALSE;
3629 GstBaseSink *basesink;
3631 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3633 GST_DEBUG_OBJECT (basesink, "Trying pull mode first");
3635 gst_base_sink_set_flushing (basesink, pad, FALSE);
3637 /* we need to have the pull mode enabled */
3638 if (!basesink->can_activate_pull) {
3639 GST_DEBUG_OBJECT (basesink, "pull mode disabled");
3643 /* check if downstreams supports pull mode at all */
3644 if (!gst_pad_check_pull_range (pad)) {
3645 GST_DEBUG_OBJECT (basesink, "pull mode not supported");
3649 /* set the pad mode before starting the task so that it's in the
3650 * correct state for the new thread. also the sink set_caps and get_caps
3651 * function checks this */
3652 basesink->pad_mode = GST_ACTIVATE_PULL;
3654 /* we first try to negotiate a format so that when we try to activate
3655 * downstream, it knows about our format */
3656 if (!gst_base_sink_negotiate_pull (basesink)) {
3657 GST_DEBUG_OBJECT (basesink, "failed to negotiate in pull mode");
3661 /* ok activate now */
3662 if (!gst_pad_activate_pull (pad, TRUE)) {
3663 /* clear any pending caps */
3664 GST_OBJECT_LOCK (basesink);
3665 gst_caps_replace (&basesink->priv->pull_caps, NULL);
3666 GST_OBJECT_UNLOCK (basesink);
3667 GST_DEBUG_OBJECT (basesink, "failed to activate in pull mode");
3671 GST_DEBUG_OBJECT (basesink, "Success activating pull mode");
3675 /* push mode fallback */
3677 GST_DEBUG_OBJECT (basesink, "Falling back to push mode");
3678 if ((result = gst_pad_activate_push (pad, TRUE))) {
3679 GST_DEBUG_OBJECT (basesink, "Success activating push mode");
3684 GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
3685 gst_base_sink_set_flushing (basesink, pad, TRUE);
3688 gst_object_unref (basesink);
3694 gst_base_sink_pad_activate_push (GstPad * pad, gboolean active)
3697 GstBaseSink *basesink;
3699 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3702 if (!basesink->can_activate_push) {
3704 basesink->pad_mode = GST_ACTIVATE_NONE;
3707 basesink->pad_mode = GST_ACTIVATE_PUSH;
3710 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH)) {
3711 g_warning ("Internal GStreamer activation error!!!");
3714 gst_base_sink_set_flushing (basesink, pad, TRUE);
3716 basesink->pad_mode = GST_ACTIVATE_NONE;
3720 gst_object_unref (basesink);
3726 gst_base_sink_negotiate_pull (GstBaseSink * basesink)
3733 /* this returns the intersection between our caps and the peer caps. If there
3734 * is no peer, it returns NULL and we can't operate in pull mode so we can
3735 * fail the negotiation. */
3736 caps = gst_pad_get_allowed_caps (GST_BASE_SINK_PAD (basesink));
3737 if (caps == NULL || gst_caps_is_empty (caps))
3738 goto no_caps_possible;
3740 GST_DEBUG_OBJECT (basesink, "allowed caps: %" GST_PTR_FORMAT, caps);
3742 caps = gst_caps_make_writable (caps);
3743 /* get the first (prefered) format */
3744 gst_caps_truncate (caps);
3746 gst_pad_fixate_caps (GST_BASE_SINK_PAD (basesink), caps);
3748 GST_DEBUG_OBJECT (basesink, "fixated to: %" GST_PTR_FORMAT, caps);
3750 if (gst_caps_is_any (caps)) {
3751 GST_DEBUG_OBJECT (basesink, "caps were ANY after fixating, "
3753 /* neither side has template caps in this case, so they are prepared for
3754 pull() without setcaps() */
3756 } else if (gst_caps_is_fixed (caps)) {
3757 if (!gst_pad_set_caps (GST_BASE_SINK_PAD (basesink), caps))
3758 goto could_not_set_caps;
3760 GST_OBJECT_LOCK (basesink);
3761 gst_caps_replace (&basesink->priv->pull_caps, caps);
3762 GST_OBJECT_UNLOCK (basesink);
3767 gst_caps_unref (caps);
3773 GST_INFO_OBJECT (basesink, "Pipeline could not agree on caps");
3774 GST_DEBUG_OBJECT (basesink, "get_allowed_caps() returned EMPTY");
3776 gst_caps_unref (caps);
3781 GST_INFO_OBJECT (basesink, "Could not set caps: %" GST_PTR_FORMAT, caps);
3782 gst_caps_unref (caps);
3787 /* this won't get called until we implement an activate function */
3789 gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active)
3791 gboolean result = FALSE;
3792 GstBaseSink *basesink;
3793 GstBaseSinkClass *bclass;
3795 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3796 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3802 /* we mark we have a newsegment here because pull based
3803 * mode works just fine without having a newsegment before the
3805 format = GST_FORMAT_BYTES;
3807 gst_segment_init (&basesink->segment, format);
3808 gst_segment_init (basesink->abidata.ABI.clip_segment, format);
3809 GST_OBJECT_LOCK (basesink);
3810 basesink->have_newsegment = TRUE;
3811 GST_OBJECT_UNLOCK (basesink);
3813 /* get the peer duration in bytes */
3814 result = gst_pad_query_peer_duration (pad, &format, &duration);
3816 GST_DEBUG_OBJECT (basesink,
3817 "setting duration in bytes to %" G_GINT64_FORMAT, duration);
3818 gst_segment_set_duration (basesink->abidata.ABI.clip_segment, format,
3820 gst_segment_set_duration (&basesink->segment, format, duration);
3822 GST_DEBUG_OBJECT (basesink, "unknown duration");
3825 if (bclass->activate_pull)
3826 result = bclass->activate_pull (basesink, TRUE);
3831 goto activate_failed;
3834 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PULL)) {
3835 g_warning ("Internal GStreamer activation error!!!");
3838 result = gst_base_sink_set_flushing (basesink, pad, TRUE);
3839 if (bclass->activate_pull)
3840 result &= bclass->activate_pull (basesink, FALSE);
3841 basesink->pad_mode = GST_ACTIVATE_NONE;
3842 /* clear any pending caps */
3843 GST_OBJECT_LOCK (basesink);
3844 gst_caps_replace (&basesink->priv->pull_caps, NULL);
3845 GST_OBJECT_UNLOCK (basesink);
3848 gst_object_unref (basesink);
3855 /* reset, as starting the thread failed */
3856 basesink->pad_mode = GST_ACTIVATE_NONE;
3858 GST_ERROR_OBJECT (basesink, "subclass failed to activate in pull mode");
3863 /* send an event to our sinkpad peer. */
3865 gst_base_sink_send_event (GstElement * element, GstEvent * event)
3868 GstBaseSink *basesink = GST_BASE_SINK (element);
3869 gboolean forward, result = TRUE;
3870 GstActivateMode mode;
3872 GST_OBJECT_LOCK (element);
3873 /* get the pad and the scheduling mode */
3874 pad = gst_object_ref (basesink->sinkpad);
3875 mode = basesink->pad_mode;
3876 GST_OBJECT_UNLOCK (element);
3878 /* only push UPSTREAM events upstream */
3879 forward = GST_EVENT_IS_UPSTREAM (event);
3881 switch (GST_EVENT_TYPE (event)) {
3882 case GST_EVENT_LATENCY:
3884 GstClockTime latency;
3886 gst_event_parse_latency (event, &latency);
3888 /* store the latency. We use this to adjust the running_time before syncing
3889 * it to the clock. */
3890 GST_OBJECT_LOCK (element);
3891 basesink->priv->latency = latency;
3892 if (!basesink->priv->have_latency)
3894 GST_OBJECT_UNLOCK (element);
3895 GST_DEBUG_OBJECT (basesink, "latency set to %" GST_TIME_FORMAT,
3896 GST_TIME_ARGS (latency));
3898 /* We forward this event so that all elements know about the global pipeline
3899 * latency. This is interesting for an element when it wants to figure out
3900 * when a particular piece of data will be rendered. */
3903 case GST_EVENT_SEEK:
3904 /* in pull mode we will execute the seek */
3905 if (mode == GST_ACTIVATE_PULL)
3906 result = gst_base_sink_perform_seek (basesink, pad, event);
3908 case GST_EVENT_STEP:
3909 result = gst_base_sink_perform_step (basesink, pad, event);
3917 result = gst_pad_push_event (pad, event);
3919 /* not forwarded, unref the event */
3920 gst_event_unref (event);
3923 gst_object_unref (pad);
3928 gst_base_sink_peer_query (GstBaseSink * sink, GstQuery * query)
3931 gboolean res = FALSE;
3933 if ((peer = gst_pad_get_peer (sink->sinkpad))) {
3934 res = gst_pad_query (peer, query);
3935 gst_object_unref (peer);
3940 /* get the end position of the last seen object, this is used
3941 * for EOS and for making sure that we don't report a position we
3942 * have not reached yet. With LOCK. */
3944 gst_base_sink_get_position_last (GstBaseSink * basesink, GstFormat format,
3948 GstSegment *segment;
3949 gboolean ret = TRUE;
3951 segment = &basesink->segment;
3952 oformat = segment->format;
3954 if (oformat == GST_FORMAT_TIME) {
3955 /* return last observed stream time, we keep the stream time around in the
3957 *cur = basesink->priv->current_sstop;
3959 /* convert last stop to stream time */
3960 *cur = gst_segment_to_stream_time (segment, oformat, segment->last_stop);
3963 if (*cur != -1 && oformat != format) {
3964 GST_OBJECT_UNLOCK (basesink);
3965 /* convert to the target format if we need to, release lock first */
3967 gst_pad_query_convert (basesink->sinkpad, oformat, *cur, &format, cur);
3970 GST_OBJECT_LOCK (basesink);
3973 GST_DEBUG_OBJECT (basesink, "POSITION: %" GST_TIME_FORMAT,
3974 GST_TIME_ARGS (*cur));
3979 /* get the position when we are PAUSED, this is the stream time of the buffer
3980 * that prerolled. If no buffer is prerolled (we are still flushing), this
3981 * value will be -1. With LOCK. */
3983 gst_base_sink_get_position_paused (GstBaseSink * basesink, GstFormat format,
3988 GstSegment *segment;
3991 /* we don't use the clip segment in pull mode, when seeking we update the
3992 * main segment directly with the new segment values without it having to be
3993 * activated by the rendering after preroll */
3994 if (basesink->pad_mode == GST_ACTIVATE_PUSH)
3995 segment = basesink->abidata.ABI.clip_segment;
3997 segment = &basesink->segment;
3998 oformat = segment->format;
4000 if (oformat == GST_FORMAT_TIME) {
4001 *cur = basesink->priv->current_sstart;
4003 *cur = gst_segment_to_stream_time (segment, oformat, segment->last_stop);
4006 time = segment->time;
4009 *cur = MAX (*cur, time);
4010 GST_DEBUG_OBJECT (basesink, "POSITION as max: %" GST_TIME_FORMAT
4011 ", time %" GST_TIME_FORMAT, GST_TIME_ARGS (*cur), GST_TIME_ARGS (time));
4013 /* we have no buffer, use the segment times. */
4014 if (segment->rate >= 0.0) {
4015 /* forward, next position is always the time of the segment */
4017 GST_DEBUG_OBJECT (basesink, "POSITION as time: %" GST_TIME_FORMAT,
4018 GST_TIME_ARGS (*cur));
4020 /* reverse, next expected timestamp is segment->stop. We use the function
4021 * to get things right for negative applied_rates. */
4022 *cur = gst_segment_to_stream_time (segment, oformat, segment->stop);
4023 GST_DEBUG_OBJECT (basesink, "reverse POSITION: %" GST_TIME_FORMAT,
4024 GST_TIME_ARGS (*cur));
4029 if (res && oformat != format) {
4030 GST_OBJECT_UNLOCK (basesink);
4032 gst_pad_query_convert (basesink->sinkpad, oformat, *cur, &format, cur);
4035 GST_OBJECT_LOCK (basesink);
4042 gst_base_sink_get_position (GstBaseSink * basesink, GstFormat format,
4043 gint64 * cur, gboolean * upstream)
4046 gboolean res = FALSE;
4047 GstFormat oformat, tformat;
4048 GstClockTime now, base, latency;
4049 gint64 time, accum, duration;
4053 GST_OBJECT_LOCK (basesink);
4054 /* our intermediate time format */
4055 tformat = GST_FORMAT_TIME;
4056 /* get the format in the segment */
4057 oformat = basesink->segment.format;
4059 /* can only give answer based on the clock if not EOS */
4060 if (G_UNLIKELY (basesink->eos))
4063 /* we can only get the segment when we are not NULL or READY */
4064 if (!basesink->have_newsegment)
4067 /* when not in PLAYING or when we're busy with a state change, we
4068 * cannot read from the clock so we report time based on the
4069 * last seen timestamp. */
4070 if (GST_STATE (basesink) != GST_STATE_PLAYING ||
4071 GST_STATE_PENDING (basesink) != GST_STATE_VOID_PENDING)
4074 /* we need to sync on the clock. */
4075 if (basesink->sync == FALSE)
4078 /* and we need a clock */
4079 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (basesink)) == NULL))
4082 /* collect all data we need holding the lock */
4083 if (GST_CLOCK_TIME_IS_VALID (basesink->segment.time))
4084 time = basesink->segment.time;
4088 if (GST_CLOCK_TIME_IS_VALID (basesink->segment.stop))
4089 duration = basesink->segment.stop - basesink->segment.start;
4093 base = GST_ELEMENT_CAST (basesink)->base_time;
4094 accum = basesink->segment.accum;
4095 rate = basesink->segment.rate * basesink->segment.applied_rate;
4096 latency = basesink->priv->latency;
4098 gst_object_ref (clock);
4100 /* this function might release the LOCK */
4101 gst_base_sink_get_position_last (basesink, format, &last);
4103 /* need to release the object lock before we can get the time,
4104 * a clock might take the LOCK of the provider, which could be
4105 * a basesink subclass. */
4106 GST_OBJECT_UNLOCK (basesink);
4108 now = gst_clock_get_time (clock);
4110 if (oformat != tformat) {
4111 /* convert accum, time and duration to time */
4112 if (!gst_pad_query_convert (basesink->sinkpad, oformat, accum, &tformat,
4114 goto convert_failed;
4115 if (!gst_pad_query_convert (basesink->sinkpad, oformat, duration, &tformat,
4117 goto convert_failed;
4118 if (!gst_pad_query_convert (basesink->sinkpad, oformat, time, &tformat,
4120 goto convert_failed;
4123 /* subtract base time and accumulated time from the clock time.
4124 * Make sure we don't go negative. This is the current time in
4125 * the segment which we need to scale with the combined
4126 * rate and applied rate. */
4129 base = MIN (now, base);
4131 /* for negative rates we need to count back from from the segment
4136 *cur = time + gst_guint64_to_gdouble (now - base) * rate;
4138 /* never report more than last seen position */
4140 *cur = MIN (last, *cur);
4142 gst_object_unref (clock);
4144 GST_DEBUG_OBJECT (basesink,
4145 "now %" GST_TIME_FORMAT " - base %" GST_TIME_FORMAT " - accum %"
4146 GST_TIME_FORMAT " + time %" GST_TIME_FORMAT,
4147 GST_TIME_ARGS (now), GST_TIME_ARGS (base),
4148 GST_TIME_ARGS (accum), GST_TIME_ARGS (time));
4150 if (oformat != format) {
4151 /* convert time to final format */
4152 if (!gst_pad_query_convert (basesink->sinkpad, tformat, *cur, &format, cur))
4153 goto convert_failed;
4159 GST_DEBUG_OBJECT (basesink, "res: %d, POSITION: %" GST_TIME_FORMAT,
4160 res, GST_TIME_ARGS (*cur));
4166 GST_DEBUG_OBJECT (basesink, "position in EOS");
4167 res = gst_base_sink_get_position_last (basesink, format, cur);
4168 GST_OBJECT_UNLOCK (basesink);
4173 GST_DEBUG_OBJECT (basesink, "position in PAUSED");
4174 res = gst_base_sink_get_position_paused (basesink, format, cur);
4175 GST_OBJECT_UNLOCK (basesink);
4180 /* in NULL or READY we always return FALSE and -1 */
4181 GST_DEBUG_OBJECT (basesink, "position in wrong state, return -1");
4184 GST_OBJECT_UNLOCK (basesink);
4189 /* report last seen timestamp if any, else ask upstream to answer */
4190 if ((*cur = basesink->priv->current_sstart) != -1)
4195 GST_DEBUG_OBJECT (basesink, "no sync, res %d, POSITION %" GST_TIME_FORMAT,
4196 res, GST_TIME_ARGS (*cur));
4197 GST_OBJECT_UNLOCK (basesink);
4202 GST_DEBUG_OBJECT (basesink, "convert failed, try upstream");
4209 gst_base_sink_query (GstElement * element, GstQuery * query)
4211 gboolean res = FALSE;
4213 GstBaseSink *basesink = GST_BASE_SINK (element);
4215 switch (GST_QUERY_TYPE (query)) {
4216 case GST_QUERY_POSITION:
4220 gboolean upstream = FALSE;
4222 gst_query_parse_position (query, &format, NULL);
4224 GST_DEBUG_OBJECT (basesink, "position format %d", format);
4226 /* first try to get the position based on the clock */
4228 gst_base_sink_get_position (basesink, format, &cur, &upstream))) {
4229 gst_query_set_position (query, format, cur);
4230 } else if (upstream) {
4231 /* fallback to peer query */
4232 res = gst_base_sink_peer_query (basesink, query);
4236 case GST_QUERY_DURATION:
4238 GstFormat format, uformat;
4239 gint64 duration, uduration;
4241 gst_query_parse_duration (query, &format, NULL);
4243 GST_DEBUG_OBJECT (basesink, "duration query in format %s",
4244 gst_format_get_name (format));
4246 if (basesink->pad_mode == GST_ACTIVATE_PULL) {
4247 uformat = GST_FORMAT_BYTES;
4249 /* get the duration in bytes, in pull mode that's all we are sure to
4250 * know. We have to explicitly get this value from upstream instead of
4251 * using our cached value because it might change. Duration caching
4252 * should be done at a higher level. */
4253 res = gst_pad_query_peer_duration (basesink->sinkpad, &uformat,
4256 gst_segment_set_duration (&basesink->segment, uformat, uduration);
4257 if (format != uformat) {
4258 /* convert to the requested format */
4259 res = gst_pad_query_convert (basesink->sinkpad, uformat, uduration,
4260 &format, &duration);
4262 duration = uduration;
4265 /* set the result */
4266 gst_query_set_duration (query, format, duration);
4270 /* in push mode we simply forward upstream */
4271 res = gst_base_sink_peer_query (basesink, query);
4275 case GST_QUERY_LATENCY:
4277 gboolean live, us_live;
4278 GstClockTime min, max;
4280 if ((res = gst_base_sink_query_latency (basesink, &live, &us_live, &min,
4282 gst_query_set_latency (query, live, min, max);
4286 case GST_QUERY_JITTER:
4288 case GST_QUERY_RATE:
4289 /* gst_query_set_rate (query, basesink->segment_rate); */
4292 case GST_QUERY_SEGMENT:
4294 /* FIXME, bring start/stop to stream time */
4295 gst_query_set_segment (query, basesink->segment.rate,
4296 GST_FORMAT_TIME, basesink->segment.start, basesink->segment.stop);
4299 case GST_QUERY_SEEKING:
4300 case GST_QUERY_CONVERT:
4301 case GST_QUERY_FORMATS:
4303 res = gst_base_sink_peer_query (basesink, query);
4309 static GstStateChangeReturn
4310 gst_base_sink_change_state (GstElement * element, GstStateChange transition)
4312 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
4313 GstBaseSink *basesink = GST_BASE_SINK (element);
4314 GstBaseSinkClass *bclass;
4315 GstBaseSinkPrivate *priv;
4317 priv = basesink->priv;
4319 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4321 switch (transition) {
4322 case GST_STATE_CHANGE_NULL_TO_READY:
4324 if (!bclass->start (basesink))
4327 case GST_STATE_CHANGE_READY_TO_PAUSED:
4328 /* need to complete preroll before this state change completes, there
4329 * is no data flow in READY so we can safely assume we need to preroll. */
4330 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4331 GST_DEBUG_OBJECT (basesink, "READY to PAUSED");
4332 basesink->have_newsegment = FALSE;
4333 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
4334 gst_segment_init (basesink->abidata.ABI.clip_segment,
4335 GST_FORMAT_UNDEFINED);
4336 basesink->offset = 0;
4337 basesink->have_preroll = FALSE;
4338 priv->step_unlock = FALSE;
4339 basesink->need_preroll = TRUE;
4340 basesink->playing_async = TRUE;
4341 priv->current_sstart = -1;
4342 priv->current_sstop = -1;
4343 priv->eos_rtime = -1;
4345 basesink->eos = FALSE;
4346 priv->received_eos = FALSE;
4347 gst_base_sink_reset_qos (basesink);
4348 priv->commited = FALSE;
4349 priv->call_preroll = TRUE;
4350 priv->current_step.valid = FALSE;
4351 priv->pending_step.valid = FALSE;
4352 if (priv->async_enabled) {
4353 GST_DEBUG_OBJECT (basesink, "doing async state change");
4354 /* when async enabled, post async-start message and return ASYNC from
4355 * the state change function */
4356 ret = GST_STATE_CHANGE_ASYNC;
4357 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4358 gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
4360 priv->have_latency = TRUE;
4362 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4364 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
4365 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4366 if (!gst_base_sink_needs_preroll (basesink)) {
4367 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, don't need preroll");
4368 /* no preroll needed anymore now. */
4369 basesink->playing_async = FALSE;
4370 basesink->need_preroll = FALSE;
4371 if (basesink->eos) {
4372 GstMessage *message;
4374 /* need to post EOS message here */
4375 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
4376 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
4377 gst_message_set_seqnum (message, basesink->priv->seqnum);
4378 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
4380 GST_DEBUG_OBJECT (basesink, "signal preroll");
4381 GST_PAD_PREROLL_SIGNAL (basesink->sinkpad);
4384 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, we are not prerolled");
4385 basesink->need_preroll = TRUE;
4386 basesink->playing_async = TRUE;
4387 priv->call_preroll = TRUE;
4388 priv->commited = FALSE;
4389 if (priv->async_enabled) {
4390 GST_DEBUG_OBJECT (basesink, "doing async state change");
4391 ret = GST_STATE_CHANGE_ASYNC;
4392 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4393 gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
4396 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4403 GstStateChangeReturn bret;
4405 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4406 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
4407 goto activate_failed;
4410 switch (transition) {
4411 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
4412 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED");
4413 /* FIXME, make sure we cannot enter _render first */
4415 /* we need to call ::unlock before locking PREROLL_LOCK
4416 * since we lock it before going into ::render */
4418 bclass->unlock (basesink);
4420 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4421 /* now that we have the PREROLL lock, clear our unlock request */
4422 if (bclass->unlock_stop)
4423 bclass->unlock_stop (basesink);
4425 /* we need preroll again and we set the flag before unlocking the clockid
4426 * because if the clockid is unlocked before a current buffer expired, we
4427 * can use that buffer to preroll with */
4428 basesink->need_preroll = TRUE;
4430 if (basesink->clock_id) {
4431 gst_clock_id_unschedule (basesink->clock_id);
4434 /* if we don't have a preroll buffer we need to wait for a preroll and
4436 if (!gst_base_sink_needs_preroll (basesink)) {
4437 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, we are prerolled");
4438 basesink->playing_async = FALSE;
4440 if (GST_STATE_TARGET (GST_ELEMENT (basesink)) <= GST_STATE_READY) {
4441 ret = GST_STATE_CHANGE_SUCCESS;
4443 GST_DEBUG_OBJECT (basesink,
4444 "PLAYING to PAUSED, we are not prerolled");
4445 basesink->playing_async = TRUE;
4446 priv->commited = FALSE;
4447 priv->call_preroll = TRUE;
4448 if (priv->async_enabled) {
4449 GST_DEBUG_OBJECT (basesink, "doing async state change");
4450 ret = GST_STATE_CHANGE_ASYNC;
4451 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4452 gst_message_new_async_start (GST_OBJECT_CAST (basesink),
4457 GST_DEBUG_OBJECT (basesink, "rendered: %" G_GUINT64_FORMAT
4458 ", dropped: %" G_GUINT64_FORMAT, priv->rendered, priv->dropped);
4460 gst_base_sink_reset_qos (basesink);
4461 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4463 case GST_STATE_CHANGE_PAUSED_TO_READY:
4464 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4465 /* start by reseting our position state with the object lock so that the
4466 * position query gets the right idea. We do this before we post the
4467 * messages so that the message handlers pick this up. */
4468 GST_OBJECT_LOCK (basesink);
4469 basesink->have_newsegment = FALSE;
4470 priv->current_sstart = -1;
4471 priv->current_sstop = -1;
4472 priv->have_latency = FALSE;
4473 GST_OBJECT_UNLOCK (basesink);
4475 gst_base_sink_set_last_buffer (basesink, NULL);
4476 priv->call_preroll = FALSE;
4478 if (!priv->commited) {
4479 if (priv->async_enabled) {
4480 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, posting async-done");
4482 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4483 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
4484 GST_STATE_PLAYING, GST_STATE_PAUSED, GST_STATE_READY));
4486 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4487 gst_message_new_async_done (GST_OBJECT_CAST (basesink)));
4489 priv->commited = TRUE;
4491 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, don't need_preroll");
4493 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4495 case GST_STATE_CHANGE_READY_TO_NULL:
4497 if (!bclass->stop (basesink)) {
4498 GST_WARNING_OBJECT (basesink, "failed to stop");
4501 gst_base_sink_set_last_buffer (basesink, NULL);
4502 priv->call_preroll = FALSE;
4513 GST_DEBUG_OBJECT (basesink, "failed to start");
4514 return GST_STATE_CHANGE_FAILURE;
4518 GST_DEBUG_OBJECT (basesink,
4519 "element failed to change states -- activation problem?");
4520 return GST_STATE_CHANGE_FAILURE;