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, #GstBaseSrc
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 class_init function, like so:
42 * my_element_class_init (GstMyElementClass *klass)
44 * GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
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 * #GstBaseSinkClass.preroll() vmethod with this preroll buffer and will then
59 * commit 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 #GstBaseSinkClass.get_times(). If this
63 * function returns #GST_CLOCK_TIME_NONE for the start time, no synchronisation
64 * will be done. Synchronisation can be disabled entirely by setting the object
65 * #GstBaseSink:sync property to %FALSE.
67 * After synchronisation the virtual method #GstBaseSinkClass.render() will be
68 * called. Subclasses should minimally implement this method.
70 * Since 0.10.3 subclasses that synchronise on the clock in the
71 * #GstBaseSinkClass.render() method are supported as well. These classes
72 * typically receive a buffer in the render method and can then potentially
73 * block on the clock while rendering. A typical example is an audiosink.
74 * Since 0.10.11 these subclasses can use gst_base_sink_wait_preroll() to
75 * perform the blocking wait.
77 * Upon receiving the EOS event in the PLAYING state, #GstBaseSink will wait
78 * for the clock to reach the time indicated by the stop time of the last
79 * #GstBaseSinkClass.get_times() call before posting an EOS message. When the
80 * element receives EOS in PAUSED, preroll completes, the event is queued and an
81 * EOS message is posted when going to PLAYING.
83 * #GstBaseSink will internally use the #GST_EVENT_NEWSEGMENT events to schedule
84 * synchronisation and clipping of buffers. Buffers that fall completely outside
85 * of the current segment are dropped. Buffers that fall partially in the
86 * segment are rendered (and prerolled). Subclasses should do any subbuffer
87 * clipping themselves when needed.
89 * #GstBaseSink will by default report the current playback position in
90 * #GST_FORMAT_TIME based on the current clock time and segment information.
91 * If no clock has been set on the element, the query will be forwarded
94 * The #GstBaseSinkClass.set_caps() function will be called when the subclass
95 * should configure itself to process a specific media type.
97 * The #GstBaseSinkClass.start() and #GstBaseSinkClass.stop() virtual methods
98 * will be called when resources should be allocated. Any
99 * #GstBaseSinkClass.preroll(), #GstBaseSinkClass.render() and
100 * #GstBaseSinkClass.set_caps() function will be called between the
101 * #GstBaseSinkClass.start() and #GstBaseSinkClass.stop() calls.
103 * The #GstBaseSinkClass.event() virtual method will be called when an event is
104 * received by #GstBaseSink. Normally this method should only be overriden by
105 * very specific elements (such as file sinks) which need to handle the
106 * newsegment event specially.
108 * The #GstBaseSinkClass.unlock() method is called when the elements should
109 * unblock any blocking operations they perform in the
110 * #GstBaseSinkClass.render() method. This is mostly useful when the
111 * #GstBaseSinkClass.render() method performs a blocking write on a file
112 * descriptor, for example.
114 * The #GstBaseSink:max-lateness property affects how the sink deals with
115 * buffers that arrive too late in the sink. A buffer arrives too late in the
116 * sink when 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
122 * #GstBaseSinkClass.get_times() method does not return a valid start time or
123 * max-lateness is set to -1 (the default).
124 * Subclasses can use gst_base_sink_set_max_lateness() to configure the
125 * max-lateness value.
127 * The #GstBaseSink:qos property will enable the quality-of-service features of
128 * the basesink which gather statistics about the real-time performance of the
129 * clock synchronisation. For each buffer received in the sink, statistics are
130 * gathered and a QOS event is sent upstream with these numbers. This
131 * information can then be used by upstream elements to reduce their processing
134 * Since 0.10.15 the #GstBaseSink:async property can be used to instruct the
135 * sink to never perform an ASYNC state change. This feature is mostly usable
136 * when dealing with non-synchronized streams or sparse streams.
138 * Last reviewed on 2007-08-29 (0.10.15)
145 #include <gst/gst_private.h>
147 #include "gstbasesink.h"
148 #include <gst/gst-i18n-lib.h>
150 GST_DEBUG_CATEGORY_STATIC (gst_base_sink_debug);
151 #define GST_CAT_DEFAULT gst_base_sink_debug
153 #define GST_BASE_SINK_GET_PRIVATE(obj) \
154 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_SINK, GstBaseSinkPrivate))
156 #define GST_FLOW_STEP GST_FLOW_CUSTOM_ERROR
160 gboolean valid; /* if this info is valid */
161 guint32 seqnum; /* the seqnum of the STEP event */
162 GstFormat format; /* the format of the amount */
163 guint64 amount; /* the total amount of data to skip */
164 guint64 position; /* the position in the stepped data */
165 guint64 duration; /* the duration in time of the skipped data */
166 guint64 start; /* running_time of the start */
167 gdouble rate; /* rate of skipping */
168 gdouble start_rate; /* rate before skipping */
169 guint64 start_start; /* start position skipping */
170 guint64 start_stop; /* stop position skipping */
171 gboolean flush; /* if this was a flushing step */
172 gboolean intermediate; /* if this is an intermediate step */
173 gboolean need_preroll; /* if we need preroll after this step */
176 struct _GstBaseSinkPrivate
178 gint qos_enabled; /* ATOMIC */
179 gboolean async_enabled;
180 GstClockTimeDiff ts_offset;
181 GstClockTime render_delay;
183 /* start, stop of current buffer, stream time, used to report position */
184 GstClockTime current_sstart;
185 GstClockTime current_sstop;
187 /* start, stop and jitter of current buffer, running time */
188 GstClockTime current_rstart;
189 GstClockTime current_rstop;
190 GstClockTimeDiff current_jitter;
191 /* the running time of the previous buffer */
192 GstClockTime prev_rstart;
194 /* EOS sync time in running time */
195 GstClockTime eos_rtime;
197 /* last buffer that arrived in time, running time */
198 GstClockTime last_render_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;
206 GstClockTime avg_in_diff;
208 /* these are done on system time. avg_jitter and avg_render are
209 * compared to eachother to see if the rendering time takes a
210 * huge amount of the processing, If so we are flooded with
212 GstClockTime last_left_systime;
213 GstClockTime avg_jitter;
214 GstClockTime start, stop;
215 GstClockTime avg_render;
217 /* number of rendered and dropped frames */
222 GstClockTime latency;
224 /* if we already commited the state */
226 /* state change to playing ongoing */
229 /* when we received EOS */
230 gboolean received_eos;
232 /* when we are prerolled and able to report latency */
233 gboolean have_latency;
235 /* the last buffer we prerolled or rendered. Useful for making snapshots */
236 gint enable_last_sample; /* atomic */
237 GstBuffer *last_buffer;
240 /* negotiated caps */
243 /* blocksize for pulling */
248 /* seqnum of the stream */
251 gboolean call_preroll;
252 gboolean step_unlock;
254 /* we have a pending and a current step operation */
255 GstStepInfo current_step;
256 GstStepInfo pending_step;
258 /* Cached GstClockID */
259 GstClockID cached_clock_id;
261 /* for throttling and QoS */
262 GstClockTime earliest_in_time;
263 GstClockTime throttle_time;
268 #define DO_RUNNING_AVG(avg,val,size) (((val) + ((size)-1) * (avg)) / (size))
270 /* generic running average, this has a neutral window size */
271 #define UPDATE_RUNNING_AVG(avg,val) DO_RUNNING_AVG(avg,val,8)
273 /* the windows for these running averages are experimentally obtained.
274 * possitive values get averaged more while negative values use a small
275 * window so we can react faster to badness. */
276 #define UPDATE_RUNNING_AVG_P(avg,val) DO_RUNNING_AVG(avg,val,16)
277 #define UPDATE_RUNNING_AVG_N(avg,val) DO_RUNNING_AVG(avg,val,4)
279 /* BaseSink properties */
281 #define DEFAULT_CAN_ACTIVATE_PULL FALSE /* fixme: enable me */
282 #define DEFAULT_CAN_ACTIVATE_PUSH TRUE
284 #define DEFAULT_SYNC TRUE
285 #define DEFAULT_MAX_LATENESS -1
286 #define DEFAULT_QOS FALSE
287 #define DEFAULT_ASYNC TRUE
288 #define DEFAULT_TS_OFFSET 0
289 #define DEFAULT_BLOCKSIZE 4096
290 #define DEFAULT_RENDER_DELAY 0
291 #define DEFAULT_ENABLE_LAST_SAMPLE TRUE
292 #define DEFAULT_THROTTLE_TIME 0
302 PROP_ENABLE_LAST_SAMPLE,
310 static GstElementClass *parent_class = NULL;
312 static void gst_base_sink_class_init (GstBaseSinkClass * klass);
313 static void gst_base_sink_init (GstBaseSink * trans, gpointer g_class);
314 static void gst_base_sink_finalize (GObject * object);
317 gst_base_sink_get_type (void)
319 static volatile gsize base_sink_type = 0;
321 if (g_once_init_enter (&base_sink_type)) {
323 static const GTypeInfo base_sink_info = {
324 sizeof (GstBaseSinkClass),
327 (GClassInitFunc) gst_base_sink_class_init,
330 sizeof (GstBaseSink),
332 (GInstanceInitFunc) gst_base_sink_init,
335 _type = g_type_register_static (GST_TYPE_ELEMENT,
336 "GstBaseSink", &base_sink_info, G_TYPE_FLAG_ABSTRACT);
337 g_once_init_leave (&base_sink_type, _type);
339 return base_sink_type;
342 static void gst_base_sink_set_property (GObject * object, guint prop_id,
343 const GValue * value, GParamSpec * pspec);
344 static void gst_base_sink_get_property (GObject * object, guint prop_id,
345 GValue * value, GParamSpec * pspec);
347 static gboolean gst_base_sink_send_event (GstElement * element,
349 static gboolean default_element_query (GstElement * element, GstQuery * query);
351 static GstCaps *gst_base_sink_default_get_caps (GstBaseSink * sink,
353 static gboolean gst_base_sink_default_set_caps (GstBaseSink * sink,
355 static void gst_base_sink_default_get_times (GstBaseSink * basesink,
356 GstBuffer * buffer, GstClockTime * start, GstClockTime * end);
357 static gboolean gst_base_sink_set_flushing (GstBaseSink * basesink,
358 GstPad * pad, gboolean flushing);
359 static gboolean gst_base_sink_default_activate_pull (GstBaseSink * basesink,
361 static gboolean gst_base_sink_default_do_seek (GstBaseSink * sink,
362 GstSegment * segment);
363 static gboolean gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
364 GstEvent * event, GstSegment * segment);
366 static GstStateChangeReturn gst_base_sink_change_state (GstElement * element,
367 GstStateChange transition);
369 static gboolean gst_base_sink_sink_query (GstPad * pad, GstObject * parent,
371 static GstFlowReturn gst_base_sink_chain (GstPad * pad, GstObject * parent,
373 static GstFlowReturn gst_base_sink_chain_list (GstPad * pad, GstObject * parent,
374 GstBufferList * list);
376 static void gst_base_sink_loop (GstPad * pad);
377 static gboolean gst_base_sink_pad_activate (GstPad * pad, GstObject * parent);
378 static gboolean gst_base_sink_pad_activate_mode (GstPad * pad,
379 GstObject * parent, GstPadMode mode, gboolean active);
380 static gboolean gst_base_sink_default_event (GstBaseSink * basesink,
382 static GstFlowReturn gst_base_sink_default_wait_eos (GstBaseSink * basesink,
384 static gboolean gst_base_sink_event (GstPad * pad, GstObject * parent,
387 static gboolean gst_base_sink_default_query (GstBaseSink * sink,
390 static gboolean gst_base_sink_negotiate_pull (GstBaseSink * basesink);
391 static void gst_base_sink_default_fixate (GstBaseSink * bsink, GstCaps * caps);
392 static void gst_base_sink_fixate (GstBaseSink * bsink, GstCaps * caps);
394 /* check if an object was too late */
395 static gboolean gst_base_sink_is_too_late (GstBaseSink * basesink,
396 GstMiniObject * obj, GstClockTime rstart, GstClockTime rstop,
397 GstClockReturn status, GstClockTimeDiff jitter);
400 gst_base_sink_class_init (GstBaseSinkClass * klass)
402 GObjectClass *gobject_class;
403 GstElementClass *gstelement_class;
405 gobject_class = G_OBJECT_CLASS (klass);
406 gstelement_class = GST_ELEMENT_CLASS (klass);
408 GST_DEBUG_CATEGORY_INIT (gst_base_sink_debug, "basesink", 0,
411 g_type_class_add_private (klass, sizeof (GstBaseSinkPrivate));
413 parent_class = g_type_class_peek_parent (klass);
415 gobject_class->finalize = gst_base_sink_finalize;
416 gobject_class->set_property = gst_base_sink_set_property;
417 gobject_class->get_property = gst_base_sink_get_property;
419 g_object_class_install_property (gobject_class, PROP_SYNC,
420 g_param_spec_boolean ("sync", "Sync", "Sync on the clock", DEFAULT_SYNC,
421 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
423 g_object_class_install_property (gobject_class, PROP_MAX_LATENESS,
424 g_param_spec_int64 ("max-lateness", "Max Lateness",
425 "Maximum number of nanoseconds that a buffer can be late before it "
426 "is dropped (-1 unlimited)", -1, G_MAXINT64, DEFAULT_MAX_LATENESS,
427 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
429 g_object_class_install_property (gobject_class, PROP_QOS,
430 g_param_spec_boolean ("qos", "Qos",
431 "Generate Quality-of-Service events upstream", DEFAULT_QOS,
432 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
436 * If set to #TRUE, the basesink will perform asynchronous state changes.
437 * When set to #FALSE, the sink will not signal the parent when it prerolls.
438 * Use this option when dealing with sparse streams or when synchronisation is
443 g_object_class_install_property (gobject_class, PROP_ASYNC,
444 g_param_spec_boolean ("async", "Async",
445 "Go asynchronously to PAUSED", DEFAULT_ASYNC,
446 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
448 * GstBaseSink:ts-offset
450 * Controls the final synchronisation, a negative value will render the buffer
451 * earlier while a positive value delays playback. This property can be
452 * used to fix synchronisation in bad files.
456 g_object_class_install_property (gobject_class, PROP_TS_OFFSET,
457 g_param_spec_int64 ("ts-offset", "TS Offset",
458 "Timestamp offset in nanoseconds", G_MININT64, G_MAXINT64,
459 DEFAULT_TS_OFFSET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
462 * GstBaseSink:enable-last-sample
464 * Enable the last-sample property. If FALSE, basesink doesn't keep a
465 * reference to the last buffer arrived and the last-sample property is always
466 * set to NULL. This can be useful if you need buffers to be released as soon
467 * as possible, eg. if you're using a buffer pool.
471 g_object_class_install_property (gobject_class, PROP_ENABLE_LAST_SAMPLE,
472 g_param_spec_boolean ("enable-last-sample", "Enable Last Buffer",
473 "Enable the last-sample property", DEFAULT_ENABLE_LAST_SAMPLE,
474 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
477 * GstBaseSink:last-sample
479 * The last buffer that arrived in the sink and was used for preroll or for
480 * rendering. This property can be used to generate thumbnails. This property
481 * can be NULL when the sink has not yet received a bufer.
485 g_object_class_install_property (gobject_class, PROP_LAST_SAMPLE,
486 g_param_spec_boxed ("last-sample", "Last Sample",
487 "The last sample received in the sink", GST_TYPE_SAMPLE,
488 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
490 * GstBaseSink:blocksize
492 * The amount of bytes to pull when operating in pull mode.
496 /* FIXME 0.11: blocksize property should be int, otherwise min>max.. */
497 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
498 g_param_spec_uint ("blocksize", "Block size",
499 "Size in bytes to pull per buffer (0 = default)", 0, G_MAXUINT,
500 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
502 * GstBaseSink:render-delay
504 * The additional delay between synchronisation and actual rendering of the
505 * media. This property will add additional latency to the device in order to
506 * make other sinks compensate for the delay.
510 g_object_class_install_property (gobject_class, PROP_RENDER_DELAY,
511 g_param_spec_uint64 ("render-delay", "Render Delay",
512 "Additional render delay of the sink in nanoseconds", 0, G_MAXUINT64,
513 DEFAULT_RENDER_DELAY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
515 * GstBaseSink:throttle-time
517 * The time to insert between buffers. This property can be used to control
518 * the maximum amount of buffers per second to render. Setting this property
519 * to a value bigger than 0 will make the sink create THROTTLE QoS events.
523 g_object_class_install_property (gobject_class, PROP_THROTTLE_TIME,
524 g_param_spec_uint64 ("throttle-time", "Throttle time",
525 "The time to keep between rendered buffers (unused)", 0, G_MAXUINT64,
526 DEFAULT_THROTTLE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
528 gstelement_class->change_state =
529 GST_DEBUG_FUNCPTR (gst_base_sink_change_state);
530 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_sink_send_event);
531 gstelement_class->query = GST_DEBUG_FUNCPTR (default_element_query);
533 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_sink_default_get_caps);
534 klass->set_caps = GST_DEBUG_FUNCPTR (gst_base_sink_default_set_caps);
535 klass->fixate = GST_DEBUG_FUNCPTR (gst_base_sink_default_fixate);
536 klass->activate_pull =
537 GST_DEBUG_FUNCPTR (gst_base_sink_default_activate_pull);
538 klass->get_times = GST_DEBUG_FUNCPTR (gst_base_sink_default_get_times);
539 klass->query = GST_DEBUG_FUNCPTR (gst_base_sink_default_query);
540 klass->event = GST_DEBUG_FUNCPTR (gst_base_sink_default_event);
541 klass->wait_eos = GST_DEBUG_FUNCPTR (gst_base_sink_default_wait_eos);
543 /* Registering debug symbols for function pointers */
544 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_fixate);
545 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_activate);
546 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_activate_mode);
547 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_event);
548 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_chain);
549 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_chain_list);
550 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_sink_query);
554 gst_base_sink_query_caps (GstBaseSink * bsink, GstPad * pad, GstCaps * filter)
556 GstBaseSinkClass *bclass;
557 GstCaps *caps = NULL;
560 bclass = GST_BASE_SINK_GET_CLASS (bsink);
561 fixed = GST_PAD_IS_FIXED_CAPS (pad);
563 if (fixed || bsink->pad_mode == GST_PAD_MODE_PULL) {
564 /* if we are operating in pull mode or fixed caps, we only accept the
565 * currently negotiated caps */
566 caps = gst_pad_get_current_caps (pad);
569 if (bclass->get_caps)
570 caps = bclass->get_caps (bsink, filter);
573 GstPadTemplate *pad_template;
576 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass),
578 if (pad_template != NULL) {
579 caps = gst_pad_template_get_caps (pad_template);
582 GstCaps *intersection;
585 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
586 gst_caps_unref (caps);
597 gst_base_sink_default_fixate (GstBaseSink * bsink, GstCaps * caps)
599 GST_DEBUG_OBJECT (bsink, "using default caps fixate function");
600 gst_caps_fixate (caps);
604 gst_base_sink_fixate (GstBaseSink * bsink, GstCaps * caps)
606 GstBaseSinkClass *bclass;
608 bclass = GST_BASE_SINK_GET_CLASS (bsink);
611 bclass->fixate (bsink, caps);
615 gst_base_sink_init (GstBaseSink * basesink, gpointer g_class)
617 GstPadTemplate *pad_template;
618 GstBaseSinkPrivate *priv;
620 basesink->priv = priv = GST_BASE_SINK_GET_PRIVATE (basesink);
623 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
624 g_return_if_fail (pad_template != NULL);
626 basesink->sinkpad = gst_pad_new_from_template (pad_template, "sink");
628 gst_pad_set_activate_function (basesink->sinkpad, gst_base_sink_pad_activate);
629 gst_pad_set_activatemode_function (basesink->sinkpad,
630 gst_base_sink_pad_activate_mode);
631 gst_pad_set_query_function (basesink->sinkpad, gst_base_sink_sink_query);
632 gst_pad_set_event_function (basesink->sinkpad, gst_base_sink_event);
633 gst_pad_set_chain_function (basesink->sinkpad, gst_base_sink_chain);
634 gst_pad_set_chain_list_function (basesink->sinkpad, gst_base_sink_chain_list);
635 gst_element_add_pad (GST_ELEMENT_CAST (basesink), basesink->sinkpad);
637 basesink->pad_mode = GST_PAD_MODE_NONE;
638 g_mutex_init (&basesink->preroll_lock);
639 g_cond_init (&basesink->preroll_cond);
640 priv->have_latency = FALSE;
642 basesink->can_activate_push = DEFAULT_CAN_ACTIVATE_PUSH;
643 basesink->can_activate_pull = DEFAULT_CAN_ACTIVATE_PULL;
645 basesink->sync = DEFAULT_SYNC;
646 basesink->max_lateness = DEFAULT_MAX_LATENESS;
647 g_atomic_int_set (&priv->qos_enabled, DEFAULT_QOS);
648 priv->async_enabled = DEFAULT_ASYNC;
649 priv->ts_offset = DEFAULT_TS_OFFSET;
650 priv->render_delay = DEFAULT_RENDER_DELAY;
651 priv->blocksize = DEFAULT_BLOCKSIZE;
652 priv->cached_clock_id = NULL;
653 g_atomic_int_set (&priv->enable_last_sample, DEFAULT_ENABLE_LAST_SAMPLE);
654 priv->throttle_time = DEFAULT_THROTTLE_TIME;
656 GST_OBJECT_FLAG_SET (basesink, GST_ELEMENT_FLAG_SINK);
660 gst_base_sink_finalize (GObject * object)
662 GstBaseSink *basesink;
664 basesink = GST_BASE_SINK (object);
666 g_mutex_clear (&basesink->preroll_lock);
667 g_cond_clear (&basesink->preroll_cond);
669 G_OBJECT_CLASS (parent_class)->finalize (object);
673 * gst_base_sink_set_sync:
675 * @sync: the new sync value.
677 * Configures @sink to synchronize on the clock or not. When
678 * @sync is FALSE, incoming samples will be played as fast as
679 * possible. If @sync is TRUE, the timestamps of the incomming
680 * buffers will be used to schedule the exact render time of its
686 gst_base_sink_set_sync (GstBaseSink * sink, gboolean sync)
688 g_return_if_fail (GST_IS_BASE_SINK (sink));
690 GST_OBJECT_LOCK (sink);
692 GST_OBJECT_UNLOCK (sink);
696 * gst_base_sink_get_sync:
699 * Checks if @sink is currently configured to synchronize against the
702 * Returns: TRUE if the sink is configured to synchronize against the clock.
707 gst_base_sink_get_sync (GstBaseSink * sink)
711 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
713 GST_OBJECT_LOCK (sink);
715 GST_OBJECT_UNLOCK (sink);
721 * gst_base_sink_set_max_lateness:
723 * @max_lateness: the new max lateness value.
725 * Sets the new max lateness value to @max_lateness. This value is
726 * used to decide if a buffer should be dropped or not based on the
727 * buffer timestamp and the current clock time. A value of -1 means
733 gst_base_sink_set_max_lateness (GstBaseSink * sink, gint64 max_lateness)
735 g_return_if_fail (GST_IS_BASE_SINK (sink));
737 GST_OBJECT_LOCK (sink);
738 sink->max_lateness = max_lateness;
739 GST_OBJECT_UNLOCK (sink);
743 * gst_base_sink_get_max_lateness:
746 * Gets the max lateness value. See gst_base_sink_set_max_lateness for
749 * Returns: The maximum time in nanoseconds that a buffer can be late
750 * before it is dropped and not rendered. A value of -1 means an
756 gst_base_sink_get_max_lateness (GstBaseSink * sink)
760 g_return_val_if_fail (GST_IS_BASE_SINK (sink), -1);
762 GST_OBJECT_LOCK (sink);
763 res = sink->max_lateness;
764 GST_OBJECT_UNLOCK (sink);
770 * gst_base_sink_set_qos_enabled:
772 * @enabled: the new qos value.
774 * Configures @sink to send Quality-of-Service events upstream.
779 gst_base_sink_set_qos_enabled (GstBaseSink * sink, gboolean enabled)
781 g_return_if_fail (GST_IS_BASE_SINK (sink));
783 g_atomic_int_set (&sink->priv->qos_enabled, enabled);
787 * gst_base_sink_is_qos_enabled:
790 * Checks if @sink is currently configured to send Quality-of-Service events
793 * Returns: TRUE if the sink is configured to perform Quality-of-Service.
798 gst_base_sink_is_qos_enabled (GstBaseSink * sink)
802 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
804 res = g_atomic_int_get (&sink->priv->qos_enabled);
810 * gst_base_sink_set_async_enabled:
812 * @enabled: the new async value.
814 * Configures @sink to perform all state changes asynchronusly. When async is
815 * disabled, the sink will immediately go to PAUSED instead of waiting for a
816 * preroll buffer. This feature is useful if the sink does not synchronize
817 * against the clock or when it is dealing with sparse streams.
822 gst_base_sink_set_async_enabled (GstBaseSink * sink, gboolean enabled)
824 g_return_if_fail (GST_IS_BASE_SINK (sink));
826 GST_BASE_SINK_PREROLL_LOCK (sink);
827 g_atomic_int_set (&sink->priv->async_enabled, enabled);
828 GST_LOG_OBJECT (sink, "set async enabled to %d", enabled);
829 GST_BASE_SINK_PREROLL_UNLOCK (sink);
833 * gst_base_sink_is_async_enabled:
836 * Checks if @sink is currently configured to perform asynchronous state
839 * Returns: TRUE if the sink is configured to perform asynchronous state
845 gst_base_sink_is_async_enabled (GstBaseSink * sink)
849 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
851 res = g_atomic_int_get (&sink->priv->async_enabled);
857 * gst_base_sink_set_ts_offset:
859 * @offset: the new offset
861 * Adjust the synchronisation of @sink with @offset. A negative value will
862 * render buffers earlier than their timestamp. A positive value will delay
863 * rendering. This function can be used to fix playback of badly timestamped
869 gst_base_sink_set_ts_offset (GstBaseSink * sink, GstClockTimeDiff offset)
871 g_return_if_fail (GST_IS_BASE_SINK (sink));
873 GST_OBJECT_LOCK (sink);
874 sink->priv->ts_offset = offset;
875 GST_LOG_OBJECT (sink, "set time offset to %" G_GINT64_FORMAT, offset);
876 GST_OBJECT_UNLOCK (sink);
880 * gst_base_sink_get_ts_offset:
883 * Get the synchronisation offset of @sink.
885 * Returns: The synchronisation offset.
890 gst_base_sink_get_ts_offset (GstBaseSink * sink)
892 GstClockTimeDiff res;
894 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
896 GST_OBJECT_LOCK (sink);
897 res = sink->priv->ts_offset;
898 GST_OBJECT_UNLOCK (sink);
904 * gst_base_sink_get_last_sample:
907 * Get the last sample that arrived in the sink and was used for preroll or for
908 * rendering. This property can be used to generate thumbnails.
910 * The #GstCaps on the sample can be used to determine the type of the buffer.
912 * Free-function: gst_sample_unref
914 * Returns: (transfer full): a #GstSample. gst_sample_unref() after usage.
915 * This function returns NULL when no buffer has arrived in the sink yet
916 * or when the sink is not in PAUSED or PLAYING.
921 gst_base_sink_get_last_sample (GstBaseSink * sink)
923 GstSample *res = NULL;
925 g_return_val_if_fail (GST_IS_BASE_SINK (sink), NULL);
927 GST_OBJECT_LOCK (sink);
928 if (sink->priv->last_buffer) {
929 res = gst_sample_new (sink->priv->last_buffer,
930 sink->priv->last_caps, &sink->segment, NULL);
932 GST_OBJECT_UNLOCK (sink);
937 /* with OBJECT_LOCK */
939 gst_base_sink_set_last_buffer_unlocked (GstBaseSink * sink, GstBuffer * buffer)
943 old = sink->priv->last_buffer;
944 if (G_LIKELY (old != buffer)) {
945 GST_DEBUG_OBJECT (sink, "setting last buffer to %p", buffer);
946 if (G_LIKELY (buffer))
947 gst_buffer_ref (buffer);
948 sink->priv->last_buffer = buffer;
950 /* copy over the caps */
951 gst_caps_replace (&sink->priv->last_caps, sink->priv->caps);
953 gst_caps_replace (&sink->priv->last_caps, NULL);
957 /* avoid unreffing with the lock because cleanup code might want to take the
959 if (G_LIKELY (old)) {
960 GST_OBJECT_UNLOCK (sink);
961 gst_buffer_unref (old);
962 GST_OBJECT_LOCK (sink);
967 gst_base_sink_set_last_buffer (GstBaseSink * sink, GstBuffer * buffer)
969 if (!g_atomic_int_get (&sink->priv->enable_last_sample))
972 GST_OBJECT_LOCK (sink);
973 gst_base_sink_set_last_buffer_unlocked (sink, buffer);
974 GST_OBJECT_UNLOCK (sink);
978 * gst_base_sink_set_last_sample_enabled:
980 * @enabled: the new enable-last-sample value.
982 * Configures @sink to store the last received sample in the last-sample
988 gst_base_sink_set_last_sample_enabled (GstBaseSink * sink, gboolean enabled)
990 g_return_if_fail (GST_IS_BASE_SINK (sink));
992 /* Only take lock if we change the value */
993 if (g_atomic_int_compare_and_exchange (&sink->priv->enable_last_sample,
994 !enabled, enabled) && !enabled) {
995 GST_OBJECT_LOCK (sink);
996 gst_base_sink_set_last_buffer_unlocked (sink, NULL);
997 GST_OBJECT_UNLOCK (sink);
1002 * gst_base_sink_is_last_sample_enabled:
1005 * Checks if @sink is currently configured to store the last received sample in
1006 * the last-sample property.
1008 * Returns: TRUE if the sink is configured to store the last received sample.
1013 gst_base_sink_is_last_sample_enabled (GstBaseSink * sink)
1015 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
1017 return g_atomic_int_get (&sink->priv->enable_last_sample);
1021 * gst_base_sink_get_latency:
1024 * Get the currently configured latency.
1026 * Returns: The configured latency.
1031 gst_base_sink_get_latency (GstBaseSink * sink)
1035 GST_OBJECT_LOCK (sink);
1036 res = sink->priv->latency;
1037 GST_OBJECT_UNLOCK (sink);
1043 * gst_base_sink_query_latency:
1045 * @live: (out) (allow-none): if the sink is live
1046 * @upstream_live: (out) (allow-none): if an upstream element is live
1047 * @min_latency: (out) (allow-none): the min latency of the upstream elements
1048 * @max_latency: (out) (allow-none): the max latency of the upstream elements
1050 * Query the sink for the latency parameters. The latency will be queried from
1051 * the upstream elements. @live will be TRUE if @sink is configured to
1052 * synchronize against the clock. @upstream_live will be TRUE if an upstream
1055 * If both @live and @upstream_live are TRUE, the sink will want to compensate
1056 * for the latency introduced by the upstream elements by setting the
1057 * @min_latency to a strictly possitive value.
1059 * This function is mostly used by subclasses.
1061 * Returns: TRUE if the query succeeded.
1066 gst_base_sink_query_latency (GstBaseSink * sink, gboolean * live,
1067 gboolean * upstream_live, GstClockTime * min_latency,
1068 GstClockTime * max_latency)
1070 gboolean l, us_live, res, have_latency;
1071 GstClockTime min, max, render_delay;
1073 GstClockTime us_min, us_max;
1075 /* we are live when we sync to the clock */
1076 GST_OBJECT_LOCK (sink);
1078 have_latency = sink->priv->have_latency;
1079 render_delay = sink->priv->render_delay;
1080 GST_OBJECT_UNLOCK (sink);
1082 /* assume no latency */
1088 GST_DEBUG_OBJECT (sink, "we are ready for LATENCY query");
1089 /* we are ready for a latency query this is when we preroll or when we are
1091 query = gst_query_new_latency ();
1093 /* ask the peer for the latency */
1094 if ((res = gst_pad_peer_query (sink->sinkpad, query))) {
1095 /* get upstream min and max latency */
1096 gst_query_parse_latency (query, &us_live, &us_min, &us_max);
1099 /* upstream live, use its latency, subclasses should use these
1100 * values to create the complete latency. */
1105 /* we need to add the render delay if we are live */
1107 min += render_delay;
1109 max += render_delay;
1112 gst_query_unref (query);
1114 GST_DEBUG_OBJECT (sink, "we are not yet ready for LATENCY query");
1118 /* not live, we tried to do the query, if it failed we return TRUE anyway */
1122 GST_DEBUG_OBJECT (sink, "latency query failed but we are not live");
1124 GST_DEBUG_OBJECT (sink, "latency query failed and we are live");
1129 GST_DEBUG_OBJECT (sink, "latency query: live: %d, have_latency %d,"
1130 " upstream: %d, min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT, l,
1131 have_latency, us_live, GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1136 *upstream_live = us_live;
1146 * gst_base_sink_set_render_delay:
1147 * @sink: a #GstBaseSink
1148 * @delay: the new delay
1150 * Set the render delay in @sink to @delay. The render delay is the time
1151 * between actual rendering of a buffer and its synchronisation time. Some
1152 * devices might delay media rendering which can be compensated for with this
1155 * After calling this function, this sink will report additional latency and
1156 * other sinks will adjust their latency to delay the rendering of their media.
1158 * This function is usually called by subclasses.
1163 gst_base_sink_set_render_delay (GstBaseSink * sink, GstClockTime delay)
1165 GstClockTime old_render_delay;
1167 g_return_if_fail (GST_IS_BASE_SINK (sink));
1169 GST_OBJECT_LOCK (sink);
1170 old_render_delay = sink->priv->render_delay;
1171 sink->priv->render_delay = delay;
1172 GST_LOG_OBJECT (sink, "set render delay to %" GST_TIME_FORMAT,
1173 GST_TIME_ARGS (delay));
1174 GST_OBJECT_UNLOCK (sink);
1176 if (delay != old_render_delay) {
1177 GST_DEBUG_OBJECT (sink, "posting latency changed");
1178 gst_element_post_message (GST_ELEMENT_CAST (sink),
1179 gst_message_new_latency (GST_OBJECT_CAST (sink)));
1184 * gst_base_sink_get_render_delay:
1185 * @sink: a #GstBaseSink
1187 * Get the render delay of @sink. see gst_base_sink_set_render_delay() for more
1188 * information about the render delay.
1190 * Returns: the render delay of @sink.
1195 gst_base_sink_get_render_delay (GstBaseSink * sink)
1197 GstClockTimeDiff res;
1199 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1201 GST_OBJECT_LOCK (sink);
1202 res = sink->priv->render_delay;
1203 GST_OBJECT_UNLOCK (sink);
1209 * gst_base_sink_set_blocksize:
1210 * @sink: a #GstBaseSink
1211 * @blocksize: the blocksize in bytes
1213 * Set the number of bytes that the sink will pull when it is operating in pull
1218 /* FIXME 0.11: blocksize property should be int, otherwise min>max.. */
1220 gst_base_sink_set_blocksize (GstBaseSink * sink, guint blocksize)
1222 g_return_if_fail (GST_IS_BASE_SINK (sink));
1224 GST_OBJECT_LOCK (sink);
1225 sink->priv->blocksize = blocksize;
1226 GST_LOG_OBJECT (sink, "set blocksize to %u", blocksize);
1227 GST_OBJECT_UNLOCK (sink);
1231 * gst_base_sink_get_blocksize:
1232 * @sink: a #GstBaseSink
1234 * Get the number of bytes that the sink will pull when it is operating in pull
1237 * Returns: the number of bytes @sink will pull in pull mode.
1241 /* FIXME 0.11: blocksize property should be int, otherwise min>max.. */
1243 gst_base_sink_get_blocksize (GstBaseSink * sink)
1247 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1249 GST_OBJECT_LOCK (sink);
1250 res = sink->priv->blocksize;
1251 GST_OBJECT_UNLOCK (sink);
1257 * gst_base_sink_set_throttle_time:
1258 * @sink: a #GstBaseSink
1259 * @throttle: the throttle time in nanoseconds
1261 * Set the time that will be inserted between rendered buffers. This
1262 * can be used to control the maximum buffers per second that the sink
1268 gst_base_sink_set_throttle_time (GstBaseSink * sink, guint64 throttle)
1270 g_return_if_fail (GST_IS_BASE_SINK (sink));
1272 GST_OBJECT_LOCK (sink);
1273 sink->priv->throttle_time = throttle;
1274 GST_LOG_OBJECT (sink, "set throttle_time to %" G_GUINT64_FORMAT, throttle);
1275 GST_OBJECT_UNLOCK (sink);
1279 * gst_base_sink_get_throttle_time:
1280 * @sink: a #GstBaseSink
1282 * Get the time that will be inserted between frames to control the
1283 * maximum buffers per second.
1285 * Returns: the number of nanoseconds @sink will put between frames.
1290 gst_base_sink_get_throttle_time (GstBaseSink * sink)
1294 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1296 GST_OBJECT_LOCK (sink);
1297 res = sink->priv->throttle_time;
1298 GST_OBJECT_UNLOCK (sink);
1304 gst_base_sink_set_property (GObject * object, guint prop_id,
1305 const GValue * value, GParamSpec * pspec)
1307 GstBaseSink *sink = GST_BASE_SINK (object);
1311 gst_base_sink_set_sync (sink, g_value_get_boolean (value));
1313 case PROP_MAX_LATENESS:
1314 gst_base_sink_set_max_lateness (sink, g_value_get_int64 (value));
1317 gst_base_sink_set_qos_enabled (sink, g_value_get_boolean (value));
1320 gst_base_sink_set_async_enabled (sink, g_value_get_boolean (value));
1322 case PROP_TS_OFFSET:
1323 gst_base_sink_set_ts_offset (sink, g_value_get_int64 (value));
1325 case PROP_BLOCKSIZE:
1326 gst_base_sink_set_blocksize (sink, g_value_get_uint (value));
1328 case PROP_RENDER_DELAY:
1329 gst_base_sink_set_render_delay (sink, g_value_get_uint64 (value));
1331 case PROP_ENABLE_LAST_SAMPLE:
1332 gst_base_sink_set_last_sample_enabled (sink, g_value_get_boolean (value));
1334 case PROP_THROTTLE_TIME:
1335 gst_base_sink_set_throttle_time (sink, g_value_get_uint64 (value));
1338 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1344 gst_base_sink_get_property (GObject * object, guint prop_id, GValue * value,
1347 GstBaseSink *sink = GST_BASE_SINK (object);
1351 g_value_set_boolean (value, gst_base_sink_get_sync (sink));
1353 case PROP_MAX_LATENESS:
1354 g_value_set_int64 (value, gst_base_sink_get_max_lateness (sink));
1357 g_value_set_boolean (value, gst_base_sink_is_qos_enabled (sink));
1360 g_value_set_boolean (value, gst_base_sink_is_async_enabled (sink));
1362 case PROP_TS_OFFSET:
1363 g_value_set_int64 (value, gst_base_sink_get_ts_offset (sink));
1365 case PROP_LAST_SAMPLE:
1366 gst_value_take_buffer (value, gst_base_sink_get_last_sample (sink));
1368 case PROP_ENABLE_LAST_SAMPLE:
1369 g_value_set_boolean (value, gst_base_sink_is_last_sample_enabled (sink));
1371 case PROP_BLOCKSIZE:
1372 g_value_set_uint (value, gst_base_sink_get_blocksize (sink));
1374 case PROP_RENDER_DELAY:
1375 g_value_set_uint64 (value, gst_base_sink_get_render_delay (sink));
1377 case PROP_THROTTLE_TIME:
1378 g_value_set_uint64 (value, gst_base_sink_get_throttle_time (sink));
1381 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1388 gst_base_sink_default_get_caps (GstBaseSink * sink, GstCaps * filter)
1394 gst_base_sink_default_set_caps (GstBaseSink * sink, GstCaps * caps)
1399 /* with PREROLL_LOCK, STREAM_LOCK */
1401 gst_base_sink_commit_state (GstBaseSink * basesink)
1403 /* commit state and proceed to next pending state */
1404 GstState current, next, pending, post_pending;
1405 gboolean post_paused = FALSE;
1406 gboolean post_async_done = FALSE;
1407 gboolean post_playing = FALSE;
1408 gboolean reset_time;
1410 /* we are certainly not playing async anymore now */
1411 basesink->playing_async = FALSE;
1413 GST_OBJECT_LOCK (basesink);
1414 current = GST_STATE (basesink);
1415 next = GST_STATE_NEXT (basesink);
1416 pending = GST_STATE_PENDING (basesink);
1417 post_pending = pending;
1418 reset_time = basesink->priv->reset_time;
1419 basesink->priv->reset_time = FALSE;
1422 case GST_STATE_PLAYING:
1424 GST_DEBUG_OBJECT (basesink, "commiting state to PLAYING");
1426 basesink->need_preroll = FALSE;
1427 post_async_done = TRUE;
1428 basesink->priv->commited = TRUE;
1429 post_playing = TRUE;
1430 /* post PAUSED too when we were READY */
1431 if (current == GST_STATE_READY) {
1436 case GST_STATE_PAUSED:
1437 GST_DEBUG_OBJECT (basesink, "commiting state to PAUSED");
1439 post_async_done = TRUE;
1440 basesink->priv->commited = TRUE;
1441 post_pending = GST_STATE_VOID_PENDING;
1443 case GST_STATE_READY:
1444 case GST_STATE_NULL:
1446 case GST_STATE_VOID_PENDING:
1447 goto nothing_pending;
1452 /* we can report latency queries now */
1453 basesink->priv->have_latency = TRUE;
1455 GST_STATE (basesink) = pending;
1456 GST_STATE_NEXT (basesink) = GST_STATE_VOID_PENDING;
1457 GST_STATE_PENDING (basesink) = GST_STATE_VOID_PENDING;
1458 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_SUCCESS;
1459 GST_OBJECT_UNLOCK (basesink);
1462 GST_DEBUG_OBJECT (basesink, "posting PAUSED state change message");
1463 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1464 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1465 current, next, post_pending));
1467 if (post_async_done) {
1468 GST_DEBUG_OBJECT (basesink, "posting async-done message");
1469 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1470 gst_message_new_async_done (GST_OBJECT_CAST (basesink), reset_time));
1473 GST_DEBUG_OBJECT (basesink, "posting PLAYING state change message");
1474 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1475 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1476 next, pending, GST_STATE_VOID_PENDING));
1479 GST_STATE_BROADCAST (basesink);
1485 /* Depending on the state, set our vars. We get in this situation when the
1486 * state change function got a change to update the state vars before the
1487 * streaming thread did. This is fine but we need to make sure that we
1488 * update the need_preroll var since it was TRUE when we got here and might
1489 * become FALSE if we got to PLAYING. */
1490 GST_DEBUG_OBJECT (basesink, "nothing to commit, now in %s",
1491 gst_element_state_get_name (current));
1493 case GST_STATE_PLAYING:
1494 basesink->need_preroll = FALSE;
1496 case GST_STATE_PAUSED:
1497 basesink->need_preroll = TRUE;
1500 basesink->need_preroll = FALSE;
1501 basesink->flushing = TRUE;
1504 /* we can report latency queries now */
1505 basesink->priv->have_latency = TRUE;
1506 GST_OBJECT_UNLOCK (basesink);
1511 /* app is going to READY */
1512 GST_DEBUG_OBJECT (basesink, "stopping");
1513 basesink->need_preroll = FALSE;
1514 basesink->flushing = TRUE;
1515 GST_OBJECT_UNLOCK (basesink);
1521 start_stepping (GstBaseSink * sink, GstSegment * segment,
1522 GstStepInfo * pending, GstStepInfo * current)
1525 GstMessage *message;
1527 GST_DEBUG_OBJECT (sink, "update pending step");
1529 GST_OBJECT_LOCK (sink);
1530 memcpy (current, pending, sizeof (GstStepInfo));
1531 pending->valid = FALSE;
1532 GST_OBJECT_UNLOCK (sink);
1534 /* post message first */
1536 gst_message_new_step_start (GST_OBJECT (sink), TRUE, current->format,
1537 current->amount, current->rate, current->flush, current->intermediate);
1538 gst_message_set_seqnum (message, current->seqnum);
1539 gst_element_post_message (GST_ELEMENT (sink), message);
1541 /* get the running time of where we paused and remember it */
1542 current->start = gst_element_get_start_time (GST_ELEMENT_CAST (sink));
1543 gst_segment_set_running_time (segment, GST_FORMAT_TIME, current->start);
1545 /* set the new rate for the remainder of the segment */
1546 current->start_rate = segment->rate;
1547 segment->rate *= current->rate;
1550 if (segment->rate > 0.0)
1551 current->start_stop = segment->stop;
1553 current->start_start = segment->start;
1555 if (current->format == GST_FORMAT_TIME) {
1556 end = current->start + current->amount;
1557 if (!current->flush) {
1558 /* update the segment clipping regions for non-flushing seeks */
1559 if (segment->rate > 0.0) {
1560 segment->stop = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1561 segment->position = segment->stop;
1565 position = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1566 segment->time = position;
1567 segment->start = position;
1568 segment->position = position;
1573 GST_DEBUG_OBJECT (sink, "segment now %" GST_SEGMENT_FORMAT, segment);
1574 GST_DEBUG_OBJECT (sink, "step started at running_time %" GST_TIME_FORMAT,
1575 GST_TIME_ARGS (current->start));
1577 if (current->amount == -1) {
1578 GST_DEBUG_OBJECT (sink, "step amount == -1, stop stepping");
1579 current->valid = FALSE;
1581 GST_DEBUG_OBJECT (sink, "step amount: %" G_GUINT64_FORMAT ", format: %s, "
1582 "rate: %f", current->amount, gst_format_get_name (current->format),
1588 stop_stepping (GstBaseSink * sink, GstSegment * segment,
1589 GstStepInfo * current, gint64 rstart, gint64 rstop, gboolean eos)
1591 gint64 stop, position;
1592 GstMessage *message;
1594 GST_DEBUG_OBJECT (sink, "step complete");
1596 if (segment->rate > 0.0)
1601 GST_DEBUG_OBJECT (sink,
1602 "step stop at running_time %" GST_TIME_FORMAT, GST_TIME_ARGS (stop));
1605 current->duration = current->position;
1607 current->duration = stop - current->start;
1609 GST_DEBUG_OBJECT (sink, "step elapsed running_time %" GST_TIME_FORMAT,
1610 GST_TIME_ARGS (current->duration));
1612 position = current->start + current->duration;
1614 /* now move the segment to the new running time */
1615 gst_segment_set_running_time (segment, GST_FORMAT_TIME, position);
1617 if (current->flush) {
1618 /* and remove the time we flushed, start time did not change */
1619 segment->base = current->start;
1621 /* start time is now the stepped position */
1622 gst_element_set_start_time (GST_ELEMENT_CAST (sink), position);
1625 /* restore the previous rate */
1626 segment->rate = current->start_rate;
1628 if (segment->rate > 0.0)
1629 segment->stop = current->start_stop;
1631 segment->start = current->start_start;
1633 /* post the step done when we know the stepped duration in TIME */
1635 gst_message_new_step_done (GST_OBJECT_CAST (sink), current->format,
1636 current->amount, current->rate, current->flush, current->intermediate,
1637 current->duration, eos);
1638 gst_message_set_seqnum (message, current->seqnum);
1639 gst_element_post_message (GST_ELEMENT_CAST (sink), message);
1641 if (!current->intermediate)
1642 sink->need_preroll = current->need_preroll;
1644 /* and the current step info finished and becomes invalid */
1645 current->valid = FALSE;
1649 handle_stepping (GstBaseSink * sink, GstSegment * segment,
1650 GstStepInfo * current, guint64 * cstart, guint64 * cstop, guint64 * rstart,
1653 gboolean step_end = FALSE;
1655 /* see if we need to skip this buffer because of stepping */
1656 switch (current->format) {
1657 case GST_FORMAT_TIME:
1660 guint64 first, last;
1663 if (segment->rate > 0.0) {
1664 if (segment->stop == *cstop)
1665 *rstop = *rstart + current->amount;
1670 if (segment->start == *cstart)
1671 *rstart = *rstop + current->amount;
1677 end = current->start + current->amount;
1678 current->position = first - current->start;
1680 abs_rate = ABS (segment->rate);
1681 if (G_UNLIKELY (abs_rate != 1.0))
1682 current->position /= abs_rate;
1684 GST_DEBUG_OBJECT (sink,
1685 "buffer: %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT,
1686 GST_TIME_ARGS (first), GST_TIME_ARGS (last));
1687 GST_DEBUG_OBJECT (sink,
1688 "got time step %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT "/%"
1689 GST_TIME_FORMAT, GST_TIME_ARGS (current->position),
1690 GST_TIME_ARGS (last - current->start),
1691 GST_TIME_ARGS (current->amount));
1693 if ((current->flush && current->position >= current->amount)
1695 GST_DEBUG_OBJECT (sink, "step ended, we need clipping");
1697 if (segment->rate > 0.0) {
1699 *cstart = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1702 *cstop = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1705 GST_DEBUG_OBJECT (sink,
1706 "cstart %" GST_TIME_FORMAT ", rstart %" GST_TIME_FORMAT,
1707 GST_TIME_ARGS (*cstart), GST_TIME_ARGS (*rstart));
1708 GST_DEBUG_OBJECT (sink,
1709 "cstop %" GST_TIME_FORMAT ", rstop %" GST_TIME_FORMAT,
1710 GST_TIME_ARGS (*cstop), GST_TIME_ARGS (*rstop));
1713 case GST_FORMAT_BUFFERS:
1714 GST_DEBUG_OBJECT (sink,
1715 "got default step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1716 current->position, current->amount);
1718 if (current->position < current->amount) {
1719 current->position++;
1724 case GST_FORMAT_DEFAULT:
1726 GST_DEBUG_OBJECT (sink,
1727 "got unknown step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1728 current->position, current->amount);
1734 /* with STREAM_LOCK, PREROLL_LOCK
1736 * Returns TRUE if the object needs synchronisation and takes therefore
1737 * part in prerolling.
1739 * rsstart/rsstop contain the start/stop in stream time.
1740 * rrstart/rrstop contain the start/stop in running time.
1743 gst_base_sink_get_sync_times (GstBaseSink * basesink, GstMiniObject * obj,
1744 GstClockTime * rsstart, GstClockTime * rsstop,
1745 GstClockTime * rrstart, GstClockTime * rrstop, gboolean * do_sync,
1746 gboolean * stepped, GstStepInfo * step, gboolean * step_end)
1748 GstBaseSinkClass *bclass;
1750 GstClockTime start, stop; /* raw start/stop timestamps */
1751 guint64 cstart, cstop; /* clipped raw timestamps */
1752 guint64 rstart, rstop; /* clipped timestamps converted to running time */
1753 GstClockTime sstart, sstop; /* clipped timestamps converted to stream time */
1755 GstBaseSinkPrivate *priv;
1756 GstSegment *segment;
1759 priv = basesink->priv;
1760 segment = &basesink->segment;
1762 /* start with nothing */
1763 start = stop = GST_CLOCK_TIME_NONE;
1765 if (G_UNLIKELY (GST_IS_EVENT (obj))) {
1766 GstEvent *event = GST_EVENT_CAST (obj);
1768 switch (GST_EVENT_TYPE (event)) {
1769 /* EOS event needs syncing */
1772 if (segment->rate >= 0.0) {
1773 sstart = sstop = priv->current_sstop;
1774 if (!GST_CLOCK_TIME_IS_VALID (sstart)) {
1775 /* we have not seen a buffer yet, use the segment values */
1776 sstart = sstop = gst_segment_to_stream_time (segment,
1777 segment->format, segment->stop);
1780 sstart = sstop = priv->current_sstart;
1781 if (!GST_CLOCK_TIME_IS_VALID (sstart)) {
1782 /* we have not seen a buffer yet, use the segment values */
1783 sstart = sstop = gst_segment_to_stream_time (segment,
1784 segment->format, segment->start);
1788 rstart = rstop = priv->eos_rtime;
1789 *do_sync = rstart != -1;
1790 GST_DEBUG_OBJECT (basesink, "sync times for EOS %" GST_TIME_FORMAT,
1791 GST_TIME_ARGS (rstart));
1792 /* if we are stepping, we end now */
1793 *step_end = step->valid;
1798 /* other events do not need syncing */
1806 /* else do buffer sync code */
1807 buffer = GST_BUFFER_CAST (obj);
1809 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1811 /* just get the times to see if we need syncing, if the start returns -1 we
1813 if (bclass->get_times)
1814 bclass->get_times (basesink, buffer, &start, &stop);
1816 if (!GST_CLOCK_TIME_IS_VALID (start)) {
1817 /* we don't need to sync but we still want to get the timestamps for
1818 * tracking the position */
1819 gst_base_sink_default_get_times (basesink, buffer, &start, &stop);
1825 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
1826 ", stop: %" GST_TIME_FORMAT ", do_sync %d", GST_TIME_ARGS (start),
1827 GST_TIME_ARGS (stop), *do_sync);
1829 /* collect segment and format for code clarity */
1830 format = segment->format;
1833 if (G_UNLIKELY (!gst_segment_clip (segment, format,
1834 start, stop, &cstart, &cstop))) {
1836 GST_DEBUG_OBJECT (basesink, "step out of segment");
1837 /* when we are stepping, pretend we're at the end of the segment */
1838 if (segment->rate > 0.0) {
1839 cstart = segment->stop;
1840 cstop = segment->stop;
1842 cstart = segment->start;
1843 cstop = segment->start;
1847 goto out_of_segment;
1850 if (G_UNLIKELY (start != cstart || stop != cstop)) {
1851 GST_DEBUG_OBJECT (basesink, "clipped to: start %" GST_TIME_FORMAT
1852 ", stop: %" GST_TIME_FORMAT, GST_TIME_ARGS (cstart),
1853 GST_TIME_ARGS (cstop));
1856 /* set last stop position */
1857 if (G_LIKELY (stop != GST_CLOCK_TIME_NONE && cstop != GST_CLOCK_TIME_NONE))
1858 segment->position = cstop;
1860 segment->position = cstart;
1863 rstart = gst_segment_to_running_time (segment, format, cstart);
1864 rstop = gst_segment_to_running_time (segment, format, cstop);
1866 if (G_UNLIKELY (step->valid)) {
1867 if (!(*step_end = handle_stepping (basesink, segment, step, &cstart, &cstop,
1868 &rstart, &rstop))) {
1869 /* step is still busy, we discard data when we are flushing */
1870 *stepped = step->flush;
1871 GST_DEBUG_OBJECT (basesink, "stepping busy");
1874 /* this can produce wrong values if we accumulated non-TIME segments. If this happens,
1875 * upstream is behaving very badly */
1876 sstart = gst_segment_to_stream_time (segment, format, cstart);
1877 sstop = gst_segment_to_stream_time (segment, format, cstop);
1880 /* eos_done label only called when doing EOS, we also stop stepping then */
1881 if (*step_end && step->flush) {
1882 GST_DEBUG_OBJECT (basesink, "flushing step ended");
1883 stop_stepping (basesink, segment, step, rstart, rstop, eos);
1885 /* re-determine running start times for adjusted segment
1886 * (which has a flushed amount of running/accumulated time removed) */
1887 if (!GST_IS_EVENT (obj)) {
1888 GST_DEBUG_OBJECT (basesink, "refresh sync times");
1899 /* buffers and EOS always need syncing and preroll */
1905 /* we usually clip in the chain function already but stepping could cause
1906 * the segment to be updated later. we return FALSE so that we don't try
1908 GST_LOG_OBJECT (basesink, "buffer skipped, not in segment");
1913 /* with STREAM_LOCK, PREROLL_LOCK, LOCK
1914 * adjust a timestamp with the latency and timestamp offset. This function does
1915 * not adjust for the render delay. */
1917 gst_base_sink_adjust_time (GstBaseSink * basesink, GstClockTime time)
1919 GstClockTimeDiff ts_offset;
1921 /* don't do anything funny with invalid timestamps */
1922 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
1925 time += basesink->priv->latency;
1927 /* apply offset, be carefull for underflows */
1928 ts_offset = basesink->priv->ts_offset;
1929 if (ts_offset < 0) {
1930 ts_offset = -ts_offset;
1931 if (ts_offset < time)
1938 /* subtract the render delay again, which was included in the latency */
1939 if (time > basesink->priv->render_delay)
1940 time -= basesink->priv->render_delay;
1948 * gst_base_sink_wait_clock:
1950 * @time: the running_time to be reached
1951 * @jitter: (out) (allow-none): the jitter to be filled with time diff, or NULL
1953 * This function will block until @time is reached. It is usually called by
1954 * subclasses that use their own internal synchronisation.
1956 * If @time is not valid, no sycnhronisation is done and #GST_CLOCK_BADTIME is
1957 * returned. Likewise, if synchronisation is disabled in the element or there
1958 * is no clock, no synchronisation is done and #GST_CLOCK_BADTIME is returned.
1960 * This function should only be called with the PREROLL_LOCK held, like when
1961 * receiving an EOS event in the #GstBaseSinkClass.event() vmethod or when
1962 * receiving a buffer in
1963 * the #GstBaseSinkClass.render() vmethod.
1965 * The @time argument should be the running_time of when this method should
1966 * return and is not adjusted with any latency or offset configured in the
1971 * Returns: #GstClockReturn
1974 gst_base_sink_wait_clock (GstBaseSink * sink, GstClockTime time,
1975 GstClockTimeDiff * jitter)
1979 GstClockTime base_time;
1981 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
1984 GST_OBJECT_LOCK (sink);
1985 if (G_UNLIKELY (!sink->sync))
1988 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (sink)) == NULL))
1991 base_time = GST_ELEMENT_CAST (sink)->base_time;
1992 GST_LOG_OBJECT (sink,
1993 "time %" GST_TIME_FORMAT ", base_time %" GST_TIME_FORMAT,
1994 GST_TIME_ARGS (time), GST_TIME_ARGS (base_time));
1996 /* add base_time to running_time to get the time against the clock */
1999 /* Re-use existing clockid if available */
2000 /* FIXME: Casting to GstClockEntry only works because the types
2002 if (G_LIKELY (sink->priv->cached_clock_id != NULL
2003 && GST_CLOCK_ENTRY_CLOCK ((GstClockEntry *) sink->
2004 priv->cached_clock_id) == clock)) {
2005 if (!gst_clock_single_shot_id_reinit (clock, sink->priv->cached_clock_id,
2007 gst_clock_id_unref (sink->priv->cached_clock_id);
2008 sink->priv->cached_clock_id = gst_clock_new_single_shot_id (clock, time);
2011 if (sink->priv->cached_clock_id != NULL)
2012 gst_clock_id_unref (sink->priv->cached_clock_id);
2013 sink->priv->cached_clock_id = gst_clock_new_single_shot_id (clock, time);
2015 GST_OBJECT_UNLOCK (sink);
2017 /* A blocking wait is performed on the clock. We save the ClockID
2018 * so we can unlock the entry at any time. While we are blocking, we
2019 * release the PREROLL_LOCK so that other threads can interrupt the
2021 sink->clock_id = sink->priv->cached_clock_id;
2022 /* release the preroll lock while waiting */
2023 GST_BASE_SINK_PREROLL_UNLOCK (sink);
2025 ret = gst_clock_id_wait (sink->priv->cached_clock_id, jitter);
2027 GST_BASE_SINK_PREROLL_LOCK (sink);
2028 sink->clock_id = NULL;
2032 /* no syncing needed */
2035 GST_DEBUG_OBJECT (sink, "time not valid, no sync needed");
2036 return GST_CLOCK_BADTIME;
2040 GST_DEBUG_OBJECT (sink, "sync disabled");
2041 GST_OBJECT_UNLOCK (sink);
2042 return GST_CLOCK_BADTIME;
2046 GST_DEBUG_OBJECT (sink, "no clock, can't sync");
2047 GST_OBJECT_UNLOCK (sink);
2048 return GST_CLOCK_BADTIME;
2053 * gst_base_sink_wait_preroll:
2056 * If the #GstBaseSinkClass.render() method performs its own synchronisation
2057 * against the clock it must unblock when going from PLAYING to the PAUSED state
2058 * and call this method before continuing to render the remaining data.
2060 * This function will block until a state change to PLAYING happens (in which
2061 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
2062 * to a state change to READY or a FLUSH event (in which case this function
2063 * returns #GST_FLOW_FLUSHING).
2065 * This function should only be called with the PREROLL_LOCK held, like in the
2068 * Returns: #GST_FLOW_OK if the preroll completed and processing can
2069 * continue. Any other return value should be returned from the render vmethod.
2074 gst_base_sink_wait_preroll (GstBaseSink * sink)
2076 sink->have_preroll = TRUE;
2077 GST_DEBUG_OBJECT (sink, "waiting in preroll for flush or PLAYING");
2078 /* block until the state changes, or we get a flush, or something */
2079 GST_BASE_SINK_PREROLL_WAIT (sink);
2080 sink->have_preroll = FALSE;
2081 if (G_UNLIKELY (sink->flushing))
2083 if (G_UNLIKELY (sink->priv->step_unlock))
2085 GST_DEBUG_OBJECT (sink, "continue after preroll");
2092 GST_DEBUG_OBJECT (sink, "preroll interrupted because of flush");
2093 return GST_FLOW_FLUSHING;
2097 sink->priv->step_unlock = FALSE;
2098 GST_DEBUG_OBJECT (sink, "preroll interrupted because of step");
2099 return GST_FLOW_STEP;
2104 * gst_base_sink_do_preroll:
2106 * @obj: (transfer none): the mini object that caused the preroll
2108 * If the @sink spawns its own thread for pulling buffers from upstream it
2109 * should call this method after it has pulled a buffer. If the element needed
2110 * to preroll, this function will perform the preroll and will then block
2111 * until the element state is changed.
2113 * This function should be called with the PREROLL_LOCK held.
2115 * Returns: #GST_FLOW_OK if the preroll completed and processing can
2116 * continue. Any other return value should be returned from the render vmethod.
2121 gst_base_sink_do_preroll (GstBaseSink * sink, GstMiniObject * obj)
2125 while (G_UNLIKELY (sink->need_preroll)) {
2126 GST_DEBUG_OBJECT (sink, "prerolling object %p", obj);
2128 /* if it's a buffer, we need to call the preroll method */
2129 if (sink->priv->call_preroll) {
2130 GstBaseSinkClass *bclass;
2133 if (GST_IS_BUFFER_LIST (obj)) {
2134 buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0);
2135 g_assert (NULL != buf);
2136 } else if (GST_IS_BUFFER (obj)) {
2137 buf = GST_BUFFER_CAST (obj);
2138 /* For buffer lists do not set last buffer for now */
2139 gst_base_sink_set_last_buffer (sink, buf);
2144 GST_DEBUG_OBJECT (sink, "preroll buffer %" GST_TIME_FORMAT,
2145 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
2147 bclass = GST_BASE_SINK_GET_CLASS (sink);
2148 if (bclass->preroll)
2149 if ((ret = bclass->preroll (sink, buf)) != GST_FLOW_OK)
2150 goto preroll_canceled;
2152 sink->priv->call_preroll = FALSE;
2157 if (G_LIKELY (sink->playing_async)) {
2158 if (G_UNLIKELY (!gst_base_sink_commit_state (sink)))
2162 /* need to recheck here because the commit state could have
2163 * made us not need the preroll anymore */
2164 if (G_LIKELY (sink->need_preroll)) {
2165 /* block until the state changes, or we get a flush, or something */
2166 ret = gst_base_sink_wait_preroll (sink);
2167 if ((ret != GST_FLOW_OK) && (ret != GST_FLOW_STEP))
2168 goto preroll_failed;
2176 GST_DEBUG_OBJECT (sink, "preroll failed, abort state");
2177 gst_element_abort_state (GST_ELEMENT_CAST (sink));
2182 GST_DEBUG_OBJECT (sink, "stopping while commiting state");
2183 return GST_FLOW_FLUSHING;
2187 GST_DEBUG_OBJECT (sink, "preroll failed: %s", gst_flow_get_name (ret));
2193 * gst_base_sink_wait_eos:
2195 * @time: the running_time to be reached
2196 * @jitter: (out) (allow-none): the jitter to be filled with time diff, or NULL
2198 * This function will block until @time is reached. It is usually called by
2199 * subclasses that use their own internal synchronisation but want to let the
2200 * EOS be handled by the base class.
2202 * This function should only be called with the PREROLL_LOCK held, like when
2203 * receiving an EOS event in the ::event vmethod.
2205 * The @time argument should be the running_time of when the EOS should happen
2206 * and will be adjusted with any latency and offset configured in the sink.
2208 * Returns: #GstFlowReturn
2213 gst_base_sink_wait_eos (GstBaseSink * sink, GstClockTime time,
2214 GstClockTimeDiff * jitter)
2216 GstClockReturn status;
2222 GST_DEBUG_OBJECT (sink, "checking preroll");
2224 /* first wait for the playing state before we can continue */
2225 while (G_UNLIKELY (sink->need_preroll)) {
2226 ret = gst_base_sink_wait_preroll (sink);
2227 if ((ret != GST_FLOW_OK) && (ret != GST_FLOW_STEP))
2231 /* preroll done, we can sync since we are in PLAYING now. */
2232 GST_DEBUG_OBJECT (sink, "possibly waiting for clock to reach %"
2233 GST_TIME_FORMAT, GST_TIME_ARGS (time));
2235 /* compensate for latency, ts_offset and render delay */
2236 stime = gst_base_sink_adjust_time (sink, time);
2238 /* wait for the clock, this can be interrupted because we got shut down or
2240 status = gst_base_sink_wait_clock (sink, stime, jitter);
2242 GST_DEBUG_OBJECT (sink, "clock returned %d", status);
2244 /* invalid time, no clock or sync disabled, just continue then */
2245 if (status == GST_CLOCK_BADTIME)
2248 /* waiting could have been interrupted and we can be flushing now */
2249 if (G_UNLIKELY (sink->flushing))
2252 /* retry if we got unscheduled, which means we did not reach the timeout
2253 * yet. if some other error occures, we continue. */
2254 } while (status == GST_CLOCK_UNSCHEDULED);
2256 GST_DEBUG_OBJECT (sink, "end of stream");
2263 GST_DEBUG_OBJECT (sink, "we are flushing");
2264 return GST_FLOW_FLUSHING;
2268 /* with STREAM_LOCK, PREROLL_LOCK
2270 * Make sure we are in PLAYING and synchronize an object to the clock.
2272 * If we need preroll, we are not in PLAYING. We try to commit the state
2273 * if needed and then block if we still are not PLAYING.
2275 * We start waiting on the clock in PLAYING. If we got interrupted, we
2276 * immediately try to re-preroll.
2278 * Some objects do not need synchronisation (most events) and so this function
2279 * immediately returns GST_FLOW_OK.
2281 * for objects that arrive later than max-lateness to be synchronized to the
2282 * clock have the @late boolean set to TRUE.
2284 * This function keeps a running average of the jitter (the diff between the
2285 * clock time and the requested sync time). The jitter is negative for
2286 * objects that arrive in time and positive for late buffers.
2288 * does not take ownership of obj.
2290 static GstFlowReturn
2291 gst_base_sink_do_sync (GstBaseSink * basesink,
2292 GstMiniObject * obj, gboolean * late, gboolean * step_end)
2294 GstClockTimeDiff jitter = 0;
2296 GstClockReturn status = GST_CLOCK_OK;
2297 GstClockTime rstart, rstop, sstart, sstop, stime;
2299 GstBaseSinkPrivate *priv;
2301 GstStepInfo *current, *pending;
2304 priv = basesink->priv;
2307 sstart = sstop = rstart = rstop = GST_CLOCK_TIME_NONE;
2311 priv->current_rstart = GST_CLOCK_TIME_NONE;
2313 /* get stepping info */
2314 current = &priv->current_step;
2315 pending = &priv->pending_step;
2317 /* get timing information for this object against the render segment */
2318 syncable = gst_base_sink_get_sync_times (basesink, obj,
2319 &sstart, &sstop, &rstart, &rstop, &do_sync, &stepped, current, step_end);
2321 if (G_UNLIKELY (stepped))
2324 /* a syncable object needs to participate in preroll and
2325 * clocking. All buffers and EOS are syncable. */
2326 if (G_UNLIKELY (!syncable))
2329 /* store timing info for current object */
2330 priv->current_rstart = rstart;
2331 priv->current_rstop = (GST_CLOCK_TIME_IS_VALID (rstop) ? rstop : rstart);
2333 /* save sync time for eos when the previous object needed sync */
2334 priv->eos_rtime = (do_sync ? priv->current_rstop : GST_CLOCK_TIME_NONE);
2336 /* calculate inter frame spacing */
2337 if (G_UNLIKELY (priv->prev_rstart != -1 && priv->prev_rstart < rstart)) {
2338 GstClockTime in_diff;
2340 in_diff = rstart - priv->prev_rstart;
2342 if (priv->avg_in_diff == -1)
2343 priv->avg_in_diff = in_diff;
2345 priv->avg_in_diff = UPDATE_RUNNING_AVG (priv->avg_in_diff, in_diff);
2347 GST_LOG_OBJECT (basesink, "avg frame diff %" GST_TIME_FORMAT,
2348 GST_TIME_ARGS (priv->avg_in_diff));
2351 priv->prev_rstart = rstart;
2353 if (G_UNLIKELY (priv->earliest_in_time != -1
2354 && rstart < priv->earliest_in_time))
2358 /* first do preroll, this makes sure we commit our state
2359 * to PAUSED and can continue to PLAYING. We cannot perform
2360 * any clock sync in PAUSED because there is no clock. */
2361 ret = gst_base_sink_do_preroll (basesink, obj);
2362 if (G_UNLIKELY (ret != GST_FLOW_OK))
2363 goto preroll_failed;
2365 /* update the segment with a pending step if the current one is invalid and we
2366 * have a new pending one. We only accept new step updates after a preroll */
2367 if (G_UNLIKELY (pending->valid && !current->valid)) {
2368 start_stepping (basesink, &basesink->segment, pending, current);
2372 /* After rendering we store the position of the last buffer so that we can use
2373 * it to report the position. We need to take the lock here. */
2374 GST_OBJECT_LOCK (basesink);
2375 priv->current_sstart = sstart;
2376 priv->current_sstop = (GST_CLOCK_TIME_IS_VALID (sstop) ? sstop : sstart);
2377 GST_OBJECT_UNLOCK (basesink);
2382 /* adjust for latency */
2383 stime = gst_base_sink_adjust_time (basesink, rstart);
2385 /* preroll done, we can sync since we are in PLAYING now. */
2386 GST_DEBUG_OBJECT (basesink, "possibly waiting for clock to reach %"
2387 GST_TIME_FORMAT ", adjusted %" GST_TIME_FORMAT,
2388 GST_TIME_ARGS (rstart), GST_TIME_ARGS (stime));
2390 /* This function will return immediately if start == -1, no clock
2391 * or sync is disabled with GST_CLOCK_BADTIME. */
2392 status = gst_base_sink_wait_clock (basesink, stime, &jitter);
2394 GST_DEBUG_OBJECT (basesink, "clock returned %d, jitter %c%" GST_TIME_FORMAT,
2395 status, (jitter < 0 ? '-' : ' '), GST_TIME_ARGS (ABS (jitter)));
2397 /* invalid time, no clock or sync disabled, just render */
2398 if (status == GST_CLOCK_BADTIME)
2401 /* waiting could have been interrupted and we can be flushing now */
2402 if (G_UNLIKELY (basesink->flushing))
2405 /* check for unlocked by a state change, we are not flushing so
2406 * we can try to preroll on the current buffer. */
2407 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
2408 GST_DEBUG_OBJECT (basesink, "unscheduled, waiting some more");
2409 priv->call_preroll = TRUE;
2413 /* successful syncing done, record observation */
2414 priv->current_jitter = jitter;
2416 /* check if the object should be dropped */
2417 *late = gst_base_sink_is_too_late (basesink, obj, rstart, rstop,
2426 GST_DEBUG_OBJECT (basesink, "skipped stepped object %p", obj);
2432 GST_DEBUG_OBJECT (basesink, "non syncable object %p", obj);
2437 GST_DEBUG_OBJECT (basesink, "dropped because of QoS %p", obj);
2443 GST_DEBUG_OBJECT (basesink, "we are flushing");
2444 return GST_FLOW_FLUSHING;
2448 GST_DEBUG_OBJECT (basesink, "preroll failed");
2455 gst_base_sink_send_qos (GstBaseSink * basesink, GstQOSType type,
2456 gdouble proportion, GstClockTime time, GstClockTimeDiff diff)
2461 /* generate Quality-of-Service event */
2462 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2463 "qos: type %d, proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2464 GST_TIME_FORMAT, type, proportion, diff, GST_TIME_ARGS (time));
2466 event = gst_event_new_qos (type, proportion, diff, time);
2469 res = gst_pad_push_event (basesink->sinkpad, event);
2475 gst_base_sink_perform_qos (GstBaseSink * sink, gboolean dropped)
2477 GstBaseSinkPrivate *priv;
2478 GstClockTime start, stop;
2479 GstClockTimeDiff jitter;
2480 GstClockTime pt, entered, left;
2481 GstClockTime duration;
2486 start = priv->current_rstart;
2488 if (priv->current_step.valid)
2491 /* if Quality-of-Service disabled, do nothing */
2492 if (!g_atomic_int_get (&priv->qos_enabled) ||
2493 !GST_CLOCK_TIME_IS_VALID (start))
2496 stop = priv->current_rstop;
2497 jitter = priv->current_jitter;
2500 /* this is the time the buffer entered the sink */
2501 if (start < -jitter)
2504 entered = start + jitter;
2507 /* this is the time the buffer entered the sink */
2508 entered = start + jitter;
2509 /* this is the time the buffer left the sink */
2510 left = start + jitter;
2513 /* calculate duration of the buffer */
2514 if (GST_CLOCK_TIME_IS_VALID (stop) && stop != start)
2515 duration = stop - start;
2517 duration = priv->avg_in_diff;
2519 /* if we have the time when the last buffer left us, calculate
2520 * processing time */
2521 if (GST_CLOCK_TIME_IS_VALID (priv->last_left)) {
2522 if (entered > priv->last_left) {
2523 pt = entered - priv->last_left;
2531 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "start: %" GST_TIME_FORMAT
2532 ", stop %" GST_TIME_FORMAT ", entered %" GST_TIME_FORMAT ", left %"
2533 GST_TIME_FORMAT ", pt: %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT
2534 ",jitter %" G_GINT64_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (stop),
2535 GST_TIME_ARGS (entered), GST_TIME_ARGS (left), GST_TIME_ARGS (pt),
2536 GST_TIME_ARGS (duration), jitter);
2538 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "avg_duration: %" GST_TIME_FORMAT
2539 ", avg_pt: %" GST_TIME_FORMAT ", avg_rate: %g",
2540 GST_TIME_ARGS (priv->avg_duration), GST_TIME_ARGS (priv->avg_pt),
2543 /* collect running averages. for first observations, we copy the
2545 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_duration))
2546 priv->avg_duration = duration;
2548 priv->avg_duration = UPDATE_RUNNING_AVG (priv->avg_duration, duration);
2550 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_pt))
2553 priv->avg_pt = UPDATE_RUNNING_AVG (priv->avg_pt, pt);
2555 if (priv->avg_duration != 0)
2557 gst_guint64_to_gdouble (priv->avg_pt) /
2558 gst_guint64_to_gdouble (priv->avg_duration);
2562 if (GST_CLOCK_TIME_IS_VALID (priv->last_left)) {
2563 if (dropped || priv->avg_rate < 0.0) {
2564 priv->avg_rate = rate;
2567 priv->avg_rate = UPDATE_RUNNING_AVG_N (priv->avg_rate, rate);
2569 priv->avg_rate = UPDATE_RUNNING_AVG_P (priv->avg_rate, rate);
2573 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink,
2574 "updated: avg_duration: %" GST_TIME_FORMAT ", avg_pt: %" GST_TIME_FORMAT
2575 ", avg_rate: %g", GST_TIME_ARGS (priv->avg_duration),
2576 GST_TIME_ARGS (priv->avg_pt), priv->avg_rate);
2579 if (priv->avg_rate >= 0.0) {
2581 GstClockTimeDiff diff;
2583 /* if we have a valid rate, start sending QoS messages */
2584 if (priv->current_jitter < 0) {
2585 /* make sure we never go below 0 when adding the jitter to the
2587 if (priv->current_rstart < -priv->current_jitter)
2588 priv->current_jitter = -priv->current_rstart;
2591 if (priv->throttle_time > 0) {
2592 diff = priv->throttle_time;
2593 type = GST_QOS_TYPE_THROTTLE;
2595 diff = priv->current_jitter;
2597 type = GST_QOS_TYPE_OVERFLOW;
2599 type = GST_QOS_TYPE_UNDERFLOW;
2602 gst_base_sink_send_qos (sink, type, priv->avg_rate, priv->current_rstart,
2606 /* record when this buffer will leave us */
2607 priv->last_left = left;
2610 /* reset all qos measuring */
2612 gst_base_sink_reset_qos (GstBaseSink * sink)
2614 GstBaseSinkPrivate *priv;
2618 priv->last_render_time = GST_CLOCK_TIME_NONE;
2619 priv->prev_rstart = GST_CLOCK_TIME_NONE;
2620 priv->earliest_in_time = GST_CLOCK_TIME_NONE;
2621 priv->last_left = GST_CLOCK_TIME_NONE;
2622 priv->avg_duration = GST_CLOCK_TIME_NONE;
2623 priv->avg_pt = GST_CLOCK_TIME_NONE;
2624 priv->avg_rate = -1.0;
2625 priv->avg_render = GST_CLOCK_TIME_NONE;
2626 priv->avg_in_diff = GST_CLOCK_TIME_NONE;
2632 /* Checks if the object was scheduled too late.
2634 * rstart/rstop contain the running_time start and stop values
2637 * status and jitter contain the return values from the clock wait.
2639 * returns TRUE if the buffer was too late.
2642 gst_base_sink_is_too_late (GstBaseSink * basesink, GstMiniObject * obj,
2643 GstClockTime rstart, GstClockTime rstop,
2644 GstClockReturn status, GstClockTimeDiff jitter)
2647 guint64 max_lateness;
2648 GstBaseSinkPrivate *priv;
2650 priv = basesink->priv;
2654 /* only for objects that were too late */
2655 if (G_LIKELY (status != GST_CLOCK_EARLY))
2658 max_lateness = basesink->max_lateness;
2660 /* check if frame dropping is enabled */
2661 if (max_lateness == -1)
2664 /* only check for buffers */
2665 if (G_UNLIKELY (!GST_IS_BUFFER (obj)))
2668 /* can't do check if we don't have a timestamp */
2669 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (rstart)))
2672 /* we can add a valid stop time */
2673 if (GST_CLOCK_TIME_IS_VALID (rstop))
2674 max_lateness += rstop;
2676 max_lateness += rstart;
2677 /* no stop time, use avg frame diff */
2678 if (priv->avg_in_diff != -1)
2679 max_lateness += priv->avg_in_diff;
2682 /* if the jitter bigger than duration and lateness we are too late */
2683 if ((late = rstart + jitter > max_lateness)) {
2684 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, basesink,
2685 "buffer is too late %" GST_TIME_FORMAT
2686 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (rstart + jitter),
2687 GST_TIME_ARGS (max_lateness));
2688 /* !!emergency!!, if we did not receive anything valid for more than a
2689 * second, render it anyway so the user sees something */
2690 if (GST_CLOCK_TIME_IS_VALID (priv->last_render_time) &&
2691 rstart - priv->last_render_time > GST_SECOND) {
2693 GST_ELEMENT_WARNING (basesink, CORE, CLOCK,
2694 (_("A lot of buffers are being dropped.")),
2695 ("There may be a timestamping problem, or this computer is too slow."));
2696 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, basesink,
2697 "**emergency** last buffer at %" GST_TIME_FORMAT " > GST_SECOND",
2698 GST_TIME_ARGS (priv->last_render_time));
2703 if (!late || !GST_CLOCK_TIME_IS_VALID (priv->last_render_time)) {
2704 priv->last_render_time = rstart;
2705 /* the next allowed input timestamp */
2706 if (priv->throttle_time > 0)
2707 priv->earliest_in_time = rstart + priv->throttle_time;
2714 GST_DEBUG_OBJECT (basesink, "object was scheduled in time");
2719 GST_DEBUG_OBJECT (basesink, "frame dropping disabled");
2724 GST_DEBUG_OBJECT (basesink, "object is not a buffer");
2729 GST_DEBUG_OBJECT (basesink, "buffer has no timestamp");
2734 /* called before and after calling the render vmethod. It keeps track of how
2735 * much time was spent in the render method and is used to check if we are
2738 gst_base_sink_do_render_stats (GstBaseSink * basesink, gboolean start)
2740 GstBaseSinkPrivate *priv;
2742 priv = basesink->priv;
2745 priv->start = gst_util_get_timestamp ();
2747 GstClockTime elapsed;
2749 priv->stop = gst_util_get_timestamp ();
2751 elapsed = GST_CLOCK_DIFF (priv->start, priv->stop);
2753 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_render))
2754 priv->avg_render = elapsed;
2756 priv->avg_render = UPDATE_RUNNING_AVG (priv->avg_render, elapsed);
2758 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2759 "avg_render: %" GST_TIME_FORMAT, GST_TIME_ARGS (priv->avg_render));
2764 gst_base_sink_flush_start (GstBaseSink * basesink, GstPad * pad)
2766 /* make sure we are not blocked on the clock also clear any pending
2768 gst_base_sink_set_flushing (basesink, pad, TRUE);
2770 /* we grab the stream lock but that is not needed since setting the
2771 * sink to flushing would make sure no state commit is being done
2773 GST_PAD_STREAM_LOCK (pad);
2774 gst_base_sink_reset_qos (basesink);
2775 /* and we need to commit our state again on the next
2776 * prerolled buffer */
2777 basesink->playing_async = TRUE;
2778 if (basesink->priv->async_enabled) {
2779 gst_element_lost_state (GST_ELEMENT_CAST (basesink));
2781 /* start time reset in above case as well;
2782 * arranges for a.o. proper position reporting when flushing in PAUSED */
2783 gst_element_set_start_time (GST_ELEMENT_CAST (basesink), 0);
2784 basesink->priv->have_latency = TRUE;
2786 gst_base_sink_set_last_buffer (basesink, NULL);
2787 GST_PAD_STREAM_UNLOCK (pad);
2791 gst_base_sink_flush_stop (GstBaseSink * basesink, GstPad * pad,
2792 gboolean reset_time)
2794 /* unset flushing so we can accept new data, this also flushes out any EOS
2796 gst_base_sink_set_flushing (basesink, pad, FALSE);
2798 /* for position reporting */
2799 GST_OBJECT_LOCK (basesink);
2800 basesink->priv->current_sstart = GST_CLOCK_TIME_NONE;
2801 basesink->priv->current_sstop = GST_CLOCK_TIME_NONE;
2802 basesink->priv->eos_rtime = GST_CLOCK_TIME_NONE;
2803 basesink->priv->call_preroll = TRUE;
2804 basesink->priv->current_step.valid = FALSE;
2805 basesink->priv->pending_step.valid = FALSE;
2806 if (basesink->pad_mode == GST_PAD_MODE_PUSH) {
2807 /* we need new segment info after the flush. */
2808 basesink->have_newsegment = FALSE;
2810 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
2813 basesink->priv->reset_time = reset_time;
2814 GST_OBJECT_UNLOCK (basesink);
2817 static GstFlowReturn
2818 gst_base_sink_default_wait_eos (GstBaseSink * basesink, GstEvent * event)
2821 gboolean late, step_end;
2823 ret = gst_base_sink_do_sync (basesink, GST_MINI_OBJECT_CAST (event),
2830 gst_base_sink_default_event (GstBaseSink * basesink, GstEvent * event)
2832 gboolean result = TRUE;
2833 GstBaseSinkClass *bclass;
2835 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2837 switch (GST_EVENT_TYPE (event)) {
2838 case GST_EVENT_FLUSH_START:
2840 GST_DEBUG_OBJECT (basesink, "flush-start %p", event);
2841 gst_base_sink_flush_start (basesink, basesink->sinkpad);
2844 case GST_EVENT_FLUSH_STOP:
2846 gboolean reset_time;
2848 gst_event_parse_flush_stop (event, &reset_time);
2849 GST_DEBUG_OBJECT (basesink, "flush-stop %p, reset_time: %d", event,
2851 gst_base_sink_flush_stop (basesink, basesink->sinkpad, reset_time);
2856 GstMessage *message;
2859 /* we set the received EOS flag here so that we can use it when testing if
2860 * we are prerolled and to refuse more buffers. */
2861 basesink->priv->received_eos = TRUE;
2864 if (G_LIKELY (bclass->wait_eos)) {
2867 ret = bclass->wait_eos (basesink, event);
2868 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2874 /* the EOS event is completely handled so we mark
2875 * ourselves as being in the EOS state. eos is also
2876 * protected by the object lock so we can read it when
2877 * answering the POSITION query. */
2878 GST_OBJECT_LOCK (basesink);
2879 basesink->eos = TRUE;
2880 GST_OBJECT_UNLOCK (basesink);
2882 /* ok, now we can post the message */
2883 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
2885 seqnum = basesink->priv->seqnum = gst_event_get_seqnum (event);
2886 GST_DEBUG_OBJECT (basesink, "Got seqnum #%" G_GUINT32_FORMAT, seqnum);
2888 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
2889 gst_message_set_seqnum (message, seqnum);
2890 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
2893 case GST_EVENT_CAPS:
2897 GST_DEBUG_OBJECT (basesink, "caps %p", event);
2899 gst_event_parse_caps (event, &caps);
2900 if (bclass->set_caps)
2901 result = bclass->set_caps (basesink, caps);
2904 GST_OBJECT_LOCK (basesink);
2905 gst_caps_replace (&basesink->priv->caps, caps);
2906 GST_OBJECT_UNLOCK (basesink);
2910 case GST_EVENT_SEGMENT:
2911 /* configure the segment */
2912 /* The segment is protected with both the STREAM_LOCK and the OBJECT_LOCK.
2913 * We protect with the OBJECT_LOCK so that we can use the values to
2914 * safely answer a POSITION query. */
2915 GST_OBJECT_LOCK (basesink);
2916 /* the newsegment event is needed to bring the buffer timestamps to the
2917 * stream time and to drop samples outside of the playback segment. */
2918 gst_event_copy_segment (event, &basesink->segment);
2919 GST_DEBUG_OBJECT (basesink, "configured SEGMENT %" GST_SEGMENT_FORMAT,
2920 &basesink->segment);
2921 basesink->have_newsegment = TRUE;
2922 GST_OBJECT_UNLOCK (basesink);
2926 GstTagList *taglist;
2928 gst_event_parse_tag (event, &taglist);
2930 gst_element_post_message (GST_ELEMENT_CAST (basesink),
2931 gst_message_new_tag (GST_OBJECT_CAST (basesink),
2932 gst_tag_list_copy (taglist)));
2935 case GST_EVENT_SINK_MESSAGE:
2937 GstMessage *msg = NULL;
2939 gst_event_parse_sink_message (event, &msg);
2941 gst_element_post_message (GST_ELEMENT_CAST (basesink), msg);
2948 gst_event_unref (event);
2954 gst_base_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
2956 GstBaseSink *basesink;
2957 gboolean result = TRUE;
2958 GstBaseSinkClass *bclass;
2960 basesink = GST_BASE_SINK_CAST (parent);
2961 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2963 GST_DEBUG_OBJECT (basesink, "received event %p %" GST_PTR_FORMAT, event,
2966 switch (GST_EVENT_TYPE (event)) {
2967 case GST_EVENT_FLUSH_STOP:
2968 /* special case for this serialized event because we don't want to grab
2969 * the PREROLL lock or check if we were flushing */
2971 result = bclass->event (basesink, event);
2974 if (GST_EVENT_IS_SERIALIZED (event)) {
2975 GST_BASE_SINK_PREROLL_LOCK (basesink);
2976 if (G_UNLIKELY (basesink->flushing))
2979 if (G_UNLIKELY (basesink->priv->received_eos))
2983 result = bclass->event (basesink, event);
2985 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
2988 result = bclass->event (basesink, event);
2998 GST_DEBUG_OBJECT (basesink, "we are flushing");
2999 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3000 gst_event_unref (event);
3007 GST_DEBUG_OBJECT (basesink, "Event received after EOS, dropping");
3008 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3009 gst_event_unref (event);
3015 /* default implementation to calculate the start and end
3016 * timestamps on a buffer, subclasses can override
3019 gst_base_sink_default_get_times (GstBaseSink * basesink, GstBuffer * buffer,
3020 GstClockTime * start, GstClockTime * end)
3022 GstClockTime timestamp, duration;
3024 /* first sync on DTS, else use PTS */
3025 timestamp = GST_BUFFER_DTS (buffer);
3026 if (!GST_CLOCK_TIME_IS_VALID (timestamp))
3027 timestamp = GST_BUFFER_PTS (buffer);
3029 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
3030 /* get duration to calculate end time */
3031 duration = GST_BUFFER_DURATION (buffer);
3032 if (GST_CLOCK_TIME_IS_VALID (duration)) {
3033 *end = timestamp + duration;
3039 /* must be called with PREROLL_LOCK */
3041 gst_base_sink_needs_preroll (GstBaseSink * basesink)
3043 gboolean is_prerolled, res;
3045 /* we have 2 cases where the PREROLL_LOCK is released:
3046 * 1) we are blocking in the PREROLL_LOCK and thus are prerolled.
3047 * 2) we are syncing on the clock
3049 is_prerolled = basesink->have_preroll || basesink->priv->received_eos;
3050 res = !is_prerolled;
3052 GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d => needs preroll: %d",
3053 basesink->have_preroll, basesink->priv->received_eos, res);
3058 /* with STREAM_LOCK, PREROLL_LOCK
3060 * Takes a buffer and compare the timestamps with the last segment.
3061 * If the buffer falls outside of the segment boundaries, drop it.
3062 * Else send the buffer for preroll and rendering.
3064 * This function takes ownership of the buffer.
3066 static GstFlowReturn
3067 gst_base_sink_chain_unlocked (GstBaseSink * basesink, GstPad * pad,
3070 GstBaseSinkClass *bclass;
3071 GstBaseSinkPrivate *priv = basesink->priv;
3073 GstClockTime start = GST_CLOCK_TIME_NONE, end = GST_CLOCK_TIME_NONE;
3074 GstSegment *segment;
3075 GstBuffer *sync_buf;
3077 gboolean late, step_end;
3079 if (G_UNLIKELY (basesink->flushing))
3082 if (G_UNLIKELY (priv->received_eos))
3085 if (GST_IS_BUFFER_LIST (obj)) {
3086 sync_buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0);
3087 g_assert (NULL != sync_buf);
3089 sync_buf = GST_BUFFER_CAST (obj);
3092 /* for code clarity */
3093 segment = &basesink->segment;
3095 if (G_UNLIKELY (!basesink->have_newsegment)) {
3098 sync = gst_base_sink_get_sync (basesink);
3100 GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
3101 (_("Internal data flow problem.")),
3102 ("Received buffer without a new-segment. Assuming timestamps start from 0."));
3105 /* this means this sink will assume timestamps start from 0 */
3106 GST_OBJECT_LOCK (basesink);
3109 basesink->segment.start = 0;
3110 basesink->segment.stop = -1;
3111 basesink->have_newsegment = TRUE;
3112 GST_OBJECT_UNLOCK (basesink);
3115 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3117 /* check if the buffer needs to be dropped, we first ask the subclass for the
3119 if (bclass->get_times)
3120 bclass->get_times (basesink, sync_buf, &start, &end);
3122 if (!GST_CLOCK_TIME_IS_VALID (start)) {
3123 /* if the subclass does not want sync, we use our own values so that we at
3124 * least clip the buffer to the segment */
3125 gst_base_sink_default_get_times (basesink, sync_buf, &start, &end);
3128 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
3129 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
3131 /* a dropped buffer does not participate in anything */
3132 if (GST_CLOCK_TIME_IS_VALID (start) && (segment->format == GST_FORMAT_TIME)) {
3133 if (G_UNLIKELY (!gst_segment_clip (segment,
3134 GST_FORMAT_TIME, start, end, NULL, NULL)))
3135 goto out_of_segment;
3142 /* synchronize this object, non syncable objects return OK
3144 ret = gst_base_sink_do_sync (basesink, GST_MINI_OBJECT_CAST (sync_buf),
3146 if (G_UNLIKELY (ret != GST_FLOW_OK))
3149 /* drop late buffers unconditionally, let's hope it's unlikely */
3150 if (G_UNLIKELY (late))
3153 /* read once, to get same value before and after */
3154 do_qos = g_atomic_int_get (&priv->qos_enabled);
3156 GST_DEBUG_OBJECT (basesink, "rendering object %p", obj);
3158 /* record rendering time for QoS and stats */
3160 gst_base_sink_do_render_stats (basesink, TRUE);
3162 if (!GST_IS_BUFFER_LIST (obj)) {
3163 /* For buffer lists do not set last buffer for now. */
3164 gst_base_sink_set_last_buffer (basesink, GST_BUFFER_CAST (obj));
3167 ret = bclass->render (basesink, GST_BUFFER_CAST (obj));
3169 if (bclass->render_list)
3170 ret = bclass->render_list (basesink, GST_BUFFER_LIST_CAST (obj));
3174 gst_base_sink_do_render_stats (basesink, FALSE);
3176 if (ret == GST_FLOW_STEP)
3179 if (G_UNLIKELY (basesink->flushing))
3186 /* the step ended, check if we need to activate a new step */
3187 GST_DEBUG_OBJECT (basesink, "step ended");
3188 stop_stepping (basesink, &basesink->segment, &priv->current_step,
3189 priv->current_rstart, priv->current_rstop, basesink->eos);
3193 gst_base_sink_perform_qos (basesink, late);
3195 GST_DEBUG_OBJECT (basesink, "object unref after render %p", obj);
3196 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3203 GST_DEBUG_OBJECT (basesink, "sink is flushing");
3204 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3205 return GST_FLOW_FLUSHING;
3209 GST_DEBUG_OBJECT (basesink, "we are EOS, dropping object, return EOS");
3210 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3211 return GST_FLOW_EOS;
3215 GST_DEBUG_OBJECT (basesink, "dropping buffer, out of clipping segment");
3216 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3221 GST_DEBUG_OBJECT (basesink, "do_sync returned %s", gst_flow_get_name (ret));
3227 GST_DEBUG_OBJECT (basesink, "buffer late, dropping");
3229 if (g_atomic_int_get (&priv->qos_enabled)) {
3230 GstMessage *qos_msg;
3231 GstClockTime timestamp, duration;
3233 timestamp = GST_BUFFER_TIMESTAMP (GST_BUFFER_CAST (sync_buf));
3234 duration = GST_BUFFER_DURATION (GST_BUFFER_CAST (sync_buf));
3236 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
3237 "qos: dropped buffer rt %" GST_TIME_FORMAT ", st %" GST_TIME_FORMAT
3238 ", ts %" GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT,
3239 GST_TIME_ARGS (priv->current_rstart),
3240 GST_TIME_ARGS (priv->current_sstart), GST_TIME_ARGS (timestamp),
3241 GST_TIME_ARGS (duration));
3242 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
3243 "qos: rendered %" G_GUINT64_FORMAT ", dropped %" G_GUINT64_FORMAT,
3244 priv->rendered, priv->dropped);
3247 gst_message_new_qos (GST_OBJECT_CAST (basesink), basesink->sync,
3248 priv->current_rstart, priv->current_sstart, timestamp, duration);
3249 gst_message_set_qos_values (qos_msg, priv->current_jitter, priv->avg_rate,
3251 gst_message_set_qos_stats (qos_msg, GST_FORMAT_BUFFERS, priv->rendered,
3253 gst_element_post_message (GST_ELEMENT_CAST (basesink), qos_msg);
3261 static GstFlowReturn
3262 gst_base_sink_chain_main (GstBaseSink * basesink, GstPad * pad, gpointer obj)
3264 GstFlowReturn result;
3266 if (G_UNLIKELY (basesink->pad_mode != GST_PAD_MODE_PUSH))
3269 GST_BASE_SINK_PREROLL_LOCK (basesink);
3270 result = gst_base_sink_chain_unlocked (basesink, pad, obj);
3271 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3279 GST_OBJECT_LOCK (pad);
3280 GST_WARNING_OBJECT (basesink,
3281 "Push on pad %s:%s, but it was not activated in push mode",
3282 GST_DEBUG_PAD_NAME (pad));
3283 GST_OBJECT_UNLOCK (pad);
3284 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3285 /* we don't post an error message this will signal to the peer
3286 * pushing that EOS is reached. */
3287 result = GST_FLOW_EOS;
3292 static GstFlowReturn
3293 gst_base_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
3295 GstBaseSink *basesink;
3297 basesink = GST_BASE_SINK (parent);
3299 return gst_base_sink_chain_main (basesink, pad, buf);
3302 static GstFlowReturn
3303 gst_base_sink_chain_list (GstPad * pad, GstObject * parent,
3304 GstBufferList * list)
3306 GstBaseSink *basesink;
3307 GstBaseSinkClass *bclass;
3308 GstFlowReturn result;
3310 basesink = GST_BASE_SINK (parent);
3311 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3313 if (G_LIKELY (bclass->render_list)) {
3314 result = gst_base_sink_chain_main (basesink, pad, list);
3319 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3321 len = gst_buffer_list_length (list);
3323 result = GST_FLOW_OK;
3324 for (i = 0; i < len; i++) {
3325 buffer = gst_buffer_list_get (list, i);
3326 result = gst_base_sink_chain_main (basesink, pad,
3327 gst_buffer_ref (buffer));
3328 if (result != GST_FLOW_OK)
3331 gst_buffer_list_unref (list);
3338 gst_base_sink_default_do_seek (GstBaseSink * sink, GstSegment * segment)
3340 gboolean res = TRUE;
3342 /* update our offset if the start/stop position was updated */
3343 if (segment->format == GST_FORMAT_BYTES) {
3344 segment->time = segment->start;
3345 } else if (segment->start == 0) {
3346 /* seek to start, we can implement a default for this. */
3350 GST_INFO_OBJECT (sink, "Can't do a default seek");
3356 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
3359 gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
3360 GstEvent * event, GstSegment * segment)
3362 /* By default, we try one of 2 things:
3363 * - For absolute seek positions, convert the requested position to our
3364 * configured processing format and place it in the output segment \
3365 * - For relative seek positions, convert our current (input) values to the
3366 * seek format, adjust by the relative seek offset and then convert back to
3367 * the processing format
3369 GstSeekType cur_type, stop_type;
3372 GstFormat seek_format;
3375 gboolean res = TRUE;
3377 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3378 &cur_type, &cur, &stop_type, &stop);
3380 if (seek_format == segment->format) {
3381 gst_segment_do_seek (segment, rate, seek_format, flags,
3382 cur_type, cur, stop_type, stop, &update);
3386 if (cur_type != GST_SEEK_TYPE_NONE) {
3387 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3389 gst_pad_query_convert (sink->sinkpad, seek_format, cur, segment->format,
3391 cur_type = GST_SEEK_TYPE_SET;
3394 if (res && stop_type != GST_SEEK_TYPE_NONE) {
3395 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3397 gst_pad_query_convert (sink->sinkpad, seek_format, stop,
3398 segment->format, &stop);
3399 stop_type = GST_SEEK_TYPE_SET;
3402 /* And finally, configure our output segment in the desired format */
3403 gst_segment_do_seek (segment, rate, segment->format, flags, cur_type, cur,
3404 stop_type, stop, &update);
3413 GST_DEBUG_OBJECT (sink, "undefined format given, seek aborted.");
3418 /* perform a seek, only executed in pull mode */
3420 gst_base_sink_perform_seek (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3424 GstFormat seek_format, dest_format;
3426 GstSeekType cur_type, stop_type;
3427 gboolean seekseg_configured = FALSE;
3429 gboolean update, res = TRUE;
3430 GstSegment seeksegment;
3432 dest_format = sink->segment.format;
3435 GST_DEBUG_OBJECT (sink, "performing seek with event %p", event);
3436 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3437 &cur_type, &cur, &stop_type, &stop);
3439 flush = flags & GST_SEEK_FLAG_FLUSH;
3441 GST_DEBUG_OBJECT (sink, "performing seek without event");
3446 GST_DEBUG_OBJECT (sink, "flushing upstream");
3447 gst_pad_push_event (pad, gst_event_new_flush_start ());
3448 gst_base_sink_flush_start (sink, pad);
3450 GST_DEBUG_OBJECT (sink, "pausing pulling thread");
3453 GST_PAD_STREAM_LOCK (pad);
3455 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
3456 * copy the current segment info into the temp segment that we can actually
3457 * attempt the seek with. We only update the real segment if the seek succeeds. */
3458 if (!seekseg_configured) {
3459 memcpy (&seeksegment, &sink->segment, sizeof (GstSegment));
3461 /* now configure the final seek segment */
3463 if (sink->segment.format != seek_format) {
3464 /* OK, here's where we give the subclass a chance to convert the relative
3465 * seek into an absolute one in the processing format. We set up any
3466 * absolute seek above, before taking the stream lock. */
3467 if (!gst_base_sink_default_prepare_seek_segment (sink, event,
3469 GST_DEBUG_OBJECT (sink,
3470 "Preparing the seek failed after flushing. " "Aborting seek");
3474 /* The seek format matches our processing format, no need to ask the
3475 * the subclass to configure the segment. */
3476 gst_segment_do_seek (&seeksegment, rate, seek_format, flags,
3477 cur_type, cur, stop_type, stop, &update);
3480 /* Else, no seek event passed, so we're just (re)starting the
3485 GST_DEBUG_OBJECT (sink, "segment configured from %" G_GINT64_FORMAT
3486 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
3487 seeksegment.start, seeksegment.stop, seeksegment.position);
3489 /* do the seek, segment.position contains the new position. */
3490 res = gst_base_sink_default_do_seek (sink, &seeksegment);
3495 GST_DEBUG_OBJECT (sink, "stop flushing upstream");
3496 gst_pad_push_event (pad, gst_event_new_flush_stop (TRUE));
3497 gst_base_sink_flush_stop (sink, pad, TRUE);
3498 } else if (res && sink->running) {
3499 /* we are running the current segment and doing a non-flushing seek,
3500 * close the segment first based on the position. */
3501 GST_DEBUG_OBJECT (sink, "closing running segment %" G_GINT64_FORMAT
3502 " to %" G_GINT64_FORMAT, sink->segment.start, sink->segment.position);
3505 /* The subclass must have converted the segment to the processing format
3507 if (res && seeksegment.format != dest_format) {
3508 GST_DEBUG_OBJECT (sink, "Subclass failed to prepare a seek segment "
3509 "in the correct format. Aborting seek.");
3513 /* if successful seek, we update our real segment and push
3514 * out the new segment. */
3516 gst_segment_copy_into (&seeksegment, &sink->segment);
3518 if (sink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3519 gst_element_post_message (GST_ELEMENT (sink),
3520 gst_message_new_segment_start (GST_OBJECT (sink),
3521 sink->segment.format, sink->segment.position));
3525 sink->priv->discont = TRUE;
3526 sink->running = TRUE;
3528 GST_PAD_STREAM_UNLOCK (pad);
3534 set_step_info (GstBaseSink * sink, GstStepInfo * current, GstStepInfo * pending,
3535 guint seqnum, GstFormat format, guint64 amount, gdouble rate,
3536 gboolean flush, gboolean intermediate)
3538 GST_OBJECT_LOCK (sink);
3539 pending->seqnum = seqnum;
3540 pending->format = format;
3541 pending->amount = amount;
3542 pending->position = 0;
3543 pending->rate = rate;
3544 pending->flush = flush;
3545 pending->intermediate = intermediate;
3546 pending->valid = TRUE;
3547 /* flush invalidates the current stepping segment */
3549 current->valid = FALSE;
3550 GST_OBJECT_UNLOCK (sink);
3554 gst_base_sink_perform_step (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3556 GstBaseSinkPrivate *priv;
3557 GstBaseSinkClass *bclass;
3558 gboolean flush, intermediate;
3563 GstStepInfo *pending, *current;
3564 GstMessage *message;
3566 bclass = GST_BASE_SINK_GET_CLASS (sink);
3569 GST_DEBUG_OBJECT (sink, "performing step with event %p", event);
3571 gst_event_parse_step (event, &format, &amount, &rate, &flush, &intermediate);
3572 seqnum = gst_event_get_seqnum (event);
3574 pending = &priv->pending_step;
3575 current = &priv->current_step;
3577 /* post message first */
3578 message = gst_message_new_step_start (GST_OBJECT (sink), FALSE, format,
3579 amount, rate, flush, intermediate);
3580 gst_message_set_seqnum (message, seqnum);
3581 gst_element_post_message (GST_ELEMENT (sink), message);
3584 /* we need to call ::unlock before locking PREROLL_LOCK
3585 * since we lock it before going into ::render */
3587 bclass->unlock (sink);
3589 GST_BASE_SINK_PREROLL_LOCK (sink);
3590 /* now that we have the PREROLL lock, clear our unlock request */
3591 if (bclass->unlock_stop)
3592 bclass->unlock_stop (sink);
3594 /* update the stepinfo and make it valid */
3595 set_step_info (sink, current, pending, seqnum, format, amount, rate, flush,
3598 if (sink->priv->async_enabled) {
3599 /* and we need to commit our state again on the next
3600 * prerolled buffer */
3601 sink->playing_async = TRUE;
3602 priv->pending_step.need_preroll = TRUE;
3603 sink->need_preroll = FALSE;
3604 gst_element_lost_state (GST_ELEMENT_CAST (sink));
3606 sink->priv->have_latency = TRUE;
3607 sink->need_preroll = FALSE;
3609 priv->current_sstart = GST_CLOCK_TIME_NONE;
3610 priv->current_sstop = GST_CLOCK_TIME_NONE;
3611 priv->eos_rtime = GST_CLOCK_TIME_NONE;
3612 priv->call_preroll = TRUE;
3613 gst_base_sink_set_last_buffer (sink, NULL);
3614 gst_base_sink_reset_qos (sink);
3616 if (sink->clock_id) {
3617 gst_clock_id_unschedule (sink->clock_id);
3620 if (sink->have_preroll) {
3621 GST_DEBUG_OBJECT (sink, "signal waiter");
3622 priv->step_unlock = TRUE;
3623 GST_BASE_SINK_PREROLL_SIGNAL (sink);
3625 GST_BASE_SINK_PREROLL_UNLOCK (sink);
3627 /* update the stepinfo and make it valid */
3628 set_step_info (sink, current, pending, seqnum, format, amount, rate, flush,
3638 gst_base_sink_loop (GstPad * pad)
3641 GstBaseSink *basesink;
3642 GstBuffer *buf = NULL;
3643 GstFlowReturn result;
3647 parent = GST_OBJECT_PARENT (pad);
3648 basesink = GST_BASE_SINK (parent);
3650 g_assert (basesink->pad_mode == GST_PAD_MODE_PULL);
3652 if ((blocksize = basesink->priv->blocksize) == 0)
3655 offset = basesink->segment.position;
3657 GST_DEBUG_OBJECT (basesink, "pulling %" G_GUINT64_FORMAT ", %u",
3660 result = gst_pad_pull_range (pad, offset, blocksize, &buf);
3661 if (G_UNLIKELY (result != GST_FLOW_OK))
3664 if (G_UNLIKELY (buf == NULL))
3667 offset += gst_buffer_get_size (buf);
3669 basesink->segment.position = offset;
3671 GST_BASE_SINK_PREROLL_LOCK (basesink);
3672 result = gst_base_sink_chain_unlocked (basesink, pad, buf);
3673 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3674 if (G_UNLIKELY (result != GST_FLOW_OK))
3682 GST_LOG_OBJECT (basesink, "pausing task, reason %s",
3683 gst_flow_get_name (result));
3684 gst_pad_pause_task (pad);
3685 if (result == GST_FLOW_EOS) {
3686 /* perform EOS logic */
3687 if (basesink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3688 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3689 gst_message_new_segment_done (GST_OBJECT_CAST (basesink),
3690 basesink->segment.format, basesink->segment.position));
3692 gst_base_sink_event (pad, parent, gst_event_new_eos ());
3694 } else if (result == GST_FLOW_NOT_LINKED || result <= GST_FLOW_EOS) {
3695 /* for fatal errors we post an error message, post the error
3696 * first so the app knows about the error first.
3697 * wrong-state is not a fatal error because it happens due to
3698 * flushing and posting an error message in that case is the
3699 * wrong thing to do, e.g. when basesrc is doing a flushing
3701 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
3702 (_("Internal data stream error.")),
3703 ("stream stopped, reason %s", gst_flow_get_name (result)));
3704 gst_base_sink_event (pad, parent, gst_event_new_eos ());
3710 GST_LOG_OBJECT (basesink, "no buffer, pausing");
3711 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
3712 (_("Internal data flow error.")), ("element returned NULL buffer"));
3713 result = GST_FLOW_ERROR;
3719 gst_base_sink_set_flushing (GstBaseSink * basesink, GstPad * pad,
3722 GstBaseSinkClass *bclass;
3724 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3727 /* unlock any subclasses, we need to do this before grabbing the
3728 * PREROLL_LOCK since we hold this lock before going into ::render. */
3730 bclass->unlock (basesink);
3733 GST_BASE_SINK_PREROLL_LOCK (basesink);
3734 basesink->flushing = flushing;
3736 /* step 1, now that we have the PREROLL lock, clear our unlock request */
3737 if (bclass->unlock_stop)
3738 bclass->unlock_stop (basesink);
3740 /* set need_preroll before we unblock the clock. If the clock is unblocked
3741 * before timing out, we can reuse the buffer for preroll. */
3742 basesink->need_preroll = TRUE;
3744 /* step 2, unblock clock sync (if any) or any other blocking thing */
3745 if (basesink->clock_id) {
3746 gst_clock_id_unschedule (basesink->clock_id);
3749 /* flush out the data thread if it's locked in finish_preroll, this will
3750 * also flush out the EOS state */
3751 GST_DEBUG_OBJECT (basesink,
3752 "flushing out data thread, need preroll to TRUE");
3754 /* we can't have EOS anymore now */
3755 basesink->eos = FALSE;
3756 basesink->priv->received_eos = FALSE;
3757 basesink->have_preroll = FALSE;
3758 basesink->priv->step_unlock = FALSE;
3759 /* can't report latency anymore until we preroll again */
3760 if (basesink->priv->async_enabled) {
3761 GST_OBJECT_LOCK (basesink);
3762 basesink->priv->have_latency = FALSE;
3763 GST_OBJECT_UNLOCK (basesink);
3765 /* and signal any waiters now */
3766 GST_BASE_SINK_PREROLL_SIGNAL (basesink);
3768 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3774 gst_base_sink_default_activate_pull (GstBaseSink * basesink, gboolean active)
3780 result = gst_pad_start_task (basesink->sinkpad,
3781 (GstTaskFunction) gst_base_sink_loop, basesink->sinkpad);
3783 /* step 2, make sure streaming finishes */
3784 result = gst_pad_stop_task (basesink->sinkpad);
3791 gst_base_sink_pad_activate (GstPad * pad, GstObject * parent)
3793 gboolean result = FALSE;
3794 GstBaseSink *basesink;
3798 basesink = GST_BASE_SINK (parent);
3800 GST_DEBUG_OBJECT (basesink, "Trying pull mode first");
3802 gst_base_sink_set_flushing (basesink, pad, FALSE);
3804 /* we need to have the pull mode enabled */
3805 if (!basesink->can_activate_pull) {
3806 GST_DEBUG_OBJECT (basesink, "pull mode disabled");
3810 /* check if downstreams supports pull mode at all */
3811 query = gst_query_new_scheduling ();
3813 if (!gst_pad_peer_query (pad, query)) {
3814 gst_query_unref (query);
3815 GST_DEBUG_OBJECT (basesink, "peer query faild, no pull mode");
3819 /* parse result of the query */
3820 pull_mode = gst_query_has_scheduling_mode (query, GST_PAD_MODE_PULL);
3821 gst_query_unref (query);
3824 GST_DEBUG_OBJECT (basesink, "pull mode not supported");
3828 /* set the pad mode before starting the task so that it's in the
3829 * correct state for the new thread. also the sink set_caps and get_caps
3830 * function checks this */
3831 basesink->pad_mode = GST_PAD_MODE_PULL;
3833 /* we first try to negotiate a format so that when we try to activate
3834 * downstream, it knows about our format */
3835 if (!gst_base_sink_negotiate_pull (basesink)) {
3836 GST_DEBUG_OBJECT (basesink, "failed to negotiate in pull mode");
3840 /* ok activate now */
3841 if (!gst_pad_activate_mode (pad, GST_PAD_MODE_PULL, TRUE)) {
3842 /* clear any pending caps */
3843 GST_OBJECT_LOCK (basesink);
3844 gst_caps_replace (&basesink->priv->caps, NULL);
3845 GST_OBJECT_UNLOCK (basesink);
3846 GST_DEBUG_OBJECT (basesink, "failed to activate in pull mode");
3850 GST_DEBUG_OBJECT (basesink, "Success activating pull mode");
3854 /* push mode fallback */
3856 GST_DEBUG_OBJECT (basesink, "Falling back to push mode");
3857 if ((result = gst_pad_activate_mode (pad, GST_PAD_MODE_PUSH, TRUE))) {
3858 GST_DEBUG_OBJECT (basesink, "Success activating push mode");
3863 GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
3864 gst_base_sink_set_flushing (basesink, pad, TRUE);
3871 gst_base_sink_pad_activate_push (GstPad * pad, GstObject * parent,
3875 GstBaseSink *basesink;
3877 basesink = GST_BASE_SINK (parent);
3880 if (!basesink->can_activate_push) {
3882 basesink->pad_mode = GST_PAD_MODE_NONE;
3885 basesink->pad_mode = GST_PAD_MODE_PUSH;
3888 if (G_UNLIKELY (basesink->pad_mode != GST_PAD_MODE_PUSH)) {
3889 g_warning ("Internal GStreamer activation error!!!");
3892 gst_base_sink_set_flushing (basesink, pad, TRUE);
3894 basesink->pad_mode = GST_PAD_MODE_NONE;
3902 gst_base_sink_negotiate_pull (GstBaseSink * basesink)
3909 /* this returns the intersection between our caps and the peer caps. If there
3910 * is no peer, it returns NULL and we can't operate in pull mode so we can
3911 * fail the negotiation. */
3912 caps = gst_pad_get_allowed_caps (GST_BASE_SINK_PAD (basesink));
3913 if (caps == NULL || gst_caps_is_empty (caps))
3914 goto no_caps_possible;
3916 GST_DEBUG_OBJECT (basesink, "allowed caps: %" GST_PTR_FORMAT, caps);
3918 if (gst_caps_is_any (caps)) {
3919 GST_DEBUG_OBJECT (basesink, "caps were ANY after fixating, "
3921 /* neither side has template caps in this case, so they are prepared for
3922 pull() without setcaps() */
3925 caps = gst_caps_make_writable (caps);
3927 gst_base_sink_fixate (basesink, caps);
3928 GST_DEBUG_OBJECT (basesink, "fixated to: %" GST_PTR_FORMAT, caps);
3930 if (gst_caps_is_fixed (caps)) {
3931 if (!gst_pad_send_event (GST_BASE_SINK_PAD (basesink),
3932 gst_event_new_caps (caps)))
3933 goto could_not_set_caps;
3939 gst_caps_unref (caps);
3945 GST_INFO_OBJECT (basesink, "Pipeline could not agree on caps");
3946 GST_DEBUG_OBJECT (basesink, "get_allowed_caps() returned EMPTY");
3948 gst_caps_unref (caps);
3953 GST_INFO_OBJECT (basesink, "Could not set caps: %" GST_PTR_FORMAT, caps);
3954 gst_caps_unref (caps);
3959 /* this won't get called until we implement an activate function */
3961 gst_base_sink_pad_activate_pull (GstPad * pad, GstObject * parent,
3964 gboolean result = FALSE;
3965 GstBaseSink *basesink;
3966 GstBaseSinkClass *bclass;
3968 basesink = GST_BASE_SINK (parent);
3969 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3974 /* we mark we have a newsegment here because pull based
3975 * mode works just fine without having a newsegment before the
3977 gst_segment_init (&basesink->segment, GST_FORMAT_BYTES);
3978 GST_OBJECT_LOCK (basesink);
3979 basesink->have_newsegment = TRUE;
3980 GST_OBJECT_UNLOCK (basesink);
3982 /* get the peer duration in bytes */
3983 result = gst_pad_peer_query_duration (pad, GST_FORMAT_BYTES, &duration);
3985 GST_DEBUG_OBJECT (basesink,
3986 "setting duration in bytes to %" G_GINT64_FORMAT, duration);
3987 basesink->segment.duration = duration;
3989 GST_DEBUG_OBJECT (basesink, "unknown duration");
3992 if (bclass->activate_pull)
3993 result = bclass->activate_pull (basesink, TRUE);
3998 goto activate_failed;
4001 if (G_UNLIKELY (basesink->pad_mode != GST_PAD_MODE_PULL)) {
4002 g_warning ("Internal GStreamer activation error!!!");
4005 result = gst_base_sink_set_flushing (basesink, pad, TRUE);
4006 if (bclass->activate_pull)
4007 result &= bclass->activate_pull (basesink, FALSE);
4008 basesink->pad_mode = GST_PAD_MODE_NONE;
4017 /* reset, as starting the thread failed */
4018 basesink->pad_mode = GST_PAD_MODE_NONE;
4020 GST_ERROR_OBJECT (basesink, "subclass failed to activate in pull mode");
4026 gst_base_sink_pad_activate_mode (GstPad * pad, GstObject * parent,
4027 GstPadMode mode, gboolean active)
4032 case GST_PAD_MODE_PULL:
4033 res = gst_base_sink_pad_activate_pull (pad, parent, active);
4035 case GST_PAD_MODE_PUSH:
4036 res = gst_base_sink_pad_activate_push (pad, parent, active);
4039 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
4046 /* send an event to our sinkpad peer. */
4048 gst_base_sink_send_event (GstElement * element, GstEvent * event)
4051 GstBaseSink *basesink = GST_BASE_SINK (element);
4052 gboolean forward, result = TRUE;
4055 GST_OBJECT_LOCK (element);
4056 /* get the pad and the scheduling mode */
4057 pad = gst_object_ref (basesink->sinkpad);
4058 mode = basesink->pad_mode;
4059 GST_OBJECT_UNLOCK (element);
4061 /* only push UPSTREAM events upstream */
4062 forward = GST_EVENT_IS_UPSTREAM (event);
4064 GST_DEBUG_OBJECT (basesink, "handling event %p %" GST_PTR_FORMAT, event,
4067 switch (GST_EVENT_TYPE (event)) {
4068 case GST_EVENT_LATENCY:
4070 GstClockTime latency;
4072 gst_event_parse_latency (event, &latency);
4074 /* store the latency. We use this to adjust the running_time before syncing
4075 * it to the clock. */
4076 GST_OBJECT_LOCK (element);
4077 basesink->priv->latency = latency;
4078 if (!basesink->priv->have_latency)
4080 GST_OBJECT_UNLOCK (element);
4081 GST_DEBUG_OBJECT (basesink, "latency set to %" GST_TIME_FORMAT,
4082 GST_TIME_ARGS (latency));
4084 /* We forward this event so that all elements know about the global pipeline
4085 * latency. This is interesting for an element when it wants to figure out
4086 * when a particular piece of data will be rendered. */
4089 case GST_EVENT_SEEK:
4090 /* in pull mode we will execute the seek */
4091 if (mode == GST_PAD_MODE_PULL)
4092 result = gst_base_sink_perform_seek (basesink, pad, event);
4094 case GST_EVENT_STEP:
4095 result = gst_base_sink_perform_step (basesink, pad, event);
4103 result = gst_pad_push_event (pad, event);
4105 /* not forwarded, unref the event */
4106 gst_event_unref (event);
4109 gst_object_unref (pad);
4111 GST_DEBUG_OBJECT (basesink, "handled event %p %" GST_PTR_FORMAT ": %d", event,
4118 gst_base_sink_get_position (GstBaseSink * basesink, GstFormat format,
4119 gint64 * cur, gboolean * upstream)
4121 GstClock *clock = NULL;
4122 gboolean res = FALSE;
4124 GstSegment *segment;
4125 GstClockTime now, latency;
4126 GstClockTimeDiff base_time;
4127 gint64 time, base, duration;
4130 gboolean last_seen, with_clock, in_paused;
4132 GST_OBJECT_LOCK (basesink);
4133 /* we can only get the segment when we are not NULL or READY */
4134 if (!basesink->have_newsegment)
4138 /* when not in PLAYING or when we're busy with a state change, we
4139 * cannot read from the clock so we report time based on the
4140 * last seen timestamp. */
4141 if (GST_STATE (basesink) != GST_STATE_PLAYING ||
4142 GST_STATE_PENDING (basesink) != GST_STATE_VOID_PENDING) {
4146 segment = &basesink->segment;
4148 /* get the format in the segment */
4149 oformat = segment->format;
4151 /* report with last seen position when EOS */
4152 last_seen = basesink->eos;
4154 /* assume we will use the clock for getting the current position */
4156 if (basesink->sync == FALSE)
4159 /* and we need a clock */
4160 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (basesink)) == NULL))
4163 gst_object_ref (clock);
4165 /* mainloop might be querying position when going to playing async,
4166 * while (audio) rendering might be quickly advancing stream position,
4167 * so use clock asap rather than last reported position */
4168 if (in_paused && with_clock && g_atomic_int_get (&basesink->priv->to_playing)) {
4169 GST_DEBUG_OBJECT (basesink, "going to PLAYING, so not PAUSED");
4173 /* collect all data we need holding the lock */
4174 if (GST_CLOCK_TIME_IS_VALID (segment->time))
4175 time = segment->time;
4179 if (GST_CLOCK_TIME_IS_VALID (segment->stop))
4180 duration = segment->stop - segment->start;
4184 base = segment->base;
4185 rate = segment->rate * segment->applied_rate;
4186 latency = basesink->priv->latency;
4188 if (oformat == GST_FORMAT_TIME) {
4191 start = basesink->priv->current_sstart;
4192 stop = basesink->priv->current_sstop;
4195 /* in paused we use the last position as a lower bound */
4196 if (stop == -1 || segment->rate > 0.0)
4201 /* in playing, use last stop time as upper bound */
4202 if (start == -1 || segment->rate > 0.0)
4208 /* convert last stop to stream time */
4209 last = gst_segment_to_stream_time (segment, oformat, segment->position);
4213 /* in paused, use start_time */
4214 base_time = GST_ELEMENT_START_TIME (basesink);
4215 GST_DEBUG_OBJECT (basesink, "in paused, using start time %" GST_TIME_FORMAT,
4216 GST_TIME_ARGS (base_time));
4217 } else if (with_clock) {
4218 /* else use clock when needed */
4219 base_time = GST_ELEMENT_CAST (basesink)->base_time;
4220 GST_DEBUG_OBJECT (basesink, "using clock and base time %" GST_TIME_FORMAT,
4221 GST_TIME_ARGS (base_time));
4223 /* else, no sync or clock -> no base time */
4224 GST_DEBUG_OBJECT (basesink, "no sync or no clock");
4228 /* no base_time, we can't calculate running_time, use last seem timestamp to report
4230 if (base_time == -1)
4233 /* need to release the object lock before we can get the time,
4234 * a clock might take the LOCK of the provider, which could be
4235 * a basesink subclass. */
4236 GST_OBJECT_UNLOCK (basesink);
4239 /* in EOS or when no valid stream_time, report the value of last seen
4242 /* no timestamp, we need to ask upstream */
4243 GST_DEBUG_OBJECT (basesink, "no last seen timestamp, asking upstream");
4248 GST_DEBUG_OBJECT (basesink, "using last seen timestamp %" GST_TIME_FORMAT,
4249 GST_TIME_ARGS (last));
4252 if (oformat != GST_FORMAT_TIME) {
4253 /* convert base, time and duration to time */
4254 if (!gst_pad_query_convert (basesink->sinkpad, oformat, base,
4255 GST_FORMAT_TIME, &base))
4256 goto convert_failed;
4257 if (!gst_pad_query_convert (basesink->sinkpad, oformat, duration,
4258 GST_FORMAT_TIME, &duration))
4259 goto convert_failed;
4260 if (!gst_pad_query_convert (basesink->sinkpad, oformat, time,
4261 GST_FORMAT_TIME, &time))
4262 goto convert_failed;
4263 if (!gst_pad_query_convert (basesink->sinkpad, oformat, last,
4264 GST_FORMAT_TIME, &last))
4265 goto convert_failed;
4267 /* assume time format from now on */
4268 oformat = GST_FORMAT_TIME;
4271 if (!in_paused && with_clock) {
4272 now = gst_clock_get_time (clock);
4278 /* subtract base time and base time from the clock time.
4279 * Make sure we don't go negative. This is the current time in
4280 * the segment which we need to scale with the combined
4281 * rate and applied rate. */
4283 base_time += latency;
4284 if (GST_CLOCK_DIFF (base_time, now) < 0)
4287 /* for negative rates we need to count back from the segment
4292 *cur = time + gst_guint64_to_gdouble (now - base_time) * rate;
4295 /* never report less than segment values in paused */
4297 *cur = MAX (last, *cur);
4299 /* never report more than last seen position in playing */
4301 *cur = MIN (last, *cur);
4304 GST_DEBUG_OBJECT (basesink,
4305 "now %" GST_TIME_FORMAT " - base_time %" GST_TIME_FORMAT " - base %"
4306 GST_TIME_FORMAT " + time %" GST_TIME_FORMAT " last %" GST_TIME_FORMAT,
4307 GST_TIME_ARGS (now), GST_TIME_ARGS (base_time), GST_TIME_ARGS (base),
4308 GST_TIME_ARGS (time), GST_TIME_ARGS (last));
4311 if (oformat != format) {
4312 /* convert to final format */
4313 if (!gst_pad_query_convert (basesink->sinkpad, oformat, *cur, format, cur))
4314 goto convert_failed;
4320 GST_DEBUG_OBJECT (basesink, "res: %d, POSITION: %" GST_TIME_FORMAT,
4321 res, GST_TIME_ARGS (*cur));
4324 gst_object_unref (clock);
4331 /* in NULL or READY we always return FALSE and -1 */
4332 GST_DEBUG_OBJECT (basesink, "position in wrong state, return -1");
4335 GST_OBJECT_UNLOCK (basesink);
4340 GST_DEBUG_OBJECT (basesink, "convert failed, try upstream");
4348 gst_base_sink_get_duration (GstBaseSink * basesink, GstFormat format,
4349 gint64 * dur, gboolean * upstream)
4351 gboolean res = FALSE;
4353 if (basesink->pad_mode == GST_PAD_MODE_PULL) {
4356 /* get the duration in bytes, in pull mode that's all we are sure to
4357 * know. We have to explicitly get this value from upstream instead of
4358 * using our cached value because it might change. Duration caching
4359 * should be done at a higher level. */
4361 gst_pad_peer_query_duration (basesink->sinkpad, GST_FORMAT_BYTES,
4364 basesink->segment.duration = uduration;
4365 if (format != GST_FORMAT_BYTES) {
4366 /* convert to the requested format */
4368 gst_pad_query_convert (basesink->sinkpad, GST_FORMAT_BYTES,
4369 uduration, format, dur);
4383 default_element_query (GstElement * element, GstQuery * query)
4385 gboolean res = FALSE;
4387 GstBaseSink *basesink = GST_BASE_SINK (element);
4389 switch (GST_QUERY_TYPE (query)) {
4390 case GST_QUERY_POSITION:
4394 gboolean upstream = FALSE;
4396 gst_query_parse_position (query, &format, NULL);
4398 GST_DEBUG_OBJECT (basesink, "position query in format %s",
4399 gst_format_get_name (format));
4401 /* first try to get the position based on the clock */
4403 gst_base_sink_get_position (basesink, format, &cur, &upstream))) {
4404 gst_query_set_position (query, format, cur);
4405 } else if (upstream) {
4406 /* fallback to peer query */
4407 res = gst_pad_peer_query (basesink->sinkpad, query);
4410 /* we can handle a few things if upstream failed */
4411 if (format == GST_FORMAT_PERCENT) {
4414 res = gst_base_sink_get_position (basesink, GST_FORMAT_TIME, &cur,
4416 if (!res && upstream) {
4418 gst_pad_peer_query_position (basesink->sinkpad, GST_FORMAT_TIME,
4422 res = gst_base_sink_get_duration (basesink, GST_FORMAT_TIME, &dur,
4424 if (!res && upstream) {
4426 gst_pad_peer_query_duration (basesink->sinkpad,
4427 GST_FORMAT_TIME, &dur);
4433 pos = gst_util_uint64_scale (100 * GST_FORMAT_PERCENT_SCALE, cur,
4435 gst_query_set_position (query, GST_FORMAT_PERCENT, pos);
4441 case GST_QUERY_DURATION:
4445 gboolean upstream = FALSE;
4447 gst_query_parse_duration (query, &format, NULL);
4449 GST_DEBUG_OBJECT (basesink, "duration query in format %s",
4450 gst_format_get_name (format));
4453 gst_base_sink_get_duration (basesink, format, &dur, &upstream))) {
4454 gst_query_set_duration (query, format, dur);
4455 } else if (upstream) {
4456 /* fallback to peer query */
4457 res = gst_pad_peer_query (basesink->sinkpad, query);
4460 /* we can handle a few things if upstream failed */
4461 if (format == GST_FORMAT_PERCENT) {
4462 gst_query_set_duration (query, GST_FORMAT_PERCENT,
4463 GST_FORMAT_PERCENT_MAX);
4469 case GST_QUERY_LATENCY:
4471 gboolean live, us_live;
4472 GstClockTime min, max;
4474 if ((res = gst_base_sink_query_latency (basesink, &live, &us_live, &min,
4476 gst_query_set_latency (query, live, min, max);
4480 case GST_QUERY_JITTER:
4482 case GST_QUERY_RATE:
4483 /* gst_query_set_rate (query, basesink->segment_rate); */
4486 case GST_QUERY_SEGMENT:
4488 if (basesink->pad_mode == GST_PAD_MODE_PULL) {
4489 gst_query_set_segment (query, basesink->segment.rate,
4490 GST_FORMAT_TIME, basesink->segment.start, basesink->segment.stop);
4493 res = gst_pad_peer_query (basesink->sinkpad, query);
4497 case GST_QUERY_SEEKING:
4498 case GST_QUERY_CONVERT:
4499 case GST_QUERY_FORMATS:
4501 res = gst_pad_peer_query (basesink->sinkpad, query);
4504 GST_DEBUG_OBJECT (basesink, "query %s returns %d",
4505 GST_QUERY_TYPE_NAME (query), res);
4511 gst_base_sink_default_query (GstBaseSink * basesink, GstQuery * query)
4514 GstBaseSinkClass *bclass;
4516 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4518 switch (GST_QUERY_TYPE (query)) {
4519 case GST_QUERY_ALLOCATION:
4521 if (bclass->propose_allocation)
4522 res = bclass->propose_allocation (basesink, query);
4527 case GST_QUERY_CAPS:
4529 GstCaps *caps, *filter;
4531 gst_query_parse_caps (query, &filter);
4532 caps = gst_base_sink_query_caps (basesink, basesink->sinkpad, filter);
4533 gst_query_set_caps_result (query, caps);
4534 gst_caps_unref (caps);
4538 case GST_QUERY_ACCEPT_CAPS:
4540 GstCaps *caps, *allowed;
4543 /* slightly faster than the default implementation */
4544 gst_query_parse_accept_caps (query, &caps);
4545 allowed = gst_base_sink_query_caps (basesink, basesink->sinkpad, NULL);
4546 subset = gst_caps_is_subset (caps, allowed);
4547 gst_query_set_accept_caps_result (query, subset);
4553 gst_pad_query_default (basesink->sinkpad, GST_OBJECT_CAST (basesink),
4561 gst_base_sink_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
4563 GstBaseSink *basesink;
4564 GstBaseSinkClass *bclass;
4567 basesink = GST_BASE_SINK_CAST (parent);
4568 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4571 res = bclass->query (basesink, query);
4578 static GstStateChangeReturn
4579 gst_base_sink_change_state (GstElement * element, GstStateChange transition)
4581 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
4582 GstBaseSink *basesink = GST_BASE_SINK (element);
4583 GstBaseSinkClass *bclass;
4584 GstBaseSinkPrivate *priv;
4586 priv = basesink->priv;
4588 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4590 switch (transition) {
4591 case GST_STATE_CHANGE_NULL_TO_READY:
4593 if (!bclass->start (basesink))
4596 case GST_STATE_CHANGE_READY_TO_PAUSED:
4597 /* need to complete preroll before this state change completes, there
4598 * is no data flow in READY so we can safely assume we need to preroll. */
4599 GST_BASE_SINK_PREROLL_LOCK (basesink);
4600 GST_DEBUG_OBJECT (basesink, "READY to PAUSED");
4601 basesink->have_newsegment = FALSE;
4602 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
4603 basesink->offset = 0;
4604 basesink->have_preroll = FALSE;
4605 priv->step_unlock = FALSE;
4606 basesink->need_preroll = TRUE;
4607 basesink->playing_async = TRUE;
4608 basesink->priv->reset_time = FALSE;
4609 priv->current_sstart = GST_CLOCK_TIME_NONE;
4610 priv->current_sstop = GST_CLOCK_TIME_NONE;
4611 priv->eos_rtime = GST_CLOCK_TIME_NONE;
4613 basesink->eos = FALSE;
4614 priv->received_eos = FALSE;
4615 gst_base_sink_reset_qos (basesink);
4616 priv->commited = FALSE;
4617 priv->call_preroll = TRUE;
4618 priv->current_step.valid = FALSE;
4619 priv->pending_step.valid = FALSE;
4620 if (priv->async_enabled) {
4621 GST_DEBUG_OBJECT (basesink, "doing async state change");
4622 /* when async enabled, post async-start message and return ASYNC from
4623 * the state change function */
4624 ret = GST_STATE_CHANGE_ASYNC;
4625 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4626 gst_message_new_async_start (GST_OBJECT_CAST (basesink)));
4628 priv->have_latency = TRUE;
4630 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
4632 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
4633 GST_BASE_SINK_PREROLL_LOCK (basesink);
4634 g_atomic_int_set (&basesink->priv->to_playing, TRUE);
4635 if (!gst_base_sink_needs_preroll (basesink)) {
4636 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, don't need preroll");
4637 /* no preroll needed anymore now. */
4638 basesink->playing_async = FALSE;
4639 basesink->need_preroll = FALSE;
4640 if (basesink->eos) {
4641 GstMessage *message;
4643 /* need to post EOS message here */
4644 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
4645 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
4646 gst_message_set_seqnum (message, basesink->priv->seqnum);
4647 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
4649 GST_DEBUG_OBJECT (basesink, "signal preroll");
4650 GST_BASE_SINK_PREROLL_SIGNAL (basesink);
4653 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, we are not prerolled");
4654 basesink->need_preroll = TRUE;
4655 basesink->playing_async = TRUE;
4656 priv->call_preroll = TRUE;
4657 priv->commited = FALSE;
4658 if (priv->async_enabled) {
4659 GST_DEBUG_OBJECT (basesink, "doing async state change");
4660 ret = GST_STATE_CHANGE_ASYNC;
4661 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4662 gst_message_new_async_start (GST_OBJECT_CAST (basesink)));
4665 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
4672 GstStateChangeReturn bret;
4674 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4675 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
4676 goto activate_failed;
4679 switch (transition) {
4680 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
4681 /* completed transition, so need not be marked any longer
4682 * And it should be unmarked, since e.g. losing our position upon flush
4683 * does not really change state to PAUSED ... */
4684 g_atomic_int_set (&basesink->priv->to_playing, FALSE);
4686 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
4687 g_atomic_int_set (&basesink->priv->to_playing, FALSE);
4688 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED");
4689 /* FIXME, make sure we cannot enter _render first */
4691 /* we need to call ::unlock before locking PREROLL_LOCK
4692 * since we lock it before going into ::render */
4694 bclass->unlock (basesink);
4696 GST_BASE_SINK_PREROLL_LOCK (basesink);
4697 GST_DEBUG_OBJECT (basesink, "got preroll lock");
4698 /* now that we have the PREROLL lock, clear our unlock request */
4699 if (bclass->unlock_stop)
4700 bclass->unlock_stop (basesink);
4702 /* we need preroll again and we set the flag before unlocking the clockid
4703 * because if the clockid is unlocked before a current buffer expired, we
4704 * can use that buffer to preroll with */
4705 basesink->need_preroll = TRUE;
4707 if (basesink->clock_id) {
4708 GST_DEBUG_OBJECT (basesink, "unschedule clock");
4709 gst_clock_id_unschedule (basesink->clock_id);
4712 /* if we don't have a preroll buffer we need to wait for a preroll and
4714 if (!gst_base_sink_needs_preroll (basesink)) {
4715 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, we are prerolled");
4716 basesink->playing_async = FALSE;
4718 if (GST_STATE_TARGET (GST_ELEMENT (basesink)) <= GST_STATE_READY) {
4719 GST_DEBUG_OBJECT (basesink, "element is <= READY");
4720 ret = GST_STATE_CHANGE_SUCCESS;
4722 GST_DEBUG_OBJECT (basesink,
4723 "PLAYING to PAUSED, we are not prerolled");
4724 basesink->playing_async = TRUE;
4725 priv->commited = FALSE;
4726 priv->call_preroll = TRUE;
4727 if (priv->async_enabled) {
4728 GST_DEBUG_OBJECT (basesink, "doing async state change");
4729 ret = GST_STATE_CHANGE_ASYNC;
4730 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4731 gst_message_new_async_start (GST_OBJECT_CAST (basesink)));
4735 GST_DEBUG_OBJECT (basesink, "rendered: %" G_GUINT64_FORMAT
4736 ", dropped: %" G_GUINT64_FORMAT, priv->rendered, priv->dropped);
4738 gst_base_sink_reset_qos (basesink);
4739 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
4741 case GST_STATE_CHANGE_PAUSED_TO_READY:
4742 GST_BASE_SINK_PREROLL_LOCK (basesink);
4743 /* start by resetting our position state with the object lock so that the
4744 * position query gets the right idea. We do this before we post the
4745 * messages so that the message handlers pick this up. */
4746 GST_OBJECT_LOCK (basesink);
4747 basesink->have_newsegment = FALSE;
4748 priv->current_sstart = GST_CLOCK_TIME_NONE;
4749 priv->current_sstop = GST_CLOCK_TIME_NONE;
4750 priv->have_latency = FALSE;
4751 if (priv->cached_clock_id) {
4752 gst_clock_id_unref (priv->cached_clock_id);
4753 priv->cached_clock_id = NULL;
4755 gst_caps_replace (&basesink->priv->caps, NULL);
4756 GST_OBJECT_UNLOCK (basesink);
4758 gst_base_sink_set_last_buffer (basesink, NULL);
4759 priv->call_preroll = FALSE;
4761 if (!priv->commited) {
4762 if (priv->async_enabled) {
4763 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, posting async-done");
4765 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4766 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
4767 GST_STATE_PLAYING, GST_STATE_PAUSED, GST_STATE_READY));
4769 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4770 gst_message_new_async_done (GST_OBJECT_CAST (basesink), FALSE));
4772 priv->commited = TRUE;
4774 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, don't need_preroll");
4776 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
4778 case GST_STATE_CHANGE_READY_TO_NULL:
4780 if (!bclass->stop (basesink)) {
4781 GST_WARNING_OBJECT (basesink, "failed to stop");
4784 gst_base_sink_set_last_buffer (basesink, NULL);
4785 priv->call_preroll = FALSE;
4796 GST_DEBUG_OBJECT (basesink, "failed to start");
4797 return GST_STATE_CHANGE_FAILURE;
4801 GST_DEBUG_OBJECT (basesink,
4802 "element failed to change states -- activation problem?");
4803 return GST_STATE_CHANGE_FAILURE;