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 GstCaps *gst_base_sink_default_fixate (GstBaseSink * bsink,
393 static GstCaps *gst_base_sink_fixate (GstBaseSink * bsink, GstCaps * caps);
395 /* check if an object was too late */
396 static gboolean gst_base_sink_is_too_late (GstBaseSink * basesink,
397 GstMiniObject * obj, GstClockTime rstart, GstClockTime rstop,
398 GstClockReturn status, GstClockTimeDiff jitter);
401 gst_base_sink_class_init (GstBaseSinkClass * klass)
403 GObjectClass *gobject_class;
404 GstElementClass *gstelement_class;
406 gobject_class = G_OBJECT_CLASS (klass);
407 gstelement_class = GST_ELEMENT_CLASS (klass);
409 GST_DEBUG_CATEGORY_INIT (gst_base_sink_debug, "basesink", 0,
412 g_type_class_add_private (klass, sizeof (GstBaseSinkPrivate));
414 parent_class = g_type_class_peek_parent (klass);
416 gobject_class->finalize = gst_base_sink_finalize;
417 gobject_class->set_property = gst_base_sink_set_property;
418 gobject_class->get_property = gst_base_sink_get_property;
420 g_object_class_install_property (gobject_class, PROP_SYNC,
421 g_param_spec_boolean ("sync", "Sync", "Sync on the clock", DEFAULT_SYNC,
422 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
424 g_object_class_install_property (gobject_class, PROP_MAX_LATENESS,
425 g_param_spec_int64 ("max-lateness", "Max Lateness",
426 "Maximum number of nanoseconds that a buffer can be late before it "
427 "is dropped (-1 unlimited)", -1, G_MAXINT64, DEFAULT_MAX_LATENESS,
428 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
430 g_object_class_install_property (gobject_class, PROP_QOS,
431 g_param_spec_boolean ("qos", "Qos",
432 "Generate Quality-of-Service events upstream", DEFAULT_QOS,
433 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
437 * If set to #TRUE, the basesink will perform asynchronous state changes.
438 * When set to #FALSE, the sink will not signal the parent when it prerolls.
439 * Use this option when dealing with sparse streams or when synchronisation is
444 g_object_class_install_property (gobject_class, PROP_ASYNC,
445 g_param_spec_boolean ("async", "Async",
446 "Go asynchronously to PAUSED", DEFAULT_ASYNC,
447 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
449 * GstBaseSink:ts-offset
451 * Controls the final synchronisation, a negative value will render the buffer
452 * earlier while a positive value delays playback. This property can be
453 * used to fix synchronisation in bad files.
457 g_object_class_install_property (gobject_class, PROP_TS_OFFSET,
458 g_param_spec_int64 ("ts-offset", "TS Offset",
459 "Timestamp offset in nanoseconds", G_MININT64, G_MAXINT64,
460 DEFAULT_TS_OFFSET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
463 * GstBaseSink:enable-last-sample
465 * Enable the last-sample property. If FALSE, basesink doesn't keep a
466 * reference to the last buffer arrived and the last-sample property is always
467 * set to NULL. This can be useful if you need buffers to be released as soon
468 * as possible, eg. if you're using a buffer pool.
472 g_object_class_install_property (gobject_class, PROP_ENABLE_LAST_SAMPLE,
473 g_param_spec_boolean ("enable-last-sample", "Enable Last Buffer",
474 "Enable the last-sample property", DEFAULT_ENABLE_LAST_SAMPLE,
475 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
478 * GstBaseSink:last-sample
480 * The last buffer that arrived in the sink and was used for preroll or for
481 * rendering. This property can be used to generate thumbnails. This property
482 * can be NULL when the sink has not yet received a bufer.
486 g_object_class_install_property (gobject_class, PROP_LAST_SAMPLE,
487 g_param_spec_boxed ("last-sample", "Last Sample",
488 "The last sample received in the sink", GST_TYPE_SAMPLE,
489 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
491 * GstBaseSink:blocksize
493 * The amount of bytes to pull when operating in pull mode.
497 /* FIXME 0.11: blocksize property should be int, otherwise min>max.. */
498 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
499 g_param_spec_uint ("blocksize", "Block size",
500 "Size in bytes to pull per buffer (0 = default)", 0, G_MAXUINT,
501 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
503 * GstBaseSink:render-delay
505 * The additional delay between synchronisation and actual rendering of the
506 * media. This property will add additional latency to the device in order to
507 * make other sinks compensate for the delay.
511 g_object_class_install_property (gobject_class, PROP_RENDER_DELAY,
512 g_param_spec_uint64 ("render-delay", "Render Delay",
513 "Additional render delay of the sink in nanoseconds", 0, G_MAXUINT64,
514 DEFAULT_RENDER_DELAY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
516 * GstBaseSink:throttle-time
518 * The time to insert between buffers. This property can be used to control
519 * the maximum amount of buffers per second to render. Setting this property
520 * to a value bigger than 0 will make the sink create THROTTLE QoS events.
524 g_object_class_install_property (gobject_class, PROP_THROTTLE_TIME,
525 g_param_spec_uint64 ("throttle-time", "Throttle time",
526 "The time to keep between rendered buffers (unused)", 0, G_MAXUINT64,
527 DEFAULT_THROTTLE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
529 gstelement_class->change_state =
530 GST_DEBUG_FUNCPTR (gst_base_sink_change_state);
531 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_sink_send_event);
532 gstelement_class->query = GST_DEBUG_FUNCPTR (default_element_query);
534 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_sink_default_get_caps);
535 klass->set_caps = GST_DEBUG_FUNCPTR (gst_base_sink_default_set_caps);
536 klass->fixate = GST_DEBUG_FUNCPTR (gst_base_sink_default_fixate);
537 klass->activate_pull =
538 GST_DEBUG_FUNCPTR (gst_base_sink_default_activate_pull);
539 klass->get_times = GST_DEBUG_FUNCPTR (gst_base_sink_default_get_times);
540 klass->query = GST_DEBUG_FUNCPTR (gst_base_sink_default_query);
541 klass->event = GST_DEBUG_FUNCPTR (gst_base_sink_default_event);
542 klass->wait_eos = GST_DEBUG_FUNCPTR (gst_base_sink_default_wait_eos);
544 /* Registering debug symbols for function pointers */
545 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_fixate);
546 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_activate);
547 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_activate_mode);
548 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_event);
549 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_chain);
550 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_chain_list);
551 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_sink_query);
555 gst_base_sink_query_caps (GstBaseSink * bsink, GstPad * pad, GstCaps * filter)
557 GstBaseSinkClass *bclass;
558 GstCaps *caps = NULL;
561 bclass = GST_BASE_SINK_GET_CLASS (bsink);
562 fixed = GST_PAD_IS_FIXED_CAPS (pad);
564 if (fixed || bsink->pad_mode == GST_PAD_MODE_PULL) {
565 /* if we are operating in pull mode or fixed caps, we only accept the
566 * currently negotiated caps */
567 caps = gst_pad_get_current_caps (pad);
570 if (bclass->get_caps)
571 caps = bclass->get_caps (bsink, filter);
574 GstPadTemplate *pad_template;
577 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass),
579 if (pad_template != NULL) {
580 caps = gst_pad_template_get_caps (pad_template);
583 GstCaps *intersection;
586 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
587 gst_caps_unref (caps);
598 gst_base_sink_default_fixate (GstBaseSink * bsink, GstCaps * caps)
600 GST_DEBUG_OBJECT (bsink, "using default caps fixate function");
601 return gst_caps_fixate (caps);
605 gst_base_sink_fixate (GstBaseSink * bsink, GstCaps * caps)
607 GstBaseSinkClass *bclass;
609 bclass = GST_BASE_SINK_GET_CLASS (bsink);
612 caps = bclass->fixate (bsink, caps);
618 gst_base_sink_init (GstBaseSink * basesink, gpointer g_class)
620 GstPadTemplate *pad_template;
621 GstBaseSinkPrivate *priv;
623 basesink->priv = priv = GST_BASE_SINK_GET_PRIVATE (basesink);
626 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
627 g_return_if_fail (pad_template != NULL);
629 basesink->sinkpad = gst_pad_new_from_template (pad_template, "sink");
631 gst_pad_set_activate_function (basesink->sinkpad, gst_base_sink_pad_activate);
632 gst_pad_set_activatemode_function (basesink->sinkpad,
633 gst_base_sink_pad_activate_mode);
634 gst_pad_set_query_function (basesink->sinkpad, gst_base_sink_sink_query);
635 gst_pad_set_event_function (basesink->sinkpad, gst_base_sink_event);
636 gst_pad_set_chain_function (basesink->sinkpad, gst_base_sink_chain);
637 gst_pad_set_chain_list_function (basesink->sinkpad, gst_base_sink_chain_list);
638 gst_element_add_pad (GST_ELEMENT_CAST (basesink), basesink->sinkpad);
640 basesink->pad_mode = GST_PAD_MODE_NONE;
641 g_mutex_init (&basesink->preroll_lock);
642 g_cond_init (&basesink->preroll_cond);
643 priv->have_latency = FALSE;
645 basesink->can_activate_push = DEFAULT_CAN_ACTIVATE_PUSH;
646 basesink->can_activate_pull = DEFAULT_CAN_ACTIVATE_PULL;
648 basesink->sync = DEFAULT_SYNC;
649 basesink->max_lateness = DEFAULT_MAX_LATENESS;
650 g_atomic_int_set (&priv->qos_enabled, DEFAULT_QOS);
651 priv->async_enabled = DEFAULT_ASYNC;
652 priv->ts_offset = DEFAULT_TS_OFFSET;
653 priv->render_delay = DEFAULT_RENDER_DELAY;
654 priv->blocksize = DEFAULT_BLOCKSIZE;
655 priv->cached_clock_id = NULL;
656 g_atomic_int_set (&priv->enable_last_sample, DEFAULT_ENABLE_LAST_SAMPLE);
657 priv->throttle_time = DEFAULT_THROTTLE_TIME;
659 GST_OBJECT_FLAG_SET (basesink, GST_ELEMENT_FLAG_SINK);
663 gst_base_sink_finalize (GObject * object)
665 GstBaseSink *basesink;
667 basesink = GST_BASE_SINK (object);
669 g_mutex_clear (&basesink->preroll_lock);
670 g_cond_clear (&basesink->preroll_cond);
672 G_OBJECT_CLASS (parent_class)->finalize (object);
676 * gst_base_sink_set_sync:
678 * @sync: the new sync value.
680 * Configures @sink to synchronize on the clock or not. When
681 * @sync is FALSE, incoming samples will be played as fast as
682 * possible. If @sync is TRUE, the timestamps of the incomming
683 * buffers will be used to schedule the exact render time of its
689 gst_base_sink_set_sync (GstBaseSink * sink, gboolean sync)
691 g_return_if_fail (GST_IS_BASE_SINK (sink));
693 GST_OBJECT_LOCK (sink);
695 GST_OBJECT_UNLOCK (sink);
699 * gst_base_sink_get_sync:
702 * Checks if @sink is currently configured to synchronize against the
705 * Returns: TRUE if the sink is configured to synchronize against the clock.
710 gst_base_sink_get_sync (GstBaseSink * sink)
714 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
716 GST_OBJECT_LOCK (sink);
718 GST_OBJECT_UNLOCK (sink);
724 * gst_base_sink_set_max_lateness:
726 * @max_lateness: the new max lateness value.
728 * Sets the new max lateness value to @max_lateness. This value is
729 * used to decide if a buffer should be dropped or not based on the
730 * buffer timestamp and the current clock time. A value of -1 means
736 gst_base_sink_set_max_lateness (GstBaseSink * sink, gint64 max_lateness)
738 g_return_if_fail (GST_IS_BASE_SINK (sink));
740 GST_OBJECT_LOCK (sink);
741 sink->max_lateness = max_lateness;
742 GST_OBJECT_UNLOCK (sink);
746 * gst_base_sink_get_max_lateness:
749 * Gets the max lateness value. See gst_base_sink_set_max_lateness for
752 * Returns: The maximum time in nanoseconds that a buffer can be late
753 * before it is dropped and not rendered. A value of -1 means an
759 gst_base_sink_get_max_lateness (GstBaseSink * sink)
763 g_return_val_if_fail (GST_IS_BASE_SINK (sink), -1);
765 GST_OBJECT_LOCK (sink);
766 res = sink->max_lateness;
767 GST_OBJECT_UNLOCK (sink);
773 * gst_base_sink_set_qos_enabled:
775 * @enabled: the new qos value.
777 * Configures @sink to send Quality-of-Service events upstream.
782 gst_base_sink_set_qos_enabled (GstBaseSink * sink, gboolean enabled)
784 g_return_if_fail (GST_IS_BASE_SINK (sink));
786 g_atomic_int_set (&sink->priv->qos_enabled, enabled);
790 * gst_base_sink_is_qos_enabled:
793 * Checks if @sink is currently configured to send Quality-of-Service events
796 * Returns: TRUE if the sink is configured to perform Quality-of-Service.
801 gst_base_sink_is_qos_enabled (GstBaseSink * sink)
805 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
807 res = g_atomic_int_get (&sink->priv->qos_enabled);
813 * gst_base_sink_set_async_enabled:
815 * @enabled: the new async value.
817 * Configures @sink to perform all state changes asynchronusly. When async is
818 * disabled, the sink will immediately go to PAUSED instead of waiting for a
819 * preroll buffer. This feature is useful if the sink does not synchronize
820 * against the clock or when it is dealing with sparse streams.
825 gst_base_sink_set_async_enabled (GstBaseSink * sink, gboolean enabled)
827 g_return_if_fail (GST_IS_BASE_SINK (sink));
829 GST_BASE_SINK_PREROLL_LOCK (sink);
830 g_atomic_int_set (&sink->priv->async_enabled, enabled);
831 GST_LOG_OBJECT (sink, "set async enabled to %d", enabled);
832 GST_BASE_SINK_PREROLL_UNLOCK (sink);
836 * gst_base_sink_is_async_enabled:
839 * Checks if @sink is currently configured to perform asynchronous state
842 * Returns: TRUE if the sink is configured to perform asynchronous state
848 gst_base_sink_is_async_enabled (GstBaseSink * sink)
852 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
854 res = g_atomic_int_get (&sink->priv->async_enabled);
860 * gst_base_sink_set_ts_offset:
862 * @offset: the new offset
864 * Adjust the synchronisation of @sink with @offset. A negative value will
865 * render buffers earlier than their timestamp. A positive value will delay
866 * rendering. This function can be used to fix playback of badly timestamped
872 gst_base_sink_set_ts_offset (GstBaseSink * sink, GstClockTimeDiff offset)
874 g_return_if_fail (GST_IS_BASE_SINK (sink));
876 GST_OBJECT_LOCK (sink);
877 sink->priv->ts_offset = offset;
878 GST_LOG_OBJECT (sink, "set time offset to %" G_GINT64_FORMAT, offset);
879 GST_OBJECT_UNLOCK (sink);
883 * gst_base_sink_get_ts_offset:
886 * Get the synchronisation offset of @sink.
888 * Returns: The synchronisation offset.
893 gst_base_sink_get_ts_offset (GstBaseSink * sink)
895 GstClockTimeDiff res;
897 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
899 GST_OBJECT_LOCK (sink);
900 res = sink->priv->ts_offset;
901 GST_OBJECT_UNLOCK (sink);
907 * gst_base_sink_get_last_sample:
910 * Get the last sample that arrived in the sink and was used for preroll or for
911 * rendering. This property can be used to generate thumbnails.
913 * The #GstCaps on the sample can be used to determine the type of the buffer.
915 * Free-function: gst_sample_unref
917 * Returns: (transfer full): a #GstSample. gst_sample_unref() after usage.
918 * This function returns NULL when no buffer has arrived in the sink yet
919 * or when the sink is not in PAUSED or PLAYING.
924 gst_base_sink_get_last_sample (GstBaseSink * sink)
926 GstSample *res = NULL;
928 g_return_val_if_fail (GST_IS_BASE_SINK (sink), NULL);
930 GST_OBJECT_LOCK (sink);
931 if (sink->priv->last_buffer) {
932 res = gst_sample_new (sink->priv->last_buffer,
933 sink->priv->last_caps, &sink->segment, NULL);
935 GST_OBJECT_UNLOCK (sink);
940 /* with OBJECT_LOCK */
942 gst_base_sink_set_last_buffer_unlocked (GstBaseSink * sink, GstBuffer * buffer)
946 old = sink->priv->last_buffer;
947 if (G_LIKELY (old != buffer)) {
948 GST_DEBUG_OBJECT (sink, "setting last buffer to %p", buffer);
949 if (G_LIKELY (buffer))
950 gst_buffer_ref (buffer);
951 sink->priv->last_buffer = buffer;
953 /* copy over the caps */
954 gst_caps_replace (&sink->priv->last_caps, sink->priv->caps);
956 gst_caps_replace (&sink->priv->last_caps, NULL);
960 /* avoid unreffing with the lock because cleanup code might want to take the
962 if (G_LIKELY (old)) {
963 GST_OBJECT_UNLOCK (sink);
964 gst_buffer_unref (old);
965 GST_OBJECT_LOCK (sink);
970 gst_base_sink_set_last_buffer (GstBaseSink * sink, GstBuffer * buffer)
972 if (!g_atomic_int_get (&sink->priv->enable_last_sample))
975 GST_OBJECT_LOCK (sink);
976 gst_base_sink_set_last_buffer_unlocked (sink, buffer);
977 GST_OBJECT_UNLOCK (sink);
981 * gst_base_sink_set_last_sample_enabled:
983 * @enabled: the new enable-last-sample value.
985 * Configures @sink to store the last received sample in the last-sample
991 gst_base_sink_set_last_sample_enabled (GstBaseSink * sink, gboolean enabled)
993 g_return_if_fail (GST_IS_BASE_SINK (sink));
995 /* Only take lock if we change the value */
996 if (g_atomic_int_compare_and_exchange (&sink->priv->enable_last_sample,
997 !enabled, enabled) && !enabled) {
998 GST_OBJECT_LOCK (sink);
999 gst_base_sink_set_last_buffer_unlocked (sink, NULL);
1000 GST_OBJECT_UNLOCK (sink);
1005 * gst_base_sink_is_last_sample_enabled:
1008 * Checks if @sink is currently configured to store the last received sample in
1009 * the last-sample property.
1011 * Returns: TRUE if the sink is configured to store the last received sample.
1016 gst_base_sink_is_last_sample_enabled (GstBaseSink * sink)
1018 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
1020 return g_atomic_int_get (&sink->priv->enable_last_sample);
1024 * gst_base_sink_get_latency:
1027 * Get the currently configured latency.
1029 * Returns: The configured latency.
1034 gst_base_sink_get_latency (GstBaseSink * sink)
1038 GST_OBJECT_LOCK (sink);
1039 res = sink->priv->latency;
1040 GST_OBJECT_UNLOCK (sink);
1046 * gst_base_sink_query_latency:
1048 * @live: (out) (allow-none): if the sink is live
1049 * @upstream_live: (out) (allow-none): if an upstream element is live
1050 * @min_latency: (out) (allow-none): the min latency of the upstream elements
1051 * @max_latency: (out) (allow-none): the max latency of the upstream elements
1053 * Query the sink for the latency parameters. The latency will be queried from
1054 * the upstream elements. @live will be TRUE if @sink is configured to
1055 * synchronize against the clock. @upstream_live will be TRUE if an upstream
1058 * If both @live and @upstream_live are TRUE, the sink will want to compensate
1059 * for the latency introduced by the upstream elements by setting the
1060 * @min_latency to a strictly possitive value.
1062 * This function is mostly used by subclasses.
1064 * Returns: TRUE if the query succeeded.
1069 gst_base_sink_query_latency (GstBaseSink * sink, gboolean * live,
1070 gboolean * upstream_live, GstClockTime * min_latency,
1071 GstClockTime * max_latency)
1073 gboolean l, us_live, res, have_latency;
1074 GstClockTime min, max, render_delay;
1076 GstClockTime us_min, us_max;
1078 /* we are live when we sync to the clock */
1079 GST_OBJECT_LOCK (sink);
1081 have_latency = sink->priv->have_latency;
1082 render_delay = sink->priv->render_delay;
1083 GST_OBJECT_UNLOCK (sink);
1085 /* assume no latency */
1091 GST_DEBUG_OBJECT (sink, "we are ready for LATENCY query");
1092 /* we are ready for a latency query this is when we preroll or when we are
1094 query = gst_query_new_latency ();
1096 /* ask the peer for the latency */
1097 if ((res = gst_pad_peer_query (sink->sinkpad, query))) {
1098 /* get upstream min and max latency */
1099 gst_query_parse_latency (query, &us_live, &us_min, &us_max);
1102 /* upstream live, use its latency, subclasses should use these
1103 * values to create the complete latency. */
1108 /* we need to add the render delay if we are live */
1110 min += render_delay;
1112 max += render_delay;
1115 gst_query_unref (query);
1117 GST_DEBUG_OBJECT (sink, "we are not yet ready for LATENCY query");
1121 /* not live, we tried to do the query, if it failed we return TRUE anyway */
1125 GST_DEBUG_OBJECT (sink, "latency query failed but we are not live");
1127 GST_DEBUG_OBJECT (sink, "latency query failed and we are live");
1132 GST_DEBUG_OBJECT (sink, "latency query: live: %d, have_latency %d,"
1133 " upstream: %d, min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT, l,
1134 have_latency, us_live, GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1139 *upstream_live = us_live;
1149 * gst_base_sink_set_render_delay:
1150 * @sink: a #GstBaseSink
1151 * @delay: the new delay
1153 * Set the render delay in @sink to @delay. The render delay is the time
1154 * between actual rendering of a buffer and its synchronisation time. Some
1155 * devices might delay media rendering which can be compensated for with this
1158 * After calling this function, this sink will report additional latency and
1159 * other sinks will adjust their latency to delay the rendering of their media.
1161 * This function is usually called by subclasses.
1166 gst_base_sink_set_render_delay (GstBaseSink * sink, GstClockTime delay)
1168 GstClockTime old_render_delay;
1170 g_return_if_fail (GST_IS_BASE_SINK (sink));
1172 GST_OBJECT_LOCK (sink);
1173 old_render_delay = sink->priv->render_delay;
1174 sink->priv->render_delay = delay;
1175 GST_LOG_OBJECT (sink, "set render delay to %" GST_TIME_FORMAT,
1176 GST_TIME_ARGS (delay));
1177 GST_OBJECT_UNLOCK (sink);
1179 if (delay != old_render_delay) {
1180 GST_DEBUG_OBJECT (sink, "posting latency changed");
1181 gst_element_post_message (GST_ELEMENT_CAST (sink),
1182 gst_message_new_latency (GST_OBJECT_CAST (sink)));
1187 * gst_base_sink_get_render_delay:
1188 * @sink: a #GstBaseSink
1190 * Get the render delay of @sink. see gst_base_sink_set_render_delay() for more
1191 * information about the render delay.
1193 * Returns: the render delay of @sink.
1198 gst_base_sink_get_render_delay (GstBaseSink * sink)
1200 GstClockTimeDiff res;
1202 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1204 GST_OBJECT_LOCK (sink);
1205 res = sink->priv->render_delay;
1206 GST_OBJECT_UNLOCK (sink);
1212 * gst_base_sink_set_blocksize:
1213 * @sink: a #GstBaseSink
1214 * @blocksize: the blocksize in bytes
1216 * Set the number of bytes that the sink will pull when it is operating in pull
1221 /* FIXME 0.11: blocksize property should be int, otherwise min>max.. */
1223 gst_base_sink_set_blocksize (GstBaseSink * sink, guint blocksize)
1225 g_return_if_fail (GST_IS_BASE_SINK (sink));
1227 GST_OBJECT_LOCK (sink);
1228 sink->priv->blocksize = blocksize;
1229 GST_LOG_OBJECT (sink, "set blocksize to %u", blocksize);
1230 GST_OBJECT_UNLOCK (sink);
1234 * gst_base_sink_get_blocksize:
1235 * @sink: a #GstBaseSink
1237 * Get the number of bytes that the sink will pull when it is operating in pull
1240 * Returns: the number of bytes @sink will pull in pull mode.
1244 /* FIXME 0.11: blocksize property should be int, otherwise min>max.. */
1246 gst_base_sink_get_blocksize (GstBaseSink * sink)
1250 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1252 GST_OBJECT_LOCK (sink);
1253 res = sink->priv->blocksize;
1254 GST_OBJECT_UNLOCK (sink);
1260 * gst_base_sink_set_throttle_time:
1261 * @sink: a #GstBaseSink
1262 * @throttle: the throttle time in nanoseconds
1264 * Set the time that will be inserted between rendered buffers. This
1265 * can be used to control the maximum buffers per second that the sink
1271 gst_base_sink_set_throttle_time (GstBaseSink * sink, guint64 throttle)
1273 g_return_if_fail (GST_IS_BASE_SINK (sink));
1275 GST_OBJECT_LOCK (sink);
1276 sink->priv->throttle_time = throttle;
1277 GST_LOG_OBJECT (sink, "set throttle_time to %" G_GUINT64_FORMAT, throttle);
1278 GST_OBJECT_UNLOCK (sink);
1282 * gst_base_sink_get_throttle_time:
1283 * @sink: a #GstBaseSink
1285 * Get the time that will be inserted between frames to control the
1286 * maximum buffers per second.
1288 * Returns: the number of nanoseconds @sink will put between frames.
1293 gst_base_sink_get_throttle_time (GstBaseSink * sink)
1297 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1299 GST_OBJECT_LOCK (sink);
1300 res = sink->priv->throttle_time;
1301 GST_OBJECT_UNLOCK (sink);
1307 gst_base_sink_set_property (GObject * object, guint prop_id,
1308 const GValue * value, GParamSpec * pspec)
1310 GstBaseSink *sink = GST_BASE_SINK (object);
1314 gst_base_sink_set_sync (sink, g_value_get_boolean (value));
1316 case PROP_MAX_LATENESS:
1317 gst_base_sink_set_max_lateness (sink, g_value_get_int64 (value));
1320 gst_base_sink_set_qos_enabled (sink, g_value_get_boolean (value));
1323 gst_base_sink_set_async_enabled (sink, g_value_get_boolean (value));
1325 case PROP_TS_OFFSET:
1326 gst_base_sink_set_ts_offset (sink, g_value_get_int64 (value));
1328 case PROP_BLOCKSIZE:
1329 gst_base_sink_set_blocksize (sink, g_value_get_uint (value));
1331 case PROP_RENDER_DELAY:
1332 gst_base_sink_set_render_delay (sink, g_value_get_uint64 (value));
1334 case PROP_ENABLE_LAST_SAMPLE:
1335 gst_base_sink_set_last_sample_enabled (sink, g_value_get_boolean (value));
1337 case PROP_THROTTLE_TIME:
1338 gst_base_sink_set_throttle_time (sink, g_value_get_uint64 (value));
1341 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1347 gst_base_sink_get_property (GObject * object, guint prop_id, GValue * value,
1350 GstBaseSink *sink = GST_BASE_SINK (object);
1354 g_value_set_boolean (value, gst_base_sink_get_sync (sink));
1356 case PROP_MAX_LATENESS:
1357 g_value_set_int64 (value, gst_base_sink_get_max_lateness (sink));
1360 g_value_set_boolean (value, gst_base_sink_is_qos_enabled (sink));
1363 g_value_set_boolean (value, gst_base_sink_is_async_enabled (sink));
1365 case PROP_TS_OFFSET:
1366 g_value_set_int64 (value, gst_base_sink_get_ts_offset (sink));
1368 case PROP_LAST_SAMPLE:
1369 gst_value_take_buffer (value, gst_base_sink_get_last_sample (sink));
1371 case PROP_ENABLE_LAST_SAMPLE:
1372 g_value_set_boolean (value, gst_base_sink_is_last_sample_enabled (sink));
1374 case PROP_BLOCKSIZE:
1375 g_value_set_uint (value, gst_base_sink_get_blocksize (sink));
1377 case PROP_RENDER_DELAY:
1378 g_value_set_uint64 (value, gst_base_sink_get_render_delay (sink));
1380 case PROP_THROTTLE_TIME:
1381 g_value_set_uint64 (value, gst_base_sink_get_throttle_time (sink));
1384 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1391 gst_base_sink_default_get_caps (GstBaseSink * sink, GstCaps * filter)
1397 gst_base_sink_default_set_caps (GstBaseSink * sink, GstCaps * caps)
1402 /* with PREROLL_LOCK, STREAM_LOCK */
1404 gst_base_sink_commit_state (GstBaseSink * basesink)
1406 /* commit state and proceed to next pending state */
1407 GstState current, next, pending, post_pending;
1408 gboolean post_paused = FALSE;
1409 gboolean post_async_done = FALSE;
1410 gboolean post_playing = FALSE;
1411 gboolean reset_time;
1413 /* we are certainly not playing async anymore now */
1414 basesink->playing_async = FALSE;
1416 GST_OBJECT_LOCK (basesink);
1417 current = GST_STATE (basesink);
1418 next = GST_STATE_NEXT (basesink);
1419 pending = GST_STATE_PENDING (basesink);
1420 post_pending = pending;
1421 reset_time = basesink->priv->reset_time;
1422 basesink->priv->reset_time = FALSE;
1425 case GST_STATE_PLAYING:
1427 GST_DEBUG_OBJECT (basesink, "commiting state to PLAYING");
1429 basesink->need_preroll = FALSE;
1430 post_async_done = TRUE;
1431 basesink->priv->commited = TRUE;
1432 post_playing = TRUE;
1433 /* post PAUSED too when we were READY */
1434 if (current == GST_STATE_READY) {
1439 case GST_STATE_PAUSED:
1440 GST_DEBUG_OBJECT (basesink, "commiting state to PAUSED");
1442 post_async_done = TRUE;
1443 basesink->priv->commited = TRUE;
1444 post_pending = GST_STATE_VOID_PENDING;
1446 case GST_STATE_READY:
1447 case GST_STATE_NULL:
1449 case GST_STATE_VOID_PENDING:
1450 goto nothing_pending;
1455 /* we can report latency queries now */
1456 basesink->priv->have_latency = TRUE;
1458 GST_STATE (basesink) = pending;
1459 GST_STATE_NEXT (basesink) = GST_STATE_VOID_PENDING;
1460 GST_STATE_PENDING (basesink) = GST_STATE_VOID_PENDING;
1461 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_SUCCESS;
1462 GST_OBJECT_UNLOCK (basesink);
1465 GST_DEBUG_OBJECT (basesink, "posting PAUSED state change message");
1466 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1467 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1468 current, next, post_pending));
1470 if (post_async_done) {
1471 GST_DEBUG_OBJECT (basesink, "posting async-done message");
1472 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1473 gst_message_new_async_done (GST_OBJECT_CAST (basesink), reset_time));
1476 GST_DEBUG_OBJECT (basesink, "posting PLAYING state change message");
1477 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1478 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1479 next, pending, GST_STATE_VOID_PENDING));
1482 GST_STATE_BROADCAST (basesink);
1488 /* Depending on the state, set our vars. We get in this situation when the
1489 * state change function got a change to update the state vars before the
1490 * streaming thread did. This is fine but we need to make sure that we
1491 * update the need_preroll var since it was TRUE when we got here and might
1492 * become FALSE if we got to PLAYING. */
1493 GST_DEBUG_OBJECT (basesink, "nothing to commit, now in %s",
1494 gst_element_state_get_name (current));
1496 case GST_STATE_PLAYING:
1497 basesink->need_preroll = FALSE;
1499 case GST_STATE_PAUSED:
1500 basesink->need_preroll = TRUE;
1503 basesink->need_preroll = FALSE;
1504 basesink->flushing = TRUE;
1507 /* we can report latency queries now */
1508 basesink->priv->have_latency = TRUE;
1509 GST_OBJECT_UNLOCK (basesink);
1514 /* app is going to READY */
1515 GST_DEBUG_OBJECT (basesink, "stopping");
1516 basesink->need_preroll = FALSE;
1517 basesink->flushing = TRUE;
1518 GST_OBJECT_UNLOCK (basesink);
1524 start_stepping (GstBaseSink * sink, GstSegment * segment,
1525 GstStepInfo * pending, GstStepInfo * current)
1528 GstMessage *message;
1530 GST_DEBUG_OBJECT (sink, "update pending step");
1532 GST_OBJECT_LOCK (sink);
1533 memcpy (current, pending, sizeof (GstStepInfo));
1534 pending->valid = FALSE;
1535 GST_OBJECT_UNLOCK (sink);
1537 /* post message first */
1539 gst_message_new_step_start (GST_OBJECT (sink), TRUE, current->format,
1540 current->amount, current->rate, current->flush, current->intermediate);
1541 gst_message_set_seqnum (message, current->seqnum);
1542 gst_element_post_message (GST_ELEMENT (sink), message);
1544 /* get the running time of where we paused and remember it */
1545 current->start = gst_element_get_start_time (GST_ELEMENT_CAST (sink));
1546 gst_segment_set_running_time (segment, GST_FORMAT_TIME, current->start);
1548 /* set the new rate for the remainder of the segment */
1549 current->start_rate = segment->rate;
1550 segment->rate *= current->rate;
1553 if (segment->rate > 0.0)
1554 current->start_stop = segment->stop;
1556 current->start_start = segment->start;
1558 if (current->format == GST_FORMAT_TIME) {
1559 end = current->start + current->amount;
1560 if (!current->flush) {
1561 /* update the segment clipping regions for non-flushing seeks */
1562 if (segment->rate > 0.0) {
1563 segment->stop = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1564 segment->position = segment->stop;
1568 position = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1569 segment->time = position;
1570 segment->start = position;
1571 segment->position = position;
1576 GST_DEBUG_OBJECT (sink, "segment now %" GST_SEGMENT_FORMAT, segment);
1577 GST_DEBUG_OBJECT (sink, "step started at running_time %" GST_TIME_FORMAT,
1578 GST_TIME_ARGS (current->start));
1580 if (current->amount == -1) {
1581 GST_DEBUG_OBJECT (sink, "step amount == -1, stop stepping");
1582 current->valid = FALSE;
1584 GST_DEBUG_OBJECT (sink, "step amount: %" G_GUINT64_FORMAT ", format: %s, "
1585 "rate: %f", current->amount, gst_format_get_name (current->format),
1591 stop_stepping (GstBaseSink * sink, GstSegment * segment,
1592 GstStepInfo * current, gint64 rstart, gint64 rstop, gboolean eos)
1594 gint64 stop, position;
1595 GstMessage *message;
1597 GST_DEBUG_OBJECT (sink, "step complete");
1599 if (segment->rate > 0.0)
1604 GST_DEBUG_OBJECT (sink,
1605 "step stop at running_time %" GST_TIME_FORMAT, GST_TIME_ARGS (stop));
1608 current->duration = current->position;
1610 current->duration = stop - current->start;
1612 GST_DEBUG_OBJECT (sink, "step elapsed running_time %" GST_TIME_FORMAT,
1613 GST_TIME_ARGS (current->duration));
1615 position = current->start + current->duration;
1617 /* now move the segment to the new running time */
1618 gst_segment_set_running_time (segment, GST_FORMAT_TIME, position);
1620 if (current->flush) {
1621 /* and remove the time we flushed, start time did not change */
1622 segment->base = current->start;
1624 /* start time is now the stepped position */
1625 gst_element_set_start_time (GST_ELEMENT_CAST (sink), position);
1628 /* restore the previous rate */
1629 segment->rate = current->start_rate;
1631 if (segment->rate > 0.0)
1632 segment->stop = current->start_stop;
1634 segment->start = current->start_start;
1636 /* post the step done when we know the stepped duration in TIME */
1638 gst_message_new_step_done (GST_OBJECT_CAST (sink), current->format,
1639 current->amount, current->rate, current->flush, current->intermediate,
1640 current->duration, eos);
1641 gst_message_set_seqnum (message, current->seqnum);
1642 gst_element_post_message (GST_ELEMENT_CAST (sink), message);
1644 if (!current->intermediate)
1645 sink->need_preroll = current->need_preroll;
1647 /* and the current step info finished and becomes invalid */
1648 current->valid = FALSE;
1652 handle_stepping (GstBaseSink * sink, GstSegment * segment,
1653 GstStepInfo * current, guint64 * cstart, guint64 * cstop, guint64 * rstart,
1656 gboolean step_end = FALSE;
1658 /* see if we need to skip this buffer because of stepping */
1659 switch (current->format) {
1660 case GST_FORMAT_TIME:
1663 guint64 first, last;
1666 if (segment->rate > 0.0) {
1667 if (segment->stop == *cstop)
1668 *rstop = *rstart + current->amount;
1673 if (segment->start == *cstart)
1674 *rstart = *rstop + current->amount;
1680 end = current->start + current->amount;
1681 current->position = first - current->start;
1683 abs_rate = ABS (segment->rate);
1684 if (G_UNLIKELY (abs_rate != 1.0))
1685 current->position /= abs_rate;
1687 GST_DEBUG_OBJECT (sink,
1688 "buffer: %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT,
1689 GST_TIME_ARGS (first), GST_TIME_ARGS (last));
1690 GST_DEBUG_OBJECT (sink,
1691 "got time step %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT "/%"
1692 GST_TIME_FORMAT, GST_TIME_ARGS (current->position),
1693 GST_TIME_ARGS (last - current->start),
1694 GST_TIME_ARGS (current->amount));
1696 if ((current->flush && current->position >= current->amount)
1698 GST_DEBUG_OBJECT (sink, "step ended, we need clipping");
1700 if (segment->rate > 0.0) {
1702 *cstart = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1705 *cstop = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1708 GST_DEBUG_OBJECT (sink,
1709 "cstart %" GST_TIME_FORMAT ", rstart %" GST_TIME_FORMAT,
1710 GST_TIME_ARGS (*cstart), GST_TIME_ARGS (*rstart));
1711 GST_DEBUG_OBJECT (sink,
1712 "cstop %" GST_TIME_FORMAT ", rstop %" GST_TIME_FORMAT,
1713 GST_TIME_ARGS (*cstop), GST_TIME_ARGS (*rstop));
1716 case GST_FORMAT_BUFFERS:
1717 GST_DEBUG_OBJECT (sink,
1718 "got default step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1719 current->position, current->amount);
1721 if (current->position < current->amount) {
1722 current->position++;
1727 case GST_FORMAT_DEFAULT:
1729 GST_DEBUG_OBJECT (sink,
1730 "got unknown step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1731 current->position, current->amount);
1737 /* with STREAM_LOCK, PREROLL_LOCK
1739 * Returns TRUE if the object needs synchronisation and takes therefore
1740 * part in prerolling.
1742 * rsstart/rsstop contain the start/stop in stream time.
1743 * rrstart/rrstop contain the start/stop in running time.
1746 gst_base_sink_get_sync_times (GstBaseSink * basesink, GstMiniObject * obj,
1747 GstClockTime * rsstart, GstClockTime * rsstop,
1748 GstClockTime * rrstart, GstClockTime * rrstop, gboolean * do_sync,
1749 gboolean * stepped, GstStepInfo * step, gboolean * step_end)
1751 GstBaseSinkClass *bclass;
1753 GstClockTime start, stop; /* raw start/stop timestamps */
1754 guint64 cstart, cstop; /* clipped raw timestamps */
1755 guint64 rstart, rstop; /* clipped timestamps converted to running time */
1756 GstClockTime sstart, sstop; /* clipped timestamps converted to stream time */
1758 GstBaseSinkPrivate *priv;
1759 GstSegment *segment;
1762 priv = basesink->priv;
1763 segment = &basesink->segment;
1765 /* start with nothing */
1766 start = stop = GST_CLOCK_TIME_NONE;
1768 if (G_UNLIKELY (GST_IS_EVENT (obj))) {
1769 GstEvent *event = GST_EVENT_CAST (obj);
1771 switch (GST_EVENT_TYPE (event)) {
1772 /* EOS event needs syncing */
1775 if (segment->rate >= 0.0) {
1776 sstart = sstop = priv->current_sstop;
1777 if (!GST_CLOCK_TIME_IS_VALID (sstart)) {
1778 /* we have not seen a buffer yet, use the segment values */
1779 sstart = sstop = gst_segment_to_stream_time (segment,
1780 segment->format, segment->stop);
1783 sstart = sstop = priv->current_sstart;
1784 if (!GST_CLOCK_TIME_IS_VALID (sstart)) {
1785 /* we have not seen a buffer yet, use the segment values */
1786 sstart = sstop = gst_segment_to_stream_time (segment,
1787 segment->format, segment->start);
1791 rstart = rstop = priv->eos_rtime;
1792 *do_sync = rstart != -1;
1793 GST_DEBUG_OBJECT (basesink, "sync times for EOS %" GST_TIME_FORMAT,
1794 GST_TIME_ARGS (rstart));
1795 /* if we are stepping, we end now */
1796 *step_end = step->valid;
1801 /* other events do not need syncing */
1809 /* else do buffer sync code */
1810 buffer = GST_BUFFER_CAST (obj);
1812 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1814 /* just get the times to see if we need syncing, if the start returns -1 we
1816 if (bclass->get_times)
1817 bclass->get_times (basesink, buffer, &start, &stop);
1819 if (!GST_CLOCK_TIME_IS_VALID (start)) {
1820 /* we don't need to sync but we still want to get the timestamps for
1821 * tracking the position */
1822 gst_base_sink_default_get_times (basesink, buffer, &start, &stop);
1828 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
1829 ", stop: %" GST_TIME_FORMAT ", do_sync %d", GST_TIME_ARGS (start),
1830 GST_TIME_ARGS (stop), *do_sync);
1832 /* collect segment and format for code clarity */
1833 format = segment->format;
1836 if (G_UNLIKELY (!gst_segment_clip (segment, format,
1837 start, stop, &cstart, &cstop))) {
1839 GST_DEBUG_OBJECT (basesink, "step out of segment");
1840 /* when we are stepping, pretend we're at the end of the segment */
1841 if (segment->rate > 0.0) {
1842 cstart = segment->stop;
1843 cstop = segment->stop;
1845 cstart = segment->start;
1846 cstop = segment->start;
1850 goto out_of_segment;
1853 if (G_UNLIKELY (start != cstart || stop != cstop)) {
1854 GST_DEBUG_OBJECT (basesink, "clipped to: start %" GST_TIME_FORMAT
1855 ", stop: %" GST_TIME_FORMAT, GST_TIME_ARGS (cstart),
1856 GST_TIME_ARGS (cstop));
1859 /* set last stop position */
1860 if (G_LIKELY (stop != GST_CLOCK_TIME_NONE && cstop != GST_CLOCK_TIME_NONE))
1861 segment->position = cstop;
1863 segment->position = cstart;
1866 rstart = gst_segment_to_running_time (segment, format, cstart);
1867 rstop = gst_segment_to_running_time (segment, format, cstop);
1869 if (G_UNLIKELY (step->valid)) {
1870 if (!(*step_end = handle_stepping (basesink, segment, step, &cstart, &cstop,
1871 &rstart, &rstop))) {
1872 /* step is still busy, we discard data when we are flushing */
1873 *stepped = step->flush;
1874 GST_DEBUG_OBJECT (basesink, "stepping busy");
1877 /* this can produce wrong values if we accumulated non-TIME segments. If this happens,
1878 * upstream is behaving very badly */
1879 sstart = gst_segment_to_stream_time (segment, format, cstart);
1880 sstop = gst_segment_to_stream_time (segment, format, cstop);
1883 /* eos_done label only called when doing EOS, we also stop stepping then */
1884 if (*step_end && step->flush) {
1885 GST_DEBUG_OBJECT (basesink, "flushing step ended");
1886 stop_stepping (basesink, segment, step, rstart, rstop, eos);
1888 /* re-determine running start times for adjusted segment
1889 * (which has a flushed amount of running/accumulated time removed) */
1890 if (!GST_IS_EVENT (obj)) {
1891 GST_DEBUG_OBJECT (basesink, "refresh sync times");
1902 /* buffers and EOS always need syncing and preroll */
1908 /* we usually clip in the chain function already but stepping could cause
1909 * the segment to be updated later. we return FALSE so that we don't try
1911 GST_LOG_OBJECT (basesink, "buffer skipped, not in segment");
1916 /* with STREAM_LOCK, PREROLL_LOCK, LOCK
1917 * adjust a timestamp with the latency and timestamp offset. This function does
1918 * not adjust for the render delay. */
1920 gst_base_sink_adjust_time (GstBaseSink * basesink, GstClockTime time)
1922 GstClockTimeDiff ts_offset;
1924 /* don't do anything funny with invalid timestamps */
1925 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
1928 time += basesink->priv->latency;
1930 /* apply offset, be carefull for underflows */
1931 ts_offset = basesink->priv->ts_offset;
1932 if (ts_offset < 0) {
1933 ts_offset = -ts_offset;
1934 if (ts_offset < time)
1941 /* subtract the render delay again, which was included in the latency */
1942 if (time > basesink->priv->render_delay)
1943 time -= basesink->priv->render_delay;
1951 * gst_base_sink_wait_clock:
1953 * @time: the running_time to be reached
1954 * @jitter: (out) (allow-none): the jitter to be filled with time diff, or NULL
1956 * This function will block until @time is reached. It is usually called by
1957 * subclasses that use their own internal synchronisation.
1959 * If @time is not valid, no sycnhronisation is done and #GST_CLOCK_BADTIME is
1960 * returned. Likewise, if synchronisation is disabled in the element or there
1961 * is no clock, no synchronisation is done and #GST_CLOCK_BADTIME is returned.
1963 * This function should only be called with the PREROLL_LOCK held, like when
1964 * receiving an EOS event in the #GstBaseSinkClass.event() vmethod or when
1965 * receiving a buffer in
1966 * the #GstBaseSinkClass.render() vmethod.
1968 * The @time argument should be the running_time of when this method should
1969 * return and is not adjusted with any latency or offset configured in the
1974 * Returns: #GstClockReturn
1977 gst_base_sink_wait_clock (GstBaseSink * sink, GstClockTime time,
1978 GstClockTimeDiff * jitter)
1982 GstClockTime base_time;
1984 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
1987 GST_OBJECT_LOCK (sink);
1988 if (G_UNLIKELY (!sink->sync))
1991 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (sink)) == NULL))
1994 base_time = GST_ELEMENT_CAST (sink)->base_time;
1995 GST_LOG_OBJECT (sink,
1996 "time %" GST_TIME_FORMAT ", base_time %" GST_TIME_FORMAT,
1997 GST_TIME_ARGS (time), GST_TIME_ARGS (base_time));
1999 /* add base_time to running_time to get the time against the clock */
2002 /* Re-use existing clockid if available */
2003 /* FIXME: Casting to GstClockEntry only works because the types
2005 if (G_LIKELY (sink->priv->cached_clock_id != NULL
2006 && GST_CLOCK_ENTRY_CLOCK ((GstClockEntry *) sink->priv->
2007 cached_clock_id) == clock)) {
2008 if (!gst_clock_single_shot_id_reinit (clock, sink->priv->cached_clock_id,
2010 gst_clock_id_unref (sink->priv->cached_clock_id);
2011 sink->priv->cached_clock_id = gst_clock_new_single_shot_id (clock, time);
2014 if (sink->priv->cached_clock_id != NULL)
2015 gst_clock_id_unref (sink->priv->cached_clock_id);
2016 sink->priv->cached_clock_id = gst_clock_new_single_shot_id (clock, time);
2018 GST_OBJECT_UNLOCK (sink);
2020 /* A blocking wait is performed on the clock. We save the ClockID
2021 * so we can unlock the entry at any time. While we are blocking, we
2022 * release the PREROLL_LOCK so that other threads can interrupt the
2024 sink->clock_id = sink->priv->cached_clock_id;
2025 /* release the preroll lock while waiting */
2026 GST_BASE_SINK_PREROLL_UNLOCK (sink);
2028 ret = gst_clock_id_wait (sink->priv->cached_clock_id, jitter);
2030 GST_BASE_SINK_PREROLL_LOCK (sink);
2031 sink->clock_id = NULL;
2035 /* no syncing needed */
2038 GST_DEBUG_OBJECT (sink, "time not valid, no sync needed");
2039 return GST_CLOCK_BADTIME;
2043 GST_DEBUG_OBJECT (sink, "sync disabled");
2044 GST_OBJECT_UNLOCK (sink);
2045 return GST_CLOCK_BADTIME;
2049 GST_DEBUG_OBJECT (sink, "no clock, can't sync");
2050 GST_OBJECT_UNLOCK (sink);
2051 return GST_CLOCK_BADTIME;
2056 * gst_base_sink_wait_preroll:
2059 * If the #GstBaseSinkClass.render() method performs its own synchronisation
2060 * against the clock it must unblock when going from PLAYING to the PAUSED state
2061 * and call this method before continuing to render the remaining data.
2063 * This function will block until a state change to PLAYING happens (in which
2064 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
2065 * to a state change to READY or a FLUSH event (in which case this function
2066 * returns #GST_FLOW_FLUSHING).
2068 * This function should only be called with the PREROLL_LOCK held, like in the
2071 * Returns: #GST_FLOW_OK if the preroll completed and processing can
2072 * continue. Any other return value should be returned from the render vmethod.
2077 gst_base_sink_wait_preroll (GstBaseSink * sink)
2079 sink->have_preroll = TRUE;
2080 GST_DEBUG_OBJECT (sink, "waiting in preroll for flush or PLAYING");
2081 /* block until the state changes, or we get a flush, or something */
2082 GST_BASE_SINK_PREROLL_WAIT (sink);
2083 sink->have_preroll = FALSE;
2084 if (G_UNLIKELY (sink->flushing))
2086 if (G_UNLIKELY (sink->priv->step_unlock))
2088 GST_DEBUG_OBJECT (sink, "continue after preroll");
2095 GST_DEBUG_OBJECT (sink, "preroll interrupted because of flush");
2096 return GST_FLOW_FLUSHING;
2100 sink->priv->step_unlock = FALSE;
2101 GST_DEBUG_OBJECT (sink, "preroll interrupted because of step");
2102 return GST_FLOW_STEP;
2107 * gst_base_sink_do_preroll:
2109 * @obj: (transfer none): the mini object that caused the preroll
2111 * If the @sink spawns its own thread for pulling buffers from upstream it
2112 * should call this method after it has pulled a buffer. If the element needed
2113 * to preroll, this function will perform the preroll and will then block
2114 * until the element state is changed.
2116 * This function should be called with the PREROLL_LOCK held.
2118 * Returns: #GST_FLOW_OK if the preroll completed and processing can
2119 * continue. Any other return value should be returned from the render vmethod.
2124 gst_base_sink_do_preroll (GstBaseSink * sink, GstMiniObject * obj)
2128 while (G_UNLIKELY (sink->need_preroll)) {
2129 GST_DEBUG_OBJECT (sink, "prerolling object %p", obj);
2131 /* if it's a buffer, we need to call the preroll method */
2132 if (sink->priv->call_preroll) {
2133 GstBaseSinkClass *bclass;
2136 if (GST_IS_BUFFER_LIST (obj)) {
2137 buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0);
2138 g_assert (NULL != buf);
2139 } else if (GST_IS_BUFFER (obj)) {
2140 buf = GST_BUFFER_CAST (obj);
2141 /* For buffer lists do not set last buffer for now */
2142 gst_base_sink_set_last_buffer (sink, buf);
2147 GST_DEBUG_OBJECT (sink, "preroll buffer %" GST_TIME_FORMAT,
2148 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
2150 bclass = GST_BASE_SINK_GET_CLASS (sink);
2151 if (bclass->preroll)
2152 if ((ret = bclass->preroll (sink, buf)) != GST_FLOW_OK)
2153 goto preroll_canceled;
2155 sink->priv->call_preroll = FALSE;
2160 if (G_LIKELY (sink->playing_async)) {
2161 if (G_UNLIKELY (!gst_base_sink_commit_state (sink)))
2165 /* need to recheck here because the commit state could have
2166 * made us not need the preroll anymore */
2167 if (G_LIKELY (sink->need_preroll)) {
2168 /* block until the state changes, or we get a flush, or something */
2169 ret = gst_base_sink_wait_preroll (sink);
2170 if ((ret != GST_FLOW_OK) && (ret != GST_FLOW_STEP))
2171 goto preroll_failed;
2179 GST_DEBUG_OBJECT (sink, "preroll failed, abort state");
2180 gst_element_abort_state (GST_ELEMENT_CAST (sink));
2185 GST_DEBUG_OBJECT (sink, "stopping while commiting state");
2186 return GST_FLOW_FLUSHING;
2190 GST_DEBUG_OBJECT (sink, "preroll failed: %s", gst_flow_get_name (ret));
2196 * gst_base_sink_wait_eos:
2198 * @time: the running_time to be reached
2199 * @jitter: (out) (allow-none): the jitter to be filled with time diff, or NULL
2201 * This function will block until @time is reached. It is usually called by
2202 * subclasses that use their own internal synchronisation but want to let the
2203 * EOS be handled by the base class.
2205 * This function should only be called with the PREROLL_LOCK held, like when
2206 * receiving an EOS event in the ::event vmethod.
2208 * The @time argument should be the running_time of when the EOS should happen
2209 * and will be adjusted with any latency and offset configured in the sink.
2211 * Returns: #GstFlowReturn
2216 gst_base_sink_wait_eos (GstBaseSink * sink, GstClockTime time,
2217 GstClockTimeDiff * jitter)
2219 GstClockReturn status;
2225 GST_DEBUG_OBJECT (sink, "checking preroll");
2227 /* first wait for the playing state before we can continue */
2228 while (G_UNLIKELY (sink->need_preroll)) {
2229 ret = gst_base_sink_wait_preroll (sink);
2230 if ((ret != GST_FLOW_OK) && (ret != GST_FLOW_STEP))
2234 /* preroll done, we can sync since we are in PLAYING now. */
2235 GST_DEBUG_OBJECT (sink, "possibly waiting for clock to reach %"
2236 GST_TIME_FORMAT, GST_TIME_ARGS (time));
2238 /* compensate for latency, ts_offset and render delay */
2239 stime = gst_base_sink_adjust_time (sink, time);
2241 /* wait for the clock, this can be interrupted because we got shut down or
2243 status = gst_base_sink_wait_clock (sink, stime, jitter);
2245 GST_DEBUG_OBJECT (sink, "clock returned %d", status);
2247 /* invalid time, no clock or sync disabled, just continue then */
2248 if (status == GST_CLOCK_BADTIME)
2251 /* waiting could have been interrupted and we can be flushing now */
2252 if (G_UNLIKELY (sink->flushing))
2255 /* retry if we got unscheduled, which means we did not reach the timeout
2256 * yet. if some other error occures, we continue. */
2257 } while (status == GST_CLOCK_UNSCHEDULED);
2259 GST_DEBUG_OBJECT (sink, "end of stream");
2266 GST_DEBUG_OBJECT (sink, "we are flushing");
2267 return GST_FLOW_FLUSHING;
2271 /* with STREAM_LOCK, PREROLL_LOCK
2273 * Make sure we are in PLAYING and synchronize an object to the clock.
2275 * If we need preroll, we are not in PLAYING. We try to commit the state
2276 * if needed and then block if we still are not PLAYING.
2278 * We start waiting on the clock in PLAYING. If we got interrupted, we
2279 * immediately try to re-preroll.
2281 * Some objects do not need synchronisation (most events) and so this function
2282 * immediately returns GST_FLOW_OK.
2284 * for objects that arrive later than max-lateness to be synchronized to the
2285 * clock have the @late boolean set to TRUE.
2287 * This function keeps a running average of the jitter (the diff between the
2288 * clock time and the requested sync time). The jitter is negative for
2289 * objects that arrive in time and positive for late buffers.
2291 * does not take ownership of obj.
2293 static GstFlowReturn
2294 gst_base_sink_do_sync (GstBaseSink * basesink,
2295 GstMiniObject * obj, gboolean * late, gboolean * step_end)
2297 GstClockTimeDiff jitter = 0;
2299 GstClockReturn status = GST_CLOCK_OK;
2300 GstClockTime rstart, rstop, sstart, sstop, stime;
2302 GstBaseSinkPrivate *priv;
2304 GstStepInfo *current, *pending;
2307 priv = basesink->priv;
2310 sstart = sstop = rstart = rstop = GST_CLOCK_TIME_NONE;
2314 priv->current_rstart = GST_CLOCK_TIME_NONE;
2316 /* get stepping info */
2317 current = &priv->current_step;
2318 pending = &priv->pending_step;
2320 /* get timing information for this object against the render segment */
2321 syncable = gst_base_sink_get_sync_times (basesink, obj,
2322 &sstart, &sstop, &rstart, &rstop, &do_sync, &stepped, current, step_end);
2324 if (G_UNLIKELY (stepped))
2327 /* a syncable object needs to participate in preroll and
2328 * clocking. All buffers and EOS are syncable. */
2329 if (G_UNLIKELY (!syncable))
2332 /* store timing info for current object */
2333 priv->current_rstart = rstart;
2334 priv->current_rstop = (GST_CLOCK_TIME_IS_VALID (rstop) ? rstop : rstart);
2336 /* save sync time for eos when the previous object needed sync */
2337 priv->eos_rtime = (do_sync ? priv->current_rstop : GST_CLOCK_TIME_NONE);
2339 /* calculate inter frame spacing */
2340 if (G_UNLIKELY (priv->prev_rstart != -1 && priv->prev_rstart < rstart)) {
2341 GstClockTime in_diff;
2343 in_diff = rstart - priv->prev_rstart;
2345 if (priv->avg_in_diff == -1)
2346 priv->avg_in_diff = in_diff;
2348 priv->avg_in_diff = UPDATE_RUNNING_AVG (priv->avg_in_diff, in_diff);
2350 GST_LOG_OBJECT (basesink, "avg frame diff %" GST_TIME_FORMAT,
2351 GST_TIME_ARGS (priv->avg_in_diff));
2354 priv->prev_rstart = rstart;
2356 if (G_UNLIKELY (priv->earliest_in_time != -1
2357 && rstart < priv->earliest_in_time))
2361 /* first do preroll, this makes sure we commit our state
2362 * to PAUSED and can continue to PLAYING. We cannot perform
2363 * any clock sync in PAUSED because there is no clock. */
2364 ret = gst_base_sink_do_preroll (basesink, obj);
2365 if (G_UNLIKELY (ret != GST_FLOW_OK))
2366 goto preroll_failed;
2368 /* update the segment with a pending step if the current one is invalid and we
2369 * have a new pending one. We only accept new step updates after a preroll */
2370 if (G_UNLIKELY (pending->valid && !current->valid)) {
2371 start_stepping (basesink, &basesink->segment, pending, current);
2375 /* After rendering we store the position of the last buffer so that we can use
2376 * it to report the position. We need to take the lock here. */
2377 GST_OBJECT_LOCK (basesink);
2378 priv->current_sstart = sstart;
2379 priv->current_sstop = (GST_CLOCK_TIME_IS_VALID (sstop) ? sstop : sstart);
2380 GST_OBJECT_UNLOCK (basesink);
2385 /* adjust for latency */
2386 stime = gst_base_sink_adjust_time (basesink, rstart);
2388 /* preroll done, we can sync since we are in PLAYING now. */
2389 GST_DEBUG_OBJECT (basesink, "possibly waiting for clock to reach %"
2390 GST_TIME_FORMAT ", adjusted %" GST_TIME_FORMAT,
2391 GST_TIME_ARGS (rstart), GST_TIME_ARGS (stime));
2393 /* This function will return immediately if start == -1, no clock
2394 * or sync is disabled with GST_CLOCK_BADTIME. */
2395 status = gst_base_sink_wait_clock (basesink, stime, &jitter);
2397 GST_DEBUG_OBJECT (basesink, "clock returned %d, jitter %c%" GST_TIME_FORMAT,
2398 status, (jitter < 0 ? '-' : ' '), GST_TIME_ARGS (ABS (jitter)));
2400 /* invalid time, no clock or sync disabled, just render */
2401 if (status == GST_CLOCK_BADTIME)
2404 /* waiting could have been interrupted and we can be flushing now */
2405 if (G_UNLIKELY (basesink->flushing))
2408 /* check for unlocked by a state change, we are not flushing so
2409 * we can try to preroll on the current buffer. */
2410 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
2411 GST_DEBUG_OBJECT (basesink, "unscheduled, waiting some more");
2412 priv->call_preroll = TRUE;
2416 /* successful syncing done, record observation */
2417 priv->current_jitter = jitter;
2419 /* check if the object should be dropped */
2420 *late = gst_base_sink_is_too_late (basesink, obj, rstart, rstop,
2429 GST_DEBUG_OBJECT (basesink, "skipped stepped object %p", obj);
2435 GST_DEBUG_OBJECT (basesink, "non syncable object %p", obj);
2440 GST_DEBUG_OBJECT (basesink, "dropped because of QoS %p", obj);
2446 GST_DEBUG_OBJECT (basesink, "we are flushing");
2447 return GST_FLOW_FLUSHING;
2451 GST_DEBUG_OBJECT (basesink, "preroll failed");
2458 gst_base_sink_send_qos (GstBaseSink * basesink, GstQOSType type,
2459 gdouble proportion, GstClockTime time, GstClockTimeDiff diff)
2464 /* generate Quality-of-Service event */
2465 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2466 "qos: type %d, proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2467 GST_TIME_FORMAT, type, proportion, diff, GST_TIME_ARGS (time));
2469 event = gst_event_new_qos (type, proportion, diff, time);
2472 res = gst_pad_push_event (basesink->sinkpad, event);
2478 gst_base_sink_perform_qos (GstBaseSink * sink, gboolean dropped)
2480 GstBaseSinkPrivate *priv;
2481 GstClockTime start, stop;
2482 GstClockTimeDiff jitter;
2483 GstClockTime pt, entered, left;
2484 GstClockTime duration;
2489 start = priv->current_rstart;
2491 if (priv->current_step.valid)
2494 /* if Quality-of-Service disabled, do nothing */
2495 if (!g_atomic_int_get (&priv->qos_enabled) ||
2496 !GST_CLOCK_TIME_IS_VALID (start))
2499 stop = priv->current_rstop;
2500 jitter = priv->current_jitter;
2503 /* this is the time the buffer entered the sink */
2504 if (start < -jitter)
2507 entered = start + jitter;
2510 /* this is the time the buffer entered the sink */
2511 entered = start + jitter;
2512 /* this is the time the buffer left the sink */
2513 left = start + jitter;
2516 /* calculate duration of the buffer */
2517 if (GST_CLOCK_TIME_IS_VALID (stop) && stop != start)
2518 duration = stop - start;
2520 duration = priv->avg_in_diff;
2522 /* if we have the time when the last buffer left us, calculate
2523 * processing time */
2524 if (GST_CLOCK_TIME_IS_VALID (priv->last_left)) {
2525 if (entered > priv->last_left) {
2526 pt = entered - priv->last_left;
2534 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "start: %" GST_TIME_FORMAT
2535 ", stop %" GST_TIME_FORMAT ", entered %" GST_TIME_FORMAT ", left %"
2536 GST_TIME_FORMAT ", pt: %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT
2537 ",jitter %" G_GINT64_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (stop),
2538 GST_TIME_ARGS (entered), GST_TIME_ARGS (left), GST_TIME_ARGS (pt),
2539 GST_TIME_ARGS (duration), jitter);
2541 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "avg_duration: %" GST_TIME_FORMAT
2542 ", avg_pt: %" GST_TIME_FORMAT ", avg_rate: %g",
2543 GST_TIME_ARGS (priv->avg_duration), GST_TIME_ARGS (priv->avg_pt),
2546 /* collect running averages. for first observations, we copy the
2548 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_duration))
2549 priv->avg_duration = duration;
2551 priv->avg_duration = UPDATE_RUNNING_AVG (priv->avg_duration, duration);
2553 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_pt))
2556 priv->avg_pt = UPDATE_RUNNING_AVG (priv->avg_pt, pt);
2558 if (priv->avg_duration != 0)
2560 gst_guint64_to_gdouble (priv->avg_pt) /
2561 gst_guint64_to_gdouble (priv->avg_duration);
2565 if (GST_CLOCK_TIME_IS_VALID (priv->last_left)) {
2566 if (dropped || priv->avg_rate < 0.0) {
2567 priv->avg_rate = rate;
2570 priv->avg_rate = UPDATE_RUNNING_AVG_N (priv->avg_rate, rate);
2572 priv->avg_rate = UPDATE_RUNNING_AVG_P (priv->avg_rate, rate);
2576 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink,
2577 "updated: avg_duration: %" GST_TIME_FORMAT ", avg_pt: %" GST_TIME_FORMAT
2578 ", avg_rate: %g", GST_TIME_ARGS (priv->avg_duration),
2579 GST_TIME_ARGS (priv->avg_pt), priv->avg_rate);
2582 if (priv->avg_rate >= 0.0) {
2584 GstClockTimeDiff diff;
2586 /* if we have a valid rate, start sending QoS messages */
2587 if (priv->current_jitter < 0) {
2588 /* make sure we never go below 0 when adding the jitter to the
2590 if (priv->current_rstart < -priv->current_jitter)
2591 priv->current_jitter = -priv->current_rstart;
2594 if (priv->throttle_time > 0) {
2595 diff = priv->throttle_time;
2596 type = GST_QOS_TYPE_THROTTLE;
2598 diff = priv->current_jitter;
2600 type = GST_QOS_TYPE_OVERFLOW;
2602 type = GST_QOS_TYPE_UNDERFLOW;
2605 gst_base_sink_send_qos (sink, type, priv->avg_rate, priv->current_rstart,
2609 /* record when this buffer will leave us */
2610 priv->last_left = left;
2613 /* reset all qos measuring */
2615 gst_base_sink_reset_qos (GstBaseSink * sink)
2617 GstBaseSinkPrivate *priv;
2621 priv->last_render_time = GST_CLOCK_TIME_NONE;
2622 priv->prev_rstart = GST_CLOCK_TIME_NONE;
2623 priv->earliest_in_time = GST_CLOCK_TIME_NONE;
2624 priv->last_left = GST_CLOCK_TIME_NONE;
2625 priv->avg_duration = GST_CLOCK_TIME_NONE;
2626 priv->avg_pt = GST_CLOCK_TIME_NONE;
2627 priv->avg_rate = -1.0;
2628 priv->avg_render = GST_CLOCK_TIME_NONE;
2629 priv->avg_in_diff = GST_CLOCK_TIME_NONE;
2635 /* Checks if the object was scheduled too late.
2637 * rstart/rstop contain the running_time start and stop values
2640 * status and jitter contain the return values from the clock wait.
2642 * returns TRUE if the buffer was too late.
2645 gst_base_sink_is_too_late (GstBaseSink * basesink, GstMiniObject * obj,
2646 GstClockTime rstart, GstClockTime rstop,
2647 GstClockReturn status, GstClockTimeDiff jitter)
2650 guint64 max_lateness;
2651 GstBaseSinkPrivate *priv;
2653 priv = basesink->priv;
2657 /* only for objects that were too late */
2658 if (G_LIKELY (status != GST_CLOCK_EARLY))
2661 max_lateness = basesink->max_lateness;
2663 /* check if frame dropping is enabled */
2664 if (max_lateness == -1)
2667 /* only check for buffers */
2668 if (G_UNLIKELY (!GST_IS_BUFFER (obj)))
2671 /* can't do check if we don't have a timestamp */
2672 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (rstart)))
2675 /* we can add a valid stop time */
2676 if (GST_CLOCK_TIME_IS_VALID (rstop))
2677 max_lateness += rstop;
2679 max_lateness += rstart;
2680 /* no stop time, use avg frame diff */
2681 if (priv->avg_in_diff != -1)
2682 max_lateness += priv->avg_in_diff;
2685 /* if the jitter bigger than duration and lateness we are too late */
2686 if ((late = rstart + jitter > max_lateness)) {
2687 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, basesink,
2688 "buffer is too late %" GST_TIME_FORMAT
2689 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (rstart + jitter),
2690 GST_TIME_ARGS (max_lateness));
2691 /* !!emergency!!, if we did not receive anything valid for more than a
2692 * second, render it anyway so the user sees something */
2693 if (GST_CLOCK_TIME_IS_VALID (priv->last_render_time) &&
2694 rstart - priv->last_render_time > GST_SECOND) {
2696 GST_ELEMENT_WARNING (basesink, CORE, CLOCK,
2697 (_("A lot of buffers are being dropped.")),
2698 ("There may be a timestamping problem, or this computer is too slow."));
2699 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, basesink,
2700 "**emergency** last buffer at %" GST_TIME_FORMAT " > GST_SECOND",
2701 GST_TIME_ARGS (priv->last_render_time));
2706 if (!late || !GST_CLOCK_TIME_IS_VALID (priv->last_render_time)) {
2707 priv->last_render_time = rstart;
2708 /* the next allowed input timestamp */
2709 if (priv->throttle_time > 0)
2710 priv->earliest_in_time = rstart + priv->throttle_time;
2717 GST_DEBUG_OBJECT (basesink, "object was scheduled in time");
2722 GST_DEBUG_OBJECT (basesink, "frame dropping disabled");
2727 GST_DEBUG_OBJECT (basesink, "object is not a buffer");
2732 GST_DEBUG_OBJECT (basesink, "buffer has no timestamp");
2737 /* called before and after calling the render vmethod. It keeps track of how
2738 * much time was spent in the render method and is used to check if we are
2741 gst_base_sink_do_render_stats (GstBaseSink * basesink, gboolean start)
2743 GstBaseSinkPrivate *priv;
2745 priv = basesink->priv;
2748 priv->start = gst_util_get_timestamp ();
2750 GstClockTime elapsed;
2752 priv->stop = gst_util_get_timestamp ();
2754 elapsed = GST_CLOCK_DIFF (priv->start, priv->stop);
2756 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_render))
2757 priv->avg_render = elapsed;
2759 priv->avg_render = UPDATE_RUNNING_AVG (priv->avg_render, elapsed);
2761 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2762 "avg_render: %" GST_TIME_FORMAT, GST_TIME_ARGS (priv->avg_render));
2767 gst_base_sink_flush_start (GstBaseSink * basesink, GstPad * pad)
2769 /* make sure we are not blocked on the clock also clear any pending
2771 gst_base_sink_set_flushing (basesink, pad, TRUE);
2773 /* we grab the stream lock but that is not needed since setting the
2774 * sink to flushing would make sure no state commit is being done
2776 GST_PAD_STREAM_LOCK (pad);
2777 gst_base_sink_reset_qos (basesink);
2778 /* and we need to commit our state again on the next
2779 * prerolled buffer */
2780 basesink->playing_async = TRUE;
2781 if (basesink->priv->async_enabled) {
2782 gst_element_lost_state (GST_ELEMENT_CAST (basesink));
2784 /* start time reset in above case as well;
2785 * arranges for a.o. proper position reporting when flushing in PAUSED */
2786 gst_element_set_start_time (GST_ELEMENT_CAST (basesink), 0);
2787 basesink->priv->have_latency = TRUE;
2789 gst_base_sink_set_last_buffer (basesink, NULL);
2790 GST_PAD_STREAM_UNLOCK (pad);
2794 gst_base_sink_flush_stop (GstBaseSink * basesink, GstPad * pad,
2795 gboolean reset_time)
2797 /* unset flushing so we can accept new data, this also flushes out any EOS
2799 gst_base_sink_set_flushing (basesink, pad, FALSE);
2801 /* for position reporting */
2802 GST_OBJECT_LOCK (basesink);
2803 basesink->priv->current_sstart = GST_CLOCK_TIME_NONE;
2804 basesink->priv->current_sstop = GST_CLOCK_TIME_NONE;
2805 basesink->priv->eos_rtime = GST_CLOCK_TIME_NONE;
2806 basesink->priv->call_preroll = TRUE;
2807 basesink->priv->current_step.valid = FALSE;
2808 basesink->priv->pending_step.valid = FALSE;
2809 if (basesink->pad_mode == GST_PAD_MODE_PUSH) {
2810 /* we need new segment info after the flush. */
2811 basesink->have_newsegment = FALSE;
2813 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
2816 basesink->priv->reset_time = reset_time;
2817 GST_OBJECT_UNLOCK (basesink);
2820 static GstFlowReturn
2821 gst_base_sink_default_wait_eos (GstBaseSink * basesink, GstEvent * event)
2824 gboolean late, step_end;
2826 ret = gst_base_sink_do_sync (basesink, GST_MINI_OBJECT_CAST (event),
2833 gst_base_sink_default_event (GstBaseSink * basesink, GstEvent * event)
2835 gboolean result = TRUE;
2836 GstBaseSinkClass *bclass;
2838 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2840 switch (GST_EVENT_TYPE (event)) {
2841 case GST_EVENT_FLUSH_START:
2843 GST_DEBUG_OBJECT (basesink, "flush-start %p", event);
2844 gst_base_sink_flush_start (basesink, basesink->sinkpad);
2847 case GST_EVENT_FLUSH_STOP:
2849 gboolean reset_time;
2851 gst_event_parse_flush_stop (event, &reset_time);
2852 GST_DEBUG_OBJECT (basesink, "flush-stop %p, reset_time: %d", event,
2854 gst_base_sink_flush_stop (basesink, basesink->sinkpad, reset_time);
2859 GstMessage *message;
2862 /* we set the received EOS flag here so that we can use it when testing if
2863 * we are prerolled and to refuse more buffers. */
2864 basesink->priv->received_eos = TRUE;
2867 if (G_LIKELY (bclass->wait_eos)) {
2870 ret = bclass->wait_eos (basesink, event);
2871 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2877 /* the EOS event is completely handled so we mark
2878 * ourselves as being in the EOS state. eos is also
2879 * protected by the object lock so we can read it when
2880 * answering the POSITION query. */
2881 GST_OBJECT_LOCK (basesink);
2882 basesink->eos = TRUE;
2883 GST_OBJECT_UNLOCK (basesink);
2885 /* ok, now we can post the message */
2886 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
2888 seqnum = basesink->priv->seqnum = gst_event_get_seqnum (event);
2889 GST_DEBUG_OBJECT (basesink, "Got seqnum #%" G_GUINT32_FORMAT, seqnum);
2891 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
2892 gst_message_set_seqnum (message, seqnum);
2893 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
2896 case GST_EVENT_CAPS:
2900 GST_DEBUG_OBJECT (basesink, "caps %p", event);
2902 gst_event_parse_caps (event, &caps);
2903 if (bclass->set_caps)
2904 result = bclass->set_caps (basesink, caps);
2907 GST_OBJECT_LOCK (basesink);
2908 gst_caps_replace (&basesink->priv->caps, caps);
2909 GST_OBJECT_UNLOCK (basesink);
2913 case GST_EVENT_SEGMENT:
2914 /* configure the segment */
2915 /* The segment is protected with both the STREAM_LOCK and the OBJECT_LOCK.
2916 * We protect with the OBJECT_LOCK so that we can use the values to
2917 * safely answer a POSITION query. */
2918 GST_OBJECT_LOCK (basesink);
2919 /* the newsegment event is needed to bring the buffer timestamps to the
2920 * stream time and to drop samples outside of the playback segment. */
2921 gst_event_copy_segment (event, &basesink->segment);
2922 GST_DEBUG_OBJECT (basesink, "configured SEGMENT %" GST_SEGMENT_FORMAT,
2923 &basesink->segment);
2924 basesink->have_newsegment = TRUE;
2925 GST_OBJECT_UNLOCK (basesink);
2929 GstTagList *taglist;
2931 gst_event_parse_tag (event, &taglist);
2933 gst_element_post_message (GST_ELEMENT_CAST (basesink),
2934 gst_message_new_tag (GST_OBJECT_CAST (basesink),
2935 gst_tag_list_copy (taglist)));
2938 case GST_EVENT_SINK_MESSAGE:
2940 GstMessage *msg = NULL;
2942 gst_event_parse_sink_message (event, &msg);
2944 gst_element_post_message (GST_ELEMENT_CAST (basesink), msg);
2951 gst_event_unref (event);
2957 gst_base_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
2959 GstBaseSink *basesink;
2960 gboolean result = TRUE;
2961 GstBaseSinkClass *bclass;
2963 basesink = GST_BASE_SINK_CAST (parent);
2964 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2966 GST_DEBUG_OBJECT (basesink, "received event %p %" GST_PTR_FORMAT, event,
2969 switch (GST_EVENT_TYPE (event)) {
2970 case GST_EVENT_FLUSH_STOP:
2971 /* special case for this serialized event because we don't want to grab
2972 * the PREROLL lock or check if we were flushing */
2974 result = bclass->event (basesink, event);
2977 if (GST_EVENT_IS_SERIALIZED (event)) {
2978 GST_BASE_SINK_PREROLL_LOCK (basesink);
2979 if (G_UNLIKELY (basesink->flushing))
2982 if (G_UNLIKELY (basesink->priv->received_eos))
2986 result = bclass->event (basesink, event);
2988 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
2991 result = bclass->event (basesink, event);
3001 GST_DEBUG_OBJECT (basesink, "we are flushing");
3002 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3003 gst_event_unref (event);
3010 GST_DEBUG_OBJECT (basesink, "Event received after EOS, dropping");
3011 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3012 gst_event_unref (event);
3018 /* default implementation to calculate the start and end
3019 * timestamps on a buffer, subclasses can override
3022 gst_base_sink_default_get_times (GstBaseSink * basesink, GstBuffer * buffer,
3023 GstClockTime * start, GstClockTime * end)
3025 GstClockTime timestamp, duration;
3027 /* first sync on DTS, else use PTS */
3028 timestamp = GST_BUFFER_DTS (buffer);
3029 if (!GST_CLOCK_TIME_IS_VALID (timestamp))
3030 timestamp = GST_BUFFER_PTS (buffer);
3032 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
3033 /* get duration to calculate end time */
3034 duration = GST_BUFFER_DURATION (buffer);
3035 if (GST_CLOCK_TIME_IS_VALID (duration)) {
3036 *end = timestamp + duration;
3042 /* must be called with PREROLL_LOCK */
3044 gst_base_sink_needs_preroll (GstBaseSink * basesink)
3046 gboolean is_prerolled, res;
3048 /* we have 2 cases where the PREROLL_LOCK is released:
3049 * 1) we are blocking in the PREROLL_LOCK and thus are prerolled.
3050 * 2) we are syncing on the clock
3052 is_prerolled = basesink->have_preroll || basesink->priv->received_eos;
3053 res = !is_prerolled;
3055 GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d => needs preroll: %d",
3056 basesink->have_preroll, basesink->priv->received_eos, res);
3061 /* with STREAM_LOCK, PREROLL_LOCK
3063 * Takes a buffer and compare the timestamps with the last segment.
3064 * If the buffer falls outside of the segment boundaries, drop it.
3065 * Else send the buffer for preroll and rendering.
3067 * This function takes ownership of the buffer.
3069 static GstFlowReturn
3070 gst_base_sink_chain_unlocked (GstBaseSink * basesink, GstPad * pad,
3073 GstBaseSinkClass *bclass;
3074 GstBaseSinkPrivate *priv = basesink->priv;
3076 GstClockTime start = GST_CLOCK_TIME_NONE, end = GST_CLOCK_TIME_NONE;
3077 GstSegment *segment;
3078 GstBuffer *sync_buf;
3080 gboolean late, step_end;
3082 if (G_UNLIKELY (basesink->flushing))
3085 if (G_UNLIKELY (priv->received_eos))
3088 if (GST_IS_BUFFER_LIST (obj)) {
3089 sync_buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0);
3090 g_assert (NULL != sync_buf);
3092 sync_buf = GST_BUFFER_CAST (obj);
3095 /* for code clarity */
3096 segment = &basesink->segment;
3098 if (G_UNLIKELY (!basesink->have_newsegment)) {
3101 sync = gst_base_sink_get_sync (basesink);
3103 GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
3104 (_("Internal data flow problem.")),
3105 ("Received buffer without a new-segment. Assuming timestamps start from 0."));
3108 /* this means this sink will assume timestamps start from 0 */
3109 GST_OBJECT_LOCK (basesink);
3112 basesink->segment.start = 0;
3113 basesink->segment.stop = -1;
3114 basesink->have_newsegment = TRUE;
3115 GST_OBJECT_UNLOCK (basesink);
3118 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3120 /* check if the buffer needs to be dropped, we first ask the subclass for the
3122 if (bclass->get_times)
3123 bclass->get_times (basesink, sync_buf, &start, &end);
3125 if (!GST_CLOCK_TIME_IS_VALID (start)) {
3126 /* if the subclass does not want sync, we use our own values so that we at
3127 * least clip the buffer to the segment */
3128 gst_base_sink_default_get_times (basesink, sync_buf, &start, &end);
3131 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
3132 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
3134 /* a dropped buffer does not participate in anything */
3135 if (GST_CLOCK_TIME_IS_VALID (start) && (segment->format == GST_FORMAT_TIME)) {
3136 if (G_UNLIKELY (!gst_segment_clip (segment,
3137 GST_FORMAT_TIME, start, end, NULL, NULL)))
3138 goto out_of_segment;
3145 /* synchronize this object, non syncable objects return OK
3147 ret = gst_base_sink_do_sync (basesink, GST_MINI_OBJECT_CAST (sync_buf),
3149 if (G_UNLIKELY (ret != GST_FLOW_OK))
3152 /* drop late buffers unconditionally, let's hope it's unlikely */
3153 if (G_UNLIKELY (late))
3156 /* read once, to get same value before and after */
3157 do_qos = g_atomic_int_get (&priv->qos_enabled);
3159 GST_DEBUG_OBJECT (basesink, "rendering object %p", obj);
3161 /* record rendering time for QoS and stats */
3163 gst_base_sink_do_render_stats (basesink, TRUE);
3165 if (!GST_IS_BUFFER_LIST (obj)) {
3166 /* For buffer lists do not set last buffer for now. */
3167 gst_base_sink_set_last_buffer (basesink, GST_BUFFER_CAST (obj));
3170 ret = bclass->render (basesink, GST_BUFFER_CAST (obj));
3172 if (bclass->render_list)
3173 ret = bclass->render_list (basesink, GST_BUFFER_LIST_CAST (obj));
3177 gst_base_sink_do_render_stats (basesink, FALSE);
3179 if (ret == GST_FLOW_STEP)
3182 if (G_UNLIKELY (basesink->flushing))
3189 /* the step ended, check if we need to activate a new step */
3190 GST_DEBUG_OBJECT (basesink, "step ended");
3191 stop_stepping (basesink, &basesink->segment, &priv->current_step,
3192 priv->current_rstart, priv->current_rstop, basesink->eos);
3196 gst_base_sink_perform_qos (basesink, late);
3198 GST_DEBUG_OBJECT (basesink, "object unref after render %p", obj);
3199 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3206 GST_DEBUG_OBJECT (basesink, "sink is flushing");
3207 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3208 return GST_FLOW_FLUSHING;
3212 GST_DEBUG_OBJECT (basesink, "we are EOS, dropping object, return EOS");
3213 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3214 return GST_FLOW_EOS;
3218 GST_DEBUG_OBJECT (basesink, "dropping buffer, out of clipping segment");
3219 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3224 GST_DEBUG_OBJECT (basesink, "do_sync returned %s", gst_flow_get_name (ret));
3230 GST_DEBUG_OBJECT (basesink, "buffer late, dropping");
3232 if (g_atomic_int_get (&priv->qos_enabled)) {
3233 GstMessage *qos_msg;
3234 GstClockTime timestamp, duration;
3236 timestamp = GST_BUFFER_TIMESTAMP (GST_BUFFER_CAST (sync_buf));
3237 duration = GST_BUFFER_DURATION (GST_BUFFER_CAST (sync_buf));
3239 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
3240 "qos: dropped buffer rt %" GST_TIME_FORMAT ", st %" GST_TIME_FORMAT
3241 ", ts %" GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT,
3242 GST_TIME_ARGS (priv->current_rstart),
3243 GST_TIME_ARGS (priv->current_sstart), GST_TIME_ARGS (timestamp),
3244 GST_TIME_ARGS (duration));
3245 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
3246 "qos: rendered %" G_GUINT64_FORMAT ", dropped %" G_GUINT64_FORMAT,
3247 priv->rendered, priv->dropped);
3250 gst_message_new_qos (GST_OBJECT_CAST (basesink), basesink->sync,
3251 priv->current_rstart, priv->current_sstart, timestamp, duration);
3252 gst_message_set_qos_values (qos_msg, priv->current_jitter, priv->avg_rate,
3254 gst_message_set_qos_stats (qos_msg, GST_FORMAT_BUFFERS, priv->rendered,
3256 gst_element_post_message (GST_ELEMENT_CAST (basesink), qos_msg);
3264 static GstFlowReturn
3265 gst_base_sink_chain_main (GstBaseSink * basesink, GstPad * pad, gpointer obj)
3267 GstFlowReturn result;
3269 if (G_UNLIKELY (basesink->pad_mode != GST_PAD_MODE_PUSH))
3272 GST_BASE_SINK_PREROLL_LOCK (basesink);
3273 result = gst_base_sink_chain_unlocked (basesink, pad, obj);
3274 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3282 GST_OBJECT_LOCK (pad);
3283 GST_WARNING_OBJECT (basesink,
3284 "Push on pad %s:%s, but it was not activated in push mode",
3285 GST_DEBUG_PAD_NAME (pad));
3286 GST_OBJECT_UNLOCK (pad);
3287 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3288 /* we don't post an error message this will signal to the peer
3289 * pushing that EOS is reached. */
3290 result = GST_FLOW_EOS;
3295 static GstFlowReturn
3296 gst_base_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
3298 GstBaseSink *basesink;
3300 basesink = GST_BASE_SINK (parent);
3302 return gst_base_sink_chain_main (basesink, pad, buf);
3305 static GstFlowReturn
3306 gst_base_sink_chain_list (GstPad * pad, GstObject * parent,
3307 GstBufferList * list)
3309 GstBaseSink *basesink;
3310 GstBaseSinkClass *bclass;
3311 GstFlowReturn result;
3313 basesink = GST_BASE_SINK (parent);
3314 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3316 if (G_LIKELY (bclass->render_list)) {
3317 result = gst_base_sink_chain_main (basesink, pad, list);
3322 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3324 len = gst_buffer_list_length (list);
3326 result = GST_FLOW_OK;
3327 for (i = 0; i < len; i++) {
3328 buffer = gst_buffer_list_get (list, i);
3329 result = gst_base_sink_chain_main (basesink, pad,
3330 gst_buffer_ref (buffer));
3331 if (result != GST_FLOW_OK)
3334 gst_buffer_list_unref (list);
3341 gst_base_sink_default_do_seek (GstBaseSink * sink, GstSegment * segment)
3343 gboolean res = TRUE;
3345 /* update our offset if the start/stop position was updated */
3346 if (segment->format == GST_FORMAT_BYTES) {
3347 segment->time = segment->start;
3348 } else if (segment->start == 0) {
3349 /* seek to start, we can implement a default for this. */
3353 GST_INFO_OBJECT (sink, "Can't do a default seek");
3359 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
3362 gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
3363 GstEvent * event, GstSegment * segment)
3365 /* By default, we try one of 2 things:
3366 * - For absolute seek positions, convert the requested position to our
3367 * configured processing format and place it in the output segment \
3368 * - For relative seek positions, convert our current (input) values to the
3369 * seek format, adjust by the relative seek offset and then convert back to
3370 * the processing format
3372 GstSeekType cur_type, stop_type;
3375 GstFormat seek_format;
3378 gboolean res = TRUE;
3380 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3381 &cur_type, &cur, &stop_type, &stop);
3383 if (seek_format == segment->format) {
3384 gst_segment_do_seek (segment, rate, seek_format, flags,
3385 cur_type, cur, stop_type, stop, &update);
3389 if (cur_type != GST_SEEK_TYPE_NONE) {
3390 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3392 gst_pad_query_convert (sink->sinkpad, seek_format, cur, segment->format,
3394 cur_type = GST_SEEK_TYPE_SET;
3397 if (res && stop_type != GST_SEEK_TYPE_NONE) {
3398 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3400 gst_pad_query_convert (sink->sinkpad, seek_format, stop,
3401 segment->format, &stop);
3402 stop_type = GST_SEEK_TYPE_SET;
3405 /* And finally, configure our output segment in the desired format */
3406 gst_segment_do_seek (segment, rate, segment->format, flags, cur_type, cur,
3407 stop_type, stop, &update);
3416 GST_DEBUG_OBJECT (sink, "undefined format given, seek aborted.");
3421 /* perform a seek, only executed in pull mode */
3423 gst_base_sink_perform_seek (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3427 GstFormat seek_format, dest_format;
3429 GstSeekType cur_type, stop_type;
3430 gboolean seekseg_configured = FALSE;
3432 gboolean update, res = TRUE;
3433 GstSegment seeksegment;
3435 dest_format = sink->segment.format;
3438 GST_DEBUG_OBJECT (sink, "performing seek with event %p", event);
3439 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3440 &cur_type, &cur, &stop_type, &stop);
3442 flush = flags & GST_SEEK_FLAG_FLUSH;
3444 GST_DEBUG_OBJECT (sink, "performing seek without event");
3449 GST_DEBUG_OBJECT (sink, "flushing upstream");
3450 gst_pad_push_event (pad, gst_event_new_flush_start ());
3451 gst_base_sink_flush_start (sink, pad);
3453 GST_DEBUG_OBJECT (sink, "pausing pulling thread");
3456 GST_PAD_STREAM_LOCK (pad);
3458 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
3459 * copy the current segment info into the temp segment that we can actually
3460 * attempt the seek with. We only update the real segment if the seek succeeds. */
3461 if (!seekseg_configured) {
3462 memcpy (&seeksegment, &sink->segment, sizeof (GstSegment));
3464 /* now configure the final seek segment */
3466 if (sink->segment.format != seek_format) {
3467 /* OK, here's where we give the subclass a chance to convert the relative
3468 * seek into an absolute one in the processing format. We set up any
3469 * absolute seek above, before taking the stream lock. */
3470 if (!gst_base_sink_default_prepare_seek_segment (sink, event,
3472 GST_DEBUG_OBJECT (sink,
3473 "Preparing the seek failed after flushing. " "Aborting seek");
3477 /* The seek format matches our processing format, no need to ask the
3478 * the subclass to configure the segment. */
3479 gst_segment_do_seek (&seeksegment, rate, seek_format, flags,
3480 cur_type, cur, stop_type, stop, &update);
3483 /* Else, no seek event passed, so we're just (re)starting the
3488 GST_DEBUG_OBJECT (sink, "segment configured from %" G_GINT64_FORMAT
3489 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
3490 seeksegment.start, seeksegment.stop, seeksegment.position);
3492 /* do the seek, segment.position contains the new position. */
3493 res = gst_base_sink_default_do_seek (sink, &seeksegment);
3498 GST_DEBUG_OBJECT (sink, "stop flushing upstream");
3499 gst_pad_push_event (pad, gst_event_new_flush_stop (TRUE));
3500 gst_base_sink_flush_stop (sink, pad, TRUE);
3501 } else if (res && sink->running) {
3502 /* we are running the current segment and doing a non-flushing seek,
3503 * close the segment first based on the position. */
3504 GST_DEBUG_OBJECT (sink, "closing running segment %" G_GINT64_FORMAT
3505 " to %" G_GINT64_FORMAT, sink->segment.start, sink->segment.position);
3508 /* The subclass must have converted the segment to the processing format
3510 if (res && seeksegment.format != dest_format) {
3511 GST_DEBUG_OBJECT (sink, "Subclass failed to prepare a seek segment "
3512 "in the correct format. Aborting seek.");
3516 /* if successful seek, we update our real segment and push
3517 * out the new segment. */
3519 gst_segment_copy_into (&seeksegment, &sink->segment);
3521 if (sink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3522 gst_element_post_message (GST_ELEMENT (sink),
3523 gst_message_new_segment_start (GST_OBJECT (sink),
3524 sink->segment.format, sink->segment.position));
3528 sink->priv->discont = TRUE;
3529 sink->running = TRUE;
3531 GST_PAD_STREAM_UNLOCK (pad);
3537 set_step_info (GstBaseSink * sink, GstStepInfo * current, GstStepInfo * pending,
3538 guint seqnum, GstFormat format, guint64 amount, gdouble rate,
3539 gboolean flush, gboolean intermediate)
3541 GST_OBJECT_LOCK (sink);
3542 pending->seqnum = seqnum;
3543 pending->format = format;
3544 pending->amount = amount;
3545 pending->position = 0;
3546 pending->rate = rate;
3547 pending->flush = flush;
3548 pending->intermediate = intermediate;
3549 pending->valid = TRUE;
3550 /* flush invalidates the current stepping segment */
3552 current->valid = FALSE;
3553 GST_OBJECT_UNLOCK (sink);
3557 gst_base_sink_perform_step (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3559 GstBaseSinkPrivate *priv;
3560 GstBaseSinkClass *bclass;
3561 gboolean flush, intermediate;
3566 GstStepInfo *pending, *current;
3567 GstMessage *message;
3569 bclass = GST_BASE_SINK_GET_CLASS (sink);
3572 GST_DEBUG_OBJECT (sink, "performing step with event %p", event);
3574 gst_event_parse_step (event, &format, &amount, &rate, &flush, &intermediate);
3575 seqnum = gst_event_get_seqnum (event);
3577 pending = &priv->pending_step;
3578 current = &priv->current_step;
3580 /* post message first */
3581 message = gst_message_new_step_start (GST_OBJECT (sink), FALSE, format,
3582 amount, rate, flush, intermediate);
3583 gst_message_set_seqnum (message, seqnum);
3584 gst_element_post_message (GST_ELEMENT (sink), message);
3587 /* we need to call ::unlock before locking PREROLL_LOCK
3588 * since we lock it before going into ::render */
3590 bclass->unlock (sink);
3592 GST_BASE_SINK_PREROLL_LOCK (sink);
3593 /* now that we have the PREROLL lock, clear our unlock request */
3594 if (bclass->unlock_stop)
3595 bclass->unlock_stop (sink);
3597 /* update the stepinfo and make it valid */
3598 set_step_info (sink, current, pending, seqnum, format, amount, rate, flush,
3601 if (sink->priv->async_enabled) {
3602 /* and we need to commit our state again on the next
3603 * prerolled buffer */
3604 sink->playing_async = TRUE;
3605 priv->pending_step.need_preroll = TRUE;
3606 sink->need_preroll = FALSE;
3607 gst_element_lost_state (GST_ELEMENT_CAST (sink));
3609 sink->priv->have_latency = TRUE;
3610 sink->need_preroll = FALSE;
3612 priv->current_sstart = GST_CLOCK_TIME_NONE;
3613 priv->current_sstop = GST_CLOCK_TIME_NONE;
3614 priv->eos_rtime = GST_CLOCK_TIME_NONE;
3615 priv->call_preroll = TRUE;
3616 gst_base_sink_set_last_buffer (sink, NULL);
3617 gst_base_sink_reset_qos (sink);
3619 if (sink->clock_id) {
3620 gst_clock_id_unschedule (sink->clock_id);
3623 if (sink->have_preroll) {
3624 GST_DEBUG_OBJECT (sink, "signal waiter");
3625 priv->step_unlock = TRUE;
3626 GST_BASE_SINK_PREROLL_SIGNAL (sink);
3628 GST_BASE_SINK_PREROLL_UNLOCK (sink);
3630 /* update the stepinfo and make it valid */
3631 set_step_info (sink, current, pending, seqnum, format, amount, rate, flush,
3641 gst_base_sink_loop (GstPad * pad)
3644 GstBaseSink *basesink;
3645 GstBuffer *buf = NULL;
3646 GstFlowReturn result;
3650 parent = GST_OBJECT_PARENT (pad);
3651 basesink = GST_BASE_SINK (parent);
3653 g_assert (basesink->pad_mode == GST_PAD_MODE_PULL);
3655 if ((blocksize = basesink->priv->blocksize) == 0)
3658 offset = basesink->segment.position;
3660 GST_DEBUG_OBJECT (basesink, "pulling %" G_GUINT64_FORMAT ", %u",
3663 result = gst_pad_pull_range (pad, offset, blocksize, &buf);
3664 if (G_UNLIKELY (result != GST_FLOW_OK))
3667 if (G_UNLIKELY (buf == NULL))
3670 offset += gst_buffer_get_size (buf);
3672 basesink->segment.position = offset;
3674 GST_BASE_SINK_PREROLL_LOCK (basesink);
3675 result = gst_base_sink_chain_unlocked (basesink, pad, buf);
3676 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3677 if (G_UNLIKELY (result != GST_FLOW_OK))
3685 GST_LOG_OBJECT (basesink, "pausing task, reason %s",
3686 gst_flow_get_name (result));
3687 gst_pad_pause_task (pad);
3688 if (result == GST_FLOW_EOS) {
3689 /* perform EOS logic */
3690 if (basesink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3691 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3692 gst_message_new_segment_done (GST_OBJECT_CAST (basesink),
3693 basesink->segment.format, basesink->segment.position));
3695 gst_base_sink_event (pad, parent, gst_event_new_eos ());
3697 } else if (result == GST_FLOW_NOT_LINKED || result <= GST_FLOW_EOS) {
3698 /* for fatal errors we post an error message, post the error
3699 * first so the app knows about the error first.
3700 * wrong-state is not a fatal error because it happens due to
3701 * flushing and posting an error message in that case is the
3702 * wrong thing to do, e.g. when basesrc is doing a flushing
3704 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
3705 (_("Internal data stream error.")),
3706 ("stream stopped, reason %s", gst_flow_get_name (result)));
3707 gst_base_sink_event (pad, parent, gst_event_new_eos ());
3713 GST_LOG_OBJECT (basesink, "no buffer, pausing");
3714 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
3715 (_("Internal data flow error.")), ("element returned NULL buffer"));
3716 result = GST_FLOW_ERROR;
3722 gst_base_sink_set_flushing (GstBaseSink * basesink, GstPad * pad,
3725 GstBaseSinkClass *bclass;
3727 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3730 /* unlock any subclasses, we need to do this before grabbing the
3731 * PREROLL_LOCK since we hold this lock before going into ::render. */
3733 bclass->unlock (basesink);
3736 GST_BASE_SINK_PREROLL_LOCK (basesink);
3737 basesink->flushing = flushing;
3739 /* step 1, now that we have the PREROLL lock, clear our unlock request */
3740 if (bclass->unlock_stop)
3741 bclass->unlock_stop (basesink);
3743 /* set need_preroll before we unblock the clock. If the clock is unblocked
3744 * before timing out, we can reuse the buffer for preroll. */
3745 basesink->need_preroll = TRUE;
3747 /* step 2, unblock clock sync (if any) or any other blocking thing */
3748 if (basesink->clock_id) {
3749 gst_clock_id_unschedule (basesink->clock_id);
3752 /* flush out the data thread if it's locked in finish_preroll, this will
3753 * also flush out the EOS state */
3754 GST_DEBUG_OBJECT (basesink,
3755 "flushing out data thread, need preroll to TRUE");
3757 /* we can't have EOS anymore now */
3758 basesink->eos = FALSE;
3759 basesink->priv->received_eos = FALSE;
3760 basesink->have_preroll = FALSE;
3761 basesink->priv->step_unlock = FALSE;
3762 /* can't report latency anymore until we preroll again */
3763 if (basesink->priv->async_enabled) {
3764 GST_OBJECT_LOCK (basesink);
3765 basesink->priv->have_latency = FALSE;
3766 GST_OBJECT_UNLOCK (basesink);
3768 /* and signal any waiters now */
3769 GST_BASE_SINK_PREROLL_SIGNAL (basesink);
3771 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3777 gst_base_sink_default_activate_pull (GstBaseSink * basesink, gboolean active)
3783 result = gst_pad_start_task (basesink->sinkpad,
3784 (GstTaskFunction) gst_base_sink_loop, basesink->sinkpad);
3786 /* step 2, make sure streaming finishes */
3787 result = gst_pad_stop_task (basesink->sinkpad);
3794 gst_base_sink_pad_activate (GstPad * pad, GstObject * parent)
3796 gboolean result = FALSE;
3797 GstBaseSink *basesink;
3801 basesink = GST_BASE_SINK (parent);
3803 GST_DEBUG_OBJECT (basesink, "Trying pull mode first");
3805 gst_base_sink_set_flushing (basesink, pad, FALSE);
3807 /* we need to have the pull mode enabled */
3808 if (!basesink->can_activate_pull) {
3809 GST_DEBUG_OBJECT (basesink, "pull mode disabled");
3813 /* check if downstreams supports pull mode at all */
3814 query = gst_query_new_scheduling ();
3816 if (!gst_pad_peer_query (pad, query)) {
3817 gst_query_unref (query);
3818 GST_DEBUG_OBJECT (basesink, "peer query faild, no pull mode");
3822 /* parse result of the query */
3823 pull_mode = gst_query_has_scheduling_mode (query, GST_PAD_MODE_PULL);
3824 gst_query_unref (query);
3827 GST_DEBUG_OBJECT (basesink, "pull mode not supported");
3831 /* set the pad mode before starting the task so that it's in the
3832 * correct state for the new thread. also the sink set_caps and get_caps
3833 * function checks this */
3834 basesink->pad_mode = GST_PAD_MODE_PULL;
3836 /* we first try to negotiate a format so that when we try to activate
3837 * downstream, it knows about our format */
3838 if (!gst_base_sink_negotiate_pull (basesink)) {
3839 GST_DEBUG_OBJECT (basesink, "failed to negotiate in pull mode");
3843 /* ok activate now */
3844 if (!gst_pad_activate_mode (pad, GST_PAD_MODE_PULL, TRUE)) {
3845 /* clear any pending caps */
3846 GST_OBJECT_LOCK (basesink);
3847 gst_caps_replace (&basesink->priv->caps, NULL);
3848 GST_OBJECT_UNLOCK (basesink);
3849 GST_DEBUG_OBJECT (basesink, "failed to activate in pull mode");
3853 GST_DEBUG_OBJECT (basesink, "Success activating pull mode");
3857 /* push mode fallback */
3859 GST_DEBUG_OBJECT (basesink, "Falling back to push mode");
3860 if ((result = gst_pad_activate_mode (pad, GST_PAD_MODE_PUSH, TRUE))) {
3861 GST_DEBUG_OBJECT (basesink, "Success activating push mode");
3866 GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
3867 gst_base_sink_set_flushing (basesink, pad, TRUE);
3874 gst_base_sink_pad_activate_push (GstPad * pad, GstObject * parent,
3878 GstBaseSink *basesink;
3880 basesink = GST_BASE_SINK (parent);
3883 if (!basesink->can_activate_push) {
3885 basesink->pad_mode = GST_PAD_MODE_NONE;
3888 basesink->pad_mode = GST_PAD_MODE_PUSH;
3891 if (G_UNLIKELY (basesink->pad_mode != GST_PAD_MODE_PUSH)) {
3892 g_warning ("Internal GStreamer activation error!!!");
3895 gst_base_sink_set_flushing (basesink, pad, TRUE);
3897 basesink->pad_mode = GST_PAD_MODE_NONE;
3905 gst_base_sink_negotiate_pull (GstBaseSink * basesink)
3912 /* this returns the intersection between our caps and the peer caps. If there
3913 * is no peer, it returns NULL and we can't operate in pull mode so we can
3914 * fail the negotiation. */
3915 caps = gst_pad_get_allowed_caps (GST_BASE_SINK_PAD (basesink));
3916 if (caps == NULL || gst_caps_is_empty (caps))
3917 goto no_caps_possible;
3919 GST_DEBUG_OBJECT (basesink, "allowed caps: %" GST_PTR_FORMAT, caps);
3921 if (gst_caps_is_any (caps)) {
3922 GST_DEBUG_OBJECT (basesink, "caps were ANY after fixating, "
3924 /* neither side has template caps in this case, so they are prepared for
3925 pull() without setcaps() */
3929 caps = gst_base_sink_fixate (basesink, caps);
3930 GST_DEBUG_OBJECT (basesink, "fixated to: %" GST_PTR_FORMAT, caps);
3932 if (gst_caps_is_fixed (caps)) {
3933 if (!gst_pad_send_event (GST_BASE_SINK_PAD (basesink),
3934 gst_event_new_caps (caps)))
3935 goto could_not_set_caps;
3941 gst_caps_unref (caps);
3947 GST_INFO_OBJECT (basesink, "Pipeline could not agree on caps");
3948 GST_DEBUG_OBJECT (basesink, "get_allowed_caps() returned EMPTY");
3950 gst_caps_unref (caps);
3955 GST_INFO_OBJECT (basesink, "Could not set caps: %" GST_PTR_FORMAT, caps);
3956 gst_caps_unref (caps);
3961 /* this won't get called until we implement an activate function */
3963 gst_base_sink_pad_activate_pull (GstPad * pad, GstObject * parent,
3966 gboolean result = FALSE;
3967 GstBaseSink *basesink;
3968 GstBaseSinkClass *bclass;
3970 basesink = GST_BASE_SINK (parent);
3971 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3976 /* we mark we have a newsegment here because pull based
3977 * mode works just fine without having a newsegment before the
3979 gst_segment_init (&basesink->segment, GST_FORMAT_BYTES);
3980 GST_OBJECT_LOCK (basesink);
3981 basesink->have_newsegment = TRUE;
3982 GST_OBJECT_UNLOCK (basesink);
3984 /* get the peer duration in bytes */
3985 result = gst_pad_peer_query_duration (pad, GST_FORMAT_BYTES, &duration);
3987 GST_DEBUG_OBJECT (basesink,
3988 "setting duration in bytes to %" G_GINT64_FORMAT, duration);
3989 basesink->segment.duration = duration;
3991 GST_DEBUG_OBJECT (basesink, "unknown duration");
3994 if (bclass->activate_pull)
3995 result = bclass->activate_pull (basesink, TRUE);
4000 goto activate_failed;
4003 if (G_UNLIKELY (basesink->pad_mode != GST_PAD_MODE_PULL)) {
4004 g_warning ("Internal GStreamer activation error!!!");
4007 result = gst_base_sink_set_flushing (basesink, pad, TRUE);
4008 if (bclass->activate_pull)
4009 result &= bclass->activate_pull (basesink, FALSE);
4010 basesink->pad_mode = GST_PAD_MODE_NONE;
4019 /* reset, as starting the thread failed */
4020 basesink->pad_mode = GST_PAD_MODE_NONE;
4022 GST_ERROR_OBJECT (basesink, "subclass failed to activate in pull mode");
4028 gst_base_sink_pad_activate_mode (GstPad * pad, GstObject * parent,
4029 GstPadMode mode, gboolean active)
4034 case GST_PAD_MODE_PULL:
4035 res = gst_base_sink_pad_activate_pull (pad, parent, active);
4037 case GST_PAD_MODE_PUSH:
4038 res = gst_base_sink_pad_activate_push (pad, parent, active);
4041 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
4048 /* send an event to our sinkpad peer. */
4050 gst_base_sink_send_event (GstElement * element, GstEvent * event)
4053 GstBaseSink *basesink = GST_BASE_SINK (element);
4054 gboolean forward, result = TRUE;
4057 GST_OBJECT_LOCK (element);
4058 /* get the pad and the scheduling mode */
4059 pad = gst_object_ref (basesink->sinkpad);
4060 mode = basesink->pad_mode;
4061 GST_OBJECT_UNLOCK (element);
4063 /* only push UPSTREAM events upstream */
4064 forward = GST_EVENT_IS_UPSTREAM (event);
4066 GST_DEBUG_OBJECT (basesink, "handling event %p %" GST_PTR_FORMAT, event,
4069 switch (GST_EVENT_TYPE (event)) {
4070 case GST_EVENT_LATENCY:
4072 GstClockTime latency;
4074 gst_event_parse_latency (event, &latency);
4076 /* store the latency. We use this to adjust the running_time before syncing
4077 * it to the clock. */
4078 GST_OBJECT_LOCK (element);
4079 basesink->priv->latency = latency;
4080 if (!basesink->priv->have_latency)
4082 GST_OBJECT_UNLOCK (element);
4083 GST_DEBUG_OBJECT (basesink, "latency set to %" GST_TIME_FORMAT,
4084 GST_TIME_ARGS (latency));
4086 /* We forward this event so that all elements know about the global pipeline
4087 * latency. This is interesting for an element when it wants to figure out
4088 * when a particular piece of data will be rendered. */
4091 case GST_EVENT_SEEK:
4092 /* in pull mode we will execute the seek */
4093 if (mode == GST_PAD_MODE_PULL)
4094 result = gst_base_sink_perform_seek (basesink, pad, event);
4096 case GST_EVENT_STEP:
4097 result = gst_base_sink_perform_step (basesink, pad, event);
4105 result = gst_pad_push_event (pad, event);
4107 /* not forwarded, unref the event */
4108 gst_event_unref (event);
4111 gst_object_unref (pad);
4113 GST_DEBUG_OBJECT (basesink, "handled event %p %" GST_PTR_FORMAT ": %d", event,
4120 gst_base_sink_get_position (GstBaseSink * basesink, GstFormat format,
4121 gint64 * cur, gboolean * upstream)
4123 GstClock *clock = NULL;
4124 gboolean res = FALSE;
4126 GstSegment *segment;
4127 GstClockTime now, latency;
4128 GstClockTimeDiff base_time;
4129 gint64 time, base, duration;
4132 gboolean last_seen, with_clock, in_paused;
4134 GST_OBJECT_LOCK (basesink);
4135 /* we can only get the segment when we are not NULL or READY */
4136 if (!basesink->have_newsegment)
4140 /* when not in PLAYING or when we're busy with a state change, we
4141 * cannot read from the clock so we report time based on the
4142 * last seen timestamp. */
4143 if (GST_STATE (basesink) != GST_STATE_PLAYING ||
4144 GST_STATE_PENDING (basesink) != GST_STATE_VOID_PENDING) {
4148 segment = &basesink->segment;
4150 /* get the format in the segment */
4151 oformat = segment->format;
4153 /* report with last seen position when EOS */
4154 last_seen = basesink->eos;
4156 /* assume we will use the clock for getting the current position */
4158 if (basesink->sync == FALSE)
4161 /* and we need a clock */
4162 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (basesink)) == NULL))
4165 gst_object_ref (clock);
4167 /* mainloop might be querying position when going to playing async,
4168 * while (audio) rendering might be quickly advancing stream position,
4169 * so use clock asap rather than last reported position */
4170 if (in_paused && with_clock && g_atomic_int_get (&basesink->priv->to_playing)) {
4171 GST_DEBUG_OBJECT (basesink, "going to PLAYING, so not PAUSED");
4175 /* collect all data we need holding the lock */
4176 if (GST_CLOCK_TIME_IS_VALID (segment->time))
4177 time = segment->time;
4181 if (GST_CLOCK_TIME_IS_VALID (segment->stop))
4182 duration = segment->stop - segment->start;
4186 base = segment->base;
4187 rate = segment->rate * segment->applied_rate;
4188 latency = basesink->priv->latency;
4190 if (oformat == GST_FORMAT_TIME) {
4193 start = basesink->priv->current_sstart;
4194 stop = basesink->priv->current_sstop;
4197 /* in paused we use the last position as a lower bound */
4198 if (stop == -1 || segment->rate > 0.0)
4203 /* in playing, use last stop time as upper bound */
4204 if (start == -1 || segment->rate > 0.0)
4210 /* convert last stop to stream time */
4211 last = gst_segment_to_stream_time (segment, oformat, segment->position);
4215 /* in paused, use start_time */
4216 base_time = GST_ELEMENT_START_TIME (basesink);
4217 GST_DEBUG_OBJECT (basesink, "in paused, using start time %" GST_TIME_FORMAT,
4218 GST_TIME_ARGS (base_time));
4219 } else if (with_clock) {
4220 /* else use clock when needed */
4221 base_time = GST_ELEMENT_CAST (basesink)->base_time;
4222 GST_DEBUG_OBJECT (basesink, "using clock and base time %" GST_TIME_FORMAT,
4223 GST_TIME_ARGS (base_time));
4225 /* else, no sync or clock -> no base time */
4226 GST_DEBUG_OBJECT (basesink, "no sync or no clock");
4230 /* no base_time, we can't calculate running_time, use last seem timestamp to report
4232 if (base_time == -1)
4235 /* need to release the object lock before we can get the time,
4236 * a clock might take the LOCK of the provider, which could be
4237 * a basesink subclass. */
4238 GST_OBJECT_UNLOCK (basesink);
4241 /* in EOS or when no valid stream_time, report the value of last seen
4244 /* no timestamp, we need to ask upstream */
4245 GST_DEBUG_OBJECT (basesink, "no last seen timestamp, asking upstream");
4250 GST_DEBUG_OBJECT (basesink, "using last seen timestamp %" GST_TIME_FORMAT,
4251 GST_TIME_ARGS (last));
4254 if (oformat != GST_FORMAT_TIME) {
4255 /* convert base, time and duration to time */
4256 if (!gst_pad_query_convert (basesink->sinkpad, oformat, base,
4257 GST_FORMAT_TIME, &base))
4258 goto convert_failed;
4259 if (!gst_pad_query_convert (basesink->sinkpad, oformat, duration,
4260 GST_FORMAT_TIME, &duration))
4261 goto convert_failed;
4262 if (!gst_pad_query_convert (basesink->sinkpad, oformat, time,
4263 GST_FORMAT_TIME, &time))
4264 goto convert_failed;
4265 if (!gst_pad_query_convert (basesink->sinkpad, oformat, last,
4266 GST_FORMAT_TIME, &last))
4267 goto convert_failed;
4269 /* assume time format from now on */
4270 oformat = GST_FORMAT_TIME;
4273 if (!in_paused && with_clock) {
4274 now = gst_clock_get_time (clock);
4280 /* subtract base time and base time from the clock time.
4281 * Make sure we don't go negative. This is the current time in
4282 * the segment which we need to scale with the combined
4283 * rate and applied rate. */
4285 base_time += latency;
4286 if (GST_CLOCK_DIFF (base_time, now) < 0)
4289 /* for negative rates we need to count back from the segment
4294 *cur = time + gst_guint64_to_gdouble (now - base_time) * rate;
4297 /* never report less than segment values in paused */
4299 *cur = MAX (last, *cur);
4301 /* never report more than last seen position in playing */
4303 *cur = MIN (last, *cur);
4306 GST_DEBUG_OBJECT (basesink,
4307 "now %" GST_TIME_FORMAT " - base_time %" GST_TIME_FORMAT " - base %"
4308 GST_TIME_FORMAT " + time %" GST_TIME_FORMAT " last %" GST_TIME_FORMAT,
4309 GST_TIME_ARGS (now), GST_TIME_ARGS (base_time), GST_TIME_ARGS (base),
4310 GST_TIME_ARGS (time), GST_TIME_ARGS (last));
4313 if (oformat != format) {
4314 /* convert to final format */
4315 if (!gst_pad_query_convert (basesink->sinkpad, oformat, *cur, format, cur))
4316 goto convert_failed;
4322 GST_DEBUG_OBJECT (basesink, "res: %d, POSITION: %" GST_TIME_FORMAT,
4323 res, GST_TIME_ARGS (*cur));
4326 gst_object_unref (clock);
4333 /* in NULL or READY we always return FALSE and -1 */
4334 GST_DEBUG_OBJECT (basesink, "position in wrong state, return -1");
4337 GST_OBJECT_UNLOCK (basesink);
4342 GST_DEBUG_OBJECT (basesink, "convert failed, try upstream");
4350 gst_base_sink_get_duration (GstBaseSink * basesink, GstFormat format,
4351 gint64 * dur, gboolean * upstream)
4353 gboolean res = FALSE;
4355 if (basesink->pad_mode == GST_PAD_MODE_PULL) {
4358 /* get the duration in bytes, in pull mode that's all we are sure to
4359 * know. We have to explicitly get this value from upstream instead of
4360 * using our cached value because it might change. Duration caching
4361 * should be done at a higher level. */
4363 gst_pad_peer_query_duration (basesink->sinkpad, GST_FORMAT_BYTES,
4366 basesink->segment.duration = uduration;
4367 if (format != GST_FORMAT_BYTES) {
4368 /* convert to the requested format */
4370 gst_pad_query_convert (basesink->sinkpad, GST_FORMAT_BYTES,
4371 uduration, format, dur);
4385 default_element_query (GstElement * element, GstQuery * query)
4387 gboolean res = FALSE;
4389 GstBaseSink *basesink = GST_BASE_SINK (element);
4391 switch (GST_QUERY_TYPE (query)) {
4392 case GST_QUERY_POSITION:
4396 gboolean upstream = FALSE;
4398 gst_query_parse_position (query, &format, NULL);
4400 GST_DEBUG_OBJECT (basesink, "position query in format %s",
4401 gst_format_get_name (format));
4403 /* first try to get the position based on the clock */
4405 gst_base_sink_get_position (basesink, format, &cur, &upstream))) {
4406 gst_query_set_position (query, format, cur);
4407 } else if (upstream) {
4408 /* fallback to peer query */
4409 res = gst_pad_peer_query (basesink->sinkpad, query);
4412 /* we can handle a few things if upstream failed */
4413 if (format == GST_FORMAT_PERCENT) {
4416 res = gst_base_sink_get_position (basesink, GST_FORMAT_TIME, &cur,
4418 if (!res && upstream) {
4420 gst_pad_peer_query_position (basesink->sinkpad, GST_FORMAT_TIME,
4424 res = gst_base_sink_get_duration (basesink, GST_FORMAT_TIME, &dur,
4426 if (!res && upstream) {
4428 gst_pad_peer_query_duration (basesink->sinkpad,
4429 GST_FORMAT_TIME, &dur);
4435 pos = gst_util_uint64_scale (100 * GST_FORMAT_PERCENT_SCALE, cur,
4437 gst_query_set_position (query, GST_FORMAT_PERCENT, pos);
4443 case GST_QUERY_DURATION:
4447 gboolean upstream = FALSE;
4449 gst_query_parse_duration (query, &format, NULL);
4451 GST_DEBUG_OBJECT (basesink, "duration query in format %s",
4452 gst_format_get_name (format));
4455 gst_base_sink_get_duration (basesink, format, &dur, &upstream))) {
4456 gst_query_set_duration (query, format, dur);
4457 } else if (upstream) {
4458 /* fallback to peer query */
4459 res = gst_pad_peer_query (basesink->sinkpad, query);
4462 /* we can handle a few things if upstream failed */
4463 if (format == GST_FORMAT_PERCENT) {
4464 gst_query_set_duration (query, GST_FORMAT_PERCENT,
4465 GST_FORMAT_PERCENT_MAX);
4471 case GST_QUERY_LATENCY:
4473 gboolean live, us_live;
4474 GstClockTime min, max;
4476 if ((res = gst_base_sink_query_latency (basesink, &live, &us_live, &min,
4478 gst_query_set_latency (query, live, min, max);
4482 case GST_QUERY_JITTER:
4484 case GST_QUERY_RATE:
4485 /* gst_query_set_rate (query, basesink->segment_rate); */
4488 case GST_QUERY_SEGMENT:
4490 if (basesink->pad_mode == GST_PAD_MODE_PULL) {
4491 gst_query_set_segment (query, basesink->segment.rate,
4492 GST_FORMAT_TIME, basesink->segment.start, basesink->segment.stop);
4495 res = gst_pad_peer_query (basesink->sinkpad, query);
4499 case GST_QUERY_SEEKING:
4500 case GST_QUERY_CONVERT:
4501 case GST_QUERY_FORMATS:
4503 res = gst_pad_peer_query (basesink->sinkpad, query);
4506 GST_DEBUG_OBJECT (basesink, "query %s returns %d",
4507 GST_QUERY_TYPE_NAME (query), res);
4513 gst_base_sink_default_query (GstBaseSink * basesink, GstQuery * query)
4516 GstBaseSinkClass *bclass;
4518 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4520 switch (GST_QUERY_TYPE (query)) {
4521 case GST_QUERY_ALLOCATION:
4523 if (bclass->propose_allocation)
4524 res = bclass->propose_allocation (basesink, query);
4529 case GST_QUERY_CAPS:
4531 GstCaps *caps, *filter;
4533 gst_query_parse_caps (query, &filter);
4534 caps = gst_base_sink_query_caps (basesink, basesink->sinkpad, filter);
4535 gst_query_set_caps_result (query, caps);
4536 gst_caps_unref (caps);
4540 case GST_QUERY_ACCEPT_CAPS:
4542 GstCaps *caps, *allowed;
4545 /* slightly faster than the default implementation */
4546 gst_query_parse_accept_caps (query, &caps);
4547 allowed = gst_base_sink_query_caps (basesink, basesink->sinkpad, NULL);
4548 subset = gst_caps_is_subset (caps, allowed);
4549 gst_query_set_accept_caps_result (query, subset);
4553 case GST_QUERY_DRAIN:
4558 gst_pad_query_default (basesink->sinkpad, GST_OBJECT_CAST (basesink),
4566 gst_base_sink_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
4568 GstBaseSink *basesink;
4569 GstBaseSinkClass *bclass;
4572 basesink = GST_BASE_SINK_CAST (parent);
4573 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4576 res = bclass->query (basesink, query);
4583 static GstStateChangeReturn
4584 gst_base_sink_change_state (GstElement * element, GstStateChange transition)
4586 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
4587 GstBaseSink *basesink = GST_BASE_SINK (element);
4588 GstBaseSinkClass *bclass;
4589 GstBaseSinkPrivate *priv;
4591 priv = basesink->priv;
4593 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4595 switch (transition) {
4596 case GST_STATE_CHANGE_NULL_TO_READY:
4598 if (!bclass->start (basesink))
4601 case GST_STATE_CHANGE_READY_TO_PAUSED:
4602 /* need to complete preroll before this state change completes, there
4603 * is no data flow in READY so we can safely assume we need to preroll. */
4604 GST_BASE_SINK_PREROLL_LOCK (basesink);
4605 GST_DEBUG_OBJECT (basesink, "READY to PAUSED");
4606 basesink->have_newsegment = FALSE;
4607 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
4608 basesink->offset = 0;
4609 basesink->have_preroll = FALSE;
4610 priv->step_unlock = FALSE;
4611 basesink->need_preroll = TRUE;
4612 basesink->playing_async = TRUE;
4613 basesink->priv->reset_time = FALSE;
4614 priv->current_sstart = GST_CLOCK_TIME_NONE;
4615 priv->current_sstop = GST_CLOCK_TIME_NONE;
4616 priv->eos_rtime = GST_CLOCK_TIME_NONE;
4618 basesink->eos = FALSE;
4619 priv->received_eos = FALSE;
4620 gst_base_sink_reset_qos (basesink);
4621 priv->commited = FALSE;
4622 priv->call_preroll = TRUE;
4623 priv->current_step.valid = FALSE;
4624 priv->pending_step.valid = FALSE;
4625 if (priv->async_enabled) {
4626 GST_DEBUG_OBJECT (basesink, "doing async state change");
4627 /* when async enabled, post async-start message and return ASYNC from
4628 * the state change function */
4629 ret = GST_STATE_CHANGE_ASYNC;
4630 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4631 gst_message_new_async_start (GST_OBJECT_CAST (basesink)));
4633 priv->have_latency = TRUE;
4635 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
4637 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
4638 GST_BASE_SINK_PREROLL_LOCK (basesink);
4639 g_atomic_int_set (&basesink->priv->to_playing, TRUE);
4640 if (!gst_base_sink_needs_preroll (basesink)) {
4641 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, don't need preroll");
4642 /* no preroll needed anymore now. */
4643 basesink->playing_async = FALSE;
4644 basesink->need_preroll = FALSE;
4645 if (basesink->eos) {
4646 GstMessage *message;
4648 /* need to post EOS message here */
4649 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
4650 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
4651 gst_message_set_seqnum (message, basesink->priv->seqnum);
4652 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
4654 GST_DEBUG_OBJECT (basesink, "signal preroll");
4655 GST_BASE_SINK_PREROLL_SIGNAL (basesink);
4658 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, we are not prerolled");
4659 basesink->need_preroll = TRUE;
4660 basesink->playing_async = TRUE;
4661 priv->call_preroll = TRUE;
4662 priv->commited = FALSE;
4663 if (priv->async_enabled) {
4664 GST_DEBUG_OBJECT (basesink, "doing async state change");
4665 ret = GST_STATE_CHANGE_ASYNC;
4666 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4667 gst_message_new_async_start (GST_OBJECT_CAST (basesink)));
4670 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
4677 GstStateChangeReturn bret;
4679 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4680 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
4681 goto activate_failed;
4684 switch (transition) {
4685 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
4686 /* completed transition, so need not be marked any longer
4687 * And it should be unmarked, since e.g. losing our position upon flush
4688 * does not really change state to PAUSED ... */
4689 g_atomic_int_set (&basesink->priv->to_playing, FALSE);
4691 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
4692 g_atomic_int_set (&basesink->priv->to_playing, FALSE);
4693 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED");
4694 /* FIXME, make sure we cannot enter _render first */
4696 /* we need to call ::unlock before locking PREROLL_LOCK
4697 * since we lock it before going into ::render */
4699 bclass->unlock (basesink);
4701 GST_BASE_SINK_PREROLL_LOCK (basesink);
4702 GST_DEBUG_OBJECT (basesink, "got preroll lock");
4703 /* now that we have the PREROLL lock, clear our unlock request */
4704 if (bclass->unlock_stop)
4705 bclass->unlock_stop (basesink);
4707 /* we need preroll again and we set the flag before unlocking the clockid
4708 * because if the clockid is unlocked before a current buffer expired, we
4709 * can use that buffer to preroll with */
4710 basesink->need_preroll = TRUE;
4712 if (basesink->clock_id) {
4713 GST_DEBUG_OBJECT (basesink, "unschedule clock");
4714 gst_clock_id_unschedule (basesink->clock_id);
4717 /* if we don't have a preroll buffer we need to wait for a preroll and
4719 if (!gst_base_sink_needs_preroll (basesink)) {
4720 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, we are prerolled");
4721 basesink->playing_async = FALSE;
4723 if (GST_STATE_TARGET (GST_ELEMENT (basesink)) <= GST_STATE_READY) {
4724 GST_DEBUG_OBJECT (basesink, "element is <= READY");
4725 ret = GST_STATE_CHANGE_SUCCESS;
4727 GST_DEBUG_OBJECT (basesink,
4728 "PLAYING to PAUSED, we are not prerolled");
4729 basesink->playing_async = TRUE;
4730 priv->commited = FALSE;
4731 priv->call_preroll = TRUE;
4732 if (priv->async_enabled) {
4733 GST_DEBUG_OBJECT (basesink, "doing async state change");
4734 ret = GST_STATE_CHANGE_ASYNC;
4735 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4736 gst_message_new_async_start (GST_OBJECT_CAST (basesink)));
4740 GST_DEBUG_OBJECT (basesink, "rendered: %" G_GUINT64_FORMAT
4741 ", dropped: %" G_GUINT64_FORMAT, priv->rendered, priv->dropped);
4743 gst_base_sink_reset_qos (basesink);
4744 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
4746 case GST_STATE_CHANGE_PAUSED_TO_READY:
4747 GST_BASE_SINK_PREROLL_LOCK (basesink);
4748 /* start by resetting our position state with the object lock so that the
4749 * position query gets the right idea. We do this before we post the
4750 * messages so that the message handlers pick this up. */
4751 GST_OBJECT_LOCK (basesink);
4752 basesink->have_newsegment = FALSE;
4753 priv->current_sstart = GST_CLOCK_TIME_NONE;
4754 priv->current_sstop = GST_CLOCK_TIME_NONE;
4755 priv->have_latency = FALSE;
4756 if (priv->cached_clock_id) {
4757 gst_clock_id_unref (priv->cached_clock_id);
4758 priv->cached_clock_id = NULL;
4760 gst_caps_replace (&basesink->priv->caps, NULL);
4761 GST_OBJECT_UNLOCK (basesink);
4763 gst_base_sink_set_last_buffer (basesink, NULL);
4764 priv->call_preroll = FALSE;
4766 if (!priv->commited) {
4767 if (priv->async_enabled) {
4768 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, posting async-done");
4770 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4771 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
4772 GST_STATE_PLAYING, GST_STATE_PAUSED, GST_STATE_READY));
4774 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4775 gst_message_new_async_done (GST_OBJECT_CAST (basesink), FALSE));
4777 priv->commited = TRUE;
4779 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, don't need_preroll");
4781 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
4783 case GST_STATE_CHANGE_READY_TO_NULL:
4785 if (!bclass->stop (basesink)) {
4786 GST_WARNING_OBJECT (basesink, "failed to stop");
4789 gst_base_sink_set_last_buffer (basesink, NULL);
4790 priv->call_preroll = FALSE;
4801 GST_DEBUG_OBJECT (basesink, "failed to start");
4802 return GST_STATE_CHANGE_FAILURE;
4806 GST_DEBUG_OBJECT (basesink,
4807 "element failed to change states -- activation problem?");
4808 return GST_STATE_CHANGE_FAILURE;