2 * Copyright (C) 2005-2007 Wim Taymans <wim.taymans@gmail.com>
4 * gstbasesink.c: Base class for sink elements
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
24 * @short_description: Base class for sink elements
25 * @see_also: #GstBaseTransform, #GstBaseSource
27 * #GstBaseSink is the base class for sink elements in GStreamer, such as
28 * xvimagesink or filesink. It is a layer on top of #GstElement that provides a
29 * simplified interface to plugin writers. #GstBaseSink handles many details
30 * for you, for example: preroll, clock synchronization, state changes,
31 * activation in push or pull mode, and queries.
33 * In most cases, when writing sink elements, there is no need to implement
34 * class methods from #GstElement or to set functions on pads, because the
35 * #GstBaseSink infrastructure should be sufficient.
37 * #GstBaseSink provides support for exactly one sink pad, which should be
38 * named "sink". A sink implementation (subclass of #GstBaseSink) should
39 * install a pad template in its base_init function, like so:
42 * my_element_base_init (gpointer g_class)
44 * GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
46 * // sinktemplate should be a #GstStaticPadTemplate with direction
47 * // #GST_PAD_SINK and name "sink"
48 * gst_element_class_add_pad_template (gstelement_class,
49 * gst_static_pad_template_get (&sinktemplate));
50 * // see #GstElementDetails
51 * gst_element_class_set_details (gstelement_class, &details);
55 * #GstBaseSink will handle the prerolling correctly. This means that it will
56 * return #GST_STATE_CHANGE_ASYNC from a state change to PAUSED until the first
57 * buffer arrives in this element. The base class will call the
58 * #GstBaseSink::preroll vmethod with this preroll buffer and will then commit
59 * the state change to the next asynchronously pending state.
61 * When the element is set to PLAYING, #GstBaseSink will synchronise on the
62 * clock using the times returned from ::get_times. If this function returns
63 * #GST_CLOCK_TIME_NONE for the start time, no synchronisation will be done.
64 * Synchronisation can be disabled entirely by setting the object "sync"
67 * After synchronisation the virtual method #GstBaseSink::render will be called.
68 * Subclasses should minimally implement this method.
70 * Since 0.10.3 subclasses that synchronise on the clock in the ::render method
71 * are supported as well. These classes typically receive a buffer in the render
72 * method and can then potentially block on the clock while rendering. A typical
73 * example is an audiosink. Since 0.10.11 these subclasses can use
74 * gst_base_sink_wait_preroll() to perform the blocking wait.
76 * Upon receiving the EOS event in the PLAYING state, #GstBaseSink will wait
77 * for the clock to reach the time indicated by the stop time of the last
78 * ::get_times call before posting an EOS message. When the element receives
79 * EOS in PAUSED, preroll completes, the event is queued and an EOS message is
80 * posted when going to PLAYING.
82 * #GstBaseSink will internally use the #GST_EVENT_NEWSEGMENT events to schedule
83 * synchronisation and clipping of buffers. Buffers that fall completely outside
84 * of the current segment are dropped. Buffers that fall partially in the
85 * segment are rendered (and prerolled). Subclasses should do any subbuffer
86 * clipping themselves when needed.
88 * #GstBaseSink will by default report the current playback position in
89 * #GST_FORMAT_TIME based on the current clock time and segment information.
90 * If no clock has been set on the element, the query will be forwarded
93 * The ::set_caps function will be called when the subclass should configure
94 * itself to process a specific media type.
96 * The ::start and ::stop virtual methods will be called when resources should
97 * be allocated. Any ::preroll, ::render and ::set_caps function will be
98 * called between the ::start and ::stop calls.
100 * The ::event virtual method will be called when an event is received by
101 * #GstBaseSink. Normally this method should only be overriden by very specific
102 * elements (such as file sinks) which need to handle the newsegment event
105 * #GstBaseSink provides an overridable ::buffer_alloc function that can be
106 * used by sinks that want to do reverse negotiation or to provide
107 * custom buffers (hardware buffers for example) to upstream elements.
109 * The ::unlock method is called when the elements should unblock any blocking
110 * operations they perform in the ::render method. This is mostly useful when
111 * the ::render method performs a blocking write on a file descriptor, for
114 * The max-lateness property affects how the sink deals with buffers that
115 * arrive too late in the sink. A buffer arrives too late in the sink when
116 * the presentation time (as a combination of the last segment, buffer
117 * timestamp and element base_time) plus the duration is before the current
119 * If the frame is later than max-lateness, the sink will drop the buffer
120 * without calling the render method.
121 * This feature is disabled if sync is disabled, the ::get-times method does
122 * not return a valid start time or max-lateness is set to -1 (the default).
123 * Subclasses can use gst_base_sink_set_max_lateness() to configure the
124 * max-lateness value.
126 * The qos property will enable the quality-of-service features of the basesink
127 * which gather statistics about the real-time performance of the clock
128 * synchronisation. For each buffer received in the sink, statistics are
129 * gathered and a QOS event is sent upstream with these numbers. This
130 * information can then be used by upstream elements to reduce their processing
133 * Since 0.10.15 the async property can be used to instruct the sink to never
134 * perform an ASYNC state change. This feature is mostly usable when dealing
135 * with non-synchronized streams or sparse streams.
137 * Last reviewed on 2007-08-29 (0.10.15)
144 #include "gstbasesink.h"
145 #include <gst/gstmarshal.h>
146 #include <gst/gst_private.h>
147 #include <gst/gst-i18n-lib.h>
149 GST_DEBUG_CATEGORY_STATIC (gst_base_sink_debug);
150 #define GST_CAT_DEFAULT gst_base_sink_debug
152 #define GST_BASE_SINK_GET_PRIVATE(obj) \
153 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_SINK, GstBaseSinkPrivate))
155 #define GST_FLOW_STEP GST_FLOW_CUSTOM_ERROR
159 gboolean valid; /* if this info is valid */
160 guint32 seqnum; /* the seqnum of the STEP event */
161 GstFormat format; /* the format of the amount */
162 guint64 amount; /* the total amount of data to skip */
163 guint64 position; /* the position in the stepped data */
164 guint64 duration; /* the duration in time of the skipped data */
165 guint64 start; /* running_time of the start */
166 gdouble rate; /* rate of skipping */
167 gdouble start_rate; /* rate before skipping */
168 guint64 start_start; /* start position skipping */
169 guint64 start_stop; /* stop position skipping */
170 gboolean flush; /* if this was a flushing step */
171 gboolean intermediate; /* if this is an intermediate step */
172 gboolean need_preroll; /* if we need preroll after this step */
175 /* FIXME, some stuff in ABI.data and other in Private...
176 * Make up your mind please.
178 struct _GstBaseSinkPrivate
180 gint qos_enabled; /* ATOMIC */
181 gboolean async_enabled;
182 GstClockTimeDiff ts_offset;
183 GstClockTime render_delay;
185 /* start, stop of current buffer, stream time, used to report position */
186 GstClockTime current_sstart;
187 GstClockTime current_sstop;
189 /* start, stop and jitter of current buffer, running time */
190 GstClockTime current_rstart;
191 GstClockTime current_rstop;
192 GstClockTimeDiff current_jitter;
194 /* EOS sync time in running time */
195 GstClockTime eos_rtime;
197 /* last buffer that arrived in time, running time */
198 GstClockTime last_in_time;
199 /* when the last buffer left the sink, running time */
200 GstClockTime last_left;
202 /* running averages go here these are done on running time */
204 GstClockTime avg_duration;
207 /* these are done on system time. avg_jitter and avg_render are
208 * compared to eachother to see if the rendering time takes a
209 * huge amount of the processing, If so we are flooded with
211 GstClockTime last_left_systime;
212 GstClockTime avg_jitter;
213 GstClockTime start, stop;
214 GstClockTime avg_render;
216 /* number of rendered and dropped frames */
221 GstClockTime latency;
223 /* if we already commited the state */
226 /* when we received EOS */
227 gboolean received_eos;
229 /* when we are prerolled and able to report latency */
230 gboolean have_latency;
232 /* the last buffer we prerolled or rendered. Useful for making snapshots */
233 GstBuffer *last_buffer;
235 /* caps for pull based scheduling */
238 /* blocksize for pulling */
243 /* seqnum of the stream */
246 gboolean call_preroll;
247 gboolean step_unlock;
249 /* we have a pending and a current step operation */
250 GstStepInfo current_step;
251 GstStepInfo pending_step;
254 #define DO_RUNNING_AVG(avg,val,size) (((val) + ((size)-1) * (avg)) / (size))
256 /* generic running average, this has a neutral window size */
257 #define UPDATE_RUNNING_AVG(avg,val) DO_RUNNING_AVG(avg,val,8)
259 /* the windows for these running averages are experimentally obtained.
260 * possitive values get averaged more while negative values use a small
261 * window so we can react faster to badness. */
262 #define UPDATE_RUNNING_AVG_P(avg,val) DO_RUNNING_AVG(avg,val,16)
263 #define UPDATE_RUNNING_AVG_N(avg,val) DO_RUNNING_AVG(avg,val,4)
265 /* BaseSink properties */
267 #define DEFAULT_CAN_ACTIVATE_PULL FALSE /* fixme: enable me */
268 #define DEFAULT_CAN_ACTIVATE_PUSH TRUE
270 #define DEFAULT_PREROLL_QUEUE_LEN 0
271 #define DEFAULT_SYNC TRUE
272 #define DEFAULT_MAX_LATENESS -1
273 #define DEFAULT_QOS FALSE
274 #define DEFAULT_ASYNC TRUE
275 #define DEFAULT_TS_OFFSET 0
276 #define DEFAULT_BLOCKSIZE 4096
277 #define DEFAULT_RENDER_DELAY 0
282 PROP_PREROLL_QUEUE_LEN,
294 static GstElementClass *parent_class = NULL;
296 static void gst_base_sink_class_init (GstBaseSinkClass * klass);
297 static void gst_base_sink_init (GstBaseSink * trans, gpointer g_class);
298 static void gst_base_sink_finalize (GObject * object);
301 gst_base_sink_get_type (void)
303 static volatile gsize base_sink_type = 0;
305 if (g_once_init_enter (&base_sink_type)) {
307 static const GTypeInfo base_sink_info = {
308 sizeof (GstBaseSinkClass),
311 (GClassInitFunc) gst_base_sink_class_init,
314 sizeof (GstBaseSink),
316 (GInstanceInitFunc) gst_base_sink_init,
319 _type = g_type_register_static (GST_TYPE_ELEMENT,
320 "GstBaseSink", &base_sink_info, G_TYPE_FLAG_ABSTRACT);
321 g_once_init_leave (&base_sink_type, _type);
323 return base_sink_type;
326 static void gst_base_sink_set_property (GObject * object, guint prop_id,
327 const GValue * value, GParamSpec * pspec);
328 static void gst_base_sink_get_property (GObject * object, guint prop_id,
329 GValue * value, GParamSpec * pspec);
331 static gboolean gst_base_sink_send_event (GstElement * element,
333 static gboolean gst_base_sink_query (GstElement * element, GstQuery * query);
335 static GstCaps *gst_base_sink_get_caps (GstBaseSink * sink);
336 static gboolean gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps);
337 static GstFlowReturn gst_base_sink_buffer_alloc (GstBaseSink * sink,
338 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
339 static void gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
340 GstClockTime * start, GstClockTime * end);
341 static gboolean gst_base_sink_set_flushing (GstBaseSink * basesink,
342 GstPad * pad, gboolean flushing);
343 static gboolean gst_base_sink_default_activate_pull (GstBaseSink * basesink,
345 static gboolean gst_base_sink_default_do_seek (GstBaseSink * sink,
346 GstSegment * segment);
347 static gboolean gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
348 GstEvent * event, GstSegment * segment);
350 static GstStateChangeReturn gst_base_sink_change_state (GstElement * element,
351 GstStateChange transition);
353 static GstFlowReturn gst_base_sink_chain (GstPad * pad, GstBuffer * buffer);
354 static GstFlowReturn gst_base_sink_chain_list (GstPad * pad,
355 GstBufferList * list);
357 static void gst_base_sink_loop (GstPad * pad);
358 static gboolean gst_base_sink_pad_activate (GstPad * pad);
359 static gboolean gst_base_sink_pad_activate_push (GstPad * pad, gboolean active);
360 static gboolean gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active);
361 static gboolean gst_base_sink_event (GstPad * pad, GstEvent * event);
362 static gboolean gst_base_sink_peer_query (GstBaseSink * sink, GstQuery * query);
364 static gboolean gst_base_sink_negotiate_pull (GstBaseSink * basesink);
366 /* check if an object was too late */
367 static gboolean gst_base_sink_is_too_late (GstBaseSink * basesink,
368 GstMiniObject * obj, GstClockTime start, GstClockTime stop,
369 GstClockReturn status, GstClockTimeDiff jitter);
370 static GstFlowReturn gst_base_sink_preroll_object (GstBaseSink * basesink,
371 gboolean is_list, GstMiniObject * obj);
374 gst_base_sink_class_init (GstBaseSinkClass * klass)
376 GObjectClass *gobject_class;
377 GstElementClass *gstelement_class;
379 gobject_class = G_OBJECT_CLASS (klass);
380 gstelement_class = GST_ELEMENT_CLASS (klass);
382 GST_DEBUG_CATEGORY_INIT (gst_base_sink_debug, "basesink", 0,
385 g_type_class_add_private (klass, sizeof (GstBaseSinkPrivate));
387 parent_class = g_type_class_peek_parent (klass);
389 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_base_sink_finalize);
390 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_base_sink_set_property);
391 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_base_sink_get_property);
393 /* FIXME, this next value should be configured using an event from the
394 * upstream element, ie, the BUFFER_SIZE event. */
395 g_object_class_install_property (gobject_class, PROP_PREROLL_QUEUE_LEN,
396 g_param_spec_uint ("preroll-queue-len", "Preroll queue length",
397 "Number of buffers to queue during preroll", 0, G_MAXUINT,
398 DEFAULT_PREROLL_QUEUE_LEN,
399 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
401 g_object_class_install_property (gobject_class, PROP_SYNC,
402 g_param_spec_boolean ("sync", "Sync", "Sync on the clock", DEFAULT_SYNC,
403 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
405 g_object_class_install_property (gobject_class, PROP_MAX_LATENESS,
406 g_param_spec_int64 ("max-lateness", "Max Lateness",
407 "Maximum number of nanoseconds that a buffer can be late before it "
408 "is dropped (-1 unlimited)", -1, G_MAXINT64, DEFAULT_MAX_LATENESS,
409 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
411 g_object_class_install_property (gobject_class, PROP_QOS,
412 g_param_spec_boolean ("qos", "Qos",
413 "Generate Quality-of-Service events upstream", DEFAULT_QOS,
414 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
418 * If set to #TRUE, the basesink will perform asynchronous state changes.
419 * When set to #FALSE, the sink will not signal the parent when it prerolls.
420 * Use this option when dealing with sparse streams or when synchronisation is
425 g_object_class_install_property (gobject_class, PROP_ASYNC,
426 g_param_spec_boolean ("async", "Async",
427 "Go asynchronously to PAUSED", DEFAULT_ASYNC,
428 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
430 * GstBaseSink:ts-offset
432 * Controls the final synchronisation, a negative value will render the buffer
433 * earlier while a positive value delays playback. This property can be
434 * used to fix synchronisation in bad files.
438 g_object_class_install_property (gobject_class, PROP_TS_OFFSET,
439 g_param_spec_int64 ("ts-offset", "TS Offset",
440 "Timestamp offset in nanoseconds", G_MININT64, G_MAXINT64,
441 DEFAULT_TS_OFFSET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
443 * GstBaseSink:last-buffer
445 * The last buffer that arrived in the sink and was used for preroll or for
446 * rendering. This property can be used to generate thumbnails. This property
447 * can be NULL when the sink has not yet received a bufer.
451 g_object_class_install_property (gobject_class, PROP_LAST_BUFFER,
452 gst_param_spec_mini_object ("last-buffer", "Last Buffer",
453 "The last buffer received in the sink", GST_TYPE_BUFFER,
454 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
456 * GstBaseSink:blocksize
458 * The amount of bytes to pull when operating in pull mode.
462 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
463 g_param_spec_uint ("blocksize", "Block size",
464 "Size in bytes to pull per buffer (0 = default)", 0, G_MAXUINT,
465 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
467 * GstBaseSink:render-delay
469 * The additional delay between synchronisation and actual rendering of the
470 * media. This property will add additional latency to the device in order to
471 * make other sinks compensate for the delay.
475 g_object_class_install_property (gobject_class, PROP_RENDER_DELAY,
476 g_param_spec_uint64 ("render-delay", "Render Delay",
477 "Additional render delay of the sink in nanoseconds", 0, G_MAXUINT64,
478 DEFAULT_RENDER_DELAY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
480 gstelement_class->change_state =
481 GST_DEBUG_FUNCPTR (gst_base_sink_change_state);
482 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_sink_send_event);
483 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_base_sink_query);
485 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_sink_get_caps);
486 klass->set_caps = GST_DEBUG_FUNCPTR (gst_base_sink_set_caps);
487 klass->buffer_alloc = GST_DEBUG_FUNCPTR (gst_base_sink_buffer_alloc);
488 klass->get_times = GST_DEBUG_FUNCPTR (gst_base_sink_get_times);
489 klass->activate_pull =
490 GST_DEBUG_FUNCPTR (gst_base_sink_default_activate_pull);
494 gst_base_sink_pad_getcaps (GstPad * pad)
496 GstBaseSinkClass *bclass;
498 GstCaps *caps = NULL;
500 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
501 bclass = GST_BASE_SINK_GET_CLASS (bsink);
503 if (bsink->pad_mode == GST_ACTIVATE_PULL) {
504 /* if we are operating in pull mode we only accept the negotiated caps */
505 GST_OBJECT_LOCK (pad);
506 if ((caps = GST_PAD_CAPS (pad)))
508 GST_OBJECT_UNLOCK (pad);
511 if (bclass->get_caps)
512 caps = bclass->get_caps (bsink);
515 GstPadTemplate *pad_template;
518 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass),
520 if (pad_template != NULL) {
521 caps = gst_caps_ref (gst_pad_template_get_caps (pad_template));
525 gst_object_unref (bsink);
531 gst_base_sink_pad_setcaps (GstPad * pad, GstCaps * caps)
533 GstBaseSinkClass *bclass;
537 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
538 bclass = GST_BASE_SINK_GET_CLASS (bsink);
540 if (res && bclass->set_caps)
541 res = bclass->set_caps (bsink, caps);
543 gst_object_unref (bsink);
549 gst_base_sink_pad_fixate (GstPad * pad, GstCaps * caps)
551 GstBaseSinkClass *bclass;
554 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
555 bclass = GST_BASE_SINK_GET_CLASS (bsink);
558 bclass->fixate (bsink, caps);
560 gst_object_unref (bsink);
564 gst_base_sink_pad_buffer_alloc (GstPad * pad, guint64 offset, guint size,
565 GstCaps * caps, GstBuffer ** buf)
567 GstBaseSinkClass *bclass;
569 GstFlowReturn result = GST_FLOW_OK;
571 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
572 bclass = GST_BASE_SINK_GET_CLASS (bsink);
574 if (bclass->buffer_alloc)
575 result = bclass->buffer_alloc (bsink, offset, size, caps, buf);
577 *buf = NULL; /* fallback in gstpad.c will allocate generic buffer */
579 gst_object_unref (bsink);
585 gst_base_sink_init (GstBaseSink * basesink, gpointer g_class)
587 GstPadTemplate *pad_template;
588 GstBaseSinkPrivate *priv;
590 basesink->priv = priv = GST_BASE_SINK_GET_PRIVATE (basesink);
593 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
594 g_return_if_fail (pad_template != NULL);
596 basesink->sinkpad = gst_pad_new_from_template (pad_template, "sink");
598 gst_pad_set_getcaps_function (basesink->sinkpad,
599 GST_DEBUG_FUNCPTR (gst_base_sink_pad_getcaps));
600 gst_pad_set_setcaps_function (basesink->sinkpad,
601 GST_DEBUG_FUNCPTR (gst_base_sink_pad_setcaps));
602 gst_pad_set_fixatecaps_function (basesink->sinkpad,
603 GST_DEBUG_FUNCPTR (gst_base_sink_pad_fixate));
604 gst_pad_set_bufferalloc_function (basesink->sinkpad,
605 GST_DEBUG_FUNCPTR (gst_base_sink_pad_buffer_alloc));
606 gst_pad_set_activate_function (basesink->sinkpad,
607 GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate));
608 gst_pad_set_activatepush_function (basesink->sinkpad,
609 GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate_push));
610 gst_pad_set_activatepull_function (basesink->sinkpad,
611 GST_DEBUG_FUNCPTR (gst_base_sink_pad_activate_pull));
612 gst_pad_set_event_function (basesink->sinkpad,
613 GST_DEBUG_FUNCPTR (gst_base_sink_event));
614 gst_pad_set_chain_function (basesink->sinkpad,
615 GST_DEBUG_FUNCPTR (gst_base_sink_chain));
616 gst_pad_set_chain_list_function (basesink->sinkpad,
617 GST_DEBUG_FUNCPTR (gst_base_sink_chain_list));
618 gst_element_add_pad (GST_ELEMENT_CAST (basesink), basesink->sinkpad);
620 basesink->pad_mode = GST_ACTIVATE_NONE;
621 basesink->preroll_queue = g_queue_new ();
622 basesink->abidata.ABI.clip_segment = gst_segment_new ();
623 priv->have_latency = FALSE;
625 basesink->can_activate_push = DEFAULT_CAN_ACTIVATE_PUSH;
626 basesink->can_activate_pull = DEFAULT_CAN_ACTIVATE_PULL;
628 basesink->sync = DEFAULT_SYNC;
629 basesink->abidata.ABI.max_lateness = DEFAULT_MAX_LATENESS;
630 g_atomic_int_set (&priv->qos_enabled, DEFAULT_QOS);
631 priv->async_enabled = DEFAULT_ASYNC;
632 priv->ts_offset = DEFAULT_TS_OFFSET;
633 priv->render_delay = DEFAULT_RENDER_DELAY;
634 priv->blocksize = DEFAULT_BLOCKSIZE;
636 GST_OBJECT_FLAG_SET (basesink, GST_ELEMENT_IS_SINK);
640 gst_base_sink_finalize (GObject * object)
642 GstBaseSink *basesink;
644 basesink = GST_BASE_SINK (object);
646 g_queue_free (basesink->preroll_queue);
647 gst_segment_free (basesink->abidata.ABI.clip_segment);
649 G_OBJECT_CLASS (parent_class)->finalize (object);
653 * gst_base_sink_set_sync:
655 * @sync: the new sync value.
657 * Configures @sink to synchronize on the clock or not. When
658 * @sync is FALSE, incomming samples will be played as fast as
659 * possible. If @sync is TRUE, the timestamps of the incomming
660 * buffers will be used to schedule the exact render time of its
666 gst_base_sink_set_sync (GstBaseSink * sink, gboolean sync)
668 g_return_if_fail (GST_IS_BASE_SINK (sink));
670 GST_OBJECT_LOCK (sink);
672 GST_OBJECT_UNLOCK (sink);
676 * gst_base_sink_get_sync:
679 * Checks if @sink is currently configured to synchronize against the
682 * Returns: TRUE if the sink is configured to synchronize against the clock.
687 gst_base_sink_get_sync (GstBaseSink * sink)
691 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
693 GST_OBJECT_LOCK (sink);
695 GST_OBJECT_UNLOCK (sink);
701 * gst_base_sink_set_max_lateness:
703 * @max_lateness: the new max lateness value.
705 * Sets the new max lateness value to @max_lateness. This value is
706 * used to decide if a buffer should be dropped or not based on the
707 * buffer timestamp and the current clock time. A value of -1 means
713 gst_base_sink_set_max_lateness (GstBaseSink * sink, gint64 max_lateness)
715 g_return_if_fail (GST_IS_BASE_SINK (sink));
717 GST_OBJECT_LOCK (sink);
718 sink->abidata.ABI.max_lateness = max_lateness;
719 GST_OBJECT_UNLOCK (sink);
723 * gst_base_sink_get_max_lateness:
726 * Gets the max lateness value. See gst_base_sink_set_max_lateness for
729 * Returns: The maximum time in nanoseconds that a buffer can be late
730 * before it is dropped and not rendered. A value of -1 means an
736 gst_base_sink_get_max_lateness (GstBaseSink * sink)
740 g_return_val_if_fail (GST_IS_BASE_SINK (sink), -1);
742 GST_OBJECT_LOCK (sink);
743 res = sink->abidata.ABI.max_lateness;
744 GST_OBJECT_UNLOCK (sink);
750 * gst_base_sink_set_qos_enabled:
752 * @enabled: the new qos value.
754 * Configures @sink to send Quality-of-Service events upstream.
759 gst_base_sink_set_qos_enabled (GstBaseSink * sink, gboolean enabled)
761 g_return_if_fail (GST_IS_BASE_SINK (sink));
763 g_atomic_int_set (&sink->priv->qos_enabled, enabled);
767 * gst_base_sink_is_qos_enabled:
770 * Checks if @sink is currently configured to send Quality-of-Service events
773 * Returns: TRUE if the sink is configured to perform Quality-of-Service.
778 gst_base_sink_is_qos_enabled (GstBaseSink * sink)
782 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
784 res = g_atomic_int_get (&sink->priv->qos_enabled);
790 * gst_base_sink_set_async_enabled:
792 * @enabled: the new async value.
794 * Configures @sink to perform all state changes asynchronusly. When async is
795 * disabled, the sink will immediatly go to PAUSED instead of waiting for a
796 * preroll buffer. This feature is usefull if the sink does not synchronize
797 * against the clock or when it is dealing with sparse streams.
802 gst_base_sink_set_async_enabled (GstBaseSink * sink, gboolean enabled)
804 g_return_if_fail (GST_IS_BASE_SINK (sink));
806 GST_PAD_PREROLL_LOCK (sink->sinkpad);
807 sink->priv->async_enabled = enabled;
808 GST_LOG_OBJECT (sink, "set async enabled to %d", enabled);
809 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
813 * gst_base_sink_is_async_enabled:
816 * Checks if @sink is currently configured to perform asynchronous state
819 * Returns: TRUE if the sink is configured to perform asynchronous state
825 gst_base_sink_is_async_enabled (GstBaseSink * sink)
829 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
831 GST_PAD_PREROLL_LOCK (sink->sinkpad);
832 res = sink->priv->async_enabled;
833 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
839 * gst_base_sink_set_ts_offset:
841 * @offset: the new offset
843 * Adjust the synchronisation of @sink with @offset. A negative value will
844 * render buffers earlier than their timestamp. A positive value will delay
845 * rendering. This function can be used to fix playback of badly timestamped
851 gst_base_sink_set_ts_offset (GstBaseSink * sink, GstClockTimeDiff offset)
853 g_return_if_fail (GST_IS_BASE_SINK (sink));
855 GST_OBJECT_LOCK (sink);
856 sink->priv->ts_offset = offset;
857 GST_LOG_OBJECT (sink, "set time offset to %" G_GINT64_FORMAT, offset);
858 GST_OBJECT_UNLOCK (sink);
862 * gst_base_sink_get_ts_offset:
865 * Get the synchronisation offset of @sink.
867 * Returns: The synchronisation offset.
872 gst_base_sink_get_ts_offset (GstBaseSink * sink)
874 GstClockTimeDiff res;
876 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
878 GST_OBJECT_LOCK (sink);
879 res = sink->priv->ts_offset;
880 GST_OBJECT_UNLOCK (sink);
886 * gst_base_sink_get_last_buffer:
889 * Get the last buffer that arrived in the sink and was used for preroll or for
890 * rendering. This property can be used to generate thumbnails.
892 * The #GstCaps on the buffer can be used to determine the type of the buffer.
894 * Returns: a #GstBuffer. gst_buffer_unref() after usage. This function returns
895 * NULL when no buffer has arrived in the sink yet or when the sink is not in
901 gst_base_sink_get_last_buffer (GstBaseSink * sink)
905 g_return_val_if_fail (GST_IS_BASE_SINK (sink), NULL);
907 GST_OBJECT_LOCK (sink);
908 if ((res = sink->priv->last_buffer))
909 gst_buffer_ref (res);
910 GST_OBJECT_UNLOCK (sink);
916 gst_base_sink_set_last_buffer (GstBaseSink * sink, GstBuffer * buffer)
920 GST_OBJECT_LOCK (sink);
921 old = sink->priv->last_buffer;
922 if (G_LIKELY (old != buffer)) {
923 GST_DEBUG_OBJECT (sink, "setting last buffer to %p", buffer);
924 if (G_LIKELY (buffer))
925 gst_buffer_ref (buffer);
926 sink->priv->last_buffer = buffer;
930 GST_OBJECT_UNLOCK (sink);
932 /* avoid unreffing with the lock because cleanup code might want to take the
935 gst_buffer_unref (old);
939 * gst_base_sink_get_latency:
942 * Get the currently configured latency.
944 * Returns: The configured latency.
949 gst_base_sink_get_latency (GstBaseSink * sink)
953 GST_OBJECT_LOCK (sink);
954 res = sink->priv->latency;
955 GST_OBJECT_UNLOCK (sink);
961 * gst_base_sink_query_latency:
963 * @live: if the sink is live
964 * @upstream_live: if an upstream element is live
965 * @min_latency: the min latency of the upstream elements
966 * @max_latency: the max latency of the upstream elements
968 * Query the sink for the latency parameters. The latency will be queried from
969 * the upstream elements. @live will be TRUE if @sink is configured to
970 * synchronize against the clock. @upstream_live will be TRUE if an upstream
973 * If both @live and @upstream_live are TRUE, the sink will want to compensate
974 * for the latency introduced by the upstream elements by setting the
975 * @min_latency to a strictly possitive value.
977 * This function is mostly used by subclasses.
979 * Returns: TRUE if the query succeeded.
984 gst_base_sink_query_latency (GstBaseSink * sink, gboolean * live,
985 gboolean * upstream_live, GstClockTime * min_latency,
986 GstClockTime * max_latency)
988 gboolean l, us_live, res, have_latency;
989 GstClockTime min, max, render_delay;
991 GstClockTime us_min, us_max;
993 /* we are live when we sync to the clock */
994 GST_OBJECT_LOCK (sink);
996 have_latency = sink->priv->have_latency;
997 render_delay = sink->priv->render_delay;
998 GST_OBJECT_UNLOCK (sink);
1000 /* assume no latency */
1006 GST_DEBUG_OBJECT (sink, "we are ready for LATENCY query");
1007 /* we are ready for a latency query this is when we preroll or when we are
1009 query = gst_query_new_latency ();
1011 /* ask the peer for the latency */
1012 if ((res = gst_base_sink_peer_query (sink, query))) {
1013 /* get upstream min and max latency */
1014 gst_query_parse_latency (query, &us_live, &us_min, &us_max);
1017 /* upstream live, use its latency, subclasses should use these
1018 * values to create the complete latency. */
1023 /* we need to add the render delay if we are live */
1025 min += render_delay;
1027 max += render_delay;
1030 gst_query_unref (query);
1032 GST_DEBUG_OBJECT (sink, "we are not yet ready for LATENCY query");
1036 /* not live, we tried to do the query, if it failed we return TRUE anyway */
1040 GST_DEBUG_OBJECT (sink, "latency query failed but we are not live");
1042 GST_DEBUG_OBJECT (sink, "latency query failed and we are live");
1047 GST_DEBUG_OBJECT (sink, "latency query: live: %d, have_latency %d,"
1048 " upstream: %d, min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT, l,
1049 have_latency, us_live, GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1054 *upstream_live = us_live;
1064 * gst_base_sink_set_render_delay:
1065 * @sink: a #GstBaseSink
1066 * @delay: the new delay
1068 * Set the render delay in @sink to @delay. The render delay is the time
1069 * between actual rendering of a buffer and its synchronisation time. Some
1070 * devices might delay media rendering which can be compensated for with this
1073 * After calling this function, this sink will report additional latency and
1074 * other sinks will adjust their latency to delay the rendering of their media.
1076 * This function is usually called by subclasses.
1081 gst_base_sink_set_render_delay (GstBaseSink * sink, GstClockTime delay)
1083 GstClockTime old_render_delay;
1085 g_return_if_fail (GST_IS_BASE_SINK (sink));
1087 GST_OBJECT_LOCK (sink);
1088 old_render_delay = sink->priv->render_delay;
1089 sink->priv->render_delay = delay;
1090 GST_LOG_OBJECT (sink, "set render delay to %" GST_TIME_FORMAT,
1091 GST_TIME_ARGS (delay));
1092 GST_OBJECT_UNLOCK (sink);
1094 if (delay != old_render_delay) {
1095 GST_DEBUG_OBJECT (sink, "posting latency changed");
1096 gst_element_post_message (GST_ELEMENT_CAST (sink),
1097 gst_message_new_latency (GST_OBJECT_CAST (sink)));
1102 * gst_base_sink_get_render_delay:
1103 * @sink: a #GstBaseSink
1105 * Get the render delay of @sink. see gst_base_sink_set_render_delay() for more
1106 * information about the render delay.
1108 * Returns: the render delay of @sink.
1113 gst_base_sink_get_render_delay (GstBaseSink * sink)
1115 GstClockTimeDiff res;
1117 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1119 GST_OBJECT_LOCK (sink);
1120 res = sink->priv->render_delay;
1121 GST_OBJECT_UNLOCK (sink);
1127 * gst_base_sink_set_blocksize:
1128 * @sink: a #GstBaseSink
1129 * @blocksize: the blocksize in bytes
1131 * Set the number of bytes that the sink will pull when it is operating in pull
1137 gst_base_sink_set_blocksize (GstBaseSink * sink, guint blocksize)
1139 g_return_if_fail (GST_IS_BASE_SINK (sink));
1141 GST_OBJECT_LOCK (sink);
1142 sink->priv->blocksize = blocksize;
1143 GST_LOG_OBJECT (sink, "set blocksize to %u", blocksize);
1144 GST_OBJECT_UNLOCK (sink);
1148 * gst_base_sink_get_blocksize:
1149 * @sink: a #GstBaseSink
1151 * Get the number of bytes that the sink will pull when it is operating in pull
1154 * Returns: the number of bytes @sink will pull in pull mode.
1159 gst_base_sink_get_blocksize (GstBaseSink * sink)
1163 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1165 GST_OBJECT_LOCK (sink);
1166 res = sink->priv->blocksize;
1167 GST_OBJECT_UNLOCK (sink);
1173 gst_base_sink_set_property (GObject * object, guint prop_id,
1174 const GValue * value, GParamSpec * pspec)
1176 GstBaseSink *sink = GST_BASE_SINK (object);
1179 case PROP_PREROLL_QUEUE_LEN:
1180 /* preroll lock necessary to serialize with finish_preroll */
1181 GST_PAD_PREROLL_LOCK (sink->sinkpad);
1182 sink->preroll_queue_max_len = g_value_get_uint (value);
1183 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
1186 gst_base_sink_set_sync (sink, g_value_get_boolean (value));
1188 case PROP_MAX_LATENESS:
1189 gst_base_sink_set_max_lateness (sink, g_value_get_int64 (value));
1192 gst_base_sink_set_qos_enabled (sink, g_value_get_boolean (value));
1195 gst_base_sink_set_async_enabled (sink, g_value_get_boolean (value));
1197 case PROP_TS_OFFSET:
1198 gst_base_sink_set_ts_offset (sink, g_value_get_int64 (value));
1200 case PROP_BLOCKSIZE:
1201 gst_base_sink_set_blocksize (sink, g_value_get_uint (value));
1203 case PROP_RENDER_DELAY:
1204 gst_base_sink_set_render_delay (sink, g_value_get_uint64 (value));
1207 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1213 gst_base_sink_get_property (GObject * object, guint prop_id, GValue * value,
1216 GstBaseSink *sink = GST_BASE_SINK (object);
1219 case PROP_PREROLL_QUEUE_LEN:
1220 GST_PAD_PREROLL_LOCK (sink->sinkpad);
1221 g_value_set_uint (value, sink->preroll_queue_max_len);
1222 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
1225 g_value_set_boolean (value, gst_base_sink_get_sync (sink));
1227 case PROP_MAX_LATENESS:
1228 g_value_set_int64 (value, gst_base_sink_get_max_lateness (sink));
1231 g_value_set_boolean (value, gst_base_sink_is_qos_enabled (sink));
1234 g_value_set_boolean (value, gst_base_sink_is_async_enabled (sink));
1236 case PROP_TS_OFFSET:
1237 g_value_set_int64 (value, gst_base_sink_get_ts_offset (sink));
1239 case PROP_LAST_BUFFER:
1240 gst_value_take_buffer (value, gst_base_sink_get_last_buffer (sink));
1242 case PROP_BLOCKSIZE:
1243 g_value_set_uint (value, gst_base_sink_get_blocksize (sink));
1245 case PROP_RENDER_DELAY:
1246 g_value_set_uint64 (value, gst_base_sink_get_render_delay (sink));
1249 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1256 gst_base_sink_get_caps (GstBaseSink * sink)
1262 gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps)
1267 static GstFlowReturn
1268 gst_base_sink_buffer_alloc (GstBaseSink * sink, guint64 offset, guint size,
1269 GstCaps * caps, GstBuffer ** buf)
1275 /* with PREROLL_LOCK, STREAM_LOCK */
1277 gst_base_sink_preroll_queue_flush (GstBaseSink * basesink, GstPad * pad)
1281 GST_DEBUG_OBJECT (basesink, "flushing queue %p", basesink);
1282 while ((obj = g_queue_pop_head (basesink->preroll_queue))) {
1283 GST_DEBUG_OBJECT (basesink, "popped %p", obj);
1284 gst_mini_object_unref (obj);
1286 /* we can't have EOS anymore now */
1287 basesink->eos = FALSE;
1288 basesink->priv->received_eos = FALSE;
1289 basesink->have_preroll = FALSE;
1290 basesink->priv->step_unlock = FALSE;
1291 basesink->eos_queued = FALSE;
1292 basesink->preroll_queued = 0;
1293 basesink->buffers_queued = 0;
1294 basesink->events_queued = 0;
1295 /* can't report latency anymore until we preroll again */
1296 if (basesink->priv->async_enabled) {
1297 GST_OBJECT_LOCK (basesink);
1298 basesink->priv->have_latency = FALSE;
1299 GST_OBJECT_UNLOCK (basesink);
1301 /* and signal any waiters now */
1302 GST_PAD_PREROLL_SIGNAL (pad);
1305 /* with STREAM_LOCK, configures given segment with the event information. */
1307 gst_base_sink_configure_segment (GstBaseSink * basesink, GstPad * pad,
1308 GstEvent * event, GstSegment * segment)
1311 gdouble rate, arate;
1317 /* the newsegment event is needed to bring the buffer timestamps to the
1318 * stream time and to drop samples outside of the playback segment. */
1319 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1320 &start, &stop, &time);
1322 /* The segment is protected with both the STREAM_LOCK and the OBJECT_LOCK.
1323 * We protect with the OBJECT_LOCK so that we can use the values to
1324 * safely answer a POSITION query. */
1325 GST_OBJECT_LOCK (basesink);
1326 gst_segment_set_newsegment_full (segment, update, rate, arate, format, start,
1329 if (format == GST_FORMAT_TIME) {
1330 GST_DEBUG_OBJECT (basesink,
1331 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1332 "format GST_FORMAT_TIME, "
1333 "%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
1334 ", time %" GST_TIME_FORMAT ", accum %" GST_TIME_FORMAT,
1335 update, rate, arate, GST_TIME_ARGS (segment->start),
1336 GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time),
1337 GST_TIME_ARGS (segment->accum));
1339 GST_DEBUG_OBJECT (basesink,
1340 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1342 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
1343 G_GINT64_FORMAT ", accum %" G_GINT64_FORMAT, update, rate, arate,
1344 segment->format, segment->start, segment->stop, segment->time,
1347 GST_OBJECT_UNLOCK (basesink);
1350 /* with PREROLL_LOCK, STREAM_LOCK */
1352 gst_base_sink_commit_state (GstBaseSink * basesink)
1354 /* commit state and proceed to next pending state */
1355 GstState current, next, pending, post_pending;
1356 gboolean post_paused = FALSE;
1357 gboolean post_async_done = FALSE;
1358 gboolean post_playing = FALSE;
1360 /* we are certainly not playing async anymore now */
1361 basesink->playing_async = FALSE;
1363 GST_OBJECT_LOCK (basesink);
1364 current = GST_STATE (basesink);
1365 next = GST_STATE_NEXT (basesink);
1366 pending = GST_STATE_PENDING (basesink);
1367 post_pending = pending;
1370 case GST_STATE_PLAYING:
1372 GstBaseSinkClass *bclass;
1373 GstStateChangeReturn ret;
1375 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1377 GST_DEBUG_OBJECT (basesink, "commiting state to PLAYING");
1379 basesink->need_preroll = FALSE;
1380 post_async_done = TRUE;
1381 basesink->priv->commited = TRUE;
1382 post_playing = TRUE;
1383 /* post PAUSED too when we were READY */
1384 if (current == GST_STATE_READY) {
1388 /* make sure we notify the subclass of async playing */
1389 if (bclass->async_play) {
1390 GST_WARNING_OBJECT (basesink, "deprecated async_play");
1391 ret = bclass->async_play (basesink);
1392 if (ret == GST_STATE_CHANGE_FAILURE)
1397 case GST_STATE_PAUSED:
1398 GST_DEBUG_OBJECT (basesink, "commiting state to PAUSED");
1400 post_async_done = TRUE;
1401 basesink->priv->commited = TRUE;
1402 post_pending = GST_STATE_VOID_PENDING;
1404 case GST_STATE_READY:
1405 case GST_STATE_NULL:
1407 case GST_STATE_VOID_PENDING:
1408 goto nothing_pending;
1413 /* we can report latency queries now */
1414 basesink->priv->have_latency = TRUE;
1416 GST_STATE (basesink) = pending;
1417 GST_STATE_NEXT (basesink) = GST_STATE_VOID_PENDING;
1418 GST_STATE_PENDING (basesink) = GST_STATE_VOID_PENDING;
1419 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_SUCCESS;
1420 GST_OBJECT_UNLOCK (basesink);
1423 GST_DEBUG_OBJECT (basesink, "posting PAUSED state change message");
1424 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1425 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1426 current, next, post_pending));
1428 if (post_async_done) {
1429 GST_DEBUG_OBJECT (basesink, "posting async-done message");
1430 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1431 gst_message_new_async_done (GST_OBJECT_CAST (basesink)));
1434 GST_DEBUG_OBJECT (basesink, "posting PLAYING state change message");
1435 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1436 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1437 next, pending, GST_STATE_VOID_PENDING));
1440 GST_STATE_BROADCAST (basesink);
1446 /* Depending on the state, set our vars. We get in this situation when the
1447 * state change function got a change to update the state vars before the
1448 * streaming thread did. This is fine but we need to make sure that we
1449 * update the need_preroll var since it was TRUE when we got here and might
1450 * become FALSE if we got to PLAYING. */
1451 GST_DEBUG_OBJECT (basesink, "nothing to commit, now in %s",
1452 gst_element_state_get_name (current));
1454 case GST_STATE_PLAYING:
1455 basesink->need_preroll = FALSE;
1457 case GST_STATE_PAUSED:
1458 basesink->need_preroll = TRUE;
1461 basesink->need_preroll = FALSE;
1462 basesink->flushing = TRUE;
1465 /* we can report latency queries now */
1466 basesink->priv->have_latency = TRUE;
1467 GST_OBJECT_UNLOCK (basesink);
1472 /* app is going to READY */
1473 GST_DEBUG_OBJECT (basesink, "stopping");
1474 basesink->need_preroll = FALSE;
1475 basesink->flushing = TRUE;
1476 GST_OBJECT_UNLOCK (basesink);
1481 GST_DEBUG_OBJECT (basesink, "async commit failed");
1482 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_FAILURE;
1483 GST_OBJECT_UNLOCK (basesink);
1489 start_stepping (GstBaseSink * sink, GstSegment * segment,
1490 GstStepInfo * pending, GstStepInfo * current)
1493 GstMessage *message;
1495 GST_DEBUG_OBJECT (sink, "update pending step");
1497 GST_OBJECT_LOCK (sink);
1498 memcpy (current, pending, sizeof (GstStepInfo));
1499 pending->valid = FALSE;
1500 GST_OBJECT_UNLOCK (sink);
1502 /* post message first */
1504 gst_message_new_step_start (GST_OBJECT (sink), TRUE, current->format,
1505 current->amount, current->rate, current->flush, current->intermediate);
1506 gst_message_set_seqnum (message, current->seqnum);
1507 gst_element_post_message (GST_ELEMENT (sink), message);
1509 /* get the running time of where we paused and remember it */
1510 current->start = gst_element_get_start_time (GST_ELEMENT_CAST (sink));
1511 gst_segment_set_running_time (segment, GST_FORMAT_TIME, current->start);
1513 /* set the new rate for the remainder of the segment */
1514 current->start_rate = segment->rate;
1515 segment->rate *= current->rate;
1516 segment->abs_rate = ABS (segment->rate);
1519 if (segment->rate > 0.0)
1520 current->start_stop = segment->stop;
1522 current->start_start = segment->start;
1524 if (current->format == GST_FORMAT_TIME) {
1525 end = current->start + current->amount;
1526 if (!current->flush) {
1527 /* update the segment clipping regions for non-flushing seeks */
1528 if (segment->rate > 0.0) {
1529 segment->stop = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1530 segment->last_stop = segment->stop;
1534 position = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1535 segment->time = position;
1536 segment->start = position;
1537 segment->last_stop = position;
1542 GST_DEBUG_OBJECT (sink,
1543 "segment now rate %lf, applied rate %lf, "
1544 "format GST_FORMAT_TIME, "
1545 "%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
1546 ", time %" GST_TIME_FORMAT ", accum %" GST_TIME_FORMAT,
1547 segment->rate, segment->applied_rate, GST_TIME_ARGS (segment->start),
1548 GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time),
1549 GST_TIME_ARGS (segment->accum));
1551 GST_DEBUG_OBJECT (sink, "step started at running_time %" GST_TIME_FORMAT,
1552 GST_TIME_ARGS (current->start));
1554 if (current->amount == -1) {
1555 GST_DEBUG_OBJECT (sink, "step amount == -1, stop stepping");
1556 current->valid = FALSE;
1558 GST_DEBUG_OBJECT (sink, "step amount: %" G_GUINT64_FORMAT ", format: %s, "
1559 "rate: %f", current->amount, gst_format_get_name (current->format),
1565 stop_stepping (GstBaseSink * sink, GstSegment * segment,
1566 GstStepInfo * current, gint64 rstart, gint64 rstop, gboolean eos)
1568 gint64 stop, position;
1569 GstMessage *message;
1571 GST_DEBUG_OBJECT (sink, "step complete");
1573 if (segment->rate > 0.0)
1578 GST_DEBUG_OBJECT (sink,
1579 "step stop at running_time %" GST_TIME_FORMAT, GST_TIME_ARGS (stop));
1582 current->duration = current->position;
1584 current->duration = stop - current->start;
1586 GST_DEBUG_OBJECT (sink, "step elapsed running_time %" GST_TIME_FORMAT,
1587 GST_TIME_ARGS (current->duration));
1589 position = current->start + current->duration;
1591 /* now move the segment to the new running time */
1592 gst_segment_set_running_time (segment, GST_FORMAT_TIME, position);
1594 if (current->flush) {
1595 /* and remove the accumulated time we flushed, start time did not change */
1596 segment->accum = current->start;
1598 /* start time is now the stepped position */
1599 gst_element_set_start_time (GST_ELEMENT_CAST (sink), position);
1602 /* restore the previous rate */
1603 segment->rate = current->start_rate;
1604 segment->abs_rate = ABS (segment->rate);
1606 if (segment->rate > 0.0)
1607 segment->stop = current->start_stop;
1609 segment->start = current->start_start;
1611 /* the clip segment is used for position report in paused... */
1612 memcpy (sink->abidata.ABI.clip_segment, segment, sizeof (GstSegment));
1614 /* post the step done when we know the stepped duration in TIME */
1616 gst_message_new_step_done (GST_OBJECT_CAST (sink), current->format,
1617 current->amount, current->rate, current->flush, current->intermediate,
1618 current->duration, eos);
1619 gst_message_set_seqnum (message, current->seqnum);
1620 gst_element_post_message (GST_ELEMENT_CAST (sink), message);
1622 if (!current->intermediate)
1623 sink->need_preroll = current->need_preroll;
1625 /* and the current step info finished and becomes invalid */
1626 current->valid = FALSE;
1630 handle_stepping (GstBaseSink * sink, GstSegment * segment,
1631 GstStepInfo * current, gint64 * cstart, gint64 * cstop, gint64 * rstart,
1634 gboolean step_end = FALSE;
1636 /* see if we need to skip this buffer because of stepping */
1637 switch (current->format) {
1638 case GST_FORMAT_TIME:
1643 if (segment->rate > 0.0) {
1651 end = current->start + current->amount;
1652 current->position = first - current->start;
1654 if (G_UNLIKELY (segment->abs_rate != 1.0))
1655 current->position /= segment->abs_rate;
1657 GST_DEBUG_OBJECT (sink,
1658 "buffer: %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT,
1659 GST_TIME_ARGS (first), GST_TIME_ARGS (last));
1660 GST_DEBUG_OBJECT (sink,
1661 "got time step %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT "/%"
1662 GST_TIME_FORMAT, GST_TIME_ARGS (current->position),
1663 GST_TIME_ARGS (last - current->start),
1664 GST_TIME_ARGS (current->amount));
1666 if ((current->flush && current->position >= current->amount)
1668 GST_DEBUG_OBJECT (sink, "step ended, we need clipping");
1670 if (segment->rate > 0.0) {
1672 *cstart = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1675 *cstop = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1678 GST_DEBUG_OBJECT (sink,
1679 "cstart %" GST_TIME_FORMAT ", rstart %" GST_TIME_FORMAT,
1680 GST_TIME_ARGS (*cstart), GST_TIME_ARGS (*rstart));
1681 GST_DEBUG_OBJECT (sink,
1682 "cstop %" GST_TIME_FORMAT ", rstop %" GST_TIME_FORMAT,
1683 GST_TIME_ARGS (*cstop), GST_TIME_ARGS (*rstop));
1686 case GST_FORMAT_BUFFERS:
1687 GST_DEBUG_OBJECT (sink,
1688 "got default step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1689 current->position, current->amount);
1691 if (current->position < current->amount) {
1692 current->position++;
1697 case GST_FORMAT_DEFAULT:
1699 GST_DEBUG_OBJECT (sink,
1700 "got unknown step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1701 current->position, current->amount);
1707 /* with STREAM_LOCK, PREROLL_LOCK
1709 * Returns TRUE if the object needs synchronisation and takes therefore
1710 * part in prerolling.
1712 * rsstart/rsstop contain the start/stop in stream time.
1713 * rrstart/rrstop contain the start/stop in running time.
1716 gst_base_sink_get_sync_times (GstBaseSink * basesink, GstMiniObject * obj,
1717 GstClockTime * rsstart, GstClockTime * rsstop,
1718 GstClockTime * rrstart, GstClockTime * rrstop, gboolean * do_sync,
1719 gboolean * stepped, GstSegment * segment, GstStepInfo * step,
1720 gboolean * step_end)
1722 GstBaseSinkClass *bclass;
1724 GstClockTime start, stop; /* raw start/stop timestamps */
1725 gint64 cstart, cstop; /* clipped raw timestamps */
1726 gint64 rstart, rstop; /* clipped timestamps converted to running time */
1727 GstClockTime sstart, sstop; /* clipped timestamps converted to stream time */
1729 GstBaseSinkPrivate *priv;
1732 priv = basesink->priv;
1734 /* start with nothing */
1737 if (G_UNLIKELY (GST_IS_EVENT (obj))) {
1738 GstEvent *event = GST_EVENT_CAST (obj);
1740 switch (GST_EVENT_TYPE (event)) {
1741 /* EOS event needs syncing */
1744 if (basesink->segment.rate >= 0.0) {
1745 sstart = sstop = priv->current_sstop;
1747 /* we have not seen a buffer yet, use the segment values */
1748 sstart = sstop = gst_segment_to_stream_time (&basesink->segment,
1749 basesink->segment.format, basesink->segment.stop);
1752 sstart = sstop = priv->current_sstart;
1754 /* we have not seen a buffer yet, use the segment values */
1755 sstart = sstop = gst_segment_to_stream_time (&basesink->segment,
1756 basesink->segment.format, basesink->segment.start);
1760 rstart = rstop = priv->eos_rtime;
1761 *do_sync = rstart != -1;
1762 GST_DEBUG_OBJECT (basesink, "sync times for EOS %" GST_TIME_FORMAT,
1763 GST_TIME_ARGS (rstart));
1764 /* if we are stepping, we end now */
1765 *step_end = step->valid;
1770 /* other events do not need syncing */
1771 /* FIXME, maybe NEWSEGMENT might need synchronisation
1772 * since the POSITION query depends on accumulated times and
1773 * we cannot accumulate the current segment before the previous
1782 /* else do buffer sync code */
1783 buffer = GST_BUFFER_CAST (obj);
1785 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1787 /* just get the times to see if we need syncing, if the start returns -1 we
1789 if (bclass->get_times)
1790 bclass->get_times (basesink, buffer, &start, &stop);
1793 /* we don't need to sync but we still want to get the timestamps for
1794 * tracking the position */
1795 gst_base_sink_get_times (basesink, buffer, &start, &stop);
1801 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
1802 ", stop: %" GST_TIME_FORMAT ", do_sync %d", GST_TIME_ARGS (start),
1803 GST_TIME_ARGS (stop), *do_sync);
1805 /* collect segment and format for code clarity */
1806 format = segment->format;
1808 /* no timestamp clipping if we did not get a TIME segment format */
1809 if (G_UNLIKELY (format != GST_FORMAT_TIME)) {
1812 /* do running and stream time in TIME format */
1813 format = GST_FORMAT_TIME;
1814 GST_LOG_OBJECT (basesink, "not time format, don't clip");
1818 /* clip, only when we know about time */
1819 if (G_UNLIKELY (!gst_segment_clip (segment, GST_FORMAT_TIME,
1820 (gint64) start, (gint64) stop, &cstart, &cstop))) {
1822 GST_DEBUG_OBJECT (basesink, "step out of segment");
1823 /* when we are stepping, pretend we're at the end of the segment */
1824 if (segment->rate > 0.0) {
1825 cstart = segment->stop;
1826 cstop = segment->stop;
1828 cstart = segment->start;
1829 cstop = segment->start;
1833 goto out_of_segment;
1836 if (G_UNLIKELY (start != cstart || stop != cstop)) {
1837 GST_DEBUG_OBJECT (basesink, "clipped to: start %" GST_TIME_FORMAT
1838 ", stop: %" GST_TIME_FORMAT, GST_TIME_ARGS (cstart),
1839 GST_TIME_ARGS (cstop));
1842 /* set last stop position */
1843 if (G_LIKELY (cstop != GST_CLOCK_TIME_NONE))
1844 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, cstop);
1846 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, cstart);
1849 rstart = gst_segment_to_running_time (segment, format, cstart);
1850 rstop = gst_segment_to_running_time (segment, format, cstop);
1852 if (G_UNLIKELY (step->valid)) {
1853 if (!(*step_end = handle_stepping (basesink, segment, step, &cstart, &cstop,
1854 &rstart, &rstop))) {
1855 /* step is still busy, we discard data when we are flushing */
1856 *stepped = step->flush;
1859 /* this can produce wrong values if we accumulated non-TIME segments. If this happens,
1860 * upstream is behaving very badly */
1861 sstart = gst_segment_to_stream_time (segment, format, cstart);
1862 sstop = gst_segment_to_stream_time (segment, format, cstop);
1865 /* eos_done label only called when doing EOS, we also stop stepping then */
1866 if (*step_end && step->flush) {
1867 GST_DEBUG_OBJECT (basesink, "flushing step ended");
1868 stop_stepping (basesink, segment, step, rstart, rstop, eos);
1878 /* buffers and EOS always need syncing and preroll */
1884 /* we usually clip in the chain function already but stepping could cause
1885 * the segment to be updated later. we return FALSE so that we don't try
1887 GST_LOG_OBJECT (basesink, "buffer skipped, not in segment");
1892 /* with STREAM_LOCK, PREROLL_LOCK, LOCK
1893 * adjust a timestamp with the latency and timestamp offset */
1895 gst_base_sink_adjust_time (GstBaseSink * basesink, GstClockTime time)
1897 GstClockTimeDiff ts_offset;
1899 /* don't do anything funny with invalid timestamps */
1900 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
1903 time += basesink->priv->latency;
1905 /* apply offset, be carefull for underflows */
1906 ts_offset = basesink->priv->ts_offset;
1907 if (ts_offset < 0) {
1908 ts_offset = -ts_offset;
1909 if (ts_offset < time)
1920 * gst_base_sink_wait_clock:
1922 * @time: the running_time to be reached
1923 * @jitter: the jitter to be filled with time diff (can be NULL)
1925 * This function will block until @time is reached. It is usually called by
1926 * subclasses that use their own internal synchronisation.
1928 * If @time is not valid, no sycnhronisation is done and #GST_CLOCK_BADTIME is
1929 * returned. Likewise, if synchronisation is disabled in the element or there
1930 * is no clock, no synchronisation is done and #GST_CLOCK_BADTIME is returned.
1932 * This function should only be called with the PREROLL_LOCK held, like when
1933 * receiving an EOS event in the ::event vmethod or when receiving a buffer in
1934 * the ::render vmethod.
1936 * The @time argument should be the running_time of when this method should
1937 * return and is not adjusted with any latency or offset configured in the
1942 * Returns: #GstClockReturn
1945 gst_base_sink_wait_clock (GstBaseSink * sink, GstClockTime time,
1946 GstClockTimeDiff * jitter)
1952 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
1955 GST_OBJECT_LOCK (sink);
1956 if (G_UNLIKELY (!sink->sync))
1959 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (sink)) == NULL))
1962 /* add base_time to running_time to get the time against the clock */
1963 time += GST_ELEMENT_CAST (sink)->base_time;
1965 id = gst_clock_new_single_shot_id (clock, time);
1966 GST_OBJECT_UNLOCK (sink);
1968 /* A blocking wait is performed on the clock. We save the ClockID
1969 * so we can unlock the entry at any time. While we are blocking, we
1970 * release the PREROLL_LOCK so that other threads can interrupt the
1972 sink->clock_id = id;
1973 /* release the preroll lock while waiting */
1974 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
1976 ret = gst_clock_id_wait (id, jitter);
1978 GST_PAD_PREROLL_LOCK (sink->sinkpad);
1979 gst_clock_id_unref (id);
1980 sink->clock_id = NULL;
1984 /* no syncing needed */
1987 GST_DEBUG_OBJECT (sink, "time not valid, no sync needed");
1988 return GST_CLOCK_BADTIME;
1992 GST_DEBUG_OBJECT (sink, "sync disabled");
1993 GST_OBJECT_UNLOCK (sink);
1994 return GST_CLOCK_BADTIME;
1998 GST_DEBUG_OBJECT (sink, "no clock, can't sync");
1999 GST_OBJECT_UNLOCK (sink);
2000 return GST_CLOCK_BADTIME;
2005 * gst_base_sink_wait_preroll:
2008 * If the #GstBaseSinkClass::render method performs its own synchronisation against
2009 * the clock it must unblock when going from PLAYING to the PAUSED state and call
2010 * this method before continuing to render the remaining data.
2012 * This function will block until a state change to PLAYING happens (in which
2013 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
2014 * to a state change to READY or a FLUSH event (in which case this function
2015 * returns #GST_FLOW_WRONG_STATE).
2017 * This function should only be called with the PREROLL_LOCK held, like in the
2022 * Returns: #GST_FLOW_OK if the preroll completed and processing can
2023 * continue. Any other return value should be returned from the render vmethod.
2026 gst_base_sink_wait_preroll (GstBaseSink * sink)
2028 sink->have_preroll = TRUE;
2029 GST_DEBUG_OBJECT (sink, "waiting in preroll for flush or PLAYING");
2030 /* block until the state changes, or we get a flush, or something */
2031 GST_PAD_PREROLL_WAIT (sink->sinkpad);
2032 sink->have_preroll = FALSE;
2033 if (G_UNLIKELY (sink->flushing))
2035 if (G_UNLIKELY (sink->priv->step_unlock))
2037 GST_DEBUG_OBJECT (sink, "continue after preroll");
2044 GST_DEBUG_OBJECT (sink, "preroll interrupted because of flush");
2045 return GST_FLOW_WRONG_STATE;
2049 sink->priv->step_unlock = FALSE;
2050 GST_DEBUG_OBJECT (sink, "preroll interrupted because of step");
2051 return GST_FLOW_STEP;
2056 * gst_base_sink_do_preroll:
2058 * @obj: the object that caused the preroll
2060 * If the @sink spawns its own thread for pulling buffers from upstream it
2061 * should call this method after it has pulled a buffer. If the element needed
2062 * to preroll, this function will perform the preroll and will then block
2063 * until the element state is changed.
2065 * This function should be called with the PREROLL_LOCK held.
2069 * Returns: #GST_FLOW_OK if the preroll completed and processing can
2070 * continue. Any other return value should be returned from the render vmethod.
2073 gst_base_sink_do_preroll (GstBaseSink * sink, GstMiniObject * obj)
2077 while (G_UNLIKELY (sink->need_preroll)) {
2078 GST_DEBUG_OBJECT (sink, "prerolling object %p", obj);
2080 ret = gst_base_sink_preroll_object (sink, FALSE, obj);
2081 if (ret != GST_FLOW_OK)
2082 goto preroll_failed;
2084 /* need to recheck here because the commit state could have
2085 * made us not need the preroll anymore */
2086 if (G_LIKELY (sink->need_preroll)) {
2087 /* block until the state changes, or we get a flush, or something */
2088 ret = gst_base_sink_wait_preroll (sink);
2089 if (ret != GST_FLOW_OK) {
2090 if (ret == GST_FLOW_STEP)
2093 goto preroll_failed;
2102 GST_DEBUG_OBJECT (sink, "preroll failed %d", ret);
2108 * gst_base_sink_wait_eos:
2110 * @time: the running_time to be reached
2111 * @jitter: the jitter to be filled with time diff (can be NULL)
2113 * This function will block until @time is reached. It is usually called by
2114 * subclasses that use their own internal synchronisation but want to let the
2115 * EOS be handled by the base class.
2117 * This function should only be called with the PREROLL_LOCK held, like when
2118 * receiving an EOS event in the ::event vmethod.
2120 * The @time argument should be the running_time of when the EOS should happen
2121 * and will be adjusted with any latency and offset configured in the sink.
2125 * Returns: #GstFlowReturn
2128 gst_base_sink_wait_eos (GstBaseSink * sink, GstClockTime time,
2129 GstClockTimeDiff * jitter)
2131 GstClockReturn status;
2137 GST_DEBUG_OBJECT (sink, "checking preroll");
2139 /* first wait for the playing state before we can continue */
2140 if (G_UNLIKELY (sink->need_preroll)) {
2141 ret = gst_base_sink_wait_preroll (sink);
2142 if (ret != GST_FLOW_OK) {
2143 if (ret == GST_FLOW_STEP)
2150 /* preroll done, we can sync since we are in PLAYING now. */
2151 GST_DEBUG_OBJECT (sink, "possibly waiting for clock to reach %"
2152 GST_TIME_FORMAT, GST_TIME_ARGS (time));
2154 /* compensate for latency and ts_offset. We don't adjust for render delay
2155 * because we don't interact with the device on EOS normally. */
2156 stime = gst_base_sink_adjust_time (sink, time);
2158 /* wait for the clock, this can be interrupted because we got shut down or
2160 status = gst_base_sink_wait_clock (sink, stime, jitter);
2162 GST_DEBUG_OBJECT (sink, "clock returned %d", status);
2164 /* invalid time, no clock or sync disabled, just continue then */
2165 if (status == GST_CLOCK_BADTIME)
2168 /* waiting could have been interrupted and we can be flushing now */
2169 if (G_UNLIKELY (sink->flushing))
2172 /* retry if we got unscheduled, which means we did not reach the timeout
2173 * yet. if some other error occures, we continue. */
2174 } while (status == GST_CLOCK_UNSCHEDULED);
2176 GST_DEBUG_OBJECT (sink, "end of stream");
2183 GST_DEBUG_OBJECT (sink, "we are flushing");
2184 return GST_FLOW_WRONG_STATE;
2188 /* with STREAM_LOCK, PREROLL_LOCK
2190 * Make sure we are in PLAYING and synchronize an object to the clock.
2192 * If we need preroll, we are not in PLAYING. We try to commit the state
2193 * if needed and then block if we still are not PLAYING.
2195 * We start waiting on the clock in PLAYING. If we got interrupted, we
2196 * immediatly try to re-preroll.
2198 * Some objects do not need synchronisation (most events) and so this function
2199 * immediatly returns GST_FLOW_OK.
2201 * for objects that arrive later than max-lateness to be synchronized to the
2202 * clock have the @late boolean set to TRUE.
2204 * This function keeps a running average of the jitter (the diff between the
2205 * clock time and the requested sync time). The jitter is negative for
2206 * objects that arrive in time and positive for late buffers.
2208 * does not take ownership of obj.
2210 static GstFlowReturn
2211 gst_base_sink_do_sync (GstBaseSink * basesink, GstPad * pad,
2212 GstMiniObject * obj, gboolean * late, gboolean * step_end)
2214 GstClockTimeDiff jitter;
2216 GstClockReturn status = GST_CLOCK_OK;
2217 GstClockTime rstart, rstop, sstart, sstop, stime;
2219 GstBaseSinkPrivate *priv;
2221 GstStepInfo *current, *pending;
2224 priv = basesink->priv;
2227 sstart = sstop = rstart = rstop = -1;
2231 priv->current_rstart = -1;
2233 /* get stepping info */
2234 current = &priv->current_step;
2235 pending = &priv->pending_step;
2237 /* get timing information for this object against the render segment */
2238 syncable = gst_base_sink_get_sync_times (basesink, obj,
2239 &sstart, &sstop, &rstart, &rstop, &do_sync, &stepped, &basesink->segment,
2242 if (G_UNLIKELY (stepped))
2245 /* a syncable object needs to participate in preroll and
2246 * clocking. All buffers and EOS are syncable. */
2247 if (G_UNLIKELY (!syncable))
2250 /* store timing info for current object */
2251 priv->current_rstart = rstart;
2252 priv->current_rstop = (rstop != -1 ? rstop : rstart);
2254 /* save sync time for eos when the previous object needed sync */
2255 priv->eos_rtime = (do_sync ? priv->current_rstop : -1);
2258 /* first do preroll, this makes sure we commit our state
2259 * to PAUSED and can continue to PLAYING. We cannot perform
2260 * any clock sync in PAUSED because there is no clock. */
2261 ret = gst_base_sink_do_preroll (basesink, obj);
2262 if (G_UNLIKELY (ret != GST_FLOW_OK))
2263 goto preroll_failed;
2265 /* After rendering we store the position of the last buffer so that we can use
2266 * it to report the position. We need to take the lock here. */
2267 GST_OBJECT_LOCK (basesink);
2268 priv->current_sstart = sstart;
2269 priv->current_sstop = (sstop != -1 ? sstop : sstart);
2270 GST_OBJECT_UNLOCK (basesink);
2272 /* update the segment with a pending step if the current one is invalid and we
2273 * have a new pending one. We only accept new step updates after a preroll */
2274 if (G_UNLIKELY (pending->valid && !current->valid)) {
2275 start_stepping (basesink, &basesink->segment, pending, current);
2282 /* adjust for latency */
2283 stime = gst_base_sink_adjust_time (basesink, rstart);
2285 /* adjust for render-delay, avoid underflows */
2287 if (stime > priv->render_delay)
2288 stime -= priv->render_delay;
2293 /* preroll done, we can sync since we are in PLAYING now. */
2294 GST_DEBUG_OBJECT (basesink, "possibly waiting for clock to reach %"
2295 GST_TIME_FORMAT ", adjusted %" GST_TIME_FORMAT,
2296 GST_TIME_ARGS (rstart), GST_TIME_ARGS (stime));
2298 /* This function will return immediatly if start == -1, no clock
2299 * or sync is disabled with GST_CLOCK_BADTIME. */
2300 status = gst_base_sink_wait_clock (basesink, stime, &jitter);
2302 GST_DEBUG_OBJECT (basesink, "clock returned %d", status);
2304 /* invalid time, no clock or sync disabled, just render */
2305 if (status == GST_CLOCK_BADTIME)
2308 /* waiting could have been interrupted and we can be flushing now */
2309 if (G_UNLIKELY (basesink->flushing))
2312 /* check for unlocked by a state change, we are not flushing so
2313 * we can try to preroll on the current buffer. */
2314 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
2315 GST_DEBUG_OBJECT (basesink, "unscheduled, waiting some more");
2316 priv->call_preroll = TRUE;
2320 /* successful syncing done, record observation */
2321 priv->current_jitter = jitter;
2323 /* check if the object should be dropped */
2324 *late = gst_base_sink_is_too_late (basesink, obj, rstart, rstop,
2333 GST_DEBUG_OBJECT (basesink, "skipped stepped object %p", obj);
2339 GST_DEBUG_OBJECT (basesink, "non syncable object %p", obj);
2344 GST_DEBUG_OBJECT (basesink, "we are flushing");
2345 return GST_FLOW_WRONG_STATE;
2349 GST_DEBUG_OBJECT (basesink, "preroll failed");
2356 gst_base_sink_send_qos (GstBaseSink * basesink,
2357 gdouble proportion, GstClockTime time, GstClockTimeDiff diff)
2362 /* generate Quality-of-Service event */
2363 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2364 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2365 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (time));
2367 event = gst_event_new_qos (proportion, diff, time);
2370 res = gst_pad_push_event (basesink->sinkpad, event);
2376 gst_base_sink_perform_qos (GstBaseSink * sink, gboolean dropped)
2378 GstBaseSinkPrivate *priv;
2379 GstClockTime start, stop;
2380 GstClockTimeDiff jitter;
2381 GstClockTime pt, entered, left;
2382 GstClockTime duration;
2387 start = priv->current_rstart;
2389 if (priv->current_step.valid)
2392 /* if Quality-of-Service disabled, do nothing */
2393 if (!g_atomic_int_get (&priv->qos_enabled) || start == -1)
2396 stop = priv->current_rstop;
2397 jitter = priv->current_jitter;
2400 /* this is the time the buffer entered the sink */
2401 if (start < -jitter)
2404 entered = start + jitter;
2407 /* this is the time the buffer entered the sink */
2408 entered = start + jitter;
2409 /* this is the time the buffer left the sink */
2410 left = start + jitter;
2413 /* calculate duration of the buffer */
2415 duration = stop - start;
2419 /* if we have the time when the last buffer left us, calculate
2420 * processing time */
2421 if (priv->last_left != -1) {
2422 if (entered > priv->last_left) {
2423 pt = entered - priv->last_left;
2431 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "start: %" GST_TIME_FORMAT
2432 ", entered %" GST_TIME_FORMAT ", left %" GST_TIME_FORMAT ", pt: %"
2433 GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT ",jitter %"
2434 G_GINT64_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (entered),
2435 GST_TIME_ARGS (left), GST_TIME_ARGS (pt), GST_TIME_ARGS (duration),
2438 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "avg_duration: %" GST_TIME_FORMAT
2439 ", avg_pt: %" GST_TIME_FORMAT ", avg_rate: %g",
2440 GST_TIME_ARGS (priv->avg_duration), GST_TIME_ARGS (priv->avg_pt),
2443 /* collect running averages. for first observations, we copy the
2445 if (priv->avg_duration == -1)
2446 priv->avg_duration = duration;
2448 priv->avg_duration = UPDATE_RUNNING_AVG (priv->avg_duration, duration);
2450 if (priv->avg_pt == -1)
2453 priv->avg_pt = UPDATE_RUNNING_AVG (priv->avg_pt, pt);
2455 if (priv->avg_duration != 0)
2457 gst_guint64_to_gdouble (priv->avg_pt) /
2458 gst_guint64_to_gdouble (priv->avg_duration);
2462 if (priv->last_left != -1) {
2463 if (dropped || priv->avg_rate < 0.0) {
2464 priv->avg_rate = rate;
2467 priv->avg_rate = UPDATE_RUNNING_AVG_N (priv->avg_rate, rate);
2469 priv->avg_rate = UPDATE_RUNNING_AVG_P (priv->avg_rate, rate);
2473 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink,
2474 "updated: avg_duration: %" GST_TIME_FORMAT ", avg_pt: %" GST_TIME_FORMAT
2475 ", avg_rate: %g", GST_TIME_ARGS (priv->avg_duration),
2476 GST_TIME_ARGS (priv->avg_pt), priv->avg_rate);
2479 if (priv->avg_rate >= 0.0) {
2480 /* if we have a valid rate, start sending QoS messages */
2481 if (priv->current_jitter < 0) {
2482 /* make sure we never go below 0 when adding the jitter to the
2484 if (priv->current_rstart < -priv->current_jitter)
2485 priv->current_jitter = -priv->current_rstart;
2487 gst_base_sink_send_qos (sink, priv->avg_rate, priv->current_rstart,
2488 priv->current_jitter);
2491 /* record when this buffer will leave us */
2492 priv->last_left = left;
2495 /* reset all qos measuring */
2497 gst_base_sink_reset_qos (GstBaseSink * sink)
2499 GstBaseSinkPrivate *priv;
2503 priv->last_in_time = -1;
2504 priv->last_left = -1;
2505 priv->avg_duration = -1;
2507 priv->avg_rate = -1.0;
2508 priv->avg_render = -1;
2514 /* Checks if the object was scheduled too late.
2516 * start/stop contain the raw timestamp start and stop values
2519 * status and jitter contain the return values from the clock wait.
2521 * returns TRUE if the buffer was too late.
2524 gst_base_sink_is_too_late (GstBaseSink * basesink, GstMiniObject * obj,
2525 GstClockTime start, GstClockTime stop,
2526 GstClockReturn status, GstClockTimeDiff jitter)
2529 gint64 max_lateness;
2530 GstBaseSinkPrivate *priv;
2532 priv = basesink->priv;
2536 /* only for objects that were too late */
2537 if (G_LIKELY (status != GST_CLOCK_EARLY))
2540 max_lateness = basesink->abidata.ABI.max_lateness;
2542 /* check if frame dropping is enabled */
2543 if (max_lateness == -1)
2546 /* only check for buffers */
2547 if (G_UNLIKELY (!GST_IS_BUFFER (obj)))
2550 /* can't do check if we don't have a timestamp */
2551 if (G_UNLIKELY (start == -1))
2554 /* we can add a valid stop time */
2556 max_lateness += stop;
2558 max_lateness += start;
2560 /* if the jitter bigger than duration and lateness we are too late */
2561 if ((late = start + jitter > max_lateness)) {
2562 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, basesink,
2563 "buffer is too late %" GST_TIME_FORMAT
2564 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (start + jitter),
2565 GST_TIME_ARGS (max_lateness));
2566 /* !!emergency!!, if we did not receive anything valid for more than a
2567 * second, render it anyway so the user sees something */
2568 if (priv->last_in_time != -1 && start - priv->last_in_time > GST_SECOND) {
2570 GST_ELEMENT_WARNING (basesink, CORE, CLOCK,
2571 (_("A lot of buffers are being dropped.")),
2572 ("There may be a timestamping problem, or this computer is too slow."));
2573 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, basesink,
2574 "**emergency** last buffer at %" GST_TIME_FORMAT " > GST_SECOND",
2575 GST_TIME_ARGS (priv->last_in_time));
2581 priv->last_in_time = start;
2588 GST_DEBUG_OBJECT (basesink, "object was scheduled in time");
2593 GST_DEBUG_OBJECT (basesink, "frame dropping disabled");
2598 GST_DEBUG_OBJECT (basesink, "object is not a buffer");
2603 GST_DEBUG_OBJECT (basesink, "buffer has no timestamp");
2608 /* called before and after calling the render vmethod. It keeps track of how
2609 * much time was spent in the render method and is used to check if we are
2612 gst_base_sink_do_render_stats (GstBaseSink * basesink, gboolean start)
2614 GstBaseSinkPrivate *priv;
2616 priv = basesink->priv;
2619 priv->start = gst_util_get_timestamp ();
2621 GstClockTime elapsed;
2623 priv->stop = gst_util_get_timestamp ();
2625 elapsed = GST_CLOCK_DIFF (priv->start, priv->stop);
2627 if (priv->avg_render == -1)
2628 priv->avg_render = elapsed;
2630 priv->avg_render = UPDATE_RUNNING_AVG (priv->avg_render, elapsed);
2632 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2633 "avg_render: %" GST_TIME_FORMAT, GST_TIME_ARGS (priv->avg_render));
2637 /* with STREAM_LOCK, PREROLL_LOCK,
2639 * Synchronize the object on the clock and then render it.
2641 * takes ownership of obj.
2643 static GstFlowReturn
2644 gst_base_sink_render_object (GstBaseSink * basesink, GstPad * pad,
2645 gboolean is_list, gpointer obj)
2648 GstBaseSinkClass *bclass;
2649 gboolean late, step_end;
2652 GstBaseSinkPrivate *priv;
2654 priv = basesink->priv;
2658 * If buffer list, use the first group buffer within the list
2661 sync_obj = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0, 0);
2662 g_assert (NULL != sync_obj);
2671 /* synchronize this object, non syncable objects return OK
2673 ret = gst_base_sink_do_sync (basesink, pad, sync_obj, &late, &step_end);
2674 if (G_UNLIKELY (ret != GST_FLOW_OK))
2677 /* and now render, event or buffer/buffer list. */
2678 if (G_LIKELY (is_list || GST_IS_BUFFER (obj))) {
2679 /* drop late buffers unconditionally, let's hope it's unlikely */
2680 if (G_UNLIKELY (late))
2683 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2685 if (G_LIKELY ((is_list && bclass->render_list) ||
2686 (!is_list && bclass->render))) {
2689 /* read once, to get same value before and after */
2690 do_qos = g_atomic_int_get (&priv->qos_enabled);
2692 GST_DEBUG_OBJECT (basesink, "rendering object %p", obj);
2694 /* record rendering time for QoS and stats */
2696 gst_base_sink_do_render_stats (basesink, TRUE);
2701 /* For buffer lists do not set last buffer. Creating buffer
2702 * with meaningful data can be done only with memcpy which will
2703 * significantly affect performance */
2704 buf = GST_BUFFER_CAST (obj);
2705 gst_base_sink_set_last_buffer (basesink, buf);
2707 ret = bclass->render (basesink, buf);
2709 GstBufferList *buflist;
2711 buflist = GST_BUFFER_LIST_CAST (obj);
2713 ret = bclass->render_list (basesink, buflist);
2717 gst_base_sink_do_render_stats (basesink, FALSE);
2719 if (ret == GST_FLOW_STEP)
2722 if (G_UNLIKELY (basesink->flushing))
2728 GstEvent *event = GST_EVENT_CAST (obj);
2729 gboolean event_res = TRUE;
2732 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2734 type = GST_EVENT_TYPE (event);
2736 GST_DEBUG_OBJECT (basesink, "rendering event %p, type %s", obj,
2737 gst_event_type_get_name (type));
2740 event_res = bclass->event (basesink, event);
2742 /* when we get here we could be flushing again when the event handler calls
2743 * _wait_eos(). We have to ignore this object in that case. */
2744 if (G_UNLIKELY (basesink->flushing))
2747 if (G_LIKELY (event_res)) {
2750 seqnum = basesink->priv->seqnum = gst_event_get_seqnum (event);
2751 GST_DEBUG_OBJECT (basesink, "Got seqnum #%" G_GUINT32_FORMAT, seqnum);
2756 GstMessage *message;
2758 /* the EOS event is completely handled so we mark
2759 * ourselves as being in the EOS state. eos is also
2760 * protected by the object lock so we can read it when
2761 * answering the POSITION query. */
2762 GST_OBJECT_LOCK (basesink);
2763 basesink->eos = TRUE;
2764 GST_OBJECT_UNLOCK (basesink);
2766 /* ok, now we can post the message */
2767 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
2769 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
2770 gst_message_set_seqnum (message, seqnum);
2771 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
2774 case GST_EVENT_NEWSEGMENT:
2775 /* configure the segment */
2776 gst_base_sink_configure_segment (basesink, pad, event,
2777 &basesink->segment);
2787 /* the step ended, check if we need to activate a new step */
2788 GST_DEBUG_OBJECT (basesink, "step ended");
2789 stop_stepping (basesink, &basesink->segment, &priv->current_step,
2790 priv->current_rstart, priv->current_rstop, basesink->eos);
2794 gst_base_sink_perform_qos (basesink, late);
2796 GST_DEBUG_OBJECT (basesink, "object unref after render %p", obj);
2797 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
2803 GST_DEBUG_OBJECT (basesink, "do_sync returned %s", gst_flow_get_name (ret));
2809 GST_DEBUG_OBJECT (basesink, "buffer late, dropping");
2814 GST_DEBUG_OBJECT (basesink, "we are flushing, ignore object");
2815 gst_mini_object_unref (obj);
2816 return GST_FLOW_WRONG_STATE;
2820 /* with STREAM_LOCK, PREROLL_LOCK
2822 * Perform preroll on the given object. For buffers this means
2823 * calling the preroll subclass method.
2824 * If that succeeds, the state will be commited.
2826 * function does not take ownership of obj.
2828 static GstFlowReturn
2829 gst_base_sink_preroll_object (GstBaseSink * basesink, gboolean is_list,
2830 GstMiniObject * obj)
2834 GST_DEBUG_OBJECT (basesink, "prerolling object %p", obj);
2836 /* if it's a buffer, we need to call the preroll method */
2837 if (G_LIKELY (is_list || GST_IS_BUFFER (obj)) && basesink->priv->call_preroll) {
2838 GstBaseSinkClass *bclass;
2840 GstClockTime timestamp;
2843 buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0, 0);
2844 g_assert (NULL != buf);
2846 buf = GST_BUFFER_CAST (obj);
2849 timestamp = GST_BUFFER_TIMESTAMP (buf);
2851 GST_DEBUG_OBJECT (basesink, "preroll buffer %" GST_TIME_FORMAT,
2852 GST_TIME_ARGS (timestamp));
2855 * For buffer lists do not set last buffer. Creating buffer
2856 * with meaningful data can be done only with memcpy which will
2857 * significantly affect performance
2860 gst_base_sink_set_last_buffer (basesink, buf);
2863 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2864 if (bclass->preroll)
2865 if ((ret = bclass->preroll (basesink, buf)) != GST_FLOW_OK)
2866 goto preroll_failed;
2868 basesink->priv->call_preroll = FALSE;
2872 if (G_LIKELY (basesink->playing_async)) {
2873 if (G_UNLIKELY (!gst_base_sink_commit_state (basesink)))
2882 GST_DEBUG_OBJECT (basesink, "preroll failed, abort state");
2883 gst_element_abort_state (GST_ELEMENT_CAST (basesink));
2888 GST_DEBUG_OBJECT (basesink, "stopping while commiting state");
2889 return GST_FLOW_WRONG_STATE;
2893 /* with STREAM_LOCK, PREROLL_LOCK
2895 * Queue an object for rendering.
2896 * The first prerollable object queued will complete the preroll. If the
2897 * preroll queue if filled, we render all the objects in the queue.
2899 * This function takes ownership of the object.
2901 static GstFlowReturn
2902 gst_base_sink_queue_object_unlocked (GstBaseSink * basesink, GstPad * pad,
2903 gboolean is_list, gpointer obj, gboolean prerollable)
2905 GstFlowReturn ret = GST_FLOW_OK;
2909 if (G_UNLIKELY (basesink->need_preroll)) {
2910 if (G_LIKELY (prerollable))
2911 basesink->preroll_queued++;
2913 length = basesink->preroll_queued;
2915 GST_DEBUG_OBJECT (basesink, "now %d prerolled items", length);
2917 /* first prerollable item needs to finish the preroll */
2919 ret = gst_base_sink_preroll_object (basesink, is_list, obj);
2920 if (G_UNLIKELY (ret != GST_FLOW_OK))
2921 goto preroll_failed;
2923 /* need to recheck if we need preroll, commmit state during preroll
2924 * could have made us not need more preroll. */
2925 if (G_UNLIKELY (basesink->need_preroll)) {
2926 /* see if we can render now, if we can't add the object to the preroll
2928 if (G_UNLIKELY (length <= basesink->preroll_queue_max_len))
2932 /* we can start rendering (or blocking) the queued object
2934 q = basesink->preroll_queue;
2935 while (G_UNLIKELY (!g_queue_is_empty (q))) {
2938 o = g_queue_pop_head (q);
2939 GST_DEBUG_OBJECT (basesink, "rendering queued object %p", o);
2941 /* do something with the return value */
2942 ret = gst_base_sink_render_object (basesink, pad, FALSE, o);
2943 if (ret != GST_FLOW_OK)
2944 goto dequeue_failed;
2947 /* now render the object */
2948 ret = gst_base_sink_render_object (basesink, pad, is_list, obj);
2949 basesink->preroll_queued = 0;
2956 GST_DEBUG_OBJECT (basesink, "preroll failed, reason %s",
2957 gst_flow_get_name (ret));
2958 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
2963 /* add object to the queue and return */
2964 GST_DEBUG_OBJECT (basesink, "need more preroll data %d <= %d",
2965 length, basesink->preroll_queue_max_len);
2966 g_queue_push_tail (basesink->preroll_queue, obj);
2971 GST_DEBUG_OBJECT (basesink, "rendering queued objects failed, reason %s",
2972 gst_flow_get_name (ret));
2973 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
2980 * This function grabs the PREROLL_LOCK and adds the object to
2983 * This function takes ownership of obj.
2985 static GstFlowReturn
2986 gst_base_sink_queue_object (GstBaseSink * basesink, GstPad * pad,
2987 GstMiniObject * obj, gboolean prerollable)
2991 GST_PAD_PREROLL_LOCK (pad);
2992 if (G_UNLIKELY (basesink->flushing))
2995 if (G_UNLIKELY (basesink->priv->received_eos))
2999 gst_base_sink_queue_object_unlocked (basesink, pad, FALSE, obj,
3001 GST_PAD_PREROLL_UNLOCK (pad);
3008 GST_DEBUG_OBJECT (basesink, "sink is flushing");
3009 GST_PAD_PREROLL_UNLOCK (pad);
3010 gst_mini_object_unref (obj);
3011 return GST_FLOW_WRONG_STATE;
3015 GST_DEBUG_OBJECT (basesink,
3016 "we are EOS, dropping object, return UNEXPECTED");
3017 GST_PAD_PREROLL_UNLOCK (pad);
3018 gst_mini_object_unref (obj);
3019 return GST_FLOW_UNEXPECTED;
3024 gst_base_sink_flush_start (GstBaseSink * basesink, GstPad * pad)
3026 /* make sure we are not blocked on the clock also clear any pending
3028 gst_base_sink_set_flushing (basesink, pad, TRUE);
3030 /* we grab the stream lock but that is not needed since setting the
3031 * sink to flushing would make sure no state commit is being done
3033 GST_PAD_STREAM_LOCK (pad);
3034 gst_base_sink_reset_qos (basesink);
3035 if (basesink->priv->async_enabled) {
3036 /* and we need to commit our state again on the next
3037 * prerolled buffer */
3038 basesink->playing_async = TRUE;
3039 gst_element_lost_state (GST_ELEMENT_CAST (basesink));
3041 basesink->priv->have_latency = TRUE;
3042 basesink->need_preroll = FALSE;
3044 gst_base_sink_set_last_buffer (basesink, NULL);
3045 GST_PAD_STREAM_UNLOCK (pad);
3049 gst_base_sink_flush_stop (GstBaseSink * basesink, GstPad * pad)
3051 /* unset flushing so we can accept new data, this also flushes out any EOS
3053 gst_base_sink_set_flushing (basesink, pad, FALSE);
3055 /* for position reporting */
3056 GST_OBJECT_LOCK (basesink);
3057 basesink->priv->current_sstart = -1;
3058 basesink->priv->current_sstop = -1;
3059 basesink->priv->eos_rtime = -1;
3060 basesink->priv->call_preroll = TRUE;
3061 basesink->priv->current_step.valid = FALSE;
3062 basesink->priv->pending_step.valid = FALSE;
3063 if (basesink->pad_mode == GST_ACTIVATE_PUSH) {
3064 /* we need new segment info after the flush. */
3065 basesink->have_newsegment = FALSE;
3066 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
3067 gst_segment_init (basesink->abidata.ABI.clip_segment, GST_FORMAT_UNDEFINED);
3069 GST_OBJECT_UNLOCK (basesink);
3073 gst_base_sink_event (GstPad * pad, GstEvent * event)
3075 GstBaseSink *basesink;
3076 gboolean result = TRUE;
3077 GstBaseSinkClass *bclass;
3079 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3081 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3083 GST_DEBUG_OBJECT (basesink, "event %p (%s)", event,
3084 GST_EVENT_TYPE_NAME (event));
3086 switch (GST_EVENT_TYPE (event)) {
3091 GST_PAD_PREROLL_LOCK (pad);
3092 if (G_UNLIKELY (basesink->flushing))
3095 if (G_UNLIKELY (basesink->priv->received_eos)) {
3096 /* we can't accept anything when we are EOS */
3098 gst_event_unref (event);
3100 /* we set the received EOS flag here so that we can use it when testing if
3101 * we are prerolled and to refuse more buffers. */
3102 basesink->priv->received_eos = TRUE;
3104 /* EOS is a prerollable object, we call the unlocked version because it
3105 * does not check the received_eos flag. */
3106 ret = gst_base_sink_queue_object_unlocked (basesink, pad,
3107 FALSE, GST_MINI_OBJECT_CAST (event), TRUE);
3108 if (G_UNLIKELY (ret != GST_FLOW_OK))
3111 GST_PAD_PREROLL_UNLOCK (pad);
3114 case GST_EVENT_NEWSEGMENT:
3118 GST_DEBUG_OBJECT (basesink, "newsegment %p", event);
3120 GST_PAD_PREROLL_LOCK (pad);
3121 if (G_UNLIKELY (basesink->flushing))
3124 if (G_UNLIKELY (basesink->priv->received_eos)) {
3125 /* we can't accept anything when we are EOS */
3127 gst_event_unref (event);
3129 /* the new segment is a non prerollable item and does not block anything,
3130 * we need to configure the current clipping segment and insert the event
3131 * in the queue to serialize it with the buffers for rendering. */
3132 gst_base_sink_configure_segment (basesink, pad, event,
3133 basesink->abidata.ABI.clip_segment);
3136 gst_base_sink_queue_object_unlocked (basesink, pad,
3137 FALSE, GST_MINI_OBJECT_CAST (event), FALSE);
3138 if (G_UNLIKELY (ret != GST_FLOW_OK))
3141 GST_OBJECT_LOCK (basesink);
3142 basesink->have_newsegment = TRUE;
3143 GST_OBJECT_UNLOCK (basesink);
3146 GST_PAD_PREROLL_UNLOCK (pad);
3149 case GST_EVENT_FLUSH_START:
3151 bclass->event (basesink, event);
3153 GST_DEBUG_OBJECT (basesink, "flush-start %p", event);
3155 gst_base_sink_flush_start (basesink, pad);
3157 gst_event_unref (event);
3159 case GST_EVENT_FLUSH_STOP:
3161 bclass->event (basesink, event);
3163 GST_DEBUG_OBJECT (basesink, "flush-stop %p", event);
3165 gst_base_sink_flush_stop (basesink, pad);
3167 gst_event_unref (event);
3170 /* other events are sent to queue or subclass depending on if they
3171 * are serialized. */
3172 if (GST_EVENT_IS_SERIALIZED (event)) {
3173 gst_base_sink_queue_object (basesink, pad,
3174 GST_MINI_OBJECT_CAST (event), FALSE);
3177 bclass->event (basesink, event);
3178 gst_event_unref (event);
3183 gst_object_unref (basesink);
3190 GST_DEBUG_OBJECT (basesink, "we are flushing");
3191 GST_PAD_PREROLL_UNLOCK (pad);
3193 gst_event_unref (event);
3198 /* default implementation to calculate the start and end
3199 * timestamps on a buffer, subclasses can override
3202 gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
3203 GstClockTime * start, GstClockTime * end)
3205 GstClockTime timestamp, duration;
3207 timestamp = GST_BUFFER_TIMESTAMP (buffer);
3208 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
3210 /* get duration to calculate end time */
3211 duration = GST_BUFFER_DURATION (buffer);
3212 if (GST_CLOCK_TIME_IS_VALID (duration)) {
3213 *end = timestamp + duration;
3219 /* must be called with PREROLL_LOCK */
3221 gst_base_sink_needs_preroll (GstBaseSink * basesink)
3223 gboolean is_prerolled, res;
3225 /* we have 2 cases where the PREROLL_LOCK is released:
3226 * 1) we are blocking in the PREROLL_LOCK and thus are prerolled.
3227 * 2) we are syncing on the clock
3229 is_prerolled = basesink->have_preroll || basesink->priv->received_eos;
3230 res = !is_prerolled;
3232 GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d => needs preroll: %d",
3233 basesink->have_preroll, basesink->priv->received_eos, res);
3238 /* with STREAM_LOCK, PREROLL_LOCK
3240 * Takes a buffer and compare the timestamps with the last segment.
3241 * If the buffer falls outside of the segment boundaries, drop it.
3242 * Else queue the buffer for preroll and rendering.
3244 * This function takes ownership of the buffer.
3246 static GstFlowReturn
3247 gst_base_sink_chain_unlocked (GstBaseSink * basesink, GstPad * pad,
3248 gboolean is_list, gpointer obj)
3250 GstBaseSinkClass *bclass;
3251 GstFlowReturn result;
3252 GstClockTime start = GST_CLOCK_TIME_NONE, end = GST_CLOCK_TIME_NONE;
3253 GstSegment *clip_segment;
3254 GstBuffer *time_buf;
3256 if (G_UNLIKELY (basesink->flushing))
3259 if (G_UNLIKELY (basesink->priv->received_eos))
3263 time_buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0, 0);
3264 g_assert (NULL != time_buf);
3266 time_buf = GST_BUFFER_CAST (obj);
3269 /* for code clarity */
3270 clip_segment = basesink->abidata.ABI.clip_segment;
3272 if (G_UNLIKELY (!basesink->have_newsegment)) {
3275 sync = gst_base_sink_get_sync (basesink);
3277 GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
3278 (_("Internal data flow problem.")),
3279 ("Received buffer without a new-segment. Assuming timestamps start from 0."));
3282 /* this means this sink will assume timestamps start from 0 */
3283 GST_OBJECT_LOCK (basesink);
3284 clip_segment->start = 0;
3285 clip_segment->stop = -1;
3286 basesink->segment.start = 0;
3287 basesink->segment.stop = -1;
3288 basesink->have_newsegment = TRUE;
3289 GST_OBJECT_UNLOCK (basesink);
3292 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3294 /* check if the buffer needs to be dropped, we first ask the subclass for the
3296 if (bclass->get_times)
3297 bclass->get_times (basesink, time_buf, &start, &end);
3300 /* if the subclass does not want sync, we use our own values so that we at
3301 * least clip the buffer to the segment */
3302 gst_base_sink_get_times (basesink, time_buf, &start, &end);
3305 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
3306 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
3308 /* a dropped buffer does not participate in anything */
3309 if (GST_CLOCK_TIME_IS_VALID (start) &&
3310 (clip_segment->format == GST_FORMAT_TIME)) {
3311 if (G_UNLIKELY (!gst_segment_clip (clip_segment,
3312 GST_FORMAT_TIME, (gint64) start, (gint64) end, NULL, NULL)))
3313 goto out_of_segment;
3316 /* now we can process the buffer in the queue, this function takes ownership
3318 result = gst_base_sink_queue_object_unlocked (basesink, pad,
3319 is_list, obj, TRUE);
3325 GST_DEBUG_OBJECT (basesink, "sink is flushing");
3326 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3327 return GST_FLOW_WRONG_STATE;
3331 GST_DEBUG_OBJECT (basesink,
3332 "we are EOS, dropping object, return UNEXPECTED");
3333 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3334 return GST_FLOW_UNEXPECTED;
3338 GST_DEBUG_OBJECT (basesink, "dropping buffer, out of clipping segment");
3339 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3346 static GstFlowReturn
3347 gst_base_sink_chain_main (GstBaseSink * basesink, GstPad * pad,
3348 gboolean is_list, gpointer obj)
3350 GstFlowReturn result;
3352 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH))
3355 GST_PAD_PREROLL_LOCK (pad);
3356 result = gst_base_sink_chain_unlocked (basesink, pad, is_list, obj);
3357 GST_PAD_PREROLL_UNLOCK (pad);
3365 GST_OBJECT_LOCK (pad);
3366 GST_WARNING_OBJECT (basesink,
3367 "Push on pad %s:%s, but it was not activated in push mode",
3368 GST_DEBUG_PAD_NAME (pad));
3369 GST_OBJECT_UNLOCK (pad);
3370 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3371 /* we don't post an error message this will signal to the peer
3372 * pushing that EOS is reached. */
3373 result = GST_FLOW_UNEXPECTED;
3378 static GstFlowReturn
3379 gst_base_sink_chain (GstPad * pad, GstBuffer * buf)
3381 GstBaseSink *basesink;
3383 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
3385 return gst_base_sink_chain_main (basesink, pad, FALSE, buf);
3388 static GstFlowReturn
3389 gst_base_sink_chain_list (GstPad * pad, GstBufferList * list)
3391 GstBaseSink *basesink;
3392 GstBaseSinkClass *bclass;
3393 GstFlowReturn result;
3395 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
3396 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3398 if (G_LIKELY (bclass->render_list)) {
3399 result = gst_base_sink_chain_main (basesink, pad, TRUE, list);
3401 GstBufferListIterator *it;
3404 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3406 it = gst_buffer_list_iterate (list);
3408 if (gst_buffer_list_iterator_next_group (it)) {
3410 group = gst_buffer_list_iterator_merge_group (it);
3411 if (group == NULL) {
3412 group = gst_buffer_new ();
3413 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining empty group");
3415 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining group");
3417 result = gst_base_sink_chain_main (basesink, pad, FALSE, group);
3418 } while (result == GST_FLOW_OK
3419 && gst_buffer_list_iterator_next_group (it));
3421 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining empty group");
3423 gst_base_sink_chain_main (basesink, pad, FALSE, gst_buffer_new ());
3425 gst_buffer_list_iterator_free (it);
3426 gst_buffer_list_unref (list);
3433 gst_base_sink_default_do_seek (GstBaseSink * sink, GstSegment * segment)
3435 gboolean res = TRUE;
3437 /* update our offset if the start/stop position was updated */
3438 if (segment->format == GST_FORMAT_BYTES) {
3439 segment->time = segment->start;
3440 } else if (segment->start == 0) {
3441 /* seek to start, we can implement a default for this. */
3445 GST_INFO_OBJECT (sink, "Can't do a default seek");
3451 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
3454 gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
3455 GstEvent * event, GstSegment * segment)
3457 /* By default, we try one of 2 things:
3458 * - For absolute seek positions, convert the requested position to our
3459 * configured processing format and place it in the output segment \
3460 * - For relative seek positions, convert our current (input) values to the
3461 * seek format, adjust by the relative seek offset and then convert back to
3462 * the processing format
3464 GstSeekType cur_type, stop_type;
3467 GstFormat seek_format, dest_format;
3470 gboolean res = TRUE;
3472 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3473 &cur_type, &cur, &stop_type, &stop);
3474 dest_format = segment->format;
3476 if (seek_format == dest_format) {
3477 gst_segment_set_seek (segment, rate, seek_format, flags,
3478 cur_type, cur, stop_type, stop, &update);
3482 if (cur_type != GST_SEEK_TYPE_NONE) {
3483 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3485 gst_pad_query_convert (sink->sinkpad, seek_format, cur, &dest_format,
3487 cur_type = GST_SEEK_TYPE_SET;
3490 if (res && stop_type != GST_SEEK_TYPE_NONE) {
3491 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3493 gst_pad_query_convert (sink->sinkpad, seek_format, stop, &dest_format,
3495 stop_type = GST_SEEK_TYPE_SET;
3498 /* And finally, configure our output segment in the desired format */
3499 gst_segment_set_seek (segment, rate, dest_format, flags, cur_type, cur,
3500 stop_type, stop, &update);
3509 GST_DEBUG_OBJECT (sink, "undefined format given, seek aborted.");
3514 /* perform a seek, only executed in pull mode */
3516 gst_base_sink_perform_seek (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3520 GstFormat seek_format, dest_format;
3522 GstSeekType cur_type, stop_type;
3523 gboolean seekseg_configured = FALSE;
3525 gboolean update, res = TRUE;
3526 GstSegment seeksegment;
3528 dest_format = sink->segment.format;
3531 GST_DEBUG_OBJECT (sink, "performing seek with event %p", event);
3532 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3533 &cur_type, &cur, &stop_type, &stop);
3535 flush = flags & GST_SEEK_FLAG_FLUSH;
3537 GST_DEBUG_OBJECT (sink, "performing seek without event");
3542 GST_DEBUG_OBJECT (sink, "flushing upstream");
3543 gst_pad_push_event (pad, gst_event_new_flush_start ());
3544 gst_base_sink_flush_start (sink, pad);
3546 GST_DEBUG_OBJECT (sink, "pausing pulling thread");
3549 GST_PAD_STREAM_LOCK (pad);
3551 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
3552 * copy the current segment info into the temp segment that we can actually
3553 * attempt the seek with. We only update the real segment if the seek suceeds. */
3554 if (!seekseg_configured) {
3555 memcpy (&seeksegment, &sink->segment, sizeof (GstSegment));
3557 /* now configure the final seek segment */
3559 if (sink->segment.format != seek_format) {
3560 /* OK, here's where we give the subclass a chance to convert the relative
3561 * seek into an absolute one in the processing format. We set up any
3562 * absolute seek above, before taking the stream lock. */
3563 if (!gst_base_sink_default_prepare_seek_segment (sink, event,
3565 GST_DEBUG_OBJECT (sink,
3566 "Preparing the seek failed after flushing. " "Aborting seek");
3570 /* The seek format matches our processing format, no need to ask the
3571 * the subclass to configure the segment. */
3572 gst_segment_set_seek (&seeksegment, rate, seek_format, flags,
3573 cur_type, cur, stop_type, stop, &update);
3576 /* Else, no seek event passed, so we're just (re)starting the
3581 GST_DEBUG_OBJECT (sink, "segment configured from %" G_GINT64_FORMAT
3582 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
3583 seeksegment.start, seeksegment.stop, seeksegment.last_stop);
3585 /* do the seek, segment.last_stop contains the new position. */
3586 res = gst_base_sink_default_do_seek (sink, &seeksegment);
3591 GST_DEBUG_OBJECT (sink, "stop flushing upstream");
3592 gst_pad_push_event (pad, gst_event_new_flush_stop ());
3593 gst_base_sink_flush_stop (sink, pad);
3594 } else if (res && sink->abidata.ABI.running) {
3595 /* we are running the current segment and doing a non-flushing seek,
3596 * close the segment first based on the last_stop. */
3597 GST_DEBUG_OBJECT (sink, "closing running segment %" G_GINT64_FORMAT
3598 " to %" G_GINT64_FORMAT, sink->segment.start, sink->segment.last_stop);
3601 /* The subclass must have converted the segment to the processing format
3603 if (res && seeksegment.format != dest_format) {
3604 GST_DEBUG_OBJECT (sink, "Subclass failed to prepare a seek segment "
3605 "in the correct format. Aborting seek.");
3609 /* if successfull seek, we update our real segment and push
3610 * out the new segment. */
3612 memcpy (&sink->segment, &seeksegment, sizeof (GstSegment));
3614 if (sink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3615 gst_element_post_message (GST_ELEMENT (sink),
3616 gst_message_new_segment_start (GST_OBJECT (sink),
3617 sink->segment.format, sink->segment.last_stop));
3621 sink->priv->discont = TRUE;
3622 sink->abidata.ABI.running = TRUE;
3624 GST_PAD_STREAM_UNLOCK (pad);
3630 set_step_info (GstBaseSink * sink, GstStepInfo * current, GstStepInfo * pending,
3631 guint seqnum, GstFormat format, guint64 amount, gdouble rate,
3632 gboolean flush, gboolean intermediate)
3634 GST_OBJECT_LOCK (sink);
3635 pending->seqnum = seqnum;
3636 pending->format = format;
3637 pending->amount = amount;
3638 pending->position = 0;
3639 pending->rate = rate;
3640 pending->flush = flush;
3641 pending->intermediate = intermediate;
3642 pending->valid = TRUE;
3643 /* flush invalidates the current stepping segment */
3645 current->valid = FALSE;
3646 GST_OBJECT_UNLOCK (sink);
3650 gst_base_sink_perform_step (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3652 GstBaseSinkPrivate *priv;
3653 GstBaseSinkClass *bclass;
3654 gboolean flush, intermediate;
3659 GstStepInfo *pending, *current;
3660 GstMessage *message;
3662 bclass = GST_BASE_SINK_GET_CLASS (sink);
3665 GST_DEBUG_OBJECT (sink, "performing step with event %p", event);
3667 gst_event_parse_step (event, &format, &amount, &rate, &flush, &intermediate);
3668 seqnum = gst_event_get_seqnum (event);
3670 pending = &priv->pending_step;
3671 current = &priv->current_step;
3673 /* post message first */
3674 message = gst_message_new_step_start (GST_OBJECT (sink), FALSE, format,
3675 amount, rate, flush, intermediate);
3676 gst_message_set_seqnum (message, seqnum);
3677 gst_element_post_message (GST_ELEMENT (sink), message);
3680 /* we need to call ::unlock before locking PREROLL_LOCK
3681 * since we lock it before going into ::render */
3683 bclass->unlock (sink);
3685 GST_PAD_PREROLL_LOCK (sink->sinkpad);
3686 /* now that we have the PREROLL lock, clear our unlock request */
3687 if (bclass->unlock_stop)
3688 bclass->unlock_stop (sink);
3690 /* update the stepinfo and make it valid */
3691 set_step_info (sink, current, pending, seqnum, format, amount, rate, flush,
3694 if (sink->priv->async_enabled) {
3695 /* and we need to commit our state again on the next
3696 * prerolled buffer */
3697 sink->playing_async = TRUE;
3698 priv->pending_step.need_preroll = TRUE;
3699 sink->need_preroll = FALSE;
3700 gst_element_lost_state_full (GST_ELEMENT_CAST (sink), FALSE);
3702 sink->priv->have_latency = TRUE;
3703 sink->need_preroll = FALSE;
3705 priv->current_sstart = -1;
3706 priv->current_sstop = -1;
3707 priv->eos_rtime = -1;
3708 priv->call_preroll = TRUE;
3709 gst_base_sink_set_last_buffer (sink, NULL);
3710 gst_base_sink_reset_qos (sink);
3712 if (sink->clock_id) {
3713 gst_clock_id_unschedule (sink->clock_id);
3716 if (sink->have_preroll) {
3717 GST_DEBUG_OBJECT (sink, "signal waiter");
3718 priv->step_unlock = TRUE;
3719 GST_PAD_PREROLL_SIGNAL (sink->sinkpad);
3721 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
3723 /* update the stepinfo and make it valid */
3724 set_step_info (sink, current, pending, seqnum, format, amount, rate, flush,
3734 gst_base_sink_loop (GstPad * pad)
3736 GstBaseSink *basesink;
3737 GstBuffer *buf = NULL;
3738 GstFlowReturn result;
3742 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
3744 g_assert (basesink->pad_mode == GST_ACTIVATE_PULL);
3746 if ((blocksize = basesink->priv->blocksize) == 0)
3749 offset = basesink->segment.last_stop;
3751 GST_DEBUG_OBJECT (basesink, "pulling %" G_GUINT64_FORMAT ", %u",
3754 result = gst_pad_pull_range (pad, offset, blocksize, &buf);
3755 if (G_UNLIKELY (result != GST_FLOW_OK))
3758 if (G_UNLIKELY (buf == NULL))
3761 offset += GST_BUFFER_SIZE (buf);
3763 gst_segment_set_last_stop (&basesink->segment, GST_FORMAT_BYTES, offset);
3765 GST_PAD_PREROLL_LOCK (pad);
3766 result = gst_base_sink_chain_unlocked (basesink, pad, FALSE, buf);
3767 GST_PAD_PREROLL_UNLOCK (pad);
3768 if (G_UNLIKELY (result != GST_FLOW_OK))
3776 GST_LOG_OBJECT (basesink, "pausing task, reason %s",
3777 gst_flow_get_name (result));
3778 gst_pad_pause_task (pad);
3779 /* fatal errors and NOT_LINKED cause EOS */
3780 if (GST_FLOW_IS_FATAL (result) || result == GST_FLOW_NOT_LINKED) {
3781 if (result == GST_FLOW_UNEXPECTED) {
3782 /* perform EOS logic */
3783 if (basesink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3784 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3785 gst_message_new_segment_done (GST_OBJECT_CAST (basesink),
3786 basesink->segment.format, basesink->segment.last_stop));
3788 gst_base_sink_event (pad, gst_event_new_eos ());
3791 /* for fatal errors we post an error message, post the error
3792 * first so the app knows about the error first. */
3793 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
3794 (_("Internal data stream error.")),
3795 ("stream stopped, reason %s", gst_flow_get_name (result)));
3796 gst_base_sink_event (pad, gst_event_new_eos ());
3803 GST_LOG_OBJECT (basesink, "no buffer, pausing");
3804 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
3805 (_("Internal data flow error.")), ("element returned NULL buffer"));
3806 result = GST_FLOW_ERROR;
3812 gst_base_sink_set_flushing (GstBaseSink * basesink, GstPad * pad,
3815 GstBaseSinkClass *bclass;
3817 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3820 /* unlock any subclasses, we need to do this before grabbing the
3821 * PREROLL_LOCK since we hold this lock before going into ::render. */
3823 bclass->unlock (basesink);
3826 GST_PAD_PREROLL_LOCK (pad);
3827 basesink->flushing = flushing;
3829 /* step 1, now that we have the PREROLL lock, clear our unlock request */
3830 if (bclass->unlock_stop)
3831 bclass->unlock_stop (basesink);
3833 /* set need_preroll before we unblock the clock. If the clock is unblocked
3834 * before timing out, we can reuse the buffer for preroll. */
3835 basesink->need_preroll = TRUE;
3837 /* step 2, unblock clock sync (if any) or any other blocking thing */
3838 if (basesink->clock_id) {
3839 gst_clock_id_unschedule (basesink->clock_id);
3842 /* flush out the data thread if it's locked in finish_preroll, this will
3843 * also flush out the EOS state */
3844 GST_DEBUG_OBJECT (basesink,
3845 "flushing out data thread, need preroll to TRUE");
3846 gst_base_sink_preroll_queue_flush (basesink, pad);
3848 GST_PAD_PREROLL_UNLOCK (pad);
3854 gst_base_sink_default_activate_pull (GstBaseSink * basesink, gboolean active)
3860 result = gst_pad_start_task (basesink->sinkpad,
3861 (GstTaskFunction) gst_base_sink_loop, basesink->sinkpad);
3863 /* step 2, make sure streaming finishes */
3864 result = gst_pad_stop_task (basesink->sinkpad);
3871 gst_base_sink_pad_activate (GstPad * pad)
3873 gboolean result = FALSE;
3874 GstBaseSink *basesink;
3876 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3878 GST_DEBUG_OBJECT (basesink, "Trying pull mode first");
3880 gst_base_sink_set_flushing (basesink, pad, FALSE);
3882 /* we need to have the pull mode enabled */
3883 if (!basesink->can_activate_pull) {
3884 GST_DEBUG_OBJECT (basesink, "pull mode disabled");
3888 /* check if downstreams supports pull mode at all */
3889 if (!gst_pad_check_pull_range (pad)) {
3890 GST_DEBUG_OBJECT (basesink, "pull mode not supported");
3894 /* set the pad mode before starting the task so that it's in the
3895 * correct state for the new thread. also the sink set_caps and get_caps
3896 * function checks this */
3897 basesink->pad_mode = GST_ACTIVATE_PULL;
3899 /* we first try to negotiate a format so that when we try to activate
3900 * downstream, it knows about our format */
3901 if (!gst_base_sink_negotiate_pull (basesink)) {
3902 GST_DEBUG_OBJECT (basesink, "failed to negotiate in pull mode");
3906 /* ok activate now */
3907 if (!gst_pad_activate_pull (pad, TRUE)) {
3908 /* clear any pending caps */
3909 GST_OBJECT_LOCK (basesink);
3910 gst_caps_replace (&basesink->priv->pull_caps, NULL);
3911 GST_OBJECT_UNLOCK (basesink);
3912 GST_DEBUG_OBJECT (basesink, "failed to activate in pull mode");
3916 GST_DEBUG_OBJECT (basesink, "Success activating pull mode");
3920 /* push mode fallback */
3922 GST_DEBUG_OBJECT (basesink, "Falling back to push mode");
3923 if ((result = gst_pad_activate_push (pad, TRUE))) {
3924 GST_DEBUG_OBJECT (basesink, "Success activating push mode");
3929 GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
3930 gst_base_sink_set_flushing (basesink, pad, TRUE);
3933 gst_object_unref (basesink);
3939 gst_base_sink_pad_activate_push (GstPad * pad, gboolean active)
3942 GstBaseSink *basesink;
3944 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3947 if (!basesink->can_activate_push) {
3949 basesink->pad_mode = GST_ACTIVATE_NONE;
3952 basesink->pad_mode = GST_ACTIVATE_PUSH;
3955 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH)) {
3956 g_warning ("Internal GStreamer activation error!!!");
3959 gst_base_sink_set_flushing (basesink, pad, TRUE);
3961 basesink->pad_mode = GST_ACTIVATE_NONE;
3965 gst_object_unref (basesink);
3971 gst_base_sink_negotiate_pull (GstBaseSink * basesink)
3978 /* this returns the intersection between our caps and the peer caps. If there
3979 * is no peer, it returns NULL and we can't operate in pull mode so we can
3980 * fail the negotiation. */
3981 caps = gst_pad_get_allowed_caps (GST_BASE_SINK_PAD (basesink));
3982 if (caps == NULL || gst_caps_is_empty (caps))
3983 goto no_caps_possible;
3985 GST_DEBUG_OBJECT (basesink, "allowed caps: %" GST_PTR_FORMAT, caps);
3987 caps = gst_caps_make_writable (caps);
3988 /* get the first (prefered) format */
3989 gst_caps_truncate (caps);
3991 gst_pad_fixate_caps (GST_BASE_SINK_PAD (basesink), caps);
3993 GST_DEBUG_OBJECT (basesink, "fixated to: %" GST_PTR_FORMAT, caps);
3995 if (gst_caps_is_any (caps)) {
3996 GST_DEBUG_OBJECT (basesink, "caps were ANY after fixating, "
3998 /* neither side has template caps in this case, so they are prepared for
3999 pull() without setcaps() */
4001 } else if (gst_caps_is_fixed (caps)) {
4002 if (!gst_pad_set_caps (GST_BASE_SINK_PAD (basesink), caps))
4003 goto could_not_set_caps;
4005 GST_OBJECT_LOCK (basesink);
4006 gst_caps_replace (&basesink->priv->pull_caps, caps);
4007 GST_OBJECT_UNLOCK (basesink);
4012 gst_caps_unref (caps);
4018 GST_INFO_OBJECT (basesink, "Pipeline could not agree on caps");
4019 GST_DEBUG_OBJECT (basesink, "get_allowed_caps() returned EMPTY");
4021 gst_caps_unref (caps);
4026 GST_INFO_OBJECT (basesink, "Could not set caps: %" GST_PTR_FORMAT, caps);
4027 gst_caps_unref (caps);
4032 /* this won't get called until we implement an activate function */
4034 gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active)
4036 gboolean result = FALSE;
4037 GstBaseSink *basesink;
4038 GstBaseSinkClass *bclass;
4040 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
4041 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4047 /* we mark we have a newsegment here because pull based
4048 * mode works just fine without having a newsegment before the
4050 format = GST_FORMAT_BYTES;
4052 gst_segment_init (&basesink->segment, format);
4053 gst_segment_init (basesink->abidata.ABI.clip_segment, format);
4054 GST_OBJECT_LOCK (basesink);
4055 basesink->have_newsegment = TRUE;
4056 GST_OBJECT_UNLOCK (basesink);
4058 /* get the peer duration in bytes */
4059 result = gst_pad_query_peer_duration (pad, &format, &duration);
4061 GST_DEBUG_OBJECT (basesink,
4062 "setting duration in bytes to %" G_GINT64_FORMAT, duration);
4063 gst_segment_set_duration (basesink->abidata.ABI.clip_segment, format,
4065 gst_segment_set_duration (&basesink->segment, format, duration);
4067 GST_DEBUG_OBJECT (basesink, "unknown duration");
4070 if (bclass->activate_pull)
4071 result = bclass->activate_pull (basesink, TRUE);
4076 goto activate_failed;
4079 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PULL)) {
4080 g_warning ("Internal GStreamer activation error!!!");
4083 result = gst_base_sink_set_flushing (basesink, pad, TRUE);
4084 if (bclass->activate_pull)
4085 result &= bclass->activate_pull (basesink, FALSE);
4086 basesink->pad_mode = GST_ACTIVATE_NONE;
4087 /* clear any pending caps */
4088 GST_OBJECT_LOCK (basesink);
4089 gst_caps_replace (&basesink->priv->pull_caps, NULL);
4090 GST_OBJECT_UNLOCK (basesink);
4093 gst_object_unref (basesink);
4100 /* reset, as starting the thread failed */
4101 basesink->pad_mode = GST_ACTIVATE_NONE;
4103 GST_ERROR_OBJECT (basesink, "subclass failed to activate in pull mode");
4108 /* send an event to our sinkpad peer. */
4110 gst_base_sink_send_event (GstElement * element, GstEvent * event)
4113 GstBaseSink *basesink = GST_BASE_SINK (element);
4114 gboolean forward, result = TRUE;
4115 GstActivateMode mode;
4117 GST_OBJECT_LOCK (element);
4118 /* get the pad and the scheduling mode */
4119 pad = gst_object_ref (basesink->sinkpad);
4120 mode = basesink->pad_mode;
4121 GST_OBJECT_UNLOCK (element);
4123 /* only push UPSTREAM events upstream */
4124 forward = GST_EVENT_IS_UPSTREAM (event);
4126 switch (GST_EVENT_TYPE (event)) {
4127 case GST_EVENT_LATENCY:
4129 GstClockTime latency;
4131 gst_event_parse_latency (event, &latency);
4133 /* store the latency. We use this to adjust the running_time before syncing
4134 * it to the clock. */
4135 GST_OBJECT_LOCK (element);
4136 basesink->priv->latency = latency;
4137 if (!basesink->priv->have_latency)
4139 GST_OBJECT_UNLOCK (element);
4140 GST_DEBUG_OBJECT (basesink, "latency set to %" GST_TIME_FORMAT,
4141 GST_TIME_ARGS (latency));
4143 /* We forward this event so that all elements know about the global pipeline
4144 * latency. This is interesting for an element when it wants to figure out
4145 * when a particular piece of data will be rendered. */
4148 case GST_EVENT_SEEK:
4149 /* in pull mode we will execute the seek */
4150 if (mode == GST_ACTIVATE_PULL)
4151 result = gst_base_sink_perform_seek (basesink, pad, event);
4153 case GST_EVENT_STEP:
4154 result = gst_base_sink_perform_step (basesink, pad, event);
4162 result = gst_pad_push_event (pad, event);
4164 /* not forwarded, unref the event */
4165 gst_event_unref (event);
4168 gst_object_unref (pad);
4173 gst_base_sink_peer_query (GstBaseSink * sink, GstQuery * query)
4176 gboolean res = FALSE;
4178 if ((peer = gst_pad_get_peer (sink->sinkpad))) {
4179 res = gst_pad_query (peer, query);
4180 gst_object_unref (peer);
4185 /* get the end position of the last seen object, this is used
4186 * for EOS and for making sure that we don't report a position we
4187 * have not reached yet. With LOCK. */
4189 gst_base_sink_get_position_last (GstBaseSink * basesink, GstFormat format,
4190 gint64 * cur, gboolean * upstream)
4193 GstSegment *segment;
4194 gboolean ret = TRUE;
4196 segment = &basesink->segment;
4197 oformat = segment->format;
4199 if (oformat == GST_FORMAT_TIME) {
4200 /* return last observed stream time, we keep the stream time around in the
4202 *cur = basesink->priv->current_sstop;
4204 /* convert last stop to stream time */
4205 *cur = gst_segment_to_stream_time (segment, oformat, segment->last_stop);
4208 if (*cur != -1 && oformat != format) {
4209 GST_OBJECT_UNLOCK (basesink);
4210 /* convert to the target format if we need to, release lock first */
4212 gst_pad_query_convert (basesink->sinkpad, oformat, *cur, &format, cur);
4217 GST_OBJECT_LOCK (basesink);
4220 GST_DEBUG_OBJECT (basesink, "POSITION: %" GST_TIME_FORMAT,
4221 GST_TIME_ARGS (*cur));
4226 /* get the position when we are PAUSED, this is the stream time of the buffer
4227 * that prerolled. If no buffer is prerolled (we are still flushing), this
4228 * value will be -1. With LOCK. */
4230 gst_base_sink_get_position_paused (GstBaseSink * basesink, GstFormat format,
4231 gint64 * cur, gboolean * upstream)
4235 GstSegment *segment;
4238 /* we don't use the clip segment in pull mode, when seeking we update the
4239 * main segment directly with the new segment values without it having to be
4240 * activated by the rendering after preroll */
4241 if (basesink->pad_mode == GST_ACTIVATE_PUSH)
4242 segment = basesink->abidata.ABI.clip_segment;
4244 segment = &basesink->segment;
4245 oformat = segment->format;
4247 if (oformat == GST_FORMAT_TIME) {
4248 *cur = basesink->priv->current_sstart;
4249 if (segment->rate < 0.0 && basesink->priv->current_sstop != -1) {
4250 /* for reverse playback we prefer the stream time stop position if we have
4252 *cur = basesink->priv->current_sstop;
4255 *cur = gst_segment_to_stream_time (segment, oformat, segment->last_stop);
4258 time = segment->time;
4261 *cur = MAX (*cur, time);
4262 GST_DEBUG_OBJECT (basesink, "POSITION as max: %" GST_TIME_FORMAT
4263 ", time %" GST_TIME_FORMAT, GST_TIME_ARGS (*cur), GST_TIME_ARGS (time));
4265 /* we have no buffer, use the segment times. */
4266 if (segment->rate >= 0.0) {
4267 /* forward, next position is always the time of the segment */
4269 GST_DEBUG_OBJECT (basesink, "POSITION as time: %" GST_TIME_FORMAT,
4270 GST_TIME_ARGS (*cur));
4272 /* reverse, next expected timestamp is segment->stop. We use the function
4273 * to get things right for negative applied_rates. */
4274 *cur = gst_segment_to_stream_time (segment, oformat, segment->stop);
4275 GST_DEBUG_OBJECT (basesink, "reverse POSITION: %" GST_TIME_FORMAT,
4276 GST_TIME_ARGS (*cur));
4281 if (res && oformat != format) {
4282 GST_OBJECT_UNLOCK (basesink);
4284 gst_pad_query_convert (basesink->sinkpad, oformat, *cur, &format, cur);
4289 GST_OBJECT_LOCK (basesink);
4296 gst_base_sink_get_position (GstBaseSink * basesink, GstFormat format,
4297 gint64 * cur, gboolean * upstream)
4300 gboolean res = FALSE;
4301 GstFormat oformat, tformat;
4302 GstClockTime now, base, latency;
4303 gint64 time, accum, duration;
4307 GST_OBJECT_LOCK (basesink);
4308 /* our intermediate time format */
4309 tformat = GST_FORMAT_TIME;
4310 /* get the format in the segment */
4311 oformat = basesink->segment.format;
4313 /* can only give answer based on the clock if not EOS */
4314 if (G_UNLIKELY (basesink->eos))
4317 /* we can only get the segment when we are not NULL or READY */
4318 if (!basesink->have_newsegment)
4321 /* when not in PLAYING or when we're busy with a state change, we
4322 * cannot read from the clock so we report time based on the
4323 * last seen timestamp. */
4324 if (GST_STATE (basesink) != GST_STATE_PLAYING ||
4325 GST_STATE_PENDING (basesink) != GST_STATE_VOID_PENDING)
4328 /* we need to sync on the clock. */
4329 if (basesink->sync == FALSE)
4332 /* and we need a clock */
4333 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (basesink)) == NULL))
4336 /* collect all data we need holding the lock */
4337 if (GST_CLOCK_TIME_IS_VALID (basesink->segment.time))
4338 time = basesink->segment.time;
4342 if (GST_CLOCK_TIME_IS_VALID (basesink->segment.stop))
4343 duration = basesink->segment.stop - basesink->segment.start;
4347 base = GST_ELEMENT_CAST (basesink)->base_time;
4348 accum = basesink->segment.accum;
4349 rate = basesink->segment.rate * basesink->segment.applied_rate;
4350 latency = basesink->priv->latency;
4352 gst_object_ref (clock);
4354 /* this function might release the LOCK */
4355 gst_base_sink_get_position_last (basesink, format, &last, upstream);
4357 /* need to release the object lock before we can get the time,
4358 * a clock might take the LOCK of the provider, which could be
4359 * a basesink subclass. */
4360 GST_OBJECT_UNLOCK (basesink);
4362 now = gst_clock_get_time (clock);
4364 if (oformat != tformat) {
4365 /* convert accum, time and duration to time */
4366 if (!gst_pad_query_convert (basesink->sinkpad, oformat, accum, &tformat,
4368 goto convert_failed;
4369 if (!gst_pad_query_convert (basesink->sinkpad, oformat, duration, &tformat,
4371 goto convert_failed;
4372 if (!gst_pad_query_convert (basesink->sinkpad, oformat, time, &tformat,
4374 goto convert_failed;
4377 /* subtract base time and accumulated time from the clock time.
4378 * Make sure we don't go negative. This is the current time in
4379 * the segment which we need to scale with the combined
4380 * rate and applied rate. */
4383 base = MIN (now, base);
4385 /* for negative rates we need to count back from from the segment
4390 *cur = time + gst_guint64_to_gdouble (now - base) * rate;
4392 /* never report more than last seen position */
4394 *cur = MIN (last, *cur);
4396 gst_object_unref (clock);
4398 GST_DEBUG_OBJECT (basesink,
4399 "now %" GST_TIME_FORMAT " - base %" GST_TIME_FORMAT " - accum %"
4400 GST_TIME_FORMAT " + time %" GST_TIME_FORMAT,
4401 GST_TIME_ARGS (now), GST_TIME_ARGS (base),
4402 GST_TIME_ARGS (accum), GST_TIME_ARGS (time));
4404 if (oformat != format) {
4405 /* convert time to final format */
4406 if (!gst_pad_query_convert (basesink->sinkpad, tformat, *cur, &format, cur))
4407 goto convert_failed;
4413 GST_DEBUG_OBJECT (basesink, "res: %d, POSITION: %" GST_TIME_FORMAT,
4414 res, GST_TIME_ARGS (*cur));
4420 GST_DEBUG_OBJECT (basesink, "position in EOS");
4421 res = gst_base_sink_get_position_last (basesink, format, cur, upstream);
4422 GST_OBJECT_UNLOCK (basesink);
4427 GST_DEBUG_OBJECT (basesink, "position in PAUSED");
4428 res = gst_base_sink_get_position_paused (basesink, format, cur, upstream);
4429 GST_OBJECT_UNLOCK (basesink);
4434 /* in NULL or READY we always return FALSE and -1 */
4435 GST_DEBUG_OBJECT (basesink, "position in wrong state, return -1");
4438 GST_OBJECT_UNLOCK (basesink);
4443 /* report last seen timestamp if any, else ask upstream to answer */
4444 if ((*cur = basesink->priv->current_sstart) != -1)
4449 GST_DEBUG_OBJECT (basesink, "no sync, res %d, POSITION %" GST_TIME_FORMAT,
4450 res, GST_TIME_ARGS (*cur));
4451 GST_OBJECT_UNLOCK (basesink);
4456 GST_DEBUG_OBJECT (basesink, "convert failed, try upstream");
4463 gst_base_sink_query (GstElement * element, GstQuery * query)
4465 gboolean res = FALSE;
4467 GstBaseSink *basesink = GST_BASE_SINK (element);
4469 switch (GST_QUERY_TYPE (query)) {
4470 case GST_QUERY_POSITION:
4474 gboolean upstream = FALSE;
4476 gst_query_parse_position (query, &format, NULL);
4478 GST_DEBUG_OBJECT (basesink, "position format %d", format);
4480 /* first try to get the position based on the clock */
4482 gst_base_sink_get_position (basesink, format, &cur, &upstream))) {
4483 gst_query_set_position (query, format, cur);
4484 } else if (upstream) {
4485 /* fallback to peer query */
4486 res = gst_base_sink_peer_query (basesink, query);
4490 case GST_QUERY_DURATION:
4492 GstFormat format, uformat;
4493 gint64 duration, uduration;
4495 gst_query_parse_duration (query, &format, NULL);
4497 GST_DEBUG_OBJECT (basesink, "duration query in format %s",
4498 gst_format_get_name (format));
4500 if (basesink->pad_mode == GST_ACTIVATE_PULL) {
4501 uformat = GST_FORMAT_BYTES;
4503 /* get the duration in bytes, in pull mode that's all we are sure to
4504 * know. We have to explicitly get this value from upstream instead of
4505 * using our cached value because it might change. Duration caching
4506 * should be done at a higher level. */
4507 res = gst_pad_query_peer_duration (basesink->sinkpad, &uformat,
4510 gst_segment_set_duration (&basesink->segment, uformat, uduration);
4511 if (format != uformat) {
4512 /* convert to the requested format */
4513 res = gst_pad_query_convert (basesink->sinkpad, uformat, uduration,
4514 &format, &duration);
4516 duration = uduration;
4519 /* set the result */
4520 gst_query_set_duration (query, format, duration);
4524 /* in push mode we simply forward upstream */
4525 res = gst_base_sink_peer_query (basesink, query);
4529 case GST_QUERY_LATENCY:
4531 gboolean live, us_live;
4532 GstClockTime min, max;
4534 if ((res = gst_base_sink_query_latency (basesink, &live, &us_live, &min,
4536 gst_query_set_latency (query, live, min, max);
4540 case GST_QUERY_JITTER:
4542 case GST_QUERY_RATE:
4543 /* gst_query_set_rate (query, basesink->segment_rate); */
4546 case GST_QUERY_SEGMENT:
4548 /* FIXME, bring start/stop to stream time */
4549 gst_query_set_segment (query, basesink->segment.rate,
4550 GST_FORMAT_TIME, basesink->segment.start, basesink->segment.stop);
4553 case GST_QUERY_SEEKING:
4554 case GST_QUERY_CONVERT:
4555 case GST_QUERY_FORMATS:
4557 res = gst_base_sink_peer_query (basesink, query);
4563 static GstStateChangeReturn
4564 gst_base_sink_change_state (GstElement * element, GstStateChange transition)
4566 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
4567 GstBaseSink *basesink = GST_BASE_SINK (element);
4568 GstBaseSinkClass *bclass;
4569 GstBaseSinkPrivate *priv;
4571 priv = basesink->priv;
4573 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4575 switch (transition) {
4576 case GST_STATE_CHANGE_NULL_TO_READY:
4578 if (!bclass->start (basesink))
4581 case GST_STATE_CHANGE_READY_TO_PAUSED:
4582 /* need to complete preroll before this state change completes, there
4583 * is no data flow in READY so we can safely assume we need to preroll. */
4584 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4585 GST_DEBUG_OBJECT (basesink, "READY to PAUSED");
4586 basesink->have_newsegment = FALSE;
4587 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
4588 gst_segment_init (basesink->abidata.ABI.clip_segment,
4589 GST_FORMAT_UNDEFINED);
4590 basesink->offset = 0;
4591 basesink->have_preroll = FALSE;
4592 priv->step_unlock = FALSE;
4593 basesink->need_preroll = TRUE;
4594 basesink->playing_async = TRUE;
4595 priv->current_sstart = -1;
4596 priv->current_sstop = -1;
4597 priv->eos_rtime = -1;
4599 basesink->eos = FALSE;
4600 priv->received_eos = FALSE;
4601 gst_base_sink_reset_qos (basesink);
4602 priv->commited = FALSE;
4603 priv->call_preroll = TRUE;
4604 priv->current_step.valid = FALSE;
4605 priv->pending_step.valid = FALSE;
4606 if (priv->async_enabled) {
4607 GST_DEBUG_OBJECT (basesink, "doing async state change");
4608 /* when async enabled, post async-start message and return ASYNC from
4609 * the state change function */
4610 ret = GST_STATE_CHANGE_ASYNC;
4611 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4612 gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
4614 priv->have_latency = TRUE;
4616 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4618 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
4619 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4620 if (!gst_base_sink_needs_preroll (basesink)) {
4621 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, don't need preroll");
4622 /* no preroll needed anymore now. */
4623 basesink->playing_async = FALSE;
4624 basesink->need_preroll = FALSE;
4625 if (basesink->eos) {
4626 GstMessage *message;
4628 /* need to post EOS message here */
4629 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
4630 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
4631 gst_message_set_seqnum (message, basesink->priv->seqnum);
4632 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
4634 GST_DEBUG_OBJECT (basesink, "signal preroll");
4635 GST_PAD_PREROLL_SIGNAL (basesink->sinkpad);
4638 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, we are not prerolled");
4639 basesink->need_preroll = TRUE;
4640 basesink->playing_async = TRUE;
4641 priv->call_preroll = TRUE;
4642 priv->commited = FALSE;
4643 if (priv->async_enabled) {
4644 GST_DEBUG_OBJECT (basesink, "doing async state change");
4645 ret = GST_STATE_CHANGE_ASYNC;
4646 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4647 gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
4650 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4657 GstStateChangeReturn bret;
4659 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4660 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
4661 goto activate_failed;
4664 switch (transition) {
4665 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
4666 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED");
4667 /* FIXME, make sure we cannot enter _render first */
4669 /* we need to call ::unlock before locking PREROLL_LOCK
4670 * since we lock it before going into ::render */
4672 bclass->unlock (basesink);
4674 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4675 /* now that we have the PREROLL lock, clear our unlock request */
4676 if (bclass->unlock_stop)
4677 bclass->unlock_stop (basesink);
4679 /* we need preroll again and we set the flag before unlocking the clockid
4680 * because if the clockid is unlocked before a current buffer expired, we
4681 * can use that buffer to preroll with */
4682 basesink->need_preroll = TRUE;
4684 if (basesink->clock_id) {
4685 gst_clock_id_unschedule (basesink->clock_id);
4688 /* if we don't have a preroll buffer we need to wait for a preroll and
4690 if (!gst_base_sink_needs_preroll (basesink)) {
4691 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, we are prerolled");
4692 basesink->playing_async = FALSE;
4694 if (GST_STATE_TARGET (GST_ELEMENT (basesink)) <= GST_STATE_READY) {
4695 ret = GST_STATE_CHANGE_SUCCESS;
4697 GST_DEBUG_OBJECT (basesink,
4698 "PLAYING to PAUSED, we are not prerolled");
4699 basesink->playing_async = TRUE;
4700 priv->commited = FALSE;
4701 priv->call_preroll = TRUE;
4702 if (priv->async_enabled) {
4703 GST_DEBUG_OBJECT (basesink, "doing async state change");
4704 ret = GST_STATE_CHANGE_ASYNC;
4705 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4706 gst_message_new_async_start (GST_OBJECT_CAST (basesink),
4711 GST_DEBUG_OBJECT (basesink, "rendered: %" G_GUINT64_FORMAT
4712 ", dropped: %" G_GUINT64_FORMAT, priv->rendered, priv->dropped);
4714 gst_base_sink_reset_qos (basesink);
4715 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4717 case GST_STATE_CHANGE_PAUSED_TO_READY:
4718 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4719 /* start by reseting our position state with the object lock so that the
4720 * position query gets the right idea. We do this before we post the
4721 * messages so that the message handlers pick this up. */
4722 GST_OBJECT_LOCK (basesink);
4723 basesink->have_newsegment = FALSE;
4724 priv->current_sstart = -1;
4725 priv->current_sstop = -1;
4726 priv->have_latency = FALSE;
4727 GST_OBJECT_UNLOCK (basesink);
4729 gst_base_sink_set_last_buffer (basesink, NULL);
4730 priv->call_preroll = FALSE;
4732 if (!priv->commited) {
4733 if (priv->async_enabled) {
4734 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, posting async-done");
4736 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4737 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
4738 GST_STATE_PLAYING, GST_STATE_PAUSED, GST_STATE_READY));
4740 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4741 gst_message_new_async_done (GST_OBJECT_CAST (basesink)));
4743 priv->commited = TRUE;
4745 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, don't need_preroll");
4747 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4749 case GST_STATE_CHANGE_READY_TO_NULL:
4751 if (!bclass->stop (basesink)) {
4752 GST_WARNING_OBJECT (basesink, "failed to stop");
4755 gst_base_sink_set_last_buffer (basesink, NULL);
4756 priv->call_preroll = FALSE;
4767 GST_DEBUG_OBJECT (basesink, "failed to start");
4768 return GST_STATE_CHANGE_FAILURE;
4772 GST_DEBUG_OBJECT (basesink,
4773 "element failed to change states -- activation problem?");
4774 return GST_STATE_CHANGE_FAILURE;