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))
164 gboolean intermediate;
165 gboolean need_preroll;
168 /* FIXME, some stuff in ABI.data and other in Private...
169 * Make up your mind please.
171 struct _GstBaseSinkPrivate
173 gint qos_enabled; /* ATOMIC */
174 gboolean async_enabled;
175 GstClockTimeDiff ts_offset;
176 GstClockTime render_delay;
178 /* start, stop of current buffer, stream time, used to report position */
179 GstClockTime current_sstart;
180 GstClockTime current_sstop;
182 /* start, stop and jitter of current buffer, running time */
183 GstClockTime current_rstart;
184 GstClockTime current_rstop;
185 GstClockTimeDiff current_jitter;
187 /* EOS sync time in running time */
188 GstClockTime eos_rtime;
190 /* last buffer that arrived in time, running time */
191 GstClockTime last_in_time;
192 /* when the last buffer left the sink, running time */
193 GstClockTime last_left;
195 /* running averages go here these are done on running time */
197 GstClockTime avg_duration;
200 /* these are done on system time. avg_jitter and avg_render are
201 * compared to eachother to see if the rendering time takes a
202 * huge amount of the processing, If so we are flooded with
204 GstClockTime last_left_systime;
205 GstClockTime avg_jitter;
206 GstClockTime start, stop;
207 GstClockTime avg_render;
209 /* number of rendered and dropped frames */
214 GstClockTime latency;
216 /* if we already commited the state */
219 /* when we received EOS */
220 gboolean received_eos;
222 /* when we are prerolled and able to report latency */
223 gboolean have_latency;
225 /* the last buffer we prerolled or rendered. Useful for making snapshots */
226 GstBuffer *last_buffer;
228 /* caps for pull based scheduling */
231 /* blocksize for pulling */
236 /* seqnum of the stream */
239 gboolean call_preroll;
244 #define DO_RUNNING_AVG(avg,val,size) (((val) + ((size)-1) * (avg)) / (size))
246 /* generic running average, this has a neutral window size */
247 #define UPDATE_RUNNING_AVG(avg,val) DO_RUNNING_AVG(avg,val,8)
249 /* the windows for these running averages are experimentally obtained.
250 * possitive values get averaged more while negative values use a small
251 * window so we can react faster to badness. */
252 #define UPDATE_RUNNING_AVG_P(avg,val) DO_RUNNING_AVG(avg,val,16)
253 #define UPDATE_RUNNING_AVG_N(avg,val) DO_RUNNING_AVG(avg,val,4)
255 /* BaseSink properties */
257 #define DEFAULT_CAN_ACTIVATE_PULL FALSE /* fixme: enable me */
258 #define DEFAULT_CAN_ACTIVATE_PUSH TRUE
260 #define DEFAULT_PREROLL_QUEUE_LEN 0
261 #define DEFAULT_SYNC TRUE
262 #define DEFAULT_MAX_LATENESS -1
263 #define DEFAULT_QOS FALSE
264 #define DEFAULT_ASYNC TRUE
265 #define DEFAULT_TS_OFFSET 0
266 #define DEFAULT_BLOCKSIZE 4096
267 #define DEFAULT_RENDER_DELAY 0
272 PROP_PREROLL_QUEUE_LEN,
284 static GstElementClass *parent_class = NULL;
286 static void gst_base_sink_class_init (GstBaseSinkClass * klass);
287 static void gst_base_sink_init (GstBaseSink * trans, gpointer g_class);
288 static void gst_base_sink_finalize (GObject * object);
291 gst_base_sink_get_type (void)
293 static volatile gsize base_sink_type = 0;
295 if (g_once_init_enter (&base_sink_type)) {
297 static const GTypeInfo base_sink_info = {
298 sizeof (GstBaseSinkClass),
301 (GClassInitFunc) gst_base_sink_class_init,
304 sizeof (GstBaseSink),
306 (GInstanceInitFunc) gst_base_sink_init,
309 _type = g_type_register_static (GST_TYPE_ELEMENT,
310 "GstBaseSink", &base_sink_info, G_TYPE_FLAG_ABSTRACT);
311 g_once_init_leave (&base_sink_type, _type);
313 return base_sink_type;
316 static void gst_base_sink_set_property (GObject * object, guint prop_id,
317 const GValue * value, GParamSpec * pspec);
318 static void gst_base_sink_get_property (GObject * object, guint prop_id,
319 GValue * value, GParamSpec * pspec);
321 static gboolean gst_base_sink_send_event (GstElement * element,
323 static gboolean gst_base_sink_query (GstElement * element, GstQuery * query);
325 static GstCaps *gst_base_sink_get_caps (GstBaseSink * sink);
326 static gboolean gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps);
327 static GstFlowReturn gst_base_sink_buffer_alloc (GstBaseSink * sink,
328 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
329 static void gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
330 GstClockTime * start, GstClockTime * end);
331 static gboolean gst_base_sink_set_flushing (GstBaseSink * basesink,
332 GstPad * pad, gboolean flushing);
333 static gboolean gst_base_sink_default_activate_pull (GstBaseSink * basesink,
335 static gboolean gst_base_sink_default_do_seek (GstBaseSink * sink,
336 GstSegment * segment);
337 static gboolean gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
338 GstEvent * event, GstSegment * segment);
340 static GstStateChangeReturn gst_base_sink_change_state (GstElement * element,
341 GstStateChange transition);
343 static GstFlowReturn gst_base_sink_chain (GstPad * pad, GstBuffer * buffer);
344 static void gst_base_sink_loop (GstPad * pad);
345 static gboolean gst_base_sink_pad_activate (GstPad * pad);
346 static gboolean gst_base_sink_pad_activate_push (GstPad * pad, gboolean active);
347 static gboolean gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active);
348 static gboolean gst_base_sink_event (GstPad * pad, GstEvent * event);
349 static gboolean gst_base_sink_peer_query (GstBaseSink * sink, GstQuery * query);
351 static gboolean gst_base_sink_negotiate_pull (GstBaseSink * basesink);
353 /* check if an object was too late */
354 static gboolean gst_base_sink_is_too_late (GstBaseSink * basesink,
355 GstMiniObject * obj, GstClockTime start, GstClockTime stop,
356 GstClockReturn status, GstClockTimeDiff jitter);
357 static GstFlowReturn gst_base_sink_preroll_object (GstBaseSink * basesink,
358 GstMiniObject * obj);
361 gst_base_sink_class_init (GstBaseSinkClass * klass)
363 GObjectClass *gobject_class;
364 GstElementClass *gstelement_class;
366 gobject_class = G_OBJECT_CLASS (klass);
367 gstelement_class = GST_ELEMENT_CLASS (klass);
369 GST_DEBUG_CATEGORY_INIT (gst_base_sink_debug, "basesink", 0,
372 g_type_class_add_private (klass, sizeof (GstBaseSinkPrivate));
374 parent_class = g_type_class_peek_parent (klass);
376 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_sink_finalize);
377 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_base_sink_set_property);
378 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_base_sink_get_property);
380 /* FIXME, this next value should be configured using an event from the
381 * upstream element, ie, the BUFFER_SIZE event. */
382 g_object_class_install_property (gobject_class, PROP_PREROLL_QUEUE_LEN,
383 g_param_spec_uint ("preroll-queue-len", "Preroll queue length",
384 "Number of buffers to queue during preroll", 0, G_MAXUINT,
385 DEFAULT_PREROLL_QUEUE_LEN,
386 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
388 g_object_class_install_property (gobject_class, PROP_SYNC,
389 g_param_spec_boolean ("sync", "Sync", "Sync on the clock", DEFAULT_SYNC,
390 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
392 g_object_class_install_property (gobject_class, PROP_MAX_LATENESS,
393 g_param_spec_int64 ("max-lateness", "Max Lateness",
394 "Maximum number of nanoseconds that a buffer can be late before it "
395 "is dropped (-1 unlimited)", -1, G_MAXINT64, DEFAULT_MAX_LATENESS,
396 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
398 g_object_class_install_property (gobject_class, PROP_QOS,
399 g_param_spec_boolean ("qos", "Qos",
400 "Generate Quality-of-Service events upstream", DEFAULT_QOS,
401 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
405 * If set to #TRUE, the basesink will perform asynchronous state changes.
406 * When set to #FALSE, the sink will not signal the parent when it prerolls.
407 * Use this option when dealing with sparse streams or when synchronisation is
412 g_object_class_install_property (gobject_class, PROP_ASYNC,
413 g_param_spec_boolean ("async", "Async",
414 "Go asynchronously to PAUSED", DEFAULT_ASYNC,
415 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
417 * GstBaseSink:ts-offset
419 * Controls the final synchronisation, a negative value will render the buffer
420 * earlier while a positive value delays playback. This property can be
421 * used to fix synchronisation in bad files.
425 g_object_class_install_property (gobject_class, PROP_TS_OFFSET,
426 g_param_spec_int64 ("ts-offset", "TS Offset",
427 "Timestamp offset in nanoseconds", G_MININT64, G_MAXINT64,
428 DEFAULT_TS_OFFSET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
430 * GstBaseSink:last-buffer
432 * The last buffer that arrived in the sink and was used for preroll or for
433 * rendering. This property can be used to generate thumbnails. This property
434 * can be NULL when the sink has not yet received a bufer.
438 g_object_class_install_property (gobject_class, PROP_LAST_BUFFER,
439 gst_param_spec_mini_object ("last-buffer", "Last Buffer",
440 "The last buffer received in the sink", GST_TYPE_BUFFER,
441 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
443 * GstBaseSink:blocksize
445 * The amount of bytes to pull when operating in pull mode.
449 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
450 g_param_spec_uint ("blocksize", "Block size",
451 "Size in bytes to pull per buffer (0 = default)", 0, G_MAXUINT,
452 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
454 * GstBaseSink:render-delay
456 * The additional delay between synchronisation and actual rendering of the
457 * media. This property will add additional latency to the device in order to
458 * make other sinks compensate for the delay.
462 g_object_class_install_property (gobject_class, PROP_RENDER_DELAY,
463 g_param_spec_uint64 ("render-delay", "Render Delay",
464 "Additional render delay of the sink in nanoseconds", 0, G_MAXUINT64,
465 DEFAULT_RENDER_DELAY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
467 gstelement_class->change_state =
468 GST_DEBUG_FUNCPTR (gst_base_sink_change_state);
469 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_sink_send_event);
470 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_base_sink_query);
472 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_sink_get_caps);
473 klass->set_caps = GST_DEBUG_FUNCPTR (gst_base_sink_set_caps);
474 klass->buffer_alloc = GST_DEBUG_FUNCPTR (gst_base_sink_buffer_alloc);
475 klass->get_times = GST_DEBUG_FUNCPTR (gst_base_sink_get_times);
476 klass->activate_pull =
477 GST_DEBUG_FUNCPTR (gst_base_sink_default_activate_pull);
481 gst_base_sink_pad_getcaps (GstPad * pad)
483 GstBaseSinkClass *bclass;
485 GstCaps *caps = NULL;
487 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
488 bclass = GST_BASE_SINK_GET_CLASS (bsink);
490 if (bsink->pad_mode == GST_ACTIVATE_PULL) {
491 /* if we are operating in pull mode we only accept the negotiated caps */
492 GST_OBJECT_LOCK (pad);
493 if ((caps = GST_PAD_CAPS (pad)))
495 GST_OBJECT_UNLOCK (pad);
498 if (bclass->get_caps)
499 caps = bclass->get_caps (bsink);
502 GstPadTemplate *pad_template;
505 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass),
507 if (pad_template != NULL) {
508 caps = gst_caps_ref (gst_pad_template_get_caps (pad_template));
512 gst_object_unref (bsink);
518 gst_base_sink_pad_setcaps (GstPad * pad, GstCaps * caps)
520 GstBaseSinkClass *bclass;
524 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
525 bclass = GST_BASE_SINK_GET_CLASS (bsink);
527 if (res && bclass->set_caps)
528 res = bclass->set_caps (bsink, caps);
530 gst_object_unref (bsink);
536 gst_base_sink_pad_fixate (GstPad * pad, GstCaps * caps)
538 GstBaseSinkClass *bclass;
541 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
542 bclass = GST_BASE_SINK_GET_CLASS (bsink);
545 bclass->fixate (bsink, caps);
547 gst_object_unref (bsink);
551 gst_base_sink_pad_buffer_alloc (GstPad * pad, guint64 offset, guint size,
552 GstCaps * caps, GstBuffer ** buf)
554 GstBaseSinkClass *bclass;
556 GstFlowReturn result = GST_FLOW_OK;
558 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
559 bclass = GST_BASE_SINK_GET_CLASS (bsink);
561 if (bclass->buffer_alloc)
562 result = bclass->buffer_alloc (bsink, offset, size, caps, buf);
564 *buf = NULL; /* fallback in gstpad.c will allocate generic buffer */
566 gst_object_unref (bsink);
572 gst_base_sink_init (GstBaseSink * basesink, gpointer g_class)
574 GstPadTemplate *pad_template;
575 GstBaseSinkPrivate *priv;
577 basesink->priv = priv = GST_BASE_SINK_GET_PRIVATE (basesink);
580 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
581 g_return_if_fail (pad_template != NULL);
583 basesink->sinkpad = gst_pad_new_from_template (pad_template, "sink");
585 gst_pad_set_getcaps_function (basesink->sinkpad,
586 GST_DEBUG_FUNCPTR (gst_base_sink_pad_getcaps));
587 gst_pad_set_setcaps_function (basesink->sinkpad,
588 GST_DEBUG_FUNCPTR (gst_base_sink_pad_setcaps));
589 gst_pad_set_fixatecaps_function (basesink->sinkpad,
590 GST_DEBUG_FUNCPTR (gst_base_sink_pad_fixate));
591 gst_pad_set_bufferalloc_function (basesink->sinkpad,
592 GST_DEBUG_FUNCPTR (gst_base_sink_pad_buffer_alloc));
593 gst_pad_set_activate_function (basesink->sinkpad,
594 GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate));
595 gst_pad_set_activatepush_function (basesink->sinkpad,
596 GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate_push));
597 gst_pad_set_activatepull_function (basesink->sinkpad,
598 GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate_pull));
599 gst_pad_set_event_function (basesink->sinkpad,
600 GST_DEBUG_FUNCPTR (gst_base_sink_event));
601 gst_pad_set_chain_function (basesink->sinkpad,
602 GST_DEBUG_FUNCPTR (gst_base_sink_chain));
603 gst_element_add_pad (GST_ELEMENT_CAST (basesink), basesink->sinkpad);
605 basesink->pad_mode = GST_ACTIVATE_NONE;
606 basesink->preroll_queue = g_queue_new ();
607 basesink->abidata.ABI.clip_segment = gst_segment_new ();
608 priv->have_latency = FALSE;
610 basesink->can_activate_push = DEFAULT_CAN_ACTIVATE_PUSH;
611 basesink->can_activate_pull = DEFAULT_CAN_ACTIVATE_PULL;
613 basesink->sync = DEFAULT_SYNC;
614 basesink->abidata.ABI.max_lateness = DEFAULT_MAX_LATENESS;
615 g_atomic_int_set (&priv->qos_enabled, DEFAULT_QOS);
616 priv->async_enabled = DEFAULT_ASYNC;
617 priv->ts_offset = DEFAULT_TS_OFFSET;
618 priv->render_delay = DEFAULT_RENDER_DELAY;
619 priv->blocksize = DEFAULT_BLOCKSIZE;
621 GST_OBJECT_FLAG_SET (basesink, GST_ELEMENT_IS_SINK);
625 gst_base_sink_finalize (GObject * object)
627 GstBaseSink *basesink;
629 basesink = GST_BASE_SINK (object);
631 g_queue_free (basesink->preroll_queue);
632 gst_segment_free (basesink->abidata.ABI.clip_segment);
634 G_OBJECT_CLASS (parent_class)->finalize (object);
638 * gst_base_sink_set_sync:
640 * @sync: the new sync value.
642 * Configures @sink to synchronize on the clock or not. When
643 * @sync is FALSE, incomming samples will be played as fast as
644 * possible. If @sync is TRUE, the timestamps of the incomming
645 * buffers will be used to schedule the exact render time of its
651 gst_base_sink_set_sync (GstBaseSink * sink, gboolean sync)
653 g_return_if_fail (GST_IS_BASE_SINK (sink));
655 GST_OBJECT_LOCK (sink);
657 GST_OBJECT_UNLOCK (sink);
661 * gst_base_sink_get_sync:
664 * Checks if @sink is currently configured to synchronize against the
667 * Returns: TRUE if the sink is configured to synchronize against the clock.
672 gst_base_sink_get_sync (GstBaseSink * sink)
676 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
678 GST_OBJECT_LOCK (sink);
680 GST_OBJECT_UNLOCK (sink);
686 * gst_base_sink_set_max_lateness:
688 * @max_lateness: the new max lateness value.
690 * Sets the new max lateness value to @max_lateness. This value is
691 * used to decide if a buffer should be dropped or not based on the
692 * buffer timestamp and the current clock time. A value of -1 means
698 gst_base_sink_set_max_lateness (GstBaseSink * sink, gint64 max_lateness)
700 g_return_if_fail (GST_IS_BASE_SINK (sink));
702 GST_OBJECT_LOCK (sink);
703 sink->abidata.ABI.max_lateness = max_lateness;
704 GST_OBJECT_UNLOCK (sink);
708 * gst_base_sink_get_max_lateness:
711 * Gets the max lateness value. See gst_base_sink_set_max_lateness for
714 * Returns: The maximum time in nanoseconds that a buffer can be late
715 * before it is dropped and not rendered. A value of -1 means an
721 gst_base_sink_get_max_lateness (GstBaseSink * sink)
725 g_return_val_if_fail (GST_IS_BASE_SINK (sink), -1);
727 GST_OBJECT_LOCK (sink);
728 res = sink->abidata.ABI.max_lateness;
729 GST_OBJECT_UNLOCK (sink);
735 * gst_base_sink_set_qos_enabled:
737 * @enabled: the new qos value.
739 * Configures @sink to send Quality-of-Service events upstream.
744 gst_base_sink_set_qos_enabled (GstBaseSink * sink, gboolean enabled)
746 g_return_if_fail (GST_IS_BASE_SINK (sink));
748 g_atomic_int_set (&sink->priv->qos_enabled, enabled);
752 * gst_base_sink_is_qos_enabled:
755 * Checks if @sink is currently configured to send Quality-of-Service events
758 * Returns: TRUE if the sink is configured to perform Quality-of-Service.
763 gst_base_sink_is_qos_enabled (GstBaseSink * sink)
767 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
769 res = g_atomic_int_get (&sink->priv->qos_enabled);
775 * gst_base_sink_set_async_enabled:
777 * @enabled: the new async value.
779 * Configures @sink to perform all state changes asynchronusly. When async is
780 * disabled, the sink will immediatly go to PAUSED instead of waiting for a
781 * preroll buffer. This feature is usefull if the sink does not synchronize
782 * against the clock or when it is dealing with sparse streams.
787 gst_base_sink_set_async_enabled (GstBaseSink * sink, gboolean enabled)
789 g_return_if_fail (GST_IS_BASE_SINK (sink));
791 GST_PAD_PREROLL_LOCK (sink->sinkpad);
792 sink->priv->async_enabled = enabled;
793 GST_LOG_OBJECT (sink, "set async enabled to %d", enabled);
794 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
798 * gst_base_sink_is_async_enabled:
801 * Checks if @sink is currently configured to perform asynchronous state
804 * Returns: TRUE if the sink is configured to perform asynchronous state
810 gst_base_sink_is_async_enabled (GstBaseSink * sink)
814 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
816 GST_PAD_PREROLL_LOCK (sink->sinkpad);
817 res = sink->priv->async_enabled;
818 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
824 * gst_base_sink_set_ts_offset:
826 * @offset: the new offset
828 * Adjust the synchronisation of @sink with @offset. A negative value will
829 * render buffers earlier than their timestamp. A positive value will delay
830 * rendering. This function can be used to fix playback of badly timestamped
836 gst_base_sink_set_ts_offset (GstBaseSink * sink, GstClockTimeDiff offset)
838 g_return_if_fail (GST_IS_BASE_SINK (sink));
840 GST_OBJECT_LOCK (sink);
841 sink->priv->ts_offset = offset;
842 GST_LOG_OBJECT (sink, "set time offset to %" G_GINT64_FORMAT, offset);
843 GST_OBJECT_UNLOCK (sink);
847 * gst_base_sink_get_ts_offset:
850 * Get the synchronisation offset of @sink.
852 * Returns: The synchronisation offset.
857 gst_base_sink_get_ts_offset (GstBaseSink * sink)
859 GstClockTimeDiff res;
861 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
863 GST_OBJECT_LOCK (sink);
864 res = sink->priv->ts_offset;
865 GST_OBJECT_UNLOCK (sink);
871 * gst_base_sink_get_last_buffer:
874 * Get the last buffer that arrived in the sink and was used for preroll or for
875 * rendering. This property can be used to generate thumbnails.
877 * The #GstCaps on the buffer can be used to determine the type of the buffer.
879 * Returns: a #GstBuffer. gst_buffer_unref() after usage. This function returns
880 * NULL when no buffer has arrived in the sink yet or when the sink is not in
886 gst_base_sink_get_last_buffer (GstBaseSink * sink)
890 g_return_val_if_fail (GST_IS_BASE_SINK (sink), NULL);
892 GST_OBJECT_LOCK (sink);
893 if ((res = sink->priv->last_buffer))
894 gst_buffer_ref (res);
895 GST_OBJECT_UNLOCK (sink);
901 gst_base_sink_set_last_buffer (GstBaseSink * sink, GstBuffer * buffer)
905 GST_OBJECT_LOCK (sink);
906 old = sink->priv->last_buffer;
907 if (G_LIKELY (old != buffer)) {
908 GST_DEBUG_OBJECT (sink, "setting last buffer to %p", buffer);
909 if (G_LIKELY (buffer))
910 gst_buffer_ref (buffer);
911 sink->priv->last_buffer = buffer;
915 GST_OBJECT_UNLOCK (sink);
917 /* avoid unreffing with the lock because cleanup code might want to take the
920 gst_buffer_unref (old);
924 * gst_base_sink_get_latency:
927 * Get the currently configured latency.
929 * Returns: The configured latency.
934 gst_base_sink_get_latency (GstBaseSink * sink)
938 GST_OBJECT_LOCK (sink);
939 res = sink->priv->latency;
940 GST_OBJECT_UNLOCK (sink);
946 * gst_base_sink_query_latency:
948 * @live: if the sink is live
949 * @upstream_live: if an upstream element is live
950 * @min_latency: the min latency of the upstream elements
951 * @max_latency: the max latency of the upstream elements
953 * Query the sink for the latency parameters. The latency will be queried from
954 * the upstream elements. @live will be TRUE if @sink is configured to
955 * synchronize against the clock. @upstream_live will be TRUE if an upstream
958 * If both @live and @upstream_live are TRUE, the sink will want to compensate
959 * for the latency introduced by the upstream elements by setting the
960 * @min_latency to a strictly possitive value.
962 * This function is mostly used by subclasses.
964 * Returns: TRUE if the query succeeded.
969 gst_base_sink_query_latency (GstBaseSink * sink, gboolean * live,
970 gboolean * upstream_live, GstClockTime * min_latency,
971 GstClockTime * max_latency)
973 gboolean l, us_live, res, have_latency;
974 GstClockTime min, max, render_delay;
976 GstClockTime us_min, us_max;
978 /* we are live when we sync to the clock */
979 GST_OBJECT_LOCK (sink);
981 have_latency = sink->priv->have_latency;
982 render_delay = sink->priv->render_delay;
983 GST_OBJECT_UNLOCK (sink);
985 /* assume no latency */
991 GST_DEBUG_OBJECT (sink, "we are ready for LATENCY query");
992 /* we are ready for a latency query this is when we preroll or when we are
994 query = gst_query_new_latency ();
996 /* ask the peer for the latency */
997 if ((res = gst_base_sink_peer_query (sink, query))) {
998 /* get upstream min and max latency */
999 gst_query_parse_latency (query, &us_live, &us_min, &us_max);
1002 /* upstream live, use its latency, subclasses should use these
1003 * values to create the complete latency. */
1008 /* we need to add the render delay if we are live */
1010 min += render_delay;
1012 max += render_delay;
1015 gst_query_unref (query);
1017 GST_DEBUG_OBJECT (sink, "we are not yet ready for LATENCY query");
1021 /* not live, we tried to do the query, if it failed we return TRUE anyway */
1025 GST_DEBUG_OBJECT (sink, "latency query failed but we are not live");
1027 GST_DEBUG_OBJECT (sink, "latency query failed and we are live");
1032 GST_DEBUG_OBJECT (sink, "latency query: live: %d, have_latency %d,"
1033 " upstream: %d, min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT, l,
1034 have_latency, us_live, GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1039 *upstream_live = us_live;
1049 * gst_base_sink_set_render_delay:
1050 * @sink: a #GstBaseSink
1051 * @delay: the new delay
1053 * Set the render delay in @sink to @delay. The render delay is the time
1054 * between actual rendering of a buffer and its synchronisation time. Some
1055 * devices might delay media rendering which can be compensated for with this
1058 * After calling this function, this sink will report additional latency and
1059 * other sinks will adjust their latency to delay the rendering of their media.
1061 * This function is usually called by subclasses.
1066 gst_base_sink_set_render_delay (GstBaseSink * sink, GstClockTime delay)
1068 GstClockTime old_render_delay;
1070 g_return_if_fail (GST_IS_BASE_SINK (sink));
1072 GST_OBJECT_LOCK (sink);
1073 old_render_delay = sink->priv->render_delay;
1074 sink->priv->render_delay = delay;
1075 GST_LOG_OBJECT (sink, "set render delay to %" GST_TIME_FORMAT,
1076 GST_TIME_ARGS (delay));
1077 GST_OBJECT_UNLOCK (sink);
1079 if (delay != old_render_delay) {
1080 GST_DEBUG_OBJECT (sink, "posting latency changed");
1081 gst_element_post_message (GST_ELEMENT_CAST (sink),
1082 gst_message_new_latency (GST_OBJECT_CAST (sink)));
1087 * gst_base_sink_get_render_delay:
1088 * @sink: a #GstBaseSink
1090 * Get the render delay of @sink. see gst_base_sink_set_render_delay() for more
1091 * information about the render delay.
1093 * Returns: the render delay of @sink.
1098 gst_base_sink_get_render_delay (GstBaseSink * sink)
1100 GstClockTimeDiff res;
1102 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1104 GST_OBJECT_LOCK (sink);
1105 res = sink->priv->render_delay;
1106 GST_OBJECT_UNLOCK (sink);
1112 * gst_base_sink_set_blocksize:
1113 * @sink: a #GstBaseSink
1114 * @blocksize: the blocksize in bytes
1116 * Set the number of bytes that the sink will pull when it is operating in pull
1122 gst_base_sink_set_blocksize (GstBaseSink * sink, guint blocksize)
1124 g_return_if_fail (GST_IS_BASE_SINK (sink));
1126 GST_OBJECT_LOCK (sink);
1127 sink->priv->blocksize = blocksize;
1128 GST_LOG_OBJECT (sink, "set blocksize to %u", blocksize);
1129 GST_OBJECT_UNLOCK (sink);
1133 * gst_base_sink_get_blocksize:
1134 * @sink: a #GstBaseSink
1136 * Get the number of bytes that the sink will pull when it is operating in pull
1139 * Returns: the number of bytes @sink will pull in pull mode.
1144 gst_base_sink_get_blocksize (GstBaseSink * sink)
1148 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1150 GST_OBJECT_LOCK (sink);
1151 res = sink->priv->blocksize;
1152 GST_OBJECT_UNLOCK (sink);
1158 gst_base_sink_set_property (GObject * object, guint prop_id,
1159 const GValue * value, GParamSpec * pspec)
1161 GstBaseSink *sink = GST_BASE_SINK (object);
1164 case PROP_PREROLL_QUEUE_LEN:
1165 /* preroll lock necessary to serialize with finish_preroll */
1166 GST_PAD_PREROLL_LOCK (sink->sinkpad);
1167 sink->preroll_queue_max_len = g_value_get_uint (value);
1168 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
1171 gst_base_sink_set_sync (sink, g_value_get_boolean (value));
1173 case PROP_MAX_LATENESS:
1174 gst_base_sink_set_max_lateness (sink, g_value_get_int64 (value));
1177 gst_base_sink_set_qos_enabled (sink, g_value_get_boolean (value));
1180 gst_base_sink_set_async_enabled (sink, g_value_get_boolean (value));
1182 case PROP_TS_OFFSET:
1183 gst_base_sink_set_ts_offset (sink, g_value_get_int64 (value));
1185 case PROP_BLOCKSIZE:
1186 gst_base_sink_set_blocksize (sink, g_value_get_uint (value));
1188 case PROP_RENDER_DELAY:
1189 gst_base_sink_set_render_delay (sink, g_value_get_uint64 (value));
1192 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1198 gst_base_sink_get_property (GObject * object, guint prop_id, GValue * value,
1201 GstBaseSink *sink = GST_BASE_SINK (object);
1204 case PROP_PREROLL_QUEUE_LEN:
1205 GST_PAD_PREROLL_LOCK (sink->sinkpad);
1206 g_value_set_uint (value, sink->preroll_queue_max_len);
1207 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
1210 g_value_set_boolean (value, gst_base_sink_get_sync (sink));
1212 case PROP_MAX_LATENESS:
1213 g_value_set_int64 (value, gst_base_sink_get_max_lateness (sink));
1216 g_value_set_boolean (value, gst_base_sink_is_qos_enabled (sink));
1219 g_value_set_boolean (value, gst_base_sink_is_async_enabled (sink));
1221 case PROP_TS_OFFSET:
1222 g_value_set_int64 (value, gst_base_sink_get_ts_offset (sink));
1224 case PROP_LAST_BUFFER:
1225 gst_value_take_buffer (value, gst_base_sink_get_last_buffer (sink));
1227 case PROP_BLOCKSIZE:
1228 g_value_set_uint (value, gst_base_sink_get_blocksize (sink));
1230 case PROP_RENDER_DELAY:
1231 g_value_set_uint64 (value, gst_base_sink_get_render_delay (sink));
1234 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1241 gst_base_sink_get_caps (GstBaseSink * sink)
1247 gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps)
1252 static GstFlowReturn
1253 gst_base_sink_buffer_alloc (GstBaseSink * sink, guint64 offset, guint size,
1254 GstCaps * caps, GstBuffer ** buf)
1260 /* with PREROLL_LOCK, STREAM_LOCK */
1262 gst_base_sink_preroll_queue_flush (GstBaseSink * basesink, GstPad * pad)
1266 GST_DEBUG_OBJECT (basesink, "flushing queue %p", basesink);
1267 while ((obj = g_queue_pop_head (basesink->preroll_queue))) {
1268 GST_DEBUG_OBJECT (basesink, "popped %p", obj);
1269 gst_mini_object_unref (obj);
1271 /* we can't have EOS anymore now */
1272 basesink->eos = FALSE;
1273 basesink->priv->received_eos = FALSE;
1274 basesink->have_preroll = FALSE;
1275 basesink->eos_queued = FALSE;
1276 basesink->preroll_queued = 0;
1277 basesink->buffers_queued = 0;
1278 basesink->events_queued = 0;
1279 /* can't report latency anymore until we preroll again */
1280 if (basesink->priv->async_enabled) {
1281 GST_OBJECT_LOCK (basesink);
1282 basesink->priv->have_latency = FALSE;
1283 GST_OBJECT_UNLOCK (basesink);
1285 /* and signal any waiters now */
1286 GST_PAD_PREROLL_SIGNAL (pad);
1289 /* with STREAM_LOCK, configures given segment with the event information. */
1291 gst_base_sink_configure_segment (GstBaseSink * basesink, GstPad * pad,
1292 GstEvent * event, GstSegment * segment)
1295 gdouble rate, arate;
1301 /* the newsegment event is needed to bring the buffer timestamps to the
1302 * stream time and to drop samples outside of the playback segment. */
1303 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1304 &start, &stop, &time);
1306 /* The segment is protected with both the STREAM_LOCK and the OBJECT_LOCK.
1307 * We protect with the OBJECT_LOCK so that we can use the values to
1308 * safely answer a POSITION query. */
1309 GST_OBJECT_LOCK (basesink);
1310 gst_segment_set_newsegment_full (segment, update, rate, arate, format, start,
1313 if (format == GST_FORMAT_TIME) {
1314 GST_DEBUG_OBJECT (basesink,
1315 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1316 "format GST_FORMAT_TIME, "
1317 "%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
1318 ", time %" GST_TIME_FORMAT ", accum %" GST_TIME_FORMAT,
1319 update, rate, arate, GST_TIME_ARGS (segment->start),
1320 GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time),
1321 GST_TIME_ARGS (segment->accum));
1323 GST_DEBUG_OBJECT (basesink,
1324 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1326 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
1327 G_GINT64_FORMAT ", accum %" G_GINT64_FORMAT, update, rate, arate,
1328 segment->format, segment->start, segment->stop, segment->time,
1331 GST_OBJECT_UNLOCK (basesink);
1334 /* with PREROLL_LOCK, STREAM_LOCK */
1336 gst_base_sink_commit_state (GstBaseSink * basesink)
1338 /* commit state and proceed to next pending state */
1339 GstState current, next, pending, post_pending;
1340 gboolean post_paused = FALSE;
1341 gboolean post_async_done = FALSE;
1342 gboolean post_playing = FALSE;
1344 /* we are certainly not playing async anymore now */
1345 basesink->playing_async = FALSE;
1347 GST_OBJECT_LOCK (basesink);
1348 current = GST_STATE (basesink);
1349 next = GST_STATE_NEXT (basesink);
1350 pending = GST_STATE_PENDING (basesink);
1351 post_pending = pending;
1354 case GST_STATE_PLAYING:
1356 GstBaseSinkClass *bclass;
1357 GstStateChangeReturn ret;
1359 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1361 GST_DEBUG_OBJECT (basesink, "commiting state to PLAYING");
1363 basesink->need_preroll = FALSE;
1364 post_async_done = TRUE;
1365 basesink->priv->commited = TRUE;
1366 post_playing = TRUE;
1367 /* post PAUSED too when we were READY */
1368 if (current == GST_STATE_READY) {
1372 /* make sure we notify the subclass of async playing */
1373 if (bclass->async_play) {
1374 GST_WARNING_OBJECT (basesink, "deprecated async_play");
1375 ret = bclass->async_play (basesink);
1376 if (ret == GST_STATE_CHANGE_FAILURE)
1381 case GST_STATE_PAUSED:
1382 GST_DEBUG_OBJECT (basesink, "commiting state to PAUSED");
1384 post_async_done = TRUE;
1385 basesink->priv->commited = TRUE;
1386 post_pending = GST_STATE_VOID_PENDING;
1388 case GST_STATE_READY:
1389 case GST_STATE_NULL:
1391 case GST_STATE_VOID_PENDING:
1392 goto nothing_pending;
1397 /* we can report latency queries now */
1398 basesink->priv->have_latency = TRUE;
1400 GST_STATE (basesink) = pending;
1401 GST_STATE_NEXT (basesink) = GST_STATE_VOID_PENDING;
1402 GST_STATE_PENDING (basesink) = GST_STATE_VOID_PENDING;
1403 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_SUCCESS;
1404 GST_OBJECT_UNLOCK (basesink);
1407 GST_DEBUG_OBJECT (basesink, "posting PAUSED state change message");
1408 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1409 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1410 current, next, post_pending));
1412 if (post_async_done) {
1413 GST_DEBUG_OBJECT (basesink, "posting async-done message");
1414 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1415 gst_message_new_async_done (GST_OBJECT_CAST (basesink)));
1418 GST_DEBUG_OBJECT (basesink, "posting PLAYING state change message");
1419 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1420 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1421 next, pending, GST_STATE_VOID_PENDING));
1424 GST_STATE_BROADCAST (basesink);
1430 /* Depending on the state, set our vars. We get in this situation when the
1431 * state change function got a change to update the state vars before the
1432 * streaming thread did. This is fine but we need to make sure that we
1433 * update the need_preroll var since it was TRUE when we got here and might
1434 * become FALSE if we got to PLAYING. */
1435 GST_DEBUG_OBJECT (basesink, "nothing to commit, now in %s",
1436 gst_element_state_get_name (current));
1438 case GST_STATE_PLAYING:
1439 basesink->need_preroll = FALSE;
1441 case GST_STATE_PAUSED:
1442 basesink->need_preroll = TRUE;
1445 basesink->need_preroll = FALSE;
1446 basesink->flushing = TRUE;
1449 /* we can report latency queries now */
1450 basesink->priv->have_latency = TRUE;
1451 GST_OBJECT_UNLOCK (basesink);
1456 /* app is going to READY */
1457 GST_DEBUG_OBJECT (basesink, "stopping");
1458 basesink->need_preroll = FALSE;
1459 basesink->flushing = TRUE;
1460 GST_OBJECT_UNLOCK (basesink);
1465 GST_DEBUG_OBJECT (basesink, "async commit failed");
1466 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_FAILURE;
1467 GST_OBJECT_UNLOCK (basesink);
1473 /* with STREAM_LOCK, PREROLL_LOCK
1475 * Returns TRUE if the object needs synchronisation and takes therefore
1476 * part in prerolling.
1478 * rsstart/rsstop contain the start/stop in stream time.
1479 * rrstart/rrstop contain the start/stop in running time.
1482 gst_base_sink_get_sync_times (GstBaseSink * basesink, GstMiniObject * obj,
1483 GstClockTime * rsstart, GstClockTime * rsstop,
1484 GstClockTime * rrstart, GstClockTime * rrstop, gboolean * do_sync,
1485 GstSegment * segment)
1487 GstBaseSinkClass *bclass;
1489 GstClockTime start, stop; /* raw start/stop timestamps */
1490 gint64 cstart, cstop; /* clipped raw timestamps */
1491 gint64 rstart, rstop; /* clipped timestamps converted to running time */
1492 GstClockTime sstart, sstop; /* clipped timestamps converted to stream time */
1494 GstBaseSinkPrivate *priv;
1496 priv = basesink->priv;
1498 /* start with nothing */
1501 if (G_UNLIKELY (GST_IS_EVENT (obj))) {
1502 GstEvent *event = GST_EVENT_CAST (obj);
1504 switch (GST_EVENT_TYPE (event)) {
1505 /* EOS event needs syncing */
1508 if (basesink->segment.rate >= 0.0) {
1509 sstart = sstop = priv->current_sstop;
1511 /* we have not seen a buffer yet, use the segment values */
1512 sstart = sstop = gst_segment_to_stream_time (&basesink->segment,
1513 basesink->segment.format, basesink->segment.stop);
1516 sstart = sstop = priv->current_sstart;
1518 /* we have not seen a buffer yet, use the segment values */
1519 sstart = sstop = gst_segment_to_stream_time (&basesink->segment,
1520 basesink->segment.format, basesink->segment.start);
1524 rstart = rstop = priv->eos_rtime;
1525 *do_sync = rstart != -1;
1526 GST_DEBUG_OBJECT (basesink, "sync times for EOS %" GST_TIME_FORMAT,
1527 GST_TIME_ARGS (rstart));
1531 /* other events do not need syncing */
1532 /* FIXME, maybe NEWSEGMENT might need synchronisation
1533 * since the POSITION query depends on accumulated times and
1534 * we cannot accumulate the current segment before the previous
1541 /* else do buffer sync code */
1542 buffer = GST_BUFFER_CAST (obj);
1544 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1546 /* just get the times to see if we need syncing */
1547 if (bclass->get_times)
1548 bclass->get_times (basesink, buffer, &start, &stop);
1551 gst_base_sink_get_times (basesink, buffer, &start, &stop);
1557 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
1558 ", stop: %" GST_TIME_FORMAT ", do_sync %d", GST_TIME_ARGS (start),
1559 GST_TIME_ARGS (stop), *do_sync);
1561 /* collect segment and format for code clarity */
1562 format = segment->format;
1564 /* no timestamp clipping if we did not * get a TIME segment format */
1565 if (G_UNLIKELY (format != GST_FORMAT_TIME)) {
1568 /* do running and stream time in TIME format */
1569 format = GST_FORMAT_TIME;
1574 if (G_UNLIKELY (!gst_segment_clip (segment, GST_FORMAT_TIME,
1575 (gint64) start, (gint64) stop, &cstart, &cstop)))
1576 goto out_of_segment;
1578 if (G_UNLIKELY (start != cstart || stop != cstop)) {
1579 GST_DEBUG_OBJECT (basesink, "clipped to: start %" GST_TIME_FORMAT
1580 ", stop: %" GST_TIME_FORMAT, GST_TIME_ARGS (cstart),
1581 GST_TIME_ARGS (cstop));
1584 /* set last stop position */
1585 if (G_LIKELY (cstop != GST_CLOCK_TIME_NONE))
1586 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, cstop);
1588 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, cstart);
1591 /* this can produce wrong values if we accumulated non-TIME segments. If this happens,
1592 * upstream is behaving very badly */
1593 sstart = gst_segment_to_stream_time (segment, format, cstart);
1594 sstop = gst_segment_to_stream_time (segment, format, cstop);
1595 rstart = gst_segment_to_running_time (segment, format, cstart);
1596 rstop = gst_segment_to_running_time (segment, format, cstop);
1605 /* buffers and EOS always need syncing and preroll */
1611 /* should not happen since we clip them in the chain function already,
1612 * we return FALSE so that we don't try to sync on it. */
1613 GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
1614 (NULL), ("unexpected buffer out of segment found."));
1615 GST_LOG_OBJECT (basesink, "buffer skipped, not in segment");
1620 /* with STREAM_LOCK, PREROLL_LOCK, LOCK
1621 * adjust a timestamp with the latency and timestamp offset */
1623 gst_base_sink_adjust_time (GstBaseSink * basesink, GstClockTime time)
1625 GstClockTimeDiff ts_offset;
1627 /* don't do anything funny with invalid timestamps */
1628 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
1631 time += basesink->priv->latency;
1633 /* apply offset, be carefull for underflows */
1634 ts_offset = basesink->priv->ts_offset;
1635 if (ts_offset < 0) {
1636 ts_offset = -ts_offset;
1637 if (ts_offset < time)
1648 * gst_base_sink_wait_clock:
1650 * @time: the running_time to be reached
1651 * @jitter: the jitter to be filled with time diff (can be NULL)
1653 * This function will block until @time is reached. It is usually called by
1654 * subclasses that use their own internal synchronisation.
1656 * If @time is not valid, no sycnhronisation is done and #GST_CLOCK_BADTIME is
1657 * returned. Likewise, if synchronisation is disabled in the element or there
1658 * is no clock, no synchronisation is done and #GST_CLOCK_BADTIME is returned.
1660 * This function should only be called with the PREROLL_LOCK held, like when
1661 * receiving an EOS event in the ::event vmethod or when receiving a buffer in
1662 * the ::render vmethod.
1664 * The @time argument should be the running_time of when this method should
1665 * return and is not adjusted with any latency or offset configured in the
1670 * Returns: #GstClockReturn
1673 gst_base_sink_wait_clock (GstBaseSink * sink, GstClockTime time,
1674 GstClockTimeDiff * jitter)
1680 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
1683 GST_OBJECT_LOCK (sink);
1684 if (G_UNLIKELY (!sink->sync))
1687 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (sink)) == NULL))
1690 /* add base_time to running_time to get the time against the clock */
1691 time += GST_ELEMENT_CAST (sink)->base_time;
1693 id = gst_clock_new_single_shot_id (clock, time);
1694 GST_OBJECT_UNLOCK (sink);
1696 /* A blocking wait is performed on the clock. We save the ClockID
1697 * so we can unlock the entry at any time. While we are blocking, we
1698 * release the PREROLL_LOCK so that other threads can interrupt the
1700 sink->clock_id = id;
1701 /* release the preroll lock while waiting */
1702 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
1704 ret = gst_clock_id_wait (id, jitter);
1706 GST_PAD_PREROLL_LOCK (sink->sinkpad);
1707 gst_clock_id_unref (id);
1708 sink->clock_id = NULL;
1712 /* no syncing needed */
1715 GST_DEBUG_OBJECT (sink, "time not valid, no sync needed");
1716 return GST_CLOCK_BADTIME;
1720 GST_DEBUG_OBJECT (sink, "sync disabled");
1721 GST_OBJECT_UNLOCK (sink);
1722 return GST_CLOCK_BADTIME;
1726 GST_DEBUG_OBJECT (sink, "no clock, can't sync");
1727 GST_OBJECT_UNLOCK (sink);
1728 return GST_CLOCK_BADTIME;
1733 * gst_base_sink_wait_preroll:
1736 * If the #GstBaseSinkClass::render method performs its own synchronisation against
1737 * the clock it must unblock when going from PLAYING to the PAUSED state and call
1738 * this method before continuing to render the remaining data.
1740 * This function will block until a state change to PLAYING happens (in which
1741 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
1742 * to a state change to READY or a FLUSH event (in which case this function
1743 * returns #GST_FLOW_WRONG_STATE).
1745 * This function should only be called with the PREROLL_LOCK held, like in the
1750 * Returns: #GST_FLOW_OK if the preroll completed and processing can
1751 * continue. Any other return value should be returned from the render vmethod.
1754 gst_base_sink_wait_preroll (GstBaseSink * sink)
1756 sink->have_preroll = TRUE;
1757 GST_DEBUG_OBJECT (sink, "waiting in preroll for flush or PLAYING");
1758 /* block until the state changes, or we get a flush, or something */
1759 GST_PAD_PREROLL_WAIT (sink->sinkpad);
1760 sink->have_preroll = FALSE;
1761 if (G_UNLIKELY (sink->flushing))
1763 GST_DEBUG_OBJECT (sink, "continue after preroll");
1770 GST_DEBUG_OBJECT (sink, "preroll interrupted");
1771 return GST_FLOW_WRONG_STATE;
1776 * gst_base_sink_do_preroll:
1778 * @obj: the object that caused the preroll
1780 * If the @sink spawns its own thread for pulling buffers from upstream it
1781 * should call this method after it has pulled a buffer. If the element needed
1782 * to preroll, this function will perform the preroll and will then block
1783 * until the element state is changed.
1785 * This function should be called with the PREROLL_LOCK held.
1789 * Returns: #GST_FLOW_OK if the preroll completed and processing can
1790 * continue. Any other return value should be returned from the render vmethod.
1793 gst_base_sink_do_preroll (GstBaseSink * sink, GstMiniObject * obj)
1797 while (G_UNLIKELY (sink->need_preroll)) {
1798 GST_DEBUG_OBJECT (sink, "prerolling object %p", obj);
1800 ret = gst_base_sink_preroll_object (sink, obj);
1801 if (ret != GST_FLOW_OK)
1802 goto preroll_failed;
1804 /* need to recheck here because the commit state could have
1805 * made us not need the preroll anymore */
1806 if (G_LIKELY (sink->need_preroll)) {
1807 /* block until the state changes, or we get a flush, or something */
1808 ret = gst_base_sink_wait_preroll (sink);
1809 if (ret != GST_FLOW_OK)
1810 goto preroll_failed;
1818 GST_DEBUG_OBJECT (sink, "preroll failed %d", ret);
1824 * gst_base_sink_wait_eos:
1826 * @time: the running_time to be reached
1827 * @jitter: the jitter to be filled with time diff (can be NULL)
1829 * This function will block until @time is reached. It is usually called by
1830 * subclasses that use their own internal synchronisation but want to let the
1831 * EOS be handled by the base class.
1833 * This function should only be called with the PREROLL_LOCK held, like when
1834 * receiving an EOS event in the ::event vmethod.
1836 * The @time argument should be the running_time of when the EOS should happen
1837 * and will be adjusted with any latency and offset configured in the sink.
1841 * Returns: #GstFlowReturn
1844 gst_base_sink_wait_eos (GstBaseSink * sink, GstClockTime time,
1845 GstClockTimeDiff * jitter)
1847 GstClockReturn status;
1853 GST_DEBUG_OBJECT (sink, "checking preroll");
1855 /* first wait for the playing state before we can continue */
1856 if (G_UNLIKELY (sink->need_preroll)) {
1857 ret = gst_base_sink_wait_preroll (sink);
1858 if (ret != GST_FLOW_OK)
1862 /* preroll done, we can sync since we are in PLAYING now. */
1863 GST_DEBUG_OBJECT (sink, "possibly waiting for clock to reach %"
1864 GST_TIME_FORMAT, GST_TIME_ARGS (time));
1866 /* compensate for latency and ts_offset. We don't adjust for render delay
1867 * because we don't interact with the device on EOS normally. */
1868 stime = gst_base_sink_adjust_time (sink, time);
1870 /* wait for the clock, this can be interrupted because we got shut down or
1872 status = gst_base_sink_wait_clock (sink, stime, jitter);
1874 GST_DEBUG_OBJECT (sink, "clock returned %d", status);
1876 /* invalid time, no clock or sync disabled, just continue then */
1877 if (status == GST_CLOCK_BADTIME)
1880 /* waiting could have been interrupted and we can be flushing now */
1881 if (G_UNLIKELY (sink->flushing))
1884 /* retry if we got unscheduled, which means we did not reach the timeout
1885 * yet. if some other error occures, we continue. */
1886 } while (status == GST_CLOCK_UNSCHEDULED);
1888 GST_DEBUG_OBJECT (sink, "end of stream");
1895 GST_DEBUG_OBJECT (sink, "we are flushing");
1896 return GST_FLOW_WRONG_STATE;
1901 handle_stepping (GstBaseSink * basesink)
1903 GstBaseSinkPrivate *priv;
1905 priv = basesink->priv;
1907 /* see if we need to skip this buffer because of stepping */
1908 if (priv->step.remaining != -1) {
1909 GstMessage *message;
1914 switch (step->format) {
1915 case GST_FORMAT_TIME:
1916 GST_DEBUG_OBJECT (basesink,
1917 "got time step %" GST_TIME_FORMAT "/%" GST_TIME_FORMAT,
1918 GST_TIME_ARGS (step->remaining), GST_TIME_ARGS (step->amount));
1920 case GST_FORMAT_BUFFERS:
1921 GST_DEBUG_OBJECT (basesink,
1922 "got default step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1923 step->remaining, step->amount);
1924 if (step->remaining > 0) {
1929 case GST_FORMAT_DEFAULT:
1931 GST_DEBUG_OBJECT (basesink,
1932 "got unknown step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1933 step->remaining, step->amount);
1936 GST_DEBUG_OBJECT (basesink, "step complete");
1939 gst_message_new_step_done (GST_OBJECT_CAST (basesink), step->format,
1940 step->amount, step->rate, step->duration, step->intermediate);
1941 gst_message_set_seqnum (message, step->seqnum);
1942 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
1944 step->remaining = -1;
1945 if (!step->intermediate)
1946 basesink->need_preroll = step->need_preroll;
1952 /* with STREAM_LOCK, PREROLL_LOCK
1954 * Make sure we are in PLAYING and synchronize an object to the clock.
1956 * If we need preroll, we are not in PLAYING. We try to commit the state
1957 * if needed and then block if we still are not PLAYING.
1959 * We start waiting on the clock in PLAYING. If we got interrupted, we
1960 * immediatly try to re-preroll.
1962 * Some objects do not need synchronisation (most events) and so this function
1963 * immediatly returns GST_FLOW_OK.
1965 * for objects that arrive later than max-lateness to be synchronized to the
1966 * clock have the @late boolean set to TRUE.
1968 * This function keeps a running average of the jitter (the diff between the
1969 * clock time and the requested sync time). The jitter is negative for
1970 * objects that arrive in time and positive for late buffers.
1972 * does not take ownership of obj.
1974 static GstFlowReturn
1975 gst_base_sink_do_sync (GstBaseSink * basesink, GstPad * pad,
1976 GstMiniObject * obj, gboolean * late)
1978 GstClockTimeDiff jitter;
1980 GstClockReturn status = GST_CLOCK_OK;
1981 GstClockTime rstart, rstop, sstart, sstop, stime;
1983 GstBaseSinkPrivate *priv;
1987 priv = basesink->priv;
1990 sstart = sstop = rstart = rstop = -1;
1993 priv->current_rstart = -1;
1995 /* get timing information for this object against the render segment */
1996 syncable = gst_base_sink_get_sync_times (basesink, obj,
1997 &sstart, &sstop, &rstart, &rstop, &do_sync, &basesink->segment);
1999 /* a syncable object needs to participate in preroll and
2000 * clocking. All buffers and EOS are syncable. */
2001 if (G_UNLIKELY (!syncable))
2004 /* see if we need to skip this buffer as part of stepping */
2005 if (!handle_stepping (basesink))
2008 /* store timing info for current object */
2009 priv->current_rstart = rstart;
2010 priv->current_rstop = (rstop != -1 ? rstop : rstart);
2012 /* save sync time for eos when the previous object needed sync */
2013 priv->eos_rtime = (do_sync ? priv->current_rstop : -1);
2015 step_cookie = priv->step.cookie;
2018 /* first do preroll, this makes sure we commit our state
2019 * to PAUSED and can continue to PLAYING. We cannot perform
2020 * any clock sync in PAUSED because there is no clock. */
2021 ret = gst_base_sink_do_preroll (basesink, obj);
2022 if (G_UNLIKELY (ret != GST_FLOW_OK))
2023 goto preroll_failed;
2025 if (step_cookie != priv->step.cookie)
2028 /* After rendering we store the position of the last buffer so that we can use
2029 * it to report the position. We need to take the lock here. */
2030 GST_OBJECT_LOCK (basesink);
2031 priv->current_sstart = sstart;
2032 priv->current_sstop = (sstop != -1 ? sstop : sstart);
2033 GST_OBJECT_UNLOCK (basesink);
2038 /* adjust for latency */
2039 stime = gst_base_sink_adjust_time (basesink, rstart);
2041 /* adjust for render-delay, avoid underflows */
2043 if (stime > priv->render_delay)
2044 stime -= priv->render_delay;
2049 /* preroll done, we can sync since we are in PLAYING now. */
2050 GST_DEBUG_OBJECT (basesink, "possibly waiting for clock to reach %"
2051 GST_TIME_FORMAT ", adjusted %" GST_TIME_FORMAT,
2052 GST_TIME_ARGS (rstart), GST_TIME_ARGS (stime));
2054 /* This function will return immediatly if start == -1, no clock
2055 * or sync is disabled with GST_CLOCK_BADTIME. */
2056 status = gst_base_sink_wait_clock (basesink, stime, &jitter);
2058 GST_DEBUG_OBJECT (basesink, "clock returned %d", status);
2060 /* invalid time, no clock or sync disabled, just render */
2061 if (status == GST_CLOCK_BADTIME)
2064 /* waiting could have been interrupted and we can be flushing now */
2065 if (G_UNLIKELY (basesink->flushing))
2068 /* check for unlocked by a state change, we are not flushing so
2069 * we can try to preroll on the current buffer. */
2070 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
2071 GST_DEBUG_OBJECT (basesink, "unscheduled, waiting some more");
2072 priv->call_preroll = TRUE;
2076 /* successful syncing done, record observation */
2077 priv->current_jitter = jitter;
2079 /* check if the object should be dropped */
2080 *late = gst_base_sink_is_too_late (basesink, obj, rstart, rstop,
2089 GST_DEBUG_OBJECT (basesink, "skipped stepped object %p", obj);
2094 GST_DEBUG_OBJECT (basesink, "non syncable object %p", obj);
2099 GST_DEBUG_OBJECT (basesink, "we are flushing");
2100 return GST_FLOW_WRONG_STATE;
2104 GST_DEBUG_OBJECT (basesink, "preroll failed");
2110 gst_base_sink_send_qos (GstBaseSink * basesink,
2111 gdouble proportion, GstClockTime time, GstClockTimeDiff diff)
2116 /* generate Quality-of-Service event */
2117 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2118 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2119 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (time));
2121 event = gst_event_new_qos (proportion, diff, time);
2124 res = gst_pad_push_event (basesink->sinkpad, event);
2130 gst_base_sink_perform_qos (GstBaseSink * sink, gboolean dropped)
2132 GstBaseSinkPrivate *priv;
2133 GstClockTime start, stop;
2134 GstClockTimeDiff jitter;
2135 GstClockTime pt, entered, left;
2136 GstClockTime duration;
2141 start = priv->current_rstart;
2143 /* if Quality-of-Service disabled, do nothing */
2144 if (!g_atomic_int_get (&priv->qos_enabled) || start == -1)
2147 stop = priv->current_rstop;
2148 jitter = priv->current_jitter;
2151 /* this is the time the buffer entered the sink */
2152 if (start < -jitter)
2155 entered = start + jitter;
2158 /* this is the time the buffer entered the sink */
2159 entered = start + jitter;
2160 /* this is the time the buffer left the sink */
2161 left = start + jitter;
2164 /* calculate duration of the buffer */
2166 duration = stop - start;
2170 /* if we have the time when the last buffer left us, calculate
2171 * processing time */
2172 if (priv->last_left != -1) {
2173 if (entered > priv->last_left) {
2174 pt = entered - priv->last_left;
2182 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "start: %" GST_TIME_FORMAT
2183 ", entered %" GST_TIME_FORMAT ", left %" GST_TIME_FORMAT ", pt: %"
2184 GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT ",jitter %"
2185 G_GINT64_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (entered),
2186 GST_TIME_ARGS (left), GST_TIME_ARGS (pt), GST_TIME_ARGS (duration),
2189 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "avg_duration: %" GST_TIME_FORMAT
2190 ", avg_pt: %" GST_TIME_FORMAT ", avg_rate: %g",
2191 GST_TIME_ARGS (priv->avg_duration), GST_TIME_ARGS (priv->avg_pt),
2194 /* collect running averages. for first observations, we copy the
2196 if (priv->avg_duration == -1)
2197 priv->avg_duration = duration;
2199 priv->avg_duration = UPDATE_RUNNING_AVG (priv->avg_duration, duration);
2201 if (priv->avg_pt == -1)
2204 priv->avg_pt = UPDATE_RUNNING_AVG (priv->avg_pt, pt);
2206 if (priv->avg_duration != 0)
2208 gst_guint64_to_gdouble (priv->avg_pt) /
2209 gst_guint64_to_gdouble (priv->avg_duration);
2213 if (priv->last_left != -1) {
2214 if (dropped || priv->avg_rate < 0.0) {
2215 priv->avg_rate = rate;
2218 priv->avg_rate = UPDATE_RUNNING_AVG_N (priv->avg_rate, rate);
2220 priv->avg_rate = UPDATE_RUNNING_AVG_P (priv->avg_rate, rate);
2224 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink,
2225 "updated: avg_duration: %" GST_TIME_FORMAT ", avg_pt: %" GST_TIME_FORMAT
2226 ", avg_rate: %g", GST_TIME_ARGS (priv->avg_duration),
2227 GST_TIME_ARGS (priv->avg_pt), priv->avg_rate);
2230 if (priv->avg_rate >= 0.0) {
2231 /* if we have a valid rate, start sending QoS messages */
2232 if (priv->current_jitter < 0) {
2233 /* make sure we never go below 0 when adding the jitter to the
2235 if (priv->current_rstart < -priv->current_jitter)
2236 priv->current_jitter = -priv->current_rstart;
2238 gst_base_sink_send_qos (sink, priv->avg_rate, priv->current_rstart,
2239 priv->current_jitter);
2242 /* record when this buffer will leave us */
2243 priv->last_left = left;
2246 /* reset all qos measuring */
2248 gst_base_sink_reset_qos (GstBaseSink * sink)
2250 GstBaseSinkPrivate *priv;
2254 priv->last_in_time = -1;
2255 priv->last_left = -1;
2256 priv->avg_duration = -1;
2258 priv->avg_rate = -1.0;
2259 priv->avg_render = -1;
2265 /* Checks if the object was scheduled too late.
2267 * start/stop contain the raw timestamp start and stop values
2270 * status and jitter contain the return values from the clock wait.
2272 * returns TRUE if the buffer was too late.
2275 gst_base_sink_is_too_late (GstBaseSink * basesink, GstMiniObject * obj,
2276 GstClockTime start, GstClockTime stop,
2277 GstClockReturn status, GstClockTimeDiff jitter)
2280 gint64 max_lateness;
2281 GstBaseSinkPrivate *priv;
2283 priv = basesink->priv;
2287 /* only for objects that were too late */
2288 if (G_LIKELY (status != GST_CLOCK_EARLY))
2291 max_lateness = basesink->abidata.ABI.max_lateness;
2293 /* check if frame dropping is enabled */
2294 if (max_lateness == -1)
2297 /* only check for buffers */
2298 if (G_UNLIKELY (!GST_IS_BUFFER (obj)))
2301 /* can't do check if we don't have a timestamp */
2302 if (G_UNLIKELY (start == -1))
2305 /* we can add a valid stop time */
2307 max_lateness += stop;
2309 max_lateness += start;
2311 /* if the jitter bigger than duration and lateness we are too late */
2312 if ((late = start + jitter > max_lateness)) {
2313 GST_DEBUG_OBJECT (basesink, "buffer is too late %" GST_TIME_FORMAT
2314 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (start + jitter),
2315 GST_TIME_ARGS (max_lateness));
2316 /* !!emergency!!, if we did not receive anything valid for more than a
2317 * second, render it anyway so the user sees something */
2318 if (priv->last_in_time != -1 && start - priv->last_in_time > GST_SECOND) {
2320 GST_DEBUG_OBJECT (basesink,
2321 "**emergency** last buffer at %" GST_TIME_FORMAT " > GST_SECOND",
2322 GST_TIME_ARGS (priv->last_in_time));
2328 priv->last_in_time = start;
2335 GST_DEBUG_OBJECT (basesink, "object was scheduled in time");
2340 GST_DEBUG_OBJECT (basesink, "frame dropping disabled");
2345 GST_DEBUG_OBJECT (basesink, "object is not a buffer");
2350 GST_DEBUG_OBJECT (basesink, "buffer has no timestamp");
2355 /* called before and after calling the render vmethod. It keeps track of how
2356 * much time was spent in the render method and is used to check if we are
2359 gst_base_sink_do_render_stats (GstBaseSink * basesink, gboolean start)
2361 GstBaseSinkPrivate *priv;
2363 priv = basesink->priv;
2366 priv->start = gst_util_get_timestamp ();
2368 GstClockTime elapsed;
2370 priv->stop = gst_util_get_timestamp ();
2372 elapsed = GST_CLOCK_DIFF (priv->start, priv->stop);
2374 if (priv->avg_render == -1)
2375 priv->avg_render = elapsed;
2377 priv->avg_render = UPDATE_RUNNING_AVG (priv->avg_render, elapsed);
2379 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2380 "avg_render: %" GST_TIME_FORMAT, GST_TIME_ARGS (priv->avg_render));
2384 /* with STREAM_LOCK, PREROLL_LOCK,
2386 * Synchronize the object on the clock and then render it.
2388 * takes ownership of obj.
2390 static GstFlowReturn
2391 gst_base_sink_render_object (GstBaseSink * basesink, GstPad * pad,
2392 GstMiniObject * obj)
2394 GstFlowReturn ret = GST_FLOW_OK;
2395 GstBaseSinkClass *bclass;
2396 gboolean late = FALSE;
2397 GstBaseSinkPrivate *priv;
2399 priv = basesink->priv;
2401 /* synchronize this object, non syncable objects return OK
2403 ret = gst_base_sink_do_sync (basesink, pad, obj, &late);
2404 if (G_UNLIKELY (ret != GST_FLOW_OK))
2407 /* and now render, event or buffer. */
2408 if (G_LIKELY (GST_IS_BUFFER (obj))) {
2411 /* drop late buffers unconditionally, let's hope it's unlikely */
2412 if (G_UNLIKELY (late))
2415 buf = GST_BUFFER_CAST (obj);
2417 gst_base_sink_set_last_buffer (basesink, buf);
2419 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2421 if (G_LIKELY (bclass->render)) {
2424 /* read once, to get same value before and after */
2425 do_qos = g_atomic_int_get (&priv->qos_enabled);
2427 GST_DEBUG_OBJECT (basesink, "rendering buffer %p", obj);
2429 /* record rendering time for QoS and stats */
2431 gst_base_sink_do_render_stats (basesink, TRUE);
2433 ret = bclass->render (basesink, buf);
2438 gst_base_sink_do_render_stats (basesink, FALSE);
2441 GstEvent *event = GST_EVENT_CAST (obj);
2442 gboolean event_res = TRUE;
2445 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2447 type = GST_EVENT_TYPE (event);
2449 GST_DEBUG_OBJECT (basesink, "rendering event %p, type %s", obj,
2450 gst_event_type_get_name (type));
2453 event_res = bclass->event (basesink, event);
2455 /* when we get here we could be flushing again when the event handler calls
2456 * _wait_eos(). We have to ignore this object in that case. */
2457 if (G_UNLIKELY (basesink->flushing))
2460 if (G_LIKELY (event_res)) {
2463 seqnum = basesink->priv->seqnum = gst_event_get_seqnum (event);
2464 GST_DEBUG_OBJECT (basesink, "Got seqnum #%" G_GUINT32_FORMAT, seqnum);
2469 GstMessage *message;
2471 /* the EOS event is completely handled so we mark
2472 * ourselves as being in the EOS state. eos is also
2473 * protected by the object lock so we can read it when
2474 * answering the POSITION query. */
2475 GST_OBJECT_LOCK (basesink);
2476 basesink->eos = TRUE;
2477 GST_OBJECT_UNLOCK (basesink);
2479 /* ok, now we can post the message */
2480 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
2482 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
2483 gst_message_set_seqnum (message, seqnum);
2484 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
2487 case GST_EVENT_NEWSEGMENT:
2488 /* configure the segment */
2489 gst_base_sink_configure_segment (basesink, pad, event,
2490 &basesink->segment);
2499 gst_base_sink_perform_qos (basesink, late);
2501 GST_DEBUG_OBJECT (basesink, "object unref after render %p", obj);
2502 gst_mini_object_unref (obj);
2509 GST_DEBUG_OBJECT (basesink, "do_sync returned %s", gst_flow_get_name (ret));
2515 GST_DEBUG_OBJECT (basesink, "buffer late, dropping");
2520 GST_DEBUG_OBJECT (basesink, "we are flushing, ignore object");
2521 gst_mini_object_unref (obj);
2522 return GST_FLOW_WRONG_STATE;
2526 /* with STREAM_LOCK, PREROLL_LOCK
2528 * Perform preroll on the given object. For buffers this means
2529 * calling the preroll subclass method.
2530 * If that succeeds, the state will be commited.
2532 * function does not take ownership of obj.
2534 static GstFlowReturn
2535 gst_base_sink_preroll_object (GstBaseSink * basesink, GstMiniObject * obj)
2539 GST_DEBUG_OBJECT (basesink, "prerolling object %p", obj);
2541 /* if it's a buffer, we need to call the preroll method */
2542 if (G_LIKELY (GST_IS_BUFFER (obj)) && basesink->priv->call_preroll) {
2543 GstBaseSinkClass *bclass;
2545 GstClockTime timestamp;
2547 buf = GST_BUFFER_CAST (obj);
2548 timestamp = GST_BUFFER_TIMESTAMP (buf);
2550 GST_DEBUG_OBJECT (basesink, "preroll buffer %" GST_TIME_FORMAT,
2551 GST_TIME_ARGS (timestamp));
2553 gst_base_sink_set_last_buffer (basesink, buf);
2555 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2556 if (bclass->preroll)
2557 if ((ret = bclass->preroll (basesink, buf)) != GST_FLOW_OK)
2558 goto preroll_failed;
2560 basesink->priv->call_preroll = FALSE;
2564 if (G_LIKELY (basesink->playing_async)) {
2565 if (G_UNLIKELY (!gst_base_sink_commit_state (basesink)))
2574 GST_DEBUG_OBJECT (basesink, "preroll failed, abort state");
2575 gst_element_abort_state (GST_ELEMENT_CAST (basesink));
2580 GST_DEBUG_OBJECT (basesink, "stopping while commiting state");
2581 return GST_FLOW_WRONG_STATE;
2585 /* with STREAM_LOCK, PREROLL_LOCK
2587 * Queue an object for rendering.
2588 * The first prerollable object queued will complete the preroll. If the
2589 * preroll queue if filled, we render all the objects in the queue.
2591 * This function takes ownership of the object.
2593 static GstFlowReturn
2594 gst_base_sink_queue_object_unlocked (GstBaseSink * basesink, GstPad * pad,
2595 GstMiniObject * obj, gboolean prerollable)
2597 GstFlowReturn ret = GST_FLOW_OK;
2601 if (G_UNLIKELY (basesink->need_preroll)) {
2602 if (G_LIKELY (prerollable))
2603 basesink->preroll_queued++;
2605 length = basesink->preroll_queued;
2607 GST_DEBUG_OBJECT (basesink, "now %d prerolled items", length);
2609 /* first prerollable item needs to finish the preroll */
2611 ret = gst_base_sink_preroll_object (basesink, obj);
2612 if (G_UNLIKELY (ret != GST_FLOW_OK))
2613 goto preroll_failed;
2615 /* need to recheck if we need preroll, commmit state during preroll
2616 * could have made us not need more preroll. */
2617 if (G_UNLIKELY (basesink->need_preroll)) {
2618 /* see if we can render now, if we can't add the object to the preroll
2620 if (G_UNLIKELY (length <= basesink->preroll_queue_max_len))
2625 /* we can start rendering (or blocking) the queued object
2627 q = basesink->preroll_queue;
2628 while (G_UNLIKELY (!g_queue_is_empty (q))) {
2631 o = g_queue_pop_head (q);
2632 GST_DEBUG_OBJECT (basesink, "rendering queued object %p", o);
2634 /* do something with the return value */
2635 ret = gst_base_sink_render_object (basesink, pad, o);
2636 if (ret != GST_FLOW_OK)
2637 goto dequeue_failed;
2640 /* now render the object */
2641 ret = gst_base_sink_render_object (basesink, pad, obj);
2642 basesink->preroll_queued = 0;
2649 GST_DEBUG_OBJECT (basesink, "preroll failed, reason %s",
2650 gst_flow_get_name (ret));
2651 gst_mini_object_unref (obj);
2656 /* add object to the queue and return */
2657 GST_DEBUG_OBJECT (basesink, "need more preroll data %d <= %d",
2658 length, basesink->preroll_queue_max_len);
2659 g_queue_push_tail (basesink->preroll_queue, obj);
2664 GST_DEBUG_OBJECT (basesink, "rendering queued objects failed, reason %s",
2665 gst_flow_get_name (ret));
2666 gst_mini_object_unref (obj);
2673 * This function grabs the PREROLL_LOCK and adds the object to
2676 * This function takes ownership of obj.
2678 static GstFlowReturn
2679 gst_base_sink_queue_object (GstBaseSink * basesink, GstPad * pad,
2680 GstMiniObject * obj, gboolean prerollable)
2684 GST_PAD_PREROLL_LOCK (pad);
2685 if (G_UNLIKELY (basesink->flushing))
2688 if (G_UNLIKELY (basesink->priv->received_eos))
2691 ret = gst_base_sink_queue_object_unlocked (basesink, pad, obj, prerollable);
2692 GST_PAD_PREROLL_UNLOCK (pad);
2699 GST_DEBUG_OBJECT (basesink, "sink is flushing");
2700 GST_PAD_PREROLL_UNLOCK (pad);
2701 gst_mini_object_unref (obj);
2702 return GST_FLOW_WRONG_STATE;
2706 GST_DEBUG_OBJECT (basesink,
2707 "we are EOS, dropping object, return UNEXPECTED");
2708 GST_PAD_PREROLL_UNLOCK (pad);
2709 gst_mini_object_unref (obj);
2710 return GST_FLOW_UNEXPECTED;
2715 gst_base_sink_flush_start (GstBaseSink * basesink, GstPad * pad)
2717 /* make sure we are not blocked on the clock also clear any pending
2719 gst_base_sink_set_flushing (basesink, pad, TRUE);
2721 /* we grab the stream lock but that is not needed since setting the
2722 * sink to flushing would make sure no state commit is being done
2724 GST_PAD_STREAM_LOCK (pad);
2725 gst_base_sink_reset_qos (basesink);
2726 if (basesink->priv->async_enabled) {
2727 /* and we need to commit our state again on the next
2728 * prerolled buffer */
2729 basesink->playing_async = TRUE;
2730 gst_element_lost_state (GST_ELEMENT_CAST (basesink));
2732 basesink->priv->have_latency = TRUE;
2733 basesink->need_preroll = FALSE;
2735 gst_base_sink_set_last_buffer (basesink, NULL);
2736 GST_PAD_STREAM_UNLOCK (pad);
2740 gst_base_sink_flush_stop (GstBaseSink * basesink, GstPad * pad)
2742 /* unset flushing so we can accept new data, this also flushes out any EOS
2744 gst_base_sink_set_flushing (basesink, pad, FALSE);
2746 /* for position reporting */
2747 GST_OBJECT_LOCK (basesink);
2748 basesink->priv->current_sstart = -1;
2749 basesink->priv->current_sstop = -1;
2750 basesink->priv->eos_rtime = -1;
2751 basesink->priv->call_preroll = TRUE;
2752 if (basesink->pad_mode == GST_ACTIVATE_PUSH) {
2753 /* we need new segment info after the flush. */
2754 basesink->have_newsegment = FALSE;
2755 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
2756 gst_segment_init (basesink->abidata.ABI.clip_segment, GST_FORMAT_UNDEFINED);
2758 GST_OBJECT_UNLOCK (basesink);
2762 gst_base_sink_event (GstPad * pad, GstEvent * event)
2764 GstBaseSink *basesink;
2765 gboolean result = TRUE;
2766 GstBaseSinkClass *bclass;
2768 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
2770 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2772 GST_DEBUG_OBJECT (basesink, "event %p (%s)", event,
2773 GST_EVENT_TYPE_NAME (event));
2775 switch (GST_EVENT_TYPE (event)) {
2780 GST_PAD_PREROLL_LOCK (pad);
2781 if (G_UNLIKELY (basesink->flushing))
2784 if (G_UNLIKELY (basesink->priv->received_eos)) {
2785 /* we can't accept anything when we are EOS */
2787 gst_event_unref (event);
2789 /* we set the received EOS flag here so that we can use it when testing if
2790 * we are prerolled and to refuse more buffers. */
2791 basesink->priv->received_eos = TRUE;
2793 /* EOS is a prerollable object, we call the unlocked version because it
2794 * does not check the received_eos flag. */
2795 ret = gst_base_sink_queue_object_unlocked (basesink, pad,
2796 GST_MINI_OBJECT_CAST (event), TRUE);
2797 if (G_UNLIKELY (ret != GST_FLOW_OK))
2800 GST_PAD_PREROLL_UNLOCK (pad);
2803 case GST_EVENT_NEWSEGMENT:
2807 GST_DEBUG_OBJECT (basesink, "newsegment %p", event);
2809 GST_PAD_PREROLL_LOCK (pad);
2810 if (G_UNLIKELY (basesink->flushing))
2813 if (G_UNLIKELY (basesink->priv->received_eos)) {
2814 /* we can't accept anything when we are EOS */
2816 gst_event_unref (event);
2818 /* the new segment is a non prerollable item and does not block anything,
2819 * we need to configure the current clipping segment and insert the event
2820 * in the queue to serialize it with the buffers for rendering. */
2821 gst_base_sink_configure_segment (basesink, pad, event,
2822 basesink->abidata.ABI.clip_segment);
2825 gst_base_sink_queue_object_unlocked (basesink, pad,
2826 GST_MINI_OBJECT_CAST (event), FALSE);
2827 if (G_UNLIKELY (ret != GST_FLOW_OK))
2830 GST_OBJECT_LOCK (basesink);
2831 basesink->have_newsegment = TRUE;
2832 GST_OBJECT_UNLOCK (basesink);
2835 GST_PAD_PREROLL_UNLOCK (pad);
2838 case GST_EVENT_FLUSH_START:
2840 bclass->event (basesink, event);
2842 GST_DEBUG_OBJECT (basesink, "flush-start %p", event);
2844 gst_base_sink_flush_start (basesink, pad);
2846 gst_event_unref (event);
2848 case GST_EVENT_FLUSH_STOP:
2850 bclass->event (basesink, event);
2852 GST_DEBUG_OBJECT (basesink, "flush-stop %p", event);
2854 gst_base_sink_flush_stop (basesink, pad);
2856 gst_event_unref (event);
2859 /* other events are sent to queue or subclass depending on if they
2860 * are serialized. */
2861 if (GST_EVENT_IS_SERIALIZED (event)) {
2862 gst_base_sink_queue_object (basesink, pad,
2863 GST_MINI_OBJECT_CAST (event), FALSE);
2866 bclass->event (basesink, event);
2867 gst_event_unref (event);
2872 gst_object_unref (basesink);
2879 GST_DEBUG_OBJECT (basesink, "we are flushing");
2880 GST_PAD_PREROLL_UNLOCK (pad);
2882 gst_event_unref (event);
2887 /* default implementation to calculate the start and end
2888 * timestamps on a buffer, subclasses can override
2891 gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
2892 GstClockTime * start, GstClockTime * end)
2894 GstClockTime timestamp, duration;
2896 timestamp = GST_BUFFER_TIMESTAMP (buffer);
2897 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
2899 /* get duration to calculate end time */
2900 duration = GST_BUFFER_DURATION (buffer);
2901 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2902 *end = timestamp + duration;
2908 /* must be called with PREROLL_LOCK */
2910 gst_base_sink_needs_preroll (GstBaseSink * basesink)
2912 gboolean is_prerolled, res;
2914 /* we have 2 cases where the PREROLL_LOCK is released:
2915 * 1) we are blocking in the PREROLL_LOCK and thus are prerolled.
2916 * 2) we are syncing on the clock
2918 is_prerolled = basesink->have_preroll || basesink->priv->received_eos;
2919 res = !is_prerolled;
2921 GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d => needs preroll: %d",
2922 basesink->have_preroll, basesink->priv->received_eos, res);
2927 /* with STREAM_LOCK, PREROLL_LOCK
2929 * Takes a buffer and compare the timestamps with the last segment.
2930 * If the buffer falls outside of the segment boundaries, drop it.
2931 * Else queue the buffer for preroll and rendering.
2933 * This function takes ownership of the buffer.
2935 static GstFlowReturn
2936 gst_base_sink_chain_unlocked (GstBaseSink * basesink, GstPad * pad,
2939 GstBaseSinkClass *bclass;
2940 GstFlowReturn result;
2941 GstClockTime start = GST_CLOCK_TIME_NONE, end = GST_CLOCK_TIME_NONE;
2942 GstSegment *clip_segment;
2944 if (G_UNLIKELY (basesink->flushing))
2947 if (G_UNLIKELY (basesink->priv->received_eos))
2950 /* for code clarity */
2951 clip_segment = basesink->abidata.ABI.clip_segment;
2953 if (G_UNLIKELY (!basesink->have_newsegment)) {
2956 sync = gst_base_sink_get_sync (basesink);
2958 GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
2959 (_("Internal data flow problem.")),
2960 ("Received buffer without a new-segment. Assuming timestamps start from 0."));
2963 /* this means this sink will assume timestamps start from 0 */
2964 GST_OBJECT_LOCK (basesink);
2965 clip_segment->start = 0;
2966 clip_segment->stop = -1;
2967 basesink->segment.start = 0;
2968 basesink->segment.stop = -1;
2969 basesink->have_newsegment = TRUE;
2970 GST_OBJECT_UNLOCK (basesink);
2973 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2975 /* check if the buffer needs to be dropped, we first ask the subclass for the
2977 if (bclass->get_times)
2978 bclass->get_times (basesink, buf, &start, &end);
2981 /* if the subclass does not want sync, we use our own values so that we at
2982 * least clip the buffer to the segment */
2983 gst_base_sink_get_times (basesink, buf, &start, &end);
2986 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
2987 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
2989 /* a dropped buffer does not participate in anything */
2990 if (GST_CLOCK_TIME_IS_VALID (start) &&
2991 (clip_segment->format == GST_FORMAT_TIME)) {
2992 if (G_UNLIKELY (!gst_segment_clip (clip_segment,
2993 GST_FORMAT_TIME, (gint64) start, (gint64) end, NULL, NULL)))
2994 goto out_of_segment;
2997 /* now we can process the buffer in the queue, this function takes ownership
2999 result = gst_base_sink_queue_object_unlocked (basesink, pad,
3000 GST_MINI_OBJECT_CAST (buf), TRUE);
3007 GST_DEBUG_OBJECT (basesink, "sink is flushing");
3008 gst_buffer_unref (buf);
3009 return GST_FLOW_WRONG_STATE;
3013 GST_DEBUG_OBJECT (basesink,
3014 "we are EOS, dropping object, return UNEXPECTED");
3015 gst_buffer_unref (buf);
3016 return GST_FLOW_UNEXPECTED;
3020 GST_DEBUG_OBJECT (basesink, "dropping buffer, out of clipping segment");
3021 gst_buffer_unref (buf);
3028 static GstFlowReturn
3029 gst_base_sink_chain (GstPad * pad, GstBuffer * buf)
3031 GstBaseSink *basesink;
3032 GstFlowReturn result;
3034 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
3036 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH))
3039 GST_PAD_PREROLL_LOCK (pad);
3040 result = gst_base_sink_chain_unlocked (basesink, pad, buf);
3041 GST_PAD_PREROLL_UNLOCK (pad);
3049 GST_OBJECT_LOCK (pad);
3050 GST_WARNING_OBJECT (basesink,
3051 "Push on pad %s:%s, but it was not activated in push mode",
3052 GST_DEBUG_PAD_NAME (pad));
3053 GST_OBJECT_UNLOCK (pad);
3054 gst_buffer_unref (buf);
3055 /* we don't post an error message this will signal to the peer
3056 * pushing that EOS is reached. */
3057 result = GST_FLOW_UNEXPECTED;
3063 gst_base_sink_default_do_seek (GstBaseSink * sink, GstSegment * segment)
3065 gboolean res = TRUE;
3067 /* update our offset if the start/stop position was updated */
3068 if (segment->format == GST_FORMAT_BYTES) {
3069 segment->time = segment->start;
3070 } else if (segment->start == 0) {
3071 /* seek to start, we can implement a default for this. */
3075 GST_INFO_OBJECT (sink, "Can't do a default seek");
3081 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
3084 gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
3085 GstEvent * event, GstSegment * segment)
3087 /* By default, we try one of 2 things:
3088 * - For absolute seek positions, convert the requested position to our
3089 * configured processing format and place it in the output segment \
3090 * - For relative seek positions, convert our current (input) values to the
3091 * seek format, adjust by the relative seek offset and then convert back to
3092 * the processing format
3094 GstSeekType cur_type, stop_type;
3097 GstFormat seek_format, dest_format;
3100 gboolean res = TRUE;
3102 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3103 &cur_type, &cur, &stop_type, &stop);
3104 dest_format = segment->format;
3106 if (seek_format == dest_format) {
3107 gst_segment_set_seek (segment, rate, seek_format, flags,
3108 cur_type, cur, stop_type, stop, &update);
3112 if (cur_type != GST_SEEK_TYPE_NONE) {
3113 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3115 gst_pad_query_convert (sink->sinkpad, seek_format, cur, &dest_format,
3117 cur_type = GST_SEEK_TYPE_SET;
3120 if (res && stop_type != GST_SEEK_TYPE_NONE) {
3121 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3123 gst_pad_query_convert (sink->sinkpad, seek_format, stop, &dest_format,
3125 stop_type = GST_SEEK_TYPE_SET;
3128 /* And finally, configure our output segment in the desired format */
3129 gst_segment_set_seek (segment, rate, dest_format, flags, cur_type, cur,
3130 stop_type, stop, &update);
3139 GST_DEBUG_OBJECT (sink, "undefined format given, seek aborted.");
3144 /* perform a seek, only executed in pull mode */
3146 gst_base_sink_perform_seek (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3150 GstFormat seek_format, dest_format;
3152 GstSeekType cur_type, stop_type;
3153 gboolean seekseg_configured = FALSE;
3155 gboolean update, res = TRUE;
3156 GstSegment seeksegment;
3158 dest_format = sink->segment.format;
3161 GST_DEBUG_OBJECT (sink, "performing seek with event %p", event);
3162 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3163 &cur_type, &cur, &stop_type, &stop);
3165 flush = flags & GST_SEEK_FLAG_FLUSH;
3167 GST_DEBUG_OBJECT (sink, "performing seek without event");
3172 GST_DEBUG_OBJECT (sink, "flushing upstream");
3173 gst_pad_push_event (pad, gst_event_new_flush_start ());
3174 gst_base_sink_flush_start (sink, pad);
3176 GST_DEBUG_OBJECT (sink, "pausing pulling thread");
3179 GST_PAD_STREAM_LOCK (pad);
3181 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
3182 * copy the current segment info into the temp segment that we can actually
3183 * attempt the seek with. We only update the real segment if the seek suceeds. */
3184 if (!seekseg_configured) {
3185 memcpy (&seeksegment, &sink->segment, sizeof (GstSegment));
3187 /* now configure the final seek segment */
3189 if (sink->segment.format != seek_format) {
3190 /* OK, here's where we give the subclass a chance to convert the relative
3191 * seek into an absolute one in the processing format. We set up any
3192 * absolute seek above, before taking the stream lock. */
3193 if (!gst_base_sink_default_prepare_seek_segment (sink, event,
3195 GST_DEBUG_OBJECT (sink,
3196 "Preparing the seek failed after flushing. " "Aborting seek");
3200 /* The seek format matches our processing format, no need to ask the
3201 * the subclass to configure the segment. */
3202 gst_segment_set_seek (&seeksegment, rate, seek_format, flags,
3203 cur_type, cur, stop_type, stop, &update);
3206 /* Else, no seek event passed, so we're just (re)starting the
3211 GST_DEBUG_OBJECT (sink, "segment configured from %" G_GINT64_FORMAT
3212 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
3213 seeksegment.start, seeksegment.stop, seeksegment.last_stop);
3215 /* do the seek, segment.last_stop contains the new position. */
3216 res = gst_base_sink_default_do_seek (sink, &seeksegment);
3221 GST_DEBUG_OBJECT (sink, "stop flushing upstream");
3222 gst_pad_push_event (pad, gst_event_new_flush_stop ());
3223 gst_base_sink_flush_stop (sink, pad);
3224 } else if (res && sink->abidata.ABI.running) {
3225 /* we are running the current segment and doing a non-flushing seek,
3226 * close the segment first based on the last_stop. */
3227 GST_DEBUG_OBJECT (sink, "closing running segment %" G_GINT64_FORMAT
3228 " to %" G_GINT64_FORMAT, sink->segment.start, sink->segment.last_stop);
3231 /* The subclass must have converted the segment to the processing format
3233 if (res && seeksegment.format != dest_format) {
3234 GST_DEBUG_OBJECT (sink, "Subclass failed to prepare a seek segment "
3235 "in the correct format. Aborting seek.");
3239 /* if successfull seek, we update our real segment and push
3240 * out the new segment. */
3242 memcpy (&sink->segment, &seeksegment, sizeof (GstSegment));
3244 if (sink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3245 gst_element_post_message (GST_ELEMENT (sink),
3246 gst_message_new_segment_start (GST_OBJECT (sink),
3247 sink->segment.format, sink->segment.last_stop));
3251 sink->priv->discont = TRUE;
3252 sink->abidata.ABI.running = TRUE;
3254 GST_PAD_STREAM_UNLOCK (pad);
3260 gst_base_sink_perform_step (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3262 GstBaseSinkPrivate *priv;
3263 GstBaseSinkClass *bclass;
3264 gboolean flush, intermediate;
3270 bclass = GST_BASE_SINK_GET_CLASS (sink);
3273 GST_DEBUG_OBJECT (sink, "performing step with event %p", event);
3275 gst_event_parse_step (event, &format, &amount, &rate, &flush, &intermediate);
3276 seqnum = gst_event_get_seqnum (event);
3279 /* we need to call ::unlock before locking PREROLL_LOCK
3280 * since we lock it before going into ::render */
3282 bclass->unlock (sink);
3284 GST_PAD_PREROLL_LOCK (sink->sinkpad);
3285 priv->step.cookie++;
3287 /* update the segment */
3288 priv->step.seqnum = seqnum;
3289 priv->step.format = format;
3290 priv->step.amount = amount;
3291 priv->step.remaining = amount;
3292 priv->step.rate = rate;
3293 priv->step.intermediate = intermediate;
3295 /* now that we have the PREROLL lock, clear our unlock request */
3296 if (bclass->unlock_stop)
3297 bclass->unlock_stop (sink);
3299 if (sink->priv->async_enabled) {
3300 /* and we need to commit our state again on the next
3301 * prerolled buffer */
3302 sink->playing_async = TRUE;
3303 priv->step.need_preroll = TRUE;
3304 sink->need_preroll = FALSE;
3305 gst_element_lost_state_full (GST_ELEMENT_CAST (sink), FALSE);
3307 sink->priv->have_latency = TRUE;
3308 sink->need_preroll = FALSE;
3310 priv->call_preroll = TRUE;
3311 gst_base_sink_set_last_buffer (sink, NULL);
3312 gst_base_sink_reset_qos (sink);
3314 if (sink->clock_id) {
3315 gst_clock_id_unschedule (sink->clock_id);
3318 gst_element_post_message (GST_ELEMENT_CAST (sink),
3319 gst_message_new_async_start (GST_OBJECT_CAST (sink), FALSE));
3321 GST_DEBUG_OBJECT (sink, "signal waiter");
3322 GST_PAD_PREROLL_SIGNAL (sink->sinkpad);
3324 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
3333 gst_base_sink_loop (GstPad * pad)
3335 GstBaseSink *basesink;
3336 GstBuffer *buf = NULL;
3337 GstFlowReturn result;
3341 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
3343 g_assert (basesink->pad_mode == GST_ACTIVATE_PULL);
3345 if ((blocksize = basesink->priv->blocksize) == 0)
3348 offset = basesink->segment.last_stop;
3350 GST_DEBUG_OBJECT (basesink, "pulling %" G_GUINT64_FORMAT ", %u",
3353 result = gst_pad_pull_range (pad, offset, blocksize, &buf);
3354 if (G_UNLIKELY (result != GST_FLOW_OK))
3357 if (G_UNLIKELY (buf == NULL))
3360 offset += GST_BUFFER_SIZE (buf);
3362 gst_segment_set_last_stop (&basesink->segment, GST_FORMAT_BYTES, offset);
3364 GST_PAD_PREROLL_LOCK (pad);
3365 result = gst_base_sink_chain_unlocked (basesink, pad, buf);
3366 GST_PAD_PREROLL_UNLOCK (pad);
3367 if (G_UNLIKELY (result != GST_FLOW_OK))
3375 GST_LOG_OBJECT (basesink, "pausing task, reason %s",
3376 gst_flow_get_name (result));
3377 gst_pad_pause_task (pad);
3378 /* fatal errors and NOT_LINKED cause EOS */
3379 if (GST_FLOW_IS_FATAL (result) || result == GST_FLOW_NOT_LINKED) {
3380 if (result == GST_FLOW_UNEXPECTED) {
3381 /* perform EOS logic */
3382 if (basesink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3383 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3384 gst_message_new_segment_done (GST_OBJECT_CAST (basesink),
3385 basesink->segment.format, basesink->segment.last_stop));
3387 gst_base_sink_event (pad, gst_event_new_eos ());
3390 /* for fatal errors we post an error message, post the error
3391 * first so the app knows about the error first. */
3392 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
3393 (_("Internal data stream error.")),
3394 ("stream stopped, reason %s", gst_flow_get_name (result)));
3395 gst_base_sink_event (pad, gst_event_new_eos ());
3402 GST_LOG_OBJECT (basesink, "no buffer, pausing");
3403 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
3404 (_("Internal data flow error.")), ("element returned NULL buffer"));
3405 result = GST_FLOW_ERROR;
3411 gst_base_sink_set_flushing (GstBaseSink * basesink, GstPad * pad,
3414 GstBaseSinkClass *bclass;
3416 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3419 /* unlock any subclasses, we need to do this before grabbing the
3420 * PREROLL_LOCK since we hold this lock before going into ::render. */
3422 bclass->unlock (basesink);
3425 GST_PAD_PREROLL_LOCK (pad);
3426 basesink->flushing = flushing;
3428 /* step 1, now that we have the PREROLL lock, clear our unlock request */
3429 if (bclass->unlock_stop)
3430 bclass->unlock_stop (basesink);
3432 /* set need_preroll before we unblock the clock. If the clock is unblocked
3433 * before timing out, we can reuse the buffer for preroll. */
3434 basesink->need_preroll = TRUE;
3436 /* step 2, unblock clock sync (if any) or any other blocking thing */
3437 if (basesink->clock_id) {
3438 gst_clock_id_unschedule (basesink->clock_id);
3441 /* flush out the data thread if it's locked in finish_preroll, this will
3442 * also flush out the EOS state */
3443 GST_DEBUG_OBJECT (basesink,
3444 "flushing out data thread, need preroll to TRUE");
3445 gst_base_sink_preroll_queue_flush (basesink, pad);
3447 GST_PAD_PREROLL_UNLOCK (pad);
3453 gst_base_sink_default_activate_pull (GstBaseSink * basesink, gboolean active)
3459 result = gst_pad_start_task (basesink->sinkpad,
3460 (GstTaskFunction) gst_base_sink_loop, basesink->sinkpad);
3462 /* step 2, make sure streaming finishes */
3463 result = gst_pad_stop_task (basesink->sinkpad);
3470 gst_base_sink_pad_activate (GstPad * pad)
3472 gboolean result = FALSE;
3473 GstBaseSink *basesink;
3475 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3477 GST_DEBUG_OBJECT (basesink, "Trying pull mode first");
3479 gst_base_sink_set_flushing (basesink, pad, FALSE);
3481 /* we need to have the pull mode enabled */
3482 if (!basesink->can_activate_pull) {
3483 GST_DEBUG_OBJECT (basesink, "pull mode disabled");
3487 /* check if downstreams supports pull mode at all */
3488 if (!gst_pad_check_pull_range (pad)) {
3489 GST_DEBUG_OBJECT (basesink, "pull mode not supported");
3493 /* set the pad mode before starting the task so that it's in the
3494 * correct state for the new thread. also the sink set_caps and get_caps
3495 * function checks this */
3496 basesink->pad_mode = GST_ACTIVATE_PULL;
3498 /* we first try to negotiate a format so that when we try to activate
3499 * downstream, it knows about our format */
3500 if (!gst_base_sink_negotiate_pull (basesink)) {
3501 GST_DEBUG_OBJECT (basesink, "failed to negotiate in pull mode");
3505 /* ok activate now */
3506 if (!gst_pad_activate_pull (pad, TRUE)) {
3507 /* clear any pending caps */
3508 GST_OBJECT_LOCK (basesink);
3509 gst_caps_replace (&basesink->priv->pull_caps, NULL);
3510 GST_OBJECT_UNLOCK (basesink);
3511 GST_DEBUG_OBJECT (basesink, "failed to activate in pull mode");
3515 GST_DEBUG_OBJECT (basesink, "Success activating pull mode");
3519 /* push mode fallback */
3521 GST_DEBUG_OBJECT (basesink, "Falling back to push mode");
3522 if ((result = gst_pad_activate_push (pad, TRUE))) {
3523 GST_DEBUG_OBJECT (basesink, "Success activating push mode");
3528 GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
3529 gst_base_sink_set_flushing (basesink, pad, TRUE);
3532 gst_object_unref (basesink);
3538 gst_base_sink_pad_activate_push (GstPad * pad, gboolean active)
3541 GstBaseSink *basesink;
3543 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3546 if (!basesink->can_activate_push) {
3548 basesink->pad_mode = GST_ACTIVATE_NONE;
3551 basesink->pad_mode = GST_ACTIVATE_PUSH;
3554 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH)) {
3555 g_warning ("Internal GStreamer activation error!!!");
3558 gst_base_sink_set_flushing (basesink, pad, TRUE);
3560 basesink->pad_mode = GST_ACTIVATE_NONE;
3564 gst_object_unref (basesink);
3570 gst_base_sink_negotiate_pull (GstBaseSink * basesink)
3577 /* this returns the intersection between our caps and the peer caps. If there
3578 * is no peer, it returns NULL and we can't operate in pull mode so we can
3579 * fail the negotiation. */
3580 caps = gst_pad_get_allowed_caps (GST_BASE_SINK_PAD (basesink));
3581 if (caps == NULL || gst_caps_is_empty (caps))
3582 goto no_caps_possible;
3584 GST_DEBUG_OBJECT (basesink, "allowed caps: %" GST_PTR_FORMAT, caps);
3586 caps = gst_caps_make_writable (caps);
3587 /* get the first (prefered) format */
3588 gst_caps_truncate (caps);
3590 gst_pad_fixate_caps (GST_BASE_SINK_PAD (basesink), caps);
3592 GST_DEBUG_OBJECT (basesink, "fixated to: %" GST_PTR_FORMAT, caps);
3594 if (gst_caps_is_any (caps)) {
3595 GST_DEBUG_OBJECT (basesink, "caps were ANY after fixating, "
3597 /* neither side has template caps in this case, so they are prepared for
3598 pull() without setcaps() */
3600 } else if (gst_caps_is_fixed (caps)) {
3601 if (!gst_pad_set_caps (GST_BASE_SINK_PAD (basesink), caps))
3602 goto could_not_set_caps;
3604 GST_OBJECT_LOCK (basesink);
3605 gst_caps_replace (&basesink->priv->pull_caps, caps);
3606 GST_OBJECT_UNLOCK (basesink);
3611 gst_caps_unref (caps);
3617 GST_INFO_OBJECT (basesink, "Pipeline could not agree on caps");
3618 GST_DEBUG_OBJECT (basesink, "get_allowed_caps() returned EMPTY");
3620 gst_caps_unref (caps);
3625 GST_INFO_OBJECT (basesink, "Could not set caps: %" GST_PTR_FORMAT, caps);
3626 gst_caps_unref (caps);
3631 /* this won't get called until we implement an activate function */
3633 gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active)
3635 gboolean result = FALSE;
3636 GstBaseSink *basesink;
3637 GstBaseSinkClass *bclass;
3639 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3640 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3646 /* we mark we have a newsegment here because pull based
3647 * mode works just fine without having a newsegment before the
3649 format = GST_FORMAT_BYTES;
3651 gst_segment_init (&basesink->segment, format);
3652 gst_segment_init (basesink->abidata.ABI.clip_segment, format);
3653 GST_OBJECT_LOCK (basesink);
3654 basesink->have_newsegment = TRUE;
3655 GST_OBJECT_UNLOCK (basesink);
3657 /* get the peer duration in bytes */
3658 result = gst_pad_query_peer_duration (pad, &format, &duration);
3660 GST_DEBUG_OBJECT (basesink,
3661 "setting duration in bytes to %" G_GINT64_FORMAT, duration);
3662 gst_segment_set_duration (basesink->abidata.ABI.clip_segment, format,
3664 gst_segment_set_duration (&basesink->segment, format, duration);
3666 GST_DEBUG_OBJECT (basesink, "unknown duration");
3669 if (bclass->activate_pull)
3670 result = bclass->activate_pull (basesink, TRUE);
3675 goto activate_failed;
3678 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PULL)) {
3679 g_warning ("Internal GStreamer activation error!!!");
3682 result = gst_base_sink_set_flushing (basesink, pad, TRUE);
3683 if (bclass->activate_pull)
3684 result &= bclass->activate_pull (basesink, FALSE);
3685 basesink->pad_mode = GST_ACTIVATE_NONE;
3686 /* clear any pending caps */
3687 GST_OBJECT_LOCK (basesink);
3688 gst_caps_replace (&basesink->priv->pull_caps, NULL);
3689 GST_OBJECT_UNLOCK (basesink);
3692 gst_object_unref (basesink);
3699 /* reset, as starting the thread failed */
3700 basesink->pad_mode = GST_ACTIVATE_NONE;
3702 GST_ERROR_OBJECT (basesink, "subclass failed to activate in pull mode");
3707 /* send an event to our sinkpad peer. */
3709 gst_base_sink_send_event (GstElement * element, GstEvent * event)
3712 GstBaseSink *basesink = GST_BASE_SINK (element);
3713 gboolean forward, result = TRUE;
3714 GstActivateMode mode;
3716 GST_OBJECT_LOCK (element);
3717 /* get the pad and the scheduling mode */
3718 pad = gst_object_ref (basesink->sinkpad);
3719 mode = basesink->pad_mode;
3720 GST_OBJECT_UNLOCK (element);
3722 /* only push UPSTREAM events upstream */
3723 forward = GST_EVENT_IS_UPSTREAM (event);
3725 switch (GST_EVENT_TYPE (event)) {
3726 case GST_EVENT_LATENCY:
3728 GstClockTime latency;
3730 gst_event_parse_latency (event, &latency);
3732 /* store the latency. We use this to adjust the running_time before syncing
3733 * it to the clock. */
3734 GST_OBJECT_LOCK (element);
3735 basesink->priv->latency = latency;
3736 if (!basesink->priv->have_latency)
3738 GST_OBJECT_UNLOCK (element);
3739 GST_DEBUG_OBJECT (basesink, "latency set to %" GST_TIME_FORMAT,
3740 GST_TIME_ARGS (latency));
3742 /* We forward this event so that all elements know about the global pipeline
3743 * latency. This is interesting for an element when it wants to figure out
3744 * when a particular piece of data will be rendered. */
3747 case GST_EVENT_SEEK:
3748 /* in pull mode we will execute the seek */
3749 if (mode == GST_ACTIVATE_PULL)
3750 result = gst_base_sink_perform_seek (basesink, pad, event);
3752 case GST_EVENT_STEP:
3753 result = gst_base_sink_perform_step (basesink, pad, event);
3761 result = gst_pad_push_event (pad, event);
3763 /* not forwarded, unref the event */
3764 gst_event_unref (event);
3767 gst_object_unref (pad);
3772 gst_base_sink_peer_query (GstBaseSink * sink, GstQuery * query)
3775 gboolean res = FALSE;
3777 if ((peer = gst_pad_get_peer (sink->sinkpad))) {
3778 res = gst_pad_query (peer, query);
3779 gst_object_unref (peer);
3784 /* get the end position of the last seen object, this is used
3785 * for EOS and for making sure that we don't report a position we
3786 * have not reached yet. With LOCK. */
3788 gst_base_sink_get_position_last (GstBaseSink * basesink, GstFormat format,
3792 GstSegment *segment;
3793 gboolean ret = TRUE;
3795 segment = &basesink->segment;
3796 oformat = segment->format;
3798 if (oformat == GST_FORMAT_TIME) {
3799 /* return last observed stream time, we keep the stream time around in the
3801 *cur = basesink->priv->current_sstop;
3803 /* convert last stop to stream time */
3804 *cur = gst_segment_to_stream_time (segment, oformat, segment->last_stop);
3807 if (*cur != -1 && oformat != format) {
3808 GST_OBJECT_UNLOCK (basesink);
3809 /* convert to the target format if we need to, release lock first */
3811 gst_pad_query_convert (basesink->sinkpad, oformat, *cur, &format, cur);
3814 GST_OBJECT_LOCK (basesink);
3817 GST_DEBUG_OBJECT (basesink, "POSITION: %" GST_TIME_FORMAT,
3818 GST_TIME_ARGS (*cur));
3823 /* get the position when we are PAUSED, this is the stream time of the buffer
3824 * that prerolled. If no buffer is prerolled (we are still flushing), this
3825 * value will be -1. With LOCK. */
3827 gst_base_sink_get_position_paused (GstBaseSink * basesink, GstFormat format,
3832 GstSegment *segment;
3835 /* we don't use the clip segment in pull mode, when seeking we update the
3836 * main segment directly with the new segment values without it having to be
3837 * activated by the rendering after preroll */
3838 if (basesink->pad_mode == GST_ACTIVATE_PUSH)
3839 segment = basesink->abidata.ABI.clip_segment;
3841 segment = &basesink->segment;
3842 oformat = segment->format;
3844 if (oformat == GST_FORMAT_TIME) {
3845 *cur = basesink->priv->current_sstart;
3847 *cur = gst_segment_to_stream_time (segment, oformat, segment->last_stop);
3850 time = segment->time;
3853 *cur = MAX (*cur, time);
3854 GST_DEBUG_OBJECT (basesink, "POSITION as max: %" GST_TIME_FORMAT
3855 ", time %" GST_TIME_FORMAT, GST_TIME_ARGS (*cur), GST_TIME_ARGS (time));
3857 /* we have no buffer, use the segment times. */
3858 if (segment->rate >= 0.0) {
3859 /* forward, next position is always the time of the segment */
3861 GST_DEBUG_OBJECT (basesink, "POSITION as time: %" GST_TIME_FORMAT,
3862 GST_TIME_ARGS (*cur));
3864 /* reverse, next expected timestamp is segment->stop. We use the function
3865 * to get things right for negative applied_rates. */
3866 *cur = gst_segment_to_stream_time (segment, oformat, segment->stop);
3867 GST_DEBUG_OBJECT (basesink, "reverse POSITION: %" GST_TIME_FORMAT,
3868 GST_TIME_ARGS (*cur));
3873 if (res && oformat != format) {
3874 GST_OBJECT_UNLOCK (basesink);
3876 gst_pad_query_convert (basesink->sinkpad, oformat, *cur, &format, cur);
3879 GST_OBJECT_LOCK (basesink);
3886 gst_base_sink_get_position (GstBaseSink * basesink, GstFormat format,
3887 gint64 * cur, gboolean * upstream)
3890 gboolean res = FALSE;
3891 GstFormat oformat, tformat;
3892 GstClockTime now, base, latency;
3893 gint64 time, accum, duration;
3897 GST_OBJECT_LOCK (basesink);
3898 /* our intermediate time format */
3899 tformat = GST_FORMAT_TIME;
3900 /* get the format in the segment */
3901 oformat = basesink->segment.format;
3903 /* can only give answer based on the clock if not EOS */
3904 if (G_UNLIKELY (basesink->eos))
3907 /* we can only get the segment when we are not NULL or READY */
3908 if (!basesink->have_newsegment)
3911 /* when not in PLAYING or when we're busy with a state change, we
3912 * cannot read from the clock so we report time based on the
3913 * last seen timestamp. */
3914 if (GST_STATE (basesink) != GST_STATE_PLAYING ||
3915 GST_STATE_PENDING (basesink) != GST_STATE_VOID_PENDING)
3918 /* we need to sync on the clock. */
3919 if (basesink->sync == FALSE)
3922 /* and we need a clock */
3923 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (basesink)) == NULL))
3926 /* collect all data we need holding the lock */
3927 if (GST_CLOCK_TIME_IS_VALID (basesink->segment.time))
3928 time = basesink->segment.time;
3932 if (GST_CLOCK_TIME_IS_VALID (basesink->segment.stop))
3933 duration = basesink->segment.stop - basesink->segment.start;
3937 base = GST_ELEMENT_CAST (basesink)->base_time;
3938 accum = basesink->segment.accum;
3939 rate = basesink->segment.rate * basesink->segment.applied_rate;
3940 latency = basesink->priv->latency;
3942 gst_object_ref (clock);
3944 /* this function might release the LOCK */
3945 gst_base_sink_get_position_last (basesink, format, &last);
3947 /* need to release the object lock before we can get the time,
3948 * a clock might take the LOCK of the provider, which could be
3949 * a basesink subclass. */
3950 GST_OBJECT_UNLOCK (basesink);
3952 now = gst_clock_get_time (clock);
3954 if (oformat != tformat) {
3955 /* convert accum, time and duration to time */
3956 if (!gst_pad_query_convert (basesink->sinkpad, oformat, accum, &tformat,
3958 goto convert_failed;
3959 if (!gst_pad_query_convert (basesink->sinkpad, oformat, duration, &tformat,
3961 goto convert_failed;
3962 if (!gst_pad_query_convert (basesink->sinkpad, oformat, time, &tformat,
3964 goto convert_failed;
3967 /* subtract base time and accumulated time from the clock time.
3968 * Make sure we don't go negative. This is the current time in
3969 * the segment which we need to scale with the combined
3970 * rate and applied rate. */
3973 base = MIN (now, base);
3975 /* for negative rates we need to count back from from the segment
3980 *cur = time + gst_guint64_to_gdouble (now - base) * rate;
3982 /* never report more than last seen position */
3984 *cur = MIN (last, *cur);
3986 gst_object_unref (clock);
3988 GST_DEBUG_OBJECT (basesink,
3989 "now %" GST_TIME_FORMAT " - base %" GST_TIME_FORMAT " - accum %"
3990 GST_TIME_FORMAT " + time %" GST_TIME_FORMAT,
3991 GST_TIME_ARGS (now), GST_TIME_ARGS (base),
3992 GST_TIME_ARGS (accum), GST_TIME_ARGS (time));
3994 if (oformat != format) {
3995 /* convert time to final format */
3996 if (!gst_pad_query_convert (basesink->sinkpad, tformat, *cur, &format, cur))
3997 goto convert_failed;
4003 GST_DEBUG_OBJECT (basesink, "res: %d, POSITION: %" GST_TIME_FORMAT,
4004 res, GST_TIME_ARGS (*cur));
4010 GST_DEBUG_OBJECT (basesink, "position in EOS");
4011 res = gst_base_sink_get_position_last (basesink, format, cur);
4012 GST_OBJECT_UNLOCK (basesink);
4017 GST_DEBUG_OBJECT (basesink, "position in PAUSED");
4018 res = gst_base_sink_get_position_paused (basesink, format, cur);
4019 GST_OBJECT_UNLOCK (basesink);
4024 /* in NULL or READY we always return FALSE and -1 */
4025 GST_DEBUG_OBJECT (basesink, "position in wrong state, return -1");
4028 GST_OBJECT_UNLOCK (basesink);
4033 /* report last seen timestamp if any, else ask upstream to answer */
4034 if ((*cur = basesink->priv->current_sstart) != -1)
4039 GST_DEBUG_OBJECT (basesink, "no sync, res %d, POSITION %" GST_TIME_FORMAT,
4040 res, GST_TIME_ARGS (*cur));
4041 GST_OBJECT_UNLOCK (basesink);
4046 GST_DEBUG_OBJECT (basesink, "convert failed, try upstream");
4053 gst_base_sink_query (GstElement * element, GstQuery * query)
4055 gboolean res = FALSE;
4057 GstBaseSink *basesink = GST_BASE_SINK (element);
4059 switch (GST_QUERY_TYPE (query)) {
4060 case GST_QUERY_POSITION:
4064 gboolean upstream = FALSE;
4066 gst_query_parse_position (query, &format, NULL);
4068 GST_DEBUG_OBJECT (basesink, "position format %d", format);
4070 /* first try to get the position based on the clock */
4072 gst_base_sink_get_position (basesink, format, &cur, &upstream))) {
4073 gst_query_set_position (query, format, cur);
4074 } else if (upstream) {
4075 /* fallback to peer query */
4076 res = gst_base_sink_peer_query (basesink, query);
4080 case GST_QUERY_DURATION:
4082 GstFormat format, uformat;
4083 gint64 duration, uduration;
4085 gst_query_parse_duration (query, &format, NULL);
4087 GST_DEBUG_OBJECT (basesink, "duration query in format %s",
4088 gst_format_get_name (format));
4090 if (basesink->pad_mode == GST_ACTIVATE_PULL) {
4091 uformat = GST_FORMAT_BYTES;
4093 /* get the duration in bytes, in pull mode that's all we are sure to
4094 * know. We have to explicitly get this value from upstream instead of
4095 * using our cached value because it might change. Duration caching
4096 * should be done at a higher level. */
4097 res = gst_pad_query_peer_duration (basesink->sinkpad, &uformat,
4100 gst_segment_set_duration (&basesink->segment, uformat, uduration);
4101 if (format != uformat) {
4102 /* convert to the requested format */
4103 res = gst_pad_query_convert (basesink->sinkpad, uformat, uduration,
4104 &format, &duration);
4106 duration = uduration;
4109 /* set the result */
4110 gst_query_set_duration (query, format, duration);
4114 /* in push mode we simply forward upstream */
4115 res = gst_base_sink_peer_query (basesink, query);
4119 case GST_QUERY_LATENCY:
4121 gboolean live, us_live;
4122 GstClockTime min, max;
4124 if ((res = gst_base_sink_query_latency (basesink, &live, &us_live, &min,
4126 gst_query_set_latency (query, live, min, max);
4130 case GST_QUERY_JITTER:
4132 case GST_QUERY_RATE:
4133 /* gst_query_set_rate (query, basesink->segment_rate); */
4136 case GST_QUERY_SEGMENT:
4138 /* FIXME, bring start/stop to stream time */
4139 gst_query_set_segment (query, basesink->segment.rate,
4140 GST_FORMAT_TIME, basesink->segment.start, basesink->segment.stop);
4143 case GST_QUERY_SEEKING:
4144 case GST_QUERY_CONVERT:
4145 case GST_QUERY_FORMATS:
4147 res = gst_base_sink_peer_query (basesink, query);
4153 static GstStateChangeReturn
4154 gst_base_sink_change_state (GstElement * element, GstStateChange transition)
4156 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
4157 GstBaseSink *basesink = GST_BASE_SINK (element);
4158 GstBaseSinkClass *bclass;
4159 GstBaseSinkPrivate *priv;
4161 priv = basesink->priv;
4163 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4165 switch (transition) {
4166 case GST_STATE_CHANGE_NULL_TO_READY:
4168 if (!bclass->start (basesink))
4171 case GST_STATE_CHANGE_READY_TO_PAUSED:
4172 /* need to complete preroll before this state change completes, there
4173 * is no data flow in READY so we can safely assume we need to preroll. */
4174 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4175 GST_DEBUG_OBJECT (basesink, "READY to PAUSED");
4176 basesink->have_newsegment = FALSE;
4177 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
4178 gst_segment_init (basesink->abidata.ABI.clip_segment,
4179 GST_FORMAT_UNDEFINED);
4180 basesink->offset = 0;
4181 basesink->have_preroll = FALSE;
4182 basesink->need_preroll = TRUE;
4183 basesink->playing_async = TRUE;
4184 priv->current_sstart = -1;
4185 priv->current_sstop = -1;
4186 priv->eos_rtime = -1;
4188 basesink->eos = FALSE;
4189 priv->received_eos = FALSE;
4190 gst_base_sink_reset_qos (basesink);
4191 priv->commited = FALSE;
4192 priv->call_preroll = TRUE;
4193 priv->step.remaining = -1;
4194 if (priv->async_enabled) {
4195 GST_DEBUG_OBJECT (basesink, "doing async state change");
4196 /* when async enabled, post async-start message and return ASYNC from
4197 * the state change function */
4198 ret = GST_STATE_CHANGE_ASYNC;
4199 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4200 gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
4202 priv->have_latency = TRUE;
4204 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4206 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
4207 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4208 if (!gst_base_sink_needs_preroll (basesink)) {
4209 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, don't need preroll");
4210 /* no preroll needed anymore now. */
4211 basesink->playing_async = FALSE;
4212 basesink->need_preroll = FALSE;
4213 if (basesink->eos) {
4214 GstMessage *message;
4216 /* need to post EOS message here */
4217 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
4218 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
4219 gst_message_set_seqnum (message, basesink->priv->seqnum);
4220 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
4222 GST_DEBUG_OBJECT (basesink, "signal preroll");
4223 GST_PAD_PREROLL_SIGNAL (basesink->sinkpad);
4226 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, we are not prerolled");
4227 basesink->need_preroll = TRUE;
4228 basesink->playing_async = TRUE;
4229 priv->call_preroll = TRUE;
4230 priv->commited = FALSE;
4231 if (priv->async_enabled) {
4232 GST_DEBUG_OBJECT (basesink, "doing async state change");
4233 ret = GST_STATE_CHANGE_ASYNC;
4234 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4235 gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
4238 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4245 GstStateChangeReturn bret;
4247 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4248 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
4249 goto activate_failed;
4252 switch (transition) {
4253 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
4254 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED");
4255 /* FIXME, make sure we cannot enter _render first */
4257 /* we need to call ::unlock before locking PREROLL_LOCK
4258 * since we lock it before going into ::render */
4260 bclass->unlock (basesink);
4262 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4263 /* now that we have the PREROLL lock, clear our unlock request */
4264 if (bclass->unlock_stop)
4265 bclass->unlock_stop (basesink);
4267 /* we need preroll again and we set the flag before unlocking the clockid
4268 * because if the clockid is unlocked before a current buffer expired, we
4269 * can use that buffer to preroll with */
4270 basesink->need_preroll = TRUE;
4272 if (basesink->clock_id) {
4273 gst_clock_id_unschedule (basesink->clock_id);
4276 /* if we don't have a preroll buffer we need to wait for a preroll and
4278 if (!gst_base_sink_needs_preroll (basesink)) {
4279 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, we are prerolled");
4280 basesink->playing_async = FALSE;
4282 if (GST_STATE_TARGET (GST_ELEMENT (basesink)) <= GST_STATE_READY) {
4283 ret = GST_STATE_CHANGE_SUCCESS;
4285 GST_DEBUG_OBJECT (basesink,
4286 "PLAYING to PAUSED, we are not prerolled");
4287 basesink->playing_async = TRUE;
4288 priv->commited = FALSE;
4289 priv->call_preroll = TRUE;
4290 if (priv->async_enabled) {
4291 GST_DEBUG_OBJECT (basesink, "doing async state change");
4292 ret = GST_STATE_CHANGE_ASYNC;
4293 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4294 gst_message_new_async_start (GST_OBJECT_CAST (basesink),
4299 GST_DEBUG_OBJECT (basesink, "rendered: %" G_GUINT64_FORMAT
4300 ", dropped: %" G_GUINT64_FORMAT, priv->rendered, priv->dropped);
4302 gst_base_sink_reset_qos (basesink);
4303 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4305 case GST_STATE_CHANGE_PAUSED_TO_READY:
4306 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4307 /* start by reseting our position state with the object lock so that the
4308 * position query gets the right idea. We do this before we post the
4309 * messages so that the message handlers pick this up. */
4310 GST_OBJECT_LOCK (basesink);
4311 basesink->have_newsegment = FALSE;
4312 priv->current_sstart = -1;
4313 priv->current_sstop = -1;
4314 priv->have_latency = FALSE;
4315 GST_OBJECT_UNLOCK (basesink);
4317 gst_base_sink_set_last_buffer (basesink, NULL);
4318 priv->call_preroll = FALSE;
4320 if (!priv->commited) {
4321 if (priv->async_enabled) {
4322 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, posting async-done");
4324 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4325 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
4326 GST_STATE_PLAYING, GST_STATE_PAUSED, GST_STATE_READY));
4328 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4329 gst_message_new_async_done (GST_OBJECT_CAST (basesink)));
4331 priv->commited = TRUE;
4333 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, don't need_preroll");
4335 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4337 case GST_STATE_CHANGE_READY_TO_NULL:
4339 if (!bclass->stop (basesink)) {
4340 GST_WARNING_OBJECT (basesink, "failed to stop");
4343 gst_base_sink_set_last_buffer (basesink, NULL);
4344 priv->call_preroll = FALSE;
4355 GST_DEBUG_OBJECT (basesink, "failed to start");
4356 return GST_STATE_CHANGE_FAILURE;
4360 GST_DEBUG_OBJECT (basesink,
4361 "element failed to change states -- activation problem?");
4362 return GST_STATE_CHANGE_FAILURE;