2 * Copyright (C) 2005-2007 Wim Taymans <wim.taymans@gmail.com>
4 * gstbasesink.c: Base class for sink elements
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
24 * @short_description: Base class for sink elements
25 * @see_also: #GstBaseTransform, #GstBaseSource
27 * #GstBaseSink is the base class for sink elements in GStreamer, such as
28 * xvimagesink or filesink. It is a layer on top of #GstElement that provides a
29 * simplified interface to plugin writers. #GstBaseSink handles many details
30 * for you, for example: preroll, clock synchronization, state changes,
31 * activation in push or pull mode, and queries.
33 * In most cases, when writing sink elements, there is no need to implement
34 * class methods from #GstElement or to set functions on pads, because the
35 * #GstBaseSink infrastructure should be sufficient.
37 * #GstBaseSink provides support for exactly one sink pad, which should be
38 * named "sink". A sink implementation (subclass of #GstBaseSink) should
39 * install a pad template in its base_init function, like so:
42 * my_element_base_init (gpointer g_class)
44 * GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
46 * // sinktemplate should be a #GstStaticPadTemplate with direction
47 * // #GST_PAD_SINK and name "sink"
48 * gst_element_class_add_pad_template (gstelement_class,
49 * gst_static_pad_template_get (&sinktemplate));
50 * // see #GstElementDetails
51 * gst_element_class_set_details (gstelement_class, &details);
55 * #GstBaseSink will handle the prerolling correctly. This means that it will
56 * return #GST_STATE_CHANGE_ASYNC from a state change to PAUSED until the first
57 * buffer arrives in this element. The base class will call the
58 * #GstBaseSink::preroll vmethod with this preroll buffer and will then commit
59 * the state change to the next asynchronously pending state.
61 * When the element is set to PLAYING, #GstBaseSink will synchronise on the
62 * clock using the times returned from ::get_times. If this function returns
63 * #GST_CLOCK_TIME_NONE for the start time, no synchronisation will be done.
64 * Synchronisation can be disabled entirely by setting the object "sync"
67 * After synchronisation the virtual method #GstBaseSink::render will be called.
68 * Subclasses should minimally implement this method.
70 * Since 0.10.3 subclasses that synchronise on the clock in the ::render method
71 * are supported as well. These classes typically receive a buffer in the render
72 * method and can then potentially block on the clock while rendering. A typical
73 * example is an audiosink. Since 0.10.11 these subclasses can use
74 * gst_base_sink_wait_preroll() to perform the blocking wait.
76 * Upon receiving the EOS event in the PLAYING state, #GstBaseSink will wait
77 * for the clock to reach the time indicated by the stop time of the last
78 * ::get_times call before posting an EOS message. When the element receives
79 * EOS in PAUSED, preroll completes, the event is queued and an EOS message is
80 * posted when going to PLAYING.
82 * #GstBaseSink will internally use the #GST_EVENT_NEWSEGMENT events to schedule
83 * synchronisation and clipping of buffers. Buffers that fall completely outside
84 * of the current segment are dropped. Buffers that fall partially in the
85 * segment are rendered (and prerolled). Subclasses should do any subbuffer
86 * clipping themselves when needed.
88 * #GstBaseSink will by default report the current playback position in
89 * #GST_FORMAT_TIME based on the current clock time and segment information.
90 * If no clock has been set on the element, the query will be forwarded
93 * The ::set_caps function will be called when the subclass should configure
94 * itself to process a specific media type.
96 * The ::start and ::stop virtual methods will be called when resources should
97 * be allocated. Any ::preroll, ::render and ::set_caps function will be
98 * called between the ::start and ::stop calls.
100 * The ::event virtual method will be called when an event is received by
101 * #GstBaseSink. Normally this method should only be overriden by very specific
102 * elements (such as file sinks) which need to handle the newsegment event
105 * #GstBaseSink provides an overridable ::buffer_alloc function that can be
106 * used by sinks that want to do reverse negotiation or to provide
107 * custom buffers (hardware buffers for example) to upstream elements.
109 * The ::unlock method is called when the elements should unblock any blocking
110 * operations they perform in the ::render method. This is mostly useful when
111 * the ::render method performs a blocking write on a file descriptor, for
114 * The max-lateness property affects how the sink deals with buffers that
115 * arrive too late in the sink. A buffer arrives too late in the sink when
116 * the presentation time (as a combination of the last segment, buffer
117 * timestamp and element base_time) plus the duration is before the current
119 * If the frame is later than max-lateness, the sink will drop the buffer
120 * without calling the render method.
121 * This feature is disabled if sync is disabled, the ::get-times method does
122 * not return a valid start time or max-lateness is set to -1 (the default).
123 * Subclasses can use gst_base_sink_set_max_lateness() to configure the
124 * max-lateness value.
126 * The qos property will enable the quality-of-service features of the basesink
127 * which gather statistics about the real-time performance of the clock
128 * synchronisation. For each buffer received in the sink, statistics are
129 * gathered and a QOS event is sent upstream with these numbers. This
130 * information can then be used by upstream elements to reduce their processing
133 * Since 0.10.15 the async property can be used to instruct the sink to never
134 * perform an ASYNC state change. This feature is mostly usable when dealing
135 * with non-synchronized streams or sparse streams.
137 * Last reviewed on 2007-08-29 (0.10.15)
144 #include "gstbasesink.h"
145 #include <gst/gstmarshal.h>
146 #include <gst/gst_private.h>
147 #include <gst/gst-i18n-lib.h>
149 GST_DEBUG_CATEGORY_STATIC (gst_base_sink_debug);
150 #define GST_CAT_DEFAULT gst_base_sink_debug
152 #define GST_BASE_SINK_GET_PRIVATE(obj) \
153 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_SINK, GstBaseSinkPrivate))
155 /* FIXME, some stuff in ABI.data and other in Private...
156 * Make up your mind please.
158 struct _GstBaseSinkPrivate
160 gint qos_enabled; /* ATOMIC */
161 gboolean async_enabled;
162 GstClockTimeDiff ts_offset;
163 GstClockTime render_delay;
165 /* start, stop of current buffer, stream time, used to report position */
166 GstClockTime current_sstart;
167 GstClockTime current_sstop;
169 /* start, stop and jitter of current buffer, running time */
170 GstClockTime current_rstart;
171 GstClockTime current_rstop;
172 GstClockTimeDiff current_jitter;
174 /* EOS sync time in running time */
175 GstClockTime eos_rtime;
177 /* last buffer that arrived in time, running time */
178 GstClockTime last_in_time;
179 /* when the last buffer left the sink, running time */
180 GstClockTime last_left;
182 /* running averages go here these are done on running time */
184 GstClockTime avg_duration;
187 /* these are done on system time. avg_jitter and avg_render are
188 * compared to eachother to see if the rendering time takes a
189 * huge amount of the processing, If so we are flooded with
191 GstClockTime last_left_systime;
192 GstClockTime avg_jitter;
193 GstClockTime start, stop;
194 GstClockTime avg_render;
196 /* number of rendered and dropped frames */
201 GstClockTime latency;
203 /* if we already commited the state */
206 /* when we received EOS */
207 gboolean received_eos;
209 /* when we are prerolled and able to report latency */
210 gboolean have_latency;
212 /* the last buffer we prerolled or rendered. Useful for making snapshots */
213 GstBuffer *last_buffer;
215 /* caps for pull based scheduling */
218 /* blocksize for pulling */
223 /* seqnum of the stream */
227 #define DO_RUNNING_AVG(avg,val,size) (((val) + ((size)-1) * (avg)) / (size))
229 /* generic running average, this has a neutral window size */
230 #define UPDATE_RUNNING_AVG(avg,val) DO_RUNNING_AVG(avg,val,8)
232 /* the windows for these running averages are experimentally obtained.
233 * possitive values get averaged more while negative values use a small
234 * window so we can react faster to badness. */
235 #define UPDATE_RUNNING_AVG_P(avg,val) DO_RUNNING_AVG(avg,val,16)
236 #define UPDATE_RUNNING_AVG_N(avg,val) DO_RUNNING_AVG(avg,val,4)
238 /* BaseSink properties */
240 #define DEFAULT_CAN_ACTIVATE_PULL FALSE /* fixme: enable me */
241 #define DEFAULT_CAN_ACTIVATE_PUSH TRUE
243 #define DEFAULT_PREROLL_QUEUE_LEN 0
244 #define DEFAULT_SYNC TRUE
245 #define DEFAULT_MAX_LATENESS -1
246 #define DEFAULT_QOS FALSE
247 #define DEFAULT_ASYNC TRUE
248 #define DEFAULT_TS_OFFSET 0
249 #define DEFAULT_BLOCKSIZE 4096
250 #define DEFAULT_RENDER_DELAY 0
255 PROP_PREROLL_QUEUE_LEN,
267 static GstElementClass *parent_class = NULL;
269 static void gst_base_sink_class_init (GstBaseSinkClass * klass);
270 static void gst_base_sink_init (GstBaseSink * trans, gpointer g_class);
271 static void gst_base_sink_finalize (GObject * object);
274 gst_base_sink_get_type (void)
276 static GType base_sink_type = 0;
278 if (G_UNLIKELY (base_sink_type == 0)) {
279 static const GTypeInfo base_sink_info = {
280 sizeof (GstBaseSinkClass),
283 (GClassInitFunc) gst_base_sink_class_init,
286 sizeof (GstBaseSink),
288 (GInstanceInitFunc) gst_base_sink_init,
291 base_sink_type = g_type_register_static (GST_TYPE_ELEMENT,
292 "GstBaseSink", &base_sink_info, G_TYPE_FLAG_ABSTRACT);
294 return base_sink_type;
297 static void gst_base_sink_set_property (GObject * object, guint prop_id,
298 const GValue * value, GParamSpec * pspec);
299 static void gst_base_sink_get_property (GObject * object, guint prop_id,
300 GValue * value, GParamSpec * pspec);
302 static gboolean gst_base_sink_send_event (GstElement * element,
304 static gboolean gst_base_sink_query (GstElement * element, GstQuery * query);
306 static GstCaps *gst_base_sink_get_caps (GstBaseSink * sink);
307 static gboolean gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps);
308 static GstFlowReturn gst_base_sink_buffer_alloc (GstBaseSink * sink,
309 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
310 static void gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
311 GstClockTime * start, GstClockTime * end);
312 static gboolean gst_base_sink_set_flushing (GstBaseSink * basesink,
313 GstPad * pad, gboolean flushing);
314 static gboolean gst_base_sink_default_activate_pull (GstBaseSink * basesink,
316 static gboolean gst_base_sink_default_do_seek (GstBaseSink * sink,
317 GstSegment * segment);
318 static gboolean gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
319 GstEvent * event, GstSegment * segment);
321 static GstStateChangeReturn gst_base_sink_change_state (GstElement * element,
322 GstStateChange transition);
324 static GstFlowReturn gst_base_sink_chain (GstPad * pad, GstBuffer * buffer);
325 static void gst_base_sink_loop (GstPad * pad);
326 static gboolean gst_base_sink_pad_activate (GstPad * pad);
327 static gboolean gst_base_sink_pad_activate_push (GstPad * pad, gboolean active);
328 static gboolean gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active);
329 static gboolean gst_base_sink_event (GstPad * pad, GstEvent * event);
330 static gboolean gst_base_sink_peer_query (GstBaseSink * sink, GstQuery * query);
332 static gboolean gst_base_sink_negotiate_pull (GstBaseSink * basesink);
334 /* check if an object was too late */
335 static gboolean gst_base_sink_is_too_late (GstBaseSink * basesink,
336 GstMiniObject * obj, GstClockTime start, GstClockTime stop,
337 GstClockReturn status, GstClockTimeDiff jitter);
340 gst_base_sink_class_init (GstBaseSinkClass * klass)
342 GObjectClass *gobject_class;
343 GstElementClass *gstelement_class;
345 gobject_class = G_OBJECT_CLASS (klass);
346 gstelement_class = GST_ELEMENT_CLASS (klass);
348 GST_DEBUG_CATEGORY_INIT (gst_base_sink_debug, "basesink", 0,
351 g_type_class_add_private (klass, sizeof (GstBaseSinkPrivate));
353 parent_class = g_type_class_peek_parent (klass);
355 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_sink_finalize);
356 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_base_sink_set_property);
357 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_base_sink_get_property);
359 /* FIXME, this next value should be configured using an event from the
360 * upstream element, ie, the BUFFER_SIZE event. */
361 g_object_class_install_property (gobject_class, PROP_PREROLL_QUEUE_LEN,
362 g_param_spec_uint ("preroll-queue-len", "Preroll queue length",
363 "Number of buffers to queue during preroll", 0, G_MAXUINT,
364 DEFAULT_PREROLL_QUEUE_LEN,
365 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
367 g_object_class_install_property (gobject_class, PROP_SYNC,
368 g_param_spec_boolean ("sync", "Sync", "Sync on the clock", DEFAULT_SYNC,
369 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
371 g_object_class_install_property (gobject_class, PROP_MAX_LATENESS,
372 g_param_spec_int64 ("max-lateness", "Max Lateness",
373 "Maximum number of nanoseconds that a buffer can be late before it "
374 "is dropped (-1 unlimited)", -1, G_MAXINT64, DEFAULT_MAX_LATENESS,
375 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
377 g_object_class_install_property (gobject_class, PROP_QOS,
378 g_param_spec_boolean ("qos", "Qos",
379 "Generate Quality-of-Service events upstream", DEFAULT_QOS,
380 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
384 * If set to #TRUE, the basesink will perform asynchronous state changes.
385 * When set to #FALSE, the sink will not signal the parent when it prerolls.
386 * Use this option when dealing with sparse streams or when synchronisation is
391 g_object_class_install_property (gobject_class, PROP_ASYNC,
392 g_param_spec_boolean ("async", "Async",
393 "Go asynchronously to PAUSED", DEFAULT_ASYNC,
394 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
396 * GstBaseSink:ts-offset
398 * Controls the final synchronisation, a negative value will render the buffer
399 * earlier while a positive value delays playback. This property can be
400 * used to fix synchronisation in bad files.
404 g_object_class_install_property (gobject_class, PROP_TS_OFFSET,
405 g_param_spec_int64 ("ts-offset", "TS Offset",
406 "Timestamp offset in nanoseconds", G_MININT64, G_MAXINT64,
407 DEFAULT_TS_OFFSET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
409 * GstBaseSink:last-buffer
411 * The last buffer that arrived in the sink and was used for preroll or for
412 * rendering. This property can be used to generate thumbnails. This property
413 * can be NULL when the sink has not yet received a bufer.
417 g_object_class_install_property (gobject_class, PROP_LAST_BUFFER,
418 gst_param_spec_mini_object ("last-buffer", "Last Buffer",
419 "The last buffer received in the sink", GST_TYPE_BUFFER,
420 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
422 * GstBaseSink:blocksize
424 * The amount of bytes to pull when operating in pull mode.
428 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
429 g_param_spec_uint ("blocksize", "Block size",
430 "Size in bytes to pull per buffer (0 = default)", 0, G_MAXUINT,
431 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
433 * GstBaseSink:render-delay
435 * The additional delay between synchronisation and actual rendering of the
436 * media. This property will add additional latency to the device in order to
437 * make other sinks compensate for the delay.
441 g_object_class_install_property (gobject_class, PROP_RENDER_DELAY,
442 g_param_spec_uint64 ("render-delay", "Render Delay",
443 "Additional render delay of the sink in nanoseconds", 0, G_MAXUINT64,
444 DEFAULT_RENDER_DELAY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
446 gstelement_class->change_state =
447 GST_DEBUG_FUNCPTR (gst_base_sink_change_state);
448 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_sink_send_event);
449 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_base_sink_query);
451 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_sink_get_caps);
452 klass->set_caps = GST_DEBUG_FUNCPTR (gst_base_sink_set_caps);
453 klass->buffer_alloc = GST_DEBUG_FUNCPTR (gst_base_sink_buffer_alloc);
454 klass->get_times = GST_DEBUG_FUNCPTR (gst_base_sink_get_times);
455 klass->activate_pull =
456 GST_DEBUG_FUNCPTR (gst_base_sink_default_activate_pull);
460 gst_base_sink_pad_getcaps (GstPad * pad)
462 GstBaseSinkClass *bclass;
464 GstCaps *caps = NULL;
466 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
467 bclass = GST_BASE_SINK_GET_CLASS (bsink);
469 if (bsink->pad_mode == GST_ACTIVATE_PULL) {
470 /* if we are operating in pull mode we only accept the negotiated caps */
471 GST_OBJECT_LOCK (pad);
472 if ((caps = GST_PAD_CAPS (pad)))
474 GST_OBJECT_UNLOCK (pad);
477 if (bclass->get_caps)
478 caps = bclass->get_caps (bsink);
481 GstPadTemplate *pad_template;
484 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass),
486 if (pad_template != NULL) {
487 caps = gst_caps_ref (gst_pad_template_get_caps (pad_template));
491 gst_object_unref (bsink);
497 gst_base_sink_pad_setcaps (GstPad * pad, GstCaps * caps)
499 GstBaseSinkClass *bclass;
503 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
504 bclass = GST_BASE_SINK_GET_CLASS (bsink);
506 if (res && bclass->set_caps)
507 res = bclass->set_caps (bsink, caps);
509 gst_object_unref (bsink);
515 gst_base_sink_pad_fixate (GstPad * pad, GstCaps * caps)
517 GstBaseSinkClass *bclass;
520 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
521 bclass = GST_BASE_SINK_GET_CLASS (bsink);
524 bclass->fixate (bsink, caps);
526 gst_object_unref (bsink);
530 gst_base_sink_pad_buffer_alloc (GstPad * pad, guint64 offset, guint size,
531 GstCaps * caps, GstBuffer ** buf)
533 GstBaseSinkClass *bclass;
535 GstFlowReturn result = GST_FLOW_OK;
537 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
538 bclass = GST_BASE_SINK_GET_CLASS (bsink);
540 if (bclass->buffer_alloc)
541 result = bclass->buffer_alloc (bsink, offset, size, caps, buf);
543 *buf = NULL; /* fallback in gstpad.c will allocate generic buffer */
545 gst_object_unref (bsink);
551 gst_base_sink_init (GstBaseSink * basesink, gpointer g_class)
553 GstPadTemplate *pad_template;
554 GstBaseSinkPrivate *priv;
556 basesink->priv = priv = GST_BASE_SINK_GET_PRIVATE (basesink);
559 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
560 g_return_if_fail (pad_template != NULL);
562 basesink->sinkpad = gst_pad_new_from_template (pad_template, "sink");
564 gst_pad_set_getcaps_function (basesink->sinkpad,
565 GST_DEBUG_FUNCPTR (gst_base_sink_pad_getcaps));
566 gst_pad_set_setcaps_function (basesink->sinkpad,
567 GST_DEBUG_FUNCPTR (gst_base_sink_pad_setcaps));
568 gst_pad_set_fixatecaps_function (basesink->sinkpad,
569 GST_DEBUG_FUNCPTR (gst_base_sink_pad_fixate));
570 gst_pad_set_bufferalloc_function (basesink->sinkpad,
571 GST_DEBUG_FUNCPTR (gst_base_sink_pad_buffer_alloc));
572 gst_pad_set_activate_function (basesink->sinkpad,
573 GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate));
574 gst_pad_set_activatepush_function (basesink->sinkpad,
575 GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate_push));
576 gst_pad_set_activatepull_function (basesink->sinkpad,
577 GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate_pull));
578 gst_pad_set_event_function (basesink->sinkpad,
579 GST_DEBUG_FUNCPTR (gst_base_sink_event));
580 gst_pad_set_chain_function (basesink->sinkpad,
581 GST_DEBUG_FUNCPTR (gst_base_sink_chain));
582 gst_element_add_pad (GST_ELEMENT_CAST (basesink), basesink->sinkpad);
584 basesink->pad_mode = GST_ACTIVATE_NONE;
585 basesink->preroll_queue = g_queue_new ();
586 basesink->abidata.ABI.clip_segment = gst_segment_new ();
587 priv->have_latency = FALSE;
589 basesink->can_activate_push = DEFAULT_CAN_ACTIVATE_PUSH;
590 basesink->can_activate_pull = DEFAULT_CAN_ACTIVATE_PULL;
592 basesink->sync = DEFAULT_SYNC;
593 basesink->abidata.ABI.max_lateness = DEFAULT_MAX_LATENESS;
594 g_atomic_int_set (&priv->qos_enabled, DEFAULT_QOS);
595 priv->async_enabled = DEFAULT_ASYNC;
596 priv->ts_offset = DEFAULT_TS_OFFSET;
597 priv->render_delay = DEFAULT_RENDER_DELAY;
598 priv->blocksize = DEFAULT_BLOCKSIZE;
600 GST_OBJECT_FLAG_SET (basesink, GST_ELEMENT_IS_SINK);
604 gst_base_sink_finalize (GObject * object)
606 GstBaseSink *basesink;
608 basesink = GST_BASE_SINK (object);
610 g_queue_free (basesink->preroll_queue);
611 gst_segment_free (basesink->abidata.ABI.clip_segment);
613 G_OBJECT_CLASS (parent_class)->finalize (object);
617 * gst_base_sink_set_sync:
619 * @sync: the new sync value.
621 * Configures @sink to synchronize on the clock or not. When
622 * @sync is FALSE, incomming samples will be played as fast as
623 * possible. If @sync is TRUE, the timestamps of the incomming
624 * buffers will be used to schedule the exact render time of its
630 gst_base_sink_set_sync (GstBaseSink * sink, gboolean sync)
632 g_return_if_fail (GST_IS_BASE_SINK (sink));
634 GST_OBJECT_LOCK (sink);
636 GST_OBJECT_UNLOCK (sink);
640 * gst_base_sink_get_sync:
643 * Checks if @sink is currently configured to synchronize against the
646 * Returns: TRUE if the sink is configured to synchronize against the clock.
651 gst_base_sink_get_sync (GstBaseSink * sink)
655 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
657 GST_OBJECT_LOCK (sink);
659 GST_OBJECT_UNLOCK (sink);
665 * gst_base_sink_set_max_lateness:
667 * @max_lateness: the new max lateness value.
669 * Sets the new max lateness value to @max_lateness. This value is
670 * used to decide if a buffer should be dropped or not based on the
671 * buffer timestamp and the current clock time. A value of -1 means
677 gst_base_sink_set_max_lateness (GstBaseSink * sink, gint64 max_lateness)
679 g_return_if_fail (GST_IS_BASE_SINK (sink));
681 GST_OBJECT_LOCK (sink);
682 sink->abidata.ABI.max_lateness = max_lateness;
683 GST_OBJECT_UNLOCK (sink);
687 * gst_base_sink_get_max_lateness:
690 * Gets the max lateness value. See gst_base_sink_set_max_lateness for
693 * Returns: The maximum time in nanoseconds that a buffer can be late
694 * before it is dropped and not rendered. A value of -1 means an
700 gst_base_sink_get_max_lateness (GstBaseSink * sink)
704 g_return_val_if_fail (GST_IS_BASE_SINK (sink), -1);
706 GST_OBJECT_LOCK (sink);
707 res = sink->abidata.ABI.max_lateness;
708 GST_OBJECT_UNLOCK (sink);
714 * gst_base_sink_set_qos_enabled:
716 * @enabled: the new qos value.
718 * Configures @sink to send Quality-of-Service events upstream.
723 gst_base_sink_set_qos_enabled (GstBaseSink * sink, gboolean enabled)
725 g_return_if_fail (GST_IS_BASE_SINK (sink));
727 g_atomic_int_set (&sink->priv->qos_enabled, enabled);
731 * gst_base_sink_is_qos_enabled:
734 * Checks if @sink is currently configured to send Quality-of-Service events
737 * Returns: TRUE if the sink is configured to perform Quality-of-Service.
742 gst_base_sink_is_qos_enabled (GstBaseSink * sink)
746 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
748 res = g_atomic_int_get (&sink->priv->qos_enabled);
754 * gst_base_sink_set_async_enabled:
756 * @enabled: the new async value.
758 * Configures @sink to perform all state changes asynchronusly. When async is
759 * disabled, the sink will immediatly go to PAUSED instead of waiting for a
760 * preroll buffer. This feature is usefull if the sink does not synchronize
761 * against the clock or when it is dealing with sparse streams.
766 gst_base_sink_set_async_enabled (GstBaseSink * sink, gboolean enabled)
768 g_return_if_fail (GST_IS_BASE_SINK (sink));
770 GST_PAD_PREROLL_LOCK (sink->sinkpad);
771 sink->priv->async_enabled = enabled;
772 GST_LOG_OBJECT (sink, "set async enabled to %d", enabled);
773 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
777 * gst_base_sink_is_async_enabled:
780 * Checks if @sink is currently configured to perform asynchronous state
783 * Returns: TRUE if the sink is configured to perform asynchronous state
789 gst_base_sink_is_async_enabled (GstBaseSink * sink)
793 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
795 GST_PAD_PREROLL_LOCK (sink->sinkpad);
796 res = sink->priv->async_enabled;
797 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
803 * gst_base_sink_set_ts_offset:
805 * @offset: the new offset
807 * Adjust the synchronisation of @sink with @offset. A negative value will
808 * render buffers earlier than their timestamp. A positive value will delay
809 * rendering. This function can be used to fix playback of badly timestamped
815 gst_base_sink_set_ts_offset (GstBaseSink * sink, GstClockTimeDiff offset)
817 g_return_if_fail (GST_IS_BASE_SINK (sink));
819 GST_OBJECT_LOCK (sink);
820 sink->priv->ts_offset = offset;
821 GST_LOG_OBJECT (sink, "set time offset to %" G_GINT64_FORMAT, offset);
822 GST_OBJECT_UNLOCK (sink);
826 * gst_base_sink_get_ts_offset:
829 * Get the synchronisation offset of @sink.
831 * Returns: The synchronisation offset.
836 gst_base_sink_get_ts_offset (GstBaseSink * sink)
838 GstClockTimeDiff res;
840 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
842 GST_OBJECT_LOCK (sink);
843 res = sink->priv->ts_offset;
844 GST_OBJECT_UNLOCK (sink);
850 * gst_base_sink_get_last_buffer:
853 * Get the last buffer that arrived in the sink and was used for preroll or for
854 * rendering. This property can be used to generate thumbnails.
856 * The #GstCaps on the buffer can be used to determine the type of the buffer.
858 * Returns: a #GstBuffer. gst_buffer_unref() after usage. This function returns
859 * NULL when no buffer has arrived in the sink yet or when the sink is not in
865 gst_base_sink_get_last_buffer (GstBaseSink * sink)
869 g_return_val_if_fail (GST_IS_BASE_SINK (sink), NULL);
871 GST_OBJECT_LOCK (sink);
872 if ((res = sink->priv->last_buffer))
873 gst_buffer_ref (res);
874 GST_OBJECT_UNLOCK (sink);
880 gst_base_sink_set_last_buffer (GstBaseSink * sink, GstBuffer * buffer)
885 gst_buffer_ref (buffer);
887 GST_OBJECT_LOCK (sink);
888 old = sink->priv->last_buffer;
889 sink->priv->last_buffer = buffer;
890 GST_OBJECT_UNLOCK (sink);
893 gst_buffer_unref (old);
897 * gst_base_sink_get_latency:
900 * Get the currently configured latency.
902 * Returns: The configured latency.
907 gst_base_sink_get_latency (GstBaseSink * sink)
911 GST_OBJECT_LOCK (sink);
912 res = sink->priv->latency;
913 GST_OBJECT_UNLOCK (sink);
919 * gst_base_sink_query_latency:
921 * @live: if the sink is live
922 * @upstream_live: if an upstream element is live
923 * @min_latency: the min latency of the upstream elements
924 * @max_latency: the max latency of the upstream elements
926 * Query the sink for the latency parameters. The latency will be queried from
927 * the upstream elements. @live will be TRUE if @sink is configured to
928 * synchronize against the clock. @upstream_live will be TRUE if an upstream
931 * If both @live and @upstream_live are TRUE, the sink will want to compensate
932 * for the latency introduced by the upstream elements by setting the
933 * @min_latency to a strictly possitive value.
935 * This function is mostly used by subclasses.
937 * Returns: TRUE if the query succeeded.
942 gst_base_sink_query_latency (GstBaseSink * sink, gboolean * live,
943 gboolean * upstream_live, GstClockTime * min_latency,
944 GstClockTime * max_latency)
946 gboolean l, us_live, res, have_latency;
947 GstClockTime min, max, render_delay;
949 GstClockTime us_min, us_max;
951 /* we are live when we sync to the clock */
952 GST_OBJECT_LOCK (sink);
954 have_latency = sink->priv->have_latency;
955 render_delay = sink->priv->render_delay;
956 GST_OBJECT_UNLOCK (sink);
958 /* assume no latency */
964 GST_DEBUG_OBJECT (sink, "we are ready for LATENCY query");
965 /* we are ready for a latency query this is when we preroll or when we are
967 query = gst_query_new_latency ();
969 /* ask the peer for the latency */
970 if ((res = gst_base_sink_peer_query (sink, query))) {
971 /* get upstream min and max latency */
972 gst_query_parse_latency (query, &us_live, &us_min, &us_max);
975 /* upstream live, use its latency, subclasses should use these
976 * values to create the complete latency. */
981 /* we need to add the render delay if we are live */
988 gst_query_unref (query);
990 GST_DEBUG_OBJECT (sink, "we are not yet ready for LATENCY query");
994 /* not live, we tried to do the query, if it failed we return TRUE anyway */
998 GST_DEBUG_OBJECT (sink, "latency query failed but we are not live");
1000 GST_DEBUG_OBJECT (sink, "latency query failed and we are live");
1005 GST_DEBUG_OBJECT (sink, "latency query: live: %d, have_latency %d,"
1006 " upstream: %d, min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT, l,
1007 have_latency, us_live, GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1012 *upstream_live = us_live;
1022 * gst_base_sink_set_render_delay:
1023 * @sink: a #GstBaseSink
1024 * @delay: the new delay
1026 * Set the render delay in @sink to @delay. The render delay is the time
1027 * between actual rendering of a buffer and its synchronisation time. Some
1028 * devices might delay media rendering which can be compensated for with this
1031 * After calling this function, this sink will report additional latency and
1032 * other sinks will adjust their latency to delay the rendering of their media.
1034 * This function is usually called by subclasses.
1039 gst_base_sink_set_render_delay (GstBaseSink * sink, GstClockTime delay)
1041 GstClockTime old_render_delay;
1043 g_return_if_fail (GST_IS_BASE_SINK (sink));
1045 GST_OBJECT_LOCK (sink);
1046 old_render_delay = sink->priv->render_delay;
1047 sink->priv->render_delay = delay;
1048 GST_LOG_OBJECT (sink, "set render delay to %" GST_TIME_FORMAT,
1049 GST_TIME_ARGS (delay));
1050 GST_OBJECT_UNLOCK (sink);
1052 if (delay != old_render_delay) {
1053 GST_DEBUG_OBJECT (sink, "posting latency changed");
1054 gst_element_post_message (GST_ELEMENT_CAST (sink),
1055 gst_message_new_latency (GST_OBJECT_CAST (sink)));
1060 * gst_base_sink_get_render_delay:
1061 * @sink: a #GstBaseSink
1063 * Get the render delay of @sink. see gst_base_sink_set_render_delay() for more
1064 * information about the render delay.
1066 * Returns: the render delay of @sink.
1071 gst_base_sink_get_render_delay (GstBaseSink * sink)
1073 GstClockTimeDiff res;
1075 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1077 GST_OBJECT_LOCK (sink);
1078 res = sink->priv->render_delay;
1079 GST_OBJECT_UNLOCK (sink);
1085 * gst_base_sink_set_blocksize:
1086 * @sink: a #GstBaseSink
1087 * @blocksize: the blocksize in bytes
1089 * Set the number of bytes that the sink will pull when it is operating in pull
1095 gst_base_sink_set_blocksize (GstBaseSink * sink, guint blocksize)
1097 g_return_if_fail (GST_IS_BASE_SINK (sink));
1099 GST_OBJECT_LOCK (sink);
1100 sink->priv->blocksize = blocksize;
1101 GST_LOG_OBJECT (sink, "set blocksize to %u", blocksize);
1102 GST_OBJECT_UNLOCK (sink);
1106 * gst_base_sink_get_blocksize:
1107 * @sink: a #GstBaseSink
1109 * Get the number of bytes that the sink will pull when it is operating in pull
1112 * Returns: the number of bytes @sink will pull in pull mode.
1117 gst_base_sink_get_blocksize (GstBaseSink * sink)
1121 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1123 GST_OBJECT_LOCK (sink);
1124 res = sink->priv->blocksize;
1125 GST_OBJECT_UNLOCK (sink);
1131 gst_base_sink_set_property (GObject * object, guint prop_id,
1132 const GValue * value, GParamSpec * pspec)
1134 GstBaseSink *sink = GST_BASE_SINK (object);
1137 case PROP_PREROLL_QUEUE_LEN:
1138 /* preroll lock necessary to serialize with finish_preroll */
1139 GST_PAD_PREROLL_LOCK (sink->sinkpad);
1140 sink->preroll_queue_max_len = g_value_get_uint (value);
1141 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
1144 gst_base_sink_set_sync (sink, g_value_get_boolean (value));
1146 case PROP_MAX_LATENESS:
1147 gst_base_sink_set_max_lateness (sink, g_value_get_int64 (value));
1150 gst_base_sink_set_qos_enabled (sink, g_value_get_boolean (value));
1153 gst_base_sink_set_async_enabled (sink, g_value_get_boolean (value));
1155 case PROP_TS_OFFSET:
1156 gst_base_sink_set_ts_offset (sink, g_value_get_int64 (value));
1158 case PROP_BLOCKSIZE:
1159 gst_base_sink_set_blocksize (sink, g_value_get_uint (value));
1161 case PROP_RENDER_DELAY:
1162 gst_base_sink_set_render_delay (sink, g_value_get_uint64 (value));
1165 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1171 gst_base_sink_get_property (GObject * object, guint prop_id, GValue * value,
1174 GstBaseSink *sink = GST_BASE_SINK (object);
1177 case PROP_PREROLL_QUEUE_LEN:
1178 GST_PAD_PREROLL_LOCK (sink->sinkpad);
1179 g_value_set_uint (value, sink->preroll_queue_max_len);
1180 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
1183 g_value_set_boolean (value, gst_base_sink_get_sync (sink));
1185 case PROP_MAX_LATENESS:
1186 g_value_set_int64 (value, gst_base_sink_get_max_lateness (sink));
1189 g_value_set_boolean (value, gst_base_sink_is_qos_enabled (sink));
1192 g_value_set_boolean (value, gst_base_sink_is_async_enabled (sink));
1194 case PROP_TS_OFFSET:
1195 g_value_set_int64 (value, gst_base_sink_get_ts_offset (sink));
1197 case PROP_LAST_BUFFER:
1198 gst_value_take_buffer (value, gst_base_sink_get_last_buffer (sink));
1200 case PROP_BLOCKSIZE:
1201 g_value_set_uint (value, gst_base_sink_get_blocksize (sink));
1203 case PROP_RENDER_DELAY:
1204 g_value_set_uint64 (value, gst_base_sink_get_render_delay (sink));
1207 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1214 gst_base_sink_get_caps (GstBaseSink * sink)
1220 gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps)
1225 static GstFlowReturn
1226 gst_base_sink_buffer_alloc (GstBaseSink * sink, guint64 offset, guint size,
1227 GstCaps * caps, GstBuffer ** buf)
1233 /* with PREROLL_LOCK, STREAM_LOCK */
1235 gst_base_sink_preroll_queue_flush (GstBaseSink * basesink, GstPad * pad)
1239 GST_DEBUG_OBJECT (basesink, "flushing queue %p", basesink);
1240 while ((obj = g_queue_pop_head (basesink->preroll_queue))) {
1241 GST_DEBUG_OBJECT (basesink, "popped %p", obj);
1242 gst_mini_object_unref (obj);
1244 /* we can't have EOS anymore now */
1245 basesink->eos = FALSE;
1246 basesink->priv->received_eos = FALSE;
1247 basesink->have_preroll = FALSE;
1248 basesink->eos_queued = FALSE;
1249 basesink->preroll_queued = 0;
1250 basesink->buffers_queued = 0;
1251 basesink->events_queued = 0;
1252 /* can't report latency anymore until we preroll again */
1253 if (basesink->priv->async_enabled) {
1254 GST_OBJECT_LOCK (basesink);
1255 basesink->priv->have_latency = FALSE;
1256 GST_OBJECT_UNLOCK (basesink);
1258 /* and signal any waiters now */
1259 GST_PAD_PREROLL_SIGNAL (pad);
1262 /* with STREAM_LOCK, configures given segment with the event information. */
1264 gst_base_sink_configure_segment (GstBaseSink * basesink, GstPad * pad,
1265 GstEvent * event, GstSegment * segment)
1268 gdouble rate, arate;
1274 /* the newsegment event is needed to bring the buffer timestamps to the
1275 * stream time and to drop samples outside of the playback segment. */
1276 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1277 &start, &stop, &time);
1279 /* The segment is protected with both the STREAM_LOCK and the OBJECT_LOCK.
1280 * We protect with the OBJECT_LOCK so that we can use the values to
1281 * safely answer a POSITION query. */
1282 GST_OBJECT_LOCK (basesink);
1283 gst_segment_set_newsegment_full (segment, update, rate, arate, format, start,
1286 if (format == GST_FORMAT_TIME) {
1287 GST_DEBUG_OBJECT (basesink,
1288 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1289 "format GST_FORMAT_TIME, "
1290 "%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
1291 ", time %" GST_TIME_FORMAT ", accum %" GST_TIME_FORMAT,
1292 update, rate, arate, GST_TIME_ARGS (segment->start),
1293 GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time),
1294 GST_TIME_ARGS (segment->accum));
1296 GST_DEBUG_OBJECT (basesink,
1297 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1299 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
1300 G_GINT64_FORMAT ", accum %" G_GINT64_FORMAT, update, rate, arate,
1301 segment->format, segment->start, segment->stop, segment->time,
1304 GST_OBJECT_UNLOCK (basesink);
1307 /* with PREROLL_LOCK, STREAM_LOCK */
1309 gst_base_sink_commit_state (GstBaseSink * basesink)
1311 /* commit state and proceed to next pending state */
1312 GstState current, next, pending, post_pending;
1313 gboolean post_paused = FALSE;
1314 gboolean post_async_done = FALSE;
1315 gboolean post_playing = FALSE;
1318 /* we are certainly not playing async anymore now */
1319 basesink->playing_async = FALSE;
1321 GST_OBJECT_LOCK (basesink);
1322 current = GST_STATE (basesink);
1323 next = GST_STATE_NEXT (basesink);
1324 pending = GST_STATE_PENDING (basesink);
1325 post_pending = pending;
1326 sync = basesink->sync;
1329 case GST_STATE_PLAYING:
1331 GstBaseSinkClass *bclass;
1332 GstStateChangeReturn ret;
1334 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1336 GST_DEBUG_OBJECT (basesink, "commiting state to PLAYING");
1338 basesink->need_preroll = FALSE;
1339 post_async_done = TRUE;
1340 basesink->priv->commited = TRUE;
1341 post_playing = TRUE;
1342 /* post PAUSED too when we were READY */
1343 if (current == GST_STATE_READY) {
1347 /* make sure we notify the subclass of async playing */
1348 if (bclass->async_play) {
1349 ret = bclass->async_play (basesink);
1350 if (ret == GST_STATE_CHANGE_FAILURE)
1355 case GST_STATE_PAUSED:
1356 GST_DEBUG_OBJECT (basesink, "commiting state to PAUSED");
1358 post_async_done = TRUE;
1359 basesink->priv->commited = TRUE;
1360 post_pending = GST_STATE_VOID_PENDING;
1362 case GST_STATE_READY:
1363 case GST_STATE_NULL:
1365 case GST_STATE_VOID_PENDING:
1366 goto nothing_pending;
1371 /* we can report latency queries now */
1372 basesink->priv->have_latency = TRUE;
1374 GST_STATE (basesink) = pending;
1375 GST_STATE_NEXT (basesink) = GST_STATE_VOID_PENDING;
1376 GST_STATE_PENDING (basesink) = GST_STATE_VOID_PENDING;
1377 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_SUCCESS;
1378 GST_OBJECT_UNLOCK (basesink);
1381 GST_DEBUG_OBJECT (basesink, "posting PAUSED state change message");
1382 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1383 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1384 current, next, post_pending));
1386 if (post_async_done) {
1387 GST_DEBUG_OBJECT (basesink, "posting async-done message");
1388 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1389 gst_message_new_async_done (GST_OBJECT_CAST (basesink)));
1392 GST_DEBUG_OBJECT (basesink, "posting PLAYING state change message");
1393 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1394 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1395 next, pending, GST_STATE_VOID_PENDING));
1398 GST_STATE_BROADCAST (basesink);
1404 /* Depending on the state, set our vars. We get in this situation when the
1405 * state change function got a change to update the state vars before the
1406 * streaming thread did. This is fine but we need to make sure that we
1407 * update the need_preroll var since it was TRUE when we got here and might
1408 * become FALSE if we got to PLAYING. */
1409 GST_DEBUG_OBJECT (basesink, "nothing to commit, now in %s",
1410 gst_element_state_get_name (current));
1412 case GST_STATE_PLAYING:
1413 basesink->need_preroll = FALSE;
1415 case GST_STATE_PAUSED:
1416 basesink->need_preroll = TRUE;
1419 basesink->need_preroll = FALSE;
1420 basesink->flushing = TRUE;
1423 /* we can report latency queries now */
1424 basesink->priv->have_latency = TRUE;
1425 GST_OBJECT_UNLOCK (basesink);
1430 /* app is going to READY */
1431 GST_DEBUG_OBJECT (basesink, "stopping");
1432 basesink->need_preroll = FALSE;
1433 basesink->flushing = TRUE;
1434 GST_OBJECT_UNLOCK (basesink);
1439 GST_DEBUG_OBJECT (basesink, "async commit failed");
1440 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_FAILURE;
1441 GST_OBJECT_UNLOCK (basesink);
1447 /* with STREAM_LOCK, PREROLL_LOCK
1449 * Returns TRUE if the object needs synchronisation and takes therefore
1450 * part in prerolling.
1452 * rsstart/rsstop contain the start/stop in stream time.
1453 * rrstart/rrstop contain the start/stop in running time.
1456 gst_base_sink_get_sync_times (GstBaseSink * basesink, GstMiniObject * obj,
1457 GstClockTime * rsstart, GstClockTime * rsstop,
1458 GstClockTime * rrstart, GstClockTime * rrstop, gboolean * do_sync,
1459 GstSegment * segment)
1461 GstBaseSinkClass *bclass;
1463 GstClockTime start, stop; /* raw start/stop timestamps */
1464 gint64 cstart, cstop; /* clipped raw timestamps */
1465 gint64 rstart, rstop; /* clipped timestamps converted to running time */
1466 GstClockTime sstart, sstop; /* clipped timestamps converted to stream time */
1468 GstBaseSinkPrivate *priv;
1470 priv = basesink->priv;
1472 /* start with nothing */
1473 start = stop = sstart = sstop = rstart = rstop = -1;
1475 if (G_UNLIKELY (GST_IS_EVENT (obj))) {
1476 GstEvent *event = GST_EVENT_CAST (obj);
1478 switch (GST_EVENT_TYPE (event)) {
1479 /* EOS event needs syncing */
1482 if (basesink->segment.rate >= 0.0) {
1483 sstart = sstop = priv->current_sstop;
1485 /* we have not seen a buffer yet, use the segment values */
1486 sstart = sstop = gst_segment_to_stream_time (&basesink->segment,
1487 basesink->segment.format, basesink->segment.stop);
1490 sstart = sstop = priv->current_sstart;
1492 /* we have not seen a buffer yet, use the segment values */
1493 sstart = sstop = gst_segment_to_stream_time (&basesink->segment,
1494 basesink->segment.format, basesink->segment.start);
1498 rstart = rstop = priv->eos_rtime;
1499 *do_sync = rstart != -1;
1500 GST_DEBUG_OBJECT (basesink, "sync times for EOS %" GST_TIME_FORMAT,
1501 GST_TIME_ARGS (rstart));
1505 /* other events do not need syncing */
1506 /* FIXME, maybe NEWSEGMENT might need synchronisation
1507 * since the POSITION query depends on accumulated times and
1508 * we cannot accumulate the current segment before the previous
1515 /* else do buffer sync code */
1516 buffer = GST_BUFFER_CAST (obj);
1518 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1520 /* just get the times to see if we need syncing */
1521 if (bclass->get_times)
1522 bclass->get_times (basesink, buffer, &start, &stop);
1525 gst_base_sink_get_times (basesink, buffer, &start, &stop);
1531 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
1532 ", stop: %" GST_TIME_FORMAT ", do_sync %d", GST_TIME_ARGS (start),
1533 GST_TIME_ARGS (stop), *do_sync);
1535 /* collect segment and format for code clarity */
1536 format = segment->format;
1538 /* no timestamp clipping if we did not * get a TIME segment format */
1539 if (G_UNLIKELY (format != GST_FORMAT_TIME)) {
1542 /* do running and stream time in TIME format */
1543 format = GST_FORMAT_TIME;
1548 if (G_UNLIKELY (!gst_segment_clip (segment, GST_FORMAT_TIME,
1549 (gint64) start, (gint64) stop, &cstart, &cstop)))
1550 goto out_of_segment;
1552 if (G_UNLIKELY (start != cstart || stop != cstop)) {
1553 GST_DEBUG_OBJECT (basesink, "clipped to: start %" GST_TIME_FORMAT
1554 ", stop: %" GST_TIME_FORMAT, GST_TIME_ARGS (cstart),
1555 GST_TIME_ARGS (cstop));
1558 /* set last stop position */
1559 if (G_LIKELY (cstop != GST_CLOCK_TIME_NONE))
1560 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, cstop);
1562 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, cstart);
1565 /* this can produce wrong values if we accumulated non-TIME segments. If this happens,
1566 * upstream is behaving very badly */
1567 sstart = gst_segment_to_stream_time (segment, format, cstart);
1568 sstop = gst_segment_to_stream_time (segment, format, cstop);
1569 rstart = gst_segment_to_running_time (segment, format, cstart);
1570 rstop = gst_segment_to_running_time (segment, format, cstop);
1579 /* buffers and EOS always need syncing and preroll */
1585 /* should not happen since we clip them in the chain function already,
1586 * we return FALSE so that we don't try to sync on it. */
1587 GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
1588 (NULL), ("unexpected buffer out of segment found."));
1589 GST_LOG_OBJECT (basesink, "buffer skipped, not in segment");
1594 /* with STREAM_LOCK, PREROLL_LOCK, LOCK
1595 * adjust a timestamp with the latency and timestamp offset */
1597 gst_base_sink_adjust_time (GstBaseSink * basesink, GstClockTime time)
1599 GstClockTimeDiff ts_offset;
1601 /* don't do anything funny with invalid timestamps */
1602 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
1605 time += basesink->priv->latency;
1607 /* apply offset, be carefull for underflows */
1608 ts_offset = basesink->priv->ts_offset;
1609 if (ts_offset < 0) {
1610 ts_offset = -ts_offset;
1611 if (ts_offset < time)
1622 * gst_base_sink_wait_clock:
1624 * @time: the running_time to be reached
1625 * @jitter: the jitter to be filled with time diff (can be NULL)
1627 * This function will block until @time is reached. It is usually called by
1628 * subclasses that use their own internal synchronisation.
1630 * If @time is not valid, no sycnhronisation is done and #GST_CLOCK_BADTIME is
1631 * returned. Likewise, if synchronisation is disabled in the element or there
1632 * is no clock, no synchronisation is done and #GST_CLOCK_BADTIME is returned.
1634 * This function should only be called with the PREROLL_LOCK held, like when
1635 * receiving an EOS event in the ::event vmethod or when receiving a buffer in
1636 * the ::render vmethod.
1638 * The @time argument should be the running_time of when this method should
1639 * return and is not adjusted with any latency or offset configured in the
1644 * Returns: #GstClockReturn
1647 gst_base_sink_wait_clock (GstBaseSink * basesink, GstClockTime time,
1648 GstClockTimeDiff * jitter)
1654 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
1657 GST_OBJECT_LOCK (basesink);
1658 if (G_UNLIKELY (!basesink->sync))
1661 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (basesink)) == NULL))
1664 /* add base_time to running_time to get the time against the clock */
1665 time += GST_ELEMENT_CAST (basesink)->base_time;
1667 id = gst_clock_new_single_shot_id (clock, time);
1668 GST_OBJECT_UNLOCK (basesink);
1670 /* A blocking wait is performed on the clock. We save the ClockID
1671 * so we can unlock the entry at any time. While we are blocking, we
1672 * release the PREROLL_LOCK so that other threads can interrupt the
1674 basesink->clock_id = id;
1675 /* release the preroll lock while waiting */
1676 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
1678 ret = gst_clock_id_wait (id, jitter);
1680 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
1681 gst_clock_id_unref (id);
1682 basesink->clock_id = NULL;
1686 /* no syncing needed */
1689 GST_DEBUG_OBJECT (basesink, "time not valid, no sync needed");
1690 return GST_CLOCK_BADTIME;
1694 GST_DEBUG_OBJECT (basesink, "sync disabled");
1695 GST_OBJECT_UNLOCK (basesink);
1696 return GST_CLOCK_BADTIME;
1700 GST_DEBUG_OBJECT (basesink, "no clock, can't sync");
1701 GST_OBJECT_UNLOCK (basesink);
1702 return GST_CLOCK_BADTIME;
1707 * gst_base_sink_wait_preroll:
1710 * If the #GstBaseSinkClass::render method performs its own synchronisation against
1711 * the clock it must unblock when going from PLAYING to the PAUSED state and call
1712 * this method before continuing to render the remaining data.
1714 * This function will block until a state change to PLAYING happens (in which
1715 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
1716 * to a state change to READY or a FLUSH event (in which case this function
1717 * returns #GST_FLOW_WRONG_STATE).
1719 * This function should only be called with the PREROLL_LOCK held, like in the
1724 * Returns: #GST_FLOW_OK if the preroll completed and processing can
1725 * continue. Any other return value should be returned from the render vmethod.
1728 gst_base_sink_wait_preroll (GstBaseSink * sink)
1730 sink->have_preroll = TRUE;
1731 GST_DEBUG_OBJECT (sink, "waiting in preroll for flush or PLAYING");
1732 /* block until the state changes, or we get a flush, or something */
1733 GST_PAD_PREROLL_WAIT (sink->sinkpad);
1734 sink->have_preroll = FALSE;
1735 if (G_UNLIKELY (sink->flushing))
1737 GST_DEBUG_OBJECT (sink, "continue after preroll");
1744 GST_DEBUG_OBJECT (sink, "preroll interrupted");
1745 return GST_FLOW_WRONG_STATE;
1750 * gst_base_sink_do_preroll:
1752 * @obj: the object that caused the preroll
1754 * If the @sink spawns its own thread for pulling buffers from upstream it
1755 * should call this method after it has pulled a buffer. If the element needed
1756 * to preroll, this function will perform the preroll and will then block
1757 * until the element state is changed.
1759 * This function should be called with the PREROLL_LOCK held.
1763 * Returns: #GST_FLOW_OK if the preroll completed and processing can
1764 * continue. Any other return value should be returned from the render vmethod.
1767 gst_base_sink_do_preroll (GstBaseSink * sink, GstMiniObject * obj)
1771 while (G_UNLIKELY (sink->need_preroll)) {
1772 GST_DEBUG_OBJECT (sink, "prerolling object %p", obj);
1774 if (G_LIKELY (sink->playing_async)) {
1776 if (G_UNLIKELY (!gst_base_sink_commit_state (sink)))
1780 /* need to recheck here because the commit state could have
1781 * made us not need the preroll anymore */
1782 if (G_LIKELY (sink->need_preroll)) {
1783 /* block until the state changes, or we get a flush, or something */
1784 ret = gst_base_sink_wait_preroll (sink);
1785 if (ret != GST_FLOW_OK)
1794 GST_DEBUG_OBJECT (sink, "we are flushing");
1799 GST_DEBUG_OBJECT (sink, "stopping while commiting state");
1800 return GST_FLOW_WRONG_STATE;
1805 * gst_base_sink_wait_eos:
1807 * @time: the running_time to be reached
1808 * @jitter: the jitter to be filled with time diff (can be NULL)
1810 * This function will block until @time is reached. It is usually called by
1811 * subclasses that use their own internal synchronisation but want to let the
1812 * EOS be handled by the base class.
1814 * This function should only be called with the PREROLL_LOCK held, like when
1815 * receiving an EOS event in the ::event vmethod.
1817 * The @time argument should be the running_time of when the EOS should happen
1818 * and will be adjusted with any latency and offset configured in the sink.
1822 * Returns: #GstFlowReturn
1825 gst_base_sink_wait_eos (GstBaseSink * sink, GstClockTime time,
1826 GstClockTimeDiff * jitter)
1828 GstClockReturn status;
1834 GST_DEBUG_OBJECT (sink, "checking preroll");
1836 /* first wait for the playing state before we can continue */
1837 if (G_UNLIKELY (sink->need_preroll)) {
1838 ret = gst_base_sink_wait_preroll (sink);
1839 if (ret != GST_FLOW_OK)
1843 /* preroll done, we can sync since we are in PLAYING now. */
1844 GST_DEBUG_OBJECT (sink, "possibly waiting for clock to reach %"
1845 GST_TIME_FORMAT, GST_TIME_ARGS (time));
1847 /* compensate for latency and ts_offset. We don't adjust for render delay
1848 * because we don't interact with the device on EOS normally. */
1849 stime = gst_base_sink_adjust_time (sink, time);
1851 /* wait for the clock, this can be interrupted because we got shut down or
1853 status = gst_base_sink_wait_clock (sink, stime, jitter);
1855 GST_DEBUG_OBJECT (sink, "clock returned %d", status);
1857 /* invalid time, no clock or sync disabled, just continue then */
1858 if (status == GST_CLOCK_BADTIME)
1861 /* waiting could have been interrupted and we can be flushing now */
1862 if (G_UNLIKELY (sink->flushing))
1865 /* retry if we got unscheduled, which means we did not reach the timeout
1866 * yet. if some other error occures, we continue. */
1867 } while (status == GST_CLOCK_UNSCHEDULED);
1869 GST_DEBUG_OBJECT (sink, "end of stream");
1876 GST_DEBUG_OBJECT (sink, "we are flushing");
1877 return GST_FLOW_WRONG_STATE;
1881 /* with STREAM_LOCK, PREROLL_LOCK
1883 * Make sure we are in PLAYING and synchronize an object to the clock.
1885 * If we need preroll, we are not in PLAYING. We try to commit the state
1886 * if needed and then block if we still are not PLAYING.
1888 * We start waiting on the clock in PLAYING. If we got interrupted, we
1889 * immediatly try to re-preroll.
1891 * Some objects do not need synchronisation (most events) and so this function
1892 * immediatly returns GST_FLOW_OK.
1894 * for objects that arrive later than max-lateness to be synchronized to the
1895 * clock have the @late boolean set to TRUE.
1897 * This function keeps a running average of the jitter (the diff between the
1898 * clock time and the requested sync time). The jitter is negative for
1899 * objects that arrive in time and positive for late buffers.
1901 * does not take ownership of obj.
1903 static GstFlowReturn
1904 gst_base_sink_do_sync (GstBaseSink * basesink, GstPad * pad,
1905 GstMiniObject * obj, gboolean * late)
1907 GstClockTimeDiff jitter;
1909 GstClockReturn status = GST_CLOCK_OK;
1910 GstClockTime rstart, rstop, sstart, sstop, stime;
1912 GstBaseSinkPrivate *priv;
1914 priv = basesink->priv;
1916 sstart = sstop = rstart = rstop = -1;
1919 priv->current_rstart = -1;
1921 /* get timing information for this object against the render segment */
1922 syncable = gst_base_sink_get_sync_times (basesink, obj,
1923 &sstart, &sstop, &rstart, &rstop, &do_sync, &basesink->segment);
1925 /* a syncable object needs to participate in preroll and
1926 * clocking. All buffers and EOS are syncable. */
1927 if (G_UNLIKELY (!syncable))
1930 /* store timing info for current object */
1931 priv->current_rstart = rstart;
1932 priv->current_rstop = (rstop != -1 ? rstop : rstart);
1933 /* save sync time for eos when the previous object needed sync */
1934 priv->eos_rtime = (do_sync ? priv->current_rstop : -1);
1937 /* first do preroll, this makes sure we commit our state
1938 * to PAUSED and can continue to PLAYING. We cannot perform
1939 * any clock sync in PAUSED because there is no clock.
1941 while (G_UNLIKELY (basesink->need_preroll)) {
1942 GST_DEBUG_OBJECT (basesink, "prerolling object %p", obj);
1944 if (G_LIKELY (basesink->playing_async)) {
1946 if (G_UNLIKELY (!gst_base_sink_commit_state (basesink)))
1950 /* need to recheck here because the commit state could have
1951 * made us not need the preroll anymore */
1952 if (G_LIKELY (basesink->need_preroll)) {
1953 /* block until the state changes, or we get a flush, or something */
1954 if (gst_base_sink_wait_preroll (basesink) != GST_FLOW_OK)
1959 /* After rendering we store the position of the last buffer so that we can use
1960 * it to report the position. We need to take the lock here. */
1961 GST_OBJECT_LOCK (basesink);
1962 priv->current_sstart = sstart;
1963 priv->current_sstop = (sstop != -1 ? sstop : sstart);
1964 GST_OBJECT_UNLOCK (basesink);
1969 /* adjust for latency */
1970 stime = gst_base_sink_adjust_time (basesink, rstart);
1972 /* adjust for render-delay, avoid underflows */
1974 if (stime > priv->render_delay)
1975 stime -= priv->render_delay;
1980 /* preroll done, we can sync since we are in PLAYING now. */
1981 GST_DEBUG_OBJECT (basesink, "possibly waiting for clock to reach %"
1982 GST_TIME_FORMAT ", adjusted %" GST_TIME_FORMAT,
1983 GST_TIME_ARGS (rstart), GST_TIME_ARGS (stime));
1985 /* This function will return immediatly if start == -1, no clock
1986 * or sync is disabled with GST_CLOCK_BADTIME. */
1987 status = gst_base_sink_wait_clock (basesink, stime, &jitter);
1989 GST_DEBUG_OBJECT (basesink, "clock returned %d", status);
1991 /* invalid time, no clock or sync disabled, just render */
1992 if (status == GST_CLOCK_BADTIME)
1995 /* waiting could have been interrupted and we can be flushing now */
1996 if (G_UNLIKELY (basesink->flushing))
1999 /* check for unlocked by a state change, we are not flushing so
2000 * we can try to preroll on the current buffer. */
2001 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
2002 GST_DEBUG_OBJECT (basesink, "unscheduled, waiting some more");
2006 /* successful syncing done, record observation */
2007 priv->current_jitter = jitter;
2009 /* check if the object should be dropped */
2010 *late = gst_base_sink_is_too_late (basesink, obj, rstart, rstop,
2019 GST_DEBUG_OBJECT (basesink, "non syncable object %p", obj);
2024 GST_DEBUG_OBJECT (basesink, "we are flushing");
2025 return GST_FLOW_WRONG_STATE;
2029 GST_DEBUG_OBJECT (basesink, "stopping while commiting state");
2030 return GST_FLOW_WRONG_STATE;
2035 gst_base_sink_send_qos (GstBaseSink * basesink,
2036 gdouble proportion, GstClockTime time, GstClockTimeDiff diff)
2041 /* generate Quality-of-Service event */
2042 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2043 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2044 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (time));
2046 event = gst_event_new_qos (proportion, diff, time);
2049 res = gst_pad_push_event (basesink->sinkpad, event);
2055 gst_base_sink_perform_qos (GstBaseSink * sink, gboolean dropped)
2057 GstBaseSinkPrivate *priv;
2058 GstClockTime start, stop;
2059 GstClockTimeDiff jitter;
2060 GstClockTime pt, entered, left;
2061 GstClockTime duration;
2066 start = priv->current_rstart;
2068 /* if Quality-of-Service disabled, do nothing */
2069 if (!g_atomic_int_get (&priv->qos_enabled) || start == -1)
2072 stop = priv->current_rstop;
2073 jitter = priv->current_jitter;
2076 /* this is the time the buffer entered the sink */
2077 if (start < -jitter)
2080 entered = start + jitter;
2083 /* this is the time the buffer entered the sink */
2084 entered = start + jitter;
2085 /* this is the time the buffer left the sink */
2086 left = start + jitter;
2089 /* calculate duration of the buffer */
2091 duration = stop - start;
2095 /* if we have the time when the last buffer left us, calculate
2096 * processing time */
2097 if (priv->last_left != -1) {
2098 if (entered > priv->last_left) {
2099 pt = entered - priv->last_left;
2107 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "start: %" GST_TIME_FORMAT
2108 ", entered %" GST_TIME_FORMAT ", left %" GST_TIME_FORMAT ", pt: %"
2109 GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT ",jitter %"
2110 G_GINT64_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (entered),
2111 GST_TIME_ARGS (left), GST_TIME_ARGS (pt), GST_TIME_ARGS (duration),
2114 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "avg_duration: %" GST_TIME_FORMAT
2115 ", avg_pt: %" GST_TIME_FORMAT ", avg_rate: %g",
2116 GST_TIME_ARGS (priv->avg_duration), GST_TIME_ARGS (priv->avg_pt),
2119 /* collect running averages. for first observations, we copy the
2121 if (priv->avg_duration == -1)
2122 priv->avg_duration = duration;
2124 priv->avg_duration = UPDATE_RUNNING_AVG (priv->avg_duration, duration);
2126 if (priv->avg_pt == -1)
2129 priv->avg_pt = UPDATE_RUNNING_AVG (priv->avg_pt, pt);
2131 if (priv->avg_duration != 0)
2133 gst_guint64_to_gdouble (priv->avg_pt) /
2134 gst_guint64_to_gdouble (priv->avg_duration);
2138 if (priv->last_left != -1) {
2139 if (dropped || priv->avg_rate < 0.0) {
2140 priv->avg_rate = rate;
2143 priv->avg_rate = UPDATE_RUNNING_AVG_N (priv->avg_rate, rate);
2145 priv->avg_rate = UPDATE_RUNNING_AVG_P (priv->avg_rate, rate);
2149 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink,
2150 "updated: avg_duration: %" GST_TIME_FORMAT ", avg_pt: %" GST_TIME_FORMAT
2151 ", avg_rate: %g", GST_TIME_ARGS (priv->avg_duration),
2152 GST_TIME_ARGS (priv->avg_pt), priv->avg_rate);
2155 if (priv->avg_rate >= 0.0) {
2156 /* if we have a valid rate, start sending QoS messages */
2157 if (priv->current_jitter < 0) {
2158 /* make sure we never go below 0 when adding the jitter to the
2160 if (priv->current_rstart < -priv->current_jitter)
2161 priv->current_jitter = -priv->current_rstart;
2163 gst_base_sink_send_qos (sink, priv->avg_rate, priv->current_rstart,
2164 priv->current_jitter);
2167 /* record when this buffer will leave us */
2168 priv->last_left = left;
2171 /* reset all qos measuring */
2173 gst_base_sink_reset_qos (GstBaseSink * sink)
2175 GstBaseSinkPrivate *priv;
2179 priv->last_in_time = -1;
2180 priv->last_left = -1;
2181 priv->avg_duration = -1;
2183 priv->avg_rate = -1.0;
2184 priv->avg_render = -1;
2190 /* Checks if the object was scheduled too late.
2192 * start/stop contain the raw timestamp start and stop values
2195 * status and jitter contain the return values from the clock wait.
2197 * returns TRUE if the buffer was too late.
2200 gst_base_sink_is_too_late (GstBaseSink * basesink, GstMiniObject * obj,
2201 GstClockTime start, GstClockTime stop,
2202 GstClockReturn status, GstClockTimeDiff jitter)
2205 gint64 max_lateness;
2206 GstBaseSinkPrivate *priv;
2208 priv = basesink->priv;
2212 /* only for objects that were too late */
2213 if (G_LIKELY (status != GST_CLOCK_EARLY))
2216 max_lateness = basesink->abidata.ABI.max_lateness;
2218 /* check if frame dropping is enabled */
2219 if (max_lateness == -1)
2222 /* only check for buffers */
2223 if (G_UNLIKELY (!GST_IS_BUFFER (obj)))
2226 /* can't do check if we don't have a timestamp */
2227 if (G_UNLIKELY (start == -1))
2230 /* we can add a valid stop time */
2232 max_lateness += stop;
2234 max_lateness += start;
2236 /* if the jitter bigger than duration and lateness we are too late */
2237 if ((late = start + jitter > max_lateness)) {
2238 GST_DEBUG_OBJECT (basesink, "buffer is too late %" GST_TIME_FORMAT
2239 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (start + jitter),
2240 GST_TIME_ARGS (max_lateness));
2241 /* !!emergency!!, if we did not receive anything valid for more than a
2242 * second, render it anyway so the user sees something */
2243 if (priv->last_in_time && start - priv->last_in_time > GST_SECOND) {
2245 GST_DEBUG_OBJECT (basesink,
2246 "**emergency** last buffer at %" GST_TIME_FORMAT " > GST_SECOND",
2247 GST_TIME_ARGS (priv->last_in_time));
2253 priv->last_in_time = start;
2260 GST_DEBUG_OBJECT (basesink, "object was scheduled in time");
2265 GST_DEBUG_OBJECT (basesink, "frame dropping disabled");
2270 GST_DEBUG_OBJECT (basesink, "object is not a buffer");
2275 GST_DEBUG_OBJECT (basesink, "buffer has no timestamp");
2280 /* called before and after calling the render vmethod. It keeps track of how
2281 * much time was spent in the render method and is used to check if we are
2284 gst_base_sink_do_render_stats (GstBaseSink * basesink, gboolean start)
2286 GstBaseSinkPrivate *priv;
2288 priv = basesink->priv;
2291 priv->start = gst_util_get_timestamp ();
2293 GstClockTime elapsed;
2295 priv->stop = gst_util_get_timestamp ();
2297 elapsed = GST_CLOCK_DIFF (priv->start, priv->stop);
2299 if (priv->avg_render == -1)
2300 priv->avg_render = elapsed;
2302 priv->avg_render = UPDATE_RUNNING_AVG (priv->avg_render, elapsed);
2304 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2305 "avg_render: %" GST_TIME_FORMAT, GST_TIME_ARGS (priv->avg_render));
2309 /* with STREAM_LOCK, PREROLL_LOCK,
2311 * Synchronize the object on the clock and then render it.
2313 * takes ownership of obj.
2315 static GstFlowReturn
2316 gst_base_sink_render_object (GstBaseSink * basesink, GstPad * pad,
2317 GstMiniObject * obj)
2319 GstFlowReturn ret = GST_FLOW_OK;
2320 GstBaseSinkClass *bclass;
2321 gboolean late = FALSE;
2322 GstBaseSinkPrivate *priv;
2324 priv = basesink->priv;
2326 /* synchronize this object, non syncable objects return OK
2328 ret = gst_base_sink_do_sync (basesink, pad, obj, &late);
2329 if (G_UNLIKELY (ret != GST_FLOW_OK))
2332 /* and now render, event or buffer. */
2333 if (G_LIKELY (GST_IS_BUFFER (obj))) {
2336 /* drop late buffers unconditionally, let's hope it's unlikely */
2337 if (G_UNLIKELY (late))
2340 buf = GST_BUFFER_CAST (obj);
2342 gst_base_sink_set_last_buffer (basesink, buf);
2344 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2346 if (G_LIKELY (bclass->render)) {
2349 /* read once, to get same value before and after */
2350 do_qos = g_atomic_int_get (&priv->qos_enabled);
2352 GST_DEBUG_OBJECT (basesink, "rendering buffer %p", obj);
2354 /* record rendering time for QoS and stats */
2356 gst_base_sink_do_render_stats (basesink, TRUE);
2358 ret = bclass->render (basesink, buf);
2363 gst_base_sink_do_render_stats (basesink, FALSE);
2366 GstEvent *event = GST_EVENT_CAST (obj);
2367 gboolean event_res = TRUE;
2370 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2372 type = GST_EVENT_TYPE (event);
2374 GST_DEBUG_OBJECT (basesink, "rendering event %p, type %s", obj,
2375 gst_event_type_get_name (type));
2378 event_res = bclass->event (basesink, event);
2380 /* when we get here we could be flushing again when the event handler calls
2381 * _wait_eos(). We have to ignore this object in that case. */
2382 if (G_UNLIKELY (basesink->flushing))
2385 if (G_LIKELY (event_res)) {
2388 seqnum = basesink->priv->seqnum = gst_event_get_seqnum (event);
2389 GST_DEBUG_OBJECT (basesink, "Got seqnum #%" G_GUINT32_FORMAT, seqnum);
2394 GstMessage *message;
2396 /* the EOS event is completely handled so we mark
2397 * ourselves as being in the EOS state. eos is also
2398 * protected by the object lock so we can read it when
2399 * answering the POSITION query. */
2400 GST_OBJECT_LOCK (basesink);
2401 basesink->eos = TRUE;
2402 GST_OBJECT_UNLOCK (basesink);
2404 /* ok, now we can post the message */
2405 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
2407 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
2408 gst_message_set_seqnum (message, seqnum);
2409 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
2412 case GST_EVENT_NEWSEGMENT:
2413 /* configure the segment */
2414 gst_base_sink_configure_segment (basesink, pad, event,
2415 &basesink->segment);
2424 gst_base_sink_perform_qos (basesink, late);
2426 GST_DEBUG_OBJECT (basesink, "object unref after render %p", obj);
2427 gst_mini_object_unref (obj);
2434 GST_DEBUG_OBJECT (basesink, "do_sync returned %s", gst_flow_get_name (ret));
2440 GST_DEBUG_OBJECT (basesink, "buffer late, dropping");
2445 GST_DEBUG_OBJECT (basesink, "we are flushing, ignore object");
2446 gst_mini_object_unref (obj);
2447 return GST_FLOW_WRONG_STATE;
2451 /* with STREAM_LOCK, PREROLL_LOCK
2453 * Perform preroll on the given object. For buffers this means
2454 * calling the preroll subclass method.
2455 * If that succeeds, the state will be commited.
2457 * function does not take ownership of obj.
2459 static GstFlowReturn
2460 gst_base_sink_preroll_object (GstBaseSink * basesink, GstPad * pad,
2461 GstMiniObject * obj)
2465 GST_DEBUG_OBJECT (basesink, "do preroll %p", obj);
2467 /* if it's a buffer, we need to call the preroll method */
2468 if (G_LIKELY (GST_IS_BUFFER (obj))) {
2469 GstBaseSinkClass *bclass;
2471 GstClockTime timestamp;
2473 buf = GST_BUFFER_CAST (obj);
2474 timestamp = GST_BUFFER_TIMESTAMP (buf);
2476 GST_DEBUG_OBJECT (basesink, "preroll buffer %" GST_TIME_FORMAT,
2477 GST_TIME_ARGS (timestamp));
2479 gst_base_sink_set_last_buffer (basesink, buf);
2481 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2482 if (bclass->preroll)
2483 if ((ret = bclass->preroll (basesink, buf)) != GST_FLOW_OK)
2484 goto preroll_failed;
2488 if (G_LIKELY (basesink->playing_async)) {
2489 if (G_UNLIKELY (!gst_base_sink_commit_state (basesink)))
2498 GST_DEBUG_OBJECT (basesink, "preroll failed, abort state");
2499 gst_element_abort_state (GST_ELEMENT_CAST (basesink));
2504 GST_DEBUG_OBJECT (basesink, "stopping while commiting state");
2505 return GST_FLOW_WRONG_STATE;
2509 /* with STREAM_LOCK, PREROLL_LOCK
2511 * Queue an object for rendering.
2512 * The first prerollable object queued will complete the preroll. If the
2513 * preroll queue if filled, we render all the objects in the queue.
2515 * This function takes ownership of the object.
2517 static GstFlowReturn
2518 gst_base_sink_queue_object_unlocked (GstBaseSink * basesink, GstPad * pad,
2519 GstMiniObject * obj, gboolean prerollable)
2521 GstFlowReturn ret = GST_FLOW_OK;
2525 if (G_UNLIKELY (basesink->need_preroll)) {
2526 if (G_LIKELY (prerollable))
2527 basesink->preroll_queued++;
2529 length = basesink->preroll_queued;
2531 GST_DEBUG_OBJECT (basesink, "now %d prerolled items", length);
2533 /* first prerollable item needs to finish the preroll */
2535 ret = gst_base_sink_preroll_object (basesink, pad, obj);
2536 if (G_UNLIKELY (ret != GST_FLOW_OK))
2537 goto preroll_failed;
2539 /* need to recheck if we need preroll, commmit state during preroll
2540 * could have made us not need more preroll. */
2541 if (G_UNLIKELY (basesink->need_preroll)) {
2542 /* see if we can render now, if we can't add the object to the preroll
2544 if (G_UNLIKELY (length <= basesink->preroll_queue_max_len))
2549 /* we can start rendering (or blocking) the queued object
2551 q = basesink->preroll_queue;
2552 while (G_UNLIKELY (!g_queue_is_empty (q))) {
2555 o = g_queue_pop_head (q);
2556 GST_DEBUG_OBJECT (basesink, "rendering queued object %p", o);
2558 /* do something with the return value */
2559 ret = gst_base_sink_render_object (basesink, pad, o);
2560 if (ret != GST_FLOW_OK)
2561 goto dequeue_failed;
2564 /* now render the object */
2565 ret = gst_base_sink_render_object (basesink, pad, obj);
2566 basesink->preroll_queued = 0;
2573 GST_DEBUG_OBJECT (basesink, "preroll failed, reason %s",
2574 gst_flow_get_name (ret));
2575 gst_mini_object_unref (obj);
2580 /* add object to the queue and return */
2581 GST_DEBUG_OBJECT (basesink, "need more preroll data %d <= %d",
2582 length, basesink->preroll_queue_max_len);
2583 g_queue_push_tail (basesink->preroll_queue, obj);
2588 GST_DEBUG_OBJECT (basesink, "rendering queued objects failed, reason %s",
2589 gst_flow_get_name (ret));
2590 gst_mini_object_unref (obj);
2597 * This function grabs the PREROLL_LOCK and adds the object to
2600 * This function takes ownership of obj.
2602 static GstFlowReturn
2603 gst_base_sink_queue_object (GstBaseSink * basesink, GstPad * pad,
2604 GstMiniObject * obj, gboolean prerollable)
2608 GST_PAD_PREROLL_LOCK (pad);
2609 if (G_UNLIKELY (basesink->flushing))
2612 if (G_UNLIKELY (basesink->priv->received_eos))
2615 ret = gst_base_sink_queue_object_unlocked (basesink, pad, obj, prerollable);
2616 GST_PAD_PREROLL_UNLOCK (pad);
2623 GST_DEBUG_OBJECT (basesink, "sink is flushing");
2624 GST_PAD_PREROLL_UNLOCK (pad);
2625 gst_mini_object_unref (obj);
2626 return GST_FLOW_WRONG_STATE;
2630 GST_DEBUG_OBJECT (basesink,
2631 "we are EOS, dropping object, return UNEXPECTED");
2632 GST_PAD_PREROLL_UNLOCK (pad);
2633 gst_mini_object_unref (obj);
2634 return GST_FLOW_UNEXPECTED;
2639 gst_base_sink_flush_start (GstBaseSink * basesink, GstPad * pad)
2641 /* make sure we are not blocked on the clock also clear any pending
2643 gst_base_sink_set_flushing (basesink, pad, TRUE);
2645 /* we grab the stream lock but that is not needed since setting the
2646 * sink to flushing would make sure no state commit is being done
2648 GST_PAD_STREAM_LOCK (pad);
2649 gst_base_sink_reset_qos (basesink);
2650 if (basesink->priv->async_enabled) {
2651 /* and we need to commit our state again on the next
2652 * prerolled buffer */
2653 basesink->playing_async = TRUE;
2654 gst_element_lost_state (GST_ELEMENT_CAST (basesink));
2656 basesink->priv->have_latency = TRUE;
2657 basesink->need_preroll = FALSE;
2659 gst_base_sink_set_last_buffer (basesink, NULL);
2660 GST_PAD_STREAM_UNLOCK (pad);
2664 gst_base_sink_flush_stop (GstBaseSink * basesink, GstPad * pad)
2666 /* unset flushing so we can accept new data, this also flushes out any EOS
2668 gst_base_sink_set_flushing (basesink, pad, FALSE);
2670 /* for position reporting */
2671 GST_OBJECT_LOCK (basesink);
2672 basesink->priv->current_sstart = -1;
2673 basesink->priv->current_sstop = -1;
2674 basesink->priv->eos_rtime = -1;
2675 if (basesink->pad_mode == GST_ACTIVATE_PUSH) {
2676 /* we need new segment info after the flush. */
2677 basesink->have_newsegment = FALSE;
2678 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
2679 gst_segment_init (basesink->abidata.ABI.clip_segment, GST_FORMAT_UNDEFINED);
2681 GST_OBJECT_UNLOCK (basesink);
2685 gst_base_sink_event (GstPad * pad, GstEvent * event)
2687 GstBaseSink *basesink;
2688 gboolean result = TRUE;
2689 GstBaseSinkClass *bclass;
2691 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
2693 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2695 GST_DEBUG_OBJECT (basesink, "event %p (%s)", event,
2696 GST_EVENT_TYPE_NAME (event));
2698 switch (GST_EVENT_TYPE (event)) {
2703 GST_PAD_PREROLL_LOCK (pad);
2704 if (G_UNLIKELY (basesink->flushing))
2707 if (G_UNLIKELY (basesink->priv->received_eos)) {
2708 /* we can't accept anything when we are EOS */
2710 gst_event_unref (event);
2712 /* we set the received EOS flag here so that we can use it when testing if
2713 * we are prerolled and to refuse more buffers. */
2714 basesink->priv->received_eos = TRUE;
2716 /* EOS is a prerollable object, we call the unlocked version because it
2717 * does not check the received_eos flag. */
2718 ret = gst_base_sink_queue_object_unlocked (basesink, pad,
2719 GST_MINI_OBJECT_CAST (event), TRUE);
2720 if (G_UNLIKELY (ret != GST_FLOW_OK))
2723 GST_PAD_PREROLL_UNLOCK (pad);
2726 case GST_EVENT_NEWSEGMENT:
2730 GST_DEBUG_OBJECT (basesink, "newsegment %p", event);
2732 GST_PAD_PREROLL_LOCK (pad);
2733 if (G_UNLIKELY (basesink->flushing))
2736 if (G_UNLIKELY (basesink->priv->received_eos)) {
2737 /* we can't accept anything when we are EOS */
2739 gst_event_unref (event);
2741 /* the new segment is a non prerollable item and does not block anything,
2742 * we need to configure the current clipping segment and insert the event
2743 * in the queue to serialize it with the buffers for rendering. */
2744 gst_base_sink_configure_segment (basesink, pad, event,
2745 basesink->abidata.ABI.clip_segment);
2748 gst_base_sink_queue_object_unlocked (basesink, pad,
2749 GST_MINI_OBJECT_CAST (event), FALSE);
2750 if (G_UNLIKELY (ret != GST_FLOW_OK))
2753 GST_OBJECT_LOCK (basesink);
2754 basesink->have_newsegment = TRUE;
2755 GST_OBJECT_UNLOCK (basesink);
2758 GST_PAD_PREROLL_UNLOCK (pad);
2761 case GST_EVENT_FLUSH_START:
2763 bclass->event (basesink, event);
2765 GST_DEBUG_OBJECT (basesink, "flush-start %p", event);
2767 gst_base_sink_flush_start (basesink, pad);
2769 gst_event_unref (event);
2771 case GST_EVENT_FLUSH_STOP:
2773 bclass->event (basesink, event);
2775 GST_DEBUG_OBJECT (basesink, "flush-stop %p", event);
2777 gst_base_sink_flush_stop (basesink, pad);
2779 gst_event_unref (event);
2782 /* other events are sent to queue or subclass depending on if they
2783 * are serialized. */
2784 if (GST_EVENT_IS_SERIALIZED (event)) {
2785 gst_base_sink_queue_object (basesink, pad,
2786 GST_MINI_OBJECT_CAST (event), FALSE);
2789 bclass->event (basesink, event);
2790 gst_event_unref (event);
2795 gst_object_unref (basesink);
2802 GST_DEBUG_OBJECT (basesink, "we are flushing");
2803 GST_PAD_PREROLL_UNLOCK (pad);
2805 gst_event_unref (event);
2810 /* default implementation to calculate the start and end
2811 * timestamps on a buffer, subclasses can override
2814 gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
2815 GstClockTime * start, GstClockTime * end)
2817 GstClockTime timestamp, duration;
2819 timestamp = GST_BUFFER_TIMESTAMP (buffer);
2820 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
2822 /* get duration to calculate end time */
2823 duration = GST_BUFFER_DURATION (buffer);
2824 if (GST_CLOCK_TIME_IS_VALID (duration)) {
2825 *end = timestamp + duration;
2831 /* must be called with PREROLL_LOCK */
2833 gst_base_sink_needs_preroll (GstBaseSink * basesink)
2835 gboolean is_prerolled, res;
2837 /* we have 2 cases where the PREROLL_LOCK is released:
2838 * 1) we are blocking in the PREROLL_LOCK and thus are prerolled.
2839 * 2) we are syncing on the clock
2841 is_prerolled = basesink->have_preroll || basesink->priv->received_eos;
2842 res = !is_prerolled;
2844 GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d => needs preroll: %d",
2845 basesink->have_preroll, basesink->priv->received_eos, res);
2850 /* with STREAM_LOCK, PREROLL_LOCK
2852 * Takes a buffer and compare the timestamps with the last segment.
2853 * If the buffer falls outside of the segment boundaries, drop it.
2854 * Else queue the buffer for preroll and rendering.
2856 * This function takes ownership of the buffer.
2858 static GstFlowReturn
2859 gst_base_sink_chain_unlocked (GstBaseSink * basesink, GstPad * pad,
2862 GstBaseSinkClass *bclass;
2863 GstFlowReturn result;
2864 GstClockTime start = GST_CLOCK_TIME_NONE, end = GST_CLOCK_TIME_NONE;
2865 GstSegment *clip_segment;
2867 if (G_UNLIKELY (basesink->flushing))
2870 if (G_UNLIKELY (basesink->priv->received_eos))
2873 /* for code clarity */
2874 clip_segment = basesink->abidata.ABI.clip_segment;
2876 if (G_UNLIKELY (!basesink->have_newsegment)) {
2879 sync = gst_base_sink_get_sync (basesink);
2881 GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
2882 (_("Internal data flow problem.")),
2883 ("Received buffer without a new-segment. Assuming timestamps start from 0."));
2886 /* this means this sink will assume timestamps start from 0 */
2887 GST_OBJECT_LOCK (basesink);
2888 clip_segment->start = 0;
2889 clip_segment->stop = -1;
2890 basesink->segment.start = 0;
2891 basesink->segment.stop = -1;
2892 basesink->have_newsegment = TRUE;
2893 GST_OBJECT_UNLOCK (basesink);
2896 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2898 /* check if the buffer needs to be dropped, we first ask the subclass for the
2900 if (bclass->get_times)
2901 bclass->get_times (basesink, buf, &start, &end);
2904 /* if the subclass does not want sync, we use our own values so that we at
2905 * least clip the buffer to the segment */
2906 gst_base_sink_get_times (basesink, buf, &start, &end);
2909 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
2910 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
2912 /* a dropped buffer does not participate in anything */
2913 if (GST_CLOCK_TIME_IS_VALID (start) &&
2914 (clip_segment->format == GST_FORMAT_TIME)) {
2915 if (G_UNLIKELY (!gst_segment_clip (clip_segment,
2916 GST_FORMAT_TIME, (gint64) start, (gint64) end, NULL, NULL)))
2917 goto out_of_segment;
2920 /* now we can process the buffer in the queue, this function takes ownership
2922 result = gst_base_sink_queue_object_unlocked (basesink, pad,
2923 GST_MINI_OBJECT_CAST (buf), TRUE);
2930 GST_DEBUG_OBJECT (basesink, "sink is flushing");
2931 gst_buffer_unref (buf);
2932 return GST_FLOW_WRONG_STATE;
2936 GST_DEBUG_OBJECT (basesink,
2937 "we are EOS, dropping object, return UNEXPECTED");
2938 gst_buffer_unref (buf);
2939 return GST_FLOW_UNEXPECTED;
2943 GST_DEBUG_OBJECT (basesink, "dropping buffer, out of clipping segment");
2944 gst_buffer_unref (buf);
2951 static GstFlowReturn
2952 gst_base_sink_chain (GstPad * pad, GstBuffer * buf)
2954 GstBaseSink *basesink;
2955 GstFlowReturn result;
2957 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
2959 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH))
2962 GST_PAD_PREROLL_LOCK (pad);
2963 result = gst_base_sink_chain_unlocked (basesink, pad, buf);
2964 GST_PAD_PREROLL_UNLOCK (pad);
2972 GST_OBJECT_LOCK (pad);
2973 GST_WARNING_OBJECT (basesink,
2974 "Push on pad %s:%s, but it was not activated in push mode",
2975 GST_DEBUG_PAD_NAME (pad));
2976 GST_OBJECT_UNLOCK (pad);
2977 gst_buffer_unref (buf);
2978 /* we don't post an error message this will signal to the peer
2979 * pushing that EOS is reached. */
2980 result = GST_FLOW_UNEXPECTED;
2986 gst_base_sink_default_do_seek (GstBaseSink * sink, GstSegment * segment)
2988 gboolean res = TRUE;
2990 /* update our offset if the start/stop position was updated */
2991 if (segment->format == GST_FORMAT_BYTES) {
2992 segment->time = segment->start;
2993 } else if (segment->start == 0) {
2994 /* seek to start, we can implement a default for this. */
2998 GST_INFO_OBJECT (sink, "Can't do a default seek");
3004 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
3007 gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
3008 GstEvent * event, GstSegment * segment)
3010 /* By default, we try one of 2 things:
3011 * - For absolute seek positions, convert the requested position to our
3012 * configured processing format and place it in the output segment \
3013 * - For relative seek positions, convert our current (input) values to the
3014 * seek format, adjust by the relative seek offset and then convert back to
3015 * the processing format
3017 GstSeekType cur_type, stop_type;
3020 GstFormat seek_format, dest_format;
3023 gboolean res = TRUE;
3025 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3026 &cur_type, &cur, &stop_type, &stop);
3027 dest_format = segment->format;
3029 if (seek_format == dest_format) {
3030 gst_segment_set_seek (segment, rate, seek_format, flags,
3031 cur_type, cur, stop_type, stop, &update);
3035 if (cur_type != GST_SEEK_TYPE_NONE) {
3036 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3038 gst_pad_query_convert (sink->sinkpad, seek_format, cur, &dest_format,
3040 cur_type = GST_SEEK_TYPE_SET;
3043 if (res && stop_type != GST_SEEK_TYPE_NONE) {
3044 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3046 gst_pad_query_convert (sink->sinkpad, seek_format, stop, &dest_format,
3048 stop_type = GST_SEEK_TYPE_SET;
3051 /* And finally, configure our output segment in the desired format */
3052 gst_segment_set_seek (segment, rate, dest_format, flags, cur_type, cur,
3053 stop_type, stop, &update);
3062 GST_DEBUG_OBJECT (sink, "undefined format given, seek aborted.");
3067 /* perform a seek, only executed in pull mode */
3069 gst_base_sink_perform_seek (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3073 GstFormat seek_format, dest_format;
3075 GstSeekType cur_type, stop_type;
3076 gboolean seekseg_configured = FALSE;
3078 gboolean update, res = TRUE;
3079 GstSegment seeksegment;
3081 dest_format = sink->segment.format;
3084 GST_DEBUG_OBJECT (sink, "performing seek with event %p", event);
3085 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3086 &cur_type, &cur, &stop_type, &stop);
3088 flush = flags & GST_SEEK_FLAG_FLUSH;
3090 GST_DEBUG_OBJECT (sink, "performing seek without event");
3095 GST_DEBUG_OBJECT (sink, "flushing upstream");
3096 gst_pad_push_event (pad, gst_event_new_flush_start ());
3097 gst_base_sink_flush_start (sink, pad);
3099 GST_DEBUG_OBJECT (sink, "pausing pulling thread");
3102 GST_PAD_STREAM_LOCK (pad);
3104 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
3105 * copy the current segment info into the temp segment that we can actually
3106 * attempt the seek with. We only update the real segment if the seek suceeds. */
3107 if (!seekseg_configured) {
3108 memcpy (&seeksegment, &sink->segment, sizeof (GstSegment));
3110 /* now configure the final seek segment */
3112 if (sink->segment.format != seek_format) {
3113 /* OK, here's where we give the subclass a chance to convert the relative
3114 * seek into an absolute one in the processing format. We set up any
3115 * absolute seek above, before taking the stream lock. */
3116 if (!gst_base_sink_default_prepare_seek_segment (sink, event,
3118 GST_DEBUG_OBJECT (sink,
3119 "Preparing the seek failed after flushing. " "Aborting seek");
3123 /* The seek format matches our processing format, no need to ask the
3124 * the subclass to configure the segment. */
3125 gst_segment_set_seek (&seeksegment, rate, seek_format, flags,
3126 cur_type, cur, stop_type, stop, &update);
3129 /* Else, no seek event passed, so we're just (re)starting the
3134 GST_DEBUG_OBJECT (sink, "segment configured from %" G_GINT64_FORMAT
3135 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
3136 seeksegment.start, seeksegment.stop, seeksegment.last_stop);
3138 /* do the seek, segment.last_stop contains the new position. */
3139 res = gst_base_sink_default_do_seek (sink, &seeksegment);
3144 GST_DEBUG_OBJECT (sink, "stop flushing upstream");
3145 gst_pad_push_event (pad, gst_event_new_flush_stop ());
3146 gst_base_sink_flush_stop (sink, pad);
3147 } else if (res && sink->abidata.ABI.running) {
3148 /* we are running the current segment and doing a non-flushing seek,
3149 * close the segment first based on the last_stop. */
3150 GST_DEBUG_OBJECT (sink, "closing running segment %" G_GINT64_FORMAT
3151 " to %" G_GINT64_FORMAT, sink->segment.start, sink->segment.last_stop);
3154 /* The subclass must have converted the segment to the processing format
3156 if (res && seeksegment.format != dest_format) {
3157 GST_DEBUG_OBJECT (sink, "Subclass failed to prepare a seek segment "
3158 "in the correct format. Aborting seek.");
3162 /* if successfull seek, we update our real segment and push
3163 * out the new segment. */
3165 memcpy (&sink->segment, &seeksegment, sizeof (GstSegment));
3167 if (sink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3168 gst_element_post_message (GST_ELEMENT (sink),
3169 gst_message_new_segment_start (GST_OBJECT (sink),
3170 sink->segment.format, sink->segment.last_stop));
3174 sink->priv->discont = TRUE;
3175 sink->abidata.ABI.running = TRUE;
3177 GST_PAD_STREAM_UNLOCK (pad);
3185 gst_base_sink_loop (GstPad * pad)
3187 GstBaseSink *basesink;
3188 GstBuffer *buf = NULL;
3189 GstFlowReturn result;
3193 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
3195 g_assert (basesink->pad_mode == GST_ACTIVATE_PULL);
3197 if ((blocksize = basesink->priv->blocksize) == 0)
3200 offset = basesink->segment.last_stop;
3202 GST_DEBUG_OBJECT (basesink, "pulling %" G_GUINT64_FORMAT ", %u",
3205 result = gst_pad_pull_range (pad, offset, blocksize, &buf);
3206 if (G_UNLIKELY (result != GST_FLOW_OK))
3209 if (G_UNLIKELY (buf == NULL))
3212 offset += GST_BUFFER_SIZE (buf);
3214 gst_segment_set_last_stop (&basesink->segment, GST_FORMAT_BYTES, offset);
3216 GST_PAD_PREROLL_LOCK (pad);
3217 result = gst_base_sink_chain_unlocked (basesink, pad, buf);
3218 GST_PAD_PREROLL_UNLOCK (pad);
3219 if (G_UNLIKELY (result != GST_FLOW_OK))
3227 GST_LOG_OBJECT (basesink, "pausing task, reason %s",
3228 gst_flow_get_name (result));
3229 gst_pad_pause_task (pad);
3230 /* fatal errors and NOT_LINKED cause EOS */
3231 if (GST_FLOW_IS_FATAL (result) || result == GST_FLOW_NOT_LINKED) {
3232 if (result == GST_FLOW_UNEXPECTED) {
3233 /* perform EOS logic */
3234 if (basesink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3235 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3236 gst_message_new_segment_done (GST_OBJECT_CAST (basesink),
3237 basesink->segment.format, basesink->segment.last_stop));
3239 gst_base_sink_event (pad, gst_event_new_eos ());
3242 /* for fatal errors we post an error message, post the error
3243 * first so the app knows about the error first. */
3244 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
3245 (_("Internal data stream error.")),
3246 ("stream stopped, reason %s", gst_flow_get_name (result)));
3247 gst_base_sink_event (pad, gst_event_new_eos ());
3254 GST_LOG_OBJECT (basesink, "no buffer, pausing");
3255 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
3256 (_("Internal data flow error.")), ("element returned NULL buffer"));
3257 result = GST_FLOW_ERROR;
3263 gst_base_sink_set_flushing (GstBaseSink * basesink, GstPad * pad,
3266 GstBaseSinkClass *bclass;
3268 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3271 /* unlock any subclasses, we need to do this before grabbing the
3272 * PREROLL_LOCK since we hold this lock before going into ::render. */
3274 bclass->unlock (basesink);
3277 GST_PAD_PREROLL_LOCK (pad);
3278 basesink->flushing = flushing;
3280 /* step 1, now that we have the PREROLL lock, clear our unlock request */
3281 if (bclass->unlock_stop)
3282 bclass->unlock_stop (basesink);
3284 /* set need_preroll before we unblock the clock. If the clock is unblocked
3285 * before timing out, we can reuse the buffer for preroll. */
3286 basesink->need_preroll = TRUE;
3288 /* step 2, unblock clock sync (if any) or any other blocking thing */
3289 if (basesink->clock_id) {
3290 gst_clock_id_unschedule (basesink->clock_id);
3293 /* flush out the data thread if it's locked in finish_preroll, this will
3294 * also flush out the EOS state */
3295 GST_DEBUG_OBJECT (basesink,
3296 "flushing out data thread, need preroll to TRUE");
3297 gst_base_sink_preroll_queue_flush (basesink, pad);
3299 GST_PAD_PREROLL_UNLOCK (pad);
3305 gst_base_sink_default_activate_pull (GstBaseSink * basesink, gboolean active)
3311 result = gst_pad_start_task (basesink->sinkpad,
3312 (GstTaskFunction) gst_base_sink_loop, basesink->sinkpad);
3314 /* step 2, make sure streaming finishes */
3315 result = gst_pad_stop_task (basesink->sinkpad);
3322 gst_base_sink_pad_activate (GstPad * pad)
3324 gboolean result = FALSE;
3325 GstBaseSink *basesink;
3327 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3329 GST_DEBUG_OBJECT (basesink, "Trying pull mode first");
3331 gst_base_sink_set_flushing (basesink, pad, FALSE);
3333 /* we need to have the pull mode enabled */
3334 if (!basesink->can_activate_pull) {
3335 GST_DEBUG_OBJECT (basesink, "pull mode disabled");
3339 /* check if downstreams supports pull mode at all */
3340 if (!gst_pad_check_pull_range (pad)) {
3341 GST_DEBUG_OBJECT (basesink, "pull mode not supported");
3345 /* set the pad mode before starting the task so that it's in the
3346 * correct state for the new thread. also the sink set_caps and get_caps
3347 * function checks this */
3348 basesink->pad_mode = GST_ACTIVATE_PULL;
3350 /* we first try to negotiate a format so that when we try to activate
3351 * downstream, it knows about our format */
3352 if (!gst_base_sink_negotiate_pull (basesink)) {
3353 GST_DEBUG_OBJECT (basesink, "failed to negotiate in pull mode");
3357 /* ok activate now */
3358 if (!gst_pad_activate_pull (pad, TRUE)) {
3359 /* clear any pending caps */
3360 GST_OBJECT_LOCK (basesink);
3361 gst_caps_replace (&basesink->priv->pull_caps, NULL);
3362 GST_OBJECT_UNLOCK (basesink);
3363 GST_DEBUG_OBJECT (basesink, "failed to activate in pull mode");
3367 GST_DEBUG_OBJECT (basesink, "Success activating pull mode");
3371 /* push mode fallback */
3373 GST_DEBUG_OBJECT (basesink, "Falling back to push mode");
3374 if ((result = gst_pad_activate_push (pad, TRUE))) {
3375 GST_DEBUG_OBJECT (basesink, "Success activating push mode");
3380 GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
3381 gst_base_sink_set_flushing (basesink, pad, TRUE);
3384 gst_object_unref (basesink);
3390 gst_base_sink_pad_activate_push (GstPad * pad, gboolean active)
3393 GstBaseSink *basesink;
3395 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3398 if (!basesink->can_activate_push) {
3400 basesink->pad_mode = GST_ACTIVATE_NONE;
3403 basesink->pad_mode = GST_ACTIVATE_PUSH;
3406 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH)) {
3407 g_warning ("Internal GStreamer activation error!!!");
3410 gst_base_sink_set_flushing (basesink, pad, TRUE);
3412 basesink->pad_mode = GST_ACTIVATE_NONE;
3416 gst_object_unref (basesink);
3422 gst_base_sink_negotiate_pull (GstBaseSink * basesink)
3429 /* this returns the intersection between our caps and the peer caps. If there
3430 * is no peer, it returns NULL and we can't operate in pull mode so we can
3431 * fail the negotiation. */
3432 caps = gst_pad_get_allowed_caps (GST_BASE_SINK_PAD (basesink));
3433 if (caps == NULL || gst_caps_is_empty (caps))
3434 goto no_caps_possible;
3436 GST_DEBUG_OBJECT (basesink, "allowed caps: %" GST_PTR_FORMAT, caps);
3438 caps = gst_caps_make_writable (caps);
3439 /* get the first (prefered) format */
3440 gst_caps_truncate (caps);
3442 gst_pad_fixate_caps (GST_BASE_SINK_PAD (basesink), caps);
3444 GST_DEBUG_OBJECT (basesink, "fixated to: %" GST_PTR_FORMAT, caps);
3446 if (gst_caps_is_any (caps)) {
3447 GST_DEBUG_OBJECT (basesink, "caps were ANY after fixating, "
3449 /* neither side has template caps in this case, so they are prepared for
3450 pull() without setcaps() */
3452 } else if (gst_caps_is_fixed (caps)) {
3453 if (!gst_pad_set_caps (GST_BASE_SINK_PAD (basesink), caps))
3454 goto could_not_set_caps;
3456 GST_OBJECT_LOCK (basesink);
3457 gst_caps_replace (&basesink->priv->pull_caps, caps);
3458 GST_OBJECT_UNLOCK (basesink);
3463 gst_caps_unref (caps);
3469 GST_INFO_OBJECT (basesink, "Pipeline could not agree on caps");
3470 GST_DEBUG_OBJECT (basesink, "get_allowed_caps() returned EMPTY");
3472 gst_caps_unref (caps);
3477 GST_INFO_OBJECT (basesink, "Could not set caps: %" GST_PTR_FORMAT, caps);
3478 gst_caps_unref (caps);
3483 /* this won't get called until we implement an activate function */
3485 gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active)
3487 gboolean result = FALSE;
3488 GstBaseSink *basesink;
3489 GstBaseSinkClass *bclass;
3491 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3492 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3498 /* we mark we have a newsegment here because pull based
3499 * mode works just fine without having a newsegment before the
3501 format = GST_FORMAT_BYTES;
3503 gst_segment_init (&basesink->segment, format);
3504 gst_segment_init (basesink->abidata.ABI.clip_segment, format);
3505 GST_OBJECT_LOCK (basesink);
3506 basesink->have_newsegment = TRUE;
3507 GST_OBJECT_UNLOCK (basesink);
3509 /* get the peer duration in bytes */
3510 result = gst_pad_query_peer_duration (pad, &format, &duration);
3512 GST_DEBUG_OBJECT (basesink,
3513 "setting duration in bytes to %" G_GINT64_FORMAT, duration);
3514 gst_segment_set_duration (basesink->abidata.ABI.clip_segment, format,
3516 gst_segment_set_duration (&basesink->segment, format, duration);
3518 GST_DEBUG_OBJECT (basesink, "unknown duration");
3521 if (bclass->activate_pull)
3522 result = bclass->activate_pull (basesink, TRUE);
3527 goto activate_failed;
3529 /* but if starting the thread fails, set it back */
3531 basesink->pad_mode = GST_ACTIVATE_NONE;
3533 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PULL)) {
3534 g_warning ("Internal GStreamer activation error!!!");
3537 result = gst_base_sink_set_flushing (basesink, pad, TRUE);
3538 if (bclass->activate_pull)
3539 result &= bclass->activate_pull (basesink, FALSE);
3540 basesink->pad_mode = GST_ACTIVATE_NONE;
3541 /* clear any pending caps */
3542 GST_OBJECT_LOCK (basesink);
3543 gst_caps_replace (&basesink->priv->pull_caps, NULL);
3544 GST_OBJECT_UNLOCK (basesink);
3547 gst_object_unref (basesink);
3554 GST_ERROR_OBJECT (basesink, "subclass failed to activate in pull mode");
3559 /* send an event to our sinkpad peer. */
3561 gst_base_sink_send_event (GstElement * element, GstEvent * event)
3564 GstBaseSink *basesink = GST_BASE_SINK (element);
3565 gboolean forward, result = TRUE;
3566 GstActivateMode mode;
3568 GST_OBJECT_LOCK (element);
3569 /* get the pad and the scheduling mode */
3570 pad = gst_object_ref (basesink->sinkpad);
3571 mode = basesink->pad_mode;
3572 GST_OBJECT_UNLOCK (element);
3574 /* only push UPSTREAM events upstream and if we are in push mode */
3575 forward = GST_EVENT_IS_UPSTREAM (event) && (mode == GST_ACTIVATE_PUSH);
3577 switch (GST_EVENT_TYPE (event)) {
3578 case GST_EVENT_LATENCY:
3580 GstClockTime latency;
3582 gst_event_parse_latency (event, &latency);
3584 /* store the latency. We use this to adjust the running_time before syncing
3585 * it to the clock. */
3586 GST_OBJECT_LOCK (element);
3587 basesink->priv->latency = latency;
3588 GST_OBJECT_UNLOCK (element);
3589 GST_DEBUG_OBJECT (basesink, "latency set to %" GST_TIME_FORMAT,
3590 GST_TIME_ARGS (latency));
3592 /* We forward this event so that all elements know about the global pipeline
3593 * latency. This is interesting for an element when it wants to figure out
3594 * when a particular piece of data will be rendered. */
3597 case GST_EVENT_SEEK:
3598 /* in pull mode we will execute the seek */
3599 if (mode == GST_ACTIVATE_PULL)
3600 result = gst_base_sink_perform_seek (basesink, pad, event);
3607 result = gst_pad_push_event (pad, event);
3609 /* not forwarded, unref the event */
3610 gst_event_unref (event);
3613 gst_object_unref (pad);
3618 gst_base_sink_peer_query (GstBaseSink * sink, GstQuery * query)
3621 gboolean res = FALSE;
3623 if ((peer = gst_pad_get_peer (sink->sinkpad))) {
3624 res = gst_pad_query (peer, query);
3625 gst_object_unref (peer);
3630 /* get the end position of the last seen object, this is used
3631 * for EOS and for making sure that we don't report a position we
3632 * have not reached yet. With LOCK. */
3634 gst_base_sink_get_position_last (GstBaseSink * basesink, GstFormat format,
3638 GstSegment *segment;
3639 gboolean ret = TRUE;
3641 segment = &basesink->segment;
3642 oformat = segment->format;
3644 if (oformat == GST_FORMAT_TIME) {
3645 /* return last observed stream time, we keep the stream time around in the
3647 *cur = basesink->priv->current_sstop;
3649 /* convert last stop to stream time */
3650 *cur = gst_segment_to_stream_time (segment, oformat, segment->last_stop);
3653 if (*cur != -1 && oformat != format) {
3654 GST_OBJECT_UNLOCK (basesink);
3655 /* convert to the target format if we need to, release lock first */
3657 gst_pad_query_convert (basesink->sinkpad, oformat, *cur, &format, cur);
3660 GST_OBJECT_LOCK (basesink);
3663 GST_DEBUG_OBJECT (basesink, "POSITION: %" GST_TIME_FORMAT,
3664 GST_TIME_ARGS (*cur));
3669 /* get the position when we are PAUSED, this is the stream time of the buffer
3670 * that prerolled. If no buffer is prerolled (we are still flushing), this
3671 * value will be -1. With LOCK. */
3673 gst_base_sink_get_position_paused (GstBaseSink * basesink, GstFormat format,
3678 GstSegment *segment;
3681 /* we don't use the clip segment in pull mode, when seeking we update the
3682 * main segment directly with the new segment values without it having to be
3683 * activated by the rendering after preroll */
3684 if (basesink->pad_mode == GST_ACTIVATE_PUSH)
3685 segment = basesink->abidata.ABI.clip_segment;
3687 segment = &basesink->segment;
3688 oformat = segment->format;
3690 if (oformat == GST_FORMAT_TIME) {
3691 *cur = basesink->priv->current_sstart;
3693 *cur = gst_segment_to_stream_time (segment, oformat, segment->last_stop);
3696 time = segment->time;
3699 *cur = MAX (*cur, time);
3700 GST_DEBUG_OBJECT (basesink, "POSITION as max: %" GST_TIME_FORMAT
3701 ", time %" GST_TIME_FORMAT, GST_TIME_ARGS (*cur), GST_TIME_ARGS (time));
3703 /* we have no buffer, use the segment times. */
3704 if (segment->rate >= 0.0) {
3705 /* forward, next position is always the time of the segment */
3707 GST_DEBUG_OBJECT (basesink, "POSITION as time: %" GST_TIME_FORMAT,
3708 GST_TIME_ARGS (*cur));
3710 /* reverse, next expected timestamp is segment->stop. We use the function
3711 * to get things right for negative applied_rates. */
3712 *cur = gst_segment_to_stream_time (segment, oformat, segment->stop);
3713 GST_DEBUG_OBJECT (basesink, "reverse POSITION: %" GST_TIME_FORMAT,
3714 GST_TIME_ARGS (*cur));
3719 if (res && oformat != format) {
3720 GST_OBJECT_UNLOCK (basesink);
3722 gst_pad_query_convert (basesink->sinkpad, oformat, *cur, &format, cur);
3725 GST_OBJECT_LOCK (basesink);
3732 gst_base_sink_get_position (GstBaseSink * basesink, GstFormat format,
3733 gint64 * cur, gboolean * upstream)
3736 gboolean res = FALSE;
3737 GstFormat oformat, tformat;
3738 GstClockTime now, base, latency;
3739 gint64 time, accum, duration;
3743 GST_OBJECT_LOCK (basesink);
3744 /* our intermediate time format */
3745 tformat = GST_FORMAT_TIME;
3746 /* get the format in the segment */
3747 oformat = basesink->segment.format;
3749 /* can only give answer based on the clock if not EOS */
3750 if (G_UNLIKELY (basesink->eos))
3753 /* we can only get the segment when we are not NULL or READY */
3754 if (!basesink->have_newsegment)
3757 /* when not in PLAYING or when we're busy with a state change, we
3758 * cannot read from the clock so we report time based on the
3759 * last seen timestamp. */
3760 if (GST_STATE (basesink) != GST_STATE_PLAYING ||
3761 GST_STATE_PENDING (basesink) != GST_STATE_VOID_PENDING)
3764 /* we need to sync on the clock. */
3765 if (basesink->sync == FALSE)
3768 /* and we need a clock */
3769 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (basesink)) == NULL))
3772 /* collect all data we need holding the lock */
3773 if (GST_CLOCK_TIME_IS_VALID (basesink->segment.time))
3774 time = basesink->segment.time;
3778 if (GST_CLOCK_TIME_IS_VALID (basesink->segment.stop))
3779 duration = basesink->segment.stop - basesink->segment.start;
3783 base = GST_ELEMENT_CAST (basesink)->base_time;
3784 accum = basesink->segment.accum;
3785 rate = basesink->segment.rate * basesink->segment.applied_rate;
3786 latency = basesink->priv->latency;
3788 gst_object_ref (clock);
3790 /* this function might release the LOCK */
3791 gst_base_sink_get_position_last (basesink, format, &last);
3793 /* need to release the object lock before we can get the time,
3794 * a clock might take the LOCK of the provider, which could be
3795 * a basesink subclass. */
3796 GST_OBJECT_UNLOCK (basesink);
3798 now = gst_clock_get_time (clock);
3800 if (oformat != tformat) {
3801 /* convert accum, time and duration to time */
3802 if (!gst_pad_query_convert (basesink->sinkpad, oformat, accum, &tformat,
3804 goto convert_failed;
3805 if (!gst_pad_query_convert (basesink->sinkpad, oformat, duration, &tformat,
3807 goto convert_failed;
3808 if (!gst_pad_query_convert (basesink->sinkpad, oformat, time, &tformat,
3810 goto convert_failed;
3813 /* subtract base time and accumulated time from the clock time.
3814 * Make sure we don't go negative. This is the current time in
3815 * the segment which we need to scale with the combined
3816 * rate and applied rate. */
3819 base = MIN (now, base);
3821 /* for negative rates we need to count back from from the segment
3826 *cur = time + gst_guint64_to_gdouble (now - base) * rate;
3828 /* never report more than last seen position */
3830 *cur = MIN (last, *cur);
3832 gst_object_unref (clock);
3834 GST_DEBUG_OBJECT (basesink,
3835 "now %" GST_TIME_FORMAT " - base %" GST_TIME_FORMAT " - accum %"
3836 GST_TIME_FORMAT " + time %" GST_TIME_FORMAT,
3837 GST_TIME_ARGS (now), GST_TIME_ARGS (base),
3838 GST_TIME_ARGS (accum), GST_TIME_ARGS (time));
3840 if (oformat != format) {
3841 /* convert time to final format */
3842 if (!gst_pad_query_convert (basesink->sinkpad, tformat, *cur, &format, cur))
3843 goto convert_failed;
3849 GST_DEBUG_OBJECT (basesink, "res: %d, POSITION: %" GST_TIME_FORMAT,
3850 res, GST_TIME_ARGS (*cur));
3856 GST_DEBUG_OBJECT (basesink, "position in EOS");
3857 res = gst_base_sink_get_position_last (basesink, format, cur);
3858 GST_OBJECT_UNLOCK (basesink);
3863 GST_DEBUG_OBJECT (basesink, "position in PAUSED");
3864 res = gst_base_sink_get_position_paused (basesink, format, cur);
3865 GST_OBJECT_UNLOCK (basesink);
3870 /* in NULL or READY we always return FALSE and -1 */
3871 GST_DEBUG_OBJECT (basesink, "position in wrong state, return -1");
3874 GST_OBJECT_UNLOCK (basesink);
3879 /* report last seen timestamp if any, else ask upstream to answer */
3880 if ((*cur = basesink->priv->current_sstart) != -1)
3885 GST_DEBUG_OBJECT (basesink, "no sync, res %d, POSITION %" GST_TIME_FORMAT,
3886 res, GST_TIME_ARGS (*cur));
3887 GST_OBJECT_UNLOCK (basesink);
3892 GST_DEBUG_OBJECT (basesink, "convert failed, try upstream");
3899 gst_base_sink_query (GstElement * element, GstQuery * query)
3901 gboolean res = FALSE;
3903 GstBaseSink *basesink = GST_BASE_SINK (element);
3905 switch (GST_QUERY_TYPE (query)) {
3906 case GST_QUERY_POSITION:
3910 gboolean upstream = FALSE;
3912 gst_query_parse_position (query, &format, NULL);
3914 GST_DEBUG_OBJECT (basesink, "position format %d", format);
3916 /* first try to get the position based on the clock */
3918 gst_base_sink_get_position (basesink, format, &cur, &upstream))) {
3919 gst_query_set_position (query, format, cur);
3920 } else if (upstream) {
3921 /* fallback to peer query */
3922 res = gst_base_sink_peer_query (basesink, query);
3926 case GST_QUERY_DURATION:
3928 GstFormat format, uformat;
3929 gint64 duration, uduration;
3931 gst_query_parse_duration (query, &format, NULL);
3933 GST_DEBUG_OBJECT (basesink, "duration query in format %s",
3934 gst_format_get_name (format));
3936 if (basesink->pad_mode == GST_ACTIVATE_PULL) {
3937 uformat = GST_FORMAT_BYTES;
3939 /* get the duration in bytes, in pull mode that's all we are sure to
3940 * know. We have to explicitly get this value from upstream instead of
3941 * using our cached value because it might change. Duration caching
3942 * should be done at a higher level. */
3943 res = gst_pad_query_peer_duration (basesink->sinkpad, &uformat,
3946 gst_segment_set_duration (&basesink->segment, uformat, uduration);
3947 if (format != uformat) {
3948 /* convert to the requested format */
3949 res = gst_pad_query_convert (basesink->sinkpad, uformat, uduration,
3950 &format, &duration);
3952 duration = uduration;
3955 /* set the result */
3956 gst_query_set_duration (query, format, duration);
3960 /* in push mode we simply forward upstream */
3961 res = gst_base_sink_peer_query (basesink, query);
3965 case GST_QUERY_LATENCY:
3967 gboolean live, us_live;
3968 GstClockTime min, max;
3970 if ((res = gst_base_sink_query_latency (basesink, &live, &us_live, &min,
3972 gst_query_set_latency (query, live, min, max);
3976 case GST_QUERY_JITTER:
3978 case GST_QUERY_RATE:
3979 /* gst_query_set_rate (query, basesink->segment_rate); */
3982 case GST_QUERY_SEGMENT:
3984 /* FIXME, bring start/stop to stream time */
3985 gst_query_set_segment (query, basesink->segment.rate,
3986 GST_FORMAT_TIME, basesink->segment.start, basesink->segment.stop);
3989 case GST_QUERY_SEEKING:
3990 case GST_QUERY_CONVERT:
3991 case GST_QUERY_FORMATS:
3993 res = gst_base_sink_peer_query (basesink, query);
3999 static GstStateChangeReturn
4000 gst_base_sink_change_state (GstElement * element, GstStateChange transition)
4002 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
4003 GstBaseSink *basesink = GST_BASE_SINK (element);
4004 GstBaseSinkClass *bclass;
4005 GstBaseSinkPrivate *priv;
4007 priv = basesink->priv;
4009 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4011 switch (transition) {
4012 case GST_STATE_CHANGE_NULL_TO_READY:
4014 if (!bclass->start (basesink))
4017 case GST_STATE_CHANGE_READY_TO_PAUSED:
4018 /* need to complete preroll before this state change completes, there
4019 * is no data flow in READY so we can safely assume we need to preroll. */
4020 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4021 GST_DEBUG_OBJECT (basesink, "READY to PAUSED");
4022 basesink->have_newsegment = FALSE;
4023 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
4024 gst_segment_init (basesink->abidata.ABI.clip_segment,
4025 GST_FORMAT_UNDEFINED);
4026 basesink->offset = 0;
4027 basesink->have_preroll = FALSE;
4028 basesink->need_preroll = TRUE;
4029 basesink->playing_async = TRUE;
4030 priv->current_sstart = -1;
4031 priv->current_sstop = -1;
4032 priv->eos_rtime = -1;
4034 basesink->eos = FALSE;
4035 priv->received_eos = FALSE;
4036 gst_base_sink_reset_qos (basesink);
4037 priv->commited = FALSE;
4038 if (priv->async_enabled) {
4039 GST_DEBUG_OBJECT (basesink, "doing async state change");
4040 /* when async enabled, post async-start message and return ASYNC from
4041 * the state change function */
4042 ret = GST_STATE_CHANGE_ASYNC;
4043 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4044 gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
4046 priv->have_latency = TRUE;
4048 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4050 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
4051 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4052 if (!gst_base_sink_needs_preroll (basesink)) {
4053 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, don't need preroll");
4054 /* no preroll needed anymore now. */
4055 basesink->playing_async = FALSE;
4056 basesink->need_preroll = FALSE;
4057 if (basesink->eos) {
4058 GstMessage *message;
4060 /* need to post EOS message here */
4061 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
4062 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
4063 gst_message_set_seqnum (message, basesink->priv->seqnum);
4064 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
4066 GST_DEBUG_OBJECT (basesink, "signal preroll");
4067 GST_PAD_PREROLL_SIGNAL (basesink->sinkpad);
4070 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, we are not prerolled");
4071 basesink->need_preroll = TRUE;
4072 basesink->playing_async = TRUE;
4073 priv->commited = FALSE;
4074 if (priv->async_enabled) {
4075 GST_DEBUG_OBJECT (basesink, "doing async state change");
4076 ret = GST_STATE_CHANGE_ASYNC;
4077 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4078 gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
4081 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4088 GstStateChangeReturn bret;
4090 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4091 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
4092 goto activate_failed;
4095 switch (transition) {
4096 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
4097 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED");
4098 /* FIXME, make sure we cannot enter _render first */
4100 /* we need to call ::unlock before locking PREROLL_LOCK
4101 * since we lock it before going into ::render */
4103 bclass->unlock (basesink);
4105 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4106 /* now that we have the PREROLL lock, clear our unlock request */
4107 if (bclass->unlock_stop)
4108 bclass->unlock_stop (basesink);
4110 /* we need preroll again and we set the flag before unlocking the clockid
4111 * because if the clockid is unlocked before a current buffer expired, we
4112 * can use that buffer to preroll with */
4113 basesink->need_preroll = TRUE;
4115 if (basesink->clock_id) {
4116 gst_clock_id_unschedule (basesink->clock_id);
4119 /* if we don't have a preroll buffer we need to wait for a preroll and
4121 if (!gst_base_sink_needs_preroll (basesink)) {
4122 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, we are prerolled");
4123 basesink->playing_async = FALSE;
4125 if (GST_STATE_TARGET (GST_ELEMENT (basesink)) <= GST_STATE_READY) {
4126 ret = GST_STATE_CHANGE_SUCCESS;
4128 GST_DEBUG_OBJECT (basesink,
4129 "PLAYING to PAUSED, we are not prerolled");
4130 basesink->playing_async = TRUE;
4131 priv->commited = FALSE;
4132 if (priv->async_enabled) {
4133 GST_DEBUG_OBJECT (basesink, "doing async state change");
4134 ret = GST_STATE_CHANGE_ASYNC;
4135 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4136 gst_message_new_async_start (GST_OBJECT_CAST (basesink),
4141 GST_DEBUG_OBJECT (basesink, "rendered: %" G_GUINT64_FORMAT
4142 ", dropped: %" G_GUINT64_FORMAT, priv->rendered, priv->dropped);
4144 gst_base_sink_reset_qos (basesink);
4145 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4147 case GST_STATE_CHANGE_PAUSED_TO_READY:
4148 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4149 /* start by reseting our position state with the object lock so that the
4150 * position query gets the right idea. We do this before we post the
4151 * messages so that the message handlers pick this up. */
4152 GST_OBJECT_LOCK (basesink);
4153 basesink->have_newsegment = FALSE;
4154 priv->current_sstart = -1;
4155 priv->current_sstop = -1;
4156 priv->have_latency = FALSE;
4157 GST_OBJECT_UNLOCK (basesink);
4159 gst_base_sink_set_last_buffer (basesink, NULL);
4161 if (!priv->commited) {
4162 if (priv->async_enabled) {
4163 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, posting async-done");
4165 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4166 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
4167 GST_STATE_PLAYING, GST_STATE_PAUSED, GST_STATE_READY));
4169 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4170 gst_message_new_async_done (GST_OBJECT_CAST (basesink)));
4172 priv->commited = TRUE;
4174 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, don't need_preroll");
4176 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4178 case GST_STATE_CHANGE_READY_TO_NULL:
4180 if (!bclass->stop (basesink)) {
4181 GST_WARNING_OBJECT (basesink, "failed to stop");
4184 gst_base_sink_set_last_buffer (basesink, NULL);
4195 GST_DEBUG_OBJECT (basesink, "failed to start");
4196 return GST_STATE_CHANGE_FAILURE;
4200 GST_DEBUG_OBJECT (basesink,
4201 "element failed to change states -- activation problem?");
4202 return GST_STATE_CHANGE_FAILURE;